aboutsummaryrefslogtreecommitdiffstats
path: root/src/Pager/Backend/X11.hs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-12 02:35:57 +0000
committertv <tv@krebsco.de>2026-11-12 02:35:57 +0000
commit252f3c6f4728c19a7ee171b446e1ce168686b44b (patch)
tree1e930362ed70ee3258168dbf3ff421d02acc895b /src/Pager/Backend/X11.hs
parente974ac31ff1e08ca681db3c4108ae2a596e5655c (diff)
add a rivermonad backend next to the EWMH oneHEADmaster
desktop-pager and desktops now also work on rivermonad, a window manager for the river Wayland compositor, over its IPC socket. The window manager access of desktop-pager moves behind Pager.Backend, with the existing code in Pager.Backend.X11 and the new one in Pager.Backend.Rivermonad. desktops gets the same choice. rivermonad is used when its socket for this Wayland display exists; DESKTOP_PAGER_BACKEND=x11 or =rivermonad overrides that. Rivermonad.Client, in the library, is the IPC client: a snapshot of all workspaces and windows, and actions to view a workspace, move a window and set the list of workspaces. On X11, desktops are now looked up by name when the pager acts instead of in the list read at startup.
Diffstat (limited to 'src/Pager/Backend/X11.hs')
-rw-r--r--src/Pager/Backend/X11.hs129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/Pager/Backend/X11.hs b/src/Pager/Backend/X11.hs
new file mode 100644
index 0000000..9df5212
--- /dev/null
+++ b/src/Pager/Backend/X11.hs
@@ -0,0 +1,129 @@
+-- | The pager on an EWMH window manager on X11.
+module Pager.Backend.X11
+ ( getScreenGeometry
+ , getWorkspaces
+ , viewWorkspace
+ , shiftWindowToWorkspace
+ ) where
+
+import Data.Bits (testBit)
+import Data.Maybe (fromMaybe)
+import Data.Set (Set)
+import Data.Text (Text)
+import Pager.Types
+import qualified Data.List as List
+import qualified Data.Map as Map
+import qualified Data.Set as Set
+import qualified Data.Text as Text
+import qualified Graphics.X11 as X11
+import qualified Graphics.X11.EWMH as X11
+import qualified Graphics.X11.Extra as X11
+import qualified Graphics.X11.Xlib.Extras as X11
+
+
+getGeometry :: X11.Display -> X11.Window -> IO Geometry
+getGeometry d w = do
+ (_, x, y, width, height, _, _) <- X11.getGeometry d w
+ return Geometry
+ { geometry_x = fromIntegral x
+ , geometry_y = fromIntegral y
+ , geometry_width = fromIntegral width
+ , geometry_height = fromIntegral height
+ }
+
+getScreenGeometry :: IO Geometry
+getScreenGeometry =
+ X11.withDefaultDisplay $ \display ->
+ getGeometry display (X11.defaultRootWindow display)
+
+getWorkspaces :: Geometry -> [Int] -> IO [Workspace]
+getWorkspaces screenGeometry focusWindows =
+ X11.withDefaultDisplay $ \display ->
+ getWorkspaces' display screenGeometry (Set.fromList (map fromIntegral focusWindows))
+
+getWorkspaces' :: X11.Display -> Geometry -> Set X11.Window -> IO [Workspace]
+getWorkspaces' display screenGeometry focusWindows = do
+
+ let rootWindow = X11.defaultRootWindow display
+
+ currentDesktop <- fromMaybe 0 <$> X11.getCurrentDesktop display
+
+ workspaces <- do
+ names <- zip [0..] . fromMaybe [] <$> X11.getDesktopNames display
+ let
+ ws =
+ flip map names $ \(index, name) ->
+ Workspace
+ { workspace_geometry = screenGeometry
+ , workspace_focused = currentDesktop == index
+ , workspace_name = Text.pack name
+ , workspace_windows = []
+ }
+ return $ Map.fromList $ zip [0..] ws
+
+ clientList <-
+ maybe [] (map fromIntegral) <$>
+ X11.getWindowProperty32 display X11._NET_CLIENT_LIST rootWindow
+
+ let
+ f w = do
+ title <- X11.getWindowTitle display w
+ desktop <- fromMaybe 0 <$> X11.getWindowDesktop display w
+ geometry <- getGeometry display w
+
+ wm_hints <- X11.getWMHints display w
+
+ wm_state <-
+ maybe [] (map fromIntegral) <$>
+ X11.getWindowProperty32 display X11._NET_WM_STATE w
+
+ let urgent =
+ testBit (X11.wmh_flags wm_hints) X11.urgencyHintBit ||
+ elem X11._NET_WM_STATE_DEMANDS_ATTENTION wm_state
+
+ let
+ window =
+ Window
+ { window_id = fromIntegral w
+ , window_title = Text.pack $ fromMaybe "" title
+ , window_geometry = geometry
+ , window_focused = Set.member w focusWindows
+ , window_urgent = urgent
+ }
+
+ return ( window, desktop )
+
+ clientList' <- mapM f clientList
+
+ return
+ $ map (\ws -> ws { workspace_windows =
+ uncurry (<>) $
+ List.partition window_focused (workspace_windows ws)
+ })
+ $ Map.elems
+ $ foldr
+ (\(w, i) ->
+ Map.adjust (\ws -> ws { workspace_windows = w : workspace_windows ws })
+ i
+ )
+ workspaces
+ clientList'
+
+-- EWMH addresses desktops by index.
+withDesktopIndex :: Text -> (X11.Display -> Int -> IO ()) -> IO ()
+withDesktopIndex name f =
+ X11.withDefaultDisplay $ \display -> do
+ names <- fromMaybe [] <$> X11.getDesktopNames display
+ case Text.unpack name `List.elemIndex` names of
+ Just index -> f display index
+ Nothing -> fail $ "no such desktop: " <> show name
+
+viewWorkspace :: Text -> IO ()
+viewWorkspace name =
+ withDesktopIndex name $ \display index ->
+ X11.switchToDesktop display (fromIntegral index)
+
+shiftWindowToWorkspace :: Int -> Text -> IO ()
+shiftWindowToWorkspace window name =
+ withDesktopIndex name $ \display index ->
+ X11.moveWindowToDesktop display (fromIntegral window) (fromIntegral index)