Changes in uspace/app/gfxdemo/gfxdemo.c [400a16d:3d588be] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
r400a16d r3d588be 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 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" 53 54 54 55 static void wnd_close_event(void *); … … 68 69 }; 69 70 71 static void demo_kbd_event(kbd_event_t *); 72 70 73 static bool quit = false; 74 static FIBRIL_MUTEX_INITIALIZE(quit_lock); 75 static FIBRIL_CONDVAR_INITIALIZE(quit_cv); 71 76 static gfx_typeface_t *tface; 72 77 static gfx_font_t *font; 73 78 static gfx_coord_t vpad; 79 static console_ctrl_t *con = NULL; 80 static bool textmode; 81 static ui_t *ui; 74 82 75 83 /** Determine if we are running in text mode. 76 84 * 77 * @param w Screen width78 * @param h Screen height79 85 * @return @c true iff we are running in text mode 80 86 */ 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; 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); 85 126 } 86 127 … … 138 179 139 180 /* XXX Crude way of detecting text mode */ 140 if ( w < 256) {181 if (demo_is_text()) { 141 182 /* Create dummy font for text mode */ 142 183 rc = gfx_typeface_create(gc, &tface); … … 227 268 228 269 if (font != NULL) { 229 if (demo_is_text( w, h)) {270 if (demo_is_text()) { 230 271 rc = gfx_color_new_ega(0x1e, &color); 231 272 if (rc != EOK) … … 238 279 239 280 gfx_text_fmt_init(&fmt); 281 fmt.font = font; 240 282 fmt.color = color; 241 283 fmt.halign = gfx_halign_center; … … 243 285 244 286 pos.x = w / 2; 245 pos.y = h - 1;246 rc = gfx_puttext( font,&pos, &fmt, text);287 pos.y = h; 288 rc = gfx_puttext(&pos, &fmt, text); 247 289 if (rc != EOK) { 248 290 printf("Error rendering text.\n"); … … 315 357 gfx_color_delete(color); 316 358 317 fibril_usleep(500 * 1000); 318 359 demo_msleep(500); 319 360 if (quit) 320 361 break; … … 350 391 for (j = 0; j < h; j++) { 351 392 pixelmap_put_pixel(&pixelmap, i, j, 352 PIXEL( 0, (i % 30) < 3 ? 255 : 0,393 PIXEL(255, (i % 30) < 3 ? 255 : 0, 353 394 (j % 30) < 3 ? 255 : 0, i / 2)); 354 395 } … … 386 427 k = i * i + j * j; 387 428 pixelmap_put_pixel(&pixelmap, i, j, 388 PIXEL( 0, k, k, k));429 PIXEL(255, k, k, k)); 389 430 } 390 431 } … … 421 462 k = i * i + j * j; 422 463 pixelmap_put_pixel(&pixelmap, i, j, 423 k < w * w / 2 ? PIXEL( 0, 0, 255, 0) :424 PIXEL( 0, 255, 0, 255));464 k < w * w / 2 ? PIXEL(255, 0, 255, 0) : 465 PIXEL(255, 255, 0, 255)); 425 466 } 426 467 } … … 477 518 if (rc != EOK) 478 519 goto error; 479 fibril_usleep(250 * 1000); 480 520 521 demo_msleep(250); 481 522 if (quit) 482 523 goto out; … … 538 579 } 539 580 540 fibril_usleep(500 * 1000); 541 581 demo_msleep(500); 542 582 if (quit) 543 583 break; … … 579 619 params.rect.p1.y = 40; 580 620 params.flags = bmpf_color_key; 581 params.key_color = PIXEL( 0, 255, 0, 255);621 params.key_color = PIXEL(255, 255, 0, 255); 582 622 583 623 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 599 639 } 600 640 601 fibril_usleep(500 * 1000); 602 641 demo_msleep(500); 603 642 if (quit) 604 643 break; … … 678 717 gfx_color_delete(color); 679 718 680 if (demo_is_text( w, h)) {719 if (demo_is_text()) { 681 720 rc = gfx_color_new_ega(0x1f, &color); 682 721 if (rc != EOK) … … 689 728 690 729 gfx_text_fmt_init(&fmt); 730 fmt.font = font; 691 731 fmt.color = color; 692 732 693 733 pos.x = rect.p0.x; 694 734 pos.y = rect.p0.y; 695 rc = gfx_puttext( font,&pos, &fmt, "Top left");735 rc = gfx_puttext(&pos, &fmt, "Top left"); 696 736 if (rc != EOK) { 697 737 printf("Error rendering text.\n"); … … 702 742 pos.y = rect.p0.y; 703 743 fmt.halign = gfx_halign_center; 704 rc = gfx_puttext( font,&pos, &fmt, "Top center");744 rc = gfx_puttext(&pos, &fmt, "Top center"); 705 745 if (rc != EOK) 706 746 goto error; … … 709 749 pos.y = rect.p0.y; 710 750 fmt.halign = gfx_halign_right; 711 rc = gfx_puttext( font,&pos, &fmt, "Top right");751 rc = gfx_puttext(&pos, &fmt, "Top right"); 712 752 if (rc != EOK) 713 753 goto error; … … 718 758 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 719 759 fmt.halign = gfx_halign_left; 720 rc = gfx_puttext( font,&pos, &fmt, "Center left");760 rc = gfx_puttext(&pos, &fmt, "Center left"); 721 761 if (rc != EOK) 722 762 goto error; … … 725 765 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 726 766 fmt.halign = gfx_halign_center; 727 rc = gfx_puttext( font,&pos, &fmt, "Center");767 rc = gfx_puttext(&pos, &fmt, "Center"); 728 768 if (rc != EOK) 729 769 goto error; … … 732 772 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 733 773 fmt.halign = gfx_halign_right; 734 rc = gfx_puttext( font,&pos, &fmt, "Center right");774 rc = gfx_puttext(&pos, &fmt, "Center right"); 735 775 if (rc != EOK) 736 776 goto error; … … 741 781 pos.y = rect.p1.y - 1; 742 782 fmt.halign = gfx_halign_left; 743 rc = gfx_puttext( font,&pos, &fmt, "Bottom left");783 rc = gfx_puttext(&pos, &fmt, "Bottom left"); 744 784 if (rc != EOK) 745 785 goto error; … … 748 788 pos.y = rect.p1.y; 749 789 fmt.halign = gfx_halign_center; 750 rc = gfx_puttext( font,&pos, &fmt, "Bottom center");790 rc = gfx_puttext(&pos, &fmt, "Bottom center"); 751 791 if (rc != EOK) 752 792 goto error; … … 755 795 pos.y = rect.p1.y; 756 796 fmt.halign = gfx_halign_right; 757 rc = gfx_puttext( font,&pos, &fmt, "Bottom right");797 rc = gfx_puttext(&pos, &fmt, "Bottom right"); 758 798 if (rc != EOK) 759 799 goto error; … … 762 802 763 803 gfx_text_fmt_init(&fmt); 804 fmt.font = font; 764 805 765 806 for (i = 0; i < 8; i++) { 766 if (demo_is_text( w, h)) {807 if (demo_is_text()) { 767 808 rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color); 768 809 if (rc != EOK) … … 776 817 777 818 fmt.color = color; 819 fmt.underline = !fmt.underline; 778 820 779 821 pos.x = w / 20; 780 822 pos.y = (6 + i) * h / 15; 781 rc = gfx_puttext( font,&pos, &fmt, "The quick brown fox jumps over the lazy dog.");823 rc = gfx_puttext(&pos, &fmt, "The quick brown fox jumps over the lazy dog."); 782 824 if (rc != EOK) 783 825 goto error; … … 787 829 788 830 for (i = 0; i < 10; i++) { 789 fibril_usleep(500 * 1000); 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); 790 913 if (quit) 791 914 break; … … 884 1007 } 885 1008 886 fibril_usleep(500 * 1000); 887 1009 demo_msleep(500); 888 1010 if (quit) 889 1011 break; … … 932 1054 goto error; 933 1055 1056 rc = demo_text_abbr(gc, w, h); 1057 if (rc != EOK) 1058 goto error; 1059 934 1060 rc = demo_clip(gc, w, h); 935 1061 if (rc != EOK) … … 947 1073 static errno_t demo_console(void) 948 1074 { 949 console_ctrl_t *con = NULL;950 1075 console_gc_t *cgc = NULL; 951 1076 gfx_context_t *gc; 952 errno_t rc;953 954 printf("Init console..\n"); 1077 sysarg_t cols, rows; 1078 errno_t rc; 1079 955 1080 con = console_init(stdin, stdout); 956 1081 if (con == NULL) 957 1082 return EIO; 958 1083 959 printf("Create console GC\n"); 1084 rc = console_get_size(con, &cols, &rows); 1085 if (rc != EOK) 1086 return rc; 1087 960 1088 rc = console_gc_create(con, stdout, &cgc); 961 1089 if (rc != EOK) … … 964 1092 gc = console_gc_get_ctx(cgc); 965 1093 966 rc = demo_loop(gc, 80, 25); 1094 /* Currently console is always text. */ 1095 textmode = true; 1096 1097 rc = demo_loop(gc, cols, rows); 967 1098 if (rc != EOK) 968 1099 return rc; … … 973 1104 974 1105 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; 975 1118 } 976 1119 … … 978 1121 static errno_t demo_ui(const char *display_spec) 979 1122 { 980 ui_t *ui = NULL;981 1123 ui_wnd_params_t params; 982 1124 ui_window_t *window = NULL; … … 985 1127 gfx_rect_t wrect; 986 1128 gfx_coord2_t off; 987 errno_t rc; 988 989 printf("Init UI..\n"); 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; 990 1134 991 1135 rc = ui_create(display_spec, &ui); 992 1136 if (rc != EOK) { 993 1137 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"); 994 1144 goto error; 995 1145 } … … 1003 1153 params.caption = "GFX Demo"; 1004 1154 1155 /* Do not decorate the window in fullscreen mode */ 1156 if (ui_is_fullscreen(ui)) 1157 params.style &= ~ui_wds_decorated; 1158 1005 1159 /* 1006 1160 * Compute window rectangle such that application area corresponds 1007 1161 * to rect 1008 1162 */ 1009 ui_wdecor_rect_from_app( params.style, &rect, &wrect);1163 ui_wdecor_rect_from_app(ui, params.style, &rect, &wrect); 1010 1164 off = wrect.p0; 1011 1165 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; 1012 1174 1013 1175 rc = ui_window_create(ui, ¶ms, &window); … … 1025 1187 } 1026 1188 1027 task_retval(0); 1028 1029 rc = demo_loop(gc, rect.p1.x, rect.p1.y); 1030 if (rc != EOK) 1031 goto error; 1032 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); 1033 1210 ui_window_destroy(window); 1034 1211 ui_destroy(ui); … … 1052 1229 errno_t rc; 1053 1230 1054 printf("Init display..\n");1055 1056 1231 rc = display_open(display_svc, &display); 1057 1232 if (rc != EOK) { … … 1065 1240 params.rect.p1.x = 400; 1066 1241 params.rect.p1.y = 300; 1242 params.caption = "GFX Demo"; 1067 1243 1068 1244 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 1080 1256 task_retval(0); 1081 1257 1258 /* FIXME Assuming display service is not text mode. */ 1259 textmode = false; 1260 1082 1261 rc = demo_loop(gc, 400, 300); 1083 1262 if (rc != EOK) … … 1094 1273 } 1095 1274 1275 static void demo_quit(void) 1276 { 1277 fibril_mutex_lock(&quit_lock); 1278 quit = true; 1279 fibril_mutex_unlock(&quit_lock); 1280 fibril_condvar_broadcast(&quit_cv); 1281 } 1282 1096 1283 static void wnd_close_event(void *arg) 1097 1284 { 1098 printf("Close event\n"); 1099 quit = true; 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 } 1100 1307 } 1101 1308 1102 1309 static void wnd_kbd_event(void *arg, kbd_event_t *event) 1103 1310 { 1104 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 1105 if (event->type == KEY_PRESS) 1106 quit = true; 1311 (void)arg; 1312 demo_kbd_event(event); 1107 1313 } 1108 1314 1109 1315 static void uiwnd_close_event(ui_window_t *window, void *arg) 1110 1316 { 1111 printf("Close event\n"); 1112 quit = true; 1317 demo_quit(); 1113 1318 } 1114 1319 1115 1320 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 1116 1321 { 1117 printf("Keyboard event type=%d key=%d\n", event->type, event->key);1118 if (event->type == KEY_PRESS)1119 quit = true;1322 (void)window; 1323 (void)arg; 1324 demo_kbd_event(event); 1120 1325 } 1121 1326 … … 1129 1334 errno_t rc; 1130 1335 const char *display_svc = DISPLAY_DEFAULT; 1336 const char *ui_display_spec = UI_ANY_DEFAULT; 1131 1337 int i; 1132 1338 … … 1141 1347 } 1142 1348 1143 display_svc = argv[i++];1349 display_svc = ui_display_spec = argv[i++]; 1144 1350 } else { 1145 1351 printf("Invalid option '%s'.\n", argv[i]); … … 1158 1364 return 1; 1159 1365 } else if (str_cmp(argv[i], "ui") == 0) { 1160 rc = demo_ui( display_svc);1366 rc = demo_ui(ui_display_spec); 1161 1367 if (rc != EOK) 1162 1368 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.