feat(root): add keyboard layouts, OSDWindow, and update services & utilities

Introduce a new OSDWindow implementation, expose keyboard layout configuration, and modernize dependency handling and helper utilities.

- **New Features**
  - Added `keyboard_layouts` list (`us`, `np`) to `config.json`
  - Created `OSDWindow` class and updated `main.py` to use it
  - Introduced new service modules (`battery`, `brightness`, `capslock`, `network`, `mpris`, `todo`, `modus`) and expanded `services/__init__.py`
  - Added `CommandResult` and `run_command` helper in `utils/functions.py`

- **Enhancements**
  - Upgraded numerous dependencies to newer versions and switched `fabric` to a git source
  - Replaced bare `except:` with `except Exception:` in `WiFiPasswordDialog`
  - Improved type hints (`NamedTuple`, `TypeVar`) and refactored JSON file helpers
  - Added property notifications for battery percentage, state, and presence

- **Bug Fixes**
  - Fixed incorrect return type annotation in `read_json_file`
  - Corrected tick‑handler logic to avoid double registration
  - Properly notify listeners on battery property changes
  - Ensured proper exception handling and cleanup in utility functions
This commit is contained in:
sankalp committed 2026-03-23 23:33:53 +05:45
1 parent 129906cb9c
commit 34cfb2f49e
12 files changed
+504 -96

No files matched your search

