1 /*
   2  * Copyright (c) 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.linux;
  27 
  28 import com.sun.glass.ui.monocle.util.C;
  29 import com.sun.glass.utils.NativeLibLoader;
  30 
  31 import java.nio.ByteBuffer;
  32 import java.security.Permission;
  33 import com.sun.glass.utils.NativeLibLoader;
  34 
  35 public class LinuxSystem {
  36 
  37     private static Permission permission = new RuntimePermission("loadLibrary.*");
  38 
  39     private static LinuxSystem instance = new LinuxSystem();
  40 
  41     public static LinuxSystem getLinuxSystem() {
  42         checkPermissions();
  43         return instance;
  44     }
  45 
  46     private static void checkPermissions() {
  47         SecurityManager security = System.getSecurityManager();
  48         if (security != null) {
  49             security.checkPermission(permission);
  50         }
  51     }
  52 
  53     private LinuxSystem() {
  54         loadLibrary();
  55     }
  56 
  57     public void loadLibrary() {
  58         NativeLibLoader.loadLibrary("glass_monocle");
  59     }
  60 
  61     // dlfcn.h
  62     public static final int RTLD_LAZY = 0x00001;
  63     public static final int RTLD_GLOBAL = 0x00100;
  64 
  65 
  66     // stdlib.h
  67     public native void setenv(String key, String value, boolean overwrite);
  68 
  69     // fcntl.h
  70 
  71     public static final int O_RDONLY = 0;
  72     public static final int O_WRONLY = 1;
  73     public static final int O_RDWR = 2;
  74     public static final int O_NONBLOCK = 00004000;
  75 
  76     public native long open(String path, int flags);
  77 
  78     // unistd.h
  79     public native int close(long fd);
  80     public native long lseek(long fd, long offset, int whence);
  81     public native long write(long fd, ByteBuffer buf, int position, int limit);
  82     public native long read(long fd, ByteBuffer buf, int position, int limit);
  83 
  84     public static final int SEEK_SET = 0;
  85 
  86     // input.h
  87 
  88     static class InputAbsInfo extends C.Structure {
  89         @Override
  90         public native int sizeof();
  91         static native int getValue(long p);
  92         static native int getMinimum(long p);
  93         static native int getMaximum(long p);
  94         static native int getFuzz(long p);
  95         static native int getFlat(long p);
  96         static native int getResolution(long p);
  97     }
  98 
  99     native int EVIOCGABS(int type);
 100 
 101     // fb.h
 102 
 103     public static final int FBIOGET_VSCREENINFO = 0x4600;
 104     public static final int FBIOPUT_VSCREENINFO = 0x4601;
 105     public static final int FBIOPAN_DISPLAY = 0x4606;
 106     public static final int FBIOBLANK = 0x4611;
 107 
 108     public static final int FB_BLANK_UNBLANK = 0;
 109     public static final int FB_ACTIVATE_NOW = 0;
 110     public static final int FB_ACTIVATE_VBL = 16;
 111 
 112     public static class FbVarScreenInfo extends C.Structure {
 113         public FbVarScreenInfo() {
 114             checkPermissions();
 115         }
 116         @Override
 117         public native int sizeof();
 118         public native int getBitsPerPixel(long p);
 119         public native int getXRes(long p);
 120         public native int getYRes(long p);
 121         public native int getXResVirtual(long p);
 122         public native int getYResVirtual(long p);
 123         public native int getOffsetX(long p);
 124         public native int getOffsetY(long p);
 125         public native void setRes(long p, int x, int y);
 126         public native void setVirtualRes(long p, int x, int y);
 127         public native void setOffset(long p, int x, int y);
 128         public native void setActivate(long p, int activate);
 129         public native void setBitsPerPixel(long p, int bpp);
 130         public native void setRed(long p, int length, int offset);
 131         public native void setGreen(long p, int length, int offset);
 132         public native void setBlue(long p, int length, int offset);
 133         public native void setTransp(long p, int length, int offset);
 134     }
 135 
 136     // ioctl.h
 137 
 138     public native int ioctl(long fd, int request, long data);
 139     public native int IOW(int type, int number, int size);
 140     public native int IOR(int type, int number, int size);
 141     public native int IOWR(int type, int number, int size);
 142 
 143     // stropts.h
 144     private static int __SID = ('S' << 8);
 145     public static int I_FLUSH = __SID | 5;
 146 
 147     public static int FLUSHRW = 0x03;
 148 
 149     // errno.h
 150     public native int errno();
 151 
 152     public static final int ENXIO = 6;
 153     public static final int EAGAIN = 11;
 154 
 155     // string.h
 156     public native String strerror(int errnum);
 157 
 158     // dlfcn.h
 159     public native long dlopen(String filename, int flag);
 160     public native String dlerror();
 161     public native long dlsym(long handle, String symbol);
 162     public native int dlclose(long handle);
 163 
 164     // mman.h
 165     public static final long PROT_READ = 0x1l;
 166     public static final long PROT_WRITE = 0x2l;
 167     public static final long MAP_SHARED = 0x1l;
 168     public static final long MAP_FAILED = 0xffffffffl;
 169     public native long mmap(long addr, long length, long prot, long flags,
 170                             long fd, long offset);
 171     public native int munmap(long addr, long length);
 172 
 173     public String getErrorMessage() {
 174         return strerror(errno());
 175     }
 176 
 177     // stat.h
 178     public static int S_IRWXU = 00700;
 179 
 180     public native int mkfifo(String pathname, int mode);
 181 
 182 }