我正在使用 Ubuntu 14.04,我希望强制特定程序以预定的屏幕分辨率运行,并在关闭程序后让屏幕恢复到我的默认分辨率。该程序是括号文本编辑器当我在 1024*768 下运行 Brackets 时,扩展管理器显示不完整,如下图所示。
1280*1024 显示效果很好,但是对我的眼睛来说很不舒服。
这是我的xrandr
命令输出:
Screen 0: minimum 8 x 8, current 1024 x 768, maximum 32767 x 32767
VGA1 connected primary 1024x768+0+0 (normal left inverted right x axis y axis) 340mm x 255mm
1600x1200 74.8
1280x1024 85.0 75.0
1024x768 85.0 75.1* 70.1 60.0
1024x768i 87.1
832x624 74.6
800x600 85.1 72.2 75.0 60.3 56.2
640x480 85.0 75.0 72.8 66.7 60.0
720x400 87.8 70.1
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
答案1
您可以使用以下 Python 脚本以给定的分辨率启动您的应用程序:
#!/usr/bin/env python3
import argparse
import re
import subprocess
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
parser.add_argument('--resolution', required=True)
parser.add_argument('APP')
args = parser.parse_args()
device_context = '' # track what device's modes we are looking at
modes = [] # keep track of all the devices and modes discovered
current_modes = [] # remember the user's current settings
# Run xrandr and ask it what devices and modes are supported
xrandrinfo = subprocess.Popen('xrandr -q', shell=True, stdout=subprocess.PIPE)
output = xrandrinfo.communicate()[0].decode().split('\n')
for line in output:
# luckily the various data from xrandr are separated by whitespace...
foo = line.split()
# Check to see if the second word in the line indicates a new context
# -- if so, keep track of the context of the device we're seeing
if len(foo) >= 2: # throw out any weirdly formatted lines
if foo[1] == 'disconnected':
# we have a new context, but it should be ignored
device_context = ''
if foo[1] == 'connected':
# we have a new context that we want to test
device_context = foo[0]
elif device_context != '': # we've previously seen a 'connected' dev
# mode names seem to always be of the format [horiz]x[vert]
# (there can be non-mode information inside of a device context!)
if foo[0].find('x') != -1:
modes.append((device_context, foo[0]))
# we also want to remember what the current mode is, which xrandr
# marks with a '*' character, so we can set things back the way
# we found them at the end:
if line.find('*') != -1:
current_modes.append((device_context, foo[0]))
for mode in modes:
if args.output == mode[0] and args.resolution == mode[1]:
cmd = 'xrandr --output ' + mode[0] + ' --mode ' + mode[1]
subprocess.call(cmd, shell=True)
break
else:
print('Unable to set mode ' + args.resolution + ' for output ' + args.output)
sys.exit(1)
subprocess.call(args.APP, shell=True)
# Put things back the way we found them
for mode in current_modes:
cmd = 'xrandr --output ' + mode[0] + ' --mode ' + mode[1]
subprocess.call(cmd, shell=True)
保存上述脚本(例如my-script.py
)并使其可执行:
chmod +x my-script.py
要设置分辨率1280x1024
并开始,gedit
只需输入:
./my_script.py --output VGA1 --resolution 1280x1024 gedit
为了避免每次都输入此命令,请将脚本保存在您的主目录中,并将以下几行添加到您的.bashrc
:
alias my_bracket='~/my_script.py --output VGA1 --resolution 1280x1024 gedit'
或者更好的是,修改包安装的桌面文件/usr/local/share/applications/brackets.desktop
。
sudo gedit /usr/local/share/applications/brackets.desktop
并用以下新行替换文件内容:
[Desktop Entry]
Name=Brackets
Type=Application
Categories=Development
Exec=/home/mushir/my_script.py --output VGA1 --resolution=1280x1024 /opt/brackets/brackets
Icon=brackets
MimeType=text/html;
Keywords=Text;Editor;Write;Web;Development;
答案2
这里不使用 ubuntu(Gentoo 用户),但寻找 xrandr 包。通常你可以使用类似
xrandr --output VGA-1 --mode 640x480
改变分辨率和
xrandr --output VGA-1 --preferred
将恢复默认分辨率。
xrandr
没有选项将会给你显示名称和分辨率。
刚刚看到之前的脚本版本也使用了 xrandr :)。但也许你仍然会发现这些信息很有用。阅读手册页以了解选项调整
man xrandr