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
38 #include <linux/input.h>
40 #include <wayland-client.h>
43 #include "../shared/cairo-util.h"
46 struct window *window;
47 struct widget *widget;
48 struct display *display;
50 cairo_surface_t *image;
53 int32_t width, height;
62 cairo_matrix_t matrix;
66 get_scale(struct image *image)
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;
75 clamp_view(struct image *image)
77 struct rectangle allocation;
78 double scale = get_scale(image);
81 sw = image->width * scale;
82 sh = image->height * scale;
83 widget_get_allocation(image->widget, &allocation);
85 if (sw < allocation.width) {
87 (allocation.width - image->width * scale) / 2;
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;
95 if (sh < allocation.width) {
97 (allocation.height - image->height * scale) / 2;
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;
107 redraw_handler(struct widget *widget, void *data)
109 struct image *image = data;
110 struct rectangle allocation;
112 cairo_surface_t *surface;
113 double width, height, doc_aspect, window_aspect, scale;
114 cairo_matrix_t matrix;
115 cairo_matrix_t translate;
117 surface = window_get_surface(image->window);
118 cr = cairo_create(surface);
119 widget_get_allocation(image->widget, &allocation);
120 cairo_rectangle(cr, allocation.x, allocation.y,
121 allocation.width, allocation.height);
123 cairo_push_group(cr);
124 cairo_translate(cr, allocation.x, allocation.y);
126 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
127 cairo_set_source_rgba(cr, 0, 0, 0, 1);
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);
135 doc_aspect = width / height;
136 window_aspect = (double) allocation.width / allocation.height;
137 if (doc_aspect < window_aspect)
138 scale = allocation.height / height;
140 scale = allocation.width / width;
142 image->width = width;
143 image->height = height;
144 cairo_matrix_init_scale(&image->matrix, scale, scale);
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);
154 cairo_set_source_surface(cr, image->image, 0, 0);
155 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
158 cairo_pop_group_to_source(cr);
162 cairo_surface_destroy(surface);
166 resize_handler(struct widget *widget,
167 int32_t width, int32_t height, void *data)
169 struct image *image = data;
175 keyboard_focus_handler(struct window *window,
176 struct input *device, void *data)
178 struct image *image = data;
180 window_schedule_redraw(image->window);
184 enter_handler(struct widget *widget,
186 float x, float y, void *data)
188 struct image *image = data;
189 struct rectangle allocation;
191 widget_get_allocation(image->widget, &allocation);
195 image->pointer.x = x;
196 image->pointer.y = y;
202 move_viewport(struct image *image, double dx, double dy)
204 double scale = get_scale(image);
206 if (!image->initialized)
209 cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale);
212 window_schedule_redraw(image->window);
216 motion_handler(struct widget *widget,
217 struct input *input, uint32_t time,
218 float x, float y, void *data)
220 struct image *image = data;
221 struct rectangle allocation;
223 widget_get_allocation(image->widget, &allocation);
227 if (image->button_pressed)
228 move_viewport(image, image->pointer.x - x,
229 image->pointer.y - y);
231 image->pointer.x = x;
232 image->pointer.y = y;
234 return image->button_pressed ? CURSOR_DRAGGING : CURSOR_LEFT_PTR;
238 button_handler(struct widget *widget,
239 struct input *input, uint32_t time,
241 enum wl_pointer_button_state state,
244 struct image *image = data;
246 if (button == BTN_LEFT) {
247 image->button_pressed =
248 state == WL_POINTER_BUTTON_STATE_PRESSED;
250 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
251 input_set_pointer_image(input, CURSOR_DRAGGING);
253 input_set_pointer_image(input, CURSOR_LEFT_PTR);
258 zoom(struct image *image, double scale)
260 double x = image->pointer.x;
261 double y = image->pointer.y;
262 cairo_matrix_t scale_matrix;
264 if (!image->initialized)
267 if (get_scale(image) * scale > 20.0 ||
268 get_scale(image) * scale < 0.02)
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);
276 cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix);
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,
285 struct image *image = data;
287 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
293 window_schedule_redraw(image->window);
298 window_schedule_redraw(image->window);
301 image->matrix.xx = 1.0;
302 image->matrix.xy = 0.0;
303 image->matrix.yx = 0.0;
304 image->matrix.yy = 1.0;
306 window_schedule_redraw(image->window);
312 axis_handler(struct widget *widget, struct input *input, uint32_t time,
313 uint32_t axis, wl_fixed_t value, void *data)
315 struct image *image = data;
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));
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);
332 fullscreen_handler(struct window *window, void *data)
334 struct image *image = data;
336 image->fullscreen ^= 1;
337 window_set_fullscreen(window, image->fullscreen);
341 close_handler(void *data)
343 struct image *image = data;
345 *image->image_counter -= 1;
347 if (*image->image_counter == 0)
348 display_exit(image->display);
350 widget_destroy(image->widget);
351 window_destroy(image->window);
356 static struct image *
357 image_create(struct display *display, const char *filename,
361 char *b, *copy, title[512];;
363 image = zalloc(sizeof *image);
367 copy = strdup(filename);
369 snprintf(title, sizeof title, "Wayland Image - %s", b);
372 image->filename = strdup(filename);
373 image->image = load_cairo_surface(filename);
376 fprintf(stderr, "could not find the image %s!\n", b);
377 free(image->filename);
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;
388 image->initialized = false;
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);
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);
409 main(int argc, char *argv[])
413 int image_counter = 0;
415 d = display_create(&argc, argv);
417 fprintf(stderr, "failed to create display: %m\n");
421 for (i = 1; i < argc; i++)
422 image_create(d, argv[i], &image_counter);
424 if (image_counter > 0)