libinput: make setting the same output a no-op
[platform/upstream/weston.git] / clients / scaler.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2013 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "config.h"
26
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <cairo.h>
32
33 #include <linux/input.h>
34
35 #include "window.h"
36 #include "viewporter-client-protocol.h"
37
38 #define BUFFER_SCALE 2
39 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
40 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
41 static const int SURFACE_WIDTH = 55 * 4;
42 static const int SURFACE_HEIGHT = 77 * 4;
43 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
44 static const double RECT_Y = 25 * BUFFER_SCALE;
45 static const double RECT_W = 55 * BUFFER_SCALE;
46 static const double RECT_H = 77 * BUFFER_SCALE;
47
48 struct box {
49         struct display *display;
50         struct window *window;
51         struct widget *widget;
52         int width, height;
53
54         struct wp_viewporter *viewporter;
55         struct wp_viewport *viewport;
56
57         enum {
58                 MODE_NO_VIEWPORT,
59                 MODE_SRC_ONLY,
60                 MODE_DST_ONLY,
61                 MODE_SRC_DST
62         } mode;
63 };
64
65 static void
66 set_my_viewport(struct box *box)
67 {
68         wl_fixed_t src_x, src_y, src_width, src_height;
69         int32_t dst_width = SURFACE_WIDTH;
70         int32_t dst_height = SURFACE_HEIGHT;
71
72         if (box->mode == MODE_NO_VIEWPORT)
73                 return;
74
75         /* Cut the green border in half, take white border fully in,
76          * and black border fully out. The borders are 1px wide in buffer.
77          *
78          * The gl-renderer uses linear texture sampling, this means the
79          * top and left edges go to 100% green, bottom goes to 50% blue/black,
80          * right edge has thick white sliding to 50% red.
81          */
82         src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
83         src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
84         src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
85         src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
86
87         switch (box->mode){
88         case MODE_SRC_ONLY:
89                 /* In SRC_ONLY mode we're just cropping - in order
90                  * for the surface size to remain an integer, the
91                  * compositor will generate an error if we use a
92                  * fractional width or height.
93                  *
94                  * We use fractional width/height for the other cases
95                  * to ensure fractional values are still tested.
96                  */
97                 src_width = wl_fixed_from_int(RECT_W / BUFFER_SCALE);
98                 src_height = wl_fixed_from_int(RECT_H / BUFFER_SCALE);
99                 wp_viewport_set_source(box->viewport, src_x, src_y,
100                                        src_width, src_height);
101                 break;
102         case MODE_DST_ONLY:
103                 wp_viewport_set_destination(box->viewport,
104                                             dst_width, dst_height);
105                 break;
106         case MODE_SRC_DST:
107                 wp_viewport_set_source(box->viewport, src_x, src_y,
108                                        src_width, src_height);
109                 wp_viewport_set_destination(box->viewport,
110                                             dst_width, dst_height);
111                 break;
112         default:
113                 assert(!"not reached");
114         }
115 }
116
117 static void
118 resize_handler(struct widget *widget,
119                int32_t width, int32_t height, void *data)
120 {
121         struct box *box = data;
122
123         /* Don't resize me */
124         widget_set_size(box->widget, box->width, box->height);
125 }
126
127 static void
128 redraw_handler(struct widget *widget, void *data)
129 {
130         struct box *box = data;
131         cairo_surface_t *surface;
132         cairo_t *cr;
133
134         surface = window_get_surface(box->window);
135         if (surface == NULL ||
136             cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
137                 fprintf(stderr, "failed to create cairo egl surface\n");
138                 return;
139         }
140
141         cr = cairo_create(surface);
142         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
143         cairo_set_line_width(cr, 1.0);
144         cairo_translate(cr, RECT_X, RECT_Y);
145
146         /* red background */
147         cairo_set_source_rgba(cr, 255, 0, 0, 255);
148         cairo_paint(cr);
149
150         /* blue box */
151         cairo_set_source_rgba(cr, 0, 0, 255, 255);
152         cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
153         cairo_fill(cr);
154
155         /* black border outside the box */
156         cairo_set_source_rgb(cr, 0, 0, 0);
157         cairo_move_to(cr, 0, RECT_H + 0.5);
158         cairo_line_to(cr, RECT_W, RECT_H + 0.5);
159         cairo_stroke(cr);
160
161         /* white border inside the box */
162         cairo_set_source_rgb(cr, 1, 1, 1);
163         cairo_move_to(cr, RECT_W - 0.5, 0);
164         cairo_line_to(cr, RECT_W - 0.5, RECT_H);
165         cairo_stroke(cr);
166
167         /* the green border on inside the box, to be split half by crop */
168         cairo_set_source_rgb(cr, 0, 1, 0);
169         cairo_move_to(cr, 0.5, RECT_H);
170         cairo_line_to(cr, 0.5, 0);
171         cairo_move_to(cr, 0, 0.5);
172         cairo_line_to(cr, RECT_W, 0.5);
173         cairo_stroke(cr);
174
175         cairo_destroy(cr);
176
177         /* TODO: buffer_transform */
178
179         cairo_surface_destroy(surface);
180 }
181
182 static void
183 global_handler(struct display *display, uint32_t name,
184                const char *interface, uint32_t version, void *data)
185 {
186         struct box *box = data;
187
188         if (strcmp(interface, "wp_viewporter") == 0) {
189                 box->viewporter = display_bind(display, name,
190                                                &wp_viewporter_interface, 1);
191
192                 box->viewport = wp_viewporter_get_viewport(box->viewporter,
193                         widget_get_wl_surface(box->widget));
194
195                 set_my_viewport(box);
196         }
197 }
198
199 static void
200 button_handler(struct widget *widget,
201                struct input *input, uint32_t time,
202                uint32_t button, enum wl_pointer_button_state state, void *data)
203 {
204         struct box *box = data;
205
206         if (button != BTN_LEFT)
207                 return;
208
209         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
210                 window_move(box->window, input,
211                             display_get_serial(box->display));
212         }
213 }
214
215 static void
216 touch_down_handler(struct widget *widget, struct input *input,
217                    uint32_t serial, uint32_t time, int32_t id,
218                    float x, float y, void *data)
219 {
220         struct box *box = data;
221         window_move(box->window, input,
222                     display_get_serial(box->display));
223 }
224
225 static void
226 usage(const char *progname)
227 {
228         fprintf(stderr, "Usage: %s [mode]\n"
229                 "where 'mode' is one of\n"
230                 "  -b\tset both src and dst in viewport (default)\n"
231                 "  -d\tset only dst in viewport\n"
232                 "  -s\tset only src in viewport\n"
233                 "  -n\tdo not set viewport at all\n\n",
234                 progname);
235
236         fprintf(stderr, "Expected output with output_scale=1:\n");
237
238         fprintf(stderr, "Mode -n:\n"
239                 "  window size %dx%d px\n"
240                 "  Red box with a blue box in the upper left part.\n"
241                 "  The blue box has white right edge, black bottom edge,\n"
242                 "  and thin green left and top edges that can really\n"
243                 "  be seen only when zoomed in.\n\n",
244                 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
245
246         fprintf(stderr, "Mode -b:\n"
247                 "  window size %dx%d px\n"
248                 "  Blue box with green top and left edge,\n"
249                 "  thick white right edge with a hint of red,\n"
250                 "  and a hint of black in bottom edge.\n\n",
251                 SURFACE_WIDTH, SURFACE_HEIGHT);
252
253         fprintf(stderr, "Mode -s:\n"
254                 "  window size %.0fx%.0f px\n"
255                 "  The same as mode -b, but scaled a lot smaller.\n\n",
256                 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
257
258         fprintf(stderr, "Mode -d:\n"
259                 "  window size %dx%d px\n"
260                 "  This is horizontally squashed version of the -n mode.\n\n",
261                 SURFACE_WIDTH, SURFACE_HEIGHT);
262 }
263
264 int
265 main(int argc, char *argv[])
266 {
267         struct box box;
268         struct display *d;
269         struct timeval tv;
270         int i;
271
272         box.mode = MODE_SRC_DST;
273
274         for (i = 1; i < argc; i++) {
275                 if (strcmp("-s", argv[i]) == 0)
276                         box.mode = MODE_SRC_ONLY;
277                 else if (strcmp("-d", argv[i]) == 0)
278                         box.mode = MODE_DST_ONLY;
279                 else if (strcmp("-b", argv[i]) == 0)
280                         box.mode = MODE_SRC_DST;
281                 else if (strcmp("-n", argv[i]) == 0)
282                         box.mode = MODE_NO_VIEWPORT;
283                 else {
284                         usage(argv[0]);
285                         exit(1);
286                 }
287         }
288
289         d = display_create(&argc, argv);
290         if (d == NULL) {
291                 fprintf(stderr, "failed to create display: %m\n");
292                 return -1;
293         }
294
295         gettimeofday(&tv, NULL);
296         srandom(tv.tv_usec);
297
298         box.width = BUFFER_WIDTH / BUFFER_SCALE;
299         box.height = BUFFER_HEIGHT / BUFFER_SCALE;
300         box.display = d;
301         box.window = window_create(d);
302         box.widget = window_add_widget(box.window, &box);
303         window_set_title(box.window, "Scaler Test Box");
304         window_set_buffer_scale(box.window, BUFFER_SCALE);
305
306         widget_set_resize_handler(box.widget, resize_handler);
307         widget_set_redraw_handler(box.widget, redraw_handler);
308         widget_set_button_handler(box.widget, button_handler);
309         widget_set_default_cursor(box.widget, CURSOR_HAND1);
310         widget_set_touch_down_handler(box.widget, touch_down_handler);
311
312         window_schedule_resize(box.window, box.width, box.height);
313
314         display_set_user_data(box.display, &box);
315         display_set_global_handler(box.display, global_handler);
316
317         display_run(d);
318
319         widget_destroy(box.widget);
320         window_destroy(box.window);
321         display_destroy(d);
322
323         return 0;
324 }