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