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