代码之家  ›  专栏  ›  技术社区  ›  Günter Obiltschnig

如何检查Linux控制台屏幕保护程序是否屏蔽了屏幕

  •  8
  • Günter Obiltschnig  · 技术社区  · 15 年前

    谢谢并致以最诚挚的问候,

    Gnter公司

    5 回复  |  直到 13 年前
        1
  •  7
  •   Daenyth    15 年前

    您可以解析 xset q 具有 DISPLAY 设置,但它不漂亮。

    $ xset q
    Keyboard Control:
      auto repeat:  on    key click percent:  0    LED mask:  00000000
      XKB indicators:
        00: Caps Lock:   off    01: Num Lock:    off    02: Scroll Lock: off
        03: Compose:     off    04: Kana:        off    05: Sleep:       off
        06: Suspend:     off    07: Mute:        off    08: Misc:        off
        09: Mail:        off    10: Charging:    off    11: Shift Lock:  off
        12: Group 2:     off    13: Mouse Keys:  off
      auto repeat delay:  250    repeat rate:  30
      auto repeating keys:  00ffffffdffffbbf
                            fadfffefffedffff
                            9fffffffffffffff
                            fff7ffffffffffff
      bell percent:  50    bell pitch:  400    bell duration:  100
    Pointer Control:
      acceleration:  20/10    threshold:  4
    Screen Saver:
      prefer blanking:  yes    allow exposures:  yes
      timeout:  0    cycle:  600
    Colors:
      default colormap:  0x20    BlackPixel:  0    WhitePixel:  16777215
    Font Path:
      /usr/share/fonts/misc,/usr/share/fonts/100dpi:unscaled,/usr/share/fonts/75dpi:unscaled,/usr/share/fonts/TTF,/usr/share/fonts/Type1,/usr/share/fonts/misc/,/usr/share/fonts/TTF/,/usr/share/fonts/Type1/,/usr/share/fonts/100dpi/,/usr/share/fonts/75dpi/,built-ins
    DPMS (Energy Star):
      Standby: 1200    Suspend: 1800    Off: 0
      DPMS is Enabled
      Monitor is On
    Font cache:
      Server does not have the FontCache Extension
    
        2
  •  10
  •   Günter Obiltschnig    15 年前

    好的,我查过了 xset source code . 相关的代码部分是

    #include <X11/extensions/dpms.h>
    ...
    Display* dpy = XOpenDisplay(NULL);
    ...
    int dummy;
    CARD16 standby, suspend, off;
    BOOL onoff;
    CARD16 state;
    
    printf("DPMS (Energy Star):\n");
    if (DPMSQueryExtension(dpy, &dummy, &dummy)) 
    {
        if (DPMSCapable(dpy)) 
        {
            DPMSGetTimeouts(dpy, &standby, &suspend, &off);
            printf ("  Standby: %d    Suspend: %d    Off: %d\n",
                    standby, suspend, off);
            DPMSInfo(dpy, &state, &onoff);
            if (onoff) 
            {
                printf("  DPMS is Enabled\n");
                switch (state) 
                {
                case DPMSModeOn:
                    printf("  Monitor is On\n");
                    break;
                case DPMSModeStandby:
                    printf("  Monitor is in Standby\n");
                    break;
                case DPMSModeSuspend:
                    printf("  Monitor is in Suspend\n");
                    break;
                case DPMSModeOff:
                    printf("  Monitor is Off\n");
                    break;
                default:
                    printf("  Unrecognized response from server\n");
                }
            }
        }
    }
    

    以防其他人需要;-)

        3
  •  1
  •   BlueSkyDetector    8 年前

    我用Python和ctypes实现了Gnter的代码。

    import ctypes
    import struct
    
    ctypes.cdll.LoadLibrary('libXext.so')
    libXext = ctypes.CDLL('libXext.so')
    
    DPMSFAIL = -1
    DPMSModeOn = 0
    DPMSModeStandby = 1
    DPMSModeSuspend = 2
    DPMSModeOff = 3
    
    
    def get_DPMS_state(display_name_in_byte_string=b':0'):
        state = DPMSFAIL
        if not isinstance(display_name_in_byte_string, bytes):
            raise TypeError
        display_name = ctypes.c_char_p()
        display_name.value = display_name_in_byte_string
        libXext.XOpenDisplay.restype = ctypes.c_void_p
        display = ctypes.c_void_p(libXext.XOpenDisplay(display_name))
        dummy1_i_p = ctypes.create_string_buffer(8)
        dummy2_i_p = ctypes.create_string_buffer(8)
        if display.value:
            if libXext.DPMSQueryExtension(display, dummy1_i_p, dummy2_i_p)\
               and libXext.DPMSCapable(display):
                onoff_p = ctypes.create_string_buffer(1)
                state_p = ctypes.create_string_buffer(2)
                if libXext.DPMSInfo(display, state_p, onoff_p):
                    onoff = struct.unpack('B', onoff_p.raw)[0]
                    if onoff:
                        state = struct.unpack('H', state_p.raw)[0]
            libXext.XCloseDisplay(display)
        return state
    

    my github .

        4
  •  0
  •   vstoyanov    15 年前

    当然,KDE和Gnome现在可能有单独的屏幕保护程序实现——不幸的是,在linux GUI方面几乎没有统一性。。。

        5
  •  0
  •   stamp    14 年前

    我用这个脚本来读取DPMS信息。很好用!它是用PHP编写的,但是您可以看到它的工作原理。

    <?php
    if ( !$pid = exec('pidof X') )
        return !trigger_error(E_USER_WARNING,'Could not find pid of X');
    
    if ( !$data = file_get_contents("/proc/$pid/cmdline") )
        return !trigger_error(E_USER_WARNING,"Cound not read pid info (/proc/$pid/cmdline)");
    
    $data = explode(chr(0),$data);
    foreach($data as $key => $line) {
        if ( $line == "-auth" ) {
            $auth = $data[$key+1];
            break;
        }
    }
    
    if ( !isset($auth) )
        return !trigger_error(E_USER_WARNING,'Could not find XAUTHORITY in xinit process environment');
    
    echo exec("export DISPLAY=:0; export XAUTHORITY={$auth}; export PATH=\${PATH}:/usr/X11R6/bin; xset -q | grep \"Monitor is\" | awk '{print $3}'");
    ?>