+10 -4
View File
@@ -1,4 +1,10 @@
"""
Modus services package.
Contains background services and utilities for the shell.
"""
from fabric.audio import Audio # noqa: F401
from .battery import BatteryService # noqa: F401
from .brightness import Brightness # noqa: F401
from .capslock import CapsLock # noqa: F401
from .keyboard_layout import KeyboardLayout # noqa: F401
from .network import NetworkClient, Wifi, Ethernet, AccessPoint # noqa: F401
from .mpris import MprisPlayer, MprisPlayerManager # noqa: F401
from .todo import TodoService # noqa: F401
from .modus import ModusService # noqa: F401
from .config import on_config_change, start_config_service, ConfigServic # noqa: F401
+18 -3
View File
@@ -1,6 +1,6 @@
from typing import Literal, Optional
from fabric import Service, Signal
from fabric.core.service import Property, Service, Signal
from fabric.utils import Gio, GLib, logger
from utils.dbus_helper import GioDBusHelper
@@ -178,8 +178,23 @@ class BatteryService(Service):
"""Get display name for a power profile."""
return PowerProfile.get(profile, profile.title())
def handle_property_change(self, *_):
# You may filter which property changed by checking parameters[1]
@Property(int, "readable", default_value=0)
def percentage(self) -> int:
return self.get_property("Percentage") or 0
@Property(int, "readable", default_value=0)
def state(self) -> int:
return self.get_property("State") or 0
@Property(bool, "readable", default_value=False)
def is_present(self) -> bool:
return self.get_property("IsPresent") or False
def handle_property_change(self, *args):
# Notify about property changes for OSD and other listeners
self.notify("percentage")
self.notify("state")
self.notify("is-present")
self.emit("changed")
def handle_power_profile_change(self, *_):
+100
View File
@@ -0,0 +1,100 @@
from pathlib import Path
from fabric.core.service import Property, Service, Signal
from fabric.utils import GLib, logger
# Discover CapsLock LED device
capslock_leds = list(Path("/sys/class/leds").glob("input*::capslock"))
capslock_device = capslock_leds[0] if capslock_leds else None
class CapsLock(Service):
instance = None
@staticmethod
def get_initial():
if CapsLock.instance is None:
CapsLock.instance = CapsLock()
return CapsLock.instance
@Signal
def state_changed(self, is_on: bool) -> None:
"""Signal emitted when CapsLock state changes."""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.capslock_led_path = capslock_device
self._timeout_id = None
self._last_state = None
self._monitoring_started = False
if capslock_device is None:
logger.warning("CapsLock device not found, CapsLock service disabled")
return
# Don't access is_on property during init to avoid starting monitoring
# The last_state will be set when monitoring actually starts
def _start_led_monitoring(self):
"""Start efficient polling for CapsLock LED state changes."""
brightness_path = self.capslock_led_path / "brightness"
if not brightness_path.exists():
logger.warning(
f"CapsLock brightness file does not exist: {brightness_path}"
)
return
self._start_efficient_polling()
def _start_efficient_polling(self):
"""Start efficient polling with 100ms intervals."""
if self._timeout_id is None:
self._timeout_id = GLib.timeout_add(100, self._efficient_poll)
def _efficient_poll(self) -> bool:
"""Monitor CapsLock state with 100ms polling intervals.
Returns:
bool: True to continue polling, False to stop.
"""
try:
current_state = self._get_current_state()
if current_state != self._last_state:
self._last_state = current_state
self.emit("state_changed", current_state)
except Exception as e:
logger.error(f"CapsLock polling error: {e}")
self._timeout_id = None
return False # Stop polling on error
return True # Continue polling
def stop(self):
if self._timeout_id is not None:
GLib.source_remove(self._timeout_id)
self._timeout_id = None
def _ensure_monitoring_started(self):
if not self._monitoring_started and self.capslock_led_path:
self._monitoring_started = True
# Set initial state before starting monitoring
self._last_state = self._get_current_state()
self._start_led_monitoring()
def _get_current_state(self) -> bool:
if not self.capslock_led_path:
return False
brightness_path = self.capslock_led_path / "brightness"
if brightness_path.exists():
try:
return bool(int(brightness_path.read_text().strip()))
except (ValueError, OSError):
return False
return False
@Property(bool, "read-write", default_value=False)
def is_on(self) -> bool:
self._ensure_monitoring_started()
return self._get_current_state()
+154
View File
@@ -0,0 +1,154 @@
import json
import socket
import threading
from pathlib import Path
from fabric.core.service import Property, Service, Signal
from fabric.utils import GLib, logger, os
import config.data as data
from services.config import on_config_change
from utils.functions import read_json_file, run_command, write_json_file
HYPRCTL_BIN = "hyprctl"
class KeyboardLayout(Service):
"""Service to manage keyboard layout switching and monitoring via Hyprland."""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
@staticmethod
def get_initial():
if KeyboardLayout._instance is None:
KeyboardLayout._instance = KeyboardLayout()
return KeyboardLayout._instance
@Signal
def layout_changed(self, layout: str) -> None:
"""Signal emitted when keyboard layout changes."""
def __init__(self, **kwargs):
if getattr(self, "_initialized", False):
return
super().__init__(**kwargs)
self._initialized = True
self.layout_json_file = Path(data.CACHE_DIR) / "kb_layout.json"
self._last_layout = None
self.layouts = []
self.current_index = 0
self._init_layout_config()
self._start_event_listener()
on_config_change(self._on_config_change)
def _init_layout_config(self):
config_layouts = data.load_config().get("keyboard_layouts", ["us", "np"])
json_data = read_json_file(self.layout_json_file)
if json_data:
self.layouts = json_data.get("layouts", config_layouts)
self.current_index = json_data.get("current_index", 0)
else:
self.layouts = config_layouts
self.current_index = 0
self._save_layout_json()
# Sync with actual Hyprland state
self._sync_with_hyprland()
def _sync_with_hyprland(self):
try:
result = run_command([HYPRCTL_BIN, "devices", "-j"])
devices = json.loads(result)
keyboards = devices.get("keyboards", [])
for k in keyboards:
if k.get("main"):
layout_name = k.get("active_keymap")
if layout_name:
self._last_layout = layout_name
# Try to match with our list to update index
for i, layout_item in enumerate(self.layouts):
if layout_item.lower() in layout_name.lower():
self.current_index = i
break
break
except Exception as e:
logger.error(f"[KeyboardLayout] Sync error: {e}")
def _save_layout_json(self):
write_json_file(
{
"layouts": self.layouts,
"current_index": self.current_index,
},
self.layout_json_file,
)
def _on_config_change(self, new_config, old_config):
new_layouts = new_config.get("keyboard_layouts")
if new_layouts and new_layouts != self.layouts:
self.layouts = new_layouts
self._save_layout_json()
logger.info(f"[KeyboardLayout] Layouts updated from config: {self.layouts}")
def _start_event_listener(self):
"""Start a thread to listen for Hyprland layout events."""
thread = threading.Thread(target=self._event_loop, daemon=True)
thread.start()
def _event_loop(self):
his = os.getenv("HYPRLAND_INSTANCE_SIGNATURE")
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}")
socket_path = f"{xdg_runtime_dir}/hypr/{his}/.socket2.sock"
if not os.path.exists(socket_path):
logger.error(f"[KeyboardLayout] Hyprland socket not found: {socket_path}")
return
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
s.connect(socket_path)
logger.info("[KeyboardLayout] Connected to Hyprland event socket.")
while True:
data = s.recv(1024).decode("utf-8")
if not data:
break
for line in data.split("\n"):
if line.startswith("activelayout>>"):
# Format: activelayout>>keyboardname,layoutname
parts = line.split(">>")[1].split(",")
if len(parts) >= 2:
layout_name = parts[1]
if layout_name != self._last_layout:
self._last_layout = layout_name
GLib.idle_add(
self.emit, "layout_changed", layout_name
)
except Exception as e:
logger.error(f"[KeyboardLayout] Event listener error: {e}")
def switch_to_next(self) -> bool:
if not self.layouts:
return False
self.current_index = (self.current_index + 1) % len(self.layouts)
self.layouts[self.current_index]
# Use hyprctl to switch layout for all devices
# We assume the layouts in Hyprland config match self.layouts in order
run_command([HYPRCTL_BIN, "switchxkblayout", "all", "next"])
self._save_layout_json()
return True
@Property(str, "readable")
def current_layout(self) -> str:
return self._last_layout or "Unknown"
+9 -2
View File
@@ -164,5 +164,12 @@ class TodoService(Service):
}
# Global service instance
todo_service = TodoService()
# Global service instance getter
_todo_service = None
def get_todo_service() -> TodoService:
global _todo_service
if _todo_service is None:
_todo_service = TodoService()
return _todo_service