blob: 8f70d537ceb568792416bcc81645f65923d3b6f6 [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
107 window_draw(resizor->window);
108
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500109 window_get_child_allocation(resizor->window, &allocation);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500110
111 surface = window_get_surface(resizor->window);
112
113 cr = cairo_create(surface);
114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
115 cairo_rectangle(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500116 allocation.x,
117 allocation.y,
118 allocation.width,
119 allocation.height);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500120 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
121 cairo_fill(cr);
122 cairo_destroy(cr);
123
124 cairo_surface_destroy(surface);
125
126 window_flush(resizor->window);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500127}
128
129static void
130redraw_handler(struct window *window, void *data)
131{
132 struct resizor *resizor = data;
133
134 resizor_draw(resizor);
135}
136
137static void
138keyboard_focus_handler(struct window *window,
139 struct input *device, void *data)
140{
141 struct resizor *resizor = data;
142
143 window_schedule_redraw(resizor->window);
144}
145
146static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500147key_handler(struct window *window, struct input *input, uint32_t time,
148 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500149{
150 struct resizor *resizor = data;
151
152 if (state == 0)
153 return;
154
155 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500156 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500157 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400158 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500159 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500160 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500161 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400162 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500163 break;
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200164 case XK_Escape:
165 display_exit(resizor->display);
166 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500167 }
168}
169
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500170static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500171menu_func(struct window *window, int index, void *user_data)
172{
173 fprintf(stderr, "picked entry %d\n", index);
174}
175
176static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500177show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500178{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500179 int32_t x, y;
180 static const char *entries[] = {
181 "Roy", "Pris", "Leon", "Zhora"
182 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500183
184 input_get_position(input, &x, &y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500185 resizor->menu = window_create_menu(resizor->display,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500186 input, time, resizor->window,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500187 x - 10, y - 10,
188 menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500189
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500190 window_schedule_redraw(resizor->menu);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191}
192
193static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500194button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500195 struct input *input, uint32_t time,
196 int button, int state, void *data)
197{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500198 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500199
200 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500201 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500202 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500203 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500204 break;
205 }
206}
207
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500208static struct resizor *
209resizor_create(struct display *display)
210{
211 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500212 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500213
214 resizor = malloc(sizeof *resizor);
215 if (resizor == NULL)
216 return resizor;
217 memset(resizor, 0, sizeof *resizor);
218
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500219 resizor->window = window_create(display, 500, 400);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500220 resizor->widget = window_add_widget(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500222 resizor->display = display;
223
224 window_set_key_handler(resizor->window, key_handler);
225 window_set_user_data(resizor->window, resizor);
226 window_set_redraw_handler(resizor->window, redraw_handler);
227 window_set_keyboard_focus_handler(resizor->window,
228 keyboard_focus_handler);
229
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500230 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500231 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500232 resizor->height.previous = resizor->height.current;
233 resizor->height.target = resizor->height.current;
234 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500235
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500236 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500237 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500238
239 resizor_draw(resizor);
240
241 return resizor;
242}
243
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200244static void
245resizor_destroy(struct resizor *resizor)
246{
247 if (resizor->frame_callback)
248 wl_callback_destroy(resizor->frame_callback);
249
250 window_destroy(resizor->window);
251 free(resizor);
252}
253
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500254int
255main(int argc, char *argv[])
256{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200257 struct display *display;
258 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500259
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200260 display = display_create(&argc, &argv, NULL);
261 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500262 fprintf(stderr, "failed to create display: %m\n");
263 return -1;
264 }
265
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200266 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500267
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200268 display_run(display);
269
270 resizor_destroy(resizor);
271 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500272
273 return 0;
274}