[SDL_Tizen] Delete build warnings
[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
26
27 #include "SDL_log.h"
28 #include "SDL_hints.h"
29 #include "SDL_loadso.h"
30
31 #include "SDL_tizenvideo.h"
32 #include "SDL_tizentouch.h"
33 #include "SDL_tizenkeyboard.h"
34 #include "SDL_tizenmouse.h"
35 #include "SDL_tizenevents_c.h"
36
37 #include "SDL_tizenwindow.h"
38 #include "SDL_tizenopengles.h"
39
40 #include "../../events/SDL_mouse_c.h"
41 #include "../../joystick/tizen/SDL_sysjoystick_c.h"
42
43 #include "../SDL_egl_c.h"
44 #include "../SDL_vulkan_c.h"
45 #include "../SDL_sysvideo.h"
46 #include "../../events/SDL_windowevents_c.h"
47
48 #include <Ecore_Ipc.h>
49 #include <unistd.h>
50 #include <errno.h>
51
52 enum {
53     ROTATION_TYPE_NORMAL_ROTATION = 0,
54     ROTATION_TYPE_PRE_ROTATION,      /* use pre-rotation */
55 };
56
57 #define LOAD_FUNC(NAME) \
58 _this->tizen_pre_rotation_data->NAME = SDL_LoadFunction(_this->tizen_pre_rotation_data->prerotation_dll_handle, #NAME); \
59 if (!_this->tizen_pre_rotation_data->NAME) \
60 { \
61     SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Could not retrieve pre-rotation function " #NAME); \
62     return SDL_FALSE; \
63 }
64
65 /*SDL indicator*/
66 Ecore_Ipc_Server *ipc = NULL;
67
68 #define IPC_HEAD(_type) \
69    Ecore_Ipc_Event_Client_##_type *e = event; \
70    if (ecore_ipc_client_server_get(e->client) != ipc) \
71      return ECORE_CALLBACK_PASS_ON
72
73 void SDL_ExecuteIndicatorProcess()
74 {
75     _tizen_init_ecore_ipc();
76     unsigned int childPID = fork();
77     if(childPID == 0)
78     {
79         SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] child process : %d", getpid());
80         int ret = execl("/usr/apps/org.tizen.sdl_indicator/bin/sdl_indicator", "/usr/apps/org.tizen.sdl_indicator/bin/sdl_indicator", NULL);
81         if(ret==-1)
82         {
83             SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] Failed indicator process error:%s", strerror(errno));
84             kill(getpid(), SIGKILL);
85         }
86     }
87     else if(childPID==-1)
88     {
89         SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] Failed fork");
90     }
91     else
92     {
93         SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] parent process : %d", getpid());
94     }
95 }
96
97 void _tizen_ecore_ipc_client_send(int major, int minor, int ref, int ref_to)
98 {
99     Eina_List *ipc_clients = ecore_ipc_server_clients_get(ipc);
100     Eina_List *l;
101     Ecore_Ipc_Client *cl;
102     EINA_LIST_FOREACH(ipc_clients, l, cl)
103     ecore_ipc_client_send(cl, major, minor, ref, ref_to, 0, NULL, 0);
104 }
105
106 static Eina_Bool _cb_client_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
107 {
108     IPC_HEAD(Add);
109
110     SDL_Log("[SDL]_cb_client_add");
111     SDL_Window * window = SDL_GetVideoDevice()->windows;
112     SDL_WindowData *wind = window->driverdata;
113     wind->indicator = SDL_TRUE;
114
115     int window_w, window_h;
116     ecore_wl_screen_size_get(&window_w, &window_h);
117     _tizen_ecore_ipc_client_send(OP_RESIZE, wind->rotation, window_w, window_h);
118
119     return ECORE_CALLBACK_DONE;
120 }
121
122 static Eina_Bool _cb_client_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
123 {
124     IPC_HEAD(Del);
125     SDL_Log("[SDL]_cb_client_del");
126     Eina_List *ipc_clients = ecore_ipc_server_clients_get(ipc);
127     Eina_List *l;
128     Ecore_Ipc_Client *cl;
129     EINA_LIST_FOREACH(ipc_clients, l, cl)
130     {
131         ecore_ipc_client_del(cl);
132     }
133
134     SDL_Window * window = SDL_GetVideoDevice()->windows;
135     SDL_WindowData *wind = window->driverdata;
136     wind->indicator = SDL_FALSE;
137     return ECORE_CALLBACK_DONE;
138 }
139
140 static Eina_Bool _cb_client_data(void *data, int type EINA_UNUSED, void *event)
141 {
142     IPC_HEAD(Data);
143
144     Ecore_Ipc_Event_Server_Data* epcEvent = (Ecore_Ipc_Event_Server_Data*)event;
145     SDL_Log("[SDL]_cb_client_data: %d -> %d", epcEvent->major, epcEvent->minor);
146     return ECORE_CALLBACK_DONE;
147 }
148
149 int _tizen_init_ecore_ipc()
150 {
151
152     if(!ipc)
153     {
154         if(!ecore_ipc_init())
155             SDL_Log("[SDL]Fail ecore_ipc");
156
157         ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_USER, "sdl_indicator", 0, NULL);
158
159         if(!ipc)
160             ecore_ipc_shutdown();
161
162         ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, _cb_client_add, NULL);
163         ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, _cb_client_del, NULL);
164         ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, _cb_client_data, NULL);
165     }
166     return 0;
167 }
168
169 static int
170 _tizen_rotation_type_get()
171 {
172     static int type = ROTATION_TYPE_PRE_ROTATION;
173     static int checked = 0;
174     char *engine = NULL;
175
176     if (checked) return type;
177
178     engine = getenv("SDL_ROTATION");
179
180     if (engine)
181       {
182          if ((!strcasecmp(engine, "normal")))
183             type = ROTATION_TYPE_NORMAL_ROTATION;
184          else if ((!strcasecmp(engine, "pre_rotation")))
185             type = ROTATION_TYPE_PRE_ROTATION;
186          else
187             type = ROTATION_TYPE_PRE_ROTATION;
188       }
189     checked = 1;
190     return type;
191 }
192
193 int
194 _tizen_PreRotatotion_LoadLibrary(SDL_WindowData *_this, const char *lib_path)
195 {
196     void *lib_dll_handle = NULL;
197     char *path = NULL;
198
199     if (_this->isLoaded_pre_rotation)
200         return SDL_TRUE;
201
202     _this->tizen_pre_rotation_data = (Tizen_Prerotation_Data *) SDL_calloc(1, sizeof(Tizen_Prerotation_Data));
203     if (!_this->tizen_pre_rotation_data) {
204         return SDL_OutOfMemory();
205     }
206
207     if (!lib_path)
208         lib_dll_handle = SDL_LoadObject(lib_path);
209
210     if (!lib_dll_handle) {
211         path = "libwayland-egl.so";
212         lib_dll_handle = SDL_LoadObject(path);
213     }
214
215     _this->tizen_pre_rotation_data->prerotation_dll_handle = lib_dll_handle;
216
217     if (lib_dll_handle == NULL)
218         return SDL_FALSE;
219
220     LOAD_FUNC(wl_egl_window_set_rotation);
221     LOAD_FUNC(wl_egl_window_get_capabilities);
222     _this->isLoaded_pre_rotation = 1;
223
224     return SDL_TRUE;
225 }
226
227 SDL_bool
228 Tizen_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
229 {
230     SDL_WindowData *wmdata = (SDL_WindowData *)window->driverdata;
231     info->info.tizen.egl_display = NULL;
232     info->info.tizen.egl_surface = NULL;
233 #if SDL_VIDEO_OPENGL_EGL
234     if (_this->egl_data)
235     {
236        info->info.tizen.egl_display = (void*)_this->egl_data->egl_display;
237     }
238     info->info.tizen.egl_surface = (void*)wmdata->egl_surface;
239 #endif
240     info->subsystem = SDL_SYSWM_TIZEN;
241     return SDL_TRUE;
242 }
243
244 int
245 Tizen_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
246 {
247     return 0;  /* just succeed, the real work is done elsewhere. */
248 }
249
250 void
251 Tizen_SetWindowTitle(_THIS, SDL_Window * window)
252 {
253     SDL_WindowData *wind = window->driverdata;
254     ecore_wl_window_title_set(wind->window, window->title);
255 }
256
257 void
258 Tizen_ShowWindow(_THIS, SDL_Window *window)
259 {
260     SDL_WindowData *wind = window->driverdata;
261
262     if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS) && !wind->indicator)
263     {
264         SDL_ExecuteIndicatorProcess();
265     }
266     ecore_wl_window_show(wind->window);
267 }
268
269 void
270 Tizen_HideWindow(_THIS, SDL_Window *window)
271 {
272     SDL_WindowData *wind = window->driverdata;
273     ecore_wl_window_hide(wind->window);
274 }
275
276 void
277 Tizen_RaiseWindow(_THIS, SDL_Window *window)
278 {
279     SDL_WindowData *wind = window->driverdata;
280     ecore_wl_window_raise(wind->window);
281 }
282
283 void
284 Tizen_SetWindowFullscreen(_THIS, SDL_Window *window,
285                           SDL_VideoDisplay *_display, SDL_bool fullscreen)
286 {
287     /*DO NOTHING*/
288 }
289
290 void
291 Tizen_pre_rotation_set(SDL_WindowData *_this, int rotation)
292 {
293     tizen_wl_egl_window_rotation rot;
294     if (!_this->egl_window) return;
295
296     switch (rotation) {
297          case 90:
298             rot = TIZEN_ROTATION_270;
299             break;
300          case 180:
301             rot = TIZEN_ROTATION_180;
302             break;
303          case 270:
304             rot = TIZEN_ROTATION_90;
305             break;
306          case 0:
307             rot = TIZEN_ROTATION_0;
308             break;
309          default:
310             rot = TIZEN_ROTATION_0;
311             break;
312       }
313
314       _this->tizen_pre_rotation_data->wl_egl_window_set_rotation(_this->egl_window, rot);
315 }
316
317 void
318 _tizen_set_window_size(SDL_Window * window, int w, int h)
319 {
320     if(!window)
321     {
322         SDL_SetError("Invalid window");
323         return;
324     }
325
326     SDL_VideoDevice *_this = SDL_GetVideoDevice();
327     if (!_this) {
328         SDL_SetError("Video subsystem has not been initialized");
329         return;
330     }
331
332     if (window->magic != &_this->window_magic) {
333         return;
334     }
335
336     if (w <= 0) {
337         SDL_InvalidParamError("w");
338         return;
339     }
340     if (h <= 0) {
341         SDL_InvalidParamError("h");
342         return;
343     }
344
345     /* Make sure we don't exceed any window size limits */
346     if (window->min_w && w < window->min_w)
347     {
348         w = window->min_w;
349     }
350     if (window->max_w && w > window->max_w)
351     {
352         w = window->max_w;
353     }
354     if (window->min_h && h < window->min_h)
355     {
356         h = window->min_h;
357     }
358     if (window->max_h && h > window->max_h)
359     {
360         h = window->max_h;
361     }
362
363     window->windowed.w = w;
364     window->windowed.h = h;
365
366     window->w = w;
367     window->h = h;
368 }
369
370 void
371 _tizen_send_rotation_event(SDL_Window *window, unsigned int angle)
372 {
373     SDL_Event event;
374     SDL_WindowData *wind;
375     wind = window->driverdata;
376
377     SDL_memset(&event, 0, sizeof(event));
378     event.type = SDL_ROTATEEVENT;
379     event.user.code = 0;
380     if (wind->support_pre_rotation)
381         event.user.data1 = (void*)0;
382     else
383         event.user.data1 = (void*)angle;
384     event.user.data2 = (void*)-1;
385
386     SDL_PushEvent(&event);
387     return;
388 }
389
390 void
391 _tizen_rotation_do(SDL_WindowData *wind, int rotation)
392 {
393     if(!wind) return;
394
395     SDL_Window *window = SDL_GetVideoDevice()->windows;
396     if(!window) return;
397
398     int window_w, window_h;
399     if(wind->rotation == 0 || wind->rotation == 180)
400         ecore_wl_window_geometry_get(wind->window, 0, 0, &window_w, &window_h);
401     else
402         ecore_wl_window_geometry_get(wind->window, 0, 0, &window_h, &window_w);
403
404     _tizen_set_window_size(window, window_w, window_h);
405
406     if(wind->support_pre_rotation)
407         Tizen_pre_rotation_set(wind, rotation);
408
409     _tizen_send_rotation_event(window, wind->rotation);
410     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
411
412 }
413
414 void
415 Tizen_rotate_update(SDL_WindowData *wind)
416 {
417     if(!wind) return;
418
419     int screen_rotation;
420     if (ecore_wl_window_ignore_output_transform_get(wind->window))
421     {
422         screen_rotation = 0;
423     }
424     else
425     {
426         Ecore_Wl_Output *output = ecore_wl_window_output_find(wind->window);
427         screen_rotation = ecore_wl_output_transform_get(output) * 90;
428     }
429
430     //Set Screen Rotation
431     wind->output_rotation = screen_rotation;
432     if(wind->support_pre_rotation)
433         ecore_wl_window_buffer_transform_set(wind->window, wind->output_rotation / 90);
434
435     ecore_wl_window_rotation_set(wind->window, wind->rotation);
436
437     int rotation = (wind->output_rotation + wind->rotation) % 360;
438     _tizen_rotation_do(wind, rotation);
439 }
440
441 void
442 _tizen_window_orientaiton_hint_callback(void *userdata, const char *name, const char *oldValue, const char *newValue)
443 {
444     char *p_str = NULL;
445     char orientation_type[4][20] = {"Portrait","LandscapeLeft","PortraitUpsideDown","LandscapeRight"};
446     int checked[4] = {0,};
447     int i;
448     unsigned int j = 0;
449     SDL_WindowData *wind = (SDL_WindowData*)userdata;
450     Ecore_Wl_Window *window = wind->window;
451
452     if (wind->rotation_supported == 0)
453         return;
454
455     SDL_assert(SDL_strncmp(name, SDL_HINT_ORIENTATIONS, SDL_strlen(SDL_HINT_ORIENTATIONS)) == 0);
456
457     if ((oldValue == NULL) && (newValue == NULL))
458         return;
459
460     for (i=0;i<4;i++) {
461         p_str = SDL_strstr(newValue, orientation_type[i]);
462         if (p_str) {
463             if (p_str == newValue) {
464                 int rot = 0;
465                 if (i == 0) rot = 0;
466                 else if (i == 1) rot = 90;
467                 else if (i == 2) rot = 180;
468                 else if (i == 3) rot = 270;
469                 wind->rotation = rot;
470             }
471
472             if (i == 0) {
473                 checked[j] = 0;
474             }
475             else if (i == 1) {
476                 checked[j] = 90;
477             }
478             else if (i == 2) {
479                 checked[j] = 180;
480             }
481             else if (i == 3) {
482                 checked[j] = 270;
483             }
484             j++;
485         }
486     }
487
488     if (j > 0) {
489         if (j == 1) {
490             ecore_wl_window_rotation_preferred_rotation_set(window, wind->rotation);
491         }else {
492             ecore_wl_window_rotation_available_rotations_set(window, (const int*)checked, j);
493         }
494     }
495     Tizen_rotate_update(wind);
496 }
497
498 void
499 _tizen_window_orientation_add_hint(void *data)
500 {
501     SDL_WindowData *wind = (SDL_WindowData*)data;
502     if (wind->rotation_supported == 0)
503         return;
504
505     SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, _tizen_window_orientaiton_hint_callback, data);
506 }
507
508 static Eina_Bool
509 _tizen_cb_output_transform(void *data, int type EINA_UNUSED, void *event)
510 {
511     SDL_Window * window = SDL_GetVideoDevice()->windows;
512     SDL_WindowData *wind = window->driverdata;
513
514     if(!wind) return ECORE_CALLBACK_PASS_ON;
515
516     Ecore_Wl_Event_Output_Transform *ev = event;
517     Ecore_Wl_Output *output;
518
519     output = ecore_wl_window_output_find(wind->window);
520     if (output != ev->output) return ECORE_CALLBACK_PASS_ON;
521
522     wind->received_rotation = 1;
523
524
525    return ECORE_CALLBACK_PASS_ON;
526 }
527
528 static Eina_Bool
529 _tizen_cb_ignore_output_transform(void *data, int type EINA_UNUSED, void *event EINA_UNUSED)
530 {
531     SDL_Window * window = SDL_GetVideoDevice()->windows;
532     SDL_WindowData *wind = window->driverdata;
533
534     if(!wind) return ECORE_CALLBACK_PASS_ON;
535
536     wind->received_rotation = 1;
537
538    return ECORE_CALLBACK_PASS_ON;
539 }
540
541 void
542 _tizen_output_transform_register(SDL_WindowData *wind)
543 {
544     if(!wind) return;
545
546     Ecore_Wl_Output *output = ecore_wl_window_output_find(wind->window);
547     wind->output_rotation = ecore_wl_output_transform_get(output) * 90;
548
549     ecore_event_handler_add(ECORE_WL_EVENT_OUTPUT_TRANSFORM,
550                              _tizen_cb_output_transform, NULL);
551     ecore_event_handler_add(ECORE_WL_EVENT_IGNORE_OUTPUT_TRANSFORM,
552                              _tizen_cb_ignore_output_transform, NULL);
553 }
554
555 int
556 Tizen_CreateWindow(_THIS, SDL_Window *window)
557 {
558     SDL_VideoData *data = _this->driverdata;
559     SDL_WindowData *wind;
560     Ecore_Wl_Global *global;
561     Eina_Inlist *globals;
562
563     wind = calloc(1, sizeof * wind);
564     if (!wind) {
565         return SDL_OutOfMemory();
566     }
567
568     window->driverdata = wind;
569     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
570
571 #if SDL_VIDEO_OPENGL_EGL
572     if (window->flags & SDL_WINDOW_OPENGL) {
573         SDL_GL_LoadLibrary(NULL);
574     }
575 #endif
576
577 #if SDL_VIDEO_VULKAN
578     if (window->flags & SDL_WINDOW_VULKAN) {
579         if (!_this->vulkan_GetInstanceExtensions) {
580             SDL_SetError("No Vulkan support in video driver");
581         }
582
583         if (_this->vulkan_LoadLibrary(_this, NULL) < 0) {
584             SDL_SetError("Fail to load Vulkan Library");
585         }
586     }
587 #endif
588
589     if (window->x == SDL_WINDOWPOS_UNDEFINED) {
590         window->x = 0;
591     }
592     if (window->y == SDL_WINDOWPOS_UNDEFINED) {
593         window->y = 0;
594     }
595
596     if (!(globals = ecore_wl_globals_get()))
597       {
598          SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Failed to get wayland globals");
599          return -1;
600       }
601
602     wind->window = ecore_wl_window_new(NULL,
603                                        window->x, window->y, window->w, window->h,
604                                        ECORE_WL_WINDOW_BUFFER_TYPE_SHM);
605     if (!wind->window) {
606         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Failed to create wayland window");
607         return -1;
608     }
609     _tizen_output_transform_register(wind);
610
611     wind->surface = ecore_wl_window_surface_create(wind->window);
612     if (!wind->surface) {
613         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Failed to create wayland window surface");
614         return -1;
615     }
616     ecore_wl_window_type_set(wind->window, ECORE_WL_WINDOW_TYPE_UTILITY);
617
618     wind->rotation = 0;
619     wind->rotation_supported = 0;
620     wind->received_rotation = 0;
621
622 #if SDL_VIDEO_OPENGL_EGL
623     if (window->flags & SDL_WINDOW_OPENGL) {
624
625         if(wind->output_rotation == 90 || wind->output_rotation == 270)
626             wind->egl_window = wl_egl_window_create(wind->surface, window->h, window->w);
627         else
628             wind->egl_window = wl_egl_window_create(wind->surface, window->w, window->h);
629
630         /* Create the GLES window surface */
631         wind->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) wind->egl_window);
632         if (wind->egl_surface == EGL_NO_SURFACE) {
633             SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "failed to create a window surface");
634             return -1;
635         }
636
637         if (!_this->gl_config.alpha_size) {
638             ecore_wl_window_opaque_region_set(wind->window, window->x, window->y, window->w, window->h);
639         }
640         else {
641             wl_surface_set_opaque_region(wind->surface, NULL);
642         }
643
644         //Support PreRotation
645         wind->support_pre_rotation = 0;
646         if (_tizen_rotation_type_get() && _tizen_PreRotatotion_LoadLibrary(wind, "libwayland-egl.so")) {
647             if (wind->tizen_pre_rotation_data->wl_egl_window_get_capabilities(wind->egl_window) == TIZEN_WL_EGL_WINDOW_CAPABILITY_ROTATION_SUPPORTED ) {
648                 wind->support_pre_rotation = 1;
649             }
650         }
651         else
652         {
653             wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
654         }
655
656     }
657 #endif
658
659     wind->id = ecore_wl_window_id_get(wind->window);
660     eina_hash_add(data->windows, &wind->id, window);
661
662     Tizen_InitKeyboard(_this);
663     SDL_SetMouseFocus(window);
664
665     if (window->flags & 0x00008000) {
666         ecore_wl_window_input_region_set(wind->window, -1, -1, 1, 1);
667         ecore_wl_window_focus_skip_set(wind->window, EINA_TRUE);
668     }
669
670     EINA_INLIST_FOREACH(globals, global) {
671          if (!strcmp(global->interface, "tizen_policy_ext")) {
672               wind->rotation_supported = 1;
673               break;
674            }
675       }
676     // Add orientaiton hint cb
677     _tizen_window_orientation_add_hint((void*)wind);
678
679     Tizen_rotate_update(wind);
680
681     return 0;
682 }
683
684 void
685 Tizen_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
686 {
687     SDL_WindowData *wind = window->driverdata;
688     if(!wind->indicator)
689         SDL_ExecuteIndicatorProcess();
690 }
691
692 void
693 Tizen_SetWindowSize(_THIS, SDL_Window *window)
694 {
695     SDL_WindowData *wind = window->driverdata;
696     if (!wind->egl_window)
697         return;
698
699     if(wind->support_pre_rotation && (wind->rotation==90 || wind->rotation==270))
700         ecore_wl_window_update_size(wind->window, window->h, window->w);
701     else
702         ecore_wl_window_update_size(wind->window, window->w, window->h);
703
704     // TODO : consider to rotation status.
705     #if SDL_VIDEO_OPENGL_EGL
706     if (window->flags & SDL_WINDOW_OPENGL) {
707         if(wind->output_rotation==90 || wind->output_rotation==270)
708             wl_egl_window_resize(wind->egl_window, window->h, window->w, 0, 0);
709         else
710             wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
711     }
712     #endif
713
714 }
715
716 void
717 Tizen_GetWindowSize(_THIS, SDL_Window *window, int *w, int *h)
718 {
719     SDL_WindowData *wind = window->driverdata;
720     if (!wind->window) {
721         return;
722     }
723
724     if (w) *w = window->w;
725     if (h) *h = window->h;
726 }
727
728 void
729 Tizen_SetWindowPosition(_THIS, SDL_Window * window)
730 {
731     SDL_WindowData *wind = window->driverdata;
732     if (!wind->window) {
733         return;
734     }
735
736     // TODO : consider to rotation status.
737    ecore_wl_window_position_set(wind->window, window->x, window->y);
738 }
739
740 void
741 Tizen_DestroyWindow(_THIS, SDL_Window *window)
742 {
743     SDL_VideoData *data = _this->driverdata;
744     SDL_WindowData *wind = window->driverdata;
745
746     if (data) {
747         eina_hash_del(data->windows, &wind->id, window);
748 #if SDL_VIDEO_OPENGL_EGL
749     if (window->flags & SDL_WINDOW_OPENGL) {
750         SDL_EGL_DestroySurface(_this, wind->egl_surface);
751         wl_egl_window_destroy(wind->egl_window);
752     }
753 #endif
754         ecore_wl_window_free(wind->window);
755         SDL_free(wind);
756     }
757
758     window->driverdata = NULL;
759 }
760
761 SDL_Window*
762 Tizen_FindWindow(_THIS, Ecore_Wl_Window *ewin)
763 {
764     SDL_VideoData *data = _this->driverdata;
765     int id;
766
767     id = ecore_wl_window_id_get(ewin);
768     return (SDL_Window*)eina_hash_find(data->windows, &id);
769 }
770
771 Eina_Bool
772 _tizen_cb_event_window_visibility_change(void *data, int type, void *event)
773 {
774     _THIS = data;
775     Ecore_Wl_Event_Window_Visibility_Change *ev;
776     Ecore_Wl_Window *ew;
777     SDL_Window *window;
778
779     ev = event;
780     ew = ecore_wl_window_find(ev->win);
781     window = Tizen_FindWindow(_this, ew);
782
783     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
784     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
785     return ECORE_CALLBACK_PASS_ON;
786 }
787
788 Eina_Bool
789 _tizen_cb_window_configure(void *data, int type EINA_UNUSED, void *event)
790 {
791    _THIS = data;
792    Ecore_Wl_Window *ew;
793    SDL_Window *window;
794    SDL_WindowData *wind;
795    Ecore_Wl_Event_Window_Configure *ev;
796    ev = event;
797    ew = ecore_wl_window_find(ev->win);
798    window = Tizen_FindWindow(_this, ew);
799    wind = window->driverdata;
800
801    if (wind->rotation_supported == 0){
802       return ECORE_CALLBACK_PASS_ON;
803    }
804
805    return ECORE_CALLBACK_PASS_ON;
806 }
807
808 Eina_Bool
809 _tizen_cb_event_window_rotate(void *data, int type EINA_UNUSED, void *event)
810 {
811     _THIS = data;
812     Ecore_Wl_Event_Window_Rotate *ev;
813     Ecore_Wl_Window *ew;
814     SDL_Window *window;
815     SDL_WindowData *wind;
816
817     ev = event;
818     if (!ev) {
819         return ECORE_CALLBACK_PASS_ON;
820     }
821
822     ew = ecore_wl_window_find(ev->win);
823     window = Tizen_FindWindow(_this, ew);
824     wind = window->driverdata;
825
826     if (wind->rotation != ev->angle) {
827         /* set ecore_wayland window rotation */
828         wind->rotation = ev->angle;
829         wind->received_rotation = 1;
830     }
831
832     return ECORE_CALLBACK_PASS_ON;
833 }
834
835 int
836 Tizen_InitWindow(_THIS)
837 {
838     SDL_VideoData *data = _this->driverdata;
839
840     data->windows = eina_hash_int32_new(NULL);
841
842     ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
843                         _tizen_cb_event_window_visibility_change,_this);
844     ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_ROTATE,
845                         _tizen_cb_event_window_rotate,_this);
846     ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE,
847                         _tizen_cb_window_configure,_this);
848
849     ecore_event_handler_add(ECORE_EVENT_KEY_UP,
850                         _tizen_cb_event_keyup_change,_this);
851     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
852                         _tizen_cb_event_keydown_change,_this);
853
854     ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN,
855                         _tizen_cb_event_mousedown_change,_this);
856     ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP,
857                         _tizen_cb_event_mouseup_change,_this);
858     ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE,
859                         _tizen_cb_event_mousemove_change,_this);
860     ecore_event_handler_add(ECORE_WL_EVENT_MOUSE_IN,
861                         _tizen_cb_event_mouse_in,_this);
862     ecore_event_handler_add(ECORE_WL_EVENT_MOUSE_OUT,
863                         _tizen_cb_event_mouse_out,_this);
864
865     ecore_event_handler_add(ECORE_WL_EVENT_FOCUS_IN,
866                         _tizen_cb_event_focus_in,_this);
867     ecore_event_handler_add(ECORE_WL_EVENT_FOCUS_OUT,
868                         _tizen_cb_event_focus_out,_this);
869
870     ecore_event_handler_add(ECORE_EVENT_JOYSTICK,
871                         _tizen_cb_event_joystick_change,_this);
872
873     return 0;
874 }
875
876 void
877 Tizen_DeinitWindow(_THIS)
878 {
879     SDL_VideoData *data = _this->driverdata;
880
881     eina_hash_free(data->windows);
882 }
883 #endif /* SDL_VIDEO_DRIVER_TIZEN */
884
885 /* vi: set ts=4 sw=4 expandtab: */