[SDL_Tizen] Add SDL_Window function
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenwindow.c
index a831a90..58c0ce0 100755 (executable)
@@ -28,8 +28,6 @@
 #include "SDL_hints.h"
 #include "SDL_loadso.h"
 
-#include "SDL_tizenindicator.h"
-
 #include "SDL_tizenvideo.h"
 #include "SDL_tizentouch.h"
 #include "SDL_tizenkeyboard.h"
@@ -37,6 +35,7 @@
 #include "SDL_tizenevents_c.h"
 
 #include "SDL_tizenwindow.h"
+#include "SDL_tizenopengles.h"
 
 #include "../../events/SDL_mouse_c.h"
 #include "../../joystick/tizen/SDL_sysjoystick_c.h"
 #include "../SDL_sysvideo.h"
 #include "../../events/SDL_windowevents_c.h"
 
+#include <Ecore_Ipc.h>
+#include <unistd.h>
+#include <errno.h>
+
 enum {
     ROTATION_TYPE_NORMAL_ROTATION = 0,
     ROTATION_TYPE_PRE_ROTATION,      /* use pre-rotation */
@@ -59,6 +62,113 @@ if (!_this->tizen_pre_rotation_data->NAME) \
     return SDL_FALSE; \
 }
 
+/*SDL indicator*/
+
+
+
+Ecore_Ipc_Server *ipc = NULL;
+
+#define IPC_HEAD(_type) \
+   Ecore_Ipc_Event_Client_##_type *e = event; \
+   if (ecore_ipc_client_server_get(e->client) != ipc) \
+     return ECORE_CALLBACK_PASS_ON
+
+void SDL_ExecuteIndicatorProcess()
+{
+    _tizen_init_ecore_ipc();
+    unsigned int childPID = fork();
+    if(childPID == 0)
+    {
+        SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] child process : %d", getpid());
+        int ret = execl("/usr/apps/org.tizen.sdl_indicator/bin/sdl_indicator", "/usr/apps/org.tizen.sdl_indicator/bin/sdl_indicator", NULL);
+        if(ret==-1)
+        {
+            SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] Failed indicator process error:%s", strerror(errno));
+            kill(getpid(), SIGKILL);
+        }
+    }
+    else if(childPID==-1)
+    {
+        SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] Failed fork");
+    }
+    else
+    {
+        SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[SDL] parent process : %d", getpid());
+    }
+}
+
+void _tizen_ecore_ipc_client_send(int major, int minor, int ref, int ref_to)
+{
+    Eina_List *ipc_clients = ecore_ipc_server_clients_get(ipc);
+    Eina_List *l;
+    Ecore_Ipc_Client *cl;
+    EINA_LIST_FOREACH(ipc_clients, l, cl)
+    ecore_ipc_client_send(cl, major, minor, ref, ref_to, 0, NULL, 0);
+}
+
+static Eina_Bool _cb_client_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
+{
+    IPC_HEAD(Add);
+
+    SDL_Log("[SDL]_cb_client_add");
+    SDL_Window * window = SDL_GetVideoDevice()->windows;
+    SDL_WindowData *wind = window->driverdata;
+    wind->indicator = SDL_TRUE;
+
+    int window_w, window_h;
+    ecore_wl_screen_size_get(&window_w, &window_h);
+    _tizen_ecore_ipc_client_send(OP_RESIZE, wind->rotation, window_w, window_h);
+
+    return ECORE_CALLBACK_DONE;
+}
+
+static Eina_Bool _cb_client_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
+{
+    IPC_HEAD(Del);
+    SDL_Log("[SDL]_cb_client_del");
+    Eina_List *ipc_clients = ecore_ipc_server_clients_get(ipc);
+    Eina_List *l;
+    Ecore_Ipc_Client *cl;
+    EINA_LIST_FOREACH(ipc_clients, l, cl)
+    {
+        ecore_ipc_client_del(cl);
+    }
+
+    SDL_Window * window = SDL_GetVideoDevice()->windows;
+    SDL_WindowData *wind = window->driverdata;
+    wind->indicator = SDL_FALSE;
+    return ECORE_CALLBACK_DONE;
+}
+
+static Eina_Bool _cb_client_data(void *data, int type EINA_UNUSED, void *event)
+{
+    IPC_HEAD(Data);
+
+    Ecore_Ipc_Event_Server_Data* epcEvent = (Ecore_Ipc_Event_Server_Data*)event;
+    SDL_Log("[SDL]_cb_client_data: %d -> %d", epcEvent->major, epcEvent->minor);
+    return ECORE_CALLBACK_DONE;
+}
+
+int _tizen_init_ecore_ipc()
+{
+
+    if(!ipc)
+    {
+        if(!ecore_ipc_init())
+            SDL_Log("[SDL]Fail ecore_ipc");
+
+        ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_USER, "sdl_indicator", 0, NULL);
+
+        if(!ipc)
+            ecore_ipc_shutdown();
+
+        ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, _cb_client_add, NULL);
+        ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, _cb_client_del, NULL);
+        ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, _cb_client_data, NULL);
+    }
+    return 0;
+}
+
 static int
 _tizen_rotation_type_get()
 {
@@ -141,13 +251,39 @@ Tizen_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
 }
 
 void
