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