blob: 8186561887cf58a559d11b4b456892856ff7edfb [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øgsbergd1745212012-05-10 23:10:54 -040080 spring_update(&resizor->width);
81 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050082
Kristian Høgsbergd1745212012-05-10 23:10:54 -040083 widget_schedule_resize(resizor->widget,
84 resizor->width.current + 0.5,
85 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040086
Pekka Paalanene59d74a2011-12-15 15:26:29 +020087 if (resizor->frame_callback) {
88 wl_callback_destroy(resizor->frame_callback);
89 resizor->frame_callback = NULL;
90 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -040091
92 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
93 resizor->frame_callback =
94 wl_surface_frame(
95 window_get_wl_surface(resizor->window));
96 wl_callback_add_listener(resizor->frame_callback, &listener,
97 resizor);
98 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050099}
100
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500101static const struct wl_callback_listener listener = {
102 frame_callback
103};
104
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500105static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500106redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500107{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500108 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500109 cairo_surface_t *surface;
110 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500111 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500112
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500113 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500114
115 surface = window_get_surface(resizor->window);
116
117 cr = cairo_create(surface);
118 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
119 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500120 allocation.x,
121 allocation.y,
122 allocation.width,
123 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500124 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
125 cairo_fill(cr);
126 cairo_destroy(cr);
127
128 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500129}
130
131static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500132keyboard_focus_handler(struct window *window,
133 struct input *device, void *data)
134{
135 struct resizor *resizor = data;
136
137 window_schedule_redraw(resizor->window);
138}
139
140static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500141key_handler(struct window *window, struct input *input, uint32_t time,
142 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500143{
144 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400145 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500146
147 if (state == 0)
148 return;
149
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400150 window_get_allocation(resizor->window, &allocation);
151 resizor->width.current = allocation.width;
152 if (spring_done(&resizor->width))
153 resizor->width.target = allocation.width;
154 resizor->height.current = allocation.height;
155 if (spring_done(&resizor->height))
156 resizor->height.target = allocation.height;
157
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500158 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400159 case XKB_KEY_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400160 if (allocation.height < 400)
161 break;
162
163 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500164 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400165
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400166 case XKB_KEY_Down:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400167 if (allocation.height > 1000)
168 break;
169
170 resizor->height.target = allocation.height + 200;
171 break;
172
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400173 case XKB_KEY_Left:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400174 if (allocation.width < 400)
175 break;
176
177 resizor->width.target = allocation.width - 200;
178 break;
179
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400180 case XKB_KEY_Right:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400181 if (allocation.width > 1000)
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_Escape:
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200188 display_exit(resizor->display);
189 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500190 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400191
192 if (!resizor->frame_callback)
193 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500194}
195
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500196static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500197menu_func(struct window *window, int index, void *user_data)
198{
199 fprintf(stderr, "picked entry %d\n", index);
200}
201
202static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500203show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500204{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500205 int32_t x, y;
206 static const char *entries[] = {
207 "Roy", "Pris", "Leon", "Zhora"
208 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500209
210 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200211 window_show_menu(resizor->display, input, time, resizor->window,
212 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500213}
214
215static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500216button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500217 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100218 uint32_t button, enum wl_pointer_button_state state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500220 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221
222 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500223 case BTN_RIGHT:
Daniel Stone4dbadb12012-05-30 16:31:51 +0100224 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500225 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 break;
227 }
228}
229
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500230static struct resizor *
231resizor_create(struct display *display)
232{
233 struct resizor *resizor;
234
235 resizor = malloc(sizeof *resizor);
236 if (resizor == NULL)
237 return resizor;
238 memset(resizor, 0, sizeof *resizor);
239
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500240 resizor->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500241 resizor->widget = frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500242 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500243 resizor->display = display;
244
245 window_set_key_handler(resizor->window, key_handler);
246 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500247 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500248 window_set_keyboard_focus_handler(resizor->window,
249 keyboard_focus_handler);
250
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500251 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500252
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400253 resizor->height.previous = 400;
254 resizor->height.current = 400;
255 resizor->height.target = 400;
256
257 resizor->width.previous = 400;
258 resizor->width.current = 400;
259 resizor->width.target = 400;
260
261 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500262
263 return resizor;
264}
265
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200266static void
267resizor_destroy(struct resizor *resizor)
268{
269 if (resizor->frame_callback)
270 wl_callback_destroy(resizor->frame_callback);
271
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200272 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200273 window_destroy(resizor->window);
274 free(resizor);
275}
276
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500277int
278main(int argc, char *argv[])
279{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200280 struct display *display;
281 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500282
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400283 display = display_create(argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200284 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500285 fprintf(stderr, "failed to create display: %m\n");
286 return -1;
287 }
288
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200289 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500290
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200291 display_run(display);
292
293 resizor_destroy(resizor);
294 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500295
296 return 0;
297}