summaryrefslogtreecommitdiff
path: root/lib/xmonad.hs
blob: 047dd83d849a49a2327965196c4dca5663dd6c2d (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{-# LANGUAGE LambdaCase #-}

{-

Docs:

- EZConfig: https://hackage.haskell.org/package/xmonad-contrib-0.13/docs/XMonad-Util-EZConfig.html#g:3
- Media keys: https://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-ExtraTypes-XF86.html
- Audio control: https://xmonadhaskell.wordpress.com/2018/10/24/xmonad-audio-control/

- XMonad API: https://hackage.haskell.org/package/xmonad
- Contrib API: https://hackage.haskell.org/package/xmonad-contrib

Inspirations:

- https://reddit.simatime.com/wy695w

-}

import Data.Functor ((<&>))
import Data.List (intercalate, isPrefixOf)
import Graphics.X11.ExtraTypes.XF86
import XMonad
import XMonad.Actions.CopyWindow
import XMonad.Config
import XMonad.Config.Desktop
import XMonad.Hooks.EwmhDesktops (ewmh)
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers (doFullFloat, isFullscreen)
import XMonad.Layout.BinarySpacePartition
import XMonad.Layout.Dwindle as Dwindle
import XMonad.Layout.LayoutModifier
import XMonad.Layout.NoBorders
import XMonad.Layout.ResizableTile
import XMonad.Layout.Spacing
import XMonad.Layout.Spiral
import XMonad.Layout.Tabbed
import XMonad.Layout.ThreeColumns
import XMonad.Layout.TwoPane
import qualified XMonad.StackSet as W
import XMonad.Util.CustomKeys (customKeys)
import XMonad.Util.EZConfig (additionalKeys)
import XMonad.Util.Scratchpad
import XMonad.Util.NamedScratchpad

-- Colors
data Colors = Colors
  { foreground :: String,
    background :: String,
    highlight :: String
  }

lightTheme =
  Colors
    { highlight = "#67b11d",
      background = "#f6f1e1",
      foreground = "#655370"
    }

darkTheme =
  Colors
    { highlight = "#5d4d7a",
      background = "#292b2e",
      foreground = "#b2b2b2"
    }

getColorsFromXtheme :: IO Colors
getColorsFromXtheme =
  readFile "/home/ben/.local/share/xtheme"
    <&> filter (/= '\n')
    >>= \case
      "light" -> return lightTheme
      "dark" -> return darkTheme
      _ -> return darkTheme

altMask :: KeyMask
altMask = mod1Mask

brighter = "brightnessctl s 5%+"

dimmer = "brightnessctl s 5%-"

shouldntFloat :: String -> Bool
shouldntFloat = isPrefixOf "qutebrowser"

shouldFloat :: Query Bool
shouldFloat = do
  fs <- isFullscreen
  name <- appName
  return (fs && not (shouldntFloat name))

-- RationalRect is x y w h
scratchHook = scratchpadManageHook (W.RationalRect 0.125 0.125 0.75 0.75)

myManageHook = manageDocks <+> scratchHook <+> (shouldFloat --> doFullFloat) <+> manageHook def

-- todo: use urxvtc with daemon
termName = "/home/ben/.nix-profile/bin/urxvt"
transparentTerm = termName <> " -depth 32 -bg rgba:0000/0000/0000/4444"

scratchpads = [
    NS "pavucontrol" "pavucontrol" (className =? "Pavucontrol")
      (customFloating $ W.RationalRect (1/4) (1/4) (2/4) (2/4))
  ]

insKeys :: XConfig l -> [((KeyMask, KeySym), X ())]
insKeys conf@(XConfig {modMask = modMask}) =
  [ ((modMask, xK_y), spawn "passmenu"),
    ((modMask, xK_m), spawn "cmdtree"),
    ((modMask, xK_o), spawn "rofi -sidebar-mode -show run"),
    ((modMask, xK_n), spawn "rofi -sidebar-mode -show window"),
    ((modMask, xK_u), spawn "rofi -show calc -modi calc -no-show-match -no-sort"),
    ((modMask, xK_s), scratchpadSpawnActionTerminal transparentTerm),
    ((modMask .|. shiftMask, xK_p), namedScratchpadAction scratchpads "pavucontrol"),
    -- ((modMask, xK_h), spawn transparentTerm),
    -- refresh display via autorandr
    ( (modMask, xK_r),
      spawn $ "autorandr --cycle"
    ),
    -- lock it up
    ((modMask .|. shiftMask, xK_l), spawn "xautolock -locknow"),
    -- sticky windows
    ((modMask, xK_a), windows copyToAll), -- @@ Make focused window always visible
    ((modMask .|. shiftMask, xK_a), killAllOtherCopies), -- @@ Toggle window state back

    -- media/ function keys
    -- backlight
    ((0, xF86XK_MonBrightnessDown), spawn dimmer),
    ((0, xF86XK_MonBrightnessUp), spawn brighter),
    -- volume controls
    ((0, xF86XK_AudioMute), amixer "toggle"),
    ((0, xF86XK_AudioLowerVolume), amixer "2%-"),
    ((0, xF86XK_AudioRaiseVolume), amixer "2%+")
  ]

amixer :: String -> X ()
amixer cmd = spawn $ "amixer -q sset Master " <> cmd

-- | Golden-ratio spiral
goldenSpiral :: SpiralWithDir a
goldenSpiral = spiral (6 / 7)

myWorkspaces :: [String]
myWorkspaces = ["1[org]", "2[dev]", "3[chat]", "4[lithium]", "5[mon]", "6", "7", "8", "9"]

addSpace :: l a -> ModifiedLayout Spacing l a
addSpace =
  spacingRaw
    False
    (Border 5 5 5 5)
    True
    (Border 5 5 5 5)
    True

myTabCfg theme =
  def
    { fontName = "xft:Fira Sans:size=10:ant",
      activeBorderColor = highlight theme,
      inactiveBorderColor = background theme,
      activeColor = highlight theme,
      inactiveColor = background theme
    }

myLayout theme =
  avoidStruts $
    noBorders (tabbed shrinkText $ myTabCfg theme)
      ||| (addSpace $ noBorders Full)
      ||| twopane
      ||| Mirror twopane
      ||| ThreeCol 1 (3 / 100) (1 / 2)
      ||| ThreeColMid 1 (3 / 100) (1 / 2)
      ||| tiled
      ||| Mirror tiled
      ||| emptyBSP
  where
    --      ||| goldenSpiral
    --      ||| Spiral R Dwindle.CW (3 / 2) (11 / 10) -- L/R is where to put non-main windows

    -- The last parameter is fraction to multiply the slave window heights
    -- with. Useless here.
    tiled = addSpace $ ResizableTall nmaster delta ratio []
    -- The default number of windows in the master pane
    nmaster = 1
    -- In this layout the second pane will only show the focused window.
    twopane = addSpace $ TwoPane delta ratio
    -- Default proportion of screen occupied by master pane
    ratio = 1 / 2
    -- Percent of screen to increment by when resizing panes
    delta = 3 / 100


myConf theme = additionalKeys cfg (insKeys cfg)
  where
    cfg =
      desktopConfig
        { modMask = mod4Mask, -- super instead of alt
          normalBorderColor = background theme,
          focusedBorderColor = highlight theme,
          borderWidth = 3,
          manageHook = myManageHook,
          layoutHook = myLayout theme,
          terminal = termName,
          workspaces = myWorkspaces
        }

main :: IO ()
main = do
  theme <- getColorsFromXtheme
  xmonad $ ewmh $ docks $ myConf theme