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

74 lines
2.2 KiB
Haskell
Raw Normal View History

2010-02-22 16:50:42 +01:00
module GLX ( glxDriver
) where
import GLDriver
import Bindings.GLX
import Control.Monad (when, unless)
import Data.Bits ((.|.))
import Graphics.X11.Types
import Graphics.X11.Xlib.Display (defaultScreen, openDisplay, rootWindow)
import Graphics.X11.Xlib.Event (allocaXEvent, nextEvent, get_Window, get_EventType)
import Graphics.X11.Xlib.Types
import Graphics.X11.Xlib.Window (mapWindow)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable
data GLX = GLX Bool
glxDriver :: GLX
glxDriver = GLX False
2010-02-22 18:27:18 +01:00
instance Driver GLX where
2010-02-22 16:50:42 +01:00
initialized (GLX inited) = inited
initGL (GLX inited) = do
when (inited) $ fail "GLX already initialized"
disp <- openDisplay ""
fbconfigs <- chooseFBConfig disp (fromIntegral . defaultScreen $ disp)
2010-02-22 18:27:18 +01:00
[(renderType, rgbaBit)
, (drawableType, windowBit)
2010-02-22 18:42:09 +01:00
, (xRenderable, 1)
2010-02-22 18:27:18 +01:00
, (depthSize, 1)
, (stencilSize, 1)
2010-02-22 16:50:42 +01:00
]
visualinfo <- getVisualFromFBConfig disp (head fbconfigs)
2010-02-22 18:27:18 +01:00
rootwindow <- rootWindow disp (fromIntegral $ viScreen visualinfo)
cmap <- createColormap disp rootwindow (viVisual visualinfo) allocNone
2010-02-22 16:50:42 +01:00
2010-02-22 18:27:18 +01:00
let swa = nullSetWindowAttributes {swaColormap = cmap, swaEventMask = structureNotifyMask .|. keyPressMask .|. keyReleaseMask}
2010-02-22 16:50:42 +01:00
2010-02-22 18:27:18 +01:00
wnd <- with swa $ \swaptr -> createWindow disp rootwindow 0 0 800 600 0 (fromIntegral $ viDepth visualinfo) inputOutput (viVisual visualinfo) (cWBorderPixel.|.cWColormap.|.cWEventMask) swaptr
2010-02-22 16:50:42 +01:00
mapWindow disp wnd
waitForMapNotify disp wnd
2010-02-22 18:27:18 +01:00
ctx <- with visualinfo $ \vi -> createContext disp vi (Context nullPtr) True
makeCurrent disp wnd ctx
2010-02-22 16:50:42 +01:00
return (GLX True)
deinitGL _ = return ()
nextEvent _ = return Nothing
waitForMapNotify :: Display -> Window -> IO ()
waitForMapNotify disp wnd = allocaXEvent waitForMapNotify'
where
waitForMapNotify' event = do
Graphics.X11.Xlib.Event.nextEvent disp event
window <- get_Window event
eventType <- get_EventType event
unless (window == wnd && eventType == mapNotify) $
waitForMapNotify' event