2020-03-11 21:35:25 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-03-11 21:35:45 +01: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-03-11 21:35:45 +01: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
|
|
|
|
module will only refresh when displays are enabled or disabled (defaults to true)
|
|
|
|
|
|
|
|
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-03-11 21:35:45 +01:00
|
|
|
'''
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
import util.cli
|
|
|
|
import util.format
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import i3
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
class Module(core.module.Module):
|
2020-03-29 14:35:20 +02:00
|
|
|
def __init__(self, config):
|
2020-03-11 21:35:25 +01:00
|
|
|
widgets = []
|
2020-03-13 13:56:08 +01:00
|
|
|
super().__init__(config, widgets)
|
|
|
|
|
|
|
|
self._autoupdate = util.format.asbool(self.parameter('autoupdate', True))
|
2020-03-11 21:35:25 +01:00
|
|
|
self._needs_update = True
|
|
|
|
|
|
|
|
try:
|
2020-03-11 21:35:45 +01:00
|
|
|
i3.Subscription(self._output_update, 'output')
|
2020-03-11 21:35:25 +01:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _output_update(self, event, data, _):
|
|
|
|
self._needs_update = True
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
def update(self):
|
2020-03-11 21:35:25 +01:00
|
|
|
new_widgets = []
|
|
|
|
|
|
|
|
if self._autoupdate == False and self._needs_update == False:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._needs_update = False
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
for line in util.cli.execute('xrandr -q').split('\n'):
|
2020-03-11 21:35:45 +01:00
|
|
|
if not ' connected' in line:
|
2020-03-11 21:35:25 +01:00
|
|
|
continue
|
2020-03-11 21:35:45 +01:00
|
|
|
display = line.split(' ', 2)[0]
|
2020-03-11 21:35:25 +01:00
|
|
|
m = re.search(r'\d+x\d+\+(\d+)\+\d+', line)
|
|
|
|
|
|
|
|
widget = self.widget(display)
|
|
|
|
if not widget:
|
2020-03-13 13:56:08 +01:00
|
|
|
widget = core.widget.Widget(full_text=display, name=display)
|
|
|
|
core.input.register(widget, button=1, cmd=self._toggle)
|
|
|
|
core.input.register(widget, button=3, cmd=self._toggle)
|
2020-03-11 21:35:25 +01:00
|
|
|
new_widgets.append(widget)
|
2020-03-13 13:56:08 +01:00
|
|
|
widget.module(self)
|
2020-03-11 21:35:45 +01:00
|
|
|
widget.set('state', 'on' if m else 'off')
|
|
|
|
widget.set('pos', int(m.group(1)) if m else sys.maxsize)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
self.widgets(new_widgets)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
if self._autoupdate == False:
|
2020-03-13 13:56:08 +01:00
|
|
|
widget = core.widget.Widget(full_text='')
|
|
|
|
widget.module(self)
|
2020-03-11 21:35:45 +01:00
|
|
|
widget.set('state', 'refresh')
|
2020-03-13 13:56:08 +01:00
|
|
|
core.input.register(widget, button=1, cmd=self._refresh)
|
|
|
|
self.widgets().append(widget)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2020-03-11 21:35:45 +01:00
|
|
|
return widget.get('state', 'off')
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
def _refresh(self, event):
|
|
|
|
self._needs_update = True
|
|
|
|
|
|
|
|
def _toggle(self, event):
|
2020-03-13 13:56:08 +01:00
|
|
|
self._refresh(self, event)
|
2020-03-11 21:35:25 +01:00
|
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
if util.format.asbool(self.parameter('overwrite_i3config', False)) == True:
|
2020-03-11 21:35:45 +01:00
|
|
|
toggle_cmd = '{}/../../bin/toggle-display.sh'.format(path)
|
2020-03-11 21:35:25 +01:00
|
|
|
else:
|
2020-03-11 21:35:45 +01:00
|
|
|
toggle_cmd = 'xrandr'
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-03-11 21:35:45 +01:00
|
|
|
widget = self.widget_by_id(event['instance'])
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-03-11 21:35:45 +01:00
|
|
|
if widget.get('state') == 'on':
|
2020-03-13 13:56:08 +01:00
|
|
|
util.cli.execute('{} --output {} --off'.format(toggle_cmd, widget.name))
|
2020-03-11 21:35:25 +01:00
|
|
|
else:
|
2020-03-11 21:35:45 +01:00
|
|
|
first_neighbor = next((widget for widget in self.widgets() if widget.get('state') == 'on'), None)
|
|
|
|
last_neighbor = next((widget for widget in reversed(self.widgets()) if widget.get('state') == 'on'), None)
|
2020-03-11 21:35:25 +01:00
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
neighbor = first_neighbor if event['button'] == core.input.LEFT_MOUSE else last_neighbor
|
2020-03-11 21:35:25 +01:00
|
|
|
|
|
|
|
if neighbor is None:
|
2020-03-13 13:56:08 +01:00
|
|
|
util.cli.execute('{} --output {} --auto'.format(toggle_cmd, widget.name))
|
2020-03-11 21:35:25 +01:00
|
|
|
else:
|
2020-03-13 13:56:08 +01:00
|
|
|
util.cli.execute('{} --output {} --auto --{}-of {}'.format(toggle_cmd, widget.name,
|
|
|
|
'left' if event.get('button') == core.input.LEFT_MOUSE else 'right',
|
2020-03-11 21:35:25 +01:00
|
|
|
neighbor.name))
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|