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