blob: 9788d2562f77b42ef161392a0d262959556a8477 [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øgsberg7c221d22010-12-16 13:35:23 -050052frame_callback(void *data, uint32_t time)
53{
54 struct resizor *resizor = data;
55 double force, height;
56
57 height = resizor->height.current;
58 force = (resizor->height.target - height) / 10.0 +
59 (resizor->height.previous - height);
60
61 resizor->height.current =
62 height + (height - resizor->height.previous) + force;
63 resizor->height.previous = height;
64
65 if (resizor->height.current >= 400) {
66 resizor->height.current = 400;
67 resizor->height.previous = 400;
68 }
69
70 if (resizor->height.current <= 200) {
71 resizor->height.current = 200;
72 resizor->height.previous = 200;
73 }
74
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050075 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050076
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050077 window_schedule_redraw(resizor->window);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050078}
79
80static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050081resizor_draw(struct resizor *resizor)
82{
83 cairo_surface_t *surface;
84 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050085 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050086
87 window_draw(resizor->window);
88
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050089 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050090
91 surface = window_get_surface(resizor->window);
92
93 cr = cairo_create(surface);
94 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
95 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050096 allocation.x,
97 allocation.y,
98 allocation.width,
99 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500100 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
101 cairo_fill(cr);
102 cairo_destroy(cr);
103
104 cairo_surface_destroy(surface);
105
106 window_flush(resizor->window);
107
108 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
109 wl_display_frame_callback(display_get_display(resizor->display),
110 frame_callback, resizor);
111 }
112}
113
114static void
115redraw_handler(struct window *window, void *data)
116{
117 struct resizor *resizor = data;
118
119 resizor_draw(resizor);
120}
121
122static void
123keyboard_focus_handler(struct window *window,
124 struct input *device, void *data)
125{
126 struct resizor *resizor = data;
127
128 window_schedule_redraw(resizor->window);
129}
130
131static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500132key_handler(struct window *window, struct input *input, uint32_t time,
133 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500134{
135 struct resizor *resizor = data;
136
137 if (state == 0)
138 return;
139
140 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500141 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500142 resizor->height.target = 400;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500143 frame_callback(resizor, 0);
144 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500145 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500146 resizor->height.target = 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500147 frame_callback(resizor, 0);
148 break;
149 }
150}
151
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500152static void
153show_menu(struct resizor *resizor, struct input *input)
154{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500155 int32_t x, y, width = 200, height = 200;
156
157 input_get_position(input, &x, &y);
158 resizor->menu = window_create_transient(resizor->display,
159 resizor->window,
160 x - 10, y - 10, width, height);
161
162 window_draw(resizor->menu);
163 window_flush(resizor->menu);
164}
165
166static void
167button_handler(struct window *window,
168 struct input *input, uint32_t time,
169 int button, int state, void *data)
170{
171 struct resizor *resizor = data;
172
173 switch (button) {
174 case 274:
175 if (state)
176 show_menu(resizor, input);
177 else
178 window_destroy(resizor->menu);
179 break;
180 }
181}
182
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500183static struct resizor *
184resizor_create(struct display *display)
185{
186 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500187 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500188
189 resizor = malloc(sizeof *resizor);
190 if (resizor == NULL)
191 return resizor;
192 memset(resizor, 0, sizeof *resizor);
193
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500194 resizor->window = window_create(display, 500, 400);
195 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500196 resizor->display = display;
197
198 window_set_key_handler(resizor->window, key_handler);
199 window_set_user_data(resizor->window, resizor);
200 window_set_redraw_handler(resizor->window, redraw_handler);
201 window_set_keyboard_focus_handler(resizor->window,
202 keyboard_focus_handler);
203
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500204 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500205 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500206 resizor->height.previous = resizor->height.current;
207 resizor->height.target = resizor->height.current;
208 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500209
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500210 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500211 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500212
213 resizor_draw(resizor);
214
215 return resizor;
216}
217
218int
219main(int argc, char *argv[])
220{
221 struct display *d;
222
Tim Wiederhakeb4b67342011-04-11 13:16:33 -0400223 d = display_create(&argc, &argv, NULL, NULL);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500224 if (d == NULL) {
225 fprintf(stderr, "failed to create display: %m\n");
226 return -1;
227 }
228
229 resizor_create (d);
230
231 display_run(d);
232
233 return 0;
234}