Add trace tags and logs for Input
[platform/upstream/enlightenment.git] / src / bin / e_comp_wl.c
1 #include "e.h"
2 #include "e_foreign.h"
3 #include "e_wtz_shell.h"
4 #include "e_comp_wl_private.h"
5 #include <tizen-extension-server-protocol.h>
6
7 #include <relative-pointer-unstable-v1-server-protocol.h>
8 #include <pointer-constraints-unstable-v1-server-protocol.h>
9
10 #include <wayland-tbm-server.h>
11 #include <glib.h>
12
13 #include <libds/log.h>
14 #include <libds/single_pixel_buffer_v1.h>
15
16 #include <cpu-boosting.h>
17
18 /* handle include for printing uint64_t */
19 #define __STDC_FORMAT_MACROS
20 #include <inttypes.h>
21
22 #define E_COM_WL_PREPARE_GAP_LOG_TIME 2000
23
24 /* Resource Data Mapping: (wl_resource_get_user_data)
25  *
26  * wl_surface == e_pixmap
27  * wl_region == eina_tiler
28  * wl_subsurface == e_client
29  *
30  */
31
32 typedef struct _E_Comp_Data E_Comp_Data;
33
34 struct _E_Comp_Data
35 {
36    E_Comp_Wl_Data base;
37    struct wl_listener client_created;
38 };
39
40 static void _e_comp_wl_move_resize_init(void);
41
42 static Eina_Bool _e_comp_wl_cursor_timer_control(Evas_Callback_Type type, E_Client *ec);
43
44 /* local variables */
45 typedef struct _E_Comp_Wl_Key_Data
46 {
47    uint32_t key;
48    Ecore_Device *dev;
49 } E_Comp_Wl_Key_Data;
50
51 static Eina_List *handlers = NULL;
52 static E_Client *cursor_timer_ec = NULL;
53 static Eina_Bool need_send_released = EINA_FALSE;
54 static Eina_Bool need_send_motion = EINA_TRUE;
55
56 static GMutex flush_mutex;
57
58 static int _e_comp_wl_hooks_delete = 0;
59 static int _e_comp_wl_hooks_walking = 0;
60
61 static int _e_comp_wl_pid_hooks_delete = 0;
62 static int _e_comp_wl_pid_hooks_walking = 0;
63
64 static int _e_comp_wl_intercept_hooks_delete = 0;
65 static int _e_comp_wl_intercept_hooks_walking = 0;
66
67 static Eina_Inlist *_e_comp_wl_hooks[] =
68 {
69    [E_COMP_WL_HOOK_SHELL_SURFACE_READY] = NULL,
70    [E_COMP_WL_HOOK_SUBSURFACE_CREATE] = NULL,
71    [E_COMP_WL_HOOK_SUBSURFACE_COMMIT_TO_CACHE] = NULL,
72    [E_COMP_WL_HOOK_SUBSURFACE_SYNCHRONIZED_COMMIT] = NULL,
73    [E_COMP_WL_HOOK_BUFFER_CHANGE] = NULL,
74    [E_COMP_WL_HOOK_CLIENT_REUSE] = NULL,
75    [E_COMP_WL_HOOK_BUFFER_SIZE_CHANGE] = NULL,
76    [E_COMP_WL_HOOK_DEL] = NULL,
77    [E_COMP_WL_HOOK_CLIENT_ALPHA_CHANGE] = NULL,
78    [E_COMP_WL_HOOK_CLIENT_SURFACE_COMMIT] = NULL,
79    [E_COMP_WL_HOOK_CLIENT_BEFORE_SURFACE_COMMIT] = NULL,
80 };
81
82 static Eina_Inlist *_e_comp_wl_pid_hooks[] =
83 {
84    [E_COMP_WL_PID_HOOK_CONNECTED_CLIENT_CREATE] = NULL,
85 };
86
87 static Eina_Inlist *_e_comp_wl_intercept_hooks[] =
88 {
89    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_IN] = NULL,
90    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_OUT] = NULL,
91    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_MOVE] = NULL,
92    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_WHEEL] = NULL,
93    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_DOWN] = NULL,
94    [E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_UP] = NULL,
95 };
96
97 static Eina_List *hooks = NULL;
98
99 /* local functions */
100 static void
101 _e_comp_wl_hooks_clean(void)
102 {
103    Eina_Inlist *l;
104    E_Comp_Wl_Hook *ch;
105    unsigned int x;
106    for (x = 0; x < E_COMP_WL_HOOK_LAST; x++)
107      EINA_INLIST_FOREACH_SAFE(_e_comp_wl_hooks[x], l, ch)
108        {
109           if (!ch->delete_me) continue;
110           _e_comp_wl_hooks[x] = eina_inlist_remove(_e_comp_wl_hooks[x], EINA_INLIST_GET(ch));
111          free(ch);
112        }
113 }
114
115 static void
116 _e_comp_wl_hook_call(E_Comp_Wl_Hook_Point hookpoint, E_Client *ec)
117 {
118    E_Comp_Wl_Hook *ch;
119
120    e_object_ref(E_OBJECT(ec));
121    _e_comp_wl_hooks_walking++;
122    EINA_INLIST_FOREACH(_e_comp_wl_hooks[hookpoint], ch)
123      {
124         if (ch->delete_me) continue;
125         ch->func(ch->data, ec);
126         if ((hookpoint != E_COMP_WL_HOOK_DEL) &&
127             (e_object_is_del(E_OBJECT(ec))))
128           break;
129      }
130    _e_comp_wl_hooks_walking--;
131    if ((_e_comp_wl_hooks_walking == 0) && (_e_comp_wl_hooks_delete > 0))
132      _e_comp_wl_hooks_clean();
133    e_object_unref(E_OBJECT(ec));
134 }
135
136 static void
137 _e_comp_wl_pid_hooks_clean(void)
138 {
139    Eina_Inlist *l;
140    E_Comp_Wl_Pid_Hook *ch;
141    unsigned int x;
142    for (x = 0; x < E_COMP_WL_PID_HOOK_LAST; x++)
143      EINA_INLIST_FOREACH_SAFE(_e_comp_wl_pid_hooks[x], l, ch)
144        {
145           if (!ch->delete_me) continue;
146           _e_comp_wl_pid_hooks[x] = eina_inlist_remove(_e_comp_wl_pid_hooks[x], EINA_INLIST_GET(ch));
147          free(ch);
148        }
149 }
150
151 static void
152 _e_comp_wl_pid_hook_call(E_Comp_Wl_Pid_Hook_Point hookpoint, pid_t pid)
153 {
154    E_Comp_Wl_Pid_Hook *ch;
155
156    _e_comp_wl_pid_hooks_walking++;
157    EINA_INLIST_FOREACH(_e_comp_wl_pid_hooks[hookpoint], ch)
158      {
159         if (ch->delete_me) continue;
160         ch->func(ch->data, pid);
161      }
162    _e_comp_wl_pid_hooks_walking--;
163    if ((_e_comp_wl_pid_hooks_walking == 0) && (_e_comp_wl_pid_hooks_delete > 0))
164      _e_comp_wl_pid_hooks_clean();
165 }
166
167 static void
168 _e_comp_wl_intercept_hooks_clean(void)
169 {
170    Eina_Inlist *l;
171    E_Comp_Wl_Intercept_Hook *ch;
172    unsigned int x;
173    for (x = 0; x < E_COMP_WL_INTERCEPT_HOOK_LAST; x++)
174      EINA_INLIST_FOREACH_SAFE(_e_comp_wl_intercept_hooks[x], l, ch)
175        {
176           if (!ch->delete_me) continue;
177           _e_comp_wl_intercept_hooks[x] = eina_inlist_remove(_e_comp_wl_intercept_hooks[x], EINA_INLIST_GET(ch));
178          free(ch);
179        }
180 }
181
182 static Eina_Bool
183 _e_comp_wl_intercept_hook_call(E_Comp_Wl_Intercept_Hook_Point hookpoint, E_Client *ec)
184 {
185    E_Comp_Wl_Intercept_Hook *ch;
186    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
187
188    e_object_ref(E_OBJECT(ec));
189    _e_comp_wl_intercept_hooks_walking++;
190    EINA_INLIST_FOREACH(_e_comp_wl_intercept_hooks[hookpoint], ch)
191      {
192         if (ch->delete_me) continue;
193         res = ch->func(ch->data, ec);
194         if (!res) ret = EINA_FALSE;
195         if (e_object_is_del(E_OBJECT(ec)))
196           break;
197      }
198    _e_comp_wl_intercept_hooks_walking--;
199    if ((_e_comp_wl_intercept_hooks_walking == 0) && (_e_comp_wl_intercept_hooks_delete > 0))
200      _e_comp_wl_intercept_hooks_clean();
201    e_object_unref(E_OBJECT(ec));
202
203    return ret;
204 }
205
206
207 static void
208 _e_comp_wl_configure_send(E_Client *ec, Eina_Bool edges, Eina_Bool send_size)
209 {
210    int w, h;
211
212    if (send_size)
213      {
214         if (e_comp_object_frame_exists(ec->frame))
215           w = ec->client.w, h = ec->client.h;
216         else
217           w = ec->w, h = ec->h;
218      }
219    else
220      {
221         // Width and Height are -1 means that we don't consider size value
222         w = h = -1;
223      }
224
225    ec->comp_data->shell.configure_send(ec->comp_data->shell.surface,
226                                        edges * e_comp_wl->resize.edges,
227                                        w, h);
228 }
229
230 static void
231 _e_comp_wl_focus_check(void)
232 {
233    E_Client *ec;
234
235    if (stopping) return;
236    ec = e_client_focused_get();
237    if (!ec)
238      e_grabinput_focus(e_comp->ee_win, E_FOCUS_METHOD_PASSIVE);
239 }
240
241 static Eina_Bool
242 _e_comp_wl_cb_idle_exiter(void *data EINA_UNUSED)
243 {
244    e_comp_wl->idle_exiter_timestamp = ecore_time_get() * 1000;
245
246    TRACE_DS_ASYNC_BEGIN((intptr_t)&e_comp_wl->idle_exiter_timestamp,
247                         IDLE_EXITER~CB_PREPARE);
248
249    return ECORE_CALLBACK_RENEW;
250 }
251
252 static Eina_Bool
253 _e_comp_wl_cb_read(void *data EINA_UNUSED, Ecore_Fd_Handler *hdlr EINA_UNUSED)
254 {
255    /* dispatch pending wayland events */
256    wl_event_loop_dispatch(e_comp_wl->wl.loop, 0);
257
258    return ECORE_CALLBACK_RENEW;
259 }
260
261 void
262 e_comp_wl_display_flush()
263 {
264    if (e_comp_wl && e_comp_wl->wl.disp)
265      {
266         if (e_config->key_input_ttrace_enable)
267           {
268              TRACE_INPUT_BEGIN(wl_display_flush_clients);
269              ELOGF("INPUT", "wl_display_flush_clients|B|", NULL);
270           }
271
272         g_mutex_lock(&flush_mutex);
273         wl_display_flush_clients(e_comp_wl->wl.disp);
274         g_mutex_unlock(&flush_mutex);
275
276         if (e_config->key_input_ttrace_enable)
277           {
278              TRACE_INPUT_END();
279              ELOGF("INPUT", "wl_display_flush_clients|E|", NULL);
280           }
281      }
282 }
283
284 void
285 _e_comp_wl_main_thread_set_boosting()
286 {
287    pid_t main_thread_id = e_input_main_thread_id_get();
288    if (main_thread_id <= 0)
289      {
290         ERR("Failed to get main thread id");
291         return;
292      }
293
294    TRACE_INPUT_BEGIN("_e_comp_wl_main_thread_set_boosting");
295    resource_pid_t resource;
296    resource.pid = 0;
297    resource.tid = &main_thread_id;
298    resource.tid_count = 1;
299
300    int ret = resource_set_cpu_boosting(resource, CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_RESET_ON_FORK, -1);
301    TRACE_INPUT_END();
302
303    if (ret != 0)
304      ERR("set_cpu_boosting failed. error %d", ret);
305 }
306
307 void
308 _e_comp_wl_main_thread_clear_boosting()
309 {
310    pid_t main_thread_id = e_input_main_thread_id_get();
311    if (main_thread_id <= 0)
312      {
313         ERR("Failed to get main thread id");
314         return;
315      }
316
317    TRACE_INPUT_BEGIN("_e_comp_wl_main_thread_clear_boosting");
318    resource_pid_t resource;
319    resource.pid = 0;
320    resource.tid = &main_thread_id;
321    resource.tid_count = 1;
322
323    int ret = resource_clear_cpu_boosting(resource);
324    TRACE_INPUT_END();
325
326    if (ret != 0)
327      ERR("clear_cpu_boosting failed. error %d", ret);
328
329 }
330
331 void
332 e_comp_wl_focused_client_flush()
333 {
334    E_Client *focused_ec;
335    if (!(e_comp_wl && e_comp_wl->wl.disp)) return;
336
337    if (!g_mutex_trylock(&flush_mutex))
338      {
339         TRACE_INPUT_BEGIN("flush_mutex lock");
340         _e_comp_wl_main_thread_set_boosting();
341         g_mutex_lock(&flush_mutex);
342         _e_comp_wl_main_thread_clear_boosting();
343         TRACE_INPUT_END();
344      }
345    focused_ec = e_client_focused_get();
346
347    if (focused_ec)
348      {
349         struct wl_client *wc;
350         struct wl_resource *surface = e_comp_wl_client_surface_get(focused_ec);
351         if (!surface)
352           {
353             g_mutex_unlock(&flush_mutex);
354             return;
355           }
356
357         wc = wl_resource_get_client(surface);
358         wl_client_flush(wc);
359      }
360
361    g_mutex_unlock(&flush_mutex);
362 }
363
364 static void
365 _e_comp_wl_cb_prepare(void *data EINA_UNUSED, Ecore_Fd_Handler *hdlr EINA_UNUSED)
366 {
367    double gap_time;
368
369    if (e_comp_wl->idle_exiter_timestamp > 0.0)
370      {
371         gap_time = (ecore_time_get() * 1000) - e_comp_wl->idle_exiter_timestamp;
372         if (gap_time > E_COM_WL_PREPARE_GAP_LOG_TIME)
373           {
374              ELOGF("E_COMP", "prepare gap time: %lfms",
375                    NULL, gap_time);
376           }
377      }
378
379    e_comp_wl->idle_exiter_timestamp = 0.0;
380
381    wl_event_loop_dispatch_idle(e_comp_wl->wl.loop);
382
383    /* flush pending client events */
384    e_comp_wl_display_flush();
385
386    TRACE_DS_ASYNC_END((intptr_t)&e_comp_wl->idle_exiter_timestamp,
387                       IDLE_EXITER~CB_PREPARE);
388 }
389
390 E_API enum wl_output_transform
391 e_comp_wl_output_buffer_transform_get(E_Client *ec)
392 {
393    E_Comp_Wl_Buffer_Viewport *vp;
394    E_Comp_Wl_Buffer *buffer;
395    enum wl_output_transform transform, rotation;
396
397    if (!ec) return WL_OUTPUT_TRANSFORM_NORMAL;
398    if (e_object_is_del(E_OBJECT(ec))) return WL_OUTPUT_TRANSFORM_NORMAL;
399    if (!ec->comp_data) return WL_OUTPUT_TRANSFORM_NORMAL;
400
401    vp = &ec->comp_data->scaler.buffer_viewport;
402    if (e_comp_wl_subsurface_check(ec))
403      return vp->buffer.transform;
404
405    buffer = ec->comp_data->buffer_ref.buffer;
406
407    if (!buffer ||
408        (buffer->type != E_COMP_WL_BUFFER_TYPE_NATIVE && buffer->type != E_COMP_WL_BUFFER_TYPE_TBM))
409      return vp->buffer.transform;
410
411    rotation = buffer->transform;
412    if (rotation == 0)
413      return vp->buffer.transform;
414
415    /* ignore the flip value when calculating transform because the screen rotation
416     * functionality doesn't consider the flip output transform currently
417     */
418    transform = (4 + (vp->buffer.transform & 0x3) - rotation) & 0x3;
419
420    DBG("ec(%p) window rotation(%d) buffer_transform(%d) : transform(%d)",
421        ec, rotation, vp->buffer.transform, transform);
422
423    return transform;
424 }
425
426 EINTERN enum wl_output_transform
427 e_comp_wl_output_pending_buffer_transform_get(E_Client *ec)
428 {
429    E_Comp_Wl_Buffer_Viewport *vp;
430    E_Comp_Wl_Buffer *buffer;
431    enum wl_output_transform transform, rotation;
432
433    if (!ec) return WL_OUTPUT_TRANSFORM_NORMAL;
434    if (e_object_is_del(E_OBJECT(ec))) return WL_OUTPUT_TRANSFORM_NORMAL;
435    if (!ec->comp_data) return WL_OUTPUT_TRANSFORM_NORMAL;
436
437    vp = &ec->comp_data->pending.buffer_viewport;
438    if (e_comp_wl_subsurface_check(ec))
439      return vp->buffer.transform;
440
441    buffer = ec->comp_data->pending.buffer;
442
443    if (!buffer ||
444        (buffer->type != E_COMP_WL_BUFFER_TYPE_NATIVE && buffer->type != E_COMP_WL_BUFFER_TYPE_TBM))
445      return vp->buffer.transform;
446
447    rotation = buffer->transform;
448    if (rotation == 0)
449      return vp->buffer.transform;
450
451    /* ignore the flip value when calculating transform because the screen rotation
452     * functionality doesn't consider the flip output transform currently
453     */
454    transform = (4 + (vp->buffer.transform & 0x3) - rotation) & 0x3;
455
456    DBG("ec(%p) window rotation(%d) buffer_transform(%d) : transform(%d)",
457        ec, rotation, vp->buffer.transform, transform);
458
459    return transform;
460 }
461
462 E_API void
463 e_comp_wl_map_size_cal_from_buffer(E_Client *ec)
464 {
465    E_Comp_Wl_Buffer_Viewport *vp = &ec->comp_data->scaler.buffer_viewport;
466    E_Comp_Wl_Buffer *buffer;
467    int32_t width, height;
468
469    buffer = e_pixmap_resource_get(ec->pixmap);
470    if (!buffer)
471      {
472         ec->comp_data->width_from_buffer = 0;
473         ec->comp_data->height_from_buffer = 0;
474         return;
475      }
476
477    switch (e_comp_wl_output_buffer_transform_get(ec))
478      {
479       case WL_OUTPUT_TRANSFORM_90:
480       case WL_OUTPUT_TRANSFORM_270:
481       case WL_OUTPUT_TRANSFORM_FLIPPED_90:
482       case WL_OUTPUT_TRANSFORM_FLIPPED_270:
483         width = buffer->h / vp->buffer.scale;
484         height = buffer->w / vp->buffer.scale;
485         break;
486       default:
487         width = buffer->w / vp->buffer.scale;
488         height = buffer->h / vp->buffer.scale;
489         break;
490      }
491
492    ec->comp_data->width_from_buffer = width;
493    ec->comp_data->height_from_buffer = height;
494 }
495
496 E_API void
497 e_comp_wl_map_size_cal_from_viewport(E_Client *ec)
498 {
499    E_Comp_Wl_Buffer_Viewport *vp = &ec->comp_data->scaler.buffer_viewport;
500    int32_t width, height;
501
502    width = ec->comp_data->width_from_buffer;
503    height = ec->comp_data->height_from_buffer;
504
505    if (width == 0 && height == 0) return;
506
507    if (width != 0 && vp->surface.width != -1)
508      {
509         ec->comp_data->width_from_viewport = vp->surface.width;
510         ec->comp_data->height_from_viewport = vp->surface.height;
511         return;
512      }
513
514    if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1))
515      {
516         int32_t w = wl_fixed_to_int(wl_fixed_from_int(1) - 1 + vp->buffer.src_width);
517         int32_t h = wl_fixed_to_int(wl_fixed_from_int(1) - 1 + vp->buffer.src_height);
518         ec->comp_data->width_from_viewport = w ?: 1;
519         ec->comp_data->height_from_viewport = h ?: 1;
520         return;
521      }
522
523    ec->comp_data->width_from_viewport = width;
524    ec->comp_data->height_from_viewport = height;
525 }
526
527 E_API E_Client*
528 e_comp_wl_topmost_parent_get(E_Client *ec)
529 {
530    E_Client *parent;
531
532    if (!e_comp_wl_subsurface_check(ec))
533      return ec;
534
535    parent = e_comp_wl_subsurface_parent_get(ec);
536    while (parent)
537      {
538         if (!e_comp_wl_subsurface_check(parent))
539           return parent;
540
541         parent = e_comp_wl_subsurface_parent_get(parent);
542      }
543
544    return ec;
545 }
546
547 E_API void
548 e_comp_wl_map_apply(E_Client *ec)
549 {
550    E_Comp_Wl_Buffer_Viewport *vp;
551    E_Comp_Wl_Subsurf_Data *sdata;
552    E_Comp_Wl_Client_Data *cdata;
553    int x1, y1, x2, y2, x, y;
554    int dx = 0, dy = 0;
555    Eina_Bool zoom_animating = EINA_FALSE;
556
557    if (!ec || !ec->comp_data || e_object_is_del(E_OBJECT(ec))) return;
558
559    e_comp_object_map_update(ec->frame);
560
561    cdata = ec->comp_data;
562    vp = &cdata->scaler.buffer_viewport;
563    if ((vp->buffer.src_width == wl_fixed_from_int(-1)) &&
564        (!cdata->viewport_transform))
565      return;
566
567    if (e_comp_wl_subsurface_check(ec))
568      {
569         e_comp_wl_subsurface_global_coord_get(ec, &dx, &dy);
570
571         sdata = ec->comp_data->sub.data;
572         if (sdata->remote_surface.offscreen_parent)
573           {
574              E_Client *offscreen_parent = sdata->remote_surface.offscreen_parent;
575              Eina_Rectangle *rect;
576              Eina_List *l;
577
578              EINA_LIST_FOREACH(offscreen_parent->comp_data->remote_surface.regions, l, rect)
579                {
580                   /* TODO: If there are one more regions, it means that provider's offscreen
581                    * is displayed by one more remote_surfaces. Have to consider it later. At
582                    * this time, just consider only one remote_surface.
583                    */
584                   dx += rect->x;
585                   dy += rect->y;
586                   break;
587                }
588           }
589      }
590    else
591      {
592         dx = ec->x;
593         dy = ec->y;
594      }
595
596    evas_object_geometry_get(ec->frame, &x, &y, NULL, NULL);
597    if (x != dx || y != dy)
598      evas_object_move(ec->frame, dx, dy);
599
600    if (!cdata->viewport_transform)
601      {
602         cdata->viewport_transform = e_util_transform_new();
603         e_util_transform_role_set(cdata->viewport_transform, "viewport_transform");
604         e_client_transform_core_add(ec, cdata->viewport_transform);
605      }
606
607    e_util_transform_viewport_set(cdata->viewport_transform, dx, dy,
608                                  ec->comp_data->width_from_viewport,
609                                  ec->comp_data->height_from_viewport);
610
611    if (vp->buffer.src_width == wl_fixed_from_int(-1))
612      {
613         x1 = 0;
614         y1 = 0;
615         x2 = cdata->width_from_buffer;
616         y2 = cdata->height_from_buffer;
617      }
618    else
619      {
620         x1 = wl_fixed_to_int(vp->buffer.src_x);
621         y1 = wl_fixed_to_int(vp->buffer.src_y);
622         x2 = wl_fixed_to_int(vp->buffer.src_x + vp->buffer.src_width);
623         y2 = wl_fixed_to_int(vp->buffer.src_y + vp->buffer.src_height);
624      }
625
626    e_util_transform_texcoord_set(cdata->viewport_transform, 0, x1, y1);
627    e_util_transform_texcoord_set(cdata->viewport_transform, 1, x2, y1);
628    e_util_transform_texcoord_set(cdata->viewport_transform, 2, x2, y2);
629    e_util_transform_texcoord_set(cdata->viewport_transform, 3, x1, y2);
630
631    E_Desk *desk;
632    desk = e_comp_desk_find_by_ec(ec);
633    if (desk)
634      zoom_animating = e_desk_zoom_is_animating(desk);
635
636    ELOGF("TRANSFORM", "viewport map: point(%d,%d %dx%d) uv(%d,%d %d,%d %d,%d %d,%d), zoom_animating: %d",
637          ec, ec->x, ec->y, ec->comp_data->width_from_viewport,
638          ec->comp_data->height_from_viewport, x1, y1, x2, y1, x2, y2, x1, y2, zoom_animating);
639
640    /* workaround:: when the desk zoom is doing their animation,
641       the transform core update can override animation's zoom boundary.
642       so, don't doing transform core update while desk zoom animation */
643    if (!zoom_animating)
644      e_client_transform_core_update(ec);
645
646 }
647
648 EINTERN void
649 e_comp_wl_map_inv_coord_get(E_Client *ec, int x, int y, int *mx, int *my)
650 {
651    E_Map *map;
652    E_Map *map2;
653    int map_c = 0, map_x = 0, map_y = 0;
654    double inv_x = 0.0, inv_y = 0.0;
655
656    EINA_SAFETY_ON_NULL_RETURN(ec);
657
658    map = e_client_map_get(ec);
659    map_c = e_map_count_get(map);
660
661    if (!map || map_c <= 0)
662      {
663         *mx = x;
664         *my = y;
665         e_map_free(map);
666         return;
667      }
668
669    map2 = e_map_new();
670
671    e_map_point_coord_set(map2, 0, ec->x, ec->y, 0);
672    e_map_point_coord_set(map2, 1, ec->x + ec->w, ec->y, 0);
673    e_map_point_coord_set(map2, 2, ec->x + ec->w, ec->y + ec->h, 0);
674    e_map_point_coord_set(map2, 3, ec->x, ec->y + ec->h, 0);
675
676    for (int i = 0; i < map_c; i++)
677      {
678         e_map_point_coord_get(map, i, &map_x, &map_y, NULL);
679         e_map_point_image_uv_set(map2, i, map_x, map_y);
680      }
681    e_map_coords_get(map2, (double)x, (double)y, &inv_x, &inv_y, 0);
682
683    *mx = lround(inv_x);
684    *my = lround(inv_y);
685
686    e_map_free(map);
687    e_map_free(map2);
688 }
689
690 static void
691 _e_comp_wl_evas_cb_show(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
692 {
693    E_Client *ec, *tmp;
694    Eina_List *l;
695    E_Client *topmost;
696
697    if (!(ec = data)) return;
698    if (e_object_is_del(data)) return;
699
700    if (!ec->override) e_hints_window_visible_set(ec);
701
702    if ((!ec->override) && (!ec->re_manage) && (!ec->comp_data->reparented) &&
703        (!ec->comp_data->need_reparent))
704      {
705         ec->comp_data->need_reparent = EINA_TRUE;
706         ec->visible = EINA_TRUE;
707      }
708    if (!e_client_util_ignored_get(ec))
709      {
710         ec->take_focus = !starting;
711         EC_CHANGED(ec);
712      }
713
714    if (!ec->comp_data->need_reparent)
715      {
716         if ((ec->hidden) || (ec->iconic))
717           {
718              evas_object_hide(ec->frame);
719 //             e_comp_object_damage(ec->frame, 0, 0, ec->w, ec->h);
720           }
721         else
722           evas_object_show(ec->frame);
723      }
724
725    EINA_LIST_FOREACH(ec->e.state.video_child, l, tmp)
726      evas_object_show(tmp->frame);
727
728    topmost = e_comp_wl_topmost_parent_get(ec);
729    if (topmost == ec && (ec->comp_data->sub.list || ec->comp_data->sub.below_list))
730      e_comp_wl_subsurface_show(ec);
731
732    if (ec->comp_data->sub.below_obj)
733      evas_object_show(ec->comp_data->sub.below_obj);
734 }
735
736 static void
737 _e_comp_wl_evas_cb_hide(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
738 {
739    E_Client *ec, *tmp;
740    Eina_List *l;
741    E_Client *topmost;
742
743    if (!(ec = data)) return;
744    if (e_object_is_del(E_OBJECT(ec))) return;
745
746    /* Uncommonly some clients's final buffer can be skipped if the client
747     * requests unmap of its surface right after wl_surface@commit.
748     * So if this client evas object is hidden state and client is already
749     * unmmapped, we can consider to clear pixmap image here mandatorily.
750     */
751    if (!ec->comp_data->mapped)
752      e_pixmap_image_clear(ec->pixmap, 1);
753
754    EINA_LIST_FOREACH(ec->e.state.video_child, l, tmp)
755      evas_object_hide(tmp->frame);
756
757    topmost = e_comp_wl_topmost_parent_get(ec);
758    if (topmost == ec && (ec->comp_data->sub.list || ec->comp_data->sub.below_list))
759      e_comp_wl_subsurface_hide(ec);
760
761    if (ec->comp_data->sub.below_obj)
762      evas_object_hide(ec->comp_data->sub.below_obj);
763
764    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_unmap_signal, ec);
765 }
766
767 static void
768 _e_comp_wl_evas_cb_move(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
769 {
770    E_Client *ec;
771    E_Client *subc;
772    Eina_List *l;
773    int x = 0 , y = 0;
774
775    if (!(ec = data)) return;
776    if (e_object_is_del(E_OBJECT(ec))) return;
777
778    EINA_LIST_FOREACH(ec->comp_data->sub.list, l, subc)
779      {
780         if (!e_comp_wl_subsurface_check(subc)) continue;
781         e_comp_wl_subsurface_global_coord_get(subc, &x, &y);
782         evas_object_move(subc->frame, x, y);
783
784         if (subc->comp_data->scaler.viewport)
785           {
786              E_Comp_Wl_Client_Data *cdata = subc->comp_data;
787              if (cdata->viewport_transform)
788                e_comp_wl_map_apply(subc);
789           }
790      }
791
792    EINA_LIST_FOREACH(ec->comp_data->sub.below_list, l, subc)
793      {
794         if (!e_comp_wl_subsurface_check(subc)) continue;
795         e_comp_wl_subsurface_global_coord_get(subc, &x, &y);
796         evas_object_move(subc->frame, x, y);
797
798         if (subc->comp_data->scaler.viewport)
799           {
800              E_Comp_Wl_Client_Data *cdata = subc->comp_data;
801              if (cdata->viewport_transform)
802                e_comp_wl_map_apply(subc);
803           }
804      }
805
806    if (ec->comp_data->sub.below_obj)
807      {
808         evas_object_move(ec->comp_data->sub.below_obj, ec->x, ec->y);
809         e_comp_wl_subsurface_bg_rectangle_map_apply(ec);
810      }
811 }
812
813 static void
814 _e_comp_wl_send_touch_cancel(E_Client *ec)
815 {
816    Eina_List *l;
817    struct wl_resource *res;
818    struct wl_client *wc;
819    E_Comp_Config *comp_conf = NULL;
820
821    if (!ec) return;
822    if (e_object_is_del(E_OBJECT(ec))) return;
823    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
824
825    if (!surface) return;
826    if ((ec->ignored) && (!ec->remote_surface.provider)) return;
827
828    wc = wl_resource_get_client(surface);
829
830    comp_conf = e_comp_config_get();
831
832    EINA_LIST_FOREACH(e_comp->wl_comp_data->touch.resources, l, res)
833      {
834         if (wl_resource_get_client(res) != wc) continue;
835         if (!e_comp_wl_input_touch_check(res)) continue;
836
837         if (comp_conf && comp_conf->input_log_enable)
838            ELOGF("Touch", "Cancel name:%20s", ec, e_client_util_name_get(ec));
839
840         wl_touch_send_cancel(res);
841      }
842 }
843
844 static void
845 _e_comp_wl_touch_cancel(void)
846 {
847    E_Client *ec;
848
849    ec = e_comp_wl->ptr.ec ? e_comp_wl->ptr.ec : e_comp_wl->touch.faked_ec;
850    if (!ec) return;
851    if (!e_comp_wl->touch.pressed) return;
852
853    _e_comp_wl_send_touch_cancel(ec);
854 }
855
856 static E_Devicemgr_Input_Device *
857 _e_comp_wl_device_last_device_get(Ecore_Device_Class dev_class)
858 {
859    E_Devicemgr_Input_Device *last_kbd = NULL;
860    switch (dev_class)
861      {
862       case ECORE_DEVICE_CLASS_MOUSE:
863          return e_devicemgr->last_device_ptr;
864       case ECORE_DEVICE_CLASS_KEYBOARD:
865          last_kbd = atomic_load(&e_devicemgr->last_device_kbd);
866          return last_kbd;
867       case ECORE_DEVICE_CLASS_TOUCH:
868          return e_devicemgr->last_device_touch;
869       default:
870          return NULL;;
871      }
872    return NULL;
873 }
874
875 static void
876 _e_comp_wl_device_last_device_set(Ecore_Device_Class dev_class, E_Devicemgr_Input_Device *device)
877 {
878    switch (dev_class)
879      {
880       case ECORE_DEVICE_CLASS_MOUSE:
881          e_devicemgr->last_device_ptr = device;
882          break;
883       case ECORE_DEVICE_CLASS_KEYBOARD:
884          atomic_store(&e_devicemgr->last_device_kbd, device);
885          break;
886       case ECORE_DEVICE_CLASS_TOUCH:
887          e_devicemgr->last_device_touch = device;
888          break;
889       default:
890          break;
891      }
892 }
893
894 static E_Devicemgr_Input_Device *
895 _e_comp_wl_device_client_last_device_get(E_Client *ec, Ecore_Device_Class dev_class)
896 {
897    E_Devicemgr_Input_Device *last_kbd = NULL;
898
899    switch (dev_class)
900      {
901       case ECORE_DEVICE_CLASS_MOUSE:
902          return ec->comp_data->last_device_ptr;
903       case ECORE_DEVICE_CLASS_KEYBOARD:
904          last_kbd = atomic_load(&ec->comp_data->last_device_kbd);
905          return last_kbd;
906       case ECORE_DEVICE_CLASS_TOUCH:
907          return ec->comp_data->last_device_touch;
908       default:
909          return NULL;;
910      }
911    return NULL;
912 }
913
914 static void
915 _e_comp_wl_device_client_last_device_set(E_Client *ec, Ecore_Device_Class dev_class, E_Devicemgr_Input_Device *device)
916 {
917    switch (dev_class)
918      {
919       case ECORE_DEVICE_CLASS_MOUSE:
920          ec->comp_data->last_device_ptr = device;
921          break;
922       case ECORE_DEVICE_CLASS_KEYBOARD:
923          atomic_store(&ec->comp_data->last_device_kbd, device);
924          break;
925       case ECORE_DEVICE_CLASS_TOUCH:
926          ec->comp_data->last_device_touch = device;
927          break;
928       default:
929          break;
930      }
931 }
932
933 static void
934 _e_comp_wl_device_send_event_device(E_Client *ec, Evas_Device *dev, uint32_t timestamp)
935 {
936    E_Devicemgr_Input_Device *last_device, *ec_last_device, *input_dev;
937    struct wl_resource *dev_res;
938    const char *dev_name;
939    Ecore_Device_Class dev_class;
940    struct wl_client *wc;
941    uint32_t serial;
942    Eina_List *l, *ll;
943
944    EINA_SAFETY_ON_NULL_RETURN(dev);
945
946    if (!ec) return;
947    if (ec->cur_mouse_action || e_comp_wl->drag)
948      return;
949    if (e_object_is_del(E_OBJECT(ec))) return;
950    if ((ec->ignored) && (!ec->remote_surface.provider)) return;
951    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
952    if (!surface) return;
953
954    dev_class = (Ecore_Device_Class)evas_device_class_get(dev);
955    dev_name = evas_device_description_get(dev);
956    last_device = _e_comp_wl_device_last_device_get(dev_class);
957    ec_last_device = _e_comp_wl_device_client_last_device_get(ec, dev_class);
958
959    serial = wl_display_next_serial(e_comp_wl->wl.disp);
960    wc = wl_resource_get_client(surface);
961    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
962    EINA_LIST_FOREACH(e_devicemgr->device_list, l, input_dev)
963      {
964         if (!eina_streq(input_dev->identifier, dev_name) || (input_dev->clas != dev_class)) continue;
965         if ((!last_device) || (last_device != input_dev) || (!ec_last_device) || (ec_last_device != input_dev))
966           {
967              _e_comp_wl_device_last_device_set(dev_class, input_dev);
968              _e_comp_wl_device_client_last_device_set(ec, dev_class, input_dev);
969
970              EINA_LIST_FOREACH(input_dev->resources, ll, dev_res)
971                {
972                   if (wl_resource_get_client(dev_res) != wc) continue;
973                   tizen_input_device_send_event_device(dev_res, serial, input_dev->identifier, timestamp);
974                }
975           }
976      }
977    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
978 }
979
980 static void
981 _e_comp_wl_device_send_last_event_device(E_Client *ec, Ecore_Device_Class dev_class, uint32_t timestamp)
982 {
983    E_Devicemgr_Input_Device *last_device;
984    struct wl_resource *dev_res;
985    struct wl_client *wc;
986    uint32_t serial;
987    Eina_List *l;
988
989    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
990    if (!surface) return;
991
992    last_device = _e_comp_wl_device_last_device_get(dev_class);
993    if (!last_device) return;
994
995    _e_comp_wl_device_client_last_device_set(ec, dev_class, last_device);
996
997    serial = wl_display_next_serial(e_comp_wl->wl.disp);
998    wc = wl_resource_get_client(surface);
999    EINA_LIST_FOREACH(last_device->resources, l, dev_res)
1000      {
1001         if (wl_resource_get_client(dev_res) != wc) continue;
1002         tizen_input_device_send_event_device(dev_res, serial, last_device->identifier, timestamp);
1003      }
1004 }
1005
1006 static void
1007 _e_comp_wl_send_event_device(struct wl_client *wc, uint32_t timestamp, Ecore_Device *dev, uint32_t serial)
1008 {
1009    E_Devicemgr_Input_Device *input_dev;
1010    struct wl_resource *dev_res;
1011    const char *dev_name;
1012    Eina_List *l, *ll;
1013
1014    EINA_SAFETY_ON_NULL_RETURN(dev);
1015
1016    dev_name = ecore_device_identifier_get(dev);
1017
1018    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
1019    EINA_LIST_FOREACH(e_devicemgr->device_list, l, input_dev)
1020      {
1021         if (!eina_streq(input_dev->identifier, dev_name) ||
1022             (input_dev->clas != ecore_device_class_get(dev))) continue;
1023         _e_comp_wl_device_last_device_set(ecore_device_class_get(dev), input_dev);
1024
1025         EINA_LIST_FOREACH(input_dev->resources, ll, dev_res)
1026           {
1027              if (wl_resource_get_client(dev_res) != wc) continue;
1028              tizen_input_device_send_event_device(dev_res, serial, input_dev->identifier, timestamp);
1029           }
1030      }
1031    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
1032 }
1033
1034 static void
1035 _e_comp_wl_send_event_e_device(struct wl_client *wc, uint32_t timestamp, E_Device *dev, uint32_t serial)
1036 {
1037    E_Devicemgr_Input_Device *input_dev;
1038    struct wl_resource *dev_res;
1039    const char *dev_name;
1040    Eina_List *l, *ll;
1041
1042    EINA_SAFETY_ON_NULL_RETURN(dev);
1043
1044    dev_name = e_device_identifier_get(dev);
1045
1046    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
1047    EINA_LIST_FOREACH(e_devicemgr->device_list, l, input_dev)
1048      {
1049         if (!eina_streq(input_dev->identifier, dev_name) ||
1050             (input_dev->clas != e_device_class_get(dev))) continue;
1051         _e_comp_wl_device_last_device_set(e_device_class_get(dev), input_dev);
1052
1053         EINA_LIST_FOREACH(input_dev->resources, ll, dev_res)
1054           {
1055              if (wl_resource_get_client(dev_res) != wc) continue;
1056              tizen_input_device_send_event_device(dev_res, serial, input_dev->identifier, timestamp);
1057           }
1058      }
1059    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
1060 }
1061
1062 static void
1063 _e_comp_wl_cursor_reload(E_Client *ec)
1064 {
1065    struct wl_resource *res;
1066    struct wl_client *wc;
1067    Eina_List *l;
1068    E_Map *map;
1069    uint32_t serial;
1070    int cx, cy, px, py;
1071
1072    if (e_comp->pointer && e_comp->pointer->o_ptr &&
1073        !evas_object_visible_get(e_comp->pointer->o_ptr))
1074      e_pointer_object_set(e_comp->pointer, NULL, 0, 0);
1075
1076    if (!ec) return;
1077    if (e_object_is_del(E_OBJECT(ec))) return;
1078    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1079    if (!surface) return;
1080    if (ec->pointer_enter_sent) return;
1081
1082    px = wl_fixed_to_int(e_comp_wl->ptr.x);
1083    py = wl_fixed_to_int(e_comp_wl->ptr.y);
1084
1085    if (e_client_transform_core_enable_get(ec))
1086      {
1087         double dx = 0.0, dy = 0.0;
1088         map = e_client_map_get(ec);
1089         e_map_coords_get(map, (double)px, (double)py, &dx, &dy, 0);
1090         px = (int)dx;
1091         py = (int)dy;
1092         e_map_free(map);
1093      }
1094
1095    cx = px - ec->client.x;
1096    cy = py - ec->client.y;
1097
1098    wc = wl_resource_get_client(surface);
1099    serial = wl_display_next_serial(e_comp_wl->wl.disp);
1100    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
1101      {
1102         if (!e_comp_wl_input_pointer_check(res)) continue;
1103         if (wl_resource_get_client(res) != wc) continue;
1104         wl_pointer_send_enter(res, serial, surface,
1105                               wl_fixed_from_int(cx), wl_fixed_from_int(cy));
1106         ec->pointer_enter_sent = EINA_TRUE;
1107      }
1108    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_mousein_signal, ec);
1109 }
1110
1111 static Eina_Bool
1112 _e_comp_wl_cursor_timer(void *data)
1113 {
1114    E_Client *ec = data;
1115
1116    ELOGF("Mouse", "Cursor hide timer expired after %d sec.", ec, e_config->cursor_timer_interval);
1117    if (e_comp_wl->relative_ptr.activated) return ECORE_CALLBACK_CANCEL;
1118
1119    e_comp_wl_cursor_hide(ec);
1120
1121    return ECORE_CALLBACK_CANCEL;
1122 }
1123
1124 static void
1125 _e_comp_wl_device_send_axis(const char *dev_name, Evas_Device_Class dev_class, E_Client *ec, enum tizen_input_device_axis_type axis_type, double value)
1126 {
1127    E_Devicemgr_Input_Device *input_dev;
1128    struct wl_resource *dev_res;
1129    struct wl_client *wc;
1130    Eina_List *l, *ll;
1131    wl_fixed_t f_value;
1132
1133    if (!ec) return;
1134    if (ec->cur_mouse_action) return;
1135    if (e_object_is_del(E_OBJECT(ec))) return;
1136    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1137    if (!surface) return;
1138
1139    f_value = wl_fixed_from_double(value);
1140    wc = wl_resource_get_client(surface);
1141
1142    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
1143    EINA_LIST_FOREACH(e_devicemgr->device_list, l, input_dev)
1144      {
1145         if ((strcmp(input_dev->identifier, dev_name)) || (input_dev->clas != (Ecore_Device_Class)dev_class)) continue;
1146         EINA_LIST_FOREACH(input_dev->resources, ll, dev_res)
1147           {
1148              if (wl_resource_get_client(dev_res) != wc) continue;
1149              tizen_input_device_send_axis(dev_res, axis_type, f_value);
1150           }
1151      }
1152    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
1153 }
1154
1155 static void
1156 _e_comp_wl_device_renew_axis(const char *dev_name, Evas_Device_Class dev_class, E_Client *ec, unsigned int idx, double radius_x, double radius_y, double pressure, double angle)
1157 {
1158    if (idx >= e_input_touch_max_count_get()) return;
1159
1160    _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_RADIUS_X, radius_x);
1161    e_devicemgr->multi[idx].radius_x = radius_x;
1162    _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_RADIUS_Y, radius_y);
1163    e_devicemgr->multi[idx].radius_y = radius_y;
1164    _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_PRESSURE, pressure);
1165    e_devicemgr->multi[idx].pressure = pressure;
1166    _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_ANGLE, angle);
1167    e_devicemgr->multi[idx].angle = angle;
1168 }
1169
1170 static void
1171 _e_comp_wl_device_handle_axes(const char *dev_name, Evas_Device_Class dev_class, E_Client *ec, unsigned int idx, double radius_x, double radius_y, double pressure, double angle)
1172 {
1173    if (idx >= e_input_touch_max_count_get()) return;
1174
1175    if (e_devicemgr->multi[idx].radius_x != radius_x)
1176      {
1177         _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_RADIUS_X, radius_x);
1178         e_devicemgr->multi[idx].radius_x = radius_x;
1179      }
1180    if (e_devicemgr->multi[idx].radius_y != radius_y)
1181      {
1182         _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_RADIUS_Y, radius_y);
1183         e_devicemgr->multi[idx].radius_y = radius_y;
1184      }
1185    if (e_devicemgr->multi[idx].pressure != pressure)
1186      {
1187         _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_PRESSURE, pressure);
1188         e_devicemgr->multi[idx].pressure = pressure;
1189      }
1190    if (e_devicemgr->multi[idx].angle != angle)
1191      {
1192         _e_comp_wl_device_send_axis(dev_name, dev_class, ec, TIZEN_INPUT_DEVICE_AXIS_TYPE_ANGLE, angle);
1193         e_devicemgr->multi[idx].angle = angle;
1194      }
1195 }
1196
1197 static Eina_Bool
1198 _e_comp_wl_cursor_timer_control(Evas_Callback_Type type, E_Client *ec)
1199 {
1200    Eina_Bool ret = EINA_TRUE;
1201    switch (type)
1202      {
1203         case EVAS_CALLBACK_MOUSE_IN:
1204           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_IN, ec);
1205           if (!ret) break;
1206
1207           if (e_comp_wl->ptr.hide_tmr)
1208             {
1209                ecore_timer_del(e_comp_wl->ptr.hide_tmr);
1210                cursor_timer_ec = ec;
1211                e_comp_wl->ptr.hide_tmr = ecore_timer_add(e_config->cursor_timer_interval, _e_comp_wl_cursor_timer, ec);
1212             }
1213           else
1214             {
1215                if (e_pointer_is_hidden(e_comp->pointer))
1216                  ret = EINA_FALSE;
1217             }
1218           break;
1219
1220         case EVAS_CALLBACK_MOUSE_OUT:
1221           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_OUT, ec);
1222           if (!ret) break;
1223
1224           if (!e_comp_wl->ptr.hide_tmr && e_pointer_is_hidden(e_comp->pointer))
1225             ret = EINA_FALSE;
1226           break;
1227
1228         case EVAS_CALLBACK_MOUSE_MOVE:
1229           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_MOVE, ec);
1230           if (!ret) break;
1231
1232           if (e_pointer_is_hidden(e_comp->pointer) || !ec->pointer_enter_sent)
1233             _e_comp_wl_cursor_reload(ec);
1234           break;
1235
1236         case EVAS_CALLBACK_MOUSE_WHEEL:
1237           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_WHEEL, ec);
1238           if (!ret) break;
1239
1240           if (e_pointer_is_hidden(e_comp->pointer) || !ec->pointer_enter_sent)
1241             _e_comp_wl_cursor_reload(ec);
1242           break;
1243
1244         case EVAS_CALLBACK_MOUSE_DOWN:
1245           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_DOWN, ec);
1246           if (!ret) break;
1247
1248           if (e_comp_wl->ptr.hide_tmr)
1249             {
1250                ecore_timer_del(e_comp_wl->ptr.hide_tmr);
1251                e_comp_wl->ptr.hide_tmr = NULL;
1252             }
1253           cursor_timer_ec = NULL;
1254
1255           if (e_pointer_is_hidden(e_comp->pointer) || !ec->pointer_enter_sent)
1256             _e_comp_wl_cursor_reload(ec);
1257           break;
1258
1259         case EVAS_CALLBACK_MOUSE_UP:
1260           ret = _e_comp_wl_intercept_hook_call(E_COMP_WL_INTERCEPT_HOOK_CURSOR_TIMER_MOUSE_UP, ec);
1261           break;
1262
1263         default:
1264           break;
1265      }
1266
1267    return ret;
1268 }
1269
1270 static Eina_Bool
1271 _e_comp_wl_check_cursor_timer_needed(E_Client *ec)
1272 {
1273    if (ec->has_cursor_unset)
1274      return EINA_FALSE;
1275
1276    if (!e_config->show_cursor)
1277      return EINA_FALSE;
1278
1279    if (!e_config->use_cursor_timer)
1280      return EINA_FALSE;
1281
1282    return EINA_TRUE;
1283 }
1284
1285 static void
1286 _e_comp_wl_evas_cb_mouse_in(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event)
1287 {
1288    E_Client *ec;
1289    Evas_Event_Mouse_In *ev;
1290    struct wl_resource *res;
1291    struct wl_client *wc;
1292    Eina_List *l;
1293    uint32_t serial;
1294    E_Comp_Config *comp_conf;
1295    E_Client *prev_ptr_ec;
1296
1297    ev = event;
1298
1299    e_comp_wl->ptr.x = wl_fixed_from_int(ev->output.x);
1300    e_comp_wl->ptr.y = wl_fixed_from_int(ev->output.y);
1301
1302    if (!(ec = data)) return;
1303    if (e_object_is_del(E_OBJECT(ec))) return;
1304    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1305    if (!surface) return;
1306
1307    prev_ptr_ec = e_comp_wl->ptr.ec;
1308    if (prev_ptr_ec)
1309      {
1310         ELOGF("Mouse", "In (prev_ptr_ec: %p pointer_enter_sent: %d)", ec, prev_ptr_ec, prev_ptr_ec->pointer_enter_sent);
1311         prev_ptr_ec->pointer_enter_sent = EINA_FALSE;
1312      }
1313
1314    e_comp_wl->ptr.ec = ec;
1315
1316    comp_conf = e_comp_config_get();
1317    if (comp_conf && comp_conf->input_log_enable)
1318      ELOGF("Mouse", "In  (obj: %p, time: %d, canvas(%d, %d) output(%d, %d), name:%20s)",
1319            ec, obj, ev->timestamp, ev->canvas.x, ev->canvas.y, ev->output.x, ev->output.y,
1320            e_client_util_name_get(ec));
1321
1322    if (_e_comp_wl_check_cursor_timer_needed(ec))
1323      {
1324         if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_IN, ec))
1325           return;
1326      }
1327
1328    if (!eina_list_count(e_comp_wl->ptr.resources)) return;
1329    wc = wl_resource_get_client(surface);
1330    serial = wl_display_next_serial(e_comp_wl->wl.disp);
1331    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
1332      {
1333         if (!e_comp_wl_input_pointer_check(res)) continue;
1334         if (wl_resource_get_client(res) != wc) continue;
1335
1336         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, ev->timestamp);
1337
1338         wl_pointer_send_enter(res, serial, surface,
1339                               wl_fixed_from_int(ev->canvas.x - ec->client.x),
1340                               wl_fixed_from_int(ev->canvas.y - ec->client.y));
1341         ec->pointer_enter_sent = EINA_TRUE;
1342      }
1343
1344    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_mousein_signal, ec);
1345 }
1346
1347 static void
1348 _e_comp_wl_evas_cb_mouse_out(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event)
1349 {
1350    E_Client *ec;
1351    Evas_Event_Mouse_Out *ev;
1352    struct wl_resource *res;
1353    struct wl_client *wc;
1354    Eina_List *l;
1355    uint32_t serial;
1356    Eina_Bool inside_check;
1357    E_Comp_Config *comp_conf;
1358
1359    ev = event;
1360
1361    if (!(ec = data)) return;
1362    inside_check = E_INSIDE(ev->canvas.x, ev->canvas.y,
1363                           ec->client.x, ec->client.y, ec->client.w, ec->client.h);
1364    if (ec->cur_mouse_action && inside_check) return;
1365    if (e_object_is_del(E_OBJECT(e_comp))) return;
1366    if (e_comp_wl->ptr.ec == ec)
1367      e_comp_wl->ptr.ec = NULL;
1368    if (e_object_is_del(E_OBJECT(ec))) return;
1369
1370    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1371    if (!surface) return;
1372
1373    comp_conf = e_comp_config_get();
1374    if (comp_conf && comp_conf->input_log_enable)
1375      ELOGF("Mouse", "Out (obj: %p, time: %d, canvas(%d, %d) output(%d, %d), name:%20s)",
1376            ec, obj, ev->timestamp, ev->canvas.x, ev->canvas.y, ev->output.x, ev->output.y,
1377            e_client_util_name_get(ec));
1378
1379    if (_e_comp_wl_check_cursor_timer_needed(ec))
1380      {
1381         if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_OUT, ec))
1382           return;
1383      }
1384
1385    if (!eina_list_count(e_comp_wl->ptr.resources)) return;
1386
1387    wc = wl_resource_get_client(surface);
1388    serial = wl_display_next_serial(e_comp_wl->wl.disp);
1389    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
1390      {
1391         if (!e_comp_wl_input_pointer_check(res)) continue;
1392         if (wl_resource_get_client(res) != wc) continue;
1393         if (ec->pointer_enter_sent == EINA_FALSE) continue;
1394
1395         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, ev->timestamp);
1396
1397         wl_pointer_send_leave(res, serial, surface);
1398         ec->pointer_enter_sent = EINA_FALSE;
1399      }
1400 }
1401
1402 static void
1403 _e_comp_wl_send_touch(E_Client *ec, int idx, int canvas_x, int canvas_y, uint32_t timestamp, Eina_Bool pressed)
1404 {
1405    Eina_List *l;
1406    struct wl_client *wc;
1407    struct wl_resource *res;
1408    wl_fixed_t x, y;
1409    uint32_t serial;
1410    E_Comp_Config *comp_conf = NULL;
1411
1412    if (!ec) return;
1413    if (e_object_is_del(E_OBJECT(ec))) return;
1414    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1415    if (!surface) return;
1416
1417    wc = wl_resource_get_client(surface);
1418    serial = wl_display_next_serial(e_comp_wl->wl.disp);
1419
1420    if (pressed)
1421      {
1422         x = wl_fixed_from_int(canvas_x - ec->client.x);
1423         y = wl_fixed_from_int(canvas_y - ec->client.y);
1424      }
1425
1426    comp_conf = e_comp_config_get();
1427
1428    EINA_LIST_FOREACH(e_comp_wl->touch.resources, l, res)
1429      {
1430         if (wl_resource_get_client(res) != wc) continue;
1431         if (!e_comp_wl_input_touch_check(res)) continue;
1432         TRACE_INPUT_BEGIN(_e_comp_wl_send_touch);
1433         if (pressed)
1434           {
1435              if (comp_conf && comp_conf->input_log_enable)
1436                ELOGF("Touch", "Down (id: %d, time: %d, x:%d, y:%d, name:%20s)", ec, idx, timestamp, canvas_x - ec->client.x, canvas_y - ec->client.y, e_client_util_name_get(ec));
1437
1438              wl_touch_send_down(res, serial, timestamp, surface, idx, x, y); //id 0 for the 1st finger
1439           }
1440         else
1441           {
1442              if (comp_conf && comp_conf->input_log_enable)
1443                ELOGF("Touch", "Up (id: %d, time: %d, x:%d, y:%d, name:%20s)\n", ec, idx, timestamp, canvas_x - ec->client.x, canvas_y - ec->client.y, e_client_util_name_get(ec));
1444
1445              wl_touch_send_up(res, serial, timestamp, idx);
1446           }
1447         TRACE_INPUT_END();
1448      }
1449 }
1450
1451 static void
1452 _e_comp_wl_send_touch_move(E_Client *ec, int idx, int canvas_x, int canvas_y, uint32_t timestamp)
1453 {
1454    Eina_List *l;
1455    struct wl_client *wc;
1456    struct wl_resource *res;
1457    wl_fixed_t x, y;
1458
1459    if (!ec) return;
1460    if (ec->cur_mouse_action) return;
1461    if (e_object_is_del(E_OBJECT(ec))) return;
1462    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1463    if (!surface) return;
1464
1465    wc = wl_resource_get_client(surface);
1466
1467    x = wl_fixed_from_int(canvas_x - ec->client.x);
1468    y = wl_fixed_from_int(canvas_y - ec->client.y);
1469
1470    EINA_LIST_FOREACH(e_comp_wl->touch.resources, l, res)
1471      {
1472         if (wl_resource_get_client(res) != wc) continue;
1473         if (!e_comp_wl_input_touch_check(res)) continue;
1474         wl_touch_send_motion(res, timestamp, idx, x, y);
1475      }
1476 }
1477
1478 static void
1479 _e_comp_wl_send_mouse_move(E_Client *ec, int x, int y, unsigned int timestamp)
1480 {
1481    struct wl_resource *res;
1482    struct wl_client *wc;
1483    Eina_List *l;
1484
1485    if (!ec) return;
1486    if (ec->cur_mouse_action) return;
1487    if (e_object_is_del(E_OBJECT(ec))) return;
1488    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1489    if (!surface) return;
1490
1491    wc = wl_resource_get_client(surface);
1492    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
1493      {
1494         if (!e_comp_wl_input_pointer_check(res)) continue;
1495         if (wl_resource_get_client(res) != wc) continue;
1496         wl_pointer_send_motion(res, timestamp,
1497                                wl_fixed_from_int(x - ec->client.x),
1498                                wl_fixed_from_int(y - ec->client.y));
1499      }
1500 }
1501
1502 static void
1503 _e_comp_wl_cursor_move_timer_control(E_Client *ec)
1504 {
1505    if (e_comp_wl->ptr.hide_tmr)
1506      {
1507         if (cursor_timer_ec == ec)
1508           {
1509              ecore_timer_interval_set(e_comp_wl->ptr.hide_tmr, e_config->cursor_timer_interval);
1510              ecore_timer_reset(e_comp_wl->ptr.hide_tmr);
1511           }
1512         else
1513           {
1514              ecore_timer_del(e_comp_wl->ptr.hide_tmr);
1515              cursor_timer_ec = ec;
1516              e_comp_wl->ptr.hide_tmr = ecore_timer_add(e_config->cursor_timer_interval, _e_comp_wl_cursor_timer, ec);
1517           }
1518      }
1519    else
1520      {
1521         cursor_timer_ec = ec;
1522         e_comp_wl->ptr.hide_tmr = ecore_timer_add(e_config->cursor_timer_interval, _e_comp_wl_cursor_timer, ec);
1523      }
1524 }
1525
1526 static void
1527 _e_comp_wl_evas_cb_mouse_move(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event)
1528 {
1529    E_Client *ec;
1530    Evas_Event_Mouse_Move *ev;
1531    Evas_Device *dev = NULL;
1532    const char *dev_name;
1533    E_Comp_Config *comp_conf;
1534
1535    ev = event;
1536
1537    e_comp->wl_comp_data->ptr.x = wl_fixed_from_int(ev->cur.canvas.x);
1538    e_comp->wl_comp_data->ptr.y = wl_fixed_from_int(ev->cur.canvas.y);
1539
1540    if (!(ec = data)) return;
1541    if (e_object_is_del(E_OBJECT(ec))) return;
1542    if ((ec->ignored) && (!ec->remote_surface.provider)) return;
1543    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1544    if (!surface) return;
1545
1546    if ((!need_send_motion) && (!need_send_released) && (ec->visibility.obscured == E_VISIBILITY_FULLY_OBSCURED)) return;
1547
1548    dev = ev->dev;
1549    dev_name = evas_device_description_get(dev);
1550
1551    comp_conf = e_comp_config_get();
1552    if (comp_conf && comp_conf->input_log_enable)
1553      ELOGF("Mouse", "Move (obj: %p, time: %d, canvas(%d, %d), output(%d, %d), name:%20s) (dev:%s)",
1554            ec, obj, ev->timestamp, ev->cur.canvas.x, ev->cur.canvas.y, ev->cur.output.x, ev->cur.output.y,
1555            e_client_util_name_get(ec), dev_name);
1556
1557    if (dev && (evas_device_class_get(dev) == EVAS_DEVICE_CLASS_TOUCH))
1558      {
1559         if (!e_comp_wl->drag_client)
1560           {
1561              e_comp_wl->touch.faked_ec = ec;
1562              if (e_comp_wl->touch.pressed & (1 << 0))
1563                {
1564                   _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1565                   if (dev_name)
1566                     _e_comp_wl_device_handle_axes(dev_name, evas_device_class_get(dev),
1567                                                   ec, 0, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1568                   _e_comp_wl_send_touch_move(ec, 0, ev->cur.canvas.x, ev->cur.canvas.y, ev->timestamp);
1569                }
1570           }
1571
1572         e_pointer_touch_move(e_comp->pointer, ev->cur.output.x, ev->cur.output.y);
1573      }
1574    else
1575      {
1576         if (!e_comp_wl->drag_client)
1577           {
1578              if (_e_comp_wl_check_cursor_timer_needed(ec))
1579                {
1580                   if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_MOVE, ec))
1581                     return;
1582                }
1583
1584              _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1585              _e_comp_wl_send_mouse_move(ec, ev->cur.canvas.x, ev->cur.canvas.y, ev->timestamp);
1586
1587              if (!need_send_released) // set cursor's hide_tmr only when mouse button is not pressed
1588                {
1589                   if (_e_comp_wl_check_cursor_timer_needed(ec))
1590                     _e_comp_wl_cursor_move_timer_control(ec);
1591                }
1592           }
1593
1594         e_pointer_mouse_move(e_comp->pointer, ev->cur.output.x, ev->cur.output.y);
1595      }
1596 }
1597
1598 static void
1599 _e_comp_wl_evas_handle_mouse_button_to_touch(E_Client *ec, uint32_t timestamp, int canvas_x, int canvas_y, Eina_Bool flag)
1600 {
1601    if (ec->cur_mouse_action || e_comp_wl->drag) return;
1602    if (e_object_is_del(E_OBJECT(ec))) return;
1603    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1604    if (!surface) return;
1605    if ((ec->ignored) && (!ec->remote_surface.provider)) return;
1606
1607    e_comp_wl->ptr.button = BTN_LEFT;
1608
1609    _e_comp_wl_send_touch(ec, 0, canvas_x, canvas_y, timestamp, flag);
1610 }
1611
1612 static void
1613 _e_comp_wl_evas_cb_mouse_down(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event)
1614 {
1615    E_Client *ec = data;
1616    Evas_Event_Mouse_Down *ev = event;
1617    Evas_Device *dev = NULL;
1618    const Evas_Device *seat_dev;
1619    const char *dev_name, *seat_name;
1620    E_Comp_Config *comp_conf = NULL;
1621
1622    if (!ec) return;
1623    if (e_object_is_del(E_OBJECT(ec))) return;
1624
1625    dev = ev->dev;
1626    dev_name = evas_device_description_get(dev);
1627
1628    seat_dev = evas_device_parent_get(dev);
1629    seat_name = evas_device_name_get(seat_dev);
1630
1631    comp_conf = e_comp_config_get();
1632    if (comp_conf && comp_conf->input_log_enable)
1633      ELOGF("Touch", "Down (obj: %p, button: %d, time: %d, x:%d, y:%d, name:%20s) (dev:%s, seat:%s)",
1634            ec, obj, ev->button, ev->timestamp, ev->output.x, ev->output.y, e_client_util_name_get(ec),
1635            dev_name, seat_name);
1636
1637    _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1638
1639    if (dev &&  (evas_device_class_get(dev) == EVAS_DEVICE_CLASS_TOUCH))
1640      {
1641         e_comp_wl->touch.faked_ec = ec;
1642
1643         if (dev_name)
1644           _e_comp_wl_device_renew_axis(dev_name, evas_device_class_get(dev),
1645                                         ec, 0, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1646         _e_comp_wl_evas_handle_mouse_button_to_touch(ec, ev->timestamp, ev->canvas.x, ev->canvas.y, EINA_TRUE);
1647
1648         e_pointer_touch_move(e_comp->pointer, ev->output.x, ev->output.y);
1649         e_comp_wl->touch.pressed |= (1 << 0);
1650      }
1651    else
1652      {
1653         if (_e_comp_wl_check_cursor_timer_needed(ec))
1654           {
1655              if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_DOWN, ec))
1656                return;
1657           }
1658
1659         e_comp_wl_evas_handle_mouse_button(ec, ev->timestamp, ev->button,
1660                                            WL_POINTER_BUTTON_STATE_PRESSED);
1661
1662         e_pointer_mouse_move(e_comp->pointer, ev->output.x, ev->output.y);
1663      }
1664
1665    need_send_released = EINA_TRUE;
1666 }
1667
1668 static void
1669 _e_comp_wl_evas_cb_mouse_up(void *data, Evas *evas, Evas_Object *obj, void *event)
1670 {
1671    E_Client *ec = data;
1672    Evas_Event_Mouse_Up *ev = event;
1673    Evas_Device *dev = NULL;
1674    const Evas_Device *seat_dev;
1675    const char *dev_name, *seat_name;;
1676    Evas_Event_Flags flags;
1677    E_Comp_Config *comp_conf = NULL;
1678
1679    if (!ec) return;
1680    if (ec->cur_mouse_action) return;
1681    if (e_object_is_del(E_OBJECT(ec))) return;
1682
1683    if (!need_send_released)
1684      {
1685         need_send_motion = EINA_TRUE;
1686      }
1687
1688    dev = ev->dev;
1689    dev_name = evas_device_description_get(dev);
1690
1691    seat_dev = evas_device_parent_get(dev);
1692    seat_name = evas_device_name_get(seat_dev);
1693    flags = evas_event_default_flags_get(evas);
1694
1695    comp_conf = e_comp_config_get();
1696    if (comp_conf && comp_conf->input_log_enable)
1697      ELOGF("Touch", "Up (obj: %p, button: %d, flag: 0x%x, time: %d, x:%d, y:%d, name:%20s) (dev:%s, seat:%s)",
1698            ec, obj, ev->button, flags, ev->timestamp, ev->output.x, ev->output.y, e_client_util_name_get(ec),
1699            dev_name, seat_name);
1700
1701    if (dev && (evas_device_class_get(dev) == EVAS_DEVICE_CLASS_TOUCH))
1702      {
1703         e_comp_wl->touch.pressed &= ~(1 << 0);
1704
1705         if (!e_comp_wl->touch.pressed && e_comp_wl->touch.faked_ec)
1706           e_comp_wl->touch.faked_ec = NULL;
1707      }
1708
1709    if (flags & EVAS_EVENT_FLAG_ON_HOLD) goto finish;
1710
1711    _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1712
1713    if (dev && (evas_device_class_get(dev) == EVAS_DEVICE_CLASS_TOUCH))
1714      {
1715         if (dev_name)
1716           _e_comp_wl_device_handle_axes(dev_name, evas_device_class_get(dev),
1717                                         ec, 0, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1718         _e_comp_wl_evas_handle_mouse_button_to_touch(ec, ev->timestamp, ev->canvas.x, ev->canvas.y, EINA_FALSE);
1719      }
1720    else
1721      {
1722         if (_e_comp_wl_check_cursor_timer_needed(ec))
1723           {
1724              if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_UP, ec))
1725                return;
1726           }
1727
1728         e_comp_wl_evas_handle_mouse_button(ec, ev->timestamp, ev->button,
1729                                            WL_POINTER_BUTTON_STATE_RELEASED);
1730
1731         if (_e_comp_wl_check_cursor_timer_needed(ec))
1732           _e_comp_wl_cursor_move_timer_control(ec);
1733      }
1734
1735 finish:
1736    need_send_released = EINA_FALSE;
1737 }
1738
1739 static void
1740 _e_comp_wl_mouse_wheel_send(E_Client *ec, int direction, int z, int timestamp)
1741 {
1742    struct wl_resource *res;
1743    struct wl_client *wc;
1744    Eina_List *l;
1745    uint32_t axis, dir;
1746
1747    if (direction == 0)
1748      axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1749    else
1750      axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
1751
1752    if (z < 0)
1753      dir = -wl_fixed_from_int(abs(z));
1754    else
1755      dir = wl_fixed_from_int(z);
1756
1757    if (!ec) return;
1758    if (e_object_is_del(E_OBJECT(ec))) return;
1759    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1760    if (!surface) return;
1761
1762    wc = wl_resource_get_client(surface);
1763    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
1764      {
1765         if (!e_comp_wl_input_pointer_check(res)) continue;
1766         if (wl_resource_get_client(res) != wc) continue;
1767         wl_pointer_send_axis(res, timestamp, axis, dir);
1768      }
1769 }
1770
1771 static void
1772 _e_comp_wl_evas_cb_mouse_wheel(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
1773 {
1774    E_Client *ec;
1775    Evas_Event_Mouse_Wheel *ev;
1776
1777    ev = event;
1778    if (!(ec = data)) return;
1779    if (ec->cur_mouse_action) return;
1780    if (e_object_is_del(E_OBJECT(ec))) return;
1781    if ((ec->ignored) && (!ec->remote_surface.provider)) return;
1782    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1783    if (!surface) return;
1784
1785    if (!eina_list_count(e_comp_wl->ptr.resources))
1786      return;
1787
1788    if (_e_comp_wl_check_cursor_timer_needed(ec))
1789       {
1790          if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_WHEEL, ec))
1791            return;
1792       }
1793
1794    _e_comp_wl_device_send_event_device(ec, ev->dev, ev->timestamp);
1795
1796    _e_comp_wl_mouse_wheel_send(ec, ev->direction, ev->z, ev->timestamp);
1797
1798    if (!need_send_released) // set cursor's hide_tmr only when mouse button is not pressed
1799      {
1800         if (_e_comp_wl_check_cursor_timer_needed(ec))
1801           _e_comp_wl_cursor_move_timer_control(ec);
1802      }
1803 }
1804
1805 static void
1806 _e_comp_wl_evas_cb_multi_down(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event)
1807 {
1808    E_Client *ec = data;
1809    Evas_Event_Multi_Down *ev = event;
1810    Evas_Device *dev = NULL;
1811    const Evas_Device *seat_dev;
1812    const char *dev_name, *seat_name;
1813    Evas_Device_Class dev_class;
1814    E_Comp_Config *comp_conf = NULL;
1815
1816    if (!ec) return;
1817    if (e_object_is_del(E_OBJECT(ec))) return;
1818    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1819    if (!surface) return;
1820
1821    /* Do not deliver emulated single touch events to client */
1822    if (ev->device == 0) return;
1823
1824    dev = ev->dev;
1825    dev_name = evas_device_description_get(dev);
1826    e_comp_wl->touch.faked_ec = ec;
1827
1828    seat_dev = evas_device_parent_get(dev);
1829    seat_name = evas_device_name_get(seat_dev);
1830
1831    comp_conf = e_comp_config_get();
1832    if (comp_conf && comp_conf->input_log_enable)
1833      ELOGF("Touch", "Down (obj: %p, idx: %d, time: %d, x:%d, y:%d, name:%20s) (dev:%s, seat:%s)",
1834            ec, obj, ev->device, ev->timestamp, ev->output.x, ev->output.y, e_client_util_name_get(ec),
1835            dev_name, seat_name);
1836
1837    if (dev && dev_name)
1838      {
1839         dev_class = evas_device_class_get(dev);
1840         _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1841         _e_comp_wl_device_renew_axis(dev_name, dev_class, ec, ev->device, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1842      }
1843
1844    _e_comp_wl_send_touch(ec, ev->device, ev->canvas.x, ev->canvas.y, ev->timestamp, EINA_TRUE);
1845    e_comp_wl->touch.pressed |= (1 << ev->device);
1846 }
1847
1848 static void
1849 _e_comp_wl_evas_cb_multi_up(void *data, Evas *evas, Evas_Object *obj EINA_UNUSED, void *event)
1850 {
1851    E_Client *ec = data;
1852    Evas_Event_Multi_Up *ev = event;
1853    Evas_Device *dev = NULL;
1854    const Evas_Device *seat_dev;
1855    const char *dev_name, *seat_name;
1856    Evas_Device_Class dev_class;
1857    Evas_Event_Flags flags;
1858    E_Comp_Config *comp_conf = NULL;
1859
1860    if (!ec) return;
1861    if (e_object_is_del(E_OBJECT(ec))) return;
1862    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1863    if (!surface) return;
1864
1865    /* Do not deliver emulated single touch events to client */
1866    if (ev->device == 0) return;
1867
1868    flags = evas_event_default_flags_get(evas);
1869
1870    e_comp_wl->touch.pressed &= ~(1 << ev->device);
1871    if (!e_comp_wl->touch.pressed && e_comp_wl->touch.faked_ec)
1872      e_comp_wl->touch.faked_ec = NULL;
1873
1874    if (flags & EVAS_EVENT_FLAG_ON_HOLD) return;
1875
1876    dev = ev->dev;
1877    dev_name = evas_device_description_get(dev);
1878
1879    seat_dev = evas_device_parent_get(dev);
1880    seat_name = evas_device_name_get(seat_dev);
1881
1882    comp_conf = e_comp_config_get();
1883    if (comp_conf && comp_conf->input_log_enable)
1884      ELOGF("Touch", "Up (obj: %p, idx: %d, flag: 0x%x, time: %d, x:%d, y:%d, name:%20s) (dev:%s, seat:%s)",
1885            ec, obj, ev->device, flags, ev->timestamp, ev->output.x, ev->output.y, e_client_util_name_get(ec),
1886            dev_name, seat_name);
1887
1888    if (dev && dev_name)
1889      {
1890         dev_class = evas_device_class_get(dev);
1891         _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1892         _e_comp_wl_device_handle_axes(dev_name, dev_class, ec, ev->device, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1893      }
1894
1895    _e_comp_wl_send_touch(ec, ev->device, 0, 0, ev->timestamp, EINA_FALSE);
1896 }
1897
1898 static void
1899 _e_comp_wl_evas_cb_multi_move(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
1900 {
1901    E_Client *ec = data;
1902    Evas_Event_Multi_Move *ev = event;
1903    Evas_Device *dev = NULL;
1904    const char *dev_name;
1905    Evas_Device_Class dev_class;
1906
1907    if (!ec) return;
1908    if (e_object_is_del(E_OBJECT(ec))) return;
1909    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
1910    if (!surface) return;
1911
1912    /* Do not deliver emulated single touch events to client */
1913    if (ev->device == 0) return;
1914
1915    e_comp_wl->touch.faked_ec = ec;
1916
1917    if (e_comp_wl->touch.pressed & (1 << ev->device))
1918      {
1919         dev = ev->dev;
1920         if (dev && (dev_name = evas_device_description_get(dev)))
1921           {
1922              dev_class = evas_device_class_get(dev);
1923              _e_comp_wl_device_send_event_device(ec, dev, ev->timestamp);
1924              _e_comp_wl_device_handle_axes(dev_name, dev_class, ec, ev->device, ev->radius_x, ev->radius_y, ev->pressure, ev->angle);
1925           }
1926
1927         _e_comp_wl_send_touch_move(ec, ev->device, ev->cur.canvas.x, ev->cur.canvas.y, ev->timestamp);
1928      }
1929 }
1930
1931 static void
1932 _e_comp_wl_client_priority_adjust(int pid, int set, int adj, Eina_Bool use_adj, Eina_Bool adj_child, Eina_Bool do_child)
1933 {
1934    Eina_List *files;
1935    char *file, buff[PATH_MAX];
1936    FILE *f;
1937    int pid2, ppid;
1938    int num_read;
1939    int n;
1940
1941    if (use_adj)
1942      n = (getpriority(PRIO_PROCESS, pid) + adj);
1943    else
1944      n = set;
1945
1946    setpriority(PRIO_PROCESS, pid, n);
1947
1948    if (adj_child)
1949      use_adj = EINA_TRUE;
1950
1951    if (!do_child) return;
1952
1953    files = ecore_file_ls("/proc");
1954    EINA_LIST_FREE(files, file)
1955       {
1956          if (!isdigit(file[0]))
1957            continue;
1958
1959          snprintf(buff, sizeof(buff), "/proc/%s/stat", file);
1960          if ((f = fopen(buff, "r")))
1961            {
1962               pid2 = -1;
1963               ppid = -1;
1964               num_read = fscanf(f, "%i %*s %*s %i %*s", &pid2, &ppid);
1965               fclose(f);
1966               if (num_read == 2 && ppid == pid)
1967                 _e_comp_wl_client_priority_adjust(pid2, set,
1968                                                   adj, use_adj,
1969                                                   adj_child, do_child);
1970            }
1971
1972          free(file);
1973       }
1974 }
1975
1976 static void
1977 _e_comp_wl_client_priority_raise(E_Client *ec)
1978 {
1979    if (!e_config->priority_control) return;
1980    if (ec->netwm.pid <= 0) return;
1981    if (ec->netwm.pid == getpid()) return;
1982    _e_comp_wl_client_priority_adjust(ec->netwm.pid,
1983                                      e_config->priority - 1, -1,
1984                                      EINA_FALSE, EINA_TRUE, EINA_FALSE);
1985 }
1986
1987 static void
1988 _e_comp_wl_client_priority_normal(E_Client *ec)
1989 {
1990    if (!e_config->priority_control) return;
1991    if (ec->netwm.pid <= 0) return;
1992    if (ec->netwm.pid == getpid()) return;
1993    _e_comp_wl_client_priority_adjust(ec->netwm.pid, e_config->priority, 1,
1994                                      EINA_FALSE, EINA_TRUE, EINA_FALSE);
1995 }
1996
1997 static Eina_Bool
1998 _e_comp_wl_evas_cb_focus_in_timer(E_Client *ec)
1999 {
2000    uint32_t serial;
2001    E_Comp_Wl_Key_Data *k;
2002    struct wl_resource *res;
2003    Eina_List *l;
2004    double t;
2005
2006    if (!ec) return EINA_FALSE;
2007    if (e_object_is_del(E_OBJECT(ec))) return EINA_FALSE;
2008    if (!ec->comp_data) return EINA_FALSE;
2009
2010    ec->comp_data->on_focus_timer = NULL;
2011    g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
2012    if (!e_comp_wl->kbd.focused)
2013      {
2014         g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2015         return EINA_FALSE;
2016      }
2017    g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2018
2019    serial = wl_display_next_serial(e_comp_wl->wl.disp);
2020    t = ecore_time_unix_get();
2021
2022    g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
2023    g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
2024    EINA_LIST_FOREACH(e_comp_wl->kbd.focused, l, res)
2025      {
2026         wl_array_for_each(k, &e_comp_wl->kbd.keys)
2027           {
2028              _e_comp_wl_send_event_device(wl_resource_get_client(res), t, k->dev, serial);
2029              wl_keyboard_send_key(res, serial, t,
2030                                   k->key, WL_KEYBOARD_KEY_STATE_PRESSED);
2031           }
2032      }
2033    g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
2034    g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2035    return EINA_FALSE;
2036 }
2037
2038 /* It is called in the following cases:
2039  *  When a normal ec->frame has focus.
2040  *  Or launching image ec is replaced to the real ec.
2041  */
2042 EINTERN void
2043 e_comp_wl_feed_focus_in(E_Client *ec)
2044 {
2045    E_Client *focused;
2046    struct wl_resource *res;
2047    struct wl_client *wc;
2048    Eina_List *l;
2049
2050    if (!ec) return;
2051    if (e_object_is_del(E_OBJECT(ec))) return;
2052    if (ec->iconic) return;
2053
2054    /* block spurious focus events */
2055    focused = e_client_focused_get();
2056    if ((focused) && (ec != focused)) return;
2057
2058    /* raise client priority */
2059    _e_comp_wl_client_priority_raise(ec);
2060    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
2061    if (!surface) return;
2062
2063    wc = wl_resource_get_client(surface);
2064
2065    g_mutex_lock(&e_comp_wl->kbd.resource_mutex);
2066    EINA_LIST_FOREACH(e_comp_wl->kbd.resources, l, res)
2067      {
2068         if (wl_resource_get_client(res) == wc)
2069           {
2070              g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
2071              if (!eina_list_data_find(e_comp_wl->kbd.focused, res))
2072                e_comp_wl->kbd.focused = eina_list_append(e_comp_wl->kbd.focused, res);
2073              g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2074           }
2075      }
2076    g_mutex_unlock(&e_comp_wl->kbd.resource_mutex);
2077
2078    g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
2079    if (!e_comp_wl->kbd.focused)
2080      {
2081         g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2082         return;
2083      }
2084    g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2085
2086    e_comp_wl->kbd.focus = surface;
2087    e_comp_wl_input_keyboard_enter_send(ec);
2088    e_comp_wl_data_device_keyboard_focus_set();
2089    ec->comp_data->on_focus_timer =
2090       ecore_timer_add(((e_config->xkb.delay_held_key_input_to_focus)/1000.0),
2091                       (Ecore_Task_Cb)_e_comp_wl_evas_cb_focus_in_timer, ec);
2092    int rotation = ec->e.state.rot.ang.curr;
2093    e_pointer_rotation_set(e_comp->pointer, rotation);
2094 }
2095
2096 static void
2097 _e_comp_wl_evas_cb_focus_in(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2098 {
2099    E_Client *ec;
2100    if (!(ec = data)) return;
2101    e_comp_wl_feed_focus_in(ec);
2102 }
2103
2104 static void
2105 _e_comp_wl_evas_cb_focus_out(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2106 {
2107    E_Client *ec;
2108    struct wl_resource *res;
2109    uint32_t serial;
2110    E_Comp_Wl_Key_Data *k;
2111    Eina_List *l, *ll;
2112    double t;
2113
2114    if (!(ec = data)) return;
2115
2116    if (!ec->comp_data) return;
2117
2118    E_FREE_FUNC(ec->comp_data->on_focus_timer, ecore_timer_del);
2119
2120    /* lower client priority */
2121    if (!e_object_is_del(data))
2122      _e_comp_wl_client_priority_normal(ec);
2123
2124
2125    /* update keyboard modifier state */
2126    g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
2127    wl_array_for_each(k, &e_comp_wl->kbd.keys)
2128       e_comp_wl_input_keyboard_state_update(k->key, EINA_FALSE);
2129
2130    g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
2131
2132    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
2133    if (!surface) return;
2134
2135    g_mutex_lock(&e_comp_wl->kbd.resource_mutex);
2136    if (!eina_list_count(e_comp_wl->kbd.resources))
2137      {
2138         g_mutex_unlock(&e_comp_wl->kbd.resource_mutex);
2139         return;
2140      }
2141
2142    g_mutex_unlock(&e_comp_wl->kbd.resource_mutex);
2143
2144    /* send keyboard_leave to all keyboard resources */
2145    serial = wl_display_next_serial(e_comp_wl->wl.disp);
2146    t = ecore_time_unix_get();
2147
2148    g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
2149    g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
2150    EINA_LIST_FOREACH_SAFE(e_comp_wl->kbd.focused, l, ll, res)
2151      {
2152         wl_array_for_each(k, &e_comp_wl->kbd.keys)
2153           {
2154              _e_comp_wl_send_event_device(wl_resource_get_client(res), t, k->dev, serial);
2155               wl_keyboard_send_key(res, serial, t,
2156                                    k->key, WL_KEYBOARD_KEY_STATE_RELEASED);
2157           }
2158         wl_keyboard_send_leave(res, serial, surface);
2159         e_comp_wl->kbd.focused =
2160            eina_list_remove_list(e_comp_wl->kbd.focused, l);
2161      }
2162    g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
2163    g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
2164 }
2165
2166 static void
2167 _e_comp_wl_evas_cb_resize(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2168 {
2169    E_Client *ec;
2170
2171    if (!(ec = data)) return;
2172
2173    if ((ec->shading) || (ec->shaded)) return;
2174    if (!ec->comp_data->shell.configure_send) return;
2175
2176    /* TODO: calculate x, y with transfrom object */
2177    if ((e_client_util_resizing_get(ec)) && (!ec->transformed) && (e_comp_wl->resize.edges))
2178      {
2179         int w, h;
2180
2181         w = ec->mouse.last_down[ec->moveinfo.down.button - 1].w;
2182         h = ec->mouse.last_down[ec->moveinfo.down.button - 1].h;
2183         if (e_comp_object_frame_exists(ec->frame))
2184           e_comp_object_frame_wh_unadjust(ec->frame, w, h, &w, &h);
2185
2186         switch (ec->resize_mode)
2187           {
2188            case E_POINTER_RESIZE_TL:
2189            case E_POINTER_RESIZE_L:
2190            case E_POINTER_RESIZE_BL:
2191              w += ec->mouse.last_down[ec->moveinfo.down.button - 1].mx -
2192                ec->mouse.current.mx;
2193              break;
2194            case E_POINTER_RESIZE_TR:
2195            case E_POINTER_RESIZE_R:
2196            case E_POINTER_RESIZE_BR:
2197              w += ec->mouse.current.mx - ec->mouse.last_down[ec->moveinfo.down.button - 1].mx;
2198              break;
2199            default:
2200              break;;
2201           }
2202         switch (ec->resize_mode)
2203           {
2204            case E_POINTER_RESIZE_TL:
2205            case E_POINTER_RESIZE_T:
2206            case E_POINTER_RESIZE_TR:
2207              h += ec->mouse.last_down[ec->moveinfo.down.button - 1].my -
2208                ec->mouse.current.my;
2209              break;
2210            case E_POINTER_RESIZE_BL:
2211            case E_POINTER_RESIZE_B:
2212            case E_POINTER_RESIZE_BR:
2213              h += ec->mouse.current.my - ec->mouse.last_down[ec->moveinfo.down.button - 1].my;
2214              break;
2215            default:
2216              break;
2217           }
2218         w = E_CLAMP(w, 1, w);
2219         h = E_CLAMP(h, 1, h);
2220         e_client_resize_limit(ec, &w, &h);
2221
2222         ec->comp_data->shell.configure_send(ec->comp_data->shell.surface,
2223                                             e_comp_wl->resize.edges,
2224                                             w, h);
2225      }
2226    else if ((!ec->fullscreen) && (!ec->maximized) &&
2227             (!ec->comp_data->maximize_pre))
2228      {
2229         int pw = 0;
2230         int ph = 0;
2231         e_pixmap_size_get(ec->pixmap, &pw, &ph);
2232         if ((pw != ec->w) || (ph != ec->h))
2233           {
2234              _e_comp_wl_configure_send(ec, 1, 1);
2235           }
2236      }
2237
2238    if (ec->comp_data->sub.below_obj)
2239      e_comp_wl_subsurface_bg_rectangle_map_apply(ec);
2240 }
2241
2242 static void
2243 _e_comp_wl_evas_cb_maximize_pre(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2244 {
2245    E_Client *ec = data;
2246
2247    ec->comp_data->maximize_pre = 1;
2248 }
2249
2250 static void
2251 _e_comp_wl_evas_cb_maximize_done(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2252 {
2253    E_Client *ec = data;
2254    int w, h;
2255
2256    if (e_object_is_del(E_OBJECT(ec))) return;
2257
2258    if (ec->comp_data->shell.configure_send)
2259      {
2260         e_client_maximized_geometry_get(ec, NULL, NULL, &w, &h);
2261         ec->comp_data->shell.configure_send(ec->comp_data->shell.surface, 0, w, h);
2262      }
2263    ec->comp_data->maximize_pre = 0;
2264 }
2265
2266 static void
2267 _e_comp_wl_evas_cb_unmaximize_pre(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2268 {
2269    E_Client *ec = data;
2270
2271    ec->comp_data->maximize_pre = 1;
2272 }
2273
2274 static void
2275 _e_comp_wl_evas_cb_unmaximize_done(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2276 {
2277    E_Client *ec = data;
2278
2279    if (e_object_is_del(E_OBJECT(ec))) return;
2280
2281    /* check for wayland pixmap */
2282
2283    if (ec->comp_data->shell.configure_send)
2284      _e_comp_wl_configure_send(ec, 0, 0);
2285
2286    ec->comp_data->maximize_pre = 0;
2287 }
2288
2289 static void
2290 _e_comp_wl_evas_cb_fullscreen(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2291 {
2292    E_Client *ec = data;
2293
2294    if (e_object_is_del(E_OBJECT(ec))) return;
2295
2296    /* check for wayland pixmap */
2297
2298    if (ec->comp_data->shell.configure_send)
2299      _e_comp_wl_configure_send(ec, 0, 1);
2300
2301    ec->comp_data->maximize_pre = 0;
2302 }
2303
2304 static void
2305 _e_comp_wl_evas_cb_unfullscreen(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2306 {
2307    E_Client *ec = data;
2308
2309    if (e_object_is_del(E_OBJECT(ec))) return;
2310
2311    /* check for wayland pixmap */
2312
2313    if (ec->comp_data->shell.configure_send)
2314      _e_comp_wl_configure_send(ec, 0, 0);
2315
2316    ec->comp_data->maximize_pre = 0;
2317 }
2318
2319 static void
2320 _e_comp_wl_evas_cb_delete_request(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2321 {
2322    E_Client *ec;
2323
2324    if (!(ec = data)) return;
2325
2326    e_comp_ignore_win_del(E_PIXMAP_TYPE_WL, e_pixmap_window_get(ec->pixmap));
2327
2328    e_object_del(E_OBJECT(ec));
2329
2330    _e_comp_wl_focus_check();
2331
2332    /* TODO: Delete request send ??
2333     * NB: No such animal wrt wayland */
2334 }
2335
2336 static void
2337 _e_comp_wl_evas_cb_kill_request(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2338 {
2339    E_Client *ec;
2340
2341    if (!(ec = data)) return;
2342
2343    e_comp_ignore_win_del(E_PIXMAP_TYPE_WL, e_pixmap_window_get(ec->pixmap));
2344    if (ec->comp_data)
2345      {
2346         if (ec->comp_data->reparented)
2347           e_client_comp_hidden_set(ec, EINA_TRUE);
2348      }
2349
2350    evas_object_pass_events_set(ec->frame, EINA_TRUE);
2351    if (ec->visible) evas_object_hide(ec->frame);
2352    if (!ec->internal) e_object_del(E_OBJECT(ec));
2353
2354    _e_comp_wl_focus_check();
2355 }
2356
2357 static void
2358 _e_comp_wl_evas_cb_ping(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2359 {
2360    E_Client *ec;
2361
2362    if (!(ec = data)) return;
2363    if (!(ec->comp_data)) return;
2364    if (!(ec->comp_data->shell.ping)) return;
2365    if (!(ec->comp_data->shell.surface)) return;
2366
2367    ec->comp_data->shell.ping(ec->comp_data->shell.surface);
2368 }
2369
2370 static void
2371 _e_comp_wl_evas_cb_color_set(void *data, Evas_Object *obj, void *event EINA_UNUSED)
2372 {
2373    E_Client *ec;
2374    int a = 0;
2375
2376    if (!(ec = data)) return;
2377    evas_object_color_get(obj, NULL, NULL, NULL, &a);
2378    if (ec->netwm.opacity == a) return;
2379    ec->netwm.opacity = a;
2380    ec->netwm.opacity_changed = EINA_TRUE;
2381 }
2382
2383 static void
2384 _e_comp_wl_buffer_damage_set(E_Comp_Wl_Buffer *buffer, Eina_List *buffer_damages)
2385 {
2386    Eina_Rectangle *damage_rect = NULL;
2387    Eina_Rectangle *dmg = NULL;
2388    Eina_List *l = NULL;
2389
2390    if (buffer->type != E_COMP_WL_BUFFER_TYPE_NATIVE &&
2391        buffer->type != E_COMP_WL_BUFFER_TYPE_TBM)
2392      return;
2393
2394    if (!buffer->tbm_surface) return;
2395
2396    if (buffer_damages)
2397      {
2398         EINA_LIST_FOREACH(buffer_damages, l, dmg)
2399           {
2400              if (!damage_rect)
2401                {
2402                   damage_rect = eina_rectangle_new(dmg->x, dmg->y, dmg->w, dmg->h);
2403                   EINA_SAFETY_ON_FALSE_RETURN(damage_rect);
2404                }
2405              else
2406                eina_rectangle_union(damage_rect, dmg);
2407           }
2408      }
2409    else
2410      {
2411         damage_rect = eina_rectangle_new(0, 0, buffer->w, buffer->h);
2412         EINA_SAFETY_ON_FALSE_RETURN(damage_rect);
2413      }
2414
2415    tbm_surface_internal_set_damage(buffer->tbm_surface,
2416                                    damage_rect->x,
2417                                    damage_rect->y,
2418                                    damage_rect->w,
2419                                    damage_rect->h);
2420
2421    eina_rectangle_free(damage_rect);
2422 }
2423
2424 static void
2425 _e_comp_wl_client_evas_init(E_Client *ec)
2426 {
2427    if (!ec || !ec->comp_data) return;
2428    if (ec->comp_data->evas_init) return;
2429
2430    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_SHOW,        _e_comp_wl_evas_cb_show,        ec);
2431    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_HIDE,        _e_comp_wl_evas_cb_hide,        ec);
2432    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_MOVE,        _e_comp_wl_evas_cb_move,        ec);
2433
2434
2435    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_IN,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_in,    ec);
2436    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_OUT,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_out,   ec);
2437    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_MOVE,  EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_move,  ec);
2438    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_DOWN,  EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_down,  ec);
2439    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_UP,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_up,    ec);
2440    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_WHEEL, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_wheel, ec);
2441
2442    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_DOWN, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_down, ec);
2443    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_UP,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_up,   ec);
2444    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_MOVE, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_move, ec);
2445
2446    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_FOCUS_IN,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_focus_in,    ec);
2447    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_FOCUS_OUT,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_focus_out,   ec);
2448
2449    if (!ec->override)
2450      {
2451         evas_object_smart_callback_add(ec->frame, "client_resize",   _e_comp_wl_evas_cb_resize,          ec);
2452         evas_object_smart_callback_add(ec->frame, "maximize_pre",    _e_comp_wl_evas_cb_maximize_pre,    ec);
2453         evas_object_smart_callback_add(ec->frame, "maximize_done",   _e_comp_wl_evas_cb_maximize_done,   ec);
2454         evas_object_smart_callback_add(ec->frame, "unmaximize_pre",  _e_comp_wl_evas_cb_unmaximize_pre,  ec);
2455         evas_object_smart_callback_add(ec->frame, "unmaximize_done", _e_comp_wl_evas_cb_unmaximize_done, ec);
2456         evas_object_smart_callback_add(ec->frame, "fullscreen",      _e_comp_wl_evas_cb_fullscreen,      ec);
2457         evas_object_smart_callback_add(ec->frame, "unfullscreen",    _e_comp_wl_evas_cb_unfullscreen,    ec);
2458      }
2459
2460    /* setup delete/kill callbacks */
2461    evas_object_smart_callback_add(ec->frame, "delete_request", _e_comp_wl_evas_cb_delete_request, ec);
2462    evas_object_smart_callback_add(ec->frame, "kill_request",   _e_comp_wl_evas_cb_kill_request,   ec);
2463
2464    /* setup ping callback */
2465    evas_object_smart_callback_add(ec->frame, "ping",           _e_comp_wl_evas_cb_ping,           ec);
2466    evas_object_smart_callback_add(ec->frame, "color_set",      _e_comp_wl_evas_cb_color_set,      ec);
2467
2468    ec->comp_data->evas_init = EINA_TRUE;
2469 }
2470
2471 static void
2472 _e_comp_wl_client_evas_deinit(E_Client *ec)
2473 {
2474    if (!ec || !ec->comp_data) return;
2475    if (!ec->comp_data->evas_init) return;
2476
2477    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_SHOW, _e_comp_wl_evas_cb_show);
2478    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_HIDE, _e_comp_wl_evas_cb_hide);
2479    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOVE, _e_comp_wl_evas_cb_move);
2480
2481    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_IN,    _e_comp_wl_evas_cb_mouse_in);
2482    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_OUT,   _e_comp_wl_evas_cb_mouse_out);
2483    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_MOVE,  _e_comp_wl_evas_cb_mouse_move);
2484    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_DOWN,  _e_comp_wl_evas_cb_mouse_down);
2485    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_UP,    _e_comp_wl_evas_cb_mouse_up);
2486    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_WHEEL, _e_comp_wl_evas_cb_mouse_wheel);
2487
2488    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_DOWN, _e_comp_wl_evas_cb_multi_down);
2489    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_UP,   _e_comp_wl_evas_cb_multi_up);
2490    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_MOVE, _e_comp_wl_evas_cb_multi_move);
2491
2492    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_FOCUS_IN,  _e_comp_wl_evas_cb_focus_in);
2493    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_FOCUS_OUT, _e_comp_wl_evas_cb_focus_out);
2494
2495    if (!ec->override)
2496      {
2497         evas_object_smart_callback_del(ec->frame, "client_resize",   _e_comp_wl_evas_cb_resize);
2498         evas_object_smart_callback_del(ec->frame, "maximize_pre",    _e_comp_wl_evas_cb_maximize_pre);
2499         evas_object_smart_callback_del(ec->frame, "maximize_done",   _e_comp_wl_evas_cb_maximize_done);
2500         evas_object_smart_callback_del(ec->frame, "unmaximize_pre",  _e_comp_wl_evas_cb_unmaximize_pre);
2501         evas_object_smart_callback_del(ec->frame, "unmaximize_done", _e_comp_wl_evas_cb_unmaximize_done);
2502         evas_object_smart_callback_del(ec->frame, "fullscreen",      _e_comp_wl_evas_cb_fullscreen);
2503         evas_object_smart_callback_del(ec->frame, "unfullscreen",    _e_comp_wl_evas_cb_unfullscreen);
2504      }
2505
2506    evas_object_smart_callback_del(ec->frame, "delete_request", _e_comp_wl_evas_cb_delete_request);
2507    evas_object_smart_callback_del(ec->frame, "kill_request",   _e_comp_wl_evas_cb_kill_request);
2508
2509    evas_object_smart_callback_del(ec->frame, "ping",           _e_comp_wl_evas_cb_ping);
2510    evas_object_smart_callback_del(ec->frame, "color_set",      _e_comp_wl_evas_cb_color_set);
2511
2512    ec->comp_data->evas_init = EINA_FALSE;
2513 }
2514
2515 static Eina_Bool
2516 _e_comp_wl_cb_randr_change(void *data EINA_UNUSED, int type EINA_UNUSED, void *event EINA_UNUSED)
2517 {
2518    Eina_List *l;
2519    E_Output *eout;
2520    E_Comp_Screen *e_comp_screen;
2521    unsigned int transform = WL_OUTPUT_TRANSFORM_NORMAL;
2522
2523    if (!e_comp) return ECORE_CALLBACK_RENEW;
2524    if (!e_comp->e_comp_screen) return ECORE_CALLBACK_RENEW;
2525    e_comp_screen = e_comp->e_comp_screen;
2526
2527    EINA_LIST_FOREACH(e_comp_screen->outputs, l, eout)
2528      {
2529         if (!eout->config.enabled)
2530           {
2531              e_comp_wl_output_remove(eout->id);
2532              continue;
2533           }
2534
2535         switch (eout->config.rotation)
2536           {
2537            case 90:
2538              transform = WL_OUTPUT_TRANSFORM_90;
2539              break;
2540            case 180:
2541              transform = WL_OUTPUT_TRANSFORM_180;
2542              break;
2543            case 270:
2544              transform = WL_OUTPUT_TRANSFORM_270;
2545              break;
2546            case 0:
2547            default:
2548              transform = WL_OUTPUT_TRANSFORM_NORMAL;
2549              break;
2550           }
2551
2552         if (!e_comp_wl_output_init(eout->id, eout->info.name,
2553                                    eout->info.screen,
2554                                    eout->config.geom.x, eout->config.geom.y,
2555                                    eout->config.geom.w, eout->config.geom.h,
2556                                    eout->info.size.w, eout->info.size.h,
2557                                    eout->config.mode.refresh, 0, transform))
2558           ERR("Could not initialize screen %s", eout->info.name);
2559      }
2560
2561    return ECORE_CALLBACK_RENEW;
2562 }
2563
2564 static Eina_Bool
2565 _e_comp_wl_cb_comp_object_add(void *data EINA_UNUSED, int type EINA_UNUSED, E_Event_Comp_Object *ev)
2566 {
2567    E_Client *ec;
2568
2569    /* try to get the client from the object */
2570    if (!(ec = e_comp_object_client_get(ev->comp_object)))
2571      return ECORE_CALLBACK_RENEW;
2572
2573    /* check for client being deleted */
2574    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_RENEW;
2575
2576    /* check for wayland pixmap */
2577    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL)
2578      return ECORE_CALLBACK_RENEW;
2579
2580    /* if we have not setup evas callbacks for this client, do it */
2581    if (!ec->comp_data->evas_init) _e_comp_wl_client_evas_init(ec);
2582
2583    return ECORE_CALLBACK_RENEW;
2584 }
2585
2586 static Eina_Bool
2587 _e_comp_wl_cb_mouse_move(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Move *ev)
2588 {
2589    int ec_x, ec_y;
2590
2591    e_comp_wl->ptr.x = wl_fixed_from_int(ev->x);
2592    e_comp_wl->ptr.y = wl_fixed_from_int(ev->y);
2593
2594    if (e_comp_wl->selection.target &&
2595        e_comp_wl->drag)
2596      {
2597         struct wl_resource *res;
2598         int x, y;
2599         E_Client *ec = NULL;
2600         E_Client *legacy_target = e_comp_wl->selection.target;
2601         int device_id = e_comp_wl_data_current_device_id_get();
2602
2603         if (device_id < 0)
2604           {
2605              e_comp_wl_data_current_device_id_set(ev->multi.device);
2606           }
2607         else if (device_id != ev->multi.device)
2608           {
2609              return ECORE_CALLBACK_RENEW;
2610           }
2611
2612         ec = e_client_under_position_input_get(legacy_target->desk, ev->x, ev->y);
2613         EINA_SAFETY_ON_NULL_RETURN_VAL(ec, ECORE_CALLBACK_RENEW);
2614
2615         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
2616         EINA_SAFETY_ON_NULL_RETURN_VAL(surface, ECORE_CALLBACK_RENEW);
2617
2618         res = e_comp_wl_data_find_for_client(wl_resource_get_client(surface));
2619         EINA_SAFETY_ON_NULL_RETURN_VAL(res, ECORE_CALLBACK_RENEW);
2620
2621         if ((e_comp_wl->drag_offer != wl_resource_get_user_data(res)) &&
2622             (ec != legacy_target))
2623           {
2624              e_comp_wl_data_device_send_leave(legacy_target);
2625              e_comp_wl_data_device_send_enter(ec);
2626           }
2627
2628         if (e_comp_wl->drag)
2629           e_drag_move(e_comp_wl->drag, ev->x, ev->y);
2630
2631         if (e_client_transform_core_enable_get(ec))
2632           {
2633              int trans_x, trans_y;
2634              e_client_transform_core_input_transform(ec, ev->x, ev->y, &trans_x, &trans_y);
2635              x = trans_x - ec->client.x;
2636              y = trans_y - ec->client.y;
2637           }
2638         else
2639           {
2640              e_client_geometry_get(ec, &ec_x, &ec_y, NULL, NULL);
2641              x = ev->x - ec_x;
2642              y = ev->y - ec_y;
2643           }
2644
2645         wl_data_device_send_motion(res, ev->timestamp, wl_fixed_from_int(x), wl_fixed_from_int(y));
2646      }
2647
2648    return ECORE_CALLBACK_RENEW;
2649 }
2650
2651 static Eina_Bool
2652 _e_comp_wl_cb_mouse_relative_move(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Relative_Move *ev)
2653 {
2654    E_Client *ec;
2655    struct wl_resource *surface;
2656    struct wl_resource *res;
2657    struct wl_client *wc;
2658    Eina_List *l;
2659    E_Comp_Config *comp_conf;
2660
2661    ec = e_comp_wl->ptr_constraints.ec;
2662    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, ECORE_CALLBACK_RENEW);
2663
2664    surface = e_comp_wl_client_surface_get(ec);
2665    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, ECORE_CALLBACK_RENEW);
2666
2667    wc = wl_resource_get_client(surface);
2668
2669    comp_conf = e_comp_config_get();
2670
2671    EINA_LIST_FOREACH(e_comp_wl->relative_ptr.resources, l, res)
2672      {
2673         if (!e_comp_wl_input_relative_pointer_check(res)) continue;
2674         if (wl_resource_get_client(res) != wc) continue;
2675
2676         if (comp_conf && comp_conf->input_log_enable)
2677           ELOGF("Mouse", "Relative Move (time: %d, dx:%d dy:%d, unaccel(%d, %d) name:%20s)",
2678                 ec, ev->timestamp, ev->dx, ev->dy, ev->dx_unaccel, ev->dy_unaccel,
2679                 e_client_util_name_get(ec));
2680
2681         zwp_relative_pointer_v1_send_relative_motion(res,
2682                                                      0,
2683                                                      (uint32_t)(ev->timestamp),
2684                                                      wl_fixed_from_int(ev->dx),
2685                                                      wl_fixed_from_int(ev->dy),
2686                                                      wl_fixed_from_int(ev->dx_unaccel),
2687                                                      wl_fixed_from_int(ev->dy_unaccel));
2688      }
2689
2690    return ECORE_CALLBACK_DONE;
2691 }
2692
2693 static Eina_Bool
2694 _e_comp_wl_cb_mouse_button_cancel(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Button *ev)
2695 {
2696    _e_comp_wl_touch_cancel();
2697
2698    return ECORE_CALLBACK_PASS_ON;
2699 }
2700
2701 static Eina_Bool
2702 _e_comp_wl_cb_zone_display_state_change(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Zone_Display_State_Change *ev)
2703 {
2704    if (!ev) return ECORE_CALLBACK_PASS_ON;
2705
2706    E_Zone *zone = ev->zone;
2707
2708    E_OBJECT_CHECK_RETURN(zone, ECORE_CALLBACK_PASS_ON);
2709    E_OBJECT_TYPE_CHECK_RETURN(zone, E_ZONE_TYPE, ECORE_CALLBACK_PASS_ON);
2710
2711    if (!e_zone_is_displaying(zone))
2712      _e_comp_wl_touch_cancel();
2713
2714    return ECORE_CALLBACK_PASS_ON;
2715 }
2716
2717 static Eina_Bool
2718 _e_comp_wl_cb_client_rot_change_begin(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Client_Rotation_Change_Begin *ev)
2719 {
2720    E_Client *ec = ev->ec;
2721    E_Comp_Wl_Buffer_Viewport *vp;
2722
2723    if (!ec) return ECORE_CALLBACK_PASS_ON;
2724    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_PASS_ON;
2725    if (!ec->comp_data) return ECORE_CALLBACK_PASS_ON;
2726    if (e_comp_wl_subsurface_check(ec)) return ECORE_CALLBACK_PASS_ON;
2727    if (ec->e.state.rot.ang.next < 0) return ECORE_CALLBACK_PASS_ON;
2728
2729    vp = &ec->comp_data->scaler.buffer_viewport;
2730    vp->wait_for_transform_change = ((360 + ec->e.state.rot.ang.next - ec->e.state.rot.ang.curr) % 360) / 90;
2731
2732    DBG("ec(%p) wait_for_transform_change(%d)", ec, vp->wait_for_transform_change);
2733
2734    return ECORE_CALLBACK_PASS_ON;
2735 }
2736
2737 static Eina_Bool
2738 _e_comp_wl_cb_client_rot_change_cancel(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Client_Rotation_Change_Cancel *ev)
2739 {
2740    E_Client *ec = ev->ec;
2741    E_Comp_Wl_Buffer_Viewport *vp;
2742
2743    if (!ec) return ECORE_CALLBACK_PASS_ON;
2744    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_PASS_ON;
2745    if (!ec->comp_data) return ECORE_CALLBACK_PASS_ON;
2746    if (e_comp_wl_subsurface_check(ec)) return ECORE_CALLBACK_PASS_ON;
2747
2748    vp = &ec->comp_data->scaler.buffer_viewport;
2749    vp->wait_for_transform_change = 0;
2750
2751    DBG("ec(%p) wait_for_transform_change(%d) reset", ec, vp->wait_for_transform_change);
2752
2753    return ECORE_CALLBACK_PASS_ON;
2754 }
2755
2756 static Eina_Bool
2757 _e_comp_wl_cb_client_rot_change_end(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Client_Rotation_Change_End *ev EINA_UNUSED)
2758 {
2759    E_Client *focused_ec;
2760    int rotation;
2761
2762    focused_ec = e_client_focused_get();
2763    if (!focused_ec) return ECORE_CALLBACK_PASS_ON;
2764
2765    rotation = focused_ec->e.state.rot.ang.curr;
2766    e_pointer_rotation_set(e_comp->pointer, rotation);
2767
2768    return ECORE_CALLBACK_PASS_ON;
2769 }
2770
2771 static void
2772 _e_comp_wl_surface_state_size_update(E_Client *ec, E_Comp_Wl_Surface_State *state)
2773 {
2774    int prev_w, prev_h;
2775    Eina_Rectangle *window;
2776
2777    prev_w = state->bw;
2778    prev_h = state->bh;
2779
2780    if (!e_pixmap_size_get(ec->pixmap, &state->bw, &state->bh)) return;
2781
2782    if ((prev_w != state->bw) ||
2783        (prev_h != state->bh))
2784      {
2785         ec->changes.buf_size = EINA_TRUE;
2786      }
2787
2788    if (e_comp_object_frame_exists(ec->frame)) return;
2789    window = &ec->comp_data->shell.window;
2790    if ((!ec->borderless) && /* FIXME temporarily added this check code
2791                              * to prevent updating E_Client's size by frame */
2792        (window->x || window->y || window->w || window->h))
2793      {
2794         e_comp_object_frame_geometry_set(ec->frame,
2795                                          -window->x,
2796                                          (window->x + window->w) - state->bw,
2797                                          -window->y,
2798                                          (window->y + window->h) - state->bh);
2799      }
2800    else
2801      e_comp_object_frame_geometry_set(ec->frame, 0, 0, 0, 0);
2802 }
2803
2804 static void
2805 _e_comp_wl_surface_state_cb_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
2806 {
2807    E_Comp_Wl_Surface_State *state;
2808
2809    state =
2810      container_of(listener, E_Comp_Wl_Surface_State, buffer_destroy_listener);
2811    state->buffer = NULL;
2812 }
2813
2814 static void
2815 _e_comp_wl_surface_state_init(E_Comp_Wl_Surface_State *state, int w, int h)
2816 {
2817    state->new_attach = EINA_FALSE;
2818    state->buffer = NULL;
2819    state->buffer_destroy_listener.notify =
2820      _e_comp_wl_surface_state_cb_buffer_destroy;
2821    state->sx = state->sy = 0;
2822
2823    state->input = eina_tiler_new(w, h);
2824    eina_tiler_tile_size_set(state->input, 1, 1);
2825
2826    state->opaque = eina_tiler_new(w, h);
2827    eina_tiler_tile_size_set(state->opaque, 1, 1);
2828
2829    state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
2830    state->buffer_viewport.buffer.scale = 1;
2831    state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
2832    state->buffer_viewport.surface.width = -1;
2833    state->buffer_viewport.changed = 0;
2834
2835    e_presentation_time_container_init(&state->presentation_container);
2836 }
2837
2838 static void
2839 _e_comp_wl_surface_state_finish(E_Comp_Wl_Surface_State *state)
2840 {
2841    struct wl_resource *cb;
2842    Eina_Rectangle *dmg;
2843
2844    EINA_LIST_FREE(state->frames, cb)
2845      wl_resource_destroy(cb);
2846
2847    EINA_LIST_FREE(state->damages, dmg)
2848      eina_rectangle_free(dmg);
2849
2850    EINA_LIST_FREE(state->buffer_damages, dmg)
2851      eina_rectangle_free(dmg);
2852
2853    if (state->opaque) eina_tiler_free(state->opaque);
2854    state->opaque = NULL;
2855
2856    if (state->input) eina_tiler_free(state->input);
2857    state->input = NULL;
2858
2859    if (state->buffer) wl_list_remove(&state->buffer_destroy_listener.link);
2860    state->buffer = NULL;
2861
2862    e_presentation_time_container_finish(&state->presentation_container);
2863 }
2864
2865 static void
2866 _e_comp_wl_surface_state_buffer_set(E_Comp_Wl_Surface_State *state, E_Comp_Wl_Buffer *buffer)
2867 {
2868    if (state->buffer == buffer) return;
2869    if (state->buffer)
2870      wl_list_remove(&state->buffer_destroy_listener.link);
2871    state->buffer = buffer;
2872    if (state->buffer)
2873      wl_signal_add(&state->buffer->destroy_signal,
2874                    &state->buffer_destroy_listener);
2875 }
2876
2877 static void
2878 _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
2879 {
2880    Eina_Rectangle *dmg;
2881    Eina_Bool placed = EINA_TRUE;
2882    int x = 0, y = 0;
2883    int w, h;
2884    int nw, nh;
2885    E_Comp_Wl_Buffer *buffer;
2886    struct wl_resource *cb;
2887    Eina_List *l, *ll;
2888    E_Comp_Wl_Client_Data *cdata = e_client_cdata_get(ec);
2889    E_Comp_Wl_Buffer_Viewport *vp = &cdata->scaler.buffer_viewport;
2890    E_Zone *zone;
2891
2892    if (ec->ignored)
2893      {
2894         if ((ec->internal) ||
2895             (cdata->shell.surface && state->new_attach))
2896           {
2897              EC_CHANGED(ec);
2898              ec->new_client = 1;
2899              e_comp->new_clients++;
2900              ELOGF("COMP", "Unignore", ec);
2901              e_client_unignore(ec);
2902           }
2903      }
2904
2905    /* buffer transform */
2906    if (vp->buffer.transform != state->buffer_viewport.buffer.transform)
2907      {
2908         E_Output *eout;
2909         int transform_change = (4 + state->buffer_viewport.buffer.transform - vp->buffer.transform) & 0x3;
2910
2911         /* when buffer is transformed, we have to apply the new evas-map */
2912         state->buffer_viewport.changed = EINA_TRUE;
2913
2914         ELOGF("TRANSFORM", "buffer_transform changed: old(%d) new(%d)",
2915               ec,
2916               vp->buffer.transform, state->buffer_viewport.buffer.transform);
2917
2918         if (transform_change == vp->wait_for_transform_change)
2919           vp->wait_for_transform_change = 0;
2920
2921         // TODO: This logic has to move to e_comp_hwc or e_hwc_window and it is
2922         //       triggered by E_CLIENT_HOOK calls at those file.
2923         zone = e_comp_zone_find_by_ec(ec);
2924         if (zone)
2925           {
2926              eout = e_output_find(zone->output_id);
2927              if (eout && eout->hwc)
2928                {
2929                   if (e_hwc_policy_get(eout->hwc) == E_HWC_POLICY_PLANES)
2930                     {
2931                        if (e_comp_is_on_overlay(ec))
2932                          e_comp_hwc_client_end(ec, __FUNCTION__);
2933                     }
2934                }
2935           }
2936      }
2937
2938    /* assign a new buffer_vieport to cdata->scaler.buffer_viewport */
2939    cdata->scaler.buffer_viewport = state->buffer_viewport;
2940
2941    if (state->new_attach)
2942      {
2943         e_comp_wl_surface_attach(ec, state->buffer);
2944      }
2945
2946    /* emit a apply_viewport signal when the information of viewport and buffer is ready */
2947    wl_signal_emit(&cdata->apply_viewport_signal, &cdata->surface);
2948
2949    _e_comp_wl_surface_state_buffer_set(state, NULL);
2950
2951    if ((state->new_attach) ||
2952        (state->buffer_viewport.changed))
2953      {
2954         _e_comp_wl_surface_state_size_update(ec, state);
2955         e_comp_wl_map_size_cal_from_viewport(ec);
2956
2957         /* update the position */
2958         if (ec->changes.pos)
2959           {
2960              e_comp_object_frame_xy_unadjust(ec->frame,
2961                                              ec->x, ec->y,
2962                                              &x, &y);
2963           }
2964         else
2965           {
2966              x = ec->client.x;
2967              y = ec->client.y;
2968           }
2969
2970         if (ec->new_client) placed = ec->placed;
2971
2972         if (!ec->lock_client_size)
2973           {
2974              w = ec->w;
2975              h = ec->h;
2976
2977              ec->client.w = state->bw;
2978              ec->client.h = state->bh;
2979
2980              e_comp_object_frame_wh_adjust(ec->frame,
2981                                            ec->client.w, ec->client.h,
2982                                            &nw, &nh);
2983              e_client_size_set(ec, nw, nh);
2984
2985              if ((w != ec->w) || (h != ec->h))
2986                {
2987                   ec->changes.size = 1;
2988                   EC_CHANGED(ec);
2989                }
2990           }
2991
2992         if (ec->changes.buf_size)
2993           {
2994              ELOGF("COMP", "Buffer size is changed. size(%d,%d)", ec, state->bw, state->bh);
2995              _e_comp_wl_hook_call(E_COMP_WL_HOOK_BUFFER_SIZE_CHANGE, ec);
2996              ec->changes.buf_size = EINA_FALSE;
2997
2998              if (ec->move_after_resize)
2999                {
3000                   ELOGF("POSSIZE", "Unset move_after_resize. ec_geo(%d,%d,%dx%d)", ec, ec->x, ec->y, ec->w, ec->h);
3001                   ec->move_after_resize = EINA_FALSE;
3002                }
3003           }
3004      }
3005
3006    /* map or unmap ec */
3007    Eina_Bool pixmap_usable = e_pixmap_usable_get(ec->pixmap);
3008    Eina_Bool hide_by_request = e_client_hide_by_request_get(ec);
3009    if (!pixmap_usable || hide_by_request)
3010      {
3011         /* unmap ec */
3012         if (cdata->mapped)
3013           {
3014              if (hide_by_request)
3015                ELOGF("COMP", "Currenlty hide_by_request state. pixmap_usable:%d", ec, pixmap_usable);
3016
3017              if ((cdata->shell.surface) &&
3018                  (cdata->shell.unmap))
3019                {
3020                   ELOGF("COMP", "Try to unmap. Call shell.unmap.", ec);
3021                   if (ec->show_pending.count > 0)
3022                     {
3023                        ELOGF("E_CLIENT", "Reset show_pending!!! (count:%d, run:%d)", ec, ec->show_pending.count, ec->show_pending.running);
3024                        ec->show_pending.count = 0;
3025                        if (ec->show_pending.running)
3026                          {
3027                             // need to send visibility false;
3028                             ec->visibility.obscured = E_VISIBILITY_UNOBSCURED;
3029                             ELOGF("POL_VIS", "CLIENT VIS ON (fake).  argb:%d, opaque:%2d", ec, ec->argb, ec->visibility.opaque);
3030                             EC_CHANGED(ec);
3031                          }
3032                        ec->show_pending.running = EINA_FALSE;
3033                     }
3034                   cdata->shell.unmap(cdata->shell.surface);
3035                }
3036              else if ((ec->internal) ||
3037                       (e_comp_wl_subsurface_check(ec)) ||
3038                       (ec == e_comp_wl->drag_client))
3039                {
3040                   ELOGF("COMP", "Try to unmap. Hide window. internal:%d, sub:%p, drag:%d",
3041                         ec, ec->internal, cdata->sub.data, (ec == e_comp_wl->drag_client));
3042                   ec->visible = EINA_FALSE;
3043                   evas_object_hide(ec->frame);
3044                   cdata->mapped = 0;
3045                }
3046           }
3047
3048         if ((cdata->sub.below_obj) &&
3049             (evas_object_visible_get(cdata->sub.below_obj)))
3050           {
3051              evas_object_hide(cdata->sub.below_obj);
3052           }
3053      }
3054    else
3055      {
3056         /* map ec */
3057         if (!cdata->mapped)
3058           {
3059              if ((cdata->shell.surface) &&
3060                  (cdata->shell.map) &&
3061                  (!ec->ignored))
3062                {
3063                   ELOGF("COMP", "Try to map. Call shell.map.", ec);
3064                   cdata->shell.map(cdata->shell.surface);
3065                }
3066              else if ((ec->internal) ||
3067                       (e_comp_wl_subsurface_can_show(ec)) ||
3068                       (ec == e_comp_wl->drag_client))
3069                {
3070                   ELOGF("COMP", "Try to map. Show window. internal:%d, drag:%d",
3071                         ec, ec->internal, (ec == e_comp_wl->drag_client));
3072                   ec->visible = EINA_TRUE;
3073                   ec->ignored = 0;
3074                   evas_object_show(ec->frame);
3075                   cdata->mapped = 1;
3076                }
3077           }
3078
3079         if ((cdata->sub.below_obj) &&
3080             (!evas_object_visible_get(cdata->sub.below_obj)) &&
3081             (evas_object_visible_get(ec->frame)))
3082           {
3083              evas_object_show(cdata->sub.below_obj);
3084           }
3085      }
3086
3087    if ((state->new_attach) ||
3088        (state->buffer_viewport.changed))
3089      {
3090         if ((e_comp_wl->drag) &&
3091             (e_comp_wl->drag_client) &&
3092             (e_comp_wl->drag_client == ec))
3093           {
3094              e_drag_reference_point_set(e_comp_wl->drag, state->sx, state->sy);
3095
3096              e_drag_move(e_comp_wl->drag,
3097                          e_comp_wl->drag->x,
3098                          e_comp_wl->drag->y);
3099
3100              e_drag_resize(e_comp_wl->drag,
3101                            state->bw, state->bh);
3102           }
3103         else if ((cdata->shell.surface) &&
3104                  (cdata->shell.configure))
3105           {
3106              e_comp_wl_commit_sync_configure(ec);
3107           }
3108         else if (!e_client_video_hw_composition_check(ec))
3109           {
3110              e_client_util_move_resize_without_frame(ec, x, y, ec->w, ec->h);
3111           }
3112
3113         if (ec->new_client)
3114           {
3115              ec->placed = placed;
3116              ec->want_focus |= ec->icccm.accepts_focus && (!ec->override);
3117           }
3118      }
3119
3120    if (cdata->scaler.buffer_viewport.changed)
3121      e_comp_wl_map_apply(ec);
3122
3123    /* resize transform object */
3124    if (ec->transformed)
3125      e_client_transform_update(ec);
3126
3127    state->sx = 0;
3128    state->sy = 0;
3129    state->new_attach = EINA_FALSE;
3130
3131    EINA_LIST_FOREACH_SAFE(cdata->frames, l, ll, cb)
3132      {
3133         wl_callback_send_done(cb, (unsigned int)(ecore_loop_time_get() * 1000));
3134         wl_resource_destroy(cb);
3135      }
3136
3137    /* insert state frame callbacks into comp_data->frames
3138     * NB: This clears state->frames list */
3139    cdata->frames = eina_list_merge(cdata->frames,
3140                                            state->frames);
3141    state->frames = NULL;
3142
3143    e_presentation_time_container_feedback_discard(&cdata->presentation_container);
3144    e_presentation_time_container_feedback_merge(&cdata->presentation_container,
3145                                                 &state->presentation_container);
3146
3147    buffer = e_pixmap_resource_get(ec->pixmap);
3148
3149    /* put state damages into surface */
3150    if (ec->frame)
3151      {
3152         /* FIXME: workaround for bad wayland egl driver which doesn't send damage request */
3153         if (!eina_list_count(state->damages) && !eina_list_count(state->buffer_damages))
3154           {
3155              if ((cdata->buffer_ref.buffer) &&
3156                  ((cdata->buffer_ref.buffer->type == E_COMP_WL_BUFFER_TYPE_NATIVE) ||
3157                   (cdata->buffer_ref.buffer->type == E_COMP_WL_BUFFER_TYPE_TBM)))
3158                {
3159                   e_comp_object_damage(ec->frame,
3160                                        0, 0,
3161                                        cdata->buffer_ref.buffer->w,
3162                                        cdata->buffer_ref.buffer->h);
3163                }
3164           }
3165         else
3166           {
3167              Eina_List *damages = NULL;
3168
3169              if (buffer)
3170                _e_comp_wl_buffer_damage_set(buffer, state->buffer_damages);
3171
3172              if (eina_list_count(state->buffer_damages))
3173                {
3174                   EINA_LIST_FREE(state->buffer_damages, dmg)
3175                     {
3176                        if (buffer)
3177                          e_comp_wl_rect_convert_inverse(buffer->w, buffer->h,
3178                                                         e_comp_wl_output_buffer_transform_get(ec),
3179                                                         vp->buffer.scale,
3180                                                         dmg->x, dmg->y, dmg->w, dmg->h,
3181                                                         &dmg->x, &dmg->y, &dmg->w, &dmg->h);
3182                        damages = eina_list_append(damages, dmg);
3183                     }
3184                }
3185
3186              EINA_LIST_FREE(state->damages, dmg)
3187                damages = eina_list_append(damages, dmg);
3188
3189              EINA_LIST_FREE(damages, dmg)
3190                {
3191                   /* not creating damage for ec that shows a underlay video */
3192                   if (state->buffer_viewport.changed ||
3193                       !e_comp->wl_comp_data->available_hw_accel.underlay ||
3194                       !buffer || buffer->type != E_COMP_WL_BUFFER_TYPE_VIDEO)
3195                     e_comp_object_damage(ec->frame, dmg->x, dmg->y, dmg->w, dmg->h);
3196
3197                   eina_rectangle_free(dmg);
3198                }
3199           }
3200      }
3201
3202    /* put state opaque into surface */
3203    e_pixmap_image_opaque_set(ec->pixmap, 0, 0, 0, 0);
3204    if (state->opaque)
3205      {
3206         Eina_Rectangle *rect;
3207         Eina_Iterator *itr;
3208
3209         itr = eina_tiler_iterator_new(state->opaque);
3210         EINA_ITERATOR_FOREACH(itr, rect)
3211           {
3212              Eina_Rectangle r;
3213
3214              EINA_RECTANGLE_SET(&r, rect->x, rect->y, rect->w, rect->h);
3215              E_RECTS_CLIP_TO_RECT(r.x, r.y, r.w, r.h, 0, 0, state->bw, state->bh);
3216              e_pixmap_image_opaque_set(ec->pixmap, r.x, r.y, r.w, r.h);
3217              break;
3218           }
3219
3220         eina_iterator_free(itr);
3221      }
3222
3223    /* put state input into surface */
3224    if ((state->input) &&
3225        (!eina_tiler_empty(state->input)) &&
3226        ec->first_mapped)
3227      {
3228         Eina_Tiler *src, *tmp;
3229         int sw = ec->w;
3230         int sh = ec->h;
3231
3232         tmp = eina_tiler_new(sw, sh);
3233         eina_tiler_tile_size_set(tmp, 1, 1);
3234
3235         eina_tiler_rect_add(tmp,
3236                             &(Eina_Rectangle){0, 0, sw, sh});
3237
3238         if ((src = eina_tiler_intersection(state->input, tmp)))
3239           {
3240              Eina_Rectangle *rect;
3241              Eina_Iterator *itr;
3242
3243              e_comp_object_input_objs_del(ec->frame);
3244              itr = eina_tiler_iterator_new(src);
3245              EINA_ITERATOR_FOREACH(itr, rect)
3246                {
3247                   ELOGF("COMP", "Set Input Area x:%d, y:%d, w:%d, h:%d, ec(%dx%d), state(%dx%d)",
3248                         ec, rect->x, rect->y, rect->w, rect->h,
3249                         ec->w, ec->h, state->bw, state->bh);
3250                   e_comp_object_input_area_set(ec->frame,
3251                                                rect->x, rect->y,
3252                                                rect->w, rect->h);
3253                }
3254
3255              eina_iterator_free(itr);
3256              eina_tiler_free(src);
3257           }
3258         else
3259           e_comp_object_input_area_set(ec->frame, 0, 0, ec->w, ec->h);
3260
3261         eina_tiler_free(tmp);
3262
3263         /* clear input tiler */
3264         eina_tiler_clear(state->input);
3265      }
3266
3267    e_comp_wl_subsurface_check_below_bg_rectangle(ec);
3268
3269    if ((cdata->video_client) &&
3270        ((buffer) &&
3271         (buffer->type == E_COMP_WL_BUFFER_TYPE_VIDEO)) &&
3272        (e_comp->wl_comp_data->available_hw_accel.underlay))
3273      {
3274         e_pixmap_image_clear(ec->pixmap, 1);
3275      }
3276
3277    state->buffer_viewport.changed = 0;
3278
3279    wl_signal_emit(&cdata->state_commit_signal, &cdata->surface);
3280
3281    if (buffer &&
3282        ec->exp_iconify.buffer_flush &&
3283        e_policy_visibility_client_is_iconic(ec))
3284      {
3285         e_pixmap_buffer_clear(ec->pixmap, EINA_FALSE);
3286      }
3287 }
3288
3289 static void
3290 _e_comp_wl_surface_render_stop(E_Client *ec)
3291 {
3292    /* FIXME: this may be fine after e_pixmap can create textures for wl clients? */
3293    //if ((!ec->internal) && (!e_comp_gl_get()))
3294      ec->dead = 1;
3295
3296    /* check if internal animation is running */
3297    if (e_comp_object_is_animating(ec->frame)) return;
3298    /* check if external animation is running */
3299    if (evas_object_data_get(ec->frame, "effect_running")) return;
3300
3301    evas_object_hide(ec->frame);
3302 }
3303
3304 EINTERN void
3305 e_comp_wl_client_surface_finish(E_Client *ec)
3306 {
3307    struct wl_resource *res, *surface;
3308    struct wl_client *surface_client = NULL;
3309    Eina_List *l, *ll;
3310
3311    surface = e_comp_wl_client_surface_get(ec);
3312
3313    if (surface)
3314      surface_client = wl_resource_get_client(surface);
3315
3316    if (surface_client &&
3317        (ec == e_client_focused_get()))
3318      {
3319         g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
3320         EINA_LIST_FOREACH_SAFE(e_comp_wl->kbd.focused, l, ll, res)
3321           {
3322              if (wl_resource_get_client(res) ==
3323                  surface_client)
3324                e_comp_wl->kbd.focused =
3325                   eina_list_remove_list(e_comp_wl->kbd.focused, l);
3326
3327           }
3328
3329         g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
3330      }
3331
3332    e_comp_wl_client_surface_set(ec, NULL);
3333
3334    ec->comp_data->wl_surface = NULL;
3335    e_pixmap_win_id_del(ec->pixmap);
3336
3337    _e_comp_wl_surface_render_stop(ec);
3338    e_object_del(E_OBJECT(ec));
3339 }
3340
3341 static void
3342 _e_comp_wl_pname_get(pid_t pid, char *name, int size)
3343 {
3344    if (!name) return;
3345
3346    FILE *h;
3347    char proc[512], pname[512];
3348    size_t len;
3349
3350    snprintf(proc, 512,"/proc/%d/cmdline", pid);
3351
3352    h = fopen(proc, "r");
3353    if (h)
3354      {
3355         len = fread(pname, sizeof(char), 512, h);
3356         if (len > 0)
3357           pname[len - 1] = '\0';
3358         else
3359           strncpy(pname, "NO NAME", sizeof(pname));
3360
3361         fclose(h);
3362      }
3363    else
3364      {
3365         strncpy(pname, "NO NAME", sizeof(pname));
3366      }
3367
3368    strncpy(name, pname, size);
3369 }
3370
3371 static void
3372 _e_comp_wl_pname_print(pid_t pid)
3373 {
3374    FILE *h;
3375    char proc[512], pname[512];
3376    size_t len;
3377
3378    snprintf(proc, 512,"/proc/%d/cmdline", pid);
3379
3380    h = fopen(proc, "r");
3381    if (!h) return;
3382
3383    len = fread(pname, sizeof(char), 512, h);
3384    if (len > 0)
3385      pname[len - 1] = '\0';
3386    else
3387      strncpy(pname, "NO NAME", sizeof(pname));
3388
3389    fclose(h);
3390
3391    ELOGF("COMP", "         |%s", NULL, pname);
3392 }
3393
3394
3395 static void
3396 _e_comp_wl_connected_client_cb_destroy(struct wl_listener *listener, void *data)
3397 {
3398    struct wl_client *client = data;
3399    E_Comp_Connected_Client_Info *cinfo;
3400    E_Appinfo *eai;
3401
3402    cinfo = wl_container_of(listener, cinfo, destroy);
3403
3404    ELOGF("WL_CLIENT", "DESTROY  |client:%8p|%d|%d|%d",
3405          NULL, client, cinfo->pid, cinfo->uid, cinfo->gid);
3406
3407    eai = e_appinfo_find_with_pid(cinfo->pid);
3408    if (e_appinfo_owner_get(eai) == E_APPINFO_OWNER_SERVER)
3409      e_appinfo_del(eai);
3410
3411    e_comp->connected_clients = eina_list_remove(e_comp->connected_clients, cinfo);
3412
3413    wl_list_remove(&cinfo->destroy.link);
3414    eina_stringshare_del(cinfo->name);
3415    free(cinfo);
3416 }
3417
3418 static void
3419 _e_comp_wl_connected_client_create(struct wl_client *client, char *name, pid_t pid, uid_t uid, gid_t gid)
3420 {
3421    E_Comp_Connected_Client_Info *cinfo;
3422
3423    cinfo = E_NEW(E_Comp_Connected_Client_Info, 1);
3424    EINA_SAFETY_ON_NULL_RETURN(cinfo);
3425
3426    cinfo->name = eina_stringshare_add(name);
3427    cinfo->pid = pid;
3428    cinfo->uid = uid;
3429    cinfo->gid = gid;
3430    cinfo->destroy.notify = _e_comp_wl_connected_client_cb_destroy;
3431    wl_client_add_destroy_listener(client, &cinfo->destroy);
3432    e_comp->connected_clients = eina_list_append(e_comp->connected_clients, cinfo);
3433
3434    _e_comp_wl_pid_hook_call(E_COMP_WL_PID_HOOK_CONNECTED_CLIENT_CREATE, pid);
3435 }
3436
3437 static void
3438 _e_comp_wl_client_cb_focus_set(void *data EINA_UNUSED, E_Client *ec)
3439 {
3440    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3441
3442    /* send configure */
3443    if (ec->comp_data->shell.configure_send)
3444      {
3445         if (ec->comp_data->shell.surface)
3446           _e_comp_wl_configure_send(ec, 0, 0);
3447      }
3448
3449    e_comp_wl->kbd.focus = ec->comp_data->surface;
3450 }
3451
3452 static void
3453 _e_comp_wl_client_cb_focus_unset(void *data EINA_UNUSED, E_Client *ec)
3454 {
3455    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3456
3457    /* send configure */
3458    if (ec->comp_data->shell.configure_send)
3459      {
3460         if (ec->comp_data->shell.surface)
3461           _e_comp_wl_configure_send(ec, 0, 0);
3462      }
3463
3464    _e_comp_wl_focus_check();
3465
3466    if (e_comp_wl->kbd.focus == ec->comp_data->surface)
3467      e_comp_wl->kbd.focus = NULL;
3468 }
3469
3470 static void
3471 _e_comp_wl_client_cb_resize_begin(void *data EINA_UNUSED, E_Client *ec)
3472 {
3473    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3474    if (ec->keyboard_resizing) return;
3475
3476    /* do nothing currently */
3477    ;
3478 }
3479
3480 static void
3481 _e_comp_wl_client_cb_resize_end(void *data EINA_UNUSED, E_Client *ec)
3482 {
3483    if (e_object_is_del(E_OBJECT(ec))) return;
3484    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3485
3486    e_comp_wl->resize.edges = 0;
3487    e_comp_wl->resize.resource = NULL;
3488
3489    if (ec->pending_resize)
3490      {
3491         ec->changes.pos = EINA_TRUE;
3492         ec->changes.size = EINA_TRUE;
3493         EC_CHANGED(ec);
3494      }
3495
3496    E_FREE_LIST(ec->pending_resize, free);
3497 }
3498
3499 static void
3500 _e_comp_wl_client_cb_move_end(void *data EINA_UNUSED, E_Client *ec)
3501 {
3502    if (e_object_is_del(E_OBJECT(ec))) return;
3503    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3504 }
3505
3506 static void
3507 _e_comp_wl_output_info_send(E_Comp_Wl_Output *output, struct wl_resource *resource, pid_t pid, int res_w, int res_h)
3508 {
3509    int phys_w, phys_h;
3510    int ratio_w, ratio_h;
3511
3512    phys_w = output->phys_width;
3513    phys_h = output->phys_height;
3514
3515    if (e_config->configured_output_resolution.use)
3516      {
3517         // change the configured physical size(mm) of the output
3518         if (output->w != res_w)
3519           {
3520              ratio_w = res_w / output->w;
3521              phys_w = (int)((float)output->phys_width * (float)ratio_w);
3522           }
3523
3524         if (output->h != res_h)
3525           {
3526              ratio_h = res_h / output->h;
3527              phys_h = (int)((float)output->phys_height * (float)ratio_h);
3528           }
3529
3530         ELOGF("COMP_WL", "\tSend Configured Output (pid:%d)", NULL, pid);
3531      }
3532
3533    ELOGF("COMP_WL", "\t    Output Resolution: res(%d, %d) phy_size(%d, %d) (pid:%d).",
3534          NULL, res_w, res_h, phys_w, phys_h, pid);
3535
3536    if (wl_resource_get_version(resource) >= WL_OUTPUT_SCALE_SINCE_VERSION)
3537      wl_output_send_scale(resource, output->scale);
3538
3539    wl_output_send_geometry(resource, output->x, output->y,
3540                            phys_w, phys_h,
3541                            output->subpixel, output->make ?: "",
3542                            output->model ?: "", output->transform);
3543
3544    wl_output_send_mode(resource,  WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED,
3545                        res_w, res_h, output->refresh);
3546
3547    if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
3548      wl_output_send_done(resource);
3549 }
3550
3551 static void
3552 _e_comp_wl_cb_output_unbind(struct wl_resource *resource)
3553 {
3554    E_Comp_Wl_Output *output;
3555
3556    if (!(output = wl_resource_get_user_data(resource))) return;
3557
3558    output->resources = eina_list_remove(output->resources, resource);
3559 }
3560
3561 static void
3562 _e_comp_wl_cb_output_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3563 {
3564    E_Comp_Wl_Output *output;
3565    struct wl_resource *resource;
3566    E_Appinfo *eai = NULL;
3567    pid_t pid = 0;
3568    int res_w, res_h;
3569
3570    if (!(output = data)) return;
3571
3572    resource =
3573      wl_resource_create(client, &wl_output_interface, version, id);
3574    if (!resource)
3575      {
3576         wl_client_post_no_memory(client);
3577         return;
3578      }
3579
3580    ELOGF("COMP_WL", "Bound Output: %s", NULL, output->id);
3581    ELOGF("COMP_WL", "\tOutput Geom: %d %d %d %d", NULL, output->x, output->y, output->w, output->h);
3582
3583    output->resources = eina_list_append(output->resources, resource);
3584
3585    wl_resource_set_implementation(resource, NULL, output,
3586                                   _e_comp_wl_cb_output_unbind);
3587    wl_resource_set_user_data(resource, output);
3588
3589    // set the configured_output_resolution as a resolution of the wl_output if the use is set.
3590    if (e_config->configured_output_resolution.use)
3591      {
3592         wl_client_get_credentials(client, &pid, NULL, NULL);
3593         if (pid <= 0)
3594           {
3595              res_w = e_config->configured_output_resolution.w;
3596              res_h = e_config->configured_output_resolution.h;
3597              goto send_info;
3598           }
3599
3600         eai = e_appinfo_find_with_pid(pid);
3601         if (!eai)
3602           {
3603              res_w = e_config->configured_output_resolution.w;
3604              res_h = e_config->configured_output_resolution.h;
3605              goto send_info;
3606           }
3607
3608         if (!e_appinfo_base_output_resolution_get(eai, &res_w, &res_h))
3609           {
3610              res_w = e_config->configured_output_resolution.w;
3611              res_h = e_config->configured_output_resolution.h;
3612              goto send_info;
3613           }
3614
3615         ELOGF("COMP_WL", "Get base_screen_resolution. (pid:%d).", NULL, pid);
3616      }
3617    else
3618      {
3619         res_w = output->w;
3620         res_h = output->h;
3621      }
3622
3623 send_info:
3624    _e_comp_wl_output_info_send(output, resource, pid, res_w, res_h);
3625 }
3626
3627 static void
3628 _e_comp_wl_gl_init(void *data EINA_UNUSED)
3629 {
3630    Evas *evas = NULL;
3631    Evas_GL *evasgl = NULL;
3632    Evas_GL_API *glapi = NULL;
3633    Evas_GL_Context *ctx = NULL;
3634    Evas_GL_Surface *sfc = NULL;
3635    Evas_GL_Config *cfg = NULL;
3636    Eina_Bool res;
3637    E_Comp_Wl_Evas_Gl *evas_gl = NULL;
3638
3639    if (!e_comp_gl_get()) return;
3640
3641    evas_gl = E_NEW(E_Comp_Wl_Evas_Gl, 1);
3642    EINA_SAFETY_ON_NULL_RETURN(evas_gl);
3643
3644    /* create dummy evas gl to bind wayland display of enlightenment to egl display */
3645    e_main_ts_begin("\tE_Comp_Wl_GL Init");
3646
3647    /* if wl_drm module doesn't call e_comp_canvas_init yet,
3648     * then we should get evas from ecore_evas.
3649     */
3650    if (e_comp->evas)
3651      evas = e_comp->evas;
3652    else
3653      evas = ecore_evas_get(e_comp->ee);
3654
3655    evasgl = evas_gl_new(evas);
3656    EINA_SAFETY_ON_NULL_GOTO(evasgl, err);
3657
3658    glapi = evas_gl_api_get(evasgl);
3659    EINA_SAFETY_ON_NULL_GOTO(glapi, err);
3660    EINA_SAFETY_ON_NULL_GOTO(glapi->evasglBindWaylandDisplay, err);
3661
3662    cfg = evas_gl_config_new();
3663    EINA_SAFETY_ON_NULL_GOTO(cfg, err);
3664
3665    sfc = evas_gl_surface_create(evasgl, cfg, 1, 1);
3666    EINA_SAFETY_ON_NULL_GOTO(sfc, err);
3667
3668    ctx = evas_gl_context_create(evasgl, NULL);
3669    EINA_SAFETY_ON_NULL_GOTO(ctx, err);
3670
3671    res = evas_gl_make_current(evasgl, sfc, ctx);
3672    EINA_SAFETY_ON_FALSE_GOTO(res, err);
3673
3674    res = glapi->evasglBindWaylandDisplay(evasgl, e_comp_wl->wl.disp);
3675    EINA_SAFETY_ON_FALSE_GOTO(res, err);
3676
3677    evas_gl_config_free(cfg);
3678
3679    evas_gl->gl = evasgl;
3680    evas_gl->glapi = glapi;
3681    evas_gl->glsfc = sfc;
3682    evas_gl->glctx = ctx;
3683
3684    e_comp_wl->evas_gl = evas_gl;
3685
3686    /* for native surface */
3687    e_comp->gl = 1;
3688
3689    e_main_ts_end("\tE_Comp_Wl_GL Init Done");
3690
3691    return;
3692
3693 err:
3694    evas_gl_config_free(cfg);
3695    evas_gl_make_current(evasgl, NULL, NULL);
3696    evas_gl_context_destroy(evasgl, ctx);
3697    evas_gl_surface_destroy(evasgl, sfc);
3698    evas_gl_free(evasgl);
3699    free(evas_gl);
3700 }
3701
3702 // FIXME
3703 #if 0
3704 static void
3705 _e_comp_wl_gl_popup_cb_close(void *data,
3706                              Evas_Object *obj EINA_UNUSED,
3707                              void *event_info EINA_UNUSED)
3708 {
3709    evas_object_del(data);
3710 }
3711
3712 static void
3713 _e_comp_wl_gl_popup_cb_focus(void *data,
3714                              Evas_Object *obj EINA_UNUSED,
3715                              void *event_info EINA_UNUSED)
3716 {
3717    elm_object_focus_set(data, EINA_TRUE);
3718 }
3719 #endif
3720
3721 static Eina_Bool
3722 _e_comp_wl_gl_idle(void *data)
3723 {
3724    if (!e_comp->gl)
3725      {
3726         /* show warning window to notify failure of gl init */
3727         // TODO: yigl
3728 #if 0
3729         Evas_Object *win, *bg, *popup, *btn;
3730
3731         win = elm_win_add(NULL, "compositor warning", ELM_WIN_BASIC);
3732         elm_win_title_set(win, "Compositor Warning");
3733         elm_win_autodel_set(win, EINA_TRUE);
3734         elm_win_borderless_set(win, EINA_TRUE);
3735         elm_win_role_set(win, "notification-low");
3736         elm_win_alpha_set(win, EINA_TRUE);
3737
3738         bg = evas_object_rectangle_add(evas_object_evas_get(win));
3739         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3740         elm_win_resize_object_add(win, bg);
3741         evas_object_color_set(bg, 125, 125, 125, 125);
3742         evas_object_show(bg);
3743
3744         popup = elm_popup_add(win);
3745         elm_object_text_set(popup,
3746                             _( "Your screen does not support OpenGL.<br>"
3747                                "Falling back to software engine."));
3748         elm_object_part_text_set(popup, "title,text", "Compositor Warning");
3749
3750         btn = elm_button_add(popup);
3751         elm_object_text_set(btn, "Close");
3752         elm_object_part_content_set(popup, "button1", btn);
3753         evas_object_show(btn);
3754
3755         evas_object_smart_callback_add(win, "focus,in", _e_comp_wl_gl_popup_cb_focus, popup);
3756         evas_object_smart_callback_add(btn, "unpressed", _e_comp_wl_gl_popup_cb_close, win);
3757
3758         evas_object_show(popup);
3759         evas_object_show(win);
3760 #endif
3761      }
3762
3763    return ECORE_CALLBACK_CANCEL;
3764 }
3765
3766 static void
3767 _e_comp_wl_cb_client_created(struct wl_listener *listener, void *data)
3768 {
3769    struct wl_client *client = data;
3770    pid_t pid = 0;
3771    uid_t uid = 0;
3772    gid_t gid = 0;
3773    char name[512];
3774
3775    wl_client_get_credentials(client, &pid, &uid, &gid);
3776
3777    ELOGF("COMP", "WL_CLIENT|client:%8p|%d|%d|%d", NULL, client, pid, uid, gid);
3778
3779    _e_comp_wl_pname_print(pid);
3780    _e_comp_wl_pname_get(pid, name, sizeof(name));
3781    _e_comp_wl_connected_client_create(client, name, pid, uid, gid);
3782 }
3783
3784 static void
3785 _e_comp_wl_ds_log_handler(enum ds_log_level level, const char *fmt, va_list args)
3786 {
3787    char buf[1024] = {0, };
3788
3789    vsnprintf(buf, 1024, fmt, args);
3790    switch (level)
3791      {
3792       case DS_DBG:
3793          DBG("[libds] %s", buf);
3794          break;
3795       case DS_INF:
3796          INF("[libds] %s", buf);
3797          break;
3798       case DS_ERR:
3799          ERR("[libds] %s", buf);
3800          break;
3801       default:
3802          break;
3803      }
3804 }
3805
3806 static Eina_Bool
3807 _e_comp_wl_display_create(void)
3808 {
3809    E_Comp_Data *comp;
3810    E_Comp_Wl_Data *wl_cdata;
3811    const char *name;
3812    int fd = 0;
3813    Eina_Bool res;
3814
3815    /* create new compositor data */
3816    if (!(comp = E_NEW(E_Comp_Data, 1)))
3817      {
3818         ERR("Could not create compositor data: %m");
3819         return EINA_FALSE;
3820      }
3821    wl_cdata = &comp->base;
3822
3823    /* set compositor wayland data */
3824    e_comp_wl = e_comp->wl_comp_data = wl_cdata;
3825
3826    g_mutex_init(&flush_mutex);
3827
3828    /* try to create a wayland display */
3829    if (!(wl_cdata->wl.disp = wl_display_create()))
3830      {
3831         ERR("Could not create a Wayland display: %m");
3832         goto disp_err;
3833      }
3834
3835    /* try to setup wayland socket */
3836    if (!(name = wl_display_add_socket_auto(wl_cdata->wl.disp)))
3837      {
3838         ERR("Could not create Wayland display socket: %m");
3839         PRCTL("[Winsys] Could not create Wayland display socket: /run/wayland-0");
3840         goto sock_err;
3841      }
3842
3843    res = e_comp_socket_init(name);
3844    EINA_SAFETY_ON_FALSE_GOTO(res, sock_err);
3845    PRCTL("[Winsys] change permission and create sym link for %s", name);
3846
3847    /* set wayland display environment variable */
3848    e_env_set("WAYLAND_DISPLAY", name);
3849
3850    /* wl_cdata->output.transform = WL_OUTPUT_TRANSFORM_NORMAL; */
3851    /* wl_cdata->output.scale = e_scale; */
3852
3853    ds_log_init(DS_DBG, _e_comp_wl_ds_log_handler);
3854
3855    if (!e_compositor_init(wl_cdata->wl.disp))
3856      {
3857         ERR("Failed to initialize compositor");
3858         goto comp_err;
3859      }
3860
3861    comp->client_created.notify = _e_comp_wl_cb_client_created;
3862    wl_display_add_client_created_listener(comp->base.wl.disp, &comp->client_created);
3863
3864    if (!e_comp_wl_subsurfaces_init())
3865      {
3866         ERR("Failed to init_subsurfaces");
3867         goto subsurfaces_err;
3868      }
3869
3870    /* initialize shm mechanism */
3871    wl_display_init_shm(wl_cdata->wl.disp);
3872
3873    /* _e_comp_wl_cb_randr_change(NULL, 0, NULL); */
3874
3875    /* try to init data manager */
3876    if (!e_comp_wl_data_manager_init())
3877      {
3878         ERR("Could not initialize data manager");
3879         goto data_err;
3880      }
3881
3882    /* try to init input */
3883    if (!e_comp_wl_input_init())
3884      {
3885         ERR("Could not initialize input");
3886         goto input_err;
3887      }
3888
3889    if (e_comp_gl_get())
3890      _e_comp_wl_gl_init(NULL);
3891
3892    /* get the wayland display loop */
3893    wl_cdata->wl.loop = wl_display_get_event_loop(wl_cdata->wl.disp);
3894
3895    /* get the file descriptor of the wayland event loop */
3896    fd = wl_event_loop_get_fd(wl_cdata->wl.loop);
3897
3898    /* create a listener for wayland main loop events */
3899    wl_cdata->fd_hdlr =
3900      ecore_main_fd_handler_add(fd, (ECORE_FD_READ | ECORE_FD_ERROR),
3901                                _e_comp_wl_cb_read, wl_cdata, NULL, NULL);
3902    ecore_main_fd_handler_prepare_callback_set(wl_cdata->fd_hdlr,
3903                                               _e_comp_wl_cb_prepare, wl_cdata);
3904
3905    return EINA_TRUE;
3906
3907 input_err:
3908    e_comp_wl_data_manager_shutdown();
3909 data_err:
3910    e_comp_wl_subsurfaces_shutdown();
3911 subsurfaces_err:
3912    wl_list_remove(&comp->client_created.link);
3913 comp_err:
3914    e_env_unset("WAYLAND_DISPLAY");
3915 sock_err:
3916    wl_display_destroy(wl_cdata->wl.disp);
3917 disp_err:
3918    g_mutex_clear(&flush_mutex);
3919    free(comp);
3920    return EINA_FALSE;
3921 }
3922
3923 static void
3924 _e_comp_wl_gl_shutdown(void)
3925 {
3926    if (!e_comp_wl->evas_gl) return;
3927
3928    e_comp_wl->evas_gl->glapi->evasglUnbindWaylandDisplay(e_comp_wl->evas_gl->gl, e_comp_wl->wl.disp);
3929
3930    evas_gl_make_current(e_comp_wl->evas_gl->gl, NULL, NULL);
3931    evas_gl_context_destroy(e_comp_wl->evas_gl->gl, e_comp_wl->evas_gl->glctx);
3932    evas_gl_surface_destroy(e_comp_wl->evas_gl->gl, e_comp_wl->evas_gl->glsfc);
3933    evas_gl_free(e_comp_wl->evas_gl->gl);
3934
3935    E_FREE(e_comp_wl->evas_gl);
3936 }
3937
3938 /* public functions */
3939
3940 /**
3941  * Creates and initializes a Wayland compositor with ecore.
3942  * Registers callback handlers for keyboard and mouse activity
3943  * and other client events.
3944  *
3945  * @returns true on success, false if initialization failed.
3946  */
3947 EINTERN Eina_Bool
3948 e_comp_wl_init(void)
3949 {
3950    TRACE_DS_BEGIN(COMP_WL:INIT);
3951
3952    /* try to create a wayland compositor */
3953    if (!_e_comp_wl_display_create())
3954      {
3955         e_error_message_show(_("Enlightenment cannot create a Wayland Compositor!\n"));
3956         TRACE_DS_END();
3957         return EINA_FALSE;
3958      }
3959
3960    e_comp_wl_shell_init();
3961    e_wtz_shell_init();
3962 #ifdef HAVE_WAYLAND_TBM
3963    e_comp_wl_tbm_init();
3964 #endif
3965    e_comp_wl_remote_surface_init();
3966
3967    e_pixmap_init();
3968
3969    e_comp_wl_screenshooter_init();
3970
3971    if (!e_comp_wl_video_init())
3972      ELOGF("COMP", "Failed to initialize the e_comp_wl_video", NULL);
3973
3974    e_comp_wl_viewport_init();
3975    e_comp_wl_capture_init();
3976    e_comp_wl_renderer_init();
3977    _e_comp_wl_move_resize_init();
3978    e_presentation_time_init();
3979    ds_single_pixel_buffer_manager_v1_create(e_comp_wl->wl.disp);
3980    e_blender_init();
3981
3982    if (!e_foreign_global_init(e_comp_wl->wl.disp))
3983      ELOGF("COMP", "Failed to initialize the e_foreign global", NULL);
3984
3985    /* add event handlers to catch E events */
3986    E_LIST_HANDLER_APPEND(handlers, E_EVENT_SCREEN_CHANGE,            _e_comp_wl_cb_randr_change,        NULL);
3987    E_LIST_HANDLER_APPEND(handlers, E_EVENT_COMP_OBJECT_ADD,         _e_comp_wl_cb_comp_object_add,     NULL);
3988    E_LIST_HANDLER_APPEND(handlers, ECORE_EVENT_MOUSE_MOVE,          _e_comp_wl_cb_mouse_move,          NULL);
3989    E_LIST_HANDLER_APPEND(handlers, ECORE_EVENT_MOUSE_RELATIVE_MOVE,  _e_comp_wl_cb_mouse_relative_move, NULL);
3990    E_LIST_HANDLER_PREPEND(handlers, ECORE_EVENT_MOUSE_BUTTON_CANCEL, _e_comp_wl_cb_mouse_button_cancel, NULL);
3991    E_LIST_HANDLER_APPEND(handlers, E_EVENT_ZONE_DISPLAY_STATE_CHANGE, _e_comp_wl_cb_zone_display_state_change, NULL);
3992    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_BEGIN, _e_comp_wl_cb_client_rot_change_begin, NULL);
3993    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_CANCEL, _e_comp_wl_cb_client_rot_change_cancel, NULL);
3994    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_END, _e_comp_wl_cb_client_rot_change_end, NULL);
3995
3996    /* add hooks to catch e_client events */
3997    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_FOCUS_SET,    _e_comp_wl_client_cb_focus_set,    NULL);
3998    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_FOCUS_UNSET,  _e_comp_wl_client_cb_focus_unset,  NULL);
3999    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_RESIZE_BEGIN, _e_comp_wl_client_cb_resize_begin, NULL);
4000    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_RESIZE_END,   _e_comp_wl_client_cb_resize_end,   NULL);
4001    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_MOVE_END,     _e_comp_wl_client_cb_move_end,     NULL);
4002
4003    e_comp_wl->idle_exiter = ecore_idle_exiter_add(_e_comp_wl_cb_idle_exiter, NULL);
4004
4005    TRACE_DS_END();
4006    return EINA_TRUE;
4007 }
4008
4009 EINTERN void
4010 e_comp_wl_deferred_job(void)
4011 {
4012    ecore_idle_enterer_add(_e_comp_wl_gl_idle, NULL);
4013 }
4014
4015 /* internal functions */
4016 EINTERN void
4017 e_comp_wl_shutdown(void)
4018 {
4019    E_Comp_Data *comp = (E_Comp_Data *)e_comp_wl;
4020
4021    ecore_idle_exiter_del(e_comp_wl->idle_exiter);
4022
4023    e_comp_wl_subsurfaces_shutdown();
4024    /* free handlers */
4025    E_FREE_LIST(handlers, ecore_event_handler_del);
4026    E_FREE_LIST(hooks, e_client_hook_del);
4027    _e_comp_wl_gl_shutdown();
4028
4029    e_blender_shutdown();
4030    e_presentation_time_shutdown();
4031    e_comp_wl_renderer_shutdown();
4032    e_comp_wl_capture_shutdown();
4033    e_comp_wl_viewport_shutdown();
4034    e_comp_wl_video_shutdown();
4035    e_comp_wl_screenshooter_shutdown();
4036
4037    e_comp_wl_remote_surface_shutdown();
4038
4039    e_pixmap_shutdown();
4040
4041    e_wtz_shell_shutdown();
4042    e_comp_wl_shell_shutdown();
4043    e_comp_wl_input_shutdown();
4044
4045    wl_list_remove(&comp->client_created.link);
4046
4047    e_comp_wl = NULL;
4048    e_comp->wl_comp_data = NULL;
4049    free(comp);
4050    // TODO: yigl
4051 #if 0
4052    E_Comp_Wl_Output *output;
4053
4054    if (e_comp_wl->screenshooter.global)
4055      wl_global_destroy(e_comp_wl->screenshooter.global);
4056
4057    EINA_LIST_FREE(e_comp_wl->outputs, output)
4058      {
4059         if (output->id) eina_stringshare_del(output->id);
4060         if (output->make) eina_stringshare_del(output->make);
4061         if (output->model) eina_stringshare_del(output->model);
4062         free(output);
4063      }
4064
4065    /* delete fd handler */
4066    if (e_comp_wl->fd_hdlr) ecore_main_fd_handler_del(e_comp_wl->fd_hdlr);
4067
4068    E_FREE_FUNC(e_comp_wl->ptr.hide_tmr, ecore_timer_del);
4069    cursor_timer_ec = NULL;
4070
4071    /* free allocated data structure */
4072    free(e_comp_wl);
4073 #endif
4074 }
4075
4076 static void
4077 e_comp_wl_surface_event_simple_free(void *d EINA_UNUSED, E_Event_Client *ev)
4078 {
4079    e_object_unref(E_OBJECT(ev->ec));
4080    free(ev);
4081 }
4082
4083 EINTERN void
4084 e_comp_wl_surface_attach(E_Client *ec, E_Comp_Wl_Buffer *buffer)
4085 {
4086    E_Event_Client *ev;
4087    ev = E_NEW(E_Event_Client, 1);
4088    if (!ev) return;
4089
4090    e_comp_wl_buffer_reference(&ec->comp_data->buffer_ref, buffer);
4091
4092    /* set usable early because shell module checks this */
4093    if (ec->comp_data->shell.surface || e_comp_wl_subsurface_check(ec))
4094      e_pixmap_usable_set(ec->pixmap, (buffer != NULL));
4095
4096    e_pixmap_resource_set(ec->pixmap, buffer);
4097    e_pixmap_dirty(ec->pixmap);
4098    e_pixmap_refresh(ec->pixmap);
4099
4100    e_comp_wl_map_size_cal_from_buffer(ec);
4101    _e_comp_wl_surface_state_size_update(ec, &ec->comp_data->pending);
4102
4103    /* wm-policy module uses it */
4104    _e_comp_wl_hook_call(E_COMP_WL_HOOK_BUFFER_CHANGE, ec);
4105
4106    ev->ec = ec;
4107    e_object_ref(E_OBJECT(ec));
4108    ecore_event_add(E_EVENT_CLIENT_BUFFER_CHANGE, ev,
4109                    (Ecore_End_Cb)e_comp_wl_surface_event_simple_free, NULL);
4110 }
4111
4112 EINTERN Eina_Bool
4113 e_comp_wl_surface_commit(E_Client *ec)
4114 {
4115    Eina_Bool ignored;
4116    int x = 0, y = 0;
4117
4118    _e_comp_wl_surface_state_commit(ec, &ec->comp_data->pending);
4119    if (!e_comp_object_damage_exists(ec->frame))
4120      {
4121         if ((ec->comp_data->video_client) ||
4122             (!e_client_video_hw_composition_check(ec)))
4123           e_pixmap_image_clear(ec->pixmap, 1);
4124      }
4125
4126    ignored = ec->ignored;
4127
4128    if (e_comp_wl_subsurface_order_commit(ec))
4129      {
4130         E_Client *topmost = e_comp_wl_topmost_parent_get(ec);
4131         e_comp_wl_subsurface_restack(topmost);
4132         e_comp_wl_subsurface_restack_bg_rectangle(topmost);
4133      }
4134
4135    Eina_Bool pixmap_usable = e_pixmap_usable_get(ec->pixmap);
4136    Eina_Bool hide_by_request = e_client_hide_by_request_get(ec);
4137    if (!pixmap_usable || hide_by_request)
4138      {
4139         if (ec->comp_data->mapped)
4140           {
4141              if (hide_by_request)
4142                ELOGF("COMP", "Currenlty hide_by_request state2. pixmap_usable:%d", ec, pixmap_usable);
4143
4144              if ((ec->comp_data->shell.surface) && (ec->comp_data->shell.unmap))
4145                {
4146                   ELOGF("COMP", "Try to unmap2. Call shell.unmap.", ec);
4147                   ec->comp_data->shell.unmap(ec->comp_data->shell.surface);
4148                }
4149              else if (ec->internal || e_comp_wl_subsurface_check(ec) ||
4150                       (ec == e_comp_wl->drag_client))
4151                {
4152                   ELOGF("COMP", "Try to unmap2. Hide window. internal:%d, sub:%p, drag:%d",
4153                         ec, ec->internal, ec->comp_data->sub.data, (ec == e_comp_wl->drag_client));
4154                   ec->visible = EINA_FALSE;
4155                   evas_object_hide(ec->frame);
4156                   ec->comp_data->mapped = 0;
4157                }
4158           }
4159
4160         if (ec->comp_data->sub.below_obj && evas_object_visible_get(ec->comp_data->sub.below_obj))
4161           evas_object_hide(ec->comp_data->sub.below_obj);
4162      }
4163    else
4164      {
4165         if (!ec->comp_data->mapped)
4166           {
4167              if ((ec->comp_data->shell.surface) && (ec->comp_data->shell.map) &&
4168                  (!ec->ignored))
4169                {
4170                   ELOGF("COMP", "Try to map2. Call shell.map.", ec);
4171                   ec->comp_data->shell.map(ec->comp_data->shell.surface);
4172                }
4173              else if (ec->internal || e_comp_wl_subsurface_can_show(ec) ||
4174                       (ec == e_comp_wl->drag_client))
4175                {
4176                   ELOGF("COMP", "Try to map2. Show window. internal:%d, drag:%d",
4177                         ec, ec->internal, (ec == e_comp_wl->drag_client));
4178                   ec->visible = EINA_TRUE;
4179                   ec->ignored = 0;
4180                   evas_object_show(ec->frame);
4181                   ec->comp_data->mapped = 1;
4182                }
4183           }
4184
4185         if (ec->comp_data->sub.below_obj && !evas_object_visible_get(ec->comp_data->sub.below_obj)
4186             && evas_object_visible_get(ec->frame))
4187           evas_object_show(ec->comp_data->sub.below_obj);
4188      }
4189    ec->ignored = ignored;
4190
4191    if (ec->is_cursor && ec->visible)
4192      {
4193         /* ignore cursor changes during resize/move I guess */
4194         if (!e_client_action_get())
4195           {
4196              if (e_comp->pointer)
4197                {
4198                   x = e_comp->pointer->hot.x;
4199                   y = e_comp->pointer->hot.y;
4200                }
4201              e_pointer_object_set(e_comp->pointer, ec->frame, x, y);
4202           }
4203      }
4204    return EINA_TRUE;
4205 }
4206
4207 static E_Comp_Wl_Output *
4208 _e_comp_wl_output_get(Eina_List *outputs, const char *id)
4209 {
4210    Eina_List *l;
4211    E_Comp_Wl_Output *output;
4212
4213    EINA_LIST_FOREACH(outputs, l, output)
4214      {
4215        if (!strcmp(output->id, id))
4216          return output;
4217      }
4218
4219    return NULL;
4220 }
4221
4222 /**
4223  * Initializes information about one display output.
4224  *
4225  * Adds or updates the given data about a single display output,
4226  * with an id matching the provided id.
4227  *
4228  * @param id         identification of output to be added or changed
4229  * @param make       manufacturer name of the display output
4230  * @param model      model name of the display output
4231  * @param x          output's top left corner x coordinate
4232  * @param y          output's top left corner y coordinate
4233  * @param w          output's width in pixels
4234  * @param h          output's height in pixels
4235  * @param pw         output's physical width in millimeters
4236  * @param ph         output's physical height in millimeters
4237  * @param refresh    output's refresh rate in mHz
4238  * @param subpixel   output's subpixel layout
4239  * @param transform  output's rotation and/or mirror transformation
4240  *
4241  * @returns True if a display output object could be added or updated
4242  */
4243 EINTERN Eina_Bool
4244 e_comp_wl_output_init(const char *id, const char *make, const char *model,
4245                       int x, int y, int w, int h, int pw, int ph,
4246                       unsigned int refresh, unsigned int subpixel,
4247                       unsigned int transform)
4248 {
4249    E_Comp_Wl_Output *output;
4250    Eina_List *l2;
4251    struct wl_resource *resource;
4252
4253    /* retrieve named output; or create it if it doesn't exist */
4254    output = _e_comp_wl_output_get(e_comp_wl->outputs, id);
4255    if (!output)
4256      {
4257         if (!(output = E_NEW(E_Comp_Wl_Output, 1))) return EINA_FALSE;
4258
4259         if (id) output->id = eina_stringshare_add(id);
4260         if (make)
4261           output->make = eina_stringshare_add(make);
4262         else
4263           output->make = eina_stringshare_add("unknown");
4264         if (model)
4265           output->model = eina_stringshare_add(model);
4266         else
4267           output->model = eina_stringshare_add("unknown");
4268
4269         e_comp_wl->outputs = eina_list_append(e_comp_wl->outputs, output);
4270
4271         output->global =
4272           wl_global_create(e_comp_wl->wl.disp, &wl_output_interface,
4273                            2, output, _e_comp_wl_cb_output_bind);
4274
4275         output->resources = NULL;
4276         output->scale = e_scale;
4277      }
4278
4279    /* update the output details */
4280    output->x = x;
4281    output->y = y;
4282    output->w = w;
4283    output->h = h;
4284    output->phys_width = pw;
4285    output->phys_height = ph;
4286    output->refresh = refresh;
4287    output->subpixel = subpixel;
4288    output->transform = transform;
4289
4290    if (output->scale <= 0)
4291      output->scale = e_scale;
4292
4293    /* if we have bound resources, send updates */
4294    EINA_LIST_FOREACH(output->resources, l2, resource)
4295      {
4296         wl_output_send_geometry(resource,
4297                                 output->x, output->y,
4298                                 output->phys_width,
4299                                 output->phys_height,
4300                                 output->subpixel,
4301                                 output->make ?: "", output->model ?: "",
4302                                 output->transform);
4303
4304         if (wl_resource_get_version(resource) >= WL_OUTPUT_SCALE_SINCE_VERSION)
4305           wl_output_send_scale(resource, output->scale);
4306
4307         wl_output_send_mode(resource, WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED,
4308                             output->w, output->h, output->refresh);
4309
4310         if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4311           wl_output_send_done(resource);
4312      }
4313
4314    return EINA_TRUE;
4315 }
4316
4317 EINTERN void
4318 e_comp_wl_output_remove(const char *id)
4319 {
4320    E_Comp_Wl_Output *output;
4321
4322    output = _e_comp_wl_output_get(e_comp_wl->outputs, id);
4323    if (output)
4324      {
4325         e_comp_wl->outputs = eina_list_remove(e_comp_wl->outputs, output);
4326
4327         /* wl_global_destroy(output->global); */
4328
4329         /* eina_stringshare_del(output->id); */
4330         /* eina_stringshare_del(output->make); */
4331         /* eina_stringshare_del(output->model); */
4332
4333         /* free(output); */
4334      }
4335 }
4336
4337 static void
4338 _e_comp_wl_key_send(Ecore_Event_Key *ev, E_Device *dev, enum wl_keyboard_key_state state, Eina_List *key_list, E_Client *ec)
4339 {
4340    struct wl_resource *res;
4341    Eina_List *l;
4342    uint32_t serial, keycode;
4343    struct wl_client *wc = NULL;
4344    E_Comp_Config *comp_conf = NULL;
4345    const char *device_name = NULL;
4346
4347    keycode = (ev->keycode - 8);
4348
4349    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4350
4351    comp_conf = e_comp_config_get();
4352
4353    if (ec && ec->comp_data)
4354      {
4355         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4356
4357         if (surface)
4358           wc = wl_resource_get_client(surface);
4359      }
4360
4361    if (e_config->key_input_ttrace_enable)
4362      {
4363         TRACE_INPUT_BEGIN(wl_keyboard_send_key:%s:%s, (state ? "PRESS" : "RELEASE"), ev->keyname);
4364         ELOGF("INPUT", "wl_keyboard_send_key:%s:%s|B|", NULL, (state ? "PRESS" : "RELEASE"), ev->keyname);
4365      }
4366
4367    EINA_LIST_FOREACH(key_list, l, res)
4368      {
4369         if (wl_resource_get_client(res) != wc) continue;
4370
4371         TRACE_INPUT_BEGIN(_e_comp_wl_key_send);
4372         if (!e_input_thread_mode_get())
4373           {
4374              _e_comp_wl_send_event_device(wc, ev->timestamp, ev->dev, serial);
4375              device_name = ecore_device_name_get(ev->dev);
4376           }
4377         else
4378           {
4379              if (dev)
4380                {
4381                   _e_comp_wl_send_event_e_device(wc, ev->timestamp, dev, serial);
4382                   device_name = e_device_name_get(dev);
4383                }
4384           }
4385
4386         if (comp_conf && comp_conf->input_log_enable)
4387           ELOGF("Key", "Send Key %s (time: %d, device: %s)", ec, (state ? "Down" : "Up"), ev->timestamp, device_name);
4388
4389         wl_keyboard_send_key(res, serial, ev->timestamp,
4390                              keycode, state);
4391         TRACE_INPUT_END();
4392      }
4393
4394    if (e_config->key_input_ttrace_enable)
4395      {
4396         TRACE_INPUT_END();
4397         ELOGF("INPUT", "wl_keyboard_send_key|E|", NULL);
4398      }
4399 }
4400
4401 EINTERN Eina_Bool
4402 e_comp_wl_key_down(Ecore_Event_Key *ev, E_Device *dev)
4403 {
4404    E_Client *ec = NULL;
4405    uint32_t keycode;
4406    E_Comp_Wl_Key_Data *end, *k;
4407
4408    if (ev->window != e_comp->ee_win)
4409      {
4410         return EINA_FALSE;
4411      }
4412
4413    keycode = (ev->keycode - 8);
4414    if (!(e_comp_wl = e_comp->wl_comp_data))
4415      {
4416         return EINA_FALSE;
4417      }
4418
4419 #ifndef E_RELEASE_BUILD
4420    if ((ev->modifiers & ECORE_EVENT_MODIFIER_CTRL) &&
4421        ((ev->modifiers & ECORE_EVENT_MODIFIER_ALT) ||
4422        (ev->modifiers & ECORE_EVENT_MODIFIER_ALTGR)) &&
4423        eina_streq(ev->key, "BackSpace"))
4424      {
4425         exit(0);
4426      }
4427 #endif
4428
4429    g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
4430
4431    end = (E_Comp_Wl_Key_Data *)e_comp_wl->kbd.keys.data + (e_comp_wl->kbd.keys.size / sizeof(*k));
4432
4433    for (k = e_comp_wl->kbd.keys.data; k < end; k++)
4434      {
4435         /* ignore server-generated key repeats */
4436         if (k->key == keycode)
4437           {
4438              g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
4439              return EINA_FALSE;
4440           }
4441      }
4442
4443    g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
4444
4445    if ((!e_client_action_get()) && (!e_comp->input_key_grabs))
4446      {
4447         ec = e_client_focused_get();
4448         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4449         if (ec && ec->comp_data && surface)
4450           {
4451              g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
4452              if (e_comp_wl->kbd.focused)
4453                {
4454                   _e_comp_wl_key_send(ev, dev, WL_KEYBOARD_KEY_STATE_PRESSED, e_comp_wl->kbd.focused, ec);
4455
4456                   /* A key only sent to clients is added to the list */
4457                   g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
4458                   e_comp_wl->kbd.keys.size = (const char *)end - (const char *)e_comp_wl->kbd.keys.data;
4459
4460                   if (!(k = wl_array_add(&e_comp_wl->kbd.keys, sizeof(*k))))
4461                     {
4462                        DBG("wl_array_add: Out of memory\n");
4463                        g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
4464                        g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
4465                        return EINA_FALSE;
4466                     }
4467                   g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
4468                   k->key = keycode;
4469                   k->dev = ev->dev;
4470                }
4471              g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
4472           }
4473      }
4474
4475    /* update modifier state */
4476    e_comp_wl_input_keyboard_state_update(keycode, EINA_TRUE);
4477
4478    return !!ec;
4479 }
4480
4481 EINTERN Eina_Bool
4482 e_comp_wl_key_up(Ecore_Event_Key *ev, E_Device *dev)
4483 {
4484    E_Client *ec = NULL;
4485    uint32_t keycode, delivered_key;
4486    E_Comp_Wl_Key_Data *end, *k;
4487
4488    if (ev->window != e_comp->ee_win)
4489      {
4490         return EINA_FALSE;
4491      }
4492
4493    keycode = (ev->keycode - 8);
4494    delivered_key = 0;
4495    if (!(e_comp_wl = e_comp->wl_comp_data))
4496      {
4497         return EINA_FALSE;
4498      }
4499
4500    g_mutex_lock(&e_comp_wl->kbd.keys_mutex);
4501
4502    end = (E_Comp_Wl_Key_Data *)e_comp_wl->kbd.keys.data + (e_comp_wl->kbd.keys.size / sizeof(*k));
4503    for (k = e_comp_wl->kbd.keys.data; k < end; k++)
4504      {
4505         if (k->key == keycode)
4506           {
4507              *k = *--end;
4508              delivered_key = 1;
4509           }
4510      }
4511
4512    e_comp_wl->kbd.keys.size =
4513      (const char *)end - (const char *)e_comp_wl->kbd.keys.data;
4514
4515    g_mutex_unlock(&e_comp_wl->kbd.keys_mutex);
4516
4517    /* If a key down event have been sent to clients, send a key up event to client for garantee key event sequence pair. (down/up) */
4518    if ((delivered_key) ||
4519        ((!e_client_action_get()) && (!e_comp->input_key_grabs)))
4520      {
4521         ec = e_client_focused_get();
4522
4523         g_mutex_lock(&e_comp_wl->kbd.focused_mutex);
4524         if (e_comp_wl->kbd.focused)
4525           {
4526              _e_comp_wl_key_send(ev, dev, WL_KEYBOARD_KEY_STATE_RELEASED, e_comp_wl->kbd.focused, ec);
4527           }
4528         g_mutex_unlock(&e_comp_wl->kbd.focused_mutex);
4529      }
4530
4531    /* update modifier state */
4532    e_comp_wl_input_keyboard_state_update(keycode, EINA_FALSE);
4533
4534    return !!ec;
4535 }
4536
4537 EINTERN Eina_Bool
4538 e_comp_wl_key_process(Ecore_Event_Key *ev, E_Device *dev, int type)
4539 {
4540    Eina_Bool res = EINA_FALSE;
4541
4542    if (type == ECORE_EVENT_KEY_DOWN)
4543      {
4544         res = e_comp_wl_key_down(ev, dev);
4545      }
4546    else if (type == ECORE_EVENT_KEY_UP)
4547      {
4548         res = e_comp_wl_key_up(ev, dev);
4549      }
4550
4551    return res;
4552 }
4553
4554 EINTERN Eina_Bool
4555 e_comp_wl_evas_handle_mouse_button(E_Client *ec, uint32_t timestamp, uint32_t button_id, uint32_t state)
4556 {
4557    Eina_List *l;
4558    struct wl_client *wc;
4559    uint32_t serial, btn;
4560    struct wl_resource *res;
4561    E_Comp_Config *comp_conf = NULL;
4562
4563    if (ec->cur_mouse_action || e_comp_wl->drag)
4564      return EINA_FALSE;
4565    if (e_object_is_del(E_OBJECT(ec))) return EINA_FALSE;
4566    if ((ec->ignored) && (!ec->remote_surface.provider)) return EINA_FALSE;
4567
4568    switch (button_id)
4569      {
4570       case 1:  btn = BTN_LEFT;   break;
4571       case 2:  btn = BTN_MIDDLE; break;
4572       case 3:  btn = BTN_RIGHT;  break;
4573       default: btn = button_id;  break;
4574      }
4575
4576    e_comp_wl->ptr.button = btn;
4577    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4578    if (!surface) return EINA_FALSE;
4579
4580    if (!eina_list_count(e_comp_wl->ptr.resources))
4581      return EINA_TRUE;
4582
4583    wc = wl_resource_get_client(surface);
4584    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4585
4586    comp_conf = e_comp_config_get();
4587
4588    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
4589      {
4590         if (wl_resource_get_client(res) != wc) continue;
4591         if (!e_comp_wl_input_pointer_check(res)) continue;
4592         TRACE_INPUT_BEGIN(e_comp_wl_evas_handle_mouse_button);
4593
4594         if (comp_conf && comp_conf->input_log_enable)
4595           ELOGF("Mouse", "Button %s (btn: %d, time: %d)", ec, (state ? "Down" : "Up"), btn, timestamp);
4596
4597         wl_pointer_send_button(res, serial, timestamp, btn, state);
4598         TRACE_INPUT_END();
4599      }
4600    return EINA_TRUE;
4601 }
4602
4603 E_API void
4604 e_comp_wl_touch_cancel(void)
4605 {
4606    _e_comp_wl_touch_cancel();
4607 }
4608
4609 E_API E_Comp_Wl_Hook *
4610 e_comp_wl_hook_add(E_Comp_Wl_Hook_Point hookpoint, E_Comp_Wl_Hook_Cb func, const void *data)
4611 {
4612    E_Comp_Wl_Hook *ch;
4613
4614    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_HOOK_LAST, NULL);
4615    ch = E_NEW(E_Comp_Wl_Hook, 1);
4616    if (!ch) return NULL;
4617    ch->hookpoint = hookpoint;
4618    ch->func = func;
4619    ch->data = (void*)data;
4620    _e_comp_wl_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_hooks[hookpoint], EINA_INLIST_GET(ch));
4621    return ch;
4622 }
4623
4624 E_API void
4625 e_comp_wl_hook_del(E_Comp_Wl_Hook *ch)
4626 {
4627    ch->delete_me = 1;
4628    if (_e_comp_wl_hooks_walking == 0)
4629      {
4630         _e_comp_wl_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4631         free(ch);
4632      }
4633    else
4634      _e_comp_wl_hooks_delete++;
4635 }
4636
4637 E_API E_Comp_Wl_Pid_Hook *
4638 e_comp_wl_pid_hook_add(E_Comp_Wl_Pid_Hook_Point hookpoint, E_Comp_Wl_Pid_Hook_Cb func, const void *data)
4639 {
4640    E_Comp_Wl_Pid_Hook *ch;
4641
4642    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_PID_HOOK_LAST, NULL);
4643
4644    ch = E_NEW(E_Comp_Wl_Pid_Hook, 1);
4645    EINA_SAFETY_ON_NULL_RETURN_VAL(ch, NULL);
4646
4647    ch->hookpoint = hookpoint;
4648    ch->func = func;
4649    ch->data = (void*)data;
4650    _e_comp_wl_pid_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_pid_hooks[hookpoint], EINA_INLIST_GET(ch));
4651
4652    return ch;
4653 }
4654
4655 E_API void
4656 e_comp_wl_pid_hook_del(E_Comp_Wl_Pid_Hook *ch)
4657 {
4658    ch->delete_me = 1;
4659    if (_e_comp_wl_pid_hooks_walking == 0)
4660      {
4661         _e_comp_wl_pid_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_pid_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4662         free(ch);
4663      }
4664    else
4665      _e_comp_wl_pid_hooks_delete++;
4666 }
4667
4668 E_API E_Comp_Wl_Intercept_Hook *
4669 e_comp_wl_intercept_hook_add(E_Comp_Wl_Intercept_Hook_Point hookpoint, E_Comp_Wl_Intercept_Hook_Cb func, const void *data)
4670 {
4671    E_Comp_Wl_Intercept_Hook *ch;
4672
4673    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_INTERCEPT_HOOK_LAST, NULL);
4674    ch = E_NEW(E_Comp_Wl_Intercept_Hook, 1);
4675    if (!ch) return NULL;
4676    ch->hookpoint = hookpoint;
4677    ch->func = func;
4678    ch->data = (void*)data;
4679    _e_comp_wl_intercept_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_intercept_hooks[hookpoint], EINA_INLIST_GET(ch));
4680    return ch;
4681 }
4682
4683 E_API void
4684 e_comp_wl_intercept_hook_del(E_Comp_Wl_Intercept_Hook *ch)
4685 {
4686    ch->delete_me = 1;
4687    if (_e_comp_wl_intercept_hooks_walking == 0)
4688      {
4689         _e_comp_wl_intercept_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_intercept_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4690         free(ch);
4691      }
4692    else
4693      _e_comp_wl_intercept_hooks_delete++;
4694 }
4695
4696 EINTERN void
4697 e_comp_wl_shell_surface_ready(E_Client *ec)
4698 {
4699    if (!ec) return;
4700
4701    _e_comp_wl_hook_call(E_COMP_WL_HOOK_SHELL_SURFACE_READY, ec);
4702 }
4703
4704 E_API void
4705 e_comp_wl_input_cursor_timer_enable_set(Eina_Bool enabled)
4706 {
4707    e_config->use_cursor_timer = !!enabled;
4708
4709    if (e_config->use_cursor_timer)
4710      {
4711         if (!e_pointer_is_hidden(e_comp->pointer))
4712           {
4713              _e_comp_wl_cursor_move_timer_control(e_comp_wl->ptr.ec);
4714           }
4715      }
4716    else
4717      {
4718         if (e_comp_wl->ptr.hide_tmr)
4719           {
4720              ecore_timer_del(e_comp_wl->ptr.hide_tmr);
4721              e_comp_wl->ptr.hide_tmr = NULL;
4722           }
4723         cursor_timer_ec = NULL;
4724
4725         if (e_comp_wl->ptr.ec && e_comp_wl->ptr.ec->has_cursor_unset)
4726           return;
4727
4728         if (e_pointer_is_hidden(e_comp->pointer))
4729           {
4730              _e_comp_wl_cursor_reload(e_comp_wl->ptr.ec);
4731           }
4732      }
4733 }
4734
4735 EINTERN void
4736 e_comp_wl_send_event_device(struct wl_client *wc, uint32_t timestamp, Ecore_Device *dev, uint32_t serial)
4737 {
4738    EINA_SAFETY_ON_NULL_RETURN(wc);
4739    EINA_SAFETY_ON_NULL_RETURN(dev);
4740
4741    _e_comp_wl_send_event_device(wc, timestamp, dev, serial);
4742 }
4743
4744 EINTERN void
4745 e_comp_wl_send_event_e_device(struct wl_client *wc, uint32_t timestamp, E_Device *dev, uint32_t serial)
4746 {
4747    EINA_SAFETY_ON_NULL_RETURN(wc);
4748    EINA_SAFETY_ON_NULL_RETURN(dev);
4749
4750    _e_comp_wl_send_event_e_device(wc, timestamp, dev, serial);
4751 }
4752
4753 EINTERN Eina_Bool
4754 e_comp_wl_key_send(E_Client *ec, int keycode, Eina_Bool pressed, void *dev, uint32_t time)
4755 {
4756    struct wl_resource *res;
4757    struct wl_client *wc;
4758    Eina_List *l;
4759    uint32_t serial, wl_keycode;
4760    enum wl_keyboard_key_state state;
4761    E_Comp_Config *comp_conf = NULL;
4762    const char *dev_name = NULL;
4763
4764    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4765    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4766    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4767
4768    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4769    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4770
4771    wl_keycode = keycode - 8;
4772    EINA_SAFETY_ON_TRUE_RETURN_VAL(wl_keycode <= 0, EINA_FALSE);
4773
4774    wc = wl_resource_get_client(surface);
4775    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4776    if (!time) time = e_util_timestamp_get();
4777    if (pressed) state = WL_KEYBOARD_KEY_STATE_PRESSED;
4778    else state = WL_KEYBOARD_KEY_STATE_RELEASED;
4779
4780    comp_conf = e_comp_config_get();
4781    e_keyrouter_event_surface_send(ec, keycode);
4782
4783    if (e_config->key_input_ttrace_enable)
4784      {
4785         TRACE_INPUT_BEGIN(wl_keyboard_send_key:%s:%d, (state ? "PRESS" : "RELEASE"), keycode);
4786         ELOGF("INPUT", "wl_keyboard_send_key:%s:%d|B|", NULL, (state ? "PRESS" : "RELEASE"), keycode);
4787      }
4788
4789    g_mutex_lock(&e_comp_wl->kbd.resource_mutex);
4790    EINA_LIST_FOREACH(e_comp_wl->kbd.resources, l, res)
4791      {
4792         if (wl_resource_get_client(res) != wc) continue;
4793         if (!e_input_thread_mode_get())
4794           {
4795              if (dev)
4796                {
4797                   _e_comp_wl_send_event_device(wc, time, (Ecore_Device *)dev, serial);
4798                   dev_name = ecore_device_name_get((Ecore_Device *)dev);
4799                }
4800              else
4801                {
4802                   _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_KEYBOARD, time);
4803                }
4804           }
4805         else
4806           {
4807              if (dev)
4808                {
4809                   e_comp_wl_send_event_e_device(wc, time, (E_Device *)dev, serial);
4810                   dev_name = e_device_name_get(dev);
4811                }
4812           }
4813
4814         if (comp_conf && comp_conf->input_log_enable)
4815           ELOGF("Key", "Send Key %s (keycode: %d, time: %d, device: %s)", ec, (state ? "Down" : "Up"), wl_keycode, time,
4816                 dev_name);
4817
4818         wl_keyboard_send_key(res, serial, time,
4819                              wl_keycode, state);
4820      }
4821    g_mutex_unlock(&e_comp_wl->kbd.resource_mutex);
4822
4823    if (e_config->key_input_ttrace_enable)
4824      {
4825         TRACE_INPUT_END();
4826         ELOGF("INPUT", "wl_keyboard_send_key|E|", NULL);
4827      }
4828
4829    return EINA_TRUE;
4830 }
4831
4832 EINTERN Eina_Bool
4833 e_comp_wl_key_cancel(E_Client *ec, int keycode, Ecore_Device *dev, uint32_t time)
4834 {
4835    struct wl_resource *res;
4836    struct wl_client *wc;
4837    Eina_List *l;
4838    uint32_t serial, wl_keycode, cancel_keycode;
4839    E_Comp_Config *comp_conf = NULL;
4840
4841    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4842    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4843    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4844    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl->xkb.keymap, EINA_FALSE);
4845
4846    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4847    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4848
4849    cancel_keycode = e_comp_wl_input_keymap_keyname_to_keycode("Cancel");
4850    if (cancel_keycode == XKB_KEYCODE_INVALID)
4851      {
4852         ELOGF("Key", "Failed to send key cancel for %d key, Cancel key is not supported\n", ec, keycode);
4853         return EINA_FALSE;
4854      }
4855    cancel_keycode = cancel_keycode - 8;
4856
4857    wl_keycode = keycode - 8;
4858    EINA_SAFETY_ON_TRUE_RETURN_VAL(wl_keycode <= 0, EINA_FALSE);
4859
4860    wc = wl_resource_get_client(surface);
4861    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4862    if (!time) time = e_util_timestamp_get();
4863
4864    comp_conf = e_comp_config_get();
4865    e_keyrouter_event_surface_send(ec, keycode);
4866
4867    g_mutex_lock(&e_comp_wl->kbd.resource_mutex);
4868    EINA_LIST_FOREACH(e_comp_wl->kbd.resources, l, res)
4869      {
4870         if (wl_resource_get_client(res) != wc) continue;
4871         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4872         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_KEYBOARD, time);
4873
4874         if (comp_conf && comp_conf->input_log_enable)
4875           ELOGF("Key", "Send Key Cancel (time: %d)", ec, time);
4876         wl_keyboard_send_key(res, serial, time,
4877                              cancel_keycode, WL_KEYBOARD_KEY_STATE_PRESSED);
4878         wl_keyboard_send_key(res, serial, time,
4879                              wl_keycode, WL_KEYBOARD_KEY_STATE_RELEASED);
4880         wl_keyboard_send_key(res, serial, time,
4881                              cancel_keycode, WL_KEYBOARD_KEY_STATE_RELEASED);
4882      }
4883    g_mutex_unlock(&e_comp_wl->kbd.resource_mutex);
4884
4885    return EINA_TRUE;
4886 }
4887
4888
4889 EINTERN Eina_Bool
4890 e_comp_wl_touch_send(E_Client *ec, int idx, int x, int y, Eina_Bool pressed, Ecore_Device *dev, double radius_x, double radius_y, double pressure, double angle, uint32_t time)
4891 {
4892    struct wl_client *wc;
4893    uint32_t serial;
4894    E_Devicemgr_Input_Device *device = NULL;
4895
4896    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4897    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4898    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4899
4900    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4901    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4902
4903    if (!dev) device = _e_comp_wl_device_last_device_get(ECORE_DEVICE_CLASS_TOUCH);
4904
4905    wc = wl_resource_get_client(surface);
4906    if (!time) time = e_util_timestamp_get();
4907    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4908
4909    if (dev)
4910      {
4911         _e_comp_wl_send_event_device(wc, time, dev, serial);
4912         _e_comp_wl_device_handle_axes(ecore_device_identifier_get(dev), ECORE_DEVICE_CLASS_TOUCH, ec, idx, radius_x, radius_y, pressure, angle);
4913      }
4914    else if (device)
4915      {
4916         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_TOUCH, time);
4917         _e_comp_wl_device_handle_axes(device->identifier, device->clas, ec, idx, radius_x, radius_y, pressure, angle);
4918      }
4919
4920    x = x + ec->client.x;
4921    y = y + ec->client.y;
4922
4923    _e_comp_wl_send_touch(ec, idx, x, y, time, pressed);
4924
4925    return EINA_TRUE;
4926 }
4927
4928 EINTERN Eina_Bool
4929 e_comp_wl_touch_update_send(E_Client *ec, int idx, int x, int y, Ecore_Device *dev, double radius_x, double radius_y, double pressure, double angle, uint32_t time)
4930 {
4931    E_Devicemgr_Input_Device *device;
4932    uint32_t serial;
4933    struct wl_client *wc;
4934
4935    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4936    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4937    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4938
4939    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4940    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4941
4942    if (!dev) device = _e_comp_wl_device_last_device_get(ECORE_DEVICE_CLASS_TOUCH);
4943
4944    wc = wl_resource_get_client(surface);
4945    if (!time) time = e_util_timestamp_get();
4946    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4947
4948    if (dev)
4949      {
4950         _e_comp_wl_send_event_device(wc, time, dev, serial);
4951         _e_comp_wl_device_handle_axes(ecore_device_identifier_get(dev), ECORE_DEVICE_CLASS_TOUCH, ec, idx, radius_x, radius_y, pressure, angle);
4952      }
4953    else if (device)
4954      {
4955         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_TOUCH, time);
4956         _e_comp_wl_device_handle_axes(device->identifier, device->clas, ec, idx, radius_x, radius_y, pressure, angle);
4957      }
4958
4959    x = x + ec->client.x;
4960    y = y + ec->client.y;
4961
4962    _e_comp_wl_send_touch_move(ec, idx, x, y, time);
4963
4964    return EINA_TRUE;
4965 }
4966
4967 EINTERN Eina_Bool
4968 e_comp_wl_touch_cancel_send(E_Client *ec)
4969 {
4970    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4971    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4972    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4973
4974    _e_comp_wl_send_touch_cancel(ec);
4975
4976    return EINA_TRUE;
4977 }
4978
4979 EINTERN Eina_Bool
4980 e_comp_wl_mouse_button_send(E_Client *ec, int buttons, Eina_Bool pressed, Ecore_Device *dev, uint32_t time)
4981 {
4982    uint32_t serial;
4983    struct wl_client *wc;
4984
4985    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4986    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4987    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4988
4989    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4990    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4991
4992    wc = wl_resource_get_client(surface);
4993    if (!time) time = e_util_timestamp_get();
4994    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4995
4996    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4997    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4998
4999    if (pressed)
5000      e_comp_wl_evas_handle_mouse_button(ec, time, buttons,
5001                                           WL_POINTER_BUTTON_STATE_PRESSED);
5002    else
5003      e_comp_wl_evas_handle_mouse_button(ec, time, buttons,
5004                                           WL_POINTER_BUTTON_STATE_RELEASED);
5005
5006    return EINA_TRUE;
5007 }
5008
5009 EINTERN Eina_Bool
5010 e_comp_wl_mouse_move_send(E_Client *ec, int x, int y, Ecore_Device *dev, uint32_t time)
5011 {
5012    uint32_t serial;
5013    struct wl_client *wc;
5014
5015    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5016    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
5017    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
5018
5019    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
5020    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
5021
5022    wc = wl_resource_get_client(surface);
5023    if (!time) time = e_util_timestamp_get();
5024    serial = wl_display_next_serial(e_comp_wl->wl.disp);
5025
5026    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
5027    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
5028
5029    x = x + ec->client.x;
5030    y = y + ec->client.y;
5031
5032    _e_comp_wl_send_mouse_move(ec, x, y, time);
5033
5034    return EINA_TRUE;
5035 }
5036
5037 EINTERN Eina_Bool
5038 e_comp_wl_mouse_wheel_send(E_Client *ec, int direction, int z, Ecore_Device *dev, uint32_t time)
5039 {
5040    uint32_t serial;
5041    struct wl_client *wc;
5042
5043    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5044    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
5045    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
5046
5047    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
5048    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
5049
5050    wc = wl_resource_get_client(surface);
5051    if (!time) time = e_util_timestamp_get();
5052    serial = wl_display_next_serial(e_comp_wl->wl.disp);
5053
5054    if (_e_comp_wl_check_cursor_timer_needed(ec))
5055       {
5056          if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_WHEEL, ec))
5057            return EINA_TRUE;
5058       }
5059
5060    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
5061    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
5062
5063    _e_comp_wl_mouse_wheel_send(ec, direction, z, time);
5064
5065    if (!need_send_released) // set cursor's hide_tmr only when mouse button is not pressed
5066      {
5067         if (_e_comp_wl_check_cursor_timer_needed(ec))
5068           _e_comp_wl_cursor_move_timer_control(ec);
5069      }
5070
5071    return EINA_TRUE;
5072 }
5073
5074 EINTERN Eina_Bool
5075 e_comp_wl_mouse_in_send(E_Client *ec, int x, int y, Ecore_Device *dev, uint32_t time)
5076 {
5077    uint32_t serial;
5078    struct wl_client *wc;
5079    struct wl_resource *res;
5080    Eina_List *l;
5081
5082    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5083    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
5084
5085    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
5086    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
5087
5088    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
5089    EINA_SAFETY_ON_TRUE_RETURN_VAL(e_object_is_del(E_OBJECT(ec)), EINA_FALSE);
5090
5091    if (!eina_list_count(e_comp_wl->ptr.resources)) return EINA_FALSE;
5092    wc = wl_resource_get_client(surface);
5093    serial = wl_display_next_serial(e_comp_wl->wl.disp);
5094    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
5095      {
5096         if (!e_comp_wl_input_pointer_check(res)) continue;
5097         if (wl_resource_get_client(res) != wc) continue;
5098
5099         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
5100         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
5101
5102         wl_pointer_send_enter(res, serial, surface,
5103                               wl_fixed_from_int(x),
5104                               wl_fixed_from_int(y));
5105         ec->pointer_enter_sent = EINA_TRUE;
5106      }
5107    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_mousein_signal, ec);
5108
5109    return EINA_TRUE;
5110 }
5111
5112 EINTERN Eina_Bool
5113 e_comp_wl_mouse_out_send(E_Client *ec, Ecore_Device *dev, uint32_t time)
5114 {
5115    uint32_t serial;
5116    struct wl_client *wc;
5117    struct wl_resource *res;
5118    Eina_List *l;
5119
5120    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
5121    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5122    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
5123    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
5124    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
5125    EINA_SAFETY_ON_TRUE_RETURN_VAL(e_object_is_del(E_OBJECT(ec)), EINA_FALSE);
5126
5127    if (!eina_list_count(e_comp_wl->ptr.resources)) return EINA_FALSE;
5128    wc = wl_resource_get_client(surface);
5129    serial = wl_display_next_serial(e_comp_wl->wl.disp);
5130    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
5131      {
5132         if (!e_comp_wl_input_pointer_check(res)) continue;
5133         if (wl_resource_get_client(res) != wc) continue;
5134
5135         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
5136         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
5137
5138         wl_pointer_send_leave(res, serial, surface);
5139         ec->pointer_enter_sent = EINA_FALSE;
5140      }
5141
5142    return EINA_TRUE;
5143 }
5144
5145 EINTERN void
5146 e_comp_wl_mouse_in_renew(E_Client *ec, int buttons, int x, int y, void *data, Evas_Modifier *modifiers, Evas_Lock *locks, unsigned int timestamp, Evas_Event_Flags event_flags, Evas_Device *dev, Evas_Object *event_src)
5147 {
5148    Evas_Event_Mouse_In ev_in;
5149
5150    if (!ec) return;
5151    if (ec->pointer_enter_sent) return;
5152
5153    ev_in.buttons = buttons;
5154
5155    ev_in.output.x = x;
5156    ev_in.output.y = y;
5157    ev_in.canvas.x = x;
5158    ev_in.canvas.y = y;
5159
5160    ev_in.data = data;
5161    ev_in.modifiers = modifiers;
5162    ev_in.locks = locks;
5163    ev_in.timestamp = timestamp;
5164    ev_in.event_flags = event_flags;
5165
5166    ev_in.dev = dev;
5167    ev_in.event_src = event_src;
5168
5169    _e_comp_wl_evas_cb_mouse_in((void *)ec, NULL, NULL, &ev_in);
5170 }
5171
5172 EINTERN void
5173 e_comp_wl_mouse_out_renew(E_Client *ec, int buttons, int x, int y, void *data, Evas_Modifier *modifiers, Evas_Lock *locks, unsigned int timestamp, Evas_Event_Flags event_flags, Evas_Device *dev, Evas_Object *event_src)
5174 {
5175    Evas_Event_Mouse_Out ev_out;
5176
5177    if (!ec) return;
5178    if (!ec->pointer_enter_sent) return;
5179
5180    ev_out.buttons = buttons;
5181
5182    ev_out.output.x = x;
5183    ev_out.output.y = y;
5184    ev_out.canvas.x = x;
5185    ev_out.canvas.y = y;
5186
5187    ev_out.data = data;
5188    ev_out.modifiers = modifiers;
5189    ev_out.locks = locks;
5190    ev_out.timestamp = timestamp;
5191    ev_out.event_flags = event_flags;
5192
5193    ev_out.dev = dev;
5194    ev_out.event_src = event_src;
5195
5196    _e_comp_wl_evas_cb_mouse_out((void *)ec, NULL, NULL, &ev_out);
5197 }
5198
5199 EINTERN Eina_Bool
5200 e_comp_wl_cursor_hide(E_Client *ec)
5201 {
5202    struct wl_resource *res;
5203    struct wl_client *wc;
5204    Eina_List *l;
5205    uint32_t serial;
5206
5207    e_pointer_object_set(e_comp->pointer, NULL, 0, 0);
5208
5209    if (e_comp_wl->ptr.hide_tmr)
5210      {
5211         ecore_timer_del(e_comp_wl->ptr.hide_tmr);
5212         e_comp_wl->ptr.hide_tmr = NULL;
5213      }
5214    cursor_timer_ec = NULL;
5215
5216    if (!ec) return EINA_FALSE;
5217    if (e_object_is_del(E_OBJECT(ec))) return EINA_FALSE;
5218    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
5219    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
5220
5221    wc = wl_resource_get_client(surface);
5222    serial = wl_display_next_serial(e_comp_wl->wl.disp);
5223    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
5224      {
5225         if (!e_comp_wl_input_pointer_check(res)) continue;
5226         if (wl_resource_get_client(res) != wc) continue;
5227         if (ec->pointer_enter_sent == EINA_FALSE) continue;
5228         wl_pointer_send_leave(res, serial, surface);
5229         ec->pointer_enter_sent = EINA_FALSE;
5230      }
5231
5232    return EINA_TRUE;
5233 }
5234
5235 /* surface to buffer
5236  *   - width    : surface width
5237  *   - height   : surface height
5238  *   - transform: buffer transform
5239  *   - scale    : buffer scale
5240  * screen to output
5241  *   - width    : screen width
5242  *   - height   : screen height
5243  *   - transform: output transform
5244  *   - scale    : output scale
5245  */
5246 static E_Util_Transform_Matrix
5247 _e_comp_wl_buffer_coord_get(int width, int height, int transform, int scale)
5248 {
5249    E_Util_Transform_Matrix m;
5250
5251    e_util_transform_matrix_load_identity(&m);
5252
5253    if (transform & 0x4)
5254      {
5255         e_util_transform_matrix_translate(&m, -((double)width / 2), 0, 0);
5256         e_util_transform_matrix_flip_x(&m);
5257         e_util_transform_matrix_translate(&m, (double)width / 2, 0, 0);
5258      }
5259
5260    switch (transform & 0x3)
5261      {
5262       case WL_OUTPUT_TRANSFORM_90:
5263         e_util_transform_matrix_translate(&m, -width, 0, 0);
5264         e_util_transform_matrix_rotation_z(&m, 270);
5265         break;
5266       case WL_OUTPUT_TRANSFORM_180:
5267         e_util_transform_matrix_translate(&m, -width, -height, 0);
5268         e_util_transform_matrix_rotation_z(&m, 180);
5269         break;
5270       case WL_OUTPUT_TRANSFORM_270:
5271         e_util_transform_matrix_translate(&m, 0, -height, 0);
5272         e_util_transform_matrix_rotation_z(&m, 90);
5273         break;
5274       default:
5275         break;
5276      }
5277
5278    e_util_transform_matrix_scale(&m, scale, scale, 1);
5279
5280    return m;
5281 }
5282
5283 /* surface to buffer
5284  *   - surface width, surface height, buffer transform, buffer scale
5285  * screen to output
5286  *   - screen width, screen height, output transform, output scale
5287  */
5288 EINTERN void
5289 e_comp_wl_pos_convert(int width, int height, int transform, int scale, int sx, int sy, int *bx, int *by)
5290 {
5291    E_Util_Transform_Matrix m;
5292    E_Util_Transform_Vertex v;
5293
5294    m = _e_comp_wl_buffer_coord_get(width, height, transform, scale);
5295
5296    e_util_transform_vertex_init(&v, sx, sy, 0.0, 1.0);
5297    v = e_util_transform_matrix_multiply_vertex(&m, &v);
5298    e_util_transform_vertex_pos_round_get(&v, bx, by, NULL, NULL);
5299 }
5300
5301 /* buffer to screen
5302  *   - buffer width, buffer height, buffer transform, buffer scale
5303  */
5304 EINTERN void
5305 e_comp_wl_pos_convert_inverse(int width, int height, int transform, int scale, int bx, int by, int *sx, int *sy)
5306 {
5307    E_Util_Transform_Matrix m;
5308    E_Util_Transform_Vertex v;
5309    int tw, th;
5310
5311    if (transform != 0 || scale > 1)
5312      {
5313         tw = ((transform % 2) ? height : width) / scale;
5314         th = ((transform % 2) ? width : height) / scale;
5315      }
5316    else
5317      {
5318         tw = width;
5319         th = height;
5320      }
5321
5322    m = _e_comp_wl_buffer_coord_get(tw, th, transform, scale);
5323    m = e_util_transform_matrix_inverse_get(&m);
5324
5325    e_util_transform_vertex_init(&v, bx, by, 0.0, 1.0);
5326    v = e_util_transform_matrix_multiply_vertex(&m, &v);
5327    e_util_transform_vertex_pos_round_get(&v, sx, sy, NULL, NULL);
5328 }
5329
5330 /* surface to buffer
5331  *   - surface width, surface height, buffer transform, buffer scale
5332  * screen to output
5333  *   - screen width, screen height, output transform, output scale
5334  */
5335 EINTERN void
5336 e_comp_wl_rect_convert(int width, int height, int transform, int scale,
5337                        int sx, int sy, int sw, int sh, int *bx, int *by, int *bw, int *bh)
5338 {
5339    E_Util_Transform_Matrix m;
5340    E_Util_Transform_Rect sr = {sx, sy, sw, sh};
5341    E_Util_Transform_Rect_Vertex sv;
5342
5343    m = _e_comp_wl_buffer_coord_get(width, height, transform, scale);
5344
5345    sv = e_util_transform_rect_to_vertices(&sr);
5346    sv = e_util_transform_matrix_multiply_rect_vertex(&m, &sv);
5347    sr = e_util_transform_vertices_to_rect(&sv);
5348
5349    if (bx) *bx = sr.x;
5350    if (by) *by = sr.y;
5351    if (bw) *bw = sr.w;
5352    if (bh) *bh = sr.h;
5353 }
5354
5355 /* buffer to screen
5356  *   - buffer width, buffer height, buffer transform, buffer scale
5357  */
5358 EINTERN void
5359 e_comp_wl_rect_convert_inverse(int width, int height, int transform, int scale,
5360                                int bx, int by, int bw, int bh, int *sx, int *sy, int *sw, int *sh)
5361 {
5362    E_Util_Transform_Matrix m;
5363    E_Util_Transform_Rect br = {bx, by, bw, bh};
5364    E_Util_Transform_Rect_Vertex bv;
5365    int tw, th;
5366
5367    if (transform != 0 || scale > 1)
5368      {
5369         tw = ((transform % 2) ? height : width) / scale;
5370         th = ((transform % 2) ? width : height) / scale;
5371      }
5372    else
5373      {
5374         tw = width;
5375         th = height;
5376      }
5377
5378    m = _e_comp_wl_buffer_coord_get(tw, th, transform, scale);
5379    m = e_util_transform_matrix_inverse_get(&m);
5380
5381    bv = e_util_transform_rect_to_vertices(&br);
5382    bv = e_util_transform_matrix_multiply_rect_vertex(&m, &bv);
5383    br = e_util_transform_vertices_to_rect(&bv);
5384
5385    if (sx) *sx = br.x;
5386    if (sy) *sy = br.y;
5387    if (sw) *sw = br.w;
5388    if (sh) *sh = br.h;
5389 }
5390
5391 EINTERN E_Comp_Wl_Output*
5392 e_comp_wl_output_find(E_Client *ec)
5393 {
5394    Eina_List *l;
5395    E_Comp_Wl_Output *output;
5396
5397    if (!ec || !ec->comp_data || e_object_is_del(E_OBJECT(ec))) return NULL;
5398
5399    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5400      {
5401         if (output->transform % 2)
5402           {
5403              if (ec->x < output->y || ec->x >= (output->y + output->h) ||
5404                  ec->y < output->x || ec->y >= (output->x + output->w))
5405                continue;
5406           }
5407         else
5408           {
5409              if (ec->x < output->x || ec->x >= (output->x + output->w) ||
5410                  ec->y < output->y || ec->y >= (output->y + output->h))
5411                continue;
5412           }
5413
5414         return output;
5415      }
5416
5417    return NULL;
5418 }
5419
5420 EINTERN Eina_Array *
5421 e_comp_wl_output_find_all(E_Client *ec)
5422 {
5423    E_Comp_Wl_Output *output;
5424    Eina_Array *outputs;
5425    Eina_List *l;
5426
5427    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
5428
5429    outputs = eina_array_new(4);
5430    if (!outputs)
5431      return NULL;
5432
5433    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5434      {
5435         if (E_INTERSECTS(ec->x, ec->y, ec->w, ec->h,
5436                          output->x, output->y, output->w, output->h))
5437           {
5438              eina_array_push(outputs, output);
5439           }
5440      }
5441
5442    if (eina_array_count(outputs) == 0)
5443      {
5444         eina_array_free(outputs);
5445         return NULL;
5446      }
5447
5448    return outputs;
5449 }
5450
5451 // --------------------------------------------------------
5452 // tizen_move_resize
5453 // --------------------------------------------------------
5454 EINTERN Eina_Bool
5455 e_comp_wl_commit_sync_client_geometry_add(E_Client *ec,
5456                                           uint32_t serial,
5457                                           int32_t x,
5458                                           int32_t y,
5459                                           int32_t w,
5460                                           int32_t h)
5461 {
5462    E_Client_Pending_Geometry *geo;
5463
5464    if (!ec) goto ret;
5465    if (e_object_is_del(E_OBJECT(ec))) goto ret;
5466    if (ec->fullscreen || ec->maximized) goto err;
5467
5468    geo = E_NEW(E_Client_Pending_Geometry, 1);
5469    if (!geo) goto err;
5470
5471    geo->serial = serial;
5472    geo->x = x;
5473    geo->y = y;
5474    geo->w = w;
5475    geo->h = h;
5476
5477    ec->surface_sync.pending_geometry = eina_list_append(ec->surface_sync.pending_geometry, geo);
5478    ec->surface_sync.wait_commit = EINA_TRUE;
5479
5480    return EINA_TRUE;
5481
5482 err:
5483    ELOGF("POSSIZE", "Could not add geometry(new:%d full:%d max:%d)", ec, ec->new_client, ec->fullscreen, ec->maximized);
5484
5485 ret:
5486    return EINA_FALSE;
5487 }
5488
5489 EINTERN Eina_Bool
5490 e_comp_wl_commit_sync_configure(E_Client *ec)
5491 {
5492    Eina_List *l;
5493    E_Client_Pending_Geometry *geo;
5494    int bw, bh;
5495    Eina_Bool match_size = EINA_FALSE;
5496
5497    struct
5498      {
5499         int x, y, w, h;
5500      } config;
5501
5502    if (!ec || !ec->frame) goto ret;
5503    if (e_object_is_del(E_OBJECT(ec))) goto ret;
5504
5505    bw = bh = 0;
5506    config.x = ec->x; config.y = ec->y; config.w = ec->w; config.h = ec->h;
5507    if (!e_pixmap_size_get(ec->pixmap, &bw, &bh)) goto err;
5508
5509    if (eina_list_count(ec->surface_sync.pending_geometry))
5510      {
5511         Eina_List *ll = NULL;
5512         EINA_LIST_REVERSE_FOREACH_SAFE(ec->surface_sync.pending_geometry, l, ll, geo)
5513           {
5514              if (match_size)
5515                {
5516                   ec->surface_sync.pending_geometry = eina_list_remove(ec->surface_sync.pending_geometry, geo);
5517                   E_FREE(geo);
5518                   continue;
5519                }
5520
5521              if ((geo->w == bw) && (geo->h == bh))
5522                {
5523                   match_size = EINA_TRUE;
5524                   config.w = geo->w;
5525                   config.h = geo->h;
5526                   config.x = geo->x;
5527                   config.y = geo->y;
5528                   ec->surface_sync.pending_geometry = eina_list_remove(ec->surface_sync.pending_geometry, geo);
5529                   E_FREE(geo);
5530                }
5531           }
5532
5533         if (match_size)
5534           {
5535              if ((config.w != ec->w) || (config.h != ec->h))
5536                {
5537                   e_client_size_set(ec, config.w, config.h);
5538                   ec->changes.size = EINA_TRUE;
5539                   EC_CHANGED(ec);
5540                }
5541
5542              // FIXME: The geometry of the client would be better to be calculated
5543              //        at e_desk or at e_desk_area. So the position(x,y) of the client
5544              //        should be calcuated at the e_desk or at the e_desk_area.
5545              E_Desk *desk;
5546              desk = e_comp_desk_find_by_ec(ec);
5547              if (desk)
5548                {
5549                   ec->client.x = desk->geom.x + config.x;
5550                   ec->client.y = desk->geom.y + config.y;
5551                }
5552              else
5553                {
5554                   ec->client.x = config.x;
5555                   ec->client.y = config.y;
5556                }
5557
5558              if ((ec->client.x != ec->x) || (ec->client.y != ec->y))
5559                {
5560                   e_client_pos_set(ec, ec->client.x, ec->client.y);
5561                   ec->placed = 1;
5562                   ec->changes.pos = 1;
5563                   EC_CHANGED(ec);
5564                }
5565
5566              ELOGF("POSSIZE", "Configure pending geometry (%d,%d,%dx%d)", ec, ec->x, ec->y, ec->w, ec->h);
5567           }
5568      }
5569
5570    // cw interceptor(move,resize) won't work if wait_commit is TRUE
5571    ec->surface_sync.wait_commit = EINA_FALSE;
5572
5573    if ((ec->comp_data->shell.surface) &&
5574        (ec->comp_data->shell.configure))
5575      {
5576         ec->comp_data->shell.configure(ec->comp_data->shell.surface,
5577                                        ec->x, ec->y,
5578                                        ec->w, ec->h);
5579      }
5580
5581    // rollback wait_commit if there are pending requests remained
5582    if (eina_list_count(ec->surface_sync.pending_geometry))
5583      ec->surface_sync.wait_commit = EINA_TRUE;
5584
5585    return EINA_TRUE;
5586
5587 err:
5588    ELOGF("POSSIZE", "Could not configure geometry (%d,%d - %dx%d) bw:%d bh:%d", ec, ec->x, ec->y, ec->w, ec->h, bw, bh);
5589
5590 ret:
5591    return EINA_FALSE;
5592 }
5593
5594 static void
5595 _tz_move_resize_iface_cb_destroy(struct wl_client *client EINA_UNUSED,
5596                                  struct wl_resource *res_moveresize)
5597 {
5598    wl_resource_destroy(res_moveresize);
5599 }
5600
5601 static void
5602 _tz_move_resize_iface_cb_set_geometry(struct wl_client *client EINA_UNUSED,
5603                                       struct wl_resource *res_moveresize,
5604                                       struct wl_resource *surface,
5605                                       uint32_t serial,
5606                                       int32_t x,
5607                                       int32_t y,
5608                                       int32_t w,
5609                                       int32_t h)
5610 {
5611    /* to be implemented */
5612    E_Client *ec;
5613    Eina_Bool pending;
5614    int cur_w = 0, cur_h = 0;
5615
5616    ec = e_client_from_surface_resource(surface);
5617    if (!ec) return;
5618
5619    pending = e_client_pending_geometry_has(ec);
5620    e_client_geometry_get(ec, NULL, NULL, &cur_w, &cur_h);
5621    ELOGF("POSSIZE", "Request move_resize geometry_set. geo(%d,%d,%dx%d) cur(%dx%d). pend:%d", ec, x, y, w, h, cur_w, cur_h, pending);
5622
5623    if (!pending)
5624      {
5625         if ((cur_w == w) && (cur_h == h))
5626           {
5627              e_client_pos_set(ec, x, y);
5628              ec->changes.pos = 1;
5629              EC_CHANGED(ec);
5630              return;
5631           }
5632      }
5633
5634    if (!e_comp_wl_commit_sync_client_geometry_add(ec, serial, x, y, w, h)) goto err;
5635    return;
5636
5637 err:
5638    ELOGF("POSSIZE", "Could not add set_geometry request(serial:%d, %d,%d - %dx%d)", ec, serial, x, y, w, h);
5639 }
5640
5641 static const struct tizen_move_resize_interface _tz_move_resize_iface =
5642 {
5643    _tz_move_resize_iface_cb_destroy,
5644    _tz_move_resize_iface_cb_set_geometry,
5645 };
5646
5647 static void
5648 _tz_moveresize_cb_bind(struct wl_client *client,
5649                        void *data EINA_UNUSED,
5650                        uint32_t ver,
5651                        uint32_t id)
5652 {
5653    struct wl_resource *res_moveresize;
5654
5655    res_moveresize = wl_resource_create(client,
5656                                        &tizen_move_resize_interface,
5657                                        ver,
5658                                        id);
5659    EINA_SAFETY_ON_NULL_GOTO(res_moveresize, err);
5660
5661
5662    wl_resource_set_implementation(res_moveresize,
5663                                   &_tz_move_resize_iface,
5664                                   NULL,
5665                                   NULL);
5666    return;
5667
5668 err:
5669    ERR("Could not create tizen_move_resize_interface res: %m");
5670    wl_client_post_no_memory(client);
5671 }
5672
5673 static void
5674 _e_comp_wl_move_resize_init(void)
5675 {
5676    if (!e_comp_wl) return;
5677    if (!e_comp_wl->wl.disp) return;
5678
5679    if (!wl_global_create(e_comp_wl->wl.disp,
5680                          &tizen_move_resize_interface,
5681                          1,
5682                          NULL,
5683                          _tz_moveresize_cb_bind))
5684      {
5685         ERR("Could not create tizen_move_resize_interface to wayland globals: %m");
5686      }
5687
5688    return;
5689 }
5690
5691 EINTERN Eina_Bool
5692 e_comp_wl_pid_output_configured_resolution_send(pid_t pid, int w, int h)
5693 {
5694    E_Comp_Wl_Output *output;
5695    pid_t output_pid = 0;
5696    Eina_List *l = NULL, *l2 = NULL;
5697    struct wl_resource *resource = NULL;
5698
5699    if (!e_config->configured_output_resolution.use) return EINA_TRUE;
5700
5701    EINA_SAFETY_ON_TRUE_RETURN_VAL(pid <= 0, EINA_FALSE);
5702    EINA_SAFETY_ON_TRUE_RETURN_VAL(w < 0, EINA_FALSE);
5703    EINA_SAFETY_ON_TRUE_RETURN_VAL(h < 0, EINA_FALSE);
5704
5705    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5706      {
5707         /* if we have bound resources, send updates */
5708         EINA_LIST_FOREACH(output->resources, l2, resource)
5709           {
5710              wl_client_get_credentials(wl_resource_get_client(resource), &output_pid, NULL, NULL);
5711              if (output_pid != pid) continue;
5712
5713              ELOGF("COMP_WL", "\tSend Configured Output AGAIN ~!!!!! (pid:%d)", NULL, pid);
5714
5715              // send output information to the client
5716              _e_comp_wl_output_info_send(output, resource, pid, w, h);
5717           }
5718      }
5719
5720    return EINA_TRUE;
5721 }
5722
5723 EINTERN void
5724 e_comp_wl_surface_state_init(E_Comp_Wl_Surface_State *state, int w, int h)
5725 {
5726    _e_comp_wl_surface_state_init(state, w, h);
5727 }
5728
5729 EINTERN void
5730 e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
5731 {
5732    _e_comp_wl_surface_state_commit(ec, state);
5733 }
5734
5735 EINTERN void
5736 e_comp_wl_hook_call(E_Comp_Wl_Hook_Point hookpoint, E_Client *ec)
5737 {
5738    _e_comp_wl_hook_call(hookpoint, ec);
5739 }
5740
5741 EINTERN void
5742 e_comp_wl_surface_state_finish(E_Comp_Wl_Surface_State *state)
5743 {
5744    _e_comp_wl_surface_state_finish(state);
5745 }
5746
5747 EINTERN void
5748 e_comp_wl_surface_state_buffer_set(E_Comp_Wl_Surface_State *state, E_Comp_Wl_Buffer *buffer)
5749 {
5750    _e_comp_wl_surface_state_buffer_set(state, buffer);
5751 }
5752
5753 static void
5754 buffer_transform(int width, int height, uint32_t transform, int32_t scale,
5755                  int sx, int sy, int *dx, int *dy)
5756 {
5757    switch (transform)
5758      {
5759       case WL_OUTPUT_TRANSFORM_NORMAL:
5760       default:
5761          *dx = sx, *dy = sy;
5762          break;
5763       case WL_OUTPUT_TRANSFORM_FLIPPED:
5764          *dx = width - sx, *dy = sy;
5765          break;
5766       case WL_OUTPUT_TRANSFORM_90:
5767          *dx = height - sy, *dy = sx;
5768          break;
5769       case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5770          *dx = height - sy, *dy = width - sx;
5771          break;
5772       case WL_OUTPUT_TRANSFORM_180:
5773          *dx = width - sx, *dy = height - sy;
5774          break;
5775       case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5776          *dx = sx, *dy = height - sy;
5777          break;
5778       case WL_OUTPUT_TRANSFORM_270:
5779          *dx = sy, *dy = width - sx;
5780          break;
5781       case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5782          *dx = sy, *dy = sx;
5783          break;
5784      }
5785
5786    *dx *= scale;
5787    *dy *= scale;
5788 }
5789
5790 static void
5791 _buffer_viewport_get(E_Comp_Wl_Buffer_Viewport *vp, int bw, int bh, Eina_Rectangle *out)
5792 {
5793    int x1, y1, x2, y2;
5794    int tx1, ty1, tx2, ty2;
5795    int width_from_buffer, height_from_buffer;
5796
5797    switch (vp->buffer.transform)
5798      {
5799       case WL_OUTPUT_TRANSFORM_90:
5800       case WL_OUTPUT_TRANSFORM_270:
5801       case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5802       case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5803          width_from_buffer = bh / vp->buffer.scale;
5804          height_from_buffer = bw / vp->buffer.scale;
5805          break;
5806       default:
5807          width_from_buffer = bw / vp->buffer.scale;
5808          height_from_buffer = bh / vp->buffer.scale;
5809          break;
5810      }
5811
5812    if (vp->buffer.src_width == wl_fixed_from_int(-1))
5813      {
5814         x1 = 0.0;
5815         y1 = 0.0;
5816         x2 = width_from_buffer;
5817         y2 = height_from_buffer;
5818      }
5819    else
5820      {
5821         x1 = wl_fixed_to_int(vp->buffer.src_x);
5822         y1 = wl_fixed_to_int(vp->buffer.src_y);
5823         x2 = wl_fixed_to_int(vp->buffer.src_x + vp->buffer.src_width);
5824         y2 = wl_fixed_to_int(vp->buffer.src_y + vp->buffer.src_height);
5825      }
5826
5827    buffer_transform(width_from_buffer, height_from_buffer,
5828                     vp->buffer.transform, vp->buffer.scale, x1, y1, &tx1, &ty1);
5829    buffer_transform(width_from_buffer, height_from_buffer,
5830                     vp->buffer.transform, vp->buffer.scale, x2, y2, &tx2, &ty2);
5831
5832    out->x = (tx1 <= tx2) ? tx1 : tx2;
5833    out->y = (ty1 <= ty2) ? ty1 : ty2;
5834    out->w = (tx1 <= tx2) ? tx2 - tx1 : tx1 - tx2;
5835    out->h = (ty1 <= ty2) ? ty2 - ty1 : ty1 - ty2;
5836 }
5837
5838 /**
5839  * Convert given four coordinates to elements of rectangle
5840  * @in   p[4]        Coordinates to be converted
5841  * @out  rect        x, y, width, and height
5842  *       transform   Angle which represents TDM_TRANSFORM of rectangle
5843  * @return EINA_FALSE in following case, otherwise EINA_TRUE.
5844  *   1. The given coordinates are not represented by rectangle.
5845  *   2. All angles except for 0, 90, 180, 270.
5846  */
5847 static Eina_Bool
5848 _coords_to_rectangle_convert(Evas_Point p[4], Eina_Rectangle *rect, uint *rotation)
5849 {
5850    Eina_Bool ret = EINA_FALSE;
5851
5852    if ((p[0].y == p[1].y) && (p[1].x == p[2].x) && (p[2].y == p[3].y) && (p[3].x == p[0].x))
5853      {
5854         /* 0 or 180 */
5855         if ((p[0].x < p[2].x) && (p[0].y < p[2].y))
5856           {
5857              if (rotation)
5858                *rotation = TDM_TRANSFORM_NORMAL;
5859
5860              if (rect)
5861                EINA_RECTANGLE_SET(rect, p[0].x, p[0].y, p[2].x - p[0].x, p[2].y - p[0].y);
5862
5863              ret = EINA_TRUE;
5864           }
5865         else if ((p[0].x > p[2].x) && (p[0].y > p[2].y))
5866           {
5867              if (rotation)
5868                *rotation = TDM_TRANSFORM_180;
5869
5870              if (rect)
5871                EINA_RECTANGLE_SET(rect, p[2].x, p[2].y, p[0].x - p[2].x, p[0].y - p[2].y);
5872
5873              ret = EINA_TRUE;
5874           }
5875      }
5876    else if ((p[0].x == p[1].x) && (p[1].y == p[2].y) && (p[2].x == p[3].x) && (p[3].y == p[0].y))
5877      {
5878         /* 90 or 270 */
5879         if ((p[0].x > p[2].x) && (p[0].y < p[2].y))
5880           {
5881              if (rotation)
5882                *rotation = TDM_TRANSFORM_90;
5883
5884              if (rect)
5885                EINA_RECTANGLE_SET(rect, p[2].x, p[0].y, p[0].x - p[2].x, p[2].y - p[0].y);
5886
5887              ret = EINA_TRUE;
5888           }
5889         else if ((p[0].x < p[2].x) && (p[0].y > p[2].y))
5890           {
5891              if (rotation)
5892                *rotation = TDM_TRANSFORM_270;
5893
5894              if (rect)
5895                EINA_RECTANGLE_SET(rect, p[0].x, p[2].y, p[2].x - p[0].x, p[0].y - p[2].y);
5896
5897              ret = EINA_TRUE;
5898           }
5899      }
5900
5901    return ret;
5902 }
5903
5904 static Eina_Bool
5905 _output_viewport_get_from_evas_map(E_Map *m, Eina_Rectangle *out, unsigned int *rotation)
5906 {
5907    Evas_Point p[4];
5908    int i;
5909
5910    for (i = 0; i < 4; i++)
5911      e_map_point_coord_get(m, i, &p[i].x, &p[i].y, NULL);
5912
5913    if (!_coords_to_rectangle_convert(p, out, rotation))
5914      {
5915         DBG("Cannot convert given coords to rectangle.\n"
5916             "p1(%d %d) p2(%d %d) p3(%d %d) p4(%d %d)",
5917             p[0].x, p[0].y, p[1].x, p[1].y,
5918             p[2].x, p[2].y, p[3].x, p[3].y);
5919         return EINA_FALSE;
5920      }
5921
5922    return EINA_TRUE;
5923 }
5924
5925 static unsigned int
5926 _transform_merge_with_rotation(enum wl_output_transform transform, unsigned int rotation)
5927 {
5928    int trans_rotation, flip;
5929
5930    trans_rotation = transform & 0x3;
5931    flip = transform & 0x4;
5932
5933    return (flip + (trans_rotation + rotation) % 4);
5934 }
5935
5936 static void
5937 _e_comp_wl_surface_output_viewport_get(E_Client *ec, Eina_Rectangle *out)
5938 {
5939    if (!out)
5940      return;
5941
5942    if (!ec->comp_data)
5943      return;
5944
5945    if (e_comp_wl_subsurface_check(ec))
5946      e_comp_wl_subsurface_global_coord_get(ec, &out->x, &out->y);
5947    else
5948      {
5949         out->x = ec->x;
5950         out->y = ec->y;
5951      }
5952
5953    out->w = ec->comp_data->width_from_viewport;
5954    out->w = (out->w + 1) & ~1;
5955    out->h = ec->comp_data->height_from_viewport;
5956
5957    e_comp_object_frame_xy_unadjust(ec->frame, out->x, out->y, &out->x, &out->y);
5958    e_comp_object_frame_wh_unadjust(ec->frame, out->w, out->h, &out->w, &out->h);
5959 }
5960
5961 EINTERN Eina_Bool
5962 e_comp_wl_surface_viewport_get(E_Client *ec, Eina_Rectangle *buffer_viewport, Eina_Rectangle *output_viewport, unsigned int *transform)
5963 {
5964    E_Comp_Wl_Buffer *buffer;
5965    E_Comp_Wl_Buffer_Viewport *vp;
5966    E_Map *m;
5967    enum wl_output_transform buffer_transform;
5968    unsigned int rotation;
5969    Eina_Bool res = EINA_FALSE;
5970
5971    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5972
5973    if (e_object_is_del(E_OBJECT(ec)))
5974      return EINA_FALSE;
5975
5976    vp = &ec->comp_data->scaler.buffer_viewport;
5977
5978    if (buffer_viewport)
5979      {
5980         buffer = ec->comp_data->buffer_ref.buffer;
5981         /* Getting a viewport of buffer needs geometry of buffer. */
5982         if (!buffer)
5983           return EINA_FALSE;
5984
5985         _buffer_viewport_get(vp, buffer->w, buffer->h, buffer_viewport);
5986      }
5987
5988    if ((output_viewport) || (transform))
5989      {
5990         m = e_client_map_get(ec);
5991         if (m)
5992           res = _output_viewport_get_from_evas_map(m, output_viewport, &rotation);
5993         else
5994           _e_comp_wl_surface_output_viewport_get(ec, output_viewport);
5995         e_map_free(m);
5996
5997         if (transform)
5998           {
5999              buffer_transform = e_comp_wl_output_buffer_transform_get(ec);
6000              /* NOTE Merge transform value from evas_map with E_Comp_Wl_Buffer_Viewport's one.
6001               * Since buffer.transform isn't applied using evas_map,
6002               * it has to be taken into account here to apply buffer.transform
6003               * and rotation of e_client_transform together. */
6004              if (res)
6005                *transform = _transform_merge_with_rotation(buffer_transform, rotation);
6006              else
6007                *transform = (unsigned int)buffer_transform;
6008           }
6009      }
6010
6011    return EINA_TRUE;
6012 }
6013
6014 EINTERN Eina_Bool
6015 e_comp_wl_surface_role_set(E_Client *ec, const char *role_name, struct wl_resource *error_resource, uint32_t error_code)
6016 {
6017    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
6018    EINA_SAFETY_ON_NULL_RETURN_VAL(role_name, EINA_FALSE);
6019
6020    /* Must be called with valid comp_data */
6021    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
6022
6023    if ((ec->comp_data->role_name == NULL) ||
6024        (ec->comp_data->role_name == role_name) ||
6025        (strcmp(ec->comp_data->role_name, role_name) == 0))
6026      {
6027         ec->comp_data->role_name = role_name;
6028         return EINA_TRUE;
6029      }
6030
6031    if (error_resource)
6032      {
6033         wl_resource_post_error(error_resource, error_code,
6034                                "Cannot assign role %s to wl_surface@%d,"
6035                                " already has role %s\n",
6036                                role_name,
6037                                ec->comp_data->wl_surface ?
6038                                wl_resource_get_id(ec->comp_data->wl_surface) : -1,
6039                                ec->comp_data->role_name);
6040      }
6041
6042    return EINA_FALSE;
6043 }
6044
6045 EINTERN const char *
6046 e_comp_wl_surface_role_get(E_Client *ec)
6047 {
6048    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
6049    return ec->comp_data ? ec->comp_data->role_name : NULL;
6050 }
6051
6052 EINTERN struct wl_resource *
6053 e_comp_wl_client_surface_get(E_Client *ec)
6054 {
6055    struct wl_resource *surface = NULL;
6056    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
6057    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, NULL);
6058
6059    g_mutex_lock(&ec->comp_data->surface_mutex);
6060    surface = ec->comp_data->surface;
6061    g_mutex_unlock(&ec->comp_data->surface_mutex);
6062
6063    return surface;
6064 }
6065
6066 EINTERN void e_comp_wl_client_surface_set(E_Client *ec, struct wl_resource *surface)
6067 {
6068    EINA_SAFETY_ON_NULL_RETURN(ec);
6069    EINA_SAFETY_ON_NULL_RETURN(ec->comp_data);
6070
6071    g_mutex_lock(&ec->comp_data->surface_mutex);
6072    ec->comp_data->surface = surface;
6073    g_mutex_unlock(&ec->comp_data->surface_mutex);
6074 }
6075
6076 EINTERN E_Client *
6077 e_comp_wl_util_client_from_surface_resource(struct wl_resource *surface_resource)
6078 {
6079    return e_compositor_util_client_from_surface_resource(surface_resource);
6080 }
6081
6082 EINTERN void
6083 e_comp_wl_client_surface_pending_buffer_set(E_Client *ec, E_Comp_Wl_Buffer *buffer, int32_t sx, int32_t sy)
6084 {
6085    if (!ec->comp_data->mapped)
6086      {
6087         if (ec->comp_data->shell.surface &&
6088             !ec->internal && !e_comp_wl_subsurface_check(ec) && !ec->remote_surface.provider)
6089           {
6090              ELOGF("COMP", "Current unmapped. ATTACH buffer:%p", ec, buffer);
6091           }
6092      }
6093
6094    if (!buffer)
6095      {
6096         if (ec->comp_data->mapped)
6097           {
6098              /* will be unmapped. so run capture */
6099              e_comp_wl_remote_surface_image_save(ec);
6100           }
6101      }
6102
6103    _e_comp_wl_surface_state_buffer_set(&ec->comp_data->pending, buffer);
6104
6105    ec->comp_data->pending.sx = sx;
6106    ec->comp_data->pending.sy = sy;
6107    ec->comp_data->pending.new_attach = EINA_TRUE;
6108
6109    e_client_fps_update(ec);
6110 }
6111
6112 EINTERN void
6113 e_comp_wl_client_surface_pending_opaque_region_set(E_Client *ec, Eina_Tiler *region)
6114 {
6115    if (ec->comp_data->pending.opaque)
6116      eina_tiler_clear(ec->comp_data->pending.opaque);
6117
6118    if (region)
6119      {
6120         eina_tiler_union(ec->comp_data->pending.opaque, region);
6121
6122         if (!eina_tiler_empty(ec->comp_data->pending.opaque))
6123           {
6124              if (ec->argb)
6125                {
6126                   ec->argb = EINA_FALSE;
6127                   ELOGF("COMP", "Set argb:%d", ec, ec->argb);
6128                   EC_CHANGED(ec);
6129                   _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_ALPHA_CHANGE, ec);
6130                   e_comp_object_alpha_set(ec->frame, EINA_FALSE);
6131                }
6132           }
6133      }
6134    else
6135      {
6136         if (!ec->argb)
6137           {
6138              ec->argb = EINA_TRUE;
6139              ELOGF("COMP", "Set argb:%d", ec, ec->argb);
6140              EC_CHANGED(ec);
6141              _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_ALPHA_CHANGE, ec);
6142              e_comp_object_alpha_set(ec->frame, EINA_TRUE);
6143           }
6144      }
6145 }
6146
6147 EINTERN void
6148 e_comp_wl_client_surface_pending_input_region_set(E_Client *ec, Eina_Tiler *region)
6149 {
6150    if (ec->comp_data->pending.input)
6151      eina_tiler_clear(ec->comp_data->pending.input);
6152
6153    if (region)
6154      {
6155         if (eina_tiler_empty(region))
6156           {
6157              ELOGF("COMP", "         |unset input rect", NULL);
6158              e_comp_object_input_objs_del(ec->frame);
6159              e_comp_object_input_area_set(ec->frame, -1, -1, 1, 1);
6160           }
6161         else
6162           eina_tiler_union(ec->comp_data->pending.input, region);
6163      }
6164    else
6165      {
6166         eina_tiler_rect_add(ec->comp_data->pending.input,
6167                             &(Eina_Rectangle){0, 0, ec->client.w, ec->client.h});
6168      }
6169 }
6170
6171 EINTERN void
6172 e_comp_wl_client_surface_pending_frame_callback_add(E_Client *ec, struct wl_resource *callback_resource)
6173 {
6174    ec->comp_data->pending.frames = eina_list_prepend(ec->comp_data->pending.frames, callback_resource);
6175 }
6176
6177 EINTERN void
6178 e_comp_wl_client_surface_frame_callback_remove(E_Client *ec, struct wl_resource *callback_resource)
6179 {
6180    E_Comp_Wl_Subsurf_Data *sdata;
6181
6182    if (!ec->comp_data)
6183      return;
6184
6185    if (ec->comp_data->frames)
6186      {
6187         ec->comp_data->frames =
6188            eina_list_remove(ec->comp_data->frames, callback_resource);
6189      }
6190
6191    if (ec->comp_data->pending.frames)
6192      {
6193         ec->comp_data->pending.frames =
6194            eina_list_remove(ec->comp_data->pending.frames, callback_resource);
6195      }
6196
6197    sdata = ec->comp_data->sub.data;
6198    if ((sdata) && (sdata->cached.frames))
6199      {
6200         sdata->cached.frames =
6201            eina_list_remove(sdata->cached.frames, callback_resource);
6202      }
6203 }
6204
6205 EINTERN void
6206 e_comp_wl_client_surface_pending_commit(E_Client *ec)
6207 {
6208    if (e_object_is_del(E_OBJECT(ec))) return;
6209
6210    if (e_comp_wl_remote_surface_commit(ec)) return;
6211
6212    e_comp_wl_surface_commit(ec);
6213
6214    _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_SURFACE_COMMIT, ec);
6215 }
6216
6217 static void
6218 _e_comp_wl_client_subsurface_set(E_Client *ec, E_Comp_Wl_Subsurf_Data *sub)
6219 {
6220    e_comp_wl_client_surface_set(ec, ec->comp_data->wl_surface);
6221    ec->comp_data->sub.data = sub;
6222
6223    /* set subsurface client properties */
6224    ec->borderless = EINA_TRUE;
6225    ec->argb = EINA_TRUE;
6226    ELOGF("SUBSURFACE", "Set argb:%d", ec, ec->argb);
6227    ec->lock_border = EINA_TRUE;
6228    ec->lock_focus_in = ec->lock_focus_out = EINA_TRUE;
6229    ec->netwm.state.skip_taskbar = EINA_TRUE;
6230    ec->netwm.state.skip_pager = EINA_TRUE;
6231    ec->no_shape_cut = EINA_TRUE;
6232    ec->border_size = 0;
6233    ec->lock_user_location = 0;
6234    ec->lock_client_location = 0;
6235    ec->lock_user_size = 0;
6236    ec->lock_client_size = 0;
6237    ec->lock_client_stacking = 0;
6238    ec->lock_user_shade = 0;
6239    ec->lock_client_shade = 0;
6240    ec->lock_user_maximize = 0;
6241    ec->lock_client_maximize = 0;
6242    ec->changes.need_maximize = 0;
6243    ec->maximized = E_MAXIMIZE_NONE;
6244    EC_CHANGED(ec);
6245
6246    ec->new_client = ec->netwm.ping = EINA_TRUE;
6247    e_comp->new_clients++;
6248    e_client_unignore(ec);
6249
6250    /* Delete 'below_obj' if it was created before 'E_Client' becomes subsurface.
6251     * It's not for subsurface. */
6252    E_FREE_FUNC(ec->comp_data->sub.below_obj, evas_object_del);
6253 }
6254
6255 static void
6256 _e_comp_wl_client_subsurface_parent_set(E_Client *ec, E_Client *parent)
6257 {
6258    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
6259    E_Layer layer;
6260
6261    ELOGF("COMP", "         |subsurface_parent:%p", ec, parent);
6262
6263    sub->parent = parent;
6264
6265    if (parent->frame)
6266      {
6267         layer = e_client_layer_get(parent);
6268         if (layer > E_LAYER_BOTTOM)
6269           e_client_layer_set(ec, layer);
6270      }
6271
6272    if (parent->comp_data)
6273      {
6274         /* append this client to the parents subsurface list */
6275         parent->comp_data->sub.list_pending =
6276            eina_list_append(parent->comp_data->sub.list_pending, ec);
6277         parent->comp_data->sub.list_changed = EINA_TRUE;
6278      }
6279 }
6280
6281 EINTERN void
6282 e_comp_wl_client_subsurface_parent_unset(E_Client *ec)
6283 {
6284    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
6285    E_Client *parent = sub->parent;
6286
6287    parent->comp_data->sub.list =
6288       eina_list_remove(parent->comp_data->sub.list, ec);
6289    parent->comp_data->sub.list_pending =
6290       eina_list_remove(parent->comp_data->sub.list_pending, ec);
6291    parent->comp_data->sub.below_list =
6292       eina_list_remove(parent->comp_data->sub.below_list, ec);
6293    parent->comp_data->sub.below_list_pending =
6294       eina_list_remove(parent->comp_data->sub.below_list_pending, ec);
6295
6296    sub->parent = NULL;
6297 }
6298
6299 EINTERN void
6300 e_comp_wl_client_subsurface_init(E_Client *ec, struct wl_resource *subsurface_resource, E_Comp_Wl_Subsurf_Data *sub, E_Client *parent, E_Client *offscreen_parent)
6301 {
6302    sub->resource = subsurface_resource;
6303    sub->cached_buffer_ref.buffer = NULL;
6304    sub->synchronized = EINA_TRUE;
6305    sub->parent = parent;
6306    sub->remote_surface.offscreen_parent = offscreen_parent;
6307    if (offscreen_parent)
6308      ELOGF("SUBSURFACE", "         |offscreen_parent:%p", ec, offscreen_parent);
6309
6310    _e_comp_wl_surface_state_init(&sub->cached, ec->w, ec->h);
6311
6312    _e_comp_wl_client_subsurface_set(ec, sub);
6313    _e_comp_wl_client_subsurface_parent_set(ec, parent);
6314
6315    e_comp_wl_hook_call(E_COMP_WL_HOOK_SUBSURFACE_CREATE, ec);
6316 }
6317
6318 EINTERN void
6319 e_comp_wl_client_subsurface_finish(E_Client *ec)
6320 {
6321    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
6322
6323    if (sub->parent)
6324      e_comp_wl_client_subsurface_parent_unset(ec);
6325
6326    e_comp_wl_surface_state_finish(&sub->cached);
6327    e_comp_wl_buffer_reference(&sub->cached_buffer_ref, NULL);
6328
6329    ec->comp_data->sub.data = NULL;
6330 }
6331
6332 EINTERN void
6333 e_comp_wl_client_evas_init(E_Client *ec)
6334 {
6335    EINA_SAFETY_ON_NULL_RETURN(ec);
6336
6337    _e_comp_wl_client_evas_init(ec);
6338 }
6339
6340 EINTERN void
6341 e_comp_wl_client_evas_deinit(E_Client *ec)
6342 {
6343    EINA_SAFETY_ON_NULL_RETURN(ec);
6344
6345    _e_comp_wl_client_evas_deinit(ec);
6346 }
6347
6348 EINTERN void
6349 e_comp_wl_focus_check(void)
6350 {
6351    _e_comp_wl_focus_check();
6352 }
6353
6354 EINTERN Eina_Bool
6355 e_comp_wl_client_subsurface_cyclic_reference_check(E_Client *ec, E_Client *parent)
6356 {
6357    while (parent)
6358      {
6359         if (ec == parent)
6360           return EINA_TRUE;
6361
6362         if ((parent->comp_data) && (parent->comp_data->sub.data))
6363           parent = parent->comp_data->sub.data->parent;
6364         else
6365           break;
6366      }
6367
6368    return EINA_FALSE;
6369 }