]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cocoa/cocoadisplay.m
Basic OS X support
[libs/gui.git] / source / graphics / cocoa / cocoadisplay.m
1 #import <AppKit/NSApplication.h>
2 #import <Foundation/NSRunLoop.h>
3 #include "cocoadisplay.h"
4
5 void convert_event(NSEvent *, CocoaEvent *);
6
7 struct _CocoaDisplay
8 {
9         NSApplication *app;
10         CFMutableArrayRef event_queue;
11 };
12
13 const void *copy_event(CFAllocatorRef, const void *);
14 void free_event(CFAllocatorRef, const void *);
15
16 CocoaDisplay *create_display()
17 {
18         CocoaDisplay *display = (CocoaDisplay *)malloc(sizeof(CocoaDisplay));
19         display->app = [NSApplication sharedApplication];
20         // Since OS X 10.6
21         //[display->app setActivationPolicy:NSApplicationActivationPolicyRegular];
22         [display->app finishLaunching];
23
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);
31
32         return display;
33 }
34
35 void destroy_display(CocoaDisplay *display)
36 {
37         CFRelease(display->event_queue);
38         free(display);
39 }
40
41 void queue_event(CocoaDisplay *display, CocoaEvent *event)
42 {
43         CFArrayAppendValue(display->event_queue, event);
44 }
45
46 bool get_event(CocoaDisplay *display, CocoaEvent *buf)
47 {
48         if(CFArrayGetCount(display->event_queue))
49         {
50                 *buf = *(const CocoaEvent *)CFArrayGetValueAtIndex(display->event_queue, 0);
51                 CFArrayRemoveValueAtIndex(display->event_queue, 0);
52                 return true;
53         }
54
55         NSEvent *event = [display->app nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
56         if(event)
57         {
58                 [display->app sendEvent:event];
59                 convert_event(event, buf);
60                 return true;
61         }
62
63         return false;
64 }
65
66 const void *copy_event(CFAllocatorRef allocator, const void *event)
67 {
68         void *copy = CFAllocatorAllocate(allocator, sizeof(CocoaEvent), 0);
69         memcpy(copy, event, sizeof(CocoaEvent));
70         return copy;
71 }
72
73 void free_event(CFAllocatorRef allocator, const void *event)
74 {
75         CFAllocatorDeallocate(allocator, (void *)event);
76 }