Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
393 lines (325 loc) · 11.2 KB

SDLMain.m

File metadata and controls

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