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