2014-04-12 10:44:38 +00:00
|
|
|
/* LilyPad - Pad plugin for PS2 Emulator
|
|
|
|
* Copyright (C) 2002-2014 PCSX2 Dev Team/ChickenLiver
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Found- ation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 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 PCSX2. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
#include "Global.h"
|
|
|
|
// This is undoubtedly completely unnecessary.
|
|
|
|
#include "KeyboardQueue.h"
|
|
|
|
|
|
|
|
// What MS calls a single process Mutex. Faster, supposedly.
|
|
|
|
// More importantly, can be abbreviated, amusingly, as cSection.
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2010-04-24 21:37:39 +00:00
|
|
|
static CRITICAL_SECTION cSection;
|
|
|
|
static u8 csInitialized = 0;
|
2015-02-04 21:27:39 +00:00
|
|
|
#else
|
|
|
|
static std::mutex cSection;
|
|
|
|
#endif
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
#define EVENT_QUEUE_LEN 16
|
|
|
|
// Actually points one beyond the last queued event.
|
|
|
|
static u8 lastQueuedEvent = 0;
|
|
|
|
static u8 nextQueuedEvent = 0;
|
|
|
|
static keyEvent queuedEvents[EVENT_QUEUE_LEN];
|
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
void QueueKeyEvent(int key, int event)
|
|
|
|
{
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-04 12:16:34 +00:00
|
|
|
if (!csInitialized) {
|
|
|
|
csInitialized = 1;
|
|
|
|
InitializeCriticalSection(&cSection);
|
|
|
|
}
|
|
|
|
EnterCriticalSection(&cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#else
|
2016-09-04 12:16:34 +00:00
|
|
|
std::lock_guard<std::mutex> lock(cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#endif
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
// Don't queue events if escape is on top of queue. This is just for safety
|
|
|
|
// purposes when a game is killing the emulator for whatever reason.
|
|
|
|
if (nextQueuedEvent == lastQueuedEvent ||
|
|
|
|
queuedEvents[nextQueuedEvent].key != VK_ESCAPE ||
|
|
|
|
queuedEvents[nextQueuedEvent].evt != KEYPRESS) {
|
|
|
|
// Clear queue on escape down, bringing escape to front. May do something
|
|
|
|
// with shift/ctrl/alt and F-keys, later.
|
|
|
|
if (event == KEYPRESS && key == VK_ESCAPE) {
|
|
|
|
nextQueuedEvent = lastQueuedEvent;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
queuedEvents[lastQueuedEvent].key = key;
|
|
|
|
queuedEvents[lastQueuedEvent].evt = event;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
lastQueuedEvent = (lastQueuedEvent + 1) % EVENT_QUEUE_LEN;
|
|
|
|
// If queue wrapped around, remove last element.
|
|
|
|
if (nextQueuedEvent == lastQueuedEvent) {
|
|
|
|
nextQueuedEvent = (nextQueuedEvent + 1) % EVENT_QUEUE_LEN;
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-04 12:16:34 +00:00
|
|
|
LeaveCriticalSection(&cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#endif
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
int GetQueuedKeyEvent(keyEvent *event)
|
|
|
|
{
|
|
|
|
if (lastQueuedEvent == nextQueuedEvent)
|
|
|
|
return 0;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-04 12:16:34 +00:00
|
|
|
EnterCriticalSection(&cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#else
|
2016-09-04 12:16:34 +00:00
|
|
|
std::lock_guard<std::mutex> lock(cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#endif
|
2016-09-04 12:16:34 +00:00
|
|
|
*event = queuedEvents[nextQueuedEvent];
|
|
|
|
nextQueuedEvent = (nextQueuedEvent + 1) % EVENT_QUEUE_LEN;
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-04 12:16:34 +00:00
|
|
|
LeaveCriticalSection(&cSection);
|
2015-02-04 21:27:39 +00:00
|
|
|
#endif
|
2016-09-04 12:16:34 +00:00
|
|
|
return 1;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:16:34 +00:00
|
|
|
void ClearKeyQueue()
|
|
|
|
{
|
|
|
|
lastQueuedEvent = nextQueuedEvent;
|
2015-02-04 21:27:39 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-04 12:16:34 +00:00
|
|
|
if (csInitialized) {
|
|
|
|
DeleteCriticalSection(&cSection);
|
|
|
|
csInitialized = 0;
|
|
|
|
}
|
2015-02-04 21:27:39 +00:00
|
|
|
#endif
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|