Split OS into it's own project

This commit is contained in:
Robinson 2022-03-05 21:46:19 +01:00
parent d16dc36257
commit 9819da5892
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
9 changed files with 17 additions and 1590 deletions

View File

@ -212,7 +212,7 @@ dependencies {
api("com.dorkbox:Executor:3.9") api("com.dorkbox:Executor:3.9")
api("com.dorkbox:Updates:1.1") api("com.dorkbox:Updates:1.1")
api("com.dorkbox:OS:1.0")
val jnaVersion = "5.10.0" val jnaVersion = "5.10.0"

View File

@ -22,7 +22,6 @@ import com.sun.jna.Pointer;
import dorkbox.jna.JnaHelper; import dorkbox.jna.JnaHelper;
import dorkbox.jna.linux.structs.AppIndicatorInstanceStruct; import dorkbox.jna.linux.structs.AppIndicatorInstanceStruct;
import dorkbox.os.OS; import dorkbox.os.OS;
import dorkbox.os.OSUtil;
/** /**
* bindings for libappindicator * bindings for libappindicator
@ -137,28 +136,28 @@ class AppIndicator {
if (isGtk2) { if (isGtk2) {
if (OSUtil.Linux.isDebian()) { if (OS.Linux.INSTANCE.isDebian()) {
// debian is slightly different // debian is slightly different
return "libappindicator1"; return "libappindicator1";
} }
else if (OSUtil.Linux.isFedora()) { else if (OS.Linux.INSTANCE.isFedora()) {
return "libappindicator-gtk"; return "libappindicator-gtk";
} }
else if (OSUtil.Linux.isArch()) { else if (OS.Linux.INSTANCE.isArch()) {
return "libappindicator-gtk2"; return "libappindicator-gtk2";
} }
else { else {
return "libappindicator"; return "libappindicator";
} }
} else { } else {
if (OSUtil.Linux.isDebian()) { if (OS.Linux.INSTANCE.isDebian()) {
// debian is slightly different // debian is slightly different
return "libappindicator3-1"; return "libappindicator3-1";
} }
else if (OSUtil.Linux.isFedora()) { else if (OS.Linux.INSTANCE.isFedora()) {
return "libappindicator-gtk3"; return "libappindicator-gtk3";
} }
else if (OSUtil.Linux.isArch()) { else if (OS.Linux.INSTANCE.isArch()) {
return "libappindicator-gtk3"; return "libappindicator-gtk3";
} }
else { else {

View File

@ -37,7 +37,6 @@ import dorkbox.jna.linux.structs.GtkRequisition;
import dorkbox.jna.linux.structs.GtkStyle; import dorkbox.jna.linux.structs.GtkStyle;
import dorkbox.jna.linux.structs.PangoRectangle; import dorkbox.jna.linux.structs.PangoRectangle;
import dorkbox.os.OS; import dorkbox.os.OS;
import dorkbox.os.OSUtil;
import dorkbox.util.FileUtil; import dorkbox.util.FileUtil;
import dorkbox.util.MathUtil; import dorkbox.util.MathUtil;
@ -242,7 +241,7 @@ class GtkTheme {
OSUtil.DesktopEnv.Env env = OSUtil.DesktopEnv.get(); OS.DesktopEnv.Env env = OS.DesktopEnv.INSTANCE.getEnv();
// sometimes the scaling-factor is set. If we have gsettings, great! otherwise try KDE // sometimes the scaling-factor is set. If we have gsettings, great! otherwise try KDE
try { try {
@ -274,7 +273,7 @@ class GtkTheme {
} catch (Throwable ignore) { } catch (Throwable ignore) {
} }
if (OSUtil.DesktopEnv.isKDE()) { if (OS.DesktopEnv.INSTANCE.isKDE()) {
// check the custom KDE override file // check the custom KDE override file
try { try {
File customSettings = new File("/usr/bin/startkde-custom"); File customSettings = new File("/usr/bin/startkde-custom");
@ -382,7 +381,7 @@ class GtkTheme {
} }
} }
if (OSUtil.Linux.isUbuntu() && OSUtil.DesktopEnv.isUnity(env)) { if (OS.Linux.INSTANCE.isUbuntu() && OS.DesktopEnv.INSTANCE.isUnity(env)) {
// if we measure on ubuntu unity using a screen shot (using swing, so....) , the max size was 24, HOWEVER this goes from // if we measure on ubuntu unity using a screen shot (using swing, so....) , the max size was 24, HOWEVER this goes from
// the top->bottom of the indicator bar -- and since it was swing, it uses a different rendering method and it (honestly) // the top->bottom of the indicator bar -- and since it was swing, it uses a different rendering method and it (honestly)
// looks weird, because there is no padding for the icon. The official AppIndicator size is hardcoded... // looks weird, because there is no padding for the icon. The official AppIndicator size is hardcoded...
@ -391,9 +390,9 @@ class GtkTheme {
return 22; return 22;
} }
if (env == OSUtil.DesktopEnv.Env.XFCE) { if (env == OS.DesktopEnv.Env.XFCE) {
// xfce is easy, because it's not a GTK setting for the size (xfce notification area maximum icon size) // xfce is easy, because it's not a GTK setting for the size (xfce notification area maximum icon size)
String properties = OSUtil.DesktopEnv.queryXfce("xfce4-panel", null); String properties = OS.DesktopEnv.INSTANCE.queryXfce("xfce4-panel", null);
String[] propertiesAsList = properties.split(OS.INSTANCE.getLINE_SEPARATOR()); String[] propertiesAsList = properties.split(OS.INSTANCE.getLINE_SEPARATOR());
for (String prop : propertiesAsList) { for (String prop : propertiesAsList) {
if (prop.startsWith("/plugins/") && prop.endsWith("/size-max")) { if (prop.startsWith("/plugins/") && prop.endsWith("/size-max")) {
@ -403,9 +402,9 @@ class GtkTheme {
// xfconf-query -c xfce4-panel -p /plugins/plugin-14 (this will say 'systray' or 'tasklist' or whatever) // xfconf-query -c xfce4-panel -p /plugins/plugin-14 (this will say 'systray' or 'tasklist' or whatever)
// find the 'systray' plugin // find the 'systray' plugin
String panelString = prop.substring(0, prop.indexOf("/size-max")); String panelString = prop.substring(0, prop.indexOf("/size-max"));
String panelName = OSUtil.DesktopEnv.queryXfce("xfce4-panel", panelString).trim(); String panelName = OS.DesktopEnv.INSTANCE.queryXfce("xfce4-panel", panelString).trim();
if (panelName.equals("systray")) { if (panelName.equals("systray")) {
String size = OSUtil.DesktopEnv.queryXfce("xfce4-panel", prop).trim(); String size = OS.DesktopEnv.INSTANCE.queryXfce("xfce4-panel", prop).trim();
try { try {
return Integer.parseInt(size); return Integer.parseInt(size);
} catch (Exception e) { } catch (Exception e) {

View File

@ -22,7 +22,7 @@ import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.IntByReference; import com.sun.jna.ptr.IntByReference;
import dorkbox.jna.JnaHelper; import dorkbox.jna.JnaHelper;
import dorkbox.os.OSUtil; import dorkbox.os.OS;
/** /**
* bindings for ShCore.dll * bindings for ShCore.dll
@ -41,7 +41,7 @@ class ShCore {
private static Function GetDpiForMonitor = null; private static Function GetDpiForMonitor = null;
static { static {
if (OSUtil.Windows.isWindows8_1_plus()) { if (OS.Windows.INSTANCE.isWindows8_1_plus()) {
NativeLibrary library = JnaHelper.register("shcore", ShCore.class); NativeLibrary library = JnaHelper.register("shcore", ShCore.class);
// Abusing static fields this way is not proper, but it gets the job done nicely. // Abusing static fields this way is not proper, but it gets the job done nicely.

View File

@ -1,395 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
package dorkbox.os
import java.awt.Color
import java.io.File
import java.security.AccessController
import java.security.PrivilegedAction
import java.util.*
object OS {
// make the default unix
val LINE_SEPARATOR = getProperty("line.separator", "\n")
const val LINE_SEPARATOR_UNIX = "\n"
const val LINE_SEPARATOR_MACOS = "\r"
const val LINE_SEPARATOR_WINDOWS = "\r\n"
val TEMP_DIR = File(getProperty("java.io.tmpdir", "temp")).absoluteFile
/**
* The currently running MAJOR java version as a NUMBER. For example, "Java version 1.7u45", and converts it into 7, uses JEP 223 for java > 9
*/
val javaVersion: Int by lazy {
// this should never be a problem, but just in case
var fullJavaVersion = getProperty("java.version", "8")
if (fullJavaVersion.startsWith("1.")) {
when (fullJavaVersion[2]) {
'4' -> 4
'5' -> 5
'6' -> 6
'7' -> 7
'8' -> 8
'9' -> 9
else -> {
8
}
}
} else {
// We are >= java 10, use JEP 223 to get the version (early releases of 9 might not have JEP 223, so 10 is guaranteed to have it)
fullJavaVersion = getProperty("java.specification.version", "10")
try {
// it will ALWAYS be the major release version as an integer. See http://openjdk.java.net/jeps/223
fullJavaVersion.toInt()
} catch (ignored: Exception) {
}
}
// the last valid guess we have, since the current Java implementation, whatever it is, decided not to cooperate with JEP 223.
8
}
/**
* Returns the *ORIGINAL* system time zone, before (*IF*) it was changed to UTC
*/
val originalTimeZone = TimeZone.getDefault().id
/**
* JVM reported osName, the default (if there is none detected) is 'linux'
*/
val osName = getProperty("os.name", "linux").lowercase()
/**
* JVM reported osArch, the default (if there is none detected) is 'amd64'
*/
val osArch = getProperty("os.arch", "amd64").lowercase()
/**
* @return the optimum number of threads for a given task. Makes certain not to take ALL the threads, always returns at least one
* thread.
*/
val optimumNumberOfThreads = (Runtime.getRuntime().availableProcessors() - 2).coerceAtLeast(1)
/**
* The determined OS type
*/
val type: OSType by lazy {
if (osName.startsWith("linux")) {
// best way to determine if it's android.
// Sometimes java binaries include Android classes on the classpath, even if it isn't actually Android, so we check the VM
val isAndroid = "Dalvik" == getProperty("java.vm.name", "")
if (isAndroid) {
// android check from https://stackoverflow.com/questions/14859954/android-os-arch-output-for-arm-mips-x86
when (osArch) {
"armeabi" -> {
OSType.AndroidArm56 // really old/low-end non-hf 32bit cpu
}
"armeabi-v7a" -> {
OSType.AndroidArm7 // 32bit hf cpu
}
"arm64-v8a" -> {
OSType.AndroidArm8 // 64bit hf cpu
}
"x86" -> {
OSType.AndroidX86 // 32bit x86 (usually emulator)
}
"x86_64" -> {
OSType.AndroidX86_64 // 64bit x86 (usually emulator)
}
"mips" -> {
OSType.AndroidMips // 32bit mips
}
"mips64" -> {
OSType.AndroidMips64 // 64bit mips
}
else -> {
throw java.lang.RuntimeException("Unable to determine OS type for $osName $osArch")
}
}
} else {
// http://mail.openjdk.java.net/pipermail/jigsaw-dev/2017-April/012107.html
when(osArch) {
"i386", "x86" -> {
OSType.Linux32
}
"arm" -> {
OSType.LinuxArm32
}
"x86_64", "amd64" -> {
OSType.Linux64
}
"aarch64" -> {
OSType.LinuxArm64
}
else -> {
when {
// oddballs (android usually)
osArch.startsWith("arm64") -> {
OSType.LinuxArm64
}
osArch.startsWith("arm") -> {
if (osArch.contains("v8")) {
OSType.LinuxArm64
} else {
OSType.LinuxArm32
}
}
else -> {
throw java.lang.RuntimeException("Unable to determine OS type for $osName $osArch")
}
}
}
}
}
} else if (osName.startsWith("windows")) {
if ("amd64" == osArch) {
OSType.Windows64
} else {
OSType.Windows32
}
} else if (osName.startsWith("mac") || osName.startsWith("darwin")) {
when (osArch) {
"x86_64", "aarch64" -> {
OSType.MacOsX64
}
else -> {
OSType.MacOsX32 // new macosx is no longer 32 bit, but just in case.
}
}
} else if (osName.startsWith("freebsd") ||
osName.contains("nix") ||
osName.contains("nux") ||
osName.startsWith("aix")) {
when (osArch) {
"x86", "i386" -> {
OSType.Unix32
}
"arm" -> {
OSType.UnixArm
}
else -> {
OSType.Unix64
}
}
} else if (osName.startsWith("solaris") ||
osName.startsWith("sunos")) {
OSType.Solaris
} else {
throw java.lang.RuntimeException("Unable to determine OS type for $osName $osArch")
}
}
init {
if (!TEMP_DIR.isDirectory) {
// create the temp dir if necessary because the TEMP dir doesn't exist.
TEMP_DIR.mkdirs()
}
/*
* By default, the timer resolution on Windows ARE NOT high-resolution (16ms vs 1ms)
*
* 'Thread.sleep(1)' will not really sleep for 1ms, but will really sleep for ~16ms. This long-running sleep will trick Windows
* into using higher resolution timers.
*
* See: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6435126
*/
if (type.isWindows) {
// only necessary on windows
val timerAccuracyThread = Thread(
{
while (true) {
try {
Thread.sleep(Long.MAX_VALUE)
} catch (ignored: Exception) {
}
}
}, "FixWindowsHighResTimer")
timerAccuracyThread.isDaemon = true
timerAccuracyThread.start()
}
}
/**
* @return the System Property in a safe way for a given property, or null if it does not exist.
*/
fun getProperty(property: String): String? {
return try {
if (System.getSecurityManager() == null) {
System.getProperty(property, null)
} else {
AccessController.doPrivileged(PrivilegedAction { System.getProperty(property, null) })
}
} catch (ignored: Exception) {
null
}
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getProperty(property: String, defaultValue: String): String {
return try {
if (System.getSecurityManager() == null) {
System.getProperty(property, defaultValue)
} else {
AccessController.doPrivileged(PrivilegedAction { System.getProperty(property, defaultValue) })
}
} catch (ignored: Exception) {
defaultValue
}
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getBoolean(property: String, defaultValue: Boolean): Boolean {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }.lowercase(Locale.getDefault())
if (value.isEmpty()) {
return defaultValue
}
if ("false" == value || "no" == value || "0" == value) {
return false
}
return if ("true" == value || "yes" == value || "1" == value) {
true
} else defaultValue
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getInt(property: String, defaultValue: Int): Int {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }
try {
return value.toInt()
} catch (ignored: Exception) {
}
return defaultValue
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getLong(property: String, defaultValue: Long): Long {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }
try {
return value.toLong()
} catch (ignored: Exception) {
}
return defaultValue
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getFloat(property: String, defaultValue: Float): Float {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }
try {
return value.toFloat()
} catch (ignored: Exception) {
}
return defaultValue
}
/**
* @return the value of the Java system property with the specified `property`, while falling back to the
* specified default value if the property access fails.
*/
fun getDouble(property: String, defaultValue: Double): Double {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }
try {
return value.toDouble()
} catch (ignored: Exception) {
}
return defaultValue
}
fun getColor(property: String, defaultValue: Color): Color {
var value = getProperty(property) ?: return defaultValue
value = value.trim { it <= ' ' }
try {
return Color.decode(value)
} catch (ignored: Exception) {
}
return defaultValue
}
val is32bit = type.is32bit
val is64bit = type.is64bit
/**
* @return true if this is a x86/x64/arm architecture (intel/amd/etc) processor.
*/
val isX86 = type.isX86
val isMips = type.isMips
val isArm = type.isArm
val isLinux = type.isLinux
val isUnix = type.isUnix
val isSolaris = type.isSolaris
val isWindows = type.isWindows
val isMacOsX = type.isMacOsX
val isAndroid = type.isAndroid
/**
* Set our system to UTC time zone. Retrieve the **original** time zone via [.getOriginalTimeZone]
*/
fun setUTC() {
// have to set our default timezone to UTC. EVERYTHING will be UTC, and if we want local, we must explicitly ask for it.
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
}
/**
* @return the first line of the exception message from 'throwable', or the type if there was no message.
*/
fun getExceptionMessage(throwable: Throwable): String? {
var message = throwable.message
if (message != null) {
val index = message.indexOf(LINE_SEPARATOR)
if (index > -1) {
message = message.substring(0, index)
}
} else {
message = throwable.javaClass.simpleName
}
return message
}
}

