Add init code
[platform/core/uifw/xinfo.git] / src / xinfo.c
1 /**************************************************************************
2
3 xinfo
4
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <xinfo.h>
36 #include <time.h>
37 #include <string.h>
38
39 void display_topwins(XinfoPtr pXinfo);
40
41 static long parse_long (char *s)
42 {
43     const char *fmt = "%lu";
44     long retval = 0L;
45     int thesign = 1;
46
47     if (s && s[0]) {
48         if (s[0] == '-') s++, thesign = -1;
49         if (s[0] == '0') s++, fmt = "%lo";
50         if (s[0] == 'x' || s[0] == 'X') s++, fmt = "%lx";
51         (void) sscanf (s, fmt, &retval);
52     }
53     return (thesign * retval);
54 }
55
56 static void free_xinfo(XinfoPtr pXinfo)
57 {
58         FILE *fd;
59         if(pXinfo)
60         {
61                 fd = pXinfo->output_fd;
62                 if(fd != stderr)
63                         fclose(fd);
64                 free(pXinfo);
65         }
66 }
67
68 static void
69 make_directory(XinfoPtr pXinfo, char *args)
70 {
71         char tmp1[255], tmp2[255], tmp3[255];
72         time_t timer;
73         struct tm *t, *buf;
74         pid_t pid;
75         char *argument[4];
76
77         timer = time(NULL);
78         if (!timer)
79         {
80                 fprintf(stderr,"fail to get time\n");
81                 exit(1);
82         }
83
84         buf = calloc (1, sizeof (struct tm));
85         t = localtime_r(&timer, buf);
86         if (!t)
87         {
88                 fprintf(stderr,"fail to get local time\n");
89                 exit(1);
90         }
91
92         pXinfo->pathname = (char*) calloc(1, 255*sizeof(char));
93
94         if(pXinfo->xinfo_val == XINFO_XWD_TOPVWINS)
95         {
96                 if(args)
97                         snprintf(tmp1, 255, "%s", args);
98                 else
99                         snprintf(tmp1, 255, "./");
100         }
101         else if(pXinfo->xinfo_val == XINFO_XWD_WIN)
102         {
103                 if(args)
104                         snprintf(tmp1, 255, "./");
105         }
106
107         /* make the folder for the result of xwd files */
108         snprintf(tmp2, 255, "%s_%02d%02d-%02d%02d%02d", pXinfo->xinfovalname, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
109         snprintf(pXinfo->pathname, 255, "%s/%s",tmp1, tmp2);
110         snprintf(tmp3, 255, "%s", pXinfo->pathname);
111         argument[0] = strdup ("/bin/mkdir");
112         argument[1] = strdup ("-p");
113         argument[2] = tmp3;
114         argument[3] = NULL;
115         switch(pid = fork())
116         {
117                 case 0:
118                         if (execv("/bin/mkdir", argument) == -1)
119                         {
120                                 free (argument [0]);
121                                 free (argument [1]);
122                                 free (buf);
123                                 fprintf(stderr, "error execv: mkdir\n");
124                                 exit(1);
125                         }
126                         break;
127                 default:
128                         break;
129         }
130         free (argument [0]);
131         free (argument [1]);
132         free (buf);
133 }
134
135 static void
136 init_xinfo(XinfoPtr pXinfo, int xinfo_val, char* args)
137 {
138         char full_pathfile[255];
139         time_t timer;
140         struct tm *t, *buf;
141         int is_xwd = FALSE;
142         int is_xwd_win = FALSE;
143
144         timer = time(NULL);
145         if (!timer)
146         {
147                 fprintf(stderr,"fail to get time\n");
148                 exit(1);
149         }
150
151         buf = calloc (1, sizeof(struct tm));
152         t = localtime_r(&timer, buf);
153         if (!t)
154         {
155                 fprintf(stderr,"fail to get local time\n");
156                 free(buf);
157                 exit(1);
158         }
159
160         pXinfo->xinfo_val = xinfo_val;
161
162         pXinfo->xinfovalname = (char*) calloc(1, 255*sizeof(char));
163         switch(pXinfo->xinfo_val)
164         {
165                 case XINFO_TOPWINS:
166                                 snprintf(pXinfo->xinfovalname, 255, "%s", "topwins");
167                                 break;
168                 case XINFO_TOPVWINS:
169                                 snprintf(pXinfo->xinfovalname, 255, "%s", "topvwins");
170                                 break;
171                 case XINFO_PING:
172                                 snprintf(pXinfo->xinfovalname, 255, "%s", "ping");
173                                 break;
174                 case XINFO_XWD_TOPVWINS:
175                                 snprintf(pXinfo->xinfovalname, 255, "%s", "xwd_topvwins");
176                                 is_xwd = TRUE;
177                                 break;
178                 case XINFO_XWD_WIN:
179                                 snprintf(pXinfo->xinfovalname, 255, "%s", "xwd_win");
180                                 is_xwd = TRUE;
181                                 is_xwd_win = TRUE;
182                                 break;
183                 case XINFO_TOPVWINS_PROPS:
184                                 snprintf(pXinfo->xinfovalname, 255, "%s", "topvwins_props");
185                                 break;
186                 default:
187                                 break;
188         }
189
190         if(is_xwd)
191         {
192                 make_directory(pXinfo, args);
193                 pXinfo->output_fd = stderr;
194                 pXinfo->filename = NULL;
195                 if(is_xwd_win)
196                         pXinfo->win = parse_long(args);
197                 goto out;
198         }
199
200         /* set the path and output_fd */
201         if(!args)
202         {
203                 pXinfo->output_fd = stderr;
204                 pXinfo->filename = NULL;
205                 pXinfo->pathname = NULL;
206                 goto out;
207         }
208         else
209         {
210                 pXinfo->pathname = (char*) calloc(1, 255*sizeof(char));
211                 pXinfo->filename = (char*) calloc(1, 255*sizeof(char));
212
213                 snprintf(pXinfo->pathname, 255, "%s", args);
214                 snprintf(pXinfo->filename, 255, "%s_%02d%02d-%02d%02d%02d.log", pXinfo->xinfovalname, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
215                 snprintf(full_pathfile, 255, "%s/%s", pXinfo->pathname, pXinfo->filename);
216                 pXinfo->output_fd = fopen(full_pathfile, "a+");
217                 if(pXinfo->output_fd == NULL)
218                 {
219                         fprintf(stderr, "Error - can open file \n");
220                         free_xinfo(pXinfo);
221                         free(buf);
222                         exit(1);
223                 }
224         }
225 out:
226         free(buf);
227         return;
228 }
229
230
231
232 static void
233 usage()
234 {
235         fprintf(stderr,"usage : xinfo [-options] [output_path] \n\n");
236         fprintf(stderr,"where options include: \n");
237         fprintf(stderr,"    -topwins [output_path]      : print all top level windows. (default output_path : stdout) \n");
238         fprintf(stderr,"    -topvwins [output_path]     : print all top level visible windows (default output_path : stdout) \n");
239         fprintf(stderr,"    -ping or -p [output_path]   : ping test for all top level windows (default output_path : stdout) \n");
240         fprintf(stderr,"    -xwd_topvwins [output_path] : dump topvwins (default output_path : current working directory) \n");
241         fprintf(stderr,"    -xwd_win [window id] : dump window id (a xwd file stores in current working directory) \n");
242         fprintf(stderr,"    -topvwins_props : print all top level visible windows's property \n");
243         fprintf(stderr,"\n\n");
244         exit(1);
245 }
246
247 int main(int argc, char **argv)
248 {
249         XinfoPtr pXinfo;
250
251         int xinfo_value = -1;
252         if(argv[1])
253         {
254                 if( !strcmp(argv[1],"-h") || !strcmp(argv[1],"-help"))
255                                 usage();
256                 else if(!strcmp(argv[1], "-topwins"))
257                 {
258                         xinfo_value = XINFO_TOPWINS;
259                 }
260                 else if(!strcmp(argv[1], "-topvwins"))
261                 {
262                         xinfo_value = XINFO_TOPVWINS;
263                 }
264                 else if(!strcmp(argv[1], "-ping") || !strcmp(argv[1], "-p") )
265                 {
266                         xinfo_value = XINFO_PING;
267                 }
268                 else if(!strcmp(argv[1], "-xwd_topvwins"))
269                 {
270                         xinfo_value = XINFO_XWD_TOPVWINS;
271                 }
272                 else if(!strcmp(argv[1], "-xwd_win"))
273                 {
274                         xinfo_value = XINFO_XWD_WIN;
275                         if(!argv[2])
276                         {
277                                 fprintf(stderr, "Error : no window id \n");
278                                 usage();
279                                 exit(1);
280                         }
281                 }
282                 else if(!strcmp(argv[1], "-topvwins_props"))
283                 {
284                         xinfo_value = XINFO_TOPVWINS_PROPS;
285                 }
286
287                 else
288                         usage();
289
290                 pXinfo = (XinfoPtr) malloc(sizeof(Xinfo));
291                 if(!pXinfo)
292                 {
293                         fprintf(stderr, " alloc error \n");
294                         exit(1);
295                 }
296
297                 if(argv[2])
298                         init_xinfo(pXinfo, xinfo_value, argv[2]);
299                 else
300                         init_xinfo(pXinfo, xinfo_value, NULL);
301
302                 if(xinfo_value == XINFO_TOPWINS || xinfo_value == XINFO_TOPVWINS || xinfo_value == XINFO_PING ||
303                         xinfo_value == XINFO_XWD_TOPVWINS || xinfo_value == XINFO_XWD_WIN || xinfo_value == XINFO_TOPVWINS_PROPS)
304                 {
305                         display_topwins(pXinfo);
306                 }
307                 else
308                 {
309                         fprintf(stderr, "error unsupported xinfo vaule\n");
310                         goto error;
311                 }
312
313
314         }
315         else
316                 usage();
317
318 error:
319         free_xinfo(pXinfo);
320
321          return 0;
322 }
323
324
325