10f8248bfSmrg<?xml version="1.0" encoding="UTF-8" ?>
20f8248bfSmrg<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
30f8248bfSmrg	  "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
4eb411b4bSmrg<chapter id='Replacing_a_Keyboard_On_the_Fly'>
50f8248bfSmrg<title>Replacing a Keyboard <quote>On the Fly</quote></title>
6e9fcaa8aSmrg
7e9fcaa8aSmrg<para>
80f8248bfSmrgSome operating system and X server implementations allow
90f8248bfSmrg<quote>hot plugging</quote> of
10e9fcaa8aSmrginput devices. When using these implementations, input devices can be unplugged
11e9fcaa8aSmrgand new ones plugged in without restarting the software that is using those
12e9fcaa8aSmrgdevices. There is no provision in the standard X server for notification of
13e9fcaa8aSmrgclient programs if input devices are unplugged and/or new ones plugged in. In
14e9fcaa8aSmrgthe case of the X keyboard, this could result in the X server having a keymap
15e9fcaa8aSmrgthat does not match the new keyboard.
16e9fcaa8aSmrg</para>
17e9fcaa8aSmrg
18e9fcaa8aSmrg
19e9fcaa8aSmrg<para>
20e9fcaa8aSmrgIf the X server implementation supports the X input device extension, a client
21e9fcaa8aSmrgprogram may also change the X keyboard programmatically. The
22e9fcaa8aSmrgXChangeKeyboardDevice input extension request allows a client to designate an
23e9fcaa8aSmrginput extension keyboard device as the X keyboard, in which case the old X
24e9fcaa8aSmrgkeyboard device becomes inaccessible except via the input device extension. In
250f8248bfSmrgthis case, core protocol
260f8248bfSmrg<symbol>MappingNotify</symbol>
270f8248bfSmrgand input extension
280f8248bfSmrg<symbol>XChangeDeviceNotify</symbol>
290f8248bfSmrgevents are generated to notify all clients that a new keyboard with a new
30e9fcaa8aSmrgkeymap has been designated.
31e9fcaa8aSmrg</para>
32e9fcaa8aSmrg
33e9fcaa8aSmrg
34e9fcaa8aSmrg<para>
35e9fcaa8aSmrgWhen a client opens a connection to the X server, the server reports the
36e9fcaa8aSmrgminimum and maximum keycodes. The server keeps track of the minimum and maximum
37e9fcaa8aSmrgkeycodes last reported to each client. When delivering events to a particular
38e9fcaa8aSmrgclient, the server filters out any events that fall outside of the valid range
39e9fcaa8aSmrgfor the client.
40e9fcaa8aSmrg</para>
41e9fcaa8aSmrg
42e9fcaa8aSmrg
43e9fcaa8aSmrg<para>
440f8248bfSmrgXkb provides an
450f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
460f8248bfSmrgevent that reports a change in keyboard geometry and/or the range of supported
470f8248bfSmrgkeycodes. The server can generate an
480f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
490f8248bfSmrgevent when it detects a new keyboard or in response to an
500f8248bfSmrg<function>XkbGetKeyboardByName</function>
510f8248bfSmrgrequest that loads a new keyboard description. Selecting for
520f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
530f8248bfSmrgevents allows Xkb-aware clients to be notified whenever a keyboard change
54e9fcaa8aSmrgoccurs that may affect the keymap.
55e9fcaa8aSmrg</para>
56e9fcaa8aSmrg
57e9fcaa8aSmrg
58e9fcaa8aSmrg<para>
590f8248bfSmrgWhen a client requests
600f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
610f8248bfSmrgevents, the server compares the range of keycodes for the current keyboard to
62e9fcaa8aSmrgthe range of keycodes that are valid for the client. If they are not the same,
630f8248bfSmrgthe server immediately sends the client an
640f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
650f8248bfSmrgevent. Even if the <quote>new</quote> keyboard is not new to the server,
660f8248bfSmrgit is new to this particular client.
67e9fcaa8aSmrg</para>
68e9fcaa8aSmrg
69e9fcaa8aSmrg
70e9fcaa8aSmrg<para>
710f8248bfSmrgWhen the server sends an
720f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
730f8248bfSmrgevent to a client to inform it of a new keycode range, it resets the stored
74e9fcaa8aSmrgrange of legal keycodes for the client to the keycode range reported in the
75e9fcaa8aSmrgevent; it does not reset this range for the client if it does not sent an
760f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
770f8248bfSmrgevent to a client. Because Xkb-unaware clients and Xkb-aware clients that do
780f8248bfSmrgnot request
790f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
800f8248bfSmrgevents are never sent these events, the server’s notion of the legal keycode
81e9fcaa8aSmrgrange never changes, and these clients never receive events from keys that fall
82e9fcaa8aSmrgoutside of their notion of the legal keycode range.
83e9fcaa8aSmrg</para>
84e9fcaa8aSmrg
85e9fcaa8aSmrg
86e9fcaa8aSmrg<para>
870f8248bfSmrgClients that have not selected to receive
880f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
890f8248bfSmrgevents do, however, receive the
900f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
910f8248bfSmrgevent when a keyboard change occurs. Clients that have not selected to receive
92e9fcaa8aSmrgthis event also receive numerous other events detailing the individual changes
93e9fcaa8aSmrgthat occur when a keyboard change occurs.
94e9fcaa8aSmrg</para>
95e9fcaa8aSmrg
96e9fcaa8aSmrg
97e9fcaa8aSmrg<para>
980f8248bfSmrgClients wishing to track changes in
990f8248bfSmrg<structfield>min_key_code</structfield>
1000f8248bfSmrgand
1010f8248bfSmrg<structfield>max_key_code</structfield>
1020f8248bfSmrgmust watch for both
1030f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
1040f8248bfSmrgand
1050f8248bfSmrg<symbol>XkbMapNotify</symbol>
1060f8248bfSmrgevents, because a simple mapping change causes an
1070f8248bfSmrg<symbol>XkbMapNotify</symbol>
1080f8248bfSmrgevent and may change the range of valid keycodes, but does not cause an
1090f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
1100f8248bfSmrgevent. If a client does not select for
1110f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
1120f8248bfSmrgevents, the server restricts the range of keycodes reported to the client.
113e9fcaa8aSmrg</para>
114e9fcaa8aSmrg
115e9fcaa8aSmrg
116e9fcaa8aSmrg<para>
117e9fcaa8aSmrgIn addition to filtering out-of-range key events, Xkb:
118e9fcaa8aSmrg</para>
119e9fcaa8aSmrg
120e9fcaa8aSmrg<itemizedlist>
121e9fcaa8aSmrg<listitem>
122e9fcaa8aSmrg  <para>
1230f8248bfSmrgAdjusts core protocol
1240f8248bfSmrg<symbol>MappingNotify</symbol>
1250f8248bfSmrgevents to refer only to keys that match the stored legal range.
126e9fcaa8aSmrg  </para>
127e9fcaa8aSmrg</listitem>
128e9fcaa8aSmrg<listitem>
129e9fcaa8aSmrg  <para>
130e9fcaa8aSmrgReports keyboard mappings for keys that match the stored legal range to clients
1310f8248bfSmrgthat issue a core protocol
1320f8248bfSmrg<systemitem>GetKeyboardMapping</systemitem>
1330f8248bfSmrgrequest.
134e9fcaa8aSmrg  </para>
135e9fcaa8aSmrg</listitem>
136e9fcaa8aSmrg<listitem>
137e9fcaa8aSmrg  <para>
138e9fcaa8aSmrgReports modifier mappings only for keys that match the stored legal range to
1390f8248bfSmrgclients that issue a core protocol
1400f8248bfSmrg<systemitem>GetModifierMapping</systemitem>
1410f8248bfSmrgrequest.
142e9fcaa8aSmrg  </para>
143e9fcaa8aSmrg</listitem>
144e9fcaa8aSmrg<listitem>
145e9fcaa8aSmrg  <para>
1460f8248bfSmrgRestricts the core protocol
1470f8248bfSmrg<systemitem>ChangeKeyboardMapping</systemitem>
1480f8248bfSmrgand
1490f8248bfSmrg<systemitem>SetModifierMapping</systemitem>
1500f8248bfSmrgrequests to keys that fall inside the stored legal range.
151e9fcaa8aSmrg  </para>
152e9fcaa8aSmrg</listitem>
153e9fcaa8aSmrg</itemizedlist>
154e9fcaa8aSmrg
155e9fcaa8aSmrg<para>
156e9fcaa8aSmrgIn short, Xkb does everything possible to hide from Xkb-unaware clients the
157e9fcaa8aSmrgfact that the range of legal keycodes has changed, because such clients cannot
158e9fcaa8aSmrgbe expected to deal with them. Xkb events and requests are not modified in this
159e9fcaa8aSmrgmanner; all Xkb events report the full range of legal keycodes. No requested
160e9fcaa8aSmrgXkb events are discarded, and no Xkb requests have their keycode range clamped.
161e9fcaa8aSmrg</para>
162e9fcaa8aSmrg
1630f8248bfSmrg<indexterm significance="preferred" zone="Replacing_a_Keyboard_On_the_Fly">
1640f8248bfSmrg<primary>events</primary><secondary><symbol>XkbNewKeyboardNotify</symbol></secondary></indexterm>
1650f8248bfSmrg<indexterm significance="preferred" zone="Replacing_a_Keyboard_On_the_Fly">
1660f8248bfSmrg<primary><structname>XkbNewKeyboardNotifyEvent</structname></primary></indexterm>
167e9fcaa8aSmrg
168e9fcaa8aSmrg<para>
1690f8248bfSmrgThe structure for the
1700f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>
1710f8248bfSmrgevent is defined as follows:
172e9fcaa8aSmrg
1730f8248bfSmrg<programlisting>
174e9fcaa8aSmrgtypedef struct _XkbNewKeyboardNotify {
1750f8248bfSmrg    int            type;         /* Xkb extension base event code */
1760f8248bfSmrg    unsigned long  serial;       /* X server serial number for event */
1770f8248bfSmrg    Bool           send_event;   /* <symbol>True</symbol> &rArr; synthetically generated */
1780f8248bfSmrg    Display *      display;      /* server connection where event generated */
1790f8248bfSmrg    Time           time;         /* server time when event generated */
1800f8248bfSmrg    int            xkb_type;     /* <symbol>XkbNewKeyboardNotify</symbol> */
1810f8248bfSmrg    int            device;       /* device ID of new keyboard */
1820f8248bfSmrg    int            old_device;   /* device ID of old keyboard */
1830f8248bfSmrg    int            min_key_code; /* min keycode of new keyboard */
1840f8248bfSmrg    int            max_key_code; /* max keycode of new keyboard */
1850f8248bfSmrg    int            old_min_key_code; /* min keycode of old keyboard */
1860f8248bfSmrg    int            old_max_key_code; /* max keycode of old keyboard */
1870f8248bfSmrg    unsigned int   changed;      /* changed aspects - see masks below */
1880f8248bfSmrg    char           req_major;    /* major request that caused change */
1890f8248bfSmrg    char           req_minor;    /* minor request that caused change */
1900f8248bfSmrg} <structname>XkbNewKeyboardNotifyEvent</structname>;
191e9fcaa8aSmrg</programlisting></para>
192e9fcaa8aSmrg
193e9fcaa8aSmrg<para>
1940f8248bfSmrgTo receive name notify events, use
1950f8248bfSmrg<function>XkbSelectEvents</function>
1960f8248bfSmrg(see <link linkend="Selecting_Xkb_Events">section 4.3</link>) with
1970f8248bfSmrg<symbol>XkbNewKeyboardNotifyMask</symbol>
1980f8248bfSmrgin both the
1990f8248bfSmrg<parameter>bits_to_change</parameter>
2000f8248bfSmrgand
2010f8248bfSmrg<parameter>values_for_bits</parameter>
2020f8248bfSmrgparameters. To receive events for only specific names, use
2030f8248bfSmrg<function>XkbSelectEventDetails</function>.
2040f8248bfSmrgSet the
2050f8248bfSmrg<structfield>event_type</structfield>
2060f8248bfSmrgparameter to
2070f8248bfSmrg<symbol>XkbNewKeyboardNotify</symbol>,
2080f8248bfSmrgand set both the
2090f8248bfSmrg<parameter>bits_to_change</parameter>
2100f8248bfSmrgand
2110f8248bfSmrg<parameter>values_for_bits</parameter>
2120f8248bfSmrgdetail parameter to a mask composed of a bitwise OR of masks in
2130f8248bfSmrg<link linkend="table19.1">Table 19.1</link>.
214e9fcaa8aSmrg</para>
215e9fcaa8aSmrg
2160f8248bfSmrg<table id='table19.1' frame='topbot'>
217e9fcaa8aSmrg<title>XkbNewKeyboardNotifyEvent Details</title>
218eb411b4bSmrg<?dbfo keep-together="always" ?>
219eb411b4bSmrg<tgroup cols='3' align='left' colsep='0' rowsep='0'>
220eb411b4bSmrg<colspec colname='c1' colwidth='2.0*'/>
221eb411b4bSmrg<colspec colname='c2' colwidth='1.0*'/>
222eb411b4bSmrg<colspec colname='c3' colwidth='2.0*'/>
223e9fcaa8aSmrg<thead>
224e9fcaa8aSmrg<row rowsep='1'>
225e9fcaa8aSmrg  <entry>XkbNewKeyboardNotify Event Details</entry>
226e9fcaa8aSmrg  <entry>Value</entry>
227e9fcaa8aSmrg  <entry>Circumstances</entry>
228e9fcaa8aSmrg  </row>
229e9fcaa8aSmrg</thead>
230e9fcaa8aSmrg<tbody>
231eb411b4bSmrg  <row>
2320f8248bfSmrg    <entry><symbol>XkbNKN_KeycodesMask</symbol></entry>
233e9fcaa8aSmrg    <entry>(1L&lt;&lt;0)</entry>
234e9fcaa8aSmrg    <entry>Notification of keycode range changes wanted</entry>
235e9fcaa8aSmrg  </row>
236eb411b4bSmrg  <row>
2370f8248bfSmrg    <entry><symbol>XkbNKN_GeometryMask</symbol></entry>
238e9fcaa8aSmrg    <entry>(1L&lt;&lt;1)</entry>
239e9fcaa8aSmrg    <entry>Notification of geometry changes wanted</entry>
240e9fcaa8aSmrg  </row>
241eb411b4bSmrg  <row>
2420f8248bfSmrg    <entry><symbol>XkbNKN_DeviceIDMask</symbol></entry>
243e9fcaa8aSmrg    <entry>(1L&lt;&lt;2)</entry>
244e9fcaa8aSmrg    <entry>Notification of device ID changes wanted</entry>
245e9fcaa8aSmrg  </row>
246eb411b4bSmrg  <row>
2470f8248bfSmrg    <entry><symbol>XkbAllNewKeyboardEventsMask</symbol></entry>
248e9fcaa8aSmrg    <entry>(0x7)</entry>
249e9fcaa8aSmrg    <entry>Includes all of the above masks</entry>
250e9fcaa8aSmrg  </row>
251e9fcaa8aSmrg</tbody>
252e9fcaa8aSmrg</tgroup>
253e9fcaa8aSmrg</table>
254e9fcaa8aSmrg
255e9fcaa8aSmrg<para>
2560f8248bfSmrgThe
2570f8248bfSmrg<structfield>req_major</structfield>
2580f8248bfSmrgand
2590f8248bfSmrg<structfield>req_minor</structfield>
2600f8248bfSmrgfields indicate what type of keyboard change has occurred.
261e9fcaa8aSmrg</para>
262e9fcaa8aSmrg
263e9fcaa8aSmrg
264e9fcaa8aSmrg<para>
2650f8248bfSmrgIf
2660f8248bfSmrg<structfield>req_major</structfield>
2670f8248bfSmrgand
2680f8248bfSmrg<structfield>req_minor</structfield>
2690f8248bfSmrgare zero, the device change was not caused by a software request to the server
270e9fcaa8aSmrg— a spontaneous change has occurred, such as hot-plugging a new device. In
2710f8248bfSmrgthis case,
2720f8248bfSmrg<structfield>device</structfield>
2730f8248bfSmrgis the device identifier for the new, current X keyboard device, but no
2740f8248bfSmrgimplementation-independent guarantee can be made about
2750f8248bfSmrg<structfield>old_device</structfield>.
2760f8248bfSmrg<structfield>old_device</structfield>
2770f8248bfSmrgmay be identical to
2780f8248bfSmrg<structfield>device</structfield>
2790f8248bfSmrg(an implementor is permitted to reuse the device specifier when the device
2800f8248bfSmrgchanges); or it may be different. Note that
2810f8248bfSmrg<structfield>req_major</structfield>
2820f8248bfSmrgand
2830f8248bfSmrg<structfield>req_minor</structfield>
2840f8248bfSmrgbeing zero do not necessarily mean that the physical keyboard device has
285e9fcaa8aSmrgchanged; rather, they only imply a spontaneous change outside of software
286e9fcaa8aSmrgcontrol (some systems have keyboards that can change personality at the press
287e9fcaa8aSmrgof a key).
288e9fcaa8aSmrg</para>
289e9fcaa8aSmrg
290e9fcaa8aSmrg
291e9fcaa8aSmrg<para>
2920f8248bfSmrgIf the keyboard change is the result of an X Input Extension
2930f8248bfSmrg<systemitem>ChangeKeyboardDevice</systemitem>
2940f8248bfSmrgrequest,
2950f8248bfSmrg<structfield>req_major</structfield>
2960f8248bfSmrgcontains the input extension major opcode, and
2970f8248bfSmrg<structfield>req_minor</structfield>
2980f8248bfSmrgcontains the input extension request number for
2990f8248bfSmrg<symbol>X_ChangeKeyboardDevice</symbol>.
3000f8248bfSmrgIn this case,
3010f8248bfSmrg<structfield>device</structfield>
3020f8248bfSmrgand
3030f8248bfSmrg<structfield>old_device</structfield>
3040f8248bfSmrgare different, with
3050f8248bfSmrg<structfield>device</structfield>
3060f8248bfSmrgbeing the identifier for the new, current X keyboard device, and
3070f8248bfSmrg<structfield>old_device</structfield>
3080f8248bfSmrgbeing the identifier for the former device.
309e9fcaa8aSmrg</para>
310e9fcaa8aSmrg
311e9fcaa8aSmrg
312e9fcaa8aSmrg<para>
3130f8248bfSmrgIf the keyboard change is the result of an
3140f8248bfSmrg<function>XkbGetKeyboardByName</function>
3150f8248bfSmrgfunction call, which generates an
3160f8248bfSmrg<symbol>X_kbGetKbdByName</symbol>
3170f8248bfSmrgrequest,
3180f8248bfSmrg<structfield>req_major</structfield>
3190f8248bfSmrgcontains the Xkb extension base event code (see <link linkend="Initializing_the_Keyboard_Extension">section 2.4</link>), and
3200f8248bfSmrg<structfield>req_minor</structfield>
3210f8248bfSmrgcontains the event code for the Xkb extension request
3220f8248bfSmrg<symbol>X_kbGetKbdByName</symbol>.
3230f8248bfSmrg<structfield>device</structfield>
3240f8248bfSmrgcontains the device identifier for the new device, but nothing definitive can
3250f8248bfSmrgbe said for
3260f8248bfSmrg<structfield>old_device</structfield>;
3270f8248bfSmrgit may be identical to
3280f8248bfSmrg<structfield>device</structfield>,
3290f8248bfSmrgor it may be different, depending on the implementation.
330e9fcaa8aSmrg</para>
331e9fcaa8aSmrg
332e9fcaa8aSmrg</chapter>
333