summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsystest/main_console.c
blob: 02aa3f21c8c9edcfce97262f1313104399f9521a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id: main_console.c 3553 2011-05-05 06:14:19Z nanang $ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include "systest.h"
#include "gui.h"
#include <stdio.h>
#include <pjlib.h>

static pj_bool_t console_quit;

enum gui_key gui_msgbox(const char *title, const char *message, enum gui_flag flag)
{
    puts(title);
    puts(message);

    for (;;) {
	char input[10], *ret;

	if (flag == WITH_YESNO)
	    printf("%c:Yes  %c:No  ", KEY_YES, KEY_NO);
	else if (flag == WITH_OK)
	    printf("%c:OK  ", KEY_OK);
	else if (flag == WITH_OKCANCEL)
	    printf("%c:OK  %c:Cancel  ", KEY_OK, KEY_CANCEL);
	puts("");

	ret = fgets(input, sizeof(input), stdin);
	if (!ret)
	    return KEY_CANCEL;

	if (input[0]==KEY_NO || input[0]==KEY_YES || input[0]==KEY_CANCEL)
	    return (enum gui_key)input[0];
    }
}

pj_status_t gui_init(gui_menu *menu)
{
    PJ_UNUSED_ARG(menu);
    return PJ_SUCCESS;
}

static void print_menu(const char *indent, char *menu_id, gui_menu *menu)
{
    char child_indent[16];
    unsigned i;

    pj_ansi_snprintf(child_indent, sizeof(child_indent), "%s  ", indent);

    printf("%s%s: %s\n", indent, menu_id, menu->title);

    for (i=0; i<menu->submenu_cnt; ++i) {
	char child_id[10];

	pj_ansi_sprintf(child_id, "%s%u", menu_id, i);

	if (!menu->submenus[i])
	    puts("");
	else
	    print_menu(child_indent, child_id, menu->submenus[i]);
    }
}

pj_status_t gui_start(gui_menu *menu)
{
    while (!console_quit) {
	unsigned i;
	char input[10], *p;
	gui_menu *choice;

	puts("M E N U :");
	puts("---------");
	for (i=0; i<menu->submenu_cnt; ++i) {
	    char menu_id[4];
	    pj_ansi_sprintf(menu_id, "%u", i);
	    print_menu("", menu_id, menu->submenus[i]);
	}
	puts("");
	printf("Enter the menu number: ");

	if (!fgets(input, sizeof(input), stdin))
	    break;

	p = input;
	choice = menu;
	while (*p && *p!='\r' && *p!='\n') {
	    unsigned d = (*p - '0');
	    if (d < 0 || d >= choice->submenu_cnt) {
		puts("Invalid selection");
		choice = NULL;
		break;
	    }

	    choice = choice->submenus[d];
	    ++p;
	}

	if (choice && *p!='\r' && *p!='\n') {
	    puts("Invalid characters entered");
	    continue;
	}

	if (choice && choice->handler)
	    (*choice->handler)();
    }

    return PJ_SUCCESS;
}

void gui_destroy(void)
{
    console_quit = PJ_TRUE;
}

void gui_sleep(unsigned sec)
{
    pj_thread_sleep(sec * 1000);
}

int main()
{
    if (systest_init() != PJ_SUCCESS)
	return 1;

    systest_run();
    systest_deinit();

    return 0;
}