Re: [Vala] Xlib bindings?



Michael B. Trausch wrote:
Also, a question regarding bindings:  Do they *have* to be in .vapi
files, or is it possible to create a class that does something like
C#/Mono/.NET's P/Invoke?

Hi,

you can define extern methods this way:

 [CCode (cheader_filename = "xy.h", lowercase_prefix = "")]
 private static extern int foo (string bar);

But I'd recommend writing a vapi file. You can already do a lot of
tricks with vapi files to make the bindings convenient to use in Vala,
like specifying the constructor and free methods or renaming methods.
For example:

------- xlib.vapi ---------
[CCode (cprefix="X", cheader_filename="X11/Xlib.h")]
namespace Xlib {

        [Compact]
        [CCode (cname = "Display", free_function = "XCloseDisplay")]
        public class Display {
                [CCode (cname = "XOpenDisplay")]
                public Display (string? display_name = null);
                [CCode (cname = "XDefaultScreen")]
                public int get_default_screen ();
        }
}
---------------------------

Example usage:

------- xtest.vala --------
using Xlib;

void main () {
        Display display = new Display ();
        int screen = display.get_default_screen ();
        stdout.printf ("default screen: %d\n", screen);
        // display closed by Vala
}
---------------------------

$ valac xtest.vala --pkg xlib --vapidir . -X -lX11

Add the '-C' compiler option if you want to have a look at the generated
C code.

You could still wrap it with your own classes if it's not convenient enough.


Regards,

Frederik



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