blob: 36ab485c69a2ff402517cbdbd406b7829fafc666 [file] [log] [blame]
Chris Wilson0de19eb2009-02-21 15:22:06 -05001/*
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>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040029#include <libgen.h>
Chris Wilson0de19eb2009-02-21 15:22:06 -050030#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Chris Wilson0de19eb2009-02-21 15:22:06 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-client.h>
Chris Wilson0de19eb2009-02-21 15:22:06 -050036
37#include "window.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040038#include "cairo-util.h"
Chris Wilson0de19eb2009-02-21 15:22:06 -050039
Chris Wilson0de19eb2009-02-21 15:22:06 -050040struct image {
41 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050042 struct widget *widget;
Chris Wilson0de19eb2009-02-21 15:22:06 -050043 struct display *display;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040044 char *filename;
45 cairo_surface_t *image;
Chris Wilson0de19eb2009-02-21 15:22:06 -050046};
47
48static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050049redraw_handler(struct widget *widget, void *data)
Chris Wilson0de19eb2009-02-21 15:22:06 -050050{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050051 struct image *image = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050052 struct rectangle allocation;
Chris Wilson0de19eb2009-02-21 15:22:06 -050053 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050054 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040055 double width, height, doc_aspect, window_aspect, scale;
Chris Wilson0de19eb2009-02-21 15:22:06 -050056
Kristian Høgsbergbb977002012-01-10 19:11:42 -050057 widget_get_allocation(image->widget, &allocation);
Chris Wilson0de19eb2009-02-21 15:22:06 -050058
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050059 surface = window_get_surface(image->window);
Kristian Høgsberg09531622010-06-14 23:22:15 -040060 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -050061 widget_get_allocation(image->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050062 cairo_rectangle(cr, allocation.x, allocation.y,
63 allocation.width, allocation.height);
64 cairo_clip(cr);
65 cairo_push_group(cr);
66 cairo_translate(cr, allocation.x, allocation.y);
67
Chris Wilson0de19eb2009-02-21 15:22:06 -050068 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
69 cairo_set_source_rgba(cr, 0, 0, 0, 1);
70 cairo_paint(cr);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040071
72 width = cairo_image_surface_get_width(image->image);
73 height = cairo_image_surface_get_height(image->image);
74 doc_aspect = width / height;
75 window_aspect = (double) allocation.width / allocation.height;
76 if (doc_aspect < window_aspect)
77 scale = allocation.height / height;
78 else
79 scale = allocation.width / width;
80 cairo_scale(cr, scale, scale);
81 cairo_translate(cr,
82 (allocation.width - width * scale) / 2 / scale,
83 (allocation.height - height * scale) / 2 / scale);
84
85 cairo_set_source_surface(cr, image->image, 0, 0);
Chris Wilson0de19eb2009-02-21 15:22:06 -050086 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
87 cairo_paint(cr);
Chris Wilson0de19eb2009-02-21 15:22:06 -050088
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050089 cairo_pop_group_to_source(cr);
90 cairo_paint(cr);
91 cairo_destroy(cr);
92
Kristian Høgsberg09531622010-06-14 23:22:15 -040093 cairo_surface_destroy(surface);
Chris Wilson0de19eb2009-02-21 15:22:06 -050094}
95
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040096static void
Kristian Høgsberga341fa02010-01-24 18:10:15 -050097keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -040098 struct input *device, void *data)
Kristian Høgsberga341fa02010-01-24 18:10:15 -050099{
100 struct image *image = data;
101
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400102 window_schedule_redraw(image->window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500103}
104
Chris Wilson0de19eb2009-02-21 15:22:06 -0500105static struct image *
Kristian Høgsbergcadd0f52012-01-09 18:56:37 -0500106image_create(struct display *display, const char *filename)
Chris Wilson0de19eb2009-02-21 15:22:06 -0500107{
108 struct image *image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400109 char *b, *copy, title[512];;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500110
111 image = malloc(sizeof *image);
112 if (image == NULL)
113 return image;
114 memset(image, 0, sizeof *image);
115
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400116 copy = strdup(filename);
117 b = basename(copy);
118 snprintf(title, sizeof title, "Wayland Image - %s", b);
119 free(copy);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500120
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400121 image->filename = strdup(filename);
122 image->image = load_cairo_surface(filename);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500123 image->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500124 image->widget = frame_create(image->window, image);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500125 window_set_title(image->window, title);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500126 image->display = display;
127
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400128 window_set_user_data(image->window, image);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500129 widget_set_redraw_handler(image->widget, redraw_handler);
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400130 window_set_keyboard_focus_handler(image->window,
131 keyboard_focus_handler);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500132
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500133 widget_schedule_resize(image->widget, 500, 400);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500134
135 return image;
136}
137
Chris Wilson0de19eb2009-02-21 15:22:06 -0500138int
139main(int argc, char *argv[])
140{
Chris Wilson0de19eb2009-02-21 15:22:06 -0500141 struct display *d;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500142 int i;
143
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400144 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200145 if (d == NULL) {
146 fprintf(stderr, "failed to create display: %m\n");
147 return -1;
148 }
Chris Wilson0de19eb2009-02-21 15:22:06 -0500149
Kristian Høgsberg00439612011-01-25 15:16:01 -0500150 for (i = 1; i < argc; i++)
Kristian Høgsbergcadd0f52012-01-09 18:56:37 -0500151 image_create (d, argv[i]);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500152
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400153 display_run(d);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500154
155 return 0;
156}