From bab22b7ac1f2d97290209910fcb6f2cea68ea972 Mon Sep 17 00:00:00 2001 From: Alan Ott Date: Tue, 13 Sep 2011 10:48:39 +0800 Subject: [PATCH] Convert to Cocoa for event handling on the Test GUI on Mac Carbon is no good on 10.5 and later on 64-bit. Github user dportabella found a workaround in Cocoa[1], and I added code to make it handle the events from the dock, like the carbon version did. [1] https://github.com/signal11/hidapi/issues/14 --- testgui/Makefile.mac | 13 +++-- testgui/mac_support.h | 6 ++- testgui/mac_support_cocoa.m | 94 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 testgui/mac_support_cocoa.m diff --git a/testgui/Makefile.mac b/testgui/Makefile.mac index a61d23f..8acd1e1 100644 --- a/testgui/Makefile.mac +++ b/testgui/Makefile.mac @@ -11,10 +11,11 @@ all: testgui CC=gcc CXX=g++ COBJS=../mac/hid.o -CPPOBJS=test.o mac_support.o -OBJS=$(COBJS) $(CPPOBJS) +CPPOBJS=test.o +OBJCOBJS=mac_support_cocoa.o +OBJS=$(COBJS) $(CPPOBJS) $(OBJCOBJS) CFLAGS=-I../hidapi -Wall -g -c `fox-config --cflags` -LIBS=`fox-config --libs` -framework IOKit -framework CoreFoundation -framework Carbon +LIBS=`fox-config --libs` -framework IOKit -framework CoreFoundation -framework Cocoa testgui: $(OBJS) @@ -29,7 +30,11 @@ $(COBJS): %.o: %.c $(CPPOBJS): %.o: %.cpp $(CXX) $(CFLAGS) $< -o $@ +$(OBJCOBJS): %.o: %.m + $(CXX) $(CFLAGS) -x objective-c++ $< -o $@ + + clean: - rm *.o testgui + rm $(OBJS) testgui .PHONY: clean diff --git a/testgui/mac_support.h b/testgui/mac_support.h index d933076..7d9ab49 100644 --- a/testgui/mac_support.h +++ b/testgui/mac_support.h @@ -9,7 +9,9 @@ #ifndef MAC_SUPPORT_H__ #define MAC_SUPPORT_H__ -void init_apple_message_system(); -void check_apple_events(); +extern "C" { + void init_apple_message_system(); + void check_apple_events(); +} #endif diff --git a/testgui/mac_support_cocoa.m b/testgui/mac_support_cocoa.m new file mode 100644 index 0000000..75de7e9 --- /dev/null +++ b/testgui/mac_support_cocoa.m @@ -0,0 +1,94 @@ +/******************************* + Mac support for HID Test GUI + + Alan Ott + Signal 11 Software +*******************************/ + +#include +#import + +extern FXMainWindow *g_main_window; + + +@interface MyAppDelegate : NSObject +{ +} +@end + +@implementation MyAppDelegate +- (void) applicationWillBecomeActive:(NSNotification*)notif +{ + printf("WillBecomeActive\n"); + g_main_window->show(); + +} + +- (void) applicationWillTerminate:(NSNotification*)notif +{ + /* Doesn't get called. Not sure why */ + printf("WillTerminate\n"); + FXApp::instance()->exit(); +} + +- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender +{ + /* Doesn't get called. Not sure why */ + printf("ShouldTerminate\n"); + return YES; +} + +- (void) applicationWillHide:(NSNotification*)notif +{ + printf("WillHide\n"); + g_main_window->hide(); +} + +- (void) handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent +{ + printf("QuitEvent\n"); + FXApp::instance()->exit(); +} + +@end + +extern "C" { + +void +init_apple_message_system() +{ + static MyAppDelegate *d = [MyAppDelegate new]; + + [[NSApplication sharedApplication] setDelegate:d]; + + /* Register for Apple Events. */ + /* This is from + http://stackoverflow.com/questions/1768497/application-exit-event */ + NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager]; + [aem setEventHandler:d + andSelector:@selector(handleQuitEvent:withReplyEvent:) + forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; +} + +void +check_apple_events() +{ + NSApplication *app = [NSApplication sharedApplication]; + + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + while (1) { + NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask + untilDate:nil + inMode:NSDefaultRunLoopMode + dequeue:YES]; + if (event == NULL) + break; + else { + //printf("Event happened: Type: %d\n", event->_type); + [app sendEvent: event]; + } + } + [pool release]; +} + +} /* extern "C" */