Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 1 | /* |
| 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øgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 29 | #include <libgen.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | #include <math.h> |
| 32 | #include <time.h> |
| 33 | #include <cairo.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 34 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 35 | #include <wayland-client.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 36 | |
| 37 | #include "window.h" |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 38 | #include "cairo-util.h" |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 39 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 40 | struct image { |
| 41 | struct window *window; |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 42 | struct widget *widget; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 43 | struct display *display; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 44 | char *filename; |
| 45 | cairo_surface_t *image; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 49 | redraw_handler(struct widget *widget, void *data) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 50 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 51 | struct image *image = data; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 52 | struct rectangle allocation; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 53 | cairo_t *cr; |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 54 | cairo_surface_t *surface; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 55 | double width, height, doc_aspect, window_aspect, scale; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 56 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 57 | widget_get_allocation(image->widget, &allocation); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 58 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 59 | surface = window_get_surface(image->window); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 60 | cr = cairo_create(surface); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 61 | widget_get_allocation(image->widget, &allocation); |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 62 | 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 Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 68 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 69 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 70 | cairo_paint(cr); |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 71 | |
| 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 Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 86 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 87 | cairo_paint(cr); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 88 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 89 | cairo_pop_group_to_source(cr); |
| 90 | cairo_paint(cr); |
| 91 | cairo_destroy(cr); |
| 92 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 93 | cairo_surface_destroy(surface); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 96 | static void |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 97 | keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 98 | struct input *device, void *data) |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 99 | { |
| 100 | struct image *image = data; |
| 101 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 102 | window_schedule_redraw(image->window); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 105 | static struct image * |
Kristian Høgsberg | cadd0f5 | 2012-01-09 18:56:37 -0500 | [diff] [blame] | 106 | image_create(struct display *display, const char *filename) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 107 | { |
| 108 | struct image *image; |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 109 | char *b, *copy, title[512];; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 110 | |
| 111 | image = malloc(sizeof *image); |
| 112 | if (image == NULL) |
| 113 | return image; |
| 114 | memset(image, 0, sizeof *image); |
| 115 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 116 | copy = strdup(filename); |
| 117 | b = basename(copy); |
| 118 | snprintf(title, sizeof title, "Wayland Image - %s", b); |
| 119 | free(copy); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 120 | |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 121 | image->filename = strdup(filename); |
| 122 | image->image = load_cairo_surface(filename); |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 123 | image->window = window_create(display); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 124 | image->widget = frame_create(image->window, image); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 125 | window_set_title(image->window, title); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 126 | image->display = display; |
| 127 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 128 | window_set_user_data(image->window, image); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 129 | widget_set_redraw_handler(image->widget, redraw_handler); |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 130 | window_set_keyboard_focus_handler(image->window, |
| 131 | keyboard_focus_handler); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 132 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 133 | widget_schedule_resize(image->widget, 500, 400); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 134 | |
| 135 | return image; |
| 136 | } |
| 137 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 138 | int |
| 139 | main(int argc, char *argv[]) |
| 140 | { |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 141 | struct display *d; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 142 | int i; |
| 143 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 144 | d = display_create(argc, argv); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 145 | if (d == NULL) { |
| 146 | fprintf(stderr, "failed to create display: %m\n"); |
| 147 | return -1; |
| 148 | } |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 149 | |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 150 | for (i = 1; i < argc; i++) |
Kristian Høgsberg | cadd0f5 | 2012-01-09 18:56:37 -0500 | [diff] [blame] | 151 | image_create (d, argv[i]); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 152 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 153 | display_run(d); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |