Changes in uspace/app/gfxdemo/gfxdemo.c [0a411bbf:da15002] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
r0a411bbf 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 ui_t *ui;81 82 /** Determine if we are running in text mode.83 *84 * @param w Screen width85 * @param h Screen height86 * @return @c true iff we are running in text mode87 */88 static bool demo_is_text(gfx_coord_t w, gfx_coord_t h)89 {90 // XXX Need a proper way to determine text mode91 return w <= 80;92 }93 94 /** Sleep until timeout or quit request.95 *96 * @param msec Number of microseconds to sleep for97 */98 static void demo_msleep(unsigned msec)99 {100 errno_t rc;101 usec_t usec;102 cons_event_t cevent;103 104 if (ui != NULL)105 ui_unlock(ui);106 fibril_mutex_lock(&quit_lock);107 if (!quit) {108 if (con != NULL) {109 usec = (usec_t)msec * 1000;110 while (usec > 0 && !quit) {111 rc = console_get_event_timeout(con, &cevent, &usec);112 if (rc == EOK) {113 if (cevent.type == CEV_KEY) {114 fibril_mutex_unlock(&quit_lock);115 demo_kbd_event(&cevent.ev.key);116 fibril_mutex_lock(&quit_lock);117 }118 }119 }120 } else {121 (void) fibril_condvar_wait_timeout(&quit_cv, &quit_lock,122 (usec_t)msec * 1000);123 }124 }125 fibril_mutex_unlock(&quit_lock);126 if (ui != NULL)127 ui_lock(ui);128 }129 74 130 75 /** Clear screen. … … 270 215 271 216 if (font != NULL) { 272 if (demo_is_text(w, h)) { 273 rc = gfx_color_new_ega(0x1e, &color); 274 if (rc != EOK) 275 goto error; 276 } else { 277 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 278 if (rc != EOK) 279 goto error; 280 } 217 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 218 if (rc != EOK) 219 goto error; 281 220 282 221 gfx_text_fmt_init(&fmt); 283 fmt.font = font;284 222 fmt.color = color; 285 223 fmt.halign = gfx_halign_center; … … 287 225 288 226 pos.x = w / 2; 289 pos.y = h ;290 rc = gfx_puttext( &pos, &fmt, text);227 pos.y = h - 1; 228 rc = gfx_puttext(font, &pos, &fmt, text); 291 229 if (rc != EOK) { 292 230 printf("Error rendering text.\n"); … … 359 297 gfx_color_delete(color); 360 298 361 demo_msleep(500); 299 fibril_usleep(500 * 1000); 300 362 301 if (quit) 363 302 break; … … 393 332 for (j = 0; j < h; j++) { 394 333 pixelmap_put_pixel(&pixelmap, i, j, 395 PIXEL( 255, (i % 30) < 3 ? 255 : 0,334 PIXEL(0, (i % 30) < 3 ? 255 : 0, 396 335 (j % 30) < 3 ? 255 : 0, i / 2)); 397 336 } … … 429 368 k = i * i + j * j; 430 369 pixelmap_put_pixel(&pixelmap, i, j, 431 PIXEL( 255, k, k, k));370 PIXEL(0, k, k, k)); 432 371 } 433 372 } … … 464 403 k = i * i + j * j; 465 404 pixelmap_put_pixel(&pixelmap, i, j, 466 k < w * w / 2 ? PIXEL( 255, 0, 255, 0) :467 PIXEL( 255, 255, 0, 255));405 k < w * w / 2 ? PIXEL(0, 0, 255, 0) : 406 PIXEL(0, 255, 0, 255)); 468 407 } 469 408 } … … 520 459 if (rc != EOK) 521 460 goto error; 522 523 demo_msleep(250); 461 fibril_usleep(250 * 1000); 462 524 463 if (quit) 525 464 goto out; … … 581 520 } 582 521 583 demo_msleep(500); 522 fibril_usleep(500 * 1000); 523 584 524 if (quit) 585 525 break; … … 621 561 params.rect.p1.y = 40; 622 562 params.flags = bmpf_color_key; 623 params.key_color = PIXEL( 255, 255, 0, 255);563 params.key_color = PIXEL(0, 255, 0, 255); 624 564 625 565 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); … … 641 581 } 642 582 643 demo_msleep(500); 583 fibril_usleep(500 * 1000); 584 644 585 if (quit) 645 586 break; … … 719 660 gfx_color_delete(color); 720 661 721 if (demo_is_text(w, h)) { 722 rc = gfx_color_new_ega(0x1f, &color); 723 if (rc != EOK) 724 goto error; 725 } else { 726 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 727 if (rc != EOK) 728 goto error; 729 } 662 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 663 if (rc != EOK) 664 goto error; 730 665 731 666 gfx_text_fmt_init(&fmt); 732 fmt.font = font;733 667 fmt.color = color; 734 668 735 669 pos.x = rect.p0.x; 736 670 pos.y = rect.p0.y; 737 rc = gfx_puttext( &pos, &fmt, "Top left");671 rc = gfx_puttext(font, &pos, &fmt, "Top left"); 738 672 if (rc != EOK) { 739 673 printf("Error rendering text.\n"); … … 744 678 pos.y = rect.p0.y; 745 679 fmt.halign = gfx_halign_center; 746 rc = gfx_puttext( &pos, &fmt, "Top center");747 if (rc != EOK) 748 goto error; 749 750 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; 751 685 pos.y = rect.p0.y; 752 686 fmt.halign = gfx_halign_right; 753 rc = gfx_puttext( &pos, &fmt, "Top right");687 rc = gfx_puttext(font, &pos, &fmt, "Top right"); 754 688 if (rc != EOK) 755 689 goto error; … … 760 694 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 761 695 fmt.halign = gfx_halign_left; 762 rc = gfx_puttext( &pos, &fmt, "Center left");696 rc = gfx_puttext(font, &pos, &fmt, "Center left"); 763 697 if (rc != EOK) 764 698 goto error; … … 767 701 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 768 702 fmt.halign = gfx_halign_center; 769 rc = gfx_puttext( &pos, &fmt, "Center");770 if (rc != EOK) 771 goto error; 772 773 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; 774 708 pos.y = (rect.p0.y + rect.p1.y - 1) / 2; 775 709 fmt.halign = gfx_halign_right; 776 rc = gfx_puttext( &pos, &fmt, "Center right");710 rc = gfx_puttext(font, &pos, &fmt, "Center right"); 777 711 if (rc != EOK) 778 712 goto error; … … 783 717 pos.y = rect.p1.y - 1; 784 718 fmt.halign = gfx_halign_left; 785 rc = gfx_puttext( &pos, &fmt, "Bottom left");719 rc = gfx_puttext(font, &pos, &fmt, "Bottom left"); 786 720 if (rc != EOK) 787 721 goto error; 788 722 789 723 pos.x = (rect.p0.x + rect.p1.x - 1) / 2; 790 pos.y = rect.p1.y ;724 pos.y = rect.p1.y - 1; 791 725 fmt.halign = gfx_halign_center; 792 rc = gfx_puttext( &pos, &fmt, "Bottom center");793 if (rc != EOK) 794 goto error; 795 796 pos.x = rect.p1.x ;797 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; 798 732 fmt.halign = gfx_halign_right; 799 rc = gfx_puttext( &pos, &fmt, "Bottom right");733 rc = gfx_puttext(font, &pos, &fmt, "Bottom right"); 800 734 if (rc != EOK) 801 735 goto error; … … 804 738 805 739 gfx_text_fmt_init(&fmt); 806 fmt.font = font;807 740 808 741 for (i = 0; i < 8; i++) { 809 if (demo_is_text(w, h)) { 810 rc = gfx_color_new_ega(i != 0 ? i : 0x10, &color); 811 if (rc != EOK) 812 goto error; 813 } else { 814 rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0, 815 (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color); 816 if (rc != EOK) 817 goto error; 818 } 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; 819 746 820 747 fmt.color = color; 821 fmt.underline = !fmt.underline;822 748 823 749 pos.x = w / 20; 824 750 pos.y = (6 + i) * h / 15; 825 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."); 826 752 if (rc != EOK) 827 753 goto error; … … 831 757 832 758 for (i = 0; i < 10; i++) { 833 demo_msleep(500); 834 if (quit) 835 break; 836 } 837 838 return EOK; 839 error: 840 return rc; 841 } 842 843 /** Run text abbreviation demo on a graphic context. 844 * 845 * @param gc Graphic context 846 * @param w Width 847 * @param h Height 848 */ 849 static errno_t demo_text_abbr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h) 850 { 851 gfx_color_t *color = NULL; 852 gfx_rect_t rect; 853 gfx_coord2_t pos; 854 gfx_text_fmt_t fmt; 855 int i; 856 errno_t rc; 857 858 if (quit) 859 return EOK; 860 861 rc = demo_begin(gc, w, h, "Text abbreviation"); 862 if (rc != EOK) 863 goto error; 864 865 for (i = 0; i < 11; i++) { 866 867 rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color); 868 if (rc != EOK) 869 goto error; 870 871 rc = gfx_set_color(gc, color); 872 if (rc != EOK) 873 goto error; 874 875 rect.p0.x = w / 20; 876 rect.p0.y = (2 + 2 * i) * h / 25; 877 rect.p1.x = w - w / 20 - w * i / 12; 878 rect.p1.y = (3 + 2 * i) * h / 25; 879 880 rc = gfx_fill_rect(gc, &rect); 881 if (rc != EOK) 882 goto error; 883 884 gfx_color_delete(color); 885 886 if (demo_is_text(w, h)) { 887 rc = gfx_color_new_ega(0x1f, &color); 888 if (rc != EOK) 889 goto error; 890 } else { 891 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, 892 &color); 893 if (rc != EOK) 894 goto error; 895 } 896 897 gfx_text_fmt_init(&fmt); 898 fmt.font = font; 899 fmt.color = color; 900 fmt.abbreviate = true; 901 fmt.width = rect.p1.x - rect.p0.x; 902 903 pos.x = rect.p0.x; 904 pos.y = rect.p0.y; 905 rc = gfx_puttext(&pos, &fmt, 906 "The quick brow fox jumps over the lazy dog!"); 907 if (rc != EOK) { 908 printf("Error rendering text.\n"); 909 goto error; 910 } 911 } 912 913 for (i = 0; i < 10; i++) { 914 demo_msleep(500); 759 fibril_usleep(500 * 1000); 915 760 if (quit) 916 761 break; … … 1009 854 } 1010 855 1011 demo_msleep(500); 856 fibril_usleep(500 * 1000); 857 1012 858 if (quit) 1013 859 break; … … 1056 902 goto error; 1057 903 1058 rc = demo_text_abbr(gc, w, h);1059 if (rc != EOK)1060 goto error;1061 1062 904 rc = demo_clip(gc, w, h); 1063 905 if (rc != EOK) … … 1075 917 static errno_t demo_console(void) 1076 918 { 919 console_ctrl_t *con = NULL; 1077 920 console_gc_t *cgc = NULL; 1078 921 gfx_context_t *gc; 1079 sysarg_t cols, rows;1080 errno_t rc; 1081 922 errno_t rc; 923 924 printf("Init console..\n"); 1082 925 con = console_init(stdin, stdout); 1083 926 if (con == NULL) 1084 927 return EIO; 1085 928 1086 rc = console_get_size(con, &cols, &rows); 1087 if (rc != EOK) 1088 return rc; 1089 929 printf("Create console GC\n"); 1090 930 rc = console_gc_create(con, stdout, &cgc); 1091 931 if (rc != EOK) … … 1094 934 gc = console_gc_get_ctx(cgc); 1095 935 1096 rc = demo_loop(gc, cols, rows);936 rc = demo_loop(gc, 80, 25); 1097 937 if (rc != EOK) 1098 938 return rc; … … 1103 943 1104 944 return EOK; 1105 }1106 1107 static errno_t demo_ui_fibril(void *arg)1108 {1109 demo_ui_args_t *args = (demo_ui_args_t *)arg;1110 errno_t rc;1111 1112 ui_lock(args->ui);1113 rc = demo_loop(args->gc, args->dims.x, args->dims.y);1114 ui_unlock(args->ui);1115 ui_quit(args->ui);1116 return rc;1117 945 } 1118 946 … … 1120 948 static errno_t demo_ui(const char *display_spec) 1121 949 { 950 ui_t *ui = NULL; 1122 951 ui_wnd_params_t params; 1123 952 ui_window_t *window = NULL; … … 1126 955 gfx_rect_t wrect; 1127 956 gfx_coord2_t off; 1128 gfx_rect_t ui_rect; 1129 gfx_coord2_t dims; 1130 demo_ui_args_t args; 1131 fid_t fid; 1132 errno_t rc; 957 errno_t rc; 958 959 printf("Init UI..\n"); 1133 960 1134 961 rc = ui_create(display_spec, &ui); 1135 962 if (rc != EOK) { 1136 963 printf("Error initializing UI (%s)\n", display_spec); 1137 goto error;1138 }1139 1140 rc = ui_get_rect(ui, &ui_rect);1141 if (rc != EOK) {1142 printf("Error getting display size.\n");1143 964 goto error; 1144 965 } … … 1152 973 params.caption = "GFX Demo"; 1153 974 1154 /* Do not decorate the window in fullscreen mode */1155 if (ui_is_fullscreen(ui))1156 params.style &= ~ui_wds_decorated;1157 1158 975 /* 1159 976 * Compute window rectangle such that application area corresponds 1160 977 * to rect 1161 978 */ 1162 ui_wdecor_rect_from_app( ui,params.style, &rect, &wrect);979 ui_wdecor_rect_from_app(params.style, &rect, &wrect); 1163 980 off = wrect.p0; 1164 981 gfx_rect_rtranslate(&off, &wrect, ¶ms.rect); 1165 1166 gfx_rect_dims(&ui_rect, &dims);1167 1168 /* Make sure window is not larger than the entire screen */1169 if (params.rect.p1.x > dims.x)1170 params.rect.p1.x = dims.x;1171 if (params.rect.p1.y > dims.y)1172 params.rect.p1.y = dims.y;1173 982 1174 983 rc = ui_window_create(ui, ¶ms, &window); … … 1186 995 } 1187 996 1188 ui_window_get_app_rect(window, &rect); 1189 gfx_rect_dims(&rect, &dims); 1190 1191 if (!ui_is_fullscreen(ui)) 1192 task_retval(0); 1193 1194 args.gc = gc; 1195 args.dims = dims; 1196 args.ui = ui; 1197 1198 fid = fibril_create(demo_ui_fibril, (void *)&args); 1199 if (fid == 0) { 1200 rc = ENOMEM; 1201 goto error; 1202 } 1203 1204 fibril_add_ready(fid); 1205 1206 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 1207 1003 ui_window_destroy(window); 1208 1004 ui_destroy(ui); … … 1226 1022 errno_t rc; 1227 1023 1024 printf("Init display..\n"); 1025 1228 1026 rc = display_open(display_svc, &display); 1229 1027 if (rc != EOK) { … … 1237 1035 params.rect.p1.x = 400; 1238 1036 params.rect.p1.y = 300; 1239 params.caption = "GFX Demo";1240 1037 1241 1038 rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window); … … 1267 1064 } 1268 1065 1269 static void demo_quit(void)1270 { 1271 fibril_mutex_lock(&quit_lock);1066 static void wnd_close_event(void *arg) 1067 { 1068 printf("Close event\n"); 1272 1069 quit = true; 1273 fibril_mutex_unlock(&quit_lock);1274 fibril_condvar_broadcast(&quit_cv);1275 }1276 1277 static void wnd_close_event(void *arg)1278 {1279 demo_quit();1280 }1281 1282 static void demo_kbd_event(kbd_event_t *event)1283 {1284 if (event->type == KEY_PRESS) {1285 /* Ctrl-Q */1286 if ((event->mods & KM_CTRL) != 0 &&1287 (event->mods & KM_ALT) == 0 &&1288 (event->mods & KM_SHIFT) == 0 &&1289 event->key == KC_Q) {1290 demo_quit();1291 }1292 1293 /* Escape */1294 if ((event->mods & KM_CTRL) == 0 &&1295 (event->mods & KM_ALT) == 0 &&1296 (event->mods & KM_SHIFT) == 0 &&1297 event->key == KC_ESCAPE) {1298 demo_quit();1299 }1300 }1301 1070 } 1302 1071 1303 1072 static void wnd_kbd_event(void *arg, kbd_event_t *event) 1304 1073 { 1305 (void)arg; 1306 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; 1307 1077 } 1308 1078 1309 1079 static void uiwnd_close_event(ui_window_t *window, void *arg) 1310 1080 { 1311 demo_quit(); 1081 printf("Close event\n"); 1082 quit = true; 1312 1083 } 1313 1084 1314 1085 static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 1315 1086 { 1316 (void)window;1317 (void)arg;1318 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; 1319 1090 } 1320 1091 … … 1328 1099 errno_t rc; 1329 1100 const char *display_svc = DISPLAY_DEFAULT; 1330 const char *ui_display_spec = UI_ANY_DEFAULT;1331 1101 int i; 1332 1102 … … 1341 1111 } 1342 1112 1343 display_svc = ui_display_spec =argv[i++];1113 display_svc = argv[i++]; 1344 1114 } else { 1345 1115 printf("Invalid option '%s'.\n", argv[i]); … … 1358 1128 return 1; 1359 1129 } else if (str_cmp(argv[i], "ui") == 0) { 1360 rc = demo_ui( ui_display_spec);1130 rc = demo_ui(display_svc); 1361 1131 if (rc != EOK) 1362 1132 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.