Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 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 | |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <cairo.h> |
Kristian Høgsberg | 5c4056e | 2010-12-16 14:56:41 -0500 | [diff] [blame] | 28 | #include <math.h> |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 29 | #include <assert.h> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 30 | |
Kristian Høgsberg | 8da0fbd | 2011-12-19 15:36:10 -0500 | [diff] [blame] | 31 | #include <linux/input.h> |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 32 | #include <wayland-client.h> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 33 | |
| 34 | #include "window.h" |
| 35 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 36 | struct spring { |
| 37 | double current; |
| 38 | double target; |
| 39 | double previous; |
| 40 | }; |
| 41 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 42 | struct resizor { |
| 43 | struct display *display; |
| 44 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 45 | struct widget *widget; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 46 | struct window *menu; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 47 | struct spring width; |
| 48 | struct spring height; |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 49 | struct wl_callback *frame_callback; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static void |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 53 | spring_update(struct spring *spring) |
| 54 | { |
| 55 | double current, force; |
| 56 | |
| 57 | current = spring->current; |
| 58 | force = (spring->target - current) / 20.0 + |
| 59 | (spring->previous - current); |
| 60 | |
| 61 | spring->current = current + (current - spring->previous) + force; |
| 62 | spring->previous = current; |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | spring_done(struct spring *spring) |
| 67 | { |
| 68 | return fabs(spring->previous - spring->target) < 0.1; |
| 69 | } |
| 70 | |
| 71 | static const struct wl_callback_listener listener; |
| 72 | |
| 73 | static void |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 74 | frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 75 | { |
| 76 | struct resizor *resizor = data; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 77 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 78 | assert(!callback || callback == resizor->frame_callback); |
| 79 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 80 | spring_update(&resizor->width); |
| 81 | spring_update(&resizor->height); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 82 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 83 | widget_schedule_resize(resizor->widget, |
| 84 | resizor->width.current + 0.5, |
| 85 | resizor->height.current + 0.5); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame] | 86 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 87 | if (resizor->frame_callback) { |
| 88 | wl_callback_destroy(resizor->frame_callback); |
| 89 | resizor->frame_callback = NULL; |
| 90 | } |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 91 | |
| 92 | if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) { |
| 93 | resizor->frame_callback = |
| 94 | wl_surface_frame( |
| 95 | window_get_wl_surface(resizor->window)); |
| 96 | wl_callback_add_listener(resizor->frame_callback, &listener, |
| 97 | resizor); |
| 98 | } |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 101 | static const struct wl_callback_listener listener = { |
| 102 | frame_callback |
| 103 | }; |
| 104 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 105 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 106 | redraw_handler(struct widget *widget, void *data) |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 107 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 108 | struct resizor *resizor = data; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 109 | cairo_surface_t *surface; |
| 110 | cairo_t *cr; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 111 | struct rectangle allocation; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 112 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 113 | widget_get_allocation(resizor->widget, &allocation); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 114 | |
| 115 | surface = window_get_surface(resizor->window); |
| 116 | |
| 117 | cr = cairo_create(surface); |
| 118 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 119 | cairo_rectangle(cr, |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 120 | allocation.x, |
| 121 | allocation.y, |
| 122 | allocation.width, |
| 123 | allocation.height); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 124 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 125 | cairo_fill(cr); |
| 126 | cairo_destroy(cr); |
| 127 | |
| 128 | cairo_surface_destroy(surface); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 132 | keyboard_focus_handler(struct window *window, |
| 133 | struct input *device, void *data) |
| 134 | { |
| 135 | struct resizor *resizor = data; |
| 136 | |
| 137 | window_schedule_redraw(resizor->window); |
| 138 | } |
| 139 | |
| 140 | static void |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 141 | key_handler(struct window *window, struct input *input, uint32_t time, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 142 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 143 | void *data) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 144 | { |
| 145 | struct resizor *resizor = data; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 146 | struct rectangle allocation; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 147 | |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 148 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 149 | return; |
| 150 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 151 | window_get_allocation(resizor->window, &allocation); |
| 152 | resizor->width.current = allocation.width; |
| 153 | if (spring_done(&resizor->width)) |
| 154 | resizor->width.target = allocation.width; |
| 155 | resizor->height.current = allocation.height; |
| 156 | if (spring_done(&resizor->height)) |
| 157 | resizor->height.target = allocation.height; |
| 158 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 159 | switch (sym) { |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 160 | case XKB_KEY_Up: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 161 | if (allocation.height < 400) |
| 162 | break; |
| 163 | |
| 164 | resizor->height.target = allocation.height - 200; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 165 | break; |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 166 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 167 | case XKB_KEY_Down: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 168 | if (allocation.height > 1000) |
| 169 | break; |
| 170 | |
| 171 | resizor->height.target = allocation.height + 200; |
| 172 | break; |
| 173 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 174 | case XKB_KEY_Left: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 175 | if (allocation.width < 400) |
| 176 | break; |
| 177 | |
| 178 | resizor->width.target = allocation.width - 200; |
| 179 | break; |
| 180 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 181 | case XKB_KEY_Right: |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 182 | if (allocation.width > 1000) |
| 183 | break; |
| 184 | |
| 185 | resizor->width.target = allocation.width + 200; |
| 186 | break; |
| 187 | |
Kristian Høgsberg | bef52d1 | 2012-05-11 11:24:29 -0400 | [diff] [blame] | 188 | case XKB_KEY_Escape: |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 189 | display_exit(resizor->display); |
| 190 | break; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 191 | } |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 192 | |
| 193 | if (!resizor->frame_callback) |
| 194 | frame_callback(resizor, NULL, 0); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 197 | static void |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 198 | menu_func(struct window *window, int index, void *user_data) |
| 199 | { |
| 200 | fprintf(stderr, "picked entry %d\n", index); |
| 201 | } |
| 202 | |
| 203 | static void |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 204 | show_menu(struct resizor *resizor, struct input *input, uint32_t time) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 205 | { |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 206 | int32_t x, y; |
| 207 | static const char *entries[] = { |
| 208 | "Roy", "Pris", "Leon", "Zhora" |
| 209 | }; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 210 | |
| 211 | input_get_position(input, &x, &y); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 212 | window_show_menu(resizor->display, input, time, resizor->window, |
| 213 | x - 10, y - 10, menu_func, entries, 4); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 217 | button_handler(struct widget *widget, |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 218 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 219 | uint32_t button, enum wl_pointer_button_state state, void *data) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 220 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 221 | struct resizor *resizor = data; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 222 | |
| 223 | switch (button) { |
Kristian Høgsberg | 8da0fbd | 2011-12-19 15:36:10 -0500 | [diff] [blame] | 224 | case BTN_RIGHT: |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 225 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 226 | show_menu(resizor, input, time); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 231 | static struct resizor * |
| 232 | resizor_create(struct display *display) |
| 233 | { |
| 234 | struct resizor *resizor; |
| 235 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 236 | resizor = xzalloc(sizeof *resizor); |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 237 | resizor->window = window_create(display); |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame^] | 238 | resizor->widget = window_frame_create(resizor->window, resizor); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 239 | window_set_title(resizor->window, "Wayland Resizor"); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 240 | resizor->display = display; |
| 241 | |
| 242 | window_set_key_handler(resizor->window, key_handler); |
| 243 | window_set_user_data(resizor->window, resizor); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 244 | widget_set_redraw_handler(resizor->widget, redraw_handler); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 245 | window_set_keyboard_focus_handler(resizor->window, |
| 246 | keyboard_focus_handler); |
| 247 | |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 248 | widget_set_button_handler(resizor->widget, button_handler); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 249 | |
Kristian Høgsberg | d174521 | 2012-05-10 23:10:54 -0400 | [diff] [blame] | 250 | resizor->height.previous = 400; |
| 251 | resizor->height.current = 400; |
| 252 | resizor->height.target = 400; |
| 253 | |
| 254 | resizor->width.previous = 400; |
| 255 | resizor->width.current = 400; |
| 256 | resizor->width.target = 400; |
| 257 | |
| 258 | widget_schedule_resize(resizor->widget, 400, 400); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 259 | |
| 260 | return resizor; |
| 261 | } |
| 262 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 263 | static void |
| 264 | resizor_destroy(struct resizor *resizor) |
| 265 | { |
| 266 | if (resizor->frame_callback) |
| 267 | wl_callback_destroy(resizor->frame_callback); |
| 268 | |
Pekka Paalanen | 84d62dc | 2012-01-19 14:21:35 +0200 | [diff] [blame] | 269 | widget_destroy(resizor->widget); |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 270 | window_destroy(resizor->window); |
| 271 | free(resizor); |
| 272 | } |
| 273 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 274 | int |
| 275 | main(int argc, char *argv[]) |
| 276 | { |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 277 | struct display *display; |
| 278 | struct resizor *resizor; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 279 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 280 | display = display_create(&argc, argv); |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 281 | if (display == NULL) { |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 282 | fprintf(stderr, "failed to create display: %m\n"); |
| 283 | return -1; |
| 284 | } |
| 285 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 286 | resizor = resizor_create(display); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 287 | |
Pekka Paalanen | e59d74a | 2011-12-15 15:26:29 +0200 | [diff] [blame] | 288 | display_run(display); |
| 289 | |
| 290 | resizor_destroy(resizor); |
| 291 | display_destroy(display); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 292 | |
| 293 | return 0; |
| 294 | } |