BugFix: Package test script.
[profile/ivi/ico-uxf-device-input-controller.git] / tests / test-common.c
1 /*
2  * Copyright © 2013 TOYOTA MOTOR CORPORATION
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 /**
23  * @brief   Uint test common routines
24  *
25  * @date    Feb-08-2013
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <wayland-client.h>
38 #include "ico_window_mgr-client-protocol.h"
39 #include "test-common.h"
40
41 /*--------------------------------------------------------------------------*/
42 /**
43  * @brief   getdata: Input data string
44  *
45  * @param[in]   window_mgr  ico_window_mgr, if Null, output to stderr
46  * @param[in]   prompt      Echoback printout header
47  * @param[in]   fd          Input file discriptor
48  * @param[in]   buf         Input buffer
49  * @param[in]   size        Buffer size
50  * @return      Number of input characters
51  * @retval      >= 0        Number of input characters
52  * @retval      < 0         Input error
53  */
54 /*--------------------------------------------------------------------------*/
55 int
56 getdata(void *window_mgr, const char *prompt, int fd, char *buf, const int size)
57 {
58     int     ret;
59     int     i, j;
60
61     j = -1;
62     for (i = 0; i < (size-1); i++)  {
63         ret = read(fd, &buf[i], 1);
64
65         if (ret < 0)    {
66             return ret;
67         }
68
69         if ((buf[i] == '\n') || (buf[i] == '\r'))   break;
70         if (buf[i] == '\t') {
71             buf[i] = ' ';
72         }
73         if ((buf[i] == '#') && (j < 0)) {
74             j = i;
75         }
76     }
77     buf[i] = 0;
78     print_log("%s%s", prompt, buf);
79
80     /* Delete trailing spaces       */
81     if (j >= 0) {
82         for (; j > 0; j--)  {
83             if (buf[j-1] != ' ')    break;
84         }
85         buf[j] = 0;
86         i = j;
87     }
88
89     /* Delete header spaces         */
90     for (j = 0; buf[j]; j++)    {
91         if (buf[j] != ' ')  break;
92     }
93     if (j > 0)  {
94         strcpy( buf, &buf[j] );
95         i -= j;
96     }
97     return i;
98 }
99
100 /*--------------------------------------------------------------------------*/
101 /**
102  * @brief   print_log: Weston log output
103  *
104  * @param[in]   fmt         printf format
105  * @param[in]   ...         printf arguments
106  * @return      None
107  */
108 /*--------------------------------------------------------------------------*/
109 void
110 print_log(const char *fmt, ...)
111 {
112     va_list     ap;
113     char        log[128];
114     struct timeval  NowTime;
115     extern long timezone;
116     static int  sTimeZone = (99*60*60);
117
118     va_start(ap, fmt);
119     vsnprintf(log, sizeof(log)-2, fmt, ap);
120     va_end(ap);
121
122     gettimeofday( &NowTime, (struct timezone *)0 );
123     if( sTimeZone > (24*60*60) )    {
124         tzset();
125         sTimeZone = timezone;
126     }
127     NowTime.tv_sec -= sTimeZone;
128     fprintf(stderr, "[%02d:%02d:%02d.%03d@%d] %s\n", (int)((NowTime.tv_sec/3600) % 24),
129             (int)((NowTime.tv_sec/60) % 60), (int)(NowTime.tv_sec % 60),
130             (int)NowTime.tv_usec/1000, getpid(), log);
131 }
132
133 /*--------------------------------------------------------------------------*/
134 /**
135  * @brief   skip_spaces: skip spaces in command input
136  *
137  * @param[in]   buf         input command string
138  * @return      character pointer of not space character
139  */
140 /*--------------------------------------------------------------------------*/
141 char *
142 skip_spaces(char *buf)
143 {
144     while ((*buf == ' ') || (*buf == '\t')) {
145         buf++;
146     }
147     return buf;
148 }
149
150 /*--------------------------------------------------------------------------*/
151 /**
152  * @brief   pars_command: The word division of the command line
153  *
154  * @param[in]   buf         input command string
155  * @param[in]   pt          word pointers (maximum len words)
156  * @param[in]   len         maximum number of words
157  * @return      number of words
158  */
159 /*--------------------------------------------------------------------------*/
160 int
161 pars_command(char *buf, char *pt[], const int len)
162 {
163     char    *p;
164     int     narg;
165
166     memset(pt, 0, sizeof(int *)*10);
167     p = buf;
168     for (narg = 0; narg < len; narg++)  {
169         p = skip_spaces(p);
170         if (*p == 0)    break;
171         pt[narg] = p;
172         for (; *p; p++) {
173             if ((*p == ' ') || (*p == '\t') ||
174                 (*p == '=') || (*p == ',')) break;
175         }
176         if (*p == 0)    {
177             narg++;
178             break;
179         }
180         *p = 0;
181         p++;
182     }
183     return narg;
184 }
185
186 /*--------------------------------------------------------------------------*/
187 /**
188  * @brief   wayland_dispatch_nonblock: Read from wayland if receive data exist
189  *
190  * @param[in]   display     Wayland connection
191  * @return      None
192  */
193 /*--------------------------------------------------------------------------*/
194 void
195 wayland_dispatch_nonblock(struct wl_display *display)
196 {
197     int     nread;
198
199     /* Check wayland input      */
200     do  {
201         /* Flush send data          */
202         wl_display_flush(display);
203
204         nread = 0;
205         if (ioctl(wl_display_get_fd(display), FIONREAD, &nread) < 0)    {
206             nread = 0;
207         }
208         if (nread >= 8) {
209             /* Read event from wayland  */
210             wl_display_dispatch(display);
211         }
212     } while (nread > 0);
213 }
214
215 /*--------------------------------------------------------------------------*/
216 /**
217  * @brief   sleep_with_wayland: Sleep and receive wayland event
218  *
219  * @param[in]   display     Wayland connection
220  * @param[in]   msec        Sleep time (miri-sec)
221  * @return      None
222  */
223 /*--------------------------------------------------------------------------*/
224 void
225 sleep_with_wayland(struct wl_display *display, int msec)
226 {
227     int     nread;
228     int     fd;
229
230
231     fd = wl_display_get_fd(display);
232
233     do  {
234         /* Flush send data          */
235         wl_display_flush(display);
236
237         /* Check wayland input      */
238         nread = 0;
239         if (ioctl(fd, FIONREAD, &nread) < 0)    {
240             nread = 0;
241         }
242         if (nread >= 8) {
243             /* Read event from wayland  */
244             wl_display_dispatch(display);
245         }
246         msec -= 20;
247         if (msec >= 0)   usleep(20*1000);
248     } while (msec > 0);
249 }
250
251 /*--------------------------------------------------------------------------*/
252 /**
253  * @brief   wait_with_wayland: Wait for end and receive wayland event
254  *
255  * @param[in]   display     Wayland connection
256  * @param[in]   msec        Maximum wait time (miri-sec)
257  * @param[in]   endflag     End flag address
258  * @return      None
259  */
260 /*--------------------------------------------------------------------------*/
261 void
262 wait_with_wayland(struct wl_display *display, int msec, int *endflag)
263 {
264     int     nread;
265     int     fd;
266
267     fd = wl_display_get_fd(display);
268
269     do  {
270         /* Check wayland input      */
271         while(1)    {
272             /* Flush send data          */
273             wl_display_flush(display);
274
275             nread = 0;
276             if (ioctl(fd, FIONREAD, &nread) < 0)    {
277                 nread = 0;
278             }
279             if (nread < 8)  break;
280             /* Read event from wayland  */
281             wl_display_dispatch(display);
282         }
283         msec -= 20;
284         if (msec >= 0)   usleep(20*1000);
285     } while ((*endflag == 0) && (msec > 0));
286 }
287
288 /*--------------------------------------------------------------------------*/
289 /**
290  * @brief   sec_str_2_value: Convert seconds string to value
291  *
292  * @param[in]   ssec        Time ("sec.msec")
293  * @return      miri-second
294  */
295 /*--------------------------------------------------------------------------*/
296 int
297 sec_str_2_value(const char *ssec)
298 {
299     int     sec;
300     int     msec;
301     int     n;
302     char    *errp = NULL;
303
304     sec = strtol(ssec, &errp, 0) * 1000;
305     if ((errp != NULL) && (*errp == '.'))   {
306         msec = 0;
307         n = 0;
308         for (errp++; *errp; errp++) {
309             if ((*errp < '0') || (*errp > '9')) break;
310             msec = msec * 10 + *errp - '0';
311             n++;
312             if (n >= 3) break;
313         }
314         if (n == 1)     msec *= 100;
315         if (n == 2)     msec *= 10;
316         sec += msec;
317     }
318     return sec;
319 }
320
321 /*--------------------------------------------------------------------------*/
322 /**
323  * @brief   opengl_init: Initialize OpenGL ESv2/EGL
324  *
325  * @param[in]   display     Wayland connection
326  * @param[out]  rconf       EGL configuration
327  * @param[out]  rctx        EGL context
328  * @return      EGL display
329  * @retval      != NULL     EGL display
330  * @retval      == NULL     OpenGL/EGL initialize error
331  */
332 /*--------------------------------------------------------------------------*/
333 EGLDisplay
334 opengl_init(struct wl_display *display, EGLConfig *rconf, EGLContext *rctx)
335 {
336     EGLDisplay  dpy;                    /* EGL dsplay id                    */
337     EGLint      major, minor;
338     EGLint      num_configs;
339     EGLConfig   conf = 0;
340     EGLContext  ctx;
341
342     static const EGLint config_attribs[] = {
343         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
344         EGL_RED_SIZE, 1,
345         EGL_GREEN_SIZE, 1,
346         EGL_BLUE_SIZE, 1,
347         EGL_ALPHA_SIZE, 1,
348         EGL_DEPTH_SIZE, 1,
349         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
350         EGL_NONE
351     };
352     static const EGLint context_attribs[] = {
353         EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
354     };
355
356     dpy = eglGetDisplay((EGLNativeDisplayType)display);
357     if (! dpy)  {
358         fprintf(stderr, "eglGetDisplay Error\n");
359         return NULL;
360     }
361
362     if (eglInitialize(dpy, &major, &minor) == EGL_FALSE)    {
363         fprintf(stderr, "eglInitialize Error\n");
364         return NULL;
365     }
366
367     if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
368         fprintf(stderr, "eglBindAPI Error\n");
369         return NULL;
370     }
371
372     if (eglChooseConfig(dpy, config_attribs, &conf, 1, &num_configs) == EGL_FALSE)  {
373         fprintf(stderr, "eglChooseConfig Error\n");
374         return NULL;
375     }
376
377     ctx = eglCreateContext(dpy, conf, EGL_NO_CONTEXT, context_attribs);
378     if (! ctx)  {
379         fprintf(stderr, "eglCreateContext Error\n");
380         return NULL;
381     }
382     *rconf = conf;
383     *rctx = ctx;
384
385     wayland_dispatch_nonblock(display);
386
387     return dpy;
388 }
389
390 /*--------------------------------------------------------------------------*/
391 /**
392  * @brief   opengl_create_window: Create OpenGL/EGL window
393  *
394  * @param[in]   display     Wayland connection
395  * @param[in]   surface     Wayland surface
396  * @param[in]   dpy         EGL display
397  * @param[in]   conf        EGL configuration
398  * @param[in]   ctx         EGL context
399  * @param[in]   width       Widown width
400  * @param[in]   height      Window height
401  * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
402  * @return      EGL surface
403  * @retval      != NULL     EGL surface
404  * @retval      == NULL     Create window error
405  */
406 /*--------------------------------------------------------------------------*/
407 EGLSurface
408 opengl_create_window(struct wl_display *display, struct wl_surface *surface,
409                      EGLDisplay dpy, EGLConfig conf, EGLContext ctx,
410                      const int width, const int height, const int color)
411 {
412     struct wl_egl_window    *egl_window;
413     EGLSurface              egl_surface;
414
415     static const EGLint surface_attribs[] = {
416         EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE, EGL_NONE
417     };
418
419     egl_window = wl_egl_window_create(surface, width, height);
420     egl_surface = eglCreateWindowSurface(dpy, conf, (EGLNativeWindowType)egl_window,
421                                          surface_attribs);
422     eglMakeCurrent(dpy, egl_surface, egl_surface, ctx);
423     glViewport(0, 0, width, height);
424
425     wayland_dispatch_nonblock(display);
426
427     opengl_clear_window(color);
428
429     opengl_swap_buffer(display, dpy, egl_surface);
430
431     return egl_surface;
432 }
433
434 /*--------------------------------------------------------------------------*/
435 /**
436  * @brief   opengl_clear_window: OpenGL window clear
437  *
438  * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
439  * @return      None
440  */
441 /*--------------------------------------------------------------------------*/
442 void
443 opengl_clear_window(const unsigned int color)
444 {
445     double                  r, g, b, a;
446
447     r = (double)((color>>16) & 0x0ff);
448     r = r / 256.0;
449     g = (double)((color>>8) & 0x0ff);
450     g = g / 256.0;
451     b = (double)(color & 0x0ff);
452     b = b / 256.0;
453     a = (double)((color>>24) & 0x0ff);
454     a = (a + 1.0) / 256.0;
455
456     glClearColor(r, g, b, a);
457     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
458 }
459
460 /*--------------------------------------------------------------------------*/
461 /**
462  * @brief   opengl_swap_buffer: Swap OpenGL/EGL buffer
463  *
464  * @param[in]   display     Wayland connection
465  * @param[in]   dpy         EGL display
466  * @param[in]   egl_surface EGL surface
467  * @return      None
468  */
469 /*--------------------------------------------------------------------------*/
470 void
471 opengl_swap_buffer(struct wl_display *display, EGLDisplay dpy, EGLSurface egl_surface)
472 {
473     eglSwapBuffers(dpy, egl_surface);
474
475     wayland_dispatch_nonblock(display);
476 }
477