1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
Non-NIB-Code & other changes: Max Horn <max@quendi.de>
Feel free to customize this file to suit your needs
*/
/*
SDLMain.m and SDLMain.h carry neither a copyright or license. They are in the
public domain.
*/
#if !defined(__arm__)
#import "SDL/SDL.h"
#import "SDLMain.h"
#import <sys/param.h> /* for MAXPATHLEN */
#import <unistd.h>
#import "osx/CocoaInterface.h"
//hack around problem with xbmc's typedef int BOOL
// and obj-c's typedef unsigned char BOOL
#define BOOL XBMC_BOOL
#import "utils/StdString.h"
#import "PlatformDefs.h"
#import "ApplicationMessenger.h"
#import "storage/osx/DarwinStorageProvider.h"
#undef BOOL
#import "osx/HotKeyController.h"
#import "osx/DarwinUtils.h"
// 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
// 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 "C" {
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 */
static int gArgc;
static char **gArgv;
static BOOL gFinderLaunch;
static BOOL gCalledAppMainline = FALSE;
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;
}
static void setupApplicationMenu(void)
{
// warning: this code is very odd
NSMenu *appleMenu;
NSMenuItem *menuItem;
NSString *title;
NSString *appName;
appName = getApplicationName();
appleMenu = [[NSMenu alloc] initWithTitle:@""];
// 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
[appleMenu release];
[menuItem release];
}
// Create a window menu
static void setupWindowMenu(void)
{
NSMenu *windowMenu;
NSMenuItem *windowMenuItem;
NSMenuItem *menuItem;
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
// "Full/Windowed Toggle" item
menuItem = [[NSMenuItem alloc] initWithTitle:@"Full/Windowed Toggle" action:@selector(fullScreenToggle:) keyEquivalent:@"f"];
[windowMenu addItem:menuItem];
[menuItem release];
// "Full/Windowed Toggle" item
menuItem = [[NSMenuItem alloc] initWithTitle:@"Float on Top" action:@selector(floatOnTopToggle:) keyEquivalent:@"t"];
[windowMenu addItem:menuItem];
[menuItem release];
// "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];
}
@interface XBMCApplication : NSApplication
@end
@implementation XBMCApplication
// Called before the internal event loop has started running.
- (void) finishLaunching
{
[super finishLaunching];
}
// Invoked from the Quit menu item
- (void)terminate:(id)sender
{
// remove any notification handlers
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Post a SDL_QUIT event
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
- (void)fullScreenToggle:(id)sender
{
// Post an toggle full-screen event to the application thread.
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_USEREVENT;
event.user.code = TMSG_TOGGLEFULLSCREEN;
SDL_PushEvent(&event);
}
- (void)floatOnTopToggle:(id)sender
{
NSWindow* window = [[[NSOpenGLContext currentContext] view] window];
if ([window level] == NSFloatingWindowLevel)
{
[window setLevel:NSNormalWindowLevel];
[sender setState:NSOffState];
}
else
{
[window setLevel:NSFloatingWindowLevel];
[sender setState:NSOnState];
}
}
@end
// The main class of the application, the application's delegate
@implementation XBMCDelegate
// Set the working directory to the .app's parent directory
- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
if (shouldChdir)
{
char parentdir[MAXPATHLEN];
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);
}
}
- (void) applicationWillTerminate: (NSNotification *) note
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self
name:NSWorkspaceDidMountNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self
name:NSWorkspaceDidUnmountNotification object:nil];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:MediaKeyPower object:nil];
[center removeObserver:self name:MediaKeySoundMute object:nil];
[center removeObserver:self name:MediaKeySoundUp object:nil];
[center removeObserver:self name:MediaKeySoundDown object:nil];
[center removeObserver:self name:MediaKeyPlayPauseNotification object:nil];
[center removeObserver:self name:MediaKeyFastNotification object:nil];
[center removeObserver:self name:MediaKeyRewindNotification object:nil];
[center removeObserver:self name:MediaKeyNextNotification object:nil];
[center removeObserver:self name:MediaKeyPreviousNotification object:nil];
[[HotKeyController sharedController] disableTap];
}
- (void) applicationWillResignActive:(NSNotification *) note
{
//[[HotKeyController sharedController] sysPower:NO];
//[[HotKeyController sharedController] sysVolume:NO];
[[HotKeyController sharedController] setActive:NO];
}
- (void) applicationWillBecomeActive:(NSNotification *) note
{
//[[HotKeyController sharedController] sysPower:YES];
//[[HotKeyController sharedController] sysVolume:YES];
[[HotKeyController sharedController] setActive:YES];
}
// To use Cocoa on secondary POSIX threads, your application must first detach
// at least one NSThread object, which can immediately exit. Some info says this
// is not required anymore, who knows ?
- (void) kickstartMultiThreaded:(id)arg;
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// empty
[pool release];
}
// Called after the internal event loop has started running.
- (void) applicationDidFinishLaunching: (NSNotification *) note
{
// enable multithreading, we should NOT have to do this but as we are mixing NSThreads/pthreads...
if (![NSThread isMultiThreaded])
[NSThread detachNewThreadSelector:@selector(kickstartMultiThreaded:) toTarget:self withObject:nil];
// Set the working directory to the .app's parent directory
[self setupWorkingDirectory:gFinderLaunch];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(deviceDidMountNotification:)
name:NSWorkspaceDidMountNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(deviceDidUnMountNotification:)
name:NSWorkspaceDidUnmountNotification
object:nil];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// create media key handler singlton
[[HotKeyController sharedController] enableTap];
// add media key notifications
[center addObserver:self
selector:@selector(powerKeyNotification)
name:MediaKeyPower object:nil];
[center addObserver:self
selector:@selector(muteKeyNotification)
name:MediaKeySoundMute object:nil];
[center addObserver:self
selector:@selector(soundUpKeyNotification)
name:MediaKeySoundUp object:nil];
[center addObserver:self
selector:@selector(soundDownKeyNotification)
name:MediaKeySoundDown object:nil];
[center addObserver:self
selector:@selector(playPauseKeyNotification)
name:MediaKeyPlayPauseNotification object:nil];
[center addObserver:self
selector:@selector(fastKeyNotification)
name:MediaKeyFastNotification object:nil];
[center addObserver:self
selector:@selector(rewindKeyNotification)
name:MediaKeyRewindNotification object:nil];
[center addObserver:self
selector:@selector(nextKeyNotification)
name:MediaKeyNextNotification object:nil];
[center addObserver:self
selector:@selector(previousKeyNotification)
name:MediaKeyPreviousNotification object:nil];
// We're going to manually manage the screensaver.
setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", true);
// Hand off to main application code
gCalledAppMainline = TRUE;
// stop the main loop so we return to main (below) and can
// call SDL_main there.
[NSApp stop:nil];
//post a NOP event, so the run loop actually stops
//see http://www.cocoabuilder.com/archive/cocoa/219842-nsapp-stop.html
NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
location: NSMakePoint(0,0)
modifierFlags: 0
timestamp: 0.0
windowNumber: 0
context: nil
subtype: 0
data1: 0
data2: 0];
//
[NSApp postEvent: event atStart: true];
}
/*
* 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
{
const char *temparg;
size_t arglen;
char *arg;
char **newargv;
// MacOS is passing command line args.
if (!gFinderLaunch)
return FALSE;
// app has started, ignore this document.
if (gCalledAppMainline)
return FALSE;
temparg = [filename UTF8String];
arglen = SDL_strlen(temparg) + 1;
arg = (char *) SDL_malloc(arglen);
if (arg == NULL)
return FALSE;
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
if (newargv == NULL)
{
SDL_free(arg);
return FALSE;
}
gArgv = newargv;
SDL_strlcpy(arg, temparg, arglen);
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
return TRUE;
}
- (void) deviceDidMountNotification:(NSNotification *) note
{
// calling into c++ code, need to use autorelease pools
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CDarwinStorageProvider::SetEvent();
[pool release];
}
- (void) deviceDidUnMountNotification:(NSNotification *) note
{
// calling into c++ code, need to use autorelease pools
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CDarwinStorageProvider::SetEvent();
[pool release];
}
#define VK_SLEEP 0x143
#define VK_VOLUME_MUTE 0xAD
#define VK_VOLUME_DOWN 0xAE
#define VK_VOLUME_UP 0xAF
#define VK_MEDIA_NEXT_TRACK 0xB0
#define VK_MEDIA_PREV_TRACK 0xB1
#define VK_MEDIA_STOP 0xB2
#define VK_MEDIA_PLAY_PAUSE 0xB3
#define VK_REWIND 0x9D
#define VK_FAST_FWD 0x9E
- (void)powerKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_SLEEP;
SDL_PushEvent(&event);
}
- (void)muteKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_VOLUME_MUTE;
SDL_PushEvent(&event);
}
- (void)soundUpKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_VOLUME_UP;
SDL_PushEvent(&event);
}
- (void)soundDownKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_VOLUME_DOWN;
SDL_PushEvent(&event);
}
- (void)playPauseKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_MEDIA_PLAY_PAUSE;
SDL_PushEvent(&event);
}
- (void)fastKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_FAST_FWD;
SDL_PushEvent(&event);
}
- (void)rewindKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_REWIND;
SDL_PushEvent(&event);
}
- (void)nextKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_MEDIA_NEXT_TRACK;
SDL_PushEvent(&event);
}
- (void)previousKeyNotification
{
SDL_Event event;
memset(&event, 0, sizeof(event));
event.type = SDL_KEYDOWN;
event.key.keysym.sym = (SDLKey)VK_MEDIA_PREV_TRACK;
SDL_PushEvent(&event);
}
@end
#ifdef main
# undef main
#endif
/* Main entry point to executable - should *not* be SDL_main! */
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
XBMCDelegate *xbmc_delegate;
// Block SIGPIPE
// SIGPIPE repeatably kills us, turn it off
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
sigprocmask(SIG_BLOCK, &set, NULL);
}
/* Copy the arguments into a global variable */
/* This is passed if we are launched by double-clicking */
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
gArgv[0] = argv[0];
gArgv[1] = NULL;
gArgc = 1;
gFinderLaunch = YES;
} else {
gArgc = argc;
gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
for (int i = 0; i <= argc; i++)
gArgv[i] = argv[i];
gFinderLaunch = NO;
}
// fix open with document/movie - autostart
// on mavericks we are not called with "-psn" anymore
// as the whole ProcessSerialNumber approach is deprecated
// in that case assume finder launch - else
// we wouldn't handle documents/movies someone dragged on the app icon
if (CDarwinUtils::IsMavericks())
gFinderLaunch = TRUE;
// Ensure the application object is initialised
[XBMCApplication sharedApplication];
#ifdef SDL_USE_CPS
{
CPSProcessSerNum PSN;
/* Tell the dock about us */
if (!CPSGetCurrentProcess(&PSN))
if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
if (!CPSSetFrontProcess(&PSN))
[XBMCApplication sharedApplication];
}
#endif
// Set up the menubars
[NSApp setMainMenu:[[NSMenu alloc] init]];
setupApplicationMenu();
setupWindowMenu();
// Create XBMCDelegate and make it the app delegate
xbmc_delegate = [[XBMCDelegate alloc] init];
[NSApp setDelegate:xbmc_delegate];
// Start the main event loop
[NSApp run];
// call SDL_main which calls our real main in xbmc.cpp
// see http://lists.libsdl.org/pipermail/sdl-libsdl.org/2008-September/066542.html
int status;
status = SDL_main(gArgc, gArgv);
SDL_Quit();
[xbmc_delegate applicationWillTerminate:NULL];
[xbmc_delegate release];
[pool release];
return status;
}
#endif
|