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