blob: 395fdcdb0d6adf5ba9e61b2a80beb723ee8fc597 [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>
Pekka Paalanene59d74a2011-12-15 15:26:29 +020029#include <assert.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050030
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -050031#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-client.h>
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050033
34#include "window.h"
35
36#include <X11/keysym.h>
37
38struct resizor {
39 struct display *display;
40 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050041 struct widget *widget;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050042 struct window *menu;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050043 int32_t width;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050044
45 struct {
46 double current;
47 double target;
48 double previous;
49 } height;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020050 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050051};
52
53static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040054frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050055{
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040056 static const struct wl_callback_listener listener = {
57 frame_callback
58 };
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050059 struct resizor *resizor = data;
60 double force, height;
61
Pekka Paalanene59d74a2011-12-15 15:26:29 +020062 assert(!callback || callback == resizor->frame_callback);
63
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050064 height = resizor->height.current;
65 force = (resizor->height.target - height) / 10.0 +
66 (resizor->height.previous - height);
67
68 resizor->height.current =
69 height + (height - resizor->height.previous) + force;
70 resizor->height.previous = height;
71
72 if (resizor->height.current >= 400) {
73 resizor->height.current = 400;
74 resizor->height.previous = 400;
75 }
76
77 if (resizor->height.current <= 200) {
78 resizor->height.current = 200;
79 resizor->height.previous = 200;
80 }
81
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050082 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050083
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050084 window_schedule_redraw(resizor->window);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040085
Pekka Paalanene59d74a2011-12-15 15:26:29 +020086 if (resizor->frame_callback) {
87 wl_callback_destroy(resizor->frame_callback);
88 resizor->frame_callback = NULL;
89 }
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040090
91 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
Pekka Paalanene59d74a2011-12-15 15:26:29 +020092 resizor->frame_callback =
93 wl_surface_frame(
94 window_get_wl_surface(resizor->window));
95 wl_callback_add_listener(resizor->frame_callback, &listener,
96 resizor);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040097 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050098}
99
100static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500101resizor_draw(struct resizor *resizor)
102{
103 cairo_surface_t *surface;
104 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500105 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500106
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500107 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500108
109 surface = window_get_surface(resizor->window);
110
111 cr = cairo_create(surface);
112 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
113 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500114 allocation.x,
115 allocation.y,
116 allocation.width,
117 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500118 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
119 cairo_fill(cr);
120 cairo_destroy(cr);
121
122 cairo_surface_destroy(surface);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500123}
124
125static void
126redraw_handler(struct window *window, void *data)
127{
128 struct resizor *resizor = data;
129
130 resizor_draw(resizor);
131}
132
133static void
134keyboard_focus_handler(struct window *window,
135 struct input *device, void *data)
136{
137 struct resizor *resizor = data;
138
139 window_schedule_redraw(resizor->window);
140}
141
142static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500143key_handler(struct window *window, struct input *input, uint32_t time,
144 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500145{
146 struct resizor *resizor = data;
147
148 if (state == 0)
149 return;
150
151 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500152 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500153 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400154 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500155 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500156 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500157 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400158 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500159 break;
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200160 case XK_Escape:
161 display_exit(resizor->display);
162 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500163 }
164}
165
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500166static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167menu_func(struct window *window, int index, void *user_data)
168{
169 fprintf(stderr, "picked entry %d\n", index);
170}
171
172static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500174{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500175 int32_t x, y;
176 static const char *entries[] = {
177 "Roy", "Pris", "Leon", "Zhora"
178 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500179
180 input_get_position(input, &x, &y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500181 resizor->menu = window_create_menu(resizor->display,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500182 input, time, resizor->window,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500183 x - 10, y - 10,
184 menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500185
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500186 window_schedule_redraw(resizor->menu);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500187}
188
189static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500190button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191 struct input *input, uint32_t time,
192 int button, int state, void *data)
193{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500194 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500195
196 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500197 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500198 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500199 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500200 break;
201 }
202}
203
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500204static struct resizor *
205resizor_create(struct display *display)
206{
207 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500208 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500209
210 resizor = malloc(sizeof *resizor);
211 if (resizor == NULL)
212 return resizor;
213 memset(resizor, 0, sizeof *resizor);
214
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500215 resizor->window = window_create(display, 500, 400);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500216 resizor->widget = window_add_widget(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500217 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500218 resizor->display = display;
219
220 window_set_key_handler(resizor->window, key_handler);
221 window_set_user_data(resizor->window, resizor);
222 window_set_redraw_handler(resizor->window, redraw_handler);
223 window_set_keyboard_focus_handler(resizor->window,
224 keyboard_focus_handler);
225
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500226 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500227 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500228 resizor->height.previous = resizor->height.current;
229 resizor->height.target = resizor->height.current;
230 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500231
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500232 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500233 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500234
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500235 window_schedule_redraw(resizor->window);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500236
237 return resizor;
238}
239
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200240static void
241resizor_destroy(struct resizor *resizor)
242{
243 if (resizor->frame_callback)
244 wl_callback_destroy(resizor->frame_callback);
245
246 window_destroy(resizor->window);
247 free(resizor);
248}
249
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500250int
251main(int argc, char *argv[])
252{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200253 struct display *display;
254 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500255
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200256 display = display_create(&argc, &argv, NULL);
257 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500258 fprintf(stderr, "failed to create display: %m\n");
259 return -1;
260 }
261
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200262 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500263
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200264 display_run(display);
265
266 resizor_destroy(resizor);
267 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500268
269 return 0;
270}