1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.glass.ui.monocle.x11;
  27 
  28 import com.sun.glass.ui.Pixels;
  29 import com.sun.glass.ui.monocle.NativeScreen;
  30 import com.sun.glass.ui.monocle.AcceleratedScreen;
  31 
  32 import java.nio.Buffer;
  33 import java.nio.IntBuffer;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;
  36 
  37 public class X11Screen implements NativeScreen {
  38 
  39     private int depth;
  40     private int nativeFormat;
  41     private int width;
  42     private int height;
  43     private long nativeHandle;
  44     private long display;
  45 
  46     public X11Screen(boolean showCursor) {
  47         display = X.XOpenDisplay(null);
  48         if (display == 0l) {
  49             throw new NullPointerException("Cannot open X11 display");
  50         }
  51         long screen = X.DefaultScreenOfDisplay(display);
  52         X.XSetWindowAttributes attrs = new X.XSetWindowAttributes();
  53         attrs.setEventMask(attrs.p,
  54                            X.ButtonPressMask | X.ButtonReleaseMask
  55                                    | X.PointerMotionMask);
  56         long cwMask = X.CWEventMask;
  57         if (!showCursor) {
  58             cwMask |= X.CWCursorMask;
  59             attrs.setCursor(attrs.p, X.None);
  60         }
  61         int x = 0;
  62         int y = 0;
  63         int w = X.WidthOfScreen(screen);
  64         int h = X.HeightOfScreen(screen);
  65         String geometry = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("x11.geometry"));
  66         if (geometry != null) {
  67             try {
  68                 String size;
  69                 if (geometry.contains(",")) {
  70                     // use the first two numbers for x and y
  71                     String location;
  72                     int i = geometry.indexOf("+");
  73                     if (i >= 0) {
  74                         location = geometry.substring(0, i);
  75                         size = geometry.substring(i + 1);
  76                     } else {
  77                         location = geometry;
  78                         size = "";
  79                     }
  80                     i = location.indexOf(",");
  81                     x = Integer.parseInt(location.substring(0, i));
  82                     y = Integer.parseInt(location.substring(i + 1));
  83                 } else {
  84                     size = geometry;
  85                 }
  86                 if (size.length() > 0) {
  87                     int i = size.indexOf("x");
  88                     w = Integer.parseInt(size.substring(0, i));
  89                     h = Integer.parseInt(size.substring(i + 1));
  90                 }
  91             } catch (NumberFormatException e) {
  92                 System.err.println("Cannot parse geometry string: '"
  93                         + geometry + "'");
  94             }
  95         }
  96         long window = X.XCreateWindow(
  97                 display,
  98                 X.RootWindowOfScreen(screen),
  99                 x, y, w, h,
 100                 0, // border width
 101                 X.CopyFromParent, // depth
 102                 X.InputOutput, // class
 103                 X.CopyFromParent, // visual
 104                 cwMask,
 105                 attrs.p);
 106         X.XMapWindow(display, window);
 107         X.XStoreName(display, window, "JavaFX framebuffer container");
 108         X.XSync(display, false);
 109         int[] widthA = new int[1];
 110         int[] heightA = new int[1];
 111         int[] depthA = new int[1];
 112         X.XGetGeometry(display, window, null, null, null, widthA, heightA, null, depthA);
 113         width = widthA[0];
 114         height = heightA[0];
 115         depth = depthA[0];
 116         nativeFormat = Pixels.Format.BYTE_BGRA_PRE;
 117         nativeHandle = window;
 118     }
 119 
 120     @Override
 121     public int getDepth() {
 122         return depth;
 123     }
 124 
 125     @Override
 126     public int getNativeFormat() {
 127         return nativeFormat;
 128     }
 129 
 130     @Override
 131     public int getWidth() {
 132         return width;
 133     }
 134 
 135     @Override
 136     public int getHeight() {
 137         return height;
 138     }
 139 
 140     @Override
 141     public long getNativeHandle() {
 142         return nativeHandle;
 143     }
 144 
 145     @Override
 146     public int getDPI() {
 147         return 96;
 148     }
 149 
 150     @Override
 151     public void shutdown() {
 152     }
 153 
 154     long getDisplay() {
 155         return display;
 156     }
 157 
 158     @Override
 159     public void uploadPixels(Buffer b,
 160                              int x, int y, int width, int height,
 161                              float alpha) {
 162         // TODO: upload pixels to X11 window
 163     }
 164 
 165     @Override
 166     public void swapBuffers() {
 167     }
 168 
 169     @Override
 170     public IntBuffer getScreenCapture() {
 171         return null;
 172     }
 173 }