2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
12 #include "Ecore_Sdl.h"
13 #include "Ecore_Input.h"
15 #include "ecore_sdl_private.h"
16 #include "ecore_private.h"
17 #include "Ecore_Sdl_Keys.h"
19 #include <eina_rbtree.h>
21 int _ecore_sdl_log_dom = -1;
23 typedef struct _Ecore_SDL_Pressed Ecore_SDL_Pressed;
24 struct _Ecore_SDL_Pressed
31 EAPI int ECORE_SDL_EVENT_GOT_FOCUS = 0;
32 EAPI int ECORE_SDL_EVENT_LOST_FOCUS = 0;
33 EAPI int ECORE_SDL_EVENT_RESIZE = 0;
34 EAPI int ECORE_SDL_EVENT_EXPOSE = 0;
36 static int _ecore_sdl_init_count = 0;
37 static Eina_Rbtree *repeat = NULL;
39 static Eina_Rbtree_Direction
40 _ecore_sdl_pressed_key(const Ecore_SDL_Pressed *left,
41 const Ecore_SDL_Pressed *right,
42 __UNUSED__ void *data)
44 return left->key < right->key ? EINA_RBTREE_LEFT : EINA_RBTREE_RIGHT;
48 _ecore_sdl_pressed_node(const Ecore_SDL_Pressed *node,
50 __UNUSED__ int length,
51 __UNUSED__ void *data)
53 return node->key - *key;
57 * @defgroup Ecore_Sdl_Library_Group Framebuffer Library Functions
59 * Functions used to set up and shut down the Ecore_Framebuffer functions.
63 * Sets up the Ecore_Sdl library.
64 * @param name device target name
65 * @return @c 0 on failure. Otherwise, the number of times the library has
66 * been initialised without being shut down.
67 * @ingroup Ecore_SDL_Library_Group
70 ecore_sdl_init(const char *name __UNUSED__)
72 if(++_ecore_sdl_init_count != 1)
73 return _ecore_sdl_init_count;
74 _ecore_sdl_log_dom = eina_log_domain_register("EcoreSdl", ECORE_SDL_DEFAULT_LOG_COLOR);
75 if(_ecore_sdl_log_dom < 0)
77 EINA_LOG_ERR("Impossible to create a log domain for the Ecore SDL module.");
78 return --_ecore_sdl_init_count;
80 if (!ecore_event_init())
81 return --_ecore_sdl_init_count;
83 ECORE_SDL_EVENT_GOT_FOCUS = ecore_event_type_new();
84 ECORE_SDL_EVENT_LOST_FOCUS = ecore_event_type_new();
85 ECORE_SDL_EVENT_RESIZE = ecore_event_type_new();
86 ECORE_SDL_EVENT_EXPOSE = ecore_event_type_new();
88 SDL_EnableKeyRepeat(200, 100);
90 return _ecore_sdl_init_count;
94 * Shuts down the Ecore_Sdl library.
95 * @return @c The number of times the system has been initialised without
97 * @ingroup Ecore_SDL_Library_Group
100 ecore_sdl_shutdown(void)
102 if (--_ecore_sdl_init_count != 0);
103 return _ecore_sdl_init_count;
105 ecore_event_shutdown();
106 eina_log_domain_unregister(_ecore_sdl_log_dom);
107 _ecore_sdl_log_dom = -1;
108 return _ecore_sdl_init_count;
112 _ecore_sdl_event_modifiers(int mod)
114 unsigned int modifiers = 0;
116 if(mod & KMOD_LSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
117 if(mod & KMOD_RSHIFT) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
118 if(mod & KMOD_LCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
119 if(mod & KMOD_RCTRL) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
120 if(mod & KMOD_LALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
121 if(mod & KMOD_RALT) modifiers |= ECORE_EVENT_MODIFIER_ALT;
122 if(mod & KMOD_NUM) modifiers |= ECORE_EVENT_LOCK_NUM;
123 if(mod & KMOD_CAPS) modifiers |= ECORE_EVENT_LOCK_CAPS;
128 static Ecore_Event_Key*
129 _ecore_sdl_event_key(SDL_Event *event, double time)
134 ev = malloc(sizeof(Ecore_Event_Key));
135 if (!ev) return NULL;
137 ev->timestamp = time;
139 ev->modifiers = _ecore_sdl_event_modifiers(SDL_GetModState());
143 for (i = 0; i < sizeof(keystable) / sizeof(struct _ecore_sdl_keys_s); ++i)
144 if (keystable[i].code == event->key.keysym.sym)
146 ev->keyname = keystable[i].name;
147 ev->string = keystable[i].compose;
157 ecore_sdl_feed_events(void)
162 while(SDL_PollEvent(&event))
164 time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);
167 case SDL_MOUSEMOTION:
169 Ecore_Event_Mouse_Move *ev;
171 ev = malloc(sizeof(Ecore_Event_Mouse_Move));
174 ev->timestamp = time;
176 ev->modifiers = 0; /* FIXME: keep modifier around. */
177 ev->x = event.motion.x;
178 ev->y = event.motion.y;
182 ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
185 case SDL_MOUSEBUTTONDOWN:
187 if (event.button.button == SDL_BUTTON_WHEELUP ||
188 event.button.button == SDL_BUTTON_WHEELDOWN)
190 Ecore_Event_Mouse_Wheel *ev;
192 ev = malloc(sizeof(Ecore_Event_Mouse_Wheel));
195 ev->timestamp = time;
197 ev->modifiers = 0; /* FIXME: keep modifier around. */
199 ev->z = event.button.button == SDL_BUTTON_WHEELDOWN ? -1 : 1;
201 ecore_event_add(ECORE_EVENT_MOUSE_WHEEL, ev, NULL, NULL);
205 Ecore_Event_Mouse_Button *ev;
207 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
210 ev->timestamp = time;
212 ev->modifiers = 0; /* FIXME: keep modifier around. */
213 ev->buttons = event.button.button;
214 ev->double_click = 0;
215 ev->triple_click = 0;
217 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
221 case SDL_MOUSEBUTTONUP:
223 Ecore_Event_Mouse_Button *ev;
225 ev = malloc(sizeof(Ecore_Event_Mouse_Button));
227 ev->timestamp = time;
229 ev->modifiers = 0; /* FIXME: keep modifier around. */
230 ev->buttons = event.button.button;
231 ev->double_click = 0;
232 ev->triple_click = 0;
234 ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
237 case SDL_VIDEORESIZE:
239 Ecore_Sdl_Event_Video_Resize *ev;
241 ev = malloc(sizeof (Ecore_Sdl_Event_Video_Resize));
242 ev->w = event.resize.w;
243 ev->h = event.resize.h;
245 ecore_event_add(ECORE_SDL_EVENT_RESIZE, ev, NULL, NULL);
248 case SDL_VIDEOEXPOSE:
249 ecore_event_add(ECORE_SDL_EVENT_EXPOSE, NULL, NULL, NULL);
252 ecore_main_loop_quit();
257 Ecore_SDL_Pressed *entry;
260 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
261 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
264 ev = _ecore_sdl_event_key(&event, time);
265 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
268 ev = _ecore_sdl_event_key(&event, time);
269 if (ev) ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
273 entry = malloc(sizeof (Ecore_SDL_Pressed));
276 entry->key = event.key.keysym.sym;
278 repeat = eina_rbtree_inline_insert(repeat, EINA_RBTREE_GET(entry),
279 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
286 Ecore_SDL_Pressed *entry;
288 entry = (Ecore_SDL_Pressed*) eina_rbtree_inline_lookup(repeat, &event.key.keysym.sym, sizeof (event.key.keysym.sym),
289 EINA_RBTREE_CMP_KEY_CB(_ecore_sdl_pressed_node), NULL);
292 repeat = eina_rbtree_inline_remove(repeat, EINA_RBTREE_GET(entry),
293 EINA_RBTREE_CMP_NODE_CB(_ecore_sdl_pressed_key), NULL);
297 ev = _ecore_sdl_event_key(&event, time);
298 if (ev) ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
301 case SDL_ACTIVEEVENT:
302 /* FIXME: Focus gain. */
306 case SDL_JOYAXISMOTION:
307 case SDL_JOYBALLMOTION:
308 case SDL_JOYHATMOTION:
309 case SDL_JOYBUTTONDOWN:
310 case SDL_JOYBUTTONUP: