001/*
002 * Copyright (C) 2009-2017 the original author(s).
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fusesource.jansi.internal;
017
018import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT;
019import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER;
020
021import static org.fusesource.hawtjni.runtime.ArgFlag.*;
022
023import org.fusesource.hawtjni.runtime.ArgFlag;
024import org.fusesource.hawtjni.runtime.ClassFlag;
025import org.fusesource.hawtjni.runtime.JniArg;
026import org.fusesource.hawtjni.runtime.JniClass;
027import org.fusesource.hawtjni.runtime.JniField;
028import org.fusesource.hawtjni.runtime.JniMethod;
029import org.fusesource.hawtjni.runtime.Library;
030
031/**
032 * Interface to access some low level POSIX functions.
033 * 
034 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
035 */
036@JniClass()
037public class CLibrary {
038    
039    private static final Library LIBRARY = new Library("jansi", CLibrary.class);    
040    static {
041        LIBRARY.load();
042        init();
043    }
044
045    @JniMethod(flags={CONSTANT_INITIALIZER})
046    private static native void init();
047
048    @JniField(flags={CONSTANT}, conditional="defined(STDIN_FILENO)")
049    public static int STDIN_FILENO;
050    @JniField(flags={CONSTANT}, conditional="defined(STDOUT_FILENO)")
051    public static int STDOUT_FILENO;
052    @JniField(flags={CONSTANT}, conditional="defined(STDERR_FILENO)")
053    public static int STDERR_FILENO;
054
055    @JniField(flags={CONSTANT}, accessor="1", conditional="defined(HAVE_ISATTY)")
056    public static boolean HAVE_ISATTY;
057    @JniMethod(conditional="defined(HAVE_ISATTY)")
058    public static native int isatty(
059            @JniArg int fd);
060
061    @JniMethod(conditional="FALSE")
062    public static native String ttyname(
063            @JniArg int filedes);
064
065    @JniMethod(conditional="defined(HAVE_OPENPTY)")
066    public static native int openpty(
067            @JniArg(cast="int *", flags={NO_IN}) int[] amaster,
068            @JniArg(cast="int *", flags={NO_IN}) int[] aslave,
069            @JniArg(cast="char *", flags={NO_IN}) byte[] name,
070            @JniArg(cast="struct termios *", flags={NO_OUT}) Termios termios,
071            @JniArg(cast="struct winsize *", flags={NO_OUT}) WinSize winsize);
072
073    @JniMethod(conditional="defined(HAVE_TCGETATTR)")
074    public static native int tcgetattr(
075            @JniArg int filedes,
076            @JniArg(cast="struct termios *", flags={NO_IN}) Termios termios);
077
078    @JniMethod(conditional="defined(HAVE_TCSETATTR)")
079    public static native int tcsetattr(
080            @JniArg int filedes,
081            @JniArg int optional_actions,
082            @JniArg(cast="struct termios *", flags={NO_OUT}) Termios termios);
083
084    /*
085     * Commands passed to tcsetattr() for setting the termios structure.
086     */
087    @JniField(flags={CONSTANT}, conditional="defined(TCSANOW)")
088    public static int TCSANOW;              /* make change immediate */
089    @JniField(flags={CONSTANT}, conditional="defined(TCSADRAIN)")
090    public static int TCSADRAIN;                /* drain output, then change */
091    @JniField(flags={CONSTANT}, conditional="defined(TCSAFLUSH)")
092    public static int TCSAFLUSH;                /* drain output, flush input */
093
094    @JniField(flags={CONSTANT}, conditional="defined(TIOCGETA)")
095    public static long TIOCGETA;
096    @JniField(flags={CONSTANT}, conditional="defined(TIOCSETA)")
097    public static long TIOCSETA;
098    @JniField(flags={CONSTANT}, conditional="defined(TIOCGETD)")
099    public static long TIOCGETD;
100    @JniField(flags={CONSTANT}, conditional="defined(TIOCSETD)")
101    public static long TIOCSETD;
102    @JniField(flags={CONSTANT}, conditional="defined(TIOCGWINSZ)")
103    public static long TIOCGWINSZ;
104    @JniField(flags={CONSTANT}, conditional="defined(TIOCSWINSZ)")
105    public static long TIOCSWINSZ;
106
107    @JniMethod(conditional="defined(HAVE_IOCTL)")
108    public static native int ioctl(
109            @JniArg int filedes,
110            @JniArg long request,
111            @JniArg int[] params);
112
113    @JniMethod(conditional="defined(HAVE_IOCTL)")
114    public static native int ioctl(
115            @JniArg int filedes,
116            @JniArg long request,
117            @JniArg(flags = ArgFlag.POINTER_ARG) WinSize params);
118
119    @JniClass(flags={ClassFlag.STRUCT}, name="winsize", conditional="defined(HAVE_IOCTL)")
120    public static class WinSize {
121
122        static {
123            LIBRARY.load();
124            init();
125        }
126
127        @JniMethod(flags={CONSTANT_INITIALIZER})
128        private static native void init();
129        @JniField(flags={CONSTANT}, accessor="sizeof(struct winsize)")
130        public static int SIZEOF;
131
132        @JniField(accessor="ws_row")
133        public short ws_row;
134        @JniField(accessor="ws_col")
135        public short ws_col;
136        @JniField(accessor="ws_xpixel")
137        public short ws_xpixel;
138        @JniField(accessor="ws_ypixel")
139        public short ws_ypixel;
140
141        public WinSize() {
142        }
143
144        public WinSize(short ws_row, short ws_col) {
145            this.ws_row = ws_row;
146            this.ws_col = ws_col;
147        }
148    }
149
150    @JniClass(flags={ClassFlag.STRUCT}, name="termios", conditional = "defined(HAVE_IOCTL)")
151    public static class Termios {
152
153        static {
154            LIBRARY.load();
155            init();
156        }
157
158        @JniMethod(flags={CONSTANT_INITIALIZER})
159        private static native void init();
160        @JniField(flags={CONSTANT}, accessor="sizeof(struct termios)")
161        public static int SIZEOF;
162
163        @JniField(accessor="c_iflag")
164        public long c_iflag;
165        @JniField(accessor="c_oflag")
166        public long c_oflag;
167        @JniField(accessor="c_cflag")
168        public long c_cflag;
169        @JniField(accessor="c_lflag")
170        public long c_lflag;
171        @JniField(accessor="c_cc")
172        public byte[] c_cc = new byte[32];
173        @JniField(accessor="c_ispeed")
174        public long c_ispeed;
175        @JniField(accessor="c_ospeed")
176        public long c_ospeed;
177    }
178
179}