toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / clients / image.c
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>
29 #include <libgen.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
33 #include <cairo.h>
34
35 #include <wayland-client.h>
36
37 #include "window.h"
38 #include "../shared/cairo-util.h"
39
40 struct image {
41         struct window *window;
42         struct widget *widget;
43         struct display *display;
44         char *filename;
45         cairo_surface_t *image;
46         int fullscreen;
47         int *image_counter;
48 };
49
50 static void
51 redraw_handler(struct widget *widget, void *data)
52 {
53         struct image *image = data;
54         struct rectangle allocation;
55         cairo_t *cr;
56         cairo_surface_t *surface;
57         double width, height, doc_aspect, window_aspect, scale;
58
59         widget_get_allocation(image->widget, &allocation);
60
61         surface = window_get_surface(image->window);
62         cr = cairo_create(surface);
63         widget_get_allocation(image->widget, &allocation);
64         cairo_rectangle(cr, allocation.x, allocation.y,
65                         allocation.width, allocation.height);
66         cairo_clip(cr);
67         cairo_push_group(cr);
68         cairo_translate(cr, allocation.x, allocation.y);
69
70         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
71         cairo_set_source_rgba(cr, 0, 0, 0, 1);
72         cairo_paint(cr);
73
74         width = cairo_image_surface_get_width(image->image);
75         height = cairo_image_surface_get_height(image->image);
76         doc_aspect = width / height;
77         window_aspect = (double) allocation.width / allocation.height;
78         if (doc_aspect < window_aspect)
79                 scale = allocation.height / height;
80         else
81                 scale = allocation.width / width;
82         cairo_scale(cr, scale, scale);
83         cairo_translate(cr,
84                         (allocation.width - width * scale) / 2 / scale,
85                         (allocation.height - height * scale) / 2 / scale);
86
87         cairo_set_source_surface(cr, image->image, 0, 0);
88         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
89         cairo_paint(cr);
90
91         cairo_pop_group_to_source(cr);
92         cairo_paint(cr);
93         cairo_destroy(cr);
94
95         cairo_surface_destroy(surface);
96 }
97
98 static void
99 keyboard_focus_handler(struct window *window,
100                        struct input *device, void *data)
101 {
102         struct image *image = data;
103
104         window_schedule_redraw(image->window);
105 }
106
107 static void
108 fullscreen_handler(struct window *window, void *data)
109 {
110         struct image *image = data;
111
112         image->fullscreen ^= 1;
113         window_set_fullscreen(window, image->fullscreen);
114 }
115
116 static void
117 close_handler(struct window *window, void *data)
118 {
119         struct image *image = data;
120
121         *image->image_counter -= 1;
122
123         if (*image->image_counter == 0)
124                 display_exit(image->display);
125
126         widget_destroy(image->widget);
127         window_destroy(image->window);
128
129         free(image);
130 }
131
132 static struct image *
133 image_create(struct display *display, const char *filename,
134              int *image_counter)
135 {
136         struct image *image;
137         char *b, *copy, title[512];;
138
139         image = malloc(sizeof *image);
140         if (image == NULL)
141                 return image;
142         memset(image, 0, sizeof *image);
143
144         copy = strdup(filename);
145         b = basename(copy);
146         snprintf(title, sizeof title, "Wayland Image - %s", b);
147         free(copy);
148
149         image->filename = strdup(filename);
150         image->image = load_cairo_surface(filename);
151
152         if (!image->image) {
153                 fprintf(stderr, "could not find the image %s!\n", b);
154                 return NULL;
155         }
156
157         image->window = window_create(display);
158         image->widget = frame_create(image->window, image);
159         window_set_title(image->window, title);
160         image->display = display;
161         image->image_counter = image_counter;
162         *image_counter += 1;
163
164         window_set_user_data(image->window, image);
165         widget_set_redraw_handler(image->widget, redraw_handler);
166         window_set_keyboard_focus_handler(image->window,
167                                           keyboard_focus_handler);
168         window_set_fullscreen_handler(image->window, fullscreen_handler);
169         window_set_close_handler(image->window, close_handler);
170
171         widget_schedule_resize(image->widget, 500, 400);
172
173         return image;
174 }
175
176 int
177 main(int argc, char *argv[])
178 {
179         struct display *d;
180         int i;
181         int image_counter = 0;
182
183         d = display_create(argc, argv);
184         if (d == NULL) {
185                 fprintf(stderr, "failed to create display: %m\n");
186                 return -1;
187         }
188
189         for (i = 1; i < argc; i++)
190                 image_create(d, argv[i], &image_counter);
191
192         if (image_counter > 0)
193                 display_run(d);
194
195         return 0;
196 }