blob: 16287b383e6d491003c1ce91ed0df36c1febd9b5 [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
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010052frame_callback(struct wl_surface *surface, void *data, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050053{
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),
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100110 window_get_wl_surface(resizor->window),
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500111 frame_callback, resizor);
112 }
113}
114
115static void
116redraw_handler(struct window *window, void *data)
117{
118 struct resizor *resizor = data;
119
120 resizor_draw(resizor);
121}
122
123static void
124keyboard_focus_handler(struct window *window,
125 struct input *device, void *data)
126{
127 struct resizor *resizor = data;
128
129 window_schedule_redraw(resizor->window);
130}
131
132static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500133key_handler(struct window *window, struct input *input, uint32_t time,
134 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500135{
136 struct resizor *resizor = data;
137
138 if (state == 0)
139 return;
140
141 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500142 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500143 resizor->height.target = 400;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100144 frame_callback(window_get_wl_surface(window), resizor, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500145 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500146 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500147 resizor->height.target = 200;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100148 frame_callback(window_get_wl_surface(window), resizor, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500149 break;
150 }
151}
152
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500153static void
154show_menu(struct resizor *resizor, struct input *input)
155{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500156 int32_t x, y, width = 200, height = 200;
157
158 input_get_position(input, &x, &y);
159 resizor->menu = window_create_transient(resizor->display,
160 resizor->window,
161 x - 10, y - 10, width, height);
162
163 window_draw(resizor->menu);
164 window_flush(resizor->menu);
165}
166
167static void
168button_handler(struct window *window,
169 struct input *input, uint32_t time,
170 int button, int state, void *data)
171{
172 struct resizor *resizor = data;
173
174 switch (button) {
175 case 274:
176 if (state)
177 show_menu(resizor, input);
178 else
179 window_destroy(resizor->menu);
180 break;
181 }
182}
183
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500184static struct resizor *
185resizor_create(struct display *display)
186{
187 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500188 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500189
190 resizor = malloc(sizeof *resizor);
191 if (resizor == NULL)
192 return resizor;
193 memset(resizor, 0, sizeof *resizor);
194
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500195 resizor->window = window_create(display, 500, 400);
196 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500197 resizor->display = display;
198
199 window_set_key_handler(resizor->window, key_handler);
200 window_set_user_data(resizor->window, resizor);
201 window_set_redraw_handler(resizor->window, redraw_handler);
202 window_set_keyboard_focus_handler(resizor->window,
203 keyboard_focus_handler);
204
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500205 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500206 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500207 resizor->height.previous = resizor->height.current;
208 resizor->height.target = resizor->height.current;
209 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500210
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500211 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500212 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500213
214 resizor_draw(resizor);
215
216 return resizor;
217}
218
219int
220main(int argc, char *argv[])
221{
222 struct display *d;
223
Tim Wiederhakeb4b67342011-04-11 13:16:33 -0400224 d = display_create(&argc, &argv, NULL, NULL);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500225 if (d == NULL) {
226 fprintf(stderr, "failed to create display: %m\n");
227 return -1;
228 }
229
230 resizor_create (d);
231
232 display_run(d);
233
234 return 0;
235}