umonitor-Linux上热插拔显示器的更新解决方案

时间:2020-03-05 15:31:18  来源:igfitidea点击:

本教程将介绍umonitor在Linux上的安装和基本用法。
umonitor是用C编写的动态监视器管理,并作为单个二进制包分发。
动态监视器管理这个名称意味着屏幕监视器的位置和分辨率在热插拔时将自动更新。

我们将使用arch linux安装umonitor并查看如何使用它。

安装umonitor

可以通过从源文件生成二进制软件包来安装umonitor。
对于Arch Linux用户,我们可以使用yaourt或者类似的软件包管理器从AUR安装umonitor。

在Arch Linux上安装umonitor

如果我们运行的是Arch Linux或者其任何衍生版本(例如Manjaro),则可以使用AUR包装器轻松安装umonitor。
在使用AUR之前,请确保产品中包含以下几行

[archlinuxfr]
SigLevel = Never
Server = http://repo.archlinux.fr/$arch

更新软件包索引并安装umonitor,

$sudo pacman -Sy
:: Synchronizing package databases...
 core 145.0 KiB 780K/s 00:00        [##########################] 100%
 extra 1783.3 KiB 775K/s 00:02      [##########################] 100%
 community 4.7 MiB 659K/s 00:07     [##########################] 100%
 multilib 179.6 KiB 6.50M/s 00:00   [##########################] 100%
 archlinuxfr 11.5 KiB 0.00B/s 00:00 [##########################] 100%

更新后,使用yaourt命令行工具安装umonitor:

$yaourt -S umonitor-git --noconfirm

我们可以使用以下方法确认已安装的软件包状态:

$pacman -Qi umonitor-git
Name : umonitor-git
Version : r161.06bff8e-1
Description : Dynamic monitor configuration
Architecture : x86_64
URL : https://github.com/rliou92/umonitor
Licenses : MIT
Groups : None
Provides : None
Depends On : libconfig libxcb
Optional Deps : None
Required By : None
Optional For : None
Conflicts With : None
Replaces : None
Installed Size : 50.00 KiB
Packager : Unknown Packager
Build Date : Sat 16 Jun 2016 11:50:47 PM EAT
Install Date : Sat 16 Jun 2016 11:50:48 PM EAT
Install Reason : Explicitly installed
Install Script : Yes
Validated By : None

在其他Linux发行版上安装umonitor

对于其他Linux发行版,我们需要从源代码文件生成二进制包。
基本的依赖关系是cmake和git。

在Debian基本系统上,使用以下命令安装git:

$sudo apt-get install software-properties-common
$sudo apt-get install gcc cmake 
$sudo apt-get install git
$git clone https://github.com/rliou92/umonitor.git
Cloning into 'umonitor'...
remote: Counting objects: 5017, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 5017 (delta 32), reused 43 (delta 26), pack-reused 4962
Receiving objects: 100% (5017/5017), 11.48 MiB | 628.00 KiB/s, done.
Resolving deltas: 100% (2519/2519), done.

对于CentOS/RHEL/Fedora:

$sudo yum install git gcc cmake
$sudo yum install git cmake # For Fedora
$git clone https://github.com/rliou92/umonitor.git
Cloning into 'umonitor'...
remote: Counting objects: 5017, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 5017 (delta 32), reused 43 (delta 26), pack-reused 4962
Receiving objects: 100% (5017/5017), 11.48 MiB | 628.00 KiB/s, done.
Resolving deltas: 100% (2519/2519), done.

然后使用make命令生成二进制包:

$cd umonitor
$make
gcc -Wall -c src/screen.c -o obj/screen.o
gcc -Wall -c src/load.c -o obj/load.o
gcc -Wall -c src/save.c -o obj/save.o
gcc -Wall -c src/autoload.c -o obj/autoload.o
gcc -Wall -c src/umonitor.c -o obj/umonitor.o
gcc obj/screen.o obj/load.o obj/save.o obj/autoload.o obj/umonitor.o -Wall -lX11 -lxcb-randr -lxcb -lconfig -o bin/umonitor

二进制包将放在目录./bin中

$ls -1 bin 
umonitor

使文件可执行,然后将文件复制到$PATH中的/usr/local/bin

$chmod +x ./bin/umonitor
$sudo cp bin/umonitor /usr/local/bin/

监护人用法

要获得有关如何使用umonitor的基本帮助,请运行以下命令:

$umonitor --help
Usage: umonitor [OPTION]
Options:
-s,--save <profile_name> Saves current setup into profile_name
-d,--delete <profile_name> Removes profile_name from configuration file
-l,--load <profile_name> Loads setup from profile name
-n,--listen Daemonizes, listens for changes in the setup, and applies the new configuration automatically
-a,--autoload Load profile that matches with current configuration once
--quiet Supress program output
--help Display this help and exit
--version Output version information and exit

我们可以使用标准的xrandr或者arandr工具设置显示器的分辨率和位置。
适应设置后,通过执行以下命令将其保存到配置文件中:

$umonitor --save <profile_name>

这样可以节省:

  • 监视器供应商名称+型号
  • CRTC X和Y位置
  • 屏幕分辨率
  • 主要输出
  • 回转

umonitor配置文件存储在“~/.config/umon.conf”中。

$umonitor --load <profile_name>

要删除保存的配置文件,请使用:

$umonitor --delete <profile_name>

如果只有一个监视器(例如“笔记本电脑”屏幕),则可以使用以下命令将分辨率和屏幕设置保存到配置文件中:

$umonitor --save laptop
Profile home saved!

对于HDMI等外部显示器,请在设备中插入插件并使用xrandr对其进行配置,例如

$xrandr --output HDMI-1 --mode 1920x1080 --pos 1600x0
$xrandr --output eDP1 --mode 1600x900 --pos 0x0

然后将配置保存到umonitor配置文件:

$umonitor --save mymon
Profile mymon saved!

自动检测变化

Umonitor可以自动检测更改,但是我们必须守护该应用程序以实现此目的。
只需将--listen选项传递给umonitor命令即可。

$umonitor --listen

在开机时启动umonitor

为了能够在启动时启动umonitor程序,请将以下行添加到~/.xinitrc

umonitor --listen --quiet