+Tizen_SetWindowTitle(_THIS, SDL_Window * window)
+{
+    SDL_WindowData *wind = window->driverdata;
+    ecore_wl_window_title_set(wind->window, window->title);
+}
+
+void
 Tizen_ShowWindow(_THIS, SDL_Window *window)
 {
     SDL_WindowData *wind = window->driverdata;
+
+    if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS) && !wind->indicator)
+    {
+        SDL_ExecuteIndicatorProcess();
+    }
     ecore_wl_window_show(wind->window);
 }
 
 void
+Tizen_HideWindow(_THIS, SDL_Window *window)
+{
+    SDL_WindowData *wind = window->driverdata;
+    ecore_wl_window_hide(wind->window);
+}
+
+void
+Tizen_RaiseWindow(_THIS, SDL_Window *window)
+{
+    SDL_WindowData *wind = window->driverdata;
+    ecore_wl_window_raise(wind->window);
+}
+
+void
 Tizen_SetWindowFullscreen(_THIS, SDL_Window *window,
                           SDL_VideoDisplay *_display, SDL_bool fullscreen)
 {
@@ -155,12 +291,12 @@ Tizen_SetWindowFullscreen(_THIS, SDL_Window *window,
 }
 
 void
-Tizen_pre_rotation_set(SDL_WindowData *_this)
+Tizen_pre_rotation_set(SDL_WindowData *_this, int rotation)
 {
     tizen_wl_egl_window_rotation rot;
     if (!_this->egl_window) return;
 
-    switch (_this->rotation) {
+    switch (rotation) {
          case 90:
             rot = TIZEN_ROTATION_270;
             break;
@@ -233,11 +369,9 @@ _tizen_window_orientaiton_hint_callback(void *userdata, const char *name, const
     if (j > 0) {
         if (j == 1) {
             ecore_wl_window_rotation_preferred_rotation_set(window,wind->rotation);
-            if (wind->support_pre_rotation) {
-                Tizen_pre_rotation_set(wind);
-            }
+        }else {
+            ecore_wl_window_rotation_available_rotations_set(window, (const int*)checked, j);
         }
-        ecore_wl_window_rotation_available_rotations_set(window, (const int*)checked, j);
     }
 }
 
@@ -252,6 +386,178 @@ _tizen_window_orientation_add_hint(void *data)
     SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, _tizen_window_orientaiton_hint_callback, data);
 }
 
+static Eina_Bool
+_tizen_cb_output_transform(void *data, int type EINA_UNUSED, void *event)
+{
+    SDL_Window * window = SDL_GetVideoDevice()->windows;
+    SDL_WindowData *wind = window->driverdata;
+
+    if(!wind) return ECORE_CALLBACK_PASS_ON;
+
+    Ecore_Wl_Event_Output_Transform *ev = event;
+    Ecore_Wl_Output *output;
+
+    output = ecore_wl_window_output_find(wind->window);
+    if (output != ev->output) return ECORE_CALLBACK_PASS_ON;
+
+    wind->received_rotation = 1;
+
+
+   return ECORE_CALLBACK_PASS_ON;
+}
+
+static Eina_Bool
+_tizen_cb_ignore_output_transform(void *data, int type EINA_UNUSED, void *event EINA_UNUSED)
+{
+    SDL_Window * window = SDL_GetVideoDevice()->windows;
+    SDL_WindowData *wind = window->driverdata;
+
+    if(!wind) return ECORE_CALLBACK_PASS_ON;
+
+    wind->received_rotation = 1;
+
+   return ECORE_CALLBACK_PASS_ON;
+}
+
+void
+_tizen_output_transform_register(SDL_WindowData *wind)
+{
+    if(!wind) return;
+
+    Ecore_Wl_Output *output = ecore_wl_window_output_find(wind->window);
+    wind->output_rotation = ecore_wl_output_transform_get(output) * 90;
+
+    ecore_event_handler_add(ECORE_WL_EVENT_OUTPUT_TRANSFORM,
+                             _tizen_cb_output_transform, NULL);
+    ecore_event_handler_add(ECORE_WL_EVENT_IGNORE_OUTPUT_TRANSFORM,
+                             _tizen_cb_ignore_output_transform, NULL);
+
+}
+
+void
+_tizen_send_rotation_event(SDL_Window *window, unsigned int angle)
+{
+    SDL_Event event;
+    SDL_WindowData *wind;
+    wind = window->driverdata;
+
+    SDL_memset(&event, 0, sizeof(event));
+    event.type = SDL_ROTATEEVENT;
+    event.user.code = 0;
+    if (wind->support_pre_rotation)
+        event.user.data1 = (void*)0;
+    else
+        event.user.data1 = (void*)angle;
+    event.user.data2 = (void*)-1;
+
+    SDL_PushEvent(&event);
+    return;
+}
+
+void
+_tizen_set_window_size(SDL_Window * window, int w, int h)
+{
+    if(!window)
+    {
+        SDL_SetError("Invalid window");
+        return;
+    }
+
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+    if (!_this) {
+        SDL_SetError("Video subsystem has not been initialized");
+        return;
+    }
+
+    if (window->magic != &_this->window_magic) {
+        return;
+    }
+
+    if (w <= 0) {
+        SDL_InvalidParamError("w");
+        return;
+    }
+    if (h <= 0) {
+        SDL_InvalidParamError("h");
+        return;
+    }
+
+    /* Make sure we don't exceed any window size limits */
+    if (window->min_w && w < window->min_w)
+    {
+        w = window->min_w;
+    }
+    if (window->max_w && w > window->max_w)
+    {
+        w = window->max_w;
+    }
+    if (window->min_h && h < window->min_h)
+    {
+        h = window->min_h;
+    }
+    if (window->max_h && h > window->max_h)
+    {
+        h = window->max_h;
+    }
+
+    window->windowed.w = w;
+    window->windowed.h = h;
+
+    window->w = w;
+    window->h = h;
+}
+
+void
+_tizen_rotation_do(SDL_WindowData *wind, int rotation)
+{
+    if(!wind) return;
+
+    SDL_Window *window = SDL_GetVideoDevice()->windows;
+    if(!window) return;
+
+    int window_w, window_h;
+    if(wind->rotation == 0 || wind->rotation == 180)
+        ecore_wl_window_geometry_get(wind->window, 0, 0, &window_w, &window_h);
+    else
+        ecore_wl_window_geometry_get(wind->window, 0, 0, &window_h, &window_w);
+
+    _tizen_set_window_size(window, window_w, window_h);
+
+    if(wind->support_pre_rotation)
+        Tizen_pre_rotation_set(wind, rotation);
+
+    _tizen_send_rotation_event(window, wind->rotation);
+    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
+
+}
+
+void
+_tizen_rotate_update(SDL_WindowData *wind)
+{
+    if(!wind) return;
+
+    int screen_rotation;
+    if (ecore_wl_window_ignore_output_transform_get(wind->window))
+    {
+        screen_rotation = 0;
+    }
+    else
+    {
+        Ecore_Wl_Output *output = ecore_wl_window_output_find(wind->window);
+        screen_rotation = ecore_wl_output_transform_get(output) * 90;
+    }
+
+    //Set Screen Rotation
+    wind->output_rotation = screen_rotation;
+    if(wind->support_pre_rotation)
+        ecore_wl_window_buffer_transform_set(wind->window, wind->output_rotation / 90);
+
+    ecore_wl_window_rotation_set(wind->window, wind->rotation);
+
+    int rotation = (wind->output_rotation + wind->rotation) % 360;
+    _tizen_rotation_do(wind, rotation);
+}
+
 int
 Tizen_CreateWindow(_THIS, SDL_Window *window)
 {
@@ -267,7 +573,6 @@ Tizen_CreateWindow(_THIS, SDL_Window *window)
 
     window->driverdata = wind;
     window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */
-    window->flags |= SDL_WINDOW_BORDERLESS;     /* defalut - not support indicator*/
 
 #if SDL_VIDEO_OPENGL_EGL
     if (window->flags & SDL_WINDOW_OPENGL) {
@@ -303,15 +608,30 @@ Tizen_CreateWindow(_THIS, SDL_Window *window)
     wind->window = ecore_wl_window_new(NULL,
                                        window->x, window->y, window->w, window->h,
                                        ECORE_WL_WINDOW_BUFFER_TYPE_SHM);
+    if (!wind->window) {
+        SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Failed to create wayland window");
+        return -1;
+    }
+    _tizen_output_transform_register(wind);
+
     wind->surface = ecore_wl_window_surface_create(wind->window);
+    if (!wind->surface) {
+        SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Failed to create wayland window surface");
+        return -1;
+    }
+    ecore_wl_window_type_set(wind->window, ECORE_WL_WINDOW_TYPE_UTILITY);
+
     wind->rotation = 0;
     wind->rotation_supported = 0;
     wind->received_rotation = 0;
-    ecore_wl_window_opaque_region_set(wind->window, window->x, window->y, window->w, window->h);
 
 #if SDL_VIDEO_OPENGL_EGL
     if (window->flags & SDL_WINDOW_OPENGL) {
-        wind->egl_window = wl_egl_window_create(ecore_wl_window_surface_get(wind->window), window->w, window->h);
+
+        if(wind->output_rotation == 90 || wind->output_rotation == 270)
+            wind->egl_window = wl_egl_window_create(wind->surface, window->h, window->w);
+        else
+            wind->egl_window = wl_egl_window_create(wind->surface, window->w, window->h);
 
         /* Create the GLES window surface */
         wind->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) wind->egl_window);
@@ -320,6 +640,13 @@ Tizen_CreateWindow(_THIS, SDL_Window *window)
             return -1;
         }
 
+        if (!_this->gl_config.alpha_size) {
+            ecore_wl_window_opaque_region_set(wind->window, window->x, window->y, window->w, window->h);
+        }
+        else {
+            wl_surface_set_opaque_region(wind->surface, NULL);
+        }
+
         //Support PreRotation
         wind->support_pre_rotation = 0;
         if (_tizen_rotation_type_get() && _tizen_PreRotatotion_LoadLibrary(wind, "libwayland-egl.so")) {
@@ -327,6 +654,11 @@ Tizen_CreateWindow(_THIS, SDL_Window *window)
                 wind->support_pre_rotation = 1;
             }
         }
+        else
+        {
+            wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
+        }
+
     }
 #endif
 
