blob: ad3b71b42e46430aca5defd12e38c1e0bfe6dbc3 [file] [log] [blame]
Kristian Høgsberg7c221d22010-12-16 13:35:23 -05001/*
2 * Copyright © 2010 Intel Corporation
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050022 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <cairo.h>
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050031#include <math.h>
Pekka Paalanene59d74a2011-12-15 15:26:29 +020032#include <assert.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050033
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -050034#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050036
37#include "window.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070038#include "shared/xalloc.h"
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050039
Kristian Høgsbergd1745212012-05-10 23:10:54 -040040struct spring {
41 double current;
42 double target;
43 double previous;
44};
45
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050046struct resizor {
47 struct display *display;
48 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050049 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050050 struct window *menu;
Kristian Høgsbergd1745212012-05-10 23:10:54 -040051 struct spring width;
52 struct spring height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020053 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050054};
55
56static void
Kristian Høgsbergd1745212012-05-10 23:10:54 -040057spring_update(struct spring *spring)
58{
59 double current, force;
60
61 current = spring->current;
62 force = (spring->target - current) / 20.0 +
63 (spring->previous - current);
64
65 spring->current = current + (current - spring->previous) + force;
66 spring->previous = current;
67}
68
69static int
70spring_done(struct spring *spring)
71{
72 return fabs(spring->previous - spring->target) < 0.1;
73}
74
75static const struct wl_callback_listener listener;
76
77static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040078frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050079{
80 struct resizor *resizor = data;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050081
Pekka Paalanene59d74a2011-12-15 15:26:29 +020082 assert(!callback || callback == resizor->frame_callback);
83
Kristian Høgsberg6f394d52014-01-17 15:31:33 -080084 if (resizor->frame_callback) {
85 wl_callback_destroy(resizor->frame_callback);
86 resizor->frame_callback = NULL;
87 }
88
89 if (window_is_maximized(resizor->window))
90 return;
91
Kristian Høgsbergd1745212012-05-10 23:10:54 -040092 spring_update(&resizor->width);
93 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050094
Kristian Høgsbergd1745212012-05-10 23:10:54 -040095 widget_schedule_resize(resizor->widget,
96 resizor->width.current + 0.5,
97 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040098
Kristian Høgsbergd1745212012-05-10 23:10:54 -040099 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
100 resizor->frame_callback =
101 wl_surface_frame(
102 window_get_wl_surface(resizor->window));
103 wl_callback_add_listener(resizor->frame_callback, &listener,
104 resizor);
105 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500106}
107
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500108static const struct wl_callback_listener listener = {
109 frame_callback
110};
111
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500112static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500113redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500114{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500115 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500116 cairo_surface_t *surface;
117 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500118 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500119
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500120 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500121
122 surface = window_get_surface(resizor->window);
123
124 cr = cairo_create(surface);
125 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
126 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500127 allocation.x,
128 allocation.y,
129 allocation.width,
130 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500131 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
132 cairo_fill(cr);
133 cairo_destroy(cr);
134
135 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500136}
137
138static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500139keyboard_focus_handler(struct window *window,
140 struct input *device, void *data)
141{
142 struct resizor *resizor = data;
143
144 window_schedule_redraw(resizor->window);
145}
146
147static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500148key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100149 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
150 void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500151{
152 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400153 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500154
Daniel Stonec9785ea2012-05-30 16:31:52 +0100155 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500156 return;
157
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400158 window_get_allocation(resizor->window, &allocation);
159 resizor->width.current = allocation.width;
160 if (spring_done(&resizor->width))
161 resizor->width.target = allocation.width;
162 resizor->height.current = allocation.height;
163 if (spring_done(&resizor->height))
164 resizor->height.target = allocation.height;
165
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500166 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400167 case XKB_KEY_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400168 if (allocation.height < 400)
169 break;
170
171 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500172 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400173
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400174 case XKB_KEY_Down:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400175 if (allocation.height > 1000)
176 break;
177
178 resizor->height.target = allocation.height + 200;
179 break;
180
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400181 case XKB_KEY_Left:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400182 if (allocation.width < 400)
183 break;
184
185 resizor->width.target = allocation.width - 200;
186 break;
187
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400188 case XKB_KEY_Right:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400189 if (allocation.width > 1000)
190 break;
191
192 resizor->width.target = allocation.width + 200;
193 break;
194
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400195 case XKB_KEY_Escape:
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200196 display_exit(resizor->display);
197 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500198 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400199
200 if (!resizor->frame_callback)
201 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500202}
203
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500204static void
Jasper St. Pierredda93132014-03-13 12:06:00 -0400205menu_func(void *data, struct input *input, int index)
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500206{
207 fprintf(stderr, "picked entry %d\n", index);
208}
209
210static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500211show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500212{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500213 int32_t x, y;
214 static const char *entries[] = {
215 "Roy", "Pris", "Leon", "Zhora"
216 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500217
218 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200219 window_show_menu(resizor->display, input, time, resizor->window,
220 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221}
222
223static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500224button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500225 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100226 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500227{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500228 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229
230 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500231 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100232 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500233 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500234 break;
235 }
236}
237
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500238static struct resizor *
239resizor_create(struct display *display)
240{
241 struct resizor *resizor;
242
Peter Huttererf3d62272013-08-08 11:57:05 +1000243 resizor = xzalloc(sizeof *resizor);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500244 resizor->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500245 resizor->widget = window_frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500246 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500247 resizor->display = display;
248
249 window_set_key_handler(resizor->window, key_handler);
250 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500251 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500252 window_set_keyboard_focus_handler(resizor->window,
253 keyboard_focus_handler);
254
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500255 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500256
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400257 resizor->height.previous = 400;
258 resizor->height.current = 400;
259 resizor->height.target = 400;
260
261 resizor->width.previous = 400;
262 resizor->width.current = 400;
263 resizor->width.target = 400;
264
265 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500266
267 return resizor;
268}
269
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200270static void
271resizor_destroy(struct resizor *resizor)
272{
273 if (resizor->frame_callback)
274 wl_callback_destroy(resizor->frame_callback);
275
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200276 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200277 window_destroy(resizor->window);
278 free(resizor);
279}
280
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500281int
282main(int argc, char *argv[])
283{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200284 struct display *display;
285 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500286
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500287 display = display_create(&argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200288 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500289 fprintf(stderr, "failed to create display: %m\n");
290 return -1;
291 }
292
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200293 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500294
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200295 display_run(display);
296
297 resizor_destroy(resizor);
298 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500299
300 return 0;
301}