code_fodder
code_fodder

Reputation: 16401

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.

Upvotes: 19

Views: 42883

Answers (5)

Louis Maddox
Louis Maddox

Reputation: 5576

The --geometry answer given above and accepted simply won't work in many cases...

There are a lot of near-identical questions like this floating around the various StackExchange sites & AskUbuntu, the answer I've eventually found (on a Linux Mint distro based on Ubuntu 14.04) is to use wmctrl. I'm leaving an answer just since no one else has mentioned it on this thread.

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

wmctrl is the sort of simple unix tool you're probably looking for if you're writing Bash scripts. I also saw someone suggest using xdotool, depends what the specific goal is.

wmctrl offers window matching by window title or pid (not compatible with all types of X-managed windows)

Some helpful resources:

I connect a second monitor on the left or on the right depending on where I'm working each day, and I think the solution for me will involve

  • finding the dimensions from xrandr (as shown in BRPocock's answer),
  • parsing that to tell which is the external connected monitor (VGA/HDMI etc.) rather than the inbuilt one,
  • specifying a dimension to give to a maximised window on the connected screen (i.e. the left/right/top/bottom offset, which will change according to side of the screen being used)

Leaving my notes and [eventually] some code produced here in case it's useful to anyone else.

Upvotes: 8

BRPocock
BRPocock

Reputation: 13934

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.)

Upvotes: 11

John Ledbetter
John Ledbetter

Reputation: 14203

All you need to do is set the DISPLAY environmental variable prior to running your application.

To find out what you need to set it to, run the following on the monitor you want it to show up on:

echo $DISPLAY

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

Then you can specify that you want your app to run on that display like so:

DISPLAY=:0.1 ./my_app

Upvotes: 4

Fabio A. Correa
Fabio A. Correa

Reputation: 2036

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 start the program.

Upvotes: 0

Oliver Matthews
Oliver Matthews

Reputation: 7863

Use a fifo

open a terminal window on the monitor you want the output to appear on and do

mkfifo /tmp/myfifo
cat /tmp/myfifo

then on the source terminal do

./myProject >/tmp/myfifo

This assumes it is a console app. If it is graphical then you will need another approach, which will be dependent on what windowing manager + toolkit you are using.

Upvotes: 4

Related Questions