6bf5d7a901ab24dd75f2aa80e3fa952fe8a00ecd
[profile/ivi/ecore.git] / src / lib / ecore_input_evas / ecore_input_evas.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <string.h>
6
7 #include "Ecore.h"
8 #include "Ecore_Input.h"
9
10 #include "Ecore_Input_Evas.h"
11 #include "ecore_input_evas_private.h"
12
13 int _ecore_input_evas_log_dom = -1;
14
15 typedef struct _Ecore_Input_Window Ecore_Input_Window;
16 struct _Ecore_Input_Window
17 {
18    Evas *evas;
19    void *window;
20    Ecore_Event_Mouse_Move_Cb move_mouse;
21    int ignore_event;
22 };
23
24 static int _ecore_event_evas_init_count = 0;
25 static Ecore_Event_Handler *ecore_event_evas_handlers[8];
26 static Eina_Hash *_window_hash = NULL;
27
28 EAPI void
29 ecore_event_evas_modifier_lock_update(Evas *e, unsigned int modifiers)
30 {
31    if (modifiers & ECORE_EVENT_MODIFIER_SHIFT)
32      evas_key_modifier_on(e, "Shift");
33    else evas_key_modifier_off(e, "Shift");
34
35    if (modifiers & ECORE_EVENT_MODIFIER_CTRL)
36      evas_key_modifier_on(e, "Control");
37    else evas_key_modifier_off(e, "Control");
38
39    if (modifiers & ECORE_EVENT_MODIFIER_ALT)
40      evas_key_modifier_on(e, "Alt");
41    else evas_key_modifier_off(e, "Alt");
42
43    if (modifiers & ECORE_EVENT_MODIFIER_WIN)
44      {
45         evas_key_modifier_on(e, "Super");
46         evas_key_modifier_on(e, "Hyper");
47      }
48    else
49      {
50         evas_key_modifier_off(e, "Super");
51         evas_key_modifier_off(e, "Hyper");
52      }
53
54    if (modifiers & ECORE_EVENT_LOCK_SCROLL)
55      evas_key_lock_on(e, "Scroll_Lock");
56    else evas_key_lock_off(e, "Scroll_Lock");
57
58    if (modifiers & ECORE_EVENT_LOCK_NUM)
59      evas_key_lock_on(e, "Num_Lock");
60    else evas_key_lock_off(e, "Num_Lock");
61
62    if (modifiers & ECORE_EVENT_LOCK_CAPS)
63      evas_key_lock_on(e, "Caps_Lock");
64    else evas_key_lock_off(e, "Caps_Lock");
65 }
66
67 EAPI void
68 ecore_event_window_register(Ecore_Window id, void *window, Evas *evas, Ecore_Event_Mouse_Move_Cb move_mouse)
69 {
70    Ecore_Input_Window *w;
71
72    w = calloc(1, sizeof(Ecore_Input_Window));
73    if (!w) return;
74
75    w->evas = evas;
76    w->window = window;
77    w->move_mouse = move_mouse;
78    w->ignore_event = 0;
79
80    eina_hash_add(_window_hash, &id, w);
81
82    evas_key_modifier_add(evas, "Shift");
83    evas_key_modifier_add(evas, "Control");
84    evas_key_modifier_add(evas, "Alt");
85    evas_key_modifier_add(evas, "Meta");
86    evas_key_modifier_add(evas, "Hyper");
87    evas_key_modifier_add(evas, "Super");
88    evas_key_lock_add(evas, "Caps_Lock");
89    evas_key_lock_add(evas, "Num_Lock");
90    evas_key_lock_add(evas, "Scroll_Lock");
91 }
92
93 EAPI void
94 ecore_event_window_unregister(Ecore_Window id)
95 {
96    eina_hash_del(_window_hash, &id, NULL);
97 }
98
99 EAPI void *
100 ecore_event_window_match(Ecore_Window id)
101 {
102    Ecore_Input_Window *lookup;
103
104    lookup = eina_hash_find(_window_hash, &id);
105    if (lookup) return lookup->window;
106    return NULL;
107 }
108
109 EAPI void
110 ecore_event_window_ignore_events(Ecore_Window id, int ignore_event)
111 {
112    Ecore_Input_Window *lookup;
113
114    lookup = eina_hash_find(_window_hash, &id);
115    if (!lookup) return;
116    lookup->ignore_event = ignore_event;
117 }
118
119 static Ecore_Input_Window*
120 _ecore_event_window_match(Ecore_Window id)
121 {
122    Ecore_Input_Window *lookup;
123
124    lookup = eina_hash_find(_window_hash, &id);
125    if (!lookup) return NULL;
126    if (lookup->ignore_event) return NULL; /* Pass on event. */
127    return lookup;
128 }
129
130 static Eina_Bool
131 _ecore_event_evas_key(Ecore_Event_Key *e, Ecore_Event_Press press)
132 {
133    Ecore_Input_Window *lookup;
134
135    lookup = _ecore_event_window_match(e->window);
136    if (!lookup) return ECORE_CALLBACK_RENEW;
137    ecore_event_evas_modifier_lock_update(lookup->evas, e->modifiers);
138    if (press == ECORE_DOWN)
139      evas_event_feed_key_down(lookup->evas, e->keyname, e->key, e->string, e->compose, e->timestamp, NULL);
140    else
141      evas_event_feed_key_up(lookup->evas, e->keyname, e->key, e->string, e->compose, e->timestamp, NULL);
142    return ECORE_CALLBACK_RENEW;
143 }
144
145 static Eina_Bool
146 _ecore_event_evas_mouse_button(Ecore_Event_Mouse_Button *e, Ecore_Event_Press press)
147 {
148    Ecore_Input_Window *lookup;
149    Evas_Button_Flags flags = EVAS_BUTTON_NONE;
150
151    lookup = _ecore_event_window_match(e->window);
152    if (!lookup) return ECORE_CALLBACK_RENEW;
153    if (e->double_click) flags |= EVAS_BUTTON_DOUBLE_CLICK;
154    if (e->triple_click) flags |= EVAS_BUTTON_TRIPLE_CLICK;
155    if (e->multi.device == 0)
156      {
157         ecore_event_evas_modifier_lock_update(lookup->evas, e->modifiers);
158         if (press == ECORE_DOWN)
159           evas_event_feed_mouse_down(lookup->evas, e->buttons, flags, e->timestamp, NULL);
160         else
161           evas_event_feed_mouse_up(lookup->evas, e->buttons, flags, e->timestamp, NULL);
162      }
163    else
164      {
165         if (press == ECORE_DOWN)
166           evas_event_feed_multi_down(lookup->evas, e->multi.device, e->x, e->y, e->multi.radius, e->multi.radius_x, e->multi.radius_y, e->multi.pressure, e->multi.angle, e->multi.x, e->multi.y, flags, e->timestamp, NULL);
167         else
168           evas_event_feed_multi_up(lookup->evas, e->multi.device, e->x, e->y, e->multi.radius, e->multi.radius_x, e->multi.radius_y, e->multi.pressure, e->multi.angle, e->multi.x, e->multi.y, flags, e->timestamp, NULL);
169      }
170    return ECORE_CALLBACK_RENEW;
171 }
172
173 EAPI Eina_Bool
174 ecore_event_evas_mouse_move(void *data __UNUSED__, int type __UNUSED__, void *event)
175 {
176    Ecore_Event_Mouse_Move *e;
177    Ecore_Input_Window *lookup;
178
179    e = event;
180    lookup = _ecore_event_window_match(e->window);
181    if (!lookup) return ECORE_CALLBACK_RENEW;
182    if (e->multi.device == 0)
183      {
184         ecore_event_evas_modifier_lock_update(lookup->evas, e->modifiers);
185         lookup->move_mouse(lookup->window, e->x, e->y, e->timestamp);
186      }
187    else
188      {
189         evas_event_feed_multi_move(lookup->evas, e->multi.device, e->x, e->y, e->multi.radius, e->multi.radius_x, e->multi.radius_y, e->multi.pressure, e->multi.angle, e->multi.x, e->multi.y, e->timestamp, NULL);
190      }
191    return ECORE_CALLBACK_RENEW;
192 }
193
194 EAPI Eina_Bool
195 ecore_event_evas_mouse_button_down(void *data __UNUSED__, int type __UNUSED__, void *event)
196 {
197    return _ecore_event_evas_mouse_button((Ecore_Event_Mouse_Button *)event, ECORE_DOWN);
198 }
199
200 EAPI Eina_Bool
201 ecore_event_evas_mouse_button_up(void *data __UNUSED__, int type __UNUSED__, void *event)
202 {
203    return _ecore_event_evas_mouse_button((Ecore_Event_Mouse_Button *)event, ECORE_UP);
204 }
205
206 static Eina_Bool
207 _ecore_event_evas_mouse_io(Ecore_Event_Mouse_IO *e, Ecore_Event_IO io)
208 {
209    Ecore_Input_Window *lookup;
210
211    lookup = _ecore_event_window_match(e->window);
212    if (!lookup) return ECORE_CALLBACK_RENEW;
213    ecore_event_evas_modifier_lock_update(lookup->evas, e->modifiers);
214    switch (io)
215      {
216       case ECORE_IN:
217          evas_event_feed_mouse_in(lookup->evas, e->timestamp, NULL);
218          break;
219       case ECORE_OUT:
220          evas_event_feed_mouse_out(lookup->evas, e->timestamp, NULL);
221          break;
222       default:
223          break;
224      }
225
226    lookup->move_mouse(lookup->window, e->x, e->y, e->timestamp);
227    return ECORE_CALLBACK_RENEW;
228 }
229
230 EAPI Eina_Bool
231 ecore_event_evas_key_down(void *data __UNUSED__, int type __UNUSED__, void *event)
232 {
233    return _ecore_event_evas_key((Ecore_Event_Key *)event, ECORE_DOWN);
234 }
235
236 EAPI Eina_Bool
237 ecore_event_evas_key_up(void *data __UNUSED__, int type __UNUSED__, void *event)
238 {
239    return _ecore_event_evas_key((Ecore_Event_Key *)event, ECORE_UP);
240 }
241
242 EAPI Eina_Bool
243 ecore_event_evas_mouse_wheel(void *data __UNUSED__, int type __UNUSED__, void *event)
244 {
245    Ecore_Event_Mouse_Wheel *e;
246    Ecore_Input_Window *lookup;
247
248    e = event;
249    lookup = _ecore_event_window_match(e->window);
250    if (!lookup) return ECORE_CALLBACK_RENEW;
251    ecore_event_evas_modifier_lock_update(lookup->evas, e->modifiers);
252    evas_event_feed_mouse_wheel(lookup->evas, e->direction, e->z, e->timestamp, NULL);
253    return ECORE_CALLBACK_RENEW;
254 }
255
256 EAPI Eina_Bool
257 ecore_event_evas_mouse_in(void *data __UNUSED__, int type __UNUSED__, void *event)
258 {
259    return _ecore_event_evas_mouse_io((Ecore_Event_Mouse_IO *)event, ECORE_IN);
260 }
261
262 EAPI Eina_Bool
263 ecore_event_evas_mouse_out(void *data __UNUSED__, int type __UNUSED__, void *event)
264 {
265    return _ecore_event_evas_mouse_io((Ecore_Event_Mouse_IO *)event, ECORE_OUT);
266 }
267
268 EAPI int
269 ecore_event_evas_init(void)
270 {
271    if (++_ecore_event_evas_init_count !=  1)
272      return _ecore_event_evas_init_count;
273
274    _ecore_input_evas_log_dom = eina_log_domain_register("EcoreInputEvas",  ECORE_INPUT_EVAS_DEFAULT_LOG_COLOR);
275    if (_ecore_input_evas_log_dom < 0)
276      {
277         EINA_LOG_ERR("Impossible to create a log domain for the ecore input evas_module.");
278         return --_ecore_event_evas_init_count;
279      }
280
281    if (!ecore_init())
282      {
283         return --_ecore_event_evas_init_count;
284      }
285
286    if (!ecore_event_init())
287      {
288         goto shutdown_ecore;
289      }
290
291    ecore_event_evas_handlers[0] = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
292                                                           ecore_event_evas_key_down,
293                                                           NULL);
294    ecore_event_evas_handlers[1] = ecore_event_handler_add(ECORE_EVENT_KEY_UP,
295                                                           ecore_event_evas_key_up,
296                                                           NULL);
297    ecore_event_evas_handlers[2] = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN,
298                                                           ecore_event_evas_mouse_button_down,
299                                                           NULL);
300    ecore_event_evas_handlers[3] = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP,
301                                                           ecore_event_evas_mouse_button_up,
302                                                           NULL);
303    ecore_event_evas_handlers[4] = ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE,
304                                                           ecore_event_evas_mouse_move,
305                                                           NULL);
306    ecore_event_evas_handlers[5] = ecore_event_handler_add(ECORE_EVENT_MOUSE_WHEEL,
307                                                           ecore_event_evas_mouse_wheel,
308                                                           NULL);
309    ecore_event_evas_handlers[6] = ecore_event_handler_add(ECORE_EVENT_MOUSE_IN,
310                                                           ecore_event_evas_mouse_in,
311                                                           NULL);
312    ecore_event_evas_handlers[7] = ecore_event_handler_add(ECORE_EVENT_MOUSE_OUT,
313                                                           ecore_event_evas_mouse_out,
314                                                           NULL);
315    
316    _window_hash = eina_hash_pointer_new(free);
317    
318    return _ecore_event_evas_init_count;
319    
320    shutdown_ecore:
321    ecore_shutdown();
322    
323    return --_ecore_event_evas_init_count;
324 }
325
326 EAPI int
327 ecore_event_evas_shutdown(void)
328 {
329    size_t i;
330    
331    if (--_ecore_event_evas_init_count != 0)
332      return _ecore_event_evas_init_count;
333    
334    eina_hash_free(_window_hash);
335    _window_hash = NULL;
336    for (i = 0; i < sizeof(ecore_event_evas_handlers) / sizeof(Ecore_Event_Handler *); i++)
337      {
338         ecore_event_handler_del(ecore_event_evas_handlers[i]);
339         ecore_event_evas_handlers[i] = NULL;
340      }
341    
342    ecore_event_shutdown();
343    ecore_shutdown();
344    
345    eina_log_domain_unregister(_ecore_input_evas_log_dom);
346    _ecore_input_evas_log_dom = -1;
347    
348    return _ecore_event_evas_init_count;
349 }