source: mainline/uspace/app/launcher/launcher.c@ 0e125698

Last change on this file since 0e125698 was d8ddf7a, checked in by Jiri Svoboda <jiri@…>, 5 years ago

UI demo should demonstrate image and entry controls

We also add the ability to draw a frame around image control, use
in UI demo and in launcher.

  • Property mode set to 100644
File size: 8.5 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 *, 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", NULL);
97 } else if (pbutton == launcher->pb2) {
98 app_launch("/app/calculator", NULL);
99 } else if (pbutton == launcher->pb3) {
100 app_launch("/app/uidemo", NULL);
101 } else if (pbutton == launcher->pb4) {
102 app_launch("/app/gfxdemo", "ui");
103 }
104}
105
106static int app_launch(const char *app, const char *arg)
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,
115 arg, NULL);
116 } else {
117 printf("%s: Spawning %s\n", NAME, app);
118 rc = task_spawnl(&id, &wait, app, app, arg, NULL);
119 }
120
121 if (rc != EOK) {
122 printf("%s: Error spawning %s %s (%s)\n", NAME, app,
123 display_spec != DISPLAY_DEFAULT ? display_spec :
124 "<default>", str_error(rc));
125 return -1;
126 }
127
128 task_exit_t texit;
129 int retval;
130 rc = task_wait(&wait, &texit, &retval);
131 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
132 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
133 app, str_error(rc));
134 return -1;
135 }
136
137 return retval;
138}
139
140static void print_syntax(void)
141{
142 printf("Syntax: %s [-d <display-spec>]\n", NAME);
143}
144
145int main(int argc, char *argv[])
146{
147 int i;
148 launcher_t launcher;
149 ui_t *ui = NULL;
150 ui_wnd_params_t params;
151 ui_window_t *window = NULL;
152 ui_resource_t *ui_res;
153 gfx_bitmap_params_t logo_params;
154 gfx_bitmap_t *logo_bmp;
155 gfx_context_t *gc;
156 gfx_rect_t logo_rect;
157 gfx_rect_t rect;
158 gfx_coord2_t off;
159 errno_t rc;
160
161 i = 1;
162 while (i < argc) {
163 if (str_cmp(argv[i], "-d") == 0) {
164 ++i;
165 if (i >= argc) {
166 printf("Argument missing.\n");
167 print_syntax();
168 return 1;
169 }
170
171 display_spec = argv[i++];
172 } else {
173 printf("Invalid option '%s'.\n", argv[i]);
174 print_syntax();
175 return 1;
176 }
177 }
178
179 rc = ui_create(display_spec, &ui);
180 if (rc != EOK) {
181 printf("Error creating UI on display %s.\n", display_spec);
182 return rc;
183 }
184
185 ui_wnd_params_init(&params);
186 params.caption = "Launcher";
187 params.placement = ui_wnd_place_top_right;
188 params.rect.p0.x = 0;
189 params.rect.p0.y = 0;
190 params.rect.p1.x = 210;
191 params.rect.p1.y = 300;
192
193 memset((void *) &launcher, 0, sizeof(launcher));
194 launcher.ui = ui;
195
196 rc = ui_window_create(ui, &params, &window);
197 if (rc != EOK) {
198 printf("Error creating window.\n");
199 return rc;
200 }
201
202 ui_window_set_cb(window, &window_cb, (void *) &launcher);
203 launcher.window = window;
204
205 ui_res = ui_window_get_res(window);
206 gc = ui_window_get_gc(window);
207
208 rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
209 &logo_bmp, &logo_rect);
210 if (rc != EOK) {
211 printf("Unable to decode logo.\n");
212 return 1;
213 }
214
215 gfx_bitmap_params_init(&logo_params);
216 logo_params.rect = logo_rect;
217
218 rc = ui_fixed_create(&launcher.fixed);
219 if (rc != EOK) {
220 printf("Error creating fixed layout.\n");
221 return rc;
222 }
223
224 rc = ui_image_create(ui_res, logo_bmp, &logo_rect, &launcher.image);
225 if (rc != EOK) {
226 printf("Error creating label.\n");
227 return rc;
228 }
229
230 off.x = 6;
231 off.y = 32;
232 gfx_rect_translate(&off, &logo_rect, &rect);
233
234 /* Adjust for frame width (2 x 1 pixel) */
235 rect.p1.x += 2;
236 rect.p1.y += 2;
237 ui_image_set_rect(launcher.image, &rect);
238 ui_image_set_flags(launcher.image, ui_imgf_frame);
239
240 rc = ui_fixed_add(launcher.fixed, ui_image_ctl(launcher.image));
241 if (rc != EOK) {
242 printf("Error adding control to layout.\n");
243 return rc;
244 }
245
246 rc = ui_label_create(ui_res, "Launch application", &launcher.label);
247 if (rc != EOK) {
248 printf("Error creating label.\n");
249 return rc;
250 }
251
252 rect.p0.x = 60;
253 rect.p0.y = 107;
254 rect.p1.x = 160;
255 rect.p1.y = 120;
256 ui_label_set_rect(launcher.label, &rect);
257 ui_label_set_halign(launcher.label, gfx_halign_center);
258
259 rc = ui_fixed_add(launcher.fixed, ui_label_ctl(launcher.label));
260 if (rc != EOK) {
261 printf("Error adding control to layout.\n");
262 return rc;
263 }
264
265 rc = ui_pbutton_create(ui_res, "Terminal", &launcher.pb1);
266 if (rc != EOK) {
267 printf("Error creating button.\n");
268 return rc;
269 }
270
271 ui_pbutton_set_cb(launcher.pb1, &pbutton_cb, (void *) &launcher);
272
273 rect.p0.x = 15;
274 rect.p0.y = 130;
275 rect.p1.x = 190;
276 rect.p1.y = 158;
277 ui_pbutton_set_rect(launcher.pb1, &rect);
278
279 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb1));
280 if (rc != EOK) {
281 printf("Error adding control to layout.\n");
282 return rc;
283 }
284
285 rc = ui_pbutton_create(ui_res, "Calculator", &launcher.pb2);
286 if (rc != EOK) {
287 printf("Error creating button.\n");
288 return rc;
289 }
290
291 ui_pbutton_set_cb(launcher.pb2, &pbutton_cb, (void *) &launcher);
292
293 rect.p0.x = 15;
294 rect.p0.y = 170;
295 rect.p1.x = 190;
296 rect.p1.y = 198;
297 ui_pbutton_set_rect(launcher.pb2, &rect);
298
299 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb2));
300 if (rc != EOK) {
301 printf("Error adding control to layout.\n");
302 return rc;
303 }
304
305 rc = ui_pbutton_create(ui_res, "UI Demo", &launcher.pb3);
306 if (rc != EOK) {
307 printf("Error creating button.\n");
308 return rc;
309 }
310
311 ui_pbutton_set_cb(launcher.pb3, &pbutton_cb, (void *) &launcher);
312
313 rect.p0.x = 15;
314 rect.p0.y = 210;
315 rect.p1.x = 190;
316 rect.p1.y = 238;
317 ui_pbutton_set_rect(launcher.pb3, &rect);
318
319 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb3));
320 if (rc != EOK) {
321 printf("Error adding control to layout.\n");
322 return rc;
323 }
324
325 rc = ui_pbutton_create(ui_res, "GFX Demo", &launcher.pb4);
326 if (rc != EOK) {
327 printf("Error creating button.\n");
328 return rc;
329 }
330
331 ui_pbutton_set_cb(launcher.pb4, &pbutton_cb, (void *) &launcher);
332
333 rect.p0.x = 15;
334 rect.p0.y = 250;
335 rect.p1.x = 190;
336 rect.p1.y = 278;
337 ui_pbutton_set_rect(launcher.pb4, &rect);
338
339 rc = ui_fixed_add(launcher.fixed, ui_pbutton_ctl(launcher.pb4));
340 if (rc != EOK) {
341 printf("Error adding control to layout.\n");
342 return rc;
343 }
344
345 ui_window_add(window, ui_fixed_ctl(launcher.fixed));
346
347 (void) ui_res;
348 (void) app_launch;
349
350 rc = ui_window_paint(window);
351 if (rc != EOK) {
352 printf("Error painting window.\n");
353 return rc;
354 }
355
356 ui_run(ui);
357
358 ui_window_destroy(window);
359 ui_destroy(ui);
360
361 return 0;
362}
363
364/** @}
365 */
Note: See TracBrowser for help on using the repository browser.