Skip to content

Commit 57e38fa

Browse files
committed
Add swipe event support
1 parent 88bd871 commit 57e38fa

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.13)
22
project(sxgd)
33
set(CMAKE_VERBOSE_MAKEFILE on)
4+
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
45
set(CMAKE_CXX_STANDARD 17)
56
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
67

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Gebaar
2+
=========
3+
4+
WM Independent Gesture Daemon for libinput
5+
6+
_Gebaar means Gesture in Dutch_
7+
8+
### State of the project
9+
10+
- [x] Receiving swipe events from libinput
11+
- [ ] Receiving pinch/zoom events from libinput
12+
- [ ] Receiving rotation events from libinput
13+
- [x] Converting libinput events to motions
14+
- [ ] Running commands based on motions
15+
16+
### Screenshots of debug/testing output
17+
18+
![gestures](doc/swipes.png)

src/main.cpp

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
//
18+
1819
#include <iostream>
1920
#include <libinput.h>
2021
#include <libudev.h>
@@ -38,25 +39,55 @@ const static struct libinput_interface interface = {
3839
.open_restricted = open_restricted,
3940
.close_restricted = close_restricted,
4041
};
42+
struct swipe_event {
43+
int fingers;
44+
double x;
45+
double y;
46+
};
47+
static swipe_event swipeEvent = {};
48+
49+
static void handleSwipeEventWithoutCoords(libinput_event_gesture *gev, bool begin) {
50+
auto fingers = libinput_event_gesture_get_finger_count(gev);
51+
if(begin) {
52+
swipeEvent.fingers = fingers;
53+
} else {
54+
auto absX = abs(swipeEvent.x);
55+
auto absY = abs(swipeEvent.y);
56+
if(absX > absY) {
57+
if(swipeEvent.x < 0) {
58+
spdlog::info("{} Finger Swipe Left", swipeEvent.fingers);
59+
} else {
60+
spdlog::info("{} Finger Swipe Right", swipeEvent.fingers);
61+
}
62+
} else {
63+
if(swipeEvent.y < 0) {
64+
spdlog::info("{} Finger Swipe Up", swipeEvent.fingers);
65+
} else {
66+
spdlog::info("{} Finger Swipe Down", swipeEvent.fingers);
67+
}
68+
}
69+
swipeEvent = {};
70+
}
71+
}
72+
73+
static void handleSwipeEventWithCoords(libinput_event_gesture *gev) {
74+
swipeEvent.x += libinput_event_gesture_get_dx(gev);
75+
swipeEvent.y += libinput_event_gesture_get_dy(gev);
76+
}
4177

4278
static int handleEvents(libinput *li) {
4379
struct libinput_event *liEv;
4480
libinput_dispatch(li);
4581
while ((liEv = libinput_get_event(li))) {
46-
string evType;
47-
bool workableEvent = false;
4882
switch (libinput_event_get_type(liEv)) {
4983
case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
50-
evType = "SWIPE_STA";
51-
workableEvent = true;
84+
handleSwipeEventWithoutCoords(libinput_event_get_gesture_event(liEv), true);
5285
break;
5386
case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
54-
evType = "SWIPE_UPD";
55-
workableEvent = true;
87+
handleSwipeEventWithCoords(libinput_event_get_gesture_event(liEv));
5688
break;
5789
case LIBINPUT_EVENT_GESTURE_SWIPE_END:
58-
evType = "SWIPE_END";
59-
workableEvent = true;
90+
handleSwipeEventWithoutCoords(libinput_event_get_gesture_event(liEv), false);
6091
break;
6192
case LIBINPUT_EVENT_NONE:
6293
break;
@@ -107,13 +138,7 @@ static int handleEvents(libinput *li) {
107138
case LIBINPUT_EVENT_SWITCH_TOGGLE:
108139
break;
109140
}
110-
if (workableEvent) {
111-
struct libinput_event_gesture *gev = libinput_event_get_gesture_event(liEv);
112-
auto fingers = libinput_event_gesture_get_finger_count(gev);
113-
auto deltaX = libinput_event_gesture_get_dx(gev);
114-
auto deltaY = libinput_event_gesture_get_dy(gev);
115-
spdlog::info("Got event: {}, {} fingers, {},{}", evType, fingers, deltaX, deltaY);
116-
}
141+
117142
libinput_event_destroy(liEv);
118143
libinput_dispatch(li);
119144
}
@@ -164,8 +189,10 @@ int main() {
164189

165190
if (deviceWithGestureSupportExists) {
166191
mainLoop(li);
192+
} else {
193+
spdlog::warn("No supported devices found, this won't work");
167194
}
168195
libinput_unref(li);
169196

170197
return 0;
171-
}
198+
}

0 commit comments

Comments
 (0)