4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <app-checker-server.h>
29 #include <sys/types.h>
39 #include "simple_util.h"
42 #include "menu_db_util.h"
45 #include <Ecore_Input.h>
52 Ecore_Event_Handler *key_up;
53 Ecore_Event_Handler *key_down;
60 GSList *key_pid_list = NULL;
62 extern int app_send_cmd(int pid, int cmd, bundle *kb);
64 static gboolean __add_history_handler(gpointer user_data)
68 app_pkt_t *pkt = (app_pkt_t *)user_data;
69 struct history_data *hd = (struct history_data *)pkt->data;
73 memset(&rec, 0, sizeof(rec));
75 rec.pkg_name = hd->pkg_name;
76 rec.app_path = hd->app_path;
79 rec.arg = (char *)hd->data;
82 _D("add rua history %s %s", rec.pkg_name, rec.app_path);
84 ret = rua_add_history(&rec);
86 _D("rua add history error");
95 int __send_result_to_client(int fd, int res)
97 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
99 _E("send failed due to EPIPE.\n");
100 _E("send fail to client");
106 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
108 app_info_from_db *menu_info;
111 r_info = (char *)priv;
113 if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
116 strncat(r_info, dname, 8);
117 strncat(r_info, ":", 1);
118 strncat(r_info, _get_pkgname(menu_info), MAX_PACKAGE_STR_SIZE);
119 strncat(r_info, ":", 1);
120 strncat(r_info, _get_app_path(menu_info), MAX_PACKAGE_APP_PATH_SIZE);
121 strncat(r_info, ";", 1);
125 if (menu_info != NULL)
126 _free_app_info_from_db(menu_info);
130 int __send_running_appinfo(int fd)
132 app_pkt_t *pkt = NULL;
135 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
141 memset(pkt, 0, AUL_SOCK_MAXBUFF);
143 __proc_iter_cmdline(__get_pkginfo, pkt->data);
145 pkt->cmd = APP_RUNNING_INFO_RESULT;
146 pkt->len = strlen((char *)pkt->data) + 1;
148 if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
150 _E("send failed due to EPIPE.\n");
151 _E("send fail to client");
161 int __app_is_running(const char *pkgname)
163 char *apppath = NULL;
164 ail_appinfo_h handle;
173 ail_ret = ail_package_get_appinfo(pkgname, &handle);
174 if (ail_ret != AIL_ERROR_OK) {
175 _E("ail_get_appinfo with %s failed", pkgname);
179 ail_ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &apppath);
180 if (ail_ret != AIL_ERROR_OK) {
181 _E("ail_appinfo_get_str failed");
188 /*truncate apppath if it includes default bundles */
189 while (apppath[i] != 0) {
190 if (apppath[i] == ' ' || apppath[i] == '\t') {
197 if (__proc_iter_cmdline(NULL, apppath) > 0)
203 if (ail_destroy_appinfo(handle) != AIL_ERROR_OK)
204 _E("ail_destroy_rs failed");
208 static int __register_key_event(int pid)
213 pid_data = malloc(sizeof(int));
216 key_pid_list = g_slist_prepend(key_pid_list, pid_data);
218 _D("===key stack===");
220 for (entry = key_pid_list; entry; entry = entry->next) {
222 pid_data = (int *) entry->data;
223 _D("pid : %d",*pid_data);
230 static int __unregister_key_event(int pid)
235 for (entry = key_pid_list; entry; entry = entry->next) {
237 pid_data = (int *) entry->data;
238 if(pid == *pid_data) {
239 key_pid_list = g_slist_remove(key_pid_list, entry->data);
245 _D("===key stack===");
247 for (entry = key_pid_list; entry; entry = entry->next) {
249 pid_data = (int *) entry->data;
250 _D("pid : %d",*pid_data);
257 static gboolean __util_handler(gpointer data)
259 GPollFD *gpollfd = (GPollFD *) data;
260 int fd = gpollfd->fd;
264 struct history_data *hd;
266 char pkgname[MAX_PACKAGE_STR_SIZE];
268 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
274 case APP_ADD_HISTORY:
275 hd = (struct history_data *)pkt->data;
276 _D("cmd : %d, pkgname : %s, app_path : %s", pkt->cmd, hd->pkg_name, hd->app_path);
277 __send_result_to_client(clifd, 0);
278 g_timeout_add(1000, __add_history_handler, pkt);
280 case APP_RUNNING_INFO:
281 __send_running_appinfo(clifd);
285 strncpy(pkgname, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
286 ret = __app_is_running(pkgname);
287 __send_result_to_client(clifd, ret);
290 case APP_KEY_RESERVE:
291 ret = __register_key_event(cr.pid);
292 __send_result_to_client(clifd, ret);
295 case APP_KEY_RELEASE:
296 ret = __unregister_key_event(cr.pid);
297 __send_result_to_client(clifd, ret);
301 _E("no support packet");
307 static gboolean __au_glib_check(GSource *src)
312 fd_list = src->poll_fds;
314 tmp = (GPollFD *) fd_list->data;
315 if ((tmp->revents & (POLLIN | POLLPRI)))
317 fd_list = fd_list->next;
323 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
330 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
335 static GSourceFuncs funcs = {
336 .prepare = __au_glib_prepare,
337 .check = __au_glib_check,
338 .dispatch = __au_glib_dispatch,
342 static Eina_Bool _key_release_cb(void *data, int type, void *event)
344 Evas_Event_Key_Up *ev = event;
353 _D("Invalid event object");
354 return ECORE_CALLBACK_RENEW;
357 entry = key_pid_list;
358 if (entry && entry->data) {
359 pid_data = (int *) entry->data;
361 kb = bundle_create();
362 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
363 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_RELEASED);
365 ret = app_send_cmd(*pid_data, APP_KEY_EVENT, kb);
370 return ECORE_CALLBACK_RENEW;
374 static Eina_Bool _key_press_cb(void *data, int type, void *event)
376 Evas_Event_Key_Down *ev = event;
385 _D("Invalid event object");
386 return ECORE_CALLBACK_RENEW;
389 entry = key_pid_list;
390 if (entry && entry->data) {
391 pid_data = (int *) entry->data;
393 kb = bundle_create();
394 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
395 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_PRESSED);
397 ret = app_send_cmd(*pid_data, APP_KEY_EVENT, kb);
402 return ECORE_CALLBACK_RENEW;
405 static int __app_dead_handler(int pid, void *data)
409 ret = __unregister_key_event(pid);
414 static void __ac_key_initailize()
416 key_info.win = ecore_x_window_input_new(0, 0, 0, 1, 1);
418 _D("Failed to create hidden window");
421 ecore_x_icccm_title_set(key_info.win, "acdaemon,key,receiver");
422 ecore_x_netwm_name_set(key_info.win, "acdaemon,key,receiver");
423 ecore_x_netwm_pid_set(key_info.win, getpid());
425 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PLAYCD, EXCLUSIVE_GRAB);
426 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_STOPCD, EXCLUSIVE_GRAB);
427 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PAUSECD, EXCLUSIVE_GRAB);
428 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_NEXTSONG, EXCLUSIVE_GRAB);
429 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PREVIOUSSONG, EXCLUSIVE_GRAB);
430 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_REWIND, EXCLUSIVE_GRAB);
431 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_FASTFORWARD, EXCLUSIVE_GRAB);
433 key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
434 if (!key_info.key_up) {
435 _D("Failed to register a key up event handler");
438 key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
439 if (!key_info.key_down) {
440 _D("Failed to register a key down event handler");
443 aul_listen_app_dead_signal(__app_dead_handler, NULL);
446 static int __initialize()
453 fd = __create_server_sock(AUL_UTIL_PID);
455 src = g_source_new(&funcs, sizeof(GSource));
457 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
458 gpollfd->events = POLLIN;
461 g_source_add_poll(src, gpollfd);
462 g_source_set_callback(src, (GSourceFunc) __util_handler,
463 (gpointer) gpollfd, NULL);
464 g_source_set_priority(src, G_PRIORITY_DEFAULT);
466 r = g_source_attach(src, NULL);
469 /* TODO: error handle*/
474 __ac_key_initailize();
480 int main(int argc, char *argv[])
489 ret = ac_server_initailize();
491 ret = __initialize();
493 ecore_main_loop_begin();