diff options
author | Jochen Keil <jochen.keil@gmail.com> | 2012-08-10 11:35:35 +0200 |
---|---|---|
committer | Jochen Keil <jochen.keil@gmail.com> | 2012-08-10 11:35:35 +0200 |
commit | a7158c426ae008fb268f603f75d027683f726757 (patch) | |
tree | a7670c6ede358b8501c0a87c064346ec1ca39ed5 /src | |
parent | f51060e267f4f79ed18e4a4ffe93dad2cbeb85f0 (diff) | |
download | xmobar-a7158c426ae008fb268f603f75d027683f726757.tar.gz xmobar-a7158c426ae008fb268f603f75d027683f726757.tar.bz2 |
Add an initial (working) version of the DBus IPC module
Diffstat (limited to 'src')
-rw-r--r-- | src/IPC/DBus.hs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/IPC/DBus.hs b/src/IPC/DBus.hs new file mode 100644 index 0000000..64e3cca --- /dev/null +++ b/src/IPC/DBus.hs @@ -0,0 +1,66 @@ +----------------------------------------------------------------------------- +-- | +-- Module : DBus +-- Copyright : (c) Jochen Keil +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Jochen Keil <jochen dot keil at gmail dot com> +-- Stability : unstable +-- Portability : unportable +-- +-- DBus IPC module for Xmobar +-- +----------------------------------------------------------------------------- + +module IPC.DBus ( runIPC ) where + +import DBus +import DBus.Client +import Control.Monad ((>=>), join, when) +import Control.Concurrent + +import Signal + +safeHead :: [a] -> Maybe a +safeHead [] = Nothing +safeHead (x:_) = Just x + +instance IsVariant SignalType where + toVariant = toVariant . show + fromVariant = fromVariant >=> parseSignalType + +parseSignalType :: String -> Maybe SignalType +parseSignalType = fmap fst . safeHead . reads + +busName :: BusName +busName = busName_ "org.Xmobar.Control" + +objectPath :: ObjectPath +objectPath = objectPath_ "/org/Xmobar/Control" + +interfaceName :: InterfaceName +interfaceName = interfaceName_ "org.Xmobar.Control" + +runIPC :: MVar SignalType -> IO () +runIPC mvst = do + client <- connectSession + requestName client busName [ nameDoNotQueue ] + export client objectPath [ sendSignalMethod mvst ] + +sendSignalMethod :: MVar SignalType -> Method +sendSignalMethod mvst = method interfaceName sendSignalName + (signature_ [variantType $ toVariant $ (undefined :: SignalType)]) + (signature_ []) + sendSignalMethodCall + where + sendSignalName :: MemberName + sendSignalName = memberName_ "SendSignal" + + sendSignalMethodCall :: MethodCall -> IO Reply + sendSignalMethodCall mc = do + when ( methodCallMember mc == sendSignalName ) $ sendSignal $ + join $ safeHead $ map fromVariant $ methodCallBody mc + return ( replyReturn [] ) + + sendSignal :: Maybe SignalType -> IO () + sendSignal = maybe (return ()) (putMVar mvst) |