Re: [Vala] Using a C library



From: Justin Brown <justin brown fandingo org>
Cc: vala-list gnome org 
Sent: Wednesday, 22 April 2015, 7:05

This is the essence of what you are doing - creating an interface between your Vala code and the C code 
being generated.
It took me a while to appreciate and understand this statement. 
After getting past the initial woes, I was starting to try to turn the vapi 
into the full-fledged OOP bindings. 
You can even see that in my early code where I was putting a method into the enum.

Not sure that is a problem if it is relevant to the Vala API you are creating, e.g. 
https://git.gnome.org/browse/vala/tree/vapi/gdk-pixbuf-2.0.vapi#n245

It's clear to me now that I need to write a vapi that only maps the 
C functions/types/enums to Vala and do the user-friendly classes separately. 

Hmmm, not so sure of that. Vala is an OOP language, so on the Vala API side you probably 
want the interface to be using some kind of objects. I think the problem you are 
having is understanding how "objects" are translated into C. There are essentially
three implementations of objects in Vala - see 
https://wiki.gnome.org/Projects/Vala/Manual/Classes#Types_of_class

The first implementation is probably the one you are used to as a Vala programmer.
This type makes use of a C library, GLib, and its implementation of obejcts in C, 
GObject. The Vala syntax around GObjects gives you nice things like signals (an
implementation of the observer pattern) and properties (so my_object.property += 1 
just works without having to get and then set the value in two lines of code).

The second implementation just uses GLib's type system, GType. 

The third implementation is the one you want to be looking at for a custom
C binding. You will see the use of [Compact] before the class keyword in a
lot of the VAPIs. Looking at the Spotify API documentation ( 
https://developer.spotify.com/docs/libspotify/12.1.51/ ) the general design section talks of reference 
counting so take a look at:
https://wiki.gnome.org/Projects/Vala/LegacyBindings#Reference-counted_Compact_Classes
The Spotify API documentation also talks of asynchronous callbacks. That is beyond
my experience, apart from callbacks should probably mapped as delegates in the VAPI.


Also, thanks for the info pkg-config. I'm not at my dev. machine right now, and 
I think ultimately, I'm going to take the same approach of writing it myself, 
so I can -- at least attempt -- to understand what's happening.

pkg-config is the standard and after discussing it on this list I think taking
advantage of it would be the way to go, but the choice is yours. If you decide
to release your work then it is easy to change it to use the VAPI conventions.

Any idea why the Vala enum isn't pulling the values specified in libspotfiy/api.h? 

Only any idea, but try specifying the type as an enum:
cname = "enum sp_error"
as used by 
https://gitorious.org/spotifyd/spotifyd/source/a3487ee9c56b816c120ad41855c02ad3471d1021:libspotify.vapi#L30

Lastly, a question about gobject introspection...Back to the original question, 
is that first part (vapi + OOP library) applicable to use by for GObject introspection 
or would they other programs need to interface with the DBus service?

My understanding of GObject introspection is that it is a set of programming conventions
used by an author of a library that allows an automated tool to generate a language binding.
So if libspotify had been written using it then you wouldn't need to write your own Vala
API binding, the tool would be able to automatically generate it.


D-Bus is for inter process communication. I thought MPRIS was the standard D-Bus way 

of controlling a media stream: 
http://specifications.freedesktop.org/mpris-spec/latest/
If you are looking to write a program in Vala to use spotify and then control that
program with a nice GUI media player then I would go with D-Bus MPRIS.
Although a quick search shows that a Spotify MPRIS D-Bus interface already exists:
http://www.frandieguez.com/blog/2010/11/playing-with-d-bus-interface-of-spotify-for-linux/


Reading back over my last mail, thanks for not taking that too severely. 
I was banging my head against the wall and a little frustrated. I'm extremely 

thankful for everyone's help and guidance.

Your list of points pretty much reflected my reaction the first time I encountered linking :-)
That's probably why people wrote pkg-config.

Writing a VAPI to my mind is a pretty advanced undertaking. You need to know Vala, but also 

how Vala translates in to C. You say you come from a Python background, writing a VAPI is
equivalent to writing something like https://github.com/mopidy/pyspotify/tree/v2.x/develop/spotify


Good luck with it,

Al


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]