2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup, strerror */
21 #include <sys/types.h>
29 #include <com-core_packet.h>
33 #include <package-manager.h>
35 #include <vconf-keys.h>
40 #include "livebox-service.h"
42 #define EAPI __attribute__((visibility("default")))
43 #define DEFAULT_TIMEOUT 2.0
46 static struct supported_size_list {
49 } SIZE_LIST[NR_OF_SIZE_LIST] = {
50 { 172, 172 }, /*!< 1x1 */
51 { 348, 172 }, /*!< 2x1 */
52 { 348, 348 }, /*!< 2x2 */
53 { 700, 172 }, /*!< 4x1 */
54 { 700, 348 }, /*!< 4x2 */
55 { 700, 520 }, /*!< 4x3 */
56 { 700, 700 }, /*!< 4x4 */
57 { 207, 207 }, /*!< 21x21 */
58 { 645, 207 }, /*!< 23x21 */
59 { 645, 645 }, /*!< 23x23 */
65 const char *conf_file;
70 .dbfile = "/opt/dbspace/.livebox.db",
71 .conf_file = "/usr/share/data-provider-master/resolution.ini",
76 static inline int update_info(int width_type, int height_type, int width, int height)
80 if (width_type == 1 && height_type == 1) {
81 DbgPrint("1x1 Updated to %dx%d\n", width, height);
83 } else if (width_type == 2 && height_type == 1) {
84 DbgPrint("2x1 Updated to %dx%d\n", width, height);
86 } else if (width_type == 2 && height_type == 2) {
87 DbgPrint("2x2 Updated to %dx%d\n", width, height);
89 } else if (width_type == 4 && height_type == 1) {
90 DbgPrint("4x1 Updated to %dx%d\n", width, height);
92 } else if (width_type == 4 && height_type == 2) {
93 DbgPrint("4x2 Updated to %dx%d\n", width, height);
95 } else if (width_type == 4 && height_type == 3) {
96 DbgPrint("4x3 Updated to %dx%d\n", width, height);
98 } else if (width_type == 4 && height_type == 4) {
99 DbgPrint("4x4 Updated to %dx%d\n", width, height);
101 } else if (width_type == 21 && height_type == 21) {
102 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
104 } else if (width_type == 23 && height_type == 21) {
105 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
107 } else if (width_type == 23 && height_type == 23) {
108 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
111 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
115 SIZE_LIST[idx].w = width;
116 SIZE_LIST[idx].h = height;
120 static inline int update_from_file(void)
128 char buffer[MAX_COLUMN];
142 fp = fopen(s_info.conf_file, "r");
144 ErrPrint("Open failed: %s\n", strerror(errno));
154 if (idx == MAX_COLUMN) {
155 ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
161 if (isspace(ch) || ch == EOF)
176 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
177 ErrPrint("Invalid syntax: [%s]\n", buffer);
181 } else if (ch == '=') {
184 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
185 ErrPrint("Invalid syntax: [%s]\n", buffer);
189 } else if (ch == EOF) {
190 ErrPrint("Invalid Syntax\n");
201 if (isspace(ch) || ch == EOF)
209 if (isspace(ch) || ch == EOF) {
213 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
214 ErrPrint("Invalid syntax: [%s]\n", buffer);
216 } else if (ch == EOF) {
217 updated += update_info(width_type, height_type, width, height);
224 updated += update_info(width_type, height_type, width, height);
229 if (ch == '\n' || ch == '\r' || ch == '\f')
233 if (ch == '\n' || ch == '\r' || ch == '\f')
237 ErrPrint("Unknown status. couldn't be reach to here\n");
243 return NR_OF_SIZE_LIST - updated;
246 static int update_resolution(void)
258 if (s_info.res_resolved)
261 disp = XOpenDisplay(NULL);
263 ErrPrint("Failed to open a display\n");
267 root = XDefaultRootWindow(disp);
268 if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
273 if (update_from_file() == 0)
274 DbgPrint("Resolution info is all updated by file\n");
276 DbgPrint("Screen resolution: %dx%d\n", width, height);
277 for (i = 0; i < NR_OF_SIZE_LIST; i++) {
278 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
279 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)height / 1280.0f);
280 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
284 s_info.res_resolved = 1;
288 static inline sqlite3 *open_db(void)
292 if (!s_info.handle) {
295 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
296 if (ret != SQLITE_OK) {
297 ErrPrint("Failed to open a DB\n");
301 handle = s_info.handle;
307 static inline void close_db(sqlite3 *handle)
310 db_util_close(handle);
313 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
318 case LB_SIZE_TYPE_1x1: /*!< 172x172 */
321 case LB_SIZE_TYPE_2x1: /*!< 348x172 */
324 case LB_SIZE_TYPE_2x2: /*!< 348x348 */
327 case LB_SIZE_TYPE_4x1: /*!< 700x172 */
330 case LB_SIZE_TYPE_4x2: /*!< 700x348 */
333 case LB_SIZE_TYPE_4x3: /*!< 700x520 */
336 case LB_SIZE_TYPE_4x4: /*!< 700x700 */
339 case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
342 case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
345 case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
352 if (update_resolution() < 0)
353 ErrPrint("Failed to update resolution\n");
355 *width = SIZE_LIST[idx].w;
356 *height = SIZE_LIST[idx].h;
360 EAPI int livebox_service_trigger_update(const char *pkgname, const char *cluster, const char *category)
362 struct packet *packet;
363 struct packet *result;
367 ErrPrint("Invalid argument\n");
371 if (access("/tmp/.live.paused", R_OK) == 0) {
372 DbgPrint("Provider is paused\n");
377 cluster = "user,created";
380 category = "default";
382 packet = packet_create("service_update", "sss", pkgname, cluster, category);
384 ErrPrint("Failed to create a packet for service_update\n");
388 result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
389 packet_unref(packet);
392 if (packet_get(result, "i", &ret) != 1) {
393 ErrPrint("Failed to parse a result packet\n");
397 packet_unref(result);
399 ErrPrint("Failed to get result packet\n");
406 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
422 ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
423 if (ret != SQLITE_OK) {
424 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
430 while (sqlite3_step(stmt) == SQLITE_ROW) {
431 appid = (char *)sqlite3_column_text(stmt, 0);
432 if (!appid || !strlen(appid)) {
433 ErrPrint("APPID is not valid\n");
437 pkgid = (char *)sqlite3_column_text(stmt, 1);
438 if (!pkgid || !strlen(pkgid)) {
439 ErrPrint("pkgid is not valid\n");
443 is_prime = sqlite3_column_int(stmt, 2);
447 if (cb(appid, pkgid, is_prime, data) < 0) {
448 DbgPrint("Callback stopped package crawling\n");
454 sqlite3_finalize(stmt);
461 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
468 if (!types || !cnt || !pkgid)
475 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
476 if (ret != SQLITE_OK) {
477 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
482 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
483 if (ret != SQLITE_OK) {
484 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
486 sqlite3_finalize(stmt);
491 if (*cnt > NR_OF_SIZE_LIST)
492 *cnt = NR_OF_SIZE_LIST;
495 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
496 size = sqlite3_column_int(stmt, 0);
503 sqlite3_finalize(stmt);
510 static inline char *cur_locale(void)
513 language = vconf_get_str(VCONFKEY_LANGSET);
530 language = strdup("en-us");
532 ErrPrint("Heap: %s\n", strerror(errno));
538 static inline char *get_default_name(const char *pkgid)
549 ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
550 if (ret != SQLITE_OK) {
551 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
556 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
557 if (ret != SQLITE_OK) {
558 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
562 ret = sqlite3_step(stmt);
563 if (ret == SQLITE_ROW) {
566 tmp = (const char *)sqlite3_column_text(stmt, 0);
567 if (tmp && strlen(tmp)) {
570 ErrPrint("Heap: %s\n", strerror(errno));
576 sqlite3_finalize(stmt);
581 static inline char *get_default_icon(const char *pkgid)
592 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
593 if (ret != SQLITE_OK) {
594 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
599 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
600 if (ret != SQLITE_OK) {
601 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
605 ret = sqlite3_step(stmt);
606 if (ret == SQLITE_ROW) {
609 tmp = (const char *)sqlite3_column_text(stmt, 0);
610 if (tmp && strlen(tmp)) {
613 ErrPrint("Heap: %s\n", strerror(errno));
619 sqlite3_finalize(stmt);
624 EAPI char *livebox_service_content(const char *pkgid)
628 char *content = NULL;
635 ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
636 if (ret != SQLITE_OK) {
637 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
642 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
643 if (ret != SQLITE_OK) {
644 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
648 ret = sqlite3_step(stmt);
649 if (ret == SQLITE_ROW) {
652 tmp = (const char *)sqlite3_column_text(stmt, 0);
653 if (tmp && strlen(tmp)) {
654 content = strdup(tmp);
656 ErrPrint("Heap: %s\n", strerror(errno));
662 sqlite3_finalize(stmt);
667 EAPI char *livebox_service_setup_appid(const char *lbid)
678 ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
679 if (ret != SQLITE_OK) {
680 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
686 ret = sqlite3_bind_text(stmt, 1, lbid, -1, NULL);
687 if (ret != SQLITE_OK) {
688 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
692 ret = sqlite3_step(stmt);
693 if (ret == SQLITE_ROW) {
696 tmp = (const char *)sqlite3_column_text(stmt, 0);
697 if (!tmp || !strlen(tmp))
702 ErrPrint("Error: %s\n", strerror(errno));
707 sqlite3_finalize(stmt);
712 EAPI int livebox_service_nodisplay(const char *pkgid)
722 ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
723 if (ret != SQLITE_OK) {
724 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
729 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
730 if (ret != SQLITE_OK) {
731 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
736 ret = sqlite3_step(stmt);
737 if (ret == SQLITE_ROW)
738 ret = !!sqlite3_column_int(stmt, 0);
744 sqlite3_finalize(stmt);
749 EAPI int livebox_service_mouse_event(const char *pkgid)
759 ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
760 if (ret != SQLITE_OK) {
761 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
766 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
767 if (ret != SQLITE_OK) {
768 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
773 ret = sqlite3_step(stmt);
774 if (ret == SQLITE_ROW)
775 ret = !!sqlite3_column_int(stmt, 0);
781 sqlite3_finalize(stmt);
786 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
791 char *preview = NULL;
797 ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
798 if (ret != SQLITE_OK) {
799 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
804 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
805 if (ret != SQLITE_OK) {
806 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
810 ret = sqlite3_bind_int(stmt, 2, size_type);
811 if (ret != SQLITE_OK) {
812 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
816 ret = sqlite3_step(stmt);
817 if (ret == SQLITE_ROW) {
819 tmp = (const char *)sqlite3_column_text(stmt, 0);
820 if (tmp && strlen(tmp)) {
821 preview = strdup(tmp);
823 ErrPrint("Heap: %s\n", strerror(errno));
829 sqlite3_finalize(stmt);
834 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
843 language = strdup(lang);
845 ErrPrint("Heap: %s\n", strerror(errno));
849 language = cur_locale();
860 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
861 if (ret != SQLITE_OK) {
862 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
868 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
869 if (ret != SQLITE_OK) {
870 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
874 ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
875 if (ret != SQLITE_OK) {
876 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
880 ret = sqlite3_step(stmt);
881 if (ret == SQLITE_ROW) {
883 tmp = (const char *)sqlite3_column_text(stmt, 0);
884 if (!tmp || !strlen(tmp)) {
885 icon = get_default_icon(pkgid);
889 ErrPrint("Heap: %s\n", strerror(errno));
892 icon = get_default_icon(pkgid);
897 sqlite3_finalize(stmt);
903 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
912 language = strdup(lang);
914 ErrPrint("Error: %s\n", strerror(errno));
918 language = cur_locale();
929 ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
930 if (ret != SQLITE_OK) {
931 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
937 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
938 if (ret != SQLITE_OK) {
939 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
943 ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
944 if (ret != SQLITE_OK) {
945 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
949 ret = sqlite3_step(stmt);
950 if (ret == SQLITE_ROW) {
952 tmp = (const char *)sqlite3_column_text(stmt, 0);
953 if (!tmp || !strlen(tmp)) {
954 name = get_default_name(pkgid);
958 ErrPrint("Heap: %s\n", strerror(errno));
961 name = get_default_name(pkgid);
966 sqlite3_finalize(stmt);
972 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
979 if (!w || !h || !cnt || !pkgid)
986 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
987 if (ret != SQLITE_OK) {
988 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
993 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
994 if (ret != SQLITE_OK) {
995 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
997 sqlite3_finalize(stmt);
1002 if (*cnt > NR_OF_SIZE_LIST)
1003 *cnt = NR_OF_SIZE_LIST;
1006 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1007 size = sqlite3_column_int(stmt, 0);
1008 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1012 sqlite3_reset(stmt);
1013 sqlite3_finalize(stmt);
1020 EAPI char *livebox_service_libexec(const char *pkgid)
1037 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1038 if (ret != SQLITE_OK) {
1039 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1043 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1044 if (ret != SQLITE_OK) {
1045 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1046 sqlite3_finalize(stmt);
1050 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1051 if (ret != SQLITE_OK) {
1052 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1053 sqlite3_finalize(stmt);
1057 if (sqlite3_step(stmt) != SQLITE_ROW) {
1058 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1059 sqlite3_reset(stmt);
1060 sqlite3_finalize(stmt);
1062 libexec = util_conf_get_libexec(pkgid);
1063 DbgPrint("Fallback to conf checker: %s\n", libexec);
1067 appid = (char *)sqlite3_column_text(stmt, 0);
1068 if (!appid || !strlen(appid)) {
1069 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1070 sqlite3_reset(stmt);
1071 sqlite3_finalize(stmt);
1075 path = (char *)sqlite3_column_text(stmt, 1);
1076 if (!path || !strlen(path)) {
1077 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1078 sqlite3_reset(stmt);
1079 sqlite3_finalize(stmt);
1083 libexec = strdup(path);
1085 ErrPrint("Heap: %s\n", strerror(errno));
1086 sqlite3_reset(stmt);
1087 sqlite3_finalize(stmt);
1091 DbgPrint("libexec: %s\n", libexec);
1093 sqlite3_reset(stmt);
1094 sqlite3_finalize(stmt);
1100 static inline char *get_lb_pkgname_by_appid(const char *appid)
1116 ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1117 if (ret != SQLITE_OK) {
1118 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1123 ret = sqlite3_bind_text(stmt, 1, appid, -1, NULL);
1124 if (ret != SQLITE_OK) {
1125 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1129 ret = sqlite3_bind_text(stmt, 2, appid, -1, NULL);
1130 if (ret != SQLITE_OK) {
1131 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1135 if (sqlite3_step(stmt) != SQLITE_ROW) {
1136 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1140 tmp = (char *)sqlite3_column_text(stmt, 0);
1141 if (tmp && strlen(tmp)) {
1142 pkgid = strdup(tmp);
1144 ErrPrint("Heap: %s\n", strerror(errno));
1148 sqlite3_reset(stmt);
1149 sqlite3_finalize(stmt);
1154 EAPI char *livebox_service_pkgname(const char *appid)
1157 pkgmgr_appinfo_h handle;
1164 lb_pkgname = get_lb_pkgname_by_appid(appid);
1170 * Try to get the package id using given appid
1172 ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1173 if (ret != PKGMGR_R_OK) {
1174 ErrPrint("Failed to get appinfo\n");
1178 ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1179 if (ret != PKGMGR_R_OK) {
1180 pkgmgr_appinfo_destroy_appinfo(handle);
1181 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1185 lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1186 pkgmgr_appinfo_destroy_appinfo(handle);
1188 if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1189 return strdup(appid);
1194 EAPI char *livebox_service_provider_name(const char *lbid)
1200 char *str = "com.samsung.";
1205 while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1207 if (seq < 2 && lbid[idx] == '.') {
1213 if (!str[idx] && lbid[idx]) {
1215 return strdup(lbid);
1216 } else if (seq < 2) {
1218 if (lbid[idx] == '.') {
1220 } else if (!lbid[idx]) {
1221 ErrPrint("Invalid lbid: %s\n", lbid);
1233 ret = strdup(lbid + stage);
1235 ErrPrint("Error: %s\n", strerror(errno));
1242 EAPI char *livebox_service_appid(const char *pkgname)
1248 int is_prime __attribute__((__unused__));
1259 ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1260 if (ret != SQLITE_OK) {
1261 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1265 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
1266 if (ret != SQLITE_OK) {
1267 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1268 sqlite3_reset(stmt);
1269 sqlite3_finalize(stmt);
1273 ret = sqlite3_bind_text(stmt, 2, pkgname, -1, NULL);
1274 if (ret != SQLITE_OK) {
1275 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1276 sqlite3_reset(stmt);
1277 sqlite3_finalize(stmt);
1281 ret = sqlite3_step(stmt);
1282 if (ret != SQLITE_ROW) {
1283 pkgmgr_appinfo_h pkg_handle;
1286 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1287 sqlite3_reset(stmt);
1288 sqlite3_finalize(stmt);
1290 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1291 if (ret != PKGMGR_R_OK) {
1292 ErrPrint("Failed to get appinfo: %s\n", pkgname);
1296 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1297 if (ret != PKGMGR_R_OK) {
1298 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1299 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1303 appid = strdup(new_appid);
1305 ErrPrint("Heap: %s\n", strerror(errno));
1307 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1311 tmp = (char *)sqlite3_column_text(stmt, 0);
1312 if (!tmp || !strlen(tmp)) {
1313 ErrPrint("APPID is NIL\n");
1314 sqlite3_reset(stmt);
1315 sqlite3_finalize(stmt);
1319 appid = strdup(tmp);
1321 ErrPrint("Heap: %s\n", strerror(errno));
1322 sqlite3_reset(stmt);
1323 sqlite3_finalize(stmt);
1327 is_prime = sqlite3_column_int(stmt, 1);
1329 sqlite3_reset(stmt);
1330 sqlite3_finalize(stmt);
1336 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1353 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1354 if (ret != SQLITE_OK) {
1355 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1359 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1360 if (ret != SQLITE_OK) {
1361 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1362 sqlite3_finalize(stmt);
1366 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1367 if (ret != SQLITE_OK) {
1368 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1369 sqlite3_finalize(stmt);
1373 ret = sqlite3_step(stmt);
1374 if (ret != SQLITE_ROW) {
1375 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1376 sqlite3_reset(stmt);
1377 sqlite3_finalize(stmt);
1381 appid = (char *)sqlite3_column_text(stmt, 0);
1382 if (!appid || !strlen(appid)) {
1383 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1384 sqlite3_reset(stmt);
1385 sqlite3_finalize(stmt);
1389 lb_src = (char *)sqlite3_column_text(stmt, 1);
1390 if (!lb_src || !strlen(lb_src)) {
1391 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1392 sqlite3_reset(stmt);
1393 sqlite3_finalize(stmt);
1397 path = strdup(lb_src);
1399 ErrPrint("Heap: %s\n", strerror(errno));
1400 sqlite3_reset(stmt);
1401 sqlite3_finalize(stmt);
1405 DbgPrint("LB Src: %s\n", path);
1407 sqlite3_reset(stmt);
1408 sqlite3_finalize(stmt);
1414 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1430 ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1431 if (ret != SQLITE_OK) {
1432 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1436 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1437 if (ret != SQLITE_OK) {
1438 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1439 sqlite3_finalize(stmt);
1443 ret = sqlite3_step(stmt);
1444 if (ret != SQLITE_ROW) {
1445 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1446 sqlite3_reset(stmt);
1447 sqlite3_finalize(stmt);
1451 tmp = (char *)sqlite3_column_text(stmt, 0);
1452 if (tmp && strlen(tmp)) {
1453 group = strdup(tmp);
1455 ErrPrint("Heap: %s\n", strerror(errno));
1458 sqlite3_reset(stmt);
1459 sqlite3_finalize(stmt);
1465 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1482 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1483 if (ret != SQLITE_OK) {
1484 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1488 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1489 if (ret != SQLITE_OK) {
1490 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1491 sqlite3_finalize(stmt);
1495 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1496 if (ret != SQLITE_OK) {
1497 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1498 sqlite3_finalize(stmt);
1502 ret = sqlite3_step(stmt);
1503 if (ret != SQLITE_ROW) {
1504 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1505 sqlite3_reset(stmt);
1506 sqlite3_finalize(stmt);
1510 appid = (char *)sqlite3_column_text(stmt, 0);
1511 if (!appid || !strlen(appid)) {
1512 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1513 sqlite3_reset(stmt);
1514 sqlite3_finalize(stmt);
1518 pd_src = (char *)sqlite3_column_text(stmt, 1);
1519 if (!pd_src || !strlen(pd_src)) {
1520 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1521 sqlite3_reset(stmt);
1522 sqlite3_finalize(stmt);
1526 path = strdup(pd_src);
1528 ErrPrint("Heap: %s\n", strerror(errno));
1529 sqlite3_reset(stmt);
1530 sqlite3_finalize(stmt);
1534 DbgPrint("PD Src: %s\n", path);
1535 sqlite3_reset(stmt);
1536 sqlite3_finalize(stmt);
1542 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1558 ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1559 if (ret != SQLITE_OK) {
1560 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1564 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1565 if (ret != SQLITE_OK) {
1566 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1567 sqlite3_finalize(stmt);
1571 ret = sqlite3_step(stmt);
1572 if (ret != SQLITE_ROW) {
1573 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1574 sqlite3_reset(stmt);
1575 sqlite3_finalize(stmt);
1579 tmp = (char *)sqlite3_column_text(stmt, 0);
1580 if (tmp && strlen(tmp)) {
1581 group = strdup(tmp);
1583 ErrPrint("Heap: %s\n", strerror(errno));
1585 sqlite3_reset(stmt);
1586 sqlite3_finalize(stmt);
1592 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1596 const char *cluster;
1608 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1609 if (ret != SQLITE_OK) {
1610 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1615 while (sqlite3_step(stmt) == SQLITE_ROW) {
1616 cluster = (const char *)sqlite3_column_text(stmt, 0);
1617 if (!cluster || !strlen(cluster))
1620 if (cb(cluster, data) < 0)
1626 sqlite3_reset(stmt);
1627 sqlite3_finalize(stmt);
1633 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1637 const char *category;
1641 if (!cluster || !cb)
1648 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1649 if (ret != SQLITE_OK) {
1650 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1656 while (sqlite3_step(stmt) == SQLITE_ROW) {
1657 category = (const char *)sqlite3_column_text(stmt, 0);
1658 if (!category || !strlen(category))
1661 if (cb(cluster, category, data) < 0)
1667 sqlite3_reset(stmt);
1668 sqlite3_finalize(stmt);
1674 EAPI int livebox_service_init(void)
1676 if (s_info.handle) {
1677 DbgPrint("Already initialized\n");
1678 s_info.init_count++;
1682 s_info.handle = open_db();
1683 if (s_info.handle) {
1684 s_info.init_count++;
1691 EAPI int livebox_service_fini(void)
1693 if (!s_info.handle || s_info.init_count <= 0) {
1694 ErrPrint("Service is not initialized\n");
1698 s_info.init_count--;
1699 if (s_info.init_count > 0) {
1700 DbgPrint("Init count %d\n", s_info.init_count);
1704 db_util_close(s_info.handle);
1705 s_info.handle = NULL;
1709 EAPI int livebox_service_get_size(int type, int *width, int *height)
1720 return convert_size_from_type(type, width, height);
1723 EAPI int livebox_service_size_type(int width, int height)
1727 if (update_resolution() < 0)
1728 ErrPrint("Failed to update the size list\n");
1730 for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
1731 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
1737 return LB_SIZE_TYPE_1x1;
1739 return LB_SIZE_TYPE_2x1;
1741 return LB_SIZE_TYPE_2x2;
1743 return LB_SIZE_TYPE_4x1;
1745 return LB_SIZE_TYPE_4x2;
1747 return LB_SIZE_TYPE_4x3;
1749 return LB_SIZE_TYPE_4x4;
1751 return LB_SIZE_TYPE_EASY_1x1;
1753 return LB_SIZE_TYPE_EASY_3x1;
1755 return LB_SIZE_TYPE_EASY_3x3;
1760 return LB_SIZE_TYPE_UNKNOWN;