Changes in uspace/app/gfxdemo/gfxdemo.c [3d588be:da15002] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
r3d588be rda15002 1 1 /* 2 * Copyright (c) 202 4Jiri Svoboda2 * Copyright (c) 2020 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 83 /** Determine if we are running in text mode.84 *85 * @return @c true iff we are running in text mode86 */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 for95 */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);126 }127 74 128 75 /** Clear screen. … … 179 126 180 127 /* XXX Crude way of detecting text mode */ 181 if ( demo_is_text()) {128 if (w < 256) { 182 129 /* Create dummy font for text mode */ 183 130 rc = gfx_typeface_create(gc, &tface); … … 268 215 269 216 if (font != NULL) { 270 if (demo_is_text()) { 271 rc = gfx_color_new_ega(0x1e, &color); 272 if (rc != EOK) 273 goto error; 274 } else { 275 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 276 if (rc != EOK) 277 goto error; 278 } 217 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 218 if (rc != EOK) 219 goto error; 279 220 280 221 gfx_text_fmt_init(&fmt); 281 fmt.font = font;282 222 fmt.color = color; 283 223 fmt.halign = gfx_halign_center; … … 285 225 286 226 pos.x = w / 2; 287 pos.y = h ;288 rc = gfx_puttext( &pos, &fmt, text);227 pos.y = h - 1; 228 rc = gfx_puttext(font, &pos, &fmt, text); 289 229 if (rc != EOK) { 290 230 printf("Error rendering text.\n"); … … 357 297 gfx_color_delete(color); 358 298 359 demo_msleep(500); 299 fibril_usleep(500 * 1000); 300 360 301 if (quit) 361 302 break; … … 391 332 for (j = 0; j < h; j++) { 392 333 pixelmap_put_pixel(&pixelmap, i, j, 393 PIXEL( 255, (i % 30) < 3 ? 255 : 0,334 PIXEL(0, (i % 30) < 3 ? 255 : 0, 394 335 (j % 30) < 3 ? 255 : 0, i / 2)); 395 336 } … … 427 368 k = i * i + j * j; 428 369 pixelmap_put_pixel(&pixelmap, i, j, 429 PIXEL( 255, k, k, k));370 PIXEL(0, k, k, k)); 430 371 } 431 372 } … … 462 403 k = i * i + j * j; 463 404 pixelmap_put_pixel(&pixelmap, i, j, 464 k < w * w / 2 ? PIXEL( 255, 0, 255, 0) :465 PIXEL( 255, 255, 0, 255));405 k < w * w / 2 ? PIXEL(0, 0, 255, 0) : 406 PIXEL(0, 255, 0, 255)); 466 407 } 467 408 } … … 518 459 if (rc != EOK) 519 460 goto error; 520 521 demo_msleep(250); 461 fibril_usleep(250 * 1000); 462 522 463 if (quit) 523 464 goto out; … … 579 520 } 580 521 581 demo_msleep(500); 522 fibril_usleep(500 * 1000); 523 582 524 if (quit) 583 525 break; … … 619 561 params.rect.p1.y = 40; 620 562 params.flags = bmpf_color_key; 621 params.key_color = PIXEL( 255, 255, 0, 255);563 params.key_color = PIXEL(0, 255, 0, 255); 622 564 623 565 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 639 581 } 640 582 641 demo_msleep(500); 583 fibril_usleep(500 * 1000); 584 642 585 if (quit) 643 586 break; … … 717 660 gfx_color_delete(color); 718 661 719 if (demo_is_text()) { 720 rc = gfx_color_new_ega(0x1f, &color); 721 if (rc != EOK) 722 goto error; 723 } else { 724 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 725 if (rc != EOK) 726 goto error; 727 } 662 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 663 if (rc != EOK) 664 goto error; 728 665 729 666 gfx_text_fmt_init(&fmt); 730 fmt.font = font;731 667 fmt.color = color; 732 668 733 669 pos.x = rect.p0.x; 734 670 pos.y = rect.p0.y; 735 rc = gfx_puttext( &pos, &fmt, "Top left");671 rc = gfx_puttext(font, &pos, &fmt, "Top left"); 736 672 if (rc != EOK) { 737 673 printf("Error rendering text.\n"); … … 742 678 pos.y = rect.p0.y; 743 679 fmt.halign = gfx_halign_center; 744 rc = gfx_puttext( &pos, &fmt, "Top center");745 if (rc != EOK) 746 goto error; 747 748 pos.x = rect.p1.x ;680 rc = gfx_puttext(font, &pos, &fmt, "Top center"); 681 if (rc != EOK) 682 goto error; 683 684 pos.x = rect.p1.x - 1; 749 685 pos.y = rect.p0.y; 750 686 fmt.halign = gfx_halign_right; 751 rc = gfx_puttext( &pos, &fmt, "Top right");687 rc = gfx_puttext(font, &pos, &fmt, "Top right"); 752 688 if (rc != EOK) 753 689 goto error; … … 758 694 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 759 695 fmt.halign = gfx_halign_left; 760 rc = gfx_puttext( &pos, &fmt, "Center left");696 rc = gfx_puttext(font, &pos, &fmt, "Center left"); 761 697 if (rc != EOK) 762 698 goto error; … … 765 701 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 766 702 fmt.halign = gfx_halign_center; 767 rc = gfx_puttext( &pos, &fmt, "Center");768 if (rc != EOK) 769 goto error; 770 771 pos.x = rect.p1.x ;703 rc = gfx_puttext(font, &pos, &fmt, "Center"); 704 if (rc != EOK) 705 goto error; 706 707 pos.x = rect.p1.x - 1; 772 708 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 773 709 fmt.halign = gfx_halign_right; 774 rc = gfx_puttext( &pos, &fmt, "Center right");710 rc = gfx_puttext(font, &pos, &fmt, "Center right"); 775 711 if (rc != EOK) 776 712 goto error; … … 781 717 pos.y = rect.p1.y - 1; 782 718 fmt.halign = gfx_halign_left; 783 rc = gfx_puttext( &pos, &fmt, "Bottom left");719 rc = gfx_puttext(font, &pos, &fmt, "Bottom left"); 784 720 if (rc != EOK) 785 721 goto error; 786 722 787 723 pos.x = (rect.p0.x + rect.p1.x - 1) / 2; 788 pos.y = rect.p1.y ;724 pos.y = rect.p1.y - 1; 789 725 fmt.halign = gfx_halign_center; 790 rc = gfx_puttext( &pos, &fmt, "Bottom center");791 if (rc != EOK) 792 goto error; 793 794 pos.x = rect.p1.x ;795 pos.y = rect.p1.y ;726 rc = gfx_puttext(font, &pos, &fmt, "Bottom center"); 727 if (rc != EOK) 728 goto error; 729 730 pos.x = rect.p1.x - 1; 731 pos.y = rect.p1.y - 1; 796 732 fmt.halign = gfx_halign_right; 797 rc = gfx_puttext( &pos, &fmt, "Bottom right");733 rc = gfx_puttext(font, &pos, &fmt, "Bottom right"); 798 734 if (rc != EOK) 799 735 goto error; … … 802 738 803 739 gfx_text_fmt_init(&fmt); 804 fmt.font = font;805 740 806 741 for (i = 0; i < 8; i++) { 807 if (demo_is_text()) { 808 rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color); 809 if (rc != EOK) 810 goto error; 811 } else { 812 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0, 813 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color); 814 if (rc != EOK) 815 goto error; 816 } 742 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0, 743 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color); 744 if (rc != EOK) 745 goto error; 817 746 818 747 fmt.color = color; 819 fmt.underline = !fmt.underline;820 748 821 749 pos.x = w / 20; 822 750 pos.y = (6 + i) * h / 15; 823 rc = gfx_puttext( &pos, &fmt, "The quick brown fox jumps over the lazy dog.");751 rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog."); 824 752 if (rc != EOK) 825 753 goto error; … … 829 757 830 758 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); 759 fibril_usleep(500 * 1000); 913 760 if (quit) 914 761 break; … … 1007 854 } 1008 855 1009 demo_msleep(500); 856 fibril_usleep(500 * 1000); 857 1010 858 if (quit) 1011 859 break; … … 1054 902 goto error; 1055 903 1056 rc = demo_text_abbr(gc, w, h);1057 if (rc != EOK)1058 goto error;1059 1060 904 rc = demo_clip(gc, w, h); 1061 905 if (rc != EOK) … … 1073 917 static errno_t demo_console(void) 1074 918 { 919 console_ctrl_t *con = NULL; 1075 920 console_gc_t *cgc = NULL; 1076 921 gfx_context_t *gc; 1077 sysarg_t cols, rows;1078 errno_t rc; 1079 922 errno_t rc; 923 924 printf("Init console..\n"); 1080 925 con = console_init(stdin, stdout); 1081 926 if (con == NULL) 1082 927 return EIO; 1083 928 1084 rc = console_get_size(con, &cols, &rows); 1085 if (rc != EOK) 1086 return rc; 1087 929 printf("Create console GC\n"); 1088 930 rc = console_gc_create(con, stdout, &cgc); 1089 931 if (rc != EOK) … … 1092 934 gc = console_gc_get_ctx(cgc); 1093 935 1094 /* Currently console is always text. */ 1095 textmode = true; 1096 1097 rc = demo_loop(gc, cols, rows); 936 rc = demo_loop(gc, 80, 25); 1098 937 if (rc != EOK) 1099 938 return rc; … … 1104 943 1105 944 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 945 } 1119 946 … … 1121 948 static errno_t demo_ui(const char *display_spec) 1122 949 { 950 ui_t *ui = NULL; 1123 951 ui_wnd_params_t params; 1124 952 ui_window_t *window = NULL; … … 1127 955 gfx_rect_t wrect; 1128 956 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; 957 errno_t rc; 958 959 printf("Init UI..\n"); 1134 960 1135 961 rc = ui_create(display_spec, &ui); 1136 962 if (rc != EOK) { 1137 963 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 964 goto error; 1145 965 } … … 1153 973 params.caption = "GFX Demo"; 1154 974 1155 /* Do not decorate the window in fullscreen mode */1156 if (ui_is_fullscreen(ui))1157 params.style &= ~ui_wds_decorated;1158 1159 975 /* 1160 976 * Compute window rectangle such that application area corresponds 1161 977 * to rect 1162 978 */ 1163 ui_wdecor_rect_from_app( ui,params.style, &rect, &wrect);979 ui_wdecor_rect_from_app(params.style, &rect, &wrect); 1164 980 off = wrect.p0; 1165 981 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 982 1175 983 rc = ui_window_create(ui, ¶ms, &window); … … 1187 995 } 1188 996 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); 997 task_retval(0); 998 999 rc = demo_loop(gc, rect.p1.x, rect.p1.y); 1000 if (rc != EOK) 1001 goto error; 1002 1210 1003 ui_window_destroy(window); 1211 1004 ui_destroy(ui); … … 1229 1022 errno_t rc; 1230 1023 1024 printf("Init display..\n"); 1025 1231 1026 rc = display_open(display_svc, &display); 1232 1027 if (rc != EOK) { … … 1240 1035 params.rect.p1.x = 400; 1241 1036 params.rect.p1.y = 300; 1242 params.caption = "GFX Demo";1243 1037 1244 1038 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 1256 1050 task_retval(0); 1257 1051 1258 /* FIXME Assuming display service is not text mode. */1259 textmode = false;1260 1261 1052 rc = demo_loop(gc, 400, 300); 1262 1053 if (rc != EOK) … … 1273 1064 } 1274 1065 1275 static void demo_quit(void)1276 { 1277 fibril_mutex_lock(&quit_lock);1066 static void wnd_close_event(void *arg) 1067 { 1068 printf("Close event\n"); 1278 1069 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 1070 } 1308 1071 1309 1072 static void wnd_kbd_event(void *arg, kbd_event_t *event) 1310 1073 { 1311 (void)arg; 1312 demo_kbd_event(event); 1074 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 1075 if (event->type == KEY_PRESS) 1076 quit = true; 1313 1077 } 1314 1078 1315 1079 static void uiwnd_close_event(ui_window_t *window, void *arg) 1316 1080 { 1317 demo_quit(); 1081 printf("Close event\n"); 1082 quit = true; 1318 1083 } 1319 1084 1320 1085 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 1321 1086 { 1322 (void)window;1323 (void)arg;1324 demo_kbd_event(event);1087 printf("Keyboard event type=%d key=%d\n", event->type, event->key); 1088 if (event->type == KEY_PRESS) 1089 quit = true; 1325 1090 } 1326 1091 … … 1334 1099 errno_t rc; 1335 1100 const char *display_svc = DISPLAY_DEFAULT; 1336 const char *ui_display_spec = UI_ANY_DEFAULT;1337 1101 int i; 1338 1102 … … 1347 1111 } 1348 1112 1349 display_svc = ui_display_spec =argv[i++];1113 display_svc = argv[i++]; 1350 1114 } else { 1351 1115 printf("Invalid option '%s'.\n", argv[i]); … … 1364 1128 return 1; 1365 1129 } else if (str_cmp(argv[i], "ui") == 0) { 1366 rc = demo_ui( ui_display_spec);1130 rc = demo_ui(display_svc); 1367 1131 if (rc != EOK) 1368 1132 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.