From 24c84e47177b6ebbe3df99db53220def6c0951ec Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 25 Nov 2018 23:43:41 +0000 Subject: Xmobar.System.Utils, Xmobar.X11.Events --- src/Xmobar/App/EventLoop.hs | 4 +- src/Xmobar/Plugins/BufferedPipeReader.hs | 2 +- src/Xmobar/Plugins/CommandReader.hs | 2 +- src/Xmobar/Plugins/DateZone.hs | 1 - src/Xmobar/Plugins/EWMH.hs | 2 +- src/Xmobar/Plugins/Kbd.hs | 2 +- src/Xmobar/Plugins/Locks.hs | 2 +- src/Xmobar/Plugins/MBox.hs | 3 +- src/Xmobar/Plugins/Mail.hs | 3 +- src/Xmobar/Plugins/MarqueePipeReader.hs | 4 +- src/Xmobar/Plugins/Monitors/Common.hs | 2 +- src/Xmobar/Plugins/PipeReader.hs | 2 +- src/Xmobar/Plugins/StdinReader.hs | 4 +- src/Xmobar/Plugins/XMonadLog.hs | 2 +- src/Xmobar/Run/Commands.hs | 14 +++++- src/Xmobar/System/Utils.hs | 54 +++++++++++++++++++++ src/Xmobar/Utils.hs | 82 -------------------------------- src/Xmobar/X11/Events.hs | 36 ++++++++++++++ 18 files changed, 120 insertions(+), 101 deletions(-) create mode 100644 src/Xmobar/System/Utils.hs delete mode 100644 src/Xmobar/Utils.hs create mode 100644 src/Xmobar/X11/Events.hs (limited to 'src') diff --git a/src/Xmobar/App/EventLoop.hs b/src/Xmobar/App/EventLoop.hs index 8da617b..0d96578 100644 --- a/src/Xmobar/App/EventLoop.hs +++ b/src/Xmobar/App/EventLoop.hs @@ -36,18 +36,18 @@ import Data.Bits import Data.Map hiding (foldr, map, filter) import Data.Maybe (fromJust, isJust) -import Xmobar.Utils import Xmobar.System.Signal import Xmobar.Config.Types -import Xmobar.X11.Actions import Xmobar.Run.Commands import Xmobar.Run.Runnable +import Xmobar.X11.Actions import Xmobar.X11.Parsers import Xmobar.X11.Window import Xmobar.X11.Text import Xmobar.X11.Draw import Xmobar.X11.Bitmap as Bitmap import Xmobar.X11.Types +import Xmobar.X11.Events(nextEvent') #ifdef XFT import Graphics.X11.Xft diff --git a/src/Xmobar/Plugins/BufferedPipeReader.hs b/src/Xmobar/Plugins/BufferedPipeReader.hs index 65ecea2..ee02355 100644 --- a/src/Xmobar/Plugins/BufferedPipeReader.hs +++ b/src/Xmobar/Plugins/BufferedPipeReader.hs @@ -20,10 +20,10 @@ import Control.Concurrent.STM import System.IO import System.IO.Unsafe(unsafePerformIO) -import Xmobar.Utils(hGetLineSafe) import Xmobar.Run.Commands import Xmobar.System.Signal import Xmobar.System.Environment +import Xmobar.System.Utils(hGetLineSafe) data BufferedPipeReader = BufferedPipeReader String [(Int, Bool, String)] deriving (Read, Show) diff --git a/src/Xmobar/Plugins/CommandReader.hs b/src/Xmobar/Plugins/CommandReader.hs index 69c8e0c..4e39d72 100644 --- a/src/Xmobar/Plugins/CommandReader.hs +++ b/src/Xmobar/Plugins/CommandReader.hs @@ -17,7 +17,7 @@ module Xmobar.Plugins.CommandReader(CommandReader(..)) where import System.IO import Xmobar.Run.Commands -import Xmobar.Utils (hGetLineSafe) +import Xmobar.System.Utils (hGetLineSafe) import System.Process(runInteractiveCommand, getProcessExitCode) data CommandReader = CommandReader String String diff --git a/src/Xmobar/Plugins/DateZone.hs b/src/Xmobar/Plugins/DateZone.hs index 7215713..c2e80da 100644 --- a/src/Xmobar/Plugins/DateZone.hs +++ b/src/Xmobar/Plugins/DateZone.hs @@ -23,7 +23,6 @@ module Xmobar.Plugins.DateZone (DateZone(..)) where import Xmobar.Run.Commands -import Xmobar.Utils(tenthSeconds) #ifdef DATEZONE import Control.Concurrent.STM diff --git a/src/Xmobar/Plugins/EWMH.hs b/src/Xmobar/Plugins/EWMH.hs index 4a443d6..5b68620 100644 --- a/src/Xmobar/Plugins/EWMH.hs +++ b/src/Xmobar/Plugins/EWMH.hs @@ -29,7 +29,7 @@ import Codec.Binary.UTF8.String as UTF8 #define UTF8 #endif import Foreign.C (CChar, CLong) -import Xmobar.Utils (nextEvent') +import Xmobar.X11.Events (nextEvent') import Data.List (intersperse, intercalate) diff --git a/src/Xmobar/Plugins/Kbd.hs b/src/Xmobar/Plugins/Kbd.hs index f4dad36..6654163 100644 --- a/src/Xmobar/Plugins/Kbd.hs +++ b/src/Xmobar/Plugins/Kbd.hs @@ -21,7 +21,7 @@ import Graphics.X11.Xlib import Graphics.X11.Xlib.Extras import Xmobar.Run.Commands -import Xmobar.Utils (nextEvent') +import Xmobar.X11.Events (nextEvent') import Xmobar.System.Kbd diff --git a/src/Xmobar/Plugins/Locks.hs b/src/Xmobar/Plugins/Locks.hs index 19bce20..25d2946 100644 --- a/src/Xmobar/Plugins/Locks.hs +++ b/src/Xmobar/Plugins/Locks.hs @@ -21,7 +21,7 @@ import Control.Monad import Graphics.X11.Xlib.Extras import Xmobar.Run.Commands import Xmobar.System.Kbd -import Xmobar.Utils (nextEvent') +import Xmobar.X11.Events (nextEvent') data Locks = Locks deriving (Read, Show) diff --git a/src/Xmobar/Plugins/MBox.hs b/src/Xmobar/Plugins/MBox.hs index 4bd0ebd..7eaa998 100644 --- a/src/Xmobar/Plugins/MBox.hs +++ b/src/Xmobar/Plugins/MBox.hs @@ -18,7 +18,8 @@ module Xmobar.Plugins.MBox (MBox(..)) where import Prelude import Xmobar.Run.Commands #ifdef INOTIFY -import Xmobar.Utils (changeLoop, expandHome) + +import Xmobar.System.Utils (changeLoop, expandHome) import Control.Monad (when) import Control.Concurrent.STM diff --git a/src/Xmobar/Plugins/Mail.hs b/src/Xmobar/Plugins/Mail.hs index d59e70d..e67817b 100644 --- a/src/Xmobar/Plugins/Mail.hs +++ b/src/Xmobar/Plugins/Mail.hs @@ -17,7 +17,8 @@ module Xmobar.Plugins.Mail(Mail(..)) where import Xmobar.Run.Commands #ifdef INOTIFY -import Xmobar.Utils (expandHome, changeLoop) + +import Xmobar.System.Utils (expandHome, changeLoop) import Control.Monad import Control.Concurrent.STM diff --git a/src/Xmobar/Plugins/MarqueePipeReader.hs b/src/Xmobar/Plugins/MarqueePipeReader.hs index a48e81c..a139aba 100644 --- a/src/Xmobar/Plugins/MarqueePipeReader.hs +++ b/src/Xmobar/Plugins/MarqueePipeReader.hs @@ -16,8 +16,8 @@ module Xmobar.Plugins.MarqueePipeReader where import System.IO (openFile, IOMode(ReadWriteMode), Handle) import Xmobar.System.Environment -import Xmobar.Utils(tenthSeconds, hGetLineSafe) -import Xmobar.Run.Commands(Exec(alias, start)) +import Xmobar.Run.Commands(Exec(alias, start), tenthSeconds) +import Xmobar.System.Utils(hGetLineSafe) import System.Posix.Files (getFileStatus, isNamedPipe) import Control.Concurrent(forkIO, threadDelay) import Control.Concurrent.STM (TChan, atomically, writeTChan, tryReadTChan, newTChan) diff --git a/src/Xmobar/Plugins/Monitors/Common.hs b/src/Xmobar/Plugins/Monitors/Common.hs index f683874..383a0f1 100644 --- a/src/Xmobar/Plugins/Monitors/Common.hs +++ b/src/Xmobar/Plugins/Monitors/Common.hs @@ -76,7 +76,7 @@ import Text.ParserCombinators.Parsec import System.Console.GetOpt import Control.Exception (SomeException,handle) -import Xmobar.Utils +import Xmobar.Run.Commands -- $monitor diff --git a/src/Xmobar/Plugins/PipeReader.hs b/src/Xmobar/Plugins/PipeReader.hs index f18b9cb..d7b6a56 100644 --- a/src/Xmobar/Plugins/PipeReader.hs +++ b/src/Xmobar/Plugins/PipeReader.hs @@ -15,8 +15,8 @@ module Xmobar.Plugins.PipeReader(PipeReader(..)) where import System.IO -import Xmobar.Utils(hGetLineSafe) import Xmobar.Run.Commands(Exec(..)) +import Xmobar.System.Utils(hGetLineSafe) import Xmobar.System.Environment(expandEnv) import System.Posix.Files import Control.Concurrent(threadDelay) diff --git a/src/Xmobar/Plugins/StdinReader.hs b/src/Xmobar/Plugins/StdinReader.hs index 1d56ca8..7f5f2ae 100644 --- a/src/Xmobar/Plugins/StdinReader.hs +++ b/src/Xmobar/Plugins/StdinReader.hs @@ -23,9 +23,9 @@ import System.Posix.Process import System.Exit import System.IO import Control.Exception (SomeException(..), handle) -import Xmobar.X11.Actions (stripActions) -import Xmobar.Utils (hGetLineSafe) import Xmobar.Run.Commands +import Xmobar.X11.Actions (stripActions) +import Xmobar.System.Utils (hGetLineSafe) data StdinReader = StdinReader | UnsafeStdinReader deriving (Read, Show) diff --git a/src/Xmobar/Plugins/XMonadLog.hs b/src/Xmobar/Plugins/XMonadLog.hs index 26b6162..8261980 100644 --- a/src/Xmobar/Plugins/XMonadLog.hs +++ b/src/Xmobar/Plugins/XMonadLog.hs @@ -27,7 +27,7 @@ import Codec.Binary.UTF8.String as UTF8 #define UTF8 #endif import Foreign.C (CChar) -import Xmobar.Utils (nextEvent') +import Xmobar.X11.Events (nextEvent') import Xmobar.X11.Actions (stripActions) data XMonadLog = XMonadLog diff --git a/src/Xmobar/Run/Commands.hs b/src/Xmobar/Run/Commands.hs index 198edee..2aac344 100644 --- a/src/Xmobar/Run/Commands.hs +++ b/src/Xmobar/Run/Commands.hs @@ -17,7 +17,7 @@ -- ----------------------------------------------------------------------------- -module Xmobar.Run.Commands (Command (..), Exec (..)) where +module Xmobar.Run.Commands (Command (..), Exec (..), tenthSeconds) where import Prelude import Control.Exception (handle, SomeException(..)) @@ -25,9 +25,19 @@ import Data.Char import System.Process import System.Exit import System.IO (hClose) +import Control.Concurrent import Xmobar.System.Signal -import Xmobar.Utils (hGetLineSafe, tenthSeconds) +import Xmobar.System.Utils (hGetLineSafe) + +-- | Work around to the Int max bound: since threadDelay takes an Int, it +-- is not possible to set a thread delay grater than about 45 minutes. +-- With a little recursion we solve the problem. +tenthSeconds :: Int -> IO () +tenthSeconds s | s >= x = do threadDelay (x * 100000) + tenthSeconds (s - x) + | otherwise = threadDelay (s * 100000) + where x = (maxBound :: Int) `div` 100000 class Show e => Exec e where alias :: e -> String diff --git a/src/Xmobar/System/Utils.hs b/src/Xmobar/System/Utils.hs new file mode 100644 index 0000000..d4bdd78 --- /dev/null +++ b/src/Xmobar/System/Utils.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE CPP #-} + +------------------------------------------------------------------------------ +-- | +-- Module: Utils +-- Copyright: (c) 2010, 2018 Jose Antonio Ortega Ruiz +-- License: BSD3-style (see LICENSE) +-- +-- Maintainer: Jose A Ortega Ruiz +-- Stability: unstable +-- Portability: unportable +-- Created: Sat Dec 11, 2010 20:55 +-- +-- +-- Miscellaneous utility functions +-- +------------------------------------------------------------------------------ + + +module Xmobar.System.Utils (expandHome, changeLoop, hGetLineSafe) +where + +import Control.Monad +import Control.Concurrent.STM + +import System.Environment +import System.FilePath +import System.IO + +#if defined XFT || defined UTF8 +import qualified System.IO as S (hGetLine) +#endif + +hGetLineSafe :: Handle -> IO String +#if defined XFT || defined UTF8 +hGetLineSafe = S.hGetLine +#else +hGetLineSafe = hGetLine +#endif + + +expandHome :: FilePath -> IO FilePath +expandHome ('~':'/':path) = fmap ( path) (getEnv "HOME") +expandHome p = return p + +changeLoop :: Eq a => STM a -> (a -> IO ()) -> IO () +changeLoop s f = atomically s >>= go + where + go old = do + f old + go =<< atomically (do + new <- s + guard (new /= old) + return new) diff --git a/src/Xmobar/Utils.hs b/src/Xmobar/Utils.hs deleted file mode 100644 index a2da606..0000000 --- a/src/Xmobar/Utils.hs +++ /dev/null @@ -1,82 +0,0 @@ -{-# LANGUAGE CPP #-} - ------------------------------------------------------------------------------- --- | --- Module: Utils --- Copyright: (c) 2010, 2018 Jose Antonio Ortega Ruiz --- License: BSD3-style (see LICENSE) --- --- Maintainer: Jose A Ortega Ruiz --- Stability: unstable --- Portability: unportable --- Created: Sat Dec 11, 2010 20:55 --- --- --- Miscellaneous utility functions --- ------------------------------------------------------------------------------- - - -module Xmobar.Utils - (expandHome, changeLoop, hGetLineSafe, nextEvent', tenthSeconds) -where - -import Control.Monad -import Control.Concurrent -import Control.Concurrent.STM -import System.Posix.Types (Fd(..)) - -import System.Environment -import System.FilePath -import System.IO - -import Graphics.X11.Xlib ( - Display(..), XEventPtr, nextEvent, pending, connectionNumber) - -#if defined XFT || defined UTF8 -import qualified System.IO as S (hGetLine) -#endif - -hGetLineSafe :: Handle -> IO String -#if defined XFT || defined UTF8 -hGetLineSafe = S.hGetLine -#else -hGetLineSafe = hGetLine -#endif - - -expandHome :: FilePath -> IO FilePath -expandHome ('~':'/':path) = fmap ( path) (getEnv "HOME") -expandHome p = return p - -changeLoop :: Eq a => STM a -> (a -> IO ()) -> IO () -changeLoop s f = atomically s >>= go - where - go old = do - f old - go =<< atomically (do - new <- s - guard (new /= old) - return new) - --- | A version of nextEvent that does not block in foreign calls. -nextEvent' :: Display -> XEventPtr -> IO () -nextEvent' d p = do - pend <- pending d - if pend /= 0 - then nextEvent d p - else do - threadWaitRead (Fd fd) - nextEvent' d p - where - fd = connectionNumber d - - --- | Work around to the Int max bound: since threadDelay takes an Int, it --- is not possible to set a thread delay grater than about 45 minutes. --- With a little recursion we solve the problem. -tenthSeconds :: Int -> IO () -tenthSeconds s | s >= x = do threadDelay (x * 100000) - tenthSeconds (s - x) - | otherwise = threadDelay (s * 100000) - where x = (maxBound :: Int) `div` 100000 diff --git a/src/Xmobar/X11/Events.hs b/src/Xmobar/X11/Events.hs new file mode 100644 index 0000000..4334f6b --- /dev/null +++ b/src/Xmobar/X11/Events.hs @@ -0,0 +1,36 @@ +------------------------------------------------------------------------------ +-- | +-- Module: Xmobar.X11.Events +-- Copyright: (c) 2018 Jose Antonio Ortega Ruiz +-- License: BSD3-style (see LICENSE) +-- +-- Maintainer: jao@gnu.org +-- Stability: unstable +-- Portability: portable +-- Created: Sun Nov 25, 2018 23:24 +-- +-- +-- Utilities or event handling +-- +------------------------------------------------------------------------------ + + +module Xmobar.X11.Events(nextEvent') where + +import Control.Concurrent +import System.Posix.Types (Fd(..)) + +import Graphics.X11.Xlib ( + Display(..), XEventPtr, nextEvent, pending, connectionNumber) + +-- | A version of nextEvent that does not block in foreign calls. +nextEvent' :: Display -> XEventPtr -> IO () +nextEvent' d p = do + pend <- pending d + if pend /= 0 + then nextEvent d p + else do + threadWaitRead (Fd fd) + nextEvent' d p + where + fd = connectionNumber d -- cgit v1.2.3