blob: 945f2ae74c2620cbdb205753b284684c1621b766 [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øgsbergb67e94b2012-01-10 12:23:19 -0500101redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500102{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500103 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500104 cairo_surface_t *surface;
105 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500106 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500107
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);
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500124}
125
126static void
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500127keyboard_focus_handler(struct window *window,
128 struct input *device, void *data)
129{
130 struct resizor *resizor = data;
131
132 window_schedule_redraw(resizor->window);
133}
134
135static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500136key_handler(struct window *window, struct input *input, uint32_t time,
137 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500138{
139 struct resizor *resizor = data;
140
141 if (state == 0)
142 return;
143
144 switch (sym) {
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500145 case XK_Down:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500146 resizor->height.target = 400;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400147 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500148 break;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -0500149 case XK_Up:
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500150 resizor->height.target = 200;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400151 frame_callback(resizor, NULL, 0);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500152 break;
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200153 case XK_Escape:
154 display_exit(resizor->display);
155 break;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500156 }
157}
158
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500159static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500160menu_func(struct window *window, int index, void *user_data)
161{
162 fprintf(stderr, "picked entry %d\n", index);
163}
164
165static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166show_menu(struct resizor *resizor, struct input *input, uint32_t time)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500167{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500168 int32_t x, y;
169 static const char *entries[] = {
170 "Roy", "Pris", "Leon", "Zhora"
171 };
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500172
173 input_get_position(input, &x, &y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500174 resizor->menu = window_create_menu(resizor->display,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 input, time, resizor->window,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500176 x - 10, y - 10,
177 menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500178
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500179 window_schedule_redraw(resizor->menu);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500180}
181
182static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500183button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500184 struct input *input, uint32_t time,
185 int button, int state, void *data)
186{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500187 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500188
189 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500190 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500191 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500192 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500193 break;
194 }
195}
196
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500197static struct resizor *
198resizor_create(struct display *display)
199{
200 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500201 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500202
203 resizor = malloc(sizeof *resizor);
204 if (resizor == NULL)
205 return resizor;
206 memset(resizor, 0, sizeof *resizor);
207
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500208 resizor->window = window_create(display, 500, 400);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500209 resizor->widget = window_add_widget(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500210 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500211 resizor->display = display;
212
213 window_set_key_handler(resizor->window, key_handler);
214 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500215 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500216 window_set_keyboard_focus_handler(resizor->window,
217 keyboard_focus_handler);
218
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500219 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500220 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500221 resizor->height.previous = resizor->height.current;
222 resizor->height.target = resizor->height.current;
223 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500224
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500225 window_set_child_size(resizor->window, resizor->width, height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500226 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500227
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500228 window_schedule_redraw(resizor->window);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500229
230 return resizor;
231}
232
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200233static void
234resizor_destroy(struct resizor *resizor)
235{
236 if (resizor->frame_callback)
237 wl_callback_destroy(resizor->frame_callback);
238
239 window_destroy(resizor->window);
240 free(resizor);
241}
242
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500243int
244main(int argc, char *argv[])
245{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200246 struct display *display;
247 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500248
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200249 display = display_create(&argc, &argv, NULL);
250 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500251 fprintf(stderr, "failed to create display: %m\n");
252 return -1;
253 }
254
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200255 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500256
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200257 display_run(display);
258
259 resizor_destroy(resizor);
260 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500261
262 return 0;
263}