How do you tell if a computer\'s monitor(s) are turned on/off from the command line in Linux? I\'ve traditionally thought of monitors as output-only devices, but I\'ve noticed t
xset -q
is the way to go for a raspberry pi. A check to see if the reply contains 'Monitor is On' is a great way to use a gpio pin to turn off an LCD Backlight;
if(runOSCommand("xset -q").contains("Monitor is On")){
System.out.println("Monitor is On");
if screenLight.isHigh()) {
screenLight.low();
}
}else{
System.out.println("Monitor is Off");
if (screenLight.isLow()) {
screenLight.high();
}
}
public static String runOSCommand(String command){
String s = null;
String string = "";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null){
// System.out.println("line: " + s);
string += s;
}
p.waitFor();
// System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
return string;
}