2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2009 Chris Wilson
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.
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
36 #include <linux/input.h>
38 #include <wayland-client.h>
41 #include "../shared/cairo-util.h"
44 struct window *window;
45 struct widget *widget;
46 struct display *display;
48 cairo_surface_t *image;
51 int32_t width, height;
60 cairo_matrix_t matrix;
64 get_scale(struct image *image)
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;
73 clamp_view(struct image *image)
75 struct rectangle allocation;
76 double scale = get_scale(image);
79 sw = image->width * scale;
80 sh = image->height * scale;
81 widget_get_allocation(image->widget, &allocation);
83 if (sw < allocation.width) {
85 (allocation.width - image->width * scale) / 2;
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;
93 if (sh < allocation.width) {
95 (allocation.height - image->height * scale) / 2;
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;
105 redraw_handler(struct widget *widget, void *data)
107 struct image *image = data;
108 struct rectangle allocation;
110 cairo_surface_t *surface;
111 double width, height, doc_aspect, window_aspect, scale;
112 cairo_matrix_t matrix;
113 cairo_matrix_t translate;
115 surface = window_get_surface(image->window);
116 cr = cairo_create(surface);
117 widget_get_allocation(image->widget, &allocation);
118 cairo_rectangle(cr, allocation.x, allocation.y,
119 allocation.width, allocation.height);
121 cairo_push_group(cr);
122 cairo_translate(cr, allocation.x, allocation.y);
124 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
125 cairo_set_source_rgba(cr, 0, 0, 0, 1);
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);
133 doc_aspect = width / height;
134 window_aspect = (double) allocation.width / allocation.height;
135 if (doc_aspect < window_aspect)
136 scale = allocation.height / height;
138 scale = allocation.width / width;
140 image->width = width;
141 image->height = height;
142 cairo_matrix_init_scale(&image->matrix, scale, scale);
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);
152 cairo_set_source_surface(cr, image->image, 0, 0);
153 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
156 cairo_pop_group_to_source(cr);
160 cairo_surface_destroy(surface);
164 resize_handler(struct widget *widget,
165 int32_t width, int32_t height, void *data)
167 struct image *image = data;
173 keyboard_focus_handler(struct window *window,
174 struct input *device, void *data)
176 struct image *image = data;
178 window_schedule_redraw(image->window);
182 enter_handler(struct widget *widget,
184 float x, float y, void *data)
186 struct image *image = data;
187 struct rectangle allocation;
189 widget_get_allocation(image->widget, &allocation);
193 image->pointer.x = x;
194 image->pointer.y = y;
200 move_viewport(struct image *image, double dx, double dy)
202 double scale = get_scale(image);
204 if (!image->initialized)
207 cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale);
210 window_schedule_redraw(image->window);
214 motion_handler(struct widget *widget,
215 struct input *input, uint32_t time,
216 float x, float y, void *data)
218 struct image *image = data;
219 struct rectangle allocation;
221 widget_get_allocation(image->widget, &allocation);
225 if (image->button_pressed)
226 move_viewport(image, image->pointer.x - x,
227 image->pointer.y - y);
229 image->pointer.x = x;
230 image->pointer.y = y;
232 return image->button_pressed ? CURSOR_DRAGGING : CURSOR_LEFT_PTR;
236 button_handler(struct widget *widget,
237 struct input *input, uint32_t time,
239 enum wl_pointer_button_state state,
242 struct image *image = data;
244 if (button == BTN_LEFT) {
245 image->button_pressed =
246 state == WL_POINTER_BUTTON_STATE_PRESSED;
248 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
249 input_set_pointer_image(input, CURSOR_DRAGGING);
251 input_set_pointer_image(input, CURSOR_LEFT_PTR);
256 zoom(struct image *image, double scale)
258 double x = image->pointer.x;
259 double y = image->pointer.y;
260 cairo_matrix_t scale_matrix;
262 if (!image->initialized)
265 if (get_scale(image) * scale > 20.0 ||
266 get_scale(image) * scale < 0.02)
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);
274 cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix);
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,
283 struct image *image = data;
285 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
291 window_schedule_redraw(image->window);
296 window_schedule_redraw(image->window);
299 image->matrix.xx = 1.0;
300 image->matrix.xy = 0.0;
301 image->matrix.yx = 0.0;
302 image->matrix.yy = 1.0;
304 window_schedule_redraw(image->window);
310 axis_handler(struct widget *widget, struct input *input, uint32_t time,
311 uint32_t axis, wl_fixed_t value, void *data)
313 struct image *image = data;
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));
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);
330 fullscreen_handler(struct window *window, void *data)
332 struct image *image = data;
334 image->fullscreen ^= 1;
335 window_set_fullscreen(window, image->fullscreen);
339 close_handler(void *data)
341 struct image *image = data;
343 *image->image_counter -= 1;
345 if (*image->image_counter == 0)
346 display_exit(image->display);
348 widget_destroy(image->widget);
349 window_destroy(image->window);
354 static struct image *
355 image_create(struct display *display, const char *filename,
359 char *b, *copy, title[512];;
361 image = zalloc(sizeof *image);
365 copy = strdup(filename);
367 snprintf(title, sizeof title, "Wayland Image - %s", b);
370 image->filename = strdup(filename);
371 image->image = load_cairo_surface(filename);
374 fprintf(stderr, "could not find the image %s!\n", b);
375 free(image->filename);
380 image->window = window_create(display);
381 image->widget = window_frame_create(image->window, image);
382 window_set_title(image->window, title);
383 image->display = display;
384 image->image_counter = image_counter;
386 image->initialized = false;
388 window_set_user_data(image->window, image);
389 widget_set_redraw_handler(image->widget, redraw_handler);
390 widget_set_resize_handler(image->widget, resize_handler);
391 window_set_keyboard_focus_handler(image->window,
392 keyboard_focus_handler);
393 window_set_fullscreen_handler(image->window, fullscreen_handler);
394 window_set_close_handler(image->window, close_handler);
396 widget_set_enter_handler(image->widget, enter_handler);
397 widget_set_motion_handler(image->widget, motion_handler);
398 widget_set_button_handler(image->widget, button_handler);
399 widget_set_axis_handler(image->widget, axis_handler);
400 window_set_key_handler(image->window, key_handler);
401 widget_schedule_resize(image->widget, 500, 400);
407 main(int argc, char *argv[])
411 int image_counter = 0;
413 d = display_create(&argc, argv);
415 fprintf(stderr, "failed to create display: %m\n");
419 for (i = 1; i < argc; i++)
420 image_create(d, argv[i], &image_counter);
422 if (image_counter > 0)