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