2
* Copyright (c) 2013 Martin Decky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup viewer
45
#include <codec/tga.h>
49
#define WINDOW_WIDTH 1024
50
#define WINDOW_HEIGHT 768
52
static size_t imgs_count;
53
static size_t imgs_current = 0;
56
static window_t *main_window;
57
static surface_t *surface = NULL;
58
static canvas_t *canvas = NULL;
60
static bool img_load(const char *);
62
static void on_keyboard_event(widget_t *widget, void *data)
64
kbd_event_t *event = (kbd_event_t *) data;
67
if ((event->type == KEY_PRESS) && (event->c == 'q'))
70
if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
71
if (imgs_current == imgs_count - 1)
79
if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
80
if (imgs_current == 0)
81
imgs_current = imgs_count - 1;
89
if (!img_load(imgs[imgs_current])) {
90
printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
96
static bool img_load(const char *fname)
98
int fd = open(fname, O_RDONLY);
103
int rc = fstat(fd, &stat);
109
void *tga = malloc(stat.size);
115
ssize_t rd = read_all(fd, tga, stat.size);
116
if ((rd < 0) || (rd != (ssize_t) stat.size)) {
124
surface_t *local_surface = decode_tga(tga, stat.size, 0);
125
if (local_surface == NULL) {
132
if (canvas != NULL) {
133
if (!update_canvas(canvas, local_surface)) {
134
surface_destroy(local_surface);
138
canvas = create_canvas(window_root(main_window),
139
WINDOW_WIDTH, WINDOW_HEIGHT, local_surface);
140
if (canvas == NULL) {
141
surface_destroy(local_surface);
145
sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
149
surface_destroy(surface);
151
surface = local_surface;
156
int main(int argc, char *argv[])
159
printf("Compositor server not specified.\n");
164
printf("No image files specified.\n");
168
main_window = window_open(argv[1], true, false, "viewer", 0, 0);
170
printf("Cannot open main window.\n");
174
imgs_count = argc - 2;
175
imgs = calloc(imgs_count, sizeof(char *));
177
printf("Out of memory.\n");
181
for (int i = 0; i < argc - 2; i++) {
182
imgs[i] = str_dup(argv[i + 2]);
183
if (imgs[i] == NULL) {
184
printf("Out of memory.\n");
189
if (!img_load(imgs[imgs_current])) {
190
printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
194
window_resize(main_window, WINDOW_WIDTH, WINDOW_HEIGHT);
195
window_exec(main_window);