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 | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 157 | wl_surface_map(window->surface, |
| 158 | window->x - window->margin, |
| 159 | window->y - window->margin, |
| 160 | window->width + 2 * window->margin, |
| 161 | window->height + 2 * window->margin); |
| 162 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 163 | width = window->width - 20; |
| 164 | height = window->height - 60; |
| 165 | buffer = buffer_create(window->fd, width, height, (width * 4 + 15) & ~15); |
| 166 | window->egl_buffer = buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 167 | window->egl_surface = eglCreateSurfaceForName(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 168 | window->config, buffer->name, |
| 169 | buffer->width, buffer->height, |
| 170 | buffer->stride, NULL); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 171 | if (!eglMakeCurrent(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 172 | window->egl_surface, window->egl_surface, window->context)) |
| 173 | die("failed to make context current\n"); |
| 174 | |
| 175 | glViewport(0, 0, width, height); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 176 | window->resized = 0; |
| 177 | |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 181 | enum window_state { |
| 182 | WINDOW_STABLE, |
| 183 | WINDOW_MOVING, |
| 184 | WINDOW_RESIZING_UPPER_LEFT, |
| 185 | WINDOW_RESIZING_UPPER_RIGHT, |
| 186 | WINDOW_RESIZING_LOWER_LEFT, |
| 187 | WINDOW_RESIZING_LOWER_RIGHT |
| 188 | }; |
| 189 | |
| 190 | enum location { |
| 191 | LOCATION_INTERIOR, |
| 192 | LOCATION_UPPER_LEFT, |
| 193 | LOCATION_UPPER_RIGHT, |
| 194 | LOCATION_LOWER_LEFT, |
| 195 | LOCATION_LOWER_RIGHT, |
| 196 | LOCATION_OUTSIDE |
| 197 | }; |
| 198 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 199 | static int |
| 200 | update_gears(void *data) |
| 201 | { |
| 202 | struct window *window = data; |
| 203 | |
| 204 | if (window->resized) |
| 205 | draw_window(window); |
| 206 | gears_draw(window->gears, window->gears_angle); |
| 207 | |
| 208 | return FALSE; |
| 209 | } |
| 210 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 211 | static void |
| 212 | event_handler(struct wl_display *display, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 213 | uint32_t object, uint32_t opcode, |
| 214 | uint32_t size, uint32_t *p, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 215 | { |
| 216 | struct window *window = data; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 217 | struct buffer *buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 218 | int location; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 219 | int grip_size = 16; |
| 220 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 221 | /* FIXME: Object ID 1 is the display, for anything else we |
| 222 | * assume it's an input device. */ |
| 223 | if (object == 1 && opcode == 3) { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 224 | uint32_t key = p[0]; |
| 225 | |
| 226 | /* Ignore acknowledge events for window move requests. */ |
| 227 | if (key != 0) |
| 228 | return; |
| 229 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 230 | /* The acknowledge event means that the server |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 231 | * processed our last commit request and we can now |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 232 | * safely free the old window buffer if we resized and |
| 233 | * render the next frame into our back buffer.. */ |
| 234 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 235 | if (window->buffer != NULL) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 236 | buffer_destroy(window->buffer, window->fd); |
| 237 | window->buffer = NULL; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 238 | } |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 239 | g_idle_add(update_gears, window); |
| 240 | } else if (object == 1 && opcode == 4) { |
| 241 | /* The frame event means that the previous frame was |
| 242 | * composited, and we can now send the request to copy |
| 243 | * the frame we've rendered in the mean time into the |
| 244 | * servers surface buffer. */ |
| 245 | buffer = window->egl_buffer; |
| 246 | wl_surface_copy(window->surface, |
| 247 | 10 + window->margin, 50 + window->margin, |
| 248 | buffer->name, buffer->stride, |
| 249 | 0, 0, buffer->width, buffer->height); |
| 250 | wl_display_commit(window->display, 0); |
| 251 | window->gears_angle += 1; |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 252 | |
| 253 | } else if (object == 1) { |
| 254 | fprintf(stderr, "unexpected event from display: %d\n", |
| 255 | opcode); |
| 256 | exit(-1); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 257 | } else if (opcode == 0) { |
| 258 | int x = p[0], y = p[1]; |
| 259 | |
| 260 | window->last_x = x; |
| 261 | window->last_y = y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 262 | switch (window->state) { |
| 263 | case WINDOW_MOVING: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 264 | window->x = window->drag_x + x; |
| 265 | window->y = window->drag_y + y; |
| 266 | wl_surface_map(window->surface, |
| 267 | window->x - window->margin, |
| 268 | window->y - window->margin, |
| 269 | window->width + 2 * window->margin, |
| 270 | window->height + 2 * window->margin); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 271 | wl_display_commit(window->display, 1); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 272 | break; |
| 273 | case WINDOW_RESIZING_LOWER_RIGHT: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 274 | window->width = window->drag_x + x; |
| 275 | window->height = window->drag_y + y; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 276 | if (window->width < 400) |
| 277 | window->width = 400; |
| 278 | if (window->height < 400) |
| 279 | window->height = 400; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 280 | |
| 281 | /* Right now, resizing the window from the |
| 282 | * per-frame callback is fine, since the |
| 283 | * window drawing code is so slow that we |
| 284 | * can't draw more than one window per frame |
| 285 | * anyway. However, once we implement faster |
| 286 | * resizing, this will show lag between |
| 287 | * pointer motion and window size even if |
| 288 | * resizing is fast. We need to keep |
| 289 | * processing motion events and posting new |
| 290 | * frames as fast as possible so when the |
| 291 | * server composites the next frame it will |
| 292 | * have the most recent size possible, like |
| 293 | * what we do for window moves. */ |
| 294 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 295 | window->resized = 1; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 296 | break; |
| 297 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 298 | } else if (opcode == 1) { |
| 299 | int button = p[0], state = p[1]; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 300 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 301 | if (window->x + window->width - grip_size <= window->last_x && |
| 302 | window->last_x < window->x + window->width && |
| 303 | window->y + window->height - grip_size <= window->last_y && |
| 304 | window->last_y < window->y + window->height) { |
| 305 | location = LOCATION_LOWER_RIGHT; |
| 306 | } else if (window->x <= window->last_x && |
| 307 | window->last_x < window->x + window->width && |
| 308 | window->y <= window->last_y && |
| 309 | window->last_y < window->y + window->height) { |
| 310 | location = LOCATION_INTERIOR; |
| 311 | } else { |
| 312 | location = LOCATION_OUTSIDE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 313 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 314 | |
| 315 | if (button == 0 && state == 1) { |
| 316 | switch (location) { |
| 317 | case LOCATION_INTERIOR: |
| 318 | window->drag_x = window->x - window->last_x; |
| 319 | window->drag_y = window->y - window->last_y; |
| 320 | window->state = WINDOW_MOVING; |
| 321 | break; |
| 322 | case LOCATION_LOWER_RIGHT: |
| 323 | window->drag_x = window->width - window->last_x; |
| 324 | window->drag_y = window->height - window->last_y; |
| 325 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 326 | break; |
| 327 | default: |
| 328 | window->state = WINDOW_STABLE; |
| 329 | break; |
| 330 | } |
| 331 | } else if (button == 0 && state == 0) { |
| 332 | window->state = WINDOW_STABLE; |
| 333 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 337 | static struct window * |
| 338 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 339 | { |
| 340 | EGLint major, minor, count; |
| 341 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 342 | struct window *window; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 343 | const GLfloat red = 0, green = 0, blue = 0, alpha = 0.92; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 344 | |
| 345 | window = malloc(sizeof *window); |
| 346 | if (window == NULL) |
| 347 | return NULL; |
| 348 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 349 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 350 | window->display = display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 351 | window->surface = wl_display_create_surface(display); |
| 352 | window->x = 200; |
| 353 | window->y = 200; |
| 354 | window->width = 450; |
| 355 | window->height = 500; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 356 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 357 | window->state = WINDOW_STABLE; |
| 358 | window->fd = fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 359 | window->background = cairo_pattern_create_rgba (red, green, blue, alpha); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 360 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 361 | window->egl_display = eglCreateDisplayNative("/dev/dri/card0", "i965"); |
| 362 | if (window->egl_display == NULL) |
| 363 | die("failed to create egl display\n"); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 364 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 365 | if (!eglInitialize(window->egl_display, &major, &minor)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 366 | die("failed to initialize display\n"); |
| 367 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 368 | if (!eglGetConfigs(window->egl_display, configs, 64, &count)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 369 | die("failed to get configs\n"); |
| 370 | |
| 371 | window->config = configs[24]; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 372 | window->context = eglCreateContext(window->egl_display, window->config, NULL, NULL); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 373 | if (window->context == NULL) |
| 374 | die("failed to create context\n"); |
| 375 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame^] | 376 | draw_window(window); |
| 377 | window->gears = gears_create(0, 0, 0, 0.92); |
| 378 | gears_draw(window->gears, window->gears_angle); |
| 379 | window->gears_angle += 1; |
| 380 | wl_display_commit(window->display, 0); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 381 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 382 | return window; |
| 383 | } |
| 384 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 385 | int main(int argc, char *argv[]) |
| 386 | { |
| 387 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 388 | int fd; |
| 389 | struct window *window; |
| 390 | GMainLoop *loop; |
| 391 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 392 | |
| 393 | fd = open(gem_device, O_RDWR); |
| 394 | if (fd < 0) { |
| 395 | fprintf(stderr, "drm open failed: %m\n"); |
| 396 | return -1; |
| 397 | } |
| 398 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 399 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 400 | if (display == NULL) { |
| 401 | fprintf(stderr, "failed to create display: %m\n"); |
| 402 | return -1; |
| 403 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 404 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 405 | loop = g_main_loop_new(NULL, FALSE); |
| 406 | source = wayland_source_new(display); |
| 407 | g_source_attach(source, NULL); |
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 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 410 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 411 | wl_display_set_event_handler(display, event_handler, window); |
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 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |