Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <math.h> |
| 30 | #include <time.h> |
| 31 | #include <cairo.h> |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 32 | #include <glib.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 33 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 34 | #include <GL/gl.h> |
| 35 | #include <eagle.h> |
| 36 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 37 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 38 | #include "wayland-glib.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 39 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 40 | #include "gears.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 41 | #include "cairo-util.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 42 | |
| 43 | static const char gem_device[] = "/dev/dri/card0"; |
| 44 | static const char socket_name[] = "\0wayland"; |
| 45 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 46 | static void die(const char *msg) |
| 47 | { |
| 48 | fprintf(stderr, "%s", msg); |
| 49 | exit(EXIT_FAILURE); |
| 50 | } |
| 51 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 52 | struct window { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 53 | struct wl_display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 54 | struct wl_surface *surface; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 55 | int x, y, width, height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 56 | int margin; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 57 | int drag_x, drag_y, last_x, last_y; |
| 58 | int state; |
| 59 | uint32_t name; |
| 60 | int fd; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 61 | int resized; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 62 | cairo_pattern_t *background; |
| 63 | |
| 64 | struct buffer *buffer; |
| 65 | struct buffer *egl_buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 66 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 67 | GLfloat gears_angle; |
| 68 | struct gears *gears; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 69 | EGLDisplay egl_display; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 70 | EGLContext context; |
| 71 | EGLConfig config; |
| 72 | EGLSurface egl_surface; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 75 | static void |
| 76 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 77 | { |
| 78 | cairo_move_to(cr, x0, y0 + radius); |
| 79 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 80 | cairo_line_to(cr, x1 - radius, y0); |
| 81 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 82 | cairo_line_to(cr, x1, y1 - radius); |
| 83 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 84 | cairo_line_to(cr, x0 + radius, y1); |
| 85 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 86 | cairo_close_path(cr); |
| 87 | } |
| 88 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 89 | static gboolean |
| 90 | draw_window(void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 91 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 92 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 93 | cairo_surface_t *surface; |
| 94 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 95 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 96 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 97 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 98 | const static char title[] = "Wayland First Post"; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 99 | struct buffer *buffer; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 100 | int width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 101 | |
| 102 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 103 | window->width + window->margin * 2, |
| 104 | window->height + window->margin * 2); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 105 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 106 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 107 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 108 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 109 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 110 | cr = cairo_create(surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 111 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 112 | cairo_translate(cr, window->margin + 7, window->margin + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 113 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 114 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 8c304f6 | 2008-11-10 10:46:53 -0500 | [diff] [blame] | 115 | rounded_rect(cr, 0, 0, window->width, window->height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 116 | cairo_fill(cr); |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 117 | blur_surface(surface, 24 + radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 118 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 119 | cairo_translate(cr, -7, -5); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 120 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 121 | rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius); |
| 122 | cairo_set_source(cr, outline); |
| 123 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 124 | 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] | 125 | cairo_set_source(cr, bright); |
| 126 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 127 | 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] | 128 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 129 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 130 | |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 131 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1); |
| 132 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 133 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4); |
| 134 | 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] | 135 | cairo_set_source(cr, gradient); |
| 136 | cairo_fill(cr); |
| 137 | cairo_pattern_destroy(gradient); |
| 138 | |
| 139 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 140 | cairo_move_to(cr, 10, 50); |
| 141 | cairo_line_to(cr, window->width - 10, 50); |
| 142 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 143 | cairo_line_to(cr, 10, window->height - 10); |
| 144 | cairo_close_path(cr); |
| 145 | cairo_set_source(cr, dim); |
| 146 | cairo_stroke(cr); |
| 147 | |
| 148 | cairo_move_to(cr, 11, 51); |
| 149 | cairo_line_to(cr, window->width - 10, 51); |
| 150 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 151 | cairo_line_to(cr, 11, window->height - 10); |
| 152 | cairo_close_path(cr); |
| 153 | cairo_set_source(cr, bright); |
| 154 | cairo_stroke(cr); |
| 155 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 156 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 157 | cairo_set_font_size(cr, 14); |
| 158 | cairo_text_extents(cr, title, &extents); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 159 | 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] | 160 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 161 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 162 | cairo_set_line_width (cr, 4); |
| 163 | cairo_text_path(cr, title); |
| 164 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 165 | cairo_stroke_preserve(cr); |
| 166 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 167 | cairo_fill(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 168 | cairo_destroy(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 169 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 170 | window->buffer = buffer_create_from_cairo_surface(window->fd, surface); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 171 | cairo_surface_destroy(surface); |
| 172 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 173 | wl_surface_attach(window->surface, |
| 174 | window->buffer->name, |
| 175 | window->buffer->width, |
| 176 | window->buffer->height, |
| 177 | window->buffer->stride); |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 178 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 179 | wl_surface_map(window->surface, |
| 180 | window->x - window->margin, |
| 181 | window->y - window->margin, |
| 182 | window->width + 2 * window->margin, |
| 183 | window->height + 2 * window->margin); |
| 184 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 185 | width = window->width - 20; |
| 186 | height = window->height - 60; |
| 187 | buffer = buffer_create(window->fd, width, height, (width * 4 + 15) & ~15); |
| 188 | window->egl_buffer = buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 189 | window->egl_surface = eglCreateSurfaceForName(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 190 | window->config, buffer->name, |
| 191 | buffer->width, buffer->height, |
| 192 | buffer->stride, NULL); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 193 | if (!eglMakeCurrent(window->egl_display, |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 194 | window->egl_surface, window->egl_surface, window->context)) |
| 195 | die("failed to make context current\n"); |
| 196 | |
| 197 | glViewport(0, 0, width, height); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 198 | window->resized = 0; |
| 199 | |
| 200 | return FALSE; |
| 201 | } |
| 202 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 203 | enum window_state { |
| 204 | WINDOW_STABLE, |
| 205 | WINDOW_MOVING, |
| 206 | WINDOW_RESIZING_UPPER_LEFT, |
| 207 | WINDOW_RESIZING_UPPER_RIGHT, |
| 208 | WINDOW_RESIZING_LOWER_LEFT, |
| 209 | WINDOW_RESIZING_LOWER_RIGHT |
| 210 | }; |
| 211 | |
| 212 | enum location { |
| 213 | LOCATION_INTERIOR, |
| 214 | LOCATION_UPPER_LEFT, |
| 215 | LOCATION_UPPER_RIGHT, |
| 216 | LOCATION_LOWER_LEFT, |
| 217 | LOCATION_LOWER_RIGHT, |
| 218 | LOCATION_OUTSIDE |
| 219 | }; |
| 220 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 221 | static int |
| 222 | update_gears(void *data) |
| 223 | { |
| 224 | struct window *window = data; |
| 225 | |
| 226 | if (window->resized) |
| 227 | draw_window(window); |
| 228 | gears_draw(window->gears, window->gears_angle); |
| 229 | |
| 230 | return FALSE; |
| 231 | } |
| 232 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 233 | static void |
| 234 | event_handler(struct wl_display *display, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 235 | uint32_t object, uint32_t opcode, |
| 236 | uint32_t size, uint32_t *p, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 237 | { |
| 238 | struct window *window = data; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 239 | struct buffer *buffer; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 240 | int location; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 241 | int grip_size = 16; |
| 242 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 243 | /* FIXME: Object ID 1 is the display, for anything else we |
| 244 | * assume it's an input device. */ |
| 245 | if (object == 1 && opcode == 3) { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 246 | uint32_t key = p[0]; |
| 247 | |
| 248 | /* Ignore acknowledge events for window move requests. */ |
| 249 | if (key != 0) |
| 250 | return; |
| 251 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 252 | /* The acknowledge event means that the server |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 253 | * processed our last commit request and we can now |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 254 | * safely free the old window buffer if we resized and |
| 255 | * render the next frame into our back buffer.. */ |
| 256 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 257 | if (window->buffer != NULL) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 258 | buffer_destroy(window->buffer, window->fd); |
| 259 | window->buffer = NULL; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 260 | } |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 261 | g_idle_add(update_gears, window); |
| 262 | } else if (object == 1 && opcode == 4) { |
| 263 | /* The frame event means that the previous frame was |
| 264 | * composited, and we can now send the request to copy |
| 265 | * the frame we've rendered in the mean time into the |
| 266 | * servers surface buffer. */ |
| 267 | buffer = window->egl_buffer; |
| 268 | wl_surface_copy(window->surface, |
| 269 | 10 + window->margin, 50 + window->margin, |
| 270 | buffer->name, buffer->stride, |
| 271 | 0, 0, buffer->width, buffer->height); |
| 272 | wl_display_commit(window->display, 0); |
| 273 | window->gears_angle += 1; |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 274 | |
| 275 | } else if (object == 1) { |
| 276 | fprintf(stderr, "unexpected event from display: %d\n", |
| 277 | opcode); |
| 278 | exit(-1); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 279 | } else if (opcode == 0) { |
| 280 | int x = p[0], y = p[1]; |
| 281 | |
| 282 | window->last_x = x; |
| 283 | window->last_y = y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 284 | switch (window->state) { |
| 285 | case WINDOW_MOVING: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 286 | window->x = window->drag_x + x; |
| 287 | window->y = window->drag_y + y; |
| 288 | wl_surface_map(window->surface, |
| 289 | window->x - window->margin, |
| 290 | window->y - window->margin, |
| 291 | window->width + 2 * window->margin, |
| 292 | window->height + 2 * window->margin); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 293 | wl_display_commit(window->display, 1); |
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 | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 302 | |
| 303 | /* Right now, resizing the window from the |
| 304 | * per-frame callback is fine, since the |
| 305 | * window drawing code is so slow that we |
| 306 | * can't draw more than one window per frame |
| 307 | * anyway. However, once we implement faster |
| 308 | * resizing, this will show lag between |
| 309 | * pointer motion and window size even if |
| 310 | * resizing is fast. We need to keep |
| 311 | * processing motion events and posting new |
| 312 | * frames as fast as possible so when the |
| 313 | * server composites the next frame it will |
| 314 | * have the most recent size possible, like |
| 315 | * what we do for window moves. */ |
| 316 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 317 | window->resized = 1; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 318 | break; |
| 319 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 320 | } else if (opcode == 1) { |
| 321 | int button = p[0], state = p[1]; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 322 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 323 | if (window->x + window->width - grip_size <= window->last_x && |
| 324 | window->last_x < window->x + window->width && |
| 325 | window->y + window->height - grip_size <= window->last_y && |
| 326 | window->last_y < window->y + window->height) { |
| 327 | location = LOCATION_LOWER_RIGHT; |
| 328 | } else if (window->x <= window->last_x && |
| 329 | window->last_x < window->x + window->width && |
| 330 | window->y <= window->last_y && |
| 331 | window->last_y < window->y + window->height) { |
| 332 | location = LOCATION_INTERIOR; |
| 333 | } else { |
| 334 | location = LOCATION_OUTSIDE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 335 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 336 | |
| 337 | if (button == 0 && state == 1) { |
| 338 | switch (location) { |
| 339 | case LOCATION_INTERIOR: |
| 340 | window->drag_x = window->x - window->last_x; |
| 341 | window->drag_y = window->y - window->last_y; |
| 342 | window->state = WINDOW_MOVING; |
| 343 | break; |
| 344 | case LOCATION_LOWER_RIGHT: |
| 345 | window->drag_x = window->width - window->last_x; |
| 346 | window->drag_y = window->height - window->last_y; |
| 347 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 348 | break; |
| 349 | default: |
| 350 | window->state = WINDOW_STABLE; |
| 351 | break; |
| 352 | } |
| 353 | } else if (button == 0 && state == 0) { |
| 354 | window->state = WINDOW_STABLE; |
| 355 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 359 | static struct window * |
| 360 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 361 | { |
| 362 | EGLint major, minor, count; |
| 363 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 364 | struct window *window; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 365 | const GLfloat red = 0, green = 0, blue = 0, alpha = 0.92; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 366 | |
| 367 | window = malloc(sizeof *window); |
| 368 | if (window == NULL) |
| 369 | return NULL; |
| 370 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 371 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 372 | window->display = display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 373 | window->surface = wl_display_create_surface(display); |
| 374 | window->x = 200; |
| 375 | window->y = 200; |
| 376 | window->width = 450; |
| 377 | window->height = 500; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 378 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 379 | window->state = WINDOW_STABLE; |
| 380 | window->fd = fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 381 | window->background = cairo_pattern_create_rgba (red, green, blue, alpha); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 382 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 383 | window->egl_display = eglCreateDisplayNative("/dev/dri/card0", "i965"); |
| 384 | if (window->egl_display == NULL) |
| 385 | die("failed to create egl display\n"); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 386 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 387 | if (!eglInitialize(window->egl_display, &major, &minor)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 388 | die("failed to initialize display\n"); |
| 389 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 390 | if (!eglGetConfigs(window->egl_display, configs, 64, &count)) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 391 | die("failed to get configs\n"); |
| 392 | |
| 393 | window->config = configs[24]; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 394 | window->context = eglCreateContext(window->egl_display, window->config, NULL, NULL); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 395 | if (window->context == NULL) |
| 396 | die("failed to create context\n"); |
| 397 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 398 | draw_window(window); |
| 399 | window->gears = gears_create(0, 0, 0, 0.92); |
| 400 | gears_draw(window->gears, window->gears_angle); |
| 401 | window->gears_angle += 1; |
| 402 | wl_display_commit(window->display, 0); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 403 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 404 | return window; |
| 405 | } |
| 406 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 407 | int main(int argc, char *argv[]) |
| 408 | { |
| 409 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 410 | int fd; |
| 411 | struct window *window; |
| 412 | GMainLoop *loop; |
| 413 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 414 | |
| 415 | fd = open(gem_device, O_RDWR); |
| 416 | if (fd < 0) { |
| 417 | fprintf(stderr, "drm open failed: %m\n"); |
| 418 | return -1; |
| 419 | } |
| 420 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 421 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 422 | if (display == NULL) { |
| 423 | fprintf(stderr, "failed to create display: %m\n"); |
| 424 | return -1; |
| 425 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 426 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 427 | loop = g_main_loop_new(NULL, FALSE); |
| 428 | source = wayland_source_new(display); |
| 429 | g_source_attach(source, NULL); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 430 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 431 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 432 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 433 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 434 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 435 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |