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