modify simple-egl
[src/libhomescreen.git] / sample / simple-egl / src / simple-egl.cpp
index 8be9d2e..af39149 100644 (file)
@@ -47,7 +47,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "libwindowmanager.h"
+#include <libwindowmanager.h>
 #include <libhomescreen.hpp>
 
 #include <ilm/ivi-application-client-protocol.h>
 
 using namespace std;
 
-
+const char* log_prefix = "simple-egl";
 uint32_t g_id_ivisurf = 9009;
 long port = 1700;
 string token = string("wm");
 
 string app_name = string("Navigation");
+const char* main_role = "navigation";
 
 LibHomeScreen* hs;
 LibWindowmanager *wm;
-bool gIsDraw = false;
 
 static const struct wl_interface *types[] = {
         NULL,
@@ -149,7 +149,7 @@ struct window {
        EGLSurface egl_surface;
        struct wl_callback *callback;
        int fullscreen, opaque, buffer_size, frame_sync;
-} *gWindow;
+};
 
 static const char *vert_shader_text =
        "uniform mat4 rotation;\n"
@@ -195,7 +195,7 @@ init_egl(struct display *display, struct window *window)
 
        if (window->opaque || window->buffer_size == 16)
                config_attribs[9] = 0;
-       
+
        display->egl.dpy = weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, display->display, NULL);
        assert(display->egl.dpy);
 
@@ -224,7 +224,7 @@ init_egl(struct display *display, struct window *window)
        }
        free(configs);
        if (display->egl.conf == NULL) {
-               HMI_DEBUG("simple-egl","did not find config with buffer size %d",
+               HMI_ERROR(log_prefix,"did not find config with buffer size %d",
                        window->buffer_size);
                exit(EXIT_FAILURE);
        }
@@ -244,7 +244,7 @@ init_egl(struct display *display, struct window *window)
                        eglGetProcAddress("eglSwapBuffersWithDamageEXT");
 
        if (display->swap_buffers_with_damage)
-               HMI_DEBUG("simple-egl","has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage");
+               HMI_DEBUG(log_prefix,"has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage");
 
 }
 
@@ -272,7 +272,7 @@ create_shader(struct window *window, const char *source, GLenum shader_type)
                char log[1000];
                GLsizei len;
                glGetShaderInfoLog(shader, 1000, &len, log);
-               HMI_DEBUG("simple-egl","Error: compiling %s: %*s",
+               HMI_ERROR(log_prefix,"Error: compiling %s: %*s",
                        shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
                        len, log);
                exit(1);
@@ -301,7 +301,7 @@ init_gl(struct window *window)
                char log[1000];
                GLsizei len;
                glGetProgramInfoLog(program, 1000, &len, log);
-               HMI_DEBUG("simple-egl","Error: linking:%*s", len, log);
+               HMI_ERROR(log_prefix,"Error: linking:%*s", len, log);
                exit(1);
        }
 
@@ -327,7 +327,7 @@ create_ivi_surface(struct window *window, struct display *display)
                                               id_ivisurf, window->surface);
 
        if (window->ivi_surface == NULL) {
-               HMI_DEBUG("simple-egl","Failed to create ivi_client_surface");
+               HMI_ERROR(log_prefix,"Failed to create ivi_client_surface");
                abort();
        }
 
@@ -396,19 +396,7 @@ redraw(void *data, struct wl_callback *callback, uint32_t time)
                {  0,    0.5 }
        };
 
-       static const GLfloat verts_sub[3][2] = {
-               { -0.25, -0.5 },
-               {  0.25, -0.5 },
-               {  0,    0.5 }
-       };
-
-       static const GLfloat colors_H[3][3] = {
-               { 1, 1, 1 },
-               { 1, 1, 1 },
-               { 1, 1, 1 }
-       };
-
-       static const GLfloat colors_N[3][3] = {
+       static const GLfloat colors[3][3] = {
                { 1, 0, 0 },
                { 0, 1, 0 },
                { 0, 0, 1 }
@@ -439,7 +427,7 @@ redraw(void *data, struct wl_callback *callback, uint32_t time)
                window->benchmark_time = time;
 
        if (time - window->benchmark_time > (benchmark_interval * 1000)) {
-               HMI_DEBUG("simple-egl","%d frames in %d seconds: %f fps",
+               HMI_DEBUG(log_prefix,"%d frames in %d seconds: %f fps",
                       window->frames,
                       benchmark_interval,
                       (float) window->frames / benchmark_interval);
@@ -465,20 +453,8 @@ redraw(void *data, struct wl_callback *callback, uint32_t time)
        glClearColor(0.0, 0.0, 0.0, 0.5);
        glClear(GL_COLOR_BUFFER_BIT);
 
-       if(window->geometry.height == 1488)
-       {
-               glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
-       }
-       else
-       {
-               glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts_sub);
-       }
-       if(app_name == string("HVAC")){
-               glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_H);
-       }
-       else{
-               glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors_N);
-       }
+       glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
+       glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
        glEnableVertexAttribArray(window->gl.pos);
        glEnableVertexAttribArray(window->gl.col);
 
@@ -509,7 +485,7 @@ redraw(void *data, struct wl_callback *callback, uint32_t time)
        } else {
                eglSwapBuffers(display->egl.dpy, window->egl_surface);
        }
