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 uidemo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file User interface demo
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <display.h>
|
---|
36 | #include <gfx/color.h>
|
---|
37 | #include <gfx/coord.h>
|
---|
38 | #include <gfx/render.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <task.h>
|
---|
42 | #include <ui/label.h>
|
---|
43 | #include <ui/pbutton.h>
|
---|
44 | #include <ui/resource.h>
|
---|
45 | #include <ui/wdecor.h>
|
---|
46 | #include "uidemo.h"
|
---|
47 |
|
---|
48 | static void wnd_close_event(void *);
|
---|
49 | static void wnd_focus_event(void *);
|
---|
50 | static void wnd_kbd_event(void *, kbd_event_t *);
|
---|
51 | static void wnd_pos_event(void *, pos_event_t *);
|
---|
52 | static void wnd_unfocus_event(void *);
|
---|
53 |
|
---|
54 | static display_wnd_cb_t wnd_cb = {
|
---|
55 | .close_event = wnd_close_event,
|
---|
56 | .focus_event = wnd_focus_event,
|
---|
57 | .kbd_event = wnd_kbd_event,
|
---|
58 | .pos_event = wnd_pos_event,
|
---|
59 | .unfocus_event = wnd_unfocus_event
|
---|
60 | };
|
---|
61 |
|
---|
62 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
63 |
|
---|
64 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
65 | .clicked = pb_clicked
|
---|
66 | };
|
---|
67 |
|
---|
68 | static void wd_close(ui_wdecor_t *, void *);
|
---|
69 | static void wd_move(ui_wdecor_t *, void *, gfx_coord2_t *);
|
---|
70 |
|
---|
71 | static ui_wdecor_cb_t wdecor_cb = {
|
---|
72 | .close = wd_close,
|
---|
73 | .move = wd_move
|
---|
74 | };
|
---|
75 |
|
---|
76 | /** Print syntax. */
|
---|
77 | static void print_syntax(void)
|
---|
78 | {
|
---|
79 | printf("Syntax: uidemo [-d <display>]\n");
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Handle window close event. */
|
---|
83 | static void wnd_close_event(void *arg)
|
---|
84 | {
|
---|
85 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
86 |
|
---|
87 | printf("Close event\n");
|
---|
88 | demo->quit = true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Handle window focus event. */
|
---|
92 | static void wnd_focus_event(void *arg)
|
---|
93 | {
|
---|
94 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
95 |
|
---|
96 | if (demo->wdecor != NULL) {
|
---|
97 | ui_wdecor_set_active(demo->wdecor, true);
|
---|
98 | ui_wdecor_paint(demo->wdecor);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Handle window keyboard event */
|
---|
103 | static void wnd_kbd_event(void *arg, kbd_event_t *event)
|
---|
104 | {
|
---|
105 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
106 |
|
---|
107 | (void) demo;
|
---|
108 | printf("Keyboard event type=%d key=%d\n", event->type, event->key);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Handle window position event */
|
---|
112 | static void wnd_pos_event(void *arg, pos_event_t *event)
|
---|
113 | {
|
---|
114 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
115 |
|
---|
116 | /* Make sure we don't process events until fully initialized */
|
---|
117 | if (demo->wdecor == NULL || demo->pb1 == NULL || demo->pb2 == NULL)
|
---|
118 | return;
|
---|
119 |
|
---|
120 | ui_wdecor_pos_event(demo->wdecor, event);
|
---|
121 | ui_pbutton_pos_event(demo->pb1, event);
|
---|
122 | ui_pbutton_pos_event(demo->pb2, event);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Handle window unfocus event. */
|
---|
126 | static void wnd_unfocus_event(void *arg)
|
---|
127 | {
|
---|
128 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
129 |
|
---|
130 | if (demo->wdecor != NULL) {
|
---|
131 | ui_wdecor_set_active(demo->wdecor, false);
|
---|
132 | ui_wdecor_paint(demo->wdecor);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Push button was clicked.
|
---|
137 | *
|
---|
138 | * @param pbutton Push button
|
---|
139 | * @param arg Argument (demo)
|
---|
140 | */
|
---|
141 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
142 | {
|
---|
143 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
144 | errno_t rc;
|
---|
145 |
|
---|
146 | if (pbutton == demo->pb1) {
|
---|
147 | printf("Clicked 'Confirm' button\n");
|
---|
148 | rc = ui_label_set_text(demo->label, "Confirmed");
|
---|
149 | if (rc != EOK)
|
---|
150 | printf("Error changing label text.\n");
|
---|
151 | (void) ui_label_paint(demo->label);
|
---|
152 | } else {
|
---|
153 | printf("Clicked 'Cancel' button\n");
|
---|
154 | rc = ui_label_set_text(demo->label, "Cancelled");
|
---|
155 | if (rc != EOK)
|
---|
156 | printf("Error changing label text.\n");
|
---|
157 | (void) ui_label_paint(demo->label);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Window decoration requested window closure.
|
---|
162 | *
|
---|
163 | * @param wdecor Window decoration
|
---|
164 | * @param arg Argument (demo)
|
---|
165 | */
|
---|
166 | static void wd_close(ui_wdecor_t *wdecor, void *arg)
|
---|
167 | {
|
---|
168 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
169 |
|
---|
170 | printf("Close window requested\n");
|
---|
171 | demo->quit = true;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Window decoration requested window move.
|
---|
175 | *
|
---|
176 | * @param wdecor Window decoration
|
---|
177 | * @param arg Argument (demo)
|
---|
178 | * @param pos Position where the title bar was pressed
|
---|
179 | */
|
---|
180 | static void wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
|
---|
181 | {
|
---|
182 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
183 |
|
---|
184 | (void) display_window_move_req(demo->dwindow, pos);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Run UI demo on display server. */
|
---|
188 | static errno_t ui_demo_display(const char *display_svc)
|
---|
189 | {
|
---|
190 | display_t *display = NULL;
|
---|
191 | gfx_context_t *gc;
|
---|
192 | display_wnd_params_t params;
|
---|
193 | display_window_t *window = NULL;
|
---|
194 | ui_resource_t *ui_res;
|
---|
195 | ui_demo_t demo;
|
---|
196 | gfx_rect_t rect;
|
---|
197 | gfx_color_t *color = NULL;
|
---|
198 | errno_t rc;
|
---|
199 |
|
---|
200 | printf("Init display..\n");
|
---|
201 |
|
---|
202 | rc = display_open(display_svc, &display);
|
---|
203 | if (rc != EOK) {
|
---|
204 | printf("Error opening display.\n");
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | display_wnd_params_init(¶ms);
|
---|
209 | params.rect.p0.x = 0;
|
---|
210 | params.rect.p0.y = 0;
|
---|
211 | params.rect.p1.x = 220;
|
---|
212 | params.rect.p1.y = 100;
|
---|
213 |
|
---|
214 | memset((void *) &demo, 0, sizeof(demo));
|
---|
215 |
|
---|
216 | rc = display_window_create(display, ¶ms, &wnd_cb, (void *) &demo,
|
---|
217 | &window);
|
---|
218 | if (rc != EOK) {
|
---|
219 | printf("Error creating window.\n");
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | demo.quit = false;
|
---|
224 | demo.dwindow = window;
|
---|
225 |
|
---|
226 | rc = display_window_get_gc(window, &gc);
|
---|
227 | if (rc != EOK) {
|
---|
228 | printf("Error getting graphics context.\n");
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 | task_retval(0);
|
---|
233 |
|
---|
234 | rc = ui_resource_create(gc, &ui_res);
|
---|
235 | if (rc != EOK) {
|
---|
236 | printf("Error creating UI.\n");
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 | printf("Create window decoration\n");
|
---|
241 | rc = ui_wdecor_create(ui_res, "UI Demo", &demo.wdecor);
|
---|
242 | if (rc != EOK) {
|
---|
243 | printf("Error creating window decoration.\n");
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 |
|
---|
247 | ui_wdecor_set_rect(demo.wdecor, ¶ms.rect);
|
---|
248 | ui_wdecor_set_cb(demo.wdecor, &wdecor_cb, (void *) &demo);
|
---|
249 |
|
---|
250 | rc = ui_label_create(ui_res, "Hello there!", &demo.label);
|
---|
251 | if (rc != EOK) {
|
---|
252 | printf("Error creating label.\n");
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 | rect.p0.x = 60;
|
---|
257 | rect.p0.y = 37;
|
---|
258 | rect.p1.x = 160;
|
---|
259 | rect.p1.y = 50;
|
---|
260 | ui_label_set_rect(demo.label, &rect);
|
---|
261 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
262 |
|
---|
263 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
264 | if (rc != EOK) {
|
---|
265 | printf("Error creating button.\n");
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 |
|
---|
269 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
270 |
|
---|
271 | rect.p0.x = 15;
|
---|
272 | rect.p0.y = 60;
|
---|
273 | rect.p1.x = 105;
|
---|
274 | rect.p1.y = 88;
|
---|
275 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
276 |
|
---|
277 | ui_pbutton_set_default(demo.pb1, true);
|
---|
278 |
|
---|
279 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
280 | if (rc != EOK) {
|
---|
281 | printf("Error creating button.\n");
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|
285 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
286 |
|
---|
287 | rect.p0.x = 115;
|
---|
288 | rect.p0.y = 60;
|
---|
289 | rect.p1.x = 205;
|
---|
290 | rect.p1.y = 88;
|
---|
291 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
292 |
|
---|
293 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
294 | if (rc != EOK) {
|
---|
295 | printf("Error allocating color.\n");
|
---|
296 | return rc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | rc = gfx_set_color(gc, color);
|
---|
300 | if (rc != EOK) {
|
---|
301 | printf("Error setting color.\n");
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | rc = gfx_fill_rect(gc, ¶ms.rect);
|
---|
306 | if (rc != EOK) {
|
---|
307 | printf("Error filling background.\n");
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 | gfx_color_delete(color);
|
---|
312 | color = NULL;
|
---|
313 |
|
---|
314 | rc = ui_wdecor_paint(demo.wdecor);
|
---|
315 | if (rc != EOK) {
|
---|
316 | printf("Error painting window decoration.\n");
|
---|
317 | return rc;
|
---|
318 | }
|
---|
319 |
|
---|
320 | rc = ui_label_paint(demo.label);
|
---|
321 | if (rc != EOK) {
|
---|
322 | printf("Error painting button.\n");
|
---|
323 | return rc;
|
---|
324 | }
|
---|
325 |
|
---|
326 | rc = ui_pbutton_paint(demo.pb1);
|
---|
327 | if (rc != EOK) {
|
---|
328 | printf("Error painting button.\n");
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 | rc = ui_pbutton_paint(demo.pb2);
|
---|
333 | if (rc != EOK) {
|
---|
334 | printf("Error painting button.\n");
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 | while (!demo.quit) {
|
---|
339 | fibril_usleep(100 * 1000);
|
---|
340 | }
|
---|
341 |
|
---|
342 | ui_wdecor_destroy(demo.wdecor);
|
---|
343 | ui_pbutton_destroy(demo.pb1);
|
---|
344 | ui_pbutton_destroy(demo.pb2);
|
---|
345 |
|
---|
346 | rc = gfx_context_delete(gc);
|
---|
347 | if (rc != EOK)
|
---|
348 | return rc;
|
---|
349 |
|
---|
350 | display_window_destroy(window);
|
---|
351 | display_close(display);
|
---|
352 |
|
---|
353 | return EOK;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int main(int argc, char *argv[])
|
---|
357 | {
|
---|
358 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
359 | errno_t rc;
|
---|
360 | int i;
|
---|
361 |
|
---|
362 | i = 1;
|
---|
363 | while (i < argc && argv[i][0] == '-') {
|
---|
364 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
365 | ++i;
|
---|
366 | if (i >= argc) {
|
---|
367 | printf("Argument missing.\n");
|
---|
368 | print_syntax();
|
---|
369 | return 1;
|
---|
370 | }
|
---|
371 |
|
---|
372 | display_svc = argv[i++];
|
---|
373 | } else {
|
---|
374 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
375 | print_syntax();
|
---|
376 | return 1;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | if (i < argc) {
|
---|
381 | print_syntax();
|
---|
382 | return 1;
|
---|
383 | }
|
---|
384 |
|
---|
385 | rc = ui_demo_display(display_svc);
|
---|
386 | if (rc != EOK)
|
---|
387 | return 1;
|
---|
388 |
|
---|
389 | return 0;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /** @}
|
---|
393 | */
|
---|