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