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