-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.c
145 lines (126 loc) · 3.26 KB
/
event.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
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "event.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <errno.h>
#include <string.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <conio.h>
#else
#endif
_Atomic size_t resized = 0; // Semaphore-like
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
#include<unistd.h>
#include<termios.h>
#include<signal.h>
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
struct termios saved_attributes;
#endif
void
signal_resize(int signo)
{
++resized;
}
void
event_start()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
/* Taken from https://www.gnu.org/software/libc/manual/html_node/Noncanon-Example.html */
struct termios tattr;
/* Make sure stdin is a terminal. */
if (!isatty (STDIN_FILENO))
{
fprintf (stderr, "Not a terminal.\n");
exit (EXIT_FAILURE);
}
/* Save the terminal attributes so we can restore them later. */
tcgetattr (STDIN_FILENO, &saved_attributes);
atexit (event_end);
/* Set the funny terminal modes. */
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = signal_resize;
sa.sa_flags = 0;
sigaction(SIGWINCH, &sa, NULL);
#endif
}
struct event
event_getevent()
{
for (;;)
{
errno = 0;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
int c = getch();
#else
int c = getchar();
#endif
switch (c)
{
case -1:
if (errno != EINTR) exit(EXIT_FAILURE);
if (resized > 0)
{
--resized;
return (struct event) { .tag = RESIZE };
}
break;
case RET:
return (struct event) { .tag = RET };
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
case '\x1b': // ESC
if (getchar() != '[') // CSI
{
continue;
}
switch (c = getchar())
{
#endif
case UP: // 'A'
case DOWN: // 'B'
return (struct event) { .tag = c };
break;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
break;
}
#endif
return (struct event) { .tag = CHAR, .character = c };
}
}
void
event_end()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#else
tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
#endif
}