Skip to content

Generate Haskell bindings for GObject-Introspection capable libraries

License

Notifications You must be signed in to change notification settings

hardentoo/haskell-gi

 
 

Repository files navigation

haskell-gi

Generate Haskell bindings for GObject Introspection capable libraries.

Using the generated bindings

The most recent versions of the generated bindings are available from hackage. To install, start by making sure that you have a recent (1.24 or later) version of cabal-install:

$ cabal install cabal-install
$ cabal --version
cabal-install version 1.24.0.0
compiled using version 1.24.0.0 of the Cabal library 

Then install the bindings you need. For instance, for the gtk+ bindings you can do:

$ cabal install gi-gtk

(Note: you may need to run this command twice, due to a bug in cabal).

That's it! Here is an example "Hello World" program:

{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}

import qualified GI.Gtk as Gtk
import Data.GI.Base

main :: IO ()
main = do
  Gtk.init Nothing

  win <- new Gtk.Window [ #title := "Hi there" ]

  on win #destroy Gtk.mainQuit

  button <- new Gtk.Button [ #label := "Click me" ]

  on button #clicked (set button [ #sensitive := False,
                                   #label := "Thanks for clicking me" ])

  #add win button

  #showAll win

  Gtk.main

This program uses the new OverloadedLabels extension in GHC 8.0, so make sure you have a recent enough version of GHC installed. To run this program, copy it to a file (hello.hs, say), and then

$ ghc -o hello hello.hs
$ ./hello

For a more involved example, see for instance this WebKit example. Further documentation can be found in the Wiki.

Translating from the C API to the haskell-gi generated API

The translation from the original C API to haskell-gi is fairly straightforward: for method names simply remove the library prefix (gtk, gdk, etc.), and convert to camelCase. I.e. gtk_widget_show becomes widgetShow in the module GI.Gtk (provided by the gi-gtk package).

For properties, add the type of the object as a prefix: so the sensitive property of GtkWidget becomes widgetSensitive in gi-gtk. These can be set using the new syntax, as follows:

b <- new Button [widgetSensitive := True]

or using set after having created the button

b `set` [widgetSensitive := False]

Alternatively you can use setWidgetSensitive and friends to set properties individually if you don't like the list syntax.

Finally, for signals you want to use the onTypeSignalName functions, for example onButtonClicked:

onButtonClicked b $ do ...

This is the basic dictionary. Note that all the resulting symbols can be conveniently searched in hoogle.

There is also support for the OverloadedLabels extension in GHC 8.0 or higher. So the examples above can be shortened (by omitting the type that introduces the signal/property/method) to

b <- new Button [#sensitive := True]
on b #clicked $ do ...
#show b

Hopefully this helps to get started! For any further questions there is a gitter channel that may be helpful at https://gitter.im/haskell-gi/haskell-gi.

Binding to new libraries

It should be rather easy to generate bindings to any library with gobject-introspection support, see the examples in the bindings folder. Pull requests appreciated!

Build Status Join the chat at https://gitter.im/haskell-gi/haskell-gi

About

Generate Haskell bindings for GObject-Introspection capable libraries

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Haskell 97.7%
  • C 1.2%
  • Other 1.1%