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
31 #include <linux/input.h>
32 #include <wayland-client.h>
43 struct display *display;
44 struct window *window;
45 struct widget *widget;
49 struct wl_callback *frame_callback;
53 spring_update(struct spring *spring)
55 double current, force;
57 current = spring->current;
58 force = (spring->target - current) / 20.0 +
59 (spring->previous - current);
61 spring->current = current + (current - spring->previous) + force;
62 spring->previous = current;
66 spring_done(struct spring *spring)
68 return fabs(spring->previous - spring->target) < 0.1;
71 static const struct wl_callback_listener listener;
74 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
76 struct resizor *resizor = data;
78 assert(!callback || callback == resizor->frame_callback);
80 spring_update(&resizor->width);
81 spring_update(&resizor->height);
83 widget_schedule_resize(resizor->widget,
84 resizor->width.current + 0.5,
85 resizor->height.current + 0.5);
87 if (resizor->frame_callback) {
88 wl_callback_destroy(resizor->frame_callback);
89 resizor->frame_callback = NULL;
92 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
93 resizor->frame_callback =
95 window_get_wl_surface(resizor->window));
96 wl_callback_add_listener(resizor->frame_callback, &listener,
101 static const struct wl_callback_listener listener = {
106 redraw_handler(struct widget *widget, void *data)
108 struct resizor *resizor = data;
109 cairo_surface_t *surface;
111 struct rectangle allocation;
113 widget_get_allocation(resizor->widget, &allocation);
115 surface = window_get_surface(resizor->window);
117 cr = cairo_create(surface);
118 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
124 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
128 cairo_surface_destroy(surface);
132 keyboard_focus_handler(struct window *window,
133 struct input *device, void *data)
135 struct resizor *resizor = data;
137 window_schedule_redraw(resizor->window);
141 key_handler(struct window *window, struct input *input, uint32_t time,
142 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
145 struct resizor *resizor = data;
146 struct rectangle allocation;
148 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
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;
161 if (allocation.height < 400)
164 resizor->height.target = allocation.height - 200;
168 if (allocation.height > 1000)
171 resizor->height.target = allocation.height + 200;
175 if (allocation.width < 400)
178 resizor->width.target = allocation.width - 200;
182 if (allocation.width > 1000)
185 resizor->width.target = allocation.width + 200;
189 display_exit(resizor->display);
193 if (!resizor->frame_callback)
194 frame_callback(resizor, NULL, 0);
198 menu_func(struct window *window, int index, void *user_data)
200 fprintf(stderr, "picked entry %d\n", index);
204 show_menu(struct resizor *resizor, struct input *input, uint32_t time)
207 static const char *entries[] = {
208 "Roy", "Pris", "Leon", "Zhora"
211 input_get_position(input, &x, &y);
212 window_show_menu(resizor->display, input, time, resizor->window,
213 x - 10, y - 10, menu_func, entries, 4);
217 button_handler(struct widget *widget,
218 struct input *input, uint32_t time,
219 uint32_t button, enum wl_pointer_button_state state, void *data)
221 struct resizor *resizor = data;
225 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
226 show_menu(resizor, input, time);
231 static struct resizor *
232 resizor_create(struct display *display)
234 struct resizor *resizor;
236 resizor = xzalloc(sizeof *resizor);
237 resizor->window = window_create(display);
238 resizor->widget = frame_create(resizor->window, resizor);
239 window_set_title(resizor->window, "Wayland Resizor");
240 resizor->display = display;
242 window_set_key_handler(resizor->window, key_handler);
243 window_set_user_data(resizor->window, resizor);
244 widget_set_redraw_handler(resizor->widget, redraw_handler);
245 window_set_keyboard_focus_handler(resizor->window,
246 keyboard_focus_handler);
248 widget_set_button_handler(resizor->widget, button_handler);
250 resizor->height.previous = 400;
251 resizor->height.current = 400;
252 resizor->height.target = 400;
254 resizor->width.previous = 400;
255 resizor->width.current = 400;
256 resizor->width.target = 400;
258 widget_schedule_resize(resizor->widget, 400, 400);
264 resizor_destroy(struct resizor *resizor)
266 if (resizor->frame_callback)
267 wl_callback_destroy(resizor->frame_callback);
269 widget_destroy(resizor->widget);
270 window_destroy(resizor->window);
275 main(int argc, char *argv[])
277 struct display *display;
278 struct resizor *resizor;
280 display = display_create(&argc, argv);
281 if (display == NULL) {
282 fprintf(stderr, "failed to create display: %m\n");
286 resizor = resizor_create(display);
288 display_run(display);
290 resizor_destroy(resizor);
291 display_destroy(display);