[keyboard] Rename functions, simplify logic

The timer can be connected to one slot, which just
checks what kinds of configuration are enabled.
Then update all the ones that make sense in a live system.
This commit is contained in:
Adriaan de Groot 2024-09-01 20:39:58 +02:00
parent 783881edaa
commit 4fea2920d7
2 changed files with 48 additions and 51 deletions

View File

@ -160,24 +160,16 @@ Config::Config( QObject* parent )
, m_keyboardVariantsModel( new KeyboardVariantsModel( this ) ) , m_keyboardVariantsModel( new KeyboardVariantsModel( this ) )
, m_KeyboardGroupSwitcherModel( new KeyboardGroupsSwitchersModel( this ) ) , m_KeyboardGroupSwitcherModel( new KeyboardGroupsSwitchersModel( this ) )
{ {
m_setxkbmapTimer.setSingleShot( true ); m_applyTimer.setSingleShot( true );
connect( &m_applyTimer, &QTimer::timeout, this, &Config::apply );
// Connect signals and slots // Connect signals and slots
connect( m_keyboardModelsModel, connect( m_keyboardModelsModel,
&KeyboardModelsModel::currentIndexChanged, &KeyboardModelsModel::currentIndexChanged,
[ & ]( int index ) [ & ]( int index )
{ {
// Set Xorg keyboard model
m_selectedModel = m_keyboardModelsModel->key( index ); m_selectedModel = m_keyboardModelsModel->key( index );
if ( m_useLocale1 ) somethingChanged();
{
locale1Apply();
}
else
{
QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) );
}
emit prettyStatusChanged();
} ); } );
connect( m_keyboardLayoutsModel, connect( m_keyboardLayoutsModel,
@ -194,16 +186,14 @@ Config::Config( QObject* parent )
[ & ]( int index ) [ & ]( int index )
{ {
m_selectedVariant = m_keyboardVariantsModel->key( index ); m_selectedVariant = m_keyboardVariantsModel->key( index );
xkbChanged(); somethingChanged();
emit prettyStatusChanged();
} ); } );
connect( m_KeyboardGroupSwitcherModel, connect( m_KeyboardGroupSwitcherModel,
&KeyboardGroupsSwitchersModel::currentIndexChanged, &KeyboardGroupsSwitchersModel::currentIndexChanged,
[ & ]( int index ) [ & ]( int index )
{ {
m_selectedGroup = m_KeyboardGroupSwitcherModel->key( index ); m_selectedGroup = m_KeyboardGroupSwitcherModel->key( index );
xkbChanged(); somethingChanged();
emit prettyStatusChanged();
} ); } );
// If the user picks something explicitly -- not a consequence of // If the user picks something explicitly -- not a consequence of
@ -223,30 +213,32 @@ Config::Config( QObject* parent )
} }
void void
Config::xkbChanged() Config::somethingChanged()
{ {
// Set Xorg keyboard layout + variant if ( m_applyTimer.isActive() )
if ( m_setxkbmapTimer.isActive() )
{ {
m_setxkbmapTimer.stop(); m_applyTimer.stop();
m_setxkbmapTimer.disconnect( this );
} }
m_applyTimer.start( QApplication::keyboardInputInterval() );
if ( m_useLocale1 )
{
connect( &m_setxkbmapTimer, &QTimer::timeout, this, &Config::locale1Apply );
}
else
{
connect( &m_setxkbmapTimer, &QTimer::timeout, this, &Config::xkbApply );
}
m_setxkbmapTimer.start( QApplication::keyboardInputInterval() );
emit prettyStatusChanged(); emit prettyStatusChanged();
} }
void void
Config::locale1Apply() Config::apply()
{
if ( m_configureXkb )
{
applyXkb();
}
if ( m_configureLocale1 )
{
applyLocale1();
}
// Writing /etc/ files is not needed "live"
}
void
Config::applyLocale1()
{ {
m_additionalLayoutInfo = getAdditionalLayoutInfo( m_selectedLayout ); m_additionalLayoutInfo = getAdditionalLayoutInfo( m_selectedLayout );
@ -282,10 +274,11 @@ Config::locale1Apply()
} }
void void
Config::xkbApply() Config::applyXkb()
{ {
m_additionalLayoutInfo = getAdditionalLayoutInfo( m_selectedLayout ); m_additionalLayoutInfo = getAdditionalLayoutInfo( m_selectedLayout );
QStringList basicArguments = xkbmap_model_args( m_selectedModel );
if ( !m_additionalLayoutInfo.additionalLayout.isEmpty() ) if ( !m_additionalLayoutInfo.additionalLayout.isEmpty() )
{ {
if ( !m_selectedGroup.isEmpty() ) if ( !m_selectedGroup.isEmpty() )
@ -302,11 +295,11 @@ Config::xkbApply()
m_additionalLayoutInfo.groupSwitcher = "grp:alt_shift_toggle"; m_additionalLayoutInfo.groupSwitcher = "grp:alt_shift_toggle";
} }
QProcess::execute( basicArguments.append(
"setxkbmap",
xkbmap_layout_args_with_group_switch( { m_additionalLayoutInfo.additionalLayout, m_selectedLayout }, xkbmap_layout_args_with_group_switch( { m_additionalLayoutInfo.additionalLayout, m_selectedLayout },
{ m_additionalLayoutInfo.additionalVariant, m_selectedVariant }, { m_additionalLayoutInfo.additionalVariant, m_selectedVariant },
m_additionalLayoutInfo.groupSwitcher ) ); m_additionalLayoutInfo.groupSwitcher ) );
QProcess::execute( "setxkbmap", basicArguments );
cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant << "(added " cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant << "(added "
<< m_additionalLayoutInfo.additionalLayout << "-" << m_additionalLayoutInfo.additionalVariant << m_additionalLayoutInfo.additionalLayout << "-" << m_additionalLayoutInfo.additionalVariant
@ -314,10 +307,11 @@ Config::xkbApply()
} }
else else
{ {
QProcess::execute( "setxkbmap", xkbmap_layout_args( m_selectedLayout, m_selectedVariant ) ); basicArguments.append( xkbmap_layout_args( m_selectedLayout, m_selectedVariant ) );
QProcess::execute( "setxkbmap", basicArguments );
cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant; cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant;
} }
m_setxkbmapTimer.disconnect( this ); m_applyTimer.stop();
} }
KeyboardModelsModel* KeyboardModelsModel*
@ -455,7 +449,7 @@ Config::detectCurrentKeyboardLayout()
QString currentVariant; QString currentVariant;
QString currentModel; QString currentModel;
if ( m_useLocale1 ) if ( m_configureLocale1 )
{ {
getCurrentKeyboardLayoutLocale1( currentLayout, currentVariant, currentModel ); getCurrentKeyboardLayoutLocale1( currentLayout, currentVariant, currentModel );
} }
@ -527,8 +521,8 @@ Config::createJobs()
m_additionalLayoutInfo, m_additionalLayoutInfo,
m_xOrgConfFileName, m_xOrgConfFileName,
m_convertedKeymapPath, m_convertedKeymapPath,
m_writeEtcDefaultKeyboard, m_configureEtcDefaultKeyboard,
m_useLocale1 ); m_configureLocale1 );
list.append( Calamares::job_ptr( j ) ); list.append( Calamares::job_ptr( j ) );
return list; return list;
@ -718,8 +712,8 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
m_xOrgConfFileName = xorgConfDefault; m_xOrgConfFileName = xorgConfDefault;
} }
m_convertedKeymapPath = getString( configurationMap, "convertedKeymapPath" ); m_convertedKeymapPath = getString( configurationMap, "convertedKeymapPath" );
m_writeEtcDefaultKeyboard = getBool( configurationMap, "writeEtcDefaultKeyboard", true ); m_configureEtcDefaultKeyboard = getBool( configurationMap, "writeEtcDefaultKeyboard", true );
m_useLocale1 = getBool( configurationMap, "useLocale1", !isX11 ); m_configureLocale1 = getBool( configurationMap, "useLocale1", !isX11 );
m_guessLayout = getBool( configurationMap, "guessLayout", true ); m_guessLayout = getBool( configurationMap, "guessLayout", true );
} }

