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