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 | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 33 | #include <cairo-drm.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 34 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 35 | #include <linux/input.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 36 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 37 | #include "wayland-glib.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 38 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 39 | #include "window.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 40 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 41 | struct display { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 42 | struct wl_display *display; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 43 | struct wl_compositor *compositor; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 44 | struct wl_output *output; |
| 45 | struct wl_input_device *input_device; |
| 46 | struct rectangle screen_allocation; |
| 47 | cairo_drm_context_t *ctx; |
| 48 | int fd; |
| 49 | }; |
| 50 | |
| 51 | struct window { |
| 52 | struct display *display; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 53 | struct wl_surface *surface; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 54 | const char *title; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 55 | struct rectangle allocation, saved_allocation; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 56 | int minimum_width, minimum_height; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 57 | int margin; |
Kristian Høgsberg | c492b48 | 2008-12-12 12:00:02 -0500 | [diff] [blame] | 58 | int drag_x, drag_y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 59 | int state; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 60 | int fullscreen; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 61 | struct wl_input_device *grab_device; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 62 | uint32_t name; |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame^] | 63 | uint32_t modifiers; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 64 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 65 | cairo_surface_t *cairo_surface; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 66 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 67 | window_resize_handler_t resize_handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 68 | window_key_handler_t key_handler; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 69 | void *user_data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 70 | }; |
| 71 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 72 | static void |
| 73 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 74 | { |
| 75 | cairo_move_to(cr, x0, y0 + radius); |
| 76 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 77 | cairo_line_to(cr, x1 - radius, y0); |
| 78 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 79 | cairo_line_to(cr, x1, y1 - radius); |
| 80 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 81 | cairo_line_to(cr, x0 + radius, y1); |
| 82 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 83 | cairo_close_path(cr); |
| 84 | } |
| 85 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 86 | static void |
| 87 | window_draw_decorations(struct window *window) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 88 | { |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 89 | cairo_t *cr; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 90 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 91 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 92 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 93 | struct wl_visual *visual; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 94 | int width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 95 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 96 | window->cairo_surface = |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 97 | cairo_drm_surface_create(window->display->ctx, |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 98 | CAIRO_CONTENT_COLOR_ALPHA, |
| 99 | window->allocation.width, |
| 100 | window->allocation.height); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 101 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 102 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 103 | bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 104 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 105 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 106 | cr = cairo_create(window->cairo_surface); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 107 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 108 | width = window->allocation.width - window->margin * 2; |
| 109 | height = window->allocation.height - window->margin * 2; |
| 110 | |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 111 | cairo_translate(cr, window->margin + 7, window->margin + 5); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 112 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 113 | cairo_set_source_rgba(cr, 0, 0, 0, 0.7); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 114 | rounded_rect(cr, 0, 0, width, height, radius); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 115 | cairo_fill(cr); |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 116 | |
| 117 | #ifdef SLOW_BUT_PWETTY |
| 118 | /* FIXME: Aw, pretty drop shadows now have to fallback to sw. |
| 119 | * Ideally we should have convolution filters in cairo, but we |
| 120 | * can also fallback to compositing the shadow image a bunch |
| 121 | * of times according to the blur kernel. */ |
| 122 | { |
| 123 | cairo_surface_t *map; |
| 124 | |
| 125 | map = cairo_drm_surface_map(window->cairo_surface); |
| 126 | blur_surface(map); |
| 127 | cairo_drm_surface_unmap(window->cairo_surface, map); |
| 128 | } |
| 129 | #endif |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 130 | |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 131 | cairo_translate(cr, -7, -5); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 132 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 133 | rounded_rect(cr, 1, 1, width - 1, height - 1, radius); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 134 | cairo_set_source(cr, outline); |
| 135 | cairo_stroke(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 136 | rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 137 | cairo_set_source(cr, bright); |
| 138 | cairo_stroke(cr); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 139 | rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 140 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 141 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 143 | rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1); |
Kristian Høgsberg | 6e635f3 | 2008-11-09 09:15:46 -0500 | [diff] [blame] | 144 | gradient = cairo_pattern_create_linear (0, 0, 0, 100); |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 145 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4); |
| 146 | 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] | 147 | cairo_set_source(cr, gradient); |
| 148 | cairo_fill(cr); |
| 149 | cairo_pattern_destroy(gradient); |
| 150 | |
| 151 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 152 | cairo_move_to(cr, 10, 50); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 153 | cairo_line_to(cr, width - 10, 50); |
| 154 | cairo_line_to(cr, width - 10, height - 10); |
| 155 | cairo_line_to(cr, 10, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 156 | cairo_close_path(cr); |
| 157 | cairo_set_source(cr, dim); |
| 158 | cairo_stroke(cr); |
| 159 | |
| 160 | cairo_move_to(cr, 11, 51); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 161 | cairo_line_to(cr, width - 10, 51); |
| 162 | cairo_line_to(cr, width - 10, height - 10); |
| 163 | cairo_line_to(cr, 11, height - 10); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 164 | cairo_close_path(cr); |
| 165 | cairo_set_source(cr, bright); |
| 166 | cairo_stroke(cr); |
| 167 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 168 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 169 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 170 | cairo_text_extents(cr, window->title, &extents); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 171 | 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] | 172 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 173 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 174 | cairo_set_line_width (cr, 4); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 175 | cairo_text_path(cr, window->title); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 176 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 177 | cairo_stroke_preserve(cr); |
| 178 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 179 | cairo_fill(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 180 | cairo_destroy(cr); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 181 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 182 | visual = wl_display_get_premultiplied_argb_visual(window->display->display); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 183 | wl_surface_attach(window->surface, |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 184 | cairo_drm_surface_get_name(window->cairo_surface), |
| 185 | window->allocation.width, |
| 186 | window->allocation.height, |
| 187 | cairo_drm_surface_get_stride(window->cairo_surface), |
Kristian Høgsberg | de31d5c | 2008-12-18 17:55:33 -0500 | [diff] [blame] | 188 | visual); |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 189 | |
Kristian Høgsberg | fbdbbdc | 2008-11-28 17:06:06 -0500 | [diff] [blame] | 190 | wl_surface_map(window->surface, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 191 | window->allocation.x - window->margin, |
| 192 | window->allocation.y - window->margin, |
| 193 | window->allocation.width, |
| 194 | window->allocation.height); |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | window_draw_fullscreen(struct window *window) |
| 199 | { |
| 200 | struct wl_visual *visual; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 201 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 202 | window->cairo_surface = |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 203 | cairo_drm_surface_create(window->display->ctx, |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 204 | CAIRO_CONTENT_COLOR_ALPHA, |
| 205 | window->allocation.width, |
| 206 | window->allocation.height); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 207 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 208 | visual = wl_display_get_premultiplied_argb_visual(window->display->display); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 209 | wl_surface_attach(window->surface, |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 210 | cairo_drm_surface_get_name(window->cairo_surface), |
| 211 | window->allocation.width, |
| 212 | window->allocation.height, |
| 213 | cairo_drm_surface_get_stride(window->cairo_surface), |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 214 | visual); |
| 215 | |
| 216 | wl_surface_map(window->surface, |
| 217 | window->allocation.x, |
| 218 | window->allocation.y, |
| 219 | window->allocation.width, |
| 220 | window->allocation.height); |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | window_draw(struct window *window) |
| 225 | { |
| 226 | if (window->fullscreen) |
| 227 | window_draw_fullscreen(window); |
| 228 | else |
| 229 | window_draw_decorations(window); |
Kristian Høgsberg | 44f36e3 | 2008-11-26 12:57:31 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 232 | static void |
| 233 | window_handle_acknowledge(void *data, |
| 234 | struct wl_compositor *compositor, |
| 235 | uint32_t key, uint32_t frame) |
| 236 | { |
| 237 | struct window *window = data; |
| 238 | |
| 239 | /* The acknowledge event means that the server |
| 240 | * processed our last commit request and we can now |
| 241 | * safely free the old window buffer if we resized and |
| 242 | * render the next frame into our back buffer.. */ |
| 243 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 244 | if (key == 0 && window->cairo_surface != NULL) { |
| 245 | cairo_surface_destroy(window->cairo_surface); |
| 246 | window->cairo_surface = NULL; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | window_handle_frame(void *data, |
| 252 | struct wl_compositor *compositor, |
| 253 | uint32_t frame, uint32_t timestamp) |
| 254 | { |
| 255 | } |
| 256 | |
| 257 | static const struct wl_compositor_listener compositor_listener = { |
| 258 | window_handle_acknowledge, |
| 259 | window_handle_frame, |
| 260 | }; |
| 261 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 262 | enum window_state { |
| 263 | WINDOW_STABLE, |
| 264 | WINDOW_MOVING, |
| 265 | WINDOW_RESIZING_UPPER_LEFT, |
| 266 | WINDOW_RESIZING_UPPER_RIGHT, |
| 267 | WINDOW_RESIZING_LOWER_LEFT, |
| 268 | WINDOW_RESIZING_LOWER_RIGHT |
| 269 | }; |
| 270 | |
| 271 | enum location { |
| 272 | LOCATION_INTERIOR, |
| 273 | LOCATION_UPPER_LEFT, |
| 274 | LOCATION_UPPER_RIGHT, |
| 275 | LOCATION_LOWER_LEFT, |
| 276 | LOCATION_LOWER_RIGHT, |
| 277 | LOCATION_OUTSIDE |
| 278 | }; |
| 279 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 280 | static void |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 281 | window_handle_motion(void *data, struct wl_input_device *input_device, |
| 282 | 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] | 283 | { |
| 284 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 285 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 286 | switch (window->state) { |
| 287 | case WINDOW_MOVING: |
| 288 | if (window->fullscreen) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 289 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 290 | if (window->grab_device != input_device) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 291 | break; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 292 | window->allocation.x = window->drag_x + x; |
| 293 | window->allocation.y = window->drag_y + y; |
| 294 | wl_surface_map(window->surface, |
| 295 | window->allocation.x - window->margin, |
| 296 | window->allocation.y - window->margin, |
| 297 | window->allocation.width, |
| 298 | window->allocation.height); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 299 | wl_compositor_commit(window->display->compositor, 1); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 300 | break; |
| 301 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 302 | if (window->fullscreen) |
| 303 | break; |
| 304 | if (window->grab_device != input_device) |
| 305 | break; |
| 306 | window->allocation.width = window->drag_x + x; |
| 307 | window->allocation.height = window->drag_y + y; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 308 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 309 | if (window->resize_handler) |
| 310 | (*window->resize_handler)(window, |
| 311 | window->user_data); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 312 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 313 | break; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 317 | static void window_handle_button(void *data, struct wl_input_device *input_device, |
| 318 | uint32_t button, uint32_t state, |
| 319 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 320 | { |
| 321 | struct window *window = data; |
| 322 | int32_t left = window->allocation.x; |
| 323 | int32_t right = window->allocation.x + |
| 324 | window->allocation.width - window->margin * 2; |
| 325 | int32_t top = window->allocation.y; |
| 326 | int32_t bottom = window->allocation.y + |
| 327 | window->allocation.height - window->margin * 2; |
| 328 | int grip_size = 16, location; |
| 329 | |
| 330 | if (right - grip_size <= x && x < right && |
| 331 | bottom - grip_size <= y && y < bottom) { |
| 332 | location = LOCATION_LOWER_RIGHT; |
| 333 | } else if (left <= x && x < right && top <= y && y < bottom) { |
| 334 | location = LOCATION_INTERIOR; |
| 335 | } else { |
| 336 | location = LOCATION_OUTSIDE; |
| 337 | } |
| 338 | |
| 339 | if (button == BTN_LEFT && state == 1) { |
| 340 | switch (location) { |
| 341 | case LOCATION_INTERIOR: |
| 342 | window->drag_x = window->allocation.x - x; |
| 343 | window->drag_y = window->allocation.y - y; |
| 344 | window->state = WINDOW_MOVING; |
| 345 | window->grab_device = input_device; |
| 346 | break; |
| 347 | case LOCATION_LOWER_RIGHT: |
| 348 | window->drag_x = window->allocation.width - x; |
| 349 | window->drag_y = window->allocation.height - y; |
| 350 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 351 | window->grab_device = input_device; |
| 352 | break; |
| 353 | default: |
| 354 | window->state = WINDOW_STABLE; |
| 355 | break; |
| 356 | } |
| 357 | } else if (button == BTN_LEFT && |
| 358 | state == 0 && window->grab_device == input_device) { |
| 359 | window->state = WINDOW_STABLE; |
| 360 | } |
| 361 | } |
| 362 | |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame^] | 363 | |
| 364 | struct key { |
| 365 | uint32_t code[4]; |
| 366 | } evdev_keymap[] = { |
| 367 | { { 0, 0 } }, /* 0 */ |
| 368 | { { 0x1b, 0x1b } }, |
| 369 | { { '1', '!' } }, |
| 370 | { { '2', '@' } }, |
| 371 | { { '3', '#' } }, |
| 372 | { { '4', '$' } }, |
| 373 | { { '5', '%' } }, |
| 374 | { { '6', '^' } }, |
| 375 | { { '7', '&' } }, |
| 376 | { { '8', '*' } }, |
| 377 | { { '9', '(' } }, |
| 378 | { { '0', ')' } }, |
| 379 | { { '-', '_' } }, |
| 380 | { { '=', '+' } }, |
| 381 | { { '\b', '\b' } }, |
| 382 | { { '\t', '\t' } }, |
| 383 | |
| 384 | { { 'q', 'Q', 0x11 } }, /* 16 */ |
| 385 | { { 'w', 'W', 0x17 } }, |
| 386 | { { 'e', 'E', 0x05 } }, |
| 387 | { { 'r', 'R', 0x12 } }, |
| 388 | { { 't', 'T', 0x14 } }, |
| 389 | { { 'y', 'Y', 0x19 } }, |
| 390 | { { 'u', 'U', 0x15 } }, |
| 391 | { { 'i', 'I', 0x09 } }, |
| 392 | { { 'o', 'O', 0x0f } }, |
| 393 | { { 'p', 'P', 0x10 } }, |
| 394 | { { '[', '{', 0x1b } }, |
| 395 | { { ']', '}', 0x1d } }, |
| 396 | { { '\n', '\n' } }, |
| 397 | { { 0, 0 } }, |
| 398 | { { 'a', 'A', 0x01} }, |
| 399 | { { 's', 'S', 0x13 } }, |
| 400 | |
| 401 | { { 'd', 'D', 0x04 } }, /* 32 */ |
| 402 | { { 'f', 'F', 0x06 } }, |
| 403 | { { 'g', 'G', 0x07 } }, |
| 404 | { { 'h', 'H', 0x08 } }, |
| 405 | { { 'j', 'J', 0x0a } }, |
| 406 | { { 'k', 'K', 0x0b } }, |
| 407 | { { 'l', 'L', 0x0c } }, |
| 408 | { { ';', ':' } }, |
| 409 | { { '\'', '"' } }, |
| 410 | { { '`', '~' } }, |
| 411 | { { 0, 0 } }, |
| 412 | { { '\\', '|', 0x1c } }, |
| 413 | { { 'z', 'Z', 0x1a } }, |
| 414 | { { 'x', 'X', 0x18 } }, |
| 415 | { { 'c', 'C', 0x03 } }, |
| 416 | { { 'v', 'V', 0x16 } }, |
| 417 | |
| 418 | { { 'b', 'B', 0x02 } }, /* 48 */ |
| 419 | { { 'n', 'N', 0x0e } }, |
| 420 | { { 'm', 'M', 0x0d } }, |
| 421 | { { ',', '<' } }, |
| 422 | { { '.', '>' } }, |
| 423 | { { '/', '?' } }, |
| 424 | { { 0, 0 } }, |
| 425 | { { '*', '*' } }, |
| 426 | { { 0, 0 } }, |
| 427 | { { ' ', ' ' } }, |
| 428 | { { 0, 0 } } |
| 429 | |
| 430 | /* 59 */ |
| 431 | }; |
| 432 | |
| 433 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 434 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 435 | static void |
| 436 | window_handle_key(void *data, struct wl_input_device *input_device, |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame^] | 437 | uint32_t key, uint32_t state) |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 438 | { |
| 439 | struct window *window = data; |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame^] | 440 | uint32_t mod = 0; |
| 441 | uint32_t unicode = 0; |
| 442 | |
| 443 | switch (key) { |
| 444 | case KEY_LEFTSHIFT: |
| 445 | case KEY_RIGHTSHIFT: |
| 446 | mod = WINDOW_MODIFIER_SHIFT; |
| 447 | break; |
| 448 | case KEY_LEFTCTRL: |
| 449 | case KEY_RIGHTCTRL: |
| 450 | mod = WINDOW_MODIFIER_CONTROL; |
| 451 | break; |
| 452 | case KEY_LEFTALT: |
| 453 | case KEY_RIGHTALT: |
| 454 | mod = WINDOW_MODIFIER_ALT; |
| 455 | break; |
| 456 | default: |
| 457 | if (key < ARRAY_LENGTH(evdev_keymap)) { |
| 458 | if (window->modifiers & WINDOW_MODIFIER_CONTROL) |
| 459 | unicode = evdev_keymap[key].code[2]; |
| 460 | else if (window->modifiers & WINDOW_MODIFIER_SHIFT) |
| 461 | unicode = evdev_keymap[key].code[1]; |
| 462 | else |
| 463 | unicode = evdev_keymap[key].code[0]; |
| 464 | } |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | if (state) |
| 469 | window->modifiers |= mod; |
| 470 | else |
| 471 | window->modifiers &= ~mod; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 472 | |
| 473 | if (window->key_handler) |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame^] | 474 | (*window->key_handler)(window, key, unicode, |
| 475 | state, window->modifiers, window->user_data); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static const struct wl_input_device_listener input_device_listener = { |
| 479 | window_handle_motion, |
| 480 | window_handle_button, |
| 481 | window_handle_key, |
| 482 | }; |
| 483 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 484 | void |
| 485 | window_get_child_rectangle(struct window *window, |
| 486 | struct rectangle *rectangle) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 487 | { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 488 | if (window->fullscreen) { |
| 489 | *rectangle = window->allocation; |
| 490 | } else { |
| 491 | rectangle->x = window->margin + 10; |
| 492 | rectangle->y = window->margin + 50; |
| 493 | rectangle->width = window->allocation.width - 20 - window->margin * 2; |
| 494 | rectangle->height = window->allocation.height - 60 - window->margin * 2; |
| 495 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 499 | window_set_child_size(struct window *window, |
| 500 | struct rectangle *rectangle) |
| 501 | { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 502 | if (!window->fullscreen) { |
| 503 | window->allocation.width = rectangle->width + 20 + window->margin * 2; |
| 504 | window->allocation.height = rectangle->height + 60 + window->margin * 2; |
| 505 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 508 | cairo_surface_t * |
| 509 | window_create_surface(struct window *window, |
| 510 | struct rectangle *rectangle) |
| 511 | { |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 512 | return cairo_drm_surface_create(window->display->ctx, |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 513 | CAIRO_CONTENT_COLOR_ALPHA, |
| 514 | rectangle->width, |
| 515 | rectangle->height); |
| 516 | } |
| 517 | |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 518 | void |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 519 | window_copy(struct window *window, |
| 520 | struct rectangle *rectangle, |
| 521 | uint32_t name, uint32_t stride) |
| 522 | { |
| 523 | wl_surface_copy(window->surface, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 524 | rectangle->x, |
| 525 | rectangle->y, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 526 | name, stride, |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 527 | 0, 0, |
| 528 | rectangle->width, |
| 529 | rectangle->height); |
| 530 | } |
| 531 | |
| 532 | void |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 533 | window_copy_surface(struct window *window, |
| 534 | struct rectangle *rectangle, |
| 535 | cairo_surface_t *surface) |
| 536 | { |
| 537 | wl_surface_copy(window->surface, |
| 538 | rectangle->x, |
| 539 | rectangle->y, |
| 540 | cairo_drm_surface_get_name(surface), |
| 541 | cairo_drm_surface_get_stride(surface), |
| 542 | 0, 0, |
| 543 | rectangle->width, |
| 544 | rectangle->height); |
| 545 | } |
| 546 | |
| 547 | void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 548 | window_set_fullscreen(struct window *window, int fullscreen) |
| 549 | { |
| 550 | window->fullscreen = fullscreen; |
| 551 | if (window->fullscreen) { |
| 552 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 553 | window->allocation = window->display->screen_allocation; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 554 | } else { |
| 555 | window->allocation = window->saved_allocation; |
| 556 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void |
| 560 | window_set_resize_handler(struct window *window, |
| 561 | window_resize_handler_t handler, void *data) |
| 562 | { |
| 563 | window->resize_handler = handler; |
| 564 | window->user_data = data; |
| 565 | } |
| 566 | |
| 567 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 568 | window_set_key_handler(struct window *window, |
| 569 | window_key_handler_t handler, void *data) |
| 570 | { |
| 571 | window->key_handler = handler; |
| 572 | window->user_data = data; |
| 573 | } |
| 574 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 575 | struct window * |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 576 | window_create(struct display *display, const char *title, |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 577 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 578 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 579 | struct window *window; |
| 580 | |
| 581 | window = malloc(sizeof *window); |
| 582 | if (window == NULL) |
| 583 | return NULL; |
| 584 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 585 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 586 | window->display = display; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 587 | window->title = strdup(title); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 588 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 589 | window->allocation.x = x; |
| 590 | window->allocation.y = y; |
| 591 | window->allocation.width = width; |
| 592 | window->allocation.height = height; |
| 593 | window->saved_allocation = window->allocation; |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 594 | window->margin = 16; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 595 | window->state = WINDOW_STABLE; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 596 | |
| 597 | wl_compositor_add_listener(display->compositor, |
| 598 | &compositor_listener, window); |
| 599 | |
| 600 | wl_input_device_add_listener(display->input_device, |
| 601 | &input_device_listener, window); |
| 602 | |
| 603 | return window; |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | display_handle_geometry(void *data, |
| 608 | struct wl_output *output, |
| 609 | int32_t width, int32_t height) |
| 610 | { |
| 611 | struct display *display = data; |
| 612 | |
| 613 | display->screen_allocation.x = 0; |
| 614 | display->screen_allocation.y = 0; |
| 615 | display->screen_allocation.width = width; |
| 616 | display->screen_allocation.height = height; |
| 617 | } |
| 618 | |
| 619 | static const struct wl_output_listener output_listener = { |
| 620 | display_handle_geometry, |
| 621 | }; |
| 622 | |
| 623 | static void |
| 624 | display_handle_global(struct wl_display *display, |
| 625 | struct wl_object *object, void *data) |
| 626 | { |
| 627 | struct display *d = data; |
| 628 | |
| 629 | if (wl_object_implements(object, "compositor", 1)) { |
| 630 | d->compositor = (struct wl_compositor *) object; |
| 631 | } else if (wl_object_implements(object, "output", 1)) { |
| 632 | d->output = (struct wl_output *) object; |
| 633 | wl_output_add_listener(d->output, &output_listener, d); |
| 634 | } else if (wl_object_implements(object, "input_device", 1)) { |
| 635 | d->input_device =(struct wl_input_device *) object; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | struct display * |
| 640 | display_create(struct wl_display *display, int fd) |
| 641 | { |
| 642 | struct display *d; |
| 643 | |
| 644 | d = malloc(sizeof *d); |
| 645 | if (d == NULL) |
| 646 | return NULL; |
| 647 | |
| 648 | d->display = display; |
| 649 | d->ctx = cairo_drm_context_get_for_fd(fd); |
| 650 | if (d->ctx == NULL) { |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 651 | fprintf(stderr, "failed to get cairo drm context\n"); |
| 652 | return NULL; |
| 653 | } |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 654 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 655 | /* Set up listener so we'll catch all events. */ |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 656 | wl_display_add_global_listener(display, |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 657 | display_handle_global, d); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 658 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 659 | /* Process connection events. */ |
| 660 | wl_display_iterate(display, WL_DISPLAY_READABLE); |
| 661 | |
| 662 | return d; |
| 663 | } |
| 664 | |
| 665 | struct wl_compositor * |
| 666 | display_get_compositor(struct display *display) |
| 667 | { |
| 668 | return display->compositor; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 669 | } |