client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.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         if (resizor->frame_callback) {
81                 wl_callback_destroy(resizor->frame_callback);
82                 resizor->frame_callback = NULL;
83         }
84
85         if (window_is_maximized(resizor->window))
86                 return;
87
88         spring_update(&resizor->width);
89         spring_update(&resizor->height);
90
91         widget_schedule_resize(resizor->widget,
92                                resizor->width.current + 0.5,
93                                resizor->height.current + 0.5);
94
95         if (!spring_done(&resizor->width) || !spring_done(&resizor->height)) {
96                 resizor->frame_callback =
97                         wl_surface_frame(
98                                 window_get_wl_surface(resizor->window));
99                 wl_callback_add_listener(resizor->frame_callback, &listener,
100                                          resizor);
101         }
102 }
103
104 static const struct wl_callback_listener listener = {
105         frame_callback
106 };
107
108 static void
109 redraw_handler(struct widget *widget, void *data)
110 {
111         struct resizor *resizor = data;
112         cairo_surface_t *surface;
113         cairo_t *cr;
114         struct rectangle allocation;
115
116         widget_get_allocation(resizor->widget, &allocation);
117
118         surface = window_get_surface(resizor->window);
119
120         cr = cairo_create(surface);
121         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122         cairo_rectangle(cr,
123                         allocation.x,
124                         allocation.y,
125                         allocation.width,
126                         allocation.height);
127         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
128         cairo_fill(cr);
129         cairo_destroy(cr);
130
131         cairo_surface_destroy(surface);
132 }
133
134 static void
135 keyboard_focus_handler(struct window *window,
136                        struct input *device, void *data)
137 {
138         struct resizor *resizor = data;
139
140         window_schedule_redraw(resizor->window);
141 }
142
143 static void
144 key_handler(struct window *window, struct input *input, uint32_t time,
145             uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
146             void *data)
147 {
148         struct resizor *resizor = data;
149         struct rectangle allocation;
150
151         if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
152                 return;
153
154         window_get_allocation(resizor->window, &allocation);
155         resizor->width.current = allocation.width;
156         if (spring_done(&resizor->width))
157                 resizor->width.target = allocation.width;
158         resizor->height.current = allocation.height;
159         if (spring_done(&resizor->height))
160                 resizor->height.target = allocation.height;
161
162         switch (sym) {
163         case XKB_KEY_Up:
164                 if (allocation.height < 400)
165                         break;
166
167                 resizor->height.target = allocation.height - 200;
168                 break;
169
170         case XKB_KEY_Down:
171                 if (allocation.height > 1000)
172                         break;
173
174                 resizor->height.target = allocation.height + 200;
175                 break;
176
177         case XKB_KEY_Left:
178                 if (allocation.width < 400)
179                         break;
180
181                 resizor->width.target = allocation.width - 200;
182                 break;
183
184         case XKB_KEY_Right:
185                 if (allocation.width > 1000)
186                         break;
187
188                 resizor->width.target = allocation.width + 200;
189                 break;
190
191         case XKB_KEY_Escape:
192                 display_exit(resizor->display);
193                 break;
194         }
195
196         if (!resizor->frame_callback)
197                 frame_callback(resizor, NULL, 0);
198 }
199
200 static void
201 menu_func(struct window *window,
202           struct input *input, int index, void *user_data)
203 {
204         fprintf(stderr, "picked entry %d\n", index);
205 }
206
207 static void
208 show_menu(struct resizor *resizor, struct input *input, uint32_t time)
209 {
210         int32_t x, y;
211         static const char *entries[] = {
212                 "Roy", "Pris", "Leon", "Zhora"
213         };
214
215         input_get_position(input, &x, &y);
216         window_show_menu(resizor->display, input, time, resizor->window,
217                          x - 10, y - 10, menu_func, entries, 4);
218 }
219
220 static void
221 button_handler(struct widget *widget,
222                struct input *input, uint32_t time,
223                uint32_t button, enum wl_pointer_button_state state, void *data)
224 {
225         struct resizor *resizor = data;
226
227         switch (button) {
228         case BTN_RIGHT:
229                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
230                         show_menu(resizor, input, time);
231                 break;
232         }
233 }
234
235 static struct resizor *
236 resizor_create(struct display *display)
237 {
238         struct resizor *resizor;
239
240         resizor = xzalloc(sizeof *resizor);
241         resizor->window = window_create(display);
242         resizor->widget = window_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 }