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.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 import qualified Data.Map.Strict as Map import qualified Data.Set as Set main :: IO () main = getArgs >>= \case [] -> getWorkspaces "get" : [] -> getWorkspaces "add" : names -> addWorkspaces names "remove" : names -> removeWorkspaces names "rename" : name : [] -> renameCurrentWorkspace name "rename" : args | not (null args) && length args `mod` 2 == 0 -> renameWorkspaces renames where renames = Map.fromList (pairUp args) pairUp = \case (x:y:xs) -> (x,y) : pairUp xs _ -> [] "set" : names -> setWorkspaces names "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 <$> withDesktops getDesktopNames addWorkspaces :: [String] -> IO () addWorkspaces names = withDesktops $ \wm -> do names' <- (<>names) . fromMaybe [] <$> getDesktopNames wm setDesktopNames wm names' removeWorkspaces :: [String] -> IO () removeWorkspaces names = withDesktops $ \wm -> do names' <- filter (not . flip Set.member (Set.fromList names)) . fromMaybe [] <$> getDesktopNames wm setDesktopNames wm names' renameCurrentWorkspace :: String -> IO () renameCurrentWorkspace name = withDesktops $ \wm -> do i <- fromMaybe 0 <$> getCurrentDesktop wm names <- fromMaybe [] <$> getDesktopNames wm let names' = take i names <> [name] <> drop (i + 1) names setDesktopNames wm names' renameWorkspaces :: Map String String-> IO () renameWorkspaces renames = do withDesktops $ \wm -> do names' <- map (\name -> fromMaybe name (Map.lookup name renames)) . fromMaybe [] <$> getDesktopNames wm setDesktopNames wm names' setWorkspaces :: [String] -> IO () setWorkspaces names = withDesktops (`setDesktopNames` names) switchToWorkspace :: String -> IO () switchToWorkspace name = withDesktops (`switchToDesktopNamed` name)