xref: /aosp_15_r20/external/libxkbcommon/include/xkbcommon/xkbcommon-x11.h (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2013 Ran Benita
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #ifndef _XKBCOMMON_X11_H
25*2b949d04SAndroid Build Coastguard Worker #define _XKBCOMMON_X11_H
26*2b949d04SAndroid Build Coastguard Worker 
27*2b949d04SAndroid Build Coastguard Worker #include <xcb/xcb.h>
28*2b949d04SAndroid Build Coastguard Worker #include <xkbcommon/xkbcommon.h>
29*2b949d04SAndroid Build Coastguard Worker 
30*2b949d04SAndroid Build Coastguard Worker #ifdef __cplusplus
31*2b949d04SAndroid Build Coastguard Worker extern "C" {
32*2b949d04SAndroid Build Coastguard Worker #endif
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker /**
35*2b949d04SAndroid Build Coastguard Worker  * @file
36*2b949d04SAndroid Build Coastguard Worker  * libxkbcommon-x11 API - Additional X11 support for xkbcommon.
37*2b949d04SAndroid Build Coastguard Worker  */
38*2b949d04SAndroid Build Coastguard Worker 
39*2b949d04SAndroid Build Coastguard Worker /**
40*2b949d04SAndroid Build Coastguard Worker  * @defgroup x11 X11 support
41*2b949d04SAndroid Build Coastguard Worker  * Additional X11 support for xkbcommon.
42*2b949d04SAndroid Build Coastguard Worker  * @since 0.4.0
43*2b949d04SAndroid Build Coastguard Worker  *
44*2b949d04SAndroid Build Coastguard Worker  * @{
45*2b949d04SAndroid Build Coastguard Worker  */
46*2b949d04SAndroid Build Coastguard Worker 
47*2b949d04SAndroid Build Coastguard Worker /**
48*2b949d04SAndroid Build Coastguard Worker  * @page x11-overview Overview
49*2b949d04SAndroid Build Coastguard Worker  * @parblock
50*2b949d04SAndroid Build Coastguard Worker  *
51*2b949d04SAndroid Build Coastguard Worker  * The xkbcommon-x11 module provides a means for creating an xkb_keymap
52*2b949d04SAndroid Build Coastguard Worker  * corresponding to the currently active keymap on the X server.  To do
53*2b949d04SAndroid Build Coastguard Worker  * so, it queries the XKB X11 extension using the xcb-xkb library.  It
54*2b949d04SAndroid Build Coastguard Worker  * can be used as a replacement for Xlib's keyboard handling.
55*2b949d04SAndroid Build Coastguard Worker  *
56*2b949d04SAndroid Build Coastguard Worker  * Following is an example workflow using xkbcommon-x11.  A complete
57*2b949d04SAndroid Build Coastguard Worker  * example may be found in the tools/interactive-x11.c file in the
58*2b949d04SAndroid Build Coastguard Worker  * xkbcommon source repository.  On startup:
59*2b949d04SAndroid Build Coastguard Worker  *
60*2b949d04SAndroid Build Coastguard Worker  * 1. Connect to the X server using xcb_connect().
61*2b949d04SAndroid Build Coastguard Worker  * 2. Setup the XKB X11 extension.  You can do this either by using the
62*2b949d04SAndroid Build Coastguard Worker  *    xcb_xkb_use_extension() request directly, or by using the
63*2b949d04SAndroid Build Coastguard Worker  *    xkb_x11_setup_xkb_extension() helper function.
64*2b949d04SAndroid Build Coastguard Worker  *
65*2b949d04SAndroid Build Coastguard Worker  * The XKB extension supports using separate keymaps and states for
66*2b949d04SAndroid Build Coastguard Worker  * different keyboard devices.  The devices are identified by an integer
67*2b949d04SAndroid Build Coastguard Worker  * device ID and are managed by another X11 extension, XInput. The
68*2b949d04SAndroid Build Coastguard Worker  * original X11 protocol only had one keyboard device, called the "core
69*2b949d04SAndroid Build Coastguard Worker  * keyboard", which is still supported as a "virtual device".
70*2b949d04SAndroid Build Coastguard Worker  *
71*2b949d04SAndroid Build Coastguard Worker  * 3. We will use the core keyboard as an example.  To get its device ID,
72*2b949d04SAndroid Build Coastguard Worker  *    use either the xcb_xkb_get_device_info() request directly, or the
73*2b949d04SAndroid Build Coastguard Worker  *    xkb_x11_get_core_keyboard_device_id() helper function.
74*2b949d04SAndroid Build Coastguard Worker  * 4. Create an initial xkb_keymap for this device, using the
75*2b949d04SAndroid Build Coastguard Worker  *    xkb_x11_keymap_new_from_device() function.
76*2b949d04SAndroid Build Coastguard Worker  * 5. Create an initial xkb_state for this device, using the
77*2b949d04SAndroid Build Coastguard Worker  *    xkb_x11_state_new_from_device() function.
78*2b949d04SAndroid Build Coastguard Worker  *
79*2b949d04SAndroid Build Coastguard Worker  * @note At this point, you may consider setting various XKB controls and
80*2b949d04SAndroid Build Coastguard Worker  * XKB per-client flags.  For example, enabling detectable autorepeat: \n
81*2b949d04SAndroid Build Coastguard Worker  * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Detectable_Autorepeat
82*2b949d04SAndroid Build Coastguard Worker  *
83*2b949d04SAndroid Build Coastguard Worker  * Next, you need to react to state changes (e.g. a modifier was pressed,
84*2b949d04SAndroid Build Coastguard Worker  * the layout was changed) and to keymap changes (e.g. a tool like xkbcomp,
85*2b949d04SAndroid Build Coastguard Worker  * setxkbmap or xmodmap was used):
86*2b949d04SAndroid Build Coastguard Worker  *
87*2b949d04SAndroid Build Coastguard Worker  * 6. Select to listen to at least the following XKB events:
88*2b949d04SAndroid Build Coastguard Worker  *    NewKeyboardNotify, MapNotify, StateNotify; using the
89*2b949d04SAndroid Build Coastguard Worker  *    xcb_xkb_select_events_aux() request.
90*2b949d04SAndroid Build Coastguard Worker  * 7. When NewKeyboardNotify or MapNotify are received, recreate the
91*2b949d04SAndroid Build Coastguard Worker  *    xkb_keymap and xkb_state as described above.
92*2b949d04SAndroid Build Coastguard Worker  * 8. When StateNotify is received, update the xkb_state accordingly
93*2b949d04SAndroid Build Coastguard Worker  *    using the xkb_state_update_mask() function.
94*2b949d04SAndroid Build Coastguard Worker  *
95*2b949d04SAndroid Build Coastguard Worker  * @note It is also possible to use the KeyPress/KeyRelease @p state
96*2b949d04SAndroid Build Coastguard Worker  * field to find the effective modifier and layout state, instead of
97*2b949d04SAndroid Build Coastguard Worker  * using XkbStateNotify: \n
98*2b949d04SAndroid Build Coastguard Worker  * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Computing_A_State_Field_from_an_XKB_State
99*2b949d04SAndroid Build Coastguard Worker  * \n However, XkbStateNotify is more accurate.
100*2b949d04SAndroid Build Coastguard Worker  *
101*2b949d04SAndroid Build Coastguard Worker  * @note There is no need to call xkb_state_update_key(); the state is
102*2b949d04SAndroid Build Coastguard Worker  * already synchronized.
103*2b949d04SAndroid Build Coastguard Worker  *
104*2b949d04SAndroid Build Coastguard Worker  * Finally, when a key event is received, you can use ordinary xkbcommon
105*2b949d04SAndroid Build Coastguard Worker  * functions, like xkb_state_key_get_one_sym() and xkb_state_key_get_utf8(),
106*2b949d04SAndroid Build Coastguard Worker  * as you normally would.
107*2b949d04SAndroid Build Coastguard Worker  *
108*2b949d04SAndroid Build Coastguard Worker  * @endparblock
109*2b949d04SAndroid Build Coastguard Worker  */
110*2b949d04SAndroid Build Coastguard Worker 
111*2b949d04SAndroid Build Coastguard Worker /**
112*2b949d04SAndroid Build Coastguard Worker  * The minimal compatible major version of the XKB X11 extension which
113*2b949d04SAndroid Build Coastguard Worker  * this library can use.
114*2b949d04SAndroid Build Coastguard Worker  */
115*2b949d04SAndroid Build Coastguard Worker #define XKB_X11_MIN_MAJOR_XKB_VERSION 1
116*2b949d04SAndroid Build Coastguard Worker /**
117*2b949d04SAndroid Build Coastguard Worker  * The minimal compatible minor version of the XKB X11 extension which
118*2b949d04SAndroid Build Coastguard Worker  * this library can use (for the minimal major version).
119*2b949d04SAndroid Build Coastguard Worker  */
120*2b949d04SAndroid Build Coastguard Worker #define XKB_X11_MIN_MINOR_XKB_VERSION 0
121*2b949d04SAndroid Build Coastguard Worker 
122*2b949d04SAndroid Build Coastguard Worker /** Flags for the xkb_x11_setup_xkb_extension() function. */
123*2b949d04SAndroid Build Coastguard Worker enum xkb_x11_setup_xkb_extension_flags {
124*2b949d04SAndroid Build Coastguard Worker     /** Do not apply any flags. */
125*2b949d04SAndroid Build Coastguard Worker     XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS = 0
126*2b949d04SAndroid Build Coastguard Worker };
127*2b949d04SAndroid Build Coastguard Worker 
128*2b949d04SAndroid Build Coastguard Worker /**
129*2b949d04SAndroid Build Coastguard Worker  * Setup the XKB X11 extension for this X client.
130*2b949d04SAndroid Build Coastguard Worker  *
131*2b949d04SAndroid Build Coastguard Worker  * The xkbcommon-x11 library uses various XKB requests.  Before doing so,
132*2b949d04SAndroid Build Coastguard Worker  * an X client must notify the server that it will be using the extension.
133*2b949d04SAndroid Build Coastguard Worker  * This function (or an XCB equivalent) must be called before any other
134*2b949d04SAndroid Build Coastguard Worker  * function in this library is used.
135*2b949d04SAndroid Build Coastguard Worker  *
136*2b949d04SAndroid Build Coastguard Worker  * Some X servers may not support or disable the XKB extension.  If you
137*2b949d04SAndroid Build Coastguard Worker  * want to support such servers, you need to use a different fallback.
138*2b949d04SAndroid Build Coastguard Worker  *
139*2b949d04SAndroid Build Coastguard Worker  * You may call this function several times; it is idempotent.
140*2b949d04SAndroid Build Coastguard Worker  *
141*2b949d04SAndroid Build Coastguard Worker  * @param connection
142*2b949d04SAndroid Build Coastguard Worker  *     An XCB connection to the X server.
143*2b949d04SAndroid Build Coastguard Worker  * @param major_xkb_version
144*2b949d04SAndroid Build Coastguard Worker  *     See @p minor_xkb_version.
145*2b949d04SAndroid Build Coastguard Worker  * @param minor_xkb_version
146*2b949d04SAndroid Build Coastguard Worker  *     The XKB extension version to request.  To operate correctly, you
147*2b949d04SAndroid Build Coastguard Worker  *     must have (major_xkb_version, minor_xkb_version) >=
148*2b949d04SAndroid Build Coastguard Worker  *     (XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION),
149*2b949d04SAndroid Build Coastguard Worker  *     though this is not enforced.
150*2b949d04SAndroid Build Coastguard Worker  * @param flags
151*2b949d04SAndroid Build Coastguard Worker  *     Optional flags, or 0.
152*2b949d04SAndroid Build Coastguard Worker  * @param[out] major_xkb_version_out
153*2b949d04SAndroid Build Coastguard Worker  *     See @p minor_xkb_version_out.
154*2b949d04SAndroid Build Coastguard Worker  * @param[out] minor_xkb_version_out
155*2b949d04SAndroid Build Coastguard Worker  *     Backfilled with the compatible XKB extension version numbers picked
156*2b949d04SAndroid Build Coastguard Worker  *     by the server.  Can be NULL.
157*2b949d04SAndroid Build Coastguard Worker  * @param[out] base_event_out
158*2b949d04SAndroid Build Coastguard Worker  *     Backfilled with the XKB base (also known as first) event code, needed
159*2b949d04SAndroid Build Coastguard Worker  *     to distinguish XKB events.  Can be NULL.
160*2b949d04SAndroid Build Coastguard Worker  * @param[out] base_error_out
161*2b949d04SAndroid Build Coastguard Worker  *     Backfilled with the XKB base (also known as first) error code, needed
162*2b949d04SAndroid Build Coastguard Worker  *     to distinguish XKB errors.  Can be NULL.
163*2b949d04SAndroid Build Coastguard Worker  *
164*2b949d04SAndroid Build Coastguard Worker  * @returns 1 on success, or 0 on failure.
165*2b949d04SAndroid Build Coastguard Worker  */
166*2b949d04SAndroid Build Coastguard Worker int
167*2b949d04SAndroid Build Coastguard Worker xkb_x11_setup_xkb_extension(xcb_connection_t *connection,
168*2b949d04SAndroid Build Coastguard Worker                             uint16_t major_xkb_version,
169*2b949d04SAndroid Build Coastguard Worker                             uint16_t minor_xkb_version,
170*2b949d04SAndroid Build Coastguard Worker                             enum xkb_x11_setup_xkb_extension_flags flags,
171*2b949d04SAndroid Build Coastguard Worker                             uint16_t *major_xkb_version_out,
172*2b949d04SAndroid Build Coastguard Worker                             uint16_t *minor_xkb_version_out,
173*2b949d04SAndroid Build Coastguard Worker                             uint8_t *base_event_out,
174*2b949d04SAndroid Build Coastguard Worker                             uint8_t *base_error_out);
175*2b949d04SAndroid Build Coastguard Worker 
176*2b949d04SAndroid Build Coastguard Worker /**
177*2b949d04SAndroid Build Coastguard Worker  * Get the keyboard device ID of the core X11 keyboard.
178*2b949d04SAndroid Build Coastguard Worker  *
179*2b949d04SAndroid Build Coastguard Worker  * @param connection An XCB connection to the X server.
180*2b949d04SAndroid Build Coastguard Worker  *
181*2b949d04SAndroid Build Coastguard Worker  * @returns A device ID which may be used with other xkb_x11_* functions,
182*2b949d04SAndroid Build Coastguard Worker  *          or -1 on failure.
183*2b949d04SAndroid Build Coastguard Worker  */
184*2b949d04SAndroid Build Coastguard Worker int32_t
185*2b949d04SAndroid Build Coastguard Worker xkb_x11_get_core_keyboard_device_id(xcb_connection_t *connection);
186*2b949d04SAndroid Build Coastguard Worker 
187*2b949d04SAndroid Build Coastguard Worker /**
188*2b949d04SAndroid Build Coastguard Worker  * Create a keymap from an X11 keyboard device.
189*2b949d04SAndroid Build Coastguard Worker  *
190*2b949d04SAndroid Build Coastguard Worker  * This function queries the X server with various requests, fetches the
191*2b949d04SAndroid Build Coastguard Worker  * details of the active keymap on a keyboard device, and creates an
192*2b949d04SAndroid Build Coastguard Worker  * xkb_keymap from these details.
193*2b949d04SAndroid Build Coastguard Worker  *
194*2b949d04SAndroid Build Coastguard Worker  * @param context
195*2b949d04SAndroid Build Coastguard Worker  *     The context in which to create the keymap.
196*2b949d04SAndroid Build Coastguard Worker  * @param connection
197*2b949d04SAndroid Build Coastguard Worker  *     An XCB connection to the X server.
198*2b949d04SAndroid Build Coastguard Worker  * @param device_id
199*2b949d04SAndroid Build Coastguard Worker  *     An XInput device ID (in the range 0-127) with input class KEY.
200*2b949d04SAndroid Build Coastguard Worker  *     Passing values outside of this range is an error (the XKB protocol
201*2b949d04SAndroid Build Coastguard Worker  *     predates the XInput2 protocol, which first allowed IDs > 127).
202*2b949d04SAndroid Build Coastguard Worker  * @param flags
203*2b949d04SAndroid Build Coastguard Worker  *     Optional flags for the keymap, or 0.
204*2b949d04SAndroid Build Coastguard Worker  *
205*2b949d04SAndroid Build Coastguard Worker  * @returns A keymap retrieved from the X server, or NULL on failure.
206*2b949d04SAndroid Build Coastguard Worker  *
207*2b949d04SAndroid Build Coastguard Worker  * @memberof xkb_keymap
208*2b949d04SAndroid Build Coastguard Worker  */
209*2b949d04SAndroid Build Coastguard Worker struct xkb_keymap *
210*2b949d04SAndroid Build Coastguard Worker xkb_x11_keymap_new_from_device(struct xkb_context *context,
211*2b949d04SAndroid Build Coastguard Worker                                xcb_connection_t *connection,
212*2b949d04SAndroid Build Coastguard Worker                                int32_t device_id,
213*2b949d04SAndroid Build Coastguard Worker                                enum xkb_keymap_compile_flags flags);
214*2b949d04SAndroid Build Coastguard Worker 
215*2b949d04SAndroid Build Coastguard Worker /**
216*2b949d04SAndroid Build Coastguard Worker  * Create a new keyboard state object from an X11 keyboard device.
217*2b949d04SAndroid Build Coastguard Worker  *
218*2b949d04SAndroid Build Coastguard Worker  * This function is the same as xkb_state_new(), only pre-initialized
219*2b949d04SAndroid Build Coastguard Worker  * with the state of the device at the time this function is called.
220*2b949d04SAndroid Build Coastguard Worker  *
221*2b949d04SAndroid Build Coastguard Worker  * @param keymap
222*2b949d04SAndroid Build Coastguard Worker  *     The keymap for which to create the state.
223*2b949d04SAndroid Build Coastguard Worker  * @param connection
224*2b949d04SAndroid Build Coastguard Worker  *     An XCB connection to the X server.
225*2b949d04SAndroid Build Coastguard Worker  * @param device_id
226*2b949d04SAndroid Build Coastguard Worker  *     An XInput 1 device ID (in the range 0-255) with input class KEY.
227*2b949d04SAndroid Build Coastguard Worker  *     Passing values outside of this range is an error.
228*2b949d04SAndroid Build Coastguard Worker  *
229*2b949d04SAndroid Build Coastguard Worker  * @returns A new keyboard state object, or NULL on failure.
230*2b949d04SAndroid Build Coastguard Worker  *
231*2b949d04SAndroid Build Coastguard Worker  * @memberof xkb_state
232*2b949d04SAndroid Build Coastguard Worker  */
233*2b949d04SAndroid Build Coastguard Worker struct xkb_state *
234*2b949d04SAndroid Build Coastguard Worker xkb_x11_state_new_from_device(struct xkb_keymap *keymap,
235*2b949d04SAndroid Build Coastguard Worker                               xcb_connection_t *connection,
236*2b949d04SAndroid Build Coastguard Worker                               int32_t device_id);
237*2b949d04SAndroid Build Coastguard Worker 
238*2b949d04SAndroid Build Coastguard Worker /** @} */
239*2b949d04SAndroid Build Coastguard Worker 
240*2b949d04SAndroid Build Coastguard Worker #ifdef __cplusplus
241*2b949d04SAndroid Build Coastguard Worker } /* extern "C" */
242*2b949d04SAndroid Build Coastguard Worker #endif
243*2b949d04SAndroid Build Coastguard Worker 
244*2b949d04SAndroid Build Coastguard Worker #endif /* _XKBCOMMON_X11_H */
245