-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.c
150 lines (131 loc) · 3.18 KB
/
input.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
/*
* Copyright (c) 2011 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include "siren.h"
static void input_handle_signal(int);
static enum input_mode input_mode = INPUT_MODE_VIEW;
static pthread_mutex_t input_mode_mtx = PTHREAD_MUTEX_INITIALIZER;
static volatile sig_atomic_t input_quit;
#ifdef SIGWINCH
static volatile sig_atomic_t input_sigwinch;
#endif
void
input_end(void)
{
input_quit = 1;
}
void
input_init(void)
{
struct sigaction sa;
#ifdef VDSUSP
struct termios tio;
#endif
sa.sa_handler = input_handle_signal;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, NULL) == -1)
LOG_ERR("sigaction");
if (sigaction(SIGQUIT, &sa, NULL) == -1)
LOG_ERR("sigaction");
if (sigaction(SIGTERM, &sa, NULL) == -1)
LOG_ERR("sigaction");
#ifdef SIGWINCH
if (sigaction(SIGWINCH, &sa, NULL) == -1)
LOG_ERR("sigaction");
#endif
#ifdef VDSUSP
/*
* Check if the DSUSP special character is set to ^Y. If it is, disable
* it so that ^Y becomes an ordinary character that can be bound to a
* command.
*/
if (tcgetattr(STDIN_FILENO, &tio) == -1)
LOG_ERR("tcgetattr");
else
if (tio.c_cc[VDSUSP] == K_CTRL('Y')) {
tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
if (tcsetattr(STDIN_FILENO, TCSANOW, &tio) == -1)
LOG_ERR("tcsetattr");
}
#endif
}
enum input_mode
input_get_mode(void)
{
enum input_mode mode;
XPTHREAD_MUTEX_LOCK(&input_mode_mtx);
mode = input_mode;
XPTHREAD_MUTEX_UNLOCK(&input_mode_mtx);
return mode;
}
void
input_handle_key(void)
{
struct pollfd pfd[1];
int key;
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
while (!input_quit) {
#ifdef SIGWINCH
if (input_sigwinch) {
input_sigwinch = 0;
screen_refresh();
}
#endif
if (poll(pfd, nitems(pfd), -1) == -1) {
if (errno != EINTR)
LOG_FATAL("poll");
} else {
if (pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL))
LOG_FATALX("poll() failed");
key = screen_get_key();
if (input_mode == INPUT_MODE_VIEW)
view_handle_key(key);
else
prompt_handle_key(key);
}
}
}
static void
input_handle_signal(int sig)
{
switch (sig) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
input_quit = 1;
break;
#ifdef SIGWINCH
case SIGWINCH:
input_sigwinch = 1;
break;
#endif
}
}
void
input_set_mode(enum input_mode mode)
{
XPTHREAD_MUTEX_LOCK(&input_mode_mtx);
input_mode = mode;
XPTHREAD_MUTEX_UNLOCK(&input_mode_mtx);
}