g77, X-windows, C and pointers: HELP! 
Author Message
 g77, X-windows, C and pointers: HELP!

I'm attempting to port the interactive airfoil design program XFOIL to a PC
running X-windows under Linux.  I'm using g77 version 0.5.18, gcc 2.7.2
and Linux 2.0.0 (Slackware release).  The guts of the program is written
in fortran, including most of the plotting routines.  However, an interface
written in C is used to handle the actual Xlib calls.  As far as I can
make out, the problem is with the Display and Window data structures.
A Fortran routine obtains values for display and window from a
C-function, and then passes these values on to other C-functions
to do useful things like opening and closing windows, etc.  In the
C-function, the display and window variables are pointers, however, in
the Fortran subroutine these variables are declared as plain and
simple Integers.  So how do you get a Fortran routine to interpret a
value from C as a pointer, and to re-return it to C as a pointer
again?  FWIW, the programme works under xlf ......  I've tried
declaring the Integers as Integer*8, without much success.

For example:

Fortran part:
.
.
       Integer display
       display = XFtnInit()
       call XFtnCurrentPointer(display, Twind, ixt, iyt)
.
.

C part:
.
.
  Display *  xftninit_()
{

    Display *display;

    if ((display = XOpenDisplay(NULL)) == NULL) {
        fprintf(stderr, "Oops! Cannot open display %s.\n",
                XDisplayName(NULL));
        exit(1);
    }

    return(display);  

Quote:
}

.
.

xftncurrentpointer_(display, window, x, y)
    Display **display;
    Window *window;
    int *x, *y;
{
    Window child, root;
    int rootx, rooty;
    unsigned int keys;

    XQueryPointer(*display, DefaultRootWindow(*display), &root,
                  window, &rootx, &rooty, x, y, &keys);  
    XQueryPointer(*display, *window, &root, &child, &rootx, &rooty, x, y,
                  &keys);  

Quote:
}

.
.

Any bright ideas?

--
Charles Crosby                                  Ph: (27)-(0)-12-4202861 (w)
Dept of Mech and Aero Eng                           (27)-(0)-12-3451586 (h)
University of Pretoria, South Africa            Fax:(27)-(0)-12-436540



Sat, 29 Jan 2000 03:00:00 GMT  
 g77, X-windows, C and pointers: HELP!

Quote:

> I'm attempting to port the interactive airfoil design program XFOIL to a PC
> running X-windows under Linux.  I'm using g77 version 0.5.18, gcc 2.7.2
> and Linux 2.0.0 (Slackware release).  The guts of the program is written
> in Fortran, including most of the plotting routines.  However, an interface
> written in C is used to handle the actual Xlib calls.  As far as I can
> make out, the problem is with the Display and Window data structures.
> A Fortran routine obtains values for display and window from a
> C-function, and then passes these values on to other C-functions
> to do useful things like opening and closing windows, etc.  In the
> C-function, the display and window variables are pointers, however, in
> the Fortran subroutine these variables are declared as plain and
> simple Integers.  So how do you get a Fortran routine to interpret a
> value from C as a pointer, and to re-return it to C as a pointer
> again?  FWIW, the programme works under xlf ......  I've tried
> declaring the Integers as Integer*8, without much success.

g77 is, as Fortran compilers go, poor for interoperability with
other languages -- we're still focusing on making it work well
as a Fortran compiler!

That said, you might want to upgrade to g77 0.5.21 when it comes out
(by September 1, according to the current schedule, and we're
probably not going to let this one slip!).

In g77 0.5.21, there is a "GNU Fortran language"-defined way to
declare that an INTEGER variable must be able to hold a pointer,
namely, `INTEGER(KIND=7) SOMEPTR'.

So, if you have a C function that returns a pointer and you wish
to pass that pointer to another C function that expects the pointer
to be passed by value (instead of by reference -- figuring this
out can be tricky!), you might try:

        INTEGER(KIND=7) SOMEPTR, CFUNC1
        SOMEPTR = CFUNC1 (...)
        ...
        CALL CFUNC2 (%VAL(SOMEPTR), ...)

This is not portable to other Fortran compilers, or even to g77 0.5.20
or previous, but should work with g77 from 0.5.21 on out (though,
since it's still in beta testing, it's possible we'd have to change
it again in this area, if a highly impelling reason were found to
do so).  Note that I haven't even tried it myself!  So if it
doesn't work right, submit a bug report.

In case it's not clear -- I'm *not* recommending people do a lot
of code like the above!  But it's an okay stopgap solution if one
must use g77 and is willing to rewrite the code as g77 matures
to provide better facilities.  My impression is that many other
Fortran compilers already provides such facilities, though I'm
not sure they're interoperable either, and g77 has a particularly
challenging problem in that it must support facilities that are
portable to a *wide* variety of machines and yet look the same
in the Fortran code written to use them, something few Fortran
compilers' designers have to worry about.
--

"Practice random senselessness and act kind of beautiful."



Sun, 30 Jan 2000 03:00:00 GMT  
 g77, X-windows, C and pointers: HELP!

 Craig> In case it's not clear -- I'm *not* recommending people do a lot
 Craig> of code like the above!  But it's an okay stopgap solution if one
 Craig> must use g77 and is willing to rewrite the code as g77 matures
 Craig> to provide better facilities.  My impression is that many other
 Craig> Fortran compilers already provides such facilities, though I'm
 Craig> not sure they're interoperable either, and g77 has a particularly
 Craig> challenging problem in that it must support facilities that are
 Craig> portable to a *wide* variety of machines and yet look the same
 Craig> in the Fortran code written to use them, something few Fortran
 Craig> compilers' designers have to worry about.

Wise words.

FWIW, the only reliable solution I know for this sort of thing is to
have C glue code maintain integer `handles' for the C pointers that
need to be passed around in (for instance) Fortran and do table
lookups in the C as appropriate.  (At least one X toolkit operates
with integer handles for its objects anyhow.)



Fri, 04 Feb 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. g77 and Xwindows

2. Using XWindows window handlers in Tk

3. G77 Serial port help under Windows

4. Windows Console pointer help

5. Help with CLX and Xwindows

6. Help required with Tcl/TK for XWindows

7. to CS: or not to CS: in F-PC assembler

8. f2c/g77 and the pointer extention....

9. g77 pointers

10. g77 and POINTER

11. Q. g77 "pointer" compilation

12. POINTER in g77 ?

 

 
Powered by phpBB® Forum Software