title: X11/Xlib: virtual keyboard input and keyboard mapping synchronization issue
body: <p>For an automated test application I have to simulate large amount of unicode keyboard input into an old X11 application (of which I don't have any source access).
  My program takes the input from an UCS-2 LE encoded input stream via stdin and the basic operation is as follows:</p>
  
  <ol>
  <li>Save current keyboard layout and lock modifiers (<code>XDisplayKeycodes</code>, <code>XGetKeyboardMapping</code>, <code>XkbGetState</code>)</li>
  <li>Unlock active modifiers (<code>XkbLockModifiers</code>)</li>
  <li>Disable all X11 slave keyboard devices via Xinput2 extension</li>
  <li>Read input into a key press queue until <code>n</code> unique symbols are encountered, where <code>n</code> is the number of possible keycodes as returned by <code>XDisplayKeycodes</code>.</li>
  <li>Map these <code>n</code> unique X11 KeySyms via <code>XChangeKeyboardMapping</code> on the <code>n</code> available KeyCodes</li>
  <li>Type the correct KeyCodes for all enqueued KeySyms via <code>XTestFakeKeyEvent</code></li>
  <li>Clear the queue and continue at 4.) until no input is available</li>
  <li>Reactivate keyboards and restore initial modifiers and mappings</li>
  </ol>
  
  <p>Basically this system works better and much more performant than any virtual X11 key input tool I've seen so far.</p>
  
  <p>However, there is an issue I can currently only fix using ugly delays:</p>
  
  <p>As any other X11 application, the target application receives a <code>MappingNotify</code> (request==Keyboard) event from the X server after my application succeeded in changing the keyboard mapping table.
  The usual response of a X11 client is to call <code>XRefreshKeyboardMapping</code> to update Xlib's knowledge of the new keyboard layout.</p>
  
  <p>Now if the the client has some lag processing its X11 event queue, the <code>XRefreshKeyboardMapping</code> call might return a too recent mapping that is already some generations too far in the future.
  E.g. my input generator has already done the fourth <code>XChangeKeyboardMapping</code> when the target application just arrived at handling the second <code>MappingNotify</code> event in its XEvent queue handler.
  Actually it should get the second generation of the map, which isn't available at the X server anymore at that time.</p>
  
  <p>Unfortunately there is no map id or version of any kind in the keyboard <code>MappingNotify</code> event so that <code>XRefreshKeyboardMapping</code> could refer to a specific map ... and the X server does not seem to keep a history either.</p>
  
  <p>The result is that the X11 application's <code>KeyCode</code> to <code>KeySym</code> conversion operates with an invalid layout and generates wrong KeySyms.</p>
  
  <p>So basically I have to wait until all clients (or at least the one with the input focus) have requested and received my last <code>XChangeKeyboardMapping</code> map before I am allowed to do the next <code>XChangeKeyboardMapping</code>.</p>
  
  <p>I can fix 99.9% of the errors using a delay before <code>XChangeKeyboardMapping</code> and that delay is calculated by some ugly witchcraft (amount of key strokes etc.) and is way to high if 100% accuracy has to be achieved.</p>
  
  <p>So my question is if there is any way to programmatically be notified or to check if a X11 client has completed <code>XRefreshKeyboardMapping</code> or if its map is in sync with the server map?</p>
  
  <p>If not, is there a way to get another X11 client's current mapping via xlib (to check if the map is current)?</p>
  
  <p>Thanks for any hints!</p>