2014-10-27 05:05:52 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2014-10-27 05:05:52 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
2016-12-06 05:32:10 +00:00
|
|
|
#include <X11/keysymdef.h>
|
2014-10-27 05:05:52 +00:00
|
|
|
#include <X11/extensions/xf86vmode.h>
|
|
|
|
|
2015-11-16 06:24:21 +00:00
|
|
|
#include <boolean.h>
|
|
|
|
#include <retro_inline.h>
|
|
|
|
#include <encodings/utf.h>
|
2015-12-05 17:47:05 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
2015-11-16 06:24:21 +00:00
|
|
|
|
2016-09-12 14:45:26 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../../config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XINERAMA
|
|
|
|
#include <X11/extensions/Xinerama.h>
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 15:13:16 +00:00
|
|
|
#include "../input_keyboard.h"
|
2015-04-04 18:42:16 +00:00
|
|
|
#include "../input_keymaps.h"
|
2014-10-27 05:05:52 +00:00
|
|
|
|
|
|
|
void x11_handle_key_event(XEvent *event, XIC ic, bool filter)
|
|
|
|
{
|
|
|
|
int i;
|
2016-12-06 05:32:10 +00:00
|
|
|
unsigned key;
|
|
|
|
unsigned state = event->xkey.state;
|
2015-01-12 01:52:52 +00:00
|
|
|
uint16_t mod = 0;
|
2016-10-15 23:40:29 +00:00
|
|
|
uint32_t chars[32];
|
2014-10-27 05:05:52 +00:00
|
|
|
|
2015-05-15 12:09:08 +00:00
|
|
|
bool down = event->type == KeyPress;
|
|
|
|
int num = 0;
|
|
|
|
KeySym keysym = 0;
|
2016-10-15 23:40:29 +00:00
|
|
|
|
|
|
|
chars[0] = '\0';
|
2014-10-27 05:05:52 +00:00
|
|
|
|
2016-12-06 05:32:10 +00:00
|
|
|
if (!filter)
|
2014-10-27 05:05:52 +00:00
|
|
|
{
|
2016-12-06 05:32:10 +00:00
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
char keybuf[32];
|
2016-10-15 23:40:29 +00:00
|
|
|
|
2016-12-06 05:32:10 +00:00
|
|
|
keybuf[0] = '\0';
|
2014-10-27 05:05:52 +00:00
|
|
|
#ifdef X_HAVE_UTF8_STRING
|
2016-12-06 05:32:10 +00:00
|
|
|
Status status = 0;
|
2014-10-27 05:05:52 +00:00
|
|
|
|
2016-12-06 05:32:10 +00:00
|
|
|
/* XwcLookupString doesn't seem to work. */
|
|
|
|
num = Xutf8LookupString(ic, &event->xkey, keybuf, ARRAY_SIZE(keybuf), &keysym, &status);
|
2014-10-27 05:05:52 +00:00
|
|
|
|
2016-12-06 05:32:10 +00:00
|
|
|
/* libc functions need UTF-8 locale to work properly,
|
|
|
|
* which makes mbrtowc a bit impractical.
|
|
|
|
*
|
|
|
|
* Use custom UTF8 -> UTF-32 conversion. */
|
|
|
|
num = utf8_conv_utf32(chars, ARRAY_SIZE(chars), keybuf, num);
|
2014-10-27 05:05:52 +00:00
|
|
|
#else
|
2016-12-06 05:32:10 +00:00
|
|
|
(void)ic;
|
|
|
|
num = XLookupString(&event->xkey, keybuf, sizeof(keybuf), &keysym, NULL); /* ASCII only. */
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
chars[i] = keybuf[i] & 0x7f;
|
2014-10-27 05:05:52 +00:00
|
|
|
#endif
|
2016-12-06 05:32:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
keysym = XLookupKeysym(&event->xkey, (state & ShiftMask) || (state & LockMask));
|
2014-10-27 05:05:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 05:32:10 +00:00
|
|
|
/* We can't feed uppercase letters to the keycode translator. Seems like a bad idea
|
|
|
|
* to feed it keysyms anyway, so here is a little hack... */
|
|
|
|
if (keysym >= XK_A && keysym <= XK_Z)
|
|
|
|
keysym += XK_z - XK_Z;
|
|
|
|
|
2015-05-15 13:49:44 +00:00
|
|
|
key = input_keymaps_translate_keysym_to_rk(keysym);
|
2015-01-12 01:52:52 +00:00
|
|
|
|
|
|
|
if (state & ShiftMask)
|
|
|
|
mod |= RETROKMOD_SHIFT;
|
|
|
|
if (state & LockMask)
|
|
|
|
mod |= RETROKMOD_CAPSLOCK;
|
|
|
|
if (state & ControlMask)
|
|
|
|
mod |= RETROKMOD_CTRL;
|
|
|
|
if (state & Mod1Mask)
|
|
|
|
mod |= RETROKMOD_ALT;
|
|
|
|
if (state & Mod4Mask)
|
|
|
|
mod |= RETROKMOD_META;
|
2015-05-15 12:09:08 +00:00
|
|
|
if (IsKeypadKey(keysym))
|
|
|
|
mod |= RETROKMOD_NUMLOCK;
|
2014-10-27 05:05:52 +00:00
|
|
|
|
2015-01-29 22:59:46 +00:00
|
|
|
input_keyboard_event(down, key, chars[0], mod, RETRO_DEVICE_KEYBOARD);
|
2015-01-12 01:52:52 +00:00
|
|
|
|
2014-10-27 05:05:52 +00:00
|
|
|
for (i = 1; i < num; i++)
|
2015-01-29 06:03:20 +00:00
|
|
|
input_keyboard_event(down, RETROK_UNKNOWN,
|
2015-01-29 22:59:46 +00:00
|
|
|
chars[i], mod, RETRO_DEVICE_KEYBOARD);
|
2014-10-27 05:05:52 +00:00
|
|
|
}
|