blob: bcb975b64c40132aa89454362c256316f9917d2a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
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
Kristian Høgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050034#include <GL/gl.h>
35#include <eagle.h>
36
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050038#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050039
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050040#include "gears.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050041#include "cairo-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050042
43static const char gem_device[] = "/dev/dri/card0";
44static const char socket_name[] = "\0wayland";
45
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050046static void die(const char *msg)
47{
48 fprintf(stderr, "%s", msg);
49 exit(EXIT_FAILURE);
50}
51
Kristian Høgsberg61017b12008-11-02 18:51:48 -050052struct window {
Kristian Høgsberg40979232008-11-25 22:40:39 -050053 struct wl_display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050054 struct wl_surface *surface;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050055 int x, y, width, height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050056 int margin;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057 int drag_x, drag_y, last_x, last_y;
58 int state;
59 uint32_t name;
60 int fd;
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 int resized;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050062 cairo_pattern_t *background;
63
64 struct buffer *buffer;
65 struct buffer *egl_buffer;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050067 GLfloat gears_angle;
68 struct gears *gears;
Kristian Høgsberg40979232008-11-25 22:40:39 -050069 EGLDisplay egl_display;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050070 EGLContext context;
71 EGLConfig config;
72 EGLSurface egl_surface;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050073};
74
Kristian Høgsberge4feb562008-11-08 18:53:37 -050075static void
76rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
77{
78 cairo_move_to(cr, x0, y0 + radius);
79 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
80 cairo_line_to(cr, x1 - radius, y0);
81 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
82 cairo_line_to(cr, x1, y1 - radius);
83 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
84 cairo_line_to(cr, x0 + radius, y1);
85 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
86 cairo_close_path(cr);
87}
88
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050089static gboolean
90draw_window(void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050092 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050093 cairo_surface_t *surface;
94 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050095 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050096 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050097 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050098 const static char title[] = "Wayland First Post";
Kristian Høgsberg40979232008-11-25 22:40:39 -050099 struct buffer *buffer;
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500100 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500101
102 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg40979232008-11-25 22:40:39 -0500103 window->width + window->margin * 2,
104 window->height + window->margin * 2);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500106 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500107 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500108 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
109
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500111
Kristian Høgsberg40979232008-11-25 22:40:39 -0500112 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500113 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500114 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg8c304f62008-11-10 10:46:53 -0500115 rounded_rect(cr, 0, 0, window->width, window->height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500116 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -0500117 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500118
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500119 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500120 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500121 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
122 cairo_set_source(cr, outline);
123 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500124 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500125 cairo_set_source(cr, bright);
126 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500127 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500128 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500129 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500130
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500131 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
132 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500133 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
134 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500135 cairo_set_source(cr, gradient);
136 cairo_fill(cr);
137 cairo_pattern_destroy(gradient);
138
139 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
140 cairo_move_to(cr, 10, 50);
141 cairo_line_to(cr, window->width - 10, 50);
142 cairo_line_to(cr, window->width - 10, window->height - 10);
143 cairo_line_to(cr, 10, window->height - 10);
144 cairo_close_path(cr);
145 cairo_set_source(cr, dim);
146 cairo_stroke(cr);
147
148 cairo_move_to(cr, 11, 51);
149 cairo_line_to(cr, window->width - 10, 51);
150 cairo_line_to(cr, window->width - 10, window->height - 10);
151 cairo_line_to(cr, 11, window->height - 10);
152 cairo_close_path(cr);
153 cairo_set_source(cr, bright);
154 cairo_stroke(cr);
155
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500156 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
157 cairo_set_font_size(cr, 14);
158 cairo_text_extents(cr, title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500159 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500160 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
161 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
162 cairo_set_line_width (cr, 4);
163 cairo_text_path(cr, title);
164 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
165 cairo_stroke_preserve(cr);
166 cairo_set_source_rgb(cr, 1, 1, 1);
167 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500168 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500169
Kristian Høgsberg40979232008-11-25 22:40:39 -0500170 window->buffer = buffer_create_from_cairo_surface(window->fd, surface);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500171 cairo_surface_destroy(surface);
172
Kristian Høgsberg40979232008-11-25 22:40:39 -0500173 wl_surface_attach(window->surface,
174 window->buffer->name,
175 window->buffer->width,
176 window->buffer->height,
177 window->buffer->stride);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500178
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500179 wl_surface_map(window->surface,
180 window->x - window->margin,
181 window->y - window->margin,
182 window->width + 2 * window->margin,
183 window->height + 2 * window->margin);
184
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500185 width = window->width - 20;
186 height = window->height - 60;
187 buffer = buffer_create(window->fd, width, height, (width * 4 + 15) & ~15);
188 window->egl_buffer = buffer;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500189 window->egl_surface = eglCreateSurfaceForName(window->egl_display,
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500190 window->config, buffer->name,
191 buffer->width, buffer->height,
192 buffer->stride, NULL);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500193 if (!eglMakeCurrent(window->egl_display,
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500194 window->egl_surface, window->egl_surface, window->context))
195 die("failed to make context current\n");
196
197 glViewport(0, 0, width, height);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500198 window->resized = 0;
199
200 return FALSE;
201}
202
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500203enum window_state {
204 WINDOW_STABLE,
205 WINDOW_MOVING,
206 WINDOW_RESIZING_UPPER_LEFT,
207 WINDOW_RESIZING_UPPER_RIGHT,
208 WINDOW_RESIZING_LOWER_LEFT,
209 WINDOW_RESIZING_LOWER_RIGHT
210};
211
212enum location {
213 LOCATION_INTERIOR,
214 LOCATION_UPPER_LEFT,
215 LOCATION_UPPER_RIGHT,
216 LOCATION_LOWER_LEFT,
217 LOCATION_LOWER_RIGHT,
218 LOCATION_OUTSIDE
219};
220
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500221static int
222update_gears(void *data)
223{
224 struct window *window = data;
225
226 if (window->resized)
227 draw_window(window);
228 gears_draw(window->gears, window->gears_angle);
229
230 return FALSE;
231}
232
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500233static void
234event_handler(struct wl_display *display,
Kristian Høgsberg40979232008-11-25 22:40:39 -0500235 uint32_t object, uint32_t opcode,
236 uint32_t size, uint32_t *p, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500237{
238 struct window *window = data;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500239 struct buffer *buffer;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500240 int location;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500241 int grip_size = 16;
242
Kristian Høgsberg40979232008-11-25 22:40:39 -0500243 /* FIXME: Object ID 1 is the display, for anything else we
244 * assume it's an input device. */
245 if (object == 1 && opcode == 3) {
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500246 uint32_t key = p[0];
247
248 /* Ignore acknowledge events for window move requests. */
249 if (key != 0)
250 return;
251
Kristian Høgsberg40979232008-11-25 22:40:39 -0500252 /* The acknowledge event means that the server
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500253 * processed our last commit request and we can now
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500254 * safely free the old window buffer if we resized and
255 * render the next frame into our back buffer.. */
256
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500257 if (window->buffer != NULL) {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500258 buffer_destroy(window->buffer, window->fd);
259 window->buffer = NULL;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500260 }
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500261 g_idle_add(update_gears, window);
262 } else if (object == 1 && opcode == 4) {
263 /* The frame event means that the previous frame was
264 * composited, and we can now send the request to copy
265 * the frame we've rendered in the mean time into the
266 * servers surface buffer. */
267 buffer = window->egl_buffer;
268 wl_surface_copy(window->surface,
269 10 + window->margin, 50 + window->margin,
270 buffer->name, buffer->stride,
271 0, 0, buffer->width, buffer->height);
272 wl_display_commit(window->display, 0);
273 window->gears_angle += 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500274
275 } else if (object == 1) {
276 fprintf(stderr, "unexpected event from display: %d\n",
277 opcode);
278 exit(-1);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500279 } else if (opcode == 0) {
280 int x = p[0], y = p[1];
281
282 window->last_x = x;
283 window->last_y = y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500284 switch (window->state) {
285 case WINDOW_MOVING:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500286 window->x = window->drag_x + x;
287 window->y = window->drag_y + y;
288 wl_surface_map(window->surface,
289 window->x - window->margin,
290 window->y - window->margin,
291 window->width + 2 * window->margin,
292 window->height + 2 * window->margin);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500293 wl_display_commit(window->display, 1);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500294 break;
295 case WINDOW_RESIZING_LOWER_RIGHT:
Kristian Høgsberg40979232008-11-25 22:40:39 -0500296 window->width = window->drag_x + x;
297 window->height = window->drag_y + y;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500298 if (window->width < 400)
299 window->width = 400;
300 if (window->height < 400)
301 window->height = 400;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500302
303 /* Right now, resizing the window from the
304 * per-frame callback is fine, since the
305 * window drawing code is so slow that we
306 * can't draw more than one window per frame
307 * anyway. However, once we implement faster
308 * resizing, this will show lag between
309 * pointer motion and window size even if
310 * resizing is fast. We need to keep
311 * processing motion events and posting new
312 * frames as fast as possible so when the
313 * server composites the next frame it will
314 * have the most recent size possible, like
315 * what we do for window moves. */
316
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500317 window->resized = 1;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500318 break;
319 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500320 } else if (opcode == 1) {
321 int button = p[0], state = p[1];
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500322
Kristian Høgsberg40979232008-11-25 22:40:39 -0500323 if (window->x + window->width - grip_size <= window->last_x &&
324 window->last_x < window->x + window->width &&
325 window->y + window->height - grip_size <= window->last_y &&
326 window->last_y < window->y + window->height) {
327 location = LOCATION_LOWER_RIGHT;
328 } else if (window->x <= window->last_x &&
329 window->last_x < window->x + window->width &&
330 window->y <= window->last_y &&
331 window->last_y < window->y + window->height) {
332 location = LOCATION_INTERIOR;
333 } else {
334 location = LOCATION_OUTSIDE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500335 }
Kristian Høgsberg40979232008-11-25 22:40:39 -0500336
337 if (button == 0 && state == 1) {
338 switch (location) {
339 case LOCATION_INTERIOR:
340 window->drag_x = window->x - window->last_x;
341 window->drag_y = window->y - window->last_y;
342 window->state = WINDOW_MOVING;
343 break;
344 case LOCATION_LOWER_RIGHT:
345 window->drag_x = window->width - window->last_x;
346 window->drag_y = window->height - window->last_y;
347 window->state = WINDOW_RESIZING_LOWER_RIGHT;
348 break;
349 default:
350 window->state = WINDOW_STABLE;
351 break;
352 }
353 } else if (button == 0 && state == 0) {
354 window->state = WINDOW_STABLE;
355 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356 }
357}
358
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500359static struct window *
360window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500361{
362 EGLint major, minor, count;
363 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500364 struct window *window;
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500365 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.92;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500366
367 window = malloc(sizeof *window);
368 if (window == NULL)
369 return NULL;
370
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500371 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500372 window->display = display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500373 window->surface = wl_display_create_surface(display);
374 window->x = 200;
375 window->y = 200;
376 window->width = 450;
377 window->height = 500;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500378 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500379 window->state = WINDOW_STABLE;
380 window->fd = fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500381 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500382
Kristian Høgsberg40979232008-11-25 22:40:39 -0500383 window->egl_display = eglCreateDisplayNative("/dev/dri/card0", "i965");
384 if (window->egl_display == NULL)
385 die("failed to create egl display\n");
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500386
Kristian Høgsberg40979232008-11-25 22:40:39 -0500387 if (!eglInitialize(window->egl_display, &major, &minor))
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500388 die("failed to initialize display\n");
389
Kristian Høgsberg40979232008-11-25 22:40:39 -0500390 if (!eglGetConfigs(window->egl_display, configs, 64, &count))
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500391 die("failed to get configs\n");
392
393 window->config = configs[24];
Kristian Høgsberg40979232008-11-25 22:40:39 -0500394 window->context = eglCreateContext(window->egl_display, window->config, NULL, NULL);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500395 if (window->context == NULL)
396 die("failed to create context\n");
397
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500398 draw_window(window);
399 window->gears = gears_create(0, 0, 0, 0.92);
400 gears_draw(window->gears, window->gears_angle);
401 window->gears_angle += 1;
402 wl_display_commit(window->display, 0);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500403
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500404 return window;
405}
406
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500407int main(int argc, char *argv[])
408{
409 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500410 int fd;
411 struct window *window;
412 GMainLoop *loop;
413 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500414
415 fd = open(gem_device, O_RDWR);
416 if (fd < 0) {
417 fprintf(stderr, "drm open failed: %m\n");
418 return -1;
419 }
420
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500421 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500422 if (display == NULL) {
423 fprintf(stderr, "failed to create display: %m\n");
424 return -1;
425 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500426
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500427 loop = g_main_loop_new(NULL, FALSE);
428 source = wayland_source_new(display);
429 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500430
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500431 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500432
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500433 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500434
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500435 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500436
437 return 0;
438}