]> git.gir.st - tmk_keyboard.git/blob - key_process.c
new build method for macway
[tmk_keyboard.git] / key_process.c
1 #include <stdbool.h>
2 #include <avr/io.h>
3 #include <util/delay.h>
4 #include "usb_keyboard.h"
5 #include "usb_mouse.h"
6 #include "usb_keycodes.h"
7 #include "print.h"
8 #include "debug.h"
9 #include "util.h"
10 #include "jump_bootloader.h"
11 #include "matrix_skel.h"
12 #include "keymap_skel.h"
13 #include "controller.h"
14
15 #include "key_process.h"
16
17
18 #define MOUSE_MOVE_UNIT 10
19 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
20
21 #ifndef MOUSE_DELAY_TIME
22 # define MOUSE_DELAY_TIME 255
23 #endif
24 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
25
26
27 // TODO: refactoring
28 void proc_matrix(void) {
29 static int mouse_repeat = 0;
30
31 bool modified = false;
32 //bool has_ghost = false;
33 int key_index = 0;
34 uint8_t mouse_btn = 0;
35 int8_t mouse_x = 0;
36 int8_t mouse_y = 0;
37 int8_t mouse_vwheel = 0;
38 int8_t mouse_hwheel = 0;
39 int fn_bits = 0;
40
41 matrix_scan();
42 modified = matrix_is_modified();
43
44 if (modified) {
45 if (debug_matrix) matrix_print();
46 #ifdef DEBUG_LED
47 // LED flash for debug
48 DEBUG_LED_CONFIG;
49 DEBUG_LED_ON;
50 #endif
51 }
52
53 if (matrix_has_ghost()) {
54 // should send error?
55 debug("matrix has ghost!!\n");
56 return;
57 }
58
59 usb_keyboard_clear();
60 for (int row = 0; row < matrix_rows(); row++) {
61 for (int col = 0; col < matrix_cols(); col++) {
62 if (!matrix_is_on(row, col)) continue;
63
64 uint8_t code = keymap_get_keycode(row, col);
65 if (code == KB_NO) {
66 // do nothing
67 } else if (IS_MOD(code)) {
68 keyboard_modifier_keys |= MOD_BIT(code);
69 } else if (IS_MOUSE(code)) {
70 // mouse
71 if (code == MS_UP)
72 mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
73 if (code == MS_DOWN)
74 mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
75 if (code == MS_LEFT)
76 mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
77 if (code == MS_RIGHT)
78 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_UP) mouse_vwheel += 1;
85 if (code == MS_WH_DOWN) mouse_vwheel -= 1;
86 if (code == MS_WH_LEFT) mouse_hwheel -= 1;
87 if (code == MS_WH_RIGHT) mouse_hwheel += 1;
88 } else if (IS_FN(code)) {
89 fn_bits |= FN_BIT(code);
90 } else {
91 // normal keys
92 if (key_index < 6)
93 keyboard_keys[key_index] = code;
94 key_index++;
95 }
96 }
97 }
98 keymap_fn_proc(fn_bits);
99
100 // when 4 left modifier keys down
101 if (keymap_is_special_mode(fn_bits)) {
102 switch (keyboard_keys[0]) {
103 case KB_B: // bootloader
104 usb_keyboard_clear();
105 usb_keyboard_send();
106 print_enable = true;
107 print("jump to bootloader...\n");
108 _delay_ms(1000);
109 jump_bootloader(); // not return
110 break;
111 case KB_D: // debug all toggle
112 usb_keyboard_clear();
113 usb_keyboard_send();
114 debug_enable = !debug_enable;
115 if (debug_enable) {
116 print("debug enabled.\n");
117 print_enable = true;
118 debug_matrix = true;
119 debug_keyboard = true;
120 debug_mouse = true;
121 } else {
122 print("debug disabled.\n");
123 print_enable = false;
124 debug_matrix = false;
125 debug_keyboard = false;
126 debug_mouse = false;
127 }
128 _delay_ms(1000);
129 break;
130 case KB_X: // debug matrix toggle
131 usb_keyboard_clear();
132 usb_keyboard_send();
133 debug_matrix = !debug_matrix;
134 if (debug_matrix)
135 print("debug matrix enabled.\n");
136 else
137 print("debug matrix disabled.\n");
138 _delay_ms(1000);
139 break;
140 case KB_K: // debug keyboard toggle
141 usb_keyboard_clear();
142 usb_keyboard_send();
143 debug_keyboard = !debug_keyboard;
144 if (debug_keyboard)
145 print("debug keyboard enabled.\n");
146 else
147 print("debug keyboard disabled.\n");
148 _delay_ms(1000);
149 break;
150 case KB_M: // debug mouse toggle
151 usb_keyboard_clear();
152 usb_keyboard_send();
153 debug_mouse = !debug_mouse;
154 if (debug_mouse)
155 print("debug mouse enabled.\n");
156 else
157 print("debug mouse disabled.\n");
158 _delay_ms(1000);
159 break;
160 case KB_V: // print version & information
161 usb_keyboard_clear();
162 usb_keyboard_send();
163 print(STR(DESCRIPTION) "\n");
164 _delay_ms(1000);
165 break;
166 }
167 }
168
169
170 // send mouse packet to host
171 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
172 mouse_buttons = mouse_btn;
173 if (mouse_x && mouse_y)
174 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
175 else
176 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
177 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
178
179 // acceleration
180 _delay_ms(MOUSE_DELAY_MS);
181 mouse_repeat++;
182 } else {
183 mouse_repeat = 0;
184 }
185
186
187 // send key packet to host
188 if (modified) {
189 if (key_index > 6) {
190 //Rollover
191 }
192 usb_keyboard_send();
193 usb_keyboard_print();
194 #ifdef DEBUG_LED
195 // LED flash for debug
196 DEBUG_LED_CONFIG;
197 DEBUG_LED_OFF;
198 #endif
199 }
200 }
Imprint / Impressum