| 1 |
/* |
| 2 |
* Copyright © 2013 Ran Benita |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "x11-priv.h" |
| 9 |
|
| 10 |
int |
| 11 |
xkb_x11_setup_xkb_extension(xcb_connection_t *conn, |
| 12 |
uint16_t major_xkb_version, |
| 13 |
uint16_t minor_xkb_version, |
| 14 |
enum xkb_x11_setup_xkb_extension_flags flags, |
| 15 |
uint16_t *major_xkb_version_out, |
| 16 |
uint16_t *minor_xkb_version_out, |
| 17 |
uint8_t *base_event_out, |
| 18 |
uint8_t *base_error_out) |
| 19 |
{ |
| 20 |
uint8_t base_event, base_error; |
| 21 |
uint16_t server_major, server_minor; |
| 22 |
|
| 23 |
if (flags & ~(XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS)) { |
| 24 |
/* log_err_func(ctx, "unrecognized flags: %#x\n", flags); */ |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
{ |
| 29 |
const xcb_query_extension_reply_t *reply = |
| 30 |
xcb_get_extension_data(conn, &xcb_xkb_id); |
| 31 |
if (!reply) { |
| 32 |
/* log_err_func(ctx, "failed to query for XKB extension\n"); */ |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
if (!reply->present) { |
| 37 |
/* log_err_func(ctx, "failed to start using XKB extension: not available in server\n"); */ |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
base_event = reply->first_event; |
| 42 |
base_error = reply->first_error; |
| 43 |
} |
| 44 |
|
| 45 |
{ |
| 46 |
xcb_generic_error_t *error = NULL; |
| 47 |
xcb_xkb_use_extension_cookie_t cookie = |
| 48 |
xcb_xkb_use_extension(conn, major_xkb_version, minor_xkb_version); |
| 49 |
xcb_xkb_use_extension_reply_t *reply = |
| 50 |
xcb_xkb_use_extension_reply(conn, cookie, &error); |
| 51 |
|
| 52 |
if (!reply) { |
| 53 |
/* log_err_func(ctx, */ |
| 54 |
/* "failed to start using XKB extension: error code %d\n", */ |
| 55 |
/* error ? error->error_code : -1); */ |
| 56 |
free(error); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
if (!reply->supported) { |
| 61 |
/* log_err_func(ctx, */ |
| 62 |
/* "failed to start using XKB extension: server doesn't support version %d.%d\n", */ |
| 63 |
/* major_xkb_version, minor_xkb_version); */ |
| 64 |
free(reply); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
server_major = reply->serverMajor; |
| 69 |
server_minor = reply->serverMinor; |
| 70 |
|
| 71 |
free(reply); |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* The XkbUseExtension() in libX11 has a *bunch* of legacy stuff, but |
| 76 |
* it doesn't seem like any of it is useful to us. |
| 77 |
*/ |
| 78 |
|
| 79 |
if (major_xkb_version_out) |
| 80 |
*major_xkb_version_out = server_major; |
| 81 |
if (minor_xkb_version_out) |
| 82 |
*minor_xkb_version_out = server_minor; |
| 83 |
if (base_event_out) |
| 84 |
*base_event_out = base_event; |
| 85 |
if (base_error_out) |
| 86 |
*base_error_out = base_error; |
| 87 |
|
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
int32_t |
| 92 |
xkb_x11_get_core_keyboard_device_id(xcb_connection_t *conn) |
| 93 |
{ |
| 94 |
int32_t device_id; |
| 95 |
xcb_xkb_get_device_info_cookie_t cookie = |
| 96 |
xcb_xkb_get_device_info(conn, XCB_XKB_ID_USE_CORE_KBD, |
| 97 |
0, 0, 0, 0, 0, 0); |
| 98 |
xcb_xkb_get_device_info_reply_t *reply = |
| 99 |
xcb_xkb_get_device_info_reply(conn, cookie, NULL); |
| 100 |
|
| 101 |
if (!reply) |
| 102 |
return -1; |
| 103 |
|
| 104 |
device_id = reply->deviceID; |
| 105 |
free(reply); |
| 106 |
return device_id; |
| 107 |
} |
| 108 |
|
| 109 |
struct x11_atom_cache { |
| 110 |
/* |
| 111 |
* Invalidate the cache based on the XCB connection. |
| 112 |
* X11 atoms are actually not per connection or client, but per X server |
| 113 |
* session. But better be safe just in case we survive an X server restart. |
| 114 |
*/ |
| 115 |
xcb_connection_t *conn; |
| 116 |
struct { |
| 117 |
xcb_atom_t from; |
| 118 |
xkb_atom_t to; |
| 119 |
} cache[256]; |
| 120 |
size_t len; |
| 121 |
}; |
| 122 |
|
| 123 |
static struct x11_atom_cache * |
| 124 |
get_cache(struct xkb_context *ctx, xcb_connection_t *conn) |
| 125 |
{ |
| 126 |
if (!ctx->x11_atom_cache) { |
| 127 |
ctx->x11_atom_cache = calloc(1, sizeof(struct x11_atom_cache)); |
| 128 |
} |
| 129 |
/* Can be NULL in case the malloc failed. */ |
| 130 |
struct x11_atom_cache *cache = ctx->x11_atom_cache; |
| 131 |
if (cache && cache->conn != conn) { |
| 132 |
cache->conn = conn; |
| 133 |
cache->len = 0; |
| 134 |
} |
| 135 |
return cache; |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
x11_atom_interner_init(struct x11_atom_interner *interner, |
| 140 |
struct xkb_context *ctx, xcb_connection_t *conn) |
| 141 |
{ |
| 142 |
interner->had_error = false; |
| 143 |
interner->ctx = ctx; |
| 144 |
interner->conn = conn; |
| 145 |
interner->num_pending = 0; |
| 146 |
interner->num_copies = 0; |
| 147 |
interner->num_escaped = 0; |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
x11_atom_interner_adopt_atom(struct x11_atom_interner *interner, |
| 152 |
const xcb_atom_t atom, xkb_atom_t *out) |
| 153 |
{ |
| 154 |
*out = XKB_ATOM_NONE; |
| 155 |
|
| 156 |
if (atom == XCB_ATOM_NONE) |
| 157 |
return; |
| 158 |
|
| 159 |
/* Can be NULL in case the malloc failed. */ |
| 160 |
struct x11_atom_cache *cache = get_cache(interner->ctx, interner->conn); |
| 161 |
|
| 162 |
retry: |
| 163 |
|
| 164 |
/* Already in the cache? */ |
| 165 |
if (cache) { |
| 166 |
for (size_t c = 0; c < cache->len; c++) { |
| 167 |
if (cache->cache[c].from == atom) { |
| 168 |
*out = cache->cache[c].to; |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* Already pending? */ |
| 175 |
for (size_t i = 0; i < interner->num_pending; i++) { |
| 176 |
if (interner->pending[i].from == atom) { |
| 177 |
if (interner->num_copies == ARRAY_SIZE(interner->copies)) { |
| 178 |
x11_atom_interner_round_trip(interner); |
| 179 |
goto retry; |
| 180 |
} |
| 181 |
|
| 182 |
size_t idx = interner->num_copies++; |
| 183 |
interner->copies[idx].from = atom; |
| 184 |
interner->copies[idx].out = out; |
| 185 |
return; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/* We have to send a GetAtomName request */ |
| 190 |
if (interner->num_pending == ARRAY_SIZE(interner->pending)) { |
| 191 |
x11_atom_interner_round_trip(interner); |
| 192 |
assert(interner->num_pending < ARRAY_SIZE(interner->pending)); |
| 193 |
} |
| 194 |
size_t idx = interner->num_pending++; |
| 195 |
interner->pending[idx].from = atom; |
| 196 |
interner->pending[idx].out = out; |
| 197 |
interner->pending[idx].cookie = xcb_get_atom_name(interner->conn, atom); |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
x11_atom_interner_round_trip(struct x11_atom_interner *interner) { |
| 202 |
struct xkb_context *ctx = interner->ctx; |
| 203 |
xcb_connection_t *conn = interner->conn; |
| 204 |
|
| 205 |
/* Can be NULL in case the malloc failed. */ |
| 206 |
struct x11_atom_cache *cache = get_cache(ctx, conn); |
| 207 |
|
| 208 |
for (size_t i = 0; i < interner->num_pending; i++) { |
| 209 |
xcb_get_atom_name_reply_t *reply; |
| 210 |
|
| 211 |
reply = xcb_get_atom_name_reply(conn, interner->pending[i].cookie, NULL); |
| 212 |
if (!reply) { |
| 213 |
interner->had_error = true; |
| 214 |
continue; |
| 215 |
} |
| 216 |
xcb_atom_t x11_atom = interner->pending[i].from; |
| 217 |
xkb_atom_t atom = xkb_atom_intern(ctx, |
| 218 |
xcb_get_atom_name_name(reply), |
| 219 |
xcb_get_atom_name_name_length(reply)); |
| 220 |
free(reply); |
| 221 |
|
| 222 |
if (cache && cache->len < ARRAY_SIZE(cache->cache)) { |
| 223 |
size_t idx = cache->len++; |
| 224 |
cache->cache[idx].from = x11_atom; |
| 225 |
cache->cache[idx].to = atom; |
| 226 |
} |
| 227 |
|
| 228 |
*interner->pending[i].out = atom; |
| 229 |
|
| 230 |
for (size_t j = 0; j < interner->num_copies; j++) { |
| 231 |
if (interner->copies[j].from == x11_atom) |
| 232 |
*interner->copies[j].out = atom; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
for (size_t i = 0; i < interner->num_escaped; i++) { |
| 237 |
xcb_get_atom_name_reply_t *reply; |
| 238 |
int length; |
| 239 |
char *name; |
| 240 |
char **out = interner->escaped[i].out; |
| 241 |
|
| 242 |
reply = xcb_get_atom_name_reply(conn, interner->escaped[i].cookie, NULL); |
| 243 |
*interner->escaped[i].out = NULL; |
| 244 |
if (!reply) { |
| 245 |
interner->had_error = true; |
| 246 |
} else { |
| 247 |
length = xcb_get_atom_name_name_length(reply); |
| 248 |
name = xcb_get_atom_name_name(reply); |
| 249 |
|
| 250 |
*out = strndup(name, length); |
| 251 |
free(reply); |
| 252 |
if (*out == NULL) { |
| 253 |
interner->had_error = true; |
| 254 |
} else { |
| 255 |
XkbEscapeMapName(*out); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
interner->num_pending = 0; |
| 261 |
interner->num_copies = 0; |
| 262 |
interner->num_escaped = 0; |
| 263 |
} |
| 264 |
|
| 265 |
void |
| 266 |
x11_atom_interner_get_escaped_atom_name(struct x11_atom_interner *interner, |
| 267 |
xcb_atom_t atom, char **out) |
| 268 |
{ |
| 269 |
if (atom == 0) { |
| 270 |
*out = NULL; |
| 271 |
return; |
| 272 |
} |
| 273 |
size_t idx = interner->num_escaped++; |
| 274 |
/* There can only be a fixed number of calls to this function "in-flight", |
| 275 |
* thus we assert this number. Increase the array size if this assert fails. |
| 276 |
*/ |
| 277 |
assert(idx < ARRAY_SIZE(interner->escaped)); |
| 278 |
interner->escaped[idx].out = out; |
| 279 |
interner->escaped[idx].cookie = xcb_get_atom_name(interner->conn, atom); |
| 280 |
} |