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 { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 31 | struct wl_display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 32 | struct wl_surface *surface; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 33 | int x, y, width, height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 34 | int margin; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 35 | int drag_x, drag_y, last_x, last_y; |
| 36 | int state; |
| 37 | uint32_t name; |
| 38 | int fd; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 39 | int resized; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 40 | cairo_pattern_t *background; |
| 41 | |
| 42 | struct buffer *buffer; |
| 43 | struct buffer *egl_buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 44 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 45 | GLfloat gears_angle; |
| 46 | struct gears *gears; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 47 | EGLDisplay egl_display; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 48 | EGLContext context; |
| 49 | EGLConfig config; |
| 50 | EGLSurface egl_surface; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 53 | static void |
| 54 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 55 | { |
| 56 | cairo_move_to(cr, x0, y0 + radius); |
| 57 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 58 | cairo_line_to(cr, x1 - radius, y0); |
| 59 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 60 | cairo_line_to(cr, x1, y1 - radius); |
| 61 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 62 | cairo_line_to(cr, x0 + radius, y1); |
| 63 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 64 | cairo_close_path(cr); |
| 65 | } |
| 66 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 67 | static gboolean |
| 68 | draw_window(void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 69 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 70 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 71 | cairo_surface_t *surface; |
| 72 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 73 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 74 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 75 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 76 | const static char title[] = "Wayland First Post"; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 77 | struct buffer *buffer; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 78 | int width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 79 | |
| 80 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 81 | window->width + window->margin * 2, |
| 82 | window->height + window->margin * 2); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 83 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 84 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 85 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 86 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 87 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 88 | cr = cairo_create(surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 89 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 90 | cairo_translate(cr, window->margin + 7, window->margin + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 91 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 92 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 8c304f6 | 2008-11-10 10:46:53 -0500 | [diff] [blame] | 93 | rounded_rect(cr, 0, 0, window->width, window->height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 94 | cairo_fill(cr); |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 95 | blur_surface(surface, 24 + radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 96 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 97 | cairo_translate(cr, -7, -5); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 98 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 99 | rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius); |
| 100 | cairo_set_source(cr, outline); |
| 101 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 102 | 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] | 103 | cairo_set_source(cr, bright); |
| 104 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 105 | 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] | 106 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 107 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 108 | |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 109 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1); |
| 110 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 111 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4); |
| 112 | cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 113 | cairo_set_source(cr, gradient); |
| 114 | cairo_fill(cr); |
| 115 | cairo_pattern_destroy(gradient); |
| 116 | |
| 117 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 118 | cairo_move_to(cr, 10, 50); |
| 119 | cairo_line_to(cr, window->width - 10, 50); |
| 120 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 121 | cairo_line_to(cr, 10, window->height - 10); |
| 122 | cairo_close_path(cr); |
| 123 | cairo_set_source(cr, dim); |
| 124 | cairo_stroke(cr); |
| 125 | |
| 126 | cairo_move_to(cr, 11, 51); |
| 127 | cairo_line_to(cr, window->width - 10, 51); |
| 128 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 129 | cairo_line_to(cr, 11, window->height - 10); |
| 130 | cairo_close_path(cr); |
| 131 | cairo_set_source(cr, bright); |
| 132 | cairo_stroke(cr); |
| 133 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 134 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 135 | cairo_set_font_size(cr, 14); |
| 136 | cairo_text_extents(cr, title, &extents); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 137 | 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] | 138 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 139 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 140 | cairo_set_line_width (cr, 4); |
| 141 | cairo_text_path(cr, title); |
| 142 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 143 | cairo_stroke_preserve(cr); |
| 144 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 145 | cairo_fill(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 146 | cairo_destroy(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 147 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 148 | window->buffer = buffer_create_from_cairo_surface(window->fd, surface); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 149 | cairo_surface_destroy(surface); |
| 150 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 151 | wl_surface_attach(window->surface, |
| 152 | window->buffer->name, |
| 153 | window->buffer->width, |
| 154 | window->buffer->height, |
| 155 | window->buffer->stride); |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 156 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 157 | width = window->width - 20; |
| 158 | height = window->height - 60; |
| 159 | buffer = buffer_create(window->fd, width, height, (width * 4 + 15) & ~15); |
| 160 | window->egl_buffer = buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 161 | window->egl_surface = eglCreateSurfaceForName(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 162 | window->config, buffer->name, |
| 163 | buffer->width, buffer->height, |
| 164 | buffer->stride, NULL); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 165 | if (!eglMakeCurrent(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 166 | window->egl_surface, window->egl_surface, window->context)) |
| 167 | die("failed to make context current\n"); |
| 168 | |
| 169 | glViewport(0, 0, width, height); |
| 170 | |
| 171 | if (window->gears == NULL) |
| 172 | window->gears = gears_create(0, 0, 0, 0.92); |
| 173 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 174 | window->resized = 0; |
| 175 | |
| 176 | return FALSE; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static gboolean |
| 181 | animate_gears(gpointer data) |
| 182 | { |
| 183 | struct window *window = data; |
| 184 | struct buffer *buffer; |
| 185 | static uint32_t key; |
| 186 | |
| 187 | /* Right now, resizing the window from the animation is fine, |
| 188 | * since the window drawing code is so slow, but once we |
| 189 | * implement faster resizing, this will show lag between |
| 190 | * pointer motion and window size even if resizing is fast. |
| 191 | * We need to keep processing motion events and posting new |
| 192 | * frames as fast as possible so when the server composites |
| 193 | * the next frame it will have the most recent size possible. |
| 194 | * In that case, we need the two ack protocol, where the first |
| 195 | * ack signals that the server got the request so we can free |
| 196 | * the buffer, to prevent us from allocating a ton of buffer |
| 197 | * that will never be displayed. */ |
| 198 | if (window->resized) |
| 199 | draw_window(window); |
| 200 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 201 | gears_draw(window->gears, window->gears_angle); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 202 | |
| 203 | buffer = window->egl_buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 204 | wl_surface_copy(window->surface, |
| 205 | 10 + window->margin, 50 + window->margin, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 206 | buffer->name, buffer->stride, |
| 207 | 0, 0, buffer->width, buffer->height); |
| 208 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 209 | /* Shouldn't need to do this here, but without proper commit |
| 210 | * support in the server, doing this before rendering the |
| 211 | * gears show the window briefly before it's fully |
| 212 | * rendered. */ |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 213 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 214 | wl_surface_map(window->surface, |
| 215 | window->x - window->margin, |
| 216 | window->y - window->margin, |
| 217 | window->width + 2 * window->margin, |
| 218 | window->height + 2 * window->margin); |
| 219 | |
| 220 | wl_display_commit(window->display, key++); |
| 221 | window->gears_angle += 1; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 222 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 223 | return FALSE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | enum window_state { |
| 227 | WINDOW_STABLE, |
| 228 | WINDOW_MOVING, |
| 229 | WINDOW_RESIZING_UPPER_LEFT, |
| 230 | WINDOW_RESIZING_UPPER_RIGHT, |
| 231 | WINDOW_RESIZING_LOWER_LEFT, |
| 232 | WINDOW_RESIZING_LOWER_RIGHT |
| 233 | }; |
| 234 | |
| 235 | enum location { |
| 236 | LOCATION_INTERIOR, |
| 237 | LOCATION_UPPER_LEFT, |
| 238 | LOCATION_UPPER_RIGHT, |
| 239 | LOCATION_LOWER_LEFT, |
| 240 | LOCATION_LOWER_RIGHT, |
| 241 | LOCATION_OUTSIDE |
| 242 | }; |
| 243 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 244 | static void |
| 245 | event_handler(struct wl_display *display, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 246 | uint32_t object, uint32_t opcode, |
| 247 | uint32_t size, uint32_t *p, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 248 | { |
| 249 | struct window *window = data; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 250 | int location; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 251 | int grip_size = 16; |
| 252 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 253 | /* FIXME: Object ID 1 is the display, for anything else we |
| 254 | * assume it's an input device. */ |
| 255 | if (object == 1 && opcode == 3) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 256 | /* The acknowledge event means that the server |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 257 | * processed our last commit request and we can now |
| 258 | * safely free the buffer. */ |
| 259 | if (window->buffer != NULL) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 260 | buffer_destroy(window->buffer, window->fd); |
| 261 | window->buffer = NULL; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 262 | } |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 263 | |
| 264 | g_idle_add(animate_gears, window); |
| 265 | |
| 266 | } else if (object == 1) { |
| 267 | fprintf(stderr, "unexpected event from display: %d\n", |
| 268 | opcode); |
| 269 | exit(-1); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 270 | } else if (opcode == 0) { |
| 271 | int x = p[0], y = p[1]; |
| 272 | |
| 273 | window->last_x = x; |
| 274 | window->last_y = y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 275 | switch (window->state) { |
| 276 | case WINDOW_MOVING: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 277 | window->x = window->drag_x + x; |
| 278 | window->y = window->drag_y + y; |
| 279 | wl_surface_map(window->surface, |
| 280 | window->x - window->margin, |
| 281 | window->y - window->margin, |
| 282 | window->width + 2 * window->margin, |
| 283 | window->height + 2 * window->margin); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 284 | /* FIXME: We should do this here: |
| 285 | * |
| 286 | * wl_display_commit(window->display, 1); |
| 287 | * |
| 288 | * to make sure the server processes the move, |
| 289 | * but that'll mess with the other commit from |
| 290 | * animate_gears with the current server |
| 291 | * implementation. Since the current server |
| 292 | * doesn't rely on commit anyway yet, we can |
| 293 | * just forget about it for now. */ |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 294 | break; |
| 295 | case WINDOW_RESIZING_LOWER_RIGHT: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 296 | window->width = window->drag_x + x; |
| 297 | window->height = window->drag_y + y; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 298 | if (window->width < 400) |
| 299 | window->width = 400; |
| 300 | if (window->height < 400) |
| 301 | window->height = 400; |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 302 | window->resized = 1; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 303 | break; |
| 304 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 305 | } else if (opcode == 1) { |
| 306 | int button = p[0], state = p[1]; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 307 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 308 | if (window->x + window->width - grip_size <= window->last_x && |
| 309 | window->last_x < window->x + window->width && |
| 310 | window->y + window->height - grip_size <= window->last_y && |
| 311 | window->last_y < window->y + window->height) { |
| 312 | location = LOCATION_LOWER_RIGHT; |
| 313 | } else if (window->x <= window->last_x && |
| 314 | window->last_x < window->x + window->width && |
| 315 | window->y <= window->last_y && |
| 316 | window->last_y < window->y + window->height) { |
| 317 | location = LOCATION_INTERIOR; |
| 318 | } else { |
| 319 | location = LOCATION_OUTSIDE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 320 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 321 | |
| 322 | if (button == 0 && state == 1) { |
| 323 | switch (location) { |
| 324 | case LOCATION_INTERIOR: |
| 325 | window->drag_x = window->x - window->last_x; |
| 326 | window->drag_y = window->y - window->last_y; |
| 327 | window->state = WINDOW_MOVING; |
| 328 | break; |
| 329 | case LOCATION_LOWER_RIGHT: |
| 330 | window->drag_x = window->width - window->last_x; |
| 331 | window->drag_y = window->height - window->last_y; |
| 332 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 333 | break; |
| 334 | default: |
| 335 | window->state = WINDOW_STABLE; |
| 336 | break; |
| 337 | } |
| 338 | } else if (button == 0 && state == 0) { |
| 339 | window->state = WINDOW_STABLE; |
| 340 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 344 | static struct window * |
| 345 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 346 | { |
| 347 | EGLint major, minor, count; |
| 348 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 349 | struct window *window; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 350 | const GLfloat red = 0, green = 0, blue = 0, alpha = 0.92; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 351 | |
| 352 | window = malloc(sizeof *window); |
| 353 | if (window == NULL) |
| 354 | return NULL; |
| 355 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 356 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 357 | window->display = display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 358 | window->surface = wl_display_create_surface(display); |
| 359 | window->x = 200; |
| 360 | window->y = 200; |
| 361 | window->width = 450; |
| 362 | window->height = 500; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 363 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 364 | window->state = WINDOW_STABLE; |
| 365 | window->fd = fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 366 | window->background = cairo_pattern_create_rgba (red, green, blue, alpha); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 367 | window->resized = 1; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 368 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 369 | window->egl_display = eglCreateDisplayNative("/dev/dri/card0", "i965"); |
| 370 | if (window->egl_display == NULL) |
| 371 | die("failed to create egl display\n"); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 372 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 373 | if (!eglInitialize(window->egl_display, &major, &minor)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 374 | die("failed to initialize display\n"); |
| 375 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 376 | if (!eglGetConfigs(window->egl_display, configs, 64, &count)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 377 | die("failed to get configs\n"); |
| 378 | |
| 379 | window->config = configs[24]; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 380 | window->context = eglCreateContext(window->egl_display, window->config, NULL, NULL); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 381 | if (window->context == NULL) |
| 382 | die("failed to create context\n"); |
| 383 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame^] | 384 | animate_gears(window); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 385 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 386 | return window; |
| 387 | } |
| 388 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 389 | int main(int argc, char *argv[]) |
| 390 | { |
| 391 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 392 | int fd; |
| 393 | struct window *window; |
| 394 | GMainLoop *loop; |
| 395 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 396 | |
| 397 | fd = open(gem_device, O_RDWR); |
| 398 | if (fd < 0) { |
| 399 | fprintf(stderr, "drm open failed: %m\n"); |
| 400 | return -1; |
| 401 | } |
| 402 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 403 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 404 | if (display == NULL) { |
| 405 | fprintf(stderr, "failed to create display: %m\n"); |
| 406 | return -1; |
| 407 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 408 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 409 | loop = g_main_loop_new(NULL, FALSE); |
| 410 | source = wayland_source_new(display); |
| 411 | g_source_attach(source, NULL); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 412 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 413 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 414 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 415 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 416 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 417 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |