[SDL_Tizen] Fix coverity issue
[platform/upstream/SDL.git] / src / core / tizen / SDL_tizen.c
index 23390d1..1f1ad18 100644 (file)
 #include "../../SDL_internal.h"
 
 #if __TIZEN__
-#include "SDL_tizen.h"
-#include "SDL_log.h"
-#include "SDL_events.h"
-#include "../../events/SDL_events_c.h"
 #include <app_internal.h>
 #include <app_extension.h>
 #include <system_settings.h>
+#include <aul.h>
+
+#include "SDL_log.h"
+#include "SDL_events.h"
+#include "../../events/SDL_events_c.h"
+#include "../../video/tizen/SDL_tizenwindow.h"
+
+#include "SDL_tizen.h"
 
 static int tizen_appcore_initialized = 0;
 static appcore_context_h appcore_handle = NULL;
 static ui_app_lifecycle_callback_s event_callback = {0,};
 static app_event_handler_h handlers[5] = {NULL, };
 
+static app_control_h event_app_control;
+static SDL_bool app_control_init = 0;
+
+int _tizen_terminate_event_filter(void *userdata, SDL_Event * event);
+int _tizen_aul_status_changed_cb(int status, void *data);
+
 /* TODO  ::
  * Impplementation of serveral app core callback function for SDL Application and App Core
  * */
@@ -48,20 +58,14 @@ _tizen_sdl_create(void *data)
 }
 
 static void
-_tizen_sdl_terminate(void *data)
-{
-    SDL_Log("App Termincate");
-    SDL_SendQuit();
-    SDL_SendAppEvent(SDL_APP_TERMINATING);
-    return;
-}
-
-static void
 _tizen_sdl_pause (void *data)
 {
     SDL_Log("App Pause");
     SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
     SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
+
+    _tizen_ecore_ipc_client_send(OP_PAUSE, 0, 0, 0);
+
     return;
 }
 
@@ -71,6 +75,9 @@ _tizen_sdl_resume(void *data)
     SDL_Log("App Resume");
     SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
     SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
+
+    _tizen_ecore_ipc_client_send(OP_RESUME, 0, 0, 0);
+
     return;
 }
 
@@ -83,10 +90,18 @@ _tizen_sdl_control(app_control_h app_control, void *data)
     SDL_memset(&event, 0, sizeof(event));
     event.type = SDL_APP_CONTROL;
     event.user.code = 0;
-    event.user.data1 = (void*)app_control;
+
+    if(app_control_init == 1)
+        app_control_destroy(event_app_control);
+
+    app_control_clone(&event_app_control, app_control);
+    app_control_init = 1;
+
+    event.user.data1 = (void*)event_app_control;
     event.user.data2 = (void*)data;
 
     SDL_PushEvent(&event);
+
     return;
 }
 
@@ -94,17 +109,29 @@ static void
 _tizen_app_lang_changed(app_event_info_h event_info, void *user_data)
 {
     /*APP_EVENT_LANGUAGE_CHANGED*/
-    char *locale = NULL;
     SDL_Log("Language changed");
-    system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
-    free(locale);
+
+    SDL_Event event;
+    SDL_memset(&event, 0, sizeof(event));
+    event.type = SDL_APP_LANGUAGE_CHANGED;
+    event.user.code = 0;
+    char* language;
+    int ret = app_event_get_language(event_info, &language);
+    if(ret != APP_ERROR_NONE) {
+        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "app_event_get_language() failed. Err = %d.", ret);
+    }
+    event.user.data1 = (void*)language;
+    event.user.data2 = (void*)user_data;
+
+    SDL_PushEvent(&event);
     return;
 }
 
+/*
+//APP_EVENT_DEVICE_ORIENTATION_CHANGED
 static void
 _tizen_app_orient_changed(app_event_info_h event_info, void *user_data)
 {
-    /*APP_EVENT_DEVICE_ORIENTATION_CHANGED*/
     app_device_orientation_e orientation;
     app_event_get_device_orientation(event_info, &orientation);
     SDL_Log("Orientation Changed, Rotation Degree : %d", orientation);
@@ -118,12 +145,24 @@ _tizen_app_orient_changed(app_event_info_h event_info, void *user_data)
     SDL_PushEvent(&event);
     return;
 }
+*/
 
 static void
 _tizen_app_region_changed(app_event_info_h event_info, void *user_data)
 {
     /*APP_EVENT_REGION_FORMAT_CHANGED*/
     SDL_Log("region changed");
+
+    SDL_Event event;
+    SDL_memset(&event, 0, sizeof(event));
+    event.type = SDL_APP_REGION_CHANGED;
+    event.user.code = 0;
+    char* region;
+    app_event_get_region_format(event_info, &region);
+    event.user.data1 = (void*)region;
+    event.user.data2 = (void*)user_data;
+
+    SDL_PushEvent(&event);
 }
 
 static void
@@ -131,6 +170,18 @@ _tizen_app_low_battery(app_event_info_h event_info, void *user_data)
 {
     /*APP_EVENT_LOW_BATTERY*/
     SDL_Log("low battery");
+
+    SDL_Event event;
+    SDL_memset(&event, 0, sizeof(event));
+    event.type = SDL_APP_LOWBATTERY;
+    event.user.code = 0;
+    app_event_low_battery_status_e status;
+    app_event_get_low_battery_status(event_info, &status);
+    event.user.data1 = (void*)status;
+    event.user.data2 = (void*)user_data;
+
+    SDL_PushEvent(&event);
+    return;
 }
 
 static void
@@ -139,40 +190,89 @@ _tizen_app_low_memory(app_event_info_h event_info, void *user_data)
     /*APP_EVENT_LOW_MEMORY*/
     SDL_Log("low memory");
     SDL_SendAppEvent(SDL_APP_LOWMEMORY);
+    return;
+}
+
+int
+_tizen_aul_status_changed_cb(int status, void *data)
+{
+    if (status == STATUS_DYING) {
+        SDL_Log(":: STATUS_DYING");
+        SDL_SendQuit();
+        SDL_SendAppEvent(SDL_APP_TERMINATING);
+
+        SDL_DelEventWatch(_tizen_terminate_event_filter, NULL);
+    }
+
+    return 1;
+}
+
+int
+_tizen_terminate_event_filter(void *userdata, SDL_Event * event)
+{
+    if (!event) {
+        SDL_Log("Event is NULL");
+        return -1;
+    }
+
+    switch(event->type) {
+    case SDL_APP_TERMINATING: {
+            aul_remove_status_local_cb(_tizen_aul_status_changed_cb, NULL);
+            SDL_tizen_app_exit();
+        }
+        break;
+    default:
+        break;
+    }
+
+    return 1;
 }
 
 int
 SDL_tizen_app_init(int argc, char *argv[])
 {
+    int aul_ret = 0;
+
     SDL_Log( "SDL Tizen App initialize");
+
     if (tizen_appcore_initialized) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Already initialized!");
-        return 0;
+        SDL_LogError(SDL_LOG_CATEGORY_ASSERT,"Already initialized!");
+        return APP_ERROR_ALREADY_RUNNING;
     }
     tizen_appcore_initialized = 1;
     if (argc < 1 || argv == NULL) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"APP_ERROR_INVALID_PARAMETER");
-        return 0;
+        SDL_LogError(SDL_LOG_CATEGORY_ASSERT,"APP_ERROR_INVALID_PARAMETER");
+        return APP_ERROR_INVALID_PARAMETER;
     }
 
     event_callback.create = _tizen_sdl_create;
-    event_callback.terminate = _tizen_sdl_terminate;
     event_callback.pause = _tizen_sdl_pause;
     event_callback.resume = _tizen_sdl_resume;
     event_callback.app_control = _tizen_sdl_control;
 
     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, _tizen_app_low_battery, NULL);
     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, _tizen_app_low_memory, NULL);
-    ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, _tizen_app_orient_changed, NULL);
+//    ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, _tizen_app_orient_changed, NULL);
     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _tizen_app_lang_changed, NULL);
     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, _tizen_app_region_changed, NULL);
 
+    aul_ret = aul_add_status_local_cb(_tizen_aul_status_changed_cb, NULL);
+    if (aul_ret == AUL_R_ERROR) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "AUL status changed callback setting is failed.");
+        return APP_ERROR_INVALID_CONTEXT;
+    }
+
+    /* add event watch for SDL_APP_TERMINATING */
+    SDL_AddEventWatch(_tizen_terminate_event_filter, NULL);
+
     return ui_app_init(argc, argv, &event_callback, NULL, &appcore_handle);
 }
 
 void
 SDL_tizen_app_exit(void)
 {
+    SDL_Log("call ui_app_fini");
     ui_app_fini(appcore_handle);
+    appcore_handle = NULL;
 }
 #endif