Skip to content

Latest commit

 

History

History
375 lines (297 loc) · 10.7 KB

SDLMain.m

File metadata and controls

375 lines (297 loc) · 10.7 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>
Nov 2, 2001
Nov 2, 2001
13
14
15
/* Use this flag to determine whether we use SDLMain.nib or not */
#define SDL_USE_NIB_FILE 0
Aug 31, 2002
Aug 31, 2002
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 */
Nov 2, 2001
Nov 2, 2001
31
32
33
static int gArgc;
static char **gArgv;
Sep 4, 2001
Sep 4, 2001
34
static BOOL gFinderLaunch;
Aug 11, 2005
Aug 11, 2005
35
static BOOL gCalledAppMainline = FALSE;
Aug 21, 2001
Aug 21, 2001
36
Sep 17, 2004
Sep 17, 2004
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;
}
Nov 2, 2001
Nov 2, 2001
53
54
#if SDL_USE_NIB_FILE
/* A helper category for NSString */
Aug 21, 2001
Aug 21, 2001
55
56
57
@interface NSString (ReplaceSubString)
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
@end
Nov 2, 2001
Nov 2, 2001
58
#endif
Aug 21, 2001
Aug 21, 2001
59
Nov 2, 2001
Nov 2, 2001
60
61
@interface SDLApplication : NSApplication
@end
Nov 2, 2001
Nov 2, 2001
63
@implementation SDLApplication
64
/* Invoked from the Quit menu item */
Nov 2, 2001
Nov 2, 2001
65
- (void)terminate:(id)sender
Nov 2, 2001
Nov 2, 2001
67
/* Post a SDL_QUIT event */
Aug 21, 2001
Aug 21, 2001
68
69
70
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
Nov 2, 2001
Nov 2, 2001
72
@end
Nov 2, 2001
Nov 2, 2001
74
75
/* The main class of the application, the application's delegate */
@implementation SDLMain
Sep 4, 2001
Sep 4, 2001
76
77
/* Set the working directory to the .app's parent directory */
Sep 4, 2001
Sep 4, 2001
78
- (void) setupWorkingDirectory:(BOOL)shouldChdir
Sep 4, 2001
Sep 4, 2001
80
81
if (shouldChdir)
{
Jun 1, 2002
Jun 1, 2002
82
char parentdir[MAXPATHLEN];
Sep 17, 2004
Sep 17, 2004
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);
}
Jun 1, 2002
Jun 1, 2002
91
Aug 21, 2001
Aug 21, 2001
92
93
}
Nov 2, 2001
Nov 2, 2001
94
95
#if SDL_USE_NIB_FILE
Aug 21, 2001
Aug 21, 2001
96
/* Fix menu to contain the real app name instead of "SDL App" */
Nov 2, 2001
Nov 2, 2001
97
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
Aug 21, 2001
Aug 21, 2001
98
99
100
101
102
103
104
{
NSRange aRange;
NSEnumerator *enumerator;
NSMenuItem *menuItem;
aRange = [[aMenu title] rangeOfString:@"SDL App"];
if (aRange.length != 0)
Nov 2, 2001
Nov 2, 2001
105
[aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
Aug 21, 2001
Aug 21, 2001
106
107
108
109
110
111
enumerator = [[aMenu itemArray] objectEnumerator];
while ((menuItem = [enumerator nextObject]))
{
aRange = [[menuItem title] rangeOfString:@"SDL App"];
if (aRange.length != 0)
Nov 2, 2001
Nov 2, 2001
112
[menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
Aug 21, 2001
Aug 21, 2001
113
if ([menuItem hasSubmenu])
Nov 2, 2001
Nov 2, 2001
114
[self fixMenu:[menuItem submenu] withAppName:appName];
Aug 21, 2001
Aug 21, 2001
115
116
}
[ aMenu sizeToFit ];
Nov 2, 2001
Nov 2, 2001
119
120
#else
Sep 17, 2004
Sep 17, 2004
121
static void setApplicationMenu(void)
Nov 2, 2001
Nov 2, 2001
122
123
124
{
/* warning: this code is very odd */
NSMenu *appleMenu;
Sep 17, 2004
Sep 17, 2004
125
126
127
128
129
NSMenuItem *menuItem;
NSString *title;
NSString *appName;
appName = getApplicationName();
Nov 2, 2001
Nov 2, 2001
130
131
appleMenu = [[NSMenu alloc] initWithTitle:@""];
Sep 17, 2004
Sep 17, 2004
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 */
Nov 2, 2001
Nov 2, 2001
161
[appleMenu release];
Sep 17, 2004
Sep 17, 2004
162
[menuItem release];
Nov 2, 2001
Nov 2, 2001
163
164
165
}
/* Create a window menu */
Sep 17, 2004
Sep 17, 2004
166
static void setupWindowMenu(void)
Nov 2, 2001
Nov 2, 2001
167
{
Sep 17, 2004
Sep 17, 2004
168
169
170
NSMenu *windowMenu;
NSMenuItem *windowMenuItem;
NSMenuItem *menuItem;
Nov 2, 2001
Nov 2, 2001
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 */
Sep 17, 2004
Sep 17, 2004
193
static void CustomApplicationMain (argc, argv)
Nov 2, 2001
Nov 2, 2001
194
195
196
197
198
199
200
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDLMain *sdlMain;
/* Ensure the application object is initialised */
[SDLApplication sharedApplication];
Aug 31, 2002
Aug 31, 2002
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 */
Nov 2, 2001
Nov 2, 2001
212
213
/* Set up the menubar */
[NSApp setMainMenu:[[NSMenu alloc] init]];
Sep 17, 2004
Sep 17, 2004
214
setApplicationMenu();
Nov 2, 2001
Nov 2, 2001
215
setupWindowMenu();
Sep 17, 2004
Sep 17, 2004
216
Nov 2, 2001
Nov 2, 2001
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
Aug 11, 2005
Aug 11, 2005
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
{
Aug 22, 2005
Aug 22, 2005
248
249
250
if (!gFinderLaunch) /* MacOS is passing command line args. */
return FALSE;
Aug 11, 2005
Aug 11, 2005
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
{
Jun 11, 2001
Jun 11, 2001
282
283
int status;
284
/* Set the working directory to the .app's parent directory */
Nov 2, 2001
Nov 2, 2001
285
[self setupWorkingDirectory:gFinderLaunch];
Jun 11, 2001
Jun 11, 2001
286
Nov 2, 2001
Nov 2, 2001
287
#if SDL_USE_NIB_FILE
Aug 21, 2001
Aug 21, 2001
288
/* Set the main menu to contain the real app name instead of "SDL App" */
Sep 17, 2004
Sep 17, 2004
289
[self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
Nov 2, 2001
Nov 2, 2001
290
#endif
Aug 21, 2001
Aug 21, 2001
291
292
/* Hand off to main application code */
Aug 11, 2005
Aug 11, 2005
293
gCalledAppMainline = TRUE;
Jun 11, 2001
Jun 11, 2001
294
295
296
297
status = SDL_main (gArgc, gArgv);
/* We're done, thank you for playing */
exit(status);
Aug 21, 2001
Aug 21, 2001
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));
Nov 2, 2001
Nov 2, 2001
316
/* Get first part into buffer */
Aug 21, 2001
Aug 21, 2001
317
318
319
320
localRange.location = 0;
localRange.length = aRange.location;
[self getCharacters:buffer range:localRange];
Nov 2, 2001
Nov 2, 2001
321
/* Get middle part into buffer */
Aug 21, 2001
Aug 21, 2001
322
323
324
325
localRange.location = 0;
localRange.length = aStringLen;
[aString getCharacters:(buffer+aRange.location) range:localRange];
Nov 2, 2001
Nov 2, 2001
326
/* Get last part into buffer */
Aug 21, 2001
Aug 21, 2001
327
328
329
330
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
331
/* Build output string */
Aug 21, 2001
Aug 21, 2001
332
333
334
335
336
337
338
339
340
341
result = [NSString stringWithCharacters:buffer length:bufferSize];
NSDeallocateMemoryPages(buffer, bufferSize);
return result;
}
@end
Nov 2, 2001
Nov 2, 2001
342
343
344
345
346
#ifdef main
# undef main
#endif
Nov 2, 2001
Nov 2, 2001
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 */
Jun 11, 2001
Jun 11, 2001
352
353
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
Aug 11, 2005
Aug 11, 2005
354
355
356
gArgv = (char **) malloc(sizeof (char *) * 2);
gArgv[0] = argv[0];
gArgv[1] = NULL;
Jun 11, 2001
Jun 11, 2001
357
gArgc = 1;
Sep 17, 2004
Sep 17, 2004
358
gFinderLaunch = YES;
Jun 11, 2001
Jun 11, 2001
359
} else {
Aug 11, 2005
Aug 11, 2005
360
int i;
Jun 11, 2001
Jun 11, 2001
361
gArgc = argc;
Aug 11, 2005
Aug 11, 2005
362
363
364
gArgv = (char **) malloc(sizeof (char *) * (argc+1));
for (i = 0; i <= argc; i++)
gArgv[i] = argv[i];
Sep 17, 2004
Sep 17, 2004
365
gFinderLaunch = NO;
Jun 11, 2001
Jun 11, 2001
366
367
}
Nov 2, 2001
Nov 2, 2001
368
369
#if SDL_USE_NIB_FILE
[SDLApplication poseAsClass:[NSApplication class]];
370
NSApplicationMain (argc, argv);
Nov 2, 2001
Nov 2, 2001
371
372
373
#else
CustomApplicationMain (argc, argv);
#endif
Jun 11, 2001
Jun 11, 2001
375
}