]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard.c
added copyright and license notice.
[max/tmk_keyboard.git] / 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 USB_EXTRA_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 USB_EXTRA_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 #ifdef USB_EXTRA_ENABLE
86             // System Control
87             else if (code == KB_SYSTEM_POWER) {
88 #ifdef HOST_PJRC
89                 if (suspend && remote_wakeup) {
90                     usb_remote_wakeup();
91                 } else {
92                     host_system_send(SYSTEM_POWER_DOWN);
93                 }
94 #else
95                 host_system_send(SYSTEM_POWER_DOWN);
96 #endif
97                 host_system_send(0);
98                 _delay_ms(500);
99             } else if (code == KB_SYSTEM_SLEEP) {
100                 host_system_send(SYSTEM_SLEEP);
101                 host_system_send(0);
102                 _delay_ms(500);
103             } else if (code == KB_SYSTEM_WAKE) {
104                 host_system_send(SYSTEM_WAKE_UP);
105                 host_system_send(0);
106                 _delay_ms(500);
107             }
108             // Consumer Page
109             else if (code == KB_AUDIO_MUTE) {
110                 consumer_code = AUDIO_MUTE;
111             } else if (code == KB_AUDIO_VOL_UP) {
112                 consumer_code = AUDIO_VOL_UP;
113             } else if (code == KB_AUDIO_VOL_DOWN) {
114                 consumer_code = AUDIO_VOL_DOWN;
115             }
116             else if (code == KB_MEDIA_NEXT_TRACK) {
117                 consumer_code = TRANSPORT_NEXT_TRACK;
118             } else if (code == KB_MEDIA_PREV_TRACK) {
119                 consumer_code = TRANSPORT_PREV_TRACK;
120             } else if (code == KB_MEDIA_STOP) {
121                 consumer_code = TRANSPORT_STOP;
122             } else if (code == KB_MEDIA_PLAY_PAUSE) {
123                 consumer_code = TRANSPORT_PLAY_PAUSE;
124             } else if (code == KB_MEDIA_SELECT) {
125                 consumer_code = AL_CC_CONFIG;
126             }
127             else if (code == KB_MAIL) {
128                 consumer_code = AL_EMAIL;
129             } else if (code == KB_CALCULATOR) {
130                 consumer_code = AL_CALCULATOR;
131             } else if (code == KB_MY_COMPUTER) {
132                 consumer_code = AL_LOCAL_BROWSER;
133             }
134             else if (code == KB_WWW_SEARCH) {
135                 consumer_code = AC_SEARCH;
136             } else if (code == KB_WWW_HOME) {
137                 consumer_code = AC_HOME;
138             } else if (code == KB_WWW_BACK) {
139                 consumer_code = AC_BACK;
140             } else if (code == KB_WWW_FORWARD) {
141                 consumer_code = AC_FORWARD;
142             } else if (code == KB_WWW_STOP) {
143                 consumer_code = AC_STOP;
144             } else if (code == KB_WWW_REFRESH) {
145                 consumer_code = AC_REFRESH;
146             } else if (code == KB_WWW_FAVORITES) {
147                 consumer_code = AC_BOOKMARKS;
148             }
149 #endif
150             else if (IS_KEY(code)) {
151                 host_add_key(code);
152             }
153 #ifdef MOUSEKEY_ENABLE
154             else if (IS_MOUSEKEY(code)) {
155                 mousekey_decode(code);
156             }
157 #endif
158             else {
159                 debug("ignore keycode: "); debug_hex(code); debug("\n");
160             }
161         }
162     }
163
164     layer_switching(fn_bits);
165
166     if (command_proc()) {
167         return;
168     }
169
170     // TODO: should send only when changed from last report
171     if (matrix_is_modified()) {
172         host_send_keyboard_report();
173 #ifdef USB_EXTRA_ENABLE
174         host_consumer_send(consumer_code);
175 #endif
176 #ifdef DEBUG_LED
177         // LED flash for debug
178         DEBUG_LED_CONFIG;
179         DEBUG_LED_OFF;
180 #endif
181     }
182
183 #ifdef MOUSEKEY_ENABLE
184     mousekey_send();
185 #endif
186
187 #ifdef PS2_MOUSE_ENABLE
188     // TODO: should comform new API
189     if (ps2_mouse_read() == 0)
190         ps2_mouse_usb_send();
191 #endif
192
193     if (last_leds != host_keyboard_leds()) {
194         keyboard_set_leds(host_keyboard_leds());
195         last_leds = host_keyboard_leds();
196     }
197 }
198
199 void keyboard_set_leds(uint8_t leds)
200 {
201     led_set(leds);
202 }