Re: [Vala] help with writting vapi...

2015-11-23 Thread Andre Masella
I would extend this idea to make it a little easier to write for the user:

Create a binding as follows:

[SimpleType]
[CCode(cname = "struct timeval")]
private struct TimeVal {
[CCode(cname = "*")]
public static TimeVal(Posix.timeval v);
}

private Redis.Context redisConnectWithTimeout(string ip, int port, TimeVal
tv);
public Redis.Context connect_with_timeout(string ip, int port,
Posix.timeval tv) {
  return redisConnectWithTimeout(ip, port, TimeVal(tv));
}

This will pass the time val as a pointer and then create a second struct
that is really just a dereference operation.

I don't know if there's an easy way to use GLib.TimeVal instead of
Posix.timeval or if they are guaranteed to be compatible of every machine.

On 22 November 2015 at 21:01, Evan Nemerson <e...@coeus-group.com> wrote:

> On Sun, 2015-11-22 at 16:07 +0800, Matrix wrote:
> > Hi :
> >
> > i'm writting hiredis vala's binding..
> >
> > here is a function like this:
> >
> > redisContext *redisConnectWithTimeout(const char *ip, int port, const
> > struct timeval tv);
> >
> > so, how to tell the valac that the 3rd param is struct value not
> > struct
> > pointer?
>
> Unfortunately, this is done on a per-type basis, and virtually everyone
> passes struct timeval as a pointer so that's what the bindings assume.
> Before seeing this function, I don't know that I've ever seen it passed
> as anything but a pointer.
>
> AFAIK the only way to do this would be to create a local binding for
> struct timeval (in the redis vapi) with a [SimpleType] annotation on
> it, and have redis use that type instead of the one in glib-2.0.
>
> FWIW it might be possible to add an annotation to allow for passing a
> non-SimpleType struct as a value.  I don't spend much time in the
> compiler, but I believe a small change to
> valaccodemethodcallmodule.vala would do the trick.
>
>
> -Evan
> _______
> vala-list mailing list
> vala-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/vala-list
>
>


-- 
--Andre Masella<an...@masella.name>
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] conversion segfault

2015-02-06 Thread Andre Masella
If you look at the generated code, you will see:

_tmp1_ = g_strconcat (foobar, (const gchar*) test, NULL);

for line 4, which is not what you want. When you use an explicit cast on
any type other than a GObject, Vala doesn't know how to do a conversion, so
it does whatever conversion C would do. That usually means converting
integers from one type to another, but it can also mean converting an
integer to a pointer, as in this case.

If you do:
string x = foobar + x.to_string();

it will compile to:

test = (guint64) 1;
_tmp1_ = g_strdup_printf (% G_GUINT64_FORMAT, test);
_tmp2_ = _tmp1_;
_tmp3_ = g_strconcat (foobar, _tmp2_, NULL);

Which is probably what you want when using the + operator.

If you use an interpolated string:

 string x = @foobar$(test);

You will get the same code as Vala will automatically call `to_string` on
any non-strings that are embedded.

The reason the direct cast behaviour is needed is there are legitimately
non-string things that are strings. For instance, if you have an array of
uint8[], a direct cast is desirable.

uint8 data[12];
data[0] = 'a';
data[1] = '\0';
string x = foobar + (string) data; // Just fine and exactly what I
intended.

I agree it's not what a typical VM language would do, but Vala is trying to
have C compatibility.

On 6 February 2015 at 06:01, Daniel Brendle grindh...@skarphed.org wrote:

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Hello, fellow Valaists

 I have the following example code which segfaults:

 public int main(string[] argv){
 stdout.printf(hello world\n);
 uint64 test = 1;
 string x = foobar+(string)test;
 stdout.printf(x+\n);
 return  0;
 }

 compiled with my valac 0.26.1, the result segfaults in line 4.
 I can't imagine, this is intended behaviour.

 Of course, the proper way to go for line 4 would be something like

 string x = foobar%lld.printf(test);

 Nevertheless i wonder, why the code segfaults, shouldn't there rather
 be a type conversion error at compile time?

 Sorry, i don't have a more recent 0.27 or 0.26.2 compiler at hand.


 TIA, Grindhold
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1

 iQIcBAEBAgAGBQJU1J8SAAoJEBuqgJs6izSE+JMQAJApxYTnuGmriXvbR5KLOAcv
 ZTzJ1PbUKuGs76EewJYOS5lPX997y4DRBHPTCy7cALwaAhpNcKYAI+jb6QisUbP0
 Fu5JLRFv7xjoTC560r3yuRNPRgeMTd9esE6siGTm1oMB35buxt1XOQ9bz1iZ28n2
 vnFhaZZ3paOFBpAwVyn6lyNGmWnjquAfCc0Km1h/w94PGgFf3wAqSQugiG9dPG9U
 afOiDG1R8JFLpdQEEHCZfSd6ohq9CPjBIgncHKuRxsrfFRkmJ6CuyMdMKvzqvXeq
 PgqIQ2RiAm76lVFMH4XCfsfhupVz8oDNACqaI1AEnKNRizdWBi8kqsuBZBJtlmz6
 mJC2fim8ZIDUJo75/aJnUPTHFKrHFp307kkkp5AW4ihGQtn8Ef4q9Z0aLcAO4sSC
 vGSdnG3hysrlUxLwozoIx1cv47IHJ+A/Nn8tO//riFsrwa821dh2wioWVdq58e7b
 T5wQ+Q+nsb21LoQpd+bw2vDlRhmjJcAWXoXG/6YFPIU1zi2n9jTZ3/75m4btG6P1
 sS3L99w22KldN1JDlCCWkH+6bnrQfwOP0YArQbtP7zVWEZCmKtH3ncXrAdrkjhIp
 hcHAzgtsrH2fggQ9VGPMGrVVz5ALTxOMt2qUKtXSoC4LtlNDVFEXAtxvYIj5REjD
 L2rx0BsJxwRq+OQfRsal
 =H26t
 -END PGP SIGNATURE-
 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] I found a major bug

2014-12-19 Thread Andre Masella
Mmm, no you didn't. :-)

Gdk.Allocation allocation;
this.get_allocation(out allocation);

This is admittedly more awkward and Gtk.Widget.get_allocation has the right
type signature in C to return, but this binding works too.

On 19 December 2014 at 10:27, Flavio Danesse fdane...@gmail.com wrote:

 *Hi, I just wanted to comment that I found a major bug in gtk:*

 error: invocation of void method not allowed as expression
 Gdk.Rectangle rect = this.get_allocation();
  ^
 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list



-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] Writing bindings as legacy

2014-11-23 Thread Andre Masella
Why not something like this:

[CCode (cheader_filename = rados/librados.h, cname = rados_create2)]
public static int from_config (out ClusterClient? client, string?
cluster_name, string? user_name, Flags flags);

Vala will ensure `client` is null before calling this function, if the
result is a failure, `client` will be null. It's owned, so the
`rados_shutdown` will be called. If you want it to look like a return:

[CCode (cname=g_io_error_from_errno)]
private int _g_io_error_from_errno(int errno);
[CCode (cheader_filename = rados/librados.h, cname = rados_create2)]
private static int _from_config (out ClusterClient? client, string?
cluster_name, string? user_name, Flags flags);
public static ClusterClient? from_config(string? cluster_name, string?
user_name, Flags flags) throw GLib.IOError {
  ClusterClient? result;
  int code;
  if ((code = _from_config(out result, cluster_name, user_name, flags)) 
0) {
throw new Error(IOError.quark(), _g_io_error_from_errno(-code), %s,
GLib.strerror(-code));
  }
  return (owned) result;
}

This requires depending on gio-2.0.

Also, Flags should probably be an enum.

