Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #45 from ivanjermakov/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ivanjermakov authored Sep 12, 2020
2 parents c37ca22 + 51fd9fc commit b957789
Show file tree
Hide file tree
Showing 20 changed files with 555 additions and 596 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/kbmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ jobs:
- name: install
run: pip install .

- name: type check
run: |
pip install mypy
mypy --ignore-missing-imports kbmap
- name: test
run: python -m unittest -v

- name: install coverage
run: pip install coverage

- name: code coverage
run: coverage run -m unittest
- name: measure code coverage
run: |
pip install coverage
coverage run -m unittest
- name: install codecov
run: pip install codecov

- name: upload coverage to codecov
run: codecov
- name: codecov
run: |
pip install codecov
codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

3 changes: 3 additions & 0 deletions kbmap/action/action_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


class ActionType(Enum):
"""
Action type
"""
ModKeyAction = 1
ModTapAction = 2
LayerOnAction = 3
Expand Down
12 changes: 8 additions & 4 deletions kbmap/action/kbmap_toggle_action.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from evdev.events import KeyEvent
from evdev.events import InputEvent, KeyEvent

from kbmap import mapper
from kbmap.action.action_type import ActionType
from kbmap.log import debug


class KbmapToggleAction:
"""
Action used to toggle kbmap on and off.
When kbmap is off, all mapped physical keys directly written to UInput, bypassing any mapping.
"""

type: ActionType

def __init__(self):
def __init__(self) -> None:
self.type = ActionType.KbmapToggleAction

def handle(self, _, e, *args):
def handle(self, _, e: InputEvent, *args) -> None:
debug('-- handling kbmap toggle action --')
if e.value == KeyEvent.key_down:
debug(f'now kbmap is {"enabled" if mapper.kbmap_enabled else "disabled"}')
mapper.kbmap_enabled = not mapper.kbmap_enabled
debug(f'toggle kbmap {"enabled" if mapper.kbmap_enabled else "disabled"}')
else:
debug('key release, skipping')

23 changes: 14 additions & 9 deletions kbmap/action/layer_mod_action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Tuple

from evdev.events import KeyEvent
from evdev import UInput
from evdev.events import KeyEvent, InputEvent

from kbmap import mapper
from kbmap.action.action_type import ActionType
Expand All @@ -9,22 +10,26 @@


class LayerModAction:
"""
Layer mod action.
"""

type: ActionType
layer: int
layer_index: int
modifiers: Tuple[int, ...]

def __init__(self, layer, *modifiers):
def __init__(self, layer_index: int, *modifiers: int) -> None:
self.type = ActionType.LayerOnAction
self.layer = layer
self.layer_index = layer_index
self.modifiers = modifiers

def handle(self, ui, e, config, *args):
def handle(self, ui: UInput, e, config, *args) -> None:
debug('-- handling layer mod action --')
if e.value == KeyEvent.key_down:
mapper.enable_layer(self.layer, self, config)
mapper.enable_layer(self.layer_index, self, config)
else:
mapper.disable_layer(ui, self.layer, config)
mapper.disable_layer(ui, self.layer_index, config)

def handle_layer_key(self, ui, key, e, *args):
def handle_layer_key(self, ui: UInput, code: int, e: InputEvent, *args) -> None:
debug('-- handling LM layer key --')
ModKeyAction(key, *self.modifiers).handle(ui, e)
ModKeyAction(code, *self.modifiers).handle(ui, e)
19 changes: 13 additions & 6 deletions kbmap/action/layer_on_action.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from evdev import UInput
from evdev.events import InputEvent
from evdev.events import KeyEvent

from kbmap import mapper
from kbmap.action.action_type import ActionType
from kbmap.config import Config
from kbmap.log import debug


class LayerOnAction:
"""
Layer on action.
"""

type: ActionType
layer: int
layer_index: int

def __init__(self, layer):
def __init__(self, layer_index: int) -> None:
self.type = ActionType.LayerOnAction
self.layer = layer
self.layer_index = layer_index

def handle(self, ui, e, config, *args):
def handle(self, ui: UInput, e: InputEvent, config: Config, *args):
debug('-- handling layer on action --')
if e.value == KeyEvent.key_down:
mapper.enable_layer(self.layer, self, config)
mapper.enable_layer(self.layer_index, self, config)
else:
mapper.disable_layer(ui, self.layer, config)
mapper.disable_layer(ui, self.layer_index, config)
37 changes: 22 additions & 15 deletions kbmap/action/layer_tap_action.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
from evdev.events import KeyEvent
from evdev import UInput
from evdev.events import InputEvent, KeyEvent

from kbmap import mapper, host
from kbmap import host
from kbmap import mapper
from kbmap.action.action_type import ActionType
from kbmap.config import Config
from kbmap.log import debug


class LayerTapAction:
"""
Layer tap action.
"""

type: ActionType
layer: int
key: int
layer_index: int
code: int
used: bool

def __init__(self, layer, key):
def __init__(self, layer_index: int, code: int) -> None:
self.type = ActionType.LayerTapAction
self.layer = layer
self.key = key
self.layer_index = layer_index
self.code = code
self.used = False

def __repr__(self):
return f'LT({self.layer}, {self.key})'
def __repr__(self) -> str:
return f'LT({self.layer_index}, {self.code})'

def handle(self, ui, e, config, pos, *args):
def handle(self, ui: UInput, e: InputEvent, config: Config, pos: int, *args) -> None:
debug('-- handling layer tap action --')
if e.value == KeyEvent.key_down:
mapper.active_tap_actions[pos] = self

debug(f'LT is pressed, enabling layer [{self.layer}]')
mapper.enable_layer(self.layer, self, config)
debug(f'LT is pressed, enabling layer [{self.layer_index}]')
mapper.enable_layer(self.layer_index, self, config)
else:
mapper.active_tap_actions.pop(pos)

host.release_weak_keys(ui, config)
debug(f'LT is released, disabling layer [{self.layer}]')
debug(f'LT is released, disabling layer [{self.layer_index}]')

mapper.disable_layer(ui, self.layer, config)
mapper.disable_layer(ui, self.layer_index, config)

since_last_press = (e.timestamp() - mapper.last_press_timestamps[pos]) * 1000
debug(f'since last press: {since_last_press}')
debug(f'action is used: {self.used}')
if not self.used and since_last_press <= config.tapping_term:
debug('writing key press')
host.write_tap(ui, self.key)
host.write_tap(ui, self.code)
self.used = False
26 changes: 16 additions & 10 deletions kbmap/action/layer_toggle_action.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
from evdev.events import KeyEvent
from evdev import UInput
from evdev.events import InputEvent, KeyEvent

from kbmap import mapper
from kbmap.action.action_type import ActionType
from kbmap.config import Config
from kbmap.log import debug


class LayerToggleAction:
"""
Layer toggle action.
"""

type: ActionType
layer: int
layer_index: int

def __init__(self, layer, ):
def __init__(self, layer_index: int) -> None:
self.type = ActionType.LayerToggleAction
self.layer = layer
self.layer_index = layer_index

def handle(self, ui, e, config, *args):
def handle(self, ui: UInput, e: InputEvent, config: Config, *args) -> None:
debug('-- handling layer toggle action --')
if e.value == KeyEvent.key_down:
if mapper.active_layers[self.layer]:
debug(f'disabling layer [{self.layer}]')
mapper.disable_layer(ui, self.layer, config)
if mapper.active_layers[self.layer_index]:
debug(f'disabling layer [{self.layer_index}]')
mapper.disable_layer(ui, self.layer_index, config)
else:
debug(f'enabling layer [{self.layer}]')
mapper.enable_layer(self.layer, self, config)
debug(f'enabling layer [{self.layer_index}]')
mapper.enable_layer(self.layer_index, self, config)
25 changes: 15 additions & 10 deletions kbmap/action/mod_key_action.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
from typing import Tuple
from typing import *

from evdev.events import KeyEvent
from evdev import UInput
from evdev.events import InputEvent, KeyEvent

from kbmap import host
from kbmap.action.action_type import ActionType
from kbmap.log import debug


class ModKeyAction:
"""
Mod key action.
"""

type: ActionType
key: int
code: Union[int, None]
modifiers: Tuple[int, ...]

def __init__(self, key, *modifiers):
def __init__(self, code: Union[int, None], *modifiers) -> None:
self.type = ActionType.ModKeyAction
self.modifiers = modifiers
self.key = key
self.code = code

def handle(self, ui, e, *args):
def handle(self, ui: UInput, e: InputEvent, *args) -> None:
debug('-- handling mod key action --')
if e.value == KeyEvent.key_down:
for m in self.modifiers:
host.write_code(ui, m, e.value)
if self.key:
host.write_code(ui, self.key, e.value)
if self.code:
host.write_code(ui, self.code, e.value)
else:
if self.key:
host.write_code(ui, self.key, e.value)
if self.code:
host.write_code(ui, self.code, e.value)
for m in self.modifiers:
host.write_code(ui, m, e.value)
26 changes: 16 additions & 10 deletions kbmap/action/mod_tap_action.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
from typing import Any, Tuple
from typing import *

from evdev.events import KeyEvent
from evdev import UInput
from evdev.events import InputEvent, KeyEvent

from kbmap import mapper, host
from kbmap.action.action_type import ActionType
from kbmap.config import Config
from kbmap.log import debug


class ModTapAction:
"""
Mod tap action.
"""

type: ActionType
key: int
modifiers: Tuple[Any, ...]
code: int
modifiers: Tuple[int, ...]
used: bool

def __init__(self, key, *modifiers):
def __init__(self, code: int, *modifiers) -> None:
self.type = ActionType.ModTapAction
self.modifiers = modifiers
self.key = key
self.code = code
self.used = False

def __repr__(self):
return f'MT{"u" if self.used else ""}({self.key}, {",".join(map(str, self.modifiers))})'
def __repr__(self) -> str:
return f'MT{"u" if self.used else ""}({self.code}, {",".join(map(str, self.modifiers))})'

def handle(self, ui, e, config, pos, *args):
def handle(self, ui: UInput, e: InputEvent, config: Config, pos: int, *args) -> None:
debug('-- handling mod tap action --')
if e.value == KeyEvent.key_down:
mapper.active_tap_actions[pos] = self
Expand All @@ -42,5 +48,5 @@ def handle(self, ui, e, config, pos, *args):
debug(f'action is used: {self.used}')
if not self.used and since_last_press <= config.tapping_term:
debug('writing key press')
host.write_tap(ui, self.key)
host.write_tap(ui, self.code)
self.used = False
Loading

0 comments on commit b957789

Please sign in to comment.