Skip to content

Commit 32c69e0

Browse files
committed
Merge branch 'newapi' into develop
2 parents c9a56f9 + 01e33ea commit 32c69e0

File tree

12 files changed

+241
-33
lines changed

12 files changed

+241
-33
lines changed

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SRC += $(COMMON_DIR)/host.c \
1010
$(COMMON_DIR)/print.c \
1111
$(COMMON_DIR)/debug.c \
1212
$(COMMON_DIR)/util.c \
13+
$(COMMON_DIR)/hook.c \
1314
$(COMMON_DIR)/avr/suspend.c \
1415
$(COMMON_DIR)/avr/xprintf.S \
1516
$(COMMON_DIR)/avr/timer.c \

common/action.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2626
#include "action_macro.h"
2727
#include "action_util.h"
2828
#include "action.h"
29+
#include "hook.h"
2930

3031
#ifdef DEBUG_ACTION
3132
#include "debug.h"
@@ -39,6 +40,7 @@ void action_exec(keyevent_t event)
3940
if (!IS_NOEVENT(event)) {
4041
dprint("\n---- action_exec: start -----\n");
4142
dprint("EVENT: "); debug_event(event); dprintln();
43+
hook_matrix_change(event);
4244
}
4345

4446
keyrecord_t record = { .event = event };

common/action_layer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "action.h"
44
#include "util.h"
55
#include "action_layer.h"
6+
#include "hook.h"
67

78
#ifdef DEBUG_ACTION
89
#include "debug.h"
@@ -62,6 +63,7 @@ static void layer_state_set(uint32_t state)
6263
dprint("layer_state: ");
6364
layer_debug(); dprint(" to ");
6465
layer_state = state;
66+
hook_layer_change(layer_state);
6567
layer_debug(); dprintln();
6668
clear_keyboard_but_mods(); // To avoid stuck keys
6769
}

common/bootmagic.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "action_layer.h"
1010
#include "eeconfig.h"
1111
#include "bootmagic.h"
12+
#include "hook.h"
1213

1314
keymap_config_t keymap_config;
1415

@@ -40,6 +41,9 @@ void bootmagic(void)
4041
bootloader_jump();
4142
}
4243

