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 if (resizor->frame_callback) {
81 wl_callback_destroy(resizor->frame_callback);
82 resizor->frame_callback = NULL;
85 if (window_is_maximized(resizor->window))
88 spring_update(&resizor->width);
89 spring_update(&resizor->height);
91 widget_schedule_resize(resizor->widget,
92 resizor->width.current + 0.5,
93 resizor->height.current + 0.5);
95 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
96 resizor->frame_callback =
98 window_get_wl_surface(resizor->window));
99 wl_callback_add_listener(resizor->frame_callback, &listener,
104 static const struct wl_callback_listener listener = {
109 redraw_handler(struct widget *widget, void *data)
111 struct resizor *resizor = data;
112 cairo_surface_t *surface;
114 struct rectangle allocation;
116 widget_get_allocation(resizor->widget, &allocation);
118 surface = window_get_surface(resizor->window);
120 cr = cairo_create(surface);
121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
127 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
131 cairo_surface_destroy(surface);
135 keyboard_focus_handler(struct window *window,
136 struct input *device, void *data)
138 struct resizor *resizor = data;
140 window_schedule_redraw(resizor->window);
144 key_handler(struct window *window, struct input *input, uint32_t time,
145 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
148 struct resizor *resizor = data;
149 struct rectangle allocation;
151 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
154 window_get_allocation(resizor->window, &allocation);
155 resizor->width.current = allocation.width;
156 if (spring_done(&resizor->width))
157 resizor->width.target = allocation.width;
158 resizor->height.current = allocation.height;
159 if (spring_done(&resizor->height))
160 resizor->height.target = allocation.height;
164 if (allocation.height < 400)
167 resizor->height.target = allocation.height - 200;
171 if (allocation.height > 1000)
174 resizor->height.target = allocation.height + 200;
178 if (allocation.width < 400)
181 resizor->width.target = allocation.width - 200;
185 if (allocation.width > 1000)
188 resizor->width.target = allocation.width + 200;
192 display_exit(resizor->display);
196 if (!resizor->frame_callback)
197 frame_callback(resizor, NULL, 0);
201 menu_func(struct window *window,
202 struct input *input, int index, void *user_data)
204 fprintf(stderr, "picked entry %d\n", index);
208 show_menu(struct resizor *resizor, struct input *input, uint32_t time)
211 static const char *entries[] = {
212 "Roy", "Pris", "Leon", "Zhora"
215 input_get_position(input, &x, &y);
216 window_show_menu(resizor->display, input, time, resizor->window,
217 x - 10, y - 10, menu_func, entries, 4);
221 button_handler(struct widget *widget,
222 struct input *input, uint32_t time,
223 uint32_t button, enum wl_pointer_button_state state, void *data)
225 struct resizor *resizor = data;
229 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
230 show_menu(resizor, input, time);
235 static struct resizor *
236 resizor_create(struct display *display)
238 struct resizor *resizor;
240 resizor = xzalloc(sizeof *resizor);
241 resizor->window = window_create(display);
242 resizor->widget = window_frame_create(resizor->window, resizor);
243 window_set_title(resizor->window, "Wayland Resizor");
244 resizor->display = display;
246 window_set_key_handler(resizor->window, key_handler);
247 window_set_user_data(resizor->window, resizor);
248 widget_set_redraw_handler(resizor->widget, redraw_handler);
249 window_set_keyboard_focus_handler(resizor->window,
250 keyboard_focus_handler);
252 widget_set_button_handler(resizor->widget, button_handler);
254 resizor->height.previous = 400;
255 resizor->height.current = 400;
256 resizor->height.target = 400;
258 resizor->width.previous = 400;
259 resizor->width.current = 400;
260 resizor->width.target = 400;
262 widget_schedule_resize(resizor->widget, 400, 400);
268 resizor_destroy(struct resizor *resizor)
270 if (resizor->frame_callback)
271 wl_callback_destroy(resizor->frame_callback);
273 widget_destroy(resizor->widget);
274 window_destroy(resizor->window);
279 main(int argc, char *argv[])
281 struct display *display;
282 struct resizor *resizor;
284 display = display_create(&argc, argv);
285 if (display == NULL) {
286 fprintf(stderr, "failed to create display: %m\n");
290 resizor = resizor_create(display);
292 display_run(display);
294 resizor_destroy(resizor);
295 display_destroy(display);