From 2cc121b60226d764e11aa9e012f6b30a2af92b56 Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Sun, 5 Nov 2023 01:34:13 -0500
Subject: [PATCH] feat(ags): move lisgd config to ags

---
 devices/wim/config/ags/js/setup.js            |  33 ++++-
 .../wim/config/ags/services/touch-gestures.js | 127 ++++++++++++++++++
 devices/wim/config/hypr/main.conf             |   4 -
 devices/wim/config/hypr/scripts/lisgd.sh      |  19 ---
 4 files changed, 158 insertions(+), 25 deletions(-)
 create mode 100644 devices/wim/config/ags/services/touch-gestures.js
 delete mode 100755 devices/wim/config/hypr/scripts/lisgd.sh

diff --git a/devices/wim/config/ags/js/setup.js b/devices/wim/config/ags/js/setup.js
index 53cbb54f..5c073260 100644
--- a/devices/wim/config/ags/js/setup.js
+++ b/devices/wim/config/ags/js/setup.js
@@ -1,7 +1,8 @@
 import { execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
 
-import Tablet from '../services/tablet.js';
-import Pointers from '../services/pointers.js';
+import Tablet        from '../services/tablet.js';
+import Pointers      from '../services/pointers.js';
+import TouchGestures from '../services/touch-gestures.js';
 import closeAll from './misc/closer.js';
 
 
@@ -11,4 +12,32 @@ export default () => {
     globalThis.closeAll = closeAll;
 
     execAsync(['bash', '-c', '$AGS_PATH/startup.sh']).catch(print);
+
+    TouchGestures.addGesture({
+        name: 'oskOn',
+        gesture: 'DU',
+        edge: 'B',
+        command: 'busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true',
+    });
+
+    TouchGestures.addGesture({
+        name: 'oskOff',
+        gesture: 'UD',
+        edge: 'B',
+        command: 'busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b false',
+    });
+
+    TouchGestures.addGesture({
+        name: 'swipeSpotify1',
+        gesture: 'LR',
+        edge: 'L',
+        command: 'hyprctl dispatch togglespecialworkspace spot',
+    });
+
+    TouchGestures.addGesture({
+        name: 'swipeSpotify2',
+        gesture: 'RL',
+        edge: 'L',
+        command: 'hyprctl dispatch togglespecialworkspace spot',
+    });
 };
diff --git a/devices/wim/config/ags/services/touch-gestures.js b/devices/wim/config/ags/services/touch-gestures.js
new file mode 100644
index 00000000..bfddf23a
--- /dev/null
+++ b/devices/wim/config/ags/services/touch-gestures.js
@@ -0,0 +1,127 @@
+import Service from 'resource:///com/github/Aylur/ags/service.js';
+import { subprocess } from 'resource:///com/github/Aylur/ags/utils.js';
+
+const SCREEN = '/dev/input/by-path/platform-AMDI0010\:00-event';
+
+const GESTURE_VERIF = [
+    'LR', // Left to Right
+    'RL', // Right to Left
+    'DU', // Down to Up
+    'UD', // Up to Down
+    'DLUR', // Down to Left to Up to Right (clockwise motion from Down)
+    'DRUL', // Down to Right to Up to Left (counter-clockwise from Down)
+    'URDL', // Up to Right to Down to Left (clockwise motion from Up)
+    'ULDR', // Up to Left to Down to Right (counter-clockwise from Up)
+];
+const EDGE_VERIF = [
+    '*', // any
+    'N', // none
+    'L', // left
+    'R', // right
+    'T', // top
+    'B', // bottom
+    'TL', // top left
+    'TR', // top right
+    'BL', // bottom left
+    'BR', // bottom right
+];
+const DISTANCE_VERIF = [
+    '*', // any
+    'S', // short
+    'M', // medium
+    'L', // large
+];
+
+
+class TouchGestures extends Service {
+    static {
+        Service.register(this, {
+            'daemon-started': ['boolean'],
+            'daemon-destroyed': ['boolean'],
+        });
+    }
+
+    gestures = new Map();
+    gestureDaemon = undefined;
+
+    get gestures() { return this.gestures; }
+
+    constructor() {
+        super();
+    }
+
+    addGesture({
+        name,
+        nFingers = '1',
+        gesture,
+        edge = '*',
+        distance = '*',
+        command,
+    } = {}) {
+        gesture = String(gesture).toUpperCase();
+        if (!GESTURE_VERIF.includes(gesture)) {
+            logError('Wrong gesture id');
+            return;
+        }
+
+        edge = String(edge).toUpperCase();
+        if (!EDGE_VERIF.includes(edge)) {
+            logError('Wrong edge id');
+            return;
+        }
+
+        distance = String(distance).toUpperCase();
+        if (!DISTANCE_VERIF.includes(distance)) {
+            logError('Wrong distance id');
+            return;
+        }
+
+        this.gestures.set(name, [
+            '-g',
+            `${nFingers},${gesture},${edge},${distance},${command}`,
+        ]);
+
+        if (this.gestureDaemon)
+            this.killDaemon();
+        this.startDaemon();
+    }
+
+    startDaemon() {
+        if (this.gestureDaemon)
+            return;
+
+        var command = [
+            'lisgd', '-d', SCREEN,
+            // orientation
+            '-o', '0',
+            // Threshold of gesture recognized
+            '-t', '125',
+            // Leniency of gesture angle
+            '-r', '25',
+            // Timeout time
+            '-m', '3200',
+        ];
+
+        this.gestures.forEach(gesture => {
+            command = command.concat(gesture);
+        });
+
+        this.gestureDaemon = subprocess(
+            command,
+            () => {},
+            err => logError(err),
+        );
+        this.emit('daemon-started', true);
+    }
+
+    killDaemon() {
+        if (this.gestureDaemon) {
+            this.gestureDaemon.force_exit();
+            this.gestureDaemon = undefined;
+            this.emit('daemon-destroyed', true);
+        }
+    }
+}
+
+const touchGesturesService = new TouchGestures();
+export default touchGesturesService;
diff --git a/devices/wim/config/hypr/main.conf b/devices/wim/config/hypr/main.conf
index 89419854..ff452779 100644
--- a/devices/wim/config/hypr/main.conf
+++ b/devices/wim/config/hypr/main.conf
@@ -18,10 +18,6 @@ monitor=desc:Samsung Electric Company C27JG5x HTOM100586, 2560x1440@144, 1920x12
 # Desc of monitor has comma, so force its default on everything and specify everything else
 monitor=,                                                 2560x1440@144, 1920x1560, 1
 
-exec-once = $HYPR_PATH/lisgd.sh & 
-
-# See https://wiki.hyprland.org/Configuring/Keywords/ for more
-
 # Execute your favorite apps at launch
 exec-once = bash -c "sleep 3; nm-applet"
 exec-once = bash -c "sleep 4; blueberry-tray"
diff --git a/devices/wim/config/hypr/scripts/lisgd.sh b/devices/wim/config/hypr/scripts/lisgd.sh
deleted file mode 100755
index 3d3cd1c5..00000000
--- a/devices/wim/config/hypr/scripts/lisgd.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-
-## https://www.reddit.com/r/swaywm/comments/ocec7k/comment/i93s0ma/
-## https://git.sr.ht/~mil/lisgd/tree/0.3.7/item/config.def.h
-
-function gestures {
-  lisgd -d /dev/input/by-path/platform-AMDI0010\:00-event -o 0 -t 125 -r 25 -m 3200 \
-    -g "1,UD,B,*,R,bash -c 'busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b false'" \
-    -g "1,DU,B,*,R,bash -c 'busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true'" \
-    -g "1,RL,R,*,R,bash -c 'hyprctl dispatch togglespecialworkspace spot'" \
-    -g "1,LR,R,*,R,bash -c 'hyprctl dispatch togglespecialworkspace spot'"
-}
-
-if pgrep lisgd ; then
-  killall -r lisgd
-  gestures
-else
-  gestures
-fi