Commit Graph

58 Commits (f8c430cf7157bb45aeeb3dacce69730bad3052a4)

Author SHA1 Message Date
Isaac Freund 68dddd4132 keysym: fix underflow in binary searches
This is hit when passing an empty string and XKB_KEYSYM_CASE_INSENSITIVE
to xkb_keysym_from_name currently if `(lo + hi) / 2` is 0 and `cmp < 0`,
causing mid to underflow and the the array access into name_to_keysym on
the next iteration of the loop to be out of bounds .

We *would* use ssize_t here as it is the appropriate type, but windows
unfortunately does not define it.
2021-04-25 12:25:58 +03:00
Ran Benita 8cd688c063 keysym: avoid strtoul in xkb_keysym_from_name
Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-04-01 22:24:05 +03:00
Ran Benita 68e69b7deb keysym: use a perfect hash function for case sensitive xkb_keysym_from_name
In 7d84809fdc I added a fast path for the
case-sensitive case, but it is still slowing down Compose parsing.

Instead of the binary search, use a perfect hash function, computed with
a simple python module I found (vendored).

It is faster -- perf diff is:

   Baseline  Delta Abs  Shared Object      Symbol
   ........  .........  .................  ...................................

     22.35%    -14.04%  libc-2.33.so       [.] __strcmp_avx2
     16.75%    +10.28%  bench-compose      [.] xkb_keysym_from_name
     20.72%     +2.40%  bench-compose      [.] parse.constprop.0
      2.29%     -1.97%  bench-compose      [.] strcmp@plt
      2.56%     +1.81%  bench-compose      [.] resolve_name
      2.37%     +0.92%  libc-2.33.so       [.] __GI_____strtoull_l_internal
     26.19%     -0.63%  bench-compose      [.] lex
      1.45%     +0.56%  libc-2.33.so       [.] __memchr_avx2
      1.13%     -0.31%  libc-2.33.so       [.] __strcpy_avx2

Also reduces the binary size:

Before:

      text    data     bss     dec     hex filename
    341111    5064       8  346183   54847 build/libxkbcommon.so.0.0.0

After:

      text    data     bss     dec     hex filename
    330215    5064       8  335287   51db7 build/libxkbcommon.so.0.0.0

Note however that it's still larger than before 7d84809fdccbb5898d08388:

      text    data     bss     dec     hex filename
    320617    5168       8  325793   4f8a1 build/libxkbcommon.so.0.0.0

Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-04-01 20:06:59 +03:00
Ran Benita 1c0e28ad26 keysym: properly handle overflow in 0x keysym names
Relatedly, strtoul allows a lot of unwanted stuff (spaces, +/- sign,
thousand seperators), we really ought not use it. But that's for another
time.

Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-03-30 19:17:29 +03:00
Ran Benita 7d84809fdc keysym: fast path for case sensitive xkb_keysym_from_name
xkb_keysym_from_name() is called a lot in Compose file parsing. The
lower case handling slows things down a lot (particularly given we can't
use the optimized strcasecmp() due to locale issues). So add separate
handling for the non-case-sensitive case which is used by Compose.

To do this we need to add another version of the ks_tables table. This
adds ~20kb to the shared library binary. We can probably do something
better here but I think it's fine.

Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-03-28 16:11:36 +03:00
Ran Benita 3b506497bf keysym: inline find_sym function
It's easier when everything is in one place.

Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-03-28 16:11:36 +03:00
Ran Benita 2d87ab08f3 keysym: use a more descriptive argument name
Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-03-28 16:11:36 +03:00
Ran Benita a717549eb2 keysym: open-code bsearch
We want to optimize things here which requires messing with the binary
search some.

Signed-off-by: Ran Benita <ran@unusedvar.com>
2021-03-28 16:11:36 +03:00
Ran Benita 40aab05e77 build: include config.h manually
Previously we included it with an `-include` compiler directive. But
that's not portable. And it's better to be explicit anyway.

Every .c file should have `include "config.h"` first thing.

