blob: a31e0d73a368af0a1263434b85a1a67af83567bf [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
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -050030#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020031#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050032
33#include "window.h"
34
35#include <X11/keysym.h>
36
37struct resizor {
38 struct display *display;
39 struct window *window;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050040 struct window *menu;
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øgsberga8d1fa72011-08-23 18:14:06 -040051frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050052{
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040053 static const struct wl_callback_listener listener = {
54 frame_callback
55 };
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050056 struct resizor *resizor = data;
57 double force, height;
58
59 height = resizor->height.current;
60 force = (resizor->height.target - height) / 10.0 +
61 (resizor->height.previous - height);
62
63 resizor->height.current =
64 height + (height - resizor->height.previous) + force;
65 resizor->height.previous = height;
66
67 if (resizor->height.current >= 400) {
68 resizor->height.current = 400;
69 resizor->height.previous = 400;
70 }
71
72 if (resizor->height.current <= 200) {
73 resizor->height.current = 200;
74 resizor->height.previous = 200;
75 }
76
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050077 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050078
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050079 window_schedule_redraw(resizor->window);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040080
81 if (callback)
82 wl_callback_destroy(callback);
83
84 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
85 callback = wl_surface_frame(window_get_wl_surface(resizor->window));
86 wl_callback_add_listener(callback, &listener, resizor);
87 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050088}
89
90static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050091resizor_draw(struct resizor *resizor)
92{
93 cairo_surface_t *surface;
94 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050095 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050096
97 window_draw(resizor->window);
98
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050099 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500100
101 surface = window_get_surface(resizor->window);
102
103 cr = cairo_create(surface);
104 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
105 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500106 allocation.x,
107 allocation.y,
108 allocation.width,
109 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500110 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
111 cairo_fill(cr);
112 cairo_destroy(cr);
113
114 cairo_surface_destroy(surface);
115
116 window_flush(resizor->window);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500117}
118
119static void
120redraw_handler(struct window *window, void *data)
121{
122 struct resizor *resizor = data;
123
124 resizor_draw(resizor);
125}
126
127static void
128keyboard_focus_handler(struct window *window,
129 struct input *device, void *data)
130{
131 struct resizor *resizor = data;
132
133 window_schedule_redraw(resizor->window);
134}
135
136static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500137key_handler(struct window *window, struct input *input, uint32_t time,
138 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500139{
140 struct resizor *resizor = data;
141
142 if (state == 0)
143 return;
144
145 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500146 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500147 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400148 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500149 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500150 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500151 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400152 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500153 break;
154 }
155}
156
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500157static void
158show_menu(struct resizor *resizor, struct input *input)
159{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500160 int32_t x, y;
161 static const char *entries[] = {
162 "Roy", "Pris", "Leon", "Zhora"
163 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500164
165 input_get_position(input, &x, &y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500166 resizor->menu = window_create_menu(resizor->display,
167 resizor->window,
168 x - 10, y - 10, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500169
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500170 window_schedule_redraw(resizor->menu);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500171}
172
173static void
174button_handler(struct window *window,
175 struct input *input, uint32_t time,
176 int button, int state, void *data)
177{
178 struct resizor *resizor = data;
179
180 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500181 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500182 if (state)
183 show_menu(resizor, input);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500184 break;
185 }
186}
187
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500188static struct resizor *
189resizor_create(struct display *display)
190{
191 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500192 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500193
194 resizor = malloc(sizeof *resizor);
195 if (resizor == NULL)
196 return resizor;
197 memset(resizor, 0, sizeof *resizor);
198
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500199 resizor->window = window_create(display, 500, 400);
200 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500201 resizor->display = display;
202
203 window_set_key_handler(resizor->window, key_handler);
204 window_set_user_data(resizor->window, resizor);
205 window_set_redraw_handler(resizor->window, redraw_handler);
206 window_set_keyboard_focus_handler(resizor->window,
207 keyboard_focus_handler);
208
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500209 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500210 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500211 resizor->height.previous = resizor->height.current;
212 resizor->height.target = resizor->height.current;
213 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500214
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500215 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500216 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500217
218 resizor_draw(resizor);
219
220 return resizor;
221}
222
223int
224main(int argc, char *argv[])
225{
226 struct display *d;
227
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400228 d = display_create(&argc, &argv, NULL);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500229 if (d == NULL) {
230 fprintf(stderr, "failed to create display: %m\n");
231 return -1;
232 }
233
234 resizor_create (d);
235
236 display_run(d);
237
238 return 0;
239}