Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include <math.h> |
| 8 | #include <time.h> |
| 9 | #include <cairo.h> |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 10 | #include <glib.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 11 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 12 | #include <GL/gl.h> |
| 13 | #include <eagle.h> |
| 14 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 15 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 16 | #include "wayland-glib.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 17 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 18 | #include "gears.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 19 | #include "cairo-util.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 20 | |
| 21 | static const char gem_device[] = "/dev/dri/card0"; |
| 22 | static const char socket_name[] = "\0wayland"; |
| 23 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 24 | static void die(const char *msg) |
| 25 | { |
| 26 | fprintf(stderr, "%s", msg); |
| 27 | exit(EXIT_FAILURE); |
| 28 | } |
| 29 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 30 | struct window { |
| 31 | struct wl_surface *surface; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 32 | int x, y, width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 33 | int drag_x, drag_y, last_x, last_y; |
| 34 | int state; |
| 35 | uint32_t name; |
| 36 | int fd; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 37 | int redraw_scheduled; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 38 | cairo_pattern_t *background; |
| 39 | |
| 40 | struct buffer *buffer; |
| 41 | struct buffer *egl_buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 42 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 43 | GLfloat gears_angle; |
| 44 | struct gears *gears; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 45 | EGLDisplay display; |
| 46 | EGLContext context; |
| 47 | EGLConfig config; |
| 48 | EGLSurface egl_surface; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 49 | }; |
| 50 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 51 | static void |
| 52 | rounded_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øgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 65 | static gboolean |
| 66 | draw_window(void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 67 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 68 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 69 | cairo_surface_t *surface; |
| 70 | cairo_t *cr; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 71 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 72 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 73 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 74 | struct buffer *buffer; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 75 | const static char title[] = "Wayland First Post"; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 76 | |
| 77 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 78 | window->width + 32, window->height + 32); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 79 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 80 | 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øgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 84 | cr = cairo_create(surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 85 | |
Kristian Høgsberg | 8c304f6 | 2008-11-10 10:46:53 -0500 | [diff] [blame] | 86 | cairo_translate(cr, 16 + 7, 16 + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 87 | cairo_set_line_width (cr, border); |
| 88 | cairo_set_source_rgba(cr, 0, 0, 0, 0.5); |
Kristian Høgsberg | 8c304f6 | 2008-11-10 10:46:53 -0500 | [diff] [blame] | 89 | rounded_rect(cr, 0, 0, window->width, window->height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 90 | cairo_fill(cr); |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame^] | 91 | blur_surface(surface, 24 + radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 92 | |
| 93 | cairo_translate(cr, -5, -3); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 94 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 95 | rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius); |
| 96 | cairo_set_source(cr, outline); |
| 97 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 98 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 99 | cairo_set_source(cr, bright); |
| 100 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 101 | rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 102 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 103 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 104 | |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 105 | 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øgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 107 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.4, 0.4, 0.4); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 108 | cairo_pattern_add_color_stop_rgb(gradient, 1, 0.7, 0.7, 0.7); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 109 | 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øgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 141 | cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 142 | 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øgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 150 | cairo_destroy(cr); |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 151 | 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øgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 155 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 156 | cairo_surface_destroy(surface); |
| 157 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 158 | wl_surface_attach(window->surface, buffer->name, |
| 159 | buffer->width, buffer->height, buffer->stride); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 160 | |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 161 | wl_surface_map(window->surface, |
| 162 | window->x, window->y, |
| 163 | buffer->width, buffer->height); |
| 164 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 165 | /* 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øgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 169 | wl_surface_copy(window->surface, |
| 170 | (window->width - 300) / 2, |
| 171 | 50 + (window->height - 50 - 300) / 2, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 172 | buffer->name, buffer->stride, |
| 173 | 0, 0, buffer->width, buffer->height); |
| 174 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 175 | window->redraw_scheduled = 0; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 176 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 177 | return FALSE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | enum 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 | |
| 189 | enum 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øgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 198 | static void |
| 199 | event_handler(struct wl_display *display, |
| 200 | uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 201 | { |
| 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øgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 214 | window->buffer->width, window->buffer->height); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 215 | break; |
| 216 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 217 | window->width = window->drag_x + arg1; |
| 218 | window->height = window->drag_y + arg2; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 219 | if (window->width < 400) |
| 220 | window->width = 400; |
| 221 | if (window->height < 400) |
| 222 | window->height = 400; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 223 | if (!window->redraw_scheduled) { |
| 224 | window->redraw_scheduled = 1; |
| 225 | g_idle_add(draw_window, window); |
| 226 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 231 | if (window->x + window->width - grip_size <= window->last_x && |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 232 | 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øgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 236 | } 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øgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 241 | } 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øgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 266 | static struct window * |
| 267 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 268 | { |
| 269 | EGLint major, minor, count; |
| 270 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 271 | struct window *window; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 272 | struct buffer *buffer; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 273 | const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 274 | |
| 275 | window = malloc(sizeof *window); |
| 276 | if (window == NULL) |
| 277 | return NULL; |
| 278 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 279 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 280 | 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øgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 287 | window->background = cairo_pattern_create_rgba (red, green, blue, alpha); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 288 | |
| 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øgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 304 | /* 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øgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 324 | |
| 325 | draw_window(window); |
| 326 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 327 | return window; |
| 328 | } |
| 329 | |
| 330 | static gboolean |
| 331 | draw(gpointer data) |
| 332 | { |
| 333 | struct window *window = data; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 334 | struct buffer *buffer; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 335 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 336 | |
| 337 | if (!window->redraw_scheduled) { |
| 338 | gears_draw(window->gears, window->gears_angle); |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 339 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 340 | 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øgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 347 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 348 | window->gears_angle += 1; |
| 349 | |
| 350 | return TRUE; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 353 | int main(int argc, char *argv[]) |
| 354 | { |
| 355 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 356 | int fd; |
| 357 | struct window *window; |
| 358 | GMainLoop *loop; |
| 359 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 360 | |
| 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øgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 367 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 368 | if (display == NULL) { |
| 369 | fprintf(stderr, "failed to create display: %m\n"); |
| 370 | return -1; |
| 371 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 372 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 373 | loop = g_main_loop_new(NULL, FALSE); |
| 374 | source = wayland_source_new(display); |
| 375 | g_source_attach(source, NULL); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 376 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 377 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 378 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 379 | draw_window(window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 380 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 381 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 382 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame^] | 383 | g_timeout_add(50, draw, window); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 384 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 385 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |