blob: 711f0d8c8f7b1923d7d48703b6173ca9b511083c [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øgsberg5a315bc2012-05-15 22:33:43 -040038#include "../shared/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;
Kristian Høgsbergdf0faf72012-07-23 22:00:21 -040046 int fullscreen;
Chris Wilson0de19eb2009-02-21 15:22:06 -050047};
48
49static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050050redraw_handler(struct widget *widget, void *data)
Chris Wilson0de19eb2009-02-21 15:22:06 -050051{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050052 struct image *image = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -050053 struct rectangle allocation;
Chris Wilson0de19eb2009-02-21 15:22:06 -050054 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050055 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040056 double width, height, doc_aspect, window_aspect, scale;
Chris Wilson0de19eb2009-02-21 15:22:06 -050057
Kristian Høgsbergbb977002012-01-10 19:11:42 -050058 widget_get_allocation(image->widget, &allocation);
Chris Wilson0de19eb2009-02-21 15:22:06 -050059
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050060 surface = window_get_surface(image->window);
Kristian Høgsberg09531622010-06-14 23:22:15 -040061 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -050062 widget_get_allocation(image->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050063 cairo_rectangle(cr, allocation.x, allocation.y,
64 allocation.width, allocation.height);
65 cairo_clip(cr);
66 cairo_push_group(cr);
67 cairo_translate(cr, allocation.x, allocation.y);
68
Chris Wilson0de19eb2009-02-21 15:22:06 -050069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
70 cairo_set_source_rgba(cr, 0, 0, 0, 1);
71 cairo_paint(cr);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040072
73 width = cairo_image_surface_get_width(image->image);
74 height = cairo_image_surface_get_height(image->image);
75 doc_aspect = width / height;
76 window_aspect = (double) allocation.width / allocation.height;
77 if (doc_aspect < window_aspect)
78 scale = allocation.height / height;
79 else
80 scale = allocation.width / width;
81 cairo_scale(cr, scale, scale);
82 cairo_translate(cr,
83 (allocation.width - width * scale) / 2 / scale,
84 (allocation.height - height * scale) / 2 / scale);
85
86 cairo_set_source_surface(cr, image->image, 0, 0);
Chris Wilson0de19eb2009-02-21 15:22:06 -050087 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
88 cairo_paint(cr);
Chris Wilson0de19eb2009-02-21 15:22:06 -050089
Kristian Høgsberge164e4e2011-01-21 11:35:05 -050090 cairo_pop_group_to_source(cr);
91 cairo_paint(cr);
92 cairo_destroy(cr);
93
Kristian Høgsberg09531622010-06-14 23:22:15 -040094 cairo_surface_destroy(surface);
Chris Wilson0de19eb2009-02-21 15:22:06 -050095}
96
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097static void
Kristian Høgsberga341fa02010-01-24 18:10:15 -050098keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -040099 struct input *device, void *data)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500100{
101 struct image *image = data;
102
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400103 window_schedule_redraw(image->window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500104}
105
Kristian Høgsbergdf0faf72012-07-23 22:00:21 -0400106static void
107fullscreen_handler(struct window *window, void *data)
108{
109 struct image *image = data;
110
111 image->fullscreen ^= 1;
112 window_set_fullscreen(window, image->fullscreen);
113}
114
Chris Wilson0de19eb2009-02-21 15:22:06 -0500115static struct image *
Kristian Høgsbergcadd0f52012-01-09 18:56:37 -0500116image_create(struct display *display, const char *filename)
Chris Wilson0de19eb2009-02-21 15:22:06 -0500117{
118 struct image *image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400119 char *b, *copy, title[512];;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500120
121 image = malloc(sizeof *image);
122 if (image == NULL)
123 return image;
124 memset(image, 0, sizeof *image);
125
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400126 copy = strdup(filename);
127 b = basename(copy);
128 snprintf(title, sizeof title, "Wayland Image - %s", b);
129 free(copy);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500130
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400131 image->filename = strdup(filename);
132 image->image = load_cairo_surface(filename);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500133 image->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500134 image->widget = frame_create(image->window, image);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 window_set_title(image->window, title);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500136 image->display = display;
137
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400138 window_set_user_data(image->window, image);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500139 widget_set_redraw_handler(image->widget, redraw_handler);
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400140 window_set_keyboard_focus_handler(image->window,
141 keyboard_focus_handler);
Kristian Høgsbergdf0faf72012-07-23 22:00:21 -0400142 window_set_fullscreen_handler(image->window, fullscreen_handler);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500143
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500144 widget_schedule_resize(image->widget, 500, 400);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500145
146 return image;
147}
148
Chris Wilson0de19eb2009-02-21 15:22:06 -0500149int
150main(int argc, char *argv[])
151{
Chris Wilson0de19eb2009-02-21 15:22:06 -0500152 struct display *d;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500153 int i;
154
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400155 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200156 if (d == NULL) {
157 fprintf(stderr, "failed to create display: %m\n");
158 return -1;
159 }
Chris Wilson0de19eb2009-02-21 15:22:06 -0500160
Kristian Høgsberg00439612011-01-25 15:16:01 -0500161 for (i = 1; i < argc; i++)
Kristian Høgsbergcadd0f52012-01-09 18:56:37 -0500162 image_create (d, argv[i]);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500163
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400164 display_run(d);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500165
166 return 0;
167}