Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 22 | */ |
| 23 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 26 | #include <stdbool.h> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <cairo.h> |
Kristian Høgsberg | 5c4056e | 2010-12-16 14:56:41 -0500 | [diff] [blame] | 32 | #include <math.h> |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 33 | #include <assert.h> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 34 | |
Kristian Høgsberg | 8da0fbd | 2011-12-19 15:36:10 -0500 | [diff] [blame] | 35 | #include <linux/input.h> |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 36 | #include <wayland-client.h> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 37 | |
| 38 | #include "window.h" |
Bryce Harrington | e99e4bf | 2016-03-16 14:15:18 -0700 | [diff] [blame] | 39 | #include "shared/xalloc.h" |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 40 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 41 | struct spring { |
| 42 | double current; |
| 43 | double target; |
| 44 | double previous; |
| 45 | }; |
| 46 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 47 | struct resizor { |
| 48 | struct display *display; |
| 49 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 50 | struct widget *widget; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 51 | struct window *menu; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 52 | struct spring width; |
| 53 | struct spring height; |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 54 | struct wl_callback *frame_callback; |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 55 | bool pointer_locked; |
| 56 | struct input *locked_input; |
| 57 | float pointer_x; |
| 58 | float pointer_y; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static void |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 62 | spring_update(struct spring *spring) |
| 63 | { |
| 64 | double current, force; |
| 65 | |
| 66 | current = spring->current; |
| 67 | force = (spring->target - current) / 20.0 + |
| 68 | (spring->previous - current); |
| 69 | |
| 70 | spring->current = current + (current - spring->previous) + force; |
| 71 | spring->previous = current; |
| 72 | } |
| 73 | |
| 74 | static int |
| 75 | spring_done(struct spring *spring) |
| 76 | { |
| 77 | return fabs(spring->previous - spring->target) < 0.1; |
| 78 | } |
| 79 | |
| 80 | static const struct wl_callback_listener listener; |
| 81 | |
| 82 | static void |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 83 | frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 84 | { |
| 85 | struct resizor *resizor = data; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 86 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 87 | assert(!callback || callback == resizor->frame_callback); |
| 88 | |
Kristian Høgsberg | 6f394d5 | 2014-01-17 15:31:33 -0800 | [diff] [blame] | 89 | if (resizor->frame_callback) { |
| 90 | wl_callback_destroy(resizor->frame_callback); |
| 91 | resizor->frame_callback = NULL; |
| 92 | } |
| 93 | |
| 94 | if (window_is_maximized(resizor->window)) |
| 95 | return; |
| 96 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 97 | spring_update(&resizor->width); |
| 98 | spring_update(&resizor->height); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 99 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 100 | widget_schedule_resize(resizor->widget, |
| 101 | resizor->width.current + 0.5, |
| 102 | resizor->height.current + 0.5); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 103 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 104 | if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) { |
| 105 | resizor->frame_callback = |
| 106 | wl_surface_frame( |
| 107 | window_get_wl_surface(resizor->window)); |
| 108 | wl_callback_add_listener(resizor->frame_callback, &listener, |
| 109 | resizor); |
| 110 | } |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 113 | static const struct wl_callback_listener listener = { |
| 114 | frame_callback |
| 115 | }; |
| 116 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 117 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 118 | redraw_handler(struct widget *widget, void *data) |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 119 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 120 | struct resizor *resizor = data; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 121 | cairo_surface_t *surface; |
| 122 | cairo_t *cr; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 123 | struct rectangle allocation; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 124 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 125 | widget_get_allocation(resizor->widget, &allocation); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 126 | |
| 127 | surface = window_get_surface(resizor->window); |
| 128 | |
| 129 | cr = cairo_create(surface); |
| 130 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 131 | cairo_rectangle(cr, |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 132 | allocation.x, |
| 133 | allocation.y, |
| 134 | allocation.width, |
| 135 | allocation.height); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 136 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 137 | cairo_fill(cr); |
| 138 | cairo_destroy(cr); |
| 139 | |
| 140 | cairo_surface_destroy(surface); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 144 | keyboard_focus_handler(struct window *window, |
| 145 | struct input *device, void *data) |
| 146 | { |
| 147 | struct resizor *resizor = data; |
| 148 | |
| 149 | window_schedule_redraw(resizor->window); |
| 150 | } |
| 151 | |
| 152 | static void |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 153 | key_handler(struct window *window, struct input *input, uint32_t time, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 154 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 155 | void *data) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 156 | { |
| 157 | struct resizor *resizor = data; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 158 | struct rectangle allocation; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 159 | |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 160 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 161 | return; |
| 162 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 163 | window_get_allocation(resizor->window, &allocation); |
| 164 | resizor->width.current = allocation.width; |
| 165 | if (spring_done(&resizor->width)) |
| 166 | resizor->width.target = allocation.width; |
| 167 | resizor->height.current = allocation.height; |
| 168 | if (spring_done(&resizor->height)) |
| 169 | resizor->height.target = allocation.height; |
| 170 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 171 | switch (sym) { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 172 | case XKB_KEY_Up: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 173 | if (allocation.height < 400) |
| 174 | break; |
| 175 | |
| 176 | resizor->height.target = allocation.height - 200; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 177 | break; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 178 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 179 | case XKB_KEY_Down: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 180 | if (allocation.height > 1000) |
| 181 | break; |
| 182 | |
| 183 | resizor->height.target = allocation.height + 200; |
| 184 | break; |
| 185 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 186 | case XKB_KEY_Left: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 187 | if (allocation.width < 400) |
| 188 | break; |
| 189 | |
| 190 | resizor->width.target = allocation.width - 200; |
| 191 | break; |
| 192 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 193 | case XKB_KEY_Right: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 194 | if (allocation.width > 1000) |
| 195 | break; |
| 196 | |
| 197 | resizor->width.target = allocation.width + 200; |
| 198 | break; |
| 199 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 200 | case XKB_KEY_Escape: |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 201 | display_exit(resizor->display); |
| 202 | break; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 203 | } |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 204 | |
| 205 | if (!resizor->frame_callback) |
| 206 | frame_callback(resizor, NULL, 0); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 209 | static void |
Jasper St. Pierre | dda9313 | 2014-03-13 12:06:00 -0400 | [diff] [blame] | 210 | menu_func(void *data, struct input *input, int index) |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 211 | { |
| 212 | fprintf(stderr, "picked entry %d\n", index); |
| 213 | } |
| 214 | |
| 215 | static void |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 216 | show_menu(struct resizor *resizor, struct input *input, uint32_t time) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 217 | { |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 218 | int32_t x, y; |
| 219 | static const char *entries[] = { |
| 220 | "Roy", "Pris", "Leon", "Zhora" |
| 221 | }; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 222 | |
| 223 | input_get_position(input, &x, &y); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 224 | window_show_menu(resizor->display, input, time, resizor->window, |
| 225 | x - 10, y - 10, menu_func, entries, 4); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 229 | locked_pointer_handle_motion(struct window *window, |
| 230 | struct input *input, |
| 231 | uint32_t time, |
| 232 | float dx, |
| 233 | float dy, |
| 234 | void *data) |
| 235 | { |
| 236 | struct resizor *resizor = data; |
| 237 | |
| 238 | resizor->width.current += dx; |
| 239 | resizor->width.previous = resizor->width.current; |
| 240 | resizor->width.target = resizor->width.current; |
| 241 | |
| 242 | resizor->height.current += dy; |
| 243 | resizor->height.previous = resizor->height.current; |
| 244 | resizor->height.target = resizor->height.current; |
| 245 | |
| 246 | widget_schedule_resize(resizor->widget, |
| 247 | resizor->width.current, |
| 248 | resizor->height.current); |
| 249 | } |
| 250 | |
| 251 | static void |
| 252 | handle_pointer_locked(struct window *window, struct input *input, void *data) |
| 253 | { |
| 254 | struct resizor *resizor = data; |
| 255 | |
| 256 | resizor->pointer_locked = true; |
| 257 | input_set_pointer_image(input, CURSOR_BLANK); |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | handle_pointer_unlocked(struct window *window, struct input *input, void *data) |
| 262 | { |
| 263 | struct resizor *resizor = data; |
| 264 | |
| 265 | resizor->pointer_locked = false; |
| 266 | input_set_pointer_image(input, CURSOR_LEFT_PTR); |
| 267 | } |
| 268 | |
| 269 | static const struct wl_callback_listener locked_pointer_frame_listener; |
| 270 | |
| 271 | static void |
| 272 | locked_pointer_frame_callback(void *data, |
| 273 | struct wl_callback *callback, |
| 274 | uint32_t time) |
| 275 | { |
| 276 | struct resizor *resizor = data; |
| 277 | struct wl_surface *surface; |
| 278 | struct rectangle allocation; |
| 279 | float x, y; |
| 280 | |
| 281 | if (resizor->pointer_locked) { |
| 282 | widget_get_allocation(resizor->widget, &allocation); |
| 283 | |
| 284 | x = resizor->pointer_x + (allocation.width - allocation.x); |
| 285 | y = resizor->pointer_y + (allocation.height - allocation.y); |
| 286 | |
| 287 | widget_set_locked_pointer_cursor_hint(resizor->widget, x, y); |
| 288 | } |
| 289 | |
| 290 | wl_callback_destroy(callback); |
| 291 | |
| 292 | surface = window_get_wl_surface(resizor->window); |
| 293 | callback = wl_surface_frame(surface); |
| 294 | wl_callback_add_listener(callback, |
| 295 | &locked_pointer_frame_listener, |
| 296 | resizor); |
| 297 | } |
| 298 | |
| 299 | static const struct wl_callback_listener locked_pointer_frame_listener = { |
| 300 | locked_pointer_frame_callback |
| 301 | }; |
| 302 | |
| 303 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 304 | button_handler(struct widget *widget, |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 305 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 306 | uint32_t button, enum wl_pointer_button_state state, void *data) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 307 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 308 | struct resizor *resizor = data; |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 309 | struct rectangle allocation; |
| 310 | struct wl_surface *surface; |
| 311 | struct wl_callback *callback; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 312 | |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 313 | if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 314 | show_menu(resizor, input, time); |
| 315 | } else if (button == BTN_LEFT && |
| 316 | state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 317 | window_get_allocation(resizor->window, &allocation); |
| 318 | |
| 319 | resizor->width.current = allocation.width; |
| 320 | resizor->width.previous = allocation.width; |
| 321 | resizor->width.target = allocation.width; |
| 322 | |
| 323 | resizor->height.current = allocation.height; |
| 324 | resizor->height.previous = allocation.height; |
| 325 | resizor->height.target = allocation.height; |
| 326 | |
| 327 | window_lock_pointer(resizor->window, input); |
| 328 | window_set_pointer_locked_handler(resizor->window, |
| 329 | handle_pointer_locked, |
| 330 | handle_pointer_unlocked); |
| 331 | resizor->locked_input = input; |
| 332 | |
| 333 | surface = window_get_wl_surface(resizor->window); |
| 334 | callback = wl_surface_frame(surface); |
| 335 | wl_callback_add_listener(callback, |
| 336 | &locked_pointer_frame_listener, |
| 337 | resizor); |
| 338 | } else if (button == BTN_LEFT && |
| 339 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
| 340 | input_set_pointer_image(input, CURSOR_LEFT_PTR); |
| 341 | window_unlock_pointer(resizor->window); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 345 | static void |
| 346 | set_cursor_inv_offset(struct resizor *resizor, float x, float y) |
| 347 | { |
| 348 | struct rectangle allocation; |
| 349 | |
| 350 | widget_get_allocation(resizor->widget, &allocation); |
| 351 | |
| 352 | resizor->pointer_x = x - (allocation.width - allocation.x); |
| 353 | resizor->pointer_y = y - (allocation.height - allocation.y); |
| 354 | } |
| 355 | |
| 356 | static int |
| 357 | enter_handler(struct widget *widget, |
| 358 | struct input *input, |
| 359 | float x, float y, void *data) |
| 360 | { |
| 361 | struct resizor *resizor = data; |
| 362 | |
| 363 | set_cursor_inv_offset(resizor, x , y); |
| 364 | |
| 365 | return CURSOR_LEFT_PTR; |
| 366 | } |
| 367 | |
| 368 | static int |
| 369 | motion_handler(struct widget *widget, |
| 370 | struct input *input, uint32_t time, |
| 371 | float x, float y, void *data) |
| 372 | { |
| 373 | struct resizor *resizor = data; |
| 374 | |
| 375 | set_cursor_inv_offset(resizor, x , y); |
| 376 | |
| 377 | return CURSOR_LEFT_PTR; |
| 378 | } |
| 379 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 380 | static struct resizor * |
| 381 | resizor_create(struct display *display) |
| 382 | { |
| 383 | struct resizor *resizor; |
| 384 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 385 | resizor = xzalloc(sizeof *resizor); |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 386 | resizor->window = window_create(display); |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 387 | resizor->widget = window_frame_create(resizor->window, resizor); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 388 | window_set_title(resizor->window, "Wayland Resizor"); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 389 | resizor->display = display; |
| 390 | |
| 391 | window_set_key_handler(resizor->window, key_handler); |
| 392 | window_set_user_data(resizor->window, resizor); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 393 | widget_set_redraw_handler(resizor->widget, redraw_handler); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 394 | window_set_keyboard_focus_handler(resizor->window, |
| 395 | keyboard_focus_handler); |
| 396 | |
Jonas Ådahl | 9f8c894 | 2014-11-26 17:40:42 +0800 | [diff] [blame] | 397 | widget_set_enter_handler(resizor->widget, enter_handler); |
| 398 | widget_set_motion_handler(resizor->widget, motion_handler); |
| 399 | |
| 400 | window_set_locked_pointer_motion_handler( |
| 401 | resizor->window, locked_pointer_handle_motion); |
| 402 | |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 403 | widget_set_button_handler(resizor->widget, button_handler); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 404 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 405 | resizor->height.previous = 400; |
| 406 | resizor->height.current = 400; |
| 407 | resizor->height.target = 400; |
| 408 | |
| 409 | resizor->width.previous = 400; |
| 410 | resizor->width.current = 400; |
| 411 | resizor->width.target = 400; |
| 412 | |
| 413 | widget_schedule_resize(resizor->widget, 400, 400); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 414 | |
| 415 | return resizor; |
| 416 | } |
| 417 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 418 | static void |
| 419 | resizor_destroy(struct resizor *resizor) |
| 420 | { |
| 421 | if (resizor->frame_callback) |
| 422 | wl_callback_destroy(resizor->frame_callback); |
| 423 | |
Pekka Paalanen | 84d62dc | 2012-01-19 14:21:35 +0200 | [diff] [blame] | 424 | widget_destroy(resizor->widget); |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 425 | window_destroy(resizor->window); |
| 426 | free(resizor); |
| 427 | } |
| 428 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 429 | int |
| 430 | main(int argc, char *argv[]) |
| 431 | { |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 432 | struct display *display; |
| 433 | struct resizor *resizor; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 434 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 435 | display = display_create(&argc, argv); |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 436 | if (display == NULL) { |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 437 | fprintf(stderr, "failed to create display: %m\n"); |
| 438 | return -1; |
| 439 | } |
| 440 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 441 | resizor = resizor_create(display); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 442 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 443 | display_run(display); |
| 444 | |
| 445 | resizor_destroy(resizor); |
| 446 | display_destroy(display); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |