blob: a21d3433b1ff138badee87c6fa17a79ef3f52e00 [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";
164
165static const GOptionEntry option_entries[] = {
166 { "background", 'b', 0, G_OPTION_ARG_STRING,
167 &option_background, "Background image" },
168 { NULL }
169};
170
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500171struct screenshooter {
172 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500173 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500174};
175
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500176struct screenshooter_interface {
177 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
178};
179
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500180static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500181screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500182{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500183 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500184 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500185 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500186 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500187 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500188 unsigned char *data;
189 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500190
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500191 i = 0;
192 output = container_of(ec->output_list.next, struct wlsc_output, link);
193 while (&output->link != &ec->output_list) {
194 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500195 data = malloc(output->width * output->height * 4);
196 if (data == NULL) {
197 fprintf(stderr, "couldn't allocate image buffer\n");
198 continue;
199 }
200
201 glReadBuffer(GL_FRONT);
202 glPixelStorei(GL_PACK_ALIGNMENT, 1);
203 glReadPixels(0, 0, output->width, output->height,
204 GL_RGBA, GL_UNSIGNED_BYTE, data);
205
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500206 /* FIXME: We should just use a RGB visual for the frontbuffer. */
207 for (j = 3; j < output->width * output->height * 4; j += 4)
208 data[j] = 0xff;
209
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500210 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500211 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500212 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500213 normal = gdk_pixbuf_flip(pixbuf, FALSE);
214 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
215 gdk_pixbuf_unref(normal);
216 gdk_pixbuf_unref(pixbuf);
217 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500218
219 output = container_of(output->link.next,
220 struct wlsc_output, link);
221 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500222}
223
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500224static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500225 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500226};
227
228static const struct wl_interface screenshooter_interface = {
229 "screenshooter", 1,
230 ARRAY_LENGTH(screenshooter_methods),
231 screenshooter_methods,
232};
233
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500234struct screenshooter_interface screenshooter_implementation = {
235 screenshooter_shoot
236};
237
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500238static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500239screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500240{
241 struct screenshooter *shooter;
242
243 shooter = malloc(sizeof *shooter);
244 if (shooter == NULL)
245 return NULL;
246
247 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500248 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500249 shooter->ec = ec;
250
251 return shooter;
252};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500253
254static void
255wlsc_matrix_init(struct wlsc_matrix *matrix)
256{
257 static const struct wlsc_matrix identity = {
258 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
259 };
260
261 memcpy(matrix, &identity, sizeof identity);
262}
263
264static void
265wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
266{
267 struct wlsc_matrix tmp;
268 const GLdouble *row, *column;
269 div_t d;
270 int i, j;
271
272 for (i = 0; i < 16; i++) {
273 tmp.d[i] = 0;
274 d = div(i, 4);
275 row = m->d + d.quot * 4;
276 column = n->d + d.rem;
277 for (j = 0; j < 4; j++)
278 tmp.d[i] += row[j] * column[j * 4];
279 }
280 memcpy(m, &tmp, sizeof tmp);
281}
282
283static void
284wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
285{
286 struct wlsc_matrix translate = {
287 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
288 };
289
290 wlsc_matrix_multiply(matrix, &translate);
291}
292
293static void
294wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
295{
296 struct wlsc_matrix scale = {
297 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
298 };
299
300 wlsc_matrix_multiply(matrix, &scale);
301}
302
303static void
304wlsc_matrix_rotate(struct wlsc_matrix *matrix,
305 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
306{
307 GLdouble c = cos(angle);
308 GLdouble s = sin(angle);
309 struct wlsc_matrix rotate = {
310 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
311 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
312 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
313 0, 0, 0, 1 }
314 };
315
316 wlsc_matrix_multiply(matrix, &rotate);
317}
318
319static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500320wlsc_surface_init(struct wlsc_surface *surface,
321 struct wlsc_compositor *compositor, struct wl_visual *visual,
322 int32_t x, int32_t y, int32_t width, int32_t height)
323{
324 glGenTextures(1, &surface->texture);
325 surface->compositor = compositor;
326 surface->map.x = x;
327 surface->map.y = y;
328 surface->map.width = width;
329 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500330 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400331 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500332}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500333
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500334static struct wlsc_surface *
335wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500336 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500337 int x, int y, int width, int height)
338{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500339 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500340 int stride;
341 void *data;
342
343 stride = cairo_image_surface_get_stride(surface);
344 data = cairo_image_surface_get_data(surface);
345
346 es = malloc(sizeof *es);
347 if (es == NULL)
348 return NULL;
349
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500350 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
351 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500352 glBindTexture(GL_TEXTURE_2D, es->texture);
353 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500354 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
355 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500357 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
358 GL_BGRA, GL_UNSIGNED_BYTE, data);
359
Kristian Høgsberg54879822008-11-23 17:07:32 -0500360 return es;
361}
362
363static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400364wlsc_surface_destroy(struct wlsc_surface *surface,
365 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500366{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400367 struct wlsc_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400368
369 l = container_of(compositor->surface_destroy_listener_list.next,
370 struct wlsc_listener, link);
371 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400372 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400373 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400374 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400375 }
376
377 wl_list_remove(&surface->link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400378
379 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400380 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500381}
382
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400383static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500384pointer_path(cairo_t *cr, int x, int y)
385{
386 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
387 const int width = 16, height = 16;
388
389 cairo_move_to(cr, x, y);
390 cairo_line_to(cr, x + tx, y + ty);
391 cairo_line_to(cr, x + dx, y + dy);
392 cairo_line_to(cr, x + width - end, y + height);
393 cairo_line_to(cr, x + width, y + height - end);
394 cairo_line_to(cr, x + dy, y + dx);
395 cairo_line_to(cr, x + ty, y + tx);
396 cairo_close_path(cr);
397}
398
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500399static struct wlsc_surface *
400pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500401{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500402 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500403 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500404 cairo_surface_t *surface;
405 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500406
407 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
408 width, height);
409
410 cr = cairo_create(surface);
411 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500412 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500413 cairo_set_source_rgb(cr, 0, 0, 0);
414 cairo_stroke_preserve(cr);
415 cairo_fill(cr);
416 blur_surface(surface, width);
417
418 pointer_path(cr, hotspot_x, hotspot_y);
419 cairo_stroke_preserve(cr);
420 cairo_set_source_rgb(cr, 1, 1, 1);
421 cairo_fill(cr);
422 cairo_destroy(cr);
423
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500424 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500425 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500426 x - hotspot_x,
427 y - hotspot_y,
428 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500429
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500430 cairo_surface_destroy(surface);
431
Kristian Høgsberg54879822008-11-23 17:07:32 -0500432 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500433}
434
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500435static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500436background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500437{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500438 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500439 GdkPixbuf *pixbuf;
440 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500441 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500442 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500443
444 background = malloc(sizeof *background);
445 if (background == NULL)
446 return NULL;
447
448 g_type_init();
449
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500450 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500451 output->width,
452 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500453 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500454 if (error != NULL) {
455 free(background);
456 return NULL;
457 }
458
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500459 data = gdk_pixbuf_get_pixels(pixbuf);
460
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500461 wlsc_surface_init(background, output->compositor,
462 &output->compositor->rgb_visual,
463 output->x, output->y, output->width, output->height);
464
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500465 glBindTexture(GL_TEXTURE_2D, background->texture);
466 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500470
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500471 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500472 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500473 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500474 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500475
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500476 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
477 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500478 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500479
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500480 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500481}
482
483static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500484wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500485{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500486 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500487 GLint vertices[12];
488 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
489 GLuint indices[4] = { 0, 1, 2, 3 };
490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 vertices[0] = es->map.x;
492 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500493 vertices[2] = 0;
494
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500495 vertices[3] = es->map.x;
496 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500497 vertices[5] = 0;
498
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500499 vertices[6] = es->map.x + es->map.width;
500 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500501 vertices[8] = 0;
502
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500503 vertices[9] = es->map.x + es->map.width;
504 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505 vertices[11] = 0;
506
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500507 if (es->visual == &ec->argb_visual) {
508 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
509 glEnable(GL_BLEND);
510 } else if (es->visual == &ec->premultiplied_argb_visual) {
511 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
512 glEnable(GL_BLEND);
513 } else {
514 glDisable(GL_BLEND);
515 }
516
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500517 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400518 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500519 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500520 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500521 glEnableClientState(GL_VERTEX_ARRAY);
522 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
523 glVertexPointer(3, GL_INT, 0, vertices);
524 glTexCoordPointer(2, GL_INT, 0, tex_coords);
525 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500526 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500527}
528
529static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400530wlsc_surface_raise(struct wlsc_surface *surface)
531{
532 struct wlsc_compositor *compositor = surface->compositor;
533
534 wl_list_remove(&surface->link);
535 wl_list_insert(compositor->surface_list.prev, &surface->link);
536}
537
538static void
539wlsc_surface_lower(struct wlsc_surface *surface)
540{
541 struct wlsc_compositor *compositor = surface->compositor;
542
543 wl_list_remove(&surface->link);
544 wl_list_insert(&compositor->surface_list, &surface->link);
545}
546
547static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500548wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
549{
550 v1->x += v2->x;
551 v1->y += v2->y;
552 v1->z += v2->z;
553}
554
555static void
556wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
557{
558 v1->x -= v2->x;
559 v1->y -= v2->y;
560 v1->z -= v2->z;
561}
562
563static void
564wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
565{
566 v1->x *= s;
567 v1->y *= s;
568 v1->z *= s;
569}
570
571static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400572page_flip_handler(int fd, unsigned int frame,
573 unsigned int sec, unsigned int usec, void *data)
574{
575 struct wlsc_output *output = data;
576 struct wlsc_animate *animate, *next;
577 struct wlsc_compositor *compositor = output->compositor;
578 uint32_t msecs;
579
580 msecs = sec * 1000 + usec / 1000;
581 wl_display_post_frame(compositor->wl_display,
582 &compositor->base,
583 compositor->current_frame, msecs);
584
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400585 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400586 compositor->repaint_on_timeout = 1;
587
588 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
589 while (&animate->link != &compositor->animate_list) {
590 next = container_of(animate->link.next,
591 struct wlsc_animate, link);
592 animate->animate(animate, compositor, compositor->current_frame, msecs);
593 animate = next;
594 }
595
596 compositor->current_frame++;
597}
598
599static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500600repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400601{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500602 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500603 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500604 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500605 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400606 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500607
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500608 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
609 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500610 return;
611 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500612
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500613 glViewport(0, 0, output->width, output->height);
614 glMatrixMode(GL_PROJECTION);
615 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500616 glFrustum(-output->width / s, output->width / s,
617 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500618 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500619 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400620 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500621
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500622 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500623
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500624 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500625 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500626 else
627 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400628
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500629 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500630 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500631 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500632 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500633 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400634 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400635 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400636
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500637 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500638 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500639 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500640 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500641
642 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500643 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500644 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500645
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400646 fd = eglGetDisplayFD(ec->display);
647 output->current ^= 1;
648 eglBindColorBuffer(ec->display, output->surface, output->current);
649 drmModePageFlip(fd, output->crtc_id, output->fb_id[output->current ^ 1], output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500650}
651
652static void
653repaint(void *data)
654{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500655 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500656 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500657
658 if (!ec->repaint_needed) {
659 ec->repaint_on_timeout = 0;
660 return;
661 }
662
663 output = container_of(ec->output_list.next, struct wlsc_output, link);
664 while (&output->link != &ec->output_list) {
665 repaint_output(output);
666 output = container_of(output->link.next,
667 struct wlsc_output, link);
668 }
669
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500670 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400671}
672
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500673static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400674wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400675{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400676 struct wlsc_output *output;
677 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400678
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400679 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400680 if (compositor->repaint_on_timeout)
681 return;
682
683 fd = eglGetDisplayFD(compositor->display);
684 output = container_of(compositor->output_list.next,
685 struct wlsc_output, link);
686 while (&output->link != &compositor->output_list) {
687 drmModePageFlip(fd, output->crtc_id,
688 output->fb_id[output->current ^ 1], output);
689 output = container_of(output->link.next,
690 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500691 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400692}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500693
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500694static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500695surface_destroy(struct wl_client *client,
696 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400697{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500698 struct wlsc_surface *es = (struct wlsc_surface *) surface;
699 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500700
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500701 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400702
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400703 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400704}
705
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500706static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500707surface_attach(struct wl_client *client,
708 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500709 uint32_t width, uint32_t height, uint32_t stride,
710 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400711{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500712 struct wlsc_surface *es = (struct wlsc_surface *) surface;
713 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400714
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500715 es->width = width;
716 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400717
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500718 if (visual == &ec->argb_visual.base)
719 es->visual = &ec->argb_visual;
720 else if (visual == &ec->premultiplied_argb_visual.base)
721 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500722 else if (visual == &ec->rgb_visual.base)
723 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500724 else
725 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400726
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500727 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400728 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500729 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
730 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
731 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400732 glTextureExternalMESA(GL_TEXTURE_2D, GL_RGBA, 4,
733 width, height, stride / 4, name);
734
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400735}
736
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500737static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500738surface_map(struct wl_client *client,
739 struct wl_surface *surface,
740 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400741{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500742 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400743
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500744 es->map.x = x;
745 es->map.y = y;
746 es->map.width = width;
747 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400748}
749
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500751surface_copy(struct wl_client *client,
752 struct wl_surface *surface,
753 int32_t dst_x, int32_t dst_y,
754 uint32_t name, uint32_t stride,
755 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500756{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500757 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400758 GLuint fbo[2], rb;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500759
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400760 glGenFramebuffers(2, fbo);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500761
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400762 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo[1]);
763 glGenRenderbuffers(1, &rb);
764 glBindRenderbuffer(GL_RENDERBUFFER_EXT, rb);
765 glRenderbufferExternalMESA(GL_RENDERBUFFER_EXT,
766 GL_RGBA,
767 es->width, es->height,
768 stride / 4, name);
769 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER_EXT,
770 GL_COLOR_ATTACHMENT0_EXT,
771 GL_RENDERBUFFER_EXT,
772 rb);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400773
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400774 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo[0]);
775 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER_EXT,
776 GL_COLOR_ATTACHMENT0_EXT,
777 GL_TEXTURE_2D, es->texture, 0);
778
779 glBlitFramebuffer(x, y, x + width, y + height,
780 dst_x, dst_y, dst_x+ width, dst_y + height,
781 GL_COLOR_BUFFER_BIT, GL_NEAREST);
782
783 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
784 glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
785 glDeleteRenderbuffers(1, &rb);
786 glDeleteFramebuffers(2, fbo);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500787}
788
789static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500790surface_damage(struct wl_client *client,
791 struct wl_surface *surface,
792 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500793{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500794 /* FIXME: This need to take a damage region, of course. */
795}
796
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500797const static struct wl_surface_interface surface_interface = {
798 surface_destroy,
799 surface_attach,
800 surface_map,
801 surface_copy,
802 surface_damage
803};
804
805static void
806compositor_create_surface(struct wl_client *client,
807 struct wl_compositor *compositor, uint32_t id)
808{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500809 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400810 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500811
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400812 surface = malloc(sizeof *surface);
813 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500814 /* FIXME: Send OOM event. */
815 return;
816
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400817 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500818
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400819 wl_list_insert(ec->surface_list.prev, &surface->link);
820 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500821 &surface_interface, id);
822}
823
824static void
825compositor_commit(struct wl_client *client,
826 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500827{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500828 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500829
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400830 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500831 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500832}
833
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500834const static struct wl_compositor_interface compositor_interface = {
835 compositor_create_surface,
836 compositor_commit
837};
838
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500839static void
840wlsc_surface_transform(struct wlsc_surface *surface,
841 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
842{
843 /* Transform to surface coordinates. */
844 *sx = (x - surface->map.x) * surface->width / surface->map.width;
845 *sy = (y - surface->map.y) * surface->height / surface->map.height;
846}
847
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500848static void
849wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
850 struct wlsc_surface *surface)
851{
852 if (device->keyboard_focus == surface)
853 return;
854
855 if (device->keyboard_focus &&
856 (!surface || device->keyboard_focus->base.client != surface->base.client))
857 wl_surface_post_event(&device->keyboard_focus->base,
858 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500859 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500860
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500861 if (surface)
862 wl_surface_post_event(&surface->base,
863 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500864 WL_INPUT_KEYBOARD_FOCUS,
865 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500866
867 device->keyboard_focus = surface;
868}
869
870static void
871wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
872 struct wlsc_surface *surface)
873{
874 if (device->pointer_focus == surface)
875 return;
876
877 if (device->pointer_focus &&
878 (!surface || device->pointer_focus->base.client != surface->base.client))
879 wl_surface_post_event(&device->pointer_focus->base,
880 &device->base,
881 WL_INPUT_POINTER_FOCUS, NULL);
882 if (surface)
883 wl_surface_post_event(&surface->base,
884 &device->base,
885 WL_INPUT_POINTER_FOCUS, &surface->base);
886
887 device->pointer_focus = surface;
888}
889
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500890static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500891pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500892{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500893 struct wlsc_compositor *ec = device->ec;
894 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500895
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500896 if (device->grab > 0) {
897 wlsc_surface_transform(device->grab_surface,
898 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500899 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500900 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500901
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500902 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500903 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500904 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500905 if (es->map.x <= device->x &&
906 device->x < es->map.x + es->map.width &&
907 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500908 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500909 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500910 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500911 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500912
913 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500914 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500915
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500916 }
917
918 return NULL;
919}
920
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500921void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500922notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500923{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500924 struct wlsc_surface *es;
925 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500926 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500927 const int hotspot_x = 16, hotspot_y = 16;
928 int32_t sx, sy;
929
Ray Strodee96dcb82008-12-20 02:00:49 -0500930 if (!ec->vt_active)
931 return;
932
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500933 /* FIXME: We need some multi head love here. */
934 output = container_of(ec->output_list.next, struct wlsc_output, link);
935 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500936 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500937 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500938 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500939 if (x >= output->x + output->width)
940 x = output->x + output->width - 1;
941 if (y >= output->y + output->height)
942 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500943
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500944 device->x = x;
945 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500946 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500947
948 wlsc_input_device_set_pointer_focus(device, es);
949
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500950 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500951 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500952 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500953
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500954 device->sprite->map.x = x - hotspot_x;
955 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500956
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400957 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500958}
959
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500960void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500961notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500962 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500963{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400964 struct wlsc_surface *surface;
965 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500966 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500967
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400968 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500969 return;
970
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400971 surface = pick_surface(device, &sx, &sy);
972 if (surface) {
973 wlsc_surface_raise(surface);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500974
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500975 if (state) {
976 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400977 device->grab_surface = surface;
978 wlsc_input_device_set_keyboard_focus(device,
979 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500980 } else {
981 device->grab--;
982 }
983
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500984 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400985 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500986 WL_INPUT_BUTTON, button, state,
987 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500988
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400989 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500990 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500991}
992
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500993static void on_term_signal(int signal_number, void *data);
994
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500995void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500996notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500997 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500998{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400999 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001000 uint32_t *k, *end;
1001
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001002 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001003 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001004
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001005 switch (key | compositor->meta_state) {
Kristian Høgsberg45b7a3a2009-08-14 05:53:50 -04001006 case KEY_BACKSPACE | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001007 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001008 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001009 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001010
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001011 end = device->keys.data + device->keys.size;
1012 for (k = device->keys.data; k < end; k++) {
1013 if (*k == key)
1014 *k = *--end;
1015 }
1016 device->keys.size = (void *) end - device->keys.data;
1017 if (state) {
1018 k = wl_array_add(&device->keys, sizeof *k);
1019 *k = key;
1020 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001021
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001022 if (device->keyboard_focus != NULL)
1023 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001024 &device->base,
1025 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001026}
1027
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001028struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001029evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001030 struct wl_display *display, const char *path);
1031
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001032static void
1033handle_surface_destroy(struct wlsc_listener *listener,
1034 struct wlsc_surface *surface)
1035{
1036 struct wlsc_input_device *device =
1037 container_of(listener, struct wlsc_input_device, listener);
1038
1039 if (device->grab_surface == surface) {
1040 device->grab_surface = NULL;
1041 device->grab = 0;
1042 }
1043 if (device->keyboard_focus == surface)
1044 device->keyboard_focus = NULL;
1045 if (device->pointer_focus == surface)
1046 device->pointer_focus = NULL;
1047}
1048
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001049static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001050create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001051{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001052 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001053
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001054 device = malloc(sizeof *device);
1055 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001056 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001057
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001058 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001059 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001060 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001061 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001062 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001063 device->x = 100;
1064 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001065 device->ec = ec;
1066
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001067 device->listener.func = handle_surface_destroy;
1068 wl_list_insert(ec->surface_destroy_listener_list.prev,
1069 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001070 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001071
1072 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001073}
1074
1075void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001076wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001077{
1078 *x = device->x;
1079 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001080}
1081
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001082static void
1083post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001084{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001085 struct wlsc_output *output =
1086 container_of(global, struct wlsc_output, base);
1087
1088 wl_client_post_event(client, global,
1089 WL_OUTPUT_GEOMETRY,
1090 output->width, output->height);
1091}
1092
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001093static void
1094on_drm_input(int fd, uint32_t mask, void *data)
1095{
1096 drmEventContext evctx;
1097
1098 memset(&evctx, 0, sizeof evctx);
1099 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1100 evctx.page_flip_handler = page_flip_handler;
1101 drmHandleEvent(fd, &evctx);
1102}
1103
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001104static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001105init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001106{
1107 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001108 EGL_DEPTH_SIZE, 0,
1109 EGL_STENCIL_SIZE, 0,
1110 EGL_CONFIG_CAVEAT, EGL_NONE,
1111 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001112 EGL_NONE
1113 };
1114
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001115 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001116 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001117 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001118
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001119 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001120 if (ec->display == NULL) {
1121 fprintf(stderr, "failed to create display\n");
1122 return -1;
1123 }
1124
1125 if (!eglInitialize(ec->display, &major, &minor)) {
1126 fprintf(stderr, "failed to initialize display\n");
1127 return -1;
1128 }
1129
1130 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1131 return -1;
1132
1133 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1134 if (ec->context == NULL) {
1135 fprintf(stderr, "failed to create context\n");
1136 return -1;
1137 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001138
1139 loop = wl_display_get_event_loop(ec->wl_display);
1140 fd = eglGetDisplayFD(ec->display);
1141 ec->drm_source =
1142 wl_event_loop_add_fd(loop, fd,
1143 WL_EVENT_READABLE, on_drm_input, ec);
1144
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001145 return 0;
1146}
1147
1148static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001149create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001150{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001151 drmModeConnector *connector;
1152 drmModeRes *resources;
1153 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001154 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001155 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001156 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001157 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001158
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001159 if (ec->display == NULL && init_egl(ec, device) < 0) {
1160 fprintf(stderr, "failed to initialize egl\n");
1161 return -1;
1162 }
1163
1164 output = malloc(sizeof *output);
1165 if (output == NULL)
1166 return -1;
1167
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001168 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001169 resources = drmModeGetResources(fd);
1170 if (!resources) {
1171 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001172 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001173 }
1174
1175 for (i = 0; i < resources->count_connectors; i++) {
1176 connector = drmModeGetConnector(fd, resources->connectors[i]);
1177 if (connector == NULL)
1178 continue;
1179
1180 if (connector->connection == DRM_MODE_CONNECTED &&
1181 connector->count_modes > 0)
1182 break;
1183
1184 drmModeFreeConnector(connector);
1185 }
1186
1187 if (i == resources->count_connectors) {
1188 fprintf(stderr, "No currently active connector found.\n");
1189 return -1;
1190 }
1191
1192 mode = &connector->modes[0];
1193
1194 for (i = 0; i < resources->count_encoders; i++) {
1195 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1196
1197 if (encoder == NULL)
1198 continue;
1199
1200 if (encoder->encoder_id == connector->encoder_id)
1201 break;
1202
1203 drmModeFreeEncoder(encoder);
1204 }
1205
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001206 output->compositor = ec;
1207 output->crtc_id = encoder->crtc_id;
1208 output->connector_id = connector->connector_id;
1209 output->mode = mode;
1210 output->x = 0;
1211 output->y = 0;
1212 output->width = mode->hdisplay;
1213 output->height = mode->vdisplay;
1214
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001215 output->surface = eglCreateSurface(ec->display,
1216 ec->config,
1217 output->width,
1218 output->height,
1219 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001220 if (output->surface == NULL) {
1221 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001222 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001223 }
1224
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001225 for (i = 0; i < 2; i++) {
1226 eglGetColorBuffer(output->surface,
1227 i, &name, &handle, &stride);
1228
1229 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1230 32, 32, stride, handle, &output->fb_id[i]);
1231 if (ret) {
1232 fprintf(stderr, "failed to add fb %d: %m\n", i);
1233 return -1;
1234 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001235 }
1236
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001237 output->current = 0;
1238 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1239 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001240 &connector->connector_id, 1, mode);
1241 if (ret) {
1242 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001243 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001244 }
1245
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001246 output->base.interface = &wl_output_interface;
1247 wl_display_add_object(ec->wl_display, &output->base);
1248 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1249 wl_list_insert(ec->output_list.prev, &output->link);
1250
1251 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1252 fprintf(stderr, "failed to make context current\n");
1253 return -1;
1254 }
1255
1256 output->background = background_create(output, option_background);
1257
1258 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001259}
1260
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001261static const struct wl_interface visual_interface = {
1262 "visual", 1,
1263};
1264
1265static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001266add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001267{
1268 ec->argb_visual.base.interface = &visual_interface;
1269 ec->argb_visual.base.implementation = NULL;
1270 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001271 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001272
1273 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1274 ec->premultiplied_argb_visual.base.implementation = NULL;
1275 wl_display_add_object(ec->wl_display,
1276 &ec->premultiplied_argb_visual.base);
1277 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001278 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001279
1280 ec->rgb_visual.base.interface = &visual_interface;
1281 ec->rgb_visual.base.implementation = NULL;
1282 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001283 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1284}
1285
Ray Strode19ad6a92008-12-19 01:45:41 -05001286static void on_enter_vt(int signal_number, void *data)
1287{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001288 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001289 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001290 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001291
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001292 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001293 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001294
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001295 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001296 output = container_of(ec->output_list.next, struct wlsc_output, link);
1297 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001298 ret = drmModeSetCrtc(fd, output->crtc_id,
1299 output->fb_id[output->current], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 &output->connector_id, 1, output->mode);
1301 if (ret)
1302 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1303 output->connector_id);
1304
1305 output = container_of(output->link.next,
1306 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001307 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001308}
1309
1310static void on_leave_vt(int signal_number, void *data)
1311{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001312 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001313
1314 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001315 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001316}
1317
Ray Strode966aa112008-12-19 14:28:02 -05001318static void
1319on_tty_input(int fd, uint32_t mask, void *data)
1320{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001321 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001322
1323 /* Ignore input to tty. We get keyboard events from evdev
1324 */
1325 tcflush(ec->tty_fd, TCIFLUSH);
1326}
1327
1328static void on_term_signal(int signal_number, void *data)
1329{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001330 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001331
1332 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1333 fprintf(stderr, "could not restore terminal to canonical mode\n");
1334
1335 exit(0);
1336}
1337
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001338static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001339{
1340 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001341 struct vt_mode mode = { 0 };
1342
1343 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1344 if (ec->tty_fd <= 0) {
1345 fprintf(stderr, "failed to open active tty: %m\n");
1346 return -1;
1347 }
Ray Strode966aa112008-12-19 14:28:02 -05001348
1349 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1350 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001351 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001352 }
1353
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001354 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001355 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001356 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001357
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001358 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001359 raw_attributes.c_oflag |= OPOST | OCRNL;
1360
1361 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1362 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1363
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001364 ec->term_signal_source =
1365 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001366
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001367 ec->tty_input_source =
1368 wl_event_loop_add_fd(loop, ec->tty_fd,
1369 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001370
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001371 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001372 mode.mode = VT_PROCESS;
1373 mode.relsig = SIGUSR1;
1374 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001375 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001376 fprintf(stderr, "failed to take control of vt handling\n");
1377 }
1378
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001379 ec->leave_vt_source =
1380 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1381 ec->enter_vt_source =
1382 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001383
1384 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001385}
1386
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001387static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001388init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001389{
1390 struct udev_enumerate *e;
1391 struct udev_list_entry *entry;
1392 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001393 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001394 struct wlsc_input_device *input_device;
1395
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001396 ec->udev = udev_new();
1397 if (ec->udev == NULL) {
1398 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001399 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001400 }
1401
1402 input_device = create_input_device(ec);
1403
1404 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001405 udev_enumerate_add_match_subsystem(e, "input");
1406 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001407 udev_enumerate_scan_devices(e);
1408 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1409 path = udev_list_entry_get_name(entry);
1410 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001411 evdev_input_device_create(input_device, ec->wl_display,
1412 udev_device_get_devnode(device));
1413 }
1414 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001415
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001416 e = udev_enumerate_new(ec->udev);
1417 udev_enumerate_add_match_subsystem(e, "drm");
1418 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1419 udev_enumerate_scan_devices(e);
1420 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1421 path = udev_list_entry_get_name(entry);
1422 device = udev_device_new_from_syspath(ec->udev, path);
1423 if (create_output(ec, device) < 0) {
1424 fprintf(stderr, "failed to create output for %s\n", path);
1425 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001426 }
1427 }
1428 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001429
1430 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001431 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001432 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1433
1434 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001435}
1436
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001437static struct wlsc_compositor *
1438wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001439{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001440 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001441 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001442 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001443
1444 ec = malloc(sizeof *ec);
1445 if (ec == NULL)
1446 return NULL;
1447
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001448 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001449 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001450
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001451 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001452
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001453 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001454
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001455 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001456 wl_list_init(&ec->input_device_list);
1457 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001458 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001459 if (init_libudev(ec) < 0) {
1460 fprintf(stderr, "failed to initialize devices\n");
1461 return NULL;
1462 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001463
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001464 shooter = screenshooter_create(ec);
1465 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001466 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001467
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001468 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001469
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001470 setup_tty(ec, loop);
1471
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001472 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001473 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001474
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001475 wl_list_init(&ec->animate_list);
1476
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001477 return ec;
1478}
1479
1480/* The plan here is to generate a random anonymous socket name and
1481 * advertise that through a service on the session dbus.
1482 */
1483static const char socket_name[] = "\0wayland";
1484
1485int main(int argc, char *argv[])
1486{
1487 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001488 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001489 GError *error = NULL;
1490 GOptionContext *context;
1491
1492 context = g_option_context_new(NULL);
1493 g_option_context_add_main_entries(context, option_entries, "Wayland");
1494 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1495 fprintf(stderr, "option parsing failed: %s\n", error->message);
1496 exit(EXIT_FAILURE);
1497 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001498
1499 display = wl_display_create();
1500
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001501 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001502 if (ec == NULL) {
1503 fprintf(stderr, "failed to create compositor\n");
1504 exit(EXIT_FAILURE);
1505 }
1506
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001507 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001508 fprintf(stderr, "failed to add socket: %m\n");
1509 exit(EXIT_FAILURE);
1510 }
1511
1512 wl_display_run(display);
1513
1514 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001515}