weston-editor --help works
[profile/ivi/weston-ivi-shell.git] / clients / scaler.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2013 Collabora, Ltd.
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 "config.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <cairo.h>
30
31 #include <linux/input.h>
32
33 #include "window.h"
34 #include "scaler-client-protocol.h"
35
36 #define BUFFER_SCALE 2
37 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
38 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
39 static const int SURFACE_WIDTH = 55 * 4;
40 static const int SURFACE_HEIGHT = 77 * 4;
41 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
42 static const double RECT_Y = 25 * BUFFER_SCALE;
43 static const double RECT_W = 55 * BUFFER_SCALE;
44 static const double RECT_H = 77 * BUFFER_SCALE;
45
46 struct box {
47         struct display *display;
48         struct window *window;
49         struct widget *widget;
50         int width, height;
51
52         struct wl_scaler *scaler;
53         int scaler_version;
54         struct wl_viewport *viewport;
55
56         enum {
57                 MODE_NO_VIEWPORT,
58                 MODE_SRC_ONLY,
59                 MODE_DST_ONLY,
60                 MODE_SRC_DST
61         } mode;
62 };
63
64 static void
65 set_my_viewport(struct box *box)
66 {
67         wl_fixed_t src_x, src_y, src_width, src_height;
68         int32_t dst_width = SURFACE_WIDTH;
69         int32_t dst_height = SURFACE_HEIGHT;
70
71         if (box->mode == MODE_NO_VIEWPORT)
72                 return;
73
74         /* Cut the green border in half, take white border fully in,
75          * and black border fully out. The borders are 1px wide in buffer.
76          *
77          * The gl-renderer uses linear texture sampling, this means the
78          * top and left edges go to 100% green, bottom goes to 50% blue/black,
79          * right edge has thick white sliding to 50% red.
80          */
81         src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
82         src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
83         src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
84         src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
85
86         if (box->scaler_version < 2 && box->mode != MODE_SRC_DST) {
87                 fprintf(stderr, "Error: server's wl_scaler interface version "
88                         "%d does not support this mode.\n",
89                         box->scaler_version);
90                 exit(1);
91         }
92
93         switch (box->mode){
94         case MODE_SRC_ONLY:
95                 wl_viewport_set_source(box->viewport, src_x, src_y,
96                                        src_width, src_height);
97                 break;
98         case MODE_DST_ONLY:
99                 wl_viewport_set_destination(box->viewport,
100                                             dst_width, dst_height);
101                 break;
102         case MODE_SRC_DST:
103                 if (box->scaler_version < 2) {
104                         wl_viewport_set(box->viewport,
105                                         src_x, src_y, src_width, src_height,
106                                         dst_width, dst_height);
107                 } else {
108                         wl_viewport_set_source(box->viewport, src_x, src_y,
109                                                src_width, src_height);
110                         wl_viewport_set_destination(box->viewport,
111                                                     dst_width, dst_height);
112                 }
113                 break;
114         default:
115                 assert(!"not reached");
116         }
117 }
118
119 static void
120 resize_handler(struct widget *widget,
121                int32_t width, int32_t height, void *data)
122 {
123         struct box *box = data;
124
125         /* Dont resize me */
126         widget_set_size(box->widget, box->width, box->height);
127 }
128
129 static void
130 redraw_handler(struct widget *widget, void *data)
131 {
132         struct box *box = data;
133         cairo_surface_t *surface;
134         cairo_t *cr;
135
136         surface = window_get_surface(box->window);
137         if (surface == NULL ||
138             cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
139                 fprintf(stderr, "failed to create cairo egl surface\n");
140                 return;
141         }
142
143         cr = cairo_create(surface);
144         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
145         cairo_set_line_width(cr, 1.0);
146         cairo_translate(cr, RECT_X, RECT_Y);
147
148         /* red background */
149         cairo_set_source_rgba(cr, 255, 0, 0, 255);
150         cairo_paint(cr);
151
152         /* blue box */
153         cairo_set_source_rgba(cr, 0, 0, 255, 255);
154         cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
155         cairo_fill(cr);
156
157         /* black border outside the box */
158         cairo_set_source_rgb(cr, 0, 0, 0);
159         cairo_move_to(cr, 0, RECT_H + 0.5);
160         cairo_line_to(cr, RECT_W, RECT_H + 0.5);
161         cairo_stroke(cr);
162
163         /* white border inside the box */
164         cairo_set_source_rgb(cr, 1, 1, 1);
165         cairo_move_to(cr, RECT_W - 0.5, 0);
166         cairo_line_to(cr, RECT_W - 0.5, RECT_H);
167         cairo_stroke(cr);
168
169         /* the green border on inside the box, to be split half by crop */
170         cairo_set_source_rgb(cr, 0, 1, 0);
171         cairo_move_to(cr, 0.5, RECT_H);
172         cairo_line_to(cr, 0.5, 0);
173         cairo_move_to(cr, 0, 0.5);
174         cairo_line_to(cr, RECT_W, 0.5);
175         cairo_stroke(cr);
176
177         cairo_destroy(cr);
178
179         /* TODO: buffer_transform */
180
181         cairo_surface_destroy(surface);
182 }
183
184 static void
185 global_handler(struct display *display, uint32_t name,
186                const char *interface, uint32_t version, void *data)
187 {
188         struct box *box = data;
189
190         if (strcmp(interface, "wl_scaler") == 0) {
191                 box->scaler_version = version < 2 ? version : 2;
192
193                 box->scaler = display_bind(display, name,
194                                            &wl_scaler_interface,
195                                            box->scaler_version);
196
197                 box->viewport = wl_scaler_get_viewport(box->scaler,
198                         widget_get_wl_surface(box->widget));
199
200                 set_my_viewport(box);
201         }
202 }
203
204 static void
205 button_handler(struct widget *widget,
206                struct input *input, uint32_t time,
207                uint32_t button, enum wl_pointer_button_state state, void *data)
208 {
209         struct box *box = data;
210
211         if (button != BTN_LEFT)
212                 return;
213
214         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
215                 window_move(box->window, input,
216                             display_get_serial(box->display));
217         }
218 }
219
220 static void
221 touch_down_handler(struct widget *widget, struct input *input,
222                    uint32_t serial, uint32_t time, int32_t id,
223                    float x, float y, void *data)
224 {
225         struct box *box = data;
226         window_move(box->window, input,
227                     display_get_serial(box->display));
228 }
229
230 static void
231 usage(const char *progname)
232 {
233         fprintf(stderr, "Usage: %s [mode]\n"
234                 "where 'mode' is one of\n"
235                 "  -b\tset both src and dst in viewport (default)\n"
236                 "  -d\tset only dst in viewport\n"
237                 "  -s\tset only src in viewport\n"
238                 "  -n\tdo not set viewport at all\n\n",
239                 progname);
240
241         fprintf(stderr, "Expected output with output_scale=1:\n");
242
243         fprintf(stderr, "Mode -n:\n"
244                 "  window size %dx%d px\n"
245                 "  Red box with a blue box in the upper left part.\n"
246                 "  The blue box has white right edge, black bottom edge,\n"
247                 "  and thin green left and top edges that can really\n"
248                 "  be seen only when zoomed in.\n\n",
249                 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
250
251         fprintf(stderr, "Mode -b:\n"
252                 "  window size %dx%d px\n"
253                 "  Blue box with green top and left edge,\n"
254                 "  thick white right edge with a hint of red,\n"
255                 "  and a hint of black in bottom edge.\n\n",
256                 SURFACE_WIDTH, SURFACE_HEIGHT);
257
258         fprintf(stderr, "Mode -s:\n"
259                 "  window size %.0fx%.0f px\n"
260                 "  The same as mode -b, but scaled a lot smaller.\n\n",
261                 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
262
263         fprintf(stderr, "Mode -d:\n"
264                 "  window size %dx%d px\n"
265                 "  This is horizontally squashed version of the -n mode.\n\n",
266                 SURFACE_WIDTH, SURFACE_HEIGHT);
267 }
268
269 int
270 main(int argc, char *argv[])
271 {
272         struct box box;
273         struct display *d;
274         struct timeval tv;
275         int i;
276
277         d = display_create(&argc, argv);
278         if (d == NULL) {
279                 fprintf(stderr, "failed to create display: %m\n");
280                 return -1;
281         }
282
283         box.mode = MODE_SRC_DST;
284
285         for (i = 1; i < argc; i++) {
286                 if (strcmp("-s", argv[i]) == 0)
287                         box.mode = MODE_SRC_ONLY;
288                 else if (strcmp("-d", argv[i]) == 0)
289                         box.mode = MODE_DST_ONLY;
290                 else if (strcmp("-b", argv[i]) == 0)
291                         box.mode = MODE_SRC_DST;
292                 else if (strcmp("-n", argv[i]) == 0)
293                         box.mode = MODE_NO_VIEWPORT;
294                 else {
295                         usage(argv[0]);
296                         exit(1);
297                 }
298         }
299
300         gettimeofday(&tv, NULL);
301         srandom(tv.tv_usec);
302
303         box.width = BUFFER_WIDTH / BUFFER_SCALE;
304         box.height = BUFFER_HEIGHT / BUFFER_SCALE;
305         box.display = d;
306         box.window = window_create(d);
307         box.widget = window_add_widget(box.window, &box);
308         window_set_title(box.window, "Scaler Test Box");
309         window_set_buffer_scale(box.window, BUFFER_SCALE);
310
311         widget_set_resize_handler(box.widget, resize_handler);
312         widget_set_redraw_handler(box.widget, redraw_handler);
313         widget_set_button_handler(box.widget, button_handler);
314         widget_set_default_cursor(box.widget, CURSOR_HAND1);
315         widget_set_touch_down_handler(box.widget, touch_down_handler);
316
317         window_schedule_resize(box.window, box.width, box.height);
318
319         display_set_user_data(box.display, &box);
320         display_set_global_handler(box.display, global_handler);
321
322         display_run(d);
323
324         widget_destroy(box.widget);
325         window_destroy(box.window);
326         display_destroy(d);
327
328         return 0;
329 }