Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

environ_api and window context provider classes module #157

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/keyszer/config_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import os
import inspect
from inspect import signature
from pprint import pformat as ppf
from pprint import pprint as pp

from .lib.logger import error, debug
from .lib import window_context
from .models.action import Action
from .models.combo import Combo, ComboHint
from .models.trigger import Trigger
Expand Down Expand Up @@ -41,6 +44,77 @@
# multipurpose timeout
_TIMEOUTS = TIMEOUT_DEFAULTS


_ENVIRON = {
'session_type' : 'x11',
'wl_desktop_env': None
}


# make window_context provider classes self-documenting
def get_all_supported_environments():
supported_environments = []

# Get all classes in the window context module
all_classes = inspect.getmembers(window_context, inspect.isclass)

# shorter reference for long interface class name in 'if' condition below
WinCtxProvIface = window_context.WindowContextProviderInterface

# Iterate through each class
for name, obj in all_classes:
# If the class is a subclass of WindowContextProviderInterface
# (but not the base class itself)
if issubclass(obj, WinCtxProvIface) and obj is not WinCtxProvIface:
# Add the environments that this provider supports to the list
supported_environments.extend(obj.get_supported_environments())

# debug(f'get_all_supported_environments: {supported_environments = }')
return supported_environments


def environ_api(session_type='x11', wl_desktop_env=None):
"""
API function to specify the session type (X11/Xorg or Wayland)
and if Wayland, which desktop environment, to be used to try
to instantiate the correct window context provider object.

Default session type is 'x11' for backwards compatibility
with existing configs not using the API.
"""

# IMPORTANT: Reset wl_desktop_env to `None` if session is X11/Xorg
# Desktop is only relevant for Wayland session type
# Having anything other than `None` as desktop will not match X11/Xorg provider
# This matches environment ('x11', None) from X11/Xorg context provider
if session_type == 'x11':
wl_desktop_env = None

# disregard any capitalization mistakes by user
if isinstance(session_type, str):
session_type = session_type.casefold()
if isinstance(wl_desktop_env, str):
wl_desktop_env = wl_desktop_env.casefold()

# Get the currently supported environments
supported_environments = get_all_supported_environments()

# Construct the environment tuple based on the provided values
provided_environment_tup = (session_type, wl_desktop_env)

if provided_environment_tup not in supported_environments:
error(f'Unsupported environment: Session type: {session_type}, Desktop env: {wl_desktop_env}')
debug(f"Supported environments for keyszer: ('session_type', 'desktop_env')\n\t" +
'\n\t'.join(ppf(item) for item in supported_environments) + '\n')
sys.exit(1)

_ENVIRON.update({
'session_type': session_type,
'wl_desktop_env' : wl_desktop_env
})
debug(f"ENVIRON: Session type: '{session_type}', Desktop env: '{wl_desktop_env}'")


# global dict of delay values used to mitigate Unicode entry sequence and macro or combo failures
THROTTLE_DELAY_DEFAULTS = {
'key_pre_delay_ms': 0,
Expand Down
13 changes: 10 additions & 3 deletions src/keyszer/lib/key_context.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from ..xorg import get_xorg_context
# from ..xorg import get_xorg_context
from ..models.key import Key
from .window_context import WindowContextProvider


class KeyContext:
def __init__(self, device):
def __init__(self, device, window_context):
self._X_ctx = None
self._device = device
# self.session_type = session_type
# self.wl_desktop_env = wl_desktop_env

# self._win_ctx_provider = WindowContextProvider(self.session_type, self.wl_desktop_env)
self._win_ctx_provider: WindowContextProvider = window_context

def _query_window_context(self):
# cache this, think it might be expensive
if self._X_ctx is None:
self._X_ctx = get_xorg_context()
# self._X_ctx = get_xorg_context()
self._X_ctx = self._win_ctx_provider.get_window_context()

@property
def wm_class(self):
Expand Down
Loading