1 /*
   2  * Copyright (c) 2010, 2013, 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 
  31 import java.nio.Buffer;
  32 import java.nio.IntBuffer;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 
  36 public class X11Screen extends NativeScreen {
  37 
  38     private int depth;
  39     private int nativeFormat;
  40     private int width;
  41     private int height;
  42     private long nativeHandle;
  43     private long display;
  44 
  45     public X11Screen(boolean showCursor) {
  46         display = X.XOpenDisplay(null);
  47         if (display == 0l) {
  48             throw new NullPointerException("Cannot open X11 display");
  49         }
  50         long screen = X.DefaultScreenOfDisplay(display);
  51         X.XSetWindowAttributes attrs = new X.XSetWindowAttributes();
  52         attrs.setEventMask(attrs.p,
  53                            X.ButtonPressMask | X.ButtonReleaseMask
  54                                    | X.PointerMotionMask);
  55         long cwMask = X.CWEventMask;
  56         if (!showCursor) {
  57             cwMask |= X.CWCursorMask;
  58             attrs.setCursor(attrs.p, X.None);
  59         }
  60         int x = 0;
  61         int y = 0;
  62         int w = X.WidthOfScreen(screen);
  63         int h = X.HeightOfScreen(screen);
  64         String geometry = AccessController.doPrivileged(new PrivilegedAction<String>() {
  65             @Override
  66             public String run() {
  67                 return System.getProperty("x11.geometry");
  68             }
  69         });
  70         if (geometry != null) {
  71             try {
  72                 String size;
  73                 if (geometry.contains(",")) {
  74                     // use the first two numbers for x and y
  75                     String location;
  76                     int i = geometry.indexOf("+");
  77                     if (i >= 0) {
  78                         location = geometry.substring(0, i);
  79                         size = geometry.substring(i + 1);
  80                     } else {
  81                         location = geometry;
  82                         size = "";
  83                     }
  84                     i = location.indexOf(",");
  85                     x = Integer.parseInt(location.substring(0, i));
  86                     y = Integer.parseInt(location.substring(i + 1));
  87                 } else {
  88                     size = geometry;
  89                 }
  90                 if (size.length() > 0) {
  91                     int i = size.indexOf("x");
  92                     w = Integer.parseInt(size.substring(0, i));
  93                     h = Integer.parseInt(size.substring(i + 1));
  94                 }
  95             } catch (NumberFormatException e) {
  96                 System.err.println("Cannot parse geometry string: '"
  97                         + geometry + "'");
  98             }
  99         }
 100         long window = X.XCreateWindow(
 101                 display,
 102                 X.RootWindowOfScreen(screen),
 103                 x, y, w, h,
 104                 0, // border width
 105                 X.CopyFromParent, // depth
 106                 X.InputOutput, // class
 107                 X.CopyFromParent, // visual
 108                 cwMask,
 109                 attrs.p);
 110         X.XMapWindow(display, window);
 111         X.XStoreName(display, window, "JavaFX EGL/framebuffer container");
 112         X.XSync(display, false);
 113         int[] widthA = new int[1];
 114         int[] heightA = new int[1];
 115         int[] depthA = new int[1];
 116         X.XGetGeometry(display, window, null, null, null, widthA, heightA, null, depthA);
 117         width = widthA[0];
 118         height = heightA[0];
 119         depth = depthA[0];
 120         nativeFormat = Pixels.Format.BYTE_BGRA_PRE;
 121         nativeHandle = window;
 122     }
 123 
 124     @Override
 125     public int getDepth() {
 126         return depth;
 127     }
 128 
 129     @Override
 130     public int getNativeFormat() {
 131         return nativeFormat;
 132     }
 133 
 134     @Override
 135     public int getWidth() {
 136         return width;
 137     }
 138 
 139     @Override
 140     public int getHeight() {
 141         return height;
 142     }
 143 
 144     @Override
 145     public long getNativeHandle() {
 146         return nativeHandle;
 147     }
 148 
 149     @Override
 150     public int getDPI() {
 151         return 96;
 152     }
 153 
 154     @Override
 155     public void shutdown() {
 156     }
 157 
 158     long getDisplay() {
 159         return display;
 160     }
 161 
 162     @Override
 163     public void uploadPixels(Buffer b,
 164                              int x, int y, int width, int height,
 165                              float alpha) {
 166         // TODO: upload pixels to X11 window
 167     }
 168 
 169     @Override
 170     public void swapBuffers() {
 171     }
 172 
 173     @Override
 174     public IntBuffer getScreenCapture() {
 175         return null;
 176     }
 177 }