4 * Copyright (c) 2014, Intel Corporation.
6 * Contact: Baptiste DURAND <baptiste.durand@open.eurogiciel.org>
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.
26 #include <sys/types.h>
30 #include <pkgmgr-info.h>
34 static GMainLoop *mainloop = NULL;
44 static bundle *_create_internal_bundle(struct launch_arg *args)
49 if (!args->flag_debug && args->argc < 2)
54 bundle_add(b, AUL_K_DEBUG, "1");
56 for (i = 0; i + 1 < args->argc; i += 2) {
57 bundle_add(b, args->argv[i], args->argv[i + 1]);
58 if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
59 !strcmp(args->argv[i + 1], "SYNC"))
66 static int __launch_app_dead_handler(int pid, void *data)
68 int listen_pid = (int)data;
70 if (listen_pid == pid)
71 g_main_loop_quit(mainloop);
76 static gboolean run_func(void *data)
80 struct launch_arg *launch_arg_data = (struct launch_arg *)data;
82 kb = _create_internal_bundle(launch_arg_data);
83 pid = aul_launch_app((char *)launch_arg_data->appid, kb);
91 printf("... successfully launched pid = %d with debug %d\n",
92 pid, launch_arg_data->flag_debug);
93 if (launch_arg_data->sync) {
94 aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)pid);
98 printf("... launch failed\n");
101 g_main_loop_quit(mainloop);
106 static void print_usage(char *program)
108 printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
110 " -h --help Display this usage information.\n"
111 " -l --list Display installed apps list\n"
112 " -S --status Display running apps list\n"
113 " -s [tizen application ID] --start Launch widget with tizen application ID\n"
114 " -k [tizen application ID] --kill Kill widget with tizen application ID\n"
115 " -r [tizen application ID] --is-running Check whether application is running by tizen application ID,\n"
116 " If widget is running, 0(zero) will be returned.\n"
117 " -d --debug Activate debug mode\n"
121 static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
126 if (pkgmgrinfo_appinfo_get_label(handle, &label)) {
127 printf("Failed to get app label\n");
131 if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
132 printf("Failed to get appid\n");
136 printf("\t'%s'\t '%s'\n", label, appid);
141 static int list_app(void)
145 printf("\tApplication List for user %lu\n", (long)getuid());
146 printf("\tUser's Application \n");
147 printf("\t Name \t AppID \n");
148 printf("\t=================================================\n");
149 if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
150 getuid(), NULL) != PMINFO_R_OK)
152 printf("\t=================================================\n");
156 static int __iterfunc_status(const aul_app_info *info, void *data)
158 printf("\t %s (%d)\n", info->appid, info->pid);
162 static int __iterfunc_kill(const aul_app_info *info, void *data)
166 if (strcmp(info->appid, data) == 0) {
167 aul_kill_pid(info->pid);
168 printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
173 static int is_app_installed(char *appid)
175 int is_installed = 0;
176 pkgmgrinfo_appinfo_filter_h filter;
178 if (pkgmgrinfo_appinfo_filter_create(&filter)) {
179 printf("Failed to create filter\n");
183 if (pkgmgrinfo_appinfo_filter_add_string(filter,
184 PMINFO_APPINFO_PROP_APP_ID, appid)) {
185 printf("Failed to add filter string\n");
186 pkgmgrinfo_appinfo_filter_destroy(filter);
190 if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
192 printf("Failed to get filter count\n");
193 pkgmgrinfo_appinfo_filter_destroy(filter);
197 pkgmgrinfo_appinfo_filter_destroy(filter);
202 int main(int argc, char **argv)
204 bool disp_help = false;
205 bool disp_list = false;
206 bool disp_run_list = false;
211 struct launch_arg args;
212 static struct option long_options[] = {
213 { "help", no_argument, 0, 'h' },
214 { "list", no_argument, 0, 'l' },
215 { "status", no_argument, 0, 'S' },
216 { "start", required_argument, 0, 's' },
217 { "args", required_argument, 0, 'a' },
218 { "kill", required_argument, 0, 'k' },
219 { "is-running", required_argument, 0, 'r' },
220 { "debug", no_argument, 0, 'd' },
223 memset(&args, 0, sizeof(struct launch_arg));
226 next_opt = getopt_long(argc,
235 print_usage(argv[0]);
243 printf("Fail to display the list of "
244 "installed applications\n");
252 printf("\t appId (PID)\n");
253 if (aul_app_get_running_app_info(__iterfunc_status,
255 printf("Fail to display the list of "
256 "Running applications\n");
259 disp_run_list = true;
264 if (strlen(optarg) > 255) {
265 print_usage(argv[0]);
268 strcpy(args.appid, optarg);
280 print_usage(argv[0]);
283 } while (next_opt != -1);
286 print_usage(argv[0]);
289 args.argc = argc - optind;
290 args.argv = &argv[optind];
292 if ((op == 's') || (op == 'k') || (op == 'r')) {
293 if (is_app_installed(args.appid) <= 0) {
294 printf("The app with ID: %s is not avaible "
295 "for the user %d \n",
296 args.appid, getuid());
302 if (strlen(args.appid) <= 0) {
303 printf("result: %s\n", "failed");
306 aul_launch_init(NULL, NULL);
307 g_idle_add(run_func, args.appid);
308 mainloop = g_main_loop_new(NULL, FALSE);
310 printf("failed to create glib main loop\n");
313 g_main_loop_run(mainloop);
315 } else if (op == 'k') {
316 is_running = aul_app_is_running(args.appid);
317 if (true == is_running) {
318 aul_app_get_running_app_info(__iterfunc_kill,
321 printf("result: %s\n", "App isn't running");
324 } else if (op == 'r') {
325 is_running = aul_app_is_running(args.appid);
326 if (true == is_running) {
327 printf("result: %s\n", "running");
330 printf("result: %s\n", "not running");