chiark / gitweb /
progress displaying; some client window geometry checking
[ypp-sc-tools.web-live.git] / pctb / convert.c
1
2 #include "convert.h"
3
4
5 void debug_flush(void) {
6   eassert(!fflush(debug));
7   eassert(!ferror(debug));
8 }
9
10
11 const char *get_vardir(void) { return "."; }
12
13 static enum {
14   mf_findwindow=    01,
15   mf_screenshot=    02,
16   mf_analyse=       04,
17   
18   mode_findwindow=  01,
19   mode_screenshot=  03,
20   mode_analyse=     04,
21
22   mode_all=         07,
23 } o_mode= mode_all;
24
25 static char *o_screenshots_fn;
26 static int o_single_page, o_quiet;
27
28 FILE *screenshots_file;
29
30 int main(int argc, char **argv) {
31   const char *arg;
32   int r;
33
34   while ((arg=*++argv)) {
35     if (!strcmp(arg,"--find-window-only"))
36       o_mode= mode_findwindow;
37     else if (!strcmp(arg,"--screenshot-only"))
38       o_mode= mode_screenshot;
39     else if (!strcmp(arg,"--analyse-only"))
40       o_mode= mode_analyse;
41     else if (!strcmp(arg,"--single-page"))
42       o_single_page= 1;
43     else if (!strcmp(arg,"--quiet"))
44       o_quiet= 1;
45     else if (!strcmp(arg,"--screenshots-file"))
46       eassert( o_screenshots_fn= *++argv );
47 #define DF(f)                                   \
48     else if (!strcmp(arg,"-D" #f))              \
49       debug_flags |= dbg_##f;
50     DEBUG_FLAG_LIST
51 #undef DF
52     else if (!strcmp(arg,"--window-id")) {
53       char *ep;
54       eassert((arg=*++argv));
55       unsigned long windowid= strtoul(arg,&ep,0);
56       eassert(!*ep);
57       set_yppclient_window(windowid);
58     } else
59       eassert(!"bad option");
60   }
61   
62   if (!o_screenshots_fn) {
63     r= asprintf(&o_screenshots_fn,"%s/#pages#.ppm",get_vardir());
64     eassert(r>=0);  eassert(o_screenshots_fn);
65   }
66
67   if (o_mode & mf_findwindow) {
68     screenshot_startup();
69     find_yppclient_window();
70   }
71   if (o_mode & mf_screenshot) {
72     screenshots_file= fopen(o_screenshots_fn, "w"); eassert(screenshots_file);
73     if (o_single_page)
74       take_one_screenshot();
75     else
76       take_screenshots();
77   } else {
78     screenshots_file= fopen(o_screenshots_fn, "r"); eassert(screenshots_file);
79     read_screenshots();
80   }
81   if (o_mode & mf_analyse) {
82     analyse();
83     //output_tsv();
84   }
85   return 0;
86 }
87
88
89
90
91 DEFINE_VWRAPPERF(, progress)
92 DEFINE_VWRAPPERF(, progress_log)
93
94 static int last_progress_len;
95      
96 void vprogress(const char *fmt, va_list al) {
97   int r;
98   
99   if (o_quiet) return;
100   if (!isatty(2)) return;
101   
102   if (last_progress_len)
103     putc('\r',stderr);
104
105   r= vfprintf(stderr,fmt,al);
106   eassert(r>=0);
107
108   if (r < last_progress_len) {
109     fprintf(stderr,"%*s", last_progress_len - r, "");
110     if (!r) putc('\r', stderr);
111     else while (last_progress_len-- > r) putc('\b',stderr);
112   }
113   last_progress_len= r;
114   fflush(stderr);
115 }
116    
117 void vprogress_log(const char *fmt, va_list al) {
118   if (o_quiet) return;
119   
120   progress("");
121   vfprintf(stderr,fmt,al);
122   putc('\n',stderr);
123   fflush(stderr);
124 }