1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
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)
|