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 | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 44 | struct wl_compositor *compositor; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 45 | struct wl_surface *surface; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 46 | const char *title; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 47 | struct rectangle allocation, saved_allocation, screen_allocation; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 48 | int minimum_width, minimum_height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 49 | int margin; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 50 | int drag_x, drag_y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 51 | int state; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 52 | int fullscreen; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 53 | struct wl_input_device *grab_device; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 54 | uint32_t name; |
| 55 | int fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 56 | |
| 57 | struct buffer *buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 58 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 59 | window_resize_handler_t resize_handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 60 | window_key_handler_t key_handler; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 61 | void *user_data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 64 | static void |
| 65 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 66 | { |
| 67 | cairo_move_to(cr, x0, y0 + radius); |
| 68 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 69 | cairo_line_to(cr, x1 - radius, y0); |
| 70 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 71 | cairo_line_to(cr, x1, y1 - radius); |
| 72 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 73 | cairo_line_to(cr, x0 + radius, y1); |
| 74 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 75 | cairo_close_path(cr); |
| 76 | } |
| 77 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 78 | static void |
| 79 | window_draw_decorations(struct window *window) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 80 | { |
| 81 | cairo_surface_t *surface; |
| 82 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 83 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 84 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 85 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 86 | struct wl_visual *visual; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 87 | int width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 88 | |
| 89 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 90 | window->allocation.width, |
| 91 | window->allocation.height); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 92 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 93 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 94 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 95 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 96 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 97 | cr = cairo_create(surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 98 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 99 | width = window->allocation.width - window->margin * 2; |
| 100 | height = window->allocation.height - window->margin * 2; |
| 101 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 102 | cairo_translate(cr, window->margin + 7, window->margin + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 103 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 104 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 105 | rounded_rect(cr, 0, 0, width, height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 106 | cairo_fill(cr); |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 107 | blur_surface(surface, 24 + radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 108 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 109 | cairo_translate(cr, -7, -5); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 110 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 111 | rounded_rect(cr, 1, 1, width - 1, height - 1, radius); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 112 | cairo_set_source(cr, outline); |
| 113 | cairo_stroke(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 114 | rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 115 | cairo_set_source(cr, bright); |
| 116 | cairo_stroke(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 117 | rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 118 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 119 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 120 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 121 | rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 122 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 123 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4); |
| 124 | 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] | 125 | cairo_set_source(cr, gradient); |
| 126 | cairo_fill(cr); |
| 127 | cairo_pattern_destroy(gradient); |
| 128 | |
| 129 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 130 | cairo_move_to(cr, 10, 50); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 131 | cairo_line_to(cr, width - 10, 50); |
| 132 | cairo_line_to(cr, width - 10, height - 10); |
| 133 | cairo_line_to(cr, 10, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 134 | cairo_close_path(cr); |
| 135 | cairo_set_source(cr, dim); |
| 136 | cairo_stroke(cr); |
| 137 | |
| 138 | cairo_move_to(cr, 11, 51); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 139 | cairo_line_to(cr, width - 10, 51); |
| 140 | cairo_line_to(cr, width - 10, height - 10); |
| 141 | cairo_line_to(cr, 11, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 142 | cairo_close_path(cr); |
| 143 | cairo_set_source(cr, bright); |
| 144 | cairo_stroke(cr); |
| 145 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 146 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 147 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 148 | cairo_text_extents(cr, window->title, &extents); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 149 | cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 150 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 151 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 152 | cairo_set_line_width (cr, 4); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 153 | cairo_text_path(cr, window->title); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 154 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 155 | cairo_stroke_preserve(cr); |
| 156 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 157 | cairo_fill(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 158 | cairo_destroy(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 159 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 160 | window->buffer = buffer_create_from_cairo_surface(window->fd, surface); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 161 | cairo_surface_destroy(surface); |
| 162 | |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 163 | visual = wl_display_get_premultiplied_argb_visual(window->display); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 164 | wl_surface_attach(window->surface, |
| 165 | window->buffer->name, |
| 166 | window->buffer->width, |
| 167 | window->buffer->height, |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 168 | window->buffer->stride, |
| 169 | visual); |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 170 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 171 | wl_surface_map(window->surface, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 172 | window->allocation.x - window->margin, |
| 173 | window->allocation.y - window->margin, |
| 174 | window->allocation.width, |
| 175 | window->allocation.height); |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | window_draw_fullscreen(struct window *window) |
| 180 | { |
| 181 | struct wl_visual *visual; |
| 182 | int stride = window->allocation.width * 4; |
| 183 | |
| 184 | window->buffer = buffer_create(window->fd, |
| 185 | window->allocation.width, |
| 186 | window->allocation.height, |
| 187 | stride); |
| 188 | |
| 189 | visual = wl_display_get_premultiplied_argb_visual(window->display); |
| 190 | wl_surface_attach(window->surface, |
| 191 | window->buffer->name, |
| 192 | window->buffer->width, |
| 193 | window->buffer->height, |
| 194 | window->buffer->stride, |
| 195 | visual); |
| 196 | |
| 197 | wl_surface_map(window->surface, |
| 198 | window->allocation.x, |
| 199 | window->allocation.y, |
| 200 | window->allocation.width, |
| 201 | window->allocation.height); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | window_draw(struct window *window) |
| 206 | { |
| 207 | if (window->fullscreen) |
| 208 | window_draw_fullscreen(window); |
| 209 | else |
| 210 | window_draw_decorations(window); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 213 | static void |
| 214 | window_handle_acknowledge(void *data, |
| 215 | struct wl_compositor *compositor, |
| 216 | uint32_t key, uint32_t frame) |
| 217 | { |
| 218 | struct window *window = data; |
| 219 | |
| 220 | /* The acknowledge event means that the server |
| 221 | * processed our last commit request and we can now |
| 222 | * safely free the old window buffer if we resized and |
| 223 | * render the next frame into our back buffer.. */ |
| 224 | |
| 225 | if (key == 0 && window->buffer != NULL) { |
| 226 | buffer_destroy(window->buffer, window->fd); |
| 227 | window->buffer = NULL; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | window_handle_frame(void *data, |
| 233 | struct wl_compositor *compositor, |
| 234 | uint32_t frame, uint32_t timestamp) |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | static const struct wl_compositor_listener compositor_listener = { |
| 239 | window_handle_acknowledge, |
| 240 | window_handle_frame, |
| 241 | }; |
| 242 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 243 | enum window_state { |
| 244 | WINDOW_STABLE, |
| 245 | WINDOW_MOVING, |
| 246 | WINDOW_RESIZING_UPPER_LEFT, |
| 247 | WINDOW_RESIZING_UPPER_RIGHT, |
| 248 | WINDOW_RESIZING_LOWER_LEFT, |
| 249 | WINDOW_RESIZING_LOWER_RIGHT |
| 250 | }; |
| 251 | |
| 252 | enum location { |
| 253 | LOCATION_INTERIOR, |
| 254 | LOCATION_UPPER_LEFT, |
| 255 | LOCATION_UPPER_RIGHT, |
| 256 | LOCATION_LOWER_LEFT, |
| 257 | LOCATION_LOWER_RIGHT, |
| 258 | LOCATION_OUTSIDE |
| 259 | }; |
| 260 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 261 | static void |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 262 | window_handle_motion(void *data, struct wl_input_device *input_device, |
| 263 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 264 | { |
| 265 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 266 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 267 | switch (window->state) { |
| 268 | case WINDOW_MOVING: |
| 269 | if (window->fullscreen) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 270 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 271 | if (window->grab_device != input_device) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 272 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 273 | window->allocation.x = window->drag_x + x; |
| 274 | window->allocation.y = window->drag_y + y; |
| 275 | wl_surface_map(window->surface, |
| 276 | window->allocation.x - window->margin, |
| 277 | window->allocation.y - window->margin, |
| 278 | window->allocation.width, |
| 279 | window->allocation.height); |
| 280 | wl_compositor_commit(window->compositor, 1); |
| 281 | break; |
| 282 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 283 | if (window->fullscreen) |
| 284 | break; |
| 285 | if (window->grab_device != input_device) |
| 286 | break; |
| 287 | window->allocation.width = window->drag_x + x; |
| 288 | window->allocation.height = window->drag_y + y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 289 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 290 | if (window->resize_handler) |
| 291 | (*window->resize_handler)(window, |
| 292 | window->user_data); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 293 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 294 | break; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 298 | static void window_handle_button(void *data, struct wl_input_device *input_device, |
| 299 | uint32_t button, uint32_t state, |
| 300 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 301 | { |
| 302 | struct window *window = data; |
| 303 | int32_t left = window->allocation.x; |
| 304 | int32_t right = window->allocation.x + |
| 305 | window->allocation.width - window->margin * 2; |
| 306 | int32_t top = window->allocation.y; |
| 307 | int32_t bottom = window->allocation.y + |
| 308 | window->allocation.height - window->margin * 2; |
| 309 | int grip_size = 16, location; |
| 310 | |
| 311 | if (right - grip_size <= x && x < right && |
| 312 | bottom - grip_size <= y && y < bottom) { |
| 313 | location = LOCATION_LOWER_RIGHT; |
| 314 | } else if (left <= x && x < right && top <= y && y < bottom) { |
| 315 | location = LOCATION_INTERIOR; |
| 316 | } else { |
| 317 | location = LOCATION_OUTSIDE; |
| 318 | } |
| 319 | |
| 320 | if (button == BTN_LEFT && state == 1) { |
| 321 | switch (location) { |
| 322 | case LOCATION_INTERIOR: |
| 323 | window->drag_x = window->allocation.x - x; |
| 324 | window->drag_y = window->allocation.y - y; |
| 325 | window->state = WINDOW_MOVING; |
| 326 | window->grab_device = input_device; |
| 327 | break; |
| 328 | case LOCATION_LOWER_RIGHT: |
| 329 | window->drag_x = window->allocation.width - x; |
| 330 | window->drag_y = window->allocation.height - y; |
| 331 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 332 | window->grab_device = input_device; |
| 333 | break; |
| 334 | default: |
| 335 | window->state = WINDOW_STABLE; |
| 336 | break; |
| 337 | } |
| 338 | } else if (button == BTN_LEFT && |
| 339 | state == 0 && window->grab_device == input_device) { |
| 340 | window->state = WINDOW_STABLE; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static void |
| 345 | window_handle_key(void *data, struct wl_input_device *input_device, |
| 346 | uint32_t button, uint32_t state) |
| 347 | { |
| 348 | struct window *window = data; |
| 349 | |
| 350 | if (window->key_handler) |
| 351 | (*window->key_handler)(window, button, state, |
| 352 | window->user_data); |
| 353 | } |
| 354 | |
| 355 | static const struct wl_input_device_listener input_device_listener = { |
| 356 | window_handle_motion, |
| 357 | window_handle_button, |
| 358 | window_handle_key, |
| 359 | }; |
| 360 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 361 | void |
| 362 | window_get_child_rectangle(struct window *window, |
| 363 | struct rectangle *rectangle) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 364 | { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 365 | if (window->fullscreen) { |
| 366 | *rectangle = window->allocation; |
| 367 | } else { |
| 368 | rectangle->x = window->margin + 10; |
| 369 | rectangle->y = window->margin + 50; |
| 370 | rectangle->width = window->allocation.width - 20 - window->margin * 2; |
| 371 | rectangle->height = window->allocation.height - 60 - window->margin * 2; |
| 372 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 376 | window_set_child_size(struct window *window, |
| 377 | struct rectangle *rectangle) |
| 378 | { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 379 | if (!window->fullscreen) { |
| 380 | window->allocation.width = rectangle->width + 20 + window->margin * 2; |
| 381 | window->allocation.height = rectangle->height + 60 + window->margin * 2; |
| 382 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 386 | window_copy(struct window *window, |
| 387 | struct rectangle *rectangle, |
| 388 | uint32_t name, uint32_t stride) |
| 389 | { |
| 390 | wl_surface_copy(window->surface, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 391 | rectangle->x, |
| 392 | rectangle->y, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 393 | name, stride, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 394 | 0, 0, |
| 395 | rectangle->width, |
| 396 | rectangle->height); |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | window_set_fullscreen(struct window *window, int fullscreen) |
| 401 | { |
| 402 | window->fullscreen = fullscreen; |
| 403 | if (window->fullscreen) { |
| 404 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 405 | window->allocation = window->screen_allocation; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 406 | } else { |
| 407 | window->allocation = window->saved_allocation; |
| 408 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void |
| 412 | window_set_resize_handler(struct window *window, |
| 413 | window_resize_handler_t handler, void *data) |
| 414 | { |
| 415 | window->resize_handler = handler; |
| 416 | window->user_data = data; |
| 417 | } |
| 418 | |
| 419 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 420 | window_set_key_handler(struct window *window, |
| 421 | window_key_handler_t handler, void *data) |
| 422 | { |
| 423 | window->key_handler = handler; |
| 424 | window->user_data = data; |
| 425 | } |
| 426 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 427 | static void |
| 428 | window_handle_geometry(void *data, |
| 429 | struct wl_output *output, |
| 430 | int32_t width, int32_t height) |
| 431 | { |
| 432 | struct window *window = data; |
| 433 | |
| 434 | window->screen_allocation.x = 0; |
| 435 | window->screen_allocation.y = 0; |
| 436 | window->screen_allocation.width = width; |
| 437 | window->screen_allocation.height = height; |
| 438 | } |
| 439 | |
| 440 | static const struct wl_output_listener output_listener = { |
| 441 | window_handle_geometry, |
| 442 | }; |
| 443 | |
| 444 | static void |
| 445 | window_handle_global(struct wl_display *display, |
| 446 | struct wl_object *object, void *data) |
| 447 | { |
| 448 | struct window *window = data; |
| 449 | |
| 450 | if (wl_object_implements(object, "compositor", 1)) { |
| 451 | window->compositor = (struct wl_compositor *) object; |
| 452 | wl_compositor_add_listener(window->compositor, |
| 453 | &compositor_listener, window); |
| 454 | } else if (wl_object_implements(object, "output", 1)) { |
| 455 | struct wl_output *output = (struct wl_output *) object; |
| 456 | |
| 457 | wl_output_add_listener(output, |
| 458 | &output_listener, window); |
| 459 | } else if (wl_object_implements(object, "input_device", 1)) { |
| 460 | struct wl_input_device *input_device = |
| 461 | (struct wl_input_device *) object; |
| 462 | |
| 463 | wl_input_device_add_listener(input_device, |
| 464 | &input_device_listener, window); |
| 465 | } |
| 466 | } |
| 467 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 468 | struct window * |
| 469 | window_create(struct wl_display *display, int fd, |
| 470 | const char *title, |
| 471 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 472 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 473 | struct window *window; |
| 474 | |
| 475 | window = malloc(sizeof *window); |
| 476 | if (window == NULL) |
| 477 | return NULL; |
| 478 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 479 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 480 | window->display = display; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 481 | window->title = strdup(title); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 482 | window->compositor = wl_display_get_compositor(display); |
| 483 | window->surface = wl_compositor_create_surface(window->compositor); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 484 | window->allocation.x = x; |
| 485 | window->allocation.y = y; |
| 486 | window->allocation.width = width; |
| 487 | window->allocation.height = height; |
| 488 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 489 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 490 | window->state = WINDOW_STABLE; |
| 491 | window->fd = fd; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 492 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 493 | wl_display_add_global_listener(display, |
| 494 | window_handle_global, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 495 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 496 | return window; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 497 | } |