From 6498f9e0d11765b0453a88616709cefed7fcf0f1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 7 Sep 2024 10:45:15 -0400 Subject: [PATCH] [keyboard] Rewrite KWin config file --- src/modules/keyboard/Config.cpp | 75 +++++++++++++++++++++++++++++++++ src/modules/keyboard/Config.h | 1 + 2 files changed, 76 insertions(+) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 2f4f53323..505aa3e51 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -234,6 +235,10 @@ Config::apply() { applyLocale1(); } + if ( m_configureKWin ) + { + applyKWin(); + } // Writing /etc/ files is not needed "live" } @@ -314,6 +319,76 @@ Config::applyXkb() m_applyTimer.stop(); } +// In a config-file's list of lines, replace lines = by = +static void +replaceKey( QStringList& content, const QString& key, const QString& value ) +{ + for ( int i = 0; i < content.length(); ++i ) + { + if ( content.at( i ).startsWith( key ) ) + { + content[ i ] = key + value; + } + } +} + +static bool +rewriteKWin( const QString& path, const QString& model, const QString& layouts, const QString& variants ) +{ + if ( !QFile::exists( path ) ) + { + return false; + } + + QFile config( path ); + if ( !config.open( QIODevice::ReadOnly ) ) + { + return false; + } + QStringList content = []( QFile& f ) + { + QTextStream s( &f ); + return s.readAll().split( '\n' ); + }( config ); + config.close(); + + if ( !config.open( QIODevice::WriteOnly ) ) + { + return false; + } + + replaceKey( content, QStringLiteral( "Model=" ), model ); + replaceKey( content, QStringLiteral( "LayoutList=" ), layouts ); + replaceKey( content, QStringLiteral( "VariantList=" ), variants ); + + config.write( content.join( '\n' ).toUtf8() ); + config.close(); + + return true; +} + +void +Config::applyKWin() +{ + const auto paths = QStandardPaths::standardLocations( QStandardPaths::ConfigLocation ); + + auto join = [ &additional = m_additionalLayoutInfo.additionalLayout ]( const QString& s1, const QString& s2 ) + { return additional.isEmpty() ? s1 : QStringLiteral( "%1,%2" ).arg( s1, s2 ); }; + + const QString layouts = join( m_selectedLayout, m_additionalLayoutInfo.additionalLayout ); + const QString variants = join( m_selectedVariant, m_additionalLayoutInfo.additionalVariant ); + + for ( const auto& path : paths ) + { + const QString candidate = path + QStringLiteral( "/kxkbrc" ); + if ( rewriteKWin( candidate, m_selectedModel, layouts, variants ) ) + { + break; + } + } +} + + KeyboardModelsModel* Config::keyboardModels() const { diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index 1549e5d83..e2a8c4f0e 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -96,6 +96,7 @@ private: void apply(); void applyLocale1(); void applyXkb(); + void applyKWin(); void getCurrentKeyboardLayoutXkb( QString& currentLayout, QString& currentVariant, QString& currentModel ); void getCurrentKeyboardLayoutLocale1( QString& currentLayout, QString& currentVariant, QString& currentModel );