Wlroots 双屏下鼠标移动问题

  • 2022年 2月20日
  • 读完约需 5 分钟
  • • 
  • 标签: 
  • wayland
  • wloots
  • 最后更新于 2022年 2月20日

最后找到了这个参考帖,原因、解决方案都说得非常清楚,理论上 wlroots 的窗口管理器都可能出现这个问题,比如文中是dwl,我是用的river。用中文当一次复读机,备忘。会先贴英文原文,再用中文阐述,方便对照。

现象

dwl version 0.2.1-3 mesa version 21.1.6-1 wlroots version 0.14.1-1 wayland version 1.19.0-1 wlr-randr version 0.2.0-1
When using two monitors with different DPI, we can use the wlr-output-management-unstable-v1 protocol to adjust the screen scaling. I am using wlr-randr with the –scale option to achieve this. However, the mouse cursor becomes restricted to one screen after applying a scale factor. Setting both screens to the same scaling does not help, unless they are both set to x1 scaling, in which case the cursor can move between them again.
Weston doesn’t support the protocol, but I couldn’t find anything about this on the Sway tracker, so maybe they have been able to solve this.

wlroots 催生了一大堆原生 wayland 窗口管理器,作者提到它支持 wlr-output-management-unstable-v1 协议,Weston 还不支持。故障现象是:用 kanshi、wlr-randr,对多个显示器的分辨率和 dpi 进行设置。设置完毕后启用,就会发现鼠标只能在第一显示器范围内移动,无法移动到第二显示器。

原因分析

第二显示器的位置,是按缩放后的数值进行计算的。当两个显示器的 dpi 设置不为 1 时,如果还按照之前的计算方法,就会导致两个显示器显示区域之间出现空隙,导致光标无法移动到另一个显示器。

Solved the issue. The problem was that the new monitor position is always applied after the scaling, and therefore I had a gap between the two monitors. The position needs to be corrected to account for the scaling factor.

解决方法

%zsh> cat .config/kanshi/config
profile dual {
	output HDMI-A-1 enable scale 1.4 position 0,0 
    output DVI-I-1 enable scale 1.2 position -1920,0	
}

In this case, a scaling factor of 1.6 means that the horizontal position of a 1920x1080 monitor should be 1920/1.4 = 1372 units away from the origin. Note that this is rounded up to the next integer, because the tools I have mentioned do not support floats for positioning (maybe it is prohibited by the protocol). I found that 1371 caused issues, better to have a very small gap than an overlap in the outputs.
An example wlr-randr command to achieve what I had wanted is:

wlr-randr --output DVI-I-1 --pos -1372,0 --scale 1.4

简单来说,第二显示器的 Position计算公式应该是 Position/scale,Position 和 scale 的取值跟第二显示器的位置有关,如果它在左边,就取它的,如果是在右边,就取第一显示器的。上例中,HDMI 显示器在 (0,0) 位置,DVI 显示器在 HDMI 显示器的左边,所以它的 x 坐标理论上应该是 DVI 的显示器宽度取负值,即 -1920,位置是 (-1920,0)。但实际上应该是 -1920 / 1.4 = -1372。这里除不尽时,需注意取整方向,原则是留空比重叠好。下面用一个表格来总结一下第二显示器的 Position 取值:

第二显示器位置Position.XPosition.Y取整方式
在左-D2.X/D2.scale0绝对值向上取整,再加符号
在右D1.X/D1.scale0去掉小数部分
在上0-D2.Y/D2.scale绝对值向上取整,再加符号
在下0D1.Y/D1.scale去掉小数部分

可以根据这个表扩展更多显示器时的设置。