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