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