Re: [Vala] Adding a GIOChannel to GMainLoop



On Wed, 2007-10-24 at 16:11 +0200, Jürg Billeter wrote: 
On Wed, 2007-10-24 at 15:44 +0200, Xavier Bestel wrote:
Hi,

I have a GMainLoop (= new MainLoop(null, false)) and I wanted to add a
GIOChannel to that loop. In C I'd have written g_io_add_watch(), but I
can't find the equivalent in Vala. Could someone help me please ?

That's also still missing in the bindings.

This should work when added to the IOChannel class in glib-2.0.vapi:

[CCode (cname = "g_io_add_watch")]
public uint add_watch (IOCondition condition, IOFunc func, pointer
user_data);

Also add the following line after the IOChannel class:

public static delegate bool IOFunc (IOChannel source, IOCondition
condition, pointer data);

OK, I have a little problem here: 

my callback is declared like that:

        public bool data_received(IOChannel src, IOCondition cond, pointer data)
        {
                stdout.printf("data received");
        }

and connected like that:

                        ioc = IOChannel.unix_new(fd);
                        ioc.add_watch(IOCondition.IN, data_received, null);

but when called, it looks like "src" contains "cond", "cond" contains
"data" and "data" contains garbage.
Looking at the C code, the problem is that my callback is a method from
another object, so a "self" argument is inserted in the proto:

gboolean ml_connect_data_received (MLConnect* self, GIOChannel* src, GIOCondition cond, gpointer data) {
        g_return_val_if_fail (IS_ML_CONNECT (self), FALSE);
        fprintf (stdout, "data received");
}

Can I pass an object's method as a callback ? Looking at GIOFunc's
proto, the caller's "self" should probably be stored in "data" ...

        Xav





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