1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * %sccs.include.noredist.c% 9 * 10 * @(#)kbd.c 7.1 (Berkeley) 04/24/90 11 */ 12 13 #define L 0x01 /* locking function */ 14 #define SHF 0x02 /* keyboard shift */ 15 #define ALT 0x04 /* alternate shift -- alternate chars */ 16 #define NUM 0x08 /* numeric shift cursors vs. numeric */ 17 #define CTL 0x10 /* control shift -- allows ctl function */ 18 #define CPS 0x20 /* caps shift -- swaps case of letter */ 19 #define ASCII 0x40 /* ascii code for this key */ 20 #define STP 0x80 /* stop output */ 21 22 typedef unsigned char u_char; 23 24 u_char inb(); 25 26 u_char action[] = { 27 0, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 0- 7 */ 28 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 8-15 */ 29 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 16-23 */ 30 ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII, /* scan 24-31 */ 31 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 32-39 */ 32 ASCII, ASCII, SHF , ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 40-47 */ 33 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII, /* scan 48-55 */ 34 ALT, ASCII, CPS|L, 0, 0, ASCII, 0, 0, /* scan 56-63 */ 35 0, 0, 0, 0, 0, NUM|L, STP|L, ASCII, /* scan 64-71 */ 36 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 72-79 */ 37 ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0, /* scan 80-87 */ 38 0,0,0,0,0,0,0,0, 39 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 40 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 41 42 u_char unshift[] = { /* no shift */ 43 0, 033 , '1' , '2' , '3' , '4' , '5' , '6' , /* scan 0- 7 */ 44 '7' , '8' , '9' , '0' , '-' , '=' , 0177 ,'\t' , /* scan 8-15 */ 45 46 'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , /* scan 16-23 */ 47 'o' , 'p' , '[' , ']' , '\r' , CTL , 'a' , 's' , /* scan 24-31 */ 48 49 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , ';' , /* scan 32-39 */ 50 '\'' , '`' , SHF , '\\' , 'z' , 'x' , 'c' , 'v' , /* scan 40-47 */ 51 52 'b' , 'n' , 'm' , ',' , '.' , '/' , SHF , '*', /* scan 48-55 */ 53 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 54 55 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */ 56 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 57 58 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 59 0,0,0,0,0,0,0,0, 60 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 61 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 62 63 u_char shift[] = { /* shift shift */ 64 0, 033 , '!' , '@' , '#' , '$' , '%' , '^' , /* scan 0- 7 */ 65 '&' , '*' , '(' , ')' , '_' , '+' , 0177 ,'\t' , /* scan 8-15 */ 66 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , /* scan 16-23 */ 67 'O' , 'P' , '[' , ']' , '\r' , CTL , 'A' , 'S' , /* scan 24-31 */ 68 'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , ':' , /* scan 32-39 */ 69 '"' , '~' , SHF , '|' , 'Z' , 'X' , 'C' , 'V' , /* scan 40-47 */ 70 'B' , 'N' , 'M' , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 71 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 72 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */ 73 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 74 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 75 0,0,0,0,0,0,0,0, 76 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 77 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 78 79 u_char ctl[] = { /* CTL shift */ 80 0, 033 , '!' , 000 , '#' , '$' , '%' , 036 , /* scan 0- 7 */ 81 '&' , '*' , '(' , ')' , 037 , '+' , 034 ,'\177', /* scan 8-15 */ 82 021 , 027 , 005 , 022 , 024 , 031 , 025 , 011 , /* scan 16-23 */ 83 017 , 020 , 033 , 035 , '\r' , CTL , 001 , 013 , /* scan 24-31 */ 84 004 , 006 , 007 , 010 , 012 , 013 , 014 , ';' , /* scan 32-39 */ 85 '\'' , '`' , SHF , 034 , 032 , 030 , 003 , 026 , /* scan 40-47 */ 86 002 , 016 , 015 , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 87 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 88 CPS|L, 0, 0, 0, 0, 0, 0, 0, /* scan 64-71 */ 89 0, 0, 0, 0, 0, 0, 0, 0, /* scan 72-79 */ 90 0, 0, 0, 0, 0, 0, 0, 0, /* scan 80-87 */ 91 0, 0, 033, '7' , '4' , '1' , 0, NUM|L, /* scan 88-95 */ 92 '8' , '5' , '2' , 0, STP|L, '9' , '6' , '3' , /*scan 96-103*/ 93 '.' , 0, '*' , '-' , '+' , 0, 0, 0, /*scan 104-111*/ 94 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 95 96 #ifdef notdef 97 struct key { 98 u_short action; /* how this key functions */ 99 char ascii[8]; /* ascii result character indexed by shifts */ 100 }; 101 #endif 102 103 u_char shfts, ctls, alts, caps, num, stp; 104 105 #define KBSTATP 0x64 /* kbd status port */ 106 #define KBS_INP_BUF_FUL 0x02 /* kbd char ready */ 107 #define KBDATAP 0x60 /* kbd data port */ 108 #define KBSTATUSPORT 0x61 /* kbd status */ 109 110 u_char odt; 111 112 u_char kbd() { 113 u_char dt, brk, act; 114 115 loop: 116 do { 117 while (inb(0x64)&2) ; 118 dt = inb(0x60); 119 } while (dt == odt); 120 odt = dt; 121 122 brk = dt & 0x80 ; dt = dt & 0x7f ; 123 124 act = action[dt]; 125 if (act&SHF) { 126 if(brk) shfts = 0; else shfts = 1; 127 } 128 if (act&ALT) { 129 if(brk) alts = 0; else alts = 1; 130 } 131 if (act&NUM) { 132 if (act&L) { 133 if(!brk) num ^= 1; 134 } else if(brk) num = 0; else num = 1; 135 } 136 if (act&CTL) { 137 if(brk) ctls = 0; else ctls = 1; 138 } 139 if (act&CPS) { 140 if (act&L) { 141 if(!brk) caps ^= 1; 142 } else if(brk) caps = 0; else caps = 1; 143 } 144 if (act&STP) { 145 if (act&L) { 146 if(!brk) stp ^= 1; 147 } else if(brk) stp = 0; else stp = 1; 148 } 149 if ((act&ASCII) && !brk) { 150 u_char chr; 151 152 if (shfts){ 153 chr = shift[dt] ; } else { 154 if (ctls) { 155 chr = ctl[dt] ; } else { 156 chr = unshift[dt] ; } } 157 if (caps && (chr >= 'a' && chr <= 'z')) { 158 chr -= 'a' - 'A' ; 159 } 160 return(chr); 161 } 162 goto loop; 163 } 164