-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrterm.c
179 lines (154 loc) · 4.47 KB
/
rterm.c
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
/*
======================================================================
Module to control some display routines that implement ANSI functions.
@author : Velorek
@version : 1.0
LAST MODIFIED : 14/04/2019 Rename Headers
======================================================================
*/
/*====================================================================*/
/* COMPILER DIRECTIVES AND INCLUDES */
/*====================================================================*/
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
/*====================================================================*/
/* GLOBAL VARIABLES */
/*====================================================================*/
struct winsize max;
static struct termios term,term2, failsafe;
static int peek_character = -1;
/*====================================================================*/
/* FUNCTIONS - CODE */
/*====================================================================*/
/*-------------------------------------*/
/* Initialize new terminal i/o settings*/
/*-------------------------------------*/
void pushTerm(void) {
//Save terminal settings in failsafe to be retrived at the end
tcgetattr(0, &failsafe);
}
/*---------------------------*/
/* Reset terminal to failsafe*/
/*---------------------------*/
int resetTerm(void) {
//tcsetattr(0, TCSANOW, &failsafe);
/* flush and reset */
if (tcsetattr(0,TCSAFLUSH,&failsafe) < 0) return -1;
return 0;
}
/*--------------------------------------.*/
/* Detect whether a key has been pressed.*/
/*---------------------------------------*/
int kbhit(void)
{
if(peek_character != -1)
return 1;
tcgetattr(0, &term);
term2.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &term2);
int byteswaiting;
ioctl(0, FIONREAD, &byteswaiting);
tcsetattr(0, TCSANOW, &term);
return byteswaiting > 0;
}
/*----------------------*/
/*Read char with control*/
/*----------------------*/
int readch(void) {
char ch;
int ret=0;
if(peek_character != -1) {
ch = peek_character;
peek_character = -1;
return ch;
}
ret = read(0, &ch, 1);
ret = ret; //to avoid warning message
return ch;
}
void resetch(void) {
//Clear ch
term.c_cc[VMIN] = 0;
tcsetattr(0, TCSANOW, &term);
peek_character = 0;
}
/*------------------------------------------ */
/* Read 1 character - echo defines echo mode */
/*------------------------------------------ */
char getch() {
struct termios t;
tcgetattr(0, &t);
tcflag_t old_flag = t.c_lflag;
t.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &t);
int c = getchar();
t.c_lflag = old_flag;
tcsetattr(0, TCSANOW, &t);
return c;
}
/*----------------------------------*/
/* Move cursor to specified position*/
/*----------------------------------*/
void gotoxy(int x, int y) {
printf("%c[%d;%df", 0x1B, y, x);
}
/*---------------------*/
/* Change colour output*/
/*---------------------*/
void outputcolor(int foreground, int background) {
printf("%c[%d;%dm", 0x1b, foreground, background);
}
/*-----------------------------------------------------*/
/* Change the whole color of the screen by applying CLS*/
/*-----------------------------------------------------*/
void screencol(int x) {
gotoxy(0, 0);
outputcolor(0, x);
printf("%c[2J", 0x1b);
outputcolor(0, 0);
}
/*-----------------------*/
/* Reset ANSI ATTRIBUTES */
/*-----------------------*/
void resetAnsi(int x) {
switch (x) {
case 0: //reset all colors and attributes
printf("%c[0m", 0x1b);
break;
case 1: //reset only attributes
printf("%c[20m", 0x1b);
break;
case 2: //reset foreg. colors and not attrib.
printf("%c[39m", 0x1b);
break;
case 3: //reset back. colors and not attrib.
printf("%c[49m", 0x1b);
break;
default:
break;
}
}
/*------------------------*/
/* Get terminal dimensions*/
/*------------------------*/
int get_terminal_dimensions(int *rows, int *columns) {
ioctl(0, TIOCGWINSZ, &max);
*columns = max.ws_col;
*rows = max.ws_row;
return 0;
}
/*--------------------------*/
/* Ansi function hide cursor*/
/*--------------------------*/
void hidecursor(void) {
printf("\e[?25l");
}
/*--------------------------*/
/* Ansi function show cursor*/
/*--------------------------*/
void showcursor(void) {
printf("\e[?25h");
}