The Battle for Wesnoth  1.17.0-dev
SDLMain.mm
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /* SDLMain.m - main entry point for our Cocoa-ized SDL app
16  Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
17  Non-NIB-Code & other changes: Max Horn <max@quendi.de>
18  Edited a lot for Wesnoth by Ben Anderman <ben@happyspork.com>
19 */
20 
21 #import "SDL.h"
22 #import "SDLMain.h"
23 #include <vector>
24 
25 extern "C" int wesnoth_main(int argc, char **argv);
26 static std::vector<char*> gArgs;
27 
28 @interface WesnothSDLApplication : NSApplication
29 @end
30 
31 @implementation WesnothSDLApplication
32 /* Invoked from the Quit menu item */
33 - (void)terminate:(id)sender
34 {
35  (void) sender;
36  /* Post a SDL_QUIT event */
37  SDL_Event event;
38  event.type = SDL_QUIT;
39  SDL_PushEvent(&event);
40 }
41 
42 - (BOOL)_handleKeyEquivalent:(NSEvent *)theEvent
43 {
44  [[super mainMenu] performKeyEquivalent:theEvent];
45  return YES;
46 }
47 
48 - (void) sendEvent:(NSEvent *)event
49 {
50  if(NSEventTypeKeyDown == [event type] || NSEventTypeKeyUp == [event type])
51  {
52  if([event modifierFlags] & NSEventModifierFlagCommand)
53  {
54  [super sendEvent: event];
55  }
56  } else {
57  [super sendEvent: event];
58  }
59 }
60 @end
61 
62 /* The main class of the application, the application's delegate */
63 @implementation SDLMain
64 
65 - (IBAction) openHomepage:(id)sender
66 {
67  (void) sender;
68  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.wesnoth.org/"]];
69 }
70 
71 - (IBAction) openChangelog:(id)sender
72 {
73  (void) sender;
74  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/wesnoth/wesnoth/blob/master/changelog.md"]];
75 }
76 
77 /* Called when the internal event loop has just started running */
78 - (void) applicationDidFinishLaunching: (NSNotification *) note
79 {
80  (void) note;
81  /* This makes SDL give events to Cocoa, so it can handle things like command+h to hide, etc. */
82  setenv ("SDL_ENABLEAPPEVENTS", "1", 1);
83  setenv ("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
84 
85  /* Set config files for pango and fontconfig, so the data they need can be found */
86  setenv ("PANGO_RC_FILE", "./pangorc", 1);
87  setenv ("PANGO_SYSCONFDIR", ".", 1);
88  setenv ("PANGO_LIBDIR", ".", 1);
89  setenv ("FONTCONFIG_PATH", "./fonts/", 1);
90  setenv ("FONTCONFIG_FILE", "fonts.conf", 1);
91 
92  int status;
93 
94  /* Set the working directory to the .app's Resources directory */
95  chdir([[[NSBundle mainBundle] resourcePath] fileSystemRepresentation]);
96 
97  /* Hand off to main application code */
98  status = wesnoth_main(gArgs.size() - 1, gArgs.data());
99 
100  /* We're done, thank you for playing */
101  exit(status);
102 }
103 @end
104 
105 template<int sz>
106 bool str_eq(const char* str, const char(& cstr)[sz]) {
107  return strncmp(str, cstr, sz) == 0;
108 }
109 
110 #ifdef main
111 # undef main
112 #endif
113 
114 /* Main entry point to executable - should *not* be SDL_main! */
115 int main (int argc, char **argv)
116 {
117  gArgs.push_back(argv[0]); // Program name
118  for (int i = 1; i < argc; i++) {
119  // Filter out debug arguments that XCode might pass
120  if (str_eq(argv[i], "-ApplePersistenceIgnoreState")) {
121  i++; // Skip the argument
122  continue;
123  }
124  if (str_eq(argv[i], "-NSDocumentRevisionsDebugMode")) {
125  i++; // Skip the argument
126  continue;
127  }
128  // This is passed if launched by double-clicking
129  if (strncmp(argv[i], "-psn", 4) == 0) {
130  continue;
131  }
132  gArgs.push_back(argv[i]);
133  }
134  gArgs.push_back(nullptr);
135 
136  [WesnothSDLApplication sharedApplication];
137 
138  [[NSBundle mainBundle] loadNibNamed:@"SDLMain" owner:NSApp topLevelObjects:nil];
139 
140  [NSApp run];
141  return 0;
142 }
static std::vector< char * > gArgs
Definition: SDLMain.mm:26
int main(int argc, char **argv)
Definition: SDLMain.mm:115
std::size_t i
Definition: function.cpp:967
bool str_eq(const char *str, const char(&cstr)[sz])
Definition: SDLMain.mm:106
int wesnoth_main(int argc, char **argv)