Signed-off-by: Ran Benita <ran@unusedvar.com>
2019-12-27 13:09:11 +02:00
Adrian Perez de Castro a8acc2ff5c Use built-in istr[n]cmp() instead of strcase[n]cmp()
This avoids the problem that MSVC does not provide strcasecmp() nor
strncasecmp(), and at the same time avoids potential problems due to
locale configuration by using istrcmp() and istrncmp() which are
already in the source tree and written to cover only ASCII.
2019-12-27 12:36:39 +02:00
Peter Hutterer 13b30f4f0d keysym: handle ssharp in XConvertCase()
lowercase: LATIN SMALL LETTER SHARP S (U+00DF)
uppercase: LATIN CAPITAL LETTER SHARP S (U+1E9E)

The uppercase sharp s (XK_ssharp) is a relatively recent addition to unicode
but was added to the relevant keyboard layouts in xkeyboard-config-2.25
(d1411e5e95c)
https://gitlab.freedesktop.org/xkeyboard-config/xkeyboard-config/issues/144

Alas, the CapsLock behavior was broken on the finnish layout (maybe others).
This was due XConvertCase() never returning the uppercase characters.

Let's make this function return the right lower/upper symbols for the sharp s
and hope that the world won't get any worse because of it.

Corresponding Xlib issue:
https://gitlab.freedesktop.org/xorg/lib/libx11/issues/110

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2019-12-22 10:12:53 +02:00
Ran Benita 18d6aebec0 keysym: add xkb_keysym_to_{lower,upper} to public API
These can be useful in some odd cases.

There is already an implementation (+ tests) for internal use, so all
that's needed is to export them.

If xkbcommon were to provide a way to convert a Unicode codepoint to a
keysym, this could have been implemented externally as follows:

    uint32_t codepoint = xkb_keysym_to_utf32(keysym);
    uint32_t upper_codepoint = my_unicode_library_to_upper(codepoint);
    xkb_keysym_t upper_keysym = theoretical_xkb_keysym_from_utf32(upper_codepoint);

However keysym -> codepoint is not injective so such a function is not
possible strictly speaking.

Signed-off-by: Ran Benita <ran234@gmail.com>
2017-12-11 23:01:18 +02:00
Ran Benita b5586a6c42 keysym: fix locale dependence in xkb_keysym_from_name()
We currently use strcasecmp, which is locale-dependent. In particular,
one well-known surprise even if restricted just ASCII input is found in
the tr_TR (Turkish) locale, see e.g.
https://msdn.microsoft.com/en-us/library/ms973919.aspx#stringsinnet20_topic5

We have known to avoid locale-dependent functions before, but in this
case, we forgot.

Fix it by implementing our own simple ASCII-only strcasecmp/strncasecmp.
Might have been possible to use strcasecmp_l() with the C locale, but
went the easy route.

Side advantage is that even this non-optimized version is faster than
the optimized libc one (__strcasecmp_l_sse42) since it doesn't need to
do the locale stuff. xkb_keysym_from_name(), which uses strcasecmp
heavily, becomes faster, and so for example Compose file parsing, which
uses xkb_keysym_from_name() heavily, becomes ~20% faster.

Resolves https://github.com/xkbcommon/libxkbcommon/issues/42
Signed-off-by: Ran Benita <ran234@gmail.com>
2016-12-02 23:46:56 +02:00
Ran Benita b3f2396588 keysym: add function to test if a keysym is for a modifier
Needed for compose.

Signed-off-by: Ran Benita <ran234@gmail.com>
2014-10-03 00:15:36 +03:00
Ran Benita 67d884ec14 Remove unnecessary !!(expressions)
_Bool already does that.

Signed-off-by: Ran Benita <ran234@gmail.com>
2014-06-01 15:24:10 +03:00
Ran Benita 3d56aa3e60 keysym: use safe keysym comparison function
Instead of thinking about signed <-> unsigned an whatnot.
bsearch() is inline in glibc, so gcc optimizes this away anyway.

Signed-off-by: Ran Benita <ran234@gmail.com>
2014-02-08 17:58:39 +02:00
Ran Benita 51c9f8e24d keysym: clarify slightly confusing comparison functions
Make it clear what the search key type and array types are.

Signed-off-by: Ran Benita <ran234@gmail.com>
2014-01-02 01:19:25 +02:00
Jasper St. Pierre 68c61e7f7d ks_tables: Put all keysym names in one giant block
This makes the file take two segments instead of potentially many, causing
relocation issues.
2014-01-01 13:03:29 -05:00
Ran Benita e18e760846 keysym: fix search for lexicographically larger strings
Probably a copy/paste error from a few lines above.

Signed-off-by: Ran Benita <ran234@gmail.com>
2014-01-01 10:38:14 +02:00
Ran Benita 1499eedd82 keysym: add xkb_keysym_to_{lower,upper}
These functions are needed later; they are not API functions. The
capitalization is not locale sensitive.

Signed-off-by: Ran Benita <ran234@gmail.com>
2013-08-15 09:58:50 +03:00
Ran Benita c7aef16649 keysym: print unicode keysyms uppercase and 0-padded
Use the same format as XKeysymToString.

Signed-off-by: Ran Benita <ran234@gmail.com>
2013-03-18 22:20:02 +00:00
Ran Benita 179855116d utils: add and use ARRAY_SIZE macro
Signed-off-by: Ran Benita <ran234@gmail.com>
2012-10-16 21:31:25 +02:00
David Herrmann 7b3bd11f92 Add xkb_keysym_from_name() flags argument for case-insensitive search
This adds a flags argument to xkb_keysym_from_name() so we can perform a
case-insensitive search. This should really be supported as many keysyms
have really weird capitalization-rules.

However, as this may produce conflicts, users must be warned to only use
this for fallback paths or error-recovery. This is also the reason why the
internal XKB parsers still use the case-sensitive search.

This also adds some test-cases so the expected results are really
produced. The binary-size does _not_ change with this patch. However,
case-sensitive search may be slightly slower with this patch. But this is
barely measurable.

[ran: use bool instead of int for icase, add a recommendation to the
doc, and test a couple "thorny" cases.]

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-10-16 21:29:09 +02:00
Ran Benita 5fff637e07 makekeys: replace helper with python script and binary search
This removes the complicated and undocumented hash-table creation-helper
and replaces it with an autogenerated sorted array. The search uses simple
bsearch() now.

We also tried using gperf but it turned out to generate way to big
hashtables and when reducing the size it isn't really faster than
bsearch() anymore.

There are no users complaining about the speed of keysym lookups and we
have no benchmarks that tell that we are horribly slow. Hence, we can
safely use the simpler approach and drop all that old code.

Signed-off-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-10-16 21:10:04 +02:00
Ran Benita 9179fed76b keysym: fix xkb_keysym_is_upper/lower() to work properly
Our current code (taken from the xserver) doesn't handle unicode keysyms
at all, and there seem to be some other changes compared to libX11,
which is what xkbcomp uses. So we just copy the code that does that from
libX11.
It would be much better to not have to hardcode unicode tables like
that, but it's probably better than dealing with glibc locale stuff for
now. It also doesn't affect our binary size much.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-10-09 21:39:16 +02:00
Ran Benita 5d31b9e3e7 Add return value the xkb_keysym_get_name
This is useful to see whether the function was successful and whether
truncation occurred.
It just changes void -> int so shouldn't break API or ABI.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-09-24 09:13:32 +10:00
Ran Benita b21107056e Organize src/ and test/ headers
- Add context.h and move context-related functions from xkb-priv.h to
  it.
- Move xkb_context definition back to context.c.
- Add keysym.h and move keysym upper/lower/keypad from xkb-priv.h to it.
- Rename xkb-priv.h to map.h since it only contains keymap-related
  definitions and declarations now.
