e_devicemgr: make an internal header
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr_keyboard_grab.c
1 #include "e.h"
2 #include "e_devicemgr_private.h"
3 #include "e_input_intern.h"
4 #include "e_comp_wl_intern.h"
5
6 static void _e_devicemgr_keyboard_grab_client_cb_destroy(struct wl_listener *l, void *data);
7
8 EINTERN Eina_Bool
9 e_devicemgr_keyboard_grab_subtype_is_grabbed(Ecore_Device_Subclass subclas)
10 {
11    if ((subclas == ECORE_DEVICE_SUBCLASS_NONE) &&
12        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_NONE))
13        return EINA_TRUE;
14
15    if ((subclas == ECORE_DEVICE_SUBCLASS_REMOCON) &&
16        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_REMOCON))
17        return EINA_TRUE;
18
19    if ((subclas == ECORE_DEVICE_SUBCLASS_VIRTUAL_KEYBOARD) &&
20        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_VIRTUAL_KEYBOARD))
21        return EINA_TRUE;
22
23    DMWRN("Keyboard Grab. device subclas(%d) is not grabbed", subclas);
24    return EINA_FALSE;
25 }
26
27 static void
28 _e_devicemgr_keyboard_grab_key_handler(uint32_t keycode, uint32_t state,
29                                            uint32_t timestamp, void *dev)
30 {
31     DMINF("key(%d-%s) event is delivered to grab ec(%p)",
32           keycode, state ? "Pressed" : "Released", e_devicemgr->keyboard_grab.ec);
33
34    if (!e_comp_wl_key_send(e_devicemgr->keyboard_grab.ec, keycode, state ? EINA_TRUE : EINA_FALSE,
35                       dev, timestamp))
36       {
37          DMERR("Could not send keyboard_grab_key to client(%p)", e_devicemgr->keyboard_grab.client);
38       }
39
40    e_input_flush_keyboard_share_events();
41 }
42
43 static Eina_Bool
44 _e_devicemgr_keyboard_grab_client_remove(struct wl_client *client)
45 {
46    struct wl_listener *destroy_listener = NULL;
47
48    if (client != e_devicemgr->keyboard_grab.client)
49      return EINA_FALSE;
50
51    e_devicemgr->keyboard_grab.subtype = 0x0;
52    e_devicemgr->keyboard_grab.client = NULL;
53    e_devicemgr->keyboard_grab.ec = NULL;
54
55    if (!e_input_keyboard_grab_key_handler_set(NULL))
56      {
57         DMERR("ERROR! Could not set keyboard_grab key handler to NULL!");
58         return EINA_FALSE;
59      }
60
61    destroy_listener = wl_client_get_destroy_listener(client, _e_devicemgr_keyboard_grab_client_cb_destroy);
62    if (destroy_listener)
63      {
64         wl_list_remove(&destroy_listener->link);
65         E_FREE(destroy_listener);
66      }
67
68    return EINA_TRUE;
69 }
70
71 static void
72 _e_devicemgr_keyboard_grab_client_cb_destroy(struct wl_listener *l, void *data)
73 {
74    struct wl_client *client = (struct wl_client *)data;
75
76    if (!e_devicemgr->keyboard_grab.client) return;
77
78    wl_list_remove(&l->link);
79    E_FREE(l);
80
81    _e_devicemgr_keyboard_grab_client_remove(client);
82 }
83
84 static Eina_Bool
85 _e_devicemgr_keyboard_grab_client_add(struct wl_client *client, E_Client *ec, uint32_t subclas)
86 {
87    struct wl_listener *destroy_listener = NULL;
88
89    e_devicemgr->keyboard_grab.subtype |= subclas;
90
91    if (e_devicemgr->keyboard_grab.client == client)
92      return EINA_TRUE;
93
94    e_devicemgr->keyboard_grab.client = client;
95    e_devicemgr->keyboard_grab.ec = ec;
96
97    if (!e_input_keyboard_grab_key_handler_set(_e_devicemgr_keyboard_grab_key_handler))
98      {
99         DMERR("ERROR! Could not set keyboard_grab key handler to func!");
100         return EINA_FALSE;
101      }
102
103    destroy_listener = E_NEW(struct wl_listener, 1);
104    EINA_SAFETY_ON_NULL_GOTO(destroy_listener, failed);
105    destroy_listener->notify = _e_devicemgr_keyboard_grab_client_cb_destroy;
106    wl_client_add_destroy_listener(client, destroy_listener);
107
108    return EINA_TRUE;
109
110 failed:
111    e_devicemgr->keyboard_grab.client = NULL;
112    e_devicemgr->keyboard_grab.ec = NULL;
113    e_devicemgr->keyboard_grab.subtype = 0x0;
114
115    return EINA_FALSE;
116 }
117
118 int
119 e_devicemgr_keyboard_grab(struct wl_client *client, struct wl_resource *surface, uint32_t subclas)
120 {
121    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
122    E_Client *ec;
123    uint32_t all_subclass = TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_NONE |
124                         TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_REMOCON |
125                         TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_VIRTUAL_KEYBOARD;
126
127    if ((e_devicemgr->keyboard_grab.client) && (e_devicemgr->keyboard_grab.client != client))
128      {
129         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
130      }
131    if (!(subclas & all_subclass))
132      {
133         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_CLASS;
134      }
135
136    ec = e_client_from_surface_resource(surface);
137    if (!ec)
138      return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
139
140    if (!_e_devicemgr_keyboard_grab_client_add(client, ec, subclas))
141      ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
142
143    return ret;
144 }
145
146 int
147 e_devicemgr_keyboard_ungrab(struct wl_client *client, struct wl_resource *surface)
148 {
149    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
150    E_Client *ec;
151
152    if ((e_devicemgr->keyboard_grab.client) && (e_devicemgr->keyboard_grab.client != client))
153     {
154        return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
155     }
156
157    ec = e_client_from_surface_resource(surface);
158    if (!ec)
159      return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
160
161    if (!_e_devicemgr_keyboard_grab_client_remove(client))
162      ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
163
164    return ret;
165 }