[Vala] Using a C library
- From: Justin Brown <justin brown fandingo org>
- To: vala-list gnome org
- Subject: [Vala] Using a C library
- Date: Sat, 18 Apr 2015 13:22:50 -0500
Hello,
I'm just getting started with Vala, and I've run into a snag.
Unfortunately, even after crawling the internet for several hours trying to
find a solution, I can't seem to find any help. Sorry to ask such a basic
question.
I'm trying to wrap the proprietary libspotify C library. I've found this
guide (https://wiki.gnome.org/Projects/Vala/LegacyBindings), which is
really helping with wrapping types and methods. Combined with the Vala
tutorial, I've written a little bit of Vala code to start testing.
Unfortunately, I can't seem to find any information about how to compile my
program so far. I come from a Python background, so compiling and
including/importing other files is generally very simple, but I can't
figure out how to do it with Vala. I've even tried looking through C# and
Mono documentation, but that didn't seem useful.
My package manager has a libspotify package, so I installed it, which gives
me /usr/include/libspotify/api.h and /usr/lib64/libspotify.so. For my
program, I currently have two files fandingo.vala (main program) and
spotify.vala (classes for wrapping libspotify). They are both in the same
directory.
fandingo.vala is currently extremely simple and just has a class for DBus
communication (mostly cobbled together from a Vala DBus tutorial):
using Spotify;
[DBus (name = "org.fandingo.Fandingo")]
class Fandingo.fandingo : GLib.Object {
private int c;
public int ping(string m) {
stdout.printf("%s\n", m);
return c++;
}
public int ping_with_signal(string m) {
stdout.printf("%s\n", m);
return c++;
}
public void ping_err() throws GLib.Error {
throw new FandingoError.SOME_ERROR("Error encountered");
}
public int sp_error_code() {
Spotify.Error err = Spotify.Error.OK;
stdout.printf("Error: %s, %d\n", err.to_string(), err);
return err;
}
public signal void pong(int count, string msg);
}
[DBus (name = "org.fandingo.FandingoError")]
public errordomain FandingoError {
SOME_ERROR
}
void on_bus_acquired(DBusConnection conn) {
try {
conn.register_object("/org/fandingo/fandingo", new Fandingo.fandingo());
} catch (IOError e) {
stderr.printf("No registration\n");
}
}
void main() {
Bus.own_name (BusType.SESSION, "org.fandingo.Fandingo",
BusNameOwnerFlags.NONE, on_bus_acquired, () => {}, () => stderr.printf("No
name\n"));
new MainLoop().run();
}
spotify.vala is very simple as well, and just wraps the error enum and
method to get an error string:
namespace Spotify {
[CCode (cname = "sp_error", cprefix = "SP_ERROR_", has_type_id= false)]
public enum Error {
OK,
BAD_API_VERSION,
API_INITIALIZATION_FAILED,
TRACK_NOT_PLAYABLE,
// truncated
[CCode (cname = "sp_error_message")]
private static char* get_string(sp_error err);
public string to_string() {
return (string) Error.get_string(this);
}
}
}
I'm sure there are some programming errors, but I feel like I can work
through those if I can just figure out how to tell the compiler to use
libspotify/api.h.
When I just had fandingo.vala and was doing the DBus testing (without the
sp_error_code() method), compiling was easy:
valac --pkg gio-2.0 fandingo.vala
I've tried using
valac --pkg gio-2.0 fandingo.vala spotify.vala
But that gives me an error presumably because valac doesn't know how to
include api.h/libspotify.so.
spotify.vala:44.35-44.42: error: The type name `sp_error' could not be
found
private static char* get_string(sp_error err);
^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
I also tried adding a C-style include to spotify.vala: #include
<libspotify/api.h>, but that didn't work.
Again, sorry for the basic question, but I can't seem to figure this out.
Thanks,
Justin
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]