View File

@ -1,87 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.os
enum class OSType(name: String, vararg libraryNames: String) {
Windows32("windows_32", ".dll"),
Windows64("windows_64", ".dll"),
Linux32("linux_32", ".so"),
Linux64("linux_64", ".so"),
MacOsX32("macosx_32", ".jnilib", ".dylib"),
MacOsX64("macosx_64", ".jnilib", ".dylib"),
UnixArm("unix_arm", ".so"),
Unix32("unix_32",".so"),
Unix64("unix_64", ".so"),
Solaris("solaris", ".so"),
AndroidArm56("android_arm56", ".so"), // 32bit no hardware float support
AndroidArm7("android_arm7", ".so"), // 32bit hardware float support
AndroidArm8("android_arm8", ".so"), // 64bit (w/ hardware float. everything now has hard float)
AndroidMips("android_mips", ".so"), // 32bit mips
AndroidX86("android_x86", ".so"), // 32bit x86 (usually emulator)
AndroidMips64("android_mips64", ".so"), // 64bit mips
AndroidX86_64("android_x86_64", ".so"), // 64bit x86 (usually emulator)
/*
* Linux OS, Hard float, meaning floats are handled in hardware. WE ONLY SUPPORT HARD FLOATS for linux ARM!.
* For Raspberry-PI, Beaglebone, Odroid, etc PCs
*/
LinuxArm32("linux_arm7_hf", ".so"),
LinuxArm64("linux_arm8_hf", ".so");
val standardName: String
val libraryNames: Array<out String>
init {
this.standardName = name
this.libraryNames = libraryNames
}
val is64bit: Boolean
get() {
return this == Linux64 || this == LinuxArm64 || this == Windows64 || this == MacOsX64 || this == AndroidArm8 || this == AndroidX86_64 || this == AndroidMips64 || this == Unix64
}
val is32bit: Boolean
get() {
return this == Linux32 || this == LinuxArm32 || this == Windows32 || this == MacOsX32 || this == AndroidArm56 || this == AndroidArm7 || this == AndroidX86 || this == AndroidMips || this == UnixArm || this == Unix32
}
val isMips: Boolean
get() = this == AndroidMips || this == AndroidMips64
/**
* @return true if this is a "standard" x86/x64 architecture (intel/amd/etc) processor.
*/
val isX86: Boolean
get() = this == Linux64 || this == LinuxArm64 || this == Windows64 || this == MacOsX64 || this == Linux32 || this == LinuxArm32 || this == Windows32 || this == MacOsX32 || this == Unix32 || this == Unix64 || this == AndroidX86 || this == AndroidX86_64
val isArm: Boolean
get() = this == LinuxArm32 || this == LinuxArm64 || this == AndroidArm56 || this == AndroidArm7 || this == AndroidArm8
val isLinux: Boolean
get() = this == Linux32 || this == Linux64 || this == LinuxArm64 || this == LinuxArm32
val isUnix: Boolean
get() = this == Unix32 || this == Unix64 || this == UnixArm
val isSolaris: Boolean
get() = this == Solaris
val isWindows: Boolean
get() = this == Windows64 || this == Windows32
val isMacOsX: Boolean
get() = this == MacOsX64 || this == MacOsX32
val isAndroid: Boolean
get() = this == AndroidArm56 || this == AndroidArm7 || this == AndroidX86 || this == AndroidMips || this == AndroidArm8 || this == AndroidX86_64 || this == AndroidMips64
}

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.os;

