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