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> |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 29 | |
| 30 | #include "wayland-util.h" |
| 31 | #include "wayland-client.h" |
| 32 | #include "wayland-glib.h" |
| 33 | |
| 34 | #include "window.h" |
| 35 | |
| 36 | #include <X11/keysym.h> |
| 37 | |
| 38 | struct resizor { |
| 39 | struct display *display; |
| 40 | struct window *window; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 41 | struct window *menu; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 42 | int32_t width; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 43 | |
| 44 | struct { |
| 45 | double current; |
| 46 | double target; |
| 47 | double previous; |
| 48 | } height; |
| 49 | }; |
| 50 | |
| 51 | static void |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame^] | 52 | frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 53 | { |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame^] | 54 | static const struct wl_callback_listener listener = { |
| 55 | frame_callback |
| 56 | }; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 57 | struct resizor *resizor = data; |
| 58 | double force, height; |
| 59 | |
| 60 | height = resizor->height.current; |
| 61 | force = (resizor->height.target - height) / 10.0 + |
| 62 | (resizor->height.previous - height); |
| 63 | |
| 64 | resizor->height.current = |
| 65 | height + (height - resizor->height.previous) + force; |
| 66 | resizor->height.previous = height; |
| 67 | |
| 68 | if (resizor->height.current >= 400) { |
| 69 | resizor->height.current = 400; |
| 70 | resizor->height.previous = 400; |
| 71 | } |
| 72 | |
| 73 | if (resizor->height.current <= 200) { |
| 74 | resizor->height.current = 200; |
| 75 | resizor->height.previous = 200; |
| 76 | } |
| 77 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 78 | window_set_child_size(resizor->window, resizor->width, height + 0.5); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 79 | |
Kristian Høgsberg | 5c4056e | 2010-12-16 14:56:41 -0500 | [diff] [blame] | 80 | window_schedule_redraw(resizor->window); |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame^] | 81 | |
| 82 | if (callback) |
| 83 | wl_callback_destroy(callback); |
| 84 | |
| 85 | if (fabs(resizor->height.previous - resizor->height.target) > 0.1) { |
| 86 | callback = wl_surface_frame(window_get_wl_surface(resizor->window)); |
| 87 | wl_callback_add_listener(callback, &listener, resizor); |
| 88 | } |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 92 | resizor_draw(struct resizor *resizor) |
| 93 | { |
| 94 | cairo_surface_t *surface; |
| 95 | cairo_t *cr; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 96 | struct rectangle allocation; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 97 | |
| 98 | window_draw(resizor->window); |
| 99 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 100 | window_get_child_allocation(resizor->window, &allocation); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 101 | |
| 102 | surface = window_get_surface(resizor->window); |
| 103 | |
| 104 | cr = cairo_create(surface); |
| 105 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 106 | cairo_rectangle(cr, |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 107 | allocation.x, |
| 108 | allocation.y, |
| 109 | allocation.width, |
| 110 | allocation.height); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 111 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 112 | cairo_fill(cr); |
| 113 | cairo_destroy(cr); |
| 114 | |
| 115 | cairo_surface_destroy(surface); |
| 116 | |
| 117 | window_flush(resizor->window); |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void |
| 121 | redraw_handler(struct window *window, void *data) |
| 122 | { |
| 123 | struct resizor *resizor = data; |
| 124 | |
| 125 | resizor_draw(resizor); |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | keyboard_focus_handler(struct window *window, |
| 130 | struct input *device, void *data) |
| 131 | { |
| 132 | struct resizor *resizor = data; |
| 133 | |
| 134 | window_schedule_redraw(resizor->window); |
| 135 | } |
| 136 | |
| 137 | static void |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 138 | key_handler(struct window *window, struct input *input, uint32_t time, |
| 139 | uint32_t key, uint32_t sym, uint32_t state, void *data) |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 140 | { |
| 141 | struct resizor *resizor = data; |
| 142 | |
| 143 | if (state == 0) |
| 144 | return; |
| 145 | |
| 146 | switch (sym) { |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 147 | case XK_Down: |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 148 | resizor->height.target = 400; |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame^] | 149 | frame_callback(resizor, NULL, 0); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 150 | break; |
Kristian Høgsberg | 53a7f21 | 2010-12-16 21:11:10 -0500 | [diff] [blame] | 151 | case XK_Up: |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 152 | resizor->height.target = 200; |
Kristian Høgsberg | a8d1fa7 | 2011-08-23 18:14:06 -0400 | [diff] [blame^] | 153 | frame_callback(resizor, NULL, 0); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 158 | static void |
| 159 | show_menu(struct resizor *resizor, struct input *input) |
| 160 | { |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 161 | int32_t x, y, width = 200, height = 200; |
| 162 | |
| 163 | input_get_position(input, &x, &y); |
| 164 | resizor->menu = window_create_transient(resizor->display, |
| 165 | resizor->window, |
| 166 | x - 10, y - 10, width, height); |
| 167 | |
| 168 | window_draw(resizor->menu); |
| 169 | window_flush(resizor->menu); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | button_handler(struct window *window, |
| 174 | struct input *input, uint32_t time, |
| 175 | int button, int state, void *data) |
| 176 | { |
| 177 | struct resizor *resizor = data; |
| 178 | |
| 179 | switch (button) { |
| 180 | case 274: |
| 181 | if (state) |
| 182 | show_menu(resizor, input); |
| 183 | else |
| 184 | window_destroy(resizor->menu); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 189 | static struct resizor * |
| 190 | resizor_create(struct display *display) |
| 191 | { |
| 192 | struct resizor *resizor; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 193 | int32_t height; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 194 | |
| 195 | resizor = malloc(sizeof *resizor); |
| 196 | if (resizor == NULL) |
| 197 | return resizor; |
| 198 | memset(resizor, 0, sizeof *resizor); |
| 199 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 200 | resizor->window = window_create(display, 500, 400); |
| 201 | window_set_title(resizor->window, "Wayland Resizor"); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 202 | resizor->display = display; |
| 203 | |
| 204 | window_set_key_handler(resizor->window, key_handler); |
| 205 | window_set_user_data(resizor->window, resizor); |
| 206 | window_set_redraw_handler(resizor->window, redraw_handler); |
| 207 | window_set_keyboard_focus_handler(resizor->window, |
| 208 | keyboard_focus_handler); |
| 209 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 210 | resizor->width = 300; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 211 | resizor->height.current = 400; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 212 | resizor->height.previous = resizor->height.current; |
| 213 | resizor->height.target = resizor->height.current; |
| 214 | height = resizor->height.current + 0.5; |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 215 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 216 | window_set_child_size(resizor->window, resizor->width, height); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 217 | window_set_button_handler(resizor->window, button_handler); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 218 | |
| 219 | resizor_draw(resizor); |
| 220 | |
| 221 | return resizor; |
| 222 | } |
| 223 | |
| 224 | int |
| 225 | main(int argc, char *argv[]) |
| 226 | { |
| 227 | struct display *d; |
| 228 | |
Tim Wiederhake | b4b6734 | 2011-04-11 13:16:33 -0400 | [diff] [blame] | 229 | d = display_create(&argc, &argv, NULL, NULL); |
Kristian Høgsberg | 7c221d2 | 2010-12-16 13:35:23 -0500 | [diff] [blame] | 230 | if (d == NULL) { |
| 231 | fprintf(stderr, "failed to create display: %m\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | resizor_create (d); |
| 236 | |
| 237 | display_run(d); |
| 238 | |
| 239 | return 0; |
| 240 | } |