blob: abbf99ae7c29d0e6faa41f08a80f926c00769afa [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>
Ray Strode966aa112008-12-19 14:28:02 -050021#include <stdbool.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040022#include <stdlib.h>
23#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050024#include <stdarg.h>
Ray Strode966aa112008-12-19 14:28:02 -050025#include <termios.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040026#include <i915_drm.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050031#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050032#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050033#include <gdk-pixbuf/gdk-pixbuf.h>
34#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050035#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050037#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050038#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050039#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050040#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050041#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050042#include <fnmatch.h>
43#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044
Kristian Høgsberg890bc052008-12-30 14:31:33 -050045#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
46#include <libudev.h>
47
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040048#include <GL/gl.h>
49#include <eagle.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include "wayland.h"
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050052#include "wayland-protocol.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053#include "cairo-util.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050054#include "wayland-system-compositor.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040056#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
57
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -050058struct wlsc_matrix {
59 GLdouble d[16];
60};
61
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050062struct wl_visual {
63 struct wl_object base;
64};
65
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050066struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050067 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050068 struct wl_list link;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050069 struct wlsc_compositor *ec;
70 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050071 EGLSurface surface;
72 int32_t x, y, width, height, stride;
73
74 struct drm_mode_modeinfo *mode;
75 uint32_t fb_id;
76 uint32_t crtc_id;
77 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050078};
79
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050080struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050081 struct wl_object base;
82 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050083 struct wlsc_compositor *ec;
84 struct wlsc_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050085 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050086
87 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050088 struct wlsc_surface *grab_surface;
89 struct wlsc_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050090};
91
Kristian Høgsberg8e438622009-01-26 23:07:00 -050092struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040093 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050094 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
95
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040096 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040097 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050098 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040099 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500100
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500101 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500102 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500103 struct wl_list surface_list;
104
Ray Strode966aa112008-12-19 14:28:02 -0500105 struct wl_event_source *term_signal_source;
106
107 /* tty handling state */
108 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500109 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500110
111 struct termios terminal_attributes;
112 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500113 struct wl_event_source *enter_vt_source;
114 struct wl_event_source *leave_vt_source;
115
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500116 struct udev *udev;
117
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500118 /* Repaint state. */
119 struct wl_event_source *timer_source;
120 int repaint_needed;
121 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500122 struct timespec previous_swap;
123 uint32_t current_frame;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500124
125 uint32_t meta_state;
126 GLdouble grab_x_angle, grab_y_angle;
127 int32_t grab_x, grab_y;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400128};
129
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500130#define META_DOWN 256
131
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500132struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500133 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500134 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500135 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400136 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400137 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500138 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500139 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500140 struct wl_list link;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500141 GLdouble scale, x_angle, y_angle;
142 struct wlsc_matrix matrix;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400143};
144
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500145static const char *option_background = "background.jpg";
146
147static const GOptionEntry option_entries[] = {
148 { "background", 'b', 0, G_OPTION_ARG_STRING,
149 &option_background, "Background image" },
150 { NULL }
151};
152
153
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500154struct screenshooter {
155 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500156 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500157};
158
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500159struct screenshooter_interface {
160 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
161};
162
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500163static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500164screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500165{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500166 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500167 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500168 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500169 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500170 GError *error = NULL;
171 void *data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500172 int i;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500173
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500174 i = 0;
175 output = container_of(ec->output_list.next, struct wlsc_output, link);
176 while (&output->link != &ec->output_list) {
177 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500178 data = malloc(output->width * output->height * 4);
179 if (data == NULL) {
180 fprintf(stderr, "couldn't allocate image buffer\n");
181 continue;
182 }
183
184 glReadBuffer(GL_FRONT);
185 glPixelStorei(GL_PACK_ALIGNMENT, 1);
186 glReadPixels(0, 0, output->width, output->height,
187 GL_RGBA, GL_UNSIGNED_BYTE, data);
188
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500189 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500190 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500191 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500192 normal = gdk_pixbuf_flip(pixbuf, FALSE);
193 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
194 gdk_pixbuf_unref(normal);
195 gdk_pixbuf_unref(pixbuf);
196 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500197
198 output = container_of(output->link.next,
199 struct wlsc_output, link);
200 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500201}
202
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500203static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500204 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500205};
206
207static const struct wl_interface screenshooter_interface = {
208 "screenshooter", 1,
209 ARRAY_LENGTH(screenshooter_methods),
210 screenshooter_methods,
211};
212
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500213struct screenshooter_interface screenshooter_implementation = {
214 screenshooter_shoot
215};
216
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500217static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500218screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500219{
220 struct screenshooter *shooter;
221
222 shooter = malloc(sizeof *shooter);
223 if (shooter == NULL)
224 return NULL;
225
226 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500227 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500228 shooter->ec = ec;
229
230 return shooter;
231};
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500232
233static void
234wlsc_matrix_init(struct wlsc_matrix *matrix)
235{
236 static const struct wlsc_matrix identity = {
237 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
238 };
239
240 memcpy(matrix, &identity, sizeof identity);
241}
242
243static void
244wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
245{
246 struct wlsc_matrix tmp;
247 const GLdouble *row, *column;
248 div_t d;
249 int i, j;
250
251 for (i = 0; i < 16; i++) {
252 tmp.d[i] = 0;
253 d = div(i, 4);
254 row = m->d + d.quot * 4;
255 column = n->d + d.rem;
256 for (j = 0; j < 4; j++)
257 tmp.d[i] += row[j] * column[j * 4];
258 }
259 memcpy(m, &tmp, sizeof tmp);
260}
261
262static void
263wlsc_matrix_translate(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
264{
265 struct wlsc_matrix translate = {
266 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
267 };
268
269 wlsc_matrix_multiply(matrix, &translate);
270}
271
272static void
273wlsc_matrix_scale(struct wlsc_matrix *matrix, GLdouble x, GLdouble y, GLdouble z)
274{
275 struct wlsc_matrix scale = {
276 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
277 };
278
279 wlsc_matrix_multiply(matrix, &scale);
280}
281
282static void
283wlsc_matrix_rotate(struct wlsc_matrix *matrix,
284 GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
285{
286 GLdouble c = cos(angle);
287 GLdouble s = sin(angle);
288 struct wlsc_matrix rotate = {
289 { x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
290 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0,
291 x * z * (1 - c) + y * x, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
292 0, 0, 0, 1 }
293 };
294
295 wlsc_matrix_multiply(matrix, &rotate);
296}
297
298static void
299wlsc_surface_update_matrix(struct wlsc_surface *es)
300{
301 GLdouble tx, ty;
302
303 tx = es->map.x + es->map.width / 2;
304 ty = es->map.y + es->map.height / 2;
305
306 wlsc_matrix_init(&es->matrix);
307 wlsc_matrix_translate(&es->matrix, -tx, -ty, 0);
308 wlsc_matrix_scale(&es->matrix, es->scale, es->scale, 1);
309 wlsc_matrix_rotate(&es->matrix, es->x_angle, 0, 1, 0);
310 wlsc_matrix_rotate(&es->matrix, es->y_angle, 1, 0, 0);
311 wlsc_matrix_translate(&es->matrix, tx, ty, 0);
312}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500313
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500314static struct wlsc_surface *
315wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500316 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500317 int x, int y, int width, int height)
318{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500319 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500320 int stride;
321 void *data;
322
323 stride = cairo_image_surface_get_stride(surface);
324 data = cairo_image_surface_get_data(surface);
325
326 es = malloc(sizeof *es);
327 if (es == NULL)
328 return NULL;
329
330 glGenTextures(1, &es->texture);
331 glBindTexture(GL_TEXTURE_2D, es->texture);
332 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500333 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
334 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500336 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
337 GL_BGRA, GL_UNSIGNED_BYTE, data);
338
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500339 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500340 es->map.x = x;
341 es->map.y = y;
342 es->map.width = width;
343 es->map.height = height;
344 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500345 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500346 es->scale = 1;
347 es->x_angle = 0;
348 es->y_angle = 0;
349
350 wlsc_surface_update_matrix(es);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500351
352 return es;
353}
354
355static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500356wlsc_surface_destroy(struct wlsc_surface *es, struct wlsc_compositor *ec)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500357{
358 glDeleteTextures(1, &es->texture);
359 if (es->surface != EGL_NO_SURFACE)
360 eglDestroySurface(ec->display, es->surface);
361 free(es);
362}
363
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400364static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500365pointer_path(cairo_t *cr, int x, int y)
366{
367 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
368 const int width = 16, height = 16;
369
370 cairo_move_to(cr, x, y);
371 cairo_line_to(cr, x + tx, y + ty);
372 cairo_line_to(cr, x + dx, y + dy);
373 cairo_line_to(cr, x + width - end, y + height);
374 cairo_line_to(cr, x + width, y + height - end);
375 cairo_line_to(cr, x + dy, y + dx);
376 cairo_line_to(cr, x + ty, y + tx);
377 cairo_close_path(cr);
378}
379
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500380static struct wlsc_surface *
381pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500382{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500383 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500384 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500385 cairo_surface_t *surface;
386 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500387
388 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
389 width, height);
390
391 cr = cairo_create(surface);
392 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500393 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500394 cairo_set_source_rgb(cr, 0, 0, 0);
395 cairo_stroke_preserve(cr);
396 cairo_fill(cr);
397 blur_surface(surface, width);
398
399 pointer_path(cr, hotspot_x, hotspot_y);
400 cairo_stroke_preserve(cr);
401 cairo_set_source_rgb(cr, 1, 1, 1);
402 cairo_fill(cr);
403 cairo_destroy(cr);
404
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500405 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500406 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500407 x - hotspot_x,
408 y - hotspot_y,
409 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500410
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500411 cairo_surface_destroy(surface);
412
Kristian Høgsberg54879822008-11-23 17:07:32 -0500413 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500414}
415
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500416static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500417background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500418{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500419 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500420 GdkPixbuf *pixbuf;
421 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500422 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500423 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424
425 background = malloc(sizeof *background);
426 if (background == NULL)
427 return NULL;
428
429 g_type_init();
430
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500431 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500432 output->width,
433 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500434 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500435 if (error != NULL) {
436 free(background);
437 return NULL;
438 }
439
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500440 data = gdk_pixbuf_get_pixels(pixbuf);
441
442 glGenTextures(1, &background->texture);
443 glBindTexture(GL_TEXTURE_2D, background->texture);
444 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500445 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500448
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500449 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500450 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500451 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500452 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500453
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500454 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
455 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500456 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500457
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500458 background->compositor = output->ec;
459 background->map.x = output->x;
460 background->map.y = output->y;
461 background->map.width = output->width;
462 background->map.height = output->height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500463 background->surface = EGL_NO_SURFACE;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500464 background->visual = &output->ec->rgb_visual;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500465 background->scale = 1;
466 background->x_angle = 0;
467 background->y_angle = 0;
468 wlsc_surface_update_matrix(background);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500469
470 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500471}
472
473static void
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500474wlsc_surface_draw(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500475{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500476 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500477 GLint vertices[12];
478 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
479 GLuint indices[4] = { 0, 1, 2, 3 };
480
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500481 vertices[0] = es->map.x;
482 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500483 vertices[2] = 0;
484
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500485 vertices[3] = es->map.x;
486 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500487 vertices[5] = 0;
488
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500489 vertices[6] = es->map.x + es->map.width;
490 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500491 vertices[8] = 0;
492
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500493 vertices[9] = es->map.x + es->map.width;
494 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500495 vertices[11] = 0;
496
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500497 if (es->visual == &ec->argb_visual) {
498 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
499 glEnable(GL_BLEND);
500 } else if (es->visual == &ec->premultiplied_argb_visual) {
501 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
502 glEnable(GL_BLEND);
503 } else {
504 glDisable(GL_BLEND);
505 }
506
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500507 glPushMatrix();
508 glMultMatrixd(es->matrix.d);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500509 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500510 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500511 glEnableClientState(GL_VERTEX_ARRAY);
512 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
513 glVertexPointer(3, GL_INT, 0, vertices);
514 glTexCoordPointer(2, GL_INT, 0, tex_coords);
515 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500516 glPopMatrix();
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500517}
518
519static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500520repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400521{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500522 struct wlsc_compositor *ec = output->ec;
523 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500524 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500525 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500526
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500527 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
528 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500529 return;
530 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500531
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500532 glViewport(0, 0, output->width, output->height);
533 glMatrixMode(GL_PROJECTION);
534 glLoadIdentity();
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500535 glFrustum(-output->width / s, output->width / s,
536 output->height / s, -output->height / s, 1, 2 * s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500537 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500538 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500539 glClearColor(0, 0, 0.2, 1);
540
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500541 glTranslatef(-output->width / 2, -output->height / 2, -s / 2);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500542
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500543 if (output->background)
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500544 wlsc_surface_draw(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500545 else
546 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400547
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500548 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500549 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500550 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500551 wlsc_surface_draw(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500552 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500553 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400554 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400555
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500556 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500557 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500558 while (&eid->link != &ec->input_device_list) {
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500559 wlsc_surface_draw(eid->pointer_surface);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500560
561 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500562 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500563 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500564
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500565 eglSwapBuffers(ec->display, output->surface);
566}
567
568static void
569repaint(void *data)
570{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500571 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500572 struct wlsc_output *output;
573 struct timespec ts;
574 uint32_t msecs;
575
576 if (!ec->repaint_needed) {
577 ec->repaint_on_timeout = 0;
578 return;
579 }
580
581 output = container_of(ec->output_list.next, struct wlsc_output, link);
582 while (&output->link != &ec->output_list) {
583 repaint_output(output);
584 output = container_of(output->link.next,
585 struct wlsc_output, link);
586 }
587
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500588 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500589
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500590 clock_gettime(CLOCK_MONOTONIC, &ts);
591 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500592 wl_display_post_frame(ec->wl_display, &ec->base,
593 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500594 ec->current_frame++;
595
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500596 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500597 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400598}
599
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500600static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500601schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400602{
603 struct wl_event_loop *loop;
604
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500605 ec->repaint_needed = 1;
606 if (!ec->repaint_on_timeout) {
607 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500608 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500609 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400610}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500611
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500612static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500613surface_destroy(struct wl_client *client,
614 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400615{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500616 struct wlsc_surface *es = (struct wlsc_surface *) surface;
617 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500618
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500619 wl_list_remove(&es->link);
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500620 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400621
622 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400623}
624
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500625static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500626surface_attach(struct wl_client *client,
627 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500628 uint32_t width, uint32_t height, uint32_t stride,
629 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400630{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500631 struct wlsc_surface *es = (struct wlsc_surface *) surface;
632 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400633
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500634 if (es->surface != EGL_NO_SURFACE)
635 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400636
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500637 es->width = width;
638 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500639 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500640 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500641 if (visual == &ec->argb_visual.base)
642 es->visual = &ec->argb_visual;
643 else if (visual == &ec->premultiplied_argb_visual.base)
644 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500645 else if (visual == &ec->rgb_visual.base)
646 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500647 else
648 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400649
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500650 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400651 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500652 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
653 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
654 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500655 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400656}
657
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500658static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500659surface_map(struct wl_client *client,
660 struct wl_surface *surface,
661 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400662{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500663 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400664
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500665 es->map.x = x;
666 es->map.y = y;
667 es->map.width = width;
668 es->map.height = height;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500669 wlsc_surface_update_matrix(es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400670}
671
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500672static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500673surface_copy(struct wl_client *client,
674 struct wl_surface *surface,
675 int32_t dst_x, int32_t dst_y,
676 uint32_t name, uint32_t stride,
677 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500678{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500679 struct wlsc_surface *es = (struct wlsc_surface *) surface;
680 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500681 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500682
683 /* FIXME: glCopyPixels should work, but then we'll have to
684 * call eglMakeCurrent to set up the src and dest surfaces
685 * first. This seems cheaper, but maybe there's a better way
686 * to accomplish this. */
687
688 src = eglCreateSurfaceForName(ec->display, ec->config,
689 name, x + width, y + height, stride, NULL);
690
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500691 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500692 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500693 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500694}
695
696static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500697surface_damage(struct wl_client *client,
698 struct wl_surface *surface,
699 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500700{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500701 /* FIXME: This need to take a damage region, of course. */
702}
703
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500704const static struct wl_surface_interface surface_interface = {
705 surface_destroy,
706 surface_attach,
707 surface_map,
708 surface_copy,
709 surface_damage
710};
711
712static void
713compositor_create_surface(struct wl_client *client,
714 struct wl_compositor *compositor, uint32_t id)
715{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500716 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
717 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500718
719 es = malloc(sizeof *es);
720 if (es == NULL)
721 /* FIXME: Send OOM event. */
722 return;
723
724 es->compositor = ec;
725 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500726 es->scale = 1;
727 es->x_angle = 0;
728 es->y_angle = 0;
729
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500730 wl_list_insert(ec->surface_list.prev, &es->link);
731 glGenTextures(1, &es->texture);
732 wl_client_add_surface(client, &es->base,
733 &surface_interface, id);
734}
735
736static void
737compositor_commit(struct wl_client *client,
738 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500739{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500740 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500741
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500742 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500743 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500744}
745
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500746const static struct wl_compositor_interface compositor_interface = {
747 compositor_create_surface,
748 compositor_commit
749};
750
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500751static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500752pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500753{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500754 struct wlsc_compositor *ec = device->ec;
755 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500756
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500757 if (device->grab > 0)
758 return device->grab_surface;
759
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500760 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500761 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500762 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763 if (es->map.x <= device->x &&
764 device->x < es->map.x + es->map.width &&
765 es->map.y <= device->y &&
766 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500767 return es;
768
769 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500770 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500771
772 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500773 *sx = (device->x - es->map.x) * es->width / es->map.width;
774 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500775 }
776
777 return NULL;
778}
779
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500780void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500781notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500782{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500783 struct wlsc_surface *es;
784 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500785 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500786 const int hotspot_x = 16, hotspot_y = 16;
787 int32_t sx, sy;
788
Ray Strodee96dcb82008-12-20 02:00:49 -0500789 if (!ec->vt_active)
790 return;
791
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500792 if (ec->meta_state && device->focus_surface) {
793 es = device->focus_surface;
794 es->x_angle = ec->grab_x_angle + (x - ec->grab_x) / 50.0;
795 es->y_angle = ec->grab_y_angle + (y - ec->grab_y) / 50.0;
796 wlsc_surface_update_matrix(es);
797 }
798
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500799 /* FIXME: We need some multi head love here. */
800 output = container_of(ec->output_list.next, struct wlsc_output, link);
801 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500802 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500803 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500804 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500805 if (x >= output->x + output->width)
806 x = output->x + output->width - 1;
807 if (y >= output->y + output->height)
808 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500809
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500810 device->x = x;
811 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500812 es = pick_surface(device, &sx, &sy);
813 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500814 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500815 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500816
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500817 device->pointer_surface->map.x = x - hotspot_x;
818 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500819
820 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500821}
822
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500823void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500824notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500825 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500826{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500827 struct wlsc_surface *es;
828 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500829 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500830
Ray Strodee96dcb82008-12-20 02:00:49 -0500831 if (!ec->vt_active)
832 return;
833
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500834 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500835 if (es) {
836 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500837 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500838
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500839 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500840 /* FIXME: We need callbacks when the surfaces
841 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500842 device->grab++;
843 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500844 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500845 } else {
846 device->grab--;
847 }
848
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500849 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500850 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500851 WL_INPUT_BUTTON, button, state,
852 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500853
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500854 schedule_repaint(device->ec);
855 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500856}
857
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500858static void on_term_signal(int signal_number, void *data);
859
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500860void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500861notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500862 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500863{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500864 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500865 struct wlsc_surface *es;
Ray Strodee96dcb82008-12-20 02:00:49 -0500866
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500867 switch (key | ec->meta_state) {
868 case KEY_EJECTCD | META_DOWN:
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500869 on_term_signal(SIGTERM, ec);
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -0500870 return;
871
872 case KEY_ESC | META_DOWN:
873 if (state == 0 || device->focus_surface == NULL)
874 break;
875
876 es = device->focus_surface;
877 if (es->scale < 1.5)
878 es->scale = 2;
879 else
880 es->scale = 1;
881 wlsc_surface_update_matrix(es);
882 schedule_repaint(device->ec);
883 return;
884
885 case KEY_LEFTMETA:
886 case KEY_RIGHTMETA:
887 if (device->focus_surface) {
888 ec->grab_x_angle = device->focus_surface->x_angle;
889 ec->grab_y_angle = device->focus_surface->y_angle;
890 ec->grab_x = device->x;
891 ec->grab_y = device->y;
892 }
893 case KEY_LEFTMETA | META_DOWN:
894 case KEY_RIGHTMETA | META_DOWN:
895 ec->meta_state = state ? META_DOWN : 0;
896 return;
897 }
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500898
Ray Strodee96dcb82008-12-20 02:00:49 -0500899 if (!ec->vt_active)
900 return;
901
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500902 if (device->focus_surface != NULL)
903 wl_surface_post_event(&device->focus_surface->base,
904 &device->base,
905 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500906}
907
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500908struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500909evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500910 struct wl_display *display, const char *path);
911
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500912static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500913create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500914{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500915 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500916
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500917 device = malloc(sizeof *device);
918 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500919 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500920
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500921 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500922 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500923 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500924 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500925 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500926 device->x = 100;
927 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500928 device->ec = ec;
929
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500930 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500931
932 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500933}
934
935void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500936wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500937{
938 *x = device->x;
939 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500940}
941
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500942static void
943post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500944{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500945 struct wlsc_output *output =
946 container_of(global, struct wlsc_output, base);
947
948 wl_client_post_event(client, global,
949 WL_OUTPUT_GEOMETRY,
950 output->width, output->height);
951}
952
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500953static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500954init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500955{
956 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500957 EGL_DEPTH_SIZE, 0,
958 EGL_STENCIL_SIZE, 0,
959 EGL_CONFIG_CAVEAT, EGL_NONE,
960 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500961 EGL_NONE
962 };
963
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500964 EGLint major, minor;
965
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500966 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500967 if (ec->display == NULL) {
968 fprintf(stderr, "failed to create display\n");
969 return -1;
970 }
971
972 if (!eglInitialize(ec->display, &major, &minor)) {
973 fprintf(stderr, "failed to initialize display\n");
974 return -1;
975 }
976
977 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
978 return -1;
979
980 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
981 if (ec->context == NULL) {
982 fprintf(stderr, "failed to create context\n");
983 return -1;
984 }
985 return 0;
986}
987
988static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500989create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500990{
991 const static EGLint surface_attribs[] = {
992 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
993 EGL_NONE
994 };
995
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500996 drmModeConnector *connector;
997 drmModeRes *resources;
998 drmModeEncoder *encoder;
999 struct drm_mode_modeinfo *mode;
1000 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001001 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001002 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001003 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001004
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001005 if (ec->display == NULL && init_egl(ec, device) < 0) {
1006 fprintf(stderr, "failed to initialize egl\n");
1007 return -1;
1008 }
1009
1010 output = malloc(sizeof *output);
1011 if (output == NULL)
1012 return -1;
1013
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001014 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001015 resources = drmModeGetResources(fd);
1016 if (!resources) {
1017 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001018 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001019 }
1020
1021 for (i = 0; i < resources->count_connectors; i++) {
1022 connector = drmModeGetConnector(fd, resources->connectors[i]);
1023 if (connector == NULL)
1024 continue;
1025
1026 if (connector->connection == DRM_MODE_CONNECTED &&
1027 connector->count_modes > 0)
1028 break;
1029
1030 drmModeFreeConnector(connector);
1031 }
1032
1033 if (i == resources->count_connectors) {
1034 fprintf(stderr, "No currently active connector found.\n");
1035 return -1;
1036 }
1037
1038 mode = &connector->modes[0];
1039
1040 for (i = 0; i < resources->count_encoders; i++) {
1041 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1042
1043 if (encoder == NULL)
1044 continue;
1045
1046 if (encoder->encoder_id == connector->encoder_id)
1047 break;
1048
1049 drmModeFreeEncoder(encoder);
1050 }
1051
1052 /* Mode size at 32 bpp */
1053 create.size = mode->hdisplay * mode->vdisplay * 4;
1054 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
1055 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001056 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001057 }
1058
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001059 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001060 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001061 if (ret) {
1062 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001063 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001064 }
1065
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001066 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001067 &connector->connector_id, 1, mode);
1068 if (ret) {
1069 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001070 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001071 }
1072
1073 flink.handle = create.handle;
1074 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
1075 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001076 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001077 }
1078
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001079 output->ec = ec;
1080 output->crtc_id = encoder->crtc_id;
1081 output->connector_id = connector->connector_id;
1082 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -05001083 output->x = 0;
1084 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001085 output->width = mode->hdisplay;
1086 output->height = mode->vdisplay;
1087 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001088
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001089 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
1090 flink.name,
1091 output->width, output->height,
1092 output->stride,
1093 surface_attribs);
1094 if (output->surface == NULL) {
1095 fprintf(stderr, "failed to create surface\n");
1096 return -1;
1097 }
1098
1099 output->base.interface = &wl_output_interface;
1100 wl_display_add_object(ec->wl_display, &output->base);
1101 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
1102 wl_list_insert(ec->output_list.prev, &output->link);
1103
1104 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
1105 fprintf(stderr, "failed to make context current\n");
1106 return -1;
1107 }
1108
1109 output->background = background_create(output, option_background);
1110
1111 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001112}
1113
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001114static const struct wl_interface visual_interface = {
1115 "visual", 1,
1116};
1117
1118static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001119add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001120{
1121 ec->argb_visual.base.interface = &visual_interface;
1122 ec->argb_visual.base.implementation = NULL;
1123 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001124 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001125
1126 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1127 ec->premultiplied_argb_visual.base.implementation = NULL;
1128 wl_display_add_object(ec->wl_display,
1129 &ec->premultiplied_argb_visual.base);
1130 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001131 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001132
1133 ec->rgb_visual.base.interface = &visual_interface;
1134 ec->rgb_visual.base.implementation = NULL;
1135 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001136 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1137}
1138
Ray Strode19ad6a92008-12-19 01:45:41 -05001139static void on_enter_vt(int signal_number, void *data)
1140{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001141 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001142 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001143 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -05001144
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001145 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -05001146 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001147
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001148 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001149 output = container_of(ec->output_list.next, struct wlsc_output, link);
1150 while (&output->link != &ec->output_list) {
1151 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1152 &output->connector_id, 1, output->mode);
1153 if (ret)
1154 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1155 output->connector_id);
1156
1157 output = container_of(output->link.next,
1158 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001159 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001160}
1161
1162static void on_leave_vt(int signal_number, void *data)
1163{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001164 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001165
1166 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001167 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001168}
1169
Ray Strode966aa112008-12-19 14:28:02 -05001170static void
1171on_tty_input(int fd, uint32_t mask, void *data)
1172{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001173 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001174
1175 /* Ignore input to tty. We get keyboard events from evdev
1176 */
1177 tcflush(ec->tty_fd, TCIFLUSH);
1178}
1179
1180static void on_term_signal(int signal_number, void *data)
1181{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001182 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001183
1184 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1185 fprintf(stderr, "could not restore terminal to canonical mode\n");
1186
1187 exit(0);
1188}
1189
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001190static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001191{
1192 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001193 struct vt_mode mode = { 0 };
1194
1195 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1196 if (ec->tty_fd <= 0) {
1197 fprintf(stderr, "failed to open active tty: %m\n");
1198 return -1;
1199 }
Ray Strode966aa112008-12-19 14:28:02 -05001200
1201 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1202 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001203 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001204 }
1205
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001206 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001207 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001208 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001209
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001210 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001211 raw_attributes.c_oflag |= OPOST | OCRNL;
1212
1213 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1214 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1215
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001216 ec->term_signal_source =
1217 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001218
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001219 ec->tty_input_source =
1220 wl_event_loop_add_fd(loop, ec->tty_fd,
1221 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001222
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001223 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001224 mode.mode = VT_PROCESS;
1225 mode.relsig = SIGUSR1;
1226 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001227 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001228 fprintf(stderr, "failed to take control of vt handling\n");
1229 }
1230
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001231 ec->leave_vt_source =
1232 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1233 ec->enter_vt_source =
1234 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001235
1236 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001237}
1238
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001239static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001240init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001241{
1242 struct udev_enumerate *e;
1243 struct udev_list_entry *entry;
1244 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001245 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001246 struct wlsc_input_device *input_device;
1247
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001248 ec->udev = udev_new();
1249 if (ec->udev == NULL) {
1250 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001251 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001252 }
1253
1254 input_device = create_input_device(ec);
1255
1256 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001257 udev_enumerate_add_match_subsystem(e, "input");
1258 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001259 udev_enumerate_scan_devices(e);
1260 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1261 path = udev_list_entry_get_name(entry);
1262 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001263 evdev_input_device_create(input_device, ec->wl_display,
1264 udev_device_get_devnode(device));
1265 }
1266 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001267
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001268 e = udev_enumerate_new(ec->udev);
1269 udev_enumerate_add_match_subsystem(e, "drm");
1270 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1271 udev_enumerate_scan_devices(e);
1272 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1273 path = udev_list_entry_get_name(entry);
1274 device = udev_device_new_from_syspath(ec->udev, path);
1275 if (create_output(ec, device) < 0) {
1276 fprintf(stderr, "failed to create output for %s\n", path);
1277 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001278 }
1279 }
1280 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001281
1282 /* Create the pointer surface now that we have a current EGL context. */
1283 input_device->pointer_surface =
1284 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1285
1286 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001287}
1288
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001289static struct wlsc_compositor *
1290wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001291{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001292 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001293 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001294 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001295
1296 ec = malloc(sizeof *ec);
1297 if (ec == NULL)
1298 return NULL;
1299
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001300 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001301 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001302
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001303 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001304
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001305 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001306
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001307 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001308 wl_list_init(&ec->input_device_list);
1309 wl_list_init(&ec->output_list);
1310 if (init_libudev(ec) < 0) {
1311 fprintf(stderr, "failed to initialize devices\n");
1312 return NULL;
1313 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001314
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001315 shooter = screenshooter_create(ec);
1316 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001317 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001318
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001319 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001320
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001321 setup_tty(ec, loop);
1322
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001323 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001324 schedule_repaint(ec);
1325
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001326 return ec;
1327}
1328
1329/* The plan here is to generate a random anonymous socket name and
1330 * advertise that through a service on the session dbus.
1331 */
1332static const char socket_name[] = "\0wayland";
1333
1334int main(int argc, char *argv[])
1335{
1336 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001337 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001338 GError *error = NULL;
1339 GOptionContext *context;
1340
1341 context = g_option_context_new(NULL);
1342 g_option_context_add_main_entries(context, option_entries, "Wayland");
1343 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1344 fprintf(stderr, "option parsing failed: %s\n", error->message);
1345 exit(EXIT_FAILURE);
1346 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001347
1348 display = wl_display_create();
1349
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001350 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001351 if (ec == NULL) {
1352 fprintf(stderr, "failed to create compositor\n");
1353 exit(EXIT_FAILURE);
1354 }
1355
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001356 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001357 fprintf(stderr, "failed to add socket: %m\n");
1358 exit(EXIT_FAILURE);
1359 }
1360
1361 wl_display_run(display);
1362
1363 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001364}