blob: 3c8aeb6b0dd684d732130f2ea7825bc74af1ff6b [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040023#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050027#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <i915_drm.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050034#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050035#include <gdk-pixbuf/gdk-pixbuf.h>
36#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050037#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050038#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050039#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050040#include <linux/input.h>
Ray Strode19ad6a92008-12-19 01:45:41 -050041#include <linux/vt.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050042#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050043#include <time.h>
Kristian Høgsbergc492b482008-12-12 12:00:02 -050044#include <fnmatch.h>
45#include <dirent.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040046
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040047#include <GL/gl.h>
48#include <eagle.h>
49
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050050#include "wayland.h"
51#include "cairo-util.h"
52#include "egl-compositor.h"
53
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040054#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
55
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050056struct wl_visual {
57 struct wl_object base;
58};
59
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060struct egl_input_device {
61 struct wl_object base;
62 int32_t x, y;
63 struct egl_compositor *ec;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050064 struct egl_surface *pointer_surface;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050065 struct wl_list link;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -050066
67 int grab;
68 struct egl_surface *grab_surface;
Kristian Høgsberga7700c82008-12-12 13:48:30 -050069 struct egl_surface *focus_surface;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050070};
71
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040072struct egl_compositor {
73 struct wl_compositor base;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050074 struct wl_visual argb_visual, premultiplied_argb_visual, rgb_visual;
75
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040076 EGLDisplay display;
77 EGLSurface surface;
78 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050079 EGLConfig config;
Ray Strode19ad6a92008-12-19 01:45:41 -050080 uint32_t fb_id;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040081 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040082 int gem_fd;
Ray Strode19ad6a92008-12-19 01:45:41 -050083 int tty_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050084 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050085 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050086 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050087 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050088
Kristian Høgsbergc492b482008-12-12 12:00:02 -050089 struct wl_list input_device_list;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050090 struct wl_list surface_list;
91
Ray Strode19ad6a92008-12-19 01:45:41 -050092 struct wl_event_source *enter_vt_source;
93 struct wl_event_source *leave_vt_source;
94
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050095 /* Repaint state. */
96 struct wl_event_source *timer_source;
97 int repaint_needed;
98 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050099 struct timespec previous_swap;
100 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400101};
102
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500103struct egl_surface {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_surface base;
105 struct egl_compositor *compositor;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500106 struct wl_visual *visual;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400107 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400108 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500109 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500110 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500111 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400112};
113
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500114struct screenshooter {
115 struct wl_object base;
116 struct egl_compositor *ec;
117};
118
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500119struct screenshooter_interface {
120 void (*shoot)(struct wl_client *client, struct screenshooter *shooter);
121};
122
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500123static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500124screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500125{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500126 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500127 GLuint stride;
128 static const char filename[] = "wayland-screenshot.png";
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500129 GdkPixbuf *pixbuf;
130 GError *error = NULL;
131 void *data;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500132
133 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
Kristian Høgsberg0ea47102008-12-14 15:53:13 -0500134 pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
135 8, ec->width, ec->height, stride,
136 NULL, NULL);
137 gdk_pixbuf_save(pixbuf, filename, "png", &error, NULL);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500138}
139
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500140static const struct wl_method screenshooter_methods[] = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500141 { "shoot", "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500142};
143
144static const struct wl_interface screenshooter_interface = {
145 "screenshooter", 1,
146 ARRAY_LENGTH(screenshooter_methods),
147 screenshooter_methods,
148};
149
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500150struct screenshooter_interface screenshooter_implementation = {
151 screenshooter_shoot
152};
153
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500154static struct screenshooter *
155screenshooter_create(struct egl_compositor *ec)
156{
157 struct screenshooter *shooter;
158
159 shooter = malloc(sizeof *shooter);
160 if (shooter == NULL)
161 return NULL;
162
163 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500164 shooter->base.implementation = (void(**)(void)) &screenshooter_implementation;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500165 shooter->ec = ec;
166
167 return shooter;
168};
169
Kristian Høgsberg54879822008-11-23 17:07:32 -0500170static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500171egl_surface_create_from_cairo_surface(struct egl_compositor *ec,
172 cairo_surface_t *surface,
Kristian Høgsberg54879822008-11-23 17:07:32 -0500173 int x, int y, int width, int height)
174{
175 struct egl_surface *es;
176 int stride;
177 void *data;
178
179 stride = cairo_image_surface_get_stride(surface);
180 data = cairo_image_surface_get_data(surface);
181
182 es = malloc(sizeof *es);
183 if (es == NULL)
184 return NULL;
185
186 glGenTextures(1, &es->texture);
187 glBindTexture(GL_TEXTURE_2D, es->texture);
188 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
193 GL_BGRA, GL_UNSIGNED_BYTE, data);
194
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500195 es->compositor = ec;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500196 es->map.x = x;
197 es->map.y = y;
198 es->map.width = width;
199 es->map.height = height;
200 es->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500201 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500202
203 return es;
204}
205
206static void
207egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
208{
209 glDeleteTextures(1, &es->texture);
210 if (es->surface != EGL_NO_SURFACE)
211 eglDestroySurface(ec->display, es->surface);
212 free(es);
213}
214
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400215static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500216pointer_path(cairo_t *cr, int x, int y)
217{
218 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
219 const int width = 16, height = 16;
220
221 cairo_move_to(cr, x, y);
222 cairo_line_to(cr, x + tx, y + ty);
223 cairo_line_to(cr, x + dx, y + dy);
224 cairo_line_to(cr, x + width - end, y + height);
225 cairo_line_to(cr, x + width, y + height - end);
226 cairo_line_to(cr, x + dy, y + dx);
227 cairo_line_to(cr, x + ty, y + tx);
228 cairo_close_path(cr);
229}
230
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500231static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500232pointer_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500233{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500234 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500235 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500236 cairo_surface_t *surface;
237 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500238
239 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
240 width, height);
241
242 cr = cairo_create(surface);
243 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
244 cairo_set_line_width (cr, 2);
245 cairo_set_source_rgb(cr, 0, 0, 0);
246 cairo_stroke_preserve(cr);
247 cairo_fill(cr);
248 blur_surface(surface, width);
249
250 pointer_path(cr, hotspot_x, hotspot_y);
251 cairo_stroke_preserve(cr);
252 cairo_set_source_rgb(cr, 1, 1, 1);
253 cairo_fill(cr);
254 cairo_destroy(cr);
255
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500256 es = egl_surface_create_from_cairo_surface(ec,
257 surface,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500258 x - hotspot_x,
259 y - hotspot_y,
260 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500261
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500262 cairo_surface_destroy(surface);
263
Kristian Høgsberg54879822008-11-23 17:07:32 -0500264 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500265}
266
267static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500268background_create(struct egl_compositor *ec,
269 const char *filename, int width, int height)
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270{
271 struct egl_surface *background;
272 GdkPixbuf *pixbuf;
273 GError *error = NULL;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500274 void *data;
Ray Strode18fd33c2008-12-18 21:05:20 -0500275 GLenum format;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500276
277 background = malloc(sizeof *background);
278 if (background == NULL)
279 return NULL;
280
281 g_type_init();
282
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500283 pixbuf = gdk_pixbuf_new_from_file_at_scale (filename,
284 width, height,
285 FALSE, &error);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500286 if (error != NULL) {
287 free(background);
288 return NULL;
289 }
290
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500291 data = gdk_pixbuf_get_pixels(pixbuf);
292
293 glGenTextures(1, &background->texture);
294 glBindTexture(GL_TEXTURE_2D, background->texture);
295 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
297 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Ray Strode18fd33c2008-12-18 21:05:20 -0500299
300 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
301 format = GL_RGBA;
302 } else {
303 format = GL_RGB;
304 }
305
Kristian Høgsberg2d4219e2008-12-18 21:40:03 -0500306 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
Ray Strode18fd33c2008-12-18 21:05:20 -0500307 format, GL_UNSIGNED_BYTE, data);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500308
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500309 background->compositor = ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500310 background->map.x = 0;
311 background->map.y = 0;
312 background->map.width = width;
313 background->map.height = height;
314 background->surface = EGL_NO_SURFACE;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500315 background->visual = &ec->rgb_visual;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500316
317 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318}
319
320static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500321rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
322{
323 cairo_move_to(cr, x0, y0 + radius);
324 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
325 cairo_line_to(cr, x1 - radius, y0);
326 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
327 cairo_line_to(cr, x1, y1 - radius);
328 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
329 cairo_line_to(cr, x0 + radius, y1);
330 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
331 cairo_close_path(cr);
332}
333
334static void
335draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
336{
337 cairo_pattern_t *gradient;
338 cairo_text_extents_t extents;
339 double bright = 0.15, dim = 0.02;
340 int radius = 10;
341
342 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
343 cairo_set_line_width (cr, 2);
344 rounded_rect(cr, x, y, x + width, y + height, radius);
345 cairo_set_source_rgb(cr, dim, dim, dim);
346 cairo_stroke(cr);
347 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
348 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
349 cairo_stroke(cr);
350
351 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
352 cairo_set_source_rgb(cr, bright, bright, bright);
353 cairo_stroke(cr);
354 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
355 cairo_set_source_rgb(cr, dim, dim, dim);
356 cairo_stroke(cr);
357
358 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
359 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
360 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
361 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
362 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
363 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
364 cairo_set_source(cr, gradient);
365 cairo_fill(cr);
366
367 cairo_set_font_size(cr, 16);
368 cairo_text_extents(cr, text, &extents);
369 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
370 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
371 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
372 cairo_set_line_width (cr, 4);
373 cairo_text_path(cr, text);
374 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
375 cairo_stroke_preserve(cr);
376 cairo_set_source_rgb(cr, 1, 1, 1);
377 cairo_fill(cr);
378}
379
380static struct egl_surface *
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500381overlay_create(struct egl_compositor *ec, int x, int y, int width, int height)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500382{
383 struct egl_surface *es;
384 cairo_surface_t *surface;
385 cairo_t *cr;
386 int total_width, button_x, button_y;
387 const int button_width = 150;
388 const int button_height = 40;
389 const int spacing = 50;
390
391 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
392 width, height);
393
394 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500395 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500396 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
397 cairo_paint(cr);
398
399 total_width = button_width * 2 + spacing;
400 button_x = (width - total_width) / 2;
401 button_y = height - button_height - 20;
402 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
403 button_x += button_width + spacing;
404 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
405
406 cairo_destroy(cr);
407
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500408 es = egl_surface_create_from_cairo_surface(ec, surface,
409 x, y, width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500410
411 cairo_surface_destroy(surface);
412
413 return es;
414}
415
416static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500417draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500418{
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500419 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500420 GLint vertices[12];
421 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
422 GLuint indices[4] = { 0, 1, 2, 3 };
423
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424 vertices[0] = es->map.x;
425 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500426 vertices[2] = 0;
427
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500428 vertices[3] = es->map.x;
429 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500430 vertices[5] = 0;
431
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500432 vertices[6] = es->map.x + es->map.width;
433 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434 vertices[8] = 0;
435
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500436 vertices[9] = es->map.x + es->map.width;
437 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500438 vertices[11] = 0;
439
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500440 if (es->visual == &ec->argb_visual) {
441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
442 glEnable(GL_BLEND);
443 } else if (es->visual == &ec->premultiplied_argb_visual) {
444 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
445 glEnable(GL_BLEND);
446 } else {
447 glDisable(GL_BLEND);
448 }
449
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500450 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500451 glEnable(GL_TEXTURE_2D);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500452 glEnableClientState(GL_VERTEX_ARRAY);
453 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
454 glVertexPointer(3, GL_INT, 0, vertices);
455 glTexCoordPointer(2, GL_INT, 0, tex_coords);
456 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
457}
458
459static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500460schedule_repaint(struct egl_compositor *ec);
461
462static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500463animate_overlay(struct egl_compositor *ec)
464{
465 double force, y;
466 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500467#if 1
468 double bounce = 0.0;
469 double friction = 1.0;
470 double spring = 0.2;
471#else
472 double bounce = 0.2;
473 double friction = 0.04;
474 double spring = 0.09;
475#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500476
477 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500478 force = (ec->overlay_target - ec->overlay_y) * spring +
479 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500480
481 ec->overlay_y = y + (y - ec->overlay_previous) + force;
482 ec->overlay_previous = y;
483
484 top = ec->height - ec->overlay->map.height;
485 bottom = ec->height;
486 if (ec->overlay_y >= bottom) {
487 ec->overlay_y = bottom;
488 ec->overlay_previous = bottom;
489 }
490
491 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500492 ec->overlay_y = top + bounce * (top - ec->overlay_y);
493 ec->overlay_previous =
494 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500495 }
496
497 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500498
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500499 if (fabs(y - ec->overlay_target) > 0.2 ||
500 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500501 schedule_repaint(ec);
502}
503
504static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500505repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400506{
507 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500508 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500509 struct egl_input_device *eid;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500510 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500511 uint32_t msecs;
512
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500513 if (!ec->repaint_needed) {
514 ec->repaint_on_timeout = 0;
515 return;
516 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -0500518 if (ec->background)
519 draw_surface(ec->background);
520 else
521 glClear(GL_COLOR_BUFFER_BIT);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400522
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500523 es = container_of(ec->surface_list.next,
524 struct egl_surface, link);
525 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500526 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500527
528 es = container_of(es->link.next,
529 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400530 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400531
Kristian Høgsberg54879822008-11-23 17:07:32 -0500532 draw_surface(ec->overlay);
533
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500534 eid = container_of(ec->input_device_list.next,
535 struct egl_input_device, link);
536 while (&eid->link != &ec->input_device_list) {
537 draw_surface(eid->pointer_surface);
538
539 eid = container_of(eid->link.next,
540 struct egl_input_device, link);
541 }
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500542
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400543 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500544 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500545
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500546 clock_gettime(CLOCK_MONOTONIC, &ts);
547 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500548 wl_display_post_frame(ec->wl_display, &ec->base,
549 ec->current_frame, msecs);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500550 ec->current_frame++;
551
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500552 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500553 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500554
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500555 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400556}
557
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500558static void
559schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400560{
561 struct wl_event_loop *loop;
562
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500563 ec->repaint_needed = 1;
564 if (!ec->repaint_on_timeout) {
565 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500566 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500567 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400568}
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400569
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500570static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500571surface_destroy(struct wl_client *client,
572 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400573{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500574 struct egl_surface *es = (struct egl_surface *) surface;
575 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500576
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500577 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500578 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400579
580 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400581}
582
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500583static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500584surface_attach(struct wl_client *client,
585 struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500586 uint32_t width, uint32_t height, uint32_t stride,
587 struct wl_object *visual)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400588{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500589 struct egl_surface *es = (struct egl_surface *) surface;
590 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400591
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500592 if (es->surface != EGL_NO_SURFACE)
593 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400594
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500595 es->width = width;
596 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500597 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500598 name, width, height, stride, NULL);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500599 if (visual == &ec->argb_visual.base)
600 es->visual = &ec->argb_visual;
601 else if (visual == &ec->premultiplied_argb_visual.base)
602 es->visual = &ec->premultiplied_argb_visual;
Kristian Høgsberge10b8282008-12-18 19:58:44 -0500603 else if (visual == &ec->rgb_visual.base)
604 es->visual = &ec->rgb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500605 else
606 /* FIXME: Smack client with an exception event */;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400607
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500608 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400609 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500610 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
611 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
612 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500613 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400614}
615
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500616static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500617surface_map(struct wl_client *client,
618 struct wl_surface *surface,
619 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400620{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500621 struct egl_surface *es = (struct egl_surface *) surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400622
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500623 es->map.x = x;
624 es->map.y = y;
625 es->map.width = width;
626 es->map.height = height;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400627}
628
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500629static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500630surface_copy(struct wl_client *client,
631 struct wl_surface *surface,
632 int32_t dst_x, int32_t dst_y,
633 uint32_t name, uint32_t stride,
634 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500635{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500636 struct egl_surface *es = (struct egl_surface *) surface;
637 struct egl_compositor *ec = es->compositor;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500638 EGLSurface src;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500639
640 /* FIXME: glCopyPixels should work, but then we'll have to
641 * call eglMakeCurrent to set up the src and dest surfaces
642 * first. This seems cheaper, but maybe there's a better way
643 * to accomplish this. */
644
645 src = eglCreateSurfaceForName(ec->display, ec->config,
646 name, x + width, y + height, stride, NULL);
647
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500648 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500649 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500650 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500651}
652
653static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500654surface_damage(struct wl_client *client,
655 struct wl_surface *surface,
656 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500657{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500658 /* FIXME: This need to take a damage region, of course. */
659}
660
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500661const static struct wl_surface_interface surface_interface = {
662 surface_destroy,
663 surface_attach,
664 surface_map,
665 surface_copy,
666 surface_damage
667};
668
669static void
670compositor_create_surface(struct wl_client *client,
671 struct wl_compositor *compositor, uint32_t id)
672{
673 struct egl_compositor *ec = (struct egl_compositor *) compositor;
674 struct egl_surface *es;
675
676 es = malloc(sizeof *es);
677 if (es == NULL)
678 /* FIXME: Send OOM event. */
679 return;
680
681 es->compositor = ec;
682 es->surface = EGL_NO_SURFACE;
683 wl_list_insert(ec->surface_list.prev, &es->link);
684 glGenTextures(1, &es->texture);
685 wl_client_add_surface(client, &es->base,
686 &surface_interface, id);
687}
688
689static void
690compositor_commit(struct wl_client *client,
691 struct wl_compositor *compositor, uint32_t key)
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500692{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500693 struct egl_compositor *ec = (struct egl_compositor *) compositor;
694
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500695 schedule_repaint(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500696 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500697}
698
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500699const static struct wl_compositor_interface compositor_interface = {
700 compositor_create_surface,
701 compositor_commit
702};
703
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500704static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500705pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500706{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500707 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500708 struct egl_surface *es;
709
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500710 if (device->grab > 0)
711 return device->grab_surface;
712
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500713 es = container_of(ec->surface_list.prev,
714 struct egl_surface, link);
715 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500716 if (es->map.x <= device->x &&
717 device->x < es->map.x + es->map.width &&
718 es->map.y <= device->y &&
719 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500720 return es;
721
722 es = container_of(es->link.prev,
723 struct egl_surface, link);
724 }
725
726 return NULL;
727}
728
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500729void
730notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500731{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500732 struct egl_surface *es;
Ray Strode90e701d2008-12-18 23:05:43 -0500733 struct egl_compositor *ec = device->ec;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500734 const int hotspot_x = 16, hotspot_y = 16;
735 int32_t sx, sy;
736
Ray Strode90e701d2008-12-18 23:05:43 -0500737 if (x < 0)
738 x = 0;
739 if (y < 0)
740 y = 0;
741 if (x >= ec->width)
742 x = ec->width - 1;
743 if (y >= ec->height)
744 y = ec->height - 1;
745
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500746 es = pick_surface(device);
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500747
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500748 if (es) {
749 sx = (x - es->map.x) * es->width / es->map.width;
750 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500751 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500752 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500753 }
754
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500755 device->x = x;
756 device->y = y;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500757 device->pointer_surface->map.x = x - hotspot_x;
758 device->pointer_surface->map.y = y - hotspot_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500759
760 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500761}
762
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763void
764notify_button(struct egl_input_device *device,
765 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500766{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500767 struct egl_surface *es;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500768 int32_t sx, sy;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500769
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500770 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500771 if (es) {
772 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500773 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500774
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500775 if (state) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500776 /* FIXME: We need callbacks when the surfaces
777 * we reference here go away. */
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500778 device->grab++;
779 device->grab_surface = es;
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500780 device->focus_surface = es;
Kristian Høgsberg29573bc2008-12-11 23:27:27 -0500781 } else {
782 device->grab--;
783 }
784
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500785 sx = (device->x - es->map.x) * es->width / es->map.width;
786 sy = (device->y - es->map.y) * es->height / es->map.height;
787
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500788 /* FIXME: Swallow click on raise? */
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500789 wl_surface_post_event(&es->base, &device->base,
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500790 WL_INPUT_BUTTON, button, state,
791 device->x, device->y, sx, sy);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500792
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500793 schedule_repaint(device->ec);
794 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500795}
796
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500797void
798notify_key(struct egl_input_device *device,
799 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500800{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500801 struct egl_compositor *ec = device->ec;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500802
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500803 if (key == KEY_ESC && state == 1) {
804 if (ec->overlay_target == ec->height)
805 ec->overlay_target -= 200;
806 else
807 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500808 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500809 } else if (!wl_list_empty(&ec->surface_list)) {
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500810 if (device->focus_surface != NULL)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500811 wl_surface_post_event(&device->focus_surface->base,
Kristian Høgsberga7700c82008-12-12 13:48:30 -0500812 &device->base,
813 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500814 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500815}
816
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500817struct evdev_input_device *
818evdev_input_device_create(struct egl_input_device *device,
819 struct wl_display *display, const char *path);
820
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500821static void
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500822create_input_device(struct egl_compositor *ec, const char *glob)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500823{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500824 struct egl_input_device *device;
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500825 struct dirent *de;
826 char path[PATH_MAX];
827 const char *by_path_dir = "/dev/input/by-path";
828 DIR *dir;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500829
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500830 device = malloc(sizeof *device);
831 if (device == NULL)
832 return;
833
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500834 memset(device, 0, sizeof *device);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500835 device->base.interface = wl_input_device_get_interface();
836 wl_display_add_object(ec->wl_display, &device->base);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500837 device->x = 100;
838 device->y = 100;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500839 device->pointer_surface = pointer_create(ec,
840 device->x, device->y, 64, 64);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500841 device->ec = ec;
842
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500843 dir = opendir(by_path_dir);
844 if (dir == NULL) {
845 fprintf(stderr, "couldn't read dir %s\n", by_path_dir);
846 return;
847 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500848
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500849 while (de = readdir(dir), de != NULL) {
850 if (fnmatch(glob, de->d_name, 0))
851 continue;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500852
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500853 snprintf(path, sizeof path, "%s/%s", by_path_dir, de->d_name);
854 evdev_input_device_create(device, ec->wl_display, path);
855 }
856 closedir(dir);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500857
Kristian Høgsbergc492b482008-12-12 12:00:02 -0500858 wl_list_insert(ec->input_device_list.prev, &device->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500859}
860
861void
862egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
863{
864 *x = device->x;
865 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500866}
867
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500868static uint32_t
Ray Strode19ad6a92008-12-19 01:45:41 -0500869create_frontbuffer(int fd, int *width, int *height, int *stride, uint32_t *fb_id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500870{
871 drmModeConnector *connector;
872 drmModeRes *resources;
873 drmModeEncoder *encoder;
874 struct drm_mode_modeinfo *mode;
875 struct drm_i915_gem_create create;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500876 struct drm_gem_flink flink;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500877 int i, ret;
878
879 resources = drmModeGetResources(fd);
880 if (!resources) {
881 fprintf(stderr, "drmModeGetResources failed\n");
882 return 0;
883 }
884
885 for (i = 0; i < resources->count_connectors; i++) {
886 connector = drmModeGetConnector(fd, resources->connectors[i]);
887 if (connector == NULL)
888 continue;
889
890 if (connector->connection == DRM_MODE_CONNECTED &&
891 connector->count_modes > 0)
892 break;
893
894 drmModeFreeConnector(connector);
895 }
896
897 if (i == resources->count_connectors) {
898 fprintf(stderr, "No currently active connector found.\n");
899 return -1;
900 }
901
902 mode = &connector->modes[0];
903
904 for (i = 0; i < resources->count_encoders; i++) {
905 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
906
907 if (encoder == NULL)
908 continue;
909
910 if (encoder->encoder_id == connector->encoder_id)
911 break;
912
913 drmModeFreeEncoder(encoder);
914 }
915
916 /* Mode size at 32 bpp */
917 create.size = mode->hdisplay * mode->vdisplay * 4;
918 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
919 fprintf(stderr, "gem create failed: %m\n");
920 return 0;
921 }
922
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500923 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
Ray Strode19ad6a92008-12-19 01:45:41 -0500924 32, 32, mode->hdisplay * 4, create.handle, fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500925 if (ret) {
926 fprintf(stderr, "failed to add fb: %m\n");
927 return 0;
928 }
929
Ray Strode19ad6a92008-12-19 01:45:41 -0500930 ret = drmModeSetCrtc(fd, encoder->crtc_id, *fb_id, 0, 0,
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500931 &connector->connector_id, 1, mode);
932 if (ret) {
933 fprintf(stderr, "failed to set mode: %m\n");
934 return 0;
935 }
936
937 flink.handle = create.handle;
938 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
939 fprintf(stderr, "gem flink failed: %m\n");
940 return 0;
941 }
942
943 *width = mode->hdisplay;
944 *height = mode->vdisplay;
945 *stride = mode->hdisplay * 4;
946
947 return flink.name;
948}
949
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500950static int
951pick_config(struct egl_compositor *ec)
952{
953 EGLConfig configs[100];
954 EGLint value, count;
955 int i;
956
957 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
958 fprintf(stderr, "failed to get configs\n");
959 return -1;
960 }
961
962 ec->config = EGL_NO_CONFIG;
963 for (i = 0; i < count; i++) {
964 eglGetConfigAttrib(ec->display,
965 configs[i],
966 EGL_DEPTH_SIZE,
967 &value);
968 if (value > 0) {
969 fprintf(stderr, "config %d has depth size %d\n", i, value);
970 continue;
971 }
972
973 eglGetConfigAttrib(ec->display,
974 configs[i],
975 EGL_STENCIL_SIZE,
976 &value);
977 if (value > 0) {
978 fprintf(stderr, "config %d has stencil size %d\n", i, value);
979 continue;
980 }
981
982 eglGetConfigAttrib(ec->display,
983 configs[i],
984 EGL_CONFIG_CAVEAT,
985 &value);
986 if (value != EGL_NONE) {
987 fprintf(stderr, "config %d has caveat %d\n", i, value);
988 continue;
989 }
990
991 ec->config = configs[i];
992 break;
993 }
994
995 if (ec->config == EGL_NO_CONFIG) {
996 fprintf(stderr, "found no config without depth or stencil buffers\n");
997 return -1;
998 }
999
1000 return 0;
1001}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001002
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001003static const struct wl_interface visual_interface = {
1004 "visual", 1,
1005};
1006
1007static void
1008add_visuals(struct egl_compositor *ec)
1009{
1010 ec->argb_visual.base.interface = &visual_interface;
1011 ec->argb_visual.base.implementation = NULL;
1012 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
1013 wl_display_add_global(ec->wl_display, &ec->argb_visual.base);
1014
1015 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1016 ec->premultiplied_argb_visual.base.implementation = NULL;
1017 wl_display_add_object(ec->wl_display,
1018 &ec->premultiplied_argb_visual.base);
1019 wl_display_add_global(ec->wl_display,
1020 &ec->premultiplied_argb_visual.base);
1021
1022 ec->rgb_visual.base.interface = &visual_interface;
1023 ec->rgb_visual.base.implementation = NULL;
1024 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
1025 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base);
1026}
1027
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001028static const char gem_device[] = "/dev/dri/card0";
1029
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001030static const char *macbook_air_default_input_device[] = {
Kristian Høgsberg64949972008-12-12 14:48:46 -05001031 "pci-0000:00:1d.2-usb-0:2:1*event*",
1032 NULL
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001033};
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001034
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001035static const char *option_background = "background.jpg";
1036static const char **option_input_devices = macbook_air_default_input_device;
1037
1038static const GOptionEntry option_entries[] = {
1039 { "background", 'b', 0, G_OPTION_ARG_STRING,
1040 &option_background, "Background image" },
1041 { "input-device", 'i', 0, G_OPTION_ARG_STRING_ARRAY,
1042 &option_input_devices, "Input device glob" },
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001043 { NULL }
1044};
1045
Ray Strode19ad6a92008-12-19 01:45:41 -05001046static void on_enter_vt(int signal_number, void *data)
1047{
1048 struct egl_compositor *ec = data;
1049
1050 drmModeConnector *connector;
1051 drmModeRes *resources;
1052 drmModeEncoder *encoder;
1053 struct drm_mode_modeinfo *mode;
1054 int i, ret;
1055 int fd;
1056
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001057 ioctl(ec->tty_fd, VT_RELDISP, VT_ACKACQ);
1058
Ray Strode19ad6a92008-12-19 01:45:41 -05001059 fd = ec->gem_fd;
1060 resources = drmModeGetResources(fd);
1061 if (!resources) {
1062 fprintf(stderr, "drmModeGetResources failed\n");
1063 return;
1064 }
1065
1066 for (i = 0; i < resources->count_connectors; i++) {
1067 connector = drmModeGetConnector(fd, resources->connectors[i]);
1068 if (connector == NULL)
1069 continue;
1070
1071 if (connector->connection == DRM_MODE_CONNECTED &&
1072 connector->count_modes > 0)
1073 break;
1074
1075 drmModeFreeConnector(connector);
1076 }
1077
1078 if (i == resources->count_connectors) {
1079 fprintf(stderr, "No currently active connector found.\n");
1080 return;
1081 }
1082
1083 mode = &connector->modes[0];
1084
1085 for (i = 0; i < resources->count_encoders; i++) {
1086 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
1087
1088 if (encoder == NULL)
1089 continue;
1090
1091 if (encoder->encoder_id == connector->encoder_id)
1092 break;
1093
1094 drmModeFreeEncoder(encoder);
1095 }
1096
1097 ret = drmModeSetCrtc(fd, encoder->crtc_id, ec->fb_id, 0, 0,
1098 &connector->connector_id, 1, mode);
1099 if (ret) {
1100 fprintf(stderr, "failed to set mode: %m\n");
1101 return;
1102 }
1103
1104}
1105
1106static void on_leave_vt(int signal_number, void *data)
1107{
1108 struct egl_compositor *ec = data;
1109
1110 ioctl (ec->tty_fd, VT_RELDISP, 1);
1111}
1112
1113static void watch_for_vt_changes(struct egl_compositor *ec, struct wl_event_loop *loop)
1114{
Ray Strode19ad6a92008-12-19 01:45:41 -05001115 struct vt_mode mode = { 0 };
1116
1117 ec->tty_fd = open("/dev/tty0", O_RDWR | O_NOCTTY);
1118 mode.mode = VT_PROCESS;
1119 mode.relsig = SIGUSR1;
1120 mode.acqsig = SIGUSR2;
1121
Kristian Høgsberg38ccd3a2008-12-19 10:15:35 -05001122 if (!ioctl (ec->tty_fd, VT_SETMODE, &mode) < 0) {
Ray Strode19ad6a92008-12-19 01:45:41 -05001123 fprintf(stderr, "failed to take control of vt handling\n");
1124 }
1125
1126 ec->leave_vt_source = wl_event_loop_add_signal (loop, SIGUSR1,
1127 on_leave_vt,
1128 ec);
1129 ec->enter_vt_source = wl_event_loop_add_signal (loop, SIGUSR2,
1130 on_enter_vt,
1131 ec);
1132}
1133
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001134static struct egl_compositor *
1135egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001136{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001137 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001138 struct egl_compositor *ec;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001139 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001140 uint32_t fb_name;
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001141 int i, stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001142 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001143 const static EGLint attribs[] =
1144 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001145
1146 ec = malloc(sizeof *ec);
1147 if (ec == NULL)
1148 return NULL;
1149
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001150 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001151
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001152 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001153 if (ec->display == NULL) {
1154 fprintf(stderr, "failed to create display\n");
1155 return NULL;
1156 }
1157
1158 if (!eglInitialize(ec->display, &major, &minor)) {
1159 fprintf(stderr, "failed to initialize display\n");
1160 return NULL;
1161 }
1162
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001163 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001164 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001165
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001166 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
Ray Strode19ad6a92008-12-19 01:45:41 -05001167 &ec->width, &ec->height, &stride, &ec->fb_id);
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001168 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1169 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001170 if (ec->surface == NULL) {
1171 fprintf(stderr, "failed to create surface\n");
1172 return NULL;
1173 }
1174
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001175 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001176 if (ec->context == NULL) {
1177 fprintf(stderr, "failed to create context\n");
1178 return NULL;
1179 }
1180
1181 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1182 fprintf(stderr, "failed to make context current\n");
1183 return NULL;
1184 }
1185
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001186 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001187 glMatrixMode(GL_PROJECTION);
1188 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001189 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001190 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberg5b7f8322008-12-18 12:08:19 -05001191 glClearColor(0, 0, 0.2, 1);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001192
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001193 wl_display_set_compositor(display, &ec->base, &compositor_interface);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001194 add_visuals(ec);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001195
Kristian Høgsbergc492b482008-12-12 12:00:02 -05001196 wl_list_init(&ec->input_device_list);
1197 for (i = 0; option_input_devices[i]; i++)
1198 create_input_device(ec, option_input_devices[i]);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001199
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001200 wl_list_init(&ec->surface_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001201 ec->background = background_create(ec, option_background,
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001202 ec->width, ec->height);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -05001203 ec->overlay = overlay_create(ec, 0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001204 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001205 ec->overlay_target = ec->height;
1206 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001207
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001208 ec->gem_fd = open(gem_device, O_RDWR);
1209 if (ec->gem_fd < 0) {
1210 fprintf(stderr, "failed to open drm device\n");
1211 return NULL;
1212 }
1213
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001214 shooter = screenshooter_create(ec);
1215 wl_display_add_object(display, &shooter->base);
1216 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001217
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001218 loop = wl_display_get_event_loop(ec->wl_display);
Ray Strode19ad6a92008-12-19 01:45:41 -05001219 watch_for_vt_changes (ec, loop);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001220 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001221 ec->repaint_needed = 0;
1222 ec->repaint_on_timeout = 0;
1223
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001224 schedule_repaint(ec);
1225
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001226 return ec;
1227}
1228
1229/* The plan here is to generate a random anonymous socket name and
1230 * advertise that through a service on the session dbus.
1231 */
1232static const char socket_name[] = "\0wayland";
1233
1234int main(int argc, char *argv[])
1235{
1236 struct wl_display *display;
1237 struct egl_compositor *ec;
Kristian Høgsbergd6531262008-12-12 11:06:18 -05001238 GError *error = NULL;
1239 GOptionContext *context;
1240
1241 context = g_option_context_new(NULL);
1242 g_option_context_add_main_entries(context, option_entries, "Wayland");
1243 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1244 fprintf(stderr, "option parsing failed: %s\n", error->message);
1245 exit(EXIT_FAILURE);
1246 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001247
1248 display = wl_display_create();
1249
1250 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001251 if (ec == NULL) {
1252 fprintf(stderr, "failed to create compositor\n");
1253 exit(EXIT_FAILURE);
1254 }
1255
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001256 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001257 fprintf(stderr, "failed to add socket: %m\n");
1258 exit(EXIT_FAILURE);
1259 }
1260
1261 wl_display_run(display);
1262
1263 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001264}