blob: 93d81f933fd251600c73b9de7679546a86d8f64c [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øgsberge9d550b2008-11-19 00:49:39 -050071 int border = 2, radius = 5, shadow = 16;
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øgsberge9d550b2008-11-19 00:49:39 -050076 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050077
78 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050079 window->width + 32, window->height + 32);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050080
Kristian Høgsberge4feb562008-11-08 18:53:37 -050081 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050082 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050083 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
84
Kristian Høgsberg61017b12008-11-02 18:51:48 -050085 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050086
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050087 cairo_translate(cr, shadow + 7, shadow + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050088 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050089 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg8c304f62008-11-10 10:46:53 -050090 rounded_rect(cr, 0, 0, window->width, window->height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050091 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -050092 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050093
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050094#if 1
95 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050096 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050097 rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius);
98 cairo_set_source(cr, outline);
99 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500100 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500101 cairo_set_source(cr, bright);
102 cairo_stroke(cr);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500103 rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500104 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500106
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500107 rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1);
108 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500109 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
110 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500111 cairo_set_source(cr, gradient);
112 cairo_fill(cr);
113 cairo_pattern_destroy(gradient);
114
115 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
116 cairo_move_to(cr, 10, 50);
117 cairo_line_to(cr, window->width - 10, 50);
118 cairo_line_to(cr, window->width - 10, window->height - 10);
119 cairo_line_to(cr, 10, window->height - 10);
120 cairo_close_path(cr);
121 cairo_set_source(cr, dim);
122 cairo_stroke(cr);
123
124 cairo_move_to(cr, 11, 51);
125 cairo_line_to(cr, window->width - 10, 51);
126 cairo_line_to(cr, window->width - 10, window->height - 10);
127 cairo_line_to(cr, 11, window->height - 10);
128 cairo_close_path(cr);
129 cairo_set_source(cr, bright);
130 cairo_stroke(cr);
131
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500132 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
133 cairo_set_font_size(cr, 14);
134 cairo_text_extents(cr, title, &extents);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500135 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500136 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
137 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
138 cairo_set_line_width (cr, 4);
139 cairo_text_path(cr, title);
140 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
141 cairo_stroke_preserve(cr);
142 cairo_set_source_rgb(cr, 1, 1, 1);
143 cairo_fill(cr);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500144#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500145 cairo_destroy(cr);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500146 if (window->buffer != NULL)
147 buffer_destroy(window->buffer, window->fd);
148 buffer = buffer_create_from_cairo_surface(window->fd, surface);
149 window->buffer = buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500150
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151 cairo_surface_destroy(surface);
152
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500153 wl_surface_attach(window->surface, buffer->name,
154 buffer->width, buffer->height, buffer->stride);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500155
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500156 wl_surface_map(window->surface,
157 window->x, window->y,
158 buffer->width, buffer->height);
159
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500160 /* FIXME: Free window->buffer when we receive the ack event. */
161
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500162 width = window->width - 20;
163 height = window->height - 60;
164 buffer = buffer_create(window->fd, width, height, (width * 4 + 15) & ~15);
165 window->egl_buffer = buffer;
166 window->egl_surface = eglCreateSurfaceForName(window->display,
167 window->config, buffer->name,
168 buffer->width, buffer->height,
169 buffer->stride, NULL);
170 if (!eglMakeCurrent(window->display,
171 window->egl_surface, window->egl_surface, window->context))
172 die("failed to make context current\n");
173
174 glViewport(0, 0, width, height);
175
176 if (window->gears == NULL)
177 window->gears = gears_create(0, 0, 0, 0.92);
178
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500179 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500180 wl_surface_copy(window->surface, 10 + shadow, 50 + shadow,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500181 buffer->name, buffer->stride,
182 0, 0, buffer->width, buffer->height);
183
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500184 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500185
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500186 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187}
188
189enum window_state {
190 WINDOW_STABLE,
191 WINDOW_MOVING,
192 WINDOW_RESIZING_UPPER_LEFT,
193 WINDOW_RESIZING_UPPER_RIGHT,
194 WINDOW_RESIZING_LOWER_LEFT,
195 WINDOW_RESIZING_LOWER_RIGHT
196};
197
198enum location {
199 LOCATION_INTERIOR,
200 LOCATION_UPPER_LEFT,
201 LOCATION_UPPER_RIGHT,
202 LOCATION_LOWER_LEFT,
203 LOCATION_LOWER_RIGHT,
204 LOCATION_OUTSIDE
205};
206
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500207static void
208event_handler(struct wl_display *display,
209 uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500210{
211 struct window *window = data;
212 int location, border = 4;
213 int grip_size = 16;
214
215 if (opcode == 0) {
216 window->last_x = arg1;
217 window->last_y = arg2;
218 switch (window->state) {
219 case WINDOW_MOVING:
220 window->x = window->drag_x + arg1;
221 window->y = window->drag_y + arg2;
222 wl_surface_map(window->surface, window->x, window->y,
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500223 window->buffer->width, window->buffer->height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500224 break;
225 case WINDOW_RESIZING_LOWER_RIGHT:
226 window->width = window->drag_x + arg1;
227 window->height = window->drag_y + arg2;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500228 if (window->width < 400)
229 window->width = 400;
230 if (window->height < 400)
231 window->height = 400;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500232 if (!window->redraw_scheduled) {
233 window->redraw_scheduled = 1;
234 g_idle_add(draw_window, window);
235 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500236 break;
237 }
238 }
239
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500240 if (window->x + window->width - grip_size <= window->last_x &&
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500241 window->last_x < window->x + window->width &&
242 window->y + window->height - grip_size <= window->last_y &&
243 window->last_y < window->y + window->height) {
244 location = LOCATION_LOWER_RIGHT;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500245 } else if (window->x + border <= window->last_x &&
246 window->last_x < window->x + window->width - border &&
247 window->y + border <= window->last_y &&
248 window->last_y < window->y + window->height - border) {
249 location = LOCATION_INTERIOR;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500250 } else {
251 location = LOCATION_OUTSIDE;
252 }
253
254 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
255 switch (location) {
256 case LOCATION_INTERIOR:
257 window->drag_x = window->x - window->last_x;
258 window->drag_y = window->y - window->last_y;
259 window->state = WINDOW_MOVING;
260 break;
261 case LOCATION_LOWER_RIGHT:
262 window->drag_x = window->width - window->last_x;
263 window->drag_y = window->height - window->last_y;
264 window->state = WINDOW_RESIZING_LOWER_RIGHT;
265 break;
266 default:
267 window->state = WINDOW_STABLE;
268 break;
269 }
270 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
271 window->state = WINDOW_STABLE;
272 }
273}
274
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500275static struct window *
276window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500277{
278 EGLint major, minor, count;
279 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500280 struct window *window;
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500281 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.92;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500282
283 window = malloc(sizeof *window);
284 if (window == NULL)
285 return NULL;
286
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500287 memset(window, 0, sizeof *window);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500288 window->surface = wl_display_create_surface(display);
289 window->x = 200;
290 window->y = 200;
291 window->width = 450;
292 window->height = 500;
293 window->state = WINDOW_STABLE;
294 window->fd = fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500295 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500296
297 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
298 if (window->display == NULL)
299 die("failed to create display\n");
300
301 if (!eglInitialize(window->display, &major, &minor))
302 die("failed to initialize display\n");
303
304 if (!eglGetConfigs(window->display, configs, 64, &count))
305 die("failed to get configs\n");
306
307 window->config = configs[24];
308 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
309 if (window->context == NULL)
310 die("failed to create context\n");
311
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500312 draw_window(window);
313
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500314 return window;
315}
316
317static gboolean
318draw(gpointer data)
319{
320 struct window *window = data;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500321 struct buffer *buffer;
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500322 int shadow = 16;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500323
324 if (!window->redraw_scheduled) {
325 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500326
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500327 buffer = window->egl_buffer;
328 wl_surface_copy(window->surface,
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500329 10 + shadow, 50 + shadow,
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500330 buffer->name, buffer->stride,
331 0, 0, buffer->width, buffer->height);
332 }
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500333
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500334 window->gears_angle += 1;
335
336 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500337}
338
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500339int main(int argc, char *argv[])
340{
341 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500342 int fd;
343 struct window *window;
344 GMainLoop *loop;
345 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500346
347 fd = open(gem_device, O_RDWR);
348 if (fd < 0) {
349 fprintf(stderr, "drm open failed: %m\n");
350 return -1;
351 }
352
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500353 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500354 if (display == NULL) {
355 fprintf(stderr, "failed to create display: %m\n");
356 return -1;
357 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500358
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500359 loop = g_main_loop_new(NULL, FALSE);
360 source = wayland_source_new(display);
361 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500362
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500363 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500364
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500365 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500366
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500367 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500368
Kristian Høgsberg87330262008-11-17 22:23:55 -0500369 g_timeout_add(50, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500370
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500371 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500372
373 return 0;
374}