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