Skip to content

Commit

Permalink
bastardkb/charybdis: move keycodes definition to keyboard level
Browse files Browse the repository at this point in the history
Move custom keycodes definition and  implementation to the keyboard
level.  Users who wish to disable this and/or reduce binary size can
define `NO_CHARYBDIS_KEYCODES` in their `config.h`.
  • Loading branch information
0xcharly committed Jan 13, 2022
1 parent ce5c9d7 commit 5b65d0d
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 164 deletions.
82 changes: 0 additions & 82 deletions keyboards/bastardkb/charybdis/3x5/keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,6 @@ static uint16_t auto_pointer_layer_timer = 0;
#endif // CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_THRESHOLD
#endif // CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_ENABLE

enum charybdis_keymap_keycodes {
#ifdef VIA_ENABLE
POINTER_DEFAULT_DPI_FORWARD = USER00,
#else
POINTER_DEFAULT_DPI_FORWARD = SAFE_RANGE,
#endif // VIA_ENABLE
POINTER_DEFAULT_DPI_REVERSE,
POINTER_SNIPING_DPI_FORWARD,
POINTER_SNIPING_DPI_REVERSE,
SNIPING_MODE,
SNIPING_MODE_TOGGLE,
DRAGSCROLL_MODE,
DRAGSCROLL_MODE_TOGGLE,
KEYMAP_SAFE_RANGE,
};

#define DPI_MOD POINTER_DEFAULT_DPI_FORWARD
#define DPI_RMOD POINTER_DEFAULT_DPI_REVERSE
#define S_D_MOD POINTER_SNIPING_DPI_FORWARD
#define S_D_RMOD POINTER_SNIPING_DPI_REVERSE
#define SNIPING SNIPING_MODE
#define SNP_TOG SNIPING_MODE_TOGGLE
#define DRGSCRL DRAGSCROLL_MODE
#define DRG_TOG DRAGSCROLL_MODE_TOGGLE

#define ESC_MED LT(LAYER_MEDIA, KC_ESC)
#define SPC_NAV LT(LAYER_NAVIGATION, KC_SPC)
#define TAB_FUN LT(LAYER_FUNCTION, KC_TAB)
Expand Down Expand Up @@ -233,64 +208,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
};
// clang-format on

/** \brief Whether SHIFT mod is enabled. */
static bool has_shift_mod(void) {
#ifdef NO_ACTION_ONESHOT
return mod_config(get_mods()) & MOD_MASK_SHIFT;
#else
return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
#endif // NO_ACTION_ONESHOT
}

#ifdef POINTING_DEVICE_ENABLE
bool process_record_user(uint16_t keycode, keyrecord_t* record) {
switch (keycode) {
case POINTER_DEFAULT_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_DEFAULT_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
}
break;
case SNIPING_MODE:
charybdis_set_pointer_sniping_enabled(record->event.pressed);
break;
case SNIPING_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_sniping_enabled(
!charybdis_get_pointer_sniping_enabled());
}
break;
case DRAGSCROLL_MODE:
charybdis_set_pointer_dragscroll_enabled(record->event.pressed);
break;
case DRAGSCROLL_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_dragscroll_enabled(
!charybdis_get_pointer_dragscroll_enabled());
}
break;
}
return true;
}

#ifdef CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_ENABLE
report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
if (abs(mouse_report.x) > CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_THRESHOLD ||
Expand Down
82 changes: 0 additions & 82 deletions keyboards/bastardkb/charybdis/4x6/keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,6 @@ static uint16_t auto_pointer_layer_timer = 0;
#endif // CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_THRESHOLD
#endif // CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_ENABLE

enum charybdis_keymap_keycodes {
#ifdef VIA_ENABLE
POINTER_DEFAULT_DPI_FORWARD = USER00,
#else
POINTER_DEFAULT_DPI_FORWARD = SAFE_RANGE,
#endif // VIA_ENABLE
POINTER_DEFAULT_DPI_REVERSE,
POINTER_SNIPING_DPI_FORWARD,
POINTER_SNIPING_DPI_REVERSE,
SNIPING_MODE,
SNIPING_MODE_TOGGLE,
DRAGSCROLL_MODE,
DRAGSCROLL_MODE_TOGGLE,
KEYMAP_SAFE_RANGE,
};

#define DPI_MOD POINTER_DEFAULT_DPI_FORWARD
#define DPI_RMOD POINTER_DEFAULT_DPI_REVERSE
#define S_D_MOD POINTER_SNIPING_DPI_FORWARD
#define S_D_RMOD POINTER_SNIPING_DPI_REVERSE
#define SNIPING SNIPING_MODE
#define SNP_TOG SNIPING_MODE_TOGGLE
#define DRGSCRL DRAGSCROLL_MODE
#define DRG_TOG DRAGSCROLL_MODE_TOGGLE

#define LOWER MO(LAYER_LOWER)
#define RAISE MO(LAYER_RAISE)
#define PT_Z LT(LAYER_POINTER, KC_Z)
Expand Down Expand Up @@ -136,64 +111,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
};
// clang-format on

/** /brief Whether SHIFT mod is enabled. */
static bool has_shift_mod(void) {
#ifdef NO_ACTION_ONESHOT
return mod_config(get_mods()) & MOD_MASK_SHIFT;
#else
return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
#endif // NO_ACTION_ONESHOT
}

#ifdef POINTING_DEVICE_ENABLE
bool process_record_user(uint16_t keycode, keyrecord_t* record) {
switch (keycode) {
case POINTER_DEFAULT_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_DEFAULT_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
}
break;
case SNIPING_MODE:
charybdis_set_pointer_sniping_enabled(record->event.pressed);
break;
case SNIPING_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_sniping_enabled(
!charybdis_get_pointer_sniping_enabled());
}
break;
case DRAGSCROLL_MODE:
charybdis_set_pointer_dragscroll_enabled(record->event.pressed);
break;
case DRAGSCROLL_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_dragscroll_enabled(
!charybdis_get_pointer_dragscroll_enabled());
}
break;
}
return true;
}

#ifdef CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_ENABLE
report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
if (abs(mouse_report.x) > CHARYBDIS_AUTO_POINTER_LAYER_TRIGGER_THRESHOLD ||
Expand Down
59 changes: 59 additions & 0 deletions keyboards/bastardkb/charybdis/charybdis.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
return mouse_report;
}

#if defined(POINTING_DEVICE_ENABLE) && !defined(NO_CHARYBDIS_KEYCODES)
/** \brief Whether SHIFT mod is enabled. */
static bool has_shift_mod(void) {
#ifdef NO_ACTION_ONESHOT
return mod_config(get_mods()) & MOD_MASK_SHIFT;
#else
return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
#endif // NO_ACTION_ONESHOT
}
#endif // POINTING_DEVICE_ENABLE && !NO_CHARYBDIS_KEYCODES

bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
#ifdef CONSOLE_ENABLE
dprintf(
Expand All @@ -276,6 +287,53 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
if (!process_record_user(keycode, record)) {
return false;
}
#ifdef POINTING_DEVICE_ENABLE
#ifndef NO_CHARYBDIS_KEYCODES
switch (keycode) {
case POINTER_DEFAULT_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_DEFAULT_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_FORWARD:
if (record->event.pressed) {
// Step backward if shifted, forward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
}
break;
case POINTER_SNIPING_DPI_REVERSE:
if (record->event.pressed) {
// Step forward if shifted, backward otherwise.
charybdis_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
}
break;
case SNIPING_MODE:
charybdis_set_pointer_sniping_enabled(record->event.pressed);
break;
case SNIPING_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_sniping_enabled(
!charybdis_get_pointer_sniping_enabled());
}
break;
case DRAGSCROLL_MODE:
charybdis_set_pointer_dragscroll_enabled(record->event.pressed);
break;
case DRAGSCROLL_MODE_TOGGLE:
if (record->event.pressed) {
charybdis_set_pointer_dragscroll_enabled(
!charybdis_get_pointer_dragscroll_enabled());
}
break;
}
#endif // !NO_CHARYBDIS_KEYCODES
#ifndef MOUSEKEY_ENABLE
// Simulate mouse keys if full support is not enabled (reduces firmware size
// while maintaining support for mouse keys).
Expand All @@ -287,6 +345,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
pointing_device_send();
}
#endif // !MOUSEKEY_ENABLE
#endif // POINTING_DEVICE_ENABLE
return true;
}

Expand Down
27 changes: 27 additions & 0 deletions keyboards/bastardkb/charybdis/charybdis.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@
#ifdef POINTING_DEVICE_ENABLE
#include "quantum.h"

#ifndef NO_CHARYBDIS_KEYCODES
enum charybdis_keycodes {
#ifdef VIA_ENABLE
POINTER_DEFAULT_DPI_FORWARD = USER00,
#else
POINTER_DEFAULT_DPI_FORWARD = SAFE_RANGE,
#endif // VIA_ENABLE
POINTER_DEFAULT_DPI_REVERSE,
POINTER_SNIPING_DPI_FORWARD,
POINTER_SNIPING_DPI_REVERSE,
SNIPING_MODE,
SNIPING_MODE_TOGGLE,
DRAGSCROLL_MODE,
DRAGSCROLL_MODE_TOGGLE,
CHARYBDIS_SAFE_RANGE,
};

#define DPI_MOD POINTER_DEFAULT_DPI_FORWARD
#define DPI_RMOD POINTER_DEFAULT_DPI_REVERSE
#define S_D_MOD POINTER_SNIPING_DPI_FORWARD
#define S_D_RMOD POINTER_SNIPING_DPI_REVERSE
#define SNIPING SNIPING_MODE
#define SNP_TOG SNIPING_MODE_TOGGLE
#define DRGSCRL DRAGSCROLL_MODE
#define DRG_TOG DRAGSCROLL_MODE_TOGGLE
#endif // !NO_CHARYBDIS_KEYCODES

/** \brief Return the current DPI value for the pointer's default mode. */
uint32_t charybdis_get_pointer_default_dpi(void);

Expand Down
52 changes: 52 additions & 0 deletions keyboards/bastardkb/charybdis/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,55 @@ The acceleration factor can be further tune _via_ the
```c
#define CHARYBDIS_POINTER_ACCELERATION_FACTOR 24
```
### Custom keycodes
The Charybdis firmware defines a number of keycodes to leverage its features,
namely:
```
#ifndef NO_CHARYBDIS_KEYCODES
enum charybdis_keycodes {
POINTER_DEFAULT_DPI_FORWARD = SAFE_RANGE,
POINTER_DEFAULT_DPI_REVERSE,
POINTER_SNIPING_DPI_FORWARD,
POINTER_SNIPING_DPI_REVERSE,
SNIPING_MODE,
SNIPING_MODE_TOGGLE,
DRAGSCROLL_MODE,
DRAGSCROLL_MODE_TOGGLE,
CHARYBDIS_SAFE_RANGE,
};

#define DPI_MOD POINTER_DEFAULT_DPI_FORWARD
#define DPI_RMOD POINTER_DEFAULT_DPI_REVERSE
#define S_D_MOD POINTER_SNIPING_DPI_FORWARD
#define S_D_RMOD POINTER_SNIPING_DPI_REVERSE
#define SNIPING SNIPING_MODE
#define SNP_TOG SNIPING_MODE_TOGGLE
#define DRGSCRL DRAGSCROLL_MODE
#define DRG_TOG DRAGSCROLL_MODE_TOGGLE
#endif // !NO_CHARYBDIS_KEYCODES
```

Users extending the keycode set themselves (either in their keymap, or in their
userspace) must start at `CHARYBDIS_SAFE_RANGE` to avoid conflicts, _eg._:

```c
enum userspace_keycodes {
#ifndef NO_CHARYBDIS_KEYCODES
MY_FIRST_KEYCODE = CHARYBDIS_SAFE_RANGE,
#else
MY_FIRST_KEYCODE = SAFE_RANGE,
#endif // !NO_CHARYBDIS_KEYCODES
MY_SECOND_KEYCODE,
};
```

To disable the custom keycodes, and reduce binary size, simply add a definition
in `config.h`:

```c
#define NO_CHARYBDIS_KEYCODES
```

0 comments on commit 5b65d0d

Please sign in to comment.