blob: 7d6cd0e21f2bec36f7503de7d1bc483a5bd8f707 [file] [log] [blame]
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05005#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006#include <i915_drm.h>
7#include <sys/ioctl.h>
8#include <sys/mman.h>
9#include <fcntl.h>
10#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050011#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050012#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050013#include <gdk-pixbuf/gdk-pixbuf.h>
14#include <glib.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050015#include <sys/poll.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050016#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050017#include <math.h>
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -050018#include <linux/input.h>
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -050019#include <xf86drmMode.h>
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050020#include <sys/timerfd.h>
21#include <time.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040022
23#include "wayland.h"
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050024#include "cairo-util.h"
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040025
26#include <GL/gl.h>
27#include <eagle.h>
28
29#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
30
31struct egl_compositor {
32 struct wl_compositor base;
33 EGLDisplay display;
34 EGLSurface surface;
35 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050036 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040037 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050039 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050040 struct egl_surface *pointer;
41 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050042 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050043 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050044
45 /* Repaint state. */
46 struct wl_event_source *timer_source;
47 int repaint_needed;
48 int repaint_on_timeout;
49 int timer_fd;
50 struct timespec previous_swap;
51 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040052};
53
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050054struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040055 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040056 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050057 EGLSurface surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040058};
59
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050060static void
61die(const char *msg, ...)
62{
63 va_list ap;
64
65 va_start (ap, msg);
66 vfprintf(stderr, msg, ap);
67 va_end (ap);
68
69 exit(EXIT_FAILURE);
70}
71
72static void
73stdio_write_func (png_structp png, png_bytep data, png_size_t size)
74{
75 FILE *fp;
76 size_t ret;
77
78 fp = png_get_io_ptr (png);
79 while (size) {
80 ret = fwrite (data, 1, size, fp);
81 size -= ret;
82 data += ret;
83 if (size && ferror (fp))
84 die("write: %m\n");
85 }
86}
87
88static void
89png_simple_output_flush_fn (png_structp png_ptr)
90{
91}
92
93static void
94png_simple_error_callback (png_structp png,
95 png_const_charp error_msg)
96{
97 die("png error: %s\n", error_msg);
98}
99
100static void
101png_simple_warning_callback (png_structp png,
102 png_const_charp error_msg)
103{
104 fprintf(stderr, "png warning: %s\n", error_msg);
105}
106
107static void
108convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
109{
110 unsigned int i;
111
112 for (i = 0; i < row_info->rowbytes; i += 4) {
113 uint8_t *b = &data[i];
114 uint32_t pixel;
115
116 memcpy (&pixel, b, sizeof (uint32_t));
117 b[0] = (pixel & 0xff0000) >> 16;
118 b[1] = (pixel & 0x00ff00) >> 8;
119 b[2] = (pixel & 0x0000ff) >> 0;
120 b[3] = 0;
121 }
122}
123
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500124struct screenshooter {
125 struct wl_object base;
126 struct egl_compositor *ec;
127};
128
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500129static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500130screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500131{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500132 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500133 png_struct *png;
134 png_info *info;
135 png_byte **volatile rows = NULL;
136 png_color_16 white;
137 int depth, i;
138 FILE *fp;
139 uint8_t *data;
140 GLuint stride;
141 static const char filename[] = "wayland-screenshot.png";
142
143 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
144 if (data == NULL)
145 die("eglReadBuffer failed\n");
146 rows = malloc(ec->height * sizeof rows[0]);
147 if (rows == NULL)
148 die("malloc failed\n");
149
150 for (i = 0; i < ec->height; i++)
151 rows[i] = (png_byte *) data + i * stride;
152
153 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
154 png_simple_error_callback,
155 png_simple_warning_callback);
156 if (png == NULL)
157 die("png_create_write_struct failed\n");
158
159 info = png_create_info_struct(png);
160 if (info == NULL)
161 die("png_create_info_struct failed\n");
162
163 fp = fopen(filename, "w");
164 if (fp == NULL)
165 die("fopen failed: %m\n");
166
167 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
168
169 depth = 8;
170 png_set_IHDR(png, info,
171 ec->width,
172 ec->height, depth,
173 PNG_COLOR_TYPE_RGB,
174 PNG_INTERLACE_NONE,
175 PNG_COMPRESSION_TYPE_DEFAULT,
176 PNG_FILTER_TYPE_DEFAULT);
177
178 white.gray = (1 << depth) - 1;
179 white.red = white.blue = white.green = white.gray;
180 png_set_bKGD(png, info, &white);
181 png_write_info (png, info);
182 png_set_write_user_transform_fn(png, convert_pixels);
183
184 png_set_filler(png, 0, PNG_FILLER_AFTER);
185 png_write_image(png, rows);
186 png_write_end(png, info);
187
188 png_destroy_write_struct(&png, &info);
189 fclose(fp);
190 free(rows);
191 free(data);
192}
193
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500194static const struct wl_method screenshooter_methods[] = {
195 { "shoot", screenshooter_shoot, 0, NULL }
196};
197
198static const struct wl_interface screenshooter_interface = {
199 "screenshooter", 1,
200 ARRAY_LENGTH(screenshooter_methods),
201 screenshooter_methods,
202};
203
204static struct screenshooter *
205screenshooter_create(struct egl_compositor *ec)
206{
207 struct screenshooter *shooter;
208
209 shooter = malloc(sizeof *shooter);
210 if (shooter == NULL)
211 return NULL;
212
213 shooter->base.interface = &screenshooter_interface;
214 shooter->ec = ec;
215
216 return shooter;
217};
218
Kristian Høgsberg54879822008-11-23 17:07:32 -0500219static struct egl_surface *
220egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
221 int x, int y, int width, int height)
222{
223 struct egl_surface *es;
224 int stride;
225 void *data;
226
227 stride = cairo_image_surface_get_stride(surface);
228 data = cairo_image_surface_get_data(surface);
229
230 es = malloc(sizeof *es);
231 if (es == NULL)
232 return NULL;
233
234 glGenTextures(1, &es->texture);
235 glBindTexture(GL_TEXTURE_2D, es->texture);
236 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
237 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
238 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
240 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
241 GL_BGRA, GL_UNSIGNED_BYTE, data);
242
243 es->map.x = x;
244 es->map.y = y;
245 es->map.width = width;
246 es->map.height = height;
247 es->surface = EGL_NO_SURFACE;
248
249 return es;
250}
251
252static void
253egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
254{
255 glDeleteTextures(1, &es->texture);
256 if (es->surface != EGL_NO_SURFACE)
257 eglDestroySurface(ec->display, es->surface);
258 free(es);
259}
260
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400261static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500262pointer_path(cairo_t *cr, int x, int y)
263{
264 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
265 const int width = 16, height = 16;
266
267 cairo_move_to(cr, x, y);
268 cairo_line_to(cr, x + tx, y + ty);
269 cairo_line_to(cr, x + dx, y + dy);
270 cairo_line_to(cr, x + width - end, y + height);
271 cairo_line_to(cr, x + width, y + height - end);
272 cairo_line_to(cr, x + dy, y + dx);
273 cairo_line_to(cr, x + ty, y + tx);
274 cairo_close_path(cr);
275}
276
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500277static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500278pointer_create(int x, int y, int width, int height)
279{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500280 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500281 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500282 cairo_surface_t *surface;
283 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500284
285 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
286 width, height);
287
288 cr = cairo_create(surface);
289 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
290 cairo_set_line_width (cr, 2);
291 cairo_set_source_rgb(cr, 0, 0, 0);
292 cairo_stroke_preserve(cr);
293 cairo_fill(cr);
294 blur_surface(surface, width);
295
296 pointer_path(cr, hotspot_x, hotspot_y);
297 cairo_stroke_preserve(cr);
298 cairo_set_source_rgb(cr, 1, 1, 1);
299 cairo_fill(cr);
300 cairo_destroy(cr);
301
Kristian Høgsberg54879822008-11-23 17:07:32 -0500302 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
303
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500304 cairo_surface_destroy(surface);
305
Kristian Høgsberg54879822008-11-23 17:07:32 -0500306 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500307}
308
309static struct egl_surface *
310background_create(const char *filename, int width, int height)
311{
312 struct egl_surface *background;
313 GdkPixbuf *pixbuf;
314 GError *error = NULL;
315 int pixbuf_width, pixbuf_height;
316 void *data;
317
318 background = malloc(sizeof *background);
319 if (background == NULL)
320 return NULL;
321
322 g_type_init();
323
324 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
325 if (error != NULL) {
326 free(background);
327 return NULL;
328 }
329
330 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
331 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
332 data = gdk_pixbuf_get_pixels(pixbuf);
333
334 glGenTextures(1, &background->texture);
335 glBindTexture(GL_TEXTURE_2D, background->texture);
336 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
337 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
338 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
339 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
341 GL_BGR, GL_UNSIGNED_BYTE, data);
342
343 background->map.x = 0;
344 background->map.y = 0;
345 background->map.width = width;
346 background->map.height = height;
347 background->surface = EGL_NO_SURFACE;
348
349 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500350}
351
352static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500353rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
354{
355 cairo_move_to(cr, x0, y0 + radius);
356 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
357 cairo_line_to(cr, x1 - radius, y0);
358 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
359 cairo_line_to(cr, x1, y1 - radius);
360 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
361 cairo_line_to(cr, x0 + radius, y1);
362 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
363 cairo_close_path(cr);
364}
365
366static void
367draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
368{
369 cairo_pattern_t *gradient;
370 cairo_text_extents_t extents;
371 double bright = 0.15, dim = 0.02;
372 int radius = 10;
373
374 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
375 cairo_set_line_width (cr, 2);
376 rounded_rect(cr, x, y, x + width, y + height, radius);
377 cairo_set_source_rgb(cr, dim, dim, dim);
378 cairo_stroke(cr);
379 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
380 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
381 cairo_stroke(cr);
382
383 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
384 cairo_set_source_rgb(cr, bright, bright, bright);
385 cairo_stroke(cr);
386 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
387 cairo_set_source_rgb(cr, dim, dim, dim);
388 cairo_stroke(cr);
389
390 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
391 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
392 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
393 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
394 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
395 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
396 cairo_set_source(cr, gradient);
397 cairo_fill(cr);
398
399 cairo_set_font_size(cr, 16);
400 cairo_text_extents(cr, text, &extents);
401 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
402 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
403 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
404 cairo_set_line_width (cr, 4);
405 cairo_text_path(cr, text);
406 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
407 cairo_stroke_preserve(cr);
408 cairo_set_source_rgb(cr, 1, 1, 1);
409 cairo_fill(cr);
410}
411
412static struct egl_surface *
413overlay_create(int x, int y, int width, int height)
414{
415 struct egl_surface *es;
416 cairo_surface_t *surface;
417 cairo_t *cr;
418 int total_width, button_x, button_y;
419 const int button_width = 150;
420 const int button_height = 40;
421 const int spacing = 50;
422
423 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
424 width, height);
425
426 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500427 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500428 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
429 cairo_paint(cr);
430
431 total_width = button_width * 2 + spacing;
432 button_x = (width - total_width) / 2;
433 button_y = height - button_height - 20;
434 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
435 button_x += button_width + spacing;
436 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
437
438 cairo_destroy(cr);
439
440 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
441
442 cairo_surface_destroy(surface);
443
444 return es;
445}
446
447static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500448draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500449{
450 GLint vertices[12];
451 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
452 GLuint indices[4] = { 0, 1, 2, 3 };
453
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500454 vertices[0] = es->map.x;
455 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500456 vertices[2] = 0;
457
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458 vertices[3] = es->map.x;
459 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500460 vertices[5] = 0;
461
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500462 vertices[6] = es->map.x + es->map.width;
463 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500464 vertices[8] = 0;
465
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500466 vertices[9] = es->map.x + es->map.width;
467 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500468 vertices[11] = 0;
469
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500470 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500471 glEnable(GL_TEXTURE_2D);
472 glEnable(GL_BLEND);
473 /* Assume pre-multiplied alpha for now, this probably
474 * needs to be a wayland visual type of thing. */
475 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
476
477 glEnableClientState(GL_VERTEX_ARRAY);
478 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
479 glVertexPointer(3, GL_INT, 0, vertices);
480 glTexCoordPointer(2, GL_INT, 0, tex_coords);
481 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
482}
483
484static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500485schedule_repaint(struct egl_compositor *ec);
486
487static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500488animate_overlay(struct egl_compositor *ec)
489{
490 double force, y;
491 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500492#if 1
493 double bounce = 0.0;
494 double friction = 1.0;
495 double spring = 0.2;
496#else
497 double bounce = 0.2;
498 double friction = 0.04;
499 double spring = 0.09;
500#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500501
502 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500503 force = (ec->overlay_target - ec->overlay_y) * spring +
504 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500505
506 ec->overlay_y = y + (y - ec->overlay_previous) + force;
507 ec->overlay_previous = y;
508
509 top = ec->height - ec->overlay->map.height;
510 bottom = ec->height;
511 if (ec->overlay_y >= bottom) {
512 ec->overlay_y = bottom;
513 ec->overlay_previous = bottom;
514 }
515
516 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500517 ec->overlay_y = top + bounce * (top - ec->overlay_y);
518 ec->overlay_previous =
519 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500520 }
521
522 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500523
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500524 if (fabs(y - ec->overlay_target) > 0.2 ||
525 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500526 schedule_repaint(ec);
527}
528
529static void
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500530repaint(int fd, uint32_t mask, void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400531{
532 struct egl_compositor *ec = data;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500533 struct itimerspec its;
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400534 struct wl_surface_iterator *iterator;
535 struct wl_surface *surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500536 struct egl_surface *es;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500537 struct timespec ts;
538 uint64_t expires;
539 uint32_t msecs;
540
541 if (ec->repaint_on_timeout)
542 read(fd, &expires, sizeof expires);
543
544 if (!ec->repaint_needed) {
545 ec->repaint_on_timeout = 0;
546 return;
547 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500548
549 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400550
551 iterator = wl_surface_iterator_create(ec->wl_display, 0);
552 while (wl_surface_iterator_next(iterator, &surface)) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500553 es = wl_surface_get_data(surface);
554 if (es == NULL)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400555 continue;
556
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500557 draw_surface(es);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400558 }
559 wl_surface_iterator_destroy(iterator);
560
Kristian Høgsberg54879822008-11-23 17:07:32 -0500561 draw_surface(ec->overlay);
562
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500563 draw_surface(ec->pointer);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500564
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400565 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500566 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500567
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500568 clock_gettime(CLOCK_MONOTONIC, &ts);
569 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
570 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
571 ec->current_frame++;
572
573 its.it_interval.tv_sec = 0;
574 its.it_interval.tv_nsec = 0;
575 its.it_value.tv_sec = 0;
576 its.it_value.tv_nsec = 10 * 1000 * 1000;
577 if (timerfd_settime(ec->timer_fd, 0, &its, NULL) < 0) {
578 fprintf(stderr, "could not set timerfd\n: %m");
579 return;
580 }
581 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500582
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500583 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400584}
585
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500586static void
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500587idle_repaint(void *data)
588{
589 repaint(0, 0, data);
590}
591
592static void
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500593schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400594{
595 struct wl_event_loop *loop;
596
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500597 ec->repaint_needed = 1;
598 if (!ec->repaint_on_timeout) {
599 loop = wl_display_get_event_loop(ec->wl_display);
600 wl_event_loop_add_idle(loop, idle_repaint, ec);
601 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400602}
603
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500604static void
605notify_surface_create(struct wl_compositor *compositor,
606 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400607{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500608 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400609
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500610 es = malloc(sizeof *es);
611 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400612 return;
613
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500614 es->surface = EGL_NO_SURFACE;
615 wl_surface_set_data(surface, es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400616
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500617 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400618}
619
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500620static void
621notify_surface_destroy(struct wl_compositor *compositor,
622 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400623{
624 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 = wl_surface_get_data(surface);
628 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400629 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500630
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500631 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400632
633 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400634}
635
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500636static void
637notify_surface_attach(struct wl_compositor *compositor,
638 struct wl_surface *surface, uint32_t name,
639 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400640{
641 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500642 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400643
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500644 es = wl_surface_get_data(surface);
645 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400646 return;
647
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500648 if (es->surface != EGL_NO_SURFACE)
649 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400650
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500651 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500652 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400653
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500654 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400655 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
656 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
657 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
658 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500659 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400660}
661
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500662static void
663notify_surface_map(struct wl_compositor *compositor,
664 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400665{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500666 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400667
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500668 es = wl_surface_get_data(surface);
669 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400670 return;
671
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500672 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400673}
674
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500675static void
676notify_surface_copy(struct wl_compositor *compositor,
677 struct wl_surface *surface,
678 int32_t dst_x, int32_t dst_y,
679 uint32_t name, uint32_t stride,
680 int32_t x, int32_t y, int32_t width, int32_t height)
681{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500682 struct egl_compositor *ec = (struct egl_compositor *) compositor;
683 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500684 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500685
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500686 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500687
688 /* FIXME: glCopyPixels should work, but then we'll have to
689 * call eglMakeCurrent to set up the src and dest surfaces
690 * first. This seems cheaper, but maybe there's a better way
691 * to accomplish this. */
692
693 src = eglCreateSurfaceForName(ec->display, ec->config,
694 name, x + width, y + height, stride, NULL);
695
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500696 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500697 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500698 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500699}
700
701static void
702notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500703 struct wl_surface *surface,
704 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500705{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500706 /* FIXME: This need to take a damage region, of course. */
707}
708
709static uint32_t
710notify_commit(struct wl_compositor *compositor)
711{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500712 struct egl_compositor *ec = (struct egl_compositor *) compositor;
713
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500714 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500715
716 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500717}
718
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500719static void
720notify_pointer_motion(struct wl_compositor *compositor,
721 struct wl_object *source, int x, int y)
722{
723 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsberg961a04c2008-11-25 22:38:56 -0500724 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500725
Kristian Høgsberg961a04c2008-11-25 22:38:56 -0500726 ec->pointer->map.x = x - hotspot_x;
727 ec->pointer->map.y = y - hotspot_y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500728 schedule_repaint(ec);
729}
730
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500731static void
732notify_key(struct wl_compositor *compositor,
733 struct wl_object *source, uint32_t key, uint32_t state)
734{
735 struct egl_compositor *ec = (struct egl_compositor *) compositor;
736
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500737 if (key == KEY_ESC && state == 1) {
738 if (ec->overlay_target == ec->height)
739 ec->overlay_target -= 200;
740 else
741 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500742 schedule_repaint(ec);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500743 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500744}
745
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500746static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400747 notify_surface_create,
748 notify_surface_destroy,
749 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500750 notify_surface_map,
751 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500752 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500753 notify_commit,
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500754 notify_pointer_motion,
755 notify_key
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400756};
757
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500758static const char pointer_device_file[] =
759 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
760static const char keyboard_device_file[] =
761 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-kbd";
762
763static void
764create_input_devices(struct wl_display *display)
765{
766 struct wl_object *obj;
767 const char *path;
768
769 path = getenv("WAYLAND_POINTER");
770 if (path == NULL)
771 path = pointer_device_file;
772
773 obj = wl_input_device_create(display, path);
774 if (obj != NULL)
775 wl_display_add_object(display, obj);
776
777 path = getenv("WAYLAND_KEYBOARD");
778 if (path == NULL)
779 path = keyboard_device_file;
780
781 obj = wl_input_device_create(display, path);
782 if (obj != NULL)
783 wl_display_add_object(display, obj);
784}
785
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500786static uint32_t
787create_frontbuffer(int fd, int *width, int *height, int *stride)
788{
789 drmModeConnector *connector;
790 drmModeRes *resources;
791 drmModeEncoder *encoder;
792 struct drm_mode_modeinfo *mode;
793 struct drm_i915_gem_create create;
794 struct drm_i915_gem_pin pin;
795 struct drm_gem_flink flink;
796 unsigned int fb_id;
797 int i, ret;
798
799 resources = drmModeGetResources(fd);
800 if (!resources) {
801 fprintf(stderr, "drmModeGetResources failed\n");
802 return 0;
803 }
804
805 for (i = 0; i < resources->count_connectors; i++) {
806 connector = drmModeGetConnector(fd, resources->connectors[i]);
807 if (connector == NULL)
808 continue;
809
810 if (connector->connection == DRM_MODE_CONNECTED &&
811 connector->count_modes > 0)
812 break;
813
814 drmModeFreeConnector(connector);
815 }
816
817 if (i == resources->count_connectors) {
818 fprintf(stderr, "No currently active connector found.\n");
819 return -1;
820 }
821
822 mode = &connector->modes[0];
823
824 for (i = 0; i < resources->count_encoders; i++) {
825 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
826
827 if (encoder == NULL)
828 continue;
829
830 if (encoder->encoder_id == connector->encoder_id)
831 break;
832
833 drmModeFreeEncoder(encoder);
834 }
835
836 /* Mode size at 32 bpp */
837 create.size = mode->hdisplay * mode->vdisplay * 4;
838 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
839 fprintf(stderr, "gem create failed: %m\n");
840 return 0;
841 }
842
843 pin.handle = create.handle;
844 pin.alignment = 4096;
845 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
846 fprintf(stderr, "failed to pin buffer: %m\n");
847 return 0;
848 }
849
850 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
851 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
852 if (ret) {
853 fprintf(stderr, "failed to add fb: %m\n");
854 return 0;
855 }
856
857 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
858 &connector->connector_id, 1, mode);
859 if (ret) {
860 fprintf(stderr, "failed to set mode: %m\n");
861 return 0;
862 }
863
864 flink.handle = create.handle;
865 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
866 fprintf(stderr, "gem flink failed: %m\n");
867 return 0;
868 }
869
870 *width = mode->hdisplay;
871 *height = mode->vdisplay;
872 *stride = mode->hdisplay * 4;
873
874 return flink.name;
875}
876
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500877static int
878pick_config(struct egl_compositor *ec)
879{
880 EGLConfig configs[100];
881 EGLint value, count;
882 int i;
883
884 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
885 fprintf(stderr, "failed to get configs\n");
886 return -1;
887 }
888
889 ec->config = EGL_NO_CONFIG;
890 for (i = 0; i < count; i++) {
891 eglGetConfigAttrib(ec->display,
892 configs[i],
893 EGL_DEPTH_SIZE,
894 &value);
895 if (value > 0) {
896 fprintf(stderr, "config %d has depth size %d\n", i, value);
897 continue;
898 }
899
900 eglGetConfigAttrib(ec->display,
901 configs[i],
902 EGL_STENCIL_SIZE,
903 &value);
904 if (value > 0) {
905 fprintf(stderr, "config %d has stencil size %d\n", i, value);
906 continue;
907 }
908
909 eglGetConfigAttrib(ec->display,
910 configs[i],
911 EGL_CONFIG_CAVEAT,
912 &value);
913 if (value != EGL_NONE) {
914 fprintf(stderr, "config %d has caveat %d\n", i, value);
915 continue;
916 }
917
918 ec->config = configs[i];
919 break;
920 }
921
922 if (ec->config == EGL_NO_CONFIG) {
923 fprintf(stderr, "found no config without depth or stencil buffers\n");
924 return -1;
925 }
926
927 return 0;
928}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500929
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400930static const char gem_device[] = "/dev/dri/card0";
931
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500932WL_EXPORT struct wl_compositor *
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400933wl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400934{
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500935 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400936 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500937 const char *filename;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500938 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500939 uint32_t fb_name;
940 int stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500941 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500942 const static EGLint attribs[] =
943 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400944
945 ec = malloc(sizeof *ec);
946 if (ec == NULL)
947 return NULL;
948
949 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400950 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400951
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400952 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400953 if (ec->display == NULL) {
954 fprintf(stderr, "failed to create display\n");
955 return NULL;
956 }
957
958 if (!eglInitialize(ec->display, &major, &minor)) {
959 fprintf(stderr, "failed to initialize display\n");
960 return NULL;
961 }
962
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500963 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400964 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500965
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500966 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
967 &ec->width, &ec->height, &stride);
968 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
969 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400970 if (ec->surface == NULL) {
971 fprintf(stderr, "failed to create surface\n");
972 return NULL;
973 }
974
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500975 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400976 if (ec->context == NULL) {
977 fprintf(stderr, "failed to create context\n");
978 return NULL;
979 }
980
981 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
982 fprintf(stderr, "failed to make context current\n");
983 return NULL;
984 }
985
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500986 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400987 glMatrixMode(GL_PROJECTION);
988 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500989 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400990 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500991
992 create_input_devices(display);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400993
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500994 filename = getenv("WAYLAND_BACKGROUND");
995 if (filename == NULL)
996 filename = "background.jpg";
997 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500998 ec->pointer = pointer_create(100, 100, 64, 64);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500999 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
1000 ec->overlay_target = ec->height;
1001 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001002
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001003 ec->gem_fd = open(gem_device, O_RDWR);
1004 if (ec->gem_fd < 0) {
1005 fprintf(stderr, "failed to open drm device\n");
1006 return NULL;
1007 }
1008
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001009 shooter = screenshooter_create(ec);
1010 wl_display_add_object(display, &shooter->base);
1011 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001012
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001013 ec->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
1014 if (ec->timer_fd < 0) {
1015 fprintf(stderr, "could not create timerfd\n: %m");
1016 return NULL;
1017 }
1018
1019 loop = wl_display_get_event_loop(ec->wl_display);
1020 ec->timer_source = wl_event_loop_add_fd(loop, ec->timer_fd,
1021 WL_EVENT_READABLE,
1022 repaint, ec);
1023 ec->repaint_needed = 0;
1024 ec->repaint_on_timeout = 0;
1025
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001026 schedule_repaint(ec);
1027
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001028 return &ec->base;
1029}