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
|
module GLX ( glxDriver
) where
import GLDriver
import Bindings.GLX
import Control.Monad (when, unless)
import Data.Bits ((.|.))
import Data.Maybe (isJust)
import Graphics.X11.Types
import Graphics.X11.Xlib.Atom (internAtom)
import Graphics.X11.Xlib.Display (defaultScreen, openDisplay, rootWindow)
import Graphics.X11.Xlib.Event (XEventPtr, allocaXEvent, nextEvent, get_Window, get_EventType, pending)
import Graphics.X11.Xlib.Extras (ClassHint(..), getEvent, ev_event_type, ev_width, ev_height, ev_keycode, ev_data)
import Graphics.X11.Xlib.Misc (keycodeToKeysym, setWMProtocols)
import Graphics.X11.Xlib.Types
import Graphics.X11.Xlib.Window (destroyWindow, mapWindow, storeName)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable
data GLX = GLX
{ glxDisplay :: !Display
, glxWindow :: !Window
, glxContext :: !Context
, glxDeleteWindow :: !Atom
}
glxDriver :: GLX
glxDriver = GLX
{ glxDisplay = Display nullPtr
, glxWindow = 0
, glxContext = Context nullPtr
, glxDeleteWindow = 0
}
instance Driver GLX where
initialized glx = ((glxContext glx) /= (Context nullPtr))
initGL glx = do
when (initialized glx) $ fail "GLX already initialized"
disp <- openDisplay ""
delwnd <- internAtom disp "WM_DELETE_WINDOW" False
fbconfigs <- chooseFBConfig disp (fromIntegral . defaultScreen $ disp)
[(renderType, rgbaBit)
, (drawableType, windowBit)
, (xRenderable, 1)
, (depthSize, 1)
, (stencilSize, 1)
]
visualinfo <- getVisualFromFBConfig disp (head fbconfigs)
rootwindow <- rootWindow disp (fromIntegral $ viScreen visualinfo)
cmap <- createColormap disp rootwindow (viVisual visualinfo) allocNone
let swa = nullSetWindowAttributes {swaColormap = cmap, swaEventMask = structureNotifyMask .|. keyPressMask .|. keyReleaseMask}
wnd <- with swa $ \swaptr ->
createWindow disp rootwindow 0 0 800 600 0 (fromIntegral $ viDepth visualinfo) inputOutput (viVisual visualinfo) (cWBorderPixel.|.cWColormap.|.cWEventMask) swaptr
setClassHint disp wnd (ClassHint "HTanks" "htanks")
setWMProtocols disp wnd [delwnd]
storeName disp wnd "HTanks"
mapWindow disp wnd
waitForMapNotify disp wnd
ctx <- with visualinfo $ \vi -> createContext disp vi (Context nullPtr) True
makeCurrent disp wnd ctx
return GLX
{ glxDisplay = disp
, glxWindow = wnd
, glxContext = ctx
, glxDeleteWindow = delwnd
}
deinitGL glx = do
destroyWindow (glxDisplay glx) (glxWindow glx)
destroyContext (glxDisplay glx) (glxContext glx)
swapBuffers glx = Bindings.GLX.swapBuffers (glxDisplay glx) (glxWindow glx)
nextEvent glx = allocaXEvent $ nextEvent' glx $ glxDisplay glx
nextEvent' :: GLX -> Display -> XEventPtr -> IO (Maybe SomeEvent)
nextEvent' glx disp xevent = do
p <- pending disp
if (p > 0) then do
Graphics.X11.Xlib.Event.nextEvent disp xevent
ev <- handleEvent glx disp xevent
if isJust ev then
return ev
else
nextEvent' glx disp xevent
else
return Nothing
handleEvent :: GLX -> Display -> XEventPtr -> IO (Maybe SomeEvent)
handleEvent glx disp xevent = do
event <- getEvent xevent
let evtype = ev_event_type event
case () of
_ | evtype == configureNotify -> do
return $ Just $ SomeEvent $ ResizeEvent (fromIntegral . ev_width $ event) (fromIntegral . ev_height $ event)
| evtype == keyPress -> do
keysym <- keycodeToKeysym disp (ev_keycode event) 0
case () of
_ | keysym == xK_Escape -> return $ Just $ SomeEvent QuitEvent
| keysym == xK_Up -> return $ Just $ SomeEvent $ KeyPressEvent KeyUp
| keysym == xK_Down -> return $ Just $ SomeEvent $ KeyPressEvent KeyDown
| keysym == xK_Left -> return $ Just $ SomeEvent $ KeyPressEvent KeyLeft
| keysym == xK_Right -> return $ Just $ SomeEvent $ KeyPressEvent KeyRight
| otherwise -> return Nothing
| evtype == keyRelease -> do
keysym <- keycodeToKeysym disp (ev_keycode event) 0
case () of
_ | keysym == xK_Up -> return $ Just $ SomeEvent $ KeyReleaseEvent KeyUp
| keysym == xK_Down -> return $ Just $ SomeEvent $ KeyReleaseEvent KeyDown
| keysym == xK_Left -> return $ Just $ SomeEvent $ KeyReleaseEvent KeyLeft
| keysym == xK_Right -> return $ Just $ SomeEvent $ KeyReleaseEvent KeyRight
| otherwise -> return Nothing
| evtype == clientMessage -> do
if ((glxDeleteWindow glx) == (fromIntegral . head . ev_data $ event))
then
return $ Just $ SomeEvent QuitEvent
else
return Nothing
| otherwise -> 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
|