ch05.xml revision 0f8248bf
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3	  "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
4<chapter id='Keyboard_State'>
5<title>Keyboard State</title>
6
7<para>
8Keyboard state encompasses all of the transitory information necessary to map a physical key press or release to an appropriate event. The Xkb keyboard state consists of primitive components and additional derived components that are maintained for efficiency reasons. <link linkend="figure5.1">Figure 5.1</link> shows the components of Xkb keyboard state and their relationships.
9</para>
10
11<figure id='figure5.1'>
12  <title>Xkb State</title>
13  <mediaobject>
14  <!-- <title>Keyboard State Description</title> -->
15   <imageobject> <imagedata format="SVG" fileref="XKBlib-2.svg"/>
16   </imageobject>
17  </mediaobject>
18</figure>
19
20
21<sect1 id='Keyboard_State_Description'>
22<title>Keyboard State Description</title>
23<para>
24The Xkb keyboard state is comprised of the state of all keyboard modifiers, the keyboard group, and the state of the pointer buttons. These are grouped into the following components:
25</para>
26
27<itemizedlist>
28<listitem>
29  <para>
30The locked group and locked modifiers
31  </para>
32</listitem>
33<listitem>
34  <para>
35The latched group and latched modifiers
36  </para>
37</listitem>
38<listitem>
39  <para>
40The base group and base modifiers
41  </para>
42</listitem>
43<listitem>
44  <para>
45The effective group and effective modifiers
46  </para>
47</listitem>
48<listitem>
49  <para>
50The state of the core pointer buttons
51  </para>
52</listitem>
53</itemizedlist>
54
55<para id='modifiers'>
56The
57<firstterm>modifiers</firstterm>
58<indexterm significance="preferred" zone="modifiers">
59<primary>modifiers</primary></indexterm>
60are
61<symbol>Shift</symbol>,
62<symbol>Lock</symbol>,
63<symbol>Control</symbol>,
64and
65<symbol>Mod1</symbol>
66&ndash;
67<symbol>Mod5</symbol>,
68as defined by the core protocol. A modifier can be thought of as a toggle that is either set or unset. All modifiers are initially unset. When a modifier is locked, it is set and remains set for all future key events, until it is explicitly unset. A latched modifier is set, but automatically unsets after the next key event that does not change the keyboard state. Locked and latched modifier state can be changed by keyboard activity or via Xkb extension library functions.
69</para>
70
71
72<para id='keysym_groups'>
73The Xkb extension provides support for
74<firstterm>keysym groups</firstterm>,
75<indexterm significance="preferred" zone="keysym_groups">
76<primary>keysym groups</primary></indexterm>
77<indexterm significance="preferred" zone="keysym_groups">
78<primary>group</primary><secondary>keysym</secondary></indexterm>
79<indexterm significance="preferred" zone="keysym_groups">
80<primary>group</primary><secondary>ISO9995</secondary></indexterm>
81as defined by ISO9995:
82</para>
83
84
85<variablelist>
86  <varlistentry>
87    <term>Group</term>
88    <listitem>
89      <para>
90A logical state of a keyboard providing access to a collection of characters.
91A group usually contains a set of characters that logically belong together
92and that may be arranged on several shift levels within that group.
93      </para>
94    </listitem>
95  </varlistentry>
96</variablelist>
97
98
99
100<para>
101The Xkb extension supports up to four keysym groups. Groups are named beginning with one and indexed beginning with zero. All group states are indicated using the group index. At any point in time, there is zero or one locked group, zero or one latched group, and one base group. When a group is locked, it supersedes any previous locked group and remains the locked group for all future key events, until a new group is locked. A latched group applies only to the next key event that does not change the keyboard state. The locked and latched group can be changed by keyboard activity or via Xkb extension library functions.
102</para>
103
104
105<para>
106Changing to a different group changes the keyboard state to produce characters from a different group. Groups are typically used to switch between keysyms of different languages and locales.
107</para>
108
109
110<para>
111The
112<firstterm>pointer buttons</firstterm>
113are
114<symbol>Button1</symbol>
115&ndash;
116<symbol>Button5</symbol>,
117as defined by the core protocol.
118</para>
119
120
121<para id='base_group'>
122The
123<firstterm>base group</firstterm>
124<indexterm significance="preferred" zone="base_group">
125<primary>base group</primary></indexterm>
126<indexterm significance="preferred" zone="base_group">
127<primary>group</primary><secondary>base</secondary></indexterm>
128and
129<firstterm>base modifiers</firstterm>
130<indexterm significance="preferred" zone="base_group">
131<primary>base modifiers</primary></indexterm>
132<indexterm significance="preferred" zone="base_group">
133<primary>modifiers</primary><secondary>base</secondary></indexterm>
134represent keys that are physically or logically down. These
135and the pointer buttons can be changed by keyboard activity and
136not by Xkb requests. It is possible for a key to be logically
137down, but not physically down, and neither latched nor locked.
138<footnote><para>
139Keys may be logically down when they are physically up because
140of their electrical properties or because of the keyboard extension
141in the X server having filtered the key release, for esoteric reasons.
142</para></footnote>
143
144</para>
145
146
147<para id='effective_modifiers'>
148The
149<firstterm>effective modifiers</firstterm>
150<indexterm significance="preferred" zone="effective_modifiers">
151<primary>effective modifiers</primary></indexterm>
152<indexterm significance="preferred" zone="effective_modifiers">
153<primary>modifiers</primary><secondary>effective</secondary></indexterm>
154are the bitwise union of the locked, latched, and the base modifiers.
155</para>
156
157
158<para id='effective_group'>
159The
160<firstterm>effective group</firstterm>
161<indexterm significance="preferred" zone="effective_group">
162<primary>effective group</primary></indexterm>
163<indexterm significance="preferred" zone="effective_group">
164<primary>group</primary><secondary>effective</secondary></indexterm>
165is the arithmetic sum of the group indices of the latched group, locked group, and base group, which is then normalized by some function. The result is a meaningful group index.
166</para>
167
168<simplelist type='vert' columns='1'>
169<member>
170n = number of keyboard groups, 1&lt;= n &lt;= 4
171</member>
172
173<member>
1740 &lt;= any of locked, latched, or base group &lt; n
175</member>
176
177<member>
178effective group = f(locked group + latched group + base group)
179</member>
180</simplelist>
181
182<para>
183The function f ensures that the effective group is within range. The precise function is specified for the keyboard and can be retrieved through the keyboard description. It may wrap around, clamp down, or default. Few applications will actually examine the effective group, and far fewer still will examine the locked, latched, and base groups.
184</para>
185
186
187<para>
188There are two circumstances under which groups are normalized:
189</para>
190
191<orderedlist>
192  <listitem><para>
193The global locked or effective group changes. In this case, the changed group is normalized into range according to the settings of the
194<structfield>groups_wrap</structfield>
195field of the
196<structname>XkbControlsRec</structname>
197structure for the keyboard (see <link linkend="The_GroupsWrap_Control">section 10.7.1</link>).
198  </para></listitem>
199  <listitem><para>
200
201The Xkb library is interpreting an event with an effective group that is legal for the keyboard as a whole, but not for the key in question. In this case, the group to use for this event only is determined using the
202<structfield>group_info</structfield>
203field of the key symbol mapping
204(<structname>XkbSymMapRec</structname>)
205for the event key.
206  </para></listitem>
207</orderedlist>
208
209<para>
210Each nonmodifier key on a keyboard has zero or more symbols, or keysyms, associated with it. These are the logical symbols that the key can generate when it is pressed. The set of all possible keysyms for a keyboard is divided into groups. Each key is associated with zero or more groups; each group contains one or more symbols. When a key is pressed, the determination of which symbol for the key is selected is based on the effective group and the shift level, which is determined by which modifiers are set.
211</para>
212
213
214<para id='Xkb-aware'>
215A client that does not explicitly call Xkb functions, but that otherwise makes use of an X library containing the Xkb extension, will have keyboard state represented in bits 0 &ndash; 14 of the state field of events that report modifier and button state. Such a client is said to be
216<firstterm>Xkb-capable</firstterm>.
217<indexterm significance="preferred" zone="Xkb-aware">
218<primary>Xkb-capable client</primary></indexterm>
219
220A client that does explicitly call Xkb functions is an
221<firstterm>Xkb-aware</firstterm>
222<indexterm significance="preferred" zone="Xkb-aware">
223<primary>Xkb-aware client</primary></indexterm>
224client. The Xkb keyboard state includes information derived from the effective state and from two server parameters that can be set through the keyboard extension. The following components of keyboard state pertain to Xkb-capable and Xkb-aware clients:
225</para>
226
227<itemizedlist>
228<listitem>
229  <para>
230lookup state: lookup group and lookup modifiers
231  </para>
232</listitem>
233<listitem>
234  <para>
235grab state: grab group and grab modifiers
236  </para>
237</listitem>
238</itemizedlist>
239
240<para id='lookup_state'>
241The
242<firstterm>lookup modifiers</firstterm>
243<indexterm significance="preferred" zone="lookup_state">
244<primary>lookup modifiers</primary></indexterm>
245<indexterm significance="preferred" zone="lookup_state">
246<primary>modifiers</primary><secondary>lookup</secondary></indexterm>
247and
248<firstterm>lookup group</firstterm>
249<indexterm significance="preferred" zone="lookup_state">
250<primary>lookup group</primary></indexterm>
251<indexterm significance="preferred" zone="lookup_state">
252<primary>group</primary><secondary>lookup</secondary></indexterm>
253are represented in the state field of core X events. The modifier state and keycode of a key event are used to determine the symbols associated with the event. For
254<symbol>KeyPress</symbol>
255and
256<symbol>KeyRelease</symbol>
257events, the lookup modifiers are computed as:
258
259<literallayout>   ((base | latched | locked) &amp; ~<emphasis>server_internal_modifiers</emphasis>)</literallayout>
260</para>
261
262<para>
263Otherwise the lookup modifiers are computed as:
264
265<literallayout>   (((base | latched | (locked &amp; ~<emphasis>ignore_locks</emphasis>)) &amp; ~<emphasis>server_internal_modifiers</emphasis>)</literallayout>
266</para>
267
268<para>
269The lookup group is the same as the effective group.
270</para>
271
272
273<para>
274When an Xkb-capable or Xkb-aware client wishes to map a keycode to a keysym, it should use the
275<firstterm>lookup state</firstterm>
276<indexterm significance="preferred" zone="lookup_state">
277<primary>lookup state</primary></indexterm>
278<indexterm significance="preferred" zone="lookup_state">
279<primary>state</primary><secondary>lookup</secondary></indexterm>
280— the lookup group and the lookup modifiers.
281</para>
282
283
284<para id='grab_state'>
285The
286<firstterm>grab state</firstterm>
287<indexterm significance="preferred" zone="grab_state">
288<primary>grab state</primary></indexterm>
289<indexterm significance="preferred" zone="grab_state">
290<primary>state</primary><secondary>grab</secondary></indexterm>
291is the state used when matching events to passive grabs. If the event activates a grab, the
292<firstterm>grab modifiers</firstterm>
293<indexterm significance="preferred" zone="grab_state">
294<primary>grab modifiers</primary></indexterm>
295<indexterm significance="preferred" zone="grab_state">
296<primary>modifiers</primary><secondary>grab</secondary></indexterm>
297and
298<firstterm>grab group</firstterm>
299<indexterm significance="preferred" zone="grab_state">
300<primary>grab group</primary></indexterm>
301<indexterm significance="preferred" zone="grab_state">
302<primary>group</primary><secondary>grab</secondary></indexterm>
303are represented in the state field of core X events; otherwise, the lookup state is used. The grab modifiers are computed as:
304
305<literallayout>   (((base | latched | (locked &amp; ~ignore_locks)) &amp; ~server_internal_modifiers)</literallayout>
306</para>
307
308<para>
309If the server’s
310<emphasis>IgnoreGroupLock</emphasis>
311control (see <link linkend="The_IgnoreGroupLock_Control">section 10.7.3</link>) is not set, the grab group is the same as the effective group. Otherwise, the grab group is computed from the base group and latched group, ignoring the locked group.
312</para>
313
314
315<para>
316The final three components of Xkb state are applicable to clients that are not linked with an Xlib containing the X keyboard extension library and therefore are not aware of the keyboard extension
317(<emphasis>Xkb-unaware</emphasis>
318clients):
319</para>
320
321<itemizedlist>
322<listitem>
323  <para>
324The compatibility modifier state
325  </para>
326</listitem>
327<listitem>
328  <para>
329The compatibility lookup modifier state
330  </para>
331</listitem>
332<listitem>
333  <para>
334The compatibility grab modifier state
335  </para>
336</listitem>
337</itemizedlist>
338
339<para>
340The X11 protocol interpretation of modifiers does not include direct support for multiple groups. When an Xkb-extended X server connects to an Xkb-unaware client, the compatibility states remap the keyboard group into a core modifier whenever possible. The compatibility state corresponds to the effective modifier and effective group state, with the group remapped to a modifier. The compatibility lookup and grab states correspond to the lookup and grab states, respectively, with the group remapped to a modifier. The compatibility lookup state is reported in events that do not trigger passive grabs; otherwise, the compatibility grab state is reported.
341</para>
342
343
344</sect1>
345<sect1 id='Changing_the_Keyboard_State'>
346<title>Changing the Keyboard State</title>
347
348<sect2 id='Changing_Modifiers'>
349<title>Changing Modifiers</title>
350
351<indexterm zone="Changing_Modifiers">
352<primary>real modifiers</primary></indexterm>
353<indexterm zone="Changing_Modifiers">
354<primary>modifiers</primary><secondary>real</secondary></indexterm>
355<indexterm significance="preferred" zone="Changing_Modifiers">
356<primary>mask</primary><secondary>real modifiers</secondary></indexterm>
357
358<para>
359The functions in this section that change the use of modifiers use a mask in the parameter
360<structfield>affect</structfield>.
361It is a bitwise inclusive OR of the legal modifier masks:
362</para>
363
364<table id='table5.1' frame='none'>
365<title>Real Modifier Masks</title>
366<?dbfo keep-together="always" ?>
367<tgroup cols='1' align='left' colsep='0' rowsep='0'>
368<colspec colname='c1' colwidth='1.0*'/>
369<tbody>
370  <row>
371    <entry>Mask</entry>
372  </row>
373  <row>
374    <entry>ShiftMask</entry>
375  </row>
376  <row>
377    <entry>LockMask</entry>
378  </row>
379  <row>
380    <entry>ControlMask</entry>
381  </row>
382  <row>
383    <entry>Mod1Mask</entry>
384  </row>
385  <row>
386    <entry>Mod2Mask</entry>
387  </row>
388  <row>
389    <entry>Mod3Mask</entry>
390  </row>
391  <row>
392    <entry>Mod4Mask</entry>
393  </row>
394  <row>
395    <entry>Mod5Mask</entry>
396  </row>
397</tbody>
398</tgroup>
399</table>
400
401<para>
402To lock and unlock any of the eight real keyboard modifiers, use
403<function>XkbLockModifiers</function>:
404</para>
405
406<indexterm significance="preferred" zone="XkbLockModifiers"><primary><function>XkbLockModifiers</function></primary></indexterm>
407<funcsynopsis id="XkbLockModifiers">
408  <funcprototype>
409    <funcdef>Bool <function>XkbLockModifiers</function></funcdef>
410<!-- (
411<parameter>display, device_spec, affect, values</parameter>
412) -->
413
414    <paramdef>Display *<parameter>display</parameter></paramdef>
415    <paramdef>unsigned int <parameter>device_spec</parameter></paramdef>
416    <paramdef>unsigned int <parameter>affect</parameter></paramdef>
417    <paramdef>unsigned int <parameter>values</parameter></paramdef>
418  </funcprototype>
419</funcsynopsis>
420<variablelist>
421  <varlistentry>
422    <term>
423      <parameter>display</parameter>
424    </term>
425    <listitem>
426      <para>
427        connection to the X server
428      </para>
429    </listitem>
430  </varlistentry>
431  <varlistentry>
432    <term>
433      <parameter>device_spec</parameter>
434    </term>
435    <listitem>
436      <para>
437        device ID, or <symbol>XkbUseCoreKbd</symbol>
438      </para>
439    </listitem>
440  </varlistentry>
441  <varlistentry>
442    <term>
443      <parameter>affect</parameter>
444    </term>
445    <listitem>
446      <para>
447        mask of real modifiers whose lock state is to change
448      </para>
449    </listitem>
450  </varlistentry>
451  <varlistentry>
452    <term>
453      <parameter>values</parameter>
454    </term>
455    <listitem>
456      <para>
457        1 &rArr; lock, 0 &rArr; unlock; only for modifiers selected by <parameter>affect</parameter>
458      </para>
459    </listitem>
460  </varlistentry>
461</variablelist>
462
463<para>
464<function>XkbLockModifiers</function>
465sends a request to the server to lock the real modifiers selected by both
466<parameter>affect</parameter>
467and
468<parameter>values</parameter>
469and to unlock the real modifiers selected by
470<parameter>affect</parameter>
471but not selected by
472<parameter>values</parameter>.
473<function>XkbLockModifiers</function>
474does not wait for a reply from the server. It returns
475<symbol>True</symbol>
476if the request was sent, and
477<symbol>False</symbol>
478otherwise.
479</para>
480
481
482<para>
483To latch and unlatch any of the eight real keyboard modifiers, use
484<function>XkbLatchModifiers</function>:
485</para>
486
487
488<indexterm significance="preferred" zone="XkbLatchModifiers"><primary><function>XkbLatchModifiers</function></primary></indexterm>
489<funcsynopsis id="XkbLatchModifiers">
490  <funcprototype>
491    <funcdef>Bool <function>XkbLatchModifiers</function></funcdef>
492<!-- (
493<parameter>display, device_spec, affect, values</parameter>
494) -->
495
496    <paramdef>Display *<parameter>display</parameter></paramdef>
497    <paramdef>unsigned int <parameter>device_spec</parameter></paramdef>
498    <paramdef>unsigned int <parameter>affect</parameter></paramdef>
499    <paramdef>unsigned int <parameter>values</parameter></paramdef>
500  </funcprototype>
501</funcsynopsis>
502<variablelist>
503  <varlistentry>
504    <term>
505      <parameter>display</parameter>
506    </term>
507    <listitem>
508      <para>
509        connection to the X server
510      </para>
511    </listitem>
512  </varlistentry>
513  <varlistentry>
514    <term>
515      <parameter>device_spec</parameter>
516    </term>
517    <listitem>
518      <para>
519        device ID, or <symbol>XkbUseCoreKbd</symbol>
520      </para>
521    </listitem>
522  </varlistentry>
523  <varlistentry>
524    <term>
525      <parameter>affect</parameter>
526    </term>
527    <listitem>
528      <para>
529        mask of modifiers whose latch state is to change
530      </para>
531    </listitem>
532  </varlistentry>
533  <varlistentry>
534    <term>
535      <parameter>values</parameter>
536    </term>
537    <listitem>
538      <para>
539        1 &rArr; latch, 0 &rArr; unlatch; only for mods selected by <parameter>affect</parameter>
540      </para>
541    </listitem>
542  </varlistentry>
543</variablelist>
544
545<para>
546<function>XkbLatchModifiers</function>
547sends a request to the server to latch the real modifiers selected by both
548<parameter>affect</parameter>
549and
550<parameter>values</parameter>
551and to unlatch the real modifiers selected by
552<parameter>affect</parameter>
553but not selected by
554<parameter>values</parameter>.
555<function>XkbLatchModifiers</function>
556does not wait for a reply from the server. It returns
557<symbol>True</symbol>
558if the request was sent, and
559<symbol>False</symbol>
560otherwise.
561</para>
562
563
564</sect2>
565<sect2 id='Changing_Groups'>
566<title>Changing Groups</title>
567
568<indexterm zone="Changing_Groups">
569<primary>keysym groups</primary></indexterm>
570<indexterm zone="Changing_Groups">
571<primary>group</primary><secondary>keysym</secondary></indexterm>
572
573<para>
574Reference the keysym group indices with these symbolic constants:
575</para>
576
577<table id='table5.2' frame='topbot'>
578<title>Symbolic Group Names</title>
579<?dbfo keep-together="always" ?>
580<tgroup cols='2' align='left' colsep='0' rowsep='0'>
581<colspec colname='c1' colwidth='1.0*'/>
582<colspec colname='c2' colwidth='2.0*'/>
583<tbody>
584  <row rowsep='1'>
585    <entry>Symbolic Name</entry>
586    <entry>Value</entry>
587  </row>
588  <row>
589    <entry><symbol>XkbGroup1Index</symbol></entry>
590    <entry>0</entry>
591  </row>
592  <row>
593    <entry><symbol>XkbGroup2Index</symbol></entry>
594    <entry>1</entry>
595  </row>
596  <row>
597    <entry><symbol>XkbGroup3Index</symbol></entry>
598    <entry>2</entry>
599  </row>
600  <row>
601    <entry><symbol>XkbGroup4Index</symbol></entry>
602    <entry>3</entry>
603  </row>
604</tbody>
605</tgroup>
606</table>
607
608<para>
609To lock the keysym group, use
610<function>XkbLockGroup</function>.
611</para>
612
613<indexterm significance="preferred" zone="XkbLockGroup"><primary><function>XkbLockGroup</function></primary></indexterm>
614<funcsynopsis id="XkbLockGroup">
615  <funcprototype>
616    <funcdef>Bool <function>XkbLockGroup</function></funcdef>
617<!-- (
618<parameter>display, device_spec, group</parameter>
619) -->
620
621    <paramdef>Display *<parameter>display</parameter></paramdef>
622    <paramdef>unsigned int <parameter>device_spec</parameter></paramdef>
623    <paramdef>unsigned int <parameter>group</parameter></paramdef>
624  </funcprototype>
625</funcsynopsis>
626<variablelist>
627  <varlistentry>
628    <term>
629      <parameter>display</parameter>
630    </term>
631    <listitem>
632      <para>
633        connection to the X server
634      </para>
635    </listitem>
636  </varlistentry>
637  <varlistentry>
638    <term>
639      <parameter>device_spec</parameter>
640    </term>
641    <listitem>
642      <para>
643        device ID, or <symbol>XkbUseCoreKbd</symbol>
644      </para>
645    </listitem>
646  </varlistentry>
647  <varlistentry>
648    <term>
649      <parameter>group</parameter>
650    </term>
651    <listitem>
652      <para>
653        index of the keysym group to lock
654      </para>
655    </listitem>
656  </varlistentry>
657</variablelist>
658
659<para>
660<function>XkbLockGroup</function>
661sends a request to the server to lock the specified
662<parameter>group</parameter>
663and does not wait for a reply. It returns
664<symbol>True</symbol>
665if the request was sent and
666<symbol>False</symbol>
667otherwise.
668</para>
669
670
671<para>
672To latch the keysym group, use
673<function>XkbLatchGroup</function>.
674</para>
675
676
677<indexterm significance="preferred" zone="XkbLatchGroup"><primary><function>XkbLatchGroup</function></primary></indexterm>
678<funcsynopsis id="XkbLatchGroup">
679  <funcprototype>
680    <funcdef>Bool <function>XkbLatchGroup</function></funcdef>
681<!-- (
682<parameter>display, device_spec, group</parameter>
683) -->
684
685    <paramdef>Display *<parameter>display</parameter></paramdef>
686    <paramdef>unsigned int <parameter>device_spec</parameter></paramdef>
687    <paramdef>unsigned int <parameter>group</parameter></paramdef>
688  </funcprototype>
689</funcsynopsis>
690<variablelist>
691  <varlistentry>
692    <term>
693      <parameter>display</parameter>
694    </term>
695    <listitem>
696      <para>
697        connection to the X server
698      </para>
699    </listitem>
700  </varlistentry>
701  <varlistentry>
702    <term>
703      <parameter>device_spec</parameter>
704    </term>
705    <listitem>
706      <para>
707        device ID, or <symbol>XkbUseCoreKbd</symbol>
708      </para>
709    </listitem>
710  </varlistentry>
711  <varlistentry>
712    <term>
713      <parameter>group</parameter>
714    </term>
715    <listitem>
716      <para>
717        index of the keysym group to latch
718      </para>
719    </listitem>
720  </varlistentry>
721</variablelist>
722
723<para>
724<function>XkbLatchGroup</function>
725sends a request to the server to latch the specified group and does not wait for a reply. It returns
726<symbol>True</symbol>
727if the request was sent and
728<symbol>False</symbol>
729otherwise.
730</para>
731
732
733</sect2>
734</sect1>
735<sect1 id='Determining_Keyboard_State'>
736<title>Determining Keyboard State</title>
737
738<indexterm significance="preferred" zone="Determining_Keyboard_State">
739<primary><structname>XkbStateRec</structname></primary></indexterm>
740
741<para>
742Xkb keyboard state may be represented in an
743<structname>XkbStateRec</structname>
744structure:
745
746<programlisting>
747typedef struct {
748    unsigned char      group;                /* effective group index */
749    unsigned char      base_group;           /* base group index */
750    unsigned char      latched_group;        /* latched group index */
751    unsigned char      locked_group;         /* locked group index */
752    unsigned char      mods;                 /* effective modifiers */
753    unsigned char      base_mods;            /* base modifiers */
754    unsigned char      latched_mods;         /* latched modifiers */
755    unsigned char      locked_mods;          /* locked modifiers */
756    unsigned char      compat_state;         /* effective group &rArr; modifiers */
757    unsigned char      grab_mods;            /* modifiers used for grabs */
758    unsigned char      compat_grab_mods;     /* mods used for compatibility
759                                                mode grabs */
760    unsigned char      lookup_mods;          /* mods used to lookup symbols */
761    unsigned char      compat_lookup_mods;   /* mods used for compatibility
762                                                lookup */
763    unsigned short     ptr_buttons;          /* 1 bit &rArr; corresponding
764                                                pointer btn is down */
765} <structname>XkbStateRec</structname>, *XkbStatePtr;
766</programlisting></para>
767
768<para>
769To obtain the keyboard state, use
770<function>XkbGetState</function>.
771</para>
772
773<indexterm significance="preferred" zone="XkbGetState"><primary><function>XkbGetState</function></primary></indexterm>
774<funcsynopsis id="XkbGetState">
775  <funcprototype>
776    <funcdef>Status <function>XkbGetState</function></funcdef>
777<!-- (
778<parameter>display</parameter>,
779<parameter>device_spec</parameter>,
780<parameter>state_return</parameter>
781) -->
782
783    <paramdef>Display *<parameter>display</parameter></paramdef>
784    <paramdef>unsigned int <parameter>device_spec</parameter></paramdef>
785    <paramdef>XkbStatePtr <parameter>state_return</parameter></paramdef>
786  </funcprototype>
787</funcsynopsis>
788<variablelist>
789  <varlistentry>
790    <term>
791      <parameter>display</parameter>
792    </term>
793    <listitem>
794      <para>
795        connection to the X server
796      </para>
797    </listitem>
798  </varlistentry>
799  <varlistentry>
800    <term>
801      <parameter>device_spec</parameter>
802    </term>
803    <listitem>
804      <para>
805        device ID, or <symbol>XkbUseCoreKbd</symbol>
806      </para>
807    </listitem>
808  </varlistentry>
809  <varlistentry>
810    <term>
811      <parameter>state_return</parameter>
812    </term>
813    <listitem>
814      <para>
815        backfilled with Xkb state
816      </para>
817    </listitem>
818  </varlistentry>
819</variablelist>
820
821<para>
822The
823<function>XkbGetState</function>
824function queries the server for the current keyboard state, waits for a reply, and then backfills
825<parameter>state_return</parameter>
826with the results.
827</para>
828
829
830<para>
831All group values are expressed as group indices in the range [0..3]. Modifiers and the compatibility modifier state values are expressed as the bitwise union of the core X11 modifier masks. The pointer button state is reported as in the core X11 protocol.
832</para>
833
834
835</sect1>
836<sect1 id='Tracking_Keyboard_State'>
837<title>Tracking Keyboard State</title>
838
839<indexterm significance="preferred" zone="Tracking_Keyboard_State">
840<primary>events</primary><secondary><symbol>XkbStateNotify</symbol></secondary></indexterm>
841<indexterm significance="preferred" zone="Tracking_Keyboard_State">
842<primary><structname>XkbStateNotifyEvent</structname></primary></indexterm>
843
844<para>
845The Xkb extension reports
846<symbol>XkbStateNotify</symbol>
847events to clients wanting notification whenever the Xkb state changes. The changes reported include changes to any aspect of the keyboard state: when a modifier is set or unset, when the current group changes, or when a pointer button is pressed or released. As with all Xkb events,
848<symbol>XkbStateNotify</symbol>
849events are reported to all interested clients without regard to the current keyboard input focus or grab state.
850</para>
851
852
853<para>
854There are many different types of Xkb state changes. Xkb defines an event
855detail mask corresponding to each type of change. The event detail masks are
856listed in <link linkend="table5.3">Table 5.3</link>.
857</para>
858
859<table id='table5.3' frame='topbot'>
860<title>XkbStateNotify Event Detail Masks</title>
861<?dbfo keep-together="always" ?>
862<tgroup cols='2' align='left' colsep='0' rowsep='0'>
863<colspec colname='c1' colwidth='1.0*'/>
864<colspec colname='c2' colwidth='1.0*'/>
865<thead>
866  <row rowsep='1'>
867    <entry>Mask</entry>
868    <entry>Value</entry>
869  </row>
870</thead>
871<tbody>
872  <row>
873    <entry><symbol>XkbModifierStateMask</symbol></entry>
874    <entry>(1L &lt;&lt; 0)</entry>
875  </row>
876  <row>
877    <entry><symbol>XkbModifierBaseMask</symbol></entry>
878    <entry>(1L &lt;&lt; 1)</entry>
879  </row>
880  <row>
881    <entry><symbol>XkbModifierLatchMask</symbol></entry>
882    <entry>(1L &lt;&lt; 2)</entry>
883  </row>
884  <row>
885    <entry><symbol>XkbModifierLockMask</symbol></entry>
886    <entry>(1L &lt;&lt; 3)</entry>
887  </row>
888  <row>
889    <entry><symbol>XkbGroupStateMask</symbol></entry>
890    <entry>(1L &lt;&lt; 4)</entry>
891  </row>
892  <row>
893    <entry><symbol>XkbGroupBaseMask</symbol></entry>
894    <entry>(1L &lt;&lt; 5)</entry>
895  </row>
896  <row>
897    <entry><symbol>XkbGroupLatchMask</symbol></entry>
898    <entry>(1L &lt;&lt; 6)</entry>
899  </row>
900  <row>
901    <entry><symbol>XkbGroupLockMask</symbol></entry>
902    <entry>(1L &lt;&lt; 7)</entry>
903  </row>
904  <row>
905    <entry><symbol>XkbCompatStateMask</symbol></entry>
906    <entry>(1L &lt;&lt; 8)</entry>
907  </row>
908  <row>
909    <entry><symbol>XkbGrabModsMask</symbol></entry>
910    <entry>(1L &lt;&lt; 9)</entry>
911  </row>
912  <row>
913    <entry><symbol>XkbCompatGrabModsMask</symbol></entry>
914    <entry>(1L &lt;&lt; 10)</entry>
915  </row>
916  <row>
917    <entry><symbol>XkbLookupModsMask</symbol></entry>
918    <entry>(1L &lt;&lt; 11)</entry>
919  </row>
920  <row>
921    <entry><symbol>XkbCompatLookupModsMask</symbol></entry>
922    <entry>(1L &lt;&lt; 12)</entry>
923  </row>
924  <row>
925    <entry><symbol>XkbPointerButtonMask</symbol></entry>
926    <entry>(1L &lt;&lt; 13)</entry>
927  </row>
928  <row>
929    <entry><symbol>XkbAllStateComponentsMask</symbol></entry>
930    <entry>(0x3fff)</entry>
931  </row>
932</tbody>
933</tgroup>
934</table>
935
936<para>
937To track changes in the keyboard state for a particular device, select to receive
938<symbol>XkbStateNotify</symbol>
939events by calling either
940<function>XkbSelectEvents</function>
941or
942<function>XkbSelectEventDetails</function>
943(see <link linkend="Selecting_Xkb_Events">section 4.3</link>).
944</para>
945
946
947<para>
948To receive
949<symbol>XkbStateNotify</symbol>
950events under all possible conditions, use
951<function>XkbSelectEvents</function>
952and pass
953<symbol>XkbStateNotifyMask</symbol>
954in both
955<parameter>bits_to_change</parameter>
956and
957<parameter>values_for_bits</parameter>.
958</para>
959
960
961<para>
962To receive
963<symbol>XkbStateNotify</symbol>
964events only under certain conditions, use
965<function>XkbSelectEventDetails</function>
966using
967<symbol>XkbStateNotify</symbol>
968as the
969<structfield>event_type</structfield>
970and specifying the desired state changes in
971<parameter>bits_to_change</parameter>
972and
973<parameter>values_for_bits</parameter>
974using mask bits from <link linkend="table5.3">Table 5.3</link>.
975</para>
976
977
978<para>
979The structure for
980<symbol>XkbStateNotify</symbol>
981events is:
982
983<programlisting>
984typedef struct {
985    int            type;            /* Xkb extension base event code */
986    unsigned long  serial;          /* X server serial number for event */
987    Bool           send_event;      /* <symbol>True</symbol> &rArr; synthetically generated */
988    Display *      display;         /* server connection where event generated */
989    Time           time;            /* server time when event generated */
990    int            xkb_type;        /* <symbol>XkbStateNotify</symbol> */
991    int            device;          /* Xkb device ID,
992                                       will not be <symbol>XkbUseCoreKbd</symbol> */
993    unsigned int   changed;         /* bits indicating what has changed */
994    int            group;           /* group index of effective group */
995    int            base_group;      /* group index of base group */
996    int            latched_group;   /* group index of latched group */
997    int            locked_group;    /* group index of locked group */
998    unsigned int   mods;            /* effective modifiers */
999    unsigned int   base_mods;       /* base modifiers */
1000    unsigned int   latched_mods;    /* latched modifiers */
1001    unsigned int   locked_mods;     /* locked modifiers */
1002    int            compat_state;    /* computed compatibility state */
1003    unsigned char  grab_mods;       /* modifiers used for grabs */
1004    unsigned char  compat_grab_mods; /* modifiers used for compatibility grabs */
1005    unsigned char  lookup_mods;     /* modifiers used to lookup symbols */
1006    unsigned char  compat_lookup_mods; /* mods used for compatibility look up */
1007    int            ptr_buttons;     /* core pointer buttons */
1008    KeyCode        keycode;         /* keycode causing event,
1009                                       0 if programmatic */
1010    char           event_type;      /* core event if <structfield>req_major</structfield> or <structfield>req_minor</structfield>
1011                                       non zero */
1012    char           req_major;       /* major request code if program trigger,
1013                                       else 0 */
1014    char           req_minor;       /* minor request code if program trigger,
1015                                       else 0 */
1016} <structname>XkbStateNotifyEvent</structname>;
1017</programlisting></para>
1018
1019<para>
1020When you receive an
1021<symbol>XkbStateNotify</symbol>
1022event, the
1023<structfield>changed</structfield>
1024field indicates which elements of keyboard state have changed.
1025This will be the bitwise inclusive OR of one or more of the
1026<symbol>XkbStateNotify</symbol>
1027event detail masks shown in <link linkend="table5.3">Table 5.3</link>.
1028All fields reported in the event are valid, but only those indicated in
1029<structfield>changed</structfield>
1030have changed values.
1031</para>
1032
1033
1034<para>
1035The
1036<structfield>group</structfield>
1037field is the group index of the effective keysym group. The
1038<structfield>base_group</structfield>,
1039<structfield>latched_group</structfield>,
1040and
1041<structfield>locked_group</structfield>
1042fields are set to a group index value representing the base group,
1043the latched group, and the locked group, respectively. The X
1044server can set the modifier and compatibility state fields to
1045a union of the core modifier mask bits; this union represents the
1046corresponding modifier states. The <structfield>ptr_buttons</structfield>
1047field gives the state of the core pointer buttons as a
1048mask composed of an inclusive OR of zero or more of the
1049core pointer button masks.
1050</para>
1051
1052
1053<para>
1054Xkb state changes can occur either in response to keyboard
1055activity or under application control. If a key event
1056caused the state change, the
1057<structfield>keycode</structfield>
1058field gives the keycode of the key event, and the
1059<structfield>event_type</structfield>
1060field is set to either <symbol>KeyPress</symbol>
1061or
1062<symbol>KeyRelease</symbol>.
1063If a pointer button event caused the state change, the
1064<structfield>keycode</structfield>
1065field is zero, and the <structfield>event_type</structfield>
1066field is set to either <symbol>ButtonPress</symbol>
1067or <symbol>ButtonRelease</symbol>.
1068Otherwise, the major and minor codes of the request that caused the
1069state change are given in the
1070<structfield>req_major</structfield>
1071and
1072<structfield>req_minor</structfield>
1073fields, and the
1074<structfield>keycode</structfield>
1075field is zero. The
1076<structfield>req_major</structfield>
1077value is the same as the major extension opcode.
1078</para>
1079</sect1>
1080</chapter>
1081