blob: f3ff5b08b3a48c8da03e8fe81a6fbe9a9b6dc9c4 [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"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050049#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050050#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050051#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050052
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040053#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
54
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050055struct wl_visual {
56 struct wl_object base;
57};
58
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050059struct wl_output {
60 struct wl_object base;
61};
62
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050063struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064 struct wl_object base;
65 int32_t x, y;
66 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050067 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050068 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050069
70 int grab;
71 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050072 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050073};
74
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040075struct egl_compositor {
76 struct wl_compositor base;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050077 struct wl_output output;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050078 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
79
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040080 EGLDisplay display;
81 EGLSurface surface;
82 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050083 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040084 struct wl_display *wl_display;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -050085 int width, height, stride;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050086 struct egl_surface *background;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050087
Kristian Høgsbergc492b482008-12-12 12:00:02 -050088 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050089 struct wl_list surface_list;
90
Ray Strode966aa112008-12-19 14:28:02 -050091 struct wl_event_source *term_signal_source;
92
93 /* tty handling state */
94 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -050095 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -050096
97 struct termios terminal_attributes;
98 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -050099 struct wl_event_source *enter_vt_source;
100 struct wl_event_source *leave_vt_source;
101
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500102 /* Modesetting info. */
103 struct drm_mode_modeinfo *mode;
104 uint32_t fb_id;
105 uint32_t crtc_id;
106 uint32_t connector_id;
107
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500108 /* Repaint state. */
109 struct wl_event_source *timer_source;
110 int repaint_needed;
111 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500112 struct timespec previous_swap;
113 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400114};
115
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500116struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500117 struct wl_surface base;
118 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500119 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400120 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400121 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500122 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500123 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500124 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400125};
126
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500127struct screenshooter {
128 struct wl_object base;
129 struct egl_compositor *ec;
130};
131
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500132struct screenshooter_interface {
133 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
134};
135
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500136static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500137screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500138{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500139 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500140 GLuint stride;
141 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500142 GdkPixbuf *pixbuf;
143 GError *error = NULL;
144 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500145
146 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500147 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
148 8, ec->width, ec->height, stride,
149 NULL, NULL);
150 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500151}
152
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500153static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500154 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500155};
156
157static const struct wl_interface screenshooter_interface = {
158 "screenshooter", 1,
159 ARRAY_LENGTH(screenshooter_methods),
160 screenshooter_methods,
161};
162
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500163struct screenshooter_interface screenshooter_implementation = {
164 screenshooter_shoot
165};
166
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500167static struct screenshooter *
168screenshooter_create(struct egl_compositor *ec)
169{
170 struct screenshooter *shooter;
171
172 shooter = malloc(sizeof *shooter);
173 if (shooter == NULL)
174 return NULL;
175
176 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500177 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500178 shooter->ec = ec;
179
180 return shooter;
181};
182
Kristian Høgsberg54879822008-11-23 17:07:32 -0500183static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500184egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
185 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500186 int x, int y, int width, int height)
187{
188 struct egl_surface *es;
189 int stride;
190 void *data;
191
192 stride = cairo_image_surface_get_stride(surface);
193 data = cairo_image_surface_get_data(surface);
194
195 es = malloc(sizeof *es);
196 if (es == NULL)
197 return NULL;
198
199 glGenTextures(1, &es->texture);
200 glBindTexture(GL_TEXTURE_2D, es->texture);
201 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
206 GL_BGRA, GL_UNSIGNED_BYTE, data);
207
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500208 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500209 es->map.x = x;
210 es->map.y = y;
211 es->map.width = width;
212 es->map.height = height;
213 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500214 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500215
216 return es;
217}
218
219static void
220egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
221{
222 glDeleteTextures(1, &es->texture);
223 if (es->surface != EGL_NO_SURFACE)
224 eglDestroySurface(ec->display, es->surface);
225 free(es);
226}
227
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400228static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500229pointer_path(cairo_t *cr, int x, int y)
230{
231 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
232 const int width = 16, height = 16;
233
234 cairo_move_to(cr, x, y);
235 cairo_line_to(cr, x + tx, y + ty);
236 cairo_line_to(cr, x + dx, y + dy);
237 cairo_line_to(cr, x + width - end, y + height);
238 cairo_line_to(cr, x + width, y + height - end);
239 cairo_line_to(cr, x + dy, y + dx);
240 cairo_line_to(cr, x + ty, y + tx);
241 cairo_close_path(cr);
242}
243
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500244static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500245pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500246{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500247 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500248 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500249 cairo_surface_t *surface;
250 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500251
252 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
253 width, height);
254
255 cr = cairo_create(surface);
256 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500257 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500258 cairo_set_source_rgb(cr, 0, 0, 0);
259 cairo_stroke_preserve(cr);
260 cairo_fill(cr);
261 blur_surface(surface, width);
262
263 pointer_path(cr, hotspot_x, hotspot_y);
264 cairo_stroke_preserve(cr);
265 cairo_set_source_rgb(cr, 1, 1, 1);
266 cairo_fill(cr);
267 cairo_destroy(cr);
268
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500269 es = egl_surface_create_from_cairo_surface(ec,
270 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500271 x - hotspot_x,
272 y - hotspot_y,
273 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500274
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500275 cairo_surface_destroy(surface);
276
Kristian Høgsberg54879822008-11-23 17:07:32 -0500277 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500278}
279
280static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500281background_create(struct egl_compositor *ec,
282 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500283{
284 struct egl_surface *background;
285 GdkPixbuf *pixbuf;
286 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500287 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500288 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500289
290 background = malloc(sizeof *background);
291 if (background == NULL)
292 return NULL;
293
294 g_type_init();
295
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500296 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
297 width, height,
298 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500299 if (error != NULL) {
300 free(background);
301 return NULL;
302 }
303
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500304 data = gdk_pixbuf_get_pixels(pixbuf);
305
306 glGenTextures(1, &background->texture);
307 glBindTexture(GL_TEXTURE_2D, background->texture);
308 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500309 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
310 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500312
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500313 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500314 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500315 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500316 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500317
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500318 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500319 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500320
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500321 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500322 background->map.x = 0;
323 background->map.y = 0;
324 background->map.width = width;
325 background->map.height = height;
326 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500327 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500328
329 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500330}
331
332static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500333draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500334{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500335 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500336 GLint vertices[12];
337 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
338 GLuint indices[4] = { 0, 1, 2, 3 };
339
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500340 vertices[0] = es->map.x;
341 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500342 vertices[2] = 0;
343
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500344 vertices[3] = es->map.x;
345 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500346 vertices[5] = 0;
347
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500348 vertices[6] = es->map.x + es->map.width;
349 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500350 vertices[8] = 0;
351
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500352 vertices[9] = es->map.x + es->map.width;
353 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500354 vertices[11] = 0;
355
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500356 if (es->visual == &ec->argb_visual) {
357 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
358 glEnable(GL_BLEND);
359 } else if (es->visual == &ec->premultiplied_argb_visual) {
360 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
361 glEnable(GL_BLEND);
362 } else {
363 glDisable(GL_BLEND);
364 }
365
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500366 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500367 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500368 glEnableClientState(GL_VERTEX_ARRAY);
369 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
370 glVertexPointer(3, GL_INT, 0, vertices);
371 glTexCoordPointer(2, GL_INT, 0, tex_coords);
372 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
373}
374
375static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500376schedule_repaint(struct egl_compositor *ec);
377
378static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500379repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400380{
381 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500382 struct egl_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500383 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500384 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500385 uint32_t msecs;
386
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500387 if (!ec->repaint_needed) {
388 ec->repaint_on_timeout = 0;
389 return;
390 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500391
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500392 if (ec->background)
393 draw_surface(ec->background);
394 else
395 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400396
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500397 es = container_of(ec->surface_list.next,
398 struct egl_surface, link);
399 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500400 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500401
402 es = container_of(es->link.next,
403 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400404 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400405
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500406 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500407 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500408 while (&eid->link != &ec->input_device_list) {
409 draw_surface(eid->pointer_surface);
410
411 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500412 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500413 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500414
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400415 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500416 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500417
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500418 clock_gettime(CLOCK_MONOTONIC, &ts);
419 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500420 wl_display_post_frame(ec->wl_display, &ec->base,
421 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500422 ec->current_frame++;
423
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500424 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500425 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400426}
427
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500428static void
429schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400430{
431 struct wl_event_loop *loop;
432
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500433 ec->repaint_needed = 1;
434 if (!ec->repaint_on_timeout) {
435 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500436 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500437 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400438}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400439
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500440static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500441surface_destroy(struct wl_client *client,
442 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400443{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500444 struct egl_surface *es = (struct egl_surface *) surface;
445 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500446
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500447 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400449
450 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400451}
452
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500453static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500454surface_attach(struct wl_client *client,
455 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500456 uint32_t width, uint32_t height, uint32_t stride,
457 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400458{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500459 struct egl_surface *es = (struct egl_surface *) surface;
460 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400461
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500462 if (es->surface != EGL_NO_SURFACE)
463 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400464
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500465 es->width = width;
466 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500467 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500468 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500469 if (visual == &ec->argb_visual.base)
470 es->visual = &ec->argb_visual;
471 else if (visual == &ec->premultiplied_argb_visual.base)
472 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500473 else if (visual == &ec->rgb_visual.base)
474 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500475 else
476 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400477
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500478 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400479 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400484}
485
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500486static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500487surface_map(struct wl_client *client,
488 struct wl_surface *surface,
489 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400490{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500491 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400492
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500493 es->map.x = x;
494 es->map.y = y;
495 es->map.width = width;
496 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400497}
498
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500499static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500500surface_copy(struct wl_client *client,
501 struct wl_surface *surface,
502 int32_t dst_x, int32_t dst_y,
503 uint32_t name, uint32_t stride,
504 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500505{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500506 struct egl_surface *es = (struct egl_surface *) surface;
507 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500508 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500509
510 /* FIXME: glCopyPixels should work, but then we'll have to
511 * call eglMakeCurrent to set up the src and dest surfaces
512 * first. This seems cheaper, but maybe there's a better way
513 * to accomplish this. */
514
515 src = eglCreateSurfaceForName(ec->display, ec->config,
516 name, x + width, y + height, stride, NULL);
517
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500518 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500519 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500520 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500521}
522
523static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500524surface_damage(struct wl_client *client,
525 struct wl_surface *surface,
526 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500527{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500528 /* FIXME: This need to take a damage region, of course. */
529}
530
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500531const static struct wl_surface_interface surface_interface = {
532 surface_destroy,
533 surface_attach,
534 surface_map,
535 surface_copy,
536 surface_damage
537};
538
539static void
540compositor_create_surface(struct wl_client *client,
541 struct wl_compositor *compositor, uint32_t id)
542{
543 struct egl_compositor *ec = (struct egl_compositor *) compositor;
544 struct egl_surface *es;
545
546 es = malloc(sizeof *es);
547 if (es == NULL)
548 /* FIXME: Send OOM event. */
549 return;
550
551 es->compositor = ec;
552 es->surface = EGL_NO_SURFACE;
553 wl_list_insert(ec->surface_list.prev, &es->link);
554 glGenTextures(1, &es->texture);
555 wl_client_add_surface(client, &es->base,
556 &surface_interface, id);
557}
558
559static void
560compositor_commit(struct wl_client *client,
561 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500562{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500563 struct egl_compositor *ec = (struct egl_compositor *) compositor;
564
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500565 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500566 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500567}
568
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500569const static struct wl_compositor_interface compositor_interface = {
570 compositor_create_surface,
571 compositor_commit
572};
573
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500574static struct egl_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500575pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500576{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500577 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500578 struct egl_surface *es;
579
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500580 if (device->grab > 0)
581 return device->grab_surface;
582
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500583 es = container_of(ec->surface_list.prev,
584 struct egl_surface, link);
585 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500586 if (es->map.x <= device->x &&
587 device->x < es->map.x + es->map.width &&
588 es->map.y <= device->y &&
589 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500590 return es;
591
592 es = container_of(es->link.prev,
593 struct egl_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500594
595 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500596 *sx = (device->x - es->map.x) * es->width / es->map.width;
597 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500598 }
599
600 return NULL;
601}
602
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500603void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500604notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500605{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500606 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500607 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500608 const int hotspot_x = 16, hotspot_y = 16;
609 int32_t sx, sy;
610
Ray Strodee96dcb82008-12-20 02:00:49 -0500611 if (!ec->vt_active)
612 return;
613
Ray Strode90e701d2008-12-18 23:05:43 -0500614 if (x < 0)
615 x = 0;
616 if (y < 0)
617 y = 0;
618 if (x >= ec->width)
619 x = ec->width - 1;
620 if (y >= ec->height)
621 y = ec->height - 1;
622
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500623 device->x = x;
624 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500625 es = pick_surface(device, &sx, &sy);
626 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500627 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500628 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500629
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500630 device->pointer_surface->map.x = x - hotspot_x;
631 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500632
633 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500634}
635
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500636void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500637notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500638 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500639{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500640 struct egl_surface *es;
Ray Strodee96dcb82008-12-20 02:00:49 -0500641 struct egl_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500642 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500643
Ray Strodee96dcb82008-12-20 02:00:49 -0500644 if (!ec->vt_active)
645 return;
646
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500647 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500648 if (es) {
649 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500650 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500651
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500652 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500653 /* FIXME: We need callbacks when the surfaces
654 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500655 device->grab++;
656 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500657 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500658 } else {
659 device->grab--;
660 }
661
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500662 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500663 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500664 WL_INPUT_BUTTON, button, state,
665 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500666
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500667 schedule_repaint(device->ec);
668 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500669}
670
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500671void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500672notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500673 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500674{
Ray Strodee96dcb82008-12-20 02:00:49 -0500675 struct egl_compositor *ec = device->ec;
676
677 if (!ec->vt_active)
678 return;
679
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500680 if (device->focus_surface != NULL)
681 wl_surface_post_event(&device->focus_surface->base,
682 &device->base,
683 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500684}
685
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500686struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500687evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500688 struct wl_display *display, const char *path);
689
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500690static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500691create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500692{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500693 struct wlsc_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500694 struct dirent *de;
695 char path[PATH_MAX];
696 const char *by_path_dir = "/dev/input/by-path";
697 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500698
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500699 device = malloc(sizeof *device);
700 if (device == NULL)
701 return;
702
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500703 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500704 device->base.interface = &wl_input_device_interface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500705 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500706 device->x = 100;
707 device->y = 100;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500708 device->pointer_surface =
709 pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500710 device->ec = ec;
711
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500712 dir = opendir(by_path_dir);
713 if (dir == NULL) {
714 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
715 return;
716 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500717
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500718 while (de = readdir(dir), de != NULL) {
719 if (fnmatch(glob, de->d_name, 0))
720 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500721
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500722 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
723 evdev_input_device_create(device, ec->wl_display, path);
724 }
725 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500726
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500727 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500728}
729
730void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500731wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500732{
733 *x = device->x;
734 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500735}
736
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500737static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500738create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500739{
740 drmModeConnector *connector;
741 drmModeRes *resources;
742 drmModeEncoder *encoder;
743 struct drm_mode_modeinfo *mode;
744 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500745 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500746 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500747
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500748 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500749 resources = drmModeGetResources(fd);
750 if (!resources) {
751 fprintf(stderr, "drmModeGetResources failed\n");
752 return 0;
753 }
754
755 for (i = 0; i < resources->count_connectors; i++) {
756 connector = drmModeGetConnector(fd, resources->connectors[i]);
757 if (connector == NULL)
758 continue;
759
760 if (connector->connection == DRM_MODE_CONNECTED &&
761 connector->count_modes > 0)
762 break;
763
764 drmModeFreeConnector(connector);
765 }
766
767 if (i == resources->count_connectors) {
768 fprintf(stderr, "No currently active connector found.\n");
769 return -1;
770 }
771
772 mode = &connector->modes[0];
773
774 for (i = 0; i < resources->count_encoders; i++) {
775 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
776
777 if (encoder == NULL)
778 continue;
779
780 if (encoder->encoder_id == connector->encoder_id)
781 break;
782
783 drmModeFreeEncoder(encoder);
784 }
785
786 /* Mode size at 32 bpp */
787 create.size = mode->hdisplay * mode->vdisplay * 4;
788 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
789 fprintf(stderr, "gem create failed: %m\n");
790 return 0;
791 }
792
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500793 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500794 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500795 if (ret) {
796 fprintf(stderr, "failed to add fb: %m\n");
797 return 0;
798 }
799
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500800 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500801 &connector->connector_id, 1, mode);
802 if (ret) {
803 fprintf(stderr, "failed to set mode: %m\n");
804 return 0;
805 }
806
807 flink.handle = create.handle;
808 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
809 fprintf(stderr, "gem flink failed: %m\n");
810 return 0;
811 }
812
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500813 ec->crtc_id = encoder->crtc_id;
814 ec->connector_id = connector->connector_id;
815 ec->mode = mode;
816 ec->width = mode->hdisplay;
817 ec->height = mode->vdisplay;
818 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500819
820 return flink.name;
821}
822
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500823static const struct wl_interface visual_interface = {
824 "visual", 1,
825};
826
827static void
828add_visuals(struct egl_compositor *ec)
829{
830 ec->argb_visual.base.interface = &visual_interface;
831 ec->argb_visual.base.implementation = NULL;
832 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500833 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500834
835 ec->premultiplied_argb_visual.base.interface = &visual_interface;
836 ec->premultiplied_argb_visual.base.implementation = NULL;
837 wl_display_add_object(ec->wl_display,
838 &ec->premultiplied_argb_visual.base);
839 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500840 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500841
842 ec->rgb_visual.base.interface = &visual_interface;
843 ec->rgb_visual.base.implementation = NULL;
844 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500845 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
846}
847
848static void
849post_output_presence(struct wl_client *client, struct wl_object *global)
850{
851 struct egl_compositor *ec =
852 container_of(global, struct egl_compositor, output.base);
853
854 wl_client_post_event(client, global,
855 WL_OUTPUT_PRESENCE, ec->width, ec->height);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500856}
857
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400858static const char gem_device[] = "/dev/dri/card0";
859
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500860static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500861 "pci-0000:00:1d.2-usb-0:2:1*event*",
862 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500863};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500864
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500865static const char *option_background = "background.jpg";
866static const char **option_input_devices = macbook_air_default_input_device;
867
868static const GOptionEntry option_entries[] = {
869 { "background", 'b', 0, G_OPTION_ARG_STRING,
870 &option_background, "Background image" },
871 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
872 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500873 { NULL }
874};
875
Ray Strode19ad6a92008-12-19 01:45:41 -0500876static void on_enter_vt(int signal_number, void *data)
877{
878 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500879 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500880
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500881 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500882 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500883
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500884 fd = eglGetDisplayFD(ec->display);
885 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
886 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -0500887 if (ret) {
888 fprintf(stderr, "failed to set mode: %m\n");
889 return;
890 }
Ray Strode19ad6a92008-12-19 01:45:41 -0500891}
892
893static void on_leave_vt(int signal_number, void *data)
894{
895 struct egl_compositor *ec = data;
896
897 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -0500898 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500899}
900
Ray Strode966aa112008-12-19 14:28:02 -0500901static void
902on_tty_input(int fd, uint32_t mask, void *data)
903{
904 struct egl_compositor *ec = data;
905
906 /* Ignore input to tty. We get keyboard events from evdev
907 */
908 tcflush(ec->tty_fd, TCIFLUSH);
909}
910
911static void on_term_signal(int signal_number, void *data)
912{
913 struct egl_compositor *ec = data;
914
915 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
916 fprintf(stderr, "could not restore terminal to canonical mode\n");
917
918 exit(0);
919}
920
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500921static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -0500922{
923 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500924 struct vt_mode mode = { 0 };
925
926 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
927 if (ec->tty_fd <= 0) {
928 fprintf(stderr, "failed to open active tty: %m\n");
929 return -1;
930 }
Ray Strode966aa112008-12-19 14:28:02 -0500931
932 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
933 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500934 return -1;
Ray Strode966aa112008-12-19 14:28:02 -0500935 }
936
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500937 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -0500938 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500939 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -0500940
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500941 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -0500942 raw_attributes.c_oflag |= OPOST | OCRNL;
943
944 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
945 fprintf(stderr, "could not put terminal into raw mode: %m\n");
946
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500947 ec->term_signal_source =
948 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500949
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500950 ec->tty_input_source =
951 wl_event_loop_add_fd(loop, ec->tty_fd,
952 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500953
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500954 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500955 mode.mode = VT_PROCESS;
956 mode.relsig = SIGUSR1;
957 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500958 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -0500959 fprintf(stderr, "failed to take control of vt handling\n");
960 }
961
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500962 ec->leave_vt_source =
963 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
964 ec->enter_vt_source =
965 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500966
967 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -0500968}
969
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500970static struct egl_compositor *
971egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400972{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500973 static const EGLint config_attribs[] = {
974 EGL_DEPTH_SIZE, 0,
975 EGL_STENCIL_SIZE, 0,
976 EGL_CONFIG_CAVEAT, EGL_NONE,
977 EGL_NONE
978 };
979
980 const static EGLint attribs[] = {
981 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
982 EGL_NONE
983 };
984
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500985 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400986 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500987 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500988 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500989 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500990 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400991
992 ec = malloc(sizeof *ec);
993 if (ec == NULL)
994 return NULL;
995
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400996 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400997
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400998 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400999 if (ec->display == NULL) {
1000 fprintf(stderr, "failed to create display\n");
1001 return NULL;
1002 }
1003
1004 if (!eglInitialize(ec->display, &major, &minor)) {
1005 fprintf(stderr, "failed to initialize display\n");
1006 return NULL;
1007 }
1008
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -05001009 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001010 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001011
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001012 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001013 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001014 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001015 if (ec->surface == NULL) {
1016 fprintf(stderr, "failed to create surface\n");
1017 return NULL;
1018 }
1019
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001020 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001021 if (ec->context == NULL) {
1022 fprintf(stderr, "failed to create context\n");
1023 return NULL;
1024 }
1025
1026 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1027 fprintf(stderr, "failed to make context current\n");
1028 return NULL;
1029 }
1030
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001031 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001032 glMatrixMode(GL_PROJECTION);
1033 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001034 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001035 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001036 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001037
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001038 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001039
1040 /* FIXME: This needs to be much more expressive... something like randr 1.2. */
1041 ec->output.base.interface = &wl_output_interface;
1042 wl_display_add_object(display, &ec->output.base);
1043 wl_display_add_global(display, &ec->output.base, post_output_presence);
1044
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001045 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001046
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001047 wl_list_init(&ec->input_device_list);
1048 for (i = 0; option_input_devices[i]; i++)
1049 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001050
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001051 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001052 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001053 ec->width, ec->height);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001054
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001055 shooter = screenshooter_create(ec);
1056 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001057 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001058
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001059 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001060
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001061 setup_tty(ec, loop);
1062
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001063 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001064 ec->repaint_needed = 0;
1065 ec->repaint_on_timeout = 0;
1066
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001067 schedule_repaint(ec);
1068
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001069 return ec;
1070}
1071
1072/* The plan here is to generate a random anonymous socket name and
1073 * advertise that through a service on the session dbus.
1074 */
1075static const char socket_name[] = "\0wayland";
1076
1077int main(int argc, char *argv[])
1078{
1079 struct wl_display *display;
1080 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001081 GError *error = NULL;
1082 GOptionContext *context;
1083
1084 context = g_option_context_new(NULL);
1085 g_option_context_add_main_entries(context, option_entries, "Wayland");
1086 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1087 fprintf(stderr, "option parsing failed: %s\n", error->message);
1088 exit(EXIT_FAILURE);
1089 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001090
1091 display = wl_display_create();
1092
1093 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001094 if (ec == NULL) {
1095 fprintf(stderr, "failed to create compositor\n");
1096 exit(EXIT_FAILURE);
1097 }
1098
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001099 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001100 fprintf(stderr, "failed to add socket: %m\n");
1101 exit(EXIT_FAILURE);
1102 }
1103
1104 wl_display_run(display);
1105
1106 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001107}