- Remove unnecessary includes and some and some other small cleanups.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-09-16 15:20:18 +03:00
Daniel Stone b4b40d73ad Copyright updates
With Dan Nicholson's permission (via email), update his copyright and
license statements to the standard X.Org boilerplate MIT license, as
both myself and Ran have been using.

Clean up my copyright declarations (in some cases to correct ownership),
and add copyright/license statements from myself and/or Ran where
appropriate.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-09-12 16:58:54 +01:00
Ran Benita 89723b7cb7 utils: add/replace string equality macros
It's more tidy and less error prone, since we use strcasecmp == 0 a lot.
We replace strcmp == 0 by streq, strcasecmp == 0 by istreq,
uStrCasePrefix by istreq_prefix and uDupString by strdup_safe.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-07-27 00:27:24 +03:00
Ran Benita 58f8d2c151 utils: remove Xfuncproto.h and use our own macros
Add XKB_EXPORT to replace _X_EXPORT, and copy the definitions of
_X_ATTRIBUTE_FOO as ATTR_FOO.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-07-23 00:45:34 +03:00
Daniel Stone 9308a46039 Run source tree through uncrustify
.uncrustify.cfg committed for future reference also, but had to manually
fix up a few things: it really likes justifying struct initialisers.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-07-17 10:20:15 +01:00
Ran Benita 57374c3237 Rename KSIsLower/Upper and move to keysym.c
Seems like a more natural place, and allows to remove the src/misc.c
file.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-07-13 19:11:15 +03:00
Ran Benita 58b030bb90 Move XKB_KEY_NoSymbol to xkbcommon-keysyms.h
This avoids a couple of special cases in the code, and is more
consistent. Since anyone who includes xkbcommon.h also gets
xkbcommon-keysyms.h, and anyone who include xkbcommon-keysyms.h would
want NoSymbol anyway, there's no down side.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-06-09 12:34:57 +03:00
Daniel Stone 6433d72e7c Merge remote-tracking branch 'krh/keysyms'
Conflicts:
	src/keysym.c
	src/misc.c
	src/text.h
	src/xkbcomp/expr.c
	src/xkbcomp/parser.y
	src/xkbcomp/parseutils.c
	src/xkbcomp/symbols.c

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-05-09 20:15:46 +01:00
Kristian Høgsberg ace1e5df6d Use our own keysyms 2012-05-09 14:21:15 -04:00
Daniel Stone e1af48bc04 Rename keysym <-> string API
Change them to refer to the string representation of the keysym's name
as a name rather than a string, since we want to add API to get the
Unicode printable representation as well.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-05-09 13:22:34 +01:00
Ran Benita b610b2b953 Rename XKBcommonint.h to xkb-priv.h and use it
Make the files in the src/* directory use their own header or a
consilidated private header. This makes the file dependencies clearer.

Also drop the pointless "xkb" file name prefix, add split a few
declarations to their own files (atom.h and text.h).

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-05-08 17:29:43 +01:00
Ran Benita 8fbd44fde6 Implicitly include config.h in all files
The definitions in config.h should be available in all files an
implementation detail; it can be included through the build system
instead of having each file pull it every time.

This is especially helpful with AC_USE_SYSTEM_EXTENSIONS, as _GNU_SOURCE
and friends can have an effect by merely being defined, which can lead
to some confusion if its effective for only half the files.

And we don't really support a build _without_ config.h; so, one less
thing to worry about.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-04-09 14:16:17 +01:00
Ran Benita 18e6a6a43e Remove Xfuncproto.h and XKB.h from xkbcommon/xkbcommon.h
The kbproto header is already not needed here anymore.

Move the _X_EXPORT's to the corresponding function definitions, and use
straight extern "C" clauses instead of _XFUNCPROTOBEGIN/END.

It also makes more sense to have the EXPORT's in the source files, as it
provides some documentation to the reader, whereas in the header it's
obvious.

Signed-off-by: Ran Benita <ran234@gmail.com>

[daniels: Updated for xkb_keymap changes.]
2012-04-09 14:04:11 +01:00
Daniel Stone 632d9f0336 Eliminate remaining gcc warnings
Various one-liners (mostly removing unused variables) to make the code
safe for the full set of warnings used by the xorg macros.

On Debian-based systems, flex generates incorrect code resulting in two
warnings about yy_getcolumn and yy_setcolumn having no previous
declaration despite being non-static.  Fedora carries a patch to fix
this, and a bug has been filed on Debian's flex to add the patch:
http://bugs.debian.org/667027

Aside from this, it's now safe for --enable-strict-compilation.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-04-09 13:47:23 +01:00
Daniel Stone 495d87b06d Don't parse nonsense Unicode/hex keycodes
If a keysym was specified as "U1039andsomeextrastuffontheend", return
NoSymbol rather than 0x10001039; similarly, return NoSymbol for
"0xdeadbeefhitherehowsyourdaybeen" rather than 0xdeadbeef.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-04-03 15:12:04 +01:00
Ran Benita 602e87805b Define our own NoSymbol value and use it
Since we have our own xkb_keysym_t type, it makes sense to have our own
NoSymbol value instead of the one from X11/X.h.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-03-27 14:16:36 +01:00
Daniel Stone 0bb24c2d23 Introduce xkb_keysym_t type
Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-03-09 19:30:31 +00:00
Ran Benita 0d8874d01c makekeys: update to match the rest of libX11 makekeys
This integrates two commits from libX11:

ebd6ef0a4db0ddef0ae17ad14571518ccdeea5ba
   XStringToKeysym: Special case for XF86 keysyms

    Some XFree86 keysyms were in XKeysymDB as XF86_foo, despite really being
    XF86foo.  So, if we get to the bottom of XStringToKeysym and haven't
    found our XF86_foo, try it again as XF86foo.

    Signed-off-by: Daniel Stone <daniel@fooishbar.org>
    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>

00175397480b76d32bf82b0c7c94c91a2a95954e
    makekeys: Scan vendor keysyms as well as core

    Since we can't really live without vendor keysyms, scan them all in to
    generate ks_tables.h, rather than only doing the core ones, and leaving
    the vendor syms to be manually synchronised with XKeysymDB.

    Signed-off-by: Daniel Stone <daniel@fooishbar.org>
    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>

Notice that the xkey.sh test is changed to match libX11 behavior, i.e.
XKeysymToString(0x1008FE20) -> "XF86Ungrab" as opposed to "XF86_Ungrab".

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-02-26 21:09:37 +02:00
Ran Benita 04e687c9da makekeys: Fix build/target word size mismatch when cross-compiling
This matches commit 24283d40b1e4314c6647dda49d2a159833341a8b from
libX11:

    Since makekeys is built using build environment's compiler and
    runs natively, we have to make sure that the size of the
    Signature type is the same on both the native environment
    and the target, otherwise we get mismatches upon running X,
    and some LSB test failures (xts5).

    Use an unsigned 32-bit integer on all platforms.

    Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
    Reviewed-by: Daniel Stone <daniel@fooishbar.org>

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-02-26 21:08:55 +02:00
Ran Benita f3e4335fc6 Fix all constness warnings
These are all trivial/obvious fixes which clear a bunch of warnings.

Signed-off-by: Ran Benita <ran234@gmail.com>
2012-02-25 12:00:10 +02:00
Daniel Stone ead9d0cb62 Move include path from X11/extensions/ to xkbcommon/
Signed-off-by: Daniel Stone <daniel@fooishbar.org>
2012-02-15 16:24:50 +00:00
Kristian Høgsberg e879828717 Don't return a static buffer in public API 2010-10-08 15:33:18 -04:00
Kristian Høgsberg 3f0034a990 Rename public entry points to lowercase and underscore 2010-07-02 12:20:59 -04:00
Kristian Høgsberg 8e653493d0 Fix warning from CARD32 -> uint32_t conversion 2010-06-30 17:31:34 -04:00