Search code examples
clinuxubuntux11xlib

How to use XGetImage with multiple monitors?


I'm trying to capture the screen contents with XGetImage(). Everything works fine until I connect a second monitor. As per my understanding of the XOpenDisplay function, I should call it with something like XOpenDisplay("hostname:d.s") where 'd' is the display number and 's' is the screen number but it fails with segmentation fault. I tried with many combinations for the numbers (0.0, 0.1, 1.0, 1.1 etc). It only works if I call XOpenDisplay with NULL but I can only get an image from one of the displays. The 'nscreens' member of the 'Display' structure is 1. The second monitor works fine otherwise. I'm using Ubuntu 14.04.


Solution

  • The screen numbers are used only for distinct framebuffers, i.e. if you have a setup where the monitors are connected to different graphics cards. That is somewhat cumbersome to set up, and support in toolkits is flaky, so it is hardly used these days.

    Most multi-monitor setups are based on the XRANDR extension, which sets up multiple viewports into a large framebuffer. XGetImage() should see one large screen, and your application would need to query XRANDR to find out which portions are actually visible.