blob: 77b9fbb72c898b14b8e10d73a1710274f24bfd37 [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>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050041#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050042#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <GL/gl.h>
45#include <eagle.h>
46
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050047#include "wayland.h"
48#include "cairo-util.h"
49#include "egl-compositor.h"
50
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040051#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
52
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050053struct egl_input_device {
54 struct wl_object base;
55 int32_t x, y;
56 struct egl_compositor *ec;
57 struct egl_surface *surface;
58};
59
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040060struct egl_compositor {
61 struct wl_compositor base;
62 EGLDisplay display;
63 EGLSurface surface;
64 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050065 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040066 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040067 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050068 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050069 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050070 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050071 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050072
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050073 struct egl_input_device *input_device;
74
Kristian Høgsberg201a9042008-12-10 00:40:50 -050075 struct wl_list surface_list;
76
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050077 /* Repaint state. */
78 struct wl_event_source *timer_source;
79 int repaint_needed;
80 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050081 struct timespec previous_swap;
82 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040083};
84
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050085struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040086 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040087 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050088 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050089 struct wl_surface *wl_surface;
90 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050091 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040092};
93
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050094static void
95die(const char *msg, ...)
96{
97 va_list ap;
98
99 va_start (ap, msg);
100 vfprintf(stderr, msg, ap);
101 va_end (ap);
102
103 exit(EXIT_FAILURE);
104}
105
106static void
107stdio_write_func (png_structp png, png_bytep data, png_size_t size)
108{
109 FILE *fp;
110 size_t ret;
111
112 fp = png_get_io_ptr (png);
113 while (size) {
114 ret = fwrite (data, 1, size, fp);
115 size -= ret;
116 data += ret;
117 if (size && ferror (fp))
118 die("write: %m\n");
119 }
120}
121
122static void
123png_simple_output_flush_fn (png_structp png_ptr)
124{
125}
126
127static void
128png_simple_error_callback (png_structp png,
129 png_const_charp error_msg)
130{
131 die("png error: %s\n", error_msg);
132}
133
134static void
135png_simple_warning_callback (png_structp png,
136 png_const_charp error_msg)
137{
138 fprintf(stderr, "png warning: %s\n", error_msg);
139}
140
141static void
142convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
143{
144 unsigned int i;
145
146 for (i = 0; i < row_info->rowbytes; i += 4) {
147 uint8_t *b = &data[i];
148 uint32_t pixel;
149
150 memcpy (&pixel, b, sizeof (uint32_t));
151 b[0] = (pixel & 0xff0000) >> 16;
152 b[1] = (pixel & 0x00ff00) >> 8;
153 b[2] = (pixel & 0x0000ff) >> 0;
154 b[3] = 0;
155 }
156}
157
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500158struct screenshooter {
159 struct wl_object base;
160 struct egl_compositor *ec;
161};
162
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500163static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500164screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500165{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500166 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500167 png_struct *png;
168 png_info *info;
169 png_byte **volatile rows = NULL;
170 png_color_16 white;
171 int depth, i;
172 FILE *fp;
173 uint8_t *data;
174 GLuint stride;
175 static const char filename[] = "wayland-screenshot.png";
176
177 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
178 if (data == NULL)
179 die("eglReadBuffer failed\n");
180 rows = malloc(ec->height * sizeof rows[0]);
181 if (rows == NULL)
182 die("malloc failed\n");
183
184 for (i = 0; i < ec->height; i++)
185 rows[i] = (png_byte *) data + i * stride;
186
187 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
188 png_simple_error_callback,
189 png_simple_warning_callback);
190 if (png == NULL)
191 die("png_create_write_struct failed\n");
192
193 info = png_create_info_struct(png);
194 if (info == NULL)
195 die("png_create_info_struct failed\n");
196
197 fp = fopen(filename, "w");
198 if (fp == NULL)
199 die("fopen failed: %m\n");
200
201 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
202
203 depth = 8;
204 png_set_IHDR(png, info,
205 ec->width,
206 ec->height, depth,
207 PNG_COLOR_TYPE_RGB,
208 PNG_INTERLACE_NONE,
209 PNG_COMPRESSION_TYPE_DEFAULT,
210 PNG_FILTER_TYPE_DEFAULT);
211
212 white.gray = (1 << depth) - 1;
213 white.red = white.blue = white.green = white.gray;
214 png_set_bKGD(png, info, &white);
215 png_write_info (png, info);
216 png_set_write_user_transform_fn(png, convert_pixels);
217
218 png_set_filler(png, 0, PNG_FILLER_AFTER);
219 png_write_image(png, rows);
220 png_write_end(png, info);
221
222 png_destroy_write_struct(&png, &info);
223 fclose(fp);
224 free(rows);
225 free(data);
226}
227
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500228static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500229 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500230};
231
232static const struct wl_interface screenshooter_interface = {
233 "screenshooter", 1,
234 ARRAY_LENGTH(screenshooter_methods),
235 screenshooter_methods,
236};
237
238static struct screenshooter *
239screenshooter_create(struct egl_compositor *ec)
240{
241 struct screenshooter *shooter;
242
243 shooter = malloc(sizeof *shooter);
244 if (shooter == NULL)
245 return NULL;
246
247 shooter->base.interface = &screenshooter_interface;
248 shooter->ec = ec;
249
250 return shooter;
251};
252
Kristian Høgsberg54879822008-11-23 17:07:32 -0500253static struct egl_surface *
254egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
255 int x, int y, int width, int height)
256{
257 struct egl_surface *es;
258 int stride;
259 void *data;
260
261 stride = cairo_image_surface_get_stride(surface);
262 data = cairo_image_surface_get_data(surface);
263
264 es = malloc(sizeof *es);
265 if (es == NULL)
266 return NULL;
267
268 glGenTextures(1, &es->texture);
269 glBindTexture(GL_TEXTURE_2D, es->texture);
270 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500271 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
272 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500274 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
275 GL_BGRA, GL_UNSIGNED_BYTE, data);
276
277 es->map.x = x;
278 es->map.y = y;
279 es->map.width = width;
280 es->map.height = height;
281 es->surface = EGL_NO_SURFACE;
282
283 return es;
284}
285
286static void
287egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
288{
289 glDeleteTextures(1, &es->texture);
290 if (es->surface != EGL_NO_SURFACE)
291 eglDestroySurface(ec->display, es->surface);
292 free(es);
293}
294
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400295static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500296pointer_path(cairo_t *cr, int x, int y)
297{
298 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
299 const int width = 16, height = 16;
300
301 cairo_move_to(cr, x, y);
302 cairo_line_to(cr, x + tx, y + ty);
303 cairo_line_to(cr, x + dx, y + dy);
304 cairo_line_to(cr, x + width - end, y + height);
305 cairo_line_to(cr, x + width, y + height - end);
306 cairo_line_to(cr, x + dy, y + dx);
307 cairo_line_to(cr, x + ty, y + tx);
308 cairo_close_path(cr);
309}
310
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500311static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500312pointer_create(int x, int y, int width, int height)
313{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500314 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500316 cairo_surface_t *surface;
317 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500318
319 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
320 width, height);
321
322 cr = cairo_create(surface);
323 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
324 cairo_set_line_width (cr, 2);
325 cairo_set_source_rgb(cr, 0, 0, 0);
326 cairo_stroke_preserve(cr);
327 cairo_fill(cr);
328 blur_surface(surface, width);
329
330 pointer_path(cr, hotspot_x, hotspot_y);
331 cairo_stroke_preserve(cr);
332 cairo_set_source_rgb(cr, 1, 1, 1);
333 cairo_fill(cr);
334 cairo_destroy(cr);
335
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500336 es = egl_surface_create_from_cairo_surface(surface,
337 x - hotspot_x,
338 y - hotspot_y,
339 width, height);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500340
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500341 cairo_surface_destroy(surface);
342
Kristian Høgsberg54879822008-11-23 17:07:32 -0500343 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500344}
345
346static struct egl_surface *
347background_create(const char *filename, int width, int height)
348{
349 struct egl_surface *background;
350 GdkPixbuf *pixbuf;
351 GError *error = NULL;
352 int pixbuf_width, pixbuf_height;
353 void *data;
354
355 background = malloc(sizeof *background);
356 if (background == NULL)
357 return NULL;
358
359 g_type_init();
360
361 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
362 if (error != NULL) {
363 free(background);
364 return NULL;
365 }
366
367 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
368 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
369 data = gdk_pixbuf_get_pixels(pixbuf);
370
371 glGenTextures(1, &background->texture);
372 glBindTexture(GL_TEXTURE_2D, background->texture);
373 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500377 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
378 GL_BGR, GL_UNSIGNED_BYTE, data);
379
380 background->map.x = 0;
381 background->map.y = 0;
382 background->map.width = width;
383 background->map.height = height;
384 background->surface = EGL_NO_SURFACE;
385
386 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500387}
388
389static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500390rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
391{
392 cairo_move_to(cr, x0, y0 + radius);
393 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
394 cairo_line_to(cr, x1 - radius, y0);
395 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
396 cairo_line_to(cr, x1, y1 - radius);
397 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
398 cairo_line_to(cr, x0 + radius, y1);
399 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
400 cairo_close_path(cr);
401}
402
403static void
404draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
405{
406 cairo_pattern_t *gradient;
407 cairo_text_extents_t extents;
408 double bright = 0.15, dim = 0.02;
409 int radius = 10;
410
411 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
412 cairo_set_line_width (cr, 2);
413 rounded_rect(cr, x, y, x + width, y + height, radius);
414 cairo_set_source_rgb(cr, dim, dim, dim);
415 cairo_stroke(cr);
416 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
417 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
418 cairo_stroke(cr);
419
420 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
421 cairo_set_source_rgb(cr, bright, bright, bright);
422 cairo_stroke(cr);
423 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
424 cairo_set_source_rgb(cr, dim, dim, dim);
425 cairo_stroke(cr);
426
427 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
428 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
429 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
430 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
431 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
432 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
433 cairo_set_source(cr, gradient);
434 cairo_fill(cr);
435
436 cairo_set_font_size(cr, 16);
437 cairo_text_extents(cr, text, &extents);
438 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
439 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
440 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
441 cairo_set_line_width (cr, 4);
442 cairo_text_path(cr, text);
443 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
444 cairo_stroke_preserve(cr);
445 cairo_set_source_rgb(cr, 1, 1, 1);
446 cairo_fill(cr);
447}
448
449static struct egl_surface *
450overlay_create(int x, int y, int width, int height)
451{
452 struct egl_surface *es;
453 cairo_surface_t *surface;
454 cairo_t *cr;
455 int total_width, button_x, button_y;
456 const int button_width = 150;
457 const int button_height = 40;
458 const int spacing = 50;
459
460 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
461 width, height);
462
463 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500464 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500465 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
466 cairo_paint(cr);
467
468 total_width = button_width * 2 + spacing;
469 button_x = (width - total_width) / 2;
470 button_y = height - button_height - 20;
471 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
472 button_x += button_width + spacing;
473 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
474
475 cairo_destroy(cr);
476
477 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
478
479 cairo_surface_destroy(surface);
480
481 return es;
482}
483
484static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500485draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500486{
487 GLint vertices[12];
488 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
489 GLuint indices[4] = { 0, 1, 2, 3 };
490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 vertices[0] = es->map.x;
492 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500493 vertices[2] = 0;
494
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500495 vertices[3] = es->map.x;
496 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500497 vertices[5] = 0;
498
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500499 vertices[6] = es->map.x + es->map.width;
500 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500501 vertices[8] = 0;
502
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500503 vertices[9] = es->map.x + es->map.width;
504 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500505 vertices[11] = 0;
506
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500507 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500508 glEnable(GL_TEXTURE_2D);
509 glEnable(GL_BLEND);
510 /* Assume pre-multiplied alpha for now, this probably
511 * needs to be a wayland visual type of thing. */
512 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
513
514 glEnableClientState(GL_VERTEX_ARRAY);
515 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
516 glVertexPointer(3, GL_INT, 0, vertices);
517 glTexCoordPointer(2, GL_INT, 0, tex_coords);
518 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
519}
520
521static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500522schedule_repaint(struct egl_compositor *ec);
523
524static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500525animate_overlay(struct egl_compositor *ec)
526{
527 double force, y;
528 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500529#if 1
530 double bounce = 0.0;
531 double friction = 1.0;
532 double spring = 0.2;
533#else
534 double bounce = 0.2;
535 double friction = 0.04;
536 double spring = 0.09;
537#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500538
539 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500540 force = (ec->overlay_target - ec->overlay_y) * spring +
541 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500542
543 ec->overlay_y = y + (y - ec->overlay_previous) + force;
544 ec->overlay_previous = y;
545
546 top = ec->height - ec->overlay->map.height;
547 bottom = ec->height;
548 if (ec->overlay_y >= bottom) {
549 ec->overlay_y = bottom;
550 ec->overlay_previous = bottom;
551 }
552
553 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500554 ec->overlay_y = top + bounce * (top - ec->overlay_y);
555 ec->overlay_previous =
556 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500557 }
558
559 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500560
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500561 if (fabs(y - ec->overlay_target) > 0.2 ||
562 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500563 schedule_repaint(ec);
564}
565
566static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500567repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400568{
569 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500570 struct egl_surface *es;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500571 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500572 uint32_t msecs;
573
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500574 if (!ec->repaint_needed) {
575 ec->repaint_on_timeout = 0;
576 return;
577 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500578
579 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400580
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500581 es = container_of(ec->surface_list.next,
582 struct egl_surface, link);
583 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500584 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500585
586 es = container_of(es->link.next,
587 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400588 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400589
Kristian Høgsberg54879822008-11-23 17:07:32 -0500590 draw_surface(ec->overlay);
591
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500592 draw_surface(ec->input_device->surface);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500593
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400594 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500595 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500596
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500597 clock_gettime(CLOCK_MONOTONIC, &ts);
598 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
599 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
600 ec->current_frame++;
601
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500602 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500603 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500604
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500605 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400606}
607
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500608static void
609schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400610{
611 struct wl_event_loop *loop;
612
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500613 ec->repaint_needed = 1;
614 if (!ec->repaint_on_timeout) {
615 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500616 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500617 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400618}
619
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500620static void
621notify_surface_create(struct wl_compositor *compositor,
622 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400623{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500624 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500625 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400626
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500627 es = malloc(sizeof *es);
628 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400629 return;
630
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500631 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500632 es->wl_surface = surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500633 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500634 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400635
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500636 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400637}
638
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500639static void
640notify_surface_destroy(struct wl_compositor *compositor,
641 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400642{
643 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500644 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400645
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500646 es = wl_surface_get_data(surface);
647 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400648 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500649
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500650 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500651 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400652
653 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400654}
655
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500656static void
657notify_surface_attach(struct wl_compositor *compositor,
658 struct wl_surface *surface, uint32_t name,
659 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400660{
661 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500662 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400663
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500664 es = wl_surface_get_data(surface);
665 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400666 return;
667
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500668 if (es->surface != EGL_NO_SURFACE)
669 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400670
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500671 es->width = width;
672 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500673 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500674 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400675
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500676 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400677 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500678 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
679 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
680 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500681 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400682}
683
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500684static void
685notify_surface_map(struct wl_compositor *compositor,
686 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400687{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500688 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400689
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500690 es = wl_surface_get_data(surface);
691 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400692 return;
693
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500694 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400695}
696
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500697static void
698notify_surface_copy(struct wl_compositor *compositor,
699 struct wl_surface *surface,
700 int32_t dst_x, int32_t dst_y,
701 uint32_t name, uint32_t stride,
702 int32_t x, int32_t y, int32_t width, int32_t height)
703{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500704 struct egl_compositor *ec = (struct egl_compositor *) compositor;
705 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500706 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500707
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500708 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500709
710 /* FIXME: glCopyPixels should work, but then we'll have to
711 * call eglMakeCurrent to set up the src and dest surfaces
712 * first. This seems cheaper, but maybe there's a better way
713 * to accomplish this. */
714
715 src = eglCreateSurfaceForName(ec->display, ec->config,
716 name, x + width, y + height, stride, NULL);
717
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500718 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500719 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500720 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500721}
722
723static void
724notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500725 struct wl_surface *surface,
726 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500727{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500728 /* FIXME: This need to take a damage region, of course. */
729}
730
731static uint32_t
732notify_commit(struct wl_compositor *compositor)
733{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500734 struct egl_compositor *ec = (struct egl_compositor *) compositor;
735
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500736 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500737
738 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500739}
740
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500741static struct egl_surface *
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500742pick_surface(struct egl_input_device *device)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500743{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500744 struct egl_compositor *ec = device->ec;
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500745 struct egl_surface *es;
746
747 es = container_of(ec->surface_list.prev,
748 struct egl_surface, link);
749 while (&es->link != &ec->surface_list) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500750 if (es->map.x <= device->x &&
751 device->x < es->map.x + es->map.width &&
752 es->map.y <= device->y &&
753 device->y < es->map.y + es->map.height)
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500754 return es;
755
756 es = container_of(es->link.prev,
757 struct egl_surface, link);
758 }
759
760 return NULL;
761}
762
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500763void
764notify_motion(struct egl_input_device *device, int x, int y)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500765{
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500766 struct egl_surface *es;
767 const int hotspot_x = 16, hotspot_y = 16;
768 int32_t sx, sy;
769
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500770 es = pick_surface(device);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500771 if (es) {
772 sx = (x - es->map.x) * es->width / es->map.width;
773 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500774 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500775 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500776 }
777
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500778 device->x = x;
779 device->y = y;
780 device->surface->map.x = x - hotspot_x;
781 device->surface->map.y = y - hotspot_y;
782
783 schedule_repaint(device->ec);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500784}
785
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500786void
787notify_button(struct egl_input_device *device,
788 int32_t button, int32_t state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500789{
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500790 struct egl_surface *es;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500791
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500792 es = pick_surface(device);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500793 if (es) {
794 wl_list_remove(&es->link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500795 wl_list_insert(device->ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500796
797 /* FIXME: Swallow click on raise? */
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500798 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500799 WL_INPUT_BUTTON, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500800
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500801 schedule_repaint(device->ec);
802 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500803}
804
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500805void
806notify_key(struct egl_input_device *device,
807 uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500808{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500809 struct egl_compositor *ec = device->ec;
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500810 struct egl_surface *es;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500811
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500812 if (key == KEY_ESC && state == 1) {
813 if (ec->overlay_target == ec->height)
814 ec->overlay_target -= 200;
815 else
816 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500817 schedule_repaint(ec);
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500818 } else if (!wl_list_empty(&ec->surface_list)) {
819 /* FIXME: The event source device should track which
820 * surface has its key focus and send the event there.
821 * For now, just send it to the top surface, which
822 * effectively gives us click to focus behavior. */
823 es = container_of(ec->surface_list.prev,
824 struct egl_surface, link);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500825 wl_surface_post_event(es->wl_surface, &device->base,
Kristian Høgsberg7fdff042008-12-10 13:25:00 -0500826 WL_INPUT_KEY, key, state);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500827 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500828}
829
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500830static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400831 notify_surface_create,
832 notify_surface_destroy,
833 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500834 notify_surface_map,
835 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500836 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500837 notify_commit,
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400838};
839
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500840static const char pointer_device_file[] =
841 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
842static const char keyboard_device_file[] =
843 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-kbd";
844
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500845struct evdev_input_device *
846evdev_input_device_create(struct egl_input_device *device,
847 struct wl_display *display, const char *path);
848
849void
850egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y);
851
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500852static void
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500853create_input_devices(struct egl_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500854{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500855 struct egl_input_device *device;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500856 const char *path;
857
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500858 device = malloc(sizeof *device);
859 if (device == NULL)
860 return;
861
862 device->base.interface = wl_input_device_get_interface();
863 wl_display_add_object(ec->wl_display, &device->base);
864 ec->input_device = device;
865 device->x = 100;
866 device->y = 100;
867 device->surface = pointer_create(device->x, device->y, 64, 64);
868 device->ec = ec;
869
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500870 path = getenv("WAYLAND_POINTER");
871 if (path == NULL)
872 path = pointer_device_file;
873
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500874 evdev_input_device_create(device, ec->wl_display, path);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500875
876 path = getenv("WAYLAND_KEYBOARD");
877 if (path == NULL)
878 path = keyboard_device_file;
879
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500880 evdev_input_device_create(device, ec->wl_display, path);
881}
882
883void
884egl_device_get_position(struct egl_input_device *device, int32_t *x, int32_t *y)
885{
886 *x = device->x;
887 *y = device->y;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500888}
889
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500890static uint32_t
891create_frontbuffer(int fd, int *width, int *height, int *stride)
892{
893 drmModeConnector *connector;
894 drmModeRes *resources;
895 drmModeEncoder *encoder;
896 struct drm_mode_modeinfo *mode;
897 struct drm_i915_gem_create create;
898 struct drm_i915_gem_pin pin;
899 struct drm_gem_flink flink;
900 unsigned int fb_id;
901 int i, ret;
902
903 resources = drmModeGetResources(fd);
904 if (!resources) {
905 fprintf(stderr, "drmModeGetResources failed\n");
906 return 0;
907 }
908
909 for (i = 0; i < resources->count_connectors; i++) {
910 connector = drmModeGetConnector(fd, resources->connectors[i]);
911 if (connector == NULL)
912 continue;
913
914 if (connector->connection == DRM_MODE_CONNECTED &&
915 connector->count_modes > 0)
916 break;
917
918 drmModeFreeConnector(connector);
919 }
920
921 if (i == resources->count_connectors) {
922 fprintf(stderr, "No currently active connector found.\n");
923 return -1;
924 }
925
926 mode = &connector->modes[0];
927
928 for (i = 0; i < resources->count_encoders; i++) {
929 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
930
931 if (encoder == NULL)
932 continue;
933
934 if (encoder->encoder_id == connector->encoder_id)
935 break;
936
937 drmModeFreeEncoder(encoder);
938 }
939
940 /* Mode size at 32 bpp */
941 create.size = mode->hdisplay * mode->vdisplay * 4;
942 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
943 fprintf(stderr, "gem create failed: %m\n");
944 return 0;
945 }
946
947 pin.handle = create.handle;
948 pin.alignment = 4096;
949 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
950 fprintf(stderr, "failed to pin buffer: %m\n");
951 return 0;
952 }
953
954 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
955 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
956 if (ret) {
957 fprintf(stderr, "failed to add fb: %m\n");
958 return 0;
959 }
960
961 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
962 &connector->connector_id, 1, mode);
963 if (ret) {
964 fprintf(stderr, "failed to set mode: %m\n");
965 return 0;
966 }
967
968 flink.handle = create.handle;
969 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
970 fprintf(stderr, "gem flink failed: %m\n");
971 return 0;
972 }
973
974 *width = mode->hdisplay;
975 *height = mode->vdisplay;
976 *stride = mode->hdisplay * 4;
977
978 return flink.name;
979}
980
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500981static int
982pick_config(struct egl_compositor *ec)
983{
984 EGLConfig configs[100];
985 EGLint value, count;
986 int i;
987
988 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
989 fprintf(stderr, "failed to get configs\n");
990 return -1;
991 }
992
993 ec->config = EGL_NO_CONFIG;
994 for (i = 0; i < count; i++) {
995 eglGetConfigAttrib(ec->display,
996 configs[i],
997 EGL_DEPTH_SIZE,
998 &value);
999 if (value > 0) {
1000 fprintf(stderr, "config %d has depth size %d\n", i, value);
1001 continue;
1002 }
1003
1004 eglGetConfigAttrib(ec->display,
1005 configs[i],
1006 EGL_STENCIL_SIZE,
1007 &value);
1008 if (value > 0) {
1009 fprintf(stderr, "config %d has stencil size %d\n", i, value);
1010 continue;
1011 }
1012
1013 eglGetConfigAttrib(ec->display,
1014 configs[i],
1015 EGL_CONFIG_CAVEAT,
1016 &value);
1017 if (value != EGL_NONE) {
1018 fprintf(stderr, "config %d has caveat %d\n", i, value);
1019 continue;
1020 }
1021
1022 ec->config = configs[i];
1023 break;
1024 }
1025
1026 if (ec->config == EGL_NO_CONFIG) {
1027 fprintf(stderr, "found no config without depth or stencil buffers\n");
1028 return -1;
1029 }
1030
1031 return 0;
1032}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001033
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001034static const char gem_device[] = "/dev/dri/card0";
1035
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001036static struct egl_compositor *
1037egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001038{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001039 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001040 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001041 const char *filename;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001042 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001043 uint32_t fb_name;
1044 int stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001045 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001046 const static EGLint attribs[] =
1047 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001048
1049 ec = malloc(sizeof *ec);
1050 if (ec == NULL)
1051 return NULL;
1052
1053 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001054 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001055
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001056 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001057 if (ec->display == NULL) {
1058 fprintf(stderr, "failed to create display\n");
1059 return NULL;
1060 }
1061
1062 if (!eglInitialize(ec->display, &major, &minor)) {
1063 fprintf(stderr, "failed to initialize display\n");
1064 return NULL;
1065 }
1066
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001067 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001068 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001069
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001070 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1071 &ec->width, &ec->height, &stride);
1072 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1073 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001074 if (ec->surface == NULL) {
1075 fprintf(stderr, "failed to create surface\n");
1076 return NULL;
1077 }
1078
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001079 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001080 if (ec->context == NULL) {
1081 fprintf(stderr, "failed to create context\n");
1082 return NULL;
1083 }
1084
1085 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1086 fprintf(stderr, "failed to make context current\n");
1087 return NULL;
1088 }
1089
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001090 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001091 glMatrixMode(GL_PROJECTION);
1092 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001093 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001094 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001095
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05001096 create_input_devices(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001097
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001098 wl_list_init(&ec->surface_list);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001099 filename = getenv("WAYLAND_BACKGROUND");
1100 if (filename == NULL)
1101 filename = "background.jpg";
1102 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001103 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001104 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001105 ec->overlay_target = ec->height;
1106 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001107
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001108 ec->gem_fd = open(gem_device, O_RDWR);
1109 if (ec->gem_fd < 0) {
1110 fprintf(stderr, "failed to open drm device\n");
1111 return NULL;
1112 }
1113
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001114 shooter = screenshooter_create(ec);
1115 wl_display_add_object(display, &shooter->base);
1116 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001117
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001118 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001119 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001120 ec->repaint_needed = 0;
1121 ec->repaint_on_timeout = 0;
1122
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001123 schedule_repaint(ec);
1124
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001125 return ec;
1126}
1127
1128/* The plan here is to generate a random anonymous socket name and
1129 * advertise that through a service on the session dbus.
1130 */
1131static const char socket_name[] = "\0wayland";
1132
1133int main(int argc, char *argv[])
1134{
1135 struct wl_display *display;
1136 struct egl_compositor *ec;
1137
1138 display = wl_display_create();
1139
1140 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001141 if (ec == NULL) {
1142 fprintf(stderr, "failed to create compositor\n");
1143 exit(EXIT_FAILURE);
1144 }
1145
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001146 wl_display_set_compositor(display, &ec->base);
1147
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001148 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001149 fprintf(stderr, "failed to add socket: %m\n");
1150 exit(EXIT_FAILURE);
1151 }
1152
1153 wl_display_run(display);
1154
1155 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001156}