This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDLMain.m
393 lines (325 loc) · 11.2 KB
1
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
2
3
Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
Non-NIB-Code & other changes: Max Horn <max@quendi.de>
4
5
6
7
8
Feel free to customize this file to suit your needs
*/
#import "SDL.h"
9
#import "SDLMain.h"
10
#import <sys/param.h> /* for MAXPATHLEN */
11
12
#import <unistd.h>
13
14
15
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
but the method still is there and works. To avoid warnings, we declare
it ourselves here. */
16
17
18
@ interface NSApplication (SDL_Missing_Methods) - (void) setAppleMenu:(NSMenu
*)
menu;
19
@end
20
21
/* Use this flag to determine whether we use SDLMain.nib or not */
#define SDL_USE_NIB_FILE 0
22
23
24
25
/* Use this flag to determine whether we use CPS (docking) or not */
#define SDL_USE_CPS 1
#ifdef SDL_USE_CPS
/* Portions of CPS.h */
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
typedef struct CPSProcessSerNum
{
UInt32
lo;
UInt32
hi;
} CPSProcessSerNum;
extern
OSErr
CPSGetCurrentProcess (CPSProcessSerNum * psn);
extern
OSErr
CPSEnableForegroundOperation (CPSProcessSerNum * psn, UInt32 _arg2,
UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
extern
OSErr
CPSSetFrontProcess (CPSProcessSerNum * psn);
44
45
#endif /* SDL_USE_CPS */
46
47
48
49
50
51
52
53
54
55
56
57
58
59
static int
gArgc;
static char **
gArgv;
static
BOOL
gFinderLaunch;
static
BOOL
gCalledAppMainline = FALSE;
static NSString *
getApplicationName (void)
60
61
62
63
64
{
NSDictionary *dict;
NSString *appName = 0;
/* Determine the application name */
65
66
dict =
(NSDictionary *) CFBundleGetInfoDictionary (CFBundleGetMainBundle ());
67
if (dict)
68
69
appName =[dict objectForKey:@"CFBundleName"];
70
if (![appName length])
71
appName =[[NSProcessInfo processInfo] processName];
72
73
74
75
return appName;
}
76
77
#if SDL_USE_NIB_FILE
/* A helper category for NSString */
78
79
80
@interface NSString (ReplaceSubString) - (NSString *) stringByReplacingRange:(NSRange)
aRange with:(NSString *)
aString;
81
@end
82
#endif
83
84
@ interface SDLApplication:NSApplication
@ end @ implementation SDLApplication
85
/* Invoked from the Quit menu item */
86
- (void) terminate:(id) sender
87
{
88
/* Post a SDL_QUIT event */
89
90
SDL_Event event;
event.type = SDL_QUIT;
91
SDL_PushEvent (&event);
92
93
}
94
@end
95
/* The main class of the application, the application's delegate */
96
@ implementation SDLMain
97
/* Set the working directory to the .app's parent directory */
98
99
- (void) setupWorkingDirectory:(BOOL) shouldChdir {
if (shouldChdir) {
100
char parentdir[MAXPATHLEN];
101
102
103
104
105
106
107
108
109
CFURLRef url = CFBundleCopyBundleURL (CFBundleGetMainBundle ());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent (0, url);
if (CFURLGetFileSystemRepresentation
(url2, true, (UInt8 *) parentdir, MAXPATHLEN)) {
assert (chdir (parentdir) == 0); /* chdir to the binary app's parent */
}
CFRelease (url);
CFRelease (url2);
}
110
111
112
}
113
114
#if SDL_USE_NIB_FILE
115
/* Fix menu to contain the real app name instead of "SDL App" */
116
117
118
-(void) fixMenu:(NSMenu *)
aMenu withAppName:(NSString *)
appName {
119
120
121
122
NSRange aRange;
NSEnumerator *enumerator;
NSMenuItem *menuItem;
123
aRange =[[aMenu title] rangeOfString:@"SDL App"];
124
if (aRange.length != 0)
125
[aMenu setTitle: [[aMenu title] stringByReplacingRange: aRange with:appName]];
126
127
128
129
enumerator =[[aMenu itemArray] objectEnumerator];
while ((menuItem =[enumerator nextObject])) {
aRange =[[menuItem title] rangeOfString:@"SDL App"];
130
if (aRange.length != 0)
131
[menuItem setTitle: [[menuItem title] stringByReplacingRange: aRange with:appName]];
132
if ([menuItem hasSubmenu])
133
[self fixMenu: [menuItem submenu] withAppName:appName];
134
}
135
[aMenu sizeToFit];
136
137
}
138
139
#else
140
141
static void
setApplicationMenu (void)
142
143
144
{
/* warning: this code is very odd */
NSMenu *appleMenu;
145
146
147
NSMenuItem *menuItem;
NSString *title;
NSString *appName;
148
149
150
151
appName = getApplicationName ();
appleMenu =[[NSMenu alloc] initWithTitle:@""];
152
/* Add menu items */
153
154
155
156
title =[@"About " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (orderFrontStandardAboutPanel: )keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]];
157
158
159
title =[@"Hide " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (hide: )keyEquivalent:@"h"];
160
161
162
163
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle: @"Hide Others" action: @selector (hideOtherApplications: )keyEquivalent:@"h"];
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask |
NSCommandKeyMask)];
164
165
[appleMenu addItemWithTitle: @"Show All" action: @selector (unhideAllApplications: )keyEquivalent:@""];
166
167
[appleMenu addItem:[NSMenuItem separatorItem]];
168
169
170
title =[@"Quit " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (terminate: )keyEquivalent:@"q"];
171
172
173
/* Put menu into the menubar */
174
175
176
menuItem =[[NSMenuItem alloc] initWithTitle: @"" action: nil keyEquivalent:@""];
[menuItem setSubmenu:appleMenu];
[[NSApp mainMenu] addItem:menuItem];
177
178
/* Tell the application object that this is now the application menu */
179
[NSApp setAppleMenu:appleMenu];
180
181
/* Finally give up our references to the objects */
182
[appleMenu release];
183
[menuItem release];
184
185
186
}
/* Create a window menu */
187
188
static void
setupWindowMenu (void)
189
{
190
191
192
193
194
NSMenu *windowMenu;
NSMenuItem *windowMenuItem;
NSMenuItem *menuItem;
windowMenu =[[NSMenu alloc] initWithTitle:@"Window"];
195
196
/* "Minimize" item */
197
198
menuItem =[[NSMenuItem alloc] initWithTitle: @"Minimize" action: @selector (performMiniaturize: )keyEquivalent:@"m"];
[windowMenu addItem:menuItem];
199
[menuItem release];
200
201
/* Put menu into the menubar */
202
203
204
205
windowMenuItem =[[NSMenuItem alloc] initWithTitle: @"Window" action: nil keyEquivalent:@""];
[windowMenuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:windowMenuItem];
206
/* Tell the application object that this is now the window menu */
207
[NSApp setWindowsMenu:windowMenu];
208
209
210
211
212
213
214
/* Finally give up our references to the objects */
[windowMenu release];
[windowMenuItem release];
}
/* Replacement for NSApplicationMain */
215
216
static void
CustomApplicationMain (int argc, char **argv)
217
{
218
219
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
SDLMain *sdlMain;
220
221
222
/* Ensure the application object is initialised */
[SDLApplication sharedApplication];
223
224
225
226
227
#ifdef SDL_USE_CPS
{
CPSProcessSerNum PSN;
/* Tell the dock about us */
228
229
230
231
if (!CPSGetCurrentProcess (&PSN))
if (!CPSEnableForegroundOperation
(&PSN, 0x03, 0x3C, 0x2C, 0x1103))
if (!CPSSetFrontProcess (&PSN))
232
233
234
235
[SDLApplication sharedApplication];
}
#endif /* SDL_USE_CPS */
236
/* Set up the menubar */
237
238
239
[NSApp setMainMenu:[[NSMenu alloc] init]];
setApplicationMenu ();
setupWindowMenu ();
240
241
/* Create SDLMain and make it the app delegate */
242
243
244
sdlMain =[[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
245
246
/* Start the main event loop */
[NSApp run];
247
248
249
250
251
252
253
[sdlMain release];
[pool release];
}
#endif
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Catch document open requests...this lets us notice files when the app
* was launched by double-clicking a document, or when a document was
* dragged/dropped on the app's icon. You need to have a
* CFBundleDocumentsType section in your Info.plist to get this message,
* apparently.
*
* Files are added to gArgv, so to the app, they'll look like command line
* arguments. Previously, apps launched from the finder had nothing but
* an argv[0].
*
* This message may be received multiple times to open several docs on launch.
*
* This message is ignored once the app's mainline has been called.
*/
270
271
-(BOOL) application:(NSApplication *)
theApplication openFile:(NSString *) filename
272
{
273
274
275
276
277
const char *temparg;
size_t arglen;
char *arg;
char **newargv;
278
if (!gFinderLaunch) /* MacOS is passing command line args. */
279
280
return FALSE;
281
if (gCalledAppMainline) /* app has started, ignore this document. */
282
283
return FALSE;
284
285
286
temparg =[filename UTF8String];
arglen = SDL_strlen (temparg) + 1;
arg = (char *) SDL_malloc (arglen);
287
288
289
if (arg == NULL)
return FALSE;
290
291
292
newargv = (char **) realloc (gArgv, sizeof (char *) * (gArgc + 2));
if (newargv == NULL) {
SDL_free (arg);
293
294
295
296
return FALSE;
}
gArgv = newargv;
297
SDL_strlcpy (arg, temparg, arglen);
298
299
300
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
return TRUE;
301
302
303
}
304
/* Called when the internal event loop has just started running */
305
-(void) applicationDidFinishLaunching:(NSNotification *) note {
306
307
int status;
308
/* Set the working directory to the .app's parent directory */
309
[self setupWorkingDirectory:gFinderLaunch];
310
311
#if SDL_USE_NIB_FILE
312
/* Set the main menu to contain the real app name instead of "SDL App" */
313
[self fixMenu: [NSApp mainMenu] withAppName:getApplicationName ()];
314
#endif
315
316
/* Hand off to main application code */
317
gCalledAppMainline = TRUE;
318
319
320
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
321
exit (status);
322
}
323
324
325
326
@end @ implementation NSString (ReplaceSubString) - (NSString *) stringByReplacingRange:(NSRange)
aRange with:(NSString *)
aString
327
328
{
unsigned int bufferSize;
329
330
unsigned int selfLen =[self length];
unsigned int aStringLen =[aString length];
331
332
333
334
335
unichar *buffer;
NSRange localRange;
NSString *result;
bufferSize = selfLen + aStringLen - aRange.length;
336
337
buffer = NSAllocateMemoryPages (bufferSize * sizeof (unichar));
338
/* Get first part into buffer */
339
340
localRange.location = 0;
localRange.length = aRange.location;
341
342
[self getCharacters: buffer range:localRange];
343
/* Get middle part into buffer */
344
345
localRange.location = 0;
localRange.length = aStringLen;
346
347
[aString getCharacters: (buffer + aRange.location) range:localRange];
348
/* Get last part into buffer */
349
350
localRange.location = aRange.location + aRange.length;
localRange.length = selfLen - localRange.location;
351
352
[self getCharacters: (buffer + aRange.location + aStringLen) range:localRange];
353
/* Build output string */
354
355
356
357
result =[NSString stringWithCharacters: buffer length:bufferSize];
NSDeallocateMemoryPages (buffer, bufferSize);
358
359
360
361
return result;
}
@end
362
363
364
#ifdef main
# undef main
#endif
365
/* Main entry point to executable - should *not* be SDL_main! */
366
367
int
main (int argc, char **argv)
368
{
369
/* Copy the arguments into a global variable */
370
/* This is passed if we are launched by double-clicking */
371
372
if (argc >= 2 && strncmp (argv[1], "-psn", 4) == 0) {
gArgv = (char **) SDL_malloc (sizeof (char *) * 2);
373
374
gArgv[0] = argv[0];
gArgv[1] = NULL;
375
gArgc = 1;
376
gFinderLaunch = YES;
377
} else {
378
int i;
379
gArgc = argc;
380
gArgv = (char **) SDL_malloc (sizeof (char *) * (argc + 1));
381
382
for (i = 0; i <= argc; i++)
gArgv[i] = argv[i];
383
gFinderLaunch = NO;
384
385
}
386
#if SDL_USE_NIB_FILE
387
[SDLApplication poseAsClass:[NSApplication class]];
388
NSApplicationMain (argc, argv);
389
390
391
#else
CustomApplicationMain (argc, argv);
#endif
392
return 0;
393
}