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

Latest commit

 

History

History
384 lines (303 loc) · 11 KB

SDLMain.m

File metadata and controls

384 lines (303 loc) · 11 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"
10
11
12
#import <sys/param.h> /* for MAXPATHLEN */
#import <unistd.h>
Mar 22, 2006
Mar 22, 2006
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
Nov 2, 2001
Nov 2, 2001
20
21
22
/* Use this flag to determine whether we use SDLMain.nib or not */
#define SDL_USE_NIB_FILE 0
Aug 31, 2002
Aug 31, 2002
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 */
Nov 2, 2001
Nov 2, 2001
38
39
40
static int gArgc;
static char **gArgv;
Sep 4, 2001
Sep 4, 2001
41
static BOOL gFinderLaunch;
Aug 11, 2005
Aug 11, 2005
42
static BOOL gCalledAppMainline = FALSE;
Aug 21, 2001
Aug 21, 2001
43
Sep 17, 2004
Sep 17, 2004
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;
}
Nov 2, 2001
Nov 2, 2001
60
61
#if SDL_USE_NIB_FILE
/* A helper category for NSString */
Aug 21, 2001
Aug 21, 2001
62
63
64
@interface NSString (ReplaceSubString)
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
@end
Nov 2, 2001
Nov 2, 2001
65
#endif
Aug 21, 2001
Aug 21, 2001
66
Nov 2, 2001
Nov 2, 2001
67
68
@interface SDLApplication : NSApplication
@end
Nov 2, 2001
Nov 2, 2001
70
@implementation SDLApplication
71
/* Invoked from the Quit menu item */
Nov 2, 2001
Nov 2, 2001
72
- (void)terminate:(id)sender
Nov 2, 2001
Nov 2, 2001
74
/* Post a SDL_QUIT event */
Aug 21, 2001
Aug 21, 2001
75
76
77
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
Nov 2, 2001
Nov 2, 2001
79
@end
Nov 2, 2001
Nov 2, 2001
81
82
/* The main class of the application, the application's delegate */
@implementation SDLMain
Sep 4, 2001
Sep 4, 2001
83
84
/* Set the working directory to the .app's parent directory */
Sep 4, 2001
Sep 4, 2001
85
- (void) setupWorkingDirectory:(BOOL)shouldChdir
Sep 4, 2001
Sep 4, 2001
87
88
if (shouldChdir)
{
Jun 1, 2002
Jun 1, 2002
89
char parentdir[MAXPATHLEN];
Sep 17, 2004
Sep 17, 2004
90
91
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
Mar 22, 2006
Mar 22, 2006
92
if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
Sep 17, 2004
Sep 17, 2004
93
94
95
96
97
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
}
CFRelease(url);
CFRelease(url2);
}
Jun 1, 2002
Jun 1, 2002
98
Aug 21, 2001
Aug 21, 2001
99
100
}
Nov 2, 2001
Nov 2, 2001
101
102
#if SDL_USE_NIB_FILE
Aug 21, 2001
Aug 21, 2001
103
/* Fix menu to contain the real app name instead of "SDL App" */
Nov 2, 2001
Nov 2, 2001
104
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
Aug 21, 2001
Aug 21, 2001
105
106
107
108
109
110
111
{
NSRange aRange;
NSEnumerator *enumerator;
NSMenuItem *menuItem;
aRange = [[aMenu title] rangeOfString:@"SDL App"];
if (aRange.length != 0)
Nov 2, 2001
Nov 2, 2001
112
[aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
Aug 21, 2001
Aug 21, 2001
113
114
115
116
117
118
enumerator = [[aMenu itemArray] objectEnumerator];
while ((menuItem = [enumerator nextObject]))
{
aRange = [[menuItem title] rangeOfString:@"SDL App"];
if (aRange.length != 0)
Nov 2, 2001
Nov 2, 2001
119
[menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
Aug 21, 2001
Aug 21, 2001
120
if ([menuItem hasSubmenu])
Nov 2, 2001
Nov 2, 2001
121
[self fixMenu:[menuItem submenu] withAppName:appName];
Aug 21, 2001
Aug 21, 2001
122
123
}
[ aMenu sizeToFit ];
Nov 2, 2001
Nov 2, 2001
126
127
#else
Sep 17, 2004
Sep 17, 2004
128
static void setApplicationMenu(void)
Nov 2, 2001
Nov 2, 2001
129
130
131
{
/* warning: this code is very odd */
NSMenu *appleMenu;
Sep 17, 2004
Sep 17, 2004
132
133
134
135
136
NSMenuItem *menuItem;
NSString *title;
NSString *appName;
appName = getApplicationName();
Nov 2, 2001
Nov 2, 2001
137
138
appleMenu = [[NSMenu alloc] initWithTitle:@""];
Sep 17, 2004
Sep 17, 2004
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 */
Nov 2, 2001
Nov 2, 2001
168
[appleMenu release];
Sep 17, 2004
Sep 17, 2004
169
[menuItem release];
Nov 2, 2001
Nov 2, 2001
170
171
172
}
/* Create a window menu */
Sep 17, 2004
Sep 17, 2004
173
static void setupWindowMenu(void)
Nov 2, 2001
Nov 2, 2001
174
{
Sep 17, 2004
Sep 17, 2004
175
176
177
NSMenu *windowMenu;
NSMenuItem *windowMenuItem;
NSMenuItem *menuItem;
Nov 2, 2001
Nov 2, 2001
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 */
Jan 1, 2006
Jan 1, 2006
200
static void CustomApplicationMain (int argc, char **argv)
Nov 2, 2001
Nov 2, 2001
201
202
203
204
205
206
207
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDLMain *sdlMain;
/* Ensure the application object is initialised */
[SDLApplication sharedApplication];
Aug 31, 2002
Aug 31, 2002
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 */
Nov 2, 2001
Nov 2, 2001
219
220
/* Set up the menubar */
[NSApp setMainMenu:[[NSMenu alloc] init]];
Sep 17, 2004
Sep 17, 2004
221
setApplicationMenu();
Nov 2, 2001
Nov 2, 2001
222
setupWindowMenu();
Sep 17, 2004
Sep 17, 2004
223
Nov 2, 2001
Nov 2, 2001
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
Aug 11, 2005
Aug 11, 2005
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
{
May 17, 2006
May 17, 2006
255
256
257
258
259
const char *temparg;
size_t arglen;
char *arg;
char **newargv;
Aug 22, 2005
Aug 22, 2005
260
261
262
if (!gFinderLaunch) /* MacOS is passing command line args. */
return FALSE;
Aug 11, 2005
Aug 11, 2005
263
264
265
if (gCalledAppMainline) /* app has started, ignore this document. */
return FALSE;
May 17, 2006
May 17, 2006
266
267
268
temparg = [filename UTF8String];
arglen = SDL_strlen(temparg) + 1;
arg = (char *) SDL_malloc(arglen);
Aug 11, 2005
Aug 11, 2005
269
270
271
if (arg == NULL)
return FALSE;
May 17, 2006
May 17, 2006
272
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
Aug 11, 2005
Aug 11, 2005
273
274
if (newargv == NULL)
{
Mar 21, 2006
Mar 21, 2006
275
SDL_free(arg);
Aug 11, 2005
Aug 11, 2005
276
277
278
279
return FALSE;
}
gArgv = newargv;
Mar 21, 2006
Mar 21, 2006
280
SDL_strlcpy(arg, temparg, arglen);
Jan 1, 2006
Jan 1, 2006
281
282
283
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
return TRUE;
Aug 11, 2005
Aug 11, 2005
284
285
286
}
287
288
289
/* Called when the internal event loop has just started running */
- (void) applicationDidFinishLaunching: (NSNotification *) note
{
Jun 11, 2001
Jun 11, 2001
290
291
int status;
292
/* Set the working directory to the .app's parent directory */
Nov 2, 2001
Nov 2, 2001
293
[self setupWorkingDirectory:gFinderLaunch];
Jun 11, 2001
Jun 11, 2001
294
Nov 2, 2001
Nov 2, 2001
295
#if SDL_USE_NIB_FILE
Aug 21, 2001
Aug 21, 2001
296
/* Set the main menu to contain the real app name instead of "SDL App" */
Sep 17, 2004
Sep 17, 2004
297
[self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
Nov 2, 2001
Nov 2, 2001
298
#endif
Aug 21, 2001
Aug 21, 2001
299
300
/* Hand off to main application code */
Aug 11, 2005
Aug 11, 2005
301
gCalledAppMainline = TRUE;
Jun 11, 2001
Jun 11, 2001
302
303
304
305
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
exit(status);
Aug 21, 2001
Aug 21, 2001
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));
Nov 2, 2001
Nov 2, 2001
324
/* Get first part into buffer */
Aug 21, 2001
Aug 21, 2001
325
326
327
328
localRange.location = 0;
localRange.length = aRange.location;
[self getCharacters:buffer range:localRange];
Nov 2, 2001
Nov 2, 2001
329
/* Get middle part into buffer */
Aug 21, 2001
Aug 21, 2001
330
331
332
333
localRange.location = 0;
localRange.length = aStringLen;
[aString getCharacters:(buffer+aRange.location) range:localRange];
Nov 2, 2001
Nov 2, 2001
334
/* Get last part into buffer */
Aug 21, 2001
Aug 21, 2001
335
336
337
338
localRange.location = aRange.location + aRange.length;
localRange.length = selfLen - localRange.location;
[self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
Nov 2, 2001
Nov 2, 2001
339
/* Build output string */
Aug 21, 2001
Aug 21, 2001
340
341
342
343
344
345
346
347
348
349
result = [NSString stringWithCharacters:buffer length:bufferSize];
NSDeallocateMemoryPages(buffer, bufferSize);
return result;
}
@end
Nov 2, 2001
Nov 2, 2001
350
351
352
353
354
#ifdef main
# undef main
#endif
Nov 2, 2001
Nov 2, 2001
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 */
Jun 11, 2001
Jun 11, 2001
360
361
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
Mar 21, 2006
Mar 21, 2006
362
gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
Aug 11, 2005
Aug 11, 2005
363
364
gArgv[0] = argv[0];
gArgv[1] = NULL;
Jun 11, 2001
Jun 11, 2001
365
gArgc = 1;
Sep 17, 2004
Sep 17, 2004
366
gFinderLaunch = YES;
Jun 11, 2001
Jun 11, 2001
367
} else {
Aug 11, 2005
Aug 11, 2005
368
int i;
Jun 11, 2001
Jun 11, 2001
369
gArgc = argc;
Mar 21, 2006
Mar 21, 2006
370
gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
Aug 11, 2005
Aug 11, 2005
371
372
for (i = 0; i <= argc; i++)
gArgv[i] = argv[i];
Sep 17, 2004
Sep 17, 2004
373
gFinderLaunch = NO;
Jun 11, 2001
Jun 11, 2001
374
375
}
Nov 2, 2001
Nov 2, 2001
376
377
#if SDL_USE_NIB_FILE
[SDLApplication poseAsClass:[NSApplication class]];
378
NSApplicationMain (argc, argv);
Nov 2, 2001
Nov 2, 2001
379
380
381
#else
CustomApplicationMain (argc, argv);
#endif
Jun 11, 2001
Jun 11, 2001
383
}