Hi,<br><br><div class="gmail_quote">2012/2/5 Álvaro Castro-Castilla <span dir="ltr"><<a href="mailto:alvaro.castro.castilla@gmail.com">alvaro.castro.castilla@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi Mikael,<div><br></div><div>This is the solution that seems to work, wrapping it. </div><div><div><br></div><div>(define cairo-create-surface</div><div>  (c-lambda (unsigned-char* int int int int)</div><div>            cairo-surface-t* </div>



<div>            "</div><div>            cairo_format_t format;</div><div>            switch (___arg2) {</div><div>              case -1: format = CAIRO_FORMAT_INVALID; break;</div><div>              case 0: format = CAIRO_FORMAT_ARGB32; break;</div>



<div>              case 1: format = CAIRO_FORMAT_RGB24; break;</div><div>              case 2: format = CAIRO_FORMAT_A8; break;</div><div>              case 3: format = CAIRO_FORMAT_A1; break;</div><div>              case 4: format = CAIRO_FORMAT_RGB16_565; break;</div>



<div>              default: format = CAIRO_FORMAT_RGB24; break;</div><div>            }</div><div>            ___result = cairo_image_surface_create_for_data(___arg1, format, ___arg3, ___arg4, ___arg5);</div><div>            "))</div>



</div></blockquote><div><br>Indeed that works - and in Scheme you just make corresponding defines, i.e. (define cairo-format-rgb24 1) etc. Normally this translation you do is not beneeded as the constants can be used in themselves, which normally works as in the example below:<br>

 </div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div></div><div></div><div>It is rather inconvenient, though. I think there should be a better way. Also, this crashes Gambit, just per se:</div>

<div>(define CAIRO_FORMAT_RGB24 ((c-lambda () cairo-format-t "___result = CAIRO_FORMAT_RGB24;")))</div></blockquote><div><br>Umm.. uhu. What is cairo_format_t:s type declaration in C?<br><br>Anyhow you got it working now.<br>

<br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div><br></div><div><br></div><div>Best regards,</div><div><br></div><font color="#888888"><div>Álvaro</div></font><div><div></div><div class="h5"><div><br></div><div><br></div><div><br><br><div class="gmail_quote">2012/2/5 Mikael <span dir="ltr"><<a href="mailto:mikael.rcv@gmail.com" target="_blank">mikael.rcv@gmail.com</a>></span><br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, do something like this: In cairo-image-surface-create-for-data, do printf("Wnt %i got %i\n",CAIRO_FORMAT_RGB24,___arg2); and rerun your previous tests, do they make more sense now? In case the both values printed are the same, your sigsegvcomes from elsewhere. Brgds<br>





<br><div class="gmail_quote"><div>2012/2/5 Álvaro Castro-Castilla <span dir="ltr"><<a href="mailto:alvaro.castro.castilla@gmail.com" target="_blank">alvaro.castro.castilla@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





hi Mikael,<div><br></div><div>Thanks for your response.</div><div>Unfortunately, your solution also breaks. Actually, it is doing more or less the same I did. What you say should be used like this, right?:</div><div><div>





(image-surface (cairo-image-surface-create-for-data</div>

<div>                         (SDL::surface-pixels sdl-surface)</div><div>                         (CAIRO_FORMAT_RGB24)</div><div>                         maxx</div><div>                         maxy</div><div>                         (SDL::screen-pitch sdl-surface)))</div>





</div></blockquote></div><div><br>In case you have (define CAIRO_FORMAT_RGB24 ((c-lambda () cairo-format-t "___result = CAIRO_FORMAT_RGB24;"))  already, you should type only  CAIRO_FORMAT_RGB24 here not  (CAIRO_FORMAT_RGB24).<br>





 </div><div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>

<div></div><div>What I was proposing just decides the output at runtime (the argument is an int, but passed to a function that converts it into a enum -should convert-):</div><div>(image-surface (cairo-image-surface-create-for-data</div>







<div><div>                         (SDL::surface-pixels sdl-surface)</div><div>                         (cairo-format CAIRO_FORMAT_RGB24)</div><div>                         maxx</div><div>                         maxy</div>







<div>                         (SDL::screen-pitch sdl-surface)))</div></div></div></blockquote></div><div><br>In the Scheme world I think there should only be the need for one representation/form of the C constant.<br><br>



</div><div>

<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div></div><div>But the result is the same: segfault. Using your first solution (returning an int) causes the same problem I got at the beginning: (cairo#cairo-image-surface-create-for-data '#<|unsigned char*| #16 0x7f584104c000> 0 <a href="tel:500%20500%202000" value="+15005002000" target="_blank">500 500 2000</a>)</div>





</div></blockquote></div><div><br>To make this one spin, just declare the second argument of this procedure as an int also. Does it work?<br> <br></div><div><div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div>

<div><br></div><div>Thanks for your help.</div><div><br></div><div>Best regards</div><div><div></div><div><div><br></div><div><br></div><br><div class="gmail_quote">On Sun, Feb 5, 2012 at 6:35 PM, Mikael <span dir="ltr"><<a href="mailto:mikael.rcv@gmail.com" target="_blank">mikael.rcv@gmail.com</a>></span> wrote:<br>







<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If those are int consts from the C header files of the lib, then import them as is fromt here (define CAIRO_FORMAT_ARGB32 ((c-lambda () int "___result = CAIRO_FORMAT_ARGB32;")) etc. , and any arguments that need them declare as int too.<br>









<br>If they're C enums, maybe you want another datatype declaration of them than int - though I suppose int ought to be generally viable for that too.<br><br>The exception in your example below is not so strange, as the second arg is an int while the procedure is declared as cairo-format-t. For it to work, I suppose define CAIRO_FORMAT_RGB24 to ((c-lambda () cairo-format-t "___result = CAIRO_FORMAT_RGB24;")) . Works?<br>









<br><div class="gmail_quote">2012/2/5 Álvaro Castro-Castilla <span dir="ltr"><<a href="mailto:alvaro.castro.castilla@gmail.com" target="_blank">alvaro.castro.castilla@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div><div>

Hi,<div><br></div><div>I'm using my ffi cairo bindings, that worked fine in the past. However now that I'm using Gambit compiled with C++ support it stopped working.</div><div><br></div><div><br></div><div><br></div>











<div><br></div><div>I got this error:</div><div><div><br></div><div>*** ERROR IN main-sdl#main, "main-sdl.scm"@19.24 -- (Argument 2) Can't convert to C type</div><div>(cairo#cairo-image-surface-create-for-data '#<|unsigned char*| #16 0x7f33ba191000> <a href="tel:1%20500%20500%202000" value="+15005002000" target="_blank">1 500 500 2000</a>)</div>











</div><div><br></div><div>On the ffi side, things are declared like this:</div><div><br></div><div><div>(c-define-type cairo-format-t "cairo_format_t")</div><div>(define CAIRO_FORMAT_ARGB32 0)</div><div>(define CAIRO_FORMAT_RGB24 1)</div>











<div>(define CAIRO_FORMAT_A8 2)</div><div>(define CAIRO_FORMAT_A1 3)</div><div>(define CAIRO_FORMAT_RGB16_565 4)</div></div><div>(define cairo-image-surface-create-for-data (c-lambda (unsigned-char* cairo-format-t int int int) cairo-surface-t* "cairo_image_surface_create_for_data"))</div>











<div><br></div><div>And used like this:</div><div><br></div><div><div>(cairo-image-surface-create-for-data</div><div>                         (SDL::surface-pixels sdl-surface)</div><div>                         CAIRO_FORMAT_RGB24</div>











<div>                         maxx</div><div>                         maxy</div><div>                         (SDL::screen-pitch sdl-surface))</div></div><div><br></div><div>I've tried with this idea:</div><div><br></div>











<div><div>(define cairo-format</div><div>  (c-lambda (int) cairo-format-t</div><div>            "</div><div>            switch (___arg1) {</div><div>              case 0: ___result = CAIRO_FORMAT_ARGB32; break;</div>











<div>              case 1: ___result = CAIRO_FORMAT_RGB24; break; // and so on...</div><div>            }</div><div>            "))</div></div><div><br></div><div>And then calling on the Scheme side "(cairo-format CAIRO_FORMAT_RGB24)" instead of just using the int directly, but it segfaults.</div>











<div><br></div><div><br></div><div>How should I really handle enums properly?</div><div><br></div><div><br></div><div><br></div><div>Best regards,</div><div><br></div>
<br></div></div>_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca" target="_blank">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br></blockquote></div><br>
</blockquote></div><br></div></div></div>
</blockquote></div></div></div><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br>