source: mainline/uspace/app/viewer/viewer.c@ 75baf6e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 75baf6e was 62fbb7e, checked in by Martin Decky <martin@…>, 12 years ago

refactor window placement logic and introduce logical window placement flags

  • The original setup of window position during creation (window_open()) was quite useless, because the window had no surface yet.
  • Now the window position can be optinally set using window_resize() and various logical placement flags are available.
  • A separate window_move() routine could be introduced eventually if needed, but for the initial setup of the window the combination of window position and size works fine.
  • Property mode set to 100644
File size: 4.6 KB
Line 
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>
36#include <unistd.h>
37#include <fcntl.h>
38#include <sys/stat.h>
39#include <errno.h>
40#include <malloc.h>
41#include <stdbool.h>
42#include <window.h>
43#include <canvas.h>
44#include <surface.h>
45#include <codec/tga.h>
46
47#define NAME "viewer"
48
49#define WINDOW_WIDTH 1024
50#define WINDOW_HEIGHT 768
51
52static size_t imgs_count;
53static size_t imgs_current = 0;
54static char **imgs;
55
56static window_t *main_window;
57static surface_t *surface = NULL;
58static canvas_t *canvas = NULL;
59
60static bool img_load(const char *);
61
62static void on_keyboard_event(widget_t *widget, void *data)
63{
64 kbd_event_t *event = (kbd_event_t *) data;
65 bool update = false;
66
67 if ((event->type == KEY_PRESS) && (event->c == 'q'))
68 exit(0);
69
70 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
71 if (imgs_current == imgs_count - 1)
72 imgs_current = 0;
73 else
74 imgs_current++;
75
76 update = true;
77 }
78
79 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
80 if (imgs_current == 0)
81 imgs_current = imgs_count - 1;
82 else
83 imgs_current--;
84
85 update = true;
86 }
87
88 if (update) {
89 if (!img_load(imgs[imgs_current])) {
90 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
91 exit(2);
92 }
93 }
94}
95
96static bool img_load(const char *fname)
97{
98 int fd = open(fname, O_RDONLY);
99 if (fd < 0)
100 return false;
101
102 struct stat stat;
103 int rc = fstat(fd, &stat);
104 if (rc != EOK) {
105 close(fd);
106 return false;
107 }
108
109 void *tga = malloc(stat.size);
110 if (tga == NULL) {
111 close(fd);
112 return false;
113 }
114
115 ssize_t rd = read_all(fd, tga, stat.size);
116 if ((rd < 0) || (rd != (ssize_t) stat.size)) {
117 free(tga);
118 close(fd);
119 return false;
120 }
121
122 close(fd);
123
124 surface_t *local_surface = decode_tga(tga, stat.size, 0);
125 if (local_surface == NULL) {
126 free(tga);
127 return false;
128 }
129
130 free(tga);
131
132 if (canvas != NULL) {
133 if (!update_canvas(canvas, local_surface)) {
134 surface_destroy(local_surface);
135 return false;
136 }
137 } else {
138 canvas = create_canvas(window_root(main_window),
139 WINDOW_WIDTH, WINDOW_HEIGHT, local_surface);
140 if (canvas == NULL) {
141 surface_destroy(local_surface);
142 return false;
143 }
144
145 sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
146 }
147
148 if (surface != NULL)
149 surface_destroy(surface);
150
151 surface = local_surface;
152
153 return true;
154}
155
156int main(int argc, char *argv[])
157{
158 if (argc < 2) {
159 printf("Compositor server not specified.\n");
160 return 1;
161 }
162
163 if (argc < 3) {
164 printf("No image files specified.\n");
165 return 1;
166 }
167
168 main_window = window_open(argv[1], true, false, "viewer");
169 if (!main_window) {
170 printf("Cannot open main window.\n");
171 return 2;
172 }
173
174 imgs_count = argc - 2;
175 imgs = calloc(imgs_count, sizeof(char *));
176 if (imgs == NULL) {
177 printf("Out of memory.\n");
178 return 3;
179 }
180
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");
185 return 4;
186 }
187 }
188
189 if (!img_load(imgs[imgs_current])) {
190 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
191 return 2;
192 }
193
194 window_resize(main_window, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
195 WINDOW_PLACEMENT_ABSOLUTE);
196 window_exec(main_window);
197
198 task_retval(0);
199 async_manager();
200
201 return 0;
202}
203
204/** @}
205 */
Note: See TracBrowser for help on using the repository browser.