1 /**************************************************************************
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>
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:
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
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.
29 **************************************************************************/
39 void display_topwins(XinfoPtr pXinfo);
41 static long parse_long (char *s)
43 const char *fmt = "%lu";
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);
53 return (thesign * retval);
56 static void free_xinfo(XinfoPtr pXinfo)
61 fd = pXinfo->output_fd;
69 make_directory(XinfoPtr pXinfo, char *args)
71 char tmp1[255], tmp2[255], tmp3[255];
80 fprintf(stderr,"fail to get time\n");
84 buf = calloc (1, sizeof (struct tm));
85 t = localtime_r(&timer, buf);
88 fprintf(stderr,"fail to get local time\n");
92 pXinfo->pathname = (char*) calloc(1, 255*sizeof(char));
94 if(pXinfo->xinfo_val == XINFO_XWD_TOPVWINS)
97 snprintf(tmp1, 255, "%s", args);
99 snprintf(tmp1, 255, "./");
101 else if(pXinfo->xinfo_val == XINFO_XWD_WIN)
104 snprintf(tmp1, 255, "./");
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");
118 if (execv("/bin/mkdir", argument) == -1)
123 fprintf(stderr, "error execv: mkdir\n");
136 init_xinfo(XinfoPtr pXinfo, int xinfo_val, char* args)
138 char full_pathfile[255];
142 int is_xwd_win = FALSE;
147 fprintf(stderr,"fail to get time\n");
151 buf = calloc (1, sizeof(struct tm));
152 t = localtime_r(&timer, buf);
155 fprintf(stderr,"fail to get local time\n");
160 pXinfo->xinfo_val = xinfo_val;
162 pXinfo->xinfovalname = (char*) calloc(1, 255*sizeof(char));
163 switch(pXinfo->xinfo_val)
166 snprintf(pXinfo->xinfovalname, 255, "%s", "topwins");
169 snprintf(pXinfo->xinfovalname, 255, "%s", "topvwins");
172 snprintf(pXinfo->xinfovalname, 255, "%s", "ping");
174 case XINFO_XWD_TOPVWINS:
175 snprintf(pXinfo->xinfovalname, 255, "%s", "xwd_topvwins");
179 snprintf(pXinfo->xinfovalname, 255, "%s", "xwd_win");
183 case XINFO_TOPVWINS_PROPS:
184 snprintf(pXinfo->xinfovalname, 255, "%s", "topvwins_props");
192 make_directory(pXinfo, args);
193 pXinfo->output_fd = stderr;
194 pXinfo->filename = NULL;
196 pXinfo->win = parse_long(args);
200 /* set the path and output_fd */
203 pXinfo->output_fd = stderr;
204 pXinfo->filename = NULL;
205 pXinfo->pathname = NULL;
210 pXinfo->pathname = (char*) calloc(1, 255*sizeof(char));
211 pXinfo->filename = (char*) calloc(1, 255*sizeof(char));
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)
219 fprintf(stderr, "Error - can open file \n");
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");
247 int main(int argc, char **argv)
251 int xinfo_value = -1;
254 if( !strcmp(argv[1],"-h") || !strcmp(argv[1],"-help"))
256 else if(!strcmp(argv[1], "-topwins"))
258 xinfo_value = XINFO_TOPWINS;
260 else if(!strcmp(argv[1], "-topvwins"))
262 xinfo_value = XINFO_TOPVWINS;
264 else if(!strcmp(argv[1], "-ping") || !strcmp(argv[1], "-p") )
266 xinfo_value = XINFO_PING;
268 else if(!strcmp(argv[1], "-xwd_topvwins"))
270 xinfo_value = XINFO_XWD_TOPVWINS;
272 else if(!strcmp(argv[1], "-xwd_win"))
274 xinfo_value = XINFO_XWD_WIN;
277 fprintf(stderr, "Error : no window id \n");
282 else if(!strcmp(argv[1], "-topvwins_props"))
284 xinfo_value = XINFO_TOPVWINS_PROPS;
290 pXinfo = (XinfoPtr) malloc(sizeof(Xinfo));
293 fprintf(stderr, " alloc error \n");
298 init_xinfo(pXinfo, xinfo_value, argv[2]);
300 init_xinfo(pXinfo, xinfo_value, NULL);
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)
305 display_topwins(pXinfo);
309 fprintf(stderr, "error unsupported xinfo vaule\n");