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