source: mainline/uspace/app/launcher/launcher.c@ 0576df9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0576df9 was 0576df9, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Decoding images without libdraw

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2020 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 launcher
31 * @{
32 */
33/** @file Launcher
34 */
35
36#include <errno.h>
37#include <gfx/coord.h>
38#include <gfximage/tga.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <str.h>
43#include <str_error.h>
44#include <task.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 "images.h"
54#include "launcher.h"
55
56#define NAME "launcher"
57
58static char *display_spec = UI_DISPLAY_DEFAULT;
59
60static void wnd_close(ui_window_t *, void *);
61
62static ui_window_cb_t window_cb = {
63 .close = wnd_close
64};
65
66static void pb_clicked(ui_pbutton_t *, void *);
67
68static ui_pbutton_cb_t pbutton_cb = {
69 .clicked = pb_clicked
70};
71
72static int app_launch(const char *);
73
74/** Window close button was clicked.
75 *
76 * @param window Window
77 * @param arg Argument (launcher)
78 */
79static void wnd_close(ui_window_t *window, void *arg)
80{
81 launcher_t *launcher = (launcher_t *) arg;
82
83 ui_quit(launcher->ui);
84}
85
86/** Push button was clicked.
87 *
88 * @param pbutton Push button
89 * @param arg Argument (launcher)
90 */
91static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
92{
93 launcher_t *launcher = (launcher_t *) arg;
94
95 if (pbutton == launcher->pb1) {
96 app_launch("/app/terminal");
97 } else if (pbutton == launcher->pb2) {
98 app_launch("/app/calculator");
99 } else if (pbutton == launcher->pb3) {
100 app_launch("/app/uidemo");
101 } else if (pbutton == launcher->pb4) {
102 app_launch("/app/launcher");
103 }
104}
105
106static int app_launch(const char *app)
107{
108 errno_t rc;
109 task_id_t id;
110 task_wait_t wait;
111
112 if (display_spec != UI_DISPLAY_DEFAULT) {
113 printf("%s: Spawning %s -d %s\n", NAME, app, display_spec);
114 rc = task_spawnl(&id, &wait, app, app, "-d", display_spec, NULL);
115 } else {
116 printf("%s: Spawning %s\n", NAME, app);
117 rc = task_spawnl(&id, &wait, app, app, NULL);
118 }
119
120 if (rc != EOK) {
121 printf("%s: Error spawning %s %s (%s)\n", NAME, app,
122 display_spec != DISPLAY_DEFAULT ? display_spec :
123 "<default>", str_error(rc));
124 return -1;
125 }
126
127 task_exit_t texit;
128 int retval;
129 rc = task_wait(&wait, &texit, &retval);
130 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
131 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
132 app, str_error(rc));
133 return -1;
134 }
135
136 return retval;
137}
138
139static void print_syntax(void)
140{
141 printf("Syntax: %s [-d <display-spec>]\n", NAME);
142}
143
144int main(int argc, char *argv[])
145{
146 int i;
147 launcher_t launcher;
148 ui_t *ui = NULL;
149 ui_wnd_params_t params;
150 ui_window_t *window = NULL;
151 ui_resource_t *ui_res;
152 gfx_bitmap_params_t logo_params;
153 gfx_bitmap_t *logo_bmp;
154 gfx_context_t *gc;
155 gfx_rect_t logo_rect;
156 gfx_rect_t rect;
157 gfx_coord2_t off;
158 errno_t rc;
159
160 i = 1;
161 while (i < argc) {
162 if (str_cmp(argv[i], "-d") == 0) {
163 ++i;
164 if (i >= argc) {
165 printf("Argument missing.\n");
166 print_syntax();
167 return 1;
168 }
169
170 display_spec = argv[i++];
171 } else {
172 printf("Invalid option '%s'.\n", argv[i]);
173 print_syntax();
174 return 1;
175 }
176 }
177
178 rc = ui_create(display_spec, &ui);
179 if (rc != EOK) {
180 printf("Error creating UI on display %s.\n", display_spec);
181 return rc;
182 }
183
184 ui_wnd_params_init(&params);
185 params.caption = "Launcher";
186 params.rect.p0.x = 0;
187 params.rect.p0.y = 0;
188 params.rect.p1.x = 210;
189 params.rect.p1.y = 300;
190
191 memset((void *) &launcher, 0, sizeof(launcher));
192 launcher.ui = ui;
193
194 rc = ui_window_create(ui, &params, &window);
195 if (rc != EOK) {
196 printf("Error creating window.\n");
197 return rc;
198 }
199
200 ui_window_set_cb(window, &window_cb, (void *) &launcher);
201 launcher.window = window;
202
203 ui_res = ui_window_get_res(window);
204 gc = ui_window_get_gc(window);
205
206 rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
207 &logo_bmp, &logo_rect);
208 if (rc != EOK) {
209 printf("Unable to decode logo.\n");
210 return 1;
211 }
212
213 gfx_bitmap_params_init(&logo_params);
214 logo_params.rect = logo_rect;
215
216 rc = ui_fixed_create(&launcher.fixed);
217 if (rc != EOK) {
218 printf("Error creating fixed layout.\n");
219 return rc;
220 }
221
222 rc = ui_image_create(ui_res, logo_bmp, &logo_rect, &launcher.image);
223 if (rc != EOK) {
224 printf("Error creating label.\n");
225 return rc;
226 }
227
228 off.x = 5;
229 off.y = 32;
230 gfx_rect_translate(&off, &logo_rect, &rect);
231 ui_image_set_rect(launcher.image, &rect);
232
233 rc = ui_fixed_add(launcher.fixed, ui_image_ctl(launcher.image));
234 if (rc != EOK) {
235 printf("Error adding control to layout.\n");
236 return rc;
237 }
238 rc = ui_label_create(ui_res, "Launch application", &launcher.label);
239 if (rc != EOK) {
240 printf("Error creating label.\n");
241 return rc;
242 }
243
244 rect.p0.x = 60;
245 rect.p0.y = 107;
246 rect.p1.x = 160;
247 rect.p1.y = 120;
248 ui_label_set_rect(launcher.label, &rect);
249 ui_label_set_halign(launcher.label, gfx_halign_center);
250
251 rc = ui_fixed_add(launcher.fixed, ui_label_ctl(launcher.label));
252 if (rc != EOK) {
253 printf("Error adding control to layout.\n");
254 return rc;
255 }
256
257 rc = ui_pbutton_create(ui_res, "Terminal", &launcher.pb1);
258 if (rc != EOK) {
259 printf("Error creating button.\n");
260 return rc;
261 }
262
263 ui_pbutton_set_cb(launcher.pb1, &pbutton_cb, (void *) &launcher);
264
265 rect.p0.x = 15;
266 rect.p0.y = 130;
267 rect.p1.x = 190;
268 rect.p1.y = 158;
269 ui_pbutton_set_rect(launcher.pb1, &rect);
270
271 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb1));
272 if (rc != EOK) {
273 printf("Error adding control to layout.\n");
274 return rc;
275 }
276
277 rc = ui_pbutton_create(ui_res, "Calculator", &launcher.pb2);
278 if (rc != EOK) {
279 printf("Error creating button.\n");
280 return rc;
281 }
282
283 ui_pbutton_set_cb(launcher.pb2, &pbutton_cb, (void *) &launcher);
284
285 rect.p0.x = 15;
286 rect.p0.y = 170;
287 rect.p1.x = 190;
288 rect.p1.y = 198;
289 ui_pbutton_set_rect(launcher.pb2, &rect);
290
291 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb2));
292 if (rc != EOK) {
293 printf("Error adding control to layout.\n");
294 return rc;
295 }
296
297 rc = ui_pbutton_create(ui_res, "UI Demo", &launcher.pb3);
298 if (rc != EOK) {
299 printf("Error creating button.\n");
300 return rc;
301 }
302
303 ui_pbutton_set_cb(launcher.pb3, &pbutton_cb, (void *) &launcher);
304
305 rect.p0.x = 15;
306 rect.p0.y = 210;
307 rect.p1.x = 190;
308 rect.p1.y = 238;
309 ui_pbutton_set_rect(launcher.pb3, &rect);
310
311 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb3));
312 if (rc != EOK) {
313 printf("Error adding control to layout.\n");
314 return rc;
315 }
316
317 rc = ui_pbutton_create(ui_res, "Launcher", &launcher.pb4);
318 if (rc != EOK) {
319 printf("Error creating button.\n");
320 return rc;
321 }
322
323 ui_pbutton_set_cb(launcher.pb4, &pbutton_cb, (void *) &launcher);
324
325 rect.p0.x = 15;
326 rect.p0.y = 250;
327 rect.p1.x = 190;
328 rect.p1.y = 278;
329 ui_pbutton_set_rect(launcher.pb4, &rect);
330
331 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb4));
332 if (rc != EOK) {
333 printf("Error adding control to layout.\n");
334 return rc;
335 }
336
337 ui_window_add(window, ui_fixed_ctl(launcher.fixed));
338
339 (void) ui_res;
340 (void) app_launch;
341
342 rc = ui_window_paint(window);
343 if (rc != EOK) {
344 printf("Error painting window.\n");
345 return rc;
346 }
347
348 ui_run(ui);
349
350 ui_window_destroy(window);
351 ui_destroy(ui);
352
353 return 0;
354}
355
356/** @}
357 */
Note: See TracBrowser for help on using the repository browser.