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("EcoreSdl", ECORE_SDL_DEFAULT_LOG_COLOR);
71 if(_ecore_sdl_log_dom < 0)
73 EINA_LOG_ERR("Impossible to create a log domain for the Ecore SDL module.");
74 return --_ecore_sdl_init_count;
76 if (!ecore_event_init())
77 return --_ecore_sdl_init_count;
79 ECORE_SDL_EVENT_GOT_FOCUS = ecore_event_type_new();
80 ECORE_SDL_EVENT_LOST_FOCUS = ecore_event_type_new();
81 ECORE_SDL_EVENT_RESIZE = ecore_event_type_new();
82 ECORE_SDL_EVENT_EXPOSE = ecore_event_type_new();
84 SDL_EnableKeyRepeat(200, 100);
86 return _ecore_sdl_init_count;
90 * Shuts down the Ecore_Sdl library.
91 * @return @c The number of times the system has been initialised without
93 * @ingroup Ecore_SDL_Library_Group
96 ecore_sdl_shutdown(void)
98 if (--_ecore_sdl_init_count != 0);
99 return _ecore_sdl_init_count;
101 ecore_event_shutdown();
102 eina_log_domain_unregister(_ecore_sdl_log_dom);
103 _ecore_sdl_log_dom = -1;
104 return _ecore_sdl_init_count;
108 _ecore_sdl_event_modifiers(int mod)
110 unsigned int modifiers = 0;
112 if(mod & KMOD_LSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
113 if(mod & KMOD_RSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
114 if(mod & KMOD_LCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
115 if(mod & KMOD_RCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
116 if(mod & KMOD_LALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
117 if(mod & KMOD_RALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
118 if(mod & KMOD_NUM) modifiers |= ECORE_EVENT_LOCK_NUM;
119 if(mod & KMOD_CAPS) modifiers |= ECORE_EVENT_LOCK_CAPS;
124 static Ecore_Event_Key*
125 _ecore_sdl_event_key(SDL_Event *event, double time)
130 ev = malloc(sizeof(Ecore_Event_Key));
131 if (!ev) return NULL;
133 ev->timestamp = time;
135 ev->modifiers = _ecore_sdl_event_modifiers(SDL_GetModState());
139 for (i = 0; i < sizeof(keystable) / sizeof(struct _ecore_sdl_keys_s); ++i)
140 if (keystable[i].code == event->key.keysym.sym)
142 ev->keyname = keystable[i].name;
143 ev->string = keystable[i].compose;
153 ecore_sdl_feed_events(void)
158 while(SDL_PollEvent(&event))
160 time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);
163 case SDL_MOUSEMOTION:
165 Ecore_Event_Mouse_Move *ev;
167 ev = malloc(sizeof(Ecore_Event_Mouse_Move));
170 ev->timestamp = time;
172 ev->modifiers = 0; /* FIXME: keep modifier around. */
173 ev->x = event.motion.x;
174 ev->y = event.motion.y;
178 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
181 case SDL_MOUSEBUTTONDOWN:
183 if (event.button.button == SDL_BUTTON_WHEELUP ||
184 event.button.button == SDL_BUTTON_WHEELDOWN)
186 Ecore_Event_Mouse_Wheel *ev;
188 ev = malloc(sizeof(Ecore_Event_Mouse_Wheel));
191 ev->timestamp = time;
193 ev->modifiers = 0; /* FIXME: keep modifier around. */
195 ev->z = event.button.button == SDL_BUTTON_WHEELDOWN ? -1 : 1;
197 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
201 Ecore_Event_Mouse_Button *ev;
203 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
206 ev->timestamp = time;
208 ev->modifiers = 0; /* FIXME: keep modifier around. */
209 ev->buttons = event.button.button;
210 ev->double_click = 0;
211 ev->triple_click = 0;
213 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
217 case SDL_MOUSEBUTTONUP:
219 Ecore_Event_Mouse_Button *ev;
221 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
223 ev->timestamp = time;
225 ev->modifiers = 0; /* FIXME: keep modifier around. */
226 ev->buttons = event.button.button;
227 ev->double_click = 0;
228 ev->triple_click = 0;
230 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
233 case SDL_VIDEORESIZE:
235 Ecore_Sdl_Event_Video_Resize *ev;
237 ev = malloc(sizeof (Ecore_Sdl_Event_Video_Resize));
238 ev->w = event.resize.w;
239 ev->h = event.resize.h;
241 ecore_event_add(ECORE_SDL_EVENT_RESIZE, ev, NULL, NULL);
244 case SDL_VIDEOEXPOSE:
245 ecore_event_add(ECORE_SDL_EVENT_EXPOSE, NULL, NULL, NULL);
248 ecore_main_loop_quit();
253 Ecore_SDL_Pressed *entry;
256 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
257 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
260 ev = _ecore_sdl_event_key(&event, time);
261 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
264 ev = _ecore_sdl_event_key(&event, time);
265 if (ev) ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
269 entry = malloc(sizeof (Ecore_SDL_Pressed));
272 entry->key = event.key.keysym.sym;
274 repeat = eina_rbtree_inline_insert(repeat, EINA_RBTREE_GET(entry),
275 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
282 Ecore_SDL_Pressed *entry;
284 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
285 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
288 repeat = eina_rbtree_inline_remove(repeat, EINA_RBTREE_GET(entry),
289 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
293 ev = _ecore_sdl_event_key(&event, time);
294 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
297 case SDL_ACTIVEEVENT:
298 /* FIXME: Focus gain. */
302 case SDL_JOYAXISMOTION:
303 case SDL_JOYBALLMOTION:
304 case SDL_JOYHATMOTION:
305 case SDL_JOYBUTTONDOWN:
306 case SDL_JOYBUTTONUP: