[Vala] Setting up a GLib main loop with custom Sources with Vala 0.11.7



For reference, I have a Glib main loop with custom Source instances
working now, in case anyone else needs to do it.  With the 0.11.7
compiler I had to make a number of work-arounds for compiler bugs and
limitations, and I had to hack my glib VAPI file.  It is not ideal but
it works (although I haven't checked for leaks yet).  Hopefully in
later versions these hacks won't be necessary.

Probably this isn't the best way of solving these problems, but since
I know no better ...


- Change SourcePrepareFunc in the VAPI to use 'int*' instead of 'out
  int' for the timeout, and change the calling code accordingly:

        [CCode (has_target = false)]
        public delegate bool SourcePrepareFunc (Source source, int* timeout_);

  This works around bug 645838.  Using 'out int' would be better in
  the long run.


- Add a constructor for SourceFuncs in the VAPI, and implement
  g_source_funcs_new and g_source_funcs_free in C:

        [Compact]
        public class SourceFuncs {
                public SourceFuncs() {}
                ...

  Separate C file:

    #include <glib.h>
    GSourceFuncs* g_source_funcs_new() {
       return g_slice_alloc0(sizeof(GSourceFuncs));
    }
    void g_source_funcs_free (GSourceFuncs* sf) {
       g_slice_free1(sizeof(GSourceFuncs), sf);
    }

  Note that GLib expects SourceFuncs to be static data, i.e. it
  doesn't copy it or do any ownership stuff.  So the caller has to
  make sure it stays around as long as the Source remains active.

  I haven't added a bug as maybe there is a way to get a static
  'SourceFuncs' that I haven't discovered yet.


- Pass a 'guessed' size value to the Source superclass constructor.
  The Source constructor wants to know the size of the object
  constructed.  The code 'sizeof(MySource)' gives the size of a
  pointer, not the size of the structure, so it doesn't seem possible
  to get this value in Vala.  So I pass 256 for now which is more than
  enough.

  Maybe the VAPI needs fixing here, but I don't know enough about
  that.  I haven't added a bug for this because I can't be 100% sure
  there isn't some trick to get this to work with the existing VAPI.


Jim

-- 
 Jim Peters                  (_)/=\~/_(_)                 jim uazu net
                          (_)  /=\  ~/_  (_)
 UazĂș                  (_)    /=\    ~/_    (_)                http://
 in Peru            (_) ____ /=\ ____ ~/_ ____ (_)            uazu.net



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