summaryrefslogtreecommitdiffstats
path: root/lib/Phi/X11.hs
blob: 3fc08e6d7bdf3873e7428a7a6ad89b71296486f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Phi.X11 ( XConfig(..)
               , defaultXConfig
               , runPhi
               ) where

import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras
import Graphics.X11.Xinerama

import Graphics.Rendering.Cairo

import Control.Monad
import Data.Maybe
import Data.Bits
import Data.Char

import Control.Concurrent
import Control.Concurrent.MVar
import Control.Monad.State
import Control.Monad.Reader
import Control.Monad.Trans

import System.Posix.Types

import Phi.Phi
import qualified Phi.Types as Phi
import qualified Phi.Panel as Panel
import qualified Phi.Widget as Widget
import Phi.X11.Atoms
import qualified Phi.Bindings.Util as Util

data XConfig = XConfig { phiXScreenInfo :: !(Display -> IO [Rectangle])
                       }

data PhiState = PhiState { phiPhi        :: !Phi
                         , phiRootPixmap :: !Pixmap
                         , phiPanels     :: ![PanelState]
                         }

data PanelState = PanelState { panelWindow       :: !Window
                             , panelGC           :: !GC
                             , panelPixmap       :: !Pixmap
                             , panelSurface      :: !Surface
                             , panelArea         :: !Rectangle
                             , panelScreenArea   :: !Rectangle
                             , panelWidgetStates :: ![Widget.WidgetState]
                             }

data PhiConfig = PhiConfig { phiPanelConfig :: !Panel.PanelConfig
                           , phiXConfig     :: !XConfig
                           , phiAtoms       :: !Atoms
                           }

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

newtype PhiX a = PhiX (StateT PhiState PhiReader a)
               deriving (Monad, MonadState PhiState, MonadReader PhiConfig, MonadIO)

runPhiX :: PhiConfig -> PhiState -> PhiX a -> IO (a, PhiState)
runPhiX config st (PhiX a) = runPhiReader config $ runStateT a st

withMVarX :: MVar a -> (a -> PhiX b) -> PhiX b
withMVarX m f = do
  a <- liftIO $ takeMVar m
  b <- f a
  liftIO $ putMVar m a
  return b

defaultXConfig = XConfig { phiXScreenInfo = getScreenInfo
                         }


runPhi :: XConfig -> Panel.PanelConfig -> [Widget.Widget] -> IO ()
runPhi xconfig config widgets = do
  phi <- initPhi
  disp <- openDisplay []

  atoms <- initAtoms disp
  selectInput disp (defaultRootWindow disp) $ propertyChangeMask.|.structureNotifyMask
  
  runPhiX PhiConfig { phiXConfig = xconfig, phiPanelConfig = config, phiAtoms = atoms } PhiState { phiPhi = phi, phiRootPixmap = 0, phiPanels = [] } $ do
    updateRootPixmap disp
    
    screens <- liftIO $ phiXScreenInfo xconfig disp
    
    dispvar <- liftIO $ newEmptyMVar
    widgetStates <- liftIO $ mapM (Widget.createWidgetState phi dispvar) widgets
    panels <- mapM (createPanel disp widgetStates) screens
    
    forM_ panels $ \panel -> do
      setPanelProperties disp panel
      liftIO $ mapWindow disp (panelWindow panel)
    
    modify $ \state -> state { phiPanels = panels }
    
    updatePanels disp True
    
    liftIO $ putMVar dispvar disp
    
    liftIO $ forkIO $ receiveEvents phi dispvar

    forever $ do
      message <- liftIO $ receiveMessage phi
      handleMessage dispvar message
  return ()


handlePanel :: Message -> PanelState -> PanelState
handlePanel message panel@PanelState {panelWidgetStates = widgets} = panel {panelWidgetStates = widgets'}
  where
    widgets' = Widget.handleMessageWidgets message widgets

handleMessage :: MVar Display -> Message -> PhiX ()
handleMessage dispvar m = do
  modify $ \state@PhiState {phiPanels = panels} -> state {phiPanels = map (handlePanel m) panels}
  
  case (fromMessage m) of
    Just Repaint -> withMVarX dispvar $ \disp ->
      updatePanels disp True
    _ ->
      case (fromMessage m) of
        Just ExposeEvent {} -> withMVarX dispvar $ \disp ->
          updatePanels disp False
        Just event@PropertyEvent {} -> withMVarX dispvar $ \disp ->
          handlePropertyUpdate disp event
        _ ->
          return ()

receiveEvents :: Phi -> MVar Display -> IO ()
receiveEvents phi dispvar = do
  connection <- withMVar dispvar $ return . Fd . connectionNumber
  
  forever $ allocaXEvent $ \xevent -> do
    handled <- withMVar dispvar $ \disp -> do
      pend <- pending disp
      if pend /= 0 then
        do
          liftIO $ nextEvent disp xevent
          event <- liftIO $ getEvent xevent
          sendMessage phi event

          return True
        else return False
      
    when (not handled) $ threadWaitRead connection

updatePanels :: Display -> Bool -> PhiX ()
updatePanels disp redraw = do
  
  rootPixmap <- gets phiRootPixmap
  panels <- gets phiPanels
    
  panels' <- forM panels $ \panel -> do
    newPanel <- if not redraw then return panel else do
      let surface = panelSurface panel
          area = panelArea panel
          layoutedWidgets = withDimension area $ Widget.layoutWidgets (panelWidgetStates panel) 0 0
          panel' = panel { panelWidgetStates = layoutedWidgets }
      
      -- draw background
      liftIO $ withRectangle (panelArea panel) (copyArea disp rootPixmap (panelPixmap panel) (panelGC panel)) 0 0
      surfaceMarkDirty surface
      
      renderWith surface $ Widget.renderWidgets layoutedWidgets
      
      surfaceFlush surface
      return panel'
      
    -- copy pixmap to window
    liftIO $ withDimension (panelArea panel) (copyArea disp (panelPixmap panel) (panelWindow panel) (panelGC panel) 0 0) 0 0
    return newPanel

  modify $ \state -> state { phiPanels = panels' }


handlePropertyUpdate :: Display -> Event -> PhiX ()
handlePropertyUpdate disp PropertyEvent { ev_atom = atom } = do
  atoms <- asks phiAtoms
  panels <- gets phiPanels

  when (atom == atom_XROOTPMAP_ID atoms || atom == atom_XROOTMAP_ID atoms) $ do
    updateRootPixmap disp
    updatePanels disp True
  

updateRootPixmap :: Display -> PhiX ()
updateRootPixmap disp = do
  atoms <- asks phiAtoms
  let screen = defaultScreen disp
      rootwin = defaultRootWindow disp
  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 }


createPanel :: Display -> [Widget.WidgetState] -> Rectangle -> PhiX PanelState
createPanel disp widgets screenRect = do
  phi <- gets phiPhi
  config <- asks phiPanelConfig
  let rect = panelBounds config screenRect
      
  win <- createPanelWindow disp rect
  gc <- liftIO $ createGC disp win
  
  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
  
  return PanelState { panelWindow = win
                    , panelGC = gc
                    , panelPixmap = pixmap
                    , panelSurface = surface
                    , panelArea = rect
                    , panelScreenArea = screenRect
                    , panelWidgetStates = widgets
                    }

createPanelWindow :: Display -> Rectangle -> PhiX Window
createPanelWindow disp rect = do
  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


setPanelProperties :: Display -> PanelState -> PhiX ()
setPanelProperties disp panel = do
  atoms <- asks phiAtoms
  liftIO $ do
    storeName disp (panelWindow panel) "Phi"
    changeProperty8 disp (panelWindow panel) (atom_NET_WM_NAME atoms) (atomUTF8_STRING atoms) propModeReplace $ map (fromIntegral . ord) "Phi"
    
    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
                                                }
    changeProperty32 disp (panelWindow panel) (atom_MOTIF_WM_HINTS atoms) (atom_MOTIF_WM_HINTS atoms) propModeReplace [ 2, 0, 0, 0, 0 ]
    
    Util.setClassHint disp (panelWindow panel) ClassHint { resName = "phi", resClass = "Phi" }
    
  setStruts disp panel


setStruts :: Display -> PanelState -> PhiX ()
setStruts disp panel = do
  atoms <- asks phiAtoms
  config <- asks phiPanelConfig
  let rootwin = defaultRootWindow disp
      position = Panel.panelPosition config
      area = panelArea panel
  (_, _, _, _, rootHeight, _, _) <- liftIO $ getGeometry disp rootwin
  
  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
              Phi.Top -> makeTopStruts
              Phi.Bottom -> makeBottomStruts
  
  liftIO $ do
    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


panelBounds :: Panel.PanelConfig -> Rectangle -> Rectangle
panelBounds config screenBounds = case Panel.panelPosition config of
  Phi.Top -> screenBounds { rect_height = fromIntegral $ Panel.panelSize config }
  Phi.Bottom -> screenBounds { rect_height = fromIntegral $ Panel.panelSize config,
                                        rect_y = (rect_y screenBounds) + (fromIntegral $ rect_height screenBounds) - (fromIntegral $ Panel.panelSize config) }

withRectangle :: (Num x, Num y, Num w, Num h) => Rectangle -> (x -> y -> w -> h -> a) -> a
withRectangle r = withDimension r . withPosition r

withPosition :: (Num x, Num y) => Rectangle -> (x -> y -> a) -> a
withPosition r f = f (fromIntegral $ rect_x r) (fromIntegral $ rect_y r)

withDimension :: (Num w, Num h) => Rectangle -> (w -> h -> a) -> a
withDimension r f = f (fromIntegral $ rect_width r) (fromIntegral $ rect_height r)