Code example to use function macros

41 views
Skip to first unread message

m

unread,
Oct 14, 2022, 5:25:11 PM10/14/22
to Java Native Access
Hello, 
The docs mention that the library is capable of simulating C preprocessor function macros. I've new to both java and C system calls. I've been trying simulate some X11 Library methos, and they've worked except for when i've found the function DefaultRootWindow is a function macro (was causing a UnsatisfiedLinkError)

An excerpt would go like this 

    public interface XLibrary extends Library {
        XLibrary INSTANCE = (XLibrary) Native.load("X11", XLibrary.class);
        public Pointer XOpenDisplay(Byte byteValue);
        public Pointer XInternAtom(Pointer display, String value, boolean booleanValue);
        public Pointer XNextEvent(Pointer display, XEvent event);
        public Pointer XCloseDisplay(Pointer display);

        public Window DefaultRootWindow(Pointer display);
    }

   public static void main (String[] args){
            XLibrary.INSTANCE.XCloseDisplay(display);
    }

An issue mentioned that function macros can't be directly used, but I can't figure out how the mapping works! Thanks

Matthias Bläsing

unread,
Oct 14, 2022, 5:58:52 PM10/14/22
to jna-...@googlegroups.com
Hi,

Am Freitag, dem 14.10.2022 um 14:25 -0700 schrieb m:
> The docs mention that the library is capable of simulating C
> preprocessor function macros.

Which documentation? Please provide a link. Because no JNA covers
runtime calling, but preprocessor macros are compile time constructs.
There maybe code generators out there, that can parse header files and
convert macros to java code, but JNA itself can't do this as at runtime
the macro just does not exists.

> I've new to both java and C system calls. I've been trying simulate
> some X11 Library methos, and they've worked except for when i've
> found the function DefaultRootWindow is a function macro (was causing
> a UnsatisfiedLinkError)
>

You need to have a look at the header and convert the macro definitions
to Java code.

In this case, there is a real function that sound like a valid
replacement: XDefaultRootWindow.

Greetings

Matthias

m

unread,
Oct 15, 2022, 2:04:57 PM10/15/22
to Java Native Access
> Which documentation? Please provide a link. 
I'll quote "Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros" from the readme.

> You need to have a look at the header and convert the macro definitions to Java code.
Any resources on this part? Also, Does the same apply to identifiers. There are other Cpp X11 identifiers i.e.. XFixesSetSelectionOwnerNotifyMask, XA_PRIMARY, which I also don't know how to go about them.




Matthias Bläsing

unread,
Oct 15, 2022, 2:27:05 PM10/15/22
to jna-...@googlegroups.com
Hi,

Am Samstag, dem 15.10.2022 um 11:04 -0700 schrieb m:
> > Which documentation? Please provide a link. 
> I'll quote "Customizable mapping from Java method to native function
> name, and customizable invocation to simulate C preprocessor function
> macros" from the readme.

YOU can use JNA to simulate the result of preprocessor macros. As
explained in my first email, the information is not present at runtime
and thus can't be used, but you can read the header files and simulate.

> > You need to have a look at the header and convert the macro
> > definitions to Java code.
> Any resources on this part? Also, Does the same apply to identifiers.
> There are other Cpp X11 identifiers
> i.e.. XFixesSetSelectionOwnerNotifyMask, XA_PRIMARY, which I also
> don't know how to go about them.

Don't take this wrong, but you need to get a grab of C _and_ Java to
write mappings.

XA_PRIMARY is a preprocessor definition. If you look into the header
file, you see this:

#define XA_PRIMARY ((Atom) 1)

It now depends on whether you want to use the existing bindings in jna-
platform. For the latter, have a look here:


https://github.com/java-native-access/jna/blob/673079f3afae0b0dfeaa111071aeed909af5dcc3/contrib/platform/src/com/sun/jna/platform/unix/X11.java#L668
https://github.com/java-native-access/jna/blob/673079f3afae0b0dfeaa111071aeed909af5dcc3/contrib/platform/src/com/sun/jna/platform/unix/X11.java#L83-L168
https://github.com/java-native-access/jna/blob/673079f3afae0b0dfeaa111071aeed909af5dcc3/contrib/platform/src/com/sun/jna/platform/unix/X11.java#L62-L82

