blob: d6a44111c2029f630e2770dc93d6fe164530f09f [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>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040024#include <i915_drm.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050029#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050030#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050031#include <gdk-pixbuf/gdk-pixbuf.h>
32#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050033#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050034#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050035#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050036#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050037#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050038#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050039#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050040#include <fnmatch.h>
41#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040042
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <GL/gl.h>
44#include <eagle.h>
45
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include "wayland.h"
47#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050048#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040050#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
51
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050052struct wl_visual {
53 struct wl_object base;
54};
55
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050056struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050057 struct wl_object base;
58 int32_t x, y;
59 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050060 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050061 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050062
63 int grab;
64 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050065 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066};
67
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040068struct egl_compositor {
69 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050070 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
71
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040072 EGLDisplay display;
73 EGLSurface surface;
74 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050075 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040076 struct wl_display *wl_display;
Ray Strode19ad6a92008-12-19 01:45:41 -050077 int tty_fd;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050078 int width, height, stride;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050079 struct egl_surface *background;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050080
Kristian Høgsbergc492b482008-12-12 12:00:02 -050081 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050082 struct wl_list surface_list;
83
Ray Strode19ad6a92008-12-19 01:45:41 -050084 struct wl_event_source *enter_vt_source;
85 struct wl_event_source *leave_vt_source;
86
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050087 /* Modesetting info. */
88 struct drm_mode_modeinfo *mode;
89 uint32_t fb_id;
90 uint32_t crtc_id;
91 uint32_t connector_id;
92
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050093 /* Repaint state. */
94 struct wl_event_source *timer_source;
95 int repaint_needed;
96 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050097 struct timespec previous_swap;
98 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040099};
100
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500101struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500102 struct wl_surface base;
103 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500104 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400105 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400106 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500107 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500108 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500109 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400110};
111
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500112struct screenshooter {
113 struct wl_object base;
114 struct egl_compositor *ec;
115};
116
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500117struct screenshooter_interface {
118 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
119};
120
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500121static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500122screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500123{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500124 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500125 GLuint stride;
126 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500127 GdkPixbuf *pixbuf;
128 GError *error = NULL;
129 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500130
131 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500132 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
133 8, ec->width, ec->height, stride,
134 NULL, NULL);
135 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500136}
137
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500138static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500139 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500140};
141
142static const struct wl_interface screenshooter_interface = {
143 "screenshooter", 1,
144 ARRAY_LENGTH(screenshooter_methods),
145 screenshooter_methods,
146};
147
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500148struct screenshooter_interface screenshooter_implementation = {
149 screenshooter_shoot
150};
151
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152static struct screenshooter *
153screenshooter_create(struct egl_compositor *ec)
154{
155 struct screenshooter *shooter;
156
157 shooter = malloc(sizeof *shooter);
158 if (shooter == NULL)
159 return NULL;
160
161 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500162 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500163 shooter->ec = ec;
164
165 return shooter;
166};
167
Kristian Høgsberg54879822008-11-23 17:07:32 -0500168static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500169egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
170 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500171 int x, int y, int width, int height)
172{
173 struct egl_surface *es;
174 int stride;
175 void *data;
176
177 stride = cairo_image_surface_get_stride(surface);
178 data = cairo_image_surface_get_data(surface);
179
180 es = malloc(sizeof *es);
181 if (es == NULL)
182 return NULL;
183
184 glGenTextures(1, &es->texture);
185 glBindTexture(GL_TEXTURE_2D, es->texture);
186 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
191 GL_BGRA, GL_UNSIGNED_BYTE, data);
192
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500193 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500194 es->map.x = x;
195 es->map.y = y;
196 es->map.width = width;
197 es->map.height = height;
198 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500199 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500200
201 return es;
202}
203
204static void
205egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
206{
207 glDeleteTextures(1, &es->texture);
208 if (es->surface != EGL_NO_SURFACE)
209 eglDestroySurface(ec->display, es->surface);
210 free(es);
211}
212
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400213static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500214pointer_path(cairo_t *cr, int x, int y)
215{
216 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
217 const int width = 16, height = 16;
218
219 cairo_move_to(cr, x, y);
220 cairo_line_to(cr, x + tx, y + ty);
221 cairo_line_to(cr, x + dx, y + dy);
222 cairo_line_to(cr, x + width - end, y + height);
223 cairo_line_to(cr, x + width, y + height - end);
224 cairo_line_to(cr, x + dy, y + dx);
225 cairo_line_to(cr, x + ty, y + tx);
226 cairo_close_path(cr);
227}
228
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500229static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500230pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500231{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500232 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500233 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500234 cairo_surface_t *surface;
235 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500236
237 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
238 width, height);
239
240 cr = cairo_create(surface);
241 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
242 cairo_set_line_width (cr, 2);
243 cairo_set_source_rgb(cr, 0, 0, 0);
244 cairo_stroke_preserve(cr);
245 cairo_fill(cr);
246 blur_surface(surface, width);
247
248 pointer_path(cr, hotspot_x, hotspot_y);
249 cairo_stroke_preserve(cr);
250 cairo_set_source_rgb(cr, 1, 1, 1);
251 cairo_fill(cr);
252 cairo_destroy(cr);
253
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500254 es = egl_surface_create_from_cairo_surface(ec,
255 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500256 x - hotspot_x,
257 y - hotspot_y,
258 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500259
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500260 cairo_surface_destroy(surface);
261
Kristian Høgsberg54879822008-11-23 17:07:32 -0500262 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500263}
264
265static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500266background_create(struct egl_compositor *ec,
267 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500268{
269 struct egl_surface *background;
270 GdkPixbuf *pixbuf;
271 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500272 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500273 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274
275 background = malloc(sizeof *background);
276 if (background == NULL)
277 return NULL;
278
279 g_type_init();
280
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500281 pixbuf = gdk_pixbuf_new_from_file_at_scale (filename,
282 width, height,
283 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500284 if (error != NULL) {
285 free(background);
286 return NULL;
287 }
288
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289 data = gdk_pixbuf_get_pixels(pixbuf);
290
291 glGenTextures(1, &background->texture);
292 glBindTexture(GL_TEXTURE_2D, background->texture);
293 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500297
298 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
299 format = GL_RGBA;
300 } else {
301 format = GL_RGB;
302 }
303
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500304 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500305 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500306
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500307 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500308 background->map.x = 0;
309 background->map.y = 0;
310 background->map.width = width;
311 background->map.height = height;
312 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500313 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500314
315 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500316}
317
318static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500319draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500320{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500321 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500322 GLint vertices[12];
323 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
324 GLuint indices[4] = { 0, 1, 2, 3 };
325
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500326 vertices[0] = es->map.x;
327 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500328 vertices[2] = 0;
329
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500330 vertices[3] = es->map.x;
331 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500332 vertices[5] = 0;
333
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500334 vertices[6] = es->map.x + es->map.width;
335 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500336 vertices[8] = 0;
337
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500338 vertices[9] = es->map.x + es->map.width;
339 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500340 vertices[11] = 0;
341
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500342 if (es->visual == &ec->argb_visual) {
343 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
344 glEnable(GL_BLEND);
345 } else if (es->visual == &ec->premultiplied_argb_visual) {
346 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
347 glEnable(GL_BLEND);
348 } else {
349 glDisable(GL_BLEND);
350 }
351
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500352 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500353 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500354 glEnableClientState(GL_VERTEX_ARRAY);
355 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
356 glVertexPointer(3, GL_INT, 0, vertices);
357 glTexCoordPointer(2, GL_INT, 0, tex_coords);
358 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
359}
360
361static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500362schedule_repaint(struct egl_compositor *ec);
363
364static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500365repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400366{
367 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500368 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500369 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500370 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500371 uint32_t msecs;
372
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500373 if (!ec->repaint_needed) {
374 ec->repaint_on_timeout = 0;
375 return;
376 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500377
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500378 if (ec->background)
379 draw_surface(ec->background);
380 else
381 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400382
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500383 es = container_of(ec->surface_list.next,
384 struct egl_surface, link);
385 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500386 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500387
388 es = container_of(es->link.next,
389 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400390 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400391
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500392 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500393 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500394 while (&eid->link != &ec->input_device_list) {
395 draw_surface(eid->pointer_surface);
396
397 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500398 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500399 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500400
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400401 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500402 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500403
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500404 clock_gettime(CLOCK_MONOTONIC, &ts);
405 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500406 wl_display_post_frame(ec->wl_display, &ec->base,
407 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500408 ec->current_frame++;
409
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500410 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500411 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400412}
413
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500414static void
415schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400416{
417 struct wl_event_loop *loop;
418
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500419 ec->repaint_needed = 1;
420 if (!ec->repaint_on_timeout) {
421 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500422 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500423 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400424}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400425
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500426static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500427surface_destroy(struct wl_client *client,
428 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400429{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500430 struct egl_surface *es = (struct egl_surface *) surface;
431 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500432
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500433 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500434 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400435
436 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400437}
438
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500439static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500440surface_attach(struct wl_client *client,
441 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500442 uint32_t width, uint32_t height, uint32_t stride,
443 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400444{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500445 struct egl_surface *es = (struct egl_surface *) surface;
446 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400447
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448 if (es->surface != EGL_NO_SURFACE)
449 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400450
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500451 es->width = width;
452 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500454 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500455 if (visual == &ec->argb_visual.base)
456 es->visual = &ec->argb_visual;
457 else if (visual == &ec->premultiplied_argb_visual.base)
458 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500459 else if (visual == &ec->rgb_visual.base)
460 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500461 else
462 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400463
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500464 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400465 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500466 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500469 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400470}
471
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500472static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500473surface_map(struct wl_client *client,
474 struct wl_surface *surface,
475 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400476{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500477 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400478
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500479 es->map.x = x;
480 es->map.y = y;
481 es->map.width = width;
482 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400483}
484
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500485static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500486surface_copy(struct wl_client *client,
487 struct wl_surface *surface,
488 int32_t dst_x, int32_t dst_y,
489 uint32_t name, uint32_t stride,
490 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500491{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500492 struct egl_surface *es = (struct egl_surface *) surface;
493 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500494 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500495
496 /* FIXME: glCopyPixels should work, but then we'll have to
497 * call eglMakeCurrent to set up the src and dest surfaces
498 * first. This seems cheaper, but maybe there's a better way
499 * to accomplish this. */
500
501 src = eglCreateSurfaceForName(ec->display, ec->config,
502 name, x + width, y + height, stride, NULL);
503
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500504 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500505 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500506 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500507}
508
509static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500510surface_damage(struct wl_client *client,
511 struct wl_surface *surface,
512 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500513{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500514 /* FIXME: This need to take a damage region, of course. */
515}
516
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500517const static struct wl_surface_interface surface_interface = {
518 surface_destroy,
519 surface_attach,
520 surface_map,
521 surface_copy,
522 surface_damage
523};
524
525static void
526compositor_create_surface(struct wl_client *client,
527 struct wl_compositor *compositor, uint32_t id)
528{
529 struct egl_compositor *ec = (struct egl_compositor *) compositor;
530 struct egl_surface *es;
531
532 es = malloc(sizeof *es);
533 if (es == NULL)
534 /* FIXME: Send OOM event. */
535 return;
536
537 es->compositor = ec;
538 es->surface = EGL_NO_SURFACE;
539 wl_list_insert(ec->surface_list.prev, &es->link);
540 glGenTextures(1, &es->texture);
541 wl_client_add_surface(client, &es->base,
542 &surface_interface, id);
543}
544
545static void
546compositor_commit(struct wl_client *client,
547 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500548{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500549 struct egl_compositor *ec = (struct egl_compositor *) compositor;
550
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500551 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500552 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500553}
554
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500555const static struct wl_compositor_interface compositor_interface = {
556 compositor_create_surface,
557 compositor_commit
558};
559
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500560static struct egl_surface *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500561pick_surface(struct wlsc_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500562{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500563 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500564 struct egl_surface *es;
565
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500566 if (device->grab > 0)
567 return device->grab_surface;
568
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500569 es = container_of(ec->surface_list.prev,
570 struct egl_surface, link);
571 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500572 if (es->map.x <= device->x &&
573 device->x < es->map.x + es->map.width &&
574 es->map.y <= device->y &&
575 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500576 return es;
577
578 es = container_of(es->link.prev,
579 struct egl_surface, link);
580 }
581
582 return NULL;
583}
584
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500585void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500586notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500587{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500588 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500589 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500590 const int hotspot_x = 16, hotspot_y = 16;
591 int32_t sx, sy;
592
Ray Strode90e701d2008-12-18 23:05:43 -0500593 if (x < 0)
594 x = 0;
595 if (y < 0)
596 y = 0;
597 if (x >= ec->width)
598 x = ec->width - 1;
599 if (y >= ec->height)
600 y = ec->height - 1;
601
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500602 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500603
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500604 if (es) {
605 sx = (x - es->map.x) * es->width / es->map.width;
606 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500607 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500608 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500609 }
610
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500611 device->x = x;
612 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500613 device->pointer_surface->map.x = x - hotspot_x;
614 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500615
616 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500617}
618
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500619void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500620notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500621 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500622{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500623 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500624 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500625
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500626 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500627 if (es) {
628 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500629 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500630
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500631 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500632 /* FIXME: We need callbacks when the surfaces
633 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500634 device->grab++;
635 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500636 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500637 } else {
638 device->grab--;
639 }
640
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500641 sx = (device->x - es->map.x) * es->width / es->map.width;
642 sy = (device->y - es->map.y) * es->height / es->map.height;
643
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500644 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500645 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500646 WL_INPUT_BUTTON, button, state,
647 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500648
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500649 schedule_repaint(device->ec);
650 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500651}
652
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500653void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500654notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500655 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500656{
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500657 if (device->focus_surface != NULL)
658 wl_surface_post_event(&device->focus_surface->base,
659 &device->base,
660 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500661}
662
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500663struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500664evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500665 struct wl_display *display, const char *path);
666
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500667static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500668create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500669{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500670 struct wlsc_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500671 struct dirent *de;
672 char path[PATH_MAX];
673 const char *by_path_dir = "/dev/input/by-path";
674 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500675
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500676 device = malloc(sizeof *device);
677 if (device == NULL)
678 return;
679
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500680 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500681 device->base.interface = wl_input_device_get_interface();
682 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500683 device->x = 100;
684 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500685 device->pointer_surface = pointer_create(ec,
686 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500687 device->ec = ec;
688
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500689 dir = opendir(by_path_dir);
690 if (dir == NULL) {
691 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
692 return;
693 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500694
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500695 while (de = readdir(dir), de != NULL) {
696 if (fnmatch(glob, de->d_name, 0))
697 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500698
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500699 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
700 evdev_input_device_create(device, ec->wl_display, path);
701 }
702 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500703
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500704 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500705}
706
707void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500708wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500709{
710 *x = device->x;
711 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500712}
713
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500714static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500715create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500716{
717 drmModeConnector *connector;
718 drmModeRes *resources;
719 drmModeEncoder *encoder;
720 struct drm_mode_modeinfo *mode;
721 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500722 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500723 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500724
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500725 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500726 resources = drmModeGetResources(fd);
727 if (!resources) {
728 fprintf(stderr, "drmModeGetResources failed\n");
729 return 0;
730 }
731
732 for (i = 0; i < resources->count_connectors; i++) {
733 connector = drmModeGetConnector(fd, resources->connectors[i]);
734 if (connector == NULL)
735 continue;
736
737 if (connector->connection == DRM_MODE_CONNECTED &&
738 connector->count_modes > 0)
739 break;
740
741 drmModeFreeConnector(connector);
742 }
743
744 if (i == resources->count_connectors) {
745 fprintf(stderr, "No currently active connector found.\n");
746 return -1;
747 }
748
749 mode = &connector->modes[0];
750
751 for (i = 0; i < resources->count_encoders; i++) {
752 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
753
754 if (encoder == NULL)
755 continue;
756
757 if (encoder->encoder_id == connector->encoder_id)
758 break;
759
760 drmModeFreeEncoder(encoder);
761 }
762
763 /* Mode size at 32 bpp */
764 create.size = mode->hdisplay * mode->vdisplay * 4;
765 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
766 fprintf(stderr, "gem create failed: %m\n");
767 return 0;
768 }
769
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500770 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500771 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500772 if (ret) {
773 fprintf(stderr, "failed to add fb: %m\n");
774 return 0;
775 }
776
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500777 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500778 &connector->connector_id, 1, mode);
779 if (ret) {
780 fprintf(stderr, "failed to set mode: %m\n");
781 return 0;
782 }
783
784 flink.handle = create.handle;
785 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
786 fprintf(stderr, "gem flink failed: %m\n");
787 return 0;
788 }
789
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500790 ec->crtc_id = encoder->crtc_id;
791 ec->connector_id = connector->connector_id;
792 ec->mode = mode;
793 ec->width = mode->hdisplay;
794 ec->height = mode->vdisplay;
795 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500796
797 return flink.name;
798}
799
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500800static const struct wl_interface visual_interface = {
801 "visual", 1,
802};
803
804static void
805add_visuals(struct egl_compositor *ec)
806{
807 ec->argb_visual.base.interface = &visual_interface;
808 ec->argb_visual.base.implementation = NULL;
809 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
810 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
811
812 ec->premultiplied_argb_visual.base.interface = &visual_interface;
813 ec->premultiplied_argb_visual.base.implementation = NULL;
814 wl_display_add_object(ec->wl_display,
815 &ec->premultiplied_argb_visual.base);
816 wl_display_add_global(ec->wl_display,
817 &ec->premultiplied_argb_visual.base);
818
819 ec->rgb_visual.base.interface = &visual_interface;
820 ec->rgb_visual.base.implementation = NULL;
821 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
822 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
823}
824
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400825static const char gem_device[] = "/dev/dri/card0";
826
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500827static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500828 "pci-0000:00:1d.2-usb-0:2:1*event*",
829 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500830};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500831
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500832static const char *option_background = "background.jpg";
833static const char **option_input_devices = macbook_air_default_input_device;
834
835static const GOptionEntry option_entries[] = {
836 { "background", 'b', 0, G_OPTION_ARG_STRING,
837 &option_background, "Background image" },
838 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
839 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500840 { NULL }
841};
842
Ray Strode19ad6a92008-12-19 01:45:41 -0500843static void on_enter_vt(int signal_number, void *data)
844{
845 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500846 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500847
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500848 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
849
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500850 fd = eglGetDisplayFD(ec->display);
851 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
852 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -0500853 if (ret) {
854 fprintf(stderr, "failed to set mode: %m\n");
855 return;
856 }
Ray Strode19ad6a92008-12-19 01:45:41 -0500857}
858
859static void on_leave_vt(int signal_number, void *data)
860{
861 struct egl_compositor *ec = data;
862
863 ioctl (ec->tty_fd, VT_RELDISP, 1);
864}
865
866static void watch_for_vt_changes(struct egl_compositor *ec, struct wl_event_loop *loop)
867{
Ray Strode19ad6a92008-12-19 01:45:41 -0500868 struct vt_mode mode = { 0 };
869
870 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
871 mode.mode = VT_PROCESS;
872 mode.relsig = SIGUSR1;
873 mode.acqsig = SIGUSR2;
874
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500875 if (!ioctl (ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -0500876 fprintf(stderr, "failed to take control of vt handling\n");
877 }
878
879 ec->leave_vt_source = wl_event_loop_add_signal (loop, SIGUSR1,
880 on_leave_vt,
881 ec);
882 ec->enter_vt_source = wl_event_loop_add_signal (loop, SIGUSR2,
883 on_enter_vt,
884 ec);
885}
886
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500887static struct egl_compositor *
888egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400889{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500890 static const EGLint config_attribs[] = {
891 EGL_DEPTH_SIZE, 0,
892 EGL_STENCIL_SIZE, 0,
893 EGL_CONFIG_CAVEAT, EGL_NONE,
894 EGL_NONE
895 };
896
897 const static EGLint attribs[] = {
898 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
899 EGL_NONE
900 };
901
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500902 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400903 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500904 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500905 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500906 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500907 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400908
909 ec = malloc(sizeof *ec);
910 if (ec == NULL)
911 return NULL;
912
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400913 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400914
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400915 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400916 if (ec->display == NULL) {
917 fprintf(stderr, "failed to create display\n");
918 return NULL;
919 }
920
921 if (!eglInitialize(ec->display, &major, &minor)) {
922 fprintf(stderr, "failed to initialize display\n");
923 return NULL;
924 }
925
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500926 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400927 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500928
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500929 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500930 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500931 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400932 if (ec->surface == NULL) {
933 fprintf(stderr, "failed to create surface\n");
934 return NULL;
935 }
936
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500937 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400938 if (ec->context == NULL) {
939 fprintf(stderr, "failed to create context\n");
940 return NULL;
941 }
942
943 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
944 fprintf(stderr, "failed to make context current\n");
945 return NULL;
946 }
947
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500948 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400949 glMatrixMode(GL_PROJECTION);
950 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500951 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400952 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500953 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500954
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500955 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500956 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500957
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500958 wl_list_init(&ec->input_device_list);
959 for (i = 0; option_input_devices[i]; i++)
960 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400961
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500962 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500963 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500964 ec->width, ec->height);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500965
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500966 shooter = screenshooter_create(ec);
967 wl_display_add_object(display, &shooter->base);
968 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500969
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500970 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strode19ad6a92008-12-19 01:45:41 -0500971 watch_for_vt_changes (ec, loop);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500972 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500973 ec->repaint_needed = 0;
974 ec->repaint_on_timeout = 0;
975
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400976 schedule_repaint(ec);
977
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500978 return ec;
979}
980
981/* The plan here is to generate a random anonymous socket name and
982 * advertise that through a service on the session dbus.
983 */
984static const char socket_name[] = "\0wayland";
985
986int main(int argc, char *argv[])
987{
988 struct wl_display *display;
989 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500990 GError *error = NULL;
991 GOptionContext *context;
992
993 context = g_option_context_new(NULL);
994 g_option_context_add_main_entries(context, option_entries, "Wayland");
995 if (!g_option_context_parse(context, &argc, &argv, &error)) {
996 fprintf(stderr, "option parsing failed: %s\n", error->message);
997 exit(EXIT_FAILURE);
998 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500999
1000 display = wl_display_create();
1001
1002 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001003 if (ec == NULL) {
1004 fprintf(stderr, "failed to create compositor\n");
1005 exit(EXIT_FAILURE);
1006 }
1007
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001008 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001009 fprintf(stderr, "failed to add socket: %m\n");
1010 exit(EXIT_FAILURE);
1011 }
1012
1013 wl_display_run(display);
1014
1015 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001016}