summaryrefslogtreecommitdiff
path: root/app_launcher.c
blob: 154924df954459d56f601a9b5501a5dd7a472275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 *  app_launcher
 *
 * Copyright (c) 2014, Intel Corporation.
 *
 * Contact: Baptiste DURAND <baptiste.durand@open.eurogiciel.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <glib.h>

#include <pkgmgr-info.h>
#include <bundle.h>

#include "aul.h"

static GMainLoop *mainloop = NULL;

struct launch_arg {
	char appid[256];
	char **argv;
	int argc;
	int flag_debug;
	int sync;
} launch_arg;

static bundle *_create_internal_bundle(struct launch_arg *args)
{
	bundle *b;
	int i;

	if (!args->flag_debug && args->argc < 2)
		return NULL;

	b = bundle_create();
	if (args->flag_debug)
		bundle_add(b, AUL_K_DEBUG, "1");

	for (i = 0; i + 1 < args->argc; i += 2) {
		bundle_add(b, args->argv[i], args->argv[i + 1]);
		if (!strcmp(args->argv[i], "__LAUNCH_APP_MODE__") &&
				!strcmp(args->argv[i + 1], "SYNC"))
			args->sync = 1;
	}

	return b;
}

static int __launch_app_dead_handler(int pid, void *data)
{
	int listen_pid = (int)data;

	if (listen_pid == pid)
		g_main_loop_quit(mainloop);

	return 0;
}

static gboolean run_func(void *data)
{
	int pid;
	bundle *kb;
	struct launch_arg *launch_arg_data = (struct launch_arg *)data;

	kb = _create_internal_bundle(launch_arg_data);
	pid = aul_launch_app((char *)launch_arg_data->appid, kb);

	if (kb) {
		bundle_free(kb);
		kb = NULL;
	}

	if (pid > 0) {
		printf("... successfully launched pid = %d with debug %d\n",
				pid, launch_arg_data->flag_debug);
		if (launch_arg_data->sync) {
			aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)pid);
			return FALSE;
		}
	} else {
		printf("... launch failed\n");
	}

	g_main_loop_quit(mainloop);

	return FALSE;
}

static void print_usage(char *program)
{
	printf("Usage : %s [ OPTIONS... ] [ ARGS... ]\n", program);
	printf(
			"   -h                        --help              Display this usage information.\n"
			"   -l                        --list              Display installed apps list\n"
			"   -S                        --status            Display running apps list\n"
			"   -s [tizen application ID] --start             Launch widget with tizen application ID\n"
			"   -k [tizen application ID] --kill              Kill widget with tizen application ID\n"
			"   -r [tizen application ID] --is-running        Check whether application is running by tizen application ID,\n"
			"                                                 If widget is running, 0(zero) will be returned.\n"
			"   -d                        --debug             Activate debug mode\n"
	      );
}

static int __appinfo_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
{
	char *label;
	char *appid;

	if (pkgmgrinfo_appinfo_get_label(handle, &label))
		label = "";

	if (pkgmgrinfo_appinfo_get_appid(handle, &appid)) {
		printf("Failed to get appid\n");
		return -1;
	}

	printf("\t'%s'\t '%s'\n", label, appid);

	return 0;
}

static int list_app(void)
{
	int ret = 0;

	printf("\tApplication List for user %lu\n", (long)getuid());
	printf("\tUser's Application \n");
	printf("\t Name \t AppID \n");
	printf("\t=================================================\n");
	if (pkgmgrinfo_appinfo_get_usr_installed_list(__appinfo_list_cb,
				getuid(), NULL) != PMINFO_R_OK)
		ret = -1;
	printf("\t=================================================\n");
	return ret;
}

static int __iterfunc_status(const aul_app_info *info, void *data)
{
	printf("\t  %s (%d)\n", info->appid, info->pid);
	return 0;
}

static int __iterfunc_kill(const aul_app_info *info, void *data)
{
	if (!data)
		return 0;
	if (strcmp(info->appid, data) == 0) {
		aul_kill_pid(info->pid);
		printf("\t Kill appId: %s (%d)\n", info->appid, info->pid);
	}
	return 0;
}

