[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>
|
---|
| 43 | #include <surface.h>
|
---|
| 44 | #include <codec/tga.h>
|
---|
[1c635d6] | 45 | #include <task.h>
|
---|
[3e896e1] | 46 |
|
---|
| 47 | #define NAME "viewer"
|
---|
| 48 |
|
---|
| 49 | #define WINDOW_WIDTH 1024
|
---|
| 50 | #define WINDOW_HEIGHT 768
|
---|
| 51 |
|
---|
[c064b58] | 52 | #define DECORATION_WIDTH 8
|
---|
| 53 | #define DECORATION_HEIGHT 28
|
---|
| 54 |
|
---|
[3e896e1] | 55 | static size_t imgs_count;
|
---|
| 56 | static size_t imgs_current = 0;
|
---|
| 57 | static char **imgs;
|
---|
| 58 |
|
---|
| 59 | static window_t *main_window;
|
---|
| 60 | static surface_t *surface = NULL;
|
---|
| 61 | static canvas_t *canvas = NULL;
|
---|
| 62 |
|
---|
[c064b58] | 63 | static surface_coord_t img_width;
|
---|
| 64 | static surface_coord_t img_height;
|
---|
| 65 |
|
---|
| 66 | static bool img_load(const char *, surface_t **);
|
---|
| 67 | static bool img_setup(surface_t *);
|
---|
[3e896e1] | 68 |
|
---|
| 69 | static void on_keyboard_event(widget_t *widget, void *data)
|
---|
| 70 | {
|
---|
| 71 | kbd_event_t *event = (kbd_event_t *) data;
|
---|
| 72 | bool update = false;
|
---|
| 73 |
|
---|
| 74 | if ((event->type == KEY_PRESS) && (event->c == 'q'))
|
---|
| 75 | exit(0);
|
---|
| 76 |
|
---|
| 77 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
|
---|
| 78 | if (imgs_current == imgs_count - 1)
|
---|
| 79 | imgs_current = 0;
|
---|
| 80 | else
|
---|
| 81 | imgs_current++;
|
---|
| 82 |
|
---|
| 83 | update = true;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
|
---|
| 87 | if (imgs_current == 0)
|
---|
| 88 | imgs_current = imgs_count - 1;
|
---|
| 89 | else
|
---|
| 90 | imgs_current--;
|
---|
| 91 |
|
---|
| 92 | update = true;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if (update) {
|
---|
[c064b58] | 96 | surface_t *lsface;
|
---|
| 97 |
|
---|
| 98 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
[3e896e1] | 99 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
[c064b58] | 100 | exit(4);
|
---|
| 101 | }
|
---|
| 102 | if (!img_setup(lsface)) {
|
---|
| 103 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
| 104 | exit(6);
|
---|
[3e896e1] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[c064b58] | 109 | static bool img_load(const char *fname, surface_t **p_local_surface)
|
---|
[3e896e1] | 110 | {
|
---|
[b19e892] | 111 | int fd = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ);
|
---|
[8e670dd] | 112 | if (fd < 0)
|
---|
[3e896e1] | 113 | return false;
|
---|
| 114 |
|
---|
| 115 | struct stat stat;
|
---|
[23a0368] | 116 | int rc = vfs_stat(fd, &stat);
|
---|
| 117 | if (rc != EOK) {
|
---|
[9c4cf0d] | 118 | vfs_put(fd);
|
---|
[3e896e1] | 119 | return false;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void *tga = malloc(stat.size);
|
---|
| 123 | if (tga == NULL) {
|
---|
[9c4cf0d] | 124 | vfs_put(fd);
|
---|
[3e896e1] | 125 | return false;
|
---|
| 126 | }
|
---|
[58898d1d] | 127 |
|
---|
[ce04ea44] | 128 | ssize_t rd = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size);
|
---|
[3e896e1] | 129 | if ((rd < 0) || (rd != (ssize_t) stat.size)) {
|
---|
| 130 | free(tga);
|
---|
[9c4cf0d] | 131 | vfs_put(fd);
|
---|
[3e896e1] | 132 | return false;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[9c4cf0d] | 135 | vfs_put(fd);
|
---|
[3e896e1] | 136 |
|
---|
[c064b58] | 137 | *p_local_surface = decode_tga(tga, stat.size, 0);
|
---|
| 138 | if (*p_local_surface == NULL) {
|
---|
[3e896e1] | 139 | free(tga);
|
---|
| 140 | return false;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | free(tga);
|
---|
[c064b58] | 144 |
|
---|
| 145 | surface_get_resolution(*p_local_surface, &img_width, &img_height);
|
---|
[3e896e1] | 146 |
|
---|
[c064b58] | 147 | return true;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static bool img_setup(surface_t *local_surface)
|
---|
| 151 | {
|
---|
[3e896e1] | 152 | if (canvas != NULL) {
|
---|
| 153 | if (!update_canvas(canvas, local_surface)) {
|
---|
| 154 | surface_destroy(local_surface);
|
---|
| 155 | return false;
|
---|
| 156 | }
|
---|
| 157 | } else {
|
---|
[10cb47e] | 158 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
[c064b58] | 159 | img_width, img_height, local_surface);
|
---|
[3e896e1] | 160 | if (canvas == NULL) {
|
---|
| 161 | surface_destroy(local_surface);
|
---|
| 162 | return false;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (surface != NULL)
|
---|
| 169 | surface_destroy(surface);
|
---|
| 170 |
|
---|
| 171 | surface = local_surface;
|
---|
| 172 | return true;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | int main(int argc, char *argv[])
|
---|
| 176 | {
|
---|
[c064b58] | 177 | window_flags_t flags;
|
---|
| 178 | surface_t *lsface;
|
---|
| 179 | bool fullscreen;
|
---|
| 180 | sysarg_t dwidth;
|
---|
| 181 | sysarg_t dheight;
|
---|
| 182 |
|
---|
[3e896e1] | 183 | if (argc < 2) {
|
---|
| 184 | printf("Compositor server not specified.\n");
|
---|
| 185 | return 1;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (argc < 3) {
|
---|
| 189 | printf("No image files specified.\n");
|
---|
| 190 | return 1;
|
---|
| 191 | }
|
---|
[c064b58] | 192 |
|
---|
[3e896e1] | 193 | imgs_count = argc - 2;
|
---|
| 194 | imgs = calloc(imgs_count, sizeof(char *));
|
---|
| 195 | if (imgs == NULL) {
|
---|
| 196 | printf("Out of memory.\n");
|
---|
[c064b58] | 197 | return 2;
|
---|
[3e896e1] | 198 | }
|
---|
| 199 |
|
---|
| 200 | for (int i = 0; i < argc - 2; i++) {
|
---|
| 201 | imgs[i] = str_dup(argv[i + 2]);
|
---|
| 202 | if (imgs[i] == NULL) {
|
---|
| 203 | printf("Out of memory.\n");
|
---|
[c064b58] | 204 | return 3;
|
---|
[3e896e1] | 205 | }
|
---|
| 206 | }
|
---|
[c064b58] | 207 |
|
---|
| 208 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
[3e896e1] | 209 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
[c064b58] | 210 | return 4;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | fullscreen = ((img_width == WINDOW_WIDTH) &&
|
---|
| 214 | (img_height == WINDOW_HEIGHT));
|
---|
| 215 |
|
---|
| 216 | flags = WINDOW_MAIN;
|
---|
| 217 | if (!fullscreen)
|
---|
| 218 | flags |= WINDOW_DECORATED;
|
---|
| 219 |
|
---|
[10cb47e] | 220 | main_window = window_open(argv[1], NULL, flags, "viewer");
|
---|
[c064b58] | 221 | if (!main_window) {
|
---|
| 222 | printf("Cannot open main window.\n");
|
---|
| 223 | return 5;
|
---|
[3e896e1] | 224 | }
|
---|
| 225 |
|
---|
[c064b58] | 226 |
|
---|
| 227 | if (!img_setup(lsface)) {
|
---|
| 228 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
| 229 | return 6;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (!fullscreen) {
|
---|
| 233 | dwidth = DECORATION_WIDTH;
|
---|
| 234 | dheight = DECORATION_HEIGHT;
|
---|
| 235 | } else {
|
---|
| 236 | dwidth = 0;
|
---|
| 237 | dheight = 0;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | window_resize(main_window, 0, 0, img_width + dwidth,
|
---|
| 241 | img_height + dheight, WINDOW_PLACEMENT_ANY);
|
---|
[3e896e1] | 242 | window_exec(main_window);
|
---|
| 243 |
|
---|
| 244 | task_retval(0);
|
---|
| 245 | async_manager();
|
---|
| 246 |
|
---|
| 247 | return 0;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** @}
|
---|
| 251 | */
|
---|