On 22 November 2014 at 18:01, Gonzalo Aguilar Delgado 
gagui...@aguilardelgado.com wrote:

 Hi,

 I my case (rados lib) I found non standard anyway and trying hard to
 figure out how to do the binding. Maybe someone can help.

 I managed to create some kind of working stuff but I think is not really
 good with lot's of variable copying in generated code.

 [ The problem ]

 Rados want's a (void *) (formally rados_t) to be initialized on startup.
 This is done with rados_create2 function:

 int rados_create2(rados_t *pcluster, const char *const clustername,
   const char * const name, uint64_t flags);

 What I did is:

 Map flags: (ok)

 [SimpleType]
 [IntegerType (rank = 11)]
 [CCode (cname = uint64_t, has_type_id = false)]
 publicstruct Flags {
 }


 Create a cluster client:


 [CCode (cheader_filename = rados/librados.h, cname = void,
 free_function = rados_shutdown, has_type_id = false)]
 [Compact]
 publicclass ClusterClient {
 [CCode (cheader_filename = rados/librados.h, cname =
 rados_create2)]
 protectedstaticint create (ref unowned ClusterClient client, string?
 cluster_name, string? user_name, Flags flags);

 publicstatic unowned ClusterClient? from_config(string? cluster_name,
 string? user_name, Flags flags) {
 unowned ClusterClient result = null;
 if(create(ref result, cluster_name, user_name, flags)0){
 result = null;
 }
 return result;
 }


 }

 I code I do:

 Rados.Flags flags=0;
 unowned Rados.ClusterClient cluster = 
 Rados.ClusterClient.from_config(ceph,
 client.admin, flags);



 But this makes a lot of problems in code. And of course, not calling
 rados_shutdown because unowned.

 What I want is to do it in a way it's everything handled in the
 constructor with no static method, but don't find how to do it.

 What I really want to do is:

 Rados.Flags flags=0;
 Rados.ClusterClient cluster = Rados.ClusterClient(ceph, client.admin,
 flags);

 And the result of the rados_create2 (first argument) being copied to
 cluster var as instance object.

 How should I do it?

 Any help will be appreciated.


 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] Export vapi from libray

2014-11-21 Thread Andre Masella
If this is a shared library, it's not generally desirable to do so. What if
the user wants to use both your library and the library you have
re-exported? This will cause conflicts if there are any changes between the
two versions.

More likely, you want to declare dependencies. This is done using a `.deps`
file of the same name as your `.vapi` file. It lists the other packages on
which you depend. The Vala compiler will automatically import them as if
they were specified with the `--pkg` flag. There are plenty in
`/usr/share/vala-*/vapi/*.deps`. For example, `webkit2gtk-4.0.deps` has the
contents:

gtk+-3.0
libsoup-2.4

If the library is static, then there really isn't a good mechanism. Any
functions declared `public extern` in `.vala` will be exported, so you can
migrate some of the VAPI to `.vala` files. You could also `cat` the two
VAPIs together. This is pretty bad. I would probably convert the static
library to shared and then use the `.deps` mechanism instead.

On 21 November 2014 07:07, Andy Lees andrewl...@gmail.com wrote:

 Hi,

 I would like to export an interface to some C structures from a vala
 library that provides a number of other classes, etc.  If I include the
 vapi and associated .h file in the library, it compiles fine, but does not
 export the contents of the referenced vapi in the library vapi.

 How would one go about including the vapi contents in the library
 interface?

 Thanks for your attention.
 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] enum in a subclass

2014-11-07 Thread Andre Masella
1) You can't use `override` for methods in interfaces, only for overriding
base classes.

2) The contents of `foo` are not in scope for `bar`, only they inherited
members because they are now part of `bar`. So `Type` in `bar` is
`GLib.Type` not `foo.Type`.

public interface foo
{
public enum Type
{
a, b
}

public abstract Type get_type();
}

public class bar : foo
{
 foo.Type t = foo.Type.a;
 public new foo.Type get_type() { return t; }
}

3) You *really* *really* *really* don't want to name this method `get_type`
and you also don't want to name anything `Type` as these are used
underneath by GLib. The “fixed” version above will generate the following
broken C:

GType foo_get_type (void) G_GNUC_CONST;
fooType foo_get_type (foo* self);
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] Help about write vapi file with c libray

2014-09-26 Thread Andre Masella
Since it can be null if the open fails, it would be best as “out Port?
port”.

On 26 September 2014 10:48, Luca Bruno lethalma...@gmail.com wrote:

 On 26/09/2014 16:44, Matrix wrote:
  在 2014/9/26 22:19, Luca Bruno 写道:
  On 26/09/2014 16:09, Matrix wrote:
  Hi:
 
  I'm try using Vala to write a GUI serialport tool with libserialport.
 
  I have already have a demo with pure c implenmention.
 
  I'm still working with write vapi file to this lib follow the tutorial
  at https://wiki.gnome.org/Projects/Vala/LegacyBindings
 
  but the c struct is init by this form:
 
  enum sp_return sp_get_port_by_name(const char *portname, struct
  sp_port **port_ptr);
 
  how do i to write for such form ?
  class Port {
 [CCode (cname = sp_get_port_by_name)]
 static Return get_port_by_name (string portname, out Port);
  }
 
  Then:
 
  Port port;
  Port.get_port_by_name (foo, out port);
  ___
  vala-list mailing list
  vala-list@gnome.org
  https://mail.gnome.org/mailman/listinfo/vala-list
 
  Thank you, Luca Bruno!
 
  I follow your help, but have error
 
  there is my vapi file snippet:
 
  /*
  using GLib;
 
  namespace SerialPort {
  [CCode (cname = int, cprefix = SP_, has_type_id = false)]
  public enum Return {
  OK,
  ERR_ARG,
  ERR_FAIL,
  ERR_MEM,
  ERR_SUPP
  }
 
  [CCode (cname = struct sp_port, has_type_id = false)]
  public class Port {
  [CCode (cname = sp_get_port_by_name, has_type_id = false)]
  static Return get_port_by_name(string name, out Port);
  }
  }
  /***
 
  $ valac -C auto_test.vala --pkg libserialport
  Compilation failed: 1 error(s), 0 warning(s)
  libserialport.vapi:16.61-16.61: error: syntax error, expected identifier
  static Return open_get_by_name(string name, out Port);
 
 Of course, out Port port :)
 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] trying vapi file for ngspice, segfaults

2014-05-22 Thread Andre Masella
The test method can't have a type argument:

public static int test(string stdout,int id, int data){
stdout.printf (stdout: '%d'\n, data); //debug line
return id;
}


On 22 May 2014 11:12, Steven Vanden Branden 
stevenvandenbrandenst...@gmail.com wrote:

 vala-list-requ...@gnome.org schreef op wo 21-05-2014 om 12:00 [+]:
  I don't know why you think these are out parameters?based on a quick
  look at the documentation they seem like in parameters.

 i am wondering why i was thinking it. It almost works but i got a
 problem when trying to use the init function and passing a test function
 as the delegate.

 i used the vapi you proposed.

 [CCode (cname = SendChar, simple_generics= true, has_target = false)]
 public delegate int SendOutputT (string sendToCaller,
 int  id, T data);

 [CCode (cname = ngSpice_Init,simple_generics = true)]
 public static int initT(SendOutputT? a,
 SendSimulationStatusT? b, ControlledExitT? c,  SendVectorDataT? d,
  SendInitializationDataT? e,IsBackgroundThreadRunningT? f, T data);


 but on compile i get this:

 gint elektro_sim_ng_spice_simulator_test (GType t_type, GBoxedCopyFunc
 t_dup_func, GDestroyNotify t_destroy_func, const gchar* stdout, gint id,
 gconstpointer data);

 and here it obviously fails

 static gint _elektro_sim_ng_spice_simulator_test_send_char (const gchar*
 sendToCaller, gint id, gconstpointer data) {
 gint result;
 result = elektro_sim_ng_spice_simulator_test (sendToCaller, id,
 data);
 #line 41
 /home/steven/projecten/ElektroSimgit/ElektroSim/NGSpiceSimulator.vala
 return result;
 #line 103 NGSpiceSimulator.c
 }

 the code i use in vala is this(in constructor off a class:


 ngspice.initint(this.test, null, null, null, null, null,5);

 public static int testT(string stdout,int id, T data){
 stdout.printf (stdout: '%s'\n, stdout); //debug line
 return id;
 }

 any thoughts? i tried to remove the static but then it wont even complete
 the vala compile, ccode or vapi changes affect the GBoxedCopyFunc,
 GDestroyNotify and const gchar* stdout who are added
 to the test function in c, Or is is the send char static function that is
 not correct?

 greets,
 Steven

 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] VAPI tutorial

2013-12-27 Thread Andre Masella
Probably best to link to the legacy VAPI guide:
https://wiki.gnome.org/Projects/Vala/LegacyBindings


On 27 December 2013 15:43, rastersoft ras...@rastersoft.com wrote:

 Hi all:

 I wrote a little VAPI writing tutorial to help people to start writing
 VAPI files for libraries. I put it at

 https://wiki.gnome.org/Projects/Vala/WrittingVAPIs

 Of course it is incomplete, and is possible that it has mistakes, so I
 accept all kind of suggestions and fixes.

 --
 Nos leemos
  RASTER(Linux user #228804)
 ras...@rastersoft.com  http://www.rastersoft.com

 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] passing string length in VAPI file

2013-12-06 Thread Andre Masella
An intermediate function is the typical way to do it. You can also bind it
as `uint8[]` array and pass `string.data`. If the inner function is marked
`private` it won't appear in the generated ValaDoc. There's lots of this in
`glib-2.0.vapi`.


On 6 December 2013 07:38, rastersoft ras...@rastersoft.com wrote:

 Hi all:

 I'm completing the XCB api for Vala, but I have a problem: there are
 several functions that receive a string and its length, like:

 xcb_intern_atom (xcb_connection_t *_conn_, uint8_t _only_if_exists_,
 uint16_t _name_len_, const char *_name_);

 Currently I'm doing it this way, by defining an intermediate function:

 [CCode (cname = xcb_intern_atom)]
 public InternAtomCookie *l_intern_atom(bool only_if_exists,uint16
 len, string name);

 [CCode (cname = vala_xcb_intern_atom)]
 public InternAtomCookie *intern_atom(bool only_if_exists,string
 name) {
 this.l_intern_atom(only_if_exists,(uint16)name.length, name);
 }

 But it's not very elegant. Is there a way of doing this directly in a VAPI
 file?

 I tried with array_length_pos, but seems to not work in this case.

 Thanks.

 --
 Nos leemos
  RASTER(Linux user #228804)
 ras...@rastersoft.com  http://www.rastersoft.com

 ___
 vala-list mailing list
 vala-list@gnome.org
 https://mail.gnome.org/mailman/listinfo/vala-list




-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] Writing Bindings for Legacy C Libraries

2013-09-29 Thread Andre Masella
On 29 September 2013 01:45, Luca Bruno lethalma...@gmail.com wrote:

 Awesome! That was really a missing part of the docs. Thanks for your work,
 you have written so much and so good.


Thanks. I'm happy to contribute.


 A couple of things:
 The copy_function shouldn't be of any uses for classes (compact and not),
 if I'm not wrong.


Done.


 Vala does support multidimensional arrays very well: int[,] foo = new
 int[3,4];
 What vala does not support is stacked arrays.


Clarified. → Vala does not really do C-style stacked arrays (a.k.a. ragged
multi-dimensional arrays), so binding them is nigh impossible without extra
C code.


 Also did you link this page from some other wiki pages? Which ones?


I put a link in the Vala/UpstreamGuide#Writing_By_Hand and Vala/Bindings,
next to the Upstream Guide link.
-- 
--Andre Masellaan...@masella.name
http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


[Vala] Writing Bindings for Legacy C Libraries

2013-09-28 Thread Andre Masella
After some discussion with nemequ, I've put together some documentation
about writing bindings for non-GLib C code.

http://wiki.gnome.org/Vala/LegacyBindings

I'm sure there are many other situations that need to be covered, or
alternatives to the ones proposed; please expand or let me know and I'm
happy to write more. Thank you.
-- 
--Andre 
Masellaan...@masella.namehttps://mail.google.com/mail/?view=cmfs=1tf=1to=an...@masella.name

http://www.masella.name/
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list