Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include "systest.h" |
| 20 | #include "gui.h" |
| 21 | #include <stdio.h> |
| 22 | #include <pjlib.h> |
| 23 | |
| 24 | static pj_bool_t console_quit; |
| 25 | |
| 26 | enum gui_key gui_msgbox(const char *title, const char *message, enum gui_flag flag) |
| 27 | { |
| 28 | puts(title); |
| 29 | puts(message); |
| 30 | |
| 31 | for (;;) { |
| 32 | char input[10], *ret; |
| 33 | |
| 34 | if (flag == WITH_YESNO) |
| 35 | printf("%c:Yes %c:No ", KEY_YES, KEY_NO); |
| 36 | else if (flag == WITH_OK) |
| 37 | printf("%c:OK ", KEY_OK); |
| 38 | else if (flag == WITH_OKCANCEL) |
| 39 | printf("%c:OK %c:Cancel ", KEY_OK, KEY_CANCEL); |
| 40 | puts(""); |
| 41 | |
| 42 | ret = fgets(input, sizeof(input), stdin); |
| 43 | if (!ret) |
| 44 | return KEY_CANCEL; |
| 45 | |
| 46 | if (input[0]==KEY_NO || input[0]==KEY_YES || input[0]==KEY_CANCEL) |
| 47 | return (enum gui_key)input[0]; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pj_status_t gui_init(gui_menu *menu) |
| 52 | { |
| 53 | PJ_UNUSED_ARG(menu); |
| 54 | return PJ_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | static void print_menu(const char *indent, char *menu_id, gui_menu *menu) |
| 58 | { |
| 59 | char child_indent[16]; |
| 60 | unsigned i; |
| 61 | |
| 62 | pj_ansi_snprintf(child_indent, sizeof(child_indent), "%s ", indent); |
| 63 | |
| 64 | printf("%s%s: %s\n", indent, menu_id, menu->title); |
| 65 | |
| 66 | for (i=0; i<menu->submenu_cnt; ++i) { |
| 67 | char child_id[10]; |
| 68 | |
| 69 | pj_ansi_sprintf(child_id, "%s%u", menu_id, i); |
| 70 | |
| 71 | if (!menu->submenus[i]) |
| 72 | puts(""); |
| 73 | else |
| 74 | print_menu(child_indent, child_id, menu->submenus[i]); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pj_status_t gui_start(gui_menu *menu) |
| 79 | { |
| 80 | while (!console_quit) { |
| 81 | unsigned i; |
| 82 | char input[10], *p; |
| 83 | gui_menu *choice; |
| 84 | |
| 85 | puts("M E N U :"); |
| 86 | puts("---------"); |
| 87 | for (i=0; i<menu->submenu_cnt; ++i) { |
| 88 | char menu_id[4]; |
| 89 | pj_ansi_sprintf(menu_id, "%u", i); |
| 90 | print_menu("", menu_id, menu->submenus[i]); |
| 91 | } |
| 92 | puts(""); |
| 93 | printf("Enter the menu number: "); |
| 94 | |
| 95 | if (!fgets(input, sizeof(input), stdin)) |
| 96 | break; |
| 97 | |
| 98 | p = input; |
| 99 | choice = menu; |
| 100 | while (*p && *p!='\r' && *p!='\n') { |
| 101 | unsigned d = (*p - '0'); |
| 102 | if (d < 0 || d >= choice->submenu_cnt) { |
| 103 | puts("Invalid selection"); |
| 104 | choice = NULL; |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | choice = choice->submenus[d]; |
| 109 | ++p; |
| 110 | } |
| 111 | |
| 112 | if (choice && *p!='\r' && *p!='\n') { |
| 113 | puts("Invalid characters entered"); |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | if (choice && choice->handler) |
| 118 | (*choice->handler)(); |
| 119 | } |
| 120 | |
| 121 | return PJ_SUCCESS; |
| 122 | } |
| 123 | |
| 124 | void gui_destroy(void) |
| 125 | { |
| 126 | console_quit = PJ_TRUE; |
| 127 | } |
| 128 | |
| 129 | void gui_sleep(unsigned sec) |
| 130 | { |
| 131 | pj_thread_sleep(sec * 1000); |
| 132 | } |
| 133 | |
| 134 | int main() |
| 135 | { |
| 136 | if (systest_init() != PJ_SUCCESS) |
| 137 | return 1; |
| 138 | |
| 139 | systest_run(); |
| 140 | systest_deinit(); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |