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