[SDL_Tizen] Add Virtual Keyboard
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenwindow.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 && SDL_VIDEO_OPENGL_EGL
26
27 #include "../SDL_sysvideo.h"
28 #include "../../events/SDL_windowevents_c.h"
29 #include "../SDL_egl_c.h"
30 #include "SDL_tizenwindow.h"
31 #include "SDL_tizenvideo.h"
32 #include "SDL_tizentouch.h"
33 #include "SDL_tizenkeyboard.h"
34
35 #include "SDL_tizenmouse.h"
36 #include "SDL_tizenevents_c.h"
37 #include "SDL_log.h"
38
39 #include <wayland-egl.h>
40
41 SDL_bool
42 Tizen_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
43 {
44     TRACE_ENTER();
45     return SDL_TRUE;
46 }
47
48 int
49 Tizen_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
50 {
51     TRACE_ENTER();
52     return 0;  /* just succeed, the real work is done elsewhere. */
53 }
54
55 void
56 Tizen_ShowWindow(_THIS, SDL_Window *window)
57 {
58     TRACE_ENTER();
59     SDL_WindowData *wind = window->driverdata;
60
61     ecore_wl_window_show(wind->window);
62 }
63
64 void
65 Tizen_SetWindowFullscreen(_THIS, SDL_Window *window,
66                           SDL_VideoDisplay *_display, SDL_bool fullscreen)
67 {
68     TRACE_ENTER();
69     /*DO NOTHING*/
70 }
71
72 int
73 Tizen_CreateWindow(_THIS, SDL_Window *window)
74 {
75     TRACE_ENTER();
76     SDL_VideoData *data = _this->driverdata;
77     SDL_WindowData *wind;
78
79     wind = calloc(1, sizeof * wind);
80     if (wind == NULL)
81         return SDL_OutOfMemory();
82
83     window->driverdata = wind;
84
85     if (!(window->flags & SDL_WINDOW_OPENGL)) {
86         SDL_GL_LoadLibrary(NULL);
87         window->flags |= SDL_WINDOW_OPENGL;
88     }
89
90     if (window->x == SDL_WINDOWPOS_UNDEFINED) {
91         window->x = 0;
92     }
93     if (window->y == SDL_WINDOWPOS_UNDEFINED) {
94         window->y = 0;
95     }
96
97     wind->window = ecore_wl_window_new(NULL,
98                                        window->x, window->y, window->w, window->h,
99                                        ECORE_WL_WINDOW_BUFFER_TYPE_SHM);
100     ecore_wl_window_surface_create(wind->window);
101
102     wind->egl_window = wl_egl_window_create(ecore_wl_window_surface_get(wind->window), window->w, window->h);
103
104     /* Create the GLES window surface */
105     wind->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) wind->egl_window);
106     if (wind->egl_surface == EGL_NO_SURFACE) {
107         return SDL_SetError("failed to create a window surface");
108     }
109
110     wind->id = ecore_wl_window_id_get(wind->window);
111     eina_hash_add(data->windows, &wind->id, window);
112
113     if(keyboard.imf_context == NULL)
114         Tizen_InitKeyboard(_this);
115
116     return 0;
117 }
118
119 void
120 Tizen_SetWindowSize(_THIS, SDL_Window *window)
121 {
122     TRACE_ENTER();
123     SDL_WindowData *wind = window->driverdata;
124
125     wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
126 }
127
128 void
129 Tizen_DestroyWindow(_THIS, SDL_Window *window)
130 {
131     TRACE_ENTER();
132     SDL_VideoData *data = _this->driverdata;
133     SDL_WindowData *wind = window->driverdata;
134
135     if (data) {
136         eina_hash_del(data->windows, &wind->id, window);
137
138         SDL_EGL_DestroySurface(_this, wind->egl_surface);
139         wl_egl_window_destroy(wind->egl_window);
140         ecore_wl_window_free(wind->window);
141
142         SDL_free(wind);
143     }
144
145     window->driverdata = NULL;
146 }
147
148 static SDL_Window*
149 __tizen_find_window(_THIS, Ecore_Wl_Window *ewin)
150 {
151     SDL_VideoData *data = _this->driverdata;
152     int id;
153
154     id = ecore_wl_window_id_get(ewin);
155     return (SDL_Window*)eina_hash_find(data->windows, &id);
156 }
157
158 static Eina_Bool
159 __tizen_cb_window_visibility_change(void *data, int type, void *event)
160 {
161     _THIS = data;
162     Ecore_Wl_Event_Window_Visibility_Change *ev;
163     Ecore_Wl_Window *ew;
164     SDL_Window *window;
165
166     ev = event;
167     ew = ecore_wl_window_find(ev->win);
168     window = __tizen_find_window(_this, ew);
169
170     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "visibility window: %p, ecore_wl_window: %p\n", window, ew);
171
172     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
173     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
174
175     return ECORE_CALLBACK_PASS_ON;
176 }
177
178 int 
179 Tizen_InitWindow(_THIS)
180 {
181     TRACE_ENTER();
182     SDL_VideoData *data = _this->driverdata;
183
184     data->windows = eina_hash_int32_new(NULL);
185     
186     ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
187                         __tizen_cb_window_visibility_change, _this);
188     ecore_event_handler_add(ECORE_EVENT_KEY_UP,
189                         __tizen_cb_event_keyup_change,  NULL);
190     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
191                         __tizen_cb_event_keydown_change,        NULL);
192
193     ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN,
194                         __tizen_cb_event_mousedown_change,      _this);
195     ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP,
196                         __tizen_cb_event_mouseup_change,        _this);
197     ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE,
198                         __tizen_cb_event_mousemove_change,      _this);
199
200     return 0;
201 }
202
203 void
204 Tizen_DeinitWindow(_THIS)
205 {
206     TRACE_ENTER();
207     SDL_VideoData *data = _this->driverdata;
208
209     eina_hash_free(data->windows);
210 }
211 #endif /* SDL_VIDEO_DRIVER_TIZEN && SDL_VIDEO_OPENGL_EGL */
212
213 /* vi: set ts=4 sw=4 expandtab: */