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 |
|
---|
58 | static char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
59 |
|
---|
60 | static void wnd_close(ui_window_t *, void *);
|
---|
61 |
|
---|
62 | static ui_window_cb_t window_cb = {
|
---|
63 | .close = wnd_close
|
---|
64 | };
|
---|
65 |
|
---|
66 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
67 |
|
---|
68 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
69 | .clicked = pb_clicked
|
---|
70 | };
|
---|
71 |
|
---|
72 | static int app_launch(const char *);
|
---|
73 |
|
---|
74 | /** Window close button was clicked.
|
---|
75 | *
|
---|
76 | * @param window Window
|
---|
77 | * @param arg Argument (launcher)
|
---|
78 | */
|
---|
79 | static 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 | */
|
---|
91 | static 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 |
|
---|
106 | static 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 |
|
---|
139 | static void print_syntax(void)
|
---|
140 | {
|
---|
141 | printf("Syntax: %s [-d <display-spec>]\n", NAME);
|
---|
142 | }
|
---|
143 |
|
---|
144 | int 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(¶ms);
|
---|
185 | params.caption = "Launcher";
|
---|
186 | params.placement = ui_wnd_place_top_right;
|
---|
187 | params.rect.p0.x = 0;
|
---|
188 | params.rect.p0.y = 0;
|
---|
189 | params.rect.p1.x = 210;
|
---|
190 | params.rect.p1.y = 300;
|
---|
191 |
|
---|
192 | memset((void *) &launcher, 0, sizeof(launcher));
|
---|
193 | launcher.ui = ui;
|
---|
194 |
|
---|
195 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
196 | if (rc != EOK) {
|
---|
197 | printf("Error creating window.\n");
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 | ui_window_set_cb(window, &window_cb, (void *) &launcher);
|
---|
202 | launcher.window = window;
|
---|
203 |
|
---|
204 | ui_res = ui_window_get_res(window);
|
---|
205 | gc = ui_window_get_gc(window);
|
---|
206 |
|
---|
207 | rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
|
---|
208 | &logo_bmp, &logo_rect);
|
---|
209 | if (rc != EOK) {
|
---|
210 | printf("Unable to decode logo.\n");
|
---|
211 | return 1;
|
---|
212 | }
|
---|
213 |
|
---|
214 | gfx_bitmap_params_init(&logo_params);
|
---|
215 | logo_params.rect = logo_rect;
|
---|
216 |
|
---|
217 | rc = ui_fixed_create(&launcher.fixed);
|
---|
218 | if (rc != EOK) {
|
---|
219 | printf("Error creating fixed layout.\n");
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | rc = ui_image_create(ui_res, logo_bmp, &logo_rect, &launcher.image);
|
---|
224 | if (rc != EOK) {
|
---|
225 | printf("Error creating label.\n");
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | off.x = 5;
|
---|
230 | off.y = 32;
|
---|
231 | gfx_rect_translate(&off, &logo_rect, &rect);
|
---|
232 | ui_image_set_rect(launcher.image, &rect);
|
---|
233 |
|
---|
234 | rc = ui_fixed_add(launcher.fixed, ui_image_ctl(launcher.image));
|
---|
235 | if (rc != EOK) {
|
---|
236 | printf("Error adding control to layout.\n");
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 | rc = ui_label_create(ui_res, "Launch application", &launcher.label);
|
---|
240 | if (rc != EOK) {
|
---|
241 | printf("Error creating label.\n");
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | rect.p0.x = 60;
|
---|
246 | rect.p0.y = 107;
|
---|
247 | rect.p1.x = 160;
|
---|
248 | rect.p1.y = 120;
|
---|
249 | ui_label_set_rect(launcher.label, &rect);
|
---|
250 | ui_label_set_halign(launcher.label, gfx_halign_center);
|
---|
251 |
|
---|
252 | rc = ui_fixed_add(launcher.fixed, ui_label_ctl(launcher.label));
|
---|
253 | if (rc != EOK) {
|
---|
254 | printf("Error adding control to layout.\n");
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 |
|
---|
258 | rc = ui_pbutton_create(ui_res, "Terminal", &launcher.pb1);
|
---|
259 | if (rc != EOK) {
|
---|
260 | printf("Error creating button.\n");
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | ui_pbutton_set_cb(launcher.pb1, &pbutton_cb, (void *) &launcher);
|
---|
265 |
|
---|
266 | rect.p0.x = 15;
|
---|
267 | rect.p0.y = 130;
|
---|
268 | rect.p1.x = 190;
|
---|
269 | rect.p1.y = 158;
|
---|
270 | ui_pbutton_set_rect(launcher.pb1, &rect);
|
---|
271 |
|
---|
272 | rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb1));
|
---|
273 | if (rc != EOK) {
|
---|
274 | printf("Error adding control to layout.\n");
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 | rc = ui_pbutton_create(ui_res, "Calculator", &launcher.pb2);
|
---|
279 | if (rc != EOK) {
|
---|
280 | printf("Error creating button.\n");
|
---|
281 | return rc;
|
---|
282 | }
|
---|
283 |
|
---|
284 | ui_pbutton_set_cb(launcher.pb2, &pbutton_cb, (void *) &launcher);
|
---|
285 |
|
---|
286 | rect.p0.x = 15;
|
---|
287 | rect.p0.y = 170;
|
---|
288 | rect.p1.x = 190;
|
---|
289 | rect.p1.y = 198;
|
---|
290 | ui_pbutton_set_rect(launcher.pb2, &rect);
|
---|
291 |
|
---|
292 | rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb2));
|
---|
293 | if (rc != EOK) {
|
---|
294 | printf("Error adding control to layout.\n");
|
---|
295 | return rc;
|
---|
296 | }
|
---|
297 |
|
---|
298 | rc = ui_pbutton_create(ui_res, "UI Demo", &launcher.pb3);
|
---|
299 | if (rc != EOK) {
|
---|
300 | printf("Error creating button.\n");
|
---|
301 | return rc;
|
---|
302 | }
|
---|
303 |
|
---|
304 | ui_pbutton_set_cb(launcher.pb3, &pbutton_cb, (void *) &launcher);
|
---|
305 |
|
---|
306 | rect.p0.x = 15;
|
---|
307 | rect.p0.y = 210;
|
---|
308 | rect.p1.x = 190;
|
---|
309 | rect.p1.y = 238;
|
---|
310 | ui_pbutton_set_rect(launcher.pb3, &rect);
|
---|
311 |
|
---|
312 | rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb3));
|
---|
313 | if (rc != EOK) {
|
---|
314 | printf("Error adding control to layout.\n");
|
---|
315 | return rc;
|
---|
316 | }
|
---|
317 |
|
---|
318 | rc = ui_pbutton_create(ui_res, "Launcher", &launcher.pb4);
|
---|
319 | if (rc != EOK) {
|
---|
320 | printf("Error creating button.\n");
|
---|
321 | return rc;
|
---|
322 | }
|
---|
323 |
|
---|
324 | ui_pbutton_set_cb(launcher.pb4, &pbutton_cb, (void *) &launcher);
|
---|
325 |
|
---|
326 | rect.p0.x = 15;
|
---|
327 | rect.p0.y = 250;
|
---|
328 | rect.p1.x = 190;
|
---|
329 | rect.p1.y = 278;
|
---|
330 | ui_pbutton_set_rect(launcher.pb4, &rect);
|
---|
331 |
|
---|
332 | rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb4));
|
---|
333 | if (rc != EOK) {
|
---|
334 | printf("Error adding control to layout.\n");
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 | ui_window_add(window, ui_fixed_ctl(launcher.fixed));
|
---|
339 |
|
---|
340 | (void) ui_res;
|
---|
341 | (void) app_launch;
|
---|
342 |
|
---|
343 | rc = ui_window_paint(window);
|
---|
344 | if (rc != EOK) {
|
---|
345 | printf("Error painting window.\n");
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 | ui_run(ui);
|
---|
350 |
|
---|
351 | ui_window_destroy(window);
|
---|
352 | ui_destroy(ui);
|
---|
353 |
|
---|
354 | return 0;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /** @}
|
---|
358 | */
|
---|