5 #include <Cocoa/Cocoa.h>
10 #include <ecore_private.h>
11 #include <Ecore_Input.h>
13 #include "Ecore_Quartz.h"
14 #include "Ecore_Quartz_Keys.h"
16 EAPI int ECORE_QUARTZ_EVENT_GOT_FOCUS = 0;
17 EAPI int ECORE_QUARTZ_EVENT_LOST_FOCUS = 0;
18 EAPI int ECORE_QUARTZ_EVENT_RESIZE = 0;
19 EAPI int ECORE_QUARTZ_EVENT_EXPOSE = 0;
21 static int _ecore_quartz_init_count = 0;
26 ecore_quartz_init(const char *name __UNUSED__)
28 if (++_ecore_quartz_init_count != 1)
29 return _ecore_quartz_init_count;
31 if (!ecore_event_init())
32 return --_ecore_quartz_init_count;
34 ECORE_QUARTZ_EVENT_GOT_FOCUS = ecore_event_type_new();
35 ECORE_QUARTZ_EVENT_LOST_FOCUS = ecore_event_type_new();
36 ECORE_QUARTZ_EVENT_RESIZE = ecore_event_type_new();
37 ECORE_QUARTZ_EVENT_EXPOSE = ecore_event_type_new();
39 return _ecore_quartz_init_count;
43 * Shuts down the Ecore_Quartz library.
44 * @return @c The number of times the system has been initialised without
46 * @ingroup Ecore_Quartz_Library_Group
49 ecore_quartz_shutdown(void)
51 if (--_ecore_quartz_init_count != 0)
52 return _ecore_quartz_init_count;
54 ecore_event_shutdown();
56 return _ecore_quartz_init_count;
60 ecore_quartz_feed_events(void)
62 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.001];
63 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
65 inMode:NSDefaultRunLoopMode
68 if (!event) return; // SDL loops until null; maybe we should do that too. or not.
70 unsigned int time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);
75 case NSLeftMouseDragged:
76 case NSRightMouseDragged:
77 case NSOtherMouseDragged:
79 Ecore_Event_Mouse_Move * ev = calloc(1, sizeof(Ecore_Event_Mouse_Move));
81 ev->x = [event locationInWindow].x;
82 ev->y = [event locationInWindow].y;
86 ev->window = [event window];
87 ev->modifiers = 0; /* FIXME: keep modifier around. */
89 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
91 [NSApp sendEvent:event]; // pass along mouse events, for window manager
95 case NSRightMouseDown:
96 case NSOtherMouseDown:
98 Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button));
100 ev->x = [event locationInWindow].x;
101 ev->y = [event locationInWindow].y;
104 ev->timestamp = time;
105 ev->buttons = [event buttonNumber] + 1; // Apple indexes buttons from 0
107 if ([event clickCount] == 2)
108 ev->double_click = 1;
110 ev->double_click = 0;
112 if ([event clickCount] >= 3)
113 ev->triple_click = 1;
115 ev->triple_click = 0;
117 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
119 [NSApp sendEvent:event]; // pass along mouse events, for window manager
126 Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button));
128 ev->x = [event locationInWindow].x;
129 ev->y = [event locationInWindow].y;
132 ev->timestamp = time;
133 ev->buttons = [event buttonNumber] + 1; // Apple indexes buttons from 0
135 if ([event clickCount] == 2)
136 ev->double_click = 1;
138 ev->double_click = 0;
140 if ([event clickCount] >= 3)
141 ev->triple_click = 1;
143 ev->triple_click = 0;
145 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
147 [NSApp sendEvent:event]; // pass along mouse events, for window manager
155 ev = calloc(1, sizeof (Ecore_Event_Key));
157 ev->timestamp = time;
159 for (i = 0; i < sizeof (keystable) / sizeof (struct _ecore_quartz_keys_s); ++i)
161 if (keystable[i].code == tolower([[event charactersIgnoringModifiers] characterAtIndex:0]))
163 ev->keyname = keystable[i].name;
164 ev->string = keystable[i].compose;
166 ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
178 ev = calloc(1, sizeof (Ecore_Event_Key));
180 ev->timestamp = time;
182 for (i = 0; i < sizeof (keystable) / sizeof (struct _ecore_quartz_keys_s); ++i)
184 if (keystable[i].code == tolower([[event charactersIgnoringModifiers] characterAtIndex:0]))
186 ev->keyname = keystable[i].name;
187 ev->string = keystable[i].compose;
189 ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
198 int flags = [event modifierFlags];
200 Ecore_Event_Key *evDown = NULL;
201 Ecore_Event_Key *evUp = NULL;
203 evDown = calloc(1, sizeof (Ecore_Event_Key));
206 evUp = calloc(1, sizeof (Ecore_Event_Key));
213 // Turn special key flags on
214 if (flags & NSShiftKeyMask)
215 evDown->keyname = "Shift_L";
216 else if (flags & NSControlKeyMask)
217 evDown->keyname = "Control_L";
218 else if (flags & NSAlternateKeyMask)
219 evDown->keyname = "Alt_L";
220 else if (flags & NSCommandKeyMask)
221 evDown->keyname = "Super_L";
222 else if (flags & NSAlphaShiftKeyMask)
223 evDown->keyname = "Caps_Lock";
227 evDown->timestamp = time;
229 ecore_event_add(ECORE_EVENT_KEY_DOWN, evDown, NULL, NULL);
234 int changed_flags = flags ^ old_flags;
236 // Turn special key flags off
237 if (changed_flags & NSShiftKeyMask)
238 evUp->keyname = "Shift_L";
239 else if (changed_flags & NSControlKeyMask)
240 evUp->keyname = "Control_L";
241 else if (changed_flags & NSAlternateKeyMask)
242 evUp->keyname = "Alt_L";
243 else if (changed_flags & NSCommandKeyMask)
244 evUp->keyname = "Super_L";
245 else if (changed_flags & NSAlphaShiftKeyMask)
246 evUp->keyname = "Caps_Lock";
250 evUp->timestamp = time;
252 ecore_event_add(ECORE_EVENT_KEY_UP, evUp, NULL, NULL);
259 case NSAppKitDefined:
261 if ([event subtype] == NSApplicationActivatedEventType)
262 ecore_event_add(ECORE_QUARTZ_EVENT_GOT_FOCUS, NULL, NULL, NULL);
263 else if ([event subtype] == NSApplicationDeactivatedEventType)
264 ecore_event_add(ECORE_QUARTZ_EVENT_LOST_FOCUS, NULL, NULL, NULL);
265 [NSApp sendEvent:event]; // pass along AppKit events, for window manager
274 [NSApp sendEvent:event];