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