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
11extern "C" int wesnoth_main(int argc, char **argv);
12static 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/* Called when the internal event loop has just started running */
58- (void) applicationDidFinishLaunching: (NSNotification *) note
59{
60	(void) note;
61	/* This makes SDL give events to Cocoa, so it can handle things like command+h to hide, etc. */
62	setenv ("SDL_ENABLEAPPEVENTS", "1", 1);
63	setenv ("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
64
65    /* Set config files for pango and fontconfig, so the data they need can be found */
66	setenv ("PANGO_RC_FILE", "./pangorc", 1);
67	setenv ("PANGO_SYSCONFDIR", ".", 1);
68	setenv ("PANGO_LIBDIR", ".", 1);
69	setenv ("FONTCONFIG_PATH", "./fonts/", 1);
70	setenv ("FONTCONFIG_FILE", "fonts.conf", 1);
71
72	int status;
73
74	/* Set the working directory to the .app's Resources directory */
75	chdir([[[NSBundle mainBundle] resourcePath] fileSystemRepresentation]);
76
77	/* Hand off to main application code */
78	status = wesnoth_main(gArgs.size() - 1, gArgs.data());
79
80	/* We're done, thank you for playing */
81	exit(status);
82}
83@end
84
85template<int sz>
86bool str_eq(const char* str, const char(& cstr)[sz]) {
87	return strncmp(str, cstr, sz) == 0;
88}
89
90#ifdef main
91#  undef main
92#endif
93
94/* Main entry point to executable - should *not* be SDL_main! */
95int main (int argc, char **argv)
96{
97	gArgs.push_back(argv[0]); // Program name
98	for (int i = 1; i < argc; i++) {
99		// Filter out debug arguments that XCode might pass
100		if (str_eq(argv[i], "-ApplePersistenceIgnoreState")) {
101			i++; // Skip the argument
102			continue;
103		}
104		if (str_eq(argv[i], "-NSDocumentRevisionsDebugMode")) {
105			i++; // Skip the argument
106			continue;
107		}
108		// This is passed if launched by double-clicking
109		if (strncmp(argv[i], "-psn", 4) == 0) {
110			continue;
111		}
112		gArgs.push_back(argv[i]);
113	}
114	gArgs.push_back(nullptr);
115
116	[WesnothSDLApplication sharedApplication];
117
118	[[NSBundle mainBundle] loadNibNamed:@"SDLMain" owner:NSApp topLevelObjects:nil];
119
120	[NSApp run];
121	return 0;
122}
123