2020-07-06 21:08:32 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Module for monitoring removable drive events
|
|
|
|
--
|
|
|
|
-- Currently, its only purpose is to play Super Mario sounds when a drive is
|
|
|
|
-- inserted or removed. Why? Because I can.
|
|
|
|
|
2021-11-27 00:08:24 -05:00
|
|
|
module XMonad.Internal.DBus.Removable (runRemovableMon) where
|
2020-07-06 21:08:32 -04:00
|
|
|
|
|
|
|
import Control.Monad
|
|
|
|
|
2021-11-20 01:15:04 -05:00
|
|
|
import Data.Map.Lazy (Map, member)
|
2020-07-06 21:08:32 -04:00
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
|
2022-03-05 18:18:16 -05:00
|
|
|
import XMonad.Core (io)
|
2021-11-20 01:15:04 -05:00
|
|
|
import XMonad.Internal.Command.Desktop
|
2021-11-07 13:35:08 -05:00
|
|
|
import XMonad.Internal.Dependency
|
2020-07-06 21:08:32 -04:00
|
|
|
|
2021-06-22 00:14:21 -04:00
|
|
|
bus :: BusName
|
|
|
|
bus = busName_ "org.freedesktop.UDisks2"
|
|
|
|
|
2020-07-06 21:08:32 -04:00
|
|
|
path :: ObjectPath
|
2021-06-22 00:14:21 -04:00
|
|
|
path = objectPath_ "/org/freedesktop/UDisks2"
|
2020-07-06 21:08:32 -04:00
|
|
|
|
|
|
|
interface :: InterfaceName
|
2021-06-22 00:14:21 -04:00
|
|
|
interface = interfaceName_ "org.freedesktop.DBus.ObjectManager"
|
2020-07-06 21:08:32 -04:00
|
|
|
|
|
|
|
memAdded :: MemberName
|
2021-06-22 00:14:21 -04:00
|
|
|
memAdded = memberName_ "InterfacesAdded"
|
2020-07-06 21:08:32 -04:00
|
|
|
|
|
|
|
memRemoved :: MemberName
|
2021-06-22 00:14:21 -04:00
|
|
|
memRemoved = memberName_ "InterfacesRemoved"
|
2020-07-06 21:08:32 -04:00
|
|
|
|
2022-06-17 00:37:12 -04:00
|
|
|
dbusDep :: MemberName -> FullDep DBusDep
|
|
|
|
dbusDep m = fullDep $ Endpoint bus path interface $ Signal_ m
|
2021-11-20 11:48:05 -05:00
|
|
|
|
2022-06-17 00:37:12 -04:00
|
|
|
addedDep :: FullDep DBusDep
|
2021-11-07 20:16:53 -05:00
|
|
|
addedDep = dbusDep memAdded
|
|
|
|
|
2022-06-17 00:37:12 -04:00
|
|
|
removedDep :: FullDep DBusDep
|
2021-11-07 20:16:53 -05:00
|
|
|
removedDep = dbusDep memRemoved
|
|
|
|
|
2020-07-06 21:08:32 -04:00
|
|
|
driveInsertedSound :: FilePath
|
|
|
|
driveInsertedSound = "smb_powerup.wav"
|
|
|
|
|
|
|
|
driveRemovedSound :: FilePath
|
|
|
|
driveRemovedSound = "smb_pipe.wav"
|
|
|
|
|
|
|
|
ruleUdisks :: MatchRule
|
|
|
|
ruleUdisks = matchAny
|
|
|
|
{ matchPath = Just path
|
|
|
|
, matchInterface = Just interface
|
|
|
|
}
|
|
|
|
|
|
|
|
driveFlag :: String
|
|
|
|
driveFlag = "org.freedesktop.UDisks2.Drive"
|
|
|
|
|
|
|
|
addedHasDrive :: [Variant] -> Bool
|
|
|
|
addedHasDrive [_, a] = maybe False (member driveFlag)
|
|
|
|
(fromVariant a :: Maybe (Map String (Map String Variant)))
|
|
|
|
addedHasDrive _ = False
|
|
|
|
|
|
|
|
removedHasDrive :: [Variant] -> Bool
|
|
|
|
removedHasDrive [_, a] = maybe False (driveFlag `elem`)
|
|
|
|
(fromVariant a :: Maybe [String])
|
|
|
|
removedHasDrive _ = False
|
|
|
|
|
|
|
|
playSoundMaybe :: FilePath -> Bool -> IO ()
|
2022-03-05 18:18:16 -05:00
|
|
|
playSoundMaybe p b = when b $ io $ playSound p
|
2020-07-06 21:08:32 -04:00
|
|
|
|
2020-07-10 18:48:20 -04:00
|
|
|
-- NOTE: the udisks2 service should be already running for this module to work.
|
|
|
|
-- If it not already, we won't see any signals from the dbus until it is
|
|
|
|
-- started (it will work after it is started however). It seems safe to simply
|
|
|
|
-- enable the udisks2 service at boot; however this is not default behavior.
|
2021-11-25 00:12:00 -05:00
|
|
|
listenDevices :: Client -> IO ()
|
|
|
|
listenDevices client = do
|
2021-11-27 00:08:24 -05:00
|
|
|
addMatch' memAdded driveInsertedSound addedHasDrive
|
|
|
|
addMatch' memRemoved driveRemovedSound removedHasDrive
|
2020-07-06 21:08:32 -04:00
|
|
|
where
|
2021-11-27 00:08:24 -05:00
|
|
|
addMatch' m p f = void $ addMatch client ruleUdisks { matchMember = Just m }
|
2020-07-06 21:08:32 -04:00
|
|
|
$ playSoundMaybe p . f . signalBody
|
2021-06-22 00:14:21 -04:00
|
|
|
|
2021-11-21 22:47:43 -05:00
|
|
|
runRemovableMon :: Maybe Client -> FeatureIO
|
2022-06-16 18:50:24 -04:00
|
|
|
runRemovableMon client = feature "removeable device monitor" Default
|
2022-06-17 00:37:12 -04:00
|
|
|
$ DBusTree (Single listenDevices) client $ And (Only addedDep) (Only removedDep)
|