blob: 74b7f0a8c3d770360eff7cdcceddb59e29f0336c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050023#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050024#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025#include <sys/ioctl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <fcntl.h>
27#include <unistd.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050028#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050029#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050030#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050031#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050032#include <linux/vt.h>
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040033#include <xf86drm.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050034#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040036
Kristian Høgsberg890bc052008-12-30 14:31:33 -050037#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
38#include <libudev.h>
39
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -040040#define GL_GLEXT_PROTOTYPES
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <GL/gl.h>
42#include <eagle.h>
43
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050045#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050047#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050048
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040049#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
50
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050051struct wlsc_matrix {
52 GLdouble d[16];
53};
54
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050055struct wl_visual {
56 struct wl_object base;
57};
58
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040059struct wlsc_surface;
60
61struct wlsc_listener {
62 struct wl_list link;
63 void (*func)(struct wlsc_listener *listener,
64 struct wlsc_surface *surface);
65};
66
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050067struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050068 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050069 struct wl_list link;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -050070 struct wlsc_compositor *compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050071 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050072 EGLSurface surface;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -040073 int32_t x, y, width, height;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050074
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -040075 drmModeModeInfo mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050076 uint32_t crtc_id;
77 uint32_t connector_id;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -040078
79 uint32_t fb_id[2];
80 uint32_t current;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050081};
82
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050083struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050084 struct wl_object base;
85 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050086 struct wlsc_compositor *ec;
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -050087 struct wlsc_surface *sprite;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050088 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050089
90 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050091 struct wlsc_surface *grab_surface;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -050092 struct wlsc_surface *pointer_focus;
93 struct wlsc_surface *keyboard_focus;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050094 struct wl_array keys;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -040095
96 struct wlsc_listener listener;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050097};
98
Kristian Høgsberg8e438622009-01-26 23:07:00 -050099struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400100 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500101 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
102
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400103 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400104 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500105 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400106 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500107
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500108 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500109 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500110 struct wl_list surface_list;
111
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400112 struct wl_list surface_destroy_listener_list;
113
Ray Strode966aa112008-12-19 14:28:02 -0500114 struct wl_event_source *term_signal_source;
115
116 /* tty handling state */
117 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500118 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500119
120 struct termios terminal_attributes;
121 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500122 struct wl_event_source *enter_vt_source;
123 struct wl_event_source *leave_vt_source;
124
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500125 struct udev *udev;
126
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500127 /* Repaint state. */
128 struct wl_event_source *timer_source;
129 int repaint_needed;
130 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500131 struct timespec previous_swap;
132 uint32_t current_frame;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400133 struct wl_event_source *drm_source;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500134
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400135 uint32_t modifier_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 struct wl_list animate_list;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400137};
138
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -0400139#define MODIFIER_CTRL (1 << 8)
140#define MODIFIER_ALT (1 << 9)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500141
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400142struct wlsc_vector {
143 GLdouble x, y, z;
144};
145
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500146struct wlsc_animate {
147 struct wl_list link;
148 void (*animate)(struct wlsc_animate *animate,
149 struct wlsc_compositor *compositor,
150 uint32_t frame, uint32_t msecs);
151};
152
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500153struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500154 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500155 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500156 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400157 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400158 struct wl_map map;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500159 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500160 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500161 struct wlsc_matrix matrix;
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;
196 output = container_of(ec->output_list.next, struct wlsc_output, link);
197 while (&output->link != &ec->output_list) {
198 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500199 data = malloc(output->width * output->height * 4);
200 if (data == NULL) {
201 fprintf(stderr, "couldn't allocate image buffer\n");
202 continue;
203 }
204
205 glReadBuffer(GL_FRONT);
206 glPixelStorei(GL_PACK_ALIGNMENT, 1);
207 glReadPixels(0, 0, output->width, output->height,
208 GL_RGBA, GL_UNSIGNED_BYTE, data);
209
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500210 /* FIXME: We should just use a RGB visual for the frontbuffer. */
211 for (j = 3; j < output->width * output->height * 4; j += 4)
212 data[j] = 0xff;
213
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500214 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500215 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500216 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500217 normal = gdk_pixbuf_flip(pixbuf, FALSE);
218 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
219 gdk_pixbuf_unref(normal);
220 gdk_pixbuf_unref(pixbuf);
221 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500222
223 output = container_of(output->link.next,
224 struct wlsc_output, link);
225 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500226}
227
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500228static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500229 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500230};
231
232static const struct wl_interface screenshooter_interface = {
233 "screenshooter", 1,
234 ARRAY_LENGTH(screenshooter_methods),
235 screenshooter_methods,
236};
237
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500238struct screenshooter_interface screenshooter_implementation = {
239 screenshooter_shoot
240};
241
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500242static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500243screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500244{
245 struct screenshooter *shooter;
246
247 shooter = malloc(sizeof *shooter);
248 if (shooter == NULL)
249 return NULL;
250
251 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500252 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500253 shooter->ec = ec;
254
255 return shooter;
256};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500257
258static void
259wlsc_matrix_init(struct wlsc_matrix *matrix)
260{
261 static const struct wlsc_matrix identity = {
262 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
263 };
264
265 memcpy(matrix, &identity, sizeof identity);
266}
267
268static void
269wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
270{
271 struct wlsc_matrix tmp;
272 const GLdouble *row, *column;
273 div_t d;
274 int i, j;
275
276 for (i = 0; i < 16; i++) {
277 tmp.d[i] = 0;
278 d = div(i, 4);
279 row = m->d + d.quot * 4;
280 column = n->d + d.rem;
281 for (j = 0; j < 4; j++)
282 tmp.d[i] += row[j] * column[j * 4];
283 }
284 memcpy(m, &tmp, sizeof tmp);
285}
286
287static void
288wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
289{
290 struct wlsc_matrix translate = {
291 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
292 };
293
294 wlsc_matrix_multiply(matrix, &translate);
295}
296
297static void
298wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
299{
300 struct wlsc_matrix scale = {
301 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
302 };
303
304 wlsc_matrix_multiply(matrix, &scale);
305}
306
307static void
308wlsc_matrix_rotate(struct wlsc_matrix *matrix,
309 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
310{
311 GLdouble c = cos(angle);
312 GLdouble s = sin(angle);
313 struct wlsc_matrix rotate = {
314 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
315 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
316 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
317 0, 0, 0, 1 }
318 };
319
320 wlsc_matrix_multiply(matrix, &rotate);
321}
322
323static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500324wlsc_surface_init(struct wlsc_surface *surface,
325 struct wlsc_compositor *compositor, struct wl_visual *visual,
326 int32_t x, int32_t y, int32_t width, int32_t height)
327{
328 glGenTextures(1, &surface->texture);
329 surface->compositor = compositor;
330 surface->map.x = x;
331 surface->map.y = y;
332 surface->map.width = width;
333 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500334 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400335 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500336}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500337
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500338static struct wlsc_surface *
339wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500340 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500341 int x, int y, int width, int height)
342{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500343 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500344 int stride;
345 void *data;
346
347 stride = cairo_image_surface_get_stride(surface);
348 data = cairo_image_surface_get_data(surface);
349
350 es = malloc(sizeof *es);
351 if (es == NULL)
352 return NULL;
353
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500354 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
355 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500356 glBindTexture(GL_TEXTURE_2D, es->texture);
357 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
360 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500361 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
362 GL_BGRA, GL_UNSIGNED_BYTE, data);
363
Kristian Høgsberg54879822008-11-23 17:07:32 -0500364 return es;
365}
366
367static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400368wlsc_surface_destroy(struct wlsc_surface *surface,
369 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500370{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400371 struct wlsc_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400372
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400373 wl_list_remove(&surface->link);
374 glDeleteTextures(1, &surface->texture);
375 wl_client_remove_surface(surface->base.client, &surface->base);
376
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400377 l = container_of(compositor->surface_destroy_listener_list.next,
378 struct wlsc_listener, link);
379 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400380 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400381 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400382 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400383 }
384
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400385 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500386}
387
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400388static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500389pointer_path(cairo_t *cr, int x, int y)
390{
391 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
392 const int width = 16, height = 16;
393
394 cairo_move_to(cr, x, y);
395 cairo_line_to(cr, x + tx, y + ty);
396 cairo_line_to(cr, x + dx, y + dy);
397 cairo_line_to(cr, x + width - end, y + height);
398 cairo_line_to(cr, x + width, y + height - end);
399 cairo_line_to(cr, x + dy, y + dx);
400 cairo_line_to(cr, x + ty, y + tx);
401 cairo_close_path(cr);
402}
403
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500404static struct wlsc_surface *
405pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500406{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500407 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500408 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500409 cairo_surface_t *surface;
410 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500411
412 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
413 width, height);
414
415 cr = cairo_create(surface);
416 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500417 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418 cairo_set_source_rgb(cr, 0, 0, 0);
419 cairo_stroke_preserve(cr);
420 cairo_fill(cr);
421 blur_surface(surface, width);
422
423 pointer_path(cr, hotspot_x, hotspot_y);
424 cairo_stroke_preserve(cr);
425 cairo_set_source_rgb(cr, 1, 1, 1);
426 cairo_fill(cr);
427 cairo_destroy(cr);
428
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500429 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500430 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500431 x - hotspot_x,
432 y - hotspot_y,
433 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500434
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500435 cairo_surface_destroy(surface);
436
Kristian Høgsberg54879822008-11-23 17:07:32 -0500437 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500438}
439
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500440static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500441background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500442{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500443 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500444 GdkPixbuf *pixbuf;
445 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500446 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500447 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448
449 background = malloc(sizeof *background);
450 if (background == NULL)
451 return NULL;
452
453 g_type_init();
454
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500455 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500456 output->width,
457 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500458 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500459 if (error != NULL) {
460 free(background);
461 return NULL;
462 }
463
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500464 data = gdk_pixbuf_get_pixels(pixbuf);
465
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500466 wlsc_surface_init(background, output->compositor,
467 &output->compositor->rgb_visual,
468 output->x, output->y, output->width, output->height);
469
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500470 glBindTexture(GL_TEXTURE_2D, background->texture);
471 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500473 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
474 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500475
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500476 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500477 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500478 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500479 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500480
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500481 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
482 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500483 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500484
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500485 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500486}
487
488static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500489wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500490{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500491 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500492 GLint vertices[12];
493 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
494 GLuint indices[4] = { 0, 1, 2, 3 };
495
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500496 vertices[0] = es->map.x;
497 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500498 vertices[2] = 0;
499
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500500 vertices[3] = es->map.x;
501 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500502 vertices[5] = 0;
503
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500504 vertices[6] = es->map.x + es->map.width;
505 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500506 vertices[8] = 0;
507
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500508 vertices[9] = es->map.x + es->map.width;
509 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500510 vertices[11] = 0;
511
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500512 if (es->visual == &ec->argb_visual) {
513 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
514 glEnable(GL_BLEND);
515 } else if (es->visual == &ec->premultiplied_argb_visual) {
516 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
517 glEnable(GL_BLEND);
518 } else {
519 glDisable(GL_BLEND);
520 }
521
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500522 glPushMatrix();
Kristian Høgsberg9db4efa2009-09-12 21:09:02 -0400523 //glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500524 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500525 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500526 glEnableClientState(GL_VERTEX_ARRAY);
527 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
528 glVertexPointer(3, GL_INT, 0, vertices);
529 glTexCoordPointer(2, GL_INT, 0, tex_coords);
530 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500531 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500532}
533
534static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400535wlsc_surface_raise(struct wlsc_surface *surface)
536{
537 struct wlsc_compositor *compositor = surface->compositor;
538
539 wl_list_remove(&surface->link);
540 wl_list_insert(compositor->surface_list.prev, &surface->link);
541}
542
543static void
544wlsc_surface_lower(struct wlsc_surface *surface)
545{
546 struct wlsc_compositor *compositor = surface->compositor;
547
548 wl_list_remove(&surface->link);
549 wl_list_insert(&compositor->surface_list, &surface->link);
550}
551
552static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500553wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
554{
555 v1->x += v2->x;
556 v1->y += v2->y;
557 v1->z += v2->z;
558}
559
560static void
561wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
562{
563 v1->x -= v2->x;
564 v1->y -= v2->y;
565 v1->z -= v2->z;
566}
567
568static void
569wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
570{
571 v1->x *= s;
572 v1->y *= s;
573 v1->z *= s;
574}
575
576static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400577page_flip_handler(int fd, unsigned int frame,
578 unsigned int sec, unsigned int usec, void *data)
579{
580 struct wlsc_output *output = data;
581 struct wlsc_animate *animate, *next;
582 struct wlsc_compositor *compositor = output->compositor;
583 uint32_t msecs;
584
585 msecs = sec * 1000 + usec / 1000;
586 wl_display_post_frame(compositor->wl_display,
587 &compositor->base,
588 compositor->current_frame, msecs);
589
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400590 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400591 compositor->repaint_on_timeout = 1;
592
593 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
594 while (&animate->link != &compositor->animate_list) {
595 next = container_of(animate->link.next,
596 struct wlsc_animate, link);
597 animate->animate(animate, compositor, compositor->current_frame, msecs);
598 animate = next;
599 }
600
601 compositor->current_frame++;
602}
603
604static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500605repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400606{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500607 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500608 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500609 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500610 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400611 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500612
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500613 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
614 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500615 return;
616 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500617
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500618 glViewport(0, 0, output->width, output->height);
619 glMatrixMode(GL_PROJECTION);
620 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500621 glFrustum(-output->width / s, output->width / s,
622 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500623 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500624 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400625 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500626
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500627 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500628
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500629 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500630 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500631 else
632 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400633
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500634 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500635 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500636 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500637 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500638 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400639 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400640 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400641
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500642 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500643 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500644 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500645 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500646
647 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500648 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500649 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500650
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400651 fd = eglGetDisplayFD(ec->display);
652 output->current ^= 1;
653 eglBindColorBuffer(ec->display, output->surface, output->current);
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500654 drmModePageFlip(fd, output->crtc_id,
655 output->fb_id[output->current ^ 1],
656 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500657}
658
659static void
660repaint(void *data)
661{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500662 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500663 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500664
665 if (!ec->repaint_needed) {
666 ec->repaint_on_timeout = 0;
667 return;
668 }
669
670 output = container_of(ec->output_list.next, struct wlsc_output, link);
671 while (&output->link != &ec->output_list) {
672 repaint_output(output);
673 output = container_of(output->link.next,
674 struct wlsc_output, link);
675 }
676
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500677 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400678}
679
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500680static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400681wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400682{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400683 struct wlsc_output *output;
684 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400685
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400686 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400687 if (compositor->repaint_on_timeout)
688 return;
689
690 fd = eglGetDisplayFD(compositor->display);
691 output = container_of(compositor->output_list.next,
692 struct wlsc_output, link);
693 while (&output->link != &compositor->output_list) {
694 drmModePageFlip(fd, output->crtc_id,
Kristian Høgsberge4bf8842009-12-16 13:18:12 -0500695 output->fb_id[output->current ^ 1],
696 DRM_MODE_PAGE_FLIP_EVENT, output);
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400697 output = container_of(output->link.next,
698 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500699 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400700}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500701
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500702static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500703surface_destroy(struct wl_client *client,
704 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400705{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500706 struct wlsc_surface *es = (struct wlsc_surface *) surface;
707 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500708
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500709 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400710
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400711 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400712}
713
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500714static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500715surface_attach(struct wl_client *client,
716 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500717 uint32_t width, uint32_t height, uint32_t stride,
718 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400719{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500720 struct wlsc_surface *es = (struct wlsc_surface *) surface;
721 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400722
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500723 es->width = width;
724 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400725
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500726 if (visual == &ec->argb_visual.base)
727 es->visual = &ec->argb_visual;
728 else if (visual == &ec->premultiplied_argb_visual.base)
729 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500730 else if (visual == &ec->rgb_visual.base)
731 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500732 else
733 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400734
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500735 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400736 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500737 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
738 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
739 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400740 glTextureExternalMESA(GL_TEXTURE_2D, GL_RGBA, 4,
741 width, height, stride / 4, name);
742
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400743}
744
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500745static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500746surface_map(struct wl_client *client,
747 struct wl_surface *surface,
748 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400749{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500750 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400751
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500752 es->map.x = x;
753 es->map.y = y;
754 es->map.width = width;
755 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400756}
757
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500758static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500759surface_copy(struct wl_client *client,
760 struct wl_surface *surface,
761 int32_t dst_x, int32_t dst_y,
762 uint32_t name, uint32_t stride,
763 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500764{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500765 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400766 GLuint fbo[2], rb;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500767
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400768 glGenFramebuffers(2, fbo);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500769
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400770 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo[1]);
771 glGenRenderbuffers(1, &rb);
772 glBindRenderbuffer(GL_RENDERBUFFER_EXT, rb);
773 glRenderbufferExternalMESA(GL_RENDERBUFFER_EXT,
774 GL_RGBA,
775 es->width, es->height,
776 stride / 4, name);
777 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER_EXT,
778 GL_COLOR_ATTACHMENT0_EXT,
779 GL_RENDERBUFFER_EXT,
780 rb);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400781
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400782 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo[0]);
783 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER_EXT,
784 GL_COLOR_ATTACHMENT0_EXT,
785 GL_TEXTURE_2D, es->texture, 0);
786
787 glBlitFramebuffer(x, y, x + width, y + height,
788 dst_x, dst_y, dst_x+ width, dst_y + height,
789 GL_COLOR_BUFFER_BIT, GL_NEAREST);
790
791 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
792 glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
793 glDeleteRenderbuffers(1, &rb);
794 glDeleteFramebuffers(2, fbo);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500795}
796
797static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500798surface_damage(struct wl_client *client,
799 struct wl_surface *surface,
800 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500801{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500802 /* FIXME: This need to take a damage region, of course. */
803}
804
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500805const static struct wl_surface_interface surface_interface = {
806 surface_destroy,
807 surface_attach,
808 surface_map,
809 surface_copy,
810 surface_damage
811};
812
813static void
814compositor_create_surface(struct wl_client *client,
815 struct wl_compositor *compositor, uint32_t id)
816{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500817 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400818 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500819
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400820 surface = malloc(sizeof *surface);
821 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500822 /* FIXME: Send OOM event. */
823 return;
824
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400825 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500826
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400827 wl_list_insert(ec->surface_list.prev, &surface->link);
828 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500829 &surface_interface, id);
830}
831
832static void
833compositor_commit(struct wl_client *client,
834 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500835{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500836 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500837
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400838 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500839 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500840}
841
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500842const static struct wl_compositor_interface compositor_interface = {
843 compositor_create_surface,
844 compositor_commit
845};
846
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500847static void
848wlsc_surface_transform(struct wlsc_surface *surface,
849 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
850{
851 /* Transform to surface coordinates. */
852 *sx = (x - surface->map.x) * surface->width / surface->map.width;
853 *sy = (y - surface->map.y) * surface->height / surface->map.height;
854}
855
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500856static void
857wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
858 struct wlsc_surface *surface)
859{
860 if (device->keyboard_focus == surface)
861 return;
862
863 if (device->keyboard_focus &&
864 (!surface || device->keyboard_focus->base.client != surface->base.client))
865 wl_surface_post_event(&device->keyboard_focus->base,
866 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -0500867 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500868
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500869 if (surface)
870 wl_surface_post_event(&surface->base,
871 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500872 WL_INPUT_KEYBOARD_FOCUS,
873 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500874
875 device->keyboard_focus = surface;
876}
877
878static void
879wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
880 struct wlsc_surface *surface)
881{
882 if (device->pointer_focus == surface)
883 return;
884
885 if (device->pointer_focus &&
886 (!surface || device->pointer_focus->base.client != surface->base.client))
887 wl_surface_post_event(&device->pointer_focus->base,
888 &device->base,
889 WL_INPUT_POINTER_FOCUS, NULL);
890 if (surface)
891 wl_surface_post_event(&surface->base,
892 &device->base,
893 WL_INPUT_POINTER_FOCUS, &surface->base);
894
895 device->pointer_focus = surface;
896}
897
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500898static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500899pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500900{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500901 struct wlsc_compositor *ec = device->ec;
902 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500903
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500904 if (device->grab > 0) {
905 wlsc_surface_transform(device->grab_surface,
906 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500907 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500908 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500909
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500910 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500911 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500912 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500913 if (es->map.x <= device->x &&
914 device->x < es->map.x + es->map.width &&
915 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500916 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -0500917 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500918 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -0500919 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500920
921 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500922 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500923
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500924 }
925
926 return NULL;
927}
928
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500929void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500930notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500931{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500932 struct wlsc_surface *es;
933 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500934 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500935 const int hotspot_x = 16, hotspot_y = 16;
936 int32_t sx, sy;
937
Ray Strodee96dcb82008-12-20 02:00:49 -0500938 if (!ec->vt_active)
939 return;
940
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500941 /* FIXME: We need some multi head love here. */
942 output = container_of(ec->output_list.next, struct wlsc_output, link);
943 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500944 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500945 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500946 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500947 if (x >= output->x + output->width)
948 x = output->x + output->width - 1;
949 if (y >= output->y + output->height)
950 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500951
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500952 device->x = x;
953 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500954 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500955
956 wlsc_input_device_set_pointer_focus(device, es);
957
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500958 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500959 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500960 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500961
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500962 device->sprite->map.x = x - hotspot_x;
963 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500964
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400965 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500966}
967
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500968void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500969notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500970 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500971{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400972 struct wlsc_surface *surface;
973 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500974 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500975
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400976 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -0500977 return;
978
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400979 surface = pick_surface(device, &sx, &sy);
980 if (surface) {
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500981 if (state) {
Kristian Høgsbergffbc6072009-09-18 17:03:18 -0400982 wlsc_surface_raise(surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500983 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400984 device->grab_surface = surface;
985 wlsc_input_device_set_keyboard_focus(device,
986 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500987 } else {
988 device->grab--;
989 }
990
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500991 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400992 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500993 WL_INPUT_BUTTON, button, state,
994 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500995
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400996 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500997 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500998}
999
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001000static void on_term_signal(int signal_number, void *data);
1001
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001002void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001003notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001004 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001005{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001006 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001007 uint32_t *k, *end;
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001008 uint32_t modifier;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001009
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001010 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001011 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001012
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001013 switch (key | compositor->modifier_state) {
1014 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001015 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001016 return;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001017 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001018
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001019 switch (key) {
1020 case KEY_LEFTCTRL:
1021 case KEY_RIGHTCTRL:
1022 modifier = MODIFIER_CTRL;
1023 break;
1024
1025 case KEY_LEFTALT:
1026 case KEY_RIGHTALT:
1027 modifier = MODIFIER_ALT;
1028 break;
1029
1030 default:
1031 modifier = 0;
1032 break;
1033 }
1034
1035 if (state)
1036 compositor->modifier_state |= modifier;
1037 else
1038 compositor->modifier_state &= ~modifier;
1039
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001040 end = device->keys.data + device->keys.size;
1041 for (k = device->keys.data; k < end; k++) {
1042 if (*k == key)
1043 *k = *--end;
1044 }
1045 device->keys.size = (void *) end - device->keys.data;
1046 if (state) {
1047 k = wl_array_add(&device->keys, sizeof *k);
1048 *k = key;
1049 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001050
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001051 if (device->keyboard_focus != NULL)
1052 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001053 &device->base,
1054 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001055}
1056
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001057struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001058evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001059 struct wl_display *display, const char *path);
1060
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001061static void
1062handle_surface_destroy(struct wlsc_listener *listener,
1063 struct wlsc_surface *surface)
1064{
1065 struct wlsc_input_device *device =
1066 container_of(listener, struct wlsc_input_device, listener);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001067 struct wlsc_surface *focus;
1068 int32_t sx, sy;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001069
1070 if (device->grab_surface == surface) {
1071 device->grab_surface = NULL;
1072 device->grab = 0;
1073 }
1074 if (device->keyboard_focus == surface)
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001075 wlsc_input_device_set_keyboard_focus(device, NULL);
1076 if (device->pointer_focus == surface) {
1077 focus = pick_surface(device, &sx, &sy);
1078 wlsc_input_device_set_pointer_focus(device, focus);
1079 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
1080 }
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001081}
1082
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001083static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001084create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001085{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001086 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001087
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001088 device = malloc(sizeof *device);
1089 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001090 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001091
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001092 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001093 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001094 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001095 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001096 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001097 device->x = 100;
1098 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001099 device->ec = ec;
1100
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001101 device->listener.func = handle_surface_destroy;
1102 wl_list_insert(ec->surface_destroy_listener_list.prev,
1103 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001104 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001105
1106 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001107}
1108
1109void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001110wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001111{
1112 *x = device->x;
1113 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001114}
1115
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001116static void
1117post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001118{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001119 struct wlsc_output *output =
1120 container_of(global, struct wlsc_output, base);
1121
1122 wl_client_post_event(client, global,
1123 WL_OUTPUT_GEOMETRY,
1124 output->width, output->height);
1125}
1126
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001127static void
1128on_drm_input(int fd, uint32_t mask, void *data)
1129{
1130 drmEventContext evctx;
1131
1132 memset(&evctx, 0, sizeof evctx);
1133 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1134 evctx.page_flip_handler = page_flip_handler;
1135 drmHandleEvent(fd, &evctx);
1136}
1137
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001138static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001139init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001140{
1141 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001142 EGL_DEPTH_SIZE, 0,
1143 EGL_STENCIL_SIZE, 0,
1144 EGL_CONFIG_CAVEAT, EGL_NONE,
1145 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001146 EGL_NONE
1147 };
1148
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001149 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001150 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001151 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001152
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001153 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001154 if (ec->display == NULL) {
1155 fprintf(stderr, "failed to create display\n");
1156 return -1;
1157 }
1158
1159 if (!eglInitialize(ec->display, &major, &minor)) {
1160 fprintf(stderr, "failed to initialize display\n");
1161 return -1;
1162 }
1163
1164 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1165 return -1;
1166
1167 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1168 if (ec->context == NULL) {
1169 fprintf(stderr, "failed to create context\n");
1170 return -1;
1171 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001172
1173 loop = wl_display_get_event_loop(ec->wl_display);
1174 fd = eglGetDisplayFD(ec->display);
1175 ec->drm_source =
1176 wl_event_loop_add_fd(loop, fd,
1177 WL_EVENT_READABLE, on_drm_input, ec);
1178
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001179 return 0;
1180}
1181
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001182static drmModeModeInfo builtin_1024x768 = {
1183 63500, /* clock */
1184 1024, 1072, 1176, 1328, 0,
1185 768, 771, 775, 798, 0,
1186 59920,
1187 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1188 0,
1189 "1024x768"
1190};
1191
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001192static int
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001193create_output_for_connector(struct wlsc_compositor *ec,
1194 drmModeRes *resources,
1195 drmModeConnector *connector)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001196{
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001197 struct wlsc_output *output;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001198 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001199 drmModeModeInfo *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001200 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001201 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001202
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001203 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001204
1205 output = malloc(sizeof *output);
1206 if (output == NULL)
1207 return -1;
1208
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001209 if (connector->count_modes > 0)
1210 mode = &connector->modes[0];
1211 else
1212 mode = &builtin_1024x768;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001213
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001214 encoder = drmModeGetEncoder(fd, connector->encoders[0]);
1215 if (encoder == NULL) {
1216 fprintf(stderr, "No encoder for connector.\n");
1217 return -1;
1218 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001219
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001220 for (i = 0; i < resources->count_crtcs; i++) {
1221 if (encoder->possible_crtcs & (1 << i))
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001222 break;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001223 }
1224 if (i == resources->count_crtcs) {
1225 fprintf(stderr, "No usable crtc for encoder.\n");
1226 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001227 }
1228
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001229 output->compositor = ec;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001230 output->crtc_id = resources->crtcs[i];
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001231 output->connector_id = connector->connector_id;
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001232 output->mode = *mode;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001233 output->x = 0;
1234 output->y = 0;
1235 output->width = mode->hdisplay;
1236 output->height = mode->vdisplay;
1237
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001238 printf("using crtc %d, connector %d and encoder %d, mode %s\n",
1239 output->crtc_id,
1240 output->connector_id,
1241 encoder->encoder_id,
1242 mode->name);
1243
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001244 drmModeFreeEncoder(encoder);
1245
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001246 output->surface = eglCreateSurface(ec->display,
1247 ec->config,
1248 output->width,
1249 output->height,
1250 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001251 if (output->surface == NULL) {
1252 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001253 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001254 }
1255
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001256 for (i = 0; i < 2; i++) {
1257 eglGetColorBuffer(output->surface,
1258 i, &name, &handle, &stride);
1259
1260 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1261 32, 32, stride, handle, &output->fb_id[i]);
1262 if (ret) {
1263 fprintf(stderr, "failed to add fb %d: %m\n", i);
1264 return -1;
1265 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001266 }
1267
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001268 output->current = 0;
Kristian Høgsberge000d8c2009-09-21 15:10:24 -04001269 ret = drmModeSetCrtc(fd, output->crtc_id,
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001270 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001271 &output->connector_id, 1, &output->mode);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001272 if (ret) {
1273 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001274 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001275 }
1276
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001277 output->base.interface = &wl_output_interface;
1278 wl_display_add_object(ec->wl_display, &output->base);
1279 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1280 wl_list_insert(ec->output_list.prev, &output->link);
1281
1282 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1283 fprintf(stderr, "failed to make context current\n");
1284 return -1;
1285 }
1286
1287 output->background = background_create(output, option_background);
1288
1289 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001290}
1291
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001292static int
1293create_outputs(struct wlsc_compositor *ec)
1294{
1295 drmModeConnector *connector;
1296 drmModeRes *resources;
1297 int fd, i;
1298
1299 fd = eglGetDisplayFD(ec->display);
1300 resources = drmModeGetResources(fd);
1301 if (!resources) {
1302 fprintf(stderr, "drmModeGetResources failed\n");
1303 return -1;
1304 }
1305
1306 for (i = 0; i < resources->count_connectors; i++) {
1307 connector = drmModeGetConnector(fd, resources->connectors[i]);
1308 if (connector == NULL)
1309 continue;
1310
1311 if (connector->connection == DRM_MODE_CONNECTED &&
1312 (option_connector == 0 ||
1313 connector->connector_id == option_connector))
1314 if (create_output_for_connector(ec, resources, connector) < 0)
1315 return -1;
1316
1317 drmModeFreeConnector(connector);
1318 }
1319
1320 if (wl_list_empty(&ec->output_list)) {
1321 fprintf(stderr, "No currently active connector found.\n");
1322 return -1;
1323 }
1324
1325 drmModeFreeResources(resources);
1326
1327 return 0;
1328}
1329
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001330static const struct wl_interface visual_interface = {
1331 "visual", 1,
1332};
1333
1334static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001335add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001336{
1337 ec->argb_visual.base.interface = &visual_interface;
1338 ec->argb_visual.base.implementation = NULL;
1339 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001340 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001341
1342 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1343 ec->premultiplied_argb_visual.base.implementation = NULL;
1344 wl_display_add_object(ec->wl_display,
1345 &ec->premultiplied_argb_visual.base);
1346 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001347 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001348
1349 ec->rgb_visual.base.interface = &visual_interface;
1350 ec->rgb_visual.base.implementation = NULL;
1351 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001352 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1353}
1354
Ray Strode19ad6a92008-12-19 01:45:41 -05001355static void on_enter_vt(int signal_number, void *data)
1356{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001357 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001358 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001359 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001360
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001361 fd = eglGetDisplayFD(ec->display);
1362 ret = drmSetMaster(fd);
1363 if (ret) {
1364 fprintf(stderr, "failed to set drm master\n");
1365 return;
1366 }
1367
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001368 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001369 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001370
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001371 output = container_of(ec->output_list.next, struct wlsc_output, link);
1372 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001373 ret = drmModeSetCrtc(fd, output->crtc_id,
Kristian Høgsbergdd73dd52009-09-18 17:04:20 -04001374 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001375 &output->connector_id, 1, &output->mode);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001376 if (ret)
1377 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1378 output->connector_id);
1379
1380 output = container_of(output->link.next,
1381 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001382 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001383}
1384
1385static void on_leave_vt(int signal_number, void *data)
1386{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001387 struct wlsc_compositor *ec = data;
Kristian Høgsbergd2311cf2009-10-01 16:43:54 -04001388 int ret, fd;
1389
1390 fd = eglGetDisplayFD(ec->display);
1391 ret = drmDropMaster(fd);
1392 if (ret) {
1393 fprintf(stderr, "failed to drop drm master\n");
1394 return;
1395 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001396
1397 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001398 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001399}
1400
Ray Strode966aa112008-12-19 14:28:02 -05001401static void
1402on_tty_input(int fd, uint32_t mask, void *data)
1403{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001404 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001405
1406 /* Ignore input to tty. We get keyboard events from evdev
1407 */
1408 tcflush(ec->tty_fd, TCIFLUSH);
1409}
1410
1411static void on_term_signal(int signal_number, void *data)
1412{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001413 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001414
1415 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1416 fprintf(stderr, "could not restore terminal to canonical mode\n");
1417
1418 exit(0);
1419}
1420
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001421static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001422{
1423 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001424 struct vt_mode mode = { 0 };
1425
1426 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1427 if (ec->tty_fd <= 0) {
1428 fprintf(stderr, "failed to open active tty: %m\n");
1429 return -1;
1430 }
Ray Strode966aa112008-12-19 14:28:02 -05001431
1432 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1433 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001434 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001435 }
1436
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001437 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001438 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001439 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001440
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001441 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001442 raw_attributes.c_oflag |= OPOST | OCRNL;
1443
1444 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1445 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1446
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001447 ec->term_signal_source =
1448 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001449
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001450 ec->tty_input_source =
1451 wl_event_loop_add_fd(loop, ec->tty_fd,
1452 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001453
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001454 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001455 mode.mode = VT_PROCESS;
1456 mode.relsig = SIGUSR1;
1457 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001458 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001459 fprintf(stderr, "failed to take control of vt handling\n");
1460 }
1461
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001462 ec->leave_vt_source =
1463 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1464 ec->enter_vt_source =
1465 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001466
1467 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001468}
1469
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001470static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001471init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001472{
1473 struct udev_enumerate *e;
1474 struct udev_list_entry *entry;
1475 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001476 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001477 struct wlsc_input_device *input_device;
1478
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001479 ec->udev = udev_new();
1480 if (ec->udev == NULL) {
1481 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001482 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001483 }
1484
1485 input_device = create_input_device(ec);
1486
1487 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001488 udev_enumerate_add_match_subsystem(e, "input");
1489 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001490 udev_enumerate_scan_devices(e);
1491 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1492 path = udev_list_entry_get_name(entry);
1493 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001494 evdev_input_device_create(input_device, ec->wl_display,
1495 udev_device_get_devnode(device));
1496 }
1497 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001498
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001499 e = udev_enumerate_new(ec->udev);
1500 udev_enumerate_add_match_subsystem(e, "drm");
1501 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1502 udev_enumerate_scan_devices(e);
1503 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1504 path = udev_list_entry_get_name(entry);
1505 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsberg1b8b66f2009-09-21 16:00:17 -04001506 if (init_egl(ec, device) < 0) {
1507 fprintf(stderr, "failed to initialize egl\n");
1508 return -1;
1509 }
1510 if (create_outputs(ec) < 0) {
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001511 fprintf(stderr, "failed to create output for %s\n", path);
1512 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001513 }
1514 }
1515 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001516
1517 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001518 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001519 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1520
1521 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001522}
1523
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001524static struct wlsc_compositor *
1525wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001526{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001527 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001528 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001529 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001530
1531 ec = malloc(sizeof *ec);
1532 if (ec == NULL)
1533 return NULL;
1534
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001535 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001536 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001537
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001538 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001539
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001540 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001541
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001542 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001543 wl_list_init(&ec->input_device_list);
1544 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001545 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001546 if (init_libudev(ec) < 0) {
1547 fprintf(stderr, "failed to initialize devices\n");
1548 return NULL;
1549 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001550
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001551 shooter = screenshooter_create(ec);
1552 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001553 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001554
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001555 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001556
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001557 setup_tty(ec, loop);
1558
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001559 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001560 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001561
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001562 wl_list_init(&ec->animate_list);
1563
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001564 return ec;
1565}
1566
1567/* The plan here is to generate a random anonymous socket name and
1568 * advertise that through a service on the session dbus.
1569 */
1570static const char socket_name[] = "\0wayland";
1571
1572int main(int argc, char *argv[])
1573{
1574 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001575 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001576 GError *error = NULL;
1577 GOptionContext *context;
1578
1579 context = g_option_context_new(NULL);
1580 g_option_context_add_main_entries(context, option_entries, "Wayland");
1581 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1582 fprintf(stderr, "option parsing failed: %s\n", error->message);
1583 exit(EXIT_FAILURE);
1584 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001585
1586 display = wl_display_create();
1587
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001588 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001589 if (ec == NULL) {
1590 fprintf(stderr, "failed to create compositor\n");
1591 exit(EXIT_FAILURE);
1592 }
1593
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001594 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001595 fprintf(stderr, "failed to add socket: %m\n");
1596 exit(EXIT_FAILURE);
1597 }
1598
1599 wl_display_run(display);
1600
1601 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001602}