KeyboardLayout: add tons of layouts, add Commons/KeyboardLayout.qml for ease of adding new ones

This commit is contained in:
Ly-sec 2025-09-11 01:40:25 +02:00
parent be0b568f1f
commit 7247a26586
2 changed files with 360 additions and 30 deletions

View file

@ -9,7 +9,6 @@ import qs.Services
Singleton {
id: root
property string currentLayout: "Unknown"
property int updateInterval: 1000 // Update every second
@ -34,8 +33,9 @@ Singleton {
try {
const data = JSON.parse(text)
const layoutName = data.names[data.current_idx]
root.currentLayout = mapLayoutNameToCode(layoutName)
root.currentLayout = extractLayoutCode(layoutName)
} catch (e) {
console.log("Niri layout error:", e)
root.currentLayout = "Unknown"
}
}
@ -54,45 +54,157 @@ Singleton {
// Find the main keyboard and get its active keymap
const mainKeyboard = data.keyboards.find(kb => kb.main === true)
if (mainKeyboard && mainKeyboard.active_keymap) {
root.currentLayout = mapLayoutNameToCode(mainKeyboard.active_keymap)
root.currentLayout = extractLayoutCode(mainKeyboard.active_keymap)
} else {
root.currentLayout = "Unknown"
}
} catch (e) {
console.log("Hyprland layout error:", e)
root.currentLayout = "Unknown"
}
}
}
}
// Layout name to ISO code mapping
property var layoutMap: {
"German": "de",
"English (US)": "us",
"English (UK)": "gb",
"French": "fr",
"Spanish": "es",
"Italian": "it",
"Portuguese (Brazil)": "br",
"Portuguese": "pt",
"Russian": "ru",
"Polish": "pl",
"Swedish": "se",
"Norwegian": "no",
"Danish": "dk",
"Finnish": "fi",
"Hungarian": "hu",
"Turkish": "tr",
"Czech": "cz",
"Slovak": "sk",
"Japanese": "jp",
"Korean": "kr",
"Chinese": "cn"
// 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 = "Unknown"
} catch (e) {
console.log("X11 layout error:", e)
root.currentLayout = "Unknown"
}
}
}
}
// Map layout names to ISO codes
function mapLayoutNameToCode(layoutName) {
return layoutMap[layoutName] || layoutName // fallback to raw name if not found
// 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 = "Unknown"
} catch (e) {
console.log("Localectl error:", e)
root.currentLayout = "Unknown"
}
}
}
}
// 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) {
console.log("Gsettings current error:", 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) {
console.log("Gsettings sources error:", e)
fallbackToLocalectl()
}
}
}
}
function fallbackToLocalectl() {
localectlProcess.running = true
}
// Extract layout code from various format strings using Commons data
function extractLayoutCode(layoutString) {
if (!layoutString)
return "Unknown"
const str = layoutString.toLowerCase()
// If it's already a short code (2-3 chars), return as-is
if (/^[a-z]{2,3}(\+.*)?$/.test(str)) {
return str.split('+')[0]
}
// Extract from parentheses like "English (US)"
const parenMatch = str.match(/\(([a-z]{2,3})\)/)
if (parenMatch) {
return parenMatch[1]
}
// Check for exact matches or partial matches in language map from Commons
const entries = Object.entries(KeyboardLayout.languageMap)
for (var i = 0; i < entries.length; i++) {
const lang = entries[i][0]
const code = entries[i][1]
if (str.includes(lang)) {
return code
}
}
// If nothing matches, try first 2-3 characters if they look like a code
const codeMatch = str.match(/^([a-z]{2,3})/)
return codeMatch ? codeMatch[1] : "unknown"
}
Component.onCompleted: {
@ -101,12 +213,28 @@ Singleton {
}
function updateLayout() {
// Try compositor-specific methods first
if (CompositorService.isHyprland) {
hyprlandLayoutProcess.running = true
} else if (CompositorService.isNiri) {
niriLayoutProcess.running = true
} else {
currentLayout = "Unknown"
// 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 = "Unknown"
}
}
}
}