blob: ba8c1c551b10bd8feed8d55d082c08952b1c98fe [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);
92 blur_surface(surface);
93
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øgsbergca1d1f62008-11-03 06:59:52 -0500151
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500152 cairo_destroy(cr);
153
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500154 if (window->buffer != NULL)
155 buffer_destroy(window->buffer, window->fd);
156 buffer = buffer_create_from_cairo_surface(window->fd, surface);
157 window->buffer = buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500158
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500159 cairo_surface_destroy(surface);
160
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500161 wl_surface_attach(window->surface, buffer->name,
162 buffer->width, buffer->height, buffer->stride);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500163
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500164 wl_surface_map(window->surface,
165 window->x, window->y,
166 buffer->width, buffer->height);
167
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500168 /* FIXME: Free window->buffer when we receive the ack event. */
169
170 buffer = window->egl_buffer;
171 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500172 wl_surface_copy(window->surface,
173 (window->width - 300) / 2,
174 50 + (window->height - 50 - 300) / 2,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500175 buffer->name, buffer->stride,
176 0, 0, buffer->width, buffer->height);
177
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500178 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500179
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500180 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500181}
182
183enum window_state {
184 WINDOW_STABLE,
185 WINDOW_MOVING,
186 WINDOW_RESIZING_UPPER_LEFT,
187 WINDOW_RESIZING_UPPER_RIGHT,
188 WINDOW_RESIZING_LOWER_LEFT,
189 WINDOW_RESIZING_LOWER_RIGHT
190};
191
192enum location {
193 LOCATION_INTERIOR,
194 LOCATION_UPPER_LEFT,
195 LOCATION_UPPER_RIGHT,
196 LOCATION_LOWER_LEFT,
197 LOCATION_LOWER_RIGHT,
198 LOCATION_OUTSIDE
199};
200
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500201static void
202event_handler(struct wl_display *display,
203 uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500204{
205 struct window *window = data;
206 int location, border = 4;
207 int grip_size = 16;
208
209 if (opcode == 0) {
210 window->last_x = arg1;
211 window->last_y = arg2;
212 switch (window->state) {
213 case WINDOW_MOVING:
214 window->x = window->drag_x + arg1;
215 window->y = window->drag_y + arg2;
216 wl_surface_map(window->surface, window->x, window->y,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500217 window->buffer->width, window->buffer->height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500218 break;
219 case WINDOW_RESIZING_LOWER_RIGHT:
220 window->width = window->drag_x + arg1;
221 window->height = window->drag_y + arg2;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500222 if (window->width < 400)
223 window->width = 400;
224 if (window->height < 400)
225 window->height = 400;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500226 if (!window->redraw_scheduled) {
227 window->redraw_scheduled = 1;
228 g_idle_add(draw_window, window);
229 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500230 break;
231 }
232 }
233
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500234 if (window->x + window->width - grip_size <= window->last_x &&
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500235 window->last_x < window->x + window->width &&
236 window->y + window->height - grip_size <= window->last_y &&
237 window->last_y < window->y + window->height) {
238 location = LOCATION_LOWER_RIGHT;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500239 } else if (window->x + border <= window->last_x &&
240 window->last_x < window->x + window->width - border &&
241 window->y + border <= window->last_y &&
242 window->last_y < window->y + window->height - border) {
243 location = LOCATION_INTERIOR;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500244 } else {
245 location = LOCATION_OUTSIDE;
246 }
247
248 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
249 switch (location) {
250 case LOCATION_INTERIOR:
251 window->drag_x = window->x - window->last_x;
252 window->drag_y = window->y - window->last_y;
253 window->state = WINDOW_MOVING;
254 break;
255 case LOCATION_LOWER_RIGHT:
256 window->drag_x = window->width - window->last_x;
257 window->drag_y = window->height - window->last_y;
258 window->state = WINDOW_RESIZING_LOWER_RIGHT;
259 break;
260 default:
261 window->state = WINDOW_STABLE;
262 break;
263 }
264 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
265 window->state = WINDOW_STABLE;
266 }
267}
268
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500269static struct window *
270window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500271{
272 EGLint major, minor, count;
273 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500274 struct window *window;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500275 struct buffer *buffer;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500276 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500277
278 window = malloc(sizeof *window);
279 if (window == NULL)
280 return NULL;
281
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500282 memset(window, 0, sizeof *window);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500283 window->surface = wl_display_create_surface(display);
284 window->x = 200;
285 window->y = 200;
286 window->width = 450;
287 window->height = 500;
288 window->state = WINDOW_STABLE;
289 window->fd = fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500290 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500291
292 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
293 if (window->display == NULL)
294 die("failed to create display\n");
295
296 if (!eglInitialize(window->display, &major, &minor))
297 die("failed to initialize display\n");
298
299 if (!eglGetConfigs(window->display, configs, 64, &count))
300 die("failed to get configs\n");
301
302 window->config = configs[24];
303 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
304 if (window->context == NULL)
305 die("failed to create context\n");
306
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500307 /* FIXME: We need to get the stride right here in a chipset
308 * independent way. Maybe do it in name_cairo_surface(). */
309 buffer = buffer_create(window->fd, 300, 300, (300 * 4 + 15) & ~15);
310 window->egl_buffer = buffer;
311 window->egl_surface = eglCreateSurfaceForName(window->display,
312 window->config, buffer->name,
313 buffer->width, buffer->height,
314 buffer->stride, NULL);
315
316 if (window->egl_surface == NULL)
317 die("failed to create egl surface\n");
318
319 if (!eglMakeCurrent(window->display,
320 window->egl_surface, window->egl_surface, window->context))
321 die("failed to make context current\n");
322
323 glViewport(0, 0, 300, 300);
324
325 window->gears = gears_create(red, green, blue, alpha);
326 window->gears_angle = 0.0;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500327
328 draw_window(window);
329
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500330 return window;
331}
332
333static gboolean
334draw(gpointer data)
335{
336 struct window *window = data;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500337 struct buffer *buffer;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500338
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500339
340 if (!window->redraw_scheduled) {
341 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500342
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500343 buffer = window->egl_buffer;
344 wl_surface_copy(window->surface,
345 (window->width - 300) / 2,
346 50 + (window->height - 50 - 300) / 2,
347 buffer->name, buffer->stride,
348 0, 0, buffer->width, buffer->height);
349 }
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500350
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500351 window->gears_angle += 1;
352
353 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500354}
355
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356int main(int argc, char *argv[])
357{
358 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500359 int fd;
360 struct window *window;
361 GMainLoop *loop;
362 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500363
364 fd = open(gem_device, O_RDWR);
365 if (fd < 0) {
366 fprintf(stderr, "drm open failed: %m\n");
367 return -1;
368 }
369
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500370 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500371 if (display == NULL) {
372 fprintf(stderr, "failed to create display: %m\n");
373 return -1;
374 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500375
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500376 loop = g_main_loop_new(NULL, FALSE);
377 source = wayland_source_new(display);
378 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500379
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500380 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500381
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500382 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500383
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500384 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500385
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500386 g_timeout_add(20, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500387
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500388 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500389
390 return 0;
391}