blob: 09daebf1937a26b31fd74a0b15b34c31f95ccc1d [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øgsbergb3131d92008-12-24 19:30:25 -0500705 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500706 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500707 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500708 device->x = 100;
709 device->y = 100;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500710 device->pointer_surface =
711 pointer_create(ec, device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500712 device->ec = ec;
713
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500714 dir = opendir(by_path_dir);
715 if (dir == NULL) {
716 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
717 return;
718 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500719
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500720 while (de = readdir(dir), de != NULL) {
721 if (fnmatch(glob, de->d_name, 0))
722 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500723
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500724 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
725 evdev_input_device_create(device, ec->wl_display, path);
726 }
727 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500728
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500729 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500730}
731
732void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500733wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500734{
735 *x = device->x;
736 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500737}
738
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500739static uint32_t
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500740create_frontbuffer(struct egl_compositor *ec)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500741{
742 drmModeConnector *connector;
743 drmModeRes *resources;
744 drmModeEncoder *encoder;
745 struct drm_mode_modeinfo *mode;
746 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500747 struct drm_gem_flink flink;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500748 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500749
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500750 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500751 resources = drmModeGetResources(fd);
752 if (!resources) {
753 fprintf(stderr, "drmModeGetResources failed\n");
754 return 0;
755 }
756
757 for (i = 0; i < resources->count_connectors; i++) {
758 connector = drmModeGetConnector(fd, resources->connectors[i]);
759 if (connector == NULL)
760 continue;
761
762 if (connector->connection == DRM_MODE_CONNECTED &&
763 connector->count_modes > 0)
764 break;
765
766 drmModeFreeConnector(connector);
767 }
768
769 if (i == resources->count_connectors) {
770 fprintf(stderr, "No currently active connector found.\n");
771 return -1;
772 }
773
774 mode = &connector->modes[0];
775
776 for (i = 0; i < resources->count_encoders; i++) {
777 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
778
779 if (encoder == NULL)
780 continue;
781
782 if (encoder->encoder_id == connector->encoder_id)
783 break;
784
785 drmModeFreeEncoder(encoder);
786 }
787
788 /* Mode size at 32 bpp */
789 create.size = mode->hdisplay * mode->vdisplay * 4;
790 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
791 fprintf(stderr, "gem create failed: %m\n");
792 return 0;
793 }
794
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500795 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500796 32, 32, mode->hdisplay * 4, create.handle, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500797 if (ret) {
798 fprintf(stderr, "failed to add fb: %m\n");
799 return 0;
800 }
801
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500802 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500803 &connector->connector_id, 1, mode);
804 if (ret) {
805 fprintf(stderr, "failed to set mode: %m\n");
806 return 0;
807 }
808
809 flink.handle = create.handle;
810 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
811 fprintf(stderr, "gem flink failed: %m\n");
812 return 0;
813 }
814
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500815 ec->crtc_id = encoder->crtc_id;
816 ec->connector_id = connector->connector_id;
817 ec->mode = mode;
818 ec->width = mode->hdisplay;
819 ec->height = mode->vdisplay;
820 ec->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500821
822 return flink.name;
823}
824
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500825static const struct wl_interface visual_interface = {
826 "visual", 1,
827};
828
829static void
830add_visuals(struct egl_compositor *ec)
831{
832 ec->argb_visual.base.interface = &visual_interface;
833 ec->argb_visual.base.implementation = NULL;
834 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500835 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500836
837 ec->premultiplied_argb_visual.base.interface = &visual_interface;
838 ec->premultiplied_argb_visual.base.implementation = NULL;
839 wl_display_add_object(ec->wl_display,
840 &ec->premultiplied_argb_visual.base);
841 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500842 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500843
844 ec->rgb_visual.base.interface = &visual_interface;
845 ec->rgb_visual.base.implementation = NULL;
846 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500847 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
848}
849
850static void
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -0500851post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500852{
853 struct egl_compositor *ec =
854 container_of(global, struct egl_compositor, output.base);
855
856 wl_client_post_event(client, global,
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -0500857 WL_OUTPUT_GEOMETRY, ec->width, ec->height);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500858}
859
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400860static const char gem_device[] = "/dev/dri/card0";
861
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500862static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -0500863 "pci-0000:00:1d.2-usb-0:2:1*event*",
864 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500865};
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500866
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500867static const char *option_background = "background.jpg";
868static const char **option_input_devices = macbook_air_default_input_device;
869
870static const GOptionEntry option_entries[] = {
871 { "background", 'b', 0, G_OPTION_ARG_STRING,
872 &option_background, "Background image" },
873 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
874 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -0500875 { NULL }
876};
877
Ray Strode19ad6a92008-12-19 01:45:41 -0500878static void on_enter_vt(int signal_number, void *data)
879{
880 struct egl_compositor *ec = data;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500881 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500882
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500883 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500884 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500885
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500886 fd = eglGetDisplayFD(ec->display);
887 ret = drmModeSetCrtc(fd, ec->crtc_id, ec->fb_id, 0, 0,
888 &ec->connector_id, 1, ec->mode);
Ray Strode19ad6a92008-12-19 01:45:41 -0500889 if (ret) {
890 fprintf(stderr, "failed to set mode: %m\n");
891 return;
892 }
Ray Strode19ad6a92008-12-19 01:45:41 -0500893}
894
895static void on_leave_vt(int signal_number, void *data)
896{
897 struct egl_compositor *ec = data;
898
899 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -0500900 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500901}
902
Ray Strode966aa112008-12-19 14:28:02 -0500903static void
904on_tty_input(int fd, uint32_t mask, void *data)
905{
906 struct egl_compositor *ec = data;
907
908 /* Ignore input to tty. We get keyboard events from evdev
909 */
910 tcflush(ec->tty_fd, TCIFLUSH);
911}
912
913static void on_term_signal(int signal_number, void *data)
914{
915 struct egl_compositor *ec = data;
916
917 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
918 fprintf(stderr, "could not restore terminal to canonical mode\n");
919
920 exit(0);
921}
922
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500923static int setup_tty(struct egl_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -0500924{
925 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500926 struct vt_mode mode = { 0 };
927
928 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
929 if (ec->tty_fd <= 0) {
930 fprintf(stderr, "failed to open active tty: %m\n");
931 return -1;
932 }
Ray Strode966aa112008-12-19 14:28:02 -0500933
934 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
935 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500936 return -1;
Ray Strode966aa112008-12-19 14:28:02 -0500937 }
938
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500939 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -0500940 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500941 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -0500942
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500943 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -0500944 raw_attributes.c_oflag |= OPOST | OCRNL;
945
946 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
947 fprintf(stderr, "could not put terminal into raw mode: %m\n");
948
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500949 ec->term_signal_source =
950 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500951
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500952 ec->tty_input_source =
953 wl_event_loop_add_fd(loop, ec->tty_fd,
954 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -0500955
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500956 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -0500957 mode.mode = VT_PROCESS;
958 mode.relsig = SIGUSR1;
959 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500960 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -0500961 fprintf(stderr, "failed to take control of vt handling\n");
962 }
963
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500964 ec->leave_vt_source =
965 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
966 ec->enter_vt_source =
967 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500968
969 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -0500970}
971
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500972static struct egl_compositor *
973egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400974{
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -0500975 static const EGLint config_attribs[] = {
976 EGL_DEPTH_SIZE, 0,
977 EGL_STENCIL_SIZE, 0,
978 EGL_CONFIG_CAVEAT, EGL_NONE,
979 EGL_NONE
980 };
981
982 const static EGLint attribs[] = {
983 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
984 EGL_NONE
985 };
986
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500987 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400988 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500989 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500990 uint32_t fb_name;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500991 int i;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500992 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400993
994 ec = malloc(sizeof *ec);
995 if (ec == NULL)
996 return NULL;
997
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400998 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400999
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001000 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001001 if (ec->display == NULL) {
1002 fprintf(stderr, "failed to create display\n");
1003 return NULL;
1004 }
1005
1006 if (!eglInitialize(ec->display, &major, &minor)) {
1007 fprintf(stderr, "failed to initialize display\n");
1008 return NULL;
1009 }
1010
Kristian Høgsberg15d0f8b2008-12-19 12:13:42 -05001011 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001012 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001013
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001014 fb_name = create_frontbuffer(ec);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001015 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001016 fb_name, ec->width, ec->height, ec->stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001017 if (ec->surface == NULL) {
1018 fprintf(stderr, "failed to create surface\n");
1019 return NULL;
1020 }
1021
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001022 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001023 if (ec->context == NULL) {
1024 fprintf(stderr, "failed to create context\n");
1025 return NULL;
1026 }
1027
1028 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1029 fprintf(stderr, "failed to make context current\n");
1030 return NULL;
1031 }
1032
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001033 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001034 glMatrixMode(GL_PROJECTION);
1035 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001036 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001037 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001038 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001039
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001040 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001041
1042 /* FIXME: This needs to be much more expressive... something like randr 1.2. */
1043 ec->output.base.interface = &wl_output_interface;
1044 wl_display_add_object(display, &ec->output.base);
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -05001045 wl_display_add_global(display, &ec->output.base, post_output_geometry);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001046
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001047 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001048
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001049 wl_list_init(&ec->input_device_list);
1050 for (i = 0; option_input_devices[i]; i++)
1051 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001052
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001053 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001054 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001055 ec->width, ec->height);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001056
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001057 shooter = screenshooter_create(ec);
1058 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001059 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001060
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001061 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001062
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001063 setup_tty(ec, loop);
1064
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001065 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001066 ec->repaint_needed = 0;
1067 ec->repaint_on_timeout = 0;
1068
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001069 schedule_repaint(ec);
1070
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001071 return ec;
1072}
1073
1074/* The plan here is to generate a random anonymous socket name and
1075 * advertise that through a service on the session dbus.
1076 */
1077static const char socket_name[] = "\0wayland";
1078
1079int main(int argc, char *argv[])
1080{
1081 struct wl_display *display;
1082 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001083 GError *error = NULL;
1084 GOptionContext *context;
1085
1086 context = g_option_context_new(NULL);
1087 g_option_context_add_main_entries(context, option_entries, "Wayland");
1088 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1089 fprintf(stderr, "option parsing failed: %s\n", error->message);
1090 exit(EXIT_FAILURE);
1091 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001092
1093 display = wl_display_create();
1094
1095 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001096 if (ec == NULL) {
1097 fprintf(stderr, "failed to create compositor\n");
1098 exit(EXIT_FAILURE);
1099 }
1100
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001101 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001102 fprintf(stderr, "failed to add socket: %m\n");
1103 exit(EXIT_FAILURE);
1104 }
1105
1106 wl_display_run(display);
1107
1108 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001109}