# VICE keyboard mapping file
#
# A Keyboard map is read in as patch to the current map.
#
# File format:
# - comment lines start with '#'
# - keyword lines start with '!keyword'
# - normal lines have 'keysym/scancode row column shiftflag'
#
# Keywords and their lines are:
# '!CLEAR'               clear whole table
# '!INCLUDE filename'    read file as mapping file
# '!LSHIFT row col'      left shift keyboard row/column
# '!RSHIFT row col'      right shift keyboard row/column
# '!VSHIFT shiftkey'     virtual shift key (RSHIFT or LSHIFT)
# '!SHIFTL shiftkey'     shift lock key (RSHIFT or LSHIFT)
#  for emulated keyboards that have only one shift key, set both LSHIFT
#  and RSHIFT to the same row/col and use RSHIFT for VSHIFT and SHIFTL.
# '!LCTRL row col'       left control keyboard row/column
# '!VCTRL ctrlkey'       virtual control key (LCTRL)
# '!LCBM row col'        left CBM keyboard row/column
# '!VCBM cbmkey'         virtual CBM key (LCBM)
# '!UNDEF keysym'        remove keysym from table
#
# Shiftflag can have these values, flags can be ORed to combine them:
# 0x0000      0  key is not shifted for this keysym/scancode
# 0x0001      1  key is combined with shift for this keysym/scancode
# 0x0002      2  key is left shift on emulated machine
# 0x0004      4  key is right shift on emulated machine (use only this one
#                for emulated keyboards that have only one shift key)
# 0x0008      8  key can be shifted or not with this keysym/scancode
# 0x0010     16  deshift key for this keysym/scancode
# 0x0020     32  another definition for this keysym/scancode follows
# 0x0040     64  key is shift-lock on emulated machine
# 0x0080    128  shift modifier required on host
# 0x0100    256  key is used for an alternative keyboard mapping, e.g. C64 mode 
#                in x128
# 0x0200    512  alt-r (alt-gr) modifier required on host
# 0x0400   1024  ctrl modifier required on host
# 0x0800   2048  key is combined with cbm for this keysym/scancode
# 0x1000   4096  key is combined with ctrl for this keysym/scancode
# 0x2000   8192  key is (left) cbm on emulated machine
# 0x4000  16384  key is (left) ctrl on emulated machine
#
# Negative row values:
# 'keysym -1 n' joystick keymap A, direction n
# 'keysym -2 n' joystick keymap B, direction n
# 'keysym -3 0' first RESTORE key
# 'keysym -3 1' second RESTORE key
# 'keysym -4 0' 40/80 column key (x128)
# 'keysym -4 1' CAPS (ASCII/CC) key (x128)
# 'keysym -5 n' joyport keypad, key n (not supported in x128)
#
# Joystick direction values:
# 0      Fire
# 1      South/West
# 2      South
# 3      South/East
# 4      West
# 5      East
# 6      North/West
# 7      North
# 8      North/East
#
# Joyport keypad key layout:
# --------------------------
# |  0 |  1 |  2 |  3 |  4 |
# --------------------------
# |  5 |  6 |  7 |  8 |  9 |
# --------------------------
# | 10 | 11 | 12 | 13 | 14 |
# --------------------------
# | 15 | 16 | 17 | 18 | 19 |
# --------------------------
#
# When a bigger spaced key is used,
# it uses the upper left most key value.

# Symbolic Mapping, Swedish Layout, C128, GTK

# C128 Keyboard matrix
#
#       +-----+-----+-----+-----+-----+-----+-----+-----+
#       |Bit 0|Bit 1|Bit 2|Bit 3|Bit 4|Bit 5|Bit 6|Bit 7|
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 0| DEL |Retrn|C_L/R|  F7 |  F1 |  F3 |  F5 |C_U/D|
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 1| 3 # |  W  |  A  | 4 $ |  Z  |  S  |  E  | S_L |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 2| 5 % |  R  |  D  | 6 & |  C  |  F  |  T  |  X  |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 3| 7 ' |  Y  |  G  | 8 ( |  B  |  H  |  U  |  V  |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 4| 9 ) |  I  |  J  |  0  |  M  |  K  |  O  |  N  |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 5|  +  |  P  |  L  |  -  | . > | : [ |  @  | , < |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 6|POUND|  *  | ; ] | HOME| S_R |  =  | A_UP| / ? |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 7| 1 ! |A_LFT| CTRL| 2 " |SPACE|  C= |  Q  | R/S |
# +-----+-----+-----+-----+-----+-----+-----+-----+-----+
#
#        +-----+-----+-----+-----+-----+-----+-----+-----+
#        |Bit 0|Bit 1|Bit 2|Bit 3|Bit 4|Bit 5|Bit 6|Bit 7|
# +------+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 8 |HELP | KP 8| KP 5| TAB | KP 2| KP 4| KP 7| KP 1|
# +------+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 9 | ESC | KP +| KP -| LF  |KP CR| KP 6| KP 9| KP 3|
# +------+-----+-----+-----+-----+-----+-----+-----+-----+
# |Bit 10| ALT | KP 0| KP .|A.UP |A.DWN|A.LFT|A.RGT|NOSCR|
# +------+-----+-----+-----+-----+-----+-----+-----+-----+

# C128 Keyboard layout:
#
# ESC TAB ALT CAPS | HLP L/F 40/80 NOSCRL | up dwn lft rght | F1/2 F3/4 F5/6 F7/8      
#
#  arrow 1!  2"  3#  4$  5%  6&  7'  8(  9)  0   +   -   pound clr del  7 8 9 +  
#   ctrl   q   w   e   r   t   y   u   i   o   p   @   *   ^   restore  4 5 6 -   
#  r/s SL    a   s   d   f   g   h   j   k   l   :[  ;]  =   return     1 2 3 en 
#  CBM LSHFT   z   x   c   v   b   n   m   ,<  .>  /?  RSHFT   u/d l/r   0 .  ter  
#                               SPACE

!CLEAR
!LSHIFT 1 7
!RSHIFT 6 4
!VSHIFT RSHIFT
!SHIFTL LSHIFT
!LCBM 7 5
!VCBM LCBM
!LCTRL 7 2
!VCTRL LCTRL

# Emulated keys that can't be mapped symbolically are:
# (Functionrow)
#   ESC (-> F9)
#   TAB ( -> F10)
#   ALT (-> F11 NOTE: can't be mapped to real Alt or it would break the 
#                     menus/hotkeys)
#   CAPS LOCK ( -> F12 soft toggle)
#   HELP (-> end)
#   LINE FEED (-> Page Down)
#   40/80 Display (-> keypad divide soft toggle)
#   NO SCROLL (-> keypad multiply)
# (row0)
#   arrow left (-> underscore)
#   pound ( -> backslash)
# (row1)
#   arrow up (-> tilde ~ AND circumflex accent ^)
#   restore (-> page up)
# (row2)
#   run/stop (-> escape)
# (row3)
#   commodore (-> tab)

# Emulated keys that are mapped more than once:
# (row0)
#   apostrophe (-> apostrophe [under " ] AND grave [under ~ ] )
#   INST/DEL (-> Backspace AND Delete AND Insert)
# (row1)
#   arrow up (-> tilde ~ AND circumflex accent ^ [shift-6 on US KB])


#####################
# |Bit 0| DEL | Return | C_L/R | F7 | F1 | F3 | F5 | C_U/D |

# inst/del
BackSpace      0 0 8
# extra: delete -> ins/del
Delete         0 0 8
# extra: insert -> shift+ins/del
Insert         0 0 1
# return
Return         0 1 8
# cursor r/l
# alternative set (c64 mode)
Right          0 2 0x108
Left           0 2 0x101
# f7
F7             0 3 8
F8             0 3 1
# f1
F1             0 4 8
F2             0 4 1
# f3
F3             0 5 8
F4             0 5 1
# f4
F5             0 6 8
F6             0 6 1
# cursor u/d
# alternative set (c64 mode)
Up             0 7 0x101
Down           0 7 0x108

#####################
# |Bit 1| 3 # | W | A | 4 $ | Z | S | E | S_L |

numbersign      1 0 1
dollar          1 3 1
EuroSign 	    1 3 1   # Mac: € -> $
currency 	    1 3 1   # PC:  ¤ -> $
Shift_L         1 7 2
# shift lock
Caps_Lock       1 7 64

#####################
# |Bit 2| 5 % | R | D | 6 & | C | F | T | X |

percent         2 0 1
ampersand       2 3 1

#####################
# |Bit 3| 7 ' | Y | G | 8 ( | B | H | U | V |

# Note the duplicate mapping of both ' (shift =) and ` (with ~ on us keyboards on top left)
apostrophe      3 0 1
grave           3 0 1
parenleft       3 3 1

#####################
# |Bit 4| 9 ) | I | J | 0 | M | K | O | N |

parenright      4 0 1

#####################
# |Bit 5| + | P | L | - | . > | : [ | @ | , < |

plus            5 0 16
minus           5 3 0
period          5 4 0
greater         5 4 1
colon           5 5 16
bracketleft     5 5 1
# extra: ö -> colon
odiaeresis      5 5 0
# extra: Ö -> :+shift
Odiaeresis      5 5 1
at              5 6 16
# extra: å -> @
aring           5 6 8
# extra: Å -> @+shift
Aring           5 6 1
comma           5 7 0
less            5 7 1

#####################
# |Bit 6| POUND | * | ; ] | HOME | S_R | = | A_UP | / ? |

sterling        6 0 0
asterisk        6 1 16
semicolon       6 2 16
bracketright    6 2 1
# extra: ä -> ;
adiaeresis      6 2 8
# extra: Ä -> ;+shift
Adiaeresis      6 2 1
Home            6 3 8
Shift_R         6 4 4
equal           6 5 0
# Shift-6 on us keyboard > A_UP
asciicircum     6 6 0
asciitilde      6 6 0
# Page down -> Arrow Up
#Page_Down      6 6 8
slash           6 7 16
question        6 7 1

#####################
# |Bit 7| 1 ! |A_LFT| CTRL| 2 " |SPACE|  C= |  Q  | R/S |

exclam          7 0 1
# underscore -> arrow left
underscore      7 1 0
Control_L       7 2 16384 /* Control_L = CTRL */
quotedbl        7 3 1
space           7 4 8
Tab             7 5 8192  /* Tab = CBM */
ISO_Left_Tab    7 5 8192  /* Shift + Tab = CBM */
Escape          7 7 8

#####################
# Numbers, letters

0 4 3 8
1 7 0 8
2 7 3 8
3 1 0 8
4 1 3 8
5 2 0 8
6 2 3 8
7 3 0 8
8 3 3 8
9 4 0 8

A 1 2 1
B 3 4 1
C 2 4 1
D 2 2 1
E 1 6 1
F 2 5 1
G 3 2 1
H 3 5 1
I 4 1 1
J 4 2 1
K 4 5 1
L 5 2 1
M 4 4 1
N 4 7 1
O 4 6 1
P 5 1 1
Q 7 6 1
R 2 1 1
S 1 5 1
T 2 6 1
U 3 6 1
V 3 7 1
W 1 1 1
X 2 7 1
Y 3 1 1
Z 1 4 1

a 1 2 8
b 3 4 8
c 2 4 8
d 2 2 8
e 1 6 8
f 2 5 8
g 3 2 8
h 3 5 8
i 4 1 8
j 4 2 8
k 4 5 8
l 5 2 8
m 4 4 8
n 4 7 8
o 4 6 8
p 5 1 8
q 7 6 8
r 2 1 8
s 1 5 8
t 2 6 8
u 3 6 8
v 3 7 8
w 1 1 8
x 2 7 8
y 3 1 8
z 1 4 8

# Numeric keypad

KP_0           10 1 8
KP_1            8 7 8
KP_2            8 4 8
KP_3            9 7 8
KP_4            8 5 8
KP_5            8 2 8
KP_6            9 5 8
KP_7            8 6 8
KP_8            8 1 8
KP_9            9 6 8

# If you got here because you think the keypad enter mapping is wrongly
# pressing normal keyboard return key in the emulated machine, then you
# have hit an upstream GTK3 Windows bug.
# The mapping below is correct, GTK3 behaviour on Windows is wrong.
# See https://vice-emu.pokefinder.org/index.php/Upstream_bugs

KP_Enter        9 4 8
KP_Subtract     9 2 8
KP_Separator   10 2 8
KP_Decimal     10 2 8
KP_Add          9 1 8

# escape - tab - alt - (caps lock)
# help - linefeed - (40/80) - no scroll

# note: its really hard to map all these keys reasonable without sacrificing
#       the symbolic mapping of f1-f7 keys. the above keys are mapped to
#       first block:   f9, f10, f11, f12
#       second block: end, page down, kp /, kp *

# escape
F9              9 0 8
# tab
F10             8 3 8
# alt
F11            10 0 8
# CAPS (ASCII/CC) key
F12            -4 1

# help
End             8 0 8
# lf
Page_Down       9 3 8
# 40/80 column key
KP_Divide      -4 0
# no scroll
KP_Multiply    10 7 8


# duplicate the cursor keys

Up             10 3 8
Left           10 5 8
Right          10 6 8
Down           10 4 8

# Restore key mapping

Page_Up        -3 0
Prior          -3 1

# Dead keys support
dead_acute          3 0 1  # '
dead_grave          3 0 1  # '
dead_circumflex     6 6 16 # A_UP
dead_tilde          6 6 1  # shift+A_UP
dead_perispomeni    6 6 1  # shift+A_UP
dead_diaeresis      7 3 1  # "