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