toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / clients / resizor.c
1 /*
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>
28 #include <math.h>
29 #include <assert.h>
30
31 #include <linux/input.h>
32 #include <wayland-client.h>
33
34 #include "window.h"
35
36 struct spring {
37         double current;
38         double target;
39         double previous;
40 };
41
42 struct resizor {
43         struct display *display;
44         struct window *window;
45         struct widget *widget;
46         struct window *menu;
47         struct spring width;
48         struct spring height;
49         struct wl_callback *frame_callback;
50 };
51
52 static void
53 spring_update(struct spring *spring)
54 {
55         double current, force;
56
57         current = spring->current;
58         force = (spring->target - current) / 20.0 +
59                 (spring->previous - current);
60
61         spring->current = current + (current - spring->previous) + force;
62         spring->previous = current;
63 }
64
65 static int
66 spring_done(struct spring *spring)
67 {
68         return fabs(spring->previous - spring->target) < 0.1;
69 }
70
71 static const struct wl_callback_listener listener;
72
73 static void
74 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
75 {
76         struct resizor *resizor = data;
77
78         assert(!callback || callback == resizor->frame_callback);
79
80         spring_update(&resizor->width);
81         spring_update(&resizor->height);
82
83         widget_schedule_resize(resizor->widget,
84                                resizor->width.current + 0.5,
85                                resizor->height.current + 0.5);
86
87         if (resizor->frame_callback) {
88                 wl_callback_destroy(resizor->frame_callback);
89                 resizor->frame_callback = NULL;
90         }
91
92         if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
93                 resizor->frame_callback =
94                         wl_surface_frame(
95                                 window_get_wl_surface(resizor->window));
96                 wl_callback_add_listener(resizor->frame_callback, &listener,
97                                          resizor);
98         }
99 }
100
101 static const struct wl_callback_listener listener = {
102         frame_callback
103 };
104
105 static void
106 redraw_handler(struct widget *widget, void *data)
107 {
108         struct resizor *resizor = data;
109         cairo_surface_t *surface;
110         cairo_t *cr;
111         struct rectangle allocation;
112
113         widget_get_allocation(resizor->widget, &allocation);
114
115         surface = window_get_surface(resizor->window);
116
117         cr = cairo_create(surface);
118         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
119         cairo_rectangle(cr,
120                         allocation.x,
121                         allocation.y,
122                         allocation.width,
123                         allocation.height);
124         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
125         cairo_fill(cr);
126         cairo_destroy(cr);
127
128         cairo_surface_destroy(surface);
129 }
130
131 static void
132 keyboard_focus_handler(struct window *window,
133                        struct input *device, void *data)
134 {
135         struct resizor *resizor = data;
136
137         window_schedule_redraw(resizor->window);
138 }
139
140 static void
141 key_handler(struct window *window, struct input *input, uint32_t time,
142             uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
143             void *data)
144 {
145         struct resizor *resizor = data;
146         struct rectangle allocation;
147
148         if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
149                 return;
150
151         window_get_allocation(resizor->window, &allocation);
152         resizor->width.current = allocation.width;
153         if (spring_done(&resizor->width))
154                 resizor->width.target = allocation.width;
155         resizor->height.current = allocation.height;
156         if (spring_done(&resizor->height))
157                 resizor->height.target = allocation.height;
158
159         switch (sym) {
160         case XKB_KEY_Up:
161                 if (allocation.height < 400)
162                         break;
163
164                 resizor->height.target = allocation.height - 200;
165                 break;
166
167         case XKB_KEY_Down:
168                 if (allocation.height > 1000)
169                         break;
170
171                 resizor->height.target = allocation.height + 200;
172                 break;
173
174         case XKB_KEY_Left:
175                 if (allocation.width < 400)
176                         break;
177
178                 resizor->width.target = allocation.width - 200;
179                 break;
180
181         case XKB_KEY_Right:
182                 if (allocation.width > 1000)
183                         break;
184
185                 resizor->width.target = allocation.width + 200;
186                 break;
187
188         case XKB_KEY_Escape:
189                 display_exit(resizor->display);
190                 break;
191         }
192
193         if (!resizor->frame_callback)
194                 frame_callback(resizor, NULL, 0);
195 }
196
197 static void
198 menu_func(struct window *window, int index, void *user_data)
199 {
200         fprintf(stderr, "picked entry %d\n", index);
201 }
202
203 static void
204 show_menu(struct resizor *resizor, struct input *input, uint32_t time)
205 {
206         int32_t x, y;
207         static const char *entries[] = {
208                 "Roy", "Pris", "Leon", "Zhora"
209         };
210
211         input_get_position(input, &x, &y);
212         window_show_menu(resizor->display, input, time, resizor->window,
213                          x - 10, y - 10, menu_func, entries, 4);
214 }
215
216 static void
217 button_handler(struct widget *widget,
218                struct input *input, uint32_t time,
219                uint32_t button, enum wl_pointer_button_state state, void *data)
220 {
221         struct resizor *resizor = data;
222
223         switch (button) {
224         case BTN_RIGHT:
225                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
226                         show_menu(resizor, input, time);
227                 break;
228         }
229 }
230
231 static struct resizor *
232 resizor_create(struct display *display)
233 {
234         struct resizor *resizor;
235
236         resizor = malloc(sizeof *resizor);
237         if (resizor == NULL)
238                 return resizor;
239         memset(resizor, 0, sizeof *resizor);
240
241         resizor->window = window_create(display);
242         resizor->widget = frame_create(resizor->window, resizor);
243         window_set_title(resizor->window, "Wayland Resizor");
244         resizor->display = display;
245
246         window_set_key_handler(resizor->window, key_handler);
247         window_set_user_data(resizor->window, resizor);
248         widget_set_redraw_handler(resizor->widget, redraw_handler);
249         window_set_keyboard_focus_handler(resizor->window,
250                                           keyboard_focus_handler);
251
252         widget_set_button_handler(resizor->widget, button_handler);
253
254         resizor->height.previous = 400;
255         resizor->height.current = 400;
256         resizor->height.target = 400;
257
258         resizor->width.previous = 400;
259         resizor->width.current = 400;
260         resizor->width.target = 400;
261
262         widget_schedule_resize(resizor->widget, 400, 400);
263
264         return resizor;
265 }
266
267 static void
268 resizor_destroy(struct resizor *resizor)
269 {
270         if (resizor->frame_callback)
271                 wl_callback_destroy(resizor->frame_callback);
272
273         widget_destroy(resizor->widget);
274         window_destroy(resizor->window);
275         free(resizor);
276 }
277
278 int
279 main(int argc, char *argv[])
280 {
281         struct display *display;
282         struct resizor *resizor;
283
284         display = display_create(argc, argv);
285         if (display == NULL) {
286                 fprintf(stderr, "failed to create display: %m\n");
287                 return -1;
288         }
289
290         resizor = resizor_create(display);
291
292         display_run(display);
293
294         resizor_destroy(resizor);
295         display_destroy(display);
296
297         return 0;
298 }