diff --git a/README.md b/README.md index bb3f8ff..0b51542 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,65 @@ # swayidle-ctrl -Control swayidle timeout settings. + +This Go application allows you to manage a swayidle process for screen and display timeout on Sway desktops. + +## Features + +- Starts a swayidle process in the background with configurable timeouts. +- Terminates any existing swayidle process before starting a new one. +- Provides a flag to disable screen and display timeout functionalities. + +## Usage + +This application can be run from the command line. Here's how: + +``` +swayidle-ctrl -t1 -t2 [-off] + +Arguments: + -t1 (default: 300): Sets the timeout in seconds for screen lock activation. + -t2 (default: 600): Sets the timeout in seconds for display power off. + -off (default: false): Disables screen lock and display timeout functionalities. +``` + +### Example: + +Start swayidle with 5 minutes screen lock timeout and 10 minutes display power off timeout: + +``` bash +swayidle-ctrl -t1 300 -t2 600 +``` + +Disable screen lock and display timeout: + +```bash +swayidle-ctrl -off +``` + +## Installation + +1. Download the source code or clone the repository (if applicable). +2. Run `go build` to create the executable file (`swayidle-manager`). +3. Place the executable in a directory accessible from your PATH environment variable. + +**Alternatively, you can run the application directly from the source code using `go run main.go`.** + +### Dependencies + +This application requires the following Go package: + +* `github.com/shirou/gopsutil/process` - for process management functionalities. + +**Make sure you have these dependencies installed before running the application.** + +### Additional Notes + +* This application uses `sh -c` to execute the swayidle command. You might need to adjust this based on your specific setup. +* The application terminates any existing `swayidle` process before starting a new one. This ensures you don't have multiple instances running concurrently. +* Consider adding support for additional swayidle functionalities or configuration options in future versions. + + +This readme provides a basic overview of the application. You can customize it further by adding information about: + +* Contributing guidelines +* License information +* Troubleshooting tips \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ff0c9df --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module kattudden/swayidle-ctrl + +go 1.23.3 + +require github.com/shirou/gopsutil v3.21.11+incompatible + +require ( + github.com/go-ole/go-ole v1.3.0 // indirect + github.com/tklauser/go-sysconf v0.3.14 // indirect + github.com/tklauser/numcpus v0.9.0 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect + golang.org/x/sys v0.28.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..80d7a27 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU= +github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= +github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo= +github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS604NSRyI= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..be691ec --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "flag" + "fmt" + "os/exec" + "syscall" + + "github.com/shirou/gopsutil/process" +) + +func killSwayidleProcess() error { + processes, _ := process.Processes() + for _, process := range processes { + name, _ := process.Name() + if name == "swayidle" { + err := process.Kill() + if err != nil { + return err + } + } + } + return nil +} + +func createSwayidleProcess(t1, t2 int) error { + command := fmt.Sprintf("swayidle -w timeout %d 'swaylock -f -c 3B4252' timeout %d 'swaymsg \"output * power off\"' resume 'swaymsg \"output * power on\"' before-sleep 'swaylock -f -c 3B4252'", t1, t2) + cmd := exec.Command("sh", "-c", command) + + cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} + + err := cmd.Start() + if err != nil { + return err + } + + return nil +} + +func main() { + t1 := flag.Int("t1", 300, "Set timeout for screenlock.") + t2 := flag.Int("t2", 600, "Set timeout for display.") + disable := flag.Bool("off", false, "Disable screenlock and display timeout.") + + flag.Parse() + + // kill running swayidle instance. + err := killSwayidleProcess() + if err != nil { + panic(err) + } + + if !*disable { + err := createSwayidleProcess(*t1, *t2) + if err != nil { + panic(err) + } + } +}