447783f8 |
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 }, |
4218954f |
47 | |
48 | { XK_F6, 32 + SACTION_SAVE_STATE }, |
49 | { XK_F7, 32 + SACTION_PREV_SSLOT }, |
50 | { XK_F8, 32 + SACTION_NEXT_SSLOT }, |
51 | { XK_F9, 32 + SACTION_LOAD_STATE }, |
447783f8 |
52 | }; |
53 | |
54 | static Atom wmprotocols, wmdelwindow; |
55 | static int initialized; |
56 | |
57 | static void InitKeyboard(void) { |
58 | Display *disp = (Display *)gpuDisp; |
59 | if (disp == NULL) { |
60 | fprintf(stderr, "xkb: null display\n"); |
61 | exit(1); |
62 | } |
63 | |
64 | wmprotocols = XInternAtom(disp, "WM_PROTOCOLS", 0); |
65 | wmdelwindow = XInternAtom(disp, "WM_DELETE_WINDOW", 0); |
66 | |
67 | XkbSetDetectableAutoRepeat(disp, 1, NULL); |
68 | } |
69 | |
70 | static void DestroyKeyboard(void) { |
71 | Display *disp = (Display *)gpuDisp; |
72 | if (disp) |
73 | XkbSetDetectableAutoRepeat(disp, 0, NULL); |
74 | } |
75 | |
4218954f |
76 | int x11_update_keys(unsigned int *action) { |
447783f8 |
77 | uint8_t i; |
78 | XEvent evt; |
79 | XClientMessageEvent *xce; |
80 | uint16_t Key; |
1bd9ee68 |
81 | static int keystate_x11; |
447783f8 |
82 | int psxkey, leave = 0; |
83 | Display *disp = (Display *)gpuDisp; |
84 | |
1bd9ee68 |
85 | if (!disp) |
86 | return 0; |
87 | |
88 | if (!initialized) { |
447783f8 |
89 | initialized++; |
90 | InitKeyboard(); |
91 | } |
92 | |
447783f8 |
93 | while (XPending(disp)) { |
94 | XNextEvent(disp, &evt); |
95 | switch (evt.type) { |
96 | case KeyPress: |
97 | case KeyRelease: |
98 | Key = XLookupKeysym((XKeyEvent *)&evt, 0); |
99 | //printf("%s %x\n", evt.type == KeyPress ? "press" : "rel ", Key); |
100 | psxkey = -1; |
101 | for (i = 0; i < ARRAY_SIZE(keymap); i++) { |
102 | if (keymap[i].xkey == Key) { |
103 | psxkey = keymap[i].psxkey; |
104 | break; |
105 | } |
106 | } |
107 | |
4218954f |
108 | if (0 <= psxkey && psxkey < 32) { |
447783f8 |
109 | if (evt.type == KeyPress) |
1bd9ee68 |
110 | keystate_x11 |= 1 << psxkey; |
447783f8 |
111 | else |
1bd9ee68 |
112 | keystate_x11 &= ~(1 << psxkey); |
447783f8 |
113 | } |
4218954f |
114 | if (evt.type == KeyPress) { |
115 | if (psxkey > 32) |
116 | *action = psxkey - 32; |
117 | if (Key == XK_Escape) |
118 | leave = 1; |
119 | } |
447783f8 |
120 | break; |
121 | |
122 | case ClientMessage: |
123 | xce = (XClientMessageEvent *)&evt; |
124 | if (xce->message_type == wmprotocols && (Atom)xce->data.l[0] == wmdelwindow) |
125 | leave = 1; |
126 | break; |
127 | } |
128 | } |
129 | |
130 | if (leave) { |
0486fdc9 |
131 | extern void OnFile_Exit(); |
447783f8 |
132 | DestroyKeyboard(); |
0486fdc9 |
133 | OnFile_Exit(); |
447783f8 |
134 | exit(1); |
135 | } |
1bd9ee68 |
136 | |
137 | return keystate_x11; |
447783f8 |
138 | } |