2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 * @brief Display screen clear tool
20 #include "ico_clear_screen.h"
21 #include <ico-uxf-weston-plugin/ico_window_mgr-client-protocol.h>
23 static struct display *_display;
24 static int signal_flag = 0;
27 sigterm_catch(int signo)
32 if (_display->ico_window_mgr) {
33 ico_window_mgr_set_visible(_display->ico_window_mgr, ICO_WINDOW_MGR_V_MAINSURFACE,
34 ICO_WINDOW_MGR_VISIBLE_HIDE, ICO_WINDOW_MGR_V_NOCHANGE,
35 ICO_WINDOW_MGR_FLAGS_ANIMATION |
36 ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE);
42 wayland_dispatch_nonblock(struct wl_display *display)
46 /* Check wayland input */
49 wl_display_flush(display);
52 if (ioctl(wl_display_get_fd(display), FIONREAD, &nread) < 0) {
56 /* Read event from wayland */
57 wl_display_dispatch(display);
63 opengl_init(struct wl_display *display, EGLConfig *rconf, EGLContext *rctx)
65 EGLDisplay dpy; /* EGL dsplay id */
71 static const EGLint config_attribs[] = {
72 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
78 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
81 static const EGLint context_attribs[] = {
82 EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
85 dpy = eglGetDisplay((EGLNativeDisplayType)display);
87 fprintf(stderr, "eglGetDisplay Error\n");
91 if (eglInitialize(dpy, &major, &minor) == EGL_FALSE) {
92 fprintf(stderr, "eglInitialize Error\n");
96 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
97 fprintf(stderr, "eglBindAPI Error\n");
101 if (eglChooseConfig(dpy, config_attribs, &conf, 1, &num_configs) == EGL_FALSE) {
102 fprintf(stderr, "eglChooseConfig Error\n");
106 ctx = eglCreateContext(dpy, conf, EGL_NO_CONTEXT, context_attribs);
108 fprintf(stderr, "eglCreateContext Error\n");
114 wayland_dispatch_nonblock(display);
120 opengl_create_window(struct display *display, struct wl_surface *surface,
121 EGLDisplay dpy, EGLConfig conf, EGLContext ctx,
122 const int width, const int height, const unsigned int color,
123 const int displayno, const int posx, const int posy)
125 struct wl_egl_window *egl_window;
126 EGLSurface egl_surface;
128 static const EGLint surface_attribs[] = {
129 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE, EGL_NONE
132 egl_window = wl_egl_window_create(surface, width, height);
133 egl_surface = eglCreateWindowSurface(dpy, conf, (EGLNativeWindowType)egl_window,
135 eglMakeCurrent(dpy, egl_surface, egl_surface, ctx);
136 glViewport(0, 0, width, height);
138 wayland_dispatch_nonblock(display->display);
140 opengl_clear_window(color);
142 opengl_swap_buffer(display->display, dpy, egl_surface);
145 ico_window_mgr_set_animation(display->ico_window_mgr, ICO_WINDOW_MGR_V_MAINSURFACE,
146 ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE|
147 ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW,
148 display->animation, display->animatime);
149 ico_window_mgr_set_positionsize(display->ico_window_mgr, ICO_WINDOW_MGR_V_MAINSURFACE,
150 displayno, posx, posy,
151 ICO_WINDOW_MGR_V_NOCHANGE, ICO_WINDOW_MGR_V_NOCHANGE,
152 ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE);
153 ico_window_mgr_set_window_layer(display->ico_window_mgr, ICO_WINDOW_MGR_V_MAINSURFACE,
154 display->init_layer);
155 ico_window_mgr_set_layer_visible(display->ico_window_mgr, display->init_layer, 1);
156 ico_window_mgr_set_visible(display->ico_window_mgr, ICO_WINDOW_MGR_V_MAINSURFACE,
157 ICO_WINDOW_MGR_VISIBLE_SHOW, ICO_WINDOW_MGR_RAISE_RAISE,
158 ICO_WINDOW_MGR_FLAGS_ANIMATION |
159 ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE);
165 opengl_clear_window(const unsigned int color)
169 r = (double)((color>>16) & 0x0ff);
171 g = (double)((color>>8) & 0x0ff);
173 b = (double)(color & 0x0ff);
175 a = (double)((color>>24) & 0x0ff);
176 a = (a + 1.0) / 256.0;
178 glClearColor(r, g, b, a);
179 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
183 opengl_swap_buffer(struct wl_display *display, EGLDisplay dpy, EGLSurface egl_surface)
185 eglSwapBuffers(dpy, egl_surface);
187 wayland_dispatch_nonblock(display);
191 shell_surface_ping(void *data, struct wl_shell_surface *wl_shell_surface, uint32_t serial)
196 shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface,
197 uint32_t edges, int32_t width, int32_t height)
202 shell_surface_popup_done(void *data, struct wl_shell_surface *wl_shell_surface)
207 output_handle_geometry(void *data, struct wl_output *wl_output, int x, int y,
208 int physical_width, int physical_height, int subpixel,
209 const char *make, const char *model, int32_t transform)
211 struct output *output = (struct output*)data;
218 output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
219 int width, int height, int refresh)
221 struct output *output = (struct output*)data;
223 if (flags & WL_OUTPUT_MODE_CURRENT) {
224 output->width = width;
225 output->height = height;
230 handle_global(void *data, struct wl_registry *registry, uint32_t id,
231 const char *interface, uint32_t version) {
232 struct display *display = (struct display*)data;
233 struct output *output;
235 if (strcmp(interface, "wl_compositor") == 0) {
236 display->compositor = (struct wl_compositor*)wl_registry_bind(display->registry, id,
237 &wl_compositor_interface, 1);
239 else if (strcmp(interface, "wl_output") == 0) {
240 if (display->num_output < MAX_DISPLAY) {
241 output = (struct output*)malloc(sizeof *output);
242 output->display = display;
243 output->output = (struct wl_output*)wl_registry_bind(display->registry, id, &wl_output_interface, 1);
244 wl_output_add_listener(output->output, &output_listener, output);
245 display->output[display->num_output++] = output;
248 else if (strcmp(interface, "wl_shell") == 0) {
249 display->shell = (struct wl_shell*)wl_registry_bind(display->registry, id, &wl_shell_interface, 1);
251 else if (strcmp(interface, "ico_window_mgr") == 0) {
252 display->ico_window_mgr = (struct ico_window_mgr *)wl_registry_bind(display->registry, id, &ico_window_mgr_interface, 1);
257 surface_enter(void *data, struct wl_surface *wl_surface, struct wl_output *output)
259 struct surface *surface = (struct surface *)data;
261 surface->output = (struct output*)wl_output_get_user_data(output);
265 surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output)
267 struct surface *surface = (struct surface*)data;
269 surface->output = NULL;
274 sleep_with_wayland(struct wl_display *display, int msec)
279 fd = wl_display_get_fd(display);
282 /* Flush send data */
283 wl_display_flush(display);
285 /* Check wayland input */
287 if (ioctl(fd, FIONREAD, &nread) < 0) {
291 /* Read event from wayland */
292 wl_display_dispatch(display);
295 if (msec >= 0) usleep(20*1000);
300 create_surface(struct display *display, const char *title) {
301 struct surface *surface;
303 if (display->num_surface >= MAX_SURFACE) {
306 surface = (struct surface *)malloc(sizeof(struct surface));
308 memset(surface, 0, sizeof(struct surface));
309 surface->display = display;
310 display->surface[display->num_surface++] = surface;
311 surface->surface = wl_compositor_create_surface(display->compositor);
312 wl_surface_add_listener(surface->surface, &surface_listener, surface);
314 if (display->shell) {
315 surface->shell_surface = wl_shell_get_shell_surface(display->shell,
317 if (surface->shell_surface) {
318 wl_shell_surface_add_listener(surface->shell_surface,
319 &shell_surface_listener, display);
320 wl_shell_surface_set_toplevel(surface->shell_surface);
321 wl_shell_surface_set_title(surface->shell_surface, title);
324 wl_display_flush(display->display);
327 wl_display_roundtrip(display->display);
329 surface->dpy = opengl_init(display->display, &surface->conf, &surface->ctx);
331 surface->egl_surface = opengl_create_window(display,
337 display->init_height,
342 clear_surface(surface);
347 clear_surface(struct surface *surface)
349 struct display *display = surface->display;
351 opengl_clear_window(display->init_color);
352 opengl_swap_buffer(display->display,
353 surface->dpy, surface->egl_surface);
356 int main(int argc, char *argv[])
361 _display = malloc(sizeof(struct display));
362 memset(_display, 0, sizeof(struct display));
364 _display->displayno = 0;
365 _display->init_color = 0xff000000;
366 _display->init_width = 1920;
367 _display->init_height = 1920;
368 _display->init_layer = 201;
369 strcpy(_display->animation, "fade");
370 _display->animatime = 600;
372 for (i = 1; i < (argc-1); i++) {
373 if (strcasecmp(argv[i], "-display") == 0) {
375 _display->displayno = strtol(argv[i], (char **)0, 0);
377 else if (strcasecmp(argv[i], "-width") == 0) {
379 _display->init_width = strtol(argv[i], (char **)0, 0);
381 else if (strcasecmp(argv[i], "-height") == 0) {
383 _display->init_height = strtol(argv[i], (char **)0, 0);
385 else if (strcasecmp(argv[i], "-color") == 0) {
387 _display->init_color = strtoul(argv[i], (char **)0, 0);
389 else if (strcasecmp(argv[i], "-layer") == 0) {
391 _display->init_layer = strtol(argv[i], (char **)0, 0);
393 else if (strcasecmp(argv[i], "-animation") == 0) {
395 memset(_display->animation, 0, sizeof(_display->animation));
396 strncpy(_display->animation, argv[i], sizeof(_display->animation)-1);
398 else if (strcasecmp(argv[i], "-animatime") == 0) {
400 _display->animatime = strtol(argv[i], (char **)0, 0);
404 "usage: %s [-display no][-layer layer][-color aarrggbb][-width width]"
405 "[-height height]\n", argv[0]);
410 _display->display = wl_display_connect(NULL);
411 if (! _display->display) {
412 fprintf(stderr, "can not connect to wayland\n");
415 _display->registry = wl_display_get_registry(_display->display);
416 wl_registry_add_listener(_display->registry, ®istry_listener, _display);
418 wl_display_dispatch(_display->display);
419 sleep_with_wayland(_display->display, 300);
421 sprintf(sname, "Clear-Screen-%d-%d", getpid(), _display->displayno);
422 create_surface(_display, sname);
425 signal(SIGTERM, sigterm_catch);
427 while (signal_flag == 0) {
428 sleep_with_wayland(_display->display, 50);
430 for (i = 0; i < (450/50); i++) {
431 sleep_with_wayland(_display->display, 50);