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