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