Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * Copyright © 2009 Chris Wilson |
| 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> |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 27 | #include <stdbool.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 30 | #include <libgen.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <math.h> |
| 33 | #include <time.h> |
| 34 | #include <cairo.h> |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 35 | #include <assert.h> |
| 36 | #include <linux/input.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 37 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 38 | #include <wayland-client.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 39 | |
| 40 | #include "window.h" |
Kristian Høgsberg | 5a315bc | 2012-05-15 22:33:43 -0400 | [diff] [blame] | 41 | #include "../shared/cairo-util.h" |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 42 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 43 | struct image { |
| 44 | struct window *window; |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 45 | struct widget *widget; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 46 | struct display *display; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 47 | char *filename; |
| 48 | cairo_surface_t *image; |
Kristian Høgsberg | df0faf7 | 2012-07-23 22:00:21 -0400 | [diff] [blame] | 49 | int fullscreen; |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 50 | int *image_counter; |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 51 | int32_t width, height; |
| 52 | |
| 53 | struct { |
| 54 | double x; |
| 55 | double y; |
| 56 | } pointer; |
| 57 | bool button_pressed; |
| 58 | |
| 59 | bool initialized; |
| 60 | cairo_matrix_t matrix; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 63 | static double |
| 64 | get_scale(struct image *image) |
| 65 | { |
| 66 | assert(image->matrix.xy == 0.0 && |
| 67 | image->matrix.yx == 0.0 && |
| 68 | image->matrix.xx == image->matrix.yy); |
| 69 | return image->matrix.xx; |
| 70 | } |
| 71 | |
| 72 | static void |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 73 | clamp_view(struct image *image) |
| 74 | { |
| 75 | struct rectangle allocation; |
| 76 | double scale = get_scale(image); |
| 77 | double sw, sh; |
| 78 | |
| 79 | sw = image->width * scale; |
| 80 | sh = image->height * scale; |
| 81 | widget_get_allocation(image->widget, &allocation); |
| 82 | |
Kristian Høgsberg | d3bf676 | 2012-10-30 15:46:25 -0400 | [diff] [blame] | 83 | if (sw < allocation.width) { |
| 84 | image->matrix.x0 = |
| 85 | (allocation.width - image->width * scale) / 2; |
| 86 | } else { |
| 87 | if (image->matrix.x0 > 0.0) |
| 88 | image->matrix.x0 = 0.0; |
| 89 | if (sw + image->matrix.x0 < allocation.width) |
| 90 | image->matrix.x0 = allocation.width - sw; |
| 91 | } |
| 92 | |
| 93 | if (sh < allocation.width) { |
| 94 | image->matrix.y0 = |
| 95 | (allocation.height - image->height * scale) / 2; |
| 96 | } else { |
| 97 | if (image->matrix.y0 > 0.0) |
| 98 | image->matrix.y0 = 0.0; |
| 99 | if (sh + image->matrix.y0 < allocation.height) |
| 100 | image->matrix.y0 = allocation.height - sh; |
| 101 | } |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 104 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 105 | redraw_handler(struct widget *widget, void *data) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 106 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 107 | struct image *image = data; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 108 | struct rectangle allocation; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 109 | cairo_t *cr; |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 110 | cairo_surface_t *surface; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 111 | double width, height, doc_aspect, window_aspect, scale; |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 112 | cairo_matrix_t matrix; |
| 113 | cairo_matrix_t translate; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 114 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 115 | surface = window_get_surface(image->window); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 116 | cr = cairo_create(surface); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 117 | widget_get_allocation(image->widget, &allocation); |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 118 | cairo_rectangle(cr, allocation.x, allocation.y, |
| 119 | allocation.width, allocation.height); |
| 120 | cairo_clip(cr); |
| 121 | cairo_push_group(cr); |
| 122 | cairo_translate(cr, allocation.x, allocation.y); |
| 123 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 124 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 125 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 126 | cairo_paint(cr); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 127 | |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 128 | if (!image->initialized) { |
| 129 | image->initialized = true; |
| 130 | width = cairo_image_surface_get_width(image->image); |
| 131 | height = cairo_image_surface_get_height(image->image); |
| 132 | |
| 133 | doc_aspect = width / height; |
| 134 | window_aspect = (double) allocation.width / allocation.height; |
| 135 | if (doc_aspect < window_aspect) |
| 136 | scale = allocation.height / height; |
| 137 | else |
| 138 | scale = allocation.width / width; |
| 139 | |
| 140 | image->width = width; |
| 141 | image->height = height; |
| 142 | cairo_matrix_init_scale(&image->matrix, scale, scale); |
| 143 | |
Kristian Høgsberg | d3bf676 | 2012-10-30 15:46:25 -0400 | [diff] [blame] | 144 | clamp_view(image); |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | matrix = image->matrix; |
| 148 | cairo_matrix_init_translate(&translate, allocation.x, allocation.y); |
| 149 | cairo_matrix_multiply(&matrix, &matrix, &translate); |
| 150 | cairo_set_matrix(cr, &matrix); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 151 | |
| 152 | cairo_set_source_surface(cr, image->image, 0, 0); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 153 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 154 | cairo_paint(cr); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 155 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 156 | cairo_pop_group_to_source(cr); |
| 157 | cairo_paint(cr); |
| 158 | cairo_destroy(cr); |
| 159 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 160 | cairo_surface_destroy(surface); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 163 | static void |
Kristian Høgsberg | a369ff5 | 2012-10-30 15:09:49 -0400 | [diff] [blame] | 164 | resize_handler(struct widget *widget, |
| 165 | int32_t width, int32_t height, void *data) |
| 166 | { |
| 167 | struct image *image = data; |
| 168 | |
Kristian Høgsberg | d3bf676 | 2012-10-30 15:46:25 -0400 | [diff] [blame] | 169 | clamp_view(image); |
Kristian Høgsberg | a369ff5 | 2012-10-30 15:09:49 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 173 | keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 174 | struct input *device, void *data) |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 175 | { |
| 176 | struct image *image = data; |
| 177 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 178 | window_schedule_redraw(image->window); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 181 | static int |
| 182 | enter_handler(struct widget *widget, |
| 183 | struct input *input, |
| 184 | float x, float y, void *data) |
| 185 | { |
| 186 | struct image *image = data; |
| 187 | struct rectangle allocation; |
| 188 | |
| 189 | widget_get_allocation(image->widget, &allocation); |
| 190 | x -= allocation.x; |
| 191 | y -= allocation.y; |
| 192 | |
| 193 | image->pointer.x = x; |
| 194 | image->pointer.y = y; |
| 195 | |
| 196 | return 1; |
| 197 | } |
| 198 | |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 199 | static void |
| 200 | move_viewport(struct image *image, double dx, double dy) |
| 201 | { |
| 202 | double scale = get_scale(image); |
| 203 | |
| 204 | if (!image->initialized) |
| 205 | return; |
| 206 | |
| 207 | cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale); |
Kristian Høgsberg | d3bf676 | 2012-10-30 15:46:25 -0400 | [diff] [blame] | 208 | clamp_view(image); |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 209 | |
| 210 | window_schedule_redraw(image->window); |
| 211 | } |
| 212 | |
| 213 | static int |
| 214 | motion_handler(struct widget *widget, |
| 215 | struct input *input, uint32_t time, |
| 216 | float x, float y, void *data) |
| 217 | { |
| 218 | struct image *image = data; |
| 219 | struct rectangle allocation; |
| 220 | |
| 221 | widget_get_allocation(image->widget, &allocation); |
| 222 | x -= allocation.x; |
| 223 | y -= allocation.y; |
| 224 | |
| 225 | if (image->button_pressed) |
| 226 | move_viewport(image, image->pointer.x - x, |
| 227 | image->pointer.y - y); |
| 228 | |
| 229 | image->pointer.x = x; |
| 230 | image->pointer.y = y; |
| 231 | |
| 232 | return image->button_pressed ? CURSOR_DRAGGING : CURSOR_LEFT_PTR; |
| 233 | } |
| 234 | |
| 235 | static void |
| 236 | button_handler(struct widget *widget, |
| 237 | struct input *input, uint32_t time, |
| 238 | uint32_t button, |
| 239 | enum wl_pointer_button_state state, |
| 240 | void *data) |
| 241 | { |
| 242 | struct image *image = data; |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 243 | |
| 244 | if (button == BTN_LEFT) { |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 245 | image->button_pressed = |
| 246 | state == WL_POINTER_BUTTON_STATE_PRESSED; |
| 247 | |
Kristian Høgsberg | 9468708 | 2012-10-30 15:50:37 -0400 | [diff] [blame] | 248 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 249 | input_set_pointer_image(input, CURSOR_DRAGGING); |
| 250 | else |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 251 | input_set_pointer_image(input, CURSOR_LEFT_PTR); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static void |
| 256 | zoom(struct image *image, double scale) |
| 257 | { |
| 258 | double x = image->pointer.x; |
| 259 | double y = image->pointer.y; |
| 260 | cairo_matrix_t scale_matrix; |
| 261 | |
| 262 | if (!image->initialized) |
| 263 | return; |
| 264 | |
| 265 | if (get_scale(image) * scale > 20.0 || |
| 266 | get_scale(image) * scale < 0.02) |
| 267 | return; |
| 268 | |
| 269 | cairo_matrix_init_identity(&scale_matrix); |
| 270 | cairo_matrix_translate(&scale_matrix, x, y); |
| 271 | cairo_matrix_scale(&scale_matrix, scale, scale); |
| 272 | cairo_matrix_translate(&scale_matrix, -x, -y); |
| 273 | |
| 274 | cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix); |
Kristian Høgsberg | d3bf676 | 2012-10-30 15:46:25 -0400 | [diff] [blame] | 275 | clamp_view(image); |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static void |
Kristian Høgsberg | 719b215 | 2012-10-30 15:42:20 -0400 | [diff] [blame] | 279 | key_handler(struct window *window, struct input *input, uint32_t time, |
| 280 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 281 | void *data) |
| 282 | { |
| 283 | struct image *image = data; |
| 284 | |
| 285 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 286 | return; |
| 287 | |
| 288 | switch (sym) { |
| 289 | case XKB_KEY_minus: |
| 290 | zoom(image, 0.8); |
| 291 | window_schedule_redraw(image->window); |
| 292 | break; |
| 293 | case XKB_KEY_equal: |
| 294 | case XKB_KEY_plus: |
| 295 | zoom(image, 1.2); |
| 296 | window_schedule_redraw(image->window); |
| 297 | break; |
| 298 | case XKB_KEY_1: |
| 299 | image->matrix.xx = 1.0; |
| 300 | image->matrix.xy = 0.0; |
| 301 | image->matrix.yx = 0.0; |
| 302 | image->matrix.yy = 1.0; |
| 303 | clamp_view(image); |
| 304 | window_schedule_redraw(image->window); |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 310 | axis_handler(struct widget *widget, struct input *input, uint32_t time, |
| 311 | uint32_t axis, wl_fixed_t value, void *data) |
| 312 | { |
| 313 | struct image *image = data; |
| 314 | |
| 315 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL && |
| 316 | input_get_modifiers(input) == MOD_CONTROL_MASK) { |
| 317 | /* set zoom level to 2% per 10 axis units */ |
| 318 | zoom(image, (1.0 - wl_fixed_to_double(value) / 500.0)); |
| 319 | |
| 320 | window_schedule_redraw(image->window); |
| 321 | } else if (input_get_modifiers(input) == 0) { |
| 322 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) |
| 323 | move_viewport(image, 0, wl_fixed_to_double(value)); |
| 324 | else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) |
| 325 | move_viewport(image, wl_fixed_to_double(value), 0); |
| 326 | } |
| 327 | } |
| 328 | |
Kristian Høgsberg | df0faf7 | 2012-07-23 22:00:21 -0400 | [diff] [blame] | 329 | static void |
| 330 | fullscreen_handler(struct window *window, void *data) |
| 331 | { |
| 332 | struct image *image = data; |
| 333 | |
| 334 | image->fullscreen ^= 1; |
| 335 | window_set_fullscreen(window, image->fullscreen); |
| 336 | } |
| 337 | |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 338 | static void |
| 339 | close_handler(struct window *window, void *data) |
| 340 | { |
| 341 | struct image *image = data; |
| 342 | |
| 343 | *image->image_counter -= 1; |
| 344 | |
| 345 | if (*image->image_counter == 0) |
| 346 | display_exit(image->display); |
| 347 | |
| 348 | widget_destroy(image->widget); |
| 349 | window_destroy(image->window); |
| 350 | |
| 351 | free(image); |
| 352 | } |
| 353 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 354 | static struct image * |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 355 | image_create(struct display *display, const char *filename, |
| 356 | int *image_counter) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 357 | { |
| 358 | struct image *image; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 359 | char *b, *copy, title[512];; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 360 | |
| 361 | image = malloc(sizeof *image); |
| 362 | if (image == NULL) |
| 363 | return image; |
| 364 | memset(image, 0, sizeof *image); |
| 365 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 366 | copy = strdup(filename); |
| 367 | b = basename(copy); |
| 368 | snprintf(title, sizeof title, "Wayland Image - %s", b); |
| 369 | free(copy); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 370 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 371 | image->filename = strdup(filename); |
| 372 | image->image = load_cairo_surface(filename); |
Juan Zhao | 19a4c2d | 2012-08-06 19:45:43 -0700 | [diff] [blame] | 373 | |
| 374 | if (!image->image) { |
| 375 | fprintf(stderr, "could not find the image %s!\n", b); |
Rob Bradford | c9213e7 | 2013-07-26 16:29:41 +0100 | [diff] [blame^] | 376 | free(image->filename); |
Rob Bradford | 409f79a | 2013-01-10 19:48:55 +0000 | [diff] [blame] | 377 | free(image); |
Juan Zhao | 19a4c2d | 2012-08-06 19:45:43 -0700 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
| 380 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 381 | image->window = window_create(display); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 382 | image->widget = frame_create(image->window, image); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 383 | window_set_title(image->window, title); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 384 | image->display = display; |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 385 | image->image_counter = image_counter; |
| 386 | *image_counter += 1; |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 387 | image->initialized = false; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 388 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 389 | window_set_user_data(image->window, image); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 390 | widget_set_redraw_handler(image->widget, redraw_handler); |
Kristian Høgsberg | a369ff5 | 2012-10-30 15:09:49 -0400 | [diff] [blame] | 391 | widget_set_resize_handler(image->widget, resize_handler); |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 392 | window_set_keyboard_focus_handler(image->window, |
| 393 | keyboard_focus_handler); |
Kristian Høgsberg | df0faf7 | 2012-07-23 22:00:21 -0400 | [diff] [blame] | 394 | window_set_fullscreen_handler(image->window, fullscreen_handler); |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 395 | window_set_close_handler(image->window, close_handler); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 396 | |
Jonas Ådahl | 972d506 | 2012-09-27 18:40:46 +0200 | [diff] [blame] | 397 | widget_set_enter_handler(image->widget, enter_handler); |
| 398 | widget_set_motion_handler(image->widget, motion_handler); |
| 399 | widget_set_button_handler(image->widget, button_handler); |
| 400 | widget_set_axis_handler(image->widget, axis_handler); |
Kristian Høgsberg | 719b215 | 2012-10-30 15:42:20 -0400 | [diff] [blame] | 401 | window_set_key_handler(image->window, key_handler); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 402 | widget_schedule_resize(image->widget, 500, 400); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 403 | |
| 404 | return image; |
| 405 | } |
| 406 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 407 | int |
| 408 | main(int argc, char *argv[]) |
| 409 | { |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 410 | struct display *d; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 411 | int i; |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 412 | int image_counter = 0; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 413 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 414 | d = display_create(&argc, argv); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 415 | if (d == NULL) { |
| 416 | fprintf(stderr, "failed to create display: %m\n"); |
| 417 | return -1; |
| 418 | } |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 419 | |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 420 | for (i = 1; i < argc; i++) |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 421 | image_create(d, argv[i], &image_counter); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 422 | |
Philipp Brüschweiler | 1f54f17 | 2012-08-13 21:16:47 +0200 | [diff] [blame] | 423 | if (image_counter > 0) |
| 424 | display_run(d); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |