blob: 31396d933af41d458417bb96cc9ea209d26dbb0a [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
36#include <X11/keysym.h>
37
Kristian Høgsbergd1745212012-05-10 23:10:54 -040038struct spring {
39 double current;
40 double target;
41 double previous;
42};
43
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050044struct resizor {
45 struct display *display;
46 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050047 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050048 struct window *menu;
Kristian Høgsbergd1745212012-05-10 23:10:54 -040049 struct spring width;
50 struct spring height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020051 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050052};
53
54static void
Kristian Høgsbergd1745212012-05-10 23:10:54 -040055spring_update(struct spring *spring)
56{
57 double current, force;
58
59 current = spring->current;
60 force = (spring->target - current) / 20.0 +
61 (spring->previous - current);
62
63 spring->current = current + (current - spring->previous) + force;
64 spring->previous = current;
65}
66
67static int
68spring_done(struct spring *spring)
69{
70 return fabs(spring->previous - spring->target) < 0.1;
71}
72
73static const struct wl_callback_listener listener;
74
75static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040076frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050077{
78 struct resizor *resizor = data;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050079
Pekka Paalanene59d74a2011-12-15 15:26:29 +020080 assert(!callback || callback == resizor->frame_callback);
81
Kristian Høgsbergd1745212012-05-10 23:10:54 -040082 spring_update(&resizor->width);
83 spring_update(&resizor->height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050084
Kristian Høgsbergd1745212012-05-10 23:10:54 -040085 widget_schedule_resize(resizor->widget,
86 resizor->width.current + 0.5,
87 resizor->height.current + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040088
Pekka Paalanene59d74a2011-12-15 15:26:29 +020089 if (resizor->frame_callback) {
90 wl_callback_destroy(resizor->frame_callback);
91 resizor->frame_callback = NULL;
92 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -040093
94 if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
95 resizor->frame_callback =
96 wl_surface_frame(
97 window_get_wl_surface(resizor->window));
98 wl_callback_add_listener(resizor->frame_callback, &listener,
99 resizor);
100 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500101}
102
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500103static const struct wl_callback_listener listener = {
104 frame_callback
105};
106
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500107static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500108redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500109{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500110 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500111 cairo_surface_t *surface;
112 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500113 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500114
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500115 widget_get_allocation(resizor->widget, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500116
117 surface = window_get_surface(resizor->window);
118
119 cr = cairo_create(surface);
120 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
121 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500122 allocation.x,
123 allocation.y,
124 allocation.width,
125 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500126 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
127 cairo_fill(cr);
128 cairo_destroy(cr);
129
130 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500131}
132
133static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500134keyboard_focus_handler(struct window *window,
135 struct input *device, void *data)
136{
137 struct resizor *resizor = data;
138
139 window_schedule_redraw(resizor->window);
140}
141
142static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500143key_handler(struct window *window, struct input *input, uint32_t time,
144 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500145{
146 struct resizor *resizor = data;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400147 struct rectangle allocation;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500148
149 if (state == 0)
150 return;
151
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400152 window_get_allocation(resizor->window, &allocation);
153 resizor->width.current = allocation.width;
154 if (spring_done(&resizor->width))
155 resizor->width.target = allocation.width;
156 resizor->height.current = allocation.height;
157 if (spring_done(&resizor->height))
158 resizor->height.target = allocation.height;
159
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500160 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500161 case XK_Up:
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400162 if (allocation.height < 400)
163 break;
164
165 resizor->height.target = allocation.height - 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500166 break;
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400167
168 case XK_Down:
169 if (allocation.height > 1000)
170 break;
171
172 resizor->height.target = allocation.height + 200;
173 break;
174
175 case XK_Left:
176 if (allocation.width < 400)
177 break;
178
179 resizor->width.target = allocation.width - 200;
180 break;
181
182 case XK_Right:
183 if (allocation.width > 1000)
184 break;
185
186 resizor->width.target = allocation.width + 200;
187 break;
188
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200189 case XK_Escape:
190 display_exit(resizor->display);
191 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500192 }
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400193
194 if (!resizor->frame_callback)
195 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500196}
197
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500198static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500199menu_func(struct window *window, int index, void *user_data)
200{
201 fprintf(stderr, "picked entry %d\n", index);
202}
203
204static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500205show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500206{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500207 int32_t x, y;
208 static const char *entries[] = {
209 "Roy", "Pris", "Leon", "Zhora"
210 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500211
212 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200213 window_show_menu(resizor->display, input, time, resizor->window,
214 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500215}
216
217static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500218button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100220 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500222 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500223
224 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500225 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500227 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228 break;
229 }
230}
231
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500232static struct resizor *
233resizor_create(struct display *display)
234{
235 struct resizor *resizor;
236
237 resizor = malloc(sizeof *resizor);
238 if (resizor == NULL)
239 return resizor;
240 memset(resizor, 0, sizeof *resizor);
241
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500242 resizor->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500243 resizor->widget = frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500244 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500245 resizor->display = display;
246
247 window_set_key_handler(resizor->window, key_handler);
248 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500249 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500250 window_set_keyboard_focus_handler(resizor->window,
251 keyboard_focus_handler);
252
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500253 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500254
Kristian Høgsbergd1745212012-05-10 23:10:54 -0400255 resizor->height.previous = 400;
256 resizor->height.current = 400;
257 resizor->height.target = 400;
258
259 resizor->width.previous = 400;
260 resizor->width.current = 400;
261 resizor->width.target = 400;
262
263 widget_schedule_resize(resizor->widget, 400, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500264
265 return resizor;
266}
267
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200268static void
269resizor_destroy(struct resizor *resizor)
270{
271 if (resizor->frame_callback)
272 wl_callback_destroy(resizor->frame_callback);
273
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200274 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200275 window_destroy(resizor->window);
276 free(resizor);
277}
278
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500279int
280main(int argc, char *argv[])
281{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200282 struct display *display;
283 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500284
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400285 display = display_create(argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200286 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500287 fprintf(stderr, "failed to create display: %m\n");
288 return -1;
289 }
290
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200291 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500292
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200293 display_run(display);
294
295 resizor_destroy(resizor);
296 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500297
298 return 0;
299}