📄 keyboard.c
字号:
/* * linux/drivers/char/keyboard.c * * Written for linux by Johan Myreen as a translation from * the assembly version by Linus (with diacriticals added) * * Some additional features added by Christoph Niemann (ChN), March 1993 * * Loadable keymaps by Risto Kankkunen, May 1993 * * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993 * Added decr/incr_console, dynamic keymaps, Unicode support, * dynamic function/string keys, led setting, Sept 1994 * `Sticky' modifier keys, 951006. * * 11-11-96: SAK should now work in the raw mode (Martin Mares) * * Modified to provide 'generic' keyboard support by Hamish Macdonald * Merge with the m68k keyboard driver and split-off of the PC low-level * parts by Geert Uytterhoeven, May 1997 * * 27-05-97: Added support for the Magic SysRq Key (Martin Mares) * 30-07-98: Dead keys redone, aeb@cwi.nl. * * 04-04-1998: Added keyboard autorepeat support (some keyboards don't * autorepeat, and some keyboard changers interfere with keyboard * autorepeat settings). - Russell King (rmk@arm.linux.org.uk) */#include <linux/config.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/mm.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/random.h>#include <linux/init.h>//#include "keyb_ps2.c"//hhtech//#include <linux/keyboard.h>#include <asm/keyboard.h>#include <asm/bitops.h>#include <linux/kbd_kern.h>#include <linux/kbd_diacr.h>#include <linux/vt_kern.h>#include <linux/kbd_ll.h>#include <linux/sysrq.h>#include <linux/pm.h>#define SIZE(x) (sizeof(x)/sizeof((x)[0]))#ifndef KBD_DEFMODE#define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))#endif#ifndef KBD_DEFLEDS/* * Some laptops take the 789uiojklm,. keys as number pad when NumLock * is on. This seems a good reason to start with NumLock off. */#define KBD_DEFLEDS 0#endif#ifndef KBD_DEFLOCK#define KBD_DEFLOCK 0#endif/* * Default autorepeat settings. * DEFAULT_REPEAT_TIMEOUT is the timeout from the keypress to the first repeat * DEFAULT_REPEAT_INTERVAL is the timeout between successive repeats */#define DEFAULT_REPEAT_TIMEOUT HZ*300/1000#define DEFAULT_REPEAT_INTERVAL HZ*30/1000#define hhtech_tty ttyextern void console_callback(void *ignored);void (*kbd_ledfunc)(unsigned int led);EXPORT_SYMBOL(handle_scancode);EXPORT_SYMBOL(kbd_ledfunc);extern void ctrl_alt_del(void);//extern struct tty_struct *hhtech_tty;DECLARE_WAIT_QUEUE_HEAD(keypress_wait);struct console;int keyboard_wait_for_keypress(struct console *co){ sleep_on(&keypress_wait); return 0;}/* * global state includes the following, and various static variables * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next. * (last_console is now a global variable) *//* shift state counters.. */static unsigned char k_down[NR_SHIFT];/* keyboard key bitmap */static unsigned long key_down[256/BITS_PER_LONG]={0,0,0,0,0,0,0,0};static int dead_key_next;/* * In order to retrieve the shift_state (for the mouse server), either * the variable must be global, or a new procedure must be created to * return the value. I chose the former way. */int shift_state;static int npadch = -1; /* -1 or number assembled on pad */static unsigned char diacr;static char rep; /* flag telling character repeat */static int kbd_repeatkeycode= -1;static int kbd_repeattimeout = DEFAULT_REPEAT_TIMEOUT;static int kbd_repeatinterval= DEFAULT_REPEAT_INTERVAL;struct kbd_struct kbd_table[MAX_NR_CONSOLES];static struct tty_struct **ttytab;static struct kbd_struct * kbd = kbd_table;static struct tty_struct * tty;static void kbd_processkeycode(unsigned char scancode, char up_flag, int autorepeat);void compute_shiftstate(void);typedef void (*k_hand)(unsigned char value, char up_flag);typedef void (k_handfn)(unsigned char value, char up_flag);static k_handfn do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift, do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2, do_ignore;static k_hand key_handler[16] = { do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift, do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2, do_ignore, do_ignore};/* Key types processed even in raw modes */#define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))typedef void (*void_fnp)(void);typedef void (void_fn)(void);static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle, num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose, SAK, decr_console, incr_console, spawn_console, bare_num;static void_fnp spec_fn_table[] = { do_null, enter, show_ptregs, show_mem, show_state, send_intr, lastcons, caps_toggle, num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose, SAK, decr_console, incr_console, spawn_console, bare_num};#define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))/* maximum values each key_handler can handle */const int max_vals[] = { 255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1, NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1, 255, NR_LOCK - 1, 255};const int NR_TYPES = SIZE(max_vals);/* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */void put_queue(int);static unsigned char handle_diacr(unsigned char);/* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */struct pt_regs * kbd_pt_regs;#ifdef CONFIG_MAGIC_SYSRQstatic int sysrq_pressed;#endifstatic struct pm_dev *pm_kbd;static int mytest_and_set_bit(unsigned char nr,unsigned long *key_down){ int i,j; cli(); i=nr/32; j=nr%32; key_down[i] |=1<<j; sti(); return 0;}static int mytest_and_clear_bit(unsigned char nr,unsigned long *key_down){ int i,j; sti(); i=nr/32; j=nr%32; key_down[i] &=~(1<<j); cli(); return 1;}/* * Many other routines do put_queue, but I think either * they produce ASCII, or they produce some user-assigned * string, and in both cases we might assume that it is * in utf-8 already. */void to_utf8(ushort c){// printk("^^^^^^^to_utf8\n"); if (c < 0x80) put_queue(c); /* 0******* */ else if (c < 0x800) { put_queue(0xc0 | (c >> 6)); /* 110***** 10****** */ put_queue(0x80 | (c & 0x3f)); } else { put_queue(0xe0 | (c >> 12)); /* 1110**** 10****** 10****** */ put_queue(0x80 | ((c >> 6) & 0x3f)); put_queue(0x80 | (c & 0x3f)); } /* UTF-8 is defined for words of up to 31 bits, but we need only 16 bits here */}/* * Translation of escaped scancodes to keycodes. * This is now user-settable (for machines were it makes sense). *///int setkeycode(unsigned int scancode, unsigned int keycode)//{// return kbd_setkeycode(scancode, keycode);//}//int getkeycode(unsigned int scancode)//{// return kbd_getkeycode(scancode);//}static void key_callback(unsigned long nr);static struct timer_list key_autorepeat_timer ={ function: key_callback};void handle_scancode(unsigned char scancode, int down){ unsigned char keycode; char up_flag = down ? 0 : 0200; char raw_mode;// printk("BITS_PER_LONG=%d\n",BITS_PER_LONG); // return;// printk("\n");// printk("^^handle scancode\n"); pm_access(pm_kbd); add_keyboard_randomness(scancode | up_flag);// tty = ttytab? ttytab[fg_console]: NULL; if (hhtech_tty && (!hhtech_tty->driver_data)) { /* * We touch the tty structure via the ttytab array * without knowing whether or not tty is open, which * is inherently dangerous. We currently rely on that * fact that console_open sets tty->driver_data when * it opens it, and clears it when it closes it. */ tty = NULL; } kbd = kbd_table + fg_console; if ((raw_mode = (kbd->kbdmode == VC_RAW))) { printk("#######\n"); put_queue(scancode | up_flag); /* we do not return yet, because we want to maintain the key_down array, so that we have the correct values when finishing RAW mode or when changing VT's */ } /* * Convert scancode to keycode */ if (!kbd_translate(scancode, &keycode, raw_mode)) return; //goto out; /* * At this point the variable `keycode' contains the keycode. * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid). * We keep track of the up/down status of the key, and * return the keycode if in MEDIUMRAW mode. */ kbd_processkeycode(keycode, up_flag, 0);}static voidkbd_processkeycode(unsigned char keycode, char up_flag, int autorepeat){ char raw_mode = (kbd->kbdmode == VC_RAW); int i; //HHTECH //if(up_flag){ // printk("%x\n",up_flag); // return; //} #if 0 printk("BITS_PER_LONG=%d\n",BITS_PER_LONG); for(i=0;i<256/BITS_PER_LONG;i++) { key_down[i]=0; } printk("1--the bits are: \n"); for(i=0;i<256/BITS_PER_LONG;i++) { printk("%lx ",key_down[i]); } printk("\n"); mytest_and_set_bit(0x29, key_down); printk("2--the bits are: \n"); for(i=0;i<256/BITS_PER_LONG;i++) { printk("%lx ",key_down[i]); } printk("\n"); mytest_and_set_bit(0x1c, key_down); printk("3--the bits are: \n"); for(i=0;i<256/BITS_PER_LONG;i++) { printk("%lx ",key_down[i]); } printk("\n"); mytest_and_clear_bit(0x29, key_down); printk("4--the bits are: \n"); for(i=0;i<256/BITS_PER_LONG;i++) { printk("%lx ",key_down[i]); } printk("\n"); mytest_and_clear_bit(0x1c, key_down); printk("4--the bits are: \n"); for(i=0;i<256/BITS_PER_LONG;i++) { printk("%lx ",key_down[i]); } printk("\n"); return;#endif // printk("###1111,%d\n",up_flag); if (up_flag) { rep = 0; if(!mytest_and_clear_bit(keycode, key_down)) { up_flag = 0;//kbd_unexpected_up(keycode); printk("-->0\n"); } } else { rep = mytest_and_set_bit(keycode, key_down); /* If the keyboard autorepeated for us, ignore it. * We do our own autorepeat processing. */// printk("rep=%d\n",rep); //if (rep && !autorepeat) // return; } if (kbd_repeatkeycode == keycode || !up_flag || raw_mode) { kbd_repeatkeycode = -1; del_timer(&key_autorepeat_timer); } do_poke_blanked_console = 1; //tasklet_schedule(&console_tasklet);#ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */ if (keycode == SYSRQ_KEY) { sysrq_pressed = !up_flag; goto out; } else if (sysrq_pressed) { if (!up_flag) { handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty); goto out; } }#endif /* * Calculate the next time when we have to do some autorepeat * processing. Note that we do not do autorepeat processing * while in raw mode but we do do autorepeat processing in * medium raw mode. */ if (!up_flag && !raw_mode) { kbd_repeatkeycode = keycode; if (vc_kbd_mode(kbd, VC_REPEAT)) { if (rep) key_autorepeat_timer.expires = jiffies + kbd_repeatinterval; else key_autorepeat_timer.expires = jiffies + kbd_repeattimeout; add_timer(&key_autorepeat_timer); } } if (kbd->kbdmode == VC_MEDIUMRAW) { /* soon keycodes will require more than one byte */ put_queue(keycode + up_flag); raw_mode = 1; /* Most key classes will be ignored */ } /* * Small change in philosophy: earlier we defined repetition by * rep = keycode == prev_keycode; * prev_keycode = keycode; * but now by the fact that the depressed key was down already. * Does this ever make a difference? Yes. */ /* * Repeat a key only if the input buffers are empty or the * characters get echoed locally. This makes key repeat usable * with slow applications and under heavy loads. */ if (1/*!rep || (vc_kbd_mode(kbd,VC_REPEAT) && tty && (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))*/) { u_short keysym; u_char type; /* the XOR below used to be an OR */ int shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate; ushort *key_map = key_maps[shift_final]; if (key_map != NULL) { keysym = key_map[keycode]; type = KTYP(keysym); if (type >= 0xf0) { type -= 0xf0; if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type))) goto out; if (type == KT_LETTER) { type = KT_LATIN; if (vc_kbd_led(kbd, VC_CAPSLOCK)) { key_map = key_maps[shift_final ^ (1<<KG_SHIFT)]; if (key_map) keysym = key_map[keycode]; } }// printk("2::\n"); (*key_handler[type])(keysym & 0xff, up_flag); if (type != KT_SLOCK) kbd->slockstate = 0; } else { /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */ if (!up_flag && !raw_mode) to_utf8(keysym); } } else { /* maybe beep? */ /* we have at least to update shift_state */#if 1 /* how? two almost equivalent choices follow */ compute_shiftstate(); kbd->slockstate = 0; /* play it safe */#else keysym = U(plain_map[keycode]); type = KTYP(keysym); if (type == KT_SHIFT) printk("3::\n"); (*key_handler[type])(keysym & 0xff, up_flag);#endif } }out: // printk("4::--\n"); do_poke_blanked_console = 1;// schedule_console_callback();}void put_queue(int ch){ // printk("*****\n"); printk("%c",ch); wake_up(&keypress_wait); if (hhtech_tty) { tty_insert_flip_char(hhtech_tty, ch, 0); con_schedule_flip(hhtech_tty); }}static void puts_queue(char *cp){ wake_up(&keypress_wait); if (!hhtech_tty) return; while (*cp) { tty_insert_flip_char(hhtech_tty, *cp, 0); cp++; } con_schedule_flip(hhtech_tty);}static void applkey(int key, char mode){ static char buf[] = { 0x1b, 'O', 0x00, 0x00 }; buf[1] = (mode ? 'O' : '['); buf[2] = key; puts_queue(buf);}static void enter(void){// printk("^^^^^^^enter\n"); if (diacr) { put_queue(diacr); diacr = 0; } put_queue(13); if (vc_kbd_mode(kbd,VC_CRLF)) put_queue(10);}static void caps_toggle(void){ if (rep) return; chg_vc_kbd_led(kbd, VC_CAPSLOCK);}static void caps_on(void){ if (rep) return; set_vc_kbd_led(kbd, VC_CAPSLOCK);}static void show_ptregs(void){ if (kbd_pt_regs) show_regs(kbd_pt_regs);}static void hold(void){ if (rep || !tty) return; /* * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty); * these routines are also activated by ^S/^Q. * (And SCROLLOCK can also be set by the ioctl KDSKBLED.) */ if (tty->stopped) start_tty(tty); else stop_tty(tty);}static void num(void){ if (vc_kbd_mode(kbd,VC_APPLIC)) applkey('P', 1); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -