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>
36 static GMainLoop *mainloop = NULL;
46 static bundle *_create_internal_bundle(struct launch_arg *args)
51 if (!args->flag_debug && args->argc < 2)
56 bundle_add(b, AUL_K_DEBUG, "1");
58 for (i = 0; i + 1 < args->argc; i += 2) {
59 bundle_add(b, args->argv[i], args->argv[i + 1]);
60 if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
61 !strcmp(args->argv[i + 1], "SYNC"))
68 static int __launch_app_dead_handler(int pid, void *data)
70 int listen_pid = (intptr_t)data;
72 if (listen_pid == pid)
73 g_main_loop_quit(mainloop);
78 static gboolean run_func(void *data)
82 struct launch_arg *launch_arg_data = (struct launch_arg *)data;
84 kb = _create_internal_bundle(launch_arg_data);
85 pid = aul_launch_app((char *)launch_arg_data->appid, kb);
93 printf("... successfully launched pid = %d with debug %d\n",
94 pid, launch_arg_data->flag_debug);
95 if (launch_arg_data->sync) {
96 aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)(intptr_t)pid);
100 printf("... launch failed\n");
103 g_main_loop_quit(mainloop);
108 static void print_usage(char *program)
110 printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
112 " -h --help Display this usage information.\n"
113 " -l --list Display installed apps list\n"
114 " -S --status Display running apps list\n"
115 " -s [tizen application ID] --start Launch widget with tizen application ID\n"
116 " -k [tizen application ID] --kill Kill widget with tizen application ID\n"
117 " -t [tizen application ID] --terminate Terminate widget with tizen application ID\n"
118 " -r [tizen application ID] --is-running Check whether application is running by tizen application ID,\n"
119 " If widget is running, 0(zero) will be returned.\n"
120 " -d --debug Activate debug mode\n"
124 static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
129 if (pkgmgrinfo_appinfo_get_label(handle, &label))
132 if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
133 printf("Failed to get appid\n");
137 printf("\t'%s'\t '%s'\n", label, appid);
142 static int list_app(void)
146 printf("\tApplication List for user %lu\n", (long)getuid());
147 printf("\tUser's Application \n");
148 printf("\t Name \t AppID \n");
149 printf("\t=================================================\n");
150 if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
151 getuid(), NULL) != PMINFO_R_OK)
153 printf("\t=================================================\n");
157 static int __iterfunc_status(const aul_app_info *info, void *data)
159 printf("\t %s (%d)\n", info->appid, info->pid);
163 static int __iterfunc_kill(const aul_app_info *info, void *data)
167 if (strcmp(info->appid, data) == 0) {
168 aul_kill_pid(info->pid);
169 printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
174 static int __iterfunc_term(const aul_app_info *info, void *data)
178 if (strcmp(info->appid, data) == 0) {
179 aul_terminate_pid(info->pid);
180 printf("\t Terminate appId: %s (%d)\n", info->appid, info->pid);
185 static int is_app_installed(char *appid)
187 int is_installed = 0;
188 pkgmgrinfo_appinfo_filter_h filter;
190 if (pkgmgrinfo_appinfo_filter_create(&filter)) {
191 printf("Failed to create filter\n");
195 if (pkgmgrinfo_appinfo_filter_add_string(filter,
196 PMINFO_APPINFO_PROP_APP_ID, appid)) {
197 printf("Failed to add filter string\n");
198 pkgmgrinfo_appinfo_filter_destroy(filter);
202 if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
204 printf("Failed to get filter count\n");
205 pkgmgrinfo_appinfo_filter_destroy(filter);
209 pkgmgrinfo_appinfo_filter_destroy(filter);
214 int main(int argc, char **argv)
216 bool disp_help = false;
217 bool disp_list = false;
218 bool disp_run_list = false;
223 struct launch_arg args;
224 static struct option long_options[] = {
225 { "help", no_argument, 0, 'h' },
226 { "list", no_argument, 0, 'l' },
227 { "status", no_argument, 0, 'S' },
228 { "start", required_argument, 0, 's' },
229 { "args", required_argument, 0, 'a' },
230 { "kill", required_argument, 0, 'k' },
231 { "terminate", required_argument, 0, 't' },
232 { "is-running", required_argument, 0, 'r' },
233 { "debug", no_argument, 0, 'd' },
236 memset(&args, 0, sizeof(struct launch_arg));
239 next_opt = getopt_long(argc,
248 print_usage(argv[0]);
256 printf("Fail to display the list of "
257 "installed applications\n");
265 printf("\t appId (PID)\n");
266 if (aul_app_get_running_app_info(__iterfunc_status,
268 printf("Fail to display the list of "
269 "Running applications\n");
272 disp_run_list = true;
278 if (strlen(optarg) > 255) {
279 print_usage(argv[0]);
282 strncpy(args.appid, optarg, sizeof(args.appid) - 1);
294 print_usage(argv[0]);
297 } while (next_opt != -1);
300 print_usage(argv[0]);
303 args.argc = argc - optind;
304 args.argv = &argv[optind];
306 if ((op == 's') || (op == 'k') || (op == 'r')) {
307 if (is_app_installed(args.appid) <= 0) {
308 printf("The app with ID: %s is not avaible "
309 "for the user %d \n",
310 args.appid, getuid());
316 if (strlen(args.appid) <= 0) {
317 printf("result: %s\n", "failed");
320 aul_launch_init(NULL, NULL);
321 g_idle_add(run_func, args.appid);
322 mainloop = g_main_loop_new(NULL, FALSE);
324 printf("failed to create glib main loop\n");
327 g_main_loop_run(mainloop);
329 } else if (op == 'k') {
330 is_running = aul_app_is_running(args.appid);
331 if (true == is_running) {
332 aul_app_get_running_app_info(__iterfunc_kill,
335 printf("result: %s\n", "App isn't running");
338 } else if (op == 't') {
339 is_running = aul_app_is_running(args.appid);
340 if (true == is_running) {
341 aul_app_get_running_app_info(__iterfunc_term,
344 printf("result: %s\n", "App isn't running");
347 } else if (op == 'r') {
348 is_running = aul_app_is_running(args.appid);
349 if (true == is_running) {
350 printf("result: %s\n", "running");
353 printf("result: %s\n", "not running");