Initial release of status lib(tagging)
[framework/appfw/status.git] / test / monitor_test.c
1 /*
2  *  libstatus
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
7  *      Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <glib.h>
25 #include <status-monitor.h>
26
27 #define MAX_STRING_LEN  256
28 #define CLEAR_STDIN {gint ch; while((ch=getchar())!=EOF&&ch!='\n'); }
29
30 static GIOChannel *stdin_channel;
31 static GMainLoop *loop;
32
33 typedef void (*menu_cb)(void);
34
35 struct menu_item {
36         gchar *cmd;
37         gchar *desc;
38         menu_cb func;
39 };
40
41 static void _monitor_message_cb(const char *message, void *data)
42 {
43         printf("new message received : %s\n", message);
44 }
45
46 static void _monitor_state_cb(status_background_type_e type,
47                 status_background_state_e state, void *data)
48 {
49         switch (type) {
50         case STATUS_BACKGROUND_TYPE_MUSIC:
51                 printf("music state changed : state[%d]\n", state);
52                 break;
53         case STATUS_BACKGROUND_TYPE_RADIO:
54                 printf("radio state changed : state[%d]\n", state);
55                 break;
56         case STATUS_BACKGROUND_TYPE_VOICE_RECORDING:
57                 printf("voice recorder state changed : state[%d]\n", state);
58                 break;
59         default:
60                 printf("unknown type - [%d] \n", type);
61                 break;
62         }
63
64 }
65
66 static void do_quit(void)
67 {
68         g_main_loop_quit(loop);
69 }
70
71 static void do_message_monitor_set(void)
72 {
73         int ret = 0;
74
75         ret = status_monitor_message_cb_set(_monitor_message_cb, NULL);
76         if (ret != STATUS_ERROR_NONE) {
77                 printf("fail to status_monitor_message_cb_set - %d \n", ret);
78                 return;
79         }
80
81         printf("success to status_monitor_message_cb_set\n");
82 }
83
84 static void do_message_monitor_unset(void)
85 {
86         int ret = 0;
87
88         ret = status_monitor_message_cb_unset();
89         if (ret != STATUS_ERROR_NONE) {
90                 printf("fail to status_monitor_message_cb_unset - %d \n", ret);
91                 return;
92         }
93
94         printf("success to status_monitor_message_cb_unset\n");
95 }
96
97 static int _get_type(status_background_type_e *type)
98 {
99         int input_type;
100         int ret;
101         int pass = 0;
102         do {
103                 g_print("Select type : \n");
104                 g_print("0. Music\n");
105                 g_print("1. Radio\n");
106                 g_print("2. Voice recorder\n");
107                 ret = scanf("%d", &input_type);
108                 if (ret <= 0) {
109                         g_print("invaild input\n\n");
110                         input_type = -1;
111                 }
112                 CLEAR_STDIN;
113
114                 if (input_type >= 0 && input_type <= 2)
115                         pass = 1;
116         } while (pass != 1);
117
118         if (type)
119                 *type = (status_background_type_e)input_type;
120
121         return 0;
122 }
123
124 static void do_state_monitor_set(void)
125 {
126         int ret = 0;
127         status_background_type_e type;
128
129         if (_get_type(&type)) {
130                 g_print("fail to get type \n");
131                 return;
132         }
133
134         ret = status_monitor_state_cb_set(type, _monitor_state_cb, NULL);
135         if (ret != STATUS_ERROR_NONE) {
136                 printf("fail to status_monitor_state_cb_set"\
137                         "- type[%d], err[%d] \n", type, ret);
138                 return;
139         }
140
141         printf("success to status_monitor_state_cb_set - type[%d]\n", type);
142 }
143
144 static void do_state_monitor_unset(void)
145 {
146         int ret = 0;
147         status_background_type_e type;
148
149         if (_get_type(&type)) {
150                 g_print("fail to get type \n");
151                 return;
152         }
153
154         ret = status_monitor_state_cb_unset(type);
155         if (ret != STATUS_ERROR_NONE) {
156                 printf("fail to status_monitor_state_cb_unset"\
157                         "- type[%d], err[%d] \n", type, ret);
158                 return;
159         }
160
161         printf("success to status_monitor_state_cb_unset - type[%d]\n", type);
162 }
163
164 static void do_state_get(void)
165 {
166         int ret = 0;
167         status_background_type_e type;
168         status_background_state_e state = STATUS_BACKGROUND_STATE_STOP;
169
170         if (_get_type(&type)) {
171                 g_print("fail to get type \n");
172                 return;
173         }
174
175         ret = status_monitor_state_get(type, &state);
176         if (ret != STATUS_ERROR_NONE) {
177                 printf("fail to status_monitor_state_get"\
178                         "- type[%d], err[%d] \n", type, ret);
179                 return;
180         }
181         printf("success to status_monitor_state_get \n");
182         printf("type[%d], state[%d]\n", type, state);
183
184 }
185
186 static struct menu_item g_monitor_menu[] = {
187         {"q", "quit", do_quit},
188         {"w", "test message monitor set", do_message_monitor_set},
189         {"e", "test message monitor unset", do_message_monitor_unset},
190         {"s", "test background state monitor set", do_state_monitor_set},
191         {"a", "test background state monitor unset", do_state_monitor_unset},
192         {"g", "test background state get", do_state_get},
193         {NULL, NULL, NULL},
194 };
195
196 static void
197 monitor_display_menu(void)
198 {
199         gint i = 0;
200
201         g_print("\n");
202
203         g_print("select commands \n");
204         while (g_monitor_menu[i].cmd) {
205                 g_print("[%s] %s\n", g_monitor_menu[i].cmd,
206                                 g_monitor_menu[i].desc);
207                 i++;
208         }
209
210         g_print("\n");
211         g_print(">>> ");
212
213 }
214
215 static void
216 _monitor_parse_command(gchar *cmd)
217 {
218         gint i = 0;
219
220         while (g_monitor_menu[i].cmd) {
221                 if (g_strcmp0(g_monitor_menu[i].cmd, cmd) == 0) {
222                         if (g_monitor_menu[i].func)
223                                 g_monitor_menu[i].func();
224                         return;
225                 }
226                 i++;
227         }
228         monitor_display_menu();
229
230 }
231
232
233 static gboolean
234 _monitor_input_cb(GIOChannel *channel)
235 {
236         gchar buf[MAX_STRING_LEN+3];
237         gsize len;
238
239         g_io_channel_read(channel, buf, MAX_STRING_LEN, &len);
240         buf[len] = 0x00;
241         g_strstrip(buf);
242
243         _monitor_parse_command(buf);
244
245         return TRUE;
246
247 }
248
249 int main(int argc, char **argv)
250 {
251         loop = g_main_loop_new(NULL, FALSE);
252
253         stdin_channel = g_io_channel_unix_new (0);
254         g_io_add_watch(stdin_channel, G_IO_IN,
255                         (GIOFunc)_monitor_input_cb, NULL);
256
257         monitor_display_menu();
258
259         g_main_loop_run(loop);
260
261         g_io_channel_unref(stdin_channel);
262         g_main_loop_unref(loop);
263
264         return 0;
265 }
266
267