e_comp_wl: remove "delete_request" smart call
[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_evas_cb_kill_request(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2212 {
2213    E_Client *ec;
2214
2215    if (!(ec = data)) return;
2216
2217    e_comp_ignore_win_del(E_PIXMAP_TYPE_WL, e_pixmap_window_get(ec->pixmap));
2218    if (ec->comp_data)
2219      {
2220         if (ec->comp_data->reparented)
2221           e_client_comp_hidden_set(ec, EINA_TRUE);
2222      }
2223
2224    evas_object_pass_events_set(ec->frame, EINA_TRUE);
2225    if (ec->visible) evas_object_hide(ec->frame);
2226    if (!ec->internal) e_object_del(E_OBJECT(ec));
2227
2228    _e_comp_wl_focus_check();
2229 }
2230
2231 static void
2232 _e_comp_wl_evas_cb_ping(void *data, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
2233 {
2234    E_Client *ec;
2235
2236    if (!(ec = data)) return;
2237
2238    e_client_shell_ping(ec);
2239 }
2240
2241 static void
2242 _e_comp_wl_evas_cb_color_set(void *data, Evas_Object *obj, void *event EINA_UNUSED)
2243 {
2244    E_Client *ec;
2245    int a = 0;
2246
2247    if (!(ec = data)) return;
2248    evas_object_color_get(obj, NULL, NULL, NULL, &a);
2249    if (ec->netwm.opacity == a) return;
2250    ec->netwm.opacity = a;
2251    ec->netwm.opacity_changed = EINA_TRUE;
2252 }
2253
2254 static void
2255 _e_comp_wl_buffer_damage_set(E_Comp_Wl_Buffer *buffer, Eina_List *buffer_damages)
2256 {
2257    Eina_Rectangle *damage_rect = NULL;
2258    Eina_Rectangle *dmg = NULL;
2259    Eina_List *l = NULL;
2260
2261    if (buffer->type != E_COMP_WL_BUFFER_TYPE_NATIVE &&
2262        buffer->type != E_COMP_WL_BUFFER_TYPE_TBM)
2263      return;
2264
2265    if (!buffer->tbm_surface) return;
2266
2267    if (buffer_damages)
2268      {
2269         EINA_LIST_FOREACH(buffer_damages, l, dmg)
2270           {
2271              if (!damage_rect)
2272                {
2273                   damage_rect = eina_rectangle_new(dmg->x, dmg->y, dmg->w, dmg->h);
2274                   EINA_SAFETY_ON_FALSE_RETURN(damage_rect);
2275                }
2276              else
2277                eina_rectangle_union(damage_rect, dmg);
2278           }
2279      }
2280    else
2281      {
2282         damage_rect = eina_rectangle_new(0, 0, buffer->w, buffer->h);
2283         EINA_SAFETY_ON_FALSE_RETURN(damage_rect);
2284      }
2285
2286    tbm_surface_internal_set_damage(buffer->tbm_surface,
2287                                    damage_rect->x,
2288                                    damage_rect->y,
2289                                    damage_rect->w,
2290                                    damage_rect->h);
2291
2292    eina_rectangle_free(damage_rect);
2293 }
2294
2295 static void
2296 _e_comp_wl_client_evas_init(E_Client *ec)
2297 {
2298    if (!ec || !ec->comp_data) return;
2299    if (ec->comp_data->evas_init) return;
2300
2301    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_SHOW,        _e_comp_wl_evas_cb_show,        ec);
2302    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_HIDE,        _e_comp_wl_evas_cb_hide,        ec);
2303    evas_object_event_callback_add(ec->frame, EVAS_CALLBACK_MOVE,        _e_comp_wl_evas_cb_move,        ec);
2304
2305
2306    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_IN,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_in,    ec);
2307    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_OUT,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_out,   ec);
2308    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_MOVE,  EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_move,  ec);
2309    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_DOWN,  EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_down,  ec);
2310    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_UP,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_up,    ec);
2311    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MOUSE_WHEEL, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_mouse_wheel, ec);
2312
2313    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_DOWN, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_down, ec);
2314    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_UP,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_up,   ec);
2315    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_MULTI_MOVE, EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_multi_move, ec);
2316
2317    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_FOCUS_IN,    EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_focus_in,    ec);
2318    evas_object_event_callback_priority_add(ec->frame, EVAS_CALLBACK_FOCUS_OUT,   EVAS_CALLBACK_PRIORITY_AFTER, _e_comp_wl_evas_cb_focus_out,   ec);
2319
2320    /* setup delete/kill callbacks */
2321    evas_object_smart_callback_add(ec->frame, "kill_request",   _e_comp_wl_evas_cb_kill_request,   ec);
2322
2323    /* setup ping callback */
2324    evas_object_smart_callback_add(ec->frame, "ping",           _e_comp_wl_evas_cb_ping,           ec);
2325    evas_object_smart_callback_add(ec->frame, "color_set",      _e_comp_wl_evas_cb_color_set,      ec);
2326
2327    ec->comp_data->evas_init = EINA_TRUE;
2328 }
2329
2330 static void
2331 _e_comp_wl_client_evas_deinit(E_Client *ec)
2332 {
2333    if (!ec || !ec->comp_data) return;
2334    if (!ec->comp_data->evas_init) return;
2335
2336    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_SHOW, _e_comp_wl_evas_cb_show);
2337    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_HIDE, _e_comp_wl_evas_cb_hide);
2338    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOVE, _e_comp_wl_evas_cb_move);
2339
2340    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_IN,    _e_comp_wl_evas_cb_mouse_in);
2341    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_OUT,   _e_comp_wl_evas_cb_mouse_out);
2342    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_MOVE,  _e_comp_wl_evas_cb_mouse_move);
2343    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_DOWN,  _e_comp_wl_evas_cb_mouse_down);
2344    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_UP,    _e_comp_wl_evas_cb_mouse_up);
2345    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MOUSE_WHEEL, _e_comp_wl_evas_cb_mouse_wheel);
2346
2347    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_DOWN, _e_comp_wl_evas_cb_multi_down);
2348    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_UP,   _e_comp_wl_evas_cb_multi_up);
2349    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_MULTI_MOVE, _e_comp_wl_evas_cb_multi_move);
2350
2351    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_FOCUS_IN,  _e_comp_wl_evas_cb_focus_in);
2352    evas_object_event_callback_del(ec->frame, EVAS_CALLBACK_FOCUS_OUT, _e_comp_wl_evas_cb_focus_out);
2353
2354    evas_object_smart_callback_del(ec->frame, "kill_request",   _e_comp_wl_evas_cb_kill_request);
2355
2356    evas_object_smart_callback_del(ec->frame, "ping",           _e_comp_wl_evas_cb_ping);
2357    evas_object_smart_callback_del(ec->frame, "color_set",      _e_comp_wl_evas_cb_color_set);
2358
2359    ec->comp_data->evas_init = EINA_FALSE;
2360 }
2361
2362 static Eina_Bool
2363 _e_comp_wl_cb_randr_change(void *data EINA_UNUSED, int type EINA_UNUSED, void *event EINA_UNUSED)
2364 {
2365    Eina_List *l;
2366    E_Output *eout;
2367    E_Comp_Screen *e_comp_screen;
2368    unsigned int transform = WL_OUTPUT_TRANSFORM_NORMAL;
2369
2370    if (!e_comp) return ECORE_CALLBACK_RENEW;
2371    if (!e_comp->e_comp_screen) return ECORE_CALLBACK_RENEW;
2372    e_comp_screen = e_comp->e_comp_screen;
2373
2374    EINA_LIST_FOREACH(e_comp_screen->outputs, l, eout)
2375      {
2376         if (!eout->config.enabled)
2377           {
2378              e_comp_wl_output_remove(eout->id);
2379              continue;
2380           }
2381
2382         switch (eout->config.rotation)
2383           {
2384            case 90:
2385              transform = WL_OUTPUT_TRANSFORM_90;
2386              break;
2387            case 180:
2388              transform = WL_OUTPUT_TRANSFORM_180;
2389              break;
2390            case 270:
2391              transform = WL_OUTPUT_TRANSFORM_270;
2392              break;
2393            case 0:
2394            default:
2395              transform = WL_OUTPUT_TRANSFORM_NORMAL;
2396              break;
2397           }
2398
2399         if (!e_comp_wl_output_init(eout->id, eout->info.name,
2400                                    eout->info.screen,
2401                                    eout->config.geom.x, eout->config.geom.y,
2402                                    eout->config.geom.w, eout->config.geom.h,
2403                                    eout->info.size.w, eout->info.size.h,
2404                                    eout->config.mode.refresh, 0, transform))
2405           ERR("Could not initialize screen %s", eout->info.name);
2406      }
2407
2408    return ECORE_CALLBACK_RENEW;
2409 }
2410
2411 static Eina_Bool
2412 _e_comp_wl_cb_comp_object_add(void *data EINA_UNUSED, int type EINA_UNUSED, E_Event_Comp_Object *ev)
2413 {
2414    E_Client *ec;
2415
2416    /* try to get the client from the object */
2417    if (!(ec = e_comp_object_client_get(ev->comp_object)))
2418      return ECORE_CALLBACK_RENEW;
2419
2420    /* check for client being deleted */
2421    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_RENEW;
2422
2423    /* check for wayland pixmap */
2424    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL)
2425      return ECORE_CALLBACK_RENEW;
2426
2427    /* if we have not setup evas callbacks for this client, do it */
2428    if (!ec->comp_data->evas_init) _e_comp_wl_client_evas_init(ec);
2429
2430    return ECORE_CALLBACK_RENEW;
2431 }
2432
2433 static Eina_Bool
2434 _e_comp_wl_cb_mouse_move(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Move *ev)
2435 {
2436    int ec_x, ec_y;
2437
2438    e_comp_wl->ptr.x = wl_fixed_from_int(ev->x);
2439    e_comp_wl->ptr.y = wl_fixed_from_int(ev->y);
2440
2441    if (e_comp_wl->selection.target &&
2442        e_comp_wl->drag)
2443      {
2444         struct wl_resource *res;
2445         int x, y;
2446         E_Client *ec = NULL;
2447         E_Client *legacy_target = e_comp_wl->selection.target;
2448         int device_id = e_comp_wl_data_current_device_id_get();
2449
2450         if (device_id < 0)
2451           {
2452              e_comp_wl_data_current_device_id_set(ev->multi.device);
2453           }
2454         else if (device_id != ev->multi.device)
2455           {
2456              return ECORE_CALLBACK_RENEW;
2457           }
2458
2459         ec = e_client_under_position_input_get(legacy_target->desk, ev->x, ev->y);
2460         EINA_SAFETY_ON_NULL_RETURN_VAL(ec, ECORE_CALLBACK_RENEW);
2461
2462         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
2463         EINA_SAFETY_ON_NULL_RETURN_VAL(surface, ECORE_CALLBACK_RENEW);
2464
2465         res = e_comp_wl_data_find_for_client(wl_resource_get_client(surface));
2466         EINA_SAFETY_ON_NULL_RETURN_VAL(res, ECORE_CALLBACK_RENEW);
2467
2468         if ((e_comp_wl->drag_offer != wl_resource_get_user_data(res)) &&
2469             (ec != legacy_target))
2470           {
2471              e_comp_wl_data_device_send_leave(legacy_target);
2472              e_comp_wl_data_device_send_enter(ec);
2473           }
2474
2475         if (e_comp_wl->drag)
2476           e_drag_move(e_comp_wl->drag, ev->x, ev->y);
2477
2478         if (e_client_transform_core_enable_get(ec))
2479           {
2480              int trans_x, trans_y;
2481              e_client_transform_core_input_transform(ec, ev->x, ev->y, &trans_x, &trans_y);
2482              x = trans_x - ec->client.x;
2483              y = trans_y - ec->client.y;
2484           }
2485         else
2486           {
2487              e_client_geometry_get(ec, &ec_x, &ec_y, NULL, NULL);
2488              x = ev->x - ec_x;
2489              y = ev->y - ec_y;
2490           }
2491
2492         wl_data_device_send_motion(res, ev->timestamp, wl_fixed_from_int(x), wl_fixed_from_int(y));
2493      }
2494
2495    return ECORE_CALLBACK_RENEW;
2496 }
2497
2498 static Eina_Bool
2499 _e_comp_wl_cb_mouse_relative_move(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Relative_Move *ev)
2500 {
2501    E_Client *ec;
2502    struct wl_resource *surface;
2503    struct wl_resource *res;
2504    struct wl_client *wc;
2505    Eina_List *l;
2506    E_Comp_Config *comp_conf;
2507
2508    ec = e_comp_wl->ptr_constraints.ec;
2509    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, ECORE_CALLBACK_RENEW);
2510
2511    surface = e_comp_wl_client_surface_get(ec);
2512    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, ECORE_CALLBACK_RENEW);
2513
2514    wc = wl_resource_get_client(surface);
2515
2516    comp_conf = e_comp_config_get();
2517
2518    EINA_LIST_FOREACH(e_comp_wl->relative_ptr.resources, l, res)
2519      {
2520         if (!e_comp_wl_input_relative_pointer_check(res)) continue;
2521         if (wl_resource_get_client(res) != wc) continue;
2522
2523         if (comp_conf && comp_conf->input_log_enable)
2524           ELOGF("Mouse", "Relative Move (time: %d, dx:%d dy:%d, unaccel(%d, %d) name:%20s)",
2525                 ec, ev->timestamp, ev->dx, ev->dy, ev->dx_unaccel, ev->dy_unaccel,
2526                 e_client_util_name_get(ec));
2527
2528         zwp_relative_pointer_v1_send_relative_motion(res,
2529                                                      0,
2530                                                      (uint32_t)(ev->timestamp),
2531                                                      wl_fixed_from_int(ev->dx),
2532                                                      wl_fixed_from_int(ev->dy),
2533                                                      wl_fixed_from_int(ev->dx_unaccel),
2534                                                      wl_fixed_from_int(ev->dy_unaccel));
2535      }
2536
2537    return ECORE_CALLBACK_DONE;
2538 }
2539
2540 static Eina_Bool
2541 _e_comp_wl_cb_mouse_button_cancel(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Mouse_Button *ev)
2542 {
2543    _e_comp_wl_touch_cancel();
2544
2545    return ECORE_CALLBACK_PASS_ON;
2546 }
2547
2548 static Eina_Bool
2549 _e_comp_wl_cb_zone_display_state_change(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Zone_Display_State_Change *ev)
2550 {
2551    if (!ev) return ECORE_CALLBACK_PASS_ON;
2552
2553    E_Zone *zone = ev->zone;
2554
2555    E_OBJECT_CHECK_RETURN(zone, ECORE_CALLBACK_PASS_ON);
2556    E_OBJECT_TYPE_CHECK_RETURN(zone, E_ZONE_TYPE, ECORE_CALLBACK_PASS_ON);
2557
2558    if (!e_zone_is_displaying(zone))
2559      _e_comp_wl_touch_cancel();
2560
2561    return ECORE_CALLBACK_PASS_ON;
2562 }
2563
2564 static Eina_Bool
2565 _e_comp_wl_cb_client_rot_change_begin(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Client_Rotation_Change_Begin *ev)
2566 {
2567    E_Client *ec = ev->ec;
2568    E_Comp_Wl_Buffer_Viewport *vp;
2569
2570    if (!ec) return ECORE_CALLBACK_PASS_ON;
2571    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_PASS_ON;
2572    if (!ec->comp_data) return ECORE_CALLBACK_PASS_ON;
2573    if (e_comp_wl_subsurface_check(ec)) return ECORE_CALLBACK_PASS_ON;
2574    if (ec->e.state.rot.ang.next < 0) return ECORE_CALLBACK_PASS_ON;
2575
2576    vp = &ec->comp_data->scaler.buffer_viewport;
2577    vp->wait_for_transform_change = ((360 + ec->e.state.rot.ang.next - ec->e.state.rot.ang.curr) % 360) / 90;
2578
2579    DBG("ec(%p) wait_for_transform_change(%d)", ec, vp->wait_for_transform_change);
2580
2581    return ECORE_CALLBACK_PASS_ON;
2582 }
2583
2584 static Eina_Bool
2585 _e_comp_wl_cb_client_rot_change_cancel(void *d EINA_UNUSED, int t EINA_UNUSED, E_Event_Client_Rotation_Change_Cancel *ev)
2586 {
2587    E_Client *ec = ev->ec;
2588    E_Comp_Wl_Buffer_Viewport *vp;
2589
2590    if (!ec) return ECORE_CALLBACK_PASS_ON;
2591    if (e_object_is_del(E_OBJECT(ec))) return ECORE_CALLBACK_PASS_ON;
2592    if (!ec->comp_data) return ECORE_CALLBACK_PASS_ON;
2593    if (e_comp_wl_subsurface_check(ec)) return ECORE_CALLBACK_PASS_ON;
2594
2595    vp = &ec->comp_data->scaler.buffer_viewport;
2596    vp->wait_for_transform_change = 0;
2597
2598    DBG("ec(%p) wait_for_transform_change(%d) reset", ec, vp->wait_for_transform_change);
2599
2600    return ECORE_CALLBACK_PASS_ON;
2601 }
2602
2603 static Eina_Bool
2604 _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)
2605 {
2606    E_Client *focused_ec;
2607    int rotation;
2608
2609    focused_ec = e_client_focused_get();
2610    if (!focused_ec) return ECORE_CALLBACK_PASS_ON;
2611
2612    rotation = focused_ec->e.state.rot.ang.curr;
2613    e_pointer_rotation_set(e_comp->pointer, rotation);
2614
2615    return ECORE_CALLBACK_PASS_ON;
2616 }
2617
2618 static void
2619 _e_comp_wl_surface_state_size_update(E_Client *ec, E_Comp_Wl_Surface_State *state)
2620 {
2621    int prev_w, prev_h;
2622    Eina_Rectangle *window;
2623
2624    prev_w = state->bw;
2625    prev_h = state->bh;
2626
2627    if (!e_pixmap_size_get(ec->pixmap, &state->bw, &state->bh)) return;
2628
2629    if ((prev_w != state->bw) ||
2630        (prev_h != state->bh))
2631      {
2632         ec->changes.buf_size = EINA_TRUE;
2633      }
2634
2635    if (e_comp_object_frame_exists(ec->frame)) return;
2636    window = &ec->comp_data->shell.window;
2637    if ((!ec->borderless) && /* FIXME temporarily added this check code
2638                              * to prevent updating E_Client's size by frame */
2639        (window->x || window->y || window->w || window->h))
2640      {
2641         e_comp_object_frame_geometry_set(ec->frame,
2642                                          -window->x,
2643                                          (window->x + window->w) - state->bw,
2644                                          -window->y,
2645                                          (window->y + window->h) - state->bh);
2646      }
2647    else
2648      e_comp_object_frame_geometry_set(ec->frame, 0, 0, 0, 0);
2649 }
2650
2651 static void
2652 _e_comp_wl_surface_state_cb_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
2653 {
2654    E_Comp_Wl_Surface_State *state;
2655
2656    state =
2657      container_of(listener, E_Comp_Wl_Surface_State, buffer_destroy_listener);
2658    state->buffer = NULL;
2659 }
2660
2661 static void
2662 _e_comp_wl_surface_state_init(E_Comp_Wl_Surface_State *state, int w, int h)
2663 {
2664    state->new_attach = EINA_FALSE;
2665    state->buffer = NULL;
2666    state->buffer_destroy_listener.notify =
2667      _e_comp_wl_surface_state_cb_buffer_destroy;
2668    state->sx = state->sy = 0;
2669
2670    state->input = eina_tiler_new(w, h);
2671    eina_tiler_tile_size_set(state->input, 1, 1);
2672
2673    state->opaque = eina_tiler_new(w, h);
2674    eina_tiler_tile_size_set(state->opaque, 1, 1);
2675
2676    state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
2677    state->buffer_viewport.buffer.scale = 1;
2678    state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
2679    state->buffer_viewport.surface.width = -1;
2680    state->buffer_viewport.changed = 0;
2681
2682    e_presentation_time_container_init(&state->presentation_container);
2683 }
2684
2685 static void
2686 _e_comp_wl_surface_state_finish(E_Comp_Wl_Surface_State *state)
2687 {
2688    struct wl_resource *cb;
2689    Eina_Rectangle *dmg;
2690
2691    EINA_LIST_FREE(state->frames, cb)
2692      wl_resource_destroy(cb);
2693
2694    EINA_LIST_FREE(state->damages, dmg)
2695      eina_rectangle_free(dmg);
2696
2697    EINA_LIST_FREE(state->buffer_damages, dmg)
2698      eina_rectangle_free(dmg);
2699
2700    if (state->opaque) eina_tiler_free(state->opaque);
2701    state->opaque = NULL;
2702
2703    if (state->input) eina_tiler_free(state->input);
2704    state->input = NULL;
2705
2706    if (state->buffer) wl_list_remove(&state->buffer_destroy_listener.link);
2707    state->buffer = NULL;
2708
2709    e_presentation_time_container_finish(&state->presentation_container);
2710 }
2711
2712 static void
2713 _e_comp_wl_surface_state_buffer_set(E_Comp_Wl_Surface_State *state, E_Comp_Wl_Buffer *buffer)
2714 {
2715    if (state->buffer == buffer) return;
2716    if (state->buffer)
2717      wl_list_remove(&state->buffer_destroy_listener.link);
2718    state->buffer = buffer;
2719    if (state->buffer)
2720      wl_signal_add(&state->buffer->destroy_signal,
2721                    &state->buffer_destroy_listener);
2722 }
2723
2724 static void
2725 _e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
2726 {
2727    Eina_Rectangle *dmg;
2728    Eina_Bool placed = EINA_TRUE;
2729    int x = 0, y = 0;
2730    int w, h;
2731    int nw, nh;
2732    E_Comp_Wl_Buffer *buffer;
2733    struct wl_resource *cb;
2734    Eina_List *l, *ll;
2735    E_Comp_Wl_Client_Data *cdata = e_client_cdata_get(ec);
2736    E_Comp_Wl_Buffer_Viewport *vp = &cdata->scaler.buffer_viewport;
2737    E_Zone *zone;
2738
2739    if (ec->ignored)
2740      {
2741         if ((ec->internal) ||
2742             (cdata->shell.surface && state->new_attach))
2743           {
2744              EC_CHANGED(ec);
2745              ec->new_client = 1;
2746              e_comp->new_clients++;
2747              ELOGF("COMP", "Unignore", ec);
2748              e_client_unignore(ec);
2749           }
2750      }
2751
2752    /* buffer transform */
2753    if (vp->buffer.transform != state->buffer_viewport.buffer.transform)
2754      {
2755         E_Output *eout;
2756         int transform_change = (4 + state->buffer_viewport.buffer.transform - vp->buffer.transform) & 0x3;
2757
2758         /* when buffer is transformed, we have to apply the new evas-map */
2759         state->buffer_viewport.changed = EINA_TRUE;
2760
2761         ELOGF("TRANSFORM", "buffer_transform changed: old(%d) new(%d)",
2762               ec,
2763               vp->buffer.transform, state->buffer_viewport.buffer.transform);
2764
2765         if (transform_change == vp->wait_for_transform_change)
2766           vp->wait_for_transform_change = 0;
2767
2768         // TODO: This logic has to move to e_comp_hwc or e_hwc_window and it is
2769         //       triggered by E_CLIENT_HOOK calls at those file.
2770         zone = e_comp_zone_find_by_ec(ec);
2771         if (zone)
2772           {
2773              eout = e_output_find(zone->output_id);
2774              if (eout && eout->hwc)
2775                {
2776                   if (e_hwc_policy_get(eout->hwc) == E_HWC_POLICY_PLANES)
2777                     {
2778                        if (e_comp_is_on_overlay(ec))
2779                          e_comp_hwc_client_end(ec, __FUNCTION__);
2780                     }
2781                }
2782           }
2783      }
2784
2785    /* assign a new buffer_vieport to cdata->scaler.buffer_viewport */
2786    cdata->scaler.buffer_viewport = state->buffer_viewport;
2787
2788    if (state->new_attach)
2789      {
2790         e_comp_wl_surface_attach(ec, state->buffer);
2791      }
2792
2793    /* emit a apply_viewport signal when the information of viewport and buffer is ready */
2794    wl_signal_emit(&cdata->apply_viewport_signal, &cdata->surface);
2795
2796    _e_comp_wl_surface_state_buffer_set(state, NULL);
2797
2798    if ((state->new_attach) ||
2799        (state->buffer_viewport.changed))
2800      {
2801         _e_comp_wl_surface_state_size_update(ec, state);
2802         e_comp_wl_map_size_cal_from_viewport(ec);
2803
2804         /* update the position */
2805         if (ec->changes.pos)
2806           {
2807              e_comp_object_frame_xy_unadjust(ec->frame,
2808                                              ec->x, ec->y,
2809                                              &x, &y);
2810           }
2811         else
2812           {
2813              x = ec->client.x;
2814              y = ec->client.y;
2815           }
2816
2817         if (ec->new_client) placed = ec->placed;
2818
2819         if (!ec->lock_client_size)
2820           {
2821              w = ec->w;
2822              h = ec->h;
2823
2824              ec->client.w = state->bw;
2825              ec->client.h = state->bh;
2826
2827              e_comp_object_frame_wh_adjust(ec->frame,
2828                                            ec->client.w, ec->client.h,
2829                                            &nw, &nh);
2830              e_client_size_set(ec, nw, nh);
2831
2832              if ((w != ec->w) || (h != ec->h))
2833                {
2834                   ec->changes.size = 1;
2835                   EC_CHANGED(ec);
2836                }
2837           }
2838
2839         if (ec->changes.buf_size)
2840           {
2841              ELOGF("COMP", "Buffer size is changed. size(%d,%d)", ec, state->bw, state->bh);
2842              _e_comp_wl_hook_call(E_COMP_WL_HOOK_BUFFER_SIZE_CHANGE, ec);
2843              ec->changes.buf_size = EINA_FALSE;
2844
2845              if (ec->move_after_resize)
2846                {
2847                   ELOGF("POSSIZE", "Unset move_after_resize. ec_geo(%d,%d,%dx%d)", ec, ec->x, ec->y, ec->w, ec->h);
2848                   ec->move_after_resize = EINA_FALSE;
2849                }
2850           }
2851      }
2852
2853    /* map or unmap ec */
2854    Eina_Bool pixmap_usable = e_pixmap_usable_get(ec->pixmap);
2855    Eina_Bool hide_by_request = e_client_hide_by_request_get(ec);
2856    if (!pixmap_usable || hide_by_request)
2857      {
2858         /* unmap ec */
2859         if (cdata->mapped)
2860           {
2861              ELOGF("COMP", "Unmap. pixmap_usable:%d", ec, pixmap_usable);
2862
2863              e_client_hide(ec);
2864           }
2865
2866         if ((cdata->sub.below_obj) &&
2867             (evas_object_visible_get(cdata->sub.below_obj)))
2868           {
2869              evas_object_hide(cdata->sub.below_obj);
2870           }
2871      }
2872    else
2873      {
2874         /* map ec */
2875         e_client_show(ec);
2876
2877         if ((cdata->sub.below_obj) &&
2878             (!evas_object_visible_get(cdata->sub.below_obj)) &&
2879             (evas_object_visible_get(ec->frame)))
2880           {
2881              evas_object_show(cdata->sub.below_obj);
2882           }
2883      }
2884
2885    if ((state->new_attach) ||
2886        (state->buffer_viewport.changed))
2887      {
2888         if ((e_comp_wl->drag) &&
2889             (e_comp_wl->drag_client) &&
2890             (e_comp_wl->drag_client == ec))
2891           {
2892              e_drag_reference_point_set(e_comp_wl->drag, state->sx, state->sy);
2893
2894              e_drag_move(e_comp_wl->drag,
2895                          e_comp_wl->drag->x,
2896                          e_comp_wl->drag->y);
2897
2898              e_drag_resize(e_comp_wl->drag,
2899                            state->bw, state->bh);
2900           }
2901         else if ((cdata->shell.surface) &&
2902                  (cdata->shell.configure))
2903           {
2904              e_comp_wl_commit_sync_configure(ec);
2905           }
2906         else if (!e_client_video_hw_composition_check(ec))
2907           {
2908              e_client_util_move_resize_without_frame(ec, x, y, ec->w, ec->h);
2909           }
2910
2911         if (ec->new_client)
2912           {
2913              ec->placed = placed;
2914              ec->want_focus |= ec->icccm.accepts_focus && (!ec->override);
2915           }
2916      }
2917
2918    if (cdata->scaler.buffer_viewport.changed)
2919      e_comp_wl_map_apply(ec);
2920
2921    /* resize transform object */
2922    if (ec->transformed)
2923      e_client_transform_update(ec);
2924
2925    state->sx = 0;
2926    state->sy = 0;
2927    state->new_attach = EINA_FALSE;
2928
2929    EINA_LIST_FOREACH_SAFE(cdata->frames, l, ll, cb)
2930      {
2931         wl_callback_send_done(cb, (unsigned int)(ecore_loop_time_get() * 1000));
2932         wl_resource_destroy(cb);
2933      }
2934
2935    /* insert state frame callbacks into comp_data->frames
2936     * NB: This clears state->frames list */
2937    cdata->frames = eina_list_merge(cdata->frames,
2938                                            state->frames);
2939    state->frames = NULL;
2940
2941    e_presentation_time_container_feedback_discard(&cdata->presentation_container);
2942    e_presentation_time_container_feedback_merge(&cdata->presentation_container,
2943                                                 &state->presentation_container);
2944
2945    buffer = e_pixmap_resource_get(ec->pixmap);
2946
2947    /* put state damages into surface */
2948    if (ec->frame)
2949      {
2950         /* FIXME: workaround for bad wayland egl driver which doesn't send damage request */
2951         if (!eina_list_count(state->damages) && !eina_list_count(state->buffer_damages))
2952           {
2953              if ((cdata->buffer_ref.buffer) &&
2954                  ((cdata->buffer_ref.buffer->type == E_COMP_WL_BUFFER_TYPE_NATIVE) ||
2955                   (cdata->buffer_ref.buffer->type == E_COMP_WL_BUFFER_TYPE_TBM)))
2956                {
2957                   e_comp_object_damage(ec->frame,
2958                                        0, 0,
2959                                        cdata->buffer_ref.buffer->w,
2960                                        cdata->buffer_ref.buffer->h);
2961                }
2962           }
2963         else
2964           {
2965              Eina_List *damages = NULL;
2966
2967              if (buffer)
2968                _e_comp_wl_buffer_damage_set(buffer, state->buffer_damages);
2969
2970              if (eina_list_count(state->buffer_damages))
2971                {
2972                   EINA_LIST_FREE(state->buffer_damages, dmg)
2973                     {
2974                        if (buffer)
2975                          e_comp_wl_rect_convert_inverse(buffer->w, buffer->h,
2976                                                         e_comp_wl_output_buffer_transform_get(ec),
2977                                                         vp->buffer.scale,
2978                                                         dmg->x, dmg->y, dmg->w, dmg->h,
2979                                                         &dmg->x, &dmg->y, &dmg->w, &dmg->h);
2980                        damages = eina_list_append(damages, dmg);
2981                     }
2982                }
2983
2984              EINA_LIST_FREE(state->damages, dmg)
2985                damages = eina_list_append(damages, dmg);
2986
2987              EINA_LIST_FREE(damages, dmg)
2988                {
2989                   /* not creating damage for ec that shows a underlay video */
2990                   if (state->buffer_viewport.changed ||
2991                       !e_comp->wl_comp_data->available_hw_accel.underlay ||
2992                       !buffer || buffer->type != E_COMP_WL_BUFFER_TYPE_VIDEO)
2993                     e_comp_object_damage(ec->frame, dmg->x, dmg->y, dmg->w, dmg->h);
2994
2995                   eina_rectangle_free(dmg);
2996                }
2997           }
2998      }
2999
3000    /* put state opaque into surface */
3001    e_pixmap_image_opaque_set(ec->pixmap, 0, 0, 0, 0);
3002    if (state->opaque)
3003      {
3004         Eina_Rectangle *rect;
3005         Eina_Iterator *itr;
3006
3007         itr = eina_tiler_iterator_new(state->opaque);
3008         EINA_ITERATOR_FOREACH(itr, rect)
3009           {
3010              Eina_Rectangle r;
3011
3012              EINA_RECTANGLE_SET(&r, rect->x, rect->y, rect->w, rect->h);
3013              E_RECTS_CLIP_TO_RECT(r.x, r.y, r.w, r.h, 0, 0, state->bw, state->bh);
3014              e_pixmap_image_opaque_set(ec->pixmap, r.x, r.y, r.w, r.h);
3015              break;
3016           }
3017
3018         eina_iterator_free(itr);
3019      }
3020
3021    /* put state input into surface */
3022    if ((state->input) &&
3023        (!eina_tiler_empty(state->input)) &&
3024        ec->first_mapped)
3025      {
3026         Eina_Tiler *src, *tmp;
3027         int sw = ec->w;
3028         int sh = ec->h;
3029
3030         tmp = eina_tiler_new(sw, sh);
3031         eina_tiler_tile_size_set(tmp, 1, 1);
3032
3033         eina_tiler_rect_add(tmp,
3034                             &(Eina_Rectangle){0, 0, sw, sh});
3035
3036         if ((src = eina_tiler_intersection(state->input, tmp)))
3037           {
3038              Eina_Rectangle *rect;
3039              Eina_Iterator *itr;
3040
3041              e_comp_object_input_objs_del(ec->frame);
3042              itr = eina_tiler_iterator_new(src);
3043              EINA_ITERATOR_FOREACH(itr, rect)
3044                {
3045                   ELOGF("COMP", "Set Input Area x:%d, y:%d, w:%d, h:%d, ec(%dx%d), state(%dx%d)",
3046                         ec, rect->x, rect->y, rect->w, rect->h,
3047                         ec->w, ec->h, state->bw, state->bh);
3048                   e_comp_object_input_area_set(ec->frame,
3049                                                rect->x, rect->y,
3050                                                rect->w, rect->h);
3051                }
3052
3053              eina_iterator_free(itr);
3054              eina_tiler_free(src);
3055           }
3056         else
3057           e_comp_object_input_area_set(ec->frame, 0, 0, ec->w, ec->h);
3058
3059         eina_tiler_free(tmp);
3060
3061         /* clear input tiler */
3062         eina_tiler_clear(state->input);
3063      }
3064
3065    e_comp_wl_subsurface_check_below_bg_rectangle(ec);
3066
3067    if ((cdata->video_client) &&
3068        ((buffer) &&
3069         (buffer->type == E_COMP_WL_BUFFER_TYPE_VIDEO)) &&
3070        (e_comp->wl_comp_data->available_hw_accel.underlay))
3071      {
3072         e_pixmap_image_clear(ec->pixmap, 1);
3073      }
3074
3075    state->buffer_viewport.changed = 0;
3076
3077    wl_signal_emit(&cdata->state_commit_signal, &cdata->surface);
3078
3079    if (buffer &&
3080        ec->exp_iconify.buffer_flush &&
3081        e_policy_visibility_client_is_iconic(ec))
3082      {
3083         e_pixmap_buffer_clear(ec->pixmap, EINA_FALSE);
3084      }
3085 }
3086
3087 static void
3088 _e_comp_wl_surface_render_stop(E_Client *ec)
3089 {
3090    /* FIXME: this may be fine after e_pixmap can create textures for wl clients? */
3091    //if ((!ec->internal) && (!e_comp_gl_get()))
3092      ec->dead = 1;
3093
3094    /* check if internal animation is running */
3095    if (e_comp_object_is_animating(ec->frame)) return;
3096    /* check if external animation is running */
3097    if (evas_object_data_get(ec->frame, "effect_running")) return;
3098
3099    evas_object_hide(ec->frame);
3100 }
3101
3102 static void
3103 _e_input_thread_client_free(void *data)
3104 {
3105    E_Input_Thread_Request_EClient_Data *ec_data = data;
3106    EINA_SAFETY_ON_NULL_RETURN(ec_data);
3107
3108    INF("[input thread|%s] ec: %p, surface: %p\n", __func__, ec_data->ec, ec_data->wl_surface);
3109 }
3110
3111 EINTERN void
3112 e_comp_wl_client_surface_finish(E_Client *ec)
3113 {
3114    struct wl_resource *res, *surface;
3115    struct wl_client *surface_client = NULL;
3116    Eina_List *l, *ll;
3117
3118    surface = e_comp_wl_client_surface_get(ec);
3119
3120    if (surface)
3121      surface_client = wl_resource_get_client(surface);
3122
3123    if (surface_client &&
3124        (ec == e_client_focused_get()))
3125      {
3126         EINA_LIST_FOREACH_SAFE(e_comp_input_key->kbd.focused, l, ll, res)
3127           {
3128              if (wl_resource_get_client(res) ==
3129                  surface_client)
3130                e_comp_input_key->kbd.focused =
3131                   eina_list_remove_list(e_comp_input_key->kbd.focused, l);
3132
3133           }
3134      }
3135
3136    E_Input_Thread_Request_EClient_Data ec_free_data;
3137    memset(&ec_free_data, 0, sizeof(E_Input_Thread_Request_EClient_Data));
3138    ec_free_data.ec = ec;
3139    ec_free_data.wl_surface = ec->comp_data->wl_surface;
3140
3141    INF("[e_comp_wl_client_surface_finish] surface: %p\n", ec_free_data.wl_surface);
3142    e_input_thread_safe_call(_e_input_thread_client_free, &ec_free_data, sizeof(E_Input_Thread_Request_EClient_Data));
3143
3144    e_comp_wl_client_surface_set(ec, NULL);
3145
3146    ec->comp_data->wl_surface = NULL;
3147    e_pixmap_win_id_del(ec->pixmap);
3148
3149    _e_comp_wl_surface_render_stop(ec);
3150    e_object_del(E_OBJECT(ec));
3151 }
3152
3153 static void
3154 _e_comp_wl_pname_get(pid_t pid, char *name, int size)
3155 {
3156    if (!name) return;
3157
3158    FILE *h;
3159    char proc[512], pname[512];
3160    size_t len;
3161
3162    snprintf(proc, 512,"/proc/%d/cmdline", pid);
3163
3164    h = fopen(proc, "r");
3165    if (h)
3166      {
3167         len = fread(pname, sizeof(char), 512, h);
3168         if (len > 0)
3169           pname[len - 1] = '\0';
3170         else
3171           strncpy(pname, "NO NAME", sizeof(pname));
3172
3173         fclose(h);
3174      }
3175    else
3176      {
3177         strncpy(pname, "NO NAME", sizeof(pname));
3178      }
3179
3180    strncpy(name, pname, size);
3181 }
3182
3183 static void
3184 _e_comp_wl_pname_print(pid_t pid)
3185 {
3186    FILE *h;
3187    char proc[512], pname[512];
3188    size_t len;
3189
3190    snprintf(proc, 512,"/proc/%d/cmdline", pid);
3191
3192    h = fopen(proc, "r");
3193    if (!h) return;
3194
3195    len = fread(pname, sizeof(char), 512, h);
3196    if (len > 0)
3197      pname[len - 1] = '\0';
3198    else
3199      strncpy(pname, "NO NAME", sizeof(pname));
3200
3201    fclose(h);
3202
3203    ELOGF("COMP", "         |%s", NULL, pname);
3204 }
3205
3206
3207 static void
3208 _e_comp_wl_connected_client_cb_destroy(struct wl_listener *listener, void *data)
3209 {
3210    struct wl_client *client = data;
3211    E_Comp_Connected_Client_Info *cinfo;
3212    E_Appinfo *eai;
3213
3214    cinfo = wl_container_of(listener, cinfo, destroy);
3215
3216    ELOGF("WL_CLIENT", "DESTROY  |client:%8p|%d|%d|%d",
3217          NULL, client, cinfo->pid, cinfo->uid, cinfo->gid);
3218
3219    eai = e_appinfo_find_with_pid(cinfo->pid);
3220    if (e_appinfo_owner_get(eai) == E_APPINFO_OWNER_SERVER)
3221      e_appinfo_del(eai);
3222
3223    e_comp->connected_clients = eina_list_remove(e_comp->connected_clients, cinfo);
3224
3225    wl_list_remove(&cinfo->destroy.link);
3226    eina_stringshare_del(cinfo->name);
3227    free(cinfo);
3228 }
3229
3230 static void
3231 _e_comp_wl_connected_client_create(struct wl_client *client, char *name, pid_t pid, uid_t uid, gid_t gid)
3232 {
3233    E_Comp_Connected_Client_Info *cinfo;
3234
3235    cinfo = E_NEW(E_Comp_Connected_Client_Info, 1);
3236    EINA_SAFETY_ON_NULL_RETURN(cinfo);
3237
3238    cinfo->name = eina_stringshare_add(name);
3239    cinfo->pid = pid;
3240    cinfo->uid = uid;
3241    cinfo->gid = gid;
3242    cinfo->destroy.notify = _e_comp_wl_connected_client_cb_destroy;
3243    wl_client_add_destroy_listener(client, &cinfo->destroy);
3244    e_comp->connected_clients = eina_list_append(e_comp->connected_clients, cinfo);
3245
3246    _e_comp_wl_pid_hook_call(E_COMP_WL_PID_HOOK_CONNECTED_CLIENT_CREATE, pid);
3247 }
3248
3249 static void
3250 _e_comp_wl_client_cb_focus_set(void *data EINA_UNUSED, E_Client *ec)
3251 {
3252    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3253
3254    /* send configure */
3255    if (ec->comp_data->shell.configure_send)
3256      {
3257         if (ec->comp_data->shell.surface)
3258           _e_comp_wl_configure_send(ec, 0, 0);
3259      }
3260
3261    e_comp_input_key->kbd.focus = ec->comp_data->surface;
3262 }
3263
3264 static void
3265 _e_comp_wl_client_cb_focus_unset(void *data EINA_UNUSED, E_Client *ec)
3266 {
3267    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3268
3269    /* send configure */
3270    if (ec->comp_data->shell.configure_send)
3271      {
3272         if (ec->comp_data->shell.surface)
3273           _e_comp_wl_configure_send(ec, 0, 0);
3274      }
3275
3276    _e_comp_wl_focus_check();
3277
3278    if (e_comp_input_key->kbd.focus == ec->comp_data->surface)
3279      e_comp_input_key->kbd.focus = NULL;
3280 }
3281
3282 static void
3283 _e_comp_wl_client_cb_resize_begin(void *data EINA_UNUSED, E_Client *ec)
3284 {
3285    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3286    if (ec->keyboard_resizing) return;
3287
3288    /* do nothing currently */
3289    ;
3290 }
3291
3292 static void
3293 _e_comp_wl_client_cb_resize_end(void *data EINA_UNUSED, E_Client *ec)
3294 {
3295    if (e_object_is_del(E_OBJECT(ec))) return;
3296    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3297
3298    e_comp_wl->resize.edges = 0;
3299    e_comp_wl->resize.resource = NULL;
3300 }
3301
3302 static void
3303 _e_comp_wl_client_cb_move_end(void *data EINA_UNUSED, E_Client *ec)
3304 {
3305    if (e_object_is_del(E_OBJECT(ec))) return;
3306    if (e_pixmap_type_get(ec->pixmap) != E_PIXMAP_TYPE_WL) return;
3307 }
3308
3309 static void
3310 _e_comp_wl_output_info_send(E_Comp_Wl_Output *output, struct wl_resource *resource, pid_t pid, int res_w, int res_h)
3311 {
3312    int phys_w, phys_h;
3313    int ratio_w, ratio_h;
3314
3315    phys_w = output->phys_width;
3316    phys_h = output->phys_height;
3317
3318    if (e_config->configured_output_resolution.use)
3319      {
3320         // change the configured physical size(mm) of the output
3321         if (output->w != res_w)
3322           {
3323              ratio_w = res_w / output->w;
3324              phys_w = (int)((float)output->phys_width * (float)ratio_w);
3325           }
3326
3327         if (output->h != res_h)
3328           {
3329              ratio_h = res_h / output->h;
3330              phys_h = (int)((float)output->phys_height * (float)ratio_h);
3331           }
3332
3333         ELOGF("COMP_WL", "\tSend Configured Output (pid:%d)", NULL, pid);
3334      }
3335
3336    ELOGF("COMP_WL", "\t    Output Resolution: res(%d, %d) phy_size(%d, %d) (pid:%d).",
3337          NULL, res_w, res_h, phys_w, phys_h, pid);
3338
3339    if (wl_resource_get_version(resource) >= WL_OUTPUT_SCALE_SINCE_VERSION)
3340      wl_output_send_scale(resource, output->scale);
3341
3342    wl_output_send_geometry(resource, output->x, output->y,
3343                            phys_w, phys_h,
3344                            output->subpixel, output->make ?: "",
3345                            output->model ?: "", output->transform);
3346
3347    wl_output_send_mode(resource,  WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED,
3348                        res_w, res_h, output->refresh);
3349
3350    if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
3351      wl_output_send_done(resource);
3352 }
3353
3354 static void
3355 _e_comp_wl_cb_output_unbind(struct wl_resource *resource)
3356 {
3357    E_Comp_Wl_Output *output;
3358
3359    if (!(output = wl_resource_get_user_data(resource))) return;
3360
3361    output->resources = eina_list_remove(output->resources, resource);
3362 }
3363
3364 static void
3365 _e_comp_wl_cb_output_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3366 {
3367    E_Comp_Wl_Output *output;
3368    struct wl_resource *resource;
3369    E_Appinfo *eai = NULL;
3370    pid_t pid = 0;
3371    int res_w, res_h;
3372
3373    if (!(output = data)) return;
3374
3375    resource =
3376      wl_resource_create(client, &wl_output_interface, version, id);
3377    if (!resource)
3378      {
3379         wl_client_post_no_memory(client);
3380         return;
3381      }
3382
3383    ELOGF("COMP_WL", "Bound Output: %s", NULL, output->id);
3384    ELOGF("COMP_WL", "\tOutput Geom: %d %d %d %d", NULL, output->x, output->y, output->w, output->h);
3385
3386    output->resources = eina_list_append(output->resources, resource);
3387
3388    wl_resource_set_implementation(resource, NULL, output,
3389                                   _e_comp_wl_cb_output_unbind);
3390    wl_resource_set_user_data(resource, output);
3391
3392    // set the configured_output_resolution as a resolution of the wl_output if the use is set.
3393    if (e_config->configured_output_resolution.use)
3394      {
3395         wl_client_get_credentials(client, &pid, NULL, NULL);
3396         if (pid <= 0)
3397           {
3398              res_w = e_config->configured_output_resolution.w;
3399              res_h = e_config->configured_output_resolution.h;
3400              goto send_info;
3401           }
3402
3403         eai = e_appinfo_find_with_pid(pid);
3404         if (!eai)
3405           {
3406              res_w = e_config->configured_output_resolution.w;
3407              res_h = e_config->configured_output_resolution.h;
3408              goto send_info;
3409           }
3410
3411         if (!e_appinfo_base_output_resolution_get(eai, &res_w, &res_h))
3412           {
3413              res_w = e_config->configured_output_resolution.w;
3414              res_h = e_config->configured_output_resolution.h;
3415              goto send_info;
3416           }
3417
3418         ELOGF("COMP_WL", "Get base_screen_resolution. (pid:%d).", NULL, pid);
3419      }
3420    else
3421      {
3422         res_w = output->w;
3423         res_h = output->h;
3424      }
3425
3426 send_info:
3427    _e_comp_wl_output_info_send(output, resource, pid, res_w, res_h);
3428 }
3429
3430 static void
3431 _e_comp_wl_gl_init(void *data EINA_UNUSED)
3432 {
3433    Evas *evas = NULL;
3434    Evas_GL *evasgl = NULL;
3435    Evas_GL_API *glapi = NULL;
3436    Evas_GL_Context *ctx = NULL;
3437    Evas_GL_Surface *sfc = NULL;
3438    Evas_GL_Config *cfg = NULL;
3439    Eina_Bool res;
3440    E_Comp_Wl_Evas_Gl *evas_gl = NULL;
3441
3442    if (!e_comp_gl_get()) return;
3443
3444    evas_gl = E_NEW(E_Comp_Wl_Evas_Gl, 1);
3445    EINA_SAFETY_ON_NULL_RETURN(evas_gl);
3446
3447    /* create dummy evas gl to bind wayland display of enlightenment to egl display */
3448    e_main_ts_begin("\tE_Comp_Wl_GL Init");
3449
3450    /* if wl_drm module doesn't call e_comp_canvas_init yet,
3451     * then we should get evas from ecore_evas.
3452     */
3453    if (e_comp->evas)
3454      evas = e_comp->evas;
3455    else
3456      evas = ecore_evas_get(e_comp->ee);
3457
3458    evasgl = evas_gl_new(evas);
3459    EINA_SAFETY_ON_NULL_GOTO(evasgl, err);
3460
3461    glapi = evas_gl_api_get(evasgl);
3462    EINA_SAFETY_ON_NULL_GOTO(glapi, err);
3463    EINA_SAFETY_ON_NULL_GOTO(glapi->evasglBindWaylandDisplay, err);
3464
3465    cfg = evas_gl_config_new();
3466    EINA_SAFETY_ON_NULL_GOTO(cfg, err);
3467
3468    sfc = evas_gl_surface_create(evasgl, cfg, 1, 1);
3469    EINA_SAFETY_ON_NULL_GOTO(sfc, err);
3470
3471    ctx = evas_gl_context_create(evasgl, NULL);
3472    EINA_SAFETY_ON_NULL_GOTO(ctx, err);
3473
3474    res = evas_gl_make_current(evasgl, sfc, ctx);
3475    EINA_SAFETY_ON_FALSE_GOTO(res, err);
3476
3477    res = glapi->evasglBindWaylandDisplay(evasgl, e_comp_wl->wl.disp);
3478    EINA_SAFETY_ON_FALSE_GOTO(res, err);
3479
3480    evas_gl_config_free(cfg);
3481
3482    evas_gl->gl = evasgl;
3483    evas_gl->glapi = glapi;
3484    evas_gl->glsfc = sfc;
3485    evas_gl->glctx = ctx;
3486
3487    e_comp_wl->evas_gl = evas_gl;
3488
3489    /* for native surface */
3490    e_comp->gl = 1;
3491
3492    e_main_ts_end("\tE_Comp_Wl_GL Init Done");
3493
3494    return;
3495
3496 err:
3497    evas_gl_config_free(cfg);
3498    evas_gl_make_current(evasgl, NULL, NULL);
3499    evas_gl_context_destroy(evasgl, ctx);
3500    evas_gl_surface_destroy(evasgl, sfc);
3501    evas_gl_free(evasgl);
3502    free(evas_gl);
3503 }
3504
3505 // FIXME
3506 #if 0
3507 static void
3508 _e_comp_wl_gl_popup_cb_close(void *data,
3509                              Evas_Object *obj EINA_UNUSED,
3510                              void *event_info EINA_UNUSED)
3511 {
3512    evas_object_del(data);
3513 }
3514
3515 static void
3516 _e_comp_wl_gl_popup_cb_focus(void *data,
3517                              Evas_Object *obj EINA_UNUSED,
3518                              void *event_info EINA_UNUSED)
3519 {
3520    elm_object_focus_set(data, EINA_TRUE);
3521 }
3522 #endif
3523
3524 static Eina_Bool
3525 _e_comp_wl_gl_idle(void *data)
3526 {
3527    if (!e_comp->gl)
3528      {
3529         /* show warning window to notify failure of gl init */
3530         // TODO: yigl
3531 #if 0
3532         Evas_Object *win, *bg, *popup, *btn;
3533
3534         win = elm_win_add(NULL, "compositor warning", ELM_WIN_BASIC);
3535         elm_win_title_set(win, "Compositor Warning");
3536         elm_win_autodel_set(win, EINA_TRUE);
3537         elm_win_borderless_set(win, EINA_TRUE);
3538         elm_win_role_set(win, "notification-low");
3539         elm_win_alpha_set(win, EINA_TRUE);
3540
3541         bg = evas_object_rectangle_add(evas_object_evas_get(win));
3542         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
3543         elm_win_resize_object_add(win, bg);
3544         evas_object_color_set(bg, 125, 125, 125, 125);
3545         evas_object_show(bg);
3546
3547         popup = elm_popup_add(win);
3548         elm_object_text_set(popup,
3549                             _( "Your screen does not support OpenGL.<br>"
3550                                "Falling back to software engine."));
3551         elm_object_part_text_set(popup, "title,text", "Compositor Warning");
3552
3553         btn = elm_button_add(popup);
3554         elm_object_text_set(btn, "Close");
3555         elm_object_part_content_set(popup, "button1", btn);
3556         evas_object_show(btn);
3557
3558         evas_object_smart_callback_add(win, "focus,in", _e_comp_wl_gl_popup_cb_focus, popup);
3559         evas_object_smart_callback_add(btn, "unpressed", _e_comp_wl_gl_popup_cb_close, win);
3560
3561         evas_object_show(popup);
3562         evas_object_show(win);
3563 #endif
3564      }
3565
3566    return ECORE_CALLBACK_CANCEL;
3567 }
3568
3569 static void
3570 _e_comp_wl_cb_client_created(struct wl_listener *listener, void *data)
3571 {
3572    struct wl_client *client = data;
3573    pid_t pid = 0;
3574    uid_t uid = 0;
3575    gid_t gid = 0;
3576    char name[512];
3577
3578    wl_client_get_credentials(client, &pid, &uid, &gid);
3579
3580    ELOGF("COMP", "WL_CLIENT|client:%8p|%d|%d|%d", NULL, client, pid, uid, gid);
3581
3582    _e_comp_wl_pname_print(pid);
3583    _e_comp_wl_pname_get(pid, name, sizeof(name));
3584    _e_comp_wl_connected_client_create(client, name, pid, uid, gid);
3585 }
3586
3587 static void
3588 _e_comp_wl_ds_log_handler(enum ds_log_level level, const char *fmt, va_list args)
3589 {
3590    char buf[1024] = {0, };
3591
3592    vsnprintf(buf, 1024, fmt, args);
3593    switch (level)
3594      {
3595       case DS_DBG:
3596          DBG("[libds] %s", buf);
3597          break;
3598       case DS_INF:
3599          INF("[libds] %s", buf);
3600          break;
3601       case DS_ERR:
3602          ERR("[libds] %s", buf);
3603          break;
3604       default:
3605          break;
3606      }
3607 }
3608
3609 static Eina_Bool
3610 _e_comp_wl_display_create(void)
3611 {
3612    E_Comp_Data *comp;
3613    E_Comp_Wl_Data *wl_cdata;
3614    const char *name;
3615    int fd = 0;
3616    Eina_Bool res;
3617
3618    /* create new compositor data */
3619    if (!(comp = E_NEW(E_Comp_Data, 1)))
3620      {
3621         ERR("Could not create compositor data: %m");
3622         return EINA_FALSE;
3623      }
3624    wl_cdata = &comp->base;
3625
3626    /* set compositor wayland data */
3627    e_comp_wl = e_comp->wl_comp_data = wl_cdata;
3628
3629    g_mutex_init(&connection_mutex);
3630
3631    /* try to create a wayland display */
3632    if (!(wl_cdata->wl.disp = wl_display_create()))
3633      {
3634         ERR("Could not create a Wayland display: %m");
3635         goto disp_err;
3636      }
3637
3638    /* try to setup wayland socket */
3639    if (!(name = wl_display_add_socket_auto(wl_cdata->wl.disp)))
3640      {
3641         ERR("Could not create Wayland display socket: %m");
3642         PRCTL("[Winsys] Could not create Wayland display socket: /run/wayland-0");
3643         goto sock_err;
3644      }
3645
3646    res = e_comp_socket_init(name);
3647    EINA_SAFETY_ON_FALSE_GOTO(res, sock_err);
3648    PRCTL("[Winsys] change permission and create sym link for %s", name);
3649
3650    /* set wayland display environment variable */
3651    e_env_set("WAYLAND_DISPLAY", name);
3652
3653    /* wl_cdata->output.transform = WL_OUTPUT_TRANSFORM_NORMAL; */
3654    /* wl_cdata->output.scale = e_scale; */
3655
3656    ds_log_init(DS_DBG, _e_comp_wl_ds_log_handler);
3657
3658    if (!e_compositor_init(wl_cdata->wl.disp))
3659      {
3660         ERR("Failed to initialize compositor");
3661         goto comp_err;
3662      }
3663
3664    comp->client_created.notify = _e_comp_wl_cb_client_created;
3665    wl_display_add_client_created_listener(comp->base.wl.disp, &comp->client_created);
3666
3667    if (!e_comp_wl_subsurfaces_init())
3668      {
3669         ERR("Failed to init_subsurfaces");
3670         goto subsurfaces_err;
3671      }
3672
3673    /* initialize shm mechanism */
3674    wl_display_init_shm(wl_cdata->wl.disp);
3675
3676    /* _e_comp_wl_cb_randr_change(NULL, 0, NULL); */
3677
3678    /* try to init data manager */
3679    if (!e_comp_wl_data_manager_init())
3680      {
3681         ERR("Could not initialize data manager");
3682         goto data_err;
3683      }
3684
3685    /* try to init input */
3686    if (!e_comp_wl_input_init())
3687      {
3688         ERR("Could not initialize input");
3689         goto input_err;
3690      }
3691
3692    if (e_comp_gl_get())
3693      _e_comp_wl_gl_init(NULL);
3694
3695    /* get the wayland display loop */
3696    wl_cdata->wl.loop = wl_display_get_event_loop(wl_cdata->wl.disp);
3697
3698    /* get the file descriptor of the wayland event loop */
3699    fd = wl_event_loop_get_fd(wl_cdata->wl.loop);
3700
3701    /* create a listener for wayland main loop events */
3702    wl_cdata->fd_hdlr =
3703      ecore_main_fd_handler_add(fd, (ECORE_FD_READ | ECORE_FD_ERROR),
3704                                _e_comp_wl_cb_read, wl_cdata, NULL, NULL);
3705    ecore_main_fd_handler_prepare_callback_set(wl_cdata->fd_hdlr,
3706                                               _e_comp_wl_cb_prepare, wl_cdata);
3707
3708    /* add awake handler */
3709    ecore_main_awake_handler_add(_e_comp_wl_cb_awake, NULL);
3710
3711    return EINA_TRUE;
3712
3713 input_err:
3714    e_comp_wl_data_manager_shutdown();
3715 data_err:
3716    e_comp_wl_subsurfaces_shutdown();
3717 subsurfaces_err:
3718    wl_list_remove(&comp->client_created.link);
3719 comp_err:
3720    e_env_unset("WAYLAND_DISPLAY");
3721 sock_err:
3722    wl_display_destroy(wl_cdata->wl.disp);
3723 disp_err:
3724    g_mutex_clear(&connection_mutex);
3725    free(comp);
3726    return EINA_FALSE;
3727 }
3728
3729 static void
3730 _e_comp_wl_gl_shutdown(void)
3731 {
3732    if (!e_comp_wl->evas_gl) return;
3733
3734    e_comp_wl->evas_gl->glapi->evasglUnbindWaylandDisplay(e_comp_wl->evas_gl->gl, e_comp_wl->wl.disp);
3735
3736    evas_gl_make_current(e_comp_wl->evas_gl->gl, NULL, NULL);
3737    evas_gl_context_destroy(e_comp_wl->evas_gl->gl, e_comp_wl->evas_gl->glctx);
3738    evas_gl_surface_destroy(e_comp_wl->evas_gl->gl, e_comp_wl->evas_gl->glsfc);
3739    evas_gl_free(e_comp_wl->evas_gl->gl);
3740
3741    E_FREE(e_comp_wl->evas_gl);
3742 }
3743
3744 /* public functions */
3745
3746 /**
3747  * Creates and initializes a Wayland compositor with ecore.
3748  * Registers callback handlers for keyboard and mouse activity
3749  * and other client events.
3750  *
3751  * @returns true on success, false if initialization failed.
3752  */
3753 EINTERN Eina_Bool
3754 e_comp_wl_init(void)
3755 {
3756    TRACE_DS_BEGIN(COMP_WL:INIT);
3757
3758    /* try to create a wayland compositor */
3759    if (!_e_comp_wl_display_create())
3760      {
3761         e_error_message_show(_("Enlightenment cannot create a Wayland Compositor!\n"));
3762         TRACE_DS_END();
3763         return EINA_FALSE;
3764      }
3765
3766    e_comp_wl_shell_init();
3767    e_wtz_shell_init();
3768 #ifdef HAVE_WAYLAND_TBM
3769    e_comp_wl_tbm_init();
3770 #endif
3771    e_comp_wl_remote_surface_init();
3772
3773    e_pixmap_init();
3774
3775    e_comp_wl_screenshooter_init();
3776
3777    if (!e_comp_wl_video_init())
3778      ELOGF("COMP", "Failed to initialize the e_comp_wl_video", NULL);
3779
3780    e_comp_wl_viewport_init();
3781    e_comp_wl_capture_init();
3782    e_comp_wl_renderer_init();
3783    _e_comp_wl_move_resize_init();
3784    e_presentation_time_init();
3785    ds_single_pixel_buffer_manager_v1_create(e_comp_wl->wl.disp);
3786    e_blender_init();
3787    e_blur_manager_init();
3788
3789    if (!e_foreign_global_init(e_comp_wl->wl.disp))
3790      ELOGF("COMP", "Failed to initialize the e_foreign global", NULL);
3791
3792    /* add event handlers to catch E events */
3793    E_LIST_HANDLER_APPEND(handlers, E_EVENT_SCREEN_CHANGE,            _e_comp_wl_cb_randr_change,        NULL);
3794    E_LIST_HANDLER_APPEND(handlers, E_EVENT_COMP_OBJECT_ADD,         _e_comp_wl_cb_comp_object_add,     NULL);
3795    E_LIST_HANDLER_PREPEND(handlers, ECORE_EVENT_MOUSE_MOVE,          _e_comp_wl_cb_mouse_move,          NULL);
3796    E_LIST_HANDLER_APPEND(handlers, ECORE_EVENT_MOUSE_RELATIVE_MOVE,  _e_comp_wl_cb_mouse_relative_move, NULL);
3797    E_LIST_HANDLER_PREPEND(handlers, ECORE_EVENT_MOUSE_BUTTON_CANCEL, _e_comp_wl_cb_mouse_button_cancel, NULL);
3798    E_LIST_HANDLER_APPEND(handlers, E_EVENT_ZONE_DISPLAY_STATE_CHANGE, _e_comp_wl_cb_zone_display_state_change, NULL);
3799    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_BEGIN, _e_comp_wl_cb_client_rot_change_begin, NULL);
3800    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_CANCEL, _e_comp_wl_cb_client_rot_change_cancel, NULL);
3801    E_LIST_HANDLER_APPEND(handlers, E_EVENT_CLIENT_ROTATION_CHANGE_END, _e_comp_wl_cb_client_rot_change_end, NULL);
3802
3803    /* add hooks to catch e_client events */
3804    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_FOCUS_SET,    _e_comp_wl_client_cb_focus_set,    NULL);
3805    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_FOCUS_UNSET,  _e_comp_wl_client_cb_focus_unset,  NULL);
3806    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_RESIZE_BEGIN, _e_comp_wl_client_cb_resize_begin, NULL);
3807    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_RESIZE_END,   _e_comp_wl_client_cb_resize_end,   NULL);
3808    E_LIST_HOOK_APPEND(hooks, E_CLIENT_HOOK_MOVE_END,     _e_comp_wl_client_cb_move_end,     NULL);
3809
3810    TRACE_DS_END();
3811    return EINA_TRUE;
3812 }
3813
3814 EINTERN void
3815 e_comp_wl_deferred_job(void)
3816 {
3817    ecore_idle_enterer_add(_e_comp_wl_gl_idle, NULL);
3818 }
3819
3820 /* internal functions */
3821 EINTERN void
3822 e_comp_wl_shutdown(void)
3823 {
3824    E_Comp_Data *comp = (E_Comp_Data *)e_comp_wl;
3825
3826    e_comp_wl_subsurfaces_shutdown();
3827    /* free handlers */
3828    E_FREE_LIST(handlers, ecore_event_handler_del);
3829    E_FREE_LIST(hooks, e_client_hook_del);
3830    _e_comp_wl_gl_shutdown();
3831
3832    e_presentation_time_shutdown();
3833    e_comp_wl_renderer_shutdown();
3834    e_comp_wl_capture_shutdown();
3835    e_comp_wl_viewport_shutdown();
3836    e_comp_wl_video_shutdown();
3837    e_comp_wl_screenshooter_shutdown();
3838
3839    e_comp_wl_remote_surface_shutdown();
3840
3841    e_pixmap_shutdown();
3842
3843    e_wtz_shell_shutdown();
3844    e_comp_wl_shell_shutdown();
3845    e_comp_wl_input_shutdown();
3846
3847    wl_list_remove(&comp->client_created.link);
3848
3849    /* delete awake handler */
3850    ecore_main_awake_handler_del(_e_comp_wl_cb_awake);
3851
3852    e_comp_wl = NULL;
3853    e_comp->wl_comp_data = NULL;
3854    free(comp);
3855    // TODO: yigl
3856 #if 0
3857    E_Comp_Wl_Output *output;
3858
3859    if (e_comp_wl->screenshooter.global)
3860      wl_global_destroy(e_comp_wl->screenshooter.global);
3861
3862    EINA_LIST_FREE(e_comp_wl->outputs, output)
3863      {
3864         if (output->id) eina_stringshare_del(output->id);
3865         if (output->make) eina_stringshare_del(output->make);
3866         if (output->model) eina_stringshare_del(output->model);
3867         free(output);
3868      }
3869
3870    /* delete fd handler */
3871    if (e_comp_wl->fd_hdlr) ecore_main_fd_handler_del(e_comp_wl->fd_hdlr);
3872
3873    E_FREE_FUNC(e_comp_wl->ptr.hide_tmr, ecore_timer_del);
3874    cursor_timer_ec = NULL;
3875
3876    /* free allocated data structure */
3877    free(e_comp_wl);
3878 #endif
3879 }
3880
3881 static void
3882 e_comp_wl_surface_event_simple_free(void *d EINA_UNUSED, E_Event_Client *ev)
3883 {
3884    e_object_unref(E_OBJECT(ev->ec));
3885    free(ev);
3886 }
3887
3888 EINTERN void
3889 e_comp_wl_surface_attach(E_Client *ec, E_Comp_Wl_Buffer *buffer)
3890 {
3891    E_Event_Client *ev;
3892    ev = E_NEW(E_Event_Client, 1);
3893    if (!ev) return;
3894
3895    e_comp_wl_buffer_reference(&ec->comp_data->buffer_ref, buffer);
3896
3897    /* set usable early because shell module checks this */
3898    if (ec->comp_data->shell.surface || e_comp_wl_subsurface_check(ec))
3899      e_pixmap_usable_set(ec->pixmap, (buffer != NULL));
3900
3901    e_pixmap_resource_set(ec->pixmap, buffer);
3902    e_pixmap_dirty(ec->pixmap);
3903    e_pixmap_refresh(ec->pixmap);
3904
3905    e_comp_wl_map_size_cal_from_buffer(ec);
3906    _e_comp_wl_surface_state_size_update(ec, &ec->comp_data->pending);
3907
3908    /* wm-policy module uses it */
3909    _e_comp_wl_hook_call(E_COMP_WL_HOOK_BUFFER_CHANGE, ec);
3910
3911    ev->ec = ec;
3912    e_object_ref(E_OBJECT(ec));
3913    ecore_event_add(E_EVENT_CLIENT_BUFFER_CHANGE, ev,
3914                    (Ecore_End_Cb)e_comp_wl_surface_event_simple_free, NULL);
3915 }
3916
3917 EINTERN Eina_Bool
3918 e_comp_wl_surface_commit(E_Client *ec)
3919 {
3920    Eina_Bool ignored;
3921    int x = 0, y = 0;
3922
3923    _e_comp_wl_surface_state_commit(ec, &ec->comp_data->pending);
3924    if (!e_comp_object_damage_exists(ec->frame))
3925      {
3926         if ((ec->comp_data->video_client) ||
3927             (!e_client_video_hw_composition_check(ec)))
3928           e_pixmap_image_clear(ec->pixmap, 1);
3929      }
3930
3931    ignored = ec->ignored;
3932
3933    if (e_comp_wl_subsurface_order_commit(ec))
3934      {
3935         E_Client *topmost = e_comp_wl_topmost_parent_get(ec);
3936         e_comp_wl_subsurface_restack(topmost);
3937         e_comp_wl_subsurface_restack_bg_rectangle(topmost);
3938      }
3939
3940    ec->ignored = ignored;
3941
3942    if (ec->is_cursor && ec->visible)
3943      {
3944         /* ignore cursor changes during resize/move I guess */
3945         if (!e_client_action_get())
3946           {
3947              if (e_comp->pointer)
3948                {
3949                   x = e_comp->pointer->hot.x;
3950                   y = e_comp->pointer->hot.y;
3951                }
3952              e_pointer_object_set(e_comp->pointer, ec->frame, x, y);
3953           }
3954      }
3955    return EINA_TRUE;
3956 }
3957
3958 static E_Comp_Wl_Output *
3959 _e_comp_wl_output_get(Eina_List *outputs, const char *id)
3960 {
3961    Eina_List *l;
3962    E_Comp_Wl_Output *output;
3963
3964    EINA_LIST_FOREACH(outputs, l, output)
3965      {
3966        if (!strcmp(output->id, id))
3967          return output;
3968      }
3969
3970    return NULL;
3971 }
3972
3973 /**
3974  * Initializes information about one display output.
3975  *
3976  * Adds or updates the given data about a single display output,
3977  * with an id matching the provided id.
3978  *
3979  * @param id         identification of output to be added or changed
3980  * @param make       manufacturer name of the display output
3981  * @param model      model name of the display output
3982  * @param x          output's top left corner x coordinate
3983  * @param y          output's top left corner y coordinate
3984  * @param w          output's width in pixels
3985  * @param h          output's height in pixels
3986  * @param pw         output's physical width in millimeters
3987  * @param ph         output's physical height in millimeters
3988  * @param refresh    output's refresh rate in mHz
3989  * @param subpixel   output's subpixel layout
3990  * @param transform  output's rotation and/or mirror transformation
3991  *
3992  * @returns True if a display output object could be added or updated
3993  */
3994 EINTERN Eina_Bool
3995 e_comp_wl_output_init(const char *id, const char *make, const char *model,
3996                       int x, int y, int w, int h, int pw, int ph,
3997                       unsigned int refresh, unsigned int subpixel,
3998                       unsigned int transform)
3999 {
4000    E_Comp_Wl_Output *output;
4001    Eina_List *l2;
4002    struct wl_resource *resource;
4003
4004    /* retrieve named output; or create it if it doesn't exist */
4005    output = _e_comp_wl_output_get(e_comp_wl->outputs, id);
4006    if (!output)
4007      {
4008         if (!(output = E_NEW(E_Comp_Wl_Output, 1))) return EINA_FALSE;
4009
4010         if (id) output->id = eina_stringshare_add(id);
4011         if (make)
4012           output->make = eina_stringshare_add(make);
4013         else
4014           output->make = eina_stringshare_add("unknown");
4015         if (model)
4016           output->model = eina_stringshare_add(model);
4017         else
4018           output->model = eina_stringshare_add("unknown");
4019
4020         e_comp_wl->outputs = eina_list_append(e_comp_wl->outputs, output);
4021
4022         output->global =
4023           wl_global_create(e_comp_wl->wl.disp, &wl_output_interface,
4024                            2, output, _e_comp_wl_cb_output_bind);
4025
4026         output->resources = NULL;
4027         output->scale = e_scale;
4028      }
4029
4030    /* update the output details */
4031    output->x = x;
4032    output->y = y;
4033    output->w = w;
4034    output->h = h;
4035    output->phys_width = pw;
4036    output->phys_height = ph;
4037    output->refresh = refresh;
4038    output->subpixel = subpixel;
4039    output->transform = transform;
4040
4041    if (output->scale <= 0)
4042      output->scale = e_scale;
4043
4044    /* if we have bound resources, send updates */
4045    EINA_LIST_FOREACH(output->resources, l2, resource)
4046      {
4047         wl_output_send_geometry(resource,
4048                                 output->x, output->y,
4049                                 output->phys_width,
4050                                 output->phys_height,
4051                                 output->subpixel,
4052                                 output->make ?: "", output->model ?: "",
4053                                 output->transform);
4054
4055         if (wl_resource_get_version(resource) >= WL_OUTPUT_SCALE_SINCE_VERSION)
4056           wl_output_send_scale(resource, output->scale);
4057
4058         wl_output_send_mode(resource, WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED,
4059                             output->w, output->h, output->refresh);
4060
4061         if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4062           wl_output_send_done(resource);
4063      }
4064
4065    return EINA_TRUE;
4066 }
4067
4068 EINTERN void
4069 e_comp_wl_output_remove(const char *id)
4070 {
4071    E_Comp_Wl_Output *output;
4072
4073    output = _e_comp_wl_output_get(e_comp_wl->outputs, id);
4074    if (output)
4075      {
4076         e_comp_wl->outputs = eina_list_remove(e_comp_wl->outputs, output);
4077
4078         /* wl_global_destroy(output->global); */
4079
4080         /* eina_stringshare_del(output->id); */
4081         /* eina_stringshare_del(output->make); */
4082         /* eina_stringshare_del(output->model); */
4083
4084         /* free(output); */
4085      }
4086 }
4087
4088 static void
4089 _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)
4090 {
4091    struct wl_resource *res;
4092    Eina_List *l;
4093    uint32_t serial, keycode;
4094    struct wl_client *wc = NULL;
4095    E_Comp_Config *comp_conf = NULL;
4096    const char *device_name = NULL;
4097
4098    keycode = (ev->keycode - 8);
4099
4100    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4101
4102    comp_conf = e_comp_config_get();
4103
4104    if (ec && ec->comp_data)
4105      {
4106         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4107
4108         if (surface)
4109           wc = wl_resource_get_client(surface);
4110      }
4111
4112    if (e_config->key_input_ttrace_enable)
4113      {
4114         TRACE_INPUT_BEGIN(wl_keyboard_send_key:%s:%s, (state ? "PRESS" : "RELEASE"), ev->keyname);
4115         ELOGF("INPUT", "wl_keyboard_send_key:%s:%s|B|", NULL, (state ? "PRESS" : "RELEASE"), ev->keyname);
4116      }
4117
4118    EINA_LIST_FOREACH(key_list, l, res)
4119      {
4120         if (wl_resource_get_client(res) != wc) continue;
4121
4122         TRACE_INPUT_BEGIN(_e_comp_wl_key_send);
4123         if (!e_input_thread_mode_get())
4124           {
4125              _e_comp_wl_send_event_device(wc, ev->timestamp, ev->dev, serial);
4126              device_name = ecore_device_name_get(ev->dev);
4127           }
4128         else
4129           {
4130              if (dev)
4131                {
4132                   _e_comp_wl_send_event_e_device(wc, ev->timestamp, dev, serial);
4133                   device_name = e_device_name_get(dev);
4134                }
4135           }
4136
4137         if (comp_conf && comp_conf->input_log_enable)
4138           ELOGF("Key", "Send Key %s (time: %d, device: %s)", ec, (state ? "Down" : "Up"), ev->timestamp, device_name);
4139
4140         wl_keyboard_send_key(res, serial, ev->timestamp,
4141                              keycode, state);
4142         TRACE_INPUT_END();
4143      }
4144
4145    if (e_config->key_input_ttrace_enable)
4146      {
4147         TRACE_INPUT_END();
4148         ELOGF("INPUT", "wl_keyboard_send_key|E|", NULL);
4149      }
4150 }
4151
4152 EINTERN Eina_Bool
4153 e_comp_wl_key_down(Ecore_Event_Key *ev, E_Device *dev)
4154 {
4155    E_Client *ec = NULL;
4156    uint32_t keycode;
4157    E_Comp_Wl_Key_Data *end, *k;
4158
4159    if (ev->window != e_comp->ee_win)
4160      {
4161         return EINA_FALSE;
4162      }
4163
4164    keycode = (ev->keycode - 8);
4165    if (!(e_comp_wl = e_comp->wl_comp_data))
4166      {
4167         return EINA_FALSE;
4168      }
4169
4170 #ifndef E_RELEASE_BUILD
4171    if ((ev->modifiers & ECORE_EVENT_MODIFIER_CTRL) &&
4172        ((ev->modifiers & ECORE_EVENT_MODIFIER_ALT) ||
4173        (ev->modifiers & ECORE_EVENT_MODIFIER_ALTGR)) &&
4174        eina_streq(ev->key, "BackSpace"))
4175      {
4176         exit(0);
4177      }
4178 #endif
4179
4180    end = (E_Comp_Wl_Key_Data *)e_comp_input_key->kbd.keys.data + (e_comp_input_key->kbd.keys.size / sizeof(*k));
4181
4182    for (k = e_comp_input_key->kbd.keys.data; k < end; k++)
4183      {
4184         /* ignore server-generated key repeats */
4185         if (k->key == keycode)
4186           {
4187              return EINA_FALSE;
4188           }
4189      }
4190
4191    if ((!e_client_action_get()) && (!e_comp->input_key_grabs))
4192      {
4193         ec = e_client_focused_get();
4194         struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4195         if (ec && ec->comp_data && surface)
4196           {
4197              if (e_comp_input_key->kbd.focused)
4198                {
4199                   _e_comp_wl_key_send(ev, dev, WL_KEYBOARD_KEY_STATE_PRESSED, e_comp_input_key->kbd.focused, ec);
4200
4201                   /* A key only sent to clients is added to the list */
4202                   e_comp_input_key->kbd.keys.size = (const char *)end - (const char *)e_comp_input_key->kbd.keys.data;
4203
4204                   if (!(k = wl_array_add(&e_comp_input_key->kbd.keys, sizeof(*k))))
4205                     {
4206                        DBG("wl_array_add: Out of memory\n");
4207                        return EINA_FALSE;
4208                     }
4209                   k->key = keycode;
4210                   k->dev = ev->dev;
4211                }
4212           }
4213      }
4214
4215    /* update modifier state */
4216    e_comp_wl_input_keyboard_state_update(keycode, EINA_TRUE);
4217
4218    return !!ec;
4219 }
4220
4221 EINTERN Eina_Bool
4222 e_comp_wl_key_up(Ecore_Event_Key *ev, E_Device *dev)
4223 {
4224    E_Client *ec = NULL;
4225    uint32_t keycode, delivered_key;
4226    E_Comp_Wl_Key_Data *end, *k;
4227
4228    if (ev->window != e_comp->ee_win)
4229      {
4230         return EINA_FALSE;
4231      }
4232
4233    keycode = (ev->keycode - 8);
4234    delivered_key = 0;
4235    if (!(e_comp_wl = e_comp->wl_comp_data))
4236      {
4237         return EINA_FALSE;
4238      }
4239
4240    end = (E_Comp_Wl_Key_Data *)e_comp_input_key->kbd.keys.data + (e_comp_input_key->kbd.keys.size / sizeof(*k));
4241    for (k = e_comp_input_key->kbd.keys.data; k < end; k++)
4242      {
4243         if (k->key == keycode)
4244           {
4245              *k = *--end;
4246              delivered_key = 1;
4247           }
4248      }
4249
4250    e_comp_input_key->kbd.keys.size =
4251      (const char *)end - (const char *)e_comp_input_key->kbd.keys.data;
4252
4253    /* 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) */
4254    if ((delivered_key) ||
4255        ((!e_client_action_get()) && (!e_comp->input_key_grabs)))
4256      {
4257         ec = e_client_focused_get();
4258
4259         if (e_comp_input_key->kbd.focused)
4260           {
4261              _e_comp_wl_key_send(ev, dev, WL_KEYBOARD_KEY_STATE_RELEASED, e_comp_input_key->kbd.focused, ec);
4262           }
4263      }
4264
4265    /* update modifier state */
4266    e_comp_wl_input_keyboard_state_update(keycode, EINA_FALSE);
4267
4268    return !!ec;
4269 }
4270
4271 EINTERN Eina_Bool
4272 e_comp_wl_key_process(Ecore_Event_Key *ev, E_Device *dev, int type)
4273 {
4274    Eina_Bool res = EINA_FALSE;
4275
4276    if (type == ECORE_EVENT_KEY_DOWN)
4277      {
4278         res = e_comp_wl_key_down(ev, dev);
4279      }
4280    else if (type == ECORE_EVENT_KEY_UP)
4281      {
4282         res = e_comp_wl_key_up(ev, dev);
4283      }
4284
4285    return res;
4286 }
4287
4288 EINTERN Eina_Bool
4289 e_comp_wl_evas_handle_mouse_button(E_Client *ec, uint32_t timestamp, uint32_t button_id, uint32_t state)
4290 {
4291    Eina_List *l;
4292    struct wl_client *wc;
4293    uint32_t serial, btn;
4294    struct wl_resource *res;
4295    E_Comp_Config *comp_conf = NULL;
4296
4297    if (ec->cur_mouse_action || e_comp_wl->drag)
4298      return EINA_FALSE;
4299    if (e_object_is_del(E_OBJECT(ec))) return EINA_FALSE;
4300    if ((ec->ignored) && (!ec->remote_surface.provider)) return EINA_FALSE;
4301
4302    switch (button_id)
4303      {
4304       case 1:  btn = BTN_LEFT;   break;
4305       case 2:  btn = BTN_MIDDLE; break;
4306       case 3:  btn = BTN_RIGHT;  break;
4307       default: btn = button_id;  break;
4308      }
4309
4310    e_comp_wl->ptr.button = btn;
4311    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4312    if (!surface) return EINA_FALSE;
4313
4314    if (!eina_list_count(e_comp_wl->ptr.resources))
4315      return EINA_TRUE;
4316
4317    wc = wl_resource_get_client(surface);
4318    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4319
4320    comp_conf = e_comp_config_get();
4321
4322    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
4323      {
4324         if (wl_resource_get_client(res) != wc) continue;
4325         if (!e_comp_wl_input_pointer_check(res)) continue;
4326         TRACE_INPUT_BEGIN(e_comp_wl_evas_handle_mouse_button);
4327
4328         if (comp_conf && comp_conf->input_log_enable)
4329           ELOGF("Mouse", "Button %s (btn: %d, time: %d)", ec, (state ? "Down" : "Up"), btn, timestamp);
4330
4331         wl_pointer_send_button(res, serial, timestamp, btn, state);
4332         TRACE_INPUT_END();
4333      }
4334    return EINA_TRUE;
4335 }
4336
4337 E_API void
4338 e_comp_wl_touch_cancel(void)
4339 {
4340    _e_comp_wl_touch_cancel();
4341 }
4342
4343 E_API E_Comp_Wl_Hook *
4344 e_comp_wl_hook_add(E_Comp_Wl_Hook_Point hookpoint, E_Comp_Wl_Hook_Cb func, const void *data)
4345 {
4346    E_Comp_Wl_Hook *ch;
4347
4348    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_HOOK_LAST, NULL);
4349    ch = E_NEW(E_Comp_Wl_Hook, 1);
4350    if (!ch) return NULL;
4351    ch->hookpoint = hookpoint;
4352    ch->func = func;
4353    ch->data = (void*)data;
4354    _e_comp_wl_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_hooks[hookpoint], EINA_INLIST_GET(ch));
4355    return ch;
4356 }
4357
4358 E_API void
4359 e_comp_wl_hook_del(E_Comp_Wl_Hook *ch)
4360 {
4361    ch->delete_me = 1;
4362    if (_e_comp_wl_hooks_walking == 0)
4363      {
4364         _e_comp_wl_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4365         free(ch);
4366      }
4367    else
4368      _e_comp_wl_hooks_delete++;
4369 }
4370
4371 E_API E_Comp_Wl_Pid_Hook *
4372 e_comp_wl_pid_hook_add(E_Comp_Wl_Pid_Hook_Point hookpoint, E_Comp_Wl_Pid_Hook_Cb func, const void *data)
4373 {
4374    E_Comp_Wl_Pid_Hook *ch;
4375
4376    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_PID_HOOK_LAST, NULL);
4377
4378    ch = E_NEW(E_Comp_Wl_Pid_Hook, 1);
4379    EINA_SAFETY_ON_NULL_RETURN_VAL(ch, NULL);
4380
4381    ch->hookpoint = hookpoint;
4382    ch->func = func;
4383    ch->data = (void*)data;
4384    _e_comp_wl_pid_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_pid_hooks[hookpoint], EINA_INLIST_GET(ch));
4385
4386    return ch;
4387 }
4388
4389 E_API void
4390 e_comp_wl_pid_hook_del(E_Comp_Wl_Pid_Hook *ch)
4391 {
4392    ch->delete_me = 1;
4393    if (_e_comp_wl_pid_hooks_walking == 0)
4394      {
4395         _e_comp_wl_pid_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_pid_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4396         free(ch);
4397      }
4398    else
4399      _e_comp_wl_pid_hooks_delete++;
4400 }
4401
4402 E_API E_Comp_Wl_Intercept_Hook *
4403 e_comp_wl_intercept_hook_add(E_Comp_Wl_Intercept_Hook_Point hookpoint, E_Comp_Wl_Intercept_Hook_Cb func, const void *data)
4404 {
4405    E_Comp_Wl_Intercept_Hook *ch;
4406
4407    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_COMP_WL_INTERCEPT_HOOK_LAST, NULL);
4408    ch = E_NEW(E_Comp_Wl_Intercept_Hook, 1);
4409    if (!ch) return NULL;
4410    ch->hookpoint = hookpoint;
4411    ch->func = func;
4412    ch->data = (void*)data;
4413    _e_comp_wl_intercept_hooks[hookpoint] = eina_inlist_append(_e_comp_wl_intercept_hooks[hookpoint], EINA_INLIST_GET(ch));
4414    return ch;
4415 }
4416
4417 E_API void
4418 e_comp_wl_intercept_hook_del(E_Comp_Wl_Intercept_Hook *ch)
4419 {
4420    ch->delete_me = 1;
4421    if (_e_comp_wl_intercept_hooks_walking == 0)
4422      {
4423         _e_comp_wl_intercept_hooks[ch->hookpoint] = eina_inlist_remove(_e_comp_wl_intercept_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
4424         free(ch);
4425      }
4426    else
4427      _e_comp_wl_intercept_hooks_delete++;
4428 }
4429
4430 EINTERN void
4431 e_comp_wl_shell_surface_ready(E_Client *ec)
4432 {
4433    if (!ec) return;
4434
4435    _e_comp_wl_hook_call(E_COMP_WL_HOOK_SHELL_SURFACE_READY, ec);
4436 }
4437
4438 E_API void
4439 e_comp_wl_input_cursor_timer_enable_set(Eina_Bool enabled)
4440 {
4441    e_config->use_cursor_timer = !!enabled;
4442
4443    if (e_config->use_cursor_timer)
4444      {
4445         if (!e_pointer_is_hidden(e_comp->pointer))
4446           {
4447              _e_comp_wl_cursor_move_timer_control(e_comp_wl->ptr.ec);
4448           }
4449      }
4450    else
4451      {
4452         if (e_comp_wl->ptr.hide_tmr)
4453           {
4454              ecore_timer_del(e_comp_wl->ptr.hide_tmr);
4455              e_comp_wl->ptr.hide_tmr = NULL;
4456           }
4457         cursor_timer_ec = NULL;
4458
4459         if (e_comp_wl->ptr.ec && e_comp_wl->ptr.ec->has_cursor_unset)
4460           return;
4461
4462         if (e_pointer_is_hidden(e_comp->pointer))
4463           {
4464              _e_comp_wl_cursor_reload(e_comp_wl->ptr.ec);
4465           }
4466      }
4467 }
4468
4469 EINTERN void
4470 e_comp_wl_send_event_device(struct wl_client *wc, uint32_t timestamp, Ecore_Device *dev, uint32_t serial)
4471 {
4472    EINA_SAFETY_ON_NULL_RETURN(wc);
4473    EINA_SAFETY_ON_NULL_RETURN(dev);
4474
4475    _e_comp_wl_send_event_device(wc, timestamp, dev, serial);
4476 }
4477
4478 EINTERN void
4479 e_comp_wl_send_event_e_device(struct wl_client *wc, uint32_t timestamp, E_Device *dev, uint32_t serial)
4480 {
4481    EINA_SAFETY_ON_NULL_RETURN(wc);
4482    EINA_SAFETY_ON_NULL_RETURN(dev);
4483
4484    _e_comp_wl_send_event_e_device(wc, timestamp, dev, serial);
4485 }
4486
4487 EINTERN Eina_Bool
4488 e_comp_wl_key_send(E_Client *ec, int keycode, Eina_Bool pressed, void *dev, uint32_t time)
4489 {
4490    struct wl_resource *res;
4491    struct wl_client *wc;
4492    Eina_List *l;
4493    uint32_t serial, wl_keycode;
4494    enum wl_keyboard_key_state state;
4495    E_Comp_Config *comp_conf = NULL;
4496    const char *dev_name = NULL;
4497
4498    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4499    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4500    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4501
4502    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4503    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4504
4505    wl_keycode = keycode - 8;
4506    EINA_SAFETY_ON_TRUE_RETURN_VAL(wl_keycode <= 0, EINA_FALSE);
4507
4508    wc = wl_resource_get_client(surface);
4509    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4510    if (!time) time = e_util_timestamp_get();
4511    if (pressed) state = WL_KEYBOARD_KEY_STATE_PRESSED;
4512    else state = WL_KEYBOARD_KEY_STATE_RELEASED;
4513
4514    comp_conf = e_comp_config_get();
4515    e_keyrouter_event_surface_send(ec, keycode);
4516
4517    if (e_config->key_input_ttrace_enable)
4518      {
4519         TRACE_INPUT_BEGIN(wl_keyboard_send_key:%s:%d, (state ? "PRESS" : "RELEASE"), keycode);
4520         ELOGF("INPUT", "wl_keyboard_send_key:%s:%d|B|", NULL, (state ? "PRESS" : "RELEASE"), keycode);
4521      }
4522
4523    EINA_LIST_FOREACH(e_comp_input_key->kbd.resources, l, res)
4524      {
4525         if (wl_resource_get_client(res) != wc) continue;
4526         if (!e_input_thread_mode_get())
4527           {
4528              if (dev)
4529                {
4530                   _e_comp_wl_send_event_device(wc, time, (Ecore_Device *)dev, serial);
4531                   dev_name = ecore_device_name_get((Ecore_Device *)dev);
4532                }
4533              else
4534                {
4535                   _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_KEYBOARD, time);
4536                }
4537           }
4538         else
4539           {
4540              if (dev)
4541                {
4542                   e_comp_wl_send_event_e_device(wc, time, (E_Device *)dev, serial);
4543                   dev_name = e_device_name_get(dev);
4544                }
4545           }
4546
4547         if (comp_conf && comp_conf->input_log_enable)
4548           ELOGF("Key", "Send Key %s (keycode: %d, time: %d, device: %s)", ec, (state ? "Down" : "Up"), wl_keycode, time,
4549                 dev_name);
4550
4551         wl_keyboard_send_key(res, serial, time,
4552                              wl_keycode, state);
4553      }
4554
4555    if (e_config->key_input_ttrace_enable)
4556      {
4557         TRACE_INPUT_END();
4558         ELOGF("INPUT", "wl_keyboard_send_key|E|", NULL);
4559      }
4560
4561    return EINA_TRUE;
4562 }
4563
4564 EINTERN Eina_Bool
4565 e_comp_wl_key_cancel(E_Client *ec, int keycode, Ecore_Device *dev, uint32_t time)
4566 {
4567    struct wl_resource *res;
4568    struct wl_client *wc;
4569    Eina_List *l;
4570    uint32_t serial, wl_keycode, cancel_keycode;
4571    E_Comp_Config *comp_conf = NULL;
4572    struct xkb_keymap *keymap = NULL;
4573
4574    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4575    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4576    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4577
4578    keymap = e_comp_input_key->xkb.keymap;
4579    EINA_SAFETY_ON_NULL_RETURN_VAL(keymap, EINA_FALSE);
4580
4581    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4582    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4583
4584    cancel_keycode = e_comp_wl_input_keymap_keyname_to_keycode("Cancel");
4585    if (cancel_keycode == XKB_KEYCODE_INVALID)
4586      {
4587         ELOGF("Key", "Failed to send key cancel for %d key, Cancel key is not supported\n", ec, keycode);
4588         return EINA_FALSE;
4589      }
4590
4591    EINA_SAFETY_ON_TRUE_RETURN_VAL(cancel_keycode < 8, EINA_FALSE);
4592
4593    cancel_keycode = cancel_keycode - 8;
4594
4595    wl_keycode = keycode - 8;
4596    EINA_SAFETY_ON_TRUE_RETURN_VAL(wl_keycode <= 0, EINA_FALSE);
4597
4598    wc = wl_resource_get_client(surface);
4599    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4600    if (!time) time = e_util_timestamp_get();
4601
4602    comp_conf = e_comp_config_get();
4603    e_keyrouter_event_surface_send(ec, keycode);
4604
4605    EINA_LIST_FOREACH(e_comp_input_key->kbd.resources, l, res)
4606      {
4607         if (wl_resource_get_client(res) != wc) continue;
4608         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4609         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_KEYBOARD, time);
4610
4611         if (comp_conf && comp_conf->input_log_enable)
4612           ELOGF("Key", "Send Key Cancel (time: %d)", ec, time);
4613         wl_keyboard_send_key(res, serial, time,
4614                              cancel_keycode, WL_KEYBOARD_KEY_STATE_PRESSED);
4615         wl_keyboard_send_key(res, serial, time,
4616                              wl_keycode, WL_KEYBOARD_KEY_STATE_RELEASED);
4617         wl_keyboard_send_key(res, serial, time,
4618                              cancel_keycode, WL_KEYBOARD_KEY_STATE_RELEASED);
4619      }
4620
4621    return EINA_TRUE;
4622 }
4623
4624
4625 EINTERN Eina_Bool
4626 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)
4627 {
4628    struct wl_client *wc;
4629    uint32_t serial;
4630    E_Devicemgr_Input_Device *device = NULL;
4631
4632    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4633    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4634    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4635
4636    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4637    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4638
4639    if (!dev) device = _e_comp_wl_device_last_device_get(ECORE_DEVICE_CLASS_TOUCH);
4640
4641    wc = wl_resource_get_client(surface);
4642    if (!time) time = e_util_timestamp_get();
4643    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4644
4645    if (dev)
4646      {
4647         _e_comp_wl_send_event_device(wc, time, dev, serial);
4648         _e_comp_wl_device_handle_axes(ecore_device_identifier_get(dev), ECORE_DEVICE_CLASS_TOUCH, ec, idx, radius_x, radius_y, pressure, angle);
4649      }
4650    else if (device)
4651      {
4652         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_TOUCH, time);
4653         _e_comp_wl_device_handle_axes(device->identifier, device->clas, ec, idx, radius_x, radius_y, pressure, angle);
4654      }
4655
4656    x = x + ec->client.x;
4657    y = y + ec->client.y;
4658
4659    _e_comp_wl_send_touch(ec, idx, x, y, time, pressed);
4660
4661    return EINA_TRUE;
4662 }
4663
4664 EINTERN Eina_Bool
4665 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)
4666 {
4667    E_Devicemgr_Input_Device *device;
4668    uint32_t serial;
4669    struct wl_client *wc;
4670
4671    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4672    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4673    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4674
4675    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4676    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4677
4678    if (!dev) device = _e_comp_wl_device_last_device_get(ECORE_DEVICE_CLASS_TOUCH);
4679
4680    wc = wl_resource_get_client(surface);
4681    if (!time) time = e_util_timestamp_get();
4682    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4683
4684    if (dev)
4685      {
4686         _e_comp_wl_send_event_device(wc, time, dev, serial);
4687         _e_comp_wl_device_handle_axes(ecore_device_identifier_get(dev), ECORE_DEVICE_CLASS_TOUCH, ec, idx, radius_x, radius_y, pressure, angle);
4688      }
4689    else if (device)
4690      {
4691         _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_TOUCH, time);
4692         _e_comp_wl_device_handle_axes(device->identifier, device->clas, ec, idx, radius_x, radius_y, pressure, angle);
4693      }
4694
4695    x = x + ec->client.x;
4696    y = y + ec->client.y;
4697
4698    _e_comp_wl_send_touch_move(ec, idx, x, y, time);
4699
4700    return EINA_TRUE;
4701 }
4702
4703 EINTERN Eina_Bool
4704 e_comp_wl_touch_cancel_send(E_Client *ec)
4705 {
4706    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4707    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4708    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4709
4710    _e_comp_wl_send_touch_cancel(ec);
4711
4712    return EINA_TRUE;
4713 }
4714
4715 EINTERN Eina_Bool
4716 e_comp_wl_mouse_button_send(E_Client *ec, int buttons, Eina_Bool pressed, Ecore_Device *dev, uint32_t time)
4717 {
4718    uint32_t serial;
4719    struct wl_client *wc;
4720
4721    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4722    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4723    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4724
4725    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4726    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4727
4728    wc = wl_resource_get_client(surface);
4729    if (!time) time = e_util_timestamp_get();
4730    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4731
4732    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4733    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4734
4735    if (pressed)
4736      e_comp_wl_evas_handle_mouse_button(ec, time, buttons,
4737                                           WL_POINTER_BUTTON_STATE_PRESSED);
4738    else
4739      e_comp_wl_evas_handle_mouse_button(ec, time, buttons,
4740                                           WL_POINTER_BUTTON_STATE_RELEASED);
4741
4742    return EINA_TRUE;
4743 }
4744
4745 EINTERN Eina_Bool
4746 e_comp_wl_mouse_move_send(E_Client *ec, int x, int y, Ecore_Device *dev, uint32_t time)
4747 {
4748    uint32_t serial;
4749    struct wl_client *wc;
4750
4751    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4752    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4753    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4754
4755    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4756    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4757
4758    wc = wl_resource_get_client(surface);
4759    if (!time) time = e_util_timestamp_get();
4760    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4761
4762    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4763    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4764
4765    x = x + ec->client.x;
4766    y = y + ec->client.y;
4767
4768    _e_comp_wl_send_mouse_move(ec, x, y, time);
4769
4770    return EINA_TRUE;
4771 }
4772
4773 EINTERN Eina_Bool
4774 e_comp_wl_mouse_wheel_send(E_Client *ec, int direction, int z, Ecore_Device *dev, uint32_t time)
4775 {
4776    uint32_t serial;
4777    struct wl_client *wc;
4778
4779    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4780    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4781    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4782
4783    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4784    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4785
4786    wc = wl_resource_get_client(surface);
4787    if (!time) time = e_util_timestamp_get();
4788    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4789
4790    if (_e_comp_wl_check_cursor_timer_needed(ec))
4791       {
4792          if (!_e_comp_wl_cursor_timer_control(EVAS_CALLBACK_MOUSE_WHEEL, ec))
4793            return EINA_TRUE;
4794       }
4795
4796    if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4797    else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4798
4799    _e_comp_wl_mouse_wheel_send(ec, direction, z, time);
4800
4801    if (!need_send_released) // set cursor's hide_tmr only when mouse button is not pressed
4802      {
4803         if (_e_comp_wl_check_cursor_timer_needed(ec))
4804           _e_comp_wl_cursor_move_timer_control(ec);
4805      }
4806
4807    return EINA_TRUE;
4808 }
4809
4810 EINTERN Eina_Bool
4811 e_comp_wl_mouse_in_send(E_Client *ec, int x, int y, Ecore_Device *dev, uint32_t time)
4812 {
4813    uint32_t serial;
4814    struct wl_client *wc;
4815    struct wl_resource *res;
4816    Eina_List *l;
4817
4818    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4819    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4820
4821    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4822    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4823
4824    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4825    EINA_SAFETY_ON_TRUE_RETURN_VAL(e_object_is_del(E_OBJECT(ec)), EINA_FALSE);
4826
4827    if (!eina_list_count(e_comp_wl->ptr.resources)) return EINA_FALSE;
4828    wc = wl_resource_get_client(surface);
4829    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4830    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
4831      {
4832         if (!e_comp_wl_input_pointer_check(res)) continue;
4833         if (wl_resource_get_client(res) != wc) continue;
4834
4835         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4836         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4837
4838         wl_pointer_send_enter(res, serial, surface,
4839                               wl_fixed_from_int(x),
4840                               wl_fixed_from_int(y));
4841         ec->pointer_enter_sent = EINA_TRUE;
4842      }
4843    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_mousein_signal, ec);
4844
4845    return EINA_TRUE;
4846 }
4847
4848 EINTERN Eina_Bool
4849 e_comp_wl_mouse_out_send(E_Client *ec, Ecore_Device *dev, uint32_t time)
4850 {
4851    uint32_t serial;
4852    struct wl_client *wc;
4853    struct wl_resource *res;
4854    Eina_List *l;
4855
4856    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
4857    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
4858    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
4859    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4860    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4861    EINA_SAFETY_ON_TRUE_RETURN_VAL(e_object_is_del(E_OBJECT(ec)), EINA_FALSE);
4862
4863    if (!eina_list_count(e_comp_wl->ptr.resources)) return EINA_FALSE;
4864    wc = wl_resource_get_client(surface);
4865    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4866    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
4867      {
4868         if (!e_comp_wl_input_pointer_check(res)) continue;
4869         if (wl_resource_get_client(res) != wc) continue;
4870
4871         if (dev) _e_comp_wl_send_event_device(wc, time, dev, serial);
4872         else _e_comp_wl_device_send_last_event_device(ec, ECORE_DEVICE_CLASS_MOUSE, time);
4873
4874         wl_pointer_send_leave(res, serial, surface);
4875         ec->pointer_enter_sent = EINA_FALSE;
4876      }
4877    wl_signal_emit(&e_comp_wl->ptr_constraints.surface_mouseout_signal, ec);
4878
4879    return EINA_TRUE;
4880 }
4881
4882 EINTERN void
4883 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)
4884 {
4885    Evas_Event_Mouse_In ev_in;
4886
4887    if (!ec) return;
4888    if (ec->pointer_enter_sent) return;
4889
4890    ev_in.buttons = buttons;
4891
4892    ev_in.output.x = x;
4893    ev_in.output.y = y;
4894    ev_in.canvas.x = x;
4895    ev_in.canvas.y = y;
4896
4897    ev_in.data = data;
4898    ev_in.modifiers = modifiers;
4899    ev_in.locks = locks;
4900    ev_in.timestamp = timestamp;
4901    ev_in.event_flags = event_flags;
4902
4903    ev_in.dev = dev;
4904    ev_in.event_src = event_src;
4905
4906    _e_comp_wl_evas_cb_mouse_in((void *)ec, NULL, NULL, &ev_in);
4907 }
4908
4909 EINTERN void
4910 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)
4911 {
4912    Evas_Event_Mouse_Out ev_out;
4913
4914    if (!ec) return;
4915    if (!ec->pointer_enter_sent) return;
4916
4917    ev_out.buttons = buttons;
4918
4919    ev_out.output.x = x;
4920    ev_out.output.y = y;
4921    ev_out.canvas.x = x;
4922    ev_out.canvas.y = y;
4923
4924    ev_out.data = data;
4925    ev_out.modifiers = modifiers;
4926    ev_out.locks = locks;
4927    ev_out.timestamp = timestamp;
4928    ev_out.event_flags = event_flags;
4929
4930    ev_out.dev = dev;
4931    ev_out.event_src = event_src;
4932
4933    _e_comp_wl_evas_cb_mouse_out((void *)ec, NULL, NULL, &ev_out);
4934 }
4935
4936 EINTERN Eina_Bool
4937 e_comp_wl_cursor_hide(E_Client *ec)
4938 {
4939    struct wl_resource *res;
4940    struct wl_client *wc;
4941    Eina_List *l;
4942    uint32_t serial;
4943
4944    e_pointer_object_set(e_comp->pointer, NULL, 0, 0);
4945
4946    if (e_comp_wl->ptr.hide_tmr)
4947      {
4948         ecore_timer_del(e_comp_wl->ptr.hide_tmr);
4949         e_comp_wl->ptr.hide_tmr = NULL;
4950      }
4951    cursor_timer_ec = NULL;
4952
4953    if (!ec) return EINA_FALSE;
4954    if (e_object_is_del(E_OBJECT(ec))) return EINA_FALSE;
4955    struct wl_resource *surface = e_comp_wl_client_surface_get(ec);
4956    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, EINA_FALSE);
4957
4958    wc = wl_resource_get_client(surface);
4959    serial = wl_display_next_serial(e_comp_wl->wl.disp);
4960    EINA_LIST_FOREACH(e_comp_wl->ptr.resources, l, res)
4961      {
4962         if (!e_comp_wl_input_pointer_check(res)) continue;
4963         if (wl_resource_get_client(res) != wc) continue;
4964         if (ec->pointer_enter_sent == EINA_FALSE) continue;
4965         wl_pointer_send_leave(res, serial, surface);
4966         ec->pointer_enter_sent = EINA_FALSE;
4967      }
4968
4969    return EINA_TRUE;
4970 }
4971
4972 /* surface to buffer
4973  *   - width    : surface width
4974  *   - height   : surface height
4975  *   - transform: buffer transform
4976  *   - scale    : buffer scale
4977  * screen to output
4978  *   - width    : screen width
4979  *   - height   : screen height
4980  *   - transform: output transform
4981  *   - scale    : output scale
4982  */
4983 static E_Util_Transform_Matrix
4984 _e_comp_wl_buffer_coord_get(int width, int height, int transform, int scale)
4985 {
4986    E_Util_Transform_Matrix m;
4987
4988    e_util_transform_matrix_load_identity(&m);
4989
4990    if (transform & 0x4)
4991      {
4992         e_util_transform_matrix_translate(&m, -((double)width / 2), 0, 0);
4993         e_util_transform_matrix_flip_x(&m);
4994         e_util_transform_matrix_translate(&m, (double)width / 2, 0, 0);
4995      }
4996
4997    switch (transform & 0x3)
4998      {
4999       case WL_OUTPUT_TRANSFORM_90:
5000         e_util_transform_matrix_translate(&m, -width, 0, 0);
5001         e_util_transform_matrix_rotation_z(&m, 270);
5002         break;
5003       case WL_OUTPUT_TRANSFORM_180:
5004         e_util_transform_matrix_translate(&m, -width, -height, 0);
5005         e_util_transform_matrix_rotation_z(&m, 180);
5006         break;
5007       case WL_OUTPUT_TRANSFORM_270:
5008         e_util_transform_matrix_translate(&m, 0, -height, 0);
5009         e_util_transform_matrix_rotation_z(&m, 90);
5010         break;
5011       default:
5012         break;
5013      }
5014
5015    e_util_transform_matrix_scale(&m, scale, scale, 1);
5016
5017    return m;
5018 }
5019
5020 /* surface to buffer
5021  *   - surface width, surface height, buffer transform, buffer scale
5022  * screen to output
5023  *   - screen width, screen height, output transform, output scale
5024  */
5025 EINTERN void
5026 e_comp_wl_pos_convert(int width, int height, int transform, int scale, int sx, int sy, int *bx, int *by)
5027 {
5028    E_Util_Transform_Matrix m;
5029    E_Util_Transform_Vertex v;
5030
5031    m = _e_comp_wl_buffer_coord_get(width, height, transform, scale);
5032
5033    e_util_transform_vertex_init(&v, sx, sy, 0.0, 1.0);
5034    v = e_util_transform_matrix_multiply_vertex(&m, &v);
5035    e_util_transform_vertex_pos_round_get(&v, bx, by, NULL, NULL);
5036 }
5037
5038 /* buffer to screen
5039  *   - buffer width, buffer height, buffer transform, buffer scale
5040  */
5041 EINTERN void
5042 e_comp_wl_pos_convert_inverse(int width, int height, int transform, int scale, int bx, int by, int *sx, int *sy)
5043 {
5044    E_Util_Transform_Matrix m;
5045    E_Util_Transform_Vertex v;
5046    int tw, th;
5047
5048    if (transform != 0 || scale > 1)
5049      {
5050         tw = ((transform % 2) ? height : width) / scale;
5051         th = ((transform % 2) ? width : height) / scale;
5052      }
5053    else
5054      {
5055         tw = width;
5056         th = height;
5057      }
5058
5059    m = _e_comp_wl_buffer_coord_get(tw, th, transform, scale);
5060    m = e_util_transform_matrix_inverse_get(&m);
5061
5062    e_util_transform_vertex_init(&v, bx, by, 0.0, 1.0);
5063    v = e_util_transform_matrix_multiply_vertex(&m, &v);
5064    e_util_transform_vertex_pos_round_get(&v, sx, sy, NULL, NULL);
5065 }
5066
5067 /* surface to buffer
5068  *   - surface width, surface height, buffer transform, buffer scale
5069  * screen to output
5070  *   - screen width, screen height, output transform, output scale
5071  */
5072 EINTERN void
5073 e_comp_wl_rect_convert(int width, int height, int transform, int scale,
5074                        int sx, int sy, int sw, int sh, int *bx, int *by, int *bw, int *bh)
5075 {
5076    E_Util_Transform_Matrix m;
5077    E_Util_Transform_Rect sr = {sx, sy, sw, sh};
5078    E_Util_Transform_Rect_Vertex sv;
5079
5080    m = _e_comp_wl_buffer_coord_get(width, height, transform, scale);
5081
5082    sv = e_util_transform_rect_to_vertices(&sr);
5083    sv = e_util_transform_matrix_multiply_rect_vertex(&m, &sv);
5084    sr = e_util_transform_vertices_to_rect(&sv);
5085
5086    if (bx) *bx = sr.x;
5087    if (by) *by = sr.y;
5088    if (bw) *bw = sr.w;
5089    if (bh) *bh = sr.h;
5090 }
5091
5092 /* buffer to screen
5093  *   - buffer width, buffer height, buffer transform, buffer scale
5094  */
5095 EINTERN void
5096 e_comp_wl_rect_convert_inverse(int width, int height, int transform, int scale,
5097                                int bx, int by, int bw, int bh, int *sx, int *sy, int *sw, int *sh)
5098 {
5099    E_Util_Transform_Matrix m;
5100    E_Util_Transform_Rect br = {bx, by, bw, bh};
5101    E_Util_Transform_Rect_Vertex bv;
5102    int tw, th;
5103
5104    if (transform != 0 || scale > 1)
5105      {
5106         tw = ((transform % 2) ? height : width) / scale;
5107         th = ((transform % 2) ? width : height) / scale;
5108      }
5109    else
5110      {
5111         tw = width;
5112         th = height;
5113      }
5114
5115    m = _e_comp_wl_buffer_coord_get(tw, th, transform, scale);
5116    m = e_util_transform_matrix_inverse_get(&m);
5117
5118    bv = e_util_transform_rect_to_vertices(&br);
5119    bv = e_util_transform_matrix_multiply_rect_vertex(&m, &bv);
5120    br = e_util_transform_vertices_to_rect(&bv);
5121
5122    if (sx) *sx = br.x;
5123    if (sy) *sy = br.y;
5124    if (sw) *sw = br.w;
5125    if (sh) *sh = br.h;
5126 }
5127
5128 EINTERN E_Comp_Wl_Output*
5129 e_comp_wl_output_find(E_Client *ec)
5130 {
5131    Eina_List *l;
5132    E_Comp_Wl_Output *output;
5133
5134    if (!ec || !ec->comp_data || e_object_is_del(E_OBJECT(ec))) return NULL;
5135
5136    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5137      {
5138         if (output->transform % 2)
5139           {
5140              if (ec->x < output->y || ec->x >= (output->y + output->h) ||
5141                  ec->y < output->x || ec->y >= (output->x + output->w))
5142                continue;
5143           }
5144         else
5145           {
5146              if (ec->x < output->x || ec->x >= (output->x + output->w) ||
5147                  ec->y < output->y || ec->y >= (output->y + output->h))
5148                continue;
5149           }
5150
5151         return output;
5152      }
5153
5154    return NULL;
5155 }
5156
5157 EINTERN Eina_Array *
5158 e_comp_wl_output_find_all(E_Client *ec)
5159 {
5160    E_Comp_Wl_Output *output;
5161    Eina_Array *outputs;
5162    Eina_List *l;
5163
5164    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
5165
5166    outputs = eina_array_new(4);
5167    if (!outputs)
5168      return NULL;
5169
5170    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5171      {
5172         if (E_INTERSECTS(ec->x, ec->y, ec->w, ec->h,
5173                          output->x, output->y, output->w, output->h))
5174           {
5175              eina_array_push(outputs, output);
5176           }
5177      }
5178
5179    if (eina_array_count(outputs) == 0)
5180      {
5181         eina_array_free(outputs);
5182         return NULL;
5183      }
5184
5185    return outputs;
5186 }
5187
5188 // --------------------------------------------------------
5189 // tizen_move_resize
5190 // --------------------------------------------------------
5191 EINTERN Eina_Bool
5192 e_comp_wl_commit_sync_client_geometry_add(E_Client *ec,
5193                                           uint32_t serial,
5194                                           int32_t x,
5195                                           int32_t y,
5196                                           int32_t w,
5197                                           int32_t h)
5198 {
5199    E_Client_Pending_Geometry *geo;
5200
5201    if (!ec) goto ret;
5202    if (e_object_is_del(E_OBJECT(ec))) goto ret;
5203    if (ec->fullscreen || ec->maximized) goto err;
5204
5205    geo = E_NEW(E_Client_Pending_Geometry, 1);
5206    if (!geo) goto err;
5207
5208    geo->serial = serial;
5209    geo->x = x;
5210    geo->y = y;
5211    geo->w = w;
5212    geo->h = h;
5213
5214    ec->surface_sync.pending_geometry = eina_list_append(ec->surface_sync.pending_geometry, geo);
5215    ec->surface_sync.wait_commit = EINA_TRUE;
5216
5217    return EINA_TRUE;
5218
5219 err:
5220    ELOGF("POSSIZE", "Could not add geometry(new:%d full:%d max:%d)", ec, ec->new_client, ec->fullscreen, ec->maximized);
5221
5222 ret:
5223    return EINA_FALSE;
5224 }
5225
5226 EINTERN Eina_Bool
5227 e_comp_wl_commit_sync_configure(E_Client *ec)
5228 {
5229    Eina_List *l;
5230    E_Client_Pending_Geometry *geo;
5231    int bw, bh;
5232    Eina_Bool match_size = EINA_FALSE;
5233
5234    struct
5235      {
5236         int x, y, w, h;
5237      } config;
5238
5239    if (!ec || !ec->frame) goto ret;
5240    if (e_object_is_del(E_OBJECT(ec))) goto ret;
5241
5242    bw = bh = 0;
5243    config.x = ec->x; config.y = ec->y; config.w = ec->w; config.h = ec->h;
5244    if (!e_pixmap_size_get(ec->pixmap, &bw, &bh)) goto err;
5245
5246    if (eina_list_count(ec->surface_sync.pending_geometry))
5247      {
5248         Eina_List *ll = NULL;
5249         EINA_LIST_REVERSE_FOREACH_SAFE(ec->surface_sync.pending_geometry, l, ll, geo)
5250           {
5251              if (match_size)
5252                {
5253                   ec->surface_sync.pending_geometry = eina_list_remove(ec->surface_sync.pending_geometry, geo);
5254                   E_FREE(geo);
5255                   continue;
5256                }
5257
5258              if ((geo->w == bw) && (geo->h == bh))
5259                {
5260                   match_size = EINA_TRUE;
5261                   config.w = geo->w;
5262                   config.h = geo->h;
5263                   config.x = geo->x;
5264                   config.y = geo->y;
5265                   ec->surface_sync.pending_geometry = eina_list_remove(ec->surface_sync.pending_geometry, geo);
5266                   E_FREE(geo);
5267                }
5268           }
5269
5270         if (match_size)
5271           {
5272              if ((config.w != ec->w) || (config.h != ec->h))
5273                {
5274                   e_client_size_set(ec, config.w, config.h);
5275                   ec->changes.size = EINA_TRUE;
5276                   EC_CHANGED(ec);
5277                }
5278
5279              // FIXME: The geometry of the client would be better to be calculated
5280              //        at e_desk or at e_desk_area. So the position(x,y) of the client
5281              //        should be calcuated at the e_desk or at the e_desk_area.
5282              E_Desk *desk;
5283              desk = e_comp_desk_find_by_ec(ec);
5284              if (desk)
5285                {
5286                   ec->client.x = desk->geom.x + config.x;
5287                   ec->client.y = desk->geom.y + config.y;
5288                }
5289              else
5290                {
5291                   ec->client.x = config.x;
5292                   ec->client.y = config.y;
5293                }
5294
5295              if ((ec->client.x != ec->x) || (ec->client.y != ec->y))
5296                {
5297                   e_client_pos_set(ec, ec->client.x, ec->client.y);
5298                   ec->placed = 1;
5299                   ec->changes.pos = 1;
5300                   EC_CHANGED(ec);
5301                }
5302
5303              ELOGF("POSSIZE", "Configure pending geometry (%d,%d,%dx%d)", ec, ec->x, ec->y, ec->w, ec->h);
5304           }
5305      }
5306
5307    // cw interceptor(move,resize) won't work if wait_commit is TRUE
5308    ec->surface_sync.wait_commit = EINA_FALSE;
5309
5310    e_client_shell_configure(ec, ec->x, ec->y, ec->w, ec->h);
5311
5312    // rollback wait_commit if there are pending requests remained
5313    if (eina_list_count(ec->surface_sync.pending_geometry))
5314      ec->surface_sync.wait_commit = EINA_TRUE;
5315
5316    return EINA_TRUE;
5317
5318 err:
5319    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);
5320
5321 ret:
5322    return EINA_FALSE;
5323 }
5324
5325 static void
5326 _tz_move_resize_iface_cb_destroy(struct wl_client *client EINA_UNUSED,
5327                                  struct wl_resource *res_moveresize)
5328 {
5329    wl_resource_destroy(res_moveresize);
5330 }
5331
5332 static void
5333 _tz_move_resize_iface_cb_set_geometry(struct wl_client *client EINA_UNUSED,
5334                                       struct wl_resource *res_moveresize,
5335                                       struct wl_resource *surface,
5336                                       uint32_t serial,
5337                                       int32_t x,
5338                                       int32_t y,
5339                                       int32_t w,
5340                                       int32_t h)
5341 {
5342    /* to be implemented */
5343    E_Client *ec;
5344    Eina_Bool pending;
5345    int cur_w = 0, cur_h = 0;
5346
5347    ec = e_client_from_surface_resource(surface);
5348    if (!ec) return;
5349
5350    pending = e_client_pending_geometry_has(ec);
5351    e_client_geometry_get(ec, NULL, NULL, &cur_w, &cur_h);
5352    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);
5353
5354    if (!pending)
5355      {
5356         if ((cur_w == w) && (cur_h == h))
5357           {
5358              e_client_pos_set(ec, x, y);
5359              ec->changes.pos = 1;
5360              EC_CHANGED(ec);
5361              return;
5362           }
5363      }
5364
5365    if (!e_comp_wl_commit_sync_client_geometry_add(ec, serial, x, y, w, h)) goto err;
5366    return;
5367
5368 err:
5369    ELOGF("POSSIZE", "Could not add set_geometry request(serial:%d, %d,%d - %dx%d)", ec, serial, x, y, w, h);
5370 }
5371
5372 static const struct tizen_move_resize_interface _tz_move_resize_iface =
5373 {
5374    _tz_move_resize_iface_cb_destroy,
5375    _tz_move_resize_iface_cb_set_geometry,
5376 };
5377
5378 static void
5379 _tz_moveresize_cb_bind(struct wl_client *client,
5380                        void *data EINA_UNUSED,
5381                        uint32_t ver,
5382                        uint32_t id)
5383 {
5384    struct wl_resource *res_moveresize;
5385
5386    res_moveresize = wl_resource_create(client,
5387                                        &tizen_move_resize_interface,
5388                                        ver,
5389                                        id);
5390    EINA_SAFETY_ON_NULL_GOTO(res_moveresize, err);
5391
5392
5393    wl_resource_set_implementation(res_moveresize,
5394                                   &_tz_move_resize_iface,
5395                                   NULL,
5396                                   NULL);
5397    return;
5398
5399 err:
5400    ERR("Could not create tizen_move_resize_interface res: %m");
5401    wl_client_post_no_memory(client);
5402 }
5403
5404 static void
5405 _e_comp_wl_move_resize_init(void)
5406 {
5407    if (!e_comp_wl) return;
5408    if (!e_comp_wl->wl.disp) return;
5409
5410    if (!wl_global_create(e_comp_wl->wl.disp,
5411                          &tizen_move_resize_interface,
5412                          1,
5413                          NULL,
5414                          _tz_moveresize_cb_bind))
5415      {
5416         ERR("Could not create tizen_move_resize_interface to wayland globals: %m");
5417      }
5418
5419    return;
5420 }
5421
5422 EINTERN Eina_Bool
5423 e_comp_wl_pid_output_configured_resolution_send(pid_t pid, int w, int h)
5424 {
5425    E_Comp_Wl_Output *output;
5426    pid_t output_pid = 0;
5427    Eina_List *l = NULL, *l2 = NULL;
5428    struct wl_resource *resource = NULL;
5429
5430    if (!e_config->configured_output_resolution.use) return EINA_TRUE;
5431
5432    EINA_SAFETY_ON_TRUE_RETURN_VAL(pid <= 0, EINA_FALSE);
5433    EINA_SAFETY_ON_TRUE_RETURN_VAL(w < 0, EINA_FALSE);
5434    EINA_SAFETY_ON_TRUE_RETURN_VAL(h < 0, EINA_FALSE);
5435
5436    EINA_LIST_FOREACH(e_comp_wl->outputs, l, output)
5437      {
5438         /* if we have bound resources, send updates */
5439         EINA_LIST_FOREACH(output->resources, l2, resource)
5440           {
5441              wl_client_get_credentials(wl_resource_get_client(resource), &output_pid, NULL, NULL);
5442              if (output_pid != pid) continue;
5443
5444              ELOGF("COMP_WL", "\tSend Configured Output AGAIN ~!!!!! (pid:%d)", NULL, pid);
5445
5446              // send output information to the client
5447              _e_comp_wl_output_info_send(output, resource, pid, w, h);
5448           }
5449      }
5450
5451    return EINA_TRUE;
5452 }
5453
5454 EINTERN void
5455 e_comp_wl_surface_state_init(E_Comp_Wl_Surface_State *state, int w, int h)
5456 {
5457    _e_comp_wl_surface_state_init(state, w, h);
5458 }
5459
5460 EINTERN void
5461 e_comp_wl_surface_state_commit(E_Client *ec, E_Comp_Wl_Surface_State *state)
5462 {
5463    _e_comp_wl_surface_state_commit(ec, state);
5464 }
5465
5466 EINTERN void
5467 e_comp_wl_hook_call(E_Comp_Wl_Hook_Point hookpoint, E_Client *ec)
5468 {
5469    _e_comp_wl_hook_call(hookpoint, ec);
5470 }
5471
5472 EINTERN void
5473 e_comp_wl_surface_state_finish(E_Comp_Wl_Surface_State *state)
5474 {
5475    _e_comp_wl_surface_state_finish(state);
5476 }
5477
5478 EINTERN void
5479 e_comp_wl_surface_state_buffer_set(E_Comp_Wl_Surface_State *state, E_Comp_Wl_Buffer *buffer)
5480 {
5481    _e_comp_wl_surface_state_buffer_set(state, buffer);
5482 }
5483
5484 static void
5485 buffer_transform(int width, int height, uint32_t transform, int32_t scale,
5486                  int sx, int sy, int *dx, int *dy)
5487 {
5488    switch (transform)
5489      {
5490       case WL_OUTPUT_TRANSFORM_NORMAL:
5491       default:
5492          *dx = sx, *dy = sy;
5493          break;
5494       case WL_OUTPUT_TRANSFORM_FLIPPED:
5495          *dx = width - sx, *dy = sy;
5496          break;
5497       case WL_OUTPUT_TRANSFORM_90:
5498          *dx = height - sy, *dy = sx;
5499          break;
5500       case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5501          *dx = height - sy, *dy = width - sx;
5502          break;
5503       case WL_OUTPUT_TRANSFORM_180:
5504          *dx = width - sx, *dy = height - sy;
5505          break;
5506       case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5507          *dx = sx, *dy = height - sy;
5508          break;
5509       case WL_OUTPUT_TRANSFORM_270:
5510          *dx = sy, *dy = width - sx;
5511          break;
5512       case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5513          *dx = sy, *dy = sx;
5514          break;
5515      }
5516
5517    *dx *= scale;
5518    *dy *= scale;
5519 }
5520
5521 static void
5522 _buffer_viewport_get(E_Comp_Wl_Buffer_Viewport *vp, int bw, int bh, Eina_Rectangle *out)
5523 {
5524    int x1, y1, x2, y2;
5525    int tx1, ty1, tx2, ty2;
5526    int width_from_buffer, height_from_buffer;
5527
5528    switch (vp->buffer.transform)
5529      {
5530       case WL_OUTPUT_TRANSFORM_90:
5531       case WL_OUTPUT_TRANSFORM_270:
5532       case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5533       case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5534          width_from_buffer = bh / vp->buffer.scale;
5535          height_from_buffer = bw / vp->buffer.scale;
5536          break;
5537       default:
5538          width_from_buffer = bw / vp->buffer.scale;
5539          height_from_buffer = bh / vp->buffer.scale;
5540          break;
5541      }
5542
5543    if (vp->buffer.src_width == wl_fixed_from_int(-1))
5544      {
5545         x1 = 0.0;
5546         y1 = 0.0;
5547         x2 = width_from_buffer;
5548         y2 = height_from_buffer;
5549      }
5550    else
5551      {
5552         x1 = wl_fixed_to_int(vp->buffer.src_x);
5553         y1 = wl_fixed_to_int(vp->buffer.src_y);
5554         x2 = wl_fixed_to_int(vp->buffer.src_x + vp->buffer.src_width);
5555         y2 = wl_fixed_to_int(vp->buffer.src_y + vp->buffer.src_height);
5556      }
5557
5558    buffer_transform(width_from_buffer, height_from_buffer,
5559                     vp->buffer.transform, vp->buffer.scale, x1, y1, &tx1, &ty1);
5560    buffer_transform(width_from_buffer, height_from_buffer,
5561                     vp->buffer.transform, vp->buffer.scale, x2, y2, &tx2, &ty2);
5562
5563    out->x = (tx1 <= tx2) ? tx1 : tx2;
5564    out->y = (ty1 <= ty2) ? ty1 : ty2;
5565    out->w = (tx1 <= tx2) ? tx2 - tx1 : tx1 - tx2;
5566    out->h = (ty1 <= ty2) ? ty2 - ty1 : ty1 - ty2;
5567 }
5568
5569 /**
5570  * Convert given four coordinates to elements of rectangle
5571  * @in   p[4]        Coordinates to be converted
5572  * @out  rect        x, y, width, and height
5573  *       transform   Angle which represents TDM_TRANSFORM of rectangle
5574  * @return EINA_FALSE in following case, otherwise EINA_TRUE.
5575  *   1. The given coordinates are not represented by rectangle.
5576  *   2. All angles except for 0, 90, 180, 270.
5577  */
5578 static Eina_Bool
5579 _coords_to_rectangle_convert(Evas_Point p[4], Eina_Rectangle *rect, uint *rotation)
5580 {
5581    Eina_Bool ret = EINA_FALSE;
5582
5583    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))
5584      {
5585         /* 0 or 180 */
5586         if ((p[0].x < p[2].x) && (p[0].y < p[2].y))
5587           {
5588              if (rotation)
5589                *rotation = TDM_TRANSFORM_NORMAL;
5590
5591              if (rect)
5592                EINA_RECTANGLE_SET(rect, p[0].x, p[0].y, p[2].x - p[0].x, p[2].y - p[0].y);
5593
5594              ret = EINA_TRUE;
5595           }
5596         else if ((p[0].x > p[2].x) && (p[0].y > p[2].y))
5597           {
5598              if (rotation)
5599                *rotation = TDM_TRANSFORM_180;
5600
5601              if (rect)
5602                EINA_RECTANGLE_SET(rect, p[2].x, p[2].y, p[0].x - p[2].x, p[0].y - p[2].y);
5603
5604              ret = EINA_TRUE;
5605           }
5606      }
5607    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))
5608      {
5609         /* 90 or 270 */
5610         if ((p[0].x > p[2].x) && (p[0].y < p[2].y))
5611           {
5612              if (rotation)
5613                *rotation = TDM_TRANSFORM_90;
5614
5615              if (rect)
5616                EINA_RECTANGLE_SET(rect, p[2].x, p[0].y, p[0].x - p[2].x, p[2].y - p[0].y);
5617
5618              ret = EINA_TRUE;
5619           }
5620         else if ((p[0].x < p[2].x) && (p[0].y > p[2].y))
5621           {
5622              if (rotation)
5623                *rotation = TDM_TRANSFORM_270;
5624
5625              if (rect)
5626                EINA_RECTANGLE_SET(rect, p[0].x, p[2].y, p[2].x - p[0].x, p[0].y - p[2].y);
5627
5628              ret = EINA_TRUE;
5629           }
5630      }
5631
5632    return ret;
5633 }
5634
5635 static Eina_Bool
5636 _output_viewport_get_from_evas_map(E_Map *m, Eina_Rectangle *out, unsigned int *rotation)
5637 {
5638    Evas_Point p[4];
5639    int i;
5640
5641    for (i = 0; i < 4; i++)
5642      e_map_point_coord_get(m, i, &p[i].x, &p[i].y, NULL);
5643
5644    if (!_coords_to_rectangle_convert(p, out, rotation))
5645      {
5646         DBG("Cannot convert given coords to rectangle.\n"
5647             "p1(%d %d) p2(%d %d) p3(%d %d) p4(%d %d)",
5648             p[0].x, p[0].y, p[1].x, p[1].y,
5649             p[2].x, p[2].y, p[3].x, p[3].y);
5650         return EINA_FALSE;
5651      }
5652
5653    return EINA_TRUE;
5654 }
5655
5656 static unsigned int
5657 _transform_merge_with_rotation(enum wl_output_transform transform, unsigned int rotation)
5658 {
5659    int trans_rotation, flip;
5660
5661    trans_rotation = transform & 0x3;
5662    flip = transform & 0x4;
5663
5664    return (flip + (trans_rotation + rotation) % 4);
5665 }
5666
5667 static void
5668 _e_comp_wl_surface_output_viewport_get(E_Client *ec, Eina_Rectangle *out)
5669 {
5670    if (!out)
5671      return;
5672
5673    if (!ec->comp_data)
5674      return;
5675
5676    if (e_comp_wl_subsurface_check(ec))
5677      e_comp_wl_subsurface_global_coord_get(ec, &out->x, &out->y);
5678    else
5679      {
5680         out->x = ec->x;
5681         out->y = ec->y;
5682      }
5683
5684    out->w = ec->comp_data->width_from_viewport;
5685    out->w = (out->w + 1) & ~1;
5686    out->h = ec->comp_data->height_from_viewport;
5687
5688    e_comp_object_frame_xy_unadjust(ec->frame, out->x, out->y, &out->x, &out->y);
5689    e_comp_object_frame_wh_unadjust(ec->frame, out->w, out->h, &out->w, &out->h);
5690 }
5691
5692 EINTERN Eina_Bool
5693 e_comp_wl_surface_viewport_get(E_Client *ec, Eina_Rectangle *buffer_viewport, Eina_Rectangle *output_viewport, unsigned int *transform)
5694 {
5695    E_Comp_Wl_Buffer *buffer;
5696    E_Comp_Wl_Buffer_Viewport *vp;
5697    E_Map *m;
5698    enum wl_output_transform buffer_transform;
5699    unsigned int rotation;
5700    Eina_Bool res = EINA_FALSE;
5701
5702    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5703
5704    if (e_object_is_del(E_OBJECT(ec)))
5705      return EINA_FALSE;
5706
5707    vp = &ec->comp_data->scaler.buffer_viewport;
5708
5709    if (buffer_viewport)
5710      {
5711         buffer = ec->comp_data->buffer_ref.buffer;
5712         /* Getting a viewport of buffer needs geometry of buffer. */
5713         if (!buffer)
5714           return EINA_FALSE;
5715
5716         _buffer_viewport_get(vp, buffer->w, buffer->h, buffer_viewport);
5717      }
5718
5719    if ((output_viewport) || (transform))
5720      {
5721         m = e_client_map_get(ec);
5722         if (m)
5723           res = _output_viewport_get_from_evas_map(m, output_viewport, &rotation);
5724         else
5725           _e_comp_wl_surface_output_viewport_get(ec, output_viewport);
5726         e_map_free(m);
5727
5728         if (transform)
5729           {
5730              buffer_transform = e_comp_wl_output_buffer_transform_get(ec);
5731              /* NOTE Merge transform value from evas_map with E_Comp_Wl_Buffer_Viewport's one.
5732               * Since buffer.transform isn't applied using evas_map,
5733               * it has to be taken into account here to apply buffer.transform
5734               * and rotation of e_client_transform together. */
5735              if (res)
5736                *transform = _transform_merge_with_rotation(buffer_transform, rotation);
5737              else
5738                *transform = (unsigned int)buffer_transform;
5739           }
5740      }
5741
5742    return EINA_TRUE;
5743 }
5744
5745 EINTERN Eina_Bool
5746 e_comp_wl_surface_role_set(E_Client *ec, const char *role_name, struct wl_resource *error_resource, uint32_t error_code)
5747 {
5748    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, EINA_FALSE);
5749    EINA_SAFETY_ON_NULL_RETURN_VAL(role_name, EINA_FALSE);
5750
5751    /* Must be called with valid comp_data */
5752    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, EINA_FALSE);
5753
5754    if ((ec->comp_data->role_name == NULL) ||
5755        (ec->comp_data->role_name == role_name) ||
5756        (strcmp(ec->comp_data->role_name, role_name) == 0))
5757      {
5758         ec->comp_data->role_name = role_name;
5759         return EINA_TRUE;
5760      }
5761
5762    if (error_resource)
5763      {
5764         wl_resource_post_error(error_resource, error_code,
5765                                "Cannot assign role %s to wl_surface@%d,"
5766                                " already has role %s\n",
5767                                role_name,
5768                                ec->comp_data->wl_surface ?
5769                                wl_resource_get_id(ec->comp_data->wl_surface) : -1,
5770                                ec->comp_data->role_name);
5771      }
5772
5773    return EINA_FALSE;
5774 }
5775
5776 EINTERN const char *
5777 e_comp_wl_surface_role_get(E_Client *ec)
5778 {
5779    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
5780    return ec->comp_data ? ec->comp_data->role_name : NULL;
5781 }
5782
5783 EINTERN struct wl_resource *
5784 e_comp_wl_client_surface_get(E_Client *ec)
5785 {
5786    struct wl_resource *surface = NULL;
5787    EINA_SAFETY_ON_NULL_RETURN_VAL(ec, NULL);
5788    EINA_SAFETY_ON_NULL_RETURN_VAL(ec->comp_data, NULL);
5789
5790    g_mutex_lock(&ec->comp_data->surface_mutex);
5791    surface = ec->comp_data->surface;
5792    g_mutex_unlock(&ec->comp_data->surface_mutex);
5793
5794    return surface;
5795 }
5796
5797 EINTERN void e_comp_wl_client_surface_set(E_Client *ec, struct wl_resource *surface)
5798 {
5799    EINA_SAFETY_ON_NULL_RETURN(ec);
5800    EINA_SAFETY_ON_NULL_RETURN(ec->comp_data);
5801
5802    g_mutex_lock(&ec->comp_data->surface_mutex);
5803    ec->comp_data->surface = surface;
5804    g_mutex_unlock(&ec->comp_data->surface_mutex);
5805 }
5806
5807 EINTERN E_Client *
5808 e_comp_wl_util_client_from_surface_resource(struct wl_resource *surface_resource)
5809 {
5810    return e_compositor_util_client_from_surface_resource(surface_resource);
5811 }
5812
5813 EINTERN void
5814 e_comp_wl_client_surface_pending_buffer_set(E_Client *ec, E_Comp_Wl_Buffer *buffer, int32_t sx, int32_t sy)
5815 {
5816    if (!ec->comp_data->mapped)
5817      {
5818         if (ec->comp_data->shell.surface &&
5819             !ec->internal && !e_comp_wl_subsurface_check(ec) && !ec->remote_surface.provider)
5820           {
5821              ELOGF("COMP", "Current unmapped. ATTACH buffer:%p", ec, buffer);
5822           }
5823      }
5824
5825    if (!buffer)
5826      {
5827         if (ec->comp_data->mapped)
5828           {
5829              /* will be unmapped. so run capture */
5830              e_comp_wl_remote_surface_image_save(ec);
5831           }
5832      }
5833
5834    _e_comp_wl_surface_state_buffer_set(&ec->comp_data->pending, buffer);
5835
5836    ec->comp_data->pending.sx = sx;
5837    ec->comp_data->pending.sy = sy;
5838    ec->comp_data->pending.new_attach = EINA_TRUE;
5839
5840    e_client_fps_update(ec);
5841 }
5842
5843 EINTERN void
5844 e_comp_wl_client_surface_pending_opaque_region_set(E_Client *ec, Eina_Tiler *region)
5845 {
5846    if (ec->comp_data->pending.opaque)
5847      eina_tiler_clear(ec->comp_data->pending.opaque);
5848
5849    if (region)
5850      {
5851         eina_tiler_union(ec->comp_data->pending.opaque, region);
5852
5853         if (!eina_tiler_empty(ec->comp_data->pending.opaque))
5854           {
5855              if (ec->argb)
5856                {
5857                   ec->argb = EINA_FALSE;
5858                   ELOGF("COMP", "Set argb:%d", ec, ec->argb);
5859                   EC_CHANGED(ec);
5860                   _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_ALPHA_CHANGE, ec);
5861                   e_comp_object_alpha_set(ec->frame, EINA_FALSE);
5862                }
5863           }
5864      }
5865    else
5866      {
5867         if (!ec->argb)
5868           {
5869              ec->argb = EINA_TRUE;
5870              ELOGF("COMP", "Set argb:%d", ec, ec->argb);
5871              EC_CHANGED(ec);
5872              _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_ALPHA_CHANGE, ec);
5873              e_comp_object_alpha_set(ec->frame, EINA_TRUE);
5874           }
5875      }
5876 }
5877
5878 EINTERN void
5879 e_comp_wl_client_surface_pending_input_region_set(E_Client *ec, Eina_Tiler *region)
5880 {
5881    if (ec->comp_data->pending.input)
5882      eina_tiler_clear(ec->comp_data->pending.input);
5883
5884    if (region)
5885      {
5886         if (eina_tiler_empty(region))
5887           {
5888              ELOGF("COMP", "         |unset input rect", NULL);
5889              e_comp_object_input_objs_del(ec->frame);
5890              e_comp_object_input_area_set(ec->frame, -1, -1, 1, 1);
5891           }
5892         else
5893           eina_tiler_union(ec->comp_data->pending.input, region);
5894      }
5895    else
5896      {
5897         eina_tiler_rect_add(ec->comp_data->pending.input,
5898                             &(Eina_Rectangle){0, 0, ec->client.w, ec->client.h});
5899      }
5900 }
5901
5902 EINTERN void
5903 e_comp_wl_client_surface_pending_frame_callback_add(E_Client *ec, struct wl_resource *callback_resource)
5904 {
5905    ec->comp_data->pending.frames = eina_list_prepend(ec->comp_data->pending.frames, callback_resource);
5906 }
5907
5908 EINTERN void
5909 e_comp_wl_client_surface_frame_callback_remove(E_Client *ec, struct wl_resource *callback_resource)
5910 {
5911    E_Comp_Wl_Subsurf_Data *sdata;
5912
5913    if (!ec->comp_data)
5914      return;
5915
5916    if (ec->comp_data->frames)
5917      {
5918         ec->comp_data->frames =
5919            eina_list_remove(ec->comp_data->frames, callback_resource);
5920      }
5921
5922    if (ec->comp_data->pending.frames)
5923      {
5924         ec->comp_data->pending.frames =
5925            eina_list_remove(ec->comp_data->pending.frames, callback_resource);
5926      }
5927
5928    sdata = ec->comp_data->sub.data;
5929    if ((sdata) && (sdata->cached.frames))
5930      {
5931         sdata->cached.frames =
5932            eina_list_remove(sdata->cached.frames, callback_resource);
5933      }
5934 }
5935
5936 EINTERN void
5937 e_comp_wl_client_surface_pending_commit(E_Client *ec)
5938 {
5939    if (e_object_is_del(E_OBJECT(ec))) return;
5940
5941    if (e_comp_wl_remote_surface_commit(ec)) return;
5942
5943    e_comp_wl_surface_commit(ec);
5944
5945    _e_comp_wl_hook_call(E_COMP_WL_HOOK_CLIENT_SURFACE_COMMIT, ec);
5946 }
5947
5948 static void
5949 _e_comp_wl_client_subsurface_set(E_Client *ec, E_Comp_Wl_Subsurf_Data *sub)
5950 {
5951    e_comp_wl_client_surface_set(ec, ec->comp_data->wl_surface);
5952    ec->comp_data->sub.data = sub;
5953
5954    /* set subsurface client properties */
5955    ec->borderless = EINA_TRUE;
5956    ec->argb = EINA_TRUE;
5957    ELOGF("SUBSURFACE", "Set argb:%d", ec, ec->argb);
5958    ec->lock_border = EINA_TRUE;
5959    ec->lock_focus_in = ec->lock_focus_out = EINA_TRUE;
5960    ec->netwm.state.skip_taskbar = EINA_TRUE;
5961    ec->netwm.state.skip_pager = EINA_TRUE;
5962    ec->border_size = 0;
5963    ec->lock_user_location = 0;
5964    ec->lock_client_location = 0;
5965    ec->lock_user_size = 0;
5966    ec->lock_client_size = 0;
5967    ec->lock_client_stacking = 0;
5968    ec->lock_user_maximize = 0;
5969    ec->lock_client_maximize = 0;
5970    ec->changes.need_maximize = 0;
5971    ec->maximized = E_MAXIMIZE_NONE;
5972    EC_CHANGED(ec);
5973
5974    ec->new_client = ec->netwm.ping = EINA_TRUE;
5975    e_comp->new_clients++;
5976    e_client_unignore(ec);
5977
5978    /* Delete 'below_obj' if it was created before 'E_Client' becomes subsurface.
5979     * It's not for subsurface. */
5980    E_FREE_FUNC(ec->comp_data->sub.below_obj, evas_object_del);
5981 }
5982
5983 static void
5984 _e_comp_wl_client_subsurface_parent_set(E_Client *ec, E_Client *parent)
5985 {
5986    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
5987    E_Layer layer;
5988
5989    ELOGF("COMP", "         |subsurface_parent:%p", ec, parent);
5990
5991    sub->parent = parent;
5992
5993    if (parent->frame)
5994      {
5995         layer = e_client_layer_get(parent);
5996         if (layer > E_LAYER_BOTTOM)
5997           e_client_layer_set(ec, layer);
5998      }
5999
6000    if (parent->comp_data)
6001      {
6002         /* append this client to the parents subsurface list */
6003         parent->comp_data->sub.list_pending =
6004            eina_list_append(parent->comp_data->sub.list_pending, ec);
6005         parent->comp_data->sub.list_changed = EINA_TRUE;
6006      }
6007 }
6008
6009 EINTERN void
6010 e_comp_wl_client_subsurface_parent_unset(E_Client *ec)
6011 {
6012    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
6013    E_Client *parent = sub->parent;
6014
6015    parent->comp_data->sub.list =
6016       eina_list_remove(parent->comp_data->sub.list, ec);
6017    parent->comp_data->sub.list_pending =
6018       eina_list_remove(parent->comp_data->sub.list_pending, ec);
6019    parent->comp_data->sub.below_list =
6020       eina_list_remove(parent->comp_data->sub.below_list, ec);
6021    parent->comp_data->sub.below_list_pending =
6022       eina_list_remove(parent->comp_data->sub.below_list_pending, ec);
6023
6024    sub->parent = NULL;
6025 }
6026
6027 EINTERN void
6028 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)
6029 {
6030    sub->resource = subsurface_resource;
6031    sub->cached_buffer_ref.buffer = NULL;
6032    sub->synchronized = EINA_TRUE;
6033    sub->parent = parent;
6034    sub->remote_surface.offscreen_parent = offscreen_parent;
6035    if (offscreen_parent)
6036      ELOGF("SUBSURFACE", "         |offscreen_parent:%p", ec, offscreen_parent);
6037
6038    _e_comp_wl_surface_state_init(&sub->cached, ec->w, ec->h);
6039
6040    _e_comp_wl_client_subsurface_set(ec, sub);
6041    _e_comp_wl_client_subsurface_parent_set(ec, parent);
6042
6043    e_comp_wl_hook_call(E_COMP_WL_HOOK_SUBSURFACE_CREATE, ec);
6044 }
6045
6046 EINTERN void
6047 e_comp_wl_client_subsurface_finish(E_Client *ec)
6048 {
6049    E_Comp_Wl_Subsurf_Data *sub = ec->comp_data->sub.data;
6050
6051    if (sub->parent)
6052      e_comp_wl_client_subsurface_parent_unset(ec);
6053
6054    e_comp_wl_surface_state_finish(&sub->cached);
6055    e_comp_wl_buffer_reference(&sub->cached_buffer_ref, NULL);
6056
6057    ec->comp_data->sub.data = NULL;
6058 }
6059
6060 EINTERN void
6061 e_comp_wl_client_evas_init(E_Client *ec)
6062 {
6063    EINA_SAFETY_ON_NULL_RETURN(ec);
6064
6065    _e_comp_wl_client_evas_init(ec);
6066 }
6067
6068 EINTERN void
6069 e_comp_wl_client_evas_deinit(E_Client *ec)
6070 {
6071    EINA_SAFETY_ON_NULL_RETURN(ec);
6072
6073    _e_comp_wl_client_evas_deinit(ec);
6074 }
6075
6076 EINTERN void
6077 e_comp_wl_focus_check(void)
6078 {
6079    _e_comp_wl_focus_check();
6080 }
6081
6082 EINTERN Eina_Bool
6083 e_comp_wl_client_subsurface_cyclic_reference_check(E_Client *ec, E_Client *parent)
6084 {
6085    while (parent)
6086      {
6087         if (ec == parent)
6088           return EINA_TRUE;
6089
6090         if ((parent->comp_data) && (parent->comp_data->sub.data))
6091           parent = parent->comp_data->sub.data->parent;
6092         else
6093           break;
6094      }
6095
6096    return EINA_FALSE;
6097 }