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