@@ -350,33 +682,43 @@ Tizen_CreateWindow(_THIS, SDL_Window *window)
     // Add orientaiton hint cb
     _tizen_window_orientation_add_hint((void*)wind);
 
+    _tizen_rotate_update(wind);
+
     return 0;
 }
 
 void
 Tizen_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
 {
-//Add setting window bordered.
+    SDL_WindowData *wind = window->driverdata;
+    if(!wind->indicator)
+    {
+        SDL_ExecuteIndicatorProcess();
+    }
 }
 
 void
-_tizen_egl_window_resize(SDL_Window *window)
+_tizen_window_resize(SDL_Window *window)
 {
     SDL_WindowData *wind = window->driverdata;
     if (!wind->egl_window) {
         return;
     }
 
+    if(wind->support_pre_rotation && (wind->rotation==90 || wind->rotation==270))
+        ecore_wl_window_update_size(wind->window, window->h, window->w);
+    else
+        ecore_wl_window_update_size(wind->window, window->w, window->h);
+
     // TODO : consider to rotation status.
 #if SDL_VIDEO_OPENGL_EGL
     if (window->flags & SDL_WINDOW_OPENGL) {
-        // TODO : if window size is not FULL, we need the code about Non FullSize Window.
-        int aw, ah;
-        wl_egl_window_get_attached_size(wind->egl_window, &aw, &ah);
-        wl_egl_window_resize(wind->egl_window, aw, ah, 0, 0);
+      if(wind->output_rotation==90 || wind->output_rotation==270)
+          wl_egl_window_resize(wind->egl_window, window->h, window->w, 0, 0);
+      else
+          wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
     }
 #endif
-
 }
 
 void
