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