This brings up the "not noticing the layout switch until the next keypress" problem though. I don't know how important it is but it might be a point in favor of listening to Xkb events when available.
This seems to be a GNOME specific problem, possibly their bug, and maybe we don't have to worry about it for now.
That was with KDE though. As I understand it it's more a matter of what layouts end up in the same keymap and whether you happen to switch between layouts represented by the same keymap or not. I.e., with `pc+us+ru:2+inet(evdev)` I can switch between "us" and "ru" freely without ever triggering a MappingNotify, then a switch to "it" will cause MappingNotify to be sent and the keymap to become `pc+us+it:2+inet(evdev)`. At that point I can instead flip between "it" and "us" without any MappingNotify.
Also, I'm not sure that using Xkb events would make any difference, have you checked that it does?
Yeah, XkbStateNotify is called immediately every time the layout changes but the underlying keymap doesn't (i.e. it's just an Xkb group switch), while XkbNewKeyboardNotify is triggered when the keymap does change. Both seem to work consistently on Gnome X11 and Xwayland, KDE X11. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/10550#note_135708