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