Reduce key dictionary memory footprint
Instead of indexing `Key` objects that have multiple names by each individual name, index by the set of names. This reduces the size of the default key dictionary by a factor of between 2 and 3, and as result also reduces reallocations/defragmentation. Instead of instantiating all module/extension keys by default, append them to the `maybe_key`-generator list.
This commit is contained in:
@@ -1,35 +1,42 @@
|
||||
'''Adds international keys'''
|
||||
from kmk.extensions import Extension
|
||||
from kmk.keys import make_key
|
||||
from kmk.keys import KC, make_key
|
||||
|
||||
|
||||
class International(Extension):
|
||||
'''Adds international keys'''
|
||||
|
||||
def __init__(self):
|
||||
# International
|
||||
make_key(code=50, names=('NONUS_HASH', 'NUHS'))
|
||||
make_key(code=100, names=('NONUS_BSLASH', 'NUBS'))
|
||||
make_key(code=101, names=('APP', 'APPLICATION', 'SEL', 'WINMENU'))
|
||||
KC._generators.append(self.maybe_make_media_key)
|
||||
|
||||
make_key(code=135, names=('INT1', 'RO'))
|
||||
make_key(code=136, names=('INT2', 'KANA'))
|
||||
make_key(code=137, names=('INT3', 'JYEN'))
|
||||
make_key(code=138, names=('INT4', 'HENK'))
|
||||
make_key(code=139, names=('INT5', 'MHEN'))
|
||||
make_key(code=140, names=('INT6',))
|
||||
make_key(code=141, names=('INT7',))
|
||||
make_key(code=142, names=('INT8',))
|
||||
make_key(code=143, names=('INT9',))
|
||||
make_key(code=144, names=('LANG1', 'HAEN'))
|
||||
make_key(code=145, names=('LANG2', 'HAEJ'))
|
||||
make_key(code=146, names=('LANG3',))
|
||||
make_key(code=147, names=('LANG4',))
|
||||
make_key(code=148, names=('LANG5',))
|
||||
make_key(code=149, names=('LANG6',))
|
||||
make_key(code=150, names=('LANG7',))
|
||||
make_key(code=151, names=('LANG8',))
|
||||
make_key(code=152, names=('LANG9',))
|
||||
@staticmethod
|
||||
def maybe_make_media_key(candidate):
|
||||
codes = (
|
||||
(50, ('NONUS_HASH', 'NUHS')),
|
||||
(100, ('NONUS_BSLASH', 'NUBS')),
|
||||
(101, ('APP', 'APPLICATION', 'SEL', 'WINMENU')),
|
||||
(135, ('INT1', 'RO')),
|
||||
(136, ('INT2', 'KANA')),
|
||||
(137, ('INT3', 'JYEN')),
|
||||
(138, ('INT4', 'HENK')),
|
||||
(139, ('INT5', 'MHEN')),
|
||||
(140, ('INT6',)),
|
||||
(141, ('INT7',)),
|
||||
(142, ('INT8',)),
|
||||
(143, ('INT9',)),
|
||||
(144, ('LANG1', 'HAEN')),
|
||||
(145, ('LANG2', 'HAEJ')),
|
||||
(146, ('LANG3',)),
|
||||
(147, ('LANG4',)),
|
||||
(148, ('LANG5',)),
|
||||
(149, ('LANG6',)),
|
||||
(150, ('LANG7',)),
|
||||
(151, ('LANG8',)),
|
||||
(152, ('LANG9',)),
|
||||
)
|
||||
for code, names in codes:
|
||||
if candidate in names:
|
||||
return make_key(code=code, names=names)
|
||||
|
||||
def on_runtime_enable(self, sandbox):
|
||||
return
|
||||
|
@@ -2,7 +2,8 @@ import pwmio
|
||||
from math import e, exp, pi, sin
|
||||
|
||||
from kmk.extensions import Extension, InvalidExtensionEnvironment
|
||||
from kmk.keys import make_argumented_key, make_key
|
||||
from kmk.handlers.stock import passthrough as handler_passthrough
|
||||
from kmk.keys import KC, make_argumented_key, make_key
|
||||
from kmk.utils import clamp
|
||||
|
||||
|
||||
@@ -61,35 +62,51 @@ class LED(Extension):
|
||||
if user_animation is not None:
|
||||
self.user_animation = user_animation
|
||||
|
||||
make_argumented_key(
|
||||
names=('LED_TOG',),
|
||||
validator=self._led_key_validator,
|
||||
on_press=self._key_led_tog,
|
||||
KC._generators.append(self.maybe_make_led_key())
|
||||
|
||||
def maybe_make_led_key(self):
|
||||
argumented_keys = (
|
||||
(
|
||||
('LED_TOG',),
|
||||
self._key_led_tog,
|
||||
),
|
||||
(
|
||||
('LED_INC',),
|
||||
self._key_led_inc,
|
||||
),
|
||||
(
|
||||
('LED_DEC',),
|
||||
self._key_led_dec,
|
||||
),
|
||||
(
|
||||
('LED_SET',),
|
||||
self._key_led_set,
|
||||
),
|
||||
)
|
||||
make_argumented_key(
|
||||
names=('LED_INC',),
|
||||
validator=self._led_key_validator,
|
||||
on_press=self._key_led_inc,
|
||||
)
|
||||
make_argumented_key(
|
||||
names=('LED_DEC',),
|
||||
validator=self._led_key_validator,
|
||||
on_press=self._key_led_dec,
|
||||
)
|
||||
make_argumented_key(
|
||||
names=('LED_SET',),
|
||||
validator=self._led_set_key_validator,
|
||||
on_press=self._key_led_set,
|
||||
)
|
||||
make_key(names=('LED_ANI',), on_press=self._key_led_ani)
|
||||
make_key(names=('LED_AND',), on_press=self._key_led_and)
|
||||
make_key(
|
||||
names=('LED_MODE_PLAIN', 'LED_M_P'), on_press=self._key_led_mode_static
|
||||
)
|
||||
make_key(
|
||||
names=('LED_MODE_BREATHE', 'LED_M_B'), on_press=self._key_led_mode_breathe
|
||||
keys = (
|
||||
(('LED_ANI',), self._key_led_ani),
|
||||
(('LED_AND',), self._key_led_and),
|
||||
(('LED_MODE_PLAIN', 'LED_M_P'), self._key_led_mode_static),
|
||||
(('LED_MODE_BREATHE', 'LED_M_B'), self._key_led_mode_breathe),
|
||||
)
|
||||
|
||||
def closure(candidate):
|
||||
for names, on_press in argumented_keys:
|
||||
if candidate in names:
|
||||
return make_argumented_key(
|
||||
names=names,
|
||||
validator=self._led_key_validator,
|
||||
on_press=on_press,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
for names, on_press in keys:
|
||||
if candidate in names:
|
||||
return make_key(
|
||||
names=names, on_press=on_press, on_release=handler_passthrough
|
||||
)
|
||||
|
||||
return closure
|
||||
|
||||
def __repr__(self):
|
||||
return f'LED({self._to_dict()})'
|
||||
|
||||
|
@@ -1,9 +1,13 @@
|
||||
from kmk.extensions import Extension
|
||||
from kmk.keys import make_consumer_key
|
||||
from kmk.keys import KC, make_consumer_key
|
||||
|
||||
|
||||
class MediaKeys(Extension):
|
||||
def __init__(self):
|
||||
KC._generators.append(self.maybe_make_media_key)
|
||||
|
||||
@staticmethod
|
||||
def maybe_make_media_key(candidate):
|
||||
# Consumer ("media") keys. Most known keys aren't supported here. A much
|
||||
# longer list used to exist in this file, but the codes were almost certainly
|
||||
# incorrect, conflicting with each other, or otherwise 'weird'. We'll add them
|
||||
@@ -14,20 +18,23 @@ class MediaKeys(Extension):
|
||||
# support PC media keys, so I don't know how much value we would get out of
|
||||
# adding the old Apple-specific consumer codes, but again, PRs welcome if the
|
||||
# lack of them impacts you.
|
||||
make_consumer_key(code=226, names=('AUDIO_MUTE', 'MUTE')) # 0xE2
|
||||
make_consumer_key(code=233, names=('AUDIO_VOL_UP', 'VOLU')) # 0xE9
|
||||
make_consumer_key(code=234, names=('AUDIO_VOL_DOWN', 'VOLD')) # 0xEA
|
||||
make_consumer_key(code=111, names=('BRIGHTNESS_UP', 'BRIU')) # 0x6F
|
||||
make_consumer_key(code=112, names=('BRIGHTNESS_DOWN', 'BRID')) # 0x70
|
||||
make_consumer_key(code=181, names=('MEDIA_NEXT_TRACK', 'MNXT')) # 0xB5
|
||||
make_consumer_key(code=182, names=('MEDIA_PREV_TRACK', 'MPRV')) # 0xB6
|
||||
make_consumer_key(code=183, names=('MEDIA_STOP', 'MSTP')) # 0xB7
|
||||
make_consumer_key(
|
||||
code=205, names=('MEDIA_PLAY_PAUSE', 'MPLY')
|
||||
) # 0xCD (this may not be right)
|
||||
make_consumer_key(code=184, names=('MEDIA_EJECT', 'EJCT')) # 0xB8
|
||||
make_consumer_key(code=179, names=('MEDIA_FAST_FORWARD', 'MFFD')) # 0xB3
|
||||
make_consumer_key(code=180, names=('MEDIA_REWIND', 'MRWD')) # 0xB4
|
||||
codes = (
|
||||
(226, ('AUDIO_MUTE', 'MUTE')), # 0xE2
|
||||
(233, ('AUDIO_VOL_UP', 'VOLU')), # 0xE9
|
||||
(234, ('AUDIO_VOL_DOWN', 'VOLD')), # 0xEA
|
||||
(111, ('BRIGHTNESS_UP', 'BRIU')), # 0x6F
|
||||
(112, ('BRIGHTNESS_DOWN', 'BRID')), # 0x70
|
||||
(181, ('MEDIA_NEXT_TRACK', 'MNXT')), # 0xB5
|
||||
(182, ('MEDIA_PREV_TRACK', 'MPRV')), # 0xB6
|
||||
(183, ('MEDIA_STOP', 'MSTP')), # 0xB7
|
||||
(205, ('MEDIA_PLAY_PAUSE', 'MPLY')), # 0xCD (this may not be right)
|
||||
(184, ('MEDIA_EJECT', 'EJCT')), # 0xB8
|
||||
(179, ('MEDIA_FAST_FORWARD', 'MFFD')), # 0xB3
|
||||
(180, ('MEDIA_REWIND', 'MRWD')), # 0xB4
|
||||
)
|
||||
for code, names in codes:
|
||||
if candidate in names:
|
||||
return make_consumer_key(code=code, names=names)
|
||||
|
||||
def on_runtime_enable(self, sandbox):
|
||||
return
|
||||
|
@@ -4,7 +4,7 @@ from storage import getmount
|
||||
|
||||
from kmk.extensions import Extension
|
||||
from kmk.handlers.stock import passthrough as handler_passthrough
|
||||
from kmk.keys import make_key
|
||||
from kmk.keys import KC, make_key
|
||||
|
||||
|
||||
class Color:
|
||||
@@ -69,16 +69,35 @@ class Rgb_matrix(Extension):
|
||||
else:
|
||||
self.ledDisplay = ledDisplay
|
||||
|
||||
make_key(
|
||||
names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_BRI',), on_press=self._rgb_bri, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_BRD',), on_press=self._rgb_brd, on_release=handler_passthrough
|
||||
KC._generators.append(self.maybe_make_peg_rgb_key())
|
||||
|
||||
def maybe_make_peg_rgb_key(self):
|
||||
keys = (
|
||||
(
|
||||
('RGB_TOG',),
|
||||
self._rgb_tog,
|
||||
),
|
||||
(
|
||||
('RGB_BRI',),
|
||||
self._rgb_bri,
|
||||
),
|
||||
(
|
||||
('RGB_BRD',),
|
||||
self._rgb_brd,
|
||||
),
|
||||
)
|
||||
|
||||
def closure(candidate):
|
||||
for names, on_press in keys:
|
||||
if candidate in names:
|
||||
return make_key(
|
||||
names=names,
|
||||
on_press=on_press,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
|
||||
return closure
|
||||
|
||||
def _rgb_tog(self, *args, **kwargs):
|
||||
if self.enable:
|
||||
self.off()
|
||||
|
@@ -3,7 +3,7 @@ from math import e, exp, pi, sin
|
||||
|
||||
from kmk.extensions import Extension
|
||||
from kmk.handlers.stock import passthrough as handler_passthrough
|
||||
from kmk.keys import make_key
|
||||
from kmk.keys import KC, make_key
|
||||
from kmk.kmktime import PeriodicTimer
|
||||
from kmk.utils import Debug, clamp
|
||||
|
||||
@@ -157,69 +157,37 @@ class RGB(Extension):
|
||||
|
||||
self._substep = 0
|
||||
|
||||
make_key(
|
||||
names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_HUI',), on_press=self._rgb_hui, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_HUD',), on_press=self._rgb_hud, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_SAI',), on_press=self._rgb_sai, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_SAD',), on_press=self._rgb_sad, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_VAI',), on_press=self._rgb_vai, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_VAD',), on_press=self._rgb_vad, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_ANI',), on_press=self._rgb_ani, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_AND',), on_press=self._rgb_and, on_release=handler_passthrough
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_PLAIN', 'RGB_M_P'),
|
||||
on_press=self._rgb_mode_static,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_BREATHE', 'RGB_M_B'),
|
||||
on_press=self._rgb_mode_breathe,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_RAINBOW', 'RGB_M_R'),
|
||||
on_press=self._rgb_mode_rainbow,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_BREATHE_RAINBOW', 'RGB_M_BR'),
|
||||
on_press=self._rgb_mode_breathe_rainbow,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_SWIRL', 'RGB_M_S'),
|
||||
on_press=self._rgb_mode_swirl,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_MODE_KNIGHT', 'RGB_M_K'),
|
||||
on_press=self._rgb_mode_knight,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
make_key(
|
||||
names=('RGB_RESET', 'RGB_RST'),
|
||||
on_press=self._rgb_reset,
|
||||
on_release=handler_passthrough,
|
||||
KC._generators.append(self.maybe_make_rgb_key())
|
||||
|
||||
def maybe_make_rgb_key(self):
|
||||
keys = (
|
||||
(('RGB_TOG',), self._rgb_tog),
|
||||
(('RGB_HUI',), self._rgb_hui),
|
||||
(('RGB_HUD',), self._rgb_hud),
|
||||
(('RGB_SAI',), self._rgb_sai),
|
||||
(('RGB_SAD',), self._rgb_sad),
|
||||
(('RGB_VAI',), self._rgb_vai),
|
||||
(('RGB_VAD',), self._rgb_vad),
|
||||
(('RGB_ANI',), self._rgb_ani),
|
||||
(('RGB_AND',), self._rgb_and),
|
||||
(('RGB_MODE_PLAIN', 'RGB_M_P'), self._rgb_mode_static),
|
||||
(('RGB_MODE_BREATHE', 'RGB_M_B'), self._rgb_mode_breathe),
|
||||
(('RGB_MODE_RAINBOW', 'RGB_M_R'), self._rgb_mode_rainbow),
|
||||
(('RGB_MODE_BREATHE_RAINBOW', 'RGB_M_BR'), self._rgb_mode_breathe_rainbow),
|
||||
(('RGB_MODE_SWIRL', 'RGB_M_S'), self._rgb_mode_swirl),
|
||||
(('RGB_MODE_KNIGHT', 'RGB_M_K'), self._rgb_mode_knight),
|
||||
(('RGB_RESET', 'RGB_RST'), self._rgb_reset),
|
||||
)
|
||||
|
||||
def closure(candidate):
|
||||
for names, on_press in keys:
|
||||
if candidate in names:
|
||||
return make_key(
|
||||
names=names, on_press=on_press, on_release=handler_passthrough
|
||||
)
|
||||
|
||||
return closure
|
||||
|
||||
def on_runtime_enable(self, sandbox):
|
||||
return
|
||||
|
||||
|
@@ -4,7 +4,8 @@ import pwmio
|
||||
import time
|
||||
|
||||
from kmk.extensions import Extension, InvalidExtensionEnvironment
|
||||
from kmk.keys import make_key
|
||||
from kmk.handlers.stock import passthrough as handler_passthrough
|
||||
from kmk.keys import KC, make_key
|
||||
|
||||
|
||||
class statusLED(Extension):
|
||||
@@ -32,8 +33,24 @@ class statusLED(Extension):
|
||||
self.brightness_step = brightness_step
|
||||
self.brightness_limit = brightness_limit
|
||||
|
||||
make_key(names=('SLED_INC',), on_press=self._key_led_inc)
|
||||
make_key(names=('SLED_DEC',), on_press=self._key_led_dec)
|
||||
KC._generators.append(self.maybe_make_status_led_key())
|
||||
|
||||
def maybe_make_status_led_key(self):
|
||||
keys = (
|
||||
(('SLED_INC',), self._key_led_inc),
|
||||
(('SLED_DEC',), self._key_led_dec),
|
||||
)
|
||||
|
||||
def closure(candidate):
|
||||
for names, on_press in keys:
|
||||
if candidate in names:
|
||||
return make_key(
|
||||
names=names,
|
||||
on_press=on_press,
|
||||
on_release=handler_passthrough,
|
||||
)
|
||||
|
||||
return closure
|
||||
|
||||
def _layer_indicator(self, layer_active, *args, **kwargs):
|
||||
'''
|
||||
|
Reference in New Issue
Block a user