Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsystest/main_console.c

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