Getting X11 window id

Discussion in 'Java' started by Robert Holt, Oct 16, 2005.

  1. Robert Holt

    Robert Holt Guest

    Is there any way to get the X11 window id of a window opened through
    Swing? I need this to display the contents of another unix application
    within my Java application.

    Robert Holt
    Robert Holt, Oct 16, 2005
    1. Advertisements

  2. Robert Holt

    Ross Bamford Guest

    If you're not interested in cross platform (which I assume to be the case
    anyway!) you could try running 'xwininfo' (e.g. via Runtime.exec) and
    parsing the output (a bit). I'm not sure how portable it'd be - you'd need
    to check it out - but here running the command:

    xwininfo -name 'jEdit - Untitled-1'

    returns the window information, the second line of which (after a blank
    line) is:

    xwininfo: Window id: 0x1a0001d "jEdit - Untitled-1"

    IIRC xprop is able to do a similar thing, and may also be specifically
    able to return the ID for a named window. Both have man pages.
    Ross Bamford, Oct 16, 2005
    1. Advertisements

  3. If you're prepared to to go native, you can use JAWT:

    class MyX11Utils {
    public static native long windowID(Component comp);


    #include <jawt.h>
    #include <jawt_md.h>

    Java_MyX11Utils_windowID(JNIEnv* env, jclass cls, jobject comp) {
    JAWT awt;
    JAWT_DrawingSurface* ds = NULL;
    int dsLocked = false;
    JAWT_DrawingSurfaceInfo* dsi = NULL;
    JAWT_X11DrawingSurfaceInfo* x = NULL;
    jlong windowID = -1; /* or some appropriate error value */

    awt.version = JAWT_VERSION_1_4;
    if (!JAWT_GetAWT(env, &awt)) { goto done; }
    ds = awt.GetDrawingSurface(env, comp);
    if (!ds) { goto done; }
    dsLocked = !(ds->Lock(ds) & JAWT_LOCK_ERROR);
    if (!dsLocked) { goto done; }
    dsi = ds->GetDrawingSurfaceInfo(ds);
    if (!dsi) { goto done; }
    x = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;

    /* get the window id. Don't know x11, but is it something like
    windowID = (jlong)x->visualID;
    or using it Drawable or Display properties? */

    /* resource cleanup */
    if (dsi) { ds->FreeDrawingSurfaceInfo(dsi); }
    if (dsLocked) { ds->Unlock(ds); }
    if (ds) { awt.FreeDrawingSurface(ds); }

    return windowID;


    John Perks and Sarah Mount, Oct 16, 2005
  4. Oops, missed off a line:

    John Perks and Sarah Mount, Oct 16, 2005
  5. Robert Holt

    Roedy Green Guest

    anything platform specific of that sort is going to require JNI. The
    big problem is a Window object passed to you on the C side won't have
    a field called x11WindowId.

    You are going to have to somehow find the matching peer window on the
    C side, e.g. from its location.

    You might get somewhere chasing the peer object. Consider though that
    many Swing components are lightweight, painted on a large native GUI
    object. I presume that is true even with X11.
    Roedy Green, Oct 16, 2005
  6. Robert Holt

    Robert Holt Guest

    Thanks for the help. This is what I wanted. The xwininfo was always my
    plan for if I could not use any other method (or to write a similar
    myself which returned the window I wanted).
    Robert Holt, Oct 18, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.