For the former:

Xdefs.h defines the Atom type as:

typedef unsigned long Atom;

So an Atom is just a C long. You now need to know that the size long of
long is platform dependend and that it is so common, that JNA has
NativeLong in its core. So the define becomes:

private static final NativeLong XA_PRIMARY = new NativeLong(1);


For XFixesSetSelectionOwnerNotifyMask you find in the header:

#define XFixesSetSelectionOwnerNotifyMask (1L << 0)
#define XFixesSelectionWindowDestroyNotifyMask (1L << 1)
#define XFixesSelectionClientCloseNotifyMask (1L << 2)

It depends how this is to be used. Naively I would map them as:

private static final long XFixesSetSelectionOwnerNotifyMask = 1 << 0;
private static final long XFixesSelectionWindowDestroyNotifyMask = 1 << 1;
private static final long XFixesSelectionClientCloseNotifyMask = 1 << 2;

I encourage you to have a look at the existing X11 bindings to get a bearing.

Greetings

Matthias

m

unread,
Oct 15, 2022, 4:46:24 PM10/15/22
to Java Native Access
I was trying to replicate the behavior of the following Cpp code

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    Display *disp;
    Window root;
    Atom clip;
    XEvent evt;
    disp = XOpenDisplay(NULL);
    if (!disp) {
        fprintf(stderr, "Can't open X display\n");
        exit(1);
    }
    root = DefaultRootWindow(disp);
    clip = XInternAtom(disp, "CLIPBOARD", False);
    XFixesSelectSelectionInput(disp, root, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);
    XFixesSelectSelectionInput(disp, root, clip, XFixesSetSelectionOwnerNotifyMask);
    XNextEvent(disp, &evt);
    XCloseDisplay(disp);
}


The current java code I"m battling with is as follows. It behaves weirdly. As the shell prompt only gets returned back, when a highlighted text gets copied to a clipboard, then another event happens, like closing the editor window.

import com.sun.jna.Platform;
import com.sun.jna.Native;
import com.sun.jna.Library;
import com.sun.jna.Pointer;

import com.sun.jna.platform.unix.X11.XEvent;
import com.sun.jna.platform.unix.X11.Window;
import com.sun.jna.platform.unix.X11.Display;
import com.sun.jna.platform.unix.X11.Atom;

public class captureHighlightedText {
   
    public static void main(String[] arguments) {

        if (Platform.isLinux() == true) {
            Display display = XLibrary.INSTANCE.XOpenDisplay(null);
            if (display == null) {
                System.out.println("Can't open X display!");            
            } else {
                Atom clip = XLibrary.INSTANCE.XInternAtom(display, "CLIPBOARD", false);
                Window root = XLibrary.INSTANCE.XDefaultRootWindow(display);

                XLibrary.XFixes.INSTANCE.XFixesSelectSelectionInput(display, root, XLibrary.XA_PRIMARY, XLibrary.XFixesSelectionClientCloseNotifyMask);
                XLibrary.XFixes.INSTANCE.XFixesSelectSelectionInput(display, root, clip, XLibrary.XFixesSelectionClientCloseNotifyMask);
               
                XEvent xevent = new XEvent();
                XLibrary.INSTANCE.XNextEvent(display, xevent);
                XLibrary.INSTANCE.XCloseDisplay(display);

            }
        }
    }

    public interface XLibrary extends Library {
        XLibrary INSTANCE = Native.load("X11", XLibrary.class);
        public Display XOpenDisplay(Byte byteValue);
        public Atom XInternAtom(Display display, String value, boolean booleanValue);
        public Pointer XNextEvent(Display display, XEvent event);
        public Pointer XCloseDisplay(Display display);
        public Window XDefaultRootWindow(Display display);
        public interface XFixes extends XLibrary {
            XFixes INSTANCE = Native.load("Xfixes", XFixes.class);
            public Window XFixesSelectSelectionInput(Display display, Window rootWindow, Atom whatYa, long XfixesSelector);
        }

