Bug fix TIVI-976 ,TIVI-974 and addition of the window animation interface.
[profile/ivi/ico-uxf-weston-plugin.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   wayland_dispatch_nonblock: Read from wayland if receive data exist
136  *
137  * @param[in]   display     Wayland connection
138  * @return      None
139  */
140 /*--------------------------------------------------------------------------*/
141 void
142 wayland_dispatch_nonblock(struct wl_display *display)
143 {
144     int     nread;
145
146     /* Check wayland input      */
147     do  {
148         /* Flush send data          */
149         wl_display_flush(display);
150
151         nread = 0;
152         if (ioctl(wl_display_get_fd(display), FIONREAD, &nread) < 0)    {
153             nread = 0;
154         }
155         if (nread >= 8) {
156             /* Read event from wayland  */
157             wl_display_dispatch(display);
158         }
159     } while (nread > 0);
160 }
161
162 /*--------------------------------------------------------------------------*/
163 /**
164  * @brief   sleep_with_wayland: Sleep and receive wayland event
165  *
166  * @param[in]   display     Wayland connection
167  * @param[in]   msec        Sleep time (miri-sec)
168  * @return      None
169  */
170 /*--------------------------------------------------------------------------*/
171 void
172 sleep_with_wayland(struct wl_display *display, int msec)
173 {
174     int     nread;
175     int     fd;
176
177
178     fd = wl_display_get_fd(display);
179
180     do  {
181         /* Flush send data          */
182         wl_display_flush(display);
183
184         /* Check wayland input      */
185         nread = 0;
186         if (ioctl(fd, FIONREAD, &nread) < 0)    {
187             nread = 0;
188         }
189         if (nread >= 8) {
190             /* Read event from wayland  */
191             wl_display_dispatch(display);
192         }
193         msec -= 20;
194         if (msec >= 0)   usleep(20*1000);
195     } while (msec > 0);
196 }
197
198 /*--------------------------------------------------------------------------*/
199 /**
200  * @brief   wait_with_wayland: Wait for end and receive wayland event
201  *
202  * @param[in]   display     Wayland connection
203  * @param[in]   msec        Maximum wait time (miri-sec)
204  * @param[in]   endflag     End flag address
205  * @return      None
206  */
207 /*--------------------------------------------------------------------------*/
208 void
209 wait_with_wayland(struct wl_display *display, int msec, int *endflag)
210 {
211     int     nread;
212     int     fd;
213
214     fd = wl_display_get_fd(display);
215
216     do  {
217         /* Flush send data          */
218         wl_display_flush(display);
219
220         /* Check wayland input      */
221         nread = 0;
222         if (ioctl(fd, FIONREAD, &nread) < 0)    {
223             nread = 0;
224         }
225         if (nread >= 8) {
226             /* Read event from wayland  */
227             wl_display_dispatch(display);
228         }
229         msec -= 20;
230         if (msec >= 0)   usleep(20*1000);
231     } while ((*endflag == 0) && (msec > 0));
232 }
233
234 /*--------------------------------------------------------------------------*/
235 /**
236  * @brief   sec_str_2_value: Convert seconds string to value
237  *
238  * @param[in]   ssec        Time ("sec.msec")
239  * @return      miri-second
240  */
241 /*--------------------------------------------------------------------------*/
242 int
243 sec_str_2_value(const char *ssec)
244 {
245     int     sec;
246     int     msec;
247     int     n;
248     char    *errp = NULL;
249
250     sec = strtol(ssec, &errp, 0) * 1000;
251     if ((errp != NULL) && (*errp == '.'))   {
252         msec = 0;
253         n = 0;
254         for (errp++; *errp; errp++) {
255             if ((*errp < '0') || (*errp > '9')) break;
256             msec = msec * 10 + *errp - '0';
257             n++;
258             if (n >= 3) break;
259         }
260         if (n == 1)     msec *= 100;
261         if (n == 2)     msec *= 10;
262         sec += msec;
263     }
264     return(sec);
265 }
266
267 /*--------------------------------------------------------------------------*/
268 /**
269  * @brief   opengl_init: Initialize OpenGL ESv2/EGL
270  *
271  * @param[in]   display     Wayland connection
272  * @param[out]  rconf       EGL configuration
273  * @param[out]  rctx        EGL context
274  * @return      EGL display
275  * @retval      != NULL     EGL display
276  * @retval      == NULL     OpenGL/EGL initialize error
277  */
278 /*--------------------------------------------------------------------------*/
279 EGLDisplay
280 opengl_init(struct wl_display *display, EGLConfig *rconf, EGLContext *rctx)
281 {
282     EGLDisplay  dpy;                    /* EGL dsplay id                    */
283     EGLint      major, minor;
284     EGLint      num_configs;
285     EGLConfig   conf = 0;
286     EGLContext  ctx;
287
288     static const EGLint config_attribs[] = {
289         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
290         EGL_RED_SIZE, 1,
291         EGL_GREEN_SIZE, 1,
292         EGL_BLUE_SIZE, 1,
293         EGL_ALPHA_SIZE, 1,
294         EGL_DEPTH_SIZE, 1,
295         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
296         EGL_NONE
297     };
298     static const EGLint context_attribs[] = {
299         EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
300     };
301
302     dpy = eglGetDisplay((EGLNativeDisplayType)display);
303     if (! dpy)  {
304         fprintf(stderr, "eglGetDisplay Error\n");
305         return NULL;
306     }
307
308     if (eglInitialize(dpy, &major, &minor) == EGL_FALSE)    {
309         fprintf(stderr, "eglInitialize Error\n");
310         return NULL;
311     }
312
313     if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
314         fprintf(stderr, "eglBindAPI Error\n");
315         return NULL;
316     }
317
318     if (eglChooseConfig(dpy, config_attribs, &conf, 1, &num_configs) == EGL_FALSE)  {
319         fprintf(stderr, "eglChooseConfig Error\n");
320         return NULL;
321     }
322
323     ctx = eglCreateContext(dpy, conf, EGL_NO_CONTEXT, context_attribs);
324     if (! ctx)  {
325         fprintf(stderr, "eglCreateContext Error\n");
326         return NULL;
327     }
328     *rconf = conf;
329     *rctx = ctx;
330
331     wayland_dispatch_nonblock(display);
332
333     return(dpy);
334 }
335
336 /*--------------------------------------------------------------------------*/
337 /**
338  * @brief   opengl_create_window: Create OpenGL/EGL window
339  *
340  * @param[in]   display     Wayland connection
341  * @param[in]   surface     Wayland surface
342  * @param[in]   dpy         EGL display
343  * @param[in]   conf        EGL configuration
344  * @param[in]   ctx         EGL context
345  * @param[in]   width       Widown width
346  * @param[in]   height      Window height
347  * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
348  * @return      EGL surface
349  * @retval      != NULL     EGL surface
350  * @retval      == NULL     Create window error
351  */
352 /*--------------------------------------------------------------------------*/
353 EGLSurface
354 opengl_create_window(struct wl_display *display, struct wl_surface *surface,
355                      EGLDisplay dpy, EGLConfig conf, EGLContext ctx,
356                      const int width, const int height, const int color)
357 {
358     struct wl_egl_window    *egl_window;
359     EGLSurface              egl_surface;
360
361     static const EGLint surface_attribs[] = {
362         EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE, EGL_NONE
363     };
364
365     egl_window = wl_egl_window_create(surface, width, height);
366     egl_surface = eglCreateWindowSurface(dpy, conf, (EGLNativeWindowType)egl_window,
367                                          surface_attribs);
368     eglMakeCurrent(dpy, egl_surface, egl_surface, ctx);
369     glViewport(0, 0, width, height);
370
371     wayland_dispatch_nonblock(display);
372
373     opengl_clear_window(color);
374
375     opengl_swap_buffer(display, dpy, egl_surface);
376
377     return(egl_surface);
378 }
379
380 /*--------------------------------------------------------------------------*/
381 /**
382  * @brief   opengl_clear_window: OpenGL window clear
383  *
384  * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
385  * @return      None
386  */
387 /*--------------------------------------------------------------------------*/
388 void
389 opengl_clear_window(const unsigned int color)
390 {
391     double                  r, g, b, a;
392
393     r = (double)((color>>16) & 0x0ff);
394     r = r / 256.0;
395     g = (double)((color>>8) & 0x0ff);
396     g = g / 256.0;
397     b = (double)(color & 0x0ff);
398     b = b / 256.0;
399     a = (double)((color>>24) & 0x0ff);
400     a = (a + 1.0) / 256.0;
401
402     glClearColor(r, g, b, a);
403     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
404 }
405
406 /*--------------------------------------------------------------------------*/
407 /**
408  * @brief   opengl_create_window: Create OpenGL/EGL window
409  *
410  * @param[in]   display     Wayland connection
411  * @param[in]   dpy         EGL display
412  * @param[in]   egl_surface EGL surface
413  * @return      None
414  */
415 /*--------------------------------------------------------------------------*/
416 void
417 opengl_swap_buffer(struct wl_display *display, EGLDisplay dpy, EGLSurface egl_surface)
418 {
419     eglSwapBuffers(dpy, egl_surface);
420
421     wayland_dispatch_nonblock(display);
422 }
423