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