Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ansi_screen.c 1.9KB

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