aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-04-10 01:43:49 +1000
committerDavid Walter Seikel2014-04-10 01:43:49 +1000
commitedfd844dc7f8f503c5043bbd32235b201c331ee0 (patch)
treee3b9b09eebd2825534e0b9cd1909e497cc07fc61
parentVarious key mapping fixes for the editors. (diff)
downloadboxes-edfd844dc7f8f503c5043bbd32235b201c331ee0.zip
boxes-edfd844dc7f8f503c5043bbd32235b201c331ee0.tar.gz
boxes-edfd844dc7f8f503c5043bbd32235b201c331ee0.tar.bz2
boxes-edfd844dc7f8f503c5043bbd32235b201c331ee0.tar.xz
Add a showkey toy. Not standard, I'll see if there's an actual standard later.
-rw-r--r--showkey.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/showkey.c b/showkey.c
new file mode 100644
index 0000000..2f5947b
--- /dev/null
+++ b/showkey.c
@@ -0,0 +1,125 @@
1/* showkey.c - Shows the keys pressed.
2 *
3 * Copyright 2014 David Seikel <won_fang@yahoo.com.au>
4 *
5 * Dunno yet if this is a standard.
6
7USE_SHOWKEY(NEWTOY(showkey, "", TOYFLAG_USR|TOYFLAG_BIN))
8
9config SHOWKEY
10 bool "showkey"
11 default n
12 help
13 usage: showkey
14
15 Shows the keys pressed.
16*/
17
18#include "toys.h"
19#include "lib/handlekeys.h"
20
21typedef void (*eventHandler) (void);
22
23struct keyCommand
24{
25 char *key;
26 eventHandler handler;
27};
28
29GLOBALS(
30 unsigned h, w;
31 int x, y;
32)
33
34#define TT this.showkey
35
36
37// Callback for incoming CSI commands from the terminal.
38static void handleCSI(long extra, char *command, int *params, int count)
39{
40 int i;
41
42 // Is it a cursor location report?
43 if (strcmp("R", command) == 0)
44 {
45 printf("CSI cursor position - line %d, column %d\r\n", params[0], params[1]);
46 return;
47 }
48
49 printf("CSI command %s - ", command);
50 for (i = 0; i < count; i++)
51 printf("%d ", params[i]);
52 printf("\r\n");
53}
54
55static void quit()
56{
57 printf("Quitting.\r\n");
58 handle_keys_quit();
59}
60
61// The key to command mappings.
62static struct keyCommand simpleKeys[] =
63{
64 {"^C", quit}
65};
66
67// Callback for incoming key sequences from the user.
68static int handleKeySequence(long extra, char *sequence, int isTranslated)
69{
70 int j, l = strlen(sequence);
71
72 if (isTranslated)
73 printf("TRANSLATED - ");
74 else
75 printf("KEY - ");
76 printf("%s\r\n", sequence);
77
78 // Search for a key sequence bound to a command.
79 for (j = 0; j < (sizeof(simpleKeys) / sizeof(*simpleKeys)); j++)
80 {
81 if (strncmp(simpleKeys[j].key, sequence, l) == 0)
82 {
83 // If it's a partial match, keep accumulating them.
84 if (strlen(simpleKeys[j].key) != l)
85 return 0;
86 else
87 {
88 if (simpleKeys[j].handler) simpleKeys[j].handler();
89 return 1;
90 }
91 }
92 }
93
94 return 1;
95}
96
97void showkey_main(void)
98{
99 struct termios termIo, oldTermIo;
100
101 // Grab the old terminal settings and save it.
102 tcgetattr(0, &oldTermIo);
103 tcflush(0, TCIFLUSH);
104 termIo = oldTermIo;
105
106 // Mould the terminal to our will.
107 // In this example we are turning off all the terminal smarts, but real code
108 // might not want that.
109 termIo.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL
110 | IUCLC | IXON | IXOFF | IXANY);
111 termIo.c_oflag &= ~OPOST;
112 termIo.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | TOSTOP | ICANON | ISIG
113 | IEXTEN);
114 termIo.c_cflag &= ~(CSIZE | PARENB);
115 termIo.c_cflag |= CS8;
116 termIo.c_cc[VTIME]=0; // deciseconds.
117 termIo.c_cc[VMIN]=1;
118 tcsetattr(0, TCSANOW, &termIo);
119
120 handle_keys(0, handleKeySequence, handleCSI);
121
122 tcsetattr(0, TCSANOW, &oldTermIo);
123 puts("");
124 fflush(stdout);
125}