diff --git a/lib/Xmobar/Plugins/Bluetooth.hs b/lib/Xmobar/Plugins/Bluetooth.hs index 0436b61..805713b 100644 --- a/lib/Xmobar/Plugins/Bluetooth.hs +++ b/lib/Xmobar/Plugins/Bluetooth.hs @@ -157,6 +157,11 @@ readState = do c <- readDevices return (p, anyDevicesConnected c) +modifyState :: (BtState -> BTIO (BtState, a)) -> BTIO a +modifyState f = do + m <- asks btState + modifyMVar m f + -------------------------------------------------------------------------------- -- Object manager @@ -281,9 +286,7 @@ callGetPowered adapter = T.unpack adaptorPowered putPowered :: Maybe Bool -> BTIO () -putPowered ds = do - m <- asks btState - modifyMVar_ m (\s -> return s {btPowered = ds}) +putPowered ds = modifyState $ \s -> return (s {btPowered = ds}, ()) readPowered :: BTIO (Maybe Bool) readPowered = fmap btPowered $ readMVar =<< asks btState @@ -334,28 +337,22 @@ callGetConnected p = memberName_ (T.unpack devConnected) insertDevice :: ObjectPath -> BTDevice -> BTIO Bool -insertDevice device dev = do - m <- asks btState - modifyMVar m $ \s -> do - let new = M.insert device dev $ btDevices s - return (s {btDevices = new}, anyDevicesConnected new) +insertDevice device dev = modifyState $ \s -> do + let new = M.insert device dev $ btDevices s + return (s {btDevices = new}, anyDevicesConnected new) updateDevice :: ObjectPath -> Maybe Bool -> BTIO Bool -updateDevice device status = do - m <- asks btState - modifyMVar m $ \s -> do - let new = M.update (\d -> Just d {btDevConnected = status}) device $ btDevices s - return (s {btDevices = new}, anyDevicesConnected new) +updateDevice device status = modifyState $ \s -> do + let new = M.update (\d -> Just d {btDevConnected = status}) device $ btDevices s + return (s {btDevices = new}, anyDevicesConnected new) anyDevicesConnected :: ConnectedDevices -> Bool anyDevicesConnected = or . mapMaybe btDevConnected . M.elems removeDevice :: ObjectPath -> BTIO (Maybe BTDevice) -removeDevice device = do - m <- asks btState - modifyMVar m $ \s -> do - let devs = btDevices s - return (s {btDevices = M.delete device devs}, M.lookup device devs) +removeDevice device = modifyState $ \s -> do + let devs = btDevices s + return (s {btDevices = M.delete device devs}, M.lookup device devs) readDevices :: BTIO ConnectedDevices readDevices = fmap btDevices $ readMVar =<< asks btState