]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/keyboard.c
Merge branch 'lufa'
[tmk_firmware.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     uint16_t system_code = 0;
53 #endif
54
55     matrix_scan();
56
57     if (matrix_is_modified()) {
58         if (debug_matrix) matrix_print();
59 #ifdef DEBUG_LED
60         // LED flash for debug
61         DEBUG_LED_CONFIG;
62         DEBUG_LED_ON;
63 #endif
64     }
65
66     if (matrix_has_ghost()) {
67         // should send error?
68         debug("matrix has ghost!!\n");
69         return;
70     }
71
72     host_swap_keyboard_report();
73     host_clear_keyboard_report();
74     for (int row = 0; row < matrix_rows(); row++) {
75         for (int col = 0; col < matrix_cols(); col++) {
76             if (!matrix_is_on(row, col)) continue;
77
78             uint8_t code = layer_get_keycode(row, col);
79             if (code == KB_NO) {
80                 // do nothing
81             } else if (IS_MOD(code)) {
82                 host_add_mod_bit(MOD_BIT(code));
83             } else if (IS_FN(code)) {
84                 fn_bits |= FN_BIT(code);
85             }
86 // TODO: use table or something
87 #ifdef EXTRAKEY_ENABLE
88             // System Control
89             else if (code == KB_SYSTEM_POWER) {
90 #ifdef HOST_PJRC
91                 if (suspend && remote_wakeup) {
92                     usb_remote_wakeup();
93                 }
94 #endif
95                 system_code = SYSTEM_POWER_DOWN;
96             } else if (code == KB_SYSTEM_SLEEP) {
97                 system_code = SYSTEM_SLEEP;
98             } else if (code == KB_SYSTEM_WAKE) {
99                 system_code = SYSTEM_WAKE_UP;
100             }
101             // Consumer Page
102             else if (code == KB_AUDIO_MUTE) {
103                 consumer_code = AUDIO_MUTE;
104             } else if (code == KB_AUDIO_VOL_UP) {
105                 consumer_code = AUDIO_VOL_UP;
106             } else if (code == KB_AUDIO_VOL_DOWN) {
107                 consumer_code = AUDIO_VOL_DOWN;
108             }
109             else if (code == KB_MEDIA_NEXT_TRACK) {
110                 consumer_code = TRANSPORT_NEXT_TRACK;
111             } else if (code == KB_MEDIA_PREV_TRACK) {
112                 consumer_code = TRANSPORT_PREV_TRACK;
113             } else if (code == KB_MEDIA_STOP) {
114                 consumer_code = TRANSPORT_STOP;
115             } else if (code == KB_MEDIA_PLAY_PAUSE) {
116                 consumer_code = TRANSPORT_PLAY_PAUSE;
117             } else if (code == KB_MEDIA_SELECT) {
118                 consumer_code = AL_CC_CONFIG;
119             }
120             else if (code == KB_MAIL) {
121                 consumer_code = AL_EMAIL;
122             } else if (code == KB_CALCULATOR) {
123                 consumer_code = AL_CALCULATOR;
124             } else if (code == KB_MY_COMPUTER) {
125                 consumer_code = AL_LOCAL_BROWSER;
126             }
127             else if (code == KB_WWW_SEARCH) {
128                 consumer_code = AC_SEARCH;
129             } else if (code == KB_WWW_HOME) {
130                 consumer_code = AC_HOME;
131             } else if (code == KB_WWW_BACK) {
132                 consumer_code = AC_BACK;
133             } else if (code == KB_WWW_FORWARD) {
134                 consumer_code = AC_FORWARD;
135             } else if (code == KB_WWW_STOP) {
136                 consumer_code = AC_STOP;
137             } else if (code == KB_WWW_REFRESH) {
138                 consumer_code = AC_REFRESH;
139             } else if (code == KB_WWW_FAVORITES) {
140                 consumer_code = AC_BOOKMARKS;
141             }
142 #endif
143             else if (IS_KEY(code)) {
144                 host_add_key(code);
145             }
146 #ifdef MOUSEKEY_ENABLE
147             else if (IS_MOUSEKEY(code)) {
148                 mousekey_decode(code);
149             }
150 #endif
151             else {
152                 debug("ignore keycode: "); debug_hex(code); debug("\n");
153             }
154         }
155     }
156
157     layer_switching(fn_bits);
158
159     if (command_proc()) {
160         return;
161     }
162
163     // TODO: should send only when changed from last report
164     if (matrix_is_modified()) {
165         host_send_keyboard_report();
166 #ifdef EXTRAKEY_ENABLE
167         host_consumer_send(consumer_code);
168         host_system_send(system_code);
169 #endif
170 #ifdef DEBUG_LED
171         // LED flash for debug
172         DEBUG_LED_CONFIG;
173         DEBUG_LED_OFF;
174 #endif
175     }
176
177 #ifdef MOUSEKEY_ENABLE
178     mousekey_send();
179 #endif
180
181 #ifdef PS2_MOUSE_ENABLE
182     // TODO: should comform new API
183     if (ps2_mouse_read() == 0)
184         ps2_mouse_usb_send();
185 #endif
186
187     if (last_leds != host_keyboard_leds()) {
188         keyboard_set_leds(host_keyboard_leds());
189         last_leds = host_keyboard_leds();
190     }
191 }
192
193 void keyboard_set_leds(uint8_t leds)
194 {
195     led_set(leds);
196 }