/
NativeSystem.java
189 lines (163 loc) · 4.63 KB
/
NativeSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* JNativeHook: Global keyboard and mouse hooking for Java.
* Copyright (C) 2006-2016 Alexander Barker. All Rights Received.
* https://github.com/kwhat/jnativehook/
*
* JNativeHook is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JNativeHook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jnativehook;
// Imports.
import java.util.Locale;
/**
* A small class to determine the native system's operating system family and
* architecture. The class is only used to determine which native library to
* unpack and load at runtime. This class is never used if the native library
* is loaded using the <code>java.library.path</code> property.
*
* @author Alexander Barker (<a href="mailto:alex@1stleg.com">alex@1stleg.com</a>)
* @since 1.0
* @version 2.1
*/
public class NativeSystem {
/**
* According to the docs, Locale.ROOT is the same as the locale whose language, country, and variant are
* empty ("") strings. This is required to maintain Java 1.5 compatibility.
*/
protected static final Locale ROOT_LOCALE = new Locale("", "", "");
/**
* The operating system family enum.
*
* @see NativeSystem
*/
public enum Family {
/** The FreeBSD operating system family. */
FREEBSD,
/** The OpenBSD operating system family. */
OPENBSD,
/** The Apple OS X operating system family. */
DARWIN,
/** The Solaris operating system family. */
SOLARIS,
/** The Linux operating system family. */
LINUX,
/** The Windows operating system family. */
WINDOWS,
/** Any unsupported operating system family. */
UNSUPPORTED;
@Override
public String toString() {
return super.toString().toLowerCase(NativeSystem.ROOT_LOCALE);
}
}
/**
* The system architecture enum.
*
* @see NativeSystem
*/
public enum Arch {
/** The arm6j architecture. */
ARM,
/** The sparc architecture. */
SPARC,
/** The sparc64 architecture. */
SPARC64,
/** The ppc architecture. */
PPC,
/** The ppc64 architecture. */
PPC64,
/** The x86 architecture. */
x86,
/** The amd64 architecture. */
x86_64,
/** Any unsupported system architecture. */
UNSUPPORTED;
@Override
public String toString() {
return super.toString().toLowerCase(NativeSystem.ROOT_LOCALE);
}
}
/**
* Determines the current operating system family.
*
* @return The current operating system family enum item.
*/
public static Family getFamily() {
String osName = System.getProperty("os.name");
Family family;
if (osName.equalsIgnoreCase("freebsd")) {
family = Family.FREEBSD;
}
else if (osName.equalsIgnoreCase("openbsd")) {
family = Family.OPENBSD;
}
else if (osName.equalsIgnoreCase("mac os x")) {
family = Family.DARWIN;
}
else if (osName.equalsIgnoreCase("solaris") ||
osName.equalsIgnoreCase("sunos")) {
family = Family.SOLARIS;
}
else if (osName.equalsIgnoreCase("linux")) {
family = Family.LINUX;
}
else if (osName.toLowerCase(NativeSystem.ROOT_LOCALE).startsWith("windows")) {
family = Family.WINDOWS;
}
else {
family = Family.UNSUPPORTED;
}
return family;
}
/**
* Determines the current system architecture.
*
* @return The current system architecture.
*/
public static Arch getArchitecture() {
String osArch = System.getProperty("os.arch");
Arch arch;
if (osArch.equalsIgnoreCase("arm")) {
arch = Arch.ARM;
}
else if (osArch.equalsIgnoreCase("sparc")) {
arch = Arch.SPARC;
}
else if (osArch.equalsIgnoreCase("sparc64")) {
arch = Arch.SPARC64;
}
else if (osArch.equalsIgnoreCase("ppc") ||
osArch.equalsIgnoreCase("powerpc")) {
arch = Arch.PPC;
}
else if (osArch.equalsIgnoreCase("ppc64") ||
osArch.equalsIgnoreCase("powerpc64")) {
arch = Arch.PPC64;
}
else if (osArch.equalsIgnoreCase("x86") ||
osArch.equalsIgnoreCase("i386") ||
osArch.equalsIgnoreCase("i486") ||
osArch.equalsIgnoreCase("i586") ||
osArch.equalsIgnoreCase("i686")) {
arch = Arch.x86;
}
else if (osArch.equalsIgnoreCase("x86_64") ||
osArch.equalsIgnoreCase("amd64") ||
osArch.equalsIgnoreCase("k8")) {
arch = Arch.x86_64;
}
else {
arch = Arch.UNSUPPORTED;
}
return arch;
}
}