source: mainline/uspace/app/hello/hello.c@ 214aefb

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

Add minimalistic UI hello world application

This can serve as an example in the future, while uidemo is supposed
to grow to demonstrate most UI features.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup hello
30 * @{
31 */
32/** @file Hello world (in UI)
33 */
34
35#include <gfx/coord.h>
36#include <stdio.h>
37#include <str.h>
38#include <ui/fixed.h>
39#include <ui/label.h>
40#include <ui/resource.h>
41#include <ui/ui.h>
42#include <ui/window.h>
43#include "hello.h"
44
45static void wnd_close(ui_window_t *, void *);
46
47static ui_window_cb_t window_cb = {
48 .close = wnd_close
49};
50
51/** Window close button was clicked.
52 *
53 * @param window Window
54 * @param arg Argument (hello)
55 */
56static void wnd_close(ui_window_t *window, void *arg)
57{
58 hello_t *hello = (hello_t *) arg;
59
60 ui_quit(hello->ui);
61}
62
63/** Run hello world on display server. */
64static errno_t hello(const char *display_spec)
65{
66 ui_t *ui = NULL;
67 ui_wnd_params_t params;
68 ui_window_t *window = NULL;
69 hello_t hello;
70 gfx_rect_t rect;
71 ui_resource_t *ui_res;
72 errno_t rc;
73
74 rc = ui_create(display_spec, &ui);
75 if (rc != EOK) {
76 printf("Error creating UI on display %s.\n", display_spec);
77 return rc;
78 }
79
80 ui_wnd_params_init(&params);
81 params.caption = "Hello World";
82 params.rect.p0.x = 0;
83 params.rect.p0.y = 0;
84 params.rect.p1.x = 200;
85 params.rect.p1.y = 60;
86
87 memset((void *) &hello, 0, sizeof(hello));
88 hello.ui = ui;
89
90 rc = ui_window_create(ui, &params, &window);
91 if (rc != EOK) {
92 printf("Error creating window.\n");
93 return rc;
94 }
95
96 ui_window_set_cb(window, &window_cb, (void *) &hello);
97 hello.window = window;
98
99 ui_res = ui_window_get_res(window);
100
101 rc = ui_fixed_create(&hello.fixed);
102 if (rc != EOK) {
103 printf("Error creating fixed layout.\n");
104 return rc;
105 }
106
107 rc = ui_label_create(ui_res, "Hello, world!", &hello.label);
108 if (rc != EOK) {
109 printf("Error creating label.\n");
110 return rc;
111 }
112
113 rect.p0.x = 10;
114 rect.p0.y = 35;
115 rect.p1.x = 190;
116 rect.p1.y = 50;
117 ui_label_set_rect(hello.label, &rect);
118 ui_label_set_halign(hello.label, gfx_halign_center);
119
120 rc = ui_fixed_add(hello.fixed, ui_label_ctl(hello.label));
121 if (rc != EOK) {
122 printf("Error adding control to layout.\n");
123 return rc;
124 }
125
126 ui_window_add(window, ui_fixed_ctl(hello.fixed));
127
128 rc = ui_window_paint(window);
129 if (rc != EOK) {
130 printf("Error painting window.\n");
131 return rc;
132 }
133
134 ui_run(ui);
135
136 ui_window_destroy(window);
137 ui_destroy(ui);
138
139 return EOK;
140}
141
142static void print_syntax(void)
143{
144 printf("Syntax: hello [-d <display-spec>]\n");
145}
146
147int main(int argc, char *argv[])
148{
149 const char *display_spec = UI_DISPLAY_DEFAULT;
150 errno_t rc;
151 int i;
152
153 i = 1;
154 while (i < argc && argv[i][0] == '-') {
155 if (str_cmp(argv[i], "-d") == 0) {
156 ++i;
157 if (i >= argc) {
158 printf("Argument missing.\n");
159 print_syntax();
160 return 1;
161 }
162
163 display_spec = argv[i++];
164 } else {
165 printf("Invalid option '%s'.\n", argv[i]);
166 print_syntax();
167 return 1;
168 }
169 }
170
171 if (i < argc) {
172 print_syntax();
173 return 1;
174 }
175
176 rc = hello(display_spec);
177 if (rc != EOK)
178 return 1;
179
180 return 0;
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.