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];
21 //[display->app setActivationPolicy:NSApplicationActivationPolicyRegular];
22 [display->app finishLaunching];
24 CFArrayCallBacks callbacks;
25 callbacks.version = 0;
26 callbacks.retain = copy_event;
27 callbacks.release = free_event;
28 callbacks.copyDescription = NULL;
29 callbacks.equal = NULL;
30 display->event_queue = CFArrayCreateMutable(NULL, 0, &callbacks);
35 void destroy_display(CocoaDisplay *display)
37 CFRelease(display->event_queue);
41 void queue_event(CocoaDisplay *display, CocoaEvent *event)
43 CFArrayAppendValue(display->event_queue, event);
46 bool get_event(CocoaDisplay *display, CocoaEvent *buf)
48 if(CFArrayGetCount(display->event_queue))
50 *buf = *(const CocoaEvent *)CFArrayGetValueAtIndex(display->event_queue, 0);
51 CFArrayRemoveValueAtIndex(display->event_queue, 0);
55 NSEvent *event = [display->app nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
58 [display->app sendEvent:event];
59 convert_event(event, buf);
66 const void *copy_event(CFAllocatorRef allocator, const void *event)
68 void *copy = CFAllocatorAllocate(allocator, sizeof(CocoaEvent), 0);
69 memcpy(copy, event, sizeof(CocoaEvent));
73 void free_event(CFAllocatorRef allocator, const void *event)
75 CFAllocatorDeallocate(allocator, (void *)event);