9 #include "Ecore_Input.h"
11 #include "ecore_sdl_private.h"
12 #include "ecore_private.h"
13 #include "Ecore_Sdl_Keys.h"
15 #include <eina_rbtree.h>
17 int _ecore_sdl_log_dom = -1;
19 typedef struct _Ecore_SDL_Pressed Ecore_SDL_Pressed;
20 struct _Ecore_SDL_Pressed
27 EAPI int ECORE_SDL_EVENT_GOT_FOCUS = 0;
28 EAPI int ECORE_SDL_EVENT_LOST_FOCUS = 0;
29 EAPI int ECORE_SDL_EVENT_RESIZE = 0;
30 EAPI int ECORE_SDL_EVENT_EXPOSE = 0;
32 static int _ecore_sdl_init_count = 0;
33 static Eina_Rbtree *repeat = NULL;
35 static Eina_Rbtree_Direction
36 _ecore_sdl_pressed_key(const Ecore_SDL_Pressed *left,
37 const Ecore_SDL_Pressed *right,
38 __UNUSED__ void *data)
40 return left->key < right->key ? EINA_RBTREE_LEFT : EINA_RBTREE_RIGHT;
44 _ecore_sdl_pressed_node(const Ecore_SDL_Pressed *node,
46 __UNUSED__ int length,
47 __UNUSED__ void *data)
49 return node->key - *key;
53 * @defgroup Ecore_Sdl_Library_Group Framebuffer Library Functions
55 * Functions used to set up and shut down the Ecore_Framebuffer functions.
59 * Sets up the Ecore_Sdl library.
60 * @param name device target name
61 * @return @c 0 on failure. Otherwise, the number of times the library has
62 * been initialised without being shut down.
63 * @ingroup Ecore_SDL_Library_Group
66 ecore_sdl_init(const char *name __UNUSED__)
68 if(++_ecore_sdl_init_count != 1)
69 return _ecore_sdl_init_count;
70 _ecore_sdl_log_dom = eina_log_domain_register
71 ("ecore_sdl", ECORE_SDL_DEFAULT_LOG_COLOR);
72 if(_ecore_sdl_log_dom < 0)
74 EINA_LOG_ERR("Impossible to create a log domain for the Ecore SDL module.");
75 return --_ecore_sdl_init_count;
77 if (!ecore_event_init())
78 return --_ecore_sdl_init_count;
80 ECORE_SDL_EVENT_GOT_FOCUS = ecore_event_type_new();
81 ECORE_SDL_EVENT_LOST_FOCUS = ecore_event_type_new();
82 ECORE_SDL_EVENT_RESIZE = ecore_event_type_new();
83 ECORE_SDL_EVENT_EXPOSE = ecore_event_type_new();
85 SDL_EnableKeyRepeat(200, 100);
87 return _ecore_sdl_init_count;
91 * Shuts down the Ecore_Sdl library.
92 * @return @c The number of times the system has been initialised without
94 * @ingroup Ecore_SDL_Library_Group
97 ecore_sdl_shutdown(void)
99 if (--_ecore_sdl_init_count != 0);
100 return _ecore_sdl_init_count;
102 ecore_event_shutdown();
103 eina_log_domain_unregister(_ecore_sdl_log_dom);
104 _ecore_sdl_log_dom = -1;
105 return _ecore_sdl_init_count;
109 _ecore_sdl_event_modifiers(int mod)
111 unsigned int modifiers = 0;
113 if(mod & KMOD_LSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
114 if(mod & KMOD_RSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
115 if(mod & KMOD_LCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
116 if(mod & KMOD_RCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
117 if(mod & KMOD_LALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
118 if(mod & KMOD_RALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
119 if(mod & KMOD_NUM) modifiers |= ECORE_EVENT_LOCK_NUM;
120 if(mod & KMOD_CAPS) modifiers |= ECORE_EVENT_LOCK_CAPS;
125 static Ecore_Event_Key*
126 _ecore_sdl_event_key(SDL_Event *event, double time)
131 ev = malloc(sizeof(Ecore_Event_Key));
132 if (!ev) return NULL;
134 ev->timestamp = time;
136 ev->modifiers = _ecore_sdl_event_modifiers(SDL_GetModState());
140 for (i = 0; i < sizeof(keystable) / sizeof(struct _ecore_sdl_keys_s); ++i)
141 if (keystable[i].code == event->key.keysym.sym)
143 ev->keyname = keystable[i].name;
144 ev->string = keystable[i].compose;
154 ecore_sdl_feed_events(void)
159 while(SDL_PollEvent(&event))
161 time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);
164 case SDL_MOUSEMOTION:
166 Ecore_Event_Mouse_Move *ev;
168 ev = malloc(sizeof(Ecore_Event_Mouse_Move));
171 ev->timestamp = time;
173 ev->modifiers = 0; /* FIXME: keep modifier around. */
174 ev->x = event.motion.x;
175 ev->y = event.motion.y;
179 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
182 case SDL_MOUSEBUTTONDOWN:
184 if (event.button.button == SDL_BUTTON_WHEELUP ||
185 event.button.button == SDL_BUTTON_WHEELDOWN)
187 Ecore_Event_Mouse_Wheel *ev;
189 ev = malloc(sizeof(Ecore_Event_Mouse_Wheel));
192 ev->timestamp = time;
194 ev->modifiers = 0; /* FIXME: keep modifier around. */
196 ev->z = event.button.button == SDL_BUTTON_WHEELDOWN ? -1 : 1;
198 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
202 Ecore_Event_Mouse_Button *ev;
204 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
207 ev->timestamp = time;
209 ev->modifiers = 0; /* FIXME: keep modifier around. */
210 ev->buttons = event.button.button;
211 ev->double_click = 0;
212 ev->triple_click = 0;
214 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
218 case SDL_MOUSEBUTTONUP:
220 Ecore_Event_Mouse_Button *ev;
222 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
224 ev->timestamp = time;
226 ev->modifiers = 0; /* FIXME: keep modifier around. */
227 ev->buttons = event.button.button;
228 ev->double_click = 0;
229 ev->triple_click = 0;
231 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
234 case SDL_VIDEORESIZE:
236 Ecore_Sdl_Event_Video_Resize *ev;
238 ev = malloc(sizeof (Ecore_Sdl_Event_Video_Resize));
239 ev->w = event.resize.w;
240 ev->h = event.resize.h;
242 ecore_event_add(ECORE_SDL_EVENT_RESIZE, ev, NULL, NULL);
245 case SDL_VIDEOEXPOSE:
246 ecore_event_add(ECORE_SDL_EVENT_EXPOSE, NULL, NULL, NULL);
249 ecore_main_loop_quit();
254 Ecore_SDL_Pressed *entry;
257 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
258 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
261 ev = _ecore_sdl_event_key(&event, time);
262 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
265 ev = _ecore_sdl_event_key(&event, time);
266 if (ev) ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
270 entry = malloc(sizeof (Ecore_SDL_Pressed));
273 entry->key = event.key.keysym.sym;
275 repeat = eina_rbtree_inline_insert(repeat, EINA_RBTREE_GET(entry),
276 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
283 Ecore_SDL_Pressed *entry;
285 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
286 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
289 repeat = eina_rbtree_inline_remove(repeat, EINA_RBTREE_GET(entry),
290 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
294 ev = _ecore_sdl_event_key(&event, time);
295 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
298 case SDL_ACTIVEEVENT:
299 /* FIXME: Focus gain. */
303 case SDL_JOYAXISMOTION:
304 case SDL_JOYBALLMOTION:
305 case SDL_JOYHATMOTION:
306 case SDL_JOYBUTTONDOWN:
307 case SDL_JOYBUTTONUP: