blob: 5d342e1fc33f960a2ee1cf293730d3692f0fbc4d [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
Jonas Ådahl9f8c8942014-11-26 17:40:42 +080026#include <stdbool.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <cairo.h>
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050032#include <math.h>
Pekka Paalanene59d74a2011-12-15 15:26:29 +020033#include <assert.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050034
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -050035#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050037
38#include "window.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070039#include "shared/xalloc.h"
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050040
Kristian Høgsbergd1745212012-05-10 23:10:54 -040041struct spring {
42 double current;
43 double target;
44 double previous;
45};
46
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050047struct resizor {
48 struct display *display;
49 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050050 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050051 struct window *menu;
Kristian Høgsbergd1745212012-05-10 23:10:54 -040052 struct spring width;
53 struct spring height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020054 struct wl_callback *frame_callback;
Jonas Ådahl9f8c8942014-11-26 17:40:42 +080055 bool pointer_locked;
56 struct input *locked_input;
57 float pointer_x;
58 float pointer_y;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050059};
60
61static void
Kristian Høgsbergd1745212012-05-10 23:10:54 -040062spring_update(struct spring *spring)
63{
64 double current, force;
65
66 current = spring->current;
67 force = (spring->target - current) / 20.0 +
68 (spring->previous - current);
69
70 spring->current = current + (current - spring->previous) + force;
71 spring->previous = current;
72}
73
74static int
75spring_done(struct spring *spring)
76{
77 return fabs(spring->previous - spring->target) < 0.1;
78}
79
80static const struct wl_callback_listener listener;
81
82static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040083frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050084{
85 struct resizor *resizor = data;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050086
Pekka Paalanene59d74a2011-12-15 15:26:29 +020087 assert(!callback || callback == resizor->frame_callback);
88
Kristian Høgsberg6f394d52014-01-17 15:31:33 -080089 if (resizor->frame_callback) {
90 wl_callback_destroy(resizor->frame_callback);
91 resizor->frame_callback = NULL;
92 }
93
94 if (window_is_maximized(resizor->window))
95 return;
96
Kristian Høgsbergd1745212012-05-10 23:10:54 -040097 spring_update(&resizor->width);
98 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050099
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400100 widget_schedule_resize(resizor->widget,
101 resizor->width.current + 0.5,
102 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400103
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400104 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
105 resizor->frame_callback =
106 wl_surface_frame(
107 window_get_wl_surface(resizor->window));
108 wl_callback_add_listener(resizor->frame_callback, &listener,
109 resizor);
110 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500111}
112
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500113static const struct wl_callback_listener listener = {
114 frame_callback
115};
116
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500117static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500119{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500120 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500121 cairo_surface_t *surface;
122 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500123 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500124
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500125 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500126
127 surface = window_get_surface(resizor->window);
128
129 cr = cairo_create(surface);
130 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
131 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500132 allocation.x,
133 allocation.y,
134 allocation.width,
135 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500136 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
137 cairo_fill(cr);
138 cairo_destroy(cr);
139
140 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500141}
142
143static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500144keyboard_focus_handler(struct window *window,
145 struct input *device, void *data)
146{
147 struct resizor *resizor = data;
148
149 window_schedule_redraw(resizor->window);
150}
151
152static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500153key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100154 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
155 void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500156{
157 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400158 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500159
Daniel Stonec9785ea2012-05-30 16:31:52 +0100160 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500161 return;
162
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400163 window_get_allocation(resizor->window, &allocation);
164 resizor->width.current = allocation.width;
165 if (spring_done(&resizor->width))
166 resizor->width.target = allocation.width;
167 resizor->height.current = allocation.height;
168 if (spring_done(&resizor->height))
169 resizor->height.target = allocation.height;
170
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500171 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400172 case XKB_KEY_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400173 if (allocation.height < 400)
174 break;
175
176 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500177 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400178
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400179 case XKB_KEY_Down:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400180 if (allocation.height > 1000)
181 break;
182
183 resizor->height.target = allocation.height + 200;
184 break;
185
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400186 case XKB_KEY_Left:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400187 if (allocation.width < 400)
188 break;
189
190 resizor->width.target = allocation.width - 200;
191 break;
192
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400193 case XKB_KEY_Right:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400194 if (allocation.width > 1000)
195 break;
196
197 resizor->width.target = allocation.width + 200;
198 break;
199
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400200 case XKB_KEY_Escape:
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200201 display_exit(resizor->display);
202 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500203 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400204
205 if (!resizor->frame_callback)
206 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500207}
208
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500209static void
Jasper St. Pierredda93132014-03-13 12:06:00 -0400210menu_func(void *data, struct input *input, int index)
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500211{
212 fprintf(stderr, "picked entry %d\n", index);
213}
214
215static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500216show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500217{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500218 int32_t x, y;
219 static const char *entries[] = {
220 "Roy", "Pris", "Leon", "Zhora"
221 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500222
223 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200224 window_show_menu(resizor->display, input, time, resizor->window,
225 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226}
227
228static void
Jonas Ådahl9f8c8942014-11-26 17:40:42 +0800229locked_pointer_handle_motion(struct window *window,
230 struct input *input,
231 uint32_t time,
232 float dx,
233 float dy,
234 void *data)
235{
236 struct resizor *resizor = data;
237
238 resizor->width.current += dx;
239 resizor->width.previous = resizor->width.current;
240 resizor->width.target = resizor->width.current;
241
242 resizor->height.current += dy;
243 resizor->height.previous = resizor->height.current;
244 resizor->height.target = resizor->height.current;
245
246 widget_schedule_resize(resizor->widget,
247 resizor->width.current,
248 resizor->height.current);
249}
250
251static void
252handle_pointer_locked(struct window *window, struct input *input, void *data)
253{
254 struct resizor *resizor = data;
255
256 resizor->pointer_locked = true;
257 input_set_pointer_image(input, CURSOR_BLANK);
258}
259
260static void
261handle_pointer_unlocked(struct window *window, struct input *input, void *data)
262{
263 struct resizor *resizor = data;
264
265 resizor->pointer_locked = false;
266 input_set_pointer_image(input, CURSOR_LEFT_PTR);
267}
268
269static const struct wl_callback_listener locked_pointer_frame_listener;
270
271static void
272locked_pointer_frame_callback(void *data,
273 struct wl_callback *callback,
274 uint32_t time)
275{
276 struct resizor *resizor = data;
277 struct wl_surface *surface;
278 struct rectangle allocation;
279 float x, y;
280
281 if (resizor->pointer_locked) {
282 widget_get_allocation(resizor->widget, &allocation);
283
284 x = resizor->pointer_x + (allocation.width - allocation.x);
285 y = resizor->pointer_y + (allocation.height - allocation.y);
286
287 widget_set_locked_pointer_cursor_hint(resizor->widget, x, y);
288 }
289
290 wl_callback_destroy(callback);
291
292 surface = window_get_wl_surface(resizor->window);
293 callback = wl_surface_frame(surface);
294 wl_callback_add_listener(callback,
295 &locked_pointer_frame_listener,
296 resizor);
297}
298
299static const struct wl_callback_listener locked_pointer_frame_listener = {
300 locked_pointer_frame_callback
301};
302
303static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500304button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500305 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100306 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500307{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500308 struct resizor *resizor = data;
Jonas Ådahl9f8c8942014-11-26 17:40:42 +0800309 struct rectangle allocation;
310 struct wl_surface *surface;
311 struct wl_callback *callback;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500312
Jonas Ådahl9f8c8942014-11-26 17:40:42 +0800313 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
314 show_menu(resizor, input, time);
315 } else if (button == BTN_LEFT &&
316 state == WL_POINTER_BUTTON_STATE_PRESSED) {
317 window_get_allocation(resizor->window, &allocation);
318
319 resizor->width.current = allocation.width;
320 resizor->width.previous = allocation.width;
321 resizor->width.target = allocation.width;
322
323 resizor->height.current = allocation.height;
324 resizor->height.previous = allocation.height;
325 resizor->height.target = allocation.height;
326
327 window_lock_pointer(resizor->window, input);
328 window_set_pointer_locked_handler(resizor->window,
329 handle_pointer_locked,
330 handle_pointer_unlocked);
331 resizor->locked_input = input;
332
333 surface = window_get_wl_surface(resizor->window);
334 callback = wl_surface_frame(surface);
335 wl_callback_add_listener(callback,
336 &locked_pointer_frame_listener,
337 resizor);
338 } else if (button == BTN_LEFT &&
339 state == WL_POINTER_BUTTON_STATE_RELEASED) {
340 input_set_pointer_image(input, CURSOR_LEFT_PTR);
341 window_unlock_pointer(resizor->window);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500342 }
343}
344
Jonas Ådahl9f8c8942014-11-26 17:40:42 +0800345static void
346set_cursor_inv_offset(struct resizor *resizor, float x, float y)
347{
348 struct rectangle allocation;
349
350 widget_get_allocation(resizor->widget, &allocation);
351
352 resizor->pointer_x = x - (allocation.width - allocation.x);
353 resizor->pointer_y = y - (allocation.height - allocation.y);
354}
355
356static int
357enter_handler(struct widget *widget,
358 struct input *input,
359 float x, float y, void *data)
360{
361 struct resizor *resizor = data;
362
363 set_cursor_inv_offset(resizor, x , y);
364
365 return CURSOR_LEFT_PTR;
366}
367
368static int
369motion_handler(struct widget *widget,
370 struct input *input, uint32_t time,
371 float x, float y, void *data)
372{
373 struct resizor *resizor = data;
374
375 set_cursor_inv_offset(resizor, x , y);
376
377 return CURSOR_LEFT_PTR;
378}
379
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500380static struct resizor *
381resizor_create(struct display *display)
382{
383 struct resizor *resizor;
384
Peter Huttererf3d62272013-08-08 11:57:05 +1000385 resizor = xzalloc(sizeof *resizor);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500386 resizor->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500387 resizor->widget = window_frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500388 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500389 resizor->display = display;
390
391 window_set_key_handler(resizor->window, key_handler);
392 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500393 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500394 window_set_keyboard_focus_handler(resizor->window,
395 keyboard_focus_handler);
396
Jonas Ådahl9f8c8942014-11-26 17:40:42 +0800397 widget_set_enter_handler(resizor->widget, enter_handler);
398 widget_set_motion_handler(resizor->widget, motion_handler);
399
400 window_set_locked_pointer_motion_handler(
401 resizor->window, locked_pointer_handle_motion);
402
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500403 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500404
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400405 resizor->height.previous = 400;
406 resizor->height.current = 400;
407 resizor->height.target = 400;
408
409 resizor->width.previous = 400;
410 resizor->width.current = 400;
411 resizor->width.target = 400;
412
413 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500414
415 return resizor;
416}
417
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200418static void
419resizor_destroy(struct resizor *resizor)
420{
421 if (resizor->frame_callback)
422 wl_callback_destroy(resizor->frame_callback);
423
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200424 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200425 window_destroy(resizor->window);
426 free(resizor);
427}
428
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500429int
430main(int argc, char *argv[])
431{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200432 struct display *display;
433 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500434
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500435 display = display_create(&argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200436 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500437 fprintf(stderr, "failed to create display: %m\n");
438 return -1;
439 }
440
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200441 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500442
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200443 display_run(display);
444
445 resizor_destroy(resizor);
446 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500447
448 return 0;
449}