libgjs

About a year and a half ago (has it been that long already?), I blogged about wanting to write some gobjects. People did give me pointers to some documentation then, but for various reasons I never looked into it in detail afterwards.

Since yesterday, though, that's changed. For the last two days, I've been writing on something which I'll eventually release to the world as libgjs:

wouter@country:~/code/c/libgjs/src$ ./gjstest 
There are 1 joysticks on this system
Joystick 0 is a Logitech Inc. WingMan Gamepad Extreme,
	with 10 buttons and 4 axes.
wouter@country:~/code/c/libgjs/src$ 

Wonderful, isn't it? Obviously the above doesn't require the 306 LOC that I've written now (which isn't much, but that's mostly because I had to read a lot of documentation in between); but when finished, the API of that library will be approximately something like this:

if(gjs_joystick_get_max>=0) {
	GjsJoystick *joystick = gjs_joystick_new(0);
	if(joystick->buttons >= 1) {
		g_signal_connect(joystick, "button-pressed",
			start_firing, NULL);
		g_signal_connect(joystick, "button-released",
			stop_firing, NULL);
		g_signal_connect(joystick, "axis-moved",
			move_crosshair, NULL);
	}
	gjs_joystick_set_interval(1);
}

where start_firing, stop_firing and move_crosshair are, obviously, callback functions (they get the button or axis number as a parameter). The set_interval function sets the time between two polls of the kernel device file; this will use the glib mainloop, but it'll also be possible to use a different event loop with gjs_joystick_poll and/or gjs_joystick_poll_all.

As of now, it's almost finished; only the signals seem to be something I'm having issues with still. But that's only a matter of not having used gdb enough—I'm sure I'll get there.

What gave me the sudden idea to write this library? Something silly, really: for a project I'm currently negotiating, it might be nice if I knew how GObject works; and what better way to find that out than by writing something fun for it? Exactly.

Update: Bugs squashed, and the library (along with a small test/demo application) is available. Please try it out and let me know what you think.