44+
/* user-defined checks */
45+
hook_bootmagic();
46+
4347
/* debug enable */
4448
debug_config.raw = eeconfig_read_debug();
4549
if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {

common/hook.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2016 Jun Wako <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "keyboard.h"
19+
#include "hook.h"
20+
21+
/* -------------------------------------------------
22+
* Definitions of hardware-independent default hooks
23+
* ------------------------------------------------- */
24+
25+
/* Called on layer state change event. */
26+
/* Default behaviour: do nothing. */
27+
__attribute__((weak))
28+
void hook_layer_change(uint8_t layer_state) {
29+
(void)layer_state;
30+
}
31+
32+
/* Called periodically from the matrix scan loop (very often!) */
33+
/* Default behaviour: do nothing. */
34+
__attribute__((weak))
35+
void hook_keyboard_loop(void) {}
36+
37+
/* Called on matrix state change event (every keypress => often!) */
38+
/* Default behaviour: do nothing. */
39+
__attribute__((weak))
40+
void hook_matrix_change(keyevent_t event) {
41+
(void)event;
42+
}
43+
44+
/* Called on indicator LED update event (when reported from host). */
45+
/* Default behaviour: calls led_set (for compatibility). */
46+
__attribute__((weak))
47+
void hook_keyboard_leds_change(uint8_t led_status) {
48+
keyboard_set_leds(led_status);
49+
}
50+
51+
/* Called once, on checking the bootmagic combos. */
52+
/* Default behaviour: do nothing. */
53+
__attribute__((weak))
54+
void hook_bootmagic(void) {
55+
/* An example: */
56+
// #include "bootmagic.h"
57+
// #include "keymap.h"
58+
// if(bootmagic_scan_keycode(KC_W)) {
59+
// // do something
60+
// }
61+
}

common/hook.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2016 Jun Wako <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef _HOOKS_H_
19+
#define _HOOKS_H_
20+
21+
#include "keyboard.h"
22+
#include "led.h"
23+
24+
/* -------------------------------------
25+
* Hardware / one-off hooks
26+
* ------------------------------------- */
27+
28+
/* Called once, before initialising USB. */
29+
/* Default behaviour: do nothing. */
30+
void hook_early_init(void);
31+
32+
/* Called once, after USB is connected and keyboard initialised. */
33+
/* Default behaviour: do nothing. */
34+
void hook_late_init(void);
35+
36+
/* Called once, on getting SUSPEND event from USB. */
37+
/* Default behaviour: do nothing. */
38+
void hook_usb_suspend_entry(void);
39+
40+
/* Called repeatedly during the SUSPENDed state. */
41+
/* Default behaviour: power down and periodically check
42+
* the matrix, cause wakeup if needed. */
43+
void hook_usb_suspend_loop(void);
44+
45+
/* Called once, on getting WAKE event from USB. */
46+
/* Default behaviour: disables sleep LED breathing and restores
47+
* the "normal" indicator LED status by default. */
48+
void hook_usb_wakeup(void);
49+
50+
/* Called once, on checking the bootmagic combos. */
51+
/* Default behaviour: do nothing. */
52+
void hook_bootmagic(void);
53+
54+
/* -------------------------------------
55+
* Keyboard / periodic hooks
56+
* ------------------------------------- */
57+
58+
/* Called periodically from the keyboard loop (very often!) */
59+
/* Default behaviour: do nothing. */
60+
void hook_keyboard_loop(void);
61+
62+
/* Called on matrix state change event (every keypress => often!) */
63+
/* Default behaviour: do nothing. */
64+
void hook_matrix_change(keyevent_t event);
65+
66+
/* Called on layer state change event. */
67+
/* Default behaviour: do nothing. */
68+
void hook_layer_change(uint8_t layer_state);
69+
70+
/* Called on indicator LED update event (when reported from host). */
71+
/* Default behaviour: calls keyboard_set_leds (for compatibility). */
72+
void hook_keyboard_leds_change(uint8_t led_status);
73+
74+
#endif /* _HOOKS_H_ */

common/keyboard.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3030
#include "bootmagic.h"
3131
#include "eeconfig.h"
3232
#include "backlight.h"
33+
#include "hook.h"
3334
#ifdef MOUSEKEY_ENABLE
3435
# include "mousekey.h"
3536
#endif
@@ -128,11 +129,13 @@ void keyboard_task(void)
128129
if (debug_matrix) matrix_print();
129130
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
130131
if (matrix_change & ((matrix_row_t)1<<c)) {
131-
action_exec((keyevent_t){
132+
keyevent_t e = (keyevent_t){
132133
.key = (keypos_t){ .row = r, .col = c },
133134
.pressed = (matrix_row & ((matrix_row_t)1<<c)),
134135
.time = (timer_read() | 1) /* time should not be 0 */
135-
});
136+
};
137+
action_exec(e);
138+
hook_matrix_change(e);
136139
// record a processed key
137140
matrix_prev[r] ^= ((matrix_row_t)1<<c);
138141
// process a key per task call
@@ -146,6 +149,8 @@ void keyboard_task(void)
146149

147150
MATRIX_LOOP_END:
148151

152+
hook_keyboard_loop();
153+
149154
#ifdef MOUSEKEY_ENABLE
150155
// mousekey repeat & acceleration
151156
mousekey_task();
@@ -166,12 +171,12 @@ void keyboard_task(void)
166171
// update LED
167172
if (led_status != host_keyboard_leds()) {
168173
led_status = host_keyboard_leds();
169-
keyboard_set_leds(led_status);
174+
if (debug_keyboard) dprintf("LED: %02X\n", led_status);
175+
hook_keyboard_leds_change(led_status);
170176
}
171177
}
172178

173179
void keyboard_set_leds(uint8_t leds)
174180
{
175-
if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
176181
led_set(leds);
177182
}

protocol/chibios/main.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "sleep_led.h"
3636
#endif
3737
#include "suspend.h"
38+
#include "hook.h"
3839

3940

4041
/* -------------------------
@@ -58,6 +59,22 @@ host_driver_t chibios_driver = {
5859
send_consumer
5960
};
6061

62+
/* Default hooks definitions. */
63+
__attribute__((weak))
64+
void hook_early_init(void) {}
65+
66+
__attribute__((weak))
67+
void hook_late_init(void) {}
68+
69+
__attribute__((weak))
70+
void hook_usb_suspend_loop(void) {
71+
/* Do this in the suspended state */
72+
suspend_power_down(); // on AVR this deep sleeps for 15ms
73+
/* Remote wakeup */
74+
if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
75+
send_remote_wakeup(&USB_DRIVER);
76+
}
77+
}
6178

