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 | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame^] | 34 | #include <linux/input.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 35 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 36 | #include "wayland-glib.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 37 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 38 | #include "cairo-util.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 39 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 40 | #include "window.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 41 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 42 | struct window { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 43 | struct wl_display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 44 | struct wl_surface *surface; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 45 | const char *title; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 46 | int x, y, width, height; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 47 | int minimum_width, minimum_height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 48 | int margin; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 49 | int drag_x, drag_y, last_x, last_y; |
| 50 | int state; |
| 51 | uint32_t name; |
| 52 | int fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 53 | |
| 54 | struct buffer *buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 55 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 56 | window_resize_handler_t resize_handler; |
| 57 | window_frame_handler_t frame_handler; |
| 58 | window_acknowledge_handler_t acknowledge_handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 59 | window_key_handler_t key_handler; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 60 | void *user_data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 63 | static void |
| 64 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 65 | { |
| 66 | cairo_move_to(cr, x0, y0 + radius); |
| 67 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 68 | cairo_line_to(cr, x1 - radius, y0); |
| 69 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 70 | cairo_line_to(cr, x1, y1 - radius); |
| 71 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 72 | cairo_line_to(cr, x0 + radius, y1); |
| 73 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 74 | cairo_close_path(cr); |
| 75 | } |
| 76 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 77 | void |
| 78 | window_draw(struct window *window) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 79 | { |
| 80 | cairo_surface_t *surface; |
| 81 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 82 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 83 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 84 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 85 | |
| 86 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 87 | window->width + window->margin * 2, |
| 88 | window->height + window->margin * 2); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 89 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 90 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 91 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 92 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 93 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 94 | cr = cairo_create(surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 95 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 96 | cairo_translate(cr, window->margin + 7, window->margin + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 97 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 98 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 8c304f6 | 2008-11-10 10:46:53 -0500 | [diff] [blame] | 99 | rounded_rect(cr, 0, 0, window->width, window->height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 100 | cairo_fill(cr); |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 101 | blur_surface(surface, 24 + radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 102 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 103 | cairo_translate(cr, -7, -5); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 104 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 105 | rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius); |
| 106 | cairo_set_source(cr, outline); |
| 107 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 108 | 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] | 109 | cairo_set_source(cr, bright); |
| 110 | cairo_stroke(cr); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 111 | 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] | 112 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 113 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 114 | |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 115 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius - 1); |
| 116 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 117 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4); |
| 118 | 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] | 119 | cairo_set_source(cr, gradient); |
| 120 | cairo_fill(cr); |
| 121 | cairo_pattern_destroy(gradient); |
| 122 | |
| 123 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 124 | cairo_move_to(cr, 10, 50); |
| 125 | cairo_line_to(cr, window->width - 10, 50); |
| 126 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 127 | cairo_line_to(cr, 10, window->height - 10); |
| 128 | cairo_close_path(cr); |
| 129 | cairo_set_source(cr, dim); |
| 130 | cairo_stroke(cr); |
| 131 | |
| 132 | cairo_move_to(cr, 11, 51); |
| 133 | cairo_line_to(cr, window->width - 10, 51); |
| 134 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 135 | cairo_line_to(cr, 11, window->height - 10); |
| 136 | cairo_close_path(cr); |
| 137 | cairo_set_source(cr, bright); |
| 138 | cairo_stroke(cr); |
| 139 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 140 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 141 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 142 | cairo_text_extents(cr, window->title, &extents); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 143 | 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] | 144 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 145 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 146 | cairo_set_line_width (cr, 4); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 147 | cairo_text_path(cr, window->title); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 148 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 149 | cairo_stroke_preserve(cr); |
| 150 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 151 | cairo_fill(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 152 | cairo_destroy(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 153 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 154 | window->buffer = buffer_create_from_cairo_surface(window->fd, surface); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 155 | cairo_surface_destroy(surface); |
| 156 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 157 | wl_surface_attach(window->surface, |
| 158 | window->buffer->name, |
| 159 | window->buffer->width, |
| 160 | window->buffer->height, |
| 161 | window->buffer->stride); |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 162 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 163 | wl_surface_map(window->surface, |
| 164 | window->x - window->margin, |
| 165 | window->y - window->margin, |
| 166 | window->width + 2 * window->margin, |
| 167 | window->height + 2 * window->margin); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 170 | enum window_state { |
| 171 | WINDOW_STABLE, |
| 172 | WINDOW_MOVING, |
| 173 | WINDOW_RESIZING_UPPER_LEFT, |
| 174 | WINDOW_RESIZING_UPPER_RIGHT, |
| 175 | WINDOW_RESIZING_LOWER_LEFT, |
| 176 | WINDOW_RESIZING_LOWER_RIGHT |
| 177 | }; |
| 178 | |
| 179 | enum location { |
| 180 | LOCATION_INTERIOR, |
| 181 | LOCATION_UPPER_LEFT, |
| 182 | LOCATION_UPPER_RIGHT, |
| 183 | LOCATION_LOWER_LEFT, |
| 184 | LOCATION_LOWER_RIGHT, |
| 185 | LOCATION_OUTSIDE |
| 186 | }; |
| 187 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 188 | static void |
| 189 | event_handler(struct wl_display *display, |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 190 | uint32_t object, uint32_t opcode, |
| 191 | uint32_t size, uint32_t *p, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 192 | { |
| 193 | struct window *window = data; |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 194 | struct rectangle rectangle; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 195 | int location; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 196 | int grip_size = 16; |
| 197 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 198 | /* FIXME: Object ID 1 is the display, for anything else we |
| 199 | * assume it's an input device. */ |
| 200 | if (object == 1 && opcode == 3) { |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 201 | uint32_t key = p[0]; |
| 202 | |
| 203 | /* Ignore acknowledge events for window move requests. */ |
| 204 | if (key != 0) |
| 205 | return; |
| 206 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 207 | /* The acknowledge event means that the server |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 208 | * processed our last commit request and we can now |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 209 | * safely free the old window buffer if we resized and |
| 210 | * render the next frame into our back buffer.. */ |
| 211 | |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 212 | if (window->buffer != NULL) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 213 | buffer_destroy(window->buffer, window->fd); |
| 214 | window->buffer = NULL; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 215 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 216 | if (window->acknowledge_handler) |
| 217 | (*window->acknowledge_handler)(window, key, |
| 218 | window->user_data); |
| 219 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 220 | } else if (object == 1 && opcode == 4) { |
| 221 | /* The frame event means that the previous frame was |
| 222 | * composited, and we can now send the request to copy |
| 223 | * the frame we've rendered in the mean time into the |
| 224 | * servers surface buffer. */ |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 225 | if (window->frame_handler) |
| 226 | (*window->frame_handler)(window, p[0], p[1], |
| 227 | window->user_data); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 228 | } else if (object == 1) { |
| 229 | fprintf(stderr, "unexpected event from display: %d\n", |
| 230 | opcode); |
| 231 | exit(-1); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 232 | } else if (opcode == 0) { |
| 233 | int x = p[0], y = p[1]; |
| 234 | |
| 235 | window->last_x = x; |
| 236 | window->last_y = y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 237 | switch (window->state) { |
| 238 | case WINDOW_MOVING: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 239 | window->x = window->drag_x + x; |
| 240 | window->y = window->drag_y + y; |
| 241 | wl_surface_map(window->surface, |
| 242 | window->x - window->margin, |
| 243 | window->y - window->margin, |
| 244 | window->width + 2 * window->margin, |
| 245 | window->height + 2 * window->margin); |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 246 | wl_display_commit(window->display, 1); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 247 | break; |
| 248 | case WINDOW_RESIZING_LOWER_RIGHT: |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 249 | window->width = window->drag_x + x; |
| 250 | window->height = window->drag_y + y; |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 251 | |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 252 | window_get_child_rectangle(window, &rectangle); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 253 | if (window->resize_handler) |
| 254 | (*window->resize_handler)(window, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 255 | window->user_data); |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 256 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 257 | break; |
| 258 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 259 | } else if (opcode == 1) { |
| 260 | int button = p[0], state = p[1]; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 261 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 262 | if (window->x + window->width - grip_size <= window->last_x && |
| 263 | window->last_x < window->x + window->width && |
| 264 | window->y + window->height - grip_size <= window->last_y && |
| 265 | window->last_y < window->y + window->height) { |
| 266 | location = LOCATION_LOWER_RIGHT; |
| 267 | } else if (window->x <= window->last_x && |
| 268 | window->last_x < window->x + window->width && |
| 269 | window->y <= window->last_y && |
| 270 | window->last_y < window->y + window->height) { |
| 271 | location = LOCATION_INTERIOR; |
| 272 | } else { |
| 273 | location = LOCATION_OUTSIDE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 274 | } |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 275 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame^] | 276 | if (button == BTN_LEFT && state == 1) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 277 | switch (location) { |
| 278 | case LOCATION_INTERIOR: |
| 279 | window->drag_x = window->x - window->last_x; |
| 280 | window->drag_y = window->y - window->last_y; |
| 281 | window->state = WINDOW_MOVING; |
| 282 | break; |
| 283 | case LOCATION_LOWER_RIGHT: |
| 284 | window->drag_x = window->width - window->last_x; |
| 285 | window->drag_y = window->height - window->last_y; |
| 286 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 287 | break; |
| 288 | default: |
| 289 | window->state = WINDOW_STABLE; |
| 290 | break; |
| 291 | } |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame^] | 292 | } else if (button == BTN_LEFT && state == 0) { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 293 | window->state = WINDOW_STABLE; |
| 294 | } |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 295 | } else if (opcode == 2) { |
| 296 | if (window->key_handler) |
| 297 | (*window->key_handler)(window, p[0], p[1], |
| 298 | window->user_data); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 302 | void |
| 303 | window_get_child_rectangle(struct window *window, |
| 304 | struct rectangle *rectangle) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 305 | { |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 306 | rectangle->x = 10; |
| 307 | rectangle->y = 50; |
| 308 | rectangle->width = window->width - 20; |
| 309 | rectangle->height = window->height - 60; |
| 310 | } |
| 311 | |
| 312 | void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 313 | window_set_child_size(struct window *window, |
| 314 | struct rectangle *rectangle) |
| 315 | { |
| 316 | window->width = rectangle->width + 20; |
| 317 | window->height = rectangle->height + 60; |
| 318 | } |
| 319 | |
| 320 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 321 | window_copy(struct window *window, |
| 322 | struct rectangle *rectangle, |
| 323 | uint32_t name, uint32_t stride) |
| 324 | { |
| 325 | wl_surface_copy(window->surface, |
| 326 | window->margin + rectangle->x, |
| 327 | window->margin + rectangle->y, |
| 328 | name, stride, |
| 329 | 0, 0, rectangle->width, rectangle->height); |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | window_set_resize_handler(struct window *window, |
| 334 | window_resize_handler_t handler, void *data) |
| 335 | { |
| 336 | window->resize_handler = handler; |
| 337 | window->user_data = data; |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | window_set_frame_handler(struct window *window, |
| 342 | window_frame_handler_t handler, void *data) |
| 343 | { |
| 344 | window->frame_handler = handler; |
| 345 | window->user_data = data; |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | window_set_acknowledge_handler(struct window *window, |
| 350 | window_acknowledge_handler_t handler, void *data) |
| 351 | { |
| 352 | window->acknowledge_handler = handler; |
| 353 | window->user_data = data; |
| 354 | } |
| 355 | |
| 356 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 357 | window_set_key_handler(struct window *window, |
| 358 | window_key_handler_t handler, void *data) |
| 359 | { |
| 360 | window->key_handler = handler; |
| 361 | window->user_data = data; |
| 362 | } |
| 363 | |
| 364 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 365 | window_set_minimum_size(struct window *window, uint32_t width, int32_t height) |
| 366 | { |
| 367 | window->minimum_width = width; |
| 368 | window->minimum_height = height; |
| 369 | } |
| 370 | |
| 371 | struct window * |
| 372 | window_create(struct wl_display *display, int fd, |
| 373 | const char *title, |
| 374 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 375 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 376 | struct window *window; |
| 377 | |
| 378 | window = malloc(sizeof *window); |
| 379 | if (window == NULL) |
| 380 | return NULL; |
| 381 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 382 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 383 | window->display = display; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 384 | window->title = strdup(title); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 385 | window->surface = wl_display_create_surface(display); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 386 | window->x = x; |
| 387 | window->y = y; |
| 388 | window->minimum_width = 100; |
| 389 | window->minimum_height = 100; |
| 390 | window->width = width; |
| 391 | window->height = height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 392 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 393 | window->state = WINDOW_STABLE; |
| 394 | window->fd = fd; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 395 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 396 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 397 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 398 | return window; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 399 | } |