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
4278static 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