iPXE
ansi_screen.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <curses.h>
3 #include <ipxe/ansicol.h>
4 #include <ipxe/console.h>
5 
6 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
7 
8 static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
9 static void ansiscr_movetoyx(struct _curses_screen *scr,
10  unsigned int y, unsigned int x) __nonnull;
11 static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
12 
13 static unsigned int saved_usage;
14 
15 static void ansiscr_attrs ( struct _curses_screen *scr, attr_t attrs ) {
16  int bold = ( attrs & A_BOLD );
17  attr_t cpair = PAIR_NUMBER ( attrs );
18 
19  if ( scr->attrs != attrs ) {
20  scr->attrs = attrs;
21  /* Reset attributes and set/clear bold as appropriate */
22  printf ( "\033[0;%dm", ( bold ? 1 : 22 ) );
23  /* Set foreground and background colours */
24  ansicol_set_pair ( cpair );
25  }
26 }
27 
28 static void ansiscr_reset ( struct _curses_screen *scr ) {
29  /* Reset terminal attributes and clear screen */
30  scr->attrs = 0;
31  scr->curs_x = 0;
32  scr->curs_y = 0;
33  printf ( "\0330m" );
35  printf ( "\033[2J" );
36 }
37 
38 static void ansiscr_init ( struct _curses_screen *scr ) {
40  ansiscr_reset ( scr );
41 }
42 
43 static void ansiscr_exit ( struct _curses_screen *scr ) {
44  ansiscr_reset ( scr );
46 }
47 
48 static void ansiscr_erase ( struct _curses_screen *scr, attr_t attrs ) {
49  ansiscr_attrs ( scr, attrs );
50  printf ( "\033[2J" );
51 }
52 
53 static void ansiscr_movetoyx ( struct _curses_screen *scr,
54  unsigned int y, unsigned int x ) {
55  if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
56  /* ANSI escape sequence to update cursor position */
57  printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
58  scr->curs_x = x;
59  scr->curs_y = y;
60  }
61 }
62 
63 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
64  unsigned int character = ( c & A_CHARTEXT );
65  attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
66 
67  /* Update attributes if changed */
68  ansiscr_attrs ( scr, attrs );
69 
70  /* Print the actual character */
71  putchar ( character );
72 
73  /* Update expected cursor position */
74  if ( ++(scr->curs_x) == COLS ) {
75  scr->curs_x = 0;
76  ++scr->curs_y;
77  }
78 }
79 
80 static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
81  return getchar();
82 }
83 
84 static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
85  return iskey();
86 }
87 
88 static void ansiscr_cursor ( struct _curses_screen *scr __unused,
89  int visibility ) {
90  printf ( "\033[?25%c", ( visibility ? 'h' : 'l' ) );
91 }
92 
94  .init = ansiscr_init,
95  .exit = ansiscr_exit,
96  .erase = ansiscr_erase,
97  .movetoyx = ansiscr_movetoyx,
98  .putc = ansiscr_putc,
99  .getc = ansiscr_getc,
100  .peek = ansiscr_peek,
101  .cursor = ansiscr_cursor,
102 };
MuCurses header file.
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
Curses SCREEN object.
Definition: curses.h:33
#define A_ATTRIBUTES
Definition: curses.h:146
static void ansiscr_exit(struct _curses_screen *scr)
Definition: ansi_screen.c:43
#define CONSOLE_USAGE_TUI
Text-based user interface.
Definition: console.h:147
static int ansiscr_getc(struct _curses_screen *scr __unused)
Definition: ansi_screen.c:80
static void ansiscr_cursor(struct _curses_screen *scr __unused, int visibility)
Definition: ansi_screen.c:88
void putchar(int character)
Write a single character to each console device.
Definition: console.c:27
static __always_inline void off_t int c
Definition: efi_uaccess.h:87
static bool ansiscr_peek(struct _curses_screen *scr __unused)
Definition: ansi_screen.c:84
void ansicol_set_pair(unsigned int cpair)
Set ANSI foreground and background colour.
Definition: ansicol.c:89
static void ansiscr_erase(struct _curses_screen *scr, attr_t attrs)
Definition: ansi_screen.c:48
uint32_t attr_t
Definition: curses.h:30
#define __nonnull
Declare a function's pointer parameters as non-null - i.e.
Definition: compiler.h:592
static void ansiscr_movetoyx(struct _curses_screen *scr, unsigned int y, unsigned int x) __nonnull
Definition: ansi_screen.c:53
void(* init)(struct _curses_screen *scr)
Definition: curses.h:39
uint32_t attrs
Extended attributes (optional)
Definition: memmap.c:32
User interaction.
int getchar(void)
Read a single character from any console.
Definition: console.c:82
SCREEN _ansi_screen
Definition: ansi_screen.c:93
static void ansiscr_reset(struct _curses_screen *scr) __nonnull
Definition: ansi_screen.c:28
unsigned int curs_x
Current cursor position.
Definition: curses.h:35
static void ansiscr_init(struct _curses_screen *scr)
Definition: ansi_screen.c:38
#define COLS
Definition: curses.h:111
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define CPAIR_DEFAULT
Default colour pair.
Definition: ansicol.h:37
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
attr_t attrs
Current attribute.
Definition: curses.h:37
static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull
Definition: ansi_screen.c:63
#define A_COLOR
Definition: curses.h:149
unsigned int curs_y
Definition: curses.h:35
#define A_CHARTEXT
Definition: curses.h:147
static int console_set_usage(int usage)
Set console usage.
Definition: console.h:187
static unsigned int saved_usage
Definition: ansi_screen.c:13
uint32_t chtype
Definition: curses.h:29
static void ansiscr_attrs(struct _curses_screen *scr, attr_t attrs)
Definition: ansi_screen.c:15
#define A_BOLD
Definition: curses.h:138
#define PAIR_NUMBER(attrs)
Definition: curses.h:153
int iskey(void)
Check for available input on any console.
Definition: console.c:127
ANSI colours.