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 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;
151 char last_message[80];
152 int is_streaming = 0;
154 char *bdropfn = NULL;
155 char *rendezvous = NULL;
157 int force_monochrome = 0;
160 * Get command-line arguments.
162 while ((opt = getopt(argc, argv, "b:mr:t:")) != -1) {
165 bdropfn = aura_strdup(optarg);
168 force_monochrome = 1;
171 rendezvous = aura_strdup(optarg);
174 transport = user_get_transport(optarg);
185 transport = user_get_transport("tcp");
187 if (rendezvous == NULL) {
188 if (transport == DFUI_TRANSPORT_TCP) {
189 rendezvous = aura_strdup("9999");
191 rendezvous = aura_strdup("test");
196 setlocale (LC_ALL, "");
197 bindtextdomain (PACKAGE, LOCALEDIR);
198 textdomain (PACKAGE);
207 #ifdef SIGNAL_HANDLING
208 signal(SIGINT, signal_handler);
209 signal(SIGTERM, signal_handler);
212 curses_colors_init(force_monochrome);
216 keypad(stdscr, TRUE);
219 getmaxyx(stdscr, ymax, xmax);
221 if (bdropfn == NULL) {
222 curses_colors_set(stdscr, CURSES_COLORS_BACKDROP);
223 curses_window_blank(stdscr);
225 curses_load_backdrop(stdscr, bdropfn);
228 menubar = curses_bar_new(0, 0, 0, 1, CURSES_COLORS_MENUBAR,
230 statusbar = curses_bar_new(0, 0, 0, 1, CURSES_COLORS_STATUSBAR,
231 CURSES_BAR_WIDEN | CURSES_BAR_BOTTOM);
233 curses_bar_set_text(menubar, _("F10=Refresh Display"));
234 curses_bar_set_text(statusbar, _("Waiting for backend..."));
240 dfui_debug_file = fopen("/tmp/dfuife_curses_debug.log", "w");
243 c = dfui_connection_new(transport, rendezvous);
246 curses_bar_set_text(statusbar, _("Connected"));
249 dfui_fe_receive(c, &msgtype, &payload);
251 case DFUI_BE_MSG_PRESENT:
252 f = (struct dfui_form *)payload;
253 r = present_form(c, f);
254 #ifdef SIGNAL_HANDLING
255 if (caught_signal) abort_frontend(c);
257 dfui_fe_submit(c, r);
259 dfui_response_free(r);
261 case DFUI_BE_MSG_PROG_BEGIN:
262 pr = (struct dfui_progress *)payload;
264 curses_form_free(pf);
265 is_streaming = dfui_progress_get_streaming(pr);
266 strncpy(last_message, dfui_info_get_short_desc(
267 dfui_progress_get_info(pr)), 79);
268 pf = curses_form_construct_from_dfui_progress(pr,
269 &pbar, &plab, &pcan);
270 curses_form_draw(pf);
271 curses_form_refresh(pf);
272 dfui_progress_free(pr);
273 nodelay(stdscr, TRUE);
274 dfui_fe_progress_continue(c);
276 case DFUI_BE_MSG_PROG_UPDATE:
277 pr = (struct dfui_progress *)payload;
279 curses_widgets_update_from_dfui_progress(pr,
282 dfui_progress_free(pr);
284 if (ch == ' ' || ch == '\n' || ch == '\r') {
285 dfui_fe_progress_cancel(c);
286 } else if (ch == KEY_F(10)) {
288 curses_form_refresh(NULL);
289 dfui_fe_progress_continue(c);
291 dfui_fe_progress_continue(c);
294 case DFUI_BE_MSG_PROG_END:
297 w = curses_form_widget_add(pf, 0, pf->int_height, 0,
298 CURSES_BUTTON, "OK", -1,
299 CURSES_WIDGET_CENTER | CURSES_WIDGET_WIDEN);
301 curses_widget_set_click_cb(w, cb_click_close_form);
302 pf->widget_focus = w;
303 curses_form_widget_ensure_visible(w);
304 curses_widget_draw(w);
305 curses_form_refresh(pf);
306 curses_form_frob(pf);
308 curses_form_free(pf);
309 curses_form_refresh(NULL);
312 plab = pbar = pcan = NULL;
313 nodelay(stdscr, FALSE);
314 dfui_fe_progress_continue(c);
316 case DFUI_BE_MSG_SET_GLOBAL:
317 gp = (struct dfui_property *)payload;
321 * Check for a change to the "lang" setting...
323 if (strcmp(dfui_property_get_name(gp), "lang") == 0) {
324 set_lang_envars(dfui_property_get_value(gp));
326 /* let gettext know about changes */
329 /* BEGIN: reinit curses to use new TERM */
330 curses_bar_free(menubar);
331 curses_bar_free(statusbar);
334 newterm(getenv("TERM"), stdout, stdin);
336 curses_colors_init(force_monochrome);
340 keypad(stdscr, TRUE);
346 if (bdropfn == NULL) {
347 curses_colors_set(stdscr,
348 CURSES_COLORS_BACKDROP);
349 curses_window_blank(stdscr);
351 curses_load_backdrop(stdscr, bdropfn);
354 menubar = curses_bar_new(0, 0, 0, 1,
355 CURSES_COLORS_MENUBAR, CURSES_BAR_WIDEN);
356 statusbar = curses_bar_new(0, 0, 0, 1,
357 CURSES_COLORS_STATUSBAR,
358 CURSES_BAR_WIDEN | CURSES_BAR_BOTTOM);
359 /* END: reinit curses to use new TERM */
361 curses_bar_set_text(menubar,
362 _("F1=Help F10=Refresh Display"));
366 dfui_fe_confirm_set_global(c);
367 dfui_property_free(gp);
369 case DFUI_BE_MSG_STOP:
370 dfui_fe_confirm_stop(c);
376 dfui_fe_disconnect(c);
379 fclose(dfui_debug_file);
382 curses_bar_free(menubar);
383 curses_bar_free(statusbar);
389 if (rendezvous != NULL)