summaryrefslogtreecommitdiff
path: root/src/modules/ecore_evas/engines/cocoa/ecore_evas_cocoa.c
blob: 75ba1da62c9192b7adee8debb3d3d03888d84374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore.h>
#include "ecore_private.h"
#include <Ecore_Input.h>
#include <Ecore_Input_Evas.h>

#include <Ecore_Cocoa.h>
#include <Evas_Engine_GL_Cocoa.h>

#include "Ecore_Evas.h"
#include "ecore_evas_private.h"
#include "ecore_evas_cocoa.h"

#ifdef EAPI
# undef EAPI
#endif

#ifdef __GNUC__
# if __GNUC__ >= 4
#  define EAPI __attribute__ ((visibility("default")))
# else
#  define EAPI
# endif
#else
# define EAPI
#endif

static int                      _ecore_evas_init_count = 0;
static Ecore_Event_Handler      *ecore_evas_event_handlers[4];

static const char *_iface_name = "opengl_cocoa";
static const int _iface_version = 1;

static inline Ecore_Evas *
_ecore_evas_cocoa_match(Ecore_Cocoa_Object *cocoa_win)
{
   return ecore_event_window_match((Ecore_Window)cocoa_win);
}

static Eina_Bool
_ecore_evas_cocoa_event_got_focus(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Cocoa_Event_Window_Focused *e = event;
   Ecore_Evas                       *ee;

   ee = _ecore_evas_cocoa_match(e->cocoa_window);
   if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;

   _ecore_evas_focus_device_set(ee, NULL, EINA_TRUE);
   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_ecore_evas_cocoa_event_lost_focus(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Cocoa_Event_Window_Unfocused *e = event;
   Ecore_Evas                         *ee;

   ee = _ecore_evas_cocoa_match(e->cocoa_window);
   if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;

   _ecore_evas_focus_device_set(ee, NULL, EINA_TRUE);
   return ECORE_CALLBACK_PASS_ON;
}

static void
_ecore_evas_resize_common(Ecore_Evas *ee,
                          int         w,
                          int         h,
                          Eina_Bool   resize_cocoa)
{
   DBG("%p (old: %i %i, new: %i %i, resize_cocoa: %s)",
       ee, ee->w, ee->h, w, h, resize_cocoa ? "yes" : "no");

   ee->req.w = w;
   ee->req.h = h;

   if ((ee->w != w) || (ee->h != h))
     {
        ee->w = w;
        ee->h = h;
        if (resize_cocoa)
          ecore_cocoa_window_resize((Ecore_Cocoa_Window *)ee->prop.window, w, h);

        if (ECORE_EVAS_PORTRAIT(ee))
          {
             evas_output_size_set(ee->evas, ee->w, ee->h);
             evas_output_viewport_set(ee->evas, 0, 0, ee->w, ee->h);
          }
        else
          {
             evas_output_size_set(ee->evas, ee->h, ee->w);
             evas_output_viewport_set(ee->evas, 0, 0, ee->h, ee->w);
          }

        if (ee->func.fn_resize) ee->func.fn_resize(ee);
     }
}

static Eina_Bool
_ecore_evas_cocoa_event_window_resize(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Cocoa_Event_Window_Resize_Request *e = event;
   Ecore_Evas                   *ee;

   ee = _ecore_evas_cocoa_match(e->cocoa_window);
   if (EINA_UNLIKELY(!ee))
     {
        ERR("Unregistered Ecore_Evas for Cocoa window %p", e->cocoa_window);
        return ECORE_CALLBACK_PASS_ON;
     }
   DBG("%p", ee);

   /* Do the resize */
   _ecore_evas_resize_common(ee, e->w, e->h, EINA_FALSE);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_ecore_evas_cocoa_event_window_destroy(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Cocoa_Event_Window_Destroy *e = event;
   Ecore_Evas                       *ee;

   ee = _ecore_evas_cocoa_match(e->cocoa_window);
   if (!ee)
     {
        ERR("Unregistered Ecore_Evas for Cocoa window %p", e->cocoa_window);
        return ECORE_CALLBACK_PASS_ON;
     }
   DBG("%p", ee);

   if (ee->func.fn_delete_request) ee->func.fn_delete_request(ee);

   return ECORE_CALLBACK_PASS_ON;
}

static int
_ecore_evas_cocoa_init(void)
{
   DBG("%i", _ecore_evas_init_count);

   _ecore_evas_init_count++;
   if (_ecore_evas_init_count > 1)
     return _ecore_evas_init_count;

   ecore_event_evas_init();

   ecore_evas_event_handlers[0] =
      ecore_event_handler_add(ECORE_COCOA_EVENT_WINDOW_UNFOCUSED,
                              _ecore_evas_cocoa_event_lost_focus, NULL);
   ecore_evas_event_handlers[1] =
      ecore_event_handler_add(ECORE_COCOA_EVENT_WINDOW_FOCUSED,
                              _ecore_evas_cocoa_event_got_focus, NULL);
   ecore_evas_event_handlers[2] =
      ecore_event_handler_add(ECORE_COCOA_EVENT_WINDOW_RESIZE_REQUEST,
                              _ecore_evas_cocoa_event_window_resize, NULL);
   ecore_evas_event_handlers[3] =
      ecore_event_handler_add(ECORE_COCOA_EVENT_WINDOW_DESTROY,
                              _ecore_evas_cocoa_event_window_destroy, NULL);

   return _ecore_evas_init_count;
}

static int
_ecore_evas_cocoa_shutdown(void)
{
   DBG("%i", _ecore_evas_init_count);
   _ecore_evas_init_count--;
   if (_ecore_evas_init_count == 0)
     {
        unsigned int i;

        for (i = 0; i < EINA_C_ARRAY_LENGTH(ecore_evas_event_handlers); i++)
          ecore_event_handler_del(ecore_evas_event_handlers[i]);

        ecore_event_evas_shutdown();
     }
   return _ecore_evas_init_count;
}

static void
_ecore_evas_cocoa_free(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_free((Ecore_Cocoa_Window *)ee->prop.window);
   ecore_event_window_unregister(ee->prop.window);
   _ecore_evas_cocoa_shutdown();
   ecore_cocoa_shutdown();
}

static void
_ecore_evas_callback_resize_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
{
   if (ee) ee->func.fn_resize = func;
}

static void
_ecore_evas_size_min_set(Ecore_Evas *ee, int w, int h)
{
   ecore_cocoa_window_size_min_set((Ecore_Cocoa_Window *)ee->prop.window, w, h);
}

static void
_ecore_evas_size_max_set(Ecore_Evas *ee, int w, int h)
{
   ecore_cocoa_window_size_max_set((Ecore_Cocoa_Window *)ee->prop.window, w, h);
}

static void
_ecore_evas_size_step_set(Ecore_Evas *ee, int w, int h)
{
   ecore_cocoa_window_size_step_set((Ecore_Cocoa_Window *)ee->prop.window, w, h);
}

static void
_ecore_evas_move(Ecore_Evas *ee, int x, int y)
{
   DBG("%p", ee);
   ecore_cocoa_window_move((Ecore_Cocoa_Window *)ee->prop.window, x, y);
}

static void
_ecore_evas_resize(Ecore_Evas *ee, int w, int h)
{
   DBG("%p", ee);
   _ecore_evas_resize_common(ee, w, h, EINA_TRUE);
}

static void
_ecore_evas_move_resize(Ecore_Evas *ee, int x, int y, int w, int h)
{
   DBG("%p", ee);
   if ((ee->w == w) && (ee->h == h) && (x == ee->x) && (y == ee->y))
     return;

   ee->req.x = x;
   ee->req.y = y;
   ee->req.w = w;
   ee->req.h = h;
   ee->w = w;
   ee->h = h;
   ee->x = x;
   ee->y = y;

   ecore_cocoa_window_move_resize((Ecore_Cocoa_Window *)ee->prop.window, x, y, w, h);

   evas_output_size_set(ee->evas, ee->w, ee->h);
   evas_output_viewport_set(ee->evas, 0, 0, ee->w, ee->h);
   evas_damage_rectangle_add(ee->evas, 0, 0, ee->w, ee->h);

   if (ee->func.fn_resize) ee->func.fn_resize(ee);
}


static void
_ecore_evas_show(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_show((Ecore_Cocoa_Window *)ee->prop.window);
   evas_damage_rectangle_add(ee->evas, 0, 0, ee->w, ee->h);

   ee->prop.withdrawn = EINA_FALSE;
   if (ee->func.fn_state_change) ee->func.fn_state_change(ee);

   if (ee->visible) return;
   ee->visible = 1;
   ee->should_be_visible = 1;
   ee->draw_ok = EINA_TRUE;
   if (ee->func.fn_show) ee->func.fn_show(ee);
}


static void
_ecore_evas_hide(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_hide((Ecore_Cocoa_Window *)ee->prop.window);

   if (ee->prop.override)
     {
        ee->prop.withdrawn = EINA_TRUE;
        if (ee->func.fn_state_change) ee->func.fn_state_change(ee);
     }

   if (!ee->visible) return;
   ee->visible = 0;
   ee->should_be_visible = 0;
   ee->draw_ok = EINA_FALSE;

   if (ee->func.fn_hide) ee->func.fn_hide(ee);
}

static void
_ecore_evas_raise(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_raise((Ecore_Cocoa_Window *)ee->prop.window);
}

static void
_ecore_evas_lower(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_lower((Ecore_Cocoa_Window *)ee->prop.window);
}

static void
_ecore_evas_activate(Ecore_Evas *ee)
{
   DBG("%p", ee);

   ecore_cocoa_window_activate((Ecore_Cocoa_Window *)ee->prop.window);
}

static void
_ecore_evas_iconified_set(Ecore_Evas *ee, Eina_Bool on)
{
   DBG("%p", ee);

   ecore_cocoa_window_iconified_set((Ecore_Cocoa_Window *)ee->prop.window, on);
}

static void
_ecore_evas_title_set(Ecore_Evas *ee, const char *title)
{
   DBG("%p: %s", ee, title);

   if (eina_streq(ee->prop.title, title)) return;
   if (ee->prop.title) free(ee->prop.title);
   ee->prop.title = NULL;
   if (title) ee->prop.title = strdup(title);
   ecore_cocoa_window_title_set((Ecore_Cocoa_Window *)ee->prop.window,
                                ee->prop.title);
}

static void
_ecore_evas_object_cursor_set(Ecore_Evas *ee, Evas_Object *obj,
                              int layer EINA_UNUSED, int hot_x EINA_UNUSED,
                              int hot_y EINA_UNUSED)
{
   Ecore_Cocoa_Window *const win = (Ecore_Cocoa_Window *)(ee->prop.window);

   if (obj != _ecore_evas_default_cursor_image_get(ee))
     ecore_cocoa_window_cursor_show(win, EINA_FALSE);
}

static void
_ecore_evas_object_cursor_unset(Ecore_Evas *ee)
{
   Ecore_Cocoa_Window *const win = (Ecore_Cocoa_Window *)(ee->prop.window);
   ecore_cocoa_window_cursor_show(win, EINA_TRUE);
}

static void
_ecore_evas_withdrawn_set(Ecore_Evas *ee, Eina_Bool on)
{
   if (on)
     _ecore_evas_hide(ee);
   else
     _ecore_evas_show(ee);
}

static int
_ecore_evas_engine_cocoa_init(Ecore_Evas *ee)
{
   Evas_Engine_Info_GL_Cocoa *einfo;
   const char                *driver;
   int                        rmethod;

   DBG("%p", ee);

   driver = "gl_cocoa";

   rmethod = evas_render_method_lookup(driver);
   if (!rmethod)
     return 0;

   ee->driver = driver;
   evas_output_method_set(ee->evas, rmethod);

   einfo = (Evas_Engine_Info_GL_Cocoa *)evas_engine_info_get(ee->evas);
   if (einfo)
     {
        einfo->window = (void *)ee->prop.window;
        if (!evas_engine_info_set(ee->evas, (Evas_Engine_Info *)einfo))
          {
             ERR("evas_engine_info_set() for engine '%s' failed.", ee->driver);
             return 0;
          }
        ecore_cocoa_window_view_set(einfo->window, einfo->view);
     }
   else
     {
        ERR("evas_engine_info_set() init engine '%s' failed.", ee->driver);
        return 0;
     }

   return 1;
}

static void
_ecore_evas_screen_geometry_get(const Ecore_Evas *ee EINA_UNUSED, int *x, int *y, int *w, int *h)
{
   if (x) *x = 0;
   if (y) *y = 0;
   ecore_cocoa_screen_size_get(NULL, w, h);
   DBG("screen geometry_get  %dx%d", *w, *h);
}


static void
_ecore_evas_callback_delete_request_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
{
   ee->func.fn_delete_request = func;
}

static Ecore_Evas_Engine_Func _ecore_cocoa_engine_func =
  {
    _ecore_evas_cocoa_free,
    _ecore_evas_callback_resize_set,
    NULL,
    NULL,
    NULL,
    _ecore_evas_callback_delete_request_set,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    _ecore_evas_move, //move
    NULL,
    _ecore_evas_resize,
    _ecore_evas_move_resize,
    NULL, //rotation
    NULL, //shaped
    _ecore_evas_show,
    _ecore_evas_hide,
    _ecore_evas_raise,
    _ecore_evas_lower,
    _ecore_evas_activate,
    _ecore_evas_title_set,
    NULL,
    _ecore_evas_size_min_set,
    _ecore_evas_size_max_set,
    NULL,
    _ecore_evas_size_step_set,
    _ecore_evas_object_cursor_set,
    _ecore_evas_object_cursor_unset,
    NULL,
    NULL,
    _ecore_evas_iconified_set,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    _ecore_evas_withdrawn_set,
    NULL,
    NULL,
    NULL,
    NULL, //transparent
    NULL, // profiles_set
    NULL, // profile_set

    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,

    NULL,
    _ecore_evas_screen_geometry_get,
    NULL, // screen_dpi_get
    NULL,
    NULL,  // msg_send

    NULL, // fn_pointer_xy_get
    NULL, // fn_pointer_warp

    NULL, // fn_wm_rot_preferred_rotation_set
    NULL, // fn_wm_rot_available_rotations_set
    NULL, // fn_wm_rot_manual_rotation_done_set
    NULL, // fn_wm_rot_manual_rotation_done

    NULL, // fn_aux_hints_set
    NULL, // fn_animator_register
    NULL, // fn_animator_unregister

    NULL, // fn_evas_changed
    NULL, //fn_focus_device_set
    NULL, //fn_callback_focus_device_in_set
    NULL, //fn_callback_focus_device_out_set
    NULL, //fn_callback_device_mouse_in_set
    NULL, //fn_callback_device_mouse_out_set
    NULL, //fn_pointer_device_xy_get
    NULL, //fn_prepare
    NULL, //fn_last_tick_get
  };

static Ecore_Cocoa_Window *
_ecore_evas_cocoa_window_get(const Ecore_Evas *ee)
{
   /* See affectation of ee->prop.window in ecore_evas_cocoa_new_internal */
   return (Ecore_Cocoa_Window *)(ee->prop.window);
}


EAPI Ecore_Evas *
ecore_evas_cocoa_new_internal(Ecore_Cocoa_Window *parent EINA_UNUSED, int x, int y, int w, int h)
{
   Ecore_Evas *ee;
   Ecore_Evas_Interface_Cocoa *iface;

   if (!ecore_cocoa_init())
     return NULL;

   INF("x,y,w,h = %i,%i,%i,%i", x, y, w, h);

   ee = calloc(1, sizeof(Ecore_Evas));
   if (!ee)
     goto shutdown_ecore_cocoa;

   ECORE_MAGIC_SET(ee, ECORE_MAGIC_EVAS);

   _ecore_evas_cocoa_init();

   ee->engine.func = &_ecore_cocoa_engine_func;

   if (w < 1) w = 1;
   if (h < 1) h = 1;
   ee->visible = 0;
   ee->x = x;
   ee->y = y;
   ee->w = w;
   ee->h = h;
   ee->req.x = ee->x;
   ee->req.y = ee->y;
   ee->req.w = ee->w;
   ee->req.h = ee->h;

   ee->prop.max.w = 32767;
   ee->prop.max.h = 32767;
   ee->prop.layer = 4;
   ee->prop.request_pos = EINA_FALSE;
   ee->prop.sticky = EINA_FALSE;
   ee->prop.withdrawn = EINA_TRUE;
   ee->can_async_render = EINA_FALSE;

   ee->evas = evas_new();
   INF("ecore_evas: %p, evas: %p", ee, ee->evas);

   if (!ee->evas)
     goto free_name;

   evas_data_attach_set(ee->evas, ee);
   evas_output_size_set(ee->evas, w, h);
   evas_output_viewport_set(ee->evas, 0, 0, w, h);

   ee->prop.window = (Ecore_Window)ecore_cocoa_window_new(x, y, w, h);

   if (!ee->prop.window)
     {
        _ecore_evas_cocoa_shutdown();
        free(ee);
        return NULL;
     }

   if (!_ecore_evas_engine_cocoa_init(ee))
     {
        _ecore_evas_cocoa_shutdown();
        free(ee);
        return NULL;
     }

   /* Interface setup */
   iface = calloc(1, sizeof(*iface));
   if (EINA_UNLIKELY(!iface))
     {
        _ecore_evas_cocoa_shutdown();
        free(ee);
        return NULL;
     }
   iface->base.name = _iface_name;
   iface->base.version = _iface_version;
   iface->window_get = _ecore_evas_cocoa_window_get;
   ee->engine.ifaces = eina_list_append(ee->engine.ifaces, iface);

   _ecore_evas_register(ee);
   ecore_event_window_register(ee->prop.window, ee, ee->evas,
                               (Ecore_Event_Mouse_Move_Cb)_ecore_evas_mouse_move_process,
                               (Ecore_Event_Multi_Move_Cb)_ecore_evas_mouse_multi_move_process,
                               (Ecore_Event_Multi_Down_Cb)_ecore_evas_mouse_multi_down_process,
                               (Ecore_Event_Multi_Up_Cb)_ecore_evas_mouse_multi_up_process);
   _ecore_event_window_direct_cb_set(ee->prop.window, _ecore_evas_input_direct_cb);

   evas_event_feed_mouse_in(ee->evas, (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff), NULL);

   return ee;

   //free_window:
   /* FIXME: free window here */
   //free_evas:
   // free(ee->evas);
 free_name:
   free(ee->name);
   //free_ee:
   _ecore_evas_cocoa_shutdown();
   free(ee);
 shutdown_ecore_cocoa:
   ecore_cocoa_shutdown();

   return NULL;
}