diff options
Diffstat (limited to 'src/ppu.c')
-rw-r--r-- | src/ppu.c | 54 |
1 files changed, 43 insertions, 11 deletions
@@ -4,6 +4,7 @@ | |||
4 | 4 | ||
5 | #include <linux/fb.h> | 5 | #include <linux/fb.h> |
6 | #include <sys/ioctl.h> | 6 | #include <sys/ioctl.h> |
7 | #include <sys/kd.h> | ||
7 | #include <sys/mman.h> | 8 | #include <sys/mman.h> |
8 | 9 | ||
9 | #include "ppu.h" | 10 | #include "ppu.h" |
@@ -89,6 +90,46 @@ redraw_screen(void) { | |||
89 | } | 90 | } |
90 | } | 91 | } |
91 | 92 | ||
93 | static struct termios prev_t; | ||
94 | |||
95 | void | ||
96 | set_tty(bool graphics) { | ||
97 | // Disable TTY echo and set graphics mode. | ||
98 | int tty = open("/dev/tty0", O_RDWR); | ||
99 | if (!tty) { | ||
100 | fprintf(stderr,"error: couldn't open tty\n"); | ||
101 | exit(EXIT_FAILURE); | ||
102 | } | ||
103 | |||
104 | if (graphics) { | ||
105 | if (ioctl(tty, KDSETMODE, KD_GRAPHICS)) { | ||
106 | fprintf(stderr,"error: setting graphics mode failed\n"); | ||
107 | exit(EXIT_FAILURE); | ||
108 | } | ||
109 | struct termios t; | ||
110 | if (tcgetattr(STDIN_FILENO, &t)) { | ||
111 | fprintf(stderr, "error: couldn't disable terminal echo\n"); | ||
112 | exit(EXIT_FAILURE); | ||
113 | } | ||
114 | prev_t = t; | ||
115 | t.c_lflag &= ~((tcflag_t) ECHO); | ||
116 | if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { | ||
117 | fprintf(stderr, "error: couldn't disable terminal echo\n"); | ||
118 | exit(EXIT_FAILURE); | ||
119 | } | ||
120 | } else { | ||
121 | if (ioctl(tty, KDSETMODE, KD_TEXT)) { | ||
122 | fprintf(stderr,"error: setting text mode failed\n"); | ||
123 | exit(EXIT_FAILURE); | ||
124 | } | ||
125 | if (tcsetattr(STDIN_FILENO, TCSANOW, &prev_t)) { | ||
126 | fprintf(stderr, "error: couldn't restore terminal attributes\n"); | ||
127 | exit(EXIT_FAILURE); | ||
128 | } | ||
129 | } | ||
130 | close(tty); | ||
131 | } | ||
132 | |||
92 | int | 133 | int |
93 | ppu_init(void) { | 134 | ppu_init(void) { |
94 | // Open frambuffer and get the size. | 135 | // Open frambuffer and get the size. |
@@ -122,17 +163,8 @@ ppu_init(void) { | |||
122 | exit(EXIT_FAILURE); | 163 | exit(EXIT_FAILURE); |
123 | } | 164 | } |
124 | 165 | ||
125 | // Disable echo. | 166 | // Prepare TTY for graphics mode. |
126 | struct termios t; | 167 | set_tty(true); |
127 | if (tcgetattr(STDIN_FILENO, &t)) { | ||
128 | fprintf(stderr, "error: couldn't disable terminal echo\n"); | ||
129 | exit(EXIT_FAILURE); | ||
130 | } | ||
131 | t.c_lflag &= ~((tcflag_t) ECHO); | ||
132 | if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) { | ||
133 | fprintf(stderr, "error: couldn't disable terminal echo\n"); | ||
134 | exit(EXIT_FAILURE); | ||
135 | } | ||
136 | 168 | ||
137 | // Initialize default palette. | 169 | // Initialize default palette. |
138 | palette[0] = 0x444444; | 170 | palette[0] = 0x444444; |