From ccf2d8257d721cf0033e57677971bc129739422c Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 17 Mar 2022 09:08:27 +0100 Subject: [PATCH] chore: start gyrolock with udev to ensure iio device is well available --- 80-gyrolock.rules | 18 ++++++++++++++++++ Makefile | 3 ++- go.mod | 2 +- gyrolock.service | 9 +++------ main.go | 5 ++++- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 80-gyrolock.rules diff --git a/80-gyrolock.rules b/80-gyrolock.rules new file mode 100644 index 0000000..5e27a5b --- /dev/null +++ b/80-gyrolock.rules @@ -0,0 +1,18 @@ +# gyrolock +# Lock sessions based on gyroscope IIO sensor + +ACTION=="remove", GOTO="iio_sensor_gyrolock_end" + +# Set the sensor type for all the types we recognise +SUBSYSTEM=="iio", TEST=="in_accel_x_raw", TEST=="in_accel_y_raw", TEST=="in_accel_z_raw", ENV{IIO_SENSOR_GYROLOCK_TYPE}+="iio-poll-accel" +SUBSYSTEM=="iio", TEST=="scan_elements/in_accel_x_en", TEST=="scan_elements/in_accel_y_en", TEST=="scan_elements/in_accel_z_en", ENV{IIO_SENSOR_GYROLOCK_TYPE}+="iio-buffer-accel" +SUBSYSTEM=="iio", TEST=="scan_elements/in_rot_from_north_magnetic_tilt_comp_en", ENV{IIO_SENSOR_GYROLOCK_TYPE}+="iio-buffer-compass" +SUBSYSTEM=="iio", TEST=="in_proximity_raw", ENV{IIO_SENSOR_GYROLOCK_TYPE}+="iio-poll-proximity" +SUBSYSTEM=="input", ENV{ID_INPUT_ACCELEROMETER}=="1", ENV{IIO_SENSOR_GYROLOCK_TYPE}+="input-accel" + +ENV{IIO_SENSOR_GYROLOCK_TYPE}=="", GOTO="iio_sensor_gyrolock_end" + +# We got here because we have a sensor type, which means we need the service +TAG+="systemd", ENV{SYSTEMD_WANTS}+="gyrolock.service" + +LABEL="iio_sensor_gyrolock_end" diff --git a/Makefile b/Makefile index 6f7660c..84c77fa 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ install: install -Dm755 -t "$(BIN_DIR)/" $(BIN) install -Dm644 -t "$(SHARE_DIR)/licenses/$(BIN)/" LICENSE.md install -Dm644 -t "$(LIB_DIR)/systemd/system/" $(BIN).service + install -Dm644 -t "$(LIB_DIR)/udev/rules.d/" 80-$(BIN).rules install -Dm644 -t "$(LIB_DIR)/systemd/user/" swaylock.service .PHONY: vendor @@ -47,7 +48,7 @@ dist: clean vendor build (cd "$(TMP)" && tar -cvzf "$(BIN)-$(VERSION)-src.tar.gz" "$(BIN)-$(VERSION)") mkdir "$(TMP)/$(BIN)-$(VERSION)-linux64" - cp LICENSE.md $(BIN) $(BIN).service swaylock.service "$(TMP)/$(BIN)-$(VERSION)-linux64" + cp LICENSE.md $(BIN) $(BIN).service 80-$(BIN).rules swaylock.service "$(TMP)/$(BIN)-$(VERSION)-linux64" (cd "$(TMP)" && tar -cvzf "$(BIN)-$(VERSION)-linux64.tar.gz" "$(BIN)-$(VERSION)-linux64") mkdir -p dist diff --git a/go.mod b/go.mod index 4b03f76..add70d1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/cyrinux/gyrolock -go 1.17 +go 1.18 require github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf diff --git a/gyrolock.service b/gyrolock.service index ddb638a..a034ba1 100644 --- a/gyrolock.service +++ b/gyrolock.service @@ -1,11 +1,11 @@ [Unit] Description=Lock sessions if laptop is shake -ConditionPathIsDirectory=/sys/bus/iio -After=systemd-udevd.service [Service] +# Type=dbus +# BusName=name.levis.GyroLock ExecStart=/usr/bin/gyrolock -Restart=always +Restart=on-failure Environment="SENSITIVITY=10" # Lockdown @@ -17,6 +17,3 @@ PrivateTmp=true RestrictAddressFamilies=AF_UNIX MemoryDenyWriteExecute=true RestrictRealtime=true - -[Install] -WantedBy=basic.target diff --git a/main.go b/main.go index 5c2d3fb..a50861f 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,9 @@ type Sensor struct { } func main() { + if !isRoot() { + log.Println("It's recommanded to run it as root, running as user will be easy to disable !") + } debug, _ := strconv.ParseBool(os.Getenv("DEBUG")) sensitivity, err := strconv.ParseInt(os.Getenv("SENSITIVITY"), 0, 16) if err != nil || sensitivity < 0 { @@ -135,7 +138,7 @@ func LockSessions(debug bool, seat string) { log.Println("GyroLock lock sessions !") } else { conn.LockSession(seat) - log.Println("GyroLock lock session %v !", seat) + log.Printf("GyroLock lock session %v !", seat) } } else { log.Println("GyroLock would lock sessions !")