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