bbef393bdf92b146b5ddc1370d8e421264ca3ee5
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr_wl.c
1 #include "e_devicemgr_private.h"
2
3 #ifdef HAVE_CYNARA
4 #define E_DEVMGR_CYNARA_ERROR_CHECK_GOTO(func_name, ret, label) \
5   do \
6     { \
7        if (EINA_UNLIKELY(CYNARA_API_SUCCESS != ret)) \
8           { \
9              _e_devicemgr_util_cynara_log(func_name, ret); \
10              goto label; \
11           } \
12     } \
13   while (0)
14
15 static void
16 _e_devicemgr_util_cynara_log(const char *func_name, int err)
17 {
18 #define CYNARA_BUFSIZE 128
19    char buf[CYNARA_BUFSIZE] = "\0";
20    int ret;
21
22    ret = cynara_strerror(err, buf, CYNARA_BUFSIZE);
23    if (ret != CYNARA_API_SUCCESS)
24      {
25         DMDBG("Failed to cynara_strerror: %d (error log about %s: %d)\n", ret, func_name, err);
26         return;
27      }
28    DMDBG("%s is failed: %s\n", func_name, buf);
29 }
30
31 static Eina_Bool
32 _e_devicemgr_util_do_privilege_check(struct wl_client *client, int socket_fd, const char *rule)
33 {
34    int ret, pid;
35    char *clientSmack=NULL, *uid=NULL, *client_session=NULL;
36    Eina_Bool res = EINA_FALSE;
37
38    /* If initialization of cynara has been failed, let's not to do further permission checks. */
39    if (e_devicemgr->wl_data->p_cynara == NULL && e_devicemgr->wl_data->cynara_initialized) return EINA_TRUE;
40    if (socket_fd < 0) return EINA_FALSE;
41
42    ret = cynara_creds_socket_get_client(socket_fd, CLIENT_METHOD_SMACK, &clientSmack);
43    E_DEVMGR_CYNARA_ERROR_CHECK_GOTO("cynara_creds_socket_get_client", ret, finish);
44
45    ret = cynara_creds_socket_get_user(socket_fd, USER_METHOD_UID, &uid);
46    E_DEVMGR_CYNARA_ERROR_CHECK_GOTO("cynara_creds_socket_get_user", ret, finish);
47
48    ret = cynara_creds_socket_get_pid(socket_fd, &pid);
49    E_DEVMGR_CYNARA_ERROR_CHECK_GOTO("cynara_creds_socket_get_pid", ret, finish);
50
51    client_session = cynara_session_from_pid(pid);
52
53    ret = cynara_check(e_devicemgr->wl_data->p_cynara, clientSmack, client_session, uid, rule);
54
55    if (CYNARA_API_ACCESS_ALLOWED == ret)
56         res = EINA_TRUE;
57
58 finish:
59    E_FREE(client_session);
60    E_FREE(clientSmack);
61    E_FREE(uid);
62
63    return res;
64 }
65 #endif
66
67 static void
68 _e_devicemgr_wl_device_cb_axes_select(struct wl_client *client, struct wl_resource *resource, struct wl_array *axes)
69 {
70    return;
71 }
72
73 static void
74 _e_devicemgr_wl_device_cb_release(struct wl_client *client, struct wl_resource *resource)
75 {
76    wl_resource_destroy(resource);
77 }
78
79 static const struct tizen_input_device_interface _e_devicemgr_wl_device_interface =
80 {
81    _e_devicemgr_wl_device_cb_axes_select,
82    _e_devicemgr_wl_device_cb_release,
83 };
84
85 static void
86 _e_devicemgr_wl_device_cb_unbind(struct wl_resource *resource)
87 {
88    E_Devicemgr_Input_Device *dev;
89    E_Devicemgr_Input_Device_User_Data *device_user_data;
90    E_Devicemgr_Input_Device_Mgr_Data *mgr_data;
91
92    DMDBG("Unbind tizen_input_device: %u (client: %p)", wl_resource_get_id(resource), wl_resource_get_client(resource));
93    if (!(device_user_data = wl_resource_get_user_data(resource))) return;
94
95    mgr_data = device_user_data->mgr_data;
96    if (mgr_data)
97      {
98         mgr_data->user_data_list = eina_list_remove(mgr_data->user_data_list, device_user_data);
99      }
100
101    dev = device_user_data->dev;
102
103    device_user_data->dev = NULL;
104    device_user_data->dev_mgr_res = NULL;
105    device_user_data->seat_res = NULL;
106    device_user_data->dev_res = NULL;
107    device_user_data->mgr_data = NULL;
108    E_FREE(device_user_data);
109    wl_resource_set_user_data(resource, NULL);
110
111    if (!dev) return;
112    dev->resources = eina_list_remove(dev->resources, resource);
113 }
114
115 void
116 e_devicemgr_wl_device_update(E_Devicemgr_Input_Device *dev)
117 {
118    struct wl_array axes;
119    Eina_List *l;
120    struct wl_resource *res;
121
122    wl_array_init(&axes);
123
124    EINA_LIST_FOREACH(dev->resources, l, res)
125      {
126         tizen_input_device_send_device_info(res, dev->name, dev->clas, dev->subclas, &axes);
127      }
128 }
129
130 void
131 e_devicemgr_wl_device_add(E_Devicemgr_Input_Device *dev)
132 {
133    struct wl_resource *res, *seat_res, *dev_mgr_res;
134    Eina_List *l, *ll;
135    uint32_t serial;
136    struct wl_client *wc;
137    E_Devicemgr_Input_Device_User_Data *device_user_data;
138    struct wl_array axes;
139    E_Devicemgr_Input_Device_Mgr_Data *mgr_data;
140    E_Comp_Wl_Seat *seat_data;
141
142    /* TODO: find the seat corresponding to event */
143    serial = wl_display_next_serial(e_comp_wl->wl.disp);
144    wl_array_init(&axes);
145
146    EINA_LIST_FOREACH(e_comp_wl->seat.resources, l, seat_res)
147      {
148         seat_data = wl_resource_get_user_data(seat_res);
149         if (!seat_data) continue;
150         if (!seat_data->is_first_resource)
151           {
152              DMDBG("The seat (res:%d) is not the first resource bound to client (%p)",
153                    wl_resource_get_id(seat_res), wl_resource_get_client(seat_res));
154              continue;
155           }
156
157         wc = wl_resource_get_client(seat_res);
158
159         EINA_LIST_FOREACH(e_devicemgr->wl_data->mgr_data_list, ll, mgr_data)
160           {
161              dev_mgr_res = mgr_data->resource;
162              if (wl_resource_get_client(dev_mgr_res) != wc) continue;
163              if (!mgr_data->is_first_resource)
164                {
165                   DMDBG("This device_manager (res:%d) not the first resource bound to client (%p)",
166                         wl_resource_get_id(dev_mgr_res), wc);
167                   continue;
168                }
169              res = wl_resource_create(wc, &tizen_input_device_interface, 1, 0);
170              if (!res)
171                {
172                   DMERR("Could not create tizen_input_device resource");
173                   wl_client_post_no_memory(wc);
174                   break;
175                }
176
177              device_user_data = E_NEW(E_Devicemgr_Input_Device_User_Data, 1);
178              if (!device_user_data)
179                {
180                   DMERR("Failed to allocate memory for input device user data\n");
181                   wl_client_post_no_memory(wc);
182                   wl_resource_destroy(res);
183                   break;
184                }
185              device_user_data->dev = dev;
186              device_user_data->dev_mgr_res = dev_mgr_res;
187              device_user_data->seat_res = seat_res;
188              device_user_data->dev_res = res;
189              device_user_data->mgr_data = mgr_data;
190
191              dev->resources = eina_list_append(dev->resources, res);
192              mgr_data->user_data_list = eina_list_append(mgr_data->user_data_list, device_user_data);
193
194              wl_resource_set_implementation(res, &_e_devicemgr_wl_device_interface, device_user_data,
195                                             _e_devicemgr_wl_device_cb_unbind);
196              tizen_input_device_manager_send_device_add(dev_mgr_res, serial, dev->identifier, res, seat_res);
197              tizen_input_device_send_device_info(res, dev->name, dev->clas, dev->subclas, &axes);
198           }
199      }
200 }
201
202 void
203 e_devicemgr_wl_device_del(E_Devicemgr_Input_Device *dev)
204 {
205    struct wl_client *wc;
206    Eina_List *l, *ll, *lll;
207    struct wl_resource *res, *seat_res, *dev_mgr_res;
208    uint32_t serial;
209    E_Devicemgr_Input_Device_User_Data *device_user_data;
210    E_Devicemgr_Input_Device_Mgr_Data *mgr_data;
211
212    serial = wl_display_next_serial(e_comp_wl->wl.disp);
213
214    /* TODO: find the seat corresponding to event */
215    EINA_LIST_FOREACH(e_comp_wl->seat.resources, l, seat_res)
216      {
217         wc = wl_resource_get_client(seat_res);
218         EINA_LIST_FOREACH(e_devicemgr->wl_data->mgr_data_list, ll, mgr_data)
219           {
220              dev_mgr_res = mgr_data->resource;
221              if (wl_resource_get_client(dev_mgr_res) != wc) continue;
222              EINA_LIST_FOREACH(dev->resources, lll, res)
223                {
224                   if (wl_resource_get_client(res) != wc) continue;
225                   device_user_data = wl_resource_get_user_data(res);
226                   if (!device_user_data) continue;
227                   if (device_user_data->dev_mgr_res != dev_mgr_res)
228                     continue;
229                   if (device_user_data->seat_res != seat_res)
230                     continue;
231
232                   device_user_data->dev = NULL;
233                   tizen_input_device_manager_send_device_remove(dev_mgr_res, serial, dev->identifier, res, seat_res);
234                }
235           }
236      }
237
238    dev->resources = eina_list_free(dev->resources);
239 }
240
241 void
242 e_devicemgr_wl_detent_send_event(int detent)
243 {
244    E_Devicemgr_Input_Device *input_dev;
245    struct wl_resource *dev_res;
246    struct wl_client *wc;
247    Eina_List *l, *ll;
248    wl_fixed_t f_value;
249    E_Client *ec;
250
251    ec = e_client_focused_get();
252
253    if (!ec) return;
254    if (e_object_is_del(E_OBJECT(ec))) return;
255    if (ec->ignored) return;
256    if (!ec->comp_data || !ec->comp_data->surface) return;
257
258    f_value = wl_fixed_from_double(detent * 1.0);
259    wc = wl_resource_get_client(ec->comp_data->surface);
260
261    EINA_LIST_FOREACH(e_devicemgr->device_list, l, input_dev)
262      {
263         if (!strncmp(input_dev->name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
264           {
265              EINA_LIST_FOREACH(input_dev->resources, ll, dev_res)
266                {
267                   if (wl_resource_get_client(dev_res) != wc) continue;
268                   tizen_input_device_send_axis(dev_res, TIZEN_INPUT_DEVICE_AXIS_TYPE_DETENT, f_value);
269
270                   DMINF("DETENT : %d => E_Client: %p (pid: %d) (pname: %s)",
271                          detent, ec, ec->netwm.pid, e_client_util_name_get(ec));
272                }
273           }
274      }
275 }
276
277 void
278 e_devicemgr_wl_block_send_expired(struct wl_resource *resource)
279 {
280    if (!resource) return;
281    tizen_input_device_manager_send_block_expired(resource);
282 }
283
284 void
285 e_devicemgr_wl_touch_max_count_send(int slot, struct wl_resource *res, struct wl_resource *seat_res)
286 {
287    struct wl_resource *seat_resource, *dev_mgr_resource;
288    Eina_List *l, *ll;
289    uint32_t serial;
290    struct wl_client *wc;
291    E_Devicemgr_Input_Device_Mgr_Data *mgr_data;
292
293    serial = wl_display_next_serial(e_comp_wl->wl.disp);
294
295    if (res && seat_res)
296      {
297         if (wl_resource_get_version(res) < 4) return;
298         tizen_input_device_manager_send_max_touch_count(res, serial, slot, seat_res);
299      }
300    else
301      {
302         EINA_LIST_FOREACH(e_comp_wl->seat.resources, l, seat_resource)
303           {
304              wc = wl_resource_get_client(seat_resource);
305
306              EINA_LIST_FOREACH(e_devicemgr->wl_data->mgr_data_list, ll, mgr_data)
307                {
308                   dev_mgr_resource = mgr_data->resource;
309                   if (wl_resource_get_client(dev_mgr_resource) != wc) continue;
310                   if (wl_resource_get_version(dev_mgr_resource) < 4) continue;
311                   tizen_input_device_manager_send_max_touch_count(dev_mgr_resource, serial, slot, seat_resource);
312                }
313           }
314      }
315 }
316
317
318 static void
319 _e_devicemgr_wl_cb_block_events(struct wl_client *client, struct wl_resource *resource, uint32_t serial, uint32_t clas, uint32_t duration)
320 {
321    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
322
323 #ifdef HAVE_CYNARA
324    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
325                                                           "http://tizen.org/privilege/internal/inputdevice.block"))
326      {
327         DMERR("block_events request:priv check failed");
328         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
329         return;
330      }
331 #endif
332
333    ret = e_devicemgr_block_add(client, resource, clas, duration);
334    tizen_input_device_manager_send_error(resource, ret);
335 }
336
337 static void
338 _e_devicemgr_wl_cb_unblock_events(struct wl_client *client, struct wl_resource *resource, uint32_t serial)
339 {
340    int ret;
341
342 #ifdef HAVE_CYNARA
343    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
344                                                           "http://tizen.org/privilege/internal/inputdevice.block"))
345      {
346         DMERR("unblock_events request:priv check failed");
347         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
348         return;
349      }
350 #endif
351
352    ret = e_devicemgr_block_remove(client);
353    tizen_input_device_manager_send_error(resource, ret);
354 }
355
356 static void
357 _e_devicemgr_wl_cb_init_generator(struct wl_client *client, struct wl_resource *resource, uint32_t clas)
358 {
359    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
360
361 #ifdef HAVE_CYNARA
362    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
363                                                           "http://tizen.org/privilege/inputgenerator"))
364      {
365         DMERR("init_generator request:priv check failed");
366         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
367         return;
368      }
369 #endif
370
371    ret = e_devicemgr_inputgen_add(client, resource, clas, INPUT_GENERATOR_DEVICE);
372    tizen_input_device_manager_send_error(resource, ret);
373 }
374
375 static void
376 _e_devicemgr_wl_cb_init_generator_with_name(struct wl_client *client, struct wl_resource *resource, uint32_t clas, const char *name)
377 {
378    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
379
380 #ifdef HAVE_CYNARA
381    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
382                                                           "http://tizen.org/privilege/inputgenerator"))
383      {
384         DMERR("init_generator_with_name request:priv check failed");
385         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
386         return;
387      }
388 #endif
389
390    ret = e_devicemgr_inputgen_add(client, resource, clas, name);
391    tizen_input_device_manager_send_error(resource, ret);
392 }
393
394 static void
395 _e_devicemgr_wl_cb_deinit_generator(struct wl_client *client, struct wl_resource *resource, uint32_t clas)
396 {
397    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
398
399 #ifdef HAVE_CYNARA
400    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
401                                                           "http://tizen.org/privilege/inputgenerator"))
402      {
403         DMERR("deinit_generator request:priv check failed");
404         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
405         return;
406      }
407 #endif
408
409    e_devicemgr_inputgen_remove(client, resource, clas);
410    tizen_input_device_manager_send_error(resource, ret);
411 }
412
413 static void
414 _e_devicemgr_wl_cb_generate_key(struct wl_client *client, struct wl_resource *resource, const char *keyname, uint32_t pressed)
415 {
416    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
417
418 #ifdef HAVE_CYNARA
419    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
420                                                           "http://tizen.org/privilege/inputgenerator"))
421      {
422         DMERR("generate_key request:priv check failed");
423         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
424         return;
425      }
426 #endif
427
428    ret = e_devicemgr_inputgen_generate_key(client, resource, keyname, (Eina_Bool)!!pressed);
429    tizen_input_device_manager_send_error(resource, ret);
430 }
431
432 static void
433 _e_devicemgr_wl_cb_generate_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y, uint32_t button)
434 {
435    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
436
437 #ifdef HAVE_CYNARA
438    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
439                                                           "http://tizen.org/privilege/inputgenerator"))
440      {
441         DMERR("_generate_pointer request:priv check failed");
442         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
443         return;
444      }
445 #endif
446
447    ret = e_devicemgr_inputgen_generate_pointer(client, resource, type, x, y, button);
448    tizen_input_device_manager_send_error(resource, ret);
449 }
450
451 static void
452 _e_devicemgr_wl_cb_generate_touch(struct wl_client *client, struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y, uint32_t finger)
453 {
454    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
455
456 #ifdef HAVE_CYNARA
457    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client), "http://tizen.org/privilege/inputgenerator"))
458      {
459         DMERR("_e_input_devmgr_cb_generate_touch:priv check failed");
460         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
461         return;
462      }
463 #endif
464
465    ret = e_devicemgr_inputgen_generate_touch(client, resource, type, x, y, finger);
466    tizen_input_device_manager_send_error(resource, ret);
467 }
468
469 static void
470 _e_devicemgr_wl_cb_pointer_warp(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, wl_fixed_t x, wl_fixed_t y)
471 {
472    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
473
474    ret = e_devicemgr_input_pointer_warp(client, resource, surface, x, y);
475
476    tizen_input_device_manager_send_error(resource, ret);
477 }
478
479 static void
480 _e_devicemgr_wl_cb_destroy(struct wl_client *client, struct wl_resource *resource)
481 {
482    wl_resource_destroy(resource);
483 }
484
485 static void
486 _e_devicemgr_wl_cb_generate_axis(struct wl_client *client, struct wl_resource *resource, uint32_t type, wl_fixed_t value)
487 {
488    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
489
490 #ifdef HAVE_CYNARA
491    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
492                                                           "http://tizen.org/privilege/inputgenerator"))
493      {
494         DMERR("_generate_pointer request:priv check failed");
495         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
496         return;
497      }
498 #endif
499
500    if (type == TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_WHEEL ||
501        type == TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_HWHEEL)
502      ret = e_devicemgr_inputgen_generate_wheel(client, resource, type, (int)wl_fixed_to_double(value));
503    else
504      ret = e_devicemgr_inputgen_touch_axis_store(client, resource, type, wl_fixed_to_double(value));
505    tizen_input_device_manager_send_error(resource, ret);
506 }
507
508 static void
509 _e_devicemgr_wl_cb_set_touch_count(struct wl_client *client, struct wl_resource *resource, int32_t max_count)
510 {
511    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
512
513 #ifdef HAVE_CYNARA
514    if (EINA_FALSE == _e_devicemgr_util_do_privilege_check(client, wl_client_get_fd(client),
515                                                           "http://tizen.org/privilege/inputgenerator"))
516      {
517         DMERR("_generate_pointer request:priv check failed");
518         tizen_input_device_manager_send_error(resource, TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION);
519         return;
520      }
521 #endif
522
523    if (e_config->configured_max_touch.use)
524      {
525         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
526      }
527    else
528      {
529         if (max_count > e_input_touch_max_count_get())
530           {
531              e_input_touch_max_count_set(max_count);
532           }
533      }
534
535    tizen_input_device_manager_send_error(resource, ret);
536 }
537
538
539 static const struct tizen_input_device_manager_interface _e_devicemgr_wl_implementation = {
540    _e_devicemgr_wl_cb_block_events,
541    _e_devicemgr_wl_cb_unblock_events,
542    _e_devicemgr_wl_cb_init_generator,
543    _e_devicemgr_wl_cb_deinit_generator,
544    _e_devicemgr_wl_cb_generate_key,
545    _e_devicemgr_wl_cb_generate_pointer,
546    _e_devicemgr_wl_cb_generate_touch,
547    _e_devicemgr_wl_cb_pointer_warp,
548    _e_devicemgr_wl_cb_init_generator_with_name,
549    _e_devicemgr_wl_cb_destroy,
550    _e_devicemgr_wl_cb_generate_axis,
551    _e_devicemgr_wl_cb_set_touch_count,
552 };
553
554 static void
555 _e_devicemgr_wl_cb_unbind(struct wl_resource *resource)
556 {
557    E_Devicemgr_Input_Device_Mgr_Data *mgr_data;
558    E_Devicemgr_Input_Device_User_Data *device_user_data;
559
560    if(!e_comp_wl) return;
561
562    mgr_data = wl_resource_get_user_data(resource);
563    if (!mgr_data) return;
564
565    DMDBG("Unbind tizen_input_device_manager: %u (client: %p)", wl_resource_get_id(resource), wl_resource_get_client(resource));
566
567    EINA_LIST_FREE(mgr_data->user_data_list, device_user_data)
568      {
569         DMDBG("Destroy dev_res:%u", wl_resource_get_id(device_user_data->dev_res));
570         device_user_data->mgr_data = NULL;
571         wl_resource_destroy(device_user_data->dev_res);
572      }
573    e_devicemgr->wl_data->mgr_data_list = eina_list_remove(e_devicemgr->wl_data->mgr_data_list, mgr_data);
574
575    E_FREE(mgr_data);
576 }
577
578 static void
579 _e_devicemgr_wl_cb_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
580 {
581    struct wl_resource *res, *seat_res, *device_res;
582    Eina_List *l, *ll;
583    uint32_t serial;
584    E_Devicemgr_Input_Device *dev;
585    struct wl_array axes;
586    E_Devicemgr_Input_Device_User_Data *device_user_data;
587    E_Devicemgr_Input_Device_Mgr_Data *mgr_data, *tmp_data;
588
589    mgr_data = E_NEW(E_Devicemgr_Input_Device_Mgr_Data, 1);
590    if (!mgr_data)
591      {
592         DMERR("Failed to allocate memory for input device mgr data\n");
593         wl_client_post_no_memory(client);
594         return;
595      }
596    mgr_data->is_first_resource = 1;
597
598    EINA_LIST_FOREACH(e_devicemgr->wl_data->mgr_data_list, l, tmp_data)
599      {
600         if (wl_resource_get_client(tmp_data->resource) != client) continue;
601         DMDBG("tizen_input_device_manager (res: %d) is already bound to client (%p)",
602               wl_resource_get_id(tmp_data->resource), client);
603         mgr_data->is_first_resource = 0;
604         break;
605      }
606
607    if (!(res = wl_resource_create(client, &tizen_input_device_manager_interface, version, id)))
608      {
609         DMERR("Could not create tizen_input_device_manager_interface resource: %m");
610         wl_client_post_no_memory(client);
611         E_FREE(mgr_data);
612         return;
613      }
614
615    DMDBG("Bind tizen_input_device_manager: %u (client: %p)", wl_resource_get_id(res), wl_resource_get_client(res));
616
617    mgr_data->resource = res;
618    mgr_data->user_data_list = NULL;
619    e_devicemgr->wl_data->mgr_data_list = eina_list_append(e_devicemgr->wl_data->mgr_data_list, mgr_data);
620
621    wl_resource_set_implementation(res, &_e_devicemgr_wl_implementation, mgr_data,
622                                   _e_devicemgr_wl_cb_unbind);
623
624    EINA_LIST_FOREACH(e_comp_wl->seat.resources, l, seat_res)
625      {
626         if (wl_resource_get_client(seat_res) != client) continue;
627
628         wl_array_init(&axes);
629         serial = wl_display_next_serial(e_comp_wl->wl.disp);
630
631         if (e_devicemgr->max_touch_count > 0)
632           {
633              e_devicemgr_wl_touch_max_count_send(e_devicemgr->max_touch_count, res, seat_res);
634           }
635
636         if (!mgr_data->is_first_resource)
637           {
638              DMDBG("This device_manager (res:%d) is not the first resource bound to client (%p)",
639                    wl_resource_get_id(res), client);
640              continue;
641           }
642
643         EINA_LIST_FOREACH(e_devicemgr->device_list, ll, dev)
644           {
645              device_res = wl_resource_create(client, &tizen_input_device_interface, 1, 0);
646              if (!device_res)
647                {
648                   DMERR("Could not create tizen_input_device resource: %m");
649                   wl_client_post_no_memory(client);
650                   return;
651                }
652              device_user_data = E_NEW(E_Devicemgr_Input_Device_User_Data, 1);
653              if (!device_user_data)
654                {
655                   DMERR("Failed to allocate memory for input device user data\n");
656                   wl_client_post_no_memory(client);
657                   wl_resource_destroy(device_res);
658                   return;
659                }
660              device_user_data->dev = dev;
661              device_user_data->dev_mgr_res = res;
662              device_user_data->seat_res = seat_res;
663              device_user_data->dev_res = device_res;
664              device_user_data->mgr_data = mgr_data;
665
666              dev->resources = eina_list_append(dev->resources, device_res);
667              mgr_data->user_data_list = eina_list_append(mgr_data->user_data_list, device_user_data);
668
669              wl_resource_set_implementation(device_res, &_e_devicemgr_wl_device_interface, device_user_data,
670                                             _e_devicemgr_wl_device_cb_unbind);
671
672              tizen_input_device_manager_send_device_add(res, serial, dev->identifier, device_res, seat_res);
673              tizen_input_device_send_device_info(device_res, dev->name, dev->clas, dev->subclas, &axes);
674           }
675      }
676 }
677
678 Eina_Bool
679 e_devicemgr_wl_init(void)
680 {
681    if (!e_comp_wl) return EINA_FALSE;
682    if (!e_comp_wl->wl.disp) return EINA_FALSE;
683
684    if (e_devicemgr->wl_data) return EINA_TRUE;
685
686    e_devicemgr->wl_data = E_NEW(E_Devicemgr_Wl_Data, 1);
687    EINA_SAFETY_ON_NULL_RETURN_VAL(e_devicemgr->wl_data, EINA_FALSE);
688
689    /* try to add tizen_input_device_manager to wayland globals */
690    e_devicemgr->wl_data->global = wl_global_create(e_comp_wl->wl.disp,
691                                                    &tizen_input_device_manager_interface, 4,
692                                                    NULL, _e_devicemgr_wl_cb_bind);
693    if (!e_devicemgr->wl_data->global)
694      {
695         DMERR("Could not add tizen_input_device_manager to wayland globals");
696         return EINA_FALSE;
697      }
698    e_devicemgr->wl_data->mgr_data_list = NULL;
699
700    /* initialization of cynara for checking privilege */
701 #ifdef HAVE_CYNARA
702    int ret;
703
704    ret = cynara_initialize(&e_devicemgr->wl_data->p_cynara, NULL);
705    if (EINA_UNLIKELY(CYNARA_API_SUCCESS != ret))
706      {
707         _e_devicemgr_util_cynara_log("cynara_initialize", ret);
708         e_devicemgr->wl_data->p_cynara = NULL;
709      }
710    e_devicemgr->wl_data->cynara_initialized = EINA_TRUE;
711 #endif
712
713    return EINA_TRUE;
714 }
715
716 void
717 e_devicemgr_wl_shutdown(void)
718 {
719    if (!e_devicemgr->wl_data) return;
720    /* destroy the global seat resource */
721    if (e_devicemgr->wl_data->global)
722      wl_global_destroy(e_devicemgr->wl_data->global);
723    e_devicemgr->wl_data->global = NULL;
724    eina_list_free(e_devicemgr->wl_data->mgr_data_list);
725
726    /* deinitialization of cynara if it has been initialized */
727 #ifdef HAVE_CYNARA
728    if (e_devicemgr->wl_data->p_cynara) cynara_finish(e_devicemgr->wl_data->p_cynara);
729    e_devicemgr->wl_data->cynara_initialized = EINA_FALSE;
730 #endif
731
732    E_FREE(e_devicemgr->wl_data);
733 }
734