-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
185 lines (170 loc) · 5.35 KB
/
main.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
#include <linux/input.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#define TRIGGER_SMOOTHNESS 1
#define RUNNING_SMOOTHNESS 40
#define VERTICAL_SCROLL_CHECK_DIST 3
#define RELATIVE_ORIGIN -1
#define ACCELARATION 1
bool altDown, isVerticalScroll, isDesktopShown, isDesktopSwitched;
int curSmoothness, curSpeed;
int preX, preY;
char *getEventFileName()
{
FILE *fp;
char *eventFile = malloc(sizeof(char) * 10);
fp = popen("awk 'BEGIN{FS=\"\\n\";RS=\"\\n\\n\"}{if($2 ~ /Touchpad/) {match($6,\"event.\",eventWord); print eventWord[0]}}' /proc/bus/input/devices ", "r");
fscanf(fp, "%s", eventFile);
return eventFile;
}
void switchTabs(bool next)
{
if (altDown)
{
if (next)
system("xdotool key Tab");
else
system("xdotool key Shift+Tab");
curSmoothness += curSpeed;
curSpeed += ACCELARATION;
}
else
{
system("xdotool keydown Alt");
if (next)
system("xdotool key Tab");
else
system("xdotool key Shift+Tab");
altDown = true;
}
}
void reset()
{
altDown = false;
isDesktopShown = false;
isDesktopSwitched = false;
curSmoothness = TRIGGER_SMOOTHNESS;
curSpeed = ACCELARATION;
preX = RELATIVE_ORIGIN;
preY = RELATIVE_ORIGIN;
}
void showDesktop()
{
system("xdotool key Alt+Ctrl+d");
}
void switchDesktop(bool next)
{
if (next)
system("xdotool key Ctrl+Alt+Down");
else
system("xdotool key Ctrl+Alt+Up");
}
void handleThreeFingers(bool *threeFingers, int *threeFingersEveCount)
{
*threeFingers = !*threeFingers;
*threeFingersEveCount = 0;
if (!*threeFingers)
{
reset();
system("xdotool keyup Alt");
}
}
void handleFourFingers(bool *fourFingers, int *fourFingersEveCount)
{
*fourFingers = !*fourFingers;
*fourFingersEveCount = 0;
if (!*fourFingers)
reset();
}
void handleYChange(int value, bool *threeFingers, int *threeFingersEveCount)
{
if (value == 0)
return;
if (preY != RELATIVE_ORIGIN && abs(value - preY) >= VERTICAL_SCROLL_CHECK_DIST)
{
isVerticalScroll = true;
}
else
isVerticalScroll = false;
if (*threeFingers && *threeFingersEveCount % curSmoothness == 0 && !isDesktopShown && isVerticalScroll)
{
isDesktopShown = true;
curSmoothness = RUNNING_SMOOTHNESS;
showDesktop();
}
preY = value;
}
void handleXChnage(int value, bool *threeFingers, int *threeFingersEveCount, bool *fourFingers, int *fourFingersEveCount)
{
if (value == 0)
return;
if (preX != RELATIVE_ORIGIN && *threeFingers && *threeFingersEveCount % curSmoothness == 0 && !isVerticalScroll)
{
curSmoothness = RUNNING_SMOOTHNESS;
if (value > preX)
switchTabs(true);
else
switchTabs(false);
}
else if (preX != RELATIVE_ORIGIN && *fourFingers && *fourFingersEveCount % curSmoothness == 0 && !isVerticalScroll && !isDesktopSwitched)
{
curSmoothness = RUNNING_SMOOTHNESS;
isDesktopSwitched = true;
if (value > preX)
switchDesktop(true);
else
switchDesktop(false);
}
preX = value;
}
void handleTrackPad()
{
struct input_event ie;
int fd, threeFingersEveCount, fourFingersEveCount;
bool threeFingers = false, fourFingers = false;
char TOUCHPAD_FILE[] = "/dev/input/";
curSmoothness = TRIGGER_SMOOTHNESS;
curSpeed = ACCELARATION;
//get the touchpad event file
strcat(TOUCHPAD_FILE, getEventFileName());
if ((fd = open(TOUCHPAD_FILE, O_RDONLY)) == -1)
{
perror("Could not open the touchpad event file");
exit(EXIT_FAILURE);
}
reset();
while (read(fd, &ie, sizeof(struct input_event)))
{
if (threeFingers)
threeFingersEveCount++;
else if (fourFingers)
fourFingersEveCount++;
switch (ie.code)
{
case BTN_TOOL_TRIPLETAP:
handleThreeFingers(&threeFingers, &threeFingersEveCount);
break;
case BTN_TOOL_QUADTAP:
handleFourFingers(&fourFingers, &fourFingersEveCount);
break;
case ABS_Y:
handleYChange(ie.value, &threeFingers, &threeFingersEveCount);
break;
case ABS_X:
handleXChnage(ie.value, &threeFingers, &threeFingersEveCount, &fourFingers, &fourFingersEveCount);
break;
default:
break;
}
}
}
int main()
{
handleTrackPad();
return 0;
}