blob: d205eb6c1f76b2c28a1cef17bab586d0c1a91550 [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"
38
Kristian Høgsbergd1745212012-05-10 23:10:54 -040039struct spring {
40 double current;
41 double target;
42 double previous;
43};
44
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050045struct resizor {
46 struct display *display;
47 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050048 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050049 struct window *menu;
Kristian Høgsbergd1745212012-05-10 23:10:54 -040050 struct spring width;
51 struct spring height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020052 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050053};
54
55static void
Kristian Høgsbergd1745212012-05-10 23:10:54 -040056spring_update(struct spring *spring)
57{
58 double current, force;
59
60 current = spring->current;
61 force = (spring->target - current) / 20.0 +
62 (spring->previous - current);
63
64 spring->current = current + (current - spring->previous) + force;
65 spring->previous = current;
66}
67
68static int
69spring_done(struct spring *spring)
70{
71 return fabs(spring->previous - spring->target) < 0.1;
72}
73
74static const struct wl_callback_listener listener;
75
76static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040077frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050078{
79 struct resizor *resizor = data;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050080
Pekka Paalanene59d74a2011-12-15 15:26:29 +020081 assert(!callback || callback == resizor->frame_callback);
82
Kristian Høgsberg6f394d52014-01-17 15:31:33 -080083 if (resizor->frame_callback) {
84 wl_callback_destroy(resizor->frame_callback);
85 resizor->frame_callback = NULL;
86 }
87
88 if (window_is_maximized(resizor->window))
89 return;
90
Kristian Høgsbergd1745212012-05-10 23:10:54 -040091 spring_update(&resizor->width);
92 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050093
Kristian Høgsbergd1745212012-05-10 23:10:54 -040094 widget_schedule_resize(resizor->widget,
95 resizor->width.current + 0.5,
96 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040097
Kristian Høgsbergd1745212012-05-10 23:10:54 -040098 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
99 resizor->frame_callback =
100 wl_surface_frame(
101 window_get_wl_surface(resizor->window));
102 wl_callback_add_listener(resizor->frame_callback, &listener,
103 resizor);
104 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500105}
106
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500107static const struct wl_callback_listener listener = {
108 frame_callback
109};
110
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500111static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500112redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500113{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500114 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500115 cairo_surface_t *surface;
116 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500117 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500118
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500119 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500120
121 surface = window_get_surface(resizor->window);
122
123 cr = cairo_create(surface);
124 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
125 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500126 allocation.x,
127 allocation.y,
128 allocation.width,
129 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500130 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
131 cairo_fill(cr);
132 cairo_destroy(cr);
133
134 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500135}
136
137static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500138keyboard_focus_handler(struct window *window,
139 struct input *device, void *data)
140{
141 struct resizor *resizor = data;
142
143 window_schedule_redraw(resizor->window);
144}
145
146static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500147key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100148 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
149 void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500150{
151 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400152 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500153
Daniel Stonec9785ea2012-05-30 16:31:52 +0100154 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500155 return;
156
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400157 window_get_allocation(resizor->window, &allocation);
158 resizor->width.current = allocation.width;
159 if (spring_done(&resizor->width))
160 resizor->width.target = allocation.width;
161 resizor->height.current = allocation.height;
162 if (spring_done(&resizor->height))
163 resizor->height.target = allocation.height;
164
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500165 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400166 case XKB_KEY_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400167 if (allocation.height < 400)
168 break;
169
170 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500171 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400172
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400173 case XKB_KEY_Down:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400174 if (allocation.height > 1000)
175 break;
176
177 resizor->height.target = allocation.height + 200;
178 break;
179
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400180 case XKB_KEY_Left:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400181 if (allocation.width < 400)
182 break;
183
184 resizor->width.target = allocation.width - 200;
185 break;
186
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400187 case XKB_KEY_Right:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400188 if (allocation.width > 1000)
189 break;
190
191 resizor->width.target = allocation.width + 200;
192 break;
193
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400194 case XKB_KEY_Escape:
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200195 display_exit(resizor->display);
196 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500197 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400198
199 if (!resizor->frame_callback)
200 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500201}
202
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500203static void
Jasper St. Pierredda93132014-03-13 12:06:00 -0400204menu_func(void *data, struct input *input, int index)
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205{
206 fprintf(stderr, "picked entry %d\n", index);
207}
208
209static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500210show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500211{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500212 int32_t x, y;
213 static const char *entries[] = {
214 "Roy", "Pris", "Leon", "Zhora"
215 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500216
217 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200218 window_show_menu(resizor->display, input, time, resizor->window,
219 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220}
221
222static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500223button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100225 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500227 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228
229 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500230 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100231 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500232 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500233 break;
234 }
235}
236
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500237static struct resizor *
238resizor_create(struct display *display)
239{
240 struct resizor *resizor;
241
Peter Huttererf3d62272013-08-08 11:57:05 +1000242 resizor = xzalloc(sizeof *resizor);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500243 resizor->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500244 resizor->widget = window_frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500245 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500246 resizor->display = display;
247
248 window_set_key_handler(resizor->window, key_handler);
249 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500250 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500251 window_set_keyboard_focus_handler(resizor->window,
252 keyboard_focus_handler);
253
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500254 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500255
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400256 resizor->height.previous = 400;
257 resizor->height.current = 400;
258 resizor->height.target = 400;
259
260 resizor->width.previous = 400;
261 resizor->width.current = 400;
262 resizor->width.target = 400;
263
264 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500265
266 return resizor;
267}
268
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200269static void
270resizor_destroy(struct resizor *resizor)
271{
272 if (resizor->frame_callback)
273 wl_callback_destroy(resizor->frame_callback);
274
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200275 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200276 window_destroy(resizor->window);
277 free(resizor);
278}
279
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500280int
281main(int argc, char *argv[])
282{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200283 struct display *display;
284 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500285
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500286 display = display_create(&argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200287 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500288 fprintf(stderr, "failed to create display: %m\n");
289 return -1;
290 }
291
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200292 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500293
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200294 display_run(display);
295
296 resizor_destroy(resizor);
297 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500298
299 return 0;
300}