about summary refs log tree commit diff
path: root/cros-keyboard-map.py
blob: 3f491be830e4f0aba154661b70bc27ead7657ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3

def vivaldi_scancode_to_keyd(scancode):
    match scancode:
        case "90":
            return "previoussong"
        case "91":
            return "zoom"
        case "92":
            return "scale"
        case "93":
            return "print"
        case "94":
            return "brightnessdown"
        case "95":
            return "brightnessup"
        case "97":
            return "kbdillumdown"
        case "98":
            return "kbdillumup"
        case "99":
            return "nextsong"
        case "9A":
            return "playpause"
        case "9B":
            return "micmute"
        case "9E":
            return "kbdillumtoggle"
        case "A0":
            return "mute"
        case "AE":
            return "volumedown"
        case "B0":
            return "volumeup"
        case "E9":
            return "forward"
        case "EA":
            return "back"
        case "E7":
            return "refresh"

def load_physmap_data():
    try:
        with open("/sys/bus/platform/devices/i8042/serio0/function_row_physmap", "r") as file:
            return file.read().strip().split()
    except FileNotFoundError:
        return ""

def create_keyd_config(physmap):
    config = ""
    config += """[ids]
k:0001:0001
k:0000:0000

[main]
"""
    # make fn keys act like vivaldi keys when super isn't held
    i = 0
    for scancode in physmap:
        i += 1
        # Map zoom to f11 since most applications wont listen to zoom
        if vivaldi_scancode_to_keyd(scancode) == "zoom":
            mapping = "f11"
        else:
            mapping = vivaldi_scancode_to_keyd(scancode)
        config += f"f{i} = {mapping}\n"
    config += "\n"
    
    # make vivaldi keys act like vivaldi keys when super isn't held
    for scancode in physmap:
        # Map zoom to f11 since most applications wont listen to zoom
        if vivaldi_scancode_to_keyd(scancode) == "zoom":
            mapping = "f11"
        else:
            mapping = vivaldi_scancode_to_keyd(scancode)
        config += f"{vivaldi_scancode_to_keyd(scancode)} = {mapping}\n"

    # map lock button to coffee
    config += "\nf13=coffee\nsleep=coffee\n"

    # make fn keys act like fn keys when super is held
    i = 0
    config += "\n[meta]\n"
    for scancode in physmap:
        i += 1
        config += f"f{i} = f{i}\n"

    # make vivaldi keys act like like fn keys when super is held
    i = 0
    config += "\n"
    for scancode in physmap:
        i += 1
        config += f"{vivaldi_scancode_to_keyd(scancode)} = f{i}\n"

    # Add various extra shortcuts
    config += """\n[alt]
backspace = delete
brightnessdown = kbdillumdown
brightnessup = kbdillumup
f6 = kbdillumdown
f7 = kbdillumup

[control]
f5 = print
scale = print

[control+alt]
backspace = C-A-delete"""

    return config

def main():
    physmap = load_physmap_data()
    if not physmap:
        print("no function row mapping found, using default mapping")
        physmap = ['EA', 'E9', 'E7', '91', '92', '94', '95', 'A0', 'AE', 'B0']
    
    config = create_keyd_config(physmap)
    with open("cros.conf", "w") as conf:
        conf.write(config)

main()