From 447783f8d0108feedf3a5801c1f61853d046a733 Mon Sep 17 00:00:00 2001 From: notaz Date: Thu, 13 Jan 2011 22:41:25 +0200 Subject: [PATCH] some quick input code for PC build --- Makefile | 4 ++ frontend/plugin_lib.c | 5 ++ frontend/xkb.c | 124 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 frontend/xkb.c diff --git a/Makefile b/Makefile index 644639b7..58961582 100644 --- a/Makefile +++ b/Makefile @@ -81,6 +81,10 @@ OBJS += frontend/plat_omap.o else OBJS += frontend/plat_dummy.o endif +ifdef X11 +frontend/%.o: CFLAGS += -DX11 +OBJS += frontend/xkb.o +endif ifdef PCNT CFLAGS += -DPCNT endif diff --git a/frontend/plugin_lib.c b/frontend/plugin_lib.c index a458f0e7..93640c35 100644 --- a/frontend/plugin_lib.c +++ b/frontend/plugin_lib.c @@ -121,6 +121,11 @@ static void update_input(void) if (actions[IN_BINDTYPE_EMU] & PEV_MENU) stop = 1; keystate = actions[IN_BINDTYPE_PLAYER12]; + +#ifdef X11 + extern void x11_update_keys(void); + x11_update_keys(); +#endif } /* called on every vsync */ diff --git a/frontend/xkb.c b/frontend/xkb.c new file mode 100644 index 00000000..874531c3 --- /dev/null +++ b/frontend/xkb.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2009, Wei Mingzhi . + * All Rights Reserved. + * + * This program 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 Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "main.h" +#include "plugin_lib.h" + +static const struct { + uint16_t xkey, psxkey; +} keymap[] = { + { XK_Left, DKEY_LEFT }, + { XK_Right, DKEY_RIGHT }, + { XK_Up, DKEY_UP }, + { XK_Down, DKEY_DOWN }, + { XK_z, DKEY_CROSS }, + { XK_s, DKEY_SQUARE }, + { XK_x, DKEY_CIRCLE }, + { XK_d, DKEY_TRIANGLE }, + { XK_w, DKEY_L1 }, + { XK_r, DKEY_R1 }, + { XK_e, DKEY_L2 }, + { XK_t, DKEY_R2 }, + { XK_c, DKEY_SELECT }, + { XK_v, DKEY_START }, +}; + +static Atom wmprotocols, wmdelwindow; +static int initialized; + +static void InitKeyboard(void) { + Display *disp = (Display *)gpuDisp; + if (disp == NULL) { + fprintf(stderr, "xkb: null display\n"); + exit(1); + } + + wmprotocols = XInternAtom(disp, "WM_PROTOCOLS", 0); + wmdelwindow = XInternAtom(disp, "WM_DELETE_WINDOW", 0); + + XkbSetDetectableAutoRepeat(disp, 1, NULL); +} + +static void DestroyKeyboard(void) { + Display *disp = (Display *)gpuDisp; + if (disp) + XkbSetDetectableAutoRepeat(disp, 0, NULL); +} + +void x11_update_keys(void) { + uint8_t i; + XEvent evt; + XClientMessageEvent *xce; + uint16_t Key; + int psxkey, leave = 0; + Display *disp = (Display *)gpuDisp; + + if (initialized < 2000) { + initialized++; + InitKeyboard(); + } + + if (!disp) + return; + + while (XPending(disp)) { + XNextEvent(disp, &evt); + switch (evt.type) { + case KeyPress: + case KeyRelease: + Key = XLookupKeysym((XKeyEvent *)&evt, 0); + //printf("%s %x\n", evt.type == KeyPress ? "press" : "rel ", Key); + psxkey = -1; + for (i = 0; i < ARRAY_SIZE(keymap); i++) { + if (keymap[i].xkey == Key) { + psxkey = keymap[i].psxkey; + break; + } + } + + if (psxkey >= 0) { + if (evt.type == KeyPress) + keystate |= 1 << psxkey; + else + keystate &= ~(1 << psxkey); + } + if (evt.type == KeyPress && Key == XK_Escape) + leave = 1; + break; + + case ClientMessage: + xce = (XClientMessageEvent *)&evt; + if (xce->message_type == wmprotocols && (Atom)xce->data.l[0] == wmdelwindow) + leave = 1; + break; + } + } + + if (leave) { + DestroyKeyboard(); + exit(1); + } +} -- 2.39.5