blob: 1d6d0afdf6f09a1fadc8ec5a21a4473bbefe96b5 [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øgsberg41a10682009-02-15 22:37:03 -050075 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
135 uint32_t meta_state;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500136 struct wl_list animate_list;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400137
138 struct wlsc_session *primary;
139 struct wl_list session_list;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400140};
141
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500142#define META_DOWN 256
143
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400144struct wlsc_vector {
145 GLdouble x, y, z;
146};
147
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500148struct wlsc_animate {
149 struct wl_list link;
150 void (*animate)(struct wlsc_animate *animate,
151 struct wlsc_compositor *compositor,
152 uint32_t frame, uint32_t msecs);
153};
154
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400155struct wlsc_session {
156 struct wlsc_surface *surface;
157 struct wlsc_vector target, current, previous;
158 GLdouble target_angle, current_angle, previous_angle;
159 struct wlsc_animate animate;
160 struct wl_list link;
161 struct wlsc_listener listener;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500162};
163
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500164struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500165 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500166 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500167 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400168 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400169 struct wl_map map;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500170 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500171 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500172 struct wlsc_matrix matrix;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500173
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400174 /* FIXME: This should be it's own object at some point. */
175 struct wlsc_session session;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400176};
177
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500178static const char *option_background = "background.jpg";
179
180static const GOptionEntry option_entries[] = {
181 { "background", 'b', 0, G_OPTION_ARG_STRING,
182 &option_background, "Background image" },
183 { NULL }
184};
185
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500186struct screenshooter {
187 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500188 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500189};
190
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500191struct screenshooter_interface {
192 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
193};
194
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500195static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500196screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500197{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500198 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500199 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500200 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500201 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500202 GError *error = NULL;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500203 unsigned char *data;
204 int i, j;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500205
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500206 i = 0;
207 output = container_of(ec->output_list.next, struct wlsc_output, link);
208 while (&output->link != &ec->output_list) {
209 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500210 data = malloc(output->width * output->height * 4);
211 if (data == NULL) {
212 fprintf(stderr, "couldn't allocate image buffer\n");
213 continue;
214 }
215
216 glReadBuffer(GL_FRONT);
217 glPixelStorei(GL_PACK_ALIGNMENT, 1);
218 glReadPixels(0, 0, output->width, output->height,
219 GL_RGBA, GL_UNSIGNED_BYTE, data);
220
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500221 /* FIXME: We should just use a RGB visual for the frontbuffer. */
222 for (j = 3; j < output->width * output->height * 4; j += 4)
223 data[j] = 0xff;
224
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500225 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500226 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500227 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500228 normal = gdk_pixbuf_flip(pixbuf, FALSE);
229 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
230 gdk_pixbuf_unref(normal);
231 gdk_pixbuf_unref(pixbuf);
232 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500233
234 output = container_of(output->link.next,
235 struct wlsc_output, link);
236 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500237}
238
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500239static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500240 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500241};
242
243static const struct wl_interface screenshooter_interface = {
244 "screenshooter", 1,
245 ARRAY_LENGTH(screenshooter_methods),
246 screenshooter_methods,
247};
248
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500249struct screenshooter_interface screenshooter_implementation = {
250 screenshooter_shoot
251};
252
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500253static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500254screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500255{
256 struct screenshooter *shooter;
257
258 shooter = malloc(sizeof *shooter);
259 if (shooter == NULL)
260 return NULL;
261
262 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500263 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500264 shooter->ec = ec;
265
266 return shooter;
267};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500268
269static void
270wlsc_matrix_init(struct wlsc_matrix *matrix)
271{
272 static const struct wlsc_matrix identity = {
273 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
274 };
275
276 memcpy(matrix, &identity, sizeof identity);
277}
278
279static void
280wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
281{
282 struct wlsc_matrix tmp;
283 const GLdouble *row, *column;
284 div_t d;
285 int i, j;
286
287 for (i = 0; i < 16; i++) {
288 tmp.d[i] = 0;
289 d = div(i, 4);
290 row = m->d + d.quot * 4;
291 column = n->d + d.rem;
292 for (j = 0; j < 4; j++)
293 tmp.d[i] += row[j] * column[j * 4];
294 }
295 memcpy(m, &tmp, sizeof tmp);
296}
297
298static void
299wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
300{
301 struct wlsc_matrix translate = {
302 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
303 };
304
305 wlsc_matrix_multiply(matrix, &translate);
306}
307
308static void
309wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
310{
311 struct wlsc_matrix scale = {
312 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
313 };
314
315 wlsc_matrix_multiply(matrix, &scale);
316}
317
318static void
319wlsc_matrix_rotate(struct wlsc_matrix *matrix,
320 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
321{
322 GLdouble c = cos(angle);
323 GLdouble s = sin(angle);
324 struct wlsc_matrix rotate = {
325 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
326 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
327 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
328 0, 0, 0, 1 }
329 };
330
331 wlsc_matrix_multiply(matrix, &rotate);
332}
333
334static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500335wlsc_surface_init(struct wlsc_surface *surface,
336 struct wlsc_compositor *compositor, struct wl_visual *visual,
337 int32_t x, int32_t y, int32_t width, int32_t height)
338{
339 glGenTextures(1, &surface->texture);
340 surface->compositor = compositor;
341 surface->map.x = x;
342 surface->map.y = y;
343 surface->map.width = width;
344 surface->map.height = height;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500345 surface->visual = visual;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400346 wlsc_matrix_init(&surface->matrix);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500347}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500348
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500349static struct wlsc_surface *
350wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500351 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500352 int x, int y, int width, int height)
353{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500354 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500355 int stride;
356 void *data;
357
358 stride = cairo_image_surface_get_stride(surface);
359 data = cairo_image_surface_get_data(surface);
360
361 es = malloc(sizeof *es);
362 if (es == NULL)
363 return NULL;
364
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500365 wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
366 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500367 glBindTexture(GL_TEXTURE_2D, es->texture);
368 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500369 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
373 GL_BGRA, GL_UNSIGNED_BYTE, data);
374
Kristian Høgsberg54879822008-11-23 17:07:32 -0500375 return es;
376}
377
378static void
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400379wlsc_surface_destroy(struct wlsc_surface *surface,
380 struct wlsc_compositor *compositor)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500381{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400382 struct wlsc_listener *l, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400383
384 l = container_of(compositor->surface_destroy_listener_list.next,
385 struct wlsc_listener, link);
386 while (&l->link != &compositor->surface_destroy_listener_list) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400387 next = container_of(l->link.next, struct wlsc_listener, link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400388 l->func(l, surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400389 l = next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400390 }
391
392 wl_list_remove(&surface->link);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400393
394 glDeleteTextures(1, &surface->texture);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400395 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500396}
397
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400398static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500399pointer_path(cairo_t *cr, int x, int y)
400{
401 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
402 const int width = 16, height = 16;
403
404 cairo_move_to(cr, x, y);
405 cairo_line_to(cr, x + tx, y + ty);
406 cairo_line_to(cr, x + dx, y + dy);
407 cairo_line_to(cr, x + width - end, y + height);
408 cairo_line_to(cr, x + width, y + height - end);
409 cairo_line_to(cr, x + dy, y + dx);
410 cairo_line_to(cr, x + ty, y + tx);
411 cairo_close_path(cr);
412}
413
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500414static struct wlsc_surface *
415pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500416{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500417 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500419 cairo_surface_t *surface;
420 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500421
422 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
423 width, height);
424
425 cr = cairo_create(surface);
426 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500427 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500428 cairo_set_source_rgb(cr, 0, 0, 0);
429 cairo_stroke_preserve(cr);
430 cairo_fill(cr);
431 blur_surface(surface, width);
432
433 pointer_path(cr, hotspot_x, hotspot_y);
434 cairo_stroke_preserve(cr);
435 cairo_set_source_rgb(cr, 1, 1, 1);
436 cairo_fill(cr);
437 cairo_destroy(cr);
438
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500439 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500440 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500441 x - hotspot_x,
442 y - hotspot_y,
443 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500444
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500445 cairo_surface_destroy(surface);
446
Kristian Høgsberg54879822008-11-23 17:07:32 -0500447 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448}
449
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500450static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500451background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500452{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500453 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500454 GdkPixbuf *pixbuf;
455 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500456 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500457 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458
459 background = malloc(sizeof *background);
460 if (background == NULL)
461 return NULL;
462
463 g_type_init();
464
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500465 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500466 output->width,
467 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500468 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500469 if (error != NULL) {
470 free(background);
471 return NULL;
472 }
473
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500474 data = gdk_pixbuf_get_pixels(pixbuf);
475
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500476 wlsc_surface_init(background, output->compositor,
477 &output->compositor->rgb_visual,
478 output->x, output->y, output->width, output->height);
479
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500480 glBindTexture(GL_TEXTURE_2D, background->texture);
481 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500483 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
484 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500485
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500486 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500487 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500488 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500489 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500490
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500491 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
492 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500493 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500495 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500496}
497
498static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500499wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500500{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500501 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500502 GLint vertices[12];
503 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
504 GLuint indices[4] = { 0, 1, 2, 3 };
505
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500506 vertices[0] = es->map.x;
507 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508 vertices[2] = 0;
509
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500510 vertices[3] = es->map.x;
511 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500512 vertices[5] = 0;
513
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500514 vertices[6] = es->map.x + es->map.width;
515 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500516 vertices[8] = 0;
517
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500518 vertices[9] = es->map.x + es->map.width;
519 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500520 vertices[11] = 0;
521
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500522 if (es->visual == &ec->argb_visual) {
523 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
524 glEnable(GL_BLEND);
525 } else if (es->visual == &ec->premultiplied_argb_visual) {
526 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
527 glEnable(GL_BLEND);
528 } else {
529 glDisable(GL_BLEND);
530 }
531
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500532 glPushMatrix();
533 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500534 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500535 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500536 glEnableClientState(GL_VERTEX_ARRAY);
537 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
538 glVertexPointer(3, GL_INT, 0, vertices);
539 glTexCoordPointer(2, GL_INT, 0, tex_coords);
540 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500541 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500542}
543
544static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400545wlsc_surface_raise(struct wlsc_surface *surface)
546{
547 struct wlsc_compositor *compositor = surface->compositor;
548
549 wl_list_remove(&surface->link);
550 wl_list_insert(compositor->surface_list.prev, &surface->link);
551}
552
553static void
554wlsc_surface_lower(struct wlsc_surface *surface)
555{
556 struct wlsc_compositor *compositor = surface->compositor;
557
558 wl_list_remove(&surface->link);
559 wl_list_insert(&compositor->surface_list, &surface->link);
560}
561
562static void
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500563wlsc_vector_add(struct wlsc_vector *v1, struct wlsc_vector *v2)
564{
565 v1->x += v2->x;
566 v1->y += v2->y;
567 v1->z += v2->z;
568}
569
570static void
571wlsc_vector_subtract(struct wlsc_vector *v1, struct wlsc_vector *v2)
572{
573 v1->x -= v2->x;
574 v1->y -= v2->y;
575 v1->z -= v2->z;
576}
577
578static void
579wlsc_vector_scalar(struct wlsc_vector *v1, GLdouble s)
580{
581 v1->x *= s;
582 v1->y *= s;
583 v1->z *= s;
584}
585
586static void
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400587page_flip_handler(int fd, unsigned int frame,
588 unsigned int sec, unsigned int usec, void *data)
589{
590 struct wlsc_output *output = data;
591 struct wlsc_animate *animate, *next;
592 struct wlsc_compositor *compositor = output->compositor;
593 uint32_t msecs;
594
595 msecs = sec * 1000 + usec / 1000;
596 wl_display_post_frame(compositor->wl_display,
597 &compositor->base,
598 compositor->current_frame, msecs);
599
Kristian Høgsberg5d312db2009-09-12 16:57:02 -0400600 wl_event_source_timer_update(compositor->timer_source, 5);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400601 compositor->repaint_on_timeout = 1;
602
603 animate = container_of(compositor->animate_list.next, struct wlsc_animate, link);
604 while (&animate->link != &compositor->animate_list) {
605 next = container_of(animate->link.next,
606 struct wlsc_animate, link);
607 animate->animate(animate, compositor, compositor->current_frame, msecs);
608 animate = next;
609 }
610
611 compositor->current_frame++;
612}
613
614static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500615repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400616{
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500617 struct wlsc_compositor *ec = output->compositor;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500618 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500619 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500620 double s = 3000;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400621 int fd;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500622
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500623 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
624 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500625 return;
626 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500627
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500628 glViewport(0, 0, output->width, output->height);
629 glMatrixMode(GL_PROJECTION);
630 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500631 glFrustum(-output->width / s, output->width / s,
632 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500633 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500634 glLoadIdentity();
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400635 glClearColor(0, 0, 0, 1);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500636
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500637 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500638
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500639 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500640 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500641 else
642 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400643
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500644 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500645 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500646 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500647 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500648 es = container_of(es->link.next,
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400649 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400650 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400651
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500652 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500653 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500654 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -0500655 wlsc_surface_draw(eid->sprite);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500656
657 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500658 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500659 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500660
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400661 fd = eglGetDisplayFD(ec->display);
662 output->current ^= 1;
663 eglBindColorBuffer(ec->display, output->surface, output->current);
664 drmModePageFlip(fd, output->crtc_id, output->fb_id[output->current ^ 1], output);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500665}
666
667static void
668repaint(void *data)
669{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500670 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500671 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500672
673 if (!ec->repaint_needed) {
674 ec->repaint_on_timeout = 0;
675 return;
676 }
677
678 output = container_of(ec->output_list.next, struct wlsc_output, link);
679 while (&output->link != &ec->output_list) {
680 repaint_output(output);
681 output = container_of(output->link.next,
682 struct wlsc_output, link);
683 }
684
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500685 ec->repaint_needed = 0;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400686}
687
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500688static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400689wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400690{
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400691 struct wlsc_output *output;
692 int fd;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400693
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400694 compositor->repaint_needed = 1;
Kristian Høgsbergb0a167c2009-08-14 11:15:18 -0400695 if (compositor->repaint_on_timeout)
696 return;
697
698 fd = eglGetDisplayFD(compositor->display);
699 output = container_of(compositor->output_list.next,
700 struct wlsc_output, link);
701 while (&output->link != &compositor->output_list) {
702 drmModePageFlip(fd, output->crtc_id,
703 output->fb_id[output->current ^ 1], output);
704 output = container_of(output->link.next,
705 struct wlsc_output, link);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500706 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400707}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500708
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500709static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500710surface_destroy(struct wl_client *client,
711 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400712{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500713 struct wlsc_surface *es = (struct wlsc_surface *) surface;
714 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500715
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500716 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400717
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400718 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400719}
720
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500721static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500722surface_attach(struct wl_client *client,
723 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500724 uint32_t width, uint32_t height, uint32_t stride,
725 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400726{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500727 struct wlsc_surface *es = (struct wlsc_surface *) surface;
728 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400729
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500730 es->width = width;
731 es->height = height;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400732
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500733 if (visual == &ec->argb_visual.base)
734 es->visual = &ec->argb_visual;
735 else if (visual == &ec->premultiplied_argb_visual.base)
736 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500737 else if (visual == &ec->rgb_visual.base)
738 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500739 else
740 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400741
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500742 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400743 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500744 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
745 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
746 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400747 glTextureExternalMESA(GL_TEXTURE_2D, GL_RGBA, 4,
748 width, height, stride / 4, name);
749
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400750}
751
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500752static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400753wlsc_session_update_matrix(struct wlsc_session *session);
754
755static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500756surface_map(struct wl_client *client,
757 struct wl_surface *surface,
758 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400759{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500760 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400761
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500762 es->map.x = x;
763 es->map.y = y;
764 es->map.width = width;
765 es->map.height = height;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400766
767 wlsc_session_update_matrix(&es->session);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400768}
769
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500770static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500771surface_copy(struct wl_client *client,
772 struct wl_surface *surface,
773 int32_t dst_x, int32_t dst_y,
774 uint32_t name, uint32_t stride,
775 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500776{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500777 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400778 GLuint fbo[2], rb;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500779
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400780 glGenFramebuffers(2, fbo);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500781
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400782 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fbo[1]);
783 glGenRenderbuffers(1, &rb);
784 glBindRenderbuffer(GL_RENDERBUFFER_EXT, rb);
785 glRenderbufferExternalMESA(GL_RENDERBUFFER_EXT,
786 GL_RGBA,
787 es->width, es->height,
788 stride / 4, name);
789 glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER_EXT,
790 GL_COLOR_ATTACHMENT0_EXT,
791 GL_RENDERBUFFER_EXT,
792 rb);
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400793
Kristian Høgsberg4adaf5c2009-09-12 16:42:07 -0400794 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo[0]);
795 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER_EXT,
796 GL_COLOR_ATTACHMENT0_EXT,
797 GL_TEXTURE_2D, es->texture, 0);
798
799 glBlitFramebuffer(x, y, x + width, y + height,
800 dst_x, dst_y, dst_x+ width, dst_y + height,
801 GL_COLOR_BUFFER_BIT, GL_NEAREST);
802
803 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
804 glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
805 glDeleteRenderbuffers(1, &rb);
806 glDeleteFramebuffers(2, fbo);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500807}
808
809static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500810surface_damage(struct wl_client *client,
811 struct wl_surface *surface,
812 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500813{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500814 /* FIXME: This need to take a damage region, of course. */
815}
816
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500817const static struct wl_surface_interface surface_interface = {
818 surface_destroy,
819 surface_attach,
820 surface_map,
821 surface_copy,
822 surface_damage
823};
824
825static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400826wlsc_session_update_matrix(struct wlsc_session *session)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500827{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400828 GLdouble tx, ty;
829 struct wlsc_surface *s = session->surface;
830
831 tx = s->map.x + s->map.width / 2;
832 ty = s->map.y + s->map.height / 2;
833
834 wlsc_matrix_init(&s->matrix);
835 wlsc_matrix_translate(&s->matrix, -tx, -ty, 0);
836 wlsc_matrix_rotate(&s->matrix, session->current_angle, 0, 1, 0);
837 wlsc_matrix_translate(&s->matrix, tx + session->current.x,
838 ty + session->current.y, session->current.z);
839}
840
841static void
842wl_session_animate(struct wlsc_animate *animate,
843 struct wlsc_compositor *compositor,
844 uint32_t frame, uint32_t msecs)
845{
846 struct wlsc_session *session;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500847 struct wlsc_surface *s;
848 double angle_force, angle;
849 struct wlsc_vector force, tmp;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400850 double step = 0.2;
851 double friction = 1.5;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500852 double spring = 0.2;
853
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400854 session = container_of(animate, struct wlsc_session, animate);
855 s = session->surface;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500856
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400857 angle = session->current_angle;
858 angle_force = (session->target_angle - angle) * spring +
859 (session->previous_angle - angle) * friction;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500860
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400861 session->current_angle =
862 angle + (angle - session->previous_angle) + angle_force * step;
863 session->previous_angle = angle;
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500864
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400865 force = session->target;
866 wlsc_vector_subtract(&force, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500867 wlsc_vector_scalar(&force, spring);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400868 tmp = session->previous;
869 wlsc_vector_subtract(&tmp, &session->current);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500870 wlsc_vector_scalar(&tmp, friction);
871 wlsc_vector_add(&force, &tmp);
872
873 wlsc_vector_scalar(&force, step);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400874 wlsc_vector_add(&force, &session->current);
875 wlsc_vector_subtract(&force, &session->previous);
876 session->previous = session->current;
877 wlsc_vector_add(&session->current, &force);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500878
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400879 wlsc_session_update_matrix(session);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500880
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400881 tmp = session->current;
882 wlsc_vector_subtract(&tmp, &session->target);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500883 if (tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z > 0.001) {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400884 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500885 } else {
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400886 wl_list_remove(&session->animate.link);
887 wl_list_init(&session->animate.link);
Kristian Høgsbergbb8cd932009-02-10 19:48:47 -0500888 }
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500889}
890
891static void
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400892wlsc_session_activate(struct wlsc_compositor *compositor, struct wlsc_session *session)
893{
894 struct wlsc_session *s;
895 int i;
896
897 compositor->primary = session;
898 if (wl_list_empty(&compositor->session_list))
899 return;
900
901 session->target.x = 0;
902 session->target.y = 0;
903 session->target.z = -600;
904 session->target_angle = 0;
905 wl_list_remove(&session->animate.link);
906 wl_list_insert(compositor->animate_list.prev, &session->animate.link);
907
908 i = 0;
909 s = container_of(session->link.prev,
910 struct wlsc_session, link);
911 while (&s->link != &compositor->session_list) {
912 s->target.x = -1000 - 500 * i;
913 s->target.y = 0;
914 s->target.z = -2500;
915 s->target_angle = M_PI / 4;
916 wl_list_remove(&s->animate.link);
917 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
918 wlsc_surface_lower(s->surface);
919
920 s = container_of(s->link.prev,
921 struct wlsc_session, link);
922 i++;
923 }
924
925 i = 0;
926 s = container_of(session->link.next,
927 struct wlsc_session, link);
928 while (&s->link != &compositor->session_list) {
929 s->target.x = 1000 + 500 * i;
930 s->target.y = 0;
931 s->target.z = -2500;
932 s->target_angle = -M_PI / 4;
933 wl_list_remove(&s->animate.link);
934 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
935 wlsc_surface_lower(s->surface);
936
937 s = container_of(s->link.next,
938 struct wlsc_session, link);
939 i++;
940 }
941
942 wlsc_compositor_schedule_repaint(compositor);
943}
944
945static void
946wlsc_session_handle_surface_destroy(struct wlsc_listener *listener,
947 struct wlsc_surface *surface)
948{
949 struct wlsc_session *session =
950 container_of(listener, struct wlsc_session, listener);
951 struct wlsc_compositor *compositor;
952 struct wlsc_session *primary;
953
954 if (session->surface != surface)
955 return;
956
957 compositor = session->surface->compositor;
958 if (compositor->primary == session) {
959 printf("destroy session %p [%p, %p]\n", session, session->link.prev, session->link.next);
960 if (session->link.next != &compositor->session_list)
961 primary = container_of(session->link.next,
962 struct wlsc_session, link);
963 else if (session->link.prev != &compositor->session_list)
964 primary = container_of(session->link.prev,
965 struct wlsc_session, link);
966 else
967 primary = NULL;
968 }
969
970 wl_list_remove(&session->animate.link);
971 wl_list_remove(&session->link);
972 wl_list_remove(&session->listener.link);
973
974 if (compositor->primary == session) {
975 printf("activating session %p\n", primary);
976 wlsc_session_activate(compositor, primary);
977 }
978}
979
980static void
981wlsc_session_init(struct wlsc_session *session, struct wlsc_surface *surface)
982{
983 struct wlsc_compositor *c = surface->compositor;
984
985 session->animate.animate = wl_session_animate;
986 wl_list_init(&session->animate.link);
987 wl_list_insert(c->session_list.prev, &session->link);
988
989 session->surface = surface;
990 session->current.x = 0;
991 session->current.y = 0;
992 session->current.z = 0;
993 session->current_angle = 0;
994 session->target.x = 0;
995 session->target.y = 0;
996 session->target.z = 0;
997 session->target_angle = 0;
998 session->previous_angle = 0;
999
1000 wlsc_session_update_matrix(session);
1001
1002 session->listener.func = wlsc_session_handle_surface_destroy;
1003 wl_list_insert(c->surface_destroy_listener_list.prev,
1004 &session->listener.link);
1005
1006 printf("added session %p [%p, %p]\n", session, session->link.prev, session->link.next);
1007 printf(" - prev [%p]\n", session->link.prev->prev);
1008}
1009
1010static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001011compositor_create_surface(struct wl_client *client,
1012 struct wl_compositor *compositor, uint32_t id)
1013{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001014 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001015 struct wlsc_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001016
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001017 surface = malloc(sizeof *surface);
1018 if (surface == NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001019 /* FIXME: Send OOM event. */
1020 return;
1021
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001022 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001023
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001024 wl_list_insert(ec->surface_list.prev, &surface->link);
1025 wl_client_add_surface(client, &surface->base,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001026 &surface_interface, id);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001027
1028 wlsc_session_init(&surface->session, surface);
1029 wlsc_session_activate(ec, &surface->session);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001030}
1031
1032static void
1033compositor_commit(struct wl_client *client,
1034 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001035{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001036 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001037
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001038 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001039 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001040}
1041
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001042const static struct wl_compositor_interface compositor_interface = {
1043 compositor_create_surface,
1044 compositor_commit
1045};
1046
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001047static void
1048wlsc_surface_transform(struct wlsc_surface *surface,
1049 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1050{
1051 /* Transform to surface coordinates. */
1052 *sx = (x - surface->map.x) * surface->width / surface->map.width;
1053 *sy = (y - surface->map.y) * surface->height / surface->map.height;
1054}
1055
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001056static void
1057wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
1058 struct wlsc_surface *surface)
1059{
1060 if (device->keyboard_focus == surface)
1061 return;
1062
1063 if (device->keyboard_focus &&
1064 (!surface || device->keyboard_focus->base.client != surface->base.client))
1065 wl_surface_post_event(&device->keyboard_focus->base,
1066 &device->base,
Kristian Høgsberg786ca0d2009-03-06 21:25:21 -05001067 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001068
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001069 if (surface)
1070 wl_surface_post_event(&surface->base,
1071 &device->base,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001072 WL_INPUT_KEYBOARD_FOCUS,
1073 &surface->base, &device->keys);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001074
1075 device->keyboard_focus = surface;
1076}
1077
1078static void
1079wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
1080 struct wlsc_surface *surface)
1081{
1082 if (device->pointer_focus == surface)
1083 return;
1084
1085 if (device->pointer_focus &&
1086 (!surface || device->pointer_focus->base.client != surface->base.client))
1087 wl_surface_post_event(&device->pointer_focus->base,
1088 &device->base,
1089 WL_INPUT_POINTER_FOCUS, NULL);
1090 if (surface)
1091 wl_surface_post_event(&surface->base,
1092 &device->base,
1093 WL_INPUT_POINTER_FOCUS, &surface->base);
1094
1095 device->pointer_focus = surface;
1096}
1097
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001098static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001099pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001100{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001101 struct wlsc_compositor *ec = device->ec;
1102 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001103
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001104 if (device->grab > 0) {
1105 wlsc_surface_transform(device->grab_surface,
1106 device->x, device->y, sx, sy);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001107 return device->grab_surface;
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001108 }
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001109
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001110 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001111 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001112 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001113 if (es->map.x <= device->x &&
1114 device->x < es->map.x + es->map.width &&
1115 es->map.y <= device->y &&
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001116 device->y < es->map.y + es->map.height) {
Kristian Høgsberg7b6907f2009-02-14 17:47:55 -05001117 wlsc_surface_transform(es, device->x, device->y, sx, sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001118 return es;
Kristian Høgsberg6c9c8f82009-02-10 18:29:24 -05001119 }
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001120
1121 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001122 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001123
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001124 }
1125
1126 return NULL;
1127}
1128
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001129void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001130notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001131{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001132 struct wlsc_surface *es;
1133 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001134 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001135 const int hotspot_x = 16, hotspot_y = 16;
1136 int32_t sx, sy;
1137
Ray Strodee96dcb82008-12-20 02:00:49 -05001138 if (!ec->vt_active)
1139 return;
1140
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001141 /* FIXME: We need some multi head love here. */
1142 output = container_of(ec->output_list.next, struct wlsc_output, link);
1143 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -05001144 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001145 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -05001146 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001147 if (x >= output->x + output->width)
1148 x = output->x + output->width - 1;
1149 if (y >= output->y + output->height)
1150 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -05001151
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -05001152 device->x = x;
1153 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001154 es = pick_surface(device, &sx, &sy);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001155
1156 wlsc_input_device_set_pointer_focus(device, es);
1157
Kristian Høgsberg7e972a52008-12-21 17:26:00 -05001158 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001159 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001160 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001161
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001162 device->sprite->map.x = x - hotspot_x;
1163 device->sprite->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001164
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001165 wlsc_compositor_schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001166}
1167
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001168void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001169notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001170 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001171{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001172 struct wlsc_surface *surface;
1173 struct wlsc_compositor *compositor = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001174 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001175
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001176 if (!compositor->vt_active)
Ray Strodee96dcb82008-12-20 02:00:49 -05001177 return;
1178
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001179 surface = pick_surface(device, &sx, &sy);
1180 if (surface) {
1181 wlsc_surface_raise(surface);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001182
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001183 if (state) {
1184 device->grab++;
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001185 device->grab_surface = surface;
1186 wlsc_input_device_set_keyboard_focus(device,
1187 surface);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -05001188 } else {
1189 device->grab--;
1190 }
1191
Kristian Høgsberg5a75c902008-12-10 13:16:50 -05001192 /* FIXME: Swallow click on raise? */
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001193 wl_surface_post_event(&surface->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001194 WL_INPUT_BUTTON, button, state,
1195 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001196
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001197 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001198 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001199}
1200
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001201static void on_term_signal(int signal_number, void *data);
1202
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001203void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001204notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001205 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001206{
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001207 struct wlsc_compositor *compositor = device->ec;
1208 struct wlsc_session *s;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001209 uint32_t *k, *end;
1210
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001211 if (!compositor->vt_active)
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001212 return;
Ray Strodee96dcb82008-12-20 02:00:49 -05001213
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001214 switch (key | compositor->meta_state) {
Kristian Høgsberg45b7a3a2009-08-14 05:53:50 -04001215 case KEY_BACKSPACE | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001216 on_term_signal(SIGTERM, compositor);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001217 return;
1218
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001219 case KEY_LEFT | META_DOWN:
1220 if (!compositor->primary || !state)
1221 break;
1222 s = container_of(compositor->primary->link.prev,
1223 struct wlsc_session, link);
1224 if (&s->link != &compositor->session_list) {
1225 wlsc_session_activate(compositor, s);
1226 if (device->grab == 0)
1227 wlsc_input_device_set_keyboard_focus(device, s->surface);
1228 }
1229 return;
1230
1231
1232 case KEY_RIGHT | META_DOWN:
1233 if (!compositor->primary || !state)
1234 break;
1235 s = container_of(compositor->primary->link.next,
1236 struct wlsc_session, link);
1237 if (&s->link != &compositor->session_list) {
1238 wlsc_session_activate(compositor, s);
1239 if (device->grab == 0)
1240 wlsc_input_device_set_keyboard_focus(device, s->surface);
1241
1242 }
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001243 return;
1244
1245 case KEY_LEFTMETA:
1246 case KEY_RIGHTMETA:
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001247 case KEY_LEFTMETA | META_DOWN:
1248 case KEY_RIGHTMETA | META_DOWN:
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001249 compositor->meta_state = state ? META_DOWN : 0;
1250
1251 if (state == 0 && compositor->primary) {
1252 s = compositor->primary;
1253 s->target.z = 0;
1254 wl_list_remove(&s->animate.link);
1255 wl_list_insert(compositor->animate_list.prev, &s->animate.link);
1256 wlsc_compositor_schedule_repaint(compositor);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001257 }
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001258
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001259 return;
1260 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001261
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001262 end = device->keys.data + device->keys.size;
1263 for (k = device->keys.data; k < end; k++) {
1264 if (*k == key)
1265 *k = *--end;
1266 }
1267 device->keys.size = (void *) end - device->keys.data;
1268 if (state) {
1269 k = wl_array_add(&device->keys, sizeof *k);
1270 *k = key;
1271 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001272
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001273 if (device->keyboard_focus != NULL)
1274 wl_surface_post_event(&device->keyboard_focus->base,
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -05001275 &device->base,
1276 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001277}
1278
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001279struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001280evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001281 struct wl_display *display, const char *path);
1282
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001283static void
1284handle_surface_destroy(struct wlsc_listener *listener,
1285 struct wlsc_surface *surface)
1286{
1287 struct wlsc_input_device *device =
1288 container_of(listener, struct wlsc_input_device, listener);
1289
1290 if (device->grab_surface == surface) {
1291 device->grab_surface = NULL;
1292 device->grab = 0;
1293 }
1294 if (device->keyboard_focus == surface)
1295 device->keyboard_focus = NULL;
1296 if (device->pointer_focus == surface)
1297 device->pointer_focus = NULL;
1298}
1299
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001300static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001301create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001302{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001303 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001304
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001305 device = malloc(sizeof *device);
1306 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001307 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001308
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001309 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001310 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001311 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001312 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -05001313 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001314 device->x = 100;
1315 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001316 device->ec = ec;
1317
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001318 device->listener.func = handle_surface_destroy;
1319 wl_list_insert(ec->surface_destroy_listener_list.prev,
1320 &device->listener.link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001321 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001322
1323 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001324}
1325
1326void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05001327wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001328{
1329 *x = device->x;
1330 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001331}
1332
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001333static void
1334post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001335{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001336 struct wlsc_output *output =
1337 container_of(global, struct wlsc_output, base);
1338
1339 wl_client_post_event(client, global,
1340 WL_OUTPUT_GEOMETRY,
1341 output->width, output->height);
1342}
1343
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001344static void
1345on_drm_input(int fd, uint32_t mask, void *data)
1346{
1347 drmEventContext evctx;
1348
1349 memset(&evctx, 0, sizeof evctx);
1350 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1351 evctx.page_flip_handler = page_flip_handler;
1352 drmHandleEvent(fd, &evctx);
1353}
1354
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001355static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001356init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001357{
1358 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001359 EGL_DEPTH_SIZE, 0,
1360 EGL_STENCIL_SIZE, 0,
1361 EGL_CONFIG_CAVEAT, EGL_NONE,
1362 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001363 EGL_NONE
1364 };
1365
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001366 struct wl_event_loop *loop;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001367 EGLint major, minor;
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001368 int fd;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001369
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -05001370 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001371 if (ec->display == NULL) {
1372 fprintf(stderr, "failed to create display\n");
1373 return -1;
1374 }
1375
1376 if (!eglInitialize(ec->display, &major, &minor)) {
1377 fprintf(stderr, "failed to initialize display\n");
1378 return -1;
1379 }
1380
1381 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
1382 return -1;
1383
1384 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
1385 if (ec->context == NULL) {
1386 fprintf(stderr, "failed to create context\n");
1387 return -1;
1388 }
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001389
1390 loop = wl_display_get_event_loop(ec->wl_display);
1391 fd = eglGetDisplayFD(ec->display);
1392 ec->drm_source =
1393 wl_event_loop_add_fd(loop, fd,
1394 WL_EVENT_READABLE, on_drm_input, ec);
1395
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001396 return 0;
1397}
1398
1399static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001400create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001401{
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001402 drmModeConnector *connector;
1403 drmModeRes *resources;
1404 drmModeEncoder *encoder;
Kristian Høgsberg41a10682009-02-15 22:37:03 -05001405 drmModeModeInfo *mode;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001406 struct wlsc_output *output;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001407 uint32_t name, handle, stride;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001408 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001409
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001410 if (ec->display == NULL && init_egl(ec, device) < 0) {
1411 fprintf(stderr, "failed to initialize egl\n");
1412 return -1;
1413 }
1414
1415 output = malloc(sizeof *output);
1416 if (output == NULL)
1417 return -1;
1418
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001419 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001420 resources = drmModeGetResources(fd);
1421 if (!resources) {
1422 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001423 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001424 }
1425
1426 for (i = 0; i < resources->count_connectors; i++) {
1427 connector = drmModeGetConnector(fd, resources->connectors[i]);
1428 if (connector == NULL)
1429 continue;
1430
1431 if (connector->connection == DRM_MODE_CONNECTED &&
1432 connector->count_modes > 0)
1433 break;
1434
1435 drmModeFreeConnector(connector);
1436 }
1437
1438 if (i == resources->count_connectors) {
1439 fprintf(stderr, "No currently active connector found.\n");
1440 return -1;
1441 }
1442
1443 mode = &connector->modes[0];
1444
1445 for (i = 0; i < resources->count_encoders; i++) {
1446 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1447
1448 if (encoder == NULL)
1449 continue;
1450
1451 if (encoder->encoder_id == connector->encoder_id)
1452 break;
1453
1454 drmModeFreeEncoder(encoder);
1455 }
1456
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001457 output->compositor = ec;
1458 output->crtc_id = encoder->crtc_id;
1459 output->connector_id = connector->connector_id;
1460 output->mode = mode;
1461 output->x = 0;
1462 output->y = 0;
1463 output->width = mode->hdisplay;
1464 output->height = mode->vdisplay;
1465
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001466 output->surface = eglCreateSurface(ec->display,
1467 ec->config,
1468 output->width,
1469 output->height,
1470 2, NULL);
Kristian Høgsbergb22382b2009-03-10 23:40:35 -04001471 if (output->surface == NULL) {
1472 fprintf(stderr, "failed to create surface\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001473 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001474 }
1475
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001476 for (i = 0; i < 2; i++) {
1477 eglGetColorBuffer(output->surface,
1478 i, &name, &handle, &stride);
1479
1480 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
1481 32, 32, stride, handle, &output->fb_id[i]);
1482 if (ret) {
1483 fprintf(stderr, "failed to add fb %d: %m\n", i);
1484 return -1;
1485 }
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001486 }
1487
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001488 output->current = 0;
1489 ret = drmModeSetCrtc(fd, encoder->crtc_id,
1490 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001491 &connector->connector_id, 1, mode);
1492 if (ret) {
1493 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001494 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001495 }
1496
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001497 output->base.interface = &wl_output_interface;
1498 wl_display_add_object(ec->wl_display, &output->base);
1499 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1500 wl_list_insert(ec->output_list.prev, &output->link);
1501
1502 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1503 fprintf(stderr, "failed to make context current\n");
1504 return -1;
1505 }
1506
1507 output->background = background_create(output, option_background);
1508
1509 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001510}
1511
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001512static const struct wl_interface visual_interface = {
1513 "visual", 1,
1514};
1515
1516static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001517add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001518{
1519 ec->argb_visual.base.interface = &visual_interface;
1520 ec->argb_visual.base.implementation = NULL;
1521 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001522 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001523
1524 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1525 ec->premultiplied_argb_visual.base.implementation = NULL;
1526 wl_display_add_object(ec->wl_display,
1527 &ec->premultiplied_argb_visual.base);
1528 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001529 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001530
1531 ec->rgb_visual.base.interface = &visual_interface;
1532 ec->rgb_visual.base.implementation = NULL;
1533 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001534 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1535}
1536
Ray Strode19ad6a92008-12-19 01:45:41 -05001537static void on_enter_vt(int signal_number, void *data)
1538{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001539 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001540 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001541 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001542
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001543 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001544 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001545
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001546 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001547 output = container_of(ec->output_list.next, struct wlsc_output, link);
1548 while (&output->link != &ec->output_list) {
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001549 ret = drmModeSetCrtc(fd, output->crtc_id,
1550 output->fb_id[output->current], 0, 0,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001551 &output->connector_id, 1, output->mode);
1552 if (ret)
1553 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1554 output->connector_id);
1555
1556 output = container_of(output->link.next,
1557 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001558 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001559}
1560
1561static void on_leave_vt(int signal_number, void *data)
1562{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001563 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001564
1565 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001566 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001567}
1568
Ray Strode966aa112008-12-19 14:28:02 -05001569static void
1570on_tty_input(int fd, uint32_t mask, void *data)
1571{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001572 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001573
1574 /* Ignore input to tty. We get keyboard events from evdev
1575 */
1576 tcflush(ec->tty_fd, TCIFLUSH);
1577}
1578
1579static void on_term_signal(int signal_number, void *data)
1580{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001581 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001582
1583 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1584 fprintf(stderr, "could not restore terminal to canonical mode\n");
1585
1586 exit(0);
1587}
1588
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001589static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001590{
1591 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001592 struct vt_mode mode = { 0 };
1593
1594 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1595 if (ec->tty_fd <= 0) {
1596 fprintf(stderr, "failed to open active tty: %m\n");
1597 return -1;
1598 }
Ray Strode966aa112008-12-19 14:28:02 -05001599
1600 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1601 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001602 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001603 }
1604
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001605 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001606 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001607 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001608
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001609 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001610 raw_attributes.c_oflag |= OPOST | OCRNL;
1611
1612 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1613 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1614
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001615 ec->term_signal_source =
1616 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001617
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001618 ec->tty_input_source =
1619 wl_event_loop_add_fd(loop, ec->tty_fd,
1620 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001621
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001622 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001623 mode.mode = VT_PROCESS;
1624 mode.relsig = SIGUSR1;
1625 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001626 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001627 fprintf(stderr, "failed to take control of vt handling\n");
1628 }
1629
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001630 ec->leave_vt_source =
1631 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1632 ec->enter_vt_source =
1633 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001634
1635 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001636}
1637
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001638static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001639init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001640{
1641 struct udev_enumerate *e;
1642 struct udev_list_entry *entry;
1643 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001644 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001645 struct wlsc_input_device *input_device;
1646
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001647 ec->udev = udev_new();
1648 if (ec->udev == NULL) {
1649 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001650 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001651 }
1652
1653 input_device = create_input_device(ec);
1654
1655 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001656 udev_enumerate_add_match_subsystem(e, "input");
1657 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001658 udev_enumerate_scan_devices(e);
1659 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1660 path = udev_list_entry_get_name(entry);
1661 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001662 evdev_input_device_create(input_device, ec->wl_display,
1663 udev_device_get_devnode(device));
1664 }
1665 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001666
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001667 e = udev_enumerate_new(ec->udev);
1668 udev_enumerate_add_match_subsystem(e, "drm");
1669 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1670 udev_enumerate_scan_devices(e);
1671 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1672 path = udev_list_entry_get_name(entry);
1673 device = udev_device_new_from_syspath(ec->udev, path);
1674 if (create_output(ec, device) < 0) {
1675 fprintf(stderr, "failed to create output for %s\n", path);
1676 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001677 }
1678 }
1679 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001680
1681 /* Create the pointer surface now that we have a current EGL context. */
Kristian Høgsberg0555d8e2009-02-22 19:19:47 -05001682 input_device->sprite =
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001683 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1684
1685 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001686}
1687
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001688static struct wlsc_compositor *
1689wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001690{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001691 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001692 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001693 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001694
1695 ec = malloc(sizeof *ec);
1696 if (ec == NULL)
1697 return NULL;
1698
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001699 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001700 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001701
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001702 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001703
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001704 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001705
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001706 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001707 wl_list_init(&ec->input_device_list);
1708 wl_list_init(&ec->output_list);
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001709 wl_list_init(&ec->surface_destroy_listener_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001710 if (init_libudev(ec) < 0) {
1711 fprintf(stderr, "failed to initialize devices\n");
1712 return NULL;
1713 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001714
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001715 shooter = screenshooter_create(ec);
1716 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001717 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001718
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001719 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001720
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001721 setup_tty(ec, loop);
1722
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001723 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001724 wlsc_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001725
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001726 wl_list_init(&ec->animate_list);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001727 wl_list_init(&ec->session_list);
Kristian Høgsberg1a208d52009-02-10 14:20:26 -05001728
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001729 return ec;
1730}
1731
1732/* The plan here is to generate a random anonymous socket name and
1733 * advertise that through a service on the session dbus.
1734 */
1735static const char socket_name[] = "\0wayland";
1736
1737int main(int argc, char *argv[])
1738{
1739 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001740 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001741 GError *error = NULL;
1742 GOptionContext *context;
1743
1744 context = g_option_context_new(NULL);
1745 g_option_context_add_main_entries(context, option_entries, "Wayland");
1746 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1747 fprintf(stderr, "option parsing failed: %s\n", error->message);
1748 exit(EXIT_FAILURE);
1749 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001750
1751 display = wl_display_create();
1752
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001753 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001754 if (ec == NULL) {
1755 fprintf(stderr, "failed to create compositor\n");
1756 exit(EXIT_FAILURE);
1757 }
1758
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001759 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001760 fprintf(stderr, "failed to add socket: %m\n");
1761 exit(EXIT_FAILURE);
1762 }
1763
1764 wl_display_run(display);
1765
1766 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001767}