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