X11 XSynchronize() definition in D

Ali Çehreli acehreli at yahoo.com
Tue Aug 6 03:15:43 PDT 2013


On 08/06/2013 01:01 AM, andrea9940 wrote:

 > Hi, I'm working with the X11 library available from
 > https://github.com/D-Programming-Deimos/libX11
 > If I try to call XSynchronize(display, True) the compilation fails with
 > "Error: function deimos.X11.Xlib.XSynchronize (_XDisplay*) is not
 > callable using argument types (_XDisplay*, int)"
 >
 > I am sure the arguments are correct (see http://goo.gl/8Hzn8s for
 > example) so I think there is a conversion problem between the C and D
 > definition of the function:
 >
 > --- Xlib.h
 > extern int (*XSynchronize(
 >      Display*        /* display */,
 >      Bool        /* onoff */
 > ))(
 >      Display*        /* display */
 > );

I have written the following stubs for C:

typedef void Display;
typedef int Bool;

extern int (*XSynchronize(
     Display*        /* display */,
     Bool        /* onoff */
))(
     Display*        /* display */
);

typedef int(*PreviousAfterFunction)(Display*);

int previous_after_foo(Display* display)
{
     return 0;
}

PreviousAfterFunction XSynchronize(Display* display, Bool onoff)
{
     return &previous_after_foo;
}

int main()
{
     int (*paf)(Display*) = XSynchronize(0, 1);
     paf(0);
}

And then I have written the direct translation in D, which works as well:

struct Display
{}

alias PreviousAfterFunction = int function(Display*);

int previousAfterFoo(Display *)
{
     return 0;
}

PreviousAfterFunction XSynchronize(Display*, bool)
{
     return &previousAfterFoo;
}

void main()
{
     PreviousAfterFunction paf = XSynchronize(null, 1);
     paf(null);
}

 > --- Xlib.d
 > extern int function(
 >      Display*            /* display */,
 >      Bool                /* onoff */
 > )XSynchronize(
 >      Display*            /* display */
 > );

It looks like Xlib.d got it backwards: XSynchronize takes two parameters 
and returns a function pointer that takes a single parameter. It should 
be the following:

int function(Display*) XSynchronize(Display*, bool);

 > Also I can't understand why the C version is not "extern int
 > XSynchronize(Display*,
 >      Bool);" which would be the simplest definition.

That's different. That would be a function taking two parameters and 
returning int. However, XSynchronize takes two parameters and return a 
function pointer (that takes one parameter and returns an int).

Ali



More information about the Digitalmars-d-learn mailing list