如何通过Xlib观察连接的显示器的变化?

5
我正在尝试编写一个程序,可以检测外部显示器的插拔,并通过Xlib自动启用和配置它们。我知道有Xrandr扩展可以实现这一点。我的问题是,如何使我的应用程序能够接收到Xrandr事件?我应该使用什么事件掩码?我知道xev应用程序可以做到这一点。

1
你看过xev的源代码了吗? - n. m.
4
这是man 3 Xrandr的内容:当屏幕配置发生变化时,会向请求通知的客户端发送一个XRRScreenChangeNotifyEvent事件。客户端可以通过调用XRRSelectInput,并传递显示器、根窗口和RRScreenChangeNotifyMask掩码来执行此请求。 - n. m.
2个回答

5

4

对于第二部分,我直接向nvidia-settings发出配置。

我从能够找到的部分组合了第一部分,这样我的HTPC系统就可以在电视打开时重置屏幕环境,因为连接的电视过度扫描非常严重。

我很想看到我努力的纯C端口...

#!/usr/bin/python2

"""
Send RESET_COMMAND via os.system() call when xbc.randr reports
that a small screen (main monitor) has just changed configuration.
ie The secondary (TV) monitor has just been turned on.
"""

# Could also be xrandr settings, if need be #
# These values came from the nvidia-settings GUI with Base Mosaic enable with custom scaling on HDMI-0 determined experiementally
RESET_COMMAND = 'nvidia-settings --assign CurrentMetaMode="GPU-08c5ca05-d3cc-b022-4fab-3acab0500b7c.VGA-0: 1280x1024 +0+0, GPU-08c5ca05-d3cc-b022-4fab-3acab0500b7c.HDMI-0: 1920x1080 +1280+0 {viewportin=1920x1080, viewportout=1774x998+73+41}"'

MAIN_MONITOR_HEIGHT = 1024
MAIN_MONITOR_WIDTH = 1280

import os
# Do one reset at startup (login) - this may be a shortcoming of LXDM that has things wrong after the first login #
os.system(RESET_COMMAND)

import xcb
from xcb.xproto import *

import xcb.randr as RandR
from xcb.randr import NotifyMask, ScreenChangeNotifyEvent


def startup():
    """Hook up XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE"""
    # In the xcb.randr module, the result of
    # key = xcb.ExtensionKey('RANDR')
    # xcb._add_ext(key, randrExtension, _events, _errors)
    # is stored in xcb.randr.key and retrieved in some very odd manner =>
    randr = conn(RandR.key)
    randr.SelectInput(root.root, NotifyMask.ScreenChange)
    # may as well flush()
    conn.flush()



def run():
    """Listen for XCB_RANDR_SCREEN_CHANGE_NOTIFY"""
    currentTimestamp = 0
    connected = False
    startup()

    while True:
        try:
            event = conn.wait_for_event()
            connected = True
        except xcb.ProtocolException, error:
            print "Protocol error %s received!" % error.__class__.__name__
            break
        except Exception, error:
            print "Unexpected error received: %s" % error.message
            break

        # Once the ScreenChangeNotify Event arrives, filter down to the one we care about. #
        if isinstance(event, ScreenChangeNotifyEvent):
            # 3 consecutive events arrive with the same timestamp, #
            if currentTimestamp != event.config_timestamp:
                # so mask off the next two and #
                currentTimestamp = event.config_timestamp
                # mask off the disconnect altogether by looking at the initial screen size. #
                if ((event.width == MAIN_MONITOR_WIDTH) and (event.height == MAIN_MONITOR_HEIGHT)):
                    os.system(RESET_COMMAND)

    # won't really get here, will we?
    if connected:
        conn.disconnect()



conn = xcb.connect()
setup = conn.get_setup()
# setup.roots holds a list of screens (just one in our case) #
root = setup.roots[0]

run()

1
非常感谢您。这里是您的程序的C语言实现。 - mafrasi2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接