{-# LANGUAGE CPP, FlexibleContexts #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  X11.Bitmap
-- Copyright   :  (C) 2013, 2015, 2017, 2018, 2022, 2024 Alexander Polakov
-- License     :  BSD3
--
-- Maintainer  :  jao@gnu.org
-- Stability   :  unstable
-- Portability :  unportable
--
-----------------------------------------------------------------------------

module Xmobar.X11.Bitmap
 ( updateCache
 , drawBitmap
 , Bitmap(..)
 , BitmapCache) where

import Control.Monad
import Control.Monad.Trans(MonadIO(..))
import Data.Map hiding (map)

import Graphics.X11.Xlib hiding (Segment)

import System.Directory (doesFileExist)
import System.FilePath ((</>))
import System.Mem.Weak ( addFinalizer )

import Xmobar.X11.ColorCache

#ifdef XPM
import Xmobar.X11.XPMFile(readXPMFile)
import Control.Applicative((<|>))
#endif

#if MIN_VERSION_mtl(2, 2, 1)
import Control.Monad.Except(MonadError(..), runExceptT)

#else
import Control.Monad.Error(MonadError(..))
import Control.Monad.Trans.Error(ErrorT, runErrorT)

runExceptT :: ErrorT e m a -> m (Either e a)
runExceptT = runErrorT

#endif

data BitmapType = Mono Pixel | Poly

data Bitmap = Bitmap { Bitmap -> Dimension
width  :: Dimension
                     , Bitmap -> Dimension
height :: Dimension
                     , Bitmap -> Word64
pixmap :: Pixmap
                     , Bitmap -> Maybe Word64
shapePixmap :: Maybe Pixmap
                     , Bitmap -> BitmapType
bitmapType :: BitmapType
                     }

type BitmapCache = Map FilePath Bitmap

updateCache :: Display -> Window -> BitmapCache -> FilePath -> [FilePath]
            -> IO BitmapCache
updateCache :: Display
-> Word64 -> BitmapCache -> [Char] -> [[Char]] -> IO BitmapCache
updateCache Display
dpy Word64
win BitmapCache
cache [Char]
iconRoot [[Char]]
paths = do
  let expandPath :: [Char] -> [Char]
expandPath path :: [Char]
path@(Char
'/':[Char]
_) = [Char]
path
      expandPath path :: [Char]
path@(Char
'.':Char
'/':[Char]
_) = [Char]
path
      expandPath path :: [Char]
path@(Char
'.':Char
'.':Char
'/':[Char]
_) = [Char]
path
      expandPath [Char]
path = [Char]
iconRoot [Char] -> [Char] -> [Char]
</> [Char]
path
      go :: BitmapCache -> [Char] -> IO BitmapCache
go BitmapCache
m [Char]
path = if [Char] -> BitmapCache -> Bool
forall k a. Ord k => k -> Map k a -> Bool
member [Char]
path BitmapCache
m
                     then BitmapCache -> IO BitmapCache
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return BitmapCache
m
                     else do bitmap <- Display -> Word64 -> [Char] -> IO (Maybe Bitmap)
loadBitmap Display
dpy Word64
win ([Char] -> IO (Maybe Bitmap)) -> [Char] -> IO (Maybe Bitmap)
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
expandPath [Char]
path
                             return $ maybe m (\Bitmap
b -> [Char] -> Bitmap -> BitmapCache -> BitmapCache
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert [Char]
path Bitmap
b BitmapCache
m) bitmap
  (BitmapCache -> [Char] -> IO BitmapCache)
-> BitmapCache -> [[Char]] -> IO BitmapCache
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM BitmapCache -> [Char] -> IO BitmapCache
go BitmapCache
cache [[Char]]
paths

readBitmapFile'
    :: (MonadError String m, MonadIO m)
    => Display
    -> Drawable
    -> String
    -> m (Dimension, Dimension, Pixmap)
readBitmapFile' :: forall (m :: * -> *).
(MonadError [Char] m, MonadIO m) =>
Display -> Word64 -> [Char] -> m (Dimension, Dimension, Word64)
readBitmapFile' Display
d Word64
w [Char]
p = do
   res <- IO
  (Either
     [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
-> m (Either
        [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO
   (Either
      [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
 -> m (Either
         [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt)))
-> IO
     (Either
        [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
-> m (Either
        [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
forall a b. (a -> b) -> a -> b
$ Display
-> Word64
-> [Char]
-> IO
     (Either
        [Char] (Dimension, Dimension, Word64, Maybe CInt, Maybe CInt))
readBitmapFile Display
d Word64
w [Char]
p
   case res of
    Left [Char]
err -> [Char] -> m (Dimension, Dimension, Word64)
forall a. [Char] -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError [Char]
err
    Right (Dimension
bw, Dimension
bh, Word64
bp, Maybe CInt
_, Maybe CInt
_) -> (Dimension, Dimension, Word64) -> m (Dimension, Dimension, Word64)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dimension
bw, Dimension
bh, Word64
bp)

loadBitmap :: Display -> Drawable -> FilePath -> IO (Maybe Bitmap)
loadBitmap :: Display -> Word64 -> [Char] -> IO (Maybe Bitmap)
loadBitmap Display
d Word64
w [Char]
p = do
    exist <- [Char] -> IO Bool
doesFileExist [Char]
p
    if exist
       then do
#ifdef XPM
            res <- runExceptT (tryXBM <|> tryXPM)
#else
            res <- runExceptT tryXBM
#endif
            case res of
                 Right Bitmap
b -> Maybe Bitmap -> IO (Maybe Bitmap)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Bitmap -> IO (Maybe Bitmap))
-> Maybe Bitmap -> IO (Maybe Bitmap)
forall a b. (a -> b) -> a -> b
$ Bitmap -> Maybe Bitmap
forall a. a -> Maybe a
Just Bitmap
b
                 Left [Char]
err -> do
                     [Char] -> IO ()
putStrLn [Char]
err
                     Maybe Bitmap -> IO (Maybe Bitmap)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Bitmap
forall a. Maybe a
Nothing
       else
           return Nothing
 where tryXBM :: ExceptT [Char] IO Bitmap
tryXBM = do
           (bw, bh, bp) <- Display
-> Word64
-> [Char]
-> ExceptT [Char] IO (Dimension, Dimension, Word64)
forall (m :: * -> *).
(MonadError [Char] m, MonadIO m) =>
Display -> Word64 -> [Char] -> m (Dimension, Dimension, Word64)
readBitmapFile' Display
d Word64
w [Char]
p
           liftIO $ addFinalizer bp (freePixmap d bp)
           return $ Bitmap bw bh bp Nothing (Mono 1)
#ifdef XPM
       tryXPM :: ExceptT [Char] IO Bitmap
tryXPM = do
           (bw, bh, bp, mbpm) <- Display
-> Word64
-> [Char]
-> ExceptT [Char] IO (Dimension, Dimension, Word64, Maybe Word64)
forall (m :: * -> *).
(MonadError [Char] m, MonadIO m) =>
Display
-> Word64
-> [Char]
-> m (Dimension, Dimension, Word64, Maybe Word64)
readXPMFile Display
d Word64
w [Char]
p
           liftIO $ addFinalizer bp (freePixmap d bp)
           case mbpm of
                Maybe Word64
Nothing -> () -> ExceptT [Char] IO ()
forall a. a -> ExceptT [Char] IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                Just Word64
bpm -> IO () -> ExceptT [Char] IO ()
forall a. IO a -> ExceptT [Char] IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT [Char] IO ()) -> IO () -> ExceptT [Char] IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> IO () -> IO ()
forall key. key -> IO () -> IO ()
addFinalizer Word64
bpm (Display -> Word64 -> IO ()
freePixmap Display
d Word64
bpm)
           return $ Bitmap bw bh bp mbpm Poly
#endif

drawBitmap :: Display -> Drawable -> GC -> String -> String
              -> Position -> Position -> Bitmap -> IO ()
drawBitmap :: Display
-> Word64
-> GC
-> [Char]
-> [Char]
-> Int32
-> Int32
-> Bitmap
-> IO ()
drawBitmap Display
d Word64
p GC
gc [Char]
fc [Char]
bc Int32
x Int32
y Bitmap
i =
  Display -> [[Char]] -> ([Word64] -> IO ()) -> IO ()
forall (m :: * -> *) a.
MonadIO m =>
Display -> [[Char]] -> ([Word64] -> m a) -> m a
withColors Display
d [[Char]
fc, [Char]
bc] (([Word64] -> IO ()) -> IO ()) -> ([Word64] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[Word64]
cs -> do
    let (Word64
fc', Word64
bc') = ([Word64] -> Word64
forall a. HasCallStack => [a] -> a
head [Word64]
cs, [Word64]
cs [Word64] -> Int -> Word64
forall a. HasCallStack => [a] -> Int -> a
!! Int
1)
        w :: Dimension
w = Bitmap -> Dimension
width Bitmap
i
        h :: Dimension
h = Bitmap -> Dimension
height Bitmap
i
        y' :: Int32
y' = Int32
1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Dimension -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2
    Display -> GC -> Word64 -> IO ()
setForeground Display
d GC
gc Word64
fc'
    Display -> GC -> Word64 -> IO ()
setBackground Display
d GC
gc Word64
bc'
    case Bitmap -> Maybe Word64
shapePixmap Bitmap
i of
         Maybe Word64
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
         Just Word64
mask -> Display -> GC -> Int32 -> Int32 -> IO ()
setClipOrigin Display
d GC
gc Int32
x Int32
y' IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Display -> GC -> Word64 -> IO ()
setClipMask Display
d GC
gc Word64
mask
    case Bitmap -> BitmapType
bitmapType Bitmap
i of
         BitmapType
Poly -> Display
-> Word64
-> Word64
-> GC
-> Int32
-> Int32
-> Dimension
-> Dimension
-> Int32
-> Int32
-> IO ()
copyArea Display
d (Bitmap -> Word64
pixmap Bitmap
i) Word64
p GC
gc Int32
0 Int32
0 Dimension
w Dimension
h Int32
x Int32
y'
         Mono Word64
pl -> Display
-> Word64
-> Word64
-> GC
-> Int32
-> Int32
-> Dimension
-> Dimension
-> Int32
-> Int32
-> Word64
-> IO ()
copyPlane Display
d (Bitmap -> Word64
pixmap Bitmap
i) Word64
p GC
gc Int32
0 Int32
0 Dimension
w Dimension
h Int32
x Int32
y' Word64
pl
    Display -> GC -> Word64 -> IO ()
setClipMask Display
d GC
gc Word64
0