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

Latest commit

 

History

History
397 lines (329 loc) · 11.2 KB

SDLMain.m

File metadata and controls

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