From 252f3c6f4728c19a7ee171b446e1ce168686b44b Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 12 Nov 2026 02:35:57 +0000 Subject: add a rivermonad backend next to the EWMH one 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. --- src/desktops.hs | 81 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 20 deletions(-) (limited to 'src/desktops.hs') diff --git a/src/desktops.hs b/src/desktops.hs index 7bf5f65..12d227e 100644 --- a/src/desktops.hs +++ b/src/desktops.hs @@ -1,10 +1,13 @@ module Main (main) where import Data.List (elemIndex) +import qualified Data.List as List import Data.Map (Map) import Data.Maybe (fromMaybe) -import Graphics.X11.EWMH (getCurrentDesktop, getDesktopNames, setDesktopNames, switchToDesktop) import Graphics.X11.Extra (withDefaultDisplay) +import qualified Data.Text as Text +import qualified Graphics.X11.EWMH as EWMH +import qualified Rivermonad.Client as Rivermonad import System.Environment (getArgs) import qualified Data.Aeson as Aeson import qualified Data.ByteString.Lazy.Char8 as LBS8 @@ -31,48 +34,86 @@ main = "switch" : name : [] -> switchToWorkspace name x -> error $ "bad command: " <> show x +-- | The list of desktops of the running window manager. +data Desktops = Desktops + { getDesktopNames :: IO (Maybe [String]) + , setDesktopNames :: [String] -> IO () + -- ^ Create, remove and rename desktops to get this list. + , getCurrentDesktop :: IO (Maybe Int) + , switchToDesktopNamed :: String -> IO () + } + +-- | rivermonad if its socket is there, EWMH on X11 otherwise. +withDesktops :: (Desktops -> IO a) -> IO a +withDesktops f = + Rivermonad.selected >>= \case + True -> f rivermonad + False -> withDefaultDisplay (f . ewmh) + where + ewmh dpy = + Desktops + { getDesktopNames = EWMH.getDesktopNames dpy + , setDesktopNames = \names -> EWMH.setDesktopNames names dpy + , getCurrentDesktop = fmap fromIntegral <$> EWMH.getCurrentDesktop dpy + , switchToDesktopNamed = \name -> do + names <- fromMaybe [] <$> EWMH.getDesktopNames dpy + let Just i = name `elemIndex` names + EWMH.switchToDesktop dpy (fromIntegral i) + } + + rivermonad = + Desktops + { getDesktopNames = + Just . map (Text.unpack . Rivermonad.workspace_name) <$> snapshot + , setDesktopNames = + Rivermonad.setWorkspaces . map Text.pack + , getCurrentDesktop = + List.findIndex Rivermonad.workspace_focused <$> snapshot + , switchToDesktopNamed = + Rivermonad.viewWorkspace . Text.pack + } + + snapshot = Rivermonad.snapshot_workspaces <$> Rivermonad.getSnapshot + getWorkspaces :: IO () getWorkspaces = - LBS8.putStrLn =<< Aeson.encode <$> withDefaultDisplay getDesktopNames + LBS8.putStrLn =<< Aeson.encode <$> withDesktops getDesktopNames addWorkspaces :: [String] -> IO () addWorkspaces names = - withDefaultDisplay $ \dpy -> do + withDesktops $ \wm -> do names' <- (<>names) . - fromMaybe [] <$> getDesktopNames dpy - setDesktopNames names' dpy + fromMaybe [] <$> getDesktopNames wm + setDesktopNames wm names' removeWorkspaces :: [String] -> IO () removeWorkspaces names = - withDefaultDisplay $ \dpy -> do + withDesktops $ \wm -> do names' <- filter (not . flip Set.member (Set.fromList names)) . - fromMaybe [] <$> getDesktopNames dpy - setDesktopNames names' dpy + fromMaybe [] <$> getDesktopNames wm + setDesktopNames wm names' renameCurrentWorkspace :: String -> IO () renameCurrentWorkspace name = - withDefaultDisplay $ \dpy -> do - i <- maybe 0 fromIntegral <$> getCurrentDesktop dpy - names <- fromMaybe [] <$> getDesktopNames dpy + withDesktops $ \wm -> do + i <- fromMaybe 0 <$> getCurrentDesktop wm + names <- fromMaybe [] <$> getDesktopNames wm let names' = take i names <> [name] <> drop (i + 1) names - setDesktopNames names' dpy + setDesktopNames wm names' renameWorkspaces :: Map String String-> IO () renameWorkspaces renames = do - withDefaultDisplay $ \dpy -> do + withDesktops $ \wm -> do names' <- map (\name -> fromMaybe name (Map.lookup name renames)) . - fromMaybe [] <$> getDesktopNames dpy - setDesktopNames names' dpy + fromMaybe [] <$> getDesktopNames wm + setDesktopNames wm names' setWorkspaces :: [String] -> IO () -setWorkspaces = withDefaultDisplay . setDesktopNames +setWorkspaces names = withDesktops (`setDesktopNames` names) switchToWorkspace :: String -> IO () switchToWorkspace name = - withDefaultDisplay $ \dpy -> do - names <- fromMaybe [] <$> getDesktopNames dpy - let Just i = name `elemIndex` names - switchToDesktop dpy (fromIntegral i) + withDesktops (`switchToDesktopNamed` name) -- cgit v1.3.1