[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>
|
---|
[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;
|
---|
| 74 |
|
---|
| 75 | if ((event->type == KEY_PRESS) && (event->c == 'q'))
|
---|
| 76 | exit(0);
|
---|
| 77 |
|
---|
| 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++;
|
---|
| 83 |
|
---|
| 84 | update = true;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 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--;
|
---|
| 92 |
|
---|
| 93 | update = true;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 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;
|
---|
| 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 | }
|
---|
| 123 |
|
---|
| 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;
|
---|
| 131 | rc = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size, &nread);
|
---|
| 132 | if (rc != EOK || nread != stat.size) {
|
---|
[3e896e1] | 133 | free(tga);
|
---|
[9c4cf0d] | 134 | vfs_put(fd);
|
---|
[3e896e1] | 135 | return false;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[9c4cf0d] | 138 | vfs_put(fd);
|
---|
[3e896e1] | 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 | }
|
---|
| 145 |
|
---|
| 146 | free(tga);
|
---|
[c064b58] | 147 |
|
---|
| 148 | surface_get_resolution(*p_local_surface, &img_width, &img_height);
|
---|
[3e896e1] | 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 | }
|
---|
| 167 |
|
---|
| 168 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | if (surface != NULL)
|
---|
| 172 | surface_destroy(surface);
|
---|
| 173 |
|
---|
| 174 | surface = local_surface;
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | int main(int argc, char *argv[])
|
---|
| 179 | {
|
---|
[c064b58] | 180 | window_flags_t flags;
|
---|
| 181 | surface_t *lsface;
|
---|
| 182 | bool fullscreen;
|
---|
| 183 | sysarg_t dwidth;
|
---|
| 184 | sysarg_t dheight;
|
---|
| 185 |
|
---|
[3e896e1] | 186 | if (argc < 2) {
|
---|
| 187 | printf("Compositor server not specified.\n");
|
---|
| 188 | return 1;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (argc < 3) {
|
---|
| 192 | printf("No image files specified.\n");
|
---|
| 193 | return 1;
|
---|
| 194 | }
|
---|
[c064b58] | 195 |
|
---|
[3e896e1] | 196 | imgs_count = argc - 2;
|
---|
| 197 | imgs = calloc(imgs_count, sizeof(char *));
|
---|
| 198 | if (imgs == NULL) {
|
---|
| 199 | printf("Out of memory.\n");
|
---|
[c064b58] | 200 | return 2;
|
---|
[3e896e1] | 201 | }
|
---|
| 202 |
|
---|
| 203 | for (int i = 0; i < argc - 2; i++) {
|
---|
| 204 | imgs[i] = str_dup(argv[i + 2]);
|
---|
| 205 | if (imgs[i] == NULL) {
|
---|
| 206 | printf("Out of memory.\n");
|
---|
[c064b58] | 207 | return 3;
|
---|
[3e896e1] | 208 | }
|
---|
| 209 | }
|
---|
[c064b58] | 210 |
|
---|
| 211 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
[3e896e1] | 212 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
[c064b58] | 213 | return 4;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | fullscreen = ((img_width == WINDOW_WIDTH) &&
|
---|
| 217 | (img_height == WINDOW_HEIGHT));
|
---|
| 218 |
|
---|
| 219 | flags = WINDOW_MAIN;
|
---|
| 220 | if (!fullscreen)
|
---|
| 221 | flags |= WINDOW_DECORATED;
|
---|
| 222 |
|
---|
[10cb47e] | 223 | main_window = window_open(argv[1], NULL, flags, "viewer");
|
---|
[c064b58] | 224 | if (!main_window) {
|
---|
| 225 | printf("Cannot open main window.\n");
|
---|
| 226 | return 5;
|
---|
[3e896e1] | 227 | }
|
---|
| 228 |
|
---|
[c064b58] | 229 |
|
---|
| 230 | if (!img_setup(lsface)) {
|
---|
| 231 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
| 232 | return 6;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | if (!fullscreen) {
|
---|
| 236 | dwidth = DECORATION_WIDTH;
|
---|
| 237 | dheight = DECORATION_HEIGHT;
|
---|
| 238 | } else {
|
---|
| 239 | dwidth = 0;
|
---|
| 240 | dheight = 0;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | window_resize(main_window, 0, 0, img_width + dwidth,
|
---|
| 244 | img_height + dheight, WINDOW_PLACEMENT_ANY);
|
---|
[3e896e1] | 245 | window_exec(main_window);
|
---|
| 246 |
|
---|
| 247 | task_retval(0);
|
---|
| 248 | async_manager();
|
---|
| 249 |
|
---|
| 250 | return 0;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /** @}
|
---|
| 254 | */
|
---|