[Vala] Asynchronous construction



Hi,
trying to make the gfreenect [1] library work with Vala and in
particular to deal with the missing asynchronous constructor support I
was running into some issues. The vapi [2] is generated from GIR.

The corresponding code in C is pretty straight forward and would 
look like this:

static void on_new_kinect_device (      GObject      *obj,
                                        GAsyncResult *res,
                                        gpointer      user_data)
{
  kinect = gfreenect_device_new_finish (res, &error);
  if (kinect == NULL)
    {
      g_debug ("Failed to created kinect device: %s", error->message);
      g_error_free (error);
      clutter_main_quit ();
      return;
    }

  g_debug ("Kinect device created!");


int main (int argc, char *argv[])
{
  gfreenect_device_new (0,
                        GFREENECT_SUBDEVICE_CAMERA,
                        NULL,
                        on_new_kinect_device,
                        NULL);
   clutter_main ();

  return 0;
}

Trying to implement that in Vala, I took the approach below.
It seems however, that the init_async does not work. Looking at the
generated C-Code it looks like the g_async_initable_init_finish method 
is called right after init_async an not with in the callback as expected.
As a result the code is crashing in the underlying libusb due to 
incorrect initialization.

What am I missing here? Any idea how to solve the issue.

thanks and sorry for the lengthy post,

--tomw    

---------------------------------------------------------

using GLib;
using GFreenect;

public class Main : Object 
{

    public GFreenect.Device device;
    public Cancellable cancellable;   
        
    public Main () {
        Cancellable cancellable  = new Cancellable ();
        
    }
        
    public async void run (out Device dev) {
        debug ("Starting");
        Device device = (Device) GLib Object  new (
                                                typeof (Device),
                                                "index", 0,
                                                "subdevices", Subdevice.CAMERA
                                                );
        debug ("Device created");       
        Idle.add (run.callback);
        try {   
                debug ("Trying to run init_async");
                bool re = yield device.init_async (Priority.DEFAULT, cancellable);
                if (re) {
                        debug ("Result = OK");
                } else {
                        debug ("Result = FAILED");
                }
        } catch (Error e) {
                debug ("Error initalizing device : %s", e.message);
        }                             
  }
                                                        
   public void callback (Main? source, GLib.AsyncResult? res, void* user_data) {
        debug ("Entering Callback");
        Device dev;
        if (res != null) {
                source.run.end (res, out dev);
                debug ("Callback called - Device created");
                try {
                        Device kinect = new dev.finish(res);
                } catch (Error e) {
                        debug ("Error finalizing device : %s", e.message);
                }
        } else {
                debug ("No result delivered");
        }
   }   

   static int main (string[] args) {
        var loop = new MainLoop ();
        var app = new Main ();
        app.run.begin ((AsyncReadyCallback)callback);
        loop.run ();
                
        return 0;
   }

}
  


[1] https://gitorious.org/gfreenect
[2] http://pastebin.com/xdXKCmWR   




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