blob: 0091172f1f4611b94f367acfa2c420df9bbb9bcb [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
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#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010026#include <stdbool.h>
27#include <math.h>
28#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020029#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010030
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030031#include <linux/input.h>
32
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010033#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050034#include <wayland-egl.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010036#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010037#include <EGL/egl.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030039struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010040struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030041
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010042struct display {
43 struct wl_display *display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050044 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040045 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040046 struct wl_seat *seat;
47 struct wl_pointer *pointer;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010048 struct {
49 EGLDisplay dpy;
50 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050051 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010052 } egl;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010053 uint32_t mask;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030054 struct window *window;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010055};
56
57struct window {
58 struct display *display;
59 struct {
60 int width, height;
61 } geometry;
62 struct {
63 GLuint fbo;
64 GLuint color_rbo;
65
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010066 GLuint rotation_uniform;
67
68 GLuint pos;
69 GLuint col;
70 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050071
72 struct wl_egl_window *native;
73 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020074 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050075 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020076 struct wl_callback *callback;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030077 int fullscreen, configured;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010078};
79
80static const char *vert_shader_text =
81 "uniform mat4 rotation;\n"
82 "attribute vec4 pos;\n"
83 "attribute vec4 color;\n"
84 "varying vec4 v_color;\n"
85 "void main() {\n"
86 " gl_Position = rotation * pos;\n"
87 " v_color = color;\n"
88 "}\n";
89
90static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -050091 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010092 "varying vec4 v_color;\n"
93 "void main() {\n"
94 " gl_FragColor = v_color;\n"
95 "}\n";
96
97static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030098init_egl(struct display *display, EGLint alpha_size)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010099{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500100 static const EGLint context_attribs[] = {
101 EGL_CONTEXT_CLIENT_VERSION, 2,
102 EGL_NONE
103 };
104
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300105 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500106 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500107 EGL_RED_SIZE, 1,
108 EGL_GREEN_SIZE, 1,
109 EGL_BLUE_SIZE, 1,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300110 EGL_ALPHA_SIZE, alpha_size,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500111 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
112 EGL_NONE
113 };
114
115 EGLint major, minor, n;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100116 EGLBoolean ret;
117
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400118 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100119 assert(display->egl.dpy);
120
121 ret = eglInitialize(display->egl.dpy, &major, &minor);
122 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500123 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100124 assert(ret == EGL_TRUE);
125
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300126 ret = eglChooseConfig(display->egl.dpy, config_attribs,
127 &display->egl.conf, 1, &n);
128 assert(ret && n == 1);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500129
130 display->egl.ctx = eglCreateContext(display->egl.dpy,
131 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500132 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100133 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500134
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100135}
136
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200137static void
138fini_egl(struct display *display)
139{
140 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
141 * on eglReleaseThread(). */
142 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
143 EGL_NO_CONTEXT);
144
145 eglTerminate(display->egl.dpy);
146 eglReleaseThread();
147}
148
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100149static GLuint
150create_shader(struct window *window, const char *source, GLenum shader_type)
151{
152 GLuint shader;
153 GLint status;
154
155 shader = glCreateShader(shader_type);
156 assert(shader != 0);
157
158 glShaderSource(shader, 1, (const char **) &source, NULL);
159 glCompileShader(shader);
160
161 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
162 if (!status) {
163 char log[1000];
164 GLsizei len;
165 glGetShaderInfoLog(shader, 1000, &len, log);
166 fprintf(stderr, "Error: compiling %s: %*s\n",
167 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
168 len, log);
169 exit(1);
170 }
171
172 return shader;
173}
174
175static void
176init_gl(struct window *window)
177{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100178 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600179 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100180 GLint status;
181
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100182 glViewport(0, 0, window->geometry.width, window->geometry.height);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100183
184 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
185 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
186
Scott Moreau3ea23d02012-06-13 17:42:21 -0600187 program = glCreateProgram();
188 glAttachShader(program, frag);
189 glAttachShader(program, vert);
190 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100191
Scott Moreau3ea23d02012-06-13 17:42:21 -0600192 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100193 if (!status) {
194 char log[1000];
195 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600196 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100197 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
198 exit(1);
199 }
200
Scott Moreau3ea23d02012-06-13 17:42:21 -0600201 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100202
203 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600204 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100205
Scott Moreau3ea23d02012-06-13 17:42:21 -0600206 glBindAttribLocation(program, window->gl.pos, "pos");
207 glBindAttribLocation(program, window->gl.col, "color");
208 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100209
210 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600211 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100212}
213
214static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300215handle_ping(void *data, struct wl_shell_surface *shell_surface,
216 uint32_t serial)
217{
218 wl_shell_surface_pong(shell_surface, serial);
219}
220
221static void
222handle_configure(void *data, struct wl_shell_surface *shell_surface,
223 uint32_t edges, int32_t width, int32_t height)
224{
225 struct window *window = data;
226
227 window->geometry.width = width;
228 window->geometry.height = height;
229 window->configured = 1;
230}
231
232static void
233handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
234{
235}
236
237static const struct wl_shell_surface_listener shell_surface_listener = {
238 handle_ping,
239 handle_configure,
240 handle_popup_done
241};
242
243static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100244create_surface(struct window *window)
245{
246 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500247 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200248
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500249 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200250 window->shell_surface = wl_shell_get_shell_surface(display->shell,
251 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300252
253 wl_shell_surface_add_listener(window->shell_surface,
254 &shell_surface_listener, window);
255
256 if (window->fullscreen) {
257 window->configured = 0;
258 wl_shell_surface_set_fullscreen(window->shell_surface,
259 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
260 0, NULL);
261
262 while (!window->configured)
263 wl_display_iterate(display->display, display->mask);
264 }
265 else
266 wl_shell_surface_set_toplevel(window->shell_surface);
267
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500268 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400269 wl_egl_window_create(window->surface,
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500270 window->geometry.width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400271 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500272 window->egl_surface =
273 eglCreateWindowSurface(display->egl.dpy,
274 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500275 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100276
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500277 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
278 window->egl_surface, window->display->egl.ctx);
279 assert(ret == EGL_TRUE);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100280}
281
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200282static void
283destroy_surface(struct window *window)
284{
285 wl_egl_window_destroy(window->native);
286
287 wl_shell_surface_destroy(window->shell_surface);
288 wl_surface_destroy(window->surface);
289
290 if (window->callback)
291 wl_callback_destroy(window->callback);
292}
293
Kristian Høgsberg33418202011-08-16 23:01:28 -0400294static const struct wl_callback_listener frame_listener;
295
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100296static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400297redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100298{
299 struct window *window = data;
300 static const GLfloat verts[3][2] = {
301 { -0.5, -0.5 },
302 { 0.5, -0.5 },
303 { 0, 0.5 }
304 };
305 static const GLfloat colors[3][3] = {
306 { 1, 0, 0 },
307 { 0, 1, 0 },
308 { 0, 0, 1 }
309 };
310 GLfloat angle;
311 GLfloat rotation[4][4] = {
312 { 1, 0, 0, 0 },
313 { 0, 1, 0, 0 },
314 { 0, 0, 1, 0 },
315 { 0, 0, 0, 1 }
316 };
317 static const int32_t speed_div = 5;
318 static uint32_t start_time = 0;
319
320 if (start_time == 0)
321 start_time = time;
322
323 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
324 rotation[0][0] = cos(angle);
325 rotation[0][2] = sin(angle);
326 rotation[2][0] = -sin(angle);
327 rotation[2][2] = cos(angle);
328
329 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
330 (GLfloat *) rotation);
331
332 glClearColor(0.0, 0.0, 0.0, 0.5);
333 glClear(GL_COLOR_BUFFER_BIT);
334
335 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
336 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
337 glEnableVertexAttribArray(window->gl.pos);
338 glEnableVertexAttribArray(window->gl.col);
339
340 glDrawArrays(GL_TRIANGLES, 0, 3);
341
342 glDisableVertexAttribArray(window->gl.pos);
343 glDisableVertexAttribArray(window->gl.col);
344
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500345 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400346 if (callback)
347 wl_callback_destroy(callback);
348
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200349 window->callback = wl_surface_frame(window->surface);
350 wl_callback_add_listener(window->callback, &frame_listener, window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100351}
352
Kristian Høgsberg33418202011-08-16 23:01:28 -0400353static const struct wl_callback_listener frame_listener = {
354 redraw
355};
356
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100357static void
Daniel Stone37816df2012-05-16 18:45:18 +0100358pointer_handle_enter(void *data, struct wl_pointer *pointer,
359 uint32_t serial, struct wl_surface *surface,
360 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300361{
362 struct display *display = data;
363
364 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300365 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300366}
367
368static void
Daniel Stone37816df2012-05-16 18:45:18 +0100369pointer_handle_leave(void *data, struct wl_pointer *pointer,
370 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300371{
372}
373
374static void
Daniel Stone37816df2012-05-16 18:45:18 +0100375pointer_handle_motion(void *data, struct wl_pointer *pointer,
376 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300377{
378}
379
380static void
Daniel Stone37816df2012-05-16 18:45:18 +0100381pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
382 uint32_t serial, uint32_t time, uint32_t button,
383 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300384{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300385 struct display *display = data;
386
387 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
388 wl_shell_surface_move(display->window->shell_surface,
389 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300390}
391
392static void
Daniel Stone37816df2012-05-16 18:45:18 +0100393pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100394 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300395{
396}
397
Daniel Stone37816df2012-05-16 18:45:18 +0100398static const struct wl_pointer_listener pointer_listener = {
399 pointer_handle_enter,
400 pointer_handle_leave,
401 pointer_handle_motion,
402 pointer_handle_button,
403 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300404};
405
406static void
Daniel Stone37816df2012-05-16 18:45:18 +0100407seat_handle_capabilities(void *data, struct wl_seat *seat,
408 enum wl_seat_capability caps)
409{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400410 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100411
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400412 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
413 d->pointer = wl_seat_get_pointer(seat);
414 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
415 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
416 wl_pointer_destroy(d->pointer);
417 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100418 }
419}
420
421static const struct wl_seat_listener seat_listener = {
422 seat_handle_capabilities,
423};
424
425static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100426display_handle_global(struct wl_display *display, uint32_t id,
427 const char *interface, uint32_t version, void *data)
428{
429 struct display *d = data;
430
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400431 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400432 d->compositor =
433 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400434 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400435 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone37816df2012-05-16 18:45:18 +0100436 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400437 d->seat = wl_display_bind(d->display, id, &wl_seat_interface);
438 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400439 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100440}
441
442static int
443event_mask_update(uint32_t mask, void *data)
444{
445 struct display *d = data;
446
447 d->mask = mask;
448
449 return 0;
450}
451
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200452static int running = 1;
453
454static void
455signal_int(int signum)
456{
457 running = 0;
458}
459
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100460int
461main(int argc, char **argv)
462{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200463 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100464 struct display display = { 0 };
465 struct window window = { 0 };
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400466 int alpha_size, i;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100467
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100468 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300469 display.window = &window;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100470 window.geometry.width = 250;
471 window.geometry.height = 250;
472
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400473 alpha_size = 1;
474 for (i = 1; i < argc; i++) {
475 if (strcmp("-f", argv[i]) == 0)
476 window.fullscreen = 1;
477 if (strcmp("-o", argv[i]) == 0)
478 alpha_size = 0;
479 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300480
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100481 display.display = wl_display_connect(NULL);
482 assert(display.display);
483
484 wl_display_add_global_listener(display.display,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500485 display_handle_global, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100486
Benjamin Franzke65e50512011-05-31 11:36:31 +0200487 wl_display_get_fd(display.display, event_mask_update, &display);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400488 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200489
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400490 if (window.fullscreen)
491 alpha_size = 0;
492
493 init_egl(&display, alpha_size);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100494 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500495 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100496
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200497 sigint.sa_handler = signal_int;
498 sigemptyset(&sigint.sa_mask);
499 sigint.sa_flags = SA_RESETHAND;
500 sigaction(SIGINT, &sigint, NULL);
501
Kristian Høgsberg33418202011-08-16 23:01:28 -0400502 redraw(&window, NULL, 0);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100503
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200504 while (running)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100505 wl_display_iterate(display.display, display.mask);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500506
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200507 fprintf(stderr, "simple-egl exiting\n");
508
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200509 destroy_surface(&window);
510 fini_egl(&display);
511
512 if (display.shell)
513 wl_shell_destroy(display.shell);
514
515 if (display.compositor)
516 wl_compositor_destroy(display.compositor);
517
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200518 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500519 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200520
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100521 return 0;
522}