Package com.sun.jna.examples.unix.X11

Examples of com.sun.jna.examples.unix.X11.Xext


    }

    private com.sun.jna.Memory buffer;
    private int[] pixels;
    private void updateX11(boolean a, boolean i) {
        X11 x11 = X11.INSTANCE;
        X11.Window win = X11.Window.None;
        Display dpy = x11.XOpenDisplay(null);
        try {
            if (!alphaWindow.isDisplayable()) {
                alphaWindow.pack();
                if (System.getProperty("java.version").matches("^1\\.4\\..*"))
                    alphaWindow.setVisible(true);
                win = new X11.Window((int)Native.getWindowID(alphaWindow));
                Window parent = alphaWindow.getOwner();
                Point where = parent.getLocationOnScreen();
                where.translate(parent.getWidth(), 0);
                alphaWindow.removeAll();
                alphaWindow.setLocation(where);
                alphaWindow.setBackground(new Color(0,0,0,0));
            }
            else {
                win = new X11.Window((int)Native.getWindowID(alphaWindow));
            }
           
            if (i) {
                int w = image.getWidth(null);
                int h = image.getHeight(null);
                alphaWindow.setSize(w, h);
                if (buffer == null || buffer.getSize() != w*h*4) {
                    buffer = new com.sun.jna.Memory(w*h*4);
                    pixels = new int[w*h];
                }

                BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
                Graphics g = buf.getGraphics();
                g.drawImage(image, 0, 0, w, h, null);
               
                long start = System.currentTimeMillis();
                long blitTime, putImageTime, write;
                GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
                long gcTime = System.currentTimeMillis();
                try {
                    Raster raster = buf.getData();
                    int[] pixel = new int[4];
                    for (int y=0;y < h;y++) {
                        for (int x=0;x < w;x++) {
                            raster.getPixel(x, y, pixel);
                            int alpha = (pixel[3]&0xFF)<<24;
                            int red = (pixel[2]&0xFF);
                            int green = (pixel[1]&0xFF)<<8;
                            int blue = (pixel[0]&0xFF)<<16;
                            pixels[y*w+x] = alpha|red|green|blue;
                        }
                    }
                    blitTime = System.currentTimeMillis();
                    X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                    x11.XGetWindowAttributes(dpy, win, xwa);
                    X11.XImage image = x11.XCreateImage(dpy, xwa.visual,
                                                        32, X11.ZPixmap,
                                                        0, buffer, w, h, 32, w*4);
                    buffer.write(0, pixels, 0, pixels.length);
                    write = System.currentTimeMillis();
                    x11.XPutImage(dpy, win, gc, image, 0,0,0,0,w,h);
                    x11.XFree(image.getPointer());
                    putImageTime = System.currentTimeMillis();
                }
                finally {
                    if (gc != null)
                        x11.XFreeGC(dpy, gc);
                }
                long end = System.currentTimeMillis();
                //System.out.println("gc: " + (gcTime-start) + "ms");
                //System.out.println("blit: " + (blitTime-gcTime) + "ms");
                //System.out.println("write: " + (write-blitTime) + "ms");
                //System.out.println("put image: " + (putImageTime-write) + "ms");
                //System.out.println("total: " + (end-start) + "ms");
            }
        }
        finally {
            if (dpy != null)
                x11.XCloseDisplay(dpy);
        }
        if (a)
            WindowUtils.setWindowAlpha(alphaWindow, alpha);
       
        if (!alphaWindow.isVisible()) {
View Full Code Here


                return X11.XK_Meta_L;
            }
            return 0;
        }
        public boolean isPressed(int keycode, int location) {
            X11 lib = X11.INSTANCE;
            Display dpy = lib.XOpenDisplay(null);
            if (dpy == null) {
                throw new Error("Can't open X Display");
            }
            try {
                byte[] keys = new byte[32];
                // Ignore the return value
                lib.XQueryKeymap(dpy, keys);
                int keysym = toKeySym(keycode, location);
                for (int code=5;code < 256;code++) {
                    int idx = code / 8;
                    int shift = code % 8;
                    if ((keys[idx] & (1 << shift)) != 0) {
                        int sym = lib.XKeycodeToKeysym(dpy, (byte)code, 0).intValue();
                        if (sym == keysym)
                            return true;
                    }
                }
            }
            finally {
                lib.XCloseDisplay(dpy);
            }
            return false;
        }
