#   Keyboard.dok, Copyright (c) 1998 by Lukas Ruf,
#   Swiss Federal Institute of Technology,
#   Computer Engineering and Networks Laboratory.
#
#   TOPSY -- A Teachable Operating System.
#            Implementation of a tiny and simple
#            micro kernel for teaching purposes.
#
#   For further information, please visit http://www.tik.ee.ethz.ch/~topsy
#
#   This software is provided under the terms of the GNU General Public Licence.
#   A full copy of the GNU GPL is provided in the file COPYING found in the 
#   development root of Topsy.
#
#       This copyright notice supercedes all originally or previously used 
#       copyrights being used within the source code.
#       
#       Author: Lukas Ruf, lr@lpr.ch
#

 Topsy i386                                            Lukas Ruf, February 1998
 ------------------------------------------------------------------------------
Keyboard Handling:
------------------

Only Keyboards equipped by 102 keys are correctly handled.
(I do not like MS and therefore no 105-keyboard mapping is provided.)

The mapping happing handles only the make codes, i.e. only the scan codes
sent to the handler when a key is pressed down are processed. The break codes
(bit 7 set of make code) are only sensefull for keys ALT CTRL SHIFT. CAPS LOCK
toggles the local system variable "Caps Lock Set".
The mapping (keyboard layout) is !zweilagig!:
The make codes are converted to a unique "Logical Hardware Key Code".
This code is further converted to a code determining the keyboard
layout (Swiss,US).
To convert 102 keys uniquely 7 bit are sufficient: 0x00..0x80. Special keys
like ALT CTRL SHIFT are represented by the three additional bits:
      KEYCODE = ALT CTRL SHIFT <7b Logical Hardware Key Code>

This leads to eight different key ranges:
        0x000..0x07F      : no special key pressed down
        0x080..0x0FF      : SHIFT
        0x100..0x17F      : CTRL
        0x180..0x1FF      : CTRL-SHIFT
        0x200..0x27F      : ALT
        0x280..0x2FF      : ALT-SHIFT
        0x300..0x37F      : ALT-CTRL
        0x380..0x3FF      : ALT-CTRL-SHIFT

To enable different keyboard layouts a second table handles these 2^10 (1024)
valid keycodes. This one dimensional array (position equals to the keycode,
value at position is the ASCII-Code to insert) is build during initializing the
keyboard handler. To simplify the definition of key remapping a special logic
will be used (explained later).

;Topsy i386                                             Lukas Ruf, February 1998
;-------------------------------------------------------------------------------
; MakeKey.TBL
; -----------
; provides the first level mapping from the hardware scan code (some are created
; by the primitive keyboard handler KeyModifier) to the Topsy HW Code.
; This Topsy HW Code is extended by the Keyboard Modifiers (i.e. Alt Ctrl
; Shift (=Caps Lock set, too)) and is named THE TOPSY KEYCODE (=modified Topsy
; HW Code).
; The TOPSY KEYCODE points into a table of 1024Bytes (0..1023). The Byte
; located at that position can either be:
;       0x00    :       discard keypress
;       0x01..0xFE :    ASCII Code to insert into Keyboard Buffer
;       0xFF    :       send 16b Message located in KeyMsg.TBL at appropriate
;                       position
; The Key Modifiers (Alt, Ctrl, Shift, Caps Lock, Num Lock, Scroll Lock) do
; not generate a KeyCode by themselves. The Processing is stopped (if they are
; pressed/released) after KeyModifier().
;-------------------------------------------------------------------------------
; SCAN CODE (Hardware)  Topsy HW Code (7b)      KEY (I tried US Mapping)
; --------------------  ------------------      -----------------
        01              000                     ; ESC
        02              049                     ; 1
        03              050                     ; 2
        04              051                     ; 3
        05              052                     ; 4
        06              053                     ; 5
        07              054                     ; 6
        08              055                     ; 7
        09              056                     ; 8
        10              057                     ; 9
        11              048                     ; 0
        12              021                     ; - {'}
        13              022                     ; = {^}
        14              017                     ; Backspace
        15              016                     ; Tab
        16              077                     ; Q
        17              083                     ; W
        18              065                     ; E
        19              078                     ; R
        20              080                     ; T
        21              086                     ; Z
        22              081                     ; U
        23              069                     ; I
        24              075                     ; O
        25              076                     ; P
        26              023                     ; [ {�}
        27              024                     ; ] {�}
        28              013                     ; Enter
        29              044                     ; [Left Ctrl]
        30              061                     ; A
        31              079                     ; S
        32              064                     ; D
        33              066                     ; F
        34              067                     ; G
        35              068                     ; H
        36              070                     ; J
        37              071                     ; K
        38              072                     ; L
        39              025                     ; ; {�}
        40              026                     ; ' {�}
        41              020                     ; ` {�}
        42              042                     ; [Left Shift]
        43              027                     ; # {$}
        44              085                     ; Y
        45              084                     ; X
        46              063                     ; C
        47              082                     ; V
        48              062                     ; B
        49              074                     ; N
        50              073                     ; M
        51              028                     ; , {,}
        52              029                     ; . {.}
        53              030                     ; / {-}
        53              097                     ; /     (NumPad)
        54              043                     ; [Right Shift]
        55              098                     ; *     (NumPad)
        56              046                     ; [Left  Alt]
        57              018                     ; Space
        58              019                     ; [Caps Lock]
        59              001                     ; F1
        60              002                     ; F2
        61              003                     ; F3
        62              004                     ; F4
        63              005                     ; F5
        64              006                     ; F6
        65              007                     ; F7
        66              008                     ; F8
        67              009                     ; F9
        68              010                     ; F10
        69              058                     ; [Num Lock]
        70              014                     ; Scroll Lock
        71              094                     ; 7     (NumPad)
        72              095                     ; 8     (NumPad)
        73              096                     ; 9     (NumPad)
        74              099                     ; -     (NumPad)
        75              091                     ; 4     (NumPad)
        76              092                     ; 5     (NumPad)
        77              093                     ; 6     (NumPad)
        78              100                     ; +     (NumPad)
        79              088                     ; 1     (NumPad)
        80              089                     ; 2     (NumPad)
        81              090                     ; 3     (NumPad)
        82              087                     ; 0     (NumPad)
        83              101                     ; .     (NumPad)
        84              059                     ; (not implemented)
        85              015                     ; (not implemented)
        86              031                     ; \ {<}
        87              011                     ; F11
        88              012                     ; F12
;       89      is not used !! there is space to include further extensions :-)
        90              015                     ; BREAK  (self made MAKE CODE)
        91              060                     ; PRTSCR (self made MAKE CODE)
        92              045                     ; [Right Ctrl]    (Extended Key) (self made MAKE CODE)
        92              102                     ; Enter (NumPad)  (Extended Key) (self made MAKE CODE)
        93              047                     ; [Right Alt]     (Extended Key) (self made MAKE CODE)
        94              034                     ; Home            (Extended Key) (self made MAKE CODE)
        95              038                     ; Arrow Up        (Extended Key) (self made MAKE CODE)
;       96      cannot be used as 0xE0 & 0x7F = 96 Extended Key Signed
;       97      cannot be used as 0xE1 & 0x7F = 97 2nd Extended Key Signed
        98              036                     ; Page Up         (Extended Key) (self made MAKE CODE)
        99              039                     ; Arrow Left      (Extended Key) (self made MAKE CODE)
       100              041                     ; Arrow Right     (Extended Key) (self made MAKE CODE)
       101              035                     ; End             (Extended Key) (self made MAKE CODE)
       102              040                     ; Arrow Down      (Extended Key) (self made MAKE CODE)
       103              037                     ; Page Down       (Extended Key) (self made MAKE CODE)
       104              032                     ; Insert          (Extended Key) (self made MAKE CODE)
       105              033                     ; Delete          (Extended Key) (self made MAKE CODE)
; Original SCAN CODES (translated by KeyModifier())
; -------------------
; Special Keys: Extended Keys and Break/PrtScr
; Please Note: These keys are translated to represent a unique keyboard number...
;       28 E0           102                     ; Enter (NumPad)  (Extended Key)
;       29 E0           045                     ; [Right Ctrl]    (Extended Key)
;       29/69 E1        015                     ; Break           (see special note)
;       55 E0x2         060                     ; Print Screen    (see special note)
;       56 E0           047                     ; [Right Alt]     (Extended Key)
;       71 E0           034                     ; Home            (Extended Key)
;       72 E0           038                     ; Arrow Up        (Extended Key)
;       73 E0           036                     ; Page Up         (Extended Key)
;       75 E0           039                     ; Arrow Left      (Extended Key)
;       77 E0           041                     ; Arrow Right     (Extended Key)
;       79 E0           035                     ; End             (Extended Key)
;       80 E0           040                     ; Arrow Down      (Extended Key)
;       81 E0           037                     ; Page Down       (Extended Key)
;       82 E0           032                     ; Insert          (Extended Key)
;       83 E0           033                     ; Delete          (Extended Key)
; End of MakeKey.TBL (TRANSLATION MAKE CODE TO KEYCODE)

The Logical Hardware Key is combined with the bits of ALT CTRL SHIFT. Layouting
a keyboard handler is simplified in that not mapped keys lead to a character
derived from the previous keycode table. Unmapped keys (never mapped) do
not insert any keycode into the keyboard buffer.

The keyboard layout is defined as follows (ASCII-File)
     { <Keycode>{spaces(#>0)}<Ascii-Value> [CR]<LF>}
Defining keycodes can be done using two different methods:
        - enter the value of the keycode and the value of the ASCII letter.
        - enter abbreviation of alphanumerical values and special keys with
          modificators (ALT CTRL SHIFT)
        - enter literal #UserMsgNo (UserMsgNo is one of the unused MsgCodes)
Example:
        000     27
        001     'F1'
        065     'a'
        013     ENTER
        013     13
        048     '0'
        048     48
        189     'A'
        s-A     'A'     { Shift A }
        s-A     65      { Shift A }
        cs-A    #32779  { Ctrl-Shift A } -> Send Message to actual process
        as-A    #32780  { Alt-Shift A }  -> dito
        acs-A   #32781  { Alt-Ctrl-Shift A }
        c-F1  etc...

Understood literals:
        a-   =  Alt     �
        c-   =  Ctrl    � any combination of them, too
        s-   =  Shift   �
        A..Z
        0..9
        F1..F12
        ENTER
        INSERT
        DELETE
        HOME
        END
        LEFT
        RIGHT
        UP
        DOWN
        PGUP
        PGDN
        SPACE
        BACKSPACE
        TAB
        BREAK
        PRTSCR
        SCROLLOCK
        n0..n9  = NumLock Set.

Up to now (time problem) it is not possible to enter other definitions than
Numerical mapping to  Numerical or single character '?'. See KeybSG.TBL below.

The Translation Tables do not have to be entered in a special order of keycodes.
Neither all keycodes have to be entered. When the keyboard handler is intialized,
*ALL* keycodes translations are set to zero, i.e. no keycodes can be entered.

SPECIAL KEYCODES (cannot be modified/specially used in resulting events):
   046,047  ALT
   044,045  CTRL
   042,043  SHIFT
   019      CAPS LOCK               ( Modify type style )
   058      NUM LOCK                ( Toggle Keypad )
   801,869  CTRL-ALT-DELETE         ( Reboot  )
   319,447  CTRL-C                  ( Terminate running program )
   000      ESC                     ( Cancel action )
   013      ENTER                   ( Insert )

For example, I will include into this document a default keyboard layout
for Swiss German Keyboard Mappings:
(original file name: KeybSG.TBL)

;Topsy i386                                             Lukas Ruf, January 1998
;------------------------------------------------------------------------------
; KEYBSG: Swiss German Keyboard Layout
; First only normal key mappings are implemented.
; KEYCODE (Topsy386)    ASCII CODE      KEY NAME
; ------------------    -----------     --------
        000             027             ; ESC
        001             000             ; F1
        002             000             ; F2
        003             000             ; F3
        004             000             ; F4
        005             000             ; F5
        006             000             ; F6
        007             000             ; F7
        008             000             ; F8
        009             000             ; F9
        010             000             ; F10
        011             000             ; F11
        012             000             ; F12
        013             013             ; Enter
        014             000             ; Scroll Lock
        015             000             ; Break           (see special note)
        016             008             ; Tab
        017             009             ; Backspace
        018             000             ; Space
        019             000             ; [Caps Lock]
        020             '`'             ; ` {�}
        021             039             ; - {'}
        022             '^'             ; = {^}
        023             '�'             ; [ {�}
        024             '�'             ; ] {�}
        025             '�'             ; ; {�}
        026             '�'             ; ' {�}
        027             '$'             ; # {$}
        028             ','             ; , {,}
        029             '.'             ; . {.}
        030             '/'             ; / {-}
        031             '\'             ; \ {<}
        032             000             ; Insert          (Extended Key)
        033             000             ; Delete          (Extended Key)
        034             000             ; Home            (Extended Key)
        035             000             ; End             (Extended Key)
        036             000             ; Page Up         (Extended Key)
        037             000             ; Page Down       (Extended Key)
        038             000             ; Arrow Up        (Extended Key)
        039             000             ; Arrow Left      (Extended Key)
        040             000             ; Arrow Down      (Extended Key)
        041             000             ; Arrow Right     (Extended Key)
        042             000             ; [Left Shift]
        043             000             ; [Right Shift]
        044             000             ; [Left Ctrl]
        045             000             ; [Right Ctrl]    (Extended Key)
        046             000             ; [Left  Alt]
        047             000             ; [Right Alt]     (Extended Key)
        048             '0'             ; 0
        049             '1'             ; 1
        050             '2'             ; 2
        051             '3'             ; 3
        052             '4'             ; 4
        053             '5'             ; 5
        054             '6'             ; 6
        055             '7'             ; 7
        056             '8'             ; 8
        057             '9'             ; 9
        058             000             ; [Num Lock]
        059             000             ; (not implemented)
        060             000             ; Print Screen    (see special note)
        061             'a'             ; A
        062             'b'             ; B
        063             'c'             ; C
        064             'd'             ; D
        065             'e'             ; E
        066             'f'             ; F
        067             'g'             ; G
        068             'h'             ; H
        069             'i'             ; I
        070             'j'             ; J
        071             'k'             ; K
        072             'l'             ; L
        073             'm'             ; M
        074             'n'             ; N
        075             'o'             ; O
        076             'p'             ; P
        077             'q'             ; Q
        078             'r'             ; R
        079             's'             ; S
        080             't'             ; T
        081             'u'             ; U
        082             'v'             ; V
        083             'w'             ; W
        084             'x'             ; X
        085             'y'             ; Y
        086             'z'             ; Z
        087             '0'             ; 0     (NumPad)
        088             '1'             ; 1     (NumPad)
        089             '2'             ; 2     (NumPad)
        090             '3'             ; 3     (NumPad)
        091             '4'             ; 4     (NumPad)
        092             '5'             ; 5     (NumPad)
        093             '6'             ; 6     (NumPad)
        094             '7'             ; 7     (NumPad)
        095             '8'             ; 8     (NumPad)
        096             '9'             ; 9     (NumPad)
        097             '/'             ; /     (NumPad)
        098             '*'             ; *     (NumPad)
        099             '-'             ; -     (NumPad)
        100             '+'             ; +     (NumPad)
        101             '.'             ; .     (NumPad)
        102             013             ; Enter (NumPad) (Extended Key)

; SHIFT HOLD DOWN

        176             '='             ; 0
        177             '+'             ; 1
        178             '"'             ; 2
        179             '*'             ; 3
        180             '�'             ; 4
        181             '%'             ; 5
        182             '&'             ; 6
        183             '/'             ; 7
        184             '('             ; 8
        185             ')'             ; 9

        189             'A'             ; A
        190             'B'             ; B
        191             'C'             ; C
        192             'D'             ; D
        193             'E'             ; E
        194             'F'             ; F
        195             'G'             ; G
        196             'H'             ; H
        197             'I'             ; I
        198             'J'             ; J
        199             'K'             ; K
        200             'L'             ; L
        201             'M'             ; M
        202             'N'             ; N
        203             'O'             ; O
        204             'P'             ; P
        205             'Q'             ; Q
        206             'R'             ; R
        207             'S'             ; S
        208             'T'             ; T
        209             'U'             ; U
        210             'V'             ; V
        211             'W'             ; W
        212             'X'             ; X
        213             'Y'             ; Y
        214             'Z'             ; Z
; End of KeybSG.TBL: Swiss German Keyboard Layout