Bug 435191 - keyboard layout of previous session is always loaded on start (X11)
Summary: keyboard layout of previous session is always loaded on start (X11)
Status: CONFIRMED
Alias: None
Product: systemsettings
Classification: Applications
Component: kcm_keyboard (show other bugs)
Version: 5.21.3
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Plasma Bugs List
URL: https://invent.kde.org/plasma/plasma-...
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-31 16:03 UTC by Domenico Panella
Modified: 2021-06-19 14:11 UTC (History)
4 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Domenico Panella 2021-03-31 16:03:33 UTC
SUMMARY

Loading the keyboard layout without respect the exact order.

STEPS TO REPRODUCE
1. Go in System Settings -> Input devices -> Keyboard layout
2. For example, you add two keyboard layouts "it" (top of the list) and "us". 
3. Reboot the machine 

OBSERVED RESULT

I see the "us" keyboard layout loaded.

EXPECTED RESULT

I should see the "it" keyboard layout loaded because it is at the top of the list.

SOFTWARE/OS VERSIONS
KDE Plasma Version: 5.21.3
KDE Frameworks Version: 5.80.0
Qt Version: 5.15.2
Kernel Version: 5.10.26_1
Comment 1 Andrey 2021-03-31 16:09:12 UTC
It probably because you have layout saving feature working. KWin saves layout along with the session.
If you don't need this, set "Start new sesssion" in session settings, but then you won't get your session restored.

