-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
96 lines (79 loc) · 2.74 KB
/
main.cpp
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
#include <iostream>
#include <linux/input.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <sys/time.h>
using namespace std;
//Keeping track of presses.
struct PressRecord {
bool pressed = false;
struct timeval pressTime;
string name = "Unknown";
PressRecord (string name) : name(name) {}
PressRecord(){}
};
int main()
{
// Mapping the correct button IDs.
unordered_map<int, PressRecord> map;
map[105] = PressRecord("Left");
map[102] = PressRecord("Middle");
map[106] = PressRecord("Right");
map[116] = PressRecord("Power");
// Open the button device.
ifstream eventsfile;
eventsfile.open("/dev/input/event2", ios::in);
if(eventsfile.is_open()) {
cout << "File open for reading." << endl;
}
else {
cout << "File couldn't be opened." << endl;
return 0;
}
// Get the size of an input event in the right format!
input_event ie;
streamsize sie = static_cast<streamsize>(sizeof(struct input_event));
while(eventsfile.read((char*)&ie, sie)) {
// Read for non-zero event codes.
if(ie.code != 0) {
// Toggle the button state.
map[ie.code].pressed = !map[ie.code].pressed;
// On press
if(map[ie.code].pressed) {
gettimeofday(&map[ie.code].pressTime,NULL);
cout << map[ie.code].name << " " << "DOWN" << endl;
}
// On release
else {
struct timeval ctime;
gettimeofday(&ctime,NULL);
// Calculate length of hold
long usecs = ((ctime.tv_sec - map[ie.code].pressTime.tv_sec )*1000000L
+ctime.tv_usec) - map[ie.code].pressTime.tv_usec;
// Print out press information
cout << map[ie.code].name << " " << "UP (" << usecs << " microseconds)" << endl;
// Check if MIDDLE was held for > 1 second
if(map[ie.code].name == "Middle" && usecs > 1000000L) {
ifstream termfile;
// Then execute the contents of /etc/draft/.terminate
termfile.open("/etc/draft/.terminate", ios::in);
if(termfile.is_open()) {
cout << "Termfile exists and can be read." << endl;
termfile.close();
system("/bin/bash /etc/draft/.terminate");
}
else {
cout << "Termfile couldn't be read." << endl;
}
}
}
}
}
eventsfile.close();
return 0;
}