blob: 917364fa9664e2395c394ea5553895ad01d6a8ed [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
44#include "wayland.h"
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050045#include "cairo-util.h"
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040046
47#include <GL/gl.h>
48#include <eagle.h>
49
50#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
51
52struct egl_compositor {
53 struct wl_compositor base;
54 EGLDisplay display;
55 EGLSurface surface;
56 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050057 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040058 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040059 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050060 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050061 struct egl_surface *pointer;
62 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050063 struct egl_surface *overlay;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -050064 double overlay_y, overlay_target, overlay_previous;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050065
Kristian Høgsberg201a9042008-12-10 00:40:50 -050066 struct wl_list surface_list;
67
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050068 /* Repaint state. */
69 struct wl_event_source *timer_source;
70 int repaint_needed;
71 int repaint_on_timeout;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -050072 struct timespec previous_swap;
73 uint32_t current_frame;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040074};
75
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050076struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040077 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040078 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050079 EGLSurface surface;
Kristian Høgsberg715a0812008-12-10 10:42:04 -050080 struct wl_surface *wl_surface;
81 int width, height;
Kristian Høgsberg201a9042008-12-10 00:40:50 -050082 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040083};
84
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050085static void
86die(const char *msg, ...)
87{
88 va_list ap;
89
90 va_start (ap, msg);
91 vfprintf(stderr, msg, ap);
92 va_end (ap);
93
94 exit(EXIT_FAILURE);
95}
96
97static void
98stdio_write_func (png_structp png, png_bytep data, png_size_t size)
99{
100 FILE *fp;
101 size_t ret;
102
103 fp = png_get_io_ptr (png);
104 while (size) {
105 ret = fwrite (data, 1, size, fp);
106 size -= ret;
107 data += ret;
108 if (size && ferror (fp))
109 die("write: %m\n");
110 }
111}
112
113static void
114png_simple_output_flush_fn (png_structp png_ptr)
115{
116}
117
118static void
119png_simple_error_callback (png_structp png,
120 png_const_charp error_msg)
121{
122 die("png error: %s\n", error_msg);
123}
124
125static void
126png_simple_warning_callback (png_structp png,
127 png_const_charp error_msg)
128{
129 fprintf(stderr, "png warning: %s\n", error_msg);
130}
131
132static void
133convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
134{
135 unsigned int i;
136
137 for (i = 0; i < row_info->rowbytes; i += 4) {
138 uint8_t *b = &data[i];
139 uint32_t pixel;
140
141 memcpy (&pixel, b, sizeof (uint32_t));
142 b[0] = (pixel & 0xff0000) >> 16;
143 b[1] = (pixel & 0x00ff00) >> 8;
144 b[2] = (pixel & 0x0000ff) >> 0;
145 b[3] = 0;
146 }
147}
148
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500149struct screenshooter {
150 struct wl_object base;
151 struct egl_compositor *ec;
152};
153
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500154static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500155screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500156{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500157 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500158 png_struct *png;
159 png_info *info;
160 png_byte **volatile rows = NULL;
161 png_color_16 white;
162 int depth, i;
163 FILE *fp;
164 uint8_t *data;
165 GLuint stride;
166 static const char filename[] = "wayland-screenshot.png";
167
168 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
169 if (data == NULL)
170 die("eglReadBuffer failed\n");
171 rows = malloc(ec->height * sizeof rows[0]);
172 if (rows == NULL)
173 die("malloc failed\n");
174
175 for (i = 0; i < ec->height; i++)
176 rows[i] = (png_byte *) data + i * stride;
177
178 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
179 png_simple_error_callback,
180 png_simple_warning_callback);
181 if (png == NULL)
182 die("png_create_write_struct failed\n");
183
184 info = png_create_info_struct(png);
185 if (info == NULL)
186 die("png_create_info_struct failed\n");
187
188 fp = fopen(filename, "w");
189 if (fp == NULL)
190 die("fopen failed: %m\n");
191
192 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
193
194 depth = 8;
195 png_set_IHDR(png, info,
196 ec->width,
197 ec->height, depth,
198 PNG_COLOR_TYPE_RGB,
199 PNG_INTERLACE_NONE,
200 PNG_COMPRESSION_TYPE_DEFAULT,
201 PNG_FILTER_TYPE_DEFAULT);
202
203 white.gray = (1 << depth) - 1;
204 white.red = white.blue = white.green = white.gray;
205 png_set_bKGD(png, info, &white);
206 png_write_info (png, info);
207 png_set_write_user_transform_fn(png, convert_pixels);
208
209 png_set_filler(png, 0, PNG_FILLER_AFTER);
210 png_write_image(png, rows);
211 png_write_end(png, info);
212
213 png_destroy_write_struct(&png, &info);
214 fclose(fp);
215 free(rows);
216 free(data);
217}
218
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500219static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500220 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500221};
222
223static const struct wl_interface screenshooter_interface = {
224 "screenshooter", 1,
225 ARRAY_LENGTH(screenshooter_methods),
226 screenshooter_methods,
227};
228
229static struct screenshooter *
230screenshooter_create(struct egl_compositor *ec)
231{
232 struct screenshooter *shooter;
233
234 shooter = malloc(sizeof *shooter);
235 if (shooter == NULL)
236 return NULL;
237
238 shooter->base.interface = &screenshooter_interface;
239 shooter->ec = ec;
240
241 return shooter;
242};
243
Kristian Høgsberg54879822008-11-23 17:07:32 -0500244static struct egl_surface *
245egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
246 int x, int y, int width, int height)
247{
248 struct egl_surface *es;
249 int stride;
250 void *data;
251
252 stride = cairo_image_surface_get_stride(surface);
253 data = cairo_image_surface_get_data(surface);
254
255 es = malloc(sizeof *es);
256 if (es == NULL)
257 return NULL;
258
259 glGenTextures(1, &es->texture);
260 glBindTexture(GL_TEXTURE_2D, es->texture);
261 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500265 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
266 GL_BGRA, GL_UNSIGNED_BYTE, data);
267
268 es->map.x = x;
269 es->map.y = y;
270 es->map.width = width;
271 es->map.height = height;
272 es->surface = EGL_NO_SURFACE;
273
274 return es;
275}
276
277static void
278egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
279{
280 glDeleteTextures(1, &es->texture);
281 if (es->surface != EGL_NO_SURFACE)
282 eglDestroySurface(ec->display, es->surface);
283 free(es);
284}
285
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400286static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500287pointer_path(cairo_t *cr, int x, int y)
288{
289 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
290 const int width = 16, height = 16;
291
292 cairo_move_to(cr, x, y);
293 cairo_line_to(cr, x + tx, y + ty);
294 cairo_line_to(cr, x + dx, y + dy);
295 cairo_line_to(cr, x + width - end, y + height);
296 cairo_line_to(cr, x + width, y + height - end);
297 cairo_line_to(cr, x + dy, y + dx);
298 cairo_line_to(cr, x + ty, y + tx);
299 cairo_close_path(cr);
300}
301
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500302static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500303pointer_create(int x, int y, int width, int height)
304{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500305 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500306 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500307 cairo_surface_t *surface;
308 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500309
310 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
311 width, height);
312
313 cr = cairo_create(surface);
314 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
315 cairo_set_line_width (cr, 2);
316 cairo_set_source_rgb(cr, 0, 0, 0);
317 cairo_stroke_preserve(cr);
318 cairo_fill(cr);
319 blur_surface(surface, width);
320
321 pointer_path(cr, hotspot_x, hotspot_y);
322 cairo_stroke_preserve(cr);
323 cairo_set_source_rgb(cr, 1, 1, 1);
324 cairo_fill(cr);
325 cairo_destroy(cr);
326
Kristian Høgsberg54879822008-11-23 17:07:32 -0500327 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
328
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500329 cairo_surface_destroy(surface);
330
Kristian Høgsberg54879822008-11-23 17:07:32 -0500331 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500332}
333
334static struct egl_surface *
335background_create(const char *filename, int width, int height)
336{
337 struct egl_surface *background;
338 GdkPixbuf *pixbuf;
339 GError *error = NULL;
340 int pixbuf_width, pixbuf_height;
341 void *data;
342
343 background = malloc(sizeof *background);
344 if (background == NULL)
345 return NULL;
346
347 g_type_init();
348
349 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
350 if (error != NULL) {
351 free(background);
352 return NULL;
353 }
354
355 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
356 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
357 data = gdk_pixbuf_get_pixels(pixbuf);
358
359 glGenTextures(1, &background->texture);
360 glBindTexture(GL_TEXTURE_2D, background->texture);
361 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500365 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
366 GL_BGR, GL_UNSIGNED_BYTE, data);
367
368 background->map.x = 0;
369 background->map.y = 0;
370 background->map.width = width;
371 background->map.height = height;
372 background->surface = EGL_NO_SURFACE;
373
374 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500375}
376
377static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500378rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
379{
380 cairo_move_to(cr, x0, y0 + radius);
381 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
382 cairo_line_to(cr, x1 - radius, y0);
383 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
384 cairo_line_to(cr, x1, y1 - radius);
385 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
386 cairo_line_to(cr, x0 + radius, y1);
387 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
388 cairo_close_path(cr);
389}
390
391static void
392draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
393{
394 cairo_pattern_t *gradient;
395 cairo_text_extents_t extents;
396 double bright = 0.15, dim = 0.02;
397 int radius = 10;
398
399 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
400 cairo_set_line_width (cr, 2);
401 rounded_rect(cr, x, y, x + width, y + height, radius);
402 cairo_set_source_rgb(cr, dim, dim, dim);
403 cairo_stroke(cr);
404 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
405 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
406 cairo_stroke(cr);
407
408 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
409 cairo_set_source_rgb(cr, bright, bright, bright);
410 cairo_stroke(cr);
411 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
412 cairo_set_source_rgb(cr, dim, dim, dim);
413 cairo_stroke(cr);
414
415 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
416 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
417 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
418 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
419 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
420 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
421 cairo_set_source(cr, gradient);
422 cairo_fill(cr);
423
424 cairo_set_font_size(cr, 16);
425 cairo_text_extents(cr, text, &extents);
426 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
427 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
428 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
429 cairo_set_line_width (cr, 4);
430 cairo_text_path(cr, text);
431 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
432 cairo_stroke_preserve(cr);
433 cairo_set_source_rgb(cr, 1, 1, 1);
434 cairo_fill(cr);
435}
436
437static struct egl_surface *
438overlay_create(int x, int y, int width, int height)
439{
440 struct egl_surface *es;
441 cairo_surface_t *surface;
442 cairo_t *cr;
443 int total_width, button_x, button_y;
444 const int button_width = 150;
445 const int button_height = 40;
446 const int spacing = 50;
447
448 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
449 width, height);
450
451 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500452 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500453 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
454 cairo_paint(cr);
455
456 total_width = button_width * 2 + spacing;
457 button_x = (width - total_width) / 2;
458 button_y = height - button_height - 20;
459 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
460 button_x += button_width + spacing;
461 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
462
463 cairo_destroy(cr);
464
465 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
466
467 cairo_surface_destroy(surface);
468
469 return es;
470}
471
472static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500473draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500474{
475 GLint vertices[12];
476 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
477 GLuint indices[4] = { 0, 1, 2, 3 };
478
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500479 vertices[0] = es->map.x;
480 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500481 vertices[2] = 0;
482
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500483 vertices[3] = es->map.x;
484 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500485 vertices[5] = 0;
486
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500487 vertices[6] = es->map.x + es->map.width;
488 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500489 vertices[8] = 0;
490
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500491 vertices[9] = es->map.x + es->map.width;
492 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500493 vertices[11] = 0;
494
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500495 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500496 glEnable(GL_TEXTURE_2D);
497 glEnable(GL_BLEND);
498 /* Assume pre-multiplied alpha for now, this probably
499 * needs to be a wayland visual type of thing. */
500 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
501
502 glEnableClientState(GL_VERTEX_ARRAY);
503 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
504 glVertexPointer(3, GL_INT, 0, vertices);
505 glTexCoordPointer(2, GL_INT, 0, tex_coords);
506 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
507}
508
509static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500510schedule_repaint(struct egl_compositor *ec);
511
512static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500513animate_overlay(struct egl_compositor *ec)
514{
515 double force, y;
516 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500517#if 1
518 double bounce = 0.0;
519 double friction = 1.0;
520 double spring = 0.2;
521#else
522 double bounce = 0.2;
523 double friction = 0.04;
524 double spring = 0.09;
525#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500526
527 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500528 force = (ec->overlay_target - ec->overlay_y) * spring +
529 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500530
531 ec->overlay_y = y + (y - ec->overlay_previous) + force;
532 ec->overlay_previous = y;
533
534 top = ec->height - ec->overlay->map.height;
535 bottom = ec->height;
536 if (ec->overlay_y >= bottom) {
537 ec->overlay_y = bottom;
538 ec->overlay_previous = bottom;
539 }
540
541 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500542 ec->overlay_y = top + bounce * (top - ec->overlay_y);
543 ec->overlay_previous =
544 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500545 }
546
547 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500548
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500549 if (fabs(y - ec->overlay_target) > 0.2 ||
550 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500551 schedule_repaint(ec);
552}
553
554static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500555repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400556{
557 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500558 struct egl_surface *es;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500559 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500560 uint32_t msecs;
561
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500562 if (!ec->repaint_needed) {
563 ec->repaint_on_timeout = 0;
564 return;
565 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500566
567 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400568
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500569 es = container_of(ec->surface_list.next,
570 struct egl_surface, link);
571 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500572 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500573
574 es = container_of(es->link.next,
575 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400576 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400577
Kristian Høgsberg54879822008-11-23 17:07:32 -0500578 draw_surface(ec->overlay);
579
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500580 draw_surface(ec->pointer);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500581
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400582 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500583 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500584
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500585 clock_gettime(CLOCK_MONOTONIC, &ts);
586 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
587 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
588 ec->current_frame++;
589
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500590 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500591 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500592
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500593 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400594}
595
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500596static void
597schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400598{
599 struct wl_event_loop *loop;
600
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500601 ec->repaint_needed = 1;
602 if (!ec->repaint_on_timeout) {
603 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500604 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500605 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400606}
607
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500608static void
609notify_surface_create(struct wl_compositor *compositor,
610 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400611{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500612 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500613 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400614
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500615 es = malloc(sizeof *es);
616 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400617 return;
618
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500619 es->surface = EGL_NO_SURFACE;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500620 es->wl_surface = surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500621 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500622 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400623
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500624 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400625}
626
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500627static void
628notify_surface_destroy(struct wl_compositor *compositor,
629 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400630{
631 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500632 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400633
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500634 es = wl_surface_get_data(surface);
635 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400636 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500637
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500638 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500639 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400640
641 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400642}
643
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500644static void
645notify_surface_attach(struct wl_compositor *compositor,
646 struct wl_surface *surface, uint32_t name,
647 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400648{
649 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500650 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400651
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500652 es = wl_surface_get_data(surface);
653 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400654 return;
655
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500656 if (es->surface != EGL_NO_SURFACE)
657 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400658
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500659 es->width = width;
660 es->height = height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500661 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500662 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400663
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500664 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400665 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500666 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
667 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
668 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500669 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400670}
671
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500672static void
673notify_surface_map(struct wl_compositor *compositor,
674 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400675{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500676 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400677
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500678 es = wl_surface_get_data(surface);
679 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400680 return;
681
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500682 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400683}
684
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500685static void
686notify_surface_copy(struct wl_compositor *compositor,
687 struct wl_surface *surface,
688 int32_t dst_x, int32_t dst_y,
689 uint32_t name, uint32_t stride,
690 int32_t x, int32_t y, int32_t width, int32_t height)
691{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500692 struct egl_compositor *ec = (struct egl_compositor *) compositor;
693 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500694 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500695
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500696 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500697
698 /* FIXME: glCopyPixels should work, but then we'll have to
699 * call eglMakeCurrent to set up the src and dest surfaces
700 * first. This seems cheaper, but maybe there's a better way
701 * to accomplish this. */
702
703 src = eglCreateSurfaceForName(ec->display, ec->config,
704 name, x + width, y + height, stride, NULL);
705
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500706 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500707 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500708 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500709}
710
711static void
712notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500713 struct wl_surface *surface,
714 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500715{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500716 /* FIXME: This need to take a damage region, of course. */
717}
718
719static uint32_t
720notify_commit(struct wl_compositor *compositor)
721{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500722 struct egl_compositor *ec = (struct egl_compositor *) compositor;
723
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500724 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500725
726 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500727}
728
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500729static struct egl_surface *
730pick_surface(struct egl_compositor *ec, int32_t x, int32_t y)
731{
732 struct egl_surface *es;
733
734 es = container_of(ec->surface_list.prev,
735 struct egl_surface, link);
736 while (&es->link != &ec->surface_list) {
737 if (es->map.x <= x && x < es->map.x + es->map.width &&
738 es->map.y <= y && y < es->map.y + es->map.height)
739 return es;
740
741 es = container_of(es->link.prev,
742 struct egl_surface, link);
743 }
744
745 return NULL;
746}
747
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500748static void
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500749notify_pointer_motion(struct wl_compositor *compositor,
750 struct wl_object *source, int x, int y)
751{
752 struct egl_compositor *ec = (struct egl_compositor *) compositor;
753 struct egl_surface *es;
754 const int hotspot_x = 16, hotspot_y = 16;
755 int32_t sx, sy;
756
757 es = pick_surface(ec, x, y);
758 if (es) {
759 sx = (x - es->map.x) * es->width / es->map.width;
760 sy = (y - es->map.y) * es->height / es->map.height;
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500761 wl_surface_post_event(es->wl_surface, source,
762 WL_INPUT_MOTION, x, y, sx, sy);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500763 }
764
765 ec->pointer->map.x = x - hotspot_x;
766 ec->pointer->map.y = y - hotspot_y;
767 schedule_repaint(ec);
768}
769
770static void
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500771notify_pointer_button(struct wl_compositor *compositor,
772 struct wl_object *source,
773 int32_t button, int32_t state)
774{
775 struct egl_compositor *ec = (struct egl_compositor *) compositor;
776 struct egl_surface *es;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500777 const int hotspot_x = 16, hotspot_y = 16;
778 int x, y;
779
780 x = ec->pointer->map.x + hotspot_x;
781 y = ec->pointer->map.y + hotspot_y;
782
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500783 es = pick_surface(ec, x, y);
784 if (es) {
785 wl_list_remove(&es->link);
786 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500787
788 /* FIXME: Swallow click on raise? */
789 wl_surface_post_event(es->wl_surface, source,
790 WL_INPUT_BUTTON, button, state);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500791 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500792
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500793 schedule_repaint(ec);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500794}
795
796static void
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500797notify_key(struct wl_compositor *compositor,
798 struct wl_object *source, uint32_t key, uint32_t state)
799{
800 struct egl_compositor *ec = (struct egl_compositor *) compositor;
801
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500802 if (key == KEY_ESC && state == 1) {
803 if (ec->overlay_target == ec->height)
804 ec->overlay_target -= 200;
805 else
806 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500807 schedule_repaint(ec);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500808 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500809}
810
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500811static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400812 notify_surface_create,
813 notify_surface_destroy,
814 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500815 notify_surface_map,
816 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500817 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500818 notify_commit,
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500819 notify_pointer_motion,
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500820 notify_pointer_button,
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500821 notify_key
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400822};
823
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500824static const char pointer_device_file[] =
825 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
826static const char keyboard_device_file[] =
827 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-kbd";
828
829static void
830create_input_devices(struct wl_display *display)
831{
832 struct wl_object *obj;
833 const char *path;
834
835 path = getenv("WAYLAND_POINTER");
836 if (path == NULL)
837 path = pointer_device_file;
838
839 obj = wl_input_device_create(display, path);
840 if (obj != NULL)
841 wl_display_add_object(display, obj);
842
843 path = getenv("WAYLAND_KEYBOARD");
844 if (path == NULL)
845 path = keyboard_device_file;
846
847 obj = wl_input_device_create(display, path);
848 if (obj != NULL)
849 wl_display_add_object(display, obj);
850}
851
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500852static uint32_t
853create_frontbuffer(int fd, int *width, int *height, int *stride)
854{
855 drmModeConnector *connector;
856 drmModeRes *resources;
857 drmModeEncoder *encoder;
858 struct drm_mode_modeinfo *mode;
859 struct drm_i915_gem_create create;
860 struct drm_i915_gem_pin pin;
861 struct drm_gem_flink flink;
862 unsigned int fb_id;
863 int i, ret;
864
865 resources = drmModeGetResources(fd);
866 if (!resources) {
867 fprintf(stderr, "drmModeGetResources failed\n");
868 return 0;
869 }
870
871 for (i = 0; i < resources->count_connectors; i++) {
872 connector = drmModeGetConnector(fd, resources->connectors[i]);
873 if (connector == NULL)
874 continue;
875
876 if (connector->connection == DRM_MODE_CONNECTED &&
877 connector->count_modes > 0)
878 break;
879
880 drmModeFreeConnector(connector);
881 }
882
883 if (i == resources->count_connectors) {
884 fprintf(stderr, "No currently active connector found.\n");
885 return -1;
886 }
887
888 mode = &connector->modes[0];
889
890 for (i = 0; i < resources->count_encoders; i++) {
891 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
892
893 if (encoder == NULL)
894 continue;
895
896 if (encoder->encoder_id == connector->encoder_id)
897 break;
898
899 drmModeFreeEncoder(encoder);
900 }
901
902 /* Mode size at 32 bpp */
903 create.size = mode->hdisplay * mode->vdisplay * 4;
904 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
905 fprintf(stderr, "gem create failed: %m\n");
906 return 0;
907 }
908
909 pin.handle = create.handle;
910 pin.alignment = 4096;
911 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
912 fprintf(stderr, "failed to pin buffer: %m\n");
913 return 0;
914 }
915
916 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
917 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
918 if (ret) {
919 fprintf(stderr, "failed to add fb: %m\n");
920 return 0;
921 }
922
923 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
924 &connector->connector_id, 1, mode);
925 if (ret) {
926 fprintf(stderr, "failed to set mode: %m\n");
927 return 0;
928 }
929
930 flink.handle = create.handle;
931 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
932 fprintf(stderr, "gem flink failed: %m\n");
933 return 0;
934 }
935
936 *width = mode->hdisplay;
937 *height = mode->vdisplay;
938 *stride = mode->hdisplay * 4;
939
940 return flink.name;
941}
942
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500943static int
944pick_config(struct egl_compositor *ec)
945{
946 EGLConfig configs[100];
947 EGLint value, count;
948 int i;
949
950 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
951 fprintf(stderr, "failed to get configs\n");
952 return -1;
953 }
954
955 ec->config = EGL_NO_CONFIG;
956 for (i = 0; i < count; i++) {
957 eglGetConfigAttrib(ec->display,
958 configs[i],
959 EGL_DEPTH_SIZE,
960 &value);
961 if (value > 0) {
962 fprintf(stderr, "config %d has depth size %d\n", i, value);
963 continue;
964 }
965
966 eglGetConfigAttrib(ec->display,
967 configs[i],
968 EGL_STENCIL_SIZE,
969 &value);
970 if (value > 0) {
971 fprintf(stderr, "config %d has stencil size %d\n", i, value);
972 continue;
973 }
974
975 eglGetConfigAttrib(ec->display,
976 configs[i],
977 EGL_CONFIG_CAVEAT,
978 &value);
979 if (value != EGL_NONE) {
980 fprintf(stderr, "config %d has caveat %d\n", i, value);
981 continue;
982 }
983
984 ec->config = configs[i];
985 break;
986 }
987
988 if (ec->config == EGL_NO_CONFIG) {
989 fprintf(stderr, "found no config without depth or stencil buffers\n");
990 return -1;
991 }
992
993 return 0;
994}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500995
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400996static const char gem_device[] = "/dev/dri/card0";
997
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500998static struct egl_compositor *
999egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001000{
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001001 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001002 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001003 const char *filename;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001004 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001005 uint32_t fb_name;
1006 int stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001007 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001008 const static EGLint attribs[] =
1009 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001010
1011 ec = malloc(sizeof *ec);
1012 if (ec == NULL)
1013 return NULL;
1014
1015 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001016 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001017
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001018 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001019 if (ec->display == NULL) {
1020 fprintf(stderr, "failed to create display\n");
1021 return NULL;
1022 }
1023
1024 if (!eglInitialize(ec->display, &major, &minor)) {
1025 fprintf(stderr, "failed to initialize display\n");
1026 return NULL;
1027 }
1028
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001029 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001030 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001031
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001032 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1033 &ec->width, &ec->height, &stride);
1034 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1035 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001036 if (ec->surface == NULL) {
1037 fprintf(stderr, "failed to create surface\n");
1038 return NULL;
1039 }
1040
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001041 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001042 if (ec->context == NULL) {
1043 fprintf(stderr, "failed to create context\n");
1044 return NULL;
1045 }
1046
1047 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1048 fprintf(stderr, "failed to make context current\n");
1049 return NULL;
1050 }
1051
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001052 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001053 glMatrixMode(GL_PROJECTION);
1054 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001055 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001056 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001057
1058 create_input_devices(display);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001059
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001060 wl_list_init(&ec->surface_list);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001061 filename = getenv("WAYLAND_BACKGROUND");
1062 if (filename == NULL)
1063 filename = "background.jpg";
1064 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001065 ec->pointer = pointer_create(100, 100, 64, 64);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001066 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001067 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001068 ec->overlay_target = ec->height;
1069 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001070
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001071 ec->gem_fd = open(gem_device, O_RDWR);
1072 if (ec->gem_fd < 0) {
1073 fprintf(stderr, "failed to open drm device\n");
1074 return NULL;
1075 }
1076
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001077 shooter = screenshooter_create(ec);
1078 wl_display_add_object(display, &shooter->base);
1079 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001080
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001081 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001082 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001083 ec->repaint_needed = 0;
1084 ec->repaint_on_timeout = 0;
1085
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001086 schedule_repaint(ec);
1087
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001088 return ec;
1089}
1090
1091/* The plan here is to generate a random anonymous socket name and
1092 * advertise that through a service on the session dbus.
1093 */
1094static const char socket_name[] = "\0wayland";
1095
1096int main(int argc, char *argv[])
1097{
1098 struct wl_display *display;
1099 struct egl_compositor *ec;
1100
1101 display = wl_display_create();
1102
1103 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001104 if (ec == NULL) {
1105 fprintf(stderr, "failed to create compositor\n");
1106 exit(EXIT_FAILURE);
1107 }
1108
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001109 wl_display_set_compositor(display, &ec->base);
1110
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001111 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001112 fprintf(stderr, "failed to add socket: %m\n");
1113 exit(EXIT_FAILURE);
1114 }
1115
1116 wl_display_run(display);
1117
1118 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001119}