View File

@ -86,14 +86,16 @@ private:
* keyboard layout. This introduces a slight delay between selecting * keyboard layout. This introduces a slight delay between selecting
* a keyboard, and applying it to the system -- so that if you * a keyboard, and applying it to the system -- so that if you
* scroll through or down-arrow through the list of keyboards, * scroll through or down-arrow through the list of keyboards,
* you don't get buried under xkbset processes. * you don't get buried under updates which might take some time.
* *
* xkbChanged() is called when the selection changes, and triggers * somethingChanged() is called when the selection changes, and triggers
* a delayed call to xkbApply() which does the actual work. * a delayed call to apply() which does the actual work by calling the
* relevant apply*() functions.
*/ */
void xkbChanged(); void somethingChanged();
void xkbApply(); void apply();
void locale1Apply(); void applyLocale1();
void applyXkb();
void getCurrentKeyboardLayoutXkb( QString& currentLayout, QString& currentVariant, QString& currentModel ); void getCurrentKeyboardLayoutXkb( QString& currentLayout, QString& currentVariant, QString& currentModel );
void getCurrentKeyboardLayoutLocale1( QString& currentLayout, QString& currentVariant, QString& currentModel ); void getCurrentKeyboardLayoutLocale1( QString& currentLayout, QString& currentVariant, QString& currentModel );
@ -111,13 +113,14 @@ private:
// Layout (and corresponding info) added if current one doesn't support ASCII (e.g. Russian or Japanese) // Layout (and corresponding info) added if current one doesn't support ASCII (e.g. Russian or Japanese)
AdditionalLayoutInfo m_additionalLayoutInfo; AdditionalLayoutInfo m_additionalLayoutInfo;
QTimer m_setxkbmapTimer; QTimer m_applyTimer;
// From configuration // From configuration
QString m_xOrgConfFileName; QString m_xOrgConfFileName;
QString m_convertedKeymapPath; QString m_convertedKeymapPath;
bool m_writeEtcDefaultKeyboard = true; bool m_configureXkb = true;
bool m_useLocale1 = false; bool m_configureEtcDefaultKeyboard = true;
bool m_configureLocale1 = false;
bool m_guessLayout = false; bool m_guessLayout = false;
// The state determines whether we guess settings or preserve them: // The state determines whether we guess settings or preserve them: