[Xcb] xhsb: X Haskell Binding

Jamey Sharp jamey at minilop.net
Tue Jan 10 23:18:28 PST 2006


I've put up a really early implementation of a Haskell binding to XCB.
It demonstrates that Haskell's lazy evaluation fits well with XCB's
latency hiding mechanism. You can issue a series of requests, and the
replies won't be waited for until your program needs them for some
computation.

Let's look at an example. This program will print the atom numbers for
WM_NAME and WM_PROTOCOLS, followed by the names of the first three fonts
that match the given pattern. The empty string ("") argument to
withConnection means to use the server specified by the DISPLAY
environment variable.

> module Main where
> import XCB
> import XProto
> main = withConnection "" $ \ conn screen -> do
>          a <- internAtom conn True "WM_NAME"
>          b <- internAtom conn True "WM_PROTOCOLS"
>          c <- listFontsWithInfo conn 3 "-misc-fixed-medium-r-normal-*"
>          print (internAtomAtom a)
>          print (internAtomAtom b)
>          mapM (print . name) c

Notice that the requests look like synchronous function calls -- that
is, this looks like Xlib-style programming. However, I've included some
tracing code that shows that three requests are sent to the X server,
and *then* the replies are waited for -- just like you would write in
the equivalent XCB program. Round-trip latency is hidden automatically.

In addition to automatic latency hiding, this library doesn't require
you to think about memory management. Replies are simply garbage
collected when they're no longer referenced.

Haskell's strings are counted, not null-terminated, so they fit with the
X protocol much more easily than C-style strings. You can use all of
Haskell's tricks to construct them, such as, for example, ['a'..'z'].

Finally, Haskell supports exceptions (of a sort) so there's no need for
error-handling code in a lot of places. If anything fails in the above
program, it will disconnect from the X server and then exit.

This stuff is available in the xhsb module of XCB CVS; you can browse it
on the web at
	http://cvs.freedesktop.org/xcb/xhsb/

The big to-do item is auto-generating support for the rest of the X
protocol using the XML-XCB protocol descriptions. I started trying to
write an XSLT stylesheet for it but that seems like a lot of work. Maybe
Josh will do it. ;-)

--Jamey
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.freedesktop.org/archives/xcb/attachments/20060110/20d069ec/attachment.pgp


More information about the Xcb mailing list