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