5 #ifdef BUILD_ECORE_EVAS_COCOA
6 #import <Cocoa/Cocoa.h>
10 #include "ecore_private.h"
11 #include "Ecore_Input.h"
12 #include "Ecore_Input_Evas.h"
14 #include "ecore_evas_private.h"
15 #include "Ecore_Evas.h"
17 #ifdef BUILD_ECORE_EVAS_COCOA
18 #include "Ecore_Cocoa.h"
19 #include "Evas_Engine_Quartz.h"
21 // FIXME: this engine has lots of problems. only 1 window at a time, drawRect looks wrong, doesnt handle resizes and more
23 static int _ecore_evas_init_count = 0;
24 static Ecore_Evas *ecore_evases = NULL;
25 static Ecore_Event_Handler *ecore_evas_event_handlers[4] = {
26 NULL, NULL, NULL, NULL
28 static Ecore_Idle_Enterer *ecore_evas_idle_enterer = NULL;
29 static Ecore_Poller *ecore_evas_event = NULL;
31 static const char *ecore_evas_cocoa_default = "EFL Cocoa";
33 @interface EvasView : NSView
39 static EvasView * evas_view;
40 static NSWindow * main_window;
42 @implementation EvasView
54 - (void)drawRect:(NSRect)rect
60 EINA_INLIST_FOREACH(ecore_evases, ee)
63 evas_damage_rectangle_add(ee->evas, 0, 0, 400, 400);
68 ctx = [[NSGraphicsContext currentContext] graphicsPort];
72 - (CGContextRef)context
80 _ecore_evas_cocoa_match(void)
86 _ecore_evas_cocoa_event_got_focus(void *data __UNUSED__, int type __UNUSED__, void *event)
90 ee = _ecore_evas_cocoa_match();
92 if (!ee) return EINA_TRUE;
99 _ecore_evas_cocoa_event_lost_focus(void *data __UNUSED__, int type __UNUSED__, void *event)
103 ee = _ecore_evas_cocoa_match();
105 if (!ee) return EINA_TRUE;
106 ee->prop.focused = 0;
112 _ecore_evas_cocoa_event_video_resize(void *data __UNUSED__, int type __UNUSED__, void *event)
114 /*Ecore_Cocoa_Event_Video_Resize *e;
118 ee = _ecore_evas_cocoa_match();
120 if (!ee) return 1; // pass on event
121 evas_output_size_set(ee->evas, e->w, e->h);
127 _ecore_evas_cocoa_event_video_expose(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
133 ee = _ecore_evas_cocoa_match();
135 if (!ee) return EINA_TRUE;
136 evas_output_size_get(ee->evas, &w, &h);
137 evas_damage_rectangle_add(ee->evas, 0, 0, w, h);
143 _ecore_evas_idle_enter(void *data __UNUSED__)
149 EINA_INLIST_FOREACH(ecore_evases, ee)
152 evas_render(ee->evas);
154 evas_norender(ee->evas);
161 _ecore_evas_cocoa_event(void *data)
163 ecore_cocoa_feed_events();
169 _ecore_evas_cocoa_init(int w, int h)
171 _ecore_evas_init_count++;
172 if (_ecore_evas_init_count > 1) return _ecore_evas_init_count;
174 ecore_event_evas_init();
176 ecore_evas_idle_enterer = ecore_idle_enterer_add(_ecore_evas_idle_enter, NULL);
177 ecore_evas_event = ecore_poller_add(ECORE_POLLER_CORE, 1, ecore_evas_event, NULL);
178 ecore_poller_poll_interval_set(ECORE_POLLER_CORE, 0.006);
180 ecore_evas_event_handlers[0] = ecore_event_handler_add(ECORE_COCOA_EVENT_GOT_FOCUS, _ecore_evas_cocoa_event_got_focus, NULL);
181 ecore_evas_event_handlers[1] = ecore_event_handler_add(ECORE_COCOA_EVENT_LOST_FOCUS, _ecore_evas_cocoa_event_lost_focus, NULL);
182 ecore_evas_event_handlers[2] = ecore_event_handler_add(ECORE_COCOA_EVENT_RESIZE, _ecore_evas_cocoa_event_video_resize, NULL);
183 ecore_evas_event_handlers[3] = ecore_event_handler_add(ECORE_COCOA_EVENT_EXPOSE, _ecore_evas_cocoa_event_video_expose, NULL);
185 return _ecore_evas_init_count;
189 _ecore_evas_cocoa_shutdown(void)
191 _ecore_evas_init_count--;
192 if (_ecore_evas_init_count == 0)
196 while (ecore_evases) _ecore_evas_free(ecore_evases);
198 for (i = 0; i < sizeof (ecore_evas_event_handlers) / sizeof (Ecore_Event_Handler*); i++)
199 ecore_event_handler_del(ecore_evas_event_handlers[i]);
200 ecore_event_evas_shutdown();
201 ecore_idle_enterer_del(ecore_evas_idle_enterer);
202 ecore_evas_idle_enterer = NULL;
203 ecore_poller_del(ecore_evas_event);
204 ecore_evas_event = NULL;
206 ecore_event_evas_shutdown();
208 if (_ecore_evas_init_count < 0) _ecore_evas_init_count = 0;
209 return _ecore_evas_init_count;
213 _ecore_evas_cocoa_free(Ecore_Evas *ee)
215 ecore_evases = (Ecore_Evas *) eina_inlist_remove(EINA_INLIST_GET(ecore_evases), EINA_INLIST_GET(ee));
216 ecore_event_window_unregister(0);
217 _ecore_evas_cocoa_shutdown();
218 ecore_cocoa_shutdown();
222 _ecore_evas_resize(Ecore_Evas *ee, int w, int h)
224 if ((w == ee->w) && (h == ee->h)) return;
228 evas_output_size_set(ee->evas, ee->w, ee->h);
229 evas_output_viewport_set(ee->evas, 0, 0, ee->w, ee->h);
230 evas_damage_rectangle_add(ee->evas, 0, 0, ee->w, ee->h);
232 if (ee->func.fn_resize) ee->func.fn_resize(ee);
236 _ecore_evas_move_resize(Ecore_Evas *ee, int x __UNUSED__, int y __UNUSED__, int w, int h)
238 if ((w == ee->w) && (h == ee->h)) return;
242 evas_output_size_set(ee->evas, ee->w, ee->h);
243 evas_output_viewport_set(ee->evas, 0, 0, ee->w, ee->h);
244 evas_damage_rectangle_add(ee->evas, 0, 0, ee->w, ee->h);
246 if (ee->func.fn_resize) ee->func.fn_resize(ee);
250 _ecore_evas_object_cursor_del(void *data, Evas *e, Evas_Object *obj, void *event_info)
256 ee->prop.cursor.object = NULL;
260 _ecore_evas_object_cursor_set(Ecore_Evas *ee, Evas_Object *obj, int layer, int hot_x, int hot_y)
264 if (ee->prop.cursor.object) evas_object_del(ee->prop.cursor.object);
268 ee->prop.cursor.object = NULL;
269 ee->prop.cursor.layer = 0;
270 ee->prop.cursor.hot.x = 0;
271 ee->prop.cursor.hot.y = 0;
275 ee->prop.cursor.object = obj;
276 ee->prop.cursor.layer = layer;
277 ee->prop.cursor.hot.x = hot_x;
278 ee->prop.cursor.hot.y = hot_y;
280 evas_pointer_output_xy_get(ee->evas, &x, &y);
281 evas_object_layer_set(ee->prop.cursor.object, ee->prop.cursor.layer);
282 evas_object_move(ee->prop.cursor.object,
283 x - ee->prop.cursor.hot.x,
284 y - ee->prop.cursor.hot.y);
286 evas_object_pass_events_set(ee->prop.cursor.object, 1);
288 if (evas_pointer_inside_get(ee->evas))
289 evas_object_show(ee->prop.cursor.object);
291 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, _ecore_evas_object_cursor_del, ee);
294 static Ecore_Evas_Engine_Func _ecore_cocoa_engine_func =
296 _ecore_evas_cocoa_free,
314 _ecore_evas_move_resize,
328 _ecore_evas_object_cursor_set,
346 ecore_evas_cocoa_new(const char* name, int w, int h)
348 #ifdef BUILD_ECORE_EVAS_COCOA
349 Evas_Engine_Info_Quartz *einfo;
354 name = ecore_evas_cocoa_default;
356 rmethod = evas_render_method_lookup("quartz");
357 if (!rmethod) return NULL;
359 if (!ecore_cocoa_init(name)) return NULL;
361 ee = calloc(1, sizeof(Ecore_Evas));
363 goto shutdown_ecore_cocoa;
365 ECORE_MAGIC_SET(ee, ECORE_MAGIC_EVAS);
367 _ecore_evas_cocoa_init(w, h);
369 ecore_event_window_register(0, ee, ee->evas, _ecore_evas_mouse_move_process);
371 ee->engine.func = (Ecore_Evas_Engine_Func *)&_ecore_cocoa_engine_func;
373 ee->driver = "quartz";
374 if (name) ee->name = strdup(name);
387 ee->prop.focused = 1;
388 ee->prop.borderless = 1;
389 ee->prop.override = 1;
390 ee->prop.maximized = 1;
391 ee->prop.withdrawn = 0;
395 ee->evas = evas_new();
398 evas_data_attach_set(ee->evas, ee);
399 evas_output_method_set(ee->evas, rmethod);
401 // Set up the Cocoa runtime
402 [[NSAutoreleasePool alloc] init];
403 [NSApplication sharedApplication];
405 // Register ourselves as a full-fledged Cocoa app, instead of a NSUIElement.
406 // This gives benefits like window focus and a dock icon!
407 ProcessSerialNumber psn = { 0, kCurrentProcess };
408 TransformProcessType (&psn, kProcessTransformToForegroundApplication);
410 [NSApp finishLaunching];
412 // Create our main window, and embed an EvasView in it
413 main_window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,w,h) styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask) backing:NSBackingStoreBuffered defer:NO screen:nil];
414 /* FIXME: manage the case where main_window is NULL witht a goto free_evas; */
415 [main_window makeKeyAndOrderFront:NSApp];
416 [main_window setTitle:[NSString stringWithUTF8String:name]];
417 [main_window makeMainWindow];
418 [main_window setAcceptsMouseMovedEvents:YES];
419 [NSApp activateIgnoringOtherApps:YES];
421 evas_view = [[EvasView alloc] initWithFrame:NSMakeRect(0,0,w,h)];
422 [[main_window contentView] addSubview:evas_view];
424 // drawRect: must be run at least once, to make sure we've set ctx
427 evas_output_size_set(ee->evas, w, h);
428 evas_output_viewport_set(ee->evas, 0, 0, w, h);
430 einfo = (Evas_Engine_Info_Quartz*) evas_engine_info_get(ee->evas);
434 einfo->info.context = [[evas_view context] retain];
435 evas_engine_info_set(ee->evas, (Evas_Engine_Info *)einfo);
437 ecore_evases = (Ecore_Evas *) eina_inlist_prepend(EINA_INLIST_GET(ecore_evases), EINA_INLIST_GET(ee));
439 evas_event_feed_mouse_in(ee->evas, (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff), NULL);
440 evas_focus_in(ee->evas);
445 /* FIXME: free window here */
451 _ecore_evas_cocoa_shutdown();
453 shutdown_ecore_cocoa:
454 ecore_cocoa_shutdown();
458 ERR("OUTCH name='%s' size=%dx%d!", name ? name : "", w, h);