2014-11-20 13:21:19 +00:00
/ *
* Copyright ( C ) 2010 - 2014 Laurent CLOUET
* Author Laurent CLOUET < laurent . clouet @nopnop.net >
*
2020-05-28 13:28:42 +02:00
* This program is free software ; you can redistribute it and / or
2014-11-20 13:21:19 +00:00
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; version 2
* of the License .
*
* This program 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 General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
* /
package com.sheepit.client.os ;
2021-12-29 17:08:51 +00:00
import java.io.File ;
2014-11-20 13:21:19 +00:00
import java.io.IOException ;
2015-01-25 19:07:32 +00:00
import java.util.List ;
2015-01-16 17:51:50 +01:00
import java.util.Map ;
2014-11-20 13:21:19 +00:00
import com.sheepit.client.os.windows.Kernel32Lib ;
import com.sheepit.client.os.windows.WinProcess ;
import com.sun.jna.Native ;
public class Windows extends OS {
2021-11-11 17:08:45 +00:00
private static final int MINIMUM_SUPPORTED_BUILD = 9600 ; //windows 8.1 and Server 2012 R2
2021-09-10 00:23:57 +00:00
@Override public String name ( ) {
2014-11-20 13:21:19 +00:00
return " windows " ;
}
2020-05-28 13:28:42 +02:00
@Override public String getRenderBinaryPath ( ) {
2014-11-30 23:42:57 +00:00
return " rend.exe " ;
}
2020-05-28 13:28:42 +02:00
@Override public String getCUDALib ( ) {
2014-11-20 13:21:19 +00:00
return " nvcuda " ;
}
2023-01-06 14:19:34 +00:00
@Override public String getNVMLLib ( ) {
return " nvml " ;
}
2014-11-20 13:21:19 +00:00
2020-05-28 13:28:42 +02:00
@Override public Process exec ( List < String > command , Map < String , String > env ) throws IOException {
2014-11-20 13:21:19 +00:00
// disable a popup because the renderer might crash (seg fault)
Kernel32Lib kernel32lib = null ;
try {
2020-04-26 21:29:48 +08:00
kernel32lib = ( Kernel32Lib ) Native . load ( Kernel32Lib . path , Kernel32Lib . class ) ;
2014-11-20 13:21:19 +00:00
kernel32lib . SetErrorMode ( Kernel32Lib . SEM_NOGPFAULTERRORBOX ) ;
}
catch ( java . lang . UnsatisfiedLinkError e ) {
System . out . println ( " OS.Windows::exec failed to load kernel32lib " + e ) ;
}
catch ( java . lang . ExceptionInInitializerError e ) {
System . out . println ( " OS.Windows::exec failed to load kernel32lib " + e ) ;
}
catch ( Exception e ) {
System . out . println ( " OS.Windows::exec failed to load kernel32lib " + e ) ;
}
ProcessBuilder builder = new ProcessBuilder ( command ) ;
builder . redirectErrorStream ( true ) ;
2015-01-16 17:51:50 +01:00
if ( env ! = null ) {
builder . environment ( ) . putAll ( env ) ;
}
2014-11-20 13:21:19 +00:00
Process p = builder . start ( ) ;
WinProcess wproc = new WinProcess ( p ) ;
2017-01-05 09:58:27 +01:00
if ( env ! = null ) {
String priority = env . get ( " PRIORITY " ) ;
wproc . setPriority ( getPriorityClass ( Integer . parseInt ( priority ) ) ) ;
}
else {
wproc . setPriority ( WinProcess . PRIORITY_BELOW_NORMAL ) ;
}
2014-11-20 13:21:19 +00:00
return p ;
}
2021-09-10 00:23:57 +00:00
@Override public boolean isSupported ( ) {
2021-11-11 17:08:45 +00:00
long buildNumber = Long . MIN_VALUE ;
2022-07-29 15:40:38 +02:00
String arch = System . getProperty ( " os.arch " ) . toLowerCase ( ) ;
2021-11-11 17:08:45 +00:00
try {
2021-11-19 16:05:55 +00:00
buildNumber = Long . parseLong ( operatingSystem . getVersionInfo ( ) . getBuildNumber ( ) ) ;
2021-11-11 17:08:45 +00:00
}
catch ( NumberFormatException e ) {
System . err . println ( " Windows::isSupported Failed to extract Windows build number: " + e ) ;
}
2022-09-10 10:11:39 +00:00
catch ( NullPointerException e ) {
System . err . println ( " Windows::isSupported Failed to extract Windows build number: " + e ) ;
2023-09-09 14:06:56 +02:00
var versionInfo = operatingSystem . getVersionInfo ( ) ;
if ( versionInfo = = null ) {
return false ;
}
else {
String ver = versionInfo . getVersion ( ) ;
List < String > supportedVersions = List . of ( " 8.1 " , " 10 " , " 11 " , " Server 2016 " , " Server 2012 R2 " , " Server 2019 " , " Server 2022 " ) ;
return super . isSupported ( ) & & supportedVersions . contains ( ver ) ;
}
2022-09-10 10:11:39 +00:00
}
2022-07-29 15:40:38 +02:00
return
super . isSupported ( ) & &
buildNumber > = MINIMUM_SUPPORTED_BUILD & &
(
" amd64 " . equals ( arch ) | |
" x64 " . equals ( arch ) | |
" x86_64 " . equals ( arch )
) ;
2021-09-10 00:23:57 +00:00
}
2017-01-05 09:58:27 +01:00
int getPriorityClass ( int priority ) {
int process_class = WinProcess . PRIORITY_IDLE ;
switch ( priority ) {
case 19 :
case 18 :
case 17 :
case 16 :
case 15 :
process_class = WinProcess . PRIORITY_IDLE ;
break ;
case 14 :
case 13 :
case 12 :
case 11 :
case 10 :
case 9 :
case 8 :
case 7 :
case 6 :
case 5 :
process_class = WinProcess . PRIORITY_BELOW_NORMAL ;
break ;
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :
case - 1 :
case - 2 :
case - 3 :
process_class = WinProcess . PRIORITY_NORMAL ;
break ;
case - 4 :
case - 5 :
case - 6 :
case - 7 :
case - 8 :
case - 9 :
process_class = WinProcess . PRIORITY_ABOVE_NORMAL ;
break ;
case - 10 :
case - 11 :
case - 12 :
case - 13 :
case - 14 :
process_class = WinProcess . PRIORITY_HIGH ;
break ;
case - 15 :
case - 16 :
case - 17 :
case - 18 :
case - 19 :
process_class = WinProcess . PRIORITY_REALTIME ;
break ;
}
return process_class ;
}
2020-05-28 13:28:42 +02:00
@Override public boolean kill ( Process process ) {
2014-11-20 13:21:19 +00:00
if ( process ! = null ) {
WinProcess wproc = new WinProcess ( process ) ;
wproc . kill ( ) ;
return true ;
}
return false ;
}
2020-05-28 23:05:52 +10:00
2023-11-12 13:57:18 +00:00
@Override public boolean isHighPrioritySupported ( ) {
2020-05-28 23:05:52 +10:00
return true ;
}
2023-11-12 13:57:18 +00:00
@Override public boolean isNiceAvailable ( ) {
2020-05-28 23:05:52 +10:00
// In windows, nice is not required and therefore we return always true to show the slider in the Settings GUI
return true ;
}
2020-07-28 00:49:36 +10:00
@Override public void shutdownComputer ( int delayInMinutes ) {
try {
2021-09-10 11:49:22 +02:00
// Shutdown the computer, waiting delayInMinutes minutes, force app closure and on the shutdown screen indicate that was initiated by SheepIt app
2020-07-28 00:49:36 +10:00
ProcessBuilder builder = new ProcessBuilder ( " shutdown " , " /s " , " /f " , " /t " , String . valueOf ( delayInMinutes * 60 ) , " /c " , " \" SheepIt App has initiated this computer shutdown. \" " ) ;
Process process = builder . inheritIO ( ) . start ( ) ;
}
catch ( IOException e ) {
System . err . println (
String . format ( " Windows::shutdownComputer Unable to execute the command 'shutdown /s /f /t 60...' command. Exception %s " , e . getMessage ( ) ) ) ;
}
}
2021-12-29 17:08:51 +00:00
@Override public String getDefaultConfigFilePath ( ) {
return System . getProperty ( " user.home " ) + File . separator + " .sheepit.conf " ;
}
2014-11-20 13:21:19 +00:00
}