blob: 13eca24476211135ea3110a8af16a657b3fb7c6e [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øgsbergb22382b2009-03-10 23:40:35 -040072 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050073
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -040074 drmModeModeInfo mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050075 uint32_t crtc_id;
76 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040077
Kristian Høgsberga341fa02010-01-24 18:10:15 -050078 GLuint rbo[2];
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040079 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øgsberga341fa02010-01-24 18:10:15 -0500105 GLuint fbo;
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
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400135 uint32_t modifier_state;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400136};
137
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400138#define MODIFIER_CTRL (1 << 8)
139#define MODIFIER_ALT (1 << 9)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500140
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400141struct wlsc_vector {
142 GLdouble x, y, z;
143};
144
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500145struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500146 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500147 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500148 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400149 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400150 struct wl_map map;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500151 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500152 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500153 struct wlsc_matrix matrix;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400154};
155
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500156static const char *option_background = "background.jpg";
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400157static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500158
159static const GOptionEntry option_entries[] = {
160 { "background", 'b', 0, G_OPTION_ARG_STRING,
161 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400162 { "connector", 'c', 0, G_OPTION_ARG_INT,
163 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500164 { NULL }
165};
166
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500167struct screenshooter {
168 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500169 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500170};
171
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500172struct screenshooter_interface {
173 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
174};
175
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500176static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500177screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500178{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500179 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500180 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500181 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500182 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500183 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500184 unsigned char *data;
185 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500186
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500187 i = 0;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500188 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500189 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500190 data = malloc(output->width * output->height * 4);
191 if (data == NULL) {
192 fprintf(stderr, "couldn't allocate image buffer\n");
193 continue;
194 }
195
196 glReadBuffer(GL_FRONT);
197 glPixelStorei(GL_PACK_ALIGNMENT, 1);
198 glReadPixels(0, 0, output->width, output->height,
199 GL_RGBA, GL_UNSIGNED_BYTE, data);
200
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500201 /* FIXME: We should just use a RGB visual for the frontbuffer. */
202 for (j = 3; j < output->width * output->height * 4; j += 4)
203 data[j] = 0xff;
204
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500205 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500206 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500207 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500208 normal = gdk_pixbuf_flip(pixbuf, FALSE);
209 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
210 gdk_pixbuf_unref(normal);
211 gdk_pixbuf_unref(pixbuf);
212 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500213 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500214}
215
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500216static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500217 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500218};
219
220static const struct wl_interface screenshooter_interface = {
221 "screenshooter", 1,
222 ARRAY_LENGTH(screenshooter_methods),
223 screenshooter_methods,
224};
225
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500226struct screenshooter_interface screenshooter_implementation = {
227 screenshooter_shoot
228};
229
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500230static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500231screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500232{
233 struct screenshooter *shooter;
234
235 shooter = malloc(sizeof *shooter);
236 if (shooter == NULL)
237 return NULL;
238
239 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500240 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241 shooter->ec = ec;
242
243 return shooter;
244};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500245
246static void
247wlsc_matrix_init(struct wlsc_matrix *matrix)
248{
249 static const struct wlsc_matrix identity = {
250 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
251 };
252
253 memcpy(matrix, &identity, sizeof identity);
254}
255
256static void
257wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
258{
259 struct wlsc_matrix tmp;
260 const GLdouble *row, *column;
261 div_t d;
262 int i, j;
263
264 for (i = 0; i < 16; i++) {
265 tmp.d[i] = 0;
266 d = div(i, 4);
267 row = m->d + d.quot * 4;
268 column = n->d + d.rem;
269 for (j = 0; j < 4; j++)
270 tmp.d[i] += row[j] * column[j * 4];
271 }
272 memcpy(m, &tmp, sizeof tmp);
273}
274
275static void
276wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
277{
278 struct wlsc_matrix translate = {
279 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
280 };
281
282 wlsc_matrix_multiply(matrix, &translate);
283}
284
285static void
286wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
287{
288 struct wlsc_matrix scale = {
289 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
290 };
291
292 wlsc_matrix_multiply(matrix, &scale);
293}
294
295static void
296wlsc_matrix_rotate(struct wlsc_matrix *matrix,
297 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
298{
299 GLdouble c = cos(angle);
300 GLdouble s = sin(angle);
301 struct wlsc_matrix rotate = {
302 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
303 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
304 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
305 0, 0, 0, 1 }
306 };
307
308 wlsc_matrix_multiply(matrix, &rotate);
309}
310
311static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500312wlsc_surface_init(struct wlsc_surface *surface,
313 struct wlsc_compositor *compositor, struct wl_visual *visual,
314 int32_t x, int32_t y, int32_t width, int32_t height)
315{
316 glGenTextures(1, &surface->texture);
317 surface->compositor = compositor;
318 surface->map.x = x;
319 surface->map.y = y;
320 surface->map.width = width;
321 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500322 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400323 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500324}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500325
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500326static struct wlsc_surface *
327wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500328 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500329 int x, int y, int width, int height)
330{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500331 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500332 int stride;
333 void *data;
334
335 stride = cairo_image_surface_get_stride(surface);
336 data = cairo_image_surface_get_data(surface);
337
338 es = malloc(sizeof *es);
339 if (es == NULL)
340 return NULL;
341
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500342 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
343 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500344 glBindTexture(GL_TEXTURE_2D, es->texture);
345 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500349 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
350 GL_BGRA, GL_UNSIGNED_BYTE, data);
351
Kristian Høgsberg54879822008-11-23 17:07:32 -0500352 return es;
353}
354
355static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400356wlsc_surface_destroy(struct wlsc_surface *surface,
357 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500358{
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500359 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400360
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400361 wl_list_remove(&surface->link);
362 glDeleteTextures(1, &surface->texture);
363 wl_client_remove_surface(surface->base.client, &surface->base);
364
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500365 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400366 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400367
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400368 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500369}
370
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400371static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500372pointer_path(cairo_t *cr, int x, int y)
373{
374 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
375 const int width = 16, height = 16;
376
377 cairo_move_to(cr, x, y);
378 cairo_line_to(cr, x + tx, y + ty);
379 cairo_line_to(cr, x + dx, y + dy);
380 cairo_line_to(cr, x + width - end, y + height);
381 cairo_line_to(cr, x + width, y + height - end);
382 cairo_line_to(cr, x + dy, y + dx);
383 cairo_line_to(cr, x + ty, y + tx);
384 cairo_close_path(cr);
385}
386
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500387static struct wlsc_surface *
388pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500389{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500390 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500391 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500392 cairo_surface_t *surface;
393 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500394
395 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
396 width, height);
397
398 cr = cairo_create(surface);
399 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500400 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500401 cairo_set_source_rgb(cr, 0, 0, 0);
402 cairo_stroke_preserve(cr);
403 cairo_fill(cr);
404 blur_surface(surface, width);
405
406 pointer_path(cr, hotspot_x, hotspot_y);
407 cairo_stroke_preserve(cr);
408 cairo_set_source_rgb(cr, 1, 1, 1);
409 cairo_fill(cr);
410 cairo_destroy(cr);
411
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500412 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500413 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500414 x - hotspot_x,
415 y - hotspot_y,
416 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500417
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418 cairo_surface_destroy(surface);
419
Kristian Høgsberg54879822008-11-23 17:07:32 -0500420 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500421}
422
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500423static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500424background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500425{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500426 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500427 GdkPixbuf *pixbuf;
428 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500429 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500430 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500431
432 background = malloc(sizeof *background);
433 if (background == NULL)
434 return NULL;
435
436 g_type_init();
437
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500438 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500439 output->width,
440 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500441 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500442 if (error != NULL) {
443 free(background);
444 return NULL;
445 }
446
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500447 data = gdk_pixbuf_get_pixels(pixbuf);
448
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500449 wlsc_surface_init(background, output->compositor,
450 &output->compositor->rgb_visual,
451 output->x, output->y, output->width, output->height);
452
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 glBindTexture(GL_TEXTURE_2D, background->texture);
454 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500455 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500458
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500459 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500460 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500461 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500462 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500463
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500464 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
465 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500466 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500467
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500468 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500469}
470
471static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500472wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500473{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500474 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500475 GLint vertices[12];
476 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
477 GLuint indices[4] = { 0, 1, 2, 3 };
478
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500479 vertices[0] = es->map.x;
480 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500481 vertices[2] = 0;
482
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 vertices[3] = es->map.x;
484 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500485 vertices[5] = 0;
486
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500487 vertices[6] = es->map.x + es->map.width;
488 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500489 vertices[8] = 0;
490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 vertices[9] = es->map.x + es->map.width;
492 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500493 vertices[11] = 0;
494
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500495 if (es->visual == &ec->argb_visual) {
496 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
497 glEnable(GL_BLEND);
498 } else if (es->visual == &ec->premultiplied_argb_visual) {
499 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
500 glEnable(GL_BLEND);
501 } else {
502 glDisable(GL_BLEND);
503 }
504
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500505 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400506 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500507 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500509 glEnableClientState(GL_VERTEX_ARRAY);
510 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
511 glVertexPointer(3, GL_INT, 0, vertices);
512 glTexCoordPointer(2, GL_INT, 0, tex_coords);
513 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500514 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500515}
516
517static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400518wlsc_surface_raise(struct wlsc_surface *surface)
519{
520 struct wlsc_compositor *compositor = surface->compositor;
521
522 wl_list_remove(&surface->link);
523 wl_list_insert(compositor->surface_list.prev, &surface->link);
524}
525
526static void
527wlsc_surface_lower(struct wlsc_surface *surface)
528{
529 struct wlsc_compositor *compositor = surface->compositor;
530
531 wl_list_remove(&surface->link);
532 wl_list_insert(&compositor->surface_list, &surface->link);
533}
534
535static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500536wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
537{
538 v1->x += v2->x;
539 v1->y += v2->y;
540 v1->z += v2->z;
541}
542
543static void
544wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
545{
546 v1->x -= v2->x;
547 v1->y -= v2->y;
548 v1->z -= v2->z;
549}
550
551static void
552wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
553{
554 v1->x *= s;
555 v1->y *= s;
556 v1->z *= s;
557}
558
559static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400560page_flip_handler(int fd, unsigned int frame,
561 unsigned int sec, unsigned int usec, void *data)
562{
563 struct wlsc_output *output = data;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400564 struct wlsc_compositor *compositor = output->compositor;
565 uint32_t msecs;
566
567 msecs = sec * 1000 + usec / 1000;
568 wl_display_post_frame(compositor->wl_display,
569 &compositor->base,
570 compositor->current_frame, msecs);
571
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400572 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400573 compositor->repaint_on_timeout = 1;
574
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400575 compositor->current_frame++;
576}
577
578static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500579repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400580{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500582 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500583 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500584 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400585 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500586
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500587 glViewport(0, 0, output->width, output->height);
588 glMatrixMode(GL_PROJECTION);
589 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500590 glFrustum(-output->width / s, output->width / s,
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500591 -output->height / s, output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500592 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500593 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400594 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500595
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500596 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500597
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500598 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500599 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500600 else
601 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400602
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500603 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500604 wlsc_surface_draw(es);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400605
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500606 wl_list_for_each(eid, &ec->input_device_list, link)
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500607 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500608
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400609 fd = eglGetDisplayFD(ec->display);
610 output->current ^= 1;
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500611
612 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
613 GL_COLOR_ATTACHMENT0_EXT,
614 GL_RENDERBUFFER_EXT,
615 output->rbo[output->current]);
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500616 drmModePageFlip(fd, output->crtc_id,
617 output->fb_id[output->current ^ 1],
618 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500619}
620
621static void
622repaint(void *data)
623{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500624 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500625 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500626
627 if (!ec->repaint_needed) {
628 ec->repaint_on_timeout = 0;
629 return;
630 }
631
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500632 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500633 repaint_output(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500634
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500635 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400636}
637
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500638static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400639wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400640{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400641 struct wlsc_output *output;
642 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400643
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400644 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400645 if (compositor->repaint_on_timeout)
646 return;
647
648 fd = eglGetDisplayFD(compositor->display);
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500649 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400650 drmModePageFlip(fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500651 output->fb_id[output->current ^ 1],
652 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400653}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500654
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500655static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500656surface_destroy(struct wl_client *client,
657 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400658{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500659 struct wlsc_surface *es = (struct wlsc_surface *) surface;
660 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500661
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500662 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400663
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400664 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400665}
666
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500667static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500668surface_attach(struct wl_client *client,
669 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500670 uint32_t width, uint32_t height, uint32_t stride,
671 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400672{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500673 struct wlsc_surface *es = (struct wlsc_surface *) surface;
674 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400675
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500676 es->width = width;
677 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400678
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500679 if (visual == &ec->argb_visual.base)
680 es->visual = &ec->argb_visual;
681 else if (visual == &ec->premultiplied_argb_visual.base)
682 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500683 else if (visual == &ec->rgb_visual.base)
684 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500685 else
686 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400687
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500688 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400689 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500690 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
691 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
692 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500693 glTextureExternalINTEL(GL_TEXTURE_2D, GL_RGBA, 4,
694 width, height, stride / 4, name);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400695
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400696}
697
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500698static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500699surface_map(struct wl_client *client,
700 struct wl_surface *surface,
701 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400702{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500703 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400704
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500705 es->map.x = x;
706 es->map.y = y;
707 es->map.width = width;
708 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400709}
710
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500711static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500712surface_copy(struct wl_client *client,
713 struct wl_surface *surface,
714 int32_t dst_x, int32_t dst_y,
715 uint32_t name, uint32_t stride,
716 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500717{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500718}
719
720static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500721surface_damage(struct wl_client *client,
722 struct wl_surface *surface,
723 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500724{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500725 /* FIXME: This need to take a damage region, of course. */
726}
727
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500728const static struct wl_surface_interface surface_interface = {
729 surface_destroy,
730 surface_attach,
731 surface_map,
732 surface_copy,
733 surface_damage
734};
735
736static void
737compositor_create_surface(struct wl_client *client,
738 struct wl_compositor *compositor, uint32_t id)
739{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500740 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400741 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500742
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400743 surface = malloc(sizeof *surface);
744 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500745 /* FIXME: Send OOM event. */
746 return;
747
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400748 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500749
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400750 wl_list_insert(ec->surface_list.prev, &surface->link);
751 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500752 &surface_interface, id);
753}
754
755static void
756compositor_commit(struct wl_client *client,
757 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500758{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500759 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500760
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400761 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500762 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500763}
764
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500765const static struct wl_compositor_interface compositor_interface = {
766 compositor_create_surface,
767 compositor_commit
768};
769
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500770static void
771wlsc_surface_transform(struct wlsc_surface *surface,
772 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
773{
774 /* Transform to surface coordinates. */
775 *sx = (x - surface->map.x) * surface->width / surface->map.width;
776 *sy = (y - surface->map.y) * surface->height / surface->map.height;
777}
778
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500779static void
780wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
781 struct wlsc_surface *surface)
782{
783 if (device->keyboard_focus == surface)
784 return;
785
786 if (device->keyboard_focus &&
787 (!surface || device->keyboard_focus->base.client != surface->base.client))
788 wl_surface_post_event(&device->keyboard_focus->base,
789 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500790 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500791
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500792 if (surface)
793 wl_surface_post_event(&surface->base,
794 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500795 WL_INPUT_KEYBOARD_FOCUS,
796 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500797
798 device->keyboard_focus = surface;
799}
800
801static void
802wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
803 struct wlsc_surface *surface)
804{
805 if (device->pointer_focus == surface)
806 return;
807
808 if (device->pointer_focus &&
809 (!surface || device->pointer_focus->base.client != surface->base.client))
810 wl_surface_post_event(&device->pointer_focus->base,
811 &device->base,
812 WL_INPUT_POINTER_FOCUS, NULL);
813 if (surface)
814 wl_surface_post_event(&surface->base,
815 &device->base,
816 WL_INPUT_POINTER_FOCUS, &surface->base);
817
818 device->pointer_focus = surface;
819}
820
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500821static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500822pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500823{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500824 struct wlsc_compositor *ec = device->ec;
825 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500826
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500827 if (device->grab > 0) {
828 wlsc_surface_transform(device->grab_surface,
829 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500830 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500831 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500832
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500833 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500834 if (es->map.x <= device->x &&
835 device->x < es->map.x + es->map.width &&
836 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500837 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500838 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500839 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500840 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500841
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500842 return NULL;
843}
844
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500845void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500846notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500847{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500848 struct wlsc_surface *es;
849 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500850 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500851 const int hotspot_x = 16, hotspot_y = 16;
852 int32_t sx, sy;
853
Ray Strodee96dcb82008-12-20 02:00:49 -0500854 if (!ec->vt_active)
855 return;
856
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500857 /* FIXME: We need some multi head love here. */
858 output = container_of(ec->output_list.next, struct wlsc_output, link);
859 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500860 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500861 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500862 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500863 if (x >= output->x + output->width)
864 x = output->x + output->width - 1;
865 if (y >= output->y + output->height)
866 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500867
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500868 device->x = x;
869 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500870 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500871
872 wlsc_input_device_set_pointer_focus(device, es);
873
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500874 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500875 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500876 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500877
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500878 device->sprite->map.x = x - hotspot_x;
879 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500880
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400881 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500882}
883
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500884void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500885notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500886 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500887{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400888 struct wlsc_surface *surface;
889 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500890 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500891
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400892 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500893 return;
894
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400895 surface = pick_surface(device, &sx, &sy);
896 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500897 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400898 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500899 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400900 device->grab_surface = surface;
901 wlsc_input_device_set_keyboard_focus(device,
902 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500903 } else {
904 device->grab--;
905 }
906
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500907 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400908 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500909 WL_INPUT_BUTTON, button, state,
910 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500911
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400912 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500913 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500914}
915
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500916static void on_term_signal(int signal_number, void *data);
917
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500918void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500919notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500920 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500921{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400922 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500923 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400924 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500925
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400926 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500927 return;
Ray Strodee96dcb82008-12-20 02:00:49 -0500928
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400929 switch (key | compositor->modifier_state) {
930 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400931 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500932 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500933 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500934
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400935 switch (key) {
936 case KEY_LEFTCTRL:
937 case KEY_RIGHTCTRL:
938 modifier = MODIFIER_CTRL;
939 break;
940
941 case KEY_LEFTALT:
942 case KEY_RIGHTALT:
943 modifier = MODIFIER_ALT;
944 break;
945
946 default:
947 modifier = 0;
948 break;
949 }
950
951 if (state)
952 compositor->modifier_state |= modifier;
953 else
954 compositor->modifier_state &= ~modifier;
955
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500956 end = device->keys.data + device->keys.size;
957 for (k = device->keys.data; k < end; k++) {
958 if (*k == key)
959 *k = *--end;
960 }
961 device->keys.size = (void *) end - device->keys.data;
962 if (state) {
963 k = wl_array_add(&device->keys, sizeof *k);
964 *k = key;
965 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500966
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500967 if (device->keyboard_focus != NULL)
968 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500969 &device->base,
970 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500971}
972
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500973struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500974evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500975 struct wl_display *display, const char *path);
976
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400977static void
978handle_surface_destroy(struct wlsc_listener *listener,
979 struct wlsc_surface *surface)
980{
981 struct wlsc_input_device *device =
982 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400983 struct wlsc_surface *focus;
984 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400985
986 if (device->grab_surface == surface) {
987 device->grab_surface = NULL;
988 device->grab = 0;
989 }
990 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400991 wlsc_input_device_set_keyboard_focus(device, NULL);
992 if (device->pointer_focus == surface) {
993 focus = pick_surface(device, &sx, &sy);
994 wlsc_input_device_set_pointer_focus(device, focus);
995 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
996 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400997}
998
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500999static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001000create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001001{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001002 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001003
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 device = malloc(sizeof *device);
1005 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001006 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001007
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001008 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001009 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001010 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001011 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001012 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001013 device->x = 100;
1014 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001015 device->ec = ec;
1016
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001017 device->listener.func = handle_surface_destroy;
1018 wl_list_insert(ec->surface_destroy_listener_list.prev,
1019 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001020 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001021
1022 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001023}
1024
1025void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001026wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001027{
1028 *x = device->x;
1029 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001030}
1031
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001032static void
1033post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001034{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001035 struct wlsc_output *output =
1036 container_of(global, struct wlsc_output, base);
1037
1038 wl_client_post_event(client, global,
1039 WL_OUTPUT_GEOMETRY,
1040 output->width, output->height);
1041}
1042
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001043static void
1044on_drm_input(int fd, uint32_t mask, void *data)
1045{
1046 drmEventContext evctx;
1047
1048 memset(&evctx, 0, sizeof evctx);
1049 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1050 evctx.page_flip_handler = page_flip_handler;
1051 drmHandleEvent(fd, &evctx);
1052}
1053
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001054static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001055init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001056{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001057 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001058 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001059 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001060
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001061 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001062 if (ec->display == NULL) {
1063 fprintf(stderr, "failed to create display\n");
1064 return -1;
1065 }
1066
1067 if (!eglInitialize(ec->display, &major, &minor)) {
1068 fprintf(stderr, "failed to initialize display\n");
1069 return -1;
1070 }
1071
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001072 ec->context = eglCreateContext(ec->display, NULL, NULL, NULL);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001073 if (ec->context == NULL) {
1074 fprintf(stderr, "failed to create context\n");
1075 return -1;
1076 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001077
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001078 if (!eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE, ec->context)) {
1079 fprintf(stderr, "failed to make context current\n");
1080 return -1;
1081 }
1082
1083 glGenFramebuffers(1, &ec->fbo);
1084 glBindFramebuffer(GL_FRAMEBUFFER_EXT, ec->fbo);
1085
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001086 loop = wl_display_get_event_loop(ec->wl_display);
1087 fd = eglGetDisplayFD(ec->display);
1088 ec->drm_source =
1089 wl_event_loop_add_fd(loop, fd,
1090 WL_EVENT_READABLE, on_drm_input, ec);
1091
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001092 return 0;
1093}
1094
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001095static drmModeModeInfo builtin_1024x768 = {
1096 63500, /* clock */
1097 1024, 1072, 1176, 1328, 0,
1098 768, 771, 775, 798, 0,
1099 59920,
1100 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1101 0,
1102 "1024x768"
1103};
1104
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001105static int
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001106create_output_for_connector(struct wlsc_compositor *ec,
1107 drmModeRes *resources,
1108 drmModeConnector *connector)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001109{
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001110 struct wlsc_output *output;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001111 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001112 drmModeModeInfo *mode;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001113 GLint handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001114 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001115
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001116 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001117
1118 output = malloc(sizeof *output);
1119 if (output == NULL)
1120 return -1;
1121
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001122 if (connector->count_modes > 0)
1123 mode = &connector->modes[0];
1124 else
1125 mode = &builtin_1024x768;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001126
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001127 encoder = drmModeGetEncoder(fd, connector->encoders[0]);
1128 if (encoder == NULL) {
1129 fprintf(stderr, "No encoder for connector.\n");
1130 return -1;
1131 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001132
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001133 for (i = 0; i < resources->count_crtcs; i++) {
1134 if (encoder->possible_crtcs & (1 << i))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001135 break;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001136 }
1137 if (i == resources->count_crtcs) {
1138 fprintf(stderr, "No usable crtc for encoder.\n");
1139 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001140 }
1141
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001142 output->compositor = ec;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001143 output->crtc_id = resources->crtcs[i];
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001144 output->connector_id = connector->connector_id;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001145 output->mode = *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001146 output->x = 0;
1147 output->y = 0;
1148 output->width = mode->hdisplay;
1149 output->height = mode->vdisplay;
1150
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001151 printf("using crtc %d, connector %d and encoder %d, mode %s\n",
1152 output->crtc_id,
1153 output->connector_id,
1154 encoder->encoder_id,
1155 mode->name);
1156
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001157 drmModeFreeEncoder(encoder);
1158
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001159 glGenRenderbuffers(2, output->rbo);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001160 for (i = 0; i < 2; i++) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001161 glBindRenderbuffer(GL_RENDERBUFFER_EXT, output->rbo[i]);
1162 glRenderbufferStorage(GL_RENDERBUFFER_EXT,
1163 GL_RGBA,
1164 output->width,
1165 output->height);
1166 glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT,
1167 GL_RENDERBUFFER_STRIDE_INTEL,
1168 &stride);
1169 glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT,
1170 GL_RENDERBUFFER_HANDLE_INTEL,
1171 &handle);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001172
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001173 ret = drmModeAddFB(fd, output->width, output->height,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001174 32, 32, stride, handle, &output->fb_id[i]);
1175 if (ret) {
1176 fprintf(stderr, "failed to add fb %d: %m\n", i);
1177 return -1;
1178 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001179 }
1180
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001181 output->current = 0;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001182 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
1183 GL_COLOR_ATTACHMENT0_EXT,
1184 GL_RENDERBUFFER_EXT,
1185 output->rbo[output->current]);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001186 ret = drmModeSetCrtc(fd, output->crtc_id,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001187 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001188 &output->connector_id, 1, &output->mode);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001189 if (ret) {
1190 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001191 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001192 }
1193
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001194 output->base.interface = &wl_output_interface;
1195 wl_display_add_object(ec->wl_display, &output->base);
1196 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1197 wl_list_insert(ec->output_list.prev, &output->link);
1198
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001199 output->background = background_create(output, option_background);
1200
1201 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001202}
1203
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001204static int
1205create_outputs(struct wlsc_compositor *ec)
1206{
1207 drmModeConnector *connector;
1208 drmModeRes *resources;
1209 int fd, i;
1210
1211 fd = eglGetDisplayFD(ec->display);
1212 resources = drmModeGetResources(fd);
1213 if (!resources) {
1214 fprintf(stderr, "drmModeGetResources failed\n");
1215 return -1;
1216 }
1217
1218 for (i = 0; i < resources->count_connectors; i++) {
1219 connector = drmModeGetConnector(fd, resources->connectors[i]);
1220 if (connector == NULL)
1221 continue;
1222
1223 if (connector->connection == DRM_MODE_CONNECTED &&
1224 (option_connector == 0 ||
1225 connector->connector_id == option_connector))
1226 if (create_output_for_connector(ec, resources, connector) < 0)
1227 return -1;
1228
1229 drmModeFreeConnector(connector);
1230 }
1231
1232 if (wl_list_empty(&ec->output_list)) {
1233 fprintf(stderr, "No currently active connector found.\n");
1234 return -1;
1235 }
1236
1237 drmModeFreeResources(resources);
1238
1239 return 0;
1240}
1241
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001242static const struct wl_interface visual_interface = {
1243 "visual", 1,
1244};
1245
1246static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001247add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001248{
1249 ec->argb_visual.base.interface = &visual_interface;
1250 ec->argb_visual.base.implementation = NULL;
1251 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001252 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001253
1254 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1255 ec->premultiplied_argb_visual.base.implementation = NULL;
1256 wl_display_add_object(ec->wl_display,
1257 &ec->premultiplied_argb_visual.base);
1258 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001259 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001260
1261 ec->rgb_visual.base.interface = &visual_interface;
1262 ec->rgb_visual.base.implementation = NULL;
1263 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001264 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1265}
1266
Ray Strode19ad6a92008-12-19 01:45:41 -05001267static void on_enter_vt(int signal_number, void *data)
1268{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001269 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001270 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001271 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001272
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001273 fd = eglGetDisplayFD(ec->display);
1274 ret = drmSetMaster(fd);
1275 if (ret) {
1276 fprintf(stderr, "failed to set drm master\n");
1277 return;
1278 }
1279
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001280 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001281 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001282
Kristian Høgsberga5db5892010-02-26 10:28:44 -05001283 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001284 ret = drmModeSetCrtc(fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001285 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001286 &output->connector_id, 1, &output->mode);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001287 if (ret)
1288 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1289 output->connector_id);
Ray Strode19ad6a92008-12-19 01:45:41 -05001290 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001291}
1292
1293static void on_leave_vt(int signal_number, void *data)
1294{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001295 struct wlsc_compositor *ec = data;
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001296 int ret, fd;
1297
1298 fd = eglGetDisplayFD(ec->display);
1299 ret = drmDropMaster(fd);
1300 if (ret) {
1301 fprintf(stderr, "failed to drop drm master\n");
1302 return;
1303 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001304
1305 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001306 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001307}
1308
Ray Strode966aa112008-12-19 14:28:02 -05001309static void
1310on_tty_input(int fd, uint32_t mask, void *data)
1311{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001312 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001313
1314 /* Ignore input to tty. We get keyboard events from evdev
1315 */
1316 tcflush(ec->tty_fd, TCIFLUSH);
1317}
1318
1319static void on_term_signal(int signal_number, 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 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1324 fprintf(stderr, "could not restore terminal to canonical mode\n");
1325
1326 exit(0);
1327}
1328
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001329static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001330{
1331 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001332 struct vt_mode mode = { 0 };
1333
1334 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1335 if (ec->tty_fd <= 0) {
1336 fprintf(stderr, "failed to open active tty: %m\n");
1337 return -1;
1338 }
Ray Strode966aa112008-12-19 14:28:02 -05001339
1340 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1341 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001342 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001343 }
1344
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001345 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001346 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001347 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001348
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001349 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001350 raw_attributes.c_oflag |= OPOST | OCRNL;
1351
1352 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1353 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1354
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001355 ec->term_signal_source =
1356 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001357
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001358 ec->tty_input_source =
1359 wl_event_loop_add_fd(loop, ec->tty_fd,
1360 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001361
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001362 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001363 mode.mode = VT_PROCESS;
1364 mode.relsig = SIGUSR1;
1365 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001366 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001367 fprintf(stderr, "failed to take control of vt handling\n");
1368 }
1369
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001370 ec->leave_vt_source =
1371 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1372 ec->enter_vt_source =
1373 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001374
1375 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001376}
1377
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001378static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001379init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001380{
1381 struct udev_enumerate *e;
1382 struct udev_list_entry *entry;
1383 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001384 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001385 struct wlsc_input_device *input_device;
1386
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001387 ec->udev = udev_new();
1388 if (ec->udev == NULL) {
1389 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001390 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001391 }
1392
1393 input_device = create_input_device(ec);
1394
1395 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001396 udev_enumerate_add_match_subsystem(e, "input");
1397 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001398 udev_enumerate_scan_devices(e);
1399 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1400 path = udev_list_entry_get_name(entry);
1401 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001402 evdev_input_device_create(input_device, ec->wl_display,
1403 udev_device_get_devnode(device));
1404 }
1405 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001406
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001407 e = udev_enumerate_new(ec->udev);
1408 udev_enumerate_add_match_subsystem(e, "drm");
1409 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1410 udev_enumerate_scan_devices(e);
1411 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1412 path = udev_list_entry_get_name(entry);
1413 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001414 if (init_egl(ec, device) < 0) {
1415 fprintf(stderr, "failed to initialize egl\n");
1416 return -1;
1417 }
1418 if (create_outputs(ec) < 0) {
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001419 fprintf(stderr, "failed to create output for %s\n", path);
1420 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001421 }
1422 }
1423 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001424
1425 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001426 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001427 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1428
1429 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001430}
1431
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001432static struct wlsc_compositor *
1433wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001434{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001435 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001436 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001437 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001438
1439 ec = malloc(sizeof *ec);
1440 if (ec == NULL)
1441 return NULL;
1442
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001443 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001444 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001445
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001446 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001447
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001448 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001449
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001450 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001451 wl_list_init(&ec->input_device_list);
1452 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001453 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001454 if (init_libudev(ec) < 0) {
1455 fprintf(stderr, "failed to initialize devices\n");
1456 return NULL;
1457 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001458
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001459 shooter = screenshooter_create(ec);
1460 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001461 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001462
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001463 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001464
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001465 setup_tty(ec, loop);
1466
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001467 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001468 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001469
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001470 return ec;
1471}
1472
1473/* The plan here is to generate a random anonymous socket name and
1474 * advertise that through a service on the session dbus.
1475 */
1476static const char socket_name[] = "\0wayland";
1477
1478int main(int argc, char *argv[])
1479{
1480 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001481 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001482 GError *error = NULL;
1483 GOptionContext *context;
1484
1485 context = g_option_context_new(NULL);
1486 g_option_context_add_main_entries(context, option_entries, "Wayland");
1487 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1488 fprintf(stderr, "option parsing failed: %s\n", error->message);
1489 exit(EXIT_FAILURE);
1490 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001491
1492 display = wl_display_create();
1493
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001494 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001495 if (ec == NULL) {
1496 fprintf(stderr, "failed to create compositor\n");
1497 exit(EXIT_FAILURE);
1498 }
1499
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001500 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001501 fprintf(stderr, "failed to add socket: %m\n");
1502 exit(EXIT_FAILURE);
1503 }
1504
1505 wl_display_run(display);
1506
1507 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001508}