1 /*
   2  * Copyright (c) 2013, 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;
  27 
  28 import com.sun.glass.ui.Application;
  29 
  30 public class UInput {
  31 
  32     private static final boolean isMonocle;
  33     /** Log events? */
  34     private static final Boolean verbose = Boolean.getBoolean("verbose");
  35     /** If sync is set then we dispatch events on the application thread */
  36     private static final Boolean sync = Boolean.getBoolean("sync");
  37     private final NativeUInput nativeUInput = createNativeUInput();
  38 
  39     static {
  40         if (System.getProperty("glass.platform") == null) {
  41             System.setProperty("glass.platform", "Monocle");
  42             System.setProperty("monocle.platform", "Headless");
  43             System.setProperty("prism.order", "sw");
  44             System.setProperty("com.sun.javafx.gestures.zoom", "true");
  45             System.setProperty("com.sun.javafx.gestures.rotate", "true");
  46             System.setProperty("com.sun.javafx.gestures.scroll", "true");
  47         }
  48         isMonocle = "Monocle".equals(System.getProperty("glass.platform"));
  49         setup();
  50     }
  51 
  52     public UInput() {
  53         nativeUInput.setup();
  54         nativeUInput.init();
  55     }
  56 
  57     private static NativeUInput createNativeUInput() {
  58         if (isMonocle) {
  59             return new MonocleUInput();
  60         } else {
  61             return new LensUInput();
  62         }
  63     }
  64 
  65     public void dispose() {
  66         nativeUInput.dispose();
  67     }
  68 
  69     public static void setup() {
  70         createNativeUInput().setup();
  71     }
  72 
  73     public void processLines(String[] lines) {
  74         for (String line : lines) {
  75             processLine(line);
  76         }
  77     }
  78 
  79     public void waitForQuiet() throws InterruptedException {
  80         nativeUInput.waitForQuiet();
  81     }
  82 
  83     public void write(byte[] data, int offset, int length) {
  84         if (sync && !Application.isEventThread()) {
  85             Application.invokeAndWait(() -> nativeUInput.write(data, offset, length));
  86         } else {
  87             nativeUInput.write(data, offset, length);
  88         }
  89     }
  90 
  91     public int writeTime(byte[] data, int offset) {
  92         return nativeUInput.writeTime(data, offset);
  93     }
  94 
  95     public int writeCode(byte[] data, int offset, String code) {
  96         return nativeUInput.writeCode(data, offset, code);
  97     }
  98 
  99     public int writeValue(byte[] data, int offset, String value) {
 100         return nativeUInput.writeValue(data, offset, value);
 101     }
 102 
 103     public int writeValue(byte[] data, int offset, int value) {
 104         return nativeUInput.writeValue(data, offset, value);
 105     }
 106 
 107     public int writeLine(byte[] data, int offset, String line) {
 108         String[] args = line.split(" ");
 109         offset = writeTime(data, offset);
 110         offset = writeCode(data, offset, args[0]);
 111         if (args.length > 1) {
 112             offset = writeCode(data, offset, args[1]);
 113         } else {
 114             offset = writeCode(data, offset, "0");
 115         }
 116         if (args.length > 2) {
 117             offset = writeValue(data, offset, args[2]);
 118         } else {
 119             offset = writeValue(data, offset, 0);
 120         }
 121         return offset;
 122     }
 123 
 124     public void processLine(String line) {
 125         if (sync && !Application.isEventThread()) {
 126             Application.invokeAndWait(() -> processLineImpl(line));
 127         } else {
 128             processLineImpl(line);
 129         }
 130     }
 131 
 132     private void processLineImpl(String line) {
 133         if (verbose) {
 134             System.out.println(line);
 135         }
 136         // ignore anything after a '#'
 137         int i = line.indexOf('#');
 138         if (i >= 0) {
 139             line = line.substring(0, i);
 140         }
 141         // ignore anything before the last colon (to strip out logging noise)
 142         line = line.substring(line.lastIndexOf(':') + 1).trim();
 143         if (line.length() > 0) {
 144             String[] args = line.split(" ");
 145             switch (args.length) {
 146                 case 0: break;
 147                 case 1: nativeUInput.processLine1(line, args[0]); break;
 148                 case 2: nativeUInput.processLine2(line, args[0], args[1]); break;
 149                 case 3: nativeUInput.processLine3(line, args[0], args[1], args[2]); break;
 150                 default:
 151                     throw new IllegalArgumentException(line);
 152             }
 153         }
 154     }
 155 
 156 }