Jonny Lamb | 92d90f2 | 2013-11-26 18:19:48 +0100 | [diff] [blame] | 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 <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <cairo.h> |
| 27 | |
| 28 | #include <linux/input.h> |
| 29 | |
| 30 | #include "window.h" |
| 31 | #include "scaler-client-protocol.h" |
| 32 | |
| 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; |
| 42 | |
| 43 | struct box { |
| 44 | struct display *display; |
| 45 | struct window *window; |
| 46 | struct widget *widget; |
| 47 | int width, height; |
| 48 | |
| 49 | struct wl_scaler *scaler; |
| 50 | struct wl_surface_scaler *surface_scaler; |
| 51 | }; |
| 52 | |
| 53 | static void |
| 54 | resize_handler(struct widget *widget, |
| 55 | int32_t width, int32_t height, void *data) |
| 56 | { |
| 57 | struct box *box = data; |
| 58 | |
| 59 | /* Dont resize me */ |
| 60 | widget_set_size(box->widget, box->width, box->height); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | redraw_handler(struct widget *widget, void *data) |
| 65 | { |
| 66 | struct box *box = data; |
| 67 | cairo_surface_t *surface; |
| 68 | cairo_t *cr; |
| 69 | |
| 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"); |
| 74 | return; |
| 75 | } |
| 76 | |
| 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); |
| 81 | |
| 82 | /* red background */ |
| 83 | cairo_set_source_rgba(cr, 255, 0, 0, 255); |
| 84 | cairo_paint(cr); |
| 85 | |
| 86 | /* blue box */ |
| 87 | cairo_set_source_rgba(cr, 0, 0, 255, 255); |
| 88 | cairo_rectangle(cr, 0, 0, RECT_W, RECT_H); |
| 89 | cairo_fill(cr); |
| 90 | |
| 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); |
| 95 | cairo_stroke(cr); |
| 96 | |
| 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); |
| 101 | cairo_stroke(cr); |
| 102 | |
| 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); |
| 109 | cairo_stroke(cr); |
| 110 | |
| 111 | cairo_destroy(cr); |
| 112 | |
| 113 | /* TODO: buffer_transform */ |
| 114 | |
| 115 | cairo_surface_destroy(surface); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | global_handler(struct display *display, uint32_t name, |
| 120 | const char *interface, uint32_t version, void *data) |
| 121 | { |
| 122 | struct box *box = data; |
| 123 | wl_fixed_t src_x, src_y, src_width, src_height; |
| 124 | |
| 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. |
| 127 | * |
| 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. |
| 131 | */ |
| 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); |
| 136 | |
| 137 | if (strcmp(interface, "wl_scaler") == 0) { |
| 138 | box->scaler = display_bind(display, name, |
| 139 | &wl_scaler_interface, 1); |
| 140 | |
| 141 | box->surface_scaler = wl_scaler_get_surface_scaler(box->scaler, |
| 142 | widget_get_wl_surface(box->widget)); |
| 143 | |
| 144 | wl_surface_scaler_set(box->surface_scaler, |
| 145 | src_x, src_y, src_width, src_height, |
| 146 | SURFACE_WIDTH, SURFACE_HEIGHT); /* dst */ |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void |
| 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) |
| 154 | { |
| 155 | struct box *box = data; |
| 156 | |
| 157 | if (button != BTN_LEFT) |
| 158 | return; |
| 159 | |
| 160 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 161 | window_move(box->window, input, |
| 162 | display_get_serial(box->display)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static void |
| 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) |
| 170 | { |
| 171 | struct box *box = data; |
| 172 | window_move(box->window, input, |
| 173 | display_get_serial(box->display)); |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | main(int argc, char *argv[]) |
| 178 | { |
| 179 | struct box box; |
| 180 | struct display *d; |
| 181 | struct timeval tv; |
| 182 | |
| 183 | d = display_create(&argc, argv); |
| 184 | if (d == NULL) { |
| 185 | fprintf(stderr, "failed to create display: %m\n"); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | gettimeofday(&tv, NULL); |
| 190 | srandom(tv.tv_usec); |
| 191 | |
| 192 | box.width = BUFFER_WIDTH / BUFFER_SCALE; |
| 193 | box.height = BUFFER_HEIGHT / BUFFER_SCALE; |
| 194 | box.display = d; |
| 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); |
| 199 | |
| 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); |
| 205 | |
| 206 | window_schedule_resize(box.window, box.width, box.height); |
| 207 | |
| 208 | display_set_user_data(box.display, &box); |
| 209 | display_set_global_handler(box.display, global_handler); |
| 210 | |
| 211 | display_run(d); |
| 212 | |
| 213 | window_destroy(box.window); |
| 214 | return 0; |
| 215 | } |