blob: c70b2dedf073e99943a935492e4c6645a9473ccc [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øgsberg201a9042008-12-10 00:40:50 -050080
81 struct wl_list link;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040082};
83
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050084static void
85die(const char *msg, ...)
86{
87 va_list ap;
88
89 va_start (ap, msg);
90 vfprintf(stderr, msg, ap);
91 va_end (ap);
92
93 exit(EXIT_FAILURE);
94}
95
96static void
97stdio_write_func (png_structp png, png_bytep data, png_size_t size)
98{
99 FILE *fp;
100 size_t ret;
101
102 fp = png_get_io_ptr (png);
103 while (size) {
104 ret = fwrite (data, 1, size, fp);
105 size -= ret;
106 data += ret;
107 if (size && ferror (fp))
108 die("write: %m\n");
109 }
110}
111
112static void
113png_simple_output_flush_fn (png_structp png_ptr)
114{
115}
116
117static void
118png_simple_error_callback (png_structp png,
119 png_const_charp error_msg)
120{
121 die("png error: %s\n", error_msg);
122}
123
124static void
125png_simple_warning_callback (png_structp png,
126 png_const_charp error_msg)
127{
128 fprintf(stderr, "png warning: %s\n", error_msg);
129}
130
131static void
132convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
133{
134 unsigned int i;
135
136 for (i = 0; i < row_info->rowbytes; i += 4) {
137 uint8_t *b = &data[i];
138 uint32_t pixel;
139
140 memcpy (&pixel, b, sizeof (uint32_t));
141 b[0] = (pixel & 0xff0000) >> 16;
142 b[1] = (pixel & 0x00ff00) >> 8;
143 b[2] = (pixel & 0x0000ff) >> 0;
144 b[3] = 0;
145 }
146}
147
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500148struct screenshooter {
149 struct wl_object base;
150 struct egl_compositor *ec;
151};
152
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500153static void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500154screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter)
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500155{
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500156 struct egl_compositor *ec = shooter->ec;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500157 png_struct *png;
158 png_info *info;
159 png_byte **volatile rows = NULL;
160 png_color_16 white;
161 int depth, i;
162 FILE *fp;
163 uint8_t *data;
164 GLuint stride;
165 static const char filename[] = "wayland-screenshot.png";
166
167 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
168 if (data == NULL)
169 die("eglReadBuffer failed\n");
170 rows = malloc(ec->height * sizeof rows[0]);
171 if (rows == NULL)
172 die("malloc failed\n");
173
174 for (i = 0; i < ec->height; i++)
175 rows[i] = (png_byte *) data + i * stride;
176
177 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
178 png_simple_error_callback,
179 png_simple_warning_callback);
180 if (png == NULL)
181 die("png_create_write_struct failed\n");
182
183 info = png_create_info_struct(png);
184 if (info == NULL)
185 die("png_create_info_struct failed\n");
186
187 fp = fopen(filename, "w");
188 if (fp == NULL)
189 die("fopen failed: %m\n");
190
191 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
192
193 depth = 8;
194 png_set_IHDR(png, info,
195 ec->width,
196 ec->height, depth,
197 PNG_COLOR_TYPE_RGB,
198 PNG_INTERLACE_NONE,
199 PNG_COMPRESSION_TYPE_DEFAULT,
200 PNG_FILTER_TYPE_DEFAULT);
201
202 white.gray = (1 << depth) - 1;
203 white.red = white.blue = white.green = white.gray;
204 png_set_bKGD(png, info, &white);
205 png_write_info (png, info);
206 png_set_write_user_transform_fn(png, convert_pixels);
207
208 png_set_filler(png, 0, PNG_FILLER_AFTER);
209 png_write_image(png, rows);
210 png_write_end(png, info);
211
212 png_destroy_write_struct(&png, &info);
213 fclose(fp);
214 free(rows);
215 free(data);
216}
217
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500218static const struct wl_method screenshooter_methods[] = {
Kristian Høgsberg73f4e762008-12-08 14:07:33 -0500219 { "shoot", screenshooter_shoot, "", NULL }
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500220};
221
222static const struct wl_interface screenshooter_interface = {
223 "screenshooter", 1,
224 ARRAY_LENGTH(screenshooter_methods),
225 screenshooter_methods,
226};
227
228static struct screenshooter *
229screenshooter_create(struct egl_compositor *ec)
230{
231 struct screenshooter *shooter;
232
233 shooter = malloc(sizeof *shooter);
234 if (shooter == NULL)
235 return NULL;
236
237 shooter->base.interface = &screenshooter_interface;
238 shooter->ec = ec;
239
240 return shooter;
241};
242
Kristian Høgsberg54879822008-11-23 17:07:32 -0500243static struct egl_surface *
244egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
245 int x, int y, int width, int height)
246{
247 struct egl_surface *es;
248 int stride;
249 void *data;
250
251 stride = cairo_image_surface_get_stride(surface);
252 data = cairo_image_surface_get_data(surface);
253
254 es = malloc(sizeof *es);
255 if (es == NULL)
256 return NULL;
257
258 glGenTextures(1, &es->texture);
259 glBindTexture(GL_TEXTURE_2D, es->texture);
260 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500264 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
265 GL_BGRA, GL_UNSIGNED_BYTE, data);
266
267 es->map.x = x;
268 es->map.y = y;
269 es->map.width = width;
270 es->map.height = height;
271 es->surface = EGL_NO_SURFACE;
272
273 return es;
274}
275
276static void
277egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
278{
279 glDeleteTextures(1, &es->texture);
280 if (es->surface != EGL_NO_SURFACE)
281 eglDestroySurface(ec->display, es->surface);
282 free(es);
283}
284
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400285static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500286pointer_path(cairo_t *cr, int x, int y)
287{
288 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
289 const int width = 16, height = 16;
290
291 cairo_move_to(cr, x, y);
292 cairo_line_to(cr, x + tx, y + ty);
293 cairo_line_to(cr, x + dx, y + dy);
294 cairo_line_to(cr, x + width - end, y + height);
295 cairo_line_to(cr, x + width, y + height - end);
296 cairo_line_to(cr, x + dy, y + dx);
297 cairo_line_to(cr, x + ty, y + tx);
298 cairo_close_path(cr);
299}
300
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500301static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500302pointer_create(int x, int y, int width, int height)
303{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500304 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500305 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500306 cairo_surface_t *surface;
307 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500308
309 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
310 width, height);
311
312 cr = cairo_create(surface);
313 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
314 cairo_set_line_width (cr, 2);
315 cairo_set_source_rgb(cr, 0, 0, 0);
316 cairo_stroke_preserve(cr);
317 cairo_fill(cr);
318 blur_surface(surface, width);
319
320 pointer_path(cr, hotspot_x, hotspot_y);
321 cairo_stroke_preserve(cr);
322 cairo_set_source_rgb(cr, 1, 1, 1);
323 cairo_fill(cr);
324 cairo_destroy(cr);
325
Kristian Høgsberg54879822008-11-23 17:07:32 -0500326 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
327
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500328 cairo_surface_destroy(surface);
329
Kristian Høgsberg54879822008-11-23 17:07:32 -0500330 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500331}
332
333static struct egl_surface *
334background_create(const char *filename, int width, int height)
335{
336 struct egl_surface *background;
337 GdkPixbuf *pixbuf;
338 GError *error = NULL;
339 int pixbuf_width, pixbuf_height;
340 void *data;
341
342 background = malloc(sizeof *background);
343 if (background == NULL)
344 return NULL;
345
346 g_type_init();
347
348 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
349 if (error != NULL) {
350 free(background);
351 return NULL;
352 }
353
354 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
355 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
356 data = gdk_pixbuf_get_pixels(pixbuf);
357
358 glGenTextures(1, &background->texture);
359 glBindTexture(GL_TEXTURE_2D, background->texture);
360 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500361 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500364 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
365 GL_BGR, GL_UNSIGNED_BYTE, data);
366
367 background->map.x = 0;
368 background->map.y = 0;
369 background->map.width = width;
370 background->map.height = height;
371 background->surface = EGL_NO_SURFACE;
372
373 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500374}
375
376static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500377rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
378{
379 cairo_move_to(cr, x0, y0 + radius);
380 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
381 cairo_line_to(cr, x1 - radius, y0);
382 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
383 cairo_line_to(cr, x1, y1 - radius);
384 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
385 cairo_line_to(cr, x0 + radius, y1);
386 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
387 cairo_close_path(cr);
388}
389
390static void
391draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
392{
393 cairo_pattern_t *gradient;
394 cairo_text_extents_t extents;
395 double bright = 0.15, dim = 0.02;
396 int radius = 10;
397
398 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
399 cairo_set_line_width (cr, 2);
400 rounded_rect(cr, x, y, x + width, y + height, radius);
401 cairo_set_source_rgb(cr, dim, dim, dim);
402 cairo_stroke(cr);
403 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
404 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
405 cairo_stroke(cr);
406
407 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
408 cairo_set_source_rgb(cr, bright, bright, bright);
409 cairo_stroke(cr);
410 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
411 cairo_set_source_rgb(cr, dim, dim, dim);
412 cairo_stroke(cr);
413
414 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
415 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
416 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
417 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
418 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
419 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
420 cairo_set_source(cr, gradient);
421 cairo_fill(cr);
422
423 cairo_set_font_size(cr, 16);
424 cairo_text_extents(cr, text, &extents);
425 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
426 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
427 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
428 cairo_set_line_width (cr, 4);
429 cairo_text_path(cr, text);
430 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
431 cairo_stroke_preserve(cr);
432 cairo_set_source_rgb(cr, 1, 1, 1);
433 cairo_fill(cr);
434}
435
436static struct egl_surface *
437overlay_create(int x, int y, int width, int height)
438{
439 struct egl_surface *es;
440 cairo_surface_t *surface;
441 cairo_t *cr;
442 int total_width, button_x, button_y;
443 const int button_width = 150;
444 const int button_height = 40;
445 const int spacing = 50;
446
447 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
448 width, height);
449
450 cr = cairo_create(surface);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500451 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.8);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500452 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
453 cairo_paint(cr);
454
455 total_width = button_width * 2 + spacing;
456 button_x = (width - total_width) / 2;
457 button_y = height - button_height - 20;
458 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
459 button_x += button_width + spacing;
460 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
461
462 cairo_destroy(cr);
463
464 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
465
466 cairo_surface_destroy(surface);
467
468 return es;
469}
470
471static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500473{
474 GLint vertices[12];
475 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
476 GLuint indices[4] = { 0, 1, 2, 3 };
477
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500478 vertices[0] = es->map.x;
479 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500480 vertices[2] = 0;
481
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500482 vertices[3] = es->map.x;
483 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500484 vertices[5] = 0;
485
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500486 vertices[6] = es->map.x + es->map.width;
487 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500488 vertices[8] = 0;
489
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500490 vertices[9] = es->map.x + es->map.width;
491 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500492 vertices[11] = 0;
493
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500494 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500495 glEnable(GL_TEXTURE_2D);
496 glEnable(GL_BLEND);
497 /* Assume pre-multiplied alpha for now, this probably
498 * needs to be a wayland visual type of thing. */
499 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
500
501 glEnableClientState(GL_VERTEX_ARRAY);
502 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
503 glVertexPointer(3, GL_INT, 0, vertices);
504 glTexCoordPointer(2, GL_INT, 0, tex_coords);
505 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
506}
507
508static void
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500509schedule_repaint(struct egl_compositor *ec);
510
511static void
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500512animate_overlay(struct egl_compositor *ec)
513{
514 double force, y;
515 int32_t top, bottom;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500516#if 1
517 double bounce = 0.0;
518 double friction = 1.0;
519 double spring = 0.2;
520#else
521 double bounce = 0.2;
522 double friction = 0.04;
523 double spring = 0.09;
524#endif
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500525
526 y = ec->overlay_y;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500527 force = (ec->overlay_target - ec->overlay_y) * spring +
528 (ec->overlay_previous - y) * friction;
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500529
530 ec->overlay_y = y + (y - ec->overlay_previous) + force;
531 ec->overlay_previous = y;
532
533 top = ec->height - ec->overlay->map.height;
534 bottom = ec->height;
535 if (ec->overlay_y >= bottom) {
536 ec->overlay_y = bottom;
537 ec->overlay_previous = bottom;
538 }
539
540 if (ec->overlay_y <= top) {
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500541 ec->overlay_y = top + bounce * (top - ec->overlay_y);
542 ec->overlay_previous =
543 top + bounce * (top - ec->overlay_previous);
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500544 }
545
546 ec->overlay->map.y = ec->overlay_y + 0.5;
Kristian Høgsbergffb74062008-11-25 18:10:39 -0500547
Kristian Høgsberg73c30582008-11-25 22:45:46 -0500548 if (fabs(y - ec->overlay_target) > 0.2 ||
549 fabs(ec->overlay_y - ec->overlay_target) > 0.2)
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500550 schedule_repaint(ec);
551}
552
553static void
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500554repaint(void *data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400555{
556 struct egl_compositor *ec = data;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500557 struct egl_surface *es;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500558 struct timespec ts;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500559 uint32_t msecs;
560
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500561 if (!ec->repaint_needed) {
562 ec->repaint_on_timeout = 0;
563 return;
564 }
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500565
566 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400567
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500568 es = container_of(ec->surface_list.next,
569 struct egl_surface, link);
570 while (&es->link != &ec->surface_list) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500571 draw_surface(es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500572
573 es = container_of(es->link.next,
574 struct egl_surface, link);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400575 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400576
Kristian Høgsberg54879822008-11-23 17:07:32 -0500577 draw_surface(ec->overlay);
578
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500579 draw_surface(ec->pointer);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500580
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400581 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500582 ec->repaint_needed = 0;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500583
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500584 clock_gettime(CLOCK_MONOTONIC, &ts);
585 msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
586 wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
587 ec->current_frame++;
588
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500589 wl_event_source_timer_update(ec->timer_source, 10);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500590 ec->repaint_on_timeout = 1;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500591
Kristian Høgsberg5c1e6ec2008-11-25 13:51:36 -0500592 animate_overlay(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400593}
594
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500595static void
596schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400597{
598 struct wl_event_loop *loop;
599
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500600 ec->repaint_needed = 1;
601 if (!ec->repaint_on_timeout) {
602 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500603 wl_event_loop_add_idle(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500604 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400605}
606
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500607static void
608notify_surface_create(struct wl_compositor *compositor,
609 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400610{
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500611 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500612 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400613
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500614 es = malloc(sizeof *es);
615 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400616 return;
617
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500618 es->surface = EGL_NO_SURFACE;
619 wl_surface_set_data(surface, es);
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500620 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400621
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500622 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400623}
624
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500625static void
626notify_surface_destroy(struct wl_compositor *compositor,
627 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400628{
629 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500630 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400631
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500632 es = wl_surface_get_data(surface);
633 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400634 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500635
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500636 wl_list_remove(&es->link);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500637 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400638
639 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400640}
641
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500642static void
643notify_surface_attach(struct wl_compositor *compositor,
644 struct wl_surface *surface, uint32_t name,
645 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400646{
647 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500648 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400649
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500650 es = wl_surface_get_data(surface);
651 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400652 return;
653
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500654 if (es->surface != EGL_NO_SURFACE)
655 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400656
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500657 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500658 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400659
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500660 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400661 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Kristian Høgsberg98fed0f2008-12-09 13:35:35 -0500662 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
663 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
664 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500665 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400666}
667
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500668static void
669notify_surface_map(struct wl_compositor *compositor,
670 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400671{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500672 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400673
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500674 es = wl_surface_get_data(surface);
675 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400676 return;
677
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500678 es->map = *map;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400679}
680
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500681static void
682notify_surface_copy(struct wl_compositor *compositor,
683 struct wl_surface *surface,
684 int32_t dst_x, int32_t dst_y,
685 uint32_t name, uint32_t stride,
686 int32_t x, int32_t y, int32_t width, int32_t height)
687{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500688 struct egl_compositor *ec = (struct egl_compositor *) compositor;
689 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500690 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500691
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500692 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500693
694 /* FIXME: glCopyPixels should work, but then we'll have to
695 * call eglMakeCurrent to set up the src and dest surfaces
696 * first. This seems cheaper, but maybe there's a better way
697 * to accomplish this. */
698
699 src = eglCreateSurfaceForName(ec->display, ec->config,
700 name, x + width, y + height, stride, NULL);
701
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500702 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500703 src, GL_FRONT_LEFT, x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500704 eglDestroySurface(ec->display, src);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500705}
706
707static void
708notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500709 struct wl_surface *surface,
710 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500711{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500712 /* FIXME: This need to take a damage region, of course. */
713}
714
715static uint32_t
716notify_commit(struct wl_compositor *compositor)
717{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500718 struct egl_compositor *ec = (struct egl_compositor *) compositor;
719
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500720 schedule_repaint(ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500721
722 return ec->current_frame;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500723}
724
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500725static void
726notify_pointer_motion(struct wl_compositor *compositor,
727 struct wl_object *source, int x, int y)
728{
729 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsberg961a04c2008-11-25 22:38:56 -0500730 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500731
Kristian Høgsberg961a04c2008-11-25 22:38:56 -0500732 ec->pointer->map.x = x - hotspot_x;
733 ec->pointer->map.y = y - hotspot_y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500734 schedule_repaint(ec);
735}
736
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500737static struct egl_surface *
738pick_surface(struct egl_compositor *ec, int32_t x, int32_t y)
739{
740 struct egl_surface *es;
741
742 es = container_of(ec->surface_list.prev,
743 struct egl_surface, link);
744 while (&es->link != &ec->surface_list) {
745 if (es->map.x <= x && x < es->map.x + es->map.width &&
746 es->map.y <= y && y < es->map.y + es->map.height)
747 return es;
748
749 es = container_of(es->link.prev,
750 struct egl_surface, link);
751 }
752
753 return NULL;
754}
755
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500756static void
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500757notify_pointer_button(struct wl_compositor *compositor,
758 struct wl_object *source,
759 int32_t button, int32_t state)
760{
761 struct egl_compositor *ec = (struct egl_compositor *) compositor;
762 struct egl_surface *es;
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500763 const int hotspot_x = 16, hotspot_y = 16;
764 int x, y;
765
766 x = ec->pointer->map.x + hotspot_x;
767 y = ec->pointer->map.y + hotspot_y;
768
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500769 es = pick_surface(ec, x, y);
770 if (es) {
771 wl_list_remove(&es->link);
772 wl_list_insert(ec->surface_list.prev, &es->link);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500773 }
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500774
Kristian Høgsberg201a9042008-12-10 00:40:50 -0500775 schedule_repaint(ec);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500776}
777
778static void
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500779notify_key(struct wl_compositor *compositor,
780 struct wl_object *source, uint32_t key, uint32_t state)
781{
782 struct egl_compositor *ec = (struct egl_compositor *) compositor;
783
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500784 if (key == KEY_ESC && state == 1) {
785 if (ec->overlay_target == ec->height)
786 ec->overlay_target -= 200;
787 else
788 ec->overlay_target += 200;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500789 schedule_repaint(ec);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -0500790 }
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500791}
792
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500793static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400794 notify_surface_create,
795 notify_surface_destroy,
796 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500797 notify_surface_map,
798 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500799 notify_surface_damage,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500800 notify_commit,
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500801 notify_pointer_motion,
Kristian Høgsbergeac149a2008-12-10 00:24:18 -0500802 notify_pointer_button,
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500803 notify_key
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400804};
805
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500806static const char pointer_device_file[] =
807 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
808static const char keyboard_device_file[] =
809 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-kbd";
810
811static void
812create_input_devices(struct wl_display *display)
813{
814 struct wl_object *obj;
815 const char *path;
816
817 path = getenv("WAYLAND_POINTER");
818 if (path == NULL)
819 path = pointer_device_file;
820
821 obj = wl_input_device_create(display, path);
822 if (obj != NULL)
823 wl_display_add_object(display, obj);
824
825 path = getenv("WAYLAND_KEYBOARD");
826 if (path == NULL)
827 path = keyboard_device_file;
828
829 obj = wl_input_device_create(display, path);
830 if (obj != NULL)
831 wl_display_add_object(display, obj);
832}
833
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500834static uint32_t
835create_frontbuffer(int fd, int *width, int *height, int *stride)
836{
837 drmModeConnector *connector;
838 drmModeRes *resources;
839 drmModeEncoder *encoder;
840 struct drm_mode_modeinfo *mode;
841 struct drm_i915_gem_create create;
842 struct drm_i915_gem_pin pin;
843 struct drm_gem_flink flink;
844 unsigned int fb_id;
845 int i, ret;
846
847 resources = drmModeGetResources(fd);
848 if (!resources) {
849 fprintf(stderr, "drmModeGetResources failed\n");
850 return 0;
851 }
852
853 for (i = 0; i < resources->count_connectors; i++) {
854 connector = drmModeGetConnector(fd, resources->connectors[i]);
855 if (connector == NULL)
856 continue;
857
858 if (connector->connection == DRM_MODE_CONNECTED &&
859 connector->count_modes > 0)
860 break;
861
862 drmModeFreeConnector(connector);
863 }
864
865 if (i == resources->count_connectors) {
866 fprintf(stderr, "No currently active connector found.\n");
867 return -1;
868 }
869
870 mode = &connector->modes[0];
871
872 for (i = 0; i < resources->count_encoders; i++) {
873 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
874
875 if (encoder == NULL)
876 continue;
877
878 if (encoder->encoder_id == connector->encoder_id)
879 break;
880
881 drmModeFreeEncoder(encoder);
882 }
883
884 /* Mode size at 32 bpp */
885 create.size = mode->hdisplay * mode->vdisplay * 4;
886 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
887 fprintf(stderr, "gem create failed: %m\n");
888 return 0;
889 }
890
891 pin.handle = create.handle;
892 pin.alignment = 4096;
893 if (ioctl(fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
894 fprintf(stderr, "failed to pin buffer: %m\n");
895 return 0;
896 }
897
898 ret = drmModeAddFB(fd, mode->hdisplay, mode->vdisplay,
899 32, 32, mode->hdisplay * 4, create.handle, &fb_id);
900 if (ret) {
901 fprintf(stderr, "failed to add fb: %m\n");
902 return 0;
903 }
904
905 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
906 &connector->connector_id, 1, mode);
907 if (ret) {
908 fprintf(stderr, "failed to set mode: %m\n");
909 return 0;
910 }
911
912 flink.handle = create.handle;
913 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
914 fprintf(stderr, "gem flink failed: %m\n");
915 return 0;
916 }
917
918 *width = mode->hdisplay;
919 *height = mode->vdisplay;
920 *stride = mode->hdisplay * 4;
921
922 return flink.name;
923}
924
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500925static int
926pick_config(struct egl_compositor *ec)
927{
928 EGLConfig configs[100];
929 EGLint value, count;
930 int i;
931
932 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
933 fprintf(stderr, "failed to get configs\n");
934 return -1;
935 }
936
937 ec->config = EGL_NO_CONFIG;
938 for (i = 0; i < count; i++) {
939 eglGetConfigAttrib(ec->display,
940 configs[i],
941 EGL_DEPTH_SIZE,
942 &value);
943 if (value > 0) {
944 fprintf(stderr, "config %d has depth size %d\n", i, value);
945 continue;
946 }
947
948 eglGetConfigAttrib(ec->display,
949 configs[i],
950 EGL_STENCIL_SIZE,
951 &value);
952 if (value > 0) {
953 fprintf(stderr, "config %d has stencil size %d\n", i, value);
954 continue;
955 }
956
957 eglGetConfigAttrib(ec->display,
958 configs[i],
959 EGL_CONFIG_CAVEAT,
960 &value);
961 if (value != EGL_NONE) {
962 fprintf(stderr, "config %d has caveat %d\n", i, value);
963 continue;
964 }
965
966 ec->config = configs[i];
967 break;
968 }
969
970 if (ec->config == EGL_NO_CONFIG) {
971 fprintf(stderr, "found no config without depth or stencil buffers\n");
972 return -1;
973 }
974
975 return 0;
976}
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500977
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400978static const char gem_device[] = "/dev/dri/card0";
979
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500980static struct egl_compositor *
981egl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400982{
Kristian Høgsberg443853c2008-11-25 12:12:05 -0500983 EGLint major, minor;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400984 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500985 const char *filename;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500986 struct screenshooter *shooter;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500987 uint32_t fb_name;
988 int stride;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500989 struct wl_event_loop *loop;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -0500990 const static EGLint attribs[] =
991 { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400992
993 ec = malloc(sizeof *ec);
994 if (ec == NULL)
995 return NULL;
996
997 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400998 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400999
Kristian Høgsbergc508d932008-10-13 22:52:42 -04001000 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001001 if (ec->display == NULL) {
1002 fprintf(stderr, "failed to create display\n");
1003 return NULL;
1004 }
1005
1006 if (!eglInitialize(ec->display, &major, &minor)) {
1007 fprintf(stderr, "failed to initialize display\n");
1008 return NULL;
1009 }
1010
Kristian Høgsberg443853c2008-11-25 12:12:05 -05001011 if (pick_config(ec))
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001012 return NULL;
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001013
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05001014 fb_name = create_frontbuffer(eglGetDisplayFD(ec->display),
1015 &ec->width, &ec->height, &stride);
1016 ec->surface = eglCreateSurfaceForName(ec->display, ec->config,
1017 fb_name, ec->width, ec->height, stride, attribs);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001018 if (ec->surface == NULL) {
1019 fprintf(stderr, "failed to create surface\n");
1020 return NULL;
1021 }
1022
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -05001023 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001024 if (ec->context == NULL) {
1025 fprintf(stderr, "failed to create context\n");
1026 return NULL;
1027 }
1028
1029 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
1030 fprintf(stderr, "failed to make context current\n");
1031 return NULL;
1032 }
1033
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001034 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001035 glMatrixMode(GL_PROJECTION);
1036 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001037 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001038 glMatrixMode(GL_MODELVIEW);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001039
1040 create_input_devices(display);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001041
Kristian Høgsberg201a9042008-12-10 00:40:50 -05001042 wl_list_init(&ec->surface_list);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -05001043 filename = getenv("WAYLAND_BACKGROUND");
1044 if (filename == NULL)
1045 filename = "background.jpg";
1046 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001047 ec->pointer = pointer_create(100, 100, 64, 64);
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001048 ec->overlay = overlay_create(0, ec->height, ec->width, 200);
Kristian Høgsberg9f88b182008-12-08 13:52:08 -05001049 ec->overlay_y = ec->height;
Kristian Høgsberg9af92b32008-11-24 01:12:46 -05001050 ec->overlay_target = ec->height;
1051 ec->overlay_previous = ec->height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001052
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001053 ec->gem_fd = open(gem_device, O_RDWR);
1054 if (ec->gem_fd < 0) {
1055 fprintf(stderr, "failed to open drm device\n");
1056 return NULL;
1057 }
1058
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -05001059 shooter = screenshooter_create(ec);
1060 wl_display_add_object(display, &shooter->base);
1061 wl_display_add_global(display, &shooter->base);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05001062
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001063 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberg4a298902008-11-28 18:35:25 -05001064 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001065 ec->repaint_needed = 0;
1066 ec->repaint_on_timeout = 0;
1067
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001068 schedule_repaint(ec);
1069
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001070 return ec;
1071}
1072
1073/* The plan here is to generate a random anonymous socket name and
1074 * advertise that through a service on the session dbus.
1075 */
1076static const char socket_name[] = "\0wayland";
1077
1078int main(int argc, char *argv[])
1079{
1080 struct wl_display *display;
1081 struct egl_compositor *ec;
1082
1083 display = wl_display_create();
1084
1085 ec = egl_compositor_create(display);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05001086 if (ec == NULL) {
1087 fprintf(stderr, "failed to create compositor\n");
1088 exit(EXIT_FAILURE);
1089 }
1090
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001091 wl_display_set_compositor(display, &ec->base);
1092
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -05001093 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
Kristian Høgsberg122912c2008-12-05 11:13:50 -05001094 fprintf(stderr, "failed to add socket: %m\n");
1095 exit(EXIT_FAILURE);
1096 }
1097
1098 wl_display_run(display);
1099
1100 return 0;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001101}