Debian Codegraph @

include/xkbcommon/xkbcommon-x11.h

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