Tizen 2.1 base
[platform/core/multimedia/libmm-wfd.git] / proxy / mm_wfd_proxy_test.c
1 /*
2  * libmm-wfd
3  *
4  * Copyright (c) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, ByungWook Jang <bw.jang@samsung.com>,
7  * Manoj Kumar K <manojkumar.k@samsung.com>, Hyunil Park <hyunil46.park@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 #include <glib.h>
23 #include <mm_types.h>
24 #include <mm_error.h>
25 #include <mm_message.h>
26 #include <mm_wfd_proxy.h>
27 #include <mm_ta.h>
28 #include <dlfcn.h>
29 #include <utilX.h>
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/Xmd.h>
34
35 #define PACKAGE "mm_wfd_proxy_test"
36
37 #if defined(_USE_GMAINLOOP)
38 GMainLoop *g_loop;
39 #endif
40
41 #define MAX_STRING_LEN    2048
42
43 enum
44 {
45   CURRENT_STATUS_MAINMENU,
46   CURRENT_STATUS_SERVER_IP,
47   CURRENT_STATUS_SERVER_PORT,
48   CURRENT_STATUS_CONNECT,
49 };
50
51 static MMHandleType g_wfd = 0;
52 gboolean quit_pushing;
53 static int g_current_state = WFDSRC_STATE_NULL;
54
55 static void wfd_start();
56 static void wfd_stop();
57 static void wfd_destroy ();
58 static void set_display_mode(char *mode);
59
60 gboolean timeout_quit_program(void* data);
61 void quit_program();
62
63 static void wfd_proxy_callback(MMHandleType pHandle,
64                                    WfdSrcProxyRet error_code,
65                                    WfdSrcProxyState state,
66                                    void *user_data)
67 {
68   if(error_code == WFDSRC_ERROR_NONE)
69   {
70     if(g_current_state != state)
71     {
72       g_print("miracast server STATE changed from %d --> %d \n",g_current_state, state);
73       g_current_state = state;
74     }
75     return;
76   }
77   g_print("Miracast server notified error %d", error_code);
78   switch (state) {
79     case WFDSRC_STATE_NULL:
80     case WFDSRC_STATE_READY:
81     case WFDSRC_STATE_CONNECTION_WAIT:
82     case WFDSRC_STATE_CONNECTED:
83     {
84       wfd_destroy();
85       quit_program();
86     }break;
87     case WFDSRC_STATE_PLAYING:
88     case WFDSRC_STATE_PAUSED:
89     {
90       wfd_stop();
91       wfd_destroy();
92       quit_program();
93     }break;
94     case WFDSRC_STATE_NONE:
95     default:
96     {
97       wfd_destroy();
98       quit_program();
99     }break;
100   }
101   return;
102 }
103
104 static void wfd_input_server_ip_and_port(char* serverIP, char* port)
105 {
106   if (WfdSrcProxySetIPAddrAndPort(g_wfd, serverIP, port) != MM_ERROR_NONE)
107   {
108     g_print ("wfd_input_server_ip_and_port Failed to set attributes");
109     quit_program();
110     return;
111   }
112 }
113
114 static void wfd_connect()
115 {
116   if (WfdSrcProxyConnect(g_wfd) != MM_ERROR_NONE)
117   {
118     g_print ("wfd_start Failed to start WFD");
119     wfd_destroy();
120     quit_program();
121     return;
122   }
123 }
124
125 static void wfd_start()
126 {
127   if (WfdSrcProxyStart(g_wfd) != MM_ERROR_NONE)
128   {
129     g_print ("wfd_start Failed to start WFD");
130     wfd_destroy();
131     quit_program();
132     return;
133   }
134 }
135
136 static void wfd_stop()
137 {
138   int ret = MM_ERROR_NONE;
139
140   ret = WfdSrcProxyStop(g_wfd);
141   if (ret != MM_ERROR_NONE)
142   {
143     g_print ("wfd_stop Error to do stop...\n");
144     wfd_destroy();
145     quit_program();
146     return;
147   }
148 }
149
150 static void wfd_pause()
151 {
152   int ret = MM_ERROR_NONE;
153
154   ret = WfdSrcProxyPause(g_wfd);
155   if (ret != MM_ERROR_NONE)
156   {
157     g_print ("wfd_pause Error to do pause...\n");
158     quit_program();
159     return;
160   }
161 }
162
163 static void wfd_resume()
164 {
165   int ret = MM_ERROR_NONE;
166
167   ret = WfdSrcProxyResume(g_wfd);
168   if (ret != MM_ERROR_NONE)
169   {
170     g_print ("wfd_resume Error to do resume...\n");
171     quit_program();
172     return;
173   }
174 }
175
176 void quit_program()
177 {
178   MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_STDOUT);
179   MMTA_RELEASE();
180
181   WfdSrcProxyDeInit(g_wfd);
182   g_wfd = 0;
183   g_main_loop_quit(g_loop);
184 }
185
186 void wfd_destroy()
187 {
188   WfdSrcProxyDestroyServer(g_wfd);
189   g_print("wfd_destroy wfd server is destroyed.\n");
190 }
191
192 static void set_display_mode(char *mode)
193 {
194         int len = strlen(mode);
195         int err;
196         int ret = MM_ERROR_NONE;
197         static Atom g_sc_status_atom = 0;
198         static Display *dpy = NULL;
199         int dispmode = UTILX_SCRNCONF_DISPMODE_NULL;
200
201         if ( len < 0 || len > MAX_STRING_LEN )
202                 return;
203         dpy = XOpenDisplay(NULL);
204
205         if (!dpy) {
206         g_printf ("Error : fail to open display\n");
207         return;
208         }
209         g_sc_status_atom = XInternAtom (dpy, STR_ATOM_SCRNCONF_STATUS, False);
210        XSelectInput (dpy, RootWindow(dpy, 0), StructureNotifyMask);
211
212         g_printf("%s", mode);
213         if (strncmp(mode, "a", 1) == 0) {
214                 dispmode = UTILX_SCRNCONF_DISPMODE_CLONE;
215         }
216         else if (strncmp(mode, "b", 1) == 0) {
217                 dispmode = UTILX_SCRNCONF_DISPMODE_EXTENDED;
218         }
219         if (!utilx_scrnconf_set_dispmode (dpy, dispmode)) {
220                 g_printf ("fail to set dispmode\n");
221         }
222         g_printf("set dispmode : %d", dispmode);
223 }
224
225 static void displaymenu()
226 {
227   g_print("\n");
228   g_print("=========================================================================================\n");
229   g_print("                          MM-WFD Testsuite (press q to quit) \n");
230   g_print("-----------------------------------------------------------------------------------------\n");
231   g_print("-----------------------------------------------------------------------------------------\n");
232   g_print("a : a ip port\t");
233   g_print("c : Connect\t");
234   g_print("d : displaymode (a:clone b:desktop) \t");
235   g_print("s : Start  \t");
236   g_print("p : pause \t");
237   g_print("r : resume \t");
238   g_print("e : stop\t");
239   g_print("u : destroy server\t");
240   g_print("q : quit\t");
241   g_print("\n");
242   g_print("=========================================================================================\n");
243 }
244
245 gboolean timeout_menu_display(void* data)
246 {
247   displaymenu();
248   return FALSE;
249 }
250
251 gboolean timeout_quit_program(void* data)
252 {
253   quit_program();
254   return FALSE;
255 }
256
257 static void interpret (char *cmd)
258 {
259   //if ( strlen(cmd) == 1 )
260   {
261     if (strncmp(cmd, "a", 1) == 0)
262     {
263       gchar **IP_port;
264       g_print ("Input server IP and port number..\n\n");
265       IP_port = g_strsplit(cmd," ",0);
266       wfd_input_server_ip_and_port(IP_port[1], IP_port[2]);
267     }
268     else if (strncmp(cmd, "c", 1) == 0)
269     {
270       g_print ("Connecting... WFD..\n\n");
271       wfd_connect();
272     }
273     else if (strncmp(cmd, "d", 1) == 0)
274     {
275       gchar **Display_mode;
276       Display_mode = g_strsplit(cmd," ",0);
277       g_print ("set display mode\n\n");
278       set_display_mode(Display_mode[1]);
279     }
280     else if (strncmp(cmd, "s", 1) == 0)
281     {
282       g_print ("Starting... WFD..\n\n");
283       wfd_start();
284     }
285     else if (strncmp(cmd, "p", 1) == 0)
286     {
287       g_print ("PAUSING..\n\n");
288       wfd_pause();
289     }
290     else if (strncmp(cmd, "r", 1) == 0)
291     {
292       g_print ("RESUMING..\n\n");
293       wfd_resume();
294     }
295     else if (strncmp(cmd, "e", 1) == 0)
296     {
297       wfd_stop();
298     }
299     else if (strncmp(cmd, "u", 1) == 0)
300     {
301       wfd_destroy();
302     }
303     else if (strncmp(cmd, "q", 1) == 0)
304     {
305       quit_pushing = TRUE;
306       quit_program();
307     }
308     else
309     {
310       g_print("unknown menu \n");
311     }
312   }
313   g_timeout_add(100, timeout_menu_display, 0);
314 }
315
316 gboolean input (GIOChannel *channel)
317 {
318     char buf[MAX_STRING_LEN + 3];
319     gsize read;
320
321     g_io_channel_read(channel, buf, MAX_STRING_LEN, &read);
322     buf[read] = '\0';
323     g_strstrip(buf);
324     interpret (buf);
325     return TRUE;
326 }
327
328 int main(int argc, char *argv[])
329 {
330   int ret = 0;
331   GIOChannel *stdin_channel;
332   MMTA_INIT();
333   WfdSrcProxyInit( &g_wfd, wfd_proxy_callback, NULL);
334   stdin_channel = g_io_channel_unix_new(0);
335   g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
336
337   displaymenu();
338
339 #if defined(_USE_GMAINLOOP)
340   g_print (" <<<< GTK Loop >>>>");
341   g_loop = g_main_loop_new(NULL, FALSE);
342   g_main_loop_run(g_loop);
343   return 0;
344 #endif
345
346 }