2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
25 #include <bundle_internal.h>
31 #include "aul_app_com.h"
32 #include "aul_screen_connector.h"
34 struct aul_screen_viewer_s {
35 aul_app_com_connection_h conn;
36 aul_screen_viewer_cb callback;
37 aul_screen_type_e type;
43 static unsigned int ref;
45 static unsigned int __get_ref(void)
50 static int __add_screen_viewer(int type, bool priv, unsigned int ref)
54 char buf[MAX_PID_STR_BUFSZ];
62 snprintf(buf, sizeof(buf), "%d", type);
63 ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
64 if (ret != BUNDLE_ERROR_NONE) {
65 _E("Failed to add screen type(%d)", type);
70 snprintf(buf, sizeof(buf), "%u", ref);
71 ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
72 if (ret != BUNDLE_ERROR_NONE) {
73 _E("Failed to add viewer reference(%u)", ref);
79 ret = bundle_add(b, AUL_K_PRIVATE, "true");
80 if (ret != BUNDLE_ERROR_NONE) {
81 _E("Failed to add bundle data");
87 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
88 ADD_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
96 static int __remove_screen_viewer(int type, bool priv, unsigned int ref)
100 char buf[MAX_PID_STR_BUFSZ];
108 snprintf(buf, sizeof(buf), "%d", type);
109 ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
110 if (ret != BUNDLE_ERROR_NONE) {
111 _E("Failed to add view mode");
116 snprintf(buf, sizeof(buf), "%u", ref);
117 ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
118 if (ret != BUNDLE_ERROR_NONE) {
119 _E("Failed to add viewer reference(%u)", ref);
125 ret = bundle_add(b, AUL_K_PRIVATE, "true");
126 if (ret != BUNDLE_ERROR_NONE) {
127 _E("Failed to add bundle data");
133 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
134 REMOVE_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
142 static int __app_screen_event_cb(const char *endpoint, aul_app_com_result_e res,
143 bundle *envelope, void *user_data)
145 aul_screen_viewer_h handle = (aul_screen_viewer_h)user_data;
147 char *instance_id = NULL;
148 unsigned int *surf = NULL;
152 aul_screen_connector_event_type_e event_type;
154 bundle_get_str(envelope, "__AUL_SC_EVENT__", &event);
156 _E("Failed to get screen connector event");
158 } else if (strcmp(event, "add_screen") == 0) {
159 event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_ADD;
160 } else if (strcmp(event, "remove_screen") == 0) {
161 event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_REMOVE;
162 } else if (strcmp(event, "update_screen") == 0) {
163 event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_UPDATE;
164 } else if (strcmp(event, "focus_screen") == 0) {
165 event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_FOCUS;
167 _E("Unknown event type(%s)", event);
171 bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
173 _E("Failed to get appid");
177 bundle_get_byte(envelope, "__AUL_SC_SURFACE__",
178 (void **)&surf, &size);
180 _E("Failed to get surface");
184 bundle_get_byte(envelope, "__AUL_SC_PID__", (void **)&pid, &size);
186 _E("Failed to get pid");
189 bundle_get_str(envelope, "__AUL_SC_INSTANCE_ID__", &instance_id);
191 if (handle->callback) {
192 handle->callback(appid, instance_id, *pid, *surf,
193 event_type, handle->user_data);
195 _D("appid(%s), instance_id(%s), pid(%d), surface(%d), event_type(%d)",
196 appid, instance_id, *pid, *surf, event_type);
201 static int __screen_viewer_fini(aul_screen_viewer_h screen_viewer)
205 if (screen_viewer->conn) {
206 aul_app_com_leave(screen_viewer->conn);
207 screen_viewer->conn = NULL;
210 ret = __remove_screen_viewer(screen_viewer->type, screen_viewer->priv,
213 _E("Failed to remove screen watcher");
220 static int __screen_viewer_init(aul_screen_viewer_h screen_viewer)
224 pid_t pid = getpid();
226 snprintf(endpoint, sizeof(endpoint), "app_screen_event:%u:%d",
227 screen_viewer->ref, pid);
228 aul_app_com_create(endpoint, NULL, __app_screen_event_cb,
229 screen_viewer, &screen_viewer->conn);
230 if (screen_viewer->conn == NULL) {
231 _E("Failed to create app com");
235 ret = __add_screen_viewer(screen_viewer->type, screen_viewer->priv,
238 _E("Failed to add screen watcher");
245 API int aul_screen_connector_add_screen_viewer(aul_screen_viewer_cb callback,
246 aul_screen_type_e type, bool priv,
247 void *data, aul_screen_viewer_h *handle)
249 struct aul_screen_viewer_s *screen_viewer;
251 if (handle == NULL || callback == NULL ||
252 !(type & AUL_SCREEN_TYPE_ALL)) {
253 _E("Invalid parameter");
257 screen_viewer = (struct aul_screen_viewer_s *)calloc(1,
258 sizeof(struct aul_screen_viewer_s));
259 if (screen_viewer == NULL) {
264 screen_viewer->callback = callback;
265 screen_viewer->type = type;
266 screen_viewer->priv = priv;
267 screen_viewer->ref = __get_ref();
268 screen_viewer->user_data = data;
270 if (__screen_viewer_init(screen_viewer) < 0) {
271 __screen_viewer_fini(screen_viewer);
275 *handle = screen_viewer;
280 API int aul_screen_connector_remove_screen_viewer(aul_screen_viewer_h handle)
285 __screen_viewer_fini(handle);
291 API int aul_screen_connector_add_app_screen(const char *instance_id,
296 char buf[MAX_PID_STR_BUFSZ];
304 snprintf(buf, sizeof(buf), "%d", surf);
305 ret = bundle_add(b, AUL_K_WID, buf);
306 if (ret != BUNDLE_ERROR_NONE) {
307 _E("Failed to add surf");
313 ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
314 if (ret != BUNDLE_ERROR_NONE) {
315 _E("Failed to add instance id");
321 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
322 ADD_APP_SCREEN, b, AUL_SOCK_NOREPLY);
325 _E("Failed to add app screen");
332 API int aul_screen_connector_remove_app_screen(const char *instance_id)
344 ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
345 if (ret != BUNDLE_ERROR_NONE) {
346 _E("Failed to add instance id");
352 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
353 REMOVE_APP_SCREEN, b, AUL_SOCK_NOREPLY);
356 _E("Failed to remove app screen");
363 API int aul_screen_connector_send_update_request(const char *appid,
364 const char *instance_id)
370 _E("Invalid parameter");
380 ret = bundle_add(b, AUL_K_APPID, appid);
381 if (ret != BUNDLE_ERROR_NONE) {
382 _E("Failed to add appid");
388 ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
389 if (ret != BUNDLE_ERROR_NONE) {
390 _E("Failed to add instance id");
396 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
397 APP_UPDATE_REQUESTED, b, AUL_SOCK_NOREPLY);
400 _E("Failed to update app screen");
407 static bundle *__send_request_with_surface_id(int cmd, unsigned int surface_id)
409 app_pkt_t *pkt = NULL;
420 r = bundle_add_byte(b, "__AUL_SC_SURFACE__",
421 &surface_id, sizeof(unsigned int));
422 if (r != BUNDLE_ERROR_NONE) {
423 _E("Failed to add surface id(%u)", surface_id);
428 fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), cmd, b,
432 _E("Failed to send request(%d)", cmd);
436 aul_sock_recv_reply_pkt(fd, &pkt);
438 _E("Failed to receive the packet");
442 b = bundle_decode(pkt->data, pkt->len);
445 _E("Failed to decode bundle data");
452 API int aul_screen_connector_get_appid_by_surface_id(unsigned int surface_id,
459 _E("Invalid parameter");
463 b = __send_request_with_surface_id(APP_GET_APPID_BY_SURFACE_ID,
468 val = bundle_get_val(b, AUL_K_APPID);
470 _E("Failed to get appid");
475 *appid = strdup(val);
476 if (*appid == NULL) {
486 API int aul_screen_connector_get_instance_id_by_surface_id(
487 unsigned int surface_id, char **instance_id)
492 if (instance_id == NULL) {
493 _E("Invalid parameter");
497 b = __send_request_with_surface_id(APP_GET_INSTANCE_ID_BY_SURFACE_ID,
502 val = bundle_get_val(b, AUL_K_INSTANCE_ID);
504 _E("Failed to get instance id");
509 *instance_id = strdup(val);
510 if (*instance_id == NULL) {
521 API int aul_screen_connector_update_screen_viewer_status(
522 aul_screen_status_e status, unsigned int provider_surf)
528 if (status < AUL_SCREEN_STATUS_RESUME ||
529 status > AUL_SCREEN_STATUS_PAUSE) {
530 _E("Invalid parameter");
540 snprintf(buf, sizeof(buf), "%u", provider_surf);
541 ret = bundle_add(b, AUL_K_WID, buf);
542 if (ret != BUNDLE_ERROR_NONE) {
543 _E("Failed to add provider surface id");
548 snprintf(buf, sizeof(buf), "%d", status);
549 ret = bundle_add(b, "__AUL_SC_VIEWER_STATUS__", buf);
550 if (ret != BUNDLE_ERROR_NONE) {
551 _E("Failed to add screen status");
556 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
557 UPDATE_SCREEN_VIEWER_STATUS, b, AUL_SOCK_NOREPLY);
563 API int aul_screen_connector_trigger_focused_force(void)
566 char buf[MAX_PID_STR_BUFSZ];
575 snprintf(buf, sizeof(buf), "%d", AUL_SCREEN_TYPE_UI);
576 ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
577 if (ret != BUNDLE_ERROR_NONE) {
578 _E("Failed to add view mode");
583 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
584 TRIGGER_APP_SCREEN_FOCUSED_FORCE, b, AUL_SOCK_NOREPLY);