xref: /freebsd/contrib/dialog/guage.c (revision f5f7c05209ca2c3748fd8b27c5e80ffad49120eb)
1 /*
2  *  $Id: guage.c,v 1.60 2011/06/27 00:52:28 tom Exp $
3  *
4  *  guage.c -- implements the gauge dialog
5  *
6  *  Copyright 2000-2010,2011	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 	dlg_trim_string(buffer);
98 	result = 1;
99     } else {
100 	result = -1;
101     }
102     return result;
103 }
104 
105 static int
106 decode_percent(char *buffer)
107 {
108     char *tmp = 0;
109     long value = strtol(buffer, &tmp, 10);
110 
111     if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) {
112 	return TRUE;
113     }
114     return FALSE;
115 }
116 
117 static void
118 repaint_text(MY_OBJ * obj)
119 {
120     WINDOW *dialog = obj->obj.win;
121     int i, x;
122 
123     if (dialog != 0 && obj->obj.input != 0) {
124 	(void) werase(dialog);
125 	dlg_draw_box(dialog, 0, 0, obj->height, obj->width, dialog_attr, border_attr);
126 
127 	dlg_draw_title(dialog, obj->title);
128 
129 	wattrset(dialog, dialog_attr);
130 	dlg_draw_helpline(dialog, FALSE);
131 	dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width);
132 
133 	dlg_draw_box(dialog,
134 		     obj->height - 4, 2 + MARGIN,
135 		     2 + MARGIN, obj->width - 2 * (2 + MARGIN),
136 		     dialog_attr,
137 		     border_attr);
138 
139 	/*
140 	 * Clear the area for the progress bar by filling it with spaces
141 	 * in the title-attribute, and write the percentage with that
142 	 * attribute.
143 	 */
144 	(void) wmove(dialog, obj->height - 3, 4);
145 	wattrset(dialog, gauge_attr);
146 
147 	for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++)
148 	    (void) waddch(dialog, ' ');
149 
150 	(void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2);
151 	(void) wprintw(dialog, "%3d%%", obj->percent);
152 
153 	/*
154 	 * Now draw a bar in reverse, relative to the background.
155 	 * The window attribute was useful for painting the background,
156 	 * but requires some tweaks to reverse it.
157 	 */
158 	x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100;
159 	if ((title_attr & A_REVERSE) != 0) {
160 	    wattroff(dialog, A_REVERSE);
161 	} else {
162 	    wattrset(dialog, A_REVERSE);
163 	}
164 	(void) wmove(dialog, obj->height - 3, 4);
165 	for (i = 0; i < x; i++) {
166 	    chtype ch2 = winch(dialog);
167 	    if (title_attr & A_REVERSE) {
168 		ch2 &= ~A_REVERSE;
169 	    }
170 	    (void) waddch(dialog, ch2);
171 	}
172 
173 	(void) wrefresh(dialog);
174     }
175 }
176 
177 static bool
178 handle_input(DIALOG_CALLBACK * cb)
179 {
180     MY_OBJ *obj = (MY_OBJ *) cb;
181     bool result;
182     int status;
183     char buf[MY_LEN];
184 
185     if (dialog_state.pipe_input == 0) {
186 	status = -1;
187     } else if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
188 
189 	if (isMarker(buf)) {
190 	    /*
191 	     * Historically, next line should be percentage, but one of the
192 	     * worse-written clones of 'dialog' assumes the number is missing.
193 	     * (Gresham's Law applied to software).
194 	     */
195 	    if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
196 
197 		obj->prompt_buf[0] = '\0';
198 		if (decode_percent(buf))
199 		    obj->percent = atoi(buf);
200 		else
201 		    strcpy(obj->prompt_buf, buf);
202 
203 		/* Rest is message text */
204 		while ((status = read_data(buf, dialog_state.pipe_input)) > 0
205 		       && !isMarker(buf)) {
206 		    if (strlen(obj->prompt_buf) + strlen(buf) <
207 			sizeof(obj->prompt_buf) - 1) {
208 			strcat(obj->prompt_buf, buf);
209 		    }
210 		}
211 
212 		if (obj->prompt != obj->prompt_buf)
213 		    free(obj->prompt);
214 		obj->prompt = obj->prompt_buf;
215 	    }
216 	} else if (decode_percent(buf)) {
217 	    obj->percent = atoi(buf);
218 	}
219     } else {
220 	if (feof(dialog_state.pipe_input) ||
221 	    (ferror(dialog_state.pipe_input) && errno != EINTR)) {
222 	    delink(obj);
223 	    dlg_remove_callback(cb);
224 	}
225     }
226 
227     if (status > 0) {
228 	result = TRUE;
229 	repaint_text(obj);
230     } else {
231 	result = FALSE;
232     }
233 
234     return result;
235 }
236 
237 static bool
238 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
239 {
240     int status = TRUE;
241 
242     *result = DLG_EXIT_OK;
243     if (cb != 0) {
244 	if (!fkey && (ch == ERR)) {
245 	    (void) handle_input(cb);
246 	    /* cb might be freed in handle_input */
247 	    status = (valid((MY_OBJ *) cb) && (cb->input != 0));
248 	}
249     } else {
250 	status = FALSE;
251     }
252     return status;
253 }
254 
255 static void
256 my_cleanup(DIALOG_CALLBACK * cb)
257 {
258     MY_OBJ *obj = (MY_OBJ *) cb;
259 
260     if (valid(obj)) {
261 	if (obj->prompt != obj->prompt_buf) {
262 	    free(obj->prompt);
263 	    obj->prompt = obj->prompt_buf;
264 	}
265 	delink(obj);
266     }
267 }
268 
269 void
270 dlg_update_gauge(void *objptr, int percent)
271 {
272     MY_OBJ *obj = (MY_OBJ *) objptr;
273 
274     curs_set(0);
275     obj->percent = percent;
276     repaint_text(obj);
277 }
278 
279 /*
280  * Allocates a new object and fills it as per the arguments
281  */
282 void *
283 dlg_allocate_gauge(const char *title,
284 		   const char *cprompt,
285 		   int height,
286 		   int width,
287 		   int percent)
288 {
289     int x, y;
290     char *prompt = dlg_strclone(cprompt);
291     WINDOW *dialog;
292     MY_OBJ *obj = 0;
293 
294     dlg_tab_correct_str(prompt);
295 
296     dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
297     dlg_print_size(height, width);
298     dlg_ctl_size(height, width);
299 
300     /* center dialog box on screen */
301     x = dlg_box_x_ordinate(width);
302     y = dlg_box_y_ordinate(height);
303 
304     dialog = dlg_new_window(height, width, y, x);
305 
306     obj = dlg_calloc(MY_OBJ, 1);
307     assert_ptr(obj, "dialog_gauge");
308 
309     obj->obj.input = dialog_state.pipe_input;
310     obj->obj.win = dialog;
311     obj->obj.keep_win = TRUE;
312     obj->obj.bg_task = TRUE;
313     obj->obj.handle_getc = handle_my_getc;
314     obj->obj.handle_input = handle_input;
315 
316     obj->title = title;
317     obj->prompt = prompt;
318     obj->percent = percent;
319     obj->height = height;
320     obj->width = width;
321 
322     obj->next = all_objects;
323     all_objects = obj;
324 
325     return (void *) obj;
326 }
327 
328 void
329 dlg_free_gauge(void *objptr)
330 {
331     MY_OBJ *obj = (MY_OBJ *) objptr;
332 
333     curs_set(1);
334     if (valid(obj)) {
335 	delink(obj);
336 	obj->obj.keep_win = FALSE;
337 	dlg_remove_callback(&(obj->obj));
338 	free(obj);
339     }
340 }
341 
342 /*
343  * Display a gauge, or progress meter.  Starts at percent% and reads stdin.  If
344  * stdin is not XXX, then it is interpreted as a percentage, and the display is
345  * updated accordingly.  Otherwise the next line is the percentage, and
346  * subsequent lines up to another XXX are used for the new prompt.  Note that
347  * the size of the window never changes, so the prompt can not get any larger
348  * than the height and width specified.
349  */
350 int
351 dialog_gauge(const char *title,
352 	     const char *cprompt,
353 	     int height,
354 	     int width,
355 	     int percent)
356 {
357     int fkey;
358     int ch, result;
359     void *objptr = dlg_allocate_gauge(title, cprompt, height, width, percent);
360     MY_OBJ *obj = (MY_OBJ *) objptr;
361 
362     dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
363     dlg_update_gauge(obj, percent);
364 
365     do {
366 	ch = dlg_getc(obj->obj.win, &fkey);
367 #ifdef KEY_RESIZE
368 	if (fkey && ch == KEY_RESIZE) {
369 	    MY_OBJ *oldobj = obj;
370 
371 	    dlg_mouse_free_regions();
372 
373 	    obj = dlg_allocate_gauge(title,
374 				     cprompt,
375 				     height,
376 				     width,
377 				     oldobj->percent);
378 
379 	    /* avoid breaking new window in dlg_remove_callback */
380 	    oldobj->obj.caller = 0;
381 	    oldobj->obj.input = 0;
382 	    oldobj->obj.keep_win = FALSE;
383 
384 	    /* remove the old version of the gauge */
385 	    dlg_clear();
386 	    dlg_remove_callback(&(oldobj->obj));
387 	    refresh();
388 
389 	    dlg_add_callback_ref((DIALOG_CALLBACK **) & obj, my_cleanup);
390 	    dlg_update_gauge(obj, obj->percent);
391 	}
392 #endif
393     }
394     while (valid(obj) && handle_my_getc(&(obj->obj), ch, fkey, &result));
395 
396     dlg_free_gauge(obj);
397 
398     return (DLG_EXIT_OK);
399 }
400