Merge "[SDL_Tizen]Add SDL_SetWindowBordered function" into tizen
[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 <app_internal.h>
27 #include <app_extension.h>
28 #include <system_settings.h>
29 #include <aul.h>
30
31 #include "SDL_log.h"
32 #include "SDL_events.h"
33 #include "../../events/SDL_events_c.h"
34
35 #include "SDL_tizen.h"
36
37 static int tizen_appcore_initialized = 0;
38 static appcore_context_h appcore_handle = NULL;
39 static ui_app_lifecycle_callback_s event_callback = {0,};
40 static app_event_handler_h handlers[5] = {NULL, };
41
42 int _tizen_terminate_event_filter(void *userdata, SDL_Event * event);
43 int _tizen_aul_status_changed_cb(int status, void *data);
44
45 /* TODO  ::
46  * Impplementation of serveral app core callback function for SDL Application and App Core
47  * */
48
49 static bool
50 _tizen_sdl_create(void *data)
51 {
52     SDL_Log("App Create");
53     return true;
54 }
55
56 static void
57 _tizen_sdl_pause (void *data)
58 {
59     SDL_Log("App Pause");
60     SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
61     SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
62     return;
63 }
64
65 static void
66 _tizen_sdl_resume(void *data)
67 {
68     SDL_Log("App Resume");
69     SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
70     SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
71     return;
72 }
73
74 static void
75 _tizen_sdl_control(app_control_h app_control, void *data)
76 {
77     SDL_Log("App Control");
78
79     SDL_Event event;
80     SDL_memset(&event, 0, sizeof(event));
81     event.type = SDL_APP_CONTROL;
82     event.user.code = 0;
83     event.user.data1 = (void*)app_control;
84     event.user.data2 = (void*)data;
85
86     SDL_PushEvent(&event);
87     return;
88 }
89
90 static void
91 _tizen_app_lang_changed(app_event_info_h event_info, void *user_data)
92 {
93     /*APP_EVENT_LANGUAGE_CHANGED*/
94     SDL_Log("Language changed");
95
96     SDL_Event event;
97     SDL_memset(&event, 0, sizeof(event));
98     event.type = SDL_APP_LANGUAGE_CHANGED;
99     event.user.code = 0;
100     event.user.data1 = (void*)event_info;
101     event.user.data2 = (void*)user_data;
102
103     SDL_PushEvent(&event);
104     return;
105 }
106
107 static void
108 _tizen_app_orient_changed(app_event_info_h event_info, void *user_data)
109 {
110     /*APP_EVENT_DEVICE_ORIENTATION_CHANGED*/
111     app_device_orientation_e orientation;
112     app_event_get_device_orientation(event_info, &orientation);
113     SDL_Log("Orientation Changed, Rotation Degree : %d", orientation);
114     SDL_Event event;
115     SDL_memset(&event, 0, sizeof(event));
116     event.type = SDL_ROTATEEVENT;
117     event.user.code = 0;
118     event.user.data1 = (void*)orientation;
119     event.user.data2 = (void*)-1;
120
121     SDL_PushEvent(&event);
122     return;
123 }
124
125 static void
126 _tizen_app_region_changed(app_event_info_h event_info, void *user_data)
127 {
128     /*APP_EVENT_REGION_FORMAT_CHANGED*/
129     SDL_Log("region changed");
130
131     SDL_Event event;
132     SDL_memset(&event, 0, sizeof(event));
133     event.type = SDL_APP_RIGION_CHANGED;
134     event.user.code = 0;
135     event.user.data1 = (void*)event_info;
136     event.user.data2 = (void*)user_data;
137
138     SDL_PushEvent(&event);
139 }
140
141 static void
142 _tizen_app_low_battery(app_event_info_h event_info, void *user_data)
143 {
144     /*APP_EVENT_LOW_BATTERY*/
145     SDL_Log("low battery");
146
147     SDL_Event event;
148     SDL_memset(&event, 0, sizeof(event));
149     event.type = SDL_APP_LOWBATTERY;
150     event.user.code = 0;
151     event.user.data1 = (void*)event_info;
152     event.user.data2 = (void*)user_data;
153
154     SDL_PushEvent(&event);
155     return;
156 }
157
158 static void
159 _tizen_app_low_memory(app_event_info_h event_info, void *user_data)
160 {
161     /*APP_EVENT_LOW_MEMORY*/
162     SDL_Log("low memory");
163     SDL_SendAppEvent(SDL_APP_LOWMEMORY);
164     return;
165 }
166
167 int
168 _tizen_aul_status_changed_cb(int status, void *data)
169 {
170     if (status == STATUS_DYING) {
171         SDL_Log(":: STATUS_DYING");
172         SDL_SendQuit();
173         SDL_SendAppEvent(SDL_APP_TERMINATING);
174
175         SDL_DelEventWatch(_tizen_terminate_event_filter, NULL);
176     }
177
178     return 1;
179 }
180
181 int
182 _tizen_terminate_event_filter(void *userdata, SDL_Event * event)
183 {
184     if (!event) {
185         SDL_Log("Event is NULL");
186         return -1;
187     }
188
189     switch(event->type) {
190     case SDL_APP_TERMINATING: {
191             aul_remove_status_local_cb(_tizen_aul_status_changed_cb, NULL);
192             SDL_tizen_app_exit();
193         }
194         break;
195     default:
196         break;
197     }
198
199     return 1;
200 }
201
202 int
203 SDL_tizen_app_init(int argc, char *argv[])
204 {
205     int aul_ret = 0;
206
207     SDL_Log( "SDL Tizen App initialize");
208
209     if (tizen_appcore_initialized) {
210         SDL_LogError(SDL_LOG_CATEGORY_ASSERT,"Already initialized!");
211         return 0;
212     }
213     tizen_appcore_initialized = 1;
214     if (argc < 1 || argv == NULL) {
215         SDL_LogError(SDL_LOG_CATEGORY_ASSERT,"APP_ERROR_INVALID_PARAMETER");
216         return 0;
217     }
218
219     event_callback.create = _tizen_sdl_create;
220     event_callback.pause = _tizen_sdl_pause;
221     event_callback.resume = _tizen_sdl_resume;
222     event_callback.app_control = _tizen_sdl_control;
223
224     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, _tizen_app_low_battery, NULL);
225     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, _tizen_app_low_memory, NULL);
226     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, _tizen_app_orient_changed, NULL);
227     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _tizen_app_lang_changed, NULL);
228     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, _tizen_app_region_changed, NULL);
229
230     aul_ret = aul_add_status_local_cb(_tizen_aul_status_changed_cb, NULL);
231     if (aul_ret == AUL_R_ERROR) {
232         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "AUL status changed callback setting is failed.");
233         return 0;
234     }
235
236     /* add event watch for SDL_APP_TERMINATING */
237     SDL_AddEventWatch(_tizen_terminate_event_filter, NULL);
238
239     return ui_app_init(argc, argv, &event_callback, NULL, &appcore_handle);
240 }
241
242 void
243 SDL_tizen_app_exit(void)
244 {
245     SDL_Log("call ui_app_fini");
246     ui_app_fini(appcore_handle);
247 }
248 #endif