@@ -386,14 +728,12 @@ _tizen_setwindowsize(SDL_Window *window)
     if (!wind->window) {
         return;
     }
-    ecore_wl_window_update_size(wind->window, window->w, window->h);
 }
 
 void
 Tizen_SetWindowSize(_THIS, SDL_Window *window)
 {
-//    _tizen_setwindowsize(window);
-//    _tizen_egl_window_resize(window);
+    _tizen_window_resize(window);
 }
 
 void
@@ -504,26 +844,6 @@ _tizen_cb_window_configure(void *data, int type EINA_UNUSED, void *event)
 }
 
 
-void
-_tizen_send_rotation_event(SDL_Window *window, unsigned int angle)
-{
-    SDL_Event event;
-    SDL_WindowData *wind;
-    wind = window->driverdata;
-
-    SDL_memset(&event, 0, sizeof(event));
-    event.type = SDL_ROTATEEVENT;
-    event.user.code = 0;
-    if (wind->support_pre_rotation)
-        event.user.data1 = (void*)0;
-    else
-        event.user.data1 = (void*)angle;
-    event.user.data2 = (void*)-1;
-
-    SDL_PushEvent(&event);
-    return;
-}
-
 Eina_Bool
 _tizen_cb_event_window_rotate(void *data, int type EINA_UNUSED, void *event)
 {
@@ -545,12 +865,9 @@ _tizen_cb_event_window_rotate(void *data, int type EINA_UNUSED, void *event)
     if (wind->rotation != ev->angle) {
         /* set ecore_wayland window rotation */
         wind->rotation = ev->angle;
-        ecore_wl_window_rotation_set(ew, ev->angle);
-        // Send Rotation Event
-        _tizen_send_rotation_event(window, ev->angle);
+        wind->received_rotation = 1;
     }
 
-    wind->received_rotation = 1;
     return ECORE_CALLBACK_PASS_ON;
 }
 
@@ -588,8 +905,6 @@ Tizen_InitWindow(_THIS)
     ecore_event_handler_add(ECORE_WL_EVENT_MOUSE_OUT,
                         _tizen_cb_event_mouse_out,_this);
 
-    data->current_thread = SDL_GetThreadID(0);
-
     return 0;
 }