blob: 867ceb90c24cb73adae654a579118b6d5253eab6 [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ø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;
Pekka Paalanene59d74a2011-12-15 15:26:29 +020049 struct wl_callback *frame_callback;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050050};
51
52static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040053frame_callback(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050054{
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040055 static const struct wl_callback_listener listener = {
56 frame_callback
57 };
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050058 struct resizor *resizor = data;
59 double force, height;
60
Pekka Paalanene59d74a2011-12-15 15:26:29 +020061 assert(!callback || callback == resizor->frame_callback);
62
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050063 height = resizor->height.current;
64 force = (resizor->height.target - height) / 10.0 +
65 (resizor->height.previous - height);
66
67 resizor->height.current =
68 height + (height - resizor->height.previous) + force;
69 resizor->height.previous = height;
70
71 if (resizor->height.current >= 400) {
72 resizor->height.current = 400;
73 resizor->height.previous = 400;
74 }
75
76 if (resizor->height.current <= 200) {
77 resizor->height.current = 200;
78 resizor->height.previous = 200;
79 }
80
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050081 window_set_child_size(resizor->window, resizor->width, height + 0.5);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050082
Kristian Høgsberg5c4056e2010-12-16 14:56:41 -050083 window_schedule_redraw(resizor->window);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040084
Pekka Paalanene59d74a2011-12-15 15:26:29 +020085 if (resizor->frame_callback) {
86 wl_callback_destroy(resizor->frame_callback);
87 resizor->frame_callback = NULL;
88 }
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040089
90 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
Pekka Paalanene59d74a2011-12-15 15:26:29 +020091 resizor->frame_callback =
92 wl_surface_frame(
93 window_get_wl_surface(resizor->window));
94 wl_callback_add_listener(resizor->frame_callback, &listener,
95 resizor);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040096 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050097}
98
99static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500100resizor_draw(struct resizor *resizor)
101{
102 cairo_surface_t *surface;
103 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500104 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500105
106 window_draw(resizor->window);
107
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500108 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500109
110 surface = window_get_surface(resizor->window);
111
112 cr = cairo_create(surface);
113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
114 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500115 allocation.x,
116 allocation.y,
117 allocation.width,
118 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500119 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
120 cairo_fill(cr);
121 cairo_destroy(cr);
122
123 cairo_surface_destroy(surface);
124
125 window_flush(resizor->window);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500126}
127
128static void
129redraw_handler(struct window *window, void *data)
130{
131 struct resizor *resizor = data;
132
133 resizor_draw(resizor);
134}
135
136static void
137keyboard_focus_handler(struct window *window,
138 struct input *device, void *data)
139{
140 struct resizor *resizor = data;
141
142 window_schedule_redraw(resizor->window);
143}
144
145static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500146key_handler(struct window *window, struct input *input, uint32_t time,
147 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500148{
149 struct resizor *resizor = data;
150
151 if (state == 0)
152 return;
153
154 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500155 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500156 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400157 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500158 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500159 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500160 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400161 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500162 break;
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200163 case XK_Escape:
164 display_exit(resizor->display);
165 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500166 }
167}
168
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500169static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500171{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500172 int32_t x, y;
173 static const char *entries[] = {
174 "Roy", "Pris", "Leon", "Zhora"
175 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500176
177 input_get_position(input, &x, &y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500178 resizor->menu = window_create_menu(resizor->display,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500179 input, time, resizor->window,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500180 x - 10, y - 10, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500181
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500182 window_schedule_redraw(resizor->menu);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500183}
184
185static void
186button_handler(struct window *window,
187 struct input *input, uint32_t time,
188 int button, int state, void *data)
189{
190 struct resizor *resizor = data;
191
192 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500193 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500194 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500195 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500196 break;
197 }
198}
199
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500200static struct resizor *
201resizor_create(struct display *display)
202{
203 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500204 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500205
206 resizor = malloc(sizeof *resizor);
207 if (resizor == NULL)
208 return resizor;
209 memset(resizor, 0, sizeof *resizor);
210
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500211 resizor->window = window_create(display, 500, 400);
212 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500213 resizor->display = display;
214
215 window_set_key_handler(resizor->window, key_handler);
216 window_set_user_data(resizor->window, resizor);
217 window_set_redraw_handler(resizor->window, redraw_handler);
218 window_set_keyboard_focus_handler(resizor->window,
219 keyboard_focus_handler);
220
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500221 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500222 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500223 resizor->height.previous = resizor->height.current;
224 resizor->height.target = resizor->height.current;
225 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500226
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500227 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500228 window_set_button_handler(resizor->window, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500229
230 resizor_draw(resizor);
231
232 return resizor;
233}
234
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200235static void
236resizor_destroy(struct resizor *resizor)
237{
238 if (resizor->frame_callback)
239 wl_callback_destroy(resizor->frame_callback);
240
241 window_destroy(resizor->window);
242 free(resizor);
243}
244
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500245int
246main(int argc, char *argv[])
247{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200248 struct display *display;
249 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500250
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200251 display = display_create(&argc, &argv, NULL);
252 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500253 fprintf(stderr, "failed to create display: %m\n");
254 return -1;
255 }
256
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200257 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500258
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200259 display_run(display);
260
261 resizor_destroy(resizor);
262 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500263
264 return 0;
265}