初速1PV

プログラミングにまつわることの記録

xmonad+xmobarでワークスペースをクリックで移動できるようにする

shirai3.hatenablog.com

Gentooをインストールのしたあとの続きです。

タッチスクリーン + タッチペンでの操作を楽にするために画面をタップするだけでワークスペースを移動できるように設定します。

xmonad

xmonad.org

xmonadはXのタイル型ウインドウマネージャでHaskellで設定ファイルを書くことができます。 xmonadだけではステータスバーは無いためxmobarなど別のツールを使う必要があります。

やりかた

たとえば、コマンドラインxdotool key alt+2 を実行するとalt2を同時に押したことになり、デフォルトのxmonadではworkspace 2へ移動できます。
また、xmobarでは<action=command>text</action>でtextというラベルのボタンができ、このボタンをクリックするとcommandを実行できます。
xmonad.rs内でworkspaceに表示する文字列を<action=command>text</action>にし、xmobarrcでその文字をエスケープせずに表示するすることで、本来xmobarに表示するはずの文字列にクリック後の操作を付け加えることができます。

以下がその設定ファイルです。

Xmonadの設定

import XMonad.Hooks.EwmhDesktops

xmobarEscape = concatMap doubleLts
    where doubleLts '<' = "<<"
          doubleLts x   = [x]

main = do
    wsbar <- spawnPipe "xmobar $HOME/.xmonad/xmobarrc"
    xmonad $ ewmh defaultConfig
        { ...
        , workspaces =
            let clickable l = [ "<action=xdotool key alt+" ++ show (n) ++ ">" ++ ws ++ "</action>" | (i,ws) <- zip [1..5] l, let n = i ] in
            clickable . (map xmobarEscape) $ ["1","2","3","4","5"]
        }
       ...
       ]

Xmorbarの設定

以下ののコンフィグのcommandsの最後の要素は、よくRun StdinReaderになっていますが、これをRun UnsafeStdinReaderに書き換えます。 次にxmobarrcの一番下のtemplateも%StdinReader%%UnsafeStdinReader%に書き換えます。

-- -*- mode:haskell -*-
Config { font = "xft:Migu 1M:size=18"
       , bgColor = "#1a1e1b"
       , fgColor = "#676767"
       , position = Top
       , lowerOnStart = False
       , commands = [ Run Network "wlp0s20f3" [ "-t"       , " ⇩ <rx> : ⇧ <tx>"
                                          , "--normal" , "#d3d7cf"
                                          , "--high"   , "#88b986"
                                          ] 10

                    , Run MultiCpu        [ "-t"       , "<total0>.<total1>.<total2>.<total3>.<total4>.<total5>.<total6>.<total7>"
                                          , "--normal" , "#d3d7cf"
                                          , "--high"   , "#c16666"
                                          ] 50

                    , Run Memory          [ "-t"       , "<usedratio>%"
                                          , "--normal" , "#d3d7cf"
                                          , "--high"   , "#c16666"
                                          ] 10
                    , Run BatteryP        ["BAT0"]
                                          [ "-t"       , "<acstatus>"
                                          , "--low"    , "#c16666"
                                          , "--normal" , "#d3d7cf"
                                          , "--"
                                                , "-o" , "<left>% (<timeleft>)"
                                                , "-O" , "Charging <left>%"
                                                , "-i" , "<left>%"
                                          ] 50
                    , Run Date "%a %m/%d %H:%M" "date" 10
                    , Run UnsafeStdinReader
                    ]
       , sepChar = "%"
       , alignSep = "}{"
       , template = " %UnsafeStdinReader% }{≫%multicpu%  ≫%memory%  ≫%wlp0s20f3%  ≫%battery%  <fc=#c7a273>%date%</fc> "
       }

完成

f:id:shirai3:20190708093711p:plain
xmobarのworkspaceの表示
上の画像の数字をクリックするとその番号のワークスペースのへジャンプできます。

参考

arch-ed.dk