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