blob: 899c1527b89aa4b85c5adc94cdc08d989327014a [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
30#include "wayland-util.h"
31#include "wayland-client.h"
32#include "wayland-glib.h"
33
34#include "window.h"
35
36#include <X11/keysym.h>
37
38struct resizor {
39 struct display *display;
40 struct window *window;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050041 struct window *menu;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050042 int32_t width;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050043
44 struct {
45 double current;
46 double target;
47 double previous;
48 } height;
49};
50
51static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040052frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050053{
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040054 static const struct wl_callback_listener listener = {
55 frame_callback
56 };
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050057 struct resizor *resizor = data;
58 double force, height;
59
60 height = resizor->height.current;
61 force = (resizor->height.target - height) / 10.0 +
62 (resizor->height.previous - height);
63
64 resizor->height.current =
65 height + (height - resizor->height.previous) + force;
66 resizor->height.previous = height;
67
68 if (resizor->height.current >= 400) {
69 resizor->height.current = 400;
70 resizor->height.previous = 400;
71 }
72
73 if (resizor->height.current <= 200) {
74 resizor->height.current = 200;
75 resizor->height.previous = 200;
76 }
77
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050078 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050079
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050080 window_schedule_redraw(resizor->window);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040081
82 if (callback)
83 wl_callback_destroy(callback);
84
85 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
86 callback = wl_surface_frame(window_get_wl_surface(resizor->window));
87 wl_callback_add_listener(callback, &listener, resizor);
88 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050089}
90
91static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050092resizor_draw(struct resizor *resizor)
93{
94 cairo_surface_t *surface;
95 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050096 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050097
98 window_draw(resizor->window);
99
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500100 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500101
102 surface = window_get_surface(resizor->window);
103
104 cr = cairo_create(surface);
105 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
106 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500107 allocation.x,
108 allocation.y,
109 allocation.width,
110 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500111 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
112 cairo_fill(cr);
113 cairo_destroy(cr);
114
115 cairo_surface_destroy(surface);
116
117 window_flush(resizor->window);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500118}
119
120static void
121redraw_handler(struct window *window, void *data)
122{
123 struct resizor *resizor = data;
124
125 resizor_draw(resizor);
126}
127
128static void
129keyboard_focus_handler(struct window *window,
130 struct input *device, void *data)
131{
132 struct resizor *resizor = data;
133
134 window_schedule_redraw(resizor->window);
135}
136
137static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500138key_handler(struct window *window, struct input *input, uint32_t time,
139 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500140{
141 struct resizor *resizor = data;
142
143 if (state == 0)
144 return;
145
146 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500147 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500148 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400149 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500150 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500151 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500152 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400153 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500154 break;
155 }
156}
157
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500158static void
159show_menu(struct resizor *resizor, struct input *input)
160{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500161 int32_t x, y, width = 200, height = 200;
162
163 input_get_position(input, &x, &y);
164 resizor->menu = window_create_transient(resizor->display,
165 resizor->window,
166 x - 10, y - 10, width, height);
167
168 window_draw(resizor->menu);
169 window_flush(resizor->menu);
170}
171
172static void
173button_handler(struct window *window,
174 struct input *input, uint32_t time,
175 int button, int state, void *data)
176{
177 struct resizor *resizor = data;
178
179 switch (button) {
180 case 274:
181 if (state)
182 show_menu(resizor, input);
183 else
184 window_destroy(resizor->menu);
185 break;
186 }
187}
188
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500189static struct resizor *
190resizor_create(struct display *display)
191{
192 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500193 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500194
195 resizor = malloc(sizeof *resizor);
196 if (resizor == NULL)
197 return resizor;
198 memset(resizor, 0, sizeof *resizor);
199
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500200 resizor->window = window_create(display, 500, 400);
201 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500202 resizor->display = display;
203
204 window_set_key_handler(resizor->window, key_handler);
205 window_set_user_data(resizor->window, resizor);
206 window_set_redraw_handler(resizor->window, redraw_handler);
207 window_set_keyboard_focus_handler(resizor->window,
208 keyboard_focus_handler);
209
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500210 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500211 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500212 resizor->height.previous = resizor->height.current;
213 resizor->height.target = resizor->height.current;
214 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500215
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500216 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500217 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500218
219 resizor_draw(resizor);
220
221 return resizor;
222}
223
224int
225main(int argc, char *argv[])
226{
227 struct display *d;
228
Tim Wiederhakeb4b67342011-04-11 13:16:33 -0400229 d = display_create(&argc, &argv, NULL, NULL);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500230 if (d == NULL) {
231 fprintf(stderr, "failed to create display: %m\n");
232 return -1;
233 }
234
235 resizor_create (d);
236
237 display_run(d);
238
239 return 0;
240}