How to create a callback for “monitor plugged” on an intel graphics?

后端 未结 5 714
猫巷女王i
猫巷女王i 2020-12-12 21:40

I\'ve got an eeepc with an intel graphics. I\'d like to hook a script to the event of a monitor plugged via VGA. How to do that?

5条回答
  •  孤街浪徒
    2020-12-12 22:05

    This other answer is on the right path: you want to listen to DRM events from udev.

    I've implemented a Python script that runs some code when either USB devices or external displays are (un)plugged. I'm including below a minimal version of that script (untested):

    #!/usr/bin/env python3
    import pyudev
    
    def udev_event_received(device):
        ...  # Your code here!
    
    context = pyudev.Context()
    monitor_drm = pyudev.Monitor.from_netlink(context)
    monitor_drm.filter_by(subsystem='drm')
    observer_drm = pyudev.MonitorObserver(monitor_drm, callback=udev_event_received, daemon=False)
    
    observer_drm.start()
    
    # This will prevent the program from finishing:
    observer_drm.join()
    

    See also:

    • How can I run code whenever a USB device is (un)plugged, without requiring root permissions?
    • How can I listen for 'usb device inserted' events in Linux, in Python?
    • pyudev documentation - Asynchronous monitoring

提交回复
热议问题