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