tizen 2.3.1 release
[framework/appfw/aul-1.git] / test / launch_app.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26
27 #include <Ecore.h>
28 #include "aul.h"
29
30 #define ROOT_UID 0
31 #define TMP_FILE "/tmp/.testpkg"
32
33 static char **gargv;
34 static int gargc;
35 bundle *kb = NULL;
36
37
38 static bundle *create_internal_bundle(int start)
39 {
40         bundle *kb;
41         int i;
42         char arg[1024] = {0, };
43         char* val_array[128];
44
45         kb = bundle_create();
46         for (i = start; i < gargc - 1; i++) {
47                 if ((i + 1) > gargc - 1)
48                         bundle_add(kb, gargv[i], " ");
49                 else {
50                         int j = 1;
51                         strncpy(arg, gargv[i + 1], 1023);
52                         val_array[0] = strtok(arg,",");
53                         while(1)
54                         {
55                                 val_array[j] = strtok(NULL,",");
56                                 if(val_array[j] == NULL)
57                                         break;
58                                 j++;
59                         }
60                         if(j==1)
61                                 bundle_add(kb, gargv[i], gargv[i + 1]);
62                         else if(j>1)
63                                 bundle_add_str_array(kb, gargv[i],
64                                         (const char**)val_array, j);
65                 }
66         }
67
68         return kb;
69 }
70
71 int launch()
72 {
73         FILE *fp;
74         int ret = -1;
75         int pid = -1;
76
77         kb = create_internal_bundle(2);
78         if (NULL == kb) {
79                 printf("bundle creation fail\n");
80                 return -1;
81         }
82
83         pid = aul_launch_app(gargv[1], kb);
84
85         /* Write the package name to TMP_FILE*/
86 /*      fp = fopen(TMP_FILE, "w");
87         if (fp == NULL)
88                 return -1;
89         ret = fprintf(fp, "%d", pid);
90         fclose(fp);
91         if (ret < 0)
92                 return -1;*/
93
94         return pid;
95 }
96
97 void print_usage(char *progname)
98 {
99         printf("[usage] %s <pkgname> <key1> <val1> <key2> <val2> ...\n",
100                progname);
101 }
102
103 static int __launch_app_dead_handler(int pid, void *data)
104 {
105         int listen_pid = (int) data;
106
107         if(listen_pid == pid)
108                 ecore_main_loop_quit();
109
110         return 0;
111 }
112
113 static Eina_Bool run_func(void *data)
114 {
115         int pid = -1;
116         char *str = NULL;
117
118         if ((pid = launch()) > 0) {
119                 printf("... successfully launched\n");
120                 str      = bundle_get_val(kb, "__LAUNCH_APP_MODE__");
121
122                 if( str && strcmp(str, "SYNC") == 0 ) {
123                         aul_listen_app_dead_signal(__launch_app_dead_handler, pid);
124                 } else {
125                         ecore_main_loop_quit();
126         }
127         } else {
128                 printf("... launch failed\n");
129                 ecore_main_loop_quit();
130         }
131
132         if (kb) {
133                 bundle_free(kb);
134                 kb = NULL;
135         }
136
137         return 0;
138 }
139
140
141 int main(int argc, char **argv)
142 {
143         if (argc < 2) {
144                 print_usage(argv[0]);
145                 exit(EXIT_FAILURE);
146         }
147
148         ecore_init();
149
150         gargc = argc;
151         gargv = argv;
152
153         aul_launch_init(NULL, NULL);
154
155         ecore_idler_add(run_func, NULL);
156
157         ecore_main_loop_begin();
158
159         return 0;
160 }
161