[SDL_Tizen] Reduce CPU usage
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenevents.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4   Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
5
6   This software is provided 'as-is', without any express or implied
7   warranty.  In no event will the authors be held liable for any damages
8   arising from the use of this software.
9
10   Permission is granted to anyone to use this software for any purpose,
11   including commercial applications, and to alter it and redistribute it
12   freely, subject to the following restrictions:
13
14   1. The origin of this software must not be misrepresented; you must not
15      claim that you wrote the original software. If you use this software
16      in a product, an acknowledgment in the product documentation would be
17      appreciated but is not required.
18   2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20   3. This notice may not be removed or altered from any source distribution.
21 */
22
23 #include "../../SDL_internal.h"
24
25 #if SDL_VIDEO_DRIVER_TIZEN
26
27 #include <unistd.h>
28 #include "SDL_stdinc.h"
29 #include "SDL_assert.h"
30 #include "SDL_log.h"
31
32 #include "SDL_tizenevents_c.h"
33 #include "SDL_tizenkeyboard.h"
34
35 #include "../../events/SDL_sysevents.h"
36 #include "../../events/SDL_events_c.h"
37 #include "../../events/scancodes_xfree86.h"
38 #include "../../events/scancodes_tizen.h"
39
40 extern TizenKeyboard tizen_keyboard;
41
42 static SDL_Scancode
43 TranslateKeycode(int keycode)
44 {
45     SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
46
47     if (keycode < SDL_arraysize(Tizen_Keycodes)) {
48         scancode = Tizen_Keycodes[keycode];
49     }
50     if (scancode == SDL_SCANCODE_UNKNOWN) {
51         SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[SDL] Unknown keycode %d",keycode);
52     }
53     return scancode;
54 }
55
56 void
57 Tizen_PumpEvents(_THIS)
58 {
59     ecore_main_loop_iterate_may_block(0);
60     usleep(0);
61 }
62
63 Ecore_IMF_Keyboard_Modifiers EcoreInputModifierToEcoreIMFModifier(unsigned int ecoreModifier)
64 {
65    int modifier =  ECORE_IMF_KEYBOARD_MODIFIER_NONE;  // If no other matches returns NONE.
66
67
68    if ( ecoreModifier & ECORE_EVENT_MODIFIER_SHIFT )  // enums from ecore_input/Ecore_Input.h
69    {
70      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_SHIFT;  // enums from ecore_imf/ecore_imf.h
71    }
72
73    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALT )
74    {
75      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALT;
76    }
77
78    if ( ecoreModifier & ECORE_EVENT_MODIFIER_CTRL )
79    {
80      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_CTRL;
81    }
82
83    if ( ecoreModifier & ECORE_EVENT_MODIFIER_WIN )
84    {
85      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_WIN;
86    }
87
88    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALTGR )
89    {
90      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALTGR;
91    }
92
93    return (Ecore_IMF_Keyboard_Modifiers)modifier;
94 }
95
96 Eina_Bool
97 _tizen_cb_event_keyup_change(void *data, int type, void *event)
98 {
99     if (!event) return ECORE_CALLBACK_PASS_ON;
100
101     Ecore_Event_Key * e = event;
102     SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
103     if (!e->key) return ECORE_CALLBACK_PASS_ON;
104
105     if (tizen_keyboard.imf_context) {
106         Ecore_IMF_Event_Key_Up ecore_ev;
107
108         ecore_ev.keyname   = e->keyname;
109         ecore_ev.key       = e->key;
110         ecore_ev.string    = e->string;
111         ecore_ev.compose   = e->compose;
112         ecore_ev.timestamp = e->timestamp;
113         ecore_ev.modifiers = EcoreInputModifierToEcoreIMFModifier ( e->modifiers );
114         ecore_ev.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
115
116         if (ecore_imf_context_filter_event(tizen_keyboard.imf_context,
117                                            ECORE_IMF_EVENT_KEY_UP,
118                                            (Ecore_IMF_Event *)&ecore_ev)) {
119             return ECORE_CALLBACK_PASS_ON;
120         }
121     }
122
123     if (SDL_GetEventState(SDL_TEXTINPUT)) {
124         if (!strcmp(e->key, "BackSpace")) {
125             scancode = SDL_GetScancodeFromName(e->key);
126             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
127         }
128         else if (!strcmp(e->key, "Delete") ||
129             (!strcmp(e->key, "KP_Delete") && !e->string)) {
130             scancode = SDL_GetScancodeFromName(e->key);
131             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
132         }
133         else if ((!strcmp(e->key, "Return")) || (!strcmp(e->key, "KP_Enter"))) {
134             scancode = SDL_GetScancodeFromName(e->key);
135             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
136         }
137         else if (e->string) {
138             // Nothing
139         }
140         else {
141             scancode = TranslateKeycode(e->keycode);
142             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
143         }
144     } else {
145         scancode = TranslateKeycode(e->keycode);
146         SDL_SendKeyboardKey(SDL_RELEASED, scancode);
147     }
148
149     return ECORE_CALLBACK_PASS_ON;
150 }
151
152 Eina_Bool
153 _tizen_cb_event_keydown_change(void *data, int type, void *event)
154 {
155     if (!event) return ECORE_CALLBACK_PASS_ON;
156
157     Ecore_Event_Key * e = event;
158     SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
159     if (!e->key) return ECORE_CALLBACK_PASS_ON;
160
161     if (tizen_keyboard.imf_context) {
162         Ecore_IMF_Event_Key_Down ecore_ev;
163
164         ecore_ev.keyname   = e->keyname;
165         ecore_ev.key       = e->key;
166         ecore_ev.string    = e->string;
167         ecore_ev.compose   = e->compose;
168         ecore_ev.timestamp = e->timestamp;
169         ecore_ev.modifiers = EcoreInputModifierToEcoreIMFModifier ( e->modifiers );
170         ecore_ev.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
171
172         if (ecore_imf_context_filter_event(tizen_keyboard.imf_context,
173                                            ECORE_IMF_EVENT_KEY_DOWN,
174                                            (Ecore_IMF_Event *)&ecore_ev)) {
175             return ECORE_CALLBACK_PASS_ON;
176         }
177     }
178
179     if (SDL_GetEventState(SDL_TEXTINPUT)) {
180         if (!strcmp(e->key, "BackSpace")) {
181             scancode = SDL_GetScancodeFromName(e->key);
182             SDL_SendKeyboardKey(SDL_PRESSED, scancode);
183         }
184         else if (!strcmp(e->key, "Delete") ||
185             (!strcmp(e->key, "KP_Delete") && !e->string)) {
186             scancode = SDL_GetScancodeFromName(e->key);
187             SDL_SendKeyboardKey(SDL_PRESSED, scancode);
188         }
189         else if ((!strcmp(e->key, "Return")) || (!strcmp(e->key, "KP_Enter"))) {
190             scancode = SDL_GetScancodeFromName(e->key);
191             SDL_SendKeyboardKey(SDL_PRESSED, scancode);
192         }
193         else if (e->string) {
194             SDL_SendKeyboardText(e->string);
195         }
196         else {
197             scancode = TranslateKeycode(e->keycode);
198             SDL_SendKeyboardKey(SDL_PRESSED, scancode);
199         }
200     } else {
201         scancode = TranslateKeycode(e->keycode);
202         SDL_SendKeyboardKey(SDL_PRESSED, scancode);
203     }
204
205     return ECORE_CALLBACK_PASS_ON;
206 }
207
208 Eina_Bool
209 _tizen_cb_event_focus_in(void *data, int type EINA_UNUSED, void *event)
210 {
211     SDL_VideoDevice *_this = SDL_GetVideoDevice();
212     Ecore_Wl_Event_Focus_In *ev;
213     Ecore_Wl_Window *ew;
214     SDL_Window *window;
215
216     ev = event;
217     ew = ecore_wl_window_find(ev->win);
218     window = Tizen_FindWindow(_this, ew);
219
220     SDL_SetKeyboardFocus(window);
221     return ECORE_CALLBACK_PASS_ON;
222 }
223
224 Eina_Bool
225 _tizen_cb_event_focus_out(void *data, int type EINA_UNUSED, void *event)
226 {
227     SDL_SetKeyboardFocus(NULL);
228     return ECORE_CALLBACK_PASS_ON;
229 }
230 #endif /* SDL_VIDEO_DRIVER_TIZEN */
231
232 /* vi: set ts=4 sw=4 expandtab: */