Skip to content

Commit

Permalink
update config and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zzeneg committed Oct 3, 2024
1 parent 71505db commit 8b9cf0b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 39 deletions.
53 changes: 37 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Application is written in Rust which gives easy access to HID libraries, low-lev

## Supported platforms/providers

| | Windows | Linux | macos |
| ------------ | ------------------ | ------------------------------- |--------------------|
| | Windows | Linux | MacOS |
| ------------ | ------------------ | ------------------------------- | ------------------ |
| Time | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Volume | :heavy_check_mark: | :heavy_check_mark: (PulseAudio) | :heavy_check_mark: |
| Input layout | :heavy_check_mark: | :heavy_check_mark: (X11) | :heavy_check_mark: |
Expand All @@ -27,11 +27,25 @@ All files are available in [latest release](https://github.com/zzeneg/qmk-hid-ho

Default configuration is set to [stront](https://github.com/zzeneg/stront). For other keyboards you need to modify `qmk-hid-host.json`.

- `device` section contains information about keyboard. All values are **decimal**, make sure to convert them from hex using a [converter](https://tools.keycdn.com/hex-converter).
- `devices` section contains a list of keyboards
- `productId` - `pid` from your keyboard's `info.json`
- `usage` and `usagePage` - default values from QMK (`RAW_USAGE_ID` and `RAW_USAGE_PAGE`). No need to modify them unless they were redefined in firmware
- `name` - keyboard's name (optional, visible only in logs)
- `usage` and `usagePage` - optional, override only if `RAW_USAGE_ID` and `RAW_USAGE_PAGE` were redefined in firmware
- `layouts` - list of supported keyboard layouts in two-letter format (app sends layout's index, not name)
- `reconnectDelay` - delay between reconnecting attempts in milliseconds
- `reconnectDelay` - delay between reconnecting attempts in milliseconds (optional, default is 5000)

#### Minimal config

```json
{
"devices": [
{
"productId": "0x0844"
}
],
"layouts": ["en"]
}
```

### Windows

Expand All @@ -58,20 +72,24 @@ When you verified that the application works with your keyboard, you can use `qm
3. Start `qmk-hid-host`, add it to autorun if needed

### MacOS

1. Download `qmk-hid-host`
2. Modify `qmk-hid-host.json`
3. Add your layouts, for example:
```
"layouts": [
"ABC", "Russian"
],
```
if you don't know what layout are installed in you system, run qmk-hid-host with the layouts listed above, change lang and look at terminal output:
```
INFO qmk_hid_host::providers::layout::macos: new layout: 'ABC', layout list: ["ABC", "Russian"]
INFO qmk_hid_host::providers::layout::macos: new layout: 'Russian', layout list: ["ABC", "Russian"]
```
"new layout:" is what you need

```json
"layouts": ["ABC", "Russian"],
```

if you don't know what layout are installed in you system, run qmk-hid-host with the layouts listed above, change lang and look at terminal output:
```
INFO qmk_hid_host::providers::layout::macos: new layout: 'ABC', layout list: ["ABC", "Russian"]
INFO qmk_hid_host::providers::layout::macos: new layout: 'Russian', layout list: ["ABC", "Russian"]
```
"new layout:" is what you need
4. start `qmk-hid-host` from directory where your `qmk-hid-host.json` is located
5. If you `qmk-hid-host` stuck at `Waiting for keyboard...` there are two common mistakes:
1. You're wrong with productId in your config
Expand All @@ -84,6 +102,9 @@ INFO qmk_hid_host::providers::layout::macos: new layout: 'Russian', layout list:
3. If needed, edit `qmk-hid-host.json` in root folder and run again

## Changelog

- 2024-10-03 - add support for multiple devices, restructure config
- 2024-09-15 - add MacOS support
- 2024-02-06 - add Linux support
- 2024-01-21 - remove run as windows service, add silent version instead
- 2024-01-02 - support RUST_LOG, run as windows service
Expand Down
14 changes: 7 additions & 7 deletions dist/qmk-hid-host.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"device": {
"productId": 2116,
"usage": 97,
"usagePage": 65376
},
"layouts": ["en"],
"reconnectDelay": 5000
"devices": [
{
"name": "stront",
"productId": "0x0844"
}
],
"layouts": ["en"]
}
31 changes: 19 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@
pub struct Config {
pub devices: Vec<Device>,
pub layouts: Vec<String>,
pub reconnect_delay: u64,
pub reconnect_delay: Option<u64>,
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Device {
pub name: Option<String>,
#[serde(deserialize_with = "string_to_hex")]
pub product_id: u16,
pub usage: u16,
pub usage_page: u16,
pub usage: Option<u16>,
pub usage_page: Option<u16>,
}

pub fn get_config() -> Config {
let default_config = Config {
devices: vec![Device {
name: None,
product_id: 0x0844,
usage: 0x61,
usage_page: 0xff60,
usage: None,
usage_page: None,
}],
layouts: vec!["en".to_string()],
reconnect_delay: 5000,
reconnect_delay: None,
};

if let Ok(file) = std::fs::read_to_string("./qmk-hid-host.json") {
if let Ok(file_config) = serde_json::from_str::<Config>(&file) {
tracing::info!("Read config from file");
return file_config;
}

tracing::error!("Error while reading config from file");
return serde_json::from_str::<Config>(&file)
.map_err(|e| tracing::error!("Incorrect config file: {}", e))
.unwrap();
}

let file_content = serde_json::to_string_pretty(&default_config).unwrap();
Expand All @@ -42,3 +40,12 @@ pub fn get_config() -> Config {

return default_config;
}

fn string_to_hex<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: serde::Deserializer<'de>,
{
let value: &str = serde::Deserialize::deserialize(deserializer)?;
let hex = value.trim_start_matches("0x");
return u16::from_str_radix(hex, 16).map_err(serde::de::Error::custom);
}
6 changes: 3 additions & 3 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub struct Keyboard {
impl Keyboard {
pub fn new(device: Device, reconnect_delay: u64) -> Self {
return Self {
name: device.name.unwrap_or_else(|| "keyboard".to_string()),
name: device.name.unwrap_or("keyboard".to_string()),
product_id: device.product_id,
usage: device.usage,
usage_page: device.usage_page,
usage: device.usage.unwrap_or(0x61),
usage_page: device.usage_page.unwrap_or(0xff60),
reconnect_delay,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
for device in config.devices {
let data_sender = data_sender.clone();
let is_connected_sender = is_connected_sender.clone();
let reconnect_delay = config.reconnect_delay;
let reconnect_delay = config.reconnect_delay.unwrap_or(5000);
thread::spawn(move || {
let keyboard = Keyboard::new(device, reconnect_delay);
keyboard.connect(data_sender, is_connected_sender);
Expand Down

0 comments on commit 8b9cf0b

Please sign in to comment.