[SDL_TIZEN] code clean up
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenkeyboard.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 #include "SDL_tizenkeyboard.h"
26 #include "SDL_log.h"
27 #include "../../events/SDL_keyboard_c.h"
28
29 /* Tizen Keyboard */
30 TizenKeyboard tizen_keyboard;
31
32 void
33 _ecore_imf_event_commit_cb(void *data, Ecore_IMF_Context *ctx EINA_UNUSED, void *event_info)
34 {
35     char *commit_str = (char *)event_info;
36     SDL_SendKeyboardText(commit_str);
37     return;
38 }
39
40 void
41 _ecore_imf_event_preedit_changed_cb(void *data, Ecore_IMF_Context *ctx, void *event_info EINA_UNUSED)
42 {
43     char *preedit_string;
44     int cursor_pos;
45     Ecore_IMF_Context *imf_context = ctx;
46
47     // get preedit string and attributes
48     ecore_imf_context_preedit_string_with_attributes_get(imf_context, &preedit_string, NULL, &cursor_pos);
49
50     SDL_SendEditingText(preedit_string, 0, cursor_pos);
51     free(preedit_string);
52 }
53
54 void
55 _ecore_imf_event_state_change_cb(void *data, Ecore_IMF_Context *ctx EINA_UNUSED, int value)
56 {
57     //ECORE_IMF_INPUT_PANEL_STATE_SHOW : 0
58     //ECORE_IMF_INPUT_PANEL_STATE_HIDE : 1
59     return;
60 }
61
62 void Tizen_SetKeymap()
63 {
64     SDL_Keycode keymap[SDL_NUM_SCANCODES];
65
66     /* Add default scancode to key mapping */
67     SDL_GetDefaultKeymap(keymap);
68     SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
69 }
70
71 void Tizen_InitKeyboard(_THIS)
72 {
73     //TODO : need the ref count
74     if (tizen_keyboard.imf_context) return;
75
76     ecore_imf_init();
77
78     memset(&tizen_keyboard, 0, sizeof(tizen_keyboard));
79
80     const char *default_id = ecore_imf_context_default_id_get();
81     if (!default_id)
82       {
83          SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Can't create ecore_imf_context\n");
84          return;
85       }
86
87     tizen_keyboard.imf_context = ecore_imf_context_add(default_id);
88
89     SDL_Window *window = _this->windows;
90     SDL_WindowData *wind = window->driverdata;
91
92     ecore_imf_context_client_window_set(tizen_keyboard.imf_context, (void*)wind->id);
93
94     ecore_imf_context_event_callback_add(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_COMMIT , _ecore_imf_event_commit_cb, NULL);
95
96     ecore_imf_context_event_callback_add(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, _ecore_imf_event_preedit_changed_cb, NULL);
97
98     ecore_imf_context_input_panel_event_callback_add (tizen_keyboard.imf_context, ECORE_IMF_INPUT_PANEL_STATE_EVENT, _ecore_imf_event_state_change_cb, NULL);
99
100     ecore_imf_context_cursor_position_set(tizen_keyboard.imf_context, 0);
101     ecore_imf_context_focus_out(tizen_keyboard.imf_context);
102     ecore_imf_context_input_panel_hide(tizen_keyboard.imf_context);
103
104     Tizen_SetKeymap();
105
106 }
107
108 void Tizen_FiniKeyboard(void)
109 {
110     if(tizen_keyboard.imf_context == NULL)
111         return;
112
113     ecore_imf_context_del(tizen_keyboard.imf_context);
114     tizen_keyboard.imf_context = NULL;
115
116     ecore_imf_shutdown();
117 }
118
119 void
120 Tizen_StartTextInput(_THIS)
121 {
122     SDL_Window *window;
123
124     if (!_this) {
125         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Video subsystem must be initialized");
126         return;
127     }
128
129     if(tizen_keyboard.imf_context == NULL)
130       Tizen_InitKeyboard(_this);
131
132     window = SDL_GetKeyboardFocus();
133     if(!window){
134        SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Keyboard focused window is NULL");
135        return;
136     }
137     Tizen_ShowScreenKeyboard(_this, window);
138 }
139
140
141 void
142 Tizen_StopTextInput(_THIS)
143 {
144    if (!_this) return;
145    if (tizen_keyboard.imf_context)
146      {
147         Tizen_HideScreenKeyboard(_this, _this->windows);
148      }
149 }
150
151 void Tizen_SetTextInputRect(void)
152 {
153 }
154
155
156 SDL_bool
157 Tizen_HasScreenKeyboardSupport(_THIS)
158 {
159     return SDL_TRUE;
160 }
161
162
163 void
164 Tizen_ShowScreenKeyboard(_THIS, SDL_Window * window)
165 {
166     SDL_WindowData *wind = window->driverdata;
167     if (!tizen_keyboard.imf_context)
168           return;
169
170     ecore_imf_context_focus_in(tizen_keyboard.imf_context);
171     ecore_imf_context_input_panel_show(tizen_keyboard.imf_context);
172     ecore_imf_context_client_window_set(tizen_keyboard.imf_context, (void*)wind->id);
173 }
174
175 void
176 Tizen_HideScreenKeyboard(_THIS, SDL_Window * window)
177 {
178     if (!tizen_keyboard.imf_context)
179           return;
180
181     ecore_imf_context_focus_out(tizen_keyboard.imf_context);
182     ecore_imf_context_input_panel_hide(tizen_keyboard.imf_context);
183 }
184
185 SDL_bool
186 Tizen_IsScreenKeyboardShown(_THIS, SDL_Window * window)
187 {
188     if (!tizen_keyboard.imf_context)
189           return SDL_FALSE;
190     //EAPI Ecore_IMF_Input_Panel_State  ecore_imf_context_input_panel_state_get (Ecore_IMF_Context *ctx)
191     return ecore_imf_context_input_panel_state_get(tizen_keyboard.imf_context);
192 }