| 1 |
|
| 2 |
from enum import Enum |
| 3 |
|
| 4 |
HEADER="""#pragma once |
| 5 |
#include <QString> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
// Auto-generated based on "/usr/share/X11/xkb/rules/evdev.lst" |
| 9 |
|
| 10 |
struct layout |
| 11 |
{ |
| 12 |
QString code; |
| 13 |
QString description; |
| 14 |
}; |
| 15 |
|
| 16 |
static std::vector<struct layout> evdev_lst_layouts = {""" |
| 17 |
|
| 18 |
FOOTER="};" |
| 19 |
|
| 20 |
class Layout(): |
| 21 |
def __init__(self, layout, variant, description): |
| 22 |
self.layout=layout |
| 23 |
self.variant=variant |
| 24 |
self.description=description |
| 25 |
def __lt__(self, other): |
| 26 |
return self.description < other.description |
| 27 |
def get_layout(self): |
| 28 |
if not self.variant: |
| 29 |
return self.layout |
| 30 |
return f"{self.layout}({self.variant})" |
| 31 |
def get_description(self): |
| 32 |
return self.description |
| 33 |
|
| 34 |
def generate_code(layouts): |
| 35 |
print(HEADER) |
| 36 |
for layout in layouts: |
| 37 |
print(f' {{ "{layout.get_layout()}", "{layout.get_description()}" }},') |
| 38 |
print(FOOTER) |
| 39 |
|
| 40 |
def main(): |
| 41 |
with open("/usr/share/X11/xkb/rules/evdev.lst", 'r', encoding='UTF-8') as f: |
| 42 |
lines = f.read().split('\n') |
| 43 |
|
| 44 |
section_type=Enum('section_type', 'NONE LAYOUT VARIANT') |
| 45 |
section = section_type.NONE |
| 46 |
|
| 47 |
layouts = [] |
| 48 |
|
| 49 |
for line in lines: |
| 50 |
if line.startswith('!'): |
| 51 |
if line == "! layout": |
| 52 |
section = section_type.LAYOUT |
| 53 |
elif line == "! variant": |
| 54 |
section = section_type.VARIANT |
| 55 |
else: |
| 56 |
section = section_type.NONE |
| 57 |
continue |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if section == section_type.LAYOUT: |
| 70 |
fields = line.strip().split(None, maxsplit=1) |
| 71 |
if not fields: |
| 72 |
continue |
| 73 |
layout = fields[0] |
| 74 |
description = fields[1] |
| 75 |
layouts.append(Layout(layout, None, description)) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if section == section_type.VARIANT: |
| 87 |
fields = line.strip().split(None, maxsplit=1) |
| 88 |
if not fields: |
| 89 |
continue |
| 90 |
variant = fields[0] |
| 91 |
fields = fields[1].strip().split(':', maxsplit=1) |
| 92 |
if not fields: |
| 93 |
continue |
| 94 |
layout = fields[0] |
| 95 |
description = fields[1].strip() |
| 96 |
layouts.append(Layout(layout, variant, description)) |
| 97 |
|
| 98 |
generate_code(sorted(layouts)) |
| 99 |
|
| 100 |
if __name__ == '__main__': |
| 101 |
main() |
| 102 |
|