[f80690a] | 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>
|
---|
[c9a7adc] | 36 | #include <gfx/color.h>
|
---|
| 37 | #include <gfx/coord.h>
|
---|
| 38 | #include <gfx/render.h>
|
---|
[f80690a] | 39 | #include <stdio.h>
|
---|
| 40 | #include <str.h>
|
---|
[47728678] | 41 | #include <task.h>
|
---|
[f80690a] | 42 | #include <ui/pbutton.h>
|
---|
[47728678] | 43 | #include <ui/resource.h>
|
---|
[f6df5a3] | 44 | #include "uidemo.h"
|
---|
[47728678] | 45 |
|
---|
| 46 | static void wnd_close_event(void *);
|
---|
| 47 | static void wnd_kbd_event(void *, kbd_event_t *);
|
---|
[f6df5a3] | 48 | static void wnd_pos_event(void *, pos_event_t *);
|
---|
[47728678] | 49 |
|
---|
| 50 | static display_wnd_cb_t wnd_cb = {
|
---|
| 51 | .close_event = wnd_close_event,
|
---|
[f6df5a3] | 52 | .kbd_event = wnd_kbd_event,
|
---|
| 53 | .pos_event = wnd_pos_event
|
---|
[47728678] | 54 | };
|
---|
| 55 |
|
---|
[8ef48ece] | 56 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
| 57 |
|
---|
| 58 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
| 59 | .clicked = pb_clicked
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[47728678] | 62 | static bool quit = false;
|
---|
[f80690a] | 63 |
|
---|
[f6df5a3] | 64 | /** Print syntax. */
|
---|
[f80690a] | 65 | static void print_syntax(void)
|
---|
| 66 | {
|
---|
| 67 | printf("Syntax: uidemo [-d <display>]\n");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[f6df5a3] | 70 | /** Handle window close event. */
|
---|
[47728678] | 71 | static void wnd_close_event(void *arg)
|
---|
| 72 | {
|
---|
| 73 | printf("Close event\n");
|
---|
| 74 | quit = true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[f6df5a3] | 77 | /** Handle window keyboard event */
|
---|
[47728678] | 78 | static void wnd_kbd_event(void *arg, kbd_event_t *event)
|
---|
| 79 | {
|
---|
| 80 | printf("Keyboard event type=%d key=%d\n", event->type, event->key);
|
---|
| 81 | if (event->type == KEY_PRESS)
|
---|
| 82 | quit = true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[f6df5a3] | 85 | /** Handle window position event */
|
---|
| 86 | static void wnd_pos_event(void *arg, pos_event_t *event)
|
---|
| 87 | {
|
---|
| 88 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 89 |
|
---|
[faca61b8] | 90 | ui_pbutton_pos_event(demo->pb1, event);
|
---|
| 91 | ui_pbutton_pos_event(demo->pb2, event);
|
---|
[f6df5a3] | 92 | }
|
---|
| 93 |
|
---|
[8ef48ece] | 94 | /** Push button was clicked.
|
---|
| 95 | *
|
---|
| 96 | * @param pbutton Push button
|
---|
| 97 | * @param arg Argument (demo)
|
---|
| 98 | */
|
---|
| 99 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 100 | {
|
---|
| 101 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 102 |
|
---|
| 103 | if (pbutton == demo->pb1) {
|
---|
| 104 | printf("Clicked 'Confirm' button\n");
|
---|
| 105 | } else {
|
---|
| 106 | printf("Clicked 'Cancel' button\n");
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[47728678] | 110 | /** Run UI demo on display server. */
|
---|
| 111 | static errno_t ui_demo_display(const char *display_svc)
|
---|
| 112 | {
|
---|
| 113 | display_t *display = NULL;
|
---|
| 114 | gfx_context_t *gc;
|
---|
| 115 | display_wnd_params_t params;
|
---|
| 116 | display_window_t *window = NULL;
|
---|
| 117 | ui_resource_t *ui_res;
|
---|
[f6df5a3] | 118 | ui_demo_t demo;
|
---|
[47728678] | 119 | gfx_rect_t rect;
|
---|
[c9a7adc] | 120 | gfx_color_t *color = NULL;
|
---|
[47728678] | 121 | errno_t rc;
|
---|
| 122 |
|
---|
| 123 | printf("Init display..\n");
|
---|
| 124 |
|
---|
| 125 | rc = display_open(display_svc, &display);
|
---|
| 126 | if (rc != EOK) {
|
---|
| 127 | printf("Error opening display.\n");
|
---|
| 128 | return rc;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | display_wnd_params_init(¶ms);
|
---|
| 132 | params.rect.p0.x = 0;
|
---|
| 133 | params.rect.p0.y = 0;
|
---|
| 134 | params.rect.p1.x = 220;
|
---|
| 135 | params.rect.p1.y = 100;
|
---|
| 136 |
|
---|
[f6df5a3] | 137 | rc = display_window_create(display, ¶ms, &wnd_cb, (void *) &demo,
|
---|
| 138 | &window);
|
---|
[47728678] | 139 | if (rc != EOK) {
|
---|
| 140 | printf("Error creating window.\n");
|
---|
| 141 | return rc;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | rc = display_window_get_gc(window, &gc);
|
---|
| 145 | if (rc != EOK) {
|
---|
| 146 | printf("Error getting graphics context.\n");
|
---|
| 147 | return rc;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | task_retval(0);
|
---|
| 151 |
|
---|
| 152 | rc = ui_resource_create(gc, &ui_res);
|
---|
| 153 | if (rc != EOK) {
|
---|
| 154 | printf("Error creating UI.\n");
|
---|
[f6df5a3] | 155 | return rc;
|
---|
[47728678] | 156 | }
|
---|
| 157 |
|
---|
[f6df5a3] | 158 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
[47728678] | 159 | if (rc != EOK) {
|
---|
| 160 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 161 | return rc;
|
---|
[47728678] | 162 | }
|
---|
| 163 |
|
---|
[8ef48ece] | 164 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
| 165 |
|
---|
[47728678] | 166 | rect.p0.x = 20;
|
---|
| 167 | rect.p0.y = 50;
|
---|
| 168 | rect.p1.x = 100;
|
---|
| 169 | rect.p1.y = 80;
|
---|
[f6df5a3] | 170 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
[47728678] | 171 |
|
---|
[c9a7adc] | 172 | ui_pbutton_set_default(demo.pb1, true);
|
---|
| 173 |
|
---|
[f6df5a3] | 174 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
[47728678] | 175 | if (rc != EOK) {
|
---|
| 176 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 177 | return rc;
|
---|
[47728678] | 178 | }
|
---|
| 179 |
|
---|
[8ef48ece] | 180 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
| 181 |
|
---|
[47728678] | 182 | rect.p0.x = 120;
|
---|
| 183 | rect.p0.y = 50;
|
---|
| 184 | rect.p1.x = 200;
|
---|
| 185 | rect.p1.y = 80;
|
---|
[f6df5a3] | 186 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
[47728678] | 187 |
|
---|
[c9a7adc] | 188 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
| 189 | if (rc != EOK) {
|
---|
| 190 | printf("Error allocating color.\n");
|
---|
| 191 | return rc;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | rc = gfx_set_color(gc, color);
|
---|
| 195 | if (rc != EOK) {
|
---|
| 196 | printf("Error setting color.\n");
|
---|
| 197 | return rc;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | rc = gfx_fill_rect(gc, ¶ms.rect);
|
---|
| 201 | if (rc != EOK) {
|
---|
| 202 | printf("Error filling background.\n");
|
---|
| 203 | return rc;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | gfx_color_delete(color);
|
---|
| 207 | color = NULL;
|
---|
| 208 |
|
---|
[f6df5a3] | 209 | rc = ui_pbutton_paint(demo.pb1);
|
---|
[47728678] | 210 | if (rc != EOK) {
|
---|
| 211 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 212 | return rc;
|
---|
[47728678] | 213 | }
|
---|
| 214 |
|
---|
[f6df5a3] | 215 | rc = ui_pbutton_paint(demo.pb2);
|
---|
[47728678] | 216 | if (rc != EOK) {
|
---|
| 217 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 218 | return rc;
|
---|
[47728678] | 219 | }
|
---|
| 220 |
|
---|
| 221 | while (!quit) {
|
---|
| 222 | fibril_usleep(100 * 1000);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[f6df5a3] | 225 | ui_pbutton_destroy(demo.pb1);
|
---|
| 226 | ui_pbutton_destroy(demo.pb2);
|
---|
[47728678] | 227 |
|
---|
| 228 | rc = gfx_context_delete(gc);
|
---|
| 229 | if (rc != EOK)
|
---|
| 230 | return rc;
|
---|
| 231 |
|
---|
| 232 | display_window_destroy(window);
|
---|
| 233 | display_close(display);
|
---|
| 234 |
|
---|
| 235 | return EOK;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[f80690a] | 238 | int main(int argc, char *argv[])
|
---|
| 239 | {
|
---|
| 240 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
| 241 | errno_t rc;
|
---|
| 242 | int i;
|
---|
| 243 |
|
---|
| 244 | i = 1;
|
---|
| 245 | while (i < argc && argv[i][0] == '-') {
|
---|
| 246 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 247 | ++i;
|
---|
| 248 | if (i >= argc) {
|
---|
| 249 | printf("Argument missing.\n");
|
---|
| 250 | print_syntax();
|
---|
| 251 | return 1;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | display_svc = argv[i++];
|
---|
| 255 | } else {
|
---|
| 256 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 257 | print_syntax();
|
---|
| 258 | return 1;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | if (i < argc) {
|
---|
| 263 | print_syntax();
|
---|
| 264 | return 1;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[47728678] | 267 | rc = ui_demo_display(display_svc);
|
---|
| 268 | if (rc != EOK)
|
---|
[f80690a] | 269 | return 1;
|
---|
| 270 |
|
---|
| 271 | return 0;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /** @}
|
---|
| 275 | */
|
---|