some quick input code for PC build
[pcsx_rearmed.git] / frontend / xkb.c
1 /*
2  * Copyright (c) 2009, Wei Mingzhi <whistler@openoffice.org>.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <X11/Xlib.h>
23 #include <X11/Xutil.h>
24 #include <X11/keysym.h>
25 #include <X11/XKBlib.h>
26
27 #include "main.h"
28 #include "plugin_lib.h"
29
30 static const struct {
31         uint16_t xkey, psxkey;
32 } keymap[] = {
33         { XK_Left,      DKEY_LEFT },
34         { XK_Right,     DKEY_RIGHT },
35         { XK_Up,        DKEY_UP },
36         { XK_Down,      DKEY_DOWN },
37         { XK_z,         DKEY_CROSS },
38         { XK_s,         DKEY_SQUARE },
39         { XK_x,         DKEY_CIRCLE },
40         { XK_d,         DKEY_TRIANGLE },
41         { XK_w,         DKEY_L1 },
42         { XK_r,         DKEY_R1 },
43         { XK_e,         DKEY_L2 },
44         { XK_t,         DKEY_R2 },
45         { XK_c,         DKEY_SELECT },
46         { XK_v,         DKEY_START },
47 };
48
49 static Atom wmprotocols, wmdelwindow;
50 static int initialized;
51
52 static void InitKeyboard(void) {
53         Display *disp = (Display *)gpuDisp;
54         if (disp == NULL) {
55                 fprintf(stderr, "xkb: null display\n");
56                 exit(1);
57         }
58
59         wmprotocols = XInternAtom(disp, "WM_PROTOCOLS", 0);
60         wmdelwindow = XInternAtom(disp, "WM_DELETE_WINDOW", 0);
61
62         XkbSetDetectableAutoRepeat(disp, 1, NULL);
63 }
64
65 static void DestroyKeyboard(void) {
66         Display *disp = (Display *)gpuDisp;
67         if (disp)
68                 XkbSetDetectableAutoRepeat(disp, 0, NULL);
69 }
70
71 void x11_update_keys(void) {
72         uint8_t                                 i;
73         XEvent                                  evt;
74         XClientMessageEvent             *xce;
75         uint16_t                                Key;
76         int psxkey, leave = 0;
77         Display *disp = (Display *)gpuDisp;
78
79         if (initialized < 2000) {
80                 initialized++;
81                 InitKeyboard();
82         }
83
84         if (!disp)
85                 return;
86
87         while (XPending(disp)) {
88                 XNextEvent(disp, &evt);
89                 switch (evt.type) {
90                         case KeyPress:
91                         case KeyRelease:
92                                 Key = XLookupKeysym((XKeyEvent *)&evt, 0);
93                                 //printf("%s %x\n", evt.type == KeyPress ? "press" : "rel  ", Key);
94                                 psxkey = -1;
95                                 for (i = 0; i < ARRAY_SIZE(keymap); i++) {
96                                         if (keymap[i].xkey == Key) {
97                                                 psxkey = keymap[i].psxkey;
98                                                 break;
99                                         }
100                                 }
101
102                                 if (psxkey >= 0) {
103                                         if (evt.type == KeyPress)
104                                                 keystate |= 1 << psxkey;
105                                         else
106                                                 keystate &= ~(1 << psxkey);
107                                 }
108                                 if (evt.type == KeyPress && Key == XK_Escape)
109                                         leave = 1;
110                                 break;
111
112                         case ClientMessage:
113                                 xce = (XClientMessageEvent *)&evt;
114                                 if (xce->message_type == wmprotocols && (Atom)xce->data.l[0] == wmdelwindow)
115                                         leave = 1;
116                                 break;
117                 }
118         }
119
120         if (leave) {
121                 DestroyKeyboard();
122                 exit(1);
123         }
124 }