2009-09-21 21:46:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
* Copyright © 2009 Chris Wilson
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <glib.h>
|
2011-10-24 19:49:38 +04:00
|
|
|
#include <gio/gio.h>
|
2009-09-21 21:46:45 +04:00
|
|
|
#include <linux/input.h>
|
|
|
|
|
|
|
|
#include <glib/poppler-document.h>
|
|
|
|
#include <glib/poppler-page.h>
|
|
|
|
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2009-09-21 21:46:45 +04:00
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
|
|
|
|
struct view {
|
|
|
|
struct window *window;
|
2012-01-12 19:48:51 +04:00
|
|
|
struct widget *widget;
|
2009-09-21 21:46:45 +04:00
|
|
|
struct display *display;
|
|
|
|
|
|
|
|
PopplerDocument *document;
|
|
|
|
int page;
|
|
|
|
int fullscreen;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-01-12 19:48:51 +04:00
|
|
|
redraw_handler(struct widget *widget, void *data)
|
2009-09-21 21:46:45 +04:00
|
|
|
{
|
2012-01-12 19:48:51 +04:00
|
|
|
struct view *view = data;
|
|
|
|
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_surface_t *surface;
|
2009-09-21 21:46:45 +04:00
|
|
|
cairo_t *cr;
|
|
|
|
PopplerPage *page;
|
|
|
|
double width, height, doc_aspect, window_aspect, scale;
|
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(view->widget, &allocation);
|
2009-09-21 21:46:45 +04:00
|
|
|
|
2010-06-15 07:22:15 +04:00
|
|
|
surface = window_get_surface(view->window);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
2011-01-11 18:00:52 +03:00
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y,
|
|
|
|
allocation.width, allocation.height);
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_clip(cr);
|
2009-09-21 21:46:45 +04:00
|
|
|
|
2011-01-26 00:21:59 +03:00
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
|
2009-09-21 21:46:45 +04:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint(cr);
|
2011-01-26 00:21:59 +03:00
|
|
|
|
|
|
|
if(!view->document) {
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
window_flush(view->window);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
page = poppler_document_get_page(view->document, view->page);
|
2009-09-21 21:46:45 +04:00
|
|
|
poppler_page_get_size(page, &width, &height);
|
|
|
|
doc_aspect = width / height;
|
2011-01-11 18:00:52 +03:00
|
|
|
window_aspect = (double) allocation.width / allocation.height;
|
2009-09-21 21:46:45 +04:00
|
|
|
if (doc_aspect < window_aspect)
|
2011-01-11 18:00:52 +03:00
|
|
|
scale = allocation.height / height;
|
2009-09-21 21:46:45 +04:00
|
|
|
else
|
2011-01-11 18:00:52 +03:00
|
|
|
scale = allocation.width / width;
|
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
2009-09-21 21:46:45 +04:00
|
|
|
cairo_scale(cr, scale, scale);
|
|
|
|
cairo_translate(cr,
|
2011-01-11 18:00:52 +03:00
|
|
|
(allocation.width - width * scale) / 2 / scale,
|
|
|
|
(allocation.height - height * scale) / 2 / scale);
|
2009-09-21 21:46:45 +04:00
|
|
|
cairo_rectangle(cr, 0, 0, width, height);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
|
|
cairo_fill(cr);
|
|
|
|
poppler_page_render(page, cr);
|
|
|
|
cairo_destroy(cr);
|
2011-01-26 00:22:00 +03:00
|
|
|
cairo_surface_destroy(surface);
|
2009-09-21 21:46:45 +04:00
|
|
|
g_object_unref(G_OBJECT(page));
|
|
|
|
}
|
|
|
|
|
2011-05-13 21:51:13 +04:00
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
resize_handler(struct widget *widget,
|
2011-05-13 21:51:13 +04:00
|
|
|
int32_t width, int32_t height, void *data)
|
|
|
|
{
|
|
|
|
struct view *view = data;
|
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_set_size(view->widget, width, height);
|
2011-05-13 21:51:13 +04:00
|
|
|
}
|
|
|
|
|
2011-01-26 00:21:57 +03:00
|
|
|
static void
|
|
|
|
view_page_up(struct view *view)
|
|
|
|
{
|
|
|
|
if(view->page <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
view->page--;
|
|
|
|
window_schedule_redraw(view->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
view_page_down(struct view *view)
|
|
|
|
{
|
2011-01-26 00:21:59 +03:00
|
|
|
if(!view->document ||
|
|
|
|
view->page >= poppler_document_get_n_pages(view->document) - 1) {
|
2011-01-26 00:21:57 +03:00
|
|
|
return;
|
2011-01-26 00:21:59 +03:00
|
|
|
}
|
2011-01-26 00:21:57 +03:00
|
|
|
|
|
|
|
view->page++;
|
|
|
|
window_schedule_redraw(view->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-12 19:48:51 +04:00
|
|
|
button_handler(struct widget *widget, struct input *input, uint32_t time,
|
2012-05-04 14:21:55 +04:00
|
|
|
uint32_t button, uint32_t state, void *data)
|
2011-01-26 00:21:57 +03:00
|
|
|
{
|
2012-01-12 19:48:51 +04:00
|
|
|
struct view *view = data;
|
2011-01-26 00:21:57 +03:00
|
|
|
|
|
|
|
if(!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(button) {
|
|
|
|
case 275:
|
|
|
|
view_page_up(view);
|
|
|
|
break;
|
|
|
|
case 276:
|
|
|
|
view_page_down(view);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 21:46:45 +04:00
|
|
|
static void
|
2011-01-19 22:20:33 +03:00
|
|
|
key_handler(struct window *window, struct input *input, uint32_t time,
|
|
|
|
uint32_t key, uint32_t unicode, uint32_t state, void *data)
|
2009-09-21 21:46:45 +04:00
|
|
|
{
|
|
|
|
struct view *view = data;
|
|
|
|
|
2011-01-26 00:21:57 +03:00
|
|
|
if(!state)
|
|
|
|
return;
|
|
|
|
|
2009-09-21 21:46:45 +04:00
|
|
|
switch (key) {
|
|
|
|
case KEY_F11:
|
|
|
|
view->fullscreen ^= 1;
|
|
|
|
window_set_fullscreen(window, view->fullscreen);
|
|
|
|
break;
|
|
|
|
case KEY_SPACE:
|
|
|
|
case KEY_PAGEDOWN:
|
2011-01-26 00:21:57 +03:00
|
|
|
case KEY_RIGHT:
|
|
|
|
case KEY_DOWN:
|
|
|
|
view_page_down(view);
|
2009-09-21 21:46:45 +04:00
|
|
|
break;
|
|
|
|
case KEY_BACKSPACE:
|
|
|
|
case KEY_PAGEUP:
|
2011-01-26 00:21:57 +03:00
|
|
|
case KEY_LEFT:
|
|
|
|
case KEY_UP:
|
|
|
|
view_page_up(view);
|
2009-09-21 21:46:45 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
2010-07-29 07:50:12 +04:00
|
|
|
struct input *device, void *data)
|
2009-09-21 21:46:45 +04:00
|
|
|
{
|
|
|
|
struct view *view = data;
|
2010-06-15 07:52:50 +04:00
|
|
|
window_schedule_redraw(view->window);
|
2009-09-21 21:46:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct view *
|
2011-05-13 21:54:19 +04:00
|
|
|
view_create(struct display *display,
|
|
|
|
uint32_t key, const char *filename, int fullscreen)
|
2009-09-21 21:46:45 +04:00
|
|
|
{
|
|
|
|
struct view *view;
|
|
|
|
gchar *basename;
|
|
|
|
gchar *title;
|
2011-01-26 00:21:58 +03:00
|
|
|
GFile *file = NULL;
|
2009-09-21 21:46:45 +04:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
view = malloc(sizeof *view);
|
|
|
|
if (view == NULL)
|
|
|
|
return view;
|
|
|
|
memset(view, 0, sizeof *view);
|
|
|
|
|
2011-01-26 00:21:58 +03:00
|
|
|
file = g_file_new_for_commandline_arg(filename);
|
|
|
|
basename = g_file_get_basename(file);
|
|
|
|
if(!basename) {
|
|
|
|
title = "Wayland View";
|
|
|
|
} else {
|
|
|
|
title = g_strdup_printf("Wayland View - %s", basename);
|
|
|
|
g_free(basename);
|
|
|
|
}
|
|
|
|
|
|
|
|
view->document = poppler_document_new_from_file(g_file_get_uri(file),
|
|
|
|
NULL, &error);
|
|
|
|
|
|
|
|
if(error) {
|
|
|
|
title = "File not found";
|
|
|
|
}
|
2009-09-21 21:46:45 +04:00
|
|
|
|
2012-02-01 00:24:48 +04:00
|
|
|
view->window = window_create(display);
|
2012-01-11 07:41:05 +04:00
|
|
|
view->widget = frame_create(view->window, view);
|
2011-01-22 02:03:15 +03:00
|
|
|
window_set_title(view->window, title);
|
2009-09-21 21:46:45 +04:00
|
|
|
view->display = display;
|
|
|
|
|
2010-06-25 19:19:22 +04:00
|
|
|
window_set_user_data(view->window, view);
|
|
|
|
window_set_key_handler(view->window, key_handler);
|
2009-09-21 21:46:45 +04:00
|
|
|
window_set_keyboard_focus_handler(view->window,
|
2010-06-25 19:19:22 +04:00
|
|
|
keyboard_focus_handler);
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_button_handler(view->widget, button_handler);
|
|
|
|
widget_set_resize_handler(view->widget, resize_handler);
|
|
|
|
widget_set_redraw_handler(view->widget, redraw_handler);
|
|
|
|
|
2009-09-21 21:46:45 +04:00
|
|
|
view->page = 0;
|
2011-05-13 21:54:19 +04:00
|
|
|
|
|
|
|
view->fullscreen = fullscreen;
|
|
|
|
window_set_fullscreen(view->window, view->fullscreen);
|
|
|
|
|
2012-01-12 19:48:51 +04:00
|
|
|
window_schedule_resize(view->window, 500, 400);
|
2009-09-21 21:46:45 +04:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2011-05-13 21:54:19 +04:00
|
|
|
static int option_fullscreen;
|
|
|
|
|
2012-03-12 05:05:57 +04:00
|
|
|
static const struct weston_option view_options[] = {
|
|
|
|
{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &option_fullscreen },
|
2009-09-21 21:46:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct display *d;
|
|
|
|
int i;
|
|
|
|
|
2012-03-12 09:05:25 +04:00
|
|
|
g_type_init();
|
|
|
|
|
2012-03-12 05:05:57 +04:00
|
|
|
argc = parse_options(view_options,
|
|
|
|
ARRAY_LENGTH(view_options), argc, argv);
|
|
|
|
|
|
|
|
d = display_create(argc, argv);
|
2010-11-22 22:34:19 +03:00
|
|
|
if (d == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-21 21:46:45 +04:00
|
|
|
|
2011-01-25 23:16:01 +03:00
|
|
|
for (i = 1; i < argc; i++)
|
2011-05-13 21:54:19 +04:00
|
|
|
view_create (d, i, argv[i], option_fullscreen);
|
2009-09-21 21:46:45 +04:00
|
|
|
|
2010-06-08 22:59:44 +04:00
|
|
|
display_run(d);
|
2009-09-21 21:46:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|