Group lockscreen and display suspension related code
This commit is contained in:
48
xdg/display/display.go
Normal file
48
xdg/display/display.go
Normal file
@ -0,0 +1,48 @@
|
||||
package display
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
// Xset monitors/controls the display using xset
|
||||
type Xset struct {}
|
||||
|
||||
// Suspend the display
|
||||
func (Xset) Suspend() error {
|
||||
cmd := exec.Command("xset",
|
||||
"dpms",
|
||||
"force",
|
||||
"suspend",
|
||||
)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
// IsOn determines if the display is on
|
||||
func (Xset) IsOn() (bool, error) {
|
||||
cmd := exec.Command("xset", "q")
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return determineIfDisplayIsOn(string(outputBytes)), nil
|
||||
}
|
||||
|
||||
func determineIfDisplayIsOn(xsetOutput string) bool {
|
||||
if ! strings.Contains(xsetOutput, "Monitor") {
|
||||
// after booting xset q is missing this line
|
||||
// lacking better ideas assume display is on
|
||||
return true
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`Monitor is (\w+)`)
|
||||
matches := re.FindStringSubmatch(xsetOutput)
|
||||
if len(matches) >= 2 {
|
||||
if matches[1] == "On" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
33
xdg/keypressdetector/evdev.c
Normal file
33
xdg/keypressdetector/evdev.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include "evdev.h"
|
||||
|
||||
|
||||
int block_until_keypress(const char* keyboardPath) {
|
||||
int keyboardFd = open(keyboardPath, O_RDONLY);
|
||||
if (keyboardFd < 0) {
|
||||
return keyboardFd;
|
||||
}
|
||||
|
||||
int eventSize = sizeof(struct input_event);
|
||||
int bytesRead = 0;
|
||||
struct input_event events[NUM_EVENTS];
|
||||
|
||||
int readEvents = 1;
|
||||
while(readEvents) {
|
||||
bytesRead = read(keyboardFd, events, eventSize * NUM_EVENTS);
|
||||
if (bytesRead < 0) {
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
for(int i = 0; i < (bytesRead / eventSize); ++i) {
|
||||
struct input_event event = events[i];
|
||||
if(event.type == EV_KEY) {
|
||||
if(event.value == 1) {
|
||||
readEvents = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(keyboardFd);
|
||||
return 0;
|
||||
}
|
28
xdg/keypressdetector/evdev.go
Normal file
28
xdg/keypressdetector/evdev.go
Normal file
@ -0,0 +1,28 @@
|
||||
package keypressdetector
|
||||
|
||||
// #cgo CFLAGS: -g -Wall
|
||||
// #include "evdev.h"
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
|
||||
// Evdev can detect keypresses on /dev/input devices
|
||||
type Evdev struct {}
|
||||
|
||||
// BlockUntilKeypress waits until a key is pressed on the specified device
|
||||
func (Evdev) BlockUntilKeypress(devicePath string) error {
|
||||
path := C.CString(devicePath)
|
||||
defer C.free(unsafe.Pointer(path))
|
||||
|
||||
ret := C.block_until_keypress(path)
|
||||
if ret < 0 {
|
||||
return syscall.Errno(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
14
xdg/keypressdetector/evdev.h
Normal file
14
xdg/keypressdetector/evdev.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <linux/input.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
#define NUM_EVENTS 128
|
||||
|
||||
int block_until_keypress(const char* keyboardPath);
|
29
xdg/lockscreen/lockscreen.go
Normal file
29
xdg/lockscreen/lockscreen.go
Normal file
@ -0,0 +1,29 @@
|
||||
package lockscreen
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
// XDG uses qdbus to monitor the freedesktop screensaver status
|
||||
type XDG struct {}
|
||||
|
||||
// IsActive checks whether the screen is locked (checks if the screensaver is active)
|
||||
func (XDG) IsActive() (bool, error) {
|
||||
cmd := exec.Command("qdbus",
|
||||
"org.kde.screensaver",
|
||||
"/ScreenSaver",
|
||||
"org.freedesktop.ScreenSaver.GetActive",
|
||||
)
|
||||
outputBytes, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
output := string(outputBytes)
|
||||
|
||||
if strings.HasPrefix(output, "true") {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
Reference in New Issue
Block a user