[SDL_Tizen] delete ecore_imf_callback
[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     ecore_imf_context_event_callback_add(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, _ecore_imf_event_preedit_changed_cb, NULL);
96     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);
97
98     ecore_imf_context_cursor_position_set(tizen_keyboard.imf_context, 0);
99     ecore_imf_context_focus_out(tizen_keyboard.imf_context);
100     ecore_imf_context_input_panel_hide(tizen_keyboard.imf_context);
101
102     Tizen_SetKeymap();
103
104 }
105
106 void Tizen_FiniKeyboard(void)
107 {
108     if(tizen_keyboard.imf_context == NULL)
109         return;
110
111     ecore_imf_context_event_callback_del(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_COMMIT, _ecore_imf_event_commit_cb);
112     ecore_imf_context_event_callback_del(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, _ecore_imf_event_preedit_changed_cb);
113     ecore_imf_context_input_panel_event_callback_del(tizen_keyboard.imf_context, ECORE_IMF_CALLBACK_PREEDIT_CHANGED, _ecore_imf_event_preedit_changed_cb);
114
115     ecore_imf_context_del(tizen_keyboard.imf_context);
116     tizen_keyboard.imf_context = NULL;
117
118     ecore_imf_shutdown();
119 }
120
121 void
122 Tizen_StartTextInput(_THIS)
123 {
124     SDL_Window *window;
125
126     if (!_this) {
127         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Video subsystem must be initialized");
128         return;
129     }
130
131     if(tizen_keyboard.imf_context == NULL)
132       Tizen_InitKeyboard(_this);
133
134     window = SDL_GetKeyboardFocus();
135     if(!window){
136        SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Keyboard focused window is NULL");
137        return;
138     }
139     Tizen_ShowScreenKeyboard(_this, window);
140 }
141
142 void
143 Tizen_StopTextInput(_THIS)
144 {
145    if (!_this) return;
146    if (tizen_keyboard.imf_context)
147      {
148         Tizen_HideScreenKeyboard(_this, _this->windows);
149      }
150 }
151
152 void Tizen_SetTextInputRect(void)
153 {
154 }
155
156 SDL_bool
157 Tizen_HasScreenKeyboardSupport(_THIS)
158 {
159     return SDL_TRUE;
160 }
161
162 void
163 Tizen_ShowScreenKeyboard(_THIS, SDL_Window * window)
164 {
165     SDL_WindowData *wind = window->driverdata;
166     if (!tizen_keyboard.imf_context)
167           return;
168
169     ecore_imf_context_focus_in(tizen_keyboard.imf_context);
170     ecore_imf_context_input_panel_show(tizen_keyboard.imf_context);
171     ecore_imf_context_client_window_set(tizen_keyboard.imf_context, (void*)wind->id);
172 }
173
174 void
175 Tizen_HideScreenKeyboard(_THIS, SDL_Window * window)
176 {
177     if (!tizen_keyboard.imf_context)
178           return;
179
180     ecore_imf_context_focus_out(tizen_keyboard.imf_context);
181     ecore_imf_context_input_panel_hide(tizen_keyboard.imf_context);
182 }
183
184 SDL_bool
185 Tizen_IsScreenKeyboardShown(_THIS, SDL_Window * window)
186 {
187     if (!tizen_keyboard.imf_context)
188           return SDL_FALSE;
189     //EAPI Ecore_IMF_Input_Panel_State  ecore_imf_context_input_panel_state_get (Ecore_IMF_Context *ctx)
190     return ecore_imf_context_input_panel_state_get(tizen_keyboard.imf_context);
191 }