[xrandr] Fix neighbor functionality when some displays are excluded

This commit is contained in:
Naya Verdier 2020-07-25 10:52:10 -07:00
parent 8f57bb952d
commit 5874850bd5
No known key found for this signature in database
GPG key ID: 1A59389D46A94A4C

View file

@ -17,7 +17,6 @@ Requires the following executable:
* xrandr * xrandr
""" """
import os
import re import re
import sys import sys
@ -32,7 +31,7 @@ import util.format
try: try:
import i3 import i3
except: except ModuleNotFoundError:
pass pass
@ -42,35 +41,40 @@ class Module(core.module.Module):
super().__init__(config, theme, []) super().__init__(config, theme, [])
self._exclude = tuple(filter(len, self.parameter("exclude", "").split(","))) self._exclude = tuple(filter(len, self.parameter("exclude", "").split(",")))
self._active_displays = []
self._autoupdate = util.format.asbool(self.parameter("autoupdate", True)) self._autoupdate = util.format.asbool(self.parameter("autoupdate", True))
self._needs_update = True self._needs_update = True
try: try:
i3.Subscription(self._output_update, "output") i3.Subscription(self._output_update, "output")
except: except NameError:
pass pass
def _output_update(self, event, data, _): def _output_update(self, event, data, _):
self._needs_update = True self._needs_update = True
def update(self): def update(self):
self.clear_widgets() if not self._autoupdate and not self._needs_update:
if self._autoupdate == False and self._needs_update == False:
return return
self.clear_widgets()
self._active_displays.clear()
self._needs_update = False self._needs_update = False
for line in util.cli.execute("xrandr -q").split("\n"): for line in util.cli.execute("xrandr -q").split("\n"):
if not " connected" in line: if " connected" not in line:
continue continue
display = line.split(" ", 2)[0] display = line.split(" ", 2)[0]
resolution = re.search(r"\d+x\d+\+(\d+)\+\d+", line)
if resolution:
self._active_displays.append(display)
if display.startswith(self._exclude): if display.startswith(self._exclude):
continue continue
resolution = re.search(r"\d+x\d+\+(\d+)\+\d+", line)
widget = self.widget(display) widget = self.widget(display)
if not widget: if not widget:
widget = self.add_widget(full_text=display, name=display) widget = self.add_widget(full_text=display, name=display)
@ -79,7 +83,7 @@ class Module(core.module.Module):
widget.set("state", "on" if resolution else "off") widget.set("state", "on" if resolution else "off")
widget.set("pos", int(resolution.group(1)) if resolution else sys.maxsize) widget.set("pos", int(resolution.group(1)) if resolution else sys.maxsize)
if self._autoupdate == False: if not self._autoupdate:
widget = self.add_widget(full_text="") widget = self.add_widget(full_text="")
widget.set("state", "refresh") widget.set("state", "refresh")
core.input.register(widget, button=1, cmd=self._refresh) core.input.register(widget, button=1, cmd=self._refresh)
@ -91,9 +95,7 @@ class Module(core.module.Module):
self._needs_update = True self._needs_update = True
def _toggle(self, event): def _toggle(self, event):
self._refresh(event) if util.format.asbool(self.parameter("overwrite_i3config", False)):
if util.format.asbool(self.parameter("overwrite_i3config", False)) == True:
toggle_cmd = utility("toggle-display.sh") toggle_cmd = utility("toggle-display.sh")
else: else:
toggle_cmd = "xrandr" toggle_cmd = "xrandr"
@ -102,41 +104,20 @@ class Module(core.module.Module):
if widget.get("state") == "on": if widget.get("state") == "on":
util.cli.execute("{} --output {} --off".format(toggle_cmd, widget.name)) util.cli.execute("{} --output {} --off".format(toggle_cmd, widget.name))
elif not self._active_displays:
util.cli.execute("{} --output {} --auto".format(toggle_cmd, widget.name))
else: else:
first_neighbor = next( if event["button"] == core.input.LEFT_MOUSE:
(widget for widget in self.widgets() if widget.get("state") == "on"), side, neighbor = "left", self._active_displays[0]
None,
)
last_neighbor = next(
(
widget
for widget in reversed(self.widgets())
if widget.get("state") == "on"
),
None,
)
neighbor = (
first_neighbor
if event["button"] == core.input.LEFT_MOUSE
else last_neighbor
)
if neighbor is None:
util.cli.execute(
"{} --output {} --auto".format(toggle_cmd, widget.name)
)
else: else:
util.cli.execute( side, neighbor = "right", self._active_displays[-1]
"{} --output {} --auto --{}-of {}".format(
toggle_cmd,
widget.name,
"left"
if event.get("button") == core.input.LEFT_MOUSE
else "right",
neighbor.name,
)
)
util.cli.execute(
"{} --output {} --auto --{}-of {}".format(
toggle_cmd, widget.name, side, neighbor,
)
)
self._refresh(event)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4