6279
/* TESTING
6380
* Amber LED blinker thread, times are in milliseconds.
@@ -91,6 +108,8 @@ int main(void) {
91108
// TESTING
92109
// chThdCreateStatic(waBlinkerThread, sizeof(waBlinkerThread), NORMALPRIO, blinkerThread, NULL);
93110

111+
hook_early_init();
112+
94113
/* Init USB */
95114
init_usb_driver(&USB_DRIVER);
96115

@@ -120,21 +139,18 @@ int main(void) {
120139

121140
print("Keyboard start.\n");
122141

142+
hook_late_init();
143+
123144
/* Main loop */
124145
while(true) {
125146

126147
if(USB_DRIVER.state == USB_SUSPENDED) {
127148
print("[s]");
128149
while(USB_DRIVER.state == USB_SUSPENDED) {
129-
/* Do this in the suspended state */
130-
suspend_power_down(); // on AVR this deep sleeps for 15ms
131-
/* Remote wakeup */
132-
if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
133-
send_remote_wakeup(&USB_DRIVER);
134-
}
150+
hook_usb_suspend_loop();
135151
}
136152
/* Woken up */
137-
// variables has been already cleared by the wakeup hook
153+
// variables have been already cleared
138154
send_keyboard_report();
139155
#ifdef MOUSEKEY_ENABLE
140156
mousekey_send();

protocol/chibios/usb_main.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@
2727
#include "sleep_led.h"
2828
#include "led.h"
2929
#endif
30+
#include "hook.h"
31+
32+
/* TMK hooks */
33+
__attribute__((weak))
34+
void hook_usb_wakeup(void) {
35+
#ifdef SLEEP_LED_ENABLE
36+
sleep_led_disable();
37+
// NOTE: converters may not accept this
38+
led_set(host_keyboard_leds());
39+
#endif /* SLEEP_LED_ENABLE */
40+
}
41+
42+
__attribute__((weak))
43+
void hook_usb_suspend_entry(void) {
44+
#ifdef SLEEP_LED_ENABLE
45+
sleep_led_enable();
46+
#endif /* SLEEP_LED_ENABLE */
47+
}
48+
3049

3150
/* ---------------------------------------------------------
3251
* Global interface variables and declarations
@@ -795,19 +814,13 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
795814

796815
case USB_EVENT_SUSPEND:
797816
//TODO: from ISR! print("[S]");
798-
#ifdef SLEEP_LED_ENABLE
799-
sleep_led_enable();
800-
#endif /* SLEEP_LED_ENABLE */
817+
hook_usb_suspend_entry();
801818
return;
802819

803820
case USB_EVENT_WAKEUP:
804821
//TODO: from ISR! print("[W]");
805822
suspend_wakeup_init();
806-
#ifdef SLEEP_LED_ENABLE
807-
sleep_led_disable();
808-
// NOTE: converters may not accept this
809-
led_set(host_keyboard_leds());
810-
#endif /* SLEEP_LED_ENABLE */
823+
hook_usb_wakeup();
811824
return;
812825

813826
case USB_EVENT_STALLED:

0 commit comments

Comments
 (0)