summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/SwaybarClicks.hs
blob: c829e65d2120e6efe96958745be9fdf82cfe638c (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
{-# 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