]> git.gir.st - tmk_keyboard.git/blob - common/keyboard.c
Merge branch 'newdir'
[tmk_keyboard.git] / common / keyboard.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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 #include "keyboard.h"
18 #include "host.h"
19 #include "layer.h"
20 #include "matrix.h"
21 #include "led.h"
22 #include "usb_keycodes.h"
23 #include "timer.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "command.h"
27 #ifdef MOUSEKEY_ENABLE
28 #include "mousekey.h"
29 #endif
30 #ifdef EXTRAKEY_ENABLE
31 #include <util/delay.h>
32 #endif
33
34
35 static uint8_t last_leds = 0;
36
37
38 void keyboard_init(void)
39 {
40 timer_init();
41 matrix_init();
42 #ifdef PS2_MOUSE_ENABLE
43 ps2_mouse_init();
44 #endif
45 }
46
47 void keyboard_proc(void)
48 {
49 uint8_t fn_bits = 0;
50 #ifdef EXTRAKEY_ENABLE
51 uint16_t consumer_code = 0;
52 #endif
53
54 matrix_scan();
55
56 if (matrix_is_modified()) {
57 if (debug_matrix) matrix_print();
58 #ifdef DEBUG_LED
59 // LED flash for debug
60 DEBUG_LED_CONFIG;
61 DEBUG_LED_ON;
62 #endif
63 }
64
65 if (matrix_has_ghost()) {
66 // should send error?
67 debug("matrix has ghost!!\n");
68 return;
69 }
70
71 host_swap_keyboard_report();
72 host_clear_keyboard_report();
73 for (int row = 0; row < matrix_rows(); row++) {
74 for (int col = 0; col < matrix_cols(); col++) {
75 if (!matrix_is_on(row, col)) continue;
76
77 uint8_t code = layer_get_keycode(row, col);
78 if (code == KB_NO) {
79 // do nothing
80 } else if (IS_MOD(code)) {
81 host_add_mod_bit(MOD_BIT(code));
82 } else if (IS_FN(code)) {
83 fn_bits |= FN_BIT(code);
84 }
85 // TODO: use table or something
86 #ifdef EXTRAKEY_ENABLE
87 // System Control
88 else if (code == KB_SYSTEM_POWER) {
89 #ifdef HOST_PJRC
90 if (suspend && remote_wakeup) {
91 usb_remote_wakeup();
92 } else {
93 host_system_send(SYSTEM_POWER_DOWN);
94 }
95 #else
96 host_system_send(SYSTEM_POWER_DOWN);
97 #endif
98 host_system_send(0);
99 _delay_ms(500);
100 } else if (code == KB_SYSTEM_SLEEP) {
101 host_system_send(SYSTEM_SLEEP);
102 host_system_send(0);
103 _delay_ms(500);
104 } else if (code == KB_SYSTEM_WAKE) {
105 host_system_send(SYSTEM_WAKE_UP);
106 host_system_send(0);
107 _delay_ms(500);
108 }
109 // Consumer Page
110 else if (code == KB_AUDIO_MUTE) {
111 consumer_code = AUDIO_MUTE;
112 } else if (code == KB_AUDIO_VOL_UP) {
113 consumer_code = AUDIO_VOL_UP;
114 } else if (code == KB_AUDIO_VOL_DOWN) {
115 consumer_code = AUDIO_VOL_DOWN;
116 }
117 else if (code == KB_MEDIA_NEXT_TRACK) {
118 consumer_code = TRANSPORT_NEXT_TRACK;
119 } else if (code == KB_MEDIA_PREV_TRACK) {
120 consumer_code = TRANSPORT_PREV_TRACK;
121 } else if (code == KB_MEDIA_STOP) {
122 consumer_code = TRANSPORT_STOP;
123 } else if (code == KB_MEDIA_PLAY_PAUSE) {
124 consumer_code = TRANSPORT_PLAY_PAUSE;
125 } else if (code == KB_MEDIA_SELECT) {
126 consumer_code = AL_CC_CONFIG;
127 }
128 else if (code == KB_MAIL) {
129 consumer_code = AL_EMAIL;
130 } else if (code == KB_CALCULATOR) {
131 consumer_code = AL_CALCULATOR;
132 } else if (code == KB_MY_COMPUTER) {
133 consumer_code = AL_LOCAL_BROWSER;
134 }
135 else if (code == KB_WWW_SEARCH) {
136 consumer_code = AC_SEARCH;
137 } else if (code == KB_WWW_HOME) {
138 consumer_code = AC_HOME;
139 } else if (code == KB_WWW_BACK) {
140 consumer_code = AC_BACK;
141 } else if (code == KB_WWW_FORWARD) {
142 consumer_code = AC_FORWARD;
143 } else if (code == KB_WWW_STOP) {
144 consumer_code = AC_STOP;
145 } else if (code == KB_WWW_REFRESH) {
146 consumer_code = AC_REFRESH;
147 } else if (code == KB_WWW_FAVORITES) {
148 consumer_code = AC_BOOKMARKS;
149 }
150 #endif
151 else if (IS_KEY(code)) {
152 host_add_key(code);
153 }
154 #ifdef MOUSEKEY_ENABLE
155 else if (IS_MOUSEKEY(code)) {
156 mousekey_decode(code);
157 }
158 #endif
159 else {
160 debug("ignore keycode: "); debug_hex(code); debug("\n");
161 }
162 }
163 }
164
165 layer_switching(fn_bits);
166
167 if (command_proc()) {
168 return;
169 }
170
171 // TODO: should send only when changed from last report
172 if (matrix_is_modified()) {
173 host_send_keyboard_report();
174 #ifdef EXTRAKEY_ENABLE
175 host_consumer_send(consumer_code);
176 #endif
177 #ifdef DEBUG_LED
178 // LED flash for debug
179 DEBUG_LED_CONFIG;
180 DEBUG_LED_OFF;
181 #endif
182 }
183
184 #ifdef MOUSEKEY_ENABLE
185 mousekey_send();
186 #endif
187
188 #ifdef PS2_MOUSE_ENABLE
189 // TODO: should comform new API
190 if (ps2_mouse_read() == 0)
191 ps2_mouse_usb_send();
192 #endif
193
194 if (last_leds != host_keyboard_leds()) {
195 keyboard_set_leds(host_keyboard_leds());
196 last_leds = host_keyboard_leds();
197 }
198 }
199
200 void keyboard_set_leds(uint8_t leds)
201 {
202 led_set(leds);
203 }
Imprint / Impressum