-
Notifications
You must be signed in to change notification settings - Fork 17
/
yamui-powerkey.c
309 lines (257 loc) · 7.56 KB
/
yamui-powerkey.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Power key handler. Waits for all event devices providing KEY_POWER events.
* Exits on power key pressed for desired time or after receiving of SIGTERM.
* Returns:
* 0 - Power key was pressed,
* 1 - signal was received,
* 2 - error.
*
* Copyright (c) 2015 - 2023 Jolla Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <linux/input.h>
/*#define DEBUG*/
#include "yamui-tools.h"
#define NBITS(x) ((((x) - 1) / __BITS_PER_LONG) + 1)
#define BIT(arr, bit) ((arr[(bit) / __BITS_PER_LONG] >> \
((bit) % __BITS_PER_LONG)) & 1)
#define MAX_DEVICES 256
#define DEFAULT_DURATION 3 /* seconds */
/* EXIT_SUCCESS and EXIT_FAILURE are defined in <stdlib.h>. */
#define EXIT_SIGNAL 2
const char *app_name = "powerkey";
sig_atomic_t volatile running = 1;
/* ------------------------------------------------------------------------ */
/* Check for input device type. Returns 0 if button or touchscreen. */
static int
check_device_type(int fd, const char *name)
{
unsigned long bits[EV_MAX][NBITS(KEY_MAX)];
memset(bits, '\0', sizeof(bits));
if (ioctl(fd, EVIOCGBIT(0, EV_MAX), bits[0]) == -1) {
errorf("ioctl(, EVIOCGBIT(0, ), ) error on event device %s",
name);
return -1;
}
if (BIT(bits[0], EV_KEY)) {
if (ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bits[EV_KEY]) == -1)
errorf("ioctl(, EVIOCGBIT(EV_KEY, ), ) error on event"
" device %s", name);
else if (BIT(bits[EV_KEY], KEY_POWER)) {
debugf("Device %s supports needed key events.", name);
return 0;
}
}
debugf("Skipping unsupported device %s.", name);
return -1;
}
/* ------------------------------------------------------------------------ */
static int duration = DEFAULT_DURATION;
static struct timeval key_tv;
typedef enum {
key_up,
key_down,
key_long_press
} key_state_t;
static key_state_t power_key_state = key_up;
static struct timeval *
get_timeout_value(void)
{
if (power_key_state == key_down)
return &key_tv;
else /* key_up or key_long_press */
return NULL; /* Wait forever */
}
/* ------------------------------------------------------------------------ */
static void
set_timeout_value(int sec)
{
key_tv.tv_sec = sec;
key_tv.tv_usec = 0;
}
/* ------------------------------------------------------------------------ */
static void
reset_timeout_value(void)
{
key_tv.tv_sec = duration;
key_tv.tv_usec = 0;
}
/* ------------------------------------------------------------------------ */
typedef enum {
key_ev_up,
key_ev_down
} key_ev_t; /* Why <linux/input.h> still doesn't define it for us? */
/* Returns:
* ret_success - Power key was pressed, terminate main loop.
* ret_continue - Some other key was pressed or released, continue main loop.
*/
static ret_t
handle_event(const struct input_event *ev)
{
if (ev->type != EV_KEY || ev->code != KEY_POWER) {
/* We are not recalculating timeout value in case of
* "interrupted" key_down state because select() properly
* updates timeout value on return. This behavior of select()
* is Linux-specific, and on other platforms you have to
* recalculate timeout value by your own. */
return ret_continue; /* Ignore other events and keys */
}
if (power_key_state == key_up) {
if (ev->value == key_ev_down) {
debugf("New state: key_down");
power_key_state = key_down;
reset_timeout_value();
} /* Else key_ev_up.
* This can happen with multiple Power keys.
* Ignore and keep timeout unchanged. */
} else if (power_key_state == key_down) {
if (ev->value == key_ev_up) {
debugf("New state: key_up");
power_key_state = key_up;
} /* Else key_ev_down.
* This can happen with multiple Power keys. */
} else { /* key_long_press */
if (ev->value == key_ev_up)
return ret_success;
/* Else key_ev_down.
* This can happen with multiple Power keys. */
}
return ret_continue;
}
/* ------------------------------------------------------------------------ */
bool wait_key_up = false; /* Wait for key release event. */
/* Returns:
* ret_success - Power key was pressed, terminate main loop.
* ret_continue - Some other key was pressed or released, continue main loop.
* ret_failure - Error happens, terminate the application. */
static ret_t
handle_timeout(void)
{
if (power_key_state != key_down) { /* Should't be here. */
infof("Internal error: timeout in unexpected state: %d.\n",
power_key_state);
return ret_failure;
}
if (!wait_key_up)
return ret_success;
debugf("New state: key_long_press");
power_key_state = key_long_press;
return ret_continue;
}
/* ------------------------------------------------------------------------ */
static void
signal_handler(int sig UNUSED)
{
running = 0;
}
/* ------------------------------------------------------------------------ */
static void
usage(void)
{
printf("Usage: yamui-%s [-d <key-press-duration>] [-u]\n", app_name);
printf("-d <key-press-duration>\tThe Power key press period "
"in seconds before exit,\n");
printf("\t\t\tdefault value: %d seconds\n", DEFAULT_DURATION);
printf("-u\t\t\tExit on the key release event\n\n");
printf("Return status:\n");
printf("%d - Power key was pressed,\n", EXIT_SUCCESS);
printf("%d - error happens,\n", EXIT_FAILURE);
printf("%d - signal received.\n", EXIT_SIGNAL);
}
/* ------------------------------------------------------------------------ */
int
main(int argc, char *argv[])
{
int opt, fds[MAX_DEVICES], num_fds = 0, ret = EXIT_SIGNAL;
while ((opt = getopt(argc, argv, "d:hu")) != -1) {
switch (opt) {
case 'd':
duration = atoi(optarg);
if ((duration = atoi(optarg)) < 1) {
printf("Duration value must be positive.\n");
usage();
return EXIT_FAILURE;
}
break;
case 'u':
wait_key_up = true;
break;
case 'h':
default:
usage();
return EXIT_FAILURE;
}
}
if (optind < argc) {
usage();
return EXIT_FAILURE;
}
if (open_fds(fds, &num_fds, MAX_DEVICES, check_device_type) == -1)
return EXIT_FAILURE;
debugf("Started");
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
set_timeout_value(duration);
while (running) { /* Main loop */
int i, rv, max_fd = 0;
fd_set rfds;
struct timeval *tv;
FD_ZERO(&rfds);
for (i = 0; i < num_fds; i++) {
FD_SET(fds[i], &rfds);
if (fds[i] > max_fd)
max_fd = fds[i];
}
tv = get_timeout_value();
rv = select(max_fd + 1, &rfds, NULL, NULL, tv);
if (rv > 0) {
for (i = 0; i < num_fds; i++)
if (FD_ISSET(fds[i], &rfds)) {
ret_t r;
r = handle_events(fds[i],
handle_event);
if (r == ret_continue)
continue;
ret = get_exit_status(r);
running = 0;
break;
}
} else if (rv == 0) { /* Timeout */
ret_t r;
if ((r = handle_timeout()) == ret_continue)
continue;
ret = get_exit_status(r);
break;
} else { /* Error or signal */
if (errno != EINTR) {
errorf("Error on select()");
ret = EXIT_FAILURE;
}
break;
}
}
close_fds(fds, num_fds);
debugf("Terminated");
return ret;
}