devicemgr: implement pointer_warp
[platform/core/uifw/libds-tizen.git] / src / input_devicemgr / input_devicemgr.c
1 #include <stdlib.h>
2 #include <time.h> // gettimeofday()
3 #include <libds/backend.h>
4 #include <libds/log.h>
5 #include <libds-tizen/input_devicemgr.h>
6 #include <libds/interfaces/backend.h>
7 #include <libds/interfaces/keyboard.h>
8 #include <libds/interfaces/pointer.h>
9 #include <libds/interfaces/touch.h>
10
11 #include "util.h"
12 #include "security.h"
13 #include "input_devicemgr.h"
14
15 #define TIZEN_INPUT_DEVICEMGR_VERSION 4
16 #define TIZEN_PRIV_INPUT_GENERATOR "http://tizen.org/privilege/inputgenerator"
17 #define TIZEN_PRIV_INPUT_BLOCK "http://tizen.org/privilege/internal/inputdevice.block"
18 #define TIZEN_INPUT_DEVICEMGR_MAX_BTN 16
19
20 static const struct ds_keyboard_grab_interface devicemgr_keyboard_grab_iface;
21
22 //listeners
23 static void
24 backend_handle_destroy(struct wl_listener *listener, void *data);
25 static void
26 backend_handle_input_device_add(struct wl_listener *listener, void *data);
27 static void
28 seat_handle_destroy(struct wl_listener *listener, void *data);
29
30 //tizen_input_device_manager bind/unbind
31 static void
32 device_manager_handle_bind(struct wl_client *client, void *data,
33         uint32_t version, uint32_t id);
34 static void
35 device_manager_client_handle_resource_destroy(struct wl_resource *resource);
36
37 //tizen_input_device_manager's handlers for requests
38 static void
39 device_manager_handle_block_events(struct wl_client *client,
40         struct wl_resource *resource, uint32_t serial,
41         uint32_t clas, uint32_t duration);
42 static void
43 device_manager_handle_unblock_events(struct wl_client *client,
44         struct wl_resource *resource, uint32_t serial);
45 static void
46 device_manager_handle_init_generator(struct wl_client *client,
47         struct wl_resource *resource, uint32_t clas);
48 static void
49 device_manager_handle_init_generator_with_name(struct wl_client *client,
50         struct wl_resource *resource, uint32_t clas, const char *name);
51 static void
52 device_manager_handle_deinit_generator(struct wl_client *client,
53         struct wl_resource *resource, uint32_t clas);
54 static void
55 device_manager_handle_generate_key(struct wl_client *client,
56         struct wl_resource *resource,
57         const char *keyname, uint32_t pressed);
58 static void
59 device_manager_handle_generate_touch(struct wl_client *client,
60         struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y,
61         uint32_t finger);
62 static void
63 device_manager_handle_generate_pointer(struct wl_client *client,
64         struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y,
65         uint32_t button);
66 static void
67 device_manager_handle_pointer_warp(struct wl_client *client,
68         struct wl_resource *resource, struct wl_resource *surface,
69         wl_fixed_t x, wl_fixed_t y);
70 static void
71 device_manager_handle_destroy(struct wl_client *client,
72         struct wl_resource *resource);
73
74 //
75 static void tz_devicemgr_destroy(struct ds_tizen_input_devicemgr *tz_devicemgr);
76 static int
77 tz_devicemgr_init_generator(struct ds_tizen_input_devicemgr *tz_devicemgr,
78         struct wl_resource *resource, uint32_t clas, const char *name);
79 static int
80 tz_devicemgr_deinit_generator(struct ds_tizen_input_devicemgr *tz_devicemgr,
81         struct wl_resource *resource);
82 static bool
83 tz_devicemgr_generate_key(struct ds_input_device *device, int keycode,
84         int pressed);
85 static bool
86 tz_devicemgr_pressed_keys_update(struct ds_tizen_input_devicemgr *tz_devicemgr,
87         int keycode, bool pressed);
88 static void
89 tz_devicemgr_pressed_keys_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr);
90 static void
91 tz_devicemgr_keymap_list_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr);
92 static int
93 tz_devicemgr_keyname_to_keycode(struct wl_list *list, const char *name);
94
95 static bool
96 tz_devicemgr_generate_touch_move(struct ds_input_device *device, double x, double y,
97         uint32_t finger);
98 static bool
99 tz_devicemgr_generate_touch_down(struct ds_input_device *device, double x, double y,
100         uint32_t finger);
101 static bool
102 tz_devicemgr_generate_touch_up(struct ds_input_device *device, uint32_t finger);
103 static bool
104 tz_devicemgr_generate_mouse_move(struct ds_input_device *device, double x, double y);
105 static bool
106 tz_devicemgr_generate_mouse_button(struct ds_input_device *device, uint32_t button, bool state);
107
108 static bool
109 tz_devicemgr_check_privilege(struct ds_tizen_input_devicemgr *tz_devicemgr,
110         struct wl_client *client, const char *rule);
111
112 static void
113 tz_devicemgr_grab_keyboard(struct ds_tizen_input_devicemgr *tz_devicemgr);
114 static void
115 tz_devicemgr_ungrab_keyboard(struct ds_tizen_input_devicemgr *tz_devicemgr);
116 static void
117 tz_devicemgr_ungrab_keyboard_check(struct ds_tizen_input_devicemgr *tz_devicemgr);
118 static void
119 tz_devicemgr_blocked_keys_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr);
120
121 WL_EXPORT struct ds_tizen_input_devicemgr *
122 ds_tizen_input_devicemgr_create(struct ds_backend *backend,
123         struct ds_seat *seat)
124 {
125     struct ds_tizen_input_devicemgr *tz_devicemgr;
126
127     tz_devicemgr = calloc(1, sizeof *tz_devicemgr);
128     if (!tz_devicemgr) {
129         ds_err("Fail to allocate ds_tizen_input_devicemgr");
130         return NULL;
131     }
132
133     tz_devicemgr->backend = backend;
134     tz_devicemgr->backend_destroy.notify = backend_handle_destroy;
135     ds_backend_add_destroy_listener(backend, &tz_devicemgr->backend_destroy);
136
137     tz_devicemgr->seat = seat;
138     tz_devicemgr->seat_destroy.notify = seat_handle_destroy;
139     ds_seat_add_destroy_listener(seat, &tz_devicemgr->seat_destroy);
140
141     tz_devicemgr->new_input.notify = backend_handle_input_device_add;
142     ds_backend_add_new_input_listener(backend, &tz_devicemgr->new_input);
143
144     tz_devicemgr->global = wl_global_create(backend->display,
145             &tizen_input_device_manager_interface,
146             TIZEN_INPUT_DEVICEMGR_VERSION,
147             tz_devicemgr, device_manager_handle_bind);
148     if (!tz_devicemgr->global) {
149         goto err_global;
150     }
151
152     tz_devicemgr->devices.kbd = calloc(1,
153             sizeof(struct ds_tizen_input_devicemgr_device));
154     if (!tz_devicemgr->devices.kbd) {
155         goto err_kbd;
156     }
157     tz_devicemgr->devices.ptr = calloc(1,
158             sizeof(struct ds_tizen_input_devicemgr_device));
159     if (!tz_devicemgr->devices.ptr) {
160         goto err_ptr;
161     }
162     tz_devicemgr->devices.touch = calloc(1,
163             sizeof(struct ds_tizen_input_devicemgr_device));
164     if (!tz_devicemgr->devices.touch) {
165         goto err_touch;
166     }
167     tz_devicemgr->touch_max_count = 5;//TODO: make this variable configurable
168
169     tz_devicemgr->grab = ds_seat_create_keyboard_grab(
170             tz_devicemgr->seat, &devicemgr_keyboard_grab_iface, tz_devicemgr);
171     if (!tz_devicemgr->grab) {
172         goto err_grab;
173     }
174
175     wl_signal_init(&tz_devicemgr->events.destroy);
176     wl_signal_init(&tz_devicemgr->events.pointer_warp);
177     wl_list_init(&tz_devicemgr->clients);
178     wl_list_init(&tz_devicemgr->devices.kbd->key.pressed);
179     wl_list_init(&tz_devicemgr->keymap_list);
180     wl_list_init(&tz_devicemgr->blocked_keys);
181
182     if (!tizen_security_init()) {
183         ds_inf("tizen_security_init() is failed. go on without security");
184     }
185
186     ds_inf("Global created: ds_tizen_input_devicemgr(%p) ", tz_devicemgr);
187     return tz_devicemgr;
188
189 err_grab:
190     free(tz_devicemgr->devices.touch);
191 err_touch:
192     free(tz_devicemgr->devices.ptr);
193 err_ptr:
194     free(tz_devicemgr->devices.kbd);
195 err_kbd:
196     wl_global_destroy(tz_devicemgr->global);
197 err_global:
198     wl_list_remove(&tz_devicemgr->backend_destroy.link);
199     wl_list_remove(&tz_devicemgr->seat_destroy.link);
200     wl_list_remove(&tz_devicemgr->new_input.link);
201     free(tz_devicemgr);
202     return NULL;
203 }
204
205 WL_EXPORT void
206 ds_tizen_input_devicemgr_add_destroy_listener(
207         struct ds_tizen_input_devicemgr *tz_devicemgr,
208         struct wl_listener *listener)
209 {
210     wl_signal_add(&tz_devicemgr->events.destroy, listener);
211 }
212
213 WL_EXPORT void
214 ds_tizen_input_devicemgr_add_pointer_warp_listener(
215         struct ds_tizen_input_devicemgr *tz_devicemgr,
216         struct wl_listener *listener)
217 {
218     wl_signal_add(&tz_devicemgr->events.pointer_warp, listener);
219 }
220
221 WL_EXPORT bool
222 ds_tizen_input_devicemgr_set_keymap_list(
223         struct ds_tizen_input_devicemgr *tz_devicemgr, struct wl_list *list)
224 {
225     struct ds_tizen_input_devicemgr_keymap_data *data, *new_data;
226
227     if (!tz_devicemgr || !list) {
228         ds_err("Please insert correct data\n");
229         return false;
230     }
231
232     wl_list_for_each(data, list, link) {
233         new_data = calloc(1, sizeof *data);
234         if (!new_data) {
235             ds_err("Failed to alloc memory");
236             return false;
237         }
238         new_data->name = strdup(data->name);
239         new_data->keycode = data->keycode;
240         wl_list_insert(&tz_devicemgr->keymap_list, &new_data->link);
241     }
242     ds_inf("keymap set. length:%d",
243             wl_list_length(&tz_devicemgr->keymap_list));
244
245     return true;
246 }
247
248 WL_EXPORT bool
249 ds_tizen_input_devicemgr_set_output_width_height(
250         struct ds_tizen_input_devicemgr *tz_devicemgr,
251         uint32_t width, uint32_t height)
252 {
253     if (!tz_devicemgr) return false;
254
255     tz_devicemgr->output.width = width;
256     tz_devicemgr->output.height = height;
257
258     ds_inf("output's width: %d, height:%d", width, height);
259
260     return true;
261 }
262
263 static void
264 tz_devicemgr_destroy(struct ds_tizen_input_devicemgr *tz_devicemgr)
265 {
266     struct ds_tizen_input_devicemgr_client *client_data, *tmp;
267
268     tizen_security_finish();
269
270     tz_devicemgr_keymap_list_cleanup(tz_devicemgr);
271     tz_devicemgr_blocked_keys_cleanup(tz_devicemgr);
272     tz_devicemgr_ungrab_keyboard(tz_devicemgr);
273     ds_seat_keyboard_destroy_grab(tz_devicemgr->grab);
274
275     wl_signal_emit(&tz_devicemgr->events.destroy, tz_devicemgr);
276     wl_list_remove(&tz_devicemgr->backend_destroy.link);
277     wl_list_remove(&tz_devicemgr->seat_destroy.link);
278     wl_list_remove(&tz_devicemgr->new_input.link);
279
280     wl_global_destroy(tz_devicemgr->global);
281
282     wl_list_for_each_safe(client_data, tmp, &tz_devicemgr->clients, link) {
283         wl_list_remove(&client_data->link);
284         tz_devicemgr_deinit_generator(tz_devicemgr, client_data->resource);
285
286         wl_resource_set_user_data(client_data->resource, NULL);
287         free(client_data);
288     }
289
290     free(tz_devicemgr->devices.touch);
291     free(tz_devicemgr->devices.ptr);
292     free(tz_devicemgr->devices.kbd);
293     free(tz_devicemgr);
294 }
295
296 static void
297 backend_handle_destroy(struct wl_listener *listener, void *data)
298 {
299     struct ds_tizen_input_devicemgr *tz_devicemgr;
300
301     tz_devicemgr = wl_container_of(listener, tz_devicemgr, backend_destroy);
302
303     ds_inf("Global destroy: ds_tizen_input_devicemgr(%p)", tz_devicemgr);
304
305     tz_devicemgr_destroy(tz_devicemgr);
306 }
307
308 static void
309 seat_handle_destroy(struct wl_listener *listener, void *data)
310 {
311     struct ds_tizen_input_devicemgr *tz_devicemgr;
312
313     tz_devicemgr = wl_container_of(listener, tz_devicemgr, seat_destroy);
314
315     wl_list_remove(&tz_devicemgr->seat_destroy.link);
316     wl_list_init(&tz_devicemgr->seat_destroy.link);
317     tz_devicemgr->seat = NULL;
318 }
319
320 static void
321 tz_devicemgr_device_close(struct ds_tizen_input_devicemgr_device *dev)
322 {
323     if (!dev->input_device) return;
324     ds_input_device_destroy(dev->input_device);
325     dev->input_device = NULL;
326 }
327
328 static void
329 backend_handle_input_device_add(struct wl_listener *listener, void *data)
330 {
331     struct ds_input_device *dev = data;
332     struct ds_tizen_input_devicemgr *tz_devicemgr;
333     enum ds_input_device_type dev_type;
334
335     tz_devicemgr = wl_container_of(listener, tz_devicemgr, new_input);
336
337     dev_type = ds_input_device_get_type(dev);
338     if (dev_type == DS_INPUT_DEVICE_KEYBOARD) {
339         if (tz_devicemgr->devices.kbd->input_device) return;
340         ds_inf("devicemgr's kbd device is set to dev(%p)", dev);
341         tz_devicemgr->devices.kbd->input_device = dev;
342     }
343     else if (dev_type == DS_INPUT_DEVICE_POINTER) {
344         if (tz_devicemgr->devices.ptr->input_device) return;
345         ds_inf("devicemgr's ptr device is set to dev(%p)", dev);
346         tz_devicemgr->devices.ptr->input_device = dev;
347     }
348     else if (dev_type == DS_INPUT_DEVICE_TOUCH) {
349         if (tz_devicemgr->devices.touch->input_device) return;
350         ds_inf("devicemgr's touch device is set to dev(%p)", dev);
351         tz_devicemgr->devices.touch->input_device = dev;
352     }
353 }
354
355 static const struct tizen_input_device_manager_interface _devicemgr_impl = {
356     .block_events = device_manager_handle_block_events,
357     .unblock_events = device_manager_handle_unblock_events,
358     .init_generator = device_manager_handle_init_generator,
359     .deinit_generator = device_manager_handle_deinit_generator,
360     .generate_key = device_manager_handle_generate_key,
361     .generate_pointer = device_manager_handle_generate_pointer,
362     .generate_touch = device_manager_handle_generate_touch,
363     .pointer_warp = device_manager_handle_pointer_warp,
364     .init_generator_with_name =
365             device_manager_handle_init_generator_with_name, // v2
366     .destroy = device_manager_handle_destroy, // v3
367     .generate_axis = NULL, // v3
368     .set_touch_count = NULL, // v4
369 };
370
371 static void
372 device_manager_client_handle_resource_destroy(struct wl_resource *resource)
373 {
374     struct ds_tizen_input_devicemgr *tz_devicemgr;
375     struct ds_tizen_input_devicemgr_client *client_data, *tmp;
376
377     tz_devicemgr = wl_resource_get_user_data(resource);
378
379     tz_devicemgr_deinit_generator(tz_devicemgr, resource);
380
381     if (resource == tz_devicemgr->block_resource) {
382         tz_devicemgr_ungrab_keyboard_check(tz_devicemgr);
383         if (tz_devicemgr->timer) {
384             wl_event_source_remove(tz_devicemgr->timer);
385             tz_devicemgr->timer = NULL;
386         }
387     }
388
389     wl_list_for_each_safe(client_data, tmp, &tz_devicemgr->clients, link) {
390         if (client_data->resource == resource) {
391             wl_list_remove(&client_data->link);
392             free(client_data);
393         }
394     }
395 }
396
397 static void
398 device_manager_handle_bind(struct wl_client *client, void *data,
399         uint32_t version, uint32_t id)
400 {
401     struct ds_tizen_input_devicemgr *tz_devicemgr = data;
402     struct ds_tizen_input_devicemgr_client *client_data;
403
404     client_data = calloc(1, sizeof *client_data);
405     if (!client_data) {
406         ds_err("Failed to allocate memory !\n");
407         wl_client_post_no_memory(client);
408         return;
409     }
410
411     client_data->resource = wl_resource_create(client,
412             &tizen_input_device_manager_interface, MIN(version,4), id);
413     if (!client_data->resource) {
414         ds_err("Failed to create resource! (ver. :%d, id:%d)", version, id);
415         free(client_data);
416         wl_client_post_no_memory(client);
417         return;
418     }
419
420     client_data->init = false;
421     wl_list_init(&client_data->link);
422     wl_list_insert(&tz_devicemgr->clients, &client_data->link);
423
424     wl_resource_set_implementation(client_data->resource, &_devicemgr_impl,
425             tz_devicemgr, device_manager_client_handle_resource_destroy);
426 }
427
428 static void
429 device_manager_handle_init_generator(struct wl_client *client,
430         struct wl_resource *resource, uint32_t clas)
431 {
432     struct ds_tizen_input_devicemgr *tz_devicemgr;
433     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
434
435     tz_devicemgr = wl_resource_get_user_data(resource);
436
437     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
438             TIZEN_PRIV_INPUT_GENERATOR)) {
439         ds_err("No permission to input generate");
440         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
441         goto finish;
442     }
443
444     ret = tz_devicemgr_init_generator(tz_devicemgr, resource, clas,
445             "Input Generator");
446     if (ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
447         ds_err("Failed to init input generator\n");
448         goto finish;
449     }
450
451 finish:
452     tizen_input_device_manager_send_error(resource, ret);
453 }
454
455 static void
456 device_manager_handle_init_generator_with_name(struct wl_client *client,
457         struct wl_resource *resource, uint32_t clas, const char *name)
458 {
459     struct ds_tizen_input_devicemgr *tz_devicemgr;
460     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
461
462     tz_devicemgr = wl_resource_get_user_data(resource);
463
464     if (!name) {
465         ds_err("no name for device");
466         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
467         goto finish;
468     }
469
470     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
471             TIZEN_PRIV_INPUT_GENERATOR)) {
472         ds_err("No permission to input generate");
473         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
474         goto finish;
475     }
476
477     ret = tz_devicemgr_init_generator(tz_devicemgr, resource, clas,
478             name);
479     if (ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
480         ds_err("Failed to init input generator\n");
481         goto finish;
482     }
483
484 finish:
485     tizen_input_device_manager_send_error(resource, ret);
486 }
487
488 static void
489 device_manager_handle_deinit_generator(struct wl_client *client,
490         struct wl_resource *resource, uint32_t clas __attribute__((unused)))
491 {
492     struct ds_tizen_input_devicemgr *tz_devicemgr;
493     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
494
495     tz_devicemgr = wl_resource_get_user_data(resource);
496
497     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
498             TIZEN_PRIV_INPUT_GENERATOR)) {
499         ds_err("No permission to input generate");
500         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
501         goto finish;
502     }
503
504     ret = tz_devicemgr_deinit_generator(tz_devicemgr, resource);
505     if (ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
506         ds_err("Failed to deinit input generator\n");
507         goto finish;
508     }
509
510 finish:
511     tizen_input_device_manager_send_error(resource, ret);
512 }
513
514 struct keycode_map{
515     xkb_keysym_t keysym;
516     xkb_keycode_t keycode;
517 };
518
519 static void
520 find_keycode(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
521 {
522     struct keycode_map *found_keycodes = (struct keycode_map *)data;
523     xkb_keysym_t keysym = found_keycodes->keysym;
524     int nsyms = 0;
525     const xkb_keysym_t *syms_out = NULL;
526
527     if (found_keycodes->keycode) return;
528
529     nsyms = xkb_keymap_key_get_syms_by_level(keymap, key, 0, 0, &syms_out);
530     if (nsyms && syms_out) {
531         if (*syms_out == keysym) {
532             found_keycodes->keycode = key;
533         }
534     }
535 }
536
537 static void
538 tz_devicemgr_xkb_keycode_from_keysym(struct xkb_keymap *keymap,
539         xkb_keysym_t keysym, xkb_keycode_t *keycode)
540 {
541     struct keycode_map found_keycodes = {0,};
542     found_keycodes.keysym = keysym;
543     xkb_keymap_key_for_each(keymap, find_keycode, &found_keycodes);
544
545     *keycode = found_keycodes.keycode;
546 }
547
548 static int
549 tz_devicemgr_xkb_keyname_to_keycode(struct xkb_keymap *keymap,
550         const char *name)
551 {
552     xkb_keysym_t keysym = 0x0;
553     xkb_keycode_t keycode = 0;
554
555     if (!strncmp(name, "Keycode-", sizeof("Keycode-")-1)) {
556         keycode = atoi(name + 8);
557     } else {
558         keysym = xkb_keysym_from_name(name, XKB_KEYSYM_NO_FLAGS);
559         tz_devicemgr_xkb_keycode_from_keysym(keymap, keysym, &keycode);
560     }
561
562     return keycode;
563 }
564
565 static uint32_t
566 tz_devicemgr_client_get_device_clas(struct ds_tizen_input_devicemgr *tz_devicemgr,
567         struct wl_resource *resource)
568 {
569     struct ds_tizen_input_devicemgr_client *client_data;
570
571     wl_list_for_each(client_data, &tz_devicemgr->clients, link) {
572         if (client_data->resource == resource)
573             return client_data->clas;
574     }
575     return 0;
576 }
577
578 static void
579 device_manager_handle_generate_key(struct wl_client *client,
580         struct wl_resource *resource, const char *keyname, uint32_t pressed)
581 {
582     struct ds_tizen_input_devicemgr *tz_devicemgr;
583     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
584     int keycode = 0;
585     bool res;
586     struct ds_keyboard *kbd;
587     uint32_t clas;
588
589     tz_devicemgr = wl_resource_get_user_data(resource);
590
591     clas = tz_devicemgr_client_get_device_clas(tz_devicemgr, resource);
592     if ((clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD) == 0)
593     {
594         ds_err("Keyboard generator is not initialized by client");
595         goto finish;
596     }
597
598     if (!tz_devicemgr->devices.kbd ||
599         !tz_devicemgr->devices.kbd->input_device) {
600         ds_err("Keyboard device is not initialized\n");
601         goto finish;
602     }
603
604     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
605             TIZEN_PRIV_INPUT_GENERATOR)) {
606         ds_err("No permission to input generate");
607         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
608         goto finish;
609     }
610
611     // keyname to keycode using xkb_info
612     kbd = ds_input_device_get_keyboard(
613             tz_devicemgr->devices.kbd->input_device);
614     if (kbd->keymap) {
615         keycode = tz_devicemgr_xkb_keyname_to_keycode(kbd->keymap, keyname);
616     }
617
618     if (keycode <= 0) {
619         keycode = tz_devicemgr_keyname_to_keycode(&tz_devicemgr->keymap_list,
620                 keyname);
621     }
622     if (keycode <= 0)
623         goto finish;
624
625     res = tz_devicemgr_generate_key(tz_devicemgr->devices.kbd->input_device,
626             keycode, pressed);
627     if (!res) {
628         ds_err("Generating key is failed. key: %s, pressed: %d",
629                 keyname, pressed);
630         goto finish;
631     }
632     res = tz_devicemgr_pressed_keys_update(tz_devicemgr, keycode, pressed);
633     if (!res) {
634         ds_err("Updating pressed keys is failed. key: %s, pressed: %d",
635                 keyname, pressed);
636         goto finish;
637     }
638     ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
639
640 finish:
641     tizen_input_device_manager_send_error(resource, ret);
642 }
643
644 static void
645 device_manager_handle_generate_pointer(struct wl_client *client,
646         struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y,
647         uint32_t button)
648 {
649     struct ds_tizen_input_devicemgr *tz_devicemgr;
650     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
651     bool res;
652     bool state;
653     uint32_t clas;
654
655     tz_devicemgr = wl_resource_get_user_data(resource);
656
657     clas = tz_devicemgr_client_get_device_clas(tz_devicemgr, resource);
658     if ((clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE) == 0)
659     {
660         ds_err("Pointer generator is not initialized by client");
661         goto finish;
662     }
663
664     if (!tz_devicemgr->devices.ptr ||
665         !tz_devicemgr->devices.ptr->input_device) {
666         ds_err("Pointer device is not initialized\n");
667         goto finish;
668     }
669
670     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
671             TIZEN_PRIV_INPUT_GENERATOR)) {
672         ds_err("No permission to input generate");
673         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
674         goto finish;
675     }
676
677     if (button <= 0 || button >= TIZEN_INPUT_DEVICEMGR_MAX_BTN)
678     {
679         ds_err("Invalid button: %d\n", button);
680         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
681         goto finish;
682     }
683
684     if (type == TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE) {
685         res = tz_devicemgr_generate_mouse_move(
686                     tz_devicemgr->devices.ptr->input_device, (double)x, (double)y);
687         if (!res) goto finish;
688         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
689     }
690     else {
691         state = (type == TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN) ?
692                 true : false;
693         res = tz_devicemgr_generate_mouse_button(
694                     tz_devicemgr->devices.ptr->input_device, button, state);
695         if (!res) goto finish;
696         if (state)  tz_devicemgr->devices.ptr->mouse.pressed |= 1 << (button - 1);
697         else tz_devicemgr->devices.ptr->mouse.pressed &= ~(1 << (button - 1));
698         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
699     }
700
701 finish:
702     tizen_input_device_manager_send_error(resource, ret);
703 }
704
705 static void
706 device_manager_handle_generate_touch(struct wl_client *client,
707         struct wl_resource *resource, uint32_t type, uint32_t x, uint32_t y,
708         uint32_t finger)
709 {
710     struct ds_tizen_input_devicemgr *tz_devicemgr;
711     double transformed_x = .0, transformed_y = .0;
712     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
713     bool res;
714     uint32_t clas;
715
716     tz_devicemgr = wl_resource_get_user_data(resource);
717
718     clas = tz_devicemgr_client_get_device_clas(tz_devicemgr, resource);
719     if ((clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN) == 0)
720     {
721         ds_err("Touch generator is not initialized by client");
722         goto finish;
723     }
724
725     if (!tz_devicemgr->devices.touch ||
726         !tz_devicemgr->devices.touch->input_device) {
727         ds_err("Touch device is not initialized\n");
728         goto finish;
729     }
730
731     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
732             TIZEN_PRIV_INPUT_GENERATOR)) {
733         ds_err("No permission to input generate");
734         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
735         goto finish;
736     }
737
738     if (finger >= tz_devicemgr->touch_max_count)
739     {
740         ds_err("Invalid fingers: %d\n", finger);
741         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
742         goto finish;
743     }
744
745     if (tz_devicemgr->output.width != 0 && tz_devicemgr->output.height != 0) {
746         transformed_x = x / (double)tz_devicemgr->output.width;
747         transformed_y = y / (double)tz_devicemgr->output.height;
748     }
749
750     switch(type) {
751         case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN: // 0
752             res = tz_devicemgr_generate_touch_move(
753                     tz_devicemgr->devices.touch->input_device, transformed_x, transformed_y, finger);
754             if (!res) break;
755             res= tz_devicemgr_generate_touch_down(
756                 tz_devicemgr->devices.touch->input_device, transformed_x, transformed_y, finger);
757             if (!res) break;
758             tz_devicemgr->devices.touch->touch.pressed |= 1 << finger;
759             ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
760             break;
761         case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END: // 2
762             res = tz_devicemgr_generate_touch_up(
763                 tz_devicemgr->devices.touch->input_device, finger);
764             if (!res) break;
765             tz_devicemgr->devices.touch->touch.pressed &= ~(1 << finger);
766             ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
767             break;
768         case TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE: // 1
769             res = tz_devicemgr_generate_touch_move(
770                 tz_devicemgr->devices.touch->input_device, transformed_x, transformed_y, finger);
771             if (!res) break;
772             ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
773             break;
774         default:
775             break;
776     }
777
778 finish:
779     tizen_input_device_manager_send_error(resource, ret);
780 }
781
782 static void
783 device_manager_handle_pointer_warp(struct wl_client *client,
784         struct wl_resource *resource, struct wl_resource *surface,
785         wl_fixed_t x, wl_fixed_t y)
786 {
787     struct ds_tizen_input_devicemgr *tz_devicemgr;
788     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
789     int32_t new_x, new_y;
790     double transformed_x = .0, transformed_y = .0;
791     struct ds_tizen_input_devicemgr_event_pointer_warp ds_event;
792     struct timeval time;
793     unsigned int timestamp;
794
795     tz_devicemgr = wl_resource_get_user_data(resource);
796
797     if (!tz_devicemgr->devices.ptr ||
798         !tz_devicemgr->devices.ptr->input_device) {
799         ds_err("Pointer device is not initialized\n");
800         goto finish;
801     }
802
803     new_x = wl_fixed_to_int(x);
804     new_y = wl_fixed_to_int(y);
805
806     if (tz_devicemgr->output.width != 0 && tz_devicemgr->output.height != 0) {
807         transformed_x = new_x / (double)tz_devicemgr->output.width;
808         transformed_y = new_y / (double)tz_devicemgr->output.height;
809     }
810
811     gettimeofday(&time, NULL);
812     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
813
814     ds_event.time_msec = timestamp;
815     ds_event.surface = ds_surface_from_resource(surface);
816     ds_event.x = transformed_x;
817     ds_event.y = transformed_y;
818     ds_inf("Pointer warp. surface:%p, x:%.2f, y:%.2f", ds_event.surface,
819             ds_event.x, ds_event.y);
820
821     wl_signal_emit(&tz_devicemgr->events.pointer_warp, &ds_event);
822     ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
823
824 finish:
825     tizen_input_device_manager_send_error(resource, ret);
826 }
827
828 static void
829 device_manager_handle_destroy(struct wl_client *client,
830         struct wl_resource *resource)
831 {
832         wl_resource_destroy(resource);
833 }
834
835 static bool
836 tz_devicemgr_check_privilege(struct ds_tizen_input_devicemgr *tz_devicemgr,
837         struct wl_client *client, const char *rule)
838 {
839     pid_t pid = 0;
840     uid_t uid = 0;
841     gid_t gid = 0;
842
843     if (!client) return false;
844
845     wl_client_get_credentials(client, &pid, &uid, &gid);
846
847     return tizen_security_check_privilege(pid, uid, rule);
848 }
849
850 static const struct ds_input_device_interface input_device_iface =
851 {
852     .destroy = NULL,
853 };
854
855 static struct ds_keyboard *
856 create_ds_keyboard()
857 {
858     struct ds_keyboard *kbd;
859     kbd = calloc(1, sizeof *kbd);
860     if (!kbd) {
861         ds_err("Could not allocate memory");
862         return NULL;
863     }
864     ds_keyboard_init(kbd, NULL);
865
866     return kbd;
867 }
868
869 static struct ds_pointer *
870 create_ds_pointer()
871 {
872     struct ds_pointer *pointer;
873     pointer = calloc(1, sizeof *pointer);
874     if (!pointer) {
875         ds_err("Could not allocate memory");
876         return NULL;
877     }
878     ds_pointer_init(pointer, NULL);
879
880     return pointer;
881 }
882
883 static struct ds_touch *
884 create_ds_touch()
885 {
886     struct ds_touch *touch;
887     touch = calloc(1, sizeof *touch);
888     if (!touch) {
889         ds_err("Could not allocate memory");
890         return NULL;
891     }
892     ds_touch_init(touch, NULL);
893
894     return touch;
895 }
896
897 static int
898 tz_devicemgr_create_device(struct ds_tizen_input_devicemgr_device *dev,
899         uint32_t clas, const char *name)
900 {
901     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
902     const char *dev_name;
903
904     if (dev->name && strlen(dev->name) > 0) {
905         ds_inf("device already has name. name:%s", dev->name);
906         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
907     }
908
909     if (dev->input_device) {
910         dev_name = ds_input_device_get_name(dev->input_device);
911         ds_inf("device was already set. name:%s", dev_name);
912         dev->name = strdup(dev_name);
913         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
914     }
915
916     //input_device create
917     dev->input_device = calloc(1, sizeof(struct ds_input_device));
918     if(!dev->input_device) {
919         ds_err("Failed to create input device !\n");
920         return ret;
921     }
922
923     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD)
924     {
925         ds_input_device_init(dev->input_device, DS_INPUT_DEVICE_KEYBOARD,
926                 &input_device_iface, name, -1, -1);
927         dev->input_device->keyboard = create_ds_keyboard();
928     }
929     else if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE)
930     {
931         ds_input_device_init(dev->input_device, DS_INPUT_DEVICE_POINTER,
932                 &input_device_iface, name, -1, -1);
933         dev->input_device->pointer = create_ds_pointer();
934     }
935     else if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN)
936     {
937         ds_input_device_init(dev->input_device, DS_INPUT_DEVICE_TOUCH,
938                 &input_device_iface, name, -1, -1);
939         dev->input_device->touch = create_ds_touch();
940     }
941
942     dev->created = true;
943     dev->name = strdup(name);
944
945     return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
946 }
947
948 static int
949 tz_devicemgr_init_generator(struct ds_tizen_input_devicemgr *tz_devicemgr,
950         struct wl_resource *resource, uint32_t clas, const char *name)
951 {
952     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
953     struct ds_tizen_input_devicemgr_client *client_data;
954     struct ds_tizen_input_devicemgr_device *dev;
955     bool inited = false;
956
957     ds_inf("Init generator. name:%s", name);
958
959     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD) {
960         dev = tz_devicemgr->devices.kbd;
961         ret = tz_devicemgr_create_device(dev, clas, name);
962         if (ret == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
963             inited = true;
964             tz_devicemgr->devices.kbd->ref++;
965         }
966         if (dev->created) {
967             wl_signal_emit(&tz_devicemgr->backend->events.new_input,
968                     dev->input_device);
969         }
970     }
971     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE) {
972         dev = tz_devicemgr->devices.ptr;
973         ret = tz_devicemgr_create_device(dev, clas, name);
974         if (ret == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
975             inited = true;
976             tz_devicemgr->devices.ptr->ref++;
977         }
978         if (dev->created) {
979             wl_signal_emit(&tz_devicemgr->backend->events.new_input,
980                     dev->input_device);
981         }
982     }
983     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN) {
984         dev = tz_devicemgr->devices.touch;
985         ret = tz_devicemgr_create_device(dev, clas, name);
986         if (ret == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE) {
987             inited = true;
988             tz_devicemgr->devices.touch->ref++;
989         }
990         if (dev->created) {
991             wl_signal_emit(&tz_devicemgr->backend->events.new_input,
992                     dev->input_device);
993         }
994     }
995
996     if (inited) {
997         wl_list_for_each(client_data, &tz_devicemgr->clients, link) {
998             if (client_data->resource == resource) {
999                 if (client_data->init == false) {
1000                     client_data->init = true;
1001                     client_data->clas = clas;
1002                 }
1003                 break;
1004             }
1005         }
1006     }
1007
1008     return ret;
1009 }
1010
1011 static int
1012 tz_devicemgr_deinit_generator(struct ds_tizen_input_devicemgr *tz_devicemgr,
1013         struct wl_resource *resource)
1014 {
1015     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
1016     struct ds_tizen_input_devicemgr_client *client_data;
1017     struct ds_tizen_input_devicemgr_device *dev;
1018     uint32_t clas = 0;
1019     int i = 0;
1020     bool res;
1021
1022     ds_inf("Deinit generator.");
1023     wl_list_for_each(client_data, &tz_devicemgr->clients, link) {
1024         if (client_data->resource == resource) {
1025             if (client_data->init == true) {
1026                 client_data->init = false;
1027                 clas = client_data->clas;
1028                 break;
1029             } else {
1030                 return ret;
1031             }
1032         }
1033     }
1034     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD) {
1035         dev = tz_devicemgr->devices.kbd;
1036         dev->ref--;
1037         if (dev->ref<= 0) {
1038             dev->ref = 0;
1039             tz_devicemgr_pressed_keys_cleanup(tz_devicemgr);
1040
1041             if (dev->created) {
1042                 tz_devicemgr_device_close(dev);
1043                 dev->created = false;
1044             }
1045             free(dev->name);
1046             dev->name = NULL;
1047         }
1048     }
1049     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE) {
1050         dev = tz_devicemgr->devices.ptr;
1051         dev->ref--;
1052         if (dev->ref <= 0) {
1053             dev->ref = 0;
1054             while(dev->mouse.pressed)
1055             {
1056                 if (dev->mouse.pressed & (1 << i))
1057                 {
1058                     res = tz_devicemgr_generate_mouse_button(dev->input_device,
1059                             i + 1, false);
1060                     if (!res) break;
1061                     dev->mouse.pressed &= ~(1 << i);
1062                 }
1063                 i++;
1064                 if (i >= TIZEN_INPUT_DEVICEMGR_MAX_BTN) break;
1065             }
1066
1067             if (dev->created)
1068             {
1069                 tz_devicemgr_device_close(dev);
1070                 dev->created = false;
1071             }
1072             free(dev->name);
1073             dev->name = NULL;
1074         }
1075     }
1076     if (clas & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN) {
1077         dev = tz_devicemgr->devices.touch;
1078         dev->ref--;
1079         if (dev->ref <= 0) {
1080             dev->ref = 0;
1081             while(dev->touch.pressed)
1082             {
1083                 if (dev->touch.pressed & (1 << i))
1084                 {
1085                     res = tz_devicemgr_generate_touch_up(dev->input_device,
1086                             i);
1087                     if (!res) break;
1088                     dev->touch.pressed &= ~(1 << i);
1089                 }
1090                 i++;
1091                 if (i >= tz_devicemgr->touch_max_count) break;
1092             }
1093
1094             if (dev->created)
1095             {
1096                 tz_devicemgr_device_close(dev);
1097                 dev->created = false;
1098             }
1099             free(dev->name);
1100             dev->name = NULL;
1101         }
1102     }
1103
1104     return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
1105 }
1106
1107 static int
1108 tz_devicemgr_keyname_to_keycode(struct wl_list *list, const char *name)
1109 {
1110     struct ds_tizen_input_devicemgr_keymap_data *data;
1111
1112     if (!wl_list_empty(list)) {
1113         wl_list_for_each(data, list, link) {
1114             if (!strcmp(data->name, name)) {
1115                 return data->keycode;
1116             }
1117         }
1118     }
1119
1120     return 0;
1121 }
1122
1123 static bool
1124 tz_devicemgr_generate_key(struct ds_input_device *device, int keycode,
1125         int pressed)
1126 {
1127     struct ds_event_keyboard_key ds_event;
1128     struct timeval time;
1129     unsigned int timestamp;
1130     struct ds_keyboard *kbd;
1131
1132     kbd = ds_input_device_get_keyboard(device);
1133     if (!kbd) {
1134         ds_err("No ds_keyboard to notify event");
1135         return false;
1136     }
1137
1138     gettimeofday(&time, NULL);
1139     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1140
1141     ds_event.time_msec = timestamp;
1142     ds_event.keycode = keycode - 8;
1143     if (pressed)
1144         ds_event.state = WL_KEYBOARD_KEY_STATE_PRESSED;
1145     else
1146         ds_event.state = WL_KEYBOARD_KEY_STATE_RELEASED;
1147
1148     ds_inf("Generate key. kbd:%p, key:%d, state:%s", kbd, ds_event.keycode,
1149             (ds_event.state == WL_KEYBOARD_KEY_STATE_PRESSED) ?
1150             "PRESSED" : "RELEASED");
1151
1152     ds_keyboard_notify_key(kbd, &ds_event);
1153
1154     return true;
1155 }
1156
1157 static bool
1158 tz_devicemgr_pressed_keys_update(struct ds_tizen_input_devicemgr *tz_devicemgr,
1159         int keycode, bool pressed)
1160 {
1161     struct ds_tizen_input_devicemgr_key_info *key, *tmp;
1162
1163     if (pressed) {
1164         key = calloc(1, sizeof(*key));
1165         if (!key) {
1166             ds_err("Failed to alloc keydata memory.\n");
1167             return false;
1168         }
1169         key->keycode = keycode;
1170         wl_list_init(&key->link);
1171         wl_list_insert(&tz_devicemgr->devices.kbd->key.pressed, &key->link);
1172     }
1173     else {
1174         wl_list_for_each_safe(key, tmp, &tz_devicemgr->devices.kbd->key.pressed, link) {
1175             if (key->keycode == keycode) {
1176                 wl_list_remove(&key->link);
1177                 free(key);
1178                 break;
1179             }
1180         }
1181     }
1182
1183     ds_inf("Update pressed keys. length: %d, keycode:%d, pressed:%d",
1184             wl_list_length(&tz_devicemgr->devices.kbd->key.pressed), keycode, pressed);
1185
1186     return true;
1187 }
1188
1189 static void
1190 tz_devicemgr_pressed_keys_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr)
1191 {
1192     struct ds_tizen_input_devicemgr_key_info *keydata, *tmp;
1193
1194     ds_inf("Clean up the kbd.pressed_keys. length: %d",
1195             wl_list_length(&tz_devicemgr->devices.kbd->key.pressed));
1196
1197     wl_list_for_each_safe(keydata, tmp, &tz_devicemgr->devices.kbd->key.pressed, link) {
1198         if (tz_devicemgr->devices.kbd)
1199             tz_devicemgr_generate_key(tz_devicemgr->devices.kbd->input_device,
1200                     keydata->keycode, false);
1201         wl_list_remove(&keydata->link);
1202         free(keydata);
1203     }
1204 }
1205
1206 static void
1207 tz_devicemgr_keymap_list_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr)
1208 {
1209     struct ds_tizen_input_devicemgr_keymap_data *keymap, *tmp;
1210
1211     ds_inf("Clean up the keymap_list. length: %d",
1212             wl_list_length(&tz_devicemgr->keymap_list));
1213
1214     wl_list_for_each_safe(keymap, tmp, &tz_devicemgr->keymap_list, link) {
1215         free(keymap->name);
1216         wl_list_remove(&keymap->link);
1217         free(keymap);
1218     }
1219 }
1220
1221 static bool
1222 tz_devicemgr_generate_touch_move(struct ds_input_device *device, double x, double y,
1223         uint32_t finger)
1224 {
1225     struct ds_event_touch_motion ds_event;
1226     struct timeval time;
1227     unsigned int timestamp;
1228     struct ds_touch *touch;
1229
1230     touch = ds_input_device_get_touch(device);
1231     if (!touch) {
1232         ds_err("No ds_touch to notify event");
1233         return false;
1234     }
1235
1236     gettimeofday(&time, NULL);
1237     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1238
1239     ds_event.time_msec = timestamp;
1240     ds_event.id = finger;
1241     ds_event.x = x;
1242     ds_event.y = y;
1243     ds_inf("Generate touch motion. touch:%p, id:%d (%d, %d)", touch, ds_event.id, x, y);
1244
1245     wl_signal_emit(&touch->events.motion, &ds_event);
1246
1247     return true;
1248 }
1249
1250 static bool
1251 tz_devicemgr_generate_touch_down(struct ds_input_device *device, double x, double y,
1252         uint32_t finger)
1253 {
1254     struct ds_event_touch_down ds_event;
1255     struct timeval time;
1256     unsigned int timestamp;
1257     struct ds_touch *touch;
1258
1259     touch = ds_input_device_get_touch(device);
1260     if (!touch) {
1261         ds_err("No ds_touch to notify event");
1262         return false;
1263     }
1264
1265     gettimeofday(&time, NULL);
1266     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1267
1268     ds_event.time_msec = timestamp;
1269     ds_event.id = finger;
1270     ds_event.x = x;
1271     ds_event.y = y;
1272     ds_inf("Generate touch down. touch:%p, id:%d (%d, %d)", touch, ds_event.id, x, y);
1273
1274     wl_signal_emit(&touch->events.down, &ds_event);
1275
1276     return true;
1277 }
1278
1279 static bool
1280 tz_devicemgr_generate_touch_up(struct ds_input_device *device, uint32_t finger)
1281 {
1282     struct ds_event_touch_up ds_event;
1283     struct timeval time;
1284     unsigned int timestamp;
1285     struct ds_touch *touch;
1286
1287     touch = ds_input_device_get_touch(device);
1288     if (!touch) {
1289         ds_err("No ds_touch to notify event");
1290         return false;
1291     }
1292
1293     gettimeofday(&time, NULL);
1294     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1295
1296     ds_event.time_msec = timestamp;
1297     ds_event.id = finger;
1298     ds_inf("Generate touch up. touch:%p, id:%d", touch, ds_event.id);
1299
1300     wl_signal_emit(&touch->events.up, &ds_event);
1301
1302     return true;
1303 }
1304
1305 static bool
1306 tz_devicemgr_generate_mouse_move(struct ds_input_device *device, double x, double y)
1307 {
1308     struct ds_event_pointer_motion ds_event;
1309     struct timeval time;
1310     unsigned int timestamp;
1311     struct ds_pointer *pointer;
1312
1313     pointer = ds_input_device_get_pointer(device);
1314     if (!pointer) {
1315         ds_err("No ds_pointer to notify event");
1316         return false;
1317     }
1318
1319     gettimeofday(&time, NULL);
1320     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1321
1322     ds_event.time_msec = timestamp;
1323     ds_event.delta_x = x;
1324     ds_event.delta_y = y;
1325     ds_inf("Generate mouse motion. pointer:%p, x:%f, y:%f", pointer, ds_event.delta_x, ds_event.delta_y);
1326
1327     wl_signal_emit(&pointer->events.motion, &ds_event);
1328
1329     return true;
1330 }
1331
1332 static bool
1333 tz_devicemgr_generate_mouse_button(struct ds_input_device *device, uint32_t button, bool state)
1334 {
1335     struct ds_event_pointer_button ds_event;
1336     struct timeval time;
1337     unsigned int timestamp;
1338     struct ds_pointer *pointer;
1339
1340     pointer = ds_input_device_get_pointer(device);
1341     if (!pointer) {
1342         ds_err("No ds_pointer to notify event");
1343         return false;
1344     }
1345
1346     gettimeofday(&time, NULL);
1347     timestamp = time.tv_sec * 1000 + time.tv_usec / 1000;
1348
1349     ds_event.time_msec = timestamp;
1350     ds_event.button = button;
1351     if (state)
1352         ds_event.state = DS_BUTTON_PRESSED;
1353     else
1354         ds_event.state = DS_BUTTON_RELEASED;
1355     ds_inf("Generate mouse button. pointer:%p, button:%d, state:%s", pointer, ds_event.button, state ? "PRESSED" : "RELEASED");
1356
1357     wl_signal_emit(&pointer->events.button, &ds_event);
1358
1359     return true;
1360 }
1361
1362 static void
1363 devicemgr_keyboard_grab_iface_enter(struct ds_seat_keyboard_grab *grab,
1364             struct ds_surface *surface, uint32_t keycodes[],
1365             size_t num_keycodes, struct ds_keyboard_modifiers *modifiers)
1366 {
1367     ds_inf("devicemgr. keyboard_grab_iface_enter");
1368 }
1369
1370 static void
1371 devicemgr_keyboard_grab_iface_clear_focus(struct ds_seat_keyboard_grab *grab)
1372 {
1373     ds_inf("devicemgr. keyboard_grab_iface_clear_focus");
1374 }
1375
1376 static void
1377 tz_devicemgr_blocked_keys_cleanup(struct ds_tizen_input_devicemgr *tz_devicemgr)
1378 {
1379     struct ds_tizen_input_devicemgr_key_info *keydata, *tmp;
1380
1381     ds_inf("Clean up the blocked keys. length: %d",
1382             wl_list_length(&tz_devicemgr->blocked_keys));
1383
1384     wl_list_for_each_safe(keydata, tmp, &tz_devicemgr->blocked_keys, link) {
1385         wl_list_remove(&keydata->link);
1386         free(keydata);
1387     }
1388 }
1389
1390 static void
1391 devicemgr_keyboard_grab_iface_key(struct ds_seat_keyboard_grab *grab,
1392         uint32_t time_msec, uint32_t key, uint32_t state)
1393 {
1394     struct ds_tizen_input_devicemgr *devicemgr;
1395     struct ds_tizen_input_devicemgr_key_info *keydata, *tmp;
1396     bool key_blocked = false;
1397
1398     ds_inf("devicemgr. keyboard_grab_iface_key");
1399
1400     devicemgr = ds_seat_keyboard_grab_get_data(grab);
1401     if (!devicemgr->block_resource) {
1402         if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1403             goto finish;
1404         }
1405         else {
1406             wl_list_for_each_safe(keydata, tmp, &devicemgr->blocked_keys, link) {
1407                 if (keydata->keycode == (int)key) {
1408                     wl_list_remove(&keydata->link);
1409                     free(keydata);
1410                     key_blocked = true;
1411                     break;
1412                 }
1413             }
1414             if (wl_list_empty(&devicemgr->blocked_keys)) {
1415                 tz_devicemgr_ungrab_keyboard(devicemgr);
1416             }
1417             if (key_blocked) {
1418                 goto finish;
1419             }
1420         }
1421     }
1422
1423     if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1424         keydata = calloc(1, sizeof (*keydata));
1425         if (!keydata)
1426             goto finish;
1427         keydata->keycode = key;
1428         wl_list_init(&keydata->link);
1429         wl_list_insert(&devicemgr->blocked_keys, &keydata->link);
1430         key_blocked = true;
1431     }
1432     else {
1433         if (wl_list_empty(&devicemgr->blocked_keys))
1434             goto finish;
1435         wl_list_for_each_safe(keydata, tmp, &devicemgr->blocked_keys, link) {
1436             if (keydata->keycode == (int)key) {
1437                 wl_list_remove(&keydata->link);
1438                 free(keydata);
1439                 key_blocked = true;
1440             }
1441         }
1442     }
1443
1444 finish:
1445     if (!key_blocked)
1446         ds_inf("block key event: (%d %s)\n", key, (state ? "press" : "release"));
1447 }
1448
1449 static void
1450 devicemgr_modifiers_grab_iface_key(struct ds_seat_keyboard_grab *grab,
1451         struct ds_keyboard_modifiers *modifiers)
1452 {
1453     ds_inf("devicemgr. modifiers_grab_iface_key");
1454 }
1455
1456 static void
1457 devicemgr_cancel_grab_iface_key(struct ds_seat_keyboard_grab *grab)
1458 {
1459     ds_inf("devicemgr. cancel_grab_iface_key");
1460 }
1461
1462 static const struct ds_keyboard_grab_interface devicemgr_keyboard_grab_iface = {
1463     .enter = devicemgr_keyboard_grab_iface_enter,
1464     .clear_focus = devicemgr_keyboard_grab_iface_clear_focus,
1465     .key = devicemgr_keyboard_grab_iface_key,
1466     .modifiers = devicemgr_modifiers_grab_iface_key,
1467     .cancel = devicemgr_cancel_grab_iface_key,
1468 };
1469
1470 static void
1471 tz_devicemgr_grab_keyboard(struct ds_tizen_input_devicemgr *tz_devicemgr)
1472 {
1473     ds_seat_keyboard_start_grab(tz_devicemgr->seat, tz_devicemgr->grab);
1474 }
1475
1476 static void
1477 tz_devicemgr_ungrab_keyboard(struct ds_tizen_input_devicemgr *tz_devicemgr)
1478 {
1479         ds_seat_keyboard_end_grab(tz_devicemgr->seat);
1480 }
1481
1482 static void
1483 tz_devicemgr_ungrab_keyboard_check(struct ds_tizen_input_devicemgr *tz_devicemgr)
1484 {
1485     if (wl_list_empty(&tz_devicemgr->blocked_keys))
1486         tz_devicemgr_ungrab_keyboard(tz_devicemgr);
1487
1488     tz_devicemgr->block_resource = NULL;
1489 }
1490
1491 static bool
1492 devicemgr_add_timer(struct ds_tizen_input_devicemgr *tz_devicemgr,
1493         wl_event_loop_timer_func_t func, int time)
1494 {
1495     struct wl_event_loop *event_loop;
1496
1497     event_loop = wl_display_get_event_loop(tz_devicemgr->backend->display);
1498     if (!event_loop) {
1499         ds_err("Failed to get event_loop from display: %p",
1500                 tz_devicemgr->backend->display);
1501         return false;
1502     }
1503
1504     tz_devicemgr->timer = wl_event_loop_add_timer(event_loop, func,
1505             tz_devicemgr);
1506     if (!tz_devicemgr->timer) {
1507         ds_err("Failed to timer");
1508         return false;
1509     }
1510     wl_event_source_timer_update(tz_devicemgr->timer, time);
1511
1512     return true;
1513 }
1514
1515 static int
1516 devicemgr_block_timer(void *data)
1517 {
1518         struct ds_tizen_input_devicemgr *devicemgr = data;
1519
1520         tizen_input_device_manager_send_block_expired(devicemgr->block_resource);
1521
1522         tz_devicemgr_ungrab_keyboard_check(devicemgr);
1523
1524         wl_event_source_remove(devicemgr->timer);
1525         devicemgr->timer = NULL;
1526
1527         return 1;
1528 }
1529
1530 static void
1531 device_manager_handle_block_events(struct wl_client *client,
1532         struct wl_resource *resource, uint32_t serial, uint32_t clas,
1533         uint32_t duration)
1534 {
1535     struct ds_tizen_input_devicemgr *tz_devicemgr;
1536     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
1537     bool res;
1538
1539     tz_devicemgr = wl_resource_get_user_data(resource);
1540
1541     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
1542             TIZEN_PRIV_INPUT_BLOCK)) {
1543         ds_err("No permission to input generate");
1544         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
1545         goto finish;
1546     }
1547
1548     if (clas != TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD) {
1549         ds_err("only support keyboard device. (requested: 0x%x)\n", clas);
1550         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
1551         goto finish;
1552     }
1553
1554     if(tz_devicemgr->block_resource) {
1555         ds_err("currently the input system is already blocked\n");
1556         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
1557         goto finish;
1558     }
1559
1560     res = devicemgr_add_timer(tz_devicemgr, devicemgr_block_timer, duration);
1561     if (!res) {
1562         ds_err("Failed to add a timer\n");
1563         goto finish;
1564     }
1565
1566     tz_devicemgr_grab_keyboard(tz_devicemgr);
1567     tz_devicemgr->block_resource = resource;
1568     ds_inf("Block events. clas: %d, duration:%d", clas, duration);
1569     ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
1570
1571 finish:
1572     tizen_input_device_manager_send_error(resource, ret);
1573 }
1574
1575 static void
1576 device_manager_handle_unblock_events(struct wl_client *client,
1577         struct wl_resource *resource, uint32_t serial)
1578 {
1579     struct ds_tizen_input_devicemgr *tz_devicemgr;
1580     int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
1581
1582     tz_devicemgr = wl_resource_get_user_data(resource);
1583
1584     if (!tz_devicemgr_check_privilege(tz_devicemgr, client,
1585             TIZEN_PRIV_INPUT_BLOCK)) {
1586         ds_err("No permission to input generate");
1587         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION;
1588         goto finish;
1589     }
1590
1591     if (tz_devicemgr->block_resource != resource) {
1592         ds_err("currently the input system is blocked by another resource");
1593         ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER;
1594         goto finish;
1595     }
1596
1597     tz_devicemgr_ungrab_keyboard_check(tz_devicemgr);
1598     tz_devicemgr->block_resource = NULL;
1599     ds_inf("Unblock events.");
1600     ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
1601
1602     if (tz_devicemgr->timer) {
1603                 wl_event_source_remove(tz_devicemgr->timer);
1604                 tz_devicemgr->timer = NULL;
1605         }
1606
1607 finish:
1608     tizen_input_device_manager_send_error(resource, ret);
1609 }
1610