How to run an application in bash and choose which monitor it works on?

I have a question that is difficult to find for an answer (I always get answers for manipulating the monitor). I am writing a bash shell script to help me with my dev code, and I have two monitors.

When I run my executable file that I compiled, I want to tell him that it runs on a specific monitor (i.e., it is different from the monitor on which my terminal is open, so I can view debugging information on one screen and turn it on the application is different).

How can I do it? Something like:

./myProject > but run on monitor 2 

Where myProject is my binary executable.

Thanks to everyone.

+8
linux qt
source share
5 answers

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

If you are using Xinerama (distributing one logical output to multiple monitors), however:

  • In addition: X11 dictionary: "Display" is one or more "screens" with input devices; for example a keyboard and mouse, aka "place". A “screen” is a logical canvas that is partially or fully displayed on one or more “monitors”; when using multiple monitors for one “screen”, the windows can be partially displayed on each monitor, but they have the same X11 DISPLAY identifier; This is called Xinerama. The DISPLAY format is the host number : display-number . screen-id, for example. on my Xinerama setup, both monitors are part of screen 0 on the displayed number, which counts from 0 with every registered user on the same host. "Seats" are logical monitoring + input groups that use different hardware; multiple "mappings" may occur using the "virtual console" switching, as Gnome and KDE allow multiple users to register on the same "seat".

Most GUI tools allow you to specify window geometry using the --geometry or -geometry .

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

  • This assumes that you allow Qt to post-process your command line, for example. passing argv to QtApplication or similar.

The "logical display" will have a resolution that is the sum of the resolutions in each direction of the location of your monitors. For example, I have 2 × 1920 × 1080 displays connected right now. xrandr reports:

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

To display the window on the right monitor, I can specify a geometry line that has x coordinates between 1920 ... 3839 (inclusive).

The usual format is width x height ± x-offset ± y-offset - but width and height are optional if you prefer to accept the default values. ± are + for counting relative to the top / left or - for counting 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 software way I know to determine the display area on each monitor from the shell would be to analyze the output from xrandr ; eg.

 $ 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 

(Normally, you need to avoid splitting the single-line layer into two lines in the shell, but the '\ ... ' trick should make it legible on SO.)

+7
source share

Use fifo

open the terminal window on the monitor on which you want the output to be displayed and execute

 mkfifo /tmp/myfifo cat /tmp/myfifo 

then on the source terminal do

 ./myProject >/tmp/myfifo 

This is supposed to be a console application. If it is graphical, you will need a different approach, which will depend on which window manager + you use.

+4
source share

All you have to do is set the DISPLAY environment variable before starting the application.

To find out what you need to install, run the following on the monitor on which you want to turn it on:

 echo $DISPLAY 

You should see, for example :0.1 or :0.0 .

You can then indicate that the application should start on this display as follows:

 DISPLAY=:0.1 ./my_app 
+3
source share

The answer --geometry given above and accepted simply will not work in many cases ...

There are many almost identical questions, such as floating around various StackExchange and AskUbuntu sites, the answer I found (in the Linux Mint based on Ubuntu 14.04) should use wmctrl . I leave the answer because no one mentioned this in this thread.

(There's another called Devil Pie and another called Compiz , if you look for them too, you'll find the Q & A that I'm talking about)

wmctrl is a simple unix tool that you are probably looking for when writing Bash scripts. I also saw that someone suggests using xdotool , depending on the specific purpose.

wmctrl offers window matching by window name or pid (incompatible with all types of windows controlled by X)

Some useful resources:

I connect a second monitor to the left or right, depending on where I work every day, and I think that the solution for me will include

  • find dimensions from xrandr (as shown in BRPocock answer)
  • parsing what is an external connected monitor (VGA / HDMI, etc.) and not built-in,
  • defining measurement, which gives the maximum window on the connected screen (i.e. left / right / top / bottom offset, which will vary depending on the side of the screen used)

Leaving his notes and [in the end], some code produced here in case it would be useful to anyone else.

+2
source share

As your application uses QT, you are probably using KDE. In System Settings > Window Behavior > Advanced set Placement to Under Mouse . Click the desired monitor, ALT + Tab, to switch to your terminal, and run the program.

0
source share

All Articles