Skip to content

Commit

Permalink
Merge branch 'release/4.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-ueding committed Feb 19, 2017
2 parents cbf1d16 + 7e891c1 commit a06d751
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Changelog
#########

v4.9.0
Released: 2017-02-19 18:03:31 +0100

- Add ``lsusb`` check for docking.

v4.8.1
Released: 2017-01-20 09:26:34 +0100

Expand Down
40 changes: 38 additions & 2 deletions doc/man/thinkpad-dock.1.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. Copyright © 2013-2015 Martin Ueding <dev@martin-ueding.de>
.. Copyright © 2013-2015, 2017 Martin Ueding <dev@martin-ueding.de>
Copyright © 2015 Jim Turner <jturner314@gmail.com>
Licensed under The GNU Public License Version 2 (or later)
Expand Down Expand Up @@ -35,7 +35,7 @@ There will be an udev rule installed that will automatically dock it when set
onto the station and un-dock when you press the eject button. Technically, this
rule calls the ``thinkpad-dock-hook``.

what it does
What it does
------------

When docking, the following things are done:
Expand Down Expand Up @@ -102,6 +102,42 @@ hierarchical, I will denote the options with a dot. The first one would be

Those are the possible options:

``dock.lsusb_indicator_regex``
Some docks might not have a docking indicator in the sysfs. In `Issue 129
<https://github.com/martin-ueding/thinkpad-scripts/issues/129>`_ it has
been discussed to use a particular USB device that is attached only at the
dock to function as an indicator. If this option is set to a non-zero
length string, it will be used as a regular expression. The output of
``lsusb`` is searched for that regular expression. If a match is found, the
laptop is assumed to be on the docking station.

.. admonition:: Example

The output of ``lsusb`` might contain lines like the following::

Bus 002 Device 003: ID 056a:00e6 Wacom Co., Ltd TPCE6
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 04f2:b217 Chicony Electronics Co., Ltd Lenovo Integrated Camera (0.3MP)
Bus 001 Device 006: ID 046d:c05a Logitech, Inc. M90/M100 Optical Mouse
Bus 001 Device 008: ID 273f:1007
Bus 001 Device 005: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 004: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Some of these devices might be integrated in the docking station. One
of the USB hubs is the one in my external screen. That does not help
much because its ID is not unique. The unnamed device with ID
``273f:1007`` is only present on the docking station. Therefore I would
set the configuration value to ``273f:1007``.

At the office, I have a second docking station. There I have some other
device, say ID ``1234:1234``. Since this configuration option is a
regular expression, I could specify the following:
``273f:1007|1234:1234``. Then both devices can trigger the docking
state.

``gui.kdialog``
Please see the appropriate section in thinkpad-rotate(1), it has the same
option. *Default:*.
Expand Down
3 changes: 3 additions & 0 deletions tps/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Copyright © 2015 Jim Turner <jturner314@gmail.com>
# Licensed under The GNU Public License Version 2 (or later)

[dock]
lsusb_indicator_regex =

[gui]
kdialog = true

Expand Down
33 changes: 31 additions & 2 deletions tps/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import glob
import logging
import re
import subprocess
import sys

Expand All @@ -26,15 +27,33 @@
logger = logging.getLogger(__name__)


def is_docked():
def is_docked(config):
'''
Determines whether the laptop is on a docking station.
This checks for ``/sys/devices/platform/dock.*/docked``.
In issue 129__ it
became apparent that this is not a sufficient solution. Therefore a
configuration option allows to alternatively check for USB devices that are
present.
__ https://github.com/martin-ueding/thinkpad-scripts/issues/129
:returns: True if laptop is docked
:rtype: bool
'''
regex = config['dock']['lsusb_indicator_regex']
if len(regex) > 0:
logger.debug('Using lsusb to determine docking status.')
return _is_docked_lsusb(regex)
else:
logger.debug('Using sysfs to determine docking status.')
return _is_docked_sys_platform()

def _is_docked_sys_platform():
'''
Determines the docking status from ``/sys/devices/platform/dock.*/docked``.
'''
dockfiles = glob.glob('/sys/devices/platform/dock.*/docked')
for dockfile in dockfiles:
with open(dockfile) as handle:
Expand All @@ -47,6 +66,16 @@ def is_docked():
return False


def _is_docked_lsusb(regex):
'''
Queries ``lsusb`` and checks whether the devices are present.
'''
output = tps.check_output(['lsusb'], logger).decode().strip()
match = re.search(regex, output)
return bool(match)



def select_docking_screens(internal, primary='', secondary=''):
'''
Selects the primary, secondary, and remaining screens when docking.
Expand Down Expand Up @@ -228,7 +257,7 @@ def main():
elif options.state == 'off':
desired = False
elif options.state is None:
desired = is_docked()
desired = is_docked(config)
else:
logging.error('Desired state “%s” cannot be understood.',
options.state)
Expand Down

0 comments on commit a06d751

Please sign in to comment.