#!/usr/bin/python3

#
# Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or (at your option)
# version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# Parses keysymdef.h to construct a unicode symbol -> keysym mapping table.
#
# The lines that are parsed look like this:
# #define XK_Aogonek 0x01a1  /* U+0104 LATIN CAPITAL LETTER A WITH OGONEK */
#
# This would create a 0x0104 -> 0x01a1 mapping.
#

import sys
import re
import collections

cols = 8

if len(sys.argv) >= 2:
    keysymdef = sys.argv[1]
else:
    keysymdef = "/usr/include/X11/keysymdef.h"

keysymMap = {}

with open(keysymdef, "r") as fid:
    for line in fid:
        match = re.search(r'0x([0-9a-fA-F]+)\s+/\* U\+([0-9a-fA-F]+)', line)
        if match:
            keysym = int(match.group(1), 16)
            unicodeVal = int(match.group(2), 16)

            # ignore 1:1 mappings
            if 0x0020 <= keysym <= 0x007e or 0x00a0 <= keysym <= 0x00ff:
                continue
            # ignore unicode | 0x01000000 mappings
            elif keysym >= 0x1000000:
                continue

            keysymMap[unicodeVal] = keysym

keysymMap = collections.OrderedDict(sorted(keysymMap.items(), key=lambda t: t[0]))

print("""/*
 *  Automatically generated by keysymmap.py from parsing keysymdef.h.
 */
""")

print("const int unicodeToKeysymLen = {0};".format(len(keysymMap)))

print()

print("const uint unicodeToKeysymKeys[] = {")
keys = keysymMap.keys()
keyLen = len(keys)
for idx, val in enumerate(keys, start=1):
    hexVal = "{0:#0{1}x}".format(val, 6)

    if idx == keyLen:
        print(hexVal)
    elif (idx % cols) == 0:
        print(hexVal + ",")
    elif ((idx - 1) % cols) == 0:
        print("    " + hexVal + ", ", end="")
    else:
        print(hexVal + ", ", end="")
print("};")

print()

print("const uint unicodeToKeysymValues[] = {")
values = keysymMap.values()
valuesLen = len(values)
for idx, val in enumerate(values, start=1):
    hexVal = "{0:#0{1}x}".format(val, 6)

    if idx == valuesLen:
        print(hexVal)
    elif (idx % cols) == 0:
        print(hexVal + ",")
    elif ((idx - 1) % cols) == 0:
        print("    " + hexVal + ", ", end="")
    else:
        print(hexVal + ", ", end="")
print("};")
