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