static int is_app_installed(char *appid)
{
	int is_installed = 0;
	pkgmgrinfo_appinfo_filter_h filter;

	if (pkgmgrinfo_appinfo_filter_create(&filter)) {
		printf("Failed to create filter\n");
		return -1;
	}

	if (pkgmgrinfo_appinfo_filter_add_string(filter,
				PMINFO_APPINFO_PROP_APP_ID, appid)) {
		printf("Failed to add filter string\n");
		pkgmgrinfo_appinfo_filter_destroy(filter);
		return -1;
	}

	if (pkgmgrinfo_appinfo_usr_filter_count(filter, &is_installed,
				getuid())) {
		printf("Failed to get filter count\n");
		pkgmgrinfo_appinfo_filter_destroy(filter);
		return -1;
	}

	pkgmgrinfo_appinfo_filter_destroy(filter);

	return is_installed;
}

int main(int argc, char **argv)
{
	bool disp_help = false;
	bool disp_list = false;
	bool disp_run_list = false;
	bool is_running;
	int next_opt;
	int opt_idx = 0;
	char op = '\0';
	struct launch_arg args;
	static struct option long_options[] = {
		{ "help", no_argument, 0, 'h' },
		{ "list", no_argument, 0, 'l' },
		{ "status", no_argument, 0, 'S' },
		{ "start", required_argument, 0, 's' },
		{ "args", required_argument, 0, 'a' },
		{ "kill", required_argument, 0, 'k' },
		{ "is-running", required_argument, 0, 'r' },
		{ "debug", no_argument, 0, 'd' },
		{ 0, 0, 0, 0 }
	};
	memset(&args, 0, sizeof(struct launch_arg));

	do {
		next_opt = getopt_long(argc,
				argv,
				"hlSs:k:r:d",
				long_options,
				&opt_idx);

		switch (next_opt) {
		case 'h':
			if (!disp_help) {
				print_usage(argv[0]);
				disp_help = true;
			}
			break;
		case 'l':
			if (disp_list)
				break;
			if (list_app()) {
				printf("Fail to display the list of "
						"installed applications\n");
				return -1;
			}
			disp_list = true;
			break;
		case 'S':
			if (disp_run_list)
				break;
			printf("\t appId (PID)\n");
			if (aul_app_get_running_app_info(__iterfunc_status,
						NULL)) {
				printf("Fail to display the list of "
						"Running applications\n");
				return -1;
			}
			disp_run_list = true;
			break;
		case 's':
		case 'k':
		case 'r':
			if (strlen(optarg) > 255) {
				print_usage(argv[0]);
				return -1;
			} else {
				strcpy(args.appid, optarg);
			}
			op = next_opt;
			break;
		case 'd':
			args.flag_debug = 1;
			break;
		case '?':
			break;
		case -1:
			break;
		default:
			print_usage(argv[0]);
			break;
		}
	} while (next_opt != -1);

	if (argc == 1)
		print_usage(argv[0]);

	if (optind < argc) {
		args.argc = argc - optind;
		args.argv = &argv[optind];
	}
	if ((op == 's') || (op == 'k') || (op == 'r')) {
		if (is_app_installed(args.appid) <= 0) {
			printf("The app with ID: %s is not avaible "
					"for the user %d \n",
					args.appid, getuid());
			return -1;
		}
	}

	if (op == 's') {
		if (strlen(args.appid) <= 0) {
			printf("result: %s\n", "failed");
			return -1;
		}
		aul_launch_init(NULL, NULL);
		g_idle_add(run_func, args.appid);
		mainloop = g_main_loop_new(NULL, FALSE);
		if (!mainloop) {
			printf("failed to create glib main loop\n");
			exit(EXIT_FAILURE);
		}
		g_main_loop_run(mainloop);
		return 0;
	} else if (op == 'k') {
		is_running = aul_app_is_running(args.appid);
		if (true == is_running) {
			aul_app_get_running_app_info(__iterfunc_kill,
					args.appid);
		} else {
			printf("result: %s\n", "App isn't running");
			return 1;
		}
	} else if (op == 'r') {
		is_running = aul_app_is_running(args.appid);
		if (true == is_running) {
			printf("result: %s\n", "running");
			return 0;
		} else {
			printf("result: %s\n", "not running");
			return 1;
		}
	}

	return 0;
}