Hash :
a1f8440d
Author :
Date :
2012-06-06T10:01:43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
/*
Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of Silicon Graphics not be
used in advertising or publicity pertaining to distribution
of the software without specific prior written permission.
Silicon Graphics makes no representation about the suitability
of this software for any purpose. It is provided "as is"
without any express or implied warranty.
SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xkb-priv.h"
#include "alloc.h"
int
XkbcAllocClientMap(struct xkb_keymap *keymap, unsigned which,
size_t nTotalTypes)
{
if (!keymap || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
return BadValue;
if (!keymap->map) {
keymap->map = calloc(1, sizeof(*keymap->map));
if (!keymap->map)
return BadAlloc;
darray_init(keymap->map->types);
}
if (which & XkbKeyTypesMask)
darray_growalloc(keymap->map->types, nTotalTypes);
if (which & XkbKeySymsMask)
darray_resize0(keymap->map->key_sym_map, keymap->max_key_code + 1);
if (which & XkbModifierMapMask) {
if (!keymap->map->modmap) {
keymap->map->modmap = uTypedCalloc(keymap->max_key_code + 1,
unsigned char);
if (!keymap->map->modmap)
return BadAlloc;
}
}
return Success;
}
int
XkbcAllocServerMap(struct xkb_keymap *keymap, unsigned which,
unsigned nNewActions)
{
unsigned i;
struct xkb_server_map * map;
if (!keymap)
return BadMatch;
if (!keymap->server) {
map = uTypedCalloc(1, struct xkb_server_map);
if (!map)
return BadAlloc;
for (i = 0; i < XkbNumVirtualMods; i++)
map->vmods[i] = XkbNoModifierMask;
keymap->server = map;
}
else
map = keymap->server;
if (!which)
return Success;
if (!map->explicit) {
i = keymap->max_key_code + 1;
map->explicit = uTypedCalloc(i, unsigned char);
if (!map->explicit)
return BadAlloc;
}
if (nNewActions < 1)
nNewActions = 1;
darray_resize0(map->acts, darray_size(map->acts) + nNewActions + 1);
darray_resize0(map->key_acts, keymap->max_key_code + 1);
if (!map->behaviors) {
i = keymap->max_key_code + 1;
map->behaviors = uTypedCalloc(i, struct xkb_behavior);
if (!map->behaviors)
return BadAlloc;
}
if (!map->vmodmap) {
i = keymap->max_key_code + 1;
map->vmodmap = uTypedCalloc(i, uint32_t);
if (!map->vmodmap)
return BadAlloc;
}
return Success;
}
int
XkbcCopyKeyType(const struct xkb_key_type *from, struct xkb_key_type *into)
{
int i;
if (!from || !into)
return BadMatch;
darray_free(into->map);
free(into->preserve);
for (i = 0; i < into->num_levels; i++)
free(into->level_names[i]);
free(into->level_names);
*into = *from;
darray_copy(into->map, from->map);
if (from->preserve && !darray_empty(into->map)) {
into->preserve = calloc(darray_size(into->map),
sizeof(*into->preserve));
if (!into->preserve)
return BadAlloc;
memcpy(into->preserve, from->preserve,
darray_size(into->map) * sizeof(*into->preserve));
}
if (from->level_names && into->num_levels > 0) {
into->level_names = calloc(into->num_levels,
sizeof(*into->level_names));
if (!into->level_names)
return BadAlloc;
for (i = 0; i < into->num_levels; i++)
into->level_names[i] = strdup(from->level_names[i]);
}
return Success;
}
bool
XkbcResizeKeySyms(struct xkb_keymap *keymap, xkb_keycode_t key,
unsigned int needed)
{
darray_resize0(darray_item(keymap->map->key_sym_map, key).syms, needed);
return true;
}
union xkb_action *
XkbcResizeKeyActions(struct xkb_keymap *keymap, xkb_keycode_t key,
uint32_t needed)
{
size_t old_ndx, old_num_acts, new_ndx;
if (needed == 0) {
darray_item(keymap->server->key_acts, key) = 0;
return NULL;
}
if (XkbKeyHasActions(keymap, key) &&
XkbKeyGroupsWidth(keymap, key) >= needed)
return XkbKeyActionsPtr(keymap, key);
/*
* The key may already be in the array, but without enough space.
* This should not happen often, so in order to avoid moving and
* copying stuff from acts and key_acts, we just allocate new
* space for the key at the end, and leave the old space alone.
*/
old_ndx = darray_item(keymap->server->key_acts, key);
old_num_acts = XkbKeyNumActions(keymap, key);
new_ndx = darray_size(keymap->server->acts);
darray_resize0(keymap->server->acts, new_ndx + needed);
darray_item(keymap->server->key_acts, key) = new_ndx;
/*
* The key was already in the array, copy the old actions to the
* new space.
*/
if (old_ndx != 0)
memcpy(darray_mem(keymap->server->acts, new_ndx),
darray_mem(keymap->server->acts, old_ndx),
old_num_acts * sizeof(union xkb_action));
return XkbKeyActionsPtr(keymap, key);
}
void
XkbcFreeClientMap(struct xkb_keymap *keymap)
{
struct xkb_client_map * map;
struct xkb_key_type * type;
struct xkb_sym_map *sym_map;
if (!keymap || !keymap->map)
return;
map = keymap->map;
darray_foreach(type, map->types) {
int j;
darray_free(type->map);
free(type->preserve);
for (j = 0; j < type->num_levels; j++)
free(type->level_names[j]);
free(type->level_names);
free(type->name);
}
darray_free(map->types);
darray_foreach(sym_map, map->key_sym_map) {
free(sym_map->sym_index);
free(sym_map->num_syms);
darray_free(sym_map->syms);
}
darray_free(map->key_sym_map);
free(map->modmap);
free(keymap->map);
keymap->map = NULL;
}
void
XkbcFreeServerMap(struct xkb_keymap *keymap)
{
struct xkb_server_map * map;
if (!keymap || !keymap->server)
return;
map = keymap->server;
free(map->explicit);
darray_free(map->key_acts);
darray_free(map->acts);
free(map->behaviors);
free(map->vmodmap);
free(keymap->server);
keymap->server = NULL;
}
int
XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
{
if (!keymap)
return BadMatch;
if (!keymap->compat) {
keymap->compat = calloc(1, sizeof(*keymap->compat));
if (!keymap->compat)
return BadAlloc;
darray_init(keymap->compat->sym_interpret);
}
darray_growalloc(keymap->compat->sym_interpret, nSI);
darray_resize(keymap->compat->sym_interpret, 0);
memset(keymap->compat->groups, 0,
XkbNumKbdGroups * sizeof(*keymap->compat->groups));
return Success;
}
static void
XkbcFreeCompatMap(struct xkb_keymap *keymap)
{
if (!keymap || !keymap->compat)
return;
darray_free(keymap->compat->sym_interpret);
free(keymap->compat);
keymap->compat = NULL;
}
int
XkbcAllocNames(struct xkb_keymap *keymap, unsigned which, size_t nTotalAliases)
{
if (!keymap)
return BadMatch;
if (!keymap->names) {
keymap->names = calloc(1, sizeof(*keymap->names));
if (!keymap->names)
return BadAlloc;
darray_init(keymap->names->keys);
darray_init(keymap->names->key_aliases);
}
if ((which & XkbKTLevelNamesMask) && keymap->map) {
struct xkb_key_type * type;
darray_foreach(type, keymap->map->types) {
if (!type->level_names) {
type->level_names = calloc(type->num_levels,
sizeof(*type->level_names));
if (!type->level_names)
return BadAlloc;
}
}
}
if (which & XkbKeyNamesMask)
darray_resize0(keymap->names->keys, keymap->max_key_code + 1);
if (which & XkbKeyAliasesMask)
darray_resize0(keymap->names->key_aliases, nTotalAliases);
return Success;
}
static void
XkbcFreeNames(struct xkb_keymap *keymap)
{
struct xkb_names * names;
struct xkb_client_map * map;
struct xkb_key_type *type;
int i;
if (!keymap || !keymap->names)
return;
names = keymap->names;
map = keymap->map;
if (map) {
darray_foreach(type, map->types) {
int j;
for (j = 0; j < type->num_levels; j++)
free(type->level_names[j]);
free(type->level_names);
type->level_names = NULL;
}
}
for (i = 0; i < XkbNumVirtualMods; i++)
free(names->vmods[i]);
for (i = 0; i < XkbNumIndicators; i++)
free(names->indicators[i]);
for (i = 0; i < XkbNumKbdGroups; i++)
free(names->groups[i]);
darray_free(names->keys);
darray_free(names->key_aliases);
free(names);
keymap->names = NULL;
}
int
XkbcAllocControls(struct xkb_keymap *keymap)
{
if (!keymap)
return BadMatch;
if (!keymap->ctrls) {
keymap->ctrls = uTypedCalloc(1, struct xkb_controls);
if (!keymap->ctrls)
return BadAlloc;
}
keymap->ctrls->per_key_repeat = uTypedCalloc(keymap->max_key_code >> 3,
unsigned char);
if (!keymap->ctrls->per_key_repeat)
return BadAlloc;
return Success;
}
static void
XkbcFreeControls(struct xkb_keymap *keymap)
{
if (keymap && keymap->ctrls) {
free(keymap->ctrls->per_key_repeat);
free(keymap->ctrls);
keymap->ctrls = NULL;
}
}
int
XkbcAllocIndicatorMaps(struct xkb_keymap *keymap)
{
if (!keymap)
return BadMatch;
if (!keymap->indicators) {
keymap->indicators = uTypedCalloc(1, struct xkb_indicator);
if (!keymap->indicators)
return BadAlloc;
}
return Success;
}
static void
XkbcFreeIndicatorMaps(struct xkb_keymap *keymap)
{
if (keymap) {
free(keymap->indicators);
keymap->indicators = NULL;
}
}
struct xkb_keymap *
XkbcAllocKeyboard(struct xkb_context *ctx)
{
struct xkb_keymap *keymap;
keymap = uTypedCalloc(1, struct xkb_keymap);
if (!keymap)
return NULL;
keymap->refcnt = 1;
keymap->ctx = xkb_context_ref(ctx);
return keymap;
}
void
XkbcFreeKeyboard(struct xkb_keymap *keymap)
{
if (!keymap)
return;
XkbcFreeClientMap(keymap);
XkbcFreeServerMap(keymap);
XkbcFreeCompatMap(keymap);
XkbcFreeIndicatorMaps(keymap);
XkbcFreeNames(keymap);
XkbcFreeControls(keymap);
xkb_context_unref(keymap->ctx);
free(keymap);
}