]>
git.gir.st - tmk_keyboard.git/blob - key_process.c
3 #include <util/delay.h>
8 #include "jump_bootloader.h"
9 #include "usb_keyboard.h"
10 #include "usb_mouse.h"
11 #include "usb_extra.h"
12 #include "usb_keycodes.h"
15 #include "matrix_skel.h"
16 #include "keymap_skel.h"
17 #include "controller.h"
18 #include "key_process.h"
21 #define MOUSE_MOVE_UNIT 10
22 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
24 #ifndef MOUSE_DELAY_TIME
25 # define MOUSE_DELAY_TIME 255
27 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
31 void proc_matrix(void) {
32 static int mouse_repeat
= 0;
34 bool modified
= false;
35 uint8_t mouse_btn
= 0;
38 int8_t mouse_vwheel
= 0;
39 int8_t mouse_hwheel
= 0;
43 modified
= matrix_is_modified();
46 if (debug_matrix
) matrix_print();
48 // LED flash for debug
54 if (matrix_has_ghost()) {
56 debug("matrix has ghost!!\n");
60 usb_keyboard_swap_report();
61 usb_keyboard_clear_report();
62 for (int row
= 0; row
< matrix_rows(); row
++) {
63 for (int col
= 0; col
< matrix_cols(); col
++) {
64 if (!matrix_is_on(row
, col
)) continue;
67 uint8_t code
= layer_get_keycode(row
, col
);
70 } else if (IS_MOD(code
)) {
71 usb_keyboard_mods
|= MOD_BIT(code
);
72 } else if (IS_FN(code
)) {
73 fn_bits
|= FN_BIT(code
);
74 } else if (IS_MOUSE(code
)) {
75 if (code
== MS_UP
) mouse_y
-= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
76 if (code
== MS_DOWN
) mouse_y
+= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
77 if (code
== MS_LEFT
) mouse_x
-= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
78 if (code
== MS_RGHT
) mouse_x
+= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
79 if (code
== MS_BTN1
) mouse_btn
|= BIT_BTN1
;
80 if (code
== MS_BTN2
) mouse_btn
|= BIT_BTN2
;
81 if (code
== MS_BTN3
) mouse_btn
|= BIT_BTN3
;
82 if (code
== MS_BTN4
) mouse_btn
|= BIT_BTN4
;
83 if (code
== MS_BTN5
) mouse_btn
|= BIT_BTN5
;
84 if (code
== MS_WH_U
) mouse_vwheel
+= 1;
85 if (code
== MS_WH_D
) mouse_vwheel
-= 1;
86 if (code
== MS_WH_L
) mouse_hwheel
-= 1;
87 if (code
== MS_WH_R
) mouse_hwheel
+= 1;
90 // audio control & system control
91 else if (code
== KB_MUTE
) {
92 usb_extra_audio_send(AUDIO_MUTE
);
93 usb_extra_audio_send(0);
95 } else if (code
== KB_VOLU
) {
96 usb_extra_audio_send(AUDIO_VOL_UP
);
97 usb_extra_audio_send(0);
99 } else if (code
== KB_VOLD
) {
100 usb_extra_audio_send(AUDIO_VOL_DOWN
);
101 usb_extra_audio_send(0);
103 } else if (code
== KB_PWR
) {
104 if (suspend
&& remote_wakeup
) {
107 usb_extra_system_send(SYSTEM_POWER_DOWN
);
114 // TODO: fix ugly code
117 for (; i
< KEYBOARD_REPORT_MAX
; i
++) {
118 if (usb_keyboard_keys_prev
[i
] == code
) {
119 usb_keyboard_keys
[i
] = code
;
121 } else if (empty
== -1 && usb_keyboard_keys_prev
[i
] == 0 && usb_keyboard_keys
[i
] == 0) {
125 if (i
== KEYBOARD_REPORT_MAX
) {
127 usb_keyboard_keys
[empty
] = code
;
136 // LED flash for debug
142 layer_switching(fn_bits
);
145 // when 4 left modifier keys down
146 if (keymap_is_special_mode(fn_bits
)) {
147 switch (usb_keyboard_keys
[0]) {
150 print("b: jump to bootloader\n");
151 print("d: debug print toggle\n");
152 print("x: matrix debug toggle\n");
153 print("k: keyboard debug toggle\n");
154 print("m: mouse debug toggle\n");
155 print("p: print enable toggle\n");
156 print("v: print version\n");
157 print("t: print timer count\n");
158 print("r: print registers\n");
159 print("ESC: power down/wake up\n");
161 print_enable
= false;
163 case KB_B
: // bootloader
164 usb_keyboard_clear_report();
167 print("jump to bootloader...\n");
169 jump_bootloader(); // not return
171 case KB_D
: // debug all toggle
172 usb_keyboard_clear_report();
174 debug_enable
= !debug_enable
;
177 print("debug enabled.\n");
179 debug_keyboard
= true;
182 print("debug disabled.\n");
183 print_enable
= false;
184 debug_matrix
= false;
185 debug_keyboard
= false;
190 case KB_X
: // debug matrix toggle
191 usb_keyboard_clear_report();
193 debug_matrix
= !debug_matrix
;
195 print("debug matrix enabled.\n");
197 print("debug matrix disabled.\n");
200 case KB_K
: // debug keyboard toggle
201 usb_keyboard_clear_report();
203 debug_keyboard
= !debug_keyboard
;
205 print("debug keyboard enabled.\n");
207 print("debug keyboard disabled.\n");
210 case KB_M
: // debug mouse toggle
211 usb_keyboard_clear_report();
213 debug_mouse
= !debug_mouse
;
215 print("debug mouse enabled.\n");
217 print("debug mouse disabled.\n");
220 case KB_V
: // print version & information
221 usb_keyboard_clear_report();
224 print(STR(DESCRIPTION
) "\n");
227 case KB_T
: // print timer
228 usb_keyboard_clear_report();
231 print("timer: "); phex16(timer_count
); print("\n");
234 case KB_P
: // print toggle
235 usb_keyboard_clear_report();
238 print("print disabled.\n");
239 print_enable
= false;
242 print("print enabled.\n");
247 usb_keyboard_clear_report();
249 print("UDIEN: "); phex(UDIEN
); print("\n");
250 print("UDINT: "); phex(UDINT
); print("\n");
254 usb_keyboard_clear_report();
256 if (suspend
&& remote_wakeup
) {
259 usb_extra_system_send(SYSTEM_POWER_DOWN
);
267 // send mouse packet to host
268 if (mouse_x
|| mouse_y
|| mouse_vwheel
|| mouse_hwheel
|| mouse_btn
!= mouse_buttons
) {
269 mouse_buttons
= mouse_btn
;
270 if (mouse_x
&& mouse_y
)
271 usb_mouse_move(mouse_x
*0.7, mouse_y
*0.7, mouse_vwheel
, mouse_hwheel
);
273 usb_mouse_move(mouse_x
, mouse_y
, mouse_vwheel
, mouse_hwheel
);
274 usb_mouse_print(mouse_x
, mouse_y
, mouse_vwheel
, mouse_hwheel
);
277 _delay_ms(MOUSE_DELAY_MS
);
284 // send key packet to host