如何在我正在工作的同一显示器中打开文件?

Goo*_*bot 5 xorg multiple-monitors unity

我已将 LCD 连接到我的笔记本电脑。当我尝试在 Nautilus 中打开文件时,目标应用程序会在我的笔记本电脑显示器中打开,而不是在第二个显示器(其中 nautilus 窗口打开)中打开。

我不想更改默认显示。我想在我工作的显示器中打开窗口。如果我的文件管理器在笔记本电脑显示器中,我希望应用程序在笔记本电脑显示器中打开。如果我的文件管理器在外部显示器中,我希望在那里打开文件。

的输出 xrandr

Screen 0: minimum 320 x 200, current 3286 x 1080, maximum 32767 x 32767
eDP1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 256mm x 144mm
   1366x768       60.1*+
   1360x768       59.8     60.0  
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 connected primary 1920x1080+1366+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080      60.0*    50.0     59.9  
   1920x1080i     60.1     50.0     60.0  
   1680x1050      59.9  
   1280x1024      75.0     60.0  
   1440x900       59.9  
   1280x960       60.0  
   1280x800       59.9  
   1152x864       75.0  
   1280x720       60.0     50.0     59.9  
   1440x576i      50.1  
   1024x768       75.1     70.1     60.0  
   1440x480i      60.1     60.1  
   832x624        74.6  
   800x600        72.2     75.0     60.3     56.2  
   720x576        50.0  
   720x480        60.0     59.9  
   640x480        75.0     72.8     66.7     60.0     59.9  
   720x400        70.1  
DP1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
Run Code Online (Sandbox Code Playgroud)

Jac*_*ijm 7

您描述的行为(在当前屏幕上打开窗口)应该是默认行为,在我的 14.04 上就是这样。

由于与某些图形驱动程序/GPU 组合的轻微不兼容,在某些情况下可能会出现“特殊性”。如果没有可用的“干净”选项(修复),您可以使用下面的解决方法。
它存在一个后台脚本,寻找要出现的新窗口。如果存在新窗口,脚本会将窗口位置与当前鼠标位置进行比较。如果鼠标和新窗口不在同一屏幕上,则使用xdotoolwindowmove` 命令移动窗口。

背景脚本是一个坏主意吗?

如果您不需要后台脚本,请不要使用它。
同时:如果它添加了重要的功能和/或节省了您的时间,如果脚本组织良好并且因此“燃料不足” ,那么不这样做是很愚蠢的。

作为参考:在我的笔记本电脑和台式机上,我经常运行至少5 个后台脚本 + 偶尔一些额外的用于测试目的的脚本,没有任何通知。

为节省燃料采取了哪些措施:

  1. 该脚本具有可变循环周期,
    每 10 秒一次,脚本检查要连接的第二个屏幕。如果没有,脚本将跳过整个窗口检查过程并在 10 秒后重新检查。这意味着该脚本在附加了第二个屏幕时才起作用。连接第二个屏幕后,在 10 秒内,循环将更改为 2 秒的周期。
  2. 脚本采取的所有(下一个)动作都是有条件的,
    例如只有有新窗口等时才会检查鼠标位置。

总之,在我的系统上,由于脚本的原因,我无法注意到或测量任何额外的负载。

剧本

#!/usr/bin/env python3
import subprocess
import time

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

def screen_limit():
    screendata = [s for s in get("xrandr").split() if s.count("+") == 2]
    if len(screendata) == 2:
        return int([s.split("x")[0] for s in screendata if "+0+0" in s][0])

wd1 = get(["wmctrl", "-lG"])

t = 0
while True:
    time.sleep(2)
    # once per 10 seconds, check for a second screen
    if t == 0: 
        while True:
            rightside = screen_limit()
            # if no second screen, skip the procedure
            if rightside == None:
                time.sleep(10)
            else:
                break
    wd2 = get(["wmctrl", "-lG"])
    # check for buggy wmctrl
    if all([wd2 != None, wd1 != None]):
        wins = [w.split() for w in wd2.splitlines()]
        # check for new windows
        relevant = [w for w in wins if not w[0] in wd1]
        if relevant:
            # if new windows appeared, see if they match the mouse pos
            mousepos = int(get([
                "xdotool", "getmouselocation"
                ]).split()[0].split(":")[1])
            for w in relevant:
                check = [mousepos < rightside, int(w[2]) < rightside]
                if check[0] != check[1]:
                    # if mouse and window are not on the same screen > move
                    if check[0] == False:
                        cmd = ["xdotool", "windowmove", w[0],
                            str(int(w[2]) + rightside), w[3]]                    
                    else:
                        cmd = ["xdotool", "windowmove", w[0],
                            str(int(w[2]) - rightside), w[3]]
                    subprocess.Popen(cmd)
    wd1 = wd2
    t = 0 if t == 10 else t
    t += 1
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 该脚本同时需要wmctrlxdotool。在终端中运行:

    sudo apt-get install xdotool wmctrl
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将脚本复制到一个空文件中,另存为 move_windows.py
  3. 通过以下命令测试运行脚本:

    python3 /path/to/move_windows.py
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如果一切正常,请将其添加到启动应用程序:Dash > 启动应用程序 > 添加。添加命令:

    /bin/bash -c "sleep 15 && python3 /path/to/move_windows.py"
    
    Run Code Online (Sandbox Code Playgroud)