Skip to content
Advertisement

How do you run an application in bash and select which monitor it runs on?

I have a question that is hard to search for the answer (I always end up with answers for monitor manipulation). I am writing a bash shell script to help me with my code dev and I have two monitors.

When I run my executable that I have compiled I want to tell it to run on a particular monitor (i.e. different to the monitor that I have my terminal open on so I can view the debug on one screen and have the app on another).

How would I go about doing this? Something like:

./myProject > but run on monitor 2

Where myProject is my binary executable.

Thanks all.

Advertisement

Answer

If you run separate displays on each monitor (less likely these days), the DISPLAY environment variable is what you want.

If you use Xinerama (spreading one logical display across multiple monitors), however:

  • Aside: X11 vocabulary: A “display” is one or more “screens” with input devices; e.g. keyboard and mouse, a.k.a. a “seat.” A “screen” is a logical canvas that is partially or completely displayed on one or more “monitors;” when using multiple monitors for one “screen,” the windows can be partially displayed on each monitor, but share the same X11 DISPLAY identifier; this is called Xinerama. The DISPLAY format is host : display-number . screen-id, so e.g. on my Xinerama set-up both monitors are part of screen 0 on a display number that counts up from 0 with each logged-in user on the same host. “Seats” are logical groups of monitor+input that are using different hardware; multiple “displays” can occur using “virtual console” switching, which is how Gnome and KDE allow multiple users to sign in on a single “seat” machine.

Most GUI toolkits allow you to specify the window’s geometry using the --geometry or -geometry switch.

  • Qt uses the older MIT-style -geometry form. GTK+/Gnome uses the GNU-style --geometry.

  • This assumes that you’re allowing Qt to post-process your command-line, e.g. passing argv into QtApplication or similar.

The “logical display” will have a resolution which is the sum of the resolutions in each direction of the arrangement of your monitors. For example, I have 2 × 1920×1080 displays hooked up right now. xrandr reports:

Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192

To display a window on the right-hand monitor, I can give a geometry string that has its x co-ordinates between 1920…3839 (inclusive).

The usual format is: width x height ± x-offset ± y-offset ­­— but the width and height are optional, if you prefer to take the defaults. The ± are + to count relative to the top/left, or - to count relative to the bottom/right.

So, for example:

gedit --geometry 800x600+1920+0  # set size at top-left of right screen
gedit --geometry +1920+100       # default size at top-left of right screen
gedit --geometry -0+0            # default size at top-right of entire display

Unfortunately, the only programmatic way I know of to determine the area of the display on each monitor from the shell would be to parse the output from xrandr; e.g.

$ xrandr
Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
LVDS1 connected (normal left inverted right x axis y axis)
   1366x768       60.0 +
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   1920x1080      60.0*+
   1680x1050      60.0  
   1280x1024      60.0  
   1440x900       59.9  
   1280x720       60.0  
   1024x768       60.0  
   800x600        60.3  
   640x480        60.0  
   720x400        70.1  
HDMI1 connected 1920x1080+1920+0 (normal left inverted right x axis y axis) 510mm x 287mm
   1920x1080      60.0*+
   1680x1050      59.9  
   1280x1024      60.0  
   1440x900       59.9  
   1280x720       60.0  
   1024x768       60.0  
   800x600        60.3  
   640x480        60.0  
   720x400        70.1  
DP1 disconnected (normal left inverted right x axis y axis)

$ xrandr | perl -ne 'if (/(d+)x(d+)+(d+)+(d+)/) '
> ' { print "$3,$4 - ", $3 + $1 - 1, ",", $4 + $2 - 1, "n" }'
0,0 - 1919,1079
1920,0 - 3839,1079

(You’d normally want to avoid splitting the Perl one-liner across two lines in the shell, but the '' trick there is to make it legible on SO.)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement