Linux 如何在 bash 中运行应用程序并选择它在哪个监视器上运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18360569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you run an application in bash and select which monitor it runs on?
提问by code_fodder
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.
我有一个很难找到答案的问题(我总是以监视器操作的答案结束)。我正在编写一个 bash shell 脚本来帮助我开发代码,并且我有两个监视器。
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.
myProject
我的二进制可执行文件在哪里。
Thanks all.
谢谢大家。
采纳答案by BRPocock
If you run separate displays on each monitor (less likely these days), the DISPLAY
environment variable is what you want.
如果您在每个监视器上运行单独的显示(现在不太可能),DISPLAY
环境变量就是您想要的。
If you use Xinerama (spreading one logical display across multiple monitors), however:
但是,如果您使用 Xinerama(在多个显示器上分布一个逻辑显示),则:
- 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. TheDISPLAY
format is host:
display-number.
screen-id, so e.g. on my Xinerama set-up both monitors are part of screen0
on a display number that counts up from0
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.
- 旁白:X11 词汇:“显示器”是一个或多个带有输入设备的“屏幕”;例如键盘和鼠标,又名“座位”。“屏幕”是部分或完全显示在一个或多个“监视器”上的逻辑画布;当为一个“屏幕”使用多个显示器时,窗口可以在每个显示器上部分显示,但共享相同的 X11
DISPLAY
标识符;这被称为Xinerama。该DISPLAY
格式是主机:
显示号码.
屏幕-ID,所以如在我的Xinerama建立两个监视器屏幕的一部分0
上的显示数字,数了从0
与同一主机上的每个登录用户。“座位”是使用不同硬件的监视器+输入的逻辑组;使用“虚拟控制台”切换可以出现多个“显示”,这就是 Gnome 和 KDE 允许多个用户在单个“座位”机器上登录的方式。
Most GUI toolkits allow you to specify the window's geometry using the --geometry
or -geometry
switch.
大多数 GUI 工具包允许您使用--geometry
或-geometry
开关指定窗口的几何形状。
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
intoQtApplication
or similar.
Qt 使用旧的 MIT 风格的
-geometry
形式。GTK+/Gnome 使用 GNU 风格的--geometry
.这是假设你允许Qt来后处理您的命令行,例如,通过
argv
进QtApplication
或相似。
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:
“逻辑显示器”的分辨率是显示器排列的每个方向的分辨率之和。例如,我现在连接了 2 × 1920 × 1080 显示器。xrandr
报告:
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).
为了在右侧显示器上显示一个窗口,我可以给出一个几何字符串,其x
坐标在 1920…3839(含)之间。
The usual format is: widthx
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.
通常的格式是:width x
height ±
x-offset ±
y-offset--- 但是宽度和高度是可选的,如果您喜欢采用默认值。的±
是+
相对于计数的顶部/左,或-
相对于计数的底部/右。
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.
不幸的是,我所知道的从 shell 确定每个监视器上显示区域的唯一编程方法是解析来自xrandr
; 例如
$ 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 ", - ", + - 1, ",", + - 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.)
(您通常希望避免将 Perl 单行代码拆分成 shell 中的两行,但是'\
……'
诀窍是让它在 SO 上清晰易读。)
回答by Oliver Matthews
Use a fifo
用一个 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.
这假设它是一个控制台应用程序。如果它是图形的,那么您将需要另一种方法,这取决于您使用的窗口管理器 + 工具包。
回答by Fabio A. Correa
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.
由于您的应用程序使用 QT,因此您可能正在使用 KDE。在 中System Settings > Window Behavior > Advanced
,设置Placement
为Under Mouse
。单击所需的监视器,ALT+Tab 切换到您的终端,并启动程序。
回答by John Ledbetter
All you need to do is set the DISPLAY
environmental variable prior to running your application.
您需要做的就是DISPLAY
在运行应用程序之前设置环境变量。
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
.
例如,您应该看到:0.1
或:0.0
。
Then you can specify that you want your app to run on that display like so:
然后您可以指定您希望您的应用程序在该显示器上运行,如下所示:
DISPLAY=:0.1 ./my_app
回答by Louis Maddox
The --geometry
answer given above and accepted simply won't work in many cases...
--geometry
上面给出并接受的答案在许多情况下根本不起作用......
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.
在各种 StackExchange 站点和 AskUbuntu 上有很多类似的问题,我最终找到的答案(在基于 Ubuntu 14.04 的 Linux Mint 发行版上)是使用wmctrl
. 我要留下一个答案,因为没有其他人在这个线程上提到过它。
(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)
(还有一个叫Devil's Pie
和另一个名为Compiz
如果搜索那些过于你会发现Q&A的我说的)
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
如果您正在编写 Bash 脚本,那么您可能正在寻找一种简单的 unix 工具。我也看到有人建议使用xdotool
,取决于具体目标是什么。
wmctrl
offers window matching by window title or pid
(not compatible with all types of X-managed windows)
wmctrl
提供按窗口标题匹配的窗口或pid
(与所有类型的 X 管理窗口不兼容)
Some helpful resources:
一些有用的资源:
wmctrl
man page- user doc 1
- user doc 2
- “How to shift applications from workspace 1 to 2 using command?"”
- More specific answer RE: specifying the dimensions
- “Both
wmctrl
andxdotool
work fine if you set a window maximized.”
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)
- 从中找到尺寸
xrandr
(如BRPocock的回答所示), - 解析它以判断哪个是外部连接的显示器(VGA/HDMI 等)而不是内置显示器,
- 指定一个尺寸以提供给连接屏幕上的最大化窗口(即左/右/上/下偏移,这将根据所使用的屏幕的侧面而变化)
Leaving my notes and [eventually] some code produced herein case it's useful to anyone else.
留下我的笔记和 [最终]在这里生成的一些代码,以防对其他人有用。