blob: cd9bfdc535ebc3ef784a50e4ad6a221b25cd2e07 [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øgsbergda846ca2011-01-11 10:00:52 -050041 int32_t width;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050042
43 struct {
44 double current;
45 double target;
46 double previous;
47 } height;
48};
49
50static void
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050051frame_callback(void *data, uint32_t time)
52{
53 struct resizor *resizor = data;
54 double force, height;
55
56 height = resizor->height.current;
57 force = (resizor->height.target - height) / 10.0 +
58 (resizor->height.previous - height);
59
60 resizor->height.current =
61 height + (height - resizor->height.previous) + force;
62 resizor->height.previous = height;
63
64 if (resizor->height.current >= 400) {
65 resizor->height.current = 400;
66 resizor->height.previous = 400;
67 }
68
69 if (resizor->height.current <= 200) {
70 resizor->height.current = 200;
71 resizor->height.previous = 200;
72 }
73
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050074 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050075
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050076 window_schedule_redraw(resizor->window);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050077}
78
79static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050080resizor_draw(struct resizor *resizor)
81{
82 cairo_surface_t *surface;
83 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050084 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050085
86 window_draw(resizor->window);
87
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050088 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050089
90 surface = window_get_surface(resizor->window);
91
92 cr = cairo_create(surface);
93 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
94 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050095 allocation.x,
96 allocation.y,
97 allocation.width,
98 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050099 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
100 cairo_fill(cr);
101 cairo_destroy(cr);
102
103 cairo_surface_destroy(surface);
104
105 window_flush(resizor->window);
106
107 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
108 wl_display_frame_callback(display_get_display(resizor->display),
109 frame_callback, resizor);
110 }
111}
112
113static void
114redraw_handler(struct window *window, void *data)
115{
116 struct resizor *resizor = data;
117
118 resizor_draw(resizor);
119}
120
121static void
122keyboard_focus_handler(struct window *window,
123 struct input *device, void *data)
124{
125 struct resizor *resizor = data;
126
127 window_schedule_redraw(resizor->window);
128}
129
130static void
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500131key_handler(struct window *window, uint32_t key, uint32_t sym,
132 uint32_t state, uint32_t modifiers, void *data)
133{
134 struct resizor *resizor = data;
135
136 if (state == 0)
137 return;
138
139 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500140 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500141 resizor->height.target = 400;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500142 frame_callback(resizor, 0);
143 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500144 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500145 resizor->height.target = 200;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500146 frame_callback(resizor, 0);
147 break;
148 }
149}
150
151static struct resizor *
152resizor_create(struct display *display)
153{
154 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500155 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500156
157 resizor = malloc(sizeof *resizor);
158 if (resizor == NULL)
159 return resizor;
160 memset(resizor, 0, sizeof *resizor);
161
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500162 resizor->window = window_create(display, "Wayland Resizor", 500, 400);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500163 resizor->display = display;
164
165 window_set_key_handler(resizor->window, key_handler);
166 window_set_user_data(resizor->window, resizor);
167 window_set_redraw_handler(resizor->window, redraw_handler);
168 window_set_keyboard_focus_handler(resizor->window,
169 keyboard_focus_handler);
170
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500171 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500172 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500173 resizor->height.previous = resizor->height.current;
174 resizor->height.target = resizor->height.current;
175 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500176
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500177 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500178
179 resizor_draw(resizor);
180
181 return resizor;
182}
183
184int
185main(int argc, char *argv[])
186{
187 struct display *d;
188
189 d = display_create(&argc, &argv, NULL);
190 if (d == NULL) {
191 fprintf(stderr, "failed to create display: %m\n");
192 return -1;
193 }
194
195 resizor_create (d);
196
197 display_run(d);
198
199 return 0;
200}