{-# 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 String
path String
fc String
bc = do
  let disp :: Display
disp = XConf -> Display
T.display XConf
xconf
  case String -> Map String Bitmap -> Maybe Bitmap
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
path (XConf -> Map String 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
-> String
-> String
-> Position
-> Position
-> Bitmap
-> IO ()
B.drawBitmap Display
disp Pixmap
p GC
gc String
fc String
bc (Double -> Position
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
h) (Double -> Position
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 -> String -> (Double, Double)
lookupXBitmap XConf
xconf String
path =
  case String -> Map String Bitmap -> Maybe Bitmap
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
path (XConf -> Map String Bitmap
T.iconCache XConf
xconf) of
    Just Bitmap
bm -> (Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bitmap -> Dimension
B.width Bitmap
bm), Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bitmap -> Dimension
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 Position
_ Position
_ Dimension
w Dimension
h) CInt
depth GC -> Pixmap -> IO a
action = do
  p <- Display -> Pixmap -> Dimension -> Dimension -> CInt -> IO Pixmap
X11.createPixmap Display
disp Pixmap
win Dimension
w Dimension
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 :: String -> (Double, Double)
blook = XConf -> String -> (Double, Double)
lookupXBitmap XConf
xconf
        dctx :: DrawContext
dctx = IconDrawer
-> (String -> (Double, Double))
-> Config
-> Double
-> Double
-> [[Segment]]
-> DrawContext
D.DC IconDrawer
bdraw String -> (Double, Double)
blook Config
conf (Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w) (Dimension -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h) [[Segment]]
segments
        render :: Surface -> IO [ActionPos]
render = DrawContext -> Surface -> IO [ActionPos]
DC.drawSegments DrawContext
dctx

#ifdef XRENDER
        color :: String
color = Config -> String
C.bgColor Config
conf
        alph :: Int
alph = Config -> Int
C.alpha Config
conf
    Display -> Pixmap -> String -> Int -> Rectangle -> IO ()
XRender.drawBackground Display
disp Pixmap
p String
color Int
alph Rectangle
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 (Dimension -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w) (Dimension -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h) Surface -> IO [ActionPos]
render