Sorry if it's not intuitive. It was designed such way to not introduce new settings.
Comment 2 Domenico Panella 2021-03-31 16:13:05 UTC
I have "Start a new session" in my settings.
Comment 3 Andrey 2021-03-31 16:22:06 UTC
Oh, sorry, what I said above belong to Wayland session only. Please check it works that way.
For X11 session, I don't remember if that feature can be disabled at all.
As a workaround, you could configure layouts in Xorg config and not in the Keyboard KCM.
Comment 4 Domenico Panella 2021-04-01 07:45:05 UTC
I did this test:
I created a new user and logged in with Wayland. 
I created the same keyboard layouts
and it also worked after reboot and poweroff command.
I logged in with X11 as my user and now it also worked.
I don't know what is happened but at the moment it's working fine.
I close this bug as "Not a bug"
Comment 5 Andrey 2021-04-01 12:49:02 UTC
Could you please check if the last used layout restores for you on Wayland when "Restore last session" is set?
Comment 6 Domenico Panella 2021-04-01 13:35:28 UTC
(In reply to Andrey from comment #5)
> Could you please check if the last used layout restores for you on Wayland
> when "Restore last session" is set?

I checked that, on Wayland it restore fine while on X11 not restore last layout setted. Moreover, i set "Start new session" with these layout:
English (us) (top)
Italian (it).
I expected the English keyboard layout set while the italian is set instead.
Anyway, on X11 it not works properly as Wayland.
Comment 7 Domenico Panella 2021-04-01 13:41:38 UTC
According you, where is the problem? i see plasma-desktop/applets/keyboardlayout
(is it plasmoid?) , plasma-workspace/components/keyboardlayout or anything else ?

I could check that if you guide me.
Comment 8 Andrey 2021-04-01 15:19:15 UTC
Please make sure you have "Global switching policy" for layouts in Keyboard KCM.
I remember last used layout saving feature worked for X11 too with this policy when I first started to implement it for Wayland.
IIRC, for "Application switching policy" X11 might have problems with the saving (still needs to be confirmed, it might be for "Windows" policy instead) but Wayland should work nevertheless. For "Global" policy it worked everywhere.

If you want to check the code yourself, here is the class responsible for layout saving in X11:
https://lxr.kde.org/source/kde/workspace/plasma-desktop/kcms/keyboard/layout_memory_persister.h
Comment 9 Andrey 2021-04-01 15:22:53 UTC
Also please note the dependency on "Restore last session" exists for Wayland only - for X11, IIRC it should save unconditionally.
Comment 10 Domenico Panella 2021-04-03 09:49:17 UTC
X11

From my tests, the problem seems to be ".local/share/kded5/keyboard/session/layout_memory.xml"
Option: "Start new session"
Layout: "it,us"
The layout defined in layout_memory.xml is always loaded.
Is it correct? I expected the "it" layout is loaded because it is on top of the list.
For some reason i have this file content:

<!DOCTYPE LayoutMap>
<LayoutMap version="1.0" SwitchMode="Global">
	<item currentLayout="us"/>
</LayoutMap>

Now, If i change currentLayout from "us" to "it", it works fine!

WAYLAND

All works fine except when we restore the session.
Some applications are not restored.
I didn't see problems about keyboard layout instead.


Questions:

Option: "Restore previously saved session"
Switch Policy: "Application, Windows, Desktop"

Should it remember the keyboard layout for the applications?
Same question for desktop and window.
Comment 11 Andrey 2021-04-03 16:40:58 UTC
(In reply to Domenico Panella from comment #10)
> X11
> 
> From my tests, the problem seems to be
> ".local/share/kded5/keyboard/session/layout_memory.xml"
> Option: "Start new session"
> Layout: "it,us"
> The layout defined in layout_memory.xml is always loaded.
> Is it correct? I expected the "it" layout is loaded because it is on top of
> the list.

That doesn't matter as long as last used layout is saved.

> For some reason i have this file content:
> 
> <!DOCTYPE LayoutMap>
> <LayoutMap version="1.0" SwitchMode="Global">
> 	<item currentLayout="us"/>
> </LayoutMap>
> 
> Now, If i change currentLayout from "us" to "it", it works fine!
> 
That's where is was saved probably, as it should.
Are you sure "us" wasn't your last used layout?
Try to experiment with it a bit more.

> WAYLAND
> 
> All works fine except when we restore the session.
> Some applications are not restored.
> I didn't see problems about keyboard layout instead.
> 
> 
> Questions:
> 
> Option: "Restore previously saved session"
> Switch Policy: "Application, Windows, Desktop"
> 
> Should it remember the keyboard layout for the applications?
> Same question for desktop and window.
Yes, for all Switching Policies, layout should restore accordingly.
Just for "Windows" Policy it's not fully implemented and works the same as for App policy.
That belong to Wayland. For X11, I don't remember if saving layout for Application/Windows Policies work there.
Please note for Wayland, layouts are saved in ~/.config/kxkbrc instead.
Comment 12 Andrey 2021-04-03 16:47:29 UTC
(In reply to Andrey from comment #11)
> (In reply to Domenico Panella from comment #10)
> > X11
> > 
> > From my tests, the problem seems to be
> > ".local/share/kded5/keyboard/session/layout_memory.xml"
> > Option: "Start new session"
> > Layout: "it,us"
> > The layout defined in layout_memory.xml is always loaded.
> > Is it correct? I expected the "it" layout is loaded because it is on top of
> > the list.
> 
> That doesn't matter as long as last used layout is saved.
I mean, it's correct because as I said above, Option: "Start new session", etc. is irrelevant for layout saving on X11.
Comment 13 Andrey 2021-04-03 16:50:47 UTC
If you have some basic C/C++ skills, you could make it work the same way as on Wayland, honoring Session restore option ;)
I can guide you for the details.
Comment 14 Domenico Panella 2021-04-04 09:33:46 UTC
(In reply to Andrey from comment #13)
> If you have some basic C/C++ skills, you could make it work the same way as
> on Wayland, honoring Session restore option ;)
> I can guide you for the details.

On wayland, there are not problems about keyboard layout but 
the applications are not correctly restored.
That works fine on X11 instead.
Maybe that is another bug !?
Yes, i could do that but before starting i'd want to ask some info.
How can i contact you?
Comment 15 Andrey 2021-04-04 10:38:44 UTC
Problems with session apps restore are irrelative to this, of course it's another "bug". There was work on that, too.
You can access to me by email directly, if that won't be enough we'll decide further.
Comment 16 Andrey 2021-04-05 01:11:02 UTC
Please report Wayland session restore problems to a separate bug and ping vlad.zahorodnii@kde.org and me there.
Thanks.
Comment 17 Andrey 2021-04-05 10:52:40 UTC
I'll post my email reply here for the reference:

For restore session problem let's file an separate bug and cooperate with Vlad there because he was doing session things on Wayland.


About layout saving, here you can see

layoutMemoryPersister.restore() and layoutMemoryPersister.save() are called within constructor/destructor:

https://lxr.kde.org/source/kde/workspace/plasma-desktop/kcms/keyboard/keyboard_daemon.cpp#0065
https://lxr.kde.org/source/kde/workspace/plasma-desktop/kcms/keyboard/keyboard_daemon.cpp#0076

That is why it always restores last layout used on X11 (if you have tick "configure layout" set in KCM).


In Wayland, the restore occurs on SessionManager::loadSessionRequested() signal instead:

https://invent.kde.org/plasma/kwin/-/merge_requests/38/diffs#26c78828a6ff7d1951c40f07b43aae8637a84588_72_102

You could try to simulate that behavior on X11.

Please add me to MR if that happens.
Comment 18 Nate Graham 2021-04-05 16:25:50 UTC
So what's the status of this? is everything working as expected on X11?
Comment 19 Andrey 2021-04-05 18:15:46 UTC
It's otherwise - keyboard things (what is this issue about), - are all as expected on Wayland.
It would need some efforts to bring that functionality to X11 part.

It seems like Domenico is willing to participate in the developing.
He will create an issue on Gitlab and we could continue there to guide him.
Welcome Domenico!
Comment 20 Nate Graham 2021-04-05 20:01:27 UTC
So the bug we're tracking here is that on X11, the last-used keyboard layout is unpredictible, and we want to move it to being saved by the session restore functionality, as it is on Wayland? I just want to make sure I understand.
Comment 21 Andrey 2021-04-05 20:30:17 UTC
On X11, the last used layout is always restored, and there is no way to explicitly force exact predefined layout on start.
On Wayland, it's possible by setting "Start new session" (vs "Restore previous session") - then topmost layout set in KCM will always take place on start.

If I understand correctly, that missing functionality on X11 is the first thing wanted here.
Comment 22 Andrey 2021-04-05 20:34:24 UTC
So saving last used layout feature is working on both platforms in some degree but it's only X11 where it can't be disabled.
Comment 23 Nate Graham 2021-04-05 21:32:56 UTC
I see, so this is working as (currently) expected for each platform.
Comment 24 Domenico Panella 2021-04-06 08:19:24 UTC
Just I finished the other tests, i'll open an issue on Gitlab
Comment 25 Andrey 2021-04-06 17:40:58 UTC
As it more feature request rather than a bug, the issue on Gitlab was opened for further discussion:
https://invent.kde.org/plasma/plasma-desktop/-/issues/18
Comment 26 Domenico Panella 2021-04-12 16:03:29 UTC
A possible MR is https://invent.kde.org/plasma/plasma-desktop/-/merge_requests/416
Comment 27 Nate Graham 2021-04-12 23:20:17 UTC
Re-opening per https://bugs.kde.org/show_bug.cgi?id=431481#c15.
Comment 28 Andrey 2021-04-15 17:02:40 UTC
Nate, don't you know if we could have X11-specific Keywoards or Flags here?
Comment 29 Nate Graham 2021-04-15 18:33:31 UTC
None that are really in common use. Simply mentioning X11 in the title is probably enough.
Comment 30 Stephen Bosch 2021-06-14 21:02:47 UTC
(In reply to Domenico Panella from comment #10)
> X11
> 
> From my tests, the problem seems to be
> ".local/share/kded5/keyboard/session/layout_memory.xml"
> Option: "Start new session"
> Layout: "it,us"
> The layout defined in layout_memory.xml is always loaded.
> Is it correct? I expected the "it" layout is loaded because it is on top of
> the list.
> For some reason i have this file content:
> 
> <!DOCTYPE LayoutMap>
> <LayoutMap version="1.0" SwitchMode="Global">
> 	<item currentLayout="us"/>
> </LayoutMap>
> 
> Now, If i change currentLayout from "us" to "it", it works fine!

I am observing exactly the same issue in X11. In my case, I wanted it to use "de", but this file contains "us" and is unaffected by any changes I make to the configuration (short of removing the US keyboard layout entirely).
Comment 31 Stephen Bosch 2021-06-19 14:11:46 UTC
(In reply to Stephen Bosch from comment #30)
> (In reply to Domenico Panella from comment #10)
> > X11
> > 
> > From my tests, the problem seems to be
> > ".local/share/kded5/keyboard/session/layout_memory.xml"
> > Option: "Start new session"
> > Layout: "it,us"
> > The layout defined in layout_memory.xml is always loaded.
> > Is it correct? I expected the "it" layout is loaded because it is on top of
> > the list.
> > For some reason i have this file content:
> > 
> > <!DOCTYPE LayoutMap>
> > <LayoutMap version="1.0" SwitchMode="Global">
> > 	<item currentLayout="us"/>
> > </LayoutMap>
> > 
> > Now, If i change currentLayout from "us" to "it", it works fine!
> 
> I am observing exactly the same issue in X11. In my case, I wanted it to use
> "de", but this file contains "us" and is unaffected by any changes I make to
> the configuration (short of removing the US keyboard layout entirely).

If I manually set it by editing the layout_memory.xml, it sticks - for a time. I think it gets changed again if I then use the keyboard switcher at any time afterwards, and it doesn't seem to matter which other keyboard I use.

And it also doesn't matter what keyboard was set when the session was ended. It always picks 'us'.

My workaround for now is to set the file read-only (400).