Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ansi_screen.c 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <ipxe/console.h>
  4. FILE_LICENCE ( GPL2_OR_LATER );
  5. static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
  6. static void ansiscr_movetoyx(struct _curses_screen *scr,
  7. unsigned int y, unsigned int x) __nonnull;
  8. static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
  9. unsigned short _COLS = 80;
  10. unsigned short _LINES = 24;
  11. static unsigned int saved_usage;
  12. static void ansiscr_reset ( struct _curses_screen *scr ) {
  13. /* Reset terminal attributes and clear screen */
  14. scr->attrs = 0;
  15. scr->curs_x = 0;
  16. scr->curs_y = 0;
  17. printf ( "\033[0m" );
  18. }
  19. static void ansiscr_init ( struct _curses_screen *scr ) {
  20. saved_usage = console_set_usage ( CONSOLE_USAGE_TUI );
  21. ansiscr_reset ( scr );
  22. }
  23. static void ansiscr_exit ( struct _curses_screen *scr ) {
  24. ansiscr_reset ( scr );
  25. console_set_usage ( saved_usage );
  26. }
  27. static void ansiscr_movetoyx ( struct _curses_screen *scr,
  28. unsigned int y, unsigned int x ) {
  29. if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
  30. /* ANSI escape sequence to update cursor position */
  31. printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
  32. scr->curs_x = x;
  33. scr->curs_y = y;
  34. }
  35. }
  36. static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
  37. unsigned int character = ( c & A_CHARTEXT );
  38. attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
  39. int bold = ( attrs & A_BOLD );
  40. attr_t cpair = PAIR_NUMBER ( attrs );
  41. short fcol;
  42. short bcol;
  43. /* Update attributes if changed */
  44. if ( attrs != scr->attrs ) {
  45. scr->attrs = attrs;
  46. pair_content ( cpair, &fcol, &bcol );
  47. /* ANSI escape sequence to update character attributes */
  48. printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
  49. }
  50. /* Print the actual character */
  51. putchar ( character );
  52. /* Update expected cursor position */
  53. if ( ++(scr->curs_x) == _COLS ) {
  54. scr->curs_x = 0;
  55. ++scr->curs_y;
  56. }
  57. }
  58. static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
  59. return getchar();
  60. }
  61. static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
  62. return iskey();
  63. }
  64. SCREEN _ansi_screen = {
  65. .init = ansiscr_init,
  66. .exit = ansiscr_exit,
  67. .movetoyx = ansiscr_movetoyx,
  68. .putc = ansiscr_putc,
  69. .getc = ansiscr_getc,
  70. .peek = ansiscr_peek,
  71. };