Changes in uspace/app/gfxdemo/gfxdemo.c [d0dfbba:266ec54] in mainline
- File:
-
- 1 edited
-
uspace/app/gfxdemo/gfxdemo.c (modified) (41 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
rd0dfbba r266ec54 1 1 /* 2 * Copyright (c) 202 6Jiri Svoboda2 * Copyright (c) 2020 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 36 36 #include <display.h> 37 37 #include <fibril.h> 38 #include <fibril_synch.h>39 38 #include <gfx/bitmap.h> 40 39 #include <gfx/color.h> … … 52 51 #include <ui/window.h> 53 52 #include <ui/wdecor.h> 54 #include "gfxdemo.h"55 53 56 54 static void wnd_close_event(void *); … … 62 60 }; 63 61 64 static void uiwnd_resize_event(ui_window_t *, void *);65 62 static void uiwnd_close_event(ui_window_t *, void *); 66 63 static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 67 64 68 65 static ui_window_cb_t ui_window_cb = { 69 .resize = uiwnd_resize_event,70 66 .close = uiwnd_close_event, 71 67 .kbd = uiwnd_kbd_event 72 68 }; 73 69 74 static void demo_kbd_event(kbd_event_t *);75 76 70 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 mode91 */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 for100 */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 }143 71 144 72 /** Clear screen. … … 179 107 } 180 108 181 /** Initialize demo font.109 /** Run rectangle demo on a graphic context. 182 110 * 183 111 * @param gc Graphic context 184 112 * @param w Width 185 113 * @param h Height 186 * @return EOK on success or an error code187 */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 context261 * @param w Width262 * @param h Height263 * @param text Demo screen description264 * @return EOK on success or an error code265 */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 context335 * @param w Width336 * @param h Height337 114 */ 338 115 static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) … … 346 123 return EOK; 347 124 348 rc = demo_begin(gc, w, h, "Rectangle rendering");125 rc = clear_scr(gc, w, h); 349 126 if (rc != EOK) 350 127 return rc; … … 373 150 gfx_color_delete(color); 374 151 375 demo_msleep(500); 152 fibril_usleep(500 * 1000); 153 376 154 if (quit) 377 155 break; … … 479 257 pixelmap_put_pixel(&pixelmap, i, j, 480 258 k < w * w / 2 ? PIXEL(255, 0, 255, 0) : 481 PIXEL( 255, 255, 0, 255));259 PIXEL(0, 255, 0, 255)); 482 260 } 483 261 } … … 504 282 return EOK; 505 283 506 rc = demo_begin(gc, w, h, "Bitmap rendering without offset");284 rc = clear_scr(gc, w, h); 507 285 if (rc != EOK) 508 286 return rc; … … 534 312 if (rc != EOK) 535 313 goto error; 536 537 demo_msleep(250); 314 fibril_usleep(250 * 1000); 315 538 316 if (quit) 539 317 goto out; … … 567 345 return EOK; 568 346 569 rc = demo_begin(gc, w, h, "Bitmap rendering with offset");347 rc = clear_scr(gc, w, h); 570 348 if (rc != EOK) 571 349 return rc; … … 595 373 } 596 374 597 demo_msleep(500); 375 fibril_usleep(500 * 1000); 376 598 377 if (quit) 599 378 break; … … 625 404 return EOK; 626 405 627 rc = demo_begin(gc, w, h, "Bitmap rendering with color key");406 rc = clear_scr(gc, w, h); 628 407 if (rc != EOK) 629 408 return rc; … … 635 414 params.rect.p1.y = 40; 636 415 params.flags = bmpf_color_key; 637 params.key_color = PIXEL( 255, 255, 0, 255);416 params.key_color = PIXEL(0, 255, 0, 255); 638 417 639 418 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 655 434 } 656 435 657 demo_msleep(500); 436 fibril_usleep(500 * 1000); 437 658 438 if (quit) 659 439 break; … … 678 458 gfx_color_t *color = NULL; 679 459 gfx_rect_t rect; 460 gfx_typeface_t *tface = NULL; 461 gfx_font_info_t *finfo; 462 gfx_font_t *font = NULL; 680 463 gfx_coord2_t pos; 681 464 gfx_text_fmt_t fmt; … … 686 469 return EOK; 687 470 688 rc = demo_begin(gc, w, h, "Text rendering"); 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); 689 491 if (rc != EOK) 690 492 goto error; … … 723 525 724 526 rect.p0.x = w / 20; 725 rect.p0.y = 1* h / 15;527 rect.p0.y = 2 * h / 15; 726 528 rect.p1.x = w - w / 20; 727 rect.p1.y = 4* h / 15;529 rect.p1.y = 5 * h / 15; 728 530 729 531 rc = gfx_fill_rect(gc, &rect); … … 733 535 gfx_color_delete(color); 734 536 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 } 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; 744 544 745 545 gfx_text_fmt_init(&fmt); 746 fmt.font = font;747 fmt.color = color;748 546 749 547 pos.x = rect.p0.x; 750 548 pos.y = rect.p0.y; 751 rc = gfx_puttext( &pos, &fmt, "Top left");549 rc = gfx_puttext(font, &pos, &fmt, "Top left"); 752 550 if (rc != EOK) { 753 551 printf("Error rendering text.\n"); … … 758 556 pos.y = rect.p0.y; 759 557 fmt.halign = gfx_halign_center; 760 rc = gfx_puttext( &pos, &fmt, "Top center");761 if (rc != EOK) 762 goto error; 763 764 pos.x = rect.p1.x ;558 rc = gfx_puttext(font, &pos, &fmt, "Top center"); 559 if (rc != EOK) 560 goto error; 561 562 pos.x = rect.p1.x - 1; 765 563 pos.y = rect.p0.y; 766 564 fmt.halign = gfx_halign_right; 767 rc = gfx_puttext( &pos, &fmt, "Top right");565 rc = gfx_puttext(font, &pos, &fmt, "Top right"); 768 566 if (rc != EOK) 769 567 goto error; … … 774 572 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 775 573 fmt.halign = gfx_halign_left; 776 rc = gfx_puttext( &pos, &fmt, "Center left");574 rc = gfx_puttext(font, &pos, &fmt, "Center left"); 777 575 if (rc != EOK) 778 576 goto error; … … 781 579 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 782 580 fmt.halign = gfx_halign_center; 783 rc = gfx_puttext( &pos, &fmt, "Center");784 if (rc != EOK) 785 goto error; 786 787 pos.x = rect.p1.x ;581 rc = gfx_puttext(font, &pos, &fmt, "Center"); 582 if (rc != EOK) 583 goto error; 584 585 pos.x = rect.p1.x - 1; 788 586 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 789 587 fmt.halign = gfx_halign_right; 790 rc = gfx_puttext( &pos, &fmt, "Center right");588 rc = gfx_puttext(font, &pos, &fmt, "Center right"); 791 589 if (rc != EOK) 792 590 goto error; … … 797 595 pos.y = rect.p1.y - 1; 798 596 fmt.halign = gfx_halign_left; 799 rc = gfx_puttext( &pos, &fmt, "Bottom left");597 rc = gfx_puttext(font, &pos, &fmt, "Bottom left"); 800 598 if (rc != EOK) 801 599 goto error; 802 600 803 601 pos.x = (rect.p0.x + rect.p1.x - 1) / 2; 804 pos.y = rect.p1.y ;602 pos.y = rect.p1.y - 1; 805 603 fmt.halign = gfx_halign_center; 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 ;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; 812 610 fmt.halign = gfx_halign_right; 813 rc = gfx_puttext( &pos, &fmt, "Bottom right");611 rc = gfx_puttext(font, &pos, &fmt, "Bottom right"); 814 612 if (rc != EOK) 815 613 goto error; … … 818 616 819 617 gfx_text_fmt_init(&fmt); 820 fmt.font = font;821 618 822 619 for (i = 0; i < 8; i++) { 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; 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; 836 628 837 629 pos.x = w / 20; 838 pos.y = ( 6+ i) * h / 15;839 rc = gfx_puttext( &pos, &fmt, "The quick brown fox jumps over the lazy dog.");630 pos.y = (7 + i) * h / 15; 631 rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog."); 840 632 if (rc != EOK) 841 633 goto error; … … 845 637 846 638 for (i = 0; i < 10; i++) { 847 demo_msleep(500);639 fibril_usleep(500 * 1000); 848 640 if (quit) 849 641 break; 850 642 } 851 643 644 gfx_font_close(font); 645 gfx_typeface_destroy(tface); 852 646 return EOK; 853 647 error: 648 if (font != NULL) 649 gfx_font_close(font); 650 if (tface != NULL) 651 gfx_typeface_destroy(tface); 854 652 return rc; 855 653 } 856 654 857 /** Run text abbreviation demoon a graphic context.655 /** Run demo loop on a graphic context. 858 656 * 859 657 * @param gc Graphic context … … 861 659 * @param h Height 862 660 */ 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); 661 static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 662 { 663 errno_t rc; 664 665 while (!quit) { 666 rc = demo_rects(gc, w, h); 982 667 if (rc != EOK) 983 668 return rc; 984 669 985 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000, 986 rand() % 0x10000, &color); 670 rc = demo_bitmap(gc, w, h); 987 671 if (rc != EOK) 988 672 return rc; 989 673 990 rc = gfx_set_color(gc, color);674 rc = demo_bitmap2(gc, w, h); 991 675 if (rc != EOK) 992 676 return rc; 993 677 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); 678 rc = demo_bitmap_kc(gc, w, h); 1013 679 if (rc != EOK) 1014 680 return rc; 1015 681 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); 1048 1049 while (!quit) { 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; 682 rc = demo_text(gc, w, h); 683 if (rc != EOK) 684 return rc; 685 } 686 687 return EOK; 1084 688 } 1085 689 … … 1087 691 static errno_t demo_console(void) 1088 692 { 693 console_ctrl_t *con = NULL; 694 console_gc_t *cgc = NULL; 1089 695 gfx_context_t *gc; 1090 sysarg_t cols, rows;1091 errno_t rc; 1092 696 errno_t rc; 697 698 printf("Init console..\n"); 1093 699 con = console_init(stdin, stdout); 1094 700 if (con == NULL) 1095 701 return EIO; 1096 702 1097 rc = console_get_size(con, &cols, &rows); 1098 if (rc != EOK) 1099 return rc; 1100 703 printf("Create console GC\n"); 1101 704 rc = console_gc_create(con, stdout, &cgc); 1102 705 if (rc != EOK) … … 1105 708 gc = console_gc_get_ctx(cgc); 1106 709 1107 /* Currently console is always text. */ 1108 textmode = true; 1109 1110 scr_width = cols; 1111 scr_height = rows; 1112 1113 rc = demo_loop(gc); 710 rc = demo_loop(gc, 80, 25); 1114 711 if (rc != EOK) 1115 712 return rc; … … 1120 717 1121 718 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;1136 719 } 1137 720 … … 1139 722 static errno_t demo_ui(const char *display_spec) 1140 723 { 724 ui_t *ui = NULL; 1141 725 ui_wnd_params_t params; 1142 726 ui_window_t *window = NULL; … … 1145 729 gfx_rect_t wrect; 1146 730 gfx_coord2_t off; 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; 731 errno_t rc; 732 733 printf("Init UI..\n"); 1152 734 1153 735 rc = ui_create(display_spec, &ui); 1154 736 if (rc != EOK) { 1155 737 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");1162 738 goto error; 1163 739 } … … 1171 747 params.caption = "GFX Demo"; 1172 748 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 1179 749 /* 1180 750 * Compute window rectangle such that application area corresponds 1181 751 * to rect 1182 752 */ 1183 ui_wdecor_rect_from_app( ui,params.style, &rect, &wrect);753 ui_wdecor_rect_from_app(params.style, &rect, &wrect); 1184 754 off = wrect.p0; 1185 755 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;1194 756 1195 757 rc = ui_window_create(ui, ¶ms, &window); … … 1207 769 } 1208 770 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); 771 task_retval(0); 772 773 rc = demo_loop(gc, rect.p1.x, rect.p1.y); 774 if (rc != EOK) 775 goto error; 776 1230 777 ui_window_destroy(window); 1231 778 ui_destroy(ui); … … 1249 796 errno_t rc; 1250 797 798 printf("Init display..\n"); 799 1251 800 rc = display_open(display_svc, &display); 1252 801 if (rc != EOK) { … … 1260 809 params.rect.p1.x = 400; 1261 810 params.rect.p1.y = 300; 1262 params.caption = "GFX Demo";1263 811 1264 812 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 1276 824 task_retval(0); 1277 825 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); 826 rc = demo_loop(gc, 400, 300); 1284 827 if (rc != EOK) 1285 828 return rc; … … 1295 838 } 1296 839 1297 static void demo_quit(void)1298 { 1299 fibril_mutex_lock(&quit_lock);840 static void wnd_close_event(void *arg) 841 { 842 printf("Close event\n"); 1300 843 quit = true; 1301 fibril_mutex_unlock(&quit_lock);1302 fibril_condvar_broadcast(&quit_cv);1303 }1304 1305 static void wnd_close_event(void *arg)1306 {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 }1329 844 } 1330 845 1331 846 static void wnd_kbd_event(void *arg, kbd_event_t *event) 1332 847 { 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; 848 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 849 if (event->type == KEY_PRESS) 850 quit = true; 1346 851 } 1347 852 1348 853 static void uiwnd_close_event(ui_window_t *window, void *arg) 1349 854 { 1350 demo_quit(); 855 printf("Close event\n"); 856 quit = true; 1351 857 } 1352 858 1353 859 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 1354 860 { 1355 (void)window;1356 (void)arg;1357 demo_kbd_event(event);861 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 862 if (event->type == KEY_PRESS) 863 quit = true; 1358 864 } 1359 865 1360 866 static void print_syntax(void) 1361 867 { 1362 printf("Syntax: gfxdemo [-d <display>] {c onsole|display|ui}\n");868 printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n"); 1363 869 } 1364 870 … … 1367 873 errno_t rc; 1368 874 const char *display_svc = DISPLAY_DEFAULT; 1369 const char *ui_display_spec = UI_ANY_DEFAULT;1370 875 int i; 1371 876 … … 1380 885 } 1381 886 1382 display_svc = ui_display_spec =argv[i++];887 display_svc = argv[i++]; 1383 888 } else { 1384 889 printf("Invalid option '%s'.\n", argv[i]); … … 1388 893 } 1389 894 1390 if (i >= argc || str_cmp(argv[i], " ui") == 0) {1391 rc = demo_ ui(ui_display_spec);895 if (i >= argc || str_cmp(argv[i], "display") == 0) { 896 rc = demo_display(display_svc); 1392 897 if (rc != EOK) 1393 898 return 1; … … 1396 901 if (rc != EOK) 1397 902 return 1; 1398 } else if (str_cmp(argv[i], " display") == 0) {1399 rc = demo_ display(display_svc);903 } else if (str_cmp(argv[i], "ui") == 0) { 904 rc = demo_ui(display_svc); 1400 905 if (rc != EOK) 1401 906 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.
