Debian Codegraph @

terminal-jna/src/main/java/org/jline/terminal/impl/jna/osx/OsXNativePty.java

raw
1 /*
2 * Copyright (c) 2002-2016, the original author or authors.
3 *
4 * This software is distributable under the BSD license. See the terms of the
5 * BSD license in the documentation provided with this software.
6 *
7 * http://www.opensource.org/licenses/bsd-license.php
8 */
9 package org.jline.terminal.impl.jna.osx;
10
11 import java.io.FileDescriptor;
12 import java.io.IOException;
13
14 import com.sun.jna.Native;
15 import com.sun.jna.NativeLong;
16 import com.sun.jna.Platform;
17 import org.jline.terminal.Attributes;
18 import org.jline.terminal.Size;
19 import org.jline.terminal.impl.jna.JnaNativePty;
20
21 import static org.jline.terminal.impl.jna.osx.CLibrary.TCSANOW;
22 import static org.jline.terminal.impl.jna.osx.CLibrary.TIOCGWINSZ;
23 import static org.jline.terminal.impl.jna.osx.CLibrary.TIOCSWINSZ;
24 import static org.jline.terminal.impl.jna.osx.CLibrary.termios;
25 import static org.jline.terminal.impl.jna.osx.CLibrary.winsize;
26
27 public class OsXNativePty extends JnaNativePty {
28
29 private static final CLibrary C_LIBRARY = (CLibrary) Native.loadLibrary(Platform.C_LIBRARY_NAME, CLibrary.class);
30
31 public static OsXNativePty current() throws IOException {
32 int slave = 0;
33 byte[] buf = new byte[64];
34 C_LIBRARY.ttyname_r(slave, buf, buf.length);
35 int len = 0;
36 while (buf[len] != 0) {
37 len++;
38 }
39 String name = new String(buf, 0, len);
40 return new OsXNativePty(-1, null, slave, FileDescriptor.in, name);
41 }
42
43 public static OsXNativePty open(Attributes attr, Size size) throws IOException {
44 int[] master = new int[1];
45 int[] slave = new int[1];
46 byte[] buf = new byte[64];
47 C_LIBRARY.openpty(master, slave, buf,
48 attr != null ? new termios(attr) : null,
49 size != null ? new winsize(size) : null);
50 int len = 0;
51 while (buf[len] != 0) {
52 len++;
53 }
54 String name = new String(buf, 0, len);
55 return new OsXNativePty(master[0], newDescriptor(master[0]), slave[0], newDescriptor(slave[0]), name);
56 }
57
58 public OsXNativePty(int master, FileDescriptor masterFD, int slave, FileDescriptor slaveFD, String name) {
59 super(master, masterFD, slave, slaveFD, name);
60 }
61
62 @Override
63 public Attributes getAttr() throws IOException {
64 termios termios = new termios();
65 C_LIBRARY.tcgetattr(getSlave(), termios);
66 return termios.toAttributes();
67 }
68
69 @Override
70 public void setAttr(Attributes attr) throws IOException {
71 termios termios = new termios(attr);
72 C_LIBRARY.tcsetattr(getSlave(), TCSANOW, termios);
73 }
74
75 @Override
76 public Size getSize() throws IOException {
77 winsize sz = new winsize();
78 C_LIBRARY.ioctl(getSlave(), new NativeLong(TIOCGWINSZ), sz);
79 return sz.toSize();
80 }
81
82 @Override
83 public void setSize(Size size) throws IOException {
84 winsize sz = new winsize(size);
85 C_LIBRARY.ioctl(getSlave(), new NativeLong(TIOCSWINSZ), sz);
86 }
87
88 }