qmk-firmware/docs/feature_userspace.md

96 lines
4.0 KiB
Markdown
Raw Normal View History

2017-12-09 06:36:32 +01:00
# Userspace: Sharing Code Between Keymaps
If you use more than one keyboard with a similar keymap, you might see the benefit in being able to share code between them. Create your own folder in `users/` named the same as your keymap (ideally your github username, `<name>`) with the following structure:
* `/users/<name>/` (added to the path automatically)
* `readme.md`
* `rules.mk` (included automatically)
* `<name>.h` (optional)
* `<name>.c` (optional)
`<name>.c` will need to be added to the SRC in `rules.mk` like this:
SRC += <name>.c
Additional files may be added in the same way - it's recommended you have one named `<name>`.c/.h though.
All this only happens when you build a keymap named `<name>`, like this:
make planck:<name>
2017-12-09 06:56:58 +01:00
For example,
make planck:jack
Will include the `/users/jack/` folder in the path, along with `/users/jack/rules.mk`.
## Readme
Please include authorship (your name, github username, email), and optionally [a license that's GPL compatible](https://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses).
## Example
2017-11-14 06:52:34 +01:00
For a brief example, checkout `/users/_example/` , or for a more detailed examples check out [`template.h`](https://github.com/qmk/qmk_firmware/blob/master/users/drashna/template.h) and [`template.c`](https://github.com/qmk/qmk_firmware/blob/master/users/drashna/template.c) in `/users/drashna/` .
2017-12-09 06:56:58 +01:00
### Consolidated Macros
2017-11-14 06:52:34 +01:00
2017-12-09 06:56:58 +01:00
If you wanted to consolidate macros and other functions into your userspace for all of your keymaps, you can do that. The issue is that you then cannot call any function defined in your userspace, or it gets complicated. To better handle this, you can call the functions here and create new functions to use in individual keymaps.
2017-11-14 06:52:34 +01:00
2017-12-09 06:49:07 +01:00
First, you'd want to go through all of your `keymap.c` files and replace `process_record_user` with `process_record_keymap` instead. This way, you can still use keyboard specific codes on those boards, and use your custom "global" keycodes as well. You'll also want to replace `SAFE_RANGE` with `NEW_SAFE_RANGE` so that you wont have any overlapping keycodes
2017-11-14 06:52:34 +01:00
Then add `#include <name.h>` to all of your keymap.c files. This allows you to use these new keycodes without having to redefine them in each keymap.
Once you've done that, you'll want to set the keycode definitions that you need to the `<name>.h` file. For instance:
```
#ifndef USERSPACE
#define USERSPACE
#include "quantum.h"
2017-12-09 06:56:58 +01:00
// Define all of
2017-11-14 06:52:34 +01:00
enum custom_keycodes {
KC_MAKE = SAFE_RANGE,
NEW_SAFE_RANGE //use "NEW_SAFE_RANGE" for keymap specific codes
};
#endif
```
Now you want to create the `<name>.c` file, and add this content to it:
```
#include "<name>.h"
#include "quantum.h"
#include "action.h"
#include "version.h"
__attribute__ ((weak))
bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
return true;
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case KC_MAKE:
if (!record->event.pressed) {
2017-11-28 05:53:40 +01:00
SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
#if (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
2017-11-28 05:53:40 +01:00
":dfu "
2017-11-29 18:49:33 +01:00
#elif defined(BOOTLOADER_HALFKAY)
2017-11-28 05:53:40 +01:00
":teensy "
#elif defined(BOOTLOADER_CATERINA)
":avrdude "
#endif
2017-11-28 05:53:40 +01:00
SS_TAP(X_ENTER));
2017-11-14 06:52:34 +01:00
}
return false;
break;
}
return process_record_keymap(keycode, record);
}
```
2017-12-09 06:56:58 +01:00
This will add a new `KC_MAKE` keycode that can be used in any of your keymaps. And this keycode will output `make <keyboard>:<keymap">`, making frequent compiling easier. And this will work with any keyboard and any keymap as it will output the current boards info, so that you don't have to type this out every time.
2017-11-14 06:52:34 +01:00
2017-11-29 18:49:33 +01:00
Additionally, this should flash the newly compiled firmware automatically, using the correct utility, based on the bootloader settings (or default to just generating the HEX file). However, it should be noted that this may not work on all systems. AVRDUDE doesn't work on WSL, namely (and will dump the HEX in the ".build" folder instead).