Skip to content

Commit

Permalink
Implement session locking, unlocking
Browse files Browse the repository at this point in the history
Send the LockSessions message and UnlockSessions message via DBus to
Systemd Logind to lock/unlock the current user's desktop.
  • Loading branch information
Géza Búza committed Apr 4, 2019
1 parent 6982788 commit 4a96a15
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/session-lock-manager
12 changes: 5 additions & 7 deletions session-lock-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/elvetemedve/session-lock-manager/device"
"github.com/elvetemedve/session-lock-manager/authentication"
"github.com/elvetemedve/session-lock-manager/session"
"github.com/jochenvg/go-udev"
"os"
"fmt"
Expand All @@ -12,13 +13,10 @@ func main() {
serviceName := parseArguments()
scanner := &device.UdevScanner{&udev.Udev{}}
presence := &device.Presence{
authentication.AuthenticateCurrentUserAction(serviceName,
func(){
fmt.Println("Unlocking session.")
}, func(){}),
func(){
fmt.Println("Locking session.")
}}
authentication.AuthenticateCurrentUserAction(serviceName, session.Unlock, func(){
fmt.Fprintln(os.Stderr, "Authentication failed.")
}),
session.Lock}
_, done := presence.Scan(os.Stdout, scanner)
<-done
}
Expand Down
42 changes: 42 additions & 0 deletions session/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package session

import (
"log"
"fmt"
"github.com/godbus/dbus"
)

func Lock() {
error := callDbusMethod("LockSessions")
if error != nil {
fmt.Println(fmt.Sprintf("Failed to lock session: %s", error))
return
}

log.Println("Locking session.")
}

func Unlock() {
error := callDbusMethod("UnlockSessions")
if error != nil {
fmt.Println(fmt.Sprintf("Failed to lock session: %s", error))
return
}

log.Println("Unlocking session.")
}

func callDbusMethod(methodName string) (error) {
connection, error := dbus.SystemBus()
if error != nil {
fmt.Println(fmt.Sprintf("Failed to connectionect to session bus: %s", error))
return nil
}

object := connection.Object("org.freedesktop.login1", "/org/freedesktop/login1")
call := object.Call(fmt.Sprintf("org.freedesktop.login1.Manager.%s", methodName), 0)
if call.Err != nil {
return call.Err
}
return nil
}

0 comments on commit 4a96a15

Please sign in to comment.