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