        public static final long XFixesSelectionClientCloseNotifyMask = 1 << 2;    
        public static final Atom XA_PRIMARY = new Atom();
    }
   
}

Can you please pinpoint the ailment within java porting

Matthias Bläsing

unread,
Oct 15, 2022, 7:53:00 PM10/15/22
to jna-...@googlegroups.com
Hi,

you might have succeeded, if you had used the right mask in the Java programm.....

Anyway here is a full sample:


Greetings

Matthias
--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/511f87a6-7445-47e0-8ac5-b157007f4e5cn%40googlegroups.com.

m

unread,
Oct 16, 2022, 7:47:08 PM10/16/22
to Java Native Access
Thanks for putting the time and effort into having a functional example of the general intention. I'll put some time into understanding the code.
However I was wondering how should I get to learn more about the Library more. So far, I try to read the dry readme text, and the miniature code examples (except for the standalone working directory examples), but it seems like a foreign language for me!
 

Matthias Bläsing

unread,
Oct 17, 2022, 2:55:39 PM10/17/22
to jna-...@googlegroups.com
Hi,

Am Sonntag, dem 16.10.2022 um 16:47 -0700 schrieb m:
> > However I was wondering how should I get to learn more about the
> > Library more. So far, I try to read the dry readme text, and the
> > miniature code examples (except for the standalone working
> > directory examples), but it seems like a foreign language for me!
> >

maybe this helps:

http://java-native-access.github.io/jna/5.12.0/javadoc/

and maybe you find help here:

https://github.com/java-native-access/jna/tree/master/www

HTH

Matthias

mostafa mahmoud

unread,
Feb 24, 2024, 5:38:48 AMFeb 24
to Java Native Access
Would it be possible to append the event for grabbing the motion events of the pointer as well without many modifications?

I've tried to port the XGrabPointer function

fun XGrabPointer(
display: X11.Display?,
grab_window: X11.Window?,
owner_events: Int,
event_mask: Long,
pointer_mode: Int,
keyboard_mode: Int,
confine_to: Int,
cursor: Int,
time: Long
): Int

and then call it after the other XFixes functions, but I'm not sure if I'm doing it correctly

XFixes.INSTANCE.XGrabPointer(
display = display,
grab_window = rootWindow,
owner_events = 1,
pointer_mode = (2 or 4 or 256),
keyboard_mode = 1,
event_mask = 1,
confine_to = 0,
cursor = 0,
time = System.currentTimeMillis()
)

Dev Master

unread,
Feb 24, 2024, 5:39:45 AMFeb 24
to jna-...@googlegroups.com
Yeah


--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
Message has been deleted

Dev Master

unread,
Feb 24, 2024, 5:48:22 AMFeb 24
to jna-...@googlegroups.com
Before, introduce yourself

On Sat, Feb 24, 2024 at 1:45 PM mostafa mahmoud <solic...@gmail.com> wrote:
What modifications should I be making to the original code sample?
Message has been deleted

Dev Master

unread,
Feb 24, 2024, 5:53:55 AMFeb 24
to jna-...@googlegroups.com
Hi. I am Serhii. I am from Ukraine. Where are you from?
Can we chat Telegram or skype?
Message has been deleted

Dev Master

unread,
Feb 24, 2024, 5:58:17 AMFeb 24
to jna-...@googlegroups.com
I sent a message via telegram.
Let's discuss telegram.
Thank you

On Sat, Feb 24, 2024 at 1:55 PM mostafa mahmoud <solic...@gmail.com> wrote:
Nice to meet you!
I'm from Egypt

Ya sure. my telegram handle is @xquilt. I'll reach out in a while from now due to the timezone diff.
On Saturday, February 24, 2024 at 12:53:55 PM UTC+2 devmas...@gmail.com wrote:
Hi. I am Serhii. I am from Ukraine. Where are you from?
Can we chat Telegram or skype?

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
Message has been deleted

mostafa mahmoud

unread,
Feb 24, 2024, 1:33:16 PMFeb 24
to Java Native Access
Any input would much appreciated
Reply all
Reply to author
Forward
0 new messages