blob: 29d167f48fd0deebb4018180455d4197709a505b [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øgsbergde31d5c2008-12-18 17:55:33 -050058struct wl_visual {
59 struct wl_object base;
60};
61
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050062struct wlsc_output {
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050063 struct wl_object base;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050064 struct wl_list link;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050065 struct wlsc_compositor *ec;
66 struct wlsc_surface *background;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050067 EGLSurface surface;
68 int32_t x, y, width, height, stride;
69
70 struct drm_mode_modeinfo *mode;
71 uint32_t fb_id;
72 uint32_t crtc_id;
73 uint32_t connector_id;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -050074};
75
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050076struct wlsc_input_device {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077 struct wl_object base;
78 int32_t x, y;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050079 struct wlsc_compositor *ec;
80 struct wlsc_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050081 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050082
83 int grab;
Kristian Høgsberg8e438622009-01-26 23:07:00 -050084 struct wlsc_surface *grab_surface;
85 struct wlsc_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050086};
87
Kristian Høgsberg8e438622009-01-26 23:07:00 -050088struct wlsc_compositor {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040089 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050090 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
91
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040092 EGLDisplay display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040093 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050094 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040095 struct wl_display *wl_display;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050096
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -050097 struct wl_list output_list;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050098 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050099 struct wl_list surface_list;
100
Ray Strode966aa112008-12-19 14:28:02 -0500101 struct wl_event_source *term_signal_source;
102
103 /* tty handling state */
104 int tty_fd;
Ray Strodee96dcb82008-12-20 02:00:49 -0500105 uint32_t vt_active : 1;
Ray Strode966aa112008-12-19 14:28:02 -0500106
107 struct termios terminal_attributes;
108 struct wl_event_source *tty_input_source;
Ray Strode19ad6a92008-12-19 01:45:41 -0500109 struct wl_event_source *enter_vt_source;
110 struct wl_event_source *leave_vt_source;
111
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500112 struct udev *udev;
113
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500114 /* Repaint state. */
115 struct wl_event_source *timer_source;
116 int repaint_needed;
117 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500118 struct timespec previous_swap;
119 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400120};
121
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500122struct wlsc_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500123 struct wl_surface base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500124 struct wlsc_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500125 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400126 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400127 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500128 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500129 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500130 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400131};
132
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500133static const char *option_background = "background.jpg";
134
135static const GOptionEntry option_entries[] = {
136 { "background", 'b', 0, G_OPTION_ARG_STRING,
137 &option_background, "Background image" },
138 { NULL }
139};
140
141
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500142struct screenshooter {
143 struct wl_object base;
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500144 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500145};
146
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500147struct screenshooter_interface {
148 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
149};
150
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500151static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500152screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500153{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500154 struct wlsc_compositor *ec = shooter->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500155 struct wlsc_output *output;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500156 char buffer[256];
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500157 GdkPixbuf *pixbuf, *normal;
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500158 GError *error = NULL;
159 void *data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500160 int i;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500161
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500162 i = 0;
163 output = container_of(ec->output_list.next, struct wlsc_output, link);
164 while (&output->link != &ec->output_list) {
165 snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500166 data = malloc(output->width * output->height * 4);
167 if (data == NULL) {
168 fprintf(stderr, "couldn't allocate image buffer\n");
169 continue;
170 }
171
172 glReadBuffer(GL_FRONT);
173 glPixelStorei(GL_PACK_ALIGNMENT, 1);
174 glReadPixels(0, 0, output->width, output->height,
175 GL_RGBA, GL_UNSIGNED_BYTE, data);
176
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500177 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500178 8, output->width, output->height, output->width * 4,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500179 NULL, NULL);
Kristian Høgsbergc0b44322009-01-26 22:54:40 -0500180 normal = gdk_pixbuf_flip(pixbuf, FALSE);
181 gdk_pixbuf_save(normal, buffer, "png", &error, NULL);
182 gdk_pixbuf_unref(normal);
183 gdk_pixbuf_unref(pixbuf);
184 free(data);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500185
186 output = container_of(output->link.next,
187 struct wlsc_output, link);
188 }
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500189}
190
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500191static const struct wl_message screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500192 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500193};
194
195static const struct wl_interface screenshooter_interface = {
196 "screenshooter", 1,
197 ARRAY_LENGTH(screenshooter_methods),
198 screenshooter_methods,
199};
200
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500201struct screenshooter_interface screenshooter_implementation = {
202 screenshooter_shoot
203};
204
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500205static struct screenshooter *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500206screenshooter_create(struct wlsc_compositor *ec)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500207{
208 struct screenshooter *shooter;
209
210 shooter = malloc(sizeof *shooter);
211 if (shooter == NULL)
212 return NULL;
213
214 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500215 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500216 shooter->ec = ec;
217
218 return shooter;
219};
220
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500221static struct wlsc_surface *
222wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500223 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500224 int x, int y, int width, int height)
225{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500226 struct wlsc_surface *es;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500227 int stride;
228 void *data;
229
230 stride = cairo_image_surface_get_stride(surface);
231 data = cairo_image_surface_get_data(surface);
232
233 es = malloc(sizeof *es);
234 if (es == NULL)
235 return NULL;
236
237 glGenTextures(1, &es->texture);
238 glBindTexture(GL_TEXTURE_2D, es->texture);
239 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500243 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
244 GL_BGRA, GL_UNSIGNED_BYTE, data);
245
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500246 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500247 es->map.x = x;
248 es->map.y = y;
249 es->map.width = width;
250 es->map.height = height;
251 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500252 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500253
254 return es;
255}
256
257static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500258wlsc_surface_destroy(struct wlsc_surface *es, struct wlsc_compositor *ec)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500259{
260 glDeleteTextures(1, &es->texture);
261 if (es->surface != EGL_NO_SURFACE)
262 eglDestroySurface(ec->display, es->surface);
263 free(es);
264}
265
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400266static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500267pointer_path(cairo_t *cr, int x, int y)
268{
269 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
270 const int width = 16, height = 16;
271
272 cairo_move_to(cr, x, y);
273 cairo_line_to(cr, x + tx, y + ty);
274 cairo_line_to(cr, x + dx, y + dy);
275 cairo_line_to(cr, x + width - end, y + height);
276 cairo_line_to(cr, x + width, y + height - end);
277 cairo_line_to(cr, x + dy, y + dx);
278 cairo_line_to(cr, x + ty, y + tx);
279 cairo_close_path(cr);
280}
281
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500282static struct wlsc_surface *
283pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500284{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500285 struct wlsc_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500286 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500287 cairo_surface_t *surface;
288 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500289
290 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
291 width, height);
292
293 cr = cairo_create(surface);
294 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500295 cairo_set_line_width(cr, 2);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500296 cairo_set_source_rgb(cr, 0, 0, 0);
297 cairo_stroke_preserve(cr);
298 cairo_fill(cr);
299 blur_surface(surface, width);
300
301 pointer_path(cr, hotspot_x, hotspot_y);
302 cairo_stroke_preserve(cr);
303 cairo_set_source_rgb(cr, 1, 1, 1);
304 cairo_fill(cr);
305 cairo_destroy(cr);
306
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500307 es = wlsc_surface_create_from_cairo_surface(ec,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500308 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500309 x - hotspot_x,
310 y - hotspot_y,
311 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500312
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500313 cairo_surface_destroy(surface);
314
Kristian Høgsberg54879822008-11-23 17:07:32 -0500315 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500316}
317
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500318static struct wlsc_surface *
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500319background_create(struct wlsc_output *output, const char *filename)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500320{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500321 struct wlsc_surface *background;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500322 GdkPixbuf *pixbuf;
323 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500324 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500325 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500326
327 background = malloc(sizeof *background);
328 if (background == NULL)
329 return NULL;
330
331 g_type_init();
332
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500333 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500334 output->width,
335 output->height,
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500336 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500337 if (error != NULL) {
338 free(background);
339 return NULL;
340 }
341
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500342 data = gdk_pixbuf_get_pixels(pixbuf);
343
344 glGenTextures(1, &background->texture);
345 glBindTexture(GL_TEXTURE_2D, background->texture);
346 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Ray Strode18fd33c2008-12-18 21:05:20 -0500350
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500351 if (gdk_pixbuf_get_has_alpha(pixbuf))
Ray Strode18fd33c2008-12-18 21:05:20 -0500352 format = GL_RGBA;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -0500353 else
Ray Strode18fd33c2008-12-18 21:05:20 -0500354 format = GL_RGB;
Ray Strode18fd33c2008-12-18 21:05:20 -0500355
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500356 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
357 output->width, output->height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500358 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500359
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500360 background->compositor = output->ec;
361 background->map.x = output->x;
362 background->map.y = output->y;
363 background->map.width = output->width;
364 background->map.height = output->height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500365 background->surface = EGL_NO_SURFACE;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500366 background->visual = &output->ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500367
368 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500369}
370
371static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500372draw_surface(struct wlsc_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500373{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500374 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500375 GLint vertices[12];
376 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
377 GLuint indices[4] = { 0, 1, 2, 3 };
378
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500379 vertices[0] = es->map.x;
380 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500381 vertices[2] = 0;
382
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500383 vertices[3] = es->map.x;
384 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500385 vertices[5] = 0;
386
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500387 vertices[6] = es->map.x + es->map.width;
388 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500389 vertices[8] = 0;
390
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500391 vertices[9] = es->map.x + es->map.width;
392 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500393 vertices[11] = 0;
394
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500395 if (es->visual == &ec->argb_visual) {
396 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
397 glEnable(GL_BLEND);
398 } else if (es->visual == &ec->premultiplied_argb_visual) {
399 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
400 glEnable(GL_BLEND);
401 } else {
402 glDisable(GL_BLEND);
403 }
404
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500405 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500406 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500407 glEnableClientState(GL_VERTEX_ARRAY);
408 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
409 glVertexPointer(3, GL_INT, 0, vertices);
410 glTexCoordPointer(2, GL_INT, 0, tex_coords);
411 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
412}
413
414static void
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500415repaint_output(struct wlsc_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400416{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500417 struct wlsc_compositor *ec = output->ec;
418 struct wlsc_surface *es;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500419 struct wlsc_input_device *eid;
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500420 double s = 3000;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500421
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500422 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
423 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500424 return;
425 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500426
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500427 glViewport(0, 0, output->width, output->height);
428 glMatrixMode(GL_PROJECTION);
429 glLoadIdentity();
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500430 glFrustum(-output->width / s, output->width / s, output->height / s, -output->height / s, 1, s);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500431 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500432 glLoadIdentity();
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500433 glClearColor(0, 0, 0.2, 1);
434
Kristian Høgsbergfdec2362001-01-01 22:23:51 -0500435 glTranslatef(-output->width / 2, -output->height/ 2, -s / 2);
436
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500437 if (output->background)
438 draw_surface(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500439 else
440 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400441
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500442 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500443 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500444 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500445 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500446
447 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500448 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400449 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400450
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500451 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500452 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500453 while (&eid->link != &ec->input_device_list) {
454 draw_surface(eid->pointer_surface);
455
456 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500457 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500458 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500459
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500460 eglSwapBuffers(ec->display, output->surface);
461}
462
463static void
464repaint(void *data)
465{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500466 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500467 struct wlsc_output *output;
468 struct timespec ts;
469 uint32_t msecs;
470
471 if (!ec->repaint_needed) {
472 ec->repaint_on_timeout = 0;
473 return;
474 }
475
476 output = container_of(ec->output_list.next, struct wlsc_output, link);
477 while (&output->link != &ec->output_list) {
478 repaint_output(output);
479 output = container_of(output->link.next,
480 struct wlsc_output, link);
481 }
482
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500483 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500484
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500485 clock_gettime(CLOCK_MONOTONIC, &ts);
486 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500487 wl_display_post_frame(ec->wl_display, &ec->base,
488 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500489 ec->current_frame++;
490
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500491 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500492 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400493}
494
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500495static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500496schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400497{
498 struct wl_event_loop *loop;
499
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500500 ec->repaint_needed = 1;
501 if (!ec->repaint_on_timeout) {
502 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500503 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500504 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400505}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400506
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500507static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500508surface_destroy(struct wl_client *client,
509 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400510{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500511 struct wlsc_surface *es = (struct wlsc_surface *) surface;
512 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500513
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500514 wl_list_remove(&es->link);
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500515 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400516
517 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400518}
519
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500520static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500521surface_attach(struct wl_client *client,
522 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500523 uint32_t width, uint32_t height, uint32_t stride,
524 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400525{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500526 struct wlsc_surface *es = (struct wlsc_surface *) surface;
527 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400528
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500529 if (es->surface != EGL_NO_SURFACE)
530 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400531
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500532 es->width = width;
533 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500534 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500535 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500536 if (visual == &ec->argb_visual.base)
537 es->visual = &ec->argb_visual;
538 else if (visual == &ec->premultiplied_argb_visual.base)
539 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500540 else if (visual == &ec->rgb_visual.base)
541 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500542 else
543 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400544
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500545 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400546 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500547 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
548 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
549 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500550 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400551}
552
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500553static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500554surface_map(struct wl_client *client,
555 struct wl_surface *surface,
556 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400557{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500558 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400559
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500560 es->map.x = x;
561 es->map.y = y;
562 es->map.width = width;
563 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400564}
565
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500566static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500567surface_copy(struct wl_client *client,
568 struct wl_surface *surface,
569 int32_t dst_x, int32_t dst_y,
570 uint32_t name, uint32_t stride,
571 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500572{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500573 struct wlsc_surface *es = (struct wlsc_surface *) surface;
574 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500575 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500576
577 /* FIXME: glCopyPixels should work, but then we'll have to
578 * call eglMakeCurrent to set up the src and dest surfaces
579 * first. This seems cheaper, but maybe there's a better way
580 * to accomplish this. */
581
582 src = eglCreateSurfaceForName(ec->display, ec->config,
583 name, x + width, y + height, stride, NULL);
584
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500585 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500586 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500587 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500588}
589
590static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500591surface_damage(struct wl_client *client,
592 struct wl_surface *surface,
593 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500594{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500595 /* FIXME: This need to take a damage region, of course. */
596}
597
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500598const static struct wl_surface_interface surface_interface = {
599 surface_destroy,
600 surface_attach,
601 surface_map,
602 surface_copy,
603 surface_damage
604};
605
606static void
607compositor_create_surface(struct wl_client *client,
608 struct wl_compositor *compositor, uint32_t id)
609{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500610 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
611 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500612
613 es = malloc(sizeof *es);
614 if (es == NULL)
615 /* FIXME: Send OOM event. */
616 return;
617
618 es->compositor = ec;
619 es->surface = EGL_NO_SURFACE;
620 wl_list_insert(ec->surface_list.prev, &es->link);
621 glGenTextures(1, &es->texture);
622 wl_client_add_surface(client, &es->base,
623 &surface_interface, id);
624}
625
626static void
627compositor_commit(struct wl_client *client,
628 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500629{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500630 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500631
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500632 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500633 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500634}
635
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500636const static struct wl_compositor_interface compositor_interface = {
637 compositor_create_surface,
638 compositor_commit
639};
640
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500641static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500642pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500643{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500644 struct wlsc_compositor *ec = device->ec;
645 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500646
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500647 if (device->grab > 0)
648 return device->grab_surface;
649
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500650 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500651 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500652 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500653 if (es->map.x <= device->x &&
654 device->x < es->map.x + es->map.width &&
655 es->map.y <= device->y &&
656 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500657 return es;
658
659 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500660 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500661
662 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500663 *sx = (device->x - es->map.x) * es->width / es->map.width;
664 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500665 }
666
667 return NULL;
668}
669
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500670void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500671notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500672{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500673 struct wlsc_surface *es;
674 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500675 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500676 const int hotspot_x = 16, hotspot_y = 16;
677 int32_t sx, sy;
678
Ray Strodee96dcb82008-12-20 02:00:49 -0500679 if (!ec->vt_active)
680 return;
681
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500682 /* FIXME: We need some multi head love here. */
683 output = container_of(ec->output_list.next, struct wlsc_output, link);
684 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500685 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500686 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500687 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500688 if (x >= output->x + output->width)
689 x = output->x + output->width - 1;
690 if (y >= output->y + output->height)
691 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500692
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500693 device->x = x;
694 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500695 es = pick_surface(device, &sx, &sy);
696 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500697 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500698 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500699
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500700 device->pointer_surface->map.x = x - hotspot_x;
701 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500702
703 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500704}
705
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500706void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500707notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500708 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500709{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500710 struct wlsc_surface *es;
711 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500712 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500713
Ray Strodee96dcb82008-12-20 02:00:49 -0500714 if (!ec->vt_active)
715 return;
716
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500717 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500718 if (es) {
719 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500720 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500721
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500722 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500723 /* FIXME: We need callbacks when the surfaces
724 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500725 device->grab++;
726 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500727 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500728 } else {
729 device->grab--;
730 }
731
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500732 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500733 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500734 WL_INPUT_BUTTON, button, state,
735 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500736
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500737 schedule_repaint(device->ec);
738 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500739}
740
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500741static void on_term_signal(int signal_number, void *data);
742
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500743void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500744notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500745 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500746{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500747 struct wlsc_compositor *ec = device->ec;
Ray Strodee96dcb82008-12-20 02:00:49 -0500748
Kristian Høgsbergab909ae2001-01-01 22:24:24 -0500749 if (key == KEY_EJECTCD)
750 on_term_signal(SIGTERM, ec);
751
Ray Strodee96dcb82008-12-20 02:00:49 -0500752 if (!ec->vt_active)
753 return;
754
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500755 if (device->focus_surface != NULL)
756 wl_surface_post_event(&device->focus_surface->base,
757 &device->base,
758 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500759}
760
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500761struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500762evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763 struct wl_display *display, const char *path);
764
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500765static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500766create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500767{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500768 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500769
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500770 device = malloc(sizeof *device);
771 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500772 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500773
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500774 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500775 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500776 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500777 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500778 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500779 device->x = 100;
780 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500781 device->ec = ec;
782
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500783 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500784
785 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500786}
787
788void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500789wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500790{
791 *x = device->x;
792 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500793}
794
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500795static void
796post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500797{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500798 struct wlsc_output *output =
799 container_of(global, struct wlsc_output, base);
800
801 wl_client_post_event(client, global,
802 WL_OUTPUT_GEOMETRY,
803 output->width, output->height);
804}
805
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500806static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500807init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500808{
809 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500810 EGL_DEPTH_SIZE, 0,
811 EGL_STENCIL_SIZE, 0,
812 EGL_CONFIG_CAVEAT, EGL_NONE,
813 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500814 EGL_NONE
815 };
816
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500817 EGLint major, minor;
818
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500819 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500820 if (ec->display == NULL) {
821 fprintf(stderr, "failed to create display\n");
822 return -1;
823 }
824
825 if (!eglInitialize(ec->display, &major, &minor)) {
826 fprintf(stderr, "failed to initialize display\n");
827 return -1;
828 }
829
830 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
831 return -1;
832
833 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
834 if (ec->context == NULL) {
835 fprintf(stderr, "failed to create context\n");
836 return -1;
837 }
838 return 0;
839}
840
841static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500842create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500843{
844 const static EGLint surface_attribs[] = {
845 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
846 EGL_NONE
847 };
848
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500849 drmModeConnector *connector;
850 drmModeRes *resources;
851 drmModeEncoder *encoder;
852 struct drm_mode_modeinfo *mode;
853 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500854 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500855 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500856 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500857
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500858 if (ec->display == NULL && init_egl(ec, device) < 0) {
859 fprintf(stderr, "failed to initialize egl\n");
860 return -1;
861 }
862
863 output = malloc(sizeof *output);
864 if (output == NULL)
865 return -1;
866
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500867 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500868 resources = drmModeGetResources(fd);
869 if (!resources) {
870 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500871 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500872 }
873
874 for (i = 0; i < resources->count_connectors; i++) {
875 connector = drmModeGetConnector(fd, resources->connectors[i]);
876 if (connector == NULL)
877 continue;
878
879 if (connector->connection == DRM_MODE_CONNECTED &&
880 connector->count_modes > 0)
881 break;
882
883 drmModeFreeConnector(connector);
884 }
885
886 if (i == resources->count_connectors) {
887 fprintf(stderr, "No currently active connector found.\n");
888 return -1;
889 }
890
891 mode = &connector->modes[0];
892
893 for (i = 0; i < resources->count_encoders; i++) {
894 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
895
896 if (encoder == NULL)
897 continue;
898
899 if (encoder->encoder_id == connector->encoder_id)
900 break;
901
902 drmModeFreeEncoder(encoder);
903 }
904
905 /* Mode size at 32 bpp */
906 create.size = mode->hdisplay * mode->vdisplay * 4;
907 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
908 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500909 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500910 }
911
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500912 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500913 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500914 if (ret) {
915 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500916 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500917 }
918
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500919 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500920 &connector->connector_id, 1, mode);
921 if (ret) {
922 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500923 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500924 }
925
926 flink.handle = create.handle;
927 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
928 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500929 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500930 }
931
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500932 output->ec = ec;
933 output->crtc_id = encoder->crtc_id;
934 output->connector_id = connector->connector_id;
935 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -0500936 output->x = 0;
937 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500938 output->width = mode->hdisplay;
939 output->height = mode->vdisplay;
940 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500941
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500942 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
943 flink.name,
944 output->width, output->height,
945 output->stride,
946 surface_attribs);
947 if (output->surface == NULL) {
948 fprintf(stderr, "failed to create surface\n");
949 return -1;
950 }
951
952 output->base.interface = &wl_output_interface;
953 wl_display_add_object(ec->wl_display, &output->base);
954 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
955 wl_list_insert(ec->output_list.prev, &output->link);
956
957 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
958 fprintf(stderr, "failed to make context current\n");
959 return -1;
960 }
961
962 output->background = background_create(output, option_background);
963
964 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500965}
966
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500967static const struct wl_interface visual_interface = {
968 "visual", 1,
969};
970
971static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500972add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500973{
974 ec->argb_visual.base.interface = &visual_interface;
975 ec->argb_visual.base.implementation = NULL;
976 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500977 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500978
979 ec->premultiplied_argb_visual.base.interface = &visual_interface;
980 ec->premultiplied_argb_visual.base.implementation = NULL;
981 wl_display_add_object(ec->wl_display,
982 &ec->premultiplied_argb_visual.base);
983 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500984 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500985
986 ec->rgb_visual.base.interface = &visual_interface;
987 ec->rgb_visual.base.implementation = NULL;
988 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500989 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
990}
991
Ray Strode19ad6a92008-12-19 01:45:41 -0500992static void on_enter_vt(int signal_number, void *data)
993{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500994 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500995 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500996 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500997
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500998 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500999 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001000
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -05001001 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001002 output = container_of(ec->output_list.next, struct wlsc_output, link);
1003 while (&output->link != &ec->output_list) {
1004 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
1005 &output->connector_id, 1, output->mode);
1006 if (ret)
1007 fprintf(stderr, "failed to set mode for connector %d: %m\n",
1008 output->connector_id);
1009
1010 output = container_of(output->link.next,
1011 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001012 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001013}
1014
1015static void on_leave_vt(int signal_number, void *data)
1016{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001017 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001018
1019 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001020 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001021}
1022
Ray Strode966aa112008-12-19 14:28:02 -05001023static void
1024on_tty_input(int fd, uint32_t mask, void *data)
1025{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001026 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001027
1028 /* Ignore input to tty. We get keyboard events from evdev
1029 */
1030 tcflush(ec->tty_fd, TCIFLUSH);
1031}
1032
1033static void on_term_signal(int signal_number, void *data)
1034{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001035 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001036
1037 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1038 fprintf(stderr, "could not restore terminal to canonical mode\n");
1039
1040 exit(0);
1041}
1042
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001043static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001044{
1045 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001046 struct vt_mode mode = { 0 };
1047
1048 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1049 if (ec->tty_fd <= 0) {
1050 fprintf(stderr, "failed to open active tty: %m\n");
1051 return -1;
1052 }
Ray Strode966aa112008-12-19 14:28:02 -05001053
1054 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1055 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001056 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001057 }
1058
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001059 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001060 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001061 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001062
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001063 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001064 raw_attributes.c_oflag |= OPOST | OCRNL;
1065
1066 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1067 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1068
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001069 ec->term_signal_source =
1070 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001071
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001072 ec->tty_input_source =
1073 wl_event_loop_add_fd(loop, ec->tty_fd,
1074 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001075
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001076 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001077 mode.mode = VT_PROCESS;
1078 mode.relsig = SIGUSR1;
1079 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001080 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001081 fprintf(stderr, "failed to take control of vt handling\n");
1082 }
1083
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001084 ec->leave_vt_source =
1085 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1086 ec->enter_vt_source =
1087 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001088
1089 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001090}
1091
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001092static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001093init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001094{
1095 struct udev_enumerate *e;
1096 struct udev_list_entry *entry;
1097 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001098 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001099 struct wlsc_input_device *input_device;
1100
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001101 ec->udev = udev_new();
1102 if (ec->udev == NULL) {
1103 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001104 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001105 }
1106
1107 input_device = create_input_device(ec);
1108
1109 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001110 udev_enumerate_add_match_subsystem(e, "input");
1111 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001112 udev_enumerate_scan_devices(e);
1113 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1114 path = udev_list_entry_get_name(entry);
1115 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001116 evdev_input_device_create(input_device, ec->wl_display,
1117 udev_device_get_devnode(device));
1118 }
1119 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001120
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001121 e = udev_enumerate_new(ec->udev);
1122 udev_enumerate_add_match_subsystem(e, "drm");
1123 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1124 udev_enumerate_scan_devices(e);
1125 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1126 path = udev_list_entry_get_name(entry);
1127 device = udev_device_new_from_syspath(ec->udev, path);
1128 if (create_output(ec, device) < 0) {
1129 fprintf(stderr, "failed to create output for %s\n", path);
1130 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001131 }
1132 }
1133 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001134
1135 /* Create the pointer surface now that we have a current EGL context. */
1136 input_device->pointer_surface =
1137 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1138
1139 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001140}
1141
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001142static struct wlsc_compositor *
1143wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001144{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001145 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001146 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001147 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001148
1149 ec = malloc(sizeof *ec);
1150 if (ec == NULL)
1151 return NULL;
1152
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001153 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001154 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001155
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001156 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001157
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001158 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001159
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001160 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001161 wl_list_init(&ec->input_device_list);
1162 wl_list_init(&ec->output_list);
1163 if (init_libudev(ec) < 0) {
1164 fprintf(stderr, "failed to initialize devices\n");
1165 return NULL;
1166 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001167
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001168 shooter = screenshooter_create(ec);
1169 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001170 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001171
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001172 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001173
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001174 setup_tty(ec, loop);
1175
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001176 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001177 schedule_repaint(ec);
1178
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001179 return ec;
1180}
1181
1182/* The plan here is to generate a random anonymous socket name and
1183 * advertise that through a service on the session dbus.
1184 */
1185static const char socket_name[] = "\0wayland";
1186
1187int main(int argc, char *argv[])
1188{
1189 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001190 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001191 GError *error = NULL;
1192 GOptionContext *context;
1193
1194 context = g_option_context_new(NULL);
1195 g_option_context_add_main_entries(context, option_entries, "Wayland");
1196 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1197 fprintf(stderr, "option parsing failed: %s\n", error->message);
1198 exit(EXIT_FAILURE);
1199 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001200
1201 display = wl_display_create();
1202
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001203 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001204 if (ec == NULL) {
1205 fprintf(stderr, "failed to create compositor\n");
1206 exit(EXIT_FAILURE);
1207 }
1208
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001209 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001210 fprintf(stderr, "failed to add socket: %m\n");
1211 exit(EXIT_FAILURE);
1212 }
1213
1214 wl_display_run(display);
1215
1216 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001217}