Merge pull request #1 from kattudden/feature/systemd-service
Feature/systemd service
This commit is contained in:
33
README.md
33
README.md
@@ -1,32 +1,30 @@
|
|||||||
# swayidle-ctrl
|
# 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
|
## Features
|
||||||
|
|
||||||
- Starts a swayidle process in the background with configurable timeouts.
|
- stop / start the swayidle service.
|
||||||
- Terminates any existing swayidle process before starting a new one.
|
|
||||||
- Provides a flag to disable screen and display timeout functionalities.
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
This application can be run from the command line. Here's how:
|
This application can be run from the command line. Here's how:
|
||||||
|
|
||||||
```
|
```txt
|
||||||
swayidle-ctrl -t1 <timeout1> -t2 <timeout2> [-off]
|
swayidle-ctrl [-on|-off]
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
-t1 (default: 300): Sets the timeout in seconds for screen lock activation.
|
-on (default: false): Enables screen lock and display timeout functionalities.
|
||||||
-t2 (default: 600): Sets the timeout in seconds for display power off.
|
|
||||||
-off (default: false): Disables 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
|
``` bash
|
||||||
swayidle-ctrl -t1 300 -t2 600
|
swayidle-ctrl -on
|
||||||
```
|
```
|
||||||
|
|
||||||
Disable screen lock and display timeout:
|
Disable screen lock and display timeout:
|
||||||
@@ -45,13 +43,12 @@ swayidle-ctrl -off
|
|||||||
|
|
||||||
### Additional Notes
|
### Additional Notes
|
||||||
|
|
||||||
* This application uses `sh -c` to execute the swayidle command. You might need to adjust this based on your specific setup.
|
- 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.
|
- 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.
|
- 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:
|
This readme provides a basic overview of the application. You can customize it further by adding information about:
|
||||||
|
|
||||||
* Contributing guidelines
|
- Contributing guidelines
|
||||||
* License information
|
- License information
|
||||||
* Troubleshooting tips
|
- Troubleshooting tips
|
||||||
|
|||||||
111
main.go
111
main.go
@@ -1,60 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func killSwayidleProcess() error {
|
func startSwayidleService() error {
|
||||||
command := "ps -ef | grep swayidle | grep -v grep | grep -v ctrl"
|
command := "systemctl --user start swayidle.service"
|
||||||
cmd := exec.Command("sh", "-c", command)
|
cmd := exec.Command("sh", "-c", command)
|
||||||
|
|
||||||
output, err := cmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(strings.NewReader(string(output)))
|
|
||||||
var pids []string
|
|
||||||
for scanner.Scan() {
|
|
||||||
fields := strings.Fields(scanner.Text())
|
|
||||||
if len(fields) >= 2 {
|
|
||||||
pids = append(pids, fields[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Beenden der Prozesse
|
|
||||||
for _, pidStr := range pids {
|
|
||||||
pid, err := strconv.Atoi(pidStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
proc, err := os.FindProcess(pid)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
err = proc.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()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -63,23 +18,67 @@ func createSwayidleProcess(t1, t2 int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func stopSwayidleService() error {
|
||||||
t1 := flag.Int("t1", 300, "Set timeout for screenlock.")
|
command := "systemctl --user stop swayidle.service"
|
||||||
t2 := flag.Int("t2", 600, "Set timeout for display.")
|
cmd := exec.Command("sh", "-c", command)
|
||||||
disable := flag.Bool("off", false, "Disable screenlock and display timeout.")
|
|
||||||
|
|
||||||
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
// kill running swayidle instance.
|
// check current status of swayidle service.
|
||||||
err := killSwayidleProcess()
|
isRunning, err := getSwayidleServiceStatus()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*disable {
|
// start service if not already running.
|
||||||
err := createSwayidleProcess(*t1, *t2)
|
if *enable && !isRunning {
|
||||||
|
err := startSwayidleService()
|
||||||
if err != nil {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user