← paintstorm-distrobox-legacy

Fix event counting bug and improve OTD detection

d7e0cf29912e87d237d4499effa5bc073dd04499 · Claude · Jan 24, 2026 09:08

- Fixed "integer expected" error when counting events (grep -c returns 0, not empty)
- Use 'pgrep -f' instead of 'pgrep -x' to better detect otd-daemon
- Auto-detect OTD as running if virtual device is active (more reliable)
- Added error suppression for integer comparison to handle edge cases

The bug occurred because:
  grep -c returns "0" with exit code 1 when no matches found
  The '|| echo 0' caused output to be "0\n0"
  Integer comparison failed on this value

Fixed by using '|| true' and parameter expansion for safe defaults.

+11 −5 · 1 file changed

  • check-which-device-used.sh +11 −5
diff --git a/check-which-device-used.sh b/check-which-device-used.sh
index 5cc4555..ff25c39 100755
--- a/check-which-device-used.sh
+++ b/check-which-device-used.sh
@@ -28,7 +28,7 @@ VIRTUAL_STYLUS=$(xinput list | grep -i "OpenTabletDriver.*stylus" | head -1)
 VIRTUAL_STYLUS_ID=$(echo "$VIRTUAL_STYLUS" | grep -o 'id=[0-9]*' | cut -d= -f2)
 VIRTUAL_STYLUS_NAME=$(echo "$VIRTUAL_STYLUS" | sed 's/.*↳ //' | sed 's/id=.*//' | xargs)
 
-OTD_RUNNING=$(pgrep -x otd-daemon > /dev/null && echo "YES" || echo "NO")
+OTD_RUNNING=$(pgrep -f otd-daemon > /dev/null && echo "YES" || echo "NO")
 
 # Test devices
 echo "Testing devices (move your pen for 5 seconds)..."
@@ -42,9 +42,10 @@ VIRTUAL_EVENTS=0
 if [ -n "$NATIVE_STYLUS_ID" ]; then
     echo -n "Testing native device... "
     NATIVE_OUTPUT=$(timeout 5 xinput test "$NATIVE_STYLUS_ID" 2>&1)
-    NATIVE_EVENTS=$(echo "$NATIVE_OUTPUT" | grep -c "motion" || echo "0")
+    NATIVE_EVENTS=$(echo "$NATIVE_OUTPUT" | grep -c "motion" || true)
+    NATIVE_EVENTS=${NATIVE_EVENTS:-0}
 
-    if [ "$NATIVE_EVENTS" -gt 0 ]; then
+    if [ "$NATIVE_EVENTS" -gt 0 ] 2>/dev/null; then
         NATIVE_ACTIVE="ACTIVE"
         echo -e "${GREEN}✓${NC}"
     else
@@ -62,9 +63,10 @@ fi
 if [ -n "$VIRTUAL_STYLUS_ID" ]; then
     echo -n "Testing OTD virtual device... "
     VIRTUAL_OUTPUT=$(timeout 5 xinput test "$VIRTUAL_STYLUS_ID" 2>&1)
-    VIRTUAL_EVENTS=$(echo "$VIRTUAL_OUTPUT" | grep -c "motion" || echo "0")
+    VIRTUAL_EVENTS=$(echo "$VIRTUAL_OUTPUT" | grep -c "motion" || true)
+    VIRTUAL_EVENTS=${VIRTUAL_EVENTS:-0}
 
-    if [ "$VIRTUAL_EVENTS" -gt 0 ]; then
+    if [ "$VIRTUAL_EVENTS" -gt 0 ] 2>/dev/null; then
         VIRTUAL_ACTIVE="ACTIVE"
         echo -e "${GREEN}✓${NC}"
     else
@@ -112,6 +114,10 @@ else
 fi
 
 echo ""
+# If virtual device is active, OTD must be running even if pgrep doesn't find it
+if [ "$VIRTUAL_ACTIVE" = "ACTIVE" ] && [ "$OTD_RUNNING" = "NO" ]; then
+    OTD_RUNNING="YES (detected via virtual device)"
+fi
 printf "%-40s ${BOLD}%-8s${NC}\n" "otd-daemon status:" "$OTD_RUNNING"
 echo ""