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.headless;
  27 
  28 import com.sun.glass.ui.Pixels;
  29 import com.sun.glass.ui.monocle.Framebuffer;
  30 import com.sun.glass.ui.monocle.NativeScreen;
  31 
  32 import java.nio.Buffer;
  33 import java.nio.ByteBuffer;
  34 import java.nio.ByteOrder;
  35 import java.nio.IntBuffer;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 
  39 public class HeadlessScreen implements NativeScreen {
  40 
  41     protected int depth;
  42     protected int width;
  43     protected int height;
  44     protected Framebuffer fb;
  45 
  46     public HeadlessScreen() {
  47         this(1280, 800, 32);
  48     }
  49 
  50     protected HeadlessScreen(int defaultWidth,
  51                              int defaultHeight,
  52                              int defaultDepth) {
  53         this.width = defaultWidth;
  54         this.height = defaultHeight;
  55         this.depth = defaultDepth;
  56         String geometry = AccessController.doPrivileged(new PrivilegedAction<String>() {
  57             @Override
  58             public String run() {
  59                 return System.getProperty("headless.geometry");
  60             }
  61         });
  62         if (geometry != null && geometry.indexOf('x') > 0) {
  63             try {
  64                 int i = geometry.indexOf("x");
  65                 width = Integer.parseInt(geometry.substring(0, i));
  66                 int j = geometry.indexOf("-", i + 1);
  67                 if (j > 0) {
  68                     depth = Integer.parseInt(geometry.substring(j + 1));
  69                 } else {
  70                     j = geometry.length();
  71                 }
  72                 height = Integer.parseInt(geometry.substring(i + 1, j));
  73             } catch (NumberFormatException e) {
  74                 System.err.println("Cannot parse geometry string: '"
  75                         + geometry + "'");
  76             }
  77         }
  78         ByteBuffer bb = ByteBuffer.allocate(width * height * (depth >>> 3));
  79         bb.order(ByteOrder.nativeOrder());
  80         fb = new Framebuffer(bb, width, height, depth, true);
  81     }
  82 
  83     @Override
  84     public int getDepth() {
  85         return depth;
  86     }
  87 
  88     @Override
  89     public int getNativeFormat() {
  90         return Pixels.Format.BYTE_BGRA_PRE;
  91     }
  92 
  93     @Override
  94     public int getWidth() {
  95         return width;
  96     }
  97 
  98     @Override
  99     public int getHeight() {
 100         return height;
 101     }
 102 
 103     @Override
 104     public long getNativeHandle() {
 105         return 1l;
 106     }
 107 
 108     @Override
 109     public int getDPI() {
 110         return 96;
 111     }
 112 
 113     @Override
 114     public void shutdown() {
 115     }
 116 
 117     @Override
 118     public void uploadPixels(Buffer b,
 119                              int x, int y, int width, int height,
 120                              float alpha) {
 121         fb.composePixels(b, x, y, width, height, alpha);
 122     }
 123 
 124     @Override
 125     public void swapBuffers() {
 126         fb.reset();
 127     }
 128 
 129     @Override
 130     public IntBuffer getScreenCapture() {
 131         return fb.getBuffer().asIntBuffer();
 132     }
 133 
 134 }