This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
/
mtsd.c
304 lines (272 loc) · 7.42 KB
/
mtsd.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
/*
* The MIT License (MIT)
* Copyright (c) 2014 Ian Naval
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <errno.h>
#include <getopt.h>
#include <geis/geis.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/select.h>
#include "config.h"
#include "geis_helpers.h"
#include "helpers.h"
#include "mtsd.h"
/* Private helpers */
void update_x_y_from_touch(GeisTouch touch, float *x, float *y)
{
GeisAttr attr;
attr = geis_touch_attr_by_name(touch, GEIS_TOUCH_ATTRIBUTE_X);
*x = geis_attr_value_to_float(attr);
attr = geis_touch_attr_by_name(touch, GEIS_TOUCH_ATTRIBUTE_Y);
*y = geis_attr_value_to_float(attr);
}
void update_old_x_y_from_touch(GeisTouch touch)
{
update_x_y_from_touch(touch, &old_x, &old_y);
}
void update_new_x_y_from_touch(GeisTouch touch)
{
update_x_y_from_touch(touch, &new_x, &new_y);
}
void update_from_frame(GeisTouchSet touchset, GeisFrame frame)
{
GeisAttr attr;
GeisSize k;
int touches;
attr = geis_frame_attr_by_name(frame, GEIS_GESTURE_ATTRIBUTE_TOUCHES);
touches = geis_attr_value_to_integer(attr);
num_touches = touches > num_touches ? touches : num_touches;
if (touches == 2 && state != MOVING) {
system(WM_START_WINDOW_MOVE_CMD);
state = MOVING;
}
if (touches == 4 && state != RESIZING) {
system(WM_START_WINDOW_RESIZE_CMD);
state = RESIZING;
}
if ((touches > 2 && state == MOVING) || (touches > 4 && state == RESIZING)) {
system(WM_STOP_TRACKING_MOUSE_CMD);
state = DEFAULT;
}
if (state == MOVING || state == RESIZING) {
for (k = 0; k < geis_frame_touchid_count(frame); ++k) {
GeisSize touchid = geis_frame_touchid(frame, k);
GeisTouch touch = geis_touchset_touch_by_id(touchset, touchid);
update_x_y_from_touch(touch, &new_x, &new_y);
}
char *cmd;
size_t needed = snprintf(
NULL, 0, WM_UPDATE_MOUSE_CMD_FMT,
(int) new_x, (int) new_y);
cmd = malloc(needed);
snprintf(
cmd, needed, WM_UPDATE_MOUSE_CMD_FMT,
(int) new_x, (int) new_y);
system(cmd);
free(cmd);
}
}
bool run(int *fd, Geis *geis, GeisStatus *status)
{
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(0, &read_fds);
FD_SET(*fd, &read_fds);
int sstat = select(*fd+1, &read_fds, NULL, NULL, NULL);
if (sstat < 0) {
fatal("error %d in select(): %s\n", errno, strerror(errno));
}
if (FD_ISSET(*fd, &read_fds)) {
GeisEvent event;
*status = geis_dispatch_events(*geis);
*status = geis_next_event(*geis, &event);
while (*status == GEIS_STATUS_CONTINUE ||
*status == GEIS_STATUS_SUCCESS) {
switch (geis_event_type(event)) {
case GEIS_EVENT_DEVICE_AVAILABLE:
case GEIS_EVENT_DEVICE_UNAVAILABLE:
break;
case GEIS_EVENT_GESTURE_BEGIN:
start(event);
break;
case GEIS_EVENT_GESTURE_UPDATE:
update(event);
break;
case GEIS_EVENT_GESTURE_END:
end(event);
break;
default:
break;
}
geis_event_delete(event);
*status = geis_next_event(*geis, &event);
}
}
return !(FD_ISSET(0, &read_fds));
}
int main(const int argc, const char *argv[])
{
Geis geis;
GeisSubscription subscription;
GeisFilter filter;
GeisStatus status;
int fd;
parse_args(argc, argv);
setup();
geis = geis_new(GEIS_INIT_TRACK_DEVICES, NULL);
subscription = geis_subscription_new(geis, "mtsd", GEIS_SUBSCRIPTION_CONT);
filter = geis_filter_new(geis, "mtsd_filter");
status = geis_subscription_add_filter(subscription, filter);
status = geis_subscription_activate(subscription);
geis_get_configuration(geis, GEIS_CONFIGURATION_FD, &fd);
while (running) {
running = run(&fd, &geis, &status);
}
geis_subscription_delete(subscription);
geis_delete(geis);
return 0;
}
void sig_handler(int sig)
{
if (sig == SIGTERM || sig == SIGINT) {
running = false;
}
}
/* Public */
void parse_args(const int argc, const char *argv[])
{
char opt;
while ((opt = getopt(argc, (char**) argv, "dvh:")) != (char)-1) {
switch (opt) {
case 'd':
daemonize();
break;
case 'v':
version();
exit(EXIT_SUCCESS);
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
}
void usage(const char *program)
{
printf("Usage: %s [-d|-h|-v]\n", program);
}
void version(void)
{
printf("%s\n", VERSION);
}
void setup(void)
{
state = DEFAULT;
old_x = 0.0;
old_y = 0.0;
new_x = 0.0;
new_y = 0.0;
num_touches = 0;
running = true;
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
}
void start(GeisEvent event)
{
geis_for_each_touch(event, update_old_x_y_from_touch);
}
void update(GeisEvent event)
{
geis_for_each_frame(event, update_from_frame);
}
bool check_gesture(
bool is_swipe,
int direction,
int correct_direction,
bool gesture_is_swipe,
int gesture_num_touches)
{
bool correct_is_swipe = (is_swipe == gesture_is_swipe);
bool correct_swipe = (is_swipe && direction == correct_direction);
bool correct_gesture = (correct_swipe || !is_swipe);
bool correct_num_touches = (num_touches == gesture_num_touches);
return correct_is_swipe && correct_gesture && correct_num_touches;
}
void end(GeisEvent event)
{
float dx, dy;
geis_for_each_touch(event, update_new_x_y_from_touch);
dx = new_x - old_x;
dy = new_y - old_y;
PRINTF("dx, dy: (%f %f)\nnum_touches: %d\n", dx, dy, num_touches);
if (state == MOVING || state == RESIZING) {
system(WM_STOP_TRACKING_MOUSE_CMD);
state = DEFAULT;
}
bool is_swipe = !(abs(dx) < SWIPE_THRESHOLD && abs(dy) < SWIPE_THRESHOLD);
int direction;
if (abs(dy) > abs(dx)) {
if (dy > 0) {
direction = SWIPE_DOWN;
} else {
direction = SWIPE_UP;
}
} else {
if (dx > 0) {
direction = SWIPE_RIGHT;
} else {
direction = SWIPE_LEFT;
}
}
if (check_gesture(
is_swipe, direction, CLOSE_SWIPE_DIRECTION,
CLOSE_SWIPE, CLOSE_NUM_TOUCHES)) {
system(WM_CLOSE_WINDOW_CMD);
}
if (check_gesture(
is_swipe, direction, FULLSCREEN_ON_SWIPE_DIRECTION,
FULLSCREEN_ON_SWIPE, FULLSCREEN_ON_NUM_TOUCHES)) {
system(WM_FULLSCREEN_ON_CMD);
}
if (check_gesture(
is_swipe, direction, FULLSCREEN_OFF_SWIPE_DIRECTION,
FULLSCREEN_OFF_SWIPE, FULLSCREEN_OFF_NUM_TOUCHES)) {
system(WM_FULLSCREEN_OFF_CMD);
}
if (check_gesture(
is_swipe, direction, PREV_DESKTOP_SWIPE_DIRECTION,
PREV_DESKTOP_SWIPE, PREV_DESKTOP_NUM_TOUCHES)) {
system(WM_PREV_DESKTOP_CMD);
}
if (check_gesture(
is_swipe, direction, NEXT_DESKTOP_SWIPE_DIRECTION,
NEXT_DESKTOP_SWIPE, NEXT_DESKTOP_NUM_TOUCHES)) {
system(WM_NEXT_DESKTOP_CMD);
}
old_x = old_y = new_x = new_y = num_touches = 0;
}