#ifndef __AUL_SCREEN_CONNECTOR_H__
#define __AUL_SCREEN_CONNECTOR_H__
+#include <stdbool.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 enum {
+ AUL_SCREEN_TYPE_WIDGET = 0x01,
+ AUL_SCREEN_TYPE_WATCH = 0x02,
+ AUL_SCREEN_TYPE_UI = 0x04,
+ AUL_SCREEN_TYPE_ALL = AUL_SCREEN_TYPE_WIDGET | AUL_SCREEN_TYPE_WATCH | AUL_SCREEN_TYPE_UI,
+} aul_screen_type_e;
+
+typedef enum {
+ AUL_SCREEN_CONNECTOR_EVENT_TYPE_ADD,
+ AUL_SCREEN_CONNECTOR_EVENT_TYPE_REMOVE,
+ AUL_SCREEN_CONNECTOR_EVENT_TYPE_UPDATE,
+} aul_screen_connector_event_type_e;
+
+typedef void (*aul_screen_viewer_cb)(const char *appid,
+ const char *instance_id, const int pid,
+ const unsigned int surface_id,
+ aul_screen_connector_event_type_e event_type, 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;
+typedef struct aul_screen_viewer_s *aul_screen_viewer_h;
/*
* This API is only for Appfw internally.
*/
-int aul_screen_connector_add_app_screen(unsigned int surf);
+int aul_screen_connector_add_app_screen(const char *instance_id,
+ unsigned int surf);
/*
* This API is only for Appfw internally.
*/
-int aul_screen_connector_remove_app_screen(void);
+int aul_screen_connector_remove_app_screen(const char *instance_id);
/*
* This API is only for Appfw internally.
*/
-int aul_screen_connector_update_app_screen(const char *appid);
+int aul_screen_connector_send_update_request(const char *appid,
+ const char *instance_id);
/*
* This API is only for Appfw internally.
*/
-int aul_screen_connector_add_screen_viewer(aul_screen_viewer_handler *cbs,
- void *data);
+int aul_screen_connector_add_screen_viewer(aul_screen_viewer_cb callback,
+ aul_screen_type_e type, bool priv,
+ void *data, aul_screen_viewer_h *handle);
/*
* This API is only for Appfw internally.
*/
-int aul_screen_connector_remove_screen_viewer(void);
+int aul_screen_connector_remove_screen_viewer(aul_screen_viewer_h handle);
#ifdef __cplusplus
}
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <glib.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;
+struct aul_screen_viewer_s {
+ aul_app_com_connection_h conn;
+ aul_screen_viewer_cb callback;
+ aul_screen_type_e type;
+ bool priv;
+ unsigned int ref;
+ void *user_data;
+};
-static int __add_screen_viewer(void)
+static unsigned int ref;
+
+static unsigned int __get_ref(void)
+{
+ return ++ref;
+}
+
+static int __add_screen_viewer(int type, bool priv, unsigned int ref)
{
int ret;
bundle *b;
+ char buf[MAX_PID_STR_BUFSZ];
b = bundle_create();
if (b == NULL) {
return -1;
}
+ snprintf(buf, sizeof(buf), "%d", type);
+ ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add screen type(%d)", type);
+ bundle_free(b);
+ return -1;
+ }
+
+ snprintf(buf, sizeof(buf), "%u", ref);
+ ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add viewer reference(%u)", ref);
+ bundle_free(b);
+ return -1;
+ }
+
+ if (priv) {
+ ret = bundle_add(b, AUL_K_PRIVATE, "true");
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add bundle data");
+ bundle_free(b);
+ return -1;
+ }
+ }
+
ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
- ADD_SCREEN_VIEWER, b, AUL_SOCK_NONE);
+ ADD_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
bundle_free(b);
if (ret < 0)
return -1;
return 0;
}
-static int __remove_screen_viewer(void)
+static int __remove_screen_viewer(int type, bool priv, unsigned int ref)
{
int ret;
bundle *b;
+ char buf[MAX_PID_STR_BUFSZ];
b = bundle_create();
if (b == NULL) {
return -1;
}
+ snprintf(buf, sizeof(buf), "%d", type);
+ ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add view mode");
+ bundle_free(b);
+ return -1;
+ }
+
+ snprintf(buf, sizeof(buf), "%u", ref);
+ ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add viewer reference(%u)", ref);
+ bundle_free(b);
+ return -1;
+ }
+
+ if (priv) {
+ ret = bundle_add(b, AUL_K_PRIVATE, "true");
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add bundle data");
+ bundle_free(b);
+ return -1;
+ }
+ }
+
ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
REMOVE_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
bundle_free(b);
return 0;
}
-static int __app_screen_added(const char *endpoint, aul_app_com_result_e res,
+static int __app_screen_event_cb(const char *endpoint, aul_app_com_result_e res,
bundle *envelope, void *user_data)
{
+ aul_screen_viewer_h handle = (aul_screen_viewer_h)user_data;
char *appid = NULL;
+ char *instance_id = NULL;
unsigned int *surf = NULL;
int *pid = NULL;
size_t size;
+ char *event = NULL;
+ aul_screen_connector_event_type_e event_type;
+
+ bundle_get_str(envelope, "__AUL_SC_EVENT__", &event);
+ if (event == NULL) {
+ _E("Failed to get screen connector event");
+ return -1;
+ } else if (strcmp(event, "add_screen") == 0) {
+ event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_ADD;
+ } else if (strcmp(event, "remove_screen") == 0) {
+ event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_REMOVE;
+ } else if (strcmp(event, "update_screen") == 0) {
+ event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_UPDATE;
+ } else {
+ _E("Unknown event type(%s)", event);
+ return -1;
+ }
bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
if (appid == NULL) {
_E("Failed to get pid");
return -1;
}
+ bundle_get_str(envelope, "__AUL_SC_INSTANCE_ID__", &instance_id);
- if (screen_viewer_handler->app_added) {
- screen_viewer_handler->app_added(appid, *pid, *surf,
- screen_viewer_data);
+ if (handle->callback) {
+ handle->callback(appid, instance_id, *pid, *surf,
+ event_type, handle->user_data);
}
- _D("appid(%s), pid(%d), surface(%d)", appid, *pid, *surf);
+ _D("appid(%s), instance_id(%s), pid(%d), surface(%d), event_type(%d)",
+ appid, instance_id, *pid, *surf, event_type);
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)
+static int __screen_viewer_fini(aul_screen_viewer_h screen_viewer)
{
int ret;
- if (!screen_viewer_initialized)
- return 0;
-
- if (removed_conn) {
- aul_app_com_leave(removed_conn);
- removed_conn = NULL;
+ if (screen_viewer->conn) {
+ aul_app_com_leave(screen_viewer->conn);
+ screen_viewer->conn = NULL;
}
- if (added_conn) {
- aul_app_com_leave(added_conn);
- added_conn = NULL;
- }
-
- ret = __remove_screen_viewer();
+ ret = __remove_screen_viewer(screen_viewer->type, screen_viewer->priv,
+ screen_viewer->ref);
if (ret < 0) {
_E("Failed to remove screen watcher");
return -1;
}
- screen_viewer_initialized = false;
-
return 0;
}
-static int __screen_viewer_init(void)
+static int __screen_viewer_init(aul_screen_viewer_h screen_viewer)
{
int ret;
char endpoint[128];
pid_t pid = getpid();
- if (screen_viewer_initialized)
- return 0;
-
- snprintf(endpoint, sizeof(endpoint), "app_screen_added:%d", pid);
- aul_app_com_create(endpoint, NULL, __app_screen_added,
- NULL, &added_conn);
- if (added_conn == NULL) {
- _E("Failed to create app com - watch_app_added");
+ snprintf(endpoint, sizeof(endpoint), "app_screen_event:%u:%d",
+ screen_viewer->ref, pid);
+ aul_app_com_create(endpoint, NULL, __app_screen_event_cb,
+ screen_viewer, &screen_viewer->conn);
+ if (screen_viewer->conn == NULL) {
+ _E("Failed to create app com");
return -1;
}
- snprintf(endpoint, sizeof(endpoint), "app_screen_removed:%d", pid);
- aul_app_com_create(endpoint, 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;
- }
-
- ret = __add_screen_viewer();
+ ret = __add_screen_viewer(screen_viewer->type, screen_viewer->priv,
+ screen_viewer->ref);
if (ret < 0) {
_E("Failed to add screen watcher");
- __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)
+API int aul_screen_connector_add_screen_viewer(aul_screen_viewer_cb callback,
+ aul_screen_type_e type, bool priv,
+ void *data, aul_screen_viewer_h *handle)
{
- if (cbs == NULL) {
+ struct aul_screen_viewer_s *screen_viewer;
+
+ if (handle == NULL || callback == NULL ||
+ !(type & AUL_SCREEN_TYPE_ALL)) {
_E("Invalid parameter");
return AUL_R_EINVAL;
}
- if (__screen_viewer_init() < 0) {
- _E("Failed to initialize screen viewer");
- return AUL_R_ERROR;
+ screen_viewer = (struct aul_screen_viewer_s *)calloc(1,
+ sizeof(struct aul_screen_viewer_s));
+ if (screen_viewer == NULL) {
+ _E("Out of memory");
+ return AUL_R_EINVAL;
}
- screen_viewer_handler = cbs;
- screen_viewer_data = data;
+ screen_viewer->callback = callback;
+ screen_viewer->type = type;
+ screen_viewer->priv = priv;
+ screen_viewer->ref = __get_ref();
+ screen_viewer->user_data = data;
+
+ if (__screen_viewer_init(screen_viewer) < 0) {
+ __screen_viewer_fini(screen_viewer);
+ free(screen_viewer);
+ return AUL_R_ERROR;
+ }
+ *handle = screen_viewer;
return AUL_R_OK;
}
-API int aul_screen_connector_remove_screen_viewer(void)
+API int aul_screen_connector_remove_screen_viewer(aul_screen_viewer_h handle)
{
- if (__screen_viewer_fini() < 0) {
- _E("Failed to finalize screen viewer");
- return -1;
- }
+ if (handle == NULL)
+ return AUL_R_EINVAL;
- screen_viewer_handler = NULL;
- screen_viewer_data = NULL;
+ __screen_viewer_fini(handle);
+ free(handle);
return AUL_R_OK;
}
-API int aul_screen_connector_add_app_screen(unsigned int surf)
+API int aul_screen_connector_add_app_screen(const char *instance_id,
+ unsigned int surf)
{
int ret;
bundle *b;
return AUL_R_ERROR;
}
+ if (instance_id) {
+ ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add instance id");
+ bundle_free(b);
+ return AUL_R_ERROR;
+ }
+ }
+
ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
- ADD_APP_SCREEN, b, AUL_SOCK_NONE);
+ ADD_APP_SCREEN, b, AUL_SOCK_NOREPLY);
bundle_free(b);
if (ret < 0) {
_E("Failed to add app screen");
return AUL_R_OK;
}
-API int aul_screen_connector_remove_app_screen(void)
+API int aul_screen_connector_remove_app_screen(const char *instance_id)
{
int ret;
bundle *b;
return AUL_R_ERROR;
}
+ if (instance_id) {
+ ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add instance id");
+ bundle_free(b);
+ return AUL_R_ERROR;
+ }
+ }
+
ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
- REMOVE_APP_SCREEN, b, AUL_SOCK_NONE);
+ REMOVE_APP_SCREEN, b, AUL_SOCK_NOREPLY);
bundle_free(b);
if (ret < 0) {
_E("Failed to remove app screen");
return AUL_R_OK;
}
-API int aul_screen_connector_update_app_screen(const char *appid)
+API int aul_screen_connector_send_update_request(const char *appid,
+ const char *instance_id)
{
int ret;
bundle *b;
return AUL_R_ERROR;
}
+ if (instance_id) {
+ ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+ if (ret != BUNDLE_ERROR_NONE) {
+ _E("Failed to add instance id");
+ bundle_free(b);
+ return AUL_R_ERROR;
+ }
+ }
+
ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
- UPDATE_APP_SCREEN, b, AUL_SOCK_NONE);
+ APP_UPDATE_REQUESTED, b, AUL_SOCK_NOREPLY);
bundle_free(b);
if (ret < 0) {
_E("Failed to update app screen");