From e529258ef36ee3573a463cebb4e8775866e1732b Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Fri, 21 Jul 2023 17:12:43 +0200 Subject: [PATCH 1/3] Improve scripts/makeheader Simplify evdev and XK_ substitution and improve alignment. Also, perform some additional XK_ substitutions in comments. --- scripts/makeheader | 50 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/scripts/makeheader b/scripts/makeheader index bb0db591e..d725e496f 100755 --- a/scripts/makeheader +++ b/scripts/makeheader @@ -3,9 +3,41 @@ from __future__ import print_function import re import os -# expected format: -# #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */ -evdev_pattern = re.compile(r'^#define\s+XF86XK_(?P\w+)\s+_EVDEVK\((?P0x[0-9A-Fa-f]+)\)') +# Expected format: +# #define XF86XK_FooBar 0x1234 /* some optional comment */ +# or: +# #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */ +# We also need to match commented evdev entries: +# /* Use: XF86XK_FooBar _EVDEVK(0x123) some optional comment */ +keysym_entry_pattern = re.compile( + r'''^ + (?P\#define|/\*\s+Use:)\s+ + (?P\w*)XK_(?P\w+)(?P\s+) + (?P_EVDEVK\()?(?P0x[0-9A-Fa-f]+)(?(evdev)\)) + ''', + re.VERBOSE, +) +# Match remaining XK_ references +xorgproto_keysym_prefix_pattern = re.compile(r'\b(?P\w*)XK_(?!KOREAN\b)') + + +def make_keysym_entry(m: re.Match[str]) -> str: + ''' + Perform the substitutions + ''' + if m.group('evdev'): + if m.group('define').startswith('#'): + # Replace the xorgproto _EVDEVK macro with the actual value + # 0x10081000 is the base, the evdev hex code is added to that. + # We replace to make parsing of the keys later easier. + value = 0x10081000 + int(m.group('value'), 16) + value_str = f'{value:#x} ' + else: + value_str = f'''_EVDEVK({m.group('value')})''' + else: + value_str = m.group('value') + return f'''{m.group('define')} XKB_KEY_{m.group('prefix') or ''}{m.group('name')}{m.group('spacing')}{value_str}''' + prefix = os.environ.get('X11_HEADERS_PREFIX', '/usr') HEADERS = [ @@ -43,15 +75,9 @@ for path in HEADERS: if 'XK_Ydiaeresis' in line and '0x100000ee' in line: continue - # Replace the xorgproto _EVDEVK macro with the actual value - # 0x10081000 is the base, the evdev hex code is added to that. - # We replace to make parsing of the keys later easier. - match = re.match(evdev_pattern, line) - if match: - value = 0x10081000 + int(match.group('value'), 16) - line = re.sub(r'_EVDEVK\(0x([0-9A-Fa-f]+)\)', '{:#x}'.format(value), line) - - line = re.sub(r'#define\s*(\w*)XK_', r'#define XKB_KEY_\1', line) + # Perform _EVDEV and XK_ substitutions + line = keysym_entry_pattern.sub(make_keysym_entry, line) + line = xorgproto_keysym_prefix_pattern.sub(r'XKB_KEY_\1', line) print(line, end='') print('\n\n#endif') From 1004da2b246883fb8cef272fe176726fc3f93c3f Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Fri, 21 Jul 2023 17:25:23 +0200 Subject: [PATCH 2/3] scripts/makeheader: format with black --- scripts/makeheader | 54 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/scripts/makeheader b/scripts/makeheader index d725e496f..57bd77c0a 100755 --- a/scripts/makeheader +++ b/scripts/makeheader @@ -10,45 +10,46 @@ import os # We also need to match commented evdev entries: # /* Use: XF86XK_FooBar _EVDEVK(0x123) some optional comment */ keysym_entry_pattern = re.compile( - r'''^ + r"""^ (?P\#define|/\*\s+Use:)\s+ (?P\w*)XK_(?P\w+)(?P\s+) (?P_EVDEVK\()?(?P0x[0-9A-Fa-f]+)(?(evdev)\)) - ''', + """, re.VERBOSE, ) # Match remaining XK_ references -xorgproto_keysym_prefix_pattern = re.compile(r'\b(?P\w*)XK_(?!KOREAN\b)') +xorgproto_keysym_prefix_pattern = re.compile(r"\b(?P\w*)XK_(?!KOREAN\b)") def make_keysym_entry(m: re.Match[str]) -> str: - ''' + """ Perform the substitutions - ''' - if m.group('evdev'): - if m.group('define').startswith('#'): + """ + if m.group("evdev"): + if m.group("define").startswith("#"): # Replace the xorgproto _EVDEVK macro with the actual value # 0x10081000 is the base, the evdev hex code is added to that. # We replace to make parsing of the keys later easier. - value = 0x10081000 + int(m.group('value'), 16) - value_str = f'{value:#x} ' + value = 0x10081000 + int(m.group("value"), 16) + value_str = f"{value:#x} " else: - value_str = f'''_EVDEVK({m.group('value')})''' + value_str = f"""_EVDEVK({m.group('value')})""" else: - value_str = m.group('value') - return f'''{m.group('define')} XKB_KEY_{m.group('prefix') or ''}{m.group('name')}{m.group('spacing')}{value_str}''' + value_str = m.group("value") + return f"""{m.group('define')} XKB_KEY_{m.group('prefix') or ''}{m.group('name')}{m.group('spacing')}{value_str}""" -prefix = os.environ.get('X11_HEADERS_PREFIX', '/usr') +prefix = os.environ.get("X11_HEADERS_PREFIX", "/usr") HEADERS = [ - prefix + '/include/X11/keysymdef.h', - prefix + '/include/X11/XF86keysym.h', - prefix + '/include/X11/Sunkeysym.h', - prefix + '/include/X11/DECkeysym.h', - prefix + '/include/X11/HPkeysym.h', + prefix + "/include/X11/keysymdef.h", + prefix + "/include/X11/XF86keysym.h", + prefix + "/include/X11/Sunkeysym.h", + prefix + "/include/X11/DECkeysym.h", + prefix + "/include/X11/HPkeysym.h", ] -print('''#ifndef _XKBCOMMON_KEYSYMS_H +print( + """#ifndef _XKBCOMMON_KEYSYMS_H #define _XKBCOMMON_KEYSYMS_H /* This file is autogenerated; please do not commit directly. */ @@ -59,25 +60,26 @@ print('''#ifndef _XKBCOMMON_KEYSYMS_H */ #define XKB_KEY_NoSymbol 0x000000 /* Special KeySym */ -''') +""" +) for path in HEADERS: with open(path) as header: for line in header: - if '#ifdef' in line or '#ifndef' in line or '#endif' in line: + if "#ifdef" in line or "#ifndef" in line or "#endif" in line: continue # Remove #define _OSF_Keysyms and such. - if '#define _' in line: + if "#define _" in line: continue # Handle a duplicate definition in HPkeysyms.h which kicks in if # it's not already defined. - if 'XK_Ydiaeresis' in line and '0x100000ee' in line: + if "XK_Ydiaeresis" in line and "0x100000ee" in line: continue # Perform _EVDEV and XK_ substitutions line = keysym_entry_pattern.sub(make_keysym_entry, line) - line = xorgproto_keysym_prefix_pattern.sub(r'XKB_KEY_\1', line) + line = xorgproto_keysym_prefix_pattern.sub(r"XKB_KEY_\1", line) - print(line, end='') -print('\n\n#endif') + print(line, end="") +print("\n\n#endif") From 4038ef1b17831962d005e36caa9ce98e56236959 Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Wed, 6 Sep 2023 08:55:08 +0200 Subject: [PATCH 3/3] Review fixes --- scripts/makeheader | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/makeheader b/scripts/makeheader index 57bd77c0a..f2738bd6a 100755 --- a/scripts/makeheader +++ b/scripts/makeheader @@ -17,7 +17,13 @@ keysym_entry_pattern = re.compile( """, re.VERBOSE, ) -# Match remaining XK_ references + +# Match remaining XK_ references in the comments, e.g we will replace: +# XF86XK_CamelCaseKernelName _EVDEVK(kernel value) +# #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XK_Multi_key */ +# with: +# XKB_KEY_XF86CamelCaseKernelName _EVDEVK(kernel value) +# #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XKB_KEY_Multi_key */ xorgproto_keysym_prefix_pattern = re.compile(r"\b(?P\w*)XK_(?!KOREAN\b)") @@ -27,7 +33,7 @@ def make_keysym_entry(m: re.Match[str]) -> str: """ if m.group("evdev"): if m.group("define").startswith("#"): - # Replace the xorgproto _EVDEVK macro with the actual value + # Replace the xorgproto _EVDEVK macro with the actual value: # 0x10081000 is the base, the evdev hex code is added to that. # We replace to make parsing of the keys later easier. value = 0x10081000 + int(m.group("value"), 16) @@ -36,7 +42,11 @@ def make_keysym_entry(m: re.Match[str]) -> str: value_str = f"""_EVDEVK({m.group('value')})""" else: value_str = m.group("value") - return f"""{m.group('define')} XKB_KEY_{m.group('prefix') or ''}{m.group('name')}{m.group('spacing')}{value_str}""" + define = m.group("define") + prefix = m.group("prefix") or "" + name = m.group("name") + spacing = m.group("spacing") + return f"""{define} XKB_KEY_{prefix}{name}{spacing}{value_str}""" prefix = os.environ.get("X11_HEADERS_PREFIX", "/usr")