-    
+
        window->frames++;
 }
 
@@ -548,91 +524,39 @@ signal_int(int signum)
 }
 
 int
-init_wm(LibWindowmanager *wm)
+init_wm(LibWindowmanager *wm, struct window *window)
 {
-       HMI_DEBUG("simple-egl","called");
-       char* surfaceIdStr;
+       HMI_DEBUG(log_prefix,"called");
 
-       if (wm->init(port, token.c_str()) != 0) {
-               HMI_DEBUG("simple-egl","wm init failed. ");
-               return -1;
+       if (wm->init(port, token) != 0) {
+               HMI_ERROR(log_prefix,"wm init failed. ");
+               return -1;
        }
 
-       json_object *obj = json_object_new_object();
-       json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
-       if (wm->requestSurface(obj) != 0) {
-               HMI_DEBUG("simple-egl","wm request surface failed ");
+       g_id_ivisurf = wm->requestSurface(main_role);
+       if (g_id_ivisurf < 0) {
+               HMI_ERROR(log_prefix,"wm request surface failed ");
                return -1;
        }
+       HMI_DEBUG(log_prefix,"IVI_SURFACE_ID: %d ", g_id_ivisurf);
 
-       wm->set_event_handler(LibWindowmanager::Event_Active, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               HMI_DEBUG("simple-egl","Surface %s got activated! ", label);
-       });
-
-       wm->set_event_handler(LibWindowmanager::Event_Inactive, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               HMI_DEBUG("simple-egl","Surface %s got inactivated!", label);
-       });
-
-       wm->set_event_handler(LibWindowmanager::Event_Visible, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               HMI_DEBUG("simple-egl","Surface %s got visibled!", label);
-       });
-
-       wm->set_event_handler(LibWindowmanager::Event_Invisible, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               HMI_DEBUG("simple-egl","Surface %s got invisibled!", label);
-               gIsDraw = false;
-       });
-
-       wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               const char *area = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingArea));
-
-               HMI_DEBUG("simple-egl","Surface %s got syncDraw! Area: %s. ", label, area);
-               if ((wm->kStrLayoutNormal + "." + wm->kStrAreaFull) == std::string(area)) {
-                       HMI_DEBUG("simple-egl","Layout:%s x:%d y:%d w:%d h:%d ", area, 0, 0, 1080, 1488);
-                       wl_egl_window_resize(gWindow->native, 1080, 1488, 0, 0);
-                       gWindow->geometry.width = 1080;
-                       gWindow->geometry.height = 1488;
-               }
-               else if ((wm->kStrLayoutSplit + "." + wm->kStrAreaMain) == std::string(area) ||
-                                (wm->kStrLayoutSplit + "." + wm->kStrAreaSub) == std::string(area)) {
-                       HMI_DEBUG("simple-egl","Layout:%s x:%d y:%d w:%d h:%d ", area, 0, 0, 1080, 744);
-                       wl_egl_window_resize(gWindow->native, 1080, 744, 0, 0);
-                       gWindow->geometry.width = 1080;
-                       gWindow->geometry.height = 744;
-               }
-
-               if (!gWindow->fullscreen)
-                       gWindow->window_size = gWindow->geometry;
-               gIsDraw = true;
-               json_object *obj = json_object_new_object();
-               json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
+       WMHandler wmh;
+       wmh.on_visible = [](const char* role, bool visible){
+               // Sample code if user uses visible event
+               HMI_DEBUG(log_prefix, "role: %s, visible: %s", role, visible ? "true" : "false");
+       };
+       wmh.on_sync_draw = [wm, window](const char* role, const char* area, Rect rect) {
 
-        wm->endDraw(obj);
-    });
+               HMI_DEBUG(log_prefix,"Surface %s got syncDraw! Area: %s. w:%d, h:%d", role, area, rect.width(), rect.height());
 
-       wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [wm](json_object *object) {
-               const char *label = json_object_get_string(
-                       json_object_object_get(object, wm->kKeyDrawingName));
-               HMI_DEBUG("simple-egl","Surface %s got flushdraw! ", label);
-       });
+               wl_egl_window_resize(window->native, rect.width(), rect.height(), 0, 0);
+               window->geometry.width  = rect.width();
+               window->geometry.height = rect.height();
 
