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
35 #include <wayland-client.h>
38 #include "../shared/cairo-util.h"
41 struct window *window;
42 struct widget *widget;
43 struct display *display;
45 cairo_surface_t *image;
51 redraw_handler(struct widget *widget, void *data)
53 struct image *image = data;
54 struct rectangle allocation;
56 cairo_surface_t *surface;
57 double width, height, doc_aspect, window_aspect, scale;
59 widget_get_allocation(image->widget, &allocation);
61 surface = window_get_surface(image->window);
62 cr = cairo_create(surface);
63 widget_get_allocation(image->widget, &allocation);
64 cairo_rectangle(cr, allocation.x, allocation.y,
65 allocation.width, allocation.height);
68 cairo_translate(cr, allocation.x, allocation.y);
70 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
71 cairo_set_source_rgba(cr, 0, 0, 0, 1);
74 width = cairo_image_surface_get_width(image->image);
75 height = cairo_image_surface_get_height(image->image);
76 doc_aspect = width / height;
77 window_aspect = (double) allocation.width / allocation.height;
78 if (doc_aspect < window_aspect)
79 scale = allocation.height / height;
81 scale = allocation.width / width;
82 cairo_scale(cr, scale, scale);
84 (allocation.width - width * scale) / 2 / scale,
85 (allocation.height - height * scale) / 2 / scale);
87 cairo_set_source_surface(cr, image->image, 0, 0);
88 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
91 cairo_pop_group_to_source(cr);
95 cairo_surface_destroy(surface);
99 keyboard_focus_handler(struct window *window,
100 struct input *device, void *data)
102 struct image *image = data;
104 window_schedule_redraw(image->window);
108 fullscreen_handler(struct window *window, void *data)
110 struct image *image = data;
112 image->fullscreen ^= 1;
113 window_set_fullscreen(window, image->fullscreen);
117 close_handler(struct window *window, void *data)
119 struct image *image = data;
121 *image->image_counter -= 1;
123 if (*image->image_counter == 0)
124 display_exit(image->display);
126 widget_destroy(image->widget);
127 window_destroy(image->window);
132 static struct image *
133 image_create(struct display *display, const char *filename,
137 char *b, *copy, title[512];;
139 image = malloc(sizeof *image);
142 memset(image, 0, sizeof *image);
144 copy = strdup(filename);
146 snprintf(title, sizeof title, "Wayland Image - %s", b);
149 image->filename = strdup(filename);
150 image->image = load_cairo_surface(filename);
153 fprintf(stderr, "could not find the image %s!\n", b);
157 image->window = window_create(display);
158 image->widget = frame_create(image->window, image);
159 window_set_title(image->window, title);
160 image->display = display;
161 image->image_counter = image_counter;
164 window_set_user_data(image->window, image);
165 widget_set_redraw_handler(image->widget, redraw_handler);
166 window_set_keyboard_focus_handler(image->window,
167 keyboard_focus_handler);
168 window_set_fullscreen_handler(image->window, fullscreen_handler);
169 window_set_close_handler(image->window, close_handler);
171 widget_schedule_resize(image->widget, 500, 400);
177 main(int argc, char *argv[])
181 int image_counter = 0;
183 d = display_create(argc, argv);
185 fprintf(stderr, "failed to create display: %m\n");
189 for (i = 1; i < argc; i++)
190 image_create(d, argv[i], &image_counter);
192 if (image_counter > 0)