This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDLMain.m
397 lines (329 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
@interface NSString (ReplaceSubString) - (NSString *) stringByReplacingRange:(NSRange)
79
80
aRange
with:(NSString *)
81
aString;
82
@end
83
#endif
84
85
@ interface SDLApplication:NSApplication
@ end @ implementation SDLApplication
86
/* Invoked from the Quit menu item */
87
- (void) terminate:(id) sender
88
{
89
/* Post a SDL_QUIT event */
90
91
SDL_Event event;
event.type = SDL_QUIT;
92
SDL_PushEvent (&event);
93
94
}
95
@end
96
/* The main class of the application, the application's delegate */
97
@ implementation SDLMain
98
/* Set the working directory to the .app's parent directory */
99
100
- (void) setupWorkingDirectory:(BOOL) shouldChdir {
if (shouldChdir) {
101
char parentdir[MAXPATHLEN];
102
103
104
105
106
107
108
109
110
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);
}
111
112
113
}
114
115
#if SDL_USE_NIB_FILE
116
/* Fix menu to contain the real app name instead of "SDL App" */
117
-(void) fixMenu:(NSMenu *)
118
119
120
121
aMenu
withAppName:(NSString *)
appName
{
122
123
124
125
NSRange aRange;
NSEnumerator *enumerator;
NSMenuItem *menuItem;
126
aRange =[[aMenu title] rangeOfString:@"SDL App"];
127
if (aRange.length != 0)
128
[aMenu setTitle: [[aMenu title] stringByReplacingRange: aRange with:appName]];
129
130
131
132
enumerator =[[aMenu itemArray] objectEnumerator];
while ((menuItem =[enumerator nextObject])) {
aRange =[[menuItem title] rangeOfString:@"SDL App"];
133
if (aRange.length != 0)
134
[menuItem setTitle: [[menuItem title] stringByReplacingRange: aRange with:appName]];
135
if ([menuItem hasSubmenu])
136
[self fixMenu: [menuItem submenu] withAppName:appName];
137
}
138
[aMenu sizeToFit];
139
140
}
141
142
#else
143
144
static void
setApplicationMenu (void)
145
146
147
{
/* warning: this code is very odd */
NSMenu *appleMenu;
148
149
150
NSMenuItem *menuItem;
NSString *title;
NSString *appName;
151
152
153
154
appName = getApplicationName ();
appleMenu =[[NSMenu alloc] initWithTitle:@""];
155
/* Add menu items */
156
157
158
159
title =[@"About " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (orderFrontStandardAboutPanel: )keyEquivalent:@""];
[appleMenu addItem:[NSMenuItem separatorItem]];
160
161
162
title =[@"Hide " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (hide: )keyEquivalent:@"h"];
163
164
165
166
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle: @"Hide Others" action: @selector (hideOtherApplications: )keyEquivalent:@"h"];
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask |
NSCommandKeyMask)];
167
168
[appleMenu addItemWithTitle: @"Show All" action: @selector (unhideAllApplications: )keyEquivalent:@""];
169
170
[appleMenu addItem:[NSMenuItem separatorItem]];
171
172
173
title =[@"Quit " stringByAppendingString:appName];
[appleMenu addItemWithTitle: title action: @selector (terminate: )keyEquivalent:@"q"];
174
175
176
/* Put menu into the menubar */
177
178
179
menuItem =[[NSMenuItem alloc] initWithTitle: @"" action: nil keyEquivalent:@""];
[menuItem setSubmenu:appleMenu];
[[NSApp mainMenu] addItem:menuItem];
180
181
/* Tell the application object that this is now the application menu */
182
[NSApp setAppleMenu:appleMenu];
183
184
/* Finally give up our references to the objects */
185
[appleMenu release];
186
[menuItem release];
187
188
189
}
/* Create a window menu */
190
191
static void
setupWindowMenu (void)
192
{
193
194
195
196
197
NSMenu *windowMenu;
NSMenuItem *windowMenuItem;
NSMenuItem *menuItem;
windowMenu =[[NSMenu alloc] initWithTitle:@"Window"];
198
199
/* "Minimize" item */
200
201
menuItem =[[NSMenuItem alloc] initWithTitle: @"Minimize" action: @selector (performMiniaturize: )keyEquivalent:@"m"];
[windowMenu addItem:menuItem];
202
[menuItem release];
203
204
/* Put menu into the menubar */
205
206
207
208
windowMenuItem =[[NSMenuItem alloc] initWithTitle: @"Window" action: nil keyEquivalent:@""];
[windowMenuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:windowMenuItem];
209
/* Tell the application object that this is now the window menu */
210
[NSApp setWindowsMenu:windowMenu];
211
212
213
214
215
216
217
/* Finally give up our references to the objects */
[windowMenu release];
[windowMenuItem release];
}
/* Replacement for NSApplicationMain */
218
219
static void
CustomApplicationMain (int argc, char **argv)
220
{
221
222
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
SDLMain *sdlMain;
223
224
225
/* Ensure the application object is initialised */
[SDLApplication sharedApplication];
226
227
228
229
230
#ifdef SDL_USE_CPS
{
CPSProcessSerNum PSN;
/* Tell the dock about us */
231
232
233
234
if (!CPSGetCurrentProcess (&PSN))
if (!CPSEnableForegroundOperation
(&PSN, 0x03, 0x3C, 0x2C, 0x1103))
if (!CPSSetFrontProcess (&PSN))
235
236
237
238
[SDLApplication sharedApplication];
}
#endif /* SDL_USE_CPS */
239
/* Set up the menubar */
240
241
242
[NSApp setMainMenu:[[NSMenu alloc] init]];
setApplicationMenu ();
setupWindowMenu ();
243
244
/* Create SDLMain and make it the app delegate */
245
246
247
sdlMain =[[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
248
249
/* Start the main event loop */
[NSApp run];
250
251
252
253
254
255
256
[sdlMain release];
[pool release];
}
#endif
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* 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.
*/
273
274
-(BOOL) application:(NSApplication *)
theApplication openFile:(NSString *) filename
275
{
276
277
278
279
280
const char *temparg;
size_t arglen;
char *arg;
char **newargv;
281
if (!gFinderLaunch) /* MacOS is passing command line args. */
282
283
return FALSE;
284
if (gCalledAppMainline) /* app has started, ignore this document. */
285
286
return FALSE;
287
288
289
temparg =[filename UTF8String];
arglen = SDL_strlen (temparg) + 1;
arg = (char *) SDL_malloc (arglen);
290
291
292
if (arg == NULL)
return FALSE;
293
294
295
newargv = (char **) realloc (gArgv, sizeof (char *) * (gArgc + 2));
if (newargv == NULL) {
SDL_free (arg);
296
297
298
299
return FALSE;
}
gArgv = newargv;
300
SDL_strlcpy (arg, temparg, arglen);
301
302
303
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
return TRUE;
304
305
306
}
307
/* Called when the internal event loop has just started running */
308
-(void) applicationDidFinishLaunching:(NSNotification *) note {
309
310
int status;
311
/* Set the working directory to the .app's parent directory */
312
[self setupWorkingDirectory:gFinderLaunch];
313
314
#if SDL_USE_NIB_FILE
315
/* Set the main menu to contain the real app name instead of "SDL App" */
316
[self fixMenu: [NSApp mainMenu] withAppName:getApplicationName ()];
317
#endif
318
319
/* Hand off to main application code */
320
gCalledAppMainline = TRUE;
321
322
323
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
324
exit (status);
325
}
326
327
@end @ implementation NSString (ReplaceSubString) - (NSString *) stringByReplacingRange:(NSRange)
328
329
aRange
with:(NSString *)
330
aString
331
332
{
unsigned int bufferSize;
333
334
unsigned int selfLen =[self length];
unsigned int aStringLen =[aString length];
335
336
337
338
339
unichar *buffer;
NSRange localRange;
NSString *result;
bufferSize = selfLen + aStringLen - aRange.length;
340
341
buffer = NSAllocateMemoryPages (bufferSize * sizeof (unichar));
342
/* Get first part into buffer */
343
344
localRange.location = 0;
localRange.length = aRange.location;
345
346
[self getCharacters: buffer range:localRange];
347
/* Get middle part into buffer */
348
349
localRange.location = 0;
localRange.length = aStringLen;
350
351
[aString getCharacters: (buffer + aRange.location) range:localRange];
352
/* Get last part into buffer */
353
354
localRange.location = aRange.location + aRange.length;
localRange.length = selfLen - localRange.location;
355
356
[self getCharacters: (buffer + aRange.location + aStringLen) range:localRange];
357
/* Build output string */
358
359
360
361
result =[NSString stringWithCharacters: buffer length:bufferSize];
NSDeallocateMemoryPages (buffer, bufferSize);
362
363
364
365
return result;
}
@end
366
367
368
#ifdef main
# undef main
#endif
369
/* Main entry point to executable - should *not* be SDL_main! */
370
371
int
main (int argc, char **argv)
372
{
373
/* Copy the arguments into a global variable */
374
/* This is passed if we are launched by double-clicking */
375
376
if (argc >= 2 && strncmp (argv[1], "-psn", 4) == 0) {
gArgv = (char **) SDL_malloc (sizeof (char *) * 2);
377
378
gArgv[0] = argv[0];
gArgv[1] = NULL;
379
gArgc = 1;
380
gFinderLaunch = YES;
381
} else {
382
int i;
383
gArgc = argc;
384
gArgv = (char **) SDL_malloc (sizeof (char *) * (argc + 1));
385
386
for (i = 0; i <= argc; i++)
gArgv[i] = argv[i];
387
gFinderLaunch = NO;
388
389
}
390
#if SDL_USE_NIB_FILE
391
[SDLApplication poseAsClass:[NSApplication class]];
392
NSApplicationMain (argc, argv);
393
394
395
#else
CustomApplicationMain (argc, argv);
#endif
396
return 0;
397
}