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
018/**
019 * Interface to access some low level POSIX functions, loaded by
020 * <a href="http://fusesource.github.io/hawtjni/">HawtJNI</a> Runtime
021 * as <code>jansi</code> library.
022 *
023 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
024 * @see JansiLoader
025 */
026@SuppressWarnings("unused")
027public class CLibrary {
028
029    //
030    // Initialization
031    //
032
033    public static final boolean LOADED;
034
035    static {
036        LOADED = JansiLoader.initialize();
037        if (LOADED) {
038            init();
039        }
040    }
041
042    private static native void init();
043
044    //
045    // Constants
046    //
047
048    public static int STDOUT_FILENO = 1;
049
050    public static int STDERR_FILENO = 2;
051
052    public static boolean HAVE_ISATTY;
053
054    public static boolean HAVE_TTYNAME;
055
056    public static int TCSANOW;
057    public static int TCSADRAIN;
058    public static int TCSAFLUSH;
059    public static long TIOCGETA;
060    public static long TIOCSETA;
061    public static long TIOCGETD;
062    public static long TIOCSETD;
063    public static long TIOCGWINSZ;
064    public static long TIOCSWINSZ;
065
066    /**
067     * test whether a file descriptor refers to a terminal
068     *
069     * @param fd file descriptor
070     * @return isatty() returns 1 if fd is an open file descriptor referring to a
071     * terminal; otherwise 0 is returned, and errno is set to indicate the
072     * error
073     * @see <a href="http://man7.org/linux/man-pages/man3/isatty.3.html">ISATTY(3) man-page</a>
074     * @see <a href="http://man7.org/linux/man-pages/man3/isatty.3p.html">ISATTY(3P) man-page</a>
075     */
076    public static native int isatty(int fd);
077
078    public static native String ttyname(int filedes);
079
080    /**
081     * The openpty() function finds an available pseudoterminal and returns
082     * file descriptors for the master and slave in amaster and aslave.
083     *
084     * @param amaster master return value
085     * @param aslave  slave return value
086     * @param name    filename return value
087     * @param termios optional pty attributes
088     * @param winsize optional size
089     * @return 0 on success
090     * @see <a href="http://man7.org/linux/man-pages/man3/openpty.3.html">OPENPTY(3) man-page</a>
091     */
092    public static native int openpty(
093            int[] amaster,
094            int[] aslave,
095            byte[] name,
096            Termios termios,
097            WinSize winsize);
098
099    public static native int tcgetattr(
100            int filedes,
101            Termios termios);
102
103    public static native int tcsetattr(
104            int filedes,
105            int optional_actions,
106            Termios termios);
107
108    /**
109     * Control a STREAMS device.
110     *
111     * @see <a href="http://man7.org/linux/man-pages/man3/ioctl.3p.html">IOCTL(3P) man-page</a>
112     */
113    public static native int ioctl(
114            int filedes,
115            long request,
116            int[] params);
117
118    public static native int ioctl(
119            int filedes,
120            long request,
121            WinSize params);
122
123    /**
124     * Window sizes.
125     *
126     * @see <a href="http://man7.org/linux/man-pages/man4/tty_ioctl.4.html">IOCTL_TTY(2) man-page</a>
127     */
128    public static class WinSize {
129
130        static {
131            JansiLoader.initialize();
132            init();
133        }
134
135        private static native void init();
136
137        public static int SIZEOF;
138
139        public short ws_row;
140        public short ws_col;
141        public short ws_xpixel;
142        public short ws_ypixel;
143
144        public WinSize() {
145        }
146
147        public WinSize(short ws_row, short ws_col) {
148            this.ws_row = ws_row;
149            this.ws_col = ws_col;
150        }
151    }
152
153    /**
154     * termios structure for termios functions, describing a general terminal interface that is
155     * provided to control asynchronous communications ports
156     *
157     * @see <a href="http://man7.org/linux/man-pages/man3/termios.3.html">TERMIOS(3) man-page</a>
158     */
159    public static class Termios {
160
161        static {
162            JansiLoader.initialize();
163            init();
164        }
165
166        private static native void init();
167
168        public static int SIZEOF;
169
170        public long c_iflag;
171        public long c_oflag;
172        public long c_cflag;
173        public long c_lflag;
174        public byte[] c_cc = new byte[32];
175        public long c_ispeed;
176        public long c_ospeed;
177    }
178
179}