blob: b5ea55f698e7c282d75b84c8323eb1c98dd640e1 [file] [log] [blame]
Kristian Høgsberg7c221d22010-12-16 13:35:23 -05001/*
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øgsberg5c4056e2010-12-16 14:56:41 -050028#include <math.h>
Pekka Paalanene59d74a2011-12-15 15:26:29 +020029#include <assert.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050030
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -050031#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050033
34#include "window.h"
35
Kristian Høgsbergd1745212012-05-10 23:10:54 -040036struct spring {
37 double current;
38 double target;
39 double previous;
40};
41
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050042struct resizor {
43 struct display *display;
44 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050045 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050046 struct window *menu;
Kristian Høgsbergd1745212012-05-10 23:10:54 -040047 struct spring width;
48 struct spring height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020049 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050050};
51
52static void
Kristian Høgsbergd1745212012-05-10 23:10:54 -040053spring_update(struct spring *spring)
54{
55 double current, force;
56
57 current = spring->current;
58 force = (spring->target - current) / 20.0 +
59 (spring->previous - current);
60
61 spring->current = current + (current - spring->previous) + force;
62 spring->previous = current;
63}
64
65static int
66spring_done(struct spring *spring)
67{
68 return fabs(spring->previous - spring->target) < 0.1;
69}
70
71static const struct wl_callback_listener listener;
72
73static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040074frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050075{
76 struct resizor *resizor = data;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050077
Pekka Paalanene59d74a2011-12-15 15:26:29 +020078 assert(!callback || callback == resizor->frame_callback);
79
Kristian Høgsberg6f394d52014-01-17 15:31:33 -080080 if (resizor->frame_callback) {
81 wl_callback_destroy(resizor->frame_callback);
82 resizor->frame_callback = NULL;
83 }
84
85 if (window_is_maximized(resizor->window))
86 return;
87
Kristian Høgsbergd1745212012-05-10 23:10:54 -040088 spring_update(&resizor->width);
89 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050090
Kristian Høgsbergd1745212012-05-10 23:10:54 -040091 widget_schedule_resize(resizor->widget,
92 resizor->width.current + 0.5,
93 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040094
Kristian Høgsbergd1745212012-05-10 23:10:54 -040095 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
96 resizor->frame_callback =
97 wl_surface_frame(
98 window_get_wl_surface(resizor->window));
99 wl_callback_add_listener(resizor->frame_callback, &listener,
100 resizor);
101 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500102}
103
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500104static const struct wl_callback_listener listener = {
105 frame_callback
106};
107
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500108static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500109redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500110{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500111 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500112 cairo_surface_t *surface;
113 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500114 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500115
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500116 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500117
118 surface = window_get_surface(resizor->window);
119
120 cr = cairo_create(surface);
121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500123 allocation.x,
124 allocation.y,
125 allocation.width,
126 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500127 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
128 cairo_fill(cr);
129 cairo_destroy(cr);
130
131 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500132}
133
134static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500135keyboard_focus_handler(struct window *window,
136 struct input *device, void *data)
137{
138 struct resizor *resizor = data;
139
140 window_schedule_redraw(resizor->window);
141}
142
143static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500144key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100145 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
146 void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500147{
148 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400149 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500150
Daniel Stonec9785ea2012-05-30 16:31:52 +0100151 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500152 return;
153
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400154 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;
161
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500162 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400163 case XKB_KEY_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400164 if (allocation.height < 400)
165 break;
166
167 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500168 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400169
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400170 case XKB_KEY_Down:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400171 if (allocation.height > 1000)
172 break;
173
174 resizor->height.target = allocation.height + 200;
175 break;
176
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400177 case XKB_KEY_Left:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400178 if (allocation.width < 400)
179 break;
180
181 resizor->width.target = allocation.width - 200;
182 break;
183
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400184 case XKB_KEY_Right:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400185 if (allocation.width > 1000)
186 break;
187
188 resizor->width.target = allocation.width + 200;
189 break;
190
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400191 case XKB_KEY_Escape:
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200192 display_exit(resizor->display);
193 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500194 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400195
196 if (!resizor->frame_callback)
197 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500198}
199
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500200static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -0700201menu_func(struct window *window,
202 struct input *input, int index, void *user_data)
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500203{
204 fprintf(stderr, "picked entry %d\n", index);
205}
206
207static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500208show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500209{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500210 int32_t x, y;
211 static const char *entries[] = {
212 "Roy", "Pris", "Leon", "Zhora"
213 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500214
215 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200216 window_show_menu(resizor->display, input, time, resizor->window,
217 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500218}
219
220static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500221button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500222 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100223 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500225 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226
227 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500228 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100229 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500230 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500231 break;
232 }
233}
234
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500235static struct resizor *
236resizor_create(struct display *display)
237{
238 struct resizor *resizor;
239
Peter Huttererf3d62272013-08-08 11:57:05 +1000240 resizor = xzalloc(sizeof *resizor);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500241 resizor->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500242 resizor->widget = window_frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500243 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500244 resizor->display = display;
245
246 window_set_key_handler(resizor->window, key_handler);
247 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500248 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500249 window_set_keyboard_focus_handler(resizor->window,
250 keyboard_focus_handler);
251
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500252 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500253
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400254 resizor->height.previous = 400;
255 resizor->height.current = 400;
256 resizor->height.target = 400;
257
258 resizor->width.previous = 400;
259 resizor->width.current = 400;
260 resizor->width.target = 400;
261
262 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500263
264 return resizor;
265}
266
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200267static void
268resizor_destroy(struct resizor *resizor)
269{
270 if (resizor->frame_callback)
271 wl_callback_destroy(resizor->frame_callback);
272
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200273 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200274 window_destroy(resizor->window);
275 free(resizor);
276}
277
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500278int
279main(int argc, char *argv[])
280{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200281 struct display *display;
282 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500283
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500284 display = display_create(&argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200285 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500286 fprintf(stderr, "failed to create display: %m\n");
287 return -1;
288 }
289
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200290 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500291
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200292 display_run(display);
293
294 resizor_destroy(resizor);
295 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500296
297 return 0;
298}