2 * tel-plugin-socket-communicator
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <glib-object.h>
29 #define DEFAULT_MENU_MENU "m"
30 #define DEFAULT_MENU_PREV "p"
31 #define DEFAULT_MENU_QUIT "q"
38 struct menu_data *menu;
47 char key_buffer[MENU_DATA_SIZE];
49 static void _show_prompt()
54 static void _show_reserved_menu()
56 msg(ANSI_COLOR_DARKGRAY "--------------------------------------------" ANSI_COLOR_NORMAL);
57 msg(" [%3s] Previous menu ", DEFAULT_MENU_PREV);
58 msg(" [%3s] Show Menu ", DEFAULT_MENU_MENU);
59 msg(" [%3s] Quit ", DEFAULT_MENU_QUIT);
62 static void _show_input_ok()
67 static void _show_menu(MManager *m, struct menu_data menu[])
71 struct menu_data *item;
77 msg("============================================");
78 len = g_queue_get_length(m->title_stack);
79 msgn(ANSI_COLOR_YELLOW " Main");
81 for (i = 0; i < len; i++) {
82 msgn(ANSI_COLOR_NORMAL " >> " ANSI_COLOR_YELLOW "%s", (char *)g_queue_peek_nth(m->title_stack, i));
85 msg(ANSI_COLOR_NORMAL);
86 msg("--------------------------------------------");
91 if (item->key == NULL)
94 //msgn(" [%p][%3s] ", item, item->key);
95 msgn(" [%3s] ", item->key);
97 msg("%s " ANSI_COLOR_LIGHTBLUE "(%s)" ANSI_COLOR_NORMAL, item->title, item->data);
100 msg("%s", item->title);
106 _show_reserved_menu();
108 msg("============================================");
113 static void _show_item_data_input_msg(struct menu_data *item)
116 msg("============================================");
117 msg(" Input [%s] data ", item->title);
118 msg("--------------------------------------------");
119 msg(" current = [%s]", item->data);
123 static void _move_menu(MManager *mm, struct menu_data menu[], char *key)
125 struct menu_data *item;
131 if (!g_strcmp0(DEFAULT_MENU_PREV, key)) {
132 if (g_queue_get_length(mm->stack) > 0) {
133 mm->menu = g_queue_pop_tail(mm->stack);
134 g_queue_pop_tail(mm->title_stack);
136 _show_menu(mm, mm->menu);
137 mm->buf = key_buffer;
139 else if (!g_strcmp0(DEFAULT_MENU_MENU, key)) {
140 _show_menu(mm, mm->menu);
142 else if (!g_strcmp0(DEFAULT_MENU_QUIT, key)) {
143 g_main_loop_quit(mm->mainloop);
148 if (item->key == NULL)
151 if (!g_strcmp0(item->key, key)) {
152 if (item->sub_menu) {
153 g_queue_push_tail(mm->stack, mm->menu);
154 g_queue_push_tail(mm->title_stack, item->title);
156 mm->menu = item->sub_menu;
157 _show_menu(mm, mm->menu);
158 mm->buf = key_buffer;
162 item->callback(mm, item);
165 _show_item_data_input_msg(item);
166 mm->buf = item->data;
177 MManager *menu_manager_new(struct menu_data items[], GMainLoop *mainloop)
181 mm = calloc(sizeof(struct menu_manager), 1);
182 mm->stack = g_queue_new();
183 mm->title_stack = g_queue_new();
185 mm->mainloop = mainloop;
190 int menu_manager_run(MManager *mm)
192 _show_menu(mm, mm->menu);
194 mm->buf = key_buffer;
199 int menu_manager_set_user_data(MManager *mm, void *user_data)
204 mm->user_data = user_data;
209 void *menu_manager_ref_user_data(MManager *mm)
214 return mm->user_data;
217 gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, gpointer data)
220 char local_buf[MENU_DATA_SIZE + 1] = {
223 fgets(local_buf, MENU_DATA_SIZE, stdin);
225 if (strlen(local_buf) > 0) {
226 if (local_buf[strlen(local_buf) - 1] == '\n')
227 local_buf[strlen(local_buf) - 1] = '\0';
230 if (mm->buf == key_buffer) {
231 if (strlen(local_buf) < 1) {
236 _move_menu(mm, mm->menu, local_buf);
240 memset(mm->buf, 0, MENU_DATA_SIZE);
241 memcpy(mm->buf, local_buf, MENU_DATA_SIZE);
244 mm->buf = key_buffer;
245 _move_menu(mm, mm->menu, DEFAULT_MENU_MENU);