-       do
-       {
-        surfaceIdStr = getenv("QT_IVI_SURFACE_ID");
-       } while (surfaceIdStr == NULL);
+               wm->endDraw(role);
+       };
 
-       g_id_ivisurf = atoi(surfaceIdStr);
-       HMI_DEBUG("simple-egl","IVI_SURFACE_ID: %d ", g_id_ivisurf);
+       wm->setEventHandler(wmh);
 
        return 0;
 }
@@ -641,29 +565,21 @@ int
 init_hs(LibHomeScreen* hs){
        if(hs->init(port, token)!=0)
        {
-               HMI_DEBUG("simple-egl","homescreen init failed. ");
+               HMI_ERROR(log_prefix,"homescreen init failed. ");
                return -1;
        }
 
-       hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](json_object *object){
-               const char *application_name = json_object_get_string(
-                       json_object_object_get(object, "application_name"));
-               HMI_DEBUG("simple-egl","Event_TapShortcut application_name = %s ", application_name);
-               if(strcmp(application_name, app_name.c_str()) == 0)
-               {
-                       HMI_DEBUG("simple-egl","try to activesurface %s ", app_name.c_str());
-                       json_object *obj = json_object_new_object();
-                       json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
-                       json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
-                       gIsDraw = false;
-                       wm->activateSurface(obj);
-               }
-       });
+       hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [hs](json_object *object){
+               HMI_DEBUG("simple-egl","try to activeWindow %s ", app_name.c_str());
 
-       hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](json_object *object){
-               const char *display_message = json_object_get_string(
-                       json_object_object_get(object, "display_message"));
-        HMI_DEBUG("simple-egl","Event_OnScreenMessage display_message = %s ", display_message);
+               struct json_object *param_obj = json_object_object_get(object, "parameter");
+               const char *area = json_object_get_string(
+                       json_object_object_get(param_obj, "area"));
+               // Application should call LibWindowmanager::activateWindow() in showWindow handler
+               if(area == nullptr)
+                       wm->activateWindow(main_role, "normal.full");
+               else
+                       wm->activateWindow(main_role, area);
        });
 
        return 0;
@@ -683,17 +599,13 @@ main(int argc, char **argv)
        window.window_size = window.geometry;
        window.buffer_size = 32;
        window.frame_sync = 1;
-       gWindow = &window;
 
-       if(argc > 2)
-       {
-               if(string(argv[0]).find("hvac") != string::npos)
-                       app_name = string("HVAC");
+       if(argc > 2){
                port = strtol(argv[1], NULL, 10);
                token = argv[2];
        }
 
-       HMI_DEBUG("simple-egl","app_name: %s, port: %d, token: %s. ", app_name.c_str(), port, token.c_str());
+       HMI_DEBUG(log_prefix,"main_role: %s, port: %d, token: %s. ", main_role, port, token.c_str());
 
        display.display = wl_display_connect(NULL);
        assert(display.display);
@@ -707,9 +619,7 @@ main(int argc, char **argv)
        init_egl(&display, &window);
 
        wm = new LibWindowmanager();
-       hs = new LibHomeScreen();
-       
-       if(init_wm(wm)!=0){
+       if(init_wm(wm, &window)!=0){
                fini_egl(&display);
                if (display.ivi_application)
                        ivi_application_destroy(display.ivi_application);
@@ -720,6 +630,7 @@ main(int argc, char **argv)
                return -1;
        }
 
+       hs = new LibHomeScreen();
        if(init_hs(hs)!=0){
                fini_egl(&display);
                if (display.ivi_application)
@@ -740,12 +651,10 @@ main(int argc, char **argv)
        sigint.sa_flags = SA_RESETHAND;
        sigaction(SIGINT, &sigint, NULL);
 
-       wl_display_dispatch_pending(display.display);
-       redraw(&window, NULL, 0);
-       json_object *obj = json_object_new_object();
-       json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
-       json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
-       wm->activateSurface(obj);
+       eglSwapBuffers(window.display->egl.dpy, window.egl_surface);
+
+       // wm->activateWindow(main_role);
+       hs->publishSubscription();
 
        /* The mainloop here is a little subtle.  Redrawing will cause
         * EGL to read events so we can just call
@@ -753,17 +662,10 @@ main(int argc, char **argv)
         * queued up as a side effect. */
        while (running) {
                wl_display_dispatch_pending(display.display);
-               if(!gIsDraw) {
-                       usleep(100000);
-                       continue;
-               }
-               else
-               {
-                       redraw(&window, NULL, 0);
-               }
+               redraw(&window, NULL, 0);
        }
 
-       HMI_DEBUG("simple-egl","simple-egl exiting! ");
+       HMI_DEBUG(log_prefix,"simple-egl exiting! ");
 
        destroy_surface(&window);
        fini_egl(&display);