blob: ef4dca2ebd83617729657ae6286cd173fa1fb70d [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øgsberg16eb6752008-10-08 22:51:32 -040045#include <GL/gl.h>
46#include <eagle.h>
47
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050048#include "wayland.h"
49#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050050#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040052#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
53
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050054struct wl_visual {
55 struct wl_object base;
56};
57
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050058struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050059 struct wl_object base;
60 int32_t x, y;
61 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050062 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050063 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050064
65 int grab;
66 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050067 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068};
69
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040070struct egl_compositor {
71 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050072 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
73
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040074 EGLDisplay display;
75 EGLSurface surface;
76 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050077 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040078 struct wl_display *wl_display;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050079 int width, height, stride;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050080 struct egl_surface *background;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050081
Kristian Høgsbergc492b482008-12-12 12:00:02 -050082 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050083 struct wl_list surface_list;
84
Ray Strode966aa112008-12-19 14:28:02 -050085 struct wl_event_source *term_signal_source;
86
87 /* tty handling state */
88 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -050089 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -050090
91 struct termios terminal_attributes;
92 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -050093 struct wl_event_source *enter_vt_source;
94 struct wl_event_source *leave_vt_source;
95
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050096 /* Modesetting info. */
97 struct drm_mode_modeinfo *mode;
98 uint32_t fb_id;
99 uint32_t crtc_id;
100 uint32_t connector_id;
101
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500102 /* Repaint state. */
103 struct wl_event_source *timer_source;
104 int repaint_needed;
105 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500106 struct timespec previous_swap;
107 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400108};
109
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500110struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500111 struct wl_surface base;
112 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500113 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400114 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400115 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500116 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500117 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500118 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400119};
120
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500121struct screenshooter {
122 struct wl_object base;
123 struct egl_compositor *ec;
124};
125
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500126struct screenshooter_interface {
127 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
128};
129
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500130static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500131screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500132{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500133 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500134 GLuint stride;
135 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500136 GdkPixbuf *pixbuf;
137 GError *error = NULL;
138 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500139
140 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500141 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
142 8, ec->width, ec->height, stride,
143 NULL, NULL);
144 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500145}
146
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500147static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500148 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500149};
150
151static const struct wl_interface screenshooter_interface = {
152 "screenshooter", 1,
153 ARRAY_LENGTH(screenshooter_methods),
154 screenshooter_methods,
155};
156
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500157struct screenshooter_interface screenshooter_implementation = {
158 screenshooter_shoot
159};
160
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500161static struct screenshooter *
162screenshooter_create(struct egl_compositor *ec)
163{
164 struct screenshooter *shooter;
165
166 shooter = malloc(sizeof *shooter);
167 if (shooter == NULL)
168 return NULL;
169
170 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500171 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500172 shooter->ec = ec;
173
174 return shooter;
175};
176
Kristian Høgsberg54879822008-11-23 17:07:32 -0500177static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500178egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
179 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500180 int x, int y, int width, int height)
181{
182 struct egl_surface *es;
183 int stride;
184 void *data;
185
186 stride = cairo_image_surface_get_stride(surface);
187 data = cairo_image_surface_get_data(surface);
188
189 es = malloc(sizeof *es);
190 if (es == NULL)
191 return NULL;
192
193 glGenTextures(1, &es->texture);
194 glBindTexture(GL_TEXTURE_2D, es->texture);
195 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
200 GL_BGRA, GL_UNSIGNED_BYTE, data);
201
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500202 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500203 es->map.x = x;
204 es->map.y = y;
205 es->map.width = width;
206 es->map.height = height;
207 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500208 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500209
210 return es;
211}
212
213static void
214egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
215{
216 glDeleteTextures(1, &es->texture);
217 if (es->surface != EGL_NO_SURFACE)
218 eglDestroySurface(ec->display, es->surface);
219 free(es);
220}
221
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400222static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500223pointer_path(cairo_t *cr, int x, int y)
224{
225 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
226 const int width = 16, height = 16;
227
228 cairo_move_to(cr, x, y);
229 cairo_line_to(cr, x + tx, y + ty);
230 cairo_line_to(cr, x + dx, y + dy);
231 cairo_line_to(cr, x + width - end, y + height);
232 cairo_line_to(cr, x + width, y + height - end);
233 cairo_line_to(cr, x + dy, y + dx);
234 cairo_line_to(cr, x + ty, y + tx);
235 cairo_close_path(cr);
236}
237
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500238static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500239pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500240{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500241 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500242 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500243 cairo_surface_t *surface;
244 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500245
246 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
247 width, height);
248
249 cr = cairo_create(surface);
250 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500251 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500252 cairo_set_source_rgb(cr, 0, 0, 0);
253 cairo_stroke_preserve(cr);
254 cairo_fill(cr);
255 blur_surface(surface, width);
256
257 pointer_path(cr, hotspot_x, hotspot_y);
258 cairo_stroke_preserve(cr);
259 cairo_set_source_rgb(cr, 1, 1, 1);
260 cairo_fill(cr);
261 cairo_destroy(cr);
262
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500263 es = egl_surface_create_from_cairo_surface(ec,
264 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500265 x - hotspot_x,
266 y - hotspot_y,
267 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500268
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500269 cairo_surface_destroy(surface);
270
Kristian Høgsberg54879822008-11-23 17:07:32 -0500271 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500272}
273
274static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500275background_create(struct egl_compositor *ec,
276 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500277{
278 struct egl_surface *background;
279 GdkPixbuf *pixbuf;
280 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500281 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500282 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500283
284 background = malloc(sizeof *background);
285 if (background == NULL)
286 return NULL;
287
288 g_type_init();
289
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500290 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
291 width, height,
292 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500293 if (error != NULL) {
294 free(background);
295 return NULL;
296 }
297
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500298 data = gdk_pixbuf_get_pixels(pixbuf);
299
300 glGenTextures(1, &background->texture);
301 glBindTexture(GL_TEXTURE_2D, background->texture);
302 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500306
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500307 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500308 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500309 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500310 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500311
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500312 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500313 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500314
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500315 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500316 background->map.x = 0;
317 background->map.y = 0;
318 background->map.width = width;
319 background->map.height = height;
320 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500321 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500322
323 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500324}
325
326static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500327draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500328{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500329 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500330 GLint vertices[12];
331 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
332 GLuint indices[4] = { 0, 1, 2, 3 };
333
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500334 vertices[0] = es->map.x;
335 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500336 vertices[2] = 0;
337
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500338 vertices[3] = es->map.x;
339 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500340 vertices[5] = 0;
341
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500342 vertices[6] = es->map.x + es->map.width;
343 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500344 vertices[8] = 0;
345
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500346 vertices[9] = es->map.x + es->map.width;
347 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500348 vertices[11] = 0;
349
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500350 if (es->visual == &ec->argb_visual) {
351 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
352 glEnable(GL_BLEND);
353 } else if (es->visual == &ec->premultiplied_argb_visual) {
354 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
355 glEnable(GL_BLEND);
356 } else {
357 glDisable(GL_BLEND);
358 }
359
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500360 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500361 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500362 glEnableClientState(GL_VERTEX_ARRAY);
363 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
364 glVertexPointer(3, GL_INT, 0, vertices);
365 glTexCoordPointer(2, GL_INT, 0, tex_coords);
366 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
367}
368
369static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500370schedule_repaint(struct egl_compositor *ec);
371
372static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500373repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400374{
375 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500376 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500377 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500378 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500379 uint32_t msecs;
380
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500381 if (!ec->repaint_needed) {
382 ec->repaint_on_timeout = 0;
383 return;
384 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500385
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500386 if (ec->background)
387 draw_surface(ec->background);
388 else
389 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400390
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500391 es = container_of(ec->surface_list.next,
392 struct egl_surface, link);
393 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500394 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500395
396 es = container_of(es->link.next,
397 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400398 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400399
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500400 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500401 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500402 while (&eid->link != &ec->input_device_list) {
403 draw_surface(eid->pointer_surface);
404
405 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500406 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500407 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500408
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400409 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500410 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500411
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500412 clock_gettime(CLOCK_MONOTONIC, &ts);
413 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500414 wl_display_post_frame(ec->wl_display, &ec->base,
415 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500416 ec->current_frame++;
417
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500418 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500419 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400420}
421
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500422static void
423schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400424{
425 struct wl_event_loop *loop;
426
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500427 ec->repaint_needed = 1;
428 if (!ec->repaint_on_timeout) {
429 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500430 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500431 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400432}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400433
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500434static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500435surface_destroy(struct wl_client *client,
436 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400437{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500438 struct egl_surface *es = (struct egl_surface *) surface;
439 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500440
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500441 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500442 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400443
444 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400445}
446
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500447static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500448surface_attach(struct wl_client *client,
449 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500450 uint32_t width, uint32_t height, uint32_t stride,
451 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400452{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500453 struct egl_surface *es = (struct egl_surface *) surface;
454 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400455
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500456 if (es->surface != EGL_NO_SURFACE)
457 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400458
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500459 es->width = width;
460 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500461 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500462 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500463 if (visual == &ec->argb_visual.base)
464 es->visual = &ec->argb_visual;
465 else if (visual == &ec->premultiplied_argb_visual.base)
466 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500467 else if (visual == &ec->rgb_visual.base)
468 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500469 else
470 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400471
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400473 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500474 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
475 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
476 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500477 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400478}
479
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500480static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500481surface_map(struct wl_client *client,
482 struct wl_surface *surface,
483 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400484{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500485 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400486
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500487 es->map.x = x;
488 es->map.y = y;
489 es->map.width = width;
490 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400491}
492
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500493static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500494surface_copy(struct wl_client *client,
495 struct wl_surface *surface,
496 int32_t dst_x, int32_t dst_y,
497 uint32_t name, uint32_t stride,
498 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500499{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500500 struct egl_surface *es = (struct egl_surface *) surface;
501 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500502 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500503
504 /* FIXME: glCopyPixels should work, but then we'll have to
505 * call eglMakeCurrent to set up the src and dest surfaces
506 * first. This seems cheaper, but maybe there's a better way
507 * to accomplish this. */
508
509 src = eglCreateSurfaceForName(ec->display, ec->config,
510 name, x + width, y + height, stride, NULL);
511
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500512 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500513 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500514 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500515}
516
517static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500518surface_damage(struct wl_client *client,
519 struct wl_surface *surface,
520 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500521{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500522 /* FIXME: This need to take a damage region, of course. */
523}
524
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500525const static struct wl_surface_interface surface_interface = {
526 surface_destroy,
527 surface_attach,
528 surface_map,
529 surface_copy,
530 surface_damage
531};
532
533static void
534compositor_create_surface(struct wl_client *client,
535 struct wl_compositor *compositor, uint32_t id)
536{
537 struct egl_compositor *ec = (struct egl_compositor *) compositor;
538 struct egl_surface *es;
539
540 es = malloc(sizeof *es);
541 if (es == NULL)
542 /* FIXME: Send OOM event. */
543 return;
544
545 es->compositor = ec;
546 es->surface = EGL_NO_SURFACE;
547 wl_list_insert(ec->surface_list.prev, &es->link);
548 glGenTextures(1, &es->texture);
549 wl_client_add_surface(client, &es->base,
550 &surface_interface, id);
551}
552
553static void
554compositor_commit(struct wl_client *client,
555 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500556{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500557 struct egl_compositor *ec = (struct egl_compositor *) compositor;
558
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500559 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500560 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500561}
562
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500563const static struct wl_compositor_interface compositor_interface = {
564 compositor_create_surface,
565 compositor_commit
566};
567
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500568static struct egl_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500569pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500570{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500571 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500572 struct egl_surface *es;
573
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500574 if (device->grab > 0)
575 return device->grab_surface;
576
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500577 es = container_of(ec->surface_list.prev,
578 struct egl_surface, link);
579 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500580 if (es->map.x <= device->x &&
581 device->x < es->map.x + es->map.width &&
582 es->map.y <= device->y &&
583 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500584 return es;
585
586 es = container_of(es->link.prev,
587 struct egl_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500588
589 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500590 *sx = (device->x - es->map.x) * es->width / es->map.width;
591 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500592 }
593
594 return NULL;
595}
596
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500597void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500598notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500599{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500600 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500601 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500602 const int hotspot_x = 16, hotspot_y = 16;
603 int32_t sx, sy;
604
Ray Strodee96dcb82008-12-20 02:00:49 -0500605 if (!ec->vt_active)
606 return;
607
Ray Strode90e701d2008-12-18 23:05:43 -0500608 if (x < 0)
609 x = 0;
610 if (y < 0)
611 y = 0;
612 if (x >= ec->width)
613 x = ec->width - 1;
614 if (y >= ec->height)
615 y = ec->height - 1;
616
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500617 device->x = x;
618 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500619 es = pick_surface(device, &sx, &sy);
620 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500621 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500622 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500623
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500624 device->pointer_surface->map.x = x - hotspot_x;
625 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500626
627 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500628}
629
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500630void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500631notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500632 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500633{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500634 struct egl_surface *es;
Ray Strodee96dcb82008-12-20 02:00:49 -0500635 struct egl_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500636 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500637
Ray Strodee96dcb82008-12-20 02:00:49 -0500638 if (!ec->vt_active)
639 return;
640
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500641 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500642 if (es) {
643 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500644 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500645
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500646 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500647 /* FIXME: We need callbacks when the surfaces
648 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500649 device->grab++;
650 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500651 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500652 } else {
653 device->grab--;
654 }
655
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500656 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500657 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500658 WL_INPUT_BUTTON, button, state,
659 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500660
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500661 schedule_repaint(device->ec);
662 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500663}
664
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500665void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500666notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500667 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500668{
Ray Strodee96dcb82008-12-20 02:00:49 -0500669 struct egl_compositor *ec = device->ec;
670
671 if (!ec->vt_active)
672 return;
673
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500674 if (device->focus_surface != NULL)
675 wl_surface_post_event(&device->focus_surface->base,
676 &device->base,
677 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500678}
679
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500680struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500681evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500682 struct wl_display *display, const char *path);
683
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500684static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500685create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500686{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500687 struct wlsc_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500688 struct dirent *de;
689 char path[PATH_MAX];
690 const char *by_path_dir = "/dev/input/by-path";
691 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500692
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500693 device = malloc(sizeof *device);
694 if (device == NULL)
695 return;
696
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500697 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500698 device->base.interface = wl_input_device_get_interface();
699 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500700 device->x = 100;
701 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500702 device->pointer_surface = pointer_create(ec,
703 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500704 device->ec = ec;
705
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500706 dir = opendir(by_path_dir);
707 if (dir == NULL) {
708 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
709 return;
710 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500711
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500712 while (de = readdir(dir), de != NULL) {
713 if (fnmatch(glob, de->d_name, 0))
714 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500715
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500716 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
717 evdev_input_device_create(device, ec->wl_display, path);
718 }
719 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500720
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500721 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500722}
723
724void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500725wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500726{
727 *x = device->x;
728 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500729}
730
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500731static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500732create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500733{
734 drmModeConnector *connector;
735 drmModeRes *resources;
736 drmModeEncoder *encoder;
737 struct drm_mode_modeinfo *mode;
738 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500739 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500740 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500741
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500742 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500743 resources = drmModeGetResources(fd);
744 if (!resources) {
745 fprintf(stderr, "drmModeGetResources failed\n");
746 return 0;
747 }
748
749 for (i = 0; i < resources->count_connectors; i++) {
750 connector = drmModeGetConnector(fd, resources->connectors[i]);
751 if (connector == NULL)
752 continue;
753
754 if (connector->connection == DRM_MODE_CONNECTED &&
755 connector->count_modes > 0)
756 break;
757
758 drmModeFreeConnector(connector);
759 }
760
761 if (i == resources->count_connectors) {
762 fprintf(stderr, "No currently active connector found.\n");
763 return -1;
764 }
765
766 mode = &connector->modes[0];
767
768 for (i = 0; i < resources->count_encoders; i++) {
769 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
770
771 if (encoder == NULL)
772 continue;
773
774 if (encoder->encoder_id == connector->encoder_id)
775 break;
776
777 drmModeFreeEncoder(encoder);
778 }
779
780 /* Mode size at 32 bpp */
781 create.size = mode->hdisplay * mode->vdisplay * 4;
782 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
783 fprintf(stderr, "gem create failed: %m\n");
784 return 0;
785 }
786
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500787 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500788 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500789 if (ret) {
790 fprintf(stderr, "failed to add fb: %m\n");
791 return 0;
792 }
793
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500794 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500795 &connector->connector_id, 1, mode);
796 if (ret) {
797 fprintf(stderr, "failed to set mode: %m\n");
798 return 0;
799 }
800
801 flink.handle = create.handle;
802 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
803 fprintf(stderr, "gem flink failed: %m\n");
804 return 0;
805 }
806
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500807 ec->crtc_id = encoder->crtc_id;
808 ec->connector_id = connector->connector_id;
809 ec->mode = mode;
810 ec->width = mode->hdisplay;
811 ec->height = mode->vdisplay;
812 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500813
814 return flink.name;
815}
816
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500817static const struct wl_interface visual_interface = {
818 "visual", 1,
819};
820
821static void
822add_visuals(struct egl_compositor *ec)
823{
824 ec->argb_visual.base.interface = &visual_interface;
825 ec->argb_visual.base.implementation = NULL;
826 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
827 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
828
829 ec->premultiplied_argb_visual.base.interface = &visual_interface;
830 ec->premultiplied_argb_visual.base.implementation = NULL;
831 wl_display_add_object(ec->wl_display,
832 &ec->premultiplied_argb_visual.base);
833 wl_display_add_global(ec->wl_display,
834 &ec->premultiplied_argb_visual.base);
835
836 ec->rgb_visual.base.interface = &visual_interface;
837 ec->rgb_visual.base.implementation = NULL;
838 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
839 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
840}
841
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400842static const char gem_device[] = "/dev/dri/card0";
843
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500844static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500845 "pci-0000:00:1d.2-usb-0:2:1*event*",
846 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500847};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500848
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500849static const char *option_background = "background.jpg";
850static const char **option_input_devices = macbook_air_default_input_device;
851
852static const GOptionEntry option_entries[] = {
853 { "background", 'b', 0, G_OPTION_ARG_STRING,
854 &option_background, "Background image" },
855 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
856 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500857 { NULL }
858};
859
Ray Strode19ad6a92008-12-19 01:45:41 -0500860static void on_enter_vt(int signal_number, void *data)
861{
862 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500863 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500864
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500865 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500866 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500867
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500868 fd = eglGetDisplayFD(ec->display);
869 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
870 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -0500871 if (ret) {
872 fprintf(stderr, "failed to set mode: %m\n");
873 return;
874 }
Ray Strode19ad6a92008-12-19 01:45:41 -0500875}
876
877static void on_leave_vt(int signal_number, void *data)
878{
879 struct egl_compositor *ec = data;
880
881 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -0500882 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500883}
884
Ray Strode966aa112008-12-19 14:28:02 -0500885static void
886on_tty_input(int fd, uint32_t mask, void *data)
887{
888 struct egl_compositor *ec = data;
889
890 /* Ignore input to tty. We get keyboard events from evdev
891 */
892 tcflush(ec->tty_fd, TCIFLUSH);
893}
894
895static void on_term_signal(int signal_number, void *data)
896{
897 struct egl_compositor *ec = data;
898
899 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
900 fprintf(stderr, "could not restore terminal to canonical mode\n");
901
902 exit(0);
903}
904
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500905static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -0500906{
907 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500908 struct vt_mode mode = { 0 };
909
910 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
911 if (ec->tty_fd <= 0) {
912 fprintf(stderr, "failed to open active tty: %m\n");
913 return -1;
914 }
Ray Strode966aa112008-12-19 14:28:02 -0500915
916 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
917 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500918 return -1;
Ray Strode966aa112008-12-19 14:28:02 -0500919 }
920
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500921 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -0500922 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500923 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -0500924
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500925 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -0500926 raw_attributes.c_oflag |= OPOST | OCRNL;
927
928 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
929 fprintf(stderr, "could not put terminal into raw mode: %m\n");
930
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500931 ec->term_signal_source =
932 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500933
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500934 ec->tty_input_source =
935 wl_event_loop_add_fd(loop, ec->tty_fd,
936 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500937
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500938 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500939 mode.mode = VT_PROCESS;
940 mode.relsig = SIGUSR1;
941 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500942 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -0500943 fprintf(stderr, "failed to take control of vt handling\n");
944 }
945
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500946 ec->leave_vt_source =
947 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
948 ec->enter_vt_source =
949 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Ray Strode19ad6a92008-12-19 01:45:41 -0500950}
951
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500952static struct egl_compositor *
953egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400954{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500955 static const EGLint config_attribs[] = {
956 EGL_DEPTH_SIZE, 0,
957 EGL_STENCIL_SIZE, 0,
958 EGL_CONFIG_CAVEAT, EGL_NONE,
959 EGL_NONE
960 };
961
962 const static EGLint attribs[] = {
963 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
964 EGL_NONE
965 };
966
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500967 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400968 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500969 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500970 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500971 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500972 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400973
974 ec = malloc(sizeof *ec);
975 if (ec == NULL)
976 return NULL;
977
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400978 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400979
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400980 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400981 if (ec->display == NULL) {
982 fprintf(stderr, "failed to create display\n");
983 return NULL;
984 }
985
986 if (!eglInitialize(ec->display, &major, &minor)) {
987 fprintf(stderr, "failed to initialize display\n");
988 return NULL;
989 }
990
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500991 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400992 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500993
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500994 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500995 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500996 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400997 if (ec->surface == NULL) {
998 fprintf(stderr, "failed to create surface\n");
999 return NULL;
1000 }
1001
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001002 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001003 if (ec->context == NULL) {
1004 fprintf(stderr, "failed to create context\n");
1005 return NULL;
1006 }
1007
1008 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1009 fprintf(stderr, "failed to make context current\n");
1010 return NULL;
1011 }
1012
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001013 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001014 glMatrixMode(GL_PROJECTION);
1015 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001016 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001017 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001018 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001019
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001020 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001021 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001022
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001023 wl_list_init(&ec->input_device_list);
1024 for (i = 0; option_input_devices[i]; i++)
1025 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001026
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001027 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001028 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001029 ec->width, ec->height);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001030
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001031 shooter = screenshooter_create(ec);
1032 wl_display_add_object(display, &shooter->base);
1033 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001034
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001035 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001036
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001037 setup_tty(ec, loop);
1038
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001039 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001040 ec->repaint_needed = 0;
1041 ec->repaint_on_timeout = 0;
1042
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001043 schedule_repaint(ec);
1044
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001045 return ec;
1046}
1047
1048/* The plan here is to generate a random anonymous socket name and
1049 * advertise that through a service on the session dbus.
1050 */
1051static const char socket_name[] = "\0wayland";
1052
1053int main(int argc, char *argv[])
1054{
1055 struct wl_display *display;
1056 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001057 GError *error = NULL;
1058 GOptionContext *context;
1059
1060 context = g_option_context_new(NULL);
1061 g_option_context_add_main_entries(context, option_entries, "Wayland");
1062 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1063 fprintf(stderr, "option parsing failed: %s\n", error->message);
1064 exit(EXIT_FAILURE);
1065 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001066
1067 display = wl_display_create();
1068
1069 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001070 if (ec == NULL) {
1071 fprintf(stderr, "failed to create compositor\n");
1072 exit(EXIT_FAILURE);
1073 }
1074
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001075 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001076 fprintf(stderr, "failed to add socket: %m\n");
1077 exit(EXIT_FAILURE);
1078 }
1079
1080 wl_display_run(display);
1081
1082 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001083}