Merge from tizen 2.3.1 00/38800/1
authorKyuho Jo <kyuho.jo@samsung.com>
Wed, 29 Apr 2015 00:59:46 +0000 (09:59 +0900)
committerKyuho Jo <kyuho.jo@samsung.com>
Wed, 29 Apr 2015 00:59:46 +0000 (09:59 +0900)
Change-Id: Ic170b7e62bd1be1b556969a3309f3f5404600a9b
Signed-off-by: Kyuho Jo <kyuho.jo@samsung.com>
141 files changed:
CMakeLists.txt [new file with mode: 0644]
LICENSE [new file with mode: 0644]
data-provider-master.rule [new file with mode: 0644]
data/CMakeLists.txt [new file with mode: 0755]
data/data-provider-master [new file with mode: 0755]
data/data-provider-master.service [new file with mode: 0644]
data/device/abi.ini [new file with mode: 0755]
data/device/wayland.mobile.conf.ini [new file with mode: 0755]
data/device/wayland.mobile.resolution.ini [new file with mode: 0644]
data/device/wayland.wearable.conf.ini [new file with mode: 0755]
data/device/wayland.wearable.resolution.ini [new file with mode: 0644]
data/device/x11.mobile.320x480.conf.ini [new file with mode: 0755]
data/device/x11.mobile.320x480.resolution.ini [new file with mode: 0644]
data/device/x11.mobile.360x360.conf.ini [new file with mode: 0644]
data/device/x11.mobile.360x360.resolution.ini [new file with mode: 0644]
data/device/x11.mobile.360x480.conf.ini [new file with mode: 0644]
data/device/x11.mobile.360x480.resolution.ini [new file with mode: 0644]
data/device/x11.mobile.480x800.conf.ini [new file with mode: 0755]
data/device/x11.mobile.480x800.resolution.ini [new file with mode: 0644]
data/device/x11.mobile.conf.ini [new file with mode: 0755]
data/device/x11.mobile.resolution.ini [new file with mode: 0644]
data/device/x11.wearable.360x360.conf.ini [new file with mode: 0755]
data/device/x11.wearable.360x360.resolution.ini [new file with mode: 0644]
data/device/x11.wearable.360x480.conf.ini [new file with mode: 0755]
data/device/x11.wearable.360x480.resolution.ini [new file with mode: 0644]
data/device/x11.wearable.conf.ini [new file with mode: 0755]
data/device/x11.wearable.resolution.ini [new file with mode: 0644]
data/dump_widget.sh [new file with mode: 0755]
data/emulator/abi.ini [new file with mode: 0755]
data/emulator/wayland.mobile.conf.ini [new file with mode: 0755]
data/emulator/wayland.mobile.resolution.ini [new file with mode: 0644]
data/emulator/wayland.wearable.conf.ini [new file with mode: 0755]
data/emulator/wayland.wearable.resolution.ini [new file with mode: 0644]
data/emulator/x11.mobile.320x480.conf.ini [new file with mode: 0755]
data/emulator/x11.mobile.320x480.resolution.ini [new file with mode: 0644]
data/emulator/x11.mobile.360x360.conf.ini [new file with mode: 0644]
data/emulator/x11.mobile.360x360.resolution.ini [new file with mode: 0644]
data/emulator/x11.mobile.360x480.conf.ini [new file with mode: 0644]
data/emulator/x11.mobile.360x480.resolution.ini [new file with mode: 0644]
data/emulator/x11.mobile.480x800.conf.ini [new file with mode: 0755]
data/emulator/x11.mobile.480x800.resolution.ini [new file with mode: 0644]
data/emulator/x11.mobile.conf.ini [new file with mode: 0755]
data/emulator/x11.mobile.resolution.ini [new file with mode: 0644]
data/emulator/x11.wearable.360x360.conf.ini [new file with mode: 0755]
data/emulator/x11.wearable.360x360.resolution.ini [new file with mode: 0644]
data/emulator/x11.wearable.360x480.conf.ini [new file with mode: 0755]
data/emulator/x11.wearable.360x480.resolution.ini [new file with mode: 0644]
data/emulator/x11.wearable.conf.ini [new file with mode: 0755]
data/emulator/x11.wearable.resolution.ini [new file with mode: 0644]
include/badge_service.h [new file with mode: 0644]
include/buffer_handler.h [new file with mode: 0644]
include/client_life.h [new file with mode: 0644]
include/client_rpc.h [new file with mode: 0644]
include/conf.h [new file with mode: 0644]
include/critical_log.h [new file with mode: 0644]
include/dead_monitor.h [new file with mode: 0644]
include/debug.h [new file with mode: 0644]
include/event.h [new file with mode: 0644]
include/fault_manager.h [new file with mode: 0644]
include/file_service.h [new file with mode: 0644]
include/group.h [new file with mode: 0644]
include/instance.h [new file with mode: 0644]
include/io.h [new file with mode: 0644]
include/lite-errno.h [new file with mode: 0644]
include/main.h [new file with mode: 0644]
include/monitor.h [new file with mode: 0644]
include/notification_service.h [new file with mode: 0644]
include/package.h [new file with mode: 0644]
include/parser.h [new file with mode: 0644]
include/pkgmgr.h [new file with mode: 0644]
include/rpc_to_slave.h [new file with mode: 0644]
include/script_handler.h [new file with mode: 0644]
include/server.h [new file with mode: 0644]
include/service_common.h [new file with mode: 0644]
include/setting.h [new file with mode: 0644]
include/shortcut_service.h [new file with mode: 0644]
include/slave_life.h [new file with mode: 0644]
include/slave_rpc.h [new file with mode: 0644]
include/util.h [new file with mode: 0644]
include/utility_service.h [new file with mode: 0644]
include/widget-mgr.h [new file with mode: 0644]
include/xmonitor.h [new file with mode: 0644]
packaging/data-provider-master.changes [new file with mode: 0644]
packaging/data-provider-master.manifest [new file with mode: 0644]
packaging/data-provider-master.spec [new file with mode: 0644]
pkgmgr_widget/CMakeLists.txt [new file with mode: 0644]
pkgmgr_widget/common/CMakeLists.txt [new file with mode: 0644]
pkgmgr_widget/common/include/common.h [new file with mode: 0644]
pkgmgr_widget/common/include/dlist.h [new file with mode: 0644]
pkgmgr_widget/common/src/common.c [new file with mode: 0644]
pkgmgr_widget/common/src/dlist.c [new file with mode: 0644]
pkgmgr_widget/watch/CMakeLists.txt [new file with mode: 0644]
pkgmgr_widget/watch/src/service_watchapp.c [new file with mode: 0644]
pkgmgr_widget/watch/watchapp.xml [new file with mode: 0644]
pkgmgr_widget/watch/watchapp.xsd [new file with mode: 0644]
pkgmgr_widget/widget.xml [new file with mode: 0644]
pkgmgr_widget/widget.xsd [new file with mode: 0644]
pkgmgr_widget/widget/CMakeLists.txt [new file with mode: 0644]
pkgmgr_widget/widget/src/service_widget.c [new file with mode: 0644]
res/CMakeLists.txt [new file with mode: 0644]
res/edje/CMakeLists.txt [new file with mode: 0644]
res/edje/master.edc [new file with mode: 0644]
src/badge_service.c [new file with mode: 0644]
src/buffer_handler.c [new file with mode: 0644]
src/buffer_handler_wayland.c [new file with mode: 0644]
src/client_life.c [new file with mode: 0644]
src/client_rpc.c [new file with mode: 0644]
src/conf.c [new file with mode: 0644]
src/critical_log.c [new file with mode: 0644]
src/dead_monitor.c [new file with mode: 0644]
src/event.c [new file with mode: 0644]
src/fault_manager.c [new file with mode: 0644]
src/file_service.c [new file with mode: 0644]
src/group.c [new file with mode: 0644]
src/instance.c [new file with mode: 0644]
src/io.c [new file with mode: 0644]
src/main.c [new file with mode: 0644]
src/monitor.c [new file with mode: 0644]
src/notification_service.c [new file with mode: 0644]
src/package.c [new file with mode: 0644]
src/parser.c [new file with mode: 0644]
src/pkgmgr.c [new file with mode: 0644]
src/script_handler.c [new file with mode: 0644]
src/server.c [new file with mode: 0644]
src/service_common.c [new file with mode: 0644]
src/setting.c [new file with mode: 0644]
src/shortcut_service.c [new file with mode: 0644]
src/slave_life.c [new file with mode: 0644]
src/slave_rpc.c [new file with mode: 0644]
src/util.c [new file with mode: 0644]
src/util_wayland.c [new file with mode: 0644]
src/util_x11.c [new file with mode: 0644]
src/utility_service.c [new file with mode: 0644]
src/widget-mgr.c [new file with mode: 0644]
src/xmonitor.c [new file with mode: 0644]
src/xmonitor_wayland.c [new file with mode: 0644]
widget-mgr/CMakeLists.txt [new file with mode: 0644]
widget-mgr/include/node.h [new file with mode: 0644]
widget-mgr/include/widget-info.h [new file with mode: 0644]
widget-mgr/src/node.c [new file with mode: 0644]
widget-mgr/src/widget-mgr.c [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a9f8447
--- /dev/null
@@ -0,0 +1,226 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(${NAME} C)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkg REQUIRED
+       dlog
+       aul
+       vconf
+       sqlite3
+       db-util
+       glib-2.0
+       gio-2.0
+       bundle
+       ecore
+       eina
+       com-core
+       pkgmgr
+       notification
+       notification-service
+       badge
+       badge-service
+       libsmack
+       shortcut
+       pkgmgr-info
+       libsystemd-daemon
+)
+
+IF (LIVEBOX)
+pkg_check_modules(pkg_widget REQUIRED
+       widget_service
+)
+ENDIF (LIVEBOX)
+
+IF (X11_SUPPORT)
+pkg_check_modules(pkg_extra REQUIRED
+       ecore-x
+       x11
+       libdri2
+       libdrm
+       libtbm
+       xfixes
+       dri2proto
+       xext
+       xdamage
+)
+ENDIF (X11_SUPPORT)
+
+IF (WAYLAND_SUPPORT)
+pkg_check_modules(pkg_extra REQUIRED
+       ecore-wayland
+)
+ENDIF (WAYLAND_SUPPORT)
+
+SET(PACKAGE "${PROJECT_NAME}")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Winline -Werror -fno-builtin-malloc -fno-omit-frame-pointer -g -fPIE")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+
+#ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
+ADD_DEFINITIONS("-DPATH_MAX=256")
+ADD_DEFINITIONS("-DPACKAGE=\"${PACKAGE}\"")
+ADD_DEFINITIONS("-DLOCALEDIR=\"${LOCALEDIR}\"")
+
+ADD_DEFINITIONS("-DINFO_SOCKET=\"/opt/usr/share/live_magazine/.live.socket\"")
+ADD_DEFINITIONS("-DCLIENT_SOCKET=\"/opt/usr/share/live_magazine/.client.socket\"")
+ADD_DEFINITIONS("-DSLAVE_SOCKET=\"/opt/usr/share/live_magazine/.slave.socket\"")
+ADD_DEFINITIONS("-DSERVICE_SOCKET=\"/opt/usr/share/live_magazine/.service.socket\"")
+ADD_DEFINITIONS("-DCATEGORY_WATCH_CLOCK=\"http://tizen.org/category/wearable_clock\"")
+
+ADD_DEFINITIONS("-DCLIENT_PORT=\"8208\"")
+
+ADD_DEFINITIONS("-DBADGE_SOCKET=\"/tmp/.badge.service\"")
+ADD_DEFINITIONS("-DSHORTCUT_SOCKET=\"/tmp/.shortcut.service\"")
+ADD_DEFINITIONS("-DNOTIFICATION_SOCKET=\"/tmp/.notification.service\"")
+ADD_DEFINITIONS("-DUTILITY_SOCKET=\"/tmp/.utility.service\"")
+
+ADD_DEFINITIONS("-DUTILITY_SMACK_LABEL=\"${PROJECT_NAME}::utility\"")
+ADD_DEFINITIONS("-DSHORTCUT_SMACK_LABEL=\"${PROJECT_NAME}::shortcut\"")
+ADD_DEFINITIONS("-DNOTIFICATION_SMACK_LABEL=\"${PROJECT_NAME}::notification\"")
+ADD_DEFINITIONS("-DBADGE_SMACK_LABEL=\"${PROJECT_NAME}::badge\"")
+ADD_DEFINITIONS("-DDATA_SHARE_LABEL=\"${PROJECT_NAME}::share\"")
+ADD_DEFINITIONS("-DDEFAULT_SMACK_LABEL=\"${PROJECT_NAME}\"")
+
+ADD_DEFINITIONS("-DDEFAULT_MASTER_CONF=\"/usr/share/${PROJECT_NAME}/conf.ini\"")
+
+ADD_DEFINITIONS("-DNDEBUG")
+
+ADD_DEFINITIONS("-D_USE_ECORE_TIME_GET")
+
+IF (MOBILE)
+       ADD_DEFINITIONS("-DMOBILE")
+ELSEIF (WEARABLE)
+       ADD_DEFINITIONS("-DWEARABLE")
+ENDIF (MOBILE)
+
+SET(BUILD_SOURCE
+       src/main.c
+       src/util.c
+       src/setting.c
+       src/critical_log.c
+       src/shortcut_service.c
+       src/badge_service.c
+       src/notification_service.c
+       src/service_common.c
+)
+
+IF (LIVEBOX)
+       ADD_DEFINITIONS("-DHAVE_LIVEBOX")
+       SET(BUILD_SOURCE
+               ${BUILD_SOURCE}
+               src/utility_service.c
+               src/dead_monitor.c
+               src/package.c
+               src/instance.c
+               src/server.c
+               src/fault_manager.c
+               src/parser.c
+               src/io.c
+               src/slave_life.c
+               src/slave_rpc.c
+               src/client_life.c
+               src/client_rpc.c
+               src/group.c
+               src/script_handler.c
+               src/widget-mgr.c
+               src/pkgmgr.c
+               src/event.c
+               src/file_service.c
+               src/conf.c
+               src/monitor.c
+       )
+
+       IF (WAYLAND_SUPPORT)
+               ADD_DEFINITIONS("-DHAVE_WAYLAND")
+               SET(BUILD_SOURCE 
+                       ${BUILD_SOURCE}
+                       src/xmonitor_wayland.c
+                       src/buffer_handler_wayland.c
+                       src/util_wayland.c
+               )
+       ENDIF (WAYLAND_SUPPORT)
+
+       IF (X11_SUPPORT)
+               ADD_DEFINITIONS("-DHAVE_X11")
+               SET(BUILD_SOURCE 
+                       ${BUILD_SOURCE}
+                       src/xmonitor.c
+                       src/buffer_handler.c
+                       src/util_x11.c
+               )
+       ENDIF (X11_SUPPORT)
+ENDIF (LIVEBOX)
+
+IF (WAYLAND_SUPPORT)
+       ADD_DEFINITIONS("-DHAVE_WAYLAND")
+       SET(BUILD_SOURCE 
+               ${BUILD_SOURCE}
+               src/util_wayland.c
+       )
+ENDIF (WAYLAND_SUPPORT)
+
+IF (X11_SUPPORT)
+       ADD_DEFINITIONS("-DHAVE_X11")
+       SET(BUILD_SOURCE 
+               ${BUILD_SOURCE}
+               src/util_x11.c
+       )
+ENDIF (X11_SUPPORT)
+
+#ADD_DEFINITIONS("-D_APPLY_SCRIPT_ASYNC_UPDATE")
+#ADD_DEFINITIONS("-DFLOG")
+ADD_DEFINITIONS(${pkg_CFLAGS})
+ADD_DEFINITIONS(${pkg_LDFLAGS})
+ADD_DEFINITIONS(${pkg_extra_CFLAGS})
+ADD_DEFINITIONS(${pkg_extra_LDFLAGS})
+ADD_DEFINITIONS(${pkg_widget_CFLAGS})
+ADD_DEFINITIONS(${pkg_widget_LDFLAGS})
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${BUILD_SOURCE})
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkg_LDFLAGS} ${pkg_extra_LDFLAGS} ${pkg_widget_LDFLAGS} "-ldl -lrt -pie")
+
+IF (LIVEBOX)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/abi.ini DESTINATION /usr/share/${PROJECT_NAME} PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/${PRODUCT}.conf.ini DESTINATION /usr/share/${PROJECT_NAME} RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/${PRODUCT}.resolution.ini DESTINATION /usr/share/${PROJECT_NAME} RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.320x480.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/320x480 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.320x480.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/320x480 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.480x800.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/480x800 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.480x800.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/480x800 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.360x480.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/360x480 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.360x480.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/360x480 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.360x360.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/360x360 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.360x360.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/360x360 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/720x1280 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.mobile.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/720x1280 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.conf.ini DESTINATION /usr/share/${PROJECT_NAME}/320x320 RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/x11.wearable.resolution.ini DESTINATION /usr/share/${PROJECT_NAME}/320x320 RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/${PRODUCT}.conf.ini DESTINATION /usr/share/${PROJECT_NAME} RENAME "conf.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${TARGET}/${PRODUCT}.resolution.ini DESTINATION /usr/share/${PROJECT_NAME} RENAME "resolution.ini" PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+ENDIF (LIVEBOX)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/LICENSE DESTINATION /usr/share/license RENAME "${PROJECT_NAME}")
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.rule DESTINATION /etc/smack/accesses.d RENAME ${PROJECT_NAME})
+
+# INCLUDE FOR BUILD & INSTALL .PO FILES
+ADD_SUBDIRECTORY(res)
+ADD_SUBDIRECTORY(data)
+IF (LIVEBOX)
+ADD_SUBDIRECTORY(pkgmgr_widget)
+ENDIF (LIVEBOX)
+
+IF (X11_SUPPORT)
+       IF ("${ENGINEER_BINARY}" STREQUAL "true")
+               ADD_SUBDIRECTORY(widget-mgr)
+       ENDIF ("${ENGINEER_BINARY}" STREQUAL "true")
+ENDIF (X11_SUPPORT)
+
+# End of a file
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..571fe79
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,206 @@
+Flora License
+
+Version 1.1, April, 2013
+
+http://floralicense.org/license/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction,
+and distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by
+the copyright owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and
+all other entities that control, are controlled by, or are
+under common control with that entity. For the purposes of
+this definition, "control" means (i) the power, direct or indirect,
+to cause the direction or management of such entity,
+whether by contract or otherwise, or (ii) ownership of fifty percent (50%)
+or more of the outstanding shares, or (iii) beneficial ownership of
+such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity
+exercising permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications,
+including but not limited to software source code, documentation source,
+and configuration files.
+
+"Object" form shall mean any form resulting from mechanical
+transformation or translation of a Source form, including but
+not limited to compiled object code, generated documentation,
+and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form,
+made available under the License, as indicated by a copyright notice
+that is included in or attached to the work (an example is provided
+in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form,
+that is based on (or derived from) the Work and for which the editorial
+revisions, annotations, elaborations, or other modifications represent,
+as a whole, an original work of authorship. For the purposes of this License,
+Derivative Works shall not include works that remain separable from,
+or merely link (or bind by name) to the interfaces of, the Work and
+Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original
+version of the Work and any modifications or additions to that Work or
+Derivative Works thereof, that is intentionally submitted to Licensor
+for inclusion in the Work by the copyright owner or by an individual or
+Legal Entity authorized to submit on behalf of the copyright owner.
+For the purposes of this definition, "submitted" means any form of
+electronic, verbal, or written communication sent to the Licensor or
+its representatives, including but not limited to communication on
+electronic mailing lists, source code control systems, and issue
+tracking systems that are managed by, or on behalf of, the Licensor
+for the purpose of discussing and improving the Work, but excluding
+communication that is conspicuously marked or otherwise designated
+in writing by the copyright owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity
+on behalf of whom a Contribution has been received by Licensor and
+subsequently incorporated within the Work.
+
+"Tizen Certified Platform" shall mean a software platform that complies
+with the standards set forth in the Tizen Compliance Specification
+and passes the Tizen Compliance Tests as defined from time to time
+by the Tizen Technical Steering Group and certified by the Tizen
+Association or its designated agent.
+
+2. Grant of Copyright License.  Subject to the terms and conditions of
+this License, each Contributor hereby grants to You a perpetual,
+worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+copyright license to reproduce, prepare Derivative Works of,
+publicly display, publicly perform, sublicense, and distribute the
+Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License.  Subject to the terms and conditions of
+this License, each Contributor hereby grants to You a perpetual,
+worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+(except as stated in this section) patent license to make, have made,
+use, offer to sell, sell, import, and otherwise transfer the Work
+solely as incorporated into a Tizen Certified Platform, where such
+license applies only to those patent claims licensable by such
+Contributor that are necessarily infringed by their Contribution(s)
+alone or by combination of their Contribution(s) with the Work solely
+as incorporated into a Tizen Certified Platform to which such
+Contribution(s) was submitted. If You institute patent litigation
+against any entity (including a cross-claim or counterclaim
+in a lawsuit) alleging that the Work or a Contribution incorporated
+within the Work constitutes direct or contributory patent infringement,
+then any patent licenses granted to You under this License for that
+Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution.  You may reproduce and distribute copies of the
+Work or Derivative Works thereof pursuant to the copyright license
+above, in any medium, with or without modifications, and in Source or
+Object form, provided that You meet the following conditions:
+
+  1. You must give any other recipients of the Work or Derivative Works
+     a copy of this License; and
+  2. You must cause any modified files to carry prominent notices stating
+     that You changed the files; and
+  3. You must retain, in the Source form of any Derivative Works that
+     You distribute, all copyright, patent, trademark, and attribution
+     notices from the Source form of the Work, excluding those notices
+     that do not pertain to any part of the Derivative Works; and
+  4. If the Work includes a "NOTICE" text file as part of its distribution,
+     then any Derivative Works that You distribute must include a readable
+     copy of the attribution notices contained within such NOTICE file,
+     excluding those notices that do not pertain to any part of
+     the Derivative Works, in at least one of the following places:
+     within a NOTICE text file distributed as part of the Derivative Works;
+     within the Source form or documentation, if provided along with the
+     Derivative Works; or, within a display generated by the Derivative Works,
+     if and wherever such third-party notices normally appear.
+     The contents of the NOTICE file are for informational purposes only
+     and do not modify the License. You may add Your own attribution notices 
+     within Derivative Works that You distribute, alongside or as an addendum 
+     to the NOTICE text from the Work, provided that such additional attribution 
+     notices cannot be construed as modifying the License. You may add Your own
+     copyright statement to Your modifications and may provide additional or
+     different license terms and conditions for use, reproduction, or
+     distribution of Your modifications, or for any such Derivative Works
+     as a whole, provided Your use, reproduction, and distribution of
+     the Work otherwise complies with the conditions stated in this License
+     and your own copyright statement or terms and conditions do not conflict
+     the conditions stated in the License including section 3.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+any Contribution intentionally submitted for inclusion in the Work
+by You to the Licensor shall be under the terms and conditions of
+this License, without any additional terms or conditions.
+Notwithstanding the above, nothing herein shall supersede or modify
+the terms of any separate license agreement you may have executed
+with Licensor regarding such Contributions.
+
+6. Trademarks.  This License does not grant permission to use the trade
+names, trademarks, service marks, or product names of the Licensor,
+except as required for reasonable and customary use in describing the
+origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+agreed to in writing, Licensor provides the Work (and each
+Contributor provides its Contributions) on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+implied, including, without limitation, any warranties or conditions
+of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+PARTICULAR PURPOSE. You are solely responsible for determining the
+appropriateness of using or redistributing the Work and assume any
+risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+whether in tort (including negligence), contract, or otherwise,
+unless required by applicable law (such as deliberate and grossly
+negligent acts) or agreed to in writing, shall any Contributor be
+liable to You for damages, including any direct, indirect, special,
+incidental, or consequential damages of any character arising as a
+result of this License or out of the use or inability to use the
+Work (including but not limited to damages for loss of goodwill,
+work stoppage, computer failure or malfunction, or any and all
+other commercial damages or losses), even if such Contributor
+has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+the Work or Derivative Works thereof, You may choose to offer,
+and charge a fee for, acceptance of support, warranty, indemnity,
+or other liability obligations and/or rights consistent with this
+License. However, in accepting such obligations, You may act only
+on Your own behalf and on Your sole responsibility, not on behalf
+of any other Contributor, and only if You agree to indemnify,
+defend, and hold each Contributor harmless for any liability
+incurred by, or claims asserted against, such Contributor by reason
+of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Flora License to your work
+
+To apply the Flora License to your work, attach the following
+boilerplate notice, with the fields enclosed by brackets "[]"
+replaced with your own identifying information. (Don't include
+the brackets!) The text should be enclosed in the appropriate
+comment syntax for the file format. We also recommend that a
+file or class name and description of purpose be included on the
+same "printed page" as the copyright notice for easier
+identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Flora License, Version 1.1 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://floralicense.org/license/
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
diff --git a/data-provider-master.rule b/data-provider-master.rule
new file mode 100644 (file)
index 0000000..dafbe90
--- /dev/null
@@ -0,0 +1,33 @@
+data-provider-master::bin samsung::vconf::dynamicbox rw---- ------
+data-provider-master::bin xorg rwx--- ------
+data-provider-master::bin tizen::vconf::setting::admin rw---- ------
+data-provider-master::bin tizen::vconf::platform::r r----- ------
+data-provider-master::bin device::app_logging -w---- ------
+data-provider-master::bin device::sys_logging -w---- ------
+data-provider-master::bin tizen::vconf::public::r r----l ------
+data-provider-master::bin data-provider-master::data r-x--- ------
+data-provider-master::bin data-provider-master::share rwx--- ------
+data-provider-master::bin data-provider-master::db rw---- ------
+data-provider-master::bin data-provider-master rwx--- ------
+data-provider-master::bin pkgmgr::db rw----- ------
+data-provider-master::bin _ rw---- ------
+data-provider-master::bin security-server::api-privilege-by-pid rw---- ------
+data-provider-master::bin wrt-security-daemon r----- ------
+data-provider-master::bin security-server r----- ------
+data-provider-master::bin aul::launch --x--- ------
+data-provider-master::bin device::input rw---- ------
+data-provider-master::bin resourced r----- ------
+data-provider-master::bin syslogd r-x--- ------
+data-provider-master::bin deviced r-x--- ------
+data-provider-master::bin alarm-server r-x--- ------
+data-provider-master::bin dbus r-x--- ------
+data-provider-master::bin secure-storage r-x-- ------
+data-provider-master::bin telephony_framework r-x-- ------
+data-provider-master::bin system::media r-x-- ------
+data-provider-master::bin system::vconf --x--- ------
+data-provider-master::bin system::vconf_system r----- ------
+data-provider-master::bin notification::db rw---- ------
+data-provider-master::bin shortcut-service::db rw---- ------
+data-provider-master::bin badge::db rw---- ------
+_ data-provider-master::bin r-x--- ------
+data-provider-master::bin sdbd r----- ------
diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..2edf919
--- /dev/null
@@ -0,0 +1,4 @@
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/${PROJECT_NAME}.service DESTINATION /usr/lib/systemd/system/ PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+IF (LIVEBOX)
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/data/dump_widget.sh DESTINATION /opt/etc/dump.d/module.d/ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+ENDIF (LIVEBOX)
diff --git a/data/data-provider-master b/data/data-provider-master
new file mode 100755 (executable)
index 0000000..6dd2a92
--- /dev/null
@@ -0,0 +1,98 @@
+#!/bin/sh
+#
+# Copyright 2013  Samsung Electronics Co., Ltd
+#
+# Licensed under the Flora License, Version 1.1 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://floralicense.org/license/
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+launch_provider()
+{
+       RETRY_COUNT=0
+       while [ ! -f "/tmp/.stop.provider" ]; do
+               # PROVIDER_HEAP_MONITOR_START=false
+               # PROVIDER_DISABLE_CALL_OPTION=false
+               # PROVIDER_METHOD="shm", "pixmap", "file" (default = "file")
+               BUFMGR_LOCK_TYPE="once" BUFMGR_MAP_CACHE="true" /usr/bin/data-provider-master
+               vconftool set -t bool memory/data-provider-master/started 0 -f
+               let RETRY_COUNT=$RETRY_COUNT+1
+               if [ $RETRY_COUNT -gt 5 ]; then
+                       echo "EXCEED THE MAXIMUM RETRY COUNT: $RETRY_COUNT (max 5)"
+                       break;
+               fi
+       done
+       rm /tmp/.stop.provider
+}
+
+start ()
+{
+       OLDPID=`ps ax | grep /usr/bin/data-provider-master | grep -v grep | awk '{print $1}'`
+       if [ x"$OLDPID" != x"" ]; then
+               echo $OLDPID is already running.
+               exit 0
+       fi
+
+       rm /tmp/.stop.provider
+       launch_provider &
+}
+
+stop ()
+{
+       TMP=`which ps`
+       if [ $? -ne 0 ]; then
+               echo "'ps' is not exists"
+               exit 0
+       fi
+
+       TMP=`which grep`
+       if [ $? -ne 0 ]; then
+               echo "'grep' is not exists"
+               exit 0
+       fi
+
+       TMP=`which awk`
+       if [ $? -ne 0 ]; then
+               echo "'awk' is not exists"
+               exit 0
+       fi
+
+       if [ ! -f "/usr/bin/data-provider-master" ]; then
+               echo "Data provider master is not installed correctly";
+               exit 0;
+       fi
+
+       touch /tmp/.stop.provider
+       BIN_INODE=`stat -Lc "%i" /usr/bin/data-provider-master`
+
+       PID=`ps ax | grep 'data-provider-master' | awk '{print $1}'`
+       for I in $PID;
+       do
+               if [ ! -f "/proc/$I/exe" ]; then
+                       continue;
+               fi
+
+               INODE=`stat -Lc "%i" /proc/$I/exe 2>/dev/null`
+               if [ x"$BIN_INODE" == x"$INODE" ]; then
+                       echo "Send TERM to $I"
+                       kill $I # Try to terminate a master which is launched already
+                       break
+               fi
+       done
+}
+
+case "$1" in
+       start|"") start;;
+       stop) stop;;
+       restart) stop; start;;
+esac
+
+# End of a file
diff --git a/data/data-provider-master.service b/data/data-provider-master.service
new file mode 100644 (file)
index 0000000..283d13f
--- /dev/null
@@ -0,0 +1,34 @@
+#
+# Copyright 2013  Samsung Electronics Co., Ltd
+#
+# Licensed under the Flora License, Version 1.1 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://floralicense.org/license/
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+[Unit]
+Description=Data Provider daemon
+After=xorg.target
+
+[Service]
+User=system
+Group=system
+SmackProcessLabel=data-provider-master::bin
+Environment=BUFMGR_LOCK_TYPE="once"
+Environment=BUFMGR_MAP_CACHE="true"
+Type=notify
+ExecStart=/usr/bin/data-provider-master
+RestartSec=1
+Restart=always
+MemoryLimit=50M
+
+[Install]
+WantedBy=multi-user.target
diff --git a/data/device/abi.ini b/data/device/abi.ini
new file mode 100755 (executable)
index 0000000..40407ed
--- /dev/null
@@ -0,0 +1,14 @@
+[c]
+package=org.tizen.data-provider-slave
+
+[cpp]
+package=org.tizen.data-provider-slave
+
+[html]
+package=widget.web-provider
+
+[osp]
+package=gi2qxenosh.osp-appwidget-service
+
+[app]
+package=/APPID/
diff --git a/data/device/wayland.mobile.conf.ini b/data/device/wayland.mobile.conf.ini
new file mode 100755 (executable)
index 0000000..ea6dd0a
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=720
+base_height=1280
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=shm
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+#input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=1
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
diff --git a/data/device/wayland.mobile.resolution.ini b/data/device/wayland.mobile.resolution.ini
new file mode 100644 (file)
index 0000000..e2b5a17
--- /dev/null
@@ -0,0 +1,13 @@
+1x1=175x175
+2x1=354x175
+2x2=354x354
+4x1=712x175
+4x2=712x354
+4x3=712x533
+4x4=712x712
+4x5=712x891
+4x6=712x1070
+21x21=224x215
+23x21=680x215
+23x23=680x653
+0x0=720x1280
diff --git a/data/device/wayland.wearable.conf.ini b/data/device/wayland.wearable.conf.ini
new file mode 100755 (executable)
index 0000000..eba043d
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=320
+base_height=320
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=shm
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+#input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
diff --git a/data/device/wayland.wearable.resolution.ini b/data/device/wayland.wearable.resolution.ini
new file mode 100644 (file)
index 0000000..b9e7561
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=320x320
+2x1=0x0
+2x2=320x320
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x320
+base=320x320
diff --git a/data/device/x11.mobile.320x480.conf.ini b/data/device/x11.mobile.320x480.conf.ini
new file mode 100755 (executable)
index 0000000..fc49048
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=320
+base_height=480
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+#input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
diff --git a/data/device/x11.mobile.320x480.resolution.ini b/data/device/x11.mobile.320x480.resolution.ini
new file mode 100644 (file)
index 0000000..c848adf
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=0x0
+2x1=0x0
+2x2=0x0
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=302x302
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x480
+base=320x480
diff --git a/data/device/x11.mobile.360x360.conf.ini b/data/device/x11.mobile.360x360.conf.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/device/x11.mobile.360x360.resolution.ini b/data/device/x11.mobile.360x360.resolution.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/device/x11.mobile.360x480.conf.ini b/data/device/x11.mobile.360x480.conf.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/device/x11.mobile.360x480.resolution.ini b/data/device/x11.mobile.360x480.resolution.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/device/x11.mobile.480x800.conf.ini b/data/device/x11.mobile.480x800.conf.ini
new file mode 100755 (executable)
index 0000000..7593188
--- /dev/null
@@ -0,0 +1,45 @@
+base_width=480
+base_height=800
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=20.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event1
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+auto_align=false
+use_event_time=true
+use_gettimeofday=false
+slave_event_boost_on=-10
+slave_event_boost_off=0
+event_filter=0.5
+#slave_limit_to_ttl=true
+#frame_skip=5
diff --git a/data/device/x11.mobile.480x800.resolution.ini b/data/device/x11.mobile.480x800.resolution.ini
new file mode 100644 (file)
index 0000000..63ad36e
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=126x126
+2x1=0x0
+2x2=0x0
+4x1=0x0
+4x2=470x234
+4x3=0x0
+4x4=470x470
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=480x800
+base=480x800
diff --git a/data/device/x11.mobile.conf.ini b/data/device/x11.mobile.conf.ini
new file mode 100755 (executable)
index 0000000..2f732fa
--- /dev/null
@@ -0,0 +1,40 @@
+base_width=720
+base_height=1280
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+#input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
+#use_event_time=true
+event_filter=0.5
diff --git a/data/device/x11.mobile.resolution.ini b/data/device/x11.mobile.resolution.ini
new file mode 100644 (file)
index 0000000..e2b5a17
--- /dev/null
@@ -0,0 +1,13 @@
+1x1=175x175
+2x1=354x175
+2x2=354x354
+4x1=712x175
+4x2=712x354
+4x3=712x533
+4x4=712x712
+4x5=712x891
+4x6=712x1070
+21x21=224x215
+23x21=680x215
+23x23=680x653
+0x0=720x1280
diff --git a/data/device/x11.wearable.360x360.conf.ini b/data/device/x11.wearable.360x360.conf.ini
new file mode 100755 (executable)
index 0000000..c0cda7c
--- /dev/null
@@ -0,0 +1,39 @@
+base_width=360
+base_height=360
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event0
+pd_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+auto_align=false
+check_lcd=false
diff --git a/data/device/x11.wearable.360x360.resolution.ini b/data/device/x11.wearable.360x360.resolution.ini
new file mode 100644 (file)
index 0000000..b54c8a9
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=360x360
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=360x360
+base=360x360
diff --git a/data/device/x11.wearable.360x480.conf.ini b/data/device/x11.wearable.360x480.conf.ini
new file mode 100755 (executable)
index 0000000..b4276b2
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=360
+base_height=480
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event0
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+check_lcd=false
diff --git a/data/device/x11.wearable.360x480.resolution.ini b/data/device/x11.wearable.360x480.resolution.ini
new file mode 100644 (file)
index 0000000..fea1ffa
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=360x480
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=360x480
+base=360x480
diff --git a/data/device/x11.wearable.conf.ini b/data/device/x11.wearable.conf.ini
new file mode 100755 (executable)
index 0000000..336af73
--- /dev/null
@@ -0,0 +1,40 @@
+base_width=320
+base_height=320
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+#input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
+#use_event_time=true
+check_lcd=false
diff --git a/data/device/x11.wearable.resolution.ini b/data/device/x11.wearable.resolution.ini
new file mode 100644 (file)
index 0000000..5b6923e
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=320x320
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x320
+base=320x320
diff --git a/data/dump_widget.sh b/data/dump_widget.sh
new file mode 100755 (executable)
index 0000000..2963bbb
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+WIDGET_DEBUG=$1/widget
+mkdir -p ${WIDGET_DEBUG}
+/bin/cp -r /opt/usr/share/live_magazine ${WIDGET_DEBUG}
+/bin/cp -r /tmp/.widget.service ${WIDGET_DEBUG}/log
+/bin/cp -r /opt/dbspace/.widget.db ${WIDGET_DEBUG}
+ls -la /tmp/ | /bin/grep srw > ${WIDGET_DEBUG}/log/tmp.hidden_files
diff --git a/data/emulator/abi.ini b/data/emulator/abi.ini
new file mode 100755 (executable)
index 0000000..40407ed
--- /dev/null
@@ -0,0 +1,14 @@
+[c]
+package=org.tizen.data-provider-slave
+
+[cpp]
+package=org.tizen.data-provider-slave
+
+[html]
+package=widget.web-provider
+
+[osp]
+package=gi2qxenosh.osp-appwidget-service
+
+[app]
+package=/APPID/
diff --git a/data/emulator/wayland.mobile.conf.ini b/data/emulator/wayland.mobile.conf.ini
new file mode 100755 (executable)
index 0000000..8c432f7
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=720
+base_height=1280
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=shm
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=1
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
diff --git a/data/emulator/wayland.mobile.resolution.ini b/data/emulator/wayland.mobile.resolution.ini
new file mode 100644 (file)
index 0000000..e2b5a17
--- /dev/null
@@ -0,0 +1,13 @@
+1x1=175x175
+2x1=354x175
+2x2=354x354
+4x1=712x175
+4x2=712x354
+4x3=712x533
+4x4=712x712
+4x5=712x891
+4x6=712x1070
+21x21=224x215
+23x21=680x215
+23x23=680x653
+0x0=720x1280
diff --git a/data/emulator/wayland.wearable.conf.ini b/data/emulator/wayland.wearable.conf.ini
new file mode 100755 (executable)
index 0000000..1da65e8
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=320
+base_height=320
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=shm
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[badge]
+#auto_align=true
diff --git a/data/emulator/wayland.wearable.resolution.ini b/data/emulator/wayland.wearable.resolution.ini
new file mode 100644 (file)
index 0000000..b9e7561
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=320x320
+2x1=0x0
+2x2=320x320
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x320
+base=320x320
diff --git a/data/emulator/x11.mobile.320x480.conf.ini b/data/emulator/x11.mobile.320x480.conf.ini
new file mode 100755 (executable)
index 0000000..9341273
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=320
+base_height=480
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+#auto_align=true
diff --git a/data/emulator/x11.mobile.320x480.resolution.ini b/data/emulator/x11.mobile.320x480.resolution.ini
new file mode 100644 (file)
index 0000000..c848adf
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=0x0
+2x1=0x0
+2x2=0x0
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=302x302
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x480
+base=320x480
diff --git a/data/emulator/x11.mobile.360x360.conf.ini b/data/emulator/x11.mobile.360x360.conf.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/emulator/x11.mobile.360x360.resolution.ini b/data/emulator/x11.mobile.360x360.resolution.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/emulator/x11.mobile.360x480.conf.ini b/data/emulator/x11.mobile.360x480.conf.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/emulator/x11.mobile.360x480.resolution.ini b/data/emulator/x11.mobile.360x480.resolution.ini
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/data/emulator/x11.mobile.480x800.conf.ini b/data/emulator/x11.mobile.480x800.conf.ini
new file mode 100755 (executable)
index 0000000..f6a9e8b
--- /dev/null
@@ -0,0 +1,45 @@
+base_width=480
+base_height=800
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=30.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=20.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+auto_align=false
+use_event_time=true
+use_gettimeofday=false
+slave_event_boost_on=-10
+slave_event_boost_off=0
+event_filter=0.5
+#slave_limit_to_ttl=true
+#frame_skip=5
diff --git a/data/emulator/x11.mobile.480x800.resolution.ini b/data/emulator/x11.mobile.480x800.resolution.ini
new file mode 100644 (file)
index 0000000..63ad36e
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=126x126
+2x1=0x0
+2x2=0x0
+4x1=0x0
+4x2=470x234
+4x3=0x0
+4x4=470x470
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=480x800
+base=480x800
diff --git a/data/emulator/x11.mobile.conf.ini b/data/emulator/x11.mobile.conf.ini
new file mode 100755 (executable)
index 0000000..67818c6
--- /dev/null
@@ -0,0 +1,40 @@
+base_width=720
+base_height=1280
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[utility],[badge]
+#auto_align=true
+#use_event_time=true
+event_filter=0.5
diff --git a/data/emulator/x11.mobile.resolution.ini b/data/emulator/x11.mobile.resolution.ini
new file mode 100644 (file)
index 0000000..e2b5a17
--- /dev/null
@@ -0,0 +1,13 @@
+1x1=175x175
+2x1=354x175
+2x2=354x354
+4x1=712x175
+4x2=712x354
+4x3=712x533
+4x4=712x712
+4x5=712x891
+4x6=712x1070
+21x21=224x215
+23x21=680x215
+23x23=680x653
+0x0=720x1280
diff --git a/data/emulator/x11.wearable.360x360.conf.ini b/data/emulator/x11.wearable.360x360.conf.ini
new file mode 100755 (executable)
index 0000000..a277980
--- /dev/null
@@ -0,0 +1,39 @@
+base_width=360
+base_height=360
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+pd_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+auto_align=false
+check_lcd=false
diff --git a/data/emulator/x11.wearable.360x360.resolution.ini b/data/emulator/x11.wearable.360x360.resolution.ini
new file mode 100644 (file)
index 0000000..b54c8a9
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=360x360
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=360x360
+base=360x360
diff --git a/data/emulator/x11.wearable.360x480.conf.ini b/data/emulator/x11.wearable.360x480.conf.ini
new file mode 100755 (executable)
index 0000000..a8511a2
--- /dev/null
@@ -0,0 +1,38 @@
+base_width=360
+base_height=480
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=1048576
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[notification],[badge]
+check_lcd=false
diff --git a/data/emulator/x11.wearable.360x480.resolution.ini b/data/emulator/x11.wearable.360x480.resolution.ini
new file mode 100644 (file)
index 0000000..fea1ffa
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=360x480
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=360x480
+base=360x480
diff --git a/data/emulator/x11.wearable.conf.ini b/data/emulator/x11.wearable.conf.ini
new file mode 100755 (executable)
index 0000000..e0fc7e4
--- /dev/null
@@ -0,0 +1,40 @@
+base_width=320
+base_height=320
+minimum_period=1.0
+#script=edje
+#default_abi=c
+#default_group=disclosure
+default_period=-1.0
+default_packet_time=0.0001
+#default_content=default
+minimum_space=104857600
+#replace_tag=/APPID/
+slave_ttl=0.0
+slave_activate_time=30.0
+slave_relaunch_time=3.0
+slave_relaunch_count=3
+max_log_line=1000
+max_log_file=3
+sqilte_flush_max=1048576
+#db_path=/opt/dbspace/.widget.db
+#share_path=/opt/usr/share/live_magazine/
+log_path=/tmp/.widget.service
+#always_path=/opt/usr/share/live_magazine/always
+#reader_path=/opt/usr/share/live_magazine/reader
+#script_port_path=/usr/share/data-provider-master/plugin-script/
+ping_interval=240.0
+slave_max_load=30
+use_sw_backend=false
+provider_method=pixmap
+debug_mode=false
+overwrite_content=false
+com_core_thread=true
+use_xmonitor=false
+input=/dev/input/event2
+gbar_request_timeout=5.0
+premultiplied=0
+#emergency_disk=source=tmpfs;type=tmpfs;option=size=6M
+services=[widget],[shortcut],[badge]
+#auto_align=true
+#use_event_time=true
+check_lcd=false
diff --git a/data/emulator/x11.wearable.resolution.ini b/data/emulator/x11.wearable.resolution.ini
new file mode 100644 (file)
index 0000000..5b6923e
--- /dev/null
@@ -0,0 +1,14 @@
+1x1=170x170
+2x1=0x0
+2x2=320x320
+4x1=0x0
+4x2=0x0
+4x3=0x0
+4x4=0x0
+4x5=0x0
+4x6=0x0
+21x21=0x0
+23x21=0x0
+23x23=0x0
+0x0=320x320
+base=320x320
diff --git a/include/badge_service.h b/include/badge_service.h
new file mode 100644 (file)
index 0000000..ed49c9f
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int badge_service_init(void);
+extern int badge_service_fini(void);
+
+/* End of a file */
diff --git a/include/buffer_handler.h b/include/buffer_handler.h
new file mode 100644 (file)
index 0000000..9931c52
--- /dev/null
@@ -0,0 +1,348 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct buffer_info;
+struct inst_info;
+struct buffer;
+
+#include <widget_buffer.h>
+
+/*!
+ * \brief
+ * \param[in] type
+ * \param[in] w
+ * \param[in] h
+ * \param[in] pixel_size
+ * \return buffer_info
+ */
+extern struct buffer_info *buffer_handler_create(struct inst_info *inst, enum widget_fb_type type, int w, int h, int pixel_size);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_destroy(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_load(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_unload(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_is_loaded(const struct buffer_info *info);
+
+/*!
+ * \brief Reallocate buffer
+ * \param[in] info
+ * \param[in] w
+ * \param[in] h
+ * \return int
+ */
+extern int buffer_handler_resize(struct buffer_info *info, int w, int h);
+
+/*!
+ * \brief Only update the size information
+ * \param[in] info
+ * \param[in] w
+ * \param[in] h
+ * \return void
+ */
+extern void buffer_handler_update_size(struct buffer_info *info, int w, int h);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return const char *
+ */
+extern const char *buffer_handler_id(const struct buffer_info *info);
+
+/*!
+ * \param[in] info
+ * \return widget_fb_type
+ */
+extern enum widget_fb_type buffer_handler_type(const struct buffer_info *info);
+
+/*!
+ * \brief This API is not supported for Pixmap.
+ * \param[in] info
+ * \return void*
+ */
+extern void *buffer_handler_fb(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \param[out] w
+ * \param[out] h
+ * \return int
+ */
+extern int buffer_handler_get_size(struct buffer_info *info, int *w, int *h);
+
+/*!
+ * \brief This API only can be used for file type buffer
+ * \param[in] info
+ * \return void
+ */
+extern void buffer_handler_flush(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return 0 if fails. Return value should be casted to Pixmap type
+ */
+extern int buffer_handler_pixmap(const struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return buffer
+ */
+extern void *buffer_handler_pixmap_acquire_buffer(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_pixmap_release_buffer(void *canvas);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_stride(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_pixels(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \param[in] info
+ * \return int
+ */
+extern int buffer_handler_auto_align(void);
+
+/*!
+ * \brief
+ * \return int
+ */
+extern int buffer_handler_init(void);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_fini(void);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] info
+ * \return void *
+ * \retval NULL
+ * \retval address
+ * \pre
+ * \post
+ * \sa
+ */
+extern void *buffer_handler_pixmap_ref(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer_ptr
+ * \return int
+ * \retval 
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_pixmap_unref(void *buffer_ptr);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] pixmap
+ * \return void *
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern void *buffer_handler_pixmap_find(int pixmap);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] info
+ * \return void *
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern void *buffer_handler_pixmap_buffer(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] info
+ * \return struct inst_info *
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern struct inst_info *buffer_handler_instance(struct buffer_info *info);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] type
+ * \param[in] resource
+ * \return widget_fb_t 
+ * \retval NULL
+ * \retval address
+ * \pre
+ * \post
+ * \sa
+ */
+extern widget_fb_t buffer_handler_raw_open(enum widget_fb_type type, void *resource);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_raw_close(widget_fb_t buffer);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return void *
+ * \retval NULL
+ * \pre
+ * \post
+ * \sa
+ */
+extern void *buffer_handler_raw_data(widget_fb_t buffer);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_raw_size(widget_fb_t buffer);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_lock(struct buffer_info *buffer);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_unlock(struct buffer_info *buffer);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \param[in] data
+ * \return int
+ * \retval
+ * \pre
+ * \post
+ * \sa
+ */
+extern int buffer_handler_set_data(struct buffer_info *buffer, void *data);
+
+/*!
+ * \brief
+ * \details
+ * \remarks
+ * \param[in] buffer
+ * \return void *
+ * \retval NULL
+ * \retval address
+ * \pre
+ * \post
+ * \sa
+ */
+extern void *buffer_handler_data(struct buffer_info *buffer);
+/* End of a file */
diff --git a/include/client_life.h b/include/client_life.h
new file mode 100644 (file)
index 0000000..e4fa187
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+enum client_event {
+       CLIENT_EVENT_ACTIVATE,
+       CLIENT_EVENT_DEACTIVATE
+};
+
+enum client_global_event {
+       CLIENT_GLOBAL_EVENT_CREATE,
+       CLIENT_GLOBAL_EVENT_DESTROY
+};
+
+struct inst_info;
+struct packet;
+
+/*!
+ * \note
+ * Create & Destroy
+ */
+extern struct client_node *client_create(pid_t pid, int handle, const char *direct_addr);
+#define client_destroy(client) client_unref(client)
+
+/*!
+ * \note
+ * Reference count
+ */
+extern struct client_node *client_ref(struct client_node *client);
+extern struct client_node *client_unref(struct client_node *client);
+extern const int const client_refcnt(const struct client_node *client);
+
+/*!
+ * \note
+ * Information of client PID
+ */
+extern const pid_t const client_pid(const struct client_node *client);
+extern struct client_node *client_find_by_pid(pid_t pid);
+extern struct client_node *client_find_by_rpc_handle(int handle);
+
+/*!
+ * \note
+ * Statistics for state of client
+ */
+extern const int const client_count_paused(void);
+extern int client_is_all_paused(void);
+extern int client_count(void);
+
+/*!
+ * \note
+ * For dead signal handler
+ */
+extern struct client_node *client_deactivated_by_fault(struct client_node *client);
+extern void client_reset_fault(struct client_node *client);
+extern const int const client_is_faulted(const struct client_node *client);
+
+/*!
+ * \note
+ * For other components which wants to know the state of a client
+ */
+extern int client_event_callback_add(struct client_node *client, enum client_event event, int (*cb)(struct client_node *, void *), void *data);
+extern int client_event_callback_del(struct client_node *client, enum client_event event, int (*cb)(struct client_node *, void *), void *data);
+
+extern int client_global_event_handler_del(enum client_global_event event_type, int (*cb)(struct client_node *, void *), void *data);
+extern int client_global_event_handler_add(enum client_global_event event_type, int (*cb)(struct client_node *client, void *data), void *data);
+
+/*!
+ * \note
+ * Private data set & get
+ */
+extern int client_set_data(struct client_node *client, const char *tag, void *data);
+extern void *client_data(struct client_node *client, const char *tag);
+extern void *client_del_data(struct client_node *client, const char *tag);
+
+/*!
+ * Handling the client statues
+ * Paused or Resumed
+ */
+extern void client_paused(struct client_node *client);
+extern void client_resumed(struct client_node *client);
+
+/* Related with Context-Aware service */
+extern int client_subscribe_group(struct client_node *client, const char *cluster, const char *category);
+extern int client_unsubscribe_group(struct client_node *client, const char *cluster, const char *category);
+extern int client_is_subscribed_group(struct client_node *client, const char *cluster, const char *category);
+
+/* Related with category */
+extern int client_subscribe_category(struct client_node *client, const char *category);
+extern int client_unsubscribe_category(struct client_node *client, const char *category);
+extern int client_is_subscribed_by_category(struct client_node *client, const char *category);
+
+extern int client_init(void);
+extern void client_fini(void);
+
+extern int client_browse_group_list(const char *cluster, const char *category, int (*cb)(struct client_node *client, void *data), void *data);
+extern int client_count_of_group_subscriber(const char *cluster, const char *category);
+
+extern int client_browse_category_list(const char *category, int (*cb)(struct client_node *client, void *data), void *data);
+
+extern int client_broadcast(struct inst_info *inst, struct packet *packet);
+
+extern const char *client_direct_addr(const struct client_node *client);
+/* End of a file */
diff --git a/include/client_rpc.h b/include/client_rpc.h
new file mode 100644 (file)
index 0000000..20a8a38
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*!
+ */
+extern int client_rpc_async_request(struct client_node *client, struct packet *packet);
+extern int client_rpc_handle(struct client_node *client);
+
+/*!
+ */
+extern int client_rpc_init(struct client_node *client, int handle);
+extern int client_rpc_fini(struct client_node *client);
+
+/* End of a file */
diff --git a/include/conf.h b/include/conf.h
new file mode 100644 (file)
index 0000000..a4d0603
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct conf {
+       int debug_mode;
+       int slave_max_load;
+};
+
+extern struct conf g_conf;
+
+#define DELAY_TIME 0.0000001f
+#define HAPI __attribute__((visibility("hidden")))
+
+#if !defined(VCONFKEY_MASTER_STARTED)
+#define VCONFKEY_MASTER_STARTED        "memory/data-provider-master/started"
+#endif
+
+#if !defined(VCONFKEY_MASTER_RESTART_COUNT)
+#define VCONFKEY_MASTER_RESTART_COUNT  "memory/private/data-provider-master/restart_count"
+#endif
+
+#define CR 13
+#define LF 10
+
+/* End of a file */
diff --git a/include/critical_log.h b/include/critical_log.h
new file mode 100644 (file)
index 0000000..f060ce1
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int critical_log(const char *func, int line, const char *fmt, ...);
+extern int critical_log_init(const char *tag);
+extern void critical_log_fini(void);
+
+#define CRITICAL_LOG(args...) critical_log(__func__, __LINE__, args)
+
+/* End of a file */
diff --git a/include/dead_monitor.h b/include/dead_monitor.h
new file mode 100644 (file)
index 0000000..52ebbe4
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int dead_init(void);
+extern int dead_fini(void);
+extern int dead_callback_add(int handle, void (*dead_cb)(int handle, void *data), void *data);
+extern void *dead_callback_del(int handle, void (*dead_cb)(int handle, void *data));
+
+/* End of a file */
diff --git a/include/debug.h b/include/debug.h
new file mode 100644 (file)
index 0000000..cbf1ea0
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(FLOG)
+#define DbgPrint(format, arg...)       SECURE_LOGD(format, ##arg)
+#define ErrPrint(format, arg...)       SECURE_LOGE(format, ##arg)
+#define WarnPrint(format, arg...)      SECURE_LOGW(format, ##arg)
+#else
+extern FILE *__file_log_fp;
+#define DbgPrint(format, arg...) do { fprintf(__file_log_fp, "[LOG] [\e[32m%s/%s\e[0m:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg); fflush(__file_log_fp); } while (0)
+#define ErrPrint(format, arg...) do { fprintf(__file_log_fp, "[ERR] [\e[32m%s/%s\e[0m:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg); fflush(__file_log_fp); } while (0)
+#define WarnPrint(format, arg...) do { fprintf(__file_log_fp, "[WRN] [\e[32m%s/%s\e[0m:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg); fflush(__file_log_fp); } while (0)
+#endif
+
+// DbgPrint("FREE\n");
+#define DbgFree(a) do { \
+       free(a); \
+} while (0)
+
+#define DbgXFree(a) do { \
+       DbgPrint("XFree\n"); \
+       XFree(a); \
+} while (0)
+
+
+#if defined(LOG_TAG)
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "DATA_PROVIDER_MASTER"
+
+#if defined(_ENABLE_PERF)
+#define PERF_INIT() \
+       struct timeval __stv; \
+       struct timeval __etv; \
+       struct timeval __rtv
+
+#define PERF_BEGIN() do { \
+       if (gettimeofday(&__stv, NULL) < 0) { \
+               ErrPrint("gettimeofday: %d\n", errno); \
+       } \
+} while (0)
+
+#define PERF_MARK(tag) do { \
+       if (gettimeofday(&__etv, NULL) < 0) { \
+               ErrPrint("gettimeofday: %d\n", errno); \
+       } \
+       timersub(&__etv, &__stv, &__rtv); \
+       DbgPrint("[%s] %u.%06u\n", tag, __rtv.tv_sec, __rtv.tv_usec); \
+} while (0)
+#else
+#define PERF_INIT()
+#define PERF_BEGIN()
+#define PERF_MARK(tag)
+#endif
+
+#define HAPI __attribute__((visibility("hidden")))
+
+/* End of a file */
diff --git a/include/event.h b/include/event.h
new file mode 100644 (file)
index 0000000..b16ce31
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct event_data {
+    int x;
+    int y;
+    unsigned int keycode;
+    int device;
+    int slot;
+    struct {
+       int major;
+       int minor;
+    } touch;
+    struct {
+       int major;
+       int minor;
+    } width;
+    int distance;      /* Hovering */
+    int orientation;
+    int pressure;
+    int updated; /* Timestamp is updated */
+    double tv;
+};
+
+enum event_state {
+    EVENT_STATE_DEACTIVATED,
+    EVENT_STATE_ACTIVATE,
+    EVENT_STATE_ACTIVATED,
+    EVENT_STATE_DEACTIVATE,
+    EVENT_STATE_ERROR
+};
+
+enum event_handler_activate_type {
+       EVENT_HANDLER_DEACTIVATED = 0x00,
+       EVENT_HANDLER_ACTIVATED_BY_MOUSE_SET = 0x01,
+       EVENT_HANDLER_ACTIVATED_BY_SHOW = 0x02,
+       EVENT_HANDLER_UNKNOWN = 0x04
+};
+
+extern int event_init(void);
+extern int event_fini(void);
+extern int event_input_fd(void);
+extern int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data);
+extern int event_deactivate(int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data);
+extern int event_is_activated(void);
+extern int event_reset_cbdata(int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data, void *new_data);
+
+extern int event_deactivate_thread(enum event_handler_activate_type activate_type);
+extern int event_activate_thread(enum event_handler_activate_type activate_type);
+
+extern void event_set_mouse_xy(int x, int y, double timestamp);
+
+/* End of a file */
diff --git a/include/fault_manager.h b/include/fault_manager.h
new file mode 100644 (file)
index 0000000..cfc1bec
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int fault_check_pkgs(struct slave_node *node);
+extern int fault_func_call(struct slave_node *node, const char *pkgname, const char *filename, const char *func);
+extern int fault_func_ret(struct slave_node *node, const char *pkgname, const char *filename, const char *func);
+extern int const fault_is_occured(void);
+extern void fault_unicast_info(struct client_node *client, const char *pkgname, const char *filename, const char *func);
+extern void fault_broadcast_info(const char *pkgname, const char *filename, const char *func);
+extern int fault_info_set(struct slave_node *slave, const char *pkgname, const char *id, const char *func);
+
+/* End of a file */
diff --git a/include/file_service.h b/include/file_service.h
new file mode 100644 (file)
index 0000000..208f739
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int file_service_init(void);
+extern int file_service_fini(void);
+
+/* End of a file */
diff --git a/include/group.h b/include/group.h
new file mode 100644 (file)
index 0000000..b321977
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct cluster;
+struct category;
+struct pkg_info;
+struct context_info;
+struct context_item;
+struct context_option;
+
+extern struct cluster *group_create_cluster(const char *name);
+extern struct cluster *group_find_cluster(const char *name);
+extern int group_destroy_cluster(struct cluster *cluster);
+
+extern struct category *group_create_category(struct cluster *cluster, const char *name);
+extern struct category *group_find_category(struct cluster *cluster, const char *name);
+extern int group_destroy_category(struct category *category);
+
+extern const char * const group_category_name(struct category *category);
+extern const char * const group_cluster_name(struct cluster *cluster);
+extern const char *group_cluster_name_by_category(struct category *category);
+
+extern int group_add_package(struct category *category, const char *pkgname);
+extern int group_del_package(struct category *category, const char *pkgname);
+
+extern int group_add_widget(const char *group, const char *pkgname);
+extern int group_del_widget(const char *pkgname);
+
+extern int group_init(void);
+extern int group_fini(void);
+
+extern struct context_info *group_create_context_info(struct category *category, const char *pkgname);
+extern struct context_item *group_add_context_item(struct context_info *info, const char *ctx_item);
+extern int group_add_option(struct context_item *item, const char *key, const char *value);
+extern int group_destroy_context_info(struct context_info *info);
+
+extern Eina_List * const group_context_info_list(struct category *category);
+extern Eina_List * const group_context_item_list(struct context_info *info);
+extern Eina_List * const group_context_option_list(struct context_item *item);
+extern Eina_List * const group_cluster_list(void);
+extern Eina_List * const group_category_list(struct cluster *cluster);
+extern struct context_info * const group_context_info_from_item(struct context_item *item);
+extern struct category * const group_category_from_context_info(struct context_info *info);
+extern const char * const group_option_item_key(struct context_option *option);
+extern const char * const group_option_item_value(struct context_option *option);
+extern const char * const group_context_item(struct context_item *item);
+extern const char * const group_pkgname_from_context_info(struct context_info *info);
+
+extern void *group_context_item_del_data(struct context_item *item, const char *tag);
+extern void *group_context_item_data(struct context_item *item, const char *tag);
+extern int group_context_item_add_data(struct context_item *item, const char *tag, void *data);
+/* End of a file */
diff --git a/include/instance.h b/include/instance.h
new file mode 100644 (file)
index 0000000..f8e7f7b
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*!
+ * \note
+ * An instance has three states.
+ * ACTIVATED, DEACTIVATED, DESTROYED
+ *
+ * When the master is launched and someone requires to create this instance,
+ * The master just allocate a heap for new instance.
+ * We defined this as "DEACTIVATED" state.
+ *
+ * After master successfully allocate heap for an instance,
+ * It will send a load request to a specified slave
+ * (The slave will be specified when a package informaion is
+ * prepared via configuration file of each widget packages.)
+ * We defined this as "REQUEST_TO_ACTIVATE" state.
+ *
+ * After the slave create a new instance, it will sends back
+ * "created" event to the master.
+ * Then the master will change the state of the instance to
+ * "ACTIVATED".
+ *
+ * Sometimes, slaves can meet some unexpected problems then
+ * it will tries to clear all problems and tries to keep them in safe env.
+ * To do this, master or slave can be terminated.
+ * In this case, the master has to find the fault module(crashed widget)
+ * and prevent it from loading at the slave.
+ * And it will send requests for re-creating all other normal widgetes.
+ * We defined this as "REQUEST_TO_REACTIVATE".
+ *
+ * After slave is launched again(recovered from fault situation), it will
+ * receives "re-create" event from the master, then it will create all
+ * instances of requested widgetes.
+ *
+ * When the master receives "created" event from the slaves,
+ * It will change the instance's state to "ACTIVATED"
+ * But now, the master will not send "created" event to the clients.
+ *
+ * Because the clients don't want to know the re-created widgetes.
+ * They just want to know about fault widgetes to display deactivated
+ * message.
+ *
+ * Sometimes the master can send requests to the slave to unload instances.
+ * We defined this as "REQUEST_TO_DEACTIVATE".
+ *
+ * After the slave successfully destroy instances,
+ * The master will change the instance's state to "DEACTIVATED"
+ * It is same state with the first time when it is created in the master.
+ *
+ * Sometimes, the instances can be deleted permanently from the master and slave.
+ * We called this "destorying an instance".
+ * So we defined its states as "DESTROYED".
+ * It can make confusing us, the "DESTROYED" means like the instance is already deleted from the
+ * heap,. 
+ * Yes, it is right. But the instance cannot be deleted directly.
+ * Because some callbacks still reference it to complete its job.
+ * So the instance needs to keep this DESTROYED state for a while
+ * until all callbacks are done for their remained jobs.
+ *
+ * To unload the instance from the slave,
+ * The master should send a request to the slave,
+ * And the master should keep the instance until it receives "deleted" event from the slave.
+ * We defined this state as "REQUEST_TO_DESTROY".
+ * 
+ * After master receives "deleted" event from the slave,
+ * It will change the state of an master to "DESTROYED"
+ *
+ * There is one more event to change the state of an instance to "DESTROYED".
+ * In case of system created widget, it could be destroyed itself.
+ * So the slave will send "deleted" event to the master directly.
+ * Even if the master doesn't requests to delete it.
+ *
+ * In this case, the master will change the state of an instance to
+ * "DESTROYED" state. but it will wait to delete it from the heap until
+ * reference count of an instance reaches to ZERO.
+ */
+
+enum instance_event {
+       INSTANCE_EVENT_DESTROY,
+       INSTNACE_EVENT_UNKNOWN
+};
+
+enum instance_state {
+       INST_INIT = 0x0, /*!< Only keeps in the master */
+
+       /*!
+        */
+       INST_ACTIVATED, /*!< This instance is loaded to the slave */
+       INST_REQUEST_TO_ACTIVATE, /*!< Sent a request to a slave to load this */
+       INST_REQUEST_TO_REACTIVATE, /*!< Sent a request to a slave to load this without "created" event for clients(viewer) */
+
+       /*!
+        */
+       INST_DESTROYED, /*!< Instance is unloaded and also it requires to be deleted from the master */
+       INST_REQUEST_TO_DESTROY /*!< Sent a request to a slave, when the master receives deleted event, the master will delete this */
+};
+
+enum widget_visible_state { /*!< Must be sync'd with widget-viewer */
+       WIDGET_SHOW = 0x00, /*!< widget is showed. Default state */
+       WIDGET_HIDE = 0x01, /*!< widget is hide, with no update event, but keep update timer */
+
+       WIDGET_HIDE_WITH_PAUSE = 0x02, /*!< widget is hide, it needs to be paused (with freezed update timer) */
+
+       WIDGET_VISIBLE_ERROR = 0xFFFFFFFF /* To enlarge the size of this enumeration type */
+};
+
+#define IS_GBAR 1
+#define IS_WIDGET 0
+
+struct inst_info;
+struct pkg_info;
+struct script_handle;
+struct client_node;
+
+extern struct inst_info *instance_create(struct client_node *client, double timestamp, const char *pkgname, const char *content, const char *cluster, const char *category, double period, int width, int height);
+extern int instance_destroy(struct inst_info *inst, widget_destroy_type_e type);
+extern int instance_reload(struct inst_info *inst, widget_destroy_type_e type);
+
+extern struct inst_info * instance_ref(struct inst_info *inst);
+extern struct inst_info * instance_unref(struct inst_info *inst);
+
+extern int instance_state_reset(struct inst_info *inst);
+extern int instance_destroyed(struct inst_info *inst, int reason);
+
+extern int instance_reactivate(struct inst_info *inst);
+extern int instance_activate(struct inst_info *inst);
+
+extern int instance_recover_state(struct inst_info *inst);
+extern int instance_need_slave(struct inst_info *inst);
+
+extern void instance_set_widget_info(struct inst_info *inst, double priority, const char *content, const char *title);
+extern void instance_set_widget_size(struct inst_info *inst, int w, int h);
+extern void instance_set_gbar_size(struct inst_info *inst, int w, int h);
+extern void instance_set_alt_info(struct inst_info *inst, const char *icon, const char *name);
+
+extern int instance_set_pinup(struct inst_info *inst, int pinup);
+extern int instance_resize(struct inst_info *inst, int w, int h);
+extern int instance_hold_scroll(struct inst_info *inst, int seize);
+extern int instance_set_period(struct inst_info *inst, double period);
+extern int instance_clicked(struct inst_info *inst, const char *event, double timestamp, double x, double y);
+extern int instance_text_signal_emit(struct inst_info *inst, const char *signal_name, const char *source, double sx, double sy, double ex, double ey);
+extern int instance_signal_emit(struct inst_info *inst, const char *signal_name, const char *source, double sx, double sy, double ex, double ey, double x, double y, int down);
+extern int instance_change_group(struct inst_info *inst, const char *cluster, const char *category);
+extern int instance_set_visible_state(struct inst_info *inst, enum widget_visible_state state);
+extern enum widget_visible_state instance_visible_state(struct inst_info *inst);
+extern int instance_set_update_mode(struct inst_info *inst, int active_update);
+extern int instance_active_update(struct inst_info *inst);
+
+/*!
+ * \note
+ * getter
+ */
+extern const double const instance_timestamp(const struct inst_info *inst);
+extern struct pkg_info * const instance_package(const struct inst_info *inst);
+extern struct script_info * const instance_widget_script(const struct inst_info *inst);
+extern struct script_info * const instance_gbar_script(const struct inst_info *inst);
+extern struct buffer_info * const instance_gbar_buffer(const struct inst_info *inst);
+extern struct buffer_info * const instance_gbar_extra_buffer(const struct inst_info *inst, int idx);
+extern struct buffer_info * const instance_widget_buffer(const struct inst_info *inst);
+extern struct buffer_info * const instance_widget_extra_buffer(const struct inst_info *inst, int idx);
+extern const char * const instance_id(const struct inst_info *inst);
+extern const char * const instance_content(const struct inst_info *inst);
+extern const char * const instance_category(const struct inst_info *inst);
+extern const char * const instance_cluster(const struct inst_info *inst);
+extern const char * const instance_title(const struct inst_info *inst);
+extern const char * const instance_auto_launch(const struct inst_info *inst);
+extern const int const instance_priority(const struct inst_info *inst);
+extern const struct client_node * const instance_client(const struct inst_info *inst);
+extern const double const instance_period(const struct inst_info *inst);
+extern const int const instance_timeout(const struct inst_info *inst);
+extern const int const instance_widget_width(const struct inst_info *inst);
+extern const int const instance_widget_height(const struct inst_info *inst);
+extern const int const instance_gbar_width(const struct inst_info *inst);
+extern const int const instance_gbar_height(const struct inst_info *inst);
+extern const enum instance_state const instance_state(const struct inst_info *inst);
+
+/*!
+ * event
+ */
+extern int instance_unicast_created_event(struct inst_info *inst, struct client_node *client);
+extern int instance_unicast_deleted_event(struct inst_info *inst, struct client_node *client, int reason);
+
+extern int instance_create_widget_buffer(struct inst_info *inst, int pixels);
+extern int instance_create_widget_extra_buffer(struct inst_info *inst, int pixels, int idx);
+extern int instance_create_gbar_buffer(struct inst_info *inst, int pixels);
+extern int instance_create_gbar_extra_buffer(struct inst_info *inst, int pixels, int idx);
+
+extern void instance_slave_set_gbar_pos(struct inst_info *inst, double x, double y);
+extern void instance_slave_get_gbar_pos(struct inst_info *inst, double *x, double *y);
+
+extern int instance_slave_open_gbar(struct inst_info *inst, struct client_node *client);
+extern int instance_slave_close_gbar(struct inst_info *inst, struct client_node *client, int reason);
+
+extern int instance_freeze_updator(struct inst_info *inst);
+extern int instance_thaw_updator(struct inst_info *inst);
+
+extern int instance_send_access_event(struct inst_info *inst, int status);
+
+extern int instance_widget_update_begin(struct inst_info *inst, double priority, const char *content, const char *title);
+extern int instance_widget_update_end(struct inst_info *inst);
+
+extern int instance_gbar_update_begin(struct inst_info *inst);
+extern int instance_gbar_update_end(struct inst_info *inst);
+
+extern void instance_gbar_updated(const char *pkgname, const char *id, const char *descfile, int x, int y, int w, int h);
+extern void instance_widget_updated_by_instance(struct inst_info *inst, const char *safe_file, int x, int y, int w, int h);
+extern void instance_gbar_updated_by_instance(struct inst_info *inst, const char *descfile, int x, int y, int w, int h);
+extern void instance_extra_updated_by_instance(struct inst_info *inst, int is_gbar, int idx, int x, int y, int w, int h);
+extern void instance_extra_info_updated_by_instance(struct inst_info *inst);
+
+/*!
+ * \note
+ * if the status is WIDGET_ERROR_FAULT (slave is faulted)
+ * even though the GBAR is not created, this will forcely send the GBAR_DESTROYED event to the client.
+ */
+extern int instance_client_gbar_destroyed(struct inst_info *inst, int status);
+extern int instance_client_gbar_created(struct inst_info *inst, int status);
+
+extern int instance_client_gbar_extra_buffer_created(struct inst_info *inst, int idx);
+extern int instance_client_gbar_extra_buffer_destroyed(struct inst_info *inst, int idx);
+
+extern int instance_client_widget_extra_buffer_created(struct inst_info *inst, int idx);
+extern int instance_client_widget_extra_buffer_destroyed(struct inst_info *inst, int idx);
+
+extern int instance_send_access_status(struct inst_info *inst, int status);
+extern int instance_send_key_status(struct inst_info *inst, int status);
+extern int instance_forward_packet(struct inst_info *inst, struct packet *packet);
+
+extern struct client_node *instance_gbar_owner(struct inst_info *inst);
+
+/*!
+ * Multiple viewer
+ */
+extern int instance_add_client(struct inst_info *inst, struct client_node *client);
+extern int instance_del_client(struct inst_info *inst, struct client_node *client);
+extern int instance_has_client(struct inst_info *inst, struct client_node *client);
+extern void *instance_client_list(struct inst_info *inst);
+
+extern int instance_init(void);
+extern int instance_fini(void);
+
+extern int instance_event_callback_add(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data);
+extern int instance_event_callback_del(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data);
+extern int instance_event_callback_is_added(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data);
+
+/*!
+ */
+extern int instance_set_data(struct inst_info *inst, const char *tag, void *data);
+extern void *instance_del_data(struct inst_info *inst, const char *tag);
+extern void *instance_get_data(struct inst_info *inst, const char *tag);
+
+extern void instance_reload_period(struct inst_info *inst, double period);
+
+/**
+ * For the hello_sync or prepare_hello_sync command
+ */
+extern struct packet *instance_duplicate_packet_create(const struct packet *packet, struct inst_info *inst, struct pkg_info *info, int width, int height);
+extern struct packet *instance_watch_create(const struct packet *packet, const char *pkgname, int width, int height);
+
+extern void instance_set_orientation(struct inst_info *inst, int orientation);
+extern int instance_orientation(struct inst_info *inst);
+
+/* End of a file */
diff --git a/include/io.h b/include/io.h
new file mode 100644 (file)
index 0000000..a2e15ba
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int io_init(void);
+extern int io_fini(void);
+extern int io_load_package_db(struct pkg_info *info);
+extern char *io_widget_pkgname(const char *pkgname);
+extern int io_update_widget_package(const char *pkgname, int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data);
+extern int io_crawling_widgetes(int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data);
+extern int io_is_exists(const char *lbid);
+
+/* End of a file */
diff --git a/include/lite-errno.h b/include/lite-errno.h
new file mode 100644 (file)
index 0000000..c5cb74e
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __WIDGET_ERRNO_H
+#define __WIDGET_ERRNO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @addtogroup CAPI_WIDGET_SERVICE_MODULE
+ * @{
+ */
+
+/**
+ * @brief
+ * Definitions for the result status of widget operation.
+ */
+#if 0
+enum widget_status {
+       WIDGET_ERROR_NONE = 0x00000000, /**< Operation is successfully completed */
+       WIDGET_STATUS_ERROR = 0x80000000, /**< This will be OR'd with other specific error value */
+       WIDGET_ERROR_INVALID_PARAMETER = WIDGET_STATUS_ERROR | 0x0001, /**< Invalid request */
+       WIDGET_ERROR_FAULT = WIDGET_STATUS_ERROR | 0x0002, /**< Fault - Unable to recover from the error */
+       WIDGET_ERROR_OUT_OF_MEMORY = WIDGET_STATUS_ERROR | 0x0004, /**< Memory is not enough to do this operation */
+       WIDGET_ERROR_ALREADY_EXIST = WIDGET_STATUS_ERROR | 0x0008, /**< Already exists */
+       WIDGET_ERROR_RESOURCE_BUSY = WIDGET_STATUS_ERROR | 0x0010, /**< Busy so the operation is not started(accepted), try again */
+       WIDGET_ERROR_PERMISSION_DENIED = WIDGET_STATUS_ERROR | 0x0020, /**< Permission error */
+       WIDGET_ERROR_ALREADY = WIDGET_STATUS_ERROR | 0x0040, /**< Operation is already started */
+       WIDGET_ERROR_CANCELED = WIDGET_STATUS_ERROR | 0x0080, /**< Operation is canceled */
+       WIDGET_ERROR_IO_ERROR = WIDGET_STATUS_ERROR | 0x0100, /**< I/O Error */
+       WIDGET_ERROR_NOT_EXIST = WIDGET_STATUS_ERROR | 0x0200, /**< Not exists */
+       WIDGET_ERROR_TIMEOUT = WIDGET_STATUS_ERROR | 0x0400, /**< Timeout */
+       WIDGET_ERROR_NOT_SUPPORTED = WIDGET_STATUS_ERROR | 0x0800, /**< Operation is not implemented */
+       WIDGET_ERROR_NO_SPACE = WIDGET_STATUS_ERROR | 0x1000, /**< No space to operate */
+       WIDGET_ERROR_DISABLED = WIDGET_STATUS_ERROR | 0x2000 /**< Disabled */
+};
+#endif
+
+/*!
+ * \}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+/* End of a file */
+
diff --git a/include/main.h b/include/main.h
new file mode 100644 (file)
index 0000000..67a71d8
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
diff --git a/include/monitor.h b/include/monitor.h
new file mode 100644 (file)
index 0000000..c08bc2d
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+enum monitor_event_type {
+       MONITOR_EVENT_UNKNOWN = 0x0,
+       MONITOR_EVENT_CREATED = CMD_MONITOR_CREATE,
+       MONITOR_EVENT_DESTROYED = CMD_MONITOR_DESTROY,
+       MONITOR_EVENT_PAUSED = CMD_MONITOR_PAUSE,
+       MONITOR_EVENT_RESUMED = CMD_MONITOR_RESUME
+};
+
+struct monitor_client;
+
+extern struct monitor_client *monitor_create_client(const char *widget_id, pid_t pid, int handle);
+extern int monitor_destroy_client(struct monitor_client *monitor);
+
+extern struct monitor_client *monitor_find_client_by_pid(const char *widget_id, pid_t pid);
+extern struct monitor_client *monitor_find_client_by_handle(const char *widget_id, int handle);
+
+extern int monitor_multicast_state_change_event(const char *widget_id, enum monitor_event_type event, const char *instance_id, const char *content_info);
+
+/* End of a file */
diff --git a/include/notification_service.h b/include/notification_service.h
new file mode 100644 (file)
index 0000000..e02dfa3
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int notification_service_init(void);
+extern int notification_service_fini(void);
+
+/* End of a file */
diff --git a/include/package.h b/include/package.h
new file mode 100644 (file)
index 0000000..8922ac7
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+enum alter_type {
+       ALTER_CREATE,
+       ALTER_DESTROY
+};
+
+struct pkg_info;
+struct inst_info;
+struct context_info;
+struct slave_node;
+
+/*!
+ * \brief
+ * Construction & Destruction
+ */
+extern struct pkg_info *package_create(const char *pkgid, const char *lbid);
+extern int package_destroy(struct pkg_info *info);
+extern char *package_widget_pkgname(const char *pkgname);
+extern int package_is_widget_pkgname(const char *pkgname);
+extern struct pkg_info *package_find(const char *pkgname);
+extern const char *package_find_by_secured_slave(struct slave_node *slave);
+extern struct inst_info *package_find_instance_by_id(const char *pkgname, const char *id);
+extern struct inst_info *package_find_instance_by_timestamp(const char *pkgname, double timestamp);
+extern int package_dump_fault_info(struct pkg_info *info);
+extern int package_set_fault_info(struct pkg_info *info, double timestamp, const char *filename, const char *function);
+extern int package_get_fault_info(struct pkg_info *info, double *timestmap, const char **filename, const char **function);
+extern int package_set_category(struct pkg_info *info, const char *category);
+
+/*!
+ * \brief
+ * Readonly functions
+ */
+extern const int const package_is_fault(const struct pkg_info *info);
+extern struct slave_node * const package_slave(const struct pkg_info *info);
+extern const int const package_timeout(const struct pkg_info *info);
+extern const double const package_period(const struct pkg_info *info);
+extern const int const package_secured(const struct pkg_info *info);
+extern const char * const package_script(const struct pkg_info *info);
+extern const char * const package_abi(const struct pkg_info *info);
+extern const char * const package_widget_path(const struct pkg_info *info);
+extern const char * const package_widget_group(const struct pkg_info *info);
+extern const char * const package_gbar_path(const struct pkg_info *info);
+extern const char * const package_gbar_group(const struct pkg_info *info);
+extern const int const package_pinup(const struct pkg_info *info);
+extern const char * const package_auto_launch(const struct pkg_info *info);
+extern const unsigned int const package_size_list(const struct pkg_info *info);
+extern const int const package_gbar_width(const struct pkg_info *info);
+extern const int const package_gbar_height(const struct pkg_info *info);
+extern const char * const package_name(const struct pkg_info *info);
+extern const char * const package_libexec(struct pkg_info *info);
+extern const char *package_category(struct pkg_info *info);
+extern int package_network(struct pkg_info *info);
+extern Eina_List *package_ctx_info(struct pkg_info *pkginfo);
+
+extern int package_set_libexec(struct pkg_info *info, const char *libexec);
+extern void package_set_pinup(struct pkg_info *info, int pinup);
+extern void package_set_auto_launch(struct pkg_info *info, const char *auto_launch);
+extern void package_set_size_list(struct pkg_info *info, unsigned int size_list);
+extern void package_set_widget_type(struct pkg_info *info, enum widget_widget_type type);
+extern void package_set_gbar_type(struct pkg_info *info, enum widget_gbar_type type);
+extern int package_set_widget_group(struct pkg_info *info, const char *group);
+extern int package_set_widget_path(struct pkg_info *info, const char *path);
+extern int package_set_gbar_group(struct pkg_info *info, const char *group);
+extern int package_set_gbar_path(struct pkg_info *info, const char *path);
+extern int package_set_script(struct pkg_info *info, const char *script);
+extern void package_set_secured(struct pkg_info *info, int secured);
+extern void package_set_period(struct pkg_info *info, double period);
+extern void package_set_timeout(struct pkg_info *info, int timeout);
+extern void package_set_network(struct pkg_info *info, int network);
+extern void package_set_gbar_height(struct pkg_info *info, int height);
+extern void package_set_gbar_width(struct pkg_info *info, int width);
+extern int package_set_abi(struct pkg_info *info, const char *abi);
+extern void package_add_ctx_info(struct pkg_info *pkginfo, struct context_info *info);
+extern void package_del_ctx_info(struct pkg_info *pkginfo, struct context_info *info);
+extern void package_set_direct_input(struct pkg_info *info, int direct_input);
+extern int package_direct_input(const struct pkg_info *info);
+extern const char *package_hw_acceleration(struct pkg_info *info);
+extern int package_set_hw_acceleration(struct pkg_info *info, const char *hw_acceleration);
+
+/*!
+ * \brief
+ * Reference counter
+ */
+extern struct pkg_info * const package_ref(struct pkg_info *info);
+extern struct pkg_info * const package_unref(struct pkg_info *info);
+extern const int const package_refcnt(const struct pkg_info *info);
+
+extern const enum widget_gbar_type const package_gbar_type(const struct pkg_info *info);
+extern const enum widget_widget_type const package_widget_type(const struct pkg_info *info);
+
+extern int package_add_instance(struct pkg_info *info, struct inst_info *inst);
+extern int package_del_instance(struct pkg_info *info, struct inst_info *inst);
+extern Eina_List *package_instance_list(struct pkg_info *info);
+
+extern int package_clear_fault(struct pkg_info *info);
+extern int package_alter_instances_to_client(struct client_node *client, enum alter_type alter);
+
+extern const Eina_List *package_list(void);
+extern int const package_fault_count(struct pkg_info *info);
+
+extern int package_init(void);
+extern int package_fini(void);
+
+extern int package_is_enabled(const char *appid);
+extern int package_faulted(struct pkg_info *info, int broadcast);
+extern char *package_get_pkgid(const char *appid);
+
+extern int package_del_instance_by_category(const char *category, const char *except_widget_id);
+extern int package_instance_count(struct pkg_info *info);
+/* End of a file */
diff --git a/include/parser.h b/include/parser.h
new file mode 100644 (file)
index 0000000..2fce3d8
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct parser;
+
+extern struct parser *parser_load(const char *filename);
+extern int parser_unload(struct parser *handle);
+extern double parser_period(struct parser *handle);
+extern int parser_network(struct parser *handle);
+extern int parser_timeout(struct parser *handle);
+extern const char *parser_auto_launch(struct parser *handle);
+extern unsigned int parser_size(struct parser *handle);
+extern void parser_get_gbar_size(struct parser *handle, unsigned int *width, unsigned int *height);
+extern const char *parser_group_str(struct parser *handle);
+extern int parser_secured(struct parser *handle);
+extern int parser_pinup(struct parser *handler);
+
+extern const char *parser_widget_path(struct parser *handle);
+extern const char *parser_widget_group(struct parser *handle);
+extern const char *parser_gbar_path(struct parser *handle);
+extern const char *parser_gbar_group(struct parser *handle);
+
+extern const char *parser_abi(struct parser *handle);
+
+extern int parser_text_gbar(struct parser *handle);
+extern int parser_text_widget(struct parser *handle);
+extern int parser_buffer_widget(struct parser *handle);
+extern int parser_buffer_gbar(struct parser *handle);
+
+extern const char *parser_script(struct parser *handle);
+
+/* End of a file */
diff --git a/include/pkgmgr.h b/include/pkgmgr.h
new file mode 100644 (file)
index 0000000..e4fe4c2
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+enum pkgmgr_event_type {
+       PKGMGR_EVENT_DOWNLOAD,
+       PKGMGR_EVENT_INSTALL,
+       PKGMGR_EVENT_UPDATE,
+       PKGMGR_EVENT_UNINSTALL,
+       PKGMGR_EVENT_RECOVER
+};
+
+enum pkgmgr_status {
+       PKGMGR_STATUS_START,
+       PKGMGR_STATUS_PROCESSING,
+       PKGMGR_STATUS_COMMAND,
+       PKGMGR_STATUS_END,
+       PKGMGR_STATUS_ERROR
+};
+
+extern int pkgmgr_init(void);
+extern int pkgmgr_fini(void);
+
+extern int pkgmgr_add_event_callback(enum pkgmgr_event_type type, int (*cb)(const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data);
+
+extern void *pkgmgr_del_event_callback(enum pkgmgr_event_type type, int (*cb)(const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data);
+
+/* End of a file */
diff --git a/include/rpc_to_slave.h b/include/rpc_to_slave.h
new file mode 100644 (file)
index 0000000..d80c021
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int rpc_send_new(struct inst_info *inst, void (*ret_cb)(const char *funcname, GVariant *result, void *data), void *data, int skip_need_to_create);
+extern int rpc_send_renew(struct inst_info *inst, void (*ret_cb)(const char *funcname, GVariant *result, void *data), void *data);
+
+extern void rpc_send_update_request(const char *pkgname, const char *cluster, const char *category);
+/*!
+ * \brief
+ * \param[in] period if it is negative value, the data provider will use the default period
+ */
+extern struct inst_info *rpc_send_create_request(struct client_node *client, const char *pkgname, const char *content, const char *cluster, const char *category, double timestamp, double period);
+extern void rpc_send_resume_request(void);
+extern void rpc_send_pause_request(void);
+
+/* End of a file */
diff --git a/include/script_handler.h b/include/script_handler.h
new file mode 100644 (file)
index 0000000..5e84f2b
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct script_info;
+struct fb_info;
+
+extern struct script_info *script_handler_create(struct inst_info *inst, const char *file, const char *group, int w, int h);
+extern int script_handler_destroy(struct script_info *info);
+extern int script_handler_parse_desc(struct inst_info *inst, const char *descfile, int is_pd);
+extern int script_handler_unload(struct script_info *info, int is_pd);
+extern int script_handler_load(struct script_info *info, int is_pd);
+extern int script_handler_is_loaded(struct script_info *info);
+extern int script_handler_feed_event(struct script_info *info, int event, double timestamp);
+
+extern int script_init(void);
+extern int script_fini(void);
+
+extern int script_signal_emit(void *buffer_handle, const char *part, const char *signal, double sx, double sy, double ex, double ey);
+
+extern int script_handler_update_pointer(struct script_info *inst, int x, int y, int down);
+extern int script_handler_update_keycode(struct script_info *info, unsigned int keycode);
+extern int script_handler_resize(struct script_info *info, int w, int h);
+extern const char *script_handler_buffer_id(struct script_info *info);
+extern struct buffer_info *script_handler_buffer_info(struct script_info *info);
+
+/* End of a file */
diff --git a/include/server.h b/include/server.h
new file mode 100644 (file)
index 0000000..457ab15
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int server_init(void);
+extern int server_fini(void);
+
+/* End of a file */
diff --git a/include/service_common.h b/include/service_common.h
new file mode 100644 (file)
index 0000000..93c951b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+enum tcb_type {
+       TCB_CLIENT_TYPE_APP     = 0x00,
+       TCB_CLIENT_TYPE_SERVICE = 0x01,
+       TCB_CLIENT_TYPE_UNKNOWN = 0xff
+};
+
+enum tcb_event_type {
+       TCB_EVENT_CREATE = 0x01,
+       TCB_EVENT_DESTROY = 0x02
+};
+
+struct tcb;
+struct service_context;
+struct service_event_item;
+
+extern int tcb_fd(struct tcb *tcb);
+
+/*!
+ * \remarks This function will return valid pid only after it gets the packet from a client.
+ *          or it will returns -1.
+ * \param[in] Thread Control Block
+ * \return pid Process Id
+ * \retval -1 TCB is not valid or the client is remote host. so we cannot get the PID of it.
+ * \retval >0 Process Id (PID)
+ */
+extern int tcb_pid(struct tcb *tcb);
+
+extern struct service_context *tcb_svc_ctx(struct tcb *tcb);
+extern int tcb_client_type(struct tcb *tcb);
+extern int tcb_client_type_set(struct tcb *tcb, enum tcb_type type);
+extern int tcb_is_valid(struct service_context *svc_ctx, struct tcb *tcb);
+
+extern struct service_context *service_common_create(const char *addr, const char *label, int (*service_thread_main)(struct tcb *tcb, struct packet *packet, void *data), void *data);
+extern int service_common_destroy(struct service_context *svc_ctx);
+
+extern int service_common_multicast_packet(struct tcb *tcb, struct packet *packet, int type);
+extern int service_common_unicast_packet(struct tcb *tcb, struct packet *packet);
+
+extern struct service_event_item *service_common_add_timer(struct service_context *svc_ctx, double timer, int (*timer_cb)(struct service_context *svc_cx, void *data), void *data);
+extern int service_common_update_timer(struct service_event_item *item, double timer);
+extern int service_common_del_timer(struct service_context *svc_ctx, struct service_event_item *item);
+
+extern int service_common_fd(struct service_context *ctx);
+
+extern int service_register_tcb_callback(struct service_context *svc_ctx, struct tcb *tcb, enum tcb_event_type event, void (*cb)(struct service_context *svc_ctx, struct tcb *tcb, void *data), void *data);
+extern int service_unregister_tcb_callback(struct service_context *svc_ctx, struct tcb *tcb, enum tcb_event_type event, void (*cb)(struct service_context *svc_ctx, struct tcb *tcb, void *data), void *data);
+
+/* End of a file */
diff --git a/include/setting.h b/include/setting.h
new file mode 100644 (file)
index 0000000..d2138a5
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int setting_is_lcd_off(void);
+extern int setting_init(void);
+extern int setting_fini(void);
+
+enum oom_event_type {
+    OOM_TYPE_NORMAL = 0x00,
+    OOM_TYPE_LOW = 0x01,
+    OOM_ERROR = 0xFF
+};
+
+extern int setting_add_oom_event_callback(int (*handler)(enum oom_event_type type, void *data), void *data);
+extern int setting_del_oom_event_callback(int (*handler)(enum oom_event_type type, void *data), void *data);
+extern enum oom_event_type setting_oom_level(void);
+
+/* End of a file */
diff --git a/include/shortcut_service.h b/include/shortcut_service.h
new file mode 100644 (file)
index 0000000..2810039
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int shortcut_service_init(void);
+extern int shortcut_service_fini(void);
+
+/* End of a file */
diff --git a/include/slave_life.h b/include/slave_life.h
new file mode 100644 (file)
index 0000000..a2fce01
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*!
+ * Managing the reference counter of a slave
+ */
+
+struct slave_node;
+
+enum slave_event {
+       SLAVE_EVENT_ACTIVATE,
+       SLAVE_EVENT_DEACTIVATE, /* deactivate callback, can return REACTIVATE, DEFAULT */
+       SLAVE_EVENT_DELETE, /* Callbacks for this event type, must has not to do something with slave object.
+                              use this only for just notice the state of slave */
+       SLAVE_EVENT_FAULT, /* Critical fault */
+
+       SLAVE_EVENT_PAUSE,
+       SLAVE_EVENT_RESUME,
+
+       SLAVE_NEED_TO_REACTIVATE
+};
+
+enum slave_state {
+       /*!
+        * Launch the slave but not yet receives "hello" packet
+        */
+       SLAVE_REQUEST_TO_LAUNCH,
+
+       /*!
+        * \note
+        * Terminate the slave but not yet receives dead signal
+        */
+       SLAVE_REQUEST_TO_TERMINATE,
+
+       /*!
+        * \note
+        * No slave process exists, just slave object created
+        */
+       SLAVE_TERMINATED,
+
+       /*!
+        * \note
+        * State change request is sent,
+        */
+       SLAVE_REQUEST_TO_PAUSE,
+       SLAVE_REQUEST_TO_RESUME,
+
+       /*!
+        * \note
+        * Request an action for disconnecting to master from the provider side.
+        * This flag should be treated as an activated state.
+        */
+       SLAVE_REQUEST_TO_DISCONNECT,
+
+       /*!
+        * \note
+        * SLAVE_ACTIVATED = { SLAVE_PAUSED, SLAVE_RESUMED }
+        */
+       SLAVE_PAUSED,
+       SLAVE_RESUMED,
+
+       SLAVE_ERROR = 0xFF /* Explicitly define the size of this enum type */
+};
+
+enum PROVIDER_CTRL {
+       PROVIDER_CTRL_DEFAULT = 0x00,                   /*!< Set default control operation */
+       PROVIDER_CTRL_MANUAL_TERMINATION = 0x01,        /*!< Terminate process manually */
+       PROVIDER_CTRL_MANUAL_REACTIVATION = 0x02,       /*!< Reactivate process manually */
+};
+
+extern struct slave_node *slave_ref(struct slave_node *slave);
+extern struct slave_node *slave_unref(struct slave_node *slave);
+extern const int const slave_refcnt(struct slave_node *slave);
+
+/*!
+ * \brief
+ * Create a new slave object or destroy it
+ *
+ * \param[in] name
+ * \param[in] is_secured
+ * \param[in] abi
+ * \param[in] pkgname
+ * \param[in] period
+ * \return slave_node
+ */
+extern struct slave_node *slave_create(const char *name, int is_secured, const char *abi, const char *pkgname, int network, const char *hw_acceleration, int launch_async);
+
+/*!
+ * \brief
+ * \param[in] slave
+ * \return void
+ */
+extern void slave_destroy(struct slave_node *slave);
+
+/*!
+ * \brief
+ * Launch or terminate a slave
+ * \param[in] slave
+ * \return int
+ */
+extern int slave_activate(struct slave_node *slave);
+
+/*!
+ * To check the slave's activation state
+ */
+extern const int const slave_is_activated(struct slave_node *slave);
+extern int slave_activated(struct slave_node *slave);
+
+extern int slave_give_more_ttl(struct slave_node *slave);
+extern int slave_freeze_ttl(struct slave_node *slave);
+extern int slave_thaw_ttl(struct slave_node *slave);
+extern int slave_expired_ttl(struct slave_node *slave);
+
+/*!
+ * \NOTE
+ * To mangage the unexpected termination of a slave
+ * After this function call, the slave object can be deleted
+ */
+extern struct slave_node *slave_deactivated_by_fault(struct slave_node *slave) __attribute__((warn_unused_result));
+
+/*!
+ * \NOTE
+ * After this function, the slave object can be deleted
+ */
+extern struct slave_node *slave_deactivated(struct slave_node *slave) __attribute__((warn_unused_result));
+
+extern int slave_event_callback_add(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data);
+extern int slave_event_callback_del(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data);
+
+extern int slave_set_data(struct slave_node *slave, const char *tag, void *data);
+extern void *slave_del_data(struct slave_node *slave, const char *tag);
+extern void *slave_data(struct slave_node *slave, const char *tag);
+
+extern struct slave_node *slave_find_by_pid(pid_t pid);
+extern struct slave_node *slave_find_by_name(const char *name);
+extern struct slave_node *slave_find_by_pkgname(const char *pkgname);
+extern struct slave_node *slave_find_by_rpc_handle(int handle);
+
+extern void slave_dead_handler(struct slave_node *slave);
+extern const int const slave_is_secured(const struct slave_node *slave);
+extern const int const slave_is_app(const struct slave_node *slave);
+extern const char * const slave_name(const struct slave_node *slave);
+extern const pid_t const slave_pid(const struct slave_node *slave);
+extern const char * const slave_abi(const struct slave_node *slave);
+extern int slave_set_pid(struct slave_node *slave, pid_t pid);
+
+/*!
+ * \note
+ * Used for making decision of destroying a slave or not
+ * Used for balancing load of the slave.
+ */
+extern void slave_load_package(struct slave_node *slave);
+extern void slave_unload_package(struct slave_node *slave);
+extern int const slave_loaded_package(struct slave_node *slave);
+extern struct slave_node *slave_find_available(const char *slave_pkgname, const char *abi, int secured, int network, const char *hw_acceleration);
+
+extern double const slave_ttl(const struct slave_node *slave);
+
+/*!
+ * \note
+ * Used for making decision of activating a slave or not
+ */
+extern void slave_load_instance(struct slave_node *slave);
+
+/*!
+ * \NOTE
+ * After this function call, the slave object can be deleted.
+ */
+extern struct slave_node *slave_unload_instance(struct slave_node *slave) __attribute__((warn_unused_result));
+
+extern int const slave_loaded_instance(struct slave_node *slave);
+
+extern int slave_resume(struct slave_node *slave);
+extern int slave_pause(struct slave_node *slave);
+
+extern const char *slave_pkgname(const struct slave_node *slave);
+extern const char *slave_state_string(const struct slave_node *slave);
+
+extern enum slave_state slave_state(const struct slave_node *slave);
+extern void slave_set_state(struct slave_node *slave, enum slave_state state);
+
+extern const void *slave_list(void);
+extern int const slave_fault_count(const struct slave_node *slave);
+
+extern int slave_need_to_reactivate_instances(struct slave_node *slave);
+extern void slave_set_reactivate_instances(struct slave_node *slave, int reactivate);
+
+extern void slave_set_reactivation(struct slave_node *slave, int flag);
+extern int slave_need_to_reactivate(struct slave_node *slave);
+
+extern int slave_network(const struct slave_node *slave);
+extern void slave_set_network(struct slave_node *slave, int network);
+
+extern int slave_deactivate_all(int reactivate, int reactivate_instances, int no_timer);
+extern int slave_activate_all(void);
+
+extern void slave_set_control_option(struct slave_node *slave, int ctrl);
+extern int slave_control_option(struct slave_node *slave);
+
+extern char *slave_package_name(const char *abi, const char *lbid);
+
+extern int slave_priority(struct slave_node *slave);
+extern int slave_set_priority(struct slave_node *slave, int priority);
+
+extern int slave_valid(const struct slave_node *slave);
+extern void slave_set_valid(struct slave_node *slave);
+
+/* End of a file */
diff --git a/include/slave_rpc.h b/include/slave_rpc.h
new file mode 100644 (file)
index 0000000..6784b94
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int slave_rpc_async_request(struct slave_node *slave, const char *pkgname, struct packet *packet, void (*ret_cb)(struct slave_node *slave, const struct packet *packet, void *data), void *data, int urgent);
+extern int slave_rpc_request_only(struct slave_node *slave, const char *pkgname, struct packet *packet, int urgent);
+
+extern int slave_rpc_update_handle(struct slave_node *slave, int handle, int delete_pending_packet);
+extern int slave_rpc_ping(struct slave_node *slave);
+extern void slave_rpc_request_update(const char *pkgname, const char *id, const char *cluster, const char *category, const char *content, int force);
+extern int slave_rpc_handle(struct slave_node *slave);
+extern int slave_rpc_ping_freeze(struct slave_node *slave);
+extern int slave_rpc_ping_thaw(struct slave_node *slave);
+
+extern int slave_rpc_disconnect(struct slave_node *slave);
+
+extern int slave_rpc_init(struct slave_node *slave);
+extern int slave_rpc_fini(struct slave_node *slave);
+
+/* End of a file */
diff --git a/include/util.h b/include/util.h
new file mode 100644 (file)
index 0000000..132bfda
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern unsigned long util_string_hash(const char *str);
+extern double util_timestamp(void);
+extern int util_check_ext(const char *filename, const char *check_ptr);
+extern int util_unlink(const char *filename);
+extern int util_unlink_files(const char *folder);
+extern char *util_slavename(void);
+extern const char *util_basename(const char *name);
+extern unsigned long long util_free_space(const char *path);
+extern void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data);
+extern void util_timer_interval_set(void *timer, double interval);
+extern double util_time_delay_for_compensation(double period);
+extern void util_setup_log_disk(void);
+extern void util_remove_emergency_disk(void);
+extern void util_prepare_emergency_disk(void);
+extern int util_emergency_disk_is_mounted(void);
+extern int util_service_is_enabled(const char *tag);
+extern int util_string_is_in_list(const char *str, const char *haystack);
+
+extern int util_screen_size_get(int *width, int *height);
+extern int util_screen_init(void);
+extern int util_screen_fini(void);
+
+#define SCHEMA_FILE    "file://"
+#define SCHEMA_PIXMAP  "pixmap://"
+#define SCHEMA_SHM     "shm://"
+
+#define CRITICAL_SECTION_BEGIN(handle) \
+do { \
+       int ret; \
+       ret = pthread_mutex_lock(handle); \
+       if (ret != 0) \
+               ErrPrint("pthread_mutex_lock: %d\n", ret); \
+} while (0)
+
+#define CRITICAL_SECTION_END(handle) \
+do { \
+       int ret; \
+       ret = pthread_mutex_unlock(handle); \
+       if (ret != 0) \
+               ErrPrint("pthread_mutex_unlock: %d\n", ret); \
+} while (0)
+
+#define CANCEL_SECTION_BEGIN() do { \
+       int ret; \
+       ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
+       if (ret != 0) \
+               ErrPrint("Unable to set cancelate state: %d\n", ret); \
+} while (0)
+
+#define CANCEL_SECTION_END() do { \
+       int ret; \
+       ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
+       if (ret != 0) \
+               ErrPrint("Unable to set cancelate state: %d\n", ret); \
+} while (0)
+
+#define CLOSE_PIPE(p)  do { \
+       int status; \
+       status = close(p[PIPE_READ]); \
+       if (status < 0) \
+               ErrPrint("close: %d\n", errno); \
+       status = close(p[PIPE_WRITE]); \
+       if (status < 0) \
+               ErrPrint("close: %d\n", errno); \
+} while (0)
+
+#define PIPE_READ 0
+#define PIPE_WRITE 1
+#define PIPE_MAX 2
+
+/* End of a file */
diff --git a/include/utility_service.h b/include/utility_service.h
new file mode 100644 (file)
index 0000000..db80cd0
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern int utility_service_init(void);
+extern int utility_service_fini(void);
+
+/* End of a file */
+
diff --git a/include/widget-mgr.h b/include/widget-mgr.h
new file mode 100644 (file)
index 0000000..7061548
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct widget_mgr;
+
+extern int widget_mgr_init(void);
+extern void widget_mgr_fini(void);
+extern struct widget_mgr *widget_mgr_create(pid_t pid, int handle);
+extern void widget_mgr_destroy(struct widget_mgr *info);
+
+extern struct widget_mgr *widget_mgr_find_by_pid(pid_t pid);
+extern struct widget_mgr *widget_mgr_find_by_handle(int handle);
+
+extern const char *widget_mgr_filename(struct widget_mgr *info);
+extern pid_t widget_mgr_pid(struct widget_mgr *info);
+extern FILE *widget_mgr_fifo(struct widget_mgr *info);
+extern int widget_mgr_open_fifo(struct widget_mgr *info);
+extern void widget_mgr_close_fifo(struct widget_mgr *info);
+extern void widget_mgr_set_data(struct widget_mgr *info, void *data);
+extern void *widget_mgr_data(struct widget_mgr *info);
+
+/* End of a file */
diff --git a/include/xmonitor.h b/include/xmonitor.h
new file mode 100644 (file)
index 0000000..3e01722
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+enum xmonitor_event {
+       XMONITOR_PAUSED,
+       XMONITOR_RESUMED,
+
+       XMONITOR_ERROR = 0xFFFFFFFF /* To specify the size of this enum */
+};
+
+extern int xmonitor_init(void);
+extern void xmonitor_fini(void);
+extern int xmonitor_update_state(int pid);
+extern int xmonitor_add_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data);
+extern int xmonitor_del_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data);
+extern int xmonitor_is_paused(void);
+extern void xmonitor_handle_state_changes(void);
+extern int xmonitor_resume(struct client_node *client);
+extern int xmonitor_pause(struct client_node *client);
+
+/* End of a file */
diff --git a/packaging/data-provider-master.changes b/packaging/data-provider-master.changes
new file mode 100644 (file)
index 0000000..5e22b27
--- /dev/null
@@ -0,0 +1,8 @@
+* Wed Sep 11 2013 Rusty Lynch <rusty.lynch@intel.com> accepted/tizen/20130620.160431@d15ede1
+- Add support for systemd socket activation for notification service
+
+* Thu Jun 20 2013 Victor Hakoun <victor.hakoun@eurogiciel.fr> accepted/tizen/20130620.115235@e8bd452
+- Replace manifest with default floor
+
+* Fri Jun 07 2013 Victor Hakoun <victor.hakoun@eurogiciel.fr> accepted/tizen/20130530.164904@68752b5
+- Fix build x86_64
diff --git a/packaging/data-provider-master.manifest b/packaging/data-provider-master.manifest
new file mode 100644 (file)
index 0000000..850aa0a
--- /dev/null
@@ -0,0 +1,87 @@
+<manifest>
+       <!-- Provider master label -->
+       <define>
+               <domain name="data-provider-master" policy="restricted" />
+               <provide>
+                       <label name="data-provider-master::log" />
+                       <label name="data-provider-master::db" />
+                       <label name="data-provider-master::data" />
+                       <label name="data-provider-master::share" />
+                       <label name="data-provider-master::shortcut" />
+                       <label name="data-provider-master::shortcut.shortcut" />
+                       <label name="data-provider-master::shortcut.widget" />
+                       <label name="data-provider-master::badge" />
+                       <label name="data-provider-master::badge.client" />
+                       <label name="data-provider-master::notification" />
+                       <label name="data-provider-master::notification.client" />
+                       <label name="data-provider-master::utility" />
+                       <label name="data-provider-master::client" />
+                       <label name="data-provider-master::provider" />
+                       <label name="data-provider-master::bin" />
+               </provide>
+               <request>
+                       <smack request="sys-assert::core" type="rwxat" />
+                       <smack request="security-server::api-privilege-by-pid" type="w" />
+                       <!--
+                       <smack request="notification::db" type="rw" />
+                       <smack request="badge::db" type="rw" />
+                       -->
+               </request>
+               <permit>
+                       <smack permit="dbus" type="rx" />
+               </permit>
+       </define>
+       <request>
+               <domain name="_" />
+       </request>
+
+       <assign>
+               <!-- Executable file -->
+               <filesystem path="/usr/bin/data-provider-master" label="data-provider-master::bin" exec_label="data-provider-master::bin" />
+               <filesystem path="/opt/usr/devel/usr/bin/widget-mgr" label="data-provider-master::bin" exec_label="data-provider-master::bin" />
+               <filesystem path="/opt/usr/devel/usr/bin" label="_" />
+
+               <!-- Configuration data -->
+               <filesystem path="/usr/share/data-provider-master" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/abi.ini" label="data-provider-master::data" />
+
+               <!-- widget_service is able to access the resolution.ini file -->
+               <filesystem path="/usr/share/data-provider-master/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/320x480/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/800x480/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/360x480/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/720x1280/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/320x320/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/360x360/resolution.ini" label="_" />
+               <filesystem path="/usr/share/data-provider-master/480x800/resolution.ini" label="_" />
+
+               <!-- Slave provider and the master provider are able to access the conf.ini file -->
+               <filesystem path="/usr/share/data-provider-master/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/320x480/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/800x480/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/360x480/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/720x1280/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/320x320/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/360x360/conf.ini" label="data-provider-master::data" />
+               <filesystem path="/usr/share/data-provider-master/480x800/conf.ini" label="data-provider-master::data" />
+
+               <!-- Shared data folder -->
+               <filesystem path="/opt/usr/share/live_magazine" label="data-provider-master::share" type="transmutable" />
+               <filesystem path="/opt/usr/share/live_magazine/log" label="data-provider-master::share" type="transmutable" />
+               <filesystem path="/opt/usr/share/live_magazine/reader" label="data-provider-master::share" type="transmutable" />
+               <filesystem path="/opt/usr/share/live_magazine/always" label="data-provider-master::share" type="transmutable" />
+
+               <!-- Database -->
+               <filesystem path="/opt/dbspace/.widget.db" label="data-provider-master::db" />
+               <filesystem path="/opt/dbspace/.widget.db-journal" label="data-provider-master::db" />
+
+               <filesystem path="/usr/lib/systemd/system/data-provider-master.service" label="_" />
+               <filesystem path="/usr/lib/systemd/system/multi-user.target.wants/data-provider-master.service" label="_" />
+
+               <!-- Package manager plugin -->
+               <filesystem path="/usr/etc/package-manager/parserlib/libwidget.so" label="_" />
+
+               <!-- Logdump script -->
+               <filesystem path="/opt/etc/dump.d/module.d/dump_widget.sh" label="_" exec_label="_" />
+       </assign>
+</manifest>
diff --git a/packaging/data-provider-master.spec b/packaging/data-provider-master.spec
new file mode 100644 (file)
index 0000000..51eae2e
--- /dev/null
@@ -0,0 +1,203 @@
+%bcond_with wayland
+
+Name: data-provider-master
+Summary: Master service provider for widgetes
+Version: 1.1.5
+Release: 1
+Group: HomeTF/widget
+License: Flora
+Source0: %{name}-%{version}.tar.gz
+Source1001: %{name}.manifest
+BuildRequires: cmake, gettext-tools, smack, coreutils
+BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(aul)
+BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(sqlite3)
+BuildRequires: pkgconfig(db-util)
+BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(gio-2.0)
+BuildRequires: pkgconfig(libsmack)
+BuildRequires: pkgconfig(bundle)
+
+%if %{with wayland}
+BuildRequires: pkgconfig(ecore-wayland)
+%else
+BuildRequires: pkgconfig(ecore-x)
+BuildRequires: pkgconfig(x11)
+BuildRequires: pkgconfig(libdri2)
+BuildRequires: pkgconfig(libdrm)
+BuildRequires: pkgconfig(libtbm)
+BuildRequires: pkgconfig(xfixes)
+BuildRequires: pkgconfig(dri2proto)
+BuildRequires: pkgconfig(xext)
+BuildRequires: pkgconfig(xdamage)
+%endif
+
+BuildRequires: pkgconfig(ecore)
+BuildRequires: pkgconfig(eina)
+BuildRequires: pkgconfig(com-core)
+BuildRequires: pkgconfig(libxml-2.0)
+BuildRequires: pkgconfig(pkgmgr)
+BuildRequires: pkgconfig(pkgmgr-info)
+BuildRequires: pkgconfig(widget_service)
+BuildRequires: pkgconfig(notification)
+BuildRequires: pkgconfig(notification-service)
+BuildRequires: pkgconfig(badge)
+BuildRequires: pkgconfig(badge-service)
+BuildRequires: pkgconfig(shortcut)
+BuildRequires: pkgconfig(security-server)
+BuildRequires: pkgconfig(libsystemd-daemon)
+BuildRequires: model-build-features
+Requires(post): sys-assert
+Requires(post): dbus
+
+%description
+Manage the 2nd stage widget service provider and communicate with the viewer application.
+Keep trace on the life-cycle of the widget and status of the service providers, viewer applications.
+
+%prep
+%setup -q
+cp %{SOURCE1001} .
+
+%build
+%if 0%{?sec_build_binary_debug_enable}
+export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
+export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
+export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
+%endif
+
+export ENGINEER=false
+%if 0%{?tizen_build_binary_release_type_eng}
+export CFLAGS="${CFLAGS} -DTIZEN_ENGINEER_MODE"
+export CXXFLAGS="${CXXFLAGS} -DTIZEN_ENGINEER_MODE"
+export FFLAGS="${FFLAGS} -DTIZEN_ENGINEER_MODE"
+export ENGINEER=true
+%endif
+
+%if %{with wayland}
+export WAYLAND_SUPPORT=On
+export X11_SUPPORT=Off
+export LIVEBOX_SHM=wayland
+%else
+export WAYLAND_SUPPORT=Off
+export X11_SUPPORT=On
+export LIVEBOX_SHM=x11
+%endif
+
+%if "%{_repository}" == "wearable"
+export LIVEBOX_SHM="${LIVEBOX_SHM}.wearable"
+export MOBILE=Off
+export WEARABLE=On
+%else
+export LIVEBOX_SHM="${LIVEBOX_SHM}.mobile"
+export MOBILE=On
+export WEARABLE=Off
+%endif
+
+export LIVEBOX_SHM="${LIVEBOX_SHM}.480x800"
+export LIVEBOX=On
+
+%ifarch %ix86
+export TARGET=emulator
+%else
+export TARGET=device
+%endif
+
+%cmake . -DNAME=%{name} -DPRODUCT=${LIVEBOX_SHM} -DENGINEER_BINARY=${ENGINEER} -DWAYLAND_SUPPORT=${WAYLAND_SUPPORT} -DX11_SUPPORT=${X11_SUPPORT} -DMOBILE=${MOBILE} -DWEARABLE=${WEARABLE} -DLIVEBOX=${LIVEBOX} -DTARGET=${TARGET}
+
+CFLAGS="${CFLAGS} -Wall -Winline -Werror" LDFLAGS="${LDFLAGS}" make %{?jobs:-j%jobs}
+
+%install
+rm -rf %{buildroot}
+%make_install
+mkdir -p %{buildroot}/%{_datarootdir}/license
+mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants
+ln -sf ../%{name}.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/%{name}.service
+mkdir -p %{buildroot}/opt/usr/share/live_magazine
+mkdir -p %{buildroot}/opt/usr/share/live_magazine/log
+mkdir -p %{buildroot}/opt/usr/share/live_magazine/reader
+mkdir -p %{buildroot}/opt/usr/share/live_magazine/always
+mkdir -p %{buildroot}/opt/usr/devel/usr/bin
+mkdir -p %{buildroot}/opt/dbspace
+
+echo "widget DB file is not exists, initiate it"
+sqlite3 %{buildroot}/opt/dbspace/.widget.db-new <<EOF
+CREATE TABLE version ( version INTEGER );
+CREATE TABLE box_size ( pkgid TEXT NOT NULL, size_type INTEGER, preview TEXT, touch_effect INTEGER, need_frame INTEGER, mouse_event INTEGER, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE client (pkgid TEXT PRIMARY KEY NOT NULL, icon TEXT, name TEXT, auto_launch TEXT, gbar_size TEXT, content TEXT, nodisplay INTEGER, setup TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE groupinfo ( id INTEGER PRIMARY KEY AUTOINCREMENT, cluster TEXT NOT NULL, category TEXT NOT NULL, pkgid TEXT NOT NULL, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE groupmap (option_id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, pkgid TEXT NOT NULL, ctx_item TEXT NOT NULL, FOREIGN KEY(id) REFERENCES groupinfo(id), FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE i18n ( pkgid TEXT NOT NULL, lang TEXT COLLATE NOCASE, name TEXT, icon TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE option ( pkgid TEXT NOT NULL, option_id INTEGER, key TEXT NOT NULL, value TEXT NOT NULL, FOREIGN KEY(option_id) REFERENCES groupmap(option_id), FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+CREATE TABLE pkgmap ( pkgid TEXT PRIMARY KEY NOT NULL, appid TEXT, uiapp TEXT, prime INTEGER, category TEXT DEFAULT 'http://tizen.org/category/default' );
+CREATE TABLE provider ( pkgid TEXT PRIMARY KEY NOT NULL, network INTEGER, abi TEXT, secured INTEGER, box_type INTEGER, box_src TEXT, box_group TEXT, gbar_type INTEGER, gbar_src TEXT, gbar_group TEXT, libexec TEXT, timeout INTEGER, period TEXT, script TEXT, pinup INTEGER, count INTEGER, direct_input INTEGER DEFAULT 0, hw_acceleration TEXT DEFAULT '', FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE);
+EOF
+
+%pre
+# Executing the stop script for stopping the service of installed provider (old version)
+if [ -x %{_sysconfdir}/rc.d/init.d/%{name} ]; then
+       %{_sysconfdir}/rc.d/init.d/%{name} stop
+fi
+
+%post
+#
+# NOTE:
+# This SYSTEM_UID should be updated properly.
+# In the SPIN, system user id is 1000
+SYSTEM_UID=1000
+APP_UID=5000
+APP_GID=5000
+
+if [ ! -s /opt/dbspace/.widget.db ]; then
+       echo "DB is not exists"
+       mv /opt/dbspace/.widget.db-new /opt/dbspace/.widget.db
+       mv /opt/dbspace/.widget.db-new-journal /opt/dbspace/.widget.db-journal
+else
+       VERSION=`sqlite3 /opt/dbspace/.widget.db "SELECT * FROM version"`
+       echo "DB is already exists (Version: $VERSION)"
+       echo "==============================================="
+       sqlite3 /opt/dbspace/.widget.db "SELECT * FROM pkgmap"
+       echo "==============================================="
+       rm -rf /opt/dbspace/.widget.db-new
+       rm -rf /opt/dbspace/.widget.db-new-journal
+fi
+
+chown ${APP_UID}:${APP_GID} /opt/usr/share/live_magazine
+# System tool(widget-mgr) should be able to access this folder.
+# So give the "rx" permission to the other group. (750 -> 755)
+chmod 755 /opt/usr/share/live_magazine
+chown ${APP_UID}:${APP_GID} /opt/usr/share/live_magazine/log
+chmod 750 /opt/usr/share/live_magazine/log
+chown ${APP_UID}:${APP_GID} /opt/usr/share/live_magazine/reader
+chmod 750 /opt/usr/share/live_magazine/reader
+chown ${APP_UID}:${APP_GID} /opt/usr/share/live_magazine/always
+chmod 750 /opt/usr/share/live_magazine/always
+chown ${SYSTEM}:${APP_GID} /opt/dbspace/.widget.db
+chmod 640 /opt/dbspace/.widget.db
+chown ${SYSTEM}:${APP_GID} /opt/dbspace/.widget.db-journal
+chmod 640 /opt/dbspace/.widget.db-journal
+
+vconftool set -t bool "memory/%{name}/started" 0 -i -u ${APP_UID} -f -s system::vconf_system
+vconftool set -t int "memory/private/%{name}/restart_count" 0 -i -u ${APP_UID} -f -s %{name}
+vconftool set -t string "db/%{name}/serveraddr" "/opt/usr/share/live_magazine/.client.socket" -i -u ${APP_UID} -f -s system::vconf_system
+
+echo "Successfully installed. Please start a daemon again manually"
+
+%files -n %{name}
+%manifest %{name}.manifest
+%defattr(-,system,system,-)
+%caps(cap_chown,cap_dac_override,cap_dac_read_search,cap_sys_admin,cap_sys_nice,cap_mac_override,cap_mac_admin+ep) %{_bindir}/%{name}
+%{_libdir}/systemd/system/multi-user.target.wants/%{name}.service
+%{_libdir}/systemd/system/%{name}.service
+%{_datarootdir}/license/*
+%if 0%{?tizen_build_binary_release_type_eng}
+/opt/usr/devel/usr/bin/*
+%endif
+%{_prefix}/etc/package-manager/parserlib/*
+%{_datarootdir}/%{name}/*
+/opt/etc/dump.d/module.d/dump_widget.sh
+/opt/usr/share/live_magazine/*
+/opt/dbspace/.widget.db*
+%{_sysconfdir}/smack/accesses.d/%{name}
+
+# End of a file
diff --git a/pkgmgr_widget/CMakeLists.txt b/pkgmgr_widget/CMakeLists.txt
new file mode 100644 (file)
index 0000000..dc47d02
--- /dev/null
@@ -0,0 +1,27 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(pkgmgr_plugin C)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(bin_pkgs REQUIRED
+       dlog
+       sqlite3
+       libxml-2.0
+       db-util
+       widget_service
+)
+
+FOREACH(flag ${bin_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Wall -Werror")
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/common/include)
+
+ADD_SUBDIRECTORY(common)
+ADD_SUBDIRECTORY(watch)
+ADD_SUBDIRECTORY(widget)
+
+ADD_DEPENDENCIES("watch-application" "common")
+ADD_DEPENDENCIES("widget" "common")
+
+# End of a file
diff --git a/pkgmgr_widget/common/CMakeLists.txt b/pkgmgr_widget/common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e946380
--- /dev/null
@@ -0,0 +1,15 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(common C)
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+ADD_LIBRARY(${PROJECT_NAME} STATIC
+       src/common.c
+       src/dlist.c
+)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${bin_pkgs_LDFLAGS})
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION "etc/package-manager/parserlib")
+
+# End of a file
diff --git a/pkgmgr_widget/common/include/common.h b/pkgmgr_widget/common/include/common.h
new file mode 100644 (file)
index 0000000..9c2441a
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <string.h>
+
+#include <sqlite3.h>
+#include <db-util.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <dlog.h>
+
+#include <widget_service.h>
+
+#include "dlist.h"
+
+#if !defined(FLOG)
+#define DbgPrint(format, arg...)       SECURE_LOGD("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg)
+#define ErrPrint(format, arg...)       SECURE_LOGE("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg)
+#define ErrPrintWithConsole(format, arg...)    do { fprintf(stderr, "[%s/%s:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg); SECURE_LOGE("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg); } while (0)
+#endif
+
+#define CUR_VER 5
+#define DEFAULT_CATEGORY       "http://tizen.org/category/default"
+
+extern int begin_transaction(void);
+extern int commit_transaction(void);
+
+extern int pkglist_get_via_callback(const char *appid, int is_watch_widget, void (*cb)(const char *appid, const char *pkgid, int prime, void *data), void *data);
+
+extern void db_upgrade_db_schema(void);
+
+extern int db_install_widget(xmlNodePtr node, const char *appid);
+extern int db_install_watchapp(xmlNodePtr node, const char *appid);
+extern int db_init(void);
+extern int db_fini(void);
+
+extern int db_check(void);
+extern void delete_record_cb(const char *appid, const char *pkgid, int prime, void *data);
+
+/* End of a file */
diff --git a/pkgmgr_widget/common/include/dlist.h b/pkgmgr_widget/common/include/dlist.h
new file mode 100644 (file)
index 0000000..cd1a421
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define dlist_remove_data(list, data) do { \
+       struct dlist *l; \
+       l = dlist_find_data(list, data); \
+       list = dlist_remove(list, l); \
+} while (0)
+
+#define dlist_foreach(list, l, data) \
+       for ((l) = (list); (l) && ((data) = dlist_data(l)); (l) = dlist_next(l))
+
+#define dlist_foreach_safe(list, l, n, data) \
+       for ((l) = (list), (n) = dlist_next(l); \
+               (l) && ((data) = dlist_data(l)); \
+               (l) = (n), (n) = dlist_next(l))
+
+struct dlist;
+
+extern struct dlist *dlist_append(struct dlist *list, void *data);
+extern struct dlist *dlist_prepend(struct dlist *list, void *data);
+extern struct dlist *dlist_remove(struct dlist *list, struct dlist *l);
+extern struct dlist *dlist_find_data(struct dlist *list, void *data);
+extern void *dlist_data(struct dlist *l);
+extern struct dlist *dlist_next(struct dlist *l);
+extern struct dlist *dlist_prev(struct dlist *l);
+extern int dlist_count(struct dlist *l);
+extern struct dlist *dlist_nth(struct dlist *l, int nth);
+
+/* End of a file */
diff --git a/pkgmgr_widget/common/src/common.c b/pkgmgr_widget/common/src/common.c
new file mode 100644 (file)
index 0000000..f1d24a3
--- /dev/null
@@ -0,0 +1,3380 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <string.h>
+
+#include <sqlite3.h>
+#include <db-util.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <dlog.h>
+#include <pkgmgr-info.h>
+
+#include <widget_service.h>
+#include <widget_service_internal.h>
+
+#include "dlist.h"
+#include "common.h"
+
+#if !defined(FLOG)
+#define DbgPrint(format, arg...)       SECURE_LOGD("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg)
+#define ErrPrint(format, arg...)       SECURE_LOGE("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg)
+#define ErrPrintWithConsole(format, arg...)    do { fprintf(stderr, "[%s/%s:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg); SECURE_LOGE("[\e[32m%s/%s\e[0m:%d] " format, basename(__FILE__), __func__, __LINE__, ##arg); } while (0)
+#endif
+
+#define CUR_VER 5
+#define DEFAULT_CATEGORY       "http://tizen.org/category/default"
+
+#if !defined(WIDGET_COUNT_OF_SIZE_TYPE)
+#define WIDGET_COUNT_OF_SIZE_TYPE 13
+#endif
+
+/**
+ * @note
+ * DB Table schema
+ *
+ * version
+ * +---------+
+ * | version |
+ * +---------+
+ * |   -     |
+ * +---------+
+ * CREATE TABLE version ( version INTEGER )
+ * 
+ *
+ * pkgmap
+ * +-------+-------+-------+-------+-------------------+
+ * | appid | pkgid | uiapp | prime | categ(from ver 2) |
+ * +-------+-------+-------+-------+-------------------+
+ * |   -   |   -   |   -   |   -   |         -         |
+ * +-------+-------+-------+-------+-------------------+
+ * CREATE TABLE pkgmap ( pkgid TEXT PRIMARY KEY NOT NULL, appid TEXT, uiapp TEXT, prime INTEGER, category TEXT )
+ *
+ *
+ * provider
+ * +-------+---------+-----+---------+----------+---------+-----------+---------+--------+----------+---------+---------+--------+--------+-------+-----------------------+
+ * | pkgid | network | abi | secured | box_type | box_src | box_group | gbar_type | gbar_src | gbar_group | libexec | timeout | period | script | pinup | count(from ver 4) | direct_input | hw_acceleration |
+ * +-------+---------+-----+---------+----------+---------+-----------+---------+--------+----------+---------+---------+--------+--------+-------+-----------------------+-------|---------------|
+ * |   -   |    -    |  -  |    -    |     -    |    -    |     -     |    -    |    -   |     -    |     -   |    -    |    -   |    -   |   -   |           -           |   -   |       -       |
+ * +-------+---------+-----+---------+----------+---------+-----------+---------+--------+----------+---------+---------+--------+--------+-------+-----------------------+-------|---------------|
+ * CREATE TABLE provider ( pkgid TEXT PRIMARY KEY NOT NULL, network INTEGER, abi TEXT, secured INTEGER, box_type INTEGER, box_src TEXT, box_group TEXT, gbar_type TEXT, gbar_src TEXT, gbar_group TEXT, libexec TEXT, timeout INTEGER, period TEXT, script TEXT, pinup INTEGER, count INTEGER, direct_input INTEGER DEFAULT 0, hw_acceleration TEXT DEFAULT 'none', FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid))
+ *
+ * = box_type = { text | buffer | script | image }
+ * = gbar_type = { text | buffer | script }
+ * = network = { 1 | 0 }
+ * = secured = { 1 | 0 }
+ *
+ *
+ * client
+ * +-------+------+---------+-------------+-----------+---------+-----------+-------+
+ * | pkgid | Icon |  Name   | auto_launch | gbar_size | content | nodisplay | setup |
+ * +-------+------+---------+-------------+-----------+---------+-----------+-------+
+ * |   -   |   -  |    -    |      -      |    -      |    -    |     -     |   -   |
+ * +-------+------+---------+-------------+-----------+---------+-----------+-------+
+ * CREATE TABLE client ( pkgid TEXT PRIMARY KEY NOT NULL, icon TEXT, name TEXT, auto_launch TEXT, gbar_size TEXT, content TEXT, nodisplay INTEGER, setup TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) )
+ *
+ * = auto_launch = UI-APPID
+ * = gbar_size = WIDTHxHEIGHT
+ *
+ *
+ * i18n
+ * +-------+------+------+------+
+ * |   fk  | lang | name | icon |
+ * +-------+------+------+------+
+ * | pkgid |   -  |   -  |   -  |
+ * +-------+------+------+------+
+ * CREATE TABLE i18n ( pkgid TEXT NOT NULL, lang TEXT COLLATE NOCASE, name TEXT, icon TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) )
+ *
+ *
+ * box_size
+ * +-------+-----------+---------+--------------+------------+-------------------------+
+ * | pkgid | size_type | preview | touch_effect | need_frame | mouse_event(from ver 3) |
+ * +-------+-----------+---------+--------------+------------+-------------------------+
+ * |   -   |     -     |    -    |       -      |     -      |            -            |
+ * +-------+-----------+---------+--------------+------------+-------------------------+
+ * CREATE TABLE box_size ( pkgid TEXT NOT NULL, size_type INTEGER, preview TEXT, INTEGER, touch_effect INTEGER, need_frame INTEGER, mouse_event INTEGER, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) )
+ *
+ * = box_size_list = { WIDTHxHEIGHT; WIDTHxHEIGHT; ... }
+ *
+ * groupinfo
+ * +----+---------+----------+-------+
+ * | id | cluster | category | pkgid |
+ * +----+---------+----------+-------+
+ * |  - |    -    |    -     |   -   |
+ * +----+---------+----------+-------|
+ * CREATE TABLE groupinfo ( id INTEGER PRIMARY KEY AUTOINCREMENT, cluster TEXT NOT NULL, category TEXT NOT NULL, appid TEXT NOT NULL, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ))
+ *
+ * groupmap
+ * +-------+----+----------+-----------+
+ * | pkgid | id | ctx_item | option_id |
+ * +-------+----+----------+-----------+
+ * CREATE TABLE groupmap ( option_id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, pkgid TEXT NOT NULL, ctx_item TEXT NOT NULL, FOREIGN KEY(id) REFERENCES groupinfo(id), FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) )
+ *
+ *
+ * option
+ * +-------+-----------+-----+-------+
+ * | pkgid | option_id | key | value |
+ * +-------+-----------+-----+-------+
+ * CREATE TABLE option ( pkgid TEXT NOT NULL, option_id INTEGER, key TEXT NOT NULL, value TEXT NOT NULL, FOREIGN KEY(option_id) REFERENCES groupmap(option_id), FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid)  )
+ */
+
+#if !defined(LIBXML_TREE_ENABLED)
+       #error "LIBXML is not supporting the tree"
+#endif
+
+#if defined(LOG_TAG)
+       #undef LOG_TAG
+#endif
+
+#define LOG_TAG "PKGMGR_WIDGET2"
+
+int errno;
+
+struct i18n {
+       xmlChar *lang;
+       xmlChar *name;
+       xmlChar *icon;
+};
+
+struct widget {
+       xmlChar *pkgid;
+       int secured;
+       int network;
+       xmlChar *auto_launch;
+       xmlChar *abi;
+       xmlChar *name; /* Default name */
+       xmlChar *icon; /* Default icon */
+       xmlChar *libexec; /* Path of the SO file */
+       xmlChar *timeout; /* INTEGER, timeout */
+       xmlChar *period; /* DOUBLE, update period */
+       xmlChar *script; /* Script engine */
+       xmlChar *content; /* Content information */
+       xmlChar *setup;
+       xmlChar *uiapp; /* UI App Id */
+       xmlChar *category; /* Category of this box */
+
+       int pinup; /* Is this support the pinup feature? */
+       int primary; /* Is this primary widget? */
+       int nodisplay;
+       int count; /* Max count of instances */
+       int direct_input; /* Use the input node to get the event directly */
+
+       int default_mouse_event; /* Mouse event processing option for widget */
+       int default_touch_effect;
+       int default_need_frame;
+
+       enum widget_widget_type widget_type;
+       xmlChar *widget_src;
+       xmlChar *widget_group;
+       int size_list; /* 1x1, 2x1, 2x2, 4x1, 4x2, 4x3, 4x4 */
+
+       xmlChar *preview[WIDGET_COUNT_OF_SIZE_TYPE];
+       int touch_effect[WIDGET_COUNT_OF_SIZE_TYPE]; /* Touch effect of a widget */
+       int need_frame[WIDGET_COUNT_OF_SIZE_TYPE]; /* Box needs frame which should be cared by viewer */
+       int mouse_event[WIDGET_COUNT_OF_SIZE_TYPE];
+
+       enum widget_gbar_type gbar_type;
+       xmlChar *gbar_src;
+       xmlChar *gbar_group;
+       xmlChar *gbar_size; /* Default PD size */
+       xmlChar *hw_acceleration;
+
+       struct dlist *i18n_list;
+       struct dlist *group_list;
+};
+
+struct group {
+       xmlChar *cluster;
+       xmlChar *category;
+       xmlChar *ctx_item;
+       struct dlist *option_list;
+};
+
+struct option {
+       xmlChar *key;
+       xmlChar *value;
+};
+
+static struct {
+       const char *dbfile;
+       sqlite3 *handle;
+} s_info = {
+       .dbfile = "/opt/dbspace/.widget.db",
+       .handle = NULL,
+};
+
+static inline int next_state(int from, char ch)
+{
+       switch (ch) {
+       case '\0':
+       case '/':
+               return 1;
+       case '.':
+               if (from == 1) {
+                       return 2;
+               }
+               if (from == 2) {
+                       return 3;
+               }
+       }
+
+       return 4;
+}
+
+static inline void abspath(const char* pBuffer, char* pRet)
+{
+       int idx=0;
+       int state = 1;
+       int from;
+       int src_idx = 0;
+       int src_len = strlen(pBuffer);
+       pRet[idx] = '/';
+       idx ++;
+
+       while (src_idx <= src_len) {
+               from = state;
+               state = next_state(from, pBuffer[src_idx]);
+
+               switch (from) {
+               case 1:
+                       if (state != 1) {
+                               pRet[idx] = pBuffer[src_idx];
+                               idx++;
+                       }
+                       break;
+               case 2:
+                       if (state == 1) {
+                               if (idx > 1) {
+                                       idx--;
+                               }
+                       } else {
+                               pRet[idx] = pBuffer[src_idx];
+                               idx++;
+                       }
+                       break;
+               case 3:
+                       // Only can go to the 1 or 4
+                       if (state == 1) {
+                               idx -= 2;
+                               if (idx < 1) {
+                                       idx = 1;
+                               }
+
+                               while (idx > 1 && pRet[idx] != '/') {
+                                       idx--; /* Remove .. */
+                               }
+                               if (idx > 1 && pRet[idx] == '/') {
+                                       idx--;
+                               }
+                               while (idx > 1 && pRet[idx] != '/') {
+                                       idx--; /* Remove parent folder */
+                               }
+                       }
+               case 4:
+                       pRet[idx] = pBuffer[src_idx];
+                       idx++;
+                       break;
+               }
+
+               pRet[idx] = '\0';
+               src_idx++;
+       }
+}
+
+static inline xmlChar *abspath_strdup(xmlChar *src)
+{
+       return xmlMalloc(xmlStrlen(src) + 2);
+}
+
+int begin_transaction(void)
+{
+       sqlite3_stmt *stmt;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, "BEGIN TRANSACTION", -1, &stmt, NULL);
+
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return EXIT_FAILURE;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Failed to do update (%s)\n",
+                               sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return EXIT_FAILURE;
+       }
+
+       sqlite3_finalize(stmt);
+       return EXIT_SUCCESS;
+}
+
+static inline int rollback_transaction(void)
+{
+       int ret;
+       sqlite3_stmt *stmt;
+
+       ret = sqlite3_prepare_v2(s_info.handle, "ROLLBACK TRANSACTION", -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return EXIT_FAILURE;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Failed to do update (%s)\n",
+                               sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return EXIT_FAILURE;
+       }
+
+       sqlite3_finalize(stmt);
+       return EXIT_SUCCESS;
+}
+
+inline int commit_transaction(void)
+{
+       sqlite3_stmt *stmt;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, "COMMIT TRANSACTION", -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return EXIT_FAILURE;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Failed to do update (%s)\n",
+                               sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return EXIT_FAILURE;
+       }
+
+       sqlite3_finalize(stmt);
+       return EXIT_SUCCESS;
+}
+
+static void db_create_version(void)
+{
+       static const char *ddl = "CREATE TABLE version (version INTEGER)";
+       char *err;
+
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+}
+
+static int set_version(int version)
+{
+       static const char *dml = "INSERT INTO version (version) VALUES (?)";
+       sqlite3_stmt *stmt;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to prepare the initial DML(%s)\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       if (sqlite3_bind_int(stmt, 1, version) != SQLITE_OK) {
+               ErrPrint("Failed to bind a id(%s)\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               ErrPrint("Failed to execute the DML for version: %d\n", ret);
+               ret = -EIO;
+       } else {
+               ret = 0;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static int update_version(int version)
+{
+       static const char *dml = "UPDATE version SET version = ?";
+       sqlite3_stmt *stmt;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to prepare the initial DML(%s)\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       if (sqlite3_bind_int(stmt, 1, version) != SQLITE_OK) {
+               ErrPrint("Failed to bind a version: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               ErrPrint("Failed to execute DML: %d\n", ret);
+               ret = -EIO;
+       } else {
+               ret = 0;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static int get_version(void)
+{
+       static const char *dml = "SELECT version FROM version";
+       sqlite3_stmt *stmt;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               return -ENOSYS;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ret = -ENOENT;
+       } else {
+               ret = sqlite3_column_int(stmt, 0);
+       }
+
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+/*!
+ * \note
+ * From version 1 to 2
+ */
+static void upgrade_pkgmap_for_category(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "ALTER TABLE pkgmap ADD COLUMN category TEXT DEFAULT \"" DEFAULT_CATEGORY "\"";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return;
+}
+
+/**
+ * From version 4 to 5
+ * "provider" table should have "direct_input" column.
+ * "direct_input" will be used for selecting input event path.
+ * if it is "true", the provider must has to get all events from device node directly.
+ * The file descriptor will be given by data-provider-master
+ */
+static void upgrade_to_version_5(void)
+{
+       char *err;
+       static const char *ddl;
+
+       /*
+        * Step 1
+        * Create a new column "direct_input" for provider table
+        */
+       ddl = "ALTER TABLE provider ADD COLUMN direct_input INTEGER DEFAULT 0";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       /*
+        * Step 2
+        * Create a new column "hw_acceleration" for provider table
+        */
+       ddl = "ALTER TABLE provider ADD COLUMN hw_acceleration TEXT DEFAULT 'none'";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+}
+
+/*!
+ * \note
+ * From version 3 to 4
+ * "provider" table should have "count" column.
+ * "count" will be used for limiting creatable count of instances for each widget.
+ * Every widget developer should describe their max count of creatable instances.
+ */
+static void upgrade_to_version_4(void)
+{
+       char *err;
+       static const char *ddl;
+
+       /*
+        * Step 1
+        * Create a new column for count to provider table.
+        */
+       ddl = "ALTER TABLE provider ADD COLUMN count INTEGER DEFAULT 0";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+}
+
+/*!
+ * \note
+ * From version 2 to 3
+ * mouse_event is deleted from client table
+ * mouse_event is added to box_size table
+ *
+ * Every size has their own configuration for mouse_event flag.
+ */
+static void upgrade_to_version_3(void)
+{
+       char *err;
+       static const char *ddl;
+       static const char *dml;
+       sqlite3_stmt *select_stmt;
+       int ret;
+
+       /*
+        * Step 1
+        * Create a new column for mouse_event to box_size table.
+        */
+       ddl = "ALTER TABLE box_size ADD COLUMN mouse_event INTEGER DEFAULT 0";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       /*
+        * Step 2
+        * Copy mouse_event values from the client to the box_size table.
+        */
+       dml = "SELECT pkgid, mouse_event FROM client";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &select_stmt, NULL);
+       if (ret == SQLITE_OK) {
+               sqlite3_stmt *update_stmt;
+
+               dml = "UPDATE box_size SET mouse_event = ? WHERE pkgid = ?";
+               ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &update_stmt, NULL);
+               if (ret == SQLITE_OK) {
+                       int mouse_event;
+                       const char *pkgid;
+
+                       while (sqlite3_step(select_stmt) == SQLITE_ROW) {
+                               pkgid = (const char *)sqlite3_column_text(select_stmt, 0);
+                               if (!pkgid) {
+                                       ErrPrint("Package Id is not valid\n");
+                                       continue;
+                               }
+
+                               mouse_event = sqlite3_column_int(select_stmt, 1);
+
+                               ret = sqlite3_bind_int(update_stmt, 1, mouse_event);
+                               if (ret != SQLITE_OK) {
+                                       ErrPrint("Failed to bind mouse_event [%s], [%d]\n", pkgid, mouse_event);
+                                       sqlite3_reset(update_stmt);
+                                       sqlite3_clear_bindings(update_stmt);
+                                       continue;
+                               }
+
+                               ret = sqlite3_bind_text(update_stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
+                               if (ret != SQLITE_OK) {
+                                       ErrPrint("Failed to bind pkgid [%s], [%d]\n", pkgid, mouse_event);
+                                       sqlite3_reset(update_stmt);
+                                       sqlite3_clear_bindings(update_stmt);
+                                       continue;
+                               }
+
+                               ret = sqlite3_step(update_stmt);
+                               if (ret != SQLITE_DONE) {
+                                       ErrPrint("Failed to execute DML: %d\n", ret);
+                                       sqlite3_reset(update_stmt);
+                                       sqlite3_clear_bindings(update_stmt);
+                                       continue;
+                               }
+
+                               sqlite3_reset(update_stmt);
+                               sqlite3_clear_bindings(update_stmt);
+                       }
+
+                       sqlite3_finalize(update_stmt);
+               } else {
+                       ErrPrint("Failed to execute DML\n");
+               }
+
+               sqlite3_reset(select_stmt);
+               sqlite3_clear_bindings(select_stmt);
+               sqlite3_finalize(select_stmt);
+       } else {
+               ErrPrint("Failed to prepare the initial DML(%s)\n", sqlite3_errmsg(s_info.handle));
+       }
+
+       /*
+        * Step 3
+        * Drop a column from the client table
+        */
+       ddl = "ALTER TABLE client DROP COLUMN mouse_event";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return;
+}
+
+void db_upgrade_db_schema(void)
+{
+       int version;
+
+       version = get_version();
+
+       switch (version) {
+       case -ENOSYS:
+               db_create_version();
+               /* Need to create version table */
+       case -ENOENT:
+               if (set_version(CUR_VER) < 0) {
+                       ErrPrint("Failed to set version\n");
+               }
+               /* Need to set version */
+       case 1:
+               upgrade_pkgmap_for_category();
+       case 2:
+               upgrade_to_version_3();
+       case 3:
+               upgrade_to_version_4();
+       case 4:
+               upgrade_to_version_5();
+       default:
+               /* Need to update version */
+               DbgPrint("Old version: %d\n", version);
+               if (update_version(CUR_VER) < 0) {
+                       ErrPrint("Failed to update version\n");
+               }
+       case CUR_VER:
+               break;
+       }
+}
+
+static inline int db_create_pkgmap(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE pkgmap ( pkgid TEXT PRIMARY KEY NOT NULL, appid TEXT, uiapp TEXT, prime INTEGER, category TEXT )";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_insert_pkgmap(const char *appid, const char *pkgid, const char *uiappid, int primary, const char *category)
+{
+       int ret;
+       static const char *dml;
+       sqlite3_stmt *stmt;
+
+       dml = "INSERT INTO pkgmap ( appid, pkgid, uiapp, prime, category ) VALUES (? ,?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, uiappid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 4, primary);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 5, category, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_pkgmap(const char *pkgid)
+{
+       int ret;
+       static const char *dml;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM pkgmap WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_provider(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE provider (" \
+                  "pkgid TEXT PRIMARY KEY NOT NULL, network INTEGER, " \
+                  "abi TEXT, secured INTEGER, box_type INTEGER, " \
+                  "box_src TEXT, box_group TEXT, gbar_type INTEGER, " \
+                  "gbar_src TEXT, gbar_group TEXT, libexec TEXT, timeout INTEGER, period TEXT, script TEXT, pinup INTEGER, "\
+                  "count INTEGER, direct_input INTEGER DEFAULT 0, hw_acceleration TEXT DEFAULT 'none', "\
+                  "FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_remove_provider(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM provider WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+static int db_insert_provider(struct widget *widget)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+       char *abi = (char *)widget->abi;
+       char *box_src = (char *)widget->widget_src;
+       char *box_group = (char *)widget->widget_group;
+       char *gbar_src = (char *)widget->gbar_src;
+       char *gbar_group = (char *)widget->gbar_group;
+       char *libexec = (char *)widget->libexec;
+       char *timeout = (char *)widget->timeout;
+       char *period = (char *)widget->period;
+       char *script = (char *)widget->script;
+       char *hw_acceleration = (char *)widget->hw_acceleration;
+
+       if (!abi) {
+               abi = "c";
+       }
+
+       if (!timeout) {
+               timeout = "10";
+       }
+
+       if (!period) {
+               period = "0.0";
+       }
+
+       if (!script) {
+               script = "edje";
+       }
+
+       if (!hw_acceleration) {
+               hw_acceleration = "none";
+       }
+
+       dml = "INSERT INTO provider ( pkgid, network, abi, secured, box_type, box_src, box_group, gbar_type, gbar_src, gbar_group, libexec, timeout, period, script, pinup, count, direct_input, hw_acceleration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, (char *)widget->pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 2, widget->network);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, abi, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+       ret = sqlite3_bind_int(stmt, 4, widget->secured);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 5, widget->widget_type);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 6, box_src, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 7, box_group, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 8, widget->gbar_type);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 9, gbar_src, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 10, gbar_group, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 11, libexec, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 12, atoi(timeout));
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 13, period, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 14, script, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 15, widget->pinup);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 16, widget->count);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 17, widget->direct_input);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 18, hw_acceleration, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_client(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE client (" \
+                  "pkgid TEXT PRIMARY KEY NOT NULL, icon TEXT, name TEXT, " \
+                  "auto_launch TEXT, gbar_size TEXT, content TEXT, nodisplay INTEGER, setup TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_insert_client(struct widget *widget)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "INSERT INTO client ( pkgid, icon, name, auto_launch, gbar_size, content, nodisplay, setup ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, (char *)widget->pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, (char *)widget->icon, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, (char *)widget->name, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 4, (char *)widget->auto_launch, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 5, (char *)widget->gbar_size, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 6, (char *)widget->content, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 7, widget->nodisplay);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 8, (char *)widget->setup, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_client(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM client WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_i18n(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE i18n ( pkgid TEXT NOT NULL, lang TEXT COLLATE NOCASE, name TEXT, " \
+                  "icon TEXT, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_insert_i18n(const char *pkgid, const char *lang, const char *name, const char *icon)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       DbgPrint("%s - lang[%s] name[%s] icon[%s]\n", pkgid, lang, name, icon);
+       dml = "INSERT INTO i18n ( pkgid, lang, name, icon ) VALUES (?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, lang, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, name, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 4, icon, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_i18n(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM i18n WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               DbgPrint("No changes\n");
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_group(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE groupinfo ( id INTEGER PRIMARY KEY AUTOINCREMENT, cluster TEXT NOT NULL, category TEXT NOT NULL, pkgid TEXT NOT NULL, FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_insert_group(const char *pkgid, const char *cluster, const char *category)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "INSERT INTO groupinfo ( cluster, category, pkgid ) VALUES (?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, cluster, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, category, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static int db_get_group_id(const char *cluster, const char *category)
+{
+       static const char *dml = "SELECT id FROM groupinfo WHERE cluster = ? AND category = ?";
+       sqlite3_stmt *stmt;
+       int ret;
+
+       if (!cluster || !category) {
+               ErrPrint("Invalid argument\n");
+               return -EINVAL;
+       }
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to prepare the initial DML(%s)\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = -EIO;
+       if (sqlite3_bind_text(stmt, 1, cluster, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a cluster(%s) - %s\n", cluster, sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       if (sqlite3_bind_text(stmt, 2, category, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a category(%s) - %s\n", category, sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ErrPrint("Failed to execute the DML for %s - %s\n", cluster, category);
+               goto out;
+       }
+
+       ret = sqlite3_column_int(stmt, 0);
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_group(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM groupinfo WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               DbgPrint("No changes\n");
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_groupmap(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE groupmap (option_id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, pkgid TEXT NOT NULL, ctx_item TEXT NOT NULL, FOREIGN KEY(id) REFERENCES groupinfo(id), FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_get_option_id(int id, const char *pkgid, const char *ctx_item)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "SELECT option_id FROM groupmap WHERE id = ? AND pkgid = ? AND ctx_item = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_int(stmt, 1, id);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, ctx_item, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_column_int(stmt, 0);
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_insert_groupmap(int id, const char *pkgid, const char *ctx_item)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       DbgPrint("%d (%s) add to groupmap\n", id, pkgid);
+
+       dml = "INSERT INTO groupmap ( id, pkgid, ctx_item ) VALUES (?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_int(stmt, 1, id);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, ctx_item, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_groupmap(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM groupmap WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               DbgPrint("No changes\n");
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_option(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE option ( pkgid TEXT NOT NULL, option_id INTEGER, key TEXT NOT NULL, value TEXT NOT NULL, " \
+                  "FOREIGN KEY(option_id) REFERENCES groupmap(option_id), " \
+                  "FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static inline int db_insert_option(const char *pkgid, int option_id, const char *key, const char *value)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "INSERT INTO option (pkgid, option_id, key, value) VALUES (?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 2, option_id);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, key, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 4, value, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_option(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM option WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               DbgPrint("No changes\n");
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_create_box_size(void)
+{
+       char *err;
+       static const char *ddl;
+
+       ddl = "CREATE TABLE box_size ( pkgid TEXT NOT NULL, size_type INTEGER, preview TEXT, touch_effect INTEGER, need_frame INTEGER, mouse_event INTEGER " \
+                  "FOREIGN KEY(pkgid) REFERENCES pkgmap(pkgid) ON DELETE CASCADE)";
+       if (sqlite3_exec(s_info.handle, ddl, NULL, NULL, &err) != SQLITE_OK) {
+               ErrPrint("Failed to execute the DDL (%s)\n", err);
+               return -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               ErrPrint("No changes to DB\n");
+       }
+
+       return 0;
+}
+
+static int db_insert_box_size(const char *pkgid, int size_type, const char *preview, int touch_effect, int need_frame, int mouse_event)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       DbgPrint("box size: %s - %d (%s) is added\n", pkgid, size_type, preview);
+       dml = "INSERT INTO box_size ( pkgid, size_type, preview, touch_effect, need_frame, mouse_event ) VALUES (?, ?, ?, ?, ?, ?)";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 2, size_type);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 3, preview, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 4, touch_effect);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 5, need_frame);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = sqlite3_bind_int(stmt, 6, mouse_event);
+       if (ret != SQLITE_OK) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               ErrPrintWithConsole("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int db_remove_box_size(const char *pkgid)
+{
+       static const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+
+       dml = "DELETE FROM box_size WHERE pkgid = ?";
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+               goto out;
+       }
+
+       ret = 0;
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               DbgPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = -EIO;
+       }
+
+       if (sqlite3_changes(s_info.handle) == 0) {
+               DbgPrint("No changes\n");
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline void db_create_table(void)
+{
+       int ret;
+       begin_transaction();
+
+       ret = db_create_pkgmap();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_provider();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_client();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_i18n();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_box_size();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_group();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_option();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       ret = db_create_groupmap();
+       if (ret < 0) {
+               rollback_transaction();
+               return;
+       }
+
+       commit_transaction();
+}
+
+int db_init(void)
+{
+       int ret;
+       struct stat stat;
+
+       ret = db_util_open(s_info.dbfile, &s_info.handle, DB_UTIL_REGISTER_HOOK_METHOD);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to open a DB\n");
+               return -EIO;
+       }
+
+       if (lstat(s_info.dbfile, &stat) < 0) {
+               ErrPrint("%d\n", errno);
+               db_util_close(s_info.handle);
+               s_info.handle = NULL;
+               return -EIO;
+       }
+
+       if (!S_ISREG(stat.st_mode)) {
+               ErrPrint("Invalid file\n");
+               db_util_close(s_info.handle);
+               s_info.handle = NULL;
+               return -EINVAL;
+       }
+
+       if (!stat.st_size) {
+               db_create_table();
+       }
+
+       return 0;
+}
+
+int db_fini(void)
+{
+       if (!s_info.handle) {
+               return 0;
+       }
+
+       db_util_close(s_info.handle);
+       s_info.handle = NULL;
+
+       return 0;
+}
+
+int db_check(void)
+{
+       if (s_info.handle == NULL)
+       {
+               return 0;
+       }
+
+       return 1;
+}
+
+static int validate_pkgid(const char *appid, const char *pkgid)
+{
+       /* Just return 1 Always */
+       return 1 || !strncmp(appid, pkgid, strlen(appid));
+}
+
+static int widget_destroy(struct widget *widget)
+{
+       struct dlist *l;
+       struct dlist *n;
+       struct i18n *i18n;
+       struct group *group;
+       struct option *option;
+       struct dlist *il;
+       struct dlist *in;
+
+       xmlFree(widget->auto_launch);
+       xmlFree(widget->pkgid);
+       xmlFree(widget->abi);
+       xmlFree(widget->name);
+       xmlFree(widget->icon);
+       xmlFree(widget->widget_src);
+       xmlFree(widget->widget_group);
+       xmlFree(widget->gbar_src);
+       xmlFree(widget->gbar_group);
+       xmlFree(widget->gbar_size);
+       xmlFree(widget->libexec);
+       xmlFree(widget->script);
+       xmlFree(widget->period);
+       xmlFree(widget->content);
+       xmlFree(widget->setup);
+       xmlFree(widget->category);
+       xmlFree(widget->preview[0]); /* 1x1 */
+       xmlFree(widget->preview[1]); /* 2x1 */
+       xmlFree(widget->preview[2]); /* 2x2 */
+       xmlFree(widget->preview[3]); /* 4x1 */
+       xmlFree(widget->preview[4]); /* 4x2 */
+       xmlFree(widget->preview[5]); /* 4x3 */
+       xmlFree(widget->preview[6]); /* 4x4 */
+       xmlFree(widget->preview[7]); /* 4x5 */
+       xmlFree(widget->preview[8]); /* 4x6 */
+       xmlFree(widget->preview[9]); /* easy 1x1 */
+       xmlFree(widget->preview[10]); /* easy 3x1 */
+       xmlFree(widget->preview[11]); /* easy 3x3 */
+       xmlFree(widget->preview[12]); /* full */
+       xmlFree(widget->hw_acceleration);
+
+       dlist_foreach_safe(widget->i18n_list, l, n, i18n) {
+               widget->i18n_list = dlist_remove(widget->i18n_list, l);
+               xmlFree(i18n->name);
+               xmlFree(i18n->icon);
+               xmlFree(i18n->lang);
+               free(i18n);
+       }
+
+       dlist_foreach_safe(widget->group_list, l, n, group) {
+               widget->group_list = dlist_remove(widget->group_list, l);
+               DbgPrint("Release %s/%s\n", group->cluster, group->category);
+
+               if (group->ctx_item) {
+                       dlist_foreach_safe(group->option_list, il, in, option) {
+                               group->option_list = dlist_remove(group->option_list, il);
+                               DbgPrint("Release option %s(%s)\n", option->key, option->value);
+                               xmlFree(option->key);
+                               xmlFree(option->value);
+                               free(option);
+                       }
+                       xmlFree(group->ctx_item);
+               }
+
+               xmlFree(group->cluster);
+               xmlFree(group->category);
+               free(group);
+       }
+
+       free(widget);
+       return 0;
+}
+
+static void update_i18n_name(struct widget *widget, xmlNodePtr node)
+{
+       struct i18n *i18n;
+       struct dlist *l;
+       xmlChar *lang;
+       xmlChar *name;
+
+       name = xmlNodeGetContent(node);
+       if (!name) {
+               ErrPrint("Invalid tag\n");
+               return;
+       }
+
+       lang = xmlNodeGetLang(node);
+       if (!lang) {
+               if (widget->name) {
+                       DbgPrint("Override default name: %s\n", widget->name);
+                       xmlFree(widget->name);
+               }
+
+               widget->name = name;
+               return;
+       }
+
+       dlist_foreach(widget->i18n_list, l, i18n) {
+               if (!xmlStrcasecmp(i18n->lang, lang)) {
+                       if (i18n->name) {
+                               DbgPrint("Override name: %s\n", i18n->name);
+                               xmlFree(i18n->name);
+                       }
+
+                       i18n->name = name;
+                       return;
+               }
+       }
+
+       i18n = calloc(1, sizeof(*i18n));
+       if (!i18n) {
+               ErrPrint("calloc: %d\n", errno);
+               xmlFree(name);
+               xmlFree(lang);
+               return;
+       }
+
+       i18n->name = name;
+       i18n->lang = lang;
+       DbgPrint("Label[%s] - [%s] added\n", i18n->lang, i18n->name);
+       widget->i18n_list = dlist_append(widget->i18n_list, i18n);
+}
+
+static void update_i18n_icon(struct widget *widget, xmlNodePtr node)
+{
+       struct i18n *i18n;
+       struct dlist *l;
+       xmlChar *lang;
+       xmlChar *icon;
+
+       icon = xmlNodeGetContent(node);
+       if (!icon) {
+               ErrPrint("Invalid tag\n");
+               return;
+       }
+
+       lang = xmlNodeGetLang(node);
+       if (!lang) {
+               if (widget->icon) {
+                       DbgPrint("Override default icon: %s\n", widget->icon);
+                       xmlFree(widget->icon);
+               }
+
+               widget->icon = icon;
+               return;
+       }
+
+       dlist_foreach(widget->i18n_list, l, i18n) {
+               if (!xmlStrcasecmp(i18n->lang, lang)) {
+                       if (i18n->icon) {
+                               DbgPrint("Override icon %s for %s\n", i18n->icon, i18n->name);
+                               xmlFree(i18n->icon);
+                       }
+
+                       i18n->icon = icon;
+                       icon = abspath_strdup(i18n->icon);
+                       if (!icon) {
+                               ErrPrint("strdup: %d\n", errno);
+                       } else {
+                               abspath((char *)i18n->icon, (char *)icon);
+                               xmlFree(i18n->icon);
+                               i18n->icon = icon;
+                       }
+                       return;
+               }
+       }
+
+       i18n = calloc(1, sizeof(*i18n));
+       if (!i18n) {
+               ErrPrint("calloc: %d\n", errno);
+               xmlFree(icon);
+               xmlFree(lang);
+               return;
+       }
+
+       i18n->icon = icon;
+       icon = abspath_strdup(i18n->icon);
+       if (!icon) {
+               ErrPrint("strdup: %d\n", errno);
+       } else {
+               abspath((char *)i18n->icon, (char *)icon);
+               xmlFree(i18n->icon);
+               i18n->icon = icon;
+       }
+       i18n->lang = lang;
+       DbgPrint("Icon[%s] - [%s] added\n", i18n->lang, i18n->icon);
+       widget->i18n_list = dlist_append(widget->i18n_list, i18n);
+}
+
+static void update_launch(struct widget *widget, xmlNodePtr node)
+{
+       xmlChar *launch;
+
+       launch = xmlNodeGetContent(node);
+       if (!launch) {
+               DbgPrint("Has no launch\n");
+               return;
+       }
+
+       if (widget->auto_launch) {
+               xmlFree(widget->auto_launch);
+       }
+
+       widget->auto_launch = xmlStrdup(launch);
+       if (!widget->auto_launch) {
+               ErrPrint("Failed to duplicate string: %s\n", (char *)launch);
+               return;
+       }
+}
+
+static int update_category(struct widget *widget, xmlNodePtr node)
+{
+       xmlChar *category;
+
+       category = xmlGetProp(node, (const xmlChar *)"name");
+       if (!category) {
+               DbgPrint("Has no valid category\n");
+               return 0;
+       }
+
+       if (!xmlStrcasecmp(category, (const xmlChar *)CATEGORY_WATCH_CLOCK)) {
+               ErrPrint("Widget tries to install WATCH: %s\n", widget->pkgid);
+               return -EINVAL;
+       }
+
+       if (widget->category) {
+               xmlFree(widget->category);
+       }
+
+       widget->category = xmlStrdup(category);
+       if (!widget->category) {
+               ErrPrint("Failed to duplicate string: %s\n", (char *)category);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void update_ui_appid(struct widget *widget, xmlNodePtr node)
+{
+       xmlChar *uiapp;
+       uiapp = xmlNodeGetContent(node);
+       if (!uiapp) {
+               DbgPrint("Has no valid ui-appid\n");
+               return;
+       }
+
+       if (widget->uiapp) {
+               xmlFree(widget->uiapp);
+       }
+
+       widget->uiapp = xmlStrdup(uiapp);
+       if (!widget->uiapp) {
+               ErrPrint("Failed to duplicate string: %s\n", (char *)uiapp);
+               return;
+       }
+}
+
+static void update_setup(struct widget *widget, xmlNodePtr node)
+{
+       xmlChar *setup;
+       setup = xmlNodeGetContent(node);
+       if (!setup) {
+               DbgPrint("Has no setup\n");
+               return;
+       }
+
+       if (widget->setup) {
+               xmlFree(widget->setup);
+       }
+
+       widget->setup = xmlStrdup(setup);
+       if (!widget->setup) {
+               ErrPrint("Failed to duplicate string: %s\n", (char *)setup);
+               return;
+       }
+}
+
+static void update_content(struct widget *widget, xmlNodePtr node)
+{
+       xmlChar *content;
+       content = xmlNodeGetContent(node);
+       if (!content) {
+               DbgPrint("Has no content\n");
+               return;
+       }
+
+       if (widget->content) {
+               xmlFree(widget->content);
+       }
+
+       widget->content = xmlStrdup(content);
+       if (!widget->content) {
+               ErrPrint("Failed to duplicate string: %s\n", (char *)content);
+               return;
+       }
+}
+
+static void update_size_info(struct widget *widget, int idx, xmlNodePtr node)
+{
+       if (xmlHasProp(node, (const xmlChar *)"preview")) {
+               xmlChar *tmp_preview;
+
+               widget->preview[idx] = xmlGetProp(node, (const xmlChar *)"preview");
+
+               tmp_preview = abspath_strdup(widget->preview[idx]);
+               if (!tmp_preview) {
+                       ErrPrint("strdup: %d\n", errno);
+               } else {
+                       abspath((char *)widget->preview[idx], (char *)tmp_preview);
+                       xmlFree(widget->preview[idx]);
+                       widget->preview[idx] = tmp_preview;
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"need_frame")) {
+               xmlChar *need_frame;
+
+               need_frame = xmlGetProp(node, (const xmlChar *)"need_frame");
+               if (need_frame) {
+                       widget->need_frame[idx] = !xmlStrcasecmp(need_frame, (const xmlChar *)"true");
+                       xmlFree(need_frame);
+               } else {
+                       widget->need_frame[idx] = widget->default_need_frame;
+               }
+       } else {
+               widget->need_frame[idx] = widget->default_need_frame;
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"touch_effect")) {
+               xmlChar *touch_effect;
+
+               touch_effect = xmlGetProp(node, (const xmlChar *)"touch_effect");
+               if (touch_effect) {
+                       widget->touch_effect[idx] = !xmlStrcasecmp(touch_effect, (const xmlChar *)"true");
+                       xmlFree(touch_effect);
+               } else {
+                       widget->touch_effect[idx] = widget->default_touch_effect;
+               }
+       } else {
+               widget->touch_effect[idx] = widget->default_touch_effect;
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"mouse_event")) {
+               xmlChar *mouse_event;
+
+               mouse_event = xmlGetProp(node, (const xmlChar *)"mouse_event");
+               if (mouse_event) {
+                       widget->mouse_event[idx] = !xmlStrcasecmp(mouse_event, (const xmlChar *)"true");
+                       xmlFree(mouse_event);
+               } else {
+                       widget->mouse_event[idx] = widget->default_mouse_event;
+               }
+       } else {
+               widget->mouse_event[idx] = widget->default_mouse_event;
+       }
+}
+
+static void update_box(struct widget *widget, xmlNodePtr node)
+{
+       if (!xmlHasProp(node, (const xmlChar *)"type")) {
+               widget->widget_type = WIDGET_TYPE_FILE;
+       } else {
+               xmlChar *type;
+
+               type = xmlGetProp(node, (const xmlChar *)"type");
+               if (!type) {
+                       ErrPrint("Type is NIL\n");
+                       widget->widget_type = WIDGET_TYPE_FILE;
+               } else {
+                       if (!xmlStrcasecmp(type, (const xmlChar *)"text")) {
+                               widget->widget_type = WIDGET_TYPE_TEXT;
+                       } else if (!xmlStrcasecmp(type, (const xmlChar *)"buffer")) {
+                               widget->widget_type = WIDGET_TYPE_BUFFER;
+                       } else if (!xmlStrcasecmp(type, (const xmlChar *)"script")) {
+                               widget->widget_type = WIDGET_TYPE_SCRIPT;
+                       } else if (!xmlStrcasecmp(type, (const xmlChar *)"elm")) {
+                               widget->widget_type = WIDGET_TYPE_UIFW;
+                       } else { /* Default */
+                               widget->widget_type = WIDGET_TYPE_FILE;
+                       }
+
+                       xmlFree(type);
+               }
+       }
+
+       if (!xmlHasProp(node, (const xmlChar *)"mouse_event")) {
+               widget->default_mouse_event = 0;
+       } else {
+               xmlChar *mouse_event;
+
+               mouse_event = xmlGetProp(node, (const xmlChar *)"mouse_event");
+               if (!mouse_event) {
+                       ErrPrint("mouse_event is NIL\n");
+                       widget->default_mouse_event = 0;
+               } else {
+                       widget->default_mouse_event = !xmlStrcasecmp(mouse_event, (const xmlChar *)"true");
+                       xmlFree(mouse_event);
+               }
+       }
+
+       if (!xmlHasProp(node, (const xmlChar *)"touch_effect")) {
+               widget->default_touch_effect = 1;
+       } else {
+               xmlChar *touch_effect;
+
+               touch_effect = xmlGetProp(node, (const xmlChar *)"touch_effect");
+               if (!touch_effect) {
+                       ErrPrint("default touch_effect is NIL\n");
+                       widget->default_touch_effect = 1;
+               } else {
+                       widget->default_touch_effect = !xmlStrcasecmp(touch_effect, (const xmlChar *)"true");
+                       xmlFree(touch_effect);
+               }
+       }
+
+       if (!xmlHasProp(node, (const xmlChar *)"need_frame")) {
+               widget->default_need_frame = 0;
+       } else {
+               xmlChar *need_frame;
+
+               need_frame = xmlGetProp(node, (const xmlChar *)"need_frame");
+               if (!need_frame) {
+                       ErrPrint("default need_frame is NIL\n");
+                       widget->default_need_frame = 0;
+               } else {
+                       widget->default_need_frame = !xmlStrcasecmp(need_frame, (const xmlChar *)"true");
+                       xmlFree(need_frame);
+               }
+       }
+
+       for (node = node->children; node; node = node->next) {
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"size")) {
+                       xmlChar *size;
+                       int is_easy = 0;
+
+                       size = xmlNodeGetContent(node);
+                       if (!size) {
+                               ErrPrint("Invalid size tag\n");
+                               continue;
+                       }
+
+                       if (xmlHasProp(node, (const xmlChar *)"mode")) {
+                               xmlChar *mode;
+                               mode = xmlGetProp(node, (const xmlChar *)"mode");
+                               if (mode) {
+                                       DbgPrint("Easy mode: %s\n", mode);
+                                       is_easy = !xmlStrcasecmp(mode, (const xmlChar *)"easy");
+                                       xmlFree(mode);
+                               }
+                       }
+
+                       if (!xmlStrcasecmp(size, (const xmlChar *)"1x1")) {
+                               if (is_easy) {
+                                       widget->size_list |= WIDGET_SIZE_TYPE_EASY_1x1;
+                                       update_size_info(widget, 9, node);
+                               } else {
+                                       widget->size_list |= WIDGET_SIZE_TYPE_1x1;
+                                       update_size_info(widget, 0, node);
+                               }
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"3x1")) {
+                               if (is_easy) {
+                                       widget->size_list |= WIDGET_SIZE_TYPE_EASY_3x1;
+                                       update_size_info(widget, 10, node);
+                               } else {
+                                       ErrPrint("Invalid size tag (%s)\n", size);
+                               }
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"3x3")) {
+                               if (is_easy) {
+                                       widget->size_list |= WIDGET_SIZE_TYPE_EASY_3x3;
+                                       update_size_info(widget, 11, node);
+                               } else {
+                                       ErrPrint("Invalid size tag (%s)\n", size);
+                               }
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"2x1")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_2x1;
+                               update_size_info(widget, 1, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"2x2")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_2x2;
+                               update_size_info(widget, 2, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x1")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x1;
+                               update_size_info(widget, 3, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x2")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x2;
+                               update_size_info(widget, 4, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x3")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x3;
+                               update_size_info(widget, 5, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x4")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x4;
+                               update_size_info(widget, 6, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x5")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x5;
+                               update_size_info(widget, 7, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"4x6")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_4x6;
+                               update_size_info(widget, 8, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"21x21")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_EASY_1x1;
+                               update_size_info(widget, 9, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"23x21")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_EASY_3x1;
+                               update_size_info(widget, 10, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"23x23")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_EASY_3x3;
+                               update_size_info(widget, 11, node);
+                       } else if (!xmlStrcasecmp(size, (const xmlChar *)"0x0")) {
+                               widget->size_list |= WIDGET_SIZE_TYPE_FULL;
+                               update_size_info(widget, 12, node);
+                       } else {
+                               ErrPrint("Invalid size tag (%s)\n", size);
+                       }
+
+                       xmlFree(size);
+               } else if (!xmlStrcasecmp(node->name, (const xmlChar *)"script")) {
+                       xmlChar *src;
+
+                       if (!xmlHasProp(node, (const xmlChar *)"src")) {
+                               ErrPrint("Invalid script tag. has no src\n");
+                               continue;
+                       }
+
+                       src = xmlGetProp(node, (const xmlChar *)"src");
+                       if (!src) {
+                               ErrPrint("Invalid script tag. src is NIL\n");
+                               continue;
+                       }
+
+                       if (widget->widget_src) {
+                               DbgPrint("Override lb src: %s\n", widget->widget_src);
+                               xmlFree(widget->widget_src);
+                       }
+
+                       widget->widget_src = src;
+                       src = abspath_strdup(widget->widget_src);
+                       if (!src) {
+                               ErrPrint("strdup: %d\n", errno);
+                       } else {
+                               abspath((char *)widget->widget_src, (char *)src);
+                               xmlFree(widget->widget_src);
+                               widget->widget_src = src;
+                       }
+
+                       if (xmlHasProp(node, (const xmlChar *)"group")) {
+                               xmlChar *group;
+                               group = xmlGetProp(node, (const xmlChar *)"group");
+                               if (!group) {
+                                       ErrPrint("Group is NIL\n");
+                               } else {
+                                       if (widget->widget_group) {
+                                               DbgPrint("Override lb group: %s\n", widget->widget_group);
+                                               xmlFree(widget->widget_group);
+                                       }
+
+                                       widget->widget_group = group;
+                               }
+                       }
+               }
+       }
+}
+
+static void update_group(struct widget *widget, xmlNodePtr node)
+{
+       xmlNodePtr cluster;
+       xmlNodePtr category;
+       xmlNodePtr option_item;
+       xmlChar *cluster_name;
+       xmlChar *category_name;
+       xmlChar *ctx_item;
+
+       xmlChar *key;
+       xmlChar *value;
+
+       struct group *group;
+       struct option *option;
+
+       cluster = node;
+       for (cluster = cluster->children; cluster; cluster = cluster->next) {
+               if (xmlStrcasecmp(cluster->name, (const xmlChar *)"cluster")) {
+                       DbgPrint("Skip: %s\n", cluster->name);
+                       continue;
+               }
+
+               if (!xmlHasProp(cluster, (const xmlChar *)"name")) {
+                       ErrPrint("Invalid cluster, has no name\n");
+                       continue;
+               }
+
+               cluster_name = xmlGetProp(cluster, (const xmlChar *)"name");
+               if (!cluster_name) {
+                       ErrPrint("Invalid cluster name. NIL\n");
+                       continue;
+               }
+
+               for (category = cluster->children; category; category = category->next) {
+                       if (xmlStrcasecmp(category->name, (const xmlChar *)"category")) {
+                               DbgPrint("Skip: %s\n", category->name);
+                               continue;
+                       }
+
+                       if (!xmlHasProp(category, (const xmlChar *)"name")) {
+                               ErrPrint("Invalid category, has no name\n");
+                               continue;
+                       }
+
+                       category_name = xmlGetProp(category, (const xmlChar *)"name");
+                       if (!category_name) {
+                               ErrPrint("Invalid category name. NIL\n");
+                               continue;
+                       }
+
+                       group = calloc(1, sizeof(*group));
+                       if (!group) {
+                               ErrPrint("calloc: %d\n", errno);
+                               xmlFree(category_name);
+                               continue;
+                       }
+
+                       group->cluster = xmlStrdup(cluster_name);
+                       if (!group->cluster) {
+                               ErrPrint("xmlStrdup: %d\n", errno);
+                               xmlFree(category_name);
+                               free(group);
+                               continue;
+                       }
+
+                       group->category = category_name;
+                       widget->group_list = dlist_append(widget->group_list, group);
+
+                       if (!xmlHasProp(category, (const xmlChar *)"context")) {
+                               DbgPrint("%s, %s has no ctx info\n", group->cluster, group->category);
+                               continue;
+                       }
+
+                       ctx_item = xmlGetProp(category, (const xmlChar *)"context");
+                       if (!ctx_item) {
+                               ErrPrint("Failed to get context ID (%s, %s)\n", group->cluster, group->category);
+                               continue;
+                       }
+
+                       group->ctx_item = ctx_item;
+                       DbgPrint("Build group item: %s - %s - %s\n", group->cluster, group->category, group->ctx_item);
+
+                       for (option_item = category->children; option_item; option_item = option_item->next) {
+                               if (xmlStrcasecmp(option_item->name, (const xmlChar *)"option")) {
+                                       DbgPrint("Skip: %s\n", option_item->name);
+                                       continue;
+                               }
+
+                               if (!xmlHasProp(option_item, (const xmlChar *)"key")) {
+                                       ErrPrint("Invalid option, has no key\n");
+                                       continue;
+                               }
+
+                               if (!xmlHasProp(option_item, (const xmlChar *)"value")) {
+                                       ErrPrint("Invalid option, has no value\n");
+                                       continue;
+                               }
+
+                               key = xmlGetProp(option_item, (const xmlChar *)"key");
+                               if (!key) {
+                                       ErrPrint("Invalid key. NIL\n");
+                                       continue;
+                               }
+
+                               value = xmlGetProp(option_item, (const xmlChar *)"value");
+                               if (!value) {
+                                       ErrPrint("Invalid valid. NIL\n");
+                                       xmlFree(key);
+                                       continue;
+                               }
+
+                               option = calloc(1, sizeof(*option));
+                               if (!option) {
+                                       ErrPrint("calloc: %d\n", errno);
+                                       xmlFree(key);
+                                       xmlFree(value);
+                                       continue;
+                               }
+
+                               option->key = key;
+                               option->value = value;
+
+                               group->option_list = dlist_append(group->option_list, option);
+                       }
+               }
+
+               xmlFree(cluster_name);
+       }
+}
+
+static void update_glance_bar(struct widget *widget, xmlNodePtr node)
+{
+       if (!xmlHasProp(node, (const xmlChar *)"type")) {
+               widget->gbar_type = GBAR_TYPE_SCRIPT;
+       } else {
+               xmlChar *type;
+
+               type = xmlGetProp(node, (const xmlChar *)"type");
+               if (!type) {
+                       ErrPrint("type is NIL\n");
+                       return;
+               }
+
+               if (!xmlStrcasecmp(type, (const xmlChar *)"text")) {
+                       widget->gbar_type = GBAR_TYPE_TEXT;
+               } else if (!xmlStrcasecmp(type, (const xmlChar *)"buffer")) {
+                       widget->gbar_type = GBAR_TYPE_BUFFER;
+               } else if (!xmlStrcasecmp(type, (const xmlChar *)"elm")) {
+                       widget->gbar_type = GBAR_TYPE_UIFW;
+               } else {
+                       widget->gbar_type = GBAR_TYPE_SCRIPT;
+               }
+
+               xmlFree(type);
+       }
+
+       for (node = node->children; node; node = node->next) {
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"size")) {
+                       xmlChar *size;
+
+                       size = xmlNodeGetContent(node);
+                       if (!size) {
+                               ErrPrint("Invalid size tag\n");
+                               continue;
+                       }
+
+                       if (widget->gbar_size) {
+                               DbgPrint("Override pd size: %s\n", widget->gbar_size);
+                               xmlFree(widget->gbar_size);
+                       }
+                       widget->gbar_size = size;
+               } else if (!xmlStrcasecmp(node->name, (const xmlChar *)"script")) {
+                       xmlChar *src;
+
+                       if (!xmlHasProp(node, (const xmlChar *)"src")) {
+                               ErrPrint("Invalid script tag, has no src\n");
+                               continue;
+                       }
+
+                       src = xmlGetProp(node, (const xmlChar *)"src");
+                       if (!src) {
+                               ErrPrint("src is NIL\n");
+                               continue;
+                       }
+
+                       if (widget->gbar_src) {
+                               DbgPrint("Overide PD src: %s\n", widget->gbar_src);
+                               xmlFree(widget->gbar_src);
+                       }
+
+                       widget->gbar_src = src;
+                       src = abspath_strdup(widget->gbar_src);
+                       if (!src) {
+                               ErrPrint("strdup: %d\n", errno);
+                       } else {
+                               abspath((char *)widget->gbar_src, (char *)src);
+                               xmlFree(widget->gbar_src);
+                               widget->gbar_src = src;
+                       }
+
+                       if (xmlHasProp(node, (const xmlChar *)"group")) {
+                               xmlChar *group;
+                               group = xmlGetProp(node, (const xmlChar *)"group");
+                               if (!group) {
+                                       ErrPrint("Group is NIL\n");
+                               } else {
+                                       if (widget->gbar_group) {
+                                               DbgPrint("Override PD group : %s\n", widget->gbar_group);
+                                               xmlFree(widget->gbar_group);
+                                       }
+
+                                       widget->gbar_group = group;
+                               }
+                       }
+               } // script
+       } // for
+}
+
+static int db_insert_widget(struct widget *widget, const char *appid)
+{
+       struct dlist *l;
+       struct dlist *il;
+       struct i18n *i18n;
+       struct group *group;
+       int ret;
+       int id;
+       struct option *option;
+
+       begin_transaction();
+       ret = db_insert_pkgmap(appid, (char *)widget->pkgid, (char *)widget->uiapp, widget->primary, (char *)widget->category);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_insert_provider(widget);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_insert_client(widget);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       dlist_foreach(widget->i18n_list, l, i18n) {
+               ret = db_insert_i18n((char *)widget->pkgid, (char *)i18n->lang, (char *)i18n->name, (char *)i18n->icon);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_1x1) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_1x1, (char *)widget->preview[0], widget->touch_effect[0], widget->need_frame[0], widget->mouse_event[0]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_2x1) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_2x1, (char *)widget->preview[1], widget->touch_effect[1], widget->need_frame[1], widget->mouse_event[1]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_2x2) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_2x2, (char *)widget->preview[2], widget->touch_effect[2], widget->need_frame[2], widget->mouse_event[2]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x1) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x1, (char *)widget->preview[3], widget->touch_effect[3], widget->need_frame[3], widget->mouse_event[3]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x2) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x2, (char *)widget->preview[4], widget->touch_effect[4], widget->need_frame[4], widget->mouse_event[4]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x3) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x3, (char *)widget->preview[5], widget->touch_effect[5], widget->need_frame[5], widget->mouse_event[5]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x4) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x4, (char *)widget->preview[6], widget->touch_effect[6], widget->need_frame[6], widget->mouse_event[6]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x5) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x5, (char *)widget->preview[7], widget->touch_effect[7], widget->need_frame[7], widget->mouse_event[7]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_4x6) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_4x6, (char *)widget->preview[8], widget->touch_effect[8], widget->need_frame[8], widget->mouse_event[8]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_EASY_1x1) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_EASY_1x1, (char *)widget->preview[9], widget->touch_effect[9], widget->need_frame[9], widget->mouse_event[9]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_EASY_3x1) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_EASY_3x1, (char *)widget->preview[10], widget->touch_effect[10], widget->need_frame[10], widget->mouse_event[10]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_EASY_3x3) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_EASY_3x3, (char *)widget->preview[11], widget->touch_effect[11], widget->need_frame[11], widget->mouse_event[11]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       if (widget->size_list & WIDGET_SIZE_TYPE_FULL) {
+               ret = db_insert_box_size((char *)widget->pkgid, WIDGET_SIZE_TYPE_FULL, (char *)widget->preview[12], widget->touch_effect[12], widget->need_frame[12], widget->mouse_event[12]);
+               if (ret < 0) {
+                       goto errout;
+               }
+       }
+
+       dlist_foreach(widget->group_list, l, group) {
+               /* group ID "id" */
+               id = db_get_group_id((char *)group->cluster, (char *)group->category);
+               if (id < 0) {
+                       int ret;
+
+                       ret = db_insert_group((char *)widget->pkgid, (char *)group->cluster, (char *)group->category);
+                       if (ret < 0) {
+                               ErrPrint("[%s]-[%s] is not exists\n", group->cluster, group->category);
+                               continue;
+                       }
+
+                       DbgPrint("New group name is built - %s/%s\n", group->cluster, group->category);
+                       id = db_get_group_id((char *)group->cluster, (char *)group->category);
+                       if (id < 0) {
+                               ErrPrint("Failed to get group id for %s/%s\n", group->cluster, group->category);
+                               continue;
+                       }
+               }
+
+               if (!group->ctx_item) {
+                       DbgPrint("%s, %s - has no ctx info\n", group->cluster, group->category);
+                       continue;
+               }
+
+               ret = db_insert_groupmap(id, (char *)widget->pkgid, (char *)group->ctx_item);
+               if (ret < 0) {
+                       goto errout;
+               }
+
+               /* REUSE "id" from here , option ID */
+               id = db_get_option_id(id, (char *)widget->pkgid, (char *)group->ctx_item);
+               if (id < 0) {
+                       goto errout;
+               }
+
+               dlist_foreach(group->option_list, il, option) {
+                       ret = db_insert_option((char *)widget->pkgid, id, (char *)option->key, (char *)option->value);
+                       if (ret < 0) {
+                               goto errout;
+                       }
+               }
+       }
+
+       commit_transaction();
+       widget_destroy(widget);
+       return 0;
+
+errout:
+       ErrPrint("ROLLBACK\n");
+       rollback_transaction();
+       widget_destroy(widget);
+       return ret;
+}
+
+int db_install_widget(xmlNodePtr node, const char *appid)
+{
+       struct widget *widget;
+       xmlChar *pkgid;
+       xmlChar *tmp;
+
+       if (!xmlHasProp(node, (const xmlChar *)"appid")) {
+               ErrPrint("Missing appid\n");
+               return -EINVAL;
+       }
+
+       pkgid = xmlGetProp(node, (const xmlChar *)"appid");
+       if (!pkgid || !validate_pkgid(appid, (char *)pkgid)) {
+               ErrPrint("Invalid appid\n");
+               xmlFree(pkgid);
+               return -EINVAL;
+       }
+
+       DbgPrint("appid: %s\n", (char *)pkgid);
+
+       widget = calloc(1, sizeof(*widget));
+       if (!widget) {
+               ErrPrint("calloc: %d\n", errno);
+               xmlFree(pkgid);
+               return -ENOMEM;
+       }
+
+       widget->pkgid = pkgid;
+
+       if (xmlHasProp(node, (const xmlChar *)"count")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"count");
+               if (tmp && sscanf((const char *)tmp, "%d", &widget->count) != 1) {
+                       ErrPrint("Invalid syntax: %s\n", (const char *)tmp);
+               }
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"primary")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"primary");
+               widget->primary = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"script")) {
+               widget->script = xmlGetProp(node, (const xmlChar *)"script");
+               if (!widget->script) {
+                       ErrPrint("script is NIL\n");
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"nodisplay")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"nodisplay");
+               widget->nodisplay = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"pinup")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"pinup");
+               widget->pinup = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"period")) {
+               widget->period = xmlGetProp(node, (const xmlChar *)"period");
+               if (!widget->period) {
+                       ErrPrint("Period is NIL\n");
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"timeout")) {
+               widget->timeout = xmlGetProp(node, (const xmlChar *)"timeout");
+               if (!widget->timeout) {
+                       ErrPrint("Timeout is NIL\n");
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"secured")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"secured");
+               widget->secured = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"network")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"network");
+               widget->network = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"direct_input")) {
+               tmp = xmlGetProp(node, (const xmlChar *)"direct_input");
+               widget->direct_input = tmp && !xmlStrcasecmp(tmp, (const xmlChar *)"true");
+               xmlFree(tmp);
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"hw-acceleration")) {
+               widget->hw_acceleration = xmlGetProp(node, (const xmlChar *)"hw-acceleration");
+               if (!widget->hw_acceleration) {
+                       ErrPrint("hw-acceleration is NIL\n");
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"abi")) {
+               widget->abi = xmlGetProp(node, (const xmlChar *)"abi");
+               if (!widget->abi) {
+                       ErrPrint("ABI is NIL\n");
+                       widget_destroy(widget);
+                       return -EFAULT;
+               }
+       } else {
+               widget->abi = xmlStrdup((const xmlChar *)"c");
+               if (!widget->abi) {
+                       ErrPrint("xmlstrdup: %d\n", errno);
+                       widget_destroy(widget);
+                       return -ENOMEM;
+               }
+       }
+
+       if (xmlHasProp(node, (const xmlChar *)"libexec")) {
+               xmlChar *tmp_libexec;
+
+               widget->libexec = xmlGetProp(node, (const xmlChar *)"libexec");
+               if (!widget->libexec) {
+                       ErrPrint("libexec is NIL\n");
+                       widget_destroy(widget);
+                       return -EFAULT;
+               }
+
+               tmp_libexec = abspath_strdup(widget->libexec);
+               if (!tmp_libexec) {
+                       ErrPrint("strdup: %d\n", errno);
+                       widget_destroy(widget);
+                       return -EFAULT;
+               }
+
+               abspath((char *)widget->libexec, (char *)tmp_libexec);
+               xmlFree(widget->libexec);
+               widget->libexec = tmp_libexec;
+       } else if (!xmlStrcasecmp(widget->abi, (const xmlChar *)"c") || !xmlStrcasecmp(widget->abi, (const xmlChar *)"cpp")) {
+               char *filename;
+               int len;
+
+               len = strlen((char *)widget->pkgid) + strlen("/libexec/liblive-.so") + 1;
+
+               filename = malloc(len);
+               if (!filename) {
+                       widget_destroy(widget);
+                       return -ENOMEM;
+               }
+
+               snprintf(filename, len, "/libexec/liblive-%s.so", widget->pkgid);
+               widget->libexec = xmlStrdup((xmlChar *)filename);
+               DbgPrint("Use the default libexec: %s\n", filename);
+               free(filename);
+
+               if (!widget->libexec) {
+                       widget_destroy(widget);
+                       return -ENOMEM;
+               }
+       }
+
+       for (node = node->children; node; node = node->next) {
+               if (!xmlStrcmp(node->name, (const xmlChar *)"text")) {
+                       continue;
+               }
+
+               DbgPrint("Nodename: %s\n", node->name);
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"label")) {
+                       update_i18n_name(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"icon")) {
+                       update_i18n_icon(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"box")) {
+                       update_box(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"glancebar")) {
+                       update_glance_bar(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"group")) {
+                       update_group(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"content")) {
+                       update_content(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"setup")) {
+                       update_setup(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"launch")) {
+                       update_launch(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"ui-appid")) {
+                       update_ui_appid(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"category")) {
+                       if (update_category(widget, node) < 0) {
+                               widget_destroy(widget);
+                               return -EINVAL;
+                       }
+
+                       continue;
+               }
+       }
+
+       return db_insert_widget(widget, appid);
+}
+
+int db_install_watchapp(xmlNodePtr node, const char *appid)
+{
+       struct widget *widget;
+       xmlChar *pkgid;
+
+       if (!xmlHasProp(node, (const xmlChar *)"appid")) {
+               ErrPrint("Missing appid\n");
+               return -EINVAL;
+       }
+
+       pkgid = xmlGetProp(node, (const xmlChar *)"appid");
+       if (!pkgid || !validate_pkgid(appid, (char *)pkgid)) {
+               ErrPrint("Invalid appid\n");
+               xmlFree(pkgid);
+               return -EINVAL;
+       }
+
+       DbgPrint("appid: %s\n", (char *)pkgid);
+
+       widget = calloc(1, sizeof(*widget));
+       if (!widget) {
+               ErrPrint("strdup: %d\n", errno);
+               xmlFree(pkgid);
+               return -ENOMEM;
+       }
+
+       widget->pkgid = pkgid;
+
+       widget->primary = 1;
+       widget->secured = 1;
+       widget->nodisplay = 1;
+       widget->hw_acceleration = xmlStrdup((const xmlChar *)"use-sw"); //use-gl
+       widget->abi = xmlStrdup((const xmlChar *)"app");
+       widget->category = xmlStrdup((const xmlChar *)CATEGORY_WATCH_CLOCK);
+
+       widget->widget_type = WIDGET_TYPE_BUFFER;
+       widget->default_mouse_event = 1;
+       widget->default_touch_effect = 0;
+       widget->default_need_frame = 0;
+       widget->size_list = WIDGET_SIZE_TYPE_2x2;
+
+       if (xmlHasProp(node, (const xmlChar *)"exec")) {
+               widget->libexec = xmlGetProp(node, (const xmlChar *)"exec");
+               if (!widget->libexec) {
+                       ErrPrint("libexec is NIL\n");
+                       widget_destroy(widget);
+                       return -EFAULT;
+               }
+       }
+
+       for (node = node->children; node; node = node->next) {
+               if (!xmlStrcmp(node->name, (const xmlChar *)"text")) {
+                       continue;
+               }
+
+               DbgPrint("Nodename: %s\n", node->name);
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"label")) {
+                       update_i18n_name(widget, node);
+                       continue;
+               }
+
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)"icon")) {
+                       update_i18n_icon(widget, node);
+                       continue;
+               }
+       }
+
+       return db_insert_widget(widget, appid);
+}
+
+int db_uninstall(xmlNodePtr node, const char *appid)
+{
+       xmlChar *pkgid;
+       int ret;
+
+       if (!xmlHasProp(node, (const xmlChar *)"appid")) {
+               ErrPrint("Missing appid\n");
+               return -EINVAL;
+       }
+
+       pkgid = xmlGetProp(node, (const xmlChar *)"appid");
+       if (!validate_pkgid(appid, (char *)pkgid)) {
+               ErrPrint("Invalid package\n");
+               xmlFree(pkgid);
+               return -EINVAL;
+       }
+
+       begin_transaction();
+       ret = db_remove_box_size((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_remove_i18n((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_remove_client((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_remove_provider((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_remove_option((const char *)pkgid);
+       DbgPrint("Remove option: %d\n", ret);
+
+       ret = db_remove_groupmap((const char *)pkgid);
+       DbgPrint("Remove groupmap: %d\n", ret);
+
+       ret = db_remove_group((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       ret = db_remove_pkgmap((const char *)pkgid);
+       if (ret < 0) {
+               goto errout;
+       }
+
+       commit_transaction();
+       xmlFree(pkgid);
+
+       return 0;
+
+errout:
+       rollback_transaction();
+       xmlFree(pkgid);
+       return ret;
+}
+
+int pkglist_get_via_callback(const char *appid, int is_watch_widget, void (*cb)(const char *appid, const char *pkgid, int prime, void *data), void *data)
+{
+       const char *dml;
+       int ret;
+       sqlite3_stmt *stmt;
+       const char *pkgid;
+       int prime;
+       int cnt = 0;
+
+       if (!cb || !appid || !strlen(appid)) {
+               return -EINVAL;
+       }
+
+       if (!s_info.handle) {
+               if (db_init() < 0) {
+                       ErrPrint("Failed to init DB\n");
+                       return -EIO;
+               }
+       }
+
+       if (is_watch_widget) { /* Watch */
+               dml = "SELECT pkgid, prime FROM pkgmap WHERE appid = ? AND category = '" CATEGORY_WATCH_CLOCK "'";
+       } else { /* Widget */
+               dml = "SELECT pkgid, prime FROM pkgmap WHERE appid = ? AND (category IS NULL OR category <> '" CATEGORY_WATCH_CLOCK "')";
+       }
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to prepare the intial DML(%s)\n", sqlite3_errmsg(s_info.handle));
+               return -EIO;
+       }
+
+       ret = -EIO;
+       if (sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a cluster - %s\n", sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               pkgid = (const char *)sqlite3_column_text(stmt, 0);
+               if (!pkgid || !strlen(pkgid)) {
+                       continue;
+               }
+
+               prime = sqlite3_column_int(stmt, 1);
+               cb(appid, pkgid, prime, data);
+               cnt++;
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return cnt;
+}
+
+void delete_record_cb(const char *appid, const char *pkgid, int prime, void *data)
+{
+       int ret;
+
+       ErrPrintWithConsole("Remove old package info: appid(%s), pkgid(%s)\n", appid, pkgid);
+
+       ret = db_remove_box_size((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove box size: %d\n", ret);
+       }
+
+       ret = db_remove_i18n((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove i18n: %d\n", ret);
+       }
+
+       ret = db_remove_client((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove client: %d\n", ret);
+       }
+
+       ret = db_remove_provider((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove provider: %d\n", ret);
+       }
+
+       ret = db_remove_option((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove option: %d\n", ret);
+       }
+
+       ret = db_remove_groupmap((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove groupmap: %d\n", ret);
+       }
+
+       ret = db_remove_group((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove group: %d\n", ret);
+       }
+
+       ret = db_remove_pkgmap((char *)pkgid);
+       if (ret < 0) {
+               ErrPrint("Remove pkgmap: %d\n", ret);
+       }
+}
+
+/* End of a file */
diff --git a/pkgmgr_widget/common/src/dlist.c b/pkgmgr_widget/common/src/dlist.c
new file mode 100644 (file)
index 0000000..3ae571b
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "dlist.h"
+
+/*!
+ * \brief
+ * This dlist is called Modified Doubly Linked List.
+ *
+ * Noramlly, The dobule linked list contains address of previous and next element.
+ * This dlist also contains them, but the tail element only contains prev address.
+ *
+ * The head element's prev pointer indicates the last element.
+ * But the last element's next pointer indicates NIL.
+ *
+ * So we can find the last element while crawling this DList
+ * But we have to remember the address of the head element.
+ */
+
+struct dlist {
+       struct dlist *next;
+       struct dlist *prev;
+       void *data;
+};
+
+struct dlist *dlist_append(struct dlist *list, void *data)
+{
+       struct dlist *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               return NULL;
+       }
+
+       item->next = NULL;
+       item->data = data;
+
+       if (!list) {
+               item->prev = item;
+
+               list = item;
+       } else {
+               item->prev = list->prev;
+               item->prev->next = item;
+               list->prev = item;
+       }
+
+       assert(!list->prev->next && "item NEXT");
+
+       return list;
+}
+
+struct dlist *dlist_prepend(struct dlist *list, void *data)
+{
+       struct dlist *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               return NULL;
+       }
+
+       item->data = data;
+
+       if (!list) {
+               item->prev = item;
+               item->next = NULL;
+       } else {
+               if (list->prev->next) {
+                       list->prev->next = item;
+               }
+
+               item->prev = list->prev;
+               item->next = list;
+
+               list->prev = item;
+
+       }
+
+       return item;
+}
+
+struct dlist *dlist_remove(struct dlist *list, struct dlist *l)
+{
+       if (!list || !l) {
+               return NULL;
+       }
+
+       if (l == list) {
+               list = l->next;
+       } else {
+               l->prev->next = l->next;
+       }
+
+       if (l->next) {
+               l->next->prev = l->prev;
+       }
+       /*!
+        * \note
+        * If the removed entry 'l' has no next element, it is the last element.
+        * In this case, check the existence of the list first,
+        * and if the list is not empty, update the 'prev' of the list (which is a head element of the list) 
+        *
+        * If we didn't care about this, the head element(list) can indicates the invalid element.
+        */
+       else if (list) {
+               list->prev = l->prev;
+       }
+
+       free(l);
+       return list;
+}
+
+struct dlist *dlist_find_data(struct dlist *list, void *data)
+{
+       struct dlist *l;
+       void *_data;
+
+       dlist_foreach(list, l, _data) {
+               if (data == _data) {
+                       return l;
+               }
+       }
+
+       return NULL;
+}
+
+void *dlist_data(struct dlist *l)
+{
+       return l ? l->data : NULL;
+}
+
+struct dlist *dlist_next(struct dlist *l)
+{
+       return l ? l->next : NULL;
+}
+
+struct dlist *dlist_prev(struct dlist *l)
+{
+       return l ? l->prev : NULL;
+}
+
+int dlist_count(struct dlist *l)
+{
+       register int i;
+       struct dlist *n;
+       void *data;
+
+       i = 0;
+       dlist_foreach(l, n, data) {
+               i++;
+       }
+
+       return i;
+}
+
+struct dlist *dlist_nth(struct dlist *l, int nth)
+{
+       register int i;
+       struct dlist *n;
+
+       i = 0;
+       for (n = l; n; n = n->next) {
+               if (i == nth) {
+                       return n;
+               }
+               i++;
+       }
+
+       return NULL;
+}
+
+/* End of a file */
diff --git a/pkgmgr_widget/watch/CMakeLists.txt b/pkgmgr_widget/watch/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7ec0c53
--- /dev/null
@@ -0,0 +1,15 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(watch-application C)
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+ADD_LIBRARY(${PROJECT_NAME} MODULE
+       src/service_watchapp.c
+)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${bin_pkgs_LDFLAGS} common)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION "etc/package-manager/parserlib")
+
+# End of a file
diff --git a/pkgmgr_widget/watch/src/service_watchapp.c b/pkgmgr_widget/watch/src/service_watchapp.c
new file mode 100644 (file)
index 0000000..fe36772
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "common.h"
+
+#define WATCH_APP_TAG  "watch-application"
+#define EAPI __attribute__((visibility("default")))
+
+static inline int remove_all_watch(const char *appid)
+{
+       int cnt;
+
+    begin_transaction();
+    cnt = pkglist_get_via_callback(appid, 1, delete_record_cb, NULL);
+    commit_transaction();
+
+    if (cnt > 0) {
+               DbgPrint("Package[%s] is not deleted: %d\n", appid, cnt);
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_INSTALL(const char *appid)
+{
+    ErrPrintWithConsole("%s\n", appid);
+
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrintWithConsole("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+
+       return remove_all_watch(appid);
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_INSTALL(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    db_fini();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char *appid)
+{
+    xmlNodePtr node;
+    int ret;
+
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               ErrPrintWithConsole("Failed to init DB\n");
+               return -EIO;
+    }
+
+    node = xmlDocGetRootElement(docPtr);
+    if (!node) {
+               ErrPrintWithConsole("Invalid document\n");
+               return -EINVAL;
+    }
+
+    for (node = node->children; node; node = node->next) {
+               DbgPrint("node->name: %s\n", node->name);
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)WATCH_APP_TAG)) {
+                       ret = db_install_watchapp(node, appid);
+                       if (ret < 0) {
+                               DbgPrint("Returns: %d\n", ret);
+                       }
+               }
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrint("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+
+    return remove_all_watch(appid);
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_UPGRADE(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    db_fini();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char *appid)
+{
+    xmlNodePtr node;
+    int ret;
+
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               ErrPrint("Failed to init DB\n");
+               return -EIO;
+    }
+
+    node = xmlDocGetRootElement(docPtr);
+    if (!node) {
+               ErrPrint("Invalid document\n");
+               return -EINVAL;
+    }
+
+    for (node = node->children; node; node = node->next) {
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)WATCH_APP_TAG)) {
+                       ret = db_install_watchapp(node, appid);
+                       if (ret < 0) {
+                               DbgPrint("Returns: %d\n", ret);
+                       }
+               }
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrint("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_UNINSTALL(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+       return -EIO;
+    }
+
+       remove_all_watch(appid);
+
+    db_fini();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    if (!db_check()) {
+               return -EIO;
+    }
+
+    /* Doesn't need to do anything from here, we already dealt it with this */
+
+    return 0;
+}
+
+/* End of a file */
diff --git a/pkgmgr_widget/watch/watchapp.xml b/pkgmgr_widget/watch/watchapp.xml
new file mode 100644 (file)
index 0000000..b4e96c6
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<manifest xmlns="http://tizen.org/ns/packages" package="org.tizen.nicesj" version="1.0" install-location="auto">
+
+<watchapp appid="org.tizen.nicesj.watchapp" >
+       <label>My widget</label>
+       <icon>/usr/share/icons/small/org.tizen.live-watch.png</icon>
+       <label xml:lang="en_US">Sample</label>
+       <label xml:lang="ko_KR">Hangul</label>
+</watchapp>
+</manifest>
diff --git a/pkgmgr_widget/watch/watchapp.xsd b/pkgmgr_widget/watch/watchapp.xsd
new file mode 100644 (file)
index 0000000..23baef2
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://tizen.org/ns/packages" xmlns:packages="http://tizen.org/ns/packages">
+
+    <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
+
+    <xs:element name="watchapp">
+       <xs:complexType>
+           <xs:sequence>
+               <xs:element name="label">
+                   <xs:complexType mixed="true">
+                       <xs:attribute ref="xml:lang"/>
+                   </xs:complexType>
+               </xs:element>
+               <xs:element name="icon">
+                   <xs:complexType mixed="true">
+                       <xs:attribute ref="xml:lang"/>
+                   </xs:complexType>
+               </xs:element>           
+           </xs:sequence>
+           <xs:attribute name="appid" use="required" type="xs:string"/>
+       </xs:complexType>
+    </xs:element>
+
+</xs:schema>
diff --git a/pkgmgr_widget/widget.xml b/pkgmgr_widget/widget.xml
new file mode 100644 (file)
index 0000000..b2fe578
--- /dev/null
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<manifest xmlns="http://tizen.org/ns/packages" package="org.tizen.nicesj" version="1.0" install-location="auto">
+
+<widget appid="org.tizen.nicesj.widget" primary="true" secured="false" network="false" abi="c" timeout="20" period="10.0" libexec="/usr/apps/org.tizen.myapp/libexec/liblive-org.tizen.nicesj.so" pinup="false" script="edje" nodisplay="false" direct_input="true">
+       <label>My widget</label>
+       <icon>/usr/share/icons/small/org.tizen.live-nicesj.png</icon>
+       <label xml:lang="en_US">Sample</label>
+       <label xml:lang="ko_KR">Hangul</label>
+       <content>Content information</content>  <!-- This string will be passed to the widget_create -->
+       <category name="http://tizen.org/widget/default"></category>    <!-- Used for categorizing a package -->
+
+       <launch>org.tizen.nicesj</launch> <!-- Launch this app from homescreen directly -->
+
+       <setup>org.tizen.nicesj</setup> <!-- If the box needs to be setting up before adding it to the homscreen, this app will be launched -->
+
+       <box type="image" mouse_event="false" count="0">
+               <!-- 1x1 should be exists, mandatory size -->
+               <size touch_effect="true" preview="ABSPATH" mouse_event="false">1x1</size>
+
+               <size touch_effect="true" preview="ABSPATH" mouse_event="false">2x1</size>
+               <size touch_effect="true" need_frame="true" mouse_event="false">2x2</size>
+               <size>4x1</size>
+               <size need_frame="true">4x2</size>
+               <size touch_effect="false" need_frame="true" mouse_event="false">4x3</size>
+               <size touch_effect="false" mouse_event="false">4x4</size>
+
+               <!-- Only for the Easy home -->
+               <size>21x21</size>
+               <size preview="ABSPATH">23x21</size>
+               <size>23x23</size>
+
+               <!-- Only supported for special widget - inhouse / commercialization target only -->
+               <size>0x0</size>
+               <script src="/opt/usr/apps/org.tizen.myapp/res/script/my.edj" group="my_group" />
+       </box>
+
+       <glancebar type="script">
+               <size>720x100</size>
+               <script src="/opt/usr/apps/org.tizen.myapp/res/script/mypd.edj" group="group_string" />
+       </glancebar>
+
+       <group>
+               <cluster name="cluster1">
+                       <category name="category1" context="APP_USED_FREQUENTLY">
+                               <option key="time_span" value="value" />
+                               <option key="result_size" value="value" />
+                       </category>
+                       <category name="category2" context="APP_USED_FREQUENTLY_THISTIME">
+                               <option key="time_span" value="value" />
+                               <option key="result_size" value="value" />
+                               <option key="time_interval" value="value" />
+                       </category>
+               </cluster>
+               <cluster name="cluster2">
+                       <category name="category1" context="CONTACT_COMMUNICATED_FREQUENTLY">
+                               <option key="data_provider" value="org.tizen.contact" />
+                               <option key="time_span" value="value" />
+                               <option key="result_size" value="size" />
+                       </category>
+                       <category name="category2" context="CONTACT_COMMUNICATED_FREQUENTLY_THISTIME">
+                               <option key="data_provider" value="org.tizen.contact" />
+                               <option key="time_span" value="value" />
+                               <option key="result_size" value="value" />
+                               <option key="time_interval" value="value" />
+                       </category>
+                       <category name="category3" context="CONTACT_COMMUNICATED_ATLEASTONCE">
+                               <option key="data_provider" value="value" />
+                               <option key="time_span" value="value" />
+                               <option key="result_size" value="value" />
+                       </category>
+               </cluster>
+               <cluster name="noctx_cluster">
+                       <category name="noctx_category" />
+               </cluster>
+               <!--
+                    KEYWORD_SEARCHED_FREQUENTLY(DATA_PROVIDER, TIME_SPAN, RESULT_SIZE)
+                    MUSIC_PLAYED_FREQUENTLY(DATA_PROVIDER, TIME_SPAN, RESULT_SIZE)
+               -->
+       </group>
+</widget>
+</manifest>
diff --git a/pkgmgr_widget/widget.xsd b/pkgmgr_widget/widget.xsd
new file mode 100644 (file)
index 0000000..1532aa8
--- /dev/null
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://tizen.org/ns/packages" xmlns:packages="http://tizen.org/ns/packages">
+
+    <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
+
+    <xs:element name="widget">
+       <xs:complexType>
+           <xs:sequence>
+               <xs:element name="label">
+                   <xs:complexType mixed="true">
+                       <xs:attribute ref="xml:lang"/>
+                   </xs:complexType>
+               </xs:element>
+               <xs:element name="icon">
+                   <xs:complexType mixed="true">
+                       <xs:attribute ref="xml:lang"/>
+                   </xs:complexType>
+               </xs:element>
+               <xs:element name="content"/>
+               <xs:element name="launch"/>
+               <xs:element name="setup"/>
+               <xs:element name="category">
+                   <xs:complexType>
+                     <xs:sequence>
+                       <xs:choice maxOccurs="unbounded">
+                         <xs:any minOccurs="0" processContents="lax"/>
+                       </xs:choice>
+                     </xs:sequence>
+                     <xs:anyAttribute processContents="lax"/>
+                   </xs:complexType>
+               </xs:element>
+               <xs:element nmae="glancebar">
+                   <xs:complexType>
+                       <xs:sequence>
+                           <xs:choice maxOccurs="unbounded" minOccurs="0">
+                               <xs:element name="size"/>
+                               <xs:element name="script">
+                                   <xs:complexType>
+                                       <xs:attribute name="src" type="xs:string"/>
+                                       <xs:attribute name="group" type="xs:string"/>
+                                   </xs:complexType>
+                               </xs:element>
+                           </xs:choice>
+                       </xs:sequence>
+                   </xs:complexType>
+               </xs:element>
+               <xs:element name="box">
+                   <xs:complexType>
+                       <xs:sequence>
+                           <xs:choice maxOccurs="unbounded" minOccurs="0">
+                               <xs:element name="size">
+                                   <xs:complexType>
+                                       <xs:attribute name="touch_effect" type="xs:boolean"/>
+                                       <xs:attribute name="mouse_event" type="xs:boolean"/>
+                                       <xs:attribute name="need_frame" type="xs:boolean"/>
+                                       <xs:attribute name="preview" type="xs:string"/>
+                                   </xs:complexType>
+                               </xs:element>
+
+                               <xs:element name="script">
+                                   <xs:complexType>
+                                       <xs:attribute name="src" type="xs:string"/>
+                                       <xs:attribute name="group" type="xs:string"/>
+                                   </xs:complexType>
+                               </xs:element>
+                           </xs:choice>
+                       </xs:sequence>
+                       <xs:attribute name="type" type="xs:string"/>
+                       <xs:attribute name="mouse_event" type="xs:boolean"/>
+                       <xs:attribute name="count" type="xs:decimal"/>
+                   </xs:complexType>
+               </xs:element>
+           </xs:sequence>
+           <xs:attribute name="appid" use="required" type="xs:string"/>
+           <xs:attribute name="primary" use="required" type="xs:boolean"/>
+           <xs:attribute name="secured" type="xs:boolean"/>
+           <xs:attribute name="network" use="required" type="xs:boolean"/>
+           <xs:attribute name="abi" type="xs:string"/>
+           <xs:attribute name="timeout" use="required" type="xs:string"/>
+           <xs:attribute name="period" use="required" type="xs:string"/>
+           <xs:attribute name="libexec" type="xs:string"/>
+           <xs:attribute name="pinup" type="xs:string"/>
+           <xs:attribute name="script" type="xs:string"/>
+           <xs:attribute name="nodisplay" type="xs:boolean"/>
+       </xs:complexType>
+    </xs:element>
+
+</xs:schema>
diff --git a/pkgmgr_widget/widget/CMakeLists.txt b/pkgmgr_widget/widget/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d10a806
--- /dev/null
@@ -0,0 +1,15 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(widget C)
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+ADD_LIBRARY(${PROJECT_NAME} MODULE
+       src/service_widget.c
+)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${bin_pkgs_LDFLAGS} common)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION "etc/package-manager/parserlib")
+
+# End of a file
diff --git a/pkgmgr_widget/widget/src/service_widget.c b/pkgmgr_widget/widget/src/service_widget.c
new file mode 100644 (file)
index 0000000..48be3ef
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+#include "common.h"
+
+#define WIDGET_TAG "widget"
+#define EAPI __attribute__((visibility("default")))
+
+static inline int remove_all_widgets(const char *appid)
+{
+       int cnt;
+
+    ErrPrintWithConsole("%s\n", appid);
+
+    begin_transaction();
+    cnt = pkglist_get_via_callback(appid, 0, delete_record_cb, NULL);
+    commit_transaction();
+
+    if (cnt > 0) {
+               DbgPrint("Package[%s] is not deleted: %d\n", appid, cnt);
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_INSTALL(const char *appid)
+{
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrintWithConsole("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+
+       return remove_all_widgets(appid);
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_INSTALL(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    db_fini();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char *appid)
+{
+    xmlNodePtr node;
+    int ret;
+
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               ErrPrintWithConsole("Failed to init DB\n");
+               return -EIO;
+    }
+
+    node = xmlDocGetRootElement(docPtr);
+    if (!node) {
+               ErrPrintWithConsole("Invalid document\n");
+               return -EINVAL;
+    }
+
+    for (node = node->children; node; node = node->next) {
+               DbgPrint("node->name: %s\n", node->name);
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)WIDGET_TAG)) {
+                       ret = db_install_widget(node, appid);
+                       if (ret < 0) {
+                               DbgPrint("Returns: %d\n", ret);
+                       }
+               }
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char *appid)
+{
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrintWithConsole("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+
+       return remove_all_widgets(appid);
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_UPGRADE(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    db_fini();
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char *appid)
+{
+    xmlNodePtr node;
+    int ret;
+
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               ErrPrint("Failed to init DB\n");
+               return -EIO;
+    }
+
+    node = xmlDocGetRootElement(docPtr);
+    if (!node) {
+               ErrPrint("Invalid document\n");
+               return -EINVAL;
+    }
+
+    for (node = node->children; node; node = node->next) {
+               if (!xmlStrcasecmp(node->name, (const xmlChar *)WIDGET_TAG)) {
+                       ret = db_install_widget(node, appid);
+                       if (ret < 0) {
+                               DbgPrint("Returns: %d\n", ret);
+                       }
+               }
+    }
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL(const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+
+    if (!db_check()) {
+               if (db_init() < 0) {
+                       ErrPrint("Failed to init DB\n");
+                       return -EIO;
+               }
+    }
+
+    db_upgrade_db_schema();
+
+    return 0;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_POST_UNINSTALL(const char *appid)
+{
+       int ret;
+
+       ret = remove_all_widgets(appid);
+
+    db_fini();
+    return ret;
+}
+
+EAPI int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char *appid)
+{
+    ErrPrintWithConsole("[%s]\n", appid);
+    if (!db_check()) {
+               return -EIO;
+    }
+
+    /* Doesn't need to do anything from here, we already dealt it with this */
+
+    return 0;
+}
+
+/* End of a file */
diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt
new file mode 100644 (file)
index 0000000..2ab7c7b
--- /dev/null
@@ -0,0 +1 @@
+ADD_SUBDIRECTORY(edje)
diff --git a/res/edje/CMakeLists.txt b/res/edje/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ff341c3
--- /dev/null
@@ -0,0 +1 @@
+# End of a file
diff --git a/res/edje/master.edc b/res/edje/master.edc
new file mode 100644 (file)
index 0000000..38fcf7e
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+collections {
+       group {
+               name: "error";
+               parts {
+                       part {
+                               name: "image";
+                               type: SWALLOW;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.0 0.0; }
+                                       rel2 { relative: 1.0 1.0; }
+                               }
+                       }
+
+                       part {
+                               name: "bg";
+                               type: RECT;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.0 0.0; }
+                                       rel2 { relative: 1.0 1.0; }
+                                       color: 50 50 50 50;
+                               }
+                       }
+
+                       part {
+                               name: "pkgname";
+                               type: TEXT;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.25 0.2; to, "bg"; }
+                                       rel2 { relative: 0.75 0.35; to, "bg"; }
+                                       text {
+                                               font: "SLP:style=medium";
+                                               size: 20;
+                                               fit: 1 1;
+                                               text: "pkgname";
+                                               align: 0.5 0.5;
+                                       }
+                               }
+                       }
+                       part {
+                               name: "function";
+                               type: TEXT;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.25 0.35; to, "bg"; }
+                                       rel2 { relative: 0.75 0.5; to, "bg"; }
+                                       text {
+                                               font: "SLP:style=medium";
+                                               size: 20;
+                                               fit: 1 1;
+                                               text: "function";
+                                               align: 0.5 0.5;
+                                       }
+                               }
+                       }
+                       part {
+                               name: "slave_pid";
+                               type: TEXT;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.25 0.5; to, "bg"; }
+                                       rel2 { relative: 0.75 0.65; to, "bg"; }
+                                       text {
+                                               font: "SLP:style=medium";
+                                               size: 20;
+                                               fit: 1 1;
+                                               text: "slave_pid";
+                                               align: 0.5 0.5;
+                                       }
+                               }
+                       }
+                       part {
+                               name: "filename";
+                               type: TEXT;
+                               description {
+                                       state: "default" 0.0;
+                                       rel1 { relative: 0.0 0.65; to, "bg"; }
+                                       rel2 { relative: 1.0 0.8; to, "bg"; }
+                                       text {
+                                               font: "SLP:style=medium";
+                                               size: 20;
+                                               fit: 1 1;
+                                               text: "filename";
+                                               align: 0.5 0.5;
+                                       }
+                               }
+                       }
+               } // parts
+       }
+} // collections
+
+
+
+// End of a file
diff --git a/src/badge_service.c b/src/badge_service.c
new file mode 100644 (file)
index 0000000..e287c1d
--- /dev/null
@@ -0,0 +1,575 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include "lite-errno.h"
+#endif
+#include <packet.h>
+
+#include <sys/smack.h>
+
+#include <badge.h>
+#include <badge_db.h>
+#include <badge_setting_service.h>
+#include <security-server.h>
+
+#include "service_common.h"
+#include "debug.h"
+#include "util.h"
+#include "conf.h"
+
+static struct info {
+       Eina_List *context_list;
+       struct service_context *svc_ctx;
+} s_info = {
+       .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
+       .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
+};
+
+#define ENABLE_BS_ACCESS_CONTROL 1
+
+struct context {
+       struct tcb *tcb;
+       double seq;
+};
+
+struct badge_service {
+       const char *cmd;
+       void (*handler)(struct tcb *tcb, struct packet *packet, void *data);
+       const char *rule;
+       const char *access;
+};
+
+/*!
+ * FUNCTIONS to check smack permission
+ */
+static int _is_valid_permission(int fd, struct badge_service *service)
+{
+       int ret;
+
+       if (service->rule != NULL && service->access != NULL) {
+               ret = security_server_check_privilege_by_sockfd(fd, service->rule, service->access);
+               if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+                       ErrPrint("SMACK:Access denied\n");
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
+static int _is_manager_permission(int fd)
+{
+       int ret;
+
+       ret = security_server_check_privilege_by_sockfd(fd,
+                       "data-provider-master::badge.manager", "w");
+       if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+               ErrPrint("SMACK:not a manager\n");
+               return 0;
+       }
+
+       return 1;
+}
+
+/*!
+ * FUNCTIONS to handle badge
+ */
+static inline char *get_string(char *string)
+{
+       if (string == NULL) {
+               return NULL;
+       }
+       if (string[0] == '\0') {
+               return NULL;
+       }
+
+       return string;
+}
+
+/*!
+ * SERVICE HANDLER
+ */
+static void _handler_insert_badge(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       char *writable_pkg = NULL;
+       char *caller = NULL;
+
+       if (packet_get(packet, "sss", &pkgname, &writable_pkg, &caller) == 3) {
+               pkgname = get_string(pkgname);
+               writable_pkg = get_string(writable_pkg);
+               caller = get_string(caller);
+
+               if (pkgname != NULL && writable_pkg != NULL && caller != NULL) {
+                       ret = badge_db_insert(pkgname, writable_pkg, caller);
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "i", ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("Failed to send a reply packet:%d", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("Failed to create a reply packet");
+               }
+
+               if (ret == BADGE_ERROR_NONE) {
+                       packet_service = packet_create("insert_badge", "is", ret, pkgname);
+                       if (packet_service != NULL) {
+                               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                       ErrPrint("Failed to send a muticast packet:%d", ret_p);
+                               }
+                               packet_destroy(packet_service);
+                       } else {
+                               ErrPrint("Failed to create a multicast packet");
+                       }
+               } else {
+                       ErrPrint("Failed to insert a badge:%d", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_delete_badge(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       char *caller = NULL;
+
+       if (packet_get(packet, "ss", &pkgname, &caller) == 2) {
+               pkgname = get_string(pkgname);
+               caller = get_string(caller);
+
+               if (pkgname != NULL && caller != NULL) {
+                       if (_is_manager_permission(tcb_fd(tcb)) == 1) {
+                               ret = badge_db_delete(pkgname, pkgname);
+                       } else {
+                               ret = badge_db_delete(pkgname, caller);
+                       }
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "i", ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("Failed to send a reply packet:%d", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("Failed to create a reply packet");
+               }
+
+               if (ret == BADGE_ERROR_NONE) {
+                       packet_service = packet_create("delete_badge", "is", ret, pkgname);
+                       if (packet_service != NULL) {
+                               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                       ErrPrint("Failed to send a muticast packet:%d", ret_p);
+                               }
+                               packet_destroy(packet_service);
+                       } else {
+                               ErrPrint("Failed to create a multicast packet");
+                       }
+               } else {
+                       ErrPrint("Failed to delete a badge:%d", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_set_badge_count(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       char *caller = NULL;
+       int count = 0;
+
+       if (packet_get(packet, "ssi", &pkgname, &caller, &count) == 3) {
+               pkgname = get_string(pkgname);
+               caller = get_string(caller);
+
+               if (pkgname != NULL && caller != NULL) {
+                       ret = badge_db_set_count(pkgname, caller, count);
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "i", ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("Failed to send a reply packet:%d", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("Failed to create a reply packet");
+               }
+
+               if (ret == BADGE_ERROR_NONE) {
+                       packet_service = packet_create("set_badge_count", "isi", ret, pkgname, count);
+                       if (packet_service != NULL) {
+                               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                       ErrPrint("Failed to send a muticast packet:%d", ret_p);
+                               }
+                               packet_destroy(packet_service);
+                       } else {
+                               ErrPrint("Failed to create a multicast packet");
+                       }
+               } else {
+                       ErrPrint("Failed to set count of badge:%d", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_set_display_option(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       char *caller = NULL;
+       int is_display = 0;
+
+       if (packet_get(packet, "ssi", &pkgname, &caller, &is_display) == 3) {
+               pkgname = get_string(pkgname);
+               caller = get_string(caller);
+
+               if (pkgname != NULL && caller != NULL) {
+                       ret = badge_db_set_display_option(pkgname, caller, is_display);
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "i", ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("Failed to send a reply packet:%d", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("Failed to create a reply packet");
+               }
+
+               if (ret == BADGE_ERROR_NONE) {
+                       packet_service = packet_create("set_disp_option", "isi", ret, pkgname, is_display);
+                       if (packet_service != NULL) {
+                               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                       ErrPrint("Failed to send a muticast packet:%d", ret_p);
+                               }
+                               packet_destroy(packet_service);
+                       } else {
+                               ErrPrint("Failed to create a multicast packet");
+                       }
+               } else {
+                       ErrPrint("Failed to set display option of badge:%d", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_set_setting_property(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       int is_display = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       char *property = NULL;
+       char *value = NULL;
+
+       if (packet_get(packet, "sss", &pkgname, &property, &value) == 3) {
+               pkgname = get_string(pkgname);
+               property = get_string(property);
+               value = get_string(value);
+
+               if (pkgname != NULL && property != NULL && value != NULL) {
+                       ret = badge_setting_db_set(pkgname, property, value);
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "ii", ret, ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (ret == BADGE_ERROR_NONE) {
+                       if (strcmp(property, "OPT_BADGE") == 0) {
+                               if (strcmp(value, "ON") == 0) {
+                                       is_display = 1;
+                               } else {
+                                       is_display = 0;
+                               }
+
+                               packet_service = packet_create("set_disp_option", "isi", ret, pkgname, is_display);
+                               if (packet_service != NULL) {
+                                       if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                               ErrPrint("Failed to send a muticast packet:%d", ret_p);
+                                       }
+                                       packet_destroy(packet_service);
+                               } else {
+                                       ErrPrint("Failed to create a multicast packet");
+                               }
+                       }
+               } else {
+                       ErrPrint("failed to set noti property:%d\n", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_get_setting_property(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       char *pkgname = NULL;
+       char *property = NULL;
+       char *value = NULL;
+
+       if (packet_get(packet, "sss", &pkgname, &property) == 2) {
+               pkgname = get_string(pkgname);
+               property = get_string(property);
+
+               if (pkgname != NULL && property != NULL) {
+                       ret = badge_setting_db_get(pkgname, property, &value);
+               } else {
+                       ret = BADGE_ERROR_INVALID_PARAMETER;
+               }
+
+               packet_reply = packet_create_reply(packet, "is", ret, value);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (value != NULL) {
+                       DbgFree(value);
+               }
+       }
+}
+
+static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0;
+       struct packet *packet_reply;
+
+       ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
+       if (ret < 0) {
+               ErrPrint("Failed to set the type of client:%d", ret);
+       }
+
+       packet_reply = packet_create_reply(packet, "i", ret);
+       if (packet_reply) {
+               if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("Failed to send a reply packet:%d", ret);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("Failed to create a reply packet");
+       }
+}
+
+static void _handler_access_control_error(struct tcb *tcb, struct packet *packet)
+{
+       int ret_p = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "i", BADGE_ERROR_PERMISSION_DENIED);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("Failed to send a reply packet:%d", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("Failed to create a reply packet");
+       }
+}
+
+/*!
+ * SERVICE THREAD
+ */
+static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int i = 0;
+       const char *command;
+       static struct badge_service service_req_table[] = {
+               {
+                       .cmd = "insert_badge",
+                       .handler = _handler_insert_badge,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "w",
+               },
+               {
+                       .cmd = "delete_badge",
+                       .handler = _handler_delete_badge,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "w",
+               },
+               {
+                       .cmd = "set_badge_count",
+                       .handler = _handler_set_badge_count,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "w",
+               },
+               {
+                       .cmd = "set_disp_option",
+                       .handler = _handler_set_display_option,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "w",
+               },
+               {
+                       .cmd = "set_noti_property",
+                       .handler = _handler_set_setting_property,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "w",
+               },
+               {
+                       .cmd = "get_noti_property",
+                       .handler = _handler_get_setting_property,
+                       .rule = "data-provider-master::badge.client",
+                       .access = "r",
+               },
+               {
+                       .cmd = "service_register",
+                       .handler = _handler_service_register,
+                       .rule = NULL,
+                       .access = NULL,
+               },
+               {
+                       .cmd = NULL,
+                       .handler = NULL,
+                       .rule = NULL,
+                       .access = NULL,
+               },
+       };
+
+       if (!packet) {
+               DbgPrint("TCB: %p is terminated (NIL packet)\n", tcb);
+               return 0;
+       }
+
+       command = packet_command(packet);
+       if (!command) {
+               ErrPrint("Invalid command\n");
+               return -EINVAL;
+       }
+       DbgPrint("Command: [%s], Packet type[%d]\n", command, packet_type(packet));
+
+       switch (packet_type(packet)) {
+       case PACKET_REQ:
+               /* Need to send reply packet */
+               for (i = 0; service_req_table[i].cmd; i++) {
+                       if (strcmp(service_req_table[i].cmd, command)) {
+                               continue;
+                       }
+
+#if ENABLE_BS_ACCESS_CONTROL
+                       if (_is_valid_permission(tcb_fd(tcb), &(service_req_table[i])) == 1) {
+                               service_req_table[i].handler(tcb, packet, data);
+                       } else {
+                               _handler_access_control_error(tcb, packet);
+                       }
+#else
+                       _is_valid_permission(tcb_fd(tcb), &(service_req_table[i]));
+                       service_req_table[i].handler(tcb, packet, data);
+#endif
+                       break;
+               }
+
+               break;
+       case PACKET_REQ_NOACK:
+               break;
+       case PACKET_ACK:
+               break;
+       default:
+               ErrPrint("Packet type is not valid[%s]\n", command);
+               return -EINVAL;
+       }
+
+       /*!
+        * return value has no meanning,
+        * it will be printed by dlogutil.
+        */
+       return 0;
+}
+
+
+/*!
+ * MAIN THREAD
+ * Do not try to do anyother operation in these functions
+ */
+HAPI int badge_service_init(void)
+{
+       if (s_info.svc_ctx) {
+               ErrPrint("Already initialized\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       s_info.svc_ctx = service_common_create(BADGE_SOCKET, BADGE_SMACK_LABEL, service_thread_main, NULL);
+       if (!s_info.svc_ctx) {
+               ErrPrint("Unable to activate service thread\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Successfully initiated\n");
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int badge_service_fini(void)
+{
+       if (!s_info.svc_ctx) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       service_common_destroy(s_info.svc_ctx);
+       s_info.svc_ctx = NULL;
+       DbgPrint("Successfully finalized\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/buffer_handler.c b/src/buffer_handler.c
new file mode 100644 (file)
index 0000000..a6d9bb2
--- /dev/null
@@ -0,0 +1,1750 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h> /* access */
+#include <sys/mman.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/shm.h>
+#include <sys/ipc.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <Ecore.h>
+#include <Ecore_X.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <X11/Xproto.h>
+#include <X11/extensions/Xdamage.h>
+#include <X11/extensions/Xfixes.h>
+#include <X11/extensions/XShm.h>
+
+#include <dri2.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+#include <tbm_bufmgr.h>
+
+#include <dlog.h>
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_buffer.h>
+#include <widget_conf.h>
+#include <widget_util.h>
+
+#include "debug.h"
+#include "conf.h"
+#include "util.h"
+#include "instance.h"
+#include "package.h"
+#include "client_life.h"
+#include "client_rpc.h"
+#include "buffer_handler.h"
+#include "script_handler.h" // Reverse dependency. must has to be broken
+
+/*!
+ * \brief Allocate this in the buffer->data.
+ */
+struct gem_data {
+       DRI2Buffer *dri2_buffer;
+       unsigned int attachments[1];
+       tbm_bo pixmap_bo;
+       int count;
+       int buf_count;
+       int w;
+       int h;
+       int depth;
+       Pixmap pixmap;
+       void *data; /* Gem layer */
+       int refcnt;
+
+       void *compensate_data; /* Check the pitch value, copy this to data */
+};
+
+struct buffer_info
+{
+       void *buffer;
+       char *id;
+       widget_lock_info_t lock_info;
+
+       enum widget_fb_type type;
+
+       int w;
+       int h;
+       int pixel_size;
+       int is_loaded;
+
+       struct inst_info *inst;
+       void *data;
+};
+
+static struct {
+       tbm_bufmgr slp_bufmgr;
+       int evt_base;
+       int err_base;
+       int fd;
+       Eina_List *pixmap_list;
+} s_info = {
+       .slp_bufmgr = NULL,
+       .evt_base = 0,
+       .err_base = 0,
+       .fd = -1,
+       .pixmap_list = NULL,
+};
+
+static inline widget_fb_t create_pixmap(struct buffer_info *info)
+{
+       struct gem_data *gem;
+       widget_fb_t buffer;
+       Display *disp;
+       Window parent;
+       XGCValues gcv;
+       GC gc;
+
+       disp = ecore_x_display_get();
+       if (!disp) {
+               return NULL;
+       }
+
+       parent = DefaultRootWindow(disp);
+
+       buffer = calloc(1, sizeof(*buffer) + sizeof(*gem));
+       if (!buffer) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       gem = (struct gem_data *)buffer->data;
+
+       buffer->type = WIDGET_FB_TYPE_PIXMAP;
+       buffer->refcnt = 1;
+       buffer->state = WIDGET_FB_STATE_CREATED;
+
+       gem->attachments[0] = DRI2BufferFrontLeft;
+       gem->count = 1;
+       gem->w = info->w; /*!< This can be changed by DRI2GetBuffers */
+       gem->h = info->h; /*!< This can be changed by DRI2GetBuffers */
+       gem->depth = info->pixel_size;
+       /*!
+        * \NOTE
+        * Use the 24 Bits
+        * 32 Bits is not supported for video playing.
+        * But for the transparent background, use the 32 bits, and give up video.
+        */
+       gem->pixmap = XCreatePixmap(disp, parent, info->w, info->h, (info->pixel_size << 3));
+       if (gem->pixmap == (Pixmap)0) {
+               ErrPrint("Failed to create a pixmap\n");
+               DbgFree(buffer);
+               return NULL;
+       }
+
+       /*!
+        * \note
+        * Clear pixmap
+        */
+       memset(&gcv, 0, sizeof(gcv));
+       gc = XCreateGC(disp, gem->pixmap, GCForeground, &gcv);
+       if (gc) {
+               XFillRectangle(disp, gem->pixmap, gc, 0, 0, info->w, info->h);
+               XSync(disp, False);
+               XFreeGC(disp, gc);
+       } else {
+               XSync(disp, False);
+               ErrPrint("Unable to clear the pixmap\n");
+       }
+
+       return buffer;
+}
+
+static inline int create_gem(widget_fb_t buffer)
+{
+       struct gem_data *gem;
+       Display *disp;
+
+       disp = ecore_x_display_get();
+       if (!disp) {
+               ErrPrint("Failed to get display\n");
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       gem = (struct gem_data *)buffer->data;
+
+       if (s_info.fd < 0) {
+               gem->data = calloc(1, gem->w * gem->h * gem->depth);
+               if (!gem->data) {
+                       ErrPrint("calloc: %d\n", errno);
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               ErrPrint("DRI2(gem) is not supported - Fallback to the S/W Backend\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       DRI2CreateDrawable(disp, gem->pixmap);
+
+       gem->dri2_buffer = DRI2GetBuffers(disp, gem->pixmap,
+                       &gem->w, &gem->h, gem->attachments, gem->count, &gem->buf_count);
+       if (!gem->dri2_buffer || !gem->dri2_buffer->name) {
+               ErrPrint("Failed to get a gem buffer\n");
+               DRI2DestroyDrawable(disp, gem->pixmap);
+               return WIDGET_ERROR_FAULT;
+       }
+       /*!
+        * \How can I destroy this?
+        */
+       gem->pixmap_bo = tbm_bo_import(s_info.slp_bufmgr, gem->dri2_buffer->name);
+       if (!gem->pixmap_bo) {
+               ErrPrint("Failed to import BO\n");
+               DRI2DestroyDrawable(disp, gem->pixmap);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (WIDGET_CONF_AUTO_ALIGN && gem->dri2_buffer->pitch != gem->w * gem->depth) {
+               gem->compensate_data = calloc(1, gem->w * gem->h * gem->depth);
+               if (!gem->compensate_data) {
+                       ErrPrint("Failed to allocate heap\n");
+               }
+       }
+
+       DbgPrint("dri2_buffer: %p, name: %p, %dx%d, pitch: %d, buf_count: %d, depth: %d, compensate: %p (%d)\n",
+                       gem->dri2_buffer, gem->dri2_buffer->name, gem->w, gem->h,
+                       gem->dri2_buffer->pitch, gem->buf_count, gem->depth, gem->compensate_data, WIDGET_CONF_AUTO_ALIGN);
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline void *acquire_gem(widget_fb_t buffer)
+{
+       struct gem_data *gem;
+
+       if (!buffer) {
+               return NULL;
+       }
+
+       gem = (struct gem_data *)buffer->data;
+       if (s_info.fd < 0) {
+               ErrPrint("GEM is not supported - Use the fake gem buffer\n");
+       } else {
+               if (!gem->pixmap_bo) {
+                       ErrPrint("GEM is not created\n");
+                       return NULL;
+               }
+
+               if (!gem->data) {
+                       tbm_bo_handle handle;
+
+                       if (gem->refcnt) {
+                               ErrPrint("Already acquired. but the buffer is not valid\n");
+                               return NULL;
+                       }
+
+                       handle = tbm_bo_map(gem->pixmap_bo, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
+                       gem->data = handle.ptr;
+               }
+       }
+
+       gem->refcnt++;
+
+       /*!
+        * \note
+        * If there is a compensate canvas buffer,
+        * use it
+        */
+       return gem->compensate_data ? gem->compensate_data : gem->data;
+}
+
+static inline void release_gem(widget_fb_t buffer)
+{
+       struct gem_data *gem;
+
+       gem = (struct gem_data *)buffer->data;
+       if (s_info.fd >= 0 && !gem->pixmap_bo) {
+               ErrPrint("GEM is not created\n");
+               return;
+       }
+
+       if (!gem->data) {
+               if (gem->refcnt > 0) {
+                       ErrPrint("Reference count is not valid %d\n", gem->refcnt);
+                       gem->refcnt = 0;
+               }
+               return;
+       }
+
+       gem->refcnt--;
+       if (gem->refcnt == 0) {
+               if (s_info.fd < 0) {
+                       DbgPrint("S/W Gem buffer has no reference\n");
+               } else {
+                       /*!
+                        * \note
+                        * Update the gem buffer using compensate data buffer if it is exists
+                        */
+                       if (gem->compensate_data) {
+                               register int x;
+                               register int y;
+                               int *gem_pixel;
+                               int *pixel;
+                               int gap;
+
+                               pixel = gem->compensate_data;
+                               gem_pixel = gem->data;
+                               gap = gem->dri2_buffer->pitch - (gem->w * gem->depth);
+
+                               for (y = 0; y < gem->h; y++) {
+                                       for (x = 0; x < gem->w; x++) {
+                                               *gem_pixel++ = *pixel++;
+                                       }
+
+                                       gem_pixel = (int *)(((char *)gem_pixel) + gap);
+                               }
+                       }
+
+                       if (gem->pixmap_bo) {
+                               tbm_bo_unmap(gem->pixmap_bo);
+                       }
+
+                       gem->data = NULL;
+               }
+       } else if (gem->refcnt < 0) {
+               ErrPrint("Invalid refcnt: %d (reset)\n", gem->refcnt);
+               gem->refcnt = 0;
+       }
+}
+
+static inline int destroy_pixmap(widget_fb_t buffer)
+{
+       struct gem_data *gem;
+
+       gem = (struct gem_data *)buffer->data;
+
+       if (gem->pixmap) {
+               Display *disp;
+
+               disp = ecore_x_display_get();
+               if (!disp) {
+                       return WIDGET_ERROR_IO_ERROR;
+               }
+
+               DbgPrint("pixmap %lu\n", gem->pixmap);
+               XFreePixmap(disp, gem->pixmap);
+       }
+
+       buffer->state = WIDGET_FB_STATE_DESTROYED;
+       DbgFree(buffer);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int destroy_gem(widget_fb_t buffer)
+{
+       struct gem_data *gem;
+
+       if (!buffer) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       /*!
+        * Forcely release the acquire_buffer.
+        */
+       gem = (struct gem_data *)buffer->data;
+       if (!gem) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (s_info.fd >= 0) {
+               if (gem->compensate_data) {
+                       DbgPrint("Release compensate buffer %p\n", gem->compensate_data);
+                       DbgFree(gem->compensate_data);
+                       gem->compensate_data = NULL;
+               }
+
+               if (gem->pixmap_bo) {
+                       DbgPrint("unref pixmap bo\n");
+                       tbm_bo_unref(gem->pixmap_bo);
+                       gem->pixmap_bo = NULL;
+
+                       DRI2DestroyDrawable(ecore_x_display_get(), gem->pixmap);
+               }
+       } else if (gem->data) {
+               DbgPrint("Release fake gem buffer\n");
+               DbgFree(gem->data);
+               gem->data = NULL;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int load_file_buffer(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       double timestamp;
+       int size;
+       char *new_id;
+       int len;
+
+       len = strlen(WIDGET_CONF_IMAGE_PATH) + 40;
+       new_id = malloc(len);
+       if (!new_id) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       timestamp = util_timestamp();
+       snprintf(new_id, len, SCHEMA_FILE "%s%lf", WIDGET_CONF_IMAGE_PATH, timestamp);
+
+       size = sizeof(*buffer) + info->w * info->h * info->pixel_size;
+       if (!size) {
+               ErrPrint("Canvas buffer size is ZERO\n");
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       buffer = calloc(1, size);
+       if (!buffer) {
+               ErrPrint("Failed to allocate buffer\n");
+               DbgFree(new_id);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       buffer->type = WIDGET_FB_TYPE_FILE;
+       buffer->refcnt = 0;
+       buffer->state = WIDGET_FB_STATE_CREATED;
+       buffer->info = info;
+
+       DbgFree(info->id);
+       info->id = new_id;
+       info->buffer = buffer;
+       info->is_loaded = 1;
+
+       DbgPrint("FILE type %d created\n", size);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int load_shm_buffer(struct buffer_info *info)
+{
+       int id;
+       int size;
+       widget_fb_t buffer; /* Just for getting a size */
+       char *new_id;
+       int len;
+
+       size = info->w * info->h * info->pixel_size;
+       if (!size) {
+               ErrPrint("Invalid buffer size\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       id = shmget(IPC_PRIVATE, size + sizeof(*buffer), IPC_CREAT | 0666);
+       if (id < 0) {
+               ErrPrint("shmget: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       buffer = shmat(id, NULL, 0);
+       if (buffer == (void *)-1) {
+               ErrPrint("%s shmat: %d\n", info->id, errno);
+
+               if (shmctl(id, IPC_RMID, 0) < 0) {
+                       ErrPrint("%s shmctl: %d\n", info->id, errno);
+               }
+
+               return WIDGET_ERROR_FAULT;
+       }
+
+       buffer->type = WIDGET_FB_TYPE_SHM;
+       buffer->refcnt = id;
+       buffer->state = WIDGET_FB_STATE_CREATED; /*!< Needless */
+       buffer->info = (void *)size; /*!< Use this field to indicates the size of SHM */
+
+       len = strlen(SCHEMA_SHM) + 30; /* strlen("shm://") + 30 */
+
+       new_id = malloc(len);
+       if (!new_id) {
+               ErrPrint("malloc: %d\n", errno);
+               if (shmdt(buffer) < 0) {
+                       ErrPrint("shmdt: %d\n", errno);
+               }
+
+               if (shmctl(id, IPC_RMID, 0) < 0) {
+                       ErrPrint("shmctl: %d\n", errno);
+               }
+
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       snprintf(new_id, len, SCHEMA_SHM "%d", id);
+
+       DbgFree(info->id);
+       info->id = new_id;
+       info->buffer = buffer;
+       info->is_loaded = 1;
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int load_pixmap_buffer(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       struct gem_data *gem;
+       char *new_id;
+       int len;
+
+       /*!
+        * \NOTE
+        * Before call the buffer_handler_pixmap_ref function,
+        * You should make sure that the is_loaded value is toggled (1)
+        * Or the buffer_handler_pixmap_ref function will return NULL
+        */
+       info->is_loaded = 1;
+
+       if (info->buffer) {
+               DbgPrint("Buffer is already exists, but override it with new one\n");
+       }
+
+       buffer = buffer_handler_pixmap_ref(info);
+       if (!buffer) {
+               DbgPrint("Failed to make a reference of a pixmap\n");
+               info->is_loaded = 0;
+               return WIDGET_ERROR_FAULT;
+       }
+
+       len = strlen(SCHEMA_PIXMAP) + 30; /* strlen("pixmap://") + 30 */
+       new_id = malloc(len);
+       if (!new_id) {
+               ErrPrint("malloc: %d\n", errno);
+               info->is_loaded = 0;
+               buffer_handler_pixmap_unref(buffer);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->id);
+       info->id = new_id;
+
+       gem = (struct gem_data *)buffer->data;
+
+       snprintf(info->id, len, SCHEMA_PIXMAP "%d:%d", (int)gem->pixmap, info->pixel_size);
+       DbgPrint("Loaded pixmap(info->id): %s\n", info->id);
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_load(struct buffer_info *info)
+{
+       int ret;
+       widget_target_type_e type = WIDGET_TYPE_GBAR;
+
+       if (!info) {
+               ErrPrint("buffer handler is nil\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->is_loaded) {
+               DbgPrint("Buffer is already loaded\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       switch (info->type) {
+       case WIDGET_FB_TYPE_FILE:
+               ret = load_file_buffer(info);
+
+               if (script_handler_buffer_info(instance_gbar_script(info->inst)) != info && instance_gbar_buffer(info->inst) != info) {
+                       type = WIDGET_TYPE_WIDGET;
+               }
+               info->lock_info = widget_service_create_lock(instance_id(info->inst), type, WIDGET_LOCK_WRITE);
+               break;
+       case WIDGET_FB_TYPE_SHM:
+               ret = load_shm_buffer(info);
+
+               if (script_handler_buffer_info(instance_gbar_script(info->inst)) != info && instance_gbar_buffer(info->inst) != info) {
+                       type = WIDGET_TYPE_WIDGET;
+               }
+               info->lock_info = widget_service_create_lock(instance_id(info->inst), type, WIDGET_LOCK_WRITE);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               ret = load_pixmap_buffer(info);
+               break;
+       default:
+               ErrPrint("Invalid buffer\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return ret;
+}
+
+static inline int unload_file_buffer(struct buffer_info *info)
+{
+       const char *path;
+       char *new_id;
+
+       new_id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
+       if (!new_id) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->buffer);
+       info->buffer = NULL;
+
+       path = widget_util_uri_to_path(info->id);
+       if (path && unlink(path) < 0) {
+               ErrPrint("unlink: %d\n", errno);
+       }
+
+       DbgFree(info->id);
+       info->id = new_id;
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int unload_shm_buffer(struct buffer_info *info)
+{
+       int id;
+       char *new_id;
+
+       new_id = strdup(SCHEMA_SHM "-1");
+       if (!new_id) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (sscanf(info->id, SCHEMA_SHM "%d", &id) != 1) {
+               ErrPrint("%s Invalid ID\n", info->id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (id < 0) {
+               ErrPrint("(%s) Invalid id: %d\n", info->id, id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (shmdt(info->buffer) < 0) {
+               ErrPrint("shmdt: %d\n", errno);
+       }
+
+       if (shmctl(id, IPC_RMID, 0) < 0) {
+               ErrPrint("shmctl: %d\n", errno);
+       }
+
+       info->buffer = NULL;
+
+       DbgFree(info->id);
+       info->id = new_id;
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int unload_pixmap_buffer(struct buffer_info *info)
+{
+       int id;
+       char *new_id;
+       int pixels;
+
+       new_id = strdup(SCHEMA_PIXMAP "0:0");
+       if (!new_id) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (sscanf(info->id, SCHEMA_PIXMAP "%d:%d", &id, &pixels) != 2) {
+               ErrPrint("Invalid ID (%s)\n", info->id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (id == 0) {
+               ErrPrint("(%s) Invalid id: %d\n", info->id, id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       /*!
+        * Decrease the reference counter.
+        */
+       buffer_handler_pixmap_unref(info->buffer);
+
+       /*!
+        * \note
+        * Just clear the info->buffer.
+        * It will be reallocated again.
+        */
+       info->buffer = NULL;
+
+       DbgFree(info->id);
+       info->id = new_id;
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_unload(struct buffer_info *info)
+{
+       int ret;
+
+       if (!info) {
+               ErrPrint("buffer handler is NIL\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!info->is_loaded) {
+               ErrPrint("Buffer is not loaded\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (info->type) {
+       case WIDGET_FB_TYPE_FILE:
+               widget_service_destroy_lock(info->lock_info);
+               info->lock_info = NULL;
+               ret = unload_file_buffer(info);
+               break;
+       case WIDGET_FB_TYPE_SHM:
+               widget_service_destroy_lock(info->lock_info);
+               info->lock_info = NULL;
+               ret = unload_shm_buffer(info);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               ret = unload_pixmap_buffer(info);
+               break;
+       default:
+               ErrPrint("Invalid buffer\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       if (ret == 0) {
+               info->is_loaded = 0;
+       }
+
+       return ret;
+}
+
+EAPI const char *buffer_handler_id(const struct buffer_info *info)
+{
+       return info ? info->id : "";
+}
+
+EAPI enum widget_fb_type buffer_handler_type(const struct buffer_info *info)
+{
+       return info ? info->type : WIDGET_FB_TYPE_ERROR;
+}
+
+EAPI void *buffer_handler_fb(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+
+       if (!info) {
+               return NULL;
+       }
+
+       buffer = info->buffer;
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               void *canvas;
+               int ret;
+
+               /*!
+                * \note
+                * For getting the buffer address of gem.
+                */
+               canvas = buffer_handler_pixmap_acquire_buffer(info);
+               ret = buffer_handler_pixmap_release_buffer(canvas);
+               if (ret < 0) {
+                       ErrPrint("Failed to release buffer: %d\n", ret);
+               }
+               return canvas;
+       }
+
+       return buffer->data;
+}
+
+EAPI int buffer_handler_pixmap(const struct buffer_info *info)
+{
+       widget_fb_t buf;
+       struct gem_data *gem;
+
+       if (!info) {
+               ErrPrint("Inavlid buffer handler\n");
+               return 0;
+       }
+
+       if (info->type != WIDGET_FB_TYPE_PIXMAP) {
+               ErrPrint("Invalid buffer type\n");
+               return 0;
+       }
+
+       buf = (widget_fb_t)info->buffer;
+       if (!buf) {
+               ErrPrint("Invalid buffer data\n");
+               return 0;
+       }
+
+       gem = (struct gem_data *)buf->data;
+       return gem->pixmap;
+}
+
+EAPI void *buffer_handler_pixmap_acquire_buffer(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+
+       if (!info || !info->is_loaded) {
+               ErrPrint("Buffer is not loaded\n");
+               return NULL;
+       }
+
+       buffer = buffer_handler_pixmap_ref(info);
+       if (!buffer) {
+               return NULL;
+       }
+
+       return acquire_gem(buffer);
+}
+
+EAPI void *buffer_handler_pixmap_buffer(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       struct gem_data *gem;
+
+       if (!info) {
+               return NULL;
+       }
+
+       if (!info->is_loaded) {
+               ErrPrint("Buffer is not loaded\n");
+               return NULL;
+       }
+
+       buffer = info->buffer;
+       if (!buffer) {
+               return NULL;
+       }
+
+       gem = (struct gem_data *)buffer->data;
+       return gem->compensate_data ? gem->compensate_data : gem->data;
+}
+
+/**
+ * @return "buffer" object (Not the buffer_info)
+ */
+EAPI void *buffer_handler_pixmap_ref(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+
+       if (!info->is_loaded) {
+               ErrPrint("Buffer is not loaded\n");
+               return NULL;
+       }
+
+       if (info->type != WIDGET_FB_TYPE_PIXMAP) {
+               ErrPrint("Buffer type is not matched\n");
+               return NULL;
+       }
+
+       buffer = info->buffer;
+       if (!buffer) {
+               int need_gem = 1;
+
+               buffer = create_pixmap(info);
+               if (!buffer) {
+                       ErrPrint("Failed to create a pixmap\n");
+                       return NULL;
+               }
+
+               info->buffer = buffer;
+
+               if (info->inst) {
+                       struct pkg_info *pkg;
+
+                       pkg = instance_package(info->inst);
+
+                       if (instance_widget_buffer(info->inst) == info) {
+                               if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+                                       need_gem = 0;
+                               }
+                       } else if (instance_gbar_buffer(info->inst) == info) {
+                               if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+                                       need_gem = 0;
+                               }
+                       } else {
+                               int idx;
+
+                               for (idx = 0; idx < WIDGET_CONF_EXTRA_BUFFER_COUNT; idx++) {
+                                       if (instance_widget_extra_buffer(info->inst, idx) == info) {
+                                               if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+                                                       need_gem = 0;
+                                                       break;
+                                               }
+                                       }
+
+                                       if (instance_gbar_extra_buffer(info->inst, idx) == info) {
+                                               if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+                                                       need_gem = 0;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               if (need_gem) {
+                       if (create_gem(buffer) < 0) {
+                               /* okay, something goes wrong */
+                       }
+               }
+       } else if (buffer->state != WIDGET_FB_STATE_CREATED || buffer->type != WIDGET_FB_TYPE_PIXMAP) {
+               ErrPrint("Invalid buffer\n");
+               return NULL;
+       } else if (buffer->refcnt > 0) {
+               buffer->refcnt++;
+               return buffer;
+       }
+
+       s_info.pixmap_list = eina_list_append(s_info.pixmap_list, buffer);
+       return buffer;
+}
+
+/*!
+ * \return "buffer"
+ */
+EAPI void *buffer_handler_pixmap_find(int pixmap)
+{
+       widget_fb_t buffer;
+       struct gem_data *gem;
+       Eina_List *l;
+       Eina_List *n;
+
+       if (pixmap == 0) {
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH_SAFE(s_info.pixmap_list, l, n, buffer) {
+               if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED || buffer->type != WIDGET_FB_TYPE_PIXMAP) {
+                       s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
+                       DbgPrint("Invalid buffer (List Removed: %p)\n", buffer);
+                       continue;
+               }
+
+               gem = (struct gem_data *)buffer->data;
+               if (gem->pixmap == pixmap) {
+                       return buffer;
+               }
+       }
+
+       return NULL;
+}
+
+EAPI int buffer_handler_pixmap_release_buffer(void *canvas)
+{
+       widget_fb_t buffer;
+       struct gem_data *gem;
+       Eina_List *l;
+       Eina_List *n;
+       void *_ptr;
+
+       if (!canvas) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       EINA_LIST_FOREACH_SAFE(s_info.pixmap_list, l, n, buffer) {
+               if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED || buffer->type != WIDGET_FB_TYPE_PIXMAP) {
+                       s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
+                       continue;
+               }
+
+               gem = (struct gem_data *)buffer->data;
+               _ptr = gem->compensate_data ? gem->compensate_data : gem->data;
+
+               if (!_ptr) {
+                       continue;
+               }
+
+               if (_ptr == canvas) {
+                       release_gem(buffer);
+                       buffer_handler_pixmap_unref(buffer);
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+/*!
+ * \note
+ *
+ * \return Return NULL if the buffer is in still uses.
+ *        Return buffer_ptr if it needs to destroy
+ */
+EAPI int buffer_handler_pixmap_unref(void *buffer_ptr)
+{
+       widget_fb_t buffer = buffer_ptr;
+       struct buffer_info *info;
+
+       buffer->refcnt--;
+       if (buffer->refcnt > 0) {
+               return WIDGET_ERROR_NONE; /* Return NULL means, gem buffer still in use */
+       }
+
+       s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
+
+       info = buffer->info;
+
+       if (destroy_gem(buffer) < 0) {
+               ErrPrint("Failed to destroy gem buffer\n");
+       }
+
+       if (destroy_pixmap(buffer) < 0) {
+               ErrPrint("Failed to destroy pixmap\n");
+       }
+
+       if (info && info->buffer == buffer) {
+               info->buffer = NULL;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_is_loaded(const struct buffer_info *info)
+{
+       return info ? info->is_loaded : 0;
+}
+
+EAPI void buffer_handler_update_size(struct buffer_info *info, int w, int h)
+{
+       if (!info) {
+               return;
+       }
+
+       info->w = w;
+       info->h = h;
+}
+
+EAPI int buffer_handler_resize(struct buffer_info *info, int w, int h)
+{
+       int ret;
+
+       if (!info) {
+               ErrPrint("Invalid handler\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->w == w && info->h == h) {
+               DbgPrint("No changes\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       buffer_handler_update_size(info, w, h);
+
+       if (!info->is_loaded) {
+               DbgPrint("Buffer size is updated[%dx%d]\n", w, h);
+               return WIDGET_ERROR_NONE;
+       }
+
+       ret = buffer_handler_unload(info);
+       if (ret < 0) {
+               ErrPrint("Unload: %d\n", ret);
+       }
+
+       ret = buffer_handler_load(info);
+       if (ret < 0) {
+               ErrPrint("Load: %d\n", ret);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_get_size(struct buffer_info *info, int *w, int *h)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (w) {
+               *w = info->w;
+       }
+       if (h) {
+               *h = info->h;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI struct inst_info *buffer_handler_instance(struct buffer_info *info)
+{
+       return info->inst;
+}
+
+/*!
+ * \note
+ * Only for used S/W Backend
+ */
+static inline int sync_for_pixmap(widget_fb_t buffer)
+{
+       XShmSegmentInfo si;
+       XImage *xim;
+       GC gc;
+       Display *disp;
+       struct gem_data *gem;
+       Screen *screen;
+       Visual *visual;
+
+       if (buffer->state != WIDGET_FB_STATE_CREATED) {
+               ErrPrint("Invalid state of a FB\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (buffer->type != WIDGET_FB_TYPE_PIXMAP) {
+               ErrPrint("Invalid buffer\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       disp = ecore_x_display_get();
+       if (!disp) {
+               ErrPrint("Failed to get a display\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       gem = (struct gem_data *)buffer->data;
+       if (gem->w == 0 || gem->h == 0) {
+               DbgPrint("Nothing can be sync\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       si.shmid = shmget(IPC_PRIVATE, gem->w * gem->h * gem->depth, IPC_CREAT | 0666);
+       if (si.shmid < 0) {
+               ErrPrint("shmget: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       si.readOnly = False;
+       si.shmaddr = shmat(si.shmid, NULL, 0);
+       if (si.shmaddr == (void *)-1) {
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+                       ErrPrint("shmctl: %d\n", errno);
+               }
+               return WIDGET_ERROR_FAULT;
+       }
+
+       screen = DefaultScreenOfDisplay(disp);
+       visual = DefaultVisualOfScreen(screen);
+       /*!
+        * \NOTE
+        * XCreatePixmap can only uses 24 bits depth only.
+        */
+       xim = XShmCreateImage(disp, visual, (gem->depth << 3), ZPixmap, NULL, &si, gem->w, gem->h);
+       if (xim == NULL) {
+               if (shmdt(si.shmaddr) < 0) {
+                       ErrPrint("shmdt: %d\n", errno);
+               }
+
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+                       ErrPrint("shmctl: %d\n", errno);
+               }
+               return WIDGET_ERROR_FAULT;
+       }
+
+       xim->data = si.shmaddr;
+       XShmAttach(disp, &si);
+       XSync(disp, False);
+
+       gc = XCreateGC(disp, gem->pixmap, 0, NULL);
+       if (!gc) {
+               XShmDetach(disp, &si);
+               XDestroyImage(xim);
+
+               if (shmdt(si.shmaddr) < 0) {
+                       ErrPrint("shmdt: %d\n", errno);
+               }
+
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+                       ErrPrint("shmctl: %d\n", errno);
+               }
+
+               return WIDGET_ERROR_FAULT;
+       }
+
+       memcpy(xim->data, gem->data, gem->w * gem->h * gem->depth);
+
+       /*!
+        * \note Do not send the event.
+        *       Instead of X event, master will send the updated event to the viewer
+        */
+       XShmPutImage(disp, gem->pixmap, gc, xim, 0, 0, 0, 0, gem->w, gem->h, False);
+       XSync(disp, False);
+
+       XFreeGC(disp, gc);
+       XShmDetach(disp, &si);
+       XDestroyImage(xim);
+
+       if (shmdt(si.shmaddr) < 0) {
+               ErrPrint("shmdt: %d\n", errno);
+       }
+
+       if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+               ErrPrint("shmctl: %d\n", errno);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI void buffer_handler_flush(struct buffer_info *info)
+{
+       int fd;
+       int size;
+       widget_fb_t buffer;
+
+       if (!info || !info->buffer) {
+               return;
+       }
+
+       buffer = info->buffer;
+
+       if (buffer->type == WIDGET_FB_TYPE_PIXMAP) {
+               if (s_info.fd > 0) {
+                       //return;
+                       //PERF_INIT();
+                       //PERF_BEGIN();
+                       XRectangle rect;
+                       XserverRegion region;
+
+                       rect.x = 0;
+                       rect.y = 0;
+                       rect.width = info->w;
+                       rect.height = info->h;
+
+                       region = XFixesCreateRegion(ecore_x_display_get(), &rect, 1);
+                       XDamageAdd(ecore_x_display_get(), buffer_handler_pixmap(info), region);
+                       XFixesDestroyRegion(ecore_x_display_get(), region);
+                       XFlush(ecore_x_display_get());
+                       //PERF_MARK("XFlush");
+               } else {
+                       if (sync_for_pixmap(buffer) < 0) {
+                               ErrPrint("Failed to sync via S/W Backend\n");
+                       }
+               }
+       } else if (buffer->type == WIDGET_FB_TYPE_FILE) {
+               const char *path;
+
+               path = widget_util_uri_to_path(info->id);
+               if (!path) {
+                       ErrPrint("Invalid id: [%s]\n", info->id);
+                       return;
+               }
+
+               fd = open(path, O_WRONLY | O_CREAT, 0644);
+               if (fd < 0) {
+                       ErrPrint("%s open: %d\n", widget_util_uri_to_path(info->id), errno);
+                       return;
+               }
+
+               size = info->w * info->h * info->pixel_size;
+               widget_service_acquire_lock(info->lock_info);
+               if (write(fd, info->buffer, size) != size) {
+                       ErrPrint("write: %d\n", errno);
+               }
+               widget_service_release_lock(info->lock_info);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+       } else {
+               DbgPrint("Flush nothing\n");
+       }
+}
+
+HAPI int buffer_handler_init(void)
+{
+       int dri2Major, dri2Minor;
+       char *driverName, *deviceName;
+       drm_magic_t magic;
+
+       if (!DRI2QueryExtension(ecore_x_display_get(), &s_info.evt_base, &s_info.err_base)) {
+               ErrPrint("DRI2 is not supported\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (!DRI2QueryVersion(ecore_x_display_get(), &dri2Major, &dri2Minor)) {
+               ErrPrint("DRI2 is not supported\n");
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (!DRI2Connect(ecore_x_display_get(), DefaultRootWindow(ecore_x_display_get()), &driverName, &deviceName)) {
+               ErrPrint("DRI2 is not supported\n");
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (WIDGET_CONF_USE_SW_BACKEND) {
+               DbgPrint("Fallback to the S/W Backend\n");
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               DbgFree(deviceName);
+               DbgFree(driverName);
+               return WIDGET_ERROR_NONE;
+       }
+
+       s_info.fd = open(deviceName, O_RDWR);
+       DbgFree(deviceName);
+       DbgFree(driverName);
+       if (s_info.fd < 0) {
+               ErrPrint("Failed to open a drm device: (%d)\n", errno);
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               return WIDGET_ERROR_NONE;
+       }
+
+       drmGetMagic(s_info.fd, &magic);
+       DbgPrint("DRM Magic: 0x%X\n", magic);
+       if (!DRI2Authenticate(ecore_x_display_get(), DefaultRootWindow(ecore_x_display_get()), (unsigned int)magic)) {
+               ErrPrint("Failed to do authenticate for DRI2\n");
+               if (close(s_info.fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               s_info.fd = -1;
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               return WIDGET_ERROR_NONE;
+       }
+
+       s_info.slp_bufmgr = tbm_bufmgr_init(s_info.fd);
+       if (!s_info.slp_bufmgr) {
+               ErrPrint("Failed to init bufmgr\n");
+               if (close(s_info.fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               s_info.fd = -1;
+               s_info.evt_base = 0;
+               s_info.err_base = 0;
+               return WIDGET_ERROR_NONE;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int buffer_handler_fini(void)
+{
+       if (s_info.fd >= 0) {
+               if (close(s_info.fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               s_info.fd = -1;
+       }
+
+       if (s_info.slp_bufmgr) {
+               tbm_bufmgr_deinit(s_info.slp_bufmgr);
+               s_info.slp_bufmgr = NULL;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline widget_fb_t raw_open_file(const char *filename)
+{
+       widget_fb_t buffer;
+       int fd;
+       off_t off;
+       int ret;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0) {
+               ErrPrint("open: %d\n", errno);
+               return NULL;
+       }
+
+       off = lseek(fd, 0L, SEEK_END);
+       if (off == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       buffer = calloc(1, sizeof(*buffer) + off);
+       if (!buffer) {
+               ErrPrint("Heap: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       buffer->state = WIDGET_FB_STATE_CREATED;
+       buffer->type = WIDGET_FB_TYPE_FILE;
+       buffer->refcnt = 0;
+       buffer->info = (void *)off;
+
+       ret = read(fd, buffer->data, off);
+       if (ret < 0) {
+               ErrPrint("read: %d\n", errno);
+               DbgFree(buffer);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       if (close(fd) < 0) {
+               ErrPrint("close: %d\n", errno);
+       }
+
+       return buffer;
+}
+
+static inline int raw_close_file(widget_fb_t buffer)
+{
+       DbgFree(buffer);
+       return 0;
+}
+
+static inline widget_fb_t raw_open_shm(int shm)
+{
+       widget_fb_t buffer;
+
+       buffer = (widget_fb_t)shmat(shm, NULL, SHM_RDONLY);
+       if (buffer == (widget_fb_t)-1) {
+               ErrPrint("shmat: %d\n", errno);
+               return NULL;
+       }
+
+       return buffer;
+}
+
+static inline int raw_close_shm(widget_fb_t buffer)
+{
+       int ret;
+
+       ret = shmdt(buffer);
+       if (ret < 0) {
+               ErrPrint("shmdt: %d\n", errno);
+       }
+
+       return ret;
+}
+
+static inline widget_fb_t raw_open_pixmap(unsigned int pixmap)
+{
+       widget_fb_t buffer;
+
+       buffer = calloc(1, sizeof(*buffer) + WIDGET_CONF_DEFAULT_PIXELS);
+       if (!buffer) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       buffer->state = WIDGET_FB_STATE_CREATED;
+       buffer->type = WIDGET_FB_TYPE_PIXMAP;
+
+       return buffer;
+}
+
+static inline int raw_close_pixmap(widget_fb_t buffer)
+{
+       DbgFree(buffer);
+       return 0;
+}
+
+EAPI void *buffer_handler_raw_data(widget_fb_t buffer)
+{
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return NULL;
+       }
+
+       return buffer->data;
+}
+
+EAPI int buffer_handler_raw_size(widget_fb_t buffer)
+{
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return (int)buffer->info;
+}
+
+EAPI widget_fb_t buffer_handler_raw_open(enum widget_fb_type widget_fb_type, void *resource)
+{
+       widget_fb_t handle;
+
+       switch (widget_fb_type) {
+       case WIDGET_FB_TYPE_SHM:
+               handle = raw_open_shm((int)resource);
+               break;
+       case WIDGET_FB_TYPE_FILE:
+               handle = raw_open_file(resource);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               handle = raw_open_pixmap((unsigned int)resource);
+               break;
+       default:
+               handle = NULL;
+               break;
+       }
+
+       return handle;
+}
+
+EAPI int buffer_handler_raw_close(widget_fb_t buffer)
+{
+       int ret;
+
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (buffer->type) {
+       case WIDGET_FB_TYPE_SHM:
+               ret = raw_close_shm(buffer);
+               break;
+       case WIDGET_FB_TYPE_FILE:
+               ret = raw_close_file(buffer);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               ret = raw_close_pixmap(buffer);
+               break;
+       default:
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return ret;
+}
+
+EAPI int buffer_handler_lock(struct buffer_info *info)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_FILE) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       return widget_service_acquire_lock(info->lock_info);
+}
+
+EAPI int buffer_handler_unlock(struct buffer_info *info)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_FILE) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       return widget_service_release_lock(info->lock_info);
+}
+
+EAPI int buffer_handler_pixels(struct buffer_info *info)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return info->pixel_size;
+}
+
+EAPI int buffer_handler_auto_align(void)
+{
+       return WIDGET_CONF_AUTO_ALIGN;
+}
+
+EAPI int buffer_handler_stride(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       struct gem_data *gem;
+       int stride;
+
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (info->type) {
+       case WIDGET_FB_TYPE_FILE:
+       case WIDGET_FB_TYPE_SHM:
+               stride = info->w * info->pixel_size;
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               buffer = info->buffer;
+               if (!buffer) {
+                       stride = WIDGET_ERROR_INVALID_PARAMETER;
+                       break;
+               }
+
+               gem = (struct gem_data *)buffer->data;
+               if (!gem) {
+                       stride = WIDGET_ERROR_INVALID_PARAMETER;
+                       break;
+               }
+
+               if (!gem->dri2_buffer) {
+                       /*
+                        * Uhm...
+                        */
+                       ErrPrint("dri2_buffer info is not ready yet!\n");
+                       stride = WIDGET_ERROR_INVALID_PARAMETER;
+                       break;
+               }
+
+               stride = gem->dri2_buffer->pitch;
+               break;
+       default:
+               stride = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return stride;
+}
+
+/*!
+ * \note
+ * Only can be used by master.
+ * Plugin cannot access the user data
+ */
+
+HAPI int buffer_handler_set_data(struct buffer_info *info, void *data)
+{
+       if (!info) {
+               ErrPrint("Invalid handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info->data = data;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *buffer_handler_data(struct buffer_info *info)
+{
+       if (!info) {
+               ErrPrint("Invalid handle\n");
+               return NULL;
+       }
+
+       return info->data;
+}
+
+HAPI int buffer_handler_destroy(struct buffer_info *info)
+{
+       Eina_List *l;
+       widget_fb_t buffer;
+
+       if (!info) {
+               DbgPrint("Buffer is not created yet. info is NIL\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       EINA_LIST_FOREACH(s_info.pixmap_list, l, buffer) {
+               if (buffer->info == info) {
+                       buffer->info = NULL;
+               }
+       }
+
+       buffer_handler_unload(info);
+       DbgFree(info->id);
+       DbgFree(info);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct buffer_info *buffer_handler_create(struct inst_info *inst, enum widget_fb_type type, int w, int h, int pixel_size)
+{
+       struct buffer_info *info;
+
+       info = malloc(sizeof(*info));
+       if (!info) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       switch (type) {
+       case WIDGET_FB_TYPE_SHM:
+               if (pixel_size != WIDGET_CONF_DEFAULT_PIXELS) {
+                       DbgPrint("SHM only supportes %d bytes pixels (requested: %d)\n", WIDGET_CONF_DEFAULT_PIXELS, pixel_size);
+                       pixel_size = WIDGET_CONF_DEFAULT_PIXELS;
+               }
+
+               info->id = strdup(SCHEMA_SHM "-1");
+               if (!info->id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info);
+                       return NULL;
+               }
+               break;
+       case WIDGET_FB_TYPE_FILE:
+               if (pixel_size != WIDGET_CONF_DEFAULT_PIXELS) {
+                       DbgPrint("FILE only supportes %d bytes pixels (requested: %d)\n", WIDGET_CONF_DEFAULT_PIXELS, pixel_size);
+                       pixel_size = WIDGET_CONF_DEFAULT_PIXELS;
+               }
+
+               info->id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
+               if (!info->id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info);
+                       return NULL;
+               }
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               info->id = strdup(SCHEMA_PIXMAP "0:0");
+               if (!info->id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info);
+                       return NULL;
+               }
+               break;
+       default:
+               ErrPrint("Invalid type\n");
+               DbgFree(info);
+               return NULL;
+       }
+
+       info->lock_info = NULL;
+       info->w = w;
+       info->h = h;
+       info->pixel_size = pixel_size;
+       info->type = type;
+       info->is_loaded = 0;
+       info->inst = inst;
+       info->buffer = NULL;
+       info->data = NULL;
+
+       return info;
+}
+
+/* End of a file */
diff --git a/src/buffer_handler_wayland.c b/src/buffer_handler_wayland.c
new file mode 100644 (file)
index 0000000..7c975eb
--- /dev/null
@@ -0,0 +1,865 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h> /* access */
+#include <sys/mman.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/shm.h>
+#include <sys/ipc.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <Ecore.h>
+
+#include <dlog.h>
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_util.h>
+
+#include "debug.h"
+#include "conf.h"
+#include "util.h"
+#include "instance.h"
+#include "package.h"
+#include "client_life.h"
+#include "client_rpc.h"
+#include "buffer_handler.h"
+#include "script_handler.h" // Reverse dependency. must has to be broken
+
+struct buffer {
+       enum {
+               CREATED = 0x00beef00,
+               DESTROYED = 0x00dead00
+       } state;
+       enum buffer_type type;
+       int refcnt;
+       void *info;
+       char data[];
+};
+
+struct buffer_info
+{
+       void *buffer;
+       char *id;
+       widget_lock_info_t lock_info;
+
+       enum buffer_type type;
+
+       int w;
+       int h;
+       int pixel_size;
+       int is_loaded;
+
+       struct inst_info *inst;
+       void *data;
+};
+
+static inline int load_file_buffer(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       double timestamp;
+       int size;
+       char *new_id;
+       int len;
+
+       len = strlen(WIDGET_CONF_IMAGE_PATH) + 40;
+       new_id = malloc(len);
+       if (!new_id) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       timestamp = util_timestamp();
+       snprintf(new_id, len, SCHEMA_FILE "%s%lf", WIDGET_CONF_IMAGE_PATH, timestamp);
+
+       size = sizeof(*buffer) + info->w * info->h * info->pixel_size;
+       if (!size) {
+               ErrPrint("Canvas buffer size is ZERO\n");
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       buffer = calloc(1, size);
+       if (!buffer) {
+               ErrPrint("Failed to allocate buffer\n");
+               DbgFree(new_id);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       buffer->type = WIDGET_FB_TYPE_FILE;
+       buffer->refcnt = 0;
+       buffer->state = WIDGET_FB_STATE_CREATED;
+       buffer->info = info;
+
+       DbgFree(info->id);
+       info->id = new_id;
+       info->buffer = buffer;
+       info->is_loaded = 1;
+
+       DbgPrint("FILE type %d created\n", size);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int load_shm_buffer(struct buffer_info *info)
+{
+       int id;
+       int size;
+       widget_fb_t buffer; /* Just for getting a size */
+       char *new_id;
+       int len;
+
+       size = info->w * info->h * info->pixel_size;
+       if (!size) {
+               ErrPrint("Invalid buffer size\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       id = shmget(IPC_PRIVATE, size + sizeof(*buffer), IPC_CREAT | 0666);
+       if (id < 0) {
+               ErrPrint("shmget: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       buffer = shmat(id, NULL, 0);
+       if (buffer == (void *)-1) {
+               ErrPrint("%s shmat: %d\n", info->id, errno);
+
+               if (shmctl(id, IPC_RMID, 0) < 0) {
+                       ErrPrint("%s shmctl: %d\n", info->id, errno);
+               }
+
+               return WIDGET_ERROR_FAULT;
+       }
+
+       buffer->type = WIDGET_FB_TYPE_SHM;
+       buffer->refcnt = id;
+       buffer->state = WIDGET_FB_STATE_CREATED; /*!< Needless */
+       buffer->info = (void *)size; /*!< Use this field to indicates the size of SHM */
+
+       len = strlen(SCHEMA_SHM) + 30; /* strlen("shm://") + 30 */
+
+       new_id = malloc(len);
+       if (!new_id) {
+               ErrPrint("malloc: %d\n", errno);
+               if (shmdt(buffer) < 0) {
+                       ErrPrint("shmdt: %d\n", errno);
+               }
+
+               if (shmctl(id, IPC_RMID, 0) < 0) {
+                       ErrPrint("shmctl: %d\n", errno);
+               }
+
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       snprintf(new_id, len, SCHEMA_SHM "%d", id);
+
+       DbgFree(info->id);
+       info->id = new_id;
+       info->buffer = buffer;
+       info->is_loaded = 1;
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_load(struct buffer_info *info)
+{
+       int ret;
+       widget_target_type_e type = TARGET_TYPE_GBAR;
+
+       if (!info) {
+               ErrPrint("buffer handler is nil\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->is_loaded) {
+               DbgPrint("Buffer is already loaded\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       switch (info->type) {
+       case BUFFER_TYPE_FILE:
+               ret = load_file_buffer(info);
+               if (script_handler_buffer_info(instance_gbar_script(info->inst)) != info && instance_gbar_buffer(info->inst) != info) {
+                       type = WIDGET_TYPE_WIDGET;
+               }
+               info->lock_info = widget_service_create_lock(instance_id(info->inst), type, WIDGET_LOCK_WRITE);
+               break;
+       case BUFFER_TYPE_SHM:
+               ret = load_shm_buffer(info);
+               if (script_handler_buffer_info(instance_gbar_script(info->inst)) != info && instance_gbar_buffer(info->inst) != info) {
+                       type = WIDGET_TYPE_WIDGET;
+               }
+               info->lock_info = widget_service_create_lock(instance_id(info->inst), type, WIDGET_LOCK_WRITE);
+               break;
+       case BUFFER_TYPE_PIXMAP:
+               /**
+                * TODO: load_wl_pixmap_buffer(info);
+                */
+       default:
+               ErrPrint("Invalid buffer\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return ret;
+}
+
+static inline int unload_file_buffer(struct buffer_info *info)
+{
+       const char *path;
+       char *new_id;
+
+       new_id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
+       if (!new_id) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->buffer);
+       info->buffer = NULL;
+
+       path = widget_util_uri_to_path(info->id);
+       if (path && unlink(path) < 0) {
+               ErrPrint("unlink: %s\n", errno);
+       }
+
+       DbgFree(info->id);
+       info->id = new_id;
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int unload_shm_buffer(struct buffer_info *info)
+{
+       int id;
+       char *new_id;
+
+       new_id = strdup(SCHEMA_SHM "-1");
+       if (!new_id) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (sscanf(info->id, SCHEMA_SHM "%d", &id) != 1) {
+               ErrPrint("%s Invalid ID\n", info->id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (id < 0) {
+               ErrPrint("(%s) Invalid id: %d\n", info->id, id);
+               DbgFree(new_id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (shmdt(info->buffer) < 0) {
+               ErrPrint("shmdt: %d\n", errno);
+       }
+
+       if (shmctl(id, IPC_RMID, 0) < 0) {
+               ErrPrint("shmctl: %d\n", errno);
+       }
+
+       info->buffer = NULL;
+
+       DbgFree(info->id);
+       info->id = new_id;
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_unload(struct buffer_info *info)
+{
+       int ret;
+
+       if (!info) {
+               ErrPrint("buffer handler is NIL\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!info->is_loaded) {
+               ErrPrint("Buffer is not loaded\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (info->type) {
+       case BUFFER_TYPE_FILE:
+               widget_service_destroy_lock(info->lock_info);
+               info->lock_info = NULL;
+               ret = unload_file_buffer(info);
+               break;
+       case BUFFER_TYPE_SHM:
+               widget_service_destroy_lock(info->lock_info);
+               info->lock_info = NULL;
+               ret = unload_shm_buffer(info);
+               break;
+       case BUFFER_TYPE_PIXMAP:
+               /**
+                * @todo: unload_wl_pixmap_buffer(info)
+                */
+       default:
+               ErrPrint("Invalid buffer\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       if (ret == 0) {
+               info->is_loaded = 0;
+       }
+
+       return ret;
+}
+
+EAPI const char *buffer_handler_id(const struct buffer_info *info)
+{
+       return info ? info->id : "";
+}
+
+EAPI enum buffer_type buffer_handler_type(const struct buffer_info *info)
+{
+       return info ? info->type : WIDGET_FB_TYPE_ERROR;
+}
+
+EAPI void *buffer_handler_fb(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+
+       if (!info) {
+               return NULL;
+       }
+
+       buffer = info->buffer;
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               /**
+                */
+               return NULL;
+       }
+
+       return buffer->data;
+}
+
+EAPI int buffer_handler_pixmap(const struct buffer_info *info)
+{
+       return 0;
+}
+
+EAPI void *buffer_handler_pixmap_acquire_buffer(struct buffer_info *info)
+{
+       return NULL;
+}
+
+EAPI void *buffer_handler_pixmap_buffer(struct buffer_info *info)
+{
+       return NULL;
+}
+
+/*!
+ * \return "buffer" object (Not the buffer_info)
+ */
+EAPI void *buffer_handler_pixmap_ref(struct buffer_info *info)
+{
+       return NULL;
+}
+
+/*!
+ * \return "buffer"
+ */
+EAPI void *buffer_handler_pixmap_find(int pixmap)
+{
+       return NULL;
+}
+
+EAPI int buffer_handler_pixmap_release_buffer(void *canvas)
+{
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+/*!
+ * \note
+ *
+ * \return Return NULL if the buffer is in still uses.
+ *        Return buffer_ptr if it needs to destroy
+ */
+EAPI int buffer_handler_pixmap_unref(void *buffer_ptr)
+{
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_is_loaded(const struct buffer_info *info)
+{
+       return info ? info->is_loaded : 0;
+}
+
+EAPI void buffer_handler_update_size(struct buffer_info *info, int w, int h)
+{
+       if (!info) {
+               return;
+       }
+
+       info->w = w;
+       info->h = h;
+}
+
+EAPI int buffer_handler_resize(struct buffer_info *info, int w, int h)
+{
+       int ret;
+
+       if (!info) {
+               ErrPrint("Invalid handler\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->w == w && info->h == h) {
+               DbgPrint("No changes\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       buffer_handler_update_size(info, w, h);
+
+       if (!info->is_loaded) {
+               DbgPrint("Buffer size is updated[%dx%d]\n", w, h);
+               return WIDGET_ERROR_NONE;
+       }
+
+       ret = buffer_handler_unload(info);
+       if (ret < 0) {
+               ErrPrint("Unload: %d\n", ret);
+       }
+
+       ret = buffer_handler_load(info);
+       if (ret < 0) {
+               ErrPrint("Load: %d\n", ret);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI int buffer_handler_get_size(struct buffer_info *info, int *w, int *h)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (w) {
+               *w = info->w;
+       }
+       if (h) {
+               *h = info->h;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+EAPI struct inst_info *buffer_handler_instance(struct buffer_info *info)
+{
+       return info->inst;
+}
+
+EAPI void buffer_handler_flush(struct buffer_info *info)
+{
+       int fd;
+       int size;
+       widget_fb_t buffer;
+
+       if (!info || !info->buffer) {
+               return;
+       }
+
+       buffer = info->buffer;
+
+       if (buffer->type == WIDGET_FB_TYPE_PIXMAP) {
+               /**
+                * @TODO
+                * WL_XXX
+                * Not supported for wayland or this should be ported correctly
+                */
+       } else if (buffer->type == BUFFER_TYPE_FILE) {
+               fd = open(widget_util_uri_to_path(info->id), O_WRONLY | O_CREAT, 0644);
+               if (fd < 0) {
+                       ErrPrint("%s open falied: %d\n", widget_util_uri_to_path(info->id), errno);
+                       return;
+               }
+
+               size = info->w * info->h * info->pixel_size;
+               widget_service_acquire_lock(info->lock_info);
+               if (write(fd, info->buffer, size) != size) {
+                       ErrPrint("write: %d\n", errno);
+               }
+               widget_service_release_lock(info->lock_info);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+       } else {
+               DbgPrint("Flush nothing\n");
+       }
+}
+
+HAPI int buffer_handler_init(void)
+{
+       /*!
+        * \TODO
+        * Implement this for wayland
+        */
+       if (WIDGET_CONF_USE_SW_BACKEND) {
+               DbgPrint("Fallback to the S/W Backend\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int buffer_handler_fini(void)
+{
+       /*!
+        * \TODO
+        * Implement this for wayland
+        */
+       return WIDGET_ERROR_NONE;
+}
+
+static inline struct buffer *raw_open_file(const char *filename)
+{
+       widget_fb_t buffer;
+       int fd;
+       off_t off;
+       int ret;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0) {
+               ErrPrint("open: %d\n", errno);
+               return NULL;
+       }
+
+       off = lseek(fd, 0L, SEEK_END);
+       if (off == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       buffer = calloc(1, sizeof(*buffer) + off);
+       if (!buffer) {
+               ErrPrint("calloc: %d\n", errno);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       buffer->state = WIDGET_FB_STATE_CREATED;
+       buffer->type = WIDGET_FB_TYPE_FILE;
+       buffer->refcnt = 0;
+       buffer->info = (void *)off;
+
+       ret = read(fd, buffer->data, off);
+       if (ret < 0) {
+               ErrPrint("read: %d\n", errno);
+               DbgFree(buffer);
+
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               return NULL;
+       }
+
+       if (close(fd) < 0) {
+               ErrPrint("close: %d\n", errno);
+       }
+
+       return buffer;
+}
+
+static inline int raw_close_file(struct buffer *buffer)
+{
+       DbgFree(buffer);
+       return 0;
+}
+
+static inline struct buffer *raw_open_shm(int shm)
+{
+       widget_fb_t buffer;
+
+       buffer = (widget_fb_t)shmat(shm, NULL, SHM_RDONLY);
+       if (buffer == (struct buffer *)-1) {
+               ErrPrint("shmat: %d\n", errno);
+               return NULL;
+       }
+
+       return buffer;
+}
+
+static inline int raw_close_shm(widget_fb_t buffer)
+{
+       int ret;
+
+       ret = shmdt(buffer);
+       if (ret < 0) {
+               ErrPrint("shmdt: %d\n", errno);
+       }
+
+       return ret;
+}
+
+EAPI void *buffer_handler_raw_data(widget_fb_t buffer)
+{
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return NULL;
+       }
+
+       return buffer->data;
+}
+
+EAPI int buffer_handler_raw_size(struct buffer *buffer)
+{
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return (int)buffer->info;
+}
+
+EAPI widget_fb_t buffer_handler_raw_open(enum widget_fb_type widget_fb_type, void *resource)
+{
+       widget_fb_t handle;
+
+       switch (widget_fb_type) {
+       case WIDGET_FB_TYPE_SHM:
+               handle = raw_open_shm((int)resource);
+               break;
+       case WIDGET_FB_TYPE_FILE:
+               handle = raw_open_file(resource);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+               /**
+                * @TODO
+                *  Implements me
+                */
+       default:
+               handle = NULL;
+               break;
+       }
+
+       return handle;
+}
+
+EAPI int buffer_handler_raw_close(struct buffer *buffer)
+{
+       int ret;
+
+       if (!buffer || buffer->state != WIDGET_FB_STATE_CREATED) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (buffer->type) {
+       case WIDGET_FB_TYPE_SHM:
+               ret = raw_close_shm(buffer);
+               break;
+       case WIDGET_FB_TYPE_FILE:
+               ret = raw_close_file(buffer);
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+       default:
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return ret;
+}
+
+EAPI int buffer_handler_lock(struct buffer_info *info)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (buffer->type == WIDGET_FB_TYPE_FILE) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       return widget_service_acquire_lock(info->lock_info);
+}
+
+EAPI int buffer_handler_unlock(struct buffer_info *buffer)
+{
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_PIXMAP) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (info->type == WIDGET_FB_TYPE_FILE) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       return widget_service_release_lock(info->lock_info);
+}
+
+EAPI int buffer_handler_auto_align(void)
+{
+       return WIDGET_CONF_AUTO_ALIGN;
+}
+
+EAPI int buffer_handler_stride(struct buffer_info *info)
+{
+       widget_fb_t buffer;
+       int stride;
+
+       if (!info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (info->type) {
+       case WIDGET_FB_TYPE_FILE:
+       case WIDGET_FB_TYPE_SHM:
+               stride = info->w * info->pixel_size;
+               break;
+       case WIDGET_FB_TYPE_PIXMAP:
+       default:
+               stride = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       return stride;
+}
+
+/*!
+ * \note
+ * Only can be used by master.
+ * Plugin cannot access the user data
+ */
+
+HAPI int buffer_handler_set_data(struct buffer_info *buffer, void *data)
+{
+       if (!buffer) {
+               ErrPrint("Invalid handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       buffer->data = data;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *buffer_handler_data(struct buffer_info *buffer)
+{
+       if (!buffer) {
+               ErrPrint("Invalid handle\n");
+               return NULL;
+       }
+
+       return buffer->data;
+}
+
+HAPI int buffer_handler_destroy(struct buffer_info *info)
+{
+       Eina_List *l;
+       widget_fb_t buffer;
+
+       if (!info) {
+               DbgPrint("Buffer is not created yet. info is NIL\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       buffer_handler_unload(info);
+       DbgFree(info->id);
+       DbgFree(info);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct buffer_info *buffer_handler_create(struct inst_info *inst, enum buffer_type type, int w, int h, int pixel_size, int auto_align)
+{
+       struct buffer_info *info;
+
+       info = malloc(sizeof(*info));
+       if (!info) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       switch (type) {
+       case WIDGET_FB_TYPE_SHM:
+               if (pixel_size != DEFAULT_PIXELS) {
+                       DbgPrint("SHM only supportes %d bytes pixels (requested: %d)\n", DEFAULT_PIXELS, pixel_size);
+                       pixel_size = DEFAULT_PIXELS;
+               }
+
+               info->id = strdup(SCHEMA_SHM "-1");
+               if (!info->id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info);
+                       return NULL;
+               }
+               break;
+       case BUFFER_TYPE_FILE:
+               if (pixel_size != DEFAULT_PIXELS) {
+                       DbgPrint("FILE only supportes %d bytes pixels (requested: %d)\n", DEFAULT_PIXELS, pixel_size);
+                       pixel_size = DEFAULT_PIXELS;
+               }
+
+               info->id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
+               if (!info->id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info);
+                       return NULL;
+               }
+               break;
+       case BUFFER_TYPE_PIXMAP:
+       default:
+               ErrPrint("Invalid type\n");
+               DbgFree(info);
+               return NULL;
+       }
+
+       info->lock_info = NULL;
+       info->w = w;
+       info->h = h;
+       info->pixel_size = pixel_size;
+       info->type = type;
+       info->is_loaded = 0;
+       info->inst = inst;
+       info->buffer = NULL;
+       info->data = NULL;
+
+       return info;
+}
+
+/* End of a file */
diff --git a/src/client_life.c b/src/client_life.c
new file mode 100644 (file)
index 0000000..8d5c87c
--- /dev/null
@@ -0,0 +1,1026 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/smack.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+
+#include <dlog.h>
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+
+#include "client_life.h"
+#include "instance.h"
+#include "client_rpc.h"
+#include "debug.h"
+#include "util.h"
+#include "slave_life.h"
+#include "xmonitor.h"
+#include "conf.h"
+
+int errno;
+
+static struct {
+       Eina_List *client_list;
+       int nr_of_paused_clients;
+
+       enum global_event_process {
+               GLOBAL_EVENT_PROCESS_IDLE = 0x00,
+               GLOBAL_EVENT_PROCESS_CREATE = 0x01,
+               GLOBAL_EVENT_PROCESS_DESTROY = 0x02
+       } in_event_process;
+
+       Eina_List *create_event_list;
+       Eina_List *destroy_event_list;
+
+} s_info = {
+       .client_list = NULL,
+       .nr_of_paused_clients = 0,
+       .in_event_process = GLOBAL_EVENT_PROCESS_IDLE,
+       .create_event_list = NULL,
+       .destroy_event_list = NULL,
+};
+
+struct subscribe_item {        /* Cluster & Sub-cluster. related with Context-aware service */
+       char *cluster;
+       char *category;
+};
+
+struct category_subscribe_item {
+       char *category;
+};
+
+struct global_event_item {
+       void *cbdata;
+       int (*cb)(struct client_node *client, void *data);
+       int deleted;
+};
+
+struct event_item {
+       void *data;
+       int (*cb)(struct client_node *, void *);
+       int deleted;
+};
+
+struct data_item {
+       char *tag;
+       void *data;
+};
+
+struct client_node {
+       pid_t pid;
+       int refcnt;
+
+       int paused;
+
+       enum client_event_process {
+               CLIENT_EVENT_PROCESS_IDLE = 0x00,
+               CLIENT_EVENT_PROCESS_DEACTIVATE = 0x01,
+               CLIENT_EVENT_PROCESS_ACTIVATE = 0x02
+       } in_event_process;
+       Eina_List *event_deactivate_list;
+       Eina_List *event_activate_list;
+
+       Eina_List *data_list;
+       Eina_List *subscribe_list;
+       Eina_List *category_subscribe_list;
+
+       int faulted;
+       char *direct_addr;
+};
+
+static inline void invoke_global_destroyed_cb(struct client_node *client)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct global_event_item *item;
+
+       s_info.in_event_process |= GLOBAL_EVENT_PROCESS_DESTROY;
+       EINA_LIST_FOREACH_SAFE(s_info.destroy_event_list, l, n, item) {
+               /*!
+                * The first,
+                * item->deleted will be checked, so if it is deleted, remove item from the list
+                * The second, if the first routine takes false path,
+                * item->cb will be called, if it returns negative value, remove item from the list
+                * The third, if the second routine takes false path,
+                * Check the item->deleted again, so if it is turnned on, remove item from the list
+                */
+               if (item->deleted || item->cb(client, item->cbdata) < 0 || item->deleted) {
+                       s_info.destroy_event_list = eina_list_remove(s_info.destroy_event_list, item);
+                       DbgFree(item);
+               }
+       }
+       s_info.in_event_process &= ~GLOBAL_EVENT_PROCESS_DESTROY;
+}
+
+static inline void invoke_global_created_cb(struct client_node *client)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct global_event_item *item;
+
+       s_info.in_event_process |= GLOBAL_EVENT_PROCESS_CREATE;
+       EINA_LIST_FOREACH_SAFE(s_info.create_event_list, l, n, item) {
+               /*!
+                * The first,
+                * item->deleted will be checked, so if it is deleted, remove item from the list
+                * The second, if the first routine takes false path,
+                * item->cb will be called, if it returns negative value, remove item from the list
+                * The third, if the second routine takes false path,
+                * Check the item->deleted again, so if it is turnned on, remove item from the list
+                */
+
+               if (item->deleted || item->cb(client, item->cbdata) < 0 || item->deleted) {
+                       s_info.create_event_list = eina_list_remove(s_info.create_event_list, item);
+                       DbgFree(item);
+               }
+       }
+       s_info.in_event_process &= ~GLOBAL_EVENT_PROCESS_CREATE;
+}
+
+static inline void invoke_deactivated_cb(struct client_node *client)
+{
+       struct event_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       client->in_event_process |= CLIENT_EVENT_PROCESS_DEACTIVATE;
+       EINA_LIST_FOREACH_SAFE(client->event_deactivate_list, l, n, item) {
+               /*!
+                * The first,
+                * item->deleted will be checked, so if it is deleted, remove item from the list
+                * The second, if the first routine takes false path,
+                * item->cb will be called, if it returns negative value, remove item from the list
+                * The third, if the second routine takes false path,
+                * Check the item->deleted again, so if it is turnned on, remove item from the list
+                */
+
+               if (item->deleted || item->cb(client, item->data) < 0 || item->deleted) {
+                       client->event_deactivate_list = eina_list_remove(client->event_deactivate_list, item);
+                       DbgFree(item);
+               }
+       }
+       client->in_event_process &= ~CLIENT_EVENT_PROCESS_DEACTIVATE;
+}
+
+static inline void invoke_activated_cb(struct client_node *client)
+{
+       struct event_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       client->in_event_process |= CLIENT_EVENT_PROCESS_ACTIVATE;
+       EINA_LIST_FOREACH_SAFE(client->event_activate_list, l, n, item) {
+               /*!
+                * The first,
+                * item->deleted will be checked, so if it is deleted, remove item from the list
+                * The second, if the first routine takes false path,
+                * item->cb will be called, if it returns negative value, remove item from the list
+                * The third, if the second routine takes false path,
+                * Check the item->deleted again, so if it is turnned on, remove item from the list
+                */
+
+               if (item->deleted || item->cb(client, item->data) < 0 || item->deleted) {
+                       client->event_activate_list = eina_list_remove(client->event_activate_list, item);
+                       DbgFree(item);
+               }
+       }
+       client->in_event_process &= ~CLIENT_EVENT_PROCESS_ACTIVATE;
+}
+
+static inline void destroy_client_data(struct client_node *client)
+{
+       struct event_item *event;
+       struct data_item *data;
+       struct subscribe_item *item;
+       struct category_subscribe_item *category_item;
+       Ecore_Timer *timer;
+
+       timer = client_del_data(client, "create,timer");
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       DbgPrint("Destroy client: %p\n", client);
+
+       invoke_global_destroyed_cb(client);
+       client_rpc_fini(client); /*!< Finalize the RPC after invoke destroy callbacks */
+
+       EINA_LIST_FREE(client->data_list, data) {
+               DbgPrint("Tag is not cleared (%s)\n", data->tag);
+               DbgFree(data->tag);
+               DbgFree(data);
+       }
+
+       EINA_LIST_FREE(client->event_deactivate_list, event) {
+               DbgFree(event);
+       }
+
+       EINA_LIST_FREE(client->subscribe_list, item) {
+               DbgFree(item->cluster);
+               DbgFree(item->category);
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(client->category_subscribe_list, category_item) {
+               DbgFree(category_item->category);
+               DbgFree(category_item);
+       }
+
+       if (client->paused) {
+               s_info.nr_of_paused_clients--;
+       }
+
+       if (client->direct_addr) {
+               (void)unlink(client->direct_addr);
+               DbgFree(client->direct_addr);
+       }
+
+       s_info.client_list = eina_list_remove(s_info.client_list, client);
+       DbgFree(client);
+
+       /*!
+        * \note
+        * If there is any changes of clients,
+        * We should check the pause/resume states again.
+        */
+       xmonitor_handle_state_changes();
+}
+
+static inline struct client_node *create_client_data(pid_t pid, const char *direct_addr)
+{
+       struct client_node *client;
+
+       client = calloc(1, sizeof(*client));
+       if (!client) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       client->pid = pid;
+       client->refcnt = 1;
+
+       if (direct_addr && direct_addr[0]) {
+               client->direct_addr = strdup(direct_addr);
+               if (!client->direct_addr) {
+                       ErrPrint("Failed to allocate direct_addr (%s)\n", direct_addr);
+               }
+       }
+
+       s_info.client_list = eina_list_append(s_info.client_list, client);
+
+       /*!
+        * \note
+        * Right after create a client ADT,
+        * We assume that the client is paused.
+        */
+       client_paused(client);
+       xmonitor_handle_state_changes();
+       return client;
+}
+
+static Eina_Bool created_cb(void *data)
+{
+       (void)client_del_data(data, "create,timer");
+
+       invoke_global_created_cb(data);
+       invoke_activated_cb(data);
+       /*!
+        * \note
+        * Client PAUSE/RESUME event must has to be sent after created event.
+        */
+       xmonitor_update_state(client_pid(data));
+
+       (void)client_unref(data);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+/*!
+ * \note
+ * Noramlly, client ADT is created when it send the "acquire" packet.
+ * It means we have the handle for communicating with the client already,
+ * So we just create its ADT in this function.
+ * And invoke the global created event & activated event callbacks
+ */
+HAPI struct client_node *client_create(pid_t pid, int handle, const char *direct_addr)
+{
+       struct client_node *client;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (client) {
+               ErrPrint("Client %d(%d) is already exists\n", pid, handle);
+               return client;
+       }
+
+       client = create_client_data(pid, direct_addr);
+       if (!client) {
+               ErrPrint("Failed to create a new client (%d)\n", pid);
+               return NULL;
+       }
+
+       ret = client_rpc_init(client, handle);
+       if (ret < 0) {
+               client = client_unref(client);
+               ErrPrint("Failed to initialize the RPC for %d, Destroy client data %p(has to be 0x0)\n", pid, client);
+       } else {
+               Ecore_Timer *create_timer;
+               /*!
+                * \note
+                * To save the time to send reply packet to the client.
+                */
+               create_timer = ecore_timer_add(DELAY_TIME, created_cb, client_ref(client));
+               if (create_timer == NULL) {
+                       ErrPrint("Failed to add a timer for client created event\n");
+                       client = client_unref(client); /* Decrease refcnt for argument */
+                       client = client_unref(client); /* Destroy client object */
+                       return NULL;
+               }
+
+               ret = client_set_data(client, "create,timer", create_timer);
+               DbgPrint("Set data: %d\n", ret);
+       }
+
+       return client;
+}
+
+HAPI struct client_node *client_ref(struct client_node *client)
+{
+       if (!client) {
+               return NULL;
+       }
+
+       client->refcnt++;
+       return client;
+}
+
+HAPI struct client_node *client_unref(struct client_node *client)
+{
+       if (!client) {
+               return NULL;
+       }
+
+       if (client->refcnt == 0) {
+               ErrPrint("Client refcnt is not managed correctly\n");
+               return NULL;
+       }
+
+       client->refcnt--;
+       if (client->refcnt == 0) {
+               destroy_client_data(client);
+               client = NULL;
+       }
+
+       return client;
+}
+
+HAPI const int const client_refcnt(const struct client_node *client)
+{
+       return client->refcnt;
+}
+
+HAPI const pid_t const client_pid(const struct client_node *client)
+{
+       return client ? client->pid : (pid_t)-1;
+}
+
+HAPI struct client_node *client_find_by_pid(pid_t pid)
+{
+       Eina_List *l;
+       struct client_node *client;
+
+       EINA_LIST_FOREACH(s_info.client_list, l, client) {
+               if (client->pid == pid) {
+                       return client;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct client_node *client_find_by_rpc_handle(int handle)
+{
+       Eina_List *l;
+       struct client_node *client;
+
+       if (handle <= 0) {
+               ErrPrint("Invalid handle %d\n", handle);
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(s_info.client_list, l, client) {
+               if (client_rpc_handle(client) == handle) {
+                       return client;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI const int const client_count_paused(void)
+{
+       return s_info.nr_of_paused_clients;
+}
+
+HAPI int client_is_all_paused(void)
+{
+       DbgPrint("%d, %d\n", eina_list_count(s_info.client_list), s_info.nr_of_paused_clients);
+       return eina_list_count(s_info.client_list) == s_info.nr_of_paused_clients;
+}
+
+HAPI int client_count(void)
+{
+       return eina_list_count(s_info.client_list);
+}
+
+HAPI struct client_node *client_deactivated_by_fault(struct client_node *client)
+{
+       if (!client || client->faulted) {
+               return client;
+       }
+
+       ErrPrint("Client[%p] is faulted(%d), pid(%d)\n", client, client->refcnt, client->pid);
+       client->faulted = 1;
+       client->pid = (pid_t)-1;
+
+       invoke_deactivated_cb(client);
+       client = client_destroy(client);
+       /*!
+        * \todo
+        * Who invokes this function has to care the reference counter of a client
+        * do I need to invoke the deactivated callback from here?
+        * client->pid = (pid_t)-1;
+        * slave_unref(client)
+        */
+       return client;
+}
+
+HAPI const int const client_is_faulted(const struct client_node *client)
+{
+       /*!
+        * \note
+        * If the "client" is NIL, I assume that it is fault so returns TRUE(1)
+        */
+       return client ? client->faulted : 1;
+}
+
+HAPI void client_reset_fault(struct client_node *client)
+{
+       if (client) {
+               client->faulted = 0;
+       }
+}
+
+HAPI int client_event_callback_add(struct client_node *client, enum client_event event, int (*cb)(struct client_node *, void *), void *data)
+{
+       struct event_item *item;
+
+       if (!cb) {
+               ErrPrint("Invalid callback (cb == NULL)\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->cb = cb;
+       item->data = data;
+       item->deleted = 0;
+
+       /*!
+        * \note
+        * Use the eina_list_prepend API.
+        * To keep the sequence of a callback invocation.
+        *
+        * Here is an example sequence.
+        *
+        * client_event_callback_add(CALLBACK_01);
+        * client_event_callback_add(CALLBACK_02);
+        * client_event_callback_add(CALLBACK_03);
+        *
+        * Then the invoke_event_callback function will call the CALLBACKS as below sequence
+        *
+        * invoke_CALLBACK_03
+        * invoke_CALLBACK_02
+        * invoke_CALLBACK_01
+        */
+
+       switch (event) {
+       case CLIENT_EVENT_DEACTIVATE:
+               client->event_deactivate_list = eina_list_prepend(client->event_deactivate_list, item);
+               break;
+       case CLIENT_EVENT_ACTIVATE:
+               client->event_activate_list = eina_list_prepend(client->event_activate_list, item);
+               break;
+       default:
+               DbgFree(item);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_event_callback_del(struct client_node *client, enum client_event event, int (*cb)(struct client_node *, void *), void *data)
+{
+       struct event_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       if (!cb) {
+               ErrPrint("Invalid callback (cb == NULL)\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (event) {
+       case CLIENT_EVENT_DEACTIVATE:
+               EINA_LIST_FOREACH_SAFE(client->event_deactivate_list, l, n, item) {
+                       if (item->cb == cb && item->data == data) {
+                               if (client->in_event_process & CLIENT_EVENT_PROCESS_DEACTIVATE) {
+                                       item->deleted = 1;
+                               } else {
+                                       client->event_deactivate_list = eina_list_remove(client->event_deactivate_list, item);
+                                       DbgFree(item);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+
+       case CLIENT_EVENT_ACTIVATE:
+               EINA_LIST_FOREACH_SAFE(client->event_activate_list, l, n, item) {
+                       if (item->cb == cb && item->data == data) {
+                               if (client->in_event_process & CLIENT_EVENT_PROCESS_ACTIVATE) {
+                                       item->deleted = 1;
+                               } else {
+                                       client->event_activate_list = eina_list_remove(client->event_activate_list, item);
+                                       DbgFree(item);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+
+       default:
+               ErrPrint("Invalid event\n");
+               break;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int client_set_data(struct client_node *client, const char *tag, void *data)
+{
+       struct data_item *item;
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->tag = strdup(tag);
+       if (!item->tag) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->data = data;
+
+       client->data_list = eina_list_append(client->data_list, item);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *client_data(struct client_node *client, const char *tag)
+{
+       Eina_List *l;
+       struct data_item *item;
+
+       EINA_LIST_FOREACH(client->data_list, l, item) {
+               if (!strcmp(item->tag, tag)) {
+                       return item->data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI void *client_del_data(struct client_node *client, const char *tag)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct data_item *item;
+
+       EINA_LIST_FOREACH_SAFE(client->data_list, l, n, item) {
+               if (!strcmp(item->tag, tag)) {
+                       void *data;
+                       client->data_list = eina_list_remove(client->data_list, item);
+                       data = item->data;
+                       DbgFree(item->tag);
+                       DbgFree(item);
+                       return data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI void client_paused(struct client_node *client)
+{
+       if (client->paused) {
+               return;
+       }
+
+       client->paused = 1;
+       s_info.nr_of_paused_clients++;
+}
+
+HAPI void client_resumed(struct client_node *client)
+{
+       if (client->paused == 0) {
+               return;
+       }
+
+       client->paused = 0;
+       s_info.nr_of_paused_clients--;
+}
+
+HAPI int client_init(void)
+{
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void client_fini(void)
+{
+       struct global_event_item *handler;
+       struct client_node *client;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(s_info.client_list, l, n, client) {
+               (void)client_destroy(client);
+       }
+
+       EINA_LIST_FREE(s_info.create_event_list, handler) {
+               DbgFree(handler);
+       }
+
+       EINA_LIST_FREE(s_info.destroy_event_list, handler) {
+               DbgFree(handler);
+       }
+}
+
+HAPI int client_global_event_handler_add(enum client_global_event event_type, int (*cb)(struct client_node *client, void *data), void *data)
+{
+       struct global_event_item *handler;
+
+       handler = malloc(sizeof(*handler));
+       if (!handler) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       handler->cbdata = data;
+       handler->cb = cb;
+       handler->deleted = 0;
+
+       switch (event_type) {
+       case CLIENT_GLOBAL_EVENT_CREATE:
+               s_info.create_event_list = eina_list_prepend(s_info.create_event_list, handler);
+               break;
+       case CLIENT_GLOBAL_EVENT_DESTROY:
+               s_info.destroy_event_list = eina_list_prepend(s_info.destroy_event_list, handler);
+               break;
+       default:
+               DbgFree(handler);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_global_event_handler_del(enum client_global_event event_type, int (*cb)(struct client_node *, void *), void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct global_event_item *item;
+
+       switch (event_type) {
+       case CLIENT_GLOBAL_EVENT_CREATE:
+               EINA_LIST_FOREACH_SAFE(s_info.create_event_list, l, n, item) {
+                       if (item->cb == cb && item->cbdata == data) {
+                               if (s_info.in_event_process & GLOBAL_EVENT_PROCESS_CREATE) {
+                                       item->deleted = 1;
+                               } else {
+                                       s_info.create_event_list = eina_list_remove(s_info.create_event_list, item);
+                                       DbgFree(item);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case CLIENT_GLOBAL_EVENT_DESTROY:
+               EINA_LIST_FOREACH_SAFE(s_info.destroy_event_list, l, n, item) {
+                       if (item->cb == cb && item->cbdata == data) {
+                               if (s_info.in_event_process & GLOBAL_EVENT_PROCESS_DESTROY) {
+                                       item->deleted = 1;
+                               } else {
+                                       s_info.destroy_event_list = eina_list_remove(s_info.destroy_event_list, item);
+                                       DbgFree(item);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int client_subscribe_category(struct client_node *client, const char *category)
+{
+       struct category_subscribe_item *item;
+       Eina_List *l;
+
+       if (!category) {
+               ErrPrint("category: %p\n", category);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       EINA_LIST_FOREACH(client->category_subscribe_list, l, item) {
+               DbgPrint("item[%p], item->category[%p], category[%p]\n", item, item->category, category);
+               if (!strcasecmp(item->category, category)) {
+                       DbgPrint("[%s] is already subscribed\n");
+                       return WIDGET_ERROR_ALREADY_EXIST;
+               }
+       }
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->category = strdup(category);
+       if (!item->category) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgPrint("Subscribe category[%s]\n", item->category);
+       client->category_subscribe_list = eina_list_append(client->category_subscribe_list, item);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_unsubscribe_category(struct client_node *client, const char *category)
+{
+       struct category_subscribe_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       if (!category) {
+               ErrPrint("category: %p\n", category);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       EINA_LIST_FOREACH_SAFE(client->category_subscribe_list, l, n, item) {
+               if (!strcasecmp(category, item->category)) {
+                       client->category_subscribe_list = eina_list_remove(client->category_subscribe_list, item);
+                       DbgFree(item->category);
+                       DbgFree(item);
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int client_is_subscribed_by_category(struct client_node *client, const char *category)
+{
+       struct category_subscribe_item *item;
+       Eina_List *l;
+
+       if (!category) {
+               DbgPrint("category[%p]\n", category);
+               return 0;
+       }
+
+       EINA_LIST_FOREACH(client->category_subscribe_list, l, item) {
+               DbgPrint("item[%p], item->cateogyr[%p], category[%p]\n", item, item->category, category);
+               if (!strcasecmp(item->category, category)) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+HAPI int client_subscribe_group(struct client_node *client, const char *cluster, const char *category)
+{
+       struct subscribe_item *item;
+
+       if (!cluster || !category) {
+               ErrPrint("Cluster[%p] Category[%p]\n", cluster, category);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->cluster = strdup(cluster);
+       if (!item->cluster) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->category = strdup(category);
+       if (!item->category) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item->cluster);
+               DbgFree(item);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       client->subscribe_list = eina_list_append(client->subscribe_list, item);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_unsubscribe_group(struct client_node *client, const char *cluster, const char *category)
+{
+       struct subscribe_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       if (!cluster || !category) {
+               ErrPrint("cluster: %p, category: %p\n", cluster, category);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       EINA_LIST_FOREACH_SAFE(client->subscribe_list, l, n, item) {
+               if (!strcasecmp(cluster, item->cluster) && !strcasecmp(category, item->category)) {
+                       client->subscribe_list = eina_list_remove(client->subscribe_list, item);
+                       DbgFree(item->cluster);
+                       DbgFree(item->category);
+                       DbgFree(item);
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int client_is_subscribed_group(struct client_node *client, const char *cluster, const char *category)
+{
+       struct subscribe_item *item;
+       Eina_List *l;
+
+       if (!cluster || !category) {
+               ErrPrint("cluster: %p, category: %p\n", cluster, category);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       EINA_LIST_FOREACH(client->subscribe_list, l, item) {
+               if (!strcmp(item->cluster, "*")) {
+                       return 1;
+               }
+
+               if (strcasecmp(item->cluster, cluster)) {
+                       continue;
+               }
+
+               if (!strcmp(item->category, "*")) {
+                       return 1;
+               }
+
+               if (!strcasecmp(item->category, category)) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+HAPI int client_browse_group_list(const char *cluster, const char *category, int (*cb)(struct client_node *client, void *data), void *data)
+{
+       Eina_List *l;
+       struct client_node *client;
+       int cnt;
+
+       if (!cb || !cluster || !category) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       cnt = 0;
+       EINA_LIST_FOREACH(s_info.client_list, l, client) {
+               if (!client_is_subscribed_group(client, cluster, category)) {
+                       continue;
+               }
+
+               if (cb(client, data) < 0) {
+                       return WIDGET_ERROR_CANCELED;
+               }
+
+               cnt++;
+       }
+
+       return cnt;
+}
+
+HAPI int client_browse_category_list(const char *category, int (*cb)(struct client_node *client, void *data), void *data)
+{
+       Eina_List *l;
+       struct client_node *client;
+       int cnt;
+
+       if (!cb || !category) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       cnt = 0;
+       EINA_LIST_FOREACH(s_info.client_list, l, client) {
+               if (!client_is_subscribed_by_category(client, category)) {
+                       continue;
+               }
+
+               if (cb(client, data) < 0) {
+                       return WIDGET_ERROR_CANCELED;
+               }
+
+               cnt++;
+       }
+
+       return cnt;
+}
+
+HAPI int client_count_of_group_subscriber(const char *cluster, const char *category)
+{
+       Eina_List *l;
+       struct client_node *client;
+       int cnt;
+
+       cnt = 0;
+       EINA_LIST_FOREACH(s_info.client_list, l, client) {
+               cnt += !!client_is_subscribed_group(client, cluster, category);
+       }
+
+       return cnt;
+}
+
+HAPI int client_broadcast(struct inst_info *inst, struct packet *packet)
+{
+       Eina_List *l;
+       Eina_List *list;
+       struct client_node *client;
+
+       list = inst ? instance_client_list(inst) : s_info.client_list;
+       EINA_LIST_FOREACH(list, l, client) {
+               if (client_pid(client) == -1) {
+                       ErrPrint("Client[%p] has PID[%d]\n", client, client_pid(client));
+                       continue;
+               }
+
+               (void)client_rpc_async_request(client, packet_ref(packet));
+       }
+
+       packet_unref(packet);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char *client_direct_addr(const struct client_node *client)
+{
+       return client ? client->direct_addr : NULL;
+}
+
+/* End of a file */
diff --git a/src/client_rpc.c b/src/client_rpc.c
new file mode 100644 (file)
index 0000000..67d3ba6
--- /dev/null
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+
+#include <dlog.h>
+#include <com-core_packet.h>
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_conf.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+
+#include "client_life.h"
+#include "instance.h"
+#include "client_rpc.h"
+#include "debug.h"
+#include "conf.h"
+#include "util.h"
+
+#define RPC_TAG "rpc"
+
+/*!
+ * \note
+ * Static component information structure.
+ */
+static struct info {
+       Eina_List *command_list; /*!< Packet Q: Before sending the request, all request commands will stay here */
+       Ecore_Timer *command_consumer; /*!< This timer will consuming the command Q. sending them to the specified client */
+} s_info = {
+       .command_list = NULL,
+       .command_consumer = NULL,
+};
+
+struct client_rpc {
+       int handle; /*!< Handler for communication with client */
+};
+
+struct command {
+       struct packet *packet;
+       struct client_node *client; /*!< Target client. who should receive this command */
+};
+
+/*!
+ * \brief
+ * Creating or Destroying command object
+ */
+static inline struct command *create_command(struct client_node *client, struct packet *packet)
+{
+       struct command *command;
+
+       command = calloc(1, sizeof(*command));
+       if (!command) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       command->packet = packet_ref(packet);
+       command->client = client_ref(client);
+
+       return command;
+}
+
+static inline void destroy_command(struct command *command)
+{
+       client_unref(command->client);
+       packet_unref(command->packet);
+       DbgFree(command);
+}
+
+static inline int count_command(void)
+{
+       return eina_list_count(s_info.command_list);
+}
+
+static inline struct command *pop_command(void)
+{
+       struct command *command;
+
+       command = eina_list_nth(s_info.command_list, 0);
+       if (!command) {
+               return NULL;
+       }
+
+       s_info.command_list = eina_list_remove(s_info.command_list, command);
+       return command;
+}
+
+static Eina_Bool command_consumer_cb(void *data)
+{
+       struct command *command;
+       struct client_rpc *rpc;
+       int ret;
+
+       command = pop_command();
+       if (!command) {
+               s_info.command_consumer = NULL;
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!command->client) {
+               DbgPrint("Has no client\n");
+               goto out;
+       }
+
+       if (client_is_faulted(command->client)) {
+               ErrPrint("Client[%p] is faulted, discard command\n", command->client);
+               goto out;
+       }
+
+       rpc = client_data(command->client, RPC_TAG);
+       if (!rpc) {
+               ErrPrint("Client is not activated\n");
+               goto out;
+       }
+
+       if (rpc->handle < 0) {
+               DbgPrint("RPC is not initialized\n");
+               goto out;
+       }
+
+       ret = com_core_packet_send_only(rpc->handle, command->packet);
+       if (ret < 0) {
+               ErrPrint("Failed to send packet %d\n", ret);
+       }
+
+out:
+       destroy_command(command);
+       return ECORE_CALLBACK_RENEW;
+}
+
+static inline void push_command(struct command *command)
+{
+       s_info.command_list = eina_list_append(s_info.command_list, command);
+
+       if (s_info.command_consumer) {
+               return;
+       }
+
+       s_info.command_consumer = ecore_timer_add(WIDGET_CONF_PACKET_TIME, command_consumer_cb, NULL);
+       if (!s_info.command_consumer) {
+               ErrPrint("Failed to add command consumer\n");
+               s_info.command_list = eina_list_remove(s_info.command_list, command);
+               destroy_command(command);
+       }
+}
+
+HAPI int client_rpc_async_request(struct client_node *client, struct packet *packet)
+{
+       struct command *command;
+       struct client_rpc *rpc;
+
+       if (!client || !packet) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (client_is_faulted(client)) {
+               ErrPrint("Client[%p] is faulted\n", client);
+               packet_unref(packet);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       rpc = client_data(client, RPC_TAG);
+       if (!rpc) {
+               ErrPrint("Client[%p] is not ready for communication (%s)\n", client, packet_command(packet));
+       }
+
+       command = create_command(client, packet);
+       if (!command) {
+               packet_unref(packet);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       push_command(command);
+       packet_unref(packet);
+       return WIDGET_ERROR_NONE;
+}
+
+static int deactivated_cb(struct client_node *client, void *data)
+{
+       struct client_rpc *rpc;
+       struct command *command;
+       Eina_List *l;
+       Eina_List *n;
+
+       rpc = client_data(client, RPC_TAG);
+       if (!rpc) {
+               ErrPrint("client is not valid\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       DbgPrint("Reset handle for %d\n", client_pid(client));
+       rpc->handle = -1;
+
+       DbgPrint("Begin: Destroying command\n");
+       EINA_LIST_FOREACH_SAFE(s_info.command_list, l, n, command) {
+               if (command->client == client) {
+                       s_info.command_list = eina_list_remove(s_info.command_list, command);
+                       destroy_command(command);
+               }
+       }
+       DbgPrint("End: Destroying command\n");
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_rpc_init(struct client_node *client, int handle)
+{
+       struct client_rpc *rpc;
+       int ret;
+
+       rpc = calloc(1, sizeof(*rpc));
+       if (!rpc) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = client_set_data(client, RPC_TAG, rpc);
+       if (ret < 0) {
+               ErrPrint("Failed to set \"rpc\" for client\n");
+               DbgFree(rpc);
+               return ret;
+       }
+
+       DbgPrint("CLIENT: New handle assigned for %d, %d (old: %d)\n", client_pid(client), handle, rpc->handle);
+       rpc->handle = handle;
+
+       ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, deactivated_cb, NULL);
+       if (ret < 0) {
+               struct client_rpc *weird;
+
+               weird = client_del_data(client, RPC_TAG);
+               if (weird != rpc) {
+                       ErrPrint("What happens? (%p <> %p)\n", weird, rpc);
+               }
+               DbgFree(rpc);
+       }
+
+       return ret;
+}
+
+HAPI int client_rpc_fini(struct client_node *client)
+{
+       struct client_rpc *rpc;
+
+       rpc = client_del_data(client, RPC_TAG);
+       if (!rpc) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, deactivated_cb, NULL);
+       DbgFree(rpc);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int client_rpc_handle(struct client_node *client)
+{
+       struct client_rpc *rpc;
+
+       rpc = client_data(client, RPC_TAG);
+       if (!rpc) {
+               DbgPrint("Client has no RPC\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return rpc->handle;
+}
+
+/* End of a file */
diff --git a/src/conf.c b/src/conf.c
new file mode 100644 (file)
index 0000000..8f52983
--- /dev/null
@@ -0,0 +1,8 @@
+#include "conf.h"
+
+struct conf g_conf = {
+       .debug_mode = 0,
+       .slave_max_load = -1,
+};
+
+/* End of a file */
diff --git a/src/critical_log.c b/src/critical_log.c
new file mode 100644 (file)
index 0000000..fce937f
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <errno.h>
+#include <string.h>
+#include <libgen.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include <dlog.h>
+#include <Eina.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#include <widget_conf.h>
+#else
+#include "lite-errno.h"
+#endif
+
+#include "conf.h"
+#include "debug.h"
+#include "util.h"
+#include "critical_log.h"
+
+static struct {
+       FILE *fp;
+       int file_id;
+       int nr_of_lines;
+       char *filename;
+       pthread_mutex_t cri_lock;
+} s_info = {
+       .fp = NULL,
+       .file_id = 0,
+       .nr_of_lines = 0,
+       .filename = NULL,
+       .cri_lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
+
+
+static inline void rotate_log(void)
+{
+       char *filename;
+       int namelen;
+
+       if (s_info.nr_of_lines < WIDGET_CONF_MAX_LOG_LINE) {
+               return;
+       }
+
+       s_info.file_id = (s_info.file_id + 1) % WIDGET_CONF_MAX_LOG_FILE;
+
+       namelen = strlen(s_info.filename) + strlen(WIDGET_CONF_LOG_PATH) + 30;
+       filename = malloc(namelen);
+       if (filename) {
+               snprintf(filename, namelen, "%s/%d_%s.%d", WIDGET_CONF_LOG_PATH, s_info.file_id, s_info.filename, getpid());
+
+               if (s_info.fp) {
+                       if (fclose(s_info.fp) != 0) {
+                               ErrPrint("fclose: %d\n", errno);
+                       }
+               }
+
+               s_info.fp = fopen(filename, "w+");
+               if (!s_info.fp) {
+                       ErrPrint("Failed to open a file: %s\n", filename);
+               }
+
+               DbgFree(filename);
+       }
+
+       s_info.nr_of_lines = 0;
+}
+
+
+
+HAPI int critical_log(const char *func, int line, const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       if (!s_info.fp) {
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       CRITICAL_SECTION_BEGIN(&s_info.cri_lock);
+
+       fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);
+
+       va_start(ap, fmt);
+       ret = vfprintf(s_info.fp, fmt, ap);
+       va_end(ap);
+
+       fflush(s_info.fp);
+
+       s_info.nr_of_lines++;
+       rotate_log();
+
+       CRITICAL_SECTION_END(&s_info.cri_lock);
+       return ret;
+}
+
+
+
+HAPI int critical_log_init(const char *name)
+{
+       int namelen;
+       char *filename;
+
+       if (s_info.fp) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       s_info.filename = strdup(name);
+       if (!s_info.filename) {
+               ErrPrint("Failed to create a log file\n");
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       namelen = strlen(name) + strlen(WIDGET_CONF_LOG_PATH) + 30;
+
+       filename = malloc(namelen);
+       if (!filename) {
+               ErrPrint("Failed to create a log file\n");
+               DbgFree(s_info.filename);
+               s_info.filename = NULL;
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       snprintf(filename, namelen, "%s/%d_%s.%d", WIDGET_CONF_LOG_PATH, s_info.file_id, name, getpid());
+
+       s_info.fp = fopen(filename, "w+");
+       if (!s_info.fp) {
+               ErrPrint("fopen: %d\n", errno);
+               DbgFree(s_info.filename);
+               s_info.filename = NULL;
+               DbgFree(filename);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       DbgFree(filename);
+       return WIDGET_ERROR_NONE;
+}
+
+
+
+HAPI void critical_log_fini(void)
+{
+       if (s_info.filename) {
+               DbgFree(s_info.filename);
+               s_info.filename = NULL;
+       }
+
+       if (s_info.fp) {
+               if (fclose(s_info.fp) != 0) {
+                       ErrPrint("fclose: %d\n", errno);
+               }
+               s_info.fp = NULL;
+       }
+}
+
+
+
+/* End of a file */
diff --git a/src/dead_monitor.c b/src/dead_monitor.c
new file mode 100644 (file)
index 0000000..3233223
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <gio/gio.h>
+#include <packet.h>
+#include <com-core.h>
+#include <dlog.h>
+
+#include <Eina.h>
+#include <widget_service.h> /* destroy_type for instance.h */
+#include <widget_service_internal.h> /* destroy_type for instance.h */
+#include <widget_errno.h>
+
+#include "slave_life.h"
+#include "client_life.h"
+#include "instance.h"
+#include "fault_manager.h"
+#include "util.h"
+#include "debug.h"
+#include "widget-mgr.h"
+#include "conf.h"
+
+struct cb_item {
+       int handle;
+       void (*dead_cb)(int handle, void *data);
+       void *data;
+};
+
+static struct info {
+       Eina_List *cb_list;
+} s_info = {
+       .cb_list = NULL,
+};
+
+static int evt_cb(int handle, void *data)
+{
+       struct slave_node *slave;
+       struct client_node *client;
+       struct widget_mgr *widget_mgr;
+       struct cb_item *dead_item;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(s_info.cb_list, l, n, dead_item) {
+               if (dead_item->handle == handle) {
+                       dead_item->dead_cb(dead_item->handle, dead_item->data);
+                       break;
+               }
+       }
+
+       slave = slave_find_by_rpc_handle(handle);
+       if (slave) {
+               if (slave_pid(slave) != (pid_t)-1) {
+                       switch (slave_state(slave)) {
+                       case SLAVE_REQUEST_TO_DISCONNECT:
+                               DbgPrint("Disconnected from %d\n", slave_pid(slave));
+                       case SLAVE_REQUEST_TO_TERMINATE:
+                               slave = slave_deactivated(slave);
+                               break;
+                       default:
+                               slave = slave_deactivated_by_fault(slave);
+                               break;
+                       }
+               }
+
+               if (!slave) {
+                       DbgPrint("Slave is deleted\n");
+               }
+
+               return 0;
+       }
+
+       client = client_find_by_rpc_handle(handle);
+       if (client) {
+               if (client_pid(client) != (pid_t)-1) {
+                       client = client_deactivated_by_fault(client);
+               }
+
+               if (!client) {
+                       DbgPrint("Client is deleted\n");
+               }
+
+               return 0;
+       }
+
+       widget_mgr = widget_mgr_find_by_handle(handle);
+       if (widget_mgr) {
+               widget_mgr_destroy(widget_mgr);
+               return 0;
+       }
+
+       return 0;
+}
+
+HAPI int dead_init(void)
+{
+       com_core_add_event_callback(CONNECTOR_DISCONNECTED, evt_cb, NULL);
+       return 0;
+}
+
+HAPI int dead_fini(void)
+{
+       struct cb_item *item;
+
+       com_core_del_event_callback(CONNECTOR_DISCONNECTED, evt_cb, NULL);
+
+       EINA_LIST_FREE(s_info.cb_list, item) {
+               DbgFree(item);
+       }
+
+       return 0;
+}
+
+HAPI int dead_callback_add(int handle, void (*dead_cb)(int handle, void *data), void *data)
+{
+       struct cb_item *item;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(s_info.cb_list, l, item) {
+               if (item->handle == handle) {
+                       return WIDGET_ERROR_ALREADY_EXIST;
+               }
+       }
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->handle = handle;
+       item->dead_cb = dead_cb;
+       item->data = data;
+
+       s_info.cb_list = eina_list_append(s_info.cb_list, item);
+       return 0;
+}
+
+HAPI void *dead_callback_del(int handle, void (*dead_cb)(int handle, void *data))
+{
+       struct cb_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(s_info.cb_list, l, n, item) {
+               if (item->handle == handle && item->dead_cb == dead_cb) {
+                       void *cbdata;
+
+                       s_info.cb_list = eina_list_remove(s_info.cb_list, item);
+                       cbdata = item->data;
+                       DbgFree(item);
+
+                       return cbdata;
+               }
+       }
+
+       return NULL;
+}
+
+/* End of a file */
diff --git a/src/event.c b/src/event.c
new file mode 100644 (file)
index 0000000..1fd139a
--- /dev/null
@@ -0,0 +1,1108 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <linux/input.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+#include <dlog.h>
+#include <widget_errno.h>
+#include <widget_conf.h>
+
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+#include "event.h"
+
+#define EVENT_CH       'e'
+#define EVENT_EXIT     'x'
+
+#define PRESSURE 10
+#define DELAY_COMPENSATOR 0.1f
+
+#if !defined(EVIOCSCLOCKID)
+/**
+ * @note
+ * I just copy this from the latest input.h file.
+ * It could not be compatible with the latest one.
+ */
+#define EVIOCSCLOCKID           _IOW('E', 0xa0, int)                    /* Set clockid to be used for timestamps */
+#endif
+
+int errno;
+
+static struct info {
+       pthread_t tid;
+       Eina_List *event_list;
+       int handle;
+       pthread_mutex_t event_list_lock;
+       int evt_pipe[PIPE_MAX];
+       int tcb_pipe[PIPE_MAX];
+       Ecore_Fd_Handler *event_handler;
+
+       struct event_data event_data;
+       struct event_data skipped_event_data;
+
+       Eina_List *event_listener_list;
+       Eina_List *reactivate_list;
+
+       enum event_handler_activate_type event_handler_activated;
+       int timestamp_updated;
+} s_info = {
+       .event_handler_activated = EVENT_HANDLER_DEACTIVATED,
+       .event_list = NULL,
+       .handle = -1,
+       .event_handler = NULL,
+       .evt_pipe = { -1, -1 },
+       .tcb_pipe = { -1, -1 },
+
+       .event_data = {
+               .x = -1,
+               .y = -1,
+               .device = -1,
+               .slot = -1,
+               .keycode = 0,
+       },
+
+       .skipped_event_data = {
+               .x = -1,
+               .y = -1,
+               .device = -1,
+               .slot = -1,
+               .keycode = 0,
+       },
+
+       .event_listener_list = NULL,
+       .reactivate_list = NULL,
+       .timestamp_updated = 0,
+};
+
+struct event_listener {
+       int (*event_cb)(enum event_state state, struct event_data *event, void *data);
+       void *cbdata;
+
+       enum event_state prev_state;
+       enum event_state state;
+
+       double tv;
+       int x; /* RelX */
+       int y; /* RelY */
+};
+
+static int event_control_fini(void);
+
+HAPI int event_init(void)
+{
+       int ret;
+
+       ret = pthread_mutex_init(&s_info.event_list_lock, NULL);
+       if (ret != 0) {
+               ErrPrint("Mutex: %d\n", ret);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int event_fini(void)
+{
+       int ret;
+
+       event_control_fini();
+
+       ret = pthread_mutex_destroy(&s_info.event_list_lock);
+       if (ret != 0) {
+               ErrPrint("Mutex destroy: %d\n", ret);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/*
+ * This function can be called Event Thread.
+ */
+static int push_event_item(void)
+{
+       struct event_data *item;
+
+       if (s_info.event_data.x < 0 || s_info.event_data.y < 0) {
+               /* Waiting full event packet */
+               return WIDGET_ERROR_NONE;
+       }
+
+       item = malloc(sizeof(*item));
+       if (item) {
+               char event_ch = EVENT_CH;
+
+               memcpy(item, &s_info.event_data, sizeof(*item));
+
+               CRITICAL_SECTION_BEGIN(&s_info.event_list_lock);
+               s_info.event_list = eina_list_append(s_info.event_list, item);
+               CRITICAL_SECTION_END(&s_info.event_list_lock);
+
+               if (write(s_info.evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+                       ErrPrint("write: %d\n", errno);
+                       return WIDGET_ERROR_IO_ERROR;
+               }
+
+               /* Take a breathe */
+               pthread_yield();
+       } else {
+               ErrPrint("malloc: %d\n", errno);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static double current_time_get(void)
+{
+       double ret;
+
+       if (WIDGET_CONF_USE_GETTIMEOFDAY) {
+               struct timeval tv;
+               if (gettimeofday(&tv, NULL) < 0) {
+                       ErrPrint("gettimeofday: %d\n", errno);
+                       ret = ecore_time_get();
+               } else {
+                       ret = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0f);
+               }
+       } else {
+               ret = ecore_time_get();
+       }
+
+       return ret;
+}
+
+static void update_timestamp(struct input_event *event)
+{
+       /*
+        * Input event uses timeval instead of timespec,
+        * but its value is same as MONOTIC CLOCK TIME
+        * So we should handles it properly.
+        */
+       s_info.event_data.tv = (double)event->time.tv_sec + (double)event->time.tv_usec / 1000000.0f;
+       s_info.timestamp_updated = 1;
+}
+
+static void processing_ev_abs(struct input_event *event)
+{
+       switch (event->code) {
+#if defined(ABS_X)
+       case ABS_X:
+               break;
+#endif
+#if defined(ABS_Y)
+       case ABS_Y:
+               break;
+#endif
+#if defined(ABS_Z)
+       case ABS_Z:
+               break;
+#endif
+#if defined(ABS_RX)
+       case ABS_RX:
+               break;
+#endif
+#if defined(ABS_RY)
+       case ABS_RY:
+               break;
+#endif
+#if defined(ABS_RZ)
+       case ABS_RZ:
+               break;
+#endif
+#if defined(ABS_THROTTLE)
+       case ABS_THROTTLE:
+               break;
+#endif
+#if defined(ABS_RUDDER)
+       case ABS_RUDDER:
+               break;
+#endif
+#if defined(ABS_WHEEL)
+       case ABS_WHEEL:
+               break;
+#endif
+#if defined(ABS_GAS)
+       case ABS_GAS:
+               break;
+#endif
+#if defined(ABS_BRAKE)
+       case ABS_BRAKE:
+               break;
+#endif
+#if defined(ABS_HAT0X)
+       case ABS_HAT0X:
+               break;
+#endif
+#if defined(ABS_HAT0Y)
+       case ABS_HAT0Y:
+               break;
+#endif
+#if defined(ABS_HAT1X)
+       case ABS_HAT1X:
+               break;
+#endif
+#if defined(ABS_HAT1Y)
+       case ABS_HAT1Y:
+               break;
+#endif
+#if defined(ABS_HAT2X)
+       case ABS_HAT2X:
+               break;
+#endif
+#if defined(ABS_HAT2Y)
+       case ABS_HAT2Y:
+               break;
+#endif
+#if defined(ABS_HAT3X)
+       case ABS_HAT3X:
+               break;
+#endif
+#if defined(ABS_HAT3Y)
+       case ABS_HAT3Y:
+               break;
+#endif
+#if defined(ABS_PRESSURE)
+       case ABS_PRESSURE:
+               break;
+#endif
+#if defined(ABS_TILT_X)
+       case ABS_TILT_X:
+               break;
+#endif
+#if defined(ABS_TILT_Y)
+       case ABS_TILT_Y:
+               break;
+#endif
+#if defined(ABS_TOOL_WIDTH)
+       case ABS_TOOL_WIDTH:
+               break;
+#endif
+#if defined(ABS_VOLUME)
+       case ABS_VOLUME:
+               break;
+#endif
+#if defined(ABS_MISC)
+       case ABS_MISC:
+               break;
+#endif
+#if defined(ABS_DISTANCE)
+       case ABS_DISTANCE:
+               s_info.event_data.distance = event->value;
+               break;
+#endif
+#if defined(ABS_MT_POSITION_X)
+       case ABS_MT_POSITION_X:
+               s_info.event_data.x = event->value;
+               break;
+#endif
+#if defined(ABS_MT_POSITION_Y)
+       case ABS_MT_POSITION_Y:
+               s_info.event_data.y = event->value;
+               break;
+#endif
+#if defined(ABS_MT_SLOT)
+       case ABS_MT_SLOT:
+               s_info.event_data.slot = event->value;
+               break;
+#endif
+#if defined(ABS_MT_TRACKING_ID)
+       case ABS_MT_TRACKING_ID:
+               s_info.event_data.device = event->value;
+               break;
+#endif
+#if defined(ABS_MT_TOUCH_MAJOR)
+       case ABS_MT_TOUCH_MAJOR:
+               s_info.event_data.touch.major = event->value;
+               break;
+#endif
+#if defined(ABS_MT_TOUCH_MINOR)
+       case ABS_MT_TOUCH_MINOR:
+               s_info.event_data.touch.minor = event->value;
+               break;
+#endif
+#if defined(ABS_MT_WIDTH_MAJOR)
+       case ABS_MT_WIDTH_MAJOR:
+               s_info.event_data.width.major = event->value;
+               break;
+#endif
+#if defined(ABS_MT_WIDTH_MINOR)
+       case ABS_MT_WIDTH_MINOR:
+               s_info.event_data.width.minor = event->value;
+               break;
+#endif
+#if defined(ABS_MT_ORIENTATION)
+       case ABS_MT_ORIENTATION:
+               s_info.event_data.orientation = event->value;
+               break;
+#endif
+#if defined(ABS_MT_PRESSURE)
+       case ABS_MT_PRESSURE:
+               s_info.event_data.pressure = event->value;
+               break;
+#endif
+#if defined(ABS_MT_TOOL_X)
+       case ABS_MT_TOOL_X:
+               DbgPrint("TOOL_X: %d\n", event->value);
+               break;
+#endif
+#if defined(ABS_MT_TOOL_Y)
+       case ABS_MT_TOOL_Y:
+               DbgPrint("TOOL_Y: %d\n", event->value);
+               break;
+#endif
+#if defined(ABS_MT_TOOL_TYPE)
+       case ABS_MT_TOOL_TYPE:
+               DbgPrint("TOOL_TYPE: %d\n", event->value);
+               break;
+#endif
+#if defined(ABS_MT_BLOB_ID)
+       case ABS_MT_BLOB_ID:
+               DbgPrint("BLOB_ID: %d\n", event->value);
+               break;
+#endif
+#if defined(ABS_MT_DISTANCE)
+       case ABS_MT_DISTANCE:
+               DbgPrint("DISTANCE: %d\n", event->value);
+               break;
+#endif
+#if defined(ABS_MT_PALM)
+       case ABS_MT_PALM:
+               DbgPrint("PALM: %d\n", event->value);
+               break;
+#endif
+       default:
+#if defined(ABS_MT_COMPONENT)
+               if (event->code == ABS_MT_COMPONENT) {
+                       DbgPrint("COMPONENT: %d\n", event->value);
+                       break;
+               }
+#endif
+#if defined(ABS_MT_ANGLE)
+               if (event->code == ABS_MT_ANGLE) {
+                       DbgPrint("ANGLE: %d\n", event->value);
+                       break;
+               }
+#endif
+#if defined(ABS_MT_SUMSIZE)
+               if (event->code == ABS_MT_SUMSIZE) {
+                       DbgPrint("SUMSIZE: %d\n", event->value);
+                       break;
+               }
+#endif
+               break;
+       }
+
+       return;
+}
+
+/*
+ * Called by Event Thread
+ */
+static inline int processing_input_event(struct input_event *event)
+{
+       int ret;
+
+       if (s_info.timestamp_updated == 0) {
+               update_timestamp(event);
+       }
+
+       switch (event->type) {
+       case EV_SYN:
+               switch (event->code) {
+               case SYN_CONFIG:
+                       break;
+               case SYN_MT_REPORT:
+               case SYN_REPORT:
+                       s_info.timestamp_updated = 0;
+                       ret = push_event_item();
+                       if (ret < 0) {
+                               return ret;
+                       }
+
+                       break;
+#if defined(SYN_DROPPED)
+               case SYN_DROPPED:
+                       DbgPrint("EV_SYN, SYN_DROPPED\n");
+                       break;
+#endif
+               default:
+                       DbgPrint("EV_SYN, 0x%x\n", event->code);
+                       break;
+               }
+               break;
+       case EV_KEY:
+               DbgPrint("EV_KEY: 0x%X\n", event->value);
+               s_info.event_data.keycode = event->value;
+               break;
+       case EV_REL:
+               DbgPrint("EV_REL: 0x%X\n", event->value);
+               break;
+       case EV_ABS:
+               processing_ev_abs(event);
+               break;
+       case EV_MSC:
+       case EV_SW:
+       case EV_LED:
+       case EV_SND:
+       case EV_REP:
+       case EV_FF:
+       case EV_PWR:
+       case EV_FF_STATUS:
+       default:
+               DbgPrint("0x%X, 0x%X\n", event->type, event->code);
+               break;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static void *event_thread_main(void *data)
+{
+       fd_set set;
+       long ret = 0;
+       struct input_event input_event;
+       char *ptr = (char *)&input_event;
+       int offset = 0;
+       int readsize = 0;
+       int fd;
+       char event_ch;
+
+       while (1) {
+               FD_ZERO(&set);
+               FD_SET(s_info.handle, &set);
+               FD_SET(s_info.tcb_pipe[PIPE_READ], &set);
+
+               fd = s_info.handle > s_info.tcb_pipe[PIPE_READ] ? s_info.handle : s_info.tcb_pipe[PIPE_READ];
+               ret = select(fd + 1, &set, NULL, NULL, NULL);
+               if (ret < 0) {
+                       ret = -errno;
+                       if (errno == EINTR) {
+                               DbgPrint("Select receives INTR\n");
+                               continue;
+                       }
+                       ErrPrint("select: %d\n", errno);
+                       break;
+               } else if (ret == 0) {
+                       ErrPrint("Timeout expired\n");
+                       ret = WIDGET_ERROR_TIMED_OUT;
+                       break;
+               }
+
+               if (FD_ISSET(s_info.handle, &set)) {
+                       readsize = read(s_info.handle, ptr + offset, sizeof(input_event) - offset);
+                       if (readsize < 0) {
+                               ErrPrint("read: %d / fd: %d / offset: %d / size: %d - %d\n", errno, s_info.handle, offset, sizeof(input_event), readsize);
+                               ret = WIDGET_ERROR_FAULT;
+                               break;
+                       }
+
+                       offset += readsize;
+                       if (offset == sizeof(input_event)) {
+                               offset = 0;
+                               if (processing_input_event(&input_event) < 0) {
+                                       ret = WIDGET_ERROR_FAULT;
+                                       break;
+                               }
+                       }
+
+                       /*
+                        * If there is input event,
+                        * Try again to get the input event.
+                        */
+               } else if (!s_info.timestamp_updated && FD_ISSET(s_info.tcb_pipe[PIPE_READ], &set)) {
+                       /*!
+                        * Check the s_info.timestamp_updated flag.
+                        * If it is not ZERO, it means, there is event to be processed.
+                        * So we have to wait it to be finished.
+                        */
+                       if (read(s_info.tcb_pipe[PIPE_READ], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+                               ErrPrint("read: %d\n", errno);
+                       }
+
+                       ret = WIDGET_ERROR_CANCELED;
+                       break;
+               }
+       }
+
+       event_ch = EVENT_EXIT;
+       if (write(s_info.evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+               ErrPrint("write: %d\n", errno);
+       }
+
+       return (void *)ret;
+}
+
+static int invoke_event_cb(struct event_listener *listener, struct event_data *item)
+{
+       struct event_data modified_item;
+
+       memcpy(&modified_item, item, sizeof(modified_item));
+
+       modified_item.x -= listener->x;
+       modified_item.y -= listener->y;
+
+       if (!WIDGET_CONF_USE_EVENT_TIME) {
+               item->tv = current_time_get();
+       }
+
+       if (listener->event_cb(listener->state, &modified_item, listener->cbdata) < 0) {
+               if (eina_list_data_find(s_info.event_listener_list, listener)) {
+                       s_info.event_listener_list = eina_list_remove(s_info.event_listener_list, listener);
+                       DbgFree(listener);
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+static inline void clear_all_listener_list(void)
+{
+       struct event_listener *listener;
+       enum event_state next_state;
+       struct event_data event_data;
+       struct event_data *p_event_data;
+       Eina_List *l;
+       Eina_List *n;
+
+       DbgPrint("event listeners: %d\n", eina_list_count(s_info.event_listener_list));
+
+       if (s_info.event_data.x == -1 || s_info.event_data.y == -1) {
+               memcpy(&s_info.event_data, &s_info.skipped_event_data, sizeof(s_info.event_data));
+               DbgPrint("Use skipped event: %dx%d\n", s_info.skipped_event_data.x, s_info.skipped_event_data.y);
+       }
+
+       while (s_info.event_listener_list) {
+               EINA_LIST_FOREACH_SAFE(s_info.event_listener_list, l, n, listener) {
+
+                       DbgPrint("listener[%p] prev[%x] state[%x]\n", listener, listener->prev_state, listener->state);
+
+                       switch (listener->state) {
+                       case EVENT_STATE_ACTIVATE:
+                               p_event_data = &s_info.event_data;
+                               next_state = EVENT_STATE_ACTIVATED;
+                               break;
+                       case EVENT_STATE_ACTIVATED:
+                               p_event_data = &s_info.event_data;
+                               next_state = EVENT_STATE_DEACTIVATE;
+                               break;
+                       case EVENT_STATE_DEACTIVATE:
+                               memcpy(&event_data, &s_info.event_data, sizeof(event_data));
+                               p_event_data = &event_data;
+
+                               if (listener->prev_state == EVENT_STATE_ACTIVATE) {
+                                       /* There is no move event. we have to emulate it */
+                                       DbgPrint ("Let's emulate move event (%dx%d)\n", p_event_data->x, p_event_data->y);
+                                       listener->state = EVENT_STATE_ACTIVATE;
+                                       next_state = EVENT_STATE_ACTIVATED;
+                               } else {
+                                       next_state = EVENT_STATE_DEACTIVATED;
+                               }
+                               break;
+                       case EVENT_STATE_DEACTIVATED:
+                       default:
+                               s_info.event_listener_list = eina_list_remove(s_info.event_listener_list, listener);
+                               DbgFree(listener);
+                               continue;
+                       }
+
+                       if (invoke_event_cb(listener, p_event_data)) {
+                               continue;
+                       }
+
+                       /*!
+                        * Changing state of listener will affect to the event collecting thread.
+                        */
+                       listener->prev_state = listener->state;
+                       listener->state = next_state;
+               }
+       }
+}
+
+static int compare_timestamp(struct event_listener *listener, struct event_data *item)
+{
+       int ret;
+       if (listener->tv > item->tv) {
+               ret = 1;
+       } else if (listener->tv < item->tv) {
+               ret = -1;
+       } else {
+               ret = 0;
+       }
+       return ret;
+}
+
+static Eina_Bool event_read_cb(void *data, Ecore_Fd_Handler *handler)
+{
+       int fd;
+       struct event_data *item;
+       char event_ch;
+       struct event_listener *listener;
+       Eina_List *l;
+       Eina_List *n;
+       enum event_state next_state;
+       int state;
+
+       fd = ecore_main_fd_handler_fd_get(handler);
+       if (fd < 0) {
+               ErrPrint("Invalid fd\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (read(fd, &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+               ErrPrint("read: %d\n", errno);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (event_ch == EVENT_EXIT) {
+               /*!
+                * If the master gets event exit from evt_pipe,
+                * The event item list should be empty.
+                */
+               if (!s_info.event_list) {
+                       /* This callback must has to clear all listeners in this case */
+                       ecore_main_fd_handler_del(s_info.event_handler);
+                       s_info.event_handler = NULL;
+                       clear_all_listener_list();
+
+                       EINA_LIST_FREE(s_info.reactivate_list, listener) {
+                               s_info.event_listener_list = eina_list_append(s_info.event_listener_list, listener);
+                       }
+                       DbgPrint("Reactivate: %p\n", s_info.event_listener_list);
+
+                       if (s_info.event_listener_list) {
+                               if (event_activate_thread(EVENT_HANDLER_ACTIVATED_BY_MOUSE_SET) < 0) {
+                                       EINA_LIST_FREE(s_info.event_listener_list, listener) {
+                                               (void)listener->event_cb(EVENT_STATE_ERROR, NULL, listener->cbdata);
+                                       }
+                               }
+                       }
+
+                       DbgPrint("Event read callback finshed (%p)\n", s_info.event_listener_list);
+                       return ECORE_CALLBACK_CANCEL;
+               } else {
+                       ErrPrint("Something goes wrong, the event_list is not flushed\n");
+               }
+       }
+
+       CRITICAL_SECTION_BEGIN(&s_info.event_list_lock);
+       item = eina_list_nth(s_info.event_list, 0);
+       if (item) {
+               s_info.event_list = eina_list_remove(s_info.event_list, item);
+       }
+       CRITICAL_SECTION_END(&s_info.event_list_lock);
+
+       if (!item) {
+               ErrPrint("There is no remained event\n");
+               return ECORE_CALLBACK_RENEW;
+       }
+
+       EINA_LIST_FOREACH_SAFE(s_info.event_listener_list, l, n, listener) {
+               switch (listener->state) {
+               case EVENT_STATE_ACTIVATE:
+                       if (compare_timestamp(listener, item) > 0) {
+                               memcpy(&s_info.skipped_event_data, item, sizeof(s_info.skipped_event_data));
+                               continue;
+                       }
+
+                       next_state = EVENT_STATE_ACTIVATED;
+                       break;
+               case EVENT_STATE_DEACTIVATE:
+                       if (compare_timestamp(listener, item) < 0) {
+                               /* Consuming all events occurred while activating this listener */
+                               state = listener->prev_state;
+                               listener->prev_state = listener->state;
+
+                               if (state == EVENT_STATE_ACTIVATE) {
+                                       /**
+                                        * @note
+                                        * If the previous state is ACTIVATE
+                                        * We should create DOWN event first.
+                                        */
+                                       listener->state = EVENT_STATE_ACTIVATE;
+                               } else {
+                                       listener->state = EVENT_STATE_ACTIVATED;
+                               }
+
+                               if (invoke_event_cb(listener, item) == 1) {
+                                       /* listener is deleted */
+                                       continue;
+                               }
+
+                               if (listener->state == EVENT_STATE_ACTIVATE) {
+                                       /**
+                                        * We are already jumped into the DEACTIVATE state
+                                        * But the state is ACTIVATE, it means, we forcely chnaged it from LINE 747
+                                        * We successfully sent the DOWN event.
+                                        * We only need to send UP (or move) event.
+                                        * So change the previous states to ACTIVATED and then change current states to
+                                        * DEACTIVATE again.
+                                        */
+                                       listener->prev_state = EVENT_STATE_ACTIVATED;
+                               } else {
+                                       listener->prev_state = listener->state;
+                               }
+
+                               listener->state = EVENT_STATE_DEACTIVATE;
+                       } else {
+                               memcpy(&s_info.skipped_event_data, item, sizeof(s_info.skipped_event_data));
+                       }
+
+                       /* Do not terminate this listener, until this mets EVENT_EXIT */
+                       continue;
+               case EVENT_STATE_ACTIVATED:
+                       if (compare_timestamp(listener, item) > 0) {
+                               DbgPrint("Drop event (%lf > %lf)\n", listener->tv, item->tv);
+                               memcpy(&s_info.skipped_event_data, item, sizeof(s_info.skipped_event_data));
+                               continue;
+                       }
+                       next_state = listener->state;
+                       break;
+               case EVENT_STATE_DEACTIVATED:
+               default:
+                       /* Remove this from the list */
+                       /* Check the item again. the listener can be deleted from the callback */
+                       if (eina_list_data_find(s_info.event_listener_list, listener)) {
+                               s_info.event_listener_list = eina_list_remove(s_info.event_listener_list, listener);
+                               DbgFree(listener);
+                       }
+
+                       continue;
+               }
+
+               if (invoke_event_cb(listener, item) == 1) {
+                       continue;
+               }
+
+               listener->prev_state = listener->state;
+               listener->state = next_state;
+       }
+
+       DbgFree(item);
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static int event_control_init(void)
+{
+       int status;
+       unsigned int clockId = CLOCK_MONOTONIC;
+
+       DbgPrint("Initializing event controller\n");
+       if (s_info.handle != -1) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       s_info.handle = open(WIDGET_CONF_INPUT_PATH, O_RDONLY);
+       if (s_info.handle < 0) {
+               ErrPrint("open: %d\n", errno);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (fcntl(s_info.handle, F_SETFD, FD_CLOEXEC) < 0) {
+               ErrPrint("fcntl: %d\n", errno);
+       }
+
+       if (fcntl(s_info.handle, F_SETFL, O_NONBLOCK) < 0) {
+               ErrPrint("fcntl: %d\n", errno);
+       }
+
+       if (WIDGET_CONF_USE_EVENT_TIME && !WIDGET_CONF_USE_GETTIMEOFDAY) {
+               DbgPrint("Change timestamp to monotonic\n");
+               if (ioctl(s_info.handle, EVIOCSCLOCKID, &clockId) < 0) {
+                       ErrPrint("ioctl: %d\n", errno);
+               }
+       }
+
+       status = pipe2(s_info.evt_pipe, O_CLOEXEC);
+       if (status < 0) {
+               ErrPrint("pipe2: %d\n", errno);
+               if (close(s_info.handle) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               s_info.handle = -1;
+               return WIDGET_ERROR_FAULT;
+       }
+
+       status = pipe2(s_info.tcb_pipe, O_CLOEXEC);
+       if (status < 0) {
+               ErrPrint("pipe2: %d\n", errno);
+               if (close(s_info.handle) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               s_info.handle = -1;
+               CLOSE_PIPE(s_info.evt_pipe);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/*!
+ * This function must has to be called after event collecting thread is terminated
+ */
+static int event_control_fini(void)
+{
+       DbgPrint("Finalizing event controller\n");
+       if (s_info.handle != -1) {
+               if (close(s_info.handle) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+
+               s_info.handle = -1;
+       }
+
+       if (!eina_list_count(s_info.event_list)) {
+               if (s_info.event_handler) {
+                       ecore_main_fd_handler_del(s_info.event_handler);
+                       s_info.event_handler = NULL;
+               }
+               clear_all_listener_list();
+       }
+
+       CLOSE_PIPE(s_info.tcb_pipe);
+       CLOSE_PIPE(s_info.evt_pipe);
+
+       return WIDGET_ERROR_NONE;
+}
+
+int event_activate_thread(enum event_handler_activate_type activate_type)
+{
+       int ret;
+
+       ret = event_control_init();
+       if (ret != WIDGET_ERROR_NONE) {
+               return ret;
+       }
+
+       if (s_info.event_handler) {
+               ErrPrint("Event handler is already registered\n");
+               return WIDGET_ERROR_ALREADY_EXIST;
+       }
+
+       s_info.event_handler = ecore_main_fd_handler_add(s_info.evt_pipe[PIPE_READ], ECORE_FD_READ, event_read_cb, NULL, NULL, NULL);
+       if (!s_info.event_handler) {
+               ErrPrint("Failed to add monitor for EVT READ\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = pthread_create(&s_info.tid, NULL, event_thread_main, NULL);
+       if (ret != 0) {
+               ErrPrint("pthread_create: %d\n", ret);
+               ecore_main_fd_handler_del(s_info.event_handler);
+               s_info.event_handler = NULL;
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Event handler activated\n");
+       s_info.event_handler_activated = activate_type;
+       return WIDGET_ERROR_NONE;
+}
+
+int event_deactivate_thread(enum event_handler_activate_type activate_type)
+{
+       int status;
+       void *ret;
+       char event_ch = EVENT_CH;
+
+       if (s_info.event_handler_activated != activate_type) {
+               ErrPrint("Event handler activate type is mismatched\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       /* Terminating thread */
+       if (write(s_info.tcb_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
+               ErrPrint("write: %d\n", errno);
+       }
+
+       status = pthread_join(s_info.tid, &ret);
+       if (status != 0) {
+               ErrPrint("pthread_join: %d\n", status);
+       } else {
+               DbgPrint("Thread returns: %p\n", ret);
+       }
+
+       s_info.event_handler_activated = EVENT_HANDLER_DEACTIVATED;
+       return WIDGET_ERROR_NONE;
+}
+
+/*!
+ * x, y is the starting point.
+ */
+HAPI int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
+{
+       struct event_listener *listener;
+       int ret = WIDGET_ERROR_NONE;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(s_info.event_listener_list, l, listener) {
+               if (listener->event_cb == event_cb && listener->cbdata == data) {
+                       ErrPrint("Already registered\n");
+                       return WIDGET_ERROR_ALREADY_EXIST;
+               }
+       }
+
+       listener = malloc(sizeof(*listener));
+       if (!listener) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       listener->tv = current_time_get() - DELAY_COMPENSATOR; // Let's use the previous event.
+       DbgPrint("Activated at: %lf (%dx%d)\n", listener->tv, x, y);
+
+       listener->event_cb = event_cb;
+       listener->cbdata = data;
+       listener->prev_state = EVENT_STATE_DEACTIVATED;
+       listener->state = EVENT_STATE_ACTIVATE;
+       listener->x = x;
+       listener->y = y;
+
+       if (s_info.event_handler_activated == EVENT_HANDLER_DEACTIVATED) {
+               /*!
+                * \note
+                * We don't need to lock to access event_list here.
+                * If the _sinfo.handle is greater than 0, the event_list will not be touched.
+                * But if the s_info.handle is less than 0, it means, there is no thread,
+                * so we can access the event_list without lock.
+                */
+               if (s_info.event_list) {
+                       DbgPrint("Event thread is deactivating now. activating will be delayed\n");
+                       s_info.reactivate_list = eina_list_append(s_info.reactivate_list, listener);
+               } else {
+                       s_info.event_listener_list = eina_list_append(s_info.event_listener_list, listener);
+
+                       if ((ret = event_activate_thread(EVENT_HANDLER_ACTIVATED_BY_MOUSE_SET)) < 0) {
+                               s_info.event_listener_list = eina_list_remove(s_info.event_listener_list, listener);
+                               DbgFree(listener);
+                       }
+               }
+       } else {
+               s_info.event_listener_list = eina_list_append(s_info.event_listener_list, listener);
+       }
+
+       return ret;
+}
+
+HAPI int event_input_fd(void)
+{
+       event_control_init();
+       DbgPrint("Input event handler: %d\n", s_info.handle);
+       return s_info.handle;
+}
+
+HAPI int event_deactivate(int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
+{
+       struct event_listener *listener = NULL;
+       struct event_listener *item;
+       Eina_List *l;
+       int keep_thread = 0;
+
+       EINA_LIST_FOREACH(s_info.event_listener_list, l, item) {
+               if (item->event_cb == event_cb && item->cbdata == data) {
+                       switch (item->state) {
+                       case EVENT_STATE_ACTIVATE:
+                       case EVENT_STATE_ACTIVATED:
+                               item->prev_state = item->state;
+                               item->state = EVENT_STATE_DEACTIVATE;
+                               listener = item;
+                               break;
+                       default:
+                               /* Item is already deactivated */
+                               break;
+                       }
+               }
+
+               keep_thread += (item->state == EVENT_STATE_ACTIVATE || item->state == EVENT_STATE_ACTIVATED);
+       }
+
+       if (!listener) {
+               ErrPrint("Listener is not registered or already deactivated\n");
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       if (s_info.event_handler_activated == EVENT_HANDLER_DEACTIVATED) {
+               ErrPrint("Event handler is not actiavated\n");
+               s_info.event_listener_list = eina_list_remove(s_info.event_listener_list, listener);
+               DbgFree(listener);
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (keep_thread) {
+               DbgPrint("Keep thread\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       event_deactivate_thread(EVENT_HANDLER_ACTIVATED_BY_MOUSE_SET);
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int event_reset_cbdata(int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data, void *new_data)
+{
+       struct event_listener *item;
+       Eina_List *l;
+       int updated = 0;
+
+       EINA_LIST_FOREACH(s_info.event_listener_list, l, item) {
+               if (item->event_cb == event_cb && item->cbdata == data) {
+                       item->cbdata = new_data;
+                       updated++;
+               }
+       }
+
+       EINA_LIST_FOREACH(s_info.reactivate_list, l, item) {
+               if (item->event_cb == event_cb && item->cbdata == data) {
+                       item->cbdata = new_data;
+                       updated++;
+               }
+       }
+
+       return updated;
+}
+
+HAPI int event_is_activated(void)
+{
+       return s_info.handle >= 0;
+}
+
+HAPI void event_set_mouse_xy(int x, int y, double timestamp)
+{
+       s_info.event_data.x = x;
+       s_info.event_data.y = y;
+       s_info.event_data.tv = timestamp;
+       /**
+        * Don't touch the timestamp_updated variable.
+        * if we toggle it, the input thread will not be terminated correctly. SEE LINE: 537
+       if (!s_info.timestamp_updated) {
+               // NOP
+       }
+        */
+}
+
+/* End of a file */
diff --git a/src/fault_manager.c b/src/fault_manager.c
new file mode 100644 (file)
index 0000000..1728ff6
--- /dev/null
@@ -0,0 +1,377 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h> /* free */
+
+#include <gio/gio.h>
+
+#include <Eina.h>
+#include <packet.h>
+#include <dlog.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+#include <widget_cmd_list.h>
+
+#include "util.h"
+#include "debug.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "instance.h"
+#include "client_rpc.h"
+#include "package.h"
+#include "conf.h"
+#include "critical_log.h"
+
+static struct info {
+       Eina_List *call_list;
+       int fault_mark_count;
+} s_info = {
+       .call_list = NULL,
+       .fault_mark_count = 0,
+};
+
+struct fault_info {
+       struct slave_node *slave;
+       double timestamp;
+       char *pkgname;
+       char *filename;
+       char *func;
+};
+
+HAPI int const fault_is_occured(void)
+{
+       return s_info.fault_mark_count;
+}
+
+static void clear_log_file(struct slave_node *slave)
+{
+       char filename[BUFSIZ];
+       int ret;
+
+       ret = snprintf(filename, sizeof(filename) - 1, "%s/slave.%d", WIDGET_CONF_LOG_PATH, slave_pid(slave));
+       if (ret == sizeof(filename) - 1) {
+               filename[sizeof(filename) - 1] = '\0';
+               ErrPrint("filename buffer is overflowed\n");
+       }
+
+       if (unlink(filename) < 0) {
+               ErrPrint("unlink: %d\n", errno);
+       }
+}
+
+static char *check_log_file(struct slave_node *slave)
+{
+       char libexec[BUFSIZ];
+       char *ptr;
+       FILE *fp;
+       char filename[BUFSIZ];
+
+       snprintf(filename, sizeof(filename), "%s/slave.%d", WIDGET_CONF_LOG_PATH, slave_pid(slave));
+       fp = fopen(filename, "rt");
+       if (!fp) {
+               ErrPrint("fopen [%d]\n", errno);
+               return NULL;
+       }
+
+       ptr = fgets(libexec, sizeof(libexec), fp);
+       if (fclose(fp) != 0) {
+               ErrPrint("fclose: %d\n", errno);
+       }
+
+       if (ptr != libexec) {
+               ErrPrint("Invalid log\n");
+               return NULL;
+       }
+
+       if (unlink(filename) < 0) {
+               ErrPrint("Failed to unlink %s\n", filename);
+       }
+
+       ptr = widget_service_get_widget_id_by_libexec(libexec);
+       if (!ptr) {
+               ErrPrint("Failed to find the faulted package\n");
+       }
+
+       DbgPrint("Faulted package: %s\n", ptr);
+       return ptr;
+}
+
+HAPI void fault_unicast_info(struct client_node *client, const char *pkgname, const char *filename, const char *func)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_FAULT_PACKAGE;
+
+       if (!client || !pkgname || !filename || !func) {
+               return;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sss", pkgname, filename, func);
+       if (!packet) {
+               return;
+       }
+
+       client_rpc_async_request(client, packet);
+}
+
+HAPI void fault_broadcast_info(const char *pkgname, const char *filename, const char *func)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_FAULT_PACKAGE;
+
+       packet = packet_create_noack((const char *)&cmd, "sss", pkgname, filename, func);
+       if (!packet) {
+               ErrPrint("Failed to create a param\n");
+               return;
+       }
+
+       client_broadcast(NULL, packet);
+}
+
+static inline void dump_fault_info(const char *name, pid_t pid, const char *pkgname, const char *filename, const char *funcname)
+{
+       CRITICAL_LOG("Slavename: %s[%d]\n"      \
+                       "Package: %s\n"         \
+                       "Filename: %s\n"        \
+                       "Funcname: %s\n", name, pid, pkgname, filename, funcname);
+}
+
+HAPI int fault_info_set(struct slave_node *slave, const char *pkgname, const char *id, const char *func)
+{
+       struct pkg_info *pkg;
+       int ret;
+
+       pkg = package_find(pkgname);
+       if (!pkg) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       ret = package_set_fault_info(pkg, util_timestamp(), id, func);
+       if (ret < 0) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       dump_fault_info(slave_name(slave), slave_pid(slave), pkgname, id, func);
+       ErrPrint("Set fault %s(%d)\n", !ret ? "Success" : "Failed", ret);
+       fault_broadcast_info(pkgname, id, func);
+
+       /*!
+        * \note
+        * Update statistics
+        */
+       s_info.fault_mark_count++;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int fault_check_pkgs(struct slave_node *slave)
+{
+       struct fault_info *info;
+       struct pkg_info *pkg;
+       const char *pkgname;
+       Eina_List *l;
+       Eina_List *n;
+       int checked;
+
+       /*!
+        * \note
+        * First step.
+        * Check the log file
+        */
+       pkgname = (const char *)check_log_file(slave);
+       if (pkgname) {
+               pkg = package_find(pkgname);
+               if (pkg) {
+                       if (package_set_fault_info(pkg, util_timestamp(), NULL, NULL) != WIDGET_ERROR_CANCELED) {
+                               dump_fault_info(slave_name(slave), slave_pid(slave), pkgname, "", "");
+                               fault_broadcast_info(pkgname, "", "");
+                       }
+                       DbgFree((char *)pkgname);
+
+                       s_info.fault_mark_count = 0;
+                       clear_log_file(slave);
+                       EINA_LIST_REVERSE_FOREACH_SAFE(s_info.call_list, l, n, info) {
+                               if (info->slave != slave) {
+                                       continue;
+                               }
+
+                               s_info.call_list = eina_list_remove_list(s_info.call_list, l);
+
+                               DbgFree(info->pkgname);
+                               DbgFree(info->filename);
+                               DbgFree(info->func);
+                               DbgFree(info);
+                       }
+                       return 0;
+               }
+               DbgFree((char *)pkgname);
+       }
+
+       /*!
+        * \note
+        * Second step.
+        * Is it secured slave?
+        */
+       pkgname = package_find_by_secured_slave(slave);
+       if (pkgname) {
+               pkg = package_find(pkgname);
+               if (pkg) {
+                       if (package_set_fault_info(pkg, util_timestamp(), NULL, NULL) != WIDGET_ERROR_CANCELED) {
+                               dump_fault_info(slave_name(slave), slave_pid(slave), pkgname, "", "");
+                               fault_broadcast_info(pkgname, "", "");
+                       }
+
+                       s_info.fault_mark_count = 0;
+                       clear_log_file(slave);
+                       EINA_LIST_REVERSE_FOREACH_SAFE(s_info.call_list, l, n, info) {
+                               if (info->slave != slave) {
+                                       continue;
+                               }
+
+                               s_info.call_list = eina_list_remove_list(s_info.call_list, l);
+
+                               DbgFree(info->pkgname);
+                               DbgFree(info->filename);
+                               DbgFree(info->func);
+                               DbgFree(info);
+                       }
+
+                       return 0;
+               }
+       }
+
+       /*!
+        * \note
+        * At last, check the pair of function call and return mark
+        */
+       checked = 0;
+       EINA_LIST_REVERSE_FOREACH_SAFE(s_info.call_list, l, n, info) {
+               if (info->slave == slave) {
+                       const char *filename;
+                       const char *func;
+
+                       pkg = package_find(info->pkgname);
+                       if (!pkg) {
+                               ErrPrint("Failed to find a package %s\n", info->pkgname);
+                               continue;
+                       }
+
+                       filename = info->filename ? info->filename : "";
+                       func = info->func ? info->func : "";
+
+                       if (!checked) {
+                               if (package_set_fault_info(pkg, info->timestamp, info->filename, info->func) != WIDGET_ERROR_CANCELED) {
+                                       fault_broadcast_info(info->pkgname, info->filename, info->func);
+                               }
+                       } else {
+                               DbgPrint("Treated as a false log\n");
+                               dump_fault_info(slave_name(info->slave), slave_pid(info->slave), info->pkgname, filename, func);
+                       }
+
+                       s_info.call_list = eina_list_remove_list(s_info.call_list, l);
+
+                       DbgFree(info->pkgname);
+                       DbgFree(info->filename);
+                       DbgFree(info->func);
+                       DbgFree(info);
+                       checked = 1;
+               }
+       }
+
+       s_info.fault_mark_count = 0;
+       clear_log_file(slave);
+       return 0;
+}
+
+HAPI int fault_func_call(struct slave_node *slave, const char *pkgname, const char *filename, const char *func)
+{
+       struct fault_info *info;
+
+       info = malloc(sizeof(*info));
+       if (!info) {
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->slave = slave;
+
+       info->pkgname = strdup(pkgname);
+       if (!info->pkgname) {
+               DbgFree(info);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->filename = strdup(filename);
+       if (!info->filename) {
+               DbgFree(info->pkgname);
+               DbgFree(info);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->func = strdup(func);
+       if (!info->func) {
+               DbgFree(info->filename);
+               DbgFree(info->pkgname);
+               DbgFree(info);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->timestamp = util_timestamp();
+
+       s_info.call_list = eina_list_append(s_info.call_list, info);
+
+       s_info.fault_mark_count++;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int fault_func_ret(struct slave_node *slave, const char *pkgname, const char *filename, const char *func)
+{
+       struct fault_info *info;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(s_info.call_list, l, info) {
+               if (info->slave != slave) {
+                       continue;
+               }
+
+               if (strcmp(info->pkgname, pkgname)) {
+                       continue;
+               }
+
+               if (strcmp(info->filename, filename)) {
+                       continue;
+               }
+
+               if (strcmp(info->func, func)) {
+                       continue;
+               }
+
+               s_info.call_list = eina_list_remove_list(s_info.call_list, l);
+               DbgFree(info->filename);
+               DbgFree(info->pkgname);
+               DbgFree(info->func);
+               DbgFree(info);
+
+               s_info.fault_mark_count--;
+               return 0;
+       } 
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+/* End of a file */
diff --git a/src/file_service.c b/src/file_service.c
new file mode 100644 (file)
index 0000000..7b1ed9f
--- /dev/null
@@ -0,0 +1,720 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <pthread.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <packet.h>
+#include <com-core.h>
+
+#include "file_service.h"
+#include "service_common.h"
+#include "debug.h"
+#include "util.h"
+#include "conf.h"
+#include "buffer_handler.h"
+
+#define FILE_SERVICE_ADDR      "remote://:8209"
+
+#define PUSH_EXIT      'e'
+#define PUSH_ITEM      'i'
+
+#define PKT_CHUNKSZ    4096
+
+static struct info {
+       struct service_context *svc_ctx;
+
+       pthread_t push_thid;
+
+       Eina_List *request_list;
+       pthread_mutex_t request_list_lock;
+
+       int request_pipe[PIPE_MAX];
+} s_info = {
+       .svc_ctx = NULL,
+       .request_list = NULL,
+       .request_list_lock = PTHREAD_MUTEX_INITIALIZER,
+       .request_pipe = { 0, },
+};
+
+struct request_item {
+       enum {
+               REQUEST_TYPE_FILE = 0x00,
+               REQUEST_TYPE_SHM = 0x01,
+               REQUEST_TYPE_PIXMAP = 0x02,
+               REQUEST_TYPE_MAX = 0x02,
+       } type;
+       union {
+               char *filename;
+               int shm;
+               unsigned int pixmap;
+       } data;
+       struct tcb *tcb;
+};
+
+typedef int (*send_data_func_t)(int fd, const struct request_item *item);
+
+/*!
+ * File transfer header.
+ * This must should be shared with client.
+ */
+struct burst_head {
+       off_t size;
+       int flen;
+       char fname[];
+};
+
+struct burst_data {
+       int size;
+       char data[];
+};
+
+static inline struct request_item *create_request_item(struct tcb *tcb, int type, void *data)
+{
+       struct request_item *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       switch (type) {
+       case REQUEST_TYPE_FILE:
+               item->data.filename = strdup(data);
+               if (!item->data.filename) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(item);
+                       return NULL;
+               }
+               break;
+       case REQUEST_TYPE_PIXMAP:
+               item->data.pixmap = (unsigned int)data;
+               break;
+       case REQUEST_TYPE_SHM:
+               item->data.shm = (int)data;
+               break;
+       default:
+               ErrPrint("Invalid type of request\n");
+               DbgFree(item);
+               return NULL;
+       }
+
+       item->type = type;
+       item->tcb = tcb;
+       return item;
+}
+
+static inline int destroy_request_item(struct request_item *item)
+{
+       switch (item->type) {
+       case REQUEST_TYPE_FILE:
+               DbgFree(item->data.filename);
+               break;
+       case REQUEST_TYPE_SHM:
+       case REQUEST_TYPE_PIXMAP:
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgFree(item);
+       return WIDGET_ERROR_NONE;
+}
+
+static int request_file_handler(struct tcb *tcb, struct packet *packet, struct request_item **item)
+{
+       const char *filename;
+
+       if (packet_get(packet, "s", &filename) != 1) {
+               ErrPrint("Invalid packet\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       *item = create_request_item(tcb, REQUEST_TYPE_FILE, (void *)filename);
+       return *item ? WIDGET_ERROR_NONE : WIDGET_ERROR_OUT_OF_MEMORY;
+}
+
+static int request_pixmap_handler(struct tcb *tcb, struct packet *packet, struct request_item **item)
+{
+       unsigned int pixmap;
+
+       if (packet_get(packet, "i", &pixmap) != 1) {
+               ErrPrint("Invalid packet\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (pixmap == 0) {
+               ErrPrint("pixmap is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       /*!
+        * \TODO
+        * Attach to pixmap and copy its data to the client
+        */
+       *item = create_request_item(tcb, REQUEST_TYPE_PIXMAP, (void *)pixmap);
+       return *item ? WIDGET_ERROR_NONE : WIDGET_ERROR_OUT_OF_MEMORY;
+}
+
+static int request_shm_handler(struct tcb *tcb, struct packet *packet, struct request_item **item)
+{
+       int shm;
+
+       if (packet_get(packet, "i", &shm) != 1) {
+               ErrPrint("Invalid packet\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (shm < 0) {
+               ErrPrint("shm is not valid: %d\n", shm);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       /*!
+        * \TODO
+        * Attach to SHM and copy its buffer to the client
+        */
+       *item = create_request_item(tcb, REQUEST_TYPE_SHM, (void *)shm);
+       return *item ? WIDGET_ERROR_NONE : WIDGET_ERROR_OUT_OF_MEMORY;
+}
+
+/* SERVER THREAD */
+static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
+{
+       const char *cmd;
+       char ch = PUSH_ITEM;
+       int ret;
+       int i;
+       struct request_item *item;
+       struct packet *reply;
+       struct {
+               const char *cmd;
+               int (*request_handler)(struct tcb *tcb, struct packet *packet, struct request_item **item);
+       } cmd_table[] = {
+               {
+                       .cmd = "request,file",
+                       .request_handler = request_file_handler,
+               },
+               {
+                       .cmd = "request,pixmap",
+                       .request_handler = request_pixmap_handler,
+               },
+               {
+                       .cmd = "request,shm",
+                       .request_handler = request_shm_handler,
+               },
+               {
+                       .cmd = NULL,
+                       .request_handler = NULL,
+               },
+       };
+
+       if (!packet) {
+               DbgPrint("TCB %p is disconnected\n", tcb);
+               return WIDGET_ERROR_NONE;
+       }
+
+       cmd = packet_command(packet);
+       if (!cmd) {
+               ErrPrint("Invalid packet. cmd is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (packet_type(packet)) {
+       case PACKET_REQ:
+               for (i = 0; cmd_table[i].cmd; i++) {
+                       /*!
+                        * Protocol sequence
+                        * FILE REQUEST COMMAND (Client -> Server)
+                        * REPLY FOR REQUEST (Client <- Server)
+                        * PUSH FILE (Client <- Server)
+                        *
+                        * Client & Server must has to keep this communication sequence.
+                        */
+                       if (strcmp(cmd, cmd_table[i].cmd)) {
+                               continue;
+                       }
+
+                       item = NULL;
+                       ret = cmd_table[i].request_handler(tcb, packet, &item);
+
+                       reply = packet_create_reply(packet, "i", ret);
+                       if (!reply) {
+                               ErrPrint("Failed to create a reply packet\n");
+                               break;
+                       }
+
+                       if (service_common_unicast_packet(tcb, reply) < 0) {
+                               ErrPrint("Unable to send reply packet\n");
+                       }
+
+                       packet_destroy(reply);
+
+                       /*!
+                        * \note
+                        * After send the reply packet, file push thread can sending a file
+                        */
+                       if (ret != WIDGET_ERROR_NONE || !item) {
+                               break;
+                       }
+
+                       CRITICAL_SECTION_BEGIN(&s_info.request_list_lock);
+                       s_info.request_list = eina_list_append(s_info.request_list, item);
+                       CRITICAL_SECTION_END(&s_info.request_list_lock);
+
+                       ret = write(s_info.request_pipe[PIPE_WRITE], &ch, sizeof(ch));
+                       if (ret < 0) {
+                               ErrPrint("write: %d\n", errno);
+
+                               CRITICAL_SECTION_BEGIN(&s_info.request_list_lock);
+                               s_info.request_list = eina_list_remove(s_info.request_list, item);
+                               CRITICAL_SECTION_END(&s_info.request_list_lock);
+
+                               destroy_request_item(item);
+                               /*!
+                                * \note for the client
+                                * In this case, the client can waiting files forever.
+                                * So the client must has to wait only a few seconds.
+                                * If the client could not get the any data in that time,
+                                * it should cancel the waiting.
+                                */
+                       }
+               }
+
+               break;
+       case PACKET_REQ_NOACK:
+       case PACKET_ACK:
+               /* File service has no this case, it is passive service type */
+               ErrPrint("Invalid packet.\n");
+               break;
+       default:
+               break;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static int send_file(int handle, const struct request_item *item)
+{
+       struct burst_head *head;
+       struct burst_data *body;
+       int pktsz;
+       int flen;
+       off_t fsize;
+       int fd;
+       int ret = 0;
+
+       /* TODO: push a file to the client */
+       fd = open(item->data.filename, O_RDONLY);
+       if (fd < 0) {
+               ErrPrint("open: %d\n", errno);
+               return -EIO;
+       }
+
+       flen = strlen(item->data.filename);
+       if (flen == 0) {
+               ret = -EINVAL;
+               goto errout;
+       }
+
+       pktsz = sizeof(*head) + flen + 1;
+
+       head = malloc(pktsz);
+       if (!head) {
+               ErrPrint("malloc: %d\n", errno);
+               ret = -ENOMEM;
+               goto errout;
+       }
+
+       fsize = lseek(fd, 0L, SEEK_END);
+       if (fsize == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+               DbgFree(head);
+               ret = -EIO;
+               goto errout;
+       }
+
+       head->flen = flen;
+       head->size = fsize;
+       strcpy(head->fname, item->data.filename);
+
+       /* Anytime we can fail to send packet */
+       ret = com_core_send(handle, (void *)head, pktsz, 2.0f);
+       DbgFree(head);
+       if (ret < 0) {
+               ret = -EFAULT;
+               goto errout;
+       }
+
+       if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
+               ErrPrint("lseek: %s\n", errno);
+
+               body = malloc(sizeof(*body));
+               if (!body) {
+                       ErrPrint("malloc: %d\n", errno);
+                       return -ENOMEM;
+               }
+
+               body->size = -1;
+               ret = com_core_send(handle, (void *)body, sizeof(*body), 2.0f);
+               DbgFree(body);
+
+               if (ret < 0) {
+                       ret = -EFAULT;
+               } else {
+                       ret = -EIO;
+               }
+
+               goto errout;
+       }
+
+       body = malloc(PKT_CHUNKSZ + sizeof(*body));
+       if (!body) {
+               ErrPrint("malloc: %d\n", errno);
+               goto errout;
+       }
+
+       /* Burst pushing. */
+       while (fsize > 0) {
+               if (fsize > PKT_CHUNKSZ) {
+                       body->size = PKT_CHUNKSZ;
+               } else {
+                       body->size = fsize;
+               }
+
+               ret = read(fd, body->data, body->size); 
+               if (ret < 0) {
+                       ErrPrint("read: %d\n", errno);
+                       ret = -EIO;
+                       break;
+               }
+
+               body->size = ret;
+               fsize -= ret;
+               pktsz = sizeof(*body) + body->size;
+
+               /* Send BODY */
+               ret = com_core_send(handle, (void *)body, pktsz, 2.0f);
+               if (ret != pktsz) {
+                       ret = -EFAULT;
+                       break;
+               }
+       }
+
+       /* Send EOF */
+       body->size = -1;
+       ret = com_core_send(handle, (void *)body, sizeof(*body), 2.0f);
+       if (ret < 0) {
+               ret = -EFAULT;
+       }
+
+       DbgFree(body);
+
+errout:
+       if (close(fd) < 0) {
+               ErrPrint("close: %d\n", errno);
+       }
+
+       return ret;
+}
+
+static int send_buffer(int handle, const struct request_item *item)
+{
+       widget_fb_t buffer;
+       struct burst_head *head;
+       struct burst_data *body;
+       char *data;
+       int pktsz;
+       int ret;
+       int size;
+       int offset;
+       int type;
+
+       if (item->type == REQUEST_TYPE_SHM) {
+               type = WIDGET_FB_TYPE_SHM;
+       } else {
+               type = WIDGET_FB_TYPE_PIXMAP;
+       }
+
+       buffer = buffer_handler_raw_open(type, (void *)item->data.shm);
+       if (!buffer) {
+               return -EINVAL;
+       }
+
+       pktsz = sizeof(*head);
+
+       head = malloc(pktsz);
+       if (!head) {
+               ErrPrint("malloc: %d\n", errno);
+               (void)buffer_handler_raw_close(buffer);
+               return -ENOMEM;
+       }
+
+       size = head->size = buffer_handler_raw_size(buffer);
+       head->flen = 0;
+
+       /* Anytime we can fail to send packet */
+       ret = com_core_send(handle, (void *)head, pktsz, 2.0f);
+       DbgFree(head);
+       if (ret < 0) {
+               ret = -EFAULT;
+               goto errout;
+       }
+
+       body = malloc(sizeof(*body) + PKT_CHUNKSZ);
+       if (!body) {
+               ret = -ENOMEM;
+               goto errout;
+       }
+
+       data = (char *)buffer_handler_raw_data(buffer);
+       offset = 0;
+       while (offset < size) {
+               body->size = size - offset;
+
+               if (body->size > PKT_CHUNKSZ) {
+                       body->size = PKT_CHUNKSZ;
+               }
+
+               memcpy(body->data, data, body->size);
+               pktsz = sizeof(*body) + body->size;
+
+               ret = com_core_send(handle, (void *)body, pktsz, 2.0f);
+               if (ret < 0) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               offset += body->size;
+       }
+
+       DbgFree(body);
+
+errout:
+       (void)buffer_handler_raw_close(buffer);
+       return ret;
+}
+
+static void *push_main(void *data)
+{
+       fd_set set;
+       int ret;
+       char ch;
+       struct request_item *item;
+       int conn_fd;
+       send_data_func_t send_data[] = {
+               send_file,
+               send_buffer,
+               send_buffer,
+       };
+
+       while (1) {
+               FD_ZERO(&set);
+               FD_SET(s_info.request_pipe[PIPE_READ], &set);
+
+               ret = select(s_info.request_pipe[PIPE_READ] + 1, &set, NULL, NULL, NULL);
+               if (ret < 0) {
+                       ret = -errno;
+                       if (errno == EINTR) {
+                               ErrPrint("INTERRUPTED\n");
+                               ret = 0;
+                               continue;
+                       }
+                       ErrPrint("select: %d\n", errno);
+                       break;
+               } else if (ret == 0) {
+                       ErrPrint("Timeout\n");
+                       ret = -ETIMEDOUT;
+                       break;
+               }
+
+               if (!FD_ISSET(s_info.request_pipe[PIPE_READ], &set)) {
+                       DbgPrint("Unknown data\n");
+                       ret = -EINVAL;
+                       break;
+               }
+
+               ret = read(s_info.request_pipe[PIPE_READ], &ch, sizeof(ch));
+               if (ret != sizeof(ch)) {
+                       ErrPrint("read: %d\n", errno);
+                       ret = -EFAULT;
+                       break;
+               }
+
+               if (ch == PUSH_EXIT) {
+                       DbgPrint("Thread is terminating\n");
+                       ret = -ECANCELED;
+                       break;
+               }
+
+               CRITICAL_SECTION_BEGIN(&s_info.request_list_lock);
+               item = eina_list_nth(s_info.request_list, 0);
+               s_info.request_list = eina_list_remove(s_info.request_list, item);
+               CRITICAL_SECTION_END(&s_info.request_list_lock);
+
+               if (!item) {
+                       ErrPrint("Request item is not valid\n");
+                       continue;
+               }
+
+               /* Validate the TCB? */
+               conn_fd = tcb_is_valid(s_info.svc_ctx, item->tcb);
+               if (conn_fd < 0) {
+                       ErrPrint("TCB is not valid\n");
+                       destroy_request_item(item);
+                       continue;
+               }
+
+               /*
+                * \note
+                * From now, we cannot believe the conn_fd.
+                * It can be closed any time.
+                * Even though we using it.
+                */
+               if (item->type < REQUEST_TYPE_MAX && item->type >= 0) {
+                       (void)send_data[item->type](conn_fd, item);
+               } else {
+                       ErrPrint("Invalid type\n");
+               }
+
+               destroy_request_item(item);
+       }
+
+       return (void *)ret;
+}
+
+/* MAIN THREAD */
+int file_service_init(void)
+{
+       int status;
+
+       if (s_info.svc_ctx) {
+               ErrPrint("Already initialized\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       if (pipe2(s_info.request_pipe, O_CLOEXEC) < 0) {
+               ErrPrint("pipe: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       status = pthread_mutex_init(&s_info.request_list_lock, NULL);
+       if (status != 0) {
+               ErrPrint("Failed to create lock: %d\n", status);
+               CLOSE_PIPE(s_info.request_pipe);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       s_info.svc_ctx = service_common_create(FILE_SERVICE_ADDR, NULL, service_thread_main, NULL);
+       if (!s_info.svc_ctx) {
+               ErrPrint("Unable to activate service thread\n");
+
+               status = pthread_mutex_destroy(&s_info.request_list_lock);
+               if (status != 0) {
+                       ErrPrint("Destroy lock: %d\n", status);
+               }
+
+               CLOSE_PIPE(s_info.request_pipe);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       status = pthread_create(&s_info.push_thid, NULL, push_main, NULL);
+       if (status != 0) {
+               ErrPrint("Failed to create a push service: %d\n", status);
+
+               service_common_destroy(s_info.svc_ctx);
+               s_info.svc_ctx = NULL;
+
+               status = pthread_mutex_destroy(&s_info.request_list_lock);
+               if (status != 0) {
+                       ErrPrint("Destroy lock: %d\n", status);
+               }
+
+               CLOSE_PIPE(s_info.request_pipe);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       /*!
+        * \note
+        * Remote service doesn't need to set the additional SMAK label.
+        */
+
+       DbgPrint("Successfully initiated\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* MAIN THREAD */
+int file_service_fini(void)
+{
+       struct request_item *item;
+       int status;
+       char ch;
+       void *retval;
+
+       if (!s_info.svc_ctx) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ch = PUSH_EXIT;
+       status = write(s_info.request_pipe[PIPE_WRITE], &ch, sizeof(ch));
+       if (status != sizeof(ch)) {
+               ErrPrint("write: %d\n", errno);
+               /* Forcely terminate the thread */
+               status = pthread_cancel(s_info.push_thid);
+               if (status != 0) {
+                       ErrPrint("cancel: %d\n", status);
+               }
+       }
+
+       status = pthread_join(s_info.push_thid, &retval);
+       if (status != 0) {
+               ErrPrint("join: %d\n", status);
+       }
+
+       CRITICAL_SECTION_BEGIN(&s_info.request_list_lock);
+       EINA_LIST_FREE(s_info.request_list, item) {
+               destroy_request_item(item);
+       }
+       CRITICAL_SECTION_END(&s_info.request_list_lock);
+
+       service_common_destroy(s_info.svc_ctx);
+       s_info.svc_ctx = NULL;
+
+       status = pthread_mutex_destroy(&s_info.request_list_lock);
+       if (status != 0) {
+               ErrPrint("destroy mutex: %d\n", status);
+       }
+
+       CLOSE_PIPE(s_info.request_pipe);
+
+       DbgPrint("Successfully Finalized\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/group.c b/src/group.c
new file mode 100644 (file)
index 0000000..76b99e6
--- /dev/null
@@ -0,0 +1,889 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h> /* malloc */
+#include <errno.h>
+#include <string.h> /* strdup */
+
+#include <dlog.h>
+#include <Eina.h>
+#include <widget_errno.h>
+
+#include "util.h"
+#include "debug.h"
+#include "group.h"
+#include "conf.h"
+
+int errno;
+
+static struct info {
+       Eina_List *cluster_list;
+} s_info = {
+       .cluster_list = NULL,
+};
+
+struct cluster {
+       char *name;
+       Eina_List *category_list;
+};
+
+struct category {
+       char *name;
+       struct cluster *cluster;
+       Eina_List *info_list; /* list of instances of the struct inst_info */
+};
+
+struct context_info {
+       char *pkgname;
+       struct category *category;
+       Eina_List *context_list; /* context item list */
+};
+
+struct context_item_data {
+       char *tag;
+       void *data;
+};
+
+struct context_item {
+       char *ctx_item;
+       struct context_info *info;
+       Eina_List *option_list;
+       Eina_List *data_list;
+};
+
+struct context_option {
+       struct context_item *item;
+       char *key;
+       char *value;
+};
+
+HAPI struct context_info *group_create_context_info(struct category *category, const char *pkgname)
+{
+       struct context_info *info;
+
+       info = calloc(1, sizeof(*info));
+       if (!info) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       info->pkgname = strdup(pkgname);
+       if (!info->pkgname) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(info);
+               return NULL;
+       }
+
+       info->category = category;
+       category->info_list = eina_list_append(category->info_list, info);
+       return info;
+}
+
+static inline void del_options(struct context_item *item)
+{
+       struct context_option *option;
+
+       EINA_LIST_FREE(item->option_list, option) {
+               DbgFree(option->key);
+               DbgFree(option->value);
+               DbgFree(option);
+       }
+}
+
+static inline void del_context_item(struct context_info *info)
+{
+       struct context_item *item;
+
+       EINA_LIST_FREE(info->context_list, item) {
+               del_options(item);
+               DbgFree(item->ctx_item);
+               DbgFree(item);
+       }
+}
+
+HAPI struct context_item *group_add_context_item(struct context_info *info, const char *ctx_item)
+{
+       struct context_item *item;
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       item->ctx_item = strdup(ctx_item);
+       if (!item->ctx_item) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item);
+               return NULL;
+       }
+
+       item->info = info;
+       info->context_list = eina_list_append(info->context_list, item);
+       return item;
+}
+
+HAPI int group_add_option(struct context_item *item, const char *key, const char *value)
+{
+       struct context_option *option;
+
+       option = calloc(1, sizeof(*option));
+       if (!option) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       option->key = strdup(key);
+       if (!option->key) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(option);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       option->value = strdup(value);
+       if (!option->value) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(option->key);
+               DbgFree(option);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       option->item = item;
+       item->option_list = eina_list_append(item->option_list, option);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int group_destroy_context_info(struct context_info *info)
+{
+       struct category *category;
+
+       category = info->category;
+       if (!category) {
+               ErrPrint("No category found\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       category->info_list = eina_list_remove(category->info_list, info);
+
+       del_context_item(info);
+       DbgFree(info->pkgname);
+       DbgFree(info);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct cluster *group_create_cluster(const char *name)
+{
+       struct cluster *cluster;
+
+       cluster = malloc(sizeof(*cluster));
+       if (!cluster) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       cluster->name = strdup(name);
+       if (!cluster->name) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(cluster);
+               return NULL;
+       }
+
+       cluster->category_list = NULL;
+
+       s_info.cluster_list = eina_list_append(s_info.cluster_list, cluster);
+       return cluster;
+}
+
+HAPI struct cluster *group_find_cluster(const char *name)
+{
+       Eina_List *l;
+       struct cluster *cluster;
+
+       EINA_LIST_FOREACH(s_info.cluster_list, l, cluster) {
+               if (!strcasecmp(cluster->name, name)) {
+                       return cluster;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct category *group_create_category(struct cluster *cluster, const char *name)
+{
+       struct category *category;
+
+       category = malloc(sizeof(*category));
+       if (!category) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       category->name = strdup(name);
+       if (!category->name) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(category);
+               return NULL;
+       }
+
+       category->cluster = cluster;
+       category->info_list = NULL;
+
+       cluster->category_list = eina_list_append(cluster->category_list, category);
+       return category;
+}
+
+static inline void destroy_cluster(struct cluster *cluster)
+{
+       struct category *category;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(cluster->category_list, l, n, category) {
+               group_destroy_category(category);
+       }
+
+       DbgFree(cluster->name);
+       DbgFree(cluster);
+}
+
+HAPI int group_destroy_cluster(struct cluster *cluster)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct cluster *item;
+
+       EINA_LIST_FOREACH_SAFE(s_info.cluster_list, l, n, item) {
+               if (item == cluster) {
+                       s_info.cluster_list = eina_list_remove_list(s_info.cluster_list, l);
+                       destroy_cluster(cluster);
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+static inline void destroy_category(struct category *category)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct context_info *info;
+
+       EINA_LIST_FOREACH_SAFE(category->info_list, l, n, info) {
+               group_destroy_context_info(info);
+       }
+
+       DbgFree(category->name);
+       DbgFree(category);
+}
+
+HAPI int group_destroy_category(struct category *category)
+{
+       struct cluster *cluster;
+
+       cluster = category->cluster;
+       if (cluster) {
+               cluster->category_list = eina_list_remove(cluster->category_list, category);
+       }
+
+       destroy_category(category);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct category *group_find_category(struct cluster *cluster, const char *name)
+{
+       struct category *category;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(cluster->category_list, l, category) {
+               if (!strcasecmp(category->name, name)) {
+                       return category;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI Eina_List * const group_context_info_list(struct category *category)
+{
+       return category->info_list;
+}
+
+HAPI Eina_List *const group_context_item_list(struct context_info *info)
+{
+       return info->context_list;
+}
+
+HAPI Eina_List *const group_context_option_list(struct context_item *item)
+{
+       return item->option_list;
+}
+
+HAPI Eina_List *const group_cluster_list(void)
+{
+       return s_info.cluster_list;
+}
+
+HAPI Eina_List * const group_category_list(struct cluster *cluster)
+{
+       return cluster->category_list;
+}
+
+HAPI struct context_info * const group_context_info_from_item(struct context_item *item)
+{
+       return item->info;
+}
+
+HAPI struct category * const group_category_from_context_info(struct context_info *info)
+{
+       return info->category;
+}
+
+HAPI const char * const group_pkgname_from_context_info(struct context_info *info)
+{
+       return info->pkgname;
+}
+
+HAPI const char * const group_option_item_key(struct context_option *option)
+{
+       return option->key;
+}
+
+HAPI const char * const group_option_item_value(struct context_option *option)
+{
+       return option->value;
+}
+
+HAPI const char * const group_context_item(struct context_item *item)
+{
+       return item->ctx_item;
+}
+
+HAPI int group_context_item_add_data(struct context_item *item, const char *tag, void *data)
+{
+       struct context_item_data *tmp;
+
+       tmp = malloc(sizeof(*tmp));
+       if (!tmp) {
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       tmp->tag = strdup(tag);
+       if (!tmp->tag) {
+               DbgFree(tmp);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       tmp->data = data;
+       item->data_list = eina_list_append(item->data_list, tmp);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *group_context_item_data(struct context_item *item, const char *tag)
+{
+       struct context_item_data *tmp;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(item->data_list, l, tmp) {
+               if (!strcmp(tmp->tag, tag)) {
+                       return tmp->data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI void *group_context_item_del_data(struct context_item *item, const char *tag)
+{
+       struct context_item_data *tmp;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(item->data_list, l, n, tmp) {
+               if (!strcmp(tmp->tag, tag)) {
+                       void *data;
+
+                       item->data_list = eina_list_remove(item->data_list, tmp);
+
+                       data = tmp->data;
+
+                       DbgFree(tmp->tag);
+                       DbgFree(tmp);
+
+                       return data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI const char * const group_category_name(struct category *category)
+{
+       return category ? category->name : NULL;
+}
+
+HAPI const char * const group_cluster_name(struct cluster *cluster)
+{
+       return cluster ? cluster->name : NULL;
+}
+
+HAPI const char *group_cluster_name_by_category(struct category *category)
+{
+       return !category ? NULL : (category->cluster ? category->cluster->name : NULL);
+}
+
+static inline char *get_token(char *ptr, int *len)
+{
+       char *name;
+       int _len;
+
+       if (*len == 0) {
+               ErrPrint("Start brace but len = 0\n");
+               return NULL;
+       }
+
+       _len = *len;
+
+       while (_len > 0 && isspace(ptr[_len])) {
+               _len--;
+       }
+
+       if (_len == 0) {
+               ErrPrint("Token has no string\n");
+               return NULL;
+       }
+
+       name = malloc(_len + 1);
+       if (!name) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       strncpy(name, ptr - *len, _len);
+       name[_len] = '\0';
+
+       *len = _len;
+       return name;
+}
+
+HAPI int group_add_widget(const char *group, const char *pkgname)
+{
+       struct cluster *cluster;
+       struct category *category;
+       struct context_info *info = NULL;
+       struct context_item *item = NULL;
+       char *key;
+       char *name;
+       char *ptr;
+       int len;
+       int is_open = 0;
+       enum {
+               CLUSTER,
+               CATEGORY,
+               CONTEXT_ITEM,
+               CONTEXT_OPTION_KEY,
+               CONTEXT_OPTION_VALUE,
+               CONTEXT_ERROR = 0xFFFFFFFF
+       } state;
+
+       state = CLUSTER;
+
+       ptr = (char *)group;
+       len = 0;
+       key = NULL;
+
+       /* Skip the first space characters */
+       while (*ptr && isspace(*ptr)) ptr++;
+
+       cluster = NULL;
+       while (*ptr) {
+               if (*ptr == '{') {
+                       name = get_token(ptr, &len);
+                       if (!name) {
+                               ErrPrint("Failed to get token\n");
+                               return WIDGET_ERROR_FAULT;
+                       }
+                       /* cluster{category{context{key=value,key=value},context{key=value}}} */
+                       /* cluster{category} */
+
+                       switch (state) {
+                       case CLUSTER:
+                               cluster = group_find_cluster(name);
+                               if (!cluster) {
+                                       cluster = group_create_cluster(name);
+                               }
+
+                               if (!cluster) {
+                                       ErrPrint("Failed to get cluster\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CATEGORY;
+                               break;
+
+                       case CATEGORY:
+                               category = group_find_category(cluster, name);
+                               if (!category) {
+                                       category = group_create_category(cluster, name);
+                               }
+
+                               if (!category) {
+                                       ErrPrint("Failed to get category\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               info = group_create_context_info(category, pkgname);
+                               if (!info) {
+                                       ErrPrint("Failed to create ctx info\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CONTEXT_ITEM;
+                               break;
+
+                       case CONTEXT_ITEM:
+                               item = group_add_context_item(info, name);
+                               if (!item) {
+                                       ErrPrint("Failed to create a context item\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CONTEXT_OPTION_KEY;
+                               break;
+
+                       case CONTEXT_OPTION_KEY:
+                       case CONTEXT_OPTION_VALUE:
+                       default:
+                               ErrPrint("Invalid state\n");
+                               DbgFree(name);
+                               return WIDGET_ERROR_FAULT;
+                       }
+
+                       DbgFree(name);
+                       is_open++;
+                       len = 0;
+                       ptr++;
+                       while (*ptr && isspace(*ptr)) ptr++;
+                       continue;
+               } else if (*ptr == ',') {
+                       name = get_token(ptr, &len);
+                       if (!name) {
+                               ErrPrint("Failed to get token (len:%d)\n", len);
+                               len = 0;
+                               ptr++;
+                               while (*ptr && isspace(*ptr)) ptr++;
+                               continue;
+                       }
+
+                       switch (state) {
+                       case CLUSTER:
+                               if (is_open != 0) {
+                                       ErrPrint("Invalid state\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+                               cluster = group_find_cluster(name);
+                               if (!cluster) {
+                                       cluster = group_create_cluster(name);
+                               }
+
+                               if (!cluster) {
+                                       ErrPrint("Failed to get cluster\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CATEGORY;
+                               break;
+
+                       case CATEGORY:
+                               if (is_open != 1) {
+                                       ErrPrint("Invalid state\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+                               category = group_find_category(cluster, name);
+                               if (!category) {
+                                       category = group_create_category(cluster, name);
+                               }
+
+                               if (!category) {
+                                       ErrPrint("Failed to get category\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               info = group_create_context_info(category, pkgname);
+                               if (!info) {
+                                       ErrPrint("Failed to create ctx info\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CONTEXT_ITEM;
+                               break;
+                       case CONTEXT_ITEM:
+                               if (is_open == 1) {
+                                       category = group_find_category(cluster, name);
+                                       if (!category) {
+                                               category = group_create_category(cluster, name);
+                                       }
+
+                                       if (!category) {
+                                               ErrPrint("Failed to get category\n");
+                                               DbgFree(name);
+                                               return WIDGET_ERROR_FAULT;
+                                       }
+
+                                       info = group_create_context_info(category, pkgname);
+                                       if (!info) {
+                                               ErrPrint("Failed to create ctx info\n");
+                                               DbgFree(name);
+                                               return WIDGET_ERROR_FAULT;
+                                       }
+                               } else if (is_open == 2) {
+                                       item = group_add_context_item(info, name);
+                                       if (!item) {
+                                               ErrPrint("Failed to create a context item\n");
+                                               DbgFree(name);
+                                               return WIDGET_ERROR_FAULT;
+                                       }
+                                       state = CONTEXT_OPTION_KEY;
+                               } else {
+                                       ErrPrint("Invalid state\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               break;
+                       case CONTEXT_OPTION_VALUE:
+                               if (is_open != 3) {
+                                       ErrPrint("Invalid state\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               if (group_add_option(item, key, name) < 0) {
+                                       ErrPrint("Failed to add a new option: %s - %s\n", key, name);
+                               }
+
+                               DbgFree(key);
+                               key = NULL;
+
+                               state = CONTEXT_OPTION_KEY;
+                               break;
+                       case CONTEXT_OPTION_KEY:
+                       default:
+                               ErrPrint("Invalid state (%s)\n", name);
+                               DbgFree(name);
+                               return WIDGET_ERROR_FAULT;
+                       }
+
+                       DbgFree(name);
+                       len = 0;
+                       ptr++;
+                       while (*ptr && isspace(*ptr)) ptr++;
+                       continue;
+               } else if (*ptr == '=') {
+                       if (is_open != 3 || state != CONTEXT_OPTION_KEY) {
+                               ErrPrint("Invalid state\n");
+                               return WIDGET_ERROR_FAULT;
+                       }
+
+                       key = get_token(ptr, &len);
+                       if (!key) {
+                               ErrPrint("Failed to get token\n");
+                               return WIDGET_ERROR_FAULT;
+                       }
+
+                       state = CONTEXT_OPTION_VALUE;
+                       len = 0;
+                       ptr++;
+                       while (*ptr && isspace(*ptr)) ptr++;
+                       continue;
+               } else if (*ptr == '}') {
+                       if (is_open <= 0) {
+                               ErrPrint("Invalid state\n");
+                               return WIDGET_ERROR_FAULT;
+                       }
+
+                       name = get_token(ptr, &len);
+                       if (!name) {
+                               ErrPrint("Failed to get token, len:%d\n", len);
+                               is_open--;
+                               len = 0;
+                               ptr++;
+                               while (*ptr && isspace(*ptr)) ptr++;
+                               continue;
+                       }
+
+                       switch (state) {
+                       case CATEGORY:
+                               category = group_find_category(cluster, name);
+                               if (!category) {
+                                       category = group_create_category(cluster, name);
+                               }
+
+                               if (!category) {
+                                       ErrPrint("Failed to get category\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               info = group_create_context_info(category, pkgname);
+                               if (!info) {
+                                       ErrPrint("Failed to create ctx info\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               state = CLUSTER;
+                               break;
+                       case CONTEXT_ITEM:
+                               if (is_open == 1) {
+                                       category = group_find_category(cluster, name);
+                                       if (!category) {
+                                               category = group_create_category(cluster, name);
+                                       }
+
+                                       if (!category) {
+                                               ErrPrint("Failed to get category\n");
+                                               DbgFree(name);
+                                               return WIDGET_ERROR_FAULT;
+                                       }
+
+                                       info = group_create_context_info(category, pkgname);
+                                       if (!info) {
+                                               ErrPrint("Failed to create ctx info\n");
+                                               DbgFree(name);
+                                               return WIDGET_ERROR_FAULT;
+                                       }
+
+                                       state = CLUSTER;
+                               } else if (is_open == 2) {
+                                       state = CATEGORY;
+                               } else {
+                                       ErrPrint("Invalid state\n");
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+                               break;
+                       case CONTEXT_OPTION_VALUE:
+                               if (is_open != 2) {
+                                       ErrPrint("Invalid state (%s)\n", name);
+                                       DbgFree(name);
+                                       return WIDGET_ERROR_FAULT;
+                               }
+
+                               if (group_add_option(item, key, name) < 0) {
+                                       ErrPrint("Failed to add a new option: %s - %s\n", key, name);
+                               }
+
+                               DbgFree(key);
+                               key = NULL;
+
+                               state = CONTEXT_ITEM;
+                               break;
+                       case CONTEXT_OPTION_KEY:
+                       case CLUSTER:
+                       default:
+                               ErrPrint("Invalid state (%s)\n", name);
+                               break;
+                       }
+
+                       DbgFree(name);
+                       is_open--;
+                       len = 0;
+                       ptr++;
+                       while (*ptr && isspace(*ptr)) ptr++;
+                       continue;
+               }
+
+               len++;
+               ptr++;
+       }
+
+       /* If some cases, the key is not released, try release it, doesn't need to check NULL */
+       DbgFree(key);
+
+       if (state != CLUSTER) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int group_del_widget(const char *pkgname)
+{
+       Eina_List *l;
+       Eina_List *n;
+       Eina_List *s_l;
+       Eina_List *s_n;
+       Eina_List *i_l;
+       Eina_List *i_n;
+       struct cluster *cluster;
+       struct category *category;
+       struct context_info *info;
+
+       EINA_LIST_FOREACH_SAFE(s_info.cluster_list, l, n, cluster) {
+               EINA_LIST_FOREACH_SAFE(cluster->category_list, s_l, s_n, category) {
+                       EINA_LIST_FOREACH_SAFE(category->info_list, i_l, i_n, info) {
+                               if (!strcmp(pkgname, info->pkgname)) {
+                                       group_destroy_context_info(info);
+                               }
+                       }
+
+                       if (!category->info_list) {
+                               group_destroy_category(category);
+                       }
+               }
+
+               if (!cluster->category_list) {
+                       group_destroy_cluster(cluster);
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int group_init(void)
+{
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int group_fini(void)
+{
+       struct cluster *cluster;
+       struct category *category;
+
+       EINA_LIST_FREE(s_info.cluster_list, cluster) {
+
+               EINA_LIST_FREE(cluster->category_list, category) {
+                       destroy_category(category);
+               }
+
+               destroy_cluster(cluster);
+       }
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/instance.c b/src/instance.c
new file mode 100644 (file)
index 0000000..387485c
--- /dev/null
@@ -0,0 +1,3921 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <dlog.h>
+#include <Eina.h>
+#include <gio/gio.h>
+#include <Ecore.h>
+
+#include <packet.h>
+#include <com-core_packet.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_errno.h>
+#include <widget_cmd_list.h>
+#include <widget_buffer.h>
+#include <widget_conf.h>
+#include <widget_util.h>
+
+#include "conf.h"
+#include "util.h"
+#include "debug.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "instance.h"
+#include "client_rpc.h"
+#include "package.h"
+#include "script_handler.h"
+#include "buffer_handler.h"
+#include "setting.h"
+#include "monitor.h"
+
+int errno;
+
+static struct info {
+       enum widget_fb_type env_buf_type;
+} s_info = {
+       .env_buf_type = WIDGET_FB_TYPE_FILE,
+};
+
+struct set_pinup_cbdata {
+       struct inst_info *inst;
+       int pinup;
+};
+
+struct resize_cbdata {
+       struct inst_info *inst;
+       int w;
+       int h;
+};
+
+struct update_mode_cbdata {
+       struct inst_info *inst;
+       int active_update;
+};
+
+struct change_group_cbdata {
+       struct inst_info *inst;
+       char *cluster;
+       char *category;
+};
+
+struct period_cbdata {
+       struct inst_info *inst;
+       double period;
+};
+
+struct event_item {
+       int (*event_cb)(struct inst_info *inst, void *data);
+       void *data;
+       int deleted;
+};
+
+struct tag_item {
+       char *tag;
+       void *data;
+};
+
+struct inst_info {
+       struct pkg_info *info;
+
+       enum instance_state state; /*!< Represents current state */
+       enum instance_state requested_state; /*!< Only ACTIVATED | DESTROYED is acceptable */
+       widget_destroy_type_e destroy_type;
+       int changing_state;
+       int unicast_delete_event;
+
+       char *id;
+       double timestamp;
+
+       char *content;
+       char *cluster;
+       char *category;
+       char *title;
+       int is_pinned_up;
+       double sleep_at;
+       int scroll_locked; /*!< Scroller which is in viewer is locked. */
+       int active_update; /*!< Viewer will reload the buffer by itself, so the provider doesn't need to send the updated event */
+
+       char *icon;
+       char *name;
+
+       enum widget_visible_state visible;
+
+       struct {
+               int width;
+               int height;
+               double priority;
+
+               union {
+                       struct script_info *script;
+                       struct buffer_info *buffer;
+               } canvas;
+
+               struct buffer_info **extra_buffer;
+
+               double period;
+       } widget;
+
+       struct {
+               int width;
+               int height;
+               double x;
+               double y;
+
+               union {
+                       struct script_info *script;
+                       struct buffer_info *buffer;
+               } canvas;
+
+               struct buffer_info **extra_buffer;
+
+               struct client_node *owner;
+               int is_opened_for_reactivate;
+               int need_to_send_close_event;
+               char *pended_update_desc;
+               int pended_update_cnt;
+       } gbar;
+
+       struct client_node *client; /*!< Owner - creator */
+       Eina_List *client_list; /*!< Viewer list */
+       int refcnt;
+
+       Ecore_Timer *update_timer; /*!< Only used for secured widget */
+
+       enum event_process {
+               INST_EVENT_PROCESS_IDLE = 0x00,
+               INST_EVENT_PROCESS_DELETE = 0x01
+       } in_event_process;
+       Eina_List *delete_event_list;
+
+       Eina_List *data_list;
+       int orientation;
+};
+
+static Eina_Bool update_timer_cb(void *data);
+
+static int client_send_event(struct inst_info *instance, struct packet *packet, struct packet *owner_packet)
+{
+       /*!
+        * \note
+        * If a instance has owner, send event to it first.
+        */
+       if (instance->client) {
+               /*
+                * To prevent from packet destruction
+                */
+               if (owner_packet) {
+                       client_rpc_async_request(instance->client, owner_packet);
+               } else if (packet) {
+                       packet_ref(packet);
+                       client_rpc_async_request(instance->client, packet);
+               }
+       }
+
+       /*!
+        * And then broadcast events to all client (viewer)
+        */
+       return client_broadcast(instance, packet);
+}
+
+static inline void timer_thaw(struct inst_info *inst)
+{
+       double pending;
+       double period;
+       double delay;
+       double sleep_time;
+
+       ecore_timer_thaw(inst->update_timer);
+       period = ecore_timer_interval_get(inst->update_timer);
+       pending = ecore_timer_pending_get(inst->update_timer);
+       delay = util_time_delay_for_compensation(period) - pending;
+       ecore_timer_delay(inst->update_timer, delay);
+
+       if (inst->sleep_at == 0.0f) {
+               return;
+       }
+
+       sleep_time = util_timestamp() - inst->sleep_at;
+       if (sleep_time > pending) {
+               (void)update_timer_cb(inst);
+       }
+
+       inst->sleep_at = 0.0f;
+}
+
+static inline void timer_freeze(struct inst_info *inst)
+{
+       ecore_timer_freeze(inst->update_timer);
+
+       if (ecore_timer_interval_get(inst->update_timer) <= 1.0f) {
+               return;
+       }
+
+#if defined(_USE_ECORE_TIME_GET)
+       inst->sleep_at = ecore_time_get();
+#else
+       struct timeval tv;
+       if (gettimeofday(&tv, NULL) < 0) {
+               ErrPrint("gettimeofday: %d\n", errno);
+               tv.tv_sec = 0;
+               tv.tv_usec = 0;
+       }
+       inst->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
+#endif
+}
+
+static int viewer_deactivated_cb(struct client_node *client, void *data)
+{
+       struct inst_info *inst = data;
+       unsigned int cmd = CMD_VIEWER_DISCONNECTED;
+       struct packet *packet;
+
+       DbgPrint("%d is deleted from the list of viewer of %s(%s)\n", client_pid(client), package_name(instance_package(inst)), instance_id(inst));
+       if (!eina_list_data_find(inst->client_list, client)) {
+               ErrPrint("Not found\n");
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sss", package_name(inst->info), inst->id, client_direct_addr(inst->client));
+       if (packet) {
+               if (slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0) < 0) {
+                       ErrPrint("Failed to send request to slave: %s\n", package_name(inst->info));
+                       /*!
+                        * \note
+                        * From now, packet is not valid. because the slave_rpc_request_only will destroy it if it fails;)
+                        */
+               }
+       } else {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       inst->client_list = eina_list_remove(inst->client_list, client);
+       if (!inst->client_list && !inst->client) {
+               DbgPrint("Has no clients\n");
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_FAULT);
+       }
+
+       instance_unref(inst);
+       return -1; /*!< Remove this callback from the cb list */
+}
+
+static int pause_widget(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_WIDGET_PAUSE;
+
+       packet = packet_create_noack((const char *)&cmd, "ss", package_name(inst->info), inst->id);
+       if (!packet) {
+               ErrPrint("Failed to create a new packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
+}
+
+/*! \TODO Wake up the freeze'd timer */
+static int resume_widget(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_WIDGET_RESUME;
+
+       packet = packet_create_noack((const char *)&cmd, "ss", package_name(inst->info), inst->id);
+       if (!packet) {
+               ErrPrint("Failed to create a new packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
+}
+
+static inline int instance_recover_visible_state(struct inst_info *inst)
+{
+       int ret;
+
+       switch (inst->visible) {
+       case WIDGET_SHOW:
+       case WIDGET_HIDE:
+               instance_thaw_updator(inst);
+
+               ret = 0;
+               break;
+       case WIDGET_HIDE_WITH_PAUSE:
+               ret = pause_widget(inst);
+
+               instance_freeze_updator(inst);
+               break;
+       default:
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               break;
+       }
+
+       DbgPrint("Visible state is recovered to %d\n", ret);
+       return ret;
+}
+
+static inline void instance_send_update_mode_event(struct inst_info *inst, int active_mode, int status)
+{
+       struct packet *packet;
+       const char *pkgname;
+       unsigned int cmd = CMD_RESULT_UPDATE_MODE;
+
+       if (!inst->info) {
+               ErrPrint("Instance info is not ready to use\n");
+               return;
+       }
+
+       pkgname = package_name(inst->info);
+
+       packet = packet_create_noack((const char *)&cmd, "ssii", pkgname, inst->id, status, active_mode);
+       if (packet) {
+               client_send_event(inst, packet, NULL);
+       } else {
+               ErrPrint("Failed to send update mode event\n");
+       }
+}
+
+static inline void instance_send_update_id(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_UPDATE_ID;
+
+       packet = packet_create_noack((const char *)&cmd, "ds", inst->timestamp, inst->id);
+       if (packet) {
+               client_send_event(inst, packet, NULL);
+       } else {
+               ErrPrint("Failed to create update_id packet\n");
+       }
+}
+
+static inline void instance_send_resized_event(struct inst_info *inst, int is_gbar, int w, int h, int status)
+{
+       struct packet *packet;
+       enum widget_widget_type widget_type;
+       const char *pkgname;
+       const char *id;
+       unsigned int cmd = CMD_SIZE_CHANGED;
+
+       if (!inst->info) {
+               ErrPrint("Instance info is not ready to use\n");
+               return;
+       }
+
+       pkgname = package_name(inst->info);
+
+       widget_type = package_widget_type(inst->info);
+       if (widget_type == WIDGET_TYPE_SCRIPT) {
+               id = script_handler_buffer_id(inst->widget.canvas.script);
+       } else if (widget_type == WIDGET_TYPE_BUFFER) {
+               id = buffer_handler_id(inst->widget.canvas.buffer);
+       } else {
+               id = "";
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sssiiii", pkgname, inst->id, id, is_gbar, w, h, status);
+       if (packet) {
+               client_send_event(inst, packet, NULL);
+       } else {
+               ErrPrint("Failed to send size changed event\n");
+       }
+}
+
+static void update_mode_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct update_mode_cbdata *cbdata = data;
+       int ret;
+
+       if (!packet) {
+               ErrPrint("Invalid packet\n");
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid parameters\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               cbdata->inst->active_update = cbdata->active_update;
+       }
+
+out:
+       instance_send_update_mode_event(cbdata->inst, cbdata->active_update, ret);
+       instance_unref(cbdata->inst);
+       DbgFree(cbdata);
+}
+
+HAPI int instance_unicast_created_event(struct inst_info *inst, struct client_node *client)
+{
+       struct packet *packet;
+       enum widget_widget_type widget_type;
+       enum widget_gbar_type gbar_type;
+       const char *widget_file;
+       const char *gbar_file;
+       unsigned int cmd = CMD_CREATED;
+
+       if (!client) {
+               client = inst->client;
+               if (!client) {
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       widget_type = package_widget_type(inst->info);
+       gbar_type = package_gbar_type(inst->info);
+
+       if (widget_type == WIDGET_TYPE_SCRIPT) {
+               widget_file = script_handler_buffer_id(inst->widget.canvas.script);
+       } else if (widget_type == WIDGET_TYPE_BUFFER) {
+               widget_file = buffer_handler_id(inst->widget.canvas.buffer);
+       } else {
+               widget_file = "";
+       }
+
+       if (gbar_type == GBAR_TYPE_SCRIPT) {
+               gbar_file = script_handler_buffer_id(inst->gbar.canvas.script);
+       } else if (gbar_type == GBAR_TYPE_BUFFER) {
+               gbar_file = buffer_handler_id(inst->gbar.canvas.buffer);
+       } else {
+               gbar_file = "";
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "dsssiiiisssssdiiiiidsi",
+                       inst->timestamp,
+                       package_name(inst->info), inst->id, inst->content,
+                       inst->widget.width, inst->widget.height,
+                       inst->gbar.width, inst->gbar.height,
+                       inst->cluster, inst->category,
+                       widget_file, gbar_file,
+                       package_auto_launch(inst->info),
+                       inst->widget.priority,
+                       package_size_list(inst->info),
+                       !!inst->client,
+                       package_pinup(inst->info),
+                       widget_type, gbar_type,
+                       inst->widget.period, inst->title,
+                       inst->is_pinned_up);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_rpc_async_request(client, packet);
+}
+
+static int update_client_list(struct client_node *client, void *data)
+{
+       struct inst_info *inst = data;
+
+       if (!instance_has_client(inst, client)) {
+               instance_add_client(inst, client);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static int instance_broadcast_created_event(struct inst_info *inst)
+{
+       struct packet *packet;
+       struct packet *owner_packet;
+       enum widget_widget_type widget_type;
+       enum widget_gbar_type gbar_type;
+       const char *widget_file;
+       const char *gbar_file;
+       unsigned int cmd = CMD_CREATED;
+       int ret;
+
+       widget_type = package_widget_type(inst->info);
+       gbar_type = package_gbar_type(inst->info);
+
+       if (widget_type == WIDGET_TYPE_SCRIPT) {
+               widget_file = script_handler_buffer_id(inst->widget.canvas.script);
+       } else if (widget_type == WIDGET_TYPE_BUFFER) {
+               widget_file = buffer_handler_id(inst->widget.canvas.buffer);
+       } else {
+               widget_file = "";
+       }
+
+       if (gbar_type == GBAR_TYPE_SCRIPT) {
+               gbar_file = script_handler_buffer_id(inst->gbar.canvas.script);
+       } else if (gbar_type == GBAR_TYPE_BUFFER) {
+               gbar_file = buffer_handler_id(inst->gbar.canvas.buffer);
+       } else {
+               gbar_file = "";
+       }
+
+       if (!inst->client) {
+               client_browse_group_list(inst->cluster, inst->category, update_client_list, inst);
+       }
+
+       client_browse_category_list(package_category(inst->info), update_client_list, inst);
+       inst->unicast_delete_event = 0;
+
+       packet = packet_create_noack((const char *)&cmd, "dsssiiiisssssdiiiiidsi", 
+                       inst->timestamp,
+                       package_name(inst->info), inst->id, inst->content,
+                       inst->widget.width, inst->widget.height,
+                       inst->gbar.width, inst->gbar.height,
+                       inst->cluster, inst->category,
+                       widget_file, gbar_file,
+                       package_auto_launch(inst->info),
+                       inst->widget.priority,
+                       package_size_list(inst->info),
+                       0, /* Assume, this is a system created widget (not the ower) */
+                       package_pinup(inst->info),
+                       widget_type, gbar_type,
+                       inst->widget.period, inst->title,
+                       inst->is_pinned_up);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (inst->client) {
+               owner_packet = packet_create_noack((const char *)&cmd, "dsssiiiisssssdiiiiidsi", 
+                               inst->timestamp,
+                               package_name(inst->info), inst->id, inst->content,
+                               inst->widget.width, inst->widget.height,
+                               inst->gbar.width, inst->gbar.height,
+                               inst->cluster, inst->category,
+                               widget_file, gbar_file,
+                               package_auto_launch(inst->info),
+                               inst->widget.priority,
+                               package_size_list(inst->info),
+                               1,
+                               package_pinup(inst->info),
+                               widget_type, gbar_type,
+                               inst->widget.period, inst->title,
+                               inst->is_pinned_up);
+               if (!owner_packet) {
+                       ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+                       return WIDGET_ERROR_FAULT;
+               }
+       } else {
+               owner_packet = NULL;
+       }
+
+       ret = client_send_event(inst, packet, owner_packet);
+
+       monitor_multicast_state_change_event(package_name(inst->info), MONITOR_EVENT_CREATED, instance_id(inst), instance_content(inst));
+
+       return ret;
+}
+
+HAPI int instance_unicast_deleted_event(struct inst_info *inst, struct client_node *client, int reason)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_DELETED;
+
+       if (!client) {
+               client = inst->client;
+               if (!client) {
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdi", package_name(inst->info), inst->id, inst->timestamp, reason);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_rpc_async_request(client, packet);
+}
+
+static int instance_broadcast_deleted_event(struct inst_info *inst, int reason)
+{
+       struct packet *packet;
+       struct client_node *client;
+       Eina_List *l;
+       Eina_List *n;
+       int ret;
+       unsigned int cmd = CMD_DELETED;
+
+       packet = packet_create_noack((const char *)&cmd, "ssdi", package_name(inst->info), inst->id, inst->timestamp, reason);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = client_send_event(inst, packet, NULL);
+
+       EINA_LIST_FOREACH_SAFE(inst->client_list, l, n, client) {
+               instance_del_client(inst, client);
+       }
+
+       monitor_multicast_state_change_event(package_name(inst->info), MONITOR_EVENT_DESTROYED, instance_id(inst), instance_content(inst));
+
+       return ret;
+}
+
+static int client_deactivated_cb(struct client_node *client, void *data)
+{
+       struct inst_info *inst = data;
+       instance_destroy(inst, WIDGET_DESTROY_TYPE_FAULT);
+       return WIDGET_ERROR_NONE;
+}
+
+static int send_gbar_destroyed_to_client(struct inst_info *inst, int status)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_GBAR_DESTROYED;
+
+       if (!inst->gbar.need_to_send_close_event && status != WIDGET_ERROR_FAULT) {
+               ErrPrint("GBAR is not created\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", package_name(inst->info), inst->id, status);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       inst->gbar.need_to_send_close_event = 0;
+
+       return client_send_event(inst, packet, NULL);
+}
+
+static inline void invoke_delete_callbacks(struct inst_info *inst)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event_item *item;
+
+       inst->in_event_process |= INST_EVENT_PROCESS_DELETE;
+       EINA_LIST_FOREACH_SAFE(inst->delete_event_list, l, n, item) {
+               if (item->deleted || item->event_cb(inst, item->data) < 0 || item->deleted) {
+                       inst->delete_event_list = eina_list_remove(inst->delete_event_list, item);
+                       DbgFree(item);
+               }
+       }
+       inst->in_event_process &= ~INST_EVENT_PROCESS_DELETE;
+}
+
+HAPI int instance_event_callback_is_added(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data)
+{
+       struct event_item *item;
+       Eina_List *l;
+
+       if (!event_cb) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (type) {
+       case INSTANCE_EVENT_DESTROY:
+               EINA_LIST_FOREACH(inst->delete_event_list, l, item) {
+                       if (item->event_cb == event_cb && item->data == data) {
+                               return 1;
+                       }
+               }
+
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return 0;
+}
+
+HAPI int instance_event_callback_add(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data)
+{
+       struct event_item *item;
+
+       if (!event_cb) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (type) {
+       case INSTANCE_EVENT_DESTROY:
+               item = malloc(sizeof(*item));
+               if (!item) {
+                       ErrPrint("malloc: %d\n", errno);
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               item->event_cb = event_cb;
+               item->data = data;
+               item->deleted = 0;
+
+               inst->delete_event_list = eina_list_append(inst->delete_event_list, item);
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int instance_event_callback_del(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event_item *item;
+
+       switch (type) {
+       case INSTANCE_EVENT_DESTROY:
+               EINA_LIST_FOREACH_SAFE(inst->delete_event_list, l, n, item) {
+                       if (item->event_cb == event_cb && item->data == data) {
+                               if (inst->in_event_process & INST_EVENT_PROCESS_DELETE) {
+                                       item->deleted = 1;
+                               } else {
+                                       inst->delete_event_list = eina_list_remove(inst->delete_event_list, item);
+                                       DbgFree(item);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+static inline void destroy_instance(struct inst_info *inst)
+{
+       struct pkg_info *pkg;
+       enum widget_widget_type widget_type;
+       enum widget_gbar_type gbar_type;
+       struct slave_node *slave;
+       struct event_item *item;
+       struct tag_item *tag_item;
+
+       (void)send_gbar_destroyed_to_client(inst, WIDGET_ERROR_NONE);
+
+       invoke_delete_callbacks(inst);
+
+       pkg = inst->info;
+
+       widget_type = package_widget_type(pkg);
+       gbar_type = package_gbar_type(pkg);
+       slave = package_slave(inst->info);
+
+       DbgPrint("Instance is destroyed (%p), slave(%p)\n", inst, slave);
+
+       if (widget_type == WIDGET_TYPE_SCRIPT) {
+               (void)script_handler_unload(inst->widget.canvas.script, 0);
+               if (script_handler_destroy(inst->widget.canvas.script) == (int)WIDGET_ERROR_NONE) {
+                       inst->widget.canvas.script = NULL;
+               }
+       } else if (widget_type == WIDGET_TYPE_BUFFER) {
+               (void)buffer_handler_unload(inst->widget.canvas.buffer);
+               if (buffer_handler_destroy(inst->widget.canvas.buffer) == (int)WIDGET_ERROR_NONE) {
+                       inst->widget.canvas.buffer = NULL;
+               }
+
+               if (inst->widget.extra_buffer) {
+                       int i;
+
+                       for (i = 0; i < WIDGET_CONF_EXTRA_BUFFER_COUNT; i++) {
+                               if (!inst->widget.extra_buffer[i]) {
+                                       continue;
+                               }
+
+                               (void)buffer_handler_unload(inst->widget.extra_buffer[i]);
+                               if (buffer_handler_destroy(inst->widget.extra_buffer[i]) == (int)WIDGET_ERROR_NONE) {
+                                       inst->widget.extra_buffer[i] = NULL;
+                               }
+                       }
+
+                       DbgFree(inst->widget.extra_buffer);
+                       inst->widget.extra_buffer = NULL;
+               }
+       }
+
+       if (gbar_type == GBAR_TYPE_SCRIPT) {
+               (void)script_handler_unload(inst->gbar.canvas.script, 1);
+               if (script_handler_destroy(inst->gbar.canvas.script) == (int)WIDGET_ERROR_NONE) {
+                       inst->gbar.canvas.script = NULL;
+               }
+       } else if (gbar_type == GBAR_TYPE_BUFFER) {
+               (void)buffer_handler_unload(inst->gbar.canvas.buffer);
+               if (buffer_handler_destroy(inst->gbar.canvas.buffer) == (int)WIDGET_ERROR_NONE) {
+                       inst->gbar.canvas.buffer = NULL;
+               }
+               if (inst->gbar.extra_buffer) {
+                       int i;
+
+                       for (i = 0; i < WIDGET_CONF_EXTRA_BUFFER_COUNT; i++) {
+                               if (!inst->gbar.extra_buffer[i]) {
+                                       continue;
+                               }
+
+                               (void)buffer_handler_unload(inst->gbar.extra_buffer[i]);
+                               if (buffer_handler_destroy(inst->gbar.extra_buffer[i]) == (int)WIDGET_ERROR_NONE) {
+                                       inst->gbar.extra_buffer[i] = NULL;
+                               }
+                       }
+
+                       DbgFree(inst->gbar.extra_buffer);
+                       inst->gbar.extra_buffer = NULL;
+               }
+       }
+
+       if (inst->client) {
+               client_event_callback_del(inst->client, CLIENT_EVENT_DEACTIVATE, client_deactivated_cb, inst);
+               client_unref(inst->client);
+       }
+
+       if (inst->update_timer) {
+               ecore_timer_del(inst->update_timer);
+       }
+
+       EINA_LIST_FREE(inst->data_list, tag_item) {
+               DbgPrint("Tagged item[%s] %p\n", tag_item->tag, tag_item->data);
+               DbgFree(tag_item->tag);
+               DbgFree(tag_item);
+       }
+
+       EINA_LIST_FREE(inst->delete_event_list, item) {
+               DbgFree(item);
+       }
+
+       DbgFree(inst->icon);
+       DbgFree(inst->name);
+       DbgFree(inst->category);
+       DbgFree(inst->cluster);
+       DbgFree(inst->content);
+       DbgFree(inst->title);
+       util_unlink(widget_util_uri_to_path(inst->id));
+       DbgFree(inst->id);
+       package_del_instance(inst->info, inst);
+       DbgFree(inst);
+
+       slave = slave_unload_instance(slave);
+}
+
+static Eina_Bool update_timer_cb(void *data)
+{
+       struct inst_info *inst = (struct inst_info *)data;
+
+       slave_rpc_request_update(package_name(inst->info), inst->id, inst->cluster, inst->category, NULL, 0);
+       return ECORE_CALLBACK_RENEW;
+}
+
+static inline void unfork_package(struct inst_info *inst)
+{
+       DbgFree(inst->id);
+       inst->id = NULL;
+
+       if (inst->update_timer) {
+               ecore_timer_del(inst->update_timer);
+               inst->update_timer = NULL;
+       }
+}
+
+static inline int fork_package(struct inst_info *inst, const char *pkgname)
+{
+       struct pkg_info *info;
+       int len;
+
+       info = package_find(pkgname);
+       if (!info) {
+               ErrPrint("%s is not found\n", pkgname);
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       len = strlen(SCHEMA_FILE "%s%s_%d_%lf.png") + strlen(WIDGET_CONF_IMAGE_PATH) + strlen(package_name(info)) + 50;
+       inst->id = malloc(len);
+       if (!inst->id) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       snprintf(inst->id, len, SCHEMA_FILE "%s%s_%d_%lf.png", WIDGET_CONF_IMAGE_PATH, package_name(info), client_pid(inst->client), inst->timestamp);
+
+       instance_set_gbar_size(inst, package_gbar_width(info), package_gbar_height(info));
+
+       inst->widget.period = package_period(info);
+
+       inst->info = info;
+
+       if (package_secured(info) || (WIDGET_IS_INHOUSE(package_abi(info)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL)) {
+               if (inst->widget.period > 0.0f) {
+                       inst->update_timer = util_timer_add(inst->widget.period, update_timer_cb, inst);
+                       if (!inst->update_timer) {
+                               ErrPrint("Failed to add an update timer for instance %s\n", inst->id);
+                       } else {
+                               timer_freeze(inst); /* Freeze the update timer as default */
+                       }
+               } else {
+                       inst->update_timer = NULL;
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct inst_info *instance_create(struct client_node *client, double timestamp, const char *pkgname, const char *content, const char *cluster, const char *category, double period, int width, int height)
+{
+       struct inst_info *inst;
+
+       inst = calloc(1, sizeof(*inst));
+       if (!inst) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       inst->timestamp = timestamp;
+       inst->widget.width = width;
+       inst->widget.height = height;
+
+       inst->content = strdup(content);
+       if (!inst->content) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       inst->cluster = strdup(cluster);
+       if (!inst->cluster) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(inst->content);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       inst->category = strdup(category);
+       if (!inst->category) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(inst->cluster);
+               DbgFree(inst->content);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       inst->title = strdup(WIDGET_CONF_DEFAULT_TITLE); /*!< Use the DEFAULT Title "" */
+       if (!inst->title) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(inst->category);
+               DbgFree(inst->cluster);
+               DbgFree(inst->content);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       if (client) {
+               inst->client = client_ref(client);
+               if (client_event_callback_add(inst->client, CLIENT_EVENT_DEACTIVATE, client_deactivated_cb, inst) < 0) {
+                       ErrPrint("Failed to add client event callback: %s\n", inst->id);
+               }
+       }
+
+       if (fork_package(inst, pkgname) < 0) {
+               (void)client_unref(inst->client);
+               DbgFree(inst->title);
+               DbgFree(inst->category);
+               DbgFree(inst->cluster);
+               DbgFree(inst->content);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       inst->unicast_delete_event = 1;
+       inst->state = INST_INIT;
+       inst->requested_state = INST_INIT;
+       if (WIDGET_CONF_EXTRA_BUFFER_COUNT) {
+               inst->widget.extra_buffer = calloc(WIDGET_CONF_EXTRA_BUFFER_COUNT, sizeof(*inst->widget.extra_buffer));
+               if (!inst->widget.extra_buffer) {
+                       ErrPrint("Failed to allocate buffer for widget extra buffer\n");
+               }
+
+               inst->gbar.extra_buffer = calloc(WIDGET_CONF_EXTRA_BUFFER_COUNT, sizeof(*inst->gbar.extra_buffer));
+               if (!inst->gbar.extra_buffer) {
+                       ErrPrint("Failed to allocate buffer for gbar extra buffer\n");
+               }
+       }
+       instance_ref(inst);
+
+       if (package_add_instance(inst->info, inst) < 0) {
+               DbgFree(inst->widget.extra_buffer);
+               DbgFree(inst->gbar.extra_buffer);
+               unfork_package(inst);
+               (void)client_unref(inst->client);
+               DbgFree(inst->title);
+               DbgFree(inst->category);
+               DbgFree(inst->cluster);
+               DbgFree(inst->content);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       slave_load_instance(package_slave(inst->info));
+
+       /**
+        * @note
+        * Before activate an instance,
+        * Update its Id first for client.
+        */
+       instance_send_update_id(inst);
+
+       if (instance_activate(inst) < 0) {
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_FAULT);
+               inst = NULL;
+       } else {
+               inst->visible = WIDGET_HIDE_WITH_PAUSE;
+       }
+
+       return inst;
+}
+
+HAPI struct packet *instance_duplicate_packet_create(const struct packet *packet, struct inst_info *inst, struct pkg_info *info, int width, int height)
+{
+       struct packet *result;
+
+       /**
+        * Do not touch the "timestamp".
+        * It is assigned by Viewer, and will be used as a key to find this instance
+        */
+       instance_set_widget_size(inst, width, height);
+
+       /**
+        * @TODO
+        */
+       DbgPrint("[TODO] Instance package info: %p:%s (%p:%s)\n", inst->info, package_name(inst->info), info, package_name(info));
+       // inst->info = info;
+
+       inst->unicast_delete_event = 1;
+       result = packet_create_reply(packet, "sssiidssisiis",
+                       package_name(inst->info),
+                       inst->id,
+                       inst->content,
+                       package_timeout(inst->info),
+                       !!package_widget_path(inst->info),
+                       inst->widget.period,
+                       inst->cluster,
+                       inst->category,
+                       !!inst->client,
+                       package_abi(inst->info),
+                       inst->widget.width,
+                       inst->widget.height,
+                       client_direct_addr(inst->client));
+       if (!result) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return NULL;
+       }
+
+       /**
+        * @note
+        * Is this really necessary?
+       inst->requested_state = INST_ACTIVATED;
+        */
+       DbgPrint("[TODO] Instance request_state is not touched\n");
+       inst->changing_state--;
+
+       inst->visible = WIDGET_HIDE_WITH_PAUSE;
+       inst->state = INST_ACTIVATED;
+
+       instance_create_widget_buffer(inst, WIDGET_CONF_DEFAULT_PIXELS);
+       instance_broadcast_created_event(inst);
+       instance_thaw_updator(inst);
+
+       instance_unref(inst);
+
+       return result;
+}
+
+HAPI struct packet *instance_watch_create(const struct packet *packet, const char *pkgname, int width, int height)
+{
+       struct inst_info *inst;
+       struct packet *result;
+
+       inst = calloc(1, sizeof(*inst));
+       if (!inst) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       /**
+        * @note
+        * This timestamp will not be used by viewer.
+        * So we can set it manually from here.
+        */
+       inst->timestamp = util_timestamp();
+       inst->widget.width = width;
+       inst->widget.height = height;
+
+       if (fork_package(inst, pkgname) < 0) {
+               DbgFree(inst);
+               return NULL;
+       }
+
+       if (WIDGET_CONF_EXTRA_BUFFER_COUNT) {
+               inst->widget.extra_buffer = calloc(WIDGET_CONF_EXTRA_BUFFER_COUNT, sizeof(*inst->widget.extra_buffer));
+               if (!inst->widget.extra_buffer) {
+                       ErrPrint("Failed to allocate buffer for widget extra buffer\n");
+               }
+
+               inst->gbar.extra_buffer = calloc(WIDGET_CONF_EXTRA_BUFFER_COUNT, sizeof(*inst->gbar.extra_buffer));
+               if (!inst->gbar.extra_buffer) {
+                       ErrPrint("Failed to allocate buffer for gbar extra buffer\n");
+               }
+       }
+
+       /**
+        * We don't need to set the state of this instance from here.
+        * But, in case of fail to add a new instance to package information(inst->info) data,
+        * the "instance_destroy" will be invoked.
+        * it will try to get the state of an instance.
+        */
+       inst->state = INST_INIT;
+       inst->requested_state = INST_INIT;
+       instance_ref(inst);
+
+       /**
+        * @note
+        * We already create a slave object from caller.
+        * This will finds it and map it to package information object
+        */
+       if (package_add_instance(inst->info, inst) < 0) {
+               ErrPrint("Failed to package_add_instance\n");
+               unfork_package(inst);
+               DbgFree(inst->widget.extra_buffer);
+               DbgFree(inst->gbar.extra_buffer);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       /**
+        * Before activate an instance, update its id first for client
+       instance_send_update_id(inst);
+        */
+       DbgPrint("[TODO] send_update_id\n");
+
+       result = packet_create_reply(packet, "sssiidssisiis",
+                       package_name(inst->info),
+                       inst->id,
+                       inst->content,
+                       package_timeout(inst->info),
+                       !!package_widget_path(inst->info),
+                       inst->widget.period,
+                       inst->cluster,
+                       inst->category,
+                       !!inst->client,
+                       package_abi(inst->info),
+                       inst->widget.width,
+                       inst->widget.height,
+                       client_direct_addr(inst->client));
+
+       if (!result) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               package_del_instance(inst->info, inst); /* This will reset the inst->info->slave */
+               unfork_package(inst);
+               DbgFree(inst->widget.extra_buffer);
+               DbgFree(inst->gbar.extra_buffer);
+               DbgFree(inst);
+               return NULL;
+       }
+
+       slave_load_instance(package_slave(inst->info));
+
+       inst->requested_state = INST_ACTIVATED;
+       inst->state = INST_ACTIVATED;
+
+       inst->visible = WIDGET_HIDE_WITH_PAUSE;
+
+       instance_create_widget_buffer(inst, WIDGET_CONF_DEFAULT_PIXELS);
+       instance_broadcast_created_event(inst);
+       instance_thaw_updator(inst);
+
+       return result;
+}
+
+HAPI struct inst_info *instance_ref(struct inst_info *inst)
+{
+       if (!inst) {
+               return NULL;
+       }
+
+       inst->refcnt++;
+       return inst;
+}
+
+HAPI struct inst_info *instance_unref(struct inst_info *inst)
+{
+       if (!inst) {
+               return NULL;
+       }
+
+       if (inst->refcnt == 0) {
+               ErrPrint("Instance refcnt is not valid\n");
+               return NULL;
+       }
+
+       inst->refcnt--;
+       if (inst->refcnt == 0) {
+               destroy_instance(inst);
+               inst = NULL;
+       }
+
+       return inst;
+}
+
+static void deactivate_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct inst_info *inst = data;
+       int ret;
+
+       /*!
+        * \note
+        * In this callback, we cannot trust the "client" information.
+        * It could be cleared before reach to here.
+        */
+
+       if (!packet) {
+               /*!
+                * \note
+                * The instance_reload will care this.
+                * And it will be called from the slave activate callback.
+                */
+               goto out;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       DbgPrint("[%s] %d (0x%X)\n", inst->id, ret, inst->state);
+
+       if (inst->state == INST_DESTROYED) {
+               /*!
+                * \note
+                * Already destroyed.
+                * Do nothing at here anymore.
+                */
+               goto out;
+       }
+
+       switch (ret) {
+       case 0:
+               /*!
+                * \note
+                * Successfully unloaded
+                */
+               switch (inst->requested_state) {
+               case INST_ACTIVATED:
+                       instance_state_reset(inst);
+                       instance_reactivate(inst);
+                       break;
+               case INST_DESTROYED:
+                       if (inst->unicast_delete_event) {
+                               instance_unicast_deleted_event(inst, NULL, ret);
+                       } else {
+                               instance_broadcast_deleted_event(inst, ret);
+                       }
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+               default:
+                       /*!< Unable to reach here */
+                       break;
+               }
+
+               break;
+       case WIDGET_ERROR_INVALID_PARAMETER:
+               /*!
+                * \note
+                * Slave has no instance of this package.
+                */
+       case WIDGET_ERROR_NOT_EXIST:
+               /*!
+                * \note
+                * This instance's previous state is only can be the INST_ACTIVATED.
+                * So we should care the slave_unload_instance from here.
+                * And we should send notification to clients, about this is deleted.
+                */
+               /*!
+                * \note
+                * Slave has no instance of this.
+                * In this case, ignore the requested_state
+                * Because, this instance is already met a problem.
+                */
+       default:
+               /*!
+                * \note
+                * Failed to unload this instance.
+                * This is not possible, slave will always return WIDGET_ERROR_NOT_EXIST, WIDGET_ERROR_INVALID_PARAMETER, or 0.
+                * but care this exceptional case.
+                */
+               instance_broadcast_deleted_event(inst, ret);
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+               break;
+       }
+
+out:
+       inst->changing_state--;
+       if (inst->changing_state < 0) {
+               ErrPrint("Changing state is not correct: %d\n", inst->changing_state);
+       }
+       instance_unref(inst);
+}
+
+static void reactivate_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct inst_info *inst = data;
+       enum widget_widget_type widget_type;
+       enum widget_gbar_type gbar_type;
+       int ret;
+       const char *content;
+       const char *title;
+       int is_pinned_up;
+
+       if (!packet) {
+               /*!
+                * \note
+                * instance_reload function will care this.
+                * and it will be called from the slave_activate callback
+                */
+               goto out;
+       }
+
+       if (packet_get(packet, "issi", &ret, &content, &title, &is_pinned_up) != 4) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       if (strlen(content)) {
+               char *tmp;
+
+               tmp = strdup(content);
+               if (!tmp) {
+                       ErrPrint("strdup: %d\n", errno);
+                       goto out;
+               }
+
+               DbgFree(inst->content);
+               inst->content = tmp;
+       }
+
+       if (strlen(title)) {
+               char *tmp;
+
+               tmp = strdup(title);
+               if (!tmp) {
+                       ErrPrint("strdup: %d\n", errno);
+                       goto out;
+               }
+
+               DbgFree(inst->title);
+               inst->title = tmp;
+       }
+
+       if (inst->state == INST_DESTROYED) {
+               /*!
+                * \note
+                * Already destroyed.
+                * Do nothing at here anymore.
+                */
+               goto out;
+       }
+
+       switch (ret) {
+       case 0: /*!< normally created */
+               inst->state = INST_ACTIVATED;
+               switch (inst->requested_state) {
+               case INST_DESTROYED:
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               case INST_ACTIVATED:
+                       inst->is_pinned_up = is_pinned_up;
+                       widget_type = package_widget_type(inst->info);
+                       gbar_type = package_gbar_type(inst->info);
+
+                       /*!
+                        * \note
+                        * Optimization point.
+                        *   In case of the BUFFER type,
+                        *   the slave will request the buffer to render its contents.
+                        *   so the buffer will be automatcially recreated when it gots the
+                        *   buffer request packet.
+                        *   so load a buffer from here is not neccessary.
+                        *   I should to revise it and concrete the concept.
+                        *   Just leave it only for now.
+                        */
+
+                       if (widget_type == WIDGET_TYPE_SCRIPT && inst->widget.canvas.script) {
+                               script_handler_load(inst->widget.canvas.script, 0);
+                       } else if (widget_type == WIDGET_TYPE_BUFFER && inst->widget.canvas.buffer) {
+                               buffer_handler_load(inst->widget.canvas.buffer);
+                       }
+
+                       if (gbar_type == GBAR_TYPE_SCRIPT && inst->gbar.canvas.script && inst->gbar.is_opened_for_reactivate) {
+                               double x, y;
+                               /*!
+                                * \note
+                                * We should to send a request to open a GBAR to slave.
+                                * if we didn't send it, the slave will not recognize the state of a GBAR.
+                                * We have to keep the view of GBAR seamless even if the widget is reactivated.
+                                * To do that, send open request from here.
+                                */
+                               ret = instance_slave_open_gbar(inst, NULL);
+                               instance_slave_get_gbar_pos(inst, &x, &y);
+
+                               /*!
+                                * \note
+                                * In this case, master already loads the GBAR script.
+                                * So just send the gbar,show event to the slave again.
+                                */
+                               ret = instance_signal_emit(inst, "gbar,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
+                       } else if (gbar_type == GBAR_TYPE_BUFFER && inst->gbar.canvas.buffer && inst->gbar.is_opened_for_reactivate) {
+                               double x, y;
+
+                               buffer_handler_load(inst->gbar.canvas.buffer);
+                               instance_slave_get_gbar_pos(inst, &x, &y);
+
+                               /*!
+                                * \note
+                                * We should to send a request to open a GBAR to slave.
+                                * if we didn't send it, the slave will not recognize the state of a GBAR.
+                                * We have to keep the view of GBAR seamless even if the widget is reactivated.
+                                * To do that, send open request from here.
+                                */
+                               ret = instance_slave_open_gbar(inst, NULL);
+
+                               /*!
+                                * \note
+                                * In this case, just send the gbar,show event for keeping the compatibility
+                                */
+                               ret = instance_signal_emit(inst, "gbar,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
+                       }
+
+                       /*!
+                        * \note
+                        * After create an instance again,
+                        * Send resize request to the widget.
+                        * instance_resize(inst, inst->widget.width, inst->widget.height);
+                        *
+                        * renew request will resize the widget while creating it again
+                        */
+
+                       /*!
+                        * \note
+                        * This function will check the visiblity of a widget and
+                        * make decision whether it thaw the update timer or not.
+                        */
+                       instance_recover_visible_state(inst);
+               default:
+                       break;
+               }
+               break;
+       default:
+               instance_broadcast_deleted_event(inst, ret);
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+               break;
+       }
+
+out:
+       inst->changing_state--;
+       if (inst->changing_state < 0) {
+               ErrPrint("Changing state is not correct: %d\n", inst->changing_state);
+       }
+       instance_unref(inst);
+}
+
+static void activate_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct inst_info *inst = data;
+       int ret;
+       int w;
+       int h;
+       double priority;
+       char *content;
+       char *title;
+       int is_pinned_up;
+
+       if (!packet) {
+               /*!
+                * \note
+                * instance_reload will care this
+                * it will be called from the slave_activate callback
+                */
+               goto out;
+       }
+
+       if (packet_get(packet, "iiidssi", &ret, &w, &h, &priority, &content, &title, &is_pinned_up) != 7) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       DbgPrint("[%s] returns %d (state: 0x%X)\n", inst->id, ret, inst->state);
+
+       if (inst->state == INST_DESTROYED) {
+               /*!
+                * \note
+                * Already destroyed.
+                * Do nothing at here anymore.
+                */
+               goto out;
+       }
+
+       switch (ret) {
+       case 1: /*!< need to create */
+               if (util_free_space(WIDGET_CONF_IMAGE_PATH) > WIDGET_CONF_MINIMUM_SPACE) {
+                       struct inst_info *new_inst;
+                       new_inst = instance_create(inst->client, util_timestamp(), package_name(inst->info),
+                                       inst->content, inst->cluster, inst->category,
+                                       inst->widget.period, 0, 0);
+                       if (!new_inst) {
+                               ErrPrint("Failed to create a new instance\n");
+                       }
+               } else {
+                       ErrPrint("Not enough space\n");
+               }
+       case 0: /*!< normally created */
+               /*!
+                * \note
+                * Anyway this instance is loaded to the slave,
+                * just increase the loaded instance counter
+                * And then reset jobs.
+                */
+               instance_set_widget_size(inst, w, h);
+               instance_set_widget_info(inst, priority, content, title);
+
+               inst->state = INST_ACTIVATED;
+
+               switch (inst->requested_state) {
+               case INST_DESTROYED:
+                       /**
+                        * In this case, we should destroy the instance.
+                        */
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               case INST_ACTIVATED:
+               default:
+                       /**
+                        * @note
+                        * WIDGET should be created at the create time
+                        */
+                       inst->is_pinned_up = is_pinned_up;
+                       if (package_widget_type(inst->info) == WIDGET_TYPE_SCRIPT) {
+                               if (inst->widget.width == 0 && inst->widget.height == 0) {
+                                       widget_service_get_size(WIDGET_SIZE_TYPE_1x1, &inst->widget.width, &inst->widget.height);
+                               }
+
+                               inst->widget.canvas.script = script_handler_create(inst,
+                                               package_widget_path(inst->info),
+                                               package_widget_group(inst->info),
+                                               inst->widget.width, inst->widget.height);
+
+                               if (!inst->widget.canvas.script) {
+                                       ErrPrint("Failed to create WIDGET\n");
+                               } else {
+                                       script_handler_load(inst->widget.canvas.script, 0);
+                               }
+                       } else if (package_widget_type(inst->info) == WIDGET_TYPE_BUFFER) {
+                               instance_create_widget_buffer(inst, WIDGET_CONF_DEFAULT_PIXELS);
+                       }
+
+                       if (package_gbar_type(inst->info) == GBAR_TYPE_SCRIPT) {
+                               if (inst->gbar.width == 0 && inst->gbar.height == 0) {
+                                       instance_set_gbar_size(inst, package_gbar_width(inst->info), package_gbar_height(inst->info));
+                               }
+
+                               inst->gbar.canvas.script = script_handler_create(inst,
+                                               package_gbar_path(inst->info),
+                                               package_gbar_group(inst->info),
+                                               inst->gbar.width, inst->gbar.height);
+
+                               if (!inst->gbar.canvas.script) {
+                                       ErrPrint("Failed to create GBAR\n");
+                               }
+                       } else if (package_gbar_type(inst->info) == GBAR_TYPE_BUFFER) {
+                               instance_create_gbar_buffer(inst, WIDGET_CONF_DEFAULT_PIXELS);
+                       }
+
+                       instance_broadcast_created_event(inst);
+
+                       instance_thaw_updator(inst);
+                       break;
+               }
+               break;
+       default:
+               instance_unicast_deleted_event(inst, NULL, ret);
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+               break;
+       }
+
+out:
+       inst->changing_state--;
+       if (inst->changing_state < 0) {
+               ErrPrint("Changing state is not correct: %d\n", inst->changing_state);
+       }
+       instance_unref(inst);
+}
+
+HAPI int instance_create_gbar_buffer(struct inst_info *inst, int pixels)
+{
+       if (inst->gbar.width == 0 && inst->gbar.height == 0) {
+               instance_set_gbar_size(inst, package_gbar_width(inst->info), package_gbar_height(inst->info));
+       }
+
+       if (!inst->gbar.canvas.buffer) {
+               inst->gbar.canvas.buffer = buffer_handler_create(inst, s_info.env_buf_type, inst->gbar.width, inst->gbar.height, pixels);
+               if (!inst->gbar.canvas.buffer) {
+                       ErrPrint("Failed to create GBAR Buffer\n");
+               }
+       }
+
+       return !!inst->gbar.canvas.buffer;
+}
+
+HAPI int instance_create_gbar_extra_buffer(struct inst_info *inst, int pixels, int idx)
+{
+       if (!inst->gbar.extra_buffer) {
+               return 0;
+       }
+
+       if (inst->gbar.width == 0 && inst->gbar.height == 0) {
+               instance_set_gbar_size(inst, package_gbar_width(inst->info), package_gbar_height(inst->info));
+       }
+
+       if (!inst->gbar.extra_buffer[idx]) {
+               inst->gbar.extra_buffer[idx] = buffer_handler_create(inst, s_info.env_buf_type, inst->gbar.width, inst->gbar.height, pixels);
+               if (!inst->gbar.extra_buffer[idx]) {
+                       ErrPrint("Failed to create GBAR Extra Buffer\n");
+               }
+       }
+
+       return !!inst->gbar.extra_buffer[idx];
+}
+
+HAPI int instance_create_widget_buffer(struct inst_info *inst, int pixels)
+{
+       if (inst->widget.width == 0 && inst->widget.height == 0) {
+               widget_service_get_size(WIDGET_SIZE_TYPE_1x1, &inst->widget.width, &inst->widget.height);
+       }
+
+       if (!inst->widget.canvas.buffer) {
+               /*!
+                * \note
+                * Slave doesn't call the acquire_buffer.
+                * In this case, create the buffer from here.
+                */
+               inst->widget.canvas.buffer = buffer_handler_create(inst, s_info.env_buf_type, inst->widget.width, inst->widget.height, pixels);
+               if (!inst->widget.canvas.buffer) {
+                       ErrPrint("Failed to create WIDGET\n");
+               }
+       }
+
+       return !!inst->widget.canvas.buffer;
+}
+
+HAPI int instance_create_widget_extra_buffer(struct inst_info *inst, int pixels, int idx)
+{
+       if (!inst->widget.extra_buffer) {
+               return 0;
+       }
+
+       if (inst->widget.width == 0 && inst->widget.height == 0) {
+               widget_service_get_size(WIDGET_SIZE_TYPE_1x1, &inst->widget.width, &inst->widget.height);
+       }
+
+       if (!inst->widget.extra_buffer[idx]) {
+               inst->widget.extra_buffer[idx] = buffer_handler_create(inst, s_info.env_buf_type, inst->widget.width, inst->widget.height, pixels);
+               if (!inst->widget.extra_buffer[idx]) {
+                       ErrPrint("Failed to create widget Extra buffer\n");
+               }
+       }
+
+       return !!inst->widget.extra_buffer[idx];
+}
+
+HAPI int instance_destroy(struct inst_info *inst, widget_destroy_type_e type)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_DELETE;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (inst->state) {
+       case INST_REQUEST_TO_ACTIVATE:
+       case INST_REQUEST_TO_DESTROY:
+       case INST_REQUEST_TO_REACTIVATE:
+               inst->requested_state = INST_DESTROYED;
+               return WIDGET_ERROR_NONE;
+       case INST_INIT:
+               inst->state = INST_DESTROYED;
+               inst->requested_state = INST_DESTROYED;
+               (void)instance_unref(inst);
+               return WIDGET_ERROR_NONE;
+       case INST_DESTROYED:
+               inst->requested_state = INST_DESTROYED;
+               return WIDGET_ERROR_NONE;
+       default:
+               break;
+       }
+
+       packet = packet_create((const char *)&cmd, "ssi", package_name(inst->info), inst->id, type);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       inst->destroy_type = type;
+       inst->requested_state = INST_DESTROYED;
+       inst->state = INST_REQUEST_TO_DESTROY;
+       inst->changing_state++;
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, deactivate_cb, instance_ref(inst), 0);
+}
+
+HAPI int instance_reload(struct inst_info *inst, widget_destroy_type_e type)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_DELETE;
+       int ret;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("Reload instance (%s)\n", instance_id(inst));
+
+       switch (inst->state) {
+       case INST_REQUEST_TO_ACTIVATE:
+       case INST_REQUEST_TO_REACTIVATE:
+               return WIDGET_ERROR_NONE;
+       case INST_INIT:
+               ret = instance_activate(inst);
+               if (ret < 0) {
+                       ErrPrint("Failed to activate instance: %d (%s)\n", ret, instance_id(inst));
+               }
+               return WIDGET_ERROR_NONE;
+       case INST_DESTROYED:
+       case INST_REQUEST_TO_DESTROY:
+               DbgPrint("Instance is destroying now\n");
+               return WIDGET_ERROR_NONE;
+       default:
+               break;
+       }
+
+       packet = packet_create((const char *)&cmd, "ssi", package_name(inst->info), inst->id, type);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       inst->destroy_type = type;
+       inst->requested_state = INST_ACTIVATED;
+       inst->state = INST_REQUEST_TO_DESTROY;
+       inst->changing_state++;
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, deactivate_cb, instance_ref(inst), 0);
+}
+
+/* Client Deactivated Callback */
+static int gbar_buffer_close_cb(struct client_node *client, void *inst)
+{
+       int ret;
+
+       ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+       if (ret < 0) {
+               DbgPrint("Forcely close the GBAR ret: %d\n", ret);
+       }
+
+       instance_unref(inst);
+
+       return -1; /* Delete this callback */
+}
+
+/* Client Deactivated Callback */
+static int gbar_script_close_cb(struct client_node *client, void *inst)
+{
+       int ret;
+
+       ret = script_handler_unload(instance_gbar_script(inst), 1);
+       if (ret < 0) {
+               DbgPrint("Unload script: %d\n", ret);
+       }
+
+       ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+       if (ret < 0) {
+               DbgPrint("Forcely close the GBAR ret: %d\n", ret);
+       }
+
+       instance_unref(inst);
+
+       return -1; /* Delete this callback */
+}
+
+static inline void release_resource_for_closing_gbar(struct pkg_info *info, struct inst_info *inst, struct client_node *client)
+{
+       if (!client) {
+               client = inst->gbar.owner;
+               if (!client) {
+                       return;
+               }
+       }
+
+       /*!
+        * \note
+        * Clean up the resources
+        */
+       if (package_gbar_type(info) == GBAR_TYPE_BUFFER) {
+               if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, gbar_buffer_close_cb, inst) == 0) {
+                       /*!
+                        * \note
+                        * Only if this function succeed to remove the gbar_buffer_close_cb,
+                        * Decrease the reference count of this instance
+                        */
+                       instance_unref(inst);
+               }
+       } else if (package_gbar_type(info) == GBAR_TYPE_SCRIPT) {
+               if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, gbar_script_close_cb, inst) == 0) {
+                       /*!
+                        * \note
+                        * Only if this function succeed to remove the script_close_cb,
+                        * Decrease the reference count of this instance
+                        */
+                       instance_unref(inst);
+               }
+       } else {
+               ErrPrint("Unknown GBAR type\n");
+       }
+
+}
+
+HAPI int instance_state_reset(struct inst_info *inst)
+{
+       enum widget_widget_type widget_type;
+       enum widget_gbar_type gbar_type;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (inst->state == INST_DESTROYED) {
+               goto out;
+       }
+
+       widget_type = package_widget_type(inst->info);
+       gbar_type = package_gbar_type(inst->info);
+
+       if (widget_type == WIDGET_TYPE_SCRIPT && inst->widget.canvas.script) {
+               script_handler_unload(inst->widget.canvas.script, 0);
+       } else if (widget_type == WIDGET_TYPE_BUFFER && inst->widget.canvas.buffer) {
+               buffer_handler_unload(inst->widget.canvas.buffer);
+       }
+
+       if (gbar_type == GBAR_TYPE_SCRIPT && inst->gbar.canvas.script) {
+               inst->gbar.is_opened_for_reactivate = script_handler_is_loaded(inst->gbar.canvas.script);
+               release_resource_for_closing_gbar(instance_package(inst), inst, NULL);
+               script_handler_unload(inst->gbar.canvas.script, 1);
+       } else if (gbar_type == GBAR_TYPE_BUFFER && inst->gbar.canvas.buffer) {
+               inst->gbar.is_opened_for_reactivate = buffer_handler_is_loaded(inst->gbar.canvas.buffer);
+               release_resource_for_closing_gbar(instance_package(inst), inst, NULL);
+               buffer_handler_unload(inst->gbar.canvas.buffer);
+       }
+
+out:
+       inst->state = INST_INIT;
+       inst->requested_state = INST_INIT;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int instance_reactivate(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_RENEW;
+       int ret;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       switch (inst->state) {
+       case INST_REQUEST_TO_DESTROY:
+       case INST_REQUEST_TO_ACTIVATE:
+       case INST_REQUEST_TO_REACTIVATE:
+               inst->requested_state = INST_ACTIVATED;
+               return WIDGET_ERROR_NONE;
+       case INST_DESTROYED:
+       case INST_ACTIVATED:
+               return WIDGET_ERROR_NONE;
+       case INST_INIT:
+       default:
+               break;
+       }
+
+       packet = packet_create((const char *)&cmd, "sssiidssiisiisi",
+                       package_name(inst->info),
+                       inst->id,
+                       inst->content,
+                       package_timeout(inst->info),
+                       !!package_widget_path(inst->info),
+                       inst->widget.period,
+                       inst->cluster,
+                       inst->category,
+                       inst->widget.width, inst->widget.height,
+                       package_abi(inst->info),
+                       inst->scroll_locked,
+                       inst->active_update,
+                       client_direct_addr(inst->client),
+                       inst->orientation);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = slave_activate(package_slave(inst->info));
+       if (ret < 0 && ret != WIDGET_ERROR_ALREADY_STARTED) {
+               /*!
+                * \note
+                * If the master failed to launch the slave,
+                * Do not send any requests to the slave.
+                */
+               ErrPrint("Failed to launch the slave\n");
+               packet_destroy(packet);
+               return ret;
+       }
+
+       inst->requested_state = INST_ACTIVATED;
+       inst->state = INST_REQUEST_TO_REACTIVATE;
+       inst->changing_state++;
+
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, reactivate_cb, instance_ref(inst), 1);
+}
+
+HAPI int instance_activate(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_NEW;
+       int ret;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       switch (inst->state) {
+       case INST_REQUEST_TO_REACTIVATE:
+       case INST_REQUEST_TO_ACTIVATE:
+       case INST_REQUEST_TO_DESTROY:
+               inst->requested_state = INST_ACTIVATED;
+               return WIDGET_ERROR_NONE;
+       case INST_ACTIVATED:
+       case INST_DESTROYED:
+               return WIDGET_ERROR_NONE;
+       case INST_INIT:
+       default:
+               break;
+       }
+
+       packet = packet_create((const char *)&cmd, "sssiidssisiisi",
+                       package_name(inst->info),
+                       inst->id,
+                       inst->content,
+                       package_timeout(inst->info),
+                       !!package_widget_path(inst->info),
+                       inst->widget.period,
+                       inst->cluster,
+                       inst->category,
+                       !!inst->client,
+                       package_abi(inst->info),
+                       inst->widget.width,
+                       inst->widget.height,
+                       client_direct_addr(inst->client),
+                       inst->orientation);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = slave_activate(package_slave(inst->info));
+       if (ret < 0 && ret != WIDGET_ERROR_ALREADY_STARTED) {
+               /*!
+                * \note
+                * If the master failed to launch the slave,
+                * Do not send any requests to the slave.
+                */
+               ErrPrint("Failed to launch the slave\n");
+               packet_destroy(packet);
+               return ret;
+       }
+
+       inst->state = INST_REQUEST_TO_ACTIVATE;
+       inst->requested_state = INST_ACTIVATED;
+       inst->changing_state++;
+
+       /*!
+        * \note
+        * Try to activate a slave if it is not activated
+        */
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, activate_cb, instance_ref(inst), 1);
+}
+
+HAPI int instance_widget_update_begin(struct inst_info *inst, double priority, const char *content, const char *title)
+{
+       struct packet *packet;
+       const char *fbfile;
+       unsigned int cmd = CMD_WIDGET_UPDATE_BEGIN;
+
+       if (!inst->active_update) {
+               ErrPrint("Invalid request [%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (package_widget_type(inst->info)) {
+       case WIDGET_TYPE_BUFFER:
+               if (!inst->widget.canvas.buffer) {
+                       ErrPrint("Buffer is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               fbfile = buffer_handler_id(inst->widget.canvas.buffer);
+               break;
+       case WIDGET_TYPE_SCRIPT:
+               if (!inst->widget.canvas.script) {
+                       ErrPrint("Script is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               fbfile = script_handler_buffer_id(inst->widget.canvas.script);
+               break;
+       default:
+               ErrPrint("Invalid request[%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdsss", package_name(inst->info), inst->id, priority, content, title, fbfile);
+       if (!packet) {
+               ErrPrint("Unable to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_widget_update_end(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_WIDGET_UPDATE_END;
+
+       if (!inst->active_update) {
+               ErrPrint("Invalid request [%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (package_widget_type(inst->info)) {
+       case WIDGET_TYPE_BUFFER:
+               if (!inst->widget.canvas.buffer) {
+                       ErrPrint("Buffer is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               break;
+       case WIDGET_TYPE_SCRIPT:
+               if (!inst->widget.canvas.script) {
+                       ErrPrint("Script is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               break;
+       default:
+               ErrPrint("Invalid request[%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ss", package_name(inst->info), inst->id);
+       if (!packet) {
+               ErrPrint("Unable to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_gbar_update_begin(struct inst_info *inst)
+{
+       struct packet *packet;
+       const char *fbfile;
+       unsigned int cmd = CMD_GBAR_UPDATE_BEGIN;
+
+       if (!inst->active_update) {
+               ErrPrint("Invalid request [%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (package_gbar_type(inst->info)) {
+       case GBAR_TYPE_BUFFER:
+               if (!inst->gbar.canvas.buffer) {
+                       ErrPrint("Buffer is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               fbfile = buffer_handler_id(inst->gbar.canvas.buffer);
+               break;
+       case GBAR_TYPE_SCRIPT:
+               if (!inst->gbar.canvas.script) {
+                       ErrPrint("Script is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               fbfile = script_handler_buffer_id(inst->gbar.canvas.script);
+               break;
+       default:
+               ErrPrint("Invalid request[%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sss", package_name(inst->info), inst->id, fbfile);
+       if (!packet) {
+               ErrPrint("Unable to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_gbar_update_end(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_GBAR_UPDATE_END;
+
+       if (!inst->active_update) {
+               ErrPrint("Invalid request [%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (package_gbar_type(inst->info)) {
+       case GBAR_TYPE_BUFFER:
+               if (!inst->gbar.canvas.buffer) {
+                       ErrPrint("Buffer is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               break;
+       case GBAR_TYPE_SCRIPT:
+               if (!inst->gbar.canvas.script) {
+                       ErrPrint("Script is null [%s]\n", inst->id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+               break;
+       default:
+               ErrPrint("Invalid request[%s]\n", inst->id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ss", package_name(inst->info), inst->id);
+       if (!packet) {
+               ErrPrint("Unable to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_extra_info_updated_by_instance(struct inst_info *inst)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_EXTRA_INFO;
+
+       packet = packet_create_noack((const char *)&cmd, "ssssssd", package_name(inst->info), inst->id,
+                       inst->content, inst->title,
+                       inst->icon, inst->name,
+                       inst->widget.priority);
+       if (!packet) {
+               ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
+               return;
+       }
+
+       (void)client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_extra_updated_by_instance(struct inst_info *inst, int is_gbar, int idx, int x, int y, int w, int h)
+{
+       struct packet *packet;
+       enum widget_widget_type widget_type;
+       unsigned int cmd = CMD_EXTRA_UPDATED;
+
+       if (idx < 0 || idx > WIDGET_CONF_EXTRA_BUFFER_COUNT) {
+               ErrPrint("Invalid index\n");
+               return;
+       }
+
+       if (is_gbar == 0) {
+               if (!inst->widget.extra_buffer || inst->widget.extra_buffer[idx] == 0u) {
+                       ErrPrint("Invalid extra buffer\n");
+                       return;
+               }
+       } else {
+               if (!inst->gbar.extra_buffer || inst->gbar.extra_buffer[idx] == 0u) {
+                       ErrPrint("Invalid extra buffer\n");
+                       return;
+               }
+       }
+
+       if (inst->client && inst->visible != WIDGET_SHOW) {
+               if (inst->visible == WIDGET_HIDE) {
+                       DbgPrint("Ignore update event %s(HIDE)\n", inst->id);
+                       return;
+               }
+       }
+
+       widget_type = package_widget_type(inst->info);
+       if (widget_type != WIDGET_TYPE_BUFFER) {
+               ErrPrint("Unsupported type\n");
+               return;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssiiiiii", package_name(inst->info), inst->id, is_gbar, idx, x, y, w, h);
+       if (!packet) {
+               ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
+               return;
+       }
+
+       (void)client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_widget_updated_by_instance(struct inst_info *inst, const char *safe_file, int x, int y, int w, int h)
+{
+       struct packet *packet;
+       const char *id = NULL;
+       enum widget_widget_type widget_type;
+       unsigned int cmd = CMD_WIDGET_UPDATED;
+
+       if (inst->client && inst->visible != WIDGET_SHOW) {
+               if (inst->visible == WIDGET_HIDE) {
+                       DbgPrint("Ignore update event %s(HIDE)\n", inst->id);
+                       return;
+               }
+       }
+
+       widget_type = package_widget_type(inst->info);
+       if (widget_type == WIDGET_TYPE_SCRIPT) {
+               id = script_handler_buffer_id(inst->widget.canvas.script);
+       } else if (widget_type == WIDGET_TYPE_BUFFER) {
+               id = buffer_handler_id(inst->widget.canvas.buffer);
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssssiiii", package_name(inst->info), inst->id, id, safe_file, x, y, w, h);
+       if (!packet) {
+               ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
+               return;
+       }
+
+       (void)client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_hold_scroll(struct inst_info *inst, int hold)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_SCROLL;
+
+       DbgPrint("HOLD: (%s) %d\n", inst->id, hold);
+       if (inst->scroll_locked == hold) {
+               return WIDGET_ERROR_ALREADY_EXIST;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", package_name(inst->info), inst->id, hold);
+       if (!packet) {
+               ErrPrint("Failed to build a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       inst->scroll_locked = hold;
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_gbar_updated_by_instance(struct inst_info *inst, const char *descfile, int x, int y, int w, int h)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_GBAR_UPDATED;
+       const char *id;
+
+       if (inst->client && inst->visible != WIDGET_SHOW) {
+               DbgPrint("widget is hidden. ignore update event\n");
+               return;
+       }
+
+       if (!inst->gbar.need_to_send_close_event) {
+               DbgPrint("GBAR is not created yet. Ignore update event - %s\n", descfile);
+
+               if (inst->gbar.pended_update_desc) {
+                       DbgFree(inst->gbar.pended_update_desc);
+                       inst->gbar.pended_update_desc = NULL;
+               }
+
+               if (descfile) {
+                       inst->gbar.pended_update_desc = strdup(descfile);
+                       if (!inst->gbar.pended_update_desc) {
+                               ErrPrint("strdup: %d\n", errno);
+                       }
+               }
+
+               inst->gbar.pended_update_cnt++;
+               return;
+       }
+
+       if (!descfile) {
+               descfile = inst->id;
+       }
+
+       switch (package_gbar_type(inst->info)) {
+       case GBAR_TYPE_SCRIPT:
+               id = script_handler_buffer_id(inst->gbar.canvas.script);
+               break;
+       case GBAR_TYPE_BUFFER:
+               id = buffer_handler_id(inst->gbar.canvas.buffer);
+               break;
+       case GBAR_TYPE_TEXT:
+       default:
+               id = "";
+               break;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssssiiii", package_name(inst->info), inst->id, id, descfile, x, y, w, h);
+       if (!packet) {
+               ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
+               return;
+       }
+
+       (void)client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_gbar_updated(const char *pkgname, const char *id, const char *descfile, int x, int y, int w, int h)
+{
+       struct inst_info *inst;
+
+       inst = package_find_instance_by_id(pkgname, id);
+       if (!inst) {
+               return;
+       }
+
+       instance_gbar_updated_by_instance(inst, descfile, x, y, w, h);
+}
+
+HAPI int instance_set_update_mode(struct inst_info *inst, int active_update)
+{
+       struct packet *packet;
+       struct update_mode_cbdata *cbdata;
+       unsigned int cmd = CMD_UPDATE_MODE;
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (inst->active_update == active_update) {
+               DbgPrint("Active update is not changed: %d\n", inst->active_update);
+               return WIDGET_ERROR_ALREADY_EXIST;
+       }
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->inst = instance_ref(inst);
+       cbdata->active_update = active_update;
+
+       /* NOTE: param is resued from here */
+       packet = packet_create((const char *)&cmd, "ssi", package_name(inst->info), inst->id, active_update);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, update_mode_cb, cbdata, 0);
+}
+
+HAPI int instance_active_update(struct inst_info *inst)
+{
+       return inst->active_update;
+}
+
+HAPI void instance_set_widget_info(struct inst_info *inst, double priority, const char *content, const char *title)
+{
+       char *_content = NULL;
+       char *_title = NULL;
+
+       if (content && strlen(content)) {
+               _content = strdup(content);
+               if (!_content) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+
+       if (title && strlen(title)) {
+               _title = strdup(title);
+               if (!_title) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+
+       if (_content) {
+               DbgFree(inst->content);
+               inst->content= _content;
+       }
+
+       if (_title) {
+               DbgFree(inst->title);
+               inst->title = _title;
+       }
+
+       if (priority >= 0.0f && priority <= 1.0f) {
+               inst->widget.priority = priority;
+       }
+}
+
+HAPI void instance_set_alt_info(struct inst_info *inst, const char *icon, const char *name)
+{
+       char *_icon = NULL;
+       char *_name = NULL;
+
+       if (icon && strlen(icon)) {
+               _icon = strdup(icon);
+               if (!_icon) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+
+       if (name && strlen(name)) {
+               _name = strdup(name);
+               if (!_name) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+
+       if (_icon) {
+               DbgFree(inst->icon);
+               inst->icon = _icon;
+       }
+
+       if (_name) {
+               DbgFree(inst->name);
+               inst->name = _name;
+       }
+}
+
+HAPI void instance_set_widget_size(struct inst_info *inst, int w, int h)
+{
+       if (inst->widget.width != w || inst->widget.height != h) {
+               instance_send_resized_event(inst, IS_WIDGET, w, h, WIDGET_ERROR_NONE);
+       }
+
+       inst->widget.width = w;
+       inst->widget.height = h;
+}
+
+HAPI void instance_set_gbar_size(struct inst_info *inst, int w, int h)
+{
+       if (inst->gbar.width != w || inst->gbar.height != h) {
+               instance_send_resized_event(inst, IS_GBAR, w, h, WIDGET_ERROR_NONE);
+       }
+
+       inst->gbar.width = w;
+       inst->gbar.height = h;
+}
+
+static void pinup_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct set_pinup_cbdata *cbdata = data;
+       const char *content;
+       struct packet *result;
+       unsigned int cmd = CMD_RESULT_PINUP;
+       int ret;
+
+       if (!packet) {
+               /*!
+                * \todo
+                * Send pinup failed event to client.
+                */
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (packet_get(packet, "is", &ret, &content) != 2) {
+               /*!
+                * \todo
+                * Send pinup failed event to client
+                */
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (ret == 0) {
+               char *new_content;
+
+               new_content = strdup(content);
+               if (!new_content) {
+                       ErrPrint("strdup: %d\n", errno);
+                       /*!
+                        * \note
+                        * send pinup failed event to client
+                        */
+                       ret = WIDGET_ERROR_OUT_OF_MEMORY;
+                       goto out;
+               }
+
+               cbdata->inst->is_pinned_up = cbdata->pinup;
+               DbgFree(cbdata->inst->content);
+
+               cbdata->inst->content = new_content;
+       }
+
+out:
+       /*!
+        * \node
+        * Send PINUP Result to client.
+        * Client should wait this event.
+        */
+       result = packet_create_noack((const char *)&cmd, "iisss", ret, cbdata->inst->is_pinned_up,
+                       package_name(cbdata->inst->info), cbdata->inst->id, cbdata->inst->content);
+       if (result) {
+               (void)client_send_event(cbdata->inst, result, NULL);
+       } else {
+               ErrPrint("Failed to build a packet for %s\n", package_name(cbdata->inst->info));
+       }
+
+       instance_unref(cbdata->inst);
+       DbgFree(cbdata);
+}
+
+HAPI int instance_set_pinup(struct inst_info *inst, int pinup)
+{
+       struct set_pinup_cbdata *cbdata;
+       struct packet *packet;
+       unsigned int cmd = CMD_PINUP;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!package_pinup(inst->info)) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (pinup == inst->is_pinned_up) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->inst = instance_ref(inst);
+       cbdata->pinup = pinup;
+
+       packet = packet_create((const char *)&cmd, "ssi", package_name(inst->info), inst->id, pinup);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, pinup_cb, cbdata, 0);
+}
+
+HAPI int instance_freeze_updator(struct inst_info *inst)
+{
+       if (!inst->update_timer) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       timer_freeze(inst);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int instance_thaw_updator(struct inst_info *inst)
+{
+       if (!inst->update_timer) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (client_is_all_paused() || setting_is_lcd_off()) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (inst->visible == WIDGET_HIDE_WITH_PAUSE) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       timer_thaw(inst);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI enum widget_visible_state instance_visible_state(struct inst_info *inst)
+{
+       return inst->visible;
+}
+
+HAPI int instance_set_visible_state(struct inst_info *inst, enum widget_visible_state state)
+{
+       if (inst->visible == state) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       switch (state) {
+       case WIDGET_SHOW:
+       case WIDGET_HIDE:
+               if (inst->visible == WIDGET_HIDE_WITH_PAUSE) {
+                       if (resume_widget(inst) == 0) {
+                               inst->visible = state;
+                       }
+
+                       instance_thaw_updator(inst);
+               } else {
+                       inst->visible = state;
+               }
+               monitor_multicast_state_change_event(package_name(inst->info), MONITOR_EVENT_RESUMED, instance_id(inst), instance_content(inst));
+               break;
+
+       case WIDGET_HIDE_WITH_PAUSE:
+               if (pause_widget(inst) == 0) {
+                       inst->visible = WIDGET_HIDE_WITH_PAUSE;
+               }
+
+               instance_freeze_updator(inst);
+               monitor_multicast_state_change_event(package_name(inst->info), MONITOR_EVENT_PAUSED, instance_id(inst), instance_content(inst));
+               break;
+
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static void resize_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct resize_cbdata *cbdata = data;
+       int ret;
+
+       if (!packet) {
+               ErrPrint("RESIZE: Invalid packet\n");
+               instance_send_resized_event(cbdata->inst, IS_WIDGET, cbdata->inst->widget.width, cbdata->inst->widget.height, WIDGET_ERROR_FAULT);
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("RESIZE: Invalid parameter\n");
+               instance_send_resized_event(cbdata->inst, IS_WIDGET, cbdata->inst->widget.width, cbdata->inst->widget.height, WIDGET_ERROR_INVALID_PARAMETER);
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return;
+       }
+
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               /*!
+                * \note
+                * else waiting the first update with new size
+                */
+               if (cbdata->inst->widget.width == cbdata->w && cbdata->inst->widget.height == cbdata->h) {
+                       /*!
+                        * \note
+                        * Right after the viewer adds a new widget,
+                        * Box has no size information, then it will try to use the default size,
+                        * After a widget returns created event.
+                        *
+                        * A widget will start to generate default size content.
+                        * But the viewer doesn't know it,.
+                        *
+                        * So the viewer will try to change the size of a widget.
+                        *
+                        * At that time, the provider gots the size changed event from the widget.
+                        * So it sent the size changed event to the viewer.
+                        * But the viewer ignores it. if it doesn't care the size changed event.
+                        * (even if it cares the size changed event, there is a timing issue)
+                        *
+                        * And the provider receives resize request,
+                        * right before send the size changed event.
+                        * but there is no changes about the size.
+                        *
+                        * Now the view will waits size changed event forever.
+                        * To resolve this timing issue.
+                        *
+                        * Check the size of a widget from here.
+                        * And if the size is already updated, send the ALREADY event to the viewer
+                        * to get the size changed event callback correctly.
+                        */
+                       instance_send_resized_event(cbdata->inst, IS_WIDGET, cbdata->inst->widget.width, cbdata->inst->widget.height, WIDGET_ERROR_ALREADY_EXIST);
+                       DbgPrint("RESIZE: widget is already resized [%s - %dx%d]\n", instance_id(cbdata->inst), cbdata->w, cbdata->h);
+               } else {
+                       DbgPrint("RESIZE: Request is successfully sent [%s - %dx%d]\n", instance_id(cbdata->inst), cbdata->w, cbdata->h);
+               }
+       } else {
+               DbgPrint("RESIZE: widget rejects the new size: %s - %dx%d (%d)\n", instance_id(cbdata->inst), cbdata->w, cbdata->h, ret);
+               instance_send_resized_event(cbdata->inst, IS_WIDGET, cbdata->inst->widget.width, cbdata->inst->widget.height, ret);
+       }
+
+       instance_unref(cbdata->inst);
+       DbgFree(cbdata);
+}
+
+HAPI int instance_resize(struct inst_info *inst, int w, int h)
+{
+       struct resize_cbdata *cbdata;
+       struct packet *packet;
+       unsigned int cmd = CMD_RESIZE;
+       int ret;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package: %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->inst = instance_ref(inst);
+       cbdata->w = w;
+       cbdata->h = h;
+
+       /* NOTE: param is resued from here */
+       packet = packet_create((const char *)&cmd, "ssii", package_name(inst->info), inst->id, w, h);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("RESIZE: [%s] resize[%dx%d]\n", instance_id(inst), w, h);
+       ret = slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, resize_cb, cbdata, 0);
+       return ret;
+}
+
+static void set_period_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct period_cbdata *cbdata = data;
+       unsigned int cmd = CMD_PERIOD_CHANGED;
+       struct packet *result;
+       int ret;
+
+       if (!packet) {
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (ret == 0) {
+               cbdata->inst->widget.period = cbdata->period;
+       } else {
+               ErrPrint("Failed to set period %d\n", ret);
+       }
+
+out:
+       result = packet_create_noack((const char *)&cmd, "idss", ret, cbdata->inst->widget.period, package_name(cbdata->inst->info), cbdata->inst->id);
+       if (result) {
+               (void)client_send_event(cbdata->inst, result, NULL);
+       } else {
+               ErrPrint("Failed to build a packet for %s\n", package_name(cbdata->inst->info));
+       }
+
+       instance_unref(cbdata->inst);
+       DbgFree(cbdata);
+       return;
+}
+
+static Eina_Bool timer_updator_cb(void *data)
+{
+       struct period_cbdata *cbdata = data;
+       struct inst_info *inst;
+       struct packet *result;
+       unsigned int cmd = CMD_PERIOD_CHANGED;
+       double period;
+
+       period = cbdata->period;
+       inst = cbdata->inst;
+       DbgFree(cbdata);
+
+       inst->widget.period = period;
+       if (inst->update_timer) {
+               if (inst->widget.period == 0.0f) {
+                       ecore_timer_del(inst->update_timer);
+                       inst->update_timer = NULL;
+               } else {
+                       util_timer_interval_set(inst->update_timer, inst->widget.period);
+               }
+       } else if (inst->widget.period > 0.0f) {
+               inst->update_timer = util_timer_add(inst->widget.period, update_timer_cb, inst);
+               if (!inst->update_timer) {
+                       ErrPrint("Failed to add an update timer for instance %s\n", inst->id);
+               } else {
+                       timer_freeze(inst); /* Freeze the update timer as default */
+               }
+       }
+
+       result = packet_create_noack((const char *)&cmd, "idss", 0, inst->widget.period, package_name(inst->info), inst->id);
+       if (result) {
+               (void)client_send_event(inst, result, NULL);
+       } else {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+       }
+
+       instance_unref(inst);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+HAPI void instance_reload_period(struct inst_info *inst, double period)
+{
+       inst->widget.period = period;
+}
+
+HAPI int instance_set_period(struct inst_info *inst, double period)
+{
+       struct packet *packet;
+       struct period_cbdata *cbdata;
+       unsigned int cmd = CMD_SET_PERIOD;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (period < 0.0f) { /* Use the default period */
+               period = package_period(inst->info);
+       } else if (period > 0.0f && period < WIDGET_CONF_MINIMUM_PERIOD) {
+               period = WIDGET_CONF_MINIMUM_PERIOD; /* defined at conf.h */
+       }
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->period = period;
+       cbdata->inst = instance_ref(inst);
+
+       if (package_secured(inst->info) || (WIDGET_IS_INHOUSE(package_abi(inst->info)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL)) {
+               /*!
+                * \note
+                * Secured widget doesn't need to send its update period to the slave.
+                * Slave has no local timer for updating widgetes
+                *
+                * So update its timer at here.
+                */
+               if (!ecore_timer_add(DELAY_TIME, timer_updator_cb, cbdata)) {
+                       timer_updator_cb(cbdata);
+               }
+               return WIDGET_ERROR_NONE;
+       }
+
+       packet = packet_create((const char *)&cmd, "ssd", package_name(inst->info), inst->id, period);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, set_period_cb, cbdata, 0);
+}
+
+HAPI int instance_clicked(struct inst_info *inst, const char *event, double timestamp, double x, double y)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_CLICKED;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       /* NOTE: param is resued from here */
+       packet = packet_create_noack((const char *)&cmd, "sssddd", package_name(inst->info), inst->id, event, timestamp, x, y);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
+}
+
+HAPI int instance_signal_emit(struct inst_info *inst, const char *signal, const char *part, double sx, double sy, double ex, double ey, double x, double y, int down)
+{
+       const char *pkgname;
+       const char *id;
+       struct slave_node *slave;
+       struct packet *packet;
+       struct pkg_info *pkg;
+       unsigned int cmd = CMD_SCRIPT;
+
+       pkg = instance_package(inst);
+       pkgname = package_name(pkg);
+       id = instance_id(inst);
+       if (!pkgname || !id) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssssddddddi",
+                       pkgname, id,
+                       signal, part,
+                       sx, sy, ex, ey,
+                       x, y, down);
+       if (!packet) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(slave, pkgname, packet, 0); 
+}
+
+HAPI int instance_text_signal_emit(struct inst_info *inst, const char *signal_name, const char *source, double sx, double sy, double ex, double ey)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_TEXT_SIGNAL;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssssdddd", package_name(inst->info), inst->id, signal_name, source, sx, sy, ex, ey);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
+}
+
+static void change_group_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       struct change_group_cbdata *cbdata = data;
+       struct packet *result;
+       unsigned int cmd = CMD_GROUP_CHANGED;
+       int ret;
+
+       if (!packet) {
+               DbgFree(cbdata->cluster);
+               DbgFree(cbdata->category);
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid packet\n");
+               DbgFree(cbdata->cluster);
+               DbgFree(cbdata->category);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (ret == 0) {
+               DbgFree(cbdata->inst->cluster);
+               cbdata->inst->cluster = cbdata->cluster;
+
+               DbgFree(cbdata->inst->category);
+               cbdata->inst->category = cbdata->category;
+       } else {
+               DbgFree(cbdata->cluster);
+               DbgFree(cbdata->category);
+       }
+
+out:
+       result = packet_create_noack((const char *)&cmd, "ssiss",
+                       package_name(cbdata->inst->info), cbdata->inst->id, ret,
+                       cbdata->inst->cluster, cbdata->inst->category);
+       if (!result) {
+               ErrPrint("Failed to build a packet %s\n", package_name(cbdata->inst->info));
+       } else {
+               (void)client_send_event(cbdata->inst, result, NULL);
+       }
+
+       instance_unref(cbdata->inst);
+       DbgFree(cbdata);
+}
+
+HAPI int instance_change_group(struct inst_info *inst, const char *cluster, const char *category)
+{
+       struct packet *packet;
+       struct change_group_cbdata *cbdata;
+       unsigned int cmd = CMD_CHANGE_GROUP;
+
+       if (!inst) {
+               ErrPrint("Invalid instance handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Fault package [%s]\n", package_name(inst->info));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ErrPrint("mlloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->cluster = strdup(cluster);
+       if (!cbdata->cluster) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->category = strdup(category);
+       if (!cbdata->category) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(cbdata->cluster);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->inst = instance_ref(inst);
+
+       packet = packet_create((const char *)&cmd, "ssss", package_name(inst->info), inst->id, cluster, category);
+       if (!packet) {
+               ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
+               instance_unref(cbdata->inst);
+               DbgFree(cbdata->category);
+               DbgFree(cbdata->cluster);
+               DbgFree(cbdata);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, change_group_cb, cbdata, 0);
+}
+
+HAPI const char * const instance_auto_launch(const struct inst_info *inst)
+{
+       return package_auto_launch(inst->info);
+}
+
+HAPI const int const instance_priority(const struct inst_info *inst)
+{
+       return inst->widget.priority;
+}
+
+HAPI const struct client_node *const instance_client(const struct inst_info *inst)
+{
+       return inst->client;
+}
+
+HAPI const int const instance_timeout(const struct inst_info *inst)
+{
+       return package_timeout(inst->info);
+}
+
+HAPI const double const instance_period(const struct inst_info *inst)
+{
+       return inst->widget.period;
+}
+
+HAPI const int const instance_widget_width(const struct inst_info *inst)
+{
+       return inst->widget.width;
+}
+
+HAPI const int const instance_widget_height(const struct inst_info *inst)
+{
+       return inst->widget.height;
+}
+
+HAPI const int const instance_gbar_width(const struct inst_info *inst)
+{
+       return inst->gbar.width;
+}
+
+HAPI const int const instance_gbar_height(const struct inst_info *inst)
+{
+       return inst->gbar.height;
+}
+
+HAPI struct pkg_info *const instance_package(const struct inst_info *inst)
+{
+       return inst->info;
+}
+
+HAPI struct script_info *const instance_widget_script(const struct inst_info *inst)
+{
+       return (package_widget_type(inst->info) == WIDGET_TYPE_SCRIPT) ? inst->widget.canvas.script : NULL;
+}
+
+HAPI struct script_info *const instance_gbar_script(const struct inst_info *inst)
+{
+       return (package_gbar_type(inst->info) == GBAR_TYPE_SCRIPT) ? inst->gbar.canvas.script : NULL;
+}
+
+HAPI struct buffer_info *const instance_widget_buffer(const struct inst_info *inst)
+{
+       return (package_widget_type(inst->info) == WIDGET_TYPE_BUFFER) ? inst->widget.canvas.buffer : NULL;
+}
+
+HAPI struct buffer_info *const instance_gbar_buffer(const struct inst_info *inst)
+{
+       return (package_gbar_type(inst->info) == GBAR_TYPE_BUFFER) ? inst->gbar.canvas.buffer : NULL;
+}
+
+HAPI struct buffer_info *const instance_widget_extra_buffer(const struct inst_info *inst, int idx)
+{
+       return (package_widget_type(inst->info) == WIDGET_TYPE_BUFFER) ? (inst->widget.extra_buffer ? inst->widget.extra_buffer[idx] : NULL) : NULL;
+}
+
+HAPI struct buffer_info *const instance_gbar_extra_buffer(const struct inst_info *inst, int idx)
+{
+       return (package_gbar_type(inst->info) == GBAR_TYPE_BUFFER) ? (inst->gbar.extra_buffer ? inst->gbar.extra_buffer[idx] : NULL) : NULL;
+}
+
+HAPI const char *const instance_id(const struct inst_info *inst)
+{
+       return inst->id;
+}
+
+HAPI const char *const instance_content(const struct inst_info *inst)
+{
+       return inst->content;
+}
+
+HAPI const char *const instance_category(const struct inst_info *inst)
+{
+       return inst->category;
+}
+
+HAPI const char *const instance_cluster(const struct inst_info *inst)
+{
+       return inst->cluster;
+}
+
+HAPI const char * const instance_title(const struct inst_info *inst)
+{
+       return inst->title;
+}
+
+HAPI const double const instance_timestamp(const struct inst_info *inst)
+{
+       return inst->timestamp;
+}
+
+HAPI const enum instance_state const instance_state(const struct inst_info *inst)
+{
+       return inst->state;
+}
+
+HAPI int instance_destroyed(struct inst_info *inst, int reason)
+{
+       switch (inst->state) {
+       case INST_INIT:
+       case INST_REQUEST_TO_ACTIVATE:
+               if (inst->unicast_delete_event) {
+                       /*!
+                        * \note
+                        * No other clients know the existence of this instance,
+                        * only who added this knows it.
+                        * So send deleted event to only it.
+                        */
+                       DbgPrint("Send deleted event - unicast - %p\n", inst->client);
+                       instance_unicast_deleted_event(inst, NULL, reason);
+               } else {
+                       instance_broadcast_deleted_event(inst, reason);
+               }
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+               break;
+       case INST_REQUEST_TO_REACTIVATE:
+       case INST_REQUEST_TO_DESTROY:
+       case INST_ACTIVATED:
+               DbgPrint("Send deleted event - multicast\n");
+               instance_broadcast_deleted_event(inst, reason);
+               instance_state_reset(inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+       case INST_DESTROYED:
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/*!
+ * Invoked when a slave is activated
+ */
+HAPI int instance_recover_state(struct inst_info *inst)
+{
+       int ret = 0;
+
+       if (inst->changing_state > 0) {
+               DbgPrint("Doesn't need to recover the state\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (package_is_fault(inst->info)) {
+               ErrPrint("Package is faulted(%s), Delete it\n", inst->id);
+               inst->requested_state = INST_DESTROYED;
+       }
+
+       switch (inst->state) {
+       case INST_ACTIVATED:
+       case INST_REQUEST_TO_REACTIVATE:
+       case INST_REQUEST_TO_DESTROY:
+               switch (inst->requested_state) {
+               case INST_ACTIVATED:
+                       DbgPrint("Req. to RE-ACTIVATED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       instance_reactivate(inst);
+                       ret = 1;
+                       break;
+               case INST_DESTROYED:
+                       DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case INST_INIT:
+       case INST_REQUEST_TO_ACTIVATE:
+               switch (inst->requested_state) {
+               case INST_ACTIVATED:
+               case INST_INIT:
+                       DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       if (instance_activate(inst) < 0) {
+                               DbgPrint("Failed to reactivate the instance\n");
+                               instance_broadcast_deleted_event(inst, WIDGET_ERROR_FAULT);
+                               instance_state_reset(inst);
+                               instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       } else {
+                               ret = 1;
+                       }
+                       break;
+               case INST_DESTROYED:
+                       DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case INST_DESTROYED:
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+/*!
+ * Invoked when a slave is deactivated
+ */
+HAPI int instance_need_slave(struct inst_info *inst)
+{
+       int ret = 0;
+
+       if (inst->client && client_is_faulted(inst->client)) {
+               /*!
+                * \note
+                * In this case, the client is faulted(disconnected)
+                * when the client is deactivated, its widgetes should be removed too.
+                * So if the current inst is created by the faulted client,
+                * remove it and don't try to recover its states
+                */
+
+               DbgPrint("CLIENT FAULT: Req. to DESTROYED (%s)\n", package_name(inst->info));
+               switch (inst->state) {
+               case INST_INIT:
+               case INST_ACTIVATED:
+               case INST_REQUEST_TO_REACTIVATE:
+               case INST_REQUEST_TO_DESTROY:
+               case INST_REQUEST_TO_ACTIVATE:
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               case INST_DESTROYED:
+                       break;
+               }
+
+               return WIDGET_ERROR_NONE;
+       }
+
+       switch (inst->state) {
+       case INST_ACTIVATED:
+       case INST_REQUEST_TO_REACTIVATE:
+       case INST_REQUEST_TO_DESTROY:
+               switch (inst->requested_state) {
+               case INST_INIT:
+               case INST_ACTIVATED:
+                       DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
+                       ret = 1;
+                       break;
+               case INST_DESTROYED:
+                       DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case INST_INIT:
+       case INST_REQUEST_TO_ACTIVATE:
+               switch (inst->requested_state) {
+               case INST_INIT:
+               case INST_ACTIVATED:
+                       DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
+                       ret = 1;
+                       break;
+               case INST_DESTROYED:
+                       DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case INST_DESTROYED:
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+HAPI int instance_forward_packet(struct inst_info *inst, struct packet *packet)
+{
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_send_key_status(struct inst_info *inst, int status)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_KEY_STATUS;
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", package_name(inst->info), inst->id, status);
+       if (!packet) {
+               ErrPrint("Failed to build a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_send_access_status(struct inst_info *inst, int status)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_ACCESS_STATUS;
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", package_name(inst->info), inst->id, status);
+       if (!packet) {
+               ErrPrint("Failed to build a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI void instance_slave_set_gbar_pos(struct inst_info *inst, double x, double y)
+{
+       inst->gbar.x = x;
+       inst->gbar.y = y;
+}
+
+HAPI void instance_slave_get_gbar_pos(struct inst_info *inst, double *x, double *y)
+{
+       if (x) {
+               *x = inst->gbar.x;
+       }
+
+       if (y) {
+               *y = inst->gbar.y;
+       }
+}
+
+HAPI int instance_slave_open_gbar(struct inst_info *inst, struct client_node *client)
+{
+       const char *pkgname;
+       const char *id;
+       struct packet *packet;
+       struct slave_node *slave;
+       const struct pkg_info *info;
+       unsigned int cmd = CMD_GBAR_SHOW;
+       int ret;
+
+       if (!client) {
+               client = inst->gbar.owner;
+               if (!client) {
+                       ErrPrint("Client is not valid\n");
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       } else if (inst->gbar.owner) {
+               if (inst->gbar.owner != client) {
+                       ErrPrint("Client is already owned\n");
+                       return WIDGET_ERROR_ALREADY_EXIST;
+               }
+       }
+
+       info = instance_package(inst);
+       if (!info) {
+               ErrPrint("No package info\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(info);
+       if (!slave) {
+               ErrPrint("No slave\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       pkgname = package_name(info);
+       id = instance_id(inst);
+
+       if (!pkgname || !id) {
+               ErrPrint("pkgname[%s] id[%s]\n", pkgname, id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssiidd", pkgname, id, instance_gbar_width(inst), instance_gbar_height(inst), inst->gbar.x, inst->gbar.y);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       /*!
+        * \note
+        * Do not return from here even though we failed to freeze the TTL timer.
+        * Because the TTL timer is not able to be exists.
+        * So we can ignore this error.
+        */
+       (void)slave_freeze_ttl(slave);
+
+       DbgPrint("PERF_WIDGET\n");
+       ret = slave_rpc_request_only(slave, pkgname, packet, 0);
+       if (ret < 0) {
+               ErrPrint("Unable to send request to slave\n");
+               /*!
+                * \note
+                * Also we can ignore the TTL timer at here too ;)
+                */
+               (void)slave_thaw_ttl(slave);
+               return ret;
+       }
+
+       /*!
+        * \note
+        * If a client is disconnected, the slave has to close the GBAR
+        * So the gbar_buffer_close_cb/gbar_script_close_cb will catch the disconnection event
+        * then it will send the close request to the slave
+        */
+       if (package_gbar_type(info) == GBAR_TYPE_BUFFER) {
+               instance_ref(inst);
+               if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, gbar_buffer_close_cb, inst) < 0) {
+                       instance_unref(inst);
+               }
+       } else if (package_gbar_type(info) == GBAR_TYPE_SCRIPT) {
+               instance_ref(inst);
+               if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, gbar_script_close_cb, inst) < 0) {
+                       instance_unref(inst);
+               }
+       }
+
+       inst->gbar.owner = client;
+       return ret;
+}
+
+HAPI int instance_slave_close_gbar(struct inst_info *inst, struct client_node *client, int reason)
+{
+       const char *pkgname;
+       const char *id;
+       struct packet *packet;
+       struct slave_node *slave;
+       struct pkg_info *pkg;
+       unsigned int cmd = CMD_GBAR_HIDE;
+       int ret;
+
+       if (inst->gbar.owner != client) {
+               ErrPrint("Has no permission\n");
+               return WIDGET_ERROR_PERMISSION_DENIED;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               ErrPrint("No package info\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("No assigned slave\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       pkgname = package_name(pkg);
+       id = instance_id(inst);
+
+       if (!pkgname || !id) {
+               ErrPrint("pkgname[%s] & id[%s] is not valid\n", pkgname, id);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", pkgname, id, reason);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       slave_thaw_ttl(slave);
+
+       ret = slave_rpc_request_only(slave, pkgname, packet, 0);
+       release_resource_for_closing_gbar(pkg, inst, client);
+       inst->gbar.owner = NULL;
+       DbgPrint("PERF_WIDGET\n");
+       return ret;
+}
+
+HAPI int instance_client_gbar_created(struct inst_info *inst, int status)
+{
+       struct packet *packet;
+       const char *buf_id;
+       unsigned int cmd = CMD_GBAR_CREATED;
+       int ret;
+
+       if (inst->gbar.need_to_send_close_event) {
+               DbgPrint("GBAR is already created\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (package_gbar_type(inst->info)) {
+       case GBAR_TYPE_SCRIPT:
+               buf_id = script_handler_buffer_id(inst->gbar.canvas.script);
+               break;
+       case GBAR_TYPE_BUFFER:
+               buf_id = buffer_handler_id(inst->gbar.canvas.buffer);
+               break;
+       case GBAR_TYPE_TEXT:
+       default:
+               buf_id = "";
+               break;
+       }
+
+       inst->gbar.need_to_send_close_event = (status == 0);
+
+       packet = packet_create_noack((const char *)&cmd, "sssiii", 
+                       package_name(inst->info), inst->id, buf_id,
+                       inst->gbar.width, inst->gbar.height, status);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = client_send_event(inst, packet, NULL);
+
+       if (inst->gbar.need_to_send_close_event && inst->gbar.pended_update_cnt) {
+               DbgPrint("Apply pended desc(%d) - %s\n", inst->gbar.pended_update_cnt, inst->gbar.pended_update_desc);
+               instance_gbar_updated_by_instance(inst, inst->gbar.pended_update_desc, 0, 0, inst->gbar.width, inst->gbar.height);
+               inst->gbar.pended_update_cnt = 0;
+               DbgFree(inst->gbar.pended_update_desc);
+               inst->gbar.pended_update_desc = NULL;
+       }
+
+       return ret;
+}
+
+HAPI int instance_client_gbar_destroyed(struct inst_info *inst, int status)
+{
+       return send_gbar_destroyed_to_client(inst, status);
+}
+
+HAPI int instance_client_gbar_extra_buffer_created(struct inst_info *inst, int idx)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_GBAR_CREATE_XBUF;
+       int pixmap;
+
+       pixmap = buffer_handler_pixmap(inst->gbar.extra_buffer[idx]);
+       if (pixmap == 0) {
+               ErrPrint("Invalid buffer\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssii", package_name(inst->info), inst->id, pixmap, idx);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_client_gbar_extra_buffer_destroyed(struct inst_info *inst, int idx)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_GBAR_DESTROY_XBUF;
+       int pixmap;
+
+       pixmap = buffer_handler_pixmap(inst->gbar.extra_buffer[idx]);
+       if (pixmap == 0) {
+               ErrPrint("Invalid buffer\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssii", package_name(inst->info), inst->id, pixmap, idx);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_client_widget_extra_buffer_created(struct inst_info *inst, int idx)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_WIDGET_CREATE_XBUF;
+       int pixmap;
+
+       pixmap = buffer_handler_pixmap(inst->widget.extra_buffer[idx]);
+       if (pixmap == 0) {
+               ErrPrint("Invalid buffer\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssii", package_name(inst->info), inst->id, pixmap, idx);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+HAPI int instance_client_widget_extra_buffer_destroyed(struct inst_info *inst, int idx)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_WIDGET_DESTROY_XBUF;
+       int pixmap;
+
+       pixmap = buffer_handler_pixmap(inst->widget.extra_buffer[idx]);
+       if (pixmap == 0) {
+               ErrPrint("Invalid buffer\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssii", package_name(inst->info), inst->id, pixmap, idx);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return client_send_event(inst, packet, NULL);
+}
+
+/**
+ * \note
+ *  this function will be called after checking the list.
+ *  only if the client is not in the list, this will be called.
+ */
+HAPI int instance_add_client(struct inst_info *inst, struct client_node *client)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_VIEWER_CONNECTED;
+
+       if (inst->client == client) {
+               ErrPrint("Owner cannot be the viewer\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("%d is added to the list of viewer of %s(%s)\n", client_pid(client), package_name(instance_package(inst)), instance_id(inst));
+       if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, viewer_deactivated_cb, inst) < 0) {
+               ErrPrint("Failed to add a deactivate callback\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sss", package_name(inst->info), inst->id, client_direct_addr(inst->client));
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, viewer_deactivated_cb, inst);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       instance_ref(inst);
+       inst->client_list = eina_list_append(inst->client_list, client);
+
+       return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
+}
+
+HAPI int instance_del_client(struct inst_info *inst, struct client_node *client)
+{
+       if (inst->client == client) {
+               ErrPrint("Owner is not in the viewer list\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, viewer_deactivated_cb, inst);
+       viewer_deactivated_cb(client, inst);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int instance_has_client(struct inst_info *inst, struct client_node *client)
+{
+       return !!eina_list_data_find(inst->client_list, client);
+}
+
+HAPI void *instance_client_list(struct inst_info *inst)
+{
+       return inst->client_list;
+}
+
+HAPI int instance_init(void)
+{
+       if (!strcasecmp(WIDGET_CONF_PROVIDER_METHOD, "shm")) {
+               s_info.env_buf_type = WIDGET_FB_TYPE_SHM;
+       } else if (!strcasecmp(WIDGET_CONF_PROVIDER_METHOD, "pixmap")) {
+               s_info.env_buf_type = WIDGET_FB_TYPE_PIXMAP;
+       }
+       /* Default method is WIDGET_FB_TYPE_FILE */
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int instance_fini(void)
+{
+       return WIDGET_ERROR_NONE;
+}
+
+static inline struct tag_item *find_tag_item(struct inst_info *inst, const char *tag)
+{
+       struct tag_item *item;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(inst->data_list, l, item) {
+               if (!strcmp(item->tag, tag)) {
+                       return item;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI int instance_set_data(struct inst_info *inst, const char *tag, void *data)
+{
+       struct tag_item *item;
+
+       item = find_tag_item(inst, tag);
+       if (!item) {
+               item = malloc(sizeof(*item));
+               if (!item) {
+                       ErrPrint("malloc: %d\n", errno);
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               item->tag = strdup(tag);
+               if (!item->tag) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(item);
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               inst->data_list = eina_list_append(inst->data_list, item);
+       }
+
+       if (!data) {
+               inst->data_list = eina_list_remove(inst->data_list, item);
+               DbgFree(item->tag);
+               DbgFree(item);
+       } else {
+               item->data = data;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *instance_del_data(struct inst_info *inst, const char *tag)
+{
+       struct tag_item *item;
+       void *data;
+
+       item = find_tag_item(inst, tag);
+       if (!item) {
+               return NULL;
+       }
+
+       inst->data_list = eina_list_remove(inst->data_list, item);
+       data = item->data;
+       DbgFree(item->tag);
+       DbgFree(item);
+
+       return data;
+}
+
+HAPI void *instance_get_data(struct inst_info *inst, const char *tag)
+{
+       struct tag_item *item;
+
+       item = find_tag_item(inst, tag);
+       if (!item) {
+               return NULL;
+       }
+
+       return item->data;
+}
+
+HAPI struct client_node *instance_gbar_owner(struct inst_info *inst)
+{
+       return inst->gbar.owner;
+}
+
+HAPI void instance_set_orientation(struct inst_info *inst, int degree)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_ORIENTATION;
+
+       if (inst->orientation == degree) {
+               return;
+       }
+
+       inst->orientation = degree;
+
+       packet = packet_create_noack((const char *)&cmd, "ssi", package_name(inst->info), inst->id, degree);
+       if (!packet) {
+               ErrPrint("Failed to create a new packet\n");
+               return;
+       }
+
+       if (slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0) != WIDGET_ERROR_NONE) {
+               /* packet will be destroyed by slave_rpc_request_only if it fails */
+               ErrPrint("Failed to send a request\n");
+       }
+
+       return;
+}
+
+HAPI int instance_orientation(struct inst_info *inst)
+{
+       return inst->orientation;
+}
+
+/* End of a file */
diff --git a/src/io.c b/src/io.c
new file mode 100644 (file)
index 0000000..9f59fdd
--- /dev/null
+++ b/src/io.c
@@ -0,0 +1,727 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#include <dlog.h>
+#include <Eina.h>
+#include <sqlite3.h>
+#include <db-util.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+
+#include "debug.h"
+#include "conf.h"
+#include "parser.h"
+#include "group.h"
+#include "util.h"
+#include "client_life.h"
+#include "slave_life.h"
+#include "package.h"
+#include "io.h"
+
+int errno;
+
+static struct {
+       sqlite3 *handle;
+} s_info = {
+       .handle = NULL,
+};
+
+static inline int build_client_info(struct pkg_info *info)
+{
+       static const char *dml = "SELECT auto_launch, gbar_size FROM client WHERE pkgid = ?";
+       sqlite3_stmt *stmt;
+       int width;
+       int height;
+       int ret;
+       const char *tmp;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to bind a pkgname %s\n", package_name(info));
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ErrPrint("%s has no records (%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
+               sqlite3_reset(stmt);
+               sqlite3_clear_bindings(stmt);
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       package_set_auto_launch(info, (const char *)sqlite3_column_text(stmt, 0));
+
+       tmp = (const char *)sqlite3_column_text(stmt, 1);
+       if (tmp && strlen(tmp)) {
+               if (sscanf(tmp, "%dx%d", &width, &height) != 2) {
+                       ErrPrint("Failed to get GBAR width and Height (%s)\n", tmp);
+               } else {
+                       package_set_gbar_width(info, width);
+                       package_set_gbar_height(info, height);
+               }
+       }
+
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int build_provider_info(struct pkg_info *info)
+{
+       static const char *dml = "SELECT provider.network, provider.abi, provider.secured, provider.box_type, provider.box_src, provider.box_group, provider.gbar_type, provider.gbar_src, provider.gbar_group, provider.libexec, provider.timeout, provider.period, provider.script, provider.pinup, pkgmap.appid, provider.direct_input, provider.hw_acceleration, pkgmap.category FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?";
+       sqlite3_stmt *stmt;
+       int ret;
+       const char *tmp;
+       const char *appid;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (sqlite3_bind_text(stmt, 2, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ErrPrint("%s has no record(%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
+               sqlite3_reset(stmt);
+               sqlite3_clear_bindings(stmt);
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       appid = (const char *)sqlite3_column_text(stmt, 14);
+       if (!appid || !strlen(appid)) {
+               ErrPrint("Failed to execute the DML for %s\n", package_name(info));
+               sqlite3_reset(stmt);
+               sqlite3_clear_bindings(stmt);
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       package_set_network(info, sqlite3_column_int(stmt, 0));
+       package_set_secured(info, sqlite3_column_int(stmt, 2));
+
+       tmp = (const char *)sqlite3_column_text(stmt, 1);
+       if (tmp && strlen(tmp)) {
+               package_set_abi(info, tmp);
+       }
+
+       package_set_widget_type(info, sqlite3_column_int(stmt, 3));
+       tmp = (const char *)sqlite3_column_text(stmt, 4);
+       if (tmp && strlen(tmp)) {
+               package_set_widget_path(info, tmp);
+
+               tmp = (const char *)sqlite3_column_text(stmt, 5);
+               if (tmp && strlen(tmp)) {
+                       package_set_widget_group(info, tmp);
+               }
+       }
+
+       package_set_gbar_type(info, sqlite3_column_int(stmt, 6));
+       tmp = (const char *)sqlite3_column_text(stmt, 7);
+       if (tmp && strlen(tmp)) {
+               package_set_gbar_path(info, tmp);
+
+               tmp = (const char *)sqlite3_column_text(stmt, 8);
+               if (tmp && strlen(tmp)) {
+                       package_set_gbar_group(info, tmp);
+               }
+       }
+
+       tmp = (const char *)sqlite3_column_text(stmt, 9);
+       if (tmp && strlen(tmp)) {
+               package_set_libexec(info, tmp);
+       }
+
+       package_set_timeout(info, sqlite3_column_int(stmt, 10));
+
+       tmp = (const char *)sqlite3_column_text(stmt, 11);
+       if (tmp && strlen(tmp)) {
+               package_set_period(info, atof(tmp));
+       }
+
+       tmp = (const char *)sqlite3_column_text(stmt, 12);
+       if (tmp && strlen(tmp)) {
+               package_set_script(info, tmp);
+       }
+
+       package_set_pinup(info, sqlite3_column_int(stmt, 13));
+       package_set_direct_input(info, sqlite3_column_int(stmt, 15));
+       package_set_hw_acceleration(info, (const char *)sqlite3_column_text(stmt, 16));
+       package_set_category(info, (const char *)sqlite3_column_text(stmt, 17));
+
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int build_box_size_info(struct pkg_info *info)
+{
+       static const char *dml = "SELECT size_type FROM box_size WHERE pkgid = ?";
+       sqlite3_stmt *stmt;
+       int ret;
+       unsigned int size_type;
+       unsigned int size_list;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT) != SQLITE_OK) {
+               ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       size_list = 0;
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               size_type = sqlite3_column_int(stmt, 0);
+               size_list |= size_type;
+       }
+
+       package_set_size_list(info, size_list);
+
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int load_context_option(struct context_item *item, int id)
+{
+       static const char *dml = "SELECT key, value FROM option WHERE option_id = ?";
+       sqlite3_stmt *stmt;
+       const char *key;
+       const char *value;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_int(stmt, 1, id);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = WIDGET_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_NOT_EXIST;
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               key = (const char *)sqlite3_column_text(stmt, 0);
+               if (!key || !strlen(key)) {
+                       ErrPrint("KEY is nil\n");
+                       continue;
+               }
+
+               value = (const char *)sqlite3_column_text(stmt, 1);
+               if (!value || !strlen(value)) {
+                       ErrPrint("VALUE is nil\n");
+                       continue;
+               }
+
+               ret = group_add_option(item, key, value);
+               if (ret < 0) {
+                       break;
+               }
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int load_context_item(struct context_info *info, int id)
+{
+       static const char *dml = "SELECT ctx_item, option_id FROM groupmap WHERE id = ?";
+       struct context_item *item;
+       sqlite3_stmt *stmt;
+       const char *ctx_item;
+       int option_id;
+       int ret;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_int(stmt, 1, id);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = WIDGET_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_NOT_EXIST;
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               ctx_item = (const char *)sqlite3_column_text(stmt, 0);
+               option_id = sqlite3_column_int(stmt, 1);
+
+               item = group_add_context_item(info, ctx_item);
+               if (!item) {
+                       ErrPrint("Failed to add a new context item\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       break;
+               }
+
+               ret = load_context_option(item, option_id);
+               if (ret < 0) {
+                       break;
+               }
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+static inline int build_group_info(struct pkg_info *info)
+{
+       static const char *dml = "SELECT id, cluster, category FROM groupinfo WHERE pkgid = ?";
+       sqlite3_stmt *stmt;
+       int ret;
+       int id;
+       const char *cluster_name;
+       const char *category_name;
+       struct cluster *cluster;
+       struct category *category;
+       struct context_info *ctx_info;
+
+       ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to bind a package name(%s)\n", package_name(info));
+               sqlite3_finalize(stmt);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               id = sqlite3_column_int(stmt, 0);
+               cluster_name = (const char *)sqlite3_column_text(stmt, 1);
+               if (!cluster_name || !strlen(cluster_name)) {
+                       DbgPrint("Cluster name is not valid\n");
+                       continue;
+               }
+
+               category_name = (const char *)sqlite3_column_text(stmt, 2);
+               if (!category_name || !strlen(category_name)) {
+                       DbgPrint("Category name is not valid\n");
+                       continue;
+               }
+
+               cluster = group_find_cluster(cluster_name);
+               if (!cluster) {
+                       cluster = group_create_cluster(cluster_name);
+                       if (!cluster) {
+                               ErrPrint("Failed to create a cluster(%s)\n", cluster_name);
+                               continue;
+                       }
+               }
+
+               category = group_find_category(cluster, category_name);
+               if (!category) {
+                       category = group_create_category(cluster, category_name);
+                       if (!category) {
+                               ErrPrint("Failed to create a category(%s)\n", category_name);
+                               continue;
+                       }
+               }
+
+               /*!
+                * \TODO
+                * Step 1. Get the list of the context item from the DB using 'id'
+                *         {context_item, option_id}
+                * Step 2. Get the list of the options from the DB using option_id
+                *         key, value
+                */
+               ctx_info = group_create_context_info(category, package_name(info));
+               if (ctx_info) {
+                       ret = load_context_item(ctx_info, id);
+                       if (ret < 0) {
+                               if (ret == (int)WIDGET_ERROR_NOT_EXIST) {
+                                       DbgPrint("Has no specific context info\n");
+                               } else {
+                                       DbgPrint("Context info is not valid\n");
+                                       group_destroy_context_info(ctx_info);
+                                       ctx_info = NULL;
+                               }
+                       }
+
+                       if (ctx_info) {
+                               package_add_ctx_info(info, ctx_info);
+                       }
+               }
+       }
+
+       sqlite3_reset(stmt);
+       sqlite3_clear_bindings(stmt);
+       sqlite3_finalize(stmt);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int io_is_exists(const char *lbid)
+{
+       sqlite3_stmt *stmt;
+       int ret;
+
+       if (!s_info.handle) {
+               ErrPrint("DB is not ready\n");
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_prepare_v2(s_info.handle, "SELECT COUNT(pkgid) FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = WIDGET_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ErrPrint("%s has no record (%s)\n", lbid, sqlite3_errmsg(s_info.handle));
+               ret = WIDGET_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       ret = sqlite3_column_int(stmt, 0);
+out:
+       sqlite3_reset(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+HAPI char *io_widget_pkgname(const char *pkgname)
+{
+       sqlite3_stmt *stmt;
+       char *pkgid;
+       char *tmp;
+       int ret;
+
+       pkgid = NULL;
+
+       if (!s_info.handle) {
+               ErrPrint("DB is not ready\n");
+               return NULL;
+       }
+
+       ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return NULL;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       if (sqlite3_step(stmt) != SQLITE_ROW) {
+               ErrPrint("%s has no record (%s)\n", pkgname, sqlite3_errmsg(s_info.handle));
+               goto out;
+       }
+
+       tmp = (char *)sqlite3_column_text(stmt, 0);
+       if (tmp && strlen(tmp)) {
+               pkgid = strdup(tmp);
+               if (!pkgid) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+
+out:
+       sqlite3_reset(stmt);
+       sqlite3_finalize(stmt);
+       return pkgid;
+}
+
+HAPI int io_crawling_widgetes(int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data)
+{
+       DIR *dir;
+
+       if (!s_info.handle) {
+               ErrPrint("DB is not ready\n");
+       } else {
+               int ret;
+               sqlite3_stmt *stmt;
+
+               ret = sqlite3_prepare_v2(s_info.handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
+               if (ret != SQLITE_OK) {
+                       ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               } else {
+                       const char *lbid;
+                       const char *pkgid;
+                       int prime;
+
+                       while (sqlite3_step(stmt) == SQLITE_ROW) {
+                               pkgid = (const char *)sqlite3_column_text(stmt, 0);
+                               if (!pkgid || !strlen(pkgid)) {
+                                       continue;
+                               }
+
+                               lbid = (const char *)sqlite3_column_text(stmt, 1);
+                               if (!lbid || !strlen(lbid)) {
+                                       continue;
+                               }
+
+                               prime = (int)sqlite3_column_int(stmt, 1);
+
+                               if (cb(pkgid, lbid, prime, data) < 0) {
+                                       sqlite3_reset(stmt);
+                                       sqlite3_finalize(stmt);
+                                       return WIDGET_ERROR_CANCELED;
+                               }
+                       }
+
+                       sqlite3_reset(stmt);
+                       sqlite3_finalize(stmt);
+               }
+       }
+
+       dir = opendir(WIDGET_CONF_ROOT_PATH);
+       if (!dir) {
+               ErrPrint("opendir: %d\n", errno);
+       } else {
+               struct dirent *ent;
+
+               while ((ent = readdir(dir))) {
+                       if (ent->d_name[0] == '.') {
+                               continue;
+                       }
+
+                       if (cb(ent->d_name, ent->d_name, -2, data) < 0) {
+                               if (closedir(dir) < 0) {
+                                       ErrPrint("closedir: %d\n", errno);
+                               }
+                               return WIDGET_ERROR_CANCELED;
+                       }
+               }
+
+               if (closedir(dir) < 0) {
+                       ErrPrint("closedir: %d\n", errno);
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int io_update_widget_package(const char *pkgid, int (*cb)(const char *pkgid, const char *lbid, int prime, void *data), void *data)
+{
+       sqlite3_stmt *stmt;
+       char *lbid;
+       int prime;
+       int ret;
+
+       if (!cb || !pkgid) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!s_info.handle) {
+               ErrPrint("DB is not ready\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       ret = 0;
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               lbid = (char *)sqlite3_column_text(stmt, 0);
+               if (!lbid || !strlen(lbid)) {
+                       continue;
+               }
+
+               prime = sqlite3_column_int(stmt, 1);
+
+               if (cb(pkgid, lbid, prime, data) < 0) {
+                       DbgPrint("Callback canceled\n");
+                       break;
+               }
+
+               ret++;
+       }
+out:
+       sqlite3_reset(stmt);
+       sqlite3_finalize(stmt);
+       return ret;
+}
+
+HAPI int io_load_package_db(struct pkg_info *info)
+{
+       int ret;
+
+       if (!s_info.handle) {
+               ErrPrint("DB is not ready\n");
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       ret = build_provider_info(info);
+       if (ret < 0) {
+               return ret;
+       }
+
+       ret = build_client_info(info);
+       if (ret < 0) {
+               return ret;
+       }
+
+       ret = build_box_size_info(info);
+       if (ret < 0) {
+               return ret;
+       }
+
+       ret = build_group_info(info);
+       if (ret < 0) {
+               return ret;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int db_init(void)
+{
+       int ret;
+       struct stat stat;
+
+       ret = db_util_open_with_options(WIDGET_CONF_DBFILE, &s_info.handle, SQLITE_OPEN_READONLY, NULL);
+       if (ret != SQLITE_OK) {
+               ErrPrint("Failed to open a DB\n");
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (lstat(WIDGET_CONF_DBFILE, &stat) < 0) {
+               ErrPrint("lstat: %d\n", errno);
+               db_util_close(s_info.handle);
+               s_info.handle = NULL;
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (!S_ISREG(stat.st_mode)) {
+               ErrPrint("Invalid file\n");
+               db_util_close(s_info.handle);
+               s_info.handle = NULL;
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (stat.st_size <= 0) {
+               DbgPrint("Size is %d (But use this ;)\n", stat.st_size);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int db_fini(void)
+{
+       if (!s_info.handle) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       db_util_close(s_info.handle);
+       s_info.handle = NULL;
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int io_init(void)
+{
+       int ret;
+
+       ret = db_init();
+       if (ret < 0) {
+               DbgPrint("DB initialized: %d\n", ret);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int io_fini(void)
+{
+       int ret;
+
+       ret = db_fini();
+       if (ret < 0) {
+               DbgPrint("DB finalized: %d\n", ret);
+       }
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/main.c b/src/main.c
new file mode 100644 (file)
index 0000000..d42801c
--- /dev/null
@@ -0,0 +1,458 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/signalfd.h>
+#include <ctype.h>
+
+#include <systemd/sd-daemon.h>
+
+#include <Ecore.h>
+#include <glib.h>
+#include <glib-object.h>
+#include <aul.h>
+#include <vconf.h>
+
+#include <packet.h>
+#include <dlog.h>
+
+#if defined(HAVE_LIVEBOX)
+
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+#include <widget_abi.h>
+
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "instance.h"
+#include "buffer_handler.h"
+#include "script_handler.h"
+#include "package.h"
+#include "group.h"
+#include "dead_monitor.h"
+#include "io.h"
+#include "xmonitor.h"
+#include "server.h"
+#include "event.h"
+#include "file_service.h"
+#include "utility_service.h"
+#endif
+
+#include "conf.h"
+#include "setting.h"
+#include "util.h"
+#include "debug.h"
+#include "critical_log.h"
+#include "shortcut_service.h"
+#include "notification_service.h"
+#include "badge_service.h"
+
+#if defined(FLOG)
+#define TMP_LOG_FILE "/tmp/live.log"
+FILE *__file_log_fp;
+#endif
+
+static inline int app_create(void)
+{
+       int ret;
+
+       if (access(WIDGET_CONF_LOG_PATH, R_OK | W_OK) != 0) {
+               if (mkdir(WIDGET_CONF_LOG_PATH, 0755) < 0) {
+                       ErrPrint("mkdir %s (%d)\n", WIDGET_CONF_LOG_PATH, errno);
+               }
+       }
+
+       /*!
+        * \note
+        * Dead signal handler has to be initialized before
+        * initate package or client (slave and client).
+        *
+        * Because while creating slaves for packages.
+        * It could be crashed before complete the initation stage.
+        *
+        * Then the dead callback should be invoked to handle it properly.
+        *
+        * To enable the dead signal handler,
+        * dead_init should be done before other components are initiated.
+        */
+       ret = setting_init();
+       if (ret < 0) {
+               DbgPrint("Setting initialized: %d\n", ret);
+       }
+
+#if defined(HAVE_LIVEBOX)
+       ret = client_init();
+       if (ret < 0) {
+               DbgPrint("Client initialized: %d\n", ret);
+       }
+
+       ret = dead_init();
+       if (ret < 0) {
+               DbgPrint("Dead callback is registered: %d\n", ret);
+       }
+
+       ret = group_init();
+       if (ret < 0) {
+               DbgPrint("group init: %d\n", ret);
+       }
+
+       ret = io_init();
+       if (ret < 0) {
+               DbgPrint("Init I/O: %d\n", ret);
+       }
+
+       ret = package_init();
+       if (ret < 0) {
+               DbgPrint("pkgmgr initialized: %d\n", ret);
+       }
+
+       instance_init();
+
+       ret = xmonitor_init();
+       if (ret < 0) {
+               DbgPrint("XMonitor init is done: %d\n", ret);
+       }
+
+       ret = buffer_handler_init();
+       if (ret < 0) {
+               DbgPrint("Buffer handler init is done: %d\n", ret);
+       }
+
+       /*!
+        * \note
+        * After initiate all other sub-systtems,
+        * Enable the server socket.
+        */
+       ret = server_init();
+       if (ret < 0) {
+               DbgPrint("Server initialized: %d\n", ret);
+       }
+
+       event_init();
+
+       script_init();
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_FILE)) {
+               file_service_init();
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_UTILITY)) {
+               utility_service_init();
+       }
+#endif
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_SHORTCUT)) {
+               shortcut_service_init();
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_NOTIFICATION)) {
+               notification_service_init();
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_BADGE)) {
+               badge_service_init();
+       }
+
+       return 0;
+}
+
+static inline int app_terminate(void)
+{
+       int ret;
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_BADGE)) {
+               ret = badge_service_fini();
+               if (ret < 0) {
+                       DbgPrint("badge: %d\n", ret);
+               }
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_NOTIFICATION)) {
+               ret = notification_service_fini();
+               if (ret < 0) {
+                       DbgPrint("noti: %d\n", ret);
+               }
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_SHORTCUT)) {
+               ret = shortcut_service_fini();
+               if (ret < 0) {
+                       DbgPrint("shortcut: %d\n", ret);
+               }
+       }
+
+#if defined(HAVE_LIVEBOX)
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_FILE)) {
+               ret = file_service_fini();
+               if (ret < 0) {
+                       DbgPrint("Finalize the file service: %d\n", ret);
+               }
+       }
+
+       ret = server_fini();
+       if (ret < 0) {
+               DbgPrint("Finalize server: %d\n", ret);
+       }
+
+       ret = dead_fini();
+       if (ret < 0) {
+               DbgPrint("dead signal handler finalized: %d\n", ret);
+       }
+
+       if (util_service_is_enabled(WIDGET_CONF_SERVICE_UTILITY)) {
+               ret = utility_service_fini();
+               if (ret < 0) {
+                       DbgPrint("utility: %d\n", ret);
+               }
+       }
+
+       ret = event_fini();
+       if (ret < 0) {
+               DbgPrint("event: %d\n", ret);
+       }
+
+       ret = setting_fini();
+       if (ret < 0) {
+               DbgPrint("Finalize setting : %d\n", ret);
+       }
+
+       ret = instance_fini();
+       if (ret < 0) {
+               DbgPrint("Finalizing instances: %d\n", ret);
+       }
+
+       ret = package_fini();
+       if (ret < 0) {
+               DbgPrint("Finalize package info: %d\n", ret);
+       }
+
+       ret = script_fini();
+       if (ret < 0) {
+               DbgPrint("script: %d\n", ret);
+       }
+
+       ret = buffer_handler_fini();
+       if (ret < 0) {
+               DbgPrint("buffer handler: %d\n", ret);
+       }
+
+       xmonitor_fini();
+
+       client_fini();
+
+       ret = io_fini();
+       if (ret < 0) {
+               DbgPrint("IO finalized: %d\n", ret);
+       }
+
+       ret = group_fini();
+       if (ret < 0) {
+               DbgPrint("Group finalized: %d\n", ret);
+       }
+#endif
+
+       DbgPrint("Terminated\n");
+       return 0;
+}
+
+static Eina_Bool signal_cb(void *data, Ecore_Fd_Handler *handler)
+{
+       struct signalfd_siginfo fdsi;
+       ssize_t size;
+       int fd;
+
+       fd = ecore_main_fd_handler_fd_get(handler);
+       if (fd < 0) {
+               ErrPrint("Unable to get FD\n");
+               ecore_main_fd_handler_del(handler);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       size = read(fd, &fdsi, sizeof(fdsi));
+       if (size != sizeof(fdsi)) {
+               ErrPrint("read: %d\n", errno);
+               ecore_main_fd_handler_del(handler);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (fdsi.ssi_signo == SIGTERM) {
+               int cfd;
+
+               CRITICAL_LOG("Terminated(SIGTERM)\n");
+
+               cfd = creat("/tmp/.stop.provider", 0644);
+               if (cfd < 0 || close(cfd) < 0) {
+                       ErrPrint("stop.provider: %d\n", errno);
+               }
+
+               vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
+               //exit(0);
+               ecore_main_loop_quit();
+       } else if (fdsi.ssi_signo == SIGUSR1) {
+               /*!
+                * Turn off auto-reactivation
+                * Terminate all slaves
+                */
+#if defined(HAVE_LIVEBOX)
+               CRITICAL_LOG("USRS1, Deactivate ALL\n");
+               slave_deactivate_all(0, 1, 1);
+#endif
+       } else if (fdsi.ssi_signo == SIGUSR2) {
+               /*!
+                * Turn on auto-reactivation
+                * Launch all slaves again
+                */
+#if defined(HAVE_LIVEBOX)
+               CRITICAL_LOG("USR2, Activate ALL\n");
+               slave_activate_all();
+#endif
+       } else {
+               CRITICAL_LOG("Unknown SIG[%d] received\n", fdsi.ssi_signo);
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       int restart_count = 0;
+       sigset_t mask;
+       Ecore_Fd_Handler *signal_handler = NULL;
+
+#if defined(FLOG)
+       __file_log_fp = fopen(TMP_LOG_FILE, "w+t");
+       if (!__file_log_fp) {
+               __file_log_fp = fdopen(1, "w+t");
+       }
+#endif
+
+       /* appcore_agent_terminate */
+       if (ecore_init() <= 0) {
+               return -EFAULT;
+       }
+
+       ecore_app_args_set(argc, (const char **)argv);
+
+#if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
+       g_type_init();
+#endif
+
+       if (util_screen_init() <= 0) {
+               ecore_shutdown();
+               return -EFAULT;
+       }
+
+       widget_conf_init();
+       widget_conf_load();
+       widget_abi_init();
+
+       /*!
+        * How could we care this return values?
+        * Is there any way to print something on the screen?
+        */
+       ret = critical_log_init(util_basename(argv[0]));
+       if (ret < 0) {
+               ErrPrint("Failed to init the critical log\n");
+       }
+
+       /*!
+        * \note
+        * Clear old contents files before start the master provider.
+        */
+       (void)util_unlink_files(WIDGET_CONF_ALWAYS_PATH);
+       (void)util_unlink_files(WIDGET_CONF_READER_PATH);
+       (void)util_unlink_files(WIDGET_CONF_IMAGE_PATH);
+       (void)util_unlink_files(WIDGET_CONF_LOG_PATH);
+
+       if (util_free_space(WIDGET_CONF_IMAGE_PATH) < WIDGET_CONF_MINIMUM_SPACE) {
+               util_remove_emergency_disk();
+               util_prepare_emergency_disk();
+       }
+
+       util_setup_log_disk();
+
+       sigemptyset(&mask);
+
+       ret = sigaddset(&mask, SIGTERM);
+       if (ret < 0) {
+               CRITICAL_LOG("sigaddset: %d\n", errno);
+       }
+
+       ret = sigaddset(&mask, SIGUSR1);
+       if (ret < 0) {
+               CRITICAL_LOG("sigaddset: %d\n", errno);
+       }
+
+       ret = sigaddset(&mask, SIGUSR2);
+       if (ret < 0) {
+               CRITICAL_LOG("sigaddset: %d\n", errno);
+       }
+
+       ret = sigprocmask(SIG_BLOCK, &mask, NULL);
+       if (ret < 0) {
+               CRITICAL_LOG("sigprocmask: %d\n", errno);
+       }
+
+       ret = signalfd(-1, &mask, 0);
+       if (ret < 0) {
+               CRITICAL_LOG("signalfd: %d\n", errno);
+       } else {
+               signal_handler = ecore_main_fd_handler_add(ret, ECORE_FD_READ, signal_cb, NULL, NULL, NULL);
+               CRITICAL_LOG("Signal handler initiated: %d\n", ret);
+       }
+
+       app_create();
+       sd_notify(0, "READY=1");
+
+       vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
+       restart_count++;
+       vconf_set_int(VCONFKEY_MASTER_RESTART_COUNT, restart_count);
+
+       vconf_set_bool(VCONFKEY_MASTER_STARTED, 1);
+       ecore_main_loop_begin();
+       vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
+
+       app_terminate();
+
+       util_screen_fini();
+
+       if (signal_handler) {
+               ecore_main_fd_handler_del(signal_handler);
+       }
+
+       ecore_shutdown();
+       critical_log_fini();
+
+#if defined(FLOG)
+       if (__file_log_fp) {
+               fclose(__file_log_fp);
+       }
+#endif
+
+       widget_conf_reset();
+       widget_abi_fini();
+       return 0;
+}
+
+/* End of a file */
diff --git a/src/monitor.c b/src/monitor.c
new file mode 100644 (file)
index 0000000..aaa1576
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <Eina.h>
+#include <dlog.h>
+
+#include <widget_errno.h>
+#include <widget_cmd_list.h>
+#include <packet.h>
+#include <com-core_packet.h>
+
+#include "debug.h"
+#include "dead_monitor.h"
+#include "monitor.h"
+#include "util.h"
+
+int errno;
+
+struct monitor_client {
+       char *widget_id;
+       pid_t pid;
+       int handle;
+};
+
+static struct info {
+       Eina_List *monitor_list;
+} s_info = {
+       .monitor_list = NULL,
+};
+
+static void monitor_disconnected_cb(int handle, void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct monitor_client *monitor;
+
+       EINA_LIST_FOREACH_SAFE(s_info.monitor_list, l, n, monitor) {
+               if (monitor->handle == handle) {
+                       DbgPrint("monitor: %d is deleted (%s)\n", monitor->handle, monitor->widget_id);
+                       s_info.monitor_list = eina_list_remove(s_info.monitor_list, monitor);
+                       DbgFree(monitor->widget_id);
+                       DbgFree(monitor);
+               }
+       }
+}
+
+HAPI struct monitor_client *monitor_create_client(const char *widget_id, pid_t pid, int handle)
+{
+       struct monitor_client *monitor;
+
+       monitor = calloc(1, sizeof(*monitor));
+       if (!monitor) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       if (widget_id) {
+               monitor->widget_id = strdup(widget_id);
+               if (!monitor->widget_id) {
+                       ErrPrint("strdup: %s, %d\n", widget_id, errno);
+                       DbgFree(monitor);
+                       return NULL;
+               }
+       }
+       monitor->pid = pid;
+       monitor->handle = handle;
+
+       s_info.monitor_list = eina_list_append(s_info.monitor_list, monitor);
+
+       if (dead_callback_add(handle, monitor_disconnected_cb, NULL) < 0) {
+               ErrPrint("It's Okay. Dead callback is already registered for %d\n", handle);
+       }
+
+       return monitor;
+}
+
+HAPI int monitor_destroy_client(struct monitor_client *monitor)
+{
+       Eina_List *l;
+       struct monitor_client *item;
+       int handle;
+       int cnt;
+
+       s_info.monitor_list = eina_list_remove(s_info.monitor_list, monitor);
+       handle = monitor->handle;
+       DbgFree(monitor->widget_id);
+       DbgFree(monitor);
+
+       cnt = 0;
+       EINA_LIST_FOREACH(s_info.monitor_list, l, item) {
+               if (item->handle == handle) {
+                       cnt++;
+               }
+       }
+
+       if (cnt == 0) {
+               dead_callback_del(handle, monitor_disconnected_cb);
+               ErrPrint("Registered monitor object is not valid\n");
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct monitor_client *monitor_find_client_by_pid(const char *widget_id, pid_t pid)
+{
+       Eina_List *l;
+       struct monitor_client *monitor;
+
+       monitor = NULL;
+       EINA_LIST_FOREACH(s_info.monitor_list, l, monitor) {
+               if (monitor->pid == pid) {
+                       if (monitor->widget_id && widget_id) {
+                               if (!strcmp(monitor->widget_id, widget_id)) {
+                                       break;
+                               }
+                       } else if (monitor->widget_id == widget_id) {
+                               break;
+                       }
+               }
+
+               monitor = NULL;
+       }
+
+       return monitor;
+}
+
+HAPI struct monitor_client *monitor_find_client_by_handle(const char *widget_id, int handle)
+{
+       Eina_List *l;
+       struct monitor_client *monitor;
+
+       monitor = NULL;
+       EINA_LIST_FOREACH(s_info.monitor_list, l, monitor) {
+               if (monitor->handle == handle) {
+                       if (monitor->widget_id && widget_id) {
+                               if (!strcmp(monitor->widget_id, widget_id)) {
+                                       break;
+                               }
+                       } else if (monitor->widget_id == widget_id) {
+                               break;
+                       }
+               }
+
+               monitor = NULL;
+       }
+
+       return monitor;
+}
+
+HAPI int monitor_multicast_state_change_event(const char *widget_id, enum monitor_event_type event, const char *instance_id, const char *content_info)
+{
+       struct monitor_client *monitor;
+       Eina_List *l;
+       int cnt;
+       unsigned int cmd;
+       struct packet *packet;
+
+       cmd = (unsigned int)event;
+
+       packet = packet_create_noack((const char *)&cmd, "dsss", util_timestamp(), widget_id, instance_id, content_info);
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       cnt = 0;
+       EINA_LIST_FOREACH(s_info.monitor_list, l, monitor) {
+               if (widget_id && monitor->widget_id) {
+                       if (!strcmp(widget_id, monitor->widget_id)) {
+                               com_core_packet_send_only(monitor->handle, packet);
+                       }
+               } else if (monitor->widget_id == NULL) {
+                       com_core_packet_send_only(monitor->handle, packet);
+               }
+       }
+
+       packet_destroy(packet);
+
+       DbgPrint("%d events are multicasted\n", cnt);
+       return cnt;
+}
+
+/* End of a file */
diff --git a/src/notification_service.c b/src/notification_service.c
new file mode 100644 (file)
index 0000000..dcda394
--- /dev/null
@@ -0,0 +1,792 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include "lite-errno.h"
+#endif
+#include <packet.h>
+
+#include <sys/smack.h>
+#include <security-server.h>
+
+#include <vconf.h>
+#include <notification.h>
+#include <notification_internal.h>
+#include <notification_ipc.h>
+#include <notification_noti.h>
+#include <notification_setting_service.h>
+
+#include "service_common.h"
+#include "debug.h"
+#include "util.h"
+#include "conf.h"
+
+#ifndef NOTIFICATION_DEL_PACKET_UNIT
+#define NOTIFICATION_DEL_PACKET_UNIT 10
+#endif
+
+static struct info {
+       Eina_List *context_list;
+       struct service_context *svc_ctx;
+} s_info = {
+       .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
+       .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
+};
+
+struct context {
+       struct tcb *tcb;
+       double seq;
+};
+
+struct noti_service {
+       const char *cmd;
+       void (*handler)(struct tcb *tcb, struct packet *packet, void *data);
+       const char *rule;
+       const char *access;
+       void (*handler_access_error)(struct tcb *tcb, struct packet *packet);
+};
+
+static inline char *_string_get(char *string)
+{
+       if (string == NULL) {
+               return NULL;
+       }
+       if (string[0] == '\0') {
+               return NULL;
+       }
+
+       return string;
+}
+
+/*!
+ * FUNCTIONS to create packets
+ */
+static inline int _priv_id_get_from_list(int num_data, int *list, int index) {
+       if (index < num_data) {
+               return *(list + index);
+       } else {
+               return -1;
+       }
+}
+
+static inline struct packet *_packet_create_with_list(int op_num, int *list, int start_index) {
+       return packet_create(
+                       "del_noti_multiple",
+                       "iiiiiiiiiii",
+                       ((op_num - start_index) > NOTIFICATION_DEL_PACKET_UNIT) ? NOTIFICATION_DEL_PACKET_UNIT : op_num - start_index,
+                       _priv_id_get_from_list(op_num, list, start_index),
+                       _priv_id_get_from_list(op_num, list, start_index + 1),
+                       _priv_id_get_from_list(op_num, list, start_index + 2),
+                       _priv_id_get_from_list(op_num, list, start_index + 3),
+                       _priv_id_get_from_list(op_num, list, start_index + 4),
+                       _priv_id_get_from_list(op_num, list, start_index + 5),
+                       _priv_id_get_from_list(op_num, list, start_index + 6),
+                       _priv_id_get_from_list(op_num, list, start_index + 7),
+                       _priv_id_get_from_list(op_num, list, start_index + 8),
+                       _priv_id_get_from_list(op_num, list, start_index + 9)
+                       );
+}
+
+/*!
+ * SERVICE HANDLER
+ */
+static void _handler_insert_noti(struct tcb *tcb, struct packet *packet, notification_h noti, void *data)
+{
+       int ret = 0, ret_p = 0;
+       int priv_id = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+
+       ret = notification_noti_insert(noti);
+       notification_get_id(noti, NULL, &priv_id);
+       DbgPrint("priv_id: [%d]\n", priv_id);
+       packet_reply = packet_create_reply(packet, "ii", ret, priv_id);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("failed to send reply packet: %d\n", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("failed to create a reply packet\n");
+       }
+
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ErrPrint("failed to insert a notification: %d\n", ret);
+               return ;
+       }
+
+       packet_service = notification_ipc_make_packet_from_noti(noti, "add_noti", 2);
+       if (packet_service != NULL) {
+               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                       ErrPrint("failed to send a multicast packet: %d\n", ret_p);
+               }
+               packet_destroy(packet_service);
+       } else {
+               ErrPrint("failed to create a multicats packet\n");
+       }
+}
+
+/*static void _handler_insert(struct tcb *tcb, struct packet *packet, void *data) // not used
+  {
+  notification_h noti = NULL;
+
+  noti = notification_create(NOTIFICATION_TYPE_NOTI);
+  if (noti != NULL) {
+  if (notification_ipc_make_noti_from_packet(noti, packet) == NOTIFICATION_ERROR_NONE) {
+  _handler_insert_noti(tcb, packet, noti, data);
+  } else {
+  ErrPrint("Failed to create the packet");
+  }
+  notification_free(noti);
+  }
+  }*/
+
+static void _handler_update_noti(struct tcb *tcb, struct packet *packet, notification_h noti, void *data)
+{
+       int ret = 0, ret_p = 0;
+       int priv_id = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+
+       ret = notification_noti_update(noti);
+
+       notification_get_id(noti, NULL, &priv_id);
+       DbgPrint("priv_id: [%d]\n", priv_id);
+       packet_reply = packet_create_reply(packet, "ii", ret, priv_id);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("failed to send reply packet:%d\n", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("failed to create a reply packet\n");
+       }
+
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ErrPrint("failed to update a notification:%d\n", ret);
+               return ;
+       }
+
+       packet_service = notification_ipc_make_packet_from_noti(noti, "update_noti", 2);
+       if (packet_service != NULL) {
+               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                       ErrPrint("failed to send a multicast packet: %d\n", ret_p);
+               }
+               packet_destroy(packet_service);
+       }
+}
+
+static void _handler_update(struct tcb *tcb, struct packet *packet, void *data)
+{
+       notification_h noti = NULL;
+
+       noti = notification_create(NOTIFICATION_TYPE_NOTI);
+       if (noti != NULL) {
+               if (notification_ipc_make_noti_from_packet(noti, packet) == NOTIFICATION_ERROR_NONE) {
+                       _handler_update_noti(tcb, packet, noti, data);
+               } else {
+                       ErrPrint("Failed to create the packet");
+               }
+               notification_free(noti);
+       }
+}
+
+static void _handler_check_noti_by_tag(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0;
+       notification_h noti = NULL;
+
+       noti = notification_create(NOTIFICATION_TYPE_NOTI);
+       if (noti != NULL) {
+               if (notification_ipc_make_noti_from_packet(noti, packet) == NOTIFICATION_ERROR_NONE) {
+                       ret = notification_noti_check_tag(noti);
+                       if (ret == NOTIFICATION_ERROR_NOT_EXIST_ID) {
+                               _handler_insert_noti(tcb, packet, noti, data);
+                       } else if (ret == NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
+                               _handler_update_noti(tcb, packet, noti, data);
+                       }
+               }
+               notification_free(noti);
+       }
+}
+
+static void _handler_load_noti_by_tag(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       char* tag;
+       char* pkgname;
+       struct packet *packet_reply = NULL;
+       notification_h noti = NULL;
+
+       noti = notification_create(NOTIFICATION_TYPE_NOTI);
+       if (noti != NULL) {
+               if (packet_get(packet, "ss", &pkgname, &tag) == 2) {
+                       ret = notification_noti_get_by_tag(noti, pkgname, tag);
+                       packet_reply = notification_ipc_make_reply_packet_from_noti(noti, packet);
+                       if (packet_reply) {
+                               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                                       ErrPrint("failed to send reply packet: %d\n", ret_p);
+                               }
+                               packet_destroy(packet_reply);
+                       } else {
+                               ErrPrint("failed to create a reply packet\n");
+                       }
+
+                       if (ret != NOTIFICATION_ERROR_NONE) {
+                               ErrPrint("failed to load_noti_by_tag : %d\n", ret);
+                               notification_free(noti);
+                               return ;
+                       }
+               } else {
+                       ErrPrint("Failed to create the packet");
+               }
+               notification_free(noti);
+       }
+}
+
+
+static void _handler_refresh(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "i", ret);
+       if (packet_reply) {
+               if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("failed to send reply packet:%d\n", ret);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("failed to create a reply packet\n");
+       }
+
+       if ((ret = service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+               ErrPrint("failed to send a multicast packet:%d\n", ret);
+       }
+}
+
+static void _handler_delete_single(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int num_changes = 0;
+       int ret = 0, ret_p = 0;
+       int priv_id = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+
+       if (packet_get(packet, "si", &pkgname, &priv_id) == 2) {
+               pkgname = _string_get(pkgname);
+
+               ret = notification_noti_delete_by_priv_id_get_changes(pkgname, priv_id, &num_changes);
+
+               DbgPrint("priv_id: [%d] num_delete:%d\n", priv_id, num_changes);
+               packet_reply = packet_create_reply(packet, "ii", ret, priv_id);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (ret != NOTIFICATION_ERROR_NONE || num_changes <= 0) {
+                       ErrPrint("failed to delete a notification:%d %d\n", ret, num_changes);
+                       return ;
+               }
+
+               packet_service = packet_create("del_noti_single", "ii", 1, priv_id);
+               if (packet_service != NULL) {
+                       if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                               ErrPrint("failed to send a multicast packet: %d\n", ret_p);
+                       }
+                       packet_destroy(packet_service);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_delete_multiple(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       struct packet *packet_service = NULL;
+       char *pkgname = NULL;
+       notification_type_e type = 0;
+       int num_deleted = 0;
+       int *list_deleted = NULL;
+
+       if (packet_get(packet, "si", &pkgname, &type) == 2) {
+               pkgname = _string_get(pkgname);
+               DbgPrint("pkgname: [%s] type: [%d]\n", pkgname, type);
+
+               ret = notification_noti_delete_all(type, pkgname, &num_deleted, &list_deleted);
+               DbgPrint("ret: [%d] num_deleted: [%d]\n", ret, num_deleted);
+
+               packet_reply = packet_create_reply(packet, "ii", ret, num_deleted);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (ret != NOTIFICATION_ERROR_NONE) {
+                       ErrPrint("failed to delete notifications:%d\n", ret);
+                       if (list_deleted != NULL) {
+                               DbgFree(list_deleted);
+                       }
+                       return ;
+               }
+
+               if (num_deleted > 0) {
+                       if (num_deleted <= NOTIFICATION_DEL_PACKET_UNIT) {
+                               packet_service = _packet_create_with_list(num_deleted, list_deleted, 0);
+
+                               if (packet_service) {
+                                       if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                               ErrPrint("failed to send a multicast packet: %d\n", ret_p);
+                                       }
+                                       packet_destroy(packet_service);
+                               } else {
+                                       ErrPrint("failed to create a multicast packet\n");
+                               }
+                       } else {
+                               int set = 0;
+                               int set_total = num_deleted / NOTIFICATION_DEL_PACKET_UNIT;
+
+                               for (set = 0; set <= set_total; set++) {
+                                       packet_service = _packet_create_with_list(num_deleted,
+                                                       list_deleted, set * NOTIFICATION_DEL_PACKET_UNIT);
+
+                                       if (packet_service) {
+                                               if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+                                                       ErrPrint("failed to send a multicast packet:%d\n", ret_p);
+                                               }
+                                               packet_destroy(packet_service);
+                                       } else {
+                                               ErrPrint("failed to create a multicast packet\n");
+                                       }
+                               }
+                       }
+               }
+
+               if (list_deleted != NULL) {
+                       DbgFree(list_deleted);
+                       list_deleted = NULL;
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_noti_property_set(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       char *pkgname = NULL;
+       char *property = NULL;
+       char *value = NULL;
+
+       if (packet_get(packet, "sss", &pkgname, &property, &value) == 3) {
+               pkgname = _string_get(pkgname);
+               property = _string_get(property);
+               value = _string_get(value);
+
+               ret = notification_setting_db_set(pkgname, property, value);
+
+               packet_reply = packet_create_reply(packet, "ii", ret, ret);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (ret != NOTIFICATION_ERROR_NONE) {
+                       ErrPrint("failed to set noti property:%d\n", ret);
+               }
+       } else {
+               ErrPrint("Failed to get data from the packet");
+       }
+}
+
+static void _handler_noti_property_get(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0, ret_p = 0;
+       struct packet *packet_reply = NULL;
+       char *pkgname = NULL;
+       char *property = NULL;
+       char *value = NULL;
+
+       if (packet_get(packet, "sss", &pkgname, &property) == 2) {
+               pkgname = _string_get(pkgname);
+               property = _string_get(property);
+
+               ret = notification_setting_db_get(pkgname, property, &value);
+
+               packet_reply = packet_create_reply(packet, "is", ret, value);
+               if (packet_reply) {
+                       if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                               ErrPrint("failed to send reply packet:%d\n", ret_p);
+                       }
+                       packet_destroy(packet_reply);
+               } else {
+                       ErrPrint("failed to create a reply packet\n");
+               }
+
+               if (value != NULL) {
+                       DbgFree(value);
+               }
+       }
+}
+
+static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0;
+       struct packet *packet_reply;
+
+       ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
+
+       packet_reply = packet_create_reply(packet, "i", ret);
+       if (packet_reply) {
+               if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("failed to send reply packet:%d\n", ret);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("failed to create a reply packet\n");
+       }
+}
+
+static void _handler_post_toast_message(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int ret = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "i", ret);
+       if (packet_reply) {
+               if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("failed to send reply packet:%d\n", ret);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("failed to create a reply packet\n");
+       }
+
+       if ((ret = service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE)) < 0) {
+               ErrPrint("failed to send a multicast packet:%d\n", ret);
+       }
+
+}
+
+/*!
+ * SERVICE PERMISSION CHECK
+ */
+static void _permission_check_common(struct tcb *tcb, struct packet *packet)
+{
+       int ret_p = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "ii", NOTIFICATION_ERROR_PERMISSION_DENIED, 0);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("Failed to send a reply packet:%d", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("Failed to create a reply packet");
+       }
+}
+
+static void _permission_check_refresh(struct tcb *tcb, struct packet *packet)
+{
+       int ret_p = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "i", NOTIFICATION_ERROR_PERMISSION_DENIED);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("Failed to send a reply packet:%d", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("Failed to create a reply packet");
+       }
+}
+
+static void _permission_check_property_get(struct tcb *tcb, struct packet *packet)
+{
+       int ret_p = 0;
+       struct packet *packet_reply = NULL;
+
+       packet_reply = packet_create_reply(packet, "is", NOTIFICATION_ERROR_PERMISSION_DENIED, NULL);
+       if (packet_reply) {
+               if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
+                       ErrPrint("Failed to send a reply packet:%d", ret_p);
+               }
+               packet_destroy(packet_reply);
+       } else {
+               ErrPrint("Failed to create a reply packet");
+       }
+}
+
+static int _persmission_check(int fd, struct noti_service *service)
+{
+       int ret;
+
+       if (service->rule != NULL && service->access != NULL) {
+               ret = security_server_check_privilege_by_sockfd(fd, service->rule, service->access);
+               if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+                       ErrPrint("SMACK:Access denied\n");
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
+/*!
+ * NOTIFICATION SERVICE INITIALIZATION
+ */
+static void _notification_data_init(void)
+{
+       int property = 0;
+       int priv_id = 0;
+       char *noti_pkgname = NULL;
+       notification_h noti = NULL;
+       notification_list_h noti_list = NULL;
+       notification_list_h noti_list_head = NULL;
+       notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
+
+       notification_get_list(NOTIFICATION_TYPE_NONE, -1, &noti_list);
+       noti_list_head = noti_list;
+
+       while (noti_list != NULL) {
+               noti = notification_list_get_data(noti_list);
+               if (noti) {
+                       notification_get_id(noti, NULL, &priv_id);
+                       notification_get_pkgname(noti, &noti_pkgname);
+                       notification_get_property(noti, &property);
+                       notification_get_type(noti, &noti_type);
+
+                       if (noti_type == NOTIFICATION_TYPE_ONGOING
+                                       || property & NOTIFICATION_PROP_VOLATILE_DISPLAY) {
+                               notification_noti_delete_by_priv_id(noti_pkgname, priv_id);
+                       }
+               }
+               noti_list = notification_list_get_next(noti_list);
+       }
+
+       if (noti_list_head != NULL) {
+               notification_free_list(noti_list_head);
+       }
+}
+
+static void _notification_init(void) {
+       int ret = -1;
+       int restart_count = 0;
+
+       ret = vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
+       if (ret == 0 && restart_count <= 1) {
+               _notification_data_init();
+       }
+}
+
+/*!
+ * SERVICE THREAD
+ */
+static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
+{
+       int i = 0;
+       const char *command;
+
+       static struct noti_service service_req_table[] = {
+               {
+                       .cmd = "add_noti",
+                       .handler = _handler_check_noti_by_tag,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "update_noti",
+                       .handler = _handler_update,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "load_noti_by_tag",
+                       .handler = _handler_load_noti_by_tag,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "r",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "refresh_noti",
+                       .handler = _handler_refresh,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_refresh,
+               },
+               {
+                       .cmd = "del_noti_single",
+                       .handler = _handler_delete_single,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "del_noti_multiple",
+                       .handler = _handler_delete_multiple,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "set_noti_property",
+                       .handler = _handler_noti_property_set,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "w",
+                       .handler_access_error = _permission_check_common,
+               },
+               {
+                       .cmd = "get_noti_property",
+                       .handler = _handler_noti_property_get,
+                       .rule = "data-provider-master::notification.client",
+                       .access = "r",
+                       .handler_access_error = _permission_check_property_get,
+               },
+               {
+                       .cmd = "service_register",
+                       .handler = _handler_service_register,
+                       .rule = NULL,
+                       .access = NULL,
+                       .handler_access_error = NULL,
+               },
+               {
+                       .cmd = "post_toast",
+                       .handler = _handler_post_toast_message,
+                       .rule = NULL,
+                       .access = NULL,
+                       .handler_access_error = NULL,
+               },
+               {
+                       .cmd = NULL,
+                       .handler = NULL,
+                       .rule = NULL,
+                       .access = NULL,
+                       .handler_access_error = NULL,
+               },
+       };
+
+       if (!packet) {
+               DbgPrint("TCB: %p is terminated\n", tcb);
+               return 0;
+       }
+
+       command = packet_command(packet);
+       if (!command) {
+               ErrPrint("Invalid command\n");
+               return -EINVAL;
+       }
+
+       switch (packet_type(packet)) {
+       case PACKET_REQ:
+               /* Need to send reply packet */
+               DbgPrint("%p REQ: Command: [%s]\n", tcb, command);
+
+               for (i = 0; service_req_table[i].cmd; i++) {
+                       if (strcmp(service_req_table[i].cmd, command)) {
+                               continue;
+                       }
+
+                       if (_persmission_check(tcb_fd(tcb), &(service_req_table[i])) == 1) {
+                               service_req_table[i].handler(tcb, packet, data);
+                       } else {
+                               if (service_req_table[i].handler_access_error != NULL) {
+                                       service_req_table[i].handler_access_error(tcb, packet);
+                               }
+                       }
+                       break;
+               }
+
+               break;
+       case PACKET_REQ_NOACK:
+               break;
+       case PACKET_ACK:
+               break;
+       default:
+               ErrPrint("Packet type is not valid[%s]\n", command);
+               return -EINVAL;
+       }
+
+       /*!
+        * return value has no meanning,
+        * it will be printed by dlogutil.
+        */
+       return 0;
+}
+
+
+/*!
+ * MAIN THREAD
+ * Do not try to do anyother operation in these functions
+ */
+HAPI int notification_service_init(void)
+{
+       if (s_info.svc_ctx) {
+               ErrPrint("Already initialized\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       _notification_init();
+
+       s_info.svc_ctx = service_common_create(NOTIFICATION_SOCKET, NOTIFICATION_SMACK_LABEL, service_thread_main, NULL);
+       if (!s_info.svc_ctx) {
+               ErrPrint("Unable to activate service thread\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Successfully initiated\n");
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int notification_service_fini(void)
+{
+       if (!s_info.svc_ctx) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       service_common_destroy(s_info.svc_ctx);
+       s_info.svc_ctx = NULL;
+       DbgPrint("Successfully Finalized\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/package.c b/src/package.c
new file mode 100644 (file)
index 0000000..0481175
--- /dev/null
@@ -0,0 +1,1772 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h> /* strcmp */
+#include <stdlib.h> /* free */
+
+#include <dlog.h>
+#include <Eina.h>
+
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+#include <pkgmgr-info.h>
+
+#include "critical_log.h"
+#include "debug.h"
+#include "util.h"
+#include "parser.h"
+#include "conf.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "package.h"
+#include "fault_manager.h"
+#include "instance.h"
+#include "script_handler.h"
+#include "group.h"
+#include "io.h"
+#include "pkgmgr.h"
+#include "xmonitor.h"
+
+int errno;
+
+struct fault_info {
+       double timestamp;
+       char *filename;
+       char *function;
+};
+
+/*!
+ * pkg_info describes the loaded package.
+ */
+
+struct pkg_info {
+       char *pkgid;
+       char *widget_id;
+
+       struct {
+               enum widget_widget_type type;
+
+               union {
+                       struct {
+                               char *path;
+                               char *group;
+                       } script;
+
+                       struct {
+                               /*!< Reserved for future use */
+                       } file;
+
+                       struct {
+                               /*!< Reserved for future use */
+                       } text;
+
+                       struct {
+                               /*!< Reserved for future use */
+                       } buffer;
+               } info;
+
+               unsigned int size_list;
+               char *auto_launch;
+               int pinup;
+               int timeout;
+               double period;
+               char *libexec;
+       } widget;
+
+       struct {
+               enum widget_gbar_type type;
+
+               union {
+                       struct {
+                               char *path;
+                               char *group;
+                       } script;
+
+                       struct {
+                               /*!< Reserved for future use */
+                       } text;
+
+                       struct {
+                               /*!< Reserved for future use */
+                       } buffer;
+               } info;
+
+               unsigned int width;
+               unsigned int height;
+       } gbar;
+
+       int network;
+       int secured;
+       int direct_input;
+       char *script; /* script type: edje, ... */
+       char *abi;
+       char *hw_acceleration;
+       char *category;
+
+       double faulted_at; /* Fault started time */
+       int fault_count;
+       struct fault_info *fault_info;
+
+       struct slave_node *slave;
+       int refcnt;
+
+       Eina_List *inst_list;
+       Eina_List *ctx_list;
+
+       int is_uninstalled;
+};
+
+static struct {
+       Eina_List *pkg_list;
+} s_info = {
+       .pkg_list = NULL,
+};
+
+static int slave_activated_cb(struct slave_node *slave, void *data)
+{
+       struct pkg_info *info = data;
+       struct inst_info *inst;
+       Eina_List *l;
+       Eina_List *n;
+       int cnt;
+       int ret;
+
+       if (!slave_need_to_reactivate_instances(slave)) {
+               DbgPrint("Do not need to reactivate instances\n");
+               return 0;
+       }
+
+       cnt = 0;
+       EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+               ret = instance_recover_state(inst);
+               if (!ret) {
+                       continue;
+               }
+
+               instance_thaw_updator(inst);
+               cnt++;
+       }
+
+       DbgPrint("Recover state for %d instances of %s\n", cnt, package_name(info));
+       return 0;
+}
+
+static int slave_fault_cb(struct slave_node *slave, void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct inst_info *inst;
+       struct pkg_info *info = (struct pkg_info *)data;
+
+       if (package_is_fault(info)) {
+               ErrPrint("Already faulted package: %s\n", package_name(info));
+               return 0;
+       }
+
+       (void)package_set_fault_info(info, util_timestamp(), slave_name(slave), __func__);
+       fault_broadcast_info(package_name(info), slave_name(slave), __func__);
+
+       DbgPrint("Slave critical fault - package: %s (by slave fault %s\n", package_name(info), slave_name(slave));
+       EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+               DbgPrint("Destroy instance %p\n", inst);
+               instance_destroyed(inst, WIDGET_ERROR_FAULT);
+       }
+
+       return 0;
+}
+
+static int slave_deactivated_cb(struct slave_node *slave, void *data)
+{
+       struct pkg_info *info = data;
+       struct inst_info *inst;
+       Eina_List *l;
+       Eina_List *n;
+       int cnt = 0;
+
+       if (info->fault_info) {
+               EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+                       instance_destroyed(inst, WIDGET_ERROR_FAULT);
+               }
+       } else {
+               EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+                       cnt += instance_need_slave(inst);
+                       /*!
+                        * instance_deactivated will call the slave_unload_instance.
+                        * if the loaded instance counter meets 0,
+                        * the slave will be deactivated.
+                        * so we should not call the instance activate function
+                        * from here.
+                        *
+                        * activate slave when the slave is reactivated
+                        */
+               }
+       }
+
+       return cnt ? SLAVE_NEED_TO_REACTIVATE : 0;
+}
+
+static int xmonitor_paused_cb(void *data)
+{
+       struct pkg_info *info = (struct pkg_info *)data;
+       struct inst_info *inst;
+       Eina_List *l;
+
+       if (slave_state(info->slave) != SLAVE_TERMINATED) {
+               return 0;
+       }
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               instance_freeze_updator(inst);
+       }
+
+       return 0;
+}
+
+static int xmonitor_resumed_cb(void *data)
+{
+       struct pkg_info *info = data;
+       struct inst_info *inst;
+       Eina_List *l;
+
+       if (slave_state(info->slave) != SLAVE_TERMINATED) {
+               return 0;
+       }
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               instance_thaw_updator(inst);
+       }
+
+       return 0;
+}
+
+static int slave_paused_cb(struct slave_node *slave, void *data)
+{
+       struct pkg_info *info = (struct pkg_info *)data;
+       struct inst_info *inst;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               instance_freeze_updator(inst);
+       }
+
+       return 0;
+}
+
+static int slave_resumed_cb(struct slave_node *slave, void *data)
+{
+       struct pkg_info *info = (struct pkg_info *)data;
+       struct inst_info *inst;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               instance_thaw_updator(inst);
+       }
+
+       return 0;
+}
+
+static inline void destroy_package(struct pkg_info *info)
+{
+       eina_list_free(info->ctx_list);
+       /* This items will be deleted from group_del_widget */
+       info->ctx_list = NULL;
+
+       group_del_widget(info->widget_id);
+       package_clear_fault(info);
+
+       s_info.pkg_list = eina_list_remove(s_info.pkg_list, info);
+
+       if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+               DbgFree(info->widget.info.script.path);
+               DbgFree(info->widget.info.script.group);
+       }
+
+       if (info->gbar.type == GBAR_TYPE_SCRIPT) {
+               DbgFree(info->gbar.info.script.path);
+               DbgFree(info->gbar.info.script.group);
+       }
+
+       DbgFree(info->script);
+       DbgFree(info->abi);
+       DbgFree(info->widget_id);
+       DbgFree(info->widget.libexec);
+       DbgFree(info->widget.auto_launch);
+       DbgFree(info->pkgid);
+       DbgFree(info->hw_acceleration);
+
+       DbgFree(info);
+}
+
+static inline int load_conf(struct pkg_info *info)
+{
+       struct parser *parser;
+       const char *str;
+       const char *group;
+
+       parser = parser_load(info->widget_id);
+       if (!parser) {
+               info->widget.size_list = 0x01; /* Default */
+
+               info->script = strdup(WIDGET_CONF_DEFAULT_SCRIPT);
+               if (!info->script) {
+                       ErrPrint("strdup: %d\n", errno);
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               info->abi = strdup(WIDGET_CONF_DEFAULT_ABI);
+               if (!info->abi) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(info->script);
+                       info->script = NULL;
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               info->gbar.width = WIDGET_CONF_BASE_W;
+               info->gbar.height = WIDGET_CONF_BASE_H >> 2;
+               info->widget.pinup = 1;
+               return WIDGET_ERROR_NONE;
+       }
+
+       info->widget.type = WIDGET_TYPE_FILE;
+       if (parser_text_widget(parser)) {
+               info->widget.type = WIDGET_TYPE_TEXT;
+       } else if (parser_buffer_widget(parser)) {
+               info->widget.type = WIDGET_TYPE_BUFFER;
+       } else {
+               str = parser_widget_path(parser);
+               if (str) {
+                       info->widget.type = WIDGET_TYPE_SCRIPT;
+
+                       info->widget.info.script.path = strdup(str);
+                       if (!info->widget.info.script.path) {
+                               ErrPrint("strdup: %d\n", errno);
+                               parser_unload(parser);
+                               return WIDGET_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       str = parser_widget_group(parser);
+                       if (str) {
+                               info->widget.info.script.group = strdup(str);
+                               if (!info->widget.info.script.group) {
+                                       ErrPrint("strdup: %d\n", errno);
+                                       DbgFree(info->widget.info.script.path);
+                                       parser_unload(parser);
+                                       return WIDGET_ERROR_OUT_OF_MEMORY;
+                               }
+                       }
+               }
+       }
+
+       if (parser_text_gbar(parser)) {
+               info->gbar.type = GBAR_TYPE_TEXT;
+       } else if (parser_buffer_gbar(parser)) {
+               info->gbar.type = GBAR_TYPE_BUFFER;
+       } else {
+               str = parser_gbar_path(parser);
+               if (str) {
+                       info->gbar.type = GBAR_TYPE_SCRIPT;
+                       info->gbar.info.script.path = strdup(str);
+                       if (!info->gbar.info.script.path) {
+                               ErrPrint("strdup: %d\n", errno);
+                               if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+                                       DbgFree(info->widget.info.script.path);
+                                       DbgFree(info->widget.info.script.group);
+                               }
+                               parser_unload(parser);
+                               return WIDGET_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       str = parser_gbar_group(parser);
+                       if (str) {
+                               info->gbar.info.script.group = strdup(str);
+                               if (!info->gbar.info.script.group) {
+                                       ErrPrint("strdup: %d\n", errno);
+                                       DbgFree(info->gbar.info.script.path);
+                                       if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+                                               DbgFree(info->widget.info.script.path);
+                                               DbgFree(info->widget.info.script.group);
+                                       }
+                                       parser_unload(parser);
+                                       return WIDGET_ERROR_OUT_OF_MEMORY;
+                               }
+                       }
+               }
+       }
+
+       str = parser_script(parser);
+       str = str ? str : WIDGET_CONF_DEFAULT_SCRIPT;
+       info->script = strdup(str);
+       if (!info->script) {
+               ErrPrint("strdup: %d\n", errno);
+               if (info->gbar.type == GBAR_TYPE_SCRIPT) {
+                       DbgFree(info->gbar.info.script.path);
+                       DbgFree(info->gbar.info.script.group);
+               }
+
+               if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+                       DbgFree(info->widget.info.script.path);
+                       DbgFree(info->widget.info.script.group);
+               }
+
+               parser_unload(parser);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       str = parser_abi(parser);
+       str = str ? str : WIDGET_CONF_DEFAULT_ABI;
+       info->abi = strdup(str);
+       if (!info->abi) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(info->script);
+               if (info->gbar.type == GBAR_TYPE_SCRIPT) {
+                       DbgFree(info->gbar.info.script.path);
+                       DbgFree(info->gbar.info.script.group);
+               }
+
+               if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+                       DbgFree(info->widget.info.script.path);
+                       DbgFree(info->widget.info.script.group);
+               }
+               parser_unload(parser);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->widget.timeout = parser_timeout(parser);
+       info->network = parser_network(parser);
+
+       info->widget.period = parser_period(parser);
+       if (info->widget.period < 0.0f) {
+               info->widget.period = 0.0f;
+       } else if (info->widget.period > 0.0f && info->widget.period < WIDGET_CONF_MINIMUM_PERIOD) {
+               info->widget.period = WIDGET_CONF_MINIMUM_PERIOD;
+       }
+
+       info->widget.size_list = parser_size(parser);
+
+       str = parser_auto_launch(parser);
+       str = str ? str : "";
+       info->widget.auto_launch = strdup(str);
+       if (!info->widget.auto_launch) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(info->abi);
+               DbgFree(info->script);
+               if (info->gbar.type == GBAR_TYPE_SCRIPT) {
+                       DbgFree(info->gbar.info.script.path);
+                       DbgFree(info->gbar.info.script.group);
+               }
+
+               if (info->widget.type == WIDGET_TYPE_SCRIPT) {
+                       DbgFree(info->widget.info.script.path);
+                       DbgFree(info->widget.info.script.group);
+               }
+               parser_unload(parser);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->secured = parser_secured(parser);
+       info->widget.pinup = parser_pinup(parser);
+
+       parser_get_gbar_size(parser, &info->gbar.width, &info->gbar.height);
+
+       group = parser_group_str(parser);
+       if (group && group_add_widget(group, info->widget_id) < 0) {
+               ErrPrint("Failed to build cluster tree for %s{%s}\n", info->widget_id, group);
+       }
+
+       parser_unload(parser);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct pkg_info *package_create(const char *pkgid, const char *widget_id)
+{
+       struct pkg_info *pkginfo;
+
+       pkginfo = calloc(1, sizeof(*pkginfo));
+       if (!pkginfo) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       pkginfo->pkgid = strdup(pkgid);
+       if (!pkginfo->pkgid) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(pkginfo);
+               return NULL;
+       }
+
+       pkginfo->widget_id = io_widget_pkgname(widget_id);
+       if (!pkginfo->widget_id) {
+               ErrPrint("Failed to get pkgname, fallback to fs checker\n");
+               pkginfo->widget_id = strdup(widget_id);
+               if (!pkginfo->widget_id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(pkginfo->pkgid);
+                       DbgFree(pkginfo);
+                       return NULL;
+               }
+       }
+
+       if (io_load_package_db(pkginfo) < 0) {
+               ErrPrint("Failed to load DB, fall back to conf file loader\n");
+               if (load_conf(pkginfo) < 0) {
+                       ErrPrint("Failed to initiate the conf file loader\n");
+                       DbgFree(pkginfo->widget_id);
+                       DbgFree(pkginfo->pkgid);
+                       DbgFree(pkginfo);
+                       return NULL;
+               }
+       }
+
+       package_ref(pkginfo);
+
+       s_info.pkg_list = eina_list_append(s_info.pkg_list, pkginfo);
+
+       return pkginfo;
+}
+
+HAPI int package_destroy(struct pkg_info *info)
+{
+       package_unref(info);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI Eina_List *package_ctx_info(struct pkg_info *pkginfo)
+{
+       return pkginfo->ctx_list;
+}
+
+HAPI void package_add_ctx_info(struct pkg_info *pkginfo, struct context_info *info)
+{
+       pkginfo->ctx_list = eina_list_append(pkginfo->ctx_list, info);
+}
+
+HAPI void package_del_ctx_info(struct pkg_info *pkginfo, struct context_info *info)
+{
+       pkginfo->ctx_list = eina_list_remove(pkginfo->ctx_list, info);
+}
+
+HAPI char *package_widget_pkgname(const char *pkgname)
+{
+       char *widget_id;
+
+       widget_id = io_widget_pkgname(pkgname);
+       if (!widget_id) {
+               widget_id = strdup(pkgname);
+               if (!widget_id) {
+                       ErrPrint("strdup: %d\n", errno);
+                       return NULL;
+               }
+       }
+
+       return widget_id;
+}
+
+HAPI int package_is_widget_pkgname(const char *pkgname)
+{
+       char *widget_id;
+       int ret;
+
+       widget_id = package_widget_pkgname(pkgname);
+       ret = !!widget_id;
+       DbgFree(widget_id);
+
+       return ret;
+}
+
+HAPI struct pkg_info *package_find(const char *widget_id)
+{
+       Eina_List *l;
+       struct pkg_info *info;
+
+       if (!widget_id) {
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(s_info.pkg_list, l, info) {
+               if (!strcmp(info->widget_id, widget_id)) {
+                       return info;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct inst_info *package_find_instance_by_id(const char *widget_id, const char *id)
+{
+       Eina_List *l;
+       struct inst_info *inst;
+       struct pkg_info *info;
+
+       info = package_find(widget_id);
+       if (!info) {
+               ErrPrint("Package %s is not exists\n", widget_id);
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               if (!strcmp(instance_id(inst), id)) {
+                       return inst;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct inst_info *package_find_instance_by_timestamp(const char *widget_id, double timestamp)
+{
+       Eina_List *l;
+       struct inst_info *inst;
+       struct pkg_info *info;
+
+       info = package_find(widget_id);
+       if (!info) {
+               ErrPrint("Package %s is not exists\n", widget_id);
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               if (instance_timestamp(inst) == timestamp) {
+                       return inst;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI int package_dump_fault_info(struct pkg_info *info)
+{
+       if (!info->fault_info) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       CRITICAL_LOG("=============\n");
+       CRITICAL_LOG("faulted at %lf\n", info->fault_info->timestamp);
+       CRITICAL_LOG("Package: %s\n", info->widget_id);
+       CRITICAL_LOG("Function: %s\n", info->fault_info->function);
+       CRITICAL_LOG("InstanceID: %s\n", info->fault_info->filename);
+       return WIDGET_ERROR_NONE;
+}
+
+static int package_is_faulted(struct pkg_info *info, double timestamp)
+{
+       if ((timestamp - info->faulted_at) < WIDGET_CONF_FAULT_DETECT_IN_TIME) {
+               info->fault_count++;
+               DbgPrint("Triggered faulted_in_time (%lf)\n", WIDGET_CONF_FAULT_DETECT_IN_TIME);
+       } else {
+               info->fault_count = 1;
+               info->faulted_at = timestamp;
+       }
+
+       if (info->fault_count > WIDGET_CONF_FAULT_DETECT_COUNT) {
+               info->fault_count = 0;
+               info->faulted_at = timestamp;
+               DbgPrint("Triggered fault_count, return 1\n", WIDGET_CONF_FAULT_DETECT_COUNT);
+               return 1;
+       }
+
+       DbgPrint("Faulted: %d (%d / %lf)\n", info->fault_count, WIDGET_CONF_FAULT_DETECT_COUNT, WIDGET_CONF_FAULT_DETECT_IN_TIME);
+       return 0;
+}
+
+HAPI int package_get_fault_info(struct pkg_info *info, double *timestamp, const char **filename, const char **function)
+{
+       if (!info->fault_info) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       *timestamp = info->fault_info->timestamp;
+       *filename = info->fault_info->filename;
+       *function = info->fault_info->function;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_set_fault_info(struct pkg_info *info, double timestamp, const char *filename, const char *function)
+{
+       struct fault_info *fault;
+
+       if (!package_is_faulted(info, timestamp)) {
+               DbgPrint("Faulted but excused\n");
+               return WIDGET_ERROR_CANCELED;
+       }
+
+       package_clear_fault(info);
+
+       fault = calloc(1, sizeof(*fault));
+       if (!fault) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       fault->timestamp = timestamp;
+       if (!filename) {
+               filename = "unknown";
+       }
+       if (!function) {
+               function = "unknown";
+       }
+
+       fault->filename = strdup(filename);
+       if (!fault->filename) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(fault);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       fault->function = strdup(function);
+       if (!fault->function) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(fault->filename);
+               DbgFree(fault);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       info->fault_info = fault;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_clear_fault(struct pkg_info *info)
+{
+       if (!info->fault_info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       package_dump_fault_info(info);
+
+       DbgFree(info->fault_info->function);
+       DbgFree(info->fault_info->filename);
+       DbgFree(info->fault_info);
+       info->fault_info = NULL;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const int const package_is_fault(const struct pkg_info *info)
+{
+       return !!info->fault_info;
+}
+
+HAPI struct slave_node * const package_slave(const struct pkg_info *info)
+{
+       return info->slave;
+}
+
+HAPI const int const package_timeout(const struct pkg_info *info)
+{
+       return info->widget.timeout;
+}
+
+HAPI void package_set_timeout(struct pkg_info *info, int timeout)
+{
+       info->widget.timeout = timeout;
+}
+
+HAPI const double const package_period(const struct pkg_info *info)
+{
+       return info->widget.period;
+}
+
+HAPI void package_set_period(struct pkg_info *info, double period)
+{
+       info->widget.period = period;
+}
+
+HAPI const int const package_secured(const struct pkg_info *info)
+{
+       return info->secured;
+}
+
+HAPI void package_set_secured(struct pkg_info *info, int secured)
+{
+       info->secured = secured;
+}
+
+HAPI const char * const package_script(const struct pkg_info *info)
+{
+       return info->script;
+}
+
+HAPI int package_set_script(struct pkg_info *info, const char *script)
+{
+       char *tmp;
+
+       tmp = strdup(script);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->script);
+       info->script = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char * const package_abi(const struct pkg_info *info)
+{
+       return info->abi;
+}
+
+HAPI int package_set_abi(struct pkg_info *info, const char *abi)
+{
+       char *tmp;
+       tmp = strdup(abi);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->abi);
+       info->abi = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char * const package_widget_path(const struct pkg_info *info)
+{
+       if (info->widget.type != WIDGET_TYPE_SCRIPT) {
+               return NULL;
+       }
+
+       return info->widget.info.script.path;
+}
+
+HAPI int package_set_widget_path(struct pkg_info *info, const char *path)
+{
+       char *tmp;
+
+       if (info->widget.type != WIDGET_TYPE_SCRIPT) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(path);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->widget.info.script.path);
+       info->widget.info.script.path = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char * const package_widget_group(const struct pkg_info *info)
+{
+       if (info->widget.type != WIDGET_TYPE_SCRIPT) {
+               return NULL;
+       }
+
+       return info->widget.info.script.group;
+}
+
+HAPI int package_set_widget_group(struct pkg_info *info, const char *group)
+{
+       char *tmp;
+
+       if (info->widget.type != WIDGET_TYPE_SCRIPT) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(group);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->widget.info.script.group);
+       info->widget.info.script.group = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char * const package_gbar_path(const struct pkg_info *info)
+{
+       if (info->gbar.type != GBAR_TYPE_SCRIPT) {
+               return NULL;
+       }
+
+       return info->gbar.info.script.path;
+}
+
+HAPI int package_set_gbar_path(struct pkg_info *info, const char *path)
+{
+       char *tmp;
+
+       if (info->gbar.type != GBAR_TYPE_SCRIPT) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(path);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->gbar.info.script.path);
+       info->gbar.info.script.path = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char * const package_gbar_group(const struct pkg_info *info)
+{
+       if (info->gbar.type != GBAR_TYPE_SCRIPT) {
+               return NULL;
+       }
+
+       return info->gbar.info.script.group;
+}
+
+HAPI int package_set_gbar_group(struct pkg_info *info, const char *group)
+{
+       char *tmp;
+
+       if (info->gbar.type != GBAR_TYPE_SCRIPT) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(group);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->gbar.info.script.group);
+       info->gbar.info.script.group = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const int const package_pinup(const struct pkg_info *info)
+{
+       return info->widget.pinup;
+}
+
+HAPI void package_set_pinup(struct pkg_info *info, int pinup)
+{
+       info->widget.pinup = pinup;
+}
+
+HAPI const char * const package_auto_launch(const struct pkg_info *info)
+{
+       return info->widget.auto_launch;
+}
+
+HAPI void package_set_auto_launch(struct pkg_info *info, const char *auto_launch)
+{
+       if (!auto_launch) {
+               auto_launch = "";
+       }
+
+       info->widget.auto_launch = strdup(auto_launch);
+       if (!info->widget.auto_launch) {
+               ErrPrint("strdup: %d\n", errno);
+               return;
+       }
+}
+
+HAPI const unsigned int const package_size_list(const struct pkg_info *info)
+{
+       return info->widget.size_list;
+}
+
+HAPI void package_set_size_list(struct pkg_info *info, unsigned int size_list)
+{
+       info->widget.size_list = size_list;
+}
+
+HAPI const int const package_gbar_width(const struct pkg_info *info)
+{
+       return info->gbar.width;
+}
+
+HAPI void package_set_gbar_width(struct pkg_info *info, int width)
+{
+       info->gbar.width = width;
+}
+
+HAPI const int const package_gbar_height(const struct pkg_info *info)
+{
+       return info->gbar.height;
+}
+
+HAPI void package_set_gbar_height(struct pkg_info *info, int height)
+{
+       info->gbar.height = height;
+}
+
+HAPI struct pkg_info * const package_ref(struct pkg_info *info)
+{
+       info->refcnt++;
+       return info;
+}
+
+HAPI struct pkg_info * const package_unref(struct pkg_info *info)
+{
+       if (info->refcnt == 0) {
+               ErrPrint("Invalid request\n");
+               return NULL;
+       }
+
+       info->refcnt--;
+       if (info->refcnt == 0) {
+               destroy_package(info);
+               info = NULL;
+       }
+
+       return info;
+}
+
+HAPI const int const package_refcnt(const struct pkg_info *info)
+{
+       return info->refcnt;
+}
+
+HAPI const enum widget_widget_type package_widget_type(const struct pkg_info *info)
+{
+       return info ? info->widget.type : WIDGET_TYPE_NONE;
+}
+
+HAPI void package_set_widget_type(struct pkg_info *info, enum widget_widget_type type)
+{
+       info->widget.type = type;
+}
+
+HAPI const char * const package_libexec(struct pkg_info *info)
+{
+       return info->widget.libexec;
+}
+
+HAPI int package_set_libexec(struct pkg_info *info, const char *libexec)
+{
+       char *tmp;
+
+       tmp = strdup(libexec);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->widget.libexec);
+       info->widget.libexec = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_network(struct pkg_info *info)
+{
+       return info->network;
+}
+
+HAPI void package_set_network(struct pkg_info *info, int network)
+{
+       info->network = network;
+}
+
+HAPI void package_set_direct_input(struct pkg_info *info, int direct_input)
+{
+       info->direct_input = direct_input;
+}
+
+HAPI int package_direct_input(const struct pkg_info *info)
+{
+       return info->direct_input;
+}
+
+HAPI const enum widget_gbar_type const package_gbar_type(const struct pkg_info *info)
+{
+       return info ? info->gbar.type : GBAR_TYPE_NONE;
+}
+
+HAPI void package_set_gbar_type(struct pkg_info *info, enum widget_gbar_type type)
+{
+       info->gbar.type = type;
+}
+
+HAPI const char *package_hw_acceleration(struct pkg_info *info)
+{
+       return info->hw_acceleration;
+}
+
+HAPI int package_set_hw_acceleration(struct pkg_info *info, const char *hw_acceleration)
+{
+       char *tmp;
+
+       if (!hw_acceleration || !info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(hw_acceleration);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->hw_acceleration);
+       info->hw_acceleration = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_set_category(struct pkg_info *info, const char *category)
+{
+       char *tmp;
+
+       if (!category || !info) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       tmp = strdup(category);
+       if (!tmp) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(info->category);
+       info->category = tmp;
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char *package_category(struct pkg_info *info)
+{
+       return info->category;
+}
+
+/*!
+ * \note
+ * Add the instance to the package info.
+ * If a package has no slave, assign a new slave.
+ */
+static inline int assign_new_slave(const char *slave_pkgname, struct pkg_info *info)
+{
+       char *s_name;
+       int launch_async = 0;
+
+       s_name = util_slavename();
+       if (!s_name) {
+               ErrPrint("Failed to get a new slave name\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       launch_async = (!!package_category(info) && !strcmp(package_category(info), CATEGORY_WATCH_CLOCK));
+
+       DbgPrint("New slave[%s] is assigned for %s (using %s / abi[%s] / accel[%s] / launch_async[%d])\n", s_name, info->widget_id, slave_pkgname, info->abi, info->hw_acceleration, launch_async);
+       info->slave = slave_create(s_name, info->secured, info->abi, slave_pkgname, info->network, info->hw_acceleration, launch_async);
+
+       DbgFree(s_name);
+
+       if (!info->slave) {
+               /*!
+                * \note
+                * package_destroy will try to remove "info" from the pkg_list.
+                * but we didn't add this to it yet.
+                * If the list method couldn't find an "info" from the list,
+                * it just do nothing so I'll leave this.
+                */
+               return WIDGET_ERROR_FAULT;
+       }
+       /*!
+        * \note
+        * Slave is not activated yet.
+        */
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_add_instance(struct pkg_info *info, struct inst_info *inst)
+{
+       if (!info->inst_list) {
+               char *slave_pkgname;
+
+               slave_pkgname = slave_package_name(info->abi, info->widget_id);
+               if (!slave_pkgname) {
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               info->slave = slave_find_available(slave_pkgname, info->abi, info->secured, info->network, info->hw_acceleration);
+               if (!info->slave) {
+                       int ret;
+
+                       ret = assign_new_slave(slave_pkgname, info);
+                       DbgFree(slave_pkgname);
+                       if (ret < 0) {
+                               return ret;
+                       }
+               } else {
+                       DbgFree(slave_pkgname);
+                       DbgPrint("Slave %s is used for %s\n", slave_name(info->slave), info->widget_id);
+               }
+
+               (void)slave_ref(info->slave);
+               slave_load_package(info->slave);
+               (void)slave_event_callback_add(info->slave, SLAVE_EVENT_DEACTIVATE, slave_deactivated_cb, info);
+               (void)slave_event_callback_add(info->slave, SLAVE_EVENT_ACTIVATE, slave_activated_cb, info);
+               (void)slave_event_callback_add(info->slave, SLAVE_EVENT_FAULT, slave_fault_cb, info);
+
+               if (info->secured || (WIDGET_IS_INHOUSE(package_abi(info)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL)) {
+                       (void)slave_event_callback_add(info->slave, SLAVE_EVENT_PAUSE, slave_paused_cb, info);
+                       (void)slave_event_callback_add(info->slave, SLAVE_EVENT_RESUME, slave_resumed_cb, info);
+
+                       /*!
+                        * \note
+                        * In case of the slave is terminated because of expired TTL timer,
+                        * Master should freeze the all update time.
+                        * But the callback should check the slave's state to prevent from duplicated freezing.
+                        *
+                        * This callback will freeze the timer only if a slave doesn't running.
+                        */
+                       (void)xmonitor_add_event_callback(XMONITOR_PAUSED, xmonitor_paused_cb, info);
+                       (void)xmonitor_add_event_callback(XMONITOR_RESUMED, xmonitor_resumed_cb, info);
+               }
+       }
+
+       info->inst_list = eina_list_append(info->inst_list, inst);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int package_del_instance(struct pkg_info *info, struct inst_info *inst)
+{
+       info->inst_list = eina_list_remove(info->inst_list, inst);
+
+       if (info->inst_list) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (info->slave) {
+               slave_unload_package(info->slave);
+
+               slave_event_callback_del(info->slave, SLAVE_EVENT_FAULT, slave_fault_cb, info);
+               slave_event_callback_del(info->slave, SLAVE_EVENT_DEACTIVATE, slave_deactivated_cb, info);
+               slave_event_callback_del(info->slave, SLAVE_EVENT_ACTIVATE, slave_activated_cb, info);
+
+               if (info->secured || (WIDGET_IS_INHOUSE(package_abi(info)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL)) {
+                       slave_event_callback_del(info->slave, SLAVE_EVENT_PAUSE, slave_paused_cb, info);
+                       slave_event_callback_del(info->slave, SLAVE_EVENT_RESUME, slave_resumed_cb, info);
+
+                       xmonitor_del_event_callback(XMONITOR_PAUSED, xmonitor_paused_cb, info);
+                       xmonitor_del_event_callback(XMONITOR_RESUMED, xmonitor_resumed_cb, info);
+               }
+
+               slave_unref(info->slave);
+               info->slave = NULL;
+       }
+
+       if (info->is_uninstalled) {
+               package_destroy(info);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI Eina_List *package_instance_list(struct pkg_info *info)
+{
+       return info->inst_list;
+}
+
+static int client_created_cb(struct client_node *client, void *data)
+{
+       struct pkg_info *info;
+       Eina_List *l;
+
+       struct inst_info *inst;
+       Eina_List *i_l;
+
+       EINA_LIST_FOREACH(s_info.pkg_list, l, info) {
+               if (info->fault_info) {
+                       fault_unicast_info(client, info->widget_id, info->fault_info->filename, info->fault_info->function);
+                       continue;
+               }
+
+               EINA_LIST_FOREACH(info->inst_list, i_l, inst) {
+                       switch (instance_state(inst)) {
+                       case INST_INIT:
+                               /* Will be send a created event after the instance gets created event */
+                               break;
+                       case INST_ACTIVATED: /*!< This instance is actiavted, and used */
+                       case INST_REQUEST_TO_REACTIVATE: /*!< This instance will be reactivated soon */
+                       case INST_REQUEST_TO_DESTROY: /*!< This instance will be destroy soon */
+                               if (instance_client(inst) == client) {
+                                       instance_unicast_created_event(inst, client);
+                               } else if (instance_client(inst) == NULL) {
+                                       /*!
+                                        * \note
+                                        * Instances are lives in the system cluster/sub-cluster
+                                        */
+                                       if (client_is_subscribed_group(client, instance_cluster(inst), instance_category(inst))
+                                               || client_is_subscribed_by_category(client, package_category(instance_package(inst))))
+                                       {
+                                               instance_unicast_created_event(inst, client);
+                                               DbgPrint("(Subscribed) Created package: %s\n", info->widget_id);
+                                       }
+                               }
+
+                               break;
+                       default:
+                               DbgPrint("%s(%s) is not activated (%d)\n",
+                                               package_name(info), instance_id(inst), instance_state(inst));
+                               break;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+static int io_uninstall_cb(const char *pkgid, const char *widget_id, int prime, void *data)
+{
+       struct pkg_info *info;
+       Eina_List *l;
+       Eina_List *n;
+       struct inst_info *inst;
+
+       DbgPrint("Package %s is uninstalled\n", widget_id);
+       info = package_find(widget_id);
+       if (!info) {
+               DbgPrint("%s is not yet loaded\n", widget_id);
+               return 0;
+       }
+
+       info->is_uninstalled = 1;
+
+       /*!
+        * \NOTE
+        * Don't delete an item from the inst_list.
+        * destroy callback will use this list again.
+        * So, Don't touch it from here.
+        */
+       if (info->inst_list) {
+               EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_UNINSTALL);
+               }
+       } else {
+               package_destroy(info);
+       }
+
+       return 0;
+}
+
+static inline void reload_package_info(struct pkg_info *info)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct inst_info *inst;
+       widget_size_type_e size_type;
+       int width;
+       int height;
+       double old_period;
+
+       DbgPrint("Already exists, try to update it\n");
+
+       old_period = info->widget.period;
+
+       group_del_widget(info->widget_id);
+       package_clear_fault(info);
+
+       /*!
+        * \NOTE:
+        * Nested DB I/O
+        */
+       io_load_package_db(info);
+
+       /*!
+        * \note
+        * Without "is_uninstalled", the package will be kept
+        */
+       EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+               width = instance_widget_width(inst);
+               height = instance_widget_height(inst);
+               widget_service_get_size_type(width, height, &size_type);
+               if (info->widget.size_list & size_type) {
+                       if (instance_period(inst) == old_period) {
+                               instance_reload_period(inst, package_period(info));
+                       }
+                       instance_reload(inst, WIDGET_DESTROY_TYPE_UPGRADE);
+               } else {
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_UNINSTALL);
+               }
+       }
+}
+
+static int io_install_cb(const char *pkgid, const char *widget_id, int prime, void *data)
+{
+       struct pkg_info *info;
+
+       info = package_find(widget_id);
+       if (info) {
+               /*!
+                * Already exists. skip to create this.
+                */
+               return 0;
+       }
+
+       info = package_create(pkgid, widget_id);
+       if (!info) {
+               ErrPrint("Failed to build an info %s\n", widget_id);
+       } else {
+               DbgPrint("widget %s is built\n", widget_id);
+       }
+
+       return 0;
+}
+
+static int uninstall_cb(const char *pkgname, enum pkgmgr_status status, double value, void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct pkg_info *info;
+
+       if (status != PKGMGR_STATUS_END) {
+               return 0;
+       }
+
+       EINA_LIST_FOREACH_SAFE(s_info.pkg_list, l, n, info) {
+               if (!strcmp(info->pkgid, pkgname)) {
+                       io_uninstall_cb(pkgname, info->widget_id, -1, NULL);
+               }
+       }
+
+       return 0;
+}
+
+static int update_cb(const char *pkgname, enum pkgmgr_status status, double value, void *data)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct pkg_info *info;
+
+       if (status != PKGMGR_STATUS_END) {
+               return 0;
+       }
+
+       EINA_LIST_FOREACH_SAFE(s_info.pkg_list, l, n, info) {
+               if (!strcmp(info->pkgid, pkgname)) {
+                       DbgPrint("Update widget_id: %s\n", info->widget_id);
+                       if (io_is_exists(info->widget_id) == 1) {
+                               reload_package_info(info);
+                       } else {
+                               io_uninstall_cb(pkgname, info->widget_id, -1, NULL);
+                       }
+               }
+       }
+
+       (void)io_update_widget_package(pkgname, io_install_cb, NULL);
+       return 0;
+}
+
+static int crawling_widgetes(const char *pkgid, const char *widget_id, int prime, void *data)
+{
+       if (package_find(widget_id)) {
+               ErrPrint("Information of %s is already built\n", widget_id);
+       } else {
+               struct pkg_info *info;
+               info = package_create(pkgid, widget_id);
+               if (info) {
+                       DbgPrint("[%s] information is built prime(%d)\n", widget_id, prime);
+               }
+       }
+
+       return 0;
+}
+
+HAPI int package_init(void)
+{
+       client_global_event_handler_add(CLIENT_GLOBAL_EVENT_CREATE, client_created_cb, NULL);
+       pkgmgr_init();
+
+       pkgmgr_add_event_callback(PKGMGR_EVENT_INSTALL, update_cb, NULL);
+       pkgmgr_add_event_callback(PKGMGR_EVENT_UNINSTALL, uninstall_cb, NULL);
+       pkgmgr_add_event_callback(PKGMGR_EVENT_UPDATE, update_cb, NULL);
+
+       io_crawling_widgetes(crawling_widgetes, NULL);
+       return 0;
+}
+
+HAPI int package_fini(void)
+{
+       Eina_List *p_l;
+       Eina_List *p_n;
+       Eina_List *i_l;
+       Eina_List *i_n;
+       struct pkg_info *info;
+       struct inst_info *inst;
+
+       pkgmgr_del_event_callback(PKGMGR_EVENT_INSTALL, update_cb, NULL);
+       pkgmgr_del_event_callback(PKGMGR_EVENT_UNINSTALL, uninstall_cb, NULL);
+       pkgmgr_del_event_callback(PKGMGR_EVENT_UPDATE, update_cb, NULL);
+       pkgmgr_fini();
+       client_global_event_handler_del(CLIENT_GLOBAL_EVENT_CREATE, client_created_cb, NULL);
+
+       EINA_LIST_FOREACH_SAFE(s_info.pkg_list, p_l, p_n, info) {
+               EINA_LIST_FOREACH_SAFE(info->inst_list, i_l, i_n, inst) {
+                       instance_state_reset(inst);
+                       instance_destroy(inst, WIDGET_DESTROY_TYPE_TERMINATE);
+               }
+
+               package_destroy(info);
+       }
+
+       return 0;
+}
+
+/**
+ * Find a standalone provider
+ */
+HAPI const char *package_find_by_secured_slave(struct slave_node *slave)
+{
+       Eina_List *l;
+       struct pkg_info *info;
+
+       if (!slave_is_secured(slave) && !slave_is_app(slave)) {
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(s_info.pkg_list, l, info) {
+               if (info->slave == slave) {
+                       return info->widget_id;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI const char * const package_name(const struct pkg_info *info)
+{
+       return info->widget_id;
+}
+
+/*!
+ * del_or_creat : 1 == create, 0 == delete
+ */
+HAPI int package_alter_instances_to_client(struct client_node *client, enum alter_type alter)
+{
+       struct pkg_info *info;
+       Eina_List *l;
+
+       struct inst_info *inst;
+       Eina_List *i_l;
+
+       EINA_LIST_FOREACH(s_info.pkg_list, l, info) {
+               EINA_LIST_FOREACH(info->inst_list, i_l, inst) {
+                       if (instance_client(inst) == client) {
+                               DbgPrint("Skip owner\n");
+                               continue;
+                       }
+
+                       if (!client_is_subscribed_group(client, instance_cluster(inst), instance_category(inst))
+                               && !client_is_subscribed_by_category(client, package_category(instance_package(inst))))
+                       {
+                               DbgPrint("Not subscribed [%s]\n", package_category(instance_package(inst)));
+                               continue;
+                       }
+
+                       switch (instance_state(inst)) {
+                       case INST_INIT:
+                       case INST_REQUEST_TO_ACTIVATE:
+                               /* Will be send a created event after the instance gets created event */
+                               switch (alter) {
+                               case ALTER_CREATE:
+                                       DbgPrint("ALTER_CREATE\n");
+                                       if (!instance_has_client(inst, client)) {
+                                               instance_add_client(inst, client);
+                                       }
+                                       break;
+                               case ALTER_DESTROY:
+                                       DbgPrint("ALTER_DESTROY\n");
+                                       if (instance_has_client(inst, client)) {
+                                               instance_del_client(inst, client);
+                                       }
+                                       break;
+                               default:
+                                       break;
+                               }
+                               break;
+                       case INST_ACTIVATED: /*!< This instance is actiavted, and used */
+                       case INST_REQUEST_TO_REACTIVATE: /*!< This instance will be reactivated soon */
+                       case INST_REQUEST_TO_DESTROY: /*!< This instance will be destroy soon */
+                               /*!
+                                * \note
+                                * Instances are lives in the system cluster/sub-cluster
+                                */
+                               switch (alter) {
+                               case ALTER_CREATE:
+                                       DbgPrint("ALTER_CREATE\n");
+                                       if (!instance_has_client(inst, client)) {
+                                               instance_unicast_created_event(inst, client);
+                                               instance_add_client(inst, client);
+                                               DbgPrint("(Subscribed) Created package: %s\n", info->widget_id);
+                                       }
+                                       break;
+                               case ALTER_DESTROY:
+                                       DbgPrint("ALTER_DESTROY\n");
+                                       if (instance_has_client(inst, client)) {
+                                               instance_unicast_deleted_event(inst, client, WIDGET_ERROR_NONE);
+                                               instance_del_client(inst, client);
+                                       }
+                                       break;
+                               default:
+                                       break;
+                               }
+
+                               break;
+                       default:
+                               DbgPrint("%s(%s) is not activated (%d)\n", package_name(info), instance_id(inst), instance_state(inst));
+                               break;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+HAPI const Eina_List *package_list(void)
+{
+       return s_info.pkg_list;
+}
+
+HAPI int const package_fault_count(struct pkg_info *info)
+{
+       return info ? info->fault_count : 0;
+}
+
+HAPI int package_instance_count(struct pkg_info *info)
+{
+       Eina_List *l;
+       struct inst_info *inst;
+       int count = 0;
+
+       EINA_LIST_FOREACH(info->inst_list, l, inst) {
+               switch (instance_state(inst)) {
+               case INST_INIT:
+               case INST_ACTIVATED:
+               case INST_REQUEST_TO_ACTIVATE:
+               case INST_REQUEST_TO_REACTIVATE:
+                       count++;
+                       break;
+               case INST_DESTROYED:
+               case INST_REQUEST_TO_DESTROY:
+               default:
+                       break;
+               }
+       }
+
+       return count;
+}
+
+HAPI int package_is_enabled(const char *appid)
+{
+       pkgmgrinfo_appinfo_h handle;
+       bool enabled;
+       int ret;
+
+       ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
+       if (ret != PMINFO_R_OK) {
+               ErrPrint("Failed to get info\n");
+               return 0;
+       }
+
+       ret = pkgmgrinfo_appinfo_is_enabled(handle, &enabled);
+       if (ret != PMINFO_R_OK) {
+               ErrPrint("Failed to get info\n");
+               enabled = false;
+       }
+
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return enabled == true;
+}
+
+HAPI int package_faulted(struct pkg_info *pkg, int broadcast)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct slave_node *slave;
+       struct inst_info *inst;
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package has no slave?\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       /* Emulated fault routine */
+       // (void)package_set_fault_info(pkg, util_timestamp(), slave_name(slave), __func__);
+       if (broadcast) {
+               fault_broadcast_info(package_name(pkg), slave_name(slave), __func__);
+       }
+
+       DbgPrint("package: %s (forucely faulted %s)\n", package_name(pkg), slave_name(slave));
+       EINA_LIST_FOREACH_SAFE(pkg->inst_list, l, n, inst) {
+               DbgPrint("Destroy instance %p\n", inst);
+               instance_destroy(inst, WIDGET_DESTROY_TYPE_FAULT);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI char *package_get_pkgid(const char *appid)
+{
+       int ret;
+       pkgmgrinfo_appinfo_h handle;
+       char *new_appid = NULL;
+       char *pkgid = NULL;
+
+       ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
+       if (ret != PMINFO_R_OK) {
+               ErrPrint("Failed to get appinfo\n");
+               return NULL;
+       }
+
+       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &new_appid);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               ErrPrint("Failed to get pkgname for (%s)\n", appid);
+               return NULL;
+       }
+
+       if (new_appid && new_appid[0] != '\0') {
+               pkgid = strdup(new_appid);
+               if (!pkgid) {
+                       ErrPrint("strdup: %d\n", errno);
+               }
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+
+       return pkgid;
+}
+
+HAPI int package_del_instance_by_category(const char *category, const char *except_widget_id)
+{
+       Eina_List *l;
+       Eina_List *n;
+       Eina_List *m;
+       struct inst_info *inst;
+       struct pkg_info *info;
+       int ret;
+
+       EINA_LIST_FOREACH(s_info.pkg_list, m, info) {
+               ErrPrint("pkgid[%s] category [%s]\n", info->pkgid, info->category);
+               if (info->category && !strcmp(category, info->category)) {
+                       if (except_widget_id && !strcmp(package_name(info), except_widget_id)){
+                               ErrPrint("package_name[%s] widget_id[%s]\n", package_name(info), except_widget_id);
+                               continue;
+                       }
+
+                       EINA_LIST_FOREACH_SAFE(info->inst_list, l, n, inst) {
+                               ret = instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                               ErrPrint("instance_destroy return [%d]\n", ret);
+                       }
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/parser.c b/src/parser.c
new file mode 100644 (file)
index 0000000..ecdac56
--- /dev/null
@@ -0,0 +1,892 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h> /* strtod */
+#include <errno.h>
+#include <ctype.h> /* isspace */
+
+#include <Eina.h>
+#include <dlog.h>
+
+#include <widget_service.h>
+#include <widget_errno.h>
+#include <widget_conf.h>
+
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+#include "parser.h"
+
+static Eina_List *s_list;
+int errno;
+
+#if __x86__64__
+#define RETURN_TYPE long long
+#else
+#define RETURN_TYPE int
+#endif
+
+struct parser {
+       char *filename;
+       double period;
+       int timeout;
+       int network;
+       char *auto_launch;
+       unsigned int size;
+       unsigned int gbar_width;
+       unsigned int gbar_height;
+       char *group;
+       int secured;
+
+       char *gbar_path;
+       char *gbar_group;
+
+       char *widget_path;
+       char *widget_group;
+       int pinup;
+       int text_gbar;
+       int text_lb;
+       int buffer_gbar;
+       int buffer_lb;
+
+       char *abi;
+
+       char *script;
+};
+
+HAPI double parser_period(struct parser *handle)
+{
+       return handle->period;
+}
+
+HAPI int parser_network(struct parser *handle)
+{
+       return handle->network;
+}
+
+HAPI int parser_timeout(struct parser *handle)
+{
+       return handle->timeout;
+}
+
+HAPI const char *parser_auto_launch(struct parser *handle)
+{
+       return handle->auto_launch;
+}
+
+HAPI const char *parser_script(struct parser *handle)
+{
+       return handle->script;
+}
+
+HAPI const char *parser_abi(struct parser *handle)
+{
+       return handle->abi;
+}
+
+HAPI unsigned int parser_size(struct parser *handle)
+{
+       return handle->size;
+}
+
+HAPI const char *parser_widget_path(struct parser *handle)
+{
+       return handle->widget_path;
+}
+
+HAPI const char *parser_widget_group(struct parser *handle)
+{
+       return handle->widget_group;
+}
+
+HAPI const char *parser_gbar_path(struct parser *handle)
+{
+       return handle->gbar_path;
+}
+
+HAPI const char *parser_gbar_group(struct parser *handle)
+{
+       return handle->gbar_group;
+}
+
+HAPI const char *parser_group_str(struct parser *handle)
+{
+       return handle->group;
+}
+
+HAPI int parser_secured(struct parser *handle)
+{
+       return handle->secured;
+}
+
+HAPI void parser_get_gbar_size(struct parser *handle, unsigned int *width, unsigned int *height)
+{
+       *width = handle->gbar_width;
+       *height = handle->gbar_height;
+}
+
+HAPI int parser_pinup(struct parser *handle)
+{
+       return handle->pinup;
+}
+
+HAPI int parser_text_widget(struct parser *handle)
+{
+       return handle->text_lb;
+}
+
+HAPI int parser_text_gbar(struct parser *handle)
+{
+       return handle->text_gbar;
+}
+
+HAPI int parser_buffer_widget(struct parser *handle)
+{
+       return handle->buffer_lb;
+}
+
+HAPI int parser_buffer_gbar(struct parser *handle)
+{
+       return handle->buffer_gbar;
+}
+
+HAPI RETURN_TYPE parser_find(const char *pkgname)
+{
+       Eina_List *l;
+       struct parser *item;
+       char *filename;
+       int len;
+       int ret;
+
+       len = strlen(pkgname) * 2 + strlen(WIDGET_CONF_CONF_PATH);
+
+       filename = malloc(len);
+       if (!filename) {
+               return (RETURN_TYPE)0;
+       }
+
+       ret = snprintf(filename, len, WIDGET_CONF_CONF_PATH, pkgname, pkgname);
+       if (ret < 0) {
+               DbgFree(filename);
+               return (RETURN_TYPE)0;
+       }
+
+       DbgPrint("Conf file %s for package %s\n", filename, pkgname);
+
+       EINA_LIST_FOREACH(s_list, l, item) {
+               if (!strcmp(item->filename, filename)) {
+                       DbgFree(filename);
+                       return (RETURN_TYPE)item;
+               }
+       }
+
+       DbgFree(filename);
+       return (RETURN_TYPE)0;
+}
+
+static inline int parse_size(const char *buffer, unsigned int *size)
+{
+       register int i;
+       int w;
+       int h;
+
+       enum {
+               START,
+               WIDTH,
+               DELIM,
+               HEIGHT,
+               ERROR,
+               STOP,
+               END
+       } state;
+
+       *size = 0;
+       state = START;
+       i = 0;
+       w = 0;
+       h = 0;
+
+       while (state != ERROR && state != END) {
+               switch (state) {
+               case START:
+                       switch (buffer[i]) {
+                       case '1'...'9':
+                               state = WIDTH;
+                               break;
+                       case ' ':
+                       case '\t':
+                       case ';':
+                               i++;
+                               break;
+                       case '\0':
+                               state = END;
+                               break;
+                       default:
+                               state = ERROR;
+                               break;
+                       }
+                       break;
+               case WIDTH:
+                       switch (buffer[i]) {
+                       case '0'...'9':
+                               w = (w * 10) + (buffer[i] - '0');
+                               i++;
+                               break;
+                       case 'x':
+                               state = DELIM;
+                               i++;
+                               break;
+                       default:
+                               state = ERROR;
+                               break;
+                       }
+
+                       break;
+               case DELIM:
+                       switch (buffer[i]) {
+                       case '1'...'9':
+                               state = HEIGHT;
+                               break;
+                       case ' ':
+                       case '\t':
+                               i++;
+                               break;
+                       default:
+                               state = ERROR;
+                               break;
+                       }
+                       break;
+               case HEIGHT:
+                       switch (buffer[i]) {
+                       case '0'...'9':
+                               h = (h * 10) + (buffer[i] - '0');
+                               i++;
+                               break;
+                       case ';':
+                       case '\0':
+                               state = STOP;
+                               break;
+                       default:
+                               state = ERROR;
+                               break;
+                       }
+                       break;
+               case STOP:
+                       if (w == 1 && h == 1) {
+                               *size |= WIDGET_SIZE_TYPE_1x1;
+                       } else if (w == 2 && h == 1) {
+                               *size |= WIDGET_SIZE_TYPE_2x1;
+                       } else if (w == 2 && h == 2) {
+                               *size |= WIDGET_SIZE_TYPE_2x2;
+                       } else if (w == 4 && h == 1) {
+                               *size |= WIDGET_SIZE_TYPE_4x1;
+                       } else if (w == 4 && h == 2) {
+                               *size |= WIDGET_SIZE_TYPE_4x2;
+                       } else if (w == 4 && h == 3) {
+                               *size |= WIDGET_SIZE_TYPE_4x3;
+                       } else if (w == 4 && h == 4) {
+                               *size |= WIDGET_SIZE_TYPE_4x4;
+                       } else if (w == 21 && h == 21) {
+                               *size |= WIDGET_SIZE_TYPE_EASY_1x1;
+                       } else if (w == 23 && h == 21) {
+                               *size |= WIDGET_SIZE_TYPE_EASY_3x1;
+                       } else if (w == 23 && h == 23) {
+                               *size |= WIDGET_SIZE_TYPE_EASY_3x3;
+                       } else {
+                               ErrPrint("Invalid size type: %dx%d\n", w, h);
+                       }
+
+                       if (buffer[i] == ';') {
+                               state = START;
+                       } else if (buffer[i] == '\0') {
+                               state = END;
+                       }
+
+                       w = 0;
+                       h = 0;
+                       break;
+               default:
+                       return -1;
+               }
+       }
+
+       return *size ? 0 : -1;
+}
+
+/*!
+ * \note
+ * This will change the value of "buffer"
+ */
+static inline const char *rtrim(char *buffer)
+{
+       int len;
+
+       len = strlen(buffer);
+       while (len > 0 && isspace(buffer[len - 1])) {
+               len--;
+       }
+
+       if (len <= 0) {
+               return NULL;
+       }
+
+       buffer[len] = '\0';
+
+       return buffer;
+}
+
+/*!
+ * \note
+ * This will change the value of "buffer"
+ */
+static inline char *dup_rtrim(char *buffer)
+{
+       char *ret;
+
+       if (!rtrim(buffer)) {
+               return NULL;
+       }
+
+       ret = strdup(buffer);
+       if (!ret) {
+               ErrPrint("strdup: %d\n", errno);
+               return NULL;
+       }
+
+       return ret;
+}
+
+static void period_handler(struct parser *item, char *buffer)
+{
+       char *tmp = NULL;
+
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->period = strtod(buffer, &tmp);
+}
+
+static void timeout_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->timeout = atoi(buffer);
+}
+
+static void network_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->network = !!atoi(buffer);
+}
+
+static void auto_launch_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->auto_launch = strdup(buffer);
+       if (!item->auto_launch) {
+               ErrPrint("strdup: %d\n", errno);
+               return;
+       }
+}
+
+static void size_handler(struct parser *item, char *buffer)
+{
+       if (parse_size(buffer, &item->size) == -1) {
+               ErrPrint("Failed to get size\n");
+               item->size = 0x00000001;
+       }
+}
+
+static void gbar_size_handler(struct parser *item, char *buffer)
+{
+       if (sscanf(buffer, "%ux%u", &item->gbar_width, &item->gbar_height) != 2) {
+               ErrPrint("parse pd size\n");
+       }
+}
+
+static void text_widget_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->text_lb = !!atoi(buffer);
+}
+
+static void abi_handler(struct parser *item, char *buffer)
+{
+       item->abi = dup_rtrim(buffer);
+}
+
+static void script_handler(struct parser *item, char *buffer)
+{
+       item->script = dup_rtrim(buffer);
+}
+
+static void buffer_gbar_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->buffer_gbar = !!atoi(buffer);
+}
+
+static void buffer_widget_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->buffer_lb = !!atoi(buffer);
+}
+
+static void text_gbar_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->text_gbar = !!atoi(buffer);
+}
+
+static void pinup_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->pinup = !!atoi(buffer);
+}
+
+static void widget_path_handler(struct parser *item, char *buffer)
+{
+       if (item->widget_path) {
+               DbgFree(item->widget_path);
+       }
+
+       item->widget_path = dup_rtrim(buffer);
+       if (!item->widget_path) {
+               ErrPrint("strdup: %d\n", errno);
+       }
+}
+
+static void group_handler(struct parser *item, char *buffer)
+{
+       if (item->group) {
+               DbgFree(item->group);
+       }
+
+       item->group = dup_rtrim(buffer);
+       if (!item->group) {
+               ErrPrint("strdup: %d\n", errno);
+       }
+}
+
+static void secured_handler(struct parser *item, char *buffer)
+{
+       if (!rtrim(buffer)) {
+               return;
+       }
+
+       item->secured = !!atoi(buffer);
+}
+
+static void widget_group_handler(struct parser *item, char *buffer)
+{
+       if (item->widget_group) {
+               DbgFree(item->widget_group);
+       }
+
+       item->widget_group = dup_rtrim(buffer);
+       if (!item->widget_group) {
+               ErrPrint("strdup: %d\n", errno);
+       }
+}
+
+static void gbar_path_handler(struct parser *item, char *buffer)
+{
+       if (item->gbar_path) {
+               DbgFree(item->gbar_path);
+       }
+
+       item->gbar_path = dup_rtrim(buffer);
+       if (!item->gbar_path) {
+               ErrPrint("strdup: %d\n", errno);
+       }
+}
+
+static void gbar_group_handler(struct parser *item, char *buffer)
+{
+       if (item->gbar_group) {
+               DbgFree(item->gbar_group);
+       }
+
+       item->gbar_group = dup_rtrim(buffer);
+       if (!item->gbar_group) {
+               ErrPrint("strdup: %d\n", errno);
+       }
+}
+
+HAPI struct parser *parser_load(const char *pkgname)
+{
+       struct parser *item;
+       FILE *fp;
+       int c;
+       enum state {
+               START,
+               SPACE,
+               TOKEN,
+               VALUE,
+               ERROR,
+               COMMENT,
+               END
+       } state;
+       int ch_idx;
+       int token_idx;
+       int buffer_idx;
+       int quote;
+       int len;
+       int linelen;
+       char buffer[256];
+       static const struct token_parser {
+               const char *name;
+               void (*handler)(struct parser *, char *buffer);
+       } token_handler[] = {
+               {
+                       .name = "period",
+                       .handler = period_handler,
+               },
+               {
+                       .name = "timeout",
+                       .handler = timeout_handler,
+               },
+               {
+                       .name = "network",
+                       .handler = network_handler,
+               },
+               {
+                       .name = "auto_launch",
+                       .handler = auto_launch_handler,
+               },
+               {
+                       .name = "size",
+                       .handler = size_handler,
+               },
+               {
+                       .name = "group",
+                       .handler = group_handler,
+               },
+               {
+                       .name = "secured",
+                       .handler = secured_handler,
+               },
+               {
+                       .name = "widget_path",
+                       .handler = widget_path_handler,
+               },
+               {
+                       .name = "widget_group",
+                       .handler = widget_group_handler,
+               },
+               {
+                       .name = "gbar_path",
+                       .handler = gbar_path_handler,
+               },
+               {
+                       .name = "gbar_group",
+                       .handler = gbar_group_handler,
+               },
+               {
+                       .name = "gbar_size",
+                       .handler = gbar_size_handler,
+               },
+               {
+                       .name = "pinup",
+                       .handler = pinup_handler,
+               },
+               {
+                       .name = "text_widget",
+                       .handler = text_widget_handler,
+               },
+               {
+                       .name = "text_gbar",
+                       .handler = text_gbar_handler,
+               },
+               {
+                       .name = "buffer_widget",
+                       .handler = buffer_widget_handler,
+               },
+               {
+                       .name = "buffer_gbar",
+                       .handler = buffer_gbar_handler,
+               },
+               {
+                       .name = "script",
+                       .handler = script_handler,
+               },
+               {
+                       .name = "abi",
+                       .handler = abi_handler,
+               },
+               {
+                       .name = NULL,
+                       .handler = NULL,
+               },
+       };
+       int ret;
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               return 0;
+       }
+
+       /* live-, .conf */
+       len = strlen(WIDGET_CONF_CONF_PATH) + strlen(pkgname) * 2;
+       item->filename = malloc(len);
+       if (!item->filename) {
+               ErrPrint("malloc: %d\n", errno);
+               DbgFree(item);
+               return 0;
+       }
+
+       ret = snprintf(item->filename, len, WIDGET_CONF_CONF_PATH, pkgname, pkgname);
+       if (ret < 0) {
+               ErrPrint("snprintf: %d\n", errno);
+               DbgFree(item->filename);
+               DbgFree(item);
+               return 0;
+       }
+
+       item->widget_path = NULL;
+       item->widget_group = NULL;
+       item->gbar_width = 0;
+       item->gbar_height = 0;
+       item->auto_launch = NULL;
+       item->size = 0x00000001;
+       item->group = NULL;
+       item->secured = 0;
+       item->pinup = 0;
+
+       fp = fopen(item->filename, "rt");
+       if (!fp) {
+               DbgFree(item->filename);
+               DbgFree(item);
+               return 0;
+       }
+
+       state = START;
+       ch_idx = 0;
+       token_idx = -1;
+       buffer_idx = 0;
+       quote = 0;
+       linelen = 0;
+       do {
+               c = getc(fp);
+               if ((c == EOF) && (state == VALUE)) {
+                       state = END;
+               }
+
+               switch (state) {
+               case COMMENT:
+                       if (c == CR || c == LF || c == EOF) {
+                               buffer[buffer_idx] = '\0';
+
+                               state = START;
+                               token_idx = -1;
+                               ch_idx = 0;
+                               buffer_idx = 0;
+                               linelen = -1; /* Will be ZERO by follwing increment code */
+                               quote = 0;
+                       } else {
+                               buffer[buffer_idx++] = c;
+                               if (buffer_idx == (sizeof(buffer) - 1)) {
+                                       buffer[buffer_idx] = '\0';
+                                       buffer_idx = 0;
+                               }
+                       }
+                       break;
+               case START:
+                       if (linelen == 0 && c == '#') {
+                               state = COMMENT;
+                       } else if (isspace(c)) {
+                               /* Ignore empty space */
+                       } else {
+                               state = TOKEN;
+                               ungetc(c, fp);
+                       }
+                       break;
+               case SPACE:
+                       if (c == '=') {
+                               state = VALUE;
+                       } else if (!isspace(c)) {
+                               state = ERROR;
+                       }
+                       break;
+               case VALUE:
+                       if (c == '"') {
+                               if (quote == 1) {
+                                       buffer[buffer_idx] = '\0';
+                                       state = END;
+                               } else if (buffer_idx != 0) {
+                                       buffer[buffer_idx++] = c;
+                                       if (buffer_idx >= sizeof(buffer)) {
+                                               state = ERROR;
+                                       }
+                               } else {
+                                       quote = 1;
+                               }
+                       } else if (isspace(c)) {
+                               if (buffer_idx == 0) {
+                                       /* Ignore */
+                               } else if (quote == 1) {
+                                       buffer[buffer_idx++] = c;
+                                       if (buffer_idx >= sizeof(buffer)) {
+                                               state = ERROR;
+                                       }
+                               } else {
+                                       buffer[buffer_idx] = '\0';
+                                       ungetc(c, fp);
+                                       state = END;
+                               }
+                       } else {
+                               buffer[buffer_idx++] = c;
+                               if (buffer_idx >= sizeof(buffer)) {
+                                       state = ERROR;
+                               }
+                       }
+                       break;
+               case TOKEN:
+                       if (c == '=') {
+                               if (token_idx < 0) {
+                                       state = ERROR;
+                               } else {
+                                       state = VALUE;
+                               }
+                       } else if (isspace(c)) {
+                               if (token_idx < 0) {
+                                       break;
+                               }
+
+                               if (token_handler[token_idx].name[ch_idx] != '\0') {
+                                       state = ERROR;
+                               } else {
+                                       state = SPACE;
+                               }
+                       } else  {
+                               if (token_idx < 0) {
+                                       /* Now start to find a token! */
+                                       token_idx = 0;
+                               }
+
+                               if (token_handler[token_idx].name[ch_idx] == c) {
+                                       ch_idx++;
+                               } else {
+                                       ungetc(c, fp);
+                                       while (ch_idx-- > 0) {
+                                               ungetc(token_handler[token_idx].name[ch_idx], fp);
+                                       }
+
+                                       token_idx++;
+
+                                       if (token_handler[token_idx].name == NULL) {
+                                               state = ERROR;
+                                       } else {
+                                               ch_idx = 0;
+                                       }
+                               }
+                       }
+                       break;
+               case ERROR:
+                       if (c == CR || c == LF || c == EOF) {
+                               state = START;
+                               token_idx = -1;
+                               buffer_idx = 0;
+                               ch_idx = 0;
+                               linelen = -1;
+                               quote = 0;
+                       }
+                       break;
+               case END:
+                       if (c == LF || c == CR || c == EOF) {
+                               state = START;
+
+                               /*!
+                                * \NOTE
+                                * Make the string terminator
+                                */
+                               buffer[buffer_idx] = '\0';
+
+                               if (token_idx >= 0 && token_handler[token_idx].handler) {
+                                       token_handler[token_idx].handler(item, buffer);
+                               }
+
+                               token_idx = -1;
+                               ch_idx = 0;
+                               buffer_idx = 0;
+                               linelen = -1;
+                               quote = 0;
+                               /* Finish */
+                       } else if (isspace(c)) {
+                               /* ignore */
+                       } else {
+                               state = ERROR;
+                       }
+                       break;
+               default:
+                       /* ?? */
+                       break;
+               }
+
+               linelen++;
+       } while (c != EOF);
+
+       if (fclose(fp) != 0) {
+               ErrPrint("fclose: %d\n", errno);
+       }
+
+       s_list = eina_list_append(s_list, item);
+       return item;
+}
+
+HAPI int parser_unload(struct parser *item)
+{
+       s_list = eina_list_remove(s_list, item);
+
+       DbgFree(item->auto_launch);
+       DbgFree(item->abi);
+       DbgFree(item->script);
+       DbgFree(item->group);
+       DbgFree(item->gbar_group);
+       DbgFree(item->gbar_path);
+       DbgFree(item->widget_group);
+       DbgFree(item->widget_path);
+       DbgFree(item->filename);
+       DbgFree(item);
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/pkgmgr.c b/src/pkgmgr.c
new file mode 100644 (file)
index 0000000..5d2fb17
--- /dev/null
@@ -0,0 +1,626 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <dlog.h>
+#include <package-manager.h>
+#include <widget_errno.h>
+
+#include <Ecore.h>
+#include "util.h"
+#include "debug.h"
+#include "pkgmgr.h"
+#include "conf.h"
+
+struct item {
+       char *pkgname;
+       char *icon;
+
+       enum pkgmgr_event_type type;
+       enum pkgmgr_status status;
+};
+
+static struct {
+       pkgmgr_client *listen_pc;
+       Eina_List *item_list;
+
+       Eina_List *install_event;
+       Eina_List *uninstall_event;
+       Eina_List *update_event;
+       Eina_List *download_event;
+       Eina_List *recover_event;
+} s_info = {
+       .listen_pc = NULL,
+       .item_list = NULL,
+
+       .install_event = NULL,
+       .uninstall_event = NULL,
+       .update_event = NULL,
+       .download_event = NULL,
+       .recover_event = NULL,
+};
+
+struct event_item {
+       int (*cb)(const char *pkgname, enum pkgmgr_status status, double value, void *data);
+       void *data;
+};
+
+static inline void invoke_install_event_handler(const char *pkgname, enum pkgmgr_status status, double value)
+{
+       Eina_List *l;
+       struct event_item *item;
+
+       EINA_LIST_FOREACH(s_info.install_event, l, item) {
+               if (item->cb) {
+                       item->cb(pkgname, status, value, item->data);
+               }
+       }
+}
+
+static inline void invoke_uninstall_event_handler(const char *pkgname, enum pkgmgr_status status, double value)
+{
+       Eina_List *l;
+       struct event_item *item;
+
+       EINA_LIST_FOREACH(s_info.uninstall_event, l, item) {
+               if (item->cb) {
+                       item->cb(pkgname, status, value, item->data);
+               }
+       }
+}
+
+static inline void invoke_update_event_handler(const char *pkgname, enum pkgmgr_status status, double value)
+{
+       Eina_List *l;
+       struct event_item *item;
+
+       EINA_LIST_FOREACH(s_info.update_event, l, item) {
+               if (item->cb) {
+                       item->cb(pkgname, status, value, item->data);
+               }
+       }
+}
+
+static inline void invoke_download_event_handler(const char *pkgname, enum pkgmgr_status status, double value)
+{
+       Eina_List *l;
+       struct event_item *item;
+
+       EINA_LIST_FOREACH(s_info.download_event, l, item) {
+               if (item->cb) {
+                       item->cb(pkgname, status, value, item->data);
+               }
+       }
+}
+
+static inline void invoke_recover_event_handler(const char *pkgname, enum pkgmgr_status status, double value)
+{
+       Eina_List *l;
+       struct event_item *item;
+
+       EINA_LIST_FOREACH(s_info.recover_event, l, item) {
+               if (item->cb) {
+                       item->cb(pkgname, status, value, item->data);
+               }
+       }
+}
+
+static inline void invoke_callback(const char *pkgname, struct item *item, double value)
+{
+       switch (item->type) {
+       case PKGMGR_EVENT_DOWNLOAD:
+               invoke_download_event_handler(pkgname, item->status, value);
+               break;
+       case PKGMGR_EVENT_UNINSTALL:
+               invoke_uninstall_event_handler(pkgname, item->status, value);
+               break;
+       case PKGMGR_EVENT_INSTALL:
+               invoke_install_event_handler(pkgname, item->status, value);
+               break;
+       case PKGMGR_EVENT_UPDATE:
+               invoke_update_event_handler(pkgname, item->status, value);
+               break;
+       case PKGMGR_EVENT_RECOVER:
+               invoke_recover_event_handler(pkgname, item->status, value);
+               break;
+       default:
+               ErrPrint("Unknown type: %d\n", item->type);
+               break;
+       }
+}
+
+static inline int is_valid_status(struct item *item, const char *status)
+{
+       const char *expected_status;
+
+       switch (item->type) {
+       case PKGMGR_EVENT_DOWNLOAD:
+               expected_status = "download";
+               break;
+       case PKGMGR_EVENT_UNINSTALL:
+               expected_status = "uninstall";
+               break;
+       case PKGMGR_EVENT_INSTALL:
+               expected_status = "install";
+               break;
+       case PKGMGR_EVENT_UPDATE:
+               expected_status = "update";
+               break;
+       case PKGMGR_EVENT_RECOVER:
+               expected_status = "recover";
+               break;
+       default:
+               return 0;
+       }
+
+       return !strcasecmp(status, expected_status);
+}
+
+static struct item *find_item(const char *pkgname)
+{
+       Eina_List *l;
+       struct item *item;
+
+       if (!pkgname) {
+               ErrPrint("Package name is not valid\n");
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(s_info.item_list, l, item) {
+               if (strcmp(item->pkgname, pkgname)) {
+                       continue;
+               }
+
+               return item;
+       }
+
+       DbgPrint("Package %s is not found\n", pkgname);
+       return NULL;
+}
+
+static int start_cb(const char *pkgname, const char *val, void *data)
+{
+       struct item *item;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->pkgname = strdup(pkgname);
+       if (!item->pkgname) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(item);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->status = PKGMGR_STATUS_START;
+
+       if (!strcasecmp(val, "download")) {
+               item->type = PKGMGR_EVENT_DOWNLOAD;
+       } else if (!strcasecmp(val, "uninstall")) {
+               item->type = PKGMGR_EVENT_UNINSTALL;
+       } else if (!strcasecmp(val, "install")) {
+               item->type = PKGMGR_EVENT_INSTALL;
+       } else if (!strcasecmp(val, "update")) {
+               item->type = PKGMGR_EVENT_UPDATE;
+       } else if (!strcasecmp(val, "recover")) {
+               item->type = PKGMGR_EVENT_RECOVER;
+       } else {
+               DbgFree(item->pkgname);
+               DbgFree(item);
+               ErrPrint("Invalid val: %s\n", val);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       s_info.item_list = eina_list_append(s_info.item_list, item);
+
+       invoke_callback(pkgname, item, 0.0f);
+       return WIDGET_ERROR_NONE;
+}
+
+static int icon_path_cb(const char *pkgname, const char *val, void *data)
+{
+       struct item *item;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       if (item->icon) {
+               DbgFree(item->icon);
+       }
+
+       item->icon = strdup(val);
+       if (!item->icon) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+static int command_cb(const char *pkgname, const char *val, void *data)
+{
+       struct item *item;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       if (!is_valid_status(item, val)) {
+               DbgPrint("Invalid status: %d, %s\n", item->type, val);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       item->status = PKGMGR_STATUS_COMMAND;
+       invoke_callback(pkgname, item, 0.0f);
+       return WIDGET_ERROR_NONE;
+}
+
+static int error_cb(const char *pkgname, const char *val, void *data)
+{
+       /* val = error */
+       struct item *item;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       item->status = PKGMGR_STATUS_ERROR;
+       invoke_callback(pkgname, item, 0.0f);
+       return WIDGET_ERROR_NONE;
+}
+
+static int change_pkgname_cb(const char *pkgname, const char *val, void *data)
+{
+       struct item *item;
+       char *new_pkgname;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       new_pkgname = strdup(val);
+       if (!new_pkgname) {
+               ErrPrint("strdup: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       DbgFree(item->pkgname);
+       item->pkgname = new_pkgname;
+       return WIDGET_ERROR_NONE;
+}
+
+static int download_cb(const char *pkgname, const char *val, void *data)
+{
+       /* val = integer */
+       struct item *item;
+       double value;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               DbgPrint("ITEM is not started from the start_cb\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (item->type != PKGMGR_EVENT_DOWNLOAD) {
+               DbgPrint("TYPE is not \"download\" : %d\n", item->type);
+               item->type = PKGMGR_EVENT_DOWNLOAD;
+       }
+
+       switch (item->status) {
+       case PKGMGR_STATUS_START:
+       case PKGMGR_STATUS_COMMAND:
+               item->status = PKGMGR_STATUS_PROCESSING;
+       case PKGMGR_STATUS_PROCESSING:
+               break;
+       default:
+               ErrPrint("Invalid state [%s, %s]\n", pkgname, val);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (val) {
+               if (sscanf(val, "%lf", &value) != 1) {
+                       value = (double)WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       } else {
+               value = (double)WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       invoke_download_event_handler(pkgname, item->status, value);
+       return WIDGET_ERROR_NONE;
+}
+
+static int progress_cb(const char *pkgname, const char *val, void *data)
+{
+       /* val = integer */
+       struct item *item;
+       double value;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               ErrPrint("ITEM is not started from the start_cb\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (item->status) {
+       case PKGMGR_STATUS_START:
+       case PKGMGR_STATUS_COMMAND:
+               item->status = PKGMGR_STATUS_PROCESSING;
+       case PKGMGR_STATUS_PROCESSING:
+               break;
+       default:
+               ErrPrint("Invalid state [%s, %s]\n", pkgname, val);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (val) {
+               if (sscanf(val, "%lf", &value) != 1) {
+                       value = (double)WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       } else {
+               value = (double)WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       invoke_callback(pkgname, item, value);
+       return WIDGET_ERROR_NONE;
+}
+
+static int end_cb(const char *pkgname, const char *val, void *data)
+{
+       struct item *item;
+
+       DbgPrint("[%s] %s\n", pkgname, val);
+
+       item = find_item(pkgname);
+       if (!item) {
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       item->status = !strcasecmp(val, "ok") ? PKGMGR_STATUS_END : PKGMGR_STATUS_ERROR;
+
+       invoke_callback(pkgname, item, 0.0f);
+
+       s_info.item_list = eina_list_remove(s_info.item_list, item);
+       DbgFree(item->icon);
+       DbgFree(item->pkgname);
+       DbgFree(item);
+       return WIDGET_ERROR_NONE;
+}
+
+static struct pkgmgr_handler {
+       const char *key;
+       int (*func)(const char *package, const char *val, void *data);
+} handler[] = {
+       { "install_percent", progress_cb },
+       { "download_percent", download_cb },
+       { "start", start_cb },
+       { "end", end_cb },
+       { "change_pkg_name", change_pkgname_cb },
+       { "icon_path", icon_path_cb },
+       { "command", command_cb },
+       { "error", error_cb },
+       { NULL, NULL },
+};
+
+static int pkgmgr_cb(int req_id, const char *type, const char *pkgname, const char *key, const char *val, const void *pmsg, void *data)
+{
+       register int i;
+       int ret;
+
+       for (i = 0; handler[i].key; i++) {
+               if (strcasecmp(key, handler[i].key)) {
+                       continue;
+               }
+
+               ret = handler[i].func(pkgname, val, data);
+               if (ret < 0) {
+                       DbgPrint("REQ[%d] pkgname[%s], type[%s], key[%s], val[%s], ret = %d\n",
+                                       req_id, pkgname, type, key, val, ret);
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int pkgmgr_init(void)
+{
+       if (s_info.listen_pc) {
+               return WIDGET_ERROR_ALREADY_EXIST;
+       }
+
+       s_info.listen_pc = pkgmgr_client_new(PC_LISTENING);
+       if (!s_info.listen_pc) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (pkgmgr_client_listen_status(s_info.listen_pc, pkgmgr_cb, NULL) != PKGMGR_R_OK) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int pkgmgr_fini(void)
+{
+       struct event_item *item;
+       struct item *ctx;
+
+       if (!s_info.listen_pc) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (pkgmgr_client_free(s_info.listen_pc) != PKGMGR_R_OK) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       s_info.listen_pc = NULL;
+
+       EINA_LIST_FREE(s_info.download_event, item) {
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(s_info.uninstall_event, item) {
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(s_info.install_event, item) {
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(s_info.update_event, item) {
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(s_info.recover_event, item) {
+               DbgFree(item);
+       }
+
+       EINA_LIST_FREE(s_info.item_list, ctx) {
+               DbgFree(ctx->pkgname);
+               DbgFree(ctx->icon);
+               DbgFree(ctx);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int pkgmgr_add_event_callback(enum pkgmgr_event_type type, int (*cb)(const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data)
+{
+       struct event_item *item;
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->cb = cb;
+       item->data = data;
+
+       switch (type) {
+       case PKGMGR_EVENT_DOWNLOAD:
+               s_info.download_event = eina_list_prepend(s_info.download_event, item);
+               break;
+       case PKGMGR_EVENT_UNINSTALL:
+               s_info.uninstall_event = eina_list_prepend(s_info.uninstall_event, item);
+               break;
+       case PKGMGR_EVENT_INSTALL:
+               s_info.install_event = eina_list_prepend(s_info.install_event, item);
+               break;
+       case PKGMGR_EVENT_UPDATE:
+               s_info.update_event = eina_list_prepend(s_info.update_event, item);
+               break;
+       case PKGMGR_EVENT_RECOVER:
+               s_info.recover_event = eina_list_prepend(s_info.recover_event, item);
+               break;
+       default:
+               DbgFree(item);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *pkgmgr_del_event_callback(enum pkgmgr_event_type type, int (*cb)(const char *pkgname, enum pkgmgr_status status, double value, void *data), void *data)
+{
+       struct event_item *item;
+       Eina_List *l;
+       void *cbdata = NULL;
+
+       switch (type) {
+       case PKGMGR_EVENT_DOWNLOAD:
+               EINA_LIST_FOREACH(s_info.download_event, l, item) {
+                       if (item->cb == cb && item->data == data) {
+                               s_info.download_event = eina_list_remove(s_info.download_event, item);
+                               cbdata = item->data;
+                               DbgFree(item);
+                               break;
+                       }
+               }
+               break;
+       case PKGMGR_EVENT_UNINSTALL:
+               EINA_LIST_FOREACH(s_info.uninstall_event, l, item) {
+                       if (item->cb == cb && item->data == data) {
+                               s_info.uninstall_event = eina_list_remove(s_info.uninstall_event, item);
+                               cbdata = item->data;
+                               DbgFree(item);
+                               break;
+                       }
+               }
+               break;
+       case PKGMGR_EVENT_INSTALL:
+               EINA_LIST_FOREACH(s_info.install_event, l, item) {
+                       if (item->cb == cb && item->data == data) {
+                               s_info.install_event = eina_list_remove(s_info.install_event, item);
+                               cbdata = item->data;
+                               DbgFree(item);
+                               break;
+                       }
+               }
+               break;
+       case PKGMGR_EVENT_UPDATE:
+               EINA_LIST_FOREACH(s_info.update_event, l, item) {
+                       if (item->cb == cb && item->data == data) {
+                               s_info.update_event = eina_list_remove(s_info.update_event, item);
+                               cbdata = item->data;
+                               DbgFree(item);
+                               break;
+                       }
+               }
+               break;
+       case PKGMGR_EVENT_RECOVER:
+               EINA_LIST_FOREACH(s_info.recover_event, l, item) {
+                       if (item->cb == cb && item->data == data) {
+                               s_info.recover_event = eina_list_remove(s_info.recover_event, item);
+                               cbdata = item->data;
+                               DbgFree(item);
+                               break;
+                       }
+               }
+               break;
+       default:
+               ErrPrint("Invalid type\n");
+               break;
+       }
+
+       return cbdata;
+}
+
+/* End of a file */
diff --git a/src/script_handler.c b/src/script_handler.c
new file mode 100644 (file)
index 0000000..4aec836
--- /dev/null
@@ -0,0 +1,1513 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h> /* free */
+#include <ctype.h>
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <Ecore.h>
+#include <Eina.h>
+
+#include <dlog.h>
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "package.h"
+#include "instance.h"
+#include "buffer_handler.h"
+#include "script_handler.h"
+#include "debug.h"
+#include "conf.h"
+#include "util.h"
+
+#define INFO_SIZE "size"
+#define INFO_CATEGORY "category"
+
+static const char *type_list[] = {
+       "access",
+       "access,operation",
+       "color",
+       "drag",
+       "image",
+       "info",
+       "script",
+       "signal",
+       "text",
+       NULL
+};
+
+static const char *field_list[] = {
+       "type",
+       "part",
+       "data",
+       "option",
+       "id",
+       "target",
+       "file",
+       NULL
+};
+
+int errno;
+
+static struct info {
+       Eina_List *script_port_list;
+       enum widget_fb_type env_buf_type;
+} s_info = {
+       .script_port_list = NULL,
+       .env_buf_type = WIDGET_FB_TYPE_FILE,
+};
+
+struct script_port {
+       void *handle;
+
+       const char *(*magic_id)(void);
+       int (*update_color)(void *handle, const char *id, const char *part, const char *rgba);
+       int (*update_text)(void *handle, const char *id, const char *part, const char *text);
+       int (*update_image)(void *handle, const char *id, const char *part, const char *path, const char *option);
+       int (*update_access)(void *handle, const char *id, const char *part, const char *text, const char *option);
+       int (*operate_access)(void *handle, const char *id, const char *part, const char *operation, const char *option);
+       int (*update_script)(void *handle, const char *src_id, const char *target_id, const char *part, const char *path, const char *option);
+       int (*update_signal)(void *handle, const char *id, const char *part, const char *signal);
+       int (*update_drag)(void *handle, const char *id, const char *part, double x, double y);
+       int (*update_size)(void *handle, const char *id, int w, int h);
+       int (*update_category)(void *handle, const char *id, const char *category);
+       int (*feed_event)(void *handle, int event_type, int x, int y, int down, unsigned int keycode, double timestamp);
+
+       void *(*create)(void *buffer_info, const char *file, const char *option);
+       int (*destroy)(void *handle);
+
+       int (*load)(void *handle, int (*render_pre)(void *buffer_info, void *data), int (*render_post)(void *buffer_info, void *data), void *data);
+       int (*unload)(void *handle);
+
+       int (*init)(double scale, int premultiplied);
+       int (*fini)(void);
+};
+
+enum block_type {
+       TYPE_ACCESS,
+       TYPE_ACCESS_OP,
+       TYPE_COLOR,
+       TYPE_DRAG,
+       TYPE_IMAGE,
+       TYPE_INFO,
+       TYPE_SCRIPT,
+       TYPE_SIGNAL,
+       TYPE_TEXT,
+       TYPE_MAX
+};
+
+enum field_type {
+       FIELD_TYPE,
+       FIELD_PART,
+       FIELD_DATA,
+       FIELD_OPTION,
+       FIELD_ID,
+       FIELD_TARGET,
+       FIELD_FILE
+};
+
+struct block {
+       enum block_type type;
+       char *part;
+       char *data;
+       char *option;
+       char *id;
+       char *target;
+       char *file;
+
+       /* Should be released */
+       char *filebuf;
+       const char *filename;
+};
+
+struct script_info {
+       struct buffer_info *buffer_handle; 
+       int loaded;
+
+       int w;
+       int h;
+
+       int x;
+       int y;
+       int down;
+
+       unsigned int keycode;
+
+       struct script_port *port;
+       void *port_data;
+
+       Eina_List *cached_blocks;
+};
+
+static inline void consuming_parsed_block(struct inst_info *inst, int is_pd, struct block *block);
+
+static int load_all_ports(void)
+{
+       struct script_port *item;
+       struct dirent *ent;
+       DIR *dir;
+       char *path;
+       int pathlen;
+
+       dir = opendir(WIDGET_CONF_SCRIPT_PORT_PATH);
+       if (!dir) {
+               ErrPrint("opendir: %d\n", errno);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       while ((ent = readdir(dir))) {
+               if (ent->d_name[0] == '.') {
+                       continue;
+               }
+
+               pathlen = strlen(ent->d_name) + strlen(WIDGET_CONF_SCRIPT_PORT_PATH) + 1;
+               path = malloc(pathlen);
+               if (!path) {
+                       ErrPrint("malloc: %d %d\n", errno, pathlen);
+                       if (closedir(dir) < 0) {
+                               ErrPrint("closedir: %d\n", errno);
+                       }
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               snprintf(path, pathlen, "%s%s", WIDGET_CONF_SCRIPT_PORT_PATH, ent->d_name);
+
+               item = malloc(sizeof(*item));
+               if (!item) {
+                       ErrPrint("malloc: %d\n", errno);
+                       DbgFree(path);
+                       if (closedir(dir) < 0) {
+                               ErrPrint("closedir: %d\n", errno);
+                       }
+                       return WIDGET_ERROR_OUT_OF_MEMORY;
+               }
+
+               DbgPrint("Open SCRIPT PORT: %s\n", path);
+               item->handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW | RTLD_DEEPBIND);
+               DbgFree(path);
+               if (!item->handle) {
+                       ErrPrint("Error: %s\n", dlerror());
+                       DbgFree(item);
+                       if (closedir(dir) < 0) {
+                               ErrPrint("closedir: %d\n", errno);
+                       }
+                       return WIDGET_ERROR_FAULT;
+               }
+
+               item->magic_id = dlsym(item->handle, "script_magic_id");
+               if (!item->magic_id) {
+                       goto errout;
+               }
+
+               DbgPrint("SCRIPT PORT magic id: %s\n", item->magic_id());
+
+               item->update_color = dlsym(item->handle, "script_update_color");
+               if (!item->update_color) {
+                       goto errout;
+               }
+
+               item->update_text = dlsym(item->handle, "script_update_text");
+               if (!item->update_text) {
+                       goto errout;
+               }
+
+               item->update_image = dlsym(item->handle, "script_update_image");
+               if (!item->update_image) {
+                       goto errout;
+               }
+
+               item->update_access = dlsym(item->handle, "script_update_access");
+               if (!item->update_access) {
+                       goto errout;
+               }
+
+               item->operate_access = dlsym(item->handle, "script_operate_access");
+               if (!item->operate_access) {
+                       goto errout;
+               }
+
+               item->update_script = dlsym(item->handle, "script_update_script");
+               if (!item->update_script) {
+                       goto errout;
+               }
+
+               item->update_signal = dlsym(item->handle, "script_update_signal");
+               if (!item->update_signal) {
+                       goto errout;
+               }
+
+               item->update_drag = dlsym(item->handle, "script_update_drag");
+               if (!item->update_drag) {
+                       goto errout;
+               }
+
+               item->update_size = dlsym(item->handle, "script_update_size");
+               if (!item->update_size) {
+                       goto errout;
+               }
+
+               item->update_category = dlsym(item->handle, "script_update_category");
+               if (!item->update_category) {
+                       goto errout;
+               }
+
+               item->create = dlsym(item->handle, "script_create");
+               if (!item->create) {
+                       goto errout;
+               }
+
+               item->destroy = dlsym(item->handle, "script_destroy");
+               if (!item->destroy) {
+                       goto errout;
+               }
+
+               item->load = dlsym(item->handle, "script_load");
+               if (!item->load) {
+                       goto errout;
+               }
+
+               item->unload = dlsym(item->handle, "script_unload");
+               if (!item->unload) {
+                       goto errout;
+               }
+
+               item->init = dlsym(item->handle, "script_init");
+               if (!item->init) {
+                       goto errout;
+               }
+
+               item->fini = dlsym(item->handle, "script_fini");
+               if (!item->fini) {
+                       goto errout;
+               }
+
+               item->feed_event = dlsym(item->handle, "script_feed_event");
+               if (!item->feed_event) {
+                       goto errout;
+               }
+
+               if (item->init(WIDGET_CONF_SCALE_WIDTH_FACTOR, WIDGET_CONF_PREMULTIPLIED_COLOR) < 0) {
+                       ErrPrint("Failed to initialize script engine\n");
+                       goto errout;
+               }
+
+               s_info.script_port_list = eina_list_append(s_info.script_port_list, item);
+       }
+
+       if (closedir(dir) < 0) {
+               ErrPrint("closedir: %d\n", errno);
+       }
+
+       return WIDGET_ERROR_NONE;
+
+errout:
+       ErrPrint("Error: %s\n", dlerror());
+       if (dlclose(item->handle) != 0) {
+               ErrPrint("dlclose: %d\n", errno);
+       }
+       DbgFree(item);
+       if (closedir(dir) < 0) {
+               ErrPrint("closedir: %d\n", errno);
+       }
+       return WIDGET_ERROR_FAULT;
+}
+
+static inline struct script_port *find_port(const char *magic_id)
+{
+       Eina_List *l;
+       struct script_port *item;
+
+       if (!s_info.script_port_list) {
+               int ret;
+
+               ret = load_all_ports();
+               if (ret < 0) {
+                       ErrPrint("load_all_ports: %d\n", ret);
+               }
+       }
+
+       EINA_LIST_FOREACH(s_info.script_port_list, l, item) {
+               if (!strcmp(item->magic_id(), magic_id)) {
+                       return item;
+               }
+       }
+
+       return NULL;
+}
+
+static inline void delete_block(struct block *block)
+{
+       DbgFree(block->filebuf);
+       DbgFree(block);
+}
+
+static int render_post_cb(void *_buffer_handle, void *data)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct inst_info *inst;
+       struct buffer_info *buffer_handle = _buffer_handle;
+       struct script_info *info;
+
+       inst = buffer_handler_instance(buffer_handle);
+       if (!inst) {
+               goto out;
+       }
+
+       if (instance_state(inst) != INST_ACTIVATED) {
+               ErrPrint("Render post invoked but instance is not activated\n");
+               PERF_MARK(__func__);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = instance_widget_script(inst);
+       if (info && info == data) {
+               buffer_handler_flush(buffer_handle);
+               instance_widget_updated_by_instance(inst, NULL, info->x, info->y, info->w, info->h);
+               PERF_MARK("lb,update");
+               return WIDGET_ERROR_NONE;
+       }
+
+       info = instance_gbar_script(inst);
+       if (info && info == data) {
+               buffer_handler_flush(buffer_handle);
+               instance_gbar_updated_by_instance(inst, NULL, info->x, info->y, info->w, info->h);
+               PERF_MARK("pd,update");
+               return WIDGET_ERROR_NONE;
+       }
+
+out:
+       ErrPrint("Failed to sync\n");
+       PERF_MARK(__func__);
+       return WIDGET_ERROR_FAULT;
+}
+
+/*!
+ * \NOTE
+ * Exported API
+ */
+EAPI int script_signal_emit(void *buffer_handle, const char *part, const char *signal, double sx, double sy, double ex, double ey)
+{
+       struct script_info *info;
+       struct inst_info *inst;
+       int w;
+       int h;
+       double fx;
+       double fy;
+
+       if (!buffer_handle) {
+               ErrPrint("Invalid handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = buffer_handler_data(buffer_handle);
+       if (!info) {
+               ErrPrint("Invalid handle\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       inst = buffer_handler_instance(buffer_handle);
+       if (!inst) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!signal || strlen(signal) == 0) {
+               signal = "";
+       }
+
+       if (!part || strlen(part) == 0) {
+               part = "";
+       }
+
+       buffer_handler_get_size(buffer_handle, &w, &h);
+
+       fx = (double)info->x / (double)w;
+       fy = (double)info->y / (double)h;
+
+       return instance_signal_emit(inst, signal, part, sx, sy, ex, ey, fx, fy, info->down);
+}
+
+static inline void flushing_cached_block(struct script_info *info)
+{
+       struct block *block;
+       struct inst_info *inst;
+       int is_pd;
+
+       inst = buffer_handler_instance(info->buffer_handle);
+       if (!inst) {
+               ErrPrint("Instance is not valid\n");
+               EINA_LIST_FREE(info->cached_blocks, block) {
+                       delete_block(block);
+               }
+               return;
+       }
+
+       is_pd = instance_gbar_script(inst) == info;
+
+       EINA_LIST_FREE(info->cached_blocks, block) {
+               consuming_parsed_block(inst, is_pd, block);
+       }
+}
+
+HAPI int script_handler_load(struct script_info *info, int is_pd)
+{
+       struct inst_info *inst;
+
+       if (!info || !info->port) {
+               ErrPrint("Script handler is not created\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->loaded > 0) {
+               info->loaded++;
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (info->port->load(info->port_data, NULL, render_post_cb, info) < 0) {
+               ErrPrint("Unable to load the script\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       info->loaded = 1;
+       flushing_cached_block(info);
+
+       inst = buffer_handler_instance(info->buffer_handle);
+       if (inst) {
+               script_signal_emit(info->buffer_handle, instance_id(inst),
+                               is_pd ? "gbar,show" : "widget,show", 0.0f, 0.0f, 0.0f, 0.0f);
+       }
+       buffer_handler_flush(info->buffer_handle);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int script_handler_unload(struct script_info *info, int is_pd)
+{
+       struct inst_info *inst;
+
+       if (!info || !info->port) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info->loaded--;
+       if (info->loaded > 0) {
+               return WIDGET_ERROR_RESOURCE_BUSY;
+       }
+
+       if (info->loaded < 0) {
+               info->loaded = 0;
+               return WIDGET_ERROR_ALREADY_EXIST;
+       }
+
+       inst = buffer_handler_instance(info->buffer_handle);
+       if (inst) {
+               script_signal_emit(info->buffer_handle, instance_id(inst),
+                               is_pd ? "gbar,hide" : "widget,hide", 0.0f, 0.0f, 0.0f, 0.0f);
+       }
+
+       if (info->port->unload(info->port_data) < 0) {
+               ErrPrint("Failed to unload script object. but go ahead\n");
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI struct script_info *script_handler_create(struct inst_info *inst, const char *file, const char *option, int w, int h)
+{
+       struct script_info *info;
+
+       DbgPrint("Create script: %s (%s) %dx%d\n", file, option, w, h);
+
+       if (!file) {
+               return NULL;
+       }
+
+       info = calloc(1, sizeof(*info));
+       if (!info) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       info->buffer_handle = buffer_handler_create(inst, s_info.env_buf_type, w, h, WIDGET_CONF_DEFAULT_PIXELS);
+       if (!info->buffer_handle) {
+               /* buffer_handler_create will prints some log */
+               DbgFree(info);
+               return NULL;
+       }
+
+       (void)buffer_handler_set_data(info->buffer_handle, info);
+
+       info->port = find_port(package_script(instance_package(inst)));
+       if (!info->port) {
+               ErrPrint("Failed to find a proper port for [%s]%s\n",
+                               instance_package(inst), package_script(instance_package(inst)));
+               buffer_handler_destroy(info->buffer_handle);
+               DbgFree(info);
+               return NULL;
+       }
+
+       info->port_data = info->port->create(info->buffer_handle, file, option);
+       if (!info->port_data) {
+               ErrPrint("Failed to create a port (%s - %s)\n", file, option);
+               buffer_handler_destroy(info->buffer_handle);
+               DbgFree(info);
+               return NULL;
+       }
+
+       return info;
+}
+
+HAPI int script_handler_destroy(struct script_info *info)
+{
+       struct block *block;
+       int ret;
+
+       if (!info || !info->port) {
+               ErrPrint("port is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (info->loaded != 0) {
+               ErrPrint("Script handler is not unloaded\n");
+               return WIDGET_ERROR_RESOURCE_BUSY;
+       }
+
+       ret = info->port->destroy(info->port_data);
+       if (ret < 0) {
+               ErrPrint("Failed to destroy port, but go ahead: %d\n", ret);
+       }
+
+       (void)buffer_handler_destroy(info->buffer_handle);
+
+       EINA_LIST_FREE(info->cached_blocks, block) {
+               delete_block(block);
+       }
+
+       DbgFree(info);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int script_handler_is_loaded(struct script_info *info)
+{
+       return info ? info->loaded > 0 : 0;
+}
+
+HAPI struct buffer_info *script_handler_buffer_info(struct script_info *info)
+{
+       if (!info) {
+               return NULL;
+       }
+
+       return info->buffer_handle;
+}
+
+static int update_script_color(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part || !block->data) {
+               ErrPrint("Block or part or data is not valid\n");
+               PERF_MARK("color");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("color");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("color");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->update_color(info->port_data, block->id, block->part, block->data);
+       PERF_MARK("color");
+       return ret;
+}
+
+static int update_script_text(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part || !block->data) {
+               ErrPrint("Block or part or data is not valid\n");
+               PERF_MARK("text");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("text");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("text");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("[%s] %s (%s)\n", block->id, block->part, block->data);
+       ret = info->port->update_text(info->port_data, block->id, block->part, block->data);
+
+       PERF_MARK("text");
+       return ret;
+}
+
+static int update_script_image(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part) {
+               ErrPrint("Block or part is not valid\n");
+               PERF_MARK("image");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("image");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("image");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("[%s] %s (%s)\n", block->id, block->part, block->data);
+       ret = info->port->update_image(info->port_data, block->id, block->part, block->data, block->option);
+       PERF_MARK("image");
+       return ret;
+}
+
+static int update_access(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part) {
+               ErrPrint("Block or block->part is NIL\n");
+               PERF_MARK("access");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("access");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("access");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->update_access(info->port_data, block->id, block->part, block->data, block->option);
+       PERF_MARK("access");
+       return ret;
+}
+
+static int operate_access(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part) {
+               ErrPrint("Block or block->part is NIL\n");
+               PERF_MARK("operate_access");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("operate_access");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("operate_access");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->operate_access(info->port_data, block->id, block->part, block->data, block->option);
+       PERF_MARK("operate_access");
+       return ret;
+}
+
+static int update_script_script(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block || !block->part) {
+               ErrPrint("Block or part is NIL\n");
+               PERF_MARK("script");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("script");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("script");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->update_script(info->port_data, block->id, block->target, block->part, block->data, block->option);
+       PERF_MARK("script");
+       return ret;
+}
+
+static int update_script_signal(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       int ret;
+
+       if (!block) {
+               ErrPrint("block is NIL\n");
+               PERF_MARK("signal");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("signal");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("signal");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->update_signal(info->port_data, block->id, block->part, block->data);
+       PERF_MARK("signal");
+       return ret;
+}
+
+static int update_script_drag(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+       double dx, dy;
+       int ret;
+
+       if (!block || !block->data || !block->part) {
+               ErrPrint("block or block->data or block->part is NIL\n");
+               PERF_MARK("drag");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("drag");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (sscanf(block->data, "%lfx%lf", &dx, &dy) != 2) {
+               ErrPrint("Invalid format of data (DRAG data [%s])\n", block->data);
+               PERF_MARK("drag");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("drag");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->update_drag(info->port_data, block->id, block->part, dx, dy);
+       PERF_MARK("drag");
+       return ret;
+}
+
+static void update_size_for_script(struct script_info *info, struct inst_info *inst, int w, int h)
+{
+       /*!
+        * \note
+        * After update the buffer size,
+        * If it required to be unload and load.
+        * New size of buffer will be allocated
+        */
+       buffer_handler_update_size(info->buffer_handle, w, h);
+
+       if (info->port->update_size) {
+               (void)info->port->update_size(info->port_data, NULL, w, h);
+       }
+
+       if (instance_widget_script(inst) == info) {
+               instance_set_widget_size(inst, w, h);
+       } else if (instance_gbar_script(inst) == info) {
+               instance_set_gbar_size(inst, w, h);
+       } else {
+               ErrPrint("Unknown script\n");
+       }
+}
+
+HAPI int script_handler_resize(struct script_info *info, int w, int h)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct inst_info *inst;
+
+       if (!info) {
+               ErrPrint("info[%p] resize is ignored\n", info);
+               PERF_MARK("resize");
+               return WIDGET_ERROR_NONE;
+       }
+
+       inst = buffer_handler_instance(info->buffer_handle);
+       if (!inst) {
+               ErrPrint("Instance is not valid\n");
+               PERF_MARK("resize");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       update_size_for_script(info, inst, w, h);
+
+       PERF_MARK("resize");
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI const char *script_handler_buffer_id(struct script_info *info)
+{
+       if (!info || !info->buffer_handle) {
+               ErrPrint("Invalid info\n");
+               return "";
+       }
+
+       return buffer_handler_id(info->buffer_handle);
+}
+
+static int update_info(struct inst_info *inst, struct block *block, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       struct script_info *info;
+
+       if (!block || !block->part || !block->data) {
+               ErrPrint("block or block->part or block->data is NIL\n");
+               PERF_MARK("info");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               PERF_MARK("info");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               PERF_MARK("info");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!strcasecmp(block->part, INFO_SIZE)) {
+               int w, h;
+
+               if (sscanf(block->data, "%dx%d", &w, &h) != 2) {
+                       ErrPrint("Invalid format for SIZE(%s)\n", block->data);
+                       PERF_MARK("info");
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+
+               if (!block->id) {
+                       update_size_for_script(info, inst, w, h);
+               } else {
+                       (void)info->port->update_size(info->port_data, block->id, w, h);
+               }
+       } else if (!strcasecmp(block->part, INFO_CATEGORY)) {
+               (void)info->port->update_category(info->port_data, block->id, block->data);
+       }
+       PERF_MARK("info");
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline void consuming_parsed_block(struct inst_info *inst, int is_pd, struct block *block)
+{
+       struct script_info *info;
+       typedef int (*update_function_t)(struct inst_info *inst, struct block *block, int is_pd);
+       update_function_t updators[] = {
+               update_access,
+               operate_access,
+               update_script_color,
+               update_script_drag,
+               update_script_image,
+               update_info,
+               update_script_script,
+               update_script_signal,
+               update_script_text,
+               NULL
+       };
+
+       info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+       if (!info) {
+               ErrPrint("info is NIL (%d, %s)\n", is_pd, instance_id(inst));
+               goto free_out;
+       }
+
+       if (script_handler_is_loaded(info)) {
+               if (block->type >= 0 || block->type < TYPE_MAX) {
+                       (void)updators[block->type](inst, block, is_pd);
+               } else {
+                       ErrPrint("Block type[%d] is not valid\n", block->type);
+               }
+       } else {
+               info->cached_blocks = eina_list_append(info->cached_blocks, block);
+               DbgPrint("Block is cached (%p), %d, %s\n", block, eina_list_count(info->cached_blocks), instance_id(inst));
+               return;
+       }
+
+free_out:
+       delete_block(block);
+}
+
+HAPI int script_init(void)
+{
+       if (!strcasecmp(WIDGET_CONF_PROVIDER_METHOD, "shm")) {
+               s_info.env_buf_type = WIDGET_FB_TYPE_SHM;
+       } else if (!strcasecmp(WIDGET_CONF_PROVIDER_METHOD, "pixmap")) {
+               s_info.env_buf_type = WIDGET_FB_TYPE_PIXMAP;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int script_fini(void)
+{
+       struct script_port *item;
+       /*!
+        * \TODO: Release all handles
+        */
+       EINA_LIST_FREE(s_info.script_port_list, item) {
+               item->fini();
+               if (dlclose(item->handle) != 0) {
+                       ErrPrint("dlclose: %d\n", errno);
+               }
+               DbgFree(item);
+       }
+
+       return 0;
+}
+
+HAPI int script_handler_update_pointer(struct script_info *info, int x, int y, int down)
+{
+       if (!info) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       info->x = x;
+       info->y = y;
+
+       if (down == 0) {
+               info->down = 0;
+       } else if (down == 1) {
+               info->down = 1;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int script_handler_update_keycode(struct script_info *info, unsigned int keycode)
+{
+       if (!info) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       info->keycode = keycode;
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int script_handler_feed_event(struct script_info *info, int event, double timestamp)
+{
+       int ret;
+
+       if (!info->port) {
+               ErrPrint("info->port is NIL\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = info->port->feed_event(info->port_data, event, info->x, info->y, info->down, info->keycode, timestamp);
+       return ret;
+}
+
+static inline char *load_file(const char *filename)
+{
+       char *filebuf = NULL;
+       int fd;
+       off_t filesize;
+       int ret;
+       size_t readsize = 0;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0) {
+               ErrPrint("open: %d\n", errno);
+               return NULL;
+       }
+
+       filesize = lseek(fd, 0L, SEEK_END);
+       if (filesize == (off_t)-1) {
+               ErrPrint("lseek: %d\n", errno);
+               goto errout;
+       }
+
+       if (lseek(fd, 0L, SEEK_SET) < 0) {
+               ErrPrint("lseek: %d\n", errno);
+               goto errout;
+       }
+
+       filebuf = malloc(filesize + 1);
+       if (!filebuf) {
+               ErrPrint("malloc: %d\n", errno);
+               goto errout;
+       }
+
+       while (readsize < filesize) {
+               ret = read(fd, filebuf + readsize, (size_t)filesize - readsize);
+               if (ret < 0) {
+                       if (errno == EINTR) {
+                               DbgPrint("Read is interrupted\n");
+                               continue;
+                       }
+
+                       ErrPrint("read: %d\n", errno);
+                       free(filebuf);
+                       filebuf = NULL;
+                       break;
+               }
+
+               readsize += ret;
+       }
+
+       if (filebuf) {
+               filebuf[readsize] = '\0';
+       }
+
+       /*!
+        * \note
+        * Now, we are ready to parse the filebuf.
+        */
+
+errout:
+       if (close(fd) < 0) {
+               ErrPrint("close: %d\n", errno);
+       }
+
+       return filebuf;
+}
+
+#if defined(_APPLY_SCRIPT_ASYNC_UPDATE)
+struct apply_data {
+       struct inst_info *inst;
+       Eina_List *block_list;
+       int is_pd;
+};
+
+static Eina_Bool apply_changes_cb(void *_data)
+{
+       struct apply_data *data = _data;
+       struct block *block;
+
+       block = eina_list_nth(data->block_list, 0);
+       data->block_list = eina_list_remove(data->block_list, block);
+       consuming_parsed_block(data->inst, data->is_pd, block);
+
+       if (!data->block_list) {
+               free(data);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+#endif
+
+HAPI int script_handler_parse_desc(struct inst_info *inst, const char *filename, int is_pd)
+{
+       PERF_INIT();
+       PERF_BEGIN();
+       int type_idx = 0;
+       int type_len = 0;
+       int field_idx = 0;
+       int field_len = 0;
+       char *filebuf;
+       char *fileptr;
+       char *ptr = NULL;
+       struct block *block = NULL;
+       Eina_List *block_list = NULL;
+       enum state {
+               BEGIN,
+               FIELD,
+               DATA,
+               END,
+               DONE,
+               ERROR,
+       } state;
+
+       filebuf = load_file(filename);
+       if (!filebuf) {
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       fileptr = filebuf;
+
+       state = BEGIN;
+       while (*fileptr && state != ERROR) {
+               switch (state) {
+               case BEGIN:
+                       if (*fileptr == '{') {
+                               block = calloc(1, sizeof(*block));
+                               if (!block) {
+                                       ErrPrint("calloc: %d\n", errno);
+                                       state = ERROR;
+                                       continue;
+                               }
+                               state = FIELD;
+                               ptr = NULL;
+                       }
+                       break;
+               case FIELD:
+                       if (isspace(*fileptr)) {
+                               if (ptr != NULL) {
+                                       *fileptr = '\0';
+                               }
+                       } else if (*fileptr == '=') {
+                               *fileptr = '\0';
+                               ptr = NULL;
+                               state = DATA;
+                       } else if (ptr == NULL) {
+                               ptr = fileptr;
+                               field_idx = 0;
+                               field_len = 0;
+
+                               while (field_list[field_idx]) {
+                                       if (field_list[field_idx][field_len] == *fileptr) {
+                                               break;
+                                       }
+                                       field_idx++;
+                               }
+
+                               if (!field_list[field_idx]) {
+                                       ErrPrint("Invalid field\n");
+                                       state = ERROR;
+                                       continue;
+                               }
+
+                               field_len++;
+                       } else {
+                               if (field_list[field_idx][field_len] != *fileptr) {
+                                       field_idx++;
+                                       while (field_list[field_idx]) {
+                                               if (!strncmp(field_list[field_idx], fileptr - field_len, field_len)) {
+                                                       break;
+                                               } else {
+                                                       field_idx++;
+                                               }
+                                       }
+
+                                       if (!field_list[field_idx]) {
+                                               state = ERROR;
+                                               ErrPrint("field is not valid\n");
+                                               continue;
+                                       }
+                               }
+
+                               field_len++;
+                       }
+                       break;
+               case DATA:
+                       switch (field_idx) {
+                       case FIELD_TYPE:
+                               if (ptr == NULL) {
+                                       if (isspace(*fileptr)) {
+                                               break;
+                                       }
+
+                                       if (*fileptr == '\0') {
+                                               state = ERROR;
+                                               ErrPrint("Type is not valid\n");
+                                               continue;
+                                       }
+
+                                       ptr = fileptr;
+                                       type_idx = 0;
+                                       type_len = 0;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (type_list[type_idx][type_len] != *fileptr) {
+                                       type_idx++;
+                                       while (type_list[type_idx]) {
+                                               if (!strncmp(type_list[type_idx], fileptr - type_len, type_len)) {
+                                                       break;
+                                               } else {
+                                                       type_idx++;
+                                               }
+                                       }
+
+                                       if (!type_list[type_idx]) {
+                                               state = ERROR;
+                                               ErrPrint("type is not valid (%s)\n", fileptr - type_len);
+                                               continue;
+                                       }
+                               }
+
+                               if (!*fileptr) {
+                                       block->type = type_idx;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+
+                               type_len++;
+                               break;
+                       case FIELD_PART:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->part = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                               break;
+                       case FIELD_DATA:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->data = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                               break;
+                       case FIELD_OPTION:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->option = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                               break;
+                       case FIELD_ID:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->id = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                               break;
+                       case FIELD_TARGET:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->target = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                               break;
+                       case FIELD_FILE:
+                               if (ptr == NULL) {
+                                       ptr = fileptr;
+                               }
+
+                               if (*fileptr && (*fileptr == '\n' || *fileptr == '\r' || *fileptr == '\f')) {
+                                       *fileptr = '\0';
+                               }
+
+                               if (!*fileptr) {
+                                       block->target = ptr;
+                                       state = DONE;
+                                       ptr = NULL;
+                               }
+                       default:
+                               break;
+                       }
+
+                       break;
+               case DONE:
+                       if (isspace(*fileptr)) {
+                       } else if (*fileptr == '}') {
+                               state = BEGIN;
+                               block->filename = filename;
+                               block_list = eina_list_append(block_list, block);
+                               block = NULL;
+                       } else {
+                               state = FIELD;
+                               continue;
+                       }
+                       break;
+               case END:
+               default:
+                       break;
+               }
+
+               fileptr++;
+       }
+
+       if (state != BEGIN) {
+               ErrPrint("State %d\n", state);
+
+               free(filebuf);
+               free(block);
+
+               EINA_LIST_FREE(block_list, block) {
+                       free(block);
+               }
+
+               PERF_MARK("parser");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       block = eina_list_data_get(eina_list_last(block_list));
+       if (block) {
+               block->filebuf = filebuf;
+       } else {
+               ErrPrint("Last block is not exists (There is no parsed block)\n");
+               free(filebuf);
+       }
+
+       PERF_MARK("parser");
+
+#if defined(_APPLY_SCRIPT_ASYNC_UPDATE)
+       struct apply_data *data;
+
+       data = malloc(sizeof(*data));
+       if (data) {
+               data->inst = inst;
+               data->is_pd = is_pd;
+               data->block_list = block_list;
+               if (!ecore_timer_add(0.001f, apply_changes_cb, data)) {
+                       ErrPrint("Failed to add timer\n");
+                       free(data);
+                       EINA_LIST_FREE(block_list, block) {
+                               consuming_parsed_block(inst, is_pd, block);
+                       }
+               }
+       } else {
+               ErrPrint("malloc: %d\n", errno);
+               EINA_LIST_FREE(block_list, block) {
+                       consuming_parsed_block(inst, is_pd, block);
+               }
+       }
+#else
+       ErrPrint("Begin: Set content for EDJE object\n");
+       EINA_LIST_FREE(block_list, block) {
+               consuming_parsed_block(inst, is_pd, block);
+       }
+       ErrPrint("End: Set content for EDJE object\n");
+
+       /*!
+        * Doesn't need to force to render the contents.
+        struct script_info *info;
+        info = is_pd ? instance_gbar_script(inst) : instance_widget_script(inst);
+        if (info && info->ee) {
+        ecore_evas_manual_render(info->ee);
+        }
+        */
+#endif
+
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/server.c b/src/server.c
new file mode 100644 (file)
index 0000000..9d4ad00
--- /dev/null
@@ -0,0 +1,9796 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <sys/smack.h>
+
+#include <dlog.h>
+#include <aul.h>
+#include <Ecore.h>
+
+#include <packet.h>
+#include <com-core_packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_cmd_list.h>
+#include <widget_conf.h>
+#include <widget_abi.h>
+#include <widget_script.h>
+#include <widget_util.h>
+
+#include "critical_log.h"
+#include "conf.h"
+#include "debug.h"
+#include "server.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "instance.h"
+#include "client_rpc.h"
+#include "package.h"
+#include "script_handler.h"
+#include "buffer_handler.h"
+#include "util.h"
+#include "fault_manager.h"
+#include "group.h"
+#include "xmonitor.h"
+#include "widget-mgr.h"
+#include "io.h"
+#include "event.h"
+#include "dead_monitor.h"
+#include "monitor.h"
+
+#define GBAR_OPEN_MONITOR_TAG "gbar,open,monitor"
+#define GBAR_RESIZE_MONITOR_TAG "gbar,resize,monitor"
+#define GBAR_CLOSE_MONITOR_TAG "gbar,close,monitor"
+
+#define LAZY_GBAR_OPEN_TAG "lazy,gbar,open"
+#define LAZY_GBAR_CLOSE_TAG "lazy,gbar,close"
+
+#define ACCESS_TYPE_DOWN 0
+#define ACCESS_TYPE_MOVE 1
+#define ACCESS_TYPE_UP 2
+#define ACCESS_TYPE_CUR 0
+#define ACCESS_TYPE_NEXT 1
+#define ACCESS_TYPE_PREV 2
+#define ACCESS_TYPE_OFF 3
+
+struct sync_ctx_item {
+       int pid;
+       int handle;
+       char *pkgname;
+       double timestamp;
+       Ecore_Timer *prepare_sync_wait_timer;
+};
+
+static struct info {
+       int info_fd;
+       int client_fd;
+       int service_fd;
+       int slave_fd;
+       int remote_client_fd;
+
+       Eina_List *hello_sync_ctx_list;
+} s_info = {
+       .info_fd = -1,
+       .client_fd = -1,
+       .service_fd = -1,
+       .slave_fd = -1,
+       .remote_client_fd = -1,
+       .hello_sync_ctx_list = NULL,
+};
+
+struct access_info {
+       int x;
+       int y;
+       int type;
+};
+
+/* Share this with provider */
+enum target_type {
+       TYPE_WIDGET,
+       TYPE_GBAR,
+       TYPE_ERROR
+};
+
+struct event_cbdata {
+       int status;
+       struct inst_info *inst;
+};
+
+struct deleted_item {
+       struct client_node *client;
+       struct inst_info *inst;
+};
+
+/**
+ * Returns widget Id from provider Id
+ */
+static char *is_valid_slave(pid_t pid, const char *abi, const char *provider_pkgname)
+{
+       char pid_pkgname[pathconf("/", _PC_PATH_MAX)];
+       const char *abi_pkgname;
+       widget_list_h list_handle;
+       char *pkgid;
+       char *widget_id;
+       char *converted_provider_pkgname = NULL;
+       int verified;
+
+       if (aul_app_get_pkgname_bypid(pid, pid_pkgname, sizeof(pid_pkgname)) != AUL_R_OK) {
+               ErrPrint("pid[%d] is not authroized provider package\n", pid);
+               return NULL;
+       }
+
+       abi_pkgname = widget_abi_get_pkgname_by_abi(abi);
+       if (!abi_pkgname) {
+               ErrPrint("ABI has no valid entry[%s]\n", abi);
+               return NULL;
+       }
+
+       if (!strcmp(abi_pkgname, pid_pkgname)) {
+               /*!
+                * This request is comes from predefined service provider.
+                * In this case, we should believe its request.
+                */
+               return strdup(provider_pkgname);
+       }
+
+       /*!
+        * This request is comes from standalone widget provider (not the predefined service provider)
+        */
+       pkgid = package_get_pkgid(provider_pkgname);
+       list_handle = widget_service_create_widget_list(pkgid, NULL);
+       DbgFree(pkgid);
+
+       verified = 0;
+       widget_id = NULL;
+       while (widget_service_get_item_from_widget_list(list_handle, NULL, &widget_id, NULL) == WIDGET_ERROR_NONE) {
+               if (!widget_id) {
+                       ErrPrint("Invalid widget_id\n");
+                       continue;
+               }
+
+               // tmp == /APPID/.provider <<- PROVIDER UI-APP
+               // widget_id == org.tizen.watch-hello <<-- WIDGET ID
+               // provider_pkgname == org.tizen.watch-hello.provider
+               converted_provider_pkgname = widget_util_replace_string(abi_pkgname, WIDGET_CONF_REPLACE_TAG_APPID, widget_id);
+               if (!converted_provider_pkgname) {
+                       DbgFree(widget_id);
+                       widget_id = NULL;
+                       continue;
+               }
+
+               /* Verify the Package Name */
+               verified = !strcmp(converted_provider_pkgname, provider_pkgname);
+               DbgFree(converted_provider_pkgname);
+
+               if (verified) {
+                       break;
+               }
+
+               DbgFree(widget_id);
+               widget_id = NULL;
+       }
+
+       widget_service_destroy_widget_list(list_handle);
+       return widget_id;
+}
+
+static Eina_Bool lazy_key_status_cb(void *data)
+{
+       struct event_cbdata *cbdata = data;
+
+       if (instance_unref(cbdata->inst)) {
+               instance_send_key_status(cbdata->inst, cbdata->status);
+       } else {
+               DbgPrint("Skip sending key status (%d)\n", cbdata->status);
+       }
+       /*!
+        * If instance_unref returns NULL,
+        * The instance is destroyed. it means, we don't need to send event to the viewer
+        */
+       DbgFree(cbdata);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool lazy_access_status_cb(void *data)
+{
+       struct event_cbdata *cbdata = data;
+
+       if (instance_unref(cbdata->inst)) {
+               instance_send_access_status(cbdata->inst, cbdata->status);
+       } else {
+               DbgPrint("Skip sending access status (%d)\n", cbdata->status);
+       }
+       /*!
+        * If instance_unref returns NULL,
+        * The instance is destroyed. it means, we don't need to send event to the viewer
+        */
+       DbgFree(cbdata);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+int send_delayed_key_status(struct inst_info *inst, int ret)
+{
+       struct event_cbdata *cbdata;
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ret = WIDGET_ERROR_OUT_OF_MEMORY;
+       } else {
+               cbdata->inst = instance_ref(inst);
+               cbdata->status = ret;
+
+               if (!ecore_timer_add(DELAY_TIME, lazy_key_status_cb, cbdata)) {
+                       (void)instance_unref(cbdata->inst);
+                       DbgFree(cbdata);
+                       ret = WIDGET_ERROR_FAULT;
+               } else {
+                       ret = WIDGET_ERROR_NONE;
+               }
+       }
+
+       return ret;
+}
+
+int send_delayed_access_status(struct inst_info *inst, int ret)
+{
+       struct event_cbdata *cbdata;
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ret = WIDGET_ERROR_OUT_OF_MEMORY;
+       } else {
+               cbdata->inst = instance_ref(inst);
+               cbdata->status = ret;
+
+               if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
+                       (void)instance_unref(cbdata->inst);
+                       DbgFree(cbdata);
+                       ret = WIDGET_ERROR_FAULT;
+               } else {
+                       ret = WIDGET_ERROR_NONE;
+               }
+       }
+
+       return ret;
+}
+
+static int forward_widget_event_packet(const struct pkg_info *pkg, struct inst_info *inst, const struct packet *packet)
+{
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       int ret;
+
+       buffer = instance_widget_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       packet_ref((struct packet *)packet);
+       ret = slave_rpc_request_only(slave, package_name(pkg), (struct packet *)packet, 0);
+
+out:
+       return ret;
+}
+
+static int forward_gbar_event_packet(const struct pkg_info *pkg, struct inst_info *inst, const struct packet *packet)
+{
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       int ret;
+
+       buffer = instance_gbar_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       packet_ref((struct packet *)packet);
+       ret = slave_rpc_request_only(slave, package_name(pkg), (struct packet *)packet, 0);
+
+out:
+       return ret;
+}
+
+static int forward_gbar_access_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, struct access_info *event)
+{
+       int ret;
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       struct packet *p;
+
+       buffer = instance_gbar_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       p = packet_create_noack(command, "ssdiii", package_name(pkg), instance_id(inst), timestamp, event->x, event->y, event->type);
+       ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
+
+out:
+       return ret;
+}
+
+static int forward_widget_access_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, struct access_info *event)
+{
+       int ret;
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       struct packet *p;
+
+       buffer = instance_widget_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       p = packet_create_noack(command, "ssdiii", package_name(pkg), instance_id(inst), timestamp, event->x, event->y, event->type);
+       ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
+
+out:
+       return ret;
+}
+
+static int forward_gbar_key_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, unsigned int keycode)
+{
+       int ret;
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       struct packet *p;
+
+       buffer = instance_widget_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       p = packet_create_noack(command, "ssdi", package_name(pkg), instance_id(inst), timestamp, keycode);
+       ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
+
+out:
+       return ret;
+}
+
+static int forward_widget_key_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, unsigned int keycode)
+{
+       int ret;
+       struct buffer_info *buffer;
+       struct slave_node *slave;
+       struct packet *p;
+
+       buffer = instance_widget_buffer(inst);
+       if (!buffer) {
+               ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ErrPrint("Package[%s] has no slave\n", package_name(pkg));
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       p = packet_create_noack(command, "ssdi", package_name(pkg), instance_id(inst), timestamp, keycode);
+       ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
+
+out:
+       return ret;
+}
+
+static int slave_fault_open_script_cb(struct slave_node *slave, void *data)
+{
+       Ecore_Timer *timer;
+
+       (void)script_handler_unload(instance_gbar_script(data), 1);
+       (void)instance_slave_close_gbar(data, instance_gbar_owner(data), WIDGET_CLOSE_GBAR_FAULT);
+       (void)instance_client_gbar_created(data, WIDGET_ERROR_FAULT);
+
+       timer = instance_del_data(data, LAZY_GBAR_OPEN_TAG);
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       (void)instance_unref(data);
+
+       return -1; /* remove this handler */
+}
+
+static int slave_fault_open_buffer_cb(struct slave_node *slave, void *data)
+{
+       Ecore_Timer *timer;
+
+       (void)instance_slave_close_gbar(data, instance_gbar_owner(data), WIDGET_CLOSE_GBAR_FAULT);
+       (void)instance_client_gbar_created(data, WIDGET_ERROR_FAULT);
+
+       timer = instance_del_data(data, GBAR_OPEN_MONITOR_TAG);
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       (void)instance_unref(data);
+
+       return -1; /* remove this handler */
+}
+
+static int slave_fault_close_script_cb(struct slave_node *slave, void *data)
+{
+       Ecore_Timer *timer;
+
+       (void)instance_client_gbar_destroyed(data, WIDGET_ERROR_FAULT);
+
+       timer = instance_del_data(data, LAZY_GBAR_CLOSE_TAG);
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       (void)instance_unref(data);
+
+       return -1; /* remove this handler */
+}
+
+static int slave_fault_close_buffer_cb(struct slave_node *slave, void *data)
+{
+       Ecore_Timer *timer;
+
+       (void)instance_client_gbar_destroyed(data, WIDGET_ERROR_FAULT);
+
+       timer = instance_del_data(data, LAZY_GBAR_CLOSE_TAG);
+       if (!timer) {
+               timer = instance_del_data(data, GBAR_CLOSE_MONITOR_TAG);
+       }
+
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       (void)instance_unref(data);
+
+       return -1; /* remove this handler */
+}
+
+static int slave_fault_resize_buffer_cb(struct slave_node *slave, void *data)
+{
+       Ecore_Timer *timer;
+
+       (void)instance_slave_close_gbar(data, instance_gbar_owner(data), WIDGET_CLOSE_GBAR_FAULT);
+       (void)instance_client_gbar_destroyed(data, WIDGET_ERROR_FAULT);
+
+       timer = instance_del_data(data, GBAR_RESIZE_MONITOR_TAG);
+       if (timer) {
+               ecore_timer_del(timer);
+       }
+
+       (void)instance_unref(data);
+
+       return -1; /* remove this handler */
+}
+
+static int key_event_widget_route_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       struct slave_node *slave;
+       struct packet *packet;
+       unsigned int cmd;
+
+       if (!inst) {
+               DbgPrint("Instance is deleted.\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               cmd = CMD_WIDGET_KEY_DOWN;
+               break;
+       case EVENT_STATE_ACTIVATED:
+               cmd = CMD_WIDGET_KEY_DOWN;
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               cmd = CMD_WIDGET_KEY_UP;
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdi", package_name(pkg), instance_id(inst), event_info->tv, event_info->keycode);
+       if (!packet) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
+}
+
+static int mouse_event_widget_route_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       struct slave_node *slave;
+       struct packet *packet;
+       unsigned int cmd;
+
+       if (!inst) {
+               DbgPrint("Instance is deleted.\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               cmd = CMD_WIDGET_MOUSE_DOWN;
+               break;
+       case EVENT_STATE_ACTIVATED:
+               cmd = CMD_WIDGET_MOUSE_MOVE;
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               cmd = CMD_WIDGET_MOUSE_UP;
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdii", package_name(pkg), instance_id(inst), event_info->tv, event_info->x, event_info->y);
+       if (!packet) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
+}
+
+static int key_event_widget_consume_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct script_info *script;
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       double timestamp;
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return 0;
+       }
+
+       script = instance_widget_script(inst);
+       if (!script) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       timestamp = event_info->tv;
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               break;
+       case EVENT_STATE_ACTIVATED:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_UP, timestamp);
+               break;
+       default:
+               ErrPrint("Unknown event\n");
+               break;
+       }
+
+       return 0;
+}
+
+static int mouse_event_widget_consume_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct script_info *script;
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       double timestamp;
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return 0;
+       }
+
+       script = instance_widget_script(inst);
+       if (!script) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       timestamp = event_info->tv;
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               script_handler_update_pointer(script, event_info->x, event_info->y, 1);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_DOWN, timestamp);
+               break;
+       case EVENT_STATE_ACTIVATED:
+               script_handler_update_pointer(script, event_info->x, event_info->y, -1);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_MOVE, timestamp);
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               script_handler_update_pointer(script, event_info->x, event_info->y, 0);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_UP, timestamp);
+               break;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
+static int key_event_gbar_route_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       struct slave_node *slave;
+       struct packet *packet;
+       unsigned int cmd;
+
+       if (!inst) {
+               DbgPrint("Instance is deleted.\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               cmd = CMD_GBAR_KEY_DOWN;
+               break;
+       case EVENT_STATE_ACTIVATED:
+               cmd = CMD_GBAR_KEY_DOWN;
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               cmd = CMD_GBAR_KEY_UP;
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdi", package_name(pkg), instance_id(inst), event_info->tv, event_info->keycode);
+       if (!packet) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
+}
+
+static int mouse_event_gbar_route_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       struct slave_node *slave;
+       struct packet *packet;
+       unsigned int cmd;
+
+       if (!inst) {
+               DbgPrint("Instance is deleted.\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               cmd = CMD_GBAR_MOUSE_DOWN;
+               break;
+       case EVENT_STATE_ACTIVATED:
+               cmd = CMD_GBAR_MOUSE_MOVE;
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               cmd = CMD_GBAR_MOUSE_UP;
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "ssdii", package_name(pkg), instance_id(inst), event_info->tv, event_info->x, event_info->y);
+       if (!packet) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
+}
+
+static int key_event_gbar_consume_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct script_info *script;
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       double timestamp;
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return 0;
+       }
+
+       script = instance_gbar_script(inst);
+       if (!script) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       timestamp = event_info->tv;
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               break;
+       case EVENT_STATE_ACTIVATED:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               script_handler_update_keycode(script, event_info->keycode);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_KEY_UP, timestamp);
+               break;
+       default:
+               ErrPrint("Unknown event\n");
+               break;
+       }
+
+       return 0;
+}
+
+static int mouse_event_gbar_consume_cb(enum event_state state, struct event_data *event_info, void *data)
+{
+       struct script_info *script;
+       struct inst_info *inst = data;
+       const struct pkg_info *pkg;
+       double timestamp;
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               return 0;
+       }
+
+       script = instance_gbar_script(inst);
+       if (!script) {
+               return WIDGET_ERROR_FAULT;
+       }
+
+       timestamp = event_info->tv;
+
+       switch (state) {
+       case EVENT_STATE_ACTIVATE:
+               script_handler_update_pointer(script, event_info->x, event_info->y, 1);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_DOWN, timestamp);
+               break;
+       case EVENT_STATE_ACTIVATED:
+               script_handler_update_pointer(script, event_info->x, event_info->y, -1);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_MOVE, timestamp);
+               break;
+       case EVENT_STATE_DEACTIVATE:
+               script_handler_update_pointer(script, event_info->x, event_info->y, 0);
+               (void)script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_UP, timestamp);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
+static struct packet *client_acquire(pid_t pid, int handle, const struct packet *packet) /*!< timestamp, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *direct_addr;
+       double timestamp;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (client) {
+               ErrPrint("Client is already exists %d\n", pid);
+               ret = WIDGET_ERROR_ALREADY_EXIST;
+               goto out;
+       }
+
+       if (packet_get(packet, "ds", &timestamp, &direct_addr) != 2) {
+               ErrPrint("Invalid arguemnt\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = 0;
+       /*!
+        * \note
+        * client_create will invoke the client created callback
+        */
+       client = client_create(pid, handle, direct_addr);
+       if (!client) {
+               ErrPrint("Failed to create a new client for %d\n", pid);
+               ret = WIDGET_ERROR_FAULT;
+       }
+
+out:
+       result = packet_create_reply(packet, "ii", ret, WIDGET_CONF_EXTRA_BUFFER_COUNT);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *cilent_release(pid_t pid, int handle, const struct packet *packet) /*!< pid, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       client_destroy(client);
+       ret = 0;
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static int validate_request(pid_t pid, struct slave_node *slave, const char *pkgname, const char *id, struct inst_info **out_inst, const struct pkg_info **out_pkg)
+{
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       if (slave) {
+               if (slave_valid(slave)) {
+                       inst = package_find_instance_by_id(pkgname, id);
+               } else {
+                       ErrPrint("slave is not valid (%s)\n", id);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       } else {
+               inst = package_find_instance_by_id(pkgname, id);
+       }
+
+       if (!inst) {
+               ErrPrint("Instance is not exists (%s)\n", id);
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       pkg = instance_package(inst);
+       if (!pkg) {
+               ErrPrint("System error - instance has no package?\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (package_is_fault(pkg)) {
+               ErrPrint("Faulted package: %s\n", package_name(pkg));
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (out_inst) {
+               *out_inst = inst;
+       }
+
+       if (out_pkg) {
+               *out_pkg = pkg;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/*!< pid, pkgname, filename, event, timestamp, x, y, ret */
+static struct packet *client_clicked(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       const char *event;
+       double timestamp;
+       double x;
+       double y;
+       int ret;
+       struct inst_info *inst;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sssddd", &pkgname, &id, &event, &timestamp, &x, &y);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               (void)instance_clicked(inst, event, timestamp, x, y);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_update_mode(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       int active_update;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       struct inst_info *inst;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &active_update);
+       if (ret != 3) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               /*!
+                * \note
+                * Send change update mode request to a slave
+                */
+               ret = instance_set_update_mode(inst, active_update);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+/* pid, pkgname, filename, signal_name, source, s, sy, ex, ey, ret */
+static struct packet *client_text_signal(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       const char *signal_name;
+       const char *source;
+       double sx;
+       double sy;
+       double ex;
+       double ey;
+       struct inst_info *inst = NULL;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssssdddd", &pkgname, &id, &signal_name, &source, &sx, &sy, &ex, &ey);
+       if (ret != 8) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               ret = instance_text_signal_emit(inst, signal_name, source, sx, sy, ex, ey);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static Eina_Bool lazy_delete_cb(void *data)
+{
+       struct deleted_item *item = data;
+
+       DbgPrint("Lazy delete callback called\n");
+       /*!
+        * Before invoke this callback, the instance is able to already remove this client
+        * So check it again
+        */
+       if (instance_has_client(item->inst, item->client)) {
+               (void)instance_unicast_deleted_event(item->inst, item->client, WIDGET_ERROR_NONE);
+               (void)instance_del_client(item->inst, item->client);
+       }
+
+       (void)client_unref(item->client);
+       (void)instance_unref(item->inst);
+       DbgFree(item);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *client_delete(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       int ret;
+       int type;
+       double timestamp;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssid", &pkgname, &id, &type, &timestamp);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+       /*!
+        * \note
+        * Below two types must has to be sync'd with widget-viewer
+        *
+        * WIDGET_DELETE_PERMANENTLY = 0x01,
+        * WIDGET_DELETE_TEMPORARY = 0x02,
+        *
+        */
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               DbgPrint("Failed to find by id(%s), try to find it using timestamp(%lf)\n", id, timestamp);
+               inst = package_find_instance_by_timestamp(pkgname, timestamp);
+               if (!inst) {
+                       goto out;
+               }
+
+               pkg = instance_package(inst);
+               if (!pkg) {
+                       ErrPrint("Package info is not valid: %s\n", id);
+                       goto out;
+               }
+       }
+
+       if (package_is_fault(pkg)) {
+               DbgPrint("Faulted package. will be deleted soon: %s\n", id);
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               if (instance_has_client(inst, client)) {
+                       struct deleted_item *item;
+
+                       item = malloc(sizeof(*item));
+                       if (!item) {
+                               ErrPrint("malloc: %d\n", errno);
+                               ret = WIDGET_ERROR_OUT_OF_MEMORY;
+                       } else {
+                               /*!
+                                * \NOTE:
+                                * Send DELETED EVENT to the client.
+                                * after return from this function.
+                                *
+                                * Client will prepare the deleted event after get this function's return value.
+                                * So We have to make a delay to send a deleted event.
+                                */
+
+                               item->client = client_ref(client);
+                               item->inst = instance_ref(inst);
+
+                               if (!ecore_timer_add(DELAY_TIME, lazy_delete_cb, item)) {
+                                       ErrPrint("Failed to add a delayzed delete callback\n");
+                                       (void)client_unref(client);
+                                       (void)instance_unref(inst);
+                                       DbgFree(item);
+                                       ret = WIDGET_ERROR_FAULT;
+                               } else {
+                                       ret = WIDGET_ERROR_NONE;
+                               }
+                       }
+               } else {
+                       ErrPrint("Client has no permission\n");
+                       ret = WIDGET_ERROR_PERMISSION_DENIED;
+               }
+       } else {
+               switch (type) {
+               case WIDGET_DELETE_PERMANENTLY:
+                       ret = instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       break;
+               case WIDGET_DELETE_TEMPORARY:
+                       ret = instance_destroy(inst, WIDGET_DESTROY_TYPE_TEMPORARY);
+                       break;
+               default:
+                       break;
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_resize(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, w, h, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       int w;
+       int h;
+       struct inst_info *inst = NULL;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssii", &pkgname, &id, &w, &h);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("RESIZE: Client request resize to %dx%d (pid: %d, pkgname: %s)\n", w, h, pid, pkgname);
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               ret = WIDGET_ERROR_PERMISSION_DENIED;
+       } else {
+               ret = instance_resize(inst, w, h);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_new(pid_t pid, int handle, const struct packet *packet) /* pid, timestamp, pkgname, content, cluster, category, period, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *content;
+       const char *cluster;
+       const char *category;
+       double period;
+       double timestamp;
+       int ret;
+       struct pkg_info *info;
+       int width;
+       int height;
+       char *widget_id;
+       char *mainappid;
+       int max_count;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "dssssdii", &timestamp, &pkgname, &content, &cluster, &category, &period, &width, &height);
+       if (ret != 8) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("pid[%d] period[%lf] pkgname[%s] content[%s] cluster[%s] category[%s] period[%lf]\n",
+                       pid, timestamp, pkgname, content, cluster, category, period);
+
+       widget_id = package_widget_pkgname(pkgname);
+       if (!widget_id) {
+               ErrPrint("This %s has no widget package\n", pkgname);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       /**
+        * @TODO
+        * Should be replaced with package manager API to decide whether it is enabled or not.
+        */
+       mainappid = widget_service_get_main_app_id(widget_id);
+       if (!package_is_enabled(mainappid)) {
+               ErrPrint("%s is disabled\n", mainappid);
+               DbgFree(mainappid);
+               DbgFree(widget_id);
+               ret = WIDGET_ERROR_DISABLED;
+               goto out;
+       }
+       DbgFree(mainappid);
+
+       info = package_find(widget_id);
+       if (!info) {
+               char *pkgid;
+               pkgid = widget_service_get_package_id(widget_id);
+               if (!pkgid) {
+                       DbgFree(mainappid);
+                       DbgFree(widget_id);
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               info = package_create(pkgid, widget_id);
+               DbgFree(pkgid);
+       }
+
+       max_count = widget_service_get_widget_max_count(widget_id);
+       if (max_count < 0) {
+               ErrPrint("Failed to get max_count[%s] = %d\n", widget_id, max_count);
+               max_count = 0;
+       }
+
+       if (!info) {
+               ret = WIDGET_ERROR_FAULT;
+       } else if (package_is_fault(info)) {
+               ret = WIDGET_ERROR_FAULT;
+       } else if (util_free_space(WIDGET_CONF_IMAGE_PATH) <= WIDGET_CONF_MINIMUM_SPACE) {
+               ErrPrint("Not enough space\n");
+               ret = WIDGET_ERROR_FILE_NO_SPACE_ON_DEVICE;
+       } else if (max_count && max_count <= package_instance_count(info)) {
+               ErrPrint("Reached to the max count of widgets %d, %d\n", max_count, package_instance_count(info));
+               ret = WIDGET_ERROR_CANCELED;
+       } else {
+               struct inst_info *inst;
+
+               if (period > 0.0f && period < WIDGET_CONF_MINIMUM_PERIOD) {
+                       period = WIDGET_CONF_MINIMUM_PERIOD;
+               }
+
+               inst = instance_create(client, timestamp, widget_id, content, cluster, category, period, width, height);
+               /*!
+                * \note
+                * Using the "inst" without validate its value is at my disposal. ;)
+                */
+               ret = inst ? 0 : WIDGET_ERROR_FAULT;
+       }
+
+       DbgFree(widget_id);
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_change_visibility(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       enum widget_visible_state state;
+       int ret;
+       struct inst_info *inst;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, (int *)&state);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               ret = WIDGET_ERROR_PERMISSION_DENIED;
+       } else {
+               ret = instance_set_visible_state(inst, state);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_set_period(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, period, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       double period;
+       int ret;
+       struct inst_info *inst = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssd", &pkgname, &id, &period);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("pid[%d] pkgname[%s] period[%lf]\n", pid, pkgname, period);
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               ret = WIDGET_ERROR_PERMISSION_DENIED;
+       } else {
+               ret = instance_set_period(inst, period);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_change_group(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, cluster, category, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       const char *cluster;
+       const char *category;
+       struct inst_info *inst = NULL;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssss", &pkgname, &id, &cluster, &category);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("pid[%d] pkgname[%s] cluster[%s] category[%s]\n", pid, pkgname, cluster, category);
+
+       /*!
+        * \NOTE:
+        * Trust the package name which are sent by the client.
+        * The package has to be a widget package name.
+        */
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               ret = WIDGET_ERROR_PERMISSION_DENIED;
+       } else {
+               ret = instance_change_group(inst, cluster, category);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_mouse_enter(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_IN, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_leave(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OUT, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_down(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, id, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, 1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_DOWN, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_up(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, 0);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_UP, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_move(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_MOVE, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_move(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_MOVE, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static int inst_del_cb(struct inst_info *inst, void *data)
+{
+       int ret;
+
+       /*
+        * If you deactivate the event thread,
+        * It will calls event callbacks.
+        * And the event callbacks will try to access the "inst"
+        */
+       (void)event_deactivate(data, inst);
+
+       /* Reset callback data to prevent accessing inst from event callback */
+       ret = event_reset_cbdata(data, inst, NULL);
+       DbgPrint("Instance delete callback called: %s (%d)\n", instance_id(inst), ret);
+
+       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+               (void)slave_set_priority(package_slave(instance_package(inst)), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+       }
+
+       return -1; /* Delete this callback */
+}
+
+static struct packet *client_gbar_key_set(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = event_activate(0, 0, key_event_gbar_route_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_route_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_route_cb);
+                       }
+               }
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               ret = event_activate(0, 0, key_event_gbar_consume_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_consume_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_consume_cb);
+                       }
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_key_unset(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = event_deactivate(key_event_gbar_route_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_route_cb);
+                }
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               ret = event_deactivate(key_event_gbar_consume_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_gbar_consume_cb);
+                }
+                */
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_set(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = event_activate(0, 0, key_event_widget_route_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_route_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_route_cb);
+                       }
+               }
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               ret = event_activate(0, 0, key_event_widget_consume_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_consume_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_consume_cb);
+                       }
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_unset(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = event_deactivate(key_event_widget_route_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_route_cb);
+                }
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               ret = event_deactivate(key_event_widget_consume_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, key_event_widget_consume_cb);
+                }
+                */
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_mouse_set(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               if (package_direct_input(pkg) == 0 || packet_set_fd((struct packet *)packet, event_input_fd()) < 0) {
+                       ret = event_activate(x, y, mouse_event_widget_route_cb, inst);
+                       if (ret == WIDGET_ERROR_NONE) {
+                               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                               }
+                               if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_route_cb) <= 0) {
+                                       instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_route_cb);
+                               }
+                       }
+               } else {
+                       struct slave_node *slave;
+
+                       DbgPrint("Direct input is enabled(set for %s:%d)\n", id, packet_fd(packet));
+                       slave = package_slave(pkg);
+                       if (slave) {
+                               packet_ref((struct packet *)packet);
+                               ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
+                       } else {
+                               ErrPrint("Unable to find a slave for %s\n", pkgname);
+                               ret = WIDGET_ERROR_FAULT;
+                       }
+               }
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               ret = event_activate(x, y, mouse_event_widget_consume_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_consume_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_consume_cb);
+                       }
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+out:
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_unset(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               if (package_direct_input(pkg) == 0) {
+                       /* Forcely update the X,Y position using viewer's */
+                       event_set_mouse_xy(x, y, timestamp);
+
+                       ret = event_deactivate(mouse_event_widget_route_cb, inst);
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+                       }
+                       /*
+                        * This delete callback will be removed when the instance will be destroyed.
+                        if (ret == 0) {
+                        instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_route_cb);
+                        }
+                        */
+               } else {
+                       struct slave_node *slave;
+
+                       DbgPrint("Direct input is enabled(unset) for %s\n", id);
+                       slave = package_slave(pkg);
+                       if (slave) {
+                               packet_ref((struct packet *)packet);
+                               ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
+                       } else {
+                               ErrPrint("Unable to find a slave for %s\n", pkgname);
+                               ret = WIDGET_ERROR_FAULT;
+                       }
+               }
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               /* Forcely update the X,Y position using viewer's */
+               event_set_mouse_xy(x, y, timestamp);
+
+               ret = event_deactivate(mouse_event_widget_consume_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_widget_consume_cb);
+                }
+                */
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+out:
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_set(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               if (package_direct_input(pkg) == 0 || packet_set_fd((struct packet *)packet, event_input_fd()) < 0) {
+                       ret = event_activate(x, y, mouse_event_gbar_route_cb, inst);
+                       if (ret == WIDGET_ERROR_NONE) {
+                               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                               }
+                               if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_route_cb) <= 0) {
+                                       instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_route_cb);
+                               }
+                       }
+               } else {
+                       struct slave_node *slave;
+
+                       DbgPrint("Direct input is enabled(set for %s:%d)\n", id, packet_fd(packet));
+                       slave = package_slave(pkg);
+                       if (slave) {
+                               packet_ref((struct packet *)packet);
+                               ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
+                       } else {
+                               ErrPrint("Unable to find a slave for %s\n", pkgname);
+                               ret = WIDGET_ERROR_FAULT;
+                       }
+               }
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               ret = event_activate(x, y, mouse_event_gbar_consume_cb, inst);
+               if (ret == WIDGET_ERROR_NONE) {
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_ON);
+                       }
+                       if (instance_event_callback_is_added(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_consume_cb) <= 0) {
+                               instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_consume_cb);
+                       }
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_on_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_ON_SCROLL, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_off_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OFF_SCROLL, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_on_hold(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_ON_HOLD, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_off_hold(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OFF_HOLD, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_on_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_ON_SCROLL, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_off_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OFF_SCROLL, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_on_hold(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_ON_HOLD, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_off_hold(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_event_packet(pkg, inst, packet);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OFF_HOLD, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_mouse_unset(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               if (package_direct_input(pkg) == 0) {
+                       /* Forcely update the X,Y position using viewer's */
+                       event_set_mouse_xy(x, y, timestamp);
+
+                       ret = event_deactivate(mouse_event_gbar_route_cb, inst);
+                       if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                               (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+                       }
+                       /*
+                        * This delete callback will be removed when the instance will be destroyed.
+                        if (ret == 0) {
+                        instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_route_cb);
+                        }
+                        */
+               } else {
+                       struct slave_node *slave;
+
+                       DbgPrint("Direct input is enabled(unset) for %s\n", id);
+                       slave = package_slave(pkg);
+                       if (slave) {
+                               packet_ref((struct packet *)packet);
+                               ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
+                       } else {
+                               ErrPrint("Unable to find a slave for %s\n", pkgname);
+                               ret = WIDGET_ERROR_FAULT;
+                       }
+               }
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               /* Forcely update the X,Y position using viewer's */
+               event_set_mouse_xy(x, y, timestamp);
+
+               ret = event_deactivate(mouse_event_gbar_consume_cb, inst);
+               if (WIDGET_CONF_SLAVE_EVENT_BOOST_OFF != WIDGET_CONF_SLAVE_EVENT_BOOST_ON) {
+                       (void)slave_set_priority(package_slave(pkg), WIDGET_CONF_SLAVE_EVENT_BOOST_OFF);
+               }
+               /*
+                * This delete callback will be removed when the instance will be destroyed.
+                if (ret == 0) {
+                instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, mouse_event_gbar_consume_cb);
+                }
+                */
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+out:
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_enter(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_IN, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_leave(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, -1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_OUT, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_down(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, 1);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_DOWN, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_widget_mouse_up(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       int x;
+       int y;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
+       if (ret != 5) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_event_packet(pkg, inst, packet);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, x, y, 0);
+               script_handler_feed_event(script, WIDGET_SCRIPT_MOUSE_UP, timestamp);
+               ret = 0;
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_gbar_access_action(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_ACTION, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_SCROLL, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_value_change(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_VALUE_CHANGE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_mouse(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_MOUSE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_back(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_BACK, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_over(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_OVER, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_read(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_READ, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_enable(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+               int type;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               type = (event.type == 0) ? WIDGET_SCRIPT_ACCESS_DISABLE : WIDGET_SCRIPT_ACCESS_ENABLE;
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, type, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_hl(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+               int type;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               switch (event.type) {
+               case ACCESS_TYPE_CUR:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT;
+                       break;
+               case ACCESS_TYPE_NEXT:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT_NEXT;
+                       break;
+               case ACCESS_TYPE_PREV:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT_PREV;
+                       break;
+               case ACCESS_TYPE_OFF:
+                       type = WIDGET_SCRIPT_ACCESS_UNHIGHLIGHT;
+                       break;
+               default:
+                       ret = WIDGET_ERROR_INVALID_PARAMETER;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, type, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_access_activate(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_ACTIVATE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_key_focus_in(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_FOCUS_IN, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_key_focus_out(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_FOCUS_OUT, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_key_down(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_pause_request(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       double timestamp;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is paused - manually reported\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "d", &timestamp);
+       if (ret != 1) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (WIDGET_CONF_USE_XMONITOR) {
+               DbgPrint("XMONITOR enabled. ignore client paused request\n");
+       } else {
+               xmonitor_pause(client);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *client_resume_request(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       double timestamp;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "d", &timestamp);
+       if (ret != 1) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       if (WIDGET_CONF_USE_XMONITOR) {
+               DbgPrint("XMONITOR enabled. ignore client resumed request\n");
+       } else {
+               xmonitor_resume(client);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *client_orientation(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       double timestamp;
+       int degree;
+       int ret;
+       struct pkg_info *pkg;
+       Eina_List *pkg_list;
+       Eina_List *l;
+       Eina_List *inst_list;
+       Eina_List *inst_l;
+       struct inst_info *inst;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "di", &timestamp, &degree);
+       if (ret != 2) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       ret = 0;
+
+       pkg_list = (Eina_List *)package_list();
+
+       EINA_LIST_FOREACH(pkg_list, l, pkg) {
+               inst_list = package_instance_list(pkg);
+               EINA_LIST_FOREACH(inst_list, inst_l, inst) {
+                       if (instance_client(inst) == client || instance_has_client(inst, client)) {
+                               instance_set_orientation(inst, degree);
+                               ret++;
+                       }
+               }
+       }
+       DbgPrint("%d instances are affected (orientation: %d)\n", ret, degree);
+
+out:
+       return NULL;
+}
+
+static struct packet *client_gbar_key_up(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Invalid parameter\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               ret = forward_gbar_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_gbar_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_UP, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_hl(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+               int type;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               switch (event.type) {
+               case ACCESS_TYPE_CUR:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT;
+                       break;
+               case ACCESS_TYPE_NEXT:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT_NEXT;
+                       break;
+               case ACCESS_TYPE_PREV:
+                       type = WIDGET_SCRIPT_ACCESS_HIGHLIGHT_PREV;
+                       break;
+               case ACCESS_TYPE_OFF:
+                       type = WIDGET_SCRIPT_ACCESS_UNHIGHLIGHT;
+                       break;
+               default:
+                       ret = WIDGET_ERROR_INVALID_PARAMETER;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, type, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_action(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_ACTION, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_SCROLL, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_value_change(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_VALUE_CHANGE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_mouse(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_MOUSE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_back(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_BACK, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_over(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_OVER, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_read(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_READ, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_enable(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       struct access_info event;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exist\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+               /*!
+                * Enen if it fails to send packet,
+                * The packet will be unref'd
+                * So we don't need to check the ret value.
+                */
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+               int type;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               type = (event.type == 0) ? WIDGET_SCRIPT_ACCESS_DISABLE : WIDGET_SCRIPT_ACCESS_ENABLE;
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, type, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_access_activate(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       struct access_info event;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdiii", &pkgname, &id, &timestamp, &event.x, &event.y, &event.type);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_access_packet(pkg, inst, packet_command(packet), timestamp, &event);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ErrPrint("Instance has no script\n");
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_pointer(script, event.x, event.y, event.type);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_ACCESS_ACTIVATE, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_access_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_down(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_DOWN, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_focus_in(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_FOCUS_IN, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_focus_out(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_FOCUS_OUT, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_key_up(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       double timestamp;
+       unsigned int keycode;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       struct packet *result;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &keycode);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = forward_widget_key_packet(pkg, inst, packet_command(packet), timestamp, keycode);
+       } else if (package_widget_type(pkg) == WIDGET_TYPE_SCRIPT) {
+               struct script_info *script;
+
+               script = instance_widget_script(inst);
+               if (!script) {
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               script_handler_update_keycode(script, keycode);
+               ret = script_handler_feed_event(script, WIDGET_SCRIPT_KEY_UP, timestamp);
+               if (ret >= 0) {
+                       ret = send_delayed_key_status(inst, ret);
+               }
+       } else {
+               ErrPrint("Unsupported package\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static int release_pixmap_cb(struct client_node *client, void *canvas)
+{
+       DbgPrint("Forcely unref the \"buffer\"\n");
+       buffer_handler_pixmap_unref(canvas);
+       return -1; /* Delete this callback */
+}
+
+static struct packet *client_widget_acquire_xpixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       struct inst_info *inst;
+       int ret;
+       int pixmap = 0;
+       void *buf_ptr;
+       struct buffer_info *buffer;
+       int idx;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &idx);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (idx >= WIDGET_CONF_EXTRA_BUFFER_COUNT || idx < 0) {
+               DbgPrint("Index is not valid: %d\n", idx);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       buffer = instance_widget_extra_buffer(inst, idx);
+       if (!buffer) {
+               ErrPrint("Extra buffer for %d is not available\n", idx);
+               goto out;
+       }
+
+       buf_ptr = buffer_handler_pixmap_ref(buffer);
+       if (!buf_ptr) {
+               ErrPrint("Failed to ref pixmap\n");
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
+       if (ret < 0) {
+               ErrPrint("Failed to add a new client deactivate callback\n");
+               buffer_handler_pixmap_unref(buf_ptr);
+       } else {
+               pixmap = buffer_handler_pixmap(buffer);
+               ret = WIDGET_ERROR_NONE;
+       }
+
+out:
+       result = packet_create_reply(packet, "ii", pixmap, ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_acquire_pixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       struct inst_info *inst;
+       int ret;
+       int pixmap = 0;
+       void *buf_ptr;
+       struct buffer_info *buffer;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       buffer = instance_widget_buffer(inst);
+       if (!buffer) {
+               struct script_info *script_info;
+
+               script_info = instance_widget_script(inst);
+               if (!script_info) {
+                       ErrPrint("Unable to get WIDGET buffer: %s\n", id);
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               buffer = script_handler_buffer_info(script_info);
+               if (!buffer) {
+                       ErrPrint("Unable to get buffer_info: %s\n", id);
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+       }
+
+       buf_ptr = buffer_handler_pixmap_ref(buffer);
+       if (!buf_ptr) {
+               ErrPrint("Failed to ref pixmap\n");
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
+       if (ret < 0) {
+               ErrPrint("Failed to add a new client deactivate callback\n");
+               buffer_handler_pixmap_unref(buf_ptr);
+       } else {
+               pixmap = buffer_handler_pixmap(buffer);
+               ret = WIDGET_ERROR_NONE;
+       }
+
+out:
+       result = packet_create_reply(packet, "ii", pixmap, ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_widget_release_pixmap(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       int pixmap;
+       void *buf_ptr;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, NULL, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               DbgPrint("It seems that the instance is already deleted: %s\n", id);
+       }
+
+       buf_ptr = buffer_handler_pixmap_find(pixmap);
+       if (!buf_ptr) {
+               ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
+               goto out;
+       }
+
+       if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0) {
+               buffer_handler_pixmap_unref(buf_ptr);
+       }
+
+out:
+       /**
+        * @note No reply packet
+        */
+       return NULL;
+}
+
+static struct packet *client_gbar_acquire_xpixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       struct inst_info *inst;
+       int ret;
+       int pixmap = 0;
+       void *buf_ptr;
+       struct buffer_info *buffer;
+       int idx;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &idx);
+       if (ret != 3) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       if (idx >= WIDGET_CONF_EXTRA_BUFFER_COUNT || idx < 0) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       buffer = instance_gbar_extra_buffer(inst, idx);
+       if (!buffer) {
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       buf_ptr = buffer_handler_pixmap_ref(buffer);
+       if (!buf_ptr) {
+               ErrPrint("Failed to ref pixmap\n");
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
+       if (ret < 0) {
+               ErrPrint("Failed to add a new client deactivate callback\n");
+               buffer_handler_pixmap_unref(buf_ptr);
+       } else {
+               pixmap = buffer_handler_pixmap(buffer);
+               ret = WIDGET_ERROR_NONE;
+       }
+
+out:
+       result = packet_create_reply(packet, "ii", pixmap, ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_acquire_pixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+{
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       struct inst_info *inst;
+       int ret;
+       int pixmap = 0;
+       void *buf_ptr;
+       struct buffer_info *buffer;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_get_data(inst, GBAR_RESIZE_MONITOR_TAG)) {
+               ret = WIDGET_ERROR_RESOURCE_BUSY;
+               goto out;
+       }
+
+       buffer = instance_gbar_buffer(inst);
+       if (!buffer) {
+               struct script_info *script_info;
+
+               script_info = instance_gbar_script(inst);
+               if (!script_info) {
+                       ErrPrint("Unable to get WIDGET buffer: %s\n", id);
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+
+               buffer = script_handler_buffer_info(script_info);
+               if (!buffer) {
+                       ErrPrint("Unable to get buffer_info: %s\n", id);
+                       ret = WIDGET_ERROR_FAULT;
+                       goto out;
+               }
+       }
+
+       buf_ptr = buffer_handler_pixmap_ref(buffer);
+       if (!buf_ptr) {
+               ErrPrint("Failed to ref pixmap\n");
+               ret = WIDGET_ERROR_FAULT;
+               goto out;
+       }
+
+       ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
+       if (ret < 0) {
+               ErrPrint("Failed to add a new client deactivate callback\n");
+               buffer_handler_pixmap_unref(buf_ptr);
+       } else {
+               pixmap = buffer_handler_pixmap(buffer);
+               ret = WIDGET_ERROR_NONE;
+       }
+
+out:
+       result = packet_create_reply(packet, "ii", pixmap, ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_gbar_release_pixmap(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *pkgname;
+       const char *id;
+       struct client_node *client;
+       int pixmap;
+       void *buf_ptr;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, NULL, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               DbgPrint("It seems that the instance is already deleted: %s\n", id);
+       }
+
+       buf_ptr = buffer_handler_pixmap_find(pixmap);
+       if (!buf_ptr) {
+               ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
+               goto out;
+       }
+
+       if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0) {
+               buffer_handler_pixmap_unref(buf_ptr);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_pinup_changed(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, pinup, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       int pinup;
+       int ret;
+       struct inst_info *inst;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               pinup = 0;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &pinup);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               pinup = 0;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               ret = instance_set_pinup(inst, pinup);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static Eina_Bool lazy_gbar_created_cb(void *inst)
+{
+       struct pkg_info *pkg;
+
+       if (!instance_del_data(inst, LAZY_GBAR_OPEN_TAG)) {
+               ErrPrint("lazy,pd,open is already deleted.\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       pkg = instance_package(inst);
+       if (pkg) {
+               struct slave_node *slave;
+
+               slave = package_slave(pkg);
+               if (slave) {
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_script_cb, inst);
+               }
+       }
+
+       /*!
+        * After unref instance first,
+        * if the instance is not destroyed, try to notify the created GBAR event to the client.
+        */
+       if (instance_unref(inst)) {
+               int ret;
+               ret = instance_client_gbar_created(inst, WIDGET_ERROR_NONE);
+               if (ret < 0) {
+                       DbgPrint("Send GBAR Create event (%d) to client\n", ret);
+               }
+       }
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool lazy_gbar_destroyed_cb(void *inst)
+{
+       struct pkg_info *pkg;
+       struct slave_node *slave;
+
+       if (!instance_del_data(inst, LAZY_GBAR_CLOSE_TAG)) {
+               ErrPrint("lazy,pd,close is already deleted.\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       pkg = instance_package(inst);
+       if (pkg) {
+               slave = package_slave(pkg);
+               if (slave) {
+                       if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+                               DbgPrint("Delete script type close callback\n");
+                               (void)slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_script_cb, inst);
+                       } else if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+                               DbgPrint("Delete buffer type close callback\n");
+                               (void)slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
+                       }
+               }
+       }
+
+       if (instance_unref(inst)) {
+               int ret;
+
+               /*!
+                * If the instance is not deleted, we should send pd-destroy event from here.
+                */
+               ret = instance_client_gbar_destroyed(inst, WIDGET_ERROR_NONE);
+               if (ret < 0) {
+                       ErrPrint("Failed sending GBAR Destroy event (%d)\n", ret);
+               }
+       }
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *client_gbar_move(pid_t pid, int handle, const struct packet *packet) /* pkgname, id, x, y */
+{
+       struct client_node *client;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       const char *pkgname;
+       const char *id;
+       double x = 0.0f;
+       double y = 0.0f;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdd", &pkgname, &id, &x, &y);
+       if (ret != 4) {
+               ErrPrint("Parameter is not correct\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               instance_slave_set_gbar_pos(inst, x, y);
+               ret = instance_signal_emit(inst, "pd,move", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               int ix;
+               int iy;
+
+               instance_slave_set_gbar_pos(inst, x, y);
+               ix = x * instance_gbar_width(inst);
+               iy = y * instance_gbar_height(inst);
+               script_handler_update_pointer(instance_gbar_script(inst), ix, iy, 0);
+               ret = instance_signal_emit(inst, "pd,move", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
+       } else {
+               ErrPrint("Invalid GBAR type\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+out:
+       DbgPrint("Update GBAR position: %d\n", ret);
+       return NULL;
+}
+
+static Eina_Bool gbar_open_monitor_cb(void *inst)
+{
+       struct pkg_info *pkg;
+
+       pkg = instance_package(inst);
+       if (pkg) {
+               struct slave_node *slave;
+
+               slave = package_slave(pkg);
+               if (slave) {
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst);
+               }
+       }
+
+       (void)instance_slave_close_gbar(inst, instance_gbar_owner(inst), WIDGET_CLOSE_GBAR_TIMEOUT);
+       (void)instance_client_gbar_created(inst, WIDGET_ERROR_TIMED_OUT);
+       (void)instance_del_data(inst, GBAR_OPEN_MONITOR_TAG);
+       (void)instance_unref(inst);
+       ErrPrint("GBAR Open request is timed-out (%lf)\n", WIDGET_CONF_GBAR_REQUEST_TIMEOUT);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool gbar_close_monitor_cb(void *inst)
+{
+       struct pkg_info *pkg;
+
+       pkg = instance_package(inst);
+       if (pkg) {
+               struct slave_node *slave;
+
+               slave = package_slave(pkg);
+               if (slave) {
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
+               }
+       }
+
+       (void)instance_client_gbar_destroyed(inst, WIDGET_ERROR_TIMED_OUT);
+       (void)instance_del_data(inst, GBAR_CLOSE_MONITOR_TAG);
+       (void)instance_unref(inst);
+       ErrPrint("GBAR Close request is not processed in %lf seconds\n", WIDGET_CONF_GBAR_REQUEST_TIMEOUT);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool gbar_resize_monitor_cb(void *inst)
+{
+       struct pkg_info *pkg;
+
+       pkg = instance_package(inst);
+       if (pkg) {
+               struct slave_node *slave;
+               slave = package_slave(pkg);
+               if (slave) {
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst);
+               }
+       }
+
+       (void)instance_slave_close_gbar(inst, instance_gbar_owner(inst), WIDGET_CLOSE_GBAR_TIMEOUT);
+       (void)instance_client_gbar_destroyed(inst, WIDGET_ERROR_TIMED_OUT);
+       (void)instance_del_data(inst, GBAR_RESIZE_MONITOR_TAG);
+       (void)instance_unref(inst);
+       ErrPrint("GBAR Resize request is not processed in %lf seconds\n", WIDGET_CONF_GBAR_REQUEST_TIMEOUT);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *client_create_gbar(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       Ecore_Timer *gbar_monitor;
+       double x;
+       double y;
+
+       DbgPrint("PERF_WIDGET\n");
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdd", &pkgname, &id, &x, &y);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_gbar_owner(inst)) {
+               ErrPrint("GBAR is already owned\n");
+               ret = WIDGET_ERROR_ALREADY_EXIST;
+       } else if (package_gbar_type(instance_package(inst)) == GBAR_TYPE_BUFFER) {
+               gbar_monitor = instance_get_data(inst, LAZY_GBAR_CLOSE_TAG);
+               if (gbar_monitor) {
+                       ecore_timer_del(gbar_monitor);
+                       /* This timer attribute will be deleted */
+                       lazy_gbar_destroyed_cb(inst);
+               }
+
+               if (instance_get_data(inst, GBAR_OPEN_MONITOR_TAG)) {
+                       DbgPrint("GBAR Open request is already processed\n");
+                       ret = WIDGET_ERROR_ALREADY_EXIST;
+                       goto out;
+               }
+
+               if (instance_get_data(inst, GBAR_CLOSE_MONITOR_TAG)) {
+                       DbgPrint("GBAR Close request is already in process\n");
+                       ret = WIDGET_ERROR_RESOURCE_BUSY;
+                       goto out;
+               }
+
+               if (instance_get_data(inst, GBAR_RESIZE_MONITOR_TAG)) {
+                       DbgPrint("GBAR resize request is already in process\n");
+                       ret = WIDGET_ERROR_RESOURCE_BUSY;
+                       goto out;
+               }
+
+               instance_slave_set_gbar_pos(inst, x, y);
+               /*!
+                * \note
+                * Send request to the slave.
+                * The SLAVE must has to repsonse this via "release_buffer" method.
+                */
+               ret = instance_slave_open_gbar(inst, client);
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       ret = instance_signal_emit(inst, "gbar,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
+                       if (ret != WIDGET_ERROR_NONE) {
+                               int tmp_ret;
+
+                               tmp_ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_FAULT);
+                               if (tmp_ret < 0) {
+                                       ErrPrint("Unable to send script event for openning GBAR [%s], %d\n", pkgname, tmp_ret);
+                               }
+                       } else {
+                               gbar_monitor = ecore_timer_add(WIDGET_CONF_GBAR_REQUEST_TIMEOUT, gbar_open_monitor_cb, instance_ref(inst));
+                               if (!gbar_monitor) {
+                                       (void)instance_unref(inst);
+                                       ErrPrint("Failed to create a timer for GBAR Open monitor\n");
+                               } else {
+                                       struct slave_node *slave;
+
+                                       (void)instance_set_data(inst, GBAR_OPEN_MONITOR_TAG, gbar_monitor);
+
+                                       slave = package_slave(pkg);
+                                       if (!slave) {
+                                               ErrPrint("Failed to get slave(%s)\n", pkgname);
+                                               goto out;
+                                       }
+
+                                       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst) != WIDGET_ERROR_NONE) {
+                                               ErrPrint("Failed to add fault handler: %s\n");
+                                       }
+                               }
+                       }
+               } else {
+                       ErrPrint("Unable to send request for openning GBAR [%s]\n", pkgname);
+               }
+
+               /*!
+                * \note
+                * GBAR craeted event will be send by the acquire_buffer function.
+                * Because the slave will make request the acquire_buffer to
+                * render the GBAR
+                *
+                * instance_client_gbar_created(inst);
+                */
+       } else if (package_gbar_type(instance_package(inst)) == GBAR_TYPE_SCRIPT) {
+               int ix;
+               int iy;
+
+               gbar_monitor = instance_get_data(inst, LAZY_GBAR_CLOSE_TAG);
+               if (gbar_monitor) {
+                       ecore_timer_del(gbar_monitor);
+                       /* lazy,pd,close will be deleted */
+                       lazy_gbar_destroyed_cb(inst);
+               }
+
+               /*!
+                * \note
+                * ret value should be cared but in this case,
+                * we ignore this for this moment, so we have to handle this error later.
+                *
+                * if ret is less than 0, the slave has some problem.
+                * but the script mode doesn't need slave for rendering default view of GBAR
+                * so we can hanle it later.
+                */
+               instance_slave_set_gbar_pos(inst, x, y);
+               ix = x * instance_gbar_width(inst);
+               iy = y * instance_gbar_height(inst);
+
+               script_handler_update_pointer(instance_gbar_script(inst), ix, iy, 0);
+
+               ret = instance_slave_open_gbar(inst, client);
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       ret = script_handler_load(instance_gbar_script(inst), 1);
+
+                       /*!
+                        * \note
+                        * Send the GBAR created event to the clients,
+                        */
+                       if (ret == (int)WIDGET_ERROR_NONE) {
+
+                               /*!
+                                * \note
+                                * But the created event has to be send afte return
+                                * from this function or the viewer couldn't care
+                                * the event correctly.
+                                */
+                               inst = instance_ref(inst); /* To guarantee the inst */
+
+                               /*!
+                                * \note
+                                * At here, we don't need to rememeber the timer object.
+                                * Even if the timer callback is called, after the instance is destroyed.
+                                * lazy_gbar_created_cb will decrease the instance refcnt first.
+                                * At that time, if the instance is released, the timer callback will do nothing.
+                                *
+                                * 13-05-28
+                                * I change my mind. There is no requirements to keep the timer handler.
+                                * But I just add it to the tagged-data of the instance.
+                                * Just reserve for future-use.
+                                */
+                               gbar_monitor = ecore_timer_add(DELAY_TIME, lazy_gbar_created_cb, inst);
+                               if (!gbar_monitor) {
+                                       ret = script_handler_unload(instance_gbar_script(inst), 1);
+                                       ErrPrint("Unload script: %d\n", ret);
+
+                                       ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+                                       ErrPrint("Close GBAR %d\n", ret);
+
+                                       inst = instance_unref(inst);
+                                       if (!inst) {
+                                               DbgPrint("Instance destroyed\n");
+                                       }
+
+                                       ErrPrint("Instance: %s\n", pkgname);
+
+                                       ret = WIDGET_ERROR_FAULT;
+                               } else {
+                                       struct slave_node *slave;
+
+                                       (void)instance_set_data(inst, LAZY_GBAR_OPEN_TAG, gbar_monitor);
+
+                                       slave = package_slave(pkg);
+                                       if (!slave) {
+                                               ErrPrint("Failed to get slave: %s\n", pkgname);
+                                               goto out;
+                                       }
+
+                                       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_script_cb, inst) != WIDGET_ERROR_NONE) {
+                                               ErrPrint("Failed to add fault callback: %s\n", pkgname);
+                                       }
+                               }
+                       } else {
+                               int tmp_ret;
+                               tmp_ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_FAULT);
+                               if (tmp_ret < 0) {
+                                       ErrPrint("Unable to load script: %d, (close: %d)\n", ret, tmp_ret);
+                               }
+                       }
+               } else {
+                       ErrPrint("Unable open GBAR(%s): %d\n", pkgname, ret);
+               }
+       } else {
+               ErrPrint("Invalid GBAR TYPE\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_destroy_gbar(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       Ecore_Timer *gbar_monitor;
+       struct slave_node *slave;
+
+       DbgPrint("PERF_WIDGET\n");
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       slave = package_slave(pkg);
+       if (!slave) {
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (instance_gbar_owner(inst) != client) {
+               if (instance_gbar_owner(inst) == NULL) {
+                       ErrPrint("GBAR looks already closed\n");
+                       ret = WIDGET_ERROR_ALREADY_STARTED;
+               } else {
+                       ErrPrint("GBAR owner mimatched\n");
+                       ret = WIDGET_ERROR_PERMISSION_DENIED;
+               }
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               DbgPrint("Buffer type GBAR\n");
+               gbar_monitor = instance_del_data(inst, GBAR_OPEN_MONITOR_TAG);
+               if (gbar_monitor) {
+                       ErrPrint("GBAR Open request is found. cancel it [%s]\n", pkgname);
+
+                       if (slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst) < 0) {
+                               DbgPrint("Failed to delete a deactivate callback\n");
+                       }
+
+                       /*!
+                        * \note
+                        * We should return negative value
+                        * Or we have to send "destroyed" event to the client.
+                        * If we didn't send destroyed event after return SUCCESS from here,
+                        * The client will permanently waiting destroyed event.
+                        * Because they understand that the destroy request is successfully processed.
+                        */
+                       ret = instance_client_gbar_created(inst, WIDGET_ERROR_CANCELED);
+                       if (ret < 0) {
+                               ErrPrint("GBAR client create event: %d\n", ret);
+                       }
+
+                       ret = instance_client_gbar_destroyed(inst, WIDGET_ERROR_NONE);
+                       if (ret < 0) {
+                               ErrPrint("GBAR client destroy event: %d\n", ret);
+                       }
+
+                       ret = instance_signal_emit(inst, "gbar,hide", instance_id(inst), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
+                       if (ret < 0) {
+                               ErrPrint("GBAR close signal emit failed: %d\n", ret);
+                       }
+
+                       ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+                       if (ret < 0) {
+                               ErrPrint("GBAR close request failed: %d\n", ret);
+                       }
+
+                       ecore_timer_del(gbar_monitor);
+                       inst = instance_unref(inst);
+                       if (!inst) {
+                               DbgPrint("Instance is deleted\n");
+                       }
+               } else if (instance_get_data(inst, LAZY_GBAR_CLOSE_TAG) || instance_get_data(inst, GBAR_CLOSE_MONITOR_TAG)) {
+                       DbgPrint("Close monitor is already fired\n");
+                       ret = WIDGET_ERROR_ALREADY_STARTED;
+               } else {
+                       int resize_aborted = 0;
+
+                       gbar_monitor = instance_del_data(inst, GBAR_RESIZE_MONITOR_TAG);
+                       if (gbar_monitor) {
+                               ErrPrint("GBAR Resize request is found. clear it [%s]\n", pkgname);
+                               if (slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst) < 0) {
+                                       DbgPrint("Failed to delete a deactivate callback\n");
+                               }
+
+                               ecore_timer_del(gbar_monitor);
+
+                               inst = instance_unref(inst);
+                               if (!inst) {
+                                       DbgPrint("Instance is destroyed while resizing\n");
+                                       goto out;
+                               }
+
+                               resize_aborted = 1;
+                       }
+
+                       ret = instance_signal_emit(inst, "gbar,hide", instance_id(inst), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
+                       if (ret < 0) {
+                               ErrPrint("GBAR close signal emit failed: %d\n", ret);
+                       }
+
+                       ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+                       if (ret < 0) {
+                               ErrPrint("GBAR close request failed: %d\n", ret);
+                       } else if (resize_aborted) {
+                               gbar_monitor = ecore_timer_add(DELAY_TIME, lazy_gbar_destroyed_cb, instance_ref(inst));
+                               if (!gbar_monitor) {
+                                       ErrPrint("Failed to create a timer: %s\n", pkgname);
+                                       inst = instance_unref(inst);
+                                       if (!inst) {
+                                               DbgPrint("Instance is deleted\n");
+                                       }
+                               } else {
+                                       DbgPrint("Resize is aborted\n");
+                                       (void)instance_set_data(inst, LAZY_GBAR_CLOSE_TAG, gbar_monitor);
+                                       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst) < 0) {
+                                               ErrPrint("Failed to add a slave event callback\n");
+                                       }
+                               }
+                       } else {
+                               gbar_monitor = ecore_timer_add(WIDGET_CONF_GBAR_REQUEST_TIMEOUT, gbar_close_monitor_cb, instance_ref(inst));
+                               if (!gbar_monitor) {
+                                       ErrPrint("Failed to add pd close monitor\n");
+                                       inst = instance_unref(inst);
+                                       if (!inst) {
+                                               ErrPrint("Instance is deleted while closing GBAR\n");
+                                       }
+                               } else {
+                                       DbgPrint("Add close monitor\n");
+                                       (void)instance_set_data(inst, GBAR_CLOSE_MONITOR_TAG, gbar_monitor);
+                                       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst) < 0) {
+                                               ErrPrint("Failed to add SLAVE EVENT callback\n");
+                                       }
+                               }
+                       }
+
+                       /*!
+                        * \note
+                        * release_buffer will be called by the slave after this routine.
+                        * It will send the "gbar_destroyed" event to the client
+                        *
+                        * instance_client_gbar_destroyed(inst, WIDGET_ERROR_NONE);
+                        *
+                        * Or the "gbar_close_monitor_cb" or "lazy_gbar_destroyed_cb" will be called.
+                        */
+               }
+       } else if (package_gbar_type(pkg) == GBAR_TYPE_SCRIPT) {
+               DbgPrint("Script TYPE GBAR\n");
+               gbar_monitor = instance_get_data(inst, LAZY_GBAR_OPEN_TAG);
+               if (gbar_monitor) {
+                       ecore_timer_del(gbar_monitor);
+                       (void)lazy_gbar_created_cb(inst);
+               }
+
+               ret = script_handler_unload(instance_gbar_script(inst), 1);
+               if (ret < 0) {
+                       ErrPrint("Unable to unload the script: %s, %d\n", pkgname, ret);
+               }
+
+               /*!
+                * \note
+                * Send request to the slave.
+                * The SLAVE must has to repsonse this via "release_buffer" method.
+                */
+               ret = instance_slave_close_gbar(inst, client, WIDGET_CLOSE_GBAR_NORMAL);
+               if (ret < 0) {
+                       ErrPrint("Unable to close the GBAR: %s, %d\n", pkgname, ret);
+               }
+
+               /*!
+                * \note
+                * Send the destroyed GBAR event to the client
+                */
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       /*!
+                        * \note
+                        * 13-05-28
+                        * I've changed my mind. There is no requirements to keep the timer handler.
+                        * But I just add it to the tagged-data of the instance.
+                        * Just reserve for future-use.
+                        */
+                       DbgPrint("Add lazy GBAR destroy timer\n");
+                       gbar_monitor = ecore_timer_add(DELAY_TIME, lazy_gbar_destroyed_cb, instance_ref(inst));
+                       if (!gbar_monitor) {
+                               ErrPrint("Failed to create a timer: %s\n", pkgname);
+                               inst = instance_unref(inst);
+                               if (!inst) {
+                                       DbgPrint("instance is deleted\n");
+                               }
+                       } else {
+                               (void)instance_set_data(inst, LAZY_GBAR_CLOSE_TAG, gbar_monitor);
+                               if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_script_cb, inst) < 0) {
+                                       ErrPrint("Failed to add a event callback for slave\n");
+                               }
+                       }
+               }
+       } else {
+               ErrPrint("Invalid GBAR TYPE\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_activate_package(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, ret */
+{
+       struct client_node *client;
+       struct packet *result;
+       const char *pkgname;
+       int ret;
+       struct pkg_info *info;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               pkgname = "";
+               goto out;
+       }
+
+       ret = packet_get(packet, "s", &pkgname);
+       if (ret != 1) {
+               ErrPrint("Parameter is not matched\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               pkgname = "";
+               goto out;
+       }
+
+       DbgPrint("pid[%d] pkgname[%s]\n", pid, pkgname);
+
+       /*!
+        * \NOTE:
+        * Validate the widget package name.
+        */
+       if (!package_is_widget_pkgname(pkgname)) {
+               ErrPrint("%s is not a valid widget package\n", pkgname);
+               pkgname = "";
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       info = package_find(pkgname);
+       if (!info) {
+               ret = WIDGET_ERROR_NOT_EXIST;
+       } else {
+               ret = package_clear_fault(info);
+       }
+
+out:
+       result = packet_create_reply(packet, "is", ret, pkgname);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_subscribed_group(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *cluster;
+       const char *category;
+       struct client_node *client;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &cluster, &category);
+       if (ret != 2) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
+       if (!strlen(cluster) || !strcasecmp(cluster, WIDGET_CONF_DEFAULT_CLUSTER)) {
+               ErrPrint("Invalid cluster name\n");
+               goto out;
+       }
+
+       /*!
+        * \todo
+        * SUBSCRIBE cluster & sub-cluster for a client.
+        */
+       ret = client_subscribe_group(client, cluster, category);
+       if (ret == 0) {
+               package_alter_instances_to_client(client, ALTER_CREATE);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_delete_cluster(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *cluster;
+       struct client_node *client;
+       struct packet *result;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "s", &cluster);
+       if (ret != 1) {
+               ErrPrint("Invalid parameters\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("pid[%d] cluster[%s]\n", pid, cluster);
+
+       if (!strlen(cluster) || !strcasecmp(cluster, WIDGET_CONF_DEFAULT_CLUSTER)) {
+               ErrPrint("Invalid cluster: %s\n", cluster);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       /*!
+        * \todo
+        */
+       ret = WIDGET_ERROR_NOT_SUPPORTED;
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static inline int update_pkg_cb(struct category *category, const char *pkgname, int force)
+{
+       const char *c_name;
+       const char *s_name;
+
+       c_name = group_cluster_name_by_category(category);
+       s_name = group_category_name(category);
+
+       if (!c_name || !s_name || !pkgname) {
+               ErrPrint("Name is not valid\n");
+               return EXIT_FAILURE;
+       }
+
+       DbgPrint("Send refresh request: %s (%s/%s)\n", pkgname, c_name, s_name);
+       slave_rpc_request_update(pkgname, "", c_name, s_name, NULL, force);
+
+       /* Just try to create a new package */
+       if (util_free_space(WIDGET_CONF_IMAGE_PATH) > WIDGET_CONF_MINIMUM_SPACE) {
+               double timestamp;
+               struct inst_info *inst;
+
+               timestamp = util_timestamp();
+               /*!
+                * \NOTE
+                * Don't need to check the subscribed clients.
+                * Because this callback is called by the requests of clients.
+                * It means. some clients wants to handle this instances ;)
+                */
+               inst = instance_create(NULL, timestamp, pkgname, "", c_name, s_name, WIDGET_CONF_DEFAULT_PERIOD, 0, 0);
+               if (!inst) {
+                       ErrPrint("Failed to create a new instance\n");
+               }
+       }
+
+       return EXIT_SUCCESS;
+}
+
+static struct packet *client_update(pid_t pid, int handle, const struct packet *packet)
+{
+       struct inst_info *inst;
+       struct client_node *client;
+       const char *pkgname;
+       const char *id;
+       int force;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Cilent %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &force);
+       if (ret != 3) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_client(inst) != client) {
+               /* PERMISSIONS */
+               ErrPrint("Insufficient permissions [%s] - %d\n", pkgname, pid);
+       } else {
+               slave_rpc_request_update(pkgname, id, instance_cluster(inst), instance_category(inst), NULL, force);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_refresh_group(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *cluster_id;
+       const char *category_id;
+       struct client_node *client;
+       int ret;
+       struct cluster *cluster;
+       struct category *category;
+       struct context_info *info;
+       Eina_List *info_list;
+       Eina_List *l;
+       int force;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Cilent %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &cluster_id, &category_id, &force);
+       if (ret != 3) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster_id, category_id);
+
+       if (!strlen(cluster_id) || !strcasecmp(cluster_id, WIDGET_CONF_DEFAULT_CLUSTER)) {
+               ErrPrint("Invalid cluster name: %s\n", cluster_id);
+               goto out;
+       }
+
+       cluster = group_find_cluster(cluster_id);
+       if (!cluster) {
+               ErrPrint("Cluster [%s] is not registered\n", cluster_id);
+               goto out;
+       }
+
+       category = group_find_category(cluster, category_id);
+       if (!category) {
+               ErrPrint("Category [%s] is not registered\n", category_id);
+               goto out;
+       }
+
+       info_list = group_context_info_list(category);
+       EINA_LIST_FOREACH(info_list, l, info) {
+               update_pkg_cb(category, group_pkgname_from_context_info(info), force);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_delete_category(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *cluster;
+       const char *category;
+       struct client_node *client;
+       struct packet *result;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &cluster, &category);
+       if (ret != 2) {
+               ErrPrint("Invalid paramenters\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("pid[%d] cluster[%s] category[%s]\n", pid, cluster, category);
+       if (!strlen(cluster) || !strcasecmp(cluster, WIDGET_CONF_DEFAULT_CLUSTER)) {
+               ErrPrint("Invalid cluster: %s\n", cluster);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       /*!
+        * \todo
+        */
+       ret = WIDGET_ERROR_NOT_SUPPORTED;
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *client_unsubscribed_group(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *cluster;
+       const char *category;
+       struct client_node *client;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &cluster, &category);
+       if (ret != 2) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
+
+       if (!strlen(cluster) || !strcasecmp(cluster, WIDGET_CONF_DEFAULT_CLUSTER)) {
+               ErrPrint("Invalid cluster name: %s\n", cluster);
+               goto out;
+       }
+
+       /*!
+        * \todo
+        * UNSUBSCRIBE cluster & sub-cluster for a client.
+        */
+       ret = client_unsubscribe_group(client, cluster, category);
+       if (ret == 0) {
+               package_alter_instances_to_client(client, ALTER_DESTROY);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_subscribed_category(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *category;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "s", &category);
+       if (ret != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("[%d] category[%s]\n", pid, category);
+       if (!strlen(category)) {
+               ErrPrint("Invalid category name: %s\n", category);
+               goto out;
+       }
+
+       /*!
+        * \TODO
+        * 1. Get a list of created widget instances which are categorized by given "category"
+        * 2. Send created events to the client.
+        * 3. Add this client to "client_only_view_list"
+        */
+       if (client_subscribe_category(client, category) == WIDGET_ERROR_NONE) {
+               package_alter_instances_to_client(client, ALTER_CREATE);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static struct packet *client_unsubscribed_category(pid_t pid, int handle, const struct packet *packet)
+{
+       struct client_node *client;
+       const char *category;
+       int ret;
+
+       client = client_find_by_rpc_handle(handle);
+       if (!client) {
+               ErrPrint("Client %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "s", &category);
+       if (ret != 1) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       DbgPrint("[%d] category[%s]\n", pid, category);
+       if (!strlen(category)) {
+               ErrPrint("Invalid category name: %s\n", category);
+               goto out;
+       }
+
+       /*!
+        * \TODO
+        * 0. Is this client subscribed to given "category"?
+        * 1. Get a list of created widget instances
+        * 2. and then send destroyed event to this client.
+        * 3. Remove this client from the "client_only_view_list"
+        */
+       if (client_unsubscribe_category(client, category) == WIDGET_ERROR_NONE) {
+               package_alter_instances_to_client(client, ALTER_DESTROY);
+       }
+
+out:
+       /*! \note No reply packet */
+       return NULL;
+}
+
+static inline __attribute__((always_inline)) struct slave_node *debug_mode_enabled(int pid, const char *slavename, const char *pkgname, int secured, const char *abi, const char *acceleration)
+{
+       struct slave_node *slave;
+
+       slave = slave_find_by_pkgname(pkgname);
+       if (!slave) {
+               slave = slave_create(slavename, secured, abi, pkgname, 0, acceleration, 0);
+               if (!slave) {
+                       return NULL;
+               }
+
+               DbgPrint("New slave is created net(%d) abi(%s) secured(%d) accel(%s)\n", 0, abi, secured, acceleration);
+       } else {
+               DbgPrint("Registered slave is replaced with this new one\n");
+       }
+
+       slave_set_pid(slave, pid);
+       slave_set_valid(slave);
+
+       DbgPrint("Provider is forcely activated, pkgname(%s), abi(%s), slavename(%s)\n", pkgname, abi, slavename);
+       return slave;
+}
+
+static struct packet *slave_hello(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
+{
+       char pkgname[pathconf("/", _PC_PATH_MAX)];
+       struct slave_node *slave;
+       const char *acceleration;
+       const char *slavename;
+       const char *abi;
+       int secured;
+       int ret;
+
+       ret = packet_get(packet, "isss", &secured, &slavename, &acceleration, &abi);
+       if (ret != 4) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       if (acceleration[0] == '\0') {
+               acceleration = NULL;
+       }
+
+       DbgPrint("New slave[%s](%d) is arrived\n", slavename, pid);
+
+       slave = slave_find_by_name(slavename);
+       if (!slave) { /* Try again to find a slave using pid */
+               slave = slave_find_by_pid(pid);
+       }
+
+       if (aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname)) != AUL_R_OK) {
+               ErrPrint("pid[%d] is not authroized provider package, try to find it using its name[%s]\n", pid, slavename);
+               goto out;
+       }
+
+       if (!slave) {
+               if (WIDGET_CONF_DEBUG_MODE || g_conf.debug_mode) {
+                       slave = debug_mode_enabled(pid, slavename, pkgname, secured, abi, acceleration);
+                       if (!slave) {
+                               ErrPrint("Failed to create a new slave for %s\n", slavename);
+                               goto out;
+                       }
+               } else {
+                       ErrPrint("Request comes from unknown: %d\n", pid);
+                       ret = aul_terminate_pid_async(pid);
+                       DbgPrint("Terminate %d (%d)\n", pid, ret);
+                       goto out;
+               }
+       } else {
+               if (slave_pid(slave) != pid) {
+                       if (slave_pid(slave) > 0) {
+                               CRITICAL_LOG("Slave(%s) is already assigned to %d\n", slave_name(slave), slave_pid(slave));
+                               if (pid > 0) {
+                                       ret = aul_terminate_pid_async(pid);
+                                       CRITICAL_LOG("Terminate %d (ret: %d)\n", pid, ret);
+                               }
+                               goto out;
+                       }
+                       CRITICAL_LOG("PID of slave(%s) is updated (%d -> %d)\n", slave_name(slave), slave_pid(slave), pid);
+                       slave_set_pid(slave, pid);
+               }
+
+               if (!slave_valid(slave)) {
+                       char *widget_id;
+
+                       widget_id = is_valid_slave(pid, abi, pkgname);
+                       if (widget_id) {
+                               slave_set_valid(slave);
+                               DbgFree(widget_id);
+                       } else {
+                               ErrPrint("slave is not valid (%s)\n", pkgname);
+                               goto out;
+                       }
+               }
+       }
+
+       /*!
+        * \note
+        * After updating handle,
+        * slave activated callback will be called.
+        */
+       slave_rpc_update_handle(slave, handle, 0);
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_ctrl(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       int ctrl;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "i", &ctrl);
+       if (ret != 1) {
+               ErrPrint("Parameter is not matched\n");
+       } else {
+               slave_set_control_option(slave, ctrl);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_ping(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
+{
+       struct slave_node *slave;
+       const char *slavename;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "s", &slavename);
+       if (ret != 1) {
+               ErrPrint("Parameter is not matched\n");
+       } else {
+               slave_rpc_ping(slave);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_faulted(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct pkg_info *pkg;
+       const char *pkgname;
+       const char *id;
+       const char *func;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sss", &pkgname, &id, &func);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = fault_info_set(slave, pkgname, id, func);
+       DbgPrint("Slave Faulted: %s (%d)\n", slave_name(slave), ret);
+
+       pkg = package_find(pkgname);
+       if (!pkg) {
+               ErrPrint("There is no package info found: %s\n", pkgname);
+       } else {
+               package_faulted(pkg, 0);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_widget_update_begin(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       const char *pkgname;
+       const char *id;
+       double priority;
+       const char *content;
+       const char *title;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssdss", &pkgname, &id, &priority, &content, &title);
+       if (ret != 5) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = instance_widget_update_begin(inst, priority, content, title);
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       slave_freeze_ttl(slave);
+               }
+       } else {
+               ErrPrint("Invalid request[%s]\n", id);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_widget_update_end(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       const char *pkgname;
+       const char *id;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               ret = instance_widget_update_end(inst);
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       slave_thaw_ttl(slave);
+               }
+       } else {
+               ErrPrint("Invalid request[%s]\n", id);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_gbar_update_begin(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       const struct pkg_info *pkg;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               (void)instance_gbar_update_begin(inst);
+       } else {
+               ErrPrint("Invalid request[%s]\n", id);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_key_status(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int status;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &status);
+       if (ret != 3) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               } else {
+                       (void)instance_forward_packet(inst, packet_ref((struct packet *)packet));
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_access_status(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int status;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &status);
+       if (ret != 3) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               } else {
+                       (void)instance_forward_packet(inst, packet_ref((struct packet *)packet));
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_close_gbar(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int status;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &status);
+       if (ret != 3) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               } else {
+                       (void)instance_forward_packet(inst, packet_ref((struct packet *)packet));
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_gbar_update_end(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       const struct pkg_info *pkg;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Invalid parameters\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               (void)instance_gbar_update_end(inst);
+       } else {
+               ErrPrint("Invalid request[%s]\n", id);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_call(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       const char *func;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sss", &pkgname, &id, &func);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = fault_func_call(slave, pkgname, id, func);
+       slave_give_more_ttl(slave);
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_ret(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       const char *func;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sss", &pkgname, &id, &func);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = fault_func_ret(slave, pkgname, id, func);
+       slave_give_more_ttl(slave);
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_extra_info(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       const char *content_info;
+       const char *title;
+       const char *icon;
+       const char *name;
+       double priority;
+       int ret;
+       struct inst_info *inst;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssssssd", &pkgname, &id, &content_info, &title, &icon, &name, &priority);
+       if (ret != 7) {
+               ErrPrint("Parameter is not matchd\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+                       goto out;
+               }
+
+               instance_set_widget_info(inst, priority, content_info, title);
+               instance_set_alt_info(inst, icon, name);
+               instance_extra_info_updated_by_instance(inst);
+               slave_give_more_ttl(slave);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, width, height, ret */
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *safe_filename;
+       const char *id;
+       int w;
+       int h;
+       int x;
+       int y;
+       int ret;
+       struct inst_info *inst;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sssiiii", &pkgname, &id, &safe_filename, &x, &y, &w, &h);
+       if (ret != 7) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+                       goto out;
+               }
+
+               switch (package_widget_type(instance_package(inst))) {
+               case WIDGET_TYPE_SCRIPT:
+                       script_handler_resize(instance_widget_script(inst), w, h);
+                       if (safe_filename) {
+                               (void)script_handler_parse_desc(inst, safe_filename, 0);
+                       } else {
+                               safe_filename = widget_util_uri_to_path(id);
+                               (void)script_handler_parse_desc(inst, safe_filename, 0);
+                       }
+
+                       if (unlink(safe_filename) < 0) {
+                               ErrPrint("unlink: %d - %s\n", errno, safe_filename);
+                       }
+                       break;
+               case WIDGET_TYPE_BUFFER:
+               default:
+                       /*!
+                        * \check
+                        * text format (inst)
+                        */
+                       instance_widget_updated_by_instance(inst, safe_filename, x, y, w, h);
+                       break;
+               }
+
+               slave_give_more_ttl(slave);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_hold_scroll(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const char *pkgname;
+       const char *id;
+       int seize;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
+       if (ret != 3) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               if (instance_state(inst) == INST_DESTROYED) {
+                       ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               } else {
+                       (void)instance_hold_scroll(inst, seize);
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_extra_updated(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       int idx;
+       int x;
+       int y;
+       int w;
+       int h;
+       int ret;
+       int is_gbar;
+       struct inst_info *inst;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ssiiiiii", &pkgname, &id, &is_gbar, &idx, &x, &y, &w, &h);
+       if (ret != 8) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       (void)instance_extra_updated_by_instance(inst, is_gbar, idx, x, y, w, h);
+out:
+       return NULL;
+}
+
+static struct packet *slave_desc_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, decsfile, ret */
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       const char *descfile;
+       int x;
+       int y;
+       int w;
+       int h;
+       int ret;
+       struct inst_info *inst;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "sssiiii", &pkgname, &id, &descfile, &x, &y, &w, &h);
+       if (ret != 7) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       switch (package_gbar_type(instance_package(inst))) {
+       case GBAR_TYPE_SCRIPT:
+               DbgPrint("%s updated (%s)\n", instance_id(inst), descfile);
+               if (script_handler_is_loaded(instance_gbar_script(inst))) {
+                       (void)script_handler_parse_desc(inst, descfile, 1);
+               }
+               break;
+       case GBAR_TYPE_TEXT:
+               instance_set_gbar_size(inst, 0, 0);
+       case GBAR_TYPE_BUFFER:
+               instance_gbar_updated(pkgname, id, descfile, x, y, w, h);
+               break;
+       default:
+               DbgPrint("Ignore updated DESC(%s)\n", pkgname);
+               break;
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *slave_deleted(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, id, ret */
+{
+       struct slave_node *slave;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       struct inst_info *inst;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               goto out;
+       }
+
+       ret = packet_get(packet, "ss", &pkgname, &id);
+       if (ret != 2) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, NULL);
+       if (ret == (int)WIDGET_ERROR_NONE) {
+               ret = instance_destroyed(inst, WIDGET_ERROR_NONE);
+       }
+
+out:
+       return NULL;
+}
+
+/*!
+ * \note for the BUFFER Type slave
+ */
+static struct packet *slave_acquire_buffer(pid_t pid, int handle, const struct packet *packet) /* type, id, w, h, size */
+{
+       enum target_type target;
+       const char *pkgname;
+       const char *id;
+       int w;
+       int h;
+       int pixel_size;
+       struct packet *result;
+       struct slave_node *slave;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Failed to find a slave\n");
+               id = "";
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       ret = packet_get(packet, "issiii", &target, &pkgname, &id, &w, &h, &pixel_size);
+       if (ret != 6) {
+               ErrPrint("Invalid argument\n");
+               id = "";
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       id = "";
+
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_INVALID_PARAMETER;
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", package_name(pkg));
+               goto out;
+       }
+
+       if (target == TYPE_WIDGET && package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_widget_buffer(inst);
+               if (!info) {
+                       if (!instance_create_widget_buffer(inst, pixel_size)) {
+                               ErrPrint("Failed to create a WIDGET buffer\n");
+                               ret = WIDGET_ERROR_FAULT;
+                               goto out;
+                       }
+
+                       info = instance_widget_buffer(inst);
+                       if (!info) {
+                               ErrPrint("WIDGET buffer is not valid\n");
+                               /*!
+                                * \NOTE
+                                * ret value should not be changed.
+                                */
+                               goto out;
+                       }
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               ret = buffer_handler_load(info);
+               if (ret == 0) {
+                       instance_set_widget_size(inst, w, h);
+                       instance_set_widget_info(inst, WIDGET_CONF_PRIORITY_NO_CHANGE, WIDGET_CONF_CONTENT_NO_CHANGE, WIDGET_CONF_TITLE_NO_CHANGE);
+                       id = buffer_handler_id(info);
+               } else {
+                       ErrPrint("Failed to load a buffer(%d)\n", ret);
+               }
+       } else if (target == TYPE_GBAR && package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               struct buffer_info *info;
+               Ecore_Timer *gbar_monitor;
+               int is_resize;
+
+               is_resize = 0;
+               gbar_monitor = instance_del_data(inst, GBAR_OPEN_MONITOR_TAG);
+               if (!gbar_monitor) {
+                       gbar_monitor = instance_del_data(inst, GBAR_RESIZE_MONITOR_TAG);
+                       is_resize = !!gbar_monitor;
+                       if (!is_resize) {
+                               /* Invalid request. Reject this */
+                               ErrPrint("Invalid request\n");
+                               goto out;
+                       }
+
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst);
+               } else {
+                       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst);
+               }
+
+               ecore_timer_del(gbar_monitor);
+               inst = instance_unref(inst);
+               if (!inst) {
+                       ErrPrint("Instance refcnt is ZERO: %s\n", package_name(pkg));
+                       goto out;
+               }
+
+               info = instance_gbar_buffer(inst);
+               if (!info) {
+                       if (!instance_create_gbar_buffer(inst, pixel_size)) {
+                               ErrPrint("Failed to create a GBAR buffer\n");
+                               ret = WIDGET_ERROR_FAULT;
+                               instance_client_gbar_created(inst, ret);
+                               goto out;
+                       }
+
+                       info = instance_gbar_buffer(inst);
+                       if (!info) {
+                               ErrPrint("GBAR buffer is not valid\n");
+                               /*!
+                                * \NOTE
+                                * ret value should not be changed.
+                                */
+                               instance_client_gbar_created(inst, ret);
+                               goto out;
+                       }
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               ret = buffer_handler_load(info);
+               if (ret == 0) {
+                       instance_set_gbar_size(inst, w, h);
+                       id = buffer_handler_id(info);
+               } else {
+                       ErrPrint("Failed to load a buffer (%d)\n", ret);
+               }
+
+               /*!
+                * Send the GBAR created event to the client
+                */
+               if (!is_resize) {
+                       instance_client_gbar_created(inst, ret);
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "is", ret, id);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *slave_acquire_extra_buffer(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct inst_info *inst;
+       struct packet *result;
+       const struct pkg_info *pkg;
+       const char *pkgname;
+       const char *id;
+       int pixel_size;
+       int target;
+       int idx;
+       int ret;
+       int w;
+       int h;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               id = "";
+               goto out;
+       }
+
+       ret = packet_get(packet, "issiiii", &target, &pkgname, &id, &w, &h, &pixel_size, &idx);
+       if (ret != 7) {
+               ErrPrint("Invalid parameters\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               id = "";
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       id = "";
+
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_INVALID_PARAMETER;
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (target == TYPE_WIDGET && package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_widget_extra_buffer(inst, idx);
+               if (!info) {
+                       if (!instance_create_widget_extra_buffer(inst, pixel_size, idx)) {
+                               ErrPrint("Failed to create a WIDGET buffer\n");
+                               ret = WIDGET_ERROR_FAULT;
+                               goto out;
+                       }
+
+                       info = instance_widget_extra_buffer(inst, idx);
+                       if (!info) {
+                               ErrPrint("WIDGET extra buffer is not valid\n");
+                               /*!
+                                * \NOTE
+                                * ret value should not be changed.
+                                */
+                               goto out;
+                       }
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               ret = buffer_handler_load(info);
+               if (ret == 0) {
+                       /**
+                        * @todo
+                        * Send the extra buffer info to the viewer.
+                        * Then the viewer will try to acquire extra pixmap(aka, resource id) information
+                        */
+                       id = buffer_handler_id(info);
+                       DbgPrint("Extra buffer is loaded: %s\n", id);
+                       (void)instance_client_widget_extra_buffer_created(inst, idx);
+               } else {
+                       ErrPrint("Failed to load a buffer(%d)\n", ret);
+               }
+       } else if (target == TYPE_GBAR && package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_gbar_extra_buffer(inst, idx);
+               if (!info) {
+                       if (!instance_create_gbar_extra_buffer(inst, pixel_size, idx)) {
+                               ErrPrint("Failed to create a GBAR buffer\n");
+                               ret = WIDGET_ERROR_FAULT;
+                               goto out;
+                       }
+
+                       info = instance_gbar_extra_buffer(inst, idx);
+                       if (!info) {
+                               ErrPrint("GBAR buffer is not valid\n");
+                               /*!
+                                * \NOTE
+                                * ret value should not be changed.
+                                */
+                               goto out;
+                       }
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               ret = buffer_handler_load(info);
+               if (ret == 0) {
+                       id = buffer_handler_id(info);
+                       /**
+                        * @todo
+                        * Send the extra buffer acquired event to the viewer
+                        */
+                       DbgPrint("Extra buffer is loaded: %s\n", id);
+                       (void)instance_client_gbar_extra_buffer_created(inst, idx);
+               } else {
+                       ErrPrint("Failed to load a buffer (%d)\n", ret);
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "is", ret, id);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *slave_resize_buffer(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct packet *result;
+       enum target_type type;
+       const char *pkgname;
+       const char *id;
+       int w;
+       int h;
+       struct inst_info *inst = NULL;
+       const struct pkg_info *pkg = NULL;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Failed to find a slave\n");
+               ret = WIDGET_ERROR_NOT_EXIST;
+               id = "";
+               goto out;
+       }
+
+       ret = packet_get(packet, "issii", &type, &pkgname, &id, &w, &h);
+       if (ret != 5) {
+               ErrPrint("Invalid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               id = "";
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       id = "";
+
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_INVALID_PARAMETER;
+       /*!
+        * \note
+        * Reset "id", It will be re-used from here
+        */
+
+       if (instance_state(inst) == INST_DESTROYED) {
+               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+               goto out;
+       }
+
+       if (type == TYPE_WIDGET && package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_widget_buffer(inst);
+               if (!info) {
+                       goto out;
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               /*!
+                * \note
+                * id is resued for newly assigned ID
+                */
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       id = buffer_handler_id(info);
+                       instance_set_widget_size(inst, w, h);
+                       instance_set_widget_info(inst, WIDGET_CONF_PRIORITY_NO_CHANGE, WIDGET_CONF_CONTENT_NO_CHANGE, WIDGET_CONF_TITLE_NO_CHANGE);
+               }
+       } else if (type == TYPE_GBAR && package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_gbar_buffer(inst);
+               if (!info) {
+                       goto out;
+               }
+
+               ret = buffer_handler_resize(info, w, h);
+               /*!
+                * \note
+                * id is resued for newly assigned ID
+                */
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       id = buffer_handler_id(info);
+                       instance_set_gbar_size(inst, w, h);
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "is", ret, id);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *slave_release_buffer(pid_t pid, int handle, const struct packet *packet)
+{
+       enum target_type type;
+       const char *pkgname;
+       const char *id;
+       struct packet *result;
+       struct slave_node *slave;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+       int ret;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Failed to find a slave\n");
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       if (packet_get(packet, "iss", &type, &pkgname, &id) != 3) {
+               ErrPrint("Inavlid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_INVALID_PARAMETER;
+
+       if (type == TYPE_WIDGET && package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               struct buffer_info *info;
+
+               info = instance_widget_buffer(inst);
+               ret = buffer_handler_unload(info);
+       } else if (type == TYPE_GBAR && package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               struct buffer_info *info;
+               Ecore_Timer *gbar_monitor;
+
+               gbar_monitor = instance_del_data(inst, GBAR_CLOSE_MONITOR_TAG);
+               if (!gbar_monitor && !package_is_fault(pkg)) {
+                       ErrPrint("Slave requests to release a buffer\n");
+                       /**
+                        * @note
+                        * In this case just keep going to release buffer,
+                        * Even if a user(client) doesn't wants to destroy the GBAR.
+                        *
+                        * If the slave tries to destroy GBAR buffer, it should be
+                        * released and reported to the client about its status.
+                        *
+                        * Even if the pd is destroyed by timeout handler,
+                        * instance_client_gbar_destroyed function will be ignored
+                        * by pd.need_to_send_close_event flag.
+                        * which will be checked by instance_client_gbar_destroyed function.
+                        */
+
+                       /**
+                        * @note
+                        * provider can try to resize the buffer size.
+                        * in that case, it will release the buffer first.
+                        * Then even though the client doesn't request to close the GBAR,
+                        * the provider can release it.
+                        * If we send the close event to the client,
+                        * The client will not able to allocate GBAR again.
+                        * In this case, add the pd,monitor again. from here.
+                        * to wait the re-allocate buffer.
+                        * If the client doesn't request buffer reallocation,
+                        * Treat it as a fault. and close the GBAR.
+                        */
+                       info = instance_gbar_buffer(inst);
+                       ret = buffer_handler_unload(info);
+
+                       if (ret == (int)WIDGET_ERROR_NONE) {
+                               gbar_monitor = ecore_timer_add(WIDGET_CONF_GBAR_REQUEST_TIMEOUT, gbar_resize_monitor_cb, instance_ref(inst));
+                               if (!gbar_monitor) {
+                                       ErrPrint("Failed to create a timer for GBAR Open monitor\n");
+                                       inst = instance_unref(inst);
+                                       if (!inst) {
+                                               DbgPrint("Instance is deleted\n");
+                                       }
+                               } else {
+                                       (void)instance_set_data(inst, GBAR_RESIZE_MONITOR_TAG, gbar_monitor);
+                                       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst) != WIDGET_ERROR_NONE) {
+                                               ErrPrint("Failed to add event handler: %s\n", pkgname);
+                                       }
+                               }
+                       }
+               } else {
+                       if (gbar_monitor) {
+                               /**
+                                * @note
+                                * If the instance has gbar_monitor, the pd close requested from client via client_destroy_gbar.
+                                */
+                               slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
+                               ecore_timer_del(gbar_monitor);
+
+                               inst = instance_unref(inst);
+                               if (!inst) {
+                                       ErrPrint("Instance is released: %s\n", pkgname);
+                                       ret = WIDGET_ERROR_FAULT;
+                                       goto out;
+                               }
+                       } /* else {
+                                @note
+                                This case means that the package is faulted so the service provider tries to release the buffer
+                          */
+
+                       info = instance_gbar_buffer(inst);
+                       ret = buffer_handler_unload(info);
+
+                       /**
+                        * @note
+                        * Send the GBAR destroyed event to the client
+                        */
+                       instance_client_gbar_destroyed(inst, ret);
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *slave_release_extra_buffer(pid_t pid, int handle, const struct packet *packet)
+{
+       struct slave_node *slave;
+       struct packet *result;
+       const char *id;
+       struct buffer_info *info = NULL;
+       int ret;
+       int idx;
+       int type;
+       const char *pkgname;
+       struct inst_info *inst;
+       const struct pkg_info *pkg;
+
+       slave = slave_find_by_pid(pid);
+       if (!slave) {
+               ErrPrint("Slave %d is not exists\n", pid);
+               ret = WIDGET_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       if (packet_get(packet, "issi", &type, &pkgname, &id, &idx) != 4) {
+               ErrPrint("Inavlid argument\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = validate_request(pid, slave, pkgname, id, &inst, &pkg);
+       if (ret != WIDGET_ERROR_NONE) {
+               goto out;
+       }
+
+       ret = WIDGET_ERROR_INVALID_PARAMETER;
+
+       if (type == TYPE_WIDGET && package_widget_type(pkg) == WIDGET_TYPE_BUFFER) {
+               info = instance_widget_extra_buffer(inst, idx);
+               (void)instance_client_widget_extra_buffer_destroyed(inst, idx);
+       } else if (type == TYPE_GBAR && package_gbar_type(pkg) == GBAR_TYPE_BUFFER) {
+               info = instance_gbar_extra_buffer(inst, idx);
+               (void)instance_client_gbar_extra_buffer_destroyed(inst, idx);
+       }
+
+       if (info) {
+               ret = buffer_handler_unload(info);
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a reply packet\n");
+       }
+
+       return result;
+}
+
+static void delete_ctx_cb(int handle, void *data)
+{
+       struct sync_ctx_item *ctx = data;
+
+       s_info.hello_sync_ctx_list = eina_list_remove(s_info.hello_sync_ctx_list, ctx);
+
+       if (ctx->handle != handle) {
+               ErrPrint("Context is not valid (%s)\n", ctx->pkgname);
+       }
+
+       DbgFree(ctx->pkgname);
+       DbgFree(ctx);
+}
+
+static struct packet *slave_hello_sync_prepare(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
+{
+       char pkgname[pathconf("/", _PC_PATH_MAX)];
+       int ret;
+       struct sync_ctx_item *ctx;
+       double timestamp;
+       Eina_List *l;
+       Eina_List *n;
+
+       ret = packet_get(packet, "d", &timestamp);
+       if (ret != 1) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       if (aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname)) != AUL_R_OK) {
+               ErrPrint("pid[%d] is not authroized provider package, try to find it using its name\n", pid);
+               goto out;
+       }
+
+       ctx = NULL;
+       EINA_LIST_FOREACH_SAFE(s_info.hello_sync_ctx_list, l, n, ctx) {
+               if (ctx->timestamp == timestamp) {
+                       if (strcmp(pkgname, ctx->pkgname)) {
+                               ErrPrint("timestamp is valid, but pkgname is not matched: %s <> %s\n", pkgname, ctx->pkgname);
+                               /* Go ahead */
+                       }
+
+                       break;
+               }
+               ctx = NULL;
+       }
+
+       if (ctx) {
+               struct slave_node *slave;
+               /**
+                * @note
+                * This is only possible that the hello_sync comes first.
+                * In that case, we should activate the slave from here.
+                */
+               s_info.hello_sync_ctx_list  = eina_list_remove(s_info.hello_sync_ctx_list, ctx);
+               if (ctx->prepare_sync_wait_timer) {
+                       ecore_timer_del(ctx->prepare_sync_wait_timer);
+               }
+               DbgFree(ctx->pkgname);
+               DbgFree(ctx);
+
+               slave = slave_find_by_pid(pid);
+               if (!slave) {
+                       ErrPrint("Unable to activate slave %d\n", pid);
+                       goto out;
+               }
+
+               DbgPrint("Update handle for %s (%d)\n", pkgname, handle);
+               slave_rpc_update_handle(slave, handle, 1);
+       } else {
+               ctx = calloc(1, sizeof(*ctx));
+               if (!ctx) {
+                       ErrPrint("calloc: %d\n", errno);
+                       goto out;
+               }
+
+               ctx->timestamp = timestamp;
+
+               ctx->pkgname = strdup(pkgname);
+               if (!ctx->pkgname) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(ctx);
+                       goto out;
+               }
+
+               ctx->pid = pid;
+               ctx->handle = handle;
+
+               DbgPrint("Sync context created: %s\n", pkgname);
+               s_info.hello_sync_ctx_list = eina_list_append(s_info.hello_sync_ctx_list, ctx);
+
+               if (dead_callback_add(handle, delete_ctx_cb, ctx) < 0) {
+                       ErrPrint("Failed to add dead callback\n");
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static Eina_Bool prepare_sync_wait_cb(void *data)
+{
+       struct sync_ctx_item *item = data;
+
+       s_info.hello_sync_ctx_list = eina_list_remove(s_info.hello_sync_ctx_list, item);
+
+       ErrPrint("Sync timer(%lf) expired (%s), Terminate %d\n", WIDGET_CONF_SLAVE_ACTIVATE_TIME * 2.0f, item->pkgname, item->pid);
+
+       if (item->pid > 0) {
+               aul_terminate_pid_async(item->pid);
+       }
+
+       item->prepare_sync_wait_timer = NULL;
+       DbgFree(item->pkgname);
+       DbgFree(item);
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *slave_hello_sync(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
+{
+       struct packet *result = NULL;
+       struct slave_node *slave;
+       const char *slavename;
+       const char *slave_pkgname;
+       const char *acceleration;
+       const char *abi;
+       char *widget_id;
+       int secured;
+       int ret;
+       char pkgname[pathconf("/", _PC_PATH_MAX)];
+       double timestamp;
+
+       ret = packet_get(packet, "dissss", &timestamp, &secured, &slavename, &slave_pkgname, &acceleration, &abi);
+       if (ret != 6) {
+               ErrPrint("Parameter is not matched\n");
+               goto out;
+       }
+
+       if (acceleration[0] == '\0') {
+               acceleration = NULL;
+       }
+
+       DbgPrint("New slave[%s](%d) is arrived.\n", slavename, pid);
+
+       /**
+        * In case of watch-app, the slavename is declared by app itself.
+        * So slave_find_by_name will always fail.
+        * But the slave object can be found from slave_find_by_pid.
+        */
+       slave = slave_find_by_name(slavename);
+       if (!slave) { /* Try again to find a slave using pid */
+               slave = slave_find_by_pid(pid);
+               if (!slave) {
+                       slave = slave_find_by_pkgname(slave_pkgname);
+                       ErrPrint("Failed to find, Find it using pkgname(%s) - %p\n", slave_pkgname, slave);
+               }
+       }
+
+       if (aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname)) != AUL_R_OK) {
+               ErrPrint("pid[%d] is not authroized provider package, try to find it using its name[%s]\n", pid, slavename);
+               goto out;
+       }
+
+       if (slave || !(WIDGET_CONF_DEBUG_MODE || g_conf.debug_mode)) {
+               struct sync_ctx_item *item;
+               Eina_List *l;
+               Eina_List *n;
+
+               item = NULL;
+               EINA_LIST_FOREACH_SAFE(s_info.hello_sync_ctx_list, l, n, item) {
+                       if (item->timestamp == timestamp && item->pid == pid) {
+                               s_info.hello_sync_ctx_list = eina_list_remove(s_info.hello_sync_ctx_list, item);
+                               break;
+                       }
+                       item = NULL;
+               }
+
+               if (!item) {
+                       ErrPrint("There is no such sync context (%d), Waiting prepare ctx\n", pid);
+
+                       item = calloc(1, sizeof(*item));
+                       if (!item) {
+                               ErrPrint("calloc: %d\n", errno);
+                               goto out;
+                       }
+
+                       item->pkgname = strdup(pkgname);
+                       if (!item->pkgname) {
+                               ErrPrint("strdup: %d\n", errno);
+                               DbgFree(item);
+                               goto out;
+                       }
+
+                       item->handle = -1;
+                       item->pid = pid;
+                       item->timestamp = timestamp;
+
+                       /**
+                        * @note
+                        * If the prepare sync doesn't come in ACTIVATE_TIME * 2.0f, there is a problem. we have to cancel it
+                        */
+                       item->prepare_sync_wait_timer = ecore_timer_add(WIDGET_CONF_SLAVE_ACTIVATE_TIME * 2.0f, prepare_sync_wait_cb, item);
+
+                       /**
+                        * @note
+                        * How can we delete this if there is no prepare context?
+                        */
+                       s_info.hello_sync_ctx_list = eina_list_append(s_info.hello_sync_ctx_list, item);
+
+                       /**
+                        * @note
+                        * Reset the handle to prevent from updating RPC handle of SLAVE object
+                        */
+                       handle = -1;
+               } else {
+                       if (strcmp(item->pkgname, pkgname)) {
+                               ErrPrint("HELLO_SYNC is comes from different package: %s <> %s\n", item->pkgname, pkgname);
+                       }
+
+                       handle = item->handle;
+
+                       if (dead_callback_del(handle, delete_ctx_cb) != item) {
+                               ErrPrint("Dead callback is not valid\n");
+                       }
+
+                       DbgFree(item->pkgname);
+                       DbgFree(item);
+
+                       DbgPrint("Hello sync context found: %s\n", pkgname);
+               }
+       }
+
+       if (!slave) {
+               if (WIDGET_CONF_DEBUG_MODE || g_conf.debug_mode) {
+                       slave = debug_mode_enabled(pid, slavename, pkgname, secured, abi, acceleration);
+                       if (!slave) {
+                               ErrPrint("Failed to create a new slave for %s\n", slavename);
+                               goto out;
+                       }
+               } else {
+                       struct pkg_info *info;
+                       int network;
+                       int width, height;
+                       unsigned int widget_size;
+                       const char *category;
+                       const char *db_acceleration;
+                       int db_secured;
+                       const char *tmp;
+
+                       widget_id = is_valid_slave(pid, abi, pkgname);
+                       if (!widget_id) {
+                               goto out;
+                       }
+
+                       info = package_find(widget_id);
+                       if (!info) {
+                               char *pkgid;
+
+                               pkgid = widget_service_get_package_id(widget_id);
+                               if (!pkgid) {
+                                       DbgFree(widget_id);
+                                       goto out;
+                               }
+
+                               info = package_create(pkgid, widget_id);
+                               DbgFree(pkgid);
+                       }
+
+                       category = package_category(info);
+                       tmp = package_abi(info);
+                       db_secured = package_secured(info);
+                       db_acceleration = package_hw_acceleration(info);
+
+                       if (db_secured != secured) {
+                               DbgPrint("%s secured (%d)\n", pkgname, db_secured);
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       if (strcmp(tmp, abi)) {
+                               DbgPrint("%s abi (%s)\n", pkgname, tmp);
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       if (strcmp(acceleration, db_acceleration)) {
+                               DbgPrint("%s accel (%s)\n", pkgname, db_acceleration);
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       if (util_string_is_in_list(category, WIDGET_CONF_CATEGORY_LIST) == 0) {
+                               DbgPrint("%s category (%s)\n", pkgname, category);
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       network = package_network(info);
+
+                       if (strcmp(CATEGORY_WATCH_CLOCK, category) == 0) {
+                               /**
+                                * if the new provider is watch app,
+                                * destroy the old watch app instance
+                                */
+                               package_del_instance_by_category(CATEGORY_WATCH_CLOCK, NULL);
+                       }
+
+                       /**
+                        * If a provider sent hello_sync, we will assumes it as a watch widget.
+                        * In this case, activate it again asynchronously.
+                        */
+                       slave = slave_create(slavename, secured, abi, pkgname, network, acceleration, 1);
+                       if (!slave) {
+                               ErrPrint("Failed to create a new slave for %s\n", slavename);
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       slave_set_state(slave, SLAVE_REQUEST_TO_LAUNCH);
+                       slave_set_pid(slave, pid);
+                       slave_set_valid(slave);
+
+                       if (handle >= 0) {
+                               /**
+                                * @note
+                                * In this case, there could not be exists any pended packets.
+                                * But we tried to clear them for a case!
+                                */
+                               slave_rpc_update_handle(slave, handle, 1);
+                       } else {
+                               DbgPrint("Slave RPC should be updated soon (waiting prepare sync)\n");
+                       }
+
+                       DbgPrint("Slave is activated by request: %d (%s)/(%s)\n", pid, pkgname, slavename);
+
+                       widget_size = package_size_list(info);
+
+                       if (widget_size & WIDGET_SIZE_TYPE_2x2) {
+                               widget_service_get_size(WIDGET_SIZE_TYPE_2x2, &width, &height);
+                       } else if (widget_size & WIDGET_SIZE_TYPE_4x4) {
+                               widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
+                       } else {
+                               widget_service_get_size(WIDGET_SIZE_TYPE_1x1, &width, &height);
+                               DbgPrint("widget(%s] does not support size [2x2], [4x4]\n",pkgname);
+                       }
+
+                       result = instance_watch_create(packet, widget_id, width, height);
+                       if (!result) {
+                               ErrPrint("Failed to create a new instance\n");
+                               if (slave_unref(slave)) {
+                                       ErrPrint("Slave is not deleted yet\n");
+                               }
+                       }
+                       DbgFree(widget_id);
+               }
+       } else {
+               struct pkg_info *info;
+               struct inst_info *inst;
+               int width, height;
+               unsigned int widget_size;
+               Eina_List *inst_list;
+               Eina_List *inst_l;
+               const char *category;
+
+               widget_id = is_valid_slave(pid, abi, pkgname);
+               if (!widget_id) {
+                       goto out;
+               }
+
+               info = package_find(widget_id);
+               if (!info) {
+                       char *pkgid;
+
+                       pkgid = widget_service_get_package_id(widget_id);
+                       if (!pkgid) {
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+
+                       info = package_create(pkgid, widget_id);
+                       DbgFree(pkgid);
+               }
+
+               /**
+                * Finding a created instance information
+                */
+               category = package_category(info);
+               inst_list = package_instance_list(info);
+               inst = NULL;
+               EINA_LIST_FOREACH(inst_list, inst_l, inst) {
+                       if (!strcmp(package_name(instance_package(inst)), widget_id)) {
+                               break;
+                       }
+                       inst = NULL;
+               }
+
+               if (!inst) {
+                       ErrPrint("No valid instance");
+                       DbgFree(widget_id);
+                       goto out;
+               }
+
+               if (slave_pid(slave) != pid) {
+                       if (slave_pid(slave) > 0) {
+                               CRITICAL_LOG("Slave(%s) is already assigned to %d\n", slave_name(slave), slave_pid(slave));
+                               if (pid > 0) {
+                                       ret = aul_terminate_pid_async(pid);
+                                       CRITICAL_LOG("Terminate %d (ret: %d)\n", pid, ret);
+                               }
+                               DbgFree(widget_id);
+                               goto out;
+                       }
+                       CRITICAL_LOG("PID of slave(%s) is updated (%d -> %d)\n", slave_name(slave), slave_pid(slave), pid);
+                       slave_set_pid(slave, pid);
+               }
+
+               slave_set_valid(slave);
+
+               /**
+                * @TODO
+                * This should able to be configurable by .conf file.
+                * Watch instance can be created only ONE in this system.
+                */
+               if (strcmp(CATEGORY_WATCH_CLOCK, category) == 0) {
+                       /**
+                        * @note
+                        * If a new provider is watch app, destroy the old watch app instance
+                        */
+                       package_del_instance_by_category(CATEGORY_WATCH_CLOCK, widget_id);
+               }
+
+               if (handle >= 0) {
+                       slave_rpc_update_handle(slave, handle, 1);
+
+                       /**
+                        * @note
+                        * This PAUSE/RESUME state will be sent to the provider after the instance is created.
+                        * Because, this command is sync. so there is no way to get the state packet first then create packet.
+                        */
+               } else {
+                       DbgPrint("Slave RPC should be updated soon (waiting prepare sync)\n");
+               }
+
+               DbgPrint("Slave is activated by request: %d (%s)/(%s)\n", pid, pkgname, slavename);
+               widget_size = package_size_list(info);
+
+               if (widget_size & WIDGET_SIZE_TYPE_2x2) {
+                       widget_service_get_size(WIDGET_SIZE_TYPE_2x2, &width, &height);
+               } else if (widget_size & WIDGET_SIZE_TYPE_4x4) {
+                       widget_service_get_size(WIDGET_SIZE_TYPE_4x4, &width, &height);
+               } else {
+                       widget_service_get_size(WIDGET_SIZE_TYPE_1x1, &width, &height);
+                       DbgPrint("widget(%s] does not support size [2x2], [4x4]\n",pkgname);
+               }
+
+               result = instance_duplicate_packet_create(packet, inst, info, width, height);
+
+               DbgFree(widget_id);
+       }
+
+out:
+       return result;
+}
+
+static struct packet *service_instance_count(pid_t pid, int handle, const struct packet *packet)
+{
+       struct packet *result;
+       struct pkg_info *pkg;
+       double timestamp;
+       const char *pkgname;
+       const char *cluster;
+       const char *category;
+       Eina_List *pkg_list;
+       Eina_List *l;
+       Eina_List *inst_list;
+       Eina_List *inst_l;
+       struct inst_info *inst;
+       int ret;
+
+       ret = packet_get(packet, "sssd", &pkgname, &cluster, &category, &timestamp);
+       if (ret != 4) {
+               ErrPrint("Invalid packet\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       pkg_list = (Eina_List *)package_list();
+
+       ret = 0;
+       EINA_LIST_FOREACH(pkg_list, l, pkg) {
+               if (pkgname && pkgname[0]) {
+                       if (strcmp(package_name(pkg), pkgname)) {
+                               continue;
+                       }
+               }
+
+               inst_list = package_instance_list(pkg);
+               EINA_LIST_FOREACH(inst_list, inst_l, inst) {
+                       if (cluster && cluster[0]) {
+                               if (strcmp(instance_cluster(inst), cluster)) {
+                                       continue;
+                               }
+                       }
+
+                       if (category && category[0]) {
+                               if (strcmp(instance_category(inst), category)) {
+                                       continue;
+                               }
+                       }
+
+                       ret++;
+               }
+       }
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *service_get_content(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *inst_id;
+       const char *widget_id;
+       struct inst_info *inst;
+       struct packet *result;
+
+       if (packet_get(packet, "ss", &widget_id, &inst_id) != 2) {
+               ErrPrint("Invalid parameter\n");
+               return NULL;
+       }
+
+       inst = package_find_instance_by_id(widget_id, inst_id);
+       if (!inst) {
+               result = packet_create_reply(packet, "is", WIDGET_ERROR_NOT_EXIST, "?");
+       } else {
+               result = packet_create_reply(packet, "is", WIDGET_ERROR_NONE, instance_content(inst));
+       }
+
+       if (!result) {
+               ErrPrint("Failed to create a result packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *service_get_inst_list(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *widget_id;
+       struct pkg_info *pkg;
+       struct packet *result;
+
+       if (packet_get(packet, "s", &widget_id) != 1) {
+               ErrPrint("Invalid parameter\n");
+               return NULL;
+       }
+
+       pkg = package_find(widget_id);
+       if (!pkg) {
+               result = packet_create_reply(packet, "iis", WIDGET_ERROR_NOT_EXIST, 0, NULL);
+       } else {
+               Eina_List *inst_list;
+
+               inst_list = package_instance_list(pkg);
+               if (!inst_list) {
+                       result = packet_create_reply(packet, "iis", WIDGET_ERROR_NOT_EXIST, 0, NULL);
+               } else {
+                       char *id_buffer;
+                       int size = sysconf(_SC_PAGESIZE);
+                       Eina_List *l;
+                       struct inst_info *inst;
+                       int offset;
+                       int len;
+                       int cnt = 0;
+
+                       id_buffer = malloc(size);
+                       if (!id_buffer) {
+                               result = packet_create_reply(packet, "iis", WIDGET_ERROR_OUT_OF_MEMORY, 0, NULL);
+                               goto out;
+                       }
+
+                       offset = 0;
+                       EINA_LIST_FOREACH(inst_list, l, inst) {
+                               len = strlen(instance_id(inst));
+                               if (offset + len > size) {
+                                       /* Expanding the ID_BUFFER */
+                                       char *resized_buffer;
+                                       size += sysconf(_SC_PAGESIZE);
+
+                                       DbgPrint("Expanding heap to %d\n", size);
+
+                                       resized_buffer = realloc(id_buffer, size);
+                                       if (!resized_buffer) {
+                                               ErrPrint("realloc: %d\n", errno);
+                                               DbgFree(id_buffer);
+                                               result = packet_create_reply(packet, "iis", WIDGET_ERROR_OUT_OF_MEMORY, 0, NULL);
+                                               goto out;
+                                       }
+
+                                       id_buffer = resized_buffer;
+                                       cnt++;
+                               }
+
+                               strcpy(id_buffer + offset, instance_id(inst));
+                               /* Replace last NULL with NEW_LINE */
+                               id_buffer[offset + len] = '\n';
+                               offset += (len + 1);
+                       }
+
+                       id_buffer[offset] = '\0';
+                       result = packet_create_reply(packet, "iis", WIDGET_ERROR_NONE, cnt, id_buffer);
+                       DbgFree(id_buffer);
+               }
+       }
+
+out:
+       return result;
+}
+
+static struct packet *monitor_register(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *widget_id;
+
+       if (packet_get(packet, "s", &widget_id) != 1) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       if (widget_id[0] == '*' && widget_id[1] == '\0') {
+               widget_id = NULL;
+       }
+
+       DbgPrint("Register monitor target for [%s]\n", widget_id);
+
+       if (monitor_find_client_by_pid(widget_id, pid)) {
+               ErrPrint("Already registered: [%s], %d\n", widget_id, pid);
+       } else {
+               if (monitor_create_client(widget_id, pid, handle) == NULL) {
+                       ErrPrint("Failed to create a new monitor client\n");
+               }
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *monitor_unregister(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *widget_id;
+       struct monitor_client *monitor;
+
+       if (packet_get(packet, "s", &widget_id) != 1) {
+               ErrPrint("Invalid parameter\n");
+               goto out;
+       }
+
+       if (widget_id[0] == '*' && widget_id[1] == '\0') {
+               widget_id = NULL;
+       }
+
+       monitor = monitor_find_client_by_pid(widget_id, pid);
+       if (monitor) {
+               DbgPrint("Unregister monitor target: %s\n", widget_id);
+               monitor_destroy_client(monitor);
+       } else {
+               ErrPrint("Monitor for %s is not exists(%d)\n", widget_id, pid);
+       }
+
+out:
+       return NULL;
+}
+
+static struct packet *service_change_period(pid_t pid, int handle, const struct packet *packet)
+{
+       struct inst_info *inst;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       double period;
+       int ret;
+
+       ret = packet_get(packet, "ssd", &pkgname, &id, &period);
+       if (ret != 3) {
+               ErrPrint("Invalid packet\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       if (!strlen(id)) {
+               struct pkg_info *pkg;
+
+               pkg = package_find(pkgname);
+               if (!pkg) {
+                       ret = WIDGET_ERROR_NOT_EXIST;
+               } else if (package_is_fault(pkg)) {
+                       ret = WIDGET_ERROR_FAULT;
+               } else {
+                       Eina_List *inst_list;
+                       Eina_List *l;
+
+                       inst_list = package_instance_list(pkg);
+                       EINA_LIST_FOREACH(inst_list, l, inst) {
+                               ret = instance_set_period(inst, period);
+                               if (ret < 0) {
+                                       ErrPrint("Failed to change the period of %s to (%lf)\n", pkgname, period);
+                               }
+                       }
+               }
+       } else {
+               ret = validate_request(pid, NULL, pkgname, id, &inst, NULL);
+               if (ret == (int)WIDGET_ERROR_NONE) {
+                       if (instance_state(inst) == INST_DESTROYED) {
+                               ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
+                               ret = WIDGET_ERROR_INVALID_PARAMETER;
+                               goto out;
+                       }
+
+                       ret = instance_set_period(inst, period);
+               }
+       }
+
+       DbgPrint("Change the update period: %s, %lf : %d\n", pkgname, period, ret);
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *service_update(pid_t pid, int handle, const struct packet *packet)
+{
+       Eina_List *inst_list;
+       struct pkg_info *pkg;
+       struct packet *result;
+       const char *pkgname;
+       const char *id;
+       const char *cluster;
+       const char *category;
+       const char *content;
+       int force;
+       char *widget_id;
+       int ret;
+
+       ret = packet_get(packet, "sssssi", &pkgname, &id, &cluster, &category, &content, &force);
+       if (ret != 6) {
+               ErrPrint("Invalid Packet\n");
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       widget_id = package_widget_pkgname(pkgname);
+       if (!widget_id) {
+               ErrPrint("Invalid package %s\n", pkgname);
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       pkg = package_find(widget_id);
+       if (!pkg) {
+               ret = WIDGET_ERROR_NOT_EXIST;
+               DbgFree(widget_id);
+               goto out;
+       }
+
+       if (package_is_fault(pkg)) {
+               ret = WIDGET_ERROR_FAULT;
+               DbgFree(widget_id);
+               goto out;
+       }
+
+       inst_list = package_instance_list(pkg);
+       if (!eina_list_count(inst_list)) {
+               ret = WIDGET_ERROR_NOT_EXIST;
+               DbgFree(widget_id);
+               goto out;
+       }
+
+       if (id && strlen(id)) {
+               Eina_List *l;
+               struct inst_info *inst;
+
+               ret = WIDGET_ERROR_NOT_EXIST;
+               EINA_LIST_FOREACH(inst_list, l, inst) {
+                       if (!strcmp(instance_id(inst), id)) {
+                               ret = WIDGET_ERROR_NONE;
+                               break;
+                       }
+               }
+
+               if (ret == (int)WIDGET_ERROR_NOT_EXIST) {
+                       DbgFree(widget_id);
+                       goto out;
+               }
+       }
+
+       /*!
+        * \TODO
+        * Validate the update requstor.
+        */
+       slave_rpc_request_update(widget_id, id, cluster, category, content, force);
+       DbgFree(widget_id);
+       ret = WIDGET_ERROR_NONE;
+
+out:
+       result = packet_create_reply(packet, "i", ret);
+       if (!result) {
+               ErrPrint("Failed to create a packet\n");
+       }
+
+       return result;
+}
+
+static struct packet *widget_mgr_hello(pid_t pid, int handle, const struct packet *packet)
+{
+       struct widget_mgr *info;
+       struct packet *result;
+       int ret;
+       const char *fifo_name;
+       double timestamp;
+
+       DbgPrint("Request arrived from %d\n", pid);
+
+       if (packet_get(packet, "d", &timestamp) != 1) {
+               ErrPrint("Invalid packet\n");
+               fifo_name = "";
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       info = widget_mgr_create(pid, handle);
+       if (!info) {
+               ErrPrint("Failed to create a widget_mgr object\n");
+               fifo_name = "";
+               ret = WIDGET_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = 0;
+       fifo_name = widget_mgr_filename(info);
+       DbgPrint("FIFO Created: %s (Serve for %d)\n", fifo_name, pid);
+
+out:
+       result = packet_create_reply(packet, "si", fifo_name, ret);
+       if (!result) {
+               ErrPrint("Failed to create a result packet\n");
+       }
+
+       return result;
+}
+
+static Eina_Bool lazy_slave_list_cb(void *info)
+{
+       FILE *fp;
+       Eina_List *list;
+       Eina_List *l;
+       struct slave_node *slave;
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       list = (Eina_List *)slave_list();
+       EINA_LIST_FOREACH(list, l, slave) {
+               fprintf(fp, "%d %s %s %s %d %d %d %s %d %d %lf\n", 
+                               slave_pid(slave),
+                               slave_name(slave),
+                               slave_pkgname(slave),
+                               slave_abi(slave),
+                               slave_is_secured(slave),
+                               slave_refcnt(slave),
+                               slave_fault_count(slave),
+                               slave_state_string(slave),
+                               slave_loaded_instance(slave),
+                               slave_loaded_package(slave),
+                               slave_ttl(slave)
+                          );
+       }
+
+       fprintf(fp, "EOD\n");
+       widget_mgr_close_fifo(info);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *widget_mgr_slave_list(pid_t pid, int handle, const struct packet *packet)
+{
+       struct widget_mgr *info;
+       double timestamp;
+
+       if (packet_get(packet, "d", &timestamp) != 1) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       info = widget_mgr_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       lazy_slave_list_cb(info);
+out:
+       return NULL;
+}
+
+static inline const char *visible_state_string(enum widget_visible_state state)
+{
+       switch (state) {
+       case WIDGET_SHOW:
+               return "Show";
+       case WIDGET_HIDE:
+               return "Hide";
+       case WIDGET_HIDE_WITH_PAUSE:
+               return "Paused";
+       default:
+               break;
+       }
+
+       return "Unknown";
+}
+
+static Eina_Bool inst_list_cb(void *info)
+{
+       FILE *fp;
+       char *pkgname;
+       struct pkg_info *pkg;
+       Eina_List *l;
+       Eina_List *inst_list;
+       struct inst_info *inst;
+
+       pkgname = widget_mgr_data(info);
+       if (!pkgname) {
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               ErrPrint("Invalid fp\n");
+               widget_mgr_close_fifo(info);
+               free(pkgname);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!package_is_widget_pkgname(pkgname)) {
+               ErrPrint("Invalid package name\n");
+               free(pkgname);
+               goto close_out;
+       }
+
+       pkg = package_find(pkgname);
+       free(pkgname);
+       if (!pkg) {
+               ErrPrint("Package is not exists\n");
+               goto close_out;
+       }
+
+       inst_list = package_instance_list(pkg);
+       EINA_LIST_FOREACH(inst_list, l, inst) {
+               fprintf(fp, "%s %s %s %s %lf %s %d %d\n",
+                               instance_id(inst),
+                               buffer_handler_id(instance_widget_buffer(inst)),
+                               instance_cluster(inst),
+                               instance_category(inst),
+                               instance_period(inst),
+                               visible_state_string(instance_visible_state(inst)),
+                               instance_widget_width(inst),
+                               instance_widget_height(inst));
+       }
+
+close_out:
+       fprintf(fp, "EOD\n");
+       widget_mgr_close_fifo(info);
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *widget_mgr_inst_list(pid_t pid, int handle, const struct packet *packet)
+{
+       const char *pkgname;
+       char *dup_pkgname;
+       struct widget_mgr *info;
+
+       if (packet_get(packet, "s", &pkgname) != 1) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       info = widget_mgr_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       dup_pkgname = strdup(pkgname);
+       if (!dup_pkgname) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       widget_mgr_set_data(info, dup_pkgname);
+       inst_list_cb(info);
+
+out:
+       return NULL;
+}
+
+static Eina_Bool pkg_list_cb(void *info)
+{
+       FILE *fp;
+       Eina_List *l;
+       Eina_List *list;
+       Eina_List *inst_list;
+       struct pkg_info *pkg;
+       struct slave_node *slave;
+       const char *slavename;
+       pid_t pid;
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               DbgPrint("Failed to open a pipe\n");
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       list = (Eina_List *)package_list();
+       EINA_LIST_FOREACH(list, l, pkg) {
+               slave = package_slave(pkg);
+
+               if (slave) {
+                       slavename = slave_name(slave);
+                       pid = slave_pid(slave);
+               } else {
+                       pid = (pid_t)-1;
+                       slavename = "";
+               }
+
+               inst_list = (Eina_List *)package_instance_list(pkg);
+               fprintf(fp, "%d %s %s %s %d %d %d\n",
+                               pid,
+                               strlen(slavename) ? slavename : "(none)",
+                               package_name(pkg),
+                               package_abi(pkg),
+                               package_refcnt(pkg),
+                               package_fault_count(pkg),
+                               eina_list_count(inst_list)
+                          );
+               DbgPrint("%d %s %s %s %d %d %d\n",
+                               pid,
+                               strlen(slavename) ? slavename : "(none)",
+                               package_name(pkg),
+                               package_abi(pkg),
+                               package_refcnt(pkg),
+                               package_fault_count(pkg),
+                               eina_list_count(inst_list)
+                               );
+       }
+
+       fprintf(fp, "EOD\n");
+       DbgPrint("EOD\n");
+       widget_mgr_close_fifo(info);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *widget_mgr_pkg_list(pid_t pid, int handle, const struct packet *packet)
+{
+       struct widget_mgr *info;
+       double timestamp;
+
+       if (packet_get(packet, "d", &timestamp) != 1) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       DbgPrint("Package List: %lf\n", timestamp);
+
+       info = widget_mgr_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       pkg_list_cb(info);
+out:
+       return NULL;
+}
+
+static struct packet *widget_mgr_slave_ctrl(pid_t pid, int handle, const struct packet *packet)
+{
+       return NULL;
+}
+
+static Eina_Bool pkg_ctrl_rmpack_cb(void *info)
+{
+       FILE *fp;
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       fprintf(fp, "%d\n", ENOSYS);
+       fprintf(fp, "EOD\n");
+       widget_mgr_close_fifo(info);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool pkg_ctrl_rminst_cb(void *info)
+{
+       FILE *fp;
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       fprintf(fp, "%d\n", (int)widget_mgr_data(info));
+       fprintf(fp, "EOD\n");
+       widget_mgr_close_fifo(info);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool pkg_ctrl_faultinst_cb(void *info)
+{
+       FILE *fp;
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       fprintf(fp, "%d\n", (int)widget_mgr_data(info));
+       fprintf(fp, "EOD\n");
+       widget_mgr_close_fifo(info);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *widget_mgr_pkg_ctrl(pid_t pid, int handle, const struct packet *packet)
+{
+       struct widget_mgr *info;
+       char *cmd;
+       char *pkgname;
+       char *id;
+
+       if (packet_get(packet, "sss", &cmd, &pkgname, &id) != 3) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       info = widget_mgr_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       if (!strcmp(cmd, "rmpack")) {
+               pkg_ctrl_rmpack_cb(info);
+       } else if (!strcmp(cmd, "rminst")) {
+               struct inst_info *inst;
+               inst = package_find_instance_by_id(pkgname, id);
+               if (!inst) {
+                       widget_mgr_set_data(info, (void *)ENOENT);
+               } else {
+                       (void)instance_destroy(inst, WIDGET_DESTROY_TYPE_DEFAULT);
+                       widget_mgr_set_data(info, (void *)0);
+               }
+
+               pkg_ctrl_rminst_cb(info);
+       } else if (!strcmp(cmd, "faultinst")) {
+               struct inst_info *inst;
+               inst = package_find_instance_by_id(pkgname, id);
+               if (!inst) {
+                       widget_mgr_set_data(info, (void *)ENOENT);
+               } else {
+                       struct pkg_info *pkg;
+
+                       pkg = instance_package(inst);
+                       if (!pkg) {
+                               widget_mgr_set_data(info, (void *)EFAULT);
+                       } else {
+                               (void)package_faulted(pkg, 1);
+                               widget_mgr_set_data(info, (void *)0);
+                       }
+               }
+
+               pkg_ctrl_faultinst_cb(info);
+       }
+
+out:
+       return NULL;
+}
+
+static Eina_Bool master_ctrl_cb(void *info)
+{
+       FILE *fp;
+
+       widget_mgr_open_fifo(info);
+       fp = widget_mgr_fifo(info);
+       if (!fp) {
+               widget_mgr_close_fifo(info);
+               return ECORE_CALLBACK_CANCEL;
+       }
+       fprintf(fp, "%d\nEOD\n", (int)widget_mgr_data(info));
+       widget_mgr_close_fifo(info);
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct packet *widget_mgr_master_ctrl(pid_t pid, int handle, const struct packet *packet)
+{
+       struct widget_mgr *info;
+       char *cmd;
+       char *var;
+       char *val;
+       int ret = WIDGET_ERROR_INVALID_PARAMETER;
+
+       if (packet_get(packet, "sss", &cmd, &var, &val) != 3) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       info = widget_mgr_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       if (!strcasecmp(var, "debug")) {
+               if (!strcasecmp(cmd, "set")) {
+                       g_conf.debug_mode = !strcasecmp(val, "on");
+               } else if (!strcasecmp(cmd, "get")) {
+               }
+               ret = g_conf.debug_mode;
+       } else if (!strcasecmp(var, "slave_max_load")) {
+               if (!strcasecmp(cmd, "set")) {
+                       g_conf.slave_max_load = atoi(val);
+               } else if (!strcasecmp(cmd, "get")) {
+               }
+               ret = g_conf.slave_max_load;
+       }
+
+       widget_mgr_set_data(info, (void *)ret);
+       master_ctrl_cb(info);
+
+out:
+       return NULL;
+}
+
+static struct method s_info_table[] = {
+       {
+               .cmd = CMD_STR_INFO_HELLO,
+               .handler = widget_mgr_hello,
+       },
+       {
+               .cmd = CMD_STR_INFO_SLAVE_LIST,
+               .handler = widget_mgr_slave_list,
+       },
+       {
+               .cmd = CMD_STR_INFO_PKG_LIST,
+               .handler = widget_mgr_pkg_list,
+       },
+       {
+               .cmd = CMD_STR_INFO_INST_LIST,
+               .handler = widget_mgr_inst_list,
+       },
+       {
+               .cmd = CMD_STR_INFO_SLAVE_CTRL,
+               .handler = widget_mgr_slave_ctrl,
+       },
+       {
+               .cmd = CMD_STR_INFO_PKG_CTRL,
+               .handler = widget_mgr_pkg_ctrl,
+       },
+       {
+               .cmd = CMD_STR_INFO_MASTER_CTRL,
+               .handler = widget_mgr_master_ctrl,
+       },
+       {
+               .cmd = NULL,
+               .handler = NULL,
+       },
+};
+
+static struct method s_client_table[] = {
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_MOVE,
+               .handler = client_gbar_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_MOVE,
+               .handler = client_widget_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_DOWN,
+               .handler = client_gbar_mouse_down, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_UP,
+               .handler = client_gbar_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_DOWN,
+               .handler = client_widget_mouse_down, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_UP,
+               .handler = client_widget_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_ENTER,
+               .handler = client_gbar_mouse_enter, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_LEAVE,
+               .handler = client_gbar_mouse_leave, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_ENTER,
+               .handler = client_widget_mouse_enter, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_LEAVE,
+               .handler = client_widget_mouse_leave, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_ON_SCROLL,
+               .handler = client_widget_mouse_on_scroll,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_OFF_SCROLL,
+               .handler = client_widget_mouse_off_scroll,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_ON_SCROLL,
+               .handler = client_gbar_mouse_on_scroll,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_OFF_SCROLL,
+               .handler = client_gbar_mouse_off_scroll,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_ON_HOLD,
+               .handler = client_widget_mouse_on_hold,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_OFF_HOLD,
+               .handler = client_widget_mouse_off_hold,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_ON_HOLD,
+               .handler = client_gbar_mouse_on_hold,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_OFF_HOLD,
+               .handler = client_gbar_mouse_off_hold,
+       },
+       {
+               .cmd = CMD_STR_CLICKED,
+               .handler = client_clicked, /*!< pid, pkgname, filename, event, timestamp, x, y, ret */
+       },
+       {
+               .cmd = CMD_STR_TEXT_SIGNAL,
+               .handler = client_text_signal, /* pid, pkgname, filename, signal_name, source, s, sy, ex, ey, ret */
+       },
+       {
+               .cmd = CMD_STR_DELETE,
+               .handler = client_delete, /* pid, pkgname, filename, ret */
+       },
+       {
+               .cmd = CMD_STR_RESIZE,
+               .handler = client_resize, /* pid, pkgname, filename, w, h, ret */
+       },
+       {
+               .cmd = CMD_STR_NEW,
+               .handler = client_new, /* pid, timestamp, pkgname, content, cluster, category, period, ret */
+       },
+       {
+               .cmd = CMD_STR_SET_PERIOD,
+               .handler = client_set_period, /* pid, pkgname, filename, period, ret, period */
+       },
+       {
+               .cmd = CMD_STR_CHANGE_GROUP,
+               .handler = client_change_group, /* pid, pkgname, filename, cluster, category, ret */
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOVE,
+               .handler = client_gbar_move, /* pkgname, id, x, y */
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_HL,
+               .handler = client_gbar_access_hl,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_ACTIVATE,
+               .handler = client_gbar_access_activate,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_ACTION,
+               .handler = client_gbar_access_action,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_SCROLL,
+               .handler = client_gbar_access_scroll,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_VALUE_CHANGE,
+               .handler = client_gbar_access_value_change,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_MOUSE,
+               .handler = client_gbar_access_mouse,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_BACK,
+               .handler = client_gbar_access_back,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_OVER,
+               .handler = client_gbar_access_over,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_READ,
+               .handler = client_gbar_access_read,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACCESS_ENABLE,
+               .handler = client_gbar_access_enable,
+       },
+
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_HL,
+               .handler = client_widget_access_hl,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_ACTIVATE,
+               .handler = client_widget_access_activate,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_ACTION,
+               .handler = client_widget_access_action,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_SCROLL,
+               .handler = client_widget_access_scroll,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_VALUE_CHANGE,
+               .handler = client_widget_access_value_change,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_MOUSE,
+               .handler = client_widget_access_mouse,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_BACK,
+               .handler = client_widget_access_back,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_OVER,
+               .handler = client_widget_access_over,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_READ,
+               .handler = client_widget_access_read,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACCESS_ENABLE,
+               .handler = client_widget_access_enable,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_KEY_DOWN,
+               .handler = client_widget_key_down,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_KEY_UP,
+               .handler = client_widget_key_up,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_KEY_FOCUS_IN,
+               .handler = client_widget_key_focus_in,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_KEY_FOCUS_OUT,
+               .handler = client_widget_key_focus_out,
+       },
+       {
+               .cmd = CMD_STR_GBAR_KEY_DOWN,
+               .handler = client_gbar_key_down,
+       },
+       {
+               .cmd = CMD_STR_GBAR_KEY_UP,
+               .handler = client_gbar_key_up,
+       },
+       {
+               .cmd = CMD_STR_GBAR_KEY_FOCUS_IN,
+               .handler = client_gbar_key_focus_in,
+       },
+       {
+               .cmd = CMD_STR_GBAR_KEY_FOCUS_OUT,
+               .handler = client_gbar_key_focus_out,
+       },
+       {
+               .cmd = CMD_STR_UPDATE_MODE,
+               .handler = client_update_mode,
+       },
+       // Cut HERE. Above list must be sync'd with provider list.
+
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_SET,
+               .handler = client_widget_mouse_set,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_MOUSE_UNSET,
+               .handler = client_widget_mouse_unset,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_SET,
+               .handler = client_gbar_mouse_set,
+       },
+       {
+               .cmd = CMD_STR_GBAR_MOUSE_UNSET,
+               .handler = client_gbar_mouse_unset,
+       },
+       {
+               .cmd = CMD_STR_ORIENTATION,
+               .handler = client_orientation,
+       },
+       {
+               .cmd = CMD_STR_CHANGE_VISIBILITY,
+               .handler = client_change_visibility,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACQUIRE_PIXMAP,
+               .handler = client_widget_acquire_pixmap,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_RELEASE_PIXMAP,
+               .handler = client_widget_release_pixmap,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACQUIRE_PIXMAP,
+               .handler = client_gbar_acquire_pixmap,
+       },
+       {
+               .cmd = CMD_STR_GBAR_RELEASE_PIXMAP,
+               .handler = client_gbar_release_pixmap,
+       },
+       {
+               .cmd = CMD_STR_ACQUIRE,
+               .handler = client_acquire, /*!< pid, ret */
+       },
+       {
+               .cmd = CMD_STR_RELEASE,
+               .handler = cilent_release, /*!< pid, ret */
+       },
+       {
+               .cmd = CMD_STR_PINUP_CHANGED,
+               .handler = client_pinup_changed, /* pid, pkgname, filename, pinup, ret */
+       },
+       {
+               .cmd = CMD_STR_CREATE_GBAR,
+               .handler = client_create_gbar, /* pid, pkgname, filename, ret */
+       },
+       {
+               .cmd = CMD_STR_DESTROY_GBAR,
+               .handler = client_destroy_gbar, /* pid, pkgname, filename, ret */
+       },
+       {
+               .cmd = CMD_STR_ACTIVATE_PACKAGE,
+               .handler = client_activate_package, /* pid, pkgname, ret */
+       },
+       {
+               .cmd = CMD_STR_SUBSCRIBE, /* pid, cluster, sub-cluster */
+               .handler = client_subscribed_group,
+       },
+       {
+               .cmd = CMD_STR_UNSUBSCRIBE, /* pid, cluster, sub-cluster */
+               .handler = client_unsubscribed_group,
+       },
+       {
+               .cmd = CMD_STR_DELETE_CLUSTER,
+               .handler = client_delete_cluster,
+       },
+       {
+               .cmd = CMD_STR_DELETE_CATEGORY,
+               .handler = client_delete_category,
+       },
+       {
+               .cmd = CMD_STR_REFRESH_GROUP,
+               .handler = client_refresh_group,
+       },
+       {
+               .cmd = CMD_STR_UPDATE,
+               .handler = client_update,
+       },
+
+       {
+               .cmd = CMD_STR_WIDGET_KEY_SET,
+               .handler = client_widget_key_set,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_KEY_UNSET,
+               .handler = client_widget_key_unset,
+       },
+
+       {
+               .cmd = CMD_STR_GBAR_KEY_SET,
+               .handler = client_gbar_key_set,
+       },
+       {
+               .cmd = CMD_STR_GBAR_KEY_UNSET,
+               .handler = client_gbar_key_unset,
+       },
+
+       {
+               .cmd = CMD_STR_CLIENT_PAUSED,
+               .handler = client_pause_request,
+       },
+       {
+               .cmd = CMD_STR_CLIENT_RESUMED,
+               .handler = client_resume_request,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_ACQUIRE_XPIXMAP,
+               .handler = client_widget_acquire_xpixmap,
+       },
+       {
+               .cmd = CMD_STR_GBAR_ACQUIRE_XPIXMAP,
+               .handler = client_gbar_acquire_xpixmap,
+       },
+       {
+               .cmd = CMD_STR_SUBSCRIBE_CATEGORY,
+               .handler = client_subscribed_category,
+       },
+       {
+               .cmd = CMD_STR_UNSUBSCRIBE_CATEGORY,
+               .handler = client_unsubscribed_category,
+       },
+
+    {
+        .cmd = CMD_STR_HELLO_SYNC,
+        .handler = slave_hello_sync, /* slave_name, ret */
+    },
+    {
+        .cmd = CMD_STR_HELLO_SYNC_PREPARE,
+        .handler = slave_hello_sync_prepare, /* timestamp */
+    },
+
+       {
+               .cmd = NULL,
+               .handler = NULL,
+       },
+};
+
+static struct method s_service_table[] = {
+       {
+               .cmd = CMD_STR_SERVICE_UPDATE,
+               .handler = service_update,
+       },
+       {
+               .cmd = CMD_STR_SERVICE_CHANGE_PERIOD,
+               .handler = service_change_period,
+       },
+       {
+               .cmd = CMD_STR_SERVICE_INST_CNT,
+               .handler = service_instance_count,
+       },
+       {
+               .cmd = CMD_STR_MONITOR_REGISTER,
+               .handler = monitor_register,
+       },
+       {
+               .cmd = CMD_STR_MONITOR_UNREGISTER,
+               .handler = monitor_unregister,
+       },
+       {
+               .cmd = CMD_STR_SERVICE_GET_CONTENT,
+               .handler = service_get_content,
+       },
+       {
+               .cmd = CMD_STR_SERVICE_GET_INST_LIST,
+               .handler = service_get_inst_list,
+       },
+       {
+               .cmd = NULL,
+               .handler = NULL,
+       },
+};
+
+static struct method s_slave_table[] = {
+       {
+               .cmd = CMD_STR_UPDATED,
+               .handler = slave_updated, /* slave_name, pkgname, filename, width, height, ret */
+       },
+       {
+               .cmd = CMD_STR_DESC_UPDATED,
+               .handler = slave_desc_updated, /* slave_name, pkgname, filename, decsfile, ret */
+       },
+       {
+               .cmd = CMD_STR_EXTRA_UPDATED,
+               .handler = slave_extra_updated,
+       },
+       {
+               .cmd = CMD_STR_EXTRA_INFO,
+               .handler = slave_extra_info, /* slave_name, pkgname, filename, priority, content_info, title, icon, name */
+       },
+       {
+               .cmd = CMD_STR_DELETED,
+               .handler = slave_deleted, /* slave_name, pkgname, filename, ret */
+       },
+       {
+               .cmd = CMD_STR_FAULTED,
+               .handler = slave_faulted, /* slave_name, pkgname, id, funcname */
+       },
+       {
+               .cmd = CMD_STR_SCROLL,
+               .handler = slave_hold_scroll, /* slave_name, pkgname, id, seize */
+       },
+
+       {
+               .cmd = CMD_STR_WIDGET_UPDATE_BEGIN,
+               .handler = slave_widget_update_begin,
+       },
+       {
+               .cmd = CMD_STR_WIDGET_UPDATE_END,
+               .handler = slave_widget_update_end,
+       },
+       {
+               .cmd = CMD_STR_GBAR_UPDATE_BEGIN,
+               .handler = slave_gbar_update_begin,
+       },
+       {
+               .cmd = CMD_STR_GBAR_UPDATE_END,
+               .handler = slave_gbar_update_end,
+       },
+
+       {
+               .cmd = CMD_STR_ACCESS_STATUS,
+               .handler = slave_access_status,
+       },
+       {
+               .cmd = CMD_STR_KEY_STATUS,
+               .handler = slave_key_status,
+       },
+       {
+               .cmd = CMD_STR_CLOSE_GBAR,
+               .handler = slave_close_gbar,
+       },
+
+       {
+               .cmd = CMD_STR_CALL,
+               .handler = slave_call, /* slave_name, pkgname, filename, function, ret */
+       },
+       {
+               .cmd = CMD_STR_RET,
+               .handler = slave_ret, /* slave_name, pkgname, filename, function, ret */
+       },
+       {
+               .cmd = CMD_STR_ACQUIRE_BUFFER,
+               .handler = slave_acquire_buffer, /* slave_name, id, w, h, size, - out - type, shmid */
+       },
+       {
+               .cmd = CMD_STR_RESIZE_BUFFER,
+               .handler = slave_resize_buffer,
+       },
+       {
+               .cmd = CMD_STR_RELEASE_BUFFER,
+               .handler = slave_release_buffer, /* slave_name, id - ret */
+       },
+       {
+               .cmd = CMD_STR_HELLO,
+               .handler = slave_hello, /* slave_name, ret */
+       },
+       {
+               .cmd = CMD_STR_PING,
+               .handler = slave_ping, /* slave_name, ret */
+       },
+       {
+               .cmd = CMD_STR_CTRL,
+               .handler = slave_ctrl, /* control bits */
+       },
+
+       {
+               .cmd = CMD_STR_ACQUIRE_XBUFFER,
+               .handler = slave_acquire_extra_buffer,
+       },
+       {
+               .cmd = CMD_STR_RELEASE_XBUFFER,
+               .handler = slave_release_extra_buffer,
+       },
+
+    {
+        .cmd = CMD_STR_HELLO_SYNC,
+        .handler = slave_hello_sync, /* slave_name, ret */
+    },
+    {
+        .cmd = CMD_STR_HELLO_SYNC_PREPARE,
+        .handler = slave_hello_sync_prepare, /* timestamp */
+    },
+
+       {
+               .cmd = NULL,
+               .handler = NULL,
+       },
+};
+
+HAPI int server_init(void)
+{
+       com_core_packet_use_thread(WIDGET_CONF_COM_CORE_THREAD);
+
+       if (unlink(INFO_SOCKET) < 0) {
+               ErrPrint("unlink info: %d\n", errno);
+       }
+
+       if (unlink(SLAVE_SOCKET) < 0) {
+               ErrPrint("unlink slave: %d\n", errno);
+       }
+
+       if (unlink(CLIENT_SOCKET) < 0) {
+               ErrPrint("unlink client: %d\n", errno);
+       }
+
+       if (unlink(SERVICE_SOCKET) < 0) {
+               ErrPrint("unlink service: %d\n", errno);
+       }
+
+       s_info.info_fd = com_core_packet_server_init(INFO_SOCKET, s_info_table);
+       if (s_info.info_fd < 0) {
+               ErrPrint("Failed to create a info socket\n");
+       }
+
+       s_info.slave_fd = com_core_packet_server_init_with_permission(SLAVE_SOCKET, s_slave_table, "data-provider-master::provider");
+       if (s_info.slave_fd < 0) {
+               ErrPrint("Failed to create a slave socket\n");
+       }
+
+       s_info.client_fd = com_core_packet_server_init_with_permission(CLIENT_SOCKET, s_client_table, "data-provider-master::client");
+       if (s_info.client_fd < 0) {
+               ErrPrint("Failed to create a client socket\n");
+       }
+
+       /*!
+        * \note
+        * remote://:8208
+        * Skip address to use the NULL.
+        */
+       s_info.remote_client_fd = com_core_packet_server_init_with_permission("remote://:"CLIENT_PORT, s_client_table, "data-provider-master::client");
+       if (s_info.client_fd < 0) {
+               ErrPrint("Failed to create a remote client socket\n");
+       }
+
+       s_info.service_fd = com_core_packet_server_init_with_permission(SERVICE_SOCKET, s_service_table, "data-provider-master");
+       if (s_info.service_fd < 0) {
+               ErrPrint("Faild to create a service socket\n");
+       }
+
+       if (chmod(INFO_SOCKET, 0600) < 0) {
+               ErrPrint("chmod info: %d\n", errno);
+       }
+
+       if (chmod(SLAVE_SOCKET, 0666) < 0) {
+               ErrPrint("chmod slave: %d\n", errno);
+       }
+
+       if (chmod(CLIENT_SOCKET, 0666) < 0) {
+               ErrPrint("chmod client: %d\n", errno);
+       }
+
+       if (chmod(SERVICE_SOCKET, 0666) < 0) {
+               ErrPrint("chmod service: %d\n", errno);
+       }
+
+       return 0;
+}
+
+HAPI int server_fini(void)
+{
+       if (s_info.info_fd > 0) {
+               com_core_packet_server_fini(s_info.info_fd);
+               s_info.info_fd = -1;
+       }
+
+       if (s_info.slave_fd > 0) {
+               com_core_packet_server_fini(s_info.slave_fd);
+               s_info.slave_fd = -1;
+       }
+
+       if (s_info.client_fd > 0) {
+               com_core_packet_server_fini(s_info.client_fd);
+               s_info.client_fd = -1;
+       }
+
+       if (s_info.remote_client_fd > 0) {
+               com_core_packet_server_fini(s_info.remote_client_fd);
+               s_info.remote_client_fd = -1;
+       }
+
+       if (s_info.service_fd > 0) {
+               com_core_packet_server_fini(s_info.service_fd);
+               s_info.service_fd = -1;
+       }
+
+       return 0;
+}
+
+/* End of a file */
diff --git a/src/service_common.c b/src/service_common.c
new file mode 100644 (file)
index 0000000..d250eba
--- /dev/null
@@ -0,0 +1,1193 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <pthread.h>
+#include <secure_socket.h>
+#include <packet.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/timerfd.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+
+#include <dlog.h>
+#include <Eina.h>
+#include <com-core.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include <lite-errno.h>
+#endif
+
+#include "service_common.h"
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+
+#define EVT_CH         'e'
+#define EVT_END_CH     'x'
+#define DEFAULT_TIMEOUT        2.0f
+
+int errno;
+
+struct service_event_item {
+       enum {
+               SERVICE_EVENT_TIMER
+       } type;
+
+       union {
+               struct {
+                       int fd;
+               } timer;
+       } info;
+
+       int (*event_cb)(struct service_context *svc_cx, void *data);
+       void *cbdata;
+};
+
+struct tcb_event_cbdata {
+       struct tcb *tcb;
+       void (*cb)(struct service_context *svc_ctx, struct tcb *tcb, void *data);
+       void *data;
+};
+
+/*!
+ * \note
+ * Server information and global (only in this file-scope) variables are defined
+ */
+struct service_context {
+       pthread_t server_thid; /*!< Server thread Id */
+       int fd; /*!< Server socket handle */
+
+       Eina_List *tcb_list; /*!< TCB list, list of every thread for client connections */
+       pthread_mutex_t tcb_list_lock;
+
+       Eina_List *packet_list;
+       pthread_mutex_t packet_list_lock;
+       int evt_pipe[PIPE_MAX];
+       int tcb_pipe[PIPE_MAX];
+
+       int (*service_thread_main)(struct tcb *tcb, struct packet *packet, void *data);
+       void *service_thread_data;
+
+       Eina_List *event_list;
+
+       Eina_List *tcb_create_cb_list;
+       Eina_List *tcb_destroy_cb_list;
+};
+
+struct packet_info {
+       struct tcb *tcb;
+       struct packet *packet;
+};
+
+/*!
+ * \note
+ * Thread Control Block
+ * - The main server will create a thread for every client connections.
+ *   When a new client is comming to us, this TCB block will be allocated and initialized.
+ */
+struct tcb { /* Thread controll block */
+       struct service_context *svc_ctx;
+       pthread_t thid; /*!< Thread Id */
+       int fd; /*!< Connection handle */
+       enum tcb_type type;
+       int ctrl_pipe[PIPE_MAX];
+       pid_t pid; /*!< Keep the PID of client, if the client is remote one, this will be -1 */
+};
+
+/*!
+ * Do services for clients
+ * Routing packets to destination processes.
+ * CLIENT THREAD
+ */
+static void *client_packet_pump_main(void *data)
+{
+       struct tcb *tcb = data;
+       struct service_context *svc_ctx = tcb->svc_ctx;
+       struct packet *packet = NULL;
+       fd_set set;
+       char *ptr = NULL;
+       int size = 0;
+       int packet_offset = 0;
+       int recv_offset = 0;
+       long ret;
+       int fd;
+       char evt_ch = EVT_CH;
+       enum {
+               RECV_INIT,
+               RECV_HEADER,
+               RECV_PAYLOAD,
+               RECV_DONE,
+       } recv_state;
+       struct packet_info *packet_info;
+       Eina_List *l;
+
+       ret = 0;
+       recv_state = RECV_INIT;
+       /*!
+        * \note
+        * To escape from the switch statement, we use this ret value
+        */
+       while (ret == 0) {
+               FD_ZERO(&set);
+               FD_SET(tcb->fd, &set);
+               FD_SET(tcb->ctrl_pipe[PIPE_READ], &set);
+               fd = tcb->fd > tcb->ctrl_pipe[PIPE_READ] ? tcb->fd : tcb->ctrl_pipe[PIPE_READ];
+               ret = select(fd + 1, &set, NULL, NULL, NULL);
+               if (ret < 0) {
+                       ret = -errno;
+                       if (errno == EINTR) {
+                               ErrPrint("INTERRUPTED\n");
+                               ret = 0;
+                               continue;
+                       }
+                       ErrPrint("select: %d\n", errno);
+                       DbgFree(ptr);
+                       ptr = NULL;
+                       break;
+               } else if (ret == 0) {
+                       ErrPrint("Timeout\n");
+                       ret = -ETIMEDOUT;
+                       DbgFree(ptr);
+                       ptr = NULL;
+                       break;
+               }
+
+               if (FD_ISSET(tcb->ctrl_pipe[PIPE_READ], &set)) {
+                       DbgPrint("Thread is canceled\n");
+                       ret = -ECANCELED;
+                       DbgFree(ptr);
+                       ptr = NULL;
+                       break;
+               }
+
+               if (!FD_ISSET(tcb->fd, &set)) {
+                       ErrPrint("Unexpected handler is toggled\n");
+                       ret = -EINVAL;
+                       DbgFree(ptr);
+                       ptr = NULL;
+                       break;
+               }
+
+               /*!
+                * \TODO
+                * Service!!! Receive packet & route packet
+                */
+               switch (recv_state) {
+               case RECV_INIT:
+                       size = packet_header_size();
+                       packet_offset = 0;
+                       recv_offset = 0;
+                       packet = NULL;
+                       ptr = malloc(size);
+                       if (!ptr) {
+                               ErrPrint("malloc: %d\n", errno);
+                               ret = -ENOMEM;
+                               break;
+                       }
+                       recv_state = RECV_HEADER;
+                       /* Go through, don't break from here */
+               case RECV_HEADER:
+                       ret = secure_socket_recv(tcb->fd, ptr, size - recv_offset, &tcb->pid);
+                       if (ret <= 0) {
+                               if (ret == 0) {
+                                       ret = -ECANCELED;
+                               }
+                               DbgFree(ptr);
+                               ptr = NULL;
+                               break;
+                       }
+
+                       recv_offset += ret;
+                       ret = 0;
+
+                       if (recv_offset == size) {
+                               packet = packet_build(packet, packet_offset, ptr, size);
+                               DbgFree(ptr);
+                               ptr = NULL;
+                               if (!packet) {
+                                       ret = -EFAULT;
+                                       break;
+                               }
+
+                               packet_offset += recv_offset;
+
+                               size = packet_payload_size(packet);
+                               if (size <= 0) {
+                                       recv_state = RECV_DONE;
+                                       recv_offset = 0;
+                                       break;
+                               }
+
+                               recv_state = RECV_PAYLOAD;
+                               recv_offset = 0;
+
+                               ptr = malloc(size);
+                               if (!ptr) {
+                                       ErrPrint("malloc: %d\n", errno);
+                                       ret = -ENOMEM;
+                               }
+                       }
+                       break;
+               case RECV_PAYLOAD:
+                       ret = secure_socket_recv(tcb->fd, ptr, size - recv_offset, &tcb->pid);
+                       if (ret <= 0) {
+                               if (ret == 0) {
+                                       ret = -ECANCELED;
+                               }
+                               DbgFree(ptr);
+                               ptr = NULL;
+                               break;
+                       }
+
+                       recv_offset += ret;
+                       ret = 0;
+
+                       if (recv_offset == size) {
+                               packet = packet_build(packet, packet_offset, ptr, size);
+                               DbgFree(ptr);
+                               ptr = NULL;
+                               if (!packet) {
+                                       ret = -EFAULT;
+                                       break;
+                               }
+
+                               packet_offset += recv_offset;
+
+                               recv_state = RECV_DONE;
+                               recv_offset = 0;
+                       }
+                       break;
+               case RECV_DONE:
+               default:
+                       /* Dead code */
+                       break;
+               }
+
+               if (recv_state == RECV_DONE) {
+                       /*!
+                        * Push this packet to the packet list with TCB
+                        * Then the service main function will get this.
+                        */
+                       packet_info = malloc(sizeof(*packet_info));
+                       if (!packet_info) {
+                               ret = -errno;
+                               ErrPrint("malloc: %d\n", errno);
+                               packet_destroy(packet);
+                               break;
+                       }
+
+                       packet_info->packet = packet;
+                       packet_info->tcb = tcb;
+
+                       CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+                       svc_ctx->packet_list = eina_list_append(svc_ctx->packet_list, packet_info);
+                       CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+                       if (write(svc_ctx->evt_pipe[PIPE_WRITE], &evt_ch, sizeof(evt_ch)) != sizeof(evt_ch)) {
+                               ret = -errno;
+                               ErrPrint("write: %d\n", errno);
+                               CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+                               svc_ctx->packet_list = eina_list_remove(svc_ctx->packet_list, packet_info);
+                               CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+                               packet_destroy(packet);
+                               DbgFree(packet_info);
+                               ErrPrint("Terminate thread: %p\n", tcb);
+                               break;
+                       } else {
+                               DbgPrint("Packet received: %d bytes\n", packet_offset);
+                               recv_state = RECV_INIT;
+                       }
+
+                       /* Take a breathe */
+                       pthread_yield();
+               }
+       }
+
+       CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+       EINA_LIST_FOREACH(svc_ctx->packet_list, l, packet_info) {
+               if (packet_info->tcb == tcb) {
+                       DbgPrint("Reset ptr of the TCB[%p] in the list of packet info\n", tcb);
+                       packet_info->tcb = NULL;
+               }
+       }
+       CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+       /*!
+        * \note
+        * Emit a signal to collect this TCB from the SERVER THREAD.
+        */
+       if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &tcb, sizeof(tcb)) != sizeof(tcb)) {
+               ErrPrint("write: %d\n", errno);
+       }
+
+       return (void *)ret;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int service_register_tcb_callback(struct service_context *svc_ctx, struct tcb *tcb, enum tcb_event_type event, void (*cb)(struct service_context *svc_ctx, struct tcb *tcb, void *data), void *data)
+{
+       struct tcb_event_cbdata *cbdata;
+
+       cbdata = malloc(sizeof(*cbdata));
+       if (!cbdata) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       cbdata->tcb = tcb;
+       cbdata->cb = cb;
+       cbdata->data = data;
+
+       switch (event) {
+       case TCB_EVENT_CREATE:
+               if (tcb) {
+                       DbgPrint("To catch the create event of TCB does not requires \"tcb\" handle\n");
+               }
+               svc_ctx->tcb_create_cb_list = eina_list_append(svc_ctx->tcb_create_cb_list, cbdata);
+               break;
+       case TCB_EVENT_DESTROY:
+               svc_ctx->tcb_destroy_cb_list = eina_list_append(svc_ctx->tcb_destroy_cb_list, cbdata);
+               break;
+       default:
+               DbgFree(cbdata);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int service_unregister_tcb_callback(struct service_context *svc_ctx, struct tcb *tcb, enum tcb_event_type event, void (*cb)(struct service_context *svc_ctx, struct tcb *tcb, void *data), void *data)
+{
+       struct tcb_event_cbdata *cbdata;
+       Eina_List *l;
+
+       switch (event) {
+       case TCB_EVENT_CREATE:
+               EINA_LIST_FOREACH(svc_ctx->tcb_create_cb_list, l, cbdata) {
+                       if (cbdata->tcb == tcb && cbdata->cb == cb && cbdata->data == data) {
+                               svc_ctx->tcb_create_cb_list = eina_list_remove(svc_ctx->tcb_create_cb_list, cbdata);
+                               DbgFree(cbdata);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case TCB_EVENT_DESTROY:
+               EINA_LIST_FOREACH(svc_ctx->tcb_destroy_cb_list, l, cbdata) {
+                       if (cbdata->tcb == tcb && cbdata->cb == cb && cbdata->data == data) {
+                               svc_ctx->tcb_destroy_cb_list = eina_list_remove(svc_ctx->tcb_destroy_cb_list, cbdata);
+                               DbgFree(cbdata);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+static inline struct tcb *tcb_create(struct service_context *svc_ctx, int fd)
+{
+       struct tcb *tcb;
+       int status;
+       struct tcb_event_cbdata *cbdata;
+       Eina_List *l;
+       Eina_List *n;
+
+       tcb = malloc(sizeof(*tcb));
+       if (!tcb) {
+               ErrPrint("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       if (pipe2(tcb->ctrl_pipe, O_CLOEXEC) < 0) {
+               ErrPrint("pipe2: %d\n", errno);
+               DbgFree(tcb);
+               return NULL;
+       }
+
+       tcb->fd = fd;
+       tcb->svc_ctx = svc_ctx;
+       tcb->type = TCB_CLIENT_TYPE_APP;
+       tcb->pid = -1;
+
+       DbgPrint("Create a new service thread [%d]\n", fd);
+       status = pthread_create(&tcb->thid, NULL, client_packet_pump_main, tcb);
+       if (status != 0) {
+               ErrPrint("pthread_create: %d\n", status);
+               CLOSE_PIPE(tcb->ctrl_pipe);
+               DbgFree(tcb);
+               return NULL;
+       }
+
+       CRITICAL_SECTION_BEGIN(&svc_ctx->tcb_list_lock);
+       svc_ctx->tcb_list = eina_list_append(svc_ctx->tcb_list, tcb);
+       CRITICAL_SECTION_END(&svc_ctx->tcb_list_lock);
+
+       EINA_LIST_FOREACH_SAFE(svc_ctx->tcb_create_cb_list, l, n, cbdata) {
+               if (!cbdata->cb) {
+                       /* ASSERT */
+                       ErrPrint("invalid CB\n");
+                       svc_ctx->tcb_create_cb_list = eina_list_remove(svc_ctx->tcb_create_cb_list, cbdata);
+                       DbgFree(cbdata);
+                       continue;
+               }
+
+               cbdata->cb(svc_ctx, tcb, cbdata->data);
+       }
+
+       return tcb;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+static inline void tcb_teminate_all(struct service_context *svc_ctx)
+{
+       struct tcb *tcb;
+       void *ret;
+       int status;
+       char ch = EVT_END_CH;
+
+       /*!
+        * We don't need to make critical section on here.
+        * If we call this after terminate the server thread first.
+        * Then there is no other thread to access tcb_list.
+        */
+       EINA_LIST_FREE(svc_ctx->tcb_list, tcb) {
+               /*!
+                * ASSERT(tcb->fd >= 0);
+                */
+               if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) {
+                       ErrPrint("write: %d\n", errno);
+               }
+
+               status = pthread_join(tcb->thid, &ret);
+               if (status != 0) {
+                       ErrPrint("Unable to join a thread: %d\n", status);
+               } else {
+                       DbgPrint("Thread returns: %p\n", ret);
+               }
+
+               secure_socket_destroy_handle(tcb->fd);
+
+               CLOSE_PIPE(tcb->ctrl_pipe);
+               DbgFree(tcb);
+       }
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+static inline void tcb_destroy(struct service_context *svc_ctx, struct tcb *tcb)
+{
+       void *ret;
+       int status;
+       char ch = EVT_END_CH;
+       struct tcb_event_cbdata *cbdata;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(svc_ctx->tcb_destroy_cb_list, l, n, cbdata) {
+               if (!cbdata->cb) {
+                       /* ASSERT */
+                       ErrPrint("invalid CB\n");
+                       svc_ctx->tcb_destroy_cb_list = eina_list_remove(svc_ctx->tcb_destroy_cb_list, cbdata);
+                       DbgFree(cbdata);
+                       continue;
+               }
+
+               if (cbdata->tcb != tcb) {
+                       continue;
+               }
+
+               cbdata->cb(svc_ctx, tcb, cbdata->data);
+
+               if (eina_list_data_find(svc_ctx->tcb_destroy_cb_list, cbdata)) {
+                       svc_ctx->tcb_destroy_cb_list = eina_list_remove(svc_ctx->tcb_destroy_cb_list, cbdata);
+                       DbgFree(cbdata);
+               }
+       }
+
+       CRITICAL_SECTION_BEGIN(&svc_ctx->tcb_list_lock);
+       svc_ctx->tcb_list = eina_list_remove(svc_ctx->tcb_list, tcb);
+       CRITICAL_SECTION_END(&svc_ctx->tcb_list_lock);
+       /*!
+        * ASSERT(tcb->fd >= 0);
+        * Close the connection, and then collecting the return value of thread
+        */
+       if (write(tcb->ctrl_pipe[PIPE_WRITE], &ch, sizeof(ch)) != sizeof(ch)) {
+               ErrPrint("write: %d\n", errno);
+       }
+
+       status = pthread_join(tcb->thid, &ret);
+       if (status != 0) {
+               ErrPrint("Unable to join a thread: %d\n", status);
+       } else {
+               DbgPrint("Thread returns: %p\n", ret);
+       }
+
+       secure_socket_destroy_handle(tcb->fd);
+
+       CLOSE_PIPE(tcb->ctrl_pipe);
+       DbgFree(tcb);
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+static inline int update_fdset(struct service_context *svc_ctx, fd_set *set)
+{
+       Eina_List *l;
+       struct service_event_item *item;
+       int fd = 0;
+
+       FD_ZERO(set);
+
+       FD_SET(svc_ctx->fd, set);
+       fd = svc_ctx->fd;
+
+       FD_SET(svc_ctx->tcb_pipe[PIPE_READ], set);
+       if (svc_ctx->tcb_pipe[PIPE_READ] > fd) {
+               fd = svc_ctx->tcb_pipe[PIPE_READ];
+       }
+
+       FD_SET(svc_ctx->evt_pipe[PIPE_READ], set);
+       if (svc_ctx->evt_pipe[PIPE_READ] > fd) {
+               fd = svc_ctx->evt_pipe[PIPE_READ];
+       }
+
+       EINA_LIST_FOREACH(svc_ctx->event_list, l, item) {
+               if (item->type == SERVICE_EVENT_TIMER) {
+                       FD_SET(item->info.timer.fd, set);
+                       if (fd < item->info.timer.fd) {
+                               fd = item->info.timer.fd;
+                       }
+               }
+       }
+
+       return fd + 1;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+static inline void processing_timer_event(struct service_context *svc_ctx, fd_set *set)
+{
+       uint64_t expired_count;
+       Eina_List *l;
+       Eina_List *n;
+       struct service_event_item *item;
+
+       EINA_LIST_FOREACH_SAFE(svc_ctx->event_list, l, n, item) {
+               switch (item->type) {
+               case SERVICE_EVENT_TIMER:
+                       if (!FD_ISSET(item->info.timer.fd, set)) {
+                               break;
+                       }
+
+                       if (read(item->info.timer.fd, &expired_count, sizeof(expired_count)) == sizeof(expired_count)) {
+                               DbgPrint("Expired %d times\n", expired_count);
+                               if (item->event_cb(svc_ctx, item->cbdata) >= 0) {
+                                       break;
+                               }
+                       } else {
+                               ErrPrint("read: %d\n", errno);
+                       }
+
+                       if (!eina_list_data_find(svc_ctx->event_list, item)) {
+                               break;
+                       }
+
+                       svc_ctx->event_list = eina_list_remove(svc_ctx->event_list, item);
+                       if (close(item->info.timer.fd) < 0) {
+                               ErrPrint("close: %d\n", errno);
+                       }
+                       DbgFree(item);
+                       break;
+               default:
+                       ErrPrint("Unknown event: %d\n", item->type);
+                       break;
+               }
+       }
+}
+
+/*!
+ * Accept new client connections
+ * And create a new thread for service.
+ *
+ * Create Client threads & Destroying them
+ * SERVER THREAD
+ */
+static void *server_main(void *data)
+{
+       struct service_context *svc_ctx = data;
+       fd_set set;
+       fd_set except_set;
+       long ret;
+       int client_fd;
+       struct tcb *tcb;
+       int fd;
+       char evt_ch;
+       struct packet_info *packet_info;
+
+       DbgPrint("Server thread is activated\n");
+       while (1) {
+               fd = update_fdset(svc_ctx, &set);
+               memcpy(&except_set, &set, sizeof(set));
+
+               ret = select(fd, &set, NULL, &except_set, NULL);
+               if (ret < 0) {
+                       ret = -errno;
+                       if (errno == EINTR) {
+                               DbgPrint("INTERRUPTED\n");
+                               continue;
+                       }
+                       ErrPrint("select: %d\n", errno);
+                       break;
+               } else if (ret == 0) {
+                       ErrPrint("Timeout\n");
+                       ret = -ETIMEDOUT;
+                       break;
+               }
+
+               if (FD_ISSET(svc_ctx->fd, &set)) {
+                       client_fd = secure_socket_get_connection_handle(svc_ctx->fd);
+                       if (client_fd < 0) {
+                               ErrPrint("Failed to establish a new connection [%d]\n", svc_ctx->fd);
+                               ret = -EFAULT;
+                               break;
+                       }
+
+                       tcb = tcb_create(svc_ctx, client_fd);
+                       if (!tcb) {
+                               ErrPrint("Failed to create a new TCB: %d (%d)\n", client_fd, svc_ctx->fd);
+                               secure_socket_destroy_handle(client_fd);
+                       }
+               }
+
+               if (FD_ISSET(svc_ctx->evt_pipe[PIPE_READ], &set)) {
+                       if (read(svc_ctx->evt_pipe[PIPE_READ], &evt_ch, sizeof(evt_ch)) != sizeof(evt_ch)) {
+                               ErrPrint("read: %d\n", errno);
+                               ret = -EFAULT;
+                               break;
+                       }
+
+                       CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+                       packet_info = eina_list_nth(svc_ctx->packet_list, 0);
+                       svc_ctx->packet_list = eina_list_remove(svc_ctx->packet_list, packet_info);
+                       CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+                       if (packet_info) {
+                               /*!
+                                * \CRITICAL
+                                * What happens if the client thread is terminated, so the packet_info->tcb is deleted
+                                * while processing svc_ctx->service_thread_main?
+                                */
+                               ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data);
+                               if (ret < 0) {
+                                       ErrPrint("Service thread returns: %d\n", ret);
+                               }
+
+                               packet_destroy(packet_info->packet);
+                               DbgFree(packet_info);
+                       }
+
+                       /* Take a breathe */
+                       pthread_yield();
+               }
+
+               processing_timer_event(svc_ctx, &set);
+
+               /*!
+                * \note
+                * Destroying TCB should be processed at last.
+                */
+               if (FD_ISSET(svc_ctx->tcb_pipe[PIPE_READ], &set)) {
+                       Eina_List *lockfree_packet_list;
+                       Eina_List *l;
+                       Eina_List *n;
+
+                       if (read(svc_ctx->tcb_pipe[PIPE_READ], &tcb, sizeof(tcb)) != sizeof(tcb)) {
+                               ErrPrint("read: %d\n", errno);
+                               ret = -EFAULT;
+                               break;
+                       }
+
+                       if (!tcb) {
+                               ErrPrint("Terminate service thread\n");
+                               ret = -ECANCELED;
+                               break;
+                       }
+
+                       lockfree_packet_list = NULL;
+                       CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+                       EINA_LIST_FOREACH_SAFE(svc_ctx->packet_list, l, n, packet_info) {
+                               if (packet_info->tcb != tcb) {
+                                       continue;
+                               }
+
+                               svc_ctx->packet_list = eina_list_remove(svc_ctx->packet_list, packet_info);
+                               lockfree_packet_list = eina_list_append(lockfree_packet_list, packet_info);
+                       }
+                       CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+                       EINA_LIST_FREE(lockfree_packet_list, packet_info) {
+                               ret = read(svc_ctx->evt_pipe[PIPE_READ], &evt_ch, sizeof(evt_ch));
+                               DbgPrint("Flushing filtered pipe: %d (%c)\n", ret, evt_ch);
+                               ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data);
+                               if (ret < 0) {
+                                       ErrPrint("Service thread returns: %d\n", ret);
+                               }
+                               packet_destroy(packet_info->packet);
+                               DbgFree(packet_info);
+                       }
+
+                       /*!
+                        * \note
+                        * Invoke the service thread main, to notify the termination of a TCB
+                        */
+                       ret = svc_ctx->service_thread_main(tcb, NULL, svc_ctx->service_thread_data);
+
+                       /*!
+                        * at this time, the client thread can access this tcb.
+                        * how can I protect this TCB from deletion without disturbing the server thread?
+                        */
+                       tcb_destroy(svc_ctx, tcb);
+               }
+
+               /* If there is no such triggered FD? */
+       }
+
+       /*!
+        * Consuming all pended packets before terminates server thread.
+        *
+        * If the server thread is terminated, we should flush all pended packets.
+        * And we should services them.
+        * While processing this routine, the mutex is locked.
+        * So every other client thread will be slowed down, sequently, every clients can meet problems.
+        * But in case of termination of server thread, there could be systemetic problem.
+        * This only should be happenes while terminating the master daemon process.
+        */
+       CRITICAL_SECTION_BEGIN(&svc_ctx->packet_list_lock);
+       EINA_LIST_FREE(svc_ctx->packet_list, packet_info) {
+               ret = read(svc_ctx->evt_pipe[PIPE_READ], &evt_ch, sizeof(evt_ch));
+               DbgPrint("Flushing pipe: %d (%c)\n", ret, evt_ch);
+               ret = svc_ctx->service_thread_main(packet_info->tcb, packet_info->packet, svc_ctx->service_thread_data);
+               if (ret < 0) {
+                       ErrPrint("Service thread returns: %d\n", ret);
+               }
+               packet_destroy(packet_info->packet);
+               DbgFree(packet_info);
+       }
+       CRITICAL_SECTION_END(&svc_ctx->packet_list_lock);
+
+       tcb_teminate_all(svc_ctx);
+       return (void *)ret;
+}
+
+/*!
+ * \NOTE
+ * MAIN THREAD
+ */
+HAPI struct service_context *service_common_create(const char *addr, const char *label, int (*service_thread_main)(struct tcb *tcb, struct packet *packet, void *data), void *data)
+{
+       int status;
+       struct service_context *svc_ctx;
+
+       if (!service_thread_main || !addr) {
+               ErrPrint("Invalid argument\n");
+               return NULL;
+       }
+
+       if (strncmp(addr, COM_CORE_REMOTE_SCHEME, strlen(COM_CORE_REMOTE_SCHEME))) {
+               int offset = 0;
+
+               offset = strlen(COM_CORE_LOCAL_SCHEME);
+               if (strncmp(addr, COM_CORE_LOCAL_SCHEME, offset)) {
+                       offset = 0;
+               }
+
+               if (unlink(addr + offset) < 0) {
+                       ErrPrint("unlink [%s] - %d\n", addr, errno);
+               }
+       }
+
+       svc_ctx = calloc(1, sizeof(*svc_ctx));
+       if (!svc_ctx) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       svc_ctx->fd = secure_socket_create_server_with_permission(addr, label);
+       if (svc_ctx->fd < 0) {
+               DbgFree(svc_ctx);
+               return NULL;
+       }
+
+       svc_ctx->service_thread_main = service_thread_main;
+       svc_ctx->service_thread_data = data;
+
+       if (fcntl(svc_ctx->fd, F_SETFD, FD_CLOEXEC) < 0) {
+               ErrPrint("fcntl: %d\n", errno);
+       }
+
+       if (fcntl(svc_ctx->fd, F_SETFL, O_NONBLOCK) < 0) {
+               ErrPrint("fcntl: %d\n", errno);
+       }
+
+       if (pipe2(svc_ctx->evt_pipe, O_CLOEXEC) < 0) {
+               ErrPrint("pipe2: %d\n", errno);
+               secure_socket_destroy_handle(svc_ctx->fd);
+               DbgFree(svc_ctx);
+               return NULL;
+       }
+
+       if (pipe2(svc_ctx->tcb_pipe, O_CLOEXEC) < 0) {
+               ErrPrint("pipe2: %d\n", errno);
+               CLOSE_PIPE(svc_ctx->evt_pipe);
+               secure_socket_destroy_handle(svc_ctx->fd);
+               DbgFree(svc_ctx);
+               return NULL;
+       }
+
+       status = pthread_mutex_init(&svc_ctx->packet_list_lock, NULL);
+       if (status != 0) {
+               ErrPrint("Unable to create a mutex: %d\n", status);
+               CLOSE_PIPE(svc_ctx->evt_pipe);
+               CLOSE_PIPE(svc_ctx->tcb_pipe);
+               secure_socket_destroy_handle(svc_ctx->fd);
+               DbgFree(svc_ctx);
+               return NULL;
+       }
+
+       DbgPrint("Creating server thread\n");
+       status = pthread_create(&svc_ctx->server_thid, NULL, server_main, svc_ctx);
+       if (status != 0) {
+               ErrPrint("Unable to create a thread for shortcut service: %d\n", status);
+               status = pthread_mutex_destroy(&svc_ctx->packet_list_lock);
+               if (status != 0) {
+                       ErrPrint("mutex_destroy: %d\n", status);
+               }
+               CLOSE_PIPE(svc_ctx->evt_pipe);
+               CLOSE_PIPE(svc_ctx->tcb_pipe);
+               secure_socket_destroy_handle(svc_ctx->fd);
+               DbgFree(svc_ctx);
+               return NULL;
+       }
+
+       /*!
+        * \note
+        * To give a chance to run for server thread.
+        */
+       pthread_yield();
+
+       return svc_ctx;
+}
+
+/*!
+ * \note
+ * MAIN THREAD
+ */
+HAPI int service_common_destroy(struct service_context *svc_ctx)
+{
+       int status = 0;
+       void *ret;
+
+       if (!svc_ctx) {
+               return -EINVAL;
+       }
+
+       /*!
+        * \note
+        * Terminate server thread
+        */
+       if (write(svc_ctx->tcb_pipe[PIPE_WRITE], &status, sizeof(status)) != sizeof(status)) {
+               ErrPrint("write: %d\n", errno);
+       }
+
+       status = pthread_join(svc_ctx->server_thid, &ret);
+       if (status != 0) {
+               ErrPrint("Join: %d\n", status);
+       } else {
+               DbgPrint("Thread returns: %p\n", ret);
+       }
+
+       secure_socket_destroy_handle(svc_ctx->fd);
+
+       status = pthread_mutex_destroy(&svc_ctx->packet_list_lock);
+       if (status != 0) {
+               ErrPrint("destroy_mutex: %d\n", status);
+       }
+
+       CLOSE_PIPE(svc_ctx->evt_pipe);
+       CLOSE_PIPE(svc_ctx->tcb_pipe);
+       DbgFree(svc_ctx);
+       return 0;
+}
+
+/*!
+ * \note
+ * SERVER THREAD or OTHER THREAD (not main)
+ */
+HAPI int tcb_is_valid(struct service_context *svc_ctx, struct tcb *tcb)
+{
+       Eina_List *l;
+       struct tcb *tmp;
+       int ret = -ENOENT;
+
+       CRITICAL_SECTION_BEGIN(&svc_ctx->tcb_list_lock);
+       EINA_LIST_FOREACH(svc_ctx->tcb_list, l, tmp) {
+               if (tmp == tcb /* && tcb->svc_ctx == svc_ctx */) {
+                       ret = tcb->fd;
+                       break;
+               }
+       }
+       CRITICAL_SECTION_END(&svc_ctx->tcb_list_lock);
+
+       return ret;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int tcb_pid(struct tcb *tcb)
+{
+       if (!tcb) {
+               return -1;
+       }
+
+       return tcb->pid;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int tcb_fd(struct tcb *tcb)
+{
+       if (!tcb) {
+               return -EINVAL;
+       }
+
+       return tcb->fd;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int tcb_client_type(struct tcb *tcb)
+{
+       if (!tcb) {
+               return -EINVAL;
+       }
+
+       return tcb->type;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int tcb_client_type_set(struct tcb *tcb, enum tcb_type type)
+{
+       if (!tcb) {
+               return -EINVAL;
+       }
+
+       DbgPrint("TCB[%p] Client type is changed to %d from %d\n", tcb, type, tcb->type);
+       tcb->type = type;
+       return 0;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI struct service_context *tcb_svc_ctx(struct tcb *tcb)
+{
+       if (!tcb) {
+               return NULL;
+       }
+
+       return tcb->svc_ctx;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int service_common_unicast_packet(struct tcb *tcb, struct packet *packet)
+{
+       if (!tcb || !packet) {
+               DbgPrint("Invalid unicast: tcb[%p], packet[%p]\n", tcb, packet);
+               return -EINVAL;
+       }
+
+       DbgPrint("Unicast packet\n");
+       return com_core_send(tcb->fd, (void *)packet_data(packet), packet_size(packet), DEFAULT_TIMEOUT);
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int service_common_multicast_packet(struct tcb *tcb, struct packet *packet, int type)
+{
+       Eina_List *l;
+       struct tcb *target;
+       struct service_context *svc_ctx;
+       int ret;
+
+       if (!tcb || !packet) {
+               DbgPrint("Invalid multicast: tcb[%p], packet[%p]\n", tcb, packet);
+               return -EINVAL;
+       }
+
+       svc_ctx = tcb->svc_ctx;
+
+       DbgPrint("Multicasting packets\n");
+
+       /*!
+        * \note
+        * Does not need to make a critical section from here.
+        */
+       EINA_LIST_FOREACH(svc_ctx->tcb_list, l, target) {
+               if (target == tcb || target->type != type) {
+                       DbgPrint("Skip target: %p(%d) == %p/%d\n", target, target->type, tcb, type);
+                       continue;
+               }
+
+               ret = com_core_send(target->fd, (void *)packet_data(packet), packet_size(packet), DEFAULT_TIMEOUT);
+               if (ret < 0) {
+                       ErrPrint("Failed to send packet: %d\n", ret);
+               }
+       }
+       DbgPrint("Finish to multicast packet\n");
+       return 0;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI struct service_event_item *service_common_add_timer(struct service_context *svc_ctx, double timer, int (*timer_cb)(struct service_context *svc_cx, void *data), void *data)
+{
+       struct service_event_item *item;
+
+       item = calloc(1, sizeof(*item));
+       if (!item) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       item->type = SERVICE_EVENT_TIMER;
+       item->info.timer.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
+       if (item->info.timer.fd < 0) {
+               ErrPrint("timerfd_create: %d\n", errno);
+               DbgFree(item);
+               return NULL;
+       }
+
+       if (service_common_update_timer(item, timer) < 0) {
+               if (close(item->info.timer.fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+               DbgFree(item);
+               return NULL;
+       }
+
+       item->event_cb = timer_cb;
+       item->cbdata = data;
+
+       svc_ctx->event_list = eina_list_append(svc_ctx->event_list, item);
+       return item;
+}
+
+HAPI int service_common_update_timer(struct service_event_item *item, double timer)
+{
+       struct itimerspec spec;
+
+       spec.it_interval.tv_sec = (time_t)timer;
+       spec.it_interval.tv_nsec = (timer - spec.it_interval.tv_sec) * 1000000000;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &spec.it_value) < 0) {
+               ErrPrint("clock_gettime: %d\n", errno);
+               return -EFAULT;
+       }
+
+       spec.it_value.tv_sec += spec.it_interval.tv_sec;
+       spec.it_value.tv_nsec += spec.it_interval.tv_nsec;
+
+       if (timerfd_settime(item->info.timer.fd, TFD_TIMER_ABSTIME, &spec, NULL) < 0) {
+               ErrPrint("timerfd_settime: %d\n", errno);
+               return -EFAULT;
+       }
+
+       DbgPrint("Armed interval: %u %u\n", spec.it_interval.tv_sec, spec.it_interval.tv_nsec);
+       return 0;
+}
+
+/*!
+ * \note
+ * SERVER THREAD
+ */
+HAPI int service_common_del_timer(struct service_context *svc_ctx, struct service_event_item *item)
+{
+       if (!eina_list_data_find(svc_ctx->event_list, item)) {
+               ErrPrint("Invalid event item\n");
+               return -EINVAL;
+       }
+
+       svc_ctx->event_list = eina_list_remove(svc_ctx->event_list, item);
+
+       if (close(item->info.timer.fd) < 0) {
+               ErrPrint("close: %d\n", errno);
+       }
+       DbgFree(item);
+       return 0;
+}
+
+HAPI int service_common_fd(struct service_context *ctx)
+{
+       return ctx->fd;
+}
+
+/* End of a file */
diff --git a/src/setting.c b/src/setting.c
new file mode 100644 (file)
index 0000000..11dd052
--- /dev/null
@@ -0,0 +1,333 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <malloc.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <locale.h>
+
+#include <vconf.h>
+#include <dlog.h>
+
+#include <Ecore.h>
+#include <Eina.h>
+
+#include "setting.h"
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+#include "critical_log.h"
+
+#if defined(HAVE_LIVEBOX)
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_conf.h>
+#include <widget_errno.h>
+#include "client_life.h"
+#include "slave_life.h"
+#include "xmonitor.h"
+#include "package.h"
+#include "instance.h"
+#else
+#define xmonitor_handle_state_changes()
+#endif
+
+int errno;
+
+struct event_cbdata {
+       int (*handler)(enum oom_event_type type, void *data);
+       void *data;
+       int deleted;
+};
+
+static struct {
+       int deactivated;
+       Eina_List *oom_event_list;
+       int oom_event_in_process;
+} s_info = {
+       .deactivated = 0,
+       .oom_event_list = NULL,
+       .oom_event_in_process = 0,
+};
+
+static void lcd_state_cb(keynode_t *node, void *user_data)
+{
+       if (!node) {
+               return;
+       }
+
+       xmonitor_handle_state_changes();
+}
+
+HAPI int setting_is_lcd_off(void)
+{
+       int state;
+
+       if (!WIDGET_CONF_CHECK_LCD) {
+               /* Always turned on */
+               return 0;
+       }
+
+       if (vconf_get_int(VCONFKEY_PM_STATE, &state) != 0) {
+               ErrPrint("Idle lock state is not valid\n");
+               state = VCONFKEY_PM_STATE_NORMAL; /* UNLOCK */
+       }
+
+       ErrPrint("State: %d, (%d:lcdoff, %d:sleep)\n", state, VCONFKEY_PM_STATE_LCDOFF, VCONFKEY_PM_STATE_SLEEP);
+       return state == VCONFKEY_PM_STATE_LCDOFF || state == VCONFKEY_PM_STATE_SLEEP;
+}
+
+static void power_off_cb(keynode_t *node, void *user_data)
+{
+       int val;
+       CRITICAL_LOG("Terminated(vconf)\n");
+
+       if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) != 0) {
+               ErrPrint("Failed to get power off status (%d)\n", val);
+               return;
+       }
+
+       if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
+               DbgPrint("Power off requested: Ignored\n");
+       } else {
+               ErrPrint("Unknown power state: %d\n", val);
+       }
+}
+
+static void region_changed_cb(keynode_t *node, void *user_data)
+{
+       char *region;
+       char *r;
+
+       region = vconf_get_str(VCONFKEY_REGIONFORMAT);
+       if (!region) {
+               return;
+       }
+
+       setenv("LC_CTYPE", region, 1);
+       setenv("LC_NUMERIC", region, 1);
+       setenv("LC_TIME", region, 1);
+       setenv("LC_COLLATE", region, 1);
+       setenv("LC_MONETARY", region, 1);
+       setenv("LC_PAPER", region, 1);
+       setenv("LC_NAME", region, 1);
+       setenv("LC_ADDRESS", region, 1);
+       setenv("LC_TELEPHONE", region, 1);
+       setenv("LC_MEASUREMENT", region, 1);
+       setenv("LC_IDENTIFICATION", region, 1);
+
+       r = setlocale(LC_ALL, "");
+       if (r == NULL) {
+               ErrPrint("Failed to change region\n");
+       }
+
+       DbgFree(region);
+}
+
+static void lang_changed_cb(keynode_t *node, void *user_data)
+{
+       char *lang;
+       char *r;
+
+       lang = vconf_get_str(VCONFKEY_LANGSET);
+       if (!lang) {
+               return;
+       }
+
+       setenv("LANG", lang, 1);
+       setenv("LC_MESSAGES", lang, 1);
+
+       r = setlocale(LC_ALL, "");
+       if (!r) {
+               ErrPrint("Failed to change locale\n");
+       }
+
+       DbgPrint("Locale: %s\n", setlocale(LC_ALL, NULL));
+       DbgFree(lang);
+}
+
+static void low_mem_cb(keynode_t *node, void *user_data)
+{
+       int val;
+       Eina_List *l;
+       Eina_List *n;
+       struct event_cbdata *item;
+
+       val = vconf_keynode_get_int(node);
+
+       if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)     {
+               CRITICAL_LOG("Low memory: level %d\n", val);
+               if (s_info.deactivated == 0) {
+                       s_info.deactivated = 1;
+
+                       s_info.oom_event_in_process = 1;
+                       EINA_LIST_FOREACH_SAFE(s_info.oom_event_list, l, n, item) {
+                               if (item->deleted || item->handler(OOM_TYPE_LOW, item->data) < 0 || item->deleted) {
+                                       s_info.oom_event_list = eina_list_remove(s_info.oom_event_list, item);
+                                       free(item);
+                               }
+                       }
+                       s_info.oom_event_in_process = 0;
+
+                       //slave_deactivate_all(0, 1, 0);
+                       malloc_trim(0);
+                       ErrPrint("Fall into the low mem status\n");
+               }
+       } else {
+               CRITICAL_LOG("Normal memory: level %d\n", val);
+               if (s_info.deactivated == 1) {
+                       s_info.deactivated = 0;
+
+                       s_info.oom_event_in_process = 1;
+                       EINA_LIST_FOREACH_SAFE(s_info.oom_event_list, l, n, item) {
+                               if (item->deleted || item->handler(OOM_TYPE_NORMAL, item->data) < 0 || item->deleted) {
+                                       s_info.oom_event_list = eina_list_remove(s_info.oom_event_list, item);
+                                       free(item);
+                               }
+                       }
+                       s_info.oom_event_in_process = 0;
+
+                       //slave_activate_all();
+                       ErrPrint("Recover from the low mem status\n");
+               }
+       }
+}
+
+HAPI int setting_add_oom_event_callback(int (*handler)(enum oom_event_type type, void *data), void *data)
+{
+       struct event_cbdata *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->handler = handler;
+       item->data = data;
+       item->deleted = 0;
+
+       s_info.oom_event_list = eina_list_append(s_info.oom_event_list, item);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int setting_del_oom_event_callback(int (*handler)(enum oom_event_type type, void *data), void *data)
+{
+       struct event_cbdata *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(s_info.oom_event_list, l, n, item) {
+               if (handler == item->handler && item->data == data) {
+                       if (s_info.oom_event_in_process) {
+                               item->deleted = 1;
+                       } else {
+                               s_info.oom_event_list = eina_list_remove(s_info.oom_event_list, item);
+                               free(item);
+                       }
+                       return WIDGET_ERROR_NONE;
+               }
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI enum oom_event_type setting_oom_level(void)
+{
+       int ret;
+       int status;
+
+       ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status);
+       if (ret == 0 && status >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
+               return OOM_TYPE_LOW;
+       }
+
+       return OOM_TYPE_NORMAL;
+}
+
+HAPI int setting_init(void)
+{
+       int ret;
+
+       ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_cb, NULL);
+       if (ret < 0) {
+               ErrPrint("Failed to add vconf for lock state: %d\n", ret);
+       }
+
+       ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb, NULL);
+       if (ret < 0) {
+               ErrPrint("Failed to add vconf for power state: %d \n", ret);
+       }
+
+       ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_changed_cb, NULL);
+       if (ret < 0) {
+               ErrPrint("Failed to add vconf for lang change: %d\n", ret);
+       }
+
+       ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb, NULL);
+       if (ret < 0) {
+               ErrPrint("Failed to add vconf for region change: %d\n", ret);
+       }
+
+       ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb, NULL);
+       if (ret < 0) {
+               ErrPrint("Failed to add vconf for low mem monitor: %d\n", ret);
+       }
+
+       lang_changed_cb(NULL, NULL);
+       region_changed_cb(NULL, NULL);
+       return ret;
+}
+
+HAPI int setting_fini(void)
+{
+       int ret;
+
+       ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, low_mem_cb);
+       if (ret < 0) {
+               ErrPrint("Failed to ignore vconf key (%d)\n", ret);
+       }
+
+       ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, region_changed_cb);
+       if (ret < 0) {
+               ErrPrint("Failed to ignore vconf key (%d)\n", ret);
+       }
+
+       ret = vconf_ignore_key_changed(VCONFKEY_LANGSET, lang_changed_cb);
+       if (ret < 0) {
+               ErrPrint("Failed to ignore vconf key (%d)\n", ret);
+       }
+
+       ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE, lcd_state_cb);
+       if (ret < 0) {
+               ErrPrint("Failed to ignore vconf key (%d)\n", ret);
+       }
+
+       ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, power_off_cb);
+       if (ret < 0) {
+               ErrPrint("Failed to ignore vconf key (%d)\n", ret);
+       }
+
+       return ret;
+}
+
+/* End of a file */
diff --git a/src/shortcut_service.c b/src/shortcut_service.c
new file mode 100644 (file)
index 0000000..fa675f2
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include "lite-errno.h"
+#endif
+#include <packet.h>
+
+#include <Eina.h>
+#include <sys/smack.h>
+
+#include <security-server.h>
+#include <shortcut.h>
+
+#include "service_common.h"
+#include "debug.h"
+#include "util.h"
+#include "conf.h"
+
+static struct info {
+       Eina_List *context_list;
+       struct service_context *svc_ctx;
+} s_info = {
+       .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
+       .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
+};
+
+struct context {
+       struct tcb *tcb;
+       double seq;
+};
+
+/*!
+ * SERVICE THREAD
+ */
+static inline int put_reply_context(struct tcb *tcb, double seq)
+{
+       struct context *ctx;
+
+       ctx = malloc(sizeof(*ctx));
+       if (!ctx) {
+               ErrPrint("malloc: %d\n", errno);
+               return -ENOMEM;
+       }
+
+       ctx->tcb = tcb;
+       ctx->seq = seq; /* Could we this sequence value is uniq? */
+
+       s_info.context_list = eina_list_append(s_info.context_list, ctx);
+       return 0;
+}
+
+/*!
+ * SERVICE THREAD
+ */
+static inline struct tcb *get_reply_context(double seq)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct context *ctx;
+       struct tcb *tcb;
+
+       tcb = NULL;
+       EINA_LIST_FOREACH_SAFE(s_info.context_list, l, n, ctx) {
+               if (ctx->seq != seq) {
+                       continue;
+               }
+
+               s_info.context_list = eina_list_remove(s_info.context_list, ctx);
+               tcb = ctx->tcb;
+               DbgFree(ctx);
+               break;
+       }
+
+       return tcb;
+}
+
+static void send_reply_packet(struct tcb *tcb, struct packet *packet, int ret)
+{
+       struct packet *reply_packet;
+
+       reply_packet = packet_create_reply(packet, "i", ret);
+       if (!reply_packet) {
+               ErrPrint("Failed to create a packet\n");
+               return;
+       }
+
+       if (service_common_unicast_packet(tcb, reply_packet) < 0) {
+               ErrPrint("Unable to send reply packet\n");
+       }
+
+       packet_destroy(reply_packet);
+}
+
+/*!
+ * SERVICE THREAD
+ */
+static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
+{
+       const char *command;
+       int ret;
+
+       if (!packet) {
+               DbgPrint("TCB: %p is terminated (NIL packet)\n", tcb);
+               return 0;
+       }
+
+       command = packet_command(packet);
+       if (!command) {
+               ErrPrint("Invalid command\n");
+               return -EINVAL;
+       }
+
+       switch (packet_type(packet)) {
+       case PACKET_REQ:
+
+               /* Need to send reply packet */
+               DbgPrint("%p REQ: Command: [%s]\n", tcb, command);
+               if (!strcmp(command, "add_shortcut_widget") || !strcmp(command, "rm_shortcut_widget")) {
+                       ret = security_server_check_privilege_by_sockfd(tcb_fd(tcb), "data-provider-master::shortcut.widget", "w");
+                       if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+                               ErrPrint("SMACK:Access denied\n");
+                               send_reply_packet(tcb, packet, SHORTCUT_ERROR_PERMISSION_DENIED);
+                               break;
+                       }
+
+               } else if (!strcmp(command, "add_shortcut") || !strcmp(command, "rm_shortcut")) {
+                       ret = security_server_check_privilege_by_sockfd(tcb_fd(tcb), "data-provider-master::shortcut.shortcut", "w");
+                       if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+                               ErrPrint("SMACK:Access denied\n");
+                               send_reply_packet(tcb, packet, SHORTCUT_ERROR_PERMISSION_DENIED);
+                               break;
+                       }
+               }
+
+               if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0) {
+                       ErrPrint("Unable to send service request packet\n");
+               } else {
+                       (void)put_reply_context(tcb, packet_seq(packet));
+               }
+               break;
+       case PACKET_REQ_NOACK:
+               /* Doesn't need to send reply packet */
+               DbgPrint("%p REQ_NOACK: Command: [%s]\n", tcb, command);
+               if (!strcmp(command, "service_register")) {
+                       tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
+                       break;
+               }
+
+               if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0) {
+                       ErrPrint("Unable to send service reuqest packet\n");
+               }
+               break;
+       case PACKET_ACK:
+               /* Okay, client(or app) send a reply packet to us. */
+               DbgPrint("%p ACK: Command: [%s]\n", tcb, command);
+               tcb = get_reply_context(packet_seq(packet));
+               if (!tcb) {
+                       ErrPrint("There is no proper context\n");
+                       break;
+               }
+
+               if (tcb_is_valid(s_info.svc_ctx, tcb) < 0) {
+                       ErrPrint("TCB is not valid (already disconnected?)\n");
+                       break;
+               }
+
+               if (service_common_unicast_packet(tcb, packet) < 0) {
+                       ErrPrint("Unable to send reply packet\n");
+               }
+               break;
+       default:
+               ErrPrint("Packet type is not valid[%s]\n", command);
+               return -EINVAL;
+       }
+
+       /*!
+        * return value has no meanning,
+        * it will be printed by dlogutil.
+        */
+       return 0;
+}
+
+
+/*!
+ * MAIN THREAD
+ * Do not try to do anyother operation in these functions
+ */
+
+HAPI int shortcut_service_init(void)
+{
+       if (s_info.svc_ctx) {
+               ErrPrint("Already initialized\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       s_info.svc_ctx = service_common_create(SHORTCUT_SOCKET, SHORTCUT_SMACK_LABEL, service_thread_main, NULL);
+       if (!s_info.svc_ctx) {
+               ErrPrint("Unable to activate service thread\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Successfully initiated\n");
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int shortcut_service_fini(void)
+{
+       if (!s_info.svc_ctx) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       service_common_destroy(s_info.svc_ctx);
+       s_info.svc_ctx = NULL;
+       DbgPrint("Successfully Finalized\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/slave_life.c b/src/slave_life.c
new file mode 100644 (file)
index 0000000..40db882
--- /dev/null
@@ -0,0 +1,2000 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h> /* strerror */
+#include <errno.h> /* errno */
+#include <unistd.h> /* pid_t */
+#include <stdlib.h> /* free */
+#include <pthread.h>
+#include <malloc.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+
+#include <aul.h> /* aul_launch_app */
+#include <dlog.h>
+#include <bundle.h>
+
+#include <packet.h>
+#include <widget_errno.h>
+#include <widget_conf.h>
+#include <widget_abi.h>
+#include <widget_util.h>
+#include <widget_cmd_list.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+
+#include "critical_log.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "fault_manager.h"
+#include "debug.h"
+#include "conf.h"
+#include "setting.h"
+#include "util.h"
+#include "xmonitor.h"
+
+#include "package.h"
+#include "instance.h"
+
+#define BUNDLE_SLAVE_SVC_OP_TYPE "__APP_SVC_OP_TYPE__"
+#define APP_CONTROL_OPERATION_MAIN "http://tizen.org/appcontrol/operation/main"
+#define LOW_PRIORITY   10
+
+int errno;
+
+struct slave_node {
+       char *name;
+       char *abi;
+       char *pkgname;
+       int secured;    /* Only A package(widget) is loaded for security requirements */
+       int refcnt;
+       int fault_count;
+       int critical_fault_count;
+       enum slave_state state;
+       int network;
+
+       int loaded_instance;
+       int loaded_package;
+
+       int reactivate_instances;
+       int reactivate_slave;
+
+       int launch_async;
+
+       pid_t pid;
+
+       enum event_process {
+               SLAVE_EVENT_PROCESS_IDLE = 0x00,
+               SLAVE_EVENT_PROCESS_ACTIVATE = 0x01,
+               SLAVE_EVENT_PROCESS_DEACTIVATE = 0x02,
+               SLAVE_EVENT_PROCESS_DELETE = 0x04,
+               SLAVE_EVENT_PROCESS_FAULT = 0x08,
+               SLAVE_EVENT_PROCESS_PAUSE = 0x10,
+               SLAVE_EVENT_PROCESS_RESUME = 0x20
+       } in_event_process;
+       Eina_List *event_activate_list;
+       Eina_List *event_deactivate_list;
+       Eina_List *event_delete_list;
+       Eina_List *event_fault_list;
+       Eina_List *event_pause_list;
+       Eina_List *event_resume_list;
+
+       Eina_List *data_list;
+
+       Ecore_Timer *ttl_timer; /*!< Time to live */
+       Ecore_Timer *activate_timer; /*!< Waiting hello packet for this time */
+       Ecore_Timer *relaunch_timer; /*!< Try to relaunch service app */
+       Ecore_Timer *terminate_timer; /*!< Waiting this timer before terminate the service provider */
+       int relaunch_count;
+
+       int ctrl_option;
+
+#if defined(_USE_ECORE_TIME_GET)
+       double activated_at;
+#else
+       struct timeval activated_at;
+#endif
+
+       char *hw_acceleration;
+       int valid;
+};
+
+struct event {
+       struct slave_node *slave;
+
+       int (*evt_cb)(struct slave_node *, void *);
+       void *cbdata;
+       int deleted;
+};
+
+struct priv_data {
+       char *tag;
+       void *data;
+};
+
+static struct {
+       Eina_List *slave_list;
+       int deactivate_all_refcnt;
+} s_info = {
+       .slave_list = NULL,
+       .deactivate_all_refcnt = 0,
+};
+
+static Eina_Bool terminate_timer_cb(void *data)
+{
+       struct slave_node *slave = data;
+       int ret;
+
+       /*!
+        * \todo
+        * check the return value of the aul_terminate_pid
+        */
+       slave->state = SLAVE_REQUEST_TO_TERMINATE;
+       slave->terminate_timer = NULL;
+
+       DbgPrint("Terminate slave: %d (%s)\n", slave_pid(slave), slave_name(slave));
+       ret = aul_terminate_pid_async(slave->pid);
+       if (ret < 0) {
+               ErrPrint("Terminate slave(%s) failed. pid %d (%d)\n", slave_name(slave), slave_pid(slave), ret);
+               slave = slave_deactivated(slave);
+               if (slave == NULL) {
+                       DbgPrint("Slave is deleted\n");
+               }
+       }
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static struct slave_node *slave_deactivate(struct slave_node *slave, int no_timer)
+{
+       int ret;
+
+       /**
+        * @note
+        * The caller must has to check the slave's state.
+        * If it is not activated, this function must has not to be called.
+        */
+
+       if (slave_pid(slave) <= 0) {
+               return slave;
+       }
+
+       if (slave->ttl_timer) {
+               /**
+                * @note
+                * If there is an activated TTL timer,
+                * It has to be cleared before deactivate the slave.
+                */
+               ecore_timer_del(slave->ttl_timer);
+               slave->ttl_timer = NULL;
+       }
+
+       if ((slave->ctrl_option & PROVIDER_CTRL_MANUAL_TERMINATION) == PROVIDER_CTRL_MANUAL_TERMINATION) {
+               /*!
+                * \note
+                * In this case,
+                * The provider requests MANUAL TERMINATION control option.
+                * Master will not send terminate request in this case, the provider should be terminated by itself.
+                */
+               DbgPrint("Manual termination is turned on\n");
+               slave->state = SLAVE_REQUEST_TO_DISCONNECT;
+               (void)slave_rpc_disconnect(slave);
+       } else if (slave->terminate_timer) {
+               ErrPrint("Terminate timer is already fired (%d)\n", slave->pid);
+       } else if ((!no_timer && !slave->secured) || slave_is_app(slave)) {
+               DbgPrint("Fire the terminate timer: %d (%d)\n", slave->pid, slave_is_app(slave));
+               slave->terminate_timer = ecore_timer_add(WIDGET_CONF_SLAVE_ACTIVATE_TIME, terminate_timer_cb, slave);
+               if (!slave->terminate_timer) {
+                       /*!
+                        * \note
+                        * Normally, Call the terminate_timer_cb directly from here
+                        * But in this case. if the aul_terminate_pid failed, Call the slave_deactivated function directly.
+                        * Then the "slave" pointer can be changed. To trace it, Copy the body of terminate_timer_cb to here.
+                        */
+                       ErrPrint("Failed to add a new timer for terminating\n");
+                       DbgPrint("Terminate slave: %d (%s)\n", slave_pid(slave), slave_name(slave));
+                       /*!
+                        * \todo
+                        * check the return value of the aul_terminate_pid
+                        */
+                       slave->state = SLAVE_REQUEST_TO_TERMINATE;
+
+                       ret = aul_terminate_pid_async(slave->pid);
+                       if (ret < 0) {
+                               ErrPrint("Terminate slave(%s) failed. pid %d (%d)\n", slave_name(slave), slave_pid(slave), ret);
+                               slave = slave_deactivated(slave);
+                       }
+               }
+       } else {
+               /*!
+                * \todo
+                * check the return value of the aul_terminate_pid
+                */
+               slave->state = SLAVE_REQUEST_TO_TERMINATE;
+
+               DbgPrint("Terminate slave: %d (%s)\n", slave_pid(slave), slave_name(slave));
+               ret = aul_terminate_pid_async(slave->pid);
+               if (ret < 0) {
+                       ErrPrint("Terminate slave(%s) failed. pid %d (%d)\n", slave_name(slave), slave_pid(slave), ret);
+                       slave = slave_deactivated(slave);
+               }
+       }
+
+       return slave;
+}
+
+static Eina_Bool slave_ttl_cb(void *data)
+{
+       struct pkg_info *info;
+       struct slave_node *slave = (struct slave_node *)data;
+       Eina_List *l;
+       Eina_List *pkg_list;
+
+       pkg_list = (Eina_List *)package_list();
+       EINA_LIST_FOREACH(pkg_list, l, info) {
+               if (package_slave(info) == slave) {
+                       struct inst_info *inst;
+                       Eina_List *inst_list;
+                       Eina_List *n;
+
+                       inst_list = (Eina_List *)package_instance_list(info);
+                       EINA_LIST_FOREACH(inst_list, n, inst) {
+                               if (instance_visible_state(inst) == WIDGET_SHOW) {
+                                       DbgPrint("Instance is in show, give more ttl to %d for %s\n", slave_pid(slave), instance_id(inst));
+                                       return ECORE_CALLBACK_RENEW;
+                               }
+                       }
+               }
+       }
+
+       /*!
+        * \note
+        * ttl_timer must has to be set to NULL before deactivate the slave
+        * It will be used for making decision of the expired TTL timer or the fault of a widget.
+        */
+       slave->ttl_timer = NULL;
+
+       if (slave_is_activated(slave)) {
+               slave_set_reactivation(slave, 0);
+               slave_set_reactivate_instances(slave, 1);
+
+               slave = slave_deactivate(slave, 1);
+               if (!slave) {
+                       DbgPrint("Slave is deleted\n");
+               }
+       }
+
+       /*! To recover all instances state it is activated again */
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static inline int xmonitor_pause_cb(void *data)
+{
+       slave_pause(data);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int xmonitor_resume_cb(void *data)
+{
+       slave_resume(data);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline struct slave_node *create_slave_node(const char *name, int is_secured, const char *abi, const char *pkgname, int network, const char *hw_acceleration, int launch_async)
+{
+       struct slave_node *slave;
+
+       slave = calloc(1, sizeof(*slave));
+       if (!slave) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       slave->name = strdup(name);
+       if (!slave->name) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(slave);
+               return NULL;
+       }
+
+       slave->abi = strdup(abi);
+       if (!slave->abi) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(slave->name);
+               DbgFree(slave);
+               return NULL;
+       }
+
+       slave->pkgname = strdup(pkgname);
+       if (!slave->pkgname) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(slave->abi);
+               DbgFree(slave->name);
+               DbgFree(slave);
+               return NULL;
+       }
+
+       if (hw_acceleration) {
+               slave->hw_acceleration = strdup(hw_acceleration);
+               if (!slave->hw_acceleration) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(slave->pkgname);
+                       DbgFree(slave->abi);
+                       DbgFree(slave->name);
+                       DbgFree(slave);
+                       return NULL;
+               }
+       }
+
+       slave->secured = is_secured;
+       slave->pid = (pid_t)-1;
+       slave->state = SLAVE_TERMINATED;
+       slave->network = network;
+       slave->relaunch_count = WIDGET_CONF_SLAVE_RELAUNCH_COUNT;
+       slave->launch_async = launch_async;
+
+       xmonitor_add_event_callback(XMONITOR_PAUSED, xmonitor_pause_cb, slave);
+       xmonitor_add_event_callback(XMONITOR_RESUMED, xmonitor_resume_cb, slave);
+
+       s_info.slave_list = eina_list_append(s_info.slave_list, slave);
+       return slave;
+}
+
+static inline void invoke_delete_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_DELETE;
+       EINA_LIST_FOREACH_SAFE(slave->event_delete_list, l, n, event) {
+               if (event->deleted || event->evt_cb(event->slave, event->cbdata) < 0 || event->deleted) {
+                       slave->event_delete_list = eina_list_remove(slave->event_delete_list, event);
+                       DbgFree(event);
+               }
+       }
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_DELETE;
+}
+
+static inline void destroy_slave_node(struct slave_node *slave)
+{
+       struct event *event;
+       struct priv_data *priv;
+
+       if (slave_pid(slave) != (pid_t)-1) {
+               ErrPrint("Slave is not deactivated\n");
+               return;
+       }
+
+       xmonitor_del_event_callback(XMONITOR_PAUSED, xmonitor_pause_cb, slave);
+       xmonitor_del_event_callback(XMONITOR_RESUMED, xmonitor_resume_cb, slave);
+
+       invoke_delete_cb(slave);
+       slave_rpc_fini(slave); /*!< Finalize the RPC after handling all delete callbacks */
+
+       EINA_LIST_FREE(slave->event_delete_list, event) {
+               DbgFree(event);
+       }
+
+       EINA_LIST_FREE(slave->event_activate_list, event) {
+               DbgFree(event);
+       }
+
+       EINA_LIST_FREE(slave->event_deactivate_list, event) {
+               DbgFree(event);
+       }
+
+       EINA_LIST_FREE(slave->event_fault_list, event) {
+               DbgFree(event);
+       }
+
+       EINA_LIST_FREE(slave->data_list, priv) {
+               DbgFree(priv->tag);
+               DbgFree(priv);
+       }
+
+       s_info.slave_list = eina_list_remove(s_info.slave_list, slave);
+
+       if (slave->ttl_timer) {
+               ecore_timer_del(slave->ttl_timer);
+       }
+
+       if (slave->activate_timer) {
+               ecore_timer_del(slave->activate_timer);
+       }
+
+       if (slave->relaunch_timer) {
+               ecore_timer_del(slave->relaunch_timer);
+       }
+
+       DbgFree(slave->abi);
+       DbgFree(slave->name);
+       DbgFree(slave->pkgname);
+       DbgFree(slave->hw_acceleration);
+       DbgFree(slave);
+       return;
+}
+
+static inline struct slave_node *find_slave(const char *name)
+{
+       struct slave_node *slave;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (!strcmp(slave->name, name)) {
+                       return slave;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI int slave_expired_ttl(struct slave_node *slave)
+{
+       if (!slave) {
+               return 0;
+       }
+
+       if (!slave_is_app(slave) && !slave->secured && !(WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL)) {
+               return 0;
+       }
+
+       return !!slave->ttl_timer;
+}
+
+HAPI struct slave_node *slave_ref(struct slave_node *slave)
+{
+       if (!slave) {
+               return NULL;
+       }
+
+       slave->refcnt++;
+       return slave;
+}
+
+HAPI struct slave_node *slave_unref(struct slave_node *slave)
+{
+       if (!slave) {
+               return NULL;
+       }
+
+       if (slave->refcnt == 0) {
+               ErrPrint("Slave refcnt is not valid\n");
+               return NULL;
+       }
+
+       slave->refcnt--;
+       if (slave->refcnt == 0) {
+               destroy_slave_node(slave);
+               slave = NULL;
+       }
+
+       return slave;
+}
+
+HAPI const int const slave_refcnt(struct slave_node *slave)
+{
+       return slave->refcnt;
+}
+
+HAPI struct slave_node *slave_create(const char *name, int is_secured, const char *abi, const char *pkgname, int network, const char *hw_acceleration, int launch_async)
+{
+       struct slave_node *slave;
+
+       slave = find_slave(name);
+       if (slave) {
+               if (slave->secured != is_secured) {
+                       ErrPrint("Exists slave and creating slave's security flag is not matched\n");
+               }
+               return slave;
+       }
+
+       slave = create_slave_node(name, is_secured, abi, pkgname, network, hw_acceleration, launch_async);
+       if (!slave) {
+               return NULL;
+       }
+
+       slave_ref(slave);
+       slave_rpc_init(slave);
+
+       return slave;
+}
+
+/*!
+ * \note
+ * Before destroying slave object,
+ * you should check the RPC(slave_async_XXX) state and Private data field (slave_set_data)
+ */
+HAPI void slave_destroy(struct slave_node *slave)
+{
+       slave_unref(slave);
+}
+
+static inline struct slave_node *invoke_fault_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+
+       slave_ref(slave);
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_FAULT;
+       EINA_LIST_FOREACH_SAFE(slave->event_fault_list, l, n, event) {
+               if (event->deleted || event->evt_cb(event->slave, event->cbdata) < 0 || event->deleted) {
+                       slave->event_fault_list = eina_list_remove(slave->event_fault_list, event);
+                       DbgFree(event);
+               }
+       }
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_FAULT;
+       slave = slave_unref(slave);
+
+       return slave;
+}
+
+static inline void invoke_activate_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_ACTIVATE;
+       EINA_LIST_FOREACH_SAFE(slave->event_activate_list, l, n, event) {
+               if (event->deleted || event->evt_cb(event->slave, event->cbdata) < 0 || event->deleted) {
+                       slave->event_activate_list = eina_list_remove(slave->event_activate_list, event);
+                       DbgFree(event);
+               }
+       }
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_ACTIVATE;
+}
+
+static Eina_Bool activate_timer_cb(void *data)
+{
+       struct slave_node *slave = data;
+
+       if (slave->relaunch_timer) {
+               ecore_timer_del(slave->relaunch_timer);
+               slave->relaunch_timer = NULL;
+       }
+
+       slave->fault_count++;
+
+       if (invoke_fault_cb(slave) == NULL) {
+               ErrPrint("Slave is deleted while processing fault handler\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       slave_set_reactivation(slave, 0);
+       slave_set_reactivate_instances(slave, 0);
+
+       slave->activate_timer = NULL;
+       if (slave_pid(slave) > 0) {
+               int ret;
+               DbgPrint("Try to terminate PID: %d\n", slave_pid(slave));
+               ret = aul_terminate_pid_async(slave_pid(slave));
+               if (ret < 0) {
+                       ErrPrint("Terminate failed, pid %d (reason: %d)\n", slave_pid(slave), ret);
+               }
+       }
+
+       CRITICAL_LOG("Slave is not activated in %lf sec (slave: %s)\n", WIDGET_CONF_SLAVE_ACTIVATE_TIME, slave_name(slave));
+       slave = slave_deactivated(slave);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static inline void invoke_slave_fault_handler(struct slave_node *slave)
+{
+       slave->fault_count++;
+       if (invoke_fault_cb(slave) == NULL) {
+               ErrPrint("Slave is deleted while processing fault handler\n");
+               return;
+       }
+
+       slave_set_reactivation(slave, 0);
+       slave_set_reactivate_instances(slave, 0);
+
+       if (slave_pid(slave) > 0) {
+               if ((slave->ctrl_option & PROVIDER_CTRL_MANUAL_TERMINATION) == PROVIDER_CTRL_MANUAL_TERMINATION) {
+                       DbgPrint("Manual termination is turned on\n");
+                       (void)slave_rpc_disconnect(slave);
+               } else {
+                       int ret;
+                       DbgPrint("Try to terminate PID: %d\n", slave_pid(slave));
+                       ret = aul_terminate_pid_async(slave_pid(slave));
+                       if (ret < 0) {
+                               ErrPrint("Terminate failed, pid %d (reason: %d)\n", slave_pid(slave), ret);
+                       }
+               }
+       }
+
+       slave->state = SLAVE_TERMINATED;
+}
+
+static Eina_Bool relaunch_timer_cb(void *data)
+{
+       struct slave_node *slave = data;
+       int ret = ECORE_CALLBACK_CANCEL;
+
+       if (!slave->activate_timer) {
+               ErrPrint("Activate timer is not valid\n");
+               slave->relaunch_timer = NULL;
+
+               invoke_slave_fault_handler(slave);
+       } else if (!slave->relaunch_count) {
+               ErrPrint("Relaunch count is exhahausted\n");
+               ecore_timer_del(slave->activate_timer);
+               slave->activate_timer = NULL;
+
+               slave->relaunch_timer = NULL;
+               invoke_slave_fault_handler(slave);
+       } else {
+               bundle *param;
+
+               param = bundle_create();
+               if (!param) {
+                       ErrPrint("Failed to create a bundle\n");
+
+                       ecore_timer_del(slave->activate_timer);
+                       slave->activate_timer = NULL;
+
+                       slave->relaunch_timer = NULL;
+
+                       invoke_slave_fault_handler(slave);
+               } else {
+                       bundle_add(param, BUNDLE_SLAVE_SVC_OP_TYPE, APP_CONTROL_OPERATION_MAIN);
+                       bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_NAME, slave_name(slave));
+                       bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_SECURED, ((WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) || slave->secured) ? "true" : "false");
+                       bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_ABI, slave->abi);
+                       bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_HW_ACCELERATION, slave->hw_acceleration);
+
+                       if (slave->launch_async) {
+                               ErrPrint("Launch App Async [%s]\n", slave_pkgname(slave));
+                               slave->pid = (pid_t)aul_launch_app_async(slave_pkgname(slave), param);
+                       } else {
+                               ErrPrint("Launch App Sync [%s]\n", slave_pkgname(slave));
+                               slave->pid = (pid_t)aul_launch_app(slave_pkgname(slave), param);
+                       }
+
+                       bundle_free(param);
+
+                       switch (slave->pid) {
+                       case AUL_R_EHIDDENFORGUEST:     /**< App hidden for guest mode */
+                       case AUL_R_ENOLAUNCHPAD:        /**< no launchpad */
+                       case AUL_R_EILLACC:             /**< Illegal Access */
+                       case AUL_R_EINVAL:              /**< Invalid argument */
+                       case AUL_R_ENOINIT:             /**< AUL handler NOT initialized */
+                       case AUL_R_ERROR:               /**< General error */
+                               CRITICAL_LOG("Failed to launch a new slave %s (%d)\n", slave_name(slave), slave->pid);
+                               slave->pid = (pid_t)-1;
+                               ecore_timer_del(slave->activate_timer);
+                               slave->activate_timer = NULL;
+
+                               slave->relaunch_timer = NULL;
+
+                               invoke_slave_fault_handler(slave);
+                               /* Waiting app-launch result */
+                               break;
+                       case AUL_R_ETIMEOUT:            /**< Timeout */
+                       case AUL_R_ECOMM:               /**< Comunication Error */
+                       case AUL_R_ETERMINATING:        /**< application terminating */
+                       case AUL_R_ECANCELED:           /**< Operation canceled */
+                       case AUL_R_EREJECTED:
+                               slave->relaunch_count--;
+
+                               CRITICAL_LOG("Try relaunch again %s (%d), %d\n", slave_name(slave), slave->pid, slave->relaunch_count);
+                               slave->pid = (pid_t)-1;
+                               ret = ECORE_CALLBACK_RENEW;
+                               ecore_timer_reset(slave->activate_timer);
+                               /* Try again after a few secs later */
+                               break;
+                       case AUL_R_LOCAL:               /**< Launch by himself */
+                       case AUL_R_OK:                  /**< General success */
+                       default:
+                               DbgPrint("Slave %s is launched with %d as %s\n", slave_pkgname(slave), slave->pid, slave_name(slave));
+                               slave->relaunch_timer = NULL;
+                               ecore_timer_reset(slave->activate_timer);
+                               break;
+                       }
+               }
+
+       }
+
+       return ret;
+}
+
+HAPI int slave_activate(struct slave_node *slave)
+{
+       /**
+        * @todo
+        * If a slave is deactivated by OOM killer,
+        * We should not activate it again from here.
+        * Instead of this, it should be reactivated by user event or oom event(normal)
+        */
+
+       /**
+        * @note
+        * This check code can be replaced with the slave->state check code
+        * If the slave data has the PID, it means, it is activated
+        * Even if it is in the termiating sequence, it will have the PID
+        * until it is terminated at last.
+        * So we can use this simple code for checking the slave's last state.
+        * whether it is alive or not.
+        */
+       if (slave_pid(slave) != (pid_t)-1) {
+               if (slave->terminate_timer) {
+                       DbgPrint("Clear terminate timer. to reuse (%d)\n", slave->pid);
+                       ecore_timer_del(slave->terminate_timer);
+                       slave->terminate_timer = NULL;
+               } else if (slave_state(slave) == SLAVE_REQUEST_TO_TERMINATE || slave_state(slave) == SLAVE_REQUEST_TO_DISCONNECT) {
+                       slave_set_reactivation(slave, 1);
+               }
+               return WIDGET_ERROR_ALREADY_STARTED;
+       } else if (slave_state(slave) == SLAVE_REQUEST_TO_LAUNCH) {
+               DbgPrint("Slave is already launched: but the AUL is timed out\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       if (WIDGET_CONF_DEBUG_MODE || g_conf.debug_mode) {
+               DbgPrint("Debug Mode enabled. name[%s] secured[%d] abi[%s]\n", slave_name(slave), slave->secured, slave->abi);
+       } else {
+               bundle *param;
+
+               slave->relaunch_count = WIDGET_CONF_SLAVE_RELAUNCH_COUNT;
+
+               param = bundle_create();
+               if (!param) {
+                       ErrPrint("Failed to create a bundle\n");
+                       return WIDGET_ERROR_FAULT;
+               }
+
+               bundle_add(param, BUNDLE_SLAVE_SVC_OP_TYPE, APP_CONTROL_OPERATION_MAIN);
+               bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_NAME, slave_name(slave));
+               bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_SECURED, ((WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) || slave->secured) ? "true" : "false");
+               bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_ABI, slave->abi);
+               bundle_add(param, WIDGET_CONF_BUNDLE_SLAVE_HW_ACCELERATION, slave->hw_acceleration);
+
+               if (slave->launch_async) {
+                       ErrPrint("Launch App Async [%s]\n", slave_pkgname(slave));
+                       slave->pid = (pid_t)aul_launch_app_async(slave_pkgname(slave), param);
+               } else {
+                       ErrPrint("Launch App Sync [%s]\n", slave_pkgname(slave));
+                       slave->pid = (pid_t)aul_launch_app(slave_pkgname(slave), param);
+               }
+
+               bundle_free(param);
+
+               switch (slave->pid) {
+               case AUL_R_EHIDDENFORGUEST:     /**< App hidden for guest mode */
+               case AUL_R_ENOLAUNCHPAD:        /**< no launchpad */
+               case AUL_R_EILLACC:             /**< Illegal Access */
+               case AUL_R_EINVAL:              /**< Invalid argument */
+               case AUL_R_ENOINIT:             /**< AUL handler NOT initialized */
+               case AUL_R_ERROR:               /**< General error */
+                       CRITICAL_LOG("Failed to launch a new slave %s (%d)\n", slave_name(slave), slave->pid);
+                       slave->pid = (pid_t)-1;
+                       /* Waiting app-launch result */
+                       break;
+               case AUL_R_ECOMM:               /**< Comunication Error */
+               case AUL_R_ETERMINATING:        /**< application terminating */
+               case AUL_R_ECANCELED:           /**< Operation canceled */
+               case AUL_R_ETIMEOUT:            /**< Timeout */
+               case AUL_R_EREJECTED:
+                       CRITICAL_LOG("Try relaunch this soon %s (%d)\n", slave_name(slave), slave->pid);
+                       slave->relaunch_timer = ecore_timer_add(WIDGET_CONF_SLAVE_RELAUNCH_TIME, relaunch_timer_cb, slave);
+                       if (!slave->relaunch_timer) {
+                               CRITICAL_LOG("Failed to register a relaunch timer (%s)\n", slave_name(slave));
+                               slave->pid = (pid_t)-1;
+                               return WIDGET_ERROR_FAULT;
+                       }
+                       /* Try again after a few secs later */
+                       break;
+               case AUL_R_LOCAL:               /**< Launch by himself */
+               case AUL_R_OK:                  /**< General success */
+               default:
+                       DbgPrint("Slave %s is launched with %d as %s\n", slave_pkgname(slave), slave->pid, slave_name(slave));
+                       break;
+               }
+
+               slave->activate_timer = ecore_timer_add(WIDGET_CONF_SLAVE_ACTIVATE_TIME, activate_timer_cb, slave);
+               if (!slave->activate_timer) {
+                       ErrPrint("Failed to register an activate timer\n");
+               }
+       }
+
+       slave->state = SLAVE_REQUEST_TO_LAUNCH;
+       /*!
+        * \note
+        * Increase the refcnt of a slave,
+        * To prevent from making an orphan(slave).
+        */
+       (void)slave_ref(slave);
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_give_more_ttl(struct slave_node *slave)
+{
+       double delay;
+
+       if (!(WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) && ((!slave_is_app(slave) && !slave->secured) || !slave->ttl_timer)) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       delay = WIDGET_CONF_SLAVE_TTL - ecore_timer_pending_get(slave->ttl_timer);
+       ecore_timer_delay(slave->ttl_timer, delay);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_freeze_ttl(struct slave_node *slave)
+{
+       if (!(WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) && ((!slave_is_app(slave) && !slave->secured) || !slave->ttl_timer)) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ecore_timer_freeze(slave->ttl_timer);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_thaw_ttl(struct slave_node *slave)
+{
+       double delay;
+
+       if (!(WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) && ((!slave_is_app(slave) && !slave->secured) || !slave->ttl_timer)) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       ecore_timer_thaw(slave->ttl_timer);
+
+       delay = WIDGET_CONF_SLAVE_TTL - ecore_timer_pending_get(slave->ttl_timer);
+       ecore_timer_delay(slave->ttl_timer, delay);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_activated(struct slave_node *slave)
+{
+       /**
+        * @note
+        * Send the client's state to the provider.
+        * After the slave is activated.
+        */
+       if (xmonitor_is_paused()) {
+               slave_pause(slave);
+       } else {
+               slave_resume(slave);
+       }
+
+       /**
+        * Condition for activating TTL Timer
+        * 1. If the slave is INHOUSE(data-provider-slave) and LIMIT_TO_TTL is true, and SLAVE_TTL is greater than 0.0f
+        * 2. Service provider is "secured" and SLAVE_TTL is greater than 0.0f
+        */
+       if (((WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL) || slave->secured == 1 || slave_is_app(slave)) && WIDGET_CONF_SLAVE_TTL > 0.0f) {
+               DbgPrint("Slave deactivation timer is added (%s - %lf)\n", slave_name(slave), WIDGET_CONF_SLAVE_TTL);
+               slave->ttl_timer = ecore_timer_add(WIDGET_CONF_SLAVE_TTL, slave_ttl_cb, slave);
+               if (!slave->ttl_timer) {
+                       ErrPrint("Failed to create a TTL timer\n");
+               }
+       }
+
+       invoke_activate_cb(slave);
+
+       slave_set_reactivation(slave, 0);
+       slave_set_reactivate_instances(slave, 0);
+
+#if defined(_USE_ECORE_TIME_GET)
+       slave->activated_at = ecore_time_get();
+#else
+       if (gettimeofday(&slave->activated_at, NULL) < 0) {
+               ErrPrint("getimeofday: %d\n", errno);
+               slave->activated_at.tv_sec = 0;
+               slave->activated_at.tv_usec = 0;
+       }
+#endif
+
+       if (slave->activate_timer) {
+               ecore_timer_del(slave->activate_timer);
+               slave->activate_timer = NULL;
+       }
+
+       if (slave->relaunch_timer) {
+               ecore_timer_del(slave->relaunch_timer);
+               slave->relaunch_timer = NULL;
+       }
+
+       slave_set_priority(slave, LOW_PRIORITY);
+       return WIDGET_ERROR_NONE;
+}
+
+static inline int invoke_deactivate_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+       int ret;
+       int reactivate = 0;
+
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_DEACTIVATE;
+
+       EINA_LIST_FOREACH_SAFE(slave->event_deactivate_list, l, n, event) {
+               if (event->deleted) {
+                       slave->event_deactivate_list = eina_list_remove(slave->event_deactivate_list, event);
+                       DbgFree(event);
+                       continue;
+               }
+
+               ret = event->evt_cb(event->slave, event->cbdata);
+               if (ret < 0 || event->deleted) {
+                       slave->event_deactivate_list = eina_list_remove(slave->event_deactivate_list, event);
+                       DbgFree(event);
+               }
+
+               if (ret == SLAVE_NEED_TO_REACTIVATE) {
+                       reactivate++;
+               }
+       }
+
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_DEACTIVATE;
+
+       return reactivate;
+}
+
+HAPI struct slave_node *slave_deactivated(struct slave_node *slave)
+{
+       int reactivate;
+
+       slave->pid = (pid_t)-1;
+       slave->state = SLAVE_TERMINATED;
+
+       if (slave->ttl_timer) {
+               ecore_timer_del(slave->ttl_timer);
+               slave->ttl_timer = NULL;
+       }
+
+       if (slave->activate_timer) {
+               ecore_timer_del(slave->activate_timer);
+               slave->activate_timer = NULL;
+       }
+
+       if (slave->relaunch_timer) {
+               ecore_timer_del(slave->relaunch_timer);
+               slave->relaunch_timer = NULL;
+       }
+
+       if (slave->terminate_timer) {
+               ecore_timer_del(slave->terminate_timer);
+               slave->terminate_timer = NULL;
+       }
+
+       /**
+        * @note
+        * FOR SAFETY
+        * If the deactivated event callback is called for package.c
+        * It can delete the instance if it has fault information
+        * then it also try to delete the slave object again.
+        * To prevent from unexpected slave object deletetion while handling callback,
+        * increase the refcnt of slave
+        * when it get back from callback, try to decrease the refcnt of slave
+        * At that time, we can delete slave safely.
+        */
+       slave_ref(slave);
+       reactivate = invoke_deactivate_cb(slave);
+       slave = slave_unref(slave);
+       if (!slave) {
+               ErrPrint("Slave object is deleted\n");
+               return slave;
+       }
+
+       slave = slave_unref(slave);
+       if (!slave) {
+               DbgPrint("SLAVE object is destroyed\n");
+               return slave;
+       }
+
+       if ((slave->ctrl_option & PROVIDER_CTRL_MANUAL_REACTIVATION) == PROVIDER_CTRL_MANUAL_REACTIVATION) {
+               /*!
+                * \note
+                * In this case, the provider(Slave) should be reactivated by itself or user.
+                * The master will not reactivate it automatically.
+                */
+               DbgPrint("Manual reactivate option is turned on\n");
+       } else if (reactivate && slave_need_to_reactivate(slave) && setting_oom_level() == OOM_TYPE_NORMAL) {
+               int ret;
+
+               DbgPrint("Need to reactivate a slave\n");
+               ret = slave_activate(slave);
+               if (ret < 0 && ret != WIDGET_ERROR_ALREADY_STARTED) {
+                       ErrPrint("Failed to reactivate a slave\n");
+               }
+       } else if (slave_loaded_instance(slave) == 0) {
+               /*!
+                * \note
+                * If a slave has no more instances,
+                * Destroy it
+                */
+               slave = slave_unref(slave);
+       }
+
+       return slave;
+}
+
+HAPI struct slave_node *slave_deactivated_by_fault(struct slave_node *slave)
+{
+       int ret;
+       int reactivate = 1;
+       int reactivate_instances = 1;
+       int max_load;
+
+       if (g_conf.slave_max_load < 0) {
+               max_load = WIDGET_CONF_SLAVE_MAX_LOAD;
+       } else {
+               max_load = g_conf.slave_max_load;
+       }
+
+       if (!slave_is_activated(slave)) {
+               DbgPrint("Deactivating in progress\n");
+               if (slave_loaded_instance(slave) == 0) {
+                       slave = slave_unref(slave);
+               }
+
+               return slave;
+       }
+
+       slave->fault_count++;
+
+       (void)fault_check_pkgs(slave);
+
+       if (slave_pid(slave) > 0) {
+               DbgPrint("Try to terminate PID: %d\n", slave_pid(slave));
+               ret = aul_terminate_pid_async(slave_pid(slave));
+               if (ret < 0) {
+                       ErrPrint("Terminate failed, pid %d\n", slave_pid(slave));
+               }
+       }
+
+#if defined(_USE_ECORE_TIME_GET)
+       double faulted_at;
+
+       faulted_at = ecore_time_get();
+       if (faulted_at - slave->activated_at < WIDGET_CONF_MINIMUM_REACTIVATION_TIME) {
+               slave->critical_fault_count++;
+
+               if (!slave_loaded_instance(slave) || slave->critical_fault_count >= max_load) {
+                       ErrPrint("Reactivation time is too fast and frequently occurred - Stop to auto reactivation\n");
+                       reactivate = 0;
+                       reactivate_instances = 0;
+                       slave->critical_fault_count = 0;
+                       /*!
+                        * \note
+                        * Fault callback can access the slave information.
+                        */
+                       if (invoke_fault_cb(slave) == NULL) {
+                               ErrPrint("Slave is deleted while processing fault handler\n");
+                               return NULL;
+                       }
+               } else {
+                       slave->critical_fault_count = 0;
+               }
+       }
+#else
+       struct timeval faulted_at;
+
+       if (gettimeofday(&faulted_at, NULL) == 0) {
+               struct timeval rtv;
+
+               timersub(&faulted_at, &slave->activated_at, &rtv);
+               if (rtv.tv_sec < WIDGET_CONF_MINIMUM_REACTIVATION_TIME) {
+                       slave->critical_fault_count++;
+                       if (!slave_loaded_instance(slave) || slave->critical_fault_count >= max_load) {
+                               ErrPrint("Reactivation time is too fast and frequently occurred - Stop to auto reactivation\n");
+                               reactivate = 0;
+                               reactivate_instances = 0;
+                               slave->critical_fault_count = 0;
+                               /*!
+                                * \note
+                                * Fault callback can access the slave information.
+                                */
+                               if (invoke_fault_cb(slave) == NULL) {
+                                       ErrPrint("Slave is deleted while processing fault handler\n");
+                                       return NULL;
+                               }
+                       }
+               } else {
+                       slave->critical_fault_count = 0;
+               }
+       } else {
+               ErrPrint("gettimeofday: %d\n", errno);
+       }
+#endif
+
+       slave_set_reactivation(slave, reactivate);
+       slave_set_reactivate_instances(slave, reactivate_instances);
+
+       slave = slave_deactivated(slave);
+       return slave;
+}
+
+HAPI const int const slave_is_activated(struct slave_node *slave)
+{
+       switch (slave->state) {
+       case SLAVE_REQUEST_TO_TERMINATE:
+       case SLAVE_TERMINATED:
+               return 0;
+       case SLAVE_REQUEST_TO_DISCONNECT:
+               /* This case should be treated as an activated state.
+                * To send the last request to the provider.
+                */
+       case SLAVE_REQUEST_TO_LAUNCH:
+               /* Not yet launched. but the slave incurred an unexpected error */
+       case SLAVE_REQUEST_TO_PAUSE:
+       case SLAVE_REQUEST_TO_RESUME:
+       case SLAVE_PAUSED:
+       case SLAVE_RESUMED:
+               return 1;
+       default:
+               return slave->pid != (pid_t)-1;
+       }
+
+       /* Could not be reach to here */
+       return 0;
+}
+
+HAPI int slave_event_callback_add(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data)
+{
+       struct event *ev;
+
+       ev = calloc(1, sizeof(*ev));
+       if (!ev) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       ev->slave = slave;
+       ev->cbdata = data;
+       ev->evt_cb = cb;
+
+       /*!
+        * \note
+        * Use the eina_list_prepend API.
+        * To keep the sequence of a callback invocation.
+        *
+        * Here is an example sequence.
+        *
+        * slave_event_callback_add(CALLBACK_01);
+        * slave_event_callback_add(CALLBACK_02);
+        * slave_event_callback_add(CALLBACK_03);
+        *
+        * Then the invoke_event_callback function will call the CALLBACKS as below sequence
+        *
+        * invoke_CALLBACK_03
+        * invoke_CALLBACK_02
+        * invoke_CALLBACK_01
+        */
+
+       switch (event) {
+       case SLAVE_EVENT_ACTIVATE:
+               slave->event_activate_list = eina_list_prepend(slave->event_activate_list, ev);
+               break;
+       case SLAVE_EVENT_DELETE:
+               slave->event_delete_list = eina_list_prepend(slave->event_delete_list, ev);
+               break;
+       case SLAVE_EVENT_DEACTIVATE:
+               slave->event_deactivate_list = eina_list_prepend(slave->event_deactivate_list, ev);
+               break;
+       case SLAVE_EVENT_PAUSE:
+               slave->event_pause_list = eina_list_prepend(slave->event_pause_list, ev);
+               break;
+       case SLAVE_EVENT_RESUME:
+               slave->event_resume_list = eina_list_prepend(slave->event_resume_list, ev);
+               break;
+       case SLAVE_EVENT_FAULT:
+               slave->event_fault_list = eina_list_prepend(slave->event_fault_list, ev);
+               break;
+       default:
+               DbgFree(ev);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_event_callback_del(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data)
+{
+       struct event *ev;
+       Eina_List *l;
+       Eina_List *n;
+
+       switch (event) {
+       case SLAVE_EVENT_DEACTIVATE:
+               EINA_LIST_FOREACH_SAFE(slave->event_deactivate_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_DEACTIVATE) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_deactivate_list = eina_list_remove(slave->event_deactivate_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case SLAVE_EVENT_DELETE:
+               EINA_LIST_FOREACH_SAFE(slave->event_delete_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_DELETE) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_delete_list = eina_list_remove(slave->event_delete_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case SLAVE_EVENT_ACTIVATE:
+               EINA_LIST_FOREACH_SAFE(slave->event_activate_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_ACTIVATE) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_activate_list = eina_list_remove(slave->event_activate_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case SLAVE_EVENT_PAUSE:
+               EINA_LIST_FOREACH_SAFE(slave->event_pause_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_PAUSE) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_pause_list = eina_list_remove(slave->event_pause_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case SLAVE_EVENT_RESUME:
+               EINA_LIST_FOREACH_SAFE(slave->event_resume_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_RESUME) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_resume_list = eina_list_remove(slave->event_resume_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       case SLAVE_EVENT_FAULT:
+               EINA_LIST_FOREACH_SAFE(slave->event_fault_list, l, n, ev) {
+                       if (ev->evt_cb == cb && ev->cbdata == data) {
+                               if (slave->in_event_process & SLAVE_EVENT_PROCESS_FAULT) {
+                                       ev->deleted = 1;
+                               } else {
+                                       slave->event_fault_list = eina_list_remove(slave->event_fault_list, ev);
+                                       DbgFree(ev);
+                               }
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int slave_set_data(struct slave_node *slave, const char *tag, void *data)
+{
+       struct priv_data *priv;
+
+       priv = calloc(1, sizeof(*priv));
+       if (!priv) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       priv->tag = strdup(tag);
+       if (!priv->tag) {
+               ErrPrint("strdup: %d\n", errno);
+               DbgFree(priv);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       priv->data = data;
+       slave->data_list = eina_list_append(slave->data_list, priv);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void *slave_del_data(struct slave_node *slave, const char *tag)
+{
+       struct priv_data *priv;
+       void *data;
+       Eina_List *l;
+       Eina_List *n;
+
+       EINA_LIST_FOREACH_SAFE(slave->data_list, l, n, priv) {
+               if (!strcmp(priv->tag, tag)) {
+                       slave->data_list = eina_list_remove(slave->data_list, priv);
+
+                       data = priv->data;
+                       DbgFree(priv->tag);
+                       DbgFree(priv);
+                       return data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI void *slave_data(struct slave_node *slave, const char *tag)
+{
+       struct priv_data *priv;
+       Eina_List *l;
+
+       EINA_LIST_FOREACH(slave->data_list, l, priv) {
+               if (!strcmp(priv->tag, tag)) {
+                       return priv->data;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct slave_node *slave_find_by_pid(pid_t pid)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (slave_pid(slave) == pid) {
+                       return slave;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct slave_node *slave_find_by_name(const char *name)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (!strcmp(slave_name(slave), name)) {
+                       return slave;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct slave_node *slave_find_available(const char *slave_pkgname, const char *abi, int secured, int network, const char *hw_acceleration)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (slave->secured != secured) {
+                       continue;
+               }
+
+               if ((slave->state == SLAVE_REQUEST_TO_TERMINATE || slave->state == SLAVE_REQUEST_TO_DISCONNECT) && slave->loaded_instance == 0) {
+                       /*!
+                        * \note
+                        * If a slave is in request_to_terminate state,
+                        * and the slave object has no more intances,
+                        * the slave object will be deleted soon.
+                        * so we cannot reuse it.
+                        *
+                        * This object is not usable.
+                        */
+                       continue;
+               }
+
+               if (strcasecmp(slave->abi, abi)) {
+                       continue;
+               }
+
+               if (strcasecmp(slave->pkgname, slave_pkgname)) {
+                       continue;
+               }
+
+               if (slave->hw_acceleration != hw_acceleration) {
+                       if (!slave->hw_acceleration || !hw_acceleration || strcasecmp(slave->hw_acceleration, hw_acceleration)) {
+                               continue;
+                       }
+               }
+
+               if (slave->secured) {
+                       if (slave->loaded_package == 0) {
+                               DbgPrint("Found secured slave - has no instances (%s)\n", slave_name(slave));
+                               return slave;
+                       }
+               } else if (slave->network == network) {
+                       DbgPrint("slave[%s] loaded_package[%d] net: [%d]\n", slave_name(slave), slave->loaded_package, slave->network);
+                       if (!strcasecmp(abi, WIDGET_CONF_DEFAULT_ABI)) {
+                               int max_load;
+                               if (g_conf.slave_max_load < 0) {
+                                       max_load = WIDGET_CONF_SLAVE_MAX_LOAD;
+                               } else {
+                                       max_load = g_conf.slave_max_load;
+                               }
+
+                               if (slave->loaded_package < max_load) {
+                                       return slave;
+                               }
+                       } else {
+                               return slave;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct slave_node *slave_find_by_pkgname(const char *pkgname)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (!strcmp(slave_pkgname(slave), pkgname)) {
+                       if (slave_pid(slave) == (pid_t)-1 || slave_pid(slave) == (pid_t)0) {
+                               return slave;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct slave_node *slave_find_by_rpc_handle(int handle)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+
+       if (handle <= 0) {
+               ErrPrint("Invalid RPC handle: %d\n", handle);
+               return NULL;
+       }
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               if (slave_rpc_handle(slave) == handle) {
+                       return slave;
+               }
+       }
+
+       /* Not found */
+       return NULL;
+}
+
+HAPI char *slave_package_name(const char *abi, const char *lbid)
+{
+       char *s_pkgname;
+       const char *tmp;
+
+       tmp = widget_abi_get_pkgname_by_abi(abi);
+       if (!tmp) {
+               ErrPrint("Failed to find a proper pkgname of a slave\n");
+               return NULL;
+       }
+
+       s_pkgname = widget_util_replace_string(tmp, WIDGET_CONF_REPLACE_TAG_APPID, lbid);
+       if (!s_pkgname) {
+               DbgPrint("Failed to get replaced string\n");
+               s_pkgname = strdup(tmp);
+               if (!s_pkgname) {
+                       ErrPrint("strdup: %d\n", errno);
+                       return NULL;
+               }
+       }
+
+       return s_pkgname;
+}
+
+HAPI void slave_load_package(struct slave_node *slave)
+{
+       slave->loaded_package++;
+}
+
+HAPI void slave_unload_package(struct slave_node *slave)
+{
+       if (!slave || slave->loaded_package == 0) {
+               ErrPrint("Slave loaded package is not correct\n");
+               return;
+       }
+
+       slave->loaded_package--;
+}
+
+HAPI void slave_load_instance(struct slave_node *slave)
+{
+       slave->loaded_instance++;
+       DbgPrint("Instance: (%d)%d\n", slave_pid(slave), slave->loaded_instance);
+}
+
+HAPI int const slave_loaded_instance(struct slave_node *slave)
+{
+       return slave->loaded_instance;
+}
+
+HAPI int const slave_loaded_package(struct slave_node *slave)
+{
+       return slave->loaded_package;
+}
+
+HAPI struct slave_node *slave_unload_instance(struct slave_node *slave)
+{
+       if (!slave || slave->loaded_instance == 0) {
+               ErrPrint("Slave loaded instance is not correct\n");
+               return slave;
+       }
+
+       slave->loaded_instance--;
+       DbgPrint("Instance: (%d)%d\n", slave_pid(slave), slave->loaded_instance);
+       if (slave->loaded_instance == 0 && slave_is_activated(slave)) {
+               slave_set_reactivation(slave, 0);
+               slave_set_reactivate_instances(slave, 0);
+
+               slave = slave_deactivate(slave, 0);
+       }
+
+       return slave;
+}
+
+HAPI const int const slave_is_secured(const struct slave_node *slave)
+{
+       return slave->secured;
+}
+
+HAPI const int const slave_is_app(const struct slave_node *slave)
+{
+       return !strcasecmp(slave_abi(slave), WIDGET_CONF_APP_ABI);
+}
+
+HAPI const char * const slave_name(const struct slave_node *slave)
+{
+       return slave->name;
+}
+
+HAPI const char * const slave_abi(const struct slave_node *slave)
+{
+       return slave->abi;
+}
+
+HAPI const pid_t const slave_pid(const struct slave_node *slave)
+{
+       return slave->pid;
+}
+
+HAPI int slave_set_pid(struct slave_node *slave, pid_t pid)
+{
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("Slave PID is updated to %d from %d\n", pid, slave_pid(slave));
+
+       slave->pid = pid;
+       return WIDGET_ERROR_NONE;
+}
+
+static inline void invoke_resumed_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_RESUME;
+       EINA_LIST_FOREACH_SAFE(slave->event_resume_list, l, n, event) {
+               if (event->deleted || event->evt_cb(event->slave, event->cbdata) < 0 || event->deleted) {
+                       slave->event_resume_list = eina_list_remove(slave->event_resume_list, event);
+                       DbgFree(event);
+               }
+       }
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_RESUME;
+}
+
+static void resume_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       int ret;
+
+       if (slave->state == SLAVE_REQUEST_TO_TERMINATE || slave->state == SLAVE_REQUEST_TO_DISCONNECT) {
+               DbgPrint("Slave is terminating now. ignore resume result\n");
+               return;
+       }
+
+       if (!packet) {
+               ErrPrint("Failed to change the state of the slave\n");
+               slave->state = SLAVE_PAUSED;
+               return;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid parameter\n");
+               return;
+       }
+
+       if (ret == 0) {
+               slave->state = SLAVE_RESUMED;
+               slave_rpc_ping_thaw(slave);
+               invoke_resumed_cb(slave);
+       }
+}
+
+static inline void invoke_paused_cb(struct slave_node *slave)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct event *event;
+
+       slave->in_event_process |= SLAVE_EVENT_PROCESS_PAUSE;
+       EINA_LIST_FOREACH_SAFE(slave->event_pause_list, l, n, event) {
+               if (event->deleted || event->evt_cb(event->slave, event->cbdata) < 0 || event->deleted) {
+                       slave->event_pause_list = eina_list_remove(slave->event_pause_list, event);
+                       DbgFree(event);
+               }
+       }
+       slave->in_event_process &= ~SLAVE_EVENT_PROCESS_PAUSE;
+}
+
+static void pause_cb(struct slave_node *slave, const struct packet *packet, void *data)
+{
+       int ret;
+
+       if (slave->state == SLAVE_REQUEST_TO_TERMINATE || slave->state == SLAVE_REQUEST_TO_DISCONNECT) {
+               DbgPrint("Slave is terminating now. ignore pause result\n");
+               return;
+       }
+
+       if (!packet) {
+               ErrPrint("Failed to change the state of the slave\n");
+               slave->state = SLAVE_RESUMED;
+               return;
+       }
+
+       if (packet_get(packet, "i", &ret) != 1) {
+               ErrPrint("Invalid parameter\n");
+               return;
+       }
+
+       if (ret == 0) {
+               slave->state = SLAVE_PAUSED;
+               slave_rpc_ping_freeze(slave);
+               invoke_paused_cb(slave);
+       }
+}
+
+HAPI int slave_resume(struct slave_node *slave)
+{
+       double timestamp;
+       struct packet *packet;
+       unsigned int cmd = CMD_RESUME;
+
+       switch (slave->state) {
+       case SLAVE_REQUEST_TO_DISCONNECT:
+       case SLAVE_REQUEST_TO_TERMINATE:
+       case SLAVE_TERMINATED:
+               ErrPrint("Slave state[%d]\n", slave->state);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       case SLAVE_RESUMED:
+       case SLAVE_REQUEST_TO_RESUME:
+               ErrPrint("Slave state[%d]\n", slave->state);
+               return WIDGET_ERROR_NONE;
+       default:
+               break;
+       }
+
+       timestamp = util_timestamp();
+
+       packet = packet_create((const char *)&cmd, "d", timestamp);
+       if (!packet) {
+               ErrPrint("Failed to prepare param\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       slave->state = SLAVE_REQUEST_TO_RESUME;
+       return slave_rpc_async_request(slave, NULL, packet, resume_cb, NULL, 0);
+}
+
+HAPI int slave_pause(struct slave_node *slave)
+{
+       double timestamp;
+       struct packet *packet;
+       unsigned int cmd = CMD_PAUSE;
+
+       switch (slave->state) {
+       case SLAVE_REQUEST_TO_DISCONNECT:
+       case SLAVE_REQUEST_TO_TERMINATE:
+       case SLAVE_TERMINATED:
+               ErrPrint("Slave state[%d]\n", slave->state);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       case SLAVE_PAUSED:
+       case SLAVE_REQUEST_TO_PAUSE:
+               ErrPrint("Slave state[%d]\n", slave->state);
+               return WIDGET_ERROR_NONE;
+       default:
+               break;
+       }
+
+       timestamp = util_timestamp();
+
+       packet = packet_create((const char *)&cmd, "d", timestamp);
+       if (!packet) {
+               ErrPrint("Failed to prepare param\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       slave->state = SLAVE_REQUEST_TO_PAUSE;
+       return slave_rpc_async_request(slave, NULL, packet, pause_cb, NULL, 0);
+}
+
+HAPI const char *slave_pkgname(const struct slave_node *slave)
+{
+       return slave ? slave->pkgname : NULL;
+}
+
+HAPI enum slave_state slave_state(const struct slave_node *slave)
+{
+       return slave ? slave->state : SLAVE_ERROR;
+}
+
+HAPI void slave_set_state(struct slave_node *slave, enum slave_state state)
+{
+       slave->state = state;
+}
+
+HAPI const char *slave_state_string(const struct slave_node *slave)
+{
+       switch (slave->state) {
+       case SLAVE_REQUEST_TO_DISCONNECT:
+               return "RequestToDisconnect";
+       case SLAVE_REQUEST_TO_LAUNCH:
+               return "RequestToLaunch";
+       case SLAVE_REQUEST_TO_TERMINATE:
+               return "RequestToTerminate";
+       case SLAVE_TERMINATED:
+               return "Terminated";
+       case SLAVE_REQUEST_TO_PAUSE:
+               return "RequestToPause";
+       case SLAVE_REQUEST_TO_RESUME:
+               return "RequestToResume";
+       case SLAVE_PAUSED:
+               return "Paused";
+       case SLAVE_RESUMED:
+               return "Resumed";
+       case SLAVE_ERROR:
+               return "Error";
+       default:
+               break;
+       }
+
+       return "Unknown";
+}
+
+HAPI const void *slave_list(void)
+{
+       return s_info.slave_list;
+}
+
+HAPI int const slave_fault_count(const struct slave_node *slave)
+{
+       return slave->fault_count;
+}
+
+HAPI double const slave_ttl(const struct slave_node *slave)
+{
+       if (!slave->ttl_timer) {
+               return 0.0f;
+       }
+
+       return ecore_timer_pending_get(slave->ttl_timer);
+}
+
+HAPI void slave_set_reactivate_instances(struct slave_node *slave, int reactivate)
+{
+       slave->reactivate_instances = reactivate;
+}
+
+HAPI int slave_need_to_reactivate_instances(struct slave_node *slave)
+{
+       return slave->reactivate_instances;
+}
+
+HAPI void slave_set_reactivation(struct slave_node *slave, int flag)
+{
+       slave->reactivate_slave = flag;
+}
+
+HAPI int slave_need_to_reactivate(struct slave_node *slave)
+{
+       int reactivate;
+
+       if (!WIDGET_CONF_REACTIVATE_ON_PAUSE) {
+               Eina_List *pkg_list;
+               Eina_List *l;
+               struct pkg_info *info;
+
+               /**
+                * @TODO
+                * Check all instances on this slave, whether they are all paused or not.
+                */
+               pkg_list = (Eina_List *)package_list();
+
+               reactivate = 0;
+
+               EINA_LIST_FOREACH(pkg_list, l, info) {
+                       if (package_slave(info) == slave) {
+                               Eina_List *inst_list;
+                               Eina_List *n;
+                               struct inst_info *inst;
+
+                               inst_list = (Eina_List *)package_instance_list(info);
+                               EINA_LIST_FOREACH(inst_list, n, inst) {
+                                       if (instance_visible_state(inst) == WIDGET_SHOW) {
+                                               reactivate++;
+                                       }
+                               }
+                       }
+               }
+               DbgPrint("visible instances: %d\n", reactivate);
+       } else {
+               reactivate = 1;
+       }
+
+       return reactivate && slave->reactivate_slave;
+}
+
+HAPI int slave_network(const struct slave_node *slave)
+{
+       return slave->network;
+}
+
+HAPI void slave_set_network(struct slave_node *slave, int network)
+{
+       slave->network = network;
+}
+
+HAPI int slave_deactivate_all(int reactivate, int reactivate_instances, int no_timer)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct slave_node *slave;
+       int cnt = 0;
+
+       s_info.deactivate_all_refcnt++;
+       if (s_info.deactivate_all_refcnt > 1) {
+               return 0;
+       }
+       DbgPrint("Deactivate all\n");
+
+       EINA_LIST_FOREACH_SAFE(s_info.slave_list, l, n, slave) {
+               if (slave_is_activated(slave)) {
+                       slave_set_reactivate_instances(slave, reactivate_instances);
+                       slave_set_reactivation(slave, reactivate);
+
+                       if (!slave_deactivate(slave, no_timer)) {
+                               s_info.slave_list = eina_list_remove(s_info.slave_list, slave);
+                       }
+               }
+
+               cnt++;
+       }
+
+       return cnt;
+}
+
+HAPI int slave_activate_all(void)
+{
+       Eina_List *l;
+       struct slave_node *slave;
+       int cnt = 0;
+
+       s_info.deactivate_all_refcnt--;
+       if (s_info.deactivate_all_refcnt > 0) {
+               return 0;
+       }
+       DbgPrint("Activate all\n");
+
+       EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
+               slave_activate(slave);
+               cnt++;
+       }
+
+       return cnt;
+}
+
+HAPI void slave_set_control_option(struct slave_node *slave, int ctrl)
+{
+       slave->ctrl_option = ctrl;
+}
+
+HAPI int slave_control_option(struct slave_node *slave)
+{
+       return slave->ctrl_option;
+}
+
+HAPI int slave_set_priority(struct slave_node *slave, int priority)
+{
+       pid_t pid;
+
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pid = slave_pid(slave);
+       if (pid <= 0) {
+               DbgPrint("Skip for %d\n", pid);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (setpriority(PRIO_PROCESS, slave_pid(slave), priority) < 0) {
+               ErrPrint("setpriority: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_priority(struct slave_node *slave)
+{
+       pid_t pid;
+       int priority;
+
+       if (!slave) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       pid = slave_pid(slave);
+       if (pid <= 0) {
+               DbgPrint("Skip for %d\n", pid);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       priority = getpriority(PRIO_PROCESS, pid);
+       if (priority < 0) {
+               ErrPrint("getpriority: %d\n", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       return priority;
+}
+
+HAPI int slave_valid(const struct slave_node *slave)
+{
+    if (!slave->valid) {
+        DbgPrint("slave is invalid");
+    }
+
+    return slave->valid;
+}
+
+HAPI void slave_set_valid(struct slave_node *slave)
+{
+    DbgPrint("slave is set valid\n");
+    slave->valid = 1;
+}
+
+/* End of a file */
diff --git a/src/slave_rpc.c b/src/slave_rpc.c
new file mode 100644 (file)
index 0000000..3c0bd29
--- /dev/null
@@ -0,0 +1,712 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h> /* strerror */
+#include <errno.h> /* errno */
+#include <unistd.h> /* pid_t */
+#include <stdlib.h> /* free */
+#include <assert.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+
+#include <dlog.h>
+
+#include <packet.h>
+#include <com-core_packet.h>
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <widget_cmd_list.h>
+#include <widget_conf.h>
+
+#include "debug.h"
+#include "slave_life.h"
+#include "slave_rpc.h"
+#include "client_life.h"
+#include "package.h"
+#include "fault_manager.h"
+#include "util.h"
+#include "conf.h"
+
+struct slave_rpc {
+       Ecore_Timer *pong_timer;
+       int handle;
+
+       unsigned long ping_count;
+       unsigned long next_ping_count;
+       Eina_List *pending_list;
+};
+
+struct command {
+       /* create_command, destroy_command will care these varaibles */
+       char *pkgname;
+       struct packet *packet;
+       struct slave_node *slave;
+       int ttl; /* If it fails to handle this, destroy this */
+
+       /* Don't need to care these data */
+       void (*ret_cb)(struct slave_node *slave, const struct packet *packet, void *cbdata);
+       void *cbdata;
+};
+
+static struct info {
+       Eina_List *command_list;
+       Ecore_Timer *command_consuming_timer;
+} s_info = {
+       .command_list = NULL,
+       .command_consuming_timer = NULL,
+};
+
+#define DEFAULT_CMD_TTL 3
+
+static void prepend_command(struct command *command);
+
+static inline struct command *create_command(struct slave_node *slave, const char *pkgname, struct packet *packet)
+{
+       struct command *command;
+
+       command = calloc(1, sizeof(*command));
+       if (!command) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       if (pkgname) {
+               command->pkgname = strdup(pkgname);
+               if (!command->pkgname) {
+                       ErrPrint("strdup: %d\n", errno);
+                       DbgFree(command);
+                       return NULL;
+               }
+       }
+
+       command->slave = slave_ref(slave); /*!< To prevent from destroying of the slave while communicating with the slave */
+       command->packet = packet_ref(packet);
+       command->ttl = DEFAULT_CMD_TTL;
+
+       return command;
+}
+
+static inline void destroy_command(struct command *command)
+{
+       slave_unref(command->slave);
+       packet_unref(command->packet);
+       DbgFree(command->pkgname);
+       DbgFree(command);
+}
+
+static inline struct command *pop_command(void)
+{
+       struct command *command;
+
+       command = eina_list_nth(s_info.command_list, 0);
+       if (!command) {
+               return NULL;
+       }
+
+       s_info.command_list = eina_list_remove(s_info.command_list, command);
+       return command;
+}
+
+static int slave_async_cb(pid_t pid, int handle, const struct packet *packet, void *data)
+{
+       struct command *command = data;
+
+       if (!command) {
+               ErrPrint("Command is NIL\n");
+               return WIDGET_ERROR_NONE;
+       }
+
+       /*!
+        * \note
+        * command->packet is not valid from here.
+        */
+       if (!slave_is_activated(command->slave)) {
+               ErrPrint("Slave is not activated (accidently dead)\n");
+               if (command->ret_cb) {
+                       command->ret_cb(command->slave, packet, command->cbdata);
+               }
+               goto out;
+       }
+
+       if (!packet) {
+               DbgPrint("packet == NULL\n");
+               if (command->ret_cb) {
+                       command->ret_cb(command->slave, packet, command->cbdata);
+               }
+
+               /*
+                * \NOTE
+                * Slave will be deactivated from dead monitor if it lost its connections.
+                * So we don't need to care it again from here.
+
+                command->slave = slave_deactivated_by_fault(command->slave);
+
+                */
+               goto out;
+       }
+
+       if (command->ret_cb) {
+               command->ret_cb(command->slave, packet, command->cbdata);
+       }
+
+out:
+       destroy_command(command);
+       return WIDGET_ERROR_NONE;
+}
+
+static Eina_Bool command_consumer_cb(void *data)
+{
+       struct command *command;
+       struct slave_rpc *rpc;
+
+       command = pop_command();
+       if (!command) {
+               s_info.command_consuming_timer = NULL;
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!slave_is_activated(command->slave)) {
+               ErrPrint("Slave is not activated: %s(%d)\n",
+                               slave_name(command->slave), slave_pid(command->slave));
+               goto errout;
+       }
+
+       if (command->pkgname) {
+               struct pkg_info *info;
+
+               info = package_find(command->pkgname);
+               if (info && package_is_fault(info)) {
+                       ErrPrint("info: %p (%s) is fault package\n", info, command->pkgname);
+                       // goto errout;
+               }
+       }
+
+       rpc = slave_data(command->slave, "rpc");
+       if (!rpc || rpc->handle < 0) {
+               ErrPrint("Slave has no rpc info\n");
+               goto errout;
+       }
+
+       if (packet_type(command->packet) == PACKET_REQ_NOACK) {
+               if (com_core_packet_send_only(rpc->handle, command->packet) == 0) {
+                       /* Keep a slave alive, while processing events */
+                       slave_give_more_ttl(command->slave);
+                       destroy_command(command);
+                       return ECORE_CALLBACK_RENEW;
+               }
+       } else if (packet_type(command->packet) == PACKET_REQ) {
+               if (com_core_packet_async_send(rpc->handle, command->packet, 0.0f, slave_async_cb, command) == 0) {
+                       /* Keep a slave alive, while processing events */
+                       slave_give_more_ttl(command->slave);
+                       return ECORE_CALLBACK_RENEW;
+               }
+       }
+
+       /*!
+        * \WARN
+        * What happens at here?
+        * We are failed to send a packet!!!
+        * Let's try to send this again
+        */
+       /*!
+        * \todo
+        * Do we need to handle this error?
+        * Close current connection and make new one?
+        * how about pended command lists?
+        */
+       DbgPrint("Packet type: %d\n", packet_type(command->packet));
+       DbgPrint("Packet: %p\n", command->packet);
+       DbgPrint("Handle: %d\n", rpc->handle);
+       DbgPrint("PID: %d\n", slave_pid(command->slave));
+       DbgPrint("Name: %s\n", slave_name(command->slave));
+       DbgPrint("Package: %s\n", command->pkgname);
+       command->ttl--;
+       if (command->ttl == 0) {
+               DbgPrint("Discard packet (%d)\n", command->ttl);
+               destroy_command(command);
+       } else {
+               DbgPrint("Send again (%d)\n", command->ttl);
+               prepend_command(command);
+       }
+       return ECORE_CALLBACK_RENEW;
+
+errout:
+       if (command->ret_cb) {
+               command->ret_cb(command->slave, NULL, command->cbdata);
+       }
+
+       destroy_command(command);
+       return ECORE_CALLBACK_RENEW;
+}
+
+static void prepend_command(struct command *command)
+{
+       s_info.command_list = eina_list_prepend(s_info.command_list, command);
+
+       if (s_info.command_consuming_timer) {
+               return;
+       }
+
+       s_info.command_consuming_timer = ecore_timer_add(WIDGET_CONF_PACKET_TIME, command_consumer_cb, NULL);
+       if (!s_info.command_consuming_timer) {
+               ErrPrint("Failed to add command consumer\n");
+               s_info.command_list = eina_list_remove(s_info.command_list, command);
+               destroy_command(command);
+       }
+}
+
+static void push_command(struct command *command)
+{
+       s_info.command_list = eina_list_append(s_info.command_list, command);
+
+       if (s_info.command_consuming_timer) {
+               return;
+       }
+
+       s_info.command_consuming_timer = ecore_timer_add(WIDGET_CONF_PACKET_TIME, command_consumer_cb, NULL);
+       if (!s_info.command_consuming_timer) {
+               ErrPrint("Failed to add command consumer\n");
+               s_info.command_list = eina_list_remove(s_info.command_list, command);
+               destroy_command(command);
+       }
+}
+
+static int slave_deactivate_cb(struct slave_node *slave, void *data)
+{
+       struct slave_rpc *rpc;
+       struct command *command;
+       Eina_List *l;
+       Eina_List *n;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               /*!
+                * \note
+                * Return negative value will remove this callback from the event list of the slave
+                */
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (rpc->pong_timer) {
+               ecore_timer_del(rpc->pong_timer);
+               rpc->pong_timer = NULL;
+       } else {
+               ErrPrint("slave has no pong timer\n");
+       }
+
+       if (rpc->handle < 0) {
+               EINA_LIST_FREE(rpc->pending_list, command) {
+                       assert(command->slave == slave);
+                       if (command->ret_cb) {
+                               command->ret_cb(command->slave, NULL, command->cbdata);
+                       }
+                       destroy_command(command);
+               }
+       } else {
+               EINA_LIST_FOREACH_SAFE(s_info.command_list, l, n, command) {
+                       if (command->slave == slave) {
+                               s_info.command_list = eina_list_remove(s_info.command_list, command);
+                               if (command->ret_cb) {
+                                       command->ret_cb(command->slave, NULL, command->cbdata);
+                               }
+                               destroy_command(command);
+                       }
+               }
+       }
+
+       /*!
+        * \note
+        * Reset handle
+        */
+       DbgPrint("Reset handle for %d (%d)\n", slave_pid(slave), rpc->handle);
+       rpc->handle = -1;
+
+       /*!
+        * \todo
+        * Make statistics table
+        */
+       rpc->ping_count = 0;
+       rpc->next_ping_count = 1;
+       return WIDGET_ERROR_NONE;
+}
+
+static Eina_Bool ping_timeout_cb(void *data)
+{
+       struct slave_rpc *rpc;
+       struct slave_node *slave = data;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave RPC is not valid (%s)\n", slave_name(slave));
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       /*!
+        * \note
+        * Clear the pong_timer
+        */
+       rpc->pong_timer = NULL;
+
+       if (!slave_is_activated(slave)) {
+               ErrPrint("Slave is not activated (%s)\n", slave_name(slave));
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       /*!
+        * Dead callback will handling this
+        */
+       DbgPrint("Slave PING TIMEOUT: %s(%d) : %p\n", slave_name(slave), slave_pid(slave), slave);
+       slave = slave_deactivated_by_fault(slave);
+       if (!slave) {
+               DbgPrint("Slave is deleted\n");
+       }
+
+       return ECORE_CALLBACK_CANCEL;
+}
+
+HAPI int slave_rpc_async_request(struct slave_node *slave, const char *pkgname, struct packet *packet, void (*ret_cb)(struct slave_node *slave, const struct packet *packet, void *data), void *data, int urgent)
+{
+       struct command *command;
+       struct slave_rpc *rpc;
+
+       command = create_command(slave, pkgname, packet);
+       if (!command) {
+               ErrPrint("Failed to create command\n");
+
+               if (ret_cb) {
+                       ret_cb(slave, NULL, data);
+               }
+
+               packet_unref(packet);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       command->ret_cb = ret_cb;
+       command->cbdata = data;
+       packet_unref(packet);
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave has no RPC\n");
+               if (ret_cb) {
+                       ret_cb(slave, NULL, data);
+               }
+               destroy_command(command);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (rpc->handle < 0) {
+               DbgPrint("RPC handle is not ready to use it\n");
+               if (((slave_control_option(slave) & PROVIDER_CTRL_MANUAL_REACTIVATION) == PROVIDER_CTRL_MANUAL_REACTIVATION || slave_is_secured(slave) || slave_is_app(slave) || (WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL))
+                               && !slave_is_activated(slave))
+               {
+                       int ret;
+                       DbgPrint("Activate slave forcely\n");
+                       ret = slave_activate(slave);
+                       if (ret < 0 && ret != WIDGET_ERROR_ALREADY_STARTED) {
+
+                               if (ret_cb) {
+                                       ret_cb(slave, NULL, data);
+                               }
+
+                               destroy_command(command);
+                               return ret;
+                       }
+               }
+
+               if (urgent) {
+                       rpc->pending_list = eina_list_prepend(rpc->pending_list, command);
+               } else {
+                       rpc->pending_list = eina_list_append(rpc->pending_list, command);
+               }
+
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (urgent) {
+               prepend_command(command);
+       } else {
+               push_command(command);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_request_only(struct slave_node *slave, const char *pkgname, struct packet *packet, int urgent)
+{
+       struct command *command;
+       struct slave_rpc *rpc;
+
+       command = create_command(slave, pkgname, packet);
+       if (!command) {
+               ErrPrint("Failed to create a command\n");
+               packet_unref(packet);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       command->ret_cb = NULL;
+       command->cbdata = NULL;
+       packet_unref(packet);
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave has no RPC\n");
+               destroy_command(command);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (rpc->handle < 0) {
+               DbgPrint("RPC handle is not ready to use it\n");
+               if (((slave_control_option(slave) & PROVIDER_CTRL_MANUAL_REACTIVATION) == PROVIDER_CTRL_MANUAL_REACTIVATION || slave_is_secured(slave) || slave_is_app(slave) || (WIDGET_IS_INHOUSE(slave_abi(slave)) && WIDGET_CONF_SLAVE_LIMIT_TO_TTL))
+                               && !slave_is_activated(slave))
+               {
+                       int ret;
+
+                       DbgPrint("Activate slave forcely\n");
+                       ret = slave_activate(slave);
+                       if (ret < 0 && ret != WIDGET_ERROR_ALREADY_STARTED) {
+                               destroy_command(command);
+                               return ret;
+                       }
+               }
+
+               if (urgent) {
+                       rpc->pending_list = eina_list_prepend(rpc->pending_list, command);
+               } else {
+                       rpc->pending_list = eina_list_append(rpc->pending_list, command);
+               }
+
+               return WIDGET_ERROR_NONE;
+       }
+
+       if (urgent) {
+               prepend_command(command);
+       } else {
+               push_command(command);
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_update_handle(struct slave_node *slave, int handle, int delete_pending_packet)
+{
+       struct slave_rpc *rpc;
+       struct command *command;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       DbgPrint("SLAVE: New handle assigned for %d, %d\n", slave_pid(slave), handle);
+       rpc->handle = handle;
+       if (rpc->pong_timer) {
+               ecore_timer_del(rpc->pong_timer);
+       }
+
+       rpc->pong_timer = ecore_timer_add(WIDGET_CONF_DEFAULT_PING_TIME, ping_timeout_cb, slave);
+       if (!rpc->pong_timer) {
+               ErrPrint("Failed to add ping timer\n");
+       }
+
+       /*!
+        * \note
+        * slave_activated will call the activated callback.
+        * activated callback will try to recover the normal instances state.
+        * so the reset_fault should be called after slave_activated function.
+        */
+       slave_activated(slave);
+
+       EINA_LIST_FREE(rpc->pending_list, command) {
+               if (delete_pending_packet) {
+                       destroy_command(command);
+               } else {
+                       push_command(command);
+               }
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_init(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = calloc(1, sizeof(*rpc));
+       if (!rpc) {
+               ErrPrint("calloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (slave_set_data(slave, "rpc", rpc) < 0) {
+               DbgFree(rpc);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_deactivate_cb, NULL) < 0) {
+               ErrPrint("Failed to add event callback\n");
+       }
+
+       rpc->ping_count = 0;
+       rpc->next_ping_count = 1;
+       rpc->handle = -1;
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_fini(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = slave_del_data(slave, "rpc");
+       if (!rpc) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_deactivate_cb, NULL);
+
+       if (rpc->pong_timer) {
+               ecore_timer_del(rpc->pong_timer);
+       }
+
+       DbgFree(rpc);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_ping(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave RPC is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!slave_is_activated(slave)) {
+               ErrPrint("Slave is not activated\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       rpc->ping_count++;
+       if (rpc->ping_count != rpc->next_ping_count) {
+               ErrPrint("Ping count is not correct\n");
+               rpc->next_ping_count = rpc->ping_count;
+       }
+       rpc->next_ping_count++;
+
+       ecore_timer_reset(rpc->pong_timer);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_ping_freeze(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave RPC is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!slave_is_activated(slave)) {
+               ErrPrint("Slave is not activated\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ecore_timer_freeze(rpc->pong_timer);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int slave_rpc_ping_thaw(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               ErrPrint("Slave RPC is not valid\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!slave_is_activated(slave)) {
+               ErrPrint("Slave is not activated\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       ecore_timer_thaw(rpc->pong_timer);
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void slave_rpc_request_update(const char *pkgname, const char *id, const char *cluster, const char *category, const char *content, int force)
+{
+       struct slave_node *slave;
+       struct pkg_info *info;
+       struct packet *packet;
+       unsigned int cmd = CMD_UPDATE_CONTENT;
+
+       info = package_find(pkgname);
+       if (!info) {
+               ErrPrint("Failed to find a package\n");
+               return;
+       }
+
+       slave = package_slave(info);
+       if (!slave) {
+               ErrPrint("Failed to find a slave for %s\n", pkgname);
+               return;
+       }
+
+       packet = packet_create_noack((const char *)&cmd, "sssssi", pkgname, id, cluster, category, content, force);
+       if (!packet) {
+               ErrPrint("Failed to create a new param\n");
+               return;
+       }
+
+       (void)slave_rpc_request_only(slave, pkgname, packet, 0);
+}
+
+HAPI int slave_rpc_handle(struct slave_node *slave)
+{
+       struct slave_rpc *rpc;
+
+       rpc = slave_data(slave, "rpc");
+       if (!rpc) {
+               DbgPrint("Slave RPC is not initiated\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return rpc->handle;
+}
+
+HAPI int slave_rpc_disconnect(struct slave_node *slave)
+{
+       struct packet *packet;
+       unsigned int cmd = CMD_DISCONNECT;
+
+       packet = packet_create_noack((const char *)&cmd, "d", util_timestamp());
+       if (!packet) {
+               ErrPrint("Failed to create a packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Send disconnection request packet\n");
+       return slave_rpc_request_only(slave, NULL, packet, 0);
+}
+
+/* End of a file */
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..2e5f557
--- /dev/null
@@ -0,0 +1,584 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <sys/time.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/vfs.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <ctype.h>
+#include <sys/statvfs.h>
+#include <sys/mount.h>
+#include <strings.h>
+
+#include <sys/smack.h>
+#include <dlog.h>
+#include <Eina.h>
+#include <Ecore.h>
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#include <widget_conf.h>
+#else
+#include "lite-errno.h"
+#define WIDGET_CONF_IMAGE_PATH "/tmp/"
+#endif
+
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+
+#define DELIM ';'
+
+static struct info {
+       int emergency_mounted;
+} s_info = {
+       .emergency_mounted = 0,
+};
+
+int errno;
+
+HAPI unsigned long util_string_hash(const char *str)
+{
+       unsigned long ret = 0;
+
+       while (*str) {
+               ret += (unsigned long)(*str++);
+       }
+
+       ret %= 371773;
+       return ret;
+}
+
+HAPI double util_timestamp(void)
+{
+#if defined(_USE_ECORE_TIME_GET)
+       return ecore_time_get();
+#else
+       struct timeval tv;
+       if (gettimeofday(&tv, NULL) < 0) {
+               static unsigned long internal_count = 0;
+               ErrPrint("gettimeofday: %d\n", errno);
+               tv.tv_sec = internal_count++;
+               tv.tv_usec = 0;
+       }
+
+       return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
+#endif
+}
+
+HAPI int util_check_ext(const char *filename, const char *check_ptr)
+{
+       int name_len;
+
+       name_len = strlen(filename);
+       while (--name_len >= 0 && *check_ptr) {
+               if (filename[name_len] != *check_ptr) {
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+
+               check_ptr ++;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int util_unlink(const char *filename)
+{
+       char *descfile;
+       int desclen;
+       int ret;
+
+       if (!filename) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       desclen = strlen(filename) + 6; /* .desc */
+       descfile = malloc(desclen);
+       if (!descfile) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = snprintf(descfile, desclen, "%s.desc", filename);
+       if (ret < 0) {
+               ErrPrint("snprintf: %d\n", errno);
+               DbgFree(descfile);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       (void)unlink(descfile);
+       DbgFree(descfile);
+       (void)unlink(filename);
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI char *util_slavename(void)
+{
+       char slavename[BUFSIZ];
+       static unsigned long idx = 0;
+
+       snprintf(slavename, sizeof(slavename), "%lu_%lf", idx++, util_timestamp());
+       return strdup(slavename);
+}
+
+HAPI const char *util_basename(const char *name)
+{
+       int length;
+       length = name ? strlen(name) : 0;
+       if (!length) {
+               return ".";
+       }
+
+       while (--length > 0 && name[length] != '/');
+
+       return length <= 0 ? name : (name + length + (name[length] == '/'));
+}
+
+/*!
+ * Return size of stroage in Bytes unit.
+ */
+HAPI unsigned long long util_free_space(const char *path)
+{
+       struct statvfs st;
+       unsigned long long space;
+
+       if (statvfs(path, &st) < 0) {
+               ErrPrint("statvfs: %d\n", errno);
+               return 0lu;
+       }
+
+       space = (unsigned long long)st.f_bsize * (unsigned long long)st.f_bavail;
+       DbgPrint("Available size: %llu, f_bsize: %lu, f_bavail: %lu\n", space, st.f_bsize, st.f_bavail);
+       /*!
+        * \note
+        * Must have to check the overflow
+        */
+
+       return space;
+}
+
+static inline char *extend_heap(char *buffer, int *sz, int incsz)
+{
+       char *tmp;
+
+       *sz += incsz;
+       tmp = realloc(buffer, *sz);
+       if (!tmp) {
+               ErrPrint("realloc: %d\n", errno);
+               return NULL;
+       }
+
+       return tmp;
+}
+
+HAPI double util_time_delay_for_compensation(double period)
+{
+       unsigned long long curtime;
+       unsigned long long _period;
+       unsigned long long remain;
+       struct timeval tv;
+       double ret;
+
+       if (period == 0.0f) {
+               DbgPrint("Period is ZERO\n");
+               return 0.0f;
+       }
+
+       if (gettimeofday(&tv, NULL) < 0){
+               ErrPrint("gettimeofday: %d\n", errno);
+               return period;
+       }
+
+       curtime = (unsigned long long)tv.tv_sec * 1000000llu + (unsigned long long)tv.tv_usec;
+
+       _period = (unsigned long long)(period * (double)1000000);
+       if (_period == 0llu) {
+               ErrPrint("%lf <> %llu\n", period, _period);
+               return period;
+       }
+
+       remain = curtime % _period;
+
+       ret = (double)remain / (double)1000000;
+       return period - ret;
+}
+
+HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
+{
+       Ecore_Timer *timer;
+       double delay;
+
+       timer = ecore_timer_add(interval, cb, data);
+       if (!timer) {
+               return NULL;
+       }
+
+       delay = util_time_delay_for_compensation(interval) - interval;
+       ecore_timer_delay(timer, delay);
+       DbgPrint("Compensate timer: %lf\n", delay);
+
+       return timer;
+}
+
+HAPI void util_timer_interval_set(void *timer, double interval)
+{
+       double delay;
+       ecore_timer_interval_set(timer, interval);
+
+       delay = util_time_delay_for_compensation(interval) - interval;
+       ecore_timer_delay(timer, delay);
+}
+
+HAPI int util_unlink_files(const char *folder)
+{
+       struct stat info;
+       DIR *handle;
+       struct dirent *entry;
+       char *abspath;
+       int len;
+
+       if (lstat(folder, &info) < 0) {
+               ErrPrint("lstat: %d\n", errno);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       if (!S_ISDIR(info.st_mode)) {
+               ErrPrint("Error: %s is not a folder", folder);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       handle = opendir(folder);
+       if (!handle) {
+               ErrPrint("opendir: %d\n", errno);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       while ((entry = readdir(handle))) {
+               if (!strcmp(entry->d_name, ".")) {
+                       continue;
+               }
+
+               if (!strcmp(entry->d_name, "..")) {
+                       continue;
+               }
+
+               len = strlen(folder) + strlen(entry->d_name) + 3;
+               abspath = calloc(1, len);
+               if (!abspath) {
+                       ErrPrint("calloc: %d\n", errno);
+                       continue;
+               }
+               snprintf(abspath, len - 1, "%s/%s", folder, entry->d_name);
+
+               if (unlink(abspath) < 0) {
+                       DbgPrint("unlink: %d\n", errno);
+               }
+
+               DbgFree(abspath);
+       }
+
+       if (closedir(handle) < 0) {
+               ErrPrint("closedir: %d\n", errno);
+       }
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void util_remove_emergency_disk(void)
+{
+       int ret;
+       ret = umount(WIDGET_CONF_IMAGE_PATH);
+       if (ret < 0) {
+               ErrPrint("umount: %d\n", errno);
+       }
+
+       DbgPrint("Try to unmount[%s] %d\n", WIDGET_CONF_IMAGE_PATH, ret);
+       s_info.emergency_mounted = 0;
+}
+
+HAPI void util_prepare_emergency_disk(void)
+{
+       char *buf;
+       char *source = NULL;
+       char *type = NULL;
+       char *option = NULL;
+       char *ptr;
+       char *rollback_ptr;
+       int tag_idx;
+       int idx;
+       int len;
+       int ret;
+       static const char *tag[] = {
+               "source",
+               "type",
+               "option",
+               NULL,
+       };
+       enum tag_type {
+               TAG_SOURCE,
+               TAG_TYPE,
+               TAG_OPTION,
+               TAG_ERROR
+       };
+
+       buf = strdup(WIDGET_CONF_EMERGENCY_DISK);
+       if (!buf) {
+               ErrPrint("Failed to prepare emergency disk info\n");
+               return;
+       }
+
+       rollback_ptr = ptr = buf;
+       idx = 0;
+       tag_idx = 0;
+       len = strlen(ptr);
+
+       while (tag[tag_idx] != NULL && ptr != (buf + len)) {
+               if (tag[tag_idx][idx] == '\0') {
+                       if (*ptr == '=' || isblank(*ptr)) {
+                               switch (tag_idx) {
+                               case TAG_SOURCE:
+                                       if (source) {
+                                               ErrPrint("source[%s] is overrided\n", source);
+                                       }
+
+                                       while ((*ptr != '\0' && *ptr != ';') && (*ptr == '=' || isblank(*ptr))) {
+                                               ptr++;
+                                       }
+
+                                       source = ptr;
+                                       while (*ptr != '\0' && *ptr != ';') {
+                                               ptr++;
+                                       }
+
+                                       if (*source == '\0') {
+                                               type = NULL;
+                                       }
+
+                                       *ptr = '\0';
+                                       rollback_ptr = ptr + 1;
+                                       idx = 0;
+                                       break;
+                               case TAG_TYPE:
+                                       if (type) {
+                                               ErrPrint("type[%s] is overrided\n", type);
+                                       }
+
+                                       while ((*ptr != '\0' && *ptr != ';') && (*ptr == '=' || isblank(*ptr))) {
+                                               ptr++;
+                                       }
+
+                                       type = ptr;
+                                       while (*ptr != '\0' && *ptr != ';') {
+                                               ptr++;
+                                       }
+
+                                       if (*type == '\0') {
+                                               type = NULL;
+                                       }
+
+                                       *ptr = '\0';
+                                       rollback_ptr = ptr + 1;
+                                       idx = 0;
+                                       break;
+                               case TAG_OPTION:
+                                       if (option) {
+                                               ErrPrint("option[%s] is overrided\n", option);
+                                       }
+
+                                       while ((*ptr != '\0' && *ptr != ';') && (*ptr == '=' || isblank(*ptr))) {
+                                               ptr++;
+                                       }
+
+                                       option = ptr;
+                                       while (*ptr != '\0' && *ptr != ';') {
+                                               ptr++;
+                                       }
+
+                                       if (*option == '\0') {
+                                               option = NULL;
+                                       }
+
+                                       *ptr = '\0';
+                                       rollback_ptr = ptr + 1;
+                                       idx = 0;
+                                       break;
+                               default:
+                                       break;
+                               }
+                       } else {
+                               ptr = rollback_ptr;
+                               tag_idx++;
+                               idx = 0;
+                       }
+               } else if (tag[tag_idx][idx] != *ptr) {
+                       ptr = rollback_ptr;
+                       tag_idx++;
+                       idx = 0;
+               } else {
+                       ptr++;
+                       idx++;
+               } // tag
+       }
+
+       DbgPrint("source[%s] type[%s] option[%s]\n", source, type, option);
+
+       ret = mount(source, WIDGET_CONF_IMAGE_PATH, type, MS_NOSUID | MS_NOEXEC, option);
+       DbgFree(buf);
+       if (ret < 0) {
+               ErrPrint("Failed to mount: %d\n", errno);
+               return;
+       }
+
+       ErrPrint("Disk space is not enough, use the tmpfs. Currently required minimum space is %lu bytes\n", WIDGET_CONF_MINIMUM_SPACE);
+       if (chmod(WIDGET_CONF_IMAGE_PATH, 0750) < 0) {
+               ErrPrint("chmod: %d\n", errno);
+       }
+
+       if (chown(WIDGET_CONF_IMAGE_PATH, 5000, 5000) < 0) {
+               ErrPrint("chown: %d\n", errno);
+       }
+
+       ret = smack_setlabel(WIDGET_CONF_IMAGE_PATH, DATA_SHARE_LABEL, SMACK_LABEL_ACCESS);
+       if (ret != 0) {
+               ErrPrint("Failed to set SMACK for %s (%d)\n", WIDGET_CONF_IMAGE_PATH, ret);
+       } else {
+               ret = smack_setlabel(WIDGET_CONF_IMAGE_PATH, "1", SMACK_LABEL_TRANSMUTE);
+               DbgPrint("[%s] is successfully created (t: %d)\n", WIDGET_CONF_IMAGE_PATH, ret);
+       }
+
+       if (mkdir(WIDGET_CONF_ALWAYS_PATH, 0755) < 0) {
+               ErrPrint("mkdir: (%s) %d\n", WIDGET_CONF_ALWAYS_PATH, errno);
+       } else {
+               if (chmod(WIDGET_CONF_ALWAYS_PATH, 0750) < 0) {
+                       ErrPrint("chmod: %d\n", errno);
+               }
+
+               if (chown(WIDGET_CONF_ALWAYS_PATH, 5000, 5000) < 0) {
+                       ErrPrint("chown: %d\n", errno);
+               }
+
+               ret = smack_setlabel(WIDGET_CONF_ALWAYS_PATH, DATA_SHARE_LABEL, SMACK_LABEL_ACCESS);
+               if (ret != 0) {
+                       ErrPrint("Failed to set SMACK for %s (%d)\n", WIDGET_CONF_ALWAYS_PATH, ret);
+               } else {
+                       ret = smack_setlabel(WIDGET_CONF_ALWAYS_PATH, "1", SMACK_LABEL_TRANSMUTE);
+                       DbgPrint("[%s] is successfully created (t: %d)\n", WIDGET_CONF_ALWAYS_PATH, ret);
+               }
+       }
+
+       if (mkdir(WIDGET_CONF_READER_PATH, 0755) < 0) {
+               ErrPrint("mkdir: (%s) %d\n", WIDGET_CONF_READER_PATH, errno);
+       } else {
+               if (chmod(WIDGET_CONF_READER_PATH, 0750) < 0) {
+                       ErrPrint("chmod: %d\n", errno);
+               }
+
+               if (chown(WIDGET_CONF_READER_PATH, 5000, 5000) < 0) {
+                       ErrPrint("chown: %d\n", errno);
+               }
+
+               ret = smack_setlabel(WIDGET_CONF_READER_PATH, DATA_SHARE_LABEL, SMACK_LABEL_ACCESS);
+               if (ret != 0) {
+                       ErrPrint("Failed to set SMACK for %s (%d)\n", WIDGET_CONF_READER_PATH, ret);
+               } else {
+                       ret = smack_setlabel(WIDGET_CONF_READER_PATH, "1", SMACK_LABEL_TRANSMUTE);
+                       DbgPrint("[%s] is successfully created (t: %d)\n", WIDGET_CONF_READER_PATH, ret);
+               }
+       }
+
+       s_info.emergency_mounted = 1;
+}
+
+HAPI int util_emergency_disk_is_mounted(void)
+{
+       return s_info.emergency_mounted;
+}
+
+HAPI void util_setup_log_disk(void)
+{
+       int ret;
+
+       if (access(WIDGET_CONF_LOG_PATH, R_OK | W_OK | X_OK) == 0) {
+               DbgPrint("[%s] is already accessible\n", WIDGET_CONF_LOG_PATH);
+               return;
+       }
+
+       DbgPrint("Initiate the critical log folder [%s]\n", WIDGET_CONF_LOG_PATH);
+       if (mkdir(WIDGET_CONF_LOG_PATH, 0755) < 0) {
+               ErrPrint("mkdir: %d\n", errno);
+       } else {
+               if (chmod(WIDGET_CONF_LOG_PATH, 0750) < 0) {
+                       ErrPrint("chmod: %d\n", errno);
+               }
+
+               if (chown(WIDGET_CONF_LOG_PATH, 5000, 5000) < 0) {
+                       ErrPrint("chown: %d\n", errno);
+               }
+
+               ret = smack_setlabel(WIDGET_CONF_LOG_PATH, DATA_SHARE_LABEL, SMACK_LABEL_ACCESS);
+               if (ret != 0) {
+                       ErrPrint("Failed to set SMACK for %s (%d)\n", WIDGET_CONF_LOG_PATH, ret);
+               } else {
+                       ret = smack_setlabel(WIDGET_CONF_LOG_PATH, "1", SMACK_LABEL_TRANSMUTE);
+                       DbgPrint("[%s] is successfully created (t: %d)\n", WIDGET_CONF_LOG_PATH, ret);
+               }
+       }
+}
+
+HAPI int util_service_is_enabled(const char *tag)
+{
+       return !!strcasestr(WIDGET_CONF_SERVICES, tag);
+}
+
+HAPI int util_string_is_in_list(const char *str, const char *haystack)
+{
+       int len;
+       const char *ptr;
+       const char *check;
+
+       if (!str) {
+               return 0;
+       }
+
+       check = ptr = haystack;
+       len = 0;
+       while (*ptr) {
+               switch (*ptr++) {
+               case DELIM:
+                       if (len > 0) {
+                               if (!strncasecmp(str, check, len) && str[len] == '\0') {
+                                       return 1;
+                               }
+                       }
+                       check = ptr;
+                       len = 0;
+                       break;
+               default:
+                       len++;
+                       break;
+               }
+       }
+
+       return (len > 0 && !strncmp(str, check, len) && str[len] == '\0');
+}
+
+/* End of a file */
diff --git a/src/util_wayland.c b/src/util_wayland.c
new file mode 100644 (file)
index 0000000..7d4300f
--- /dev/null
@@ -0,0 +1,42 @@
+#include "util.h"
+
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include "lite-errno.h"
+#endif
+
+static struct info {
+       int initialized;
+} s_info = {
+       .initialized = 0,
+};
+
+int util_screen_size_get(int *width, int *height)
+{
+       if (!s_info.initialized) {
+               ErrPrint("Not initialized\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       *width = 0;
+       *height = 0;
+//     ecore_wl_screen_size_get();
+       return WIDGET_ERROR_NOT_SUPPORTED;
+}
+
+int util_screen_init(void)
+{
+       s_info.initialized = 1;
+//     ecore_wl_init();
+       return WIDGET_ERROR_NONE;
+}
+
+int util_screen_fini(void)
+{
+       s_info.initialized = 0;
+//     ecore_wl_shutdown();
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/util_x11.c b/src/util_x11.c
new file mode 100644 (file)
index 0000000..cc62b7f
--- /dev/null
@@ -0,0 +1,41 @@
+#include <Ecore_X.h>
+#include <dlog.h>
+
+#if defined(HAVE_LIVEBOX)
+#include <widget_errno.h>
+#else
+#include "lite-errno.h"
+#endif
+#include "util.h"
+#include "debug.h"
+
+static struct info {
+       int initialized;
+} s_info = {
+       .initialized = 0,
+};
+
+int util_screen_size_get(int *width, int *height)
+{
+       if (!s_info.initialized) {
+               ErrPrint("Not initialized\n");
+               return WIDGET_ERROR_FAULT;
+       }
+       ecore_x_window_size_get(0, width, height);
+       return WIDGET_ERROR_NONE;
+}
+
+int util_screen_init(void)
+{
+       s_info.initialized = 1;
+       return ecore_x_init(NULL);
+}
+
+int util_screen_fini(void)
+{
+       s_info.initialized = 0;
+       return ecore_x_shutdown();
+}
+
+/* End of a file */
+
diff --git a/src/utility_service.c b/src/utility_service.c
new file mode 100644 (file)
index 0000000..69a49cd
--- /dev/null
@@ -0,0 +1,432 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+#include <aul.h>
+
+#include <widget_errno.h>
+#include <packet.h>
+
+#include <sys/smack.h>
+
+#include "critical_log.h"
+#include "service_common.h"
+#include "utility_service.h"
+#include "debug.h"
+#include "util.h"
+#include "conf.h"
+
+#ifndef SVC_PKG
+#define SVC_PKG                "org.tizen.data-provider-slave.icon"
+#endif
+
+#ifndef LAUNCH_TIMEOUT
+#define LAUNCH_TIMEOUT 10.0f
+#endif
+
+#ifndef TTL_TIMEOUT
+#define TTL_TIMEOUT    30.0f
+#endif
+
+static struct info {
+       Eina_List *pending_list;
+       Eina_List *context_list;
+       struct service_context *svc_ctx;
+
+       struct tcb *svc_daemon;
+       int svc_daemon_is_launched;
+       int svc_daemon_pid;
+
+       struct service_event_item *launch_timer; 
+       struct service_event_item *delay_launcher;
+       struct service_event_item *ttl_timer;
+} s_info = {
+       .pending_list = NULL,
+       .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
+       .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
+
+       .svc_daemon = NULL,
+       .svc_daemon_is_launched = 0,
+       .svc_daemon_pid = -1,
+
+       .launch_timer = NULL,
+       .delay_launcher = NULL,
+       .ttl_timer = NULL,
+};
+
+struct pending_item {
+       struct tcb *tcb;
+       struct packet *packet;
+};
+
+struct context {
+       struct tcb *tcb;
+       double seq;
+};
+
+static int lazy_launcher_cb(struct service_context *svc_ctx, void *data);
+
+static int put_reply_tcb(struct tcb *tcb, double seq)
+{
+       struct context *ctx;
+
+       ctx = malloc(sizeof(*ctx));
+       if (!ctx) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       ctx->tcb = tcb;
+       ctx->seq = seq;
+
+       s_info.context_list = eina_list_append(s_info.context_list, ctx);
+
+       return WIDGET_ERROR_NONE;
+}
+
+static inline struct tcb *get_reply_tcb(double seq)
+{
+       Eina_List *l;
+       Eina_List *n;
+       struct context *ctx;
+       struct tcb *tcb;
+
+       EINA_LIST_FOREACH_SAFE(s_info.context_list, l, n, ctx) {
+               if (ctx->seq != seq) {
+                       continue;
+               }
+
+               s_info.context_list = eina_list_remove(s_info.context_list, ctx);
+               tcb = ctx->tcb;
+               DbgFree(ctx);
+               return tcb;
+       }
+
+       return NULL;
+}
+
+static inline int flush_pended_request(void)
+{
+       /*!
+        * Flush all pended requests
+        */
+       struct pending_item *item;
+       int ret;
+
+       EINA_LIST_FREE(s_info.pending_list, item) {
+               if (tcb_is_valid(s_info.svc_ctx, s_info.svc_daemon) >= 0) {
+                       ret = service_common_unicast_packet(s_info.svc_daemon, item->packet);
+               } else {
+                       ret = -EFAULT;
+               }
+
+               if (ret < 0) {
+                       if (tcb_is_valid(s_info.svc_ctx, item->tcb) >= 0) {
+                               struct packet *reply;
+                               reply = packet_create_reply(item->packet, "i", ret);
+                               if (service_common_unicast_packet(item->tcb, reply) < 0) {
+                                       ErrPrint("Unable to send packet\n");
+                               }
+                               packet_destroy(reply);
+                       }
+               } else {
+                       put_reply_tcb(item->tcb, packet_seq(item->packet));
+               }
+               packet_unref(item->packet);
+               DbgFree(item);
+       }
+
+       return 0;
+}
+
+static inline int put_pended_request(struct tcb *tcb, struct packet *packet)
+{
+       struct pending_item *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->tcb = tcb;
+       item->packet = packet_ref(packet);
+       if (!item->packet) {
+               DbgFree(item);
+               ErrPrint("Unable to ref packet\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       s_info.pending_list = eina_list_append(s_info.pending_list, item);
+       return 0;
+}
+
+static int launch_timeout_cb(struct service_context *svc_ctx, void *data)
+{
+       struct pending_item *item;
+       struct packet *reply;
+
+       EINA_LIST_FREE(s_info.pending_list, item) {
+               if (tcb_is_valid(s_info.svc_ctx, item->tcb) >= 0) {
+                       reply = packet_create_reply(item->packet, "i", -EFAULT);
+                       if (!reply) {
+                               ErrPrint("Unable to create a packet\n");
+                       } else {
+                               int ret;
+
+                               ret = service_common_unicast_packet(item->tcb, reply);
+                               if (ret < 0) {
+                                       ErrPrint("Failed to send reply packet: %d\n", ret);
+                               }
+
+                               packet_destroy(reply);
+                       }
+               } else {
+                       ErrPrint("TCB is already terminated\n");
+               }
+
+               packet_unref(item->packet);
+               DbgFree(item);
+       }
+
+       s_info.launch_timer = NULL;
+       s_info.svc_daemon_is_launched = 0;
+       s_info.svc_daemon_pid = -1;
+       return -ECANCELED; /* Delete this timer */
+}
+
+static int launch_svc(struct service_context *svc_ctx)
+{
+       pid_t pid;
+       int ret = WIDGET_ERROR_NONE;
+
+       pid = aul_launch_app(SVC_PKG, NULL);
+       switch (pid) {
+       case AUL_R_EHIDDENFORGUEST:     /**< App hidden for guest mode */
+       case AUL_R_ENOLAUNCHPAD:        /**< no launchpad */
+       case AUL_R_EILLACC:             /**< Illegal Access */
+       case AUL_R_EINVAL:              /**< Invalid argument */
+       case AUL_R_ENOINIT:             /**< AUL handler NOT initialized */
+       case AUL_R_ERROR:               /**< General error */
+               ErrPrint("Failed to launch an app: %s(%d)\n", SVC_PKG, pid);
+               ret = WIDGET_ERROR_FAULT;
+               break;
+       case AUL_R_ETIMEOUT:            /**< Timeout */
+       case AUL_R_ECOMM:               /**< Comunication Error */
+       case AUL_R_ETERMINATING:        /**< application terminating */
+       case AUL_R_ECANCELED:           /**< Operation canceled */
+               /* Need time to launch app again */
+               ErrPrint("Terminating now, try to launch this after few sec later: %s(%d)\n", SVC_PKG, pid);
+               s_info.svc_daemon_is_launched = 1;
+               s_info.delay_launcher = service_common_add_timer(svc_ctx, LAUNCH_TIMEOUT, lazy_launcher_cb, NULL);
+               if (!s_info.delay_launcher) {
+                       ErrPrint("Unable to add delay launcher\n");
+                       ret = WIDGET_ERROR_FAULT;
+               }
+               break;
+       case AUL_R_LOCAL:               /**< Launch by himself */
+       case AUL_R_OK:                  /**< General success */
+       default:
+               DbgPrint("Launched: %s(%d)\n", SVC_PKG, pid);
+               s_info.svc_daemon_is_launched = 1;
+               s_info.svc_daemon_pid = pid;
+               s_info.launch_timer = service_common_add_timer(svc_ctx, LAUNCH_TIMEOUT, launch_timeout_cb, NULL);
+               if (!s_info.launch_timer) {
+                       ErrPrint("Unable to create launch timer\n");
+               }
+       }
+
+       return ret;
+}
+
+static int lazy_launcher_cb(struct service_context *svc_ctx, void *data)
+{
+       s_info.delay_launcher = NULL;
+
+       (void)launch_svc(svc_ctx);
+       return -ECANCELED;
+}
+
+static int ttl_timer_cb(struct service_context *svc_ctx, void *data)
+{
+       DbgPrint("TTL Timer is expired: PID(%d)\n", s_info.svc_daemon_pid);
+       (void)aul_terminate_pid_async(s_info.svc_daemon_pid);
+
+       s_info.ttl_timer = NULL;
+       s_info.svc_daemon_is_launched = 0;
+       s_info.svc_daemon_pid = -1;
+       s_info.svc_daemon = NULL;
+       return -ECANCELED;
+}
+
+static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
+{
+       struct packet *reply;
+       const char *cmd;
+       int ret;
+
+       if (!packet) {
+               DbgPrint("TCB %p is terminated (NIL packet), %d\n", tcb, s_info.svc_daemon_pid);
+
+               if (tcb == s_info.svc_daemon) {
+                       s_info.svc_daemon = NULL;
+                       s_info.svc_daemon_is_launched = 0;
+                       s_info.svc_daemon_pid = -1;
+
+                       if (s_info.ttl_timer) {
+                               service_common_del_timer(tcb_svc_ctx(tcb), s_info.ttl_timer);
+                               s_info.ttl_timer = NULL;
+                       }
+               }
+
+               return WIDGET_ERROR_NONE;
+       }
+
+       cmd = packet_command(packet);
+       if (!cmd) {
+               ErrPrint("Invalid packet\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (packet_type(packet)) {
+       case PACKET_REQ:
+               if (!s_info.svc_daemon_is_launched) {
+                       ret = launch_svc(tcb_svc_ctx(tcb));
+                       if (ret != WIDGET_ERROR_NONE) {
+                               goto reply_out;
+                       }
+               }
+
+               if (!s_info.svc_daemon) {
+                       ret = put_pended_request(tcb, packet);
+                       if (ret < 0) {
+                               goto reply_out;
+                       }
+               } else if (tcb_is_valid(s_info.svc_ctx, s_info.svc_daemon) >= 0) { 
+                       ret = service_common_unicast_packet(s_info.svc_daemon, packet);
+                       if (ret <0) {
+                               goto reply_out;
+                       }
+
+                       put_reply_tcb(tcb, packet_seq(packet));
+
+                       if (s_info.ttl_timer && service_common_update_timer(s_info.ttl_timer, TTL_TIMEOUT) < 0) {
+                               ErrPrint("Failed to update timer\n");
+                       }
+               }
+
+               break;
+       case PACKET_REQ_NOACK:
+               if (!strcmp(cmd, "service_register")) {
+                       if (!s_info.svc_daemon_is_launched) {
+                               ErrPrint("Service daemon is not launched. but something tries to register a service\n");
+                               return WIDGET_ERROR_INVALID_PARAMETER;
+                       }
+
+                       if (s_info.svc_daemon) {
+                               ErrPrint("Service daemon is already prepared\n");
+                               return WIDGET_ERROR_INVALID_PARAMETER;
+                       }
+
+                       if (s_info.launch_timer) {
+                               service_common_del_timer(tcb_svc_ctx(tcb), s_info.launch_timer);
+                               s_info.launch_timer = NULL;
+                       }
+
+                       s_info.ttl_timer = service_common_add_timer(tcb_svc_ctx(tcb), TTL_TIMEOUT, ttl_timer_cb, NULL);
+                       if (!s_info.ttl_timer) {
+                               ErrPrint("Failed to add TTL timer\n");
+                               if (s_info.svc_daemon_pid > 0) {
+                                       ret = aul_terminate_pid_async(s_info.svc_daemon_pid);
+                                       ErrPrint("Terminate: %d\n", ret);
+                                       s_info.svc_daemon_pid = -1;
+                               }
+                               s_info.svc_daemon_is_launched = 0;
+                               return WIDGET_ERROR_FAULT;
+                       }
+                       DbgPrint("TTL Timer is added: %p\n", s_info.ttl_timer);
+
+                       s_info.svc_daemon = tcb;
+                       flush_pended_request();
+               }
+               break;
+       case PACKET_ACK:
+               tcb = get_reply_tcb(packet_seq(packet));
+               if (!tcb) {
+                       ErrPrint("Unable to find reply tcb\n");
+                       break;
+               }
+
+               if (tcb_is_valid(s_info.svc_ctx, tcb) < 0) {
+                       ErrPrint("TCB is not valid\n");
+                       break;
+               }
+
+               ret = service_common_unicast_packet(tcb, packet);
+               if (ret < 0) {
+                       ErrPrint("Unable to forward the reply packet\n");
+               }
+               break;
+       default:
+               ErrPrint("Packet type is not valid[%s]\n", cmd);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+
+reply_out:
+       ErrPrint("Error: %d\n", ret);
+       reply = packet_create_reply(packet, "i", ret);
+       if (service_common_unicast_packet(tcb, reply) < 0) {
+               ErrPrint("Unable to send reply packet\n");
+       }
+       packet_destroy(reply);
+       return ret;
+}
+
+int utility_service_init(void)
+{
+       if (s_info.svc_ctx) {
+               ErrPrint("Already initialized\n");
+               return WIDGET_ERROR_ALREADY_STARTED;
+       }
+
+       s_info.svc_ctx = service_common_create(UTILITY_SOCKET, UTILITY_SMACK_LABEL, service_thread_main, NULL);
+       if (!s_info.svc_ctx) {
+               ErrPrint("Unable to activate service thread\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       DbgPrint("Successfully initiated\n");
+       return WIDGET_ERROR_NONE;
+}
+
+int utility_service_fini(void)
+{
+       if (!s_info.svc_ctx) {
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       service_common_destroy(s_info.svc_ctx);
+       s_info.svc_ctx = NULL;
+       DbgPrint("Successfully Finalized\n");
+       return WIDGET_ERROR_NONE;
+}
+
+/* End of a file */
diff --git a/src/widget-mgr.c b/src/widget-mgr.c
new file mode 100644 (file)
index 0000000..bf815e9
--- /dev/null
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <Eina.h>
+
+#include <dlog.h>
+#include <widget_errno.h>
+
+#include "util.h"
+#include "debug.h"
+#include "conf.h"
+
+int errno;
+
+static struct info {
+       Eina_List *info_list;
+} s_info = {
+       .info_list = NULL,
+};
+
+struct widget_mgr {
+       FILE *fp;
+       char fifo_name[60];
+       pid_t pid;
+       int handle;
+       void *data;
+};
+
+HAPI int widget_mgr_init(void)
+{
+       return 0;
+}
+
+HAPI void widget_mgr_fini(void)
+{
+       struct widget_mgr *info;
+
+       EINA_LIST_FREE(s_info.info_list, info) {
+               if (fclose(info->fp) != 0) {
+                       ErrPrint("fclose: %d\n", errno);
+               }
+               if (unlink(info->fifo_name) < 0) {
+                       ErrPrint("unlink: %d\n", errno);
+               }
+               DbgFree(info);
+       }
+}
+
+static inline int valid_requestor(pid_t pid)
+{
+       char cmdline[60]; /* strlen("/proc/%d/cmdline") + 30 */
+       struct stat target;
+       struct stat src;
+
+       snprintf(cmdline, sizeof(cmdline), "/proc/%d/exe", pid);
+
+       DbgPrint("Open cmdline: %s (%d)\n", cmdline, pid);
+
+       if (stat(cmdline, &target) < 0) {
+               ErrPrint("Error: %d\n", errno);
+               return 0;
+       }
+
+       if (stat("/opt/usr/devel/usr/bin/widget-mgr", &src) < 0) {
+               ErrPrint("Error: %d\n", errno);
+               return 0;
+       }
+
+       return target.st_ino == src.st_ino;
+}
+
+HAPI void widget_mgr_set_data(struct widget_mgr *info, void *data)
+{
+       info->data = data;
+}
+
+HAPI void *widget_mgr_data(struct widget_mgr *info)
+{
+       return info->data;
+}
+
+HAPI struct widget_mgr *widget_mgr_create(pid_t pid, int handle)
+{
+       struct widget_mgr *info;
+
+       if (!valid_requestor(pid)) {
+               ErrPrint("Invalid requestor\n");
+               return NULL;
+       }
+
+       info = calloc(1, sizeof(*info));
+       if (!info) {
+               ErrPrint("calloc: %d\n", errno);
+               return NULL;
+       }
+
+       snprintf(info->fifo_name, sizeof(info->fifo_name), "/tmp/.live_info.%lf", util_timestamp());
+       if (mkfifo(info->fifo_name, 0644) < 0) {
+               ErrPrint("mkfifo: %d\n", errno);
+               if (unlink(info->fifo_name) < 0) {
+                       ErrPrint("unlink: %d\n", errno);
+               }
+               DbgFree(info);
+               return NULL;
+       }
+
+       info->fp = NULL;
+       info->pid = pid;
+       info->handle = handle;
+
+       DbgPrint("Live info is successfully created\n");
+       s_info.info_list = eina_list_append(s_info.info_list, info);
+       return info;
+}
+
+HAPI int widget_mgr_open_fifo(struct widget_mgr *info)
+{
+       DbgPrint("FIFO is created (%s)\n", info->fifo_name);
+       info->fp = fopen(info->fifo_name, "w");
+       if (!info->fp) {
+               ErrPrint("open: %d\n", errno);
+               return WIDGET_ERROR_IO_ERROR;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void widget_mgr_close_fifo(struct widget_mgr *info)
+{
+       if (info->fp) {
+               if (fclose(info->fp) != 0) {
+                       ErrPrint("fclose: %d\n", errno);
+               }
+               info->fp = NULL;
+       }
+}
+
+HAPI void widget_mgr_destroy(struct widget_mgr *info)
+{
+       s_info.info_list = eina_list_remove(s_info.info_list, info);
+       widget_mgr_close_fifo(info);
+       if (unlink(info->fifo_name) < 0) {
+               ErrPrint("unlink: %d\n", errno);
+       }
+       DbgFree(info);
+}
+
+HAPI pid_t widget_mgr_pid(struct widget_mgr *info)
+{
+       return info ? info->pid : (pid_t)-1;
+}
+
+HAPI const char *widget_mgr_filename(struct widget_mgr *info)
+{
+       return info ? info->fifo_name : NULL;
+}
+
+HAPI FILE *widget_mgr_fifo(struct widget_mgr *info)
+{
+       return info ? info->fp : NULL;
+}
+
+HAPI struct widget_mgr *widget_mgr_find_by_pid(pid_t pid)
+{
+       Eina_List *l;
+       struct widget_mgr *info;
+
+       EINA_LIST_FOREACH(s_info.info_list, l, info) {
+               if (info->pid == pid) {
+                       return info;
+               }
+       }
+
+       return NULL;
+}
+
+HAPI struct widget_mgr *widget_mgr_find_by_handle(int handle)
+{
+       Eina_List *l;
+       struct widget_mgr *info;
+
+       EINA_LIST_FOREACH(s_info.info_list, l, info) {
+               if (info->handle == handle) {
+                       return info;
+               }
+       }
+
+       return NULL;
+}
+
+/* End of a file */
diff --git a/src/xmonitor.c b/src/xmonitor.c
new file mode 100644 (file)
index 0000000..e270157
--- /dev/null
@@ -0,0 +1,491 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <assert.h>
+#include <errno.h>
+#include <malloc.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <Ecore_X.h>
+#include <Ecore.h>
+
+#include <sqlite3.h>
+
+#include <gio/gio.h>
+#include <dlog.h>
+#include <widget_errno.h>
+#include <widget_conf.h>
+
+#include "conf.h"
+#include "debug.h"
+#include "client_life.h"
+#include "slave_life.h"
+#include "main.h"
+#include "util.h"
+#include "setting.h"
+#include "xmonitor.h"
+
+int errno;
+
+struct event_item {
+       int (*cb)(void *user_data);
+       void *user_data;
+};
+
+static struct info {
+       Ecore_Event_Handler *create_handler;
+       Ecore_Event_Handler *destroy_handler;
+       Ecore_Event_Handler *client_handler;
+
+       Eina_List *pause_list;
+       Eina_List *resume_list;
+
+       int paused;
+} s_info = {
+       .create_handler = NULL,
+       .destroy_handler = NULL,
+       .client_handler = NULL,
+
+       .pause_list = NULL,
+       .resume_list = NULL,
+
+       .paused = 1, /*!< The provider is treated as paused process when it is launched */
+};
+
+static inline void touch_paused_file(void)
+{
+       int fd;
+       fd = creat(WIDGET_CONF_PAUSED_FILE, 0644);
+       if (fd >= 0) {
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+       } else {
+               ErrPrint("Create .live.paused: %d\n", errno);
+       }
+}
+
+static inline void remove_paused_file(void)
+{
+       if (unlink(WIDGET_CONF_PAUSED_FILE) < 0) {
+               ErrPrint("Unlink .live.paused: %d\n", errno);
+       }
+}
+
+static inline int get_pid(Ecore_X_Window win)
+{
+       int pid;
+       Ecore_X_Atom atom;
+       unsigned char *in_pid;
+       int num;
+
+       atom = ecore_x_atom_get("X_CLIENT_PID");
+       if (ecore_x_window_prop_property_get(win, atom, ECORE_X_ATOM_CARDINAL,
+                               sizeof(int), &in_pid, &num) == EINA_FALSE) {
+               if (ecore_x_netwm_pid_get(win, &pid) == EINA_FALSE) {
+                       ErrPrint("Failed to get PID from a window 0x%X\n", win);
+                       return WIDGET_ERROR_INVALID_PARAMETER;
+               }
+       } else if (in_pid) {
+               pid = *(int *)in_pid;
+               DbgFree(in_pid);
+       } else {
+               ErrPrint("Failed to get PID\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return pid;
+}
+
+static Eina_Bool create_cb(void *data, int type, void *event)
+{
+       Ecore_X_Event_Window_Create *info = event;
+       ecore_x_window_client_sniff(info->win);
+       return ECORE_CALLBACK_PASS_ON;
+}
+
+static Eina_Bool destroy_cb(void *data, int type, void *event)
+{
+       // Ecore_X_Event_Window_Destroy *info = event;
+       return ECORE_CALLBACK_PASS_ON;
+}
+
+HAPI void xmonitor_handle_state_changes(void)
+{
+       int paused;
+       Eina_List *l;
+       struct event_item *item;
+
+       paused = client_is_all_paused() || setting_is_lcd_off();
+       if (s_info.paused == paused) {
+               return;
+       }
+
+       s_info.paused = paused;
+
+       if (s_info.paused) {
+               EINA_LIST_FOREACH(s_info.pause_list, l, item) {
+                       if (item->cb) {
+                               item->cb(item->user_data);
+                       }
+               }
+
+               touch_paused_file();
+
+               sqlite3_release_memory(WIDGET_CONF_SQLITE_FLUSH_MAX);
+               malloc_trim(0);
+       } else {
+               remove_paused_file();
+
+               EINA_LIST_FOREACH(s_info.resume_list, l, item) {
+                       if (item->cb) {
+                               item->cb(item->user_data);
+                       }
+               }
+       }
+}
+
+HAPI int xmonitor_update_state(int target_pid)
+{
+       Ecore_X_Window win;
+       struct client_node *client;
+       int pid;
+
+       if (!WIDGET_CONF_USE_XMONITOR || target_pid < 0) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       win = ecore_x_window_focus_get();
+
+       pid = get_pid(win);
+       if (pid <= 0) {
+               DbgPrint("Focused window has no PID %X\n", win);
+               client = client_find_by_pid(target_pid);
+               if (client) {
+                       DbgPrint("Client window has no focus now\n");
+                       client_paused(client);
+               }
+               return WIDGET_ERROR_NOT_EXIST;
+       }
+
+       client = client_find_by_pid(pid);
+       if (!client) {
+               DbgPrint("Client %d is not registered yet\n", pid);
+               client = client_find_by_pid(target_pid);
+               if (client) {
+                       DbgPrint("Client window has no focus now\n");
+                       client_paused(client);
+               }
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       if (target_pid != pid) {
+               DbgPrint("Client is paused\n");
+               client_paused(client);
+       } else {
+               DbgPrint("Client is resumed\n");
+               client_resumed(client);
+       }
+
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+static Eina_Bool client_cb(void *data, int type, void *event)
+{
+       Ecore_X_Event_Client_Message *info = event;
+       struct client_node *client;
+       char *name;
+       int pid;
+
+       pid = get_pid(info->win);
+       if (pid <= 0) {
+               return ECORE_CALLBACK_PASS_ON;
+       }
+
+       client = client_find_by_pid(pid);
+       if (!client) {
+               return ECORE_CALLBACK_PASS_ON;
+       }
+
+       name = ecore_x_atom_name_get(info->message_type);
+       if (!name) {
+               return ECORE_CALLBACK_PASS_ON;
+       }
+
+       if (!strcmp(name, "_X_ILLUME_DEACTIVATE_WINDOW")) {
+               xmonitor_pause(client);
+       } else if (!strcmp(name, "_X_ILLUME_ACTIVATE_WINDOW")) {
+               xmonitor_resume(client);
+       } else {
+               /* ignore event */
+       }
+
+       DbgFree(name);
+       return ECORE_CALLBACK_PASS_ON;
+}
+
+static inline void sniff_all_windows(void)
+{
+       Ecore_X_Window root;
+       Ecore_X_Window ret;
+       struct stack_item *new_item;
+       struct stack_item *item;
+       Eina_List *win_stack;
+       //int pid;
+       struct stack_item {
+               Ecore_X_Window *wins;
+               int nr_of_wins;
+               int i;
+       };
+
+       root = ecore_x_window_root_first_get();
+       ecore_x_window_sniff(root);
+
+       new_item = malloc(sizeof(*new_item));
+       if (!new_item) {
+               ErrPrint("Error(%d)\n", errno);
+               return;
+       }
+
+       new_item->nr_of_wins = 0;
+       new_item->wins =
+               ecore_x_window_children_get(root, &new_item->nr_of_wins);
+       new_item->i = 0;
+
+       win_stack = NULL;
+
+       if (new_item->wins) {
+               win_stack = eina_list_append(win_stack, new_item);
+       } else {
+               DbgFree(new_item);
+       }
+
+       while ((item = eina_list_nth(win_stack, 0))) {
+               win_stack = eina_list_remove(win_stack, item);
+
+               if (!item->wins) {
+                       DbgFree(item);
+                       continue;
+               }
+
+               while (item->i < item->nr_of_wins) {
+                       ret = item->wins[item->i];
+
+                       /*
+                        * Now we don't need to care about visibility of window,
+                        * just check whether it is registered or not.
+                        * (ecore_x_window_visible_get(ret))
+                        */
+                       ecore_x_window_client_sniff(ret);
+
+                       new_item = malloc(sizeof(*new_item));
+                       if (!new_item) {
+                               ErrPrint("Error %d\n", errno);
+                               item->i++;
+                               continue;
+                       }
+
+                       new_item->i = 0;
+                       new_item->nr_of_wins = 0;
+                       new_item->wins =
+                               ecore_x_window_children_get(ret,
+                                               &new_item->nr_of_wins);
+                       if (new_item->wins) {
+                               win_stack =
+                                       eina_list_append(win_stack, new_item);
+                       } else {
+                               DbgFree(new_item);
+                       }
+
+                       item->i++;
+               }
+
+               DbgFree(item->wins);
+               DbgFree(item);
+       }
+
+       return;
+}
+
+HAPI int xmonitor_pause(struct client_node *client)
+{
+       DbgPrint("%d is paused\n", client_pid(client));
+       client_paused(client);
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_resume(struct client_node *client)
+{
+       DbgPrint("%d is resumed\n", client_pid(client));
+       client_resumed(client);
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+static inline void disable_xmonitor(void)
+{
+       ecore_event_handler_del(s_info.create_handler);
+       ecore_event_handler_del(s_info.destroy_handler);
+       ecore_event_handler_del(s_info.client_handler);
+
+       s_info.create_handler = NULL;
+       s_info.destroy_handler = NULL;
+       s_info.client_handler = NULL;
+}
+
+static inline int enable_xmonitor(void)
+{
+       if (ecore_x_composite_query() == EINA_FALSE) {
+               DbgPrint("====> COMPOSITOR IS NOT ENABLED\n");
+       }
+
+       s_info.create_handler =
+               ecore_event_handler_add(ECORE_X_EVENT_WINDOW_CREATE,
+                               create_cb, NULL);
+       if (!s_info.create_handler) {
+               ErrPrint("Failed to add create event handler\n");
+               return WIDGET_ERROR_FAULT;
+       }
+
+       s_info.destroy_handler =
+               ecore_event_handler_add(ECORE_X_EVENT_WINDOW_DESTROY,
+                               destroy_cb, NULL);
+       if (!s_info.destroy_handler) {
+               ErrPrint("Failed to add destroy event handler\n");
+               ecore_event_handler_del(s_info.create_handler);
+               s_info.create_handler = NULL;
+               return WIDGET_ERROR_FAULT;
+       }
+
+       s_info.client_handler =
+               ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
+                               client_cb, NULL);
+       if (!s_info.client_handler) {
+               ErrPrint("Failed to add focus out event handler\n");
+               ecore_event_handler_del(s_info.create_handler);
+               ecore_event_handler_del(s_info.destroy_handler);
+               s_info.create_handler = NULL;
+               s_info.destroy_handler = NULL;
+               return WIDGET_ERROR_FAULT;
+       }
+
+       sniff_all_windows();
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_init(void)
+{
+       if (WIDGET_CONF_USE_XMONITOR) {
+               int ret;
+               ret = enable_xmonitor();
+               if (ret < 0) {
+                       return ret;
+               }
+       }
+
+       s_info.paused = client_is_all_paused() || setting_is_lcd_off();
+       if (s_info.paused) {
+               touch_paused_file();
+       } else {
+               remove_paused_file();
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void xmonitor_fini(void)
+{
+       if (WIDGET_CONF_USE_XMONITOR) {
+               disable_xmonitor();
+       }
+}
+
+HAPI int xmonitor_add_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data)
+{
+       struct event_item *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("Heap: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->cb = cb;
+       item->user_data = user_data;
+
+       switch (event) {
+       case XMONITOR_PAUSED:
+               s_info.pause_list = eina_list_prepend(s_info.pause_list, item);
+               break;
+       case XMONITOR_RESUMED:
+               s_info.resume_list = eina_list_prepend(s_info.resume_list, item);
+               break;
+       default:
+               ErrPrint("Invalid event type\n");
+               DbgFree(item);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_del_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data)
+{
+       struct event_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       switch (event) {
+       case XMONITOR_PAUSED:
+               EINA_LIST_FOREACH_SAFE(s_info.pause_list, l, n, item) {
+                       if (item->cb == cb && item->user_data == user_data) {
+                               s_info.pause_list = eina_list_remove(s_info.pause_list, item);
+                               DbgFree(item);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+
+       case XMONITOR_RESUMED:
+               EINA_LIST_FOREACH_SAFE(s_info.resume_list, l, n, item) {
+                       if (item->cb == cb && item->user_data == user_data) {
+                               s_info.resume_list = eina_list_remove(s_info.resume_list, item);
+                               DbgFree(item);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               ErrPrint("Invalid event type\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int xmonitor_is_paused(void)
+{
+       return s_info.paused;
+}
+
+/* End of a file */
diff --git a/src/xmonitor_wayland.c b/src/xmonitor_wayland.c
new file mode 100644 (file)
index 0000000..76bf86c
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <assert.h>
+#include <errno.h>
+#include <malloc.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <sqlite3.h>
+
+#include <gio/gio.h>
+#include <dlog.h>
+#include <widget_errno.h>
+
+#include "conf.h"
+#include "debug.h"
+#include "client_life.h"
+#include "slave_life.h"
+#include "main.h"
+#include "util.h"
+#include "setting.h"
+#include "xmonitor.h"
+
+int errno;
+
+struct event_item {
+       int (*cb)(void *user_data);
+       void *user_data;
+};
+
+static struct info {
+       Ecore_Event_Handler *create_handler;
+       Ecore_Event_Handler *destroy_handler;
+       Ecore_Event_Handler *client_handler;
+
+       Eina_List *pause_list;
+       Eina_List *resume_list;
+
+       int paused;
+} s_info = {
+       .create_handler = NULL,
+       .destroy_handler = NULL,
+       .client_handler = NULL,
+
+       .pause_list = NULL,
+       .resume_list = NULL,
+
+       .paused = 1, /*!< The provider is treated as paused process when it is launched */
+};
+
+static inline void touch_paused_file(void)
+{
+       int fd;
+       fd = creat(WIDGET_CONF_PAUSED_FILE, 0644);
+       if (fd >= 0) {
+               if (close(fd) < 0) {
+                       ErrPrint("close: %d\n", errno);
+               }
+       } else {
+               ErrPrint("Create .live.paused: %d\n", errno);
+       }
+}
+
+static inline void remove_paused_file(void)
+{
+       if (unlink(WIDGET_CONF_PAUSED_FILE) < 0) {
+               ErrPrint("Unlink .live.paused: %d\n", errno);
+       }
+}
+
+HAPI void xmonitor_handle_state_changes(void)
+{
+       int paused;
+       Eina_List *l;
+       struct event_item *item;
+
+       paused = client_is_all_paused() || setting_is_lcd_off();
+       if (s_info.paused == paused) {
+               return;
+       }
+
+       s_info.paused = paused;
+
+       if (s_info.paused) {
+               EINA_LIST_FOREACH(s_info.pause_list, l, item) {
+                       if (item->cb) {
+                               item->cb(item->user_data);
+                       }
+               }
+
+               touch_paused_file();
+
+               sqlite3_release_memory(SQLITE_FLUSH_MAX);
+               malloc_trim(0);
+       } else {
+               remove_paused_file();
+
+               EINA_LIST_FOREACH(s_info.resume_list, l, item) {
+                       if (item->cb) {
+                               item->cb(item->user_data);
+                       }
+               }
+       }
+}
+
+HAPI int xmonitor_update_state(int target_pid)
+{
+       struct client_node *client;
+
+       if (!WIDGET_CONF_USE_XMONITOR || target_pid < 0) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       /*!
+        * \TODO
+        * Find the top(focuesd) window's PID
+        * Compare it with target_pid.
+        * If it is what we finding, call the
+        * xmonitor_pause or xmonitor_resume
+        */
+
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_pause(struct client_node *client)
+{
+       DbgPrint("%d is paused\n", client_pid(client));
+       client_paused(client);
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_resume(struct client_node *client)
+{
+       DbgPrint("%d is resumed\n", client_pid(client));
+       client_resumed(client);
+       xmonitor_handle_state_changes();
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_init(void)
+{
+       if (WIDGET_CONF_USE_XMONITOR) {
+               return WIDGET_ERROR_NONE;
+       }
+
+       s_info.paused = client_is_all_paused() || setting_is_lcd_off();
+       if (s_info.paused) {
+               touch_paused_file();
+       } else {
+               remove_paused_file();
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI void xmonitor_fini(void)
+{
+       if (WIDGET_CONF_USE_XMONITOR) {
+       }
+}
+
+HAPI int xmonitor_add_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data)
+{
+       struct event_item *item;
+
+       item = malloc(sizeof(*item));
+       if (!item) {
+               ErrPrint("malloc: %d\n", errno);
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       item->cb = cb;
+       item->user_data = user_data;
+
+       switch (event) {
+       case XMONITOR_PAUSED:
+               s_info.pause_list = eina_list_prepend(s_info.pause_list, item);
+               break;
+       case XMONITOR_RESUMED:
+               s_info.resume_list = eina_list_prepend(s_info.resume_list, item);
+               break;
+       default:
+               ErrPrint("Invalid event type\n");
+               DbgFree(item);
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NONE;
+}
+
+HAPI int xmonitor_del_event_callback(enum xmonitor_event event, int (*cb)(void *user_data), void *user_data)
+{
+       struct event_item *item;
+       Eina_List *l;
+       Eina_List *n;
+
+       switch (event) {
+       case XMONITOR_PAUSED:
+               EINA_LIST_FOREACH_SAFE(s_info.pause_list, l, n, item) {
+                       if (item->cb == cb && item->user_data == user_data) {
+                               s_info.pause_list = eina_list_remove(s_info.pause_list, item);
+                               DbgFree(item);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+
+       case XMONITOR_RESUMED:
+               EINA_LIST_FOREACH_SAFE(s_info.resume_list, l, n, item) {
+                       if (item->cb == cb && item->user_data == user_data) {
+                               s_info.resume_list = eina_list_remove(s_info.resume_list, item);
+                               DbgFree(item);
+                               return WIDGET_ERROR_NONE;
+                       }
+               }
+               break;
+       default:
+               ErrPrint("Invalid event type\n");
+               return WIDGET_ERROR_INVALID_PARAMETER;
+       }
+
+       return WIDGET_ERROR_NOT_EXIST;
+}
+
+HAPI int xmonitor_is_paused(void)
+{
+       return s_info.paused;
+}
+
+/* End of a file */
+
diff --git a/widget-mgr/CMakeLists.txt b/widget-mgr/CMakeLists.txt
new file mode 100644 (file)
index 0000000..58b7601
--- /dev/null
@@ -0,0 +1,39 @@
+PROJECT(widget-mgr C)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/widget-mgr/include)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(info_pkgs REQUIRED
+       dlog
+       db-util
+       com-core
+       ecore
+       glib-2.0
+       gio-2.0
+       widget_service
+       xdamage
+       xfixes
+       x11
+       xext
+       bundle
+)
+
+FOREACH(flag ${info_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Wall -Werror -fPIE")
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+ADD_DEFINITIONS("-DSOCKET_FILE=\"/opt/usr/share/live_magazine/.live.socket\"")
+
+ADD_EXECUTABLE(${PROJECT_NAME}
+       src/widget-mgr.c
+       src/node.c
+)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${info_pkgs_LDFLAGS} "-pie")
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /opt/usr/devel/usr/bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+
+# End of a file
diff --git a/widget-mgr/include/node.h b/widget-mgr/include/node.h
new file mode 100644 (file)
index 0000000..e3fe593
--- /dev/null
@@ -0,0 +1,41 @@
+
+enum node_type {
+       NODE_DIR,
+       NODE_FILE,
+       NODE_LINK,
+};
+
+struct node;
+
+#define NODE_READ   0x01
+#define NODE_WRITE  0x02
+#define NODE_EXEC   0x04
+
+extern struct node *node_find(const struct node *node, const char *path);
+extern struct node *node_create(struct node *parent, const char *name, enum node_type type, int mode);
+extern void *node_destroy(struct node *node);
+
+extern struct node * const node_next_sibling(const struct node *node);
+extern struct node * const node_prev_sibling(const struct node *node);
+
+extern struct node * const node_child(const struct node *node);
+extern struct node * const node_parent(const struct node *node);
+
+extern void node_set_mode(struct node *node, int mode);
+extern void node_set_data(struct node *node, void *data);
+
+extern const int const node_mode(const struct node *node);
+extern void * const node_data(const struct node *node);
+
+extern void node_set_type(struct node *node, enum node_type type);
+extern const enum node_type const node_type(const struct node *node);
+
+extern const char * const node_name(const struct node *node);
+
+extern char *node_to_abspath(const struct node *node);
+
+extern int node_age(struct node *node);
+extern void node_set_age(struct node *node, int age);
+
+extern void node_delete(struct node *node, void (del_cb)(struct node *node));
+/* End of a file */
diff --git a/widget-mgr/include/widget-info.h b/widget-mgr/include/widget-info.h
new file mode 100644 (file)
index 0000000..67a71d8
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
diff --git a/widget-mgr/src/node.c b/widget-mgr/src/node.c
new file mode 100644 (file)
index 0000000..45c1dc7
--- /dev/null
@@ -0,0 +1,391 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "node.h"
+
+struct node {
+       char *name;
+       enum node_type type;
+
+       void *data;
+
+       struct node *parent;
+       unsigned char mode;
+       int age;
+
+       struct {
+               struct node *next;
+               struct node *prev;
+       } sibling;
+
+       struct node *child;
+};
+
+int errno; /* External symbol */
+
+char *node_to_abspath(const struct node *node)
+{
+       char *path;
+       char *ptr;
+       const struct node *tmp;
+       int len = 0;
+
+       tmp = node;
+       while (tmp && node_name(tmp)) {
+               len += strlen(node_name(tmp)) + 1; /* trail '/' */
+               tmp = node_parent(tmp);
+       }
+
+       path = malloc(len + 3); /* '/' and '\0' */
+       if (!path) {
+               return NULL;
+       }
+
+       if (!len) {
+               path[0] = '/';
+               path[1] = '\0';
+       } else {
+               ptr = path + len + 1;
+               *ptr = '\0';
+               ptr--;
+               *ptr = '/';
+               tmp = node;
+               while (tmp && node_name(tmp)) {
+                       ptr -= (strlen(node_name(tmp)) + 1);
+                       *ptr = '/';
+                       strncpy(ptr + 1, node_name(tmp), strlen(node_name(tmp)));
+                       tmp = node_parent(tmp);
+               }
+       }
+
+       return path;
+}
+
+static inline int next_state(int from, char ch)
+{
+       switch (ch)
+       {
+       case '\0':
+       case '/':
+               return 1;
+       case '.':
+               if (from == 1) {
+                       return 2;
+               }
+               if (from == 2) {
+                       return 3;
+               }
+       }
+
+       return 4;
+}
+
+static inline void abspath(const char* pBuffer, char* pRet)
+{
+       int idx=0;
+       int state = 1;
+       int from;
+       int src_idx = 0;
+       int src_len = strlen(pBuffer);
+       pRet[idx] = '/';
+       idx ++;
+
+       while (src_idx <= src_len) {
+               from = state;
+               state = next_state(from, pBuffer[src_idx]);
+
+               switch (from) {
+               case 1:
+                       if (state != 1) {
+                               pRet[idx] = pBuffer[src_idx];
+                               idx++;
+                       }
+                       break;
+               case 2:
+                       if (state == 1) {
+                               if (idx > 1) {
+                                       idx--;
+                               }
+                       } else {
+                               pRet[idx] = pBuffer[src_idx];
+                               idx++;
+                       }
+                       break;
+               case 3:
+                       // Only can go to the 1 or 4
+                       if (state == 1) {
+                               idx -= 2;
+                               if (idx < 1) {
+                                       idx = 1;
+                               }
+
+                               while (idx > 1 && pRet[idx] != '/') {
+                                       idx--; /* Remove .. */
+                               }
+                               if (idx > 1 && pRet[idx] == '/') {
+                                       idx--;
+                               }
+                               while (idx > 1 && pRet[idx] != '/') {
+                                       idx--; /* Remove parent folder */
+                               }
+                       }
+               case 4:
+                       pRet[idx] = pBuffer[src_idx];
+                       idx++;
+                       break;
+               }
+
+               pRet[idx] = '\0';
+               src_idx++;
+       }
+}
+
+struct node *node_find(const struct node *node, const char *path)
+{
+       int len = 0;
+       char *ptr;
+       char *buffer;
+
+       if (*path != '/') {
+               while (node->parent && path[0] == '.' && path[1] == '.') {
+                       if (path[2] != '/' && path[2] != '\0') {
+                               break;
+                       }
+
+                       path += 2;
+                       path += (path[2] == '/');
+                       node = node->parent;
+               }
+       }
+
+       buffer = malloc(strlen(path) + 3); /* add 2 more bytes */
+       if (!buffer) {
+               printf("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       abspath(path, buffer);
+
+       ptr = buffer;
+       do {
+               ptr += (*ptr == '/');
+               for (len = 0; ptr[len] && ptr[len] != '/'; len++);
+               if (!len) {
+                       break;
+               }
+
+               if (!strncmp("..", ptr, len)) {
+                       ptr += len;
+                       node = node->parent ? node->parent : node;
+                       continue;
+               }
+
+               if (!strncmp(".", ptr, len)) {
+                       ptr += len;
+                       continue;
+               }
+
+               node = node->child;
+               if (!node) {
+                       break;
+               }
+
+               while (node) {
+                       if (!strncmp(node->name, ptr, len) && node->name[len] == '\0') {
+                               ptr += len;
+                               break;
+                       }
+
+                       node = node->sibling.next;
+               }
+       } while (*ptr && node);
+
+       free(buffer);
+       return (struct node *)node;
+}
+
+struct node *node_create(struct node *parent, const char *name, enum node_type type, int mode)
+{
+       struct node *node;
+
+       node = malloc(sizeof(*node));
+       if (!node) {
+               printf("malloc: %d\n", errno);
+               return NULL;
+       }
+
+       node->parent = parent;
+
+       if (name) {
+               node->name = strdup(name);
+               if (!node->name) {
+                       printf("strdup: %d\n", errno);
+                       free(node);
+                       return NULL;
+               }
+       } else {
+               node->name = NULL;
+       }
+
+       node->type = type;
+
+       node->sibling.next = NULL;
+       node->sibling.prev = NULL;
+
+       node->child = NULL;
+       node->data = NULL;
+
+       if (parent) {
+               if (parent->child) {
+                       struct node *tmp;
+                       tmp = parent->child;
+                       while (tmp->sibling.next) {
+                               tmp = tmp->sibling.next;
+                       }
+
+                       tmp->sibling.next = node;
+                       node->sibling.prev = tmp;
+               } else {
+                       parent->child = node;
+               }
+       }
+
+       node->mode = mode;
+       return node;
+}
+
+void *node_destroy(struct node *node)
+{
+       void *data;
+
+       data = node->data;
+       free(node->name);
+       free(node);
+
+       return data;
+}
+
+void node_delete(struct node *node, void (del_cb)(struct node *node))
+{
+       struct node *tmp;
+       struct node *next;
+       struct node *parent;
+
+       if (node->sibling.prev) {
+               node->sibling.prev->sibling.next = node->sibling.next;
+       }
+
+       if (node->sibling.next) {
+               node->sibling.next->sibling.prev = node->sibling.prev;
+       }
+
+       /* Isolate the node */
+       node->sibling.prev = NULL;
+       node->sibling.next = NULL;
+
+       if (node->parent) {
+               if (node->parent->child == node) {
+                       node->parent->child = NULL;
+               }
+
+               node->parent = NULL;
+       }
+
+       tmp = node;
+       while (tmp) {
+               /* Reach to the leaf node */
+               while (tmp->child) {
+                       tmp = tmp->child;
+               }
+
+               parent = tmp->parent;
+               next = tmp->sibling.next;
+
+               if (parent && parent->child == tmp) {
+                       parent->child = NULL;
+               }
+
+               if (del_cb) {
+                       del_cb(tmp);
+               }
+
+               node_destroy(tmp);
+
+               if (next) {
+                       tmp = next;
+               } else if (parent) {
+                       tmp = parent;
+               } else {
+                       tmp = NULL;
+               }
+       }
+}
+
+struct node * const node_next_sibling(const struct node *node)
+{
+       return node->sibling.next;
+}
+
+struct node * const node_prev_sibling(const struct node *node)
+{
+       return node->sibling.prev;
+}
+
+void node_set_mode(struct node *node, int mode)
+{
+       node->mode = mode;
+}
+
+void node_set_data(struct node *node, void *data)
+{
+       node->data = data;
+}
+
+void node_set_type(struct node *node, enum node_type type)
+{
+       node->type = type;
+}
+
+struct node * const node_child(const struct node *node)
+{
+       return node->child;
+}
+
+struct node * const node_parent(const struct node *node)
+{
+       return node->parent;
+}
+
+const int const node_mode(const struct node *node)
+{
+       return node->mode;
+}
+
+void * const node_data(const struct node *node)
+{
+       return node->data;
+}
+
+const enum node_type const node_type(const struct node *node)
+{
+       return node->type;
+}
+
+const char * const node_name(const struct node *node)
+{
+       return node->name;
+}
+
+void node_set_age(struct node *node, int age)
+{
+       node->age = age;
+}
+
+int node_age(struct node *node)
+{
+       return node->age;
+}
+
+/* End of a file */
diff --git a/widget-mgr/src/widget-mgr.c b/widget-mgr/src/widget-mgr.c
new file mode 100644 (file)
index 0000000..80d6bd9
--- /dev/null
@@ -0,0 +1,2191 @@
+/*
+ * Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <errno.h>
+#include <libgen.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <sys/shm.h>
+#include <sys/ipc.h>
+#include <ctype.h>
+#include <bundle.h>
+
+#include <X11/Xlib.h>
+#include <X11/extensions/Xdamage.h>
+#include <X11/extensions/Xfixes.h>
+#include <X11/extensions/XShm.h>
+#include <X11/Xutil.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <packet.h>
+#include <com-core_packet.h>
+#include <com-core.h>
+
+#include <widget_errno.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+
+#include <Ecore.h>
+
+#include "widget-info.h"
+#include "node.h"
+
+#define PROMPT "widget-mgr "
+#define PROVIDER_FOLDER "provider"
+#define PACKAGE_FOLDER "package"
+
+struct package {
+       int primary;
+       char *pkgid;
+
+       int pid;
+       char *slavename;
+       char *abi;
+       int refcnt;
+       int fault_count;
+       int inst_count;
+};
+
+struct instance {
+       char *id;
+       char *buf_id;
+       char *cluster;
+       char *category;
+       double period;
+       char *state;
+       int width;
+       int height;
+};
+
+struct slave {
+       int pid;
+       char *pkgname;
+       char *abi;
+       int secured;
+       int refcnt;
+       int fault_count;
+       char *state;
+       int loaded_inst;
+       int loaded_pkg;
+       double ttl;
+};
+
+enum command {
+       NOP,
+       PKG_LIST,
+       INST_LIST,
+       SLAVE_LIST,     
+       INST_CTRL,
+       SLAVE_CTRL,
+       MASTER_CTRL,
+};
+
+static struct info {
+       int fifo_handle;
+       int fd;
+       Ecore_Fd_Handler *fd_handler;
+       Ecore_Fd_Handler *in_handler;
+
+       struct node *rootdir;
+       struct node *curdir;
+       struct node *targetdir;
+
+       enum command cmd;
+
+       int input_fd;
+       int verbose;
+
+       int age;
+
+       char *history[1024];
+       int history_top;
+       int history_idx;
+
+       struct node *quick_search_node;
+       int quick_idx;
+
+       int dump;
+} s_info = {
+       .fifo_handle = -EINVAL,
+       .fd = -EINVAL,
+       .fd_handler = NULL,
+       .in_handler = NULL,
+       .rootdir = NULL,
+       .curdir = NULL,
+       .targetdir = NULL,
+       .cmd = NOP,
+       .input_fd = STDIN_FILENO,
+       .verbose = 0,
+       .age = 0,
+       .history = { 0, },
+       .history_top = 0,
+       .history_idx = 0,
+       .quick_search_node = NULL,
+       .quick_idx = 0,
+       .dump = 0,
+};
+
+char *optarg;
+int errno;
+int optind;
+int optopt;
+int opterr;
+
+static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler);
+
+static inline const char *trim_cmd(const char *cmd)
+{
+       while (*cmd && *cmd == ' ') cmd++;
+
+       return cmd;
+}
+
+static Eina_Bool process_line_cb(void *data)
+{
+       input_cb(NULL, NULL);
+       return ECORE_CALLBACK_CANCEL;
+}
+
+static inline void prompt(const char *cmdline)
+{
+       char *path;
+
+       if (s_info.input_fd != STDIN_FILENO) {
+               /* To prevent recursive call, add function to the main loop (idler) */
+               ecore_idler_add(process_line_cb, NULL);
+               return;
+       }
+
+       path = node_to_abspath(s_info.curdir);
+       printf(PROMPT"%s # %s", path, cmdline ? cmdline : "");
+       free(path);
+}
+
+static void provider_del_cb(struct node *node)
+{
+       struct slave *info;
+
+       info = node_data(node);
+       if (!info) {
+               return;
+       }
+
+       free(info->pkgname);
+       free(info->abi);
+       free(info->state);
+       free(info);
+}
+
+static void package_del_cb(struct node *node)
+{
+       struct package *info;
+
+       info = node_data(node);
+       if (!info) {
+               return;
+       }
+
+       free(info->pkgid);
+       free(info->slavename);
+       free(info->abi);
+       free(info);
+}
+
+static void inst_del_cb(struct node *node)
+{
+       struct instance *info;
+
+       info = node_data(node);
+       if (!info) {
+               return;
+       }
+
+       free(info->id);
+       free(info->buf_id);
+       free(info->cluster);
+       free(info->category);
+       free(info->state);
+       free(info);
+}
+
+static void ls(void)
+{
+       struct node *node;
+       int cnt = 0;
+       int is_package;
+       int is_provider;
+       int is_instance;
+       struct node *next_node;
+
+       if (!(node_mode(s_info.targetdir) & NODE_READ)) {
+               printf("Access denied\n");
+               return;
+       }
+
+       is_package = node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), PACKAGE_FOLDER);
+       is_provider = !is_package && node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), PROVIDER_FOLDER);
+       is_instance = !is_package && !is_provider && node_parent(s_info.targetdir) && node_name(node_parent(s_info.targetdir)) && !strcmp(node_name(node_parent(s_info.targetdir)), PACKAGE_FOLDER);
+
+       node = node_child(s_info.targetdir);
+       while (node) {
+               if (is_package) {
+                       struct package *info;
+
+                       next_node = node_next_sibling(node);
+                       if (node_age(node) != s_info.age) {
+                               node_delete(node, package_del_cb);
+                               node = next_node;
+                               continue;
+                       }
+
+                       info = node_data(node);
+                       printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "(none)", info->abi ? info->abi : "?");
+               } else if (is_provider) {
+                       struct slave *info;
+
+                       next_node = node_next_sibling(node);
+                       if (node_age(node) != s_info.age) {
+                               node_delete(node, provider_del_cb);
+                               node = next_node;
+                               continue;
+                       }
+
+                       info = node_data(node);
+                       printf("%6d %3d %5s %5.2f ", info->pid, info->loaded_inst, info->abi ? info->abi : "?", info->ttl);
+               } else if (is_instance) {
+                       struct instance *info;
+                       struct stat stat;
+                       char buf[4096];
+
+                       next_node = node_next_sibling(node);
+
+                       if (node_age(node) != s_info.age) {
+                               node_delete(node, inst_del_cb);
+                               node = next_node;
+                               continue;
+                       }
+
+                       info = node_data(node);
+
+                       printf(" %5.2f %s %6s %10s %10s %4dx%-4d ", info->period, info->buf_id, info->state, info->cluster, info->category, info->width, info->height);
+                       snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
+                       if (lstat(buf, &stat) < 0) {
+                               printf("%3d ERR ", errno);
+                       } else {
+                               printf("%2.2lf KB ", (double)stat.st_size / 1024.0f);
+                       }
+               }
+
+               if (node_type(node) == NODE_DIR) {
+                       printf("%s/", node_name(node));
+               } else if (node_type(node) == NODE_FILE) {
+                       printf("%s", node_name(node));
+               }
+
+               printf("\n");
+               node = node_next_sibling(node);
+               cnt++;
+       }
+
+       printf("Total: %d\n", cnt);
+}
+
+static void send_slave_list(void)
+{
+       struct packet *packet;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       packet = packet_create_noack("slave_list", "d", 0.0f);
+       if (!packet) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to send a packet: %d\n", ret);
+               return;
+       }
+
+       s_info.cmd = SLAVE_LIST;
+       s_info.age++;
+}
+
+/*!
+ * var = debug, slave_max_load
+ * cmd = set / get
+ */
+static void send_command(const char *cmd, const char *var, const char *val)
+{
+       struct packet *packet;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       packet = packet_create_noack("master_ctrl", "sss", cmd, var, val);
+       if (!packet) {
+               printf("Failed to create a ctrl packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to send packet ctrl\n");
+               return;
+       }
+
+       s_info.cmd = MASTER_CTRL;
+       s_info.age++;
+}
+
+static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
+{
+       struct node *parent = data;
+       struct node *node;
+       struct package *info;
+
+       node = node_find(parent, lbid);
+       if (node) {
+               info = node_data(node);
+               if (!info) {
+                       printf("Invalid node\n");
+                       return -EINVAL;
+               }
+
+               free(info->pkgid);
+               info->pkgid = strdup(appid);
+               if (!info->pkgid) {
+                       printf("strdup: %d\n", errno);
+                       return -ENOMEM;
+               }
+
+               node_set_age(node, s_info.age);
+               return 0;
+       }
+
+       info = calloc(1, sizeof(*info));
+       if (!info) {
+               printf("calloc: %d\n", errno);
+               return -ENOMEM;
+       }
+
+       info->pkgid = strdup(appid);
+       if (!info->pkgid) {
+               printf("strdup: %d\n", errno);
+               free(info);
+               return -ENOMEM;
+       }
+
+       info->primary = is_prime;
+
+       node = node_create(parent, lbid, NODE_DIR, NODE_READ | NODE_EXEC);
+       if (!node) {
+               free(info->pkgid);
+               free(info);
+               return -ENOMEM;
+       }
+
+       node_set_data(node, info);
+       node_set_age(node, s_info.age);
+       return 0;
+}
+
+static void send_pkg_list(void)
+{
+       struct packet *packet;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       packet = packet_create_noack("pkg_list", "d", 0.0f);
+       if (!packet) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       s_info.cmd = PKG_LIST;
+       s_info.age++;
+
+       widget_service_get_widget_list(pkglist_cb, s_info.targetdir);
+}
+
+static void send_inst_delete(void)
+{
+       struct packet *packet;
+       struct node *parent;
+       const char *name;
+       struct instance *inst;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       parent = node_parent(s_info.targetdir);
+       if (!parent) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       if (!node_parent(parent)) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       name = node_name(node_parent(parent));
+       if (!name || strcmp(name, PACKAGE_FOLDER)) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       inst = node_data(s_info.targetdir);
+       name = node_name(parent);
+
+       packet = packet_create_noack("pkg_ctrl", "sss", "rminst", name, inst->id);
+       if (!packet) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to send a packet: %d\n", ret);
+               return;
+       }
+
+       s_info.cmd = INST_CTRL;
+       s_info.age++;
+}
+
+static void send_inst_fault(void)
+{
+       struct packet *packet;
+       struct node *parent;
+       const char *name;
+       struct instance *inst;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       parent = node_parent(s_info.targetdir);
+       if (!parent) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       if (!node_parent(parent)) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       name = node_name(node_parent(parent));
+       if (!name || strcmp(name, PACKAGE_FOLDER)) {
+               printf("Invalid argument\n");
+               return;
+       }
+
+       inst = node_data(s_info.targetdir);
+       name = node_name(parent);
+
+       packet = packet_create_noack("pkg_ctrl", "sss", "faultinst", name, inst->id);
+       if (!packet) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to send a packet: %d\n", ret);
+               return;
+       }
+
+       s_info.cmd = INST_CTRL;
+       s_info.age++;
+}
+
+static void send_inst_list(const char *pkgname)
+{
+       struct packet *packet;
+       int ret;
+
+       if (s_info.cmd != NOP) {
+               printf("Previous command is not finished\n");
+               return;
+       }
+
+       packet = packet_create_noack("inst_list", "s", pkgname);
+       if (!packet) {
+               printf("Failed to create a packet\n");
+               return;
+       }
+
+       ret = com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
+       if (ret < 0) {
+               printf("Failed to send a packet: %d\n", ret);
+               return;
+       }
+
+       s_info.cmd = INST_LIST;
+       s_info.age++;
+}
+
+static void help(void)
+{
+       printf("widget-mgr - widget utility\n");
+       printf("------------------------------ [Option] ------------------------------\n");
+       printf("-b Batch mode\n");
+       printf("-x execute command\n");
+       printf("------------------------------ [Command list] ------------------------------\n");
+       printf("\e[32mcd [PATH] - Change directory\e[0m\n");
+       printf("\e[32mls [ | PATH] - List up content as a file\e[0m\n");
+       printf("\e[32mrm [PKG_ID|INST_ID] - Delete package or instance\e[0m\n");
+       printf("\e[32mstat [path] - Display the information of given path\e[0m\n");
+       printf("\e[32mset [debug] [on|off] Set the control variable of master provider\e[0m\n");
+       printf("\e[32mx damage Pix x y w h - Create damage event for given pixmap\e[0m\n");
+       printf("\e[32mx move Pix x y - Move the window\e[0m\n");
+       printf("\e[32mx resize Pix w h - Resize the window\e[0m\n");
+       printf("\e[32mx map Pix - Show the window\e[0m\n");
+       printf("\e[32mx unmap Pix - Hide the window\e[0m\n");
+       printf("\e[32mx capture Pix outfile - Capture pixmap and save it to outfile\e[0m\n");
+       printf("\e[32msh [command] Execute shell command, [command] should be abspath\e[0m\n");
+       printf("\e[32mexit - \e[0m\n");
+       printf("\e[32mquit - \e[0m\n");
+       printf("----------------------------------------------------------------------------\n");
+}
+
+static inline void init_directory(void)
+{
+       struct node *node;
+       s_info.rootdir = node_create(NULL, NULL, NODE_DIR, NODE_READ | NODE_EXEC);
+       if (!s_info.rootdir) {
+               return;
+       }
+
+       node = node_create(s_info.rootdir, PROVIDER_FOLDER, NODE_DIR, NODE_READ | NODE_EXEC);
+       if (!node) {
+               node_destroy(s_info.rootdir);
+               s_info.rootdir = NULL;
+               return;
+       }
+
+       node = node_create(s_info.rootdir, PACKAGE_FOLDER, NODE_DIR, NODE_READ | NODE_EXEC);
+       if (!node) {
+               node_destroy(node_child(s_info.rootdir));
+               node_destroy(s_info.rootdir);
+               s_info.rootdir = NULL;
+               return;
+       }
+
+       s_info.curdir = s_info.rootdir;
+       return;
+}
+
+static inline void fini_directory(void)
+{
+}
+
+static struct node *update_target_dir(const char *cmd)
+{
+       struct node *node;
+
+       node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
+       node = node_find(node, cmd);
+
+       return node;
+}
+
+static int get_token(const char *src, char *out)
+{
+       int len = 0;
+       while (*src && *src == ' ') src++;
+
+       if (!*src) {
+               return 0;
+       }
+
+       while (*src && *src != ' ') {
+               *out++ = *src++;
+               len++;
+       }
+
+       *out = '\0';
+       return len;
+}
+
+static inline int do_stat(const char *cmd)
+{
+       int i;
+       struct node *node;
+       struct node *parent;
+       char *tmp;
+       enum stat_type {
+               PKG_INSTANCE = 0x01,
+               PKG,
+               PROVIDER_INSTANCE,
+               PROVIDER,
+               ROOT,
+       } type;
+
+       cmd = trim_cmd(cmd + 5);
+       if (!*cmd){
+               printf("Invalid argument\n");
+               return -EINVAL;
+       }
+
+       node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
+       if (!node) {
+               printf("Invalid path\n");
+               return -EINVAL;
+       }
+
+       i = 0;
+       type = ROOT;
+       parent = node_parent(node);
+       while (parent) {
+               if (!node_name(parent)) {
+                       printf("%s has no info\n", node_name(node));
+                       return -EINVAL;
+               } else if (!strcmp(node_name(parent), PACKAGE_FOLDER)) {
+                       type = (i == 0) ? PKG : PKG_INSTANCE;
+                       break;
+               } else if (!strcmp(node_name(parent), PROVIDER_FOLDER)){
+                       type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
+                       break;
+               }
+
+               parent = node_parent(parent);
+               i++;
+               if (i > 1) {
+                       printf("%s is invalid path\n", node_name(node));
+                       return -EINVAL;
+               }
+       }
+
+       switch (type){
+       case PKG:
+               tmp = widget_service_get_name(node_name(node), NULL);
+               printf("Name: %s (", tmp);
+               free(tmp);
+
+               i = widget_service_is_enabled(node_name(node));
+               printf("%s)\n", i ? "enabled" : "disabled");
+
+               tmp = widget_service_get_icon(node_name(node), NULL);
+               printf("Icon: %s\n", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_category(node_name(node));
+               printf("Category: %s\n", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_provider_name(node_name(node));
+               printf("Provider: %s (content:", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_content_string(node_name(node));
+               printf("%s)\n", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_widget_script_path(node_name(node));
+               printf("LB Script: %s (", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_widget_script_group(node_name(node));
+               printf("%s)\n", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_gbar_script_path(node_name(node));
+               printf("PD Script: %s (", tmp);
+               free(tmp);
+
+               tmp = widget_service_get_gbar_script_group(node_name(node));
+               printf("%s)\n", tmp);
+               free(tmp);
+
+               break;
+       case PROVIDER:
+               printf("Not supported yet\n");
+               break;
+       case PKG_INSTANCE:
+               printf("Not supported yet\n");
+               break;
+       case PROVIDER_INSTANCE:
+               printf("Not supported yet\n");
+               break;
+       case ROOT:
+               printf("Not supported yet\n");
+               break;
+       }
+
+       return 0;
+}
+
+static int do_set(const char *cmd)
+{
+       int i;
+       char variable[4096] = { '0', };
+
+       cmd += 4;
+       i = get_token(cmd, variable);
+
+       cmd = trim_cmd(cmd + i);
+       if (!i || !*cmd) {
+               printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
+               return -EINVAL;
+       }
+
+       send_command("set", variable, cmd);
+       return 0;
+}
+
+static inline int do_get(const char *cmd)
+{
+       cmd = trim_cmd(cmd + 4);
+       if (!*cmd) {
+               printf("Invalid argument(%s): get [VAR]\n", cmd);
+               return -EINVAL;
+       }
+
+       send_command("get", cmd, "");
+       return 0;
+}
+
+static inline int do_ls(const char *cmd)
+{
+       const char *name;
+       struct node *parent;
+
+       cmd = trim_cmd(cmd + 2);
+
+       s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
+       if (!s_info.targetdir) {
+               printf("%s is not exists\n", cmd);
+               return -ENOENT;
+       }
+
+       name = node_name(s_info.targetdir);
+       if (name) {
+               if (!strcmp(name, PACKAGE_FOLDER)) {
+                       if (s_info.cmd == NOP) {
+                               send_pkg_list();
+                               return 0;
+                       }
+
+                       printf("Waiting the server response\n");
+                       return -EBUSY;
+               } else if (!strcmp(name, PROVIDER_FOLDER)) {
+                       if (s_info.cmd == NOP) {
+                               send_slave_list();
+                               return 0;
+                       }
+
+                       printf("Waiting the server response\n");
+                       return -EBUSY;
+               }
+       }
+
+       parent = node_parent(s_info.targetdir);
+       if (parent && node_name(parent)) {
+               if (!strcmp(node_name(parent), PACKAGE_FOLDER)) {
+                       if (s_info.cmd != NOP) {
+                               printf("Waiting the server response\n");
+                               return -EBUSY;
+                       }
+
+                       send_inst_list(name);
+                       return 0;
+               }
+       }
+
+       ls();
+       return -1;
+}
+
+static inline int do_cd(const char *cmd)
+{
+       cmd = trim_cmd(cmd + 2);
+       if (!*cmd) {
+               return -1;
+       }
+
+       if (s_info.cmd != NOP) {
+               printf("Waiting the server response\n");
+               return -EBUSY;
+       }
+
+       s_info.targetdir = update_target_dir(cmd);
+       if (!s_info.targetdir) {
+               printf("%s is not exists\n", cmd);
+               return -ENOENT;
+       }
+
+       if (node_type(s_info.targetdir) != NODE_DIR) {
+               printf("Unable change directory to %s\n", cmd);
+               return -EINVAL;
+       }
+
+       if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
+               printf("Access denied %s\n", cmd);
+               return -EACCES;
+       }
+
+       s_info.curdir = s_info.targetdir;
+       return -1;
+}
+
+static inline int do_rm(const char *cmd)
+{
+       cmd = trim_cmd(cmd + 2);
+       if (!*cmd) {
+               return -1;
+       }
+
+       if (s_info.cmd != NOP) {
+               printf("Waiting the server response\n");
+               return -EBUSY;
+       }
+
+       s_info.targetdir = update_target_dir(cmd);
+       if (!s_info.targetdir) {
+               printf("%s is not exists\n", cmd);
+               return -ENOENT;
+       }
+
+       if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
+               printf("Access denied %s\n", cmd);
+               return -EACCES;
+       }
+
+       send_inst_delete();
+       return 0;
+}
+
+static inline int do_fault(const char *cmd)
+{
+       cmd = trim_cmd(cmd + 5);
+       if (!*cmd) {
+               return -1;
+       }
+
+       if (s_info.cmd != NOP) {
+               printf("Waiting the server response\n");
+               return -EBUSY;
+       }
+
+       s_info.targetdir = update_target_dir(cmd);
+       if (!s_info.targetdir) {
+               printf("%s is not exists\n", cmd);
+               return -ENOENT;
+       }
+
+       if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
+               printf("Access denied %s\n", cmd);
+               return -EACCES;
+       }
+
+       send_inst_fault();
+       return 0;
+}
+
+#if !defined(WCOREDUMP)
+#define WCOREDUMP(a)   0
+#endif
+
+static void do_sh(const char *cmd)
+{
+       pid_t pid;
+
+       cmd = trim_cmd(cmd + 3);
+       if (!*cmd) {
+               return;
+       }
+
+       pid = fork();
+       if (pid == 0) {
+               char command[256];
+               int idx;
+               idx = 0;
+
+               while (idx < (sizeof(command) - 1) && *cmd && *cmd != ' ') {
+                       command[idx++] = *cmd++;
+               }
+               command[idx] = '\0';
+
+               if (execl(command, cmd, NULL) < 0) {
+                       printf("execl: %d\n", errno);
+               }
+
+               exit(0);
+       } else if (pid < 0) {
+               printf("Failed to create a new process: %d\n", errno);
+       } else {
+               int status;
+               if (waitpid(pid, &status, 0) < 0) {
+                       printf("waitpid: %d\n", errno);
+               } else {
+                       if (WIFEXITED(status)) {
+                               printf("Exit: %d\n", WEXITSTATUS(status));
+                       } else if (WIFSIGNALED(status)) {
+                               printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
+                       } else if (WIFSTOPPED(status)) {
+                               printf("Stopped by %d\n", WSTOPSIG(status));
+                       } else if (WIFCONTINUED(status)) {
+                               printf("Child is resumed\n");
+                       }
+               }
+       }
+}
+
+static inline int get_pixmap_size(Display *disp, Pixmap id, int *x, int *y, unsigned int *w, unsigned int *h)
+{
+       Window dummy_win;
+       unsigned int dummy_border, dummy_depth;
+       int _x;
+       int _y;
+
+       if (!x) {
+               x = &_x;
+       }
+
+       if (!y) {
+               y = &_y;
+       }
+
+       if (!XGetGeometry(disp, id, &dummy_win, x, y, w, h, &dummy_border, &dummy_depth)) {
+               return -EFAULT;
+       }
+
+       return 0;
+}
+
+static inline int do_capture(Display *disp, Pixmap id, const char *filename)
+{
+       XShmSegmentInfo si;
+       XImage *xim;
+       Visual *visual;
+       unsigned int w;
+       unsigned int h;
+       int bufsz;
+       int fd;
+       Screen *screen;
+
+       screen = DefaultScreenOfDisplay(disp);
+       visual = DefaultVisualOfScreen(screen);
+
+       if (get_pixmap_size(disp, id, NULL, NULL, &w, &h) < 0) {
+               printf("Failed to get size of a pixmap\n");
+               return -EINVAL;
+       }
+
+       printf("Pixmap size: %dx%d\n", w, h);
+       bufsz = w * h * sizeof(int);
+
+       si.shmid = shmget(IPC_PRIVATE, bufsz, IPC_CREAT | 0666);
+       if (si.shmid < 0) {
+               printf("shmget: %d\n", errno);
+               return -EFAULT;
+       }
+
+       si.readOnly = False;
+       si.shmaddr = shmat(si.shmid, NULL, 0);
+       if (si.shmaddr == (void *)-1) {
+
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+                       printf("shmctl: %d\n", errno);
+               }
+
+               return -EFAULT;
+       }
+
+       /*!
+        * \NOTE
+        * Use the 24 bits Pixmap for Video player
+        */
+       xim = XShmCreateImage(disp, visual, 24 /* (depth << 3) */, ZPixmap, NULL, &si, w, h);
+       if (xim == NULL) {
+               if (shmdt(si.shmaddr) < 0) {
+                       printf("shmdt: %d\n", errno);
+               }
+
+               if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+                       printf("shmctl: %d\n", errno);
+               }
+
+               return -EFAULT;
+       }
+
+       xim->data = si.shmaddr;
+       XShmAttach(disp, &si);
+
+       XShmGetImage(disp, id, xim, 0, 0, 0xFFFFFFFF);
+       XSync(disp, False);
+
+       fd = open(filename, O_CREAT | O_RDWR, 0644);
+       if (fd >= 0) {
+               if (write(fd, xim->data, bufsz) != bufsz) {
+                       printf("Data is not fully written\n");
+               }
+
+               if (close(fd) < 0) {
+                       printf("close: %d\n", errno);
+               }
+       } else {
+               printf("Error: %d\n", errno);
+       }
+
+       XShmDetach(disp, &si);
+       XDestroyImage(xim);
+
+       if (shmdt(si.shmaddr) < 0) {
+               printf("shmdt: %d\n", errno);
+       }
+
+       if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
+               printf("shmctl: %d\n", errno);
+       }
+
+       return 0;
+}
+
+static int widget_lifecycle_event(const char *widget_id, widget_lifecycle_event_e ev, const char *instance_id, void *data)
+{
+       printf("[%s] [%s] [STATE: 0x%X]\n", widget_id, instance_id, (unsigned int)ev);
+       return WIDGET_ERROR_NONE;
+}
+
+static void do_monitor(const char *cmd)
+{
+       cmd = trim_cmd(cmd + strlen("monitor"));
+
+       if (!*cmd) {
+               /* Monitor all instances */
+       } else {
+               /* Monitor one */
+               printf("Instance = [%s]\n", cmd);
+       }
+
+       widget_service_set_lifecycle_event_cb(*cmd ? cmd : NULL, widget_lifecycle_event, NULL);
+}
+
+static int widget_inst_list_cb(const char *widget_id, const char *instance_id, void *data)
+{
+       printf("Instance: %s\n", instance_id);
+       return WIDGET_ERROR_NONE;
+}
+
+static void do_get_list(const char *cmd)
+{
+       cmd = trim_cmd(cmd + strlen("get_list"));
+       if (!*cmd) {
+               return;
+       }
+
+       printf("Instance list of %s\n", cmd);
+       if (widget_service_get_widget_instance_list(cmd, widget_inst_list_cb, NULL) == WIDGET_ERROR_NONE) {
+               printf("=== Done\n");
+       } else {
+               printf("Failed to get instance list\n");
+       }
+}
+
+static void do_get_content(const char *cmd)
+{
+       char widget_id[256];
+       char inst_id[256];
+       cmd = trim_cmd(cmd + strlen("get_content"));
+       if (!*cmd) {
+               return;
+       }
+
+       if (sscanf(cmd, "%[^ ] %s", widget_id, inst_id) == 2) {
+               bundle *b;
+               if (widget_service_get_content_of_widget_instance(widget_id, inst_id, &b) == WIDGET_ERROR_NONE) {
+                       int len;
+                       char *content = NULL;
+
+                       if (b && bundle_encode(b, (bundle_raw **)&content, &len) == BUNDLE_ERROR_NONE) {
+                               printf("Widget: %s\nInstance: %s\nContent: [%s]\n", widget_id, inst_id, content);
+                               bundle_free(b);
+                       } else {
+                               printf("Widget: %s\nInstance: %s\nbundle: %p\n", widget_id, inst_id, b);
+                       }
+
+                       free(content);
+               } else {
+                       printf("Failed to get content info for %s\n", inst_id);
+               }
+       }
+}
+
+static void do_demonitor(const char *cmd)
+{
+       cmd = trim_cmd(cmd + strlen("demonitor"));
+       if (!*cmd) {
+       } else {
+               printf("Instance = [%s]\n", cmd);
+       }
+
+       widget_service_unset_lifecycle_event_cb(*cmd ? cmd : NULL, NULL);
+}
+
+static void do_dump(const char *cmd)
+{
+       char path[256];
+       int idx;
+       struct node *node;
+       struct node *curdir;
+       struct node *target;
+
+       cmd = trim_cmd(cmd + 5);
+       if (!*cmd) {
+               return;
+       }
+
+       curdir = s_info.curdir;
+       do {
+               target = NULL;
+               idx = 0;
+               while (cmd[idx] != '/' && cmd[idx] != '\0' && cmd[idx] != ' ') {
+                       idx++;
+               }
+
+               if (!idx) {
+                       break;
+               }
+
+               strncpy(path, cmd, idx);
+               path[idx] = '\0';
+               cmd += idx;
+
+               node = node_child(curdir);
+               while (node) {
+                       if (!strcmp(node_name(node), path)) {
+                               curdir = node;
+                               target = curdir;
+                               break;
+                       }
+
+                       node = node_next_sibling(node);
+               }
+
+               if (*cmd != '/') {
+                       break;
+               }
+       } while ((*++cmd));
+
+       if (target && node_type(target) != NODE_DIR) {
+               struct instance *instinfo;
+
+               if (node_type(target) == NODE_LINK) {
+                       /* Follow up node */
+               }
+
+               instinfo = node_data(target);
+               if (instinfo) {
+                       while (*cmd == ' ') {
+                               cmd++;
+                       }
+
+                       if (sscanf(cmd, "%255[^ ]", path) != 1) {
+                               printf("dump file OUT_FILENAME\n");
+                               return;
+                       }
+
+                       if (instinfo->buf_id) {
+                               unsigned int pixmap;
+                               int pixel_size;
+                               if (sscanf(instinfo->buf_id, "pixmap://%u:%d", &pixmap, &pixel_size) == 2) {
+                                       Display *disp;
+
+                                       disp = XOpenDisplay(NULL);
+                                       if (!disp) {
+                                               printf("Failed to connect to the X\n");
+                                               return;
+                                       }
+
+                                       if (do_capture(disp, (Pixmap)pixmap, path) == 0) {
+                                               printf("Captured: %s\n", path);
+                                       }
+                                       XCloseDisplay(disp);
+                               } else {
+                                       printf("Unsupported type\n");
+                               }
+                       } else {
+                               printf("Buffer is not exists\n");
+                       }
+               }
+       } else {
+               printf("target only can be a filename\n");
+       }
+}
+
+static void do_x(const char *cmd)
+{
+       Display *disp;
+
+       cmd = trim_cmd(cmd + 2);
+       if (!*cmd) {
+               return;
+       }
+
+       disp = XOpenDisplay(NULL);
+       if (!disp) {
+               printf("Failed to connect to the X\n");
+               return;
+       }
+
+       if (!strncasecmp(cmd, "damage ", 7)) {
+               unsigned int winId;
+               XRectangle rect;
+               XserverRegion region;
+               int x, y, w, h;
+
+               cmd += 7;
+
+               if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
+                       printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
+                       return;
+               }
+               rect.x = x;
+               rect.y = y;
+               rect.width = w;
+               rect.height = h;
+               region = XFixesCreateRegion(disp, &rect, 1);
+               XDamageAdd(disp, winId, region);
+               XFixesDestroyRegion(disp, region);
+               XFlush(disp);
+
+               printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
+       } else if (!strncasecmp(cmd, "capture ", 8)) {
+               unsigned int winId;
+               char filename[256];
+
+               cmd += 8;
+
+               if (sscanf(cmd, "%u %255[^ ]", &winId, filename) != 2) {
+                       printf("Invalid argument\nx capture WINID_DEC FILENAME (%s)\n", cmd);
+                       return;
+               }
+               if (do_capture(disp, winId, filename) == 0) {
+                       printf("Captured: %s\n", filename);
+               }
+       } else if (!strncasecmp(cmd, "resize ", 7)) {
+               unsigned int winId;
+               int w;
+               int h;
+
+               cmd += 7;
+
+               if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
+                       printf("Invalid argument\nx resize WINID_DEC W H\n");
+                       return;
+               }
+
+               XResizeWindow(disp, winId, w, h);
+               printf("Resize: %u %d %d\n", winId, w, h);
+       } else if (!strncasecmp(cmd, "move ", 5)) {
+               unsigned int winId;
+               int x;
+               int y;
+
+               cmd += 5;
+               if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
+                       printf("Invalid argument\nx move WINID_DEC X Y\n");
+                       return;
+               }
+
+               XMoveWindow(disp, winId, x, y);
+               printf("Move: %u %d %d\n", winId, x, y);
+       } else if (!strncasecmp(cmd, "map ", 4)) {
+               unsigned int winId;
+               cmd += 4;
+               if (sscanf(cmd, "%u", &winId) != 1) {
+                       printf("Invalid argument\nx map WINID_DEC\n");
+                       return;
+               }
+               XMapRaised(disp, winId);
+               printf("Map: %u\n", winId);
+       } else if (!strncasecmp(cmd, "unmap ", 6)) {
+               unsigned int winId;
+               cmd += 6;
+               if (sscanf(cmd, "%u", &winId) != 1) {
+                       printf("Invalid argument\nx unmap WINID_DEC\n");
+                       return;
+               }
+               XUnmapWindow(disp, winId);
+               printf("Unmap: %u\n", winId);
+       } else {
+               printf("Unknown command\n");
+       }
+
+       XCloseDisplay(disp);
+}
+
+static inline void put_command(const char *cmd)
+{
+       if (s_info.history[s_info.history_top]) {
+               free(s_info.history[s_info.history_top]);
+               s_info.history[s_info.history_top] = NULL;
+       }
+
+       s_info.history[s_info.history_top] = strdup(cmd);
+       s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
+}
+
+static inline const char *get_command(int idx)
+{
+       idx = s_info.history_top + idx;
+       while (idx < 0) {
+               idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
+       }
+
+       return s_info.history[idx];
+}
+
+static void do_command(const char *cmd)
+{
+       /* Skip the first spaces */
+       cmd = trim_cmd(cmd);
+
+       if (strlen(cmd) && *cmd != '#') {
+               if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
+                       ecore_main_loop_quit();
+               } else if (!strncasecmp(cmd, "set ", 4)) {
+                       if (do_set(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "stat ", 5)) {
+                       do_stat(cmd);
+               } else if (!strncasecmp(cmd, "get ", 4)) {
+                       if (do_get(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "ls", 2)) {
+                       if (do_ls(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "cd", 2)) {
+                       if (do_cd(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "rm", 2)) {
+                       if (do_rm(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "fault", 5)) {
+                       if (do_fault(cmd) == 0) {
+                               return;
+                       }
+               } else if (!strncasecmp(cmd, "sh ", strlen("sh "))) {
+                       do_sh(cmd);
+               } else if (!strncasecmp(cmd, "x ", strlen("x "))) {
+                       do_x(cmd);
+               } else if (!strncasecmp(cmd, "dump", strlen("dump"))) {
+                       do_dump(cmd);
+               } else if (!strncasecmp(cmd, "monitor", strlen("monitor"))) {
+                       do_monitor(cmd);
+               } else if (!strncasecmp(cmd, "demonitor", strlen("demonitor"))) {
+                       do_demonitor(cmd);
+               } else if (!strncasecmp(cmd, "get_content", strlen("get_content"))) {
+                       do_get_content(cmd);
+               } else if (!strncasecmp(cmd, "get_list", strlen("get_list"))) {
+                       do_get_list(cmd);
+               } else {
+                       help();
+               }
+       }
+
+       prompt(NULL);
+       return;
+}
+
+#define CMD_BUFFER_SIZE 256
+
+static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
+{
+       static int idx = 0;
+       static char cmd_buffer[CMD_BUFFER_SIZE];
+       unsigned char ch;
+       int fd;
+       int ret;
+       const char escape_str[] = { 0x1b, 0x5b, 0x0 };
+       const char *escape_ptr = escape_str;
+       const char *tmp;
+
+       if (fd_handler) {
+               fd = ecore_main_fd_handler_fd_get(fd_handler);
+               if (fd < 0) {
+                       printf("FD is not valid: %d\n", fd);
+                       return ECORE_CALLBACK_CANCEL;
+               }
+       } else {
+               fd = s_info.input_fd;
+       }
+
+       /*!
+        * \note
+        * Using this routine, we can implement the command recommend algorithm.
+        * When a few more characters are matched with history of command, we can show it to user
+        * Then the user will choose one or write new command
+        */
+
+       /* Silly.. Silly */
+       while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
+               if (*escape_ptr == '\0') {
+                       /* Function key */
+                       switch (ch) {
+                       case 0x41: /* UP */
+                               printf("%s2K%s1G", escape_str, escape_str);
+                               tmp = get_command(--s_info.history_idx);
+                               if (!tmp) {
+                                       s_info.history_idx = 0;
+                                       cmd_buffer[0] = '\0';
+                                       prompt(NULL);
+                               } else {
+                                       strcpy(cmd_buffer, tmp);
+                                       idx = strlen(cmd_buffer);
+                                       prompt(cmd_buffer);
+                               }
+                               break;
+                       case 0x42: /* DOWN */
+                               if (s_info.history_idx >= 0) {
+                                       break;
+                               }
+
+                               printf("%s2K%s1G", escape_str, escape_str);
+                               tmp = get_command(++s_info.history_idx);
+                               if (s_info.history_idx == 0) {
+                                       s_info.history_idx = 0;
+                                       cmd_buffer[0] = '\0';
+                                       prompt(NULL);
+                               } else {
+                                       strncpy(cmd_buffer, tmp, CMD_BUFFER_SIZE - 1);
+                                       cmd_buffer[CMD_BUFFER_SIZE - 1]  = '\0';
+                                       idx = strlen(cmd_buffer);
+                                       prompt(cmd_buffer);
+                               }
+                               break;
+                       case 0x43: /* RIGHT */
+                               break;
+                       case 0x44: /* LEFT */
+                               break;
+                       default:
+                               break;
+                       }
+
+                       escape_ptr = escape_str;
+                       continue;
+               } else if (ch == *escape_ptr) {
+                       escape_ptr++;
+                       continue;
+               }
+
+               switch (ch) {
+               case 0x7F: /* BKSP */
+               case 0x80:
+                       cmd_buffer[idx] = '\0';
+                       if (idx > 0) {
+                               idx--;
+                               cmd_buffer[idx] = ' ';
+                               putc('\r', stdout);
+                               prompt(cmd_buffer);
+                       }
+
+                       cmd_buffer[idx] = '\0';
+                       putc('\r', stdout);
+                       prompt(cmd_buffer);
+                       break;
+               case 0x09: /* TAB */
+                       if (!s_info.quick_search_node) {
+                               s_info.quick_search_node = node_child(s_info.curdir);
+                               s_info.quick_idx = idx;
+                       } else {
+                               s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
+                               idx = s_info.quick_idx;
+                       }
+
+                       if (!s_info.quick_search_node) {
+                               break;
+                       }
+
+                       printf("%s2K%s1G", escape_str, escape_str);
+                       strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
+                       idx += strlen(node_name(s_info.quick_search_node));
+                       prompt(cmd_buffer);
+                       break;
+               case '\n':
+               case '\r':
+                       cmd_buffer[idx] = '\0';
+                       idx = 0;
+                       if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
+                               putc((int)'\n', stdout);
+                       }
+                       do_command(cmd_buffer);
+                       put_command(cmd_buffer);
+                       memset(cmd_buffer, 0, sizeof(cmd_buffer));
+                       s_info.history_idx = 0;
+                       s_info.quick_search_node = NULL;
+
+                       /* Make a main loop processing for command handling */
+                       return ECORE_CALLBACK_RENEW;
+               default:
+                       if (isascii(ch)) {
+                               cmd_buffer[idx++] = ch;
+
+                               if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
+                                       putc((int)ch, stdout);
+                               }
+
+                               if (idx == sizeof(cmd_buffer) - 1) {
+                                       cmd_buffer[idx] = '\0';
+                                       printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
+                                       idx = 0;
+                               }
+                       } else {
+                               printf("Unknown character: 0x%X\n", (unsigned int)ch);
+                       }
+                       break;
+               }
+       }
+
+       if (ret < 0 && !fd_handler) {
+               ecore_main_loop_quit();
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static void processing_line_buffer(const char *buffer)
+{
+       int pid;
+       char slavename[256];
+       char pkgname[256];
+       char abi[256];
+       char inst_id[4096];
+       char buf_id[256];
+       char cluster[256];
+       char category[256];
+       char str_period[64];
+       char state[10];
+       int refcnt;
+       int fault_count;
+       int list_count;
+       int loaded_inst;
+       int loaded_pkg;
+       double ttl;
+       int secured;
+       double period;
+       int width;
+       int height;
+       struct node *node;
+       struct package *pkginfo;
+       struct instance *instinfo;
+       struct slave *slaveinfo;
+       int i;
+
+       switch (s_info.cmd) {
+       case PKG_LIST:
+               if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
+                       printf("Invalid format : [%s]\n", buffer);
+                       return;
+               }
+
+               node = node_find(s_info.targetdir, pkgname);
+               if (!node) {
+                       pkginfo = calloc(1, sizeof(*pkginfo));
+                       if (!pkginfo) {
+                               printf("calloc: %d\n", errno);
+                               return;
+                       }
+
+                       pkginfo->pkgid = strdup("conf.file");
+                       if (!pkginfo->pkgid) {
+                               printf("strdup: %d\n", errno);
+                       }
+
+                       pkginfo->primary = 1;
+
+                       node = node_create(s_info.targetdir, pkgname, NODE_DIR, NODE_READ | NODE_EXEC);
+                       if (!node) {
+                               free(pkginfo->pkgid);
+                               free(pkginfo);
+                               printf("Failed to create a new node (%s)\n", pkgname);
+                               return;
+                       }
+
+                       node_set_data(node, pkginfo);
+               } else {
+                       pkginfo = node_data(node);
+                       if (!pkginfo) {
+                               printf("Package info is inavlid\n");
+                               return;
+                       }
+
+                       free(pkginfo->slavename);
+                       free(pkginfo->abi);
+
+                       pkginfo->slavename = NULL;
+                       pkginfo->abi = NULL;
+               }
+
+               node_set_age(node, s_info.age);
+
+               pkginfo->slavename = strdup(slavename);
+               if (!pkginfo->slavename) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               pkginfo->abi = strdup(abi);
+               if (!pkginfo->abi) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               pkginfo->pid = pid;
+               pkginfo->refcnt = refcnt;
+               pkginfo->fault_count = fault_count;
+               pkginfo->inst_count = list_count;
+               break;
+       case SLAVE_LIST:
+               if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d %[^ ] %d %d %lf", &pid, slavename, pkgname, abi, &secured, &refcnt, &fault_count, state, &loaded_inst, &loaded_pkg, &ttl) != 11) {
+                       printf("Invalid format : [%s]\n", buffer);
+                       return;
+               }
+               node = node_find(s_info.targetdir, slavename);
+               if (!node) {
+                       slaveinfo = calloc(1, sizeof(*slaveinfo));
+                       if (!slaveinfo) {
+                               printf("Error: %d\n", errno);
+                               return;
+                       }
+
+                       node = node_create(s_info.targetdir, slavename, NODE_DIR, NODE_READ | NODE_EXEC);
+                       if (!node) {
+                               free(slaveinfo);
+                               return;
+                       }
+
+                       node_set_data(node, slaveinfo);
+               } else {
+                       slaveinfo = node_data(node);
+               }
+
+               node_set_age(node, s_info.age);
+
+               free(slaveinfo->pkgname);
+               free(slaveinfo->abi);
+               free(slaveinfo->state);
+
+               slaveinfo->pkgname = strdup(pkgname);
+               if (!slaveinfo->pkgname) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               slaveinfo->abi = strdup(abi);
+               if (!slaveinfo->abi) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               slaveinfo->state = strdup(state);
+               if (!slaveinfo->state) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               slaveinfo->pid = pid;
+               slaveinfo->secured = secured;
+               slaveinfo->refcnt = refcnt;
+               slaveinfo->fault_count = fault_count;
+               slaveinfo->loaded_inst = loaded_inst;
+               slaveinfo->loaded_pkg = loaded_pkg;
+               slaveinfo->ttl = ttl;
+               break;
+       case INST_LIST:
+               if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %d %d", inst_id, buf_id, cluster, category, str_period, state, &width, &height) != 8) {
+                       if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %d %d", inst_id, cluster, category, str_period, state, &width, &height) != 7) {
+                               printf("Invalid format : [%s]\n", buffer);
+                               return;
+                       } else {
+                               buf_id[0] = '\0';
+                       }
+               }
+
+               period = strtod(str_period, NULL);
+
+               for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
+               i += (inst_id[i] == '/');
+
+               node = node_find(s_info.targetdir, inst_id + i);
+               if (!node) {
+                       instinfo = calloc(1, sizeof(*instinfo));
+                       if (!instinfo) {
+                               printf("calloc: %d\n", errno);
+                               return;
+                       }
+
+                       node = node_create(s_info.targetdir, inst_id + i, NODE_FILE, NODE_READ | NODE_WRITE);
+                       if (!node) {
+                               free(instinfo);
+                               return;
+                       }
+
+                       node_set_data(node, instinfo);
+               } else {
+                       instinfo = node_data(node);
+               }
+
+               node_set_age(node, s_info.age);
+
+               free(instinfo->id);
+               instinfo->id = NULL;
+               free(instinfo->buf_id);
+               instinfo->buf_id = NULL;
+               free(instinfo->cluster);
+               instinfo->cluster = NULL;
+               free(instinfo->category);
+               instinfo->category = NULL;
+               free(instinfo->state);
+               instinfo->state = NULL;
+
+               instinfo->id = strdup(inst_id);
+               if (!instinfo->id) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               instinfo->cluster = strdup(cluster);
+               if (!instinfo->cluster) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               instinfo->category = strdup(category);
+               if (!instinfo->category) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               instinfo->state = strdup(state);
+               if (!instinfo->state) {
+                       printf("strdup: %d\n", errno);
+               }
+
+               if (strlen(buf_id)) {
+                       instinfo->buf_id = strdup(buf_id);
+               }
+
+               instinfo->period = period;
+               instinfo->width = width;
+               instinfo->height = height;
+               break;
+       case INST_CTRL:
+               sscanf(buffer, "%d", &i);
+               printf("%d\n", i);
+               printf("Result: %d\n", i);
+               break;
+       case SLAVE_CTRL:
+               sscanf(buffer, "%d", &i);
+               printf("Result: %d\n", i);
+               break;
+       case MASTER_CTRL:
+               sscanf(buffer, "%d", &i);
+               printf("Result: %d\n", i);
+               break;
+       default:
+               break;
+       }
+}
+
+static inline void do_line_command(void)
+{
+       switch (s_info.cmd) {
+       case PKG_LIST:
+               ls();
+               break;
+       case INST_LIST:
+               ls();
+               break;
+       case SLAVE_LIST:
+               ls();
+               break;
+       case INST_CTRL:
+               break;
+       case SLAVE_CTRL:
+               break;
+       case MASTER_CTRL:
+               break;
+       default:
+               break;
+       }
+       prompt(NULL);
+}
+
+static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
+{
+       int fd;
+       static char *line_buffer = NULL;
+       static int line_index = 0;
+       static int bufsz = 256;
+       char ch;
+
+       fd = ecore_main_fd_handler_fd_get(fd_handler);
+       if (fd < 0) {
+               printf("FD is not valid: %d\n", fd);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
+               printf("read: %d\n", errno);
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!line_buffer) {
+               line_index = 0;
+               line_buffer = malloc(bufsz);
+               if (!line_buffer) {
+                       printf("malloc: %d\n", errno);
+                       return ECORE_CALLBACK_CANCEL;
+               }
+       }       
+
+       if (ch == '\n') { /* End of a line */
+               if (line_index == bufsz - 1) {
+                       char *new_buf;
+                       new_buf = realloc(line_buffer, bufsz + 2);
+                       if (!new_buf) {
+                               printf("realloc: %d\n", errno);
+                               free(line_buffer);
+                               line_buffer = NULL;
+                               line_index = 0;
+                               bufsz = 256;
+                               return ECORE_CALLBACK_CANCEL;
+                       }
+
+                       line_buffer = new_buf;
+               }
+
+               line_buffer[line_index] = '\0';
+
+               if (!strcmp(line_buffer, "EOD")) {
+                       do_line_command();
+                       s_info.cmd = NOP;
+               } else {
+                       processing_line_buffer(line_buffer);
+               }
+
+               free(line_buffer);
+               line_buffer = NULL;
+               line_index = 0;
+               bufsz = 256;
+       } else {
+               char *new_buf;
+
+               line_buffer[line_index++] = ch;
+               if (line_index == bufsz - 1) {
+                       bufsz += 256;
+                       new_buf = realloc(line_buffer, bufsz);
+                       if (!new_buf) {
+                               printf("realloc: %d\n", errno);
+                               free(line_buffer);
+                               line_buffer = NULL;
+                               line_index = 0;
+                               bufsz = 256;
+                               return ECORE_CALLBACK_CANCEL;
+                       }
+
+                       line_buffer = new_buf;
+               }
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
+{
+       const char *fifo_name;
+       int ret;
+
+       if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
+               printf("Invalid packet\n");
+               return -EFAULT;
+       }
+
+       if (ret != 0) {
+               printf("Returns %d\n", ret);
+               return ret;
+       }
+
+       printf("FIFO: %s\n", fifo_name);
+
+       s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
+       if (s_info.fifo_handle < 0) {
+               printf("open: %d\n", errno);
+               s_info.fifo_handle = -EINVAL;
+               ecore_main_loop_quit();
+               return -EINVAL;
+       }
+
+       s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
+       if (!s_info.fd_handler) {
+               printf("Failed to add a fd handler\n");
+               if (close(s_info.fifo_handle) < 0) {
+                       printf("close: %d\n", errno);
+               }
+               s_info.fifo_handle = -EINVAL;
+               ecore_main_loop_quit();
+               return -EFAULT;
+       }
+
+       prompt(NULL);
+
+       if (s_info.input_fd == STDIN_FILENO) {
+               if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0) {
+                       printf("fcntl: %d\n", errno);
+               }
+
+               s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
+               if (!s_info.in_handler) {
+                       printf("Failed to add a input handler\n");
+                       ecore_main_loop_quit();
+                       return -EFAULT;
+               }
+       }
+
+       return 0;
+}
+
+static int disconnected_cb(int handle, void *data)
+{
+       printf("Disconnected\n");
+       ecore_main_loop_quit();
+       return 0;
+}
+
+static int connected_cb(int handle, void *data)
+{
+       struct packet *packet;
+
+       printf("Connected\n");
+
+       packet = packet_create("widget_mgr_hello", "d", 0.0f);
+       if (!packet) {
+               printf("Failed to build a packet for hello\n");
+               com_core_packet_client_fini(s_info.fd);
+               s_info.fd = -EINVAL;
+               return -EFAULT;
+       }
+
+       s_info.fd = handle;
+
+       if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
+               printf("Failed to send a packet hello\n");
+               packet_destroy(packet);
+               com_core_packet_client_fini(s_info.fd);
+               s_info.fd = -EINVAL;
+               return -EFAULT;
+       }
+
+       packet_destroy(packet);
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct termios ttystate;
+       static struct method s_table[] = {
+               {
+                       .cmd = NULL,
+                       .handler = NULL,
+               },
+       };
+       static struct option long_options[] = {
+               { "batchmode", required_argument, 0, 'b' },
+               { "help", no_argument, 0, 'h' },
+               { "verbose", required_argument, 0, 'v' },
+               { "execute", required_argument, 0, 'x' },
+               { 0, 0, 0, 0 }
+       };
+       int option_index;
+       int c;
+
+       do {
+               c = getopt_long(argc, argv, "b:hv:x:d", long_options, &option_index);
+               switch (c) {
+               case 'b':
+                       if (!optarg || !*optarg) {
+                               printf("Invalid argument\n");
+                               help();
+                               return -EINVAL;
+                       }
+
+                       if (s_info.input_fd != STDIN_FILENO) {
+                               /* Close the previously, opened file */
+                               if (close(s_info.input_fd) < 0) {
+                                       printf("close: %d\n", errno);
+                               }
+                       }
+
+                       s_info.input_fd = open(optarg, O_RDONLY);
+                       if (s_info.input_fd < 0) {
+                               printf("Unable to access %s (%d)\n", optarg, errno);
+                               return -EIO;
+                       }
+                       break;
+               case 'h':
+                       help();
+                       return 0;
+               case 'v':
+                       if (!optarg || !*optarg) {
+                               printf("Invalid argument\n");
+                               help();
+                               return -EINVAL;
+                       }
+
+                       s_info.verbose = !strcmp(optarg, "true");
+                       break;
+               case 'x':
+                       if (!optarg || !*optarg) {
+                               printf("Invalid argument\n");
+                               help();
+                               return -EINVAL;
+                       }
+                       break;
+               case 'd':
+                       /** Dump all buffer to a files */
+                       s_info.dump = 1;
+                       break;
+               default:
+                       break;
+               }
+       } while (c != -1);
+
+       ecore_init();
+
+#if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
+       g_type_init();
+#endif
+
+       /**
+        * Make the system daemon can access proc file to validate myself
+        */
+
+       com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
+       com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
+       widget_service_init();
+
+       s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
+       if (s_info.fd < 0) {
+               printf("Failed to make a connection\n");
+               return -EIO;
+       }
+
+       if (s_info.input_fd == STDIN_FILENO) {
+               printf("Type your command on below empty line\n");
+
+               if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
+                       printf("Error: %d\n", errno);
+               } else {
+                       ttystate.c_lflag &= ~(ICANON | ECHO);
+                       ttystate.c_cc[VMIN] = 1;
+
+                       if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
+                               printf("Error: %d\n", errno);
+                       }
+               }
+       } else {
+               printf("Batch mode enabled\n");
+       }
+
+       if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0) {
+               printf("setvbuf: %d\n", errno);
+       }
+
+       init_directory();
+
+       ecore_main_loop_begin();
+
+       fini_directory();
+       widget_service_fini();
+
+       if (s_info.fd > 0) {
+               com_core_packet_client_fini(s_info.fd);
+               s_info.fd = -EINVAL;
+       }
+
+       if (s_info.fd_handler) {
+               ecore_main_fd_handler_del(s_info.fd_handler);
+               s_info.fd_handler = NULL;
+       }
+
+       if (s_info.input_fd == STDIN_FILENO) {
+               ttystate.c_lflag |= ICANON | ECHO;
+               if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
+                       printf("tcsetattr: %d\n", errno);
+               }
+       } else {
+               if (close(s_info.input_fd) < 0) {
+                       printf("close: %d\n", errno);
+               }
+       }
+
+       if (s_info.fifo_handle > 0) {
+               if (close(s_info.fifo_handle) < 0) {
+                       printf("close: %d\n", errno);
+               }
+               s_info.fifo_handle = -EINVAL;
+       }
+
+       if (s_info.in_handler) {
+               ecore_main_fd_handler_del(s_info.in_handler);
+               s_info.in_handler = NULL;
+       }
+
+       ecore_shutdown();
+       putc((int)'\n', stdout);
+       return 0;
+}
+
+/* End of a file */