keyrouter: Fix wrong return value
[platform/core/uifw/libds-tizen.git] / src / libds / input_device.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wayland-server.h>
5
6 #include "libds/log.h"
7 #include "libds/interfaces/input_device.h"
8 #include "libds/interfaces/pointer.h"
9 #include "libds/interfaces/keyboard.h"
10 #include "libds/interfaces/touch.h"
11
12 WL_EXPORT enum ds_input_device_type
13 ds_input_device_get_type(struct ds_input_device *dev)
14 {
15     return dev->type;
16 }
17
18 WL_EXPORT struct ds_pointer *
19 ds_input_device_get_pointer(struct ds_input_device *dev)
20 {
21     if (dev->type != DS_INPUT_DEVICE_POINTER) {
22         ds_err("Given ds_input_device is not a pointer device");
23         return NULL;
24     }
25
26     return dev->pointer;
27 }
28
29 WL_EXPORT struct ds_keyboard *
30 ds_input_device_get_keyboard(struct ds_input_device *dev)
31 {
32     if (dev->type != DS_INPUT_DEVICE_KEYBOARD) {
33         ds_err("Given ds_input_device is not a keyboard device");
34         return NULL;
35     }
36
37     return dev->keyboard;
38 }
39
40 WL_EXPORT struct ds_touch *
41 ds_input_device_get_touch(struct ds_input_device *dev)
42 {
43     if (dev->type != DS_INPUT_DEVICE_TOUCH) {
44         ds_err("Given ds_input_device is not a touch device");
45         return NULL;
46     }
47
48     return dev->touch;
49 }
50
51 WL_EXPORT void
52 ds_input_device_add_destroy_listener(struct ds_input_device *dev,
53         struct wl_listener *listener)
54 {
55     wl_signal_add(&dev->events.destroy, listener);
56 }
57
58 void
59 ds_input_device_init(struct ds_input_device *dev,
60         enum ds_input_device_type type,
61         const struct ds_input_device_interface *iface,
62         const char *name, int vendor, int product)
63 {
64     dev->type = type;
65     dev->iface = iface;
66     dev->name = strdup(name);
67
68     wl_signal_init(&dev->events.destroy);
69 }
70
71 void
72 ds_input_device_destroy(struct ds_input_device *dev)
73 {
74     wl_signal_emit(&dev->events.destroy, dev);
75
76     if (dev->_device) {
77         switch (dev->type) {
78             case DS_INPUT_DEVICE_POINTER:
79                 ds_pointer_destroy(dev->pointer);
80                 break;
81             case DS_INPUT_DEVICE_KEYBOARD:
82                 ds_keyboard_destroy(dev->keyboard);
83                 break;
84             default:
85                 ds_err("Warning: leaking memory %p %p %d",
86                         dev->_device, dev, dev->type);
87                 break;
88         }
89     }
90
91     free(dev->name);
92     if (dev->iface && dev->iface->destroy)
93         dev->iface->destroy(dev);
94     else
95         free(dev);
96 }