| 1 |
{-# LANGUAGE OverloadedStrings #-} |
| 2 |
module Main where |
| 3 |
|
| 4 |
import Control.Monad |
| 5 |
import DBus.Client |
| 6 |
import Data.Int |
| 7 |
import Data.Maybe |
| 8 |
import Data.Ratio |
| 9 |
import Data.Semigroup ((<>)) |
| 10 |
import qualified Data.Text as T |
| 11 |
import Data.Version (showVersion) |
| 12 |
import qualified GI.Gdk as Gdk |
| 13 |
import qualified GI.Gtk as Gtk |
| 14 |
import Graphics.UI.GIGtkStrut |
| 15 |
import Options.Applicative |
| 16 |
import qualified StatusNotifier.Host.Service as Host |
| 17 |
import StatusNotifier.TransparentWindow |
| 18 |
import StatusNotifier.Tray |
| 19 |
import System.Log.Logger |
| 20 |
import System.Posix.Process |
| 21 |
import Text.Printf |
| 22 |
|
| 23 |
import Paths_gtk_sni_tray (version) |
| 24 |
|
| 25 |
positionP :: Parser StrutPosition |
| 26 |
positionP = fromMaybe TopPos <$> optional |
| 27 |
( flag' TopPos |
| 28 |
( long "top" |
| 29 |
<> help "Position the bar at the top of the screen" |
| 30 |
) |
| 31 |
<|> flag' BottomPos |
| 32 |
( long "bottom" |
| 33 |
<> help "Position the bar at the bottom of the screen" |
| 34 |
) |
| 35 |
<|> flag' LeftPos |
| 36 |
( long "left" |
| 37 |
<> help "Position the bar on the left side of the screen" |
| 38 |
) |
| 39 |
<|> flag' RightPos |
| 40 |
( long "right" |
| 41 |
<> help "Position the bar on the right side of the screen" |
| 42 |
)) |
| 43 |
|
| 44 |
alignmentP :: Parser StrutAlignment |
| 45 |
alignmentP = fromMaybe Center <$> optional |
| 46 |
( flag' Beginning |
| 47 |
( long "beginning" |
| 48 |
<> help "Use beginning alignment" |
| 49 |
) |
| 50 |
<|> flag' Center |
| 51 |
( long "center" |
| 52 |
<> help "Use center alignment" |
| 53 |
) |
| 54 |
<|> flag' End |
| 55 |
( long "end" |
| 56 |
<> help "Use end alignment" |
| 57 |
)) |
| 58 |
|
| 59 |
sizeP :: Parser Int32 |
| 60 |
sizeP = |
| 61 |
option auto |
| 62 |
( long "size" |
| 63 |
<> short 's' |
| 64 |
<> help "Set the size of the bar" |
| 65 |
<> value 30 |
| 66 |
<> metavar "SIZE" |
| 67 |
) |
| 68 |
|
| 69 |
paddingP :: Parser Int32 |
| 70 |
paddingP = |
| 71 |
option auto |
| 72 |
( long "padding" |
| 73 |
<> short 'p' |
| 74 |
<> help "Set the padding of the bar" |
| 75 |
<> value 0 |
| 76 |
<> metavar "PADDING" |
| 77 |
) |
| 78 |
|
| 79 |
monitorNumberP :: Parser [Int32] |
| 80 |
monitorNumberP = many $ |
| 81 |
option auto |
| 82 |
( long "monitor" |
| 83 |
<> short 'm' |
| 84 |
<> help "Display a tray bar on the given monitor" |
| 85 |
<> metavar "MONITOR" |
| 86 |
) |
| 87 |
|
| 88 |
logP :: Parser Priority |
| 89 |
logP = |
| 90 |
option auto |
| 91 |
( long "log-level" |
| 92 |
<> short 'l' |
| 93 |
<> help "Set the log level" |
| 94 |
<> metavar "LEVEL" |
| 95 |
<> value WARNING |
| 96 |
) |
| 97 |
|
| 98 |
colorP :: Parser (Maybe String) |
| 99 |
colorP = optional $ |
| 100 |
strOption |
| 101 |
( long "color" |
| 102 |
<> short 'c' |
| 103 |
<> help "Set the background color of the tray; See https://developer.gnome.org/gdk3/stable/gdk3-RGBA-Colors.html#gdk-rgba-parse for acceptable values" |
| 104 |
<> metavar "COLOR" |
| 105 |
) |
| 106 |
|
| 107 |
expandP :: Parser Bool |
| 108 |
expandP = |
| 109 |
switch |
| 110 |
( long "expand" |
| 111 |
<> help "Let icons expand into the space allocated to the tray" |
| 112 |
<> short 'e' |
| 113 |
) |
| 114 |
|
| 115 |
startWatcherP :: Parser Bool |
| 116 |
startWatcherP = |
| 117 |
switch |
| 118 |
( long "watcher" |
| 119 |
<> short 'w' |
| 120 |
<> help "Start a Watcher to handle SNI registration if one does not exist" |
| 121 |
) |
| 122 |
|
| 123 |
noStrutP :: Parser Bool |
| 124 |
noStrutP = |
| 125 |
switch |
| 126 |
( long "no-strut" |
| 127 |
<> help "Do not set strut properties for the gtk window" |
| 128 |
) |
| 129 |
|
| 130 |
barLengthP :: Parser Rational |
| 131 |
barLengthP = |
| 132 |
option auto |
| 133 |
( long "length" |
| 134 |
<> help "Set the proportion of the screen that the tray bar should occupy -- values are parsed as haskell rationals (e.g. 1 % 2)" |
| 135 |
<> value 1 |
| 136 |
) |
| 137 |
|
| 138 |
overlayScaleP :: Parser Rational |
| 139 |
overlayScaleP = |
| 140 |
option auto |
| 141 |
( long "overlay-scale" |
| 142 |
<> short 'o' |
| 143 |
<> help "The proportion of the tray icon's size that should be set for overlay icons." |
| 144 |
<> value (5 % 7) |
| 145 |
) |
| 146 |
|
| 147 |
getColor colorString = do |
| 148 |
rgba <- Gdk.newZeroRGBA |
| 149 |
colorParsed <- Gdk.rGBAParse rgba (T.pack colorString) |
| 150 |
unless colorParsed $ do |
| 151 |
logM "StatusNotifier.Tray" WARNING "Failed to parse provided color" |
| 152 |
void $ Gdk.rGBAParse rgba "#000000" |
| 153 |
return rgba |
| 154 |
|
| 155 |
buildWindows :: StrutPosition |
| 156 |
-> StrutAlignment |
| 157 |
-> Int32 |
| 158 |
-> Int32 |
| 159 |
-> [Int32] |
| 160 |
-> Priority |
| 161 |
-> Maybe String |
| 162 |
-> Bool |
| 163 |
-> Bool |
| 164 |
-> Bool |
| 165 |
-> Rational |
| 166 |
-> Rational |
| 167 |
-> IO () |
| 168 |
buildWindows pos align size padding monitors priority maybeColorString expand |
| 169 |
startWatcher noStrut length overlayScale = do |
| 170 |
Gtk.init Nothing |
| 171 |
logger <- getLogger "StatusNotifier" |
| 172 |
saveGlobalLogger $ setLevel priority logger |
| 173 |
client <- connectSession |
| 174 |
logger <- getRootLogger |
| 175 |
pid <- getProcessID |
| 176 |
-- Okay to use a forced pattern here because we want to die if this fails anyway |
| 177 |
Just host <- |
| 178 |
Host.build |
| 179 |
Host.defaultParams |
| 180 |
{ Host.dbusClient = Just client |
| 181 |
, Host.uniqueIdentifier = printf "standalone-%s" $ show pid |
| 182 |
, Host.startWatcher = startWatcher |
| 183 |
} |
| 184 |
let c1 = |
| 185 |
defaultStrutConfig |
| 186 |
{ strutPosition = pos |
| 187 |
, strutAlignment = align |
| 188 |
, strutXPadding = padding |
| 189 |
, strutYPadding = padding |
| 190 |
} |
| 191 |
defaultRatio = ScreenRatio length |
| 192 |
configBase = |
| 193 |
case pos of |
| 194 |
TopPos -> c1 {strutHeight = ExactSize size, strutWidth = defaultRatio} |
| 195 |
BottomPos -> |
| 196 |
c1 {strutHeight = ExactSize size, strutWidth = defaultRatio} |
| 197 |
RightPos -> |
| 198 |
c1 {strutHeight = defaultRatio, strutWidth = ExactSize size} |
| 199 |
LeftPos -> |
| 200 |
c1 {strutHeight = defaultRatio, strutWidth = ExactSize size} |
| 201 |
buildWithConfig config = do |
| 202 |
let orientation = |
| 203 |
case strutPosition config of |
| 204 |
TopPos -> Gtk.OrientationHorizontal |
| 205 |
BottomPos -> Gtk.OrientationHorizontal |
| 206 |
_ -> Gtk.OrientationVertical |
| 207 |
tray <- |
| 208 |
buildTray host client |
| 209 |
TrayParams |
| 210 |
{ trayOrientation = orientation |
| 211 |
, trayImageSize = Expand |
| 212 |
, trayIconExpand = expand |
| 213 |
, trayAlignment = align |
| 214 |
, trayOverlayScale = overlayScale |
| 215 |
, trayLeftClickAction = Activate |
| 216 |
, trayMiddleClickAction = SecondaryActivate |
| 217 |
, trayRightClickAction = PopupMenu |
| 218 |
} |
| 219 |
window <- Gtk.windowNew Gtk.WindowTypeToplevel |
| 220 |
when (not noStrut) $ |
| 221 |
setupStrutWindow config window |
| 222 |
maybe |
| 223 |
(makeWindowTransparent window) |
| 224 |
(getColor >=> |
| 225 |
Gtk.widgetOverrideBackgroundColor window [Gtk.StateFlagsNormal] . |
| 226 |
Just) |
| 227 |
maybeColorString |
| 228 |
Gtk.containerAdd window tray |
| 229 |
Gtk.widgetShowAll window |
| 230 |
runForMonitor monitor = |
| 231 |
buildWithConfig configBase {strutMonitor = Just monitor} |
| 232 |
if null monitors |
| 233 |
then buildWithConfig configBase |
| 234 |
else mapM_ runForMonitor monitors |
| 235 |
Gtk.main |
| 236 |
|
| 237 |
parser :: Parser (IO ()) |
| 238 |
parser = |
| 239 |
buildWindows <$> positionP <*> alignmentP <*> sizeP <*> paddingP <*> |
| 240 |
monitorNumberP <*> logP <*> colorP <*> expandP <*> startWatcherP <*> |
| 241 |
noStrutP <*> barLengthP <*> overlayScaleP |
| 242 |
|
| 243 |
versionOption :: Parser (a -> a) |
| 244 |
versionOption = infoOption |
| 245 |
(printf "gtk-sni-tray-standalone %s" $ showVersion version) |
| 246 |
( long "version" |
| 247 |
<> help "Show the version number of gtk-sni-tray" |
| 248 |
) |
| 249 |
|
| 250 |
main :: IO () |
| 251 |
main = |
| 252 |
join $ execParser $ info (helper <*> versionOption <*> parser) |
| 253 |
( fullDesc |
| 254 |
<> progDesc "Run a standalone StatusNotifierItem/AppIndicator tray" |
| 255 |
) |