forked from AVGP/panic-button
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
80 lines (60 loc) · 1.36 KB
/
main.c
File metadata and controls
80 lines (60 loc) · 1.36 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#include "button.h"
int main()
{
struct libusb_device_handle *dev = openButton();
if (!dev) {
exit(1);
}
/* Remember if the lid is open */
int state = BUTTON_STATE_CLOSED;
while (1) {
int val = getButtonState(dev);
if (val != state) {
if (val == BUTTON_STATE_OPENED) {
state = val;
if (fork() == 0) {
system("panicbutton-opened.sh");
exit(0);
}
fprintf(stderr, "Opened.\n");
}
else if (val == BUTTON_STATE_PRESSED) {
if (fork() == 0) {
system("panicbutton-down.sh");
exit(0);
}
fprintf(stderr, "Button down.\n");
/* Wait until the button is up again */
while (getButtonState(dev) == BUTTON_STATE_PRESSED) usleep(100000);
if (fork() == 0) {
system("panicbutton-up.sh");
exit(0);
}
fprintf(stderr, "Button up.\n");
}
else if (val == BUTTON_STATE_CLOSED) {
state = val;
if (fork() == 0) {
system("panicbutton-closed.sh");
exit(0);
}
fprintf(stderr, "Closed.\n");
}
else if (val == 20) {
fprintf(stderr, "Button pushed while the lid is closed?\n");
}
else {
fprintf(stderr, "Unknown button status %d\n", val);
}
}
usleep(100000);
};
if (libusb_release_interface(dev, 0) != 0) {
printf("Cannot unclaim button\n");
}
libusb_close(dev);
return 0;
}