7
|
1 #ifndef CONIO_H
|
|
2 #define CONIO_H
|
|
3
|
|
4 #include <termios.h>
|
|
5 #include <unistd.h>
|
|
6 #include <stdio.h>
|
|
7 #include <fcntl.h>
|
|
8 #include <stdlib.h>
|
|
9 #include <string.h>
|
|
10
|
|
11 #define cprintf printf
|
|
12 #define cscanf scanf
|
|
13 #define cgets gets
|
|
14
|
|
15 #define CLEAR "\x1b[2J"
|
|
16 #define SET11 "\x1b[1;1f"
|
|
17 #define CURSOR_UP "\x1b[1A"
|
|
18 #define ERASE_LINE "\x1b[2K"
|
|
19 #define BLINK_SLOW "\x1b[5m"
|
|
20 #define BLINK_RAPID "\x1b[6m"
|
|
21 #define CC_CLEAR "\x1b[0m"
|
|
22
|
|
23 #define BLACK "\x1b[30m"
|
|
24 #define RED "\x1b[31m"
|
|
25 #define GREEN "\x1b[32m"
|
|
26 #define YELLOW "\x1b[33m"
|
|
27 #define BLUE "\x1b[34m"
|
|
28 #define MAGENTA "\x1b[35m"
|
|
29 #define CYAN "\x1b[36m"
|
|
30 #define WHITE "\x1b[37m"
|
|
31
|
|
32 #define IBLACK "\x1b[30;1m"
|
|
33 #define IRED "\x1b[31;1m"
|
|
34 #define IGREEN "\x1b[32;1m"
|
|
35 #define IYELLOW "\x1b[33;1m"
|
|
36 #define IBLUE "\x1b[34;1m"
|
|
37 #define IMAGENTA "\x1b[35;1m"
|
|
38 #define ICYAN "\x1b[36;1m"
|
|
39 #define IWHITE "\x1b[37;1m"
|
|
40
|
|
41 #define BGC_BLACK "\x1b[40m"
|
|
42 #define BGC_RED "\x1b[41m"
|
|
43 #define BGC_GREEN "\x1b[42m"
|
|
44 #define BGC_YELLOW "\x1b[43m"
|
|
45 #define BGC_BLUE "\x1b[44m"
|
|
46 #define BGC_MAGENTA "\x1b[45m"
|
|
47 #define BGC_CYAN "\x1b[46m"
|
|
48 #define BGC_WHITE "\x1b[47m"
|
|
49
|
|
50 #define BGC_IBLACK "\x1b[40;1m"
|
|
51 #define BGC_IRED "\x1b[41;1m"
|
|
52 #define BGC_IGREEN "\x1b[42;1m"
|
|
53 #define BGC_IYELLOW "\x1b[43;1m"
|
|
54 #define BGC_IBLUE "\x1b[44;1m"
|
|
55 #define BGC_IMAGENTA "\x1b[45;1m"
|
|
56 #define BGC_ICYAN "\x1b[46;1m"
|
|
57 #define BGC_IWHITE "\x1b[47;1m"
|
|
58
|
|
59 static struct termios oldterm, newterm;
|
|
60
|
|
61 void initTermios(int echo)
|
|
62 {
|
|
63 tcgetattr(0, &oldterm);
|
|
64 newterm = oldterm;
|
|
65 newterm.c_lflag &= ~ICANON;
|
|
66 newterm.c_lflag &= echo ? ECHO : ~ECHO;
|
|
67 tcsetattr(0, TCSANOW, &newterm);
|
|
68 }
|
|
69 void resetTermios(void)
|
|
70 {
|
|
71 tcsetattr(0, TCSANOW, &oldterm);
|
|
72 }
|
|
73
|
|
74 int getch_(int echo)
|
|
75 {
|
|
76 int ch;
|
|
77 initTermios(echo);
|
|
78 ch = getchar();
|
|
79 resetTermios();
|
|
80 return ch;
|
|
81 }
|
|
82
|
|
83 void cagxy(unsigned int x, unsigned int y)
|
|
84 {
|
|
85 printf("%s\x1b[%d;%df", CLEAR, y, x);
|
|
86 }
|
|
87
|
|
88 void clrscr()
|
|
89 {
|
|
90 printf("%s%s",CLEAR, SET11);
|
|
91 }
|
|
92
|
|
93 int getch(void)
|
|
94 {
|
|
95 return getch_(0);
|
|
96 }
|
|
97
|
|
98 int getche(void)
|
|
99 {
|
|
100 return getch_(1);
|
|
101 }
|
|
102
|
|
103 void gotox(unsigned int x)
|
|
104 {
|
|
105 printf("\x1b[%dG", x);
|
|
106 }
|
|
107
|
|
108 void gotoxy(unsigned int x, unsigned int y)
|
|
109 {
|
|
110 printf("\x1b[%d;%df", y, x);
|
|
111 }
|
|
112
|
|
113 void nocursor()
|
|
114 {
|
|
115 printf("\x1b[?25l");
|
|
116 }
|
|
117
|
|
118 void reset_video()
|
|
119 {
|
|
120 printf("\x1b[0m");
|
|
121 }
|
|
122
|
|
123 void showcursor()
|
|
124 {
|
|
125 printf("\x1b[?25h");
|
|
126 }
|
|
127
|
|
128 void textcolor(char *color)
|
|
129 {
|
|
130 printf("%s",color);
|
|
131 }
|
|
132
|
|
133 void textbackground(char color[11])
|
|
134 {
|
|
135 char col[11];
|
|
136 strcpy(col,color);
|
|
137 col[2]='4';
|
|
138 printf("%s",col);
|
|
139 }
|
|
140
|
|
141 void delline()
|
|
142 {
|
|
143 printf("%s%s", ERASE_LINE, CURSOR_UP);
|
|
144 }
|
|
145
|
|
146 void clreol()
|
|
147 {
|
|
148 printf("%s",CLEAR);
|
|
149 }
|
|
150 int putch(const char c)
|
|
151 {
|
|
152 printf("%c",c);
|
|
153 return (int)c;
|
|
154 }
|
|
155
|
|
156 int cputs(const char*str)
|
|
157 {
|
|
158 printf(str);
|
|
159 return 0;
|
|
160 }
|
|
161
|
|
162
|
|
163 int wherexy(int *x, int *y)
|
|
164 {
|
|
165 printf("\033[6n");
|
|
166 if(getch() != '\x1B') return 0;
|
|
167 if(getch() != '\x5B') return 0;
|
|
168 int in;
|
|
169 int ly = 0;
|
|
170 while((in = getch()) != ';')
|
|
171 ly = ly * 10 + in - '0';
|
|
172 int lx = 0;
|
|
173 while((in = getch()) != 'R')
|
|
174 lx = lx * 10 + in - '0';
|
|
175 *x = lx;
|
|
176 *y = ly;
|
|
177 }
|
|
178 int wherex()
|
|
179 {
|
|
180 int x=0,y=0;
|
|
181 wherexy(&x, &y);
|
|
182 return x;
|
|
183 }
|
|
184
|
|
185 int wherey()
|
|
186 {
|
|
187 int x=0,y=0;
|
|
188 wherexy(&x, &y);
|
|
189 return y;
|
|
190 }
|
|
191
|
|
192 int kbhit()
|
|
193 {
|
|
194 struct termios oldt, newt;
|
|
195 int ch;
|
|
196 int oldf;
|
|
197
|
|
198 tcgetattr(STDIN_FILENO, &oldt);
|
|
199 newt = oldt;
|
|
200 newt.c_lflag &= ~(ICANON | ECHO);
|
|
201 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
|
202 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
|
|
203 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
|
|
204
|
|
205 ch = getchar();
|
|
206
|
|
207 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
|
208 fcntl(STDIN_FILENO, F_SETFL, oldf);
|
|
209
|
|
210 if(ch != EOF)
|
|
211 {
|
|
212 ungetc(ch, stdin);
|
|
213 return 1;
|
|
214 }
|
|
215 return 0;
|
|
216 }
|
|
217 #endif
|
|
218
|