5 Commits

Author SHA1 Message Date
kattudden
c991eb9c00 Update Readme. 2025-01-07 16:24:59 +01:00
kattudden
242b99d075 Use Systemd Service. 2025-01-07 16:15:19 +01:00
Michael Lehmann
796379826f Update readme 2024-12-19 23:06:40 +01:00
Michael Lehmann
db40fbdc2d Use only go core modules. 2024-12-19 23:01:51 +01:00
Michael Lehmann
cc85816e3f Fix missing vendor dir 2024-12-19 22:19:04 +01:00
4 changed files with 71 additions and 82 deletions

View File

@@ -1,32 +1,30 @@
# swayidle-ctrl
This Go application allows you to manage a swayidle process for screen and display timeout on Sway desktops.
This Go application allows you to enable or disable the swayidle service.
It would also be possible to do it directly with systemctl...
## 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.
- stop / start the swayidle service.
## Usage
This application can be run from the command line. Here's how:
```
swayidle-ctrl -t1 <timeout1> -t2 <timeout2> [-off]
```txt
swayidle-ctrl [-on|-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.
-on (default: false): Enables screen lock and display timeout functionalities.
-off (default: false): Disables screen lock and display timeout functionalities.
```
### Example:
### Examples
Start swayidle with 5 minutes screen lock timeout and 10 minutes display power off timeout:
Enable screen lock and display timeout:
``` bash
swayidle-ctrl -t1 300 -t2 600
swayidle-ctrl -on
```
Disable screen lock and display timeout:
@@ -38,28 +36,19 @@ 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`).
2. Run `go build` to create the executable file (`swayidle-ctrl`).
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 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
- Contributing guidelines
- License information
- Troubleshooting tips

10
go.mod
View File

@@ -1,13 +1,3 @@
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
)

15
go.sum
View File

@@ -1,15 +0,0 @@
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=

85
main.go
View File

@@ -4,31 +4,12 @@ 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)
func startSwayidleService() error {
command := "systemctl --user start swayidle.service"
cmd := exec.Command("sh", "-c", command)
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
err := cmd.Start()
if err != nil {
return err
@@ -37,23 +18,67 @@ func createSwayidleProcess(t1, t2 int) error {
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.")
func stopSwayidleService() error {
command := "systemctl --user stop swayidle.service"
cmd := exec.Command("sh", "-c", command)
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func getSwayidleServiceStatus() (bool, error) {
command := "systemctl --user is-active --quiet swayidle.service"
cmd := exec.Command("sh", "-c", command)
err := cmd.Run()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
/*
Get exit code to evaluate service status.
3 == not Running.
Everything else indicating an Error.
*/
if exitError.ExitCode() == 3 {
return false, nil
}
}
return false, err
}
// Der Service läuft
return true, nil
}
func main() {
enable := flag.Bool("on", false, "Enable screenlock and display timeout.")
disable := flag.Bool("off", false, "Disable screenlock and display timeout.")
flag.Parse()
// kill running swayidle instance.
err := killSwayidleProcess()
// check current status of swayidle service.
isRunning, err := getSwayidleServiceStatus()
if err != nil {
panic(err)
}
if !*disable {
err := createSwayidleProcess(*t1, *t2)
// start service if not already running.
if *enable && !isRunning {
err := startSwayidleService()
if err != nil {
panic(err)
fmt.Println("Failed to start swayidle Service!")
return
}
}
// stop service if not already stoped.
if *disable && isRunning {
err := stopSwayidleService()
if err != nil {
fmt.Println("Failed to stop swayidle Service!")
return
}
}
}