blob: 0781d6a2615c2d418bc4bfa44c9e6ee2d4714565 [file] [log] [blame]
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005#include <fcntl.h>
6#include <unistd.h>
7#include <math.h>
8#include <time.h>
9#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050010#include <glib.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050011
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050012#include <GL/gl.h>
13#include <eagle.h>
14
Kristian Høgsberg61017b12008-11-02 18:51:48 -050015#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050016#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050017
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050018#include "gears.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050019#include "cairo-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050020
21static const char gem_device[] = "/dev/dri/card0";
22static const char socket_name[] = "\0wayland";
23
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050024static void die(const char *msg)
25{
26 fprintf(stderr, "%s", msg);
27 exit(EXIT_FAILURE);
28}
29
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030struct window {
31 struct wl_surface *surface;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050032 int x, y, width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033 int drag_x, drag_y, last_x, last_y;
34 int state;
35 uint32_t name;
36 int fd;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037 int redraw_scheduled;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050038 cairo_pattern_t *background;
39
40 struct buffer *buffer;
41 struct buffer *egl_buffer;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050042
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050043 GLfloat gears_angle;
44 struct gears *gears;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050045 EGLDisplay display;
46 EGLContext context;
47 EGLConfig config;
48 EGLSurface egl_surface;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050049};
50
Kristian Høgsberge4feb562008-11-08 18:53:37 -050051static void
52rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
53{
54 cairo_move_to(cr, x0, y0 + radius);
55 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
56 cairo_line_to(cr, x1 - radius, y0);
57 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
58 cairo_line_to(cr, x1, y1 - radius);
59 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
60 cairo_line_to(cr, x0 + radius, y1);
61 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
62 cairo_close_path(cr);
63}
64
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050065static gboolean
66draw_window(void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050067{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050068 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050069 cairo_surface_t *surface;
70 cairo_t *cr;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050071 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050072 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050073 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050074 struct buffer *buffer;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050075 const static char title[] = "Wayland First Post";
Kristian Høgsberg61017b12008-11-02 18:51:48 -050076
77 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050078 window->width + 32, window->height + 32);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079
Kristian Høgsberge4feb562008-11-08 18:53:37 -050080 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
81 bright = cairo_pattern_create_rgb(0.6, 0.6, 0.6);
82 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
83
Kristian Høgsberg61017b12008-11-02 18:51:48 -050084 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050085
86 cairo_translate(cr, 16 + 5, 16 + 3);
87 cairo_set_line_width (cr, border);
88 cairo_set_source_rgba(cr, 0, 0, 0, 0.5);
89 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
90 cairo_stroke_preserve(cr);
91 cairo_fill(cr);
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050092 blur_surface(surface, 16 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050093
94 cairo_translate(cr, -5, -3);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050095 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050096 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
97 cairo_set_source(cr, outline);
98 cairo_stroke(cr);
99 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius);
100 cairo_set_source(cr, bright);
101 cairo_stroke(cr);
102 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius);
103 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500105
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500106 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius);
107 gradient = cairo_pattern_create_linear (0, 0, 0, window->height);
108 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.4, 0.4, 0.4);
109 cairo_pattern_add_color_stop_rgb(gradient, 0.2, 0.7, 0.7, 0.7);
110 cairo_set_source(cr, gradient);
111 cairo_fill(cr);
112 cairo_pattern_destroy(gradient);
113
114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
115 cairo_move_to(cr, 10, 50);
116 cairo_line_to(cr, window->width - 10, 50);
117 cairo_line_to(cr, window->width - 10, window->height - 10);
118 cairo_line_to(cr, 10, window->height - 10);
119 cairo_close_path(cr);
120 cairo_set_source(cr, dim);
121 cairo_stroke(cr);
122
123 cairo_move_to(cr, 11, 51);
124 cairo_line_to(cr, window->width - 10, 51);
125 cairo_line_to(cr, window->width - 10, window->height - 10);
126 cairo_line_to(cr, 11, window->height - 10);
127 cairo_close_path(cr);
128 cairo_set_source(cr, bright);
129 cairo_stroke(cr);
130
131 cairo_move_to(cr, 10, 50);
132 cairo_line_to(cr, window->width - 10, 50);
133 cairo_line_to(cr, window->width - 10, window->height - 10);
134 cairo_line_to(cr, 10, window->height - 10);
135 cairo_close_path(cr);
136 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
137 cairo_fill(cr);
138
139 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
140 cairo_set_font_size(cr, 14);
141 cairo_text_extents(cr, title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500142 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500143 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
144 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
145 cairo_set_line_width (cr, 4);
146 cairo_text_path(cr, title);
147 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
148 cairo_stroke_preserve(cr);
149 cairo_set_source_rgb(cr, 1, 1, 1);
150 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151 cairo_destroy(cr);
152
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500153 if (window->buffer != NULL)
154 buffer_destroy(window->buffer, window->fd);
155 buffer = buffer_create_from_cairo_surface(window->fd, surface);
156 window->buffer = buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500157
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500158 cairo_surface_destroy(surface);
159
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500160 wl_surface_attach(window->surface, buffer->name,
161 buffer->width, buffer->height, buffer->stride);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500162
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500163 wl_surface_map(window->surface,
164 window->x, window->y,
165 buffer->width, buffer->height);
166
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500167 /* FIXME: Free window->buffer when we receive the ack event. */
168
169 buffer = window->egl_buffer;
170 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500171 wl_surface_copy(window->surface,
172 (window->width - 300) / 2,
173 50 + (window->height - 50 - 300) / 2,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500174 buffer->name, buffer->stride,
175 0, 0, buffer->width, buffer->height);
176
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500177 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500178
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500179 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500180}
181
182enum window_state {
183 WINDOW_STABLE,
184 WINDOW_MOVING,
185 WINDOW_RESIZING_UPPER_LEFT,
186 WINDOW_RESIZING_UPPER_RIGHT,
187 WINDOW_RESIZING_LOWER_LEFT,
188 WINDOW_RESIZING_LOWER_RIGHT
189};
190
191enum location {
192 LOCATION_INTERIOR,
193 LOCATION_UPPER_LEFT,
194 LOCATION_UPPER_RIGHT,
195 LOCATION_LOWER_LEFT,
196 LOCATION_LOWER_RIGHT,
197 LOCATION_OUTSIDE
198};
199
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500200static void
201event_handler(struct wl_display *display,
202 uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500203{
204 struct window *window = data;
205 int location, border = 4;
206 int grip_size = 16;
207
208 if (opcode == 0) {
209 window->last_x = arg1;
210 window->last_y = arg2;
211 switch (window->state) {
212 case WINDOW_MOVING:
213 window->x = window->drag_x + arg1;
214 window->y = window->drag_y + arg2;
215 wl_surface_map(window->surface, window->x, window->y,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500216 window->buffer->width, window->buffer->height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500217 break;
218 case WINDOW_RESIZING_LOWER_RIGHT:
219 window->width = window->drag_x + arg1;
220 window->height = window->drag_y + arg2;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500221 if (window->width < 400)
222 window->width = 400;
223 if (window->height < 400)
224 window->height = 400;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500225 if (!window->redraw_scheduled) {
226 window->redraw_scheduled = 1;
227 g_idle_add(draw_window, window);
228 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500229 break;
230 }
231 }
232
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500233 if (window->x + window->width - grip_size <= window->last_x &&
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500234 window->last_x < window->x + window->width &&
235 window->y + window->height - grip_size <= window->last_y &&
236 window->last_y < window->y + window->height) {
237 location = LOCATION_LOWER_RIGHT;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500238 } else if (window->x + border <= window->last_x &&
239 window->last_x < window->x + window->width - border &&
240 window->y + border <= window->last_y &&
241 window->last_y < window->y + window->height - border) {
242 location = LOCATION_INTERIOR;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500243 } else {
244 location = LOCATION_OUTSIDE;
245 }
246
247 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
248 switch (location) {
249 case LOCATION_INTERIOR:
250 window->drag_x = window->x - window->last_x;
251 window->drag_y = window->y - window->last_y;
252 window->state = WINDOW_MOVING;
253 break;
254 case LOCATION_LOWER_RIGHT:
255 window->drag_x = window->width - window->last_x;
256 window->drag_y = window->height - window->last_y;
257 window->state = WINDOW_RESIZING_LOWER_RIGHT;
258 break;
259 default:
260 window->state = WINDOW_STABLE;
261 break;
262 }
263 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
264 window->state = WINDOW_STABLE;
265 }
266}
267
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500268static struct window *
269window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500270{
271 EGLint major, minor, count;
272 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500273 struct window *window;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500274 struct buffer *buffer;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500275 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500276
277 window = malloc(sizeof *window);
278 if (window == NULL)
279 return NULL;
280
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500281 memset(window, 0, sizeof *window);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500282 window->surface = wl_display_create_surface(display);
283 window->x = 200;
284 window->y = 200;
285 window->width = 450;
286 window->height = 500;
287 window->state = WINDOW_STABLE;
288 window->fd = fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500289 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500290
291 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
292 if (window->display == NULL)
293 die("failed to create display\n");
294
295 if (!eglInitialize(window->display, &major, &minor))
296 die("failed to initialize display\n");
297
298 if (!eglGetConfigs(window->display, configs, 64, &count))
299 die("failed to get configs\n");
300
301 window->config = configs[24];
302 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
303 if (window->context == NULL)
304 die("failed to create context\n");
305
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500306 /* FIXME: We need to get the stride right here in a chipset
307 * independent way. Maybe do it in name_cairo_surface(). */
308 buffer = buffer_create(window->fd, 300, 300, (300 * 4 + 15) & ~15);
309 window->egl_buffer = buffer;
310 window->egl_surface = eglCreateSurfaceForName(window->display,
311 window->config, buffer->name,
312 buffer->width, buffer->height,
313 buffer->stride, NULL);
314
315 if (window->egl_surface == NULL)
316 die("failed to create egl surface\n");
317
318 if (!eglMakeCurrent(window->display,
319 window->egl_surface, window->egl_surface, window->context))
320 die("failed to make context current\n");
321
322 glViewport(0, 0, 300, 300);
323
324 window->gears = gears_create(red, green, blue, alpha);
325 window->gears_angle = 0.0;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500326
327 draw_window(window);
328
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500329 return window;
330}
331
332static gboolean
333draw(gpointer data)
334{
335 struct window *window = data;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500336 struct buffer *buffer;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500337
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500338
339 if (!window->redraw_scheduled) {
340 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500341
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500342 buffer = window->egl_buffer;
343 wl_surface_copy(window->surface,
344 (window->width - 300) / 2,
345 50 + (window->height - 50 - 300) / 2,
346 buffer->name, buffer->stride,
347 0, 0, buffer->width, buffer->height);
348 }
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500349
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500350 window->gears_angle += 1;
351
352 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500353}
354
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500355int main(int argc, char *argv[])
356{
357 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500358 int fd;
359 struct window *window;
360 GMainLoop *loop;
361 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500362
363 fd = open(gem_device, O_RDWR);
364 if (fd < 0) {
365 fprintf(stderr, "drm open failed: %m\n");
366 return -1;
367 }
368
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500369 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500370 if (display == NULL) {
371 fprintf(stderr, "failed to create display: %m\n");
372 return -1;
373 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500374
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500375 loop = g_main_loop_new(NULL, FALSE);
376 source = wayland_source_new(display);
377 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500378
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500379 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500380
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500381 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500382
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500383 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500384
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500385 g_timeout_add(20, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500386
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500387 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500388
389 return 0;
390}