summaryrefslogtreecommitdiff
path: root/tests/test-common.c
blob: 5b83995e4fcce7b31583e5bc41a5bb74777ace0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
/**
 * @brief   Uint test common routines
 *
 * @date    Feb-08-2013
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <wayland-client.h>
#include <ico_window_mgr-client-protocol.h>
#include "test-common.h"

/*--------------------------------------------------------------------------*/
/**
 * @brief   getdata: Input data string
 *
 * @param[in]   window_mgr  ico_window_mgr, if Null, output to stderr
 * @param[in]   prompt      Echoback printout header
 * @param[in]   fd          Input file discriptor
 * @param[in]   buf         Input buffer
 * @param[in]   size        Buffer size
 * @return      Number of input characters
 * @retval      >= 0        Number of input characters
 * @retval      < 0         Input error
 */
/*--------------------------------------------------------------------------*/
int
getdata(void *window_mgr, const char *prompt, int fd, char *buf, const int size)
{
    int     ret;
    int     i, j;

    j = -1;
    for (i = 0; i < (size-1); i++)  {
        ret = read(fd, &buf[i], 1);

        if (ret < 0)    {
            return(ret);
        }

        if ((buf[i] == '\n') || (buf[i] == '\r'))   break;
        if (buf[i] == '\t') {
            buf[i] = ' ';
        }
        if ((buf[i] == '#') && (j < 0)) {
            j = i;
        }
    }
    buf[i] = 0;
    print_log("%s%s", prompt, buf);

    /* Delete trailing spaces       */
    if (j >= 0) {
        for (; j > 0; j--)  {
            if (buf[j-1] != ' ')    break;
        }
        buf[j] = 0;
        i = j;
    }

    /* Delete header spaces         */
    for (j = 0; buf[j]; j++)    {
        if (buf[j] != ' ')  break;
    }
    if (j > 0)  {
        strcpy( buf, &buf[j] );
        i -= j;
    }
    return(i);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   print_log: Weston log output
 *
 * @param[in]   fmt         printf format
 * @param[in]   ...         printf arguments
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
print_log(const char *fmt, ...)
{
    va_list     ap;
    char        log[128];
    struct timeval  NowTime;
    extern long timezone;
    static int  sTimeZone = (99*60*60);

    va_start(ap, fmt);
    vsnprintf(log, sizeof(log)-2, fmt, ap);
    va_end(ap);

    gettimeofday( &NowTime, (struct timezone *)0 );
    if( sTimeZone > (24*60*60) )    {
        tzset();
        sTimeZone = timezone;
    }
    NowTime.tv_sec -= sTimeZone;
    fprintf(stderr, "[%02d:%02d:%02d.%03d@%d] %s\n",
            (int)((NowTime.tv_sec/3600) % 24),
            (int)((NowTime.tv_sec/60) % 60),
            (int)(NowTime.tv_sec % 60),
            (int)NowTime.tv_usec/1000, getpid(), log);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   wayland_dispatch_nonblock: Read from wayland if receive data exist
 *
 * @param[in]   display     Wayland connection
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
wayland_dispatch_nonblock(struct wl_display *display)
{
    int     nread;

    /* Check wayland input      */
    do  {
        /* Flush send data          */
        wl_display_flush(display);

        nread = 0;
        if (ioctl(wl_display_get_fd(display), FIONREAD, &nread) < 0)    {
            nread = 0;
        }
        if (nread >= 8) {
            /* Read event from wayland  */
            wl_display_dispatch(display);
        }
    } while (nread > 0);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   sleep_with_wayland: Sleep and receive wayland event
 *
 * @param[in]   display     Wayland connection
 * @param[in]   msec        Sleep time (miri-sec)
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
sleep_with_wayland(struct wl_display *display, int msec)
{
    int     nread;
    int     fd;


    fd = wl_display_get_fd(display);

    do  {
        /* Flush send data          */
        wl_display_flush(display);

        /* Check wayland input      */
        nread = 0;
        if (ioctl(fd, FIONREAD, &nread) < 0)    {
            nread = 0;
        }
        if (nread >= 8) {
            /* Read event from wayland  */
            wl_display_dispatch(display);
        }
        msec -= 20;
        if (msec >= 0)   usleep(20*1000);
    } while (msec > 0);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   wait_with_wayland: Wait for end and receive wayland event
 *
 * @param[in]   display     Wayland connection
 * @param[in]   msec        Maximum wait time (miri-sec)
 * @param[in]   endflag     End flag address
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
wait_with_wayland(struct wl_display *display, int msec, int *endflag)
{
    int     nread;
    int     fd;

    fd = wl_display_get_fd(display);

    do  {
        /* Flush send data          */
        wl_display_flush(display);

        /* Check wayland input      */
        nread = 0;
        if (ioctl(fd, FIONREAD, &nread) < 0)    {
            nread = 0;
        }
        if (nread >= 8) {
            /* Read event from wayland  */
            wl_display_dispatch(display);
        }
        msec -= 20;
        if (msec >= 0)   usleep(20*1000);
    } while ((*endflag == 0) && (msec > 0));
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   sec_str_2_value: Convert seconds string to value
 *
 * @param[in]   ssec        Time ("sec.msec")
 * @return      miri-second
 */
/*--------------------------------------------------------------------------*/
int
sec_str_2_value(const char *ssec)
{
    int     sec;
    int     msec;
    int     n;
    char    *errp = NULL;

    sec = strtol(ssec, &errp, 0) * 1000;
    if ((errp != NULL) && (*errp == '.'))   {
        msec = 0;
        n = 0;
        for (errp++; *errp; errp++) {
            if ((*errp < '0') || (*errp > '9')) break;
            msec = msec * 10 + *errp - '0';
            n++;
            if (n >= 3) break;
        }
        if (n == 1)     msec *= 100;
        if (n == 2)     msec *= 10;
        sec += msec;
    }
    return(sec);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   opengl_init: Initialize OpenGL ESv2/EGL
 *
 * @param[in]   display     Wayland connection
 * @param[out]  rconf       EGL configuration
 * @param[out]  rctx        EGL context
 * @return      EGL display
 * @retval      != NULL     EGL display
 * @retval      == NULL     OpenGL/EGL initialize error
 */
/*--------------------------------------------------------------------------*/
EGLDisplay
opengl_init(struct wl_display *display, EGLConfig *rconf, EGLContext *rctx)
{
    EGLDisplay  dpy;                    /* EGL dsplay id                    */
    EGLint      major, minor;
    EGLint      num_configs;
    EGLConfig   conf = 0;
    EGLContext  ctx;

    static const EGLint config_attribs[] = {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_RED_SIZE, 1,
        EGL_GREEN_SIZE, 1,
        EGL_BLUE_SIZE, 1,
        EGL_ALPHA_SIZE, 1,
        EGL_DEPTH_SIZE, 1,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_NONE
    };
    static const EGLint context_attribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
    };

    dpy = eglGetDisplay((EGLNativeDisplayType)display);
    if (! dpy)  {
        fprintf(stderr, "eglGetDisplay Error\n");
        return NULL;
    }

    if (eglInitialize(dpy, &major, &minor) == EGL_FALSE)    {
        fprintf(stderr, "eglInitialize Error\n");
        return NULL;
    }

    if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
        fprintf(stderr, "eglBindAPI Error\n");
        return NULL;
    }

    if (eglChooseConfig(dpy, config_attribs, &conf, 1, &num_configs) == EGL_FALSE)  {
        fprintf(stderr, "eglChooseConfig Error\n");
        return NULL;
    }

    ctx = eglCreateContext(dpy, conf, EGL_NO_CONTEXT, context_attribs);
    if (! ctx)  {
        fprintf(stderr, "eglCreateContext Error\n");
        return NULL;
    }
    *rconf = conf;
    *rctx = ctx;

    wayland_dispatch_nonblock(display);

    return(dpy);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   opengl_create_window: Create OpenGL/EGL window
 *
 * @param[in]   display     Wayland connection
 * @param[in]   surface     Wayland surface
 * @param[in]   dpy         EGL display
 * @param[in]   conf        EGL configuration
 * @param[in]   ctx         EGL context
 * @param[in]   width       Widown width
 * @param[in]   height      Window height
 * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
 * @return      EGL surface
 * @retval      != NULL     EGL surface
 * @retval      == NULL     Create window error
 */
/*--------------------------------------------------------------------------*/
EGLSurface
opengl_create_window(struct wl_display *display, struct wl_surface *surface,
                     EGLDisplay dpy, EGLConfig conf, EGLContext ctx,
                     const int width, const int height, const int color)
{
    struct wl_egl_window    *egl_window;
    EGLSurface              egl_surface;

    static const EGLint surface_attribs[] = {
        EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE, EGL_NONE
    };

    egl_window = wl_egl_window_create(surface, width, height);
    egl_surface = eglCreateWindowSurface(dpy, conf, (EGLNativeWindowType)egl_window,
                                         surface_attribs);
    eglMakeCurrent(dpy, egl_surface, egl_surface, ctx);
    glViewport(0, 0, width, height);

    wayland_dispatch_nonblock(display);

    opengl_clear_window(color);

    opengl_swap_buffer(display, dpy, egl_surface);

    return(egl_surface);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   opengl_clear_window: OpenGL window clear
 *
 * @param[in]   color       Initiale color(A<<24|R<<16|G<<8|B)
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
opengl_clear_window(const unsigned int color)
{
    double                  r, g, b, a;

    r = (double)((color>>16) & 0x0ff);
    r = r / 256.0;
    g = (double)((color>>8) & 0x0ff);
    g = g / 256.0;
    b = (double)(color & 0x0ff);
    b = b / 256.0;
    a = (double)((color>>24) & 0x0ff);
    a = (a + 1.0) / 256.0;

    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
}

/*--------------------------------------------------------------------------*/
/**
 * @brief   opengl_create_window: Create OpenGL/EGL window
 *
 * @param[in]   display     Wayland connection
 * @param[in]   dpy         EGL display
 * @param[in]   egl_surface EGL surface
 * @return      None
 */
/*--------------------------------------------------------------------------*/
void
opengl_swap_buffer(struct wl_display *display, EGLDisplay dpy, EGLSurface egl_surface)
{
    eglSwapBuffers(dpy, egl_surface);

    wayland_dispatch_nonblock(display);
}