blob: fe3bf324f665878d4a67649b0b19e31358a57e42 [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);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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øgsbergfbdbbdc2008-11-28 17:06:06 -0500420
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500421 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
422 fprintf(stderr, "failed to make context current\n");
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500423 return;
424 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500425
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500426 glViewport(0, 0, output->width, output->height);
427 glMatrixMode(GL_PROJECTION);
428 glLoadIdentity();
429 glOrtho(0, output->width, output->height, 0, 0, 1000.0);
430 glMatrixMode(GL_MODELVIEW);
431 glClearColor(0, 0, 0.2, 1);
432
433 if (output->background)
434 draw_surface(output->background);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500435 else
436 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400437
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500438 es = container_of(ec->surface_list.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500439 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500440 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500441 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500442
443 es = container_of(es->link.next,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500444 struct wlsc_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400445 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400446
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500447 eid = container_of(ec->input_device_list.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500448 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500449 while (&eid->link != &ec->input_device_list) {
450 draw_surface(eid->pointer_surface);
451
452 eid = container_of(eid->link.next,
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500453 struct wlsc_input_device, link);
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500454 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500455
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500456 eglSwapBuffers(ec->display, output->surface);
457}
458
459static void
460repaint(void *data)
461{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500462 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500463 struct wlsc_output *output;
464 struct timespec ts;
465 uint32_t msecs;
466
467 if (!ec->repaint_needed) {
468 ec->repaint_on_timeout = 0;
469 return;
470 }
471
472 output = container_of(ec->output_list.next, struct wlsc_output, link);
473 while (&output->link != &ec->output_list) {
474 repaint_output(output);
475 output = container_of(output->link.next,
476 struct wlsc_output, link);
477 }
478
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500479 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500480
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500481 clock_gettime(CLOCK_MONOTONIC, &ts);
482 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500483 wl_display_post_frame(ec->wl_display, &ec->base,
484 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500485 ec->current_frame++;
486
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500487 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500488 ec->repaint_on_timeout = 1;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400489}
490
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500491static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500492schedule_repaint(struct wlsc_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400493{
494 struct wl_event_loop *loop;
495
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500496 ec->repaint_needed = 1;
497 if (!ec->repaint_on_timeout) {
498 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500499 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500500 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400501}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400502
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500503static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500504surface_destroy(struct wl_client *client,
505 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400506{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500507 struct wlsc_surface *es = (struct wlsc_surface *) surface;
508 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500509
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500510 wl_list_remove(&es->link);
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500511 wlsc_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400512
513 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400514}
515
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500516static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500517surface_attach(struct wl_client *client,
518 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500519 uint32_t width, uint32_t height, uint32_t stride,
520 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400521{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500522 struct wlsc_surface *es = (struct wlsc_surface *) surface;
523 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400524
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500525 if (es->surface != EGL_NO_SURFACE)
526 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400527
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500528 es->width = width;
529 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500530 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500531 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500532 if (visual == &ec->argb_visual.base)
533 es->visual = &ec->argb_visual;
534 else if (visual == &ec->premultiplied_argb_visual.base)
535 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500536 else if (visual == &ec->rgb_visual.base)
537 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500538 else
539 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400540
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500541 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400542 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500543 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
545 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500546 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400547}
548
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500549static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500550surface_map(struct wl_client *client,
551 struct wl_surface *surface,
552 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400553{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500554 struct wlsc_surface *es = (struct wlsc_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400555
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500556 es->map.x = x;
557 es->map.y = y;
558 es->map.width = width;
559 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400560}
561
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500562static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500563surface_copy(struct wl_client *client,
564 struct wl_surface *surface,
565 int32_t dst_x, int32_t dst_y,
566 uint32_t name, uint32_t stride,
567 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500568{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500569 struct wlsc_surface *es = (struct wlsc_surface *) surface;
570 struct wlsc_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500571 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500572
573 /* FIXME: glCopyPixels should work, but then we'll have to
574 * call eglMakeCurrent to set up the src and dest surfaces
575 * first. This seems cheaper, but maybe there's a better way
576 * to accomplish this. */
577
578 src = eglCreateSurfaceForName(ec->display, ec->config,
579 name, x + width, y + height, stride, NULL);
580
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500581 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500582 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500583 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500584}
585
586static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500587surface_damage(struct wl_client *client,
588 struct wl_surface *surface,
589 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500590{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500591 /* FIXME: This need to take a damage region, of course. */
592}
593
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500594const static struct wl_surface_interface surface_interface = {
595 surface_destroy,
596 surface_attach,
597 surface_map,
598 surface_copy,
599 surface_damage
600};
601
602static void
603compositor_create_surface(struct wl_client *client,
604 struct wl_compositor *compositor, uint32_t id)
605{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500606 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
607 struct wlsc_surface *es;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500608
609 es = malloc(sizeof *es);
610 if (es == NULL)
611 /* FIXME: Send OOM event. */
612 return;
613
614 es->compositor = ec;
615 es->surface = EGL_NO_SURFACE;
616 wl_list_insert(ec->surface_list.prev, &es->link);
617 glGenTextures(1, &es->texture);
618 wl_client_add_surface(client, &es->base,
619 &surface_interface, id);
620}
621
622static void
623compositor_commit(struct wl_client *client,
624 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500625{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500626 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500627
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500628 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500629 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500630}
631
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500632const static struct wl_compositor_interface compositor_interface = {
633 compositor_create_surface,
634 compositor_commit
635};
636
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500637static struct wlsc_surface *
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500638pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500639{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500640 struct wlsc_compositor *ec = device->ec;
641 struct wlsc_surface *es;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500642
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500643 if (device->grab > 0)
644 return device->grab_surface;
645
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500646 es = container_of(ec->surface_list.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500647 struct wlsc_surface, link);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500648 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500649 if (es->map.x <= device->x &&
650 device->x < es->map.x + es->map.width &&
651 es->map.y <= device->y &&
652 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500653 return es;
654
655 es = container_of(es->link.prev,
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500656 struct wlsc_surface, link);
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500657
658 /* Transform to surface coordinates. */
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500659 *sx = (device->x - es->map.x) * es->width / es->map.width;
660 *sy = (device->y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500661 }
662
663 return NULL;
664}
665
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500666void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500667notify_motion(struct wlsc_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500668{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500669 struct wlsc_surface *es;
670 struct wlsc_compositor *ec = device->ec;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500671 struct wlsc_output *output;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500672 const int hotspot_x = 16, hotspot_y = 16;
673 int32_t sx, sy;
674
Ray Strodee96dcb82008-12-20 02:00:49 -0500675 if (!ec->vt_active)
676 return;
677
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500678 /* FIXME: We need some multi head love here. */
679 output = container_of(ec->output_list.next, struct wlsc_output, link);
680 if (x < output->x)
Ray Strode90e701d2008-12-18 23:05:43 -0500681 x = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500682 if (y < output->y)
Ray Strode90e701d2008-12-18 23:05:43 -0500683 y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500684 if (x >= output->x + output->width)
685 x = output->x + output->width - 1;
686 if (y >= output->y + output->height)
687 y = output->y + output->height - 1;
Ray Strode90e701d2008-12-18 23:05:43 -0500688
Kristian Høgsberge3ef3e52008-12-21 19:30:01 -0500689 device->x = x;
690 device->y = y;
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500691 es = pick_surface(device, &sx, &sy);
692 if (es)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500693 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500694 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500695
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500696 device->pointer_surface->map.x = x - hotspot_x;
697 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500698
699 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500700}
701
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500702void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500703notify_button(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500704 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500705{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500706 struct wlsc_surface *es;
707 struct wlsc_compositor *ec = device->ec;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500708 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500709
Ray Strodee96dcb82008-12-20 02:00:49 -0500710 if (!ec->vt_active)
711 return;
712
Kristian Høgsberg7e972a52008-12-21 17:26:00 -0500713 es = pick_surface(device, &sx, &sy);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500714 if (es) {
715 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500716 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500717
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500718 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500719 /* FIXME: We need callbacks when the surfaces
720 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500721 device->grab++;
722 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500723 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500724 } else {
725 device->grab--;
726 }
727
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500728 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500729 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500730 WL_INPUT_BUTTON, button, state,
731 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500732
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500733 schedule_repaint(device->ec);
734 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500735}
736
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500737void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500738notify_key(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500739 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500740{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500741 struct wlsc_compositor *ec = device->ec;
Ray Strodee96dcb82008-12-20 02:00:49 -0500742
743 if (!ec->vt_active)
744 return;
745
Kristian Høgsberg2c0e56b2008-12-19 13:54:40 -0500746 if (device->focus_surface != NULL)
747 wl_surface_post_event(&device->focus_surface->base,
748 &device->base,
749 WL_INPUT_KEY, key, state);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500750}
751
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500752struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500753evdev_input_device_create(struct wlsc_input_device *device,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500754 struct wl_display *display, const char *path);
755
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500756static struct wlsc_input_device *
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500757create_input_device(struct wlsc_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500758{
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500759 struct wlsc_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500760
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500761 device = malloc(sizeof *device);
762 if (device == NULL)
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500763 return NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500764
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500765 memset(device, 0, sizeof *device);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500766 device->base.interface = &wl_input_device_interface;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500767 device->base.implementation = NULL;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500768 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500769 wl_display_add_global(ec->wl_display, &device->base, NULL);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500770 device->x = 100;
771 device->y = 100;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500772 device->ec = ec;
773
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500774 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg890bc052008-12-30 14:31:33 -0500775
776 return device;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500777}
778
779void
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500780wlsc_device_get_position(struct wlsc_input_device *device, int32_t *x, int32_t *y)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500781{
782 *x = device->x;
783 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500784}
785
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500786static void
787post_output_geometry(struct wl_client *client, struct wl_object *global)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500788{
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500789 struct wlsc_output *output =
790 container_of(global, struct wlsc_output, base);
791
792 wl_client_post_event(client, global,
793 WL_OUTPUT_GEOMETRY,
794 output->width, output->height);
795}
796
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500797static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500798init_egl(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500799{
800 static const EGLint config_attribs[] = {
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500801 EGL_DEPTH_SIZE, 0,
802 EGL_STENCIL_SIZE, 0,
803 EGL_CONFIG_CAVEAT, EGL_NONE,
804 EGL_RED_SIZE, 8,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500805 EGL_NONE
806 };
807
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500808 EGLint major, minor;
809
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500810 ec->display = eglCreateDisplayNative(device);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500811 if (ec->display == NULL) {
812 fprintf(stderr, "failed to create display\n");
813 return -1;
814 }
815
816 if (!eglInitialize(ec->display, &major, &minor)) {
817 fprintf(stderr, "failed to initialize display\n");
818 return -1;
819 }
820
821 if (!eglChooseConfig(ec->display, config_attribs, &ec->config, 1, NULL))
822 return -1;
823
824 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
825 if (ec->context == NULL) {
826 fprintf(stderr, "failed to create context\n");
827 return -1;
828 }
829 return 0;
830}
831
832static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500833create_output(struct wlsc_compositor *ec, struct udev_device *device)
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500834{
835 const static EGLint surface_attribs[] = {
836 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
837 EGL_NONE
838 };
839
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500840 drmModeConnector *connector;
841 drmModeRes *resources;
842 drmModeEncoder *encoder;
843 struct drm_mode_modeinfo *mode;
844 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500845 struct drm_gem_flink flink;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500846 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500847 int i, ret, fd;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500848
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500849 if (ec->display == NULL && init_egl(ec, device) < 0) {
850 fprintf(stderr, "failed to initialize egl\n");
851 return -1;
852 }
853
854 output = malloc(sizeof *output);
855 if (output == NULL)
856 return -1;
857
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500858 fd = eglGetDisplayFD(ec->display);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500859 resources = drmModeGetResources(fd);
860 if (!resources) {
861 fprintf(stderr, "drmModeGetResources failed\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500862 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500863 }
864
865 for (i = 0; i < resources->count_connectors; i++) {
866 connector = drmModeGetConnector(fd, resources->connectors[i]);
867 if (connector == NULL)
868 continue;
869
870 if (connector->connection == DRM_MODE_CONNECTED &&
871 connector->count_modes > 0)
872 break;
873
874 drmModeFreeConnector(connector);
875 }
876
877 if (i == resources->count_connectors) {
878 fprintf(stderr, "No currently active connector found.\n");
879 return -1;
880 }
881
882 mode = &connector->modes[0];
883
884 for (i = 0; i < resources->count_encoders; i++) {
885 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
886
887 if (encoder == NULL)
888 continue;
889
890 if (encoder->encoder_id == connector->encoder_id)
891 break;
892
893 drmModeFreeEncoder(encoder);
894 }
895
896 /* Mode size at 32 bpp */
897 create.size = mode->hdisplay * mode->vdisplay * 4;
898 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
899 fprintf(stderr, "gem create failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500900 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500901 }
902
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500903 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500904 32, 32, mode->hdisplay * 4, create.handle, &output->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500905 if (ret) {
906 fprintf(stderr, "failed to add fb: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500907 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500908 }
909
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500910 ret = drmModeSetCrtc(fd, encoder->crtc_id, output->fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500911 &connector->connector_id, 1, mode);
912 if (ret) {
913 fprintf(stderr, "failed to set mode: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500914 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500915 }
916
917 flink.handle = create.handle;
918 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
919 fprintf(stderr, "gem flink failed: %m\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500920 return -1;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500921 }
922
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500923 output->ec = ec;
924 output->crtc_id = encoder->crtc_id;
925 output->connector_id = connector->connector_id;
926 output->mode = mode;
Kristian Høgsberge68d24c2009-01-15 11:44:35 -0500927 output->x = 0;
928 output->y = 0;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500929 output->width = mode->hdisplay;
930 output->height = mode->vdisplay;
931 output->stride = mode->hdisplay * 4;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500932
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500933 output->surface = eglCreateSurfaceForName(ec->display, ec->config,
934 flink.name,
935 output->width, output->height,
936 output->stride,
937 surface_attribs);
938 if (output->surface == NULL) {
939 fprintf(stderr, "failed to create surface\n");
940 return -1;
941 }
942
943 output->base.interface = &wl_output_interface;
944 wl_display_add_object(ec->wl_display, &output->base);
945 wl_display_add_global(ec->wl_display, &output->base, post_output_geometry);
946 wl_list_insert(ec->output_list.prev, &output->link);
947
948 if (!eglMakeCurrent(ec->display, output->surface, output->surface, ec->context)) {
949 fprintf(stderr, "failed to make context current\n");
950 return -1;
951 }
952
953 output->background = background_create(output, option_background);
954
955 return 0;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500956}
957
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500958static const struct wl_interface visual_interface = {
959 "visual", 1,
960};
961
962static void
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500963add_visuals(struct wlsc_compositor *ec)
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500964{
965 ec->argb_visual.base.interface = &visual_interface;
966 ec->argb_visual.base.implementation = NULL;
967 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500968 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500969
970 ec->premultiplied_argb_visual.base.interface = &visual_interface;
971 ec->premultiplied_argb_visual.base.implementation = NULL;
972 wl_display_add_object(ec->wl_display,
973 &ec->premultiplied_argb_visual.base);
974 wl_display_add_global(ec->wl_display,
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500975 &ec->premultiplied_argb_visual.base, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500976
977 ec->rgb_visual.base.interface = &visual_interface;
978 ec->rgb_visual.base.implementation = NULL;
979 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500980 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
981}
982
Ray Strode19ad6a92008-12-19 01:45:41 -0500983static void on_enter_vt(int signal_number, void *data)
984{
Kristian Høgsberg8e438622009-01-26 23:07:00 -0500985 struct wlsc_compositor *ec = data;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500986 struct wlsc_output *output;
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500987 int ret, fd;
Ray Strode19ad6a92008-12-19 01:45:41 -0500988
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500989 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
Ray Strodee96dcb82008-12-20 02:00:49 -0500990 ec->vt_active = TRUE;
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -0500991
Kristian Høgsberg2c875bd2008-12-19 10:34:02 -0500992 fd = eglGetDisplayFD(ec->display);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -0500993 output = container_of(ec->output_list.next, struct wlsc_output, link);
994 while (&output->link != &ec->output_list) {
995 ret = drmModeSetCrtc(fd, output->crtc_id, output->fb_id, 0, 0,
996 &output->connector_id, 1, output->mode);
997 if (ret)
998 fprintf(stderr, "failed to set mode for connector %d: %m\n",
999 output->connector_id);
1000
1001 output = container_of(output->link.next,
1002 struct wlsc_output, link);
Ray Strode19ad6a92008-12-19 01:45:41 -05001003 }
Ray Strode19ad6a92008-12-19 01:45:41 -05001004}
1005
1006static void on_leave_vt(int signal_number, void *data)
1007{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001008 struct wlsc_compositor *ec = data;
Ray Strode19ad6a92008-12-19 01:45:41 -05001009
1010 ioctl (ec->tty_fd, VT_RELDISP, 1);
Ray Strodee96dcb82008-12-20 02:00:49 -05001011 ec->vt_active = FALSE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001012}
1013
Ray Strode966aa112008-12-19 14:28:02 -05001014static void
1015on_tty_input(int fd, uint32_t mask, void *data)
1016{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001017 struct wlsc_compositor *ec = data;
Ray Strode966aa112008-12-19 14:28:02 -05001018
1019 /* Ignore input to tty. We get keyboard events from evdev
1020 */
1021 tcflush(ec->tty_fd, TCIFLUSH);
1022}
1023
1024static void on_term_signal(int signal_number, 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 if (tcsetattr(ec->tty_fd, TCSANOW, &ec->terminal_attributes) < 0)
1029 fprintf(stderr, "could not restore terminal to canonical mode\n");
1030
1031 exit(0);
1032}
1033
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001034static int setup_tty(struct wlsc_compositor *ec, struct wl_event_loop *loop)
Ray Strode966aa112008-12-19 14:28:02 -05001035{
1036 struct termios raw_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001037 struct vt_mode mode = { 0 };
1038
1039 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1040 if (ec->tty_fd <= 0) {
1041 fprintf(stderr, "failed to open active tty: %m\n");
1042 return -1;
1043 }
Ray Strode966aa112008-12-19 14:28:02 -05001044
1045 if (tcgetattr(ec->tty_fd, &ec->terminal_attributes) < 0) {
1046 fprintf(stderr, "could not get terminal attributes: %m\n");
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001047 return -1;
Ray Strode966aa112008-12-19 14:28:02 -05001048 }
1049
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001050 /* Ignore control characters and disable echo */
Ray Strode966aa112008-12-19 14:28:02 -05001051 raw_attributes = ec->terminal_attributes;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001052 cfmakeraw(&raw_attributes);
Ray Strode966aa112008-12-19 14:28:02 -05001053
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001054 /* Fix up line endings to be normal (cfmakeraw hoses them) */
Ray Strode966aa112008-12-19 14:28:02 -05001055 raw_attributes.c_oflag |= OPOST | OCRNL;
1056
1057 if (tcsetattr(ec->tty_fd, TCSANOW, &raw_attributes) < 0)
1058 fprintf(stderr, "could not put terminal into raw mode: %m\n");
1059
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001060 ec->term_signal_source =
1061 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001062
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001063 ec->tty_input_source =
1064 wl_event_loop_add_fd(loop, ec->tty_fd,
1065 WL_EVENT_READABLE, on_tty_input, ec);
Ray Strode966aa112008-12-19 14:28:02 -05001066
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001067 ec->vt_active = TRUE;
Ray Strode19ad6a92008-12-19 01:45:41 -05001068 mode.mode = VT_PROCESS;
1069 mode.relsig = SIGUSR1;
1070 mode.acqsig = SIGUSR2;
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001071 if (!ioctl(ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001072 fprintf(stderr, "failed to take control of vt handling\n");
1073 }
1074
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001075 ec->leave_vt_source =
1076 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, ec);
1077 ec->enter_vt_source =
1078 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, ec);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -05001079
1080 return 0;
Ray Strode19ad6a92008-12-19 01:45:41 -05001081}
1082
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001083static int
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001084init_libudev(struct wlsc_compositor *ec)
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001085{
1086 struct udev_enumerate *e;
1087 struct udev_list_entry *entry;
1088 struct udev_device *device;
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001089 const char *path;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001090 struct wlsc_input_device *input_device;
1091
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001092 ec->udev = udev_new();
1093 if (ec->udev == NULL) {
1094 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001095 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001096 }
1097
1098 input_device = create_input_device(ec);
1099
1100 e = udev_enumerate_new(ec->udev);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001101 udev_enumerate_add_match_subsystem(e, "input");
1102 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001103 udev_enumerate_scan_devices(e);
1104 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1105 path = udev_list_entry_get_name(entry);
1106 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001107 evdev_input_device_create(input_device, ec->wl_display,
1108 udev_device_get_devnode(device));
1109 }
1110 udev_enumerate_unref(e);
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001111
Kristian Høgsbergf13eb142009-01-26 21:38:14 -05001112 e = udev_enumerate_new(ec->udev);
1113 udev_enumerate_add_match_subsystem(e, "drm");
1114 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
1115 udev_enumerate_scan_devices(e);
1116 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1117 path = udev_list_entry_get_name(entry);
1118 device = udev_device_new_from_syspath(ec->udev, path);
1119 if (create_output(ec, device) < 0) {
1120 fprintf(stderr, "failed to create output for %s\n", path);
1121 return -1;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001122 }
1123 }
1124 udev_enumerate_unref(e);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001125
1126 /* Create the pointer surface now that we have a current EGL context. */
1127 input_device->pointer_surface =
1128 pointer_create(ec, input_device->x, input_device->y, 64, 64);
1129
1130 return 0;
Kristian Høgsberg890bc052008-12-30 14:31:33 -05001131}
1132
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001133static struct wlsc_compositor *
1134wlsc_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001135{
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001136 struct wlsc_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001137 struct screenshooter *shooter;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001138 struct wl_event_loop *loop;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001139
1140 ec = malloc(sizeof *ec);
1141 if (ec == NULL)
1142 return NULL;
1143
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001144 memset(ec, 0, sizeof *ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001145 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001146
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001147 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001148
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001149 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001150
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001151 wl_list_init(&ec->surface_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001152 wl_list_init(&ec->input_device_list);
1153 wl_list_init(&ec->output_list);
1154 if (init_libudev(ec) < 0) {
1155 fprintf(stderr, "failed to initialize devices\n");
1156 return NULL;
1157 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001158
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001159 shooter = screenshooter_create(ec);
1160 wl_display_add_object(display, &shooter->base);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05001161 wl_display_add_global(display, &shooter->base, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001162
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001163 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strodee96dcb82008-12-20 02:00:49 -05001164
Kristian Høgsberg0ab26242008-12-21 19:33:09 -05001165 setup_tty(ec, loop);
1166
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001167 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001168 schedule_repaint(ec);
1169
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001170 return ec;
1171}
1172
1173/* The plan here is to generate a random anonymous socket name and
1174 * advertise that through a service on the session dbus.
1175 */
1176static const char socket_name[] = "\0wayland";
1177
1178int main(int argc, char *argv[])
1179{
1180 struct wl_display *display;
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001181 struct wlsc_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001182 GError *error = NULL;
1183 GOptionContext *context;
1184
1185 context = g_option_context_new(NULL);
1186 g_option_context_add_main_entries(context, option_entries, "Wayland");
1187 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1188 fprintf(stderr, "option parsing failed: %s\n", error->message);
1189 exit(EXIT_FAILURE);
1190 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001191
1192 display = wl_display_create();
1193
Kristian Høgsberg8e438622009-01-26 23:07:00 -05001194 ec = wlsc_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001195 if (ec == NULL) {
1196 fprintf(stderr, "failed to create compositor\n");
1197 exit(EXIT_FAILURE);
1198 }
1199
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001200 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001201 fprintf(stderr, "failed to add socket: %m\n");
1202 exit(EXIT_FAILURE);
1203 }
1204
1205 wl_display_run(display);
1206
1207 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001208}