View File

@ -29,7 +29,6 @@ import dorkbox.jna.linux.GnomeVFS;
import dorkbox.jna.linux.GtkCheck; import dorkbox.jna.linux.GtkCheck;
import dorkbox.jna.linux.GtkEventDispatch; import dorkbox.jna.linux.GtkEventDispatch;
import dorkbox.os.OS; import dorkbox.os.OS;
import dorkbox.os.OSUtil;
@SuppressWarnings({"WeakerAccess", "Convert2Lambda", "Duplicates"}) @SuppressWarnings({"WeakerAccess", "Convert2Lambda", "Duplicates"})
public public
@ -311,7 +310,7 @@ class Desktop {
// ubuntu, fedora, etc MIGHT have access to gvfs-open. Ubuntu is also VERY buggy with xdg-open!! // ubuntu, fedora, etc MIGHT have access to gvfs-open. Ubuntu is also VERY buggy with xdg-open!!
new Executor().command(GVFS, path).startAsync(); new Executor().command(GVFS, path).startAsync();
} }
else if (OSUtil.DesktopEnv.isGnome() && GnomeVFS.isInited) { else if (OS.DesktopEnv.INSTANCE.isGnome() && GnomeVFS.isInited) {
GtkEventDispatch.dispatch(new Runnable() { GtkEventDispatch.dispatch(new Runnable() {
@Override @Override
public public