Skip to content

Commit

Permalink
Use XDG_SESSION_ID for LockedHint if available
Browse files Browse the repository at this point in the history
This makes it possible to detect locked session in a multi-session
setup. Beware, this is niche among niches: very few people use user
systemd and multiple X sessions together, as it's not supported by any
desktop environments.
  • Loading branch information
liskin committed Mar 5, 2021
1 parent d5872b8 commit e8c41dc
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Capture/X11.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import System.IO.Error (catchIOError)
import Control.Applicative
import Data.Either
import Data.Maybe
import Data.String
import Data.Time.Clock
import System.Environment
import System.IO
import qualified Data.MyText as T

Expand Down Expand Up @@ -167,14 +169,19 @@ isScreenSaverActive dpy = do
-- TODO: describe this better
-- dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1/session/self "org.freedesktop.login1.Session.SetLockedHint" boolean:false
isSessionLocked :: IO Bool
isSessionLocked = bracket D.connectSystem D.disconnect getLockedHint
`catch` (return . const False . D.clientErrorMessage)
isSessionLocked = do
xdgSessionId <- lookupEnv "XDG_SESSION_ID"
-- When running as systemd user unit, …/session/self doesn't work so we
-- try $XDG_SESSION_ID and fall back to …/session/auto if not set, which
-- acts like self if run directly from a session, or the user's display
-- session otherwise.
let session = fromMaybe "auto" xdgSessionId
bracket D.connectSystem D.disconnect (getLockedHint session)
`catch` (return . const False . D.clientErrorMessage)
where
dest = "org.freedesktop.login1"
-- …/session/auto is the caller's own session if they have one,
-- otherwise their user's display session
object = "/org/freedesktop/login1/session/auto"
object session = fromString $ "/org/freedesktop/login1/session/" <> session
interface = "org.freedesktop.login1.Session"
property = "LockedHint"
methodCall = (D.methodCall object interface property){ D.methodCallDestination = Just dest }
getLockedHint c = fmap (fromRight False) $ D.getPropertyValue c methodCall
methodCall obj = (D.methodCall obj interface property){ D.methodCallDestination = Just dest }
getLockedHint session c = fmap (fromRight False) $ D.getPropertyValue c $ methodCall $ object session

0 comments on commit e8c41dc

Please sign in to comment.