xref: /dragonfly/contrib/dialog/guage.c (revision 0db87cb7)
1 /*
2  *  $Id: guage.c,v 1.65 2012/11/30 10:43:31 tom Exp $
3  *
4  *  guage.c -- implements the gauge dialog
5  *
6  *  Copyright 2000-2011,2012	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  *
23  *  An earlier version of this program lists as authors
24  *	Marc Ewing, Red Hat Software
25  */
26 
27 #include <dialog.h>
28 
29 #include <errno.h>
30 
31 #define MY_LEN (MAX_LEN)/2
32 
33 #define MIN_HIGH (4)
34 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
35 
36 #define isMarker(buf) !strncmp(buf, "XXX", (size_t) 3)
37 
38 typedef struct _my_obj {
39     DIALOG_CALLBACK obj;	/* has to be first in struct */
40     struct _my_obj *next;
41     WINDOW *text;
42     const char *title;
43     char *prompt;
44     char prompt_buf[MY_LEN];
45     int percent;
46     int height;
47     int width;
48     char line[MAX_LEN + 1];
49 } MY_OBJ;
50 
51 static MY_OBJ *all_objects;
52 
53 static int
54 valid(MY_OBJ * obj)
55 {
56     MY_OBJ *list = all_objects;
57     int result = 0;
58 
59     while (list != 0) {
60 	if (list == obj) {
61 	    result = 1;
62 	    break;
63 	}
64 	list = list->next;
65     }
66     return result;
67 }
68 
69 static void
70 delink(MY_OBJ * obj)
71 {
72     MY_OBJ *p = all_objects;
73     MY_OBJ *q = 0;
74     while (p != 0) {
75 	if (p == obj) {
76 	    if (q != 0) {
77 		q->next = p->next;
78 	    } else {
79 		all_objects = p->next;
80 	    }
81 	    break;
82 	}
83 	q = p;
84 	p = p->next;
85     }
86 }
87 
88 static int
89 read_data(char *buffer, FILE *fp)
90 {
91     int result;
92 
93     if (feof(fp)) {
94 	result = 0;
95     } else if (fgets(buffer, MY_LEN, fp) != 0) {
96 	DLG_TRACE(("read_data:%s", buffer));
97 	buffer[MY_LEN] = '\0';
98 	dlg_trim_string(buffer);
99 	result = 1;
100     } else {
101 	result = -1;
102     }
103     return result;
104 }
105 
106 static int
107 decode_percent(char *buffer)
108 {
109     char *tmp = 0;
110     long value = strtol(buffer, &tmp, 10);
111 
112     if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) {
113 	return TRUE;
114     }
115     return FALSE;
116 }
117 
118 static void
119 repaint_text(MY_OBJ * obj)
120 {
121     WINDOW *dialog = obj->obj.win;
122     int i, x;
123 
124     if (dialog != 0 && obj->obj.input != 0) {
125 	(void) werase(dialog);
126 	dlg_draw_box2(dialog, 0, 0, obj->height, obj->width, dialog_attr,
127 		      border_attr, border2_attr);
128 
129 	dlg_draw_title(dialog, obj->title);
130 
131 	(void) wattrset(dialog, dialog_attr);
132 	dlg_draw_helpline(dialog, FALSE);
133 	dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width);
134 
135 	dlg_draw_box2(dialog,
136 		      obj->height - 4, 2 + MARGIN,
137 		      2 + MARGIN, obj->width - 2 * (2 + MARGIN),
138 		      dialog_attr,
139 		      border_attr,
140 		      border2_attr);
141 
142 	/*
143 	 * Clear the area for the progress bar by filling it with spaces
144 	 * in the gauge-attribute, and write the percentage with that
145 	 * attribute.
146 	 */
147 	(void) wmove(dialog, obj->height - 3, 4);
148 	(void) wattrset(dialog, gauge_attr);
149 
150 	for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++)
151 	    (void) waddch(dialog, ' ');
152 
153 	(void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2);
154 	(void) wprintw(dialog, "%3d%%", obj->percent);
155 
156 	/*
157 	 * Now draw a bar in reverse, relative to the background.
158 	 * The window attribute was useful for painting the background,
159 	 * but requires some tweaks to reverse it.
160 	 */
161 	x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100;
162 	if ((gauge_attr & A_REVERSE) != 0) {
163 	    wattroff(dialog, A_REVERSE);
164 	} else {
165 	    (void) wattrset(dialog, A_REVERSE);
166 	}
167 	(void) wmove(dialog, obj->height - 3, 4);
168 	for (i = 0; i < x; i++) {
169 	    chtype ch2 = winch(dialog);
170 	    if (gauge_attr & A_REVERSE) {
171 		ch2 &= ~A_REVERSE;
172 	    }
173 	    (void) waddch(dialog, ch2);
174 	}
175 
176 	(void) wrefresh(dialog);
177     }
178 }
179 
180 static bool
181 handle_input(DIALOG_CALLBACK * cb)
182 {
183     MY_OBJ *obj = (MY_OBJ *) cb;
184     bool result;
185     int status;
186     char buf[MY_LEN + 1];
187 
188     if (dialog_state.pipe_input == 0) {
189 	status = -1;
190     } else if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
191 
192 	if (isMarker(buf)) {
193 	    /*
194 	     * Historically, next line should be percentage, but one of the
195 	     * worse-written clones of 'dialog' assumes the number is missing.
196 	     * (Gresham's Law applied to software).
197 	     */
198 	    if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
199 
200 		obj->prompt_buf[0] = '\0';
201 		if (decode_percent(buf))
202 		    obj->percent = atoi(buf);
203 		else
204 		    strcpy(obj->prompt_buf, buf);
205 
206 		/* Rest is message text */
207 		while ((status = read_data(buf, dialog_state.pipe_input)) > 0
208 		       && !isMarker(buf)) {
209 		    if (strlen(obj->prompt_buf) + strlen(buf) <
210 			sizeof(obj->prompt_buf) - 1) {
211 			strcat(obj->prompt_buf, buf);
212 		    }
213 		}
214 
215 		if (obj->prompt != obj->prompt_buf)
216 		    free(obj->prompt);
217 		obj->prompt = obj->prompt_buf;
218 	    }
219 	} else if (decode_percent(buf)) {
220 	    obj->percent = atoi(buf);
221 	}
222     } else {
223 	if (feof(dialog_state.pipe_input) ||
224 	    (ferror(dialog_state.pipe_input) && errno != EINTR)) {
225 	    delink(obj);
226 	    dlg_remove_callback(cb);
227 	}
228     }
229 
230     if (status > 0) {
231 	result = TRUE;
232 	repaint_text(obj);
233     } else {
234 	result = FALSE;
235     }
236 
237     return result;
238 }
239 
240 static bool
241 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
242 {
243     int status = TRUE;
244 
245     *result = DLG_EXIT_OK;
246     if (cb != 0) {
247 	if (!fkey && (ch == ERR)) {
248 	    (void) handle_input(cb);
249 	    /* cb might be freed in handle_input */
250 	    status = (valid((MY_OBJ *) cb) && (cb->input != 0));
251 	}
252     } else {
253 	status = FALSE;
254     }
255     return status;
256 }
257 
258 static void
259 my_cleanup(DIALOG_CALLBACK * cb)
260 {
261     MY_OBJ *obj = (MY_OBJ *) cb;
262 
263     if (valid(obj)) {
264 	if (obj->prompt != obj->prompt_buf) {
265 	    free(obj->prompt);
266 	    obj->prompt = obj->prompt_buf;
267 	}
268 	delink(obj);
269     }
270 }
271 
272 void
273 dlg_update_gauge(void *objptr, int percent)
274 {
275     MY_OBJ *obj = (MY_OBJ *) objptr;
276 
277     curs_set(0);
278     obj->percent = percent;
279     repaint_text(obj);
280 }
281 
282 /*
283  * Allocates a new object and fills it as per the arguments
284  */
285 void *
286 dlg_allocate_gauge(const char *title,
287 		   const char *cprompt,
288 		   int height,
289 		   int width,
290 		   int percent)
291 {
292     int x, y;
293     char *prompt = dlg_strclone(cprompt);
294     WINDOW *dialog;
295     MY_OBJ *obj = 0;
296 
297     dlg_tab_correct_str(prompt);
298 
299     dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
300     dlg_print_size(height, width);
301     dlg_ctl_size(height, width);
302 
303     /* center dialog box on screen */
304     x = dlg_box_x_ordinate(width);
305     y = dlg_box_y_ordinate(height);
306 
307     dialog = dlg_new_window(height, width, y, x);
308 
309     obj = dlg_calloc(MY_OBJ, 1);
310     assert_ptr(obj, "dialog_gauge");
311 
312     obj->obj.input = dialog_state.pipe_input;
313     obj->obj.win = dialog;
314     obj->obj.keep_win = TRUE;
315     obj->obj.bg_task = TRUE;
316     obj->obj.handle_getc = handle_my_getc;
317     obj->obj.handle_input = handle_input;
318 
319     obj->title = title;
320     obj->prompt = prompt;
321     obj->percent = percent;
322     obj->height = height;
323     obj->width = width;
324 
325     obj->next = all_objects;
326     all_objects = obj;
327 
328     return (void *) obj;
329 }
330 
331 void
332 dlg_free_gauge(void *objptr)
333 {
334     MY_OBJ *obj = (MY_OBJ *) objptr;
335 
336     curs_set(1);
337     if (valid(obj)) {
338 	delink(obj);
339 	obj->obj.keep_win = FALSE;
340 	dlg_remove_callback(&(obj->obj));
341     }
342 }
343 
344 /*
345  * Display a gauge, or progress meter.  Starts at percent% and reads stdin.  If
346  * stdin is not XXX, then it is interpreted as a percentage, and the display is
347  * updated accordingly.  Otherwise the next line is the percentage, and
348  * subsequent lines up to another XXX are used for the new prompt.  Note that
349  * the size of the window never changes, so the prompt can not get any larger
350  * than the height and width specified.
351  */
352 int
353 dialog_gauge(const char *title,
354 	     const char *cprompt,
355 	     int height,
356 	     int width,
357 	     int percent)
358 {
359     int fkey;
360     int ch, result;
361     void *objptr = dlg_allocate_gauge(title, cprompt, height, width, percent);
362     MY_OBJ *obj = (MY_OBJ *) objptr;
363 
364     dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
365     dlg_update_gauge(obj, percent);
366 
367     dlg_trace_win(obj->obj.win);
368     do {
369 	ch = dlg_getc(obj->obj.win, &fkey);
370 #ifdef KEY_RESIZE
371 	if (fkey && ch == KEY_RESIZE) {
372 	    MY_OBJ *oldobj = obj;
373 
374 	    dlg_mouse_free_regions();
375 
376 	    obj = dlg_allocate_gauge(title,
377 				     cprompt,
378 				     height,
379 				     width,
380 				     oldobj->percent);
381 
382 	    /* avoid breaking new window in dlg_remove_callback */
383 	    oldobj->obj.caller = 0;
384 	    oldobj->obj.input = 0;
385 	    oldobj->obj.keep_win = FALSE;
386 
387 	    /* remove the old version of the gauge */
388 	    dlg_clear();
389 	    dlg_remove_callback(&(oldobj->obj));
390 	    refresh();
391 
392 	    dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
393 	    dlg_update_gauge(obj, obj->percent);
394 	}
395 #endif
396     }
397     while (valid(obj) && handle_my_getc(&(obj->obj), ch, fkey, &result));
398 
399     dlg_free_gauge(obj);
400 
401     return (DLG_EXIT_OK);
402 }
403