2 * Copyright © 2010 Intel Corporation
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.
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
33 #include <linux/input.h>
34 #include <wayland-client.h>
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
51 struct wl_callback *frame_callback;
55 spring_update(struct spring *spring)
57 double current, force;
59 current = spring->current;
60 force = (spring->target - current) / 20.0 +
61 (spring->previous - current);
63 spring->current = current + (current - spring->previous) + force;
64 spring->previous = current;
68 spring_done(struct spring *spring)
70 return fabs(spring->previous - spring->target) < 0.1;
73 static const struct wl_callback_listener listener;
76 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
78 struct resizor *resizor = data;
80 assert(!callback || callback == resizor->frame_callback);
82 if (resizor->frame_callback) {
83 wl_callback_destroy(resizor->frame_callback);
84 resizor->frame_callback = NULL;
87 if (window_is_maximized(resizor->window))
90 spring_update(&resizor->width);
91 spring_update(&resizor->height);
93 widget_schedule_resize(resizor->widget,
94 resizor->width.current + 0.5,
95 resizor->height.current + 0.5);
97 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
98 resizor->frame_callback =
100 window_get_wl_surface(resizor->window));
101 wl_callback_add_listener(resizor->frame_callback, &listener,
106 static const struct wl_callback_listener listener = {
111 redraw_handler(struct widget *widget, void *data)
113 struct resizor *resizor = data;
114 cairo_surface_t *surface;
116 struct rectangle allocation;
118 widget_get_allocation(resizor->widget, &allocation);
120 surface = window_get_surface(resizor->window);
122 cr = cairo_create(surface);
123 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
129 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
133 cairo_surface_destroy(surface);
137 keyboard_focus_handler(struct window *window,
138 struct input *device, void *data)
140 struct resizor *resizor = data;
142 window_schedule_redraw(resizor->window);
146 key_handler(struct window *window, struct input *input, uint32_t time,
147 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
150 struct resizor *resizor = data;
151 struct rectangle allocation;
153 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
156 window_get_allocation(resizor->window, &allocation);
157 resizor->width.current = allocation.width;
158 if (spring_done(&resizor->width))
159 resizor->width.target = allocation.width;
160 resizor->height.current = allocation.height;
161 if (spring_done(&resizor->height))
162 resizor->height.target = allocation.height;
166 if (allocation.height < 400)
169 resizor->height.target = allocation.height - 200;
173 if (allocation.height > 1000)
176 resizor->height.target = allocation.height + 200;
180 if (allocation.width < 400)
183 resizor->width.target = allocation.width - 200;
187 if (allocation.width > 1000)
190 resizor->width.target = allocation.width + 200;
194 display_exit(resizor->display);
198 if (!resizor->frame_callback)
199 frame_callback(resizor, NULL, 0);
203 menu_func(void *data, struct input *input, int index)
205 fprintf(stderr, "picked entry %d\n", index);
209 show_menu(struct resizor *resizor, struct input *input, uint32_t time)
212 static const char *entries[] = {
213 "Roy", "Pris", "Leon", "Zhora"
216 input_get_position(input, &x, &y);
217 window_show_menu(resizor->display, input, time, resizor->window,
218 x - 10, y - 10, menu_func, entries, 4);
222 button_handler(struct widget *widget,
223 struct input *input, uint32_t time,
224 uint32_t button, enum wl_pointer_button_state state, void *data)
226 struct resizor *resizor = data;
230 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
231 show_menu(resizor, input, time);
236 static struct resizor *
237 resizor_create(struct display *display)
239 struct resizor *resizor;
241 resizor = xzalloc(sizeof *resizor);
242 resizor->window = window_create(display);
243 resizor->widget = window_frame_create(resizor->window, resizor);
244 window_set_title(resizor->window, "Wayland Resizor");
245 resizor->display = display;
247 window_set_key_handler(resizor->window, key_handler);
248 window_set_user_data(resizor->window, resizor);
249 widget_set_redraw_handler(resizor->widget, redraw_handler);
250 window_set_keyboard_focus_handler(resizor->window,
251 keyboard_focus_handler);
253 widget_set_button_handler(resizor->widget, button_handler);
255 resizor->height.previous = 400;
256 resizor->height.current = 400;
257 resizor->height.target = 400;
259 resizor->width.previous = 400;
260 resizor->width.current = 400;
261 resizor->width.target = 400;
263 widget_schedule_resize(resizor->widget, 400, 400);
269 resizor_destroy(struct resizor *resizor)
271 if (resizor->frame_callback)
272 wl_callback_destroy(resizor->frame_callback);
274 widget_destroy(resizor->widget);
275 window_destroy(resizor->window);
280 main(int argc, char *argv[])
282 struct display *display;
283 struct resizor *resizor;
285 display = display_create(&argc, argv);
286 if (display == NULL) {
287 fprintf(stderr, "failed to create display: %m\n");
291 resizor = resizor_create(display);
293 display_run(display);
295 resizor_destroy(resizor);
296 display_destroy(display);