Skip to content

Commit 084bf24

Browse files
committed
Embrace UTF-8.
1 parent 6c0846d commit 084bf24

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

OpenGLRaw.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,9 @@ library
602602
hs-source-dirs: src
603603
build-depends:
604604
base >= 4 && < 5,
605+
bytestring >= 0.9 && < 0.11,
605606
containers >= 0.3 && < 0.6,
607+
text >= 0.1 && < 1.3,
606608
transformers >= 0.2 && < 0.5
607609
default-language: Haskell2010
608610
ghc-options: -Wall

src/Graphics/Rendering/OpenGL/Raw/GetProcAddress.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ import Data.Functor( (<$>), (<$) )
3434
#endif
3535
import Control.Monad ( forM )
3636
import Control.Monad.IO.Class ( MonadIO(..) )
37+
import Data.ByteString.Unsafe ( unsafePackCString, unsafeUseAsCString )
3738
import Data.Char ( isDigit )
3839
import Data.Set ( Set, fromList )
39-
import Foreign.C.String ( CString, withCString, peekCString )
40+
import Data.Text ( pack, unpack )
41+
import Data.Text.Encoding ( encodeUtf8, decodeUtf8 )
42+
import Foreign.C.String ( CString )
4043
import Foreign.C.Types
4144
import Foreign.Marshal.Alloc ( alloca )
4245
import Foreign.Marshal.Error ( throwIf )
@@ -52,7 +55,7 @@ import Text.ParserCombinators.ReadP
5255
-- | Retrieve an OpenGL function by name. Returns 'nullFunPtr' when no function
5356
-- with the given name was found.
5457
getProcAddress :: MonadIO m => String -> m (FunPtr a)
55-
getProcAddress cmd = liftIO $ withCString cmd hs_OpenGLRaw_getProcAddress
58+
getProcAddress cmd = liftIO $ withUtf8String cmd hs_OpenGLRaw_getProcAddress
5659

5760
foreign import ccall unsafe "hs_OpenGLRaw_getProcAddress"
5861
hs_OpenGLRaw_getProcAddress :: CString -> IO (FunPtr a)
@@ -175,7 +178,7 @@ parseVersion = do
175178
makeGetString :: IO (GLenum -> IO String)
176179
makeGetString = do
177180
glGetString_ <- dynGLenumIOPtrGLubyte <$> getProcAddress "glGetString"
178-
return $ \name -> glGetString_ name >>= peekGLstring
181+
return $ \name -> glGetString_ name >>= peekUtf8String . castPtr
179182

180183
foreign import CALLCONV "dynamic" dynGLenumIOPtrGLubyte
181184
:: FunPtr (GLenum -> IO (Ptr GLubyte))
@@ -184,7 +187,7 @@ foreign import CALLCONV "dynamic" dynGLenumIOPtrGLubyte
184187
makeGetStringi :: IO (GLenum -> GLuint -> IO String)
185188
makeGetStringi = do
186189
glGetStringi_ <- dynGLenumGLuintIOPtrGLubyte <$> getProcAddress "glGetStringi"
187-
return $ \name index -> glGetStringi_ name index >>= peekGLstring
190+
return $ \name index -> glGetStringi_ name index >>= peekUtf8String . castPtr
188191

189192
foreign import CALLCONV "dynamic" dynGLenumGLuintIOPtrGLubyte
190193
:: FunPtr (GLenum -> GLuint -> IO (Ptr GLubyte))
@@ -199,9 +202,13 @@ foreign import CALLCONV "dynamic" dynGLenumPtrGLintIOVoid
199202
:: FunPtr (GLenum -> Ptr GLint -> IO ())
200203
-> GLenum -> Ptr GLint -> IO ()
201204

202-
-- TODO: We currently ignore that fact that OpenGL strings are UTF8-encoded.
203-
peekGLstring :: Ptr GLubyte -> IO String
204-
peekGLstring = peekCString . castPtr
205+
--------------------------------------------------------------------------------
206+
207+
withUtf8String :: String -> (CString -> IO a) -> IO a
208+
withUtf8String = unsafeUseAsCString . encodeUtf8 . pack . (++ "\0")
209+
210+
peekUtf8String :: CString -> IO String
211+
peekUtf8String p = unpack . decodeUtf8 <$> unsafePackCString p
205212

206213
--------------------------------------------------------------------------------
207214

0 commit comments

Comments
 (0)