View Full Code Here

        }

        private void setWindowShape(final Window w, final PixmapSource src) {
            Runnable action = new Runnable() {
                public void run() {
                    X11 x11 = X11.INSTANCE;
                    Display dpy = x11.XOpenDisplay(null);
                    if (dpy == null) {
                        return;
                    }
                    Pixmap pm = null;
                    try {
                        X11.Window win = getDrawable(w);
                        pm = src.getPixmap(dpy, win);
                        Xext ext = Xext.INSTANCE;
                        ext.XShapeCombineMask(dpy, win, X11.Xext.ShapeBounding,
                                              0, 0, pm == null ? Pixmap.None : pm,
                                              X11.Xext.ShapeSet);
                    }
                    finally {
                        if (pm != null) {
                            x11.XFreePixmap(dpy, pm);
                        }
                        x11.XCloseDisplay(dpy);
                    }
                    setForceHeavyweightPopups(getWindow(w), pm != null);
                }
            };
            whenDisplayable(w, action);
View Full Code Here

            // Painting directly to the original Graphics
            // fails to properly composite unless the destination
            // is pure black.  Too bad.
            protected void paintDirect(BufferedImage buf, Rectangle bounds) {
                Window window = SwingUtilities.getWindowAncestor(this);
                X11 x11 = X11.INSTANCE;
                X11.Display dpy = x11.XOpenDisplay(null);
                X11.Window win = getDrawable(window);
                Point offset = new Point();
                win = getContentWindow(window, dpy, win, offset);
                X11.GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);

                Raster raster = buf.getData();
                int w = bounds.width;
                int h = bounds.height;
                if (buffer == null || buffer.getSize() != w*h*4) {
                    buffer = new Memory(w*h*4);
                    pixels = new int[w*h];
                }
                for (int y=0;y<h;y++) {
                    for (int x=0;x < w;x++) {
                        raster.getPixel(x, y, pixel);
                        int alpha = pixel[3]&0xFF;
                        int red = pixel[2]&0xFF;
                        int green = pixel[1]&0xFF;
                        int blue = pixel[0]&0xFF;
                        // TODO: use visual RGB masks to position bits
                        // This layout (ABGR) works empirically
                        pixels[y*w + x] = (alpha<<24)|(blue<<16)|(green<<8)|red;
                    }
                }
                X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                x11.XGetWindowAttributes(dpy, win, xwa);
                X11.XImage image =
                    x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap,
                                     0, buffer, w, h, 32, w * 4);
                buffer.write(0, pixels, 0, pixels.length);
                offset.x += bounds.x;
                offset.y += bounds.y;
                x11.XPutImage(dpy, win, gc, image, 0, 0, offset.x, offset.y, w, h);

                x11.XFree(image.getPointer());
                x11.XFreeGC(dpy, gc);
                x11.XCloseDisplay(dpy);
            }
View Full Code Here

    }
    private static class X11WindowUtils extends NativeWindowUtils {
        private static Pixmap createBitmap(final Display dpy,
                                           X11.Window win,
                                           Raster raster) {
            final X11 x11 = X11.INSTANCE;
            Rectangle bounds = raster.getBounds();
            int width = bounds.x + bounds.width;
            int height = bounds.y + bounds.height;
            final Pixmap pm = x11.XCreatePixmap(dpy, win, width, height, 1);
            final GC gc = x11.XCreateGC(dpy, pm, new NativeLong(0), null);
            if (gc == null) {
                return null;
            }
            x11.XSetForeground(dpy, gc, new NativeLong(0));
            x11.XFillRectangle(dpy, pm, gc, 0, 0, width, height);
            final List rlist = new ArrayList();
            try {
                RasterRangesUtils.outputOccupiedRanges(raster, new RasterRangesUtils.RangesOutput() {
                    public boolean outputRange(int x, int y, int w, int h) {
                        rlist.add(new Rectangle(x, y, w, h));
                        return true;
                    }
                });
                X11.XRectangle[] rects = (X11.XRectangle[])
                    new X11.XRectangle().toArray(rlist.size());
                for (int i=0;i < rects.length;i++) {
                    Rectangle r = (Rectangle)rlist.get(i);
                    rects[i].x = (short)r.x;
                    rects[i].y = (short)r.y;
                    rects[i].width = (short)r.width;
                    rects[i].height = (short)r.height;
                    // Optimization: write directly to native memory
                    Pointer p = rects[i].getPointer();
                    p.setShort(0, (short)r.x);
                    p.setShort(2, (short)r.y);
                    p.setShort(4, (short)r.width);
                    p.setShort(6, (short)r.height);
                    rects[i].setAutoSynch(false);
                    // End optimization
                }
                final int UNMASKED = 1;
                x11.XSetForeground(dpy, gc, new NativeLong(UNMASKED));
                x11.XFillRectangles(dpy, pm, gc, rects, rects.length);
            }
            finally {
                x11.XFreeGC(dpy, gc);
            }
            return pm;
        }
View Full Code Here

        private synchronized long[] getAlphaVisualIDs() {
            if (didCheck) {
                return alphaVisualIDs;
            }
            didCheck = true;
            X11 x11 = X11.INSTANCE;
            Display dpy = x11.XOpenDisplay(null);
            if (dpy == null) {
        return alphaVisualIDs;
      }
            XVisualInfo info = null;
            try {
                int screen = x11.XDefaultScreen(dpy);
                XVisualInfo template = new XVisualInfo();
                template.screen = screen;
                template.depth = 32;
                template.c_class = X11.TrueColor;
                NativeLong mask = new NativeLong(X11.VisualScreenMask
                                                 | X11.VisualDepthMask
                                                 | X11.VisualClassMask);
                IntByReference pcount = new IntByReference();
                info = x11.XGetVisualInfo(dpy, mask, template, pcount);
                if (info != null) {
                    List list = new ArrayList();
                    XVisualInfo[] infos =
                        (XVisualInfo[])info.toArray(pcount.getValue());
                    for (int i = 0; i < infos.length; i++) {
                        XRenderPictFormat format =
                            X11.Xrender.INSTANCE.XRenderFindVisualFormat(dpy,
                                                                         infos[i].visual);
                        if (format.type == X11.Xrender.PictTypeDirect
                            && format.direct.alphaMask != 0) {
                            list.add(infos[i].visualid);
                        }
                    }
                    alphaVisualIDs = new long[list.size()];
                    for (int i=0;i < alphaVisualIDs.length;i++) {
                        alphaVisualIDs[i] = ((Number)list.get(i)).longValue();
                    }
                    return alphaVisualIDs;
                }
            }
            finally {
                if (info != null) {
                    x11.XFree(info.getPointer());
                }
                x11.XCloseDisplay(dpy);
            }
            return alphaVisualIDs;
        }
View Full Code Here

        private static X11.Window getContentWindow(Window w, X11.Display dpy,
                                                   X11.Window win, Point offset) {
            if ((w instanceof Frame && !((Frame)w).isUndecorated())
                || (w instanceof Dialog && !((Dialog)w).isUndecorated())) {
                X11 x11 = X11.INSTANCE;
                X11.WindowByReference rootp = new X11.WindowByReference();
                X11.WindowByReference parentp = new X11.WindowByReference();
                PointerByReference childrenp = new PointerByReference();
                IntByReference countp = new IntByReference();
                x11.XQueryTree(dpy, win, rootp, parentp, childrenp, countp);
                Pointer p = childrenp.getValue();
                int[] ids = p.getIntArray(0, countp.getValue());
                for (int i=0;i < ids.length;i++) {
                    // TODO: more verification of correct window?
                    X11.Window child = new X11.Window(ids[i]);
                    X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                    x11.XGetWindowAttributes(dpy, child, xwa);
                    offset.x = -xwa.x;
                    offset.y = -xwa.y;
                    win = child;
                    break;
                }
                if (p != null) {
                    x11.XFree(p);
                }
            }
            return win;
        }
View Full Code Here

            if (!isWindowAlphaSupported()) {
                throw new UnsupportedOperationException("This X11 display does not provide a 32-bit visual");
            }
            Runnable action = new Runnable() {
                public void run() {
                    X11 x11 = X11.INSTANCE;
                    Display dpy = x11.XOpenDisplay(null);
                    if (dpy == null) {
            return;
          }
                    try {
                        X11.Window win = getDrawable(w);
                        if (alpha == 1f) {
                            x11.XDeleteProperty(dpy, win,
                                                x11.XInternAtom(dpy, OPACITY,
                                                                false));
                        }
                        else {
                            int opacity = (int)((long)(alpha * OPAQUE) & 0xFFFFFFFF);
                            IntByReference patom = new IntByReference(opacity);
                            x11.XChangeProperty(dpy, win,
                                                x11.XInternAtom(dpy, OPACITY,
                                                                false),
                                                X11.XA_CARDINAL, 32,
                                                X11.PropModeReplace,
                                                patom.getPointer(), 1);
                        }
                    }
                    finally {
                        x11.XCloseDisplay(dpy);
                    }
                }
            };
            whenDisplayable(w, action);
        }
View Full Code Here

        }

        private void setWindowShape(final Window w, final PixmapSource src) {
            Runnable action = new Runnable() {
                public void run() {
                    X11 x11 = X11.INSTANCE;
                    Display dpy = x11.XOpenDisplay(null);
                    if (dpy == null) {
                        return;
                    }
                    Pixmap pm = null;
                    try {
                        X11.Window win = getDrawable(w);
                        pm = src.getPixmap(dpy, win);
                        Xext ext = Xext.INSTANCE;
                        ext.XShapeCombineMask(dpy, win, X11.Xext.ShapeBounding,
                                              0, 0, pm == null ? Pixmap.None : pm,
                                              X11.Xext.ShapeSet);
                    }
                    finally {
                        if (pm != null) {
                            x11.XFreePixmap(dpy, pm);
                        }
                        x11.XCloseDisplay(dpy);
                    }
                    setForceHeavyweightPopups(getWindow(w), pm != null);
                }
            };
            whenDisplayable(w, action);
View Full Code Here

            });
        }

    private Pixmap createBitmap(final Display dpy, X11.Window win,
        Rectangle[] rectangles) {
      final X11 x11 = X11.INSTANCE;
      Rectangle bounds = new Rectangle();
      if (rectangles.length > 0) {
        bounds.setBounds(rectangles[0]);
        for (int i = 1; i < rectangles.length; i++) {
          Rectangle.union(bounds, rectangles[i], bounds);
        }
      }
      int width = bounds.x + bounds.width;
      int height = bounds.y + bounds.height;
      final Pixmap pm = x11.XCreatePixmap(dpy, win, width, height, 1);
      final GC gc = x11.XCreateGC(dpy, pm, new NativeLong(0), null);
      if (gc == null) {
        return null;
      }
      x11.XSetForeground(dpy, gc, new NativeLong(0));
      x11.XFillRectangle(dpy, pm, gc, 0, 0, width, height);
      final int UNMASKED = 1;
      x11.XSetForeground(dpy, gc, new NativeLong(UNMASKED));
      X11.XWindowAttributes atts = new X11.XWindowAttributes();
      int status = x11.XGetWindowAttributes(dpy, win, atts);
      if (status == 0) {
        return null;
      }
      try {
        for (int i = 0; i < rectangles.length; i++) {
          Rectangle rect = rectangles[i];
          x11.XFillRectangle(dpy, pm, gc, rect.x, rect.y, rect.width,
              rect.height);
        }
      } finally {
        x11.XFreeGC(dpy, gc);
      }
      return pm;
    }
View Full Code Here

TOP

Related Classes of com.sun.jna.examples.unix.X11.Xext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.