[SDL_Tizen] Add rotate event
[platform/upstream/SDL.git] / src / core / tizen / SDL_tizen.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 #include <stdio.h>
23 #include "../../SDL_internal.h"
24
25 #if __TIZEN__
26 #include "SDL_tizen.h"
27 #include "SDL_log.h"
28 #include "SDL_events.h"
29 #include <app_internal.h>
30 #include <app_extension.h>
31 #include <system_settings.h>
32
33 static int tizen_appcore_initialized = 0;
34 static appcore_context_h appcore_handle = NULL;
35 static ui_app_lifecycle_callback_s event_callback = {0,};
36 static app_event_handler_h handlers[5] = {NULL, };
37
38 /* TODO  ::
39  * Impplementation of serveral app core callback function for SDL Application and App Core
40  * */
41
42 static bool
43 _tizen_sdl_create(void *data)
44 {
45     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL_AppCore CreateCB");
46     return true;
47 }
48
49 static void
50 _tizen_sdl_terminate(void *data)
51 {
52     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL_AppCore TermincateCB");
53     SDL_SendQuit();
54     SDL_SendAppEvent(SDL_APP_TERMINATING);
55     return;
56 }
57
58 static void
59 _tizen_sdl_pause (void *data)
60 {
61     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL_AppCore PauseCB");
62     SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
63     SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
64     return;
65 }
66
67 static void
68 _tizen_sdl_resume(void *data)
69 {
70     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL_AppCore ResumeCB");
71     SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
72     SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
73     return;
74 }
75
76 static void
77 _tizen_sdl_control(app_control_h app_control, void *data)
78 {
79     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL_AppCore ControlCB");
80     return;
81 }
82
83 static void
84 _tizen_app_lang_changed(app_event_info_h event_info, void *user_data)
85 {
86     /*APP_EVENT_LANGUAGE_CHANGED*/
87     char *locale = NULL;
88     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Language changedCB");
89     system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
90     free(locale);
91     return;
92 }
93
94 static void
95 _tizen_app_orient_changed(app_event_info_h event_info, void *user_data)
96 {
97     /*APP_EVENT_DEVICE_ORIENTATION_CHANGED*/
98     app_device_orientation_e orientation;
99     app_event_get_device_orientation(event_info, &orientation);
100     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION," Orientation Changed CB, Rotation Degree : %d", orientation);
101     SDL_Event event;
102     SDL_memset(&event, 0, sizeof(event));
103     event.type = SDL_ROTATEEVENT;
104     event.user.code = NULL;
105     event.window.data1 = (void*)orientation;
106     event.user.data2 = -1;
107
108     SDL_PushEvent(&event);
109     return;
110 }
111
112 static void
113 _tizen_app_region_changed(app_event_info_h event_info, void *user_data)
114 {
115     /*APP_EVENT_REGION_FORMAT_CHANGED*/
116     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "region changedCB");
117 }
118
119 static void
120 _tizen_app_low_battery(app_event_info_h event_info, void *user_data)
121 {
122     /*APP_EVENT_LOW_BATTERY*/
123     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "low battery CB");
124 }
125
126 static void
127 _tizen_app_low_memory(app_event_info_h event_info, void *user_data)
128 {
129     /*APP_EVENT_LOW_MEMORY*/
130     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "low memory CB");
131     SDL_SendAppEvent(SDL_APP_LOWMEMORY);
132 }
133
134 int
135 SDL_tizen_app_init(int argc, char *argv[])
136 {
137     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "SDL Tizen App initialize");
138     if (tizen_appcore_initialized)
139     {
140         SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Already initialized!");
141         return 0;
142     }
143     tizen_appcore_initialized = 1;
144     if (argc < 1 || argv == NULL)
145     {
146         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "APP_ERROR_INVALID_PARAMETER");
147         return 0;
148     }
149
150     event_callback.create = _tizen_sdl_create;
151     event_callback.terminate = _tizen_sdl_terminate;
152     event_callback.pause = _tizen_sdl_pause;
153     event_callback.resume = _tizen_sdl_resume;
154     event_callback.app_control = _tizen_sdl_control;
155
156     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, _tizen_app_low_battery, NULL);
157     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, _tizen_app_low_memory, NULL);
158     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, _tizen_app_orient_changed, NULL);
159     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _tizen_app_lang_changed, NULL);
160     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, _tizen_app_region_changed, NULL);
161
162     return ui_app_init(argc, argv, &event_callback, NULL, &appcore_handle);
163 }
164
165 void
166 SDL_tizen_app_exit(void)
167 {
168     ui_app_fini(appcore_handle);
169 }
170 #endif