blob: a39489333f31b612d4b2842b47756ead5b87d84a [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);
251 cairo_set_line_width (cr, 2);
252 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øgsberg2d4219e2008-12-18 21:40:03 -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
307 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
308 format = GL_RGBA;
309 } else {
310 format = GL_RGB;
311 }
312
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500313 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500314 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500315
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500316 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500317 background->map.x = 0;
318 background->map.y = 0;
319 background->map.width = width;
320 background->map.height = height;
321 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500322 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500323
324 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500325}
326
327static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500328draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500329{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500330 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500331 GLint vertices[12];
332 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
333 GLuint indices[4] = { 0, 1, 2, 3 };
334
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500335 vertices[0] = es->map.x;
336 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500337 vertices[2] = 0;
338
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500339 vertices[3] = es->map.x;
340 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500341 vertices[5] = 0;
342
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500343 vertices[6] = es->map.x + es->map.width;
344 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500345 vertices[8] = 0;
346
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500347 vertices[9] = es->map.x + es->map.width;
348 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500349 vertices[11] = 0;
350
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500351 if (es->visual == &ec->argb_visual) {
352 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
353 glEnable(GL_BLEND);
354 } else if (es->visual == &ec->premultiplied_argb_visual) {
355 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
356 glEnable(GL_BLEND);
357 } else {
358 glDisable(GL_BLEND);
359 }
360
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500361 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500362 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500363 glEnableClientState(GL_VERTEX_ARRAY);
364 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
365 glVertexPointer(3, GL_INT, 0, vertices);
366 glTexCoordPointer(2, GL_INT, 0, tex_coords);
367 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
368}
369
370static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500371schedule_repaint(struct egl_compositor *ec);
372
373static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500374repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400375{
376 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500377 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500378 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500379 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500380 uint32_t msecs;
381
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500382 if (!ec->repaint_needed) {
383 ec->repaint_on_timeout = 0;
384 return;
385 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500386
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500387 if (ec->background)
388 draw_surface(ec->background);
389 else
390 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400391
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500392 es = container_of(ec->surface_list.next,
393 struct egl_surface, link);
394 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500395 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500396
397 es = container_of(es->link.next,
398 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400399 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400400
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500401 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500402 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500403 while (&eid->link != &ec->input_device_list) {
404 draw_surface(eid->pointer_surface);
405
406 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500407 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500408 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500409
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400410 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500411 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500412
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500413 clock_gettime(CLOCK_MONOTONIC, &ts);
414 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500415 wl_display_post_frame(ec->wl_display, &ec->base,
416 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500417 ec->current_frame++;
418
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500419 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500420 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400421}
422
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500423static void
424schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400425{
426 struct wl_event_loop *loop;
427
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500428 ec->repaint_needed = 1;
429 if (!ec->repaint_on_timeout) {
430 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500431 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500432 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400433}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400434
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500435static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500436surface_destroy(struct wl_client *client,
437 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400438{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500439 struct egl_surface *es = (struct egl_surface *) surface;
440 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500441
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500442 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500443 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400444
445 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400446}
447
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500448static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500449surface_attach(struct wl_client *client,
450 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500451 uint32_t width, uint32_t height, uint32_t stride,
452 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400453{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500454 struct egl_surface *es = (struct egl_surface *) surface;
455 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400456
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500457 if (es->surface != EGL_NO_SURFACE)
458 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400459
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500460 es->width = width;
461 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500462 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500463 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500464 if (visual == &ec->argb_visual.base)
465 es->visual = &ec->argb_visual;
466 else if (visual == &ec->premultiplied_argb_visual.base)
467 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500468 else if (visual == &ec->rgb_visual.base)
469 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500470 else
471 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400472
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500473 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400474 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500475 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
476 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500478 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400479}
480
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500481static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500482surface_map(struct wl_client *client,
483 struct wl_surface *surface,
484 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400485{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500486 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400487
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500488 es->map.x = x;
489 es->map.y = y;
490 es->map.width = width;
491 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400492}
493
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500494static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500495surface_copy(struct wl_client *client,
496 struct wl_surface *surface,
497 int32_t dst_x, int32_t dst_y,
498 uint32_t name, uint32_t stride,
499 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500500{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500501 struct egl_surface *es = (struct egl_surface *) surface;
502 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500503 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500504
505 /* FIXME: glCopyPixels should work, but then we'll have to
506 * call eglMakeCurrent to set up the src and dest surfaces
507 * first. This seems cheaper, but maybe there's a better way
508 * to accomplish this. */
509
510 src = eglCreateSurfaceForName(ec->display, ec->config,
511 name, x + width, y + height, stride, NULL);
512
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500513 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500514 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500515 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500516}
517
518static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500519surface_damage(struct wl_client *client,
520 struct wl_surface *surface,
521 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500522{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500523 /* FIXME: This need to take a damage region, of course. */
524}
525
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500526const static struct wl_surface_interface surface_interface = {
527 surface_destroy,
528 surface_attach,
529 surface_map,
530 surface_copy,
531 surface_damage
532};
533
534static void
535compositor_create_surface(struct wl_client *client,
536 struct wl_compositor *compositor, uint32_t id)
537{
538 struct egl_compositor *ec = (struct egl_compositor *) compositor;
539 struct egl_surface *es;
540
541 es = malloc(sizeof *es);
542 if (es == NULL)
543 /* FIXME: Send OOM event. */
544 return;
545
546 es->compositor = ec;
547 es->surface = EGL_NO_SURFACE;
548 wl_list_insert(ec->surface_list.prev, &es->link);
549 glGenTextures(1, &es->texture);
550 wl_client_add_surface(client, &es->base,
551 &surface_interface, id);
552}
553
554static void
555compositor_commit(struct wl_client *client,
556 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500557{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500558 struct egl_compositor *ec = (struct egl_compositor *) compositor;
559
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500560 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500561 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500562}
563
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500564const static struct wl_compositor_interface compositor_interface = {
565 compositor_create_surface,
566 compositor_commit
567};
568
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500569static struct egl_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500570pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500571{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500572 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500573 struct egl_surface *es;
574
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500575 if (device->grab > 0)
576 return device->grab_surface;
577
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500578 es = container_of(ec->surface_list.prev,
579 struct egl_surface, link);
580 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500581 if (es->map.x <= device->x &&
582 device->x < es->map.x + es->map.width &&
583 es->map.y <= device->y &&
584 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500585 return es;
586
587 es = container_of(es->link.prev,
588 struct egl_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500589
590 /* Transform to surface coordinates. */
591 *sx = (x - es->map.x) * es->width / es->map.width;
592 *sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500593 }
594
595 return NULL;
596}
597
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500598void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500599notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500600{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500601 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500602 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500603 const int hotspot_x = 16, hotspot_y = 16;
604 int32_t sx, sy;
605
Ray Strodee96dcb82008-12-20 02:00:49 -0500606 if (!ec->vt_active)
607 return;
608
Ray Strode90e701d2008-12-18 23:05:43 -0500609 if (x < 0)
610 x = 0;
611 if (y < 0)
612 y = 0;
613 if (x >= ec->width)
614 x = ec->width - 1;
615 if (y >= ec->height)
616 y = ec->height - 1;
617
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500618 es = pick_surface(device, &sx, &sy);
619 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500620 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500621 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500622
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500623 device->x = x;
624 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500625 device->pointer_surface->map.x = x - hotspot_x;
626 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500627
628 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500629}
630
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500631void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500632notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500633 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500634{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500635 struct egl_surface *es;
Ray Strodee96dcb82008-12-20 02:00:49 -0500636 struct egl_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500637 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500638
Ray Strodee96dcb82008-12-20 02:00:49 -0500639 if (!ec->vt_active)
640 return;
641
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500642 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500643 if (es) {
644 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500645 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500646
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500647 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500648 /* FIXME: We need callbacks when the surfaces
649 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500650 device->grab++;
651 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500652 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500653 } else {
654 device->grab--;
655 }
656
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500657 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500658 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500659 WL_INPUT_BUTTON, button, state,
660 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500661
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500662 schedule_repaint(device->ec);
663 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500664}
665
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500666void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500667notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500668 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500669{
Ray Strodee96dcb82008-12-20 02:00:49 -0500670 struct egl_compositor *ec = device->ec;
671
672 if (!ec->vt_active)
673 return;
674
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500675 if (device->focus_surface != NULL)
676 wl_surface_post_event(&device->focus_surface->base,
677 &device->base,
678 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500679}
680
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500681struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500682evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500683 struct wl_display *display, const char *path);
684
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500685static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500686create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500687{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500688 struct wlsc_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500689 struct dirent *de;
690 char path[PATH_MAX];
691 const char *by_path_dir = "/dev/input/by-path";
692 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500693
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500694 device = malloc(sizeof *device);
695 if (device == NULL)
696 return;
697
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500698 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500699 device->base.interface = wl_input_device_get_interface();
700 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500701 device->x = 100;
702 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500703 device->pointer_surface = pointer_create(ec,
704 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500705 device->ec = ec;
706
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500707 dir = opendir(by_path_dir);
708 if (dir == NULL) {
709 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
710 return;
711 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500712
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500713 while (de = readdir(dir), de != NULL) {
714 if (fnmatch(glob, de->d_name, 0))
715 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500716
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500717 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
718 evdev_input_device_create(device, ec->wl_display, path);
719 }
720 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500721
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500722 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500723}
724
725void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500726wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500727{
728 *x = device->x;
729 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500730}
731
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500732static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500733create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500734{
735 drmModeConnector *connector;
736 drmModeRes *resources;
737 drmModeEncoder *encoder;
738 struct drm_mode_modeinfo *mode;
739 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500740 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500741 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500742
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500743 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500744 resources = drmModeGetResources(fd);
745 if (!resources) {
746 fprintf(stderr, "drmModeGetResources failed\n");
747 return 0;
748 }
749
750 for (i = 0; i < resources->count_connectors; i++) {
751 connector = drmModeGetConnector(fd, resources->connectors[i]);
752 if (connector == NULL)
753 continue;
754
755 if (connector->connection == DRM_MODE_CONNECTED &&
756 connector->count_modes > 0)
757 break;
758
759 drmModeFreeConnector(connector);
760 }
761
762 if (i == resources->count_connectors) {
763 fprintf(stderr, "No currently active connector found.\n");
764 return -1;
765 }
766
767 mode = &connector->modes[0];
768
769 for (i = 0; i < resources->count_encoders; i++) {
770 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
771
772 if (encoder == NULL)
773 continue;
774
775 if (encoder->encoder_id == connector->encoder_id)
776 break;
777
778 drmModeFreeEncoder(encoder);
779 }
780
781 /* Mode size at 32 bpp */
782 create.size = mode->hdisplay * mode->vdisplay * 4;
783 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
784 fprintf(stderr, "gem create failed: %m\n");
785 return 0;
786 }
787
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500788 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500789 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500790 if (ret) {
791 fprintf(stderr, "failed to add fb: %m\n");
792 return 0;
793 }
794
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500795 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500796 &connector->connector_id, 1, mode);
797 if (ret) {
798 fprintf(stderr, "failed to set mode: %m\n");
799 return 0;
800 }
801
802 flink.handle = create.handle;
803 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
804 fprintf(stderr, "gem flink failed: %m\n");
805 return 0;
806 }
807
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500808 ec->crtc_id = encoder->crtc_id;
809 ec->connector_id = connector->connector_id;
810 ec->mode = mode;
811 ec->width = mode->hdisplay;
812 ec->height = mode->vdisplay;
813 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500814
815 return flink.name;
816}
817
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500818static const struct wl_interface visual_interface = {
819 "visual", 1,
820};
821
822static void
823add_visuals(struct egl_compositor *ec)
824{
825 ec->argb_visual.base.interface = &visual_interface;
826 ec->argb_visual.base.implementation = NULL;
827 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
828 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
829
830 ec->premultiplied_argb_visual.base.interface = &visual_interface;
831 ec->premultiplied_argb_visual.base.implementation = NULL;
832 wl_display_add_object(ec->wl_display,
833 &ec->premultiplied_argb_visual.base);
834 wl_display_add_global(ec->wl_display,
835 &ec->premultiplied_argb_visual.base);
836
837 ec->rgb_visual.base.interface = &visual_interface;
838 ec->rgb_visual.base.implementation = NULL;
839 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
840 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
841}
842
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400843static const char gem_device[] = "/dev/dri/card0";
844
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500845static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500846 "pci-0000:00:1d.2-usb-0:2:1*event*",
847 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500848};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500849
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500850static const char *option_background = "background.jpg";
851static const char **option_input_devices = macbook_air_default_input_device;
852
853static const GOptionEntry option_entries[] = {
854 { "background", 'b', 0, G_OPTION_ARG_STRING,
855 &option_background, "Background image" },
856 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
857 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500858 { NULL }
859};
860
Ray Strode19ad6a92008-12-19 01:45:41 -0500861static void on_enter_vt(int signal_number, void *data)
862{
863 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500864 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500865
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500866 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500867 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500868
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500869 fd = eglGetDisplayFD(ec->display);
870 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
871 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -0500872 if (ret) {
873 fprintf(stderr, "failed to set mode: %m\n");
874 return;
875 }
Ray Strode19ad6a92008-12-19 01:45:41 -0500876}
877
878static void on_leave_vt(int signal_number, void *data)
879{
880 struct egl_compositor *ec = data;
881
882 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -0500883 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500884}
885
Ray Strode966aa112008-12-19 14:28:02 -0500886static bool open_active_tty(struct egl_compositor *ec)
887{
888 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
889
890 if (ec->tty_fd <= 0) {
891 fprintf(stderr, "failed to open active tty: %m\n");
892 return FALSE;
893 }
894 return TRUE;
895}
896
897static void
898on_tty_input(int fd, uint32_t mask, void *data)
899{
900 struct egl_compositor *ec = data;
901
902 /* Ignore input to tty. We get keyboard events from evdev
903 */
904 tcflush(ec->tty_fd, TCIFLUSH);
905}
906
907static void on_term_signal(int signal_number, void *data)
908{
909 struct egl_compositor *ec = data;
910
911 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
912 fprintf(stderr, "could not restore terminal to canonical mode\n");
913
914 exit(0);
915}
916
917static void ignore_tty_input(struct egl_compositor *ec, struct wl_event_loop *loop)
918{
919 struct termios raw_attributes;
920
921 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
922 fprintf(stderr, "could not get terminal attributes: %m\n");
923 return;
924 }
925
926 /* Ignore control characters and disable echo
927 */
928 raw_attributes = ec->terminal_attributes;
929 cfmakeraw (&raw_attributes);
930
931 /* Fix up line endings to be normal
932 * (cfmakeraw hoses them)
933 */
934 raw_attributes.c_oflag |= OPOST | OCRNL;
935
936 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
937 fprintf(stderr, "could not put terminal into raw mode: %m\n");
938
939 ec->term_signal_source = wl_event_loop_add_signal(loop, SIGTERM,
940 on_term_signal,
941 ec);
942
943 ec->tty_input_source = wl_event_loop_add_fd(loop, ec->tty_fd,
944 WL_EVENT_READABLE,
945 on_tty_input, ec);
946}
947
Ray Strode19ad6a92008-12-19 01:45:41 -0500948static void watch_for_vt_changes(struct egl_compositor *ec, struct wl_event_loop *loop)
949{
Ray Strode19ad6a92008-12-19 01:45:41 -0500950 struct vt_mode mode = { 0 };
951
Ray Strode19ad6a92008-12-19 01:45:41 -0500952 mode.mode = VT_PROCESS;
953 mode.relsig = SIGUSR1;
954 mode.acqsig = SIGUSR2;
955
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500956 if (!ioctl (ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -0500957 fprintf(stderr, "failed to take control of vt handling\n");
958 }
959
960 ec->leave_vt_source = wl_event_loop_add_signal (loop, SIGUSR1,
961 on_leave_vt,
962 ec);
963 ec->enter_vt_source = wl_event_loop_add_signal (loop, SIGUSR2,
964 on_enter_vt,
965 ec);
966}
967
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500968static struct egl_compositor *
969egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400970{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500971 static const EGLint config_attribs[] = {
972 EGL_DEPTH_SIZE, 0,
973 EGL_STENCIL_SIZE, 0,
974 EGL_CONFIG_CAVEAT, EGL_NONE,
975 EGL_NONE
976 };
977
978 const static EGLint attribs[] = {
979 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
980 EGL_NONE
981 };
982
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500983 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400984 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500985 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500986 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500987 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500988 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400989
990 ec = malloc(sizeof *ec);
991 if (ec == NULL)
992 return NULL;
993
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400994 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400995
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400996 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400997 if (ec->display == NULL) {
998 fprintf(stderr, "failed to create display\n");
999 return NULL;
1000 }
1001
1002 if (!eglInitialize(ec->display, &major, &minor)) {
1003 fprintf(stderr, "failed to initialize display\n");
1004 return NULL;
1005 }
1006
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -05001007 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001008 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001009
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001010 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001011 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001012 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001013 if (ec->surface == NULL) {
1014 fprintf(stderr, "failed to create surface\n");
1015 return NULL;
1016 }
1017
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001018 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001019 if (ec->context == NULL) {
1020 fprintf(stderr, "failed to create context\n");
1021 return NULL;
1022 }
1023
1024 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1025 fprintf(stderr, "failed to make context current\n");
1026 return NULL;
1027 }
1028
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001029 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001030 glMatrixMode(GL_PROJECTION);
1031 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001032 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001033 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001034 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001035
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001036 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001037 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001038
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001039 wl_list_init(&ec->input_device_list);
1040 for (i = 0; option_input_devices[i]; i++)
1041 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001042
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001043 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001044 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001045 ec->width, ec->height);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001046
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001047 shooter = screenshooter_create(ec);
1048 wl_display_add_object(display, &shooter->base);
1049 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001050
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001051 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001052
1053 ec->vt_active = TRUE;
Ray Strode966aa112008-12-19 14:28:02 -05001054 if (open_active_tty (ec)) {
1055 ignore_tty_input (ec, loop);
1056 watch_for_vt_changes (ec, loop);
1057 }
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001058 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001059 ec->repaint_needed = 0;
1060 ec->repaint_on_timeout = 0;
1061
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001062 schedule_repaint(ec);
1063
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001064 return ec;
1065}
1066
1067/* The plan here is to generate a random anonymous socket name and
1068 * advertise that through a service on the session dbus.
1069 */
1070static const char socket_name[] = "\0wayland";
1071
1072int main(int argc, char *argv[])
1073{
1074 struct wl_display *display;
1075 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001076 GError *error = NULL;
1077 GOptionContext *context;
1078
1079 context = g_option_context_new(NULL);
1080 g_option_context_add_main_entries(context, option_entries, "Wayland");
1081 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1082 fprintf(stderr, "option parsing failed: %s\n", error->message);
1083 exit(EXIT_FAILURE);
1084 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001085
1086 display = wl_display_create();
1087
1088 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001089 if (ec == NULL) {
1090 fprintf(stderr, "failed to create compositor\n");
1091 exit(EXIT_FAILURE);
1092 }
1093
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001094 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001095 fprintf(stderr, "failed to add socket: %m\n");
1096 exit(EXIT_FAILURE);
1097 }
1098
1099 wl_display_run(display);
1100
1101 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001102}