From 4fd0f3035a4943b8cbdb91880c633a6874abe71c Mon Sep 17 00:00:00 2001 From: tmator Date: Mon, 12 Jan 2009 23:59:27 +0000 Subject: [PATCH] cocoa event wip git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1859 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DolphinWX/Src/MainNoGUI.cpp | 82 +++++++++++++++++++++++-- Source/Core/DolphinWX/Src/cocoaApp.h | 4 ++ Source/Core/DolphinWX/Src/cocoaApp.m | 44 ++++++++++++- 3 files changed, 123 insertions(+), 7 deletions(-) diff --git a/Source/Core/DolphinWX/Src/MainNoGUI.cpp b/Source/Core/DolphinWX/Src/MainNoGUI.cpp index 594d32e7f5..19682b8fce 100644 --- a/Source/Core/DolphinWX/Src/MainNoGUI.cpp +++ b/Source/Core/DolphinWX/Src/MainNoGUI.cpp @@ -7,19 +7,20 @@ #else #endif +#include "Common.h" + +#if defined(HAVE_COCOA) && HAVE_COCOA +#import "cocoaApp.h" +#endif #include "Globals.h" #include "Host.h" -#include "Common.h" #include "ISOFile.h" #include "CPUDetect.h" #include "cmdline.h" #include "Thread.h" #include "PowerPC/PowerPC.h" -#if defined(HAVE_COCOA) && HAVE_COCOA -#include "cocoaApp.h" -#endif #include "BootManager.h" void* g_pCodeWindow = NULL; @@ -93,14 +94,83 @@ void Host_SetWiiMoteConnectionState(int _State) {} //for cocoa we need to hijack the main to get event #if defined(HAVE_COCOA) && HAVE_COCOA + +@interface CocoaThread : NSObject +{ +} +- (void)cocoaThreadStart; +- (void)cocoaThreadRun:(id)sender; +- (void)cocoaThreadQuit:(NSNotification*)note; +@end + +static NSString *CocoaThreadHaveFinish = @"CocoaThreadHaveFinish"; + +int cocoaArgc; +char **cocoaArgv; int appleMain(int argc, char *argv[]); +@implementation CocoaThread + +- (void)cocoaThreadStart +{ + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cocoaThreadQuit:) name:CocoaThreadHaveFinish object:nil]; + [NSThread detachNewThreadSelector:@selector(cocoaThreadRun:) toTarget:self withObject:nil]; + +} + +- (void)cocoaThreadRun:(id)sender +{ + + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + //launch main + appleMain(cocoaArgc,cocoaArgv); + + [[NSNotificationCenter defaultCenter] postNotificationName:CocoaThreadHaveFinish object:nil]; + + [pool release]; + +} + +- (void)cocoaThreadQuit:(NSNotification*)note +{ + + [[NSNotificationCenter defaultCenter] removeObserver:self]; + +} + + +@end + + + int main(int argc, char *argv[]) { - cocoaCreateApp(); - return appleMain(argc, argv); + cocoaArgc = argc; + cocoaArgv = argv; + cocoaCreateApp(); + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + CocoaThread *thread = [[CocoaThread alloc] init]; + NSEvent *event = [[NSEvent alloc] init]; + + [thread cocoaThreadStart]; + + //cocoa event loop + while(true) + { + event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ]; + cocoaSendEvent(event); + } + + + [event release]; + [thread release]; + [pool release]; } diff --git a/Source/Core/DolphinWX/Src/cocoaApp.h b/Source/Core/DolphinWX/Src/cocoaApp.h index 259566a33a..f79d5cd19a 100644 --- a/Source/Core/DolphinWX/Src/cocoaApp.h +++ b/Source/Core/DolphinWX/Src/cocoaApp.h @@ -1,11 +1,15 @@ +#import + #ifdef __cplusplus extern "C" { #endif +void cocoaSendEvent(NSEvent *event); void cocoaCreateApp(); #ifdef __cplusplus } #endif + diff --git a/Source/Core/DolphinWX/Src/cocoaApp.m b/Source/Core/DolphinWX/Src/cocoaApp.m index b6a2ea287f..7d5a579ba0 100644 --- a/Source/Core/DolphinWX/Src/cocoaApp.m +++ b/Source/Core/DolphinWX/Src/cocoaApp.m @@ -1,5 +1,4 @@ #import "cocoaApp.h" -#import @implementation NSApplication(i) - (void)appRunning @@ -47,3 +46,46 @@ void cocoaCreateApp() } +void cocoaKeyCode(NSEvent *event) +{ + + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + NSConnection *connec = [NSConnection defaultConnection]; + + [connec setRootObject: event]; + if ([connec registerName: @"DolphinCocoaEvent"] == NO) + { + printf("error creating nsconnection\n"); + } + + [pool release]; + + +} + +void cocoaSendEvent(NSEvent *event) +{ + + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + if ( event != nil ) { + switch ([event type]) { + case NSKeyDown: + cocoaKeyCode(event); + break; + case NSKeyUp: + cocoaKeyCode(nil); + break; + default: + [NSApp sendEvent:event]; + break; + } + } + + + [pool release]; + +} + +