【问题标题】:How can I get current active screen (a screen with mouse) in KDE wayland?如何在 KDE Wayland 中获取当前活动屏幕(带鼠标的屏幕)?
【发布时间】:2022-06-20 09:03:36
【问题描述】:

我希望能够从我的 python 脚本中检测 KDE 中的活动屏幕。以前我用 Xlib 并像这样用鼠标寻找屏幕:

from Xlib import display
def mousepos():
    data = display.Display().screen().root.query_pointer()._data
    return data["root_x"], data["root_y"]

current_pos = mousepos()
x_pos = current_pos[0]
y_pos = current_pos[1]

names_handling_y = []
names_handling_x = []
...
    if x_pos >= output["pos"]["x"] and x_pos <= output["pos"]["x"] + output["size"]["width"]:
        names_handling_x.append(output["name"])
    if y_pos >= output["pos"]["y"] and y_pos <= output["pos"]["y"] + output["size"]["height"]:
        names_handling_y.append(output["name"])
...
for name in names_handling_x:
    if name in names_handling_y:
        target_output = name

但是在使用 Wayland 时我该如何做类似的事情呢?

【问题讨论】:

    标签: python x11 kde-plasma wayland


    【解决方案1】:

    我找到了以下方法。您可以对活动屏幕运行 dbus 查询(请参阅 api,在 commit 中添加)或运行 kwin 脚本。可以从另一个脚本run a kwin script。所以我可以这样做:

    #!/usr/bin/env python3
    import subprocess
    from datetime import datetime
    
    # KDE Active screen is a screen with mouse cursor.
    
    def get_active_screen_id():
        datetime_now = datetime.now()
    
        file = open("/tmp/get_active_screen.js", "w")
        script = """
        as = workspace.activeScreen;
        print(as + 1)
        """
        file.write(script)
        file.close
        del file, script
    
        script = "/tmp/get_active_screen.js"
    
        reg_script_number = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin \
                                /Scripting org.kde.kwin.Scripting.loadScript \
                                string:" + script + " | awk 'END {print $2}'",
                                capture_output=True, shell=True).stdout.decode().split("\n")[0]
    
        subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.run",
                        shell=True, stdout=subprocess.DEVNULL)
        subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /" + reg_script_number + " org.kde.kwin.Script.stop",
                        shell=True, stdout=subprocess.DEVNULL)  # unregister number
    
        since = str(datetime_now)
    
        msg = subprocess.run("journalctl _COMM=kwin_wayland -o cat --since \"" + since + "\"",
                                capture_output=True, shell=True).stdout.decode().rstrip().split("\n")
        msg = [el.lstrip("js: ") for el in msg]
        if len(msg) != 1:
            exit(1)
    
        return int(msg[0])
    
    
    active_screen_id = get_active_screen_id()
    
    

    然后我可以在my script 中使用该ID。这种方法比Xlib方法中确定鼠标位置的屏幕坐标还要好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多