Skip to content

Commit

Permalink
Add map-to-output --list-monitors
Browse files Browse the repository at this point in the history
GNOME47 no longer lists the connector names though that one is likely to
be most commonly used to map to an output since it's the only constant
one (who knows their monitors product name or serial after all...)

Let's add a --list-monitors option to list the current ones, and list
them on failure too while we're there.
  • Loading branch information
whot committed Oct 28, 2024
1 parent 63376f2 commit 7d34fa6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ $ gsetwacom tablet "056A:0357" set-left-handed true
$ gsetwacom tablet "056A:0357" set-button-action A keybinding "<Control><Alt>t"
$ gsetwacom tablet "056A:0357" set-ring-action --direction=cw --mode=2 keybinding "x"
$ gsetwacom tablet "056A:0357" map-to-monitor --list-monitors
- on DP-1: 'GSM' 'LG HDR 4K' with serial no '12345'
- on HDMI-2: 'GSM' 'LG HDR 4K' with serial no '67890'
$ gsetwacom tablet "056A:0357" map-to-monitor --connector DP-1
```
And for stylus configuration:
Expand Down
49 changes: 27 additions & 22 deletions src/gsetwacom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,10 @@ def wrapper(*args, **kwargs):
@click.option("--product", type=str, default=None)
@click.option("--serial", type=str, default=None)
@click.option("--connector", type=str, default=None)
@click.option("--list-monitors", is_flag=True, default=False, help="List all currently connected monitors")
@click.pass_context
async def tablet_map_to_monitor(
ctx,
vendor: str | None,
product: str | None,
serial: str | None,
connector: str | None,
ctx, vendor: str | None, product: str | None, serial: str | None, connector: str | None, list_monitors: bool
):
"""
Map the tablet to a given monitor. The monitor may be specified with one or more
Expand All @@ -297,7 +294,7 @@ async def tablet_map_to_monitor(
"product": product,
"serial": serial,
}
if all(args[key] is None for key in args):
if not list_monitors and all(args[key] is None for key in args):
msg = "One of --vendor, --product, --serial or --connector has to be provided"
raise click.UsageError(msg)

Expand All @@ -320,24 +317,32 @@ class Monitor:
product: str
serial: str

monitors = (Monitor(*mdata) for (mdata, *_) in monitors)
monitors = [Monitor(*mdata) for (mdata, *_) in monitors]

for monitor in monitors:
logger.info("Monitor on %s vendor '%s' product '%s' serial '%s'", *asdict(monitor).values())
if any(args[key] is not None and args[key] != getattr(monitor, key) for key in args):
continue
settings = ctx.obj
settings.set_value(
"output",
GLib.Variant(
"as",
[monitor.vendor, monitor.product, monitor.serial, monitor.connector],
),
)
break
def print_list():
msg = [f"- on {m.connector}: '{m.vendor}' '{m.product}' with serial no '{m.serial}'" for m in monitors]
return "\n".join(msg)

if list_monitors:
click.echo(print_list())
else:
msg = "Unable to find this monitor in the current configuration"
raise click.UsageError(msg)
for monitor in monitors:
logger.info("Monitor on %s vendor '%s' product '%s' serial '%s'", *asdict(monitor).values())
if any(args[key] is not None and args[key] != getattr(monitor, key) for key in args):
continue
settings = ctx.obj
settings.set_value(
"output",
GLib.Variant(
"as",
[monitor.vendor, monitor.product, monitor.serial, monitor.connector],
),
)
break
else:
msg = f"Unable to find this monitor in the current configuration. Available monitors:\n{print_list()}"

raise click.UsageError(msg)


def change_action(path: str, action: str, keybinding: str | None):
Expand Down

0 comments on commit 7d34fa6

Please sign in to comment.