[3e896e1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup viewer
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdio.h>
|
---|
[c23275a] | 36 | #include <stdlib.h>
|
---|
[23a0368] | 37 | #include <vfs/vfs.h>
|
---|
[3e896e1] | 38 | #include <errno.h>
|
---|
[38d150e] | 39 | #include <stdlib.h>
|
---|
[3e896e1] | 40 | #include <stdbool.h>
|
---|
| 41 | #include <window.h>
|
---|
| 42 | #include <canvas.h>
|
---|
[2bb6d04] | 43 | #include <draw/surface.h>
|
---|
| 44 | #include <draw/codec.h>
|
---|
[1c635d6] | 45 | #include <task.h>
|
---|
[1d6dd2a] | 46 | #include <str.h>
|
---|
[3e896e1] | 47 |
|
---|
| 48 | #define NAME "viewer"
|
---|
| 49 |
|
---|
| 50 | #define WINDOW_WIDTH 1024
|
---|
| 51 | #define WINDOW_HEIGHT 768
|
---|
| 52 |
|
---|
[c064b58] | 53 | #define DECORATION_WIDTH 8
|
---|
| 54 | #define DECORATION_HEIGHT 28
|
---|
| 55 |
|
---|
[3e896e1] | 56 | static size_t imgs_count;
|
---|
| 57 | static size_t imgs_current = 0;
|
---|
| 58 | static char **imgs;
|
---|
| 59 |
|
---|
| 60 | static window_t *main_window;
|
---|
| 61 | static surface_t *surface = NULL;
|
---|
| 62 | static canvas_t *canvas = NULL;
|
---|
| 63 |
|
---|
[c064b58] | 64 | static surface_coord_t img_width;
|
---|
| 65 | static surface_coord_t img_height;
|
---|
| 66 |
|
---|
| 67 | static bool img_load(const char *, surface_t **);
|
---|
| 68 | static bool img_setup(surface_t *);
|
---|
[3e896e1] | 69 |
|
---|
| 70 | static void on_keyboard_event(widget_t *widget, void *data)
|
---|
| 71 | {
|
---|
| 72 | kbd_event_t *event = (kbd_event_t *) data;
|
---|
| 73 | bool update = false;
|
---|
[a35b458] | 74 |
|
---|
[3e896e1] | 75 | if ((event->type == KEY_PRESS) && (event->c == 'q'))
|
---|
| 76 | exit(0);
|
---|
[a35b458] | 77 |
|
---|
[3e896e1] | 78 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
|
---|
| 79 | if (imgs_current == imgs_count - 1)
|
---|
| 80 | imgs_current = 0;
|
---|
| 81 | else
|
---|
| 82 | imgs_current++;
|
---|
[a35b458] | 83 |
|
---|
[3e896e1] | 84 | update = true;
|
---|
| 85 | }
|
---|
[a35b458] | 86 |
|
---|
[3e896e1] | 87 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
|
---|
| 88 | if (imgs_current == 0)
|
---|
| 89 | imgs_current = imgs_count - 1;
|
---|
| 90 | else
|
---|
| 91 | imgs_current--;
|
---|
[a35b458] | 92 |
|
---|
[3e896e1] | 93 | update = true;
|
---|
| 94 | }
|
---|
[a35b458] | 95 |
|
---|
[3e896e1] | 96 | if (update) {
|
---|
[c064b58] | 97 | surface_t *lsface;
|
---|
| 98 |
|
---|
| 99 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
[3e896e1] | 100 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
[c064b58] | 101 | exit(4);
|
---|
| 102 | }
|
---|
| 103 | if (!img_setup(lsface)) {
|
---|
| 104 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
| 105 | exit(6);
|
---|
[3e896e1] | 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[c064b58] | 110 | static bool img_load(const char *fname, surface_t **p_local_surface)
|
---|
[3e896e1] | 111 | {
|
---|
[f77c1c9] | 112 | int fd;
|
---|
[b7fd2a0] | 113 | errno_t rc = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ, &fd);
|
---|
[f77c1c9] | 114 | if (rc != EOK)
|
---|
[3e896e1] | 115 | return false;
|
---|
[a35b458] | 116 |
|
---|
[39330200] | 117 | vfs_stat_t stat;
|
---|
[f77c1c9] | 118 | rc = vfs_stat(fd, &stat);
|
---|
[23a0368] | 119 | if (rc != EOK) {
|
---|
[9c4cf0d] | 120 | vfs_put(fd);
|
---|
[3e896e1] | 121 | return false;
|
---|
| 122 | }
|
---|
[a35b458] | 123 |
|
---|
[3e896e1] | 124 | void *tga = malloc(stat.size);
|
---|
| 125 | if (tga == NULL) {
|
---|
[9c4cf0d] | 126 | vfs_put(fd);
|
---|
[3e896e1] | 127 | return false;
|
---|
| 128 | }
|
---|
[58898d1d] | 129 |
|
---|
[8e3498b] | 130 | size_t nread;
|
---|
[1433ecda] | 131 | rc = vfs_read(fd, (aoff64_t []) { 0 }, tga, stat.size, &nread);
|
---|
[8e3498b] | 132 | if (rc != EOK || nread != stat.size) {
|
---|
[3e896e1] | 133 | free(tga);
|
---|
[9c4cf0d] | 134 | vfs_put(fd);
|
---|
[3e896e1] | 135 | return false;
|
---|
| 136 | }
|
---|
[a35b458] | 137 |
|
---|
[9c4cf0d] | 138 | vfs_put(fd);
|
---|
[a35b458] | 139 |
|
---|
[c064b58] | 140 | *p_local_surface = decode_tga(tga, stat.size, 0);
|
---|
| 141 | if (*p_local_surface == NULL) {
|
---|
[3e896e1] | 142 | free(tga);
|
---|
| 143 | return false;
|
---|
| 144 | }
|
---|
[a35b458] | 145 |
|
---|
[3e896e1] | 146 | free(tga);
|
---|
[c064b58] | 147 |
|
---|
| 148 | surface_get_resolution(*p_local_surface, &img_width, &img_height);
|
---|
[a35b458] | 149 |
|
---|
[c064b58] | 150 | return true;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static bool img_setup(surface_t *local_surface)
|
---|
| 154 | {
|
---|
[3e896e1] | 155 | if (canvas != NULL) {
|
---|
| 156 | if (!update_canvas(canvas, local_surface)) {
|
---|
| 157 | surface_destroy(local_surface);
|
---|
| 158 | return false;
|
---|
| 159 | }
|
---|
| 160 | } else {
|
---|
[10cb47e] | 161 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
[c064b58] | 162 | img_width, img_height, local_surface);
|
---|
[3e896e1] | 163 | if (canvas == NULL) {
|
---|
| 164 | surface_destroy(local_surface);
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
[a35b458] | 167 |
|
---|
[3e896e1] | 168 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
| 169 | }
|
---|
[a35b458] | 170 |
|
---|
[3e896e1] | 171 | if (surface != NULL)
|
---|
| 172 | surface_destroy(surface);
|
---|
[a35b458] | 173 |
|
---|
[3e896e1] | 174 | surface = local_surface;
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[fd11144] | 178 | static void print_syntax(void)
|
---|
| 179 | {
|
---|
| 180 | printf("Syntax: %s [-d <display>] <image-file>...\n", NAME);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[3e896e1] | 183 | int main(int argc, char *argv[])
|
---|
| 184 | {
|
---|
[fd11144] | 185 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
[c064b58] | 186 | window_flags_t flags;
|
---|
| 187 | surface_t *lsface;
|
---|
| 188 | bool fullscreen;
|
---|
| 189 | sysarg_t dwidth;
|
---|
| 190 | sysarg_t dheight;
|
---|
[fd11144] | 191 | int i;
|
---|
| 192 |
|
---|
| 193 | i = 1;
|
---|
| 194 | while (i < argc && argv[i][0] == '-') {
|
---|
| 195 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 196 | ++i;
|
---|
| 197 | if (i >= argc) {
|
---|
| 198 | printf("Argument missing.\n");
|
---|
| 199 | print_syntax();
|
---|
| 200 | return 1;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | display_svc = argv[i++];
|
---|
| 204 | } else {
|
---|
| 205 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 206 | print_syntax();
|
---|
| 207 | return 1;
|
---|
| 208 | }
|
---|
[3e896e1] | 209 | }
|
---|
[a35b458] | 210 |
|
---|
[fd11144] | 211 | if (i >= argc) {
|
---|
[3e896e1] | 212 | printf("No image files specified.\n");
|
---|
[fd11144] | 213 | print_syntax();
|
---|
[3e896e1] | 214 | return 1;
|
---|
| 215 | }
|
---|
[c064b58] | 216 |
|
---|
[fd11144] | 217 | imgs_count = argc - i;
|
---|
[3e896e1] | 218 | imgs = calloc(imgs_count, sizeof(char *));
|
---|
| 219 | if (imgs == NULL) {
|
---|
| 220 | printf("Out of memory.\n");
|
---|
[c064b58] | 221 | return 2;
|
---|
[3e896e1] | 222 | }
|
---|
[a35b458] | 223 |
|
---|
[fd11144] | 224 | for (int j = 0; j < argc - i; j++) {
|
---|
| 225 | imgs[j] = str_dup(argv[i + j]);
|
---|
| 226 | if (imgs[j] == NULL) {
|
---|
[3e896e1] | 227 | printf("Out of memory.\n");
|
---|
[c064b58] | 228 | return 3;
|
---|
[3e896e1] | 229 | }
|
---|
| 230 | }
|
---|
[c064b58] | 231 |
|
---|
| 232 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
[3e896e1] | 233 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
[c064b58] | 234 | return 4;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | fullscreen = ((img_width == WINDOW_WIDTH) &&
|
---|
| 238 | (img_height == WINDOW_HEIGHT));
|
---|
| 239 |
|
---|
| 240 | flags = WINDOW_MAIN;
|
---|
| 241 | if (!fullscreen)
|
---|
| 242 | flags |= WINDOW_DECORATED;
|
---|
| 243 |
|
---|
[fd11144] | 244 | main_window = window_open(display_svc, NULL, flags, "viewer");
|
---|
[c064b58] | 245 | if (!main_window) {
|
---|
| 246 | printf("Cannot open main window.\n");
|
---|
| 247 | return 5;
|
---|
[3e896e1] | 248 | }
|
---|
[a35b458] | 249 |
|
---|
[c064b58] | 250 | if (!img_setup(lsface)) {
|
---|
| 251 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
| 252 | return 6;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if (!fullscreen) {
|
---|
| 256 | dwidth = DECORATION_WIDTH;
|
---|
| 257 | dheight = DECORATION_HEIGHT;
|
---|
| 258 | } else {
|
---|
| 259 | dwidth = 0;
|
---|
| 260 | dheight = 0;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | window_resize(main_window, 0, 0, img_width + dwidth,
|
---|
| 264 | img_height + dheight, WINDOW_PLACEMENT_ANY);
|
---|
[3e896e1] | 265 | window_exec(main_window);
|
---|
[a35b458] | 266 |
|
---|
[3e896e1] | 267 | task_retval(0);
|
---|
| 268 | async_manager();
|
---|
[a35b458] | 269 |
|
---|
[3e896e1] | 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /** @}
|
---|
| 274 | */
|
---|