--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 __AUL_SCREEN_CONNECTOR_H__
+#define __AUL_SCREEN_CONNECTOR_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void (*aul_app_screen_added)(const char *appid, const int pid,
+ const unsigned int surface_id, void *data);
+typedef void (*aul_app_screen_removed)(const char *appid, const int pid,
+ void *data);
+
+typedef struct aul_screen_viewer_handler_s {
+ aul_app_screen_added app_added;
+ aul_app_screen_removed app_removed;
+} aul_screen_viewer_handler;
+
+/*
+ * This API is only for Appfw internally.
+ */
+int aul_screen_connector_add_app_screen(unsigned int surf);
+
+/*
+ * This API is only for Appfw internally.
+ */
+int aul_screen_connector_remove_app_screen(void);
+
+/*
+ * This API is only for Appfw internally.
+ */
+int aul_screen_connector_update_app_screen(const char *appid);
+
+/*
+ * This API is only for Appfw internally.
+ */
+int aul_screen_connector_add_screen_viewer(aul_screen_viewer_handler *cbs,
+ void *data);
+
+/*
+ * This API is only for Appfw internally.
+ */
+int aul_screen_connector_remove_screen_viewer(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __AUL_SCREEN_CONNECTOR_H__ */
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 <stdbool.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <glib.h>
+#include <bundle.h>
+#include <bundle_internal.h>
+
+#include "aul_util.h"
+#include "aul_api.h"
+#include "aul.h"
+#include "aul_sock.h"
+#include "aul_app_com.h"
+#include "aul_screen_connector.h"
+
+static aul_app_com_connection_h added_conn;
+static aul_app_com_connection_h removed_conn;
+static bool screen_viewer_initialized;
+static aul_screen_viewer_handler *screen_viewer_handler;
+static void *screen_viewer_data;
+
+static int __add_screen_viewer(void)
+{
+ int ret;
+ bundle *b;
+
+ b = bundle_create();
+ if (b == NULL) {
+ _E("out of memory");
+ return -1;
+ }
+
+ ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
+ ADD_SCREEN_VIEWER, b, AUL_SOCK_NONE);
+ bundle_free(b);
+ if (ret < 0)
+ return -1;
+
+ return 0;
+}
+
+static int __remove_screen_viewer(void)
+{
+ int ret;
+ bundle *b;
+
+ b = bundle_create();
+ if (b == NULL) {
+ _E("out of memory");
+ return -1;
+ }
+
+ ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
+ REMOVE_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
+ bundle_free(b);
+ if (ret < 0)
+ return -1;
+
+ return 0;
+}
+
+static int __app_screen_added(const char *endpoint, aul_app_com_result_e res,
+ bundle *envelope, void *user_data)
+{
+ char *appid = NULL;
+ unsigned int *surf = NULL;
+ int *pid = NULL;
+ size_t size;
+
+ bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
+ if (appid == NULL) {
+ _E("Failed to get appid");
+ return -1;
+ }
+
+ bundle_get_byte(envelope, "__AUL_SC_SURFACE__",
+ (void **)&surf, &size);
+ if (surf == NULL) {
+ _E("Failed to get surface");
+ return -1;
+ }
+
+ bundle_get_byte(envelope, "__AUL_SC_PID__", (void **)&pid, &size);
+ if (pid == NULL) {
+ _E("Failed to get pid");
+ return -1;
+ }
+
+ if (screen_viewer_handler->app_added) {
+ screen_viewer_handler->app_added(appid, *pid, *surf,
+ screen_viewer_data);
+ }
+ _D("appid(%s), pid(%d), surface(%d)", appid, *pid, *surf);
+
+ return 0;
+}
+
+static int __app_screen_removed(const char *endpoint, aul_app_com_result_e res,
+ bundle *envelope, void *user_data)
+{
+ char *appid = NULL;
+ int *pid = NULL;
+ size_t size;
+
+ bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
+ if (appid == NULL) {
+ _E("Failed to get appid");
+ return -1;
+ }
+
+ bundle_get_byte(envelope, "__AUL_SC_PID__", (void **)&pid, &size);
+ if (pid == NULL) {
+ _E("Failed to get pid");
+ return -1;
+ }
+
+ if (screen_viewer_handler->app_removed) {
+ screen_viewer_handler->app_removed(appid, *pid,
+ screen_viewer_data);
+ }
+ _D("appid(%s), pid(%d)", appid, *pid);
+
+ return 0;
+}
+
+static int __screen_viewer_fini(void)
+{
+ int ret;
+
+ if (!screen_viewer_initialized)
+ return 0;
+
+ if (removed_conn) {
+ aul_app_com_leave(removed_conn);
+ removed_conn = NULL;
+ }
+
+ if (added_conn) {
+ aul_app_com_leave(added_conn);
+ added_conn = NULL;
+ }
+
+ ret = __remove_screen_viewer();
+ if (ret < 0) {
+ _E("Failed to remove screen watcher");
+ return -1;
+ }
+
+ screen_viewer_initialized = false;
+
+ return 0;
+}
+
+static int __screen_viewer_init(void)
+{
+ int ret;
+
+ if (screen_viewer_initialized)
+ return 0;
+
+ ret = __add_screen_viewer();
+ if (ret < 0) {
+ _E("Failed to add screen watcher");
+ return -1;
+ }
+
+ aul_app_com_create("app_screen_added", NULL, __app_screen_added,
+ NULL, &added_conn);
+ if (added_conn == NULL) {
+ _E("Failed to create app com - watch_app_added");
+ __screen_viewer_fini();
+ return -1;
+ }
+
+ aul_app_com_create("app_screen_removed", NULL, __app_screen_removed,
+ NULL, &removed_conn);
+ if (removed_conn == NULL) {
+ _E("Failed to create app com - watch_app_removed");
+ __screen_viewer_fini();
+ return -1;
+ }
+
+ screen_viewer_initialized = true;
+
+ return 0;
+}
+
+API int aul_screen_connector_add_screen_viewer(aul_screen_viewer_handler *cbs,
+ void *data)
+{
+ if (cbs == NULL) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ if (__screen_viewer_init() < 0) {
+ _E("Failed to initialize screen viewer");
+ return AUL_R_ERROR;
+ }
+
+ screen_viewer_handler = cbs;
+ screen_viewer_data = data;
+
+ return AUL_R_OK;
+}
+
+API int aul_screen_connector_remove_screen_viewer(void)
+{
+ if (__screen_viewer_fini() < 0) {
+ _E("Failed to finalize screen viewer");
+ return -1;
+ }
+
+ screen_viewer_handler = NULL;
+ screen_viewer_data = NULL;
+
+ return AUL_R_OK;
+}
+
+API int aul_screen_connector_add_app_screen(unsigned int surf)
+{
+ int ret;
+ bundle *b;
+ char buf[MAX_PID_STR_BUFSZ];
+
+ b = bundle_create();
+ if (b == NULL) {
+ _E("out of memory");
+ return AUL_R_ERROR;
+ }
+
+ snprintf(buf, sizeof(buf), "%d", surf);
+ ret = bundle_add(b, AUL_K_WID, buf);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add surf");
+ bundle_free(b);
+ return AUL_R_ERROR;
+ }
+
+ ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
+ ADD_APP_SCREEN, b, AUL_SOCK_NONE);
+ bundle_free(b);
+ if (ret < 0) {
+ _E("Failed to add app screen");
+ return ret;
+ }
+
+ return AUL_R_OK;
+}
+
+API int aul_screen_connector_remove_app_screen(void)
+{
+ int ret;
+ bundle *b;
+
+ b = bundle_create();
+ if (b == NULL) {
+ _E("out of memory");
+ return AUL_R_ERROR;
+ }
+
+ ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
+ REMOVE_APP_SCREEN, b, AUL_SOCK_NONE);
+ bundle_free(b);
+ if (ret < 0) {
+ _E("Failed to remove app screen");
+ return ret;
+ }
+
+ return AUL_R_OK;
+}
+
+API int aul_screen_connector_update_app_screen(const char *appid)
+{
+ int ret;
+ bundle *b;
+
+ if (appid == NULL) {
+ _E("Invalid parameter");
+ return -1;
+ }
+
+ b = bundle_create();
+ if (b == NULL) {
+ _E("out of memory");
+ return AUL_R_ERROR;
+ }
+
+ ret = bundle_add(b, AUL_K_APPID, appid);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add appid");
+ bundle_free(b);
+ return AUL_R_ERROR;
+ }
+
+ ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
+ UPDATE_APP_SCREEN, b, AUL_SOCK_NONE);
+ bundle_free(b);
+ if (ret < 0) {
+ _E("Failed to update app screen");
+ return ret;
+ }
+
+ return AUL_R_OK;
+}