File: keyboard.cpp

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,424 kB
  • sloc: cpp: 299,797; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (305 lines) | stat: -rw-r--r-- 9,487 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*  OnePAD - author: arcum42(@gmail.com)
 *  Copyright (C) 2009
 *
 *  Based on ZeroPAD, author zerofrog@gmail.com
 *  Copyright (C) 2006-2007
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/*
  * Theoretically, this header is for anything to do with keyboard input.
  * Pragmatically, event handing's going in here too.
  */

#if defined(__unix__)
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#endif

#include "keyboard.h"

#ifdef _WIN32
char *KeysymToChar(int keysym)
{
    LPWORD temp;

    ToAscii((UINT)keysym, NULL, NULL, temp, NULL);
    return (char *)temp;
}
#endif

#if defined(__unix__)
static bool s_grab_input = false;
static bool s_Shift = false;
static unsigned int s_previous_mouse_x = 0;
static unsigned int s_previous_mouse_y = 0;

static void AnalyzeKeyEvent(keyEvent &evt)
{
    KeySym key = (KeySym)evt.key;
    int pad = 0;
    int index = -1;

    for (int cpad = 0; cpad < GAMEPAD_NUMBER; cpad++) {
        int tmp_index = get_keyboard_key(cpad, key);
        if (tmp_index != -1) {
            pad = cpad;
            index = tmp_index;
        }
    }

    switch (evt.evt) {
        case KeyPress:
            // Shift F12 is not yet use by pcsx2. So keep it to grab/ungrab input
            // I found it very handy vs the automatic fullscreen detection
            // 1/ Does not need to detect full-screen
            // 2/ Can use a debugger in full-screen
            // 3/ Can grab input in window without the need of a pixelated full-screen
            if (key == XK_Shift_R || key == XK_Shift_L)
                s_Shift = true;
            if (key == XK_F12 && s_Shift) {
                if (!s_grab_input) {
                    s_grab_input = true;
                    XGrabPointer(GSdsp, GSwin, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, GSwin, None, CurrentTime);
                    XGrabKeyboard(GSdsp, GSwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
                } else {
                    s_grab_input = false;
                    XUngrabPointer(GSdsp, CurrentTime);
                    XUngrabKeyboard(GSdsp, CurrentTime);
                }
            }

            // Analog controls.
            if (IsAnalogKey(index)) {
                switch (index) {
                    case PAD_R_LEFT:
                    case PAD_R_UP:
                    case PAD_L_LEFT:
                    case PAD_L_UP:
                        g_key_status.press(pad, index, -MAX_ANALOG_VALUE);
                        break;
                    case PAD_R_RIGHT:
                    case PAD_R_DOWN:
                    case PAD_L_RIGHT:
                    case PAD_L_DOWN:
                        g_key_status.press(pad, index, MAX_ANALOG_VALUE);
                        break;
                }
            } else if (index != -1)
                g_key_status.press(pad, index);

            //PAD_LOG("Key pressed:%d\n", index);

            event.evt = KEYPRESS;
            event.key = key;
            break;

        case KeyRelease:
            if (key == XK_Shift_R || key == XK_Shift_L)
                s_Shift = false;

            if (index != -1)
                g_key_status.release(pad, index);

            event.evt = KEYRELEASE;
            event.key = key;
            break;

        case FocusIn:
            break;

        case FocusOut:
            s_Shift = false;
            break;

        case ButtonPress:
            if (index != -1)
                g_key_status.press(pad, index);
            break;

        case ButtonRelease:
            if (index != -1)
                g_key_status.release(pad, index);
            break;

        case MotionNotify:
            // FIXME: How to handle when the mouse does not move, no event generated!!!
            // 1/ small move == no move. Cons : can not do small movement
            // 2/ use a watchdog timer thread
            // 3/ ??? idea welcome ;)
            if (g_conf.pad_options[pad].mouse_l | g_conf.pad_options[pad].mouse_r) {
                unsigned int pad_x;
                unsigned int pad_y;
                // Note when both PADOPTION_MOUSE_R and PADOPTION_MOUSE_L are set, take only the right one
                if (g_conf.pad_options[pad].mouse_r) {
                    pad_x = PAD_R_RIGHT;
                    pad_y = PAD_R_UP;
                } else {
                    pad_x = PAD_L_RIGHT;
                    pad_y = PAD_L_UP;
                }

                unsigned x = evt.key & 0xFFFF;
                unsigned int value = (s_previous_mouse_x > x) ? s_previous_mouse_x - x : x - s_previous_mouse_x;
                value *= g_conf.get_sensibility();

                if (x == 0)
                    g_key_status.press(pad, pad_x, -MAX_ANALOG_VALUE);
                else if (x == 0xFFFF)
                    g_key_status.press(pad, pad_x, MAX_ANALOG_VALUE);
                else if (x < (s_previous_mouse_x - 2))
                    g_key_status.press(pad, pad_x, -value);
                else if (x > (s_previous_mouse_x + 2))
                    g_key_status.press(pad, pad_x, value);
                else
                    g_key_status.release(pad, pad_x);


                unsigned y = evt.key >> 16;
                value = (s_previous_mouse_y > y) ? s_previous_mouse_y - y : y - s_previous_mouse_y;
                value *= g_conf.get_sensibility();

                if (y == 0)
                    g_key_status.press(pad, pad_y, -MAX_ANALOG_VALUE);
                else if (y == 0xFFFF)
                    g_key_status.press(pad, pad_y, MAX_ANALOG_VALUE);
                else if (y < (s_previous_mouse_y - 2))
                    g_key_status.press(pad, pad_y, -value);
                else if (y > (s_previous_mouse_y + 2))
                    g_key_status.press(pad, pad_y, value);
                else
                    g_key_status.release(pad, pad_y);

                s_previous_mouse_x = x;
                s_previous_mouse_y = y;
            }

            break;
    }
}

void PollForX11KeyboardInput()
{
    keyEvent evt = {0};
    XEvent E = {0};

    // Keyboard input send by PCSX2
    g_ev_fifo.consume_all(AnalyzeKeyEvent);

    // keyboard input
    while (XPending(GSdsp) > 0) {
        XNextEvent(GSdsp, &E);

        // Change the format of the structure to be compatible with GSOpen2
        // mode (event come from pcsx2 not X)
        evt.evt = E.type;
        switch (E.type) {
            case MotionNotify:
                evt.key = (E.xbutton.x & 0xFFFF) | (E.xbutton.y << 16);
                break;
            case ButtonRelease:
            case ButtonPress:
                evt.key = E.xbutton.button;
                break;
            default:
                evt.key = (int)XLookupKeysym(&E.xkey, 0);
        }

        AnalyzeKeyEvent(evt);
    }
}

bool PollX11KeyboardMouseEvent(u32 &pkey)
{
    GdkEvent *ev = gdk_event_get();

    if (ev != NULL) {
        if (ev->type == GDK_KEY_PRESS) {
            pkey = ev->key.keyval != GDK_KEY_Escape ? ev->key.keyval : 0;
            return true;
        } else if (ev->type == GDK_BUTTON_PRESS) {
            pkey = ev->button.button;
            return true;
        }
    }

    return false;
}

#else
LRESULT WINAPI PADwndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static bool lbutton = false, rbutton = false;
    for (int pad = 0; pad < GAMEPAD_NUMBER; ++pad) 
    {
        g_key_status.keyboard_state_acces(pad);
    }

    switch (msg) {
        case WM_KEYDOWN:
            if (lParam & 0x40000000)
                return TRUE;

            for (int pad = 0; pad < GAMEPAD_NUMBER; ++pad) {
                for (int i = 0; i < MAX_KEYS; i++) {
                    assert(0);
#if 0
                    if (wParam == get_key(pad, i)) {
                        g_key_status.press(pad, i);
                        break;
                    }
#endif
                }
            }

            event.evt = KEYPRESS;
            event.key = wParam;
            break;

        case WM_KEYUP:
            for (int pad = 0; pad < GAMEPAD_NUMBER; ++pad) {
                for (int i = 0; i < MAX_KEYS; i++) {
                    assert(0);
#if 0
                    if (wParam == get_key(pad, i)) {
                        g_key_status.release(pad, i);
                        break;
                    }
#endif
                }
            }


            event.evt = KEYRELEASE;
            event.key = wParam;
            break;

        case WM_DESTROY:
        case WM_QUIT:
            event.evt = KEYPRESS;
            event.key = VK_ESCAPE;
            return GSwndProc(hWnd, msg, wParam, lParam);

        default:
            return GSwndProc(hWnd, msg, wParam, lParam);
    }

    for (int pad = 0; pad < GAMEPAD_NUMBER; ++pad)
        g_key_status.commit_status(pad);

    return TRUE;
}
#endif