blob: 02cf84c12ed710d575c0d42166490363336d1142 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050024#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <fcntl.h>
27#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050028#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050031#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050032#include <linux/vt.h>
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040033#include <xf86drm.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050034#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040036
Kristian Høgsberg890bc052008-12-30 14:31:33 -050037#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
38#include <libudev.h>
39
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -040040#define GL_GLEXT_PROTOTYPES
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <GL/gl.h>
42#include <eagle.h>
43
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050045#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050047#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050048
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040049#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
50
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050051struct wlsc_matrix {
52 GLdouble d[16];
53};
54
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050055struct wl_visual {
56 struct wl_object base;
57};
58
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040059struct wlsc_surface;
60
61struct wlsc_listener {
62 struct wl_list link;
63 void (*func)(struct wlsc_listener *listener,
64 struct wlsc_surface *surface);
65};
66
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050067struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050068 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050069 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050070 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050071 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050072 EGLSurface surface;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040073 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050074
Kristian Høgsberg41a10682009-02-15 22:37:03 -050075 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050076 uint32_t crtc_id;
77 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040078
79 uint32_t fb_id[2];
80 uint32_t current;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050081};
82
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050083struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050084 struct wl_object base;
85 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050086 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050087 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050088 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050089
90 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050091 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050092 struct wlsc_surface *pointer_focus;
93 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050094 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040095
96 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050097};
98
Kristian Høgsberg8e438622009-01-26 23:07:00 -050099struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400100 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500101 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
102
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400103 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400104 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500105 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400106 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500107
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500108 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500109 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500110 struct wl_list surface_list;
111
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400112 struct wl_list surface_destroy_listener_list;
113
Ray Strode966aa112008-12-19 14:28:02 -0500114 struct wl_event_source *term_signal_source;
115
116 /* tty handling state */
117 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500118 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500119
120 struct termios terminal_attributes;
121 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500122 struct wl_event_source *enter_vt_source;
123 struct wl_event_source *leave_vt_source;
124
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500125 struct udev *udev;
126
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500127 /* Repaint state. */
128 struct wl_event_source *timer_source;
129 int repaint_needed;
130 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500131 struct timespec previous_swap;
132 uint32_t current_frame;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400133 struct wl_event_source *drm_source;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500134
135 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 struct wl_list animate_list;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400137};
138
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500139#define META_DOWN 256
140
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400141struct wlsc_vector {
142 GLdouble x, y, z;
143};
144
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500145struct wlsc_animate {
146 struct wl_list link;
147 void (*animate)(struct wlsc_animate *animate,
148 struct wlsc_compositor *compositor,
149 uint32_t frame, uint32_t msecs);
150};
151
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500152struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500153 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500154 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500155 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400156 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400157 struct wl_map map;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500158 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500159 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500160 struct wlsc_matrix matrix;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400161};
162
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500163static const char *option_background = "background.jpg";
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400164static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500165
166static const GOptionEntry option_entries[] = {
167 { "background", 'b', 0, G_OPTION_ARG_STRING,
168 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400169 { "connector", 'c', 0, G_OPTION_ARG_INT,
170 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500171 { NULL }
172};
173
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500174struct screenshooter {
175 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500176 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500177};
178
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500179struct screenshooter_interface {
180 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
181};
182
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500183static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500184screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500185{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500186 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500187 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500188 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500189 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500190 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500191 unsigned char *data;
192 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500193
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500194 i = 0;
195 output = container_of(ec->output_list.next, struct wlsc_output, link);
196 while (&output->link != &ec->output_list) {
197 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500198 data = malloc(output->width * output->height * 4);
199 if (data == NULL) {
200 fprintf(stderr, "couldn't allocate image buffer\n");
201 continue;
202 }
203
204 glReadBuffer(GL_FRONT);
205 glPixelStorei(GL_PACK_ALIGNMENT, 1);
206 glReadPixels(0, 0, output->width, output->height,
207 GL_RGBA, GL_UNSIGNED_BYTE, data);
208
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500209 /* FIXME: We should just use a RGB visual for the frontbuffer. */
210 for (j = 3; j < output->width * output->height * 4; j += 4)
211 data[j] = 0xff;
212
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500213 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500214 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500215 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500216 normal = gdk_pixbuf_flip(pixbuf, FALSE);
217 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
218 gdk_pixbuf_unref(normal);
219 gdk_pixbuf_unref(pixbuf);
220 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500221
222 output = container_of(output->link.next,
223 struct wlsc_output, link);
224 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500225}
226
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500227static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500228 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500229};
230
231static const struct wl_interface screenshooter_interface = {
232 "screenshooter", 1,
233 ARRAY_LENGTH(screenshooter_methods),
234 screenshooter_methods,
235};
236
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500237struct screenshooter_interface screenshooter_implementation = {
238 screenshooter_shoot
239};
240
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500242screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500243{
244 struct screenshooter *shooter;
245
246 shooter = malloc(sizeof *shooter);
247 if (shooter == NULL)
248 return NULL;
249
250 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500251 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500252 shooter->ec = ec;
253
254 return shooter;
255};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500256
257static void
258wlsc_matrix_init(struct wlsc_matrix *matrix)
259{
260 static const struct wlsc_matrix identity = {
261 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
262 };
263
264 memcpy(matrix, &identity, sizeof identity);
265}
266
267static void
268wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
269{
270 struct wlsc_matrix tmp;
271 const GLdouble *row, *column;
272 div_t d;
273 int i, j;
274
275 for (i = 0; i < 16; i++) {
276 tmp.d[i] = 0;
277 d = div(i, 4);
278 row = m->d + d.quot * 4;
279 column = n->d + d.rem;
280 for (j = 0; j < 4; j++)
281 tmp.d[i] += row[j] * column[j * 4];
282 }
283 memcpy(m, &tmp, sizeof tmp);
284}
285
286static void
287wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
288{
289 struct wlsc_matrix translate = {
290 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
291 };
292
293 wlsc_matrix_multiply(matrix, &translate);
294}
295
296static void
297wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
298{
299 struct wlsc_matrix scale = {
300 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
301 };
302
303 wlsc_matrix_multiply(matrix, &scale);
304}
305
306static void
307wlsc_matrix_rotate(struct wlsc_matrix *matrix,
308 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
309{
310 GLdouble c = cos(angle);
311 GLdouble s = sin(angle);
312 struct wlsc_matrix rotate = {
313 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
314 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
315 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
316 0, 0, 0, 1 }
317 };
318
319 wlsc_matrix_multiply(matrix, &rotate);
320}
321
322static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500323wlsc_surface_init(struct wlsc_surface *surface,
324 struct wlsc_compositor *compositor, struct wl_visual *visual,
325 int32_t x, int32_t y, int32_t width, int32_t height)
326{
327 glGenTextures(1, &surface->texture);
328 surface->compositor = compositor;
329 surface->map.x = x;
330 surface->map.y = y;
331 surface->map.width = width;
332 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500333 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400334 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500335}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500336
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500337static struct wlsc_surface *
338wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500339 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500340 int x, int y, int width, int height)
341{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500342 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500343 int stride;
344 void *data;
345
346 stride = cairo_image_surface_get_stride(surface);
347 data = cairo_image_surface_get_data(surface);
348
349 es = malloc(sizeof *es);
350 if (es == NULL)
351 return NULL;
352
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500353 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
354 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500355 glBindTexture(GL_TEXTURE_2D, es->texture);
356 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
361 GL_BGRA, GL_UNSIGNED_BYTE, data);
362
Kristian Høgsberg54879822008-11-23 17:07:32 -0500363 return es;
364}
365
366static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400367wlsc_surface_destroy(struct wlsc_surface *surface,
368 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500369{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400370 struct wlsc_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400371
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400372 wl_list_remove(&surface->link);
373 glDeleteTextures(1, &surface->texture);
374 wl_client_remove_surface(surface->base.client, &surface->base);
375
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400376 l = container_of(compositor->surface_destroy_listener_list.next,
377 struct wlsc_listener, link);
378 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400379 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400380 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400381 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400382 }
383
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400384 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500385}
386
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400387static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500388pointer_path(cairo_t *cr, int x, int y)
389{
390 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
391 const int width = 16, height = 16;
392
393 cairo_move_to(cr, x, y);
394 cairo_line_to(cr, x + tx, y + ty);
395 cairo_line_to(cr, x + dx, y + dy);
396 cairo_line_to(cr, x + width - end, y + height);
397 cairo_line_to(cr, x + width, y + height - end);
398 cairo_line_to(cr, x + dy, y + dx);
399 cairo_line_to(cr, x + ty, y + tx);
400 cairo_close_path(cr);
401}
402
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500403static struct wlsc_surface *
404pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500405{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500406 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500407 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500408 cairo_surface_t *surface;
409 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500410
411 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
412 width, height);
413
414 cr = cairo_create(surface);
415 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500416 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500417 cairo_set_source_rgb(cr, 0, 0, 0);
418 cairo_stroke_preserve(cr);
419 cairo_fill(cr);
420 blur_surface(surface, width);
421
422 pointer_path(cr, hotspot_x, hotspot_y);
423 cairo_stroke_preserve(cr);
424 cairo_set_source_rgb(cr, 1, 1, 1);
425 cairo_fill(cr);
426 cairo_destroy(cr);
427
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500428 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500429 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500430 x - hotspot_x,
431 y - hotspot_y,
432 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500433
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434 cairo_surface_destroy(surface);
435
Kristian Høgsberg54879822008-11-23 17:07:32 -0500436 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500437}
438
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500439static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500440background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500441{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500442 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500443 GdkPixbuf *pixbuf;
444 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500445 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500446 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500447
448 background = malloc(sizeof *background);
449 if (background == NULL)
450 return NULL;
451
452 g_type_init();
453
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500454 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500455 output->width,
456 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500457 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458 if (error != NULL) {
459 free(background);
460 return NULL;
461 }
462
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500463 data = gdk_pixbuf_get_pixels(pixbuf);
464
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500465 wlsc_surface_init(background, output->compositor,
466 &output->compositor->rgb_visual,
467 output->x, output->y, output->width, output->height);
468
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500469 glBindTexture(GL_TEXTURE_2D, background->texture);
470 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
473 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500474
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500475 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500476 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500477 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500478 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500479
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500480 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
481 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500482 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500484 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500485}
486
487static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500488wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500489{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500490 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500491 GLint vertices[12];
492 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
493 GLuint indices[4] = { 0, 1, 2, 3 };
494
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500495 vertices[0] = es->map.x;
496 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500497 vertices[2] = 0;
498
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500499 vertices[3] = es->map.x;
500 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500501 vertices[5] = 0;
502
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500503 vertices[6] = es->map.x + es->map.width;
504 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505 vertices[8] = 0;
506
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500507 vertices[9] = es->map.x + es->map.width;
508 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500509 vertices[11] = 0;
510
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500511 if (es->visual == &ec->argb_visual) {
512 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
513 glEnable(GL_BLEND);
514 } else if (es->visual == &ec->premultiplied_argb_visual) {
515 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
516 glEnable(GL_BLEND);
517 } else {
518 glDisable(GL_BLEND);
519 }
520
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500521 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400522 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500523 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500524 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500525 glEnableClientState(GL_VERTEX_ARRAY);
526 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
527 glVertexPointer(3, GL_INT, 0, vertices);
528 glTexCoordPointer(2, GL_INT, 0, tex_coords);
529 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500530 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500531}
532
533static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400534wlsc_surface_raise(struct wlsc_surface *surface)
535{
536 struct wlsc_compositor *compositor = surface->compositor;
537
538 wl_list_remove(&surface->link);
539 wl_list_insert(compositor->surface_list.prev, &surface->link);
540}
541
542static void
543wlsc_surface_lower(struct wlsc_surface *surface)
544{
545 struct wlsc_compositor *compositor = surface->compositor;
546
547 wl_list_remove(&surface->link);
548 wl_list_insert(&compositor->surface_list, &surface->link);
549}
550
551static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500552wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
553{
554 v1->x += v2->x;
555 v1->y += v2->y;
556 v1->z += v2->z;
557}
558
559static void
560wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
561{
562 v1->x -= v2->x;
563 v1->y -= v2->y;
564 v1->z -= v2->z;
565}
566
567static void
568wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
569{
570 v1->x *= s;
571 v1->y *= s;
572 v1->z *= s;
573}
574
575static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400576page_flip_handler(int fd, unsigned int frame,
577 unsigned int sec, unsigned int usec, void *data)
578{
579 struct wlsc_output *output = data;
580 struct wlsc_animate *animate, *next;
581 struct wlsc_compositor *compositor = output->compositor;
582 uint32_t msecs;
583
584 msecs = sec * 1000 + usec / 1000;
585 wl_display_post_frame(compositor->wl_display,
586 &compositor->base,
587 compositor->current_frame, msecs);
588
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400589 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400590 compositor->repaint_on_timeout = 1;
591
592 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
593 while (&animate->link != &compositor->animate_list) {
594 next = container_of(animate->link.next,
595 struct wlsc_animate, link);
596 animate->animate(animate, compositor, compositor->current_frame, msecs);
597 animate = next;
598 }
599
600 compositor->current_frame++;
601}
602
603static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500604repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400605{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500606 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500607 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500608 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500609 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400610 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500611
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500612 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
613 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500614 return;
615 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500616
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500617 glViewport(0, 0, output->width, output->height);
618 glMatrixMode(GL_PROJECTION);
619 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500620 glFrustum(-output->width / s, output->width / s,
621 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500622 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500623 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400624 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500625
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500626 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500627
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500628 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500629 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500630 else
631 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400632
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500633 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500634 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500635 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500636 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500637 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400638 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400639 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400640
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500641 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500642 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500643 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500644 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500645
646 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500647 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500648 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500649
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400650 fd = eglGetDisplayFD(ec->display);
651 output->current ^= 1;
652 eglBindColorBuffer(ec->display, output->surface, output->current);
653 drmModePageFlip(fd, output->crtc_id, output->fb_id[output->current ^ 1], output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500654}
655
656static void
657repaint(void *data)
658{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500659 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500660 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500661
662 if (!ec->repaint_needed) {
663 ec->repaint_on_timeout = 0;
664 return;
665 }
666
667 output = container_of(ec->output_list.next, struct wlsc_output, link);
668 while (&output->link != &ec->output_list) {
669 repaint_output(output);
670 output = container_of(output->link.next,
671 struct wlsc_output, link);
672 }
673
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500674 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400675}
676
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500677static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400678wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400679{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400680 struct wlsc_output *output;
681 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400682
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400683 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400684 if (compositor->repaint_on_timeout)
685 return;
686
687 fd = eglGetDisplayFD(compositor->display);
688 output = container_of(compositor->output_list.next,
689 struct wlsc_output, link);
690 while (&output->link != &compositor->output_list) {
691 drmModePageFlip(fd, output->crtc_id,
692 output->fb_id[output->current ^ 1], output);
693 output = container_of(output->link.next,
694 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500695 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400696}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500697
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500698static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500699surface_destroy(struct wl_client *client,
700 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400701{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500702 struct wlsc_surface *es = (struct wlsc_surface *) surface;
703 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500704
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500705 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400706
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400707 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400708}
709
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500710static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500711surface_attach(struct wl_client *client,
712 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500713 uint32_t width, uint32_t height, uint32_t stride,
714 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400715{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500716 struct wlsc_surface *es = (struct wlsc_surface *) surface;
717 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400718
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500719 es->width = width;
720 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400721
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500722 if (visual == &ec->argb_visual.base)
723 es->visual = &ec->argb_visual;
724 else if (visual == &ec->premultiplied_argb_visual.base)
725 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500726 else if (visual == &ec->rgb_visual.base)
727 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500728 else
729 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400730
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500731 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400732 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500733 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
734 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
735 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400736 glTextureExternalMESA(GL_TEXTURE_2D, GL_RGBA, 4,
737 width, height, stride / 4, name);
738
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400739}
740
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500741static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500742surface_map(struct wl_client *client,
743 struct wl_surface *surface,
744 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400745{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500746 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400747
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500748 es->map.x = x;
749 es->map.y = y;
750 es->map.width = width;
751 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400752}
753
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500754static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500755surface_copy(struct wl_client *client,
756 struct wl_surface *surface,
757 int32_t dst_x, int32_t dst_y,
758 uint32_t name, uint32_t stride,
759 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500760{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500761 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400762 GLuint fbo[2], rb;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500763
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400764 glGenFramebuffers(2, fbo);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500765
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400766 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo[1]);
767 glGenRenderbuffers(1, &rb);
768 glBindRenderbuffer(GL_RENDERBUFFER_EXT, rb);
769 glRenderbufferExternalMESA(GL_RENDERBUFFER_EXT,
770 GL_RGBA,
771 es->width, es->height,
772 stride / 4, name);
773 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER_EXT,
774 GL_COLOR_ATTACHMENT0_EXT,
775 GL_RENDERBUFFER_EXT,
776 rb);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400777
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400778 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo[0]);
779 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER_EXT,
780 GL_COLOR_ATTACHMENT0_EXT,
781 GL_TEXTURE_2D, es->texture, 0);
782
783 glBlitFramebuffer(x, y, x + width, y + height,
784 dst_x, dst_y, dst_x+ width, dst_y + height,
785 GL_COLOR_BUFFER_BIT, GL_NEAREST);
786
787 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
788 glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
789 glDeleteRenderbuffers(1, &rb);
790 glDeleteFramebuffers(2, fbo);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500791}
792
793static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500794surface_damage(struct wl_client *client,
795 struct wl_surface *surface,
796 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500797{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500798 /* FIXME: This need to take a damage region, of course. */
799}
800
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500801const static struct wl_surface_interface surface_interface = {
802 surface_destroy,
803 surface_attach,
804 surface_map,
805 surface_copy,
806 surface_damage
807};
808
809static void
810compositor_create_surface(struct wl_client *client,
811 struct wl_compositor *compositor, uint32_t id)
812{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500813 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400814 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500815
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400816 surface = malloc(sizeof *surface);
817 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500818 /* FIXME: Send OOM event. */
819 return;
820
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400821 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500822
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400823 wl_list_insert(ec->surface_list.prev, &surface->link);
824 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500825 &surface_interface, id);
826}
827
828static void
829compositor_commit(struct wl_client *client,
830 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500831{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500832 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500833
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400834 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500835 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500836}
837
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500838const static struct wl_compositor_interface compositor_interface = {
839 compositor_create_surface,
840 compositor_commit
841};
842
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500843static void
844wlsc_surface_transform(struct wlsc_surface *surface,
845 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
846{
847 /* Transform to surface coordinates. */
848 *sx = (x - surface->map.x) * surface->width / surface->map.width;
849 *sy = (y - surface->map.y) * surface->height / surface->map.height;
850}
851
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500852static void
853wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
854 struct wlsc_surface *surface)
855{
856 if (device->keyboard_focus == surface)
857 return;
858
859 if (device->keyboard_focus &&
860 (!surface || device->keyboard_focus->base.client != surface->base.client))
861 wl_surface_post_event(&device->keyboard_focus->base,
862 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500863 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500864
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500865 if (surface)
866 wl_surface_post_event(&surface->base,
867 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500868 WL_INPUT_KEYBOARD_FOCUS,
869 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500870
871 device->keyboard_focus = surface;
872}
873
874static void
875wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
876 struct wlsc_surface *surface)
877{
878 if (device->pointer_focus == surface)
879 return;
880
881 if (device->pointer_focus &&
882 (!surface || device->pointer_focus->base.client != surface->base.client))
883 wl_surface_post_event(&device->pointer_focus->base,
884 &device->base,
885 WL_INPUT_POINTER_FOCUS, NULL);
886 if (surface)
887 wl_surface_post_event(&surface->base,
888 &device->base,
889 WL_INPUT_POINTER_FOCUS, &surface->base);
890
891 device->pointer_focus = surface;
892}
893
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500894static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500895pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500896{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500897 struct wlsc_compositor *ec = device->ec;
898 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500899
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500900 if (device->grab > 0) {
901 wlsc_surface_transform(device->grab_surface,
902 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500903 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500904 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500905
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500906 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500907 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500908 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500909 if (es->map.x <= device->x &&
910 device->x < es->map.x + es->map.width &&
911 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500912 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500913 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500914 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500915 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500916
917 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500918 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500919
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500920 }
921
922 return NULL;
923}
924
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500925void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500926notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500927{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500928 struct wlsc_surface *es;
929 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500930 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500931 const int hotspot_x = 16, hotspot_y = 16;
932 int32_t sx, sy;
933
Ray Strodee96dcb82008-12-20 02:00:49 -0500934 if (!ec->vt_active)
935 return;
936
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500937 /* FIXME: We need some multi head love here. */
938 output = container_of(ec->output_list.next, struct wlsc_output, link);
939 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500940 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500941 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500942 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500943 if (x >= output->x + output->width)
944 x = output->x + output->width - 1;
945 if (y >= output->y + output->height)
946 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500947
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500948 device->x = x;
949 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500950 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500951
952 wlsc_input_device_set_pointer_focus(device, es);
953
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500954 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500955 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500956 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500957
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500958 device->sprite->map.x = x - hotspot_x;
959 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500960
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400961 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500962}
963
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500964void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500965notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500966 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500967{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400968 struct wlsc_surface *surface;
969 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500970 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500971
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400972 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500973 return;
974
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400975 surface = pick_surface(device, &sx, &sy);
976 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500977 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400978 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500979 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400980 device->grab_surface = surface;
981 wlsc_input_device_set_keyboard_focus(device,
982 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500983 } else {
984 device->grab--;
985 }
986
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500987 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400988 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500989 WL_INPUT_BUTTON, button, state,
990 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500991
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400992 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500993 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500994}
995
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500996static void on_term_signal(int signal_number, void *data);
997
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500998void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500999notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001000 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001001{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001002 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001003 uint32_t *k, *end;
1004
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001005 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001006 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001007
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001008 switch (key | compositor->meta_state) {
Kristian Høgsberg45b7a3a2009-08-14 05:53:50 -04001009 case KEY_BACKSPACE | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001010 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001011 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001012 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001013
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001014 end = device->keys.data + device->keys.size;
1015 for (k = device->keys.data; k < end; k++) {
1016 if (*k == key)
1017 *k = *--end;
1018 }
1019 device->keys.size = (void *) end - device->keys.data;
1020 if (state) {
1021 k = wl_array_add(&device->keys, sizeof *k);
1022 *k = key;
1023 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001024
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001025 if (device->keyboard_focus != NULL)
1026 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001027 &device->base,
1028 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001029}
1030
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001031struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001032evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001033 struct wl_display *display, const char *path);
1034
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001035static void
1036handle_surface_destroy(struct wlsc_listener *listener,
1037 struct wlsc_surface *surface)
1038{
1039 struct wlsc_input_device *device =
1040 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001041 struct wlsc_surface *focus;
1042 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001043
1044 if (device->grab_surface == surface) {
1045 device->grab_surface = NULL;
1046 device->grab = 0;
1047 }
1048 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001049 wlsc_input_device_set_keyboard_focus(device, NULL);
1050 if (device->pointer_focus == surface) {
1051 focus = pick_surface(device, &sx, &sy);
1052 wlsc_input_device_set_pointer_focus(device, focus);
1053 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
1054 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001055}
1056
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001057static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001058create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001059{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001060 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001062 device = malloc(sizeof *device);
1063 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001064 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001065
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001066 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001067 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001068 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001069 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001070 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001071 device->x = 100;
1072 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001073 device->ec = ec;
1074
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001075 device->listener.func = handle_surface_destroy;
1076 wl_list_insert(ec->surface_destroy_listener_list.prev,
1077 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001078 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001079
1080 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001081}
1082
1083void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001084wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001085{
1086 *x = device->x;
1087 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001088}
1089
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001090static void
1091post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001092{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001093 struct wlsc_output *output =
1094 container_of(global, struct wlsc_output, base);
1095
1096 wl_client_post_event(client, global,
1097 WL_OUTPUT_GEOMETRY,
1098 output->width, output->height);
1099}
1100
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001101static void
1102on_drm_input(int fd, uint32_t mask, void *data)
1103{
1104 drmEventContext evctx;
1105
1106 memset(&evctx, 0, sizeof evctx);
1107 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1108 evctx.page_flip_handler = page_flip_handler;
1109 drmHandleEvent(fd, &evctx);
1110}
1111
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001112static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001113init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001114{
1115 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001116 EGL_DEPTH_SIZE, 0,
1117 EGL_STENCIL_SIZE, 0,
1118 EGL_CONFIG_CAVEAT, EGL_NONE,
1119 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001120 EGL_NONE
1121 };
1122
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001123 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001124 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001125 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001126
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001127 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001128 if (ec->display == NULL) {
1129 fprintf(stderr, "failed to create display\n");
1130 return -1;
1131 }
1132
1133 if (!eglInitialize(ec->display, &major, &minor)) {
1134 fprintf(stderr, "failed to initialize display\n");
1135 return -1;
1136 }
1137
1138 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1139 return -1;
1140
1141 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1142 if (ec->context == NULL) {
1143 fprintf(stderr, "failed to create context\n");
1144 return -1;
1145 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001146
1147 loop = wl_display_get_event_loop(ec->wl_display);
1148 fd = eglGetDisplayFD(ec->display);
1149 ec->drm_source =
1150 wl_event_loop_add_fd(loop, fd,
1151 WL_EVENT_READABLE, on_drm_input, ec);
1152
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001153 return 0;
1154}
1155
1156static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001157create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001158{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001159 drmModeConnector *connector;
1160 drmModeRes *resources;
1161 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001162 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001163 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001164 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001165 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001166
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001167 if (ec->display == NULL && init_egl(ec, device) < 0) {
1168 fprintf(stderr, "failed to initialize egl\n");
1169 return -1;
1170 }
1171
1172 output = malloc(sizeof *output);
1173 if (output == NULL)
1174 return -1;
1175
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001176 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001177 resources = drmModeGetResources(fd);
1178 if (!resources) {
1179 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001180 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001181 }
1182
1183 for (i = 0; i < resources->count_connectors; i++) {
1184 connector = drmModeGetConnector(fd, resources->connectors[i]);
1185 if (connector == NULL)
1186 continue;
1187
1188 if (connector->connection == DRM_MODE_CONNECTED &&
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -04001189 connector->count_modes > 0 &&
1190 (option_connector == 0 ||
1191 connector->connector_id == option_connector))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001192 break;
1193
1194 drmModeFreeConnector(connector);
1195 }
1196
1197 if (i == resources->count_connectors) {
1198 fprintf(stderr, "No currently active connector found.\n");
1199 return -1;
1200 }
1201
1202 mode = &connector->modes[0];
1203
1204 for (i = 0; i < resources->count_encoders; i++) {
1205 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1206
1207 if (encoder == NULL)
1208 continue;
1209
1210 if (encoder->encoder_id == connector->encoder_id)
1211 break;
1212
1213 drmModeFreeEncoder(encoder);
1214 }
1215
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001216 output->compositor = ec;
1217 output->crtc_id = encoder->crtc_id;
1218 output->connector_id = connector->connector_id;
1219 output->mode = mode;
1220 output->x = 0;
1221 output->y = 0;
1222 output->width = mode->hdisplay;
1223 output->height = mode->vdisplay;
1224
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001225 output->surface = eglCreateSurface(ec->display,
1226 ec->config,
1227 output->width,
1228 output->height,
1229 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001230 if (output->surface == NULL) {
1231 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001232 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001233 }
1234
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001235 for (i = 0; i < 2; i++) {
1236 eglGetColorBuffer(output->surface,
1237 i, &name, &handle, &stride);
1238
1239 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1240 32, 32, stride, handle, &output->fb_id[i]);
1241 if (ret) {
1242 fprintf(stderr, "failed to add fb %d: %m\n", i);
1243 return -1;
1244 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001245 }
1246
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001247 output->current = 0;
1248 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1249 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001250 &connector->connector_id, 1, mode);
1251 if (ret) {
1252 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001253 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001254 }
1255
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001256 output->base.interface = &wl_output_interface;
1257 wl_display_add_object(ec->wl_display, &output->base);
1258 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1259 wl_list_insert(ec->output_list.prev, &output->link);
1260
1261 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1262 fprintf(stderr, "failed to make context current\n");
1263 return -1;
1264 }
1265
1266 output->background = background_create(output, option_background);
1267
1268 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001269}
1270
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001271static const struct wl_interface visual_interface = {
1272 "visual", 1,
1273};
1274
1275static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001276add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001277{
1278 ec->argb_visual.base.interface = &visual_interface;
1279 ec->argb_visual.base.implementation = NULL;
1280 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001281 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001282
1283 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1284 ec->premultiplied_argb_visual.base.implementation = NULL;
1285 wl_display_add_object(ec->wl_display,
1286 &ec->premultiplied_argb_visual.base);
1287 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001288 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001289
1290 ec->rgb_visual.base.interface = &visual_interface;
1291 ec->rgb_visual.base.implementation = NULL;
1292 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001293 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1294}
1295
Ray Strode19ad6a92008-12-19 01:45:41 -05001296static void on_enter_vt(int signal_number, void *data)
1297{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001298 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001299 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001300 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001301
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001302 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001303 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001304
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001305 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001306 output = container_of(ec->output_list.next, struct wlsc_output, link);
1307 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001308 ret = drmModeSetCrtc(fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001309 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001310 &output->connector_id, 1, output->mode);
1311 if (ret)
1312 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1313 output->connector_id);
1314
1315 output = container_of(output->link.next,
1316 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001317 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001318}
1319
1320static void on_leave_vt(int signal_number, void *data)
1321{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001322 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001323
1324 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001325 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001326}
1327
Ray Strode966aa112008-12-19 14:28:02 -05001328static void
1329on_tty_input(int fd, uint32_t mask, void *data)
1330{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001331 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001332
1333 /* Ignore input to tty. We get keyboard events from evdev
1334 */
1335 tcflush(ec->tty_fd, TCIFLUSH);
1336}
1337
1338static void on_term_signal(int signal_number, void *data)
1339{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001340 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001341
1342 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1343 fprintf(stderr, "could not restore terminal to canonical mode\n");
1344
1345 exit(0);
1346}
1347
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001348static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001349{
1350 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001351 struct vt_mode mode = { 0 };
1352
1353 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1354 if (ec->tty_fd <= 0) {
1355 fprintf(stderr, "failed to open active tty: %m\n");
1356 return -1;
1357 }
Ray Strode966aa112008-12-19 14:28:02 -05001358
1359 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1360 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001361 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001362 }
1363
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001364 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001365 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001366 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001367
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001368 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001369 raw_attributes.c_oflag |= OPOST | OCRNL;
1370
1371 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1372 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1373
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001374 ec->term_signal_source =
1375 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001376
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001377 ec->tty_input_source =
1378 wl_event_loop_add_fd(loop, ec->tty_fd,
1379 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001380
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001381 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001382 mode.mode = VT_PROCESS;
1383 mode.relsig = SIGUSR1;
1384 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001385 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001386 fprintf(stderr, "failed to take control of vt handling\n");
1387 }
1388
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001389 ec->leave_vt_source =
1390 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1391 ec->enter_vt_source =
1392 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001393
1394 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001395}
1396
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001397static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001398init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001399{
1400 struct udev_enumerate *e;
1401 struct udev_list_entry *entry;
1402 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001403 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001404 struct wlsc_input_device *input_device;
1405
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001406 ec->udev = udev_new();
1407 if (ec->udev == NULL) {
1408 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001409 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001410 }
1411
1412 input_device = create_input_device(ec);
1413
1414 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001415 udev_enumerate_add_match_subsystem(e, "input");
1416 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001417 udev_enumerate_scan_devices(e);
1418 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1419 path = udev_list_entry_get_name(entry);
1420 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001421 evdev_input_device_create(input_device, ec->wl_display,
1422 udev_device_get_devnode(device));
1423 }
1424 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001425
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001426 e = udev_enumerate_new(ec->udev);
1427 udev_enumerate_add_match_subsystem(e, "drm");
1428 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1429 udev_enumerate_scan_devices(e);
1430 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1431 path = udev_list_entry_get_name(entry);
1432 device = udev_device_new_from_syspath(ec->udev, path);
1433 if (create_output(ec, device) < 0) {
1434 fprintf(stderr, "failed to create output for %s\n", path);
1435 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001436 }
1437 }
1438 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001439
1440 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001441 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001442 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1443
1444 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001445}
1446
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001447static struct wlsc_compositor *
1448wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001449{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001450 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001451 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001452 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001453
1454 ec = malloc(sizeof *ec);
1455 if (ec == NULL)
1456 return NULL;
1457
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001458 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001459 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001460
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001461 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001462
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001463 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001464
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001465 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001466 wl_list_init(&ec->input_device_list);
1467 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001468 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001469 if (init_libudev(ec) < 0) {
1470 fprintf(stderr, "failed to initialize devices\n");
1471 return NULL;
1472 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001473
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001474 shooter = screenshooter_create(ec);
1475 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001476 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001477
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001478 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001479
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001480 setup_tty(ec, loop);
1481
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001482 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001483 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001484
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001485 wl_list_init(&ec->animate_list);
1486
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001487 return ec;
1488}
1489
1490/* The plan here is to generate a random anonymous socket name and
1491 * advertise that through a service on the session dbus.
1492 */
1493static const char socket_name[] = "\0wayland";
1494
1495int main(int argc, char *argv[])
1496{
1497 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001498 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001499 GError *error = NULL;
1500 GOptionContext *context;
1501
1502 context = g_option_context_new(NULL);
1503 g_option_context_add_main_entries(context, option_entries, "Wayland");
1504 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1505 fprintf(stderr, "option parsing failed: %s\n", error->message);
1506 exit(EXIT_FAILURE);
1507 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001508
1509 display = wl_display_create();
1510
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001511 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001512 if (ec == NULL) {
1513 fprintf(stderr, "failed to create compositor\n");
1514 exit(EXIT_FAILURE);
1515 }
1516
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001517 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001518 fprintf(stderr, "failed to add socket: %m\n");
1519 exit(EXIT_FAILURE);
1520 }
1521
1522 wl_display_run(display);
1523
1524 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001525}