How to create a callback for monitor plugged on an intel graphics?
As a crude solution, you may be able to poll on sysfs. On my laptop I have:
$ cat /sys/class/drm/card0-LVDS-1/status
connected
$ cat /sys/class/drm/card0-VGA-1/status
disconnected
I'm guessing this requires kernel DRM and possibly KMS.
To see if you can trigger something automatically, you could run udevadm monitor --property
, and watch while you are (dis-)connecting the monitor to see if events are reported.
With my radeon, I get an event the first time I connect a VGA monitor, but no events on subsequent disconnects and reconnects. The event should look something like (using yours as an example):
KERNEL[1303765357.560848] change /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV_LOG=0
ACTION=change
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0
SUBSYSTEM=drm
HOTPLUG=1
DEVNAME=dri/card0
DEVTYPE=drm_minor
SEQNUM=2943
MAJOR=226
MINOR=0
Unfortunately there's not a lot to match against, but as long as there's only one video card in the picture that's not too important. Find where udev gets rules from on your system (probably /etc/udev/rules.d/
), and create a 99-monitor-hotplug.rules
file with:
ACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1", RUN+="/root/hotplug.sh"
udev
will then run hotplug.sh
when a display is connected. As a test, I put the following in /root/hotplug.sh
(don't forget to make this script executable):
#!/bin/sh
for output in DVI-I-1 LVDS-1 VGA-1; do
echo $output >> /root/hotplug.log
cat /sys/class/drm/card0-$output/status >> /root/hotplug.log
done
With that, I got an entry in hotplug.log
after I connected an external display. Even filtering for ACTION=="change"
, I still got some events on boot, so you may want to take that into account somehow in your script.
How to detect hot plugging of monitor in a win32 application?
Use RegisterDeviceNotification to register for getting WM_DEVICECHANGE notification.
In Java, is it possible to listen for the connection/disconnection of an external Monitor?
The AWT gives you access to screen information, although "external" is subjective as you might have 2 built-in monitors or 2 external ones.
At a basic level, you can count the monitors at any moment:
int numberOfMonitors = 0;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc = gd.getConfigurations();
if (gc.getType() == TYPE_RASTER_SCREEN) numberOfMonitors++;
}
System.out.println("Number of monitors: " + numberOfMonitors);
To detect the attachment of a new monitor, you will need to poll for the result.
This is a pure Java solution; to my knowledge if you want anything more accurate than this then you'll probably need to call some native tools on the platform(s) you are targeting. For example, probing sysfs in Linux.
Related Topics
Less Gets Keyboard Input from Stderr
Rename Multiple Files While Keeping the Same Extension on Linux
Find the Ip Address of the Client in an Ssh Session
Convert Binary Data to Hexadecimal in a Shell Script
Can't Change Tomcat 7 Heap Size
A Running Bash Script Is Hung Somewhere. How to Find Out What Line It Is On
Can Not Connect to Linux "Abstract" Unix Socket
Using Mkdir -M -P and Chown Together Correctly
How to Login to 'Firebase-Tools' on Headless Remote Server
How to Grep for Value in a Key-Value Store from Plain Text
Bash 'Swallowing' Sub-Shell Children Process When Executing a Single Command
Why Characters Received in Serial Connection Only After Pressing Enter
How to Get the List of Dependent Child Images in Docker
How to Enable Bash in Windows 10 Developer Preview
Bluetooth Low Energy: Use Bluez Stack as a Peripheral (With Custom Services and Characteristics)
Curl Does Not Work But Same Site Works with Browser