{-# 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 -> Dimension -> IconDrawer
drawXBitmap XConf
xconf GC
gc Dimension
p Double
h Double
v String
path String
fc String
bc = do
  let disp :: Display
disp = XConf -> Display
T.display XConf
xconf
  case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
path (XConf -> BitmapCache
T.iconCache XConf
xconf) of
    Just Bitmap
bm -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Display
-> Dimension
-> GC
-> String
-> String
-> Position
-> Position
-> Bitmap
-> IO ()
B.drawBitmap Display
disp Dimension
p GC
gc String
fc String
bc (forall a b. (RealFrac a, Integral b) => a -> b
round Double
h) (forall a b. (RealFrac a, Integral b) => a -> b
round Double
v) Bitmap
bm
    Maybe Bitmap
Nothing -> 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 forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
path (XConf -> BitmapCache
T.iconCache XConf
xconf) of
    Just Bitmap
bm -> (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bitmap -> Dimension
B.width Bitmap
bm), 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
-> Dimension
-> Rectangle
-> CInt
-> (GC -> Dimension -> IO a)
-> IO a
withPixmap Display
disp Dimension
win (X11.Rectangle Position
_ Position
_ Dimension
w Dimension
h) CInt
depth GC -> Dimension -> IO a
action = do
  Dimension
p <- Display
-> Dimension -> Dimension -> Dimension -> CInt -> IO Dimension
X11.createPixmap Display
disp Dimension
win Dimension
w Dimension
h CInt
depth
  GC
gc <- Display -> Dimension -> IO GC
X11.createGC Display
disp Dimension
win
  Display -> GC -> Bool -> IO ()
X11.setGraphicsExposures Display
disp GC
gc Bool
False
  a
res <- GC -> Dimension -> IO a
action GC
gc Dimension
p
  -- copy the pixmap with the new string to the window
  Display
-> Dimension
-> Dimension
-> GC
-> Position
-> Position
-> Dimension
-> Dimension
-> Position
-> Position
-> IO ()
X11.copyArea Display
disp Dimension
p Dimension
win GC
gc Position
0 Position
0 Dimension
w Dimension
h Position
0 Position
0
  -- free up everything (we do not want to leak memory!)
  Display -> GC -> IO ()
X11.freeGC Display
disp GC
gc
  Display -> Dimension -> IO ()
X11.freePixmap Display
disp Dimension
p
  -- resync (discard events, we don't read/process events from this display conn)
  Display -> Bool -> IO ()
X11.sync Display
disp Bool
True
  forall (m :: * -> *) a. Monad m => a -> m a
return a
res

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

  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a.
Display
-> Dimension
-> Rectangle
-> CInt
-> (GC -> Dimension -> IO a)
-> IO a
withPixmap Display
disp Dimension
win Rectangle
rect CInt
depth forall a b. (a -> b) -> a -> b
$ \GC
gc Dimension
p -> do
    let bdraw :: IconDrawer
bdraw = XConf -> GC -> Dimension -> IconDrawer
drawXBitmap XConf
xconf GC
gc Dimension
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 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w) (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 -> Dimension -> String -> Int -> Rectangle -> IO ()
XRender.drawBackground Display
disp Dimension
p String
color Int
alph Rectangle
rect
#endif

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