2020-03-11 21:35:25 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
"""Shows a widget for each connected screen and allows the user to enable/disable screens.
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* xrandr.overwrite_i3config: If set to 'true', this module assembles a new i3 config
|
2020-05-06 12:57:38 +02:00
|
|
|
every time a screen is enabled or disabled by taking the file '~/.config/i3/config.template'
|
|
|
|
and appending a file '~/.config/i3/config.<screen name>' for every screen.
|
2020-03-11 21:35:25 +01:00
|
|
|
* xrandr.autoupdate: If set to 'false', does *not* invoke xrandr automatically. Instead, the
|
2020-05-06 12:57:38 +02:00
|
|
|
module will only refresh when displays are enabled or disabled (defaults to true)
|
2020-07-25 17:47:31 +02:00
|
|
|
* xrandr.exclude: Comma-separated list of display name prefixes to exclude
|
2020-07-26 22:21:49 +02:00
|
|
|
* xrandr.autotoggle: Boolean flag to automatically enable new displays (defaults to false)
|
|
|
|
* xrandr.autotoggle_side: Which side to put autotoggled displays on ('right' or 'left', defaults to 'right')
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
Requires the following python module:
|
|
|
|
* (optional) i3 - if present, the need for updating the widget list is auto-detected
|
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* xrandr
|
2020-05-03 11:15:52 +02:00
|
|
|
"""
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
import core.module
|
|
|
|
import core.input
|
2020-04-02 16:30:31 +02:00
|
|
|
import core.decorators
|
2020-03-13 13:56:08 +01:00
|
|
|
|
2020-05-10 12:52:20 +02:00
|
|
|
from bumblebee_status.discover import utility
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
import util.cli
|
|
|
|
import util.format
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import i3
|
2020-07-25 20:50:13 +02:00
|
|
|
except Exception:
|
2020-03-11 21:35:25 +01:00
|
|
|
pass
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
RESOLUTION_REGEX = re.compile(r"\d+x\d+\+(\d+)\+\d+")
|
|
|
|
|
|
|
|
|
|
|
|
class DisplayInfo:
|
|
|
|
def __init__(self, name, resolution, connected, added, removed):
|
|
|
|
self.name = name
|
|
|
|
self.active = resolution is not None
|
|
|
|
self.connected = connected
|
|
|
|
self.added = added
|
|
|
|
self.removed = removed
|
|
|
|
|
|
|
|
self.position = int(resolution.group(1)) if self.active else sys.maxsize
|
|
|
|
self.state = "on" if self.active else "off"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "DisplayInfo(name={}, active={}, connected={}, added={}, removed={}, position={}, state={})".format(
|
|
|
|
self.name,
|
|
|
|
self.active,
|
|
|
|
self.connected,
|
|
|
|
self.added,
|
|
|
|
self.removed,
|
|
|
|
self.position,
|
|
|
|
self.state,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
class Module(core.module.Module):
|
2020-05-03 11:15:52 +02:00
|
|
|
@core.decorators.every(seconds=5) # takes up to 5s to detect a new screen
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
2020-05-10 12:52:20 +02:00
|
|
|
super().__init__(config, theme, [])
|
2020-03-13 13:56:08 +01:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
self._exclude = tuple(util.format.aslist(self.parameter("exclude")))
|
2020-05-03 11:15:52 +02:00
|
|
|
self._autoupdate = util.format.asbool(self.parameter("autoupdate", True))
|
2020-07-26 22:21:49 +02:00
|
|
|
self._autotoggle = util.format.asbool(self.parameter("autotoggle", False))
|
|
|
|
self._autotoggle_side = self.parameter("autotoggle_side", "right")
|
|
|
|
|
|
|
|
self._connected_displays = []
|
|
|
|
self._active_displays = []
|
|
|
|
self._initialized = False
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
i3.Subscription(self._output_update, "output")
|
2020-07-25 20:33:42 +02:00
|
|
|
except Exception:
|
2020-03-11 21:35:25 +01:00
|
|
|
pass
|
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
def _output_update(self, *_):
|
|
|
|
self.update(force=True)
|
|
|
|
|
|
|
|
def _query_displays(self):
|
|
|
|
displays = []
|
|
|
|
|
|
|
|
for line in util.cli.execute("xrandr -q").split("\n"):
|
|
|
|
# disconnected or connected
|
|
|
|
if "connected" not in line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
name = line.split(" ", 2)[0]
|
|
|
|
resolution = RESOLUTION_REGEX.search(line)
|
|
|
|
active = resolution is not None
|
|
|
|
|
|
|
|
connected = "disconnected" not in line
|
|
|
|
added = connected and not active and name not in self._connected_displays
|
|
|
|
removed = not connected and active and name in self._active_displays
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
displays.append(DisplayInfo(name, resolution, connected, added, removed))
|
|
|
|
|
|
|
|
self._connected_displays = [
|
|
|
|
display.name for display in displays if display.connected
|
|
|
|
]
|
|
|
|
self._active_displays = [display.name for display in displays if display.active]
|
|
|
|
|
|
|
|
return displays
|
|
|
|
|
|
|
|
def update(self, force=False):
|
|
|
|
if not (self._autoupdate or force or not self._initialized):
|
2020-03-11 21:35:25 +01:00
|
|
|
return
|
|
|
|
|
2020-07-25 19:52:10 +02:00
|
|
|
self.clear_widgets()
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
for display in self._query_displays():
|
|
|
|
if display.name.startswith(self._exclude):
|
2020-03-11 21:35:25 +01:00
|
|
|
continue
|
2020-07-25 17:47:31 +02:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
if self._initialized and self._autotoggle:
|
|
|
|
if display.added:
|
|
|
|
self._enable_display(display.name, self._autotoggle_side)
|
|
|
|
elif display.removed:
|
|
|
|
self._disable_display(display.name)
|
2020-07-25 19:52:10 +02:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
if not display.connected:
|
2020-07-25 17:47:31 +02:00
|
|
|
continue
|
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
widget = self.add_widget(full_text=display.name, name=display.name)
|
|
|
|
core.input.register(widget, button=1, cmd=self._toggle)
|
|
|
|
core.input.register(widget, button=3, cmd=self._toggle)
|
|
|
|
|
|
|
|
widget.set("state", display.state)
|
|
|
|
widget.set("pos", display.position)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-25 19:52:10 +02:00
|
|
|
if not self._autoupdate:
|
2020-05-09 10:57:44 +02:00
|
|
|
widget = self.add_widget(full_text="")
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("state", "refresh")
|
2020-07-26 22:21:49 +02:00
|
|
|
core.input.register(widget, button=1, cmd=self.update)
|
|
|
|
|
|
|
|
self._initialized = True
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return widget.get("state", "off")
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
def _toggle_cmd(self):
|
2020-07-25 19:52:10 +02:00
|
|
|
if util.format.asbool(self.parameter("overwrite_i3config", False)):
|
2020-07-26 22:21:49 +02:00
|
|
|
return utility("toggle-display.sh")
|
2020-03-11 21:35:25 +01:00
|
|
|
else:
|
2020-07-26 22:21:49 +02:00
|
|
|
return "xrandr"
|
|
|
|
|
|
|
|
def _disable_display(self, name):
|
|
|
|
if len(self._active_displays) > 1:
|
|
|
|
util.cli.execute("{} --output {} --off".format(self._toggle_cmd(), name))
|
|
|
|
|
|
|
|
def _enable_display(self, name, side=None):
|
|
|
|
# TODO: is there ever a case when there isn't a neighbor?
|
|
|
|
command = "{} --output {} --auto".format(self._toggle_cmd(), name)
|
|
|
|
if side and self._active_displays:
|
|
|
|
neighbor_index = 0 if side == "left" else -1
|
|
|
|
command += " --{}-of {}".format(side, self._active_displays[neighbor_index])
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-26 22:21:49 +02:00
|
|
|
util.cli.execute(command)
|
|
|
|
|
|
|
|
def _toggle(self, event):
|
2020-07-05 20:35:44 +02:00
|
|
|
widget = self.widget(widget_id=event["instance"])
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.get("state") == "on":
|
2020-07-26 22:21:49 +02:00
|
|
|
self._disable_display(widget.name)
|
2020-03-11 21:35:25 +01:00
|
|
|
else:
|
2020-07-26 22:21:49 +02:00
|
|
|
side = "left" if event["button"] == core.input.LEFT_MOUSE else "right"
|
|
|
|
self._enable_display(widget.name, side)
|
|
|
|
|
|
|
|
self.update(force=True)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-07-25 20:33:42 +02:00
|
|
|
|
2020-03-11 21:35:25 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|