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 <linux/input.h>
40
41 #include <wayland-client.h>
42
43 #include "window.h"
44 #include "shared/cairo-util.h"
45
46 struct image {
47 struct window *window;
48 struct widget *widget;
49 struct display *display;
50 char *filename;
51 cairo_surface_t *image;
52 int fullscreen;
53 int *image_counter;
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;
64 };
65
66 static double
get_scale(struct image * image)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
clamp_view(struct image * image)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
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
96 if (sh < allocation.height) {
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 }
105 }
106
107 static void
redraw_handler(struct widget * widget,void * data)108 redraw_handler(struct widget *widget, void *data)
109 {
110 struct image *image = data;
111 struct rectangle allocation;
112 cairo_t *cr;
113 cairo_surface_t *surface;
114 double width, height, doc_aspect, window_aspect, scale;
115 cairo_matrix_t matrix;
116 cairo_matrix_t translate;
117
118 surface = window_get_surface(image->window);
119 cr = cairo_create(surface);
120 widget_get_allocation(image->widget, &allocation);
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
127 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
128 cairo_set_source_rgba(cr, 0, 0, 0, 1);
129 cairo_paint(cr);
130
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
147 clamp_view(image);
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);
154
155 cairo_set_source_surface(cr, image->image, 0, 0);
156 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
157 cairo_paint(cr);
158
159 cairo_pop_group_to_source(cr);
160 cairo_paint(cr);
161 cairo_destroy(cr);
162
163 cairo_surface_destroy(surface);
164 }
165
166 static void
resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)167 resize_handler(struct widget *widget,
168 int32_t width, int32_t height, void *data)
169 {
170 struct image *image = data;
171
172 clamp_view(image);
173 }
174
175 static void
keyboard_focus_handler(struct window * window,struct input * device,void * data)176 keyboard_focus_handler(struct window *window,
177 struct input *device, void *data)
178 {
179 struct image *image = data;
180
181 window_schedule_redraw(image->window);
182 }
183
184 static int
enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)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
202 static void
move_viewport(struct image * image,double dx,double dy)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);
211 clamp_view(image);
212
213 window_schedule_redraw(image->window);
214 }
215
216 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)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
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)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;
246
247 if (button == BTN_LEFT) {
248 image->button_pressed =
249 state == WL_POINTER_BUTTON_STATE_PRESSED;
250
251 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
252 input_set_pointer_image(input, CURSOR_DRAGGING);
253 else
254 input_set_pointer_image(input, CURSOR_LEFT_PTR);
255 }
256 }
257
258 static void
zoom(struct image * image,double scale)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);
278 clamp_view(image);
279 }
280
281 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)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
axis_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t axis,wl_fixed_t value,void * data)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
332 static void
fullscreen_handler(struct window * window,void * data)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
341 static void
close_handler(void * data)342 close_handler(void *data)
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
357 static struct image *
image_create(struct display * display,const char * filename,int * image_counter)358 image_create(struct display *display, const char *filename,
359 int *image_counter)
360 {
361 struct image *image;
362 char *b, *copy, title[512];;
363
364 image = zalloc(sizeof *image);
365 if (image == NULL)
366 return image;
367
368 copy = strdup(filename);
369 b = basename(copy);
370 snprintf(title, sizeof title, "Wayland Image - %s", b);
371 free(copy);
372
373 image->filename = strdup(filename);
374 image->image = load_cairo_surface(filename);
375
376 if (!image->image) {
377 free(image->filename);
378 free(image);
379 return NULL;
380 }
381
382 image->window = window_create(display);
383 image->widget = window_frame_create(image->window, image);
384 window_set_title(image->window, title);
385 image->display = display;
386 image->image_counter = image_counter;
387 *image_counter += 1;
388 image->initialized = false;
389
390 window_set_user_data(image->window, image);
391 widget_set_redraw_handler(image->widget, redraw_handler);
392 widget_set_resize_handler(image->widget, resize_handler);
393 window_set_keyboard_focus_handler(image->window,
394 keyboard_focus_handler);
395 window_set_fullscreen_handler(image->window, fullscreen_handler);
396 window_set_close_handler(image->window, close_handler);
397
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);
402 window_set_key_handler(image->window, key_handler);
403 widget_schedule_resize(image->widget, 500, 400);
404
405 return image;
406 }
407
408 int
main(int argc,char * argv[])409 main(int argc, char *argv[])
410 {
411 struct display *d;
412 int i;
413 int image_counter = 0;
414
415 if (argc <= 1 || argv[1][0]=='-') {
416 printf("Usage: %s image...\n", argv[0]);
417 return 1;
418 }
419
420 d = display_create(&argc, argv);
421 if (d == NULL) {
422 fprintf(stderr, "failed to create display: %m\n");
423 return -1;
424 }
425
426 for (i = 1; i < argc; i++)
427 image_create(d, argv[i], &image_counter);
428
429 if (image_counter > 0)
430 display_run(d);
431
432 display_destroy(d);
433
434 return 0;
435 }
436