|
| 1 | +// Initially adapted from https://github.com/jline/jline3/blob/114e9a8f86102245ed9e2e642603f97e11ac962b/terminal-ffm/src/main/java/org/jline/terminal/impl/ffm/CLibrary.java |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2022-2023, the original author(s). |
| 5 | + * |
| 6 | + * This software is distributable under the BSD license. See the terms of the |
| 7 | + * BSD license in the documentation provided with this software. |
| 8 | + * |
| 9 | + * https://opensource.org/licenses/BSD-3-Clause |
| 10 | + */ |
| 11 | +package io.github.alexarchambault.nativeterm.internal; |
| 12 | + |
| 13 | +import java.lang.foreign.*; |
| 14 | +import java.lang.invoke.MethodHandle; |
| 15 | +import java.lang.invoke.MethodHandles; |
| 16 | +import java.lang.invoke.VarHandle; |
| 17 | +import java.lang.foreign.MemoryLayout.PathElement; |
| 18 | + |
| 19 | +import io.github.alexarchambault.nativeterm.TerminalSize; |
| 20 | + |
| 21 | +@SuppressWarnings("restricted") |
| 22 | +class CLibrary { |
| 23 | + |
| 24 | + static VarHandle lookupVarHandle(PathElement... element) { |
| 25 | + VarHandle h = winsize.LAYOUT.varHandle(element); |
| 26 | + |
| 27 | + // the last parameter of the VarHandle is additional offset, hardcode zero: |
| 28 | + h = MethodHandles.insertCoordinates(h, h.coordinateTypes().size() - 1, 0L); |
| 29 | + |
| 30 | + return h; |
| 31 | + } |
| 32 | + |
| 33 | + // Window sizes. |
| 34 | + // @see <a href="http://man7.org/linux/man-pages/man4/tty_ioctl.4.html">IOCTL_TTY(2) man-page</a> |
| 35 | + static class winsize { |
| 36 | + static final GroupLayout LAYOUT; |
| 37 | + private static final VarHandle ws_col; |
| 38 | + private static final VarHandle ws_row; |
| 39 | + |
| 40 | + static { |
| 41 | + LAYOUT = MemoryLayout.structLayout( |
| 42 | + ValueLayout.JAVA_SHORT.withName("ws_row"), |
| 43 | + ValueLayout.JAVA_SHORT.withName("ws_col"), |
| 44 | + ValueLayout.JAVA_SHORT, |
| 45 | + ValueLayout.JAVA_SHORT); |
| 46 | + ws_row = lookupVarHandle(MemoryLayout.PathElement.groupElement("ws_row")); |
| 47 | + ws_col = lookupVarHandle(MemoryLayout.PathElement.groupElement("ws_col")); |
| 48 | + } |
| 49 | + |
| 50 | + private final java.lang.foreign.MemorySegment seg; |
| 51 | + |
| 52 | + winsize() { |
| 53 | + seg = java.lang.foreign.Arena.ofAuto().allocate(LAYOUT); |
| 54 | + } |
| 55 | + |
| 56 | + java.lang.foreign.MemorySegment segment() { |
| 57 | + return seg; |
| 58 | + } |
| 59 | + |
| 60 | + short ws_col() { |
| 61 | + return (short) ws_col.get(seg); |
| 62 | + } |
| 63 | + |
| 64 | + short ws_row() { |
| 65 | + return (short) ws_row.get(seg); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + static MethodHandle ioctl; |
| 70 | + static MethodHandle isatty; |
| 71 | + static MethodHandle tcsetattr; |
| 72 | + static MethodHandle tcgetattr; |
| 73 | + static MethodHandle ttyname_r; |
| 74 | + |
| 75 | + static { |
| 76 | + // methods |
| 77 | + Linker linker = Linker.nativeLinker(); |
| 78 | + SymbolLookup lookup = SymbolLookup.loaderLookup().or(linker.defaultLookup()); |
| 79 | + // https://man7.org/linux/man-pages/man2/ioctl.2.html |
| 80 | + ioctl = linker.downcallHandle( |
| 81 | + lookup.find("ioctl").get(), |
| 82 | + FunctionDescriptor.of( |
| 83 | + ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG, ValueLayout.ADDRESS), |
| 84 | + Linker.Option.firstVariadicArg(2)); |
| 85 | + // https://www.man7.org/linux/man-pages/man3/isatty.3.html |
| 86 | + isatty = linker.downcallHandle( |
| 87 | + lookup.find("isatty").get(), FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_INT)); |
| 88 | + // https://man7.org/linux/man-pages/man3/tcsetattr.3p.html |
| 89 | + tcsetattr = linker.downcallHandle( |
| 90 | + lookup.find("tcsetattr").get(), |
| 91 | + FunctionDescriptor.of( |
| 92 | + ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.ADDRESS)); |
| 93 | + // https://man7.org/linux/man-pages/man3/tcgetattr.3p.html |
| 94 | + tcgetattr = linker.downcallHandle( |
| 95 | + lookup.find("tcgetattr").get(), |
| 96 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.ADDRESS)); |
| 97 | + // https://man7.org/linux/man-pages/man3/ttyname.3.html |
| 98 | + ttyname_r = linker.downcallHandle( |
| 99 | + lookup.find("ttyname_r").get(), |
| 100 | + FunctionDescriptor.of( |
| 101 | + ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG)); |
| 102 | + } |
| 103 | + |
| 104 | + static TerminalSize getTerminalSize(int fd) { |
| 105 | + try { |
| 106 | + winsize ws = new winsize(); |
| 107 | + int res = (int) ioctl.invoke(fd, (long) TIOCGWINSZ, ws.segment()); |
| 108 | + return TerminalSize.of(ws.ws_col(), ws.ws_row()); |
| 109 | + } catch (Throwable e) { |
| 110 | + throw new RuntimeException("Unable to call ioctl(TIOCGWINSZ)", e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // CONSTANTS |
| 115 | + |
| 116 | + private static final int TIOCGWINSZ; |
| 117 | + |
| 118 | + static { |
| 119 | + String osName = System.getProperty("os.name"); |
| 120 | + if (osName.startsWith("Linux")) { |
| 121 | + String arch = System.getProperty("os.arch"); |
| 122 | + boolean isMipsPpcOrSparc = arch.equals("mips") |
| 123 | + || arch.equals("mips64") |
| 124 | + || arch.equals("mipsel") |
| 125 | + || arch.equals("mips64el") |
| 126 | + || arch.startsWith("ppc") |
| 127 | + || arch.startsWith("sparc"); |
| 128 | + TIOCGWINSZ = isMipsPpcOrSparc ? 0x40087468 : 0x00005413; |
| 129 | + } else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) { |
| 130 | + int _TIOC = ('T' << 8); |
| 131 | + TIOCGWINSZ = (_TIOC | 104); |
| 132 | + } else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) { |
| 133 | + TIOCGWINSZ = 0x40087468; |
| 134 | + } else if (osName.startsWith("FreeBSD")) { |
| 135 | + TIOCGWINSZ = 0x40087468; |
| 136 | + } else { |
| 137 | + throw new UnsupportedOperationException(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments