How to Determine if LCD Monitor is Turned on From Linux Command Line

前端 未结 8 830
南方客
南方客 2021-01-31 04:04

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

8条回答
  •  庸人自扰
    2021-01-31 04:23

    xset -qis 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;
     }
    

提交回复
热议问题