blob: d08303408807672da4448867210b07e0e1624fe4 [file] [log] [blame]
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -04001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2009 Chris Wilson
4 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
32#include <cairo.h>
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040033#include <glib.h>
34#include <linux/input.h>
35
36#include <glib/poppler-document.h>
37#include <glib/poppler-page.h>
38
39#include "wayland-util.h"
40#include "wayland-client.h"
41#include "wayland-glib.h"
42
43#include "window.h"
44
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040045struct view {
46 struct window *window;
47 struct display *display;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040048
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040049 PopplerDocument *document;
50 int page;
51 int fullscreen;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040052};
53
54static void
55view_draw(struct view *view)
56{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050057 struct rectangle allocation;
Kristian Høgsberg09531622010-06-14 23:22:15 -040058 cairo_surface_t *surface;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040059 cairo_t *cr;
60 PopplerPage *page;
61 double width, height, doc_aspect, window_aspect, scale;
62
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040063 window_draw(view->window);
64
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050065 window_get_child_allocation(view->window, &allocation);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040066
67 page = poppler_document_get_page(view->document, view->page);
68
Kristian Høgsberg09531622010-06-14 23:22:15 -040069 surface = window_get_surface(view->window);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040070
Kristian Høgsberg09531622010-06-14 23:22:15 -040071 cr = cairo_create(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050072 cairo_rectangle(cr, allocation.x, allocation.y,
73 allocation.width, allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -040074 cairo_clip(cr);
75
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040076 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
77 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
78 cairo_paint(cr);
79 poppler_page_get_size(page, &width, &height);
80 doc_aspect = width / height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050081 window_aspect = (double) allocation.width / allocation.height;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040082 if (doc_aspect < window_aspect)
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050083 scale = allocation.height / height;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040084 else
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050085 scale = allocation.width / width;
86 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040087 cairo_scale(cr, scale, scale);
88 cairo_translate(cr,
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050089 (allocation.width - width * scale) / 2 / scale,
90 (allocation.height - height * scale) / 2 / scale);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040091 cairo_rectangle(cr, 0, 0, width, height);
92 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
93 cairo_set_source_rgb(cr, 1, 1, 1);
94 cairo_fill(cr);
95 poppler_page_render(page, cr);
96 cairo_destroy(cr);
97 g_object_unref(G_OBJECT(page));
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -040098 window_flush(view->window);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -040099}
100
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400101static void
102redraw_handler(struct window *window, void *data)
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400103{
104 struct view *view = data;
105
106 view_draw(view);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400107}
108
109static void
Tim Wiederhake655467a2011-01-25 22:21:57 +0100110view_page_up(struct view *view)
111{
112 if(view->page <= 0)
113 return;
114
115 view->page--;
116 window_schedule_redraw(view->window);
117}
118
119static void
120view_page_down(struct view *view)
121{
122 if(view->page >= poppler_document_get_n_pages(view->document) - 1)
123 return;
124
125 view->page++;
126 window_schedule_redraw(view->window);
127}
128
129static void
130button_handler(struct window *window, struct input *input, uint32_t time,
131 int button, int state, void *data)
132{
133 struct view *view = data;
134
135 if(!state)
136 return;
137
138 switch(button) {
139 case 275:
140 view_page_up(view);
141 break;
142 case 276:
143 view_page_down(view);
144 break;
145 default:
146 break;
147 }
148}
149
150static void
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -0500151key_handler(struct window *window, struct input *input, uint32_t time,
152 uint32_t key, uint32_t unicode, uint32_t state, void *data)
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400153{
154 struct view *view = data;
155
Tim Wiederhake655467a2011-01-25 22:21:57 +0100156 if(!state)
157 return;
158
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400159 switch (key) {
160 case KEY_F11:
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400161 view->fullscreen ^= 1;
162 window_set_fullscreen(window, view->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400163 window_schedule_redraw(view->window);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400164 break;
165 case KEY_SPACE:
166 case KEY_PAGEDOWN:
Tim Wiederhake655467a2011-01-25 22:21:57 +0100167 case KEY_RIGHT:
168 case KEY_DOWN:
169 view_page_down(view);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400170 break;
171 case KEY_BACKSPACE:
172 case KEY_PAGEUP:
Tim Wiederhake655467a2011-01-25 22:21:57 +0100173 case KEY_LEFT:
174 case KEY_UP:
175 view_page_up(view);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400176 break;
177 default:
178 break;
179 }
180}
181
182static void
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400183keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400184 struct input *device, void *data)
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400185{
186 struct view *view = data;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400187 window_schedule_redraw(view->window);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400188}
189
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400190static struct view *
191view_create(struct display *display, uint32_t key, const char *filename)
192{
193 struct view *view;
194 gchar *basename;
195 gchar *title;
Tim Wiederhake9aad08e2011-01-25 22:21:58 +0100196 GFile *file = NULL;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400197 GError *error = NULL;
198
199 view = malloc(sizeof *view);
200 if (view == NULL)
201 return view;
202 memset(view, 0, sizeof *view);
203
Tim Wiederhake9aad08e2011-01-25 22:21:58 +0100204 file = g_file_new_for_commandline_arg(filename);
205 basename = g_file_get_basename(file);
206 if(!basename) {
207 title = "Wayland View";
208 } else {
209 title = g_strdup_printf("Wayland View - %s", basename);
210 g_free(basename);
211 }
212
213 view->document = poppler_document_new_from_file(g_file_get_uri(file),
214 NULL, &error);
215
216 if(error) {
217 title = "File not found";
218 view->document = NULL;
219 }
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400220
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221 view->window = window_create(display, 500, 400);
222 window_set_title(view->window, title);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400223 view->display = display;
224
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400225 window_set_user_data(view->window, view);
226 window_set_redraw_handler(view->window, redraw_handler);
227 window_set_key_handler(view->window, key_handler);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400228 window_set_keyboard_focus_handler(view->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400229 keyboard_focus_handler);
Tim Wiederhake655467a2011-01-25 22:21:57 +0100230 window_set_button_handler(view->window, button_handler);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400231 view->page = 0;
232 view_draw(view);
233
234 return view;
235}
236
237static const GOptionEntry option_entries[] = {
238 { NULL }
239};
240
241int
242main(int argc, char *argv[])
243{
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400244 struct display *d;
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400245 int i;
246
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400247 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200248 if (d == NULL) {
249 fprintf(stderr, "failed to create display: %m\n");
250 return -1;
251 }
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400252
Kristian Høgsberg00439612011-01-25 15:16:01 -0500253 for (i = 1; i < argc; i++)
254 view_create (d, i, argv[i]);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400255
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400256 display_run(d);
Kristian Høgsberg8f2f7732009-09-21 13:46:45 -0400257
258 return 0;
259}