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