blob: aeea336361ee44b06f99f62bfab3e442ba5d85c4 [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øgsberg16eb6752008-10-08 22:51:32 -040042#include <GL/gl.h>
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050043#include <EGL/egl.h>
44#include <EGL/eglext.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050047#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050048#include "cairo-util.h"
Kristian Høgsberg82863022010-06-04 21:52:02 -040049#include "compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050050
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040051#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
52
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050053struct wlsc_matrix {
54 GLdouble d[16];
55};
56
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050057struct wl_visual {
58 struct wl_object base;
59};
60
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040061struct wlsc_surface;
62
63struct wlsc_listener {
64 struct wl_list link;
65 void (*func)(struct wlsc_listener *listener,
66 struct wlsc_surface *surface);
67};
68
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050069struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050070 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050071 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050072 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050073 struct wlsc_surface *background;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040074 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050075
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -040076 drmModeModeInfo mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050077 uint32_t crtc_id;
78 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040079
Kristian Høgsberga341fa02010-01-24 18:10:15 -050080 GLuint rbo[2];
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040081 uint32_t fb_id[2];
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -050082 EGLImageKHR image[2];
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040083 uint32_t current;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050084};
85
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050086struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050087 struct wl_object base;
88 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050089 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050090 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050091 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050092
93 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050094 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050095 struct wlsc_surface *pointer_focus;
96 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050097 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040098
99 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500100};
101
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500102struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400103 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500104 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
105
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400106 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400107 EGLContext context;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500108 int drm_fd;
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500109 GLuint fbo;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400110 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500111
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500112 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500113 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500114 struct wl_list surface_list;
115
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400116 struct wl_list surface_destroy_listener_list;
117
Ray Strode966aa112008-12-19 14:28:02 -0500118 struct wl_event_source *term_signal_source;
119
120 /* tty handling state */
121 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500122 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500123
124 struct termios terminal_attributes;
125 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500126 struct wl_event_source *enter_vt_source;
127 struct wl_event_source *leave_vt_source;
128
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500129 struct udev *udev;
130
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500131 /* Repaint state. */
132 struct wl_event_source *timer_source;
133 int repaint_needed;
134 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500135 struct timespec previous_swap;
136 uint32_t current_frame;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400137 struct wl_event_source *drm_source;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500138
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400139 uint32_t modifier_state;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400140};
141
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400142#define MODIFIER_CTRL (1 << 8)
143#define MODIFIER_ALT (1 << 9)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500144
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400145struct wlsc_vector {
146 GLdouble x, y, z;
147};
148
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500149struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500150 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500151 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500152 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400153 GLuint texture;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500154 EGLImageKHR image;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400155 struct wl_map map;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500156 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500157 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500158 struct wlsc_matrix matrix;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400159};
160
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500161static const char *option_background = "background.jpg";
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400162static int option_connector = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500163
164static const GOptionEntry option_entries[] = {
165 { "background", 'b', 0, G_OPTION_ARG_STRING,
166 &option_background, "Background image" },
Kristian Høgsbergf5878fa2009-09-18 17:02:41 -0400167 { "connector", 'c', 0, G_OPTION_ARG_INT,
168 &option_connector, "KMS connector" },
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500169 { NULL }
170};
171
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500172struct screenshooter {
173 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500174 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500175};
176
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500177struct screenshooter_interface {
178 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
179};
180
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500181static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500182screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500183{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500184 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500185 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500186 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500187 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500188 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500189 unsigned char *data;
190 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500191
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500192 i = 0;
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500193 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500194 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500195 data = malloc(output->width * output->height * 4);
196 if (data == NULL) {
197 fprintf(stderr, "couldn't allocate image buffer\n");
198 continue;
199 }
200
201 glReadBuffer(GL_FRONT);
202 glPixelStorei(GL_PACK_ALIGNMENT, 1);
203 glReadPixels(0, 0, output->width, output->height,
204 GL_RGBA, GL_UNSIGNED_BYTE, data);
205
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500206 /* FIXME: We should just use a RGB visual for the frontbuffer. */
207 for (j = 3; j < output->width * output->height * 4; j += 4)
208 data[j] = 0xff;
209
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500210 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500211 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500212 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500213 normal = gdk_pixbuf_flip(pixbuf, FALSE);
214 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
215 gdk_pixbuf_unref(normal);
216 gdk_pixbuf_unref(pixbuf);
217 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500218 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500219}
220
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500221static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500222 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500223};
224
225static const struct wl_interface screenshooter_interface = {
226 "screenshooter", 1,
227 ARRAY_LENGTH(screenshooter_methods),
228 screenshooter_methods,
229};
230
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500231struct screenshooter_interface screenshooter_implementation = {
232 screenshooter_shoot
233};
234
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500235static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500236screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500237{
238 struct screenshooter *shooter;
239
240 shooter = malloc(sizeof *shooter);
241 if (shooter == NULL)
242 return NULL;
243
244 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500245 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500246 shooter->ec = ec;
247
248 return shooter;
249};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500250
251static void
252wlsc_matrix_init(struct wlsc_matrix *matrix)
253{
254 static const struct wlsc_matrix identity = {
255 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
256 };
257
258 memcpy(matrix, &identity, sizeof identity);
259}
260
261static void
262wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
263{
264 struct wlsc_matrix tmp;
265 const GLdouble *row, *column;
266 div_t d;
267 int i, j;
268
269 for (i = 0; i < 16; i++) {
270 tmp.d[i] = 0;
271 d = div(i, 4);
272 row = m->d + d.quot * 4;
273 column = n->d + d.rem;
274 for (j = 0; j < 4; j++)
275 tmp.d[i] += row[j] * column[j * 4];
276 }
277 memcpy(m, &tmp, sizeof tmp);
278}
279
280static void
281wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
282{
283 struct wlsc_matrix translate = {
284 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
285 };
286
287 wlsc_matrix_multiply(matrix, &translate);
288}
289
290static void
291wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
292{
293 struct wlsc_matrix scale = {
294 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
295 };
296
297 wlsc_matrix_multiply(matrix, &scale);
298}
299
300static void
301wlsc_matrix_rotate(struct wlsc_matrix *matrix,
302 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
303{
304 GLdouble c = cos(angle);
305 GLdouble s = sin(angle);
306 struct wlsc_matrix rotate = {
307 { 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 -0500308 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
309 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 -0500310 0, 0, 0, 1 }
311 };
312
313 wlsc_matrix_multiply(matrix, &rotate);
314}
315
316static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500317wlsc_surface_init(struct wlsc_surface *surface,
318 struct wlsc_compositor *compositor, struct wl_visual *visual,
319 int32_t x, int32_t y, int32_t width, int32_t height)
320{
321 glGenTextures(1, &surface->texture);
322 surface->compositor = compositor;
323 surface->map.x = x;
324 surface->map.y = y;
325 surface->map.width = width;
326 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500327 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400328 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500329}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500330
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500331static struct wlsc_surface *
332wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500333 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500334 int x, int y, int width, int height)
335{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500336 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500337 int stride;
338 void *data;
339
340 stride = cairo_image_surface_get_stride(surface);
341 data = cairo_image_surface_get_data(surface);
342
343 es = malloc(sizeof *es);
344 if (es == NULL)
345 return NULL;
346
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500347 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
348 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500349 glBindTexture(GL_TEXTURE_2D, es->texture);
350 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500351 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
352 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500354 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
355 GL_BGRA, GL_UNSIGNED_BYTE, data);
356
Kristian Høgsberg54879822008-11-23 17:07:32 -0500357 return es;
358}
359
360static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400361wlsc_surface_destroy(struct wlsc_surface *surface,
362 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500363{
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500364 struct wlsc_listener *l;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400365
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400366 wl_list_remove(&surface->link);
367 glDeleteTextures(1, &surface->texture);
368 wl_client_remove_surface(surface->base.client, &surface->base);
369
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500370 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400371 l->func(l, surface);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400372
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400373 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500374}
375
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400376static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500377pointer_path(cairo_t *cr, int x, int y)
378{
379 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
380 const int width = 16, height = 16;
381
382 cairo_move_to(cr, x, y);
383 cairo_line_to(cr, x + tx, y + ty);
384 cairo_line_to(cr, x + dx, y + dy);
385 cairo_line_to(cr, x + width - end, y + height);
386 cairo_line_to(cr, x + width, y + height - end);
387 cairo_line_to(cr, x + dy, y + dx);
388 cairo_line_to(cr, x + ty, y + tx);
389 cairo_close_path(cr);
390}
391
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500392static struct wlsc_surface *
393pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500394{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500395 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500396 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500397 cairo_surface_t *surface;
398 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500399
400 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
401 width, height);
402
403 cr = cairo_create(surface);
404 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500405 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500406 cairo_set_source_rgb(cr, 0, 0, 0);
407 cairo_stroke_preserve(cr);
408 cairo_fill(cr);
409 blur_surface(surface, width);
410
411 pointer_path(cr, hotspot_x, hotspot_y);
412 cairo_stroke_preserve(cr);
413 cairo_set_source_rgb(cr, 1, 1, 1);
414 cairo_fill(cr);
415 cairo_destroy(cr);
416
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500417 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500418 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500419 x - hotspot_x,
420 y - hotspot_y,
421 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500422
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500423 cairo_surface_destroy(surface);
424
Kristian Høgsberg54879822008-11-23 17:07:32 -0500425 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500426}
427
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500428static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500429background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500430{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500431 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500432 GdkPixbuf *pixbuf;
433 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500434 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500435 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500436
437 background = malloc(sizeof *background);
438 if (background == NULL)
439 return NULL;
440
441 g_type_init();
442
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500443 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500444 output->width,
445 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500446 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500447 if (error != NULL) {
448 free(background);
449 return NULL;
450 }
451
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500452 data = gdk_pixbuf_get_pixels(pixbuf);
453
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500454 wlsc_surface_init(background, output->compositor,
455 &output->compositor->rgb_visual,
456 output->x, output->y, output->width, output->height);
457
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458 glBindTexture(GL_TEXTURE_2D, background->texture);
459 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500461 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
462 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500463
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500464 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500465 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500466 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500467 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500468
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500469 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
470 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500471 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500473 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500474}
475
476static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500477wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500478{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500479 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500480 GLint vertices[12];
481 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
482 GLuint indices[4] = { 0, 1, 2, 3 };
483
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500484 vertices[0] = es->map.x;
485 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500486 vertices[2] = 0;
487
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500488 vertices[3] = es->map.x;
489 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500490 vertices[5] = 0;
491
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500492 vertices[6] = es->map.x + es->map.width;
493 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500494 vertices[8] = 0;
495
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500496 vertices[9] = es->map.x + es->map.width;
497 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500498 vertices[11] = 0;
499
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500500 if (es->visual == &ec->argb_visual) {
501 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
502 glEnable(GL_BLEND);
503 } else if (es->visual == &ec->premultiplied_argb_visual) {
504 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
505 glEnable(GL_BLEND);
506 } else {
507 glDisable(GL_BLEND);
508 }
509
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500510 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400511 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500512 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500513 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500514 glEnableClientState(GL_VERTEX_ARRAY);
515 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
516 glVertexPointer(3, GL_INT, 0, vertices);
517 glTexCoordPointer(2, GL_INT, 0, tex_coords);
518 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500519 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500520}
521
522static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400523wlsc_surface_raise(struct wlsc_surface *surface)
524{
525 struct wlsc_compositor *compositor = surface->compositor;
526
527 wl_list_remove(&surface->link);
528 wl_list_insert(compositor->surface_list.prev, &surface->link);
529}
530
531static void
532wlsc_surface_lower(struct wlsc_surface *surface)
533{
534 struct wlsc_compositor *compositor = surface->compositor;
535
536 wl_list_remove(&surface->link);
537 wl_list_insert(&compositor->surface_list, &surface->link);
538}
539
540static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500541wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
542{
543 v1->x += v2->x;
544 v1->y += v2->y;
545 v1->z += v2->z;
546}
547
548static void
549wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
550{
551 v1->x -= v2->x;
552 v1->y -= v2->y;
553 v1->z -= v2->z;
554}
555
556static void
557wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
558{
559 v1->x *= s;
560 v1->y *= s;
561 v1->z *= s;
562}
563
564static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400565page_flip_handler(int fd, unsigned int frame,
566 unsigned int sec, unsigned int usec, void *data)
567{
568 struct wlsc_output *output = data;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400569 struct wlsc_compositor *compositor = output->compositor;
570 uint32_t msecs;
571
572 msecs = sec * 1000 + usec / 1000;
573 wl_display_post_frame(compositor->wl_display,
574 &compositor->base,
575 compositor->current_frame, msecs);
576
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400577 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400578 compositor->repaint_on_timeout = 1;
579
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400580 compositor->current_frame++;
581}
582
583static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500584repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400585{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500586 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500587 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500588 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500589 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500590
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500591 glViewport(0, 0, output->width, output->height);
592 glMatrixMode(GL_PROJECTION);
593 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500594 glFrustum(-output->width / s, output->width / s,
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500595 -output->height / s, output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500596 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500597 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400598 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500599
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500600 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500601
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500602 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500603 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500604 else
605 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400606
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500607 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500608 wlsc_surface_draw(es);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400609
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500610 wl_list_for_each(eid, &ec->input_device_list, link)
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500611 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500612
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400613 output->current ^= 1;
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500614
615 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
616 GL_COLOR_ATTACHMENT0_EXT,
617 GL_RENDERBUFFER_EXT,
618 output->rbo[output->current]);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500619 drmModePageFlip(ec->drm_fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500620 output->fb_id[output->current ^ 1],
621 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500622}
623
624static void
625repaint(void *data)
626{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500627 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500628 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500629
630 if (!ec->repaint_needed) {
631 ec->repaint_on_timeout = 0;
632 return;
633 }
634
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500635 wl_list_for_each(output, &ec->output_list, link)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500636 repaint_output(output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500637
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500638 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400639}
640
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500641static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400642wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400643{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400644 struct wlsc_output *output;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400645
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400646 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400647 if (compositor->repaint_on_timeout)
648 return;
649
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500650 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500651 drmModePageFlip(compositor->drm_fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500652 output->fb_id[output->current ^ 1],
653 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400654}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500655
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500656static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500657surface_destroy(struct wl_client *client,
658 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400659{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500660 struct wlsc_surface *es = (struct wlsc_surface *) surface;
661 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500662
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500663 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400664
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400665 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400666}
667
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500668static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500669surface_attach(struct wl_client *client,
670 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500671 uint32_t width, uint32_t height, uint32_t stride,
672 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400673{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500674 struct wlsc_surface *es = (struct wlsc_surface *) surface;
675 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500676 EGLint attribs[] = {
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400677 EGL_WIDTH, 0,
678 EGL_HEIGHT, 0,
679 EGL_IMAGE_NAME_MESA, 0,
680 EGL_IMAGE_STRIDE_MESA, 0,
681 EGL_IMAGE_FORMAT_MESA, EGL_FORMAT_RGBA_8888_KHR,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500682 EGL_NONE
683 };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400684
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500685 es->width = width;
686 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400687
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500688 if (visual == &ec->argb_visual.base)
689 es->visual = &ec->argb_visual;
690 else if (visual == &ec->premultiplied_argb_visual.base)
691 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500692 else if (visual == &ec->rgb_visual.base)
693 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500694 else
695 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400696
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500697 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400698 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500699 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
700 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
701 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500702
703 if (es->image)
704 eglDestroyImageKHR(ec->display, es->image);
705
706 attribs[1] = width;
707 attribs[3] = height;
708 attribs[5] = name;
709 attribs[7] = stride / 4;
710
711 es->image = eglCreateImageKHR(ec->display, ec->context,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -0400712 EGL_DRM_IMAGE_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -0500713 NULL, attribs);
714 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400715
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400716}
717
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500718static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500719surface_map(struct wl_client *client,
720 struct wl_surface *surface,
721 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400722{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500723 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400724
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500725 es->map.x = x;
726 es->map.y = y;
727 es->map.width = width;
728 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400729}
730
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500731static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500732surface_copy(struct wl_client *client,
733 struct wl_surface *surface,
734 int32_t dst_x, int32_t dst_y,
735 uint32_t name, uint32_t stride,
736 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500737{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500738}
739
740static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500741surface_damage(struct wl_client *client,
742 struct wl_surface *surface,
743 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500744{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500745 /* FIXME: This need to take a damage region, of course. */
746}
747
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500748const static struct wl_surface_interface surface_interface = {
749 surface_destroy,
750 surface_attach,
751 surface_map,
752 surface_copy,
753 surface_damage
754};
755
756static void
757compositor_create_surface(struct wl_client *client,
758 struct wl_compositor *compositor, uint32_t id)
759{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500760 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400761 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500762
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400763 surface = malloc(sizeof *surface);
764 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500765 /* FIXME: Send OOM event. */
766 return;
767
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400768 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500769
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400770 wl_list_insert(ec->surface_list.prev, &surface->link);
771 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500772 &surface_interface, id);
773}
774
775static void
776compositor_commit(struct wl_client *client,
777 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500778{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500779 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500780
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400781 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500782 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500783}
784
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500785const static struct wl_compositor_interface compositor_interface = {
786 compositor_create_surface,
787 compositor_commit
788};
789
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500790static void
791wlsc_surface_transform(struct wlsc_surface *surface,
792 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
793{
794 /* Transform to surface coordinates. */
795 *sx = (x - surface->map.x) * surface->width / surface->map.width;
796 *sy = (y - surface->map.y) * surface->height / surface->map.height;
797}
798
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500799static void
800wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
801 struct wlsc_surface *surface)
802{
803 if (device->keyboard_focus == surface)
804 return;
805
806 if (device->keyboard_focus &&
807 (!surface || device->keyboard_focus->base.client != surface->base.client))
808 wl_surface_post_event(&device->keyboard_focus->base,
809 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500810 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500811
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500812 if (surface)
813 wl_surface_post_event(&surface->base,
814 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500815 WL_INPUT_KEYBOARD_FOCUS,
816 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500817
818 device->keyboard_focus = surface;
819}
820
821static void
822wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
823 struct wlsc_surface *surface)
824{
825 if (device->pointer_focus == surface)
826 return;
827
828 if (device->pointer_focus &&
829 (!surface || device->pointer_focus->base.client != surface->base.client))
830 wl_surface_post_event(&device->pointer_focus->base,
831 &device->base,
832 WL_INPUT_POINTER_FOCUS, NULL);
833 if (surface)
834 wl_surface_post_event(&surface->base,
835 &device->base,
836 WL_INPUT_POINTER_FOCUS, &surface->base);
837
838 device->pointer_focus = surface;
839}
840
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500841static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500842pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500843{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500844 struct wlsc_compositor *ec = device->ec;
845 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500846
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500847 if (device->grab > 0) {
848 wlsc_surface_transform(device->grab_surface,
849 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500850 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500851 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500852
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500853 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500854 if (es->map.x <= device->x &&
855 device->x < es->map.x + es->map.width &&
856 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500857 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500858 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500859 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500860 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500861
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500862 return NULL;
863}
864
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500865void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500866notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500867{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500868 struct wlsc_surface *es;
869 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500870 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500871 const int hotspot_x = 16, hotspot_y = 16;
872 int32_t sx, sy;
873
Ray Strodee96dcb82008-12-20 02:00:49 -0500874 if (!ec->vt_active)
875 return;
876
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500877 /* FIXME: We need some multi head love here. */
878 output = container_of(ec->output_list.next, struct wlsc_output, link);
879 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500880 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500881 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500882 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500883 if (x >= output->x + output->width)
884 x = output->x + output->width - 1;
885 if (y >= output->y + output->height)
886 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500887
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500888 device->x = x;
889 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500890 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500891
892 wlsc_input_device_set_pointer_focus(device, es);
893
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500894 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500895 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500896 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500897
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500898 device->sprite->map.x = x - hotspot_x;
899 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500900
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400901 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500902}
903
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500904void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500905notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500906 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500907{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400908 struct wlsc_surface *surface;
909 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500910 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500911
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400912 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500913 return;
914
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400915 surface = pick_surface(device, &sx, &sy);
916 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500917 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400918 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500919 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400920 device->grab_surface = surface;
921 wlsc_input_device_set_keyboard_focus(device,
922 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500923 } else {
924 device->grab--;
925 }
926
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500927 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400928 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500929 WL_INPUT_BUTTON, button, state,
930 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500931
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400932 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500933 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500934}
935
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500936static void on_term_signal(int signal_number, void *data);
937
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500938void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500939notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500940 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500941{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400942 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500943 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400944 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500945
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400946 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500947 return;
Ray Strodee96dcb82008-12-20 02:00:49 -0500948
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400949 switch (key | compositor->modifier_state) {
950 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400951 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500952 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500953 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500954
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400955 switch (key) {
956 case KEY_LEFTCTRL:
957 case KEY_RIGHTCTRL:
958 modifier = MODIFIER_CTRL;
959 break;
960
961 case KEY_LEFTALT:
962 case KEY_RIGHTALT:
963 modifier = MODIFIER_ALT;
964 break;
965
966 default:
967 modifier = 0;
968 break;
969 }
970
971 if (state)
972 compositor->modifier_state |= modifier;
973 else
974 compositor->modifier_state &= ~modifier;
975
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500976 end = device->keys.data + device->keys.size;
977 for (k = device->keys.data; k < end; k++) {
978 if (*k == key)
979 *k = *--end;
980 }
981 device->keys.size = (void *) end - device->keys.data;
982 if (state) {
983 k = wl_array_add(&device->keys, sizeof *k);
984 *k = key;
985 }
Ray Strodee96dcb82008-12-20 02:00:49 -0500986
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500987 if (device->keyboard_focus != NULL)
988 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500989 &device->base,
990 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500991}
992
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500993struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500994evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500995 struct wl_display *display, const char *path);
996
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400997static void
998handle_surface_destroy(struct wlsc_listener *listener,
999 struct wlsc_surface *surface)
1000{
1001 struct wlsc_input_device *device =
1002 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001003 struct wlsc_surface *focus;
1004 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001005
1006 if (device->grab_surface == surface) {
1007 device->grab_surface = NULL;
1008 device->grab = 0;
1009 }
1010 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001011 wlsc_input_device_set_keyboard_focus(device, NULL);
1012 if (device->pointer_focus == surface) {
1013 focus = pick_surface(device, &sx, &sy);
1014 wlsc_input_device_set_pointer_focus(device, focus);
1015 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
1016 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001017}
1018
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001019static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001020create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001021{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001022 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001023
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001024 device = malloc(sizeof *device);
1025 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001026 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001027
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001028 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001029 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001030 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001031 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001032 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001033 device->x = 100;
1034 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001035 device->ec = ec;
1036
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001037 device->listener.func = handle_surface_destroy;
1038 wl_list_insert(ec->surface_destroy_listener_list.prev,
1039 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001040 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001041
1042 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001043}
1044
1045void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001046wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001047{
1048 *x = device->x;
1049 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001050}
1051
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001052static void
1053post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001054{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001055 struct wlsc_output *output =
1056 container_of(global, struct wlsc_output, base);
1057
1058 wl_client_post_event(client, global,
1059 WL_OUTPUT_GEOMETRY,
1060 output->width, output->height);
1061}
1062
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001063static void
1064on_drm_input(int fd, uint32_t mask, void *data)
1065{
1066 drmEventContext evctx;
1067
1068 memset(&evctx, 0, sizeof evctx);
1069 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1070 evctx.page_flip_handler = page_flip_handler;
1071 drmHandleEvent(fd, &evctx);
1072}
1073
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001074static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001075init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001076{
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001077 struct wl_event_loop *loop;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001078 EGLint major, minor, count;
1079 EGLConfig config;
1080 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001081
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001082 static const EGLint config_attribs[] = {
1083 EGL_SURFACE_TYPE, 0,
1084 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
1085 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1086 EGL_NONE
1087 };
1088
1089 get_typed_display_mesa =
1090 (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
1091 if (get_typed_display_mesa == NULL) {
1092 fprintf(stderr, "eglGetDisplayMESA() not found\n");
1093 return -1;
1094 }
1095
1096 ec->drm_fd = open(udev_device_get_devnode(device), O_RDWR);
1097 if (ec->drm_fd < 0) {
1098 /* Probably permissions error */
1099 fprintf(stderr, "couldn't open %s, skipping\n",
1100 udev_device_get_devnode(device));
1101 return -1;
1102 }
1103
1104 ec->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
1105 (void *) ec->drm_fd);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001106 if (ec->display == NULL) {
1107 fprintf(stderr, "failed to create display\n");
1108 return -1;
1109 }
1110
1111 if (!eglInitialize(ec->display, &major, &minor)) {
1112 fprintf(stderr, "failed to initialize display\n");
1113 return -1;
1114 }
1115
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001116 if (!eglChooseConfig(ec->display, config_attribs, &config, 1, &count) ||
1117 count == 0) {
1118 fprintf(stderr, "eglChooseConfig() failed\n");
1119 return -1;
1120 }
1121
1122 eglBindAPI(EGL_OPENGL_API);
1123 ec->context = eglCreateContext(ec->display, config, EGL_NO_CONTEXT, NULL);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001124 if (ec->context == NULL) {
1125 fprintf(stderr, "failed to create context\n");
1126 return -1;
1127 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001128
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001129 if (!eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE, ec->context)) {
1130 fprintf(stderr, "failed to make context current\n");
1131 return -1;
1132 }
1133
1134 glGenFramebuffers(1, &ec->fbo);
1135 glBindFramebuffer(GL_FRAMEBUFFER_EXT, ec->fbo);
1136
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001137 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001138 ec->drm_source =
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001139 wl_event_loop_add_fd(loop, ec->drm_fd,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001140 WL_EVENT_READABLE, on_drm_input, ec);
1141
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001142 return 0;
1143}
1144
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001145static drmModeModeInfo builtin_1024x768 = {
1146 63500, /* clock */
1147 1024, 1072, 1176, 1328, 0,
1148 768, 771, 775, 798, 0,
1149 59920,
1150 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1151 0,
1152 "1024x768"
1153};
1154
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001155static int
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001156create_output_for_connector(struct wlsc_compositor *ec,
1157 drmModeRes *resources,
1158 drmModeConnector *connector)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001159{
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001160 struct wlsc_output *output;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001161 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001162 drmModeModeInfo *mode;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001163 int i, ret;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001164 EGLint handle, stride, attribs[] = {
1165 EGL_WIDTH, 0,
1166 EGL_HEIGHT, 0,
1167 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
1168 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001169 EGL_NONE
1170 };
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001171
1172 output = malloc(sizeof *output);
1173 if (output == NULL)
1174 return -1;
1175
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001176 if (connector->count_modes > 0)
1177 mode = &connector->modes[0];
1178 else
1179 mode = &builtin_1024x768;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001180
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001181 encoder = drmModeGetEncoder(ec->drm_fd, connector->encoders[0]);
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001182 if (encoder == NULL) {
1183 fprintf(stderr, "No encoder for connector.\n");
1184 return -1;
1185 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001186
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001187 for (i = 0; i < resources->count_crtcs; i++) {
1188 if (encoder->possible_crtcs & (1 << i))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001189 break;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001190 }
1191 if (i == resources->count_crtcs) {
1192 fprintf(stderr, "No usable crtc for encoder.\n");
1193 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001194 }
1195
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001196 output->compositor = ec;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001197 output->crtc_id = resources->crtcs[i];
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001198 output->connector_id = connector->connector_id;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001199 output->mode = *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001200 output->x = 0;
1201 output->y = 0;
1202 output->width = mode->hdisplay;
1203 output->height = mode->vdisplay;
1204
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001205 printf("using crtc %d, connector %d and encoder %d, mode %s\n",
1206 output->crtc_id,
1207 output->connector_id,
1208 encoder->encoder_id,
1209 mode->name);
1210
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001211 drmModeFreeEncoder(encoder);
1212
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001213 glGenRenderbuffers(2, output->rbo);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001214 for (i = 0; i < 2; i++) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001215 glBindRenderbuffer(GL_RENDERBUFFER_EXT, output->rbo[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001216
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001217 attribs[1] = output->width;
1218 attribs[3] = output->height;
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001219 output->image[i] = eglCreateDRMImageMESA(ec->display, attribs);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001220 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, output->image[i]);
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001221 eglExportDRMImageMESA(ec->display, output->image[i], NULL, &handle, &stride);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001222
1223 ret = drmModeAddFB(ec->drm_fd, output->width, output->height,
Kristian Høgsberga2ee6752010-06-04 22:14:28 -04001224 32, 32, stride, handle, &output->fb_id[i]);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001225 if (ret) {
1226 fprintf(stderr, "failed to add fb %d: %m\n", i);
1227 return -1;
1228 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001229 }
1230
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001231 output->current = 0;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001232 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT,
1233 GL_COLOR_ATTACHMENT0_EXT,
1234 GL_RENDERBUFFER_EXT,
1235 output->rbo[output->current]);
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001236 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001237 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001238 &output->connector_id, 1, &output->mode);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001239 if (ret) {
1240 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001241 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001242 }
1243
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001244 output->base.interface = &wl_output_interface;
1245 wl_display_add_object(ec->wl_display, &output->base);
1246 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1247 wl_list_insert(ec->output_list.prev, &output->link);
1248
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001249 output->background = background_create(output, option_background);
1250
1251 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001252}
1253
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001254static int
1255create_outputs(struct wlsc_compositor *ec)
1256{
1257 drmModeConnector *connector;
1258 drmModeRes *resources;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001259 int i;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001260
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001261 resources = drmModeGetResources(ec->drm_fd);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001262 if (!resources) {
1263 fprintf(stderr, "drmModeGetResources failed\n");
1264 return -1;
1265 }
1266
1267 for (i = 0; i < resources->count_connectors; i++) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001268 connector = drmModeGetConnector(ec->drm_fd, resources->connectors[i]);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001269 if (connector == NULL)
1270 continue;
1271
1272 if (connector->connection == DRM_MODE_CONNECTED &&
1273 (option_connector == 0 ||
1274 connector->connector_id == option_connector))
1275 if (create_output_for_connector(ec, resources, connector) < 0)
1276 return -1;
1277
1278 drmModeFreeConnector(connector);
1279 }
1280
1281 if (wl_list_empty(&ec->output_list)) {
1282 fprintf(stderr, "No currently active connector found.\n");
1283 return -1;
1284 }
1285
1286 drmModeFreeResources(resources);
1287
1288 return 0;
1289}
1290
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001291static const struct wl_interface visual_interface = {
1292 "visual", 1,
1293};
1294
1295static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001296add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001297{
1298 ec->argb_visual.base.interface = &visual_interface;
1299 ec->argb_visual.base.implementation = NULL;
1300 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001301 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001302
1303 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1304 ec->premultiplied_argb_visual.base.implementation = NULL;
1305 wl_display_add_object(ec->wl_display,
1306 &ec->premultiplied_argb_visual.base);
1307 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001308 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001309
1310 ec->rgb_visual.base.interface = &visual_interface;
1311 ec->rgb_visual.base.implementation = NULL;
1312 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001313 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1314}
1315
Ray Strode19ad6a92008-12-19 01:45:41 -05001316static void on_enter_vt(int signal_number, void *data)
1317{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001318 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001319 struct wlsc_output *output;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001320 int ret;
Ray Strode19ad6a92008-12-19 01:45:41 -05001321
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001322 ret = drmSetMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001323 if (ret) {
1324 fprintf(stderr, "failed to set drm master\n");
1325 return;
1326 }
1327
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001328 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001329 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001330
Kristian Høgsberga5db5892010-02-26 10:28:44 -05001331 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001332 ret = drmModeSetCrtc(ec->drm_fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001333 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001334 &output->connector_id, 1, &output->mode);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001335 if (ret)
1336 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1337 output->connector_id);
Ray Strode19ad6a92008-12-19 01:45:41 -05001338 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001339}
1340
1341static void on_leave_vt(int signal_number, void *data)
1342{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001343 struct wlsc_compositor *ec = data;
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001344 int ret;
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001345
Kristian Høgsberg3afd45d2010-03-03 09:54:29 -05001346 ret = drmDropMaster(ec->drm_fd);
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001347 if (ret) {
1348 fprintf(stderr, "failed to drop drm master\n");
1349 return;
1350 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001351
1352 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001353 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001354}
1355
Ray Strode966aa112008-12-19 14:28:02 -05001356static void
1357on_tty_input(int fd, uint32_t mask, void *data)
1358{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001359 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001360
1361 /* Ignore input to tty. We get keyboard events from evdev
1362 */
1363 tcflush(ec->tty_fd, TCIFLUSH);
1364}
1365
1366static void on_term_signal(int signal_number, void *data)
1367{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001368 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001369
1370 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1371 fprintf(stderr, "could not restore terminal to canonical mode\n");
1372
1373 exit(0);
1374}
1375
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001376static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001377{
1378 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001379 struct vt_mode mode = { 0 };
1380
1381 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1382 if (ec->tty_fd <= 0) {
1383 fprintf(stderr, "failed to open active tty: %m\n");
1384 return -1;
1385 }
Ray Strode966aa112008-12-19 14:28:02 -05001386
1387 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1388 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001389 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001390 }
1391
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001392 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001393 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001394 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001395
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001396 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001397 raw_attributes.c_oflag |= OPOST | OCRNL;
1398
1399 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1400 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1401
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001402 ec->term_signal_source =
1403 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001404
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001405 ec->tty_input_source =
1406 wl_event_loop_add_fd(loop, ec->tty_fd,
1407 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001408
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001409 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001410 mode.mode = VT_PROCESS;
1411 mode.relsig = SIGUSR1;
1412 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001413 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001414 fprintf(stderr, "failed to take control of vt handling\n");
1415 }
1416
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001417 ec->leave_vt_source =
1418 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1419 ec->enter_vt_source =
1420 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001421
1422 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001423}
1424
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001425static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001426init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001427{
1428 struct udev_enumerate *e;
1429 struct udev_list_entry *entry;
1430 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001431 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001432 struct wlsc_input_device *input_device;
1433
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001434 ec->udev = udev_new();
1435 if (ec->udev == NULL) {
1436 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001437 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001438 }
1439
1440 input_device = create_input_device(ec);
1441
1442 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001443 udev_enumerate_add_match_subsystem(e, "input");
1444 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001445 udev_enumerate_scan_devices(e);
1446 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1447 path = udev_list_entry_get_name(entry);
1448 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001449 evdev_input_device_create(input_device, ec->wl_display,
1450 udev_device_get_devnode(device));
1451 }
1452 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001453
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001454 e = udev_enumerate_new(ec->udev);
1455 udev_enumerate_add_match_subsystem(e, "drm");
1456 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1457 udev_enumerate_scan_devices(e);
1458 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1459 path = udev_list_entry_get_name(entry);
1460 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001461 if (init_egl(ec, device) < 0) {
1462 fprintf(stderr, "failed to initialize egl\n");
1463 return -1;
1464 }
1465 if (create_outputs(ec) < 0) {
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001466 fprintf(stderr, "failed to create output for %s\n", path);
1467 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001468 }
1469 }
1470 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001471
1472 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001473 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001474 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1475
1476 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001477}
1478
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001479static struct wlsc_compositor *
1480wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001481{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001482 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001483 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001484 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001485
1486 ec = malloc(sizeof *ec);
1487 if (ec == NULL)
1488 return NULL;
1489
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001490 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001491 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001492
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001493 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001494
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001495 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001496
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001497 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001498 wl_list_init(&ec->input_device_list);
1499 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001500 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001501 if (init_libudev(ec) < 0) {
1502 fprintf(stderr, "failed to initialize devices\n");
1503 return NULL;
1504 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001505
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001506 shooter = screenshooter_create(ec);
1507 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001508 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001509
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001510 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001511
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001512 setup_tty(ec, loop);
1513
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001514 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001515 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001516
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001517 return ec;
1518}
1519
1520/* The plan here is to generate a random anonymous socket name and
1521 * advertise that through a service on the session dbus.
1522 */
1523static const char socket_name[] = "\0wayland";
1524
1525int main(int argc, char *argv[])
1526{
1527 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001528 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001529 GError *error = NULL;
1530 GOptionContext *context;
1531
1532 context = g_option_context_new(NULL);
1533 g_option_context_add_main_entries(context, option_entries, "Wayland");
1534 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1535 fprintf(stderr, "option parsing failed: %s\n", error->message);
1536 exit(EXIT_FAILURE);
1537 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001538
1539 display = wl_display_create();
1540
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001541 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001542 if (ec == NULL) {
1543 fprintf(stderr, "failed to create compositor\n");
1544 exit(EXIT_FAILURE);
1545 }
1546
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001547 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001548 fprintf(stderr, "failed to add socket: %m\n");
1549 exit(EXIT_FAILURE);
1550 }
1551
1552 wl_display_run(display);
1553
1554 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001555}