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