| 1 | {-# LANGUAGE OverloadedStrings #-} |
| 2 | |
| 3 | -- Copyright (C) 2009-2011 John Millikin <[email protected]> |
| 4 | -- |
| 5 | -- Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | -- you may not use this file except in compliance with the License. |
| 7 | -- You may obtain a copy of the License at |
| 8 | -- |
| 9 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | -- |
| 11 | -- Unless required by applicable law or agreed to in writing, software |
| 12 | -- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | -- See the License for the specific language governing permissions and |
| 15 | -- limitations under the License. |
| 16 | |
| 17 | module Main (main) where |
| 18 | |
| 19 | import Control.Monad |
| 20 | import Data.List (intercalate) |
| 21 | import Data.Int |
| 22 | import Data.Word |
| 23 | import System.Environment |
| 24 | import System.Exit |
| 25 | import System.IO |
| 26 | import System.Console.GetOpt |
| 27 | |
| 28 | import DBus |
| 29 | import DBus.Socket |
| 30 | |
| 31 | data Bus = Session | System |
| 32 | deriving (Show) |
| 33 | |
| 34 | data Option = BusOption Bus | AddressOption String |
| 35 | deriving (Show) |
| 36 | |
| 37 | optionInfo :: [OptDescr Option] |
| 38 | optionInfo = [ |
| 39 | Option [] ["session"] (NoArg (BusOption Session)) |
| 40 | "Monitor the session message bus. (default)" |
| 41 | , Option [] ["system"] (NoArg (BusOption System)) |
| 42 | "Monitor the system message bus." |
| 43 | , Option [] ["address"] (ReqArg AddressOption "ADDRESS") |
| 44 | "Connect to a particular bus address." |
| 45 | ] |
| 46 | |
| 47 | usage :: String -> String |
| 48 | usage name = "Usage: " ++ name ++ " [OPTION...]" |
| 49 | |
| 50 | findSocket :: [Option] -> IO Socket |
| 51 | findSocket opts = getAddress opts >>= open where |
| 52 | session = do |
| 53 | got <- getSessionAddress |
| 54 | case got of |
| 55 | Just addr -> return addr |
| 56 | Nothing -> error "DBUS_SESSION_BUS_ADDRESS is not a valid address" |
| 57 | |
| 58 | system = do |
| 59 | got <- getSystemAddress |
| 60 | case got of |
| 61 | Just addr -> return addr |
| 62 | Nothing -> error "DBUS_SYSTEM_BUS_ADDRESS is not a valid address" |
| 63 | |
| 64 | getAddress [] = session |
| 65 | getAddress ((BusOption Session):_) = session |
| 66 | getAddress ((BusOption System):_) = system |
| 67 | getAddress ((AddressOption addr):_) = case parseAddress addr of |
| 68 | Nothing -> error (show addr ++ " is not a valid address") |
| 69 | Just parsed -> return parsed |
| 70 | |
| 71 | addMatch :: Socket -> String -> IO () |
| 72 | addMatch sock match = send sock (methodCall "/org/freedesktop/DBus" "org.freedesktop.DBus" "AddMatch") |
| 73 | { methodCallDestination = Just "org.freedesktop.DBus" |
| 74 | , methodCallBody = [toVariant match] |
| 75 | } (\_ -> return ()) |
| 76 | |
| 77 | defaultFilters :: [String] |
| 78 | defaultFilters = |
| 79 | [ "type='signal',eavesdrop=true" |
| 80 | , "type='method_call',eavesdrop=true" |
| 81 | , "type='method_return',eavesdrop=true" |
| 82 | , "type='error',eavesdrop=true" |
| 83 | ] |
| 84 | |
| 85 | main :: IO () |
| 86 | main = do |
| 87 | args <- getArgs |
| 88 | let (options, userFilters, errors) = getOpt Permute optionInfo args |
| 89 | unless (null errors) $ do |
| 90 | name <- getProgName |
| 91 | hPutStrLn stderr (concat errors) |
| 92 | hPutStrLn stderr (usageInfo (usage name) optionInfo) |
| 93 | exitFailure |
| 94 | |
| 95 | sock <- findSocket options |
| 96 | |
| 97 | send sock (methodCall "/org/freedesktop/DBus" "org.freedesktop.DBus" "Hello") |
| 98 | { methodCallDestination = Just "org.freedesktop.DBus" |
| 99 | } (\_ -> return ()) |
| 100 | |
| 101 | mapM_ (addMatch sock) (if null userFilters then defaultFilters else userFilters) |
| 102 | |
| 103 | forever $ do |
| 104 | received <- receive sock |
| 105 | putStrLn (formatMessage received ++ "\n") |
| 106 | |
| 107 | -- Message formatting is verbose and mostly uninteresting, except as an |
| 108 | -- excersise in string manipulation. |
| 109 | |
| 110 | formatMessage :: ReceivedMessage -> String |
| 111 | |
| 112 | -- Method call |
| 113 | formatMessage (ReceivedMethodCall serial msg) = concat |
| 114 | [ "method call" |
| 115 | , " sender=" |
| 116 | , maybe "(null)" formatBusName (methodCallSender msg) |
| 117 | , " -> dest=" |
| 118 | , maybe "(null)" formatBusName (methodCallDestination msg) |
| 119 | , " serial=" |
| 120 | , show (serialValue serial) |
| 121 | , " path=" |
| 122 | , formatObjectPath (methodCallPath msg) |
| 123 | , "; interface=" |
| 124 | , maybe "(null)" formatInterfaceName (methodCallInterface msg) |
| 125 | , "; member=" |
| 126 | , formatMemberName (methodCallMember msg) |
| 127 | , formatBody (methodCallBody msg) |
| 128 | ] |
| 129 | |
| 130 | -- Method return |
| 131 | formatMessage (ReceivedMethodReturn _ msg) = concat |
| 132 | [ "method return" |
| 133 | , " sender=" |
| 134 | , maybe "(null)" formatBusName (methodReturnSender msg) |
| 135 | , " -> dest=" |
| 136 | , maybe "(null)" formatBusName (methodReturnDestination msg) |
| 137 | , " reply_serial=" |
| 138 | , show (serialValue (methodReturnSerial msg)) |
| 139 | , formatBody (methodReturnBody msg) |
| 140 | ] |
| 141 | |
| 142 | -- Method error |
| 143 | formatMessage (ReceivedMethodError _ msg) = concat |
| 144 | [ "error" |
| 145 | , " sender=" |
| 146 | , maybe "(null)" formatBusName (methodErrorSender msg) |
| 147 | , " -> dest=" |
| 148 | , maybe "(null)" formatBusName (methodErrorDestination msg) |
| 149 | , " error_name=" |
| 150 | , formatErrorName (methodErrorName msg) |
| 151 | , " reply_serial=" |
| 152 | , show (serialValue (methodErrorSerial msg)) |
| 153 | , formatBody (methodErrorBody msg) |
| 154 | ] |
| 155 | |
| 156 | -- Signal |
| 157 | formatMessage (ReceivedSignal serial msg) = concat |
| 158 | [ "signal" |
| 159 | , " sender=" |
| 160 | , maybe "(null)" formatBusName (signalSender msg) |
| 161 | , " -> dest=" |
| 162 | , maybe "(null)" formatBusName (signalDestination msg) |
| 163 | , " serial=" |
| 164 | , show (serialValue serial) |
| 165 | , " path=" |
| 166 | , formatObjectPath (signalPath msg) |
| 167 | , "; interface=" |
| 168 | , formatInterfaceName (signalInterface msg) |
| 169 | , "; member=" |
| 170 | , formatMemberName (signalMember msg) |
| 171 | , formatBody (signalBody msg) |
| 172 | ] |
| 173 | |
| 174 | formatMessage msg = concat |
| 175 | [ "unknown" |
| 176 | , " sender=" |
| 177 | , maybe "(null)" formatBusName (receivedMessageSender msg) |
| 178 | , " serial=" |
| 179 | , show (serialValue (receivedMessageSerial msg)) |
| 180 | , formatBody (receivedMessageBody msg) |
| 181 | ] |
| 182 | |
| 183 | formatBody :: [Variant] -> String |
| 184 | formatBody body = formatted where |
| 185 | tree = Children (map formatVariant body) |
| 186 | formatted = intercalate "\n" ("" : collapseTree 0 tree) |
| 187 | |
| 188 | -- A string tree allows easy indentation of nested structures |
| 189 | data StringTree = Line String | MultiLine [StringTree] | Children [StringTree] |
| 190 | deriving (Show) |
| 191 | |
| 192 | collapseTree :: Int -> StringTree -> [String] |
| 193 | collapseTree d (Line x) = [replicate (d*3) ' ' ++ x] |
| 194 | collapseTree d (MultiLine xs) = concatMap (collapseTree d) xs |
| 195 | collapseTree d (Children xs) = concatMap (collapseTree (d + 1)) xs |
| 196 | |
| 197 | -- Formatting for various kinds of variants, keyed to their signature type. |
| 198 | formatVariant :: Variant -> StringTree |
| 199 | formatVariant x = case variantType x of |
| 200 | |
| 201 | TypeBoolean -> Line $ let |
| 202 | Just x' = fromVariant x |
| 203 | in "boolean " ++ if x' then "true" else "false" |
| 204 | |
| 205 | TypeWord8 -> Line $ let |
| 206 | Just x' = fromVariant x |
| 207 | in "byte " ++ show (x' :: Word8) |
| 208 | |
| 209 | TypeWord16 -> Line $ let |
| 210 | Just x' = fromVariant x |
| 211 | in "uint16 " ++ show (x' :: Word16) |
| 212 | |
| 213 | TypeWord32 -> Line $ let |
| 214 | Just x' = fromVariant x |
| 215 | in "uint32 " ++ show (x' :: Word32) |
| 216 | |
| 217 | TypeWord64 -> Line $ let |
| 218 | Just x' = fromVariant x |
| 219 | in "uint64 " ++ show (x' :: Word64) |
| 220 | |
| 221 | TypeInt16 -> Line $ let |
| 222 | Just x' = fromVariant x |
| 223 | in "int16 " ++ show (x' :: Int16) |
| 224 | |
| 225 | TypeInt32 -> Line $ let |
| 226 | Just x' = fromVariant x |
| 227 | in "int32 " ++ show (x' :: Int32) |
| 228 | |
| 229 | TypeInt64 -> Line $ let |
| 230 | Just x' = fromVariant x |
| 231 | in "int64 " ++ show (x' :: Int64) |
| 232 | |
| 233 | TypeDouble -> Line $ let |
| 234 | Just x' = fromVariant x |
| 235 | in "double " ++ show (x' :: Double) |
| 236 | |
| 237 | TypeString -> Line $ let |
| 238 | Just x' = fromVariant x |
| 239 | in "string " ++ show (x' :: String) |
| 240 | |
| 241 | TypeObjectPath -> Line $ let |
| 242 | Just x' = fromVariant x |
| 243 | in "object path " ++ show (formatObjectPath x') |
| 244 | |
| 245 | TypeSignature -> Line $ let |
| 246 | Just x' = fromVariant x |
| 247 | in "signature " ++ show (formatSignature x') |
| 248 | |
| 249 | TypeArray _ -> MultiLine $ let |
| 250 | Just x' = fromVariant x |
| 251 | items = arrayItems x' |
| 252 | lines' = [ Line "array [" |
| 253 | , Children (map formatVariant items) |
| 254 | , Line "]" |
| 255 | ] |
| 256 | in lines' |
| 257 | |
| 258 | TypeDictionary _ _ -> MultiLine $ let |
| 259 | Just x' = fromVariant x |
| 260 | items = dictionaryItems x' |
| 261 | lines' = [ Line "dictionary {" |
| 262 | , Children (map formatItem items) |
| 263 | , Line "}" |
| 264 | ] |
| 265 | formatItem (k, v) = MultiLine (firstLine : vTail) where |
| 266 | Line k' = formatVariant k |
| 267 | v' = collapseTree 0 (formatVariant v) |
| 268 | vHead = head v' |
| 269 | vTail = map Line (tail v') |
| 270 | firstLine = Line (k' ++ " -> " ++ vHead) |
| 271 | in lines' |
| 272 | |
| 273 | TypeStructure _ -> MultiLine $ let |
| 274 | Just x' = fromVariant x |
| 275 | items = structureItems x' |
| 276 | lines' = [ Line "struct (" |
| 277 | , Children (map formatVariant items) |
| 278 | , Line ")" |
| 279 | ] |
| 280 | in lines' |
| 281 | |
| 282 | TypeVariant -> let |
| 283 | Just x' = fromVariant x |
| 284 | in MultiLine [Line "variant", Children [formatVariant x']] |