Add new state API Add new API to deal with xkb_state objects, including xkb_state_update_key, which runs the XKB action machinery internally to calculate what exactly happens to the state when a given key is pressed or released. The canonical way to deal with keys is now: struct xkb_state *state = xkb_state_new(xkb); xkb_keysym_t *syms; int num_syms; xkb_state_update_key(state, key, is_down); num_syms = xkb_key_get_syms(state, key, &syms); More state handling API, including a way to get at or ignore preserved modifiers, is on its way. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index 2092680..9507d15 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -490,6 +490,11 @@ struct xkb_state {
unsigned char compat_lookup_mods; /* effective mods + group */
unsigned short ptr_buttons; /* core pointer buttons */
+
+ int refcnt;
+ void *filters;
+ int num_filters;
+ struct xkb_desc *xkb;
};
#define XkbStateFieldFromRec(s) XkbBuildCoreState((s)->lookup_mods,(s)->group)
@@ -551,8 +556,42 @@ _X_EXPORT extern xkb_keysym_t
xkb_string_to_keysym(const char *s);
_X_EXPORT unsigned int
-xkb_key_get_syms(struct xkb_desc *xkb, struct xkb_state *state,
- xkb_keycode_t key, xkb_keysym_t **syms_out);
+xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
+ xkb_keysym_t **syms_out);
+
+/**
+ * @defgroup state XKB state objects
+ * Creation, destruction and manipulation of keyboard state objects, * representing modifier and group state.
+ *
+ * @{
+ */
+
+/**
+ * Allocates a new XKB state object for use with the given keymap.
+ */
+_X_EXPORT struct xkb_state *
+xkb_state_new(struct xkb_desc *xkb);
+
+/**
+ * Adds a reference to a state object, so it will not be freed until unref.
+ */
+_X_EXPORT void
+xkb_state_ref(struct xkb_state *state);
+
+/**
+ * Unrefs and potentially deallocates a state object; the caller must not
+ * use the state object after calling this.
+ */
+_X_EXPORT void
+xkb_state_unref(struct xkb_state *state);
+
+/**
+ * Updates a state object to reflect the given key being pressed or released.
+ */
+_X_EXPORT void
+xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down);
+
+/** @} */
_XFUNCPROTOEND
diff --git a/src/Makefile.am b/src/Makefile.am
index 5a4847a..ef66913 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,6 +15,7 @@ libxkbcommon_la_SOURCES = \
map.c \
maprules.c \
misc.c \
+ state.c \
text.c \
xkb.c \
xkballoc.h \
diff --git a/src/XKBcommonint.h b/src/XKBcommonint.h
index 3f83277..d09060b 100644
--- a/src/XKBcommonint.h
+++ b/src/XKBcommonint.h
@@ -86,4 +86,10 @@ authorization from the authors.
#define XkmLegalSection(m) (((m)&(~XkmKeymapLegal))==0)
#define XkmSingleSection(m) (XkmLegalSection(m)&&(((m)&(~(m)+1))==(m)))
+extern unsigned int xkb_key_get_group(struct xkb_state *state,
+ xkb_keycode_t key);
+extern unsigned int xkb_key_get_level(struct xkb_state *state,
+ xkb_keycode_t key,
+ unsigned int group);
+
#endif /* _XKBCOMMONINT_H_ */
diff --git a/src/map.c b/src/map.c
index e76d0bc..d5d20d7 100644
--- a/src/map.c
+++ b/src/map.c
@@ -61,11 +61,11 @@
/**
* Returns the level to use for the given key and state, or -1 if invalid.
*/
-static int
-xkb_key_get_level(struct xkb_desc *xkb, struct xkb_state *state,
- xkb_keycode_t key, unsigned int group)
+unsigned int
+xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
+ unsigned int group)
{
- struct xkb_key_type *type = XkbKeyType(xkb, key, group);
+ struct xkb_key_type *type = XkbKeyType(state->xkb, key, group);
unsigned int active_mods = state->mods & type->mods.mask;
int i;
@@ -80,18 +80,17 @@ xkb_key_get_level(struct xkb_desc *xkb, struct xkb_state *state,
}
/**
- * Returns the group to use for the given key and state, or -1 if invalid,
- * taking wrapping/clamping/etc into account.
+ * Returns the group to use for the given key and state, taking
+ * wrapping/clamping/etc into account.
*/
-static int
-xkb_key_get_group(struct xkb_desc *xkb, struct xkb_state *state,
- xkb_keycode_t key)
+unsigned int
+xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
{
- unsigned int info = XkbKeyGroupInfo(xkb, key);
- unsigned int num_groups = XkbKeyNumGroups(xkb, key);
+ unsigned int info = XkbKeyGroupInfo(state->xkb, key);
+ unsigned int num_groups = XkbKeyNumGroups(state->xkb, key);
int ret = state->group;
- if (ret < XkbKeyNumGroups(xkb, key))
+ if (ret < XkbKeyNumGroups(state->xkb, key))
return ret;
switch (XkbOutOfRangeGroupAction(info)) {
@@ -135,19 +134,20 @@ err:
* number of symbols pointed to in syms_out.
*/
unsigned int
-xkb_key_get_syms(struct xkb_desc *xkb, struct xkb_state *state,
- xkb_keycode_t key, xkb_keysym_t **syms_out)
+xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
+ xkb_keysym_t **syms_out)
{
+ struct xkb_desc *xkb = state->xkb;
int group;
int level;
- if (!xkb || !state || key < xkb->min_key_code || key > xkb->max_key_code)
+ if (!state || key < xkb->min_key_code || key > xkb->max_key_code)
return -1;
- group = xkb_key_get_group(xkb, state, key);
+ group = xkb_key_get_group(state, key);
if (group == -1)
goto err;
- level = xkb_key_get_level(xkb, state, key, group);
+ level = xkb_key_get_level(state, key, group);
if (level == -1)
goto err;
diff --git a/src/state.c b/src/state.c
new file mode 100644
index 0000000..f248d0f
--- /dev/null
+++ b/src/state.c
@@ -0,0 +1,463 @@
+/************************************************************
+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.
+
+********************************************************/
+
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+/*
+ * This is a bastardised version of xkbActions.c from the X server which
+ * does not support, for the moment:
+ * - AccessX sticky/debounce/etc (will come later)
+ * - pointer keys (may come later)
+ * - key redirects (unlikely)
+ * - messages (very unlikely)
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+
+#include "xkbcommon/xkbcommon.h"
+#include "XKBcommonint.h"
+
+struct xkb_filter {
+ struct xkb_state *state;
+ union xkb_action action;
+ xkb_keycode_t keycode;
+ uint32_t priv;
+ int (*func)(struct xkb_filter *filter, xkb_keycode_t key, int down);
+ int refcnt;
+ struct xkb_filter *next;
+};
+
+static union xkb_action *
+xkb_key_get_action(struct xkb_state *state, xkb_keycode_t key)
+{
+ int group, level;
+
+ if (!XkbKeyHasActions(state->xkb, key) ||
+ !XkbKeycodeInRange(state->xkb, key)) {
+ static union xkb_action fake;
+ memset(&fake, 0, sizeof(fake));
+ fake.type = XkbSA_NoAction;
+ return &fake;
+ }
+
+ group = xkb_key_get_group(state, key);
+ level = xkb_key_get_level(state, key, group);
+
+ return XkbKeyActionEntry(state->xkb, key, level, group);
+}
+
+static struct xkb_filter *
+xkb_filter_new(struct xkb_state *state)
+{
+ int i;
+ int old_size = state->num_filters;
+ struct xkb_filter *filters = state->filters;
+
+ for (i = 0; i < state->num_filters; i++) {
+ if (filters[i].func)
+ continue;
+ filters[i].state = state;
+ filters[i].refcnt = 1;
+ return &filters[i];
+ }
+
+ if (state->num_filters > 0)
+ state->num_filters *= 2;
+ else
+ state->num_filters = 4;
+ filters = realloc(filters, state->num_filters * sizeof(*filters));
+ if (!filters) { /* WSGO */
+ state->num_filters = old_size;
+ return NULL;
+ }
+ state->filters = filters;
+ memset(&filters[old_size], 0,
+ (state->num_filters - old_size) * sizeof(*filters));
+
+ filters[old_size].state = state;
+ filters[old_size].refcnt = 1;
+
+ return &filters[old_size];
+}
+
+/***====================================================================***/
+
+static int
+xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
+ int down)
+{
+ if (keycode != filter->keycode) {
+ filter->action.group.flags &= ~XkbSA_ClearLocks;
+ return 1;
+ }
+
+ if (down) {
+ filter->refcnt++;
+ return 0;
+ }
+ else if (--filter->refcnt > 0) {
+ return 0;
+ }
+
+ if (filter->action.group.flags & XkbSA_GroupAbsolute)
+ filter->state->base_group = filter->action.group.group;
+ else
+ filter->state->base_group = -filter->action.group.group;
+ if (filter->action.group.flags & XkbSA_ClearLocks)
+ filter->state->locked_group = 0;
+
+ filter->func = NULL;
+
+ return 1;
+}
+
+static int
+xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t keycode,
+ union xkb_action *action)
+{
+ struct xkb_filter *filter = xkb_filter_new(state);
+
+ if (!filter) /* WSGO */
+ return -1;
+ filter->keycode = keycode;
+ filter->func = xkb_filter_group_set_func;
+ filter->action = *action;
+
+ if (action->group.flags & XkbSA_GroupAbsolute) {
+ filter->action.group.group = filter->state->base_group;
+ filter->state->base_group = action->group.group;
+ }
+ else {
+ filter->state->base_group += action->group.group;
+ }
+
+ return 1;
+}
+
+static int
+xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
+ int down)
+{
+ if (keycode != filter->keycode) {
+ filter->action.mods.flags &= ~XkbSA_ClearLocks;
+ return 1;
+ }
+
+ if (down) {
+ filter->refcnt++;
+ return 0;
+ }
+ else if (--filter->refcnt > 0) {
+ return 0;
+ }
+
+ filter->state->base_mods &= ~(filter->action.mods.mask);
+ if (filter->action.mods.flags & XkbSA_ClearLocks)
+ filter->state->locked_mods &= ~filter->action.mods.mask;
+
+ filter->func = NULL;
+
+ return 1;
+}
+
+static int
+xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t keycode,
+ union xkb_action *action)
+{
+ struct xkb_filter *filter = xkb_filter_new(state);
+
+ if (!filter) /* WSGO */
+ return -1;
+ filter->keycode = keycode;
+ filter->func = xkb_filter_mod_set_func;
+ filter->action = *action;
+
+ filter->state->base_mods |= action->mods.mask;
+
+ return 1;
+}
+
+static int
+xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
+ int down)
+{
+ if (keycode != filter->keycode)
+ return 1;
+
+ if (down) {
+ filter->refcnt++;
+ return 0;
+ }
+ if (--filter->refcnt > 0)
+ return 0;
+
+ filter->state->locked_mods &= ~filter->priv;
+ filter->func = NULL;
+ return 1;
+}
+
+static int
+xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t keycode,
+ union xkb_action *action)
+{
+ struct xkb_filter *filter = xkb_filter_new(state);
+
+ if (!filter) /* WSGO */
+ return 0;
+
+ filter->keycode = keycode;
+ filter->func = xkb_filter_mod_lock_func;
+ filter->action = *action;
+ filter->priv = state->locked_mods & action->mods.mask;
+ state->locked_mods |= action->mods.mask;
+
+ return 1;
+}
+
+enum xkb_key_latch_state {
+ NO_LATCH,
+ LATCH_KEY_DOWN,
+ LATCH_PENDING,
+};
+
+static int
+xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
+ int down)
+{
+ enum xkb_key_latch_state latch = filter->priv;
+
+ if (down && latch == LATCH_PENDING) {
+ /* If this is a new keypress and we're awaiting our single latched
+ * keypress, then either break the latch if any random key is pressed,
+ * or promote it to a lock or plain base set if it's the same
+ * modifier. */
+ union xkb_action *action = xkb_key_get_action(filter->state, keycode);
+ if (action->type == XkbSA_LatchMods &&
+ action->mods.flags == filter->action.mods.flags &&
+ action->mods.mask == filter->action.mods.mask) {
+ filter->action = *action;
+ if (filter->action.mods.flags & XkbSA_LatchToLock) {
+ filter->action.type = XkbSA_LockMods;
+ filter->func = xkb_filter_mod_lock_func;
+ filter->state->locked_mods |= filter->action.mods.mask;
+ }
+ else {
+ filter->action.type = XkbSA_SetMods;
+ filter->func = xkb_filter_mod_set_func;
+ filter->state->base_mods |= filter->action.mods.mask;
+ }
+ filter->keycode = keycode;
+ filter->state->latched_mods &= ~filter->action.mods.mask;
+ /* XXX beep beep! */
+ return 0;
+ }
+ else if (((1 << action->type) & XkbSA_BreakLatch)) {
+ /* XXX: This may be totally broken, we might need to break the
+ * latch in the next run after this press? */
+ filter->state->latched_mods &= ~filter->action.mods.mask;
+ filter->func = NULL;
+ return 1;
+ }
+ }
+ else if (!down && keycode == filter->keycode) {
+ /* Our key got released. If we've set it to clear locks, and we
+ * currently have the same modifiers locked, then release them and
+ * don't actually latch. Else we've actually hit the latching
+ * stage, so set PENDING and move our modifier from base to
+ * latched. */
+ if (latch == NO_LATCH ||
+ ((filter->action.mods.flags & XkbSA_ClearLocks) &&
+ (filter->state->locked_mods & filter->action.mods.mask) ==
+ filter->action.mods.mask)) {
+ /* XXX: We might be a bit overenthusiastic about clearing
+ * mods other filters have set here? */
+ if (latch == LATCH_PENDING)
+ filter->state->latched_mods &= ~filter->action.mods.mask;
+ else
+ filter->state->base_mods &= ~filter->action.mods.mask;
+ filter->state->locked_mods &= ~filter->action.mods.mask;
+ filter->func = NULL;
+ }
+ else {
+ latch = LATCH_PENDING;
+ filter->state->base_mods &= ~filter->action.mods.mask;
+ filter->state->latched_mods |= filter->action.mods.mask;
+ /* XXX beep beep! */
+ }
+ }
+ else if (down && latch == LATCH_KEY_DOWN) {
+ /* Someone's pressed another key while we've still got the latching
+ * key held down, so keep the base modifier state active (from
+ * xkb_filter_mod_latch_new), but don't trip the latch, just clear
+ * it as soon as the modifier gets released. */
+ latch = NO_LATCH;
+ }
+
+ filter->priv = latch;
+
+ return 1;
+}
+
+static int
+xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t keycode,
+ union xkb_action *action)
+{
+ struct xkb_filter *filter = xkb_filter_new(state);
+ enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
+
+ if (!filter) /* WSGO */
+ return -1;
+ filter->keycode = keycode;
+ filter->priv = latch;
+ filter->func = xkb_filter_mod_latch_func;
+ filter->action = *action;
+
+ filter->state->base_mods |= action->mods.mask;
+
+ return 1;
+}
+
+/**
+ * Applies any relevant filters to the key, first from the list of filters
+ * that are currently active, then if no filter has claimed the key, possibly
+ * apply a new filter from the key action.
+ */
+static void
+xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key, int down)
+{
+ struct xkb_filter *filters = state->filters;
+ union xkb_action *act = NULL;
+ int send = 1;
+ int i;
+
+ /* First run through all the currently active filters and see if any of
+ * them have claimed this event. */
+ for (i = 0; i < state->num_filters; i++) {
+ if (!filters[i].func)
+ continue;
+ send &= (*filters[i].func)(&filters[i], key, down);
+ }
+
+ if (!send || !down)
+ return;
+
+ act = xkb_key_get_action(state, key);
+ switch (act->type) {
+ case XkbSA_SetMods:
+ send = xkb_filter_mod_set_new(state, key, act);
+ break;
+ case XkbSA_LatchMods:
+ send = xkb_filter_mod_latch_new(state, key, act);
+ break;
+ case XkbSA_LockMods:
+ send = xkb_filter_mod_lock_new(state, key, act);
+ break;
+ case XkbSA_SetGroup:
+ send = xkb_filter_group_set_new(state, key, act);
+ break;
+#if 0
+ case XkbSA_LatchGroup:
+ send = xkb_filter_mod_latch_new(state, key, act);
+ break;
+ case XkbSA_LockGroup:
+ send = xkb_filter_group_lock_new(state, key, act);
+ break;
+#endif
+ }
+
+ return;
+}
+
+struct xkb_state *
+xkb_state_new(struct xkb_desc *xkb)
+{
+ struct xkb_state *ret;
+ if (!xkb)
+ return NULL;
+
+ ret = calloc(sizeof(*ret), 1);
+ if (!ret)
+ return NULL;
+
+ ret->refcnt = 1;
+ ret->xkb = xkb;
+ return ret;
+}
+
+void
+xkb_state_unref(struct xkb_state *state)
+{
+ state->refcnt--;
+ assert(state->refcnt >= 0);
+ if (state->refcnt == 0)
+ return;
+ free(state);
+}
+
+/**
+ * Given a particular key event, updates the state structure to reflect the
+ * new modifiers.
+ */
+void
+xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down)
+{
+ xkb_filter_apply_all(state, key, down);
+
+ state->mods = (state->base_mods | state->latched_mods | state->locked_mods);
+ /* FIXME: Clamp/wrap locked_group */
+ state->group = state->locked_group + state->base_group +
+ state->latched_group;
+ /* FIXME: Clamp/wrap effective group */
+
+ /* FIXME: Update LED state. */
+}
diff --git a/test/Makefile.am b/test/Makefile.am
index 02ca6ee..44950aa 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -4,7 +4,7 @@ LDADD = $(top_builddir)/src/libxkbcommon.la
TESTS_ENVIRONMENT = $(SHELL)
-check_PROGRAMS = xkey filecomp namescomp rulescomp canonicalise
+check_PROGRAMS = xkey filecomp namescomp rulescomp canonicalise state
TESTS = $(check_PROGRAMS:=.sh)
EXTRA_DIST = \
diff --git a/test/state.c b/test/state.c
new file mode 100644
index 0000000..54fc8ba
--- /dev/null
+++ b/test/state.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/X.h>
+#include <X11/Xdefs.h>
+#include <X11/keysym.h>
+#include <linux/input.h>
+#include "xkbcommon/xkbcommon.h"
+#include "xkbcomp/utils.h"
+#include "XKBcommonint.h"
+
+/* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
+ * keycode set (where ESC is 9). */
+#define EVDEV_OFFSET 8
+
+int main(int argc, char *argv[])
+{
+ struct xkb_rule_names rmlvo;
+ struct xkb_desc *xkb;
+ struct xkb_state *state;
+ int num_syms;
+ xkb_keysym_t *syms;
+
+ rmlvo.rules = "evdev";
+ rmlvo.model = "pc104";
+ rmlvo.layout = "us";
+ rmlvo.variant = NULL;
+ rmlvo.options = NULL;
+
+ xkb = xkb_compile_keymap_from_rules(&rmlvo);
+
+ if (!xkb) {
+ fprintf(stderr, "Failed to compile keymap\n");
+ exit(1);
+ }
+
+ state = xkb_state_new(xkb);
+ assert(state->mods == 0);
+ assert(state->group == 0);
+
+ /* LCtrl down */
+ xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
+ assert(state->mods & ControlMask);
+
+ /* LCtrl + RAlt down */
+ xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 1);
+ assert(state->mods & Mod1Mask);
+ assert(state->locked_mods == 0);
+ assert(state->latched_mods == 0);
+
+ /* RAlt down */
+ xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0);
+ assert(!(state->mods & ControlMask) && (state->mods & Mod1Mask));
+
+ /* none down */
+ xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 0);
+ assert(state->mods == 0);
+ assert(state->group == 0);
+
+ /* Caps locked */
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ assert(state->mods & LockMask);
+ assert(state->mods == state->locked_mods);
+ num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
+ assert(num_syms == 1 && syms[0] == XK_Q);
+
+ /* Caps unlocked */
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ assert(state->mods == 0);
+ num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
+ assert(num_syms == 1 && syms[0] == XK_q);
+
+ xkb_state_unref(state);
+ xkb_free_keymap(xkb);
+
+ return 0;
+}