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