blob: 634952d7cf090ba8c2aea1661366eafc0b721472 [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
Kristian Høgsberg8c304f62008-11-10 10:46:53 -050086 cairo_translate(cr, 16 + 7, 16 + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050087 cairo_set_line_width (cr, border);
88 cairo_set_source_rgba(cr, 0, 0, 0, 0.5);
Kristian Høgsberg8c304f62008-11-10 10:46:53 -050089 rounded_rect(cr, 0, 0, window->width, window->height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050090 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -050091 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050092
93 cairo_translate(cr, -5, -3);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050094 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050095 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
96 cairo_set_source(cr, outline);
97 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -050098 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050099 cairo_set_source(cr, bright);
100 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500101 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500102 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500103 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500104
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500105 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
106 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500107 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.4, 0.4, 0.4);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500108 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.7, 0.7, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500109 cairo_set_source(cr, gradient);
110 cairo_fill(cr);
111 cairo_pattern_destroy(gradient);
112
113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
114 cairo_move_to(cr, 10, 50);
115 cairo_line_to(cr, window->width - 10, 50);
116 cairo_line_to(cr, window->width - 10, window->height - 10);
117 cairo_line_to(cr, 10, window->height - 10);
118 cairo_close_path(cr);
119 cairo_set_source(cr, dim);
120 cairo_stroke(cr);
121
122 cairo_move_to(cr, 11, 51);
123 cairo_line_to(cr, window->width - 10, 51);
124 cairo_line_to(cr, window->width - 10, window->height - 10);
125 cairo_line_to(cr, 11, window->height - 10);
126 cairo_close_path(cr);
127 cairo_set_source(cr, bright);
128 cairo_stroke(cr);
129
130 cairo_move_to(cr, 10, 50);
131 cairo_line_to(cr, window->width - 10, 50);
132 cairo_line_to(cr, window->width - 10, window->height - 10);
133 cairo_line_to(cr, 10, window->height - 10);
134 cairo_close_path(cr);
135 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
136 cairo_fill(cr);
137
138 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
139 cairo_set_font_size(cr, 14);
140 cairo_text_extents(cr, title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500141 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500142 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
143 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
144 cairo_set_line_width (cr, 4);
145 cairo_text_path(cr, title);
146 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
147 cairo_stroke_preserve(cr);
148 cairo_set_source_rgb(cr, 1, 1, 1);
149 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500150 cairo_destroy(cr);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500151 if (window->buffer != NULL)
152 buffer_destroy(window->buffer, window->fd);
153 buffer = buffer_create_from_cairo_surface(window->fd, surface);
154 window->buffer = buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500155
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500156 cairo_surface_destroy(surface);
157
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500158 wl_surface_attach(window->surface, buffer->name,
159 buffer->width, buffer->height, buffer->stride);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500160
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500161 wl_surface_map(window->surface,
162 window->x, window->y,
163 buffer->width, buffer->height);
164
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500165 /* FIXME: Free window->buffer when we receive the ack event. */
166
167 buffer = window->egl_buffer;
168 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500169 wl_surface_copy(window->surface,
170 (window->width - 300) / 2,
171 50 + (window->height - 50 - 300) / 2,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500172 buffer->name, buffer->stride,
173 0, 0, buffer->width, buffer->height);
174
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500175 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500176
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500177 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500178}
179
180enum window_state {
181 WINDOW_STABLE,
182 WINDOW_MOVING,
183 WINDOW_RESIZING_UPPER_LEFT,
184 WINDOW_RESIZING_UPPER_RIGHT,
185 WINDOW_RESIZING_LOWER_LEFT,
186 WINDOW_RESIZING_LOWER_RIGHT
187};
188
189enum location {
190 LOCATION_INTERIOR,
191 LOCATION_UPPER_LEFT,
192 LOCATION_UPPER_RIGHT,
193 LOCATION_LOWER_LEFT,
194 LOCATION_LOWER_RIGHT,
195 LOCATION_OUTSIDE
196};
197
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500198static void
199event_handler(struct wl_display *display,
200 uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500201{
202 struct window *window = data;
203 int location, border = 4;
204 int grip_size = 16;
205
206 if (opcode == 0) {
207 window->last_x = arg1;
208 window->last_y = arg2;
209 switch (window->state) {
210 case WINDOW_MOVING:
211 window->x = window->drag_x + arg1;
212 window->y = window->drag_y + arg2;
213 wl_surface_map(window->surface, window->x, window->y,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500214 window->buffer->width, window->buffer->height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500215 break;
216 case WINDOW_RESIZING_LOWER_RIGHT:
217 window->width = window->drag_x + arg1;
218 window->height = window->drag_y + arg2;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500219 if (window->width < 400)
220 window->width = 400;
221 if (window->height < 400)
222 window->height = 400;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500223 if (!window->redraw_scheduled) {
224 window->redraw_scheduled = 1;
225 g_idle_add(draw_window, window);
226 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500227 break;
228 }
229 }
230
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500231 if (window->x + window->width - grip_size <= window->last_x &&
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500232 window->last_x < window->x + window->width &&
233 window->y + window->height - grip_size <= window->last_y &&
234 window->last_y < window->y + window->height) {
235 location = LOCATION_LOWER_RIGHT;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500236 } else if (window->x + border <= window->last_x &&
237 window->last_x < window->x + window->width - border &&
238 window->y + border <= window->last_y &&
239 window->last_y < window->y + window->height - border) {
240 location = LOCATION_INTERIOR;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500241 } else {
242 location = LOCATION_OUTSIDE;
243 }
244
245 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
246 switch (location) {
247 case LOCATION_INTERIOR:
248 window->drag_x = window->x - window->last_x;
249 window->drag_y = window->y - window->last_y;
250 window->state = WINDOW_MOVING;
251 break;
252 case LOCATION_LOWER_RIGHT:
253 window->drag_x = window->width - window->last_x;
254 window->drag_y = window->height - window->last_y;
255 window->state = WINDOW_RESIZING_LOWER_RIGHT;
256 break;
257 default:
258 window->state = WINDOW_STABLE;
259 break;
260 }
261 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
262 window->state = WINDOW_STABLE;
263 }
264}
265
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500266static struct window *
267window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500268{
269 EGLint major, minor, count;
270 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500271 struct window *window;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500272 struct buffer *buffer;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500273 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500274
275 window = malloc(sizeof *window);
276 if (window == NULL)
277 return NULL;
278
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500279 memset(window, 0, sizeof *window);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500280 window->surface = wl_display_create_surface(display);
281 window->x = 200;
282 window->y = 200;
283 window->width = 450;
284 window->height = 500;
285 window->state = WINDOW_STABLE;
286 window->fd = fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500287 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500288
289 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
290 if (window->display == NULL)
291 die("failed to create display\n");
292
293 if (!eglInitialize(window->display, &major, &minor))
294 die("failed to initialize display\n");
295
296 if (!eglGetConfigs(window->display, configs, 64, &count))
297 die("failed to get configs\n");
298
299 window->config = configs[24];
300 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
301 if (window->context == NULL)
302 die("failed to create context\n");
303
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500304 /* FIXME: We need to get the stride right here in a chipset
305 * independent way. Maybe do it in name_cairo_surface(). */
306 buffer = buffer_create(window->fd, 300, 300, (300 * 4 + 15) & ~15);
307 window->egl_buffer = buffer;
308 window->egl_surface = eglCreateSurfaceForName(window->display,
309 window->config, buffer->name,
310 buffer->width, buffer->height,
311 buffer->stride, NULL);
312
313 if (window->egl_surface == NULL)
314 die("failed to create egl surface\n");
315
316 if (!eglMakeCurrent(window->display,
317 window->egl_surface, window->egl_surface, window->context))
318 die("failed to make context current\n");
319
320 glViewport(0, 0, 300, 300);
321
322 window->gears = gears_create(red, green, blue, alpha);
323 window->gears_angle = 0.0;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500324
325 draw_window(window);
326
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500327 return window;
328}
329
330static gboolean
331draw(gpointer data)
332{
333 struct window *window = data;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500334 struct buffer *buffer;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500335
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500336
337 if (!window->redraw_scheduled) {
338 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500339
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500340 buffer = window->egl_buffer;
341 wl_surface_copy(window->surface,
342 (window->width - 300) / 2,
343 50 + (window->height - 50 - 300) / 2,
344 buffer->name, buffer->stride,
345 0, 0, buffer->width, buffer->height);
346 }
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500347
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500348 window->gears_angle += 1;
349
350 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500351}
352
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500353int main(int argc, char *argv[])
354{
355 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500356 int fd;
357 struct window *window;
358 GMainLoop *loop;
359 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500360
361 fd = open(gem_device, O_RDWR);
362 if (fd < 0) {
363 fprintf(stderr, "drm open failed: %m\n");
364 return -1;
365 }
366
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500367 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500368 if (display == NULL) {
369 fprintf(stderr, "failed to create display: %m\n");
370 return -1;
371 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500372
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500373 loop = g_main_loop_new(NULL, FALSE);
374 source = wayland_source_new(display);
375 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500376
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500377 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500378
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500379 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500380
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500381 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500382
Kristian Høgsberg87330262008-11-17 22:23:55 -0500383 g_timeout_add(50, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500384
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500385 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500386
387 return 0;
388}