-
Notifications
You must be signed in to change notification settings - Fork 1
/
stermp.h
244 lines (211 loc) · 6.83 KB
/
stermp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* ---------------------------------------------------------- */
/* | Name: stermp.h ( Simple Terminal Play) | */
/* | Author: Gaspar Fernández (blakeyes@totaki.com) | */
/* | Date: 05 January 2009 | */
/* | Web: http://www.totaki.com/poesiabinaria/ | */
/* | Web: http://gaspar.totaki.com/ | */
/* | Web: http://binaryprose.com | */
/* ---------------------------------------------------------- */
/* | Description: | */
/* | Colors and position handling for terminal programs | */
/* | made for easy terminal projects, to make it as | */
/* | similar as possible to conio.h, and make those old | */
/* | programs portable. | */
/* ---------------------------------------------------------- */
/* | Changelog: | */
/* | 20131007 - Receive SIGIO to avoid some errors | */
/* | 20131005 - Translating comments to English, to | */
/* | upload to github and other sites. | */
/* | 20100629: Several updates: | */
/* | - Create error() function | */
/* | - Create term_init() function to initialize global | */
/* | variables. | */
/* | - Create getch() replacement | */
/* | - Create term_info() function | */
/* | - Create save_term_info() function | */
/* | - Create restore_term() function | */
/* | - Create kbhit(), kbhit2(), __kbhit() and | */
/* | kbhit_pre() functions | */
/* | - Create macros to make kbhit() faster | */
/* | - Create kbhit_usleep() to lighten CPU usage | */
/* | - Function termnoecho() smaller | */
/* | - restore_terminal() now restores stdin flags | */
/* ---------------------------------------------------------- */
/* The MIT License (MIT) */
/* Copyright (c) 2013 Gaspar Fernández */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of */
/* this software and associated documentation files (the "Software"), to deal in */
/* the Software without restriction, including without limitation the rights to */
/* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of */
/* the Software, and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS */
/* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR */
/* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER */
/* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <termios.h>
#include <string.h>
/* Color s */
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
/* Special attributes */
#define UNDERLINE 64
#define BLINK 128
/* Special font attributes */
#define UNDERLINE_ATTR 4
#define BLINK_ATTR 5
#define BRILLO_ATTR 1
/* Bright colors start in color number 9 */
#define BRILLO_MIN 9
/* Maroon is like yellow without bright. */
/* Lightgray is white without bright. */
/* Darkgray is the default terminal color */
/* Default exit code */
#define EXIT_CODE 1
typedef struct
{
int x, y;
} coord_t;
/**
* Clears screen
*/
void clrscr();
/**
* Lets us move in the terminal window to an x, y position
*
* @param x Position in X axis
* @param y Position in Y axis
*/
void gotoxy(int x, int y);
/**
* Restore terminal color
*/
void restore_color();
/**
* Restores al terminal attributes
*/
void restore_terminal();
/**
* Gets screen height (or terminal window height
*
* @return height
*/
int screenheight();
/**
* Gets screen width (or terminal window width
*
* @return width
*/
int screenwidth();
/**
* Disables keyboard echo
*
* @param canon Makes canon mode ON. This avoids us to press enter to press keys
*/
void termnoecho(int canon);
/**
* Sets background colod
*
* @param color Background color
*/
void textbackground(int color);
/**
* Sets foreground color
*
* @param color foreground color
*/
void textcolor(int color);
/**
* Gets current position or screen dimensions
*
* @param abs if 1 it returns screen dimensions
*
* @return Current position or screen dimensions
*/
coord_t wherexy(int abs);
/**
* Gets current screen X position
*
* @return X position
*/
int wherex();
/**
* Gets current screen Y position
*
* @return Y position
*/
int wherey();
/**
* Asks for a character. No need to press enter to get character.
* No echo on screen.
*
* @return Character code
*/
int getch();
/**
* Asks for a character. No need to press enter to get character.
* Character echoed on screen
*
* @return Character code
*/
int getche(); /* Simula getche()- Pide un carácter (sin INTRO) y con echo */
/**
* Tells us if a key has been pressed without stopping execution
*
* @return true if a key has been pressed
*/
int kbhit(); /* Devuelve si se ha pulsado una tecla o no (no detiene ejecución) */
/**
* Tells us if a key has been pressed without stopping execution
*
* @return keycode of the pressed key. 0 if none pressed
*/
int kbhit2(); /* Si se ha pulsado una tecla, devuelve su código (no detiene ejecución) */
/**
* Tells us if a key has been pressed without stopping execution.
* It behaves like kbhit2() but doesn't restore terminal properties.
* Useful when asking so many keys avoiding modifying and restoring
* terminal all the time, just call kbhit_pre() before making calls
* to __kbhit().
*
* @return keycode of the pressed key. 0 if none pressed
*/
int __kbhit();
/**
* Prepares the terminal for kbhit
*/
void kbhit_pre();
/**
* Saves terminal information in temporary variables
*/
void save_term_info();
/**
* Restores terminal information
*/
void term_defaults();
/**
* Restores terminal, with custom values.
*/
void restore_term(struct termios *tty, int *stdinflags);
/**
* Writes error in stderr and closes program
*/
void error(char *err);