3 #import <AppKit/NSApplication.h>
4 #import <AppKit/NSEvent.h>
5 #import <Foundation/NSAutoReleasePool.h>
6 #import <Foundation/NSRunLoop.h>
7 #include "cocoadisplay.h"
9 void convert_event(NSEvent *, CocoaEvent *);
14 NSAutoreleasePool *arp;
15 CFMutableArrayRef event_queue;
18 const void *copy_event(CFAllocatorRef, const void *);
19 void free_event(CFAllocatorRef, const void *);
21 CocoaDisplay *create_display()
23 CocoaDisplay *display = (CocoaDisplay *)malloc(sizeof(CocoaDisplay));
24 display->arp = [[NSAutoreleasePool alloc] init];
25 display->app = [NSApplication sharedApplication];
26 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
27 // This makes windows appear when run from terminal
28 if([display->app respondsToSelector:@selector(setActivationPolicy:)])
29 [display->app setActivationPolicy:NSApplicationActivationPolicyRegular];
31 [display->app finishLaunching];
33 CFArrayCallBacks callbacks;
34 callbacks.version = 0;
35 callbacks.retain = copy_event;
36 callbacks.release = free_event;
37 callbacks.copyDescription = NULL;
38 callbacks.equal = NULL;
39 display->event_queue = CFArrayCreateMutable(NULL, 0, &callbacks);
44 void destroy_display(CocoaDisplay *display)
46 CFRelease(display->event_queue);
47 [display->arp release];
51 void queue_event(CocoaDisplay *display, CocoaEvent *event)
53 CFArrayAppendValue(display->event_queue, event);
56 bool get_event(CocoaDisplay *display, CocoaEvent *buf)
58 if(CFArrayGetCount(display->event_queue))
60 *buf = *(const CocoaEvent *)CFArrayGetValueAtIndex(display->event_queue, 0);
61 CFArrayRemoveValueAtIndex(display->event_queue, 0);
65 NSEvent *event = [display->app nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
69 [display->app sendEvent:event];
70 convert_event(event, buf);
78 const void *copy_event(CFAllocatorRef allocator, const void *event)
80 void *copy = CFAllocatorAllocate(allocator, sizeof(CocoaEvent), 0);
81 memcpy(copy, event, sizeof(CocoaEvent));
85 void free_event(CFAllocatorRef allocator, const void *event)
87 CFAllocatorDeallocate(allocator, (void *)event);