Search code examples
xmonadxmobar

XMonad: how to show the currently visible workspace on xmobar when using multiple screens?


I'm using XMonad in a setup with multiple physical screens.

I would like each of the physical screens to have an instance of xmobar which
shows which workspace is visible on that particular screen, regardless of whether
that workspace is "current"/"active" or not.

E.g.

+--------------+  +--------------+                                                      
|              |  |              |                                                      
|              |  |              |                                                      
|              |  |              |                                                      
|              |  |              |                                                      
+--------------+  +--------------+                                                      
|Workspace 3   |  |Workspace 5   |                                                      
+--------------+  +--------------+                                                      

My current (minimized for clarity) xmonad.hs is below.

import XMonad                                                                           
import XMonad.Layout.NoBorders                                                          
import XMonad.Hooks.ManageDocks                                                         
import XMonad.Hooks.DynamicLog                                                          
import XMonad.Util.Run                                                                  
import XMonad.Layout.IndependentScreens                                                 
                                                                                        
main = do                                                                               
    n <- countScreens                                                                   
    xmprocs <- mapM (\i -> spawnPipe $ "xmobar" ++ " -x " ++ show i) [0..n-1]           
                                                                                        
    xmonad $ docks def                                                                  
        { layoutHook = avoidStruts $ smartBorders $ layoutHook defaultConfig            
        , logHook = mapM_ (\xmobarPipe -> dynamicLogWithPP $ def                        
            { ppOutput = hPutStrLn xmobarPipe                                           
            , ppCurrent = \s -> s                                                       
            , ppVisible = \s -> ""                                                      
            , ppHidden = \s -> ""                                                       
            , ppLayout = \s -> ""                                                       
            , ppTitle = \s -> ""                                                        
            }) xmprocs                                                                  
        }                                                                               

That is, I managed to spawn 2 instances of xmobar, one for each screen. However it simply shows the currently active Workspace (across screens) on both screens' xmobar. E.g. it would show:

+--------------+  +--------------+                                                      
|              |  |              |                                                      
|              |  |              |                                                      
|              |  |              |                                                      
|              |  |              |                                                      
+--------------+  +--------------+                                                      
|Workspace 3   |  |Workspace 3   |                                                      
+--------------+  +--------------+                                                      

Now, how do I achieve what I actually want?

I think the configuration here
https://github.com/nwf/xconfig/blob/208e6d6ce48fba45ec30bb1df1389f9ff2263edd/xmonad/lib/XMonad/Actions/XMobars.hs#L163 might contain hints to the answer but I'm not proficient enough in Haskell to
work back from that example to something minimal that I can use.


Solution

  • This works:

    import GHC.IO.Handle.Types (Handle)
    
    import XMonad (MonadIO, WorkspaceId, Layout, Window, ScreenId, ScreenDetail, WindowSet, layoutHook, logHook, X, io, ScreenId(..), gets, windowset, xmonad)
    import Graphics.X11.ExtraTypes.XF86 (xF86XK_MonBrightnessUp, xF86XK_MonBrightnessDown)
    import XMonad.Layout.NoBorders (smartBorders)
    import XMonad.Hooks.ManageDocks (docks, avoidStruts)
    import XMonad.Hooks.DynamicLog (def)
    import XMonad.Util.Run (spawnPipe, hPutStrLn)
    import XMonad.Layout.IndependentScreens (countScreens)
    import XMonad.StackSet (current, screen, visible, Screen, workspace, tag)
    
    spawnXMobar :: MonadIO m => Int -> m (Int, Handle)
    spawnXMobar i = (spawnPipe $ "xmobar" ++ " -x " ++ show i) >>= (\handle -> return (i, handle))
    
    spawnXMobars :: MonadIO m => Int -> m [(Int, Handle)]
    spawnXMobars n = mapM spawnXMobar [0..n-1]
    
    type ScreenFoo = Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail
    
    visibleScreens :: WindowSet -> [ScreenFoo]
    visibleScreens cws = ([current cws]) ++ (visible cws)
    
    joinToString :: [String] -> String
    joinToString workspaceIds = foldl (++) "" workspaceIds
    
    myLogHookForPipe :: WindowSet -> (Int, Handle) -> X ()
    myLogHookForPipe currentWindowSet (i, xmobarPipe) =
        io $ hPutStrLn xmobarPipe $ -- write to pipe and lift to XMonad
        joinToString $ map (tag . workspace) $  -- extract workspace names and join into a single string
        filter ((==) (S i) . screen) $  -- filter to this xmobar's screen only
        visibleScreens currentWindowSet  -- get list of all visible screens
    
    myLogHook :: [(Int, Handle)] -> X ()
    myLogHook xmobarPipes = do
        currentWindowSet <- gets windowset
        mapM_ (myLogHookForPipe currentWindowSet) xmobarPipes
    
    main = do
        n <- countScreens
        xmobarPipes <- spawnXMobars n
    
        xmonad $ docks def
            { layoutHook = avoidStruts $ smartBorders $ layoutHook def
            , logHook = myLogHook xmobarPipes
            }