2009-02-21 23:22:06 +03: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>
|
2012-03-12 09:05:25 +04:00
|
|
|
#include <libgen.h>
|
2009-02-21 23:22:06 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
|
2011-11-22 16:18:50 +04:00
|
|
|
#include <wayland-client.h>
|
2009-02-21 23:22:06 +03:00
|
|
|
|
|
|
|
#include "window.h"
|
2012-05-16 06:33:43 +04:00
|
|
|
#include "../shared/cairo-util.h"
|
2009-02-21 23:22:06 +03:00
|
|
|
|
|
|
|
struct image {
|
|
|
|
struct window *window;
|
2012-01-10 21:23:19 +04:00
|
|
|
struct widget *widget;
|
2009-02-21 23:22:06 +03:00
|
|
|
struct display *display;
|
2012-03-12 09:05:25 +04:00
|
|
|
char *filename;
|
|
|
|
cairo_surface_t *image;
|
2012-07-24 06:00:21 +04:00
|
|
|
int fullscreen;
|
2012-08-13 23:16:47 +04:00
|
|
|
int *image_counter;
|
2009-02-21 23:22:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-01-10 21:23:19 +04:00
|
|
|
redraw_handler(struct widget *widget, void *data)
|
2009-02-21 23:22:06 +03:00
|
|
|
{
|
2012-01-10 21:23:19 +04:00
|
|
|
struct image *image = data;
|
2011-01-11 18:00:52 +03:00
|
|
|
struct rectangle allocation;
|
2009-02-21 23:22:06 +03:00
|
|
|
cairo_t *cr;
|
2011-01-21 19:35:05 +03:00
|
|
|
cairo_surface_t *surface;
|
2012-03-12 09:05:25 +04:00
|
|
|
double width, height, doc_aspect, window_aspect, scale;
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(image->widget, &allocation);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2011-01-21 19:35:05 +03:00
|
|
|
surface = window_get_surface(image->window);
|
2010-06-15 07:22:15 +04:00
|
|
|
cr = cairo_create(surface);
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_get_allocation(image->widget, &allocation);
|
2011-01-21 19:35:05 +03:00
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y,
|
|
|
|
allocation.width, allocation.height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
cairo_push_group(cr);
|
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
|
|
|
|
2009-02-21 23:22:06 +03:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 1);
|
|
|
|
cairo_paint(cr);
|
2012-03-12 09:05:25 +04:00
|
|
|
|
|
|
|
width = cairo_image_surface_get_width(image->image);
|
|
|
|
height = cairo_image_surface_get_height(image->image);
|
|
|
|
doc_aspect = width / height;
|
|
|
|
window_aspect = (double) allocation.width / allocation.height;
|
|
|
|
if (doc_aspect < window_aspect)
|
|
|
|
scale = allocation.height / height;
|
|
|
|
else
|
|
|
|
scale = allocation.width / width;
|
|
|
|
cairo_scale(cr, scale, scale);
|
|
|
|
cairo_translate(cr,
|
|
|
|
(allocation.width - width * scale) / 2 / scale,
|
|
|
|
(allocation.height - height * scale) / 2 / scale);
|
|
|
|
|
|
|
|
cairo_set_source_surface(cr, image->image, 0, 0);
|
2009-02-21 23:22:06 +03:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
2011-01-21 19:35:05 +03:00
|
|
|
cairo_pop_group_to_source(cr);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
|
2010-06-15 07:22:15 +04:00
|
|
|
cairo_surface_destroy(surface);
|
2009-02-21 23:22:06 +03:00
|
|
|
}
|
|
|
|
|
2010-01-25 02:10:15 +03:00
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
2010-07-29 07:50:12 +04:00
|
|
|
struct input *device, void *data)
|
2010-01-25 02:10:15 +03:00
|
|
|
{
|
|
|
|
struct image *image = data;
|
|
|
|
|
2010-06-15 07:52:50 +04:00
|
|
|
window_schedule_redraw(image->window);
|
2010-01-25 02:10:15 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 06:00:21 +04:00
|
|
|
static void
|
|
|
|
fullscreen_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
struct image *image = data;
|
|
|
|
|
|
|
|
image->fullscreen ^= 1;
|
|
|
|
window_set_fullscreen(window, image->fullscreen);
|
|
|
|
}
|
|
|
|
|
2012-08-13 23:16:47 +04:00
|
|
|
static void
|
|
|
|
close_handler(struct window *window, void *data)
|
|
|
|
{
|
|
|
|
struct image *image = data;
|
|
|
|
|
|
|
|
*image->image_counter -= 1;
|
|
|
|
|
|
|
|
if (*image->image_counter == 0)
|
|
|
|
display_exit(image->display);
|
|
|
|
|
|
|
|
widget_destroy(image->widget);
|
|
|
|
window_destroy(image->window);
|
|
|
|
|
|
|
|
free(image);
|
|
|
|
}
|
|
|
|
|
2009-02-21 23:22:06 +03:00
|
|
|
static struct image *
|
2012-08-13 23:16:47 +04:00
|
|
|
image_create(struct display *display, const char *filename,
|
|
|
|
int *image_counter)
|
2009-02-21 23:22:06 +03:00
|
|
|
{
|
|
|
|
struct image *image;
|
2012-03-12 09:05:25 +04:00
|
|
|
char *b, *copy, title[512];;
|
2009-02-21 23:22:06 +03:00
|
|
|
|
|
|
|
image = malloc(sizeof *image);
|
|
|
|
if (image == NULL)
|
|
|
|
return image;
|
|
|
|
memset(image, 0, sizeof *image);
|
|
|
|
|
2012-03-12 09:05:25 +04:00
|
|
|
copy = strdup(filename);
|
|
|
|
b = basename(copy);
|
|
|
|
snprintf(title, sizeof title, "Wayland Image - %s", b);
|
|
|
|
free(copy);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2012-03-12 09:05:25 +04:00
|
|
|
image->filename = strdup(filename);
|
|
|
|
image->image = load_cairo_surface(filename);
|
2012-08-07 06:45:43 +04:00
|
|
|
|
|
|
|
if (!image->image) {
|
|
|
|
fprintf(stderr, "could not find the image %s!\n", b);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-01 00:24:48 +04:00
|
|
|
image->window = window_create(display);
|
2012-01-11 07:41:05 +04:00
|
|
|
image->widget = frame_create(image->window, image);
|
2011-01-22 02:03:15 +03:00
|
|
|
window_set_title(image->window, title);
|
2009-02-21 23:22:06 +03:00
|
|
|
image->display = display;
|
2012-08-13 23:16:47 +04:00
|
|
|
image->image_counter = image_counter;
|
|
|
|
*image_counter += 1;
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2010-06-25 19:19:22 +04:00
|
|
|
window_set_user_data(image->window, image);
|
2012-01-10 21:23:19 +04:00
|
|
|
widget_set_redraw_handler(image->widget, redraw_handler);
|
2010-07-29 07:50:12 +04:00
|
|
|
window_set_keyboard_focus_handler(image->window,
|
|
|
|
keyboard_focus_handler);
|
2012-07-24 06:00:21 +04:00
|
|
|
window_set_fullscreen_handler(image->window, fullscreen_handler);
|
2012-08-13 23:16:47 +04:00
|
|
|
window_set_close_handler(image->window, close_handler);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2012-01-11 04:11:42 +04:00
|
|
|
widget_schedule_resize(image->widget, 500, 400);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct display *d;
|
|
|
|
int i;
|
2012-08-13 23:16:47 +04:00
|
|
|
int image_counter = 0;
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2012-03-12 05:05:57 +04:00
|
|
|
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-02-21 23:22:06 +03:00
|
|
|
|
2011-01-25 23:16:01 +03:00
|
|
|
for (i = 1; i < argc; i++)
|
2012-08-13 23:16:47 +04:00
|
|
|
image_create(d, argv[i], &image_counter);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
2012-08-13 23:16:47 +04:00
|
|
|
if (image_counter > 0)
|
|
|
|
display_run(d);
|
2009-02-21 23:22:06 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|