diff options
Diffstat (limited to 'quantum')
40 files changed, 487 insertions, 198 deletions
| diff --git a/quantum/action.c b/quantum/action.c index a45e70c557..6368f7398c 100644 --- a/quantum/action.c +++ b/quantum/action.c @@ -528,6 +528,13 @@ void process_action(keyrecord_t *record, action_t action) {                              unregister_code(action.key.code);                          } else {                              ac_dprintf("MODS_TAP: No tap: add_mods\n"); +#    if defined(RETRO_TAPPING) && defined(DUMMY_MOD_NEUTRALIZER_KEYCODE) +                            // Send a dummy keycode to neutralize flashing modifiers +                            // if the key was held and then released with no interruptions. +                            if (retro_tapping_counter == 2) { +                                neutralize_flashing_modifiers(get_mods()); +                            } +#    endif                              unregister_mods(mods);                          }                      } @@ -882,7 +889,7 @@ __attribute__((weak)) void register_code(uint8_t code) {      } else if (KC_LOCKING_CAPS_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE          // Resync: ignore if caps lock already is on -        if (host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK)) return; +        if (host_keyboard_led_state().caps_lock) return;  #    endif          add_key(KC_CAPS_LOCK);          send_keyboard_report(); @@ -892,7 +899,7 @@ __attribute__((weak)) void register_code(uint8_t code) {      } else if (KC_LOCKING_NUM_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE -        if (host_keyboard_leds() & (1 << USB_LED_NUM_LOCK)) return; +        if (host_keyboard_led_state().num_lock) return;  #    endif          add_key(KC_NUM_LOCK);          send_keyboard_report(); @@ -902,7 +909,7 @@ __attribute__((weak)) void register_code(uint8_t code) {      } else if (KC_LOCKING_SCROLL_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE -        if (host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK)) return; +        if (host_keyboard_led_state().scroll_lock) return;  #    endif          add_key(KC_SCROLL_LOCK);          send_keyboard_report(); @@ -952,7 +959,7 @@ __attribute__((weak)) void unregister_code(uint8_t code) {      } else if (KC_LOCKING_CAPS_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE          // Resync: ignore if caps lock already is off -        if (!(host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK))) return; +        if (!host_keyboard_led_state().caps_lock) return;  #    endif          add_key(KC_CAPS_LOCK);          send_keyboard_report(); @@ -961,7 +968,7 @@ __attribute__((weak)) void unregister_code(uint8_t code) {      } else if (KC_LOCKING_NUM_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE -        if (!(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK))) return; +        if (!host_keyboard_led_state().num_lock) return;  #    endif          add_key(KC_NUM_LOCK);          send_keyboard_report(); @@ -970,7 +977,7 @@ __attribute__((weak)) void unregister_code(uint8_t code) {      } else if (KC_LOCKING_SCROLL_LOCK == code) {  #    ifdef LOCKING_RESYNC_ENABLE -        if (!(host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK))) return; +        if (!host_keyboard_led_state().scroll_lock) return;  #    endif          add_key(KC_SCROLL_LOCK);          send_keyboard_report(); diff --git a/quantum/action_util.c b/quantum/action_util.c index 361f410d2d..909dea0595 100644 --- a/quantum/action_util.c +++ b/quantum/action_util.c @@ -500,3 +500,28 @@ __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) {  uint8_t has_anymod(void) {      return bitpop(real_mods);  } + +#ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE +/** \brief Send a dummy keycode in between the register and unregister event of a modifier key, to neutralize the "flashing modifiers" phenomenon. + * + * \param active_mods 8-bit packed bit-array describing the currently active modifiers (in the format GASCGASC). + * + * Certain QMK features like  key overrides or retro tap must unregister a previously + * registered modifier before sending another keycode but this can trigger undesired + * keyboard shortcuts if the clean tap of a single modifier key is bound to an action + * on the host OS, as is for example the case for the left GUI key on Windows, which + * opens the Start Menu when tapped. + */ +void neutralize_flashing_modifiers(uint8_t active_mods) { +    // In most scenarios, the flashing modifiers phenomenon is a problem +    // only for a subset of modifier masks. +    const static uint8_t mods_to_neutralize[] = MODS_TO_NEUTRALIZE; +    const static uint8_t n_mods               = ARRAY_SIZE(mods_to_neutralize); +    for (uint8_t i = 0; i < n_mods; ++i) { +        if (active_mods == mods_to_neutralize[i]) { +            tap_code(DUMMY_MOD_NEUTRALIZER_KEYCODE); +            break; +        } +    } +} +#endif diff --git a/quantum/action_util.h b/quantum/action_util.h index 02f6e9e6df..831caf3c0a 100644 --- a/quantum/action_util.h +++ b/quantum/action_util.h @@ -102,6 +102,19 @@ void use_oneshot_swaphands(void);  void clear_oneshot_swaphands(void);  #endif +#ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE +// KC_A is used as the lowerbound instead of QK_BASIC because the range QK_BASIC...KC_A includes +// internal keycodes like KC_NO and KC_TRANSPARENT which are unsuitable for use with `tap_code(kc)`. +#    if !(KC_A <= DUMMY_MOD_NEUTRALIZER_KEYCODE && DUMMY_MOD_NEUTRALIZER_KEYCODE <= QK_BASIC_MAX) +#        error "DUMMY_MOD_NEUTRALIZER_KEYCODE must be a basic, unmodified, HID keycode!" +#    endif +void neutralize_flashing_modifiers(uint8_t active_mods); +#endif +#ifndef MODS_TO_NEUTRALIZE +#    define MODS_TO_NEUTRALIZE \ +        { MOD_BIT(KC_LEFT_ALT), MOD_BIT(KC_LEFT_GUI) } +#endif +  #ifdef __cplusplus  }  #endif diff --git a/quantum/audio/audio.c b/quantum/audio/audio.c index 2570ad9cd1..0300483a93 100644 --- a/quantum/audio/audio.c +++ b/quantum/audio/audio.c @@ -547,20 +547,39 @@ void audio_decrease_tempo(uint8_t tempo_change) {          note_tempo -= tempo_change;  } -// TODO in the int-math version are some bugs; songs sometimes abruptly end - maybe an issue with the timer/system-tick wrapping around? +/** + * Converts from units of 1/64ths of a beat to milliseconds. + * + * Round-off error is at most 1 millisecond. + * + * Conversion will never overflow for duration_bpm <= 699, provided that + * note_tempo is at least 10. This is quite a long duration, over ten beats. + * + * Beware that for duration_bpm > 699, the result may overflow uint16_t range + * when duration_bpm is large compared to note_tempo: + * + *    duration_bpm * 60 * 1000 / (64 * note_tempo) > UINT16_MAX + * + *    duration_bpm > (2 * 65535 / 1875) * note_tempo + *                 = 69.904 * note_tempo. + */  uint16_t audio_duration_to_ms(uint16_t duration_bpm) { -#if defined(__AVR__) -    // doing int-math saves us some bytes in the overall firmware size, but the intermediate result is less accurate before being cast to/returned as uint -    return ((uint32_t)duration_bpm * 60 * 1000) / (64 * note_tempo); -    // NOTE: beware of uint16_t overflows when note_tempo is low and/or the duration is long -#else -    return ((float)duration_bpm * 60) / (64 * note_tempo) * 1000; -#endif +    return ((uint32_t)duration_bpm * 1875) / ((uint_fast16_t)note_tempo * 2);  } + +/** + * Converts from units of milliseconds to 1/64ths of a beat. + * + * Round-off error is at most 1/64th of a beat. + * + * This conversion never overflows: since duration_ms <= UINT16_MAX = 65535 + * and note_tempo <= 255, the result is always in uint16_t range: + * + *     duration_ms * 64 * note_tempo / 60 / 1000 + *     <= 65535 * 2 * 255 / 1875 + *      = 17825.52 + *     <= UINT16_MAX. + */  uint16_t audio_ms_to_duration(uint16_t duration_ms) { -#if defined(__AVR__) -    return ((uint32_t)duration_ms * 64 * note_tempo) / 60 / 1000; -#else -    return ((float)duration_ms * 64 * note_tempo) / 60 / 1000; -#endif +    return ((uint32_t)duration_ms * 2 * note_tempo) / 1875;  } diff --git a/quantum/debounce/asym_eager_defer_pk.c b/quantum/debounce/asym_eager_defer_pk.c index 4745c6f465..0f7640a80c 100644 --- a/quantum/debounce/asym_eager_defer_pk.c +++ b/quantum/debounce/asym_eager_defer_pk.c @@ -144,6 +144,8 @@ static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[],  static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {      debounce_counter_t *debounce_pointer = debounce_counters; +    matrix_need_update = false; +      for (uint8_t row = 0; row < num_rows; row++) {          matrix_row_t delta = raw[row] ^ cooked[row];          for (uint8_t col = 0; col < MATRIX_COLS; col++) { diff --git a/quantum/debounce/sym_eager_pk.c b/quantum/debounce/sym_eager_pk.c index f736d1645c..15360441de 100644 --- a/quantum/debounce/sym_eager_pk.c +++ b/quantum/debounce/sym_eager_pk.c @@ -125,6 +125,7 @@ static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {  // upload from raw_matrix to final matrix;  static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { +    matrix_need_update                   = false;      debounce_counter_t *debounce_pointer = debounce_counters;      for (uint8_t row = 0; row < num_rows; row++) {          matrix_row_t delta        = raw[row] ^ cooked[row]; diff --git a/quantum/debounce/sym_eager_pr.c b/quantum/debounce/sym_eager_pr.c index aad5ca351b..84f897d674 100644 --- a/quantum/debounce/sym_eager_pr.c +++ b/quantum/debounce/sym_eager_pr.c @@ -119,6 +119,7 @@ static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {  // upload from raw_matrix to final matrix;  static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { +    matrix_need_update                   = false;      debounce_counter_t *debounce_pointer = debounce_counters;      for (uint8_t row = 0; row < num_rows; row++) {          matrix_row_t existing_row = cooked[row]; diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 90a0f20838..2030c82a1b 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -152,22 +152,13 @@ void dynamic_keymap_reset(void) {      for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {          for (int row = 0; row < MATRIX_ROWS; row++) {              for (int column = 0; column < MATRIX_COLS; column++) { -                if (layer < keymap_layer_count()) { -                    dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column)); -                } else { -                    dynamic_keymap_set_keycode(layer, row, column, KC_TRANSPARENT); -                } +                dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column));              }          }  #ifdef ENCODER_MAP_ENABLE          for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) { -            if (layer < encodermap_layer_count()) { -                dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true)); -                dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false)); -            } else { -                dynamic_keymap_set_encoder(layer, encoder, true, KC_TRANSPARENT); -                dynamic_keymap_set_encoder(layer, encoder, false, KC_TRANSPARENT); -            } +            dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true)); +            dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false));          }  #endif // ENCODER_MAP_ENABLE      } diff --git a/quantum/keymap.h b/quantum/keymap.h deleted file mode 100644 index a067e1aa36..0000000000 --- a/quantum/keymap.h +++ /dev/null @@ -1,20 +0,0 @@ -/* -Copyright 2012-2016 Jun Wako <wakojun@gmail.com> - -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/>. -*/ - -#pragma once - -#pragma message("'keymap.h' should no longer be included!") diff --git a/quantum/led.c b/quantum/led.c index 42144566fd..8d86374a6f 100644 --- a/quantum/led.c +++ b/quantum/led.c @@ -153,14 +153,14 @@ __attribute__((weak)) void led_set(uint8_t usb_led) {  /** \brief Trigger behaviour on transition to suspend   */  void led_suspend(void) { -    uint8_t leds_off = 0; +    led_t leds_off = {0};  #ifdef BACKLIGHT_CAPS_LOCK      if (is_backlight_enabled()) {          // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off -        leds_off |= (1 << USB_LED_CAPS_LOCK); +        leds_off.caps_lock = true;      }  #endif -    led_set(leds_off); +    led_set(leds_off.raw);  }  /** \brief Trigger behaviour on transition from suspend diff --git a/quantum/led.h b/quantum/led.h index b9ad7ed9ae..b9fad670ae 100644 --- a/quantum/led.h +++ b/quantum/led.h @@ -22,13 +22,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  /* FIXME: Add doxygen comments here. */ -/* keyboard LEDs */ -#define USB_LED_NUM_LOCK 0 -#define USB_LED_CAPS_LOCK 1 -#define USB_LED_SCROLL_LOCK 2 -#define USB_LED_COMPOSE 3 -#define USB_LED_KANA 4 -  #ifdef __cplusplus  extern "C" {  #endif diff --git a/quantum/led_matrix/led_matrix.c b/quantum/led_matrix/led_matrix.c index 828d61641a..4b692d1904 100644 --- a/quantum/led_matrix/led_matrix.c +++ b/quantum/led_matrix/led_matrix.c @@ -366,7 +366,10 @@ void led_matrix_task(void) {          case RENDERING:              led_task_render(effect);              if (effect) { -                led_matrix_indicators(); +                // Only run the basic indicators in the last render iteration (default there are 5 iterations) +                if (led_effect_params.iter == LED_MATRIX_LED_PROCESS_MAX_ITERATIONS) { +                    led_matrix_indicators(); +                }                  led_matrix_indicators_advanced(&led_effect_params);              }              break; diff --git a/quantum/led_matrix/led_matrix.h b/quantum/led_matrix/led_matrix.h index c7d360f366..9091f6890b 100644 --- a/quantum/led_matrix/led_matrix.h +++ b/quantum/led_matrix/led_matrix.h @@ -42,8 +42,9 @@  #endif  #ifndef LED_MATRIX_LED_PROCESS_LIMIT -#    define LED_MATRIX_LED_PROCESS_LIMIT (LED_MATRIX_LED_COUNT + 4) / 5 +#    define LED_MATRIX_LED_PROCESS_LIMIT ((LED_MATRIX_LED_COUNT + 4) / 5)  #endif +#define LED_MATRIX_LED_PROCESS_MAX_ITERATIONS ((LED_MATRIX_LED_COUNT + LED_MATRIX_LED_PROCESS_LIMIT - 1) / LED_MATRIX_LED_PROCESS_LIMIT)  #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT  #    if defined(LED_MATRIX_SPLIT) diff --git a/quantum/mousekey.c b/quantum/mousekey.c index df8aa613be..c982a2f40b 100644 --- a/quantum/mousekey.c +++ b/quantum/mousekey.c @@ -25,11 +25,16 @@  #include "mousekey.h"  static inline int8_t times_inv_sqrt2(int8_t x) { -    // 181/256 is pretty close to 1/sqrt(2) -    // 0.70703125                 0.707106781 -    // 1 too small for x=99 and x=198 -    // This ends up being a mult and discard lower 8 bits -    return (x * 181) >> 8; +    // 181/256 (0.70703125) is used as an approximation for 1/sqrt(2) +    // because it is close to the exact value which is 0.707106781 +    const int16_t  n = x * 181; +    const uint16_t d = 256; + +    // To ensure that the integer result is rounded accurately after +    // division, check the sign of the numerator: +    // If negative, subtract half of the denominator before dividing +    // Otherwise, add half of the denominator before dividing +    return n < 0 ? (n - d / 2) / d : (n + d / 2) / d;  }  static report_mouse_t mouse_report = {0}; @@ -74,7 +79,7 @@ uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;  uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;  /* milliseconds between repeated motion events (0-255) */  #    ifdef MK_KINETIC_SPEED -float mk_wheel_interval = 1000.0f / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS; +uint16_t mk_wheel_interval = 1000U / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;  #    else  uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;  #    endif @@ -175,7 +180,7 @@ static uint8_t wheel_unit(void) {  /*   * Kinetic movement  acceleration algorithm   * - *  current speed = I + A * T/50 + A * 0.5 * T^2 | maximum B + *  current speed = I + A * T/50 + A * (T/50)^2 * 1/2 | maximum B   *   * T: time since the mouse movement started   * E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the @@ -190,39 +195,48 @@ const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED;  const uint16_t mk_initial_speed     = MOUSEKEY_INITIAL_SPEED;  static uint8_t move_unit(void) { -    float speed = mk_initial_speed; +    uint16_t speed = mk_initial_speed; -    if (mousekey_accel & ((1 << 0) | (1 << 2))) { -        speed = mousekey_accel & (1 << 2) ? mk_accelerated_speed : mk_decelerated_speed; +    if (mousekey_accel & (1 << 0)) { +        speed = mk_decelerated_speed; +    } else if (mousekey_accel & (1 << 2)) { +        speed = mk_accelerated_speed;      } else if (mousekey_repeat && mouse_timer) { -        const float time_elapsed = timer_elapsed(mouse_timer) / 50; -        speed                    = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + MOUSEKEY_MOVE_DELTA * 0.5 * time_elapsed * time_elapsed; - -        speed = speed > mk_base_speed ? mk_base_speed : speed; +        const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50; +        speed                       = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + (MOUSEKEY_MOVE_DELTA * time_elapsed * time_elapsed) / 2; +        if (speed > mk_base_speed) { +            speed = mk_base_speed; +        }      } -      /* convert speed to USB mouse speed 1 to 127 */ -    speed = (uint8_t)(speed / (1000.0f / mk_interval)); -    speed = speed < 1 ? 1 : speed; +    speed = (uint8_t)(speed / (1000U / mk_interval)); -    return speed > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : speed; +    if (speed > MOUSEKEY_MOVE_MAX) { +        speed = MOUSEKEY_MOVE_MAX; +    } else if (speed < 1) { +        speed = 1; +    } +    return speed;  }  static uint8_t wheel_unit(void) { -    float speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS; +    uint16_t speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS; -    if (mousekey_accel & ((1 << 0) | (1 << 2))) { -        speed = mousekey_accel & (1 << 2) ? MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS : MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS; +    if (mousekey_accel & (1 << 0)) { +        speed = MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS; +    } else if (mousekey_accel & (1 << 2)) { +        speed = MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS;      } else if (mousekey_wheel_repeat && mouse_timer) {          if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) { -            const float time_elapsed = timer_elapsed(mouse_timer) / 50; -            speed                    = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + 1 * 0.5 * time_elapsed * time_elapsed; +            const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50; +            speed                       = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + (1 * time_elapsed * time_elapsed) / 2; +        } +        if (speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS) { +            speed = MOUSEKEY_WHEEL_BASE_MOVEMENTS;          } -        speed = speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS ? MOUSEKEY_WHEEL_BASE_MOVEMENTS : speed;      } -    mk_wheel_interval = 1000.0f / speed; - -    return (uint8_t)speed > MOUSEKEY_WHEEL_INITIAL_MOVEMENTS ? 2 : 1; +    mk_wheel_interval = 1000U / speed; +    return 1;  }  #        endif /* #ifndef MK_KINETIC_SPEED */ diff --git a/quantum/painter/lvgl/qp_lvgl.c b/quantum/painter/lvgl/qp_lvgl.c index 6cc0061d73..280aeaf91f 100644 --- a/quantum/painter/lvgl/qp_lvgl.c +++ b/quantum/painter/lvgl/qp_lvgl.c @@ -61,7 +61,7 @@ bool qp_lvgl_attach(painter_device_t device) {      qp_lvgl_detach();      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_lvgl_attach: fail (validation_ok == false)\n");          qp_lvgl_detach();          return false; diff --git a/quantum/painter/qp.c b/quantum/painter/qp.c index aea9381b60..f27bb7892a 100644 --- a/quantum/painter/qp.c +++ b/quantum/painter/qp.c @@ -30,6 +30,11 @@ bool qp_init(painter_device_t device, painter_rotation_t rotation) {      qp_dprintf("qp_init: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; +    if (!driver) { +        qp_dprintf("qp_init: fail (pointer to NULL)\n"); +        return false; +    } +      driver->validate_ok = false;      if (!validate_driver_integrity(driver)) {          qp_dprintf("Failed to validate driver integrity in qp_init\n"); @@ -65,7 +70,7 @@ bool qp_init(painter_device_t device, painter_rotation_t rotation) {  bool qp_power(painter_device_t device, bool power_on) {      qp_dprintf("qp_power: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_power: fail (validation_ok == false)\n");          return false;      } @@ -87,7 +92,7 @@ bool qp_power(painter_device_t device, bool power_on) {  bool qp_clear(painter_device_t device) {      qp_dprintf("qp_clear: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_clear: fail (validation_ok == false)\n");          return false;      } @@ -109,7 +114,7 @@ bool qp_clear(painter_device_t device) {  bool qp_flush(painter_device_t device) {      qp_dprintf("qp_flush: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_flush: fail (validation_ok == false)\n");          return false;      } @@ -129,9 +134,14 @@ bool qp_flush(painter_device_t device) {  // Quantum Painter External API: qp_get_geometry  void qp_get_geometry(painter_device_t device, uint16_t *width, uint16_t *height, painter_rotation_t *rotation, uint16_t *offset_x, uint16_t *offset_y) { -    qp_dprintf("qp_geometry: entry\n"); +    qp_dprintf("qp_get_geometry: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; +    if (!driver) { +        qp_dprintf("qp_get_geometry: fail (pointer to NULL)\n"); +        return; +    } +      switch (driver->rotation) {          default:          case QP_ROTATION_0: @@ -166,7 +176,7 @@ void qp_get_geometry(painter_device_t device, uint16_t *width, uint16_t *height,          *offset_y = driver->offset_y;      } -    qp_dprintf("qp_geometry: ok\n"); +    qp_dprintf("qp_get_geometry: ok\n");  }  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -176,6 +186,11 @@ void qp_set_viewport_offsets(painter_device_t device, uint16_t offset_x, uint16_      qp_dprintf("qp_set_viewport_offsets: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; +    if (!driver) { +        qp_dprintf("qp_set_viewport_offsets: fail (pointer to NULL)\n"); +        return; +    } +      driver->offset_x = offset_x;      driver->offset_y = offset_y; @@ -188,7 +203,7 @@ void qp_set_viewport_offsets(painter_device_t device, uint16_t offset_x, uint16_  bool qp_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {      qp_dprintf("qp_viewport: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_viewport: fail (validation_ok == false)\n");          return false;      } @@ -211,7 +226,7 @@ bool qp_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t  bool qp_pixdata(painter_device_t device, const void *pixel_data, uint32_t native_pixel_count) {      qp_dprintf("qp_pixdata: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_pixdata: fail (validation_ok == false)\n");          return false;      } diff --git a/quantum/painter/qp_comms.c b/quantum/painter/qp_comms.c index bcc6de8f2e..63667783e1 100644 --- a/quantum/painter/qp_comms.c +++ b/quantum/painter/qp_comms.c @@ -8,7 +8,7 @@  bool qp_comms_init(painter_device_t device) {      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_comms_init: fail (validation_ok == false)\n");          return false;      } @@ -18,7 +18,7 @@ bool qp_comms_init(painter_device_t device) {  bool qp_comms_start(painter_device_t device) {      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_comms_start: fail (validation_ok == false)\n");          return false;      } @@ -28,7 +28,7 @@ bool qp_comms_start(painter_device_t device) {  void qp_comms_stop(painter_device_t device) {      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_comms_stop: fail (validation_ok == false)\n");          return;      } @@ -38,7 +38,7 @@ void qp_comms_stop(painter_device_t device) {  uint32_t qp_comms_send(painter_device_t device, const void *data, uint32_t byte_count) {      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_comms_send: fail (validation_ok == false)\n");          return false;      } diff --git a/quantum/painter/qp_draw_circle.c b/quantum/painter/qp_draw_circle.c index 25517d91c5..7f5a7ddfcd 100644 --- a/quantum/painter/qp_draw_circle.c +++ b/quantum/painter/qp_draw_circle.c @@ -128,7 +128,7 @@ static bool qp_circle_helper_impl(painter_device_t device, uint16_t centerx, uin  bool qp_circle(painter_device_t device, uint16_t x, uint16_t y, uint16_t radius, uint8_t hue, uint8_t sat, uint8_t val, bool filled) {      qp_dprintf("qp_circle: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_circle: fail (validation_ok == false)\n");          return false;      } diff --git a/quantum/painter/qp_draw_core.c b/quantum/painter/qp_draw_core.c index 3988aaedf8..aa5fa4aa76 100644 --- a/quantum/painter/qp_draw_core.c +++ b/quantum/painter/qp_draw_core.c @@ -145,7 +145,7 @@ bool qp_internal_load_qgf_palette(qp_stream_t *stream, uint8_t bpp) {  bool qp_setpixel(painter_device_t device, uint16_t x, uint16_t y, uint8_t hue, uint8_t sat, uint8_t val) {      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_setpixel: fail (validation_ok == false)\n");          return false;      } @@ -175,7 +175,7 @@ bool qp_line(painter_device_t device, uint16_t x0, uint16_t y0, uint16_t x1, uin      qp_dprintf("qp_line(%d, %d, %d, %d): entry\n", (int)x0, (int)y0, (int)x1, (int)y1);      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_line: fail (validation_ok == false)\n");          return false;      } @@ -253,7 +253,7 @@ bool qp_internal_fillrect_helper_impl(painter_device_t device, uint16_t left, ui  bool qp_rect(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint8_t hue, uint8_t sat, uint8_t val, bool filled) {      qp_dprintf("qp_rect(%d, %d, %d, %d): entry\n", (int)left, (int)top, (int)right, (int)bottom);      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_rect: fail (validation_ok == false)\n");          return false;      } diff --git a/quantum/painter/qp_draw_ellipse.c b/quantum/painter/qp_draw_ellipse.c index 5c7abd7a7d..e912a3e91f 100644 --- a/quantum/painter/qp_draw_ellipse.c +++ b/quantum/painter/qp_draw_ellipse.c @@ -62,7 +62,7 @@ static bool qp_ellipse_helper_impl(painter_device_t device, uint16_t centerx, ui  bool qp_ellipse(painter_device_t device, uint16_t x, uint16_t y, uint16_t sizex, uint16_t sizey, uint8_t hue, uint8_t sat, uint8_t val, bool filled) {      qp_dprintf("qp_ellipse: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_ellipse: fail (validation_ok == false)\n");          return false;      } diff --git a/quantum/painter/qp_draw_image.c b/quantum/painter/qp_draw_image.c index 9f86b29f8b..fb17a05a1b 100644 --- a/quantum/painter/qp_draw_image.c +++ b/quantum/painter/qp_draw_image.c @@ -90,7 +90,7 @@ painter_image_handle_t qp_load_image_mem(const void *buffer) {  bool qp_close_image(painter_image_handle_t image) {      qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image; -    if (!qgf_image->validate_ok) { +    if (!qgf_image || !qgf_image->validate_ok) {          qp_dprintf("qp_close_image: fail (invalid image)\n");          return false;      } @@ -210,13 +210,13 @@ static bool qp_drawimage_prepare_frame_for_stream_read(painter_device_t device,  static bool qp_drawimage_recolor_impl(painter_device_t device, uint16_t x, uint16_t y, painter_image_handle_t image, int frame_number, qgf_frame_info_t *frame_info, qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888) {      qp_dprintf("qp_drawimage_recolor: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_drawimage_recolor: fail (validation_ok == false)\n");          return false;      }      qgf_image_handle_t *qgf_image = (qgf_image_handle_t *)image; -    if (!qgf_image->validate_ok) { +    if (!qgf_image || !qgf_image->validate_ok) {          qp_dprintf("qp_drawimage_recolor: fail (invalid image)\n");          return false;      } diff --git a/quantum/painter/qp_draw_text.c b/quantum/painter/qp_draw_text.c index a70caac6f4..ff6fc01d11 100644 --- a/quantum/painter/qp_draw_text.c +++ b/quantum/painter/qp_draw_text.c @@ -136,7 +136,7 @@ painter_font_handle_t qp_load_font_mem(const void *buffer) {  bool qp_close_font(painter_font_handle_t font) {      qff_font_handle_t *qff_font = (qff_font_handle_t *)font; -    if (!qff_font->validate_ok) { +    if (!qff_font || !qff_font->validate_ok) {          qp_dprintf("qp_close_font: fail (invalid font)\n");          return false;      } @@ -380,7 +380,7 @@ static inline bool qp_font_code_point_handler_drawglyph(qff_font_handle_t *qff_f  int16_t qp_textwidth(painter_font_handle_t font, const char *str) {      qff_font_handle_t *qff_font = (qff_font_handle_t *)font; -    if (!qff_font->validate_ok) { +    if (!qff_font || !qff_font->validate_ok) {          qp_dprintf("qp_textwidth: fail (invalid font)\n");          return false;      } @@ -406,13 +406,13 @@ int16_t qp_drawtext(painter_device_t device, uint16_t x, uint16_t y, painter_fon  int16_t qp_drawtext_recolor(painter_device_t device, uint16_t x, uint16_t y, painter_font_handle_t font, const char *str, uint8_t hue_fg, uint8_t sat_fg, uint8_t val_fg, uint8_t hue_bg, uint8_t sat_bg, uint8_t val_bg) {      qp_dprintf("qp_drawtext_recolor: entry\n");      painter_driver_t *driver = (painter_driver_t *)device; -    if (!driver->validate_ok) { +    if (!driver || !driver->validate_ok) {          qp_dprintf("qp_drawtext_recolor: fail (validation_ok == false)\n");          return 0;      }      qff_font_handle_t *qff_font = (qff_font_handle_t *)font; -    if (!qff_font->validate_ok) { +    if (!qff_font || !qff_font->validate_ok) {          qp_dprintf("qp_drawtext_recolor: fail (invalid font)\n");          return false;      } diff --git a/quantum/process_keycode/process_auto_shift.c b/quantum/process_keycode/process_auto_shift.c index 62c347ae0c..63e893fc18 100644 --- a/quantum/process_keycode/process_auto_shift.c +++ b/quantum/process_keycode/process_auto_shift.c @@ -75,7 +75,15 @@ __attribute__((weak)) bool get_auto_shifted_key(uint16_t keycode, keyrecord_t *r          case AUTO_SHIFT_NUMERIC:  #    endif  #    ifndef NO_AUTO_SHIFT_SPECIAL -        case AUTO_SHIFT_SPECIAL: +#        ifndef NO_AUTO_SHIFT_TAB +        case KC_TAB: +#        endif +#        ifndef NO_AUTO_SHIFT_SYMBOLS +        case AUTO_SHIFT_SYMBOLS: +#        endif +#    endif +#    ifdef AUTO_SHIFT_ENTER +        case KC_ENT:  #    endif              return true;      } diff --git a/quantum/process_keycode/process_auto_shift.h b/quantum/process_keycode/process_auto_shift.h index 66a4b3138a..780177d2ff 100644 --- a/quantum/process_keycode/process_auto_shift.h +++ b/quantum/process_keycode/process_auto_shift.h @@ -28,10 +28,14 @@  // clang-format off  #define AUTO_SHIFT_ALPHA KC_A ... KC_Z  #define AUTO_SHIFT_NUMERIC KC_1 ... KC_0 +#define AUTO_SHIFT_SYMBOLS          \ +             KC_MINUS ... KC_SLASH: \ +        case KC_NONUS_BACKSLASH + +// Kept to avoid breaking existing keymaps.  #define AUTO_SHIFT_SPECIAL          \               KC_TAB:                \ -        case KC_MINUS ... KC_SLASH: \ -        case KC_NONUS_BACKSLASH +        case AUTO_SHIFT_SYMBOLS  // clang-format on  bool process_auto_shift(uint16_t keycode, keyrecord_t *record); diff --git a/quantum/process_keycode/process_autocorrect.c b/quantum/process_keycode/process_autocorrect.c index 1376788266..cc1f19e4f9 100644 --- a/quantum/process_keycode/process_autocorrect.c +++ b/quantum/process_keycode/process_autocorrect.c @@ -1,5 +1,6 @@  // Copyright 2021 Google LLC  // Copyright 2021 @filterpaper +// Copyright 2023 Pablo Martinez (@elpekenin) <elpekenin@elpekenin.dev>  // SPDX-License-Identifier: Apache-2.0  // Original source: https://getreuer.info/posts/keyboards/autocorrection @@ -57,7 +58,7 @@ void autocorrect_toggle(void) {  }  /** - * @brief handler for determining if autocorrect should process keypress + * @brief handler for user to override whether autocorrect should process this keypress   *   * @param keycode Keycode registered by matrix press, per keymap   * @param record keyrecord_t structure @@ -67,6 +68,23 @@ void autocorrect_toggle(void) {   * @return false Stop processing and escape from autocorrect.   */  __attribute__((weak)) bool process_autocorrect_user(uint16_t *keycode, keyrecord_t *record, uint8_t *typo_buffer_size, uint8_t *mods) { +    return process_autocorrect_default_handler(keycode, record, typo_buffer_size, mods); +} + +/** + * @brief fallback handler for determining if autocorrect should process this keypress + *        can be used by user callback to get the basic keycode being "wrapped" + * + * NOTE: These values may have been edited by user callback before getting here + * + * @param keycode Keycode registered by matrix press, per keymap + * @param record keyrecord_t structure + * @param typo_buffer_size passed along to allow resetting of autocorrect buffer + * @param mods allow processing of mod status + * @return true Allow autocorection + * @return false Stop processing and escape from autocorrect. + */ +bool process_autocorrect_default_handler(uint16_t *keycode, keyrecord_t *record, uint8_t *typo_buffer_size, uint8_t *mods) {      // See quantum_keycodes.h for reference on these matched ranges.      switch (*keycode) {          // Exclude these keycodes from processing. @@ -157,10 +175,12 @@ __attribute__((weak)) bool process_autocorrect_user(uint16_t *keycode, keyrecord   *   * @param backspaces number of characters to remove   * @param str pointer to PROGMEM string to replace mistyped seletion with + * @param typo the wrong string that triggered a correction + * @param correct what it would become after the changes   * @return true apply correction   * @return false user handled replacement   */ -__attribute__((weak)) bool apply_autocorrect(uint8_t backspaces, const char *str) { +__attribute__((weak)) bool apply_autocorrect(uint8_t backspaces, const char *str, char *typo, char *correct) {      return true;  } @@ -284,11 +304,57 @@ bool process_autocorrect(uint16_t keycode, keyrecord_t *record) {          if (code & 128) { // A typo was found! Apply autocorrect.              const uint8_t backspaces = (code & 63) + !record->event.pressed; -            if (apply_autocorrect(backspaces, (char const *)(autocorrect_data + state + 1))) { +            const char *  changes    = (const char *)(autocorrect_data + state + 1); + +            /* Gather info about the typo'd word +             * +             * Since buffer may contain several words, delimited by spaces, we +             * iterate from the end to find the start and length of the typo +             */ +            char typo[AUTOCORRECT_MAX_LENGTH + 1] = {0}; // extra char for null terminator + +            uint8_t typo_len   = 0; +            uint8_t typo_start = 0; +            bool    space_last = typo_buffer[typo_buffer_size - 1] == KC_SPC; +            for (uint8_t i = typo_buffer_size; i > 0; --i) { +                // stop counting after finding space (unless it is the last thing) +                if (typo_buffer[i - 1] == KC_SPC && i != typo_buffer_size) { +                    typo_start = i; +                    break; +                } + +                ++typo_len; +            } + +            // when detecting 'typo:', reduce the length of the string by one +            if (space_last) { +                --typo_len; +            } + +            // convert buffer of keycodes into a string +            for (uint8_t i = 0; i < typo_len; ++i) { +                typo[i] = typo_buffer[typo_start + i] - KC_A + 'a'; +            } + +            /* Gather the corrected word +             * +             * A) Correction of 'typo:' -- Code takes into account +             * an extra backspace to delete the space (which we dont copy) +             * for this reason the offset is correct to "skip" the null terminator +             * +             * B) When correcting 'typo' -- Need extra offset for terminator +             */ +            char correct[AUTOCORRECT_MAX_LENGTH + 10] = {0}; // let's hope this is big enough + +            uint8_t offset = space_last ? backspaces : backspaces + 1; +            strcpy(correct, typo); +            strcpy_P(correct + typo_len - offset, changes); + +            if (apply_autocorrect(backspaces, changes, typo, correct)) {                  for (uint8_t i = 0; i < backspaces; ++i) {                      tap_code(KC_BSPC);                  } -                send_string_P((char const *)(autocorrect_data + state + 1)); +                send_string_P(changes);              }              if (keycode == KC_SPC) { diff --git a/quantum/process_keycode/process_autocorrect.h b/quantum/process_keycode/process_autocorrect.h index c7596107e5..9cc7d46a8b 100644 --- a/quantum/process_keycode/process_autocorrect.h +++ b/quantum/process_keycode/process_autocorrect.h @@ -1,5 +1,6 @@  // Copyright 2021 Google LLC  // Copyright 2021 @filterpaper +// Copyright 2023 Pablo Martinez (@elpekenin) <elpekenin@elpekenin.dev>  // SPDX-License-Identifier: Apache-2.0  // Original source: https://getreuer.info/posts/keyboards/autocorrection @@ -9,7 +10,8 @@  bool process_autocorrect(uint16_t keycode, keyrecord_t *record);  bool process_autocorrect_user(uint16_t *keycode, keyrecord_t *record, uint8_t *typo_buffer_size, uint8_t *mods); -bool apply_autocorrect(uint8_t backspaces, const char *str); +bool process_autocorrect_default_handler(uint16_t *keycode, keyrecord_t *record, uint8_t *typo_buffer_size, uint8_t *mods); +bool apply_autocorrect(uint8_t backspaces, const char *str, char *typo, char *correct);  bool autocorrect_is_enabled(void);  void autocorrect_enable(void); diff --git a/quantum/process_keycode/process_dynamic_macro.c b/quantum/process_keycode/process_dynamic_macro.c index bf6af566e2..a022949d3d 100644 --- a/quantum/process_keycode/process_dynamic_macro.c +++ b/quantum/process_keycode/process_dynamic_macro.c @@ -151,6 +151,67 @@ void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_poin      *macro_end = macro_pointer;  } +/* Both macros use the same buffer but read/write on different + * ends of it. + * + * Macro1 is written left-to-right starting from the beginning of + * the buffer. + * + * Macro2 is written right-to-left starting from the end of the + * buffer. + * + * ¯o_buffer   macro_end + *  v                   v + * +------------------------------------------------------------+ + * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<| + * +------------------------------------------------------------+ + *                           ^                                 ^ + *                         r_macro_end                  r_macro_buffer + * + * During the recording when one macro encounters the end of the + * other macro, the recording is stopped. Apart from this, there + * are no arbitrary limits for the macros' length in relation to + * each other: for example one can either have two medium sized + * macros or one long macro and one short macro. Or even one empty + * and one using the whole buffer. + */ +static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE]; + +/* Pointer to the first buffer element after the first macro. + * Initially points to the very beginning of the buffer since the + * macro is empty. */ +static keyrecord_t *macro_end = macro_buffer; + +/* The other end of the macro buffer. Serves as the beginning of + * the second macro. */ +static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1; + +/* Like macro_end but for the second macro. */ +static keyrecord_t *r_macro_end = r_macro_buffer; + +/* A persistent pointer to the current macro position (iterator) + * used during the recording. */ +static keyrecord_t *macro_pointer = NULL; + +/* 0   - no macro is being recorded right now + * 1,2 - either macro 1 or 2 is being recorded */ +static uint8_t macro_id = 0; + +/** + * If a dynamic macro is currently being recorded, stop recording. + */ +void dynamic_macro_stop_recording(void) { +    switch (macro_id) { +        case 1: +            dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end); +            break; +        case 2: +            dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end); +            break; +    } +    macro_id = 0; +} +  /* Handle the key events related to the dynamic macros. Should be   * called from process_record_user() like this:   * @@ -162,52 +223,6 @@ void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_poin   *   }   */  bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) { -    /* Both macros use the same buffer but read/write on different -     * ends of it. -     * -     * Macro1 is written left-to-right starting from the beginning of -     * the buffer. -     * -     * Macro2 is written right-to-left starting from the end of the -     * buffer. -     * -     * ¯o_buffer   macro_end -     *  v                   v -     * +------------------------------------------------------------+ -     * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<| -     * +------------------------------------------------------------+ -     *                           ^                                 ^ -     *                         r_macro_end                  r_macro_buffer -     * -     * During the recording when one macro encounters the end of the -     * other macro, the recording is stopped. Apart from this, there -     * are no arbitrary limits for the macros' length in relation to -     * each other: for example one can either have two medium sized -     * macros or one long macro and one short macro. Or even one empty -     * and one using the whole buffer. -     */ -    static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE]; - -    /* Pointer to the first buffer element after the first macro. -     * Initially points to the very beginning of the buffer since the -     * macro is empty. */ -    static keyrecord_t *macro_end = macro_buffer; - -    /* The other end of the macro buffer. Serves as the beginning of -     * the second macro. */ -    static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1; - -    /* Like macro_end but for the second macro. */ -    static keyrecord_t *r_macro_end = r_macro_buffer; - -    /* A persistent pointer to the current macro position (iterator) -     * used during the recording. */ -    static keyrecord_t *macro_pointer = NULL; - -    /* 0   - no macro is being recorded right now -     * 1,2 - either macro 1 or 2 is being recorded */ -    static uint8_t macro_id = 0; -      if (macro_id == 0) {          /* No macro recording in progress. */          if (!record->event.pressed) { @@ -238,15 +253,7 @@ bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {                  if (record->event.pressed ^ (keycode != QK_DYNAMIC_MACRO_RECORD_STOP)) { /* Ignore the initial release                                                                                            * just after the recording                                                                                            * starts for DM_RSTP. */ -                    switch (macro_id) { -                        case 1: -                            dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end); -                            break; -                        case 2: -                            dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end); -                            break; -                    } -                    macro_id = 0; +                    dynamic_macro_stop_recording();                  }                  return false;  #ifdef DYNAMIC_MACRO_NO_NESTING diff --git a/quantum/process_keycode/process_dynamic_macro.h b/quantum/process_keycode/process_dynamic_macro.h index ab70726897..9841254af4 100644 --- a/quantum/process_keycode/process_dynamic_macro.h +++ b/quantum/process_keycode/process_dynamic_macro.h @@ -39,3 +39,4 @@ void dynamic_macro_record_start_user(int8_t direction);  void dynamic_macro_play_user(int8_t direction);  void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record);  void dynamic_macro_record_end_user(int8_t direction); +void dynamic_macro_stop_recording(void); diff --git a/quantum/process_keycode/process_key_override.c b/quantum/process_keycode/process_key_override.c index 17e490e67a..de628d3fec 100644 --- a/quantum/process_keycode/process_key_override.c +++ b/quantum/process_keycode/process_key_override.c @@ -322,6 +322,15 @@ static bool try_activating_override(const uint16_t keycode, const uint8_t layer,          clear_active_override(false); +#ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE +        // Send a dummy keycode before unregistering the modifier(s) +        // so that suppressing the modifier(s) doesn't falsely get interpreted +        // by the host OS as a tap of a modifier key. +        // For example, unintended activations of the start menu on Windows when +        // using a GUI+<kc> key override with suppressed mods. +        neutralize_flashing_modifiers(active_mods); +#endif +          active_override                 = override;          active_override_trigger_is_down = true; diff --git a/quantum/quantum.c b/quantum/quantum.c index 091cf298f7..c6a40a4baa 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -310,15 +310,15 @@ bool process_record_quantum(keyrecord_t *record) {  #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)              process_music(keycode, record) &&  #endif +#ifdef CAPS_WORD_ENABLE +            process_caps_word(keycode, record) && +#endif  #ifdef KEY_OVERRIDE_ENABLE              process_key_override(keycode, record) &&  #endif  #ifdef TAP_DANCE_ENABLE              process_tap_dance(keycode, record) &&  #endif -#ifdef CAPS_WORD_ENABLE -            process_caps_word(keycode, record) && -#endif  #if defined(UNICODE_COMMON_ENABLE)              process_unicode_common(keycode, record) &&  #endif @@ -468,7 +468,7 @@ void suspend_power_down_quantum(void) {  #ifndef NO_SUSPEND_POWER_DOWN  // Turn off backlight  #    ifdef BACKLIGHT_ENABLE -    backlight_set(0); +    backlight_level_noeeprom(0);  #    endif  #    ifdef LED_MATRIX_ENABLE diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h index f931b7e4c7..d3249bd455 100644 --- a/quantum/quantum_keycodes.h +++ b/quantum/quantum_keycodes.h @@ -179,10 +179,10 @@  #define QK_UNICODE_GET_CODE_POINT(kc) ((kc)&0x7FFF)  // UNICODEMAP_ENABLE - Allows Unicode input up to 0x10FFFF, requires unicode_map -#define X(i) (QK_UNICODEMAP | ((i)&0x3FFF)) +#define UM(i) (QK_UNICODEMAP | ((i)&0x3FFF))  #define QK_UNICODEMAP_GET_INDEX(kc) ((kc)&0x3FFF) -#define XP(i, j) (QK_UNICODEMAP_PAIR | ((i)&0x7F) | (((j)&0x7F) << 7)) // 127 max i and j +#define UP(i, j) (QK_UNICODEMAP_PAIR | ((i)&0x7F) | (((j)&0x7F) << 7)) // 127 max i and j  #define QK_UNICODEMAP_PAIR_GET_UNSHIFTED_INDEX(kc) ((kc)&0x7F)  #define QK_UNICODEMAP_PAIR_GET_SHIFTED_INDEX(kc) (((kc) >> 7) & 0x7F) diff --git a/quantum/quantum_keycodes_legacy.h b/quantum/quantum_keycodes_legacy.h index 120c98bc62..ad078cdad5 100644 --- a/quantum/quantum_keycodes_legacy.h +++ b/quantum/quantum_keycodes_legacy.h @@ -53,3 +53,6 @@  #define GUI_ON  QK_MAGIC_GUI_ON  #define GUI_OFF QK_MAGIC_GUI_OFF  #define GUI_TOG QK_MAGIC_TOGGLE_GUI + +#define X(i) UM(i) +#define XP(i, j) UM(i, j) diff --git a/quantum/rgb_matrix/animations/pixel_fractal_anim.h b/quantum/rgb_matrix/animations/pixel_fractal_anim.h index 875b4ceb3d..4cd1d9b861 100644 --- a/quantum/rgb_matrix/animations/pixel_fractal_anim.h +++ b/quantum/rgb_matrix/animations/pixel_fractal_anim.h @@ -7,7 +7,11 @@ RGB_MATRIX_EFFECT(PIXEL_FRACTAL)  #    ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS  static bool PIXEL_FRACTAL(effect_params_t* params) { -#        define MID_COL MATRIX_COLS / 2 +#        if MATRIX_COLS < 2 +#            define MID_COL 1 +#        else +#            define MID_COL MATRIX_COLS / 2 +#        endif      static bool     led[MATRIX_ROWS][MID_COL];      static uint32_t wait_timer = 0; diff --git a/quantum/rgb_matrix/animations/pixel_rain_anim.h b/quantum/rgb_matrix/animations/pixel_rain_anim.h index 9d63f451e2..26cd73b578 100644 --- a/quantum/rgb_matrix/animations/pixel_rain_anim.h +++ b/quantum/rgb_matrix/animations/pixel_rain_anim.h @@ -16,13 +16,9 @@ static bool PIXEL_RAIN(effect_params_t* params) {          if (!HAS_ANY_FLAGS(g_led_config.flags[led_index], params->flags)) {              return;          } -        if (random8() & 2) { -            rgb_matrix_set_color(led_index, 0, 0, 0); -        } else { -            HSV hsv = {random8(), random8_min_max(127, 255), rgb_matrix_config.hsv.v}; -            RGB rgb = rgb_matrix_hsv_to_rgb(hsv); -            rgb_matrix_set_color(led_index, rgb.r, rgb.g, rgb.b); -        } +        HSV hsv = (random8() & 2) ? (HSV){0, 0, 0} : (HSV){random8(), random8_min_max(127, 255), rgb_matrix_config.hsv.v}; +        RGB rgb = rgb_matrix_hsv_to_rgb(hsv); +        rgb_matrix_set_color(led_index, rgb.r, rgb.g, rgb.b);          wait_timer = g_rgb_timer + interval();      } diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c index 1f3912cf7e..1680389793 100644 --- a/quantum/rgb_matrix/rgb_matrix.c +++ b/quantum/rgb_matrix/rgb_matrix.c @@ -428,7 +428,10 @@ void rgb_matrix_task(void) {          case RENDERING:              rgb_task_render(effect);              if (effect) { -                rgb_matrix_indicators(); +                // Only run the basic indicators in the last render iteration (default there are 5 iterations) +                if (rgb_effect_params.iter == RGB_MATRIX_LED_PROCESS_MAX_ITERATIONS) { +                    rgb_matrix_indicators(); +                }                  rgb_matrix_indicators_advanced(&rgb_effect_params);              }              break; diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h index 9ea248b66d..83851d8995 100644 --- a/quantum/rgb_matrix/rgb_matrix.h +++ b/quantum/rgb_matrix/rgb_matrix.h @@ -49,8 +49,9 @@  #endif  #ifndef RGB_MATRIX_LED_PROCESS_LIMIT -#    define RGB_MATRIX_LED_PROCESS_LIMIT (RGB_MATRIX_LED_COUNT + 4) / 5 +#    define RGB_MATRIX_LED_PROCESS_LIMIT ((RGB_MATRIX_LED_COUNT + 4) / 5)  #endif +#define RGB_MATRIX_LED_PROCESS_MAX_ITERATIONS ((RGB_MATRIX_LED_COUNT + RGB_MATRIX_LED_PROCESS_LIMIT - 1) / RGB_MATRIX_LED_PROCESS_LIMIT)  #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT  #    if defined(RGB_MATRIX_SPLIT) diff --git a/quantum/rgb_matrix/rgb_matrix_drivers.c b/quantum/rgb_matrix/rgb_matrix_drivers.c index d66fc801dd..6de20ac8a5 100644 --- a/quantum/rgb_matrix/rgb_matrix_drivers.c +++ b/quantum/rgb_matrix/rgb_matrix_drivers.c @@ -426,11 +426,17 @@ const rgb_matrix_driver_t rgb_matrix_driver = {  // LED color buffer  LED_TYPE rgb_matrix_ws2812_array[RGB_MATRIX_LED_COUNT]; +bool     ws2812_dirty = false; -static void init(void) {} +static void init(void) { +    ws2812_dirty = false; +}  static void flush(void) { -    ws2812_setleds(rgb_matrix_ws2812_array, RGB_MATRIX_LED_COUNT); +    if (ws2812_dirty) { +        ws2812_setleds(rgb_matrix_ws2812_array, RGB_MATRIX_LED_COUNT); +        ws2812_dirty = false; +    }  }  // Set an led in the buffer to a color @@ -448,6 +454,11 @@ static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {      }  #    endif +    if (rgb_matrix_ws2812_array[i].r == r && rgb_matrix_ws2812_array[i].g == g && rgb_matrix_ws2812_array[i].b == b) { +        return; +    } + +    ws2812_dirty                 = true;      rgb_matrix_ws2812_array[i].r = r;      rgb_matrix_ws2812_array[i].g = g;      rgb_matrix_ws2812_array[i].b = b; diff --git a/quantum/split_common/transactions.c b/quantum/split_common/transactions.c index b3c80f1194..8cd018a6ec 100644 --- a/quantum/split_common/transactions.c +++ b/quantum/split_common/transactions.c @@ -412,7 +412,7 @@ static void backlight_handlers_slave(matrix_row_t master_matrix[], matrix_row_t      uint8_t backlight_level = split_shmem->backlight_level;      split_shared_memory_unlock(); -    backlight_set(backlight_level); +    backlight_level_noeeprom(backlight_level);  }  #    define TRANSACTIONS_BACKLIGHT_MASTER() TRANSACTION_HANDLER_MASTER(backlight) diff --git a/quantum/via.c b/quantum/via.c index c54e37a175..f4293dacd4 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -31,7 +31,7 @@  #include "eeprom.h"  #include "version.h" // for QMK_BUILDDATE used in EEPROM magic -#if defined(RGB_MATRIX_ENABLE) +#if (defined(RGB_MATRIX_ENABLE) || defined(LED_MATRIX_ENABLE))  #    include <lib/lib8tion/lib8tion.h>  #endif @@ -141,6 +141,9 @@ __attribute__((weak)) void via_set_device_indication(uint8_t value) {  #if defined(RGB_MATRIX_ENABLE)      rgb_matrix_toggle_noeeprom();  #endif // RGB_MATRIX_ENABLE +#if defined(LED_MATRIX_ENABLE) +    led_matrix_toggle_noeeprom(); +#endif // LED_MATRIX_ENABLE  #if defined(AUDIO_ENABLE)      if (value == 0) {          wait_ms(10); @@ -194,6 +197,7 @@ __attribute__((weak)) void via_custom_value_command_kb(uint8_t *data, uint8_t le  //      id_qmk_backlight_channel    ->  via_qmk_backlight_command()  //      id_qmk_rgblight_channel     ->  via_qmk_rgblight_command()  //      id_qmk_rgb_matrix_channel   ->  via_qmk_rgb_matrix_command() +//      id_qmk_led_matrix_channel   ->  via_qmk_led_matrix_command()  //      id_qmk_audio_channel        ->  via_qmk_audio_command()  //  __attribute__((weak)) void via_custom_value_command(uint8_t *data, uint8_t length) { @@ -219,7 +223,14 @@ __attribute__((weak)) void via_custom_value_command(uint8_t *data, uint8_t lengt          via_qmk_rgb_matrix_command(data, length);          return;      } -#endif // RGBLIGHT_ENABLE +#endif // RGB_MATRIX_ENABLE + +#if defined(LED_MATRIX_ENABLE) +    if (*channel_id == id_qmk_led_matrix_channel) { +        via_qmk_led_matrix_command(data, length); +        return; +    } +#endif // LED_MATRIX_ENABLE  #if defined(AUDIO_ENABLE)      if (*channel_id == id_qmk_audio_channel) { @@ -692,6 +703,90 @@ void via_qmk_rgb_matrix_save(void) {  #endif // RGB_MATRIX_ENABLE +#if defined(LED_MATRIX_ENABLE) + +#    if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX +#        undef LED_MATRIX_MAXIMUM_BRIGHTNESS +#        define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX +#    endif + +void via_qmk_led_matrix_command(uint8_t *data, uint8_t length) { +    // data = [ command_id, channel_id, value_id, value_data ] +    uint8_t *command_id        = &(data[0]); +    uint8_t *value_id_and_data = &(data[2]); + +    switch (*command_id) { +        case id_custom_set_value: { +            via_qmk_led_matrix_set_value(value_id_and_data); +            break; +        } +        case id_custom_get_value: { +            via_qmk_led_matrix_get_value(value_id_and_data); +            break; +        } +        case id_custom_save: { +            via_qmk_led_matrix_save(); +            break; +        } +        default: { +            *command_id = id_unhandled; +            break; +        } +    } +} + +void via_qmk_led_matrix_get_value(uint8_t *data) { +    // data = [ value_id, value_data ] +    uint8_t *value_id   = &(data[0]); +    uint8_t *value_data = &(data[1]); + +    switch (*value_id) { +        case id_qmk_led_matrix_brightness: { +            value_data[0] = ((uint16_t)led_matrix_get_val() * UINT8_MAX) / LED_MATRIX_MAXIMUM_BRIGHTNESS; +            break; +        } +        case id_qmk_led_matrix_effect: { +            value_data[0] = led_matrix_is_enabled() ? led_matrix_get_mode() : 0; +            break; +        } +        case id_qmk_led_matrix_effect_speed: { +            value_data[0] = led_matrix_get_speed(); +            break; +        } +    } +} + +void via_qmk_led_matrix_set_value(uint8_t *data) { +    // data = [ value_id, value_data ] +    uint8_t *value_id   = &(data[0]); +    uint8_t *value_data = &(data[1]); +    switch (*value_id) { +        case id_qmk_led_matrix_brightness: { +            led_matrix_set_val_noeeprom(scale8(value_data[0], LED_MATRIX_MAXIMUM_BRIGHTNESS)); +            break; +        } +        case id_qmk_led_matrix_effect: { +            if (value_data[0] == 0) { +                led_matrix_disable_noeeprom(); +            } else { +                led_matrix_enable_noeeprom(); +                led_matrix_mode_noeeprom(value_data[0]); +            } +            break; +        } +        case id_qmk_led_matrix_effect_speed: { +            led_matrix_set_speed_noeeprom(value_data[0]); +            break; +        } +    } +} + +void via_qmk_led_matrix_save(void) { +    eeconfig_update_led_matrix(); +} + +#endif // LED_MATRIX_ENABLE +  #if defined(AUDIO_ENABLE)  extern audio_config_t audio_config; diff --git a/quantum/via.h b/quantum/via.h index ab4eb05028..6c8465b81e 100644 --- a/quantum/via.h +++ b/quantum/via.h @@ -109,6 +109,7 @@ enum via_channel_id {      id_qmk_rgblight_channel   = 2,      id_qmk_rgb_matrix_channel = 3,      id_qmk_audio_channel      = 4, +    id_qmk_led_matrix_channel = 5,  };  enum via_qmk_backlight_value { @@ -130,6 +131,12 @@ enum via_qmk_rgb_matrix_value {      id_qmk_rgb_matrix_color        = 4,  }; +enum via_qmk_led_matrix_value { +    id_qmk_led_matrix_brightness   = 1, +    id_qmk_led_matrix_effect       = 2, +    id_qmk_led_matrix_effect_speed = 3, +}; +  enum via_qmk_audio_value {      id_qmk_audio_enable        = 1,      id_qmk_audio_clicky_enable = 2, @@ -182,6 +189,13 @@ void via_qmk_rgb_matrix_get_value(uint8_t *data);  void via_qmk_rgb_matrix_save(void);  #endif +#if defined(LED_MATRIX_ENABLE) +void via_qmk_led_matrix_command(uint8_t *data, uint8_t length); +void via_qmk_led_matrix_set_value(uint8_t *data); +void via_qmk_led_matrix_get_value(uint8_t *data); +void via_qmk_led_matrix_save(void); +#endif +  #if defined(AUDIO_ENABLE)  void via_qmk_audio_command(uint8_t *data, uint8_t length);  void via_qmk_audio_set_value(uint8_t *data); | 
