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 "uidemo.h"
|
---|
45 |
|
---|
46 | static void wnd_close_event(void *);
|
---|
47 | static void wnd_kbd_event(void *, kbd_event_t *);
|
---|
48 | static void wnd_pos_event(void *, pos_event_t *);
|
---|
49 |
|
---|
50 | static display_wnd_cb_t wnd_cb = {
|
---|
51 | .close_event = wnd_close_event,
|
---|
52 | .kbd_event = wnd_kbd_event,
|
---|
53 | .pos_event = wnd_pos_event
|
---|
54 | };
|
---|
55 |
|
---|
56 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
57 |
|
---|
58 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
59 | .clicked = pb_clicked
|
---|
60 | };
|
---|
61 |
|
---|
62 | static bool quit = false;
|
---|
63 |
|
---|
64 | /** Print syntax. */
|
---|
65 | static void print_syntax(void)
|
---|
66 | {
|
---|
67 | printf("Syntax: uidemo [-d <display>]\n");
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Handle window close event. */
|
---|
71 | static void wnd_close_event(void *arg)
|
---|
72 | {
|
---|
73 | printf("Close event\n");
|
---|
74 | quit = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Handle window keyboard event */
|
---|
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 |
|
---|
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 |
|
---|
90 | ui_pbutton_pos_event(demo->pb1, event);
|
---|
91 | ui_pbutton_pos_event(demo->pb2, event);
|
---|
92 | }
|
---|
93 |
|
---|
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 |
|
---|
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;
|
---|
118 | ui_demo_t demo;
|
---|
119 | gfx_rect_t rect;
|
---|
120 | gfx_color_t *color = NULL;
|
---|
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 |
|
---|
137 | rc = display_window_create(display, ¶ms, &wnd_cb, (void *) &demo,
|
---|
138 | &window);
|
---|
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");
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
159 | if (rc != EOK) {
|
---|
160 | printf("Error creating button.\n");
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
165 |
|
---|
166 | rect.p0.x = 20;
|
---|
167 | rect.p0.y = 50;
|
---|
168 | rect.p1.x = 100;
|
---|
169 | rect.p1.y = 80;
|
---|
170 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
171 |
|
---|
172 | ui_pbutton_set_default(demo.pb1, true);
|
---|
173 |
|
---|
174 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
175 | if (rc != EOK) {
|
---|
176 | printf("Error creating button.\n");
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
181 |
|
---|
182 | rect.p0.x = 120;
|
---|
183 | rect.p0.y = 50;
|
---|
184 | rect.p1.x = 200;
|
---|
185 | rect.p1.y = 80;
|
---|
186 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
187 |
|
---|
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 |
|
---|
209 | rc = ui_pbutton_paint(demo.pb1);
|
---|
210 | if (rc != EOK) {
|
---|
211 | printf("Error painting button.\n");
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 | rc = ui_pbutton_paint(demo.pb2);
|
---|
216 | if (rc != EOK) {
|
---|
217 | printf("Error painting button.\n");
|
---|
218 | return rc;
|
---|
219 | }
|
---|
220 |
|
---|
221 | while (!quit) {
|
---|
222 | fibril_usleep(100 * 1000);
|
---|
223 | }
|
---|
224 |
|
---|
225 | ui_pbutton_destroy(demo.pb1);
|
---|
226 | ui_pbutton_destroy(demo.pb2);
|
---|
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 |
|
---|
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 |
|
---|
267 | rc = ui_demo_display(display_svc);
|
---|
268 | if (rc != EOK)
|
---|
269 | return 1;
|
---|
270 |
|
---|
271 | return 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** @}
|
---|
275 | */
|
---|