/* * set the percentage */ int wdg_percentage_set(wdg_t *wo, size_t p, size_t max) { WDG_WO_EXT(struct wdg_percentage, ww); /* set the percentage */ ww->percent = p * 100 / max; WDG_DEBUG_MSG("wdg_percentage_set: %d", ww->percent); wdg_percentage_redraw(wo); /* reached the max, selfdestruct */ if (p == max) { WDG_DEBUG_MSG("wdg_percentage_set: FINISHED"); wdg_destroy_object(&wo); wdg_redraw_all(); return WDG_PERCENTAGE_FINISHED; } /* user has requested to stop the task */ if (ww->interrupt) { WDG_DEBUG_MSG("wdg_percentage_set: INTERRUPTED"); ww->interrupt = 0; wdg_destroy_object(&wo); wdg_redraw_all(); return WDG_PERCENTAGE_INTERRUPTED; } return WDG_PERCENTAGE_UPDATED; }
/* * copy the temp buffers to the real ones */ static void wdg_input_consolidate(struct wdg_object *wo) { WDG_WO_EXT(struct wdg_input_handle, ww); char *buf; int i = 0, j; size_t buflen; void (*callback)(void); WDG_DEBUG_MSG("wdg_input_consolidate"); while(ww->fields[i] != NULL) { /* get the buffer */ buf = field_buffer(ww->fields[i+1], 0); buflen = strlen(buf); /* trim out the trailing spaces */ for (j = buflen - 1; j >= 0; j--) if (buf[j] == ' ') buf[j] = 0; else break; /* copy the buffer in the real one */ strcpy(ww->buffers[i/2], buf); /* skip the label */ i += 2; } /* execute the callback */ callback = ww->callback; wdg_destroy_object(&wo); wdg_redraw_all(); WDG_EXECUTE(callback); }
/* * destroy the dialog and * call the function associated to the button */ static void wdg_dialog_callback(struct wdg_object *wo) { WDG_WO_EXT(struct wdg_dialog, ww); void (*callback)(void); WDG_DEBUG_MSG("wdg_dialog_callback"); callback = ww->buttons[ww->focus_button].callback; wdg_destroy_object(&wo); wdg_redraw_all(); WDG_EXECUTE(callback); }
void curses_sniff_live(void) { wdg_t *menu; DEBUG_MSG("curses_sniff_live"); wdg_create_object(&menu, WDG_MENU, WDG_OBJ_WANT_FOCUS | WDG_OBJ_ROOT_OBJECT); wdg_set_title(menu, GBL_VERSION, WDG_ALIGN_RIGHT); wdg_set_color(menu, WDG_COLOR_SCREEN, EC_COLOR); wdg_set_color(menu, WDG_COLOR_WINDOW, EC_COLOR_MENU); wdg_set_color(menu, WDG_COLOR_FOCUS, EC_COLOR_FOCUS); wdg_set_color(menu, WDG_COLOR_TITLE, EC_COLOR_TITLE); /* add the menu from external files */ wdg_menu_add(menu, menu_start); wdg_menu_add(menu, menu_targets); if (GBL_SNIFF->type != SM_BRIDGED) wdg_menu_add(menu, menu_hosts); wdg_menu_add(menu, menu_view); if (GBL_SNIFF->type != SM_BRIDGED) wdg_menu_add(menu, menu_mitm); wdg_menu_add(menu, menu_filters); wdg_menu_add(menu, menu_logging); #ifdef HAVE_PLUGINS wdg_menu_add(menu, menu_plugins); #endif wdg_menu_add(menu, menu_help); wdg_draw_object(menu); /* repaint the whole screen */ wdg_redraw_all(); wdg_set_focus(menu); /* add the message flush callback */ wdg_add_idle_callback(curses_flush_msg); /* * give the control to the event dispatcher * with the emergency exit key 'Q' */ wdg_events_handler(CTRL('X')); wdg_destroy_object(&menu); }
/* * called by the messages dispatcher when the file dialog is focused */ static int wdg_file_get_msg(struct wdg_object *wo, int key, struct wdg_mouse_event *mouse) { WDG_WO_EXT(struct wdg_file_handle, ww); /* handle the message */ switch (key) { case KEY_MOUSE: /* is the mouse event within our edges ? */ if (wenclose(ww->win, mouse->y, mouse->x)) { wdg_set_focus(wo); /* pass it to the menu */ if (wdg_file_driver(wo, key, mouse) != WDG_E_SUCCESS) wdg_file_redraw(wo); } else return -WDG_E_NOTHANDLED; break; case KEY_RETURN: case KEY_DOWN: case KEY_UP: case KEY_PPAGE: case KEY_NPAGE: /* move only if focused */ if (wo->flags & WDG_OBJ_FOCUSED) { if (wdg_file_driver(wo, key, mouse) != WDG_E_SUCCESS) wdg_file_redraw(wo); } else return -WDG_E_NOTHANDLED; break; case KEY_ESC: case CTRL('Q'): wdg_destroy_object(&wo); wdg_redraw_all(); break; /* message not handled */ default: return -WDG_E_NOTHANDLED; break; } return WDG_E_SUCCESS; }
/* * called by the messages dispatcher when the menu is focused */ static int wdg_input_get_msg(struct wdg_object *wo, int key, struct wdg_mouse_event *mouse) { WDG_WO_EXT(struct wdg_input_handle, ww); WDG_DEBUG_MSG("keypress get msg: %d", key); /* handle the message */ switch (key) { case KEY_MOUSE: /* is the mouse event within our edges ? */ if (wenclose(ww->win, mouse->y, mouse->x)) { wdg_set_focus(wo); /* redraw the menu */ wdg_input_redraw(wo); } else { return -WDG_E_NOTHANDLED; } break; case KEY_ESC: case CTRL('Q'): wdg_destroy_object(&wo); wdg_redraw_all(); return WDG_EFINISHED; break; /* message not handled */ default: if (wo->flags & WDG_OBJ_FOCUSED) { return wdg_input_driver(wo, key, mouse); } else { return -WDG_E_NOTHANDLED; } break; } return WDG_E_SUCCESS; }
/* * destroy the dialog and * call the function associated to the file open dialog */ static void wdg_file_callback(struct wdg_object *wo, const char *path, char *file) { WDG_WO_EXT(struct wdg_file_handle, ww); void (*callback)(const char *, char *); char *p, *f; WDG_DEBUG_MSG("wdg_file_callback"); /* save the values before destroying the object */ callback = ww->callback; WDG_SAFE_STRDUP(p, path); WDG_SAFE_STRDUP(f, file); /* destroy the object */ wdg_destroy_object(&wo); wdg_redraw_all(); /* call the callback */ WDG_EXECUTE(callback, p, f); /* free saved data */ WDG_SAFE_FREE(f); WDG_SAFE_FREE(p); }
/* * this function will get the input from the user. * CAUTION: this is an hack, since it uses wgetch() * it will takeover the main dispatching loop !!! */ void wdg_input_get_input(wdg_t *wo) { int key, ret; struct wdg_mouse_event mouse; WDG_DEBUG_MSG("wdg_input_get_input"); /* dispatch keys to self */ WDG_LOOP { key = wgetch(stdscr); switch (key) { /* don't switch focus... */ case KEY_TAB: break; case KEY_CTRL_L: /* redrawing the screen is equivalent to resizing it */ case KEY_RESIZE: /* the screen has been resized */ wdg_redraw_all(); /* update the screen */ doupdate(); break; case ERR: /* non-blocking input reached the timeout */ /* sleep for milliseconds */ napms(WDG_INPUT_TIMEOUT * 10); refresh(); doupdate(); break; default: #ifdef NCURSES_MOUSE_VERSION /* handle mouse events */ if (key == KEY_MOUSE) { MEVENT event; getmouse(&event); mouse_trafo(&event.y, &event.x, TRUE); mouse.x = event.x; mouse.y = event.y; mouse.event = event.bstate; } #else /* we don't support mouse events */ memset(&mouse, 0, sizeof(mouse)); #endif /* dispatch the user input */ ret = wdg_input_get_msg(wo, key, &mouse); /* update the screen */ doupdate(); /* * if the object is destroyed or the input finished, * then return to the main loop */ if (ret == WDG_EFINISHED) { WDG_DEBUG_MSG("wdg_input_get_input: return to main loop"); return; } break; } } }