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