blob: dfbbf59f2dd0fc427cd44c1761120dcbc7480878 [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{
56 struct resizor *resizor = data;
57 double force, height;
58
Pekka Paalanene59d74a2011-12-15 15:26:29 +020059 assert(!callback || callback == resizor->frame_callback);
60
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050061 height = resizor->height.current;
62 force = (resizor->height.target - height) / 10.0 +
63 (resizor->height.previous - height);
64
65 resizor->height.current =
66 height + (height - resizor->height.previous) + force;
67 resizor->height.previous = height;
68
69 if (resizor->height.current >= 400) {
70 resizor->height.current = 400;
71 resizor->height.previous = 400;
72 }
73
74 if (resizor->height.current <= 200) {
75 resizor->height.current = 200;
76 resizor->height.previous = 200;
77 }
78
Kristian Høgsbergbb977002012-01-10 19:11:42 -050079 widget_schedule_resize(resizor->widget, resizor->width, height + 0.5);
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -040080
Pekka Paalanene59d74a2011-12-15 15:26:29 +020081 if (resizor->frame_callback) {
82 wl_callback_destroy(resizor->frame_callback);
83 resizor->frame_callback = NULL;
84 }
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050085}
86
Kristian Høgsbergbb977002012-01-10 19:11:42 -050087static const struct wl_callback_listener listener = {
88 frame_callback
89};
90
Kristian Høgsberg7c221d22010-12-16 13:35:23 -050091static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050092redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050093{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050094 struct resizor *resizor = data;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050095 cairo_surface_t *surface;
96 cairo_t *cr;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050097 struct rectangle allocation;
Kristian Høgsberg53a7f212010-12-16 21:11:10 -050098
Kristian Høgsbergbb977002012-01-10 19:11:42 -050099 widget_get_allocation(resizor->widget, &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);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500115
116 if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
117 resizor->frame_callback =
118 wl_surface_frame(
119 window_get_wl_surface(resizor->window));
120 wl_callback_add_listener(resizor->frame_callback, &listener,
121 resizor);
122 }
123
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);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200174 window_show_menu(resizor->display, input, time, resizor->window,
175 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500176}
177
178static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500179button_handler(struct widget *widget,
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500180 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100181 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500182{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500183 struct resizor *resizor = data;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500184
185 switch (button) {
Kristian Høgsberg8da0fbd2011-12-19 15:36:10 -0500186 case BTN_RIGHT:
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500187 if (state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500188 show_menu(resizor, input, time);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500189 break;
190 }
191}
192
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500193static struct resizor *
194resizor_create(struct display *display)
195{
196 struct resizor *resizor;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500197 int32_t height;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500198
199 resizor = malloc(sizeof *resizor);
200 if (resizor == NULL)
201 return resizor;
202 memset(resizor, 0, sizeof *resizor);
203
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500204 resizor->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500205 resizor->widget = frame_create(resizor->window, resizor);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500206 window_set_title(resizor->window, "Wayland Resizor");
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500207 resizor->display = display;
208
209 window_set_key_handler(resizor->window, key_handler);
210 window_set_user_data(resizor->window, resizor);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500211 widget_set_redraw_handler(resizor->widget, redraw_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500212 window_set_keyboard_focus_handler(resizor->window,
213 keyboard_focus_handler);
214
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500215 resizor->width = 300;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500216 resizor->height.current = 400;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500217 resizor->height.previous = resizor->height.current;
218 resizor->height.target = resizor->height.current;
219 height = resizor->height.current + 0.5;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500220
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500221 widget_set_button_handler(resizor->widget, button_handler);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500222
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500223 widget_schedule_resize(resizor->widget, resizor->width, height);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500224
225 return resizor;
226}
227
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200228static void
229resizor_destroy(struct resizor *resizor)
230{
231 if (resizor->frame_callback)
232 wl_callback_destroy(resizor->frame_callback);
233
Pekka Paalanen84d62dc2012-01-19 14:21:35 +0200234 widget_destroy(resizor->widget);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200235 window_destroy(resizor->window);
236 free(resizor);
237}
238
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500239int
240main(int argc, char *argv[])
241{
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200242 struct display *display;
243 struct resizor *resizor;
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500244
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400245 display = display_create(argc, argv);
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200246 if (display == NULL) {
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500247 fprintf(stderr, "failed to create display: %m\n");
248 return -1;
249 }
250
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200251 resizor = resizor_create(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500252
Pekka Paalanene59d74a2011-12-15 15:26:29 +0200253 display_run(display);
254
255 resizor_destroy(resizor);
256 display_destroy(display);
Kristian Høgsberg7c221d22010-12-16 13:35:23 -0500257
258 return 0;
259}