summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/SwaybarClicks.hs
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2022-02-05 02:49:22 +0000
committerjao <jao@gnu.org>2022-02-06 00:24:10 +0000
commitafc2d1ed565910b372f65eaf77ea90878ac3ab2b (patch)
tree4b2b79d683b82cc0e16460884c8bfcc6c27b33cb /src/Xmobar/Text/SwaybarClicks.hs
parentc0a9c6df0080a0db5981047f80c9c120ac1cef61 (diff)
downloadxmobar-afc2d1ed565910b372f65eaf77ea90878ac3ab2b.tar.gz
xmobar-afc2d1ed565910b372f65eaf77ea90878ac3ab2b.tar.bz2
swaybar-protocol: support for clickable Action
Diffstat (limited to 'src/Xmobar/Text/SwaybarClicks.hs')
-rw-r--r--src/Xmobar/Text/SwaybarClicks.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Xmobar/Text/SwaybarClicks.hs b/src/Xmobar/Text/SwaybarClicks.hs
new file mode 100644
index 0000000..c829e65
--- /dev/null
+++ b/src/Xmobar/Text/SwaybarClicks.hs
@@ -0,0 +1,60 @@
+{-# LANGUAGE DeriveGeneric #-}
+
+------------------------------------------------------------------------------
+-- |
+-- Module: Xmobar.Text.SwaybarClicks
+-- Copyright: (c) 2022 Jose Antonio Ortega Ruiz
+-- License: BSD3-style (see LICENSE)
+--
+-- Maintainer: jao@gnu.org
+-- Stability: unstable
+-- Portability: portable
+-- Created: Fri Feb 4, 2022 03:58
+--
+--
+-- Handling of "click" events sent by swaybar via stdin
+--
+------------------------------------------------------------------------------
+
+
+module Xmobar.Text.SwaybarClicks (startHandler) where
+
+import Control.Monad (when)
+
+
+import Data.Aeson
+
+-- import qualified Data.ByteString.Lazy as BL
+
+import GHC.Generics
+
+import Xmobar.System.Utils (forkThread)
+import Xmobar.Run.Actions (Action (..), runAction')
+
+import Data.ByteString.Lazy.UTF8 (fromString)
+
+data Click =
+ Click { name :: String , button :: Int } deriving (Eq,Show,Generic)
+
+instance FromJSON Click
+
+runClickAction :: Int -> Action -> IO ()
+runClickAction b a@(Spawn bs _) =
+ when (fromIntegral b `elem` bs) (runAction' a)
+
+handleClick :: Maybe Click -> IO ()
+handleClick Nothing = return ()
+handleClick (Just click) = do
+ let mas = read (name click) :: Maybe [Action]
+ b = button click
+ maybe (return ()) (mapM_ (runClickAction b)) mas
+
+toClick :: String -> Maybe Click
+toClick (',':s) = toClick s
+toClick s = decode (fromString s)
+
+readClicks :: IO ()
+readClicks = getLine >>= handleClick . toClick >> readClicks
+
+startHandler :: IO ()
+startHandler = forkThread "Swaybar event handler" readClicks