source: mainline/uspace/app/aboutos/aboutos.c

Last change on this file was cd27cd1, checked in by Jiri Svoboda <jiri@…>, 17 months ago

About HelenOS should fall back to console automatically + fix unused

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * Copyright (c) 2012 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup aboutos
31 * @{
32 */
33/** @file About HelenOS
34 */
35
36#include <errno.h>
37#include <gfx/coord.h>
38#include <gfximage/tga.h>
39#include <macros.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str.h>
44#include <str_error.h>
45#include <ui/fixed.h>
46#include <ui/image.h>
47#include <ui/label.h>
48#include <ui/pbutton.h>
49#include <ui/resource.h>
50#include <ui/ui.h>
51#include <ui/window.h>
52
53#include "aboutos.h"
54#include "images.h"
55
56#define NAME "aboutos"
57
58static void aboutos_wnd_close(ui_window_t *, void *);
59static void aboutos_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
60
61static ui_window_cb_t window_cb = {
62 .close = aboutos_wnd_close,
63 .kbd = aboutos_wnd_kbd
64};
65
66static void pb_clicked(ui_pbutton_t *, void *);
67
68static ui_pbutton_cb_t pbutton_cb = {
69 .clicked = pb_clicked
70};
71
72/** Window close button was clicked.
73 *
74 * @param window Window
75 * @param arg Argument (aboutos)
76 */
77static void aboutos_wnd_close(ui_window_t *window, void *arg)
78{
79 aboutos_t *aboutos = (aboutos_t *) arg;
80
81 ui_quit(aboutos->ui);
82}
83
84/** About HelenOS window keyboard event handler.
85 *
86 * @param window Window
87 * @param arg Argument (ui_prompt_dialog_t *)
88 * @param event Keyboard event
89 */
90static void aboutos_wnd_kbd(ui_window_t *window, void *arg,
91 kbd_event_t *event)
92{
93 aboutos_t *aboutos = (aboutos_t *) arg;
94
95 (void)window;
96
97 if (event->type == KEY_PRESS &&
98 (event->mods & (KM_CTRL | KM_SHIFT | KM_ALT)) == 0) {
99 if (event->key == KC_ENTER) {
100 /* Quit */
101 ui_quit(aboutos->ui);
102
103 }
104 }
105
106 ui_window_def_kbd(window, event);
107}
108
109/** Push button was clicked.
110 *
111 * @param pbutton Push button
112 * @param arg Argument (aboutos)
113 */
114static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
115{
116 aboutos_t *aboutos = (aboutos_t *) arg;
117
118 ui_quit(aboutos->ui);
119}
120
121static void print_syntax(void)
122{
123 printf("Syntax: %s [-d <display-spec>]\n", NAME);
124}
125
126int main(int argc, char *argv[])
127{
128 int i;
129 aboutos_t aboutos;
130 ui_t *ui = NULL;
131 ui_wnd_params_t params;
132 ui_window_t *window = NULL;
133 ui_resource_t *ui_res;
134 gfx_bitmap_params_t logo_params;
135 gfx_bitmap_t *logo_bmp;
136 gfx_context_t *gc;
137 gfx_rect_t logo_rect;
138 gfx_rect_t rect;
139 gfx_coord2_t off;
140 const char *dspec = UI_ANY_DEFAULT;
141 errno_t rc;
142
143 i = 1;
144 while (i < argc) {
145 if (str_cmp(argv[i], "-d") == 0) {
146 ++i;
147 if (i >= argc) {
148 printf("Argument missing.\n");
149 print_syntax();
150 return 1;
151 }
152
153 dspec = argv[i++];
154 } else {
155 printf("Invalid option '%s'.\n", argv[i]);
156 print_syntax();
157 return 1;
158 }
159 }
160
161 rc = ui_create(dspec, &ui);
162 if (rc != EOK) {
163 printf("Error creating UI on display %s.\n", dspec);
164 return rc;
165 }
166
167 ui_wnd_params_init(&params);
168 params.caption = "About HelenOS";
169
170 /* FIXME: Auto layout */
171 if (ui_is_textmode(ui)) {
172 params.rect.p0.x = 0;
173 params.rect.p0.y = 0;
174 params.rect.p1.x = 45;
175 params.rect.p1.y = 15;
176 } else {
177 params.rect.p0.x = 0;
178 params.rect.p0.y = 0;
179 params.rect.p1.x = 350;
180 params.rect.p1.y = 275;
181 }
182
183 memset((void *) &aboutos, 0, sizeof(aboutos));
184 aboutos.ui = ui;
185
186 rc = ui_window_create(ui, &params, &window);
187 if (rc != EOK) {
188 printf("Error creating window.\n");
189 return rc;
190 }
191
192 ui_window_set_cb(window, &window_cb, (void *) &aboutos);
193 aboutos.window = window;
194
195 ui_res = ui_window_get_res(window);
196 gc = ui_window_get_gc(window);
197
198 rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
199 &logo_bmp, &logo_rect);
200 if (rc != EOK) {
201 printf("Unable to decode logo.\n");
202 return 1;
203 }
204
205 gfx_bitmap_params_init(&logo_params);
206 logo_params.rect = logo_rect;
207
208 rc = ui_fixed_create(&aboutos.fixed);
209 if (rc != EOK) {
210 printf("Error creating fixed layout.\n");
211 return rc;
212 }
213
214 rc = ui_image_create(ui_res, logo_bmp, &logo_rect, &aboutos.image);
215 if (rc != EOK) {
216 printf("Error creating label.\n");
217 return rc;
218 }
219
220 off.x = 76;
221 off.y = 42;
222 gfx_rect_translate(&off, &logo_rect, &rect);
223
224 /* Adjust for frame width (2 x 1 pixel) */
225 rect.p1.x += 2;
226 rect.p1.y += 2;
227 ui_image_set_rect(aboutos.image, &rect);
228 ui_image_set_flags(aboutos.image, ui_imgf_frame);
229
230 rc = ui_fixed_add(aboutos.fixed, ui_image_ctl(aboutos.image));
231 if (rc != EOK) {
232 printf("Error adding control to layout.\n");
233 return rc;
234 }
235
236 /* Release label */
237
238 rc = ui_label_create(ui_res, "HelenOS " STRING(HELENOS_RELEASE)
239 " (" STRING(HELENOS_CODENAME) ")", &aboutos.lrelease);
240 if (rc != EOK) {
241 printf("Error creating label.\n");
242 return rc;
243 }
244
245 if (ui_is_textmode(ui)) {
246 rect.p0.x = 1;
247 rect.p0.y = 5;
248 rect.p1.x = 44;
249 rect.p1.y = 6;
250 } else {
251 rect.p0.x = 10;
252 rect.p0.y = 140;
253 rect.p1.x = 340;
254 rect.p1.y = 160;
255 }
256
257 ui_label_set_rect(aboutos.lrelease, &rect);
258 ui_label_set_halign(aboutos.lrelease, gfx_halign_center);
259
260 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.lrelease));
261 if (rc != EOK) {
262 printf("Error adding control to layout.\n");
263 return rc;
264 }
265
266 /* Copyright label */
267
268 rc = ui_label_create(ui_res, STRING(HELENOS_COPYRIGHT), &aboutos.lcopy);
269 if (rc != EOK) {
270 printf("Error creating label.\n");
271 return rc;
272 }
273
274 if (ui_is_textmode(ui)) {
275 rect.p0.x = 1;
276 rect.p0.y = 6;
277 rect.p1.x = 44;
278 rect.p1.y = 7;
279 } else {
280 rect.p0.x = 10;
281 rect.p0.y = 160;
282 rect.p1.x = 340;
283 rect.p1.y = 180;
284 }
285
286 ui_label_set_rect(aboutos.lcopy, &rect);
287 ui_label_set_halign(aboutos.lcopy, gfx_halign_center);
288
289 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.lcopy));
290 if (rc != EOK) {
291 printf("Error adding control to layout.\n");
292 return rc;
293 }
294
295 /* Architecture label */
296
297 rc = ui_label_create(ui_res, "Running on " STRING(UARCH), &aboutos.larch);
298 if (rc != EOK) {
299 printf("Error creating label.\n");
300 return rc;
301 }
302
303 if (ui_is_textmode(ui)) {
304 rect.p0.x = 1;
305 rect.p0.y = 9;
306 rect.p1.x = 44;
307 rect.p1.y = 10;
308 } else {
309 rect.p0.x = 10;
310 rect.p0.y = 190;
311 rect.p1.x = 340;
312 rect.p1.y = 210;
313 }
314
315 ui_label_set_rect(aboutos.larch, &rect);
316 ui_label_set_halign(aboutos.larch, gfx_halign_center);
317
318 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.larch));
319 if (rc != EOK) {
320 printf("Error adding control to layout.\n");
321 return rc;
322 }
323
324 /* OK button */
325
326 rc = ui_pbutton_create(ui_res, "OK", &aboutos.pbok);
327 if (rc != EOK) {
328 printf("Error creating button.\n");
329 return rc;
330 }
331
332 ui_pbutton_set_cb(aboutos.pbok, &pbutton_cb, (void *) &aboutos);
333
334 if (ui_is_textmode(ui)) {
335 rect.p0.x = 17;
336 rect.p0.y = 13;
337 rect.p1.x = 28;
338 rect.p1.y = 14;
339 } else {
340 rect.p0.x = 125;
341 rect.p0.y = 235;
342 rect.p1.x = 225;
343 rect.p1.y = rect.p0.y + 28;
344 }
345
346 ui_pbutton_set_rect(aboutos.pbok, &rect);
347 ui_pbutton_set_default(aboutos.pbok, true);
348
349 rc = ui_fixed_add(aboutos.fixed, ui_pbutton_ctl(aboutos.pbok));
350 if (rc != EOK) {
351 printf("Error adding control to layout.\n");
352 return rc;
353 }
354
355 ui_window_add(window, ui_fixed_ctl(aboutos.fixed));
356
357 rc = ui_window_paint(window);
358 if (rc != EOK) {
359 printf("Error painting window.\n");
360 return rc;
361 }
362
363 ui_run(ui);
364
365 ui_window_destroy(window);
366 ui_destroy(ui);
367
368 return 0;
369}
370
371/** @}
372 */
Note: See TracBrowser for help on using the repository browser.