source: mainline/uspace/app/gfxdemo/gfxdemo.c@ dcfd422

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dcfd422 was 0d62c10, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Rendering text in different colors via colorization

  • Property mode set to 100644
File size: 17.3 KB
Line 
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 gfxdemo
30 * @{
31 */
32/** @file Graphic demo
33 */
34
35#include <canvas.h>
36#include <congfx/console.h>
37#include <draw/surface.h>
38#include <display.h>
39#include <fibril.h>
40#include <guigfx/canvas.h>
41#include <gfx/bitmap.h>
42#include <gfx/color.h>
43#include <gfx/render.h>
44#include <gfx/font.h>
45#include <gfx/text.h>
46#include <gfx/typeface.h>
47#include <io/console.h>
48#include <io/pixelmap.h>
49#include <stdbool.h>
50#include <stdlib.h>
51#include <str.h>
52#include <task.h>
53#include <window.h>
54
55static void wnd_close_event(void *);
56static void wnd_kbd_event(void *, kbd_event_t *);
57
58static display_wnd_cb_t wnd_cb = {
59 .close_event = wnd_close_event,
60 .kbd_event = wnd_kbd_event
61};
62
63static bool quit = false;
64
65/** Clear screen.
66 *
67 * @param gc Graphic context
68 * @param w Screen width
69 * @param h Screen height
70 */
71static errno_t clear_scr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
72{
73 gfx_color_t *color = NULL;
74 gfx_rect_t rect;
75 errno_t rc;
76
77 rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
78 if (rc != EOK)
79 goto error;
80
81 rc = gfx_set_color(gc, color);
82 if (rc != EOK)
83 goto error;
84
85 rect.p0.x = 0;
86 rect.p0.y = 0;
87 rect.p1.x = w;
88 rect.p1.y = h;
89
90 rc = gfx_fill_rect(gc, &rect);
91 if (rc != EOK)
92 goto error;
93
94 gfx_color_delete(color);
95 return EOK;
96error:
97 if (color != NULL)
98 gfx_color_delete(color);
99 return rc;
100}
101
102/** Run rectangle demo on a graphic context.
103 *
104 * @param gc Graphic context
105 * @param w Width
106 * @param h Height
107 */
108static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
109{
110 gfx_color_t *color = NULL;
111 gfx_rect_t rect;
112 int i, j;
113 errno_t rc;
114
115 rc = clear_scr(gc, w, h);
116 if (rc != EOK)
117 return rc;
118
119 for (j = 0; j < 10; j++) {
120 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
121 rand() % 0x10000, &color);
122 if (rc != EOK)
123 return rc;
124
125 rc = gfx_set_color(gc, color);
126 if (rc != EOK)
127 return rc;
128
129 for (i = 0; i < 10; i++) {
130 rect.p0.x = rand() % (w - 1);
131 rect.p0.y = rand() % (h - 1);
132 rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
133 rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
134
135 rc = gfx_fill_rect(gc, &rect);
136 if (rc != EOK)
137 return rc;
138 }
139
140 gfx_color_delete(color);
141
142 fibril_usleep(500 * 1000);
143
144 if (quit)
145 break;
146 }
147
148 return EOK;
149}
150
151/** Fill bitmap with tartan pattern.
152 *
153 * @param bitmap Bitmap
154 * @param w Bitmap width
155 * @param h Bitmap height
156 * @return EOK on success or an error code
157 */
158static errno_t bitmap_tartan(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
159{
160 int i, j;
161 pixelmap_t pixelmap;
162 gfx_bitmap_alloc_t alloc;
163 errno_t rc;
164
165 rc = gfx_bitmap_get_alloc(bitmap, &alloc);
166 if (rc != EOK)
167 return rc;
168
169 /* In absence of anything else, use pixelmap */
170 pixelmap.width = w;
171 pixelmap.height = h;
172 pixelmap.data = alloc.pixels;
173
174 for (i = 0; i < w; i++) {
175 for (j = 0; j < h; j++) {
176 pixelmap_put_pixel(&pixelmap, i, j,
177 PIXEL(255, (i % 30) < 3 ? 255 : 0,
178 (j % 30) < 3 ? 255 : 0, i / 2));
179 }
180 }
181
182 return EOK;
183}
184
185/** Fill bitmap with moire pattern.
186 *
187 * @param bitmap Bitmap
188 * @param w Bitmap width
189 * @param h Bitmap height
190 * @return EOK on success or an error code
191 */
192static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
193{
194 int i, j;
195 int k;
196 pixelmap_t pixelmap;
197 gfx_bitmap_alloc_t alloc;
198 errno_t rc;
199
200 rc = gfx_bitmap_get_alloc(bitmap, &alloc);
201 if (rc != EOK)
202 return rc;
203
204 /* In absence of anything else, use pixelmap */
205 pixelmap.width = w;
206 pixelmap.height = h;
207 pixelmap.data = alloc.pixels;
208
209 for (i = 0; i < w; i++) {
210 for (j = 0; j < h; j++) {
211 k = i * i + j * j;
212 pixelmap_put_pixel(&pixelmap, i, j,
213 PIXEL(255, k, k, k));
214 }
215 }
216
217 return EOK;
218}
219
220/** Render circle to a bitmap.
221 *
222 * @param bitmap Bitmap
223 * @param w Bitmap width
224 * @param h Bitmap height
225 * @return EOK on success or an error code
226 */
227static errno_t bitmap_circle(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
228{
229 int i, j;
230 int k;
231 pixelmap_t pixelmap;
232 gfx_bitmap_alloc_t alloc;
233 errno_t rc;
234
235 rc = gfx_bitmap_get_alloc(bitmap, &alloc);
236 if (rc != EOK)
237 return rc;
238
239 /* In absence of anything else, use pixelmap */
240 pixelmap.width = w;
241 pixelmap.height = h;
242 pixelmap.data = alloc.pixels;
243
244 for (i = 0; i < w; i++) {
245 for (j = 0; j < h; j++) {
246 k = i * i + j * j;
247 pixelmap_put_pixel(&pixelmap, i, j,
248 k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
249 PIXEL(0, 255, 0, 255));
250 }
251 }
252
253 return EOK;
254}
255
256/** Run bitmap demo on a graphic context.
257 *
258 * @param gc Graphic context
259 * @param w Width
260 * @param h Height
261 */
262static errno_t demo_bitmap(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
263{
264 gfx_bitmap_t *bitmap;
265 gfx_bitmap_params_t params;
266 int i, j;
267 gfx_coord2_t offs;
268 gfx_rect_t srect;
269 errno_t rc;
270
271 rc = clear_scr(gc, w, h);
272 if (rc != EOK)
273 return rc;
274
275 gfx_bitmap_params_init(&params);
276 params.rect.p0.x = 0;
277 params.rect.p0.y = 0;
278 params.rect.p1.x = w;
279 params.rect.p1.y = h;
280
281 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
282 if (rc != EOK)
283 return rc;
284
285 rc = bitmap_tartan(bitmap, w, h);
286 if (rc != EOK)
287 goto error;
288
289 for (j = 0; j < 10; j++) {
290 for (i = 0; i < 5; i++) {
291 srect.p0.x = rand() % (w - 40);
292 srect.p0.y = rand() % (h - 20);
293 srect.p1.x = srect.p0.x + rand() % (w - srect.p0.x);
294 srect.p1.y = srect.p0.y + rand() % (h - srect.p0.y);
295 offs.x = 0;
296 offs.y = 0;
297
298 rc = gfx_bitmap_render(bitmap, &srect, &offs);
299 if (rc != EOK)
300 goto error;
301 fibril_usleep(250 * 1000);
302
303 if (quit)
304 break;
305 }
306 }
307
308 gfx_bitmap_destroy(bitmap);
309
310 return EOK;
311error:
312 gfx_bitmap_destroy(bitmap);
313 return rc;
314}
315
316/** Run second bitmap demo on a graphic context.
317 *
318 * @param gc Graphic context
319 * @param w Width
320 * @param h Height
321 */
322static errno_t demo_bitmap2(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
323{
324 gfx_bitmap_t *bitmap;
325 gfx_bitmap_params_t params;
326 int i, j;
327 gfx_coord2_t offs;
328 errno_t rc;
329
330 rc = clear_scr(gc, w, h);
331 if (rc != EOK)
332 return rc;
333
334 gfx_bitmap_params_init(&params);
335 params.rect.p0.x = 0;
336 params.rect.p0.y = 0;
337 params.rect.p1.x = 40;
338 params.rect.p1.y = 20;
339
340 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
341 if (rc != EOK)
342 return rc;
343
344 rc = bitmap_moire(bitmap, 40, 20);
345 if (rc != EOK)
346 goto error;
347
348 for (j = 0; j < 10; j++) {
349 for (i = 0; i < 10; i++) {
350 offs.x = rand() % (w - 40);
351 offs.y = rand() % (h - 20);
352
353 rc = gfx_bitmap_render(bitmap, NULL, &offs);
354 if (rc != EOK)
355 goto error;
356 }
357
358 fibril_usleep(500 * 1000);
359
360 if (quit)
361 break;
362 }
363
364 gfx_bitmap_destroy(bitmap);
365
366 return EOK;
367error:
368 gfx_bitmap_destroy(bitmap);
369 return rc;
370}
371
372/** Run bitmap color key demo on a graphic context.
373 *
374 * @param gc Graphic context
375 * @param w Width
376 * @param h Height
377 */
378static errno_t demo_bitmap_kc(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
379{
380 gfx_bitmap_t *bitmap;
381 gfx_bitmap_params_t params;
382 int i, j;
383 gfx_coord2_t offs;
384 errno_t rc;
385
386 rc = clear_scr(gc, w, h);
387 if (rc != EOK)
388 return rc;
389
390 gfx_bitmap_params_init(&params);
391 params.rect.p0.x = 0;
392 params.rect.p0.y = 0;
393 params.rect.p1.x = 40;
394 params.rect.p1.y = 40;
395 params.flags = bmpf_color_key;
396 params.key_color = PIXEL(0, 255, 0, 255);
397
398 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
399 if (rc != EOK)
400 return rc;
401
402 rc = bitmap_circle(bitmap, 40, 40);
403 if (rc != EOK)
404 goto error;
405
406 for (j = 0; j < 10; j++) {
407 for (i = 0; i < 10; i++) {
408 offs.x = j * 20 + i * 20;
409 offs.y = i * 20;
410
411 rc = gfx_bitmap_render(bitmap, NULL, &offs);
412 if (rc != EOK)
413 goto error;
414 }
415
416 fibril_usleep(500 * 1000);
417
418 if (quit)
419 break;
420 }
421
422 gfx_bitmap_destroy(bitmap);
423
424 return EOK;
425error:
426 gfx_bitmap_destroy(bitmap);
427 return rc;
428}
429
430/** Run text demo on a graphic context.
431 *
432 * @param gc Graphic context
433 * @param w Width
434 * @param h Height
435 */
436static errno_t demo_text(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
437{
438 gfx_color_t *color = NULL;
439 gfx_rect_t rect;
440 gfx_typeface_t *tface = NULL;
441 gfx_font_info_t *finfo;
442 gfx_font_t *font = NULL;
443 gfx_coord2_t pos;
444 gfx_text_fmt_t fmt;
445 int i;
446 errno_t rc;
447
448 rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
449 if (rc != EOK) {
450 printf("Error opening typeface\n");
451 goto error;
452 }
453
454 finfo = gfx_typeface_first_font(tface);
455 if (finfo == NULL) {
456 printf("Typeface contains no font.\n");
457 rc = ENOENT;
458 goto error;
459 }
460
461 rc = gfx_font_open(finfo, &font);
462 if (rc != EOK) {
463 printf("Error opening font.\n");
464 goto error;
465 }
466
467 rc = clear_scr(gc, w, h);
468 if (rc != EOK)
469 goto error;
470
471 /* Vertical bars */
472
473 for (i = 0; i < 20; i++) {
474 rc = gfx_color_new_rgb_i16(0, 0x8000 * i / 20,
475 0x8000 * i / 20, &color);
476 if (rc != EOK)
477 goto error;
478
479 rc = gfx_set_color(gc, color);
480 if (rc != EOK)
481 goto error;
482
483 rect.p0.x = w * i / 20;
484 rect.p0.y = 0;
485 rect.p1.x = w * (i + 1) / 20;
486 rect.p1.y = h;
487
488 rc = gfx_fill_rect(gc, &rect);
489 if (rc != EOK)
490 goto error;
491
492 gfx_color_delete(color);
493 }
494
495 rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
496 if (rc != EOK)
497 goto error;
498
499 rc = gfx_set_color(gc, color);
500 if (rc != EOK)
501 goto error;
502
503 rect.p0.x = w / 20;
504 rect.p0.y = 2 * h / 15;
505 rect.p1.x = w - w / 20;
506 rect.p1.y = 5 * h / 15;
507
508 rc = gfx_fill_rect(gc, &rect);
509 if (rc != EOK)
510 goto error;
511
512 gfx_color_delete(color);
513
514 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
515 if (rc != EOK)
516 goto error;
517
518 rc = gfx_set_color(gc, color);
519 if (rc != EOK)
520 goto error;
521
522 gfx_text_fmt_init(&fmt);
523
524 pos.x = rect.p0.x;
525 pos.y = rect.p0.y;
526 rc = gfx_puttext(font, &pos, &fmt, "Top left");
527 if (rc != EOK) {
528 printf("Error rendering text.\n");
529 goto error;
530 }
531
532 pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
533 pos.y = rect.p0.y;
534 fmt.halign = gfx_halign_center;
535 rc = gfx_puttext(font, &pos, &fmt, "Top center");
536 if (rc != EOK)
537 goto error;
538
539 pos.x = rect.p1.x - 1;
540 pos.y = rect.p0.y;
541 fmt.halign = gfx_halign_right;
542 rc = gfx_puttext(font, &pos, &fmt, "Top right");
543 if (rc != EOK)
544 goto error;
545
546 fmt.valign = gfx_valign_center;
547
548 pos.x = rect.p0.x;
549 pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
550 fmt.halign = gfx_halign_left;
551 rc = gfx_puttext(font, &pos, &fmt, "Center left");
552 if (rc != EOK)
553 goto error;
554
555 pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
556 pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
557 fmt.halign = gfx_halign_center;
558 rc = gfx_puttext(font, &pos, &fmt, "Center");
559 if (rc != EOK)
560 goto error;
561
562 pos.x = rect.p1.x - 1;
563 pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
564 fmt.halign = gfx_halign_right;
565 rc = gfx_puttext(font, &pos, &fmt, "Center right");
566 if (rc != EOK)
567 goto error;
568
569 fmt.valign = gfx_valign_bottom;
570
571 pos.x = rect.p0.x;
572 pos.y = rect.p1.y - 1;
573 fmt.halign = gfx_halign_left;
574 rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
575 if (rc != EOK)
576 goto error;
577
578 pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
579 pos.y = rect.p1.y - 1;
580 fmt.halign = gfx_halign_center;
581 rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
582 if (rc != EOK)
583 goto error;
584
585 pos.x = rect.p1.x - 1;
586 pos.y = rect.p1.y - 1;
587 fmt.halign = gfx_halign_right;
588 rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
589 if (rc != EOK)
590 goto error;
591
592 gfx_color_delete(color);
593
594 gfx_text_fmt_init(&fmt);
595
596 for (i = 0; i < 8; i++) {
597 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0,
598 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color);
599 if (rc != EOK)
600 goto error;
601
602 rc = gfx_set_color(gc, color);
603 if (rc != EOK)
604 goto error;
605
606 pos.x = w / 20;
607 pos.y = (7 + i) * h / 15;
608 rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
609 if (rc != EOK)
610 goto error;
611
612 gfx_color_delete(color);
613 }
614
615 for (i = 0; i < 10; i++) {
616 fibril_usleep(500 * 1000);
617 if (quit)
618 break;
619 }
620
621 gfx_font_close(font);
622 gfx_typeface_destroy(tface);
623 return EOK;
624error:
625 if (font != NULL)
626 gfx_font_close(font);
627 if (tface != NULL)
628 gfx_typeface_destroy(tface);
629 return rc;
630}
631
632/** Run demo loop on a graphic context.
633 *
634 * @param gc Graphic context
635 * @param w Width
636 * @param h Height
637 */
638static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
639{
640 errno_t rc;
641
642 while (!quit) {
643 rc = demo_rects(gc, w, h);
644 if (rc != EOK)
645 return rc;
646
647 rc = demo_bitmap(gc, w, h);
648 if (rc != EOK)
649 return rc;
650
651 rc = demo_bitmap2(gc, w, h);
652 if (rc != EOK)
653 return rc;
654
655 rc = demo_bitmap_kc(gc, w, h);
656 if (rc != EOK)
657 return rc;
658
659 rc = demo_text(gc, w, h);
660 if (rc != EOK)
661 return rc;
662 }
663
664 return EOK;
665}
666
667/** Run demo on console. */
668static errno_t demo_console(void)
669{
670 console_ctrl_t *con = NULL;
671 console_gc_t *cgc = NULL;
672 gfx_context_t *gc;
673 errno_t rc;
674
675 printf("Init console..\n");
676 con = console_init(stdin, stdout);
677 if (con == NULL)
678 return EIO;
679
680 printf("Create console GC\n");
681 rc = console_gc_create(con, stdout, &cgc);
682 if (rc != EOK)
683 return rc;
684
685 gc = console_gc_get_ctx(cgc);
686
687 rc = demo_loop(gc, 80, 25);
688 if (rc != EOK)
689 return rc;
690
691 rc = console_gc_delete(cgc);
692 if (rc != EOK)
693 return rc;
694
695 return EOK;
696}
697
698/** Run demo on canvas. */
699static errno_t demo_canvas(const char *display_svc)
700{
701 canvas_gc_t *cgc = NULL;
702 gfx_context_t *gc;
703 window_t *window = NULL;
704 pixel_t *pixbuf = NULL;
705 surface_t *surface = NULL;
706 canvas_t *canvas = NULL;
707 gfx_coord_t vw, vh;
708 errno_t rc;
709
710 printf("Init canvas..\n");
711
712 window = window_open(display_svc, NULL,
713 WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
714 if (window == NULL) {
715 printf("Error creating window.\n");
716 return -1;
717 }
718
719 vw = 400;
720 vh = 300;
721
722 pixbuf = calloc(vw * vh, sizeof(pixel_t));
723 if (pixbuf == NULL) {
724 printf("Error allocating memory for pixel buffer.\n");
725 return ENOMEM;
726 }
727
728 surface = surface_create(vw, vh, pixbuf, 0);
729 if (surface == NULL) {
730 printf("Error creating surface.\n");
731 return EIO;
732 }
733
734 canvas = create_canvas(window_root(window), NULL, vw, vh,
735 surface);
736 if (canvas == NULL) {
737 printf("Error creating canvas.\n");
738 return EIO;
739 }
740
741 window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
742 window_exec(window);
743
744 printf("Create canvas GC\n");
745 rc = canvas_gc_create(canvas, surface, &cgc);
746 if (rc != EOK)
747 return rc;
748
749 gc = canvas_gc_get_ctx(cgc);
750
751 task_retval(0);
752
753 rc = demo_loop(gc, 400, 300);
754 if (rc != EOK)
755 return rc;
756
757 rc = canvas_gc_delete(cgc);
758 if (rc != EOK)
759 return rc;
760
761 return EOK;
762}
763
764/** Run demo on display server. */
765static errno_t demo_display(const char *display_svc)
766{
767 display_t *display = NULL;
768 gfx_context_t *gc;
769 display_wnd_params_t params;
770 display_window_t *window = NULL;
771 errno_t rc;
772
773 printf("Init display..\n");
774
775 rc = display_open(display_svc, &display);
776 if (rc != EOK) {
777 printf("Error opening display.\n");
778 return rc;
779 }
780
781 display_wnd_params_init(&params);
782 params.rect.p0.x = 0;
783 params.rect.p0.y = 0;
784 params.rect.p1.x = 400;
785 params.rect.p1.y = 300;
786
787 rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
788 if (rc != EOK) {
789 printf("Error creating window.\n");
790 return rc;
791 }
792
793 rc = display_window_get_gc(window, &gc);
794 if (rc != EOK) {
795 printf("Error getting graphics context.\n");
796 return rc;
797 }
798
799 task_retval(0);
800
801 rc = demo_loop(gc, 400, 300);
802 if (rc != EOK)
803 return rc;
804
805 rc = gfx_context_delete(gc);
806 if (rc != EOK)
807 return rc;
808
809 display_window_destroy(window);
810 display_close(display);
811
812 return EOK;
813}
814
815static void wnd_close_event(void *arg)
816{
817 printf("Close event\n");
818 quit = true;
819}
820
821static void wnd_kbd_event(void *arg, kbd_event_t *event)
822{
823 printf("Keyboard event type=%d key=%d\n", event->type, event->key);
824 if (event->type == KEY_PRESS)
825 quit = true;
826}
827
828static void print_syntax(void)
829{
830 printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n");
831}
832
833int main(int argc, char *argv[])
834{
835 errno_t rc;
836 const char *display_svc = DISPLAY_DEFAULT;
837 int i;
838
839 i = 1;
840 while (i < argc && argv[i][0] == '-') {
841 if (str_cmp(argv[i], "-d") == 0) {
842 ++i;
843 if (i >= argc) {
844 printf("Argument missing.\n");
845 print_syntax();
846 return 1;
847 }
848
849 display_svc = argv[i++];
850 } else {
851 printf("Invalid option '%s'.\n", argv[i]);
852 print_syntax();
853 return 1;
854 }
855 }
856
857 if (i >= argc || str_cmp(argv[i], "display") == 0) {
858 rc = demo_display(display_svc);
859 if (rc != EOK)
860 return 1;
861 } else if (str_cmp(argv[i], "console") == 0) {
862 rc = demo_console();
863 if (rc != EOK)
864 return 1;
865 } else if (str_cmp(argv[i], "canvas") == 0) {
866 rc = demo_canvas(display_svc);
867 if (rc != EOK)
868 return 1;
869 } else {
870 print_syntax();
871 return 1;
872 }
873}
874
875/** @}
876 */
Note: See TracBrowser for help on using the repository browser.