Skip to content

Commit

Permalink
chore: start gyrolock with udev to ensure iio device is well available
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Mar 17, 2022
1 parent a83a10b commit ccf2d82
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
18 changes: 18 additions & 0 deletions 80-gyrolock.rules
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 3 additions & 6 deletions gyrolock.service
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,3 @@ PrivateTmp=true
RestrictAddressFamilies=AF_UNIX
MemoryDenyWriteExecute=true
RestrictRealtime=true

[Install]
WantedBy=basic.target
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 !")
Expand Down

0 comments on commit ccf2d82

Please sign in to comment.