blob: e73c4de368d1aa8577221948c311b0111b840a0b [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,
650 EGL_IMAGE_NAME_MESA, 0,
651 EGL_IMAGE_STRIDE_MESA, 0,
652 EGL_IMAGE_FORMAT_MESA, EGL_FORMAT_RGBA_8888_KHR,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500653 EGL_NONE
654 };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400655
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500656 es->width = width;
657 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400658
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500659 if (visual == &ec->argb_visual.base)
660 es->visual = &ec->argb_visual;
661 else if (visual == &ec->premultiplied_argb_visual.base)
662 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500663 else if (visual == &ec->rgb_visual.base)
664 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500665 else
666 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400667
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500668 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400669 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
670 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500671 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
672 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500673
674 if (es->image)
675 eglDestroyImageKHR(ec->display, es->image);
676
677 attribs[1] = width;
678 attribs[3] = height;
679 attribs[5] = name;
680 attribs[7] = stride / 4;
681
682 es->image = eglCreateImageKHR(ec->display, ec->context,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400683 EGL_DRM_IMAGE_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500684 NULL, attribs);
685 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400686
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400687}
688
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500689static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500690surface_map(struct wl_client *client,
691 struct wl_surface *surface,
692 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400693{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500694 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400695
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400696 wlsc_matrix_translate(&es->matrix, x, y, 0);
697 wlsc_matrix_scale(&es->matrix, width, height, 1);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400698}
699
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500700static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500701surface_copy(struct wl_client *client,
702 struct wl_surface *surface,
703 int32_t dst_x, int32_t dst_y,
704 uint32_t name, uint32_t stride,
705 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500706{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500707}
708
709static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500710surface_damage(struct wl_client *client,
711 struct wl_surface *surface,
712 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500713{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500714 /* FIXME: This need to take a damage region, of course. */
715}
716
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500717const static struct wl_surface_interface surface_interface = {
718 surface_destroy,
719 surface_attach,
720 surface_map,
721 surface_copy,
722 surface_damage
723};
724
725static void
726compositor_create_surface(struct wl_client *client,
727 struct wl_compositor *compositor, uint32_t id)
728{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500729 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400730 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500731
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400732 surface = malloc(sizeof *surface);
733 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500734 /* FIXME: Send OOM event. */
735 return;
736
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400737 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500738
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400739 wl_list_insert(ec->surface_list.prev, &surface->link);
740 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500741 &surface_interface, id);
742}
743
744static void
745compositor_commit(struct wl_client *client,
746 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500747{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500748 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500749
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400750 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500751 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500752}
753
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500754const static struct wl_compositor_interface compositor_interface = {
755 compositor_create_surface,
756 compositor_commit
757};
758
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500759static void
760wlsc_surface_transform(struct wlsc_surface *surface,
761 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
762{
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400763 struct wlsc_vector v = { { x, y, 0, 1 } };
764
765 wlsc_matrix_transform(&surface->matrix_inv, &v);
766 *sx = v.f[0];
767 *sy = v.f[1];
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500768}
769
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500770static void
771wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
772 struct wlsc_surface *surface)
773{
774 if (device->keyboard_focus == surface)
775 return;
776
777 if (device->keyboard_focus &&
778 (!surface || device->keyboard_focus->base.client != surface->base.client))
779 wl_surface_post_event(&device->keyboard_focus->base,
780 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500781 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500782
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500783 if (surface)
784 wl_surface_post_event(&surface->base,
785 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500786 WL_INPUT_KEYBOARD_FOCUS,
787 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500788
789 device->keyboard_focus = surface;
790}
791
792static void
793wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
794 struct wlsc_surface *surface)
795{
796 if (device->pointer_focus == surface)
797 return;
798
799 if (device->pointer_focus &&
800 (!surface || device->pointer_focus->base.client != surface->base.client))
801 wl_surface_post_event(&device->pointer_focus->base,
802 &device->base,
803 WL_INPUT_POINTER_FOCUS, NULL);
804 if (surface)
805 wl_surface_post_event(&surface->base,
806 &device->base,
807 WL_INPUT_POINTER_FOCUS, &surface->base);
808
809 device->pointer_focus = surface;
810}
811
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500812static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500813pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500814{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500815 struct wlsc_compositor *ec = device->ec;
816 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500817
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500818 if (device->grab > 0) {
819 wlsc_surface_transform(device->grab_surface,
820 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500821 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500822 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500823
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400824 wl_list_for_each(es, &ec->surface_list, link) {
825 wlsc_surface_transform(es, device->x, device->y, sx, sy);
826 if (0 <= *sx && *sx < es->width &&
827 0 <= *sy && *sy < es->height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500828 return es;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400829 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500830
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500831 return NULL;
832}
833
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500834void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500835notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500836{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500837 struct wlsc_surface *es;
838 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500839 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500840 const int hotspot_x = 16, hotspot_y = 16;
841 int32_t sx, sy;
842
Ray Strodee96dcb82008-12-20 02:00:49 -0500843 if (!ec->vt_active)
844 return;
845
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500846 /* FIXME: We need some multi head love here. */
847 output = container_of(ec->output_list.next, struct wlsc_output, link);
848 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500849 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500850 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500851 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500852 if (x >= output->x + output->width)
853 x = output->x + output->width - 1;
854 if (y >= output->y + output->height)
855 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500856
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500857 device->x = x;
858 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500859 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500860
861 wlsc_input_device_set_pointer_focus(device, es);
862
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500863 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500864 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500865 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500866
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400867 wlsc_matrix_init(&device->sprite->matrix);
868 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
869 wlsc_matrix_translate(&device->sprite->matrix,
870 x - hotspot_x, y - hotspot_y, 0);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500871
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400872 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500873}
874
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500875void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500876notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500877 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500878{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400879 struct wlsc_surface *surface;
880 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500881 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500882
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400883 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500884 return;
885
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400886 surface = pick_surface(device, &sx, &sy);
887 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500888 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400889 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500890 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400891 device->grab_surface = surface;
892 wlsc_input_device_set_keyboard_focus(device,
893 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500894 } else {
895 device->grab--;
896 }
897
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500898 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400899 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500900 WL_INPUT_BUTTON, button, state,
901 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500902
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400903 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500904 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500905}
906
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500907static void on_term_signal(int signal_number, void *data);
908
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500909void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500910notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500911 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500912{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400913 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500914 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400915 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500916
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400917 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500918 return;
Ray Strodee96dcb82008-12-20 02:00:49 -0500919
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400920 switch (key | compositor->modifier_state) {
921 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400922 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500923 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500924 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500925
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400926 switch (key) {
927 case KEY_LEFTCTRL:
928 case KEY_RIGHTCTRL:
929 modifier = MODIFIER_CTRL;
930 break;
931
932 case KEY_LEFTALT:
933 case KEY_RIGHTALT:
934 modifier = MODIFIER_ALT;
935 break;
936
937 default:
938 modifier = 0;
939 break;
940 }
941
942 if (state)
943 compositor->modifier_state |= modifier;
944 else
945 compositor->modifier_state &= ~modifier;
946
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500947 end = device->keys.data + device->keys.size;
948 for (k = device->keys.data; k < end; k++) {
949 if (*k == key)
950 *k = *--end;
951 }
952 device->keys.size = (void *) end - device->keys.data;
953 if (state) {
954 k = wl_array_add(&device->keys, sizeof *k);
955 *k = key;
956 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500957
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500958 if (device->keyboard_focus != NULL)
959 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500960 &device->base,
961 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500962}
963
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500964struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500965evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500966 struct wl_display *display, const char *path);
967
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400968static void
969handle_surface_destroy(struct wlsc_listener *listener,
970 struct wlsc_surface *surface)
971{
972 struct wlsc_input_device *device =
973 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400974 struct wlsc_surface *focus;
975 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400976
977 if (device->grab_surface == surface) {
978 device->grab_surface = NULL;
979 device->grab = 0;
980 }
981 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400982 wlsc_input_device_set_keyboard_focus(device, NULL);
983 if (device->pointer_focus == surface) {
984 focus = pick_surface(device, &sx, &sy);
985 wlsc_input_device_set_pointer_focus(device, focus);
986 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
987 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400988}
989
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500990static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500991create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500992{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500993 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500994
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500995 device = malloc(sizeof *device);
996 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500997 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500998
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500999 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001000 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001001 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001003 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 device->x = 100;
1005 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001006 device->ec = ec;
1007
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001008 device->listener.func = handle_surface_destroy;
1009 wl_list_insert(ec->surface_destroy_listener_list.prev,
1010 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001011 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001012
1013 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001014}
1015
1016void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001017wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001018{
1019 *x = device->x;
1020 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001021}
1022
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001023static void
1024post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001025{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001026 struct wlsc_output *output =
1027 container_of(global, struct wlsc_output, base);
1028
1029 wl_client_post_event(client, global,
1030 WL_OUTPUT_GEOMETRY,
1031 output->width, output->height);
1032}
1033
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001034static void
1035on_drm_input(int fd, uint32_t mask, void *data)
1036{
1037 drmEventContext evctx;
1038
1039 memset(&evctx, 0, sizeof evctx);
1040 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1041 evctx.page_flip_handler = page_flip_handler;
1042 drmHandleEvent(fd, &evctx);
1043}
1044
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001045static const char vertex_shader[] =
1046 "uniform mat4 proj;\n"
1047 "attribute vec4 position;\n"
1048 "attribute vec2 texcoord;\n"
1049 "varying vec2 v_texcoord;\n"
1050 "void main()\n"
1051 "{\n"
1052 " gl_Position = proj * position;\n"
1053 " v_texcoord = texcoord;\n"
1054 "}\n";
1055
1056static const char fragment_shader[] =
1057 /* "precision mediump float;\n" */
1058 "varying vec2 v_texcoord;\n"
1059 "uniform sampler2D tex;\n"
1060 "void main()\n"
1061 "{\n"
1062 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1063 "}\n";
1064
1065static void
1066init_shaders(struct wlsc_compositor *ec)
1067{
1068 GLuint v, f, program;
1069 const char *p;
1070 char msg[512];
1071 GLfloat vertices[4 * 5];
1072
1073 p = vertex_shader;
1074 v = glCreateShader(GL_VERTEX_SHADER);
1075 glShaderSource(v, 1, &p, NULL);
1076 glCompileShader(v);
1077 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1078 printf("vertex shader info: %s\n", msg);
1079
1080 p = fragment_shader;
1081 f = glCreateShader(GL_FRAGMENT_SHADER);
1082 glShaderSource(f, 1, &p, NULL);
1083 glCompileShader(f);
1084 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1085 printf("fragment shader info: %s\n", msg);
1086
1087 program = glCreateProgram();
1088 glAttachShader(program, v);
1089 glAttachShader(program, f);
1090 glBindAttribLocation(program, 0, "position");
1091 glBindAttribLocation(program, 1, "texcoord");
1092
1093 glLinkProgram(program);
1094 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1095 printf("info: %s\n", msg);
1096
1097 glUseProgram(program);
1098 ec->proj_uniform = glGetUniformLocation(program, "proj");
1099 ec->tex_uniform = glGetUniformLocation(program, "tex");
1100
1101 vertices[ 0] = 0.0;
1102 vertices[ 1] = 0.0;
1103 vertices[ 2] = 0.0;
1104 vertices[ 3] = 0.0;
1105 vertices[ 4] = 0.0;
1106
1107 vertices[ 5] = 0.0;
1108 vertices[ 6] = 1.0;
1109 vertices[ 7] = 0.0;
1110 vertices[ 8] = 0.0;
1111 vertices[ 9] = 1.0;
1112
1113 vertices[10] = 1.0;
1114 vertices[11] = 0.0;
1115 vertices[12] = 0.0;
1116 vertices[13] = 1.0;
1117 vertices[14] = 0.0;
1118
1119 vertices[15] = 1.0;
1120 vertices[16] = 1.0;
1121 vertices[17] = 0.0;
1122 vertices[18] = 1.0;
1123 vertices[19] = 1.0;
1124
1125 glGenBuffers(1, &ec->vbo);
1126 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1127 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
1128}
1129
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001130static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001131init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001132{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001133 struct wl_event_loop *loop;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001134 EGLint major, minor, count;
1135 EGLConfig config;
1136 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001137
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001138 static const EGLint config_attribs[] = {
1139 EGL_SURFACE_TYPE, 0,
1140 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
1141 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1142 EGL_NONE
1143 };
1144
1145 get_typed_display_mesa =
1146 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
1147 if (get_typed_display_mesa == NULL) {
1148 fprintf(stderr, "eglGetDisplayMESA() not found\n");
1149 return -1;
1150 }
1151
1152 ec->drm_fd = open(udev_device_get_devnode(device), O_RDWR);
1153 if (ec->drm_fd < 0) {
1154 /* Probably permissions error */
1155 fprintf(stderr, "couldn't open %s, skipping\n",
1156 udev_device_get_devnode(device));
1157 return -1;
1158 }
1159
1160 ec->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
1161 (void *) ec->drm_fd);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001162 if (ec->display == NULL) {
1163 fprintf(stderr, "failed to create display\n");
1164 return -1;
1165 }
1166
1167 if (!eglInitialize(ec->display, &major, &minor)) {
1168 fprintf(stderr, "failed to initialize display\n");
1169 return -1;
1170 }
1171
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001172 if (!eglChooseConfig(ec->display, config_attribs, &config, 1, &count) ||
1173 count == 0) {
1174 fprintf(stderr, "eglChooseConfig() failed\n");
1175 return -1;
1176 }
1177
1178 eglBindAPI(EGL_OPENGL_API);
1179 ec->context = eglCreateContext(ec->display, config, EGL_NO_CONTEXT, NULL);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001180 if (ec->context == NULL) {
1181 fprintf(stderr, "failed to create context\n");
1182 return -1;
1183 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001184
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001185 if (!eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE, ec->context)) {
1186 fprintf(stderr, "failed to make context current\n");
1187 return -1;
1188 }
1189
1190 glGenFramebuffers(1, &ec->fbo);
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001191 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1192 glActiveTexture(GL_TEXTURE0);
1193 init_shaders(ec);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001194
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001195 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001196 ec->drm_source =
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001197 wl_event_loop_add_fd(loop, ec->drm_fd,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001198 WL_EVENT_READABLE, on_drm_input, ec);
1199
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001200 return 0;
1201}
1202
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001203static drmModeModeInfo builtin_1024x768 = {
1204 63500, /* clock */
1205 1024, 1072, 1176, 1328, 0,
1206 768, 771, 775, 798, 0,
1207 59920,
1208 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1209 0,
1210 "1024x768"
1211};
1212
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001213static int
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001214create_output_for_connector(struct wlsc_compositor *ec,
1215 drmModeRes *resources,
1216 drmModeConnector *connector)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001217{
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001218 struct wlsc_output *output;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001219 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001220 drmModeModeInfo *mode;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001221 int i, ret;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001222 EGLint handle, stride, attribs[] = {
1223 EGL_WIDTH, 0,
1224 EGL_HEIGHT, 0,
1225 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
1226 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001227 EGL_NONE
1228 };
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001229
1230 output = malloc(sizeof *output);
1231 if (output == NULL)
1232 return -1;
1233
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001234 if (connector->count_modes > 0)
1235 mode = &connector->modes[0];
1236 else
1237 mode = &builtin_1024x768;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001238
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001239 encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001240 if (encoder == NULL) {
1241 fprintf(stderr, "No encoder for connector.\n");
1242 return -1;
1243 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001244
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001245 for (i = 0; i < resources->count_crtcs; i++) {
1246 if (encoder->possible_crtcs & (1 << i))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001247 break;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001248 }
1249 if (i == resources->count_crtcs) {
1250 fprintf(stderr, "No usable crtc for encoder.\n");
1251 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001252 }
1253
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001254 output->compositor = ec;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001255 output->crtc_id = resources->crtcs[i];
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001256 output->connector_id = connector->connector_id;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001257 output->mode = *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001258 output->x = 0;
1259 output->y = 0;
1260 output->width = mode->hdisplay;
1261 output->height = mode->vdisplay;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001262 wlsc_matrix_init(&output->matrix);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001263
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001264 wlsc_matrix_translate(&output->matrix,
1265 -output->x - output->width / 2.0,
1266 -output->y - output->height / 2.0, 0);
1267 wlsc_matrix_scale(&output->matrix,
1268 2.0 / output->width, 2.0 / output->height, 1);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001269
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001270 drmModeFreeEncoder(encoder);
1271
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001272 glGenRenderbuffers(2, output->rbo);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001273 for (i = 0; i < 2; i++) {
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001274 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001275
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001276 attribs[1] = output->width;
1277 attribs[3] = output->height;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001278 output->image[i] = eglCreateDRMImageMESA(ec->display, attribs);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001279 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, output->image[i]);
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001280 eglExportDRMImageMESA(ec->display, output->image[i], NULL, &handle, &stride);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001281
1282 ret = drmModeAddFB(ec->drm_fd, output->width, output->height,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001283 32, 32, stride, handle, &output->fb_id[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001284 if (ret) {
1285 fprintf(stderr, "failed to add fb %d: %m\n", i);
1286 return -1;
1287 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001288 }
1289
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001290 output->current = 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04001291 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1292 GL_COLOR_ATTACHMENT0,
1293 GL_RENDERBUFFER,
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001294 output->rbo[output->current]);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001295 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001296 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001297 &output->connector_id, 1, &output->mode);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001298 if (ret) {
1299 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001301 }
1302
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001303 output->base.interface = &wl_output_interface;
1304 wl_display_add_object(ec->wl_display, &output->base);
1305 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1306 wl_list_insert(ec->output_list.prev, &output->link);
1307
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001308 output->background = background_create(output, option_background);
1309
1310 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001311}
1312
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001313static int
1314create_outputs(struct wlsc_compositor *ec)
1315{
1316 drmModeConnector *connector;
1317 drmModeRes *resources;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001318 int i;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001319
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001320 resources = drmModeGetResources(ec->drm_fd);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001321 if (!resources) {
1322 fprintf(stderr, "drmModeGetResources failed\n");
1323 return -1;
1324 }
1325
1326 for (i = 0; i < resources->count_connectors; i++) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001327 connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001328 if (connector == NULL)
1329 continue;
1330
1331 if (connector->connection == DRM_MODE_CONNECTED &&
1332 (option_connector == 0 ||
1333 connector->connector_id == option_connector))
1334 if (create_output_for_connector(ec, resources, connector) < 0)
1335 return -1;
1336
1337 drmModeFreeConnector(connector);
1338 }
1339
1340 if (wl_list_empty(&ec->output_list)) {
1341 fprintf(stderr, "No currently active connector found.\n");
1342 return -1;
1343 }
1344
1345 drmModeFreeResources(resources);
1346
1347 return 0;
1348}
1349
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001350static const struct wl_interface visual_interface = {
1351 "visual", 1,
1352};
1353
1354static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001355add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001356{
1357 ec->argb_visual.base.interface = &visual_interface;
1358 ec->argb_visual.base.implementation = NULL;
1359 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001360 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001361
1362 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1363 ec->premultiplied_argb_visual.base.implementation = NULL;
1364 wl_display_add_object(ec->wl_display,
1365 &ec->premultiplied_argb_visual.base);
1366 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001367 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001368
1369 ec->rgb_visual.base.interface = &visual_interface;
1370 ec->rgb_visual.base.implementation = NULL;
1371 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001372 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1373}
1374
Ray Strode19ad6a92008-12-19 01:45:41 -05001375static void on_enter_vt(int signal_number, void *data)
1376{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001377 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001378 struct wlsc_output *output;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001379 int ret;
Ray Strode19ad6a92008-12-19 01:45:41 -05001380
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001381 ret = drmSetMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001382 if (ret) {
1383 fprintf(stderr, "failed to set drm master\n");
1384 return;
1385 }
1386
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001387 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001388 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001389
Kristian Høgsberga5db5892010-02-26 10:28:44 -05001390 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001391 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001392 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001393 &output->connector_id, 1, &output->mode);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001394 if (ret)
1395 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1396 output->connector_id);
Ray Strode19ad6a92008-12-19 01:45:41 -05001397 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001398}
1399
1400static void on_leave_vt(int signal_number, void *data)
1401{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001402 struct wlsc_compositor *ec = data;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001403 int ret;
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001404
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001405 ret = drmDropMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001406 if (ret) {
1407 fprintf(stderr, "failed to drop drm master\n");
1408 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}