Skip to content

Commit

Permalink
Allow configuration based on device name
Browse files Browse the repository at this point in the history
Configuration files can now be named after Bluetooth device names.
Files named after Bluetooth addresses take precedence if both are
present.
  • Loading branch information
tsowell committed Mar 5, 2022
1 parent 26e344c commit 6e0dd3b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ device.

You need two pieces of information about a Bluetooth device to configure it:

1. The 48-bit Bluetooth address (often represented like `11:22:33:44:FF:EE`).
You can find this in bluetoothctl or the `Uniq` field in
`/proc/bus/input/devices`, among other places.
1. The Bluetooth device name which you can find in the Kobo Bluetooth settings
menu.

2. The [Linux Input Subsystem event codes][0] that represent the input events
you want to use to turn pages. You can find these using the [evtest][1]
tool, for example.

Once you have those, create a file under `.btpt` named after the Bluetooth
address without semicolons (`11223344FFEE` for example). The file name is case
insensitive, but behavior is undefined if there are collisions.
Once you have those, create a configuration file under `.btpt`. The filename
should be the exact Bluetooth device name.

The file should have a line for each input mapping in the format:

Expand All @@ -56,9 +54,9 @@ orientation, so I want Down (65535 on the Y-axis) and the X button to go to the
previous page, and Up (0 on the Y-axis) and the B button to go to the next
page.

The physical address is `E4:17:D8:78:F4:FC` (for the purposes of
demonstration), so the configuration file is `.btpt/e417d878f4fc` with the
following contents:
In XInput mode, the device identifies itself as "8BitDo Zero 2 gamepad", so
the configuration file is `.btpt/8BitDo Zero 2 gamepad` with the following
contents:

```
prevPage EV_ABS ABS_Y 65535
Expand All @@ -67,6 +65,18 @@ prevPage EV_KEY BTN_NORTH 0
nextPage EV_KEY BTN_SOUTH 0
```

This file is also present in the `examples` directory of this repository.

#### Advanced configuration

You can also name configuration files after a device's 48-bit Bluetooth address
(often represented like `11:22:33:44:FF:EE`). When present, these files take
precedence over files named after the device name, so you can override default
behavior for specific devices.

The addresses are case insensitive, but behavior is undefined if there are
collisions.

## Building from source

`make` builds the shared library, `libbtpt.so`, which can be installed in
Expand Down
File renamed without changes.
18 changes: 14 additions & 4 deletions src/btpt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ static void invokeMainWindowController(const char *method)
}
}

bool BluetoothPageTurner::addDevice(const QString &uniq, const QString &handler)
bool BluetoothPageTurner::addDevice(
const QString &name, const QString &uniq, const QString &handler)
{
int fd;

if (devices.contains(uniq)) {
return false;
}

QFile cfg(BTPT_DIR + uniq);
/* Fall back to device name */
QFile cfg(BTPT_DIR + name);

/* Look for case-insensitive Bluetooth address in BTPT_DIR */
QStringList list = QDir(BTPT_DIR).entryList();
Expand All @@ -83,7 +85,7 @@ bool BluetoothPageTurner::addDevice(const QString &uniq, const QString &handler)

if (!cfg.open(QIODevice::ReadOnly | QIODevice::Text)) {
nh_log("unable to open %s%s",
BTPT_DIR, uniq.toStdString().c_str());
BTPT_DIR, cfg.fileName().toStdString().c_str());
return false;
}

Expand Down Expand Up @@ -191,6 +193,7 @@ bool BluetoothPageTurner::scanDevices()
QTextStream in(&text);
QString i;
QString u;
QString n;
while (!in.atEnd()) {
QString line = in.readLine();
QString type = line.section(": ", 0, 0);
Expand All @@ -202,6 +205,12 @@ bool BluetoothPageTurner::scanDevices()
u = line.section(": Uniq=", 1);
u.remove(':');
}
else if (type == "N") {
/* Bluetooth name without surrounding double-quotes */
n = line.section(": Name=", 1);
n.chop(1);
n.remove(0, 1);
}
else if (type == "H") {
/* Skip if not Bluetooth device */
if (!i.startsWith("Bus=0005 ")) {
Expand All @@ -215,7 +224,8 @@ bool BluetoothPageTurner::scanDevices()
line.section("Handlers=", 1).split(" ");
foreach(const QString &handler, handlers) {
if (handler.startsWith("event")) {
devicesAdded |= addDevice(u, handler);
devicesAdded |=
addDevice(n, u, handler);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/btpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class BluetoothPageTurner : public QThread
void run() override;

private:
bool addDevice(const QString &i, const QString &handler);
bool addDevice(
const QString &name,
const QString &uniq,
const QString &handler);
bool scanDevices();
void learn(Device &device);
QFileSystemWatcher watcher;
Expand Down

0 comments on commit 6e0dd3b

Please sign in to comment.