2022-12-15 19:14:50 -08:00
|
|
|
// Copyright 2022 Framework Computer
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2022-12-29 06:35:36 -08:00
|
|
|
#include "quantum.h"
|
2023-03-22 22:25:02 -07:00
|
|
|
#include "framework.h"
|
2023-11-26 20:55:38 -08:00
|
|
|
#include "os_detection.h"
|
2022-12-21 06:14:17 -08:00
|
|
|
|
2023-01-06 21:34:25 -08:00
|
|
|
void keyboard_post_init_kb(void) {
|
|
|
|
keyboard_post_init_user();
|
2022-12-21 06:14:17 -08:00
|
|
|
|
|
|
|
// Enable debug output
|
|
|
|
debug_enable = true;
|
|
|
|
debug_matrix = true;
|
|
|
|
debug_keyboard = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook into early keyboard initialization
|
|
|
|
*/
|
2023-01-06 21:34:25 -08:00
|
|
|
void keyboard_pre_init_kb(void) {
|
|
|
|
keyboard_pre_init_user();
|
|
|
|
|
2022-12-21 06:14:17 -08:00
|
|
|
// Mark boot as done.
|
|
|
|
// Before this, when holding down both alt keys QSPI_SS is pulled low to put
|
|
|
|
// the RP2040 in bootloader mode during reset.
|
|
|
|
setPinOutput(BOOT_DONE_GPIO);
|
2023-01-18 19:47:48 -08:00
|
|
|
writePinLow(BOOT_DONE_GPIO);
|
2023-01-05 05:09:18 -08:00
|
|
|
|
2023-01-06 21:34:25 -08:00
|
|
|
//// TODO: Do we ever need to disable it to save power?
|
|
|
|
setPinOutput(MUX_ENABLE_GPIO);
|
|
|
|
writePinHigh(MUX_ENABLE_GPIO);
|
|
|
|
|
2023-01-07 06:17:19 -08:00
|
|
|
setPinOutput(IS31FL3743A_ENABLE_GPIO);
|
2023-11-23 23:44:28 -08:00
|
|
|
#if defined(RGB_MATRIX_ENABLE)
|
2023-01-07 06:17:19 -08:00
|
|
|
writePinHigh(IS31FL3743A_ENABLE_GPIO);
|
2023-11-23 23:44:28 -08:00
|
|
|
#else
|
|
|
|
writePinLow(IS31FL3743A_ENABLE_GPIO);
|
2023-01-07 06:17:19 -08:00
|
|
|
#endif
|
|
|
|
setPinInput(SLEEP_GPIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by QMK when the keyboard suspends
|
|
|
|
*/
|
|
|
|
void suspend_power_down_kb(void) {
|
|
|
|
suspend_power_down_user();
|
|
|
|
|
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
2023-11-17 01:14:41 -08:00
|
|
|
# ifndef NO_SUSPEND_POWER_DOWN
|
2023-01-07 06:17:19 -08:00
|
|
|
writePinLow(IS31FL3743A_ENABLE_GPIO);
|
2023-11-17 01:14:41 -08:00
|
|
|
# endif
|
2023-01-07 06:17:19 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by QMK when the keyboard wakes up from suspend
|
|
|
|
*/
|
|
|
|
void suspend_wakeup_init_kb(void) {
|
|
|
|
suspend_wakeup_init_user();
|
|
|
|
|
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
2023-11-17 01:14:41 -08:00
|
|
|
# ifndef NO_SUSPEND_POWER_DOWN
|
2023-01-05 23:41:11 -08:00
|
|
|
writePinHigh(IS31FL3743A_ENABLE_GPIO);
|
2023-11-17 01:14:41 -08:00
|
|
|
# endif
|
2023-01-06 21:31:44 -08:00
|
|
|
#endif
|
2022-12-21 06:14:17 -08:00
|
|
|
}
|
2022-12-27 16:41:18 -08:00
|
|
|
|
2023-07-16 22:03:14 -07:00
|
|
|
// If in BIOS mode, no matter what the keys have been remapped to, always send them as the F keys
|
2023-11-26 20:55:38 -08:00
|
|
|
bool bios_mode = true;
|
2023-11-29 17:09:33 -08:00
|
|
|
void set_bios_mode(bool val) {
|
|
|
|
bios_mode = val;
|
|
|
|
|
|
|
|
#ifdef BIOS_HOTKEY_DEBUG
|
|
|
|
if (bios_mode) {
|
|
|
|
// Red
|
|
|
|
#if defined(RGB_MATRIX_ENABLE)
|
|
|
|
rgb_matrix_sethsv_noeeprom(0, 0xFF, 0xFF);
|
|
|
|
rgb_matrix_mode_noeeprom(1);
|
|
|
|
#endif
|
|
|
|
writePin(GP24, 1);
|
|
|
|
} else {
|
|
|
|
// White
|
|
|
|
#if defined(RGB_MATRIX_ENABLE)
|
|
|
|
rgb_matrix_sethsv_noeeprom(0, 0, 0xFF);
|
|
|
|
rgb_matrix_mode_noeeprom(1);
|
|
|
|
#endif
|
|
|
|
writePin(GP24, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2023-07-16 22:03:14 -07:00
|
|
|
bool handle_bios_hotkeys(uint16_t keycode, keyrecord_t *record) {
|
|
|
|
// Not in bios mode, no special handling, handle as normal
|
|
|
|
if (!bios_mode)
|
|
|
|
return true;
|
|
|
|
|
2023-08-29 23:38:10 -07:00
|
|
|
if (record->event.key.col == 5 && record->event.key.row == 3) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F1);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F1);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 22:03:14 -07:00
|
|
|
if (record->event.key.col == 5 && record->event.key.row == 2) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F2);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-29 23:38:10 -07:00
|
|
|
if (record->event.key.col == 4 && record->event.key.row == 6) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F3);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F3);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 4 && record->event.key.row == 3) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F4);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F4);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 10 && record->event.key.row == 4) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F5);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F5);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 10 && record->event.key.row == 3) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F6);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F6);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 10 && record->event.key.row == 2) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F7);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F7);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 15 && record->event.key.row == 1) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F8);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F8);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record->event.key.col == 11 && record->event.key.row == 3) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F9);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F9);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 22:03:14 -07:00
|
|
|
if (record->event.key.col == 8 && record->event.key.row == 4) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F10);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F10);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-29 23:38:10 -07:00
|
|
|
if (record->event.key.col == 8 && record->event.key.row == 6) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F11);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F11);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 22:03:14 -07:00
|
|
|
if (record->event.key.col == 13 && record->event.key.row == 3) {
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_F12);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_F12);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-27 16:41:18 -08:00
|
|
|
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
|
2022-12-27 22:35:44 -08:00
|
|
|
process_record_user(keycode, record);
|
2022-12-27 16:41:18 -08:00
|
|
|
|
2023-11-26 20:55:38 -08:00
|
|
|
os_variant_t os = detected_host_os();
|
2023-11-29 17:09:33 -08:00
|
|
|
set_bios_mode(true);
|
2023-11-29 19:41:38 -08:00
|
|
|
#if defined(OS_DETECTION_RGB_DEBUG) && defined(RGB_MATRIX_ENABLE)
|
|
|
|
rgb_matrix_mode_noeeprom(1);
|
|
|
|
switch (os) {
|
|
|
|
case OS_UNSURE:
|
|
|
|
rgb_matrix_sethsv(0, 0, 255); // White
|
|
|
|
break;
|
|
|
|
case OS_LINUX:
|
|
|
|
// Works on laptop and android
|
|
|
|
rgb_matrix_sethsv(213, 255, 255); // purple
|
|
|
|
break;
|
|
|
|
case OS_WINDOWS:
|
|
|
|
rgb_matrix_sethsv(170, 255, 255); // blue
|
|
|
|
break;
|
|
|
|
case OS_MACOS:
|
|
|
|
rgb_matrix_sethsv(85, 255, 255); // green
|
|
|
|
break;
|
|
|
|
case OS_UEFI:
|
|
|
|
case OS_IOS:
|
|
|
|
// works on M1 mac
|
|
|
|
rgb_matrix_sethsv(43, 255, 255); // yellow
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rgb_matrix_sethsv(125, 255, 255); // cyan
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2023-11-26 20:55:38 -08:00
|
|
|
switch (os) {
|
|
|
|
case OS_UNSURE:
|
|
|
|
break;
|
|
|
|
case OS_LINUX:
|
2023-11-29 17:09:33 -08:00
|
|
|
set_bios_mode(false);
|
2023-11-26 20:55:38 -08:00
|
|
|
break;
|
|
|
|
case OS_WINDOWS:
|
2023-11-29 17:09:33 -08:00
|
|
|
set_bios_mode(false);
|
2023-11-26 20:55:38 -08:00
|
|
|
break;
|
|
|
|
case OS_MACOS:
|
|
|
|
break;
|
|
|
|
case OS_UEFI:
|
|
|
|
case OS_IOS:
|
2023-11-29 17:09:33 -08:00
|
|
|
set_bios_mode(true);
|
2023-11-26 20:55:38 -08:00
|
|
|
// works on M1 mac
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//#endif
|
|
|
|
|
2023-11-26 22:12:17 -08:00
|
|
|
// Enable NKRO only in OS. Can cause issues with some pre-boot environments
|
|
|
|
// Framework UEFI is ok, but I think grub can't handle it properly
|
|
|
|
if (!bios_mode) {
|
|
|
|
keymap_config.nkro = 1;
|
|
|
|
}
|
|
|
|
|
2023-07-16 22:03:14 -07:00
|
|
|
if (!handle_bios_hotkeys(keycode, record)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:29:26 -07:00
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
2023-07-10 13:25:55 -07:00
|
|
|
uint8_t h;
|
|
|
|
uint8_t s;
|
|
|
|
uint8_t v;
|
|
|
|
uint8_t new_v;
|
2023-07-10 13:29:26 -07:00
|
|
|
#endif
|
2023-07-10 13:25:55 -07:00
|
|
|
|
2022-12-27 16:41:18 -08:00
|
|
|
switch (keycode) {
|
2023-07-10 13:25:55 -07:00
|
|
|
// Implement step brightness for RGB backlight
|
2023-07-10 13:29:26 -07:00
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
2023-11-23 22:41:28 -08:00
|
|
|
case RGB_MODE_FORWARD:
|
|
|
|
case RGB_MODE_REVERSE:
|
|
|
|
// Turn on if it was off
|
|
|
|
if (!rgb_matrix_is_enabled()) {
|
|
|
|
rgb_matrix_enable();
|
|
|
|
}
|
|
|
|
return true;
|
2023-07-10 13:25:55 -07:00
|
|
|
case BL_STEP:
|
|
|
|
if (record->event.pressed) {
|
|
|
|
h = rgb_matrix_get_hue();
|
|
|
|
s = rgb_matrix_get_sat();
|
|
|
|
v = rgb_matrix_get_val();
|
|
|
|
switch (v) {
|
|
|
|
default: // Default when user set to a different level
|
|
|
|
case 0:
|
|
|
|
new_v = 85;
|
|
|
|
break;
|
|
|
|
case 85:
|
|
|
|
new_v = 170;
|
|
|
|
break;
|
|
|
|
case 170:
|
|
|
|
new_v = 255;
|
|
|
|
break;
|
|
|
|
case 255:
|
|
|
|
new_v = 0;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-10 18:56:06 -07:00
|
|
|
rgb_matrix_sethsv(h, s, new_v);
|
2023-07-10 13:25:55 -07:00
|
|
|
}
|
|
|
|
return true;
|
2023-07-10 13:29:26 -07:00
|
|
|
#endif
|
2022-12-27 16:41:18 -08:00
|
|
|
case KC_SCRN:
|
|
|
|
// Simulate press WIN+P
|
|
|
|
// Works (at least) on Windows and GNOME
|
|
|
|
if (record->event.pressed) {
|
|
|
|
register_code(KC_LGUI);
|
|
|
|
register_code(KC_P);
|
|
|
|
} else {
|
|
|
|
unregister_code(KC_P);
|
2023-04-18 00:01:10 -07:00
|
|
|
unregister_code(KC_LGUI);
|
2022-12-27 16:41:18 -08:00
|
|
|
}
|
|
|
|
return false; // Skip all further processing of this key
|
|
|
|
default:
|
|
|
|
return true; // Process all other keycodes normally
|
|
|
|
}
|
2023-11-26 20:55:38 -08:00
|
|
|
|
|
|
|
return true;
|
2022-12-29 05:05:35 -08:00
|
|
|
}
|
2023-11-20 23:05:17 -08:00
|
|
|
|
2023-11-30 18:27:39 -08:00
|
|
|
#ifdef BIOS_HOTKEY_DEBUG
|
|
|
|
// Ignore capslock updates in order to manually control it
|
2023-11-20 23:05:17 -08:00
|
|
|
bool led_update_kb(led_t led_state) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-30 18:27:39 -08:00
|
|
|
#endif
|