client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.git] / clients / fullscreen.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2012 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting documentation, and
9  * that the name of the copyright holders not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no representations
12  * about the suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21  * OF THIS SOFTWARE.
22  */
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <math.h>
30 #include <cairo.h>
31
32 #include <linux/input.h>
33 #include <wayland-client.h>
34 #include "window.h"
35
36 struct fullscreen {
37         struct display *display;
38         struct window *window;
39         struct widget *widget;
40         int width, height;
41         int fullscreen;
42         float pointer_x, pointer_y;
43 };
44
45 static void
46 fullscreen_handler(struct window *window, void *data)
47 {
48         struct fullscreen *fullscreen = data;
49
50         fullscreen->fullscreen ^= 1;
51         window_set_fullscreen(window, fullscreen->fullscreen);
52 }
53
54 static void
55 resize_handler(struct widget *widget, int width, int height, void *data)
56 {
57         struct fullscreen *fullscreen = data;
58
59         widget_set_size(widget, fullscreen->width, fullscreen->height);
60 }
61
62 static void
63 draw_string(cairo_t *cr,
64             const char *fmt, ...)
65 {
66         char buffer[4096];
67         char *p, *end;
68         va_list argp;
69         cairo_text_extents_t text_extents;
70         cairo_font_extents_t font_extents;
71
72         cairo_save(cr);
73
74         cairo_select_font_face(cr, "sans",
75                                CAIRO_FONT_SLANT_NORMAL,
76                                CAIRO_FONT_WEIGHT_NORMAL);
77         cairo_set_font_size(cr, 14);
78
79         cairo_font_extents (cr, &font_extents);
80
81         va_start(argp, fmt);
82
83         vsnprintf(buffer, sizeof(buffer), fmt, argp);
84
85         p = buffer;
86         while (*p) {
87                 end = strchr(p, '\n');
88                 if (end)
89                         *end = 0;
90
91                 cairo_show_text(cr, p);
92                 cairo_text_extents (cr, p, &text_extents);
93                 cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
94
95                 if (end)
96                         p = end + 1;
97                 else
98                         break;
99         }
100
101         va_end(argp);
102
103         cairo_restore(cr);
104
105 }
106
107 static void
108 redraw_handler(struct widget *widget, void *data)
109 {
110         struct fullscreen *fullscreen = data;
111         struct rectangle allocation;
112         cairo_surface_t *surface;
113         cairo_t *cr;
114         int i;
115         double x, y, border;
116
117         surface = window_get_surface(fullscreen->window);
118         if (surface == NULL ||
119             cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
120                 fprintf(stderr, "failed to create cairo egl surface\n");
121                 return;
122         }
123
124         widget_get_allocation(fullscreen->widget, &allocation);
125
126         cr = widget_cairo_create(widget);
127
128         cairo_set_source_rgb(cr, 0, 0, 0);
129         cairo_paint (cr);
130
131         cairo_set_source_rgb(cr, 0, 0, 1);
132         cairo_set_line_width (cr, 10);
133         cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
134         cairo_stroke (cr);
135
136         cairo_move_to(cr,
137                       allocation.x + 15,
138                       allocation.y + 25);
139         cairo_set_source_rgb(cr, 1, 1, 1);
140
141         draw_string(cr,
142                     "Surface size: %d, %d\n"
143                     "Scale: %d, transform: %d\n"
144                     "Pointer: %f,%f\n"
145                     "Fullscreen: %d\n"
146                     "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n",
147                     fullscreen->width, fullscreen->height,
148                     window_get_buffer_scale (fullscreen->window),
149                     window_get_buffer_transform (fullscreen->window),
150                     fullscreen->pointer_x, fullscreen->pointer_y,
151                     fullscreen->fullscreen);
152
153         y = 100;
154         i = 0;
155         while (y + 60 < fullscreen->height) {
156                 border = (i++ % 2 == 0) ? 1 : 0.5;
157
158                 x = 50;
159                 cairo_set_line_width (cr, border);
160                 while (x + 70 < fullscreen->width) {
161                         if (fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
162                             fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
163                                 cairo_set_source_rgb(cr, 1, 0, 0);
164                                 cairo_rectangle(cr,
165                                                 x, y,
166                                                 50, 40);
167                                 cairo_fill(cr);
168                         }
169                         cairo_set_source_rgb(cr, 0, 1, 0);
170                         cairo_rectangle(cr,
171                                         x + border/2.0, y + border/2.0,
172                                         50, 40);
173                         cairo_stroke(cr);
174                         x += 60;
175                 }
176
177                 y += 50;
178         }
179
180         cairo_destroy(cr);
181 }
182
183 static void
184 key_handler(struct window *window, struct input *input, uint32_t time,
185             uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
186             void *data)
187 {
188         struct fullscreen *fullscreen = data;
189         int transform, scale;
190         static int current_size = 0;
191         int widths[] = { 640, 320, 800, 400 };
192         int heights[] = { 480, 240, 600, 300 };
193
194         if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
195                 return;
196
197         switch (sym) {
198         case XKB_KEY_t:
199                 transform = window_get_buffer_transform (window);
200                 transform = (transform + 1) % 8;
201                 window_set_buffer_transform(window, transform);
202                 window_schedule_redraw(window);
203                 break;
204
205         case XKB_KEY_s:
206                 scale = window_get_buffer_scale (window);
207                 if (scale == 1)
208                         scale = 2;
209                 else
210                         scale = 1;
211                 window_set_buffer_scale(window, scale);
212                 window_schedule_redraw(window);
213                 break;
214
215         case XKB_KEY_z:
216                 current_size = (current_size + 1) % 4;
217                 fullscreen->width = widths[current_size];
218                 fullscreen->height = heights[current_size];
219                 window_schedule_resize(fullscreen->window,
220                                        fullscreen->width, fullscreen->height);
221                 break;
222
223         case XKB_KEY_f:
224                 fullscreen->fullscreen ^= 1;
225                 window_set_fullscreen(window, fullscreen->fullscreen);
226                 break;
227
228         case XKB_KEY_q:
229                 exit (0);
230                 break;
231         }
232 }
233
234 static int
235 motion_handler(struct widget *widget,
236                struct input *input,
237                uint32_t time,
238                float x,
239                float y, void *data)
240 {
241         struct fullscreen *fullscreen = data;
242
243         fullscreen->pointer_x = x;
244         fullscreen->pointer_y = y;
245
246         widget_schedule_redraw(widget);
247         return 0;
248 }
249
250
251 static void
252 button_handler(struct widget *widget,
253                struct input *input, uint32_t time,
254                uint32_t button, enum wl_pointer_button_state state, void *data)
255 {
256         struct fullscreen *fullscreen = data;
257
258         switch (button) {
259         case BTN_LEFT:
260                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
261                         window_move(fullscreen->window, input,
262                                     display_get_serial(fullscreen->display));
263                 break;
264         case BTN_RIGHT:
265                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
266                         window_show_frame_menu(fullscreen->window, input, time);
267                 break;
268         }
269 }
270
271 static void
272 touch_handler(struct widget *widget, struct input *input, 
273                    uint32_t serial, uint32_t time, int32_t id, 
274                    float x, float y, void *data)
275 {
276         struct fullscreen *fullscreen = data;
277         window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
278 }
279
280 static void
281 usage(int error_code)
282 {
283         fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
284                 "   -w <width>\tSet window width to <width>\n"
285                 "   -h <height>\tSet window height to <height>\n"
286                 "   --help\tShow this help text\n\n");
287
288         exit(error_code);
289 }
290
291 int main(int argc, char *argv[])
292 {
293         struct fullscreen fullscreen;
294         struct display *d;
295         int i;
296
297         fullscreen.width = 640;
298         fullscreen.height = 480;
299         fullscreen.fullscreen = 0;
300
301         for (i = 1; i < argc; i++) {
302                 if (strcmp(argv[i], "-w") == 0) {
303                         if (++i >= argc)
304                                 usage(EXIT_FAILURE);
305
306                         fullscreen.width = atol(argv[i]);
307                 } else if (strcmp(argv[i], "-h") == 0) {
308                         if (++i >= argc)
309                                 usage(EXIT_FAILURE);
310
311                         fullscreen.height = atol(argv[i]);
312                 } else if (strcmp(argv[i], "--help") == 0)
313                         usage(EXIT_SUCCESS);
314                 else
315                         usage(EXIT_FAILURE);
316         }
317
318         d = display_create(&argc, argv);
319         if (d == NULL) {
320                 fprintf(stderr, "failed to create display: %m\n");
321                 return -1;
322         }
323
324         fullscreen.display = d;
325         fullscreen.window = window_create(d);
326         fullscreen.widget =
327                 window_add_widget(fullscreen.window, &fullscreen);
328
329         window_set_title(fullscreen.window, "Fullscreen");
330
331         widget_set_transparent(fullscreen.widget, 0);
332         widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
333
334         widget_set_resize_handler(fullscreen.widget, resize_handler);
335         widget_set_redraw_handler(fullscreen.widget, redraw_handler);
336         widget_set_button_handler(fullscreen.widget, button_handler);
337         widget_set_motion_handler(fullscreen.widget, motion_handler);
338
339         widget_set_touch_down_handler(fullscreen.widget, touch_handler);
340
341         window_set_key_handler(fullscreen.window, key_handler);
342         window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
343
344         window_set_user_data(fullscreen.window, &fullscreen);
345         /* Hack to set minimum allocation so we can shrink later */
346         window_schedule_resize(fullscreen.window,
347                                1, 1);
348         window_schedule_resize(fullscreen.window,
349                                fullscreen.width, fullscreen.height);
350
351         display_run(d);
352
353         return 0;
354 }