blob: 4bc6e57226996a03b34c69708c4c9c571c4c7f4c [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
372 l = container_of(compositor->surface_destroy_listener_list.next,
373 struct wlsc_listener, link);
374 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400375 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400376 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400377 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400378 }
379
380 wl_list_remove(&surface->link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400381
382 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400383 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500384}
385
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400386static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500387pointer_path(cairo_t *cr, int x, int y)
388{
389 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
390 const int width = 16, height = 16;
391
392 cairo_move_to(cr, x, y);
393 cairo_line_to(cr, x + tx, y + ty);
394 cairo_line_to(cr, x + dx, y + dy);
395 cairo_line_to(cr, x + width - end, y + height);
396 cairo_line_to(cr, x + width, y + height - end);
397 cairo_line_to(cr, x + dy, y + dx);
398 cairo_line_to(cr, x + ty, y + tx);
399 cairo_close_path(cr);
400}
401
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500402static struct wlsc_surface *
403pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500404{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500405 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500406 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500407 cairo_surface_t *surface;
408 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500409
410 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
411 width, height);
412
413 cr = cairo_create(surface);
414 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500415 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500416 cairo_set_source_rgb(cr, 0, 0, 0);
417 cairo_stroke_preserve(cr);
418 cairo_fill(cr);
419 blur_surface(surface, width);
420
421 pointer_path(cr, hotspot_x, hotspot_y);
422 cairo_stroke_preserve(cr);
423 cairo_set_source_rgb(cr, 1, 1, 1);
424 cairo_fill(cr);
425 cairo_destroy(cr);
426
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500427 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500428 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500429 x - hotspot_x,
430 y - hotspot_y,
431 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500432
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500433 cairo_surface_destroy(surface);
434
Kristian Høgsberg54879822008-11-23 17:07:32 -0500435 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500436}
437
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500438static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500439background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500440{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500441 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500442 GdkPixbuf *pixbuf;
443 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500444 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500445 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500446
447 background = malloc(sizeof *background);
448 if (background == NULL)
449 return NULL;
450
451 g_type_init();
452
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500453 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500454 output->width,
455 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500456 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500457 if (error != NULL) {
458 free(background);
459 return NULL;
460 }
461
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500462 data = gdk_pixbuf_get_pixels(pixbuf);
463
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500464 wlsc_surface_init(background, output->compositor,
465 &output->compositor->rgb_visual,
466 output->x, output->y, output->width, output->height);
467
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500468 glBindTexture(GL_TEXTURE_2D, background->texture);
469 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500470 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500473
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500474 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500475 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500476 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500477 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500478
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500479 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
480 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500481 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500482
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500484}
485
486static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500487wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500488{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500489 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500490 GLint vertices[12];
491 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
492 GLuint indices[4] = { 0, 1, 2, 3 };
493
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494 vertices[0] = es->map.x;
495 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500496 vertices[2] = 0;
497
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 vertices[3] = es->map.x;
499 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500500 vertices[5] = 0;
501
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500502 vertices[6] = es->map.x + es->map.width;
503 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500504 vertices[8] = 0;
505
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500506 vertices[9] = es->map.x + es->map.width;
507 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508 vertices[11] = 0;
509
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500510 if (es->visual == &ec->argb_visual) {
511 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
512 glEnable(GL_BLEND);
513 } else if (es->visual == &ec->premultiplied_argb_visual) {
514 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
515 glEnable(GL_BLEND);
516 } else {
517 glDisable(GL_BLEND);
518 }
519
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500520 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400521 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500522 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500523 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500524 glEnableClientState(GL_VERTEX_ARRAY);
525 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
526 glVertexPointer(3, GL_INT, 0, vertices);
527 glTexCoordPointer(2, GL_INT, 0, tex_coords);
528 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500529 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500530}
531
532static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400533wlsc_surface_raise(struct wlsc_surface *surface)
534{
535 struct wlsc_compositor *compositor = surface->compositor;
536
537 wl_list_remove(&surface->link);
538 wl_list_insert(compositor->surface_list.prev, &surface->link);
539}
540
541static void
542wlsc_surface_lower(struct wlsc_surface *surface)
543{
544 struct wlsc_compositor *compositor = surface->compositor;
545
546 wl_list_remove(&surface->link);
547 wl_list_insert(&compositor->surface_list, &surface->link);
548}
549
550static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500551wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
552{
553 v1->x += v2->x;
554 v1->y += v2->y;
555 v1->z += v2->z;
556}
557
558static void
559wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
560{
561 v1->x -= v2->x;
562 v1->y -= v2->y;
563 v1->z -= v2->z;
564}
565
566static void
567wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
568{
569 v1->x *= s;
570 v1->y *= s;
571 v1->z *= s;
572}
573
574static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400575page_flip_handler(int fd, unsigned int frame,
576 unsigned int sec, unsigned int usec, void *data)
577{
578 struct wlsc_output *output = data;
579 struct wlsc_animate *animate, *next;
580 struct wlsc_compositor *compositor = output->compositor;
581 uint32_t msecs;
582
583 msecs = sec * 1000 + usec / 1000;
584 wl_display_post_frame(compositor->wl_display,
585 &compositor->base,
586 compositor->current_frame, msecs);
587
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400588 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400589 compositor->repaint_on_timeout = 1;
590
591 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
592 while (&animate->link != &compositor->animate_list) {
593 next = container_of(animate->link.next,
594 struct wlsc_animate, link);
595 animate->animate(animate, compositor, compositor->current_frame, msecs);
596 animate = next;
597 }
598
599 compositor->current_frame++;
600}
601
602static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500603repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400604{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500605 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500606 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500607 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500608 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400609 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500610
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500611 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
612 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500613 return;
614 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500615
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500616 glViewport(0, 0, output->width, output->height);
617 glMatrixMode(GL_PROJECTION);
618 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500619 glFrustum(-output->width / s, output->width / s,
620 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500621 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500622 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400623 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500624
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500625 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500626
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500627 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500628 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500629 else
630 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400631
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500632 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500633 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500634 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500635 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500636 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400637 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400638 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400639
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500640 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500641 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500642 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500643 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500644
645 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500646 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500647 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500648
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400649 fd = eglGetDisplayFD(ec->display);
650 output->current ^= 1;
651 eglBindColorBuffer(ec->display, output->surface, output->current);
652 drmModePageFlip(fd, output->crtc_id, output->fb_id[output->current ^ 1], output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500653}
654
655static void
656repaint(void *data)
657{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500658 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500659 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500660
661 if (!ec->repaint_needed) {
662 ec->repaint_on_timeout = 0;
663 return;
664 }
665
666 output = container_of(ec->output_list.next, struct wlsc_output, link);
667 while (&output->link != &ec->output_list) {
668 repaint_output(output);
669 output = container_of(output->link.next,
670 struct wlsc_output, link);
671 }
672
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500673 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400674}
675
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500676static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400677wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400678{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400679 struct wlsc_output *output;
680 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400681
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400682 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400683 if (compositor->repaint_on_timeout)
684 return;
685
686 fd = eglGetDisplayFD(compositor->display);
687 output = container_of(compositor->output_list.next,
688 struct wlsc_output, link);
689 while (&output->link != &compositor->output_list) {
690 drmModePageFlip(fd, output->crtc_id,
691 output->fb_id[output->current ^ 1], output);
692 output = container_of(output->link.next,
693 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500694 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400695}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500696
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500697static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500698surface_destroy(struct wl_client *client,
699 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400700{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500701 struct wlsc_surface *es = (struct wlsc_surface *) surface;
702 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500703
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500704 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400705
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400706 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400707}
708
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500709static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500710surface_attach(struct wl_client *client,
711 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500712 uint32_t width, uint32_t height, uint32_t stride,
713 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400714{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500715 struct wlsc_surface *es = (struct wlsc_surface *) surface;
716 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400717
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500718 es->width = width;
719 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400720
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500721 if (visual == &ec->argb_visual.base)
722 es->visual = &ec->argb_visual;
723 else if (visual == &ec->premultiplied_argb_visual.base)
724 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500725 else if (visual == &ec->rgb_visual.base)
726 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500727 else
728 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400729
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500730 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400731 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500732 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
733 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
734 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400735 glTextureExternalMESA(GL_TEXTURE_2D, GL_RGBA, 4,
736 width, height, stride / 4, name);
737
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400738}
739
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500740static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500741surface_map(struct wl_client *client,
742 struct wl_surface *surface,
743 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400744{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500745 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400746
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500747 es->map.x = x;
748 es->map.y = y;
749 es->map.width = width;
750 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400751}
752
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500753static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500754surface_copy(struct wl_client *client,
755 struct wl_surface *surface,
756 int32_t dst_x, int32_t dst_y,
757 uint32_t name, uint32_t stride,
758 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500759{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500760 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400761 GLuint fbo[2], rb;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500762
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400763 glGenFramebuffers(2, fbo);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500764
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400765 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo[1]);
766 glGenRenderbuffers(1, &rb);
767 glBindRenderbuffer(GL_RENDERBUFFER_EXT, rb);
768 glRenderbufferExternalMESA(GL_RENDERBUFFER_EXT,
769 GL_RGBA,
770 es->width, es->height,
771 stride / 4, name);
772 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER_EXT,
773 GL_COLOR_ATTACHMENT0_EXT,
774 GL_RENDERBUFFER_EXT,
775 rb);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400776
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400777 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo[0]);
778 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER_EXT,
779 GL_COLOR_ATTACHMENT0_EXT,
780 GL_TEXTURE_2D, es->texture, 0);
781
782 glBlitFramebuffer(x, y, x + width, y + height,
783 dst_x, dst_y, dst_x+ width, dst_y + height,
784 GL_COLOR_BUFFER_BIT, GL_NEAREST);
785
786 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
787 glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
788 glDeleteRenderbuffers(1, &rb);
789 glDeleteFramebuffers(2, fbo);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500790}
791
792static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500793surface_damage(struct wl_client *client,
794 struct wl_surface *surface,
795 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500796{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500797 /* FIXME: This need to take a damage region, of course. */
798}
799
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500800const static struct wl_surface_interface surface_interface = {
801 surface_destroy,
802 surface_attach,
803 surface_map,
804 surface_copy,
805 surface_damage
806};
807
808static void
809compositor_create_surface(struct wl_client *client,
810 struct wl_compositor *compositor, uint32_t id)
811{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500812 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400813 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500814
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400815 surface = malloc(sizeof *surface);
816 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500817 /* FIXME: Send OOM event. */
818 return;
819
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400820 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500821
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400822 wl_list_insert(ec->surface_list.prev, &surface->link);
823 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500824 &surface_interface, id);
825}
826
827static void
828compositor_commit(struct wl_client *client,
829 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500830{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500831 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500832
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400833 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500834 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500835}
836
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500837const static struct wl_compositor_interface compositor_interface = {
838 compositor_create_surface,
839 compositor_commit
840};
841
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500842static void
843wlsc_surface_transform(struct wlsc_surface *surface,
844 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
845{
846 /* Transform to surface coordinates. */
847 *sx = (x - surface->map.x) * surface->width / surface->map.width;
848 *sy = (y - surface->map.y) * surface->height / surface->map.height;
849}
850
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500851static void
852wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
853 struct wlsc_surface *surface)
854{
855 if (device->keyboard_focus == surface)
856 return;
857
858 if (device->keyboard_focus &&
859 (!surface || device->keyboard_focus->base.client != surface->base.client))
860 wl_surface_post_event(&device->keyboard_focus->base,
861 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500862 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500863
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500864 if (surface)
865 wl_surface_post_event(&surface->base,
866 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500867 WL_INPUT_KEYBOARD_FOCUS,
868 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500869
870 device->keyboard_focus = surface;
871}
872
873static void
874wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
875 struct wlsc_surface *surface)
876{
877 if (device->pointer_focus == surface)
878 return;
879
880 if (device->pointer_focus &&
881 (!surface || device->pointer_focus->base.client != surface->base.client))
882 wl_surface_post_event(&device->pointer_focus->base,
883 &device->base,
884 WL_INPUT_POINTER_FOCUS, NULL);
885 if (surface)
886 wl_surface_post_event(&surface->base,
887 &device->base,
888 WL_INPUT_POINTER_FOCUS, &surface->base);
889
890 device->pointer_focus = surface;
891}
892
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500893static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500894pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500895{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500896 struct wlsc_compositor *ec = device->ec;
897 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500898
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500899 if (device->grab > 0) {
900 wlsc_surface_transform(device->grab_surface,
901 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500902 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500903 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500904
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500905 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500906 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500907 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500908 if (es->map.x <= device->x &&
909 device->x < es->map.x + es->map.width &&
910 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500911 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500912 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500913 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500914 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500915
916 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500917 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500918
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500919 }
920
921 return NULL;
922}
923
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500924void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500925notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500926{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500927 struct wlsc_surface *es;
928 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500929 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500930 const int hotspot_x = 16, hotspot_y = 16;
931 int32_t sx, sy;
932
Ray Strodee96dcb82008-12-20 02:00:49 -0500933 if (!ec->vt_active)
934 return;
935
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500936 /* FIXME: We need some multi head love here. */
937 output = container_of(ec->output_list.next, struct wlsc_output, link);
938 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500939 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500940 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500941 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500942 if (x >= output->x + output->width)
943 x = output->x + output->width - 1;
944 if (y >= output->y + output->height)
945 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500946
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500947 device->x = x;
948 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500949 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500950
951 wlsc_input_device_set_pointer_focus(device, es);
952
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500953 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500954 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500955 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500956
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500957 device->sprite->map.x = x - hotspot_x;
958 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500959
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400960 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500961}
962
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500963void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500964notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500965 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500966{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400967 struct wlsc_surface *surface;
968 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500969 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500970
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400971 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500972 return;
973
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400974 surface = pick_surface(device, &sx, &sy);
975 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500976 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400977 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500978 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400979 device->grab_surface = surface;
980 wlsc_input_device_set_keyboard_focus(device,
981 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500982 } else {
983 device->grab--;
984 }
985
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500986 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400987 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500988 WL_INPUT_BUTTON, button, state,
989 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500990
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400991 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500992 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500993}
994
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500995static void on_term_signal(int signal_number, void *data);
996
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500997void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500998notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500999 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001000{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001001 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001002 uint32_t *k, *end;
1003
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001004 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001005 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001006
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001007 switch (key | compositor->meta_state) {
Kristian Høgsberg45b7a3a2009-08-14 05:53:50 -04001008 case KEY_BACKSPACE | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001009 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001010 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001011 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001012
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001013 end = device->keys.data + device->keys.size;
1014 for (k = device->keys.data; k < end; k++) {
1015 if (*k == key)
1016 *k = *--end;
1017 }
1018 device->keys.size = (void *) end - device->keys.data;
1019 if (state) {
1020 k = wl_array_add(&device->keys, sizeof *k);
1021 *k = key;
1022 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001023
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001024 if (device->keyboard_focus != NULL)
1025 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001026 &device->base,
1027 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001028}
1029
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001030struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001031evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001032 struct wl_display *display, const char *path);
1033
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001034static void
1035handle_surface_destroy(struct wlsc_listener *listener,
1036 struct wlsc_surface *surface)
1037{
1038 struct wlsc_input_device *device =
1039 container_of(listener, struct wlsc_input_device, listener);
1040
1041 if (device->grab_surface == surface) {
1042 device->grab_surface = NULL;
1043 device->grab = 0;
1044 }
1045 if (device->keyboard_focus == surface)
1046 device->keyboard_focus = NULL;
1047 if (device->pointer_focus == surface)
1048 device->pointer_focus = NULL;
1049}
1050
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001051static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001052create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001053{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001054 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001055
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001056 device = malloc(sizeof *device);
1057 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001058 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001059
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001060 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001061 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001062 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001063 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001064 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001065 device->x = 100;
1066 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001067 device->ec = ec;
1068
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001069 device->listener.func = handle_surface_destroy;
1070 wl_list_insert(ec->surface_destroy_listener_list.prev,
1071 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001072 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001073
1074 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001075}
1076
1077void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001078wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001079{
1080 *x = device->x;
1081 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001082}
1083
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001084static void
1085post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001086{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001087 struct wlsc_output *output =
1088 container_of(global, struct wlsc_output, base);
1089
1090 wl_client_post_event(client, global,
1091 WL_OUTPUT_GEOMETRY,
1092 output->width, output->height);
1093}
1094
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001095static void
1096on_drm_input(int fd, uint32_t mask, void *data)
1097{
1098 drmEventContext evctx;
1099
1100 memset(&evctx, 0, sizeof evctx);
1101 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1102 evctx.page_flip_handler = page_flip_handler;
1103 drmHandleEvent(fd, &evctx);
1104}
1105
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001106static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001107init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001108{
1109 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001110 EGL_DEPTH_SIZE, 0,
1111 EGL_STENCIL_SIZE, 0,
1112 EGL_CONFIG_CAVEAT, EGL_NONE,
1113 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001114 EGL_NONE
1115 };
1116
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001117 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001118 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001119 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001120
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001121 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001122 if (ec->display == NULL) {
1123 fprintf(stderr, "failed to create display\n");
1124 return -1;
1125 }
1126
1127 if (!eglInitialize(ec->display, &major, &minor)) {
1128 fprintf(stderr, "failed to initialize display\n");
1129 return -1;
1130 }
1131
1132 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1133 return -1;
1134
1135 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1136 if (ec->context == NULL) {
1137 fprintf(stderr, "failed to create context\n");
1138 return -1;
1139 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001140
1141 loop = wl_display_get_event_loop(ec->wl_display);
1142 fd = eglGetDisplayFD(ec->display);
1143 ec->drm_source =
1144 wl_event_loop_add_fd(loop, fd,
1145 WL_EVENT_READABLE, on_drm_input, ec);
1146
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001147 return 0;
1148}
1149
1150static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001151create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001152{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001153 drmModeConnector *connector;
1154 drmModeRes *resources;
1155 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001156 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001157 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001158 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001159 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001160
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001161 if (ec->display == NULL && init_egl(ec, device) < 0) {
1162 fprintf(stderr, "failed to initialize egl\n");
1163 return -1;
1164 }
1165
1166 output = malloc(sizeof *output);
1167 if (output == NULL)
1168 return -1;
1169
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001170 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001171 resources = drmModeGetResources(fd);
1172 if (!resources) {
1173 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001174 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001175 }
1176
1177 for (i = 0; i < resources->count_connectors; i++) {
1178 connector = drmModeGetConnector(fd, resources->connectors[i]);
1179 if (connector == NULL)
1180 continue;
1181
1182 if (connector->connection == DRM_MODE_CONNECTED &&
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -04001183 connector->count_modes > 0 &&
1184 (option_connector == 0 ||
1185 connector->connector_id == option_connector))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001186 break;
1187
1188 drmModeFreeConnector(connector);
1189 }
1190
1191 if (i == resources->count_connectors) {
1192 fprintf(stderr, "No currently active connector found.\n");
1193 return -1;
1194 }
1195
1196 mode = &connector->modes[0];
1197
1198 for (i = 0; i < resources->count_encoders; i++) {
1199 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1200
1201 if (encoder == NULL)
1202 continue;
1203
1204 if (encoder->encoder_id == connector->encoder_id)
1205 break;
1206
1207 drmModeFreeEncoder(encoder);
1208 }
1209
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001210 output->compositor = ec;
1211 output->crtc_id = encoder->crtc_id;
1212 output->connector_id = connector->connector_id;
1213 output->mode = mode;
1214 output->x = 0;
1215 output->y = 0;
1216 output->width = mode->hdisplay;
1217 output->height = mode->vdisplay;
1218
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001219 output->surface = eglCreateSurface(ec->display,
1220 ec->config,
1221 output->width,
1222 output->height,
1223 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001224 if (output->surface == NULL) {
1225 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001226 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001227 }
1228
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001229 for (i = 0; i < 2; i++) {
1230 eglGetColorBuffer(output->surface,
1231 i, &name, &handle, &stride);
1232
1233 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1234 32, 32, stride, handle, &output->fb_id[i]);
1235 if (ret) {
1236 fprintf(stderr, "failed to add fb %d: %m\n", i);
1237 return -1;
1238 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001239 }
1240
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001241 output->current = 0;
1242 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1243 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001244 &connector->connector_id, 1, mode);
1245 if (ret) {
1246 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001247 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001248 }
1249
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001250 output->base.interface = &wl_output_interface;
1251 wl_display_add_object(ec->wl_display, &output->base);
1252 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1253 wl_list_insert(ec->output_list.prev, &output->link);
1254
1255 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1256 fprintf(stderr, "failed to make context current\n");
1257 return -1;
1258 }
1259
1260 output->background = background_create(output, option_background);
1261
1262 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001263}
1264
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001265static const struct wl_interface visual_interface = {
1266 "visual", 1,
1267};
1268
1269static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001270add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001271{
1272 ec->argb_visual.base.interface = &visual_interface;
1273 ec->argb_visual.base.implementation = NULL;
1274 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001275 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001276
1277 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1278 ec->premultiplied_argb_visual.base.implementation = NULL;
1279 wl_display_add_object(ec->wl_display,
1280 &ec->premultiplied_argb_visual.base);
1281 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001282 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001283
1284 ec->rgb_visual.base.interface = &visual_interface;
1285 ec->rgb_visual.base.implementation = NULL;
1286 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001287 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1288}
1289
Ray Strode19ad6a92008-12-19 01:45:41 -05001290static void on_enter_vt(int signal_number, void *data)
1291{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001292 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001293 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001294 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001295
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001296 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001297 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001298
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001299 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 output = container_of(ec->output_list.next, struct wlsc_output, link);
1301 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001302 ret = drmModeSetCrtc(fd, output->crtc_id,
1303 output->fb_id[output->current], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001304 &output->connector_id, 1, output->mode);
1305 if (ret)
1306 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1307 output->connector_id);
1308
1309 output = container_of(output->link.next,
1310 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001311 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001312}
1313
1314static void on_leave_vt(int signal_number, void *data)
1315{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001316 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001317
1318 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001319 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001320}
1321
Ray Strode966aa112008-12-19 14:28:02 -05001322static void
1323on_tty_input(int fd, uint32_t mask, void *data)
1324{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001325 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001326
1327 /* Ignore input to tty. We get keyboard events from evdev
1328 */
1329 tcflush(ec->tty_fd, TCIFLUSH);
1330}
1331
1332static void on_term_signal(int signal_number, void *data)
1333{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001334 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001335
1336 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1337 fprintf(stderr, "could not restore terminal to canonical mode\n");
1338
1339 exit(0);
1340}
1341
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001342static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001343{
1344 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001345 struct vt_mode mode = { 0 };
1346
1347 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1348 if (ec->tty_fd <= 0) {
1349 fprintf(stderr, "failed to open active tty: %m\n");
1350 return -1;
1351 }
Ray Strode966aa112008-12-19 14:28:02 -05001352
1353 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1354 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001355 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001356 }
1357
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001358 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001359 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001360 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001361
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001362 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001363 raw_attributes.c_oflag |= OPOST | OCRNL;
1364
1365 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1366 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1367
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001368 ec->term_signal_source =
1369 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001370
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001371 ec->tty_input_source =
1372 wl_event_loop_add_fd(loop, ec->tty_fd,
1373 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001374
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001375 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001376 mode.mode = VT_PROCESS;
1377 mode.relsig = SIGUSR1;
1378 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001379 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001380 fprintf(stderr, "failed to take control of vt handling\n");
1381 }
1382
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001383 ec->leave_vt_source =
1384 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1385 ec->enter_vt_source =
1386 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001387
1388 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001389}
1390
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001391static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001392init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001393{
1394 struct udev_enumerate *e;
1395 struct udev_list_entry *entry;
1396 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001397 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001398 struct wlsc_input_device *input_device;
1399
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001400 ec->udev = udev_new();
1401 if (ec->udev == NULL) {
1402 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001403 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001404 }
1405
1406 input_device = create_input_device(ec);
1407
1408 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001409 udev_enumerate_add_match_subsystem(e, "input");
1410 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001411 udev_enumerate_scan_devices(e);
1412 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1413 path = udev_list_entry_get_name(entry);
1414 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001415 evdev_input_device_create(input_device, ec->wl_display,
1416 udev_device_get_devnode(device));
1417 }
1418 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001419
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001420 e = udev_enumerate_new(ec->udev);
1421 udev_enumerate_add_match_subsystem(e, "drm");
1422 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1423 udev_enumerate_scan_devices(e);
1424 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1425 path = udev_list_entry_get_name(entry);
1426 device = udev_device_new_from_syspath(ec->udev, path);
1427 if (create_output(ec, device) < 0) {
1428 fprintf(stderr, "failed to create output for %s\n", path);
1429 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001430 }
1431 }
1432 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001433
1434 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001435 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001436 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1437
1438 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001439}
1440
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001441static struct wlsc_compositor *
1442wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001443{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001444 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001445 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001446 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001447
1448 ec = malloc(sizeof *ec);
1449 if (ec == NULL)
1450 return NULL;
1451
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001452 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001453 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001454
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001455 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001456
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001457 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001458
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001459 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001460 wl_list_init(&ec->input_device_list);
1461 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001462 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001463 if (init_libudev(ec) < 0) {
1464 fprintf(stderr, "failed to initialize devices\n");
1465 return NULL;
1466 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001467
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001468 shooter = screenshooter_create(ec);
1469 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001470 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001471
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001472 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001473
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001474 setup_tty(ec, loop);
1475
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001476 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001477 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001478
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001479 wl_list_init(&ec->animate_list);
1480
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001481 return ec;
1482}
1483
1484/* The plan here is to generate a random anonymous socket name and
1485 * advertise that through a service on the session dbus.
1486 */
1487static const char socket_name[] = "\0wayland";
1488
1489int main(int argc, char *argv[])
1490{
1491 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001492 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001493 GError *error = NULL;
1494 GOptionContext *context;
1495
1496 context = g_option_context_new(NULL);
1497 g_option_context_add_main_entries(context, option_entries, "Wayland");
1498 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1499 fprintf(stderr, "option parsing failed: %s\n", error->message);
1500 exit(EXIT_FAILURE);
1501 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001502
1503 display = wl_display_create();
1504
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001505 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001506 if (ec == NULL) {
1507 fprintf(stderr, "failed to create compositor\n");
1508 exit(EXIT_FAILURE);
1509 }
1510
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001511 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001512 fprintf(stderr, "failed to add socket: %m\n");
1513 exit(EXIT_FAILURE);
1514 }
1515
1516 wl_display_run(display);
1517
1518 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001519}