blob: 9ca06b082fcfc8e2f90e19c47b756af8474489ea [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øgsberg3afd45d2010-03-03 09:54:29 -050041#define EGL_EGLEXT_PROTOTYPES
Kristian Høgsberg27803c62010-06-06 22:23:21 -040042#include <GLES2/gl2.h>
43#include <GLES2/gl2ext.h>
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050044#include <EGL/egl.h>
45#include <EGL/eglext.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040046
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050047#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050048#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050049#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040050#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040052#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
53
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050054struct wlsc_matrix {
Kristian Høgsberg27803c62010-06-06 22:23:21 -040055 GLfloat d[16];
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050056};
57
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050058struct wl_visual {
59 struct wl_object base;
60};
61
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040062struct wlsc_surface;
63
64struct wlsc_listener {
65 struct wl_list link;
66 void (*func)(struct wlsc_listener *listener,
67 struct wlsc_surface *surface);
68};
69
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050070struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050071 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050072 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050073 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050074 struct wlsc_surface *background;
Kristian Høgsberg27803c62010-06-06 22:23:21 -040075 struct wlsc_matrix matrix;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040076 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050077
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -040078 drmModeModeInfo mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050079 uint32_t crtc_id;
80 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040081
Kristian Høgsberga341fa02010-01-24 18:10:15 -050082 GLuint rbo[2];
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040083 uint32_t fb_id[2];
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050084 EGLImageKHR image[2];
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040085 uint32_t current;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050086};
87
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050088struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050089 struct wl_object base;
90 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050091 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050092 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050093 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050094
95 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050096 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050097 struct wlsc_surface *pointer_focus;
98 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050099 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400100
101 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500102};
103
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500104struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400105 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500106 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
107
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400108 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400109 EGLContext context;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500110 int drm_fd;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400111 GLuint fbo, vbo;
112 GLuint proj_uniform, tex_uniform;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400113 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500114
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500115 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500116 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500117 struct wl_list surface_list;
118
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400119 struct wl_list surface_destroy_listener_list;
120
Ray Strode966aa112008-12-19 14:28:02 -0500121 struct wl_event_source *term_signal_source;
122
123 /* tty handling state */
124 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500125 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500126
127 struct termios terminal_attributes;
128 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500129 struct wl_event_source *enter_vt_source;
130 struct wl_event_source *leave_vt_source;
131
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500132 struct udev *udev;
133
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500134 /* Repaint state. */
135 struct wl_event_source *timer_source;
136 int repaint_needed;
137 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500138 struct timespec previous_swap;
139 uint32_t current_frame;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400140 struct wl_event_source *drm_source;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500141
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400142 uint32_t modifier_state;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400143};
144
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400145#define MODIFIER_CTRL (1 << 8)
146#define MODIFIER_ALT (1 << 9)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500147
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400148struct wlsc_vector {
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400149 GLfloat f[4];
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400150};
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øgsberg3afd45d2010-03-03 09:54:29 -0500157 EGLImageKHR image;
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øgsberg27803c62010-06-06 22:23:21 -0400161 struct wlsc_matrix matrix_inv;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400162};
163
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500164static const char *option_background = "background.jpg";
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400165static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500166
167static const GOptionEntry option_entries[] = {
168 { "background", 'b', 0, G_OPTION_ARG_STRING,
169 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400170 { "connector", 'c', 0, G_OPTION_ARG_INT,
171 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500172 { NULL }
173};
174
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500175struct screenshooter {
176 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500177 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500178};
179
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500180struct screenshooter_interface {
181 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
182};
183
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500184static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500185screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500186{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500187 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500188 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500189 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500190 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500191 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500192 unsigned char *data;
193 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500194
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500195 i = 0;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500196 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500197 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500198 data = malloc(output->width * output->height * 4);
199 if (data == NULL) {
200 fprintf(stderr, "couldn't allocate image buffer\n");
201 continue;
202 }
203
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500204 glPixelStorei(GL_PACK_ALIGNMENT, 1);
205 glReadPixels(0, 0, output->width, output->height,
206 GL_RGBA, GL_UNSIGNED_BYTE, data);
207
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500208 /* FIXME: We should just use a RGB visual for the frontbuffer. */
209 for (j = 3; j < output->width * output->height * 4; j += 4)
210 data[j] = 0xff;
211
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500212 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500213 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500214 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500215 normal = gdk_pixbuf_flip(pixbuf, FALSE);
216 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
217 gdk_pixbuf_unref(normal);
218 gdk_pixbuf_unref(pixbuf);
219 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500220 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500221}
222
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500223static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500224 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500225};
226
227static const struct wl_interface screenshooter_interface = {
228 "screenshooter", 1,
229 ARRAY_LENGTH(screenshooter_methods),
230 screenshooter_methods,
231};
232
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500233struct screenshooter_interface screenshooter_implementation = {
234 screenshooter_shoot
235};
236
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500237static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500238screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500239{
240 struct screenshooter *shooter;
241
242 shooter = malloc(sizeof *shooter);
243 if (shooter == NULL)
244 return NULL;
245
246 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500247 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500248 shooter->ec = ec;
249
250 return shooter;
251};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500252
253static void
254wlsc_matrix_init(struct wlsc_matrix *matrix)
255{
256 static const struct wlsc_matrix identity = {
257 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
258 };
259
260 memcpy(matrix, &identity, sizeof identity);
261}
262
263static void
264wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
265{
266 struct wlsc_matrix tmp;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400267 const GLfloat *row, *column;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500268 div_t d;
269 int i, j;
270
271 for (i = 0; i < 16; i++) {
272 tmp.d[i] = 0;
273 d = div(i, 4);
274 row = m->d + d.quot * 4;
275 column = n->d + d.rem;
276 for (j = 0; j < 4; j++)
277 tmp.d[i] += row[j] * column[j * 4];
278 }
279 memcpy(m, &tmp, sizeof tmp);
280}
281
282static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400283wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500284{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400285 struct wlsc_matrix translate = {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500286 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
287 };
288
289 wlsc_matrix_multiply(matrix, &translate);
290}
291
292static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400293wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500294{
295 struct wlsc_matrix scale = {
296 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
297 };
298
299 wlsc_matrix_multiply(matrix, &scale);
300}
301
302static void
303wlsc_matrix_rotate(struct wlsc_matrix *matrix,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400304 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500305{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400306 GLfloat c = cos(angle);
307 GLfloat s = sin(angle);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500308 struct wlsc_matrix rotate = {
309 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
Kristian Høgsbergfa548852010-03-08 17:16:01 -0500310 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
311 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500312 0, 0, 0, 1 }
313 };
314
315 wlsc_matrix_multiply(matrix, &rotate);
316}
317
318static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400319wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
320{
321 int i, j;
322 GLfloat t;
323
324 for (i = 0; i < 4; i++) {
325 t = 0;
326 for (j = 0; j < 4; j++)
327 t += v->f[j] * matrix->d[i + j * 4];
328 v->f[i] = t;
329 }
330}
331
332static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500333wlsc_surface_init(struct wlsc_surface *surface,
334 struct wlsc_compositor *compositor, struct wl_visual *visual,
335 int32_t x, int32_t y, int32_t width, int32_t height)
336{
337 glGenTextures(1, &surface->texture);
338 surface->compositor = compositor;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500339 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400340 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400341 wlsc_matrix_scale(&surface->matrix, width, height, 1);
342 wlsc_matrix_translate(&surface->matrix, x, y, 0);
343
344 wlsc_matrix_init(&surface->matrix_inv);
345 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
346 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500347}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500348
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500349static struct wlsc_surface *
350wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500351 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500352 int x, int y, int width, int height)
353{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500354 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500355 int stride;
356 void *data;
357
358 stride = cairo_image_surface_get_stride(surface);
359 data = cairo_image_surface_get_data(surface);
360
361 es = malloc(sizeof *es);
362 if (es == NULL)
363 return NULL;
364
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500365 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
366 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500367 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400368 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
369 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400373 GL_RGBA, GL_UNSIGNED_BYTE, data);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500374
Kristian Høgsberg54879822008-11-23 17:07:32 -0500375 return es;
376}
377
378static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400379wlsc_surface_destroy(struct wlsc_surface *surface,
380 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500381{
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500382 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400383
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400384 wl_list_remove(&surface->link);
385 glDeleteTextures(1, &surface->texture);
386 wl_client_remove_surface(surface->base.client, &surface->base);
387
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500388 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400389 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400390
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400391 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500392}
393
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400394static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500395pointer_path(cairo_t *cr, int x, int y)
396{
397 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
398 const int width = 16, height = 16;
399
400 cairo_move_to(cr, x, y);
401 cairo_line_to(cr, x + tx, y + ty);
402 cairo_line_to(cr, x + dx, y + dy);
403 cairo_line_to(cr, x + width - end, y + height);
404 cairo_line_to(cr, x + width, y + height - end);
405 cairo_line_to(cr, x + dy, y + dx);
406 cairo_line_to(cr, x + ty, y + tx);
407 cairo_close_path(cr);
408}
409
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500410static struct wlsc_surface *
411pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500412{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500413 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500414 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500415 cairo_surface_t *surface;
416 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500417
418 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
419 width, height);
420
421 cr = cairo_create(surface);
422 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500423 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500424 cairo_set_source_rgb(cr, 0, 0, 0);
425 cairo_stroke_preserve(cr);
426 cairo_fill(cr);
427 blur_surface(surface, width);
428
429 pointer_path(cr, hotspot_x, hotspot_y);
430 cairo_stroke_preserve(cr);
431 cairo_set_source_rgb(cr, 1, 1, 1);
432 cairo_fill(cr);
433 cairo_destroy(cr);
434
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500435 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500436 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500437 x - hotspot_x,
438 y - hotspot_y,
439 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500440
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500441 cairo_surface_destroy(surface);
442
Kristian Høgsberg54879822008-11-23 17:07:32 -0500443 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500444}
445
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500446static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500447background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500449 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500450 GdkPixbuf *pixbuf;
451 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500452 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500453 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500454
455 background = malloc(sizeof *background);
456 if (background == NULL)
457 return NULL;
458
459 g_type_init();
460
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500461 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500462 output->width,
463 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500464 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500465 if (error != NULL) {
466 free(background);
467 return NULL;
468 }
469
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500470 data = gdk_pixbuf_get_pixels(pixbuf);
471
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500472 wlsc_surface_init(background, output->compositor,
473 &output->compositor->rgb_visual,
474 output->x, output->y, output->width, output->height);
475
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500476 glBindTexture(GL_TEXTURE_2D, background->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
478 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500481
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500482 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500483 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500484 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500485 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500486
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500487 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
488 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500489 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500492}
493
494static void
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400495wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500496{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500497 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400498 struct wlsc_matrix tmp;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500499
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400500 tmp = es->matrix;
501 wlsc_matrix_multiply(&tmp, &output->matrix);
502 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
503 glUniform1i(ec->tex_uniform, 0);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500504
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500505 if (es->visual == &ec->argb_visual) {
506 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
507 glEnable(GL_BLEND);
508 } else if (es->visual == &ec->premultiplied_argb_visual) {
509 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
510 glEnable(GL_BLEND);
511 } else {
512 glDisable(GL_BLEND);
513 }
514
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500515 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500516 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400517 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
518 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
519 5 * sizeof(GLfloat), NULL);
520 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
521 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
522 glEnableVertexAttribArray(0);
523 glEnableVertexAttribArray(1);
524 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500525}
526
527static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400528wlsc_surface_raise(struct wlsc_surface *surface)
529{
530 struct wlsc_compositor *compositor = surface->compositor;
531
532 wl_list_remove(&surface->link);
533 wl_list_insert(compositor->surface_list.prev, &surface->link);
534}
535
536static void
537wlsc_surface_lower(struct wlsc_surface *surface)
538{
539 struct wlsc_compositor *compositor = surface->compositor;
540
541 wl_list_remove(&surface->link);
542 wl_list_insert(&compositor->surface_list, &surface->link);
543}
544
545static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400546page_flip_handler(int fd, unsigned int frame,
547 unsigned int sec, unsigned int usec, void *data)
548{
549 struct wlsc_output *output = data;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400550 struct wlsc_compositor *compositor = output->compositor;
551 uint32_t msecs;
552
553 msecs = sec * 1000 + usec / 1000;
554 wl_display_post_frame(compositor->wl_display,
555 &compositor->base,
556 compositor->current_frame, msecs);
557
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400558 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400559 compositor->repaint_on_timeout = 1;
560
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400561 compositor->current_frame++;
562}
563
564static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500565repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400566{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500567 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500568 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500569 struct wlsc_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500570
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500571 glViewport(0, 0, output->width, output->height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500572
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500573 if (output->background)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400574 wlsc_surface_draw(output->background, output);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500575 else
576 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400577
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500578 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400579 wlsc_surface_draw(es, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400580
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500581 wl_list_for_each(eid, &ec->input_device_list, link)
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400582 wlsc_surface_draw(eid->sprite, output);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500583
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400584 output->current ^= 1;
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500585
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400586 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
587 GL_COLOR_ATTACHMENT0,
588 GL_RENDERBUFFER,
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500589 output->rbo[output->current]);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500590 drmModePageFlip(ec->drm_fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500591 output->fb_id[output->current ^ 1],
592 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500593}
594
595static void
596repaint(void *data)
597{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500598 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500599 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500600
601 if (!ec->repaint_needed) {
602 ec->repaint_on_timeout = 0;
603 return;
604 }
605
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500606 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500607 repaint_output(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500608
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500609 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400610}
611
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500612static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400613wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400614{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400615 struct wlsc_output *output;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400616
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400617 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400618 if (compositor->repaint_on_timeout)
619 return;
620
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500621 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500622 drmModePageFlip(compositor->drm_fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500623 output->fb_id[output->current ^ 1],
624 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400625}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500626
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500627static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500628surface_destroy(struct wl_client *client,
629 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400630{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500631 struct wlsc_surface *es = (struct wlsc_surface *) surface;
632 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500633
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500634 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400635
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400636 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400637}
638
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500639static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500640surface_attach(struct wl_client *client,
641 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500642 uint32_t width, uint32_t height, uint32_t stride,
643 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400644{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500645 struct wlsc_surface *es = (struct wlsc_surface *) surface;
646 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500647 EGLint attribs[] = {
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400648 EGL_WIDTH, 0,
649 EGL_HEIGHT, 0,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400650 EGL_IMAGE_STRIDE_MESA, 0,
Kristian Høgsberg6619d382010-06-07 12:23:01 -0400651 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500652 EGL_NONE
653 };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400654
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500655 es->width = width;
656 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400657
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500658 if (visual == &ec->argb_visual.base)
659 es->visual = &ec->argb_visual;
660 else if (visual == &ec->premultiplied_argb_visual.base)
661 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500662 else if (visual == &ec->rgb_visual.base)
663 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500664 else
665 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400666
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500667 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400668 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
669 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500670 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
671 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500672
673 if (es->image)
674 eglDestroyImageKHR(ec->display, es->image);
675
676 attribs[1] = width;
677 attribs[3] = height;
Kristian Høgsberg12923832010-06-06 22:44:12 -0400678 attribs[5] = stride / 4;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500679
680 es->image = eglCreateImageKHR(ec->display, ec->context,
Kristian Høgsberg12923832010-06-06 22:44:12 -0400681 EGL_DRM_IMAGE_MESA,
682 (EGLClientBuffer) name, attribs);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500683 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400684
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400685}
686
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500687static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500688surface_map(struct wl_client *client,
689 struct wl_surface *surface,
690 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400691{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500692 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400693
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400694 wlsc_matrix_translate(&es->matrix, x, y, 0);
695 wlsc_matrix_scale(&es->matrix, width, height, 1);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400696}
697
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500698static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500699surface_copy(struct wl_client *client,
700 struct wl_surface *surface,
701 int32_t dst_x, int32_t dst_y,
702 uint32_t name, uint32_t stride,
703 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500704{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500705}
706
707static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500708surface_damage(struct wl_client *client,
709 struct wl_surface *surface,
710 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500711{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500712 /* FIXME: This need to take a damage region, of course. */
713}
714
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500715const static struct wl_surface_interface surface_interface = {
716 surface_destroy,
717 surface_attach,
718 surface_map,
719 surface_copy,
720 surface_damage
721};
722
723static void
724compositor_create_surface(struct wl_client *client,
725 struct wl_compositor *compositor, uint32_t id)
726{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500727 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400728 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500729
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400730 surface = malloc(sizeof *surface);
731 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500732 /* FIXME: Send OOM event. */
733 return;
734
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400735 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500736
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400737 wl_list_insert(ec->surface_list.prev, &surface->link);
738 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500739 &surface_interface, id);
740}
741
742static void
743compositor_commit(struct wl_client *client,
744 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500745{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500746 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500747
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400748 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500749 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750}
751
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500752const static struct wl_compositor_interface compositor_interface = {
753 compositor_create_surface,
754 compositor_commit
755};
756
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500757static void
758wlsc_surface_transform(struct wlsc_surface *surface,
759 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
760{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400761 struct wlsc_vector v = { { x, y, 0, 1 } };
762
763 wlsc_matrix_transform(&surface->matrix_inv, &v);
764 *sx = v.f[0];
765 *sy = v.f[1];
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500766}
767
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500768static void
769wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
770 struct wlsc_surface *surface)
771{
772 if (device->keyboard_focus == surface)
773 return;
774
775 if (device->keyboard_focus &&
776 (!surface || device->keyboard_focus->base.client != surface->base.client))
777 wl_surface_post_event(&device->keyboard_focus->base,
778 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500779 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500780
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500781 if (surface)
782 wl_surface_post_event(&surface->base,
783 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500784 WL_INPUT_KEYBOARD_FOCUS,
785 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500786
787 device->keyboard_focus = surface;
788}
789
790static void
791wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
792 struct wlsc_surface *surface)
793{
794 if (device->pointer_focus == surface)
795 return;
796
797 if (device->pointer_focus &&
798 (!surface || device->pointer_focus->base.client != surface->base.client))
799 wl_surface_post_event(&device->pointer_focus->base,
800 &device->base,
801 WL_INPUT_POINTER_FOCUS, NULL);
802 if (surface)
803 wl_surface_post_event(&surface->base,
804 &device->base,
805 WL_INPUT_POINTER_FOCUS, &surface->base);
806
807 device->pointer_focus = surface;
808}
809
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500810static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500811pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500812{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500813 struct wlsc_compositor *ec = device->ec;
814 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500815
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500816 if (device->grab > 0) {
817 wlsc_surface_transform(device->grab_surface,
818 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500819 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500820 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500821
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400822 wl_list_for_each(es, &ec->surface_list, link) {
823 wlsc_surface_transform(es, device->x, device->y, sx, sy);
824 if (0 <= *sx && *sx < es->width &&
825 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500826 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400827 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500828
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500829 return NULL;
830}
831
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500832void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500833notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500834{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500835 struct wlsc_surface *es;
836 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500837 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500838 const int hotspot_x = 16, hotspot_y = 16;
839 int32_t sx, sy;
840
Ray Strodee96dcb82008-12-20 02:00:49 -0500841 if (!ec->vt_active)
842 return;
843
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500844 /* FIXME: We need some multi head love here. */
845 output = container_of(ec->output_list.next, struct wlsc_output, link);
846 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500847 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500848 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500849 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500850 if (x >= output->x + output->width)
851 x = output->x + output->width - 1;
852 if (y >= output->y + output->height)
853 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500854
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500855 device->x = x;
856 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500857 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500858
859 wlsc_input_device_set_pointer_focus(device, es);
860
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500861 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500862 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500863 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500864
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400865 wlsc_matrix_init(&device->sprite->matrix);
866 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
867 wlsc_matrix_translate(&device->sprite->matrix,
868 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500869
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400870 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500871}
872
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500873void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500874notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500875 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500876{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400877 struct wlsc_surface *surface;
878 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500879 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500880
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400881 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500882 return;
883
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400884 surface = pick_surface(device, &sx, &sy);
885 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500886 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400887 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500888 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400889 device->grab_surface = surface;
890 wlsc_input_device_set_keyboard_focus(device,
891 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500892 } else {
893 device->grab--;
894 }
895
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500896 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400897 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500898 WL_INPUT_BUTTON, button, state,
899 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500900
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400901 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500902 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500903}
904
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500905static void on_term_signal(int signal_number, void *data);
906
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500907void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500908notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500909 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500910{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400911 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500912 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400913 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500914
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400915 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500916 return;
Ray Strodee96dcb82008-12-20 02:00:49 -0500917
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400918 switch (key | compositor->modifier_state) {
919 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400920 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500921 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500922 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500923
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400924 switch (key) {
925 case KEY_LEFTCTRL:
926 case KEY_RIGHTCTRL:
927 modifier = MODIFIER_CTRL;
928 break;
929
930 case KEY_LEFTALT:
931 case KEY_RIGHTALT:
932 modifier = MODIFIER_ALT;
933 break;
934
935 default:
936 modifier = 0;
937 break;
938 }
939
940 if (state)
941 compositor->modifier_state |= modifier;
942 else
943 compositor->modifier_state &= ~modifier;
944
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500945 end = device->keys.data + device->keys.size;
946 for (k = device->keys.data; k < end; k++) {
947 if (*k == key)
948 *k = *--end;
949 }
950 device->keys.size = (void *) end - device->keys.data;
951 if (state) {
952 k = wl_array_add(&device->keys, sizeof *k);
953 *k = key;
954 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500955
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500956 if (device->keyboard_focus != NULL)
957 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500958 &device->base,
959 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500960}
961
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500962struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500963evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500964 struct wl_display *display, const char *path);
965
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400966static void
967handle_surface_destroy(struct wlsc_listener *listener,
968 struct wlsc_surface *surface)
969{
970 struct wlsc_input_device *device =
971 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400972 struct wlsc_surface *focus;
973 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400974
975 if (device->grab_surface == surface) {
976 device->grab_surface = NULL;
977 device->grab = 0;
978 }
979 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400980 wlsc_input_device_set_keyboard_focus(device, NULL);
981 if (device->pointer_focus == surface) {
982 focus = pick_surface(device, &sx, &sy);
983 wlsc_input_device_set_pointer_focus(device, focus);
984 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
985 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400986}
987
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500988static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500989create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500990{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500991 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500992
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500993 device = malloc(sizeof *device);
994 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500995 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500996
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500997 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500998 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500999 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001000 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001001 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002 device->x = 100;
1003 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 device->ec = ec;
1005
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001006 device->listener.func = handle_surface_destroy;
1007 wl_list_insert(ec->surface_destroy_listener_list.prev,
1008 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001009 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001010
1011 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001012}
1013
1014void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001015wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001016{
1017 *x = device->x;
1018 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001019}
1020
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001021static void
1022post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001023{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001024 struct wlsc_output *output =
1025 container_of(global, struct wlsc_output, base);
1026
1027 wl_client_post_event(client, global,
1028 WL_OUTPUT_GEOMETRY,
1029 output->width, output->height);
1030}
1031
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001032static void
1033on_drm_input(int fd, uint32_t mask, void *data)
1034{
1035 drmEventContext evctx;
1036
1037 memset(&evctx, 0, sizeof evctx);
1038 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1039 evctx.page_flip_handler = page_flip_handler;
1040 drmHandleEvent(fd, &evctx);
1041}
1042
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001043static const char vertex_shader[] =
1044 "uniform mat4 proj;\n"
1045 "attribute vec4 position;\n"
1046 "attribute vec2 texcoord;\n"
1047 "varying vec2 v_texcoord;\n"
1048 "void main()\n"
1049 "{\n"
1050 " gl_Position = proj * position;\n"
1051 " v_texcoord = texcoord;\n"
1052 "}\n";
1053
1054static const char fragment_shader[] =
1055 /* "precision mediump float;\n" */
1056 "varying vec2 v_texcoord;\n"
1057 "uniform sampler2D tex;\n"
1058 "void main()\n"
1059 "{\n"
1060 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1061 "}\n";
1062
1063static void
1064init_shaders(struct wlsc_compositor *ec)
1065{
1066 GLuint v, f, program;
1067 const char *p;
1068 char msg[512];
1069 GLfloat vertices[4 * 5];
1070
1071 p = vertex_shader;
1072 v = glCreateShader(GL_VERTEX_SHADER);
1073 glShaderSource(v, 1, &p, NULL);
1074 glCompileShader(v);
1075 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1076 printf("vertex shader info: %s\n", msg);
1077
1078 p = fragment_shader;
1079 f = glCreateShader(GL_FRAGMENT_SHADER);
1080 glShaderSource(f, 1, &p, NULL);
1081 glCompileShader(f);
1082 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1083 printf("fragment shader info: %s\n", msg);
1084
1085 program = glCreateProgram();
1086 glAttachShader(program, v);
1087 glAttachShader(program, f);
1088 glBindAttribLocation(program, 0, "position");
1089 glBindAttribLocation(program, 1, "texcoord");
1090
1091 glLinkProgram(program);
1092 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1093 printf("info: %s\n", msg);
1094
1095 glUseProgram(program);
1096 ec->proj_uniform = glGetUniformLocation(program, "proj");
1097 ec->tex_uniform = glGetUniformLocation(program, "tex");
1098
1099 vertices[ 0] = 0.0;
1100 vertices[ 1] = 0.0;
1101 vertices[ 2] = 0.0;
1102 vertices[ 3] = 0.0;
1103 vertices[ 4] = 0.0;
1104
1105 vertices[ 5] = 0.0;
1106 vertices[ 6] = 1.0;
1107 vertices[ 7] = 0.0;
1108 vertices[ 8] = 0.0;
1109 vertices[ 9] = 1.0;
1110
1111 vertices[10] = 1.0;
1112 vertices[11] = 0.0;
1113 vertices[12] = 0.0;
1114 vertices[13] = 1.0;
1115 vertices[14] = 0.0;
1116
1117 vertices[15] = 1.0;
1118 vertices[16] = 1.0;
1119 vertices[17] = 0.0;
1120 vertices[18] = 1.0;
1121 vertices[19] = 1.0;
1122
1123 glGenBuffers(1, &ec->vbo);
1124 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1125 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
1126}
1127
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001128static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001129init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001130{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001131 struct wl_event_loop *loop;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001132 EGLint major, minor, count;
1133 EGLConfig config;
1134 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001135
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001136 static const EGLint config_attribs[] = {
1137 EGL_SURFACE_TYPE, 0,
1138 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
1139 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1140 EGL_NONE
1141 };
1142
1143 get_typed_display_mesa =
1144 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
1145 if (get_typed_display_mesa == NULL) {
1146 fprintf(stderr, "eglGetDisplayMESA() not found\n");
1147 return -1;
1148 }
1149
1150 ec->drm_fd = open(udev_device_get_devnode(device), O_RDWR);
1151 if (ec->drm_fd < 0) {
1152 /* Probably permissions error */
1153 fprintf(stderr, "couldn't open %s, skipping\n",
1154 udev_device_get_devnode(device));
1155 return -1;
1156 }
1157
1158 ec->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
1159 (void *) ec->drm_fd);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001160 if (ec->display == NULL) {
1161 fprintf(stderr, "failed to create display\n");
1162 return -1;
1163 }
1164
1165 if (!eglInitialize(ec->display, &major, &minor)) {
1166 fprintf(stderr, "failed to initialize display\n");
1167 return -1;
1168 }
1169
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001170 if (!eglChooseConfig(ec->display, config_attribs, &config, 1, &count) ||
1171 count == 0) {
1172 fprintf(stderr, "eglChooseConfig() failed\n");
1173 return -1;
1174 }
1175
1176 eglBindAPI(EGL_OPENGL_API);
1177 ec->context = eglCreateContext(ec->display, config, EGL_NO_CONTEXT, NULL);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001178 if (ec->context == NULL) {
1179 fprintf(stderr, "failed to create context\n");
1180 return -1;
1181 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001182
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001183 if (!eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE, ec->context)) {
1184 fprintf(stderr, "failed to make context current\n");
1185 return -1;
1186 }
1187
1188 glGenFramebuffers(1, &ec->fbo);
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001189 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1190 glActiveTexture(GL_TEXTURE0);
1191 init_shaders(ec);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001192
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001193 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001194 ec->drm_source =
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001195 wl_event_loop_add_fd(loop, ec->drm_fd,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001196 WL_EVENT_READABLE, on_drm_input, ec);
1197
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001198 return 0;
1199}
1200
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001201static drmModeModeInfo builtin_1024x768 = {
1202 63500, /* clock */
1203 1024, 1072, 1176, 1328, 0,
1204 768, 771, 775, 798, 0,
1205 59920,
1206 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1207 0,
1208 "1024x768"
1209};
1210
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001211static int
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001212create_output_for_connector(struct wlsc_compositor *ec,
1213 drmModeRes *resources,
1214 drmModeConnector *connector)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001215{
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001216 struct wlsc_output *output;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001217 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001218 drmModeModeInfo *mode;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001219 int i, ret;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001220 EGLint handle, stride, attribs[] = {
1221 EGL_WIDTH, 0,
1222 EGL_HEIGHT, 0,
1223 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
1224 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001225 EGL_NONE
1226 };
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001227
1228 output = malloc(sizeof *output);
1229 if (output == NULL)
1230 return -1;
1231
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001232 if (connector->count_modes > 0)
1233 mode = &connector->modes[0];
1234 else
1235 mode = &builtin_1024x768;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001236
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001237 encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001238 if (encoder == NULL) {
1239 fprintf(stderr, "No encoder for connector.\n");
1240 return -1;
1241 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001242
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001243 for (i = 0; i < resources->count_crtcs; i++) {
1244 if (encoder->possible_crtcs & (1 << i))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001245 break;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001246 }
1247 if (i == resources->count_crtcs) {
1248 fprintf(stderr, "No usable crtc for encoder.\n");
1249 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001250 }
1251
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001252 output->compositor = ec;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001253 output->crtc_id = resources->crtcs[i];
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001254 output->connector_id = connector->connector_id;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001255 output->mode = *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001256 output->x = 0;
1257 output->y = 0;
1258 output->width = mode->hdisplay;
1259 output->height = mode->vdisplay;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001260 wlsc_matrix_init(&output->matrix);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001261
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001262 wlsc_matrix_translate(&output->matrix,
1263 -output->x - output->width / 2.0,
1264 -output->y - output->height / 2.0, 0);
1265 wlsc_matrix_scale(&output->matrix,
1266 2.0 / output->width, 2.0 / output->height, 1);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001267
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001268 drmModeFreeEncoder(encoder);
1269
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001270 glGenRenderbuffers(2, output->rbo);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001271 for (i = 0; i < 2; i++) {
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001272 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001273
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001274 attribs[1] = output->width;
1275 attribs[3] = output->height;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001276 output->image[i] = eglCreateDRMImageMESA(ec->display, attribs);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001277 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, output->image[i]);
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001278 eglExportDRMImageMESA(ec->display, output->image[i], NULL, &handle, &stride);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001279
1280 ret = drmModeAddFB(ec->drm_fd, output->width, output->height,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001281 32, 32, stride, handle, &output->fb_id[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001282 if (ret) {
1283 fprintf(stderr, "failed to add fb %d: %m\n", i);
1284 return -1;
1285 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001286 }
1287
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001288 output->current = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001289 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1290 GL_COLOR_ATTACHMENT0,
1291 GL_RENDERBUFFER,
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001292 output->rbo[output->current]);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001293 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001294 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001295 &output->connector_id, 1, &output->mode);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001296 if (ret) {
1297 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001298 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001299 }
1300
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001301 output->base.interface = &wl_output_interface;
1302 wl_display_add_object(ec->wl_display, &output->base);
1303 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1304 wl_list_insert(ec->output_list.prev, &output->link);
1305
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001306 output->background = background_create(output, option_background);
1307
1308 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001309}
1310
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001311static int
1312create_outputs(struct wlsc_compositor *ec)
1313{
1314 drmModeConnector *connector;
1315 drmModeRes *resources;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001316 int i;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001317
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001318 resources = drmModeGetResources(ec->drm_fd);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001319 if (!resources) {
1320 fprintf(stderr, "drmModeGetResources failed\n");
1321 return -1;
1322 }
1323
1324 for (i = 0; i < resources->count_connectors; i++) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001325 connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001326 if (connector == NULL)
1327 continue;
1328
1329 if (connector->connection == DRM_MODE_CONNECTED &&
1330 (option_connector == 0 ||
1331 connector->connector_id == option_connector))
1332 if (create_output_for_connector(ec, resources, connector) < 0)
1333 return -1;
1334
1335 drmModeFreeConnector(connector);
1336 }
1337
1338 if (wl_list_empty(&ec->output_list)) {
1339 fprintf(stderr, "No currently active connector found.\n");
1340 return -1;
1341 }
1342
1343 drmModeFreeResources(resources);
1344
1345 return 0;
1346}
1347
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001348static const struct wl_interface visual_interface = {
1349 "visual", 1,
1350};
1351
1352static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001353add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001354{
1355 ec->argb_visual.base.interface = &visual_interface;
1356 ec->argb_visual.base.implementation = NULL;
1357 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001358 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001359
1360 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1361 ec->premultiplied_argb_visual.base.implementation = NULL;
1362 wl_display_add_object(ec->wl_display,
1363 &ec->premultiplied_argb_visual.base);
1364 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001365 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001366
1367 ec->rgb_visual.base.interface = &visual_interface;
1368 ec->rgb_visual.base.implementation = NULL;
1369 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001370 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1371}
1372
Ray Strode19ad6a92008-12-19 01:45:41 -05001373static void on_enter_vt(int signal_number, void *data)
1374{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001375 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001376 struct wlsc_output *output;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001377 int ret;
Ray Strode19ad6a92008-12-19 01:45:41 -05001378
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001379 ret = drmSetMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001380 if (ret) {
1381 fprintf(stderr, "failed to set drm master\n");
Kristian Høgsberg6a9bbd02010-06-06 22:43:55 -04001382 on_term_signal(SIGTERM, ec);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001383 return;
1384 }
1385
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001386 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001387 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001388
Kristian Høgsberga5db5892010-02-26 10:28:44 -05001389 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001390 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001391 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001392 &output->connector_id, 1, &output->mode);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001393 if (ret)
1394 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1395 output->connector_id);
Ray Strode19ad6a92008-12-19 01:45:41 -05001396 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001397}
1398
1399static void on_leave_vt(int signal_number, void *data)
1400{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001401 struct wlsc_compositor *ec = data;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001402 int ret;
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001403
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001404 ret = drmDropMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001405 if (ret) {
1406 fprintf(stderr, "failed to drop drm master\n");
Kristian Høgsberg6a9bbd02010-06-06 22:43:55 -04001407 on_term_signal(SIGTERM, ec);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001408 return;
1409 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001410
1411 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001412 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001413}
1414
Ray Strode966aa112008-12-19 14:28:02 -05001415static void
1416on_tty_input(int fd, uint32_t mask, void *data)
1417{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001418 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001419
1420 /* Ignore input to tty. We get keyboard events from evdev
1421 */
1422 tcflush(ec->tty_fd, TCIFLUSH);
1423}
1424
1425static void on_term_signal(int signal_number, void *data)
1426{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001427 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001428
1429 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1430 fprintf(stderr, "could not restore terminal to canonical mode\n");
1431
1432 exit(0);
1433}
1434
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001435static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001436{
1437 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001438 struct vt_mode mode = { 0 };
1439
1440 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1441 if (ec->tty_fd <= 0) {
1442 fprintf(stderr, "failed to open active tty: %m\n");
1443 return -1;
1444 }
Ray Strode966aa112008-12-19 14:28:02 -05001445
1446 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1447 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001448 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001449 }
1450
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001451 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001452 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001453 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001454
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001455 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001456 raw_attributes.c_oflag |= OPOST | OCRNL;
1457
1458 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1459 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1460
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001461 ec->term_signal_source =
1462 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001463
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001464 ec->tty_input_source =
1465 wl_event_loop_add_fd(loop, ec->tty_fd,
1466 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001467
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001468 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001469 mode.mode = VT_PROCESS;
1470 mode.relsig = SIGUSR1;
1471 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001472 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001473 fprintf(stderr, "failed to take control of vt handling\n");
1474 }
1475
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001476 ec->leave_vt_source =
1477 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1478 ec->enter_vt_source =
1479 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001480
1481 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001482}
1483
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001484static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001485init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001486{
1487 struct udev_enumerate *e;
1488 struct udev_list_entry *entry;
1489 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001490 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001491 struct wlsc_input_device *input_device;
1492
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001493 ec->udev = udev_new();
1494 if (ec->udev == NULL) {
1495 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001496 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001497 }
1498
1499 input_device = create_input_device(ec);
1500
1501 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001502 udev_enumerate_add_match_subsystem(e, "input");
1503 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001504 udev_enumerate_scan_devices(e);
1505 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1506 path = udev_list_entry_get_name(entry);
1507 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001508 evdev_input_device_create(input_device, ec->wl_display,
1509 udev_device_get_devnode(device));
1510 }
1511 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001512
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001513 e = udev_enumerate_new(ec->udev);
1514 udev_enumerate_add_match_subsystem(e, "drm");
1515 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1516 udev_enumerate_scan_devices(e);
1517 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1518 path = udev_list_entry_get_name(entry);
1519 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001520 if (init_egl(ec, device) < 0) {
1521 fprintf(stderr, "failed to initialize egl\n");
1522 return -1;
1523 }
1524 if (create_outputs(ec) < 0) {
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001525 fprintf(stderr, "failed to create output for %s\n", path);
1526 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001527 }
1528 }
1529 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001530
1531 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001532 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001533 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1534
1535 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001536}
1537
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001538static struct wlsc_compositor *
1539wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001540{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001541 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001542 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001543 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001544
1545 ec = malloc(sizeof *ec);
1546 if (ec == NULL)
1547 return NULL;
1548
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001549 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001550 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001551
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001552 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001553
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001554 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001555
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001556 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001557 wl_list_init(&ec->input_device_list);
1558 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001559 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001560 if (init_libudev(ec) < 0) {
1561 fprintf(stderr, "failed to initialize devices\n");
1562 return NULL;
1563 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001564
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001565 shooter = screenshooter_create(ec);
1566 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001567 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001568
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001569 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001570
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001571 setup_tty(ec, loop);
1572
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001573 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001574 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001575
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001576 return ec;
1577}
1578
1579/* The plan here is to generate a random anonymous socket name and
1580 * advertise that through a service on the session dbus.
1581 */
1582static const char socket_name[] = "\0wayland";
1583
1584int main(int argc, char *argv[])
1585{
1586 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001587 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001588 GError *error = NULL;
1589 GOptionContext *context;
1590
1591 context = g_option_context_new(NULL);
1592 g_option_context_add_main_entries(context, option_entries, "Wayland");
1593 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1594 fprintf(stderr, "option parsing failed: %s\n", error->message);
1595 exit(EXIT_FAILURE);
1596 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001597
1598 display = wl_display_create();
1599
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001600 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001601 if (ec == NULL) {
1602 fprintf(stderr, "failed to create compositor\n");
1603 exit(EXIT_FAILURE);
1604 }
1605
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001606 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001607 fprintf(stderr, "failed to add socket: %m\n");
1608 exit(EXIT_FAILURE);
1609 }
1610
1611 wl_display_run(display);
1612
1613 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001614}