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