Changes in uspace/app/gfxdemo/gfxdemo.c [3d588be:400a16d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
r3d588be r400a16d 1 1 /* 2 * Copyright (c) 202 4Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 51 51 #include <ui/window.h> 52 52 #include <ui/wdecor.h> 53 #include "gfxdemo.h"54 53 55 54 static void wnd_close_event(void *); … … 69 68 }; 70 69 71 static void demo_kbd_event(kbd_event_t *);72 73 70 static bool quit = false; 74 static FIBRIL_MUTEX_INITIALIZE(quit_lock);75 static FIBRIL_CONDVAR_INITIALIZE(quit_cv);76 71 static gfx_typeface_t *tface; 77 72 static gfx_font_t *font; 78 73 static gfx_coord_t vpad; 79 static console_ctrl_t *con = NULL;80 static bool textmode;81 static ui_t *ui;82 74 83 75 /** Determine if we are running in text mode. 84 76 * 77 * @param w Screen width 78 * @param h Screen height 85 79 * @return @c true iff we are running in text mode 86 80 */ 87 static bool demo_is_text(void) 88 { 89 return textmode; 90 } 91 92 /** Sleep until timeout or quit request. 93 * 94 * @param msec Number of microseconds to sleep for 95 */ 96 static void demo_msleep(unsigned msec) 97 { 98 errno_t rc; 99 usec_t usec; 100 cons_event_t cevent; 101 102 if (ui != NULL) 103 ui_unlock(ui); 104 fibril_mutex_lock(&quit_lock); 105 if (!quit) { 106 if (con != NULL) { 107 usec = (usec_t)msec * 1000; 108 while (usec > 0 && !quit) { 109 rc = console_get_event_timeout(con, &cevent, &usec); 110 if (rc == EOK) { 111 if (cevent.type == CEV_KEY) { 112 fibril_mutex_unlock(&quit_lock); 113 demo_kbd_event(&cevent.ev.key); 114 fibril_mutex_lock(&quit_lock); 115 } 116 } 117 } 118 } else { 119 (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock, 120 (usec_t)msec * 1000); 121 } 122 } 123 fibril_mutex_unlock(&quit_lock); 124 if (ui != NULL) 125 ui_lock(ui); 81 static bool demo_is_text(gfx_coord_t w, gfx_coord_t h) 82 { 83 // XXX Need a proper way to determine text mode 84 return w <= 80; 126 85 } 127 86 … … 179 138 180 139 /* XXX Crude way of detecting text mode */ 181 if ( demo_is_text()) {140 if (w < 256) { 182 141 /* Create dummy font for text mode */ 183 142 rc = gfx_typeface_create(gc, &tface); … … 268 227 269 228 if (font != NULL) { 270 if (demo_is_text( )) {229 if (demo_is_text(w, h)) { 271 230 rc = gfx_color_new_ega(0x1e, &color); 272 231 if (rc != EOK) … … 279 238 280 239 gfx_text_fmt_init(&fmt); 281 fmt.font = font;282 240 fmt.color = color; 283 241 fmt.halign = gfx_halign_center; … … 285 243 286 244 pos.x = w / 2; 287 pos.y = h ;288 rc = gfx_puttext( &pos, &fmt, text);245 pos.y = h - 1; 246 rc = gfx_puttext(font, &pos, &fmt, text); 289 247 if (rc != EOK) { 290 248 printf("Error rendering text.\n"); … … 357 315 gfx_color_delete(color); 358 316 359 demo_msleep(500); 317 fibril_usleep(500 * 1000); 318 360 319 if (quit) 361 320 break; … … 391 350 for (j = 0; j < h; j++) { 392 351 pixelmap_put_pixel(&pixelmap, i, j, 393 PIXEL( 255, (i % 30) < 3 ? 255 : 0,352 PIXEL(0, (i % 30) < 3 ? 255 : 0, 394 353 (j % 30) < 3 ? 255 : 0, i / 2)); 395 354 } … … 427 386 k = i * i + j * j; 428 387 pixelmap_put_pixel(&pixelmap, i, j, 429 PIXEL( 255, k, k, k));388 PIXEL(0, k, k, k)); 430 389 } 431 390 } … … 462 421 k = i * i + j * j; 463 422 pixelmap_put_pixel(&pixelmap, i, j, 464 k < w * w / 2 ? PIXEL( 255, 0, 255, 0) :465 PIXEL( 255, 255, 0, 255));423 k < w * w / 2 ? PIXEL(0, 0, 255, 0) : 424 PIXEL(0, 255, 0, 255)); 466 425 } 467 426 } … … 518 477 if (rc != EOK) 519 478 goto error; 520 521 demo_msleep(250); 479 fibril_usleep(250 * 1000); 480 522 481 if (quit) 523 482 goto out; … … 579 538 } 580 539 581 demo_msleep(500); 540 fibril_usleep(500 * 1000); 541 582 542 if (quit) 583 543 break; … … 619 579 params.rect.p1.y = 40; 620 580 params.flags = bmpf_color_key; 621 params.key_color = PIXEL( 255, 255, 0, 255);581 params.key_color = PIXEL(0, 255, 0, 255); 622 582 623 583 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 639 599 } 640 600 641 demo_msleep(500); 601 fibril_usleep(500 * 1000); 602 642 603 if (quit) 643 604 break; … … 717 678 gfx_color_delete(color); 718 679 719 if (demo_is_text( )) {680 if (demo_is_text(w, h)) { 720 681 rc = gfx_color_new_ega(0x1f, &color); 721 682 if (rc != EOK) … … 728 689 729 690 gfx_text_fmt_init(&fmt); 730 fmt.font = font;731 691 fmt.color = color; 732 692 733 693 pos.x = rect.p0.x; 734 694 pos.y = rect.p0.y; 735 rc = gfx_puttext( &pos, &fmt, "Top left");695 rc = gfx_puttext(font, &pos, &fmt, "Top left"); 736 696 if (rc != EOK) { 737 697 printf("Error rendering text.\n"); … … 742 702 pos.y = rect.p0.y; 743 703 fmt.halign = gfx_halign_center; 744 rc = gfx_puttext( &pos, &fmt, "Top center");704 rc = gfx_puttext(font, &pos, &fmt, "Top center"); 745 705 if (rc != EOK) 746 706 goto error; … … 749 709 pos.y = rect.p0.y; 750 710 fmt.halign = gfx_halign_right; 751 rc = gfx_puttext( &pos, &fmt, "Top right");711 rc = gfx_puttext(font, &pos, &fmt, "Top right"); 752 712 if (rc != EOK) 753 713 goto error; … … 758 718 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 759 719 fmt.halign = gfx_halign_left; 760 rc = gfx_puttext( &pos, &fmt, "Center left");720 rc = gfx_puttext(font, &pos, &fmt, "Center left"); 761 721 if (rc != EOK) 762 722 goto error; … … 765 725 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 766 726 fmt.halign = gfx_halign_center; 767 rc = gfx_puttext( &pos, &fmt, "Center");727 rc = gfx_puttext(font, &pos, &fmt, "Center"); 768 728 if (rc != EOK) 769 729 goto error; … … 772 732 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 773 733 fmt.halign = gfx_halign_right; 774 rc = gfx_puttext( &pos, &fmt, "Center right");734 rc = gfx_puttext(font, &pos, &fmt, "Center right"); 775 735 if (rc != EOK) 776 736 goto error; … … 781 741 pos.y = rect.p1.y - 1; 782 742 fmt.halign = gfx_halign_left; 783 rc = gfx_puttext( &pos, &fmt, "Bottom left");743 rc = gfx_puttext(font, &pos, &fmt, "Bottom left"); 784 744 if (rc != EOK) 785 745 goto error; … … 788 748 pos.y = rect.p1.y; 789 749 fmt.halign = gfx_halign_center; 790 rc = gfx_puttext( &pos, &fmt, "Bottom center");750 rc = gfx_puttext(font, &pos, &fmt, "Bottom center"); 791 751 if (rc != EOK) 792 752 goto error; … … 795 755 pos.y = rect.p1.y; 796 756 fmt.halign = gfx_halign_right; 797 rc = gfx_puttext( &pos, &fmt, "Bottom right");757 rc = gfx_puttext(font, &pos, &fmt, "Bottom right"); 798 758 if (rc != EOK) 799 759 goto error; … … 802 762 803 763 gfx_text_fmt_init(&fmt); 804 fmt.font = font;805 764 806 765 for (i = 0; i < 8; i++) { 807 if (demo_is_text( )) {766 if (demo_is_text(w, h)) { 808 767 rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color); 809 768 if (rc != EOK) … … 817 776 818 777 fmt.color = color; 819 fmt.underline = !fmt.underline;820 778 821 779 pos.x = w / 20; 822 780 pos.y = (6 + i) * h / 15; 823 rc = gfx_puttext( &pos, &fmt, "The quick brown fox jumps over the lazy dog.");781 rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog."); 824 782 if (rc != EOK) 825 783 goto error; … … 829 787 830 788 for (i = 0; i < 10; i++) { 831 demo_msleep(500); 832 if (quit) 833 break; 834 } 835 836 return EOK; 837 error: 838 return rc; 839 } 840 841 /** Run text abbreviation demo on a graphic context. 842 * 843 * @param gc Graphic context 844 * @param w Width 845 * @param h Height 846 */ 847 static errno_t demo_text_abbr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 848 { 849 gfx_color_t *color = NULL; 850 gfx_rect_t rect; 851 gfx_coord2_t pos; 852 gfx_text_fmt_t fmt; 853 int i; 854 errno_t rc; 855 856 if (quit) 857 return EOK; 858 859 rc = demo_begin(gc, w, h, "Text abbreviation"); 860 if (rc != EOK) 861 goto error; 862 863 for (i = 0; i < 11; i++) { 864 865 rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color); 866 if (rc != EOK) 867 goto error; 868 869 rc = gfx_set_color(gc, color); 870 if (rc != EOK) 871 goto error; 872 873 rect.p0.x = w / 20; 874 rect.p0.y = (2 + 2 * i) * h / 25; 875 rect.p1.x = w - w / 20 - w * i / 12; 876 rect.p1.y = (3 + 2 * i) * h / 25; 877 878 rc = gfx_fill_rect(gc, &rect); 879 if (rc != EOK) 880 goto error; 881 882 gfx_color_delete(color); 883 884 if (demo_is_text()) { 885 rc = gfx_color_new_ega(0x1f, &color); 886 if (rc != EOK) 887 goto error; 888 } else { 889 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, 890 &color); 891 if (rc != EOK) 892 goto error; 893 } 894 895 gfx_text_fmt_init(&fmt); 896 fmt.font = font; 897 fmt.color = color; 898 fmt.abbreviate = true; 899 fmt.width = rect.p1.x - rect.p0.x; 900 901 pos.x = rect.p0.x; 902 pos.y = rect.p0.y; 903 rc = gfx_puttext(&pos, &fmt, 904 "The quick brow fox jumps over the lazy dog!"); 905 if (rc != EOK) { 906 printf("Error rendering text.\n"); 907 goto error; 908 } 909 } 910 911 for (i = 0; i < 10; i++) { 912 demo_msleep(500); 789 fibril_usleep(500 * 1000); 913 790 if (quit) 914 791 break; … … 1007 884 } 1008 885 1009 demo_msleep(500); 886 fibril_usleep(500 * 1000); 887 1010 888 if (quit) 1011 889 break; … … 1054 932 goto error; 1055 933 1056 rc = demo_text_abbr(gc, w, h);1057 if (rc != EOK)1058 goto error;1059 1060 934 rc = demo_clip(gc, w, h); 1061 935 if (rc != EOK) … … 1073 947 static errno_t demo_console(void) 1074 948 { 949 console_ctrl_t *con = NULL; 1075 950 console_gc_t *cgc = NULL; 1076 951 gfx_context_t *gc; 1077 sysarg_t cols, rows;1078 errno_t rc; 1079 952 errno_t rc; 953 954 printf("Init console..\n"); 1080 955 con = console_init(stdin, stdout); 1081 956 if (con == NULL) 1082 957 return EIO; 1083 958 1084 rc = console_get_size(con, &cols, &rows); 1085 if (rc != EOK) 1086 return rc; 1087 959 printf("Create console GC\n"); 1088 960 rc = console_gc_create(con, stdout, &cgc); 1089 961 if (rc != EOK) … … 1092 964 gc = console_gc_get_ctx(cgc); 1093 965 1094 /* Currently console is always text. */ 1095 textmode = true; 1096 1097 rc = demo_loop(gc, cols, rows); 966 rc = demo_loop(gc, 80, 25); 1098 967 if (rc != EOK) 1099 968 return rc; … … 1104 973 1105 974 return EOK; 1106 }1107 1108 static errno_t demo_ui_fibril(void *arg)1109 {1110 demo_ui_args_t *args = (demo_ui_args_t *)arg;1111 errno_t rc;1112 1113 ui_lock(args->ui);1114 rc = demo_loop(args->gc, args->dims.x, args->dims.y);1115 ui_unlock(args->ui);1116 ui_quit(args->ui);1117 return rc;1118 975 } 1119 976 … … 1121 978 static errno_t demo_ui(const char *display_spec) 1122 979 { 980 ui_t *ui = NULL; 1123 981 ui_wnd_params_t params; 1124 982 ui_window_t *window = NULL; … … 1127 985 gfx_rect_t wrect; 1128 986 gfx_coord2_t off; 1129 gfx_rect_t ui_rect; 1130 gfx_coord2_t dims; 1131 demo_ui_args_t args; 1132 fid_t fid; 1133 errno_t rc; 987 errno_t rc; 988 989 printf("Init UI..\n"); 1134 990 1135 991 rc = ui_create(display_spec, &ui); 1136 992 if (rc != EOK) { 1137 993 printf("Error initializing UI (%s)\n", display_spec); 1138 goto error;1139 }1140 1141 rc = ui_get_rect(ui, &ui_rect);1142 if (rc != EOK) {1143 printf("Error getting display size.\n");1144 994 goto error; 1145 995 } … … 1153 1003 params.caption = "GFX Demo"; 1154 1004 1155 /* Do not decorate the window in fullscreen mode */1156 if (ui_is_fullscreen(ui))1157 params.style &= ~ui_wds_decorated;1158 1159 1005 /* 1160 1006 * Compute window rectangle such that application area corresponds 1161 1007 * to rect 1162 1008 */ 1163 ui_wdecor_rect_from_app( ui,params.style, &rect, &wrect);1009 ui_wdecor_rect_from_app(params.style, &rect, &wrect); 1164 1010 off = wrect.p0; 1165 1011 gfx_rect_rtranslate(&off, &wrect, ¶ms.rect); 1166 1167 gfx_rect_dims(&ui_rect, &dims);1168 1169 /* Make sure window is not larger than the entire screen */1170 if (params.rect.p1.x > dims.x)1171 params.rect.p1.x = dims.x;1172 if (params.rect.p1.y > dims.y)1173 params.rect.p1.y = dims.y;1174 1012 1175 1013 rc = ui_window_create(ui, ¶ms, &window); … … 1187 1025 } 1188 1026 1189 ui_window_get_app_rect(window, &rect); 1190 gfx_rect_dims(&rect, &dims); 1191 1192 if (!ui_is_fullscreen(ui)) 1193 task_retval(0); 1194 1195 textmode = ui_is_textmode(ui); 1196 1197 args.gc = gc; 1198 args.dims = dims; 1199 args.ui = ui; 1200 1201 fid = fibril_create(demo_ui_fibril, (void *)&args); 1202 if (fid == 0) { 1203 rc = ENOMEM; 1204 goto error; 1205 } 1206 1207 fibril_add_ready(fid); 1208 1209 ui_run(ui); 1027 task_retval(0); 1028 1029 rc = demo_loop(gc, rect.p1.x, rect.p1.y); 1030 if (rc != EOK) 1031 goto error; 1032 1210 1033 ui_window_destroy(window); 1211 1034 ui_destroy(ui); … … 1229 1052 errno_t rc; 1230 1053 1054 printf("Init display..\n"); 1055 1231 1056 rc = display_open(display_svc, &display); 1232 1057 if (rc != EOK) { … … 1240 1065 params.rect.p1.x = 400; 1241 1066 params.rect.p1.y = 300; 1242 params.caption = "GFX Demo";1243 1067 1244 1068 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 1256 1080 task_retval(0); 1257 1081 1258 /* FIXME Assuming display service is not text mode. */1259 textmode = false;1260 1261 1082 rc = demo_loop(gc, 400, 300); 1262 1083 if (rc != EOK) … … 1273 1094 } 1274 1095 1275 static void demo_quit(void)1276 { 1277 fibril_mutex_lock(&quit_lock);1096 static void wnd_close_event(void *arg) 1097 { 1098 printf("Close event\n"); 1278 1099 quit = true; 1279 fibril_mutex_unlock(&quit_lock);1280 fibril_condvar_broadcast(&quit_cv);1281 }1282 1283 static void wnd_close_event(void *arg)1284 {1285 demo_quit();1286 }1287 1288 static void demo_kbd_event(kbd_event_t *event)1289 {1290 if (event->type == KEY_PRESS) {1291 /* Ctrl-Q */1292 if ((event->mods & KM_CTRL) != 0 &&1293 (event->mods & KM_ALT) == 0 &&1294 (event->mods & KM_SHIFT) == 0 &&1295 event->key == KC_Q) {1296 demo_quit();1297 }1298 1299 /* Escape */1300 if ((event->mods & KM_CTRL) == 0 &&1301 (event->mods & KM_ALT) == 0 &&1302 (event->mods & KM_SHIFT) == 0 &&1303 event->key == KC_ESCAPE) {1304 demo_quit();1305 }1306 }1307 1100 } 1308 1101 1309 1102 static void wnd_kbd_event(void *arg, kbd_event_t *event) 1310 1103 { 1311 (void)arg; 1312 demo_kbd_event(event); 1104 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 1105 if (event->type == KEY_PRESS) 1106 quit = true; 1313 1107 } 1314 1108 1315 1109 static void uiwnd_close_event(ui_window_t *window, void *arg) 1316 1110 { 1317 demo_quit(); 1111 printf("Close event\n"); 1112 quit = true; 1318 1113 } 1319 1114 1320 1115 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 1321 1116 { 1322 (void)window;1323 (void)arg;1324 demo_kbd_event(event);1117 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 1118 if (event->type == KEY_PRESS) 1119 quit = true; 1325 1120 } 1326 1121 … … 1334 1129 errno_t rc; 1335 1130 const char *display_svc = DISPLAY_DEFAULT; 1336 const char *ui_display_spec = UI_ANY_DEFAULT;1337 1131 int i; 1338 1132 … … 1347 1141 } 1348 1142 1349 display_svc = ui_display_spec =argv[i++];1143 display_svc = argv[i++]; 1350 1144 } else { 1351 1145 printf("Invalid option '%s'.\n", argv[i]); … … 1364 1158 return 1; 1365 1159 } else if (str_cmp(argv[i], "ui") == 0) { 1366 rc = demo_ui( ui_display_spec);1160 rc = demo_ui(display_svc); 1367 1161 if (rc != EOK) 1368 1162 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.