@Override public long getSystemIdleTime () { X11.Window window = null; XScreenSaverInfo info = null; Display display = null; long idleMillis = 0L; try { display = X11.INSTANCE.XOpenDisplay(null); window = X11.INSTANCE.XDefaultRootWindow(display); info = new XScreenSaverInfo(); Xss.INSTANCE.XScreenSaverQueryInfo(display, window, info); idleMillis = info.idle.longValue(); } finally { info = null; if (display != null) X11.INSTANCE.XCloseDisplay(display); display = null; } return idleMillis; }
private void sendMessage (Display display, long mode, String msg) { X11 x = X11.INSTANCE; XEvent event = new XEvent(); event.type = X11.ClientMessage; event.setType(XClientMessageEvent.class); event.xclient.type = X11.ClientMessage; event.xclient.display = display; event.xclient.message_type = x.XInternAtom(display, _NET_WM_STATE, false); event.xclient.window = window; event.xclient.format = 32; event.xclient.data.setType(NativeLong[].class); event.xclient.data.l[0] = new NativeLong(mode); event.xclient.data.l[1] = x.XInternAtom(display, msg, false); NativeLong mask = new NativeLong(X11.SubstructureNotifyMask | X11.SubstructureRedirectMask); x.XSendEvent(display, x.XDefaultRootWindow(display), 0, mask, event); }
/** * Returns all windows managed by the window manager. * * @return all windows managed by the window manager * @throws X11Exception thrown if X11 window errors occurred */ public Window[] getWindows() throws X11Exception { byte[] bytes; Window rootWindow = getRootWindow(); try { bytes = rootWindow.getProperty(X11.XA_WINDOW, "_NET_CLIENT_LIST"); } catch (X11Exception e) { try { bytes = rootWindow.getProperty(X11.XA_CARDINAL, "_WIN_CLIENT_LIST"); } catch (X11Exception e1) { throw new X11Exception("Cannot get client list properties (_NET_CLIENT_LIST or _WIN_CLIENT_LIST)"); } } Window[] windowList = new Window[bytes.length / X11.Window.SIZE]; for (int i = 0; i < windowList.length; i++) { windowList[i] = new Window(this, new X11.Window(bytesToInt(bytes, X11.XID.SIZE * i))); } return windowList; }
/** * Returns the number of the active desktop. * * @return number of the active desktop * @throws X11Exception thrown if X11 window errors occurred */ public int getActiveDesktopNumber() throws X11Exception { Window root = getRootWindow(); int cur_desktop; try { cur_desktop = root.getIntProperty(X11.XA_CARDINAL, "_NET_CURRENT_DESKTOP"); } catch (X11Exception e) { try { cur_desktop = root.getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE"); } catch (X11Exception e1) { throw new X11Exception( "Cannot get current desktop properties (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"); } } return cur_desktop; }
/** * Returns the available desktops. * * @return available desktops * @throws X11Exception thrown if X11 window errors occurred */ public Desktop[] getDesktops() throws X11Exception { Window root = getRootWindow(); String[] desktopNames; try { desktopNames = root.getUtf8ListProperty(getAtom("UTF8_STRING"), "_NET_DESKTOP_NAMES"); } catch (X11Exception e) { try { desktopNames = root.getStringListProperty(X11.XA_STRING, "_WIN_WORKSPACE_NAMES"); } catch (X11Exception e1) { throw new X11Exception( "Cannot get desktop names properties (_NET_DESKTOP_NAMES or _WIN_WORKSPACE_NAMES)"); } } Desktop[] desktops = new Desktop[getDesktopCount()]; for (int i = 0; i < desktops.length; i++) { desktops[i] = new Desktop(this, i, desktopNames[i]); } return desktops; }
/** * Reads all modifiers from the XModifierKeymap. * * @param xModifierKeymapRef XModifierKeymap */ public void fromXModifierKeymap(X11.XModifierKeymapRef xModifierKeymapRef) { int count = xModifierKeymapRef.max_keypermod; byte[] keys = xModifierKeymapRef.modifiermap.getByteArray(0, 8 * count); ArrayList<Byte>[] allModifiers = getAllModifiers(); for (int modNr = 0; modNr < 8; modNr++) { ArrayList<Byte> modifier = allModifiers[modNr]; modifier.clear(); for (int keyNr = 0; keyNr < count; keyNr++) { byte key = keys[modNr * count + keyNr]; if (key != 0) { modifier.add(new Byte(key)); } } } }
/** * Returns an XModifierKeymap corresponding to this object. * * @return XModifierKeymap */ public X11.XModifierKeymapRef toXModifierKeyamp() { ArrayList<Byte>[] allModifiers = getAllModifiers(); // determine max list size int count = 0; for (int i = 0; i < allModifiers.length; i++) { count = Math.max(count, allModifiers[i].size()); } byte[] keys = new byte[8 * count]; for (int modNr = 0; modNr < 8; modNr++) { ArrayList<Byte> modifier = allModifiers[modNr]; for (int keyNr = 0; keyNr < modifier.size(); keyNr++) { keys[modNr * count + keyNr] = modifier.get(keyNr).byteValue(); } } X11.XModifierKeymapRef xModifierKeymapRef = new X11.XModifierKeymapRef(); xModifierKeymapRef.max_keypermod = count; xModifierKeymapRef.modifiermap = new Memory(keys.length); xModifierKeymapRef.modifiermap.write(0, keys, 0, keys.length); return xModifierKeymapRef; }
/** * Returns the geometry of the window. * * @return geometry of the window */ public Geometry getGeometry() { X11.WindowByReference junkRoot = new X11.WindowByReference(); IntByReference junkX = new IntByReference(); IntByReference junkY = new IntByReference(); IntByReference x = new IntByReference(); IntByReference y = new IntByReference(); IntByReference width = new IntByReference(); IntByReference height = new IntByReference(); IntByReference borderWidth = new IntByReference(); IntByReference depth = new IntByReference(); x11.XGetGeometry(display.x11Display, x11Window, junkRoot, junkX, junkY, width, height, borderWidth, depth); x11.XTranslateCoordinates(display.x11Display, x11Window, junkRoot.getValue(), junkX.getValue(), junkY.getValue(), x, y, junkRoot); return new Geometry(x.getValue(), y.getValue(), width.getValue(), height.getValue(), borderWidth.getValue(), depth.getValue()); }
/** * Returns the bounding box of the window. * * @return bounding box of the window */ public Rectangle getBounds() { X11.WindowByReference junkRoot = new X11.WindowByReference(); IntByReference junkX = new IntByReference(); IntByReference junkY = new IntByReference(); IntByReference x = new IntByReference(); IntByReference y = new IntByReference(); IntByReference width = new IntByReference(); IntByReference height = new IntByReference(); IntByReference border_width = new IntByReference(); IntByReference depth = new IntByReference(); x11.XGetGeometry(display.x11Display, x11Window, junkRoot, junkX, junkY, width, height, border_width, depth); x11.XTranslateCoordinates(display.x11Display, x11Window, junkRoot.getValue(), junkX.getValue(), junkY.getValue(), x, y, junkRoot); int xVal = x.getValue(); int yVal = y.getValue(); return new Rectangle(xVal, yVal, xVal + width.getValue(), yVal + height.getValue()); }
/** * Returns the property value as a null terminated byte array. * * @param xa_prop_type property type * @param xa_prop_name property name * @return property value as a null terminated byte array * @throws X11Exception thrown if X11 window errors occurred */ public byte[] getNullTerminatedProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception { byte[] bytesOrig = getProperty(xa_prop_type, xa_prop_name); byte[] bytesDest; // search for '\0' int i; for (i = 0; i < bytesOrig.length; i++) { if (bytesOrig[i] == '\0') break; } if (i < bytesOrig.length - 1) { bytesDest = new byte[i + 1]; System.arraycopy(bytesOrig, 0, bytesDest, 0, i + 1); } else { bytesDest = bytesOrig; } return bytesDest; }
public static boolean isLinuxX11 () { try { Native.loadLibrary("X11", X11.class); return true; } catch (UnsatisfiedLinkError e) { return false; } }
private void sendMessage (long mode, String msg) { X11 x = X11.INSTANCE; Display display = null; try { display = x.XOpenDisplay(null); sendMessage(display, mode, msg); } finally { if (display != null) { x.XCloseDisplay(display); } } }
@Override public long getIdleTimeMillis() { X11.Window window = null; XScreenSaverInfo info = null; Display display = null; long idleMillis = 0L; try { display = X11.INSTANCE.XOpenDisplay(null); if (display == null) { display = X11.INSTANCE.XOpenDisplay(":0.0"); } if (display == null) { throw new RuntimeException("Could not find a display, please setup your DISPLAY environment variable"); } window = X11.INSTANCE.XDefaultRootWindow(display); info = new XScreenSaverInfo(); Xss.INSTANCE.XScreenSaverQueryInfo(display, window, info); idleMillis = info.idle.longValue(); } catch(UnsatisfiedLinkError e) { throw new RuntimeException(e.getMessage(), e); } finally { info = null; if (display != null) { X11.INSTANCE.XCloseDisplay(display); } display = null; } return idleMillis; }
/** * Creates the OOWindowUtils using a given display. * * @param x11Display open display */ public Display(X11.Display x11Display) { this.x11Display = x11Display; if (x11Display == null) { throw new Error("X Display is null"); } }
/** * Get internal atoms by name. * * @param name name of the atom * @return atom */ public X11.Atom getAtom(String name) { X11.Atom atom = atomsHash.get(name); if (atom == null) { atom = x11.XInternAtom(x11Display, name, false); atomsHash.put(name, atom); } return atom; }
/** * Returns the window manager information as an window. * * @return window manager information as an window * @throws X11Exception thrown if X11 window errors occurred */ public Window getWindowManagerInfo() throws X11Exception { Window rootWindow = getRootWindow(); try { return rootWindow.getWindowProperty(X11.XA_WINDOW, "_NET_SUPPORTING_WM_CHECK"); } catch (X11Exception e) { try { return rootWindow.getWindowProperty(X11.XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK"); } catch (X11Exception e1) { throw new X11Exception( "Cannot get window manager info properties. (_NET_SUPPORTING_WM_CHECK or _WIN_SUPPORTING_WM_CHECK)"); } } }
/** * Returns the number of desktops. * * @return number of desktops * @throws X11Exception thrown if X11 window errors occurred */ public int getDesktopCount() throws X11Exception { Window root = getRootWindow(); try { return root.getIntProperty(X11.XA_CARDINAL, "_NET_NUMBER_OF_DESKTOPS"); } catch (X11Exception e) { try { return root.getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE_COUNT"); } catch (X11Exception e1) { throw new X11Exception( "Cannot get number of desktops properties (_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)"); } } }
/** * Returns the modifier keymap. * * @return modifier keymap */ public ModifierKeymap getModifierKeymap() { X11.XModifierKeymapRef xModifierKeymapRef = x11.XGetModifierMapping(x11Display); ModifierKeymap modifierKeymap = new ModifierKeymap(xModifierKeymapRef); x11.XFreeModifiermap(xModifierKeymapRef); return modifierKeymap; }
/** * Returns the title of the window. * * @return title of the window * @throws X11Exception thrown if X11 window errors occurred */ public String getTitle() throws X11Exception { try { return getUtf8Property(display.getAtom("UTF8_STRING"), "_NET_WM_NAME"); } catch (X11Exception e) { return getUtf8Property(X11.XA_STRING, X11.XA_WM_NAME); } }
/** * Returns the desktop ID of the window. * * @return desktop ID of the window * @throws X11Exception thrown if X11 window errors occurred */ public int getDesktop() throws X11Exception { try { return getIntProperty(X11.XA_CARDINAL, "_NET_WM_DESKTOP"); } catch (X11Exception e) { return getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE"); } }
/** * Returns the XWindowAttributes of the window. * * @return XWindowAttributes of the window */ public X11.XWindowAttributes getXWindowAttributes() { X11.XWindowAttributes xwa = new X11.XWindowAttributes(); x11.XGetWindowAttributes(display.x11Display, x11Window, xwa); return xwa; }
/** * Returns the property value as byte array where every '\0' character is replaced by '.'. * * @param xa_prop_type property type * @param xa_prop_name property name * @return property value as byte array where every '\0' character is replaced by '.' * @throws X11Exception thrown if X11 window errors occurred */ public byte[] getNullReplacedStringProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception { byte[] bytes = getProperty(xa_prop_type, xa_prop_name); // search for '\0' int i; for (i = 0; i < bytes.length; i++) { if (bytes[i] == '\0') { bytes[i] = '.'; } } return bytes; }
public int clientMsg(String msg, NativeLong data0, NativeLong data1, NativeLong data2, NativeLong data3, NativeLong data4) throws X11Exception { X11.XClientMessageEvent event; NativeLong mask = new NativeLong(X11.SubstructureRedirectMask | X11.SubstructureNotifyMask); event = new X11.XClientMessageEvent(); event.type = X11.ClientMessage; event.serial = new NativeLong(0); event.send_event = 1; event.message_type = display.getAtom(msg); event.window = x11Window; event.format = 32; event.data.setType(NativeLong[].class); event.data.l[0] = data0; event.data.l[1] = data1; event.data.l[2] = data2; event.data.l[3] = data3; event.data.l[4] = data4; X11.XEvent e = new X11.XEvent(); e.setTypedValue(event); if (x11.XSendEvent(display.x11Display, display.getRootWindow().x11Window, 0, mask, e) != 0) { return X11.Success; } else { throw new X11Exception("Cannot send " + msg + " event."); } }
/** * On Linux by default the handling of mouse move, enter and leave event are not propagated. * Unfortunately the "handle-events" properties hide some important expose events too, * sowe've to do some lowlevel trick to be able to get these events. * In this case we (gstreamer-linux) must handle redraw too! * * @param enableX11Events true if X11 event should have to be grabbed (mouse move, enter and leave event on Linux). */ private void handleX11Events() { if (x11Events && Platform.isLinux()) { videosink.set("handle-events", !x11Events); overlay.handleEvent(!x11Events); watcherRunning = true; new Thread() { @Override public void run() { try { final X11 x11 = X11.INSTANCE; final Display display = x11.XOpenDisplay(null); Window window = new Window(nativeHandle); x11.XSelectInput(display, window, new NativeLong(X11.ExposureMask | X11.VisibilityChangeMask | X11.StructureNotifyMask | X11.FocusChangeMask | //X11.PointerMotionMask | X11.EnterWindowMask | X11.LeaveWindowMask)); while (watcherRunning) { final XEvent xEvent = new XEvent(); x11.XNextEvent(display, xEvent); if (watcherRunning && !isDisposed()) { getDisplay().asyncExec(new Runnable() { public void run() { if (watcherRunning && !isDisposed()) { final Event swtEvent = new Event(); XCrossingEvent ce; switch (xEvent.type) { // case X11.MotionNotify: // XMotionEvent e = (XMotionEvent)xEvent.readField("xmotion"); // swtEvent.x = e.x; // swtEvent.y = e.y; // notifyListeners(SWT.MouseMove, swtEvent); // break; case X11.EnterNotify: ce = (XCrossingEvent)xEvent.readField("xcrossing"); swtEvent.x = ce.x; swtEvent.y = ce.y; notifyListeners(SWT.MouseEnter, swtEvent); break; case X11.LeaveNotify: ce = (XCrossingEvent)xEvent.readField("xcrossing"); swtEvent.x = ce.x; swtEvent.y = ce.y; notifyListeners(SWT.MouseExit, swtEvent); break; default: overlay.expose(); } } } }); } } x11.XCloseDisplay(display); } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
public X11GLFWIconSetter () { x11 = X11.INSTANCE; }
@Override public void setIcon (FileHandle iconCacheFolder, FileHandle icoFile, FileHandle pngFile) { Display display = null; try { display = x11.XOpenDisplay(null); Pixmap pixmap = new Pixmap(pngFile); if (pixmap.getWidth() != pixmap.getHeight()) { throw new IllegalStateException("Supplied icon image must be square"); } long buffer[] = new long[2 + pixmap.getWidth() * pixmap.getHeight()]; buffer[0] = pixmap.getWidth(); buffer[1] = pixmap.getHeight(); int bufIndex = 2; for (int i = 0; i < pixmap.getWidth(); i++) { for (int ii = 0; ii < pixmap.getHeight(); ii++) { int color = pixmap.getPixel(ii, i); //assuming pixmap is RGBA8888, buffer for x11 is ARGB8888 //probably not very optimized int r = ((color & 0xff000000) >>> 24); int g = ((color & 0x00ff0000) >>> 16); int b = ((color & 0x0000ff00) >>> 8); int a = ((color & 0x000000ff)); buffer[bufIndex++] = ((a << 24) | (r << 16) | (g << 8) | (b)); } } Window window = new Window(GLFWNativeX11.glfwGetX11Window(GLFW.glfwGetCurrentContext())); Pointer ptr = new Memory(buffer.length * 8); ptr.write(0, buffer, 0, buffer.length); x11.XChangeProperty(display, window, getAtom(display, _NET_WM_ICON), X11.XA_CARDINAL, 32, X11.PropModeReplace, ptr, buffer.length); pixmap.dispose(); } catch (Exception e) { throw new IllegalStateException(e); } finally { if (display != null) { x11.XCloseDisplay(display); } } }
public int nextEvent(X11.XEvent event) { return x11.XNextEvent(display.x11Display, event); }
public void sendEvent(int eventMask, X11.XEvent event) { x11.XSendEvent(display.x11Display, x11Window, 1, new NativeLong(eventMask), event); }
/** * Returns the property value as UTF8 string where every '\0' character is replaced by '.'. * * @param xa_prop_type property type * @param xa_prop_name property name * @return property value as UTF8 string where every '\0' character is replaced by '.' * @throws X11Exception thrown if X11 window errors occurred */ public String getUtf8Property(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception { try { return new String(getNullReplacedStringProperty(xa_prop_type, xa_prop_name), "UTF8"); } catch (UnsupportedEncodingException e) { throw new X11Exception(e); } }
/** * Returns the property value as UTF8 string list * * @param xa_prop_type property type * @param xa_prop_name property name * @return property value as UTF8 string list * @throws X11Exception thrown if X11 window errors occurred */ public String[] getUtf8ListProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception { try { return new String(getProperty(xa_prop_type, xa_prop_name), "UTF8").split("\0"); } catch (UnsupportedEncodingException e) { throw new X11Exception(e); } }
/** * Returns the X11 display. * * @return X11 display */ public X11.Display getX11Display() { return x11Display; }
/** * Returns the current active window. * * @return current active window * @throws X11Exception thrown if X11 window errors occurred */ public Window getActiveWindow() throws X11Exception { return getRootWindow().getWindowProperty(X11.XA_WINDOW, "_NET_ACTIVE_WINDOW"); }
/** * Returns the key symbol corresponding to the the key name. * * @param keyName name of the key * @return key symbol */ public X11.KeySym getKeySym(String keyName) { return x11.XStringToKeysym(keyName); }
/** * Returns the key symbol corresponding to the keycode. * * @param keyCode keycode * @param index element of the keycode vector * @return key symbol */ public X11.KeySym getKeySym(byte keyCode, int index) { return x11.XKeycodeToKeysym(x11Display, keyCode, index); }
/** * Returns the keycode corresponding to the key symbol. * * @param keySym key symbol * @return keycode */ public byte getKeyCode(X11.KeySym keySym) { return x11.XKeysymToKeycode(x11Display, keySym); }
/** * Returns the key name corresponding to the key symbol. * * @param keySym key symbol * @return name of the key */ public String getKeyName(X11.KeySym keySym) { return x11.XKeysymToString(keySym); }
/** * Sets the modifier keymap. * * @param modifierKeymap modifier keymap */ public void setModifierKeymap(ModifierKeymap modifierKeymap) { X11.XModifierKeymapRef xModifierKeymapRef = modifierKeymap.toXModifierKeyamp(); x11.XSetModifierMapping(x11Display, xModifierKeymapRef); }
/** * Creates a modifier keymap and reads the modifiers from the XModifierKeymap. * * @param xModifierKeymapRef XModifierKeymap */ public ModifierKeymap(X11.XModifierKeymapRef xModifierKeymapRef) { fromXModifierKeymap(xModifierKeymapRef); }
/** * Returns the X11 window object. * * @return X11 window */ public X11.Window getX11Window() { return x11Window; }
/** * Creates the window. * * @param display display where the window is allocated * @param x11Window X11 window */ public Window(X.Display display, X11.Window x11Window) { this.display = display; this.x11Window = x11Window; }