implementation of app group
[platform/core/appfw/app-core.git] / src / appcore-group.c
1 #include <Elementary.h>
2 #include <stdio.h>
3 #include <glib.h>
4 #include <aul.h>
5 #include <pkgmgr-info.h>
6 #include <bundle_internal.h>
7
8 #include "appcore-internal.h"
9
10 #define APP_SVC_K_LAUNCH_MODE   "__APP_SVC_LAUNCH_MODE__"
11
12 static int __get_top_window(int lpid)
13 {
14         int *gpids;
15         int gcnt;
16         int ret = -1;
17
18         aul_app_group_get_group_pids(lpid, &gcnt, &gpids);
19         if (gcnt > 0) {
20                 ret =  aul_app_group_get_window(gpids[gcnt-1]);
21         }
22
23         if (gpids != NULL)
24                 free(gpids);
25
26         return ret;
27 }
28
29 static gboolean __can_attach_window(bundle *b)
30 {
31         char *str = NULL;
32         char *mode = NULL;
33         char appid[255] = {0, };
34         int ret;
35
36         pkgmgrinfo_appinfo_h handle;
37         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
38         if (ret != AUL_R_OK) {
39                 _ERR("Failed to aul_app_get_appid_bypid()");
40                 return FALSE;
41         }
42
43         ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
44         if (ret != PMINFO_R_OK) {
45                 _ERR("Failed to pkgmgrinfo_appinfo_get_appinfo()");
46                 return FALSE;
47         }
48         ret = pkgmgrinfo_appinfo_get_launch_mode(handle, &mode);
49
50         if (ret != PMINFO_R_OK) {
51                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
52                 _ERR("Failed to pkgmgrinfo_appinfo_get_launch_mode()");
53                 return FALSE;
54         }
55
56         if (mode != NULL && strncmp(mode, "caller", 6) == 0) {
57                 _DBG("launch mode from db is caller");
58
59                 bundle_get_str(b, APP_SVC_K_LAUNCH_MODE, &str);
60                 if (str != NULL && strncmp(str, "group", 5) == 0) {
61                         pkgmgrinfo_appinfo_destroy_appinfo(handle);
62                         return TRUE;
63                 }
64         } else if (mode != NULL && strncmp(mode, "group", 5) == 0) {
65                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
66                 return TRUE;
67         }
68
69         pkgmgrinfo_appinfo_destroy_appinfo(handle);
70
71         return FALSE;
72 }
73
74 void appcore_group_reset(bundle *b)
75 {
76         _DBG("appcore_group_reset");
77 #ifdef X11
78         int wid = appcore_get_main_window();
79 #else
80         int wid = appcore_get_main_surface();
81 #endif
82
83         if (__can_attach_window(b)) {
84                 _DBG("attach!!");
85                 int lpid;
86                 const char *val = NULL;
87                 int caller_pid;
88                 int caller_wid;
89
90                 val = bundle_get_val(b, AUL_K_CALLER_PID);
91
92                 if (val != NULL) {
93                         caller_pid = atoi(val);
94                         lpid = aul_app_group_get_leader_pid(caller_pid);
95
96                         if (lpid != -1) {
97                                 caller_wid = __get_top_window(lpid);
98                         _DBG("lpid %d, getpid() %d, wid %d, caller_wid %d",
99                                         lpid, getpid(), wid, caller_wid);
100                                 aul_app_group_add(lpid, getpid(), wid);
101                                 aul_app_group_attach_window(caller_wid, wid);
102                         } else {
103                                 _ERR("no lpid");
104                                 elm_exit();
105                         }
106                 } else {
107                         _ERR("caller pid is null");
108                 }
109         } else {
110                 int pid = getpid();
111                 aul_app_group_add(pid, pid, wid);
112         }
113 }
114
115 void appcore_group_resume()
116 {
117         _DBG("appcore_group_resume");
118         aul_app_group_clear_top();
119 }
120