Changes in uspace/app/gfxdemo/gfxdemo.c [266ec54:d0dfbba] in mainline
- File:
-
- 1 edited
-
uspace/app/gfxdemo/gfxdemo.c (modified) (41 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
r266ec54 rd0dfbba 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2026 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 36 36 #include <display.h> 37 37 #include <fibril.h> 38 #include <fibril_synch.h> 38 39 #include <gfx/bitmap.h> 39 40 #include <gfx/color.h> … … 51 52 #include <ui/window.h> 52 53 #include <ui/wdecor.h> 54 #include "gfxdemo.h" 53 55 54 56 static void wnd_close_event(void *); … … 60 62 }; 61 63 64 static void uiwnd_resize_event(ui_window_t *, void *); 62 65 static void uiwnd_close_event(ui_window_t *, void *); 63 66 static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 64 67 65 68 static ui_window_cb_t ui_window_cb = { 69 .resize = uiwnd_resize_event, 66 70 .close = uiwnd_close_event, 67 71 .kbd = uiwnd_kbd_event 68 72 }; 69 73 74 static void demo_kbd_event(kbd_event_t *); 75 70 76 static bool quit = false; 77 static FIBRIL_MUTEX_INITIALIZE(quit_lock); 78 static FIBRIL_CONDVAR_INITIALIZE(quit_cv); 79 static gfx_typeface_t *tface; 80 static gfx_font_t *font; 81 static gfx_coord_t vpad; 82 static console_ctrl_t *con = NULL; 83 static bool textmode; 84 static console_gc_t *cgc = NULL; 85 static unsigned scr_width, scr_height; 86 static ui_t *ui; 87 88 /** Determine if we are running in text mode. 89 * 90 * @return @c true iff we are running in text mode 91 */ 92 static bool demo_is_text(void) 93 { 94 return textmode; 95 } 96 97 /** Sleep until timeout or quit request. 98 * 99 * @param msec Number of microseconds to sleep for 100 */ 101 static void demo_msleep(unsigned msec) 102 { 103 errno_t rc; 104 usec_t usec; 105 cons_event_t cevent; 106 sysarg_t cols, rows; 107 108 if (ui != NULL) 109 ui_unlock(ui); 110 fibril_mutex_lock(&quit_lock); 111 if (!quit) { 112 if (con != NULL) { 113 usec = (usec_t)msec * 1000; 114 while (usec > 0 && !quit) { 115 rc = console_get_event_timeout(con, &cevent, &usec); 116 if (rc == EOK) { 117 if (cevent.type == CEV_KEY) { 118 fibril_mutex_unlock(&quit_lock); 119 demo_kbd_event(&cevent.ev.key); 120 fibril_mutex_lock(&quit_lock); 121 } else if (cevent.type == CEV_RESIZE) { 122 rc = console_get_size(con, 123 &cols, &rows); 124 if (rc == EOK) { 125 scr_width = cols; 126 scr_height = rows; 127 } 128 rc = console_gc_resize(cgc); 129 if (rc != EOK) 130 exit(1); 131 } 132 } 133 } 134 } else { 135 (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock, 136 (usec_t)msec * 1000); 137 } 138 } 139 fibril_mutex_unlock(&quit_lock); 140 if (ui != NULL) 141 ui_lock(ui); 142 } 71 143 72 144 /** Clear screen. … … 107 179 } 108 180 109 /** Run rectangle demo on a graphic context.181 /** Initialize demo font. 110 182 * 111 183 * @param gc Graphic context 112 184 * @param w Width 113 185 * @param h Height 186 * @return EOK on success or an error code 187 */ 188 static errno_t demo_font_init(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 189 { 190 gfx_font_info_t *finfo; 191 errno_t rc; 192 193 if (quit) 194 return EOK; 195 196 /* XXX Crude way of detecting text mode */ 197 if (demo_is_text()) { 198 /* Create dummy font for text mode */ 199 rc = gfx_typeface_create(gc, &tface); 200 if (rc != EOK) { 201 printf("Error creating typeface\n"); 202 goto error; 203 } 204 205 rc = gfx_font_create_textmode(tface, &font); 206 if (rc != EOK) { 207 printf("Error creating font\n"); 208 goto error; 209 } 210 211 vpad = 0; 212 } else { 213 /* Load font */ 214 rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface); 215 if (rc != EOK) { 216 printf("Error opening typeface\n"); 217 goto error; 218 } 219 220 finfo = gfx_typeface_first_font(tface); 221 if (finfo == NULL) { 222 printf("Typeface contains no font.\n"); 223 rc = ENOENT; 224 goto error; 225 } 226 227 rc = gfx_font_open(finfo, &font); 228 if (rc != EOK) { 229 printf("Error opening font.\n"); 230 goto error; 231 } 232 233 vpad = 5; 234 } 235 236 return EOK; 237 error: 238 if (tface != NULL) 239 gfx_typeface_destroy(tface); 240 return rc; 241 } 242 243 /** Finalize demo font. */ 244 static void demo_font_fini(void) 245 { 246 if (font == NULL) 247 return; 248 249 gfx_font_close(font); 250 font = NULL; 251 252 gfx_typeface_destroy(tface); 253 tface = NULL; 254 } 255 256 /** Start a new demo screen. 257 * 258 * Clear the screen, display a status line and set up clipping. 259 * 260 * @param gc Graphic context 261 * @param w Width 262 * @param h Height 263 * @param text Demo screen description 264 * @return EOK on success or an error code 265 */ 266 static errno_t demo_begin(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h, 267 const char *text) 268 { 269 gfx_text_fmt_t fmt; 270 gfx_font_metrics_t metrics; 271 gfx_coord2_t pos; 272 gfx_color_t *color; 273 gfx_rect_t rect; 274 gfx_coord_t height; 275 errno_t rc; 276 277 rc = gfx_set_clip_rect(gc, NULL); 278 if (rc != EOK) 279 return rc; 280 281 rc = clear_scr(gc, w, h); 282 if (rc != EOK) 283 return rc; 284 285 if (font != NULL) { 286 if (demo_is_text()) { 287 rc = gfx_color_new_ega(0x1e, &color); 288 if (rc != EOK) 289 goto error; 290 } else { 291 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 292 if (rc != EOK) 293 goto error; 294 } 295 296 gfx_text_fmt_init(&fmt); 297 fmt.font = font; 298 fmt.color = color; 299 fmt.halign = gfx_halign_center; 300 fmt.valign = gfx_valign_bottom; 301 302 pos.x = w / 2; 303 pos.y = h; 304 rc = gfx_puttext(&pos, &fmt, text); 305 if (rc != EOK) { 306 printf("Error rendering text.\n"); 307 gfx_color_delete(color); 308 goto error; 309 } 310 311 gfx_color_delete(color); 312 313 gfx_font_get_metrics(font, &metrics); 314 height = metrics.ascent + metrics.descent + 1; 315 } else { 316 height = 0; 317 } 318 319 rect.p0.x = 0; 320 rect.p0.y = 0; 321 rect.p1.x = w; 322 rect.p1.y = h - height - vpad; 323 rc = gfx_set_clip_rect(gc, &rect); 324 if (rc != EOK) 325 return rc; 326 327 return EOK; 328 error: 329 return rc; 330 } 331 332 /** Run rectangle demo on a graphic context. 333 * 334 * @param gc Graphic context 335 * @param w Width 336 * @param h Height 114 337 */ 115 338 static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) … … 123 346 return EOK; 124 347 125 rc = clear_scr(gc, w, h);348 rc = demo_begin(gc, w, h, "Rectangle rendering"); 126 349 if (rc != EOK) 127 350 return rc; … … 150 373 gfx_color_delete(color); 151 374 152 fibril_usleep(500 * 1000); 153 375 demo_msleep(500); 154 376 if (quit) 155 377 break; … … 257 479 pixelmap_put_pixel(&pixelmap, i, j, 258 480 k < w * w / 2 ? PIXEL(255, 0, 255, 0) : 259 PIXEL( 0, 255, 0, 255));481 PIXEL(255, 255, 0, 255)); 260 482 } 261 483 } … … 282 504 return EOK; 283 505 284 rc = clear_scr(gc, w, h);506 rc = demo_begin(gc, w, h, "Bitmap rendering without offset"); 285 507 if (rc != EOK) 286 508 return rc; … … 312 534 if (rc != EOK) 313 535 goto error; 314 fibril_usleep(250 * 1000); 315 536 537 demo_msleep(250); 316 538 if (quit) 317 539 goto out; … … 345 567 return EOK; 346 568 347 rc = clear_scr(gc, w, h);569 rc = demo_begin(gc, w, h, "Bitmap rendering with offset"); 348 570 if (rc != EOK) 349 571 return rc; … … 373 595 } 374 596 375 fibril_usleep(500 * 1000); 376 597 demo_msleep(500); 377 598 if (quit) 378 599 break; … … 404 625 return EOK; 405 626 406 rc = clear_scr(gc, w, h);627 rc = demo_begin(gc, w, h, "Bitmap rendering with color key"); 407 628 if (rc != EOK) 408 629 return rc; … … 414 635 params.rect.p1.y = 40; 415 636 params.flags = bmpf_color_key; 416 params.key_color = PIXEL( 0, 255, 0, 255);637 params.key_color = PIXEL(255, 255, 0, 255); 417 638 418 639 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 434 655 } 435 656 436 fibril_usleep(500 * 1000); 437 657 demo_msleep(500); 438 658 if (quit) 439 659 break; … … 458 678 gfx_color_t *color = NULL; 459 679 gfx_rect_t rect; 460 gfx_typeface_t *tface = NULL;461 gfx_font_info_t *finfo;462 gfx_font_t *font = NULL;463 680 gfx_coord2_t pos; 464 681 gfx_text_fmt_t fmt; … … 469 686 return EOK; 470 687 471 rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface); 472 if (rc != EOK) { 473 printf("Error opening typeface\n"); 474 goto error; 475 } 476 477 finfo = gfx_typeface_first_font(tface); 478 if (finfo == NULL) { 479 printf("Typeface contains no font.\n"); 480 rc = ENOENT; 481 goto error; 482 } 483 484 rc = gfx_font_open(finfo, &font); 485 if (rc != EOK) { 486 printf("Error opening font.\n"); 487 goto error; 488 } 489 490 rc = clear_scr(gc, w, h); 688 rc = demo_begin(gc, w, h, "Text rendering"); 491 689 if (rc != EOK) 492 690 goto error; … … 525 723 526 724 rect.p0.x = w / 20; 527 rect.p0.y = 2* h / 15;725 rect.p0.y = 1 * h / 15; 528 726 rect.p1.x = w - w / 20; 529 rect.p1.y = 5* h / 15;727 rect.p1.y = 4 * h / 15; 530 728 531 729 rc = gfx_fill_rect(gc, &rect); … … 535 733 gfx_color_delete(color); 536 734 537 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 538 if (rc != EOK) 539 goto error; 540 541 rc = gfx_set_color(gc, color); 542 if (rc != EOK) 543 goto error; 735 if (demo_is_text()) { 736 rc = gfx_color_new_ega(0x1f, &color); 737 if (rc != EOK) 738 goto error; 739 } else { 740 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 741 if (rc != EOK) 742 goto error; 743 } 544 744 545 745 gfx_text_fmt_init(&fmt); 746 fmt.font = font; 747 fmt.color = color; 546 748 547 749 pos.x = rect.p0.x; 548 750 pos.y = rect.p0.y; 549 rc = gfx_puttext( font,&pos, &fmt, "Top left");751 rc = gfx_puttext(&pos, &fmt, "Top left"); 550 752 if (rc != EOK) { 551 753 printf("Error rendering text.\n"); … … 556 758 pos.y = rect.p0.y; 557 759 fmt.halign = gfx_halign_center; 558 rc = gfx_puttext( font,&pos, &fmt, "Top center");559 if (rc != EOK) 560 goto error; 561 562 pos.x = rect.p1.x - 1;760 rc = gfx_puttext(&pos, &fmt, "Top center"); 761 if (rc != EOK) 762 goto error; 763 764 pos.x = rect.p1.x; 563 765 pos.y = rect.p0.y; 564 766 fmt.halign = gfx_halign_right; 565 rc = gfx_puttext( font,&pos, &fmt, "Top right");767 rc = gfx_puttext(&pos, &fmt, "Top right"); 566 768 if (rc != EOK) 567 769 goto error; … … 572 774 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 573 775 fmt.halign = gfx_halign_left; 574 rc = gfx_puttext( font,&pos, &fmt, "Center left");776 rc = gfx_puttext(&pos, &fmt, "Center left"); 575 777 if (rc != EOK) 576 778 goto error; … … 579 781 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 580 782 fmt.halign = gfx_halign_center; 581 rc = gfx_puttext( font,&pos, &fmt, "Center");582 if (rc != EOK) 583 goto error; 584 585 pos.x = rect.p1.x - 1;783 rc = gfx_puttext(&pos, &fmt, "Center"); 784 if (rc != EOK) 785 goto error; 786 787 pos.x = rect.p1.x; 586 788 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 587 789 fmt.halign = gfx_halign_right; 588 rc = gfx_puttext( font,&pos, &fmt, "Center right");790 rc = gfx_puttext(&pos, &fmt, "Center right"); 589 791 if (rc != EOK) 590 792 goto error; … … 595 797 pos.y = rect.p1.y - 1; 596 798 fmt.halign = gfx_halign_left; 597 rc = gfx_puttext( font,&pos, &fmt, "Bottom left");799 rc = gfx_puttext(&pos, &fmt, "Bottom left"); 598 800 if (rc != EOK) 599 801 goto error; 600 802 601 803 pos.x = (rect.p0.x + rect.p1.x - 1) / 2; 602 pos.y = rect.p1.y - 1;804 pos.y = rect.p1.y; 603 805 fmt.halign = gfx_halign_center; 604 rc = gfx_puttext( font,&pos, &fmt, "Bottom center");605 if (rc != EOK) 606 goto error; 607 608 pos.x = rect.p1.x - 1;609 pos.y = rect.p1.y - 1;806 rc = gfx_puttext(&pos, &fmt, "Bottom center"); 807 if (rc != EOK) 808 goto error; 809 810 pos.x = rect.p1.x; 811 pos.y = rect.p1.y; 610 812 fmt.halign = gfx_halign_right; 611 rc = gfx_puttext( font,&pos, &fmt, "Bottom right");813 rc = gfx_puttext(&pos, &fmt, "Bottom right"); 612 814 if (rc != EOK) 613 815 goto error; … … 616 818 617 819 gfx_text_fmt_init(&fmt); 820 fmt.font = font; 618 821 619 822 for (i = 0; i < 8; i++) { 620 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0, 621 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color); 622 if (rc != EOK) 623 goto error; 624 625 rc = gfx_set_color(gc, color); 626 if (rc != EOK) 627 goto error; 823 if (demo_is_text()) { 824 rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color); 825 if (rc != EOK) 826 goto error; 827 } else { 828 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0, 829 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color); 830 if (rc != EOK) 831 goto error; 832 } 833 834 fmt.color = color; 835 fmt.underline = !fmt.underline; 628 836 629 837 pos.x = w / 20; 630 pos.y = ( 7+ i) * h / 15;631 rc = gfx_puttext( font,&pos, &fmt, "The quick brown fox jumps over the lazy dog.");838 pos.y = (6 + i) * h / 15; 839 rc = gfx_puttext(&pos, &fmt, "The quick brown fox jumps over the lazy dog."); 632 840 if (rc != EOK) 633 841 goto error; … … 637 845 638 846 for (i = 0; i < 10; i++) { 639 fibril_usleep(500 * 1000);847 demo_msleep(500); 640 848 if (quit) 641 849 break; 642 850 } 643 851 644 gfx_font_close(font);645 gfx_typeface_destroy(tface);646 852 return EOK; 647 853 error: 648 if (font != NULL)649 gfx_font_close(font);650 if (tface != NULL)651 gfx_typeface_destroy(tface);652 854 return rc; 653 855 } 654 856 655 /** Run demo loopon a graphic context.857 /** Run text abbreviation demo on a graphic context. 656 858 * 657 859 * @param gc Graphic context … … 659 861 * @param h Height 660 862 */ 661 static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 662 { 663 errno_t rc; 863 static errno_t demo_text_abbr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 864 { 865 gfx_color_t *color = NULL; 866 gfx_rect_t rect; 867 gfx_coord2_t pos; 868 gfx_text_fmt_t fmt; 869 int i; 870 errno_t rc; 871 872 if (quit) 873 return EOK; 874 875 rc = demo_begin(gc, w, h, "Text abbreviation"); 876 if (rc != EOK) 877 goto error; 878 879 for (i = 0; i < 11; i++) { 880 881 rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color); 882 if (rc != EOK) 883 goto error; 884 885 rc = gfx_set_color(gc, color); 886 if (rc != EOK) 887 goto error; 888 889 rect.p0.x = w / 20; 890 rect.p0.y = (2 + 2 * i) * h / 25; 891 rect.p1.x = w - w / 20 - w * i / 12; 892 rect.p1.y = (3 + 2 * i) * h / 25; 893 894 rc = gfx_fill_rect(gc, &rect); 895 if (rc != EOK) 896 goto error; 897 898 gfx_color_delete(color); 899 900 if (demo_is_text()) { 901 rc = gfx_color_new_ega(0x1f, &color); 902 if (rc != EOK) 903 goto error; 904 } else { 905 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, 906 &color); 907 if (rc != EOK) 908 goto error; 909 } 910 911 gfx_text_fmt_init(&fmt); 912 fmt.font = font; 913 fmt.color = color; 914 fmt.abbreviate = true; 915 fmt.width = rect.p1.x - rect.p0.x; 916 917 pos.x = rect.p0.x; 918 pos.y = rect.p0.y; 919 rc = gfx_puttext(&pos, &fmt, 920 "The quick brow fox jumps over the lazy dog!"); 921 if (rc != EOK) { 922 printf("Error rendering text.\n"); 923 goto error; 924 } 925 } 926 927 for (i = 0; i < 10; i++) { 928 demo_msleep(500); 929 if (quit) 930 break; 931 } 932 933 return EOK; 934 error: 935 return rc; 936 } 937 938 /** Run clipping demo on a graphic context. 939 * 940 * @param gc Graphic context 941 * @param w Width 942 * @param h Height 943 */ 944 static errno_t demo_clip(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 945 { 946 gfx_bitmap_t *bitmap; 947 gfx_color_t *color; 948 gfx_bitmap_params_t params; 949 int i, j; 950 gfx_coord2_t offs; 951 gfx_rect_t rect; 952 errno_t rc; 953 954 if (quit) 955 return EOK; 956 957 rc = demo_begin(gc, w, h, "Clipping demonstration"); 958 if (rc != EOK) 959 return rc; 960 961 gfx_bitmap_params_init(¶ms); 962 params.rect.p0.x = 0; 963 params.rect.p0.y = 0; 964 params.rect.p1.x = 40; 965 params.rect.p1.y = 20; 966 967 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); 968 if (rc != EOK) 969 return rc; 970 971 rc = bitmap_moire(bitmap, 40, 20); 972 if (rc != EOK) 973 goto error; 974 975 for (j = 0; j < 10; j++) { 976 rect.p0.x = w / 8; 977 rect.p0.y = h / 8; 978 rect.p1.x = w * 7 / 8; 979 rect.p1.y = h * 3 / 8; 980 981 rc = gfx_set_clip_rect(gc, &rect); 982 if (rc != EOK) 983 return rc; 984 985 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000, 986 rand() % 0x10000, &color); 987 if (rc != EOK) 988 return rc; 989 990 rc = gfx_set_color(gc, color); 991 if (rc != EOK) 992 return rc; 993 994 for (i = 0; i < 10; i++) { 995 rect.p0.x = rand() % (w - 1); 996 rect.p0.y = rand() % (h - 1); 997 rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x); 998 rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y); 999 1000 rc = gfx_fill_rect(gc, &rect); 1001 if (rc != EOK) 1002 return rc; 1003 } 1004 1005 gfx_color_delete(color); 1006 1007 rect.p0.x = w / 8; 1008 rect.p0.y = h * 5 / 8; 1009 rect.p1.x = w * 7 / 8; 1010 rect.p1.y = h * 7 / 8; 1011 1012 rc = gfx_set_clip_rect(gc, &rect); 1013 if (rc != EOK) 1014 return rc; 1015 1016 for (i = 0; i < 10; i++) { 1017 offs.x = rand() % (w - 40); 1018 offs.y = rand() % (h - 20); 1019 1020 rc = gfx_bitmap_render(bitmap, NULL, &offs); 1021 if (rc != EOK) 1022 goto error; 1023 } 1024 1025 demo_msleep(500); 1026 if (quit) 1027 break; 1028 } 1029 1030 (void) gfx_set_clip_rect(gc, NULL); 1031 gfx_bitmap_destroy(bitmap); 1032 return EOK; 1033 error: 1034 (void) gfx_set_clip_rect(gc, NULL); 1035 gfx_bitmap_destroy(bitmap); 1036 return rc; 1037 } 1038 1039 /** Run demo loop on a graphic context. 1040 * 1041 * @param gc Graphic context 1042 */ 1043 static errno_t demo_loop(gfx_context_t *gc) 1044 { 1045 errno_t rc; 1046 1047 (void) demo_font_init(gc, scr_width, scr_height); 664 1048 665 1049 while (!quit) { 666 rc = demo_rects(gc, w, h); 667 if (rc != EOK) 668 return rc; 669 670 rc = demo_bitmap(gc, w, h); 671 if (rc != EOK) 672 return rc; 673 674 rc = demo_bitmap2(gc, w, h); 675 if (rc != EOK) 676 return rc; 677 678 rc = demo_bitmap_kc(gc, w, h); 679 if (rc != EOK) 680 return rc; 681 682 rc = demo_text(gc, w, h); 683 if (rc != EOK) 684 return rc; 685 } 686 687 return EOK; 1050 rc = demo_rects(gc, scr_width, scr_height); 1051 if (rc != EOK) 1052 goto error; 1053 1054 rc = demo_bitmap(gc, scr_width, scr_height); 1055 if (rc != EOK) 1056 goto error; 1057 1058 rc = demo_bitmap2(gc, scr_width, scr_height); 1059 if (rc != EOK) 1060 goto error; 1061 1062 rc = demo_bitmap_kc(gc, scr_width, scr_height); 1063 if (rc != EOK) 1064 goto error; 1065 1066 rc = demo_text(gc, scr_width, scr_height); 1067 if (rc != EOK) 1068 goto error; 1069 1070 rc = demo_text_abbr(gc, scr_width, scr_height); 1071 if (rc != EOK) 1072 goto error; 1073 1074 rc = demo_clip(gc, scr_width, scr_height); 1075 if (rc != EOK) 1076 goto error; 1077 } 1078 1079 demo_font_fini(); 1080 return EOK; 1081 error: 1082 demo_font_fini(); 1083 return rc; 688 1084 } 689 1085 … … 691 1087 static errno_t demo_console(void) 692 1088 { 693 console_ctrl_t *con = NULL;694 console_gc_t *cgc = NULL;695 1089 gfx_context_t *gc; 696 errno_t rc;697 698 printf("Init console..\n"); 1090 sysarg_t cols, rows; 1091 errno_t rc; 1092 699 1093 con = console_init(stdin, stdout); 700 1094 if (con == NULL) 701 1095 return EIO; 702 1096 703 printf("Create console GC\n"); 1097 rc = console_get_size(con, &cols, &rows); 1098 if (rc != EOK) 1099 return rc; 1100 704 1101 rc = console_gc_create(con, stdout, &cgc); 705 1102 if (rc != EOK) … … 708 1105 gc = console_gc_get_ctx(cgc); 709 1106 710 rc = demo_loop(gc, 80, 25); 1107 /* Currently console is always text. */ 1108 textmode = true; 1109 1110 scr_width = cols; 1111 scr_height = rows; 1112 1113 rc = demo_loop(gc); 711 1114 if (rc != EOK) 712 1115 return rc; … … 717 1120 718 1121 return EOK; 1122 } 1123 1124 static errno_t demo_ui_fibril(void *arg) 1125 { 1126 demo_ui_args_t *args = (demo_ui_args_t *)arg; 1127 errno_t rc; 1128 1129 ui_lock(args->ui); 1130 scr_width = args->dims.x; 1131 scr_height = args->dims.y; 1132 rc = demo_loop(args->gc); 1133 ui_unlock(args->ui); 1134 ui_quit(args->ui); 1135 return rc; 719 1136 } 720 1137 … … 722 1139 static errno_t demo_ui(const char *display_spec) 723 1140 { 724 ui_t *ui = NULL;725 1141 ui_wnd_params_t params; 726 1142 ui_window_t *window = NULL; … … 729 1145 gfx_rect_t wrect; 730 1146 gfx_coord2_t off; 731 errno_t rc; 732 733 printf("Init UI..\n"); 1147 gfx_rect_t ui_rect; 1148 gfx_coord2_t dims; 1149 demo_ui_args_t args; 1150 fid_t fid; 1151 errno_t rc; 734 1152 735 1153 rc = ui_create(display_spec, &ui); 736 1154 if (rc != EOK) { 737 1155 printf("Error initializing UI (%s)\n", display_spec); 1156 goto error; 1157 } 1158 1159 rc = ui_get_rect(ui, &ui_rect); 1160 if (rc != EOK) { 1161 printf("Error getting display size.\n"); 738 1162 goto error; 739 1163 } … … 747 1171 params.caption = "GFX Demo"; 748 1172 1173 /* Do not decorate the window in fullscreen mode */ 1174 if (ui_is_fullscreen(ui)) { 1175 params.style &= ~ui_wds_decorated; 1176 params.placement = ui_wnd_place_full_screen; 1177 } 1178 749 1179 /* 750 1180 * Compute window rectangle such that application area corresponds 751 1181 * to rect 752 1182 */ 753 ui_wdecor_rect_from_app( params.style, &rect, &wrect);1183 ui_wdecor_rect_from_app(ui, params.style, &rect, &wrect); 754 1184 off = wrect.p0; 755 1185 gfx_rect_rtranslate(&off, &wrect, ¶ms.rect); 1186 1187 gfx_rect_dims(&ui_rect, &dims); 1188 1189 /* Make sure window is not larger than the entire screen */ 1190 if (params.rect.p1.x > dims.x) 1191 params.rect.p1.x = dims.x; 1192 if (params.rect.p1.y > dims.y) 1193 params.rect.p1.y = dims.y; 756 1194 757 1195 rc = ui_window_create(ui, ¶ms, &window); … … 769 1207 } 770 1208 771 task_retval(0); 772 773 rc = demo_loop(gc, rect.p1.x, rect.p1.y); 774 if (rc != EOK) 775 goto error; 776 1209 ui_window_get_app_rect(window, &rect); 1210 gfx_rect_dims(&rect, &dims); 1211 1212 if (!ui_is_fullscreen(ui)) 1213 task_retval(0); 1214 1215 textmode = ui_is_textmode(ui); 1216 1217 args.gc = gc; 1218 args.dims = dims; 1219 args.ui = ui; 1220 1221 fid = fibril_create(demo_ui_fibril, (void *)&args); 1222 if (fid == 0) { 1223 rc = ENOMEM; 1224 goto error; 1225 } 1226 1227 fibril_add_ready(fid); 1228 1229 ui_run(ui); 777 1230 ui_window_destroy(window); 778 1231 ui_destroy(ui); … … 796 1249 errno_t rc; 797 1250 798 printf("Init display..\n");799 800 1251 rc = display_open(display_svc, &display); 801 1252 if (rc != EOK) { … … 809 1260 params.rect.p1.x = 400; 810 1261 params.rect.p1.y = 300; 1262 params.caption = "GFX Demo"; 811 1263 812 1264 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 824 1276 task_retval(0); 825 1277 826 rc = demo_loop(gc, 400, 300); 1278 /* FIXME Assuming display service is not text mode. */ 1279 textmode = false; 1280 1281 scr_width = 400; 1282 scr_height = 300; 1283 rc = demo_loop(gc); 827 1284 if (rc != EOK) 828 1285 return rc; … … 838 1295 } 839 1296 1297 static void demo_quit(void) 1298 { 1299 fibril_mutex_lock(&quit_lock); 1300 quit = true; 1301 fibril_mutex_unlock(&quit_lock); 1302 fibril_condvar_broadcast(&quit_cv); 1303 } 1304 840 1305 static void wnd_close_event(void *arg) 841 1306 { 842 printf("Close event\n"); 843 quit = true; 1307 demo_quit(); 1308 } 1309 1310 static void demo_kbd_event(kbd_event_t *event) 1311 { 1312 if (event->type == KEY_PRESS) { 1313 /* Ctrl-Q */ 1314 if ((event->mods & KM_CTRL) != 0 && 1315 (event->mods & KM_ALT) == 0 && 1316 (event->mods & KM_SHIFT) == 0 && 1317 event->key == KC_Q) { 1318 demo_quit(); 1319 } 1320 1321 /* Escape */ 1322 if ((event->mods & KM_CTRL) == 0 && 1323 (event->mods & KM_ALT) == 0 && 1324 (event->mods & KM_SHIFT) == 0 && 1325 event->key == KC_ESCAPE) { 1326 demo_quit(); 1327 } 1328 } 844 1329 } 845 1330 846 1331 static void wnd_kbd_event(void *arg, kbd_event_t *event) 847 1332 { 848 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 849 if (event->type == KEY_PRESS) 850 quit = true; 1333 (void)arg; 1334 demo_kbd_event(event); 1335 } 1336 1337 static void uiwnd_resize_event(ui_window_t *window, void *arg) 1338 { 1339 gfx_rect_t rect; 1340 gfx_coord2_t dims; 1341 1342 ui_window_get_app_rect(window, &rect); 1343 gfx_rect_dims(&rect, &dims); 1344 scr_width = dims.x; 1345 scr_height = dims.y; 851 1346 } 852 1347 853 1348 static void uiwnd_close_event(ui_window_t *window, void *arg) 854 1349 { 855 printf("Close event\n"); 856 quit = true; 1350 demo_quit(); 857 1351 } 858 1352 859 1353 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 860 1354 { 861 printf("Keyboard event type=%d key=%d\n", event->type, event->key);862 if (event->type == KEY_PRESS)863 quit = true;1355 (void)window; 1356 (void)arg; 1357 demo_kbd_event(event); 864 1358 } 865 1359 866 1360 static void print_syntax(void) 867 1361 { 868 printf("Syntax: gfxdemo [-d <display>] {c anvas|console|display}\n");1362 printf("Syntax: gfxdemo [-d <display>] {console|display|ui}\n"); 869 1363 } 870 1364 … … 873 1367 errno_t rc; 874 1368 const char *display_svc = DISPLAY_DEFAULT; 1369 const char *ui_display_spec = UI_ANY_DEFAULT; 875 1370 int i; 876 1371 … … 885 1380 } 886 1381 887 display_svc = argv[i++];1382 display_svc = ui_display_spec = argv[i++]; 888 1383 } else { 889 1384 printf("Invalid option '%s'.\n", argv[i]); … … 893 1388 } 894 1389 895 if (i >= argc || str_cmp(argv[i], " display") == 0) {896 rc = demo_ display(display_svc);1390 if (i >= argc || str_cmp(argv[i], "ui") == 0) { 1391 rc = demo_ui(ui_display_spec); 897 1392 if (rc != EOK) 898 1393 return 1; … … 901 1396 if (rc != EOK) 902 1397 return 1; 903 } else if (str_cmp(argv[i], " ui") == 0) {904 rc = demo_ ui(display_svc);1398 } else if (str_cmp(argv[i], "display") == 0) { 1399 rc = demo_display(display_svc); 905 1400 if (rc != EOK) 906 1401 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.
