Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_evas / ecore_evas_wayland_common.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "ecore_evas_private.h"
6 #include "Ecore_Evas.h"
7
8 /* local structures */
9 typedef struct _EE_Wl_Smart_Data EE_Wl_Smart_Data;
10 struct _EE_Wl_Smart_Data
11 {
12    Evas_Object *frame;
13    Evas_Object *text;
14    Evas_Coord x, y, w, h;
15 };
16
17 static Evas_Smart *_ecore_evas_wl_common_smart = NULL;
18
19 /* local variables */
20
21 static int _ecore_evas_wl_init_count = 0;
22 static Ecore_Event_Handler *_ecore_evas_wl_event_hdls[5];
23
24 static Eina_Bool
25 _ecore_evas_wl_common_cb_mouse_in(void *data __UNUSED__, int type __UNUSED__, void *event)
26 {
27    Ecore_Evas *ee;
28    Ecore_Wl_Event_Mouse_In *ev;
29
30    ev = event;
31    ee = ecore_event_window_match(ev->window);
32    if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;
33    if (ev->window != ee->prop.window) return ECORE_CALLBACK_PASS_ON;
34    if (!ee->in)
35      {
36         if (ee->func.fn_mouse_in) ee->func.fn_mouse_in(ee);
37         ecore_event_evas_modifier_lock_update(ee->evas, ev->modifiers);
38         evas_event_feed_mouse_in(ee->evas, ev->timestamp, NULL);
39         ee->in = EINA_TRUE;
40      }
41    return ECORE_CALLBACK_PASS_ON;
42 }
43
44 static Eina_Bool
45 _ecore_evas_wl_common_cb_mouse_out(void *data __UNUSED__, int type __UNUSED__, void *event)
46 {
47    Ecore_Evas *ee;
48    Ecore_Wl_Event_Mouse_Out *ev;
49
50    ev = event;
51    ee = ecore_event_window_match(ev->window);
52    if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;
53    if (ev->window != ee->prop.window) return ECORE_CALLBACK_PASS_ON;
54    if (ee->in)
55      {
56         ecore_event_evas_modifier_lock_update(ee->evas, ev->modifiers);
57         evas_event_feed_mouse_out(ee->evas, ev->timestamp, NULL);
58         if (ee->func.fn_mouse_out) ee->func.fn_mouse_out(ee);
59         if (ee->prop.cursor.object) evas_object_hide(ee->prop.cursor.object);
60         ee->in = EINA_FALSE;
61      }
62    return ECORE_CALLBACK_PASS_ON;
63 }
64
65 static Eina_Bool
66 _ecore_evas_wl_common_cb_focus_in(void *data __UNUSED__, int type __UNUSED__, void *event)
67 {
68    Ecore_Evas *ee;
69    Ecore_Wl_Event_Focus_In *ev;
70
71    ev = event;
72    ee = ecore_event_window_match(ev->win);
73    if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;
74    if (ev->win != ee->prop.window) return ECORE_CALLBACK_PASS_ON;
75    ee->prop.focused = 1;
76    evas_focus_in(ee->evas);
77    if (ee->func.fn_focus_in) ee->func.fn_focus_in(ee);
78    return ECORE_CALLBACK_PASS_ON;
79 }
80
81 static Eina_Bool
82 _ecore_evas_wl_common_cb_focus_out(void *data __UNUSED__, int type __UNUSED__, void *event)
83 {
84    Ecore_Evas *ee;
85    Ecore_Wl_Event_Focus_In *ev;
86
87    ev = event;
88    ee = ecore_event_window_match(ev->win);
89    if ((!ee) || (ee->ignore_events)) return ECORE_CALLBACK_PASS_ON;
90    if (ev->win != ee->prop.window) return ECORE_CALLBACK_PASS_ON;
91    evas_focus_out(ee->evas);
92    ee->prop.focused = 0;
93    if (ee->func.fn_focus_out) ee->func.fn_focus_out(ee);
94    return ECORE_CALLBACK_PASS_ON;
95 }
96
97 static Eina_Bool
98 _ecore_evas_wl_common_cb_window_configure(void *data __UNUSED__, int type __UNUSED__, void *event)
99 {
100    Ecore_Evas *ee;
101    Ecore_Wl_Event_Window_Configure *ev;
102    int nw = 0, nh = 0;
103
104    ev = event;
105    ee = ecore_event_window_match(ev->win);
106    if (!ee) return ECORE_CALLBACK_PASS_ON;
107    if (ev->win != ee->prop.window) return ECORE_CALLBACK_PASS_ON;
108
109    if (ee->prop.fullscreen)
110      {
111         _ecore_evas_wl_common_move(ee, ev->x, ev->y);
112         ee->engine.func->fn_resize(ee, ev->w, ev->h);
113
114         return ECORE_CALLBACK_PASS_ON;
115      }
116
117    if ((ee->x != ev->x) || (ee->y != ev->y))
118      {
119         ee->req.x = ee->x;
120         ee->req.y = ee->y;
121         if (ee->func.fn_move) ee->func.fn_move(ee);
122      }
123
124    nw = ev->w;
125    nh = ev->h;
126
127    if ((ee->prop.maximized) || (!ee->prop.fullscreen))
128      {
129         int fw = 0, fh = 0;
130
131         evas_output_framespace_get(ee->evas, NULL, NULL, &fw, &fh);
132         nw = ev->w - fw;
133         nh = ev->h - fh;
134      }
135
136    if (ee->prop.min.w > nw) nw = ee->prop.min.w;
137    else if (nw > ee->prop.max.w) nw = ee->prop.max.w;
138    if (ee->prop.min.h > nh) nh = ee->prop.min.h;
139    else if (nh > ee->prop.max.h) nh = ee->prop.max.h;
140
141    if ((ee->w != nw) || (ee->h != nh))
142      {
143         ee->req.w = nw;
144         ee->req.h = nh;
145         if (ee->func.fn_resize) ee->func.fn_resize(ee);
146      }
147
148    return ECORE_CALLBACK_PASS_ON;
149 }
150
151 int
152 _ecore_evas_wl_common_init(void)
153 {
154    if (++_ecore_evas_wl_init_count != 1)
155      return _ecore_evas_wl_init_count;
156
157    _ecore_evas_wl_event_hdls[0] =
158      ecore_event_handler_add(ECORE_WL_EVENT_MOUSE_IN,
159                              _ecore_evas_wl_common_cb_mouse_in, NULL);
160    _ecore_evas_wl_event_hdls[1] =
161      ecore_event_handler_add(ECORE_WL_EVENT_MOUSE_OUT,
162                              _ecore_evas_wl_common_cb_mouse_out, NULL);
163    _ecore_evas_wl_event_hdls[2] =
164      ecore_event_handler_add(ECORE_WL_EVENT_FOCUS_IN,
165                              _ecore_evas_wl_common_cb_focus_in, NULL);
166    _ecore_evas_wl_event_hdls[3] =
167      ecore_event_handler_add(ECORE_WL_EVENT_FOCUS_OUT,
168                              _ecore_evas_wl_common_cb_focus_out, NULL);
169    _ecore_evas_wl_event_hdls[4] =
170      ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE,
171                              _ecore_evas_wl_common_cb_window_configure, NULL);
172
173    ecore_event_evas_init();
174
175    return _ecore_evas_wl_init_count;
176 }
177
178 int
179 _ecore_evas_wl_common_shutdown(void)
180 {
181    unsigned int i = 0;
182
183    if (--_ecore_evas_wl_init_count != 0)
184      return _ecore_evas_wl_init_count;
185
186    for (i = 0; i < sizeof(_ecore_evas_wl_event_hdls) / sizeof(Ecore_Event_Handler *); i++)
187      {
188         if (_ecore_evas_wl_event_hdls[i])
189           ecore_event_handler_del(_ecore_evas_wl_event_hdls[i]);
190      }
191
192    ecore_event_evas_shutdown();
193
194    return _ecore_evas_wl_init_count;
195 }
196
197 void
198 _ecore_evas_wl_common_pre_free(Ecore_Evas *ee)
199 {
200    if (!ee) return;
201    if (ee->engine.wl.frame) evas_object_del(ee->engine.wl.frame);
202 }
203
204 void
205 _ecore_evas_wl_common_free(Ecore_Evas *ee)
206 {
207    if (ee->engine.wl.win) ecore_wl_window_free(ee->engine.wl.win);
208    ee->engine.wl.win = NULL;
209
210    ecore_event_window_unregister(ee->prop.window);
211    ecore_evas_input_event_unregister(ee);
212
213    _ecore_evas_wl_common_shutdown();
214    ecore_wl_shutdown();
215 }
216
217 void
218 _ecore_evas_wl_common_resize(Ecore_Evas *ee, int w, int h)
219 {
220    if (w < 1) w = 1;
221    if (h < 1) h = 1;
222
223    ee->req.w = w;
224    ee->req.h = h;
225
226    if (!ee->prop.fullscreen)
227      {
228         int fw = 0, fh = 0;
229
230         if (ee->prop.min.w > w) w = ee->prop.min.w;
231         else if (w > ee->prop.max.w) w = ee->prop.max.w;
232         if (ee->prop.min.h > h) h = ee->prop.min.h;
233         else if (h > ee->prop.max.h) h = ee->prop.max.h;
234
235         evas_output_framespace_get(ee->evas, NULL, NULL, &fw, &fh);
236         w += fw;
237         h += fh;
238      }
239
240    if ((ee->w != w) || (ee->h != h))
241      {
242         ee->w = w;
243         ee->h = h;
244
245         if ((ee->rotation == 90) || (ee->rotation == 270))
246           {
247              evas_output_size_set(ee->evas, h, w);
248              evas_output_viewport_set(ee->evas, 0, 0, h, w);
249           }
250         else
251           {
252              evas_output_size_set(ee->evas, w, h);
253              evas_output_viewport_set(ee->evas, 0, 0, w, h);
254           }
255
256         if (ee->prop.avoid_damage)
257           {
258              int pdam = 0;
259
260              pdam = ecore_evas_avoid_damage_get(ee);
261              ecore_evas_avoid_damage_set(ee, 0);
262              ecore_evas_avoid_damage_set(ee, pdam);
263           }
264
265         if (ee->engine.wl.frame)
266           evas_object_resize(ee->engine.wl.frame, w, h);
267      }
268 }
269
270 void
271 _ecore_evas_wl_common_callback_resize_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
272 {
273    if (!ee) return;
274    ee->func.fn_resize = func;
275 }
276
277 void
278 _ecore_evas_wl_common_callback_move_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
279 {
280    if (!ee) return;
281    ee->func.fn_move = func;
282 }
283
284 void
285 _ecore_evas_wl_common_callback_delete_request_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
286 {
287    if (!ee) return;
288    ee->func.fn_delete_request = func;
289 }
290
291 void
292 _ecore_evas_wl_common_callback_focus_in_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
293 {
294    if (!ee) return;
295    ee->func.fn_focus_in = func;
296 }
297
298 void
299 _ecore_evas_wl_common_callback_focus_out_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
300 {
301    if (!ee) return;
302    ee->func.fn_focus_out = func;
303 }
304
305 void
306 _ecore_evas_wl_common_callback_mouse_in_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
307 {
308    if (!ee) return;
309    ee->func.fn_mouse_in = func;
310 }
311
312 void
313 _ecore_evas_wl_common_callback_mouse_out_set(Ecore_Evas *ee, void (*func)(Ecore_Evas *ee))
314 {
315    if (!ee) return;
316    ee->func.fn_mouse_out = func;
317 }
318
319 void
320 _ecore_evas_wl_common_move(Ecore_Evas *ee, int x, int y)
321 {
322    if (!ee) return;
323
324    ee->req.x = x;
325    ee->req.y = y;
326
327    if ((ee->x != x) || (ee->y != y))
328      {
329         ee->x = x;
330         ee->y = y;
331         if (ee->engine.wl.win)
332           ecore_wl_window_update_location(ee->engine.wl.win, x, y);
333         if (ee->func.fn_move) ee->func.fn_move(ee);
334      }
335 }
336
337 static void
338 _ecore_evas_wl_common_smart_add(Evas_Object *obj)
339 {
340    EE_Wl_Smart_Data *sd;
341    Evas *evas;
342
343    if (!(sd = calloc(1, sizeof(EE_Wl_Smart_Data)))) return;
344
345    evas = evas_object_evas_get(obj);
346
347    sd->x = 0;
348    sd->y = 0;
349    sd->w = 1;
350    sd->h = 1;
351
352    sd->frame = evas_object_rectangle_add(evas);
353    evas_object_is_frame_object_set(sd->frame, EINA_TRUE);
354    evas_object_color_set(sd->frame, 249, 249, 249, 255);
355    evas_object_smart_member_add(sd->frame, obj);
356
357    sd->text = evas_object_text_add(evas);
358    evas_object_color_set(sd->text, 0, 0, 0, 255);
359    evas_object_text_style_set(sd->text, EVAS_TEXT_STYLE_PLAIN);
360    evas_object_text_font_set(sd->text, "Sans", 10);
361    evas_object_text_text_set(sd->text, "Smart Test");
362
363    evas_object_smart_data_set(obj, sd);
364 }
365
366 static void
367 _ecore_evas_wl_common_smart_del(Evas_Object *obj)
368 {
369    EE_Wl_Smart_Data *sd;
370
371    if (!(sd = evas_object_smart_data_get(obj))) return;
372    evas_object_del(sd->text);
373    evas_object_del(sd->frame);
374    free(sd);
375 }
376
377 static void
378 _ecore_evas_wl_common_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
379 {
380    EE_Wl_Smart_Data *sd;
381
382    if (!(sd = evas_object_smart_data_get(obj))) return;
383    if ((sd->w == w) && (sd->h == h)) return;
384    sd->w = w;
385    sd->h = h;
386    evas_object_resize(sd->frame, w, h);
387 }
388
389 static void
390 _ecore_evas_wl_common_smart_show(Evas_Object *obj)
391 {
392    EE_Wl_Smart_Data *sd;
393
394    if (!(sd = evas_object_smart_data_get(obj))) return;
395    evas_object_show(sd->frame);
396    evas_object_show(sd->text);
397 }
398
399 static void
400 _ecore_evas_wl_common_smart_hide(Evas_Object *obj)
401 {
402    EE_Wl_Smart_Data *sd;
403
404    if (!(sd = evas_object_smart_data_get(obj))) return;
405    evas_object_hide(sd->text);
406    evas_object_hide(sd->frame);
407 }
408
409 static void
410 _ecore_evas_wl_common_smart_init(void)
411 {
412    if (_ecore_evas_wl_common_smart) return;
413      {
414         static const Evas_Smart_Class sc =
415           {
416              "ecore_evas_wl_frame", EVAS_SMART_CLASS_VERSION,
417              _ecore_evas_wl_common_smart_add,
418              _ecore_evas_wl_common_smart_del,
419              NULL,
420              _ecore_evas_wl_common_smart_resize,
421              _ecore_evas_wl_common_smart_show,
422              _ecore_evas_wl_common_smart_hide,
423              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
424           };
425         _ecore_evas_wl_common_smart = evas_smart_class_new(&sc);
426      }
427 }
428
429 Evas_Object *
430 _ecore_evas_wl_common_frame_add(Evas *evas)
431 {
432    _ecore_evas_wl_common_smart_init();
433    return evas_object_smart_add(evas, _ecore_evas_wl_common_smart);
434 }
435
436 void
437 _ecore_evas_wl_common_raise(Ecore_Evas *ee)
438 {
439    if ((!ee) || (!ee->visible)) return;
440    ecore_wl_window_raise(ee->engine.wl.win);
441 }
442
443 void
444 _ecore_evas_wl_common_title_set(Ecore_Evas *ee, const char *title)
445 {
446    if (!ee) return;
447    if (ee->prop.title) free(ee->prop.title);
448    ee->prop.title = NULL;
449    if (title) ee->prop.title = strdup(title);
450    if ((ee->prop.draw_frame) && (ee->engine.wl.frame))
451      {
452         EE_Wl_Smart_Data *sd;
453
454         if (!(sd = evas_object_smart_data_get(ee->engine.wl.frame))) return;
455         evas_object_text_text_set(sd->text, ee->prop.title);
456      }
457
458    if ((ee->prop.title) && (ee->engine.wl.win->shell_surface))
459      wl_shell_surface_set_title(ee->engine.wl.win->shell_surface,
460                                 ee->prop.title);
461 }
462
463 void
464 _ecore_evas_wl_common_name_class_set(Ecore_Evas *ee, const char *n, const char *c)
465 {
466    if (!ee) return;
467    if (ee->prop.name) free(ee->prop.name);
468    if (ee->prop.clas) free(ee->prop.clas);
469    ee->prop.name = NULL;
470    ee->prop.clas = NULL;
471    if (n) ee->prop.name = strdup(n);
472    if (c) ee->prop.clas = strdup(c);
473
474    if ((ee->prop.clas) && (ee->engine.wl.win->shell_surface))
475      wl_shell_surface_set_class(ee->engine.wl.win->shell_surface,
476                                 ee->prop.clas);
477 }
478
479 void
480 _ecore_evas_wl_common_size_min_set(Ecore_Evas *ee, int w, int h)
481 {
482    if (!ee) return;
483    if (w < 0) w = 0;
484    if (h < 0) h = 0;
485    if ((ee->prop.min.w == w) && (ee->prop.min.h == h)) return;
486    ee->prop.min.w = w;
487    ee->prop.min.h = h;
488 }
489
490 void
491 _ecore_evas_wl_common_size_max_set(Ecore_Evas *ee, int w, int h)
492 {
493    if (!ee) return;
494    if (w < 0) w = 0;
495    if (h < 0) h = 0;
496    if ((ee->prop.max.w == w) && (ee->prop.max.h == h)) return;
497    ee->prop.max.w = w;
498    ee->prop.max.h = h;
499 }
500
501 void
502 _ecore_evas_wl_common_size_base_set(Ecore_Evas *ee, int w, int h)
503 {
504    if (!ee) return;
505    if (w < 0) w = 0;
506    if (h < 0) h = 0;
507    if ((ee->prop.base.w == w) && (ee->prop.base.h == h)) return;
508    ee->prop.base.w = w;
509    ee->prop.base.h = h;
510 }
511
512 void
513 _ecore_evas_wl_common_size_step_set(Ecore_Evas *ee, int w, int h)
514 {
515    if (!ee) return;
516    if (w < 0) w = 0;
517    if (h < 0) h = 0;
518    if ((ee->prop.step.w == w) && (ee->prop.step.h == h)) return;
519    ee->prop.step.w = w;
520    ee->prop.step.h = h;
521 }
522
523 void
524 _ecore_evas_wl_common_layer_set(Ecore_Evas *ee, int layer)
525 {
526    if (!ee) return;
527    if (ee->prop.layer == layer) return;
528    if (layer < 1) layer = 1;
529    else if (layer > 255) layer = 255;
530    ee->prop.layer = layer;
531 }
532
533 void
534 _ecore_evas_wl_common_iconified_set(Ecore_Evas *ee, int iconify)
535 {
536    if (!ee) return;
537    if (ee->prop.iconified == iconify) return;
538    ee->prop.iconified = iconify;
539    /* FIXME: Implement this in Wayland someshow */
540 }
541
542 void
543 _ecore_evas_wl_common_maximized_set(Ecore_Evas *ee, int max)
544 {
545    if (!ee) return;
546    if (ee->prop.maximized == max) return;
547    ee->prop.maximized = max;
548    ecore_wl_window_maximized_set(ee->engine.wl.win, max);
549 }
550
551 void
552 _ecore_evas_wl_common_fullscreen_set(Ecore_Evas *ee, int full)
553 {
554    if (!ee) return;
555    if (ee->prop.fullscreen == full) return;
556    ee->prop.fullscreen = full;
557    ecore_wl_window_fullscreen_set(ee->engine.wl.win, full);
558 }
559
560 void
561 _ecore_evas_wl_common_ignore_events_set(Ecore_Evas *ee, int ignore)
562 {
563    if (!ee) return;
564    ee->ignore_events = ignore;
565    /* NB: Hmmm, may need to pass this to ecore_wl_window in the future */
566 }
567
568 int
569 _ecore_evas_wl_common_pre_render(Ecore_Evas *ee)
570 {
571    int rend = 0;
572    Eina_List *ll = NULL;
573    Ecore_Evas *ee2 = NULL;
574
575    if (ee->func.fn_pre_render) ee->func.fn_pre_render(ee);
576
577    EINA_LIST_FOREACH(ee->sub_ecore_evas, ll, ee2)
578      {
579         if (ee2->func.fn_pre_render) ee2->func.fn_pre_render(ee2);
580         if (ee2->engine.func->fn_render)
581           rend |= ee2->engine.func->fn_render(ee2);
582         if (ee2->func.fn_post_render) ee2->func.fn_post_render(ee2);
583      }
584
585    return rend;
586 }
587
588 int
589 _ecore_evas_wl_common_render_updates(Ecore_Evas *ee)
590 {
591    int rend = 0;
592    Eina_List *updates = NULL;
593
594    if ((updates = evas_render_updates(ee->evas)))
595      {
596         Eina_List *l = NULL;
597         Eina_Rectangle *r;
598
599         EINA_LIST_FOREACH(updates, l, r)
600           ecore_wl_window_damage(ee->engine.wl.win,
601                                  r->x, r->y, r->w, r->h);
602
603         ecore_wl_flush();
604
605         evas_render_updates_free(updates);
606         rend = 1;
607      }
608
609    return rend;
610 }
611
612 void
613 _ecore_evas_wl_common_post_render(Ecore_Evas *ee)
614 {
615    _ecore_evas_idle_timeout_update(ee);
616    if (ee->func.fn_post_render) ee->func.fn_post_render(ee);
617 }
618
619 int
620 _ecore_evas_wl_common_render(Ecore_Evas *ee)
621 {
622    int rend = 0;
623
624    if (!ee) return 0;
625    if (!ee->visible)
626      {
627         evas_norender(ee->evas);
628         return 0;
629      }
630
631    rend = _ecore_evas_wl_common_pre_render(ee);
632    rend |= _ecore_evas_wl_common_render_updates(ee);
633    _ecore_evas_wl_common_post_render(ee);
634
635    return rend;
636 }
637
638 void
639 _ecore_evas_wl_common_screen_geometry_get(const Ecore_Evas *ee __UNUSED__, int *x, int *y, int *w, int *h)
640 {
641    if (x) *x = 0;
642    if (y) *y = 0;
643    ecore_wl_screen_size_get(w, h);
644 }
645
646 void
647 _ecore_evas_wl_common_screen_dpi_get(const Ecore_Evas *ee __UNUSED__, int *xdpi, int *ydpi)
648 {
649    int dpi = 0;
650
651    if (xdpi) *xdpi = 0;
652    if (ydpi) *ydpi = 0;
653    /* FIXME: Ideally this needs to get the DPI from a specific screen */
654    dpi = ecore_wl_dpi_get();
655    if (xdpi) *xdpi = dpi;
656    if (ydpi) *ydpi = dpi;
657 }