summaryrefslogtreecommitdiff
path: root/src/dumpkeys.c
blob: 53a2f588d295a5d1275a69a803d890942bc56d8f (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (C) 2002 Red Hat, Inc.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#if __has_include(<sys/types.h>)
#include <sys/types.h>
#endif
#if __has_include(<sys/select.h>)
#include <sys/select.h>
#endif
#include <termios.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>

#define ESC "\033"
#define MODE_APPLICATION_KEYPAD		ESC "="
#define MODE_NORMAL_KEYPAD		ESC ">"
#define MODE_APPLICATION_CURSOR_KEYS	1
#define MODE_ALTERNATE_SCREEN		1047

enum {
	normal = 0, application = 1
} keypad_mode = normal, cursor_mode = normal;
struct termios original;

/* Output the DEC private mode set sequence. */
static void
decset(int mode, gboolean value)
{
	g_print(ESC "[?%d%c", mode, value ? 'h' : 'l');
}

/* Move the cursor to the upper left corner of the screen. */
static void
home(void)
{
	g_print(ESC "[1;1H");
}

/* Clear the screen. */
static void
clear(void)
{
	g_print(ESC "[2J");
	home();
}

/* Print the what-does-this-key-do help messages and current status. */
static void
print_help(void)
{
	g_print(ESC "[m");
	home();
	g_print(ESC "[K" "A - KEYPAD ");
	if (keypad_mode == application) {
		g_print("APPLICATION\r\n");
	} else {
		g_print("NORMAL\r\n");
	}
	g_print(ESC "[K" "B - CURSOR ");
	if (cursor_mode == application) {
		g_print("APPLICATION\r\n");
	} else {
		g_print("NORMAL\r\n");
	}
	g_print(ESC "[K" "R - RESET\r\n");
	g_print(ESC "[K" "Q - QUIT\r\n");
}

/* Reset the scrolling region, so that the entire screen becomes
 * addressable again. */
static void
reset_scrolling_region(void)
{
	g_print(ESC "[r");
}

/* Set the scrolling region, so that the help/status at the top of the
 * screen doesn't scroll off. */
static void
set_scrolling_region(void)
{
	g_print(ESC "[6;24r");
	g_print(ESC "[5;1H");
}

/* Save the current location of the cursor in the terminal's memory. */
static void
save_cursor(void)
{
	g_print(ESC "7");
}

/* Restore the cursor to the location stored in the terminal's memory. */
static void
restore_cursor(void)
{
	g_print(ESC "8");
}

/* Reset all of the keyboard modes. */
static void
reset(void)
{
	g_print(MODE_NORMAL_KEYPAD);
	decset(MODE_APPLICATION_CURSOR_KEYS, FALSE);
	reset_scrolling_region();
	restore_cursor();
}

/* Cleanly exit. */
G_GNUC_NORETURN static void
sigint_handler(int signum)
{
	if (tcsetattr(STDIN_FILENO, TCSANOW, &original) != 0) {
		perror("tcsetattr");
	}
	reset();
	_exit(1);
}

int
main(int argc, char **argv)
{
	char c;
	guint i;
	struct termios tcattr;
	GByteArray *bytes;
	gboolean done = FALSE, saved = FALSE;
	struct timeval tv;
	fd_set readset;

	/* Start up: save the cursor location and put the terminal in
	 * raw mode. */
	bytes = g_byte_array_new();
	save_cursor();
	if (tcgetattr(STDIN_FILENO, &tcattr) != 0) {
		perror("tcgetattr");
		return 1;
	}
	original = tcattr;
	signal(SIGINT, sigint_handler);
	cfmakeraw(&tcattr);
	if (tcsetattr(STDIN_FILENO, TCSANOW, &tcattr) != 0) {
		perror("tcsetattr");
		return 1;
	}

	/* Switch to the alternate screen, clear it, and reset the keyboard. */
	decset(MODE_ALTERNATE_SCREEN, TRUE);
	clear();
	reset();

	/* Main processing loop. */
	while (!done) {
		print_help();
		set_scrolling_region();
		if (saved) {
			restore_cursor();
		}

		/* Read a single byte. */
		if (read(STDIN_FILENO, &c, 1) != 1) {
			done = TRUE;
		}
		switch (c) {
		case 'A':
		case 'a':
			keypad_mode = 1 - keypad_mode;
			if (keypad_mode == normal) {
				g_print(MODE_NORMAL_KEYPAD);
			} else {
				g_print(MODE_APPLICATION_KEYPAD);
			}
			break;
		case 'B':
		case 'b':
			cursor_mode = 1 - cursor_mode;
			decset(MODE_APPLICATION_CURSOR_KEYS,
			       cursor_mode == application);
			break;
		case 'R':
		case 'r':
			keypad_mode = cursor_mode = normal;
			reset();
			break;
		case 'Q':
		case 'q':
			done = TRUE;
			break;
		case 0x0c: /* ^L */
			clear();
			if (saved) {
				restore_cursor();
				saved = FALSE;
			}
			break;
		default:
			/* We get here if it's not one of the keys we care
			 * about, so it might be a sequence. */
			if (saved) {
				restore_cursor();
			}
			g_byte_array_append(bytes, (const guint8 *) &c, 1);
			/* Wait for up to just under 1/50 second. */
			tv.tv_sec = 0;
			tv.tv_usec = 1000000 / 50;
			FD_ZERO(&readset);
			FD_SET(STDIN_FILENO, &readset);
			while (select(STDIN_FILENO + 1,
				      &readset, NULL, NULL, &tv) == 1) {
				if (read(STDIN_FILENO, &c, 1) == 1) {
					g_byte_array_append(bytes, (const guint8 *) &c, 1);
				} else {
					break;
				}
				tv.tv_sec = 0;
				tv.tv_usec = 1000000 / 50;
				FD_ZERO(&readset);
				FD_SET(STDIN_FILENO, &readset);
			}
			/* Clear this line, and print the sequence. */
			g_print(ESC "[K");
			for (i = 0; i < bytes->len; i++) {
				if (bytes->data[i] == 27) {
					g_print("<ESC> ");
				} else
				if ((((guint8)bytes->data[i]) < 32) ||
				    (((guint8)bytes->data[i]) > 126)) {
					g_print("<0x%02x> ", bytes->data[i]);
				} else {
					g_print("`%c' ", bytes->data[i]);
				}
			}
			g_print("\r\n");
			g_byte_array_set_size(bytes, 0);
			save_cursor();
			saved = TRUE;
			break;
		}
		reset_scrolling_region();
	}

	decset(MODE_ALTERNATE_SCREEN, FALSE);

	if (tcsetattr(STDIN_FILENO, TCSANOW, &original) != 0) {
		perror("tcsetattr");
		return 1;
	}

	g_byte_array_free(bytes, TRUE);

	reset();

	return 0;
}