Skip to content

Commit 6f0e303

Browse files
committed
Added apple support for M1 console
1 parent ea68c63 commit 6f0e303

File tree

5 files changed

+321
-0
lines changed

5 files changed

+321
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2023 dorkbox, llc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dorkbox.jna.macos;
17+
18+
import com.sun.jna.Native;
19+
import com.sun.jna.NativeLong;
20+
import com.sun.jna.ptr.IntByReference;
21+
22+
import dorkbox.jna.macos.structs.Termios;
23+
import dorkbox.jna.macos.structs.WindowSize;
24+
25+
@SuppressWarnings("ALL")
26+
public
27+
class CLibraryApple {
28+
static {
29+
Native.register("c");
30+
}
31+
32+
public static NativeLong TIOCGWINSZ = new NativeLong(0x40087468L);
33+
34+
35+
public static native
36+
int isatty(int fd);
37+
38+
public static native
39+
int read(int fd, IntByReference c, int count);
40+
41+
/**
42+
* https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/ioctl.2.html
43+
* Original signature : <code>int ioctl(int fildes, unsigned long request, ...);</code><br>
44+
*/
45+
public static native
46+
int ioctl(int fd, NativeLong request, WindowSize data);
47+
48+
/**
49+
* https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/tcgetattr.3.html
50+
*
51+
* Put the state of FD into *TERMIOS_P.<br>
52+
* <p>
53+
* Original signature : <code>int tcgetattr(int fildes, struct termios *termios_p)</code><br>
54+
*/
55+
public static native
56+
int tcgetattr(int fd, Termios termios_p);
57+
58+
/**
59+
* https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/tcsetattr.3.html
60+
*
61+
* Set the state of FD to *TERMIOS_P.<br>
62+
* <p>
63+
* Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>.<br>
64+
* <p>
65+
* Original signature : <code>tcsetattr(int fildes, int optional_actions, const struct termios *termios_p);</code><br>
66+
*/
67+
public static native
68+
int tcsetattr(int fd, long optional_actions, Termios termios_p);
69+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright 2023 dorkbox, llc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dorkbox.jna.macos.structs;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import com.sun.jna.NativeLong;
22+
import com.sun.jna.Structure;
23+
24+
@SuppressWarnings("ALL")
25+
public
26+
class Termios extends Structure {
27+
28+
// Input flags - software input processing
29+
public static class Input {
30+
public static final int IGNBRK = 0x00000001; // ignore BREAK condition
31+
public static final int BRKINT = 0x00000002; // map BREAK to SIGINTR
32+
public static final int IGNPAR = 0x00000004; // ignore (discard) parity errors
33+
public static final int PARMRK = 0x00000008; // mark parity and framing errors
34+
public static final int INPCK = 0x00000010; // enable checking of parity errors
35+
public static final int ISTRIP = 0x00000020; // strip 8th bit off chars
36+
public static final int INLCR = 0x00000010; // map NL into CR
37+
public static final int IGNCR = 0x00000080; // ignore CR
38+
public static final int ICRNL = 0x00000100; // map CR to NL (ala CRMOD)
39+
public static final int IXON = 0x00000200; // enable output flow control
40+
public static final int IXOFF = 0x00000400; // enable input flow control
41+
public static final int IXANY = 0x00000800; // any char will restart after stop
42+
public static final int IMAXBEL = 0x00002000; // ring bell on input queue full
43+
public static final int IUTF8 = 0x00004000; // (since Linux 2.6.4) (not in POSIX) Input is UTF8; this allows character-erase to be correctly performed in cooked mode.
44+
}
45+
46+
public static class Output {
47+
// Output flags - software output processing
48+
public static final int OPOST = 0x00000001; // enable following output processing (not set = raw output)
49+
public static final int ONLCR = 0x00000002; // map NL to CR-NL (ala CRMOD)
50+
public static final int OCRNL = 0x00000010; // map CR to NL on output
51+
public static final int ONOCR = 0x00000020; // no CR output at column 0
52+
public static final int ONLRET = 0x00000040; // NL performs CR function
53+
public static final int OFILL = 0x00000080; // Send fill characters for a delay, rather than using a timed delay.
54+
public static final int OFDEL = 0x00020000; // Fill character is ASCII DEL (0177). If unset, fill character is ASCII NUL ('\0'). (Not implemented on Linux.)
55+
}
56+
57+
58+
public static class Control {
59+
// Control flags - hardware control of terminal
60+
public static final int CSIZE = 0000060; // character size mask
61+
public static final int CS5 = 0x00000000; // 5 bits (pseudo)
62+
public static final int CS6 = 0x00000100; // 6 bits
63+
public static final int CS7 = 0x00000200; // 7 bits
64+
public static final int CS8 = 0x00000300; // 8 bits
65+
public static final int CSTOPB = 0x00000400; // send 2 stop bits
66+
public static final int CREAD = 0x00000800; // enable receiver
67+
public static final int PARENB = 0x00001000; // parity enable
68+
public static final int PARODD = 0x00002000; // odd parity, else even
69+
public static final int HUPCL = 0x00004000; // hang up on last close
70+
public static final int CLOCAL = 0x00008000; // ignore modem status lines
71+
}
72+
73+
74+
public static class Local {
75+
// "Local" flags - dumping ground for other state
76+
// Warning: some flags in this structure begin with the letter "I" and look like they belong in the input flag.
77+
public static final int ECHOKE = 0x00000001; // visual erase for line kill
78+
public static final int ECHOE = 0x00000002; // visually erase chars
79+
public static final int ECHOK = 0x00000004; // echo NL after line kill
80+
public static final int ECHO = 0x00000008; // enable echoing
81+
public static final int ECHONL = 0x00000010; // echo NL even if ECHO is off
82+
public static final int ECHOPRT = 0x00000020; // visual erase mode for hardcopy
83+
public static final int ECHOCTL = 0x00000040; // echo control chars as ^(Char)
84+
public static final int ISIG = 0x00000080; // enable signals INTR, QUIT, [D]SUSP
85+
public static final int ICANON = 0x00000100; // canonicalize input lines
86+
public static final int IEXTEN = 0x00000400; // enable DISCARD and LNEXT
87+
public static final int EXTPROC = 0x00000800; // external processing
88+
public static final int TOSTOP = 0x00400000; // stop background jobs from output
89+
public static final int FLUSHO = 0x00800000; // output being flushed (state)
90+
public static final int PENDIN = 0x20000000; // XXX retype pending input (state)
91+
public static final int NOFLSH = 0x80000000; // don't flush after interrupt
92+
}
93+
94+
95+
public static class ControlChars {
96+
// Special Control Characters
97+
//
98+
// the value is the index into c_cc[] character array.
99+
public static final int VEOF = 0;
100+
public static final int VEOL = 1;
101+
public static final int VEOL2 = 2;
102+
public static final int VERASE = 3;
103+
public static final int VWERASE = 4;
104+
public static final int VKILL = 5;
105+
public static final int VREPRINT = 6;
106+
public static final int VINTR = 8;
107+
public static final int VQUIT = 9;
108+
public static final int VSUSP = 10;
109+
public static final int VDSUSP = 11;
110+
public static final int VSTART = 12;
111+
public static final int VSTOP = 13;
112+
public static final int VLNEXT = 14;
113+
public static final int VDISCARD = 15;
114+
public static final int VMIN = 16;
115+
public static final int VTIME = 17;
116+
}
117+
118+
119+
public static void and(NativeLong org, int value) {
120+
org.setValue(org.longValue() & value);
121+
}
122+
123+
public static void and(NativeLong org, long value) {
124+
org.setValue(org.longValue() & value);
125+
}
126+
127+
public static void or(NativeLong org, long value) {
128+
org.setValue(org.longValue() | value);
129+
}
130+
131+
public static void or(NativeLong org, int value) {
132+
org.setValue(org.longValue() | value);
133+
}
134+
135+
136+
public static final long TCSANOW = 0x00000000;
137+
138+
/**
139+
* input mode flags
140+
*/
141+
public NativeLong inputFlags;
142+
143+
/**
144+
* output mode flags
145+
*/
146+
public NativeLong outputFlags;
147+
148+
/**
149+
* control mode flags
150+
*/
151+
public NativeLong controlFlags;
152+
153+
/**
154+
* local mode flags
155+
*/
156+
public NativeLong localFlags;
157+
158+
/**
159+
* control characters
160+
*/
161+
public byte[] controlChars = new byte[20];
162+
163+
/**
164+
* input speed
165+
*/
166+
public NativeLong inputSpeed;
167+
168+
/**
169+
* output speed
170+
*/
171+
public NativeLong outputSpeed;
172+
173+
public
174+
Termios() {}
175+
176+
@Override
177+
protected
178+
List<String> getFieldOrder() {
179+
return Arrays.asList("inputFlags",
180+
"outputFlags",
181+
"controlFlags",
182+
"localFlags",
183+
"controlChars",
184+
"inputSpeed",
185+
"outputSpeed");
186+
}
187+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 dorkbox, llc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dorkbox.jna.macos.structs;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import com.sun.jna.Structure;
23+
24+
public class WindowSize extends Structure {
25+
public short ws_row;
26+
public short ws_col;
27+
public short ws_xpixel;
28+
public short ws_ypixel;
29+
30+
public WindowSize() {}
31+
32+
@Override
33+
protected List<String> getFieldOrder() {
34+
return Arrays.asList("ws_row",
35+
"ws_col",
36+
"ws_xpixel",
37+
"ws_ypixel"
38+
);
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 dorkbox, llc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dorkbox.jna.macos.structs;
18+
19+
/**
20+
* Required for intellij to not complain regarding `module-info` for a multi-release jar.
21+
* This file is completely ignored by the gradle build process
22+
*/
23+
public
24+
class EmptyClass {}

src9/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
exports dorkbox.jna.macos;
77
exports dorkbox.jna.macos.cocoa;
88
exports dorkbox.jna.macos.foundation;
9+
exports dorkbox.jna.macos.structs;
910
exports dorkbox.jna.windows;
1011
exports dorkbox.jna.windows.structs;
1112

0 commit comments

Comments
 (0)