2022-11-12 16:45:32 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
|
|
|
#
|
|
|
|
# This file is part of input-remapper.
|
|
|
|
#
|
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-10-16 12:56:21 +00:00
|
|
|
import re
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Dict, Tuple, Optional, Callable
|
|
|
|
|
|
|
|
from inputremapper.configs.mapping import MappingData
|
|
|
|
from inputremapper.event_combination import EventCombination
|
|
|
|
from inputremapper.gui.messages.message_types import (
|
|
|
|
MessageType,
|
|
|
|
Name,
|
|
|
|
Capabilities,
|
|
|
|
Key,
|
|
|
|
DeviceTypes,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UInputsData:
|
|
|
|
message_type = MessageType.uinputs
|
|
|
|
uinputs: Dict[Name, Capabilities]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
string = f"{self.__class__.__name__}(uinputs={self.uinputs})"
|
|
|
|
|
|
|
|
# find all sequences of comma+space separated numbers, and shorten them
|
|
|
|
# to the first and last number
|
2022-11-12 16:45:32 +00:00
|
|
|
all_matches = list(re.finditer("(\d+, )+", string))
|
2022-10-16 12:56:21 +00:00
|
|
|
all_matches.reverse()
|
|
|
|
for match in all_matches:
|
|
|
|
start = match.start()
|
|
|
|
end = match.end()
|
|
|
|
start += string[start:].find(",") + 2
|
|
|
|
if start == end:
|
|
|
|
continue
|
|
|
|
string = f"{string[:start]}... {string[end:]}"
|
|
|
|
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class GroupsData:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message containing all available groups and their device types."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.groups
|
|
|
|
groups: Dict[Key, DeviceTypes]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class GroupData:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message with the active group and available presets for the group."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.group
|
|
|
|
group_key: str
|
|
|
|
presets: Tuple[str, ...]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class PresetData:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message with the active preset name and mapping names/combinations."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.preset
|
|
|
|
name: Optional[Name]
|
|
|
|
mappings: Optional[Tuple[MappingData, ...]]
|
|
|
|
autoload: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class StatusData:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message with the strings and id for the status bar."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.status_msg
|
|
|
|
ctx_id: int
|
|
|
|
msg: Optional[str] = None
|
|
|
|
tooltip: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class CombinationRecorded:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message with the latest recoded combination."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.combination_recorded
|
|
|
|
combination: "EventCombination"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class CombinationUpdate:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message with the old and new combination (hash for a mapping) when it changed."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.combination_update
|
|
|
|
old_combination: "EventCombination"
|
|
|
|
new_combination: "EventCombination"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UserConfirmRequest:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Message for requesting a user response (confirm/cancel) from the gui."""
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
message_type = MessageType.user_confirm_request
|
|
|
|
msg: str
|
|
|
|
respond: Callable[[bool], None] = lambda _: None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class DoStackSwitch:
|
|
|
|
"""Command the stack to switch to a different page."""
|
|
|
|
|
|
|
|
message_type = MessageType.do_stack_switch
|
|
|
|
page_index: int
|