Skip to content

Instantly share code, notes, and snippets.

@rodrigob
Created September 9, 2019 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigob/e220614d4be9dc373a2997ef8ec153a7 to your computer and use it in GitHub Desktop.
Save rodrigob/e220614d4be9dc373a2997ef8ec153a7 to your computer and use it in GitHub Desktop.
qmk_firmware/keyboards/pinky/4/keymaps/rodrigob/keymap.c
/* Copyright 2018 'Masayuki Sunahara'
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include QMK_KEYBOARD_H
// Each layer gets a name for readability, which is then used in the keymap matrix below.
// The underscores don't mean anything - you can have a layer called STUFF or any other name.
// Layer names don't all need to be of the same length, obviously, and you can also skip them
// entirely and just use numbers.
#define _QWERTY 0
#define _LOWER 3
#define _RAISE 4
#define _ADJUST 16
// Defines the keycodes used by our macros in process_record_user
enum custom_keycodes {
QWERTY = SAFE_RANGE,
LOWER,
RAISE,
ADJUST,
KC_BSPCDEL,
KC_LPRNABK,
KC_RPRNABK
};
#define ________ KC_TRNS
#define XXXXXXXX KC_NO
#define KC_LOWER LOWER
#define KC_RAISE RAISE
#define KC_ADJ ADJUST
#define KC_CTLTB CTL_T(KC_TAB)
#define KC_LALTSPC MT(MOD_LALT, KC_SPC)
#define KC_LCTLSPC MT(MOD_LCTL, KC_SPC)
#define KC_LSFTENT MT(MOD_LSFT, KC_ENT)
#define KC_RALTSPC MT(MOD_RALT, KC_SPC)
#define KC_RCTLSPC MT(MOD_RCTL, KC_SPC)
#define KC_RSFTSPC MT(MOD_RSFT, KC_SPC)
#define KC_RSFTENT KC_SFTENT
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_QWERTY] = LAYOUT( \
//,--------------------------------------------------------------------------------------. ,-----------------------------------------------------------------------------------------.
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPCDEL,\
//|---------+---------+---------+---------+-----------+-----------+----------------------| |----------------------+------------+-----------+---------+---------+---------+-----------|
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_LBRC, KC_RBRC, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSLS,\
//|---------+---------+---------+---------+-----------+-----------+----------------------| |----------------------+------------+-----------+---------+---------+---------+-----------|
//LCTL(KC_ESC), KC_A, KC_S, KC_D, KC_F, KC_G, LT(_LOWER,KC_LPRNABK), LT(_LOWER,KC_RPRNABK), KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,
KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_LPRNABK, KC_RPRNABK, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,\
//|---------+---------+---------+---------+-----------+-----------+----------------------| |----------------------+------------+-----------+---------+---------+---------+-----------|
KC_LSFTENT, KC_Z, KC_X, KC_C, KC_V, KC_B,MO(_LOWER)/*KC_RSFTENT*/, MO(_LOWER)/*KC_RSFTENT*/, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFTENT,\
//|---------+---------+---------+---------+-----------+-----------+----------------------| |----------------------+------------+-----------+---------+---------+---------+-----------|
KC_LGUI, KC_LALTSPC, KC_SPC, KC_LCTLSPC, KC_RCTLSPC, KC_SPC, KC_RALTSPC, KC_APP\
//`--------------------------------------------------------' `---------------------------------------------------------'
),
[_LOWER] = LAYOUT( \
//,---------------------------------------------------------------------. ,---------------------------------------------------------------------.
________, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_MSTP, KC_MRWD, KC_MFFD, KC_MPLY, KC_VOLD, KC_VOLU, KC_DEL,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
KC_CAPS, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, ________, KC_HOME, KC_PGDN, KC_PGUP, KC_END, KC_INS, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________ \
//`---------------------------------------' `---------------------------------------'
),
[_RAISE] = LAYOUT( \
//,---------------------------------------------------------------------. ,---------------------------------------------------------------------.
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________ \
//`---------------------------------------' `---------------------------------------'
),
[_ADJUST] = LAYOUT( \
//,---------------------------------------------------------------------. ,---------------------------------------------------------------------.
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________, ________,\
//|---------+---------+---------+---------+---------+---------+---------| |---------+---------+---------+---------+---------+---------+---------|
________, ________, ________, ________, ________, ________, ________, ________ \
//`---------------------------------------' `---------------------------------------'
)
};
// Hacky insertion of https://github.com/qmk/qmk_firmware/pull/4795 ---------
/** \brief custom modifier handling
*
* This function checks if the given modifier mask is set and the given layer is
* the current spec. If not it returns true. If the mask is set and the key
* pressed, it deregisters the currently held modifiers, executes the given
* function and returns the value returned by the function. If the key is
* released, it simply runs the function and returns its value.
*
* This function is meant to be used in process_record_user to be able to
* trigger specific action on key code with modifier, for example when shift-esc
* is pressed.
*
* \param modifier_mask modifier mask to check against current modifiers held.
* \param layer layer to check against current layer.
* \param keycode the processed keycode, will be forwarded to handler.
* \param record the processed record, will be forwarded to handler.
* \param data anonymous data to be passed to the handler.
* \param handler to be called when modifiers and layer are matched.
* \return true if modifiers or layer did not match, return value of handler otherwise.
*/
bool run_on_modifiers(uint8_t modifier_mask, int16_t layer, uint16_t keycode, keyrecord_t *record,
void *data, bool (*handler)(uint16_t keycode, keyrecord_t *record, void *data)) {
static bool in_alternate_modifier;
/* nothing to do if we are not in the right layer */
if (layer >= 0 && !layer_state_is(layer))
return true;
/* when it's a key press and modifier state is pressed */
if (record->event.pressed && (get_mods() & modifier_mask) == modifier_mask) {
in_alternate_modifier = true;
/* will send modifier up so that the os won't shift the keycode we will send */
del_mods(modifier_mask);
/* send mods modifications */
send_keyboard_report();
/* execute handler */
bool rc = handler(keycode, record, data);
add_mods(modifier_mask);
return rc;
}
/* when releasing the key and we activated alternate modifier */
if (!record->event.pressed && in_alternate_modifier) {
in_alternate_modifier = false;
/* execute handler */
bool rc = handler(keycode, record, data);
/* make sure all mods we sat up earlier are released */
clear_weak_mods();
/* send mods modification */
send_keyboard_report();
return rc;
}
return true;
}
/** \brief add a weak mod if keycode has the mod enabled */
#define maybe_add_weak_mods(keycode, mod) \
if (keycode < QK_MODS_MAX && \
(keycode & 0xff00) == QK_ ## mod) \
add_weak_mods(MOD_BIT(KC_ ## mod))
/** \brief copy keycode mods as weak mod */
static void copy_mods_as_weak(uint16_t keycode) {
/* send mods if keycode needs it */
maybe_add_weak_mods(keycode, LCTL);
maybe_add_weak_mods(keycode, LSFT);
maybe_add_weak_mods(keycode, LALT);
maybe_add_weak_mods(keycode, LGUI);
maybe_add_weak_mods(keycode, RCTL);
maybe_add_weak_mods(keycode, RSFT);
maybe_add_weak_mods(keycode, RALT);
maybe_add_weak_mods(keycode, RGUI);
}
/** \brief run_on_modifiers handler to send custom keycode
*/
static bool custom_keycode_on_modifiers_handler(uint16_t keycode, keyrecord_t *record, void *data) {
uint16_t custom_keycode = *((uint16_t *) data);
if (record->event.pressed) {
/* register custom keycode mods */
copy_mods_as_weak(custom_keycode);
send_keyboard_report();
/* send alternate key code */
register_code16(custom_keycode);
} else {
/* release the alternate key */
unregister_code16(custom_keycode);
/* make sure all mods we sat up earlier are released */
clear_weak_mods();
/* send mods modification */
send_keyboard_report();
}
return false;
}
/** \brief Send custom keycode when modifier mask is set on layer.
*
*/
bool custom_keycode_on_modifiers(uint8_t modifier_mask, int16_t layer, keyrecord_t *record, uint16_t custom_keycode) {
return run_on_modifiers(modifier_mask, layer, 0, record, &custom_keycode, custom_keycode_on_modifiers_handler);
}
// ---------
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case KC_BSPCDEL: // shift+backspace becomes delete
if (record->event.pressed){
if (get_mods() & MOD_BIT(KC_LSHIFT) || get_mods() & MOD_BIT(KC_RSHIFT)){
register_code(KC_DEL);
} else {
register_code(KC_BSPC);
}
} else {
unregister_code(KC_DEL);
unregister_code(KC_BSPC);
}
return false;
case KC_LPRNABK: // custom key ( and <
if (record->event.pressed){
if (get_mods() & MOD_BIT(KC_LSHIFT) || get_mods() & MOD_BIT(KC_RSHIFT)){
register_code16(KC_LABK);
} else {
register_code16(KC_LPRN);
}
} else {
unregister_code16(KC_LABK);
unregister_code16(KC_LPRN);
}
return false;
case KC_RPRNABK: // custom key ) and >
if (record->event.pressed){
if (get_mods() & MOD_BIT(KC_LSHIFT) || get_mods() & MOD_BIT(KC_RSHIFT)){
register_code16(KC_RABK);
} else {
register_code16(KC_RPRN);
}
} else {
unregister_code16(KC_RABK);
unregister_code16(KC_RPRN);
}
return false;
/*
// Change the SHIFT+ behaviour of some of the keys ---
case KC_BSPC: // shift+backspace becomes delete
return custom_keycode_on_modifiers(MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT), _QWERTY, record, KC_DEL);
//case KC_LPRN: // shift+( becomes <
// return custom_keycode_on_modifiers(MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT), -1, record, KC_LABK);
//case KC_RPRN: // shift+) becomes >
// return custom_keycode_on_modifiers(MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT), -1, record, KC_RABK);
*/
/*
// Below was the original pinky4 example code, leaving as is ---
case KC_QWERTY:
if (record->event.pressed) {
set_single_persistent_default_layer(_QWERTY);
}
return false;
break;
case KC_LOWER:
if (record->event.pressed) {
layer_on(_LOWER);
update_tri_layer(_LOWER, _RAISE, _ADJUST);
} else {
layer_off(_LOWER);
update_tri_layer(_LOWER, _RAISE, _ADJUST);
}
return false;
break;
case KC_RAISE:
if (record->event.pressed) {
layer_on(_RAISE);
update_tri_layer(_LOWER, _RAISE, _ADJUST);
} else {
layer_off(_RAISE);
update_tri_layer(_LOWER, _RAISE, _ADJUST);
}
return false;
break;
case KC_ADJUST:
if (record->event.pressed) {
layer_on(_ADJUST);
} else {
layer_off(_ADJUST);
}
return false;
break;
*/
} // end of "switch keycode"
return true;
}
void matrix_init_user(void) {
}
void matrix_scan_user(void) {
}
void led_set_user(uint8_t usb_led) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment