I just learned about vapigen to build a Vala .vapi interface from gobject introspection. Unfortunately it seems that through the way of g-ir-scanner some information gets lost and gir cannot transmit information such as the semantics of arrays (null-terminated or with length, etc.). I played with a “metadata” file for an hour (as described upstream), but it seems to be ignored entirely.
So for now I committed a manually adjusted vapi for gudev. This now makes it easy to write code that queries and listens to udev in Vala.
Small example:
using GUdev;
void
print_device(GUdev.Device d)
{
stdout.printf("%s → %s\n", d.get_device_file(), d.get_sysfs_path());
foreach (string s in d.get_device_file_symlinks())
stdout.printf(" link: %s\n", s);
}
void
on_uevent(GUdev.Client client, string action, GUdev.Device dev)
{
stdout.printf("[%s] ", action);
print_device(dev);
}
int main(string[] args)
{
var uc = new GUdev.Client({"usb"});
print_device(uc.query_by_device_file(args[1]));
stdout.printf("---- all block devices ---\n");
GLib.List devs = uc.query_by_subsystem("block");
foreach (GUdev.Device d in devs)
print_device(d);
stdout.printf("---- usb events ---\n");
uc.uevent.connect(on_uevent);
new GLib.MainLoop().run();
return 0;
}
Build with valac --pkg gudev-1.0 udev.vala, and perhaps specify --vapidir if you keep the gudev-1.0.vapi file somewhere locally.
Update: I reverted the commit upstream for now, since Vala 0.8 already ships a gudev vapi. I must have overlooked that when I played with vapigen.. In the long run it’s probably better to generate vapis in the projects themselves to avoid API skew, but as long as the vapi can’t be generated automatically it does not make sense to have it in udev. Above code was updated for the vala provided one (which is lacking a return type specification for query_by_subsystem()).
#1 by Bruno Girin on 2010/06/13 - 01:30
Zitieren
How does it differ from the gudev-1.0.vapi that ships with Vala 0.8 (and therefore Ubuntu Lucid)?
#2 by pitti on 2010/06/13 - 10:09
Zitieren
Ah, good point. I guess I just overlooked it while I was playing with vapigen, and gudev came to my mind first.
I reverted the udev commit upstream for now.
However, in the long term it would seem more sensible to me to generate/ship vapis by the actual projects instead of in vala, otherwise you can easily get API skew.
#3 by jari on 2010/06/13 - 11:43
Zitieren
You are using the List in a wrong way. You didn’t give a type for the List. This will not compile (at least it does not with vala-0.9.1).
The line shoould be
GLib.List devs = uc.query_by_subsystem(“block”);
Regards
#4 by jari on 2010/06/13 - 11:43
Zitieren
GLib.List devs = uc.query_by_subsystem(“block”);
#5 by jari on 2010/06/13 - 11:44
Zitieren
Interesting. The blog is filtering out the List type from my comment.
#6 by Bruno Girin on 2010/06/13 - 12:56
Zitieren
I tend to have the opposite view for the following reasons:
Providing mappings can be quite a bit of extra work for a project. What languages do they provide mappings for? Python, Vala, Java? Then that project needs developers who actually use the target language regularly, otherwise they are likely to provide a poor mapping that doesn’t integrate well with the language’s standard API. They also need experience in the target language to test their mappings.
On the other hand, if the mapping is done by the people designing the language, they can ensure that the resulting API integrates well and makes full use of the language’s abilities. But you’re right: they may not be interested (or even know about) a certain library then leading to API skew.
From the developer’s point of view, I generally prefer an API that is provided by the language because it then means I have access to all the documentation in the same place. It also gives me more confidence that the mapping was done carefully and that using that API won’t result in ugly memory leaks or other issues.
To be pragmatic, I think the key is to find out who benefits more in having the mapping available, and therefore who is more willing to dedicate resources in writing, testing and supporting the code. In the case of gudev and Vala, I would say that Vala has more to gain from the mapping because it adds an essential API to the language. In the case of a specialised library having, say, a Python mapping, the project will have more interest in writing the mapping because it will then make their library available to a very large group of developers.
#7 by pitti on 2010/06/13 - 14:04
Zitieren
jari, I had the data type fixed in my version of the vapi, it’s just wrong in the one shipped by vala 0.8.1. But it was fixed yesterday in trunk:
http://git.gnome.org/browse/vala/commit/?id=4ad749b7