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 <linux/input.h>
37 #include <glib/poppler-document.h>
38 #include <glib/poppler-page.h>
40 #include <wayland-client.h>
45 struct window *window;
46 struct widget *widget;
47 struct display *display;
49 PopplerDocument *document;
56 redraw_handler(struct widget *widget, void *data)
58 struct view *view = data;
60 struct rectangle allocation;
61 cairo_surface_t *surface;
64 double width, height, doc_aspect, window_aspect, scale;
66 widget_get_allocation(view->widget, &allocation);
68 surface = window_get_surface(view->window);
70 cr = cairo_create(surface);
71 cairo_rectangle(cr, allocation.x, allocation.y,
72 allocation.width, allocation.height);
75 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
76 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
81 cairo_surface_destroy(surface);
85 page = poppler_document_get_page(view->document, view->page);
86 poppler_page_get_size(page, &width, &height);
87 doc_aspect = width / height;
88 window_aspect = (double) allocation.width / allocation.height;
89 if (doc_aspect < window_aspect)
90 scale = allocation.height / height;
92 scale = allocation.width / width;
93 cairo_translate(cr, allocation.x, allocation.y);
94 cairo_scale(cr, scale, scale);
96 (allocation.width - width * scale) / 2 / scale,
97 (allocation.height - height * scale) / 2 / scale);
98 cairo_rectangle(cr, 0, 0, width, height);
99 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
100 cairo_set_source_rgb(cr, 1, 1, 1);
102 poppler_page_render(page, cr);
104 cairo_surface_destroy(surface);
105 g_object_unref(G_OBJECT(page));
109 resize_handler(struct widget *widget,
110 int32_t width, int32_t height, void *data)
112 struct view *view = data;
114 widget_set_size(view->widget, width, height);
118 view_page_up(struct view *view)
124 window_schedule_redraw(view->window);
128 view_page_down(struct view *view)
130 if(!view->document ||
131 view->page >= poppler_document_get_n_pages(view->document) - 1) {
136 window_schedule_redraw(view->window);
140 button_handler(struct widget *widget, struct input *input, uint32_t time,
141 uint32_t button, enum wl_pointer_button_state state,
144 struct view *view = data;
146 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
154 view_page_down(view);
162 fullscreen_handler(struct window *window, void *data)
164 struct view *view = data;
166 view->fullscreen ^= 1;
167 window_set_fullscreen(window, view->fullscreen);
171 close_handler(struct window *window, void *data)
173 struct view *view = data;
175 *view->view_counter -= 1;
176 if (*view->view_counter == 0)
177 display_exit(view->display);
179 widget_destroy(view->widget);
180 window_destroy(view->window);
182 g_object_unref(view->document);
188 key_handler(struct window *window, struct input *input, uint32_t time,
189 uint32_t key, uint32_t unicode,
190 enum wl_keyboard_key_state state, void *data)
192 struct view *view = data;
194 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
202 view_page_down(view);
216 keyboard_focus_handler(struct window *window,
217 struct input *device, void *data)
219 struct view *view = data;
220 window_schedule_redraw(view->window);
224 view_create(struct display *display,
225 uint32_t key, const char *filename, int fullscreen, int *view_counter)
231 GError *error = NULL;
233 view = malloc(sizeof *view);
236 memset(view, 0, sizeof *view);
238 file = g_file_new_for_commandline_arg(filename);
239 basename = g_file_get_basename(file);
241 title = g_strdup("Wayland View");
243 title = g_strdup_printf("Wayland View - %s", basename);
247 view->document = poppler_document_new_from_file(g_file_get_uri(file),
251 title = g_strdup("File not found");
254 view->window = window_create(display);
255 view->widget = frame_create(view->window, view);
256 window_set_title(view->window, title);
258 view->display = display;
260 window_set_user_data(view->window, view);
261 window_set_key_handler(view->window, key_handler);
262 window_set_keyboard_focus_handler(view->window,
263 keyboard_focus_handler);
264 window_set_fullscreen_handler(view->window, fullscreen_handler);
265 window_set_close_handler(view->window, close_handler);
267 widget_set_button_handler(view->widget, button_handler);
268 widget_set_resize_handler(view->widget, resize_handler);
269 widget_set_redraw_handler(view->widget, redraw_handler);
273 view->fullscreen = fullscreen;
274 window_set_fullscreen(view->window, view->fullscreen);
276 window_schedule_resize(view->window, 500, 400);
277 view->view_counter = view_counter;
283 static int option_fullscreen;
285 static const struct weston_option view_options[] = {
286 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &option_fullscreen },
290 main(int argc, char *argv[])
294 int view_counter = 0;
298 argc = parse_options(view_options,
299 ARRAY_LENGTH(view_options), argc, argv);
301 d = display_create(argc, argv);
303 fprintf(stderr, "failed to create display: %m\n");
307 for (i = 1; i < argc; i++)
308 view_create (d, i, argv[i], option_fullscreen, &view_counter);
310 if (view_counter > 0)