Fix the bug of livebox_get_pdsize function.
[platform/framework/web/livebox-viewer.git] / src / livebox.c
index e3fc6cb..d880101 100644 (file)
@@ -110,6 +110,11 @@ static inline void default_pd_destroyed_cb(struct livebox *handler, int ret, voi
        DbgPrint("Default PD destroyed event handler: %d\n", ret);
 }
 
+static inline void default_lb_size_changed_cb(struct livebox *handler, int ret, void *data)
+{
+       DbgPrint("Default LB size changed event handler: %d\n", ret);
+}
+
 static inline __attribute__((always_inline)) struct cb_info *create_cb_info(ret_cb_t cb, void *data)
 {
        struct cb_info *info;
@@ -157,9 +162,14 @@ static void resize_cb(struct livebox *handler, const struct packet *result, void
         * So the user can only get the resized value(result) from the first update event
         * after this request.
         */
-
-       if (cb)
+       if (ret == 0) {
+               DbgPrint("Resize request is done, prepare the size changed event\n");
+               handler->size_changed_cb = cb;
+               handler->size_cbdata = cbdata;
+       } else {
+               DbgPrint("Resize request is failed: %d\n", ret);
                cb(handler, ret, cbdata);
+       }
 }
 
 static void text_signal_cb(struct livebox *handler, const struct packet *result, void *data)
@@ -605,6 +615,13 @@ EAPI struct livebox *livebox_add_with_size(const char *pkgname, const char *cont
                return NULL;
        }
 
+       if (livebox_service_is_enabled(handler->pkgname) == 0) {
+               DbgPrint("Livebox [%s](%s) is disabled package\n", handler->pkgname, pkgname);
+               free(handler->pkgname);
+               free(handler);
+               return NULL;
+       }
+
        if (content) {
                handler->content = strdup(content);
                if (!handler->content) {
@@ -712,6 +729,9 @@ EAPI int livebox_set_period(struct livebox *handler, double period, ret_cb_t cb,
                return -EALREADY;
        }
 
+       if (handler->period_changed_cb)
+               DbgPrint("Already requested\n");
+
        packet = packet_create("set_period", "ssd", handler->pkgname, handler->id, period);
        if (!packet) {
                ErrPrint("Failed to build a packet %s\n", handler->pkgname);
@@ -871,12 +891,18 @@ EAPI int livebox_resize(struct livebox *handler, int type, ret_cb_t cb, void *da
                return -EALREADY;
        }
 
+       if (handler->size_changed_cb)
+               DbgPrint("Already pended\n");
+
        packet = packet_create("resize", "ssii", handler->pkgname, handler->id, w, h);
        if (!packet) {
                ErrPrint("Failed to build param\n");
                return -EFAULT;
        }
 
+       if (!cb)
+               cb = default_lb_size_changed_cb;
+
        return master_rpc_async_request(handler, packet, 0, resize_cb, create_cb_info(cb, data));
 }
 
@@ -993,6 +1019,34 @@ EAPI int livebox_create_pd_with_position(struct livebox *handler, double x, doub
        return master_rpc_async_request(handler, packet, 0, pd_create_cb, create_cb_info(cb, data));
 }
 
+EAPI int livebox_move_pd(struct livebox *handler, double x, double y)
+{
+       struct packet *packet;
+
+       if (!handler) {
+               ErrPrint("Handler is NIL\n");
+               return -EINVAL;
+       }
+
+       if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
+               ErrPrint("Handler is not valid\n");
+               return -EINVAL;
+       }
+
+       if (!handler->is_pd_created) {
+               DbgPrint("PD is not created\n");
+               return -EINVAL;
+       }
+
+       packet = packet_create_noack("pd_move", "ssdd", handler->pkgname, handler->id, x, y);
+       if (!packet) {
+               ErrPrint("Failed to build param\n");
+               return -EFAULT;
+       }
+
+       return master_rpc_request_only(handler, packet);
+}
+
 EAPI int livebox_activate(const char *pkgname, ret_cb_t cb, void *data)
 {
        struct packet *packet;
@@ -1044,7 +1098,8 @@ EAPI int livebox_content_event(struct livebox *handler, enum content_event_type
 {
        int w;
        int h;
-       const char *cmd;
+       char cmd[20] = { '\0', };
+       char *ptr = cmd;
 
        if (!handler) {
                ErrPrint("Handler is NIL\n");
@@ -1056,185 +1111,96 @@ EAPI int livebox_content_event(struct livebox *handler, enum content_event_type
                return -EINVAL;
        }
 
-       switch (type) {
-       case LB_MOUSE_DOWN:
-               if (!handler->lb.mouse_event) {
-                       ErrPrint("Box is not support the mouse event\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->lb.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "lb_mouse_down";
-               w = handler->lb.width;
-               h = handler->lb.height;
-
-               handler->lb.x = x;
-               handler->lb.y = y;
-               break;
-
-       case LB_MOUSE_UP:
-               if (!handler->lb.mouse_event) {
-                       ErrPrint("Box is not support the mouse event\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->lb.data.fb) {
-                       ErrPrint("Handler is not valid\n");
+       if (type & CONTENT_EVENT_PD_MASK) {
+               if (!handler->is_pd_created) {
+                       ErrPrint("PD is not created\n");
                        return -EINVAL;
                }
 
-               cmd = "lb_mouse_up";
-               w = handler->lb.width;
-               h = handler->lb.height;
-               break;
+               if (type & CONTENT_EVENT_MOUSE_MASK) {
+                       if (!handler->pd.data.fb) {
+                               ErrPrint("Handler is not valid\n");
+                               return -EINVAL;
+                       }
 
-       case LB_MOUSE_MOVE:
-               if (!handler->lb.mouse_event) {
-                       ErrPrint("Box is not support the mouse event\n");
-                       return -EINVAL;
+                       if (type & CONTENT_EVENT_MOUSE_MOVE) {
+                               if (fabs(x - handler->pd.x) < MINIMUM_EVENT && fabs(y - handler->pd.y) < MINIMUM_EVENT)
+                                       return -EBUSY;
+                       }
                }
 
-               if (!handler->lb.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
+               w = handler->pd.width;
+               h = handler->pd.height;
+               handler->pd.x = x;
+               handler->pd.y = y;
+               *ptr++ = 'p';
+               *ptr++ = 'd';
+       } else {
+               if (type & CONTENT_EVENT_MOUSE_MASK) {
+                       if (!handler->lb.mouse_event) {
+                               ErrPrint("Box is not support the mouse event\n");
+                               return -EINVAL;
+                       }
+
+                       if (!handler->lb.data.fb) {
+                               ErrPrint("Handler is not valid\n");
+                               return -EINVAL;
+                       }
+
+                       if (type & CONTENT_EVENT_MOUSE_MOVE) {
+                               if (fabs(x - handler->lb.x) < MINIMUM_EVENT && fabs(y - handler->lb.y) < MINIMUM_EVENT)
+                                       return -EBUSY;
+                       }
                }
 
-               if (fabs(x - handler->lb.x) < MINIMUM_EVENT && fabs(y - handler->lb.y) < MINIMUM_EVENT)
-                       return -EBUSY;
-
-               cmd = "lb_mouse_move";
                w = handler->lb.width;
                h = handler->lb.height;
-
                handler->lb.x = x;
                handler->lb.y = y;
-               break;
-
-       case LB_MOUSE_ENTER:
-               if (!handler->lb.mouse_event) {
-                       ErrPrint("Box is not support the mouse event\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->lb.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
+               *ptr++ = 'l';
+               *ptr++ = 'b';
+       }
 
-               cmd = "lb_mouse_enter";
-               w = handler->lb.width;
-               h = handler->lb.height;
+       switch ((type & ~CONTENT_EVENT_PD_MASK)) {
+       case CONTENT_EVENT_ACCESS_READ | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_read");
                break;
-
-       case LB_MOUSE_LEAVE:
-               if (!handler->lb.mouse_event) {
-                       ErrPrint("Box is not support the mouse event\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->lb.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "lb_mouse_leave";
-               w = handler->lb.width;
-               h = handler->lb.height;
+       case CONTENT_EVENT_ACCESS_READ_PREV | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_read_prev");
                break;
-
-       case PD_MOUSE_ENTER:
-               if (!handler->is_pd_created) {
-                       ErrPrint("PD is not created\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->pd.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "pd_mouse_enter";
-               w = handler->pd.width;
-               h = handler->pd.height;
+       case CONTENT_EVENT_ACCESS_READ_NEXT | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_read_next");
                break;
-
-       case PD_MOUSE_LEAVE:
-               if (!handler->is_pd_created) {
-                       ErrPrint("PD is not created\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->pd.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "pd_mouse_leave";
-               w = handler->pd.width;
-               h = handler->pd.height;
+       case CONTENT_EVENT_ACCESS_ACTIVATE | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_activate");
                break;
-
-       case PD_MOUSE_DOWN:
-               if (!handler->is_pd_created) {
-                       ErrPrint("PD is not created\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->pd.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "pd_mouse_down";
-               w = handler->pd.width;
-               h = handler->pd.height;
-
-               handler->pd.x = x;
-               handler->pd.y = y;
+       case CONTENT_EVENT_ACCESS_UP | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_up");
                break;
-
-       case PD_MOUSE_MOVE:
-               if (!handler->is_pd_created) {
-                       ErrPrint("PD is not created\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->pd.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               if (fabs(x - handler->pd.x) < MINIMUM_EVENT && fabs(y - handler->pd.y) < MINIMUM_EVENT)
-                       return -EBUSY;
-
-               cmd = "pd_mouse_move";
-               w = handler->pd.width;
-               h = handler->pd.height;
-
-               handler->pd.x = x;
-               handler->pd.y = y;
+       case CONTENT_EVENT_ACCESS_DOWN | CONTENT_EVENT_ACCESS_MASK:
+               strcpy(ptr, "_access_down");
                break;
-
-       case PD_MOUSE_UP:
-               if (!handler->is_pd_created) {
-                       ErrPrint("PD is not created\n");
-                       return -EINVAL;
-               }
-
-               if (!handler->pd.data.fb) {
-                       ErrPrint("Handler is not valid\n");
-                       return -EINVAL;
-               }
-
-               cmd = "pd_mouse_up";
-               w = handler->pd.width;
-               h = handler->pd.height;
+       case CONTENT_EVENT_MOUSE_ENTER | CONTENT_EVENT_MOUSE_MASK:
+               strcpy(ptr, "_mouse_enter");
+               break;
+       case CONTENT_EVENT_MOUSE_LEAVE | CONTENT_EVENT_MOUSE_MASK:
+               strcpy(ptr, "_mouse_leave");
+               break;
+       case CONTENT_EVENT_MOUSE_UP | CONTENT_EVENT_MOUSE_MASK:
+               strcpy(ptr, "_mouse_up");
+               break;
+       case CONTENT_EVENT_MOUSE_DOWN | CONTENT_EVENT_MOUSE_MASK:
+               strcpy(ptr, "_mouse_down");
+               break;
+       case CONTENT_EVENT_MOUSE_MOVE | CONTENT_EVENT_MOUSE_MASK:
+               strcpy(ptr, "_mouse_move");
+               break;
+       case CONTENT_EVENT_KEY_DOWN | CONTENT_EVENT_KEY_MASK:
+               strcpy(ptr, "_key_down");
+               break;
+       case CONTENT_EVENT_KEY_UP | CONTENT_EVENT_KEY_MASK:
+               strcpy(ptr, "_key_up");
                break;
-
        default:
                ErrPrint("Invalid event type\n");
                return -EINVAL;
@@ -1282,20 +1248,13 @@ EAPI int livebox_get_pdsize(struct livebox *handler, int *w, int *h)
        if (!h)
                h = &_h;
 
-       *w = handler->pd.width;
-       *h = handler->pd.height;
-
-       switch (handler->pd.type) {
-       case _PD_TYPE_BUFFER:
-       case _PD_TYPE_SCRIPT:
-               if (!handler->is_pd_created) {
-                       DbgPrint("Buffer is not created yet - reset size\n");
-                       *w = 0;
-                       *h = 0;
-               }
-               break;
-       default:
-               break;
+       if (!handler->is_pd_created) {
+               DbgPrint("Buffer is not created yet [%dx%d]\n", *w, *h);
+               *w = handler->pd.default_width;
+               *h = handler->pd.default_height;
+       } else {
+               *w = handler->pd.width;
+               *h = handler->pd.height;
        }
 
        return 0;
@@ -1359,6 +1318,9 @@ EAPI int livebox_set_group(struct livebox *handler, const char *cluster, const c
                return -EALREADY;
        }
 
+       if (handler->group_changed_cb)
+               DbgPrint("Already sent\n");
+
        packet = packet_create("change_group", "ssss", handler->pkgname, handler->id, cluster, category);
        if (!packet) {
                ErrPrint("Failed to build a param\n");
@@ -1872,6 +1834,9 @@ EAPI int livebox_set_pinup(struct livebox *handler, int flag, ret_cb_t cb, void
                return -EALREADY;
        }
 
+       if (handler->pinup_cb)
+               DbgPrint("Already sent\n");
+
        packet = packet_create("pinup_changed", "ssi", handler->pkgname, handler->id, flag);
        if (!packet) {
                ErrPrint("Failed to build a param\n");
@@ -2045,6 +2010,27 @@ EAPI int livebox_unsubscribe_group(const char *cluster, const char *category)
        return master_rpc_request_only(NULL, packet);
 }
 
+EAPI int livebox_refresh(struct livebox *handler)
+{
+       struct packet *packet;
+
+       if (!handler) {
+               ErrPrint("Hnalder is NIL\n");
+               return -EINVAL;
+       }
+
+       if (handler->state != CREATE || !handler->id)
+               return -EINVAL;
+
+       packet = packet_create_noack("update", "ss", handler->pkgname, handler->id);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return -EFAULT;
+       }
+
+       return master_rpc_request_only(handler, packet);
+}
+
 EAPI int livebox_refresh_group(const char *cluster, const char *category)
 {
        struct packet *packet;
@@ -2159,6 +2145,12 @@ void lb_set_pdsize(struct livebox *handler, int w, int h)
        handler->pd.height = h;
 }
 
+void lb_set_default_pdsize(struct livebox *handler, int w, int h)
+{
+       handler->pd.default_width = w;
+       handler->pd.default_height = h;
+}
+
 void lb_invoke_fault_handler(enum livebox_fault_type event, const char *pkgname, const char *file, const char *func)
 {
        struct dlist *l;
@@ -2569,4 +2561,30 @@ int lb_send_delete(struct livebox *handler, ret_cb_t cb, void *data)
        return master_rpc_async_request(handler, packet, 0, del_ret_cb, create_cb_info(cb, data));
 }
 
+EAPI int livebox_client_paused(void)
+{
+       struct packet *packet;
+
+       packet = packet_create_noack("client_paused", "d", util_timestamp());
+       if (!packet) {
+               ErrPrint("Failed to create a pause packet\n");
+               return -EFAULT;
+       }
+
+       return master_rpc_request_only(NULL, packet);
+}
+
+EAPI int livebox_client_resumed(void)
+{
+       struct packet *packet;
+
+       packet = packet_create_noack("client_resumed", "d", util_timestamp());
+       if (!packet) {
+               ErrPrint("Failed to create a resume packet\n");
+               return -EFAULT;
+       }
+
+       return master_rpc_request_only(NULL, packet);
+}
+
 /* End of a file */