2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Collabora, Ltd.
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.
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
28 #include <linux/input.h>
31 #include "scaler-client-protocol.h"
33 #define BUFFER_SCALE 2
34 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
35 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
36 static const int SURFACE_WIDTH = 55 * 4;
37 static const int SURFACE_HEIGHT = 77 * 4;
38 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
39 static const double RECT_Y = 25 * BUFFER_SCALE;
40 static const double RECT_W = 55 * BUFFER_SCALE;
41 static const double RECT_H = 77 * BUFFER_SCALE;
44 struct display *display;
45 struct window *window;
46 struct widget *widget;
49 struct wl_scaler *scaler;
50 struct wl_viewport *viewport;
54 resize_handler(struct widget *widget,
55 int32_t width, int32_t height, void *data)
57 struct box *box = data;
60 widget_set_size(box->widget, box->width, box->height);
64 redraw_handler(struct widget *widget, void *data)
66 struct box *box = data;
67 cairo_surface_t *surface;
70 surface = window_get_surface(box->window);
71 if (surface == NULL ||
72 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
73 fprintf(stderr, "failed to create cairo egl surface\n");
77 cr = cairo_create(surface);
78 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
79 cairo_set_line_width(cr, 1.0);
80 cairo_translate(cr, RECT_X, RECT_Y);
83 cairo_set_source_rgba(cr, 255, 0, 0, 255);
87 cairo_set_source_rgba(cr, 0, 0, 255, 255);
88 cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
91 /* black border outside the box */
92 cairo_set_source_rgb(cr, 0, 0, 0);
93 cairo_move_to(cr, 0, RECT_H + 0.5);
94 cairo_line_to(cr, RECT_W, RECT_H + 0.5);
97 /* white border inside the box */
98 cairo_set_source_rgb(cr, 1, 1, 1);
99 cairo_move_to(cr, RECT_W - 0.5, 0);
100 cairo_line_to(cr, RECT_W - 0.5, RECT_H);
103 /* the green border on inside the box, to be split half by crop */
104 cairo_set_source_rgb(cr, 0, 1, 0);
105 cairo_move_to(cr, 0.5, RECT_H);
106 cairo_line_to(cr, 0.5, 0);
107 cairo_move_to(cr, 0, 0.5);
108 cairo_line_to(cr, RECT_W, 0.5);
113 /* TODO: buffer_transform */
115 cairo_surface_destroy(surface);
119 global_handler(struct display *display, uint32_t name,
120 const char *interface, uint32_t version, void *data)
122 struct box *box = data;
123 wl_fixed_t src_x, src_y, src_width, src_height;
125 /* Cut the green border in half, take white border fully in,
126 * and black border fully out. The borders are 1px wide in buffer.
128 * The gl-renderer uses linear texture sampling, this means the
129 * top and left edges go to 100% green, bottom goes to 50% blue/black,
130 * right edge has thick white sliding to 50% red.
132 src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
133 src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
134 src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
135 src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
137 if (strcmp(interface, "wl_scaler") == 0) {
138 box->scaler = display_bind(display, name,
139 &wl_scaler_interface, 1);
141 box->viewport = wl_scaler_get_viewport(box->scaler,
142 widget_get_wl_surface(box->widget));
144 wl_viewport_set(box->viewport,
145 src_x, src_y, src_width, src_height,
146 SURFACE_WIDTH, SURFACE_HEIGHT); /* dst */
151 button_handler(struct widget *widget,
152 struct input *input, uint32_t time,
153 uint32_t button, enum wl_pointer_button_state state, void *data)
155 struct box *box = data;
157 if (button != BTN_LEFT)
160 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
161 window_move(box->window, input,
162 display_get_serial(box->display));
167 touch_down_handler(struct widget *widget, struct input *input,
168 uint32_t serial, uint32_t time, int32_t id,
169 float x, float y, void *data)
171 struct box *box = data;
172 window_move(box->window, input,
173 display_get_serial(box->display));
177 main(int argc, char *argv[])
183 d = display_create(&argc, argv);
185 fprintf(stderr, "failed to create display: %m\n");
189 gettimeofday(&tv, NULL);
192 box.width = BUFFER_WIDTH / BUFFER_SCALE;
193 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
195 box.window = window_create(d);
196 box.widget = window_add_widget(box.window, &box);
197 window_set_title(box.window, "Scaler Test Box");
198 window_set_buffer_scale(box.window, BUFFER_SCALE);
200 widget_set_resize_handler(box.widget, resize_handler);
201 widget_set_redraw_handler(box.widget, redraw_handler);
202 widget_set_button_handler(box.widget, button_handler);
203 widget_set_default_cursor(box.widget, CURSOR_HAND1);
204 widget_set_touch_down_handler(box.widget, touch_down_handler);
206 window_schedule_resize(box.window, box.width, box.height);
208 display_set_user_data(box.display, &box);
209 display_set_global_handler(box.display, global_handler);
213 window_destroy(box.window);