Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace of XKeycodeToKeysym

Tags:

c

linux

x11

When I try to build my code with the X11 headers in Ubuntu 12.04

    case KeyPress:
        xcommon_update_server_time( event.xkey.time );
        /* if( event.xkey.state & ShiftMask ) arg |= I_SHIFT; */
        /* this alternate approach allows handling of keys like '<' and '>' -- mrallen */
        if( event.xkey.state & ShiftMask ) {
            mykey = XKeycodeToKeysym( display, event.xkey.keycode, 1 );
        } else {
            mykey = XKeycodeToKeysym( display, event.xkey.keycode, 0 );
        }

What is the expected result? Compiles.

What happens instead?

warning: 'XKeycodeToKeysym' is deprecated (declared at /usr/include/X11/Xlib.h:1695) [-Wdeprecated-declarations]

As a result of https://bugs.freedesktop.org/show_bug.cgi?id=5349 XKeycodeToKeysym is now properly marked as being deprecated.

How to fix my code for warning free and correct build?

Thanks

like image 269
iattila Avatar asked Mar 23 '12 11:03

iattila


2 Answers

Provided XKB is available then the simplest replacement for XKeycodeToKeysym is:

#include <X11/XKBlib.h>
/* which declares:
     KeySym XkbKeycodeToKeysym(Display *dpy, KeyCode kc,
                               unsigned int group, unsigned int level); */

... and then the original question's code could become:

    mykey = XkbKeycodeToKeysym( display, event.xkey.keycode, 
                                0, event.xkey.state & ShiftMask ? 1 : 0);

notes:

  • this chromium bug report discusses both the XGetKeyboardMapping and the XKB replacement solutions.
  • XkbKeyCodeToKeysym man page on X.org
like image 139
humbletim Avatar answered Sep 29 '22 15:09

humbletim


Show you an example and you can do the same on your source.

Replace

keysym = XKeycodeToKeysym(dpy,xe->xkey.keycode,0)

with

{
    int keysyms_per_keycode_return;
    KeySym *keysym = XGetKeyboardMapping(dpy,
        xe->xkey.keycode,
        1,
        &keysyms_per_keycode_return);

    /* do something with keysym[0] */

    XFree(keysym);
}

Not forgetting to free the return value.

like image 36
user1991859 Avatar answered Sep 29 '22 13:09

user1991859