1 #import <AppKit/NSApplication.h>
2 #import <Foundation/NSRunLoop.h>
3 #include "cocoadisplay.h"
5 void convert_event(NSEvent *, CocoaEvent *);
10 CFMutableArrayRef event_queue;
13 const void *copy_event(CFAllocatorRef, const void *);
14 void free_event(CFAllocatorRef, const void *);
16 CocoaDisplay *create_display()
18 CocoaDisplay *display = (CocoaDisplay *)malloc(sizeof(CocoaDisplay));
19 display->app = [NSApplication sharedApplication];
20 // This makes windows appear when run from terminal
21 if([display->app respondsToSelector:@selector(setActivationPolicy:)])
22 [display->app setActivationPolicy:NSApplicationActivationPolicyRegular];
23 [display->app finishLaunching];
25 CFArrayCallBacks callbacks;
26 callbacks.version = 0;
27 callbacks.retain = copy_event;
28 callbacks.release = free_event;
29 callbacks.copyDescription = NULL;
30 callbacks.equal = NULL;
31 display->event_queue = CFArrayCreateMutable(NULL, 0, &callbacks);
36 void destroy_display(CocoaDisplay *display)
38 CFRelease(display->event_queue);
42 void queue_event(CocoaDisplay *display, CocoaEvent *event)
44 CFArrayAppendValue(display->event_queue, event);
47 bool get_event(CocoaDisplay *display, CocoaEvent *buf)
49 if(CFArrayGetCount(display->event_queue))
51 *buf = *(const CocoaEvent *)CFArrayGetValueAtIndex(display->event_queue, 0);
52 CFArrayRemoveValueAtIndex(display->event_queue, 0);
56 NSEvent *event = [display->app nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
59 [display->app sendEvent:event];
60 convert_event(event, buf);
67 const void *copy_event(CFAllocatorRef allocator, const void *event)
69 void *copy = CFAllocatorAllocate(allocator, sizeof(CocoaEvent), 0);
70 memcpy(copy, event, sizeof(CocoaEvent));
74 void free_event(CFAllocatorRef allocator, const void *event)
76 CFAllocatorDeallocate(allocator, (void *)event);