2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of Cat's Eye Technologies nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Main program for dfuife_curses.
37 * $Id: main.c,v 1.21 2005/03/25 04:51:10 cpressey Exp $
50 #include "libdfui/lang.h"
51 #define _(String) gettext (String)
52 extern int _nl_msg_cat_cntr;
54 #define _(String) (String)
57 #include "libaura/mem.h"
59 #include "libdfui/dfui.h"
61 #include "libdfui/dump.h"
63 #include "libdfui/system.h"
65 #include "curses_form.h"
66 #include "curses_widget.h"
67 #include "curses_bar.h"
68 #include "curses_util.h"
69 #include "curses_xlat.h"
73 struct curses_bar *menubar, *statusbar;
75 /*** SIGNAL HANDLING ***/
77 #ifdef SIGNAL_HANDLING
78 volatile sig_atomic_t caught_signal;
80 void signal_handler(int signo)
82 caught_signal = signo;
86 abort_frontend(struct dfui_connection *c)
97 static struct dfui_response *
98 #ifdef SIGNAL_HANDLING
99 present_form(struct dfui_connection *c, struct dfui_form *f)
101 present_form(struct dfui_connection *c __unused, struct dfui_form *f)
104 struct dfui_response *r = NULL;
105 struct curses_form *cf;
106 struct curses_widget *cw;
108 cf = curses_form_construct_from_dfui_form(f);
109 curses_form_draw(cf);
110 curses_form_refresh(cf);
111 cw = curses_form_frob(cf);
112 #ifdef SIGNAL_HANDLING
113 if (caught_signal) abort_frontend(c);
115 r = response_construct_from_curses_form(f, cf, cw);
116 curses_form_free(cf);
117 curses_form_refresh(NULL);
124 fprintf(stderr, _("Usage: %s "
125 "[-b backdrop] [-r rendezvous] [-t caps|npipe|tcp]\n"),
132 * DFUI Curses frontend.
135 main(int argc, char **argv)
137 struct dfui_connection *c;
139 struct dfui_response *r;
140 struct dfui_progress *pr;
141 struct dfui_property *gp;
143 struct curses_form *pf = NULL;
144 struct curses_widget *pbar = NULL, *plab = NULL, *pcan = NULL;
145 struct curses_widget *w;
152 char last_message[80];
153 int is_streaming = 0;
155 char *bdropfn = NULL;
156 char *rendezvous = NULL;
158 int force_monochrome = 0;
161 * Get command-line arguments.
163 while ((opt = getopt(argc, argv, "b:mr:t:")) != -1) {
166 bdropfn = aura_strdup(optarg);
169 force_monochrome = 1;
172 rendezvous = aura_strdup(optarg);
175 transport = user_get_transport(optarg);
186 transport = user_get_transport("tcp");
188 if (rendezvous == NULL) {
189 if (transport == DFUI_TRANSPORT_TCP) {
190 rendezvous = aura_strdup("9999");
192 rendezvous = aura_strdup("test");
197 setlocale (LC_ALL, "");
198 bindtextdomain (PACKAGE, LOCALEDIR);
199 textdomain (PACKAGE);
208 #ifdef SIGNAL_HANDLING
209 signal(SIGINT, signal_handler);
210 signal(SIGTERM, signal_handler);
213 curses_colors_init(force_monochrome);
217 keypad(stdscr, TRUE);
220 getmaxyx(stdscr, ymax, xmax);
222 if (bdropfn == NULL) {
223 curses_colors_set(stdscr, CURSES_COLORS_BACKDROP);
224 curses_window_blank(stdscr);
226 curses_load_backdrop(stdscr, bdropfn);
229 menubar = curses_bar_new(0, 0, 0, 1, CURSES_COLORS_MENUBAR,
231 statusbar = curses_bar_new(0, 0, 0, 1, CURSES_COLORS_STATUSBAR,
232 CURSES_BAR_WIDEN | CURSES_BAR_BOTTOM);
234 curses_bar_set_text(menubar, _("F10=Refresh Display"));
235 curses_bar_set_text(statusbar, _("Waiting for backend..."));
241 dfui_debug_file = fopen("/tmp/dfuife_curses_debug.log", "w");
244 c = dfui_connection_new(transport, rendezvous);
247 curses_bar_set_text(statusbar, _("Connected"));
250 dfui_fe_receive(c, &msgtype, &payload);
252 case DFUI_BE_MSG_PRESENT:
253 f = (struct dfui_form *)payload;
254 r = present_form(c, f);
255 #ifdef SIGNAL_HANDLING
256 if (caught_signal) abort_frontend(c);
258 dfui_fe_submit(c, r);
260 dfui_response_free(r);
262 case DFUI_BE_MSG_PROG_BEGIN:
263 pr = (struct dfui_progress *)payload;
265 curses_form_free(pf);
266 is_streaming = dfui_progress_get_streaming(pr);
267 last_amount = dfui_progress_get_amount(pr);
268 strncpy(last_message, dfui_info_get_short_desc(
269 dfui_progress_get_info(pr)), 79);
270 pf = curses_form_construct_from_dfui_progress(pr,
271 &pbar, &plab, &pcan);
272 curses_form_draw(pf);
273 curses_form_refresh(pf);
274 dfui_progress_free(pr);
275 nodelay(stdscr, TRUE);
276 dfui_fe_progress_continue(c);
278 case DFUI_BE_MSG_PROG_UPDATE:
279 pr = (struct dfui_progress *)payload;
281 curses_widgets_update_from_dfui_progress(pr,
284 dfui_progress_free(pr);
286 if (ch == ' ' || ch == '\n' || ch == '\r') {
287 dfui_fe_progress_cancel(c);
288 } else if (ch == KEY_F(10)) {
290 curses_form_refresh(NULL);
291 dfui_fe_progress_continue(c);
293 dfui_fe_progress_continue(c);
296 case DFUI_BE_MSG_PROG_END:
299 w = curses_form_widget_add(pf, 0, pf->int_height, 0,
300 CURSES_BUTTON, "OK", -1,
301 CURSES_WIDGET_CENTER | CURSES_WIDGET_WIDEN);
303 curses_widget_set_click_cb(w, cb_click_close_form);
304 pf->widget_focus = w;
305 curses_form_widget_ensure_visible(w);
306 curses_widget_draw(w);
307 curses_form_refresh(pf);
308 curses_form_frob(pf);
310 curses_form_free(pf);
311 curses_form_refresh(NULL);
314 plab = pbar = pcan = NULL;
315 nodelay(stdscr, FALSE);
316 dfui_fe_progress_continue(c);
318 case DFUI_BE_MSG_SET_GLOBAL:
319 gp = (struct dfui_property *)payload;
323 * Check for a change to the "lang" setting...
325 if (strcmp(dfui_property_get_name(gp), "lang") == 0) {
326 set_lang_envars(dfui_property_get_value(gp));
328 /* let gettext know about changes */
331 /* BEGIN: reinit curses to use new TERM */
332 curses_bar_free(menubar);
333 curses_bar_free(statusbar);
336 newterm(getenv("TERM"), stdout, stdin);
338 curses_colors_init(force_monochrome);
342 keypad(stdscr, TRUE);
348 if (bdropfn == NULL) {
349 curses_colors_set(stdscr,
350 CURSES_COLORS_BACKDROP);
351 curses_window_blank(stdscr);
353 curses_load_backdrop(stdscr, bdropfn);
356 menubar = curses_bar_new(0, 0, 0, 1,
357 CURSES_COLORS_MENUBAR, CURSES_BAR_WIDEN);
358 statusbar = curses_bar_new(0, 0, 0, 1,
359 CURSES_COLORS_STATUSBAR,
360 CURSES_BAR_WIDEN | CURSES_BAR_BOTTOM);
361 /* END: reinit curses to use new TERM */
363 curses_bar_set_text(menubar,
364 _("F1=Help F10=Refresh Display"));
368 dfui_fe_confirm_set_global(c);
369 dfui_property_free(gp);
371 case DFUI_BE_MSG_STOP:
372 dfui_fe_confirm_stop(c);
378 dfui_fe_disconnect(c);
381 fclose(dfui_debug_file);
384 curses_bar_free(menubar);
385 curses_bar_free(statusbar);
391 if (rendezvous != NULL)