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