blob: 5a60cbbdebea1bbcb4a79d8a59b4f6a15dc9e1fe [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
Ray Strode966aa112008-12-19 14:28:02 -050021#include <stdbool.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040022#include <stdlib.h>
23#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050024#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050025#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <i915_drm.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050031#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050032#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050033#include <gdk-pixbuf/gdk-pixbuf.h>
34#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050037#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050038#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050039#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050040#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050041#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050042#include <fnmatch.h>
43#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044
Kristian Høgsberg890bc052008-12-30 14:31:33 -050045#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
46#include <libudev.h>
47
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040048#include <GL/gl.h>
49#include <eagle.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050052#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050054#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040056#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
57
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050058struct wl_visual {
59 struct wl_object base;
60};
61
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050062struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050063 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050064 struct wl_list link;
65 struct egl_compositor *ec;
66 struct egl_surface *background;
67 EGLSurface surface;
68 int32_t x, y, width, height, stride;
69
70 struct drm_mode_modeinfo *mode;
71 uint32_t fb_id;
72 uint32_t crtc_id;
73 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050074};
75
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050076struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077 struct wl_object base;
78 int32_t x, y;
79 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050080 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050081 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050082
83 int grab;
84 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050085 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050086};
87
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040088struct egl_compositor {
89 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050090 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
91
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040092 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040093 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050094 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040095 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050096
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050097 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050098 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050099 struct wl_list surface_list;
100
Ray Strode966aa112008-12-19 14:28:02 -0500101 struct wl_event_source *term_signal_source;
102
103 /* tty handling state */
104 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500105 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500106
107 struct termios terminal_attributes;
108 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500109 struct wl_event_source *enter_vt_source;
110 struct wl_event_source *leave_vt_source;
111
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500112 struct udev *udev;
113
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500114 /* Repaint state. */
115 struct wl_event_source *timer_source;
116 int repaint_needed;
117 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500118 struct timespec previous_swap;
119 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400120};
121
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500122struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500123 struct wl_surface base;
124 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500125 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400126 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400127 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500128 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500129 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500130 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400131};
132
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500133static const char *option_background = "background.jpg";
134
135static const GOptionEntry option_entries[] = {
136 { "background", 'b', 0, G_OPTION_ARG_STRING,
137 &option_background, "Background image" },
138 { NULL }
139};
140
141
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500142struct screenshooter {
143 struct wl_object base;
144 struct egl_compositor *ec;
145};
146
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500147struct screenshooter_interface {
148 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
149};
150
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500151static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500153{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500154 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500155 struct wlsc_output *output;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500156 GLuint stride;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500157 char buffer[256];
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500158 GdkPixbuf *pixbuf;
159 GError *error = NULL;
160 void *data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500161 int i;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500162
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500163 i = 0;
164 output = container_of(ec->output_list.next, struct wlsc_output, link);
165 while (&output->link != &ec->output_list) {
166 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
167 data = eglReadBuffer(ec->display, output->surface, GL_FRONT_LEFT, &stride);
168 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
169 8, output->width, output->height, stride,
170 NULL, NULL);
171 gdk_pixbuf_save(pixbuf, buffer, "png", &error, NULL);
172
173 output = container_of(output->link.next,
174 struct wlsc_output, link);
175 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500176}
177
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500178static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500179 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500180};
181
182static const struct wl_interface screenshooter_interface = {
183 "screenshooter", 1,
184 ARRAY_LENGTH(screenshooter_methods),
185 screenshooter_methods,
186};
187
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500188struct screenshooter_interface screenshooter_implementation = {
189 screenshooter_shoot
190};
191
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500192static struct screenshooter *
193screenshooter_create(struct egl_compositor *ec)
194{
195 struct screenshooter *shooter;
196
197 shooter = malloc(sizeof *shooter);
198 if (shooter == NULL)
199 return NULL;
200
201 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500202 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500203 shooter->ec = ec;
204
205 return shooter;
206};
207
Kristian Høgsberg54879822008-11-23 17:07:32 -0500208static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500209egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
210 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500211 int x, int y, int width, int height)
212{
213 struct egl_surface *es;
214 int stride;
215 void *data;
216
217 stride = cairo_image_surface_get_stride(surface);
218 data = cairo_image_surface_get_data(surface);
219
220 es = malloc(sizeof *es);
221 if (es == NULL)
222 return NULL;
223
224 glGenTextures(1, &es->texture);
225 glBindTexture(GL_TEXTURE_2D, es->texture);
226 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500230 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
231 GL_BGRA, GL_UNSIGNED_BYTE, data);
232
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500233 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500234 es->map.x = x;
235 es->map.y = y;
236 es->map.width = width;
237 es->map.height = height;
238 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500239 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500240
241 return es;
242}
243
244static void
245egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
246{
247 glDeleteTextures(1, &es->texture);
248 if (es->surface != EGL_NO_SURFACE)
249 eglDestroySurface(ec->display, es->surface);
250 free(es);
251}
252
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400253static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500254pointer_path(cairo_t *cr, int x, int y)
255{
256 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
257 const int width = 16, height = 16;
258
259 cairo_move_to(cr, x, y);
260 cairo_line_to(cr, x + tx, y + ty);
261 cairo_line_to(cr, x + dx, y + dy);
262 cairo_line_to(cr, x + width - end, y + height);
263 cairo_line_to(cr, x + width, y + height - end);
264 cairo_line_to(cr, x + dy, y + dx);
265 cairo_line_to(cr, x + ty, y + tx);
266 cairo_close_path(cr);
267}
268
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500269static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500270pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500271{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500272 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500273 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500274 cairo_surface_t *surface;
275 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500276
277 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
278 width, height);
279
280 cr = cairo_create(surface);
281 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500282 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500283 cairo_set_source_rgb(cr, 0, 0, 0);
284 cairo_stroke_preserve(cr);
285 cairo_fill(cr);
286 blur_surface(surface, width);
287
288 pointer_path(cr, hotspot_x, hotspot_y);
289 cairo_stroke_preserve(cr);
290 cairo_set_source_rgb(cr, 1, 1, 1);
291 cairo_fill(cr);
292 cairo_destroy(cr);
293
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500294 es = egl_surface_create_from_cairo_surface(ec,
295 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500296 x - hotspot_x,
297 y - hotspot_y,
298 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500299
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500300 cairo_surface_destroy(surface);
301
Kristian Høgsberg54879822008-11-23 17:07:32 -0500302 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500303}
304
305static struct egl_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500306background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500307{
308 struct egl_surface *background;
309 GdkPixbuf *pixbuf;
310 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500311 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500312 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500313
314 background = malloc(sizeof *background);
315 if (background == NULL)
316 return NULL;
317
318 g_type_init();
319
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500320 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500321 output->width,
322 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500323 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500324 if (error != NULL) {
325 free(background);
326 return NULL;
327 }
328
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500329 data = gdk_pixbuf_get_pixels(pixbuf);
330
331 glGenTextures(1, &background->texture);
332 glBindTexture(GL_TEXTURE_2D, background->texture);
333 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500334 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500337
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500338 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500339 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500340 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500341 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500342
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500343 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
344 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500345 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500346
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500347 background->compositor = output->ec;
348 background->map.x = output->x;
349 background->map.y = output->y;
350 background->map.width = output->width;
351 background->map.height = output->height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500352 background->surface = EGL_NO_SURFACE;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500353 background->visual = &output->ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500354
355 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500356}
357
358static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500359draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500360{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500361 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500362 GLint vertices[12];
363 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
364 GLuint indices[4] = { 0, 1, 2, 3 };
365
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500366 vertices[0] = es->map.x;
367 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500368 vertices[2] = 0;
369
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500370 vertices[3] = es->map.x;
371 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500372 vertices[5] = 0;
373
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500374 vertices[6] = es->map.x + es->map.width;
375 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500376 vertices[8] = 0;
377
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500378 vertices[9] = es->map.x + es->map.width;
379 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500380 vertices[11] = 0;
381
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500382 if (es->visual == &ec->argb_visual) {
383 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
384 glEnable(GL_BLEND);
385 } else if (es->visual == &ec->premultiplied_argb_visual) {
386 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
387 glEnable(GL_BLEND);
388 } else {
389 glDisable(GL_BLEND);
390 }
391
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500392 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500393 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500394 glEnableClientState(GL_VERTEX_ARRAY);
395 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
396 glVertexPointer(3, GL_INT, 0, vertices);
397 glTexCoordPointer(2, GL_INT, 0, tex_coords);
398 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
399}
400
401static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500402repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400403{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500404 struct egl_compositor *ec = output->ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500405 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500406 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500407
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500408 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
409 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500410 return;
411 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500412
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500413 glViewport(0, 0, output->width, output->height);
414 glMatrixMode(GL_PROJECTION);
415 glLoadIdentity();
416 glOrtho(0, output->width, output->height, 0, 0, 1000.0);
417 glMatrixMode(GL_MODELVIEW);
418 glClearColor(0, 0, 0.2, 1);
419
420 if (output->background)
421 draw_surface(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500422 else
423 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400424
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500425 es = container_of(ec->surface_list.next,
426 struct egl_surface, link);
427 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500428 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500429
430 es = container_of(es->link.next,
431 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400432 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400433
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500434 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500435 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500436 while (&eid->link != &ec->input_device_list) {
437 draw_surface(eid->pointer_surface);
438
439 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500440 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500441 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500442
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500443 eglSwapBuffers(ec->display, output->surface);
444}
445
446static void
447repaint(void *data)
448{
449 struct egl_compositor *ec = data;
450 struct wlsc_output *output;
451 struct timespec ts;
452 uint32_t msecs;
453
454 if (!ec->repaint_needed) {
455 ec->repaint_on_timeout = 0;
456 return;
457 }
458
459 output = container_of(ec->output_list.next, struct wlsc_output, link);
460 while (&output->link != &ec->output_list) {
461 repaint_output(output);
462 output = container_of(output->link.next,
463 struct wlsc_output, link);
464 }
465
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500466 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500467
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500468 clock_gettime(CLOCK_MONOTONIC, &ts);
469 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500470 wl_display_post_frame(ec->wl_display, &ec->base,
471 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500472 ec->current_frame++;
473
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500474 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500475 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400476}
477
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500478static void
479schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400480{
481 struct wl_event_loop *loop;
482
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500483 ec->repaint_needed = 1;
484 if (!ec->repaint_on_timeout) {
485 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500486 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500487 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400488}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400489
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500490static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500491surface_destroy(struct wl_client *client,
492 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400493{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500494 struct egl_surface *es = (struct egl_surface *) surface;
495 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500496
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500497 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400499
500 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400501}
502
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500503static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500504surface_attach(struct wl_client *client,
505 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500506 uint32_t width, uint32_t height, uint32_t stride,
507 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400508{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500509 struct egl_surface *es = (struct egl_surface *) surface;
510 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400511
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500512 if (es->surface != EGL_NO_SURFACE)
513 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400514
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500515 es->width = width;
516 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500518 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500519 if (visual == &ec->argb_visual.base)
520 es->visual = &ec->argb_visual;
521 else if (visual == &ec->premultiplied_argb_visual.base)
522 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500523 else if (visual == &ec->rgb_visual.base)
524 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500525 else
526 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400527
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500528 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400529 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500530 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500533 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400534}
535
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500536static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500537surface_map(struct wl_client *client,
538 struct wl_surface *surface,
539 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400540{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500541 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400542
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500543 es->map.x = x;
544 es->map.y = y;
545 es->map.width = width;
546 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400547}
548
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500549static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500550surface_copy(struct wl_client *client,
551 struct wl_surface *surface,
552 int32_t dst_x, int32_t dst_y,
553 uint32_t name, uint32_t stride,
554 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500555{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500556 struct egl_surface *es = (struct egl_surface *) surface;
557 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500558 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500559
560 /* FIXME: glCopyPixels should work, but then we'll have to
561 * call eglMakeCurrent to set up the src and dest surfaces
562 * first. This seems cheaper, but maybe there's a better way
563 * to accomplish this. */
564
565 src = eglCreateSurfaceForName(ec->display, ec->config,
566 name, x + width, y + height, stride, NULL);
567
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500568 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500569 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500570 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500571}
572
573static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500574surface_damage(struct wl_client *client,
575 struct wl_surface *surface,
576 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500577{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500578 /* FIXME: This need to take a damage region, of course. */
579}
580
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500581const static struct wl_surface_interface surface_interface = {
582 surface_destroy,
583 surface_attach,
584 surface_map,
585 surface_copy,
586 surface_damage
587};
588
589static void
590compositor_create_surface(struct wl_client *client,
591 struct wl_compositor *compositor, uint32_t id)
592{
593 struct egl_compositor *ec = (struct egl_compositor *) compositor;
594 struct egl_surface *es;
595
596 es = malloc(sizeof *es);
597 if (es == NULL)
598 /* FIXME: Send OOM event. */
599 return;
600
601 es->compositor = ec;
602 es->surface = EGL_NO_SURFACE;
603 wl_list_insert(ec->surface_list.prev, &es->link);
604 glGenTextures(1, &es->texture);
605 wl_client_add_surface(client, &es->base,
606 &surface_interface, id);
607}
608
609static void
610compositor_commit(struct wl_client *client,
611 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500612{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500613 struct egl_compositor *ec = (struct egl_compositor *) compositor;
614
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500615 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500616 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500617}
618
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500619const static struct wl_compositor_interface compositor_interface = {
620 compositor_create_surface,
621 compositor_commit
622};
623
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500624static struct egl_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500625pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500626{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500627 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500628 struct egl_surface *es;
629
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500630 if (device->grab > 0)
631 return device->grab_surface;
632
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500633 es = container_of(ec->surface_list.prev,
634 struct egl_surface, link);
635 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500636 if (es->map.x <= device->x &&
637 device->x < es->map.x + es->map.width &&
638 es->map.y <= device->y &&
639 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500640 return es;
641
642 es = container_of(es->link.prev,
643 struct egl_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500644
645 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500646 *sx = (device->x - es->map.x) * es->width / es->map.width;
647 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500648 }
649
650 return NULL;
651}
652
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500653void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500654notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500655{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500656 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500657 struct egl_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500658 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500659 const int hotspot_x = 16, hotspot_y = 16;
660 int32_t sx, sy;
661
Ray Strodee96dcb82008-12-20 02:00:49 -0500662 if (!ec->vt_active)
663 return;
664
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500665 /* FIXME: We need some multi head love here. */
666 output = container_of(ec->output_list.next, struct wlsc_output, link);
667 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500668 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500669 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500670 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500671 if (x >= output->x + output->width)
672 x = output->x + output->width - 1;
673 if (y >= output->y + output->height)
674 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500675
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500676 device->x = x;
677 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500678 es = pick_surface(device, &sx, &sy);
679 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500680 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500681 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500682
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500683 device->pointer_surface->map.x = x - hotspot_x;
684 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500685
686 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500687}
688
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500689void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500690notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500691 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500692{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500693 struct egl_surface *es;
Ray Strodee96dcb82008-12-20 02:00:49 -0500694 struct egl_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500695 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500696
Ray Strodee96dcb82008-12-20 02:00:49 -0500697 if (!ec->vt_active)
698 return;
699
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500700 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500701 if (es) {
702 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500703 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500704
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500705 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500706 /* FIXME: We need callbacks when the surfaces
707 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500708 device->grab++;
709 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500710 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500711 } else {
712 device->grab--;
713 }
714
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500715 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500716 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500717 WL_INPUT_BUTTON, button, state,
718 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500719
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500720 schedule_repaint(device->ec);
721 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500722}
723
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500724void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500725notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500726 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500727{
Ray Strodee96dcb82008-12-20 02:00:49 -0500728 struct egl_compositor *ec = device->ec;
729
730 if (!ec->vt_active)
731 return;
732
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500733 if (device->focus_surface != NULL)
734 wl_surface_post_event(&device->focus_surface->base,
735 &device->base,
736 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500737}
738
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500739struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500740evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500741 struct wl_display *display, const char *path);
742
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500743static struct wlsc_input_device *
744create_input_device(struct egl_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500745{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500746 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500747
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500748 device = malloc(sizeof *device);
749 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500750 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500751
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500752 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500753 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500754 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500755 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500756 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500757 device->x = 100;
758 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500759 device->ec = ec;
760
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500761 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500762
763 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500764}
765
766void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500767wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500768{
769 *x = device->x;
770 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500771}
772
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500773static void
774post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500775{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500776 struct wlsc_output *output =
777 container_of(global, struct wlsc_output, base);
778
779 wl_client_post_event(client, global,
780 WL_OUTPUT_GEOMETRY,
781 output->width, output->height);
782}
783
784static const char *
785get_udev_property(struct udev_device *device, const char *name)
786{
787 struct udev_list_entry *entry;
788
789 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(device))
790 if (strcmp(udev_list_entry_get_name(entry), name) == 0)
791 return udev_list_entry_get_value(entry);
792
793 return NULL;
794}
795
796struct dri_driver_entry {
797 uint32_t vendor_id;
798 uint32_t chip_id;
799 const char *driver;
800};
801
802static const struct dri_driver_entry driver_map[] = {
803 /* FIXME: We need to extract this table from the dri drivers
804 * and store it on disk. For now, map my i965 to i965,
805 * anything else intel to i915 and that's that. */
806
807 { 0x8086, 0x2a02, "i965" },
808 { 0x8086, ~0, "i915" },
809 { 0, }
810};
811
812static const char *
813get_driver_for_device(struct udev_device *device)
814{
815 struct udev_device *parent;
816 const char *pci_id;
817 uint32_t vendor_id, chip_id;
818 int i;
819
820 parent = udev_device_get_parent(device);
821 pci_id = get_udev_property(parent, "PCI_ID");
822 if (sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2)
823 return NULL;
824
825 for (i = 0; i < ARRAY_LENGTH(driver_map); i++) {
826 if (driver_map[i].vendor_id == vendor_id &&
827 (driver_map[i].chip_id == ~0 || driver_map[i].chip_id == chip_id))
828 return driver_map[i].driver;
829 }
830
831 return NULL;
832}
833
834static int
835init_egl(struct egl_compositor *ec, struct udev_device *device)
836{
837 static const EGLint config_attribs[] = {
838 EGL_DEPTH_SIZE, 0,
839 EGL_STENCIL_SIZE, 0,
840 EGL_CONFIG_CAVEAT, EGL_NONE,
841 EGL_NONE
842 };
843
844 const char *path, *driver;
845 EGLint major, minor;
846
847 path = udev_device_get_devnode(device);
848 driver = get_driver_for_device(device);
849 if (driver == NULL) {
850 fprintf(stderr, "didn't find driver for %s\n",
851 udev_device_get_devpath(device));
852 return -1;
853 }
854
855 ec->display = eglCreateDisplayNative(path, driver);
856 if (ec->display == NULL) {
857 fprintf(stderr, "failed to create display\n");
858 return -1;
859 }
860
861 if (!eglInitialize(ec->display, &major, &minor)) {
862 fprintf(stderr, "failed to initialize display\n");
863 return -1;
864 }
865
866 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
867 return -1;
868
869 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
870 if (ec->context == NULL) {
871 fprintf(stderr, "failed to create context\n");
872 return -1;
873 }
874 return 0;
875}
876
877static int
878create_output(struct egl_compositor *ec, struct udev_device *device)
879{
880 const static EGLint surface_attribs[] = {
881 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
882 EGL_NONE
883 };
884
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500885 drmModeConnector *connector;
886 drmModeRes *resources;
887 drmModeEncoder *encoder;
888 struct drm_mode_modeinfo *mode;
889 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500890 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500891 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500892 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500893
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500894 if (ec->display == NULL && init_egl(ec, device) < 0) {
895 fprintf(stderr, "failed to initialize egl\n");
896 return -1;
897 }
898
899 output = malloc(sizeof *output);
900 if (output == NULL)
901 return -1;
902
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500903 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500904 resources = drmModeGetResources(fd);
905 if (!resources) {
906 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500907 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500908 }
909
910 for (i = 0; i < resources->count_connectors; i++) {
911 connector = drmModeGetConnector(fd, resources->connectors[i]);
912 if (connector == NULL)
913 continue;
914
915 if (connector->connection == DRM_MODE_CONNECTED &&
916 connector->count_modes > 0)
917 break;
918
919 drmModeFreeConnector(connector);
920 }
921
922 if (i == resources->count_connectors) {
923 fprintf(stderr, "No currently active connector found.\n");
924 return -1;
925 }
926
927 mode = &connector->modes[0];
928
929 for (i = 0; i < resources->count_encoders; i++) {
930 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
931
932 if (encoder == NULL)
933 continue;
934
935 if (encoder->encoder_id == connector->encoder_id)
936 break;
937
938 drmModeFreeEncoder(encoder);
939 }
940
941 /* Mode size at 32 bpp */
942 create.size = mode->hdisplay * mode->vdisplay * 4;
943 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
944 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500945 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500946 }
947
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500948 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500949 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500950 if (ret) {
951 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500952 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500953 }
954
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500955 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500956 &connector->connector_id, 1, mode);
957 if (ret) {
958 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500959 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500960 }
961
962 flink.handle = create.handle;
963 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
964 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500965 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500966 }
967
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500968 output->ec = ec;
969 output->crtc_id = encoder->crtc_id;
970 output->connector_id = connector->connector_id;
971 output->mode = mode;
972 output->width = mode->hdisplay;
973 output->height = mode->vdisplay;
974 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500975
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500976 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
977 flink.name,
978 output->width, output->height,
979 output->stride,
980 surface_attribs);
981 if (output->surface == NULL) {
982 fprintf(stderr, "failed to create surface\n");
983 return -1;
984 }
985
986 output->base.interface = &wl_output_interface;
987 wl_display_add_object(ec->wl_display, &output->base);
988 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
989 wl_list_insert(ec->output_list.prev, &output->link);
990
991 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
992 fprintf(stderr, "failed to make context current\n");
993 return -1;
994 }
995
996 output->background = background_create(output, option_background);
997
998 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500999}
1000
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001001static const struct wl_interface visual_interface = {
1002 "visual", 1,
1003};
1004
1005static void
1006add_visuals(struct egl_compositor *ec)
1007{
1008 ec->argb_visual.base.interface = &visual_interface;
1009 ec->argb_visual.base.implementation = NULL;
1010 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001011 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001012
1013 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1014 ec->premultiplied_argb_visual.base.implementation = NULL;
1015 wl_display_add_object(ec->wl_display,
1016 &ec->premultiplied_argb_visual.base);
1017 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001018 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001019
1020 ec->rgb_visual.base.interface = &visual_interface;
1021 ec->rgb_visual.base.implementation = NULL;
1022 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001023 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1024}
1025
Ray Strode19ad6a92008-12-19 01:45:41 -05001026static void on_enter_vt(int signal_number, void *data)
1027{
1028 struct egl_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001029 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001030 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001031
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001032 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001033 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001034
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001035 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001036 output = container_of(ec->output_list.next, struct wlsc_output, link);
1037 while (&output->link != &ec->output_list) {
1038 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1039 &output->connector_id, 1, output->mode);
1040 if (ret)
1041 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1042 output->connector_id);
1043
1044 output = container_of(output->link.next,
1045 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001046 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001047}
1048
1049static void on_leave_vt(int signal_number, void *data)
1050{
1051 struct egl_compositor *ec = data;
1052
1053 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001054 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001055}
1056
Ray Strode966aa112008-12-19 14:28:02 -05001057static void
1058on_tty_input(int fd, uint32_t mask, void *data)
1059{
1060 struct egl_compositor *ec = data;
1061
1062 /* Ignore input to tty. We get keyboard events from evdev
1063 */
1064 tcflush(ec->tty_fd, TCIFLUSH);
1065}
1066
1067static void on_term_signal(int signal_number, void *data)
1068{
1069 struct egl_compositor *ec = data;
1070
1071 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1072 fprintf(stderr, "could not restore terminal to canonical mode\n");
1073
1074 exit(0);
1075}
1076
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001077static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001078{
1079 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001080 struct vt_mode mode = { 0 };
1081
1082 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1083 if (ec->tty_fd <= 0) {
1084 fprintf(stderr, "failed to open active tty: %m\n");
1085 return -1;
1086 }
Ray Strode966aa112008-12-19 14:28:02 -05001087
1088 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1089 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001090 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001091 }
1092
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001093 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001094 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001095 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001096
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001097 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001098 raw_attributes.c_oflag |= OPOST | OCRNL;
1099
1100 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1101 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1102
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001103 ec->term_signal_source =
1104 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001105
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001106 ec->tty_input_source =
1107 wl_event_loop_add_fd(loop, ec->tty_fd,
1108 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001109
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001110 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001111 mode.mode = VT_PROCESS;
1112 mode.relsig = SIGUSR1;
1113 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001114 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001115 fprintf(stderr, "failed to take control of vt handling\n");
1116 }
1117
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001118 ec->leave_vt_source =
1119 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1120 ec->enter_vt_source =
1121 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001122
1123 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001124}
1125
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001126static int
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001127init_libudev(struct egl_compositor *ec)
1128{
1129 struct udev_enumerate *e;
1130 struct udev_list_entry *entry;
1131 struct udev_device *device;
1132 const char *path, *seat;
1133 struct wlsc_input_device *input_device;
1134
1135 /* FIXME: Newer (version 135+) udev has two new features that
1136 * make all this much easier: 1) we can enumerate by a
1137 * specific property. This lets us directly iterate through
1138 * the devices we care about. 2) We can attach properties to
1139 * sysfs nodes without a device file, which lets us configure
1140 * which connectors belong to a seat instead of tagging the
1141 * overall drm node. I don't want to update my system udev,
1142 * so I'm going to stick with this until the new version is in
1143 * rawhide. */
1144
1145 ec->udev = udev_new();
1146 if (ec->udev == NULL) {
1147 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001148 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001149 }
1150
1151 input_device = create_input_device(ec);
1152
1153 e = udev_enumerate_new(ec->udev);
1154 udev_enumerate_scan_devices(e);
1155 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1156 path = udev_list_entry_get_name(entry);
1157 device = udev_device_new_from_syspath(ec->udev, path);
1158
1159 /* FIXME: Should the property namespace be CK for console kit? */
1160 seat = get_udev_property(device, "WAYLAND_SEAT");
1161 if (!seat || strcmp(seat, "1") != 0)
1162 continue;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001163
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001164 if (strcmp(udev_device_get_subsystem(device), "input") == 0) {
1165 evdev_input_device_create(input_device, ec->wl_display,
1166 udev_device_get_devnode(device));
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001167 } else if (strcmp(udev_device_get_subsystem(device), "drm") == 0) {
1168 if (create_output(ec, device) < 0) {
1169 fprintf(stderr, "failed to create output for %s\n", path);
1170 return -1;
1171 }
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001172 }
1173 }
1174 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001175
1176 /* Create the pointer surface now that we have a current EGL context. */
1177 input_device->pointer_surface =
1178 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1179
1180 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001181}
1182
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001183static struct egl_compositor *
1184egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001185{
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001186 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001187 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001188 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001189
1190 ec = malloc(sizeof *ec);
1191 if (ec == NULL)
1192 return NULL;
1193
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001194 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001195 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001196
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001197 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001198
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001199 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001200
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001201 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001202 wl_list_init(&ec->input_device_list);
1203 wl_list_init(&ec->output_list);
1204 if (init_libudev(ec) < 0) {
1205 fprintf(stderr, "failed to initialize devices\n");
1206 return NULL;
1207 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001208
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001209 shooter = screenshooter_create(ec);
1210 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001211 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001212
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001213 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001214
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001215 setup_tty(ec, loop);
1216
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001217 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001218 schedule_repaint(ec);
1219
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001220 return ec;
1221}
1222
1223/* The plan here is to generate a random anonymous socket name and
1224 * advertise that through a service on the session dbus.
1225 */
1226static const char socket_name[] = "\0wayland";
1227
1228int main(int argc, char *argv[])
1229{
1230 struct wl_display *display;
1231 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001232 GError *error = NULL;
1233 GOptionContext *context;
1234
1235 context = g_option_context_new(NULL);
1236 g_option_context_add_main_entries(context, option_entries, "Wayland");
1237 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1238 fprintf(stderr, "option parsing failed: %s\n", error->message);
1239 exit(EXIT_FAILURE);
1240 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001241
1242 display = wl_display_create();
1243
1244 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001245 if (ec == NULL) {
1246 fprintf(stderr, "failed to create compositor\n");
1247 exit(EXIT_FAILURE);
1248 }
1249
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001250 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001251 fprintf(stderr, "failed to add socket: %m\n");
1252 exit(EXIT_FAILURE);
1253 }
1254
1255 wl_display_run(display);
1256
1257 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001258}