{-# LANGUAGE CPP #-}
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.X11.Draw
-- Copyright: (c) 2022 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: unportable
-- Created: Fri Sep 09, 2022 02:03
--
-- Drawing the xmobar contents using Cairo and Pango
--
--
------------------------------------------------------------------------------

module Xmobar.X11.Draw (draw) where

import qualified Data.Map as M

import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (ask)
import Foreign.C.Types as FT
import qualified Graphics.X11.Xlib as X11

import qualified Xmobar.Config.Types as C
import qualified Xmobar.Draw.Types as D
import qualified Xmobar.Draw.Cairo as DC

import qualified Xmobar.X11.Bitmap as B
import qualified Xmobar.X11.Types as T
import qualified Xmobar.X11.CairoSurface as CS

#ifdef XRENDER
import qualified Xmobar.X11.XRender as XRender
#endif

drawXBitmap :: T.XConf -> X11.GC -> X11.Pixmap -> D.IconDrawer
drawXBitmap :: XConf -> GC -> Pixmap -> IconDrawer
drawXBitmap XConf
xconf GC
gc Pixmap
p Double
h Double
v [Char]
path [Char]
fc [Char]
bc = do
  let disp :: Display
disp = XConf -> Display
T.display XConf
xconf
  case [Char] -> Map [Char] Bitmap -> Maybe Bitmap
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup [Char]
path (XConf -> Map [Char] Bitmap
T.iconCache XConf
xconf) of
    Just Bitmap
bm -> IO () -> IO ()
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Display
-> Pixmap
-> GC
-> [Char]
-> [Char]
-> Int32
-> Int32
-> Bitmap
-> IO ()
B.drawBitmap Display
disp Pixmap
p GC
gc [Char]
fc [Char]
bc (Double -> Int32
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
h) (Double -> Int32
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
v) Bitmap
bm
    Maybe Bitmap
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

lookupXBitmap :: T.XConf -> String -> (Double, Double)
lookupXBitmap :: XConf -> [Char] -> (Double, Double)
lookupXBitmap XConf
xconf [Char]
path =
  case [Char] -> Map [Char] Bitmap -> Maybe Bitmap
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup [Char]
path (XConf -> Map [Char] Bitmap
T.iconCache XConf
xconf) of
    Just Bitmap
bm -> (Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bitmap -> Word32
B.width Bitmap
bm), Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bitmap -> Word32
B.height Bitmap
bm))
    Maybe Bitmap
Nothing -> (Double
0, Double
0)

withPixmap :: X11.Display -> X11.Drawable -> X11.Rectangle -> FT.CInt
           -> (X11.GC -> X11.Pixmap -> IO a) -> IO a
withPixmap :: forall a.
Display
-> Pixmap -> Rectangle -> CInt -> (GC -> Pixmap -> IO a) -> IO a
withPixmap Display
disp Pixmap
win (X11.Rectangle Int32
_ Int32
_ Word32
w Word32
h) CInt
depth GC -> Pixmap -> IO a
action = do
  p <- Display -> Pixmap -> Word32 -> Word32 -> CInt -> IO Pixmap
X11.createPixmap Display
disp Pixmap
win Word32
w Word32
h CInt
depth
  gc <- X11.createGC disp win
  X11.setGraphicsExposures disp gc False
  res <- action gc p
  -- copy the pixmap with the new string to the window
  X11.copyArea disp p win gc 0 0 w h 0 0
  -- free up everything (we do not want to leak memory!)
  X11.freeGC disp gc
  X11.freePixmap disp p
  -- resync (discard events, we don't read/process events from this display conn)
  X11.sync disp True
  return res

draw :: [[C.Segment]] -> T.X [D.ActionPos]
draw :: [[Segment]] -> X [ActionPos]
draw [[Segment]]
segments = do
  xconf <- ReaderT XConf IO XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
  let disp = XConf -> Display
T.display XConf
xconf
      win = XConf -> Pixmap
T.window XConf
xconf
      rect@(X11.Rectangle _ _ w h) = T.rect xconf
      screen = Display -> Screen
X11.defaultScreenOfDisplay Display
disp
      depth = Screen -> CInt
X11.defaultDepthOfScreen Screen
screen
      vis = Screen -> Visual
X11.defaultVisualOfScreen Screen
screen
      conf = XConf -> Config
T.config XConf
xconf

  liftIO $ withPixmap disp win rect depth $ \GC
gc Pixmap
p -> do
    let bdraw :: IconDrawer
bdraw = XConf -> GC -> Pixmap -> IconDrawer
drawXBitmap XConf
xconf GC
gc Pixmap
p
        blook :: [Char] -> (Double, Double)
blook = XConf -> [Char] -> (Double, Double)
lookupXBitmap XConf
xconf
        dctx :: DrawContext
dctx = IconDrawer
-> ([Char] -> (Double, Double))
-> Config
-> Double
-> Double
-> [[Segment]]
-> DrawContext
D.DC IconDrawer
bdraw [Char] -> (Double, Double)
blook Config
conf (Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w) (Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
h) [[Segment]]
segments
        render :: Surface -> IO [ActionPos]
render = DrawContext -> Surface -> IO [ActionPos]
DC.drawSegments DrawContext
dctx

#ifdef XRENDER
        color = C.bgColor conf
        alph = C.alpha conf
    XRender.drawBackground disp p color alph rect
#endif

    Display
-> Pixmap
-> Visual
-> Int
-> Int
-> (Surface -> IO [ActionPos])
-> IO [ActionPos]
forall a.
Display
-> Pixmap -> Visual -> Int -> Int -> (Surface -> IO a) -> IO a
CS.withXlibSurface Display
disp Pixmap
p Visual
vis (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w) (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
h) Surface -> IO [ActionPos]
render