-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.cpp
159 lines (139 loc) · 3.58 KB
/
terminal.cpp
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
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "terminal.h"
using namespace std;
void Terminal::gotoPoint(int x, int y) {
cout << "\e[" << (y >= 0 ? y : 0) << ";" << (x >= 0 ? x : 0) << "H";
}
void Terminal::cleanScreen() {
cout << "\e[2J";
}
void Terminal::startControlMode() {
struct termios termInfo;
/* 获取当前终端信息 */
tcgetattr(STDIN_FILENO, &termInfo);
/**
* ICANON 如果在输入中看到"\n"或者EOF,会返回缓冲区内容
* ECHO 用还控制回显
*/
termInfo.c_lflag &= ~(ICANON | ECHO); // 设置新的终端属性
/**
* TCSANOW 告诉函数立即改变终端的STDIN_FILENO属性值
*/
tcsetattr(STDIN_FILENO, TCSANOW, &termInfo);
/* 隐藏光标 */
hideCursor();
/* 捕获Ctrl-C按键 */
if (signal(SIGINT, Terminal::sign_quit) == SIG_ERR) {
cout << "startControlMode" << endl;
exit(-1);
}
/* 捕获Ctrl-\按键 */
if (signal(SIGQUIT, Terminal::sign_quit) == SIG_ERR) {
cout << "startControlMode" << endl;
exit(-1);
}
}
//捕捉退出程序信号
void Terminal::sign_quit(int sig) {
stopControlMode();
exit(1);
}
void Terminal::stopControlMode() {
struct termios termInfo;
/* 获取当前终端信息 */
tcgetattr(STDIN_FILENO, &termInfo);
/**
* ICANON 如果在输入中看到"\n"或者EOF,会返回缓冲区内容
* ECHO 用还控制回显
*/
termInfo.c_lflag |= (ICANON | ECHO); // 设置新的终端属性
/**
* TCSANOW 告诉函数立即改变终端的STDIN_FILENO属性值
*/
tcsetattr(STDIN_FILENO, TCSANOW, &termInfo);
/* 显示光标 */
displayCursor();
/* 清屏 */
cleanScreen();
gotoPoint(0, 0);
}
//显示光标
void Terminal::displayCursor() {
cout << "\e[?25h";
}
//隐藏光标
void Terminal::hideCursor() {
cout << "\e[?25l";
}
//获取终端尺寸
void Terminal::getWs(int &x, int &y, int fd) {
struct winsize size;
if (ioctl(fd, TIOCGWINSZ, (char*)&size) < 0) {
cout << "ioctl" << endl;
return;
}
y = size.ws_row;
x = size.ws_col;
}
//设置终端尺寸
void Terminal::setWs(int x, int y, int fd) {
struct winsize size;
if (ioctl(fd, TIOCGWINSZ, (char*)&size) < 0) {
cout << "ioctl" << endl;
return;
}
size.ws_row = y;
size.ws_col = x;
if (ioctl(fd, TIOCSWINSZ, (char*)&size) < 0) {
cout << "ioctl" << endl;
return;
}
}
//等待特定按键
int Terminal::getKey(const int type) {
char buf[4];
size_t len;
switch(type) {
case 1:
//获取方向按键
while((len = read(STDIN_FILENO, buf, 3)) != -1) {
if (3 == len && 27 == buf[0] && 91 == buf[1]) {
return buf[2];
}
switch(buf[0]) {
case 'w':
case 'W':
//上
return UP;
case 's':
case 'S':
//上
return DOWN;
case 'a':
case 'A':
//上
return LEFT;
case 'd':
case 'D':
//上
return RIGHT;
case ESC:
//上
return ESC;
}
}
break;
case 2:
//确认键(ESC:退出;Enter:继续)
while((len = read(STDIN_FILENO, buf, 3)) != -1) {
if (1 == len && (GOON == buf[0] || ESC == buf[0])) {
return buf[0];
}
}
break;
}
return 0;
}