2010-04-13 15:55:54 +02:00
|
|
|
#import <IOKit/hidsystem/ev_keymap.h>
|
|
|
|
#import <AppKit/NSEvent.h>
|
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
#import <AppKit/NSApplication.h>
|
|
|
|
|
2010-04-22 15:57:46 +02:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2010-04-15 17:23:12 +02:00
|
|
|
#import <Foundation/NSBundle.h>
|
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
#import <Foundation/NSURL.h>
|
|
|
|
#import <AppKit/NSNibDeclarations.h>
|
|
|
|
#import <Sparkle/SUUpdater.h>
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
#import <Kernel/AvailabilityMacros.h>
|
|
|
|
|
2010-06-18 13:41:16 +02:00
|
|
|
#include "config.h"
|
2010-05-11 14:51:31 +02:00
|
|
|
#include "globalshortcuts.h"
|
2010-04-13 15:55:54 +02:00
|
|
|
#include "mac_startup.h"
|
2010-06-17 22:31:34 +02:00
|
|
|
#include "macglobalshortcutbackend.h"
|
2010-04-13 15:55:54 +02:00
|
|
|
|
2010-04-14 23:27:27 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QEvent>
|
2010-06-14 15:22:45 +02:00
|
|
|
|
|
|
|
#include <QtDebug>
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
// Capture global media keys on Mac (Cocoa only!)
|
|
|
|
// See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/
|
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
@interface MacApplication :NSApplication {
|
2010-06-17 22:31:34 +02:00
|
|
|
MacGlobalShortcutBackend* shortcut_handler_;
|
2010-06-14 15:22:45 +02:00
|
|
|
PlatformInterface* application_handler_;
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (MacGlobalShortcutBackend*) shortcut_handler;
|
|
|
|
- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler;
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (PlatformInterface*) application_handler;
|
|
|
|
- (void) SetApplicationHandler: (PlatformInterface*)handler;
|
2010-04-13 15:55:54 +02:00
|
|
|
|
2010-04-14 23:27:27 +02:00
|
|
|
- (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
|
2010-04-27 20:44:19 +02:00
|
|
|
@end
|
2010-04-14 23:27:27 +02:00
|
|
|
|
2010-06-18 13:41:16 +02:00
|
|
|
#ifdef SNOW_LEOPARD
|
2010-06-17 22:31:34 +02:00
|
|
|
@interface AppDelegate :NSObject <NSApplicationDelegate> {
|
|
|
|
#else
|
|
|
|
@interface AppDelegate :NSObject {
|
|
|
|
#endif
|
2010-06-14 15:22:45 +02:00
|
|
|
PlatformInterface* application_handler_;
|
2010-06-22 16:09:13 +02:00
|
|
|
NSMenu* dock_menu_;
|
2010-04-27 20:44:19 +02:00
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (id) initWithHandler: (PlatformInterface*)handler;
|
2010-04-14 23:27:27 +02:00
|
|
|
// NSApplicationDelegate
|
|
|
|
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag;
|
2010-06-22 16:09:13 +02:00
|
|
|
- (NSMenu*) applicationDockMenu: (NSApplication*)sender;
|
|
|
|
- (void) setDockMenu: (NSMenu*)menu;
|
2010-04-13 15:55:54 +02:00
|
|
|
@end
|
|
|
|
|
2010-04-27 20:44:19 +02:00
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
- (id) init {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
application_handler_ = nil;
|
2010-06-22 16:09:13 +02:00
|
|
|
dock_menu_ = nil;
|
2010-04-27 20:44:19 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (id) initWithHandler: (PlatformInterface*)handler {
|
2010-04-27 20:44:19 +02:00
|
|
|
application_handler_ = handler;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag {
|
|
|
|
if (application_handler_) {
|
2010-06-14 15:22:45 +02:00
|
|
|
application_handler_->Activate();
|
2010-04-27 20:44:19 +02:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
2010-06-14 15:22:45 +02:00
|
|
|
|
2010-06-22 16:09:13 +02:00
|
|
|
- (void) setDockMenu: (NSMenu*)menu {
|
|
|
|
dock_menu_ = menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMenu*) applicationDockMenu: (NSApplication*)sender {
|
|
|
|
return dock_menu_;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (BOOL) application: (NSApplication*)app openFile:(NSString*)filename {
|
|
|
|
qDebug() << "Wants to open:" << [filename UTF8String];
|
|
|
|
|
|
|
|
if (application_handler_->LoadUrl(QString::fromUtf8([filename UTF8String]))) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
2010-04-27 20:44:19 +02:00
|
|
|
@end
|
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
@implementation MacApplication
|
|
|
|
|
|
|
|
- (id) init {
|
|
|
|
if ((self = [super init])) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[self SetShortcutHandler:nil];
|
|
|
|
[self SetApplicationHandler:nil];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (MacGlobalShortcutBackend*) shortcut_handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
return shortcut_handler_;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
shortcut_handler_ = handler;
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (PlatformInterface*) application_handler {
|
2010-04-14 23:27:27 +02:00
|
|
|
return application_handler_;
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
- (void) SetApplicationHandler: (PlatformInterface*)handler {
|
2010-04-27 20:44:19 +02:00
|
|
|
AppDelegate* delegate = [[AppDelegate alloc] initWithHandler:handler];
|
|
|
|
[self setDelegate:delegate];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) sendEvent: (NSEvent*)event {
|
|
|
|
if ([event type] == NSSystemDefined && [event subtype] == 8) {
|
|
|
|
int keycode = (([event data1] & 0xFFFF0000) >> 16);
|
|
|
|
int keyflags = ([event data1] & 0x0000FFFF);
|
|
|
|
int keystate = (((keyflags & 0xFF00) >> 8)) == 0xA;
|
|
|
|
int keyrepeat = (keyflags & 0x1);
|
|
|
|
|
|
|
|
[self mediaKeyEvent: keycode state: keystate repeat: keyrepeat];
|
|
|
|
}
|
|
|
|
|
|
|
|
[super sendEvent: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat {
|
2010-04-14 23:27:27 +02:00
|
|
|
if (!shortcut_handler_) {
|
2010-04-13 15:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state == 0) {
|
2010-06-17 22:31:34 +02:00
|
|
|
shortcut_handler_->MacMediaKeyPressed(key);
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
namespace mac {
|
|
|
|
|
|
|
|
void MacMain() {
|
2010-04-22 15:57:46 +02:00
|
|
|
[[NSAutoreleasePool alloc] init];
|
2010-04-13 15:55:54 +02:00
|
|
|
// Creates and sets the magic global variable so QApplication will find it.
|
|
|
|
[MacApplication sharedApplication];
|
2010-04-15 17:23:12 +02:00
|
|
|
// Creates and sets the magic global variable for Sparkle.
|
|
|
|
[[SUUpdater sharedUpdater] setDelegate: NSApp];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
void SetShortcutHandler(MacGlobalShortcutBackend* handler) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[NSApp SetShortcutHandler: handler];
|
|
|
|
}
|
|
|
|
|
2010-06-14 15:22:45 +02:00
|
|
|
void SetApplicationHandler(PlatformInterface* handler) {
|
2010-04-14 23:27:27 +02:00
|
|
|
[NSApp SetApplicationHandler: handler];
|
2010-04-13 15:55:54 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 17:23:12 +02:00
|
|
|
void CheckForUpdates() {
|
|
|
|
[[SUUpdater sharedUpdater] checkForUpdates: NSApp];
|
|
|
|
}
|
|
|
|
|
2010-06-07 11:49:20 +02:00
|
|
|
QString GetBundlePath() {
|
|
|
|
CFURLRef app_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
CFStringRef mac_path = CFURLCopyFileSystemPath(app_url, kCFURLPOSIXPathStyle);
|
2010-06-07 13:38:43 +02:00
|
|
|
const char* path = CFStringGetCStringPtr(mac_path, CFStringGetSystemEncoding());
|
2010-06-07 11:49:20 +02:00
|
|
|
QString bundle_path = QString::fromUtf8(path);
|
|
|
|
CFRelease(app_url);
|
|
|
|
CFRelease(mac_path);
|
|
|
|
return bundle_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GetResourcesPath() {
|
|
|
|
QString bundle_path = GetBundlePath();
|
|
|
|
return bundle_path + "/Contents/Resources";
|
|
|
|
}
|
|
|
|
|
2010-04-13 15:55:54 +02:00
|
|
|
} // namespace mac
|