blob: c3d0327ac3b0956bce405303ef52d5363b33c3a2 [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>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050029
Pekka Paalanen50719bc2011-11-22 14:18:50 +020030#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050031
32#include "window.h"
33
34#include <X11/keysym.h>
35
36struct resizor {
37 struct display *display;
38 struct window *window;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050039 struct window *menu;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050040 int32_t width;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050041
42 struct {
43 double current;
44 double target;
45 double previous;
46 } height;
47};
48
49static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040050frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050051{
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040052 static const struct wl_callback_listener listener = {
53 frame_callback
54 };
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050055 struct resizor *resizor = data;
56 double force, height;
57
58 height = resizor->height.current;
59 force = (resizor->height.target - height) / 10.0 +
60 (resizor->height.previous - height);
61
62 resizor->height.current =
63 height + (height - resizor->height.previous) + force;
64 resizor->height.previous = height;
65
66 if (resizor->height.current >= 400) {
67 resizor->height.current = 400;
68 resizor->height.previous = 400;
69 }
70
71 if (resizor->height.current <= 200) {
72 resizor->height.current = 200;
73 resizor->height.previous = 200;
74 }
75
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050076 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050077
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050078 window_schedule_redraw(resizor->window);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040079
80 if (callback)
81 wl_callback_destroy(callback);
82
83 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
84 callback = wl_surface_frame(window_get_wl_surface(resizor->window));
85 wl_callback_add_listener(callback, &listener, resizor);
86 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050087}
88
89static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050090resizor_draw(struct resizor *resizor)
91{
92 cairo_surface_t *surface;
93 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050094 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050095
96 window_draw(resizor->window);
97
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050098 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050099
100 surface = window_get_surface(resizor->window);
101
102 cr = cairo_create(surface);
103 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
104 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500105 allocation.x,
106 allocation.y,
107 allocation.width,
108 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500109 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
110 cairo_fill(cr);
111 cairo_destroy(cr);
112
113 cairo_surface_destroy(surface);
114
115 window_flush(resizor->window);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500116}
117
118static void
119redraw_handler(struct window *window, void *data)
120{
121 struct resizor *resizor = data;
122
123 resizor_draw(resizor);
124}
125
126static void
127keyboard_focus_handler(struct window *window,
128 struct input *device, void *data)
129{
130 struct resizor *resizor = data;
131
132 window_schedule_redraw(resizor->window);
133}
134
135static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500136key_handler(struct window *window, struct input *input, uint32_t time,
137 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500138{
139 struct resizor *resizor = data;
140
141 if (state == 0)
142 return;
143
144 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500145 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500146 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400147 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500148 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500149 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500150 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400151 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500152 break;
153 }
154}
155
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500156static void
157show_menu(struct resizor *resizor, struct input *input)
158{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500159 int32_t x, y, width = 200, height = 200;
160
161 input_get_position(input, &x, &y);
162 resizor->menu = window_create_transient(resizor->display,
163 resizor->window,
164 x - 10, y - 10, width, height);
165
166 window_draw(resizor->menu);
167 window_flush(resizor->menu);
168}
169
170static void
171button_handler(struct window *window,
172 struct input *input, uint32_t time,
173 int button, int state, void *data)
174{
175 struct resizor *resizor = data;
176
177 switch (button) {
178 case 274:
179 if (state)
180 show_menu(resizor, input);
181 else
182 window_destroy(resizor->menu);
183 break;
184 }
185}
186
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500187static struct resizor *
188resizor_create(struct display *display)
189{
190 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500191 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500192
193 resizor = malloc(sizeof *resizor);
194 if (resizor == NULL)
195 return resizor;
196 memset(resizor, 0, sizeof *resizor);
197
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500198 resizor->window = window_create(display, 500, 400);
199 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500200 resizor->display = display;
201
202 window_set_key_handler(resizor->window, key_handler);
203 window_set_user_data(resizor->window, resizor);
204 window_set_redraw_handler(resizor->window, redraw_handler);
205 window_set_keyboard_focus_handler(resizor->window,
206 keyboard_focus_handler);
207
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500208 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500209 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500210 resizor->height.previous = resizor->height.current;
211 resizor->height.target = resizor->height.current;
212 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500213
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500214 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500215 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500216
217 resizor_draw(resizor);
218
219 return resizor;
220}
221
222int
223main(int argc, char *argv[])
224{
225 struct display *d;
226
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400227 d = display_create(&argc, &argv, NULL);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500228 if (d == NULL) {
229 fprintf(stderr, "failed to create display: %m\n");
230 return -1;
231 }
232
233 resizor_create (d);
234
235 display_run(d);
236
237 return 0;
238}