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