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.
27 #include <sys/types.h>
31 #include <pkgmgr-info.h>
33 #include <bundle_internal.h>
37 static GMainLoop *mainloop = NULL;
47 static bundle *_create_internal_bundle(struct launch_arg *args)
52 if (!args->flag_debug && args->argc < 2)
57 bundle_add(b, AUL_K_DEBUG, "1");
59 for (i = 0; i + 1 < args->argc; i += 2) {
60 bundle_add(b, args->argv[i], args->argv[i + 1]);
61 if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
62 !strcmp(args->argv[i + 1], "SYNC"))
69 static int __launch_app_dead_handler(int pid, void *data)
71 int listen_pid = (intptr_t)data;
73 if (listen_pid == pid)
74 g_main_loop_quit(mainloop);
79 static gboolean run_func(void *data)
83 struct launch_arg *launch_arg_data = (struct launch_arg *)data;
85 kb = _create_internal_bundle(launch_arg_data);
86 pid = aul_launch_app((char *)launch_arg_data->appid, kb);
94 printf("... successfully launched pid = %d with debug %d\n",
95 pid, launch_arg_data->flag_debug);
96 if (launch_arg_data->sync) {
97 aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)(intptr_t)pid);
101 printf("... launch failed\n");
104 g_main_loop_quit(mainloop);
109 static void print_usage(char *program)
111 printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
113 " -h --help Display this usage information.\n"
114 " -l --list Display installed apps list\n"
115 " -S --status Display running apps list\n"
116 " -s [tizen application ID] --start Launch widget with tizen application ID\n"
117 " -k [tizen application ID] --kill Kill widget with tizen application ID\n"
118 " -t [tizen application ID] --terminate Terminate widget with tizen application ID\n"
119 " -r [tizen application ID] --is-running Check whether application is running by tizen application ID,\n"
120 " If widget is running, 0(zero) will be returned.\n"
121 " -d --debug Activate debug mode\n"
125 static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
130 if (pkgmgrinfo_appinfo_get_label(handle, &label))
133 if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
134 printf("Failed to get appid\n");
138 printf("\t'%s'\t '%s'\n", label, appid);
143 static int list_app(void)
147 printf("\tApplication List for user %lu\n", (long)getuid());
148 printf("\tUser's Application \n");
149 printf("\t Name \t AppID \n");
150 printf("\t=================================================\n");
151 if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
152 getuid(), NULL) != PMINFO_R_OK)
154 printf("\t=================================================\n");
158 static int __iterfunc_status(const aul_app_info *info, void *data)
160 printf("\t %s (%d)\n", info->appid, info->pid);
164 static int __iterfunc_kill(const aul_app_info *info, void *data)
168 if (strcmp(info->appid, data) == 0) {
169 aul_kill_pid(info->pid);
170 printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
175 static int __iterfunc_term(const aul_app_info *info, void *data)
179 if (strcmp(info->appid, data) == 0) {
180 aul_terminate_pid(info->pid);
181 printf("\t Terminate appId: %s (%d)\n", info->appid, info->pid);
186 static int is_app_installed(char *appid)
188 int is_installed = 0;
189 pkgmgrinfo_appinfo_filter_h filter;
191 if (pkgmgrinfo_appinfo_filter_create(&filter)) {
192 printf("Failed to create filter\n");
196 if (pkgmgrinfo_appinfo_filter_add_string(filter,
197 PMINFO_APPINFO_PROP_APP_ID, appid)) {
198 printf("Failed to add filter string\n");
199 pkgmgrinfo_appinfo_filter_destroy(filter);
203 if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
205 printf("Failed to get filter count\n");
206 pkgmgrinfo_appinfo_filter_destroy(filter);
210 pkgmgrinfo_appinfo_filter_destroy(filter);
215 int main(int argc, char **argv)
217 bool disp_help = false;
218 bool disp_list = false;
219 bool disp_run_list = false;
224 struct launch_arg args;
225 static struct option long_options[] = {
226 { "help", no_argument, 0, 'h' },
227 { "list", no_argument, 0, 'l' },
228 { "status", no_argument, 0, 'S' },
229 { "start", required_argument, 0, 's' },
230 { "args", required_argument, 0, 'a' },
231 { "kill", required_argument, 0, 'k' },
232 { "terminate", required_argument, 0, 't' },
233 { "is-running", required_argument, 0, 'r' },
234 { "debug", no_argument, 0, 'd' },
237 memset(&args, 0, sizeof(struct launch_arg));
240 next_opt = getopt_long(argc,
249 print_usage(argv[0]);
257 printf("Fail to display the list of "
258 "installed applications\n");
266 printf("\t appId (PID)\n");
267 if (aul_app_get_running_app_info(__iterfunc_status,
269 printf("Fail to display the list of "
270 "Running applications\n");
273 disp_run_list = true;
279 if (strlen(optarg) > 255) {
280 print_usage(argv[0]);
283 strncpy(args.appid, optarg, sizeof(args.appid) - 1);
295 print_usage(argv[0]);
298 } while (next_opt != -1);
301 print_usage(argv[0]);
304 args.argc = argc - optind;
305 args.argv = &argv[optind];
307 if ((op == 's') || (op == 'k') || (op == 'r')) {
308 if (is_app_installed(args.appid) <= 0) {
309 printf("The app with ID: %s is not avaible "
310 "for the user %d \n",
311 args.appid, getuid());
317 if (strlen(args.appid) <= 0) {
318 printf("result: %s\n", "failed");
321 aul_launch_init(NULL, NULL);
322 g_idle_add(run_func, args.appid);
323 mainloop = g_main_loop_new(NULL, FALSE);
325 printf("failed to create glib main loop\n");
328 g_main_loop_run(mainloop);
330 } else if (op == 'k') {
331 is_running = aul_app_is_running(args.appid);
332 if (true == is_running) {
333 aul_app_get_running_app_info(__iterfunc_kill,
336 printf("result: %s\n", "App isn't running");
339 } else if (op == 't') {
340 is_running = aul_app_is_running(args.appid);
341 if (true == is_running) {
342 aul_app_get_running_app_info(__iterfunc_term,
345 printf("result: %s\n", "App isn't running");
348 } else if (op == 'r') {
349 is_running = aul_app_is_running(args.appid);
350 if (true == is_running) {
351 printf("result: %s\n", "running");
354 printf("result: %s\n", "not running");