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