KeyboardLayout: stopgap solution for sway

This commit is contained in:
Ala Alkhafaji
2025-11-11 01:53:49 +01:00
parent 43ff69238a
commit a6f487eac0
2 changed files with 82 additions and 142 deletions
-142
View File
@@ -12,122 +12,6 @@ Singleton {
property string currentLayout: I18n.tr("system.unknown-layout")
property string previousLayout: ""
property bool isInitialized: false
property int updateInterval: 1000 // Update every second
// Timer to periodically update the layout
Timer {
id: updateTimer
interval: updateInterval
running: true
repeat: true
onTriggered: {
updateLayout()
}
}
// Process for X11 systems using setxkbmap
Process {
id: x11LayoutProcess
running: false
command: ["setxkbmap", "-query"]
stdout: StdioCollector {
onStreamFinished: {
try {
const lines = text.split('\n')
for (const line of lines) {
if (line.startsWith('layout:')) {
const layout = line.split(':')[1].trim()
root.currentLayout = layout
return
}
}
root.currentLayout = I18n.tr("system.unknown-layout")
} catch (e) {
root.currentLayout = I18n.tr("system.unknown-layout")
}
}
}
}
// Process for general Wayland using localectl (systemd)
Process {
id: localectlProcess
running: false
command: ["localectl", "status"]
stdout: StdioCollector {
onStreamFinished: {
try {
const lines = text.split('\n')
for (const line of lines) {
if (line.includes("X11 Layout:")) {
const layout = line.split(':')[1].trim()
if (layout && layout !== "n/a") {
root.currentLayout = layout
return
}
}
if (line.includes("VC Keymap:")) {
const keymap = line.split(':')[1].trim()
if (keymap && keymap !== "n/a") {
root.currentLayout = extractLayoutCode(keymap)
return
}
}
}
root.currentLayout = I18n.tr("system.unknown-layout")
} catch (e) {
root.currentLayout = I18n.tr("system.unknown-layout")
}
}
}
}
// Process for generic keyboard layout detection using gsettings (GNOME-based)
Process {
id: gsettingsProcess
running: false
command: ["gsettings", "get", "org.gnome.desktop.input-sources", "current"]
stdout: StdioCollector {
onStreamFinished: {
try {
const currentIndex = parseInt(text.trim())
gsettingsSourcesProcess.running = true
} catch (e) {
fallbackToLocalectl()
}
}
}
}
Process {
id: gsettingsSourcesProcess
running: false
command: ["gsettings", "get", "org.gnome.desktop.input-sources", "sources"]
stdout: StdioCollector {
onStreamFinished: {
try {
// Parse the sources array and extract layout codes
const sourcesText = text.trim()
const matches = sourcesText.match(/\('xkb', '([^']+)'\)/g)
if (matches && matches.length > 0) {
// Get the first layout as default
const layoutMatch = matches[0].match(/\('xkb', '([^']+)'\)/)
if (layoutMatch) {
root.currentLayout = layoutMatch[1].split('+')[0] // Take first part before any variants
}
} else {
fallbackToLocalectl()
}
} catch (e) {
fallbackToLocalectl()
}
}
}
}
function fallbackToLocalectl() {
localectlProcess.running = true
}
// Updates current layout from various format strings. Called by compositors
function setCurrentLayout(layoutString) {
@@ -186,7 +70,6 @@ Singleton {
Component.onCompleted: {
Logger.i("KeyboardLayout", "Service started")
updateLayout()
// Mark as initialized after a delay to allow first layout update to complete
// This prevents showing a toast on the initial load
initializationTimer.start()
@@ -203,31 +86,6 @@ Singleton {
}
}
function updateLayout() {
// Try compositor-specific methods first
if (CompositorService.isHyprland) {
} else if (CompositorService.isNiri) {
} else {
// Try detection methods in order of preference
if (Qt.platform.os === "linux") {
// Check if we're in X11 or Wayland
const sessionType = Qt.application.arguments.find(arg => arg.includes("QT_QPA_PLATFORM")) || process.env.XDG_SESSION_TYPE
if (sessionType && sessionType.includes("xcb") || process.env.DISPLAY) {
// X11 system
x11LayoutProcess.running = true
} else {
// Wayland or unknown - try gsettings first, then localectl
gsettingsProcess.running = true
}
} else {
currentLayout = I18n.tr("system.unknown-layout")
}
}
}
// Comprehensive language name to ISO code mapping
property var languageMap: {
"english"// English variants