This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
phi/lib/Phi/X11.hs

272 lines
11 KiB
Haskell
Raw Normal View History

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2011-07-12 16:47:24 +02:00
module Phi.X11 ( XConfig(..)
, defaultXConfig
2011-07-14 06:16:04 +02:00
, initPhiX
) where
import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras
import Graphics.X11.Xinerama
2011-07-13 02:13:01 +02:00
import Graphics.Rendering.Cairo
import Control.Monad
import Data.Maybe
import Data.Bits
2011-07-13 20:13:04 +02:00
import Data.Char
import Control.Monad.State
2011-07-12 16:47:24 +02:00
import Control.Monad.Reader
import Control.Monad.Trans
2011-07-14 06:16:04 +02:00
import Phi.Phi
2011-07-14 00:09:20 +02:00
import qualified Phi.Types as Phi
2011-07-12 14:41:25 +02:00
import qualified Phi.Panel as Panel
2011-07-14 00:09:20 +02:00
import qualified Phi.Widget as Widget
2011-07-12 19:09:05 +02:00
import Phi.X11.Atoms
2011-07-13 02:13:01 +02:00
import qualified Phi.Bindings.Util as Util
2011-07-12 16:47:24 +02:00
data XConfig = XConfig { phiXScreenInfo :: Display -> IO [Rectangle]
}
2011-07-12 19:09:05 +02:00
data PhiState = PhiState { phiRootPixmap :: Pixmap
, phiPanels :: [PanelState]
}
2011-07-14 00:09:20 +02:00
data PanelState = PanelState { panelWindow :: Window
, panelGC :: GC
, panelPixmap :: Pixmap
, panelSurface :: Surface
, panelArea :: Rectangle
, panelScreenArea :: Rectangle
, panelWidgetStates :: [Widget.WidgetState]
}
2011-07-12 16:47:24 +02:00
data PhiConfig = PhiConfig { phiPanelConfig :: Panel.PanelConfig
, phiXConfig :: XConfig
2011-07-12 19:09:05 +02:00
, phiDisplay :: Display
, phiAtoms :: Atoms
2011-07-12 16:47:24 +02:00
}
newtype PhiReader a = PhiReader (ReaderT PhiConfig IO a)
deriving (Monad, MonadReader PhiConfig, MonadIO)
runPhiReader :: PhiConfig -> PhiReader a -> IO a
runPhiReader config (PhiReader a) = runReaderT a config
2011-07-14 06:16:04 +02:00
newtype PhiX a = PhiX (StateT PhiState PhiReader a)
deriving (Monad, MonadState PhiState, MonadReader PhiConfig, MonadIO)
2011-07-14 06:16:04 +02:00
runPhiX :: PhiConfig -> PhiState -> PhiX a -> IO (a, PhiState)
runPhiX config st (PhiX a) = runPhiReader config $ runStateT a st
2011-07-14 06:16:04 +02:00
liftIOContToPhiX :: ((a -> IO (b, PhiState)) -> IO (b, PhiState)) -> (a -> PhiX b) -> PhiX b
liftIOContToPhiX f c = do
2011-07-12 16:47:24 +02:00
config <- ask
state <- get
2011-07-14 06:16:04 +02:00
(a, state') <- liftIO $ f $ runPhiX config state . c
2011-07-12 16:47:24 +02:00
put state'
return a
2011-07-12 16:47:24 +02:00
defaultXConfig = XConfig { phiXScreenInfo = getScreenInfo
}
2011-07-13 02:13:01 +02:00
2011-07-14 06:16:04 +02:00
initPhiX :: Phi -> XConfig -> Panel.PanelConfig -> [Widget.Widget] -> IO ()
initPhiX phi xconfig config widgets = do
disp <- openDisplay []
2011-07-12 19:09:05 +02:00
atoms <- initAtoms disp
selectInput disp (defaultRootWindow disp) $ propertyChangeMask.|.structureNotifyMask
2011-07-14 06:16:04 +02:00
runPhiX PhiConfig { phiXConfig = xconfig, phiPanelConfig = config, phiDisplay = disp, phiAtoms = atoms } PhiState { phiRootPixmap = 0, phiPanels = [] } $ do
updateRootPixmap
2011-07-12 16:47:24 +02:00
screens <- liftIO $ phiXScreenInfo xconfig disp
2011-07-14 00:09:20 +02:00
panels <- mapM (createPanel widgets) screens
forM_ panels $ \panel -> do
setPanelProperties panel
liftIO $ mapWindow disp (panelWindow panel)
modify $ \state -> state { phiPanels = panels }
2011-07-12 19:09:05 +02:00
updatePanels True
2011-07-14 06:16:04 +02:00
liftIOContToPhiX allocaXEvent $ \xevent -> do
forever $ do
liftIO $ nextEvent disp xevent
event <- liftIO $ getEvent xevent
case event of
2011-07-12 19:09:05 +02:00
ExposeEvent {} -> updatePanels False
PropertyEvent {} -> handlePropertyUpdate event
_ -> return ()
return ()
2011-07-14 06:16:04 +02:00
updatePanels :: Bool -> PhiX ()
2011-07-12 19:09:05 +02:00
updatePanels redraw = do
disp <- asks phiDisplay
2011-07-13 02:13:01 +02:00
rootPixmap <- gets phiRootPixmap
panels <- gets phiPanels
2011-07-13 02:13:01 +02:00
2011-07-14 00:09:20 +02:00
panels' <- forM panels $ \panel -> do
newPanel <- if not redraw then return panel else do
2011-07-13 02:13:01 +02:00
let surface = panelSurface panel
area = panelArea panel
2011-07-14 01:47:10 +02:00
layoutedWidgets = withDimension area $ Widget.layoutWidgets (panelWidgetStates panel) 0 0
2011-07-14 00:09:20 +02:00
panel' = panel { panelWidgetStates = layoutedWidgets }
2011-07-13 02:13:01 +02:00
-- draw background
liftIO $ withRectangle (panelArea panel) (copyArea disp rootPixmap (panelPixmap panel) (panelGC panel)) 0 0
surfaceMarkDirty surface
2011-07-14 01:47:10 +02:00
renderWith surface $ Widget.renderWidgets layoutedWidgets
2011-07-13 02:13:01 +02:00
surfaceFlush surface
2011-07-14 00:09:20 +02:00
return panel'
2011-07-13 02:13:01 +02:00
-- copy pixmap to window
liftIO $ withDimension (panelArea panel) (copyArea disp (panelPixmap panel) (panelWindow panel) (panelGC panel) 0 0) 0 0
2011-07-14 00:09:20 +02:00
return newPanel
modify $ \state -> state { phiPanels = panels' }
2011-07-14 06:16:04 +02:00
handlePropertyUpdate :: Event -> PhiX ()
handlePropertyUpdate PropertyEvent { ev_atom = atom } = do
2011-07-12 19:09:05 +02:00
atoms <- asks phiAtoms
panels <- gets phiPanels
2011-07-12 19:09:05 +02:00
when (atom == atom_XROOTPMAP_ID atoms || atom == atom_XROOTMAP_ID atoms) $ do
updateRootPixmap
2011-07-12 19:09:05 +02:00
updatePanels True
2011-07-14 06:16:04 +02:00
updateRootPixmap :: PhiX ()
updateRootPixmap = do
2011-07-12 19:09:05 +02:00
disp <- asks phiDisplay
atoms <- asks phiAtoms
let screen = defaultScreen disp
rootwin = defaultRootWindow disp
2011-07-12 19:09:05 +02:00
pixmap <- liftM (fromMaybe 0 . listToMaybe . join . catMaybes) $ forM [atom_XROOTPMAP_ID atoms, atom_XROOTMAP_ID atoms] $
\atom -> liftIO $ rawGetWindowProperty 32 disp atom rootwin
modify $ \state -> state { phiRootPixmap = pixmap }
2011-07-14 06:16:04 +02:00
createPanel :: [Widget.Widget] -> Rectangle -> PhiX PanelState
2011-07-14 00:09:20 +02:00
createPanel widgets screenRect = do
2011-07-12 16:47:24 +02:00
config <- asks phiPanelConfig
2011-07-12 19:09:05 +02:00
disp <- asks phiDisplay
2011-07-13 02:13:01 +02:00
let rect = panelBounds config screenRect
win <- createPanelWindow rect
gc <- liftIO $ createGC disp win
2011-07-13 02:13:01 +02:00
let screen = defaultScreen disp
depth = defaultDepth disp screen
visual = defaultVisual disp screen
pixmap <- liftIO $ withDimension rect (createPixmap disp win) depth
surface <- liftIO $ withDimension rect $ Util.createXlibSurface disp pixmap visual
2011-07-14 00:09:20 +02:00
return PanelState { panelWindow = win, panelGC = gc, panelPixmap = pixmap, panelSurface = surface, panelArea = rect, panelScreenArea = screenRect, panelWidgetStates = map Widget.createWidgetState widgets }
2011-07-14 06:16:04 +02:00
createPanelWindow :: Rectangle -> PhiX Window
createPanelWindow rect = do
2011-07-12 19:09:05 +02:00
disp <- asks phiDisplay
let screen = defaultScreen disp
depth = defaultDepth disp screen
visual = defaultVisual disp screen
colormap = defaultColormap disp screen
rootwin = defaultRootWindow disp
mask = cWEventMask.|.cWColormap.|.cWBackPixel.|.cWBorderPixel
liftIO $ allocaSetWindowAttributes $ \attr -> do
set_colormap attr colormap
set_background_pixel attr 0
set_border_pixel attr 0
set_event_mask attr exposureMask
withRectangle rect (createWindow disp rootwin) 0 depth inputOutput visual mask attr
2011-07-14 06:16:04 +02:00
setPanelProperties :: PanelState -> PhiX ()
setPanelProperties panel = do
2011-07-12 19:09:05 +02:00
disp <- asks phiDisplay
atoms <- asks phiAtoms
liftIO $ do
storeName disp (panelWindow panel) "Phi"
2011-07-13 20:13:04 +02:00
changeProperty8 disp (panelWindow panel) (atom_NET_WM_NAME atoms) (atomUTF8_STRING atoms) propModeReplace $ map (fromIntegral . ord) "Phi"
2011-07-12 19:09:05 +02:00
changeProperty32 disp (panelWindow panel) (atom_NET_WM_WINDOW_TYPE atoms) aTOM propModeReplace [fromIntegral (atom_NET_WM_WINDOW_TYPE_DOCK atoms)]
changeProperty32 disp (panelWindow panel) (atom_NET_WM_DESKTOP atoms) cARDINAL propModeReplace [0xFFFFFFFF]
changeProperty32 disp (panelWindow panel) (atom_NET_WM_STATE atoms) aTOM propModeReplace [ fromIntegral (atom_NET_WM_STATE_SKIP_PAGER atoms)
, fromIntegral (atom_NET_WM_STATE_SKIP_TASKBAR atoms)
, fromIntegral (atom_NET_WM_STATE_STICKY atoms)
, fromIntegral (atom_NET_WM_STATE_BELOW atoms)
]
setWMHints disp (panelWindow panel) WMHints { wmh_flags = fromIntegral inputHintBit
, wmh_input = False
, wmh_initial_state = 0
, wmh_icon_pixmap = 0
, wmh_icon_window = 0
, wmh_icon_x = 0
, wmh_icon_y = 0
, wmh_icon_mask = 0
, wmh_window_group = 0
}
2011-07-12 19:09:05 +02:00
changeProperty32 disp (panelWindow panel) (atom_MOTIF_WM_HINTS atoms) (atom_MOTIF_WM_HINTS atoms) propModeReplace [ 2, 0, 0, 0, 0 ]
2011-07-13 02:13:01 +02:00
Util.setClassHint disp (panelWindow panel) ClassHint { resName = "phi", resClass = "Phi" }
setStruts panel
2011-07-14 06:16:04 +02:00
setStruts :: PanelState -> PhiX ()
setStruts panel = do
2011-07-12 19:09:05 +02:00
atoms <- asks phiAtoms
disp <- asks phiDisplay
2011-07-12 16:47:24 +02:00
config <- asks phiPanelConfig
let rootwin = defaultRootWindow disp
2011-07-12 16:47:24 +02:00
position = Panel.panelPosition config
area = panelArea panel
(_, _, _, _, rootHeight, _, _) <- liftIO $ getGeometry disp rootwin
2011-07-12 14:41:25 +02:00
let struts = [makeStruts i | i <- [0..11]]
where
makeTopStruts 2 = (fromIntegral $ rect_y area) + (fromIntegral $ rect_height area)
makeTopStruts 8 = (fromIntegral $ rect_x area)
makeTopStruts 9 = (fromIntegral $ rect_x area) + (fromIntegral $ rect_width area) - 1
makeTopStruts _ = 0
makeBottomStruts 3 = (fromIntegral rootHeight) - (fromIntegral $ rect_y area)
makeBottomStruts 10 = (fromIntegral $ rect_x area)
makeBottomStruts 11 = (fromIntegral $ rect_x area) + (fromIntegral $ rect_width area) - 1
makeBottomStruts _ = 0
makeStruts = case position of
2011-07-14 00:09:20 +02:00
Phi.Top -> makeTopStruts
Phi.Bottom -> makeBottomStruts
liftIO $ do
2011-07-12 19:09:05 +02:00
changeProperty32 disp (panelWindow panel) (atom_NET_WM_STRUT atoms) cARDINAL propModeReplace $ take 4 struts
changeProperty32 disp (panelWindow panel) (atom_NET_WM_STRUT_PARTIAL atoms) cARDINAL propModeReplace struts
2011-07-12 16:47:24 +02:00
panelBounds :: Panel.PanelConfig -> Rectangle -> Rectangle
panelBounds config screenBounds = case Panel.panelPosition config of
2011-07-14 00:09:20 +02:00
Phi.Top -> screenBounds { rect_height = fromIntegral $ Panel.panelSize config }
Phi.Bottom -> screenBounds { rect_height = fromIntegral $ Panel.panelSize config,
2011-07-12 16:47:24 +02:00
rect_y = (rect_y screenBounds) + (fromIntegral $ rect_height screenBounds) - (fromIntegral $ Panel.panelSize config) }
2011-07-13 02:13:01 +02:00
withRectangle :: (Num x, Num y, Num w, Num h) => Rectangle -> (x -> y -> w -> h -> a) -> a
withRectangle r = withDimension r . withPosition r
2011-07-13 02:13:01 +02:00
withPosition :: (Num x, Num y) => Rectangle -> (x -> y -> a) -> a
withPosition r f = f (fromIntegral $ rect_x r) (fromIntegral $ rect_y r)
2011-07-13 02:13:01 +02:00
withDimension :: (Num w, Num h) => Rectangle -> (w -> h -> a) -> a
withDimension r f = f (fromIntegral $ rect_width r) (fromIntegral $ rect_height r)