2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2007-03-17 00:07:34 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2007-03-17 00:07:34 +00:00
|
|
|
* 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.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2007-03-17 00:07:34 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-29 16:18:43 +00:00
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
2007-03-17 00:07:34 +00:00
|
|
|
#if !defined(DISABLE_DEFAULT_EVENTMANAGER)
|
|
|
|
|
|
|
|
#include "common/system.h"
|
2007-09-19 13:55:05 +00:00
|
|
|
#include "common/config-manager.h"
|
2011-06-13 22:19:18 +01:00
|
|
|
#include "common/translation.h"
|
2007-03-17 00:07:34 +00:00
|
|
|
#include "backends/events/default/default-events.h"
|
2020-01-23 10:50:52 +01:00
|
|
|
#include "backends/keymapper/action.h"
|
2008-08-07 16:38:39 +00:00
|
|
|
#include "backends/keymapper/keymapper.h"
|
2020-01-23 10:50:52 +01:00
|
|
|
#include "backends/keymapper/remap-widget.h"
|
2008-08-07 16:38:39 +00:00
|
|
|
#include "backends/vkeybd/virtual-keyboard.h"
|
2007-06-30 18:22:21 +00:00
|
|
|
|
|
|
|
#include "engines/engine.h"
|
2007-06-30 12:43:53 +00:00
|
|
|
#include "gui/message.h"
|
2007-03-17 00:07:34 +00:00
|
|
|
|
2009-07-25 00:59:03 +00:00
|
|
|
DefaultEventManager::DefaultEventManager(Common::EventSource *boss) :
|
2007-03-17 00:07:34 +00:00
|
|
|
_buttonState(0),
|
|
|
|
_modifierState(0),
|
2008-07-02 00:30:49 +00:00
|
|
|
_shouldQuit(false),
|
2009-01-02 01:21:38 +00:00
|
|
|
_shouldRTL(false),
|
2017-09-09 15:47:37 +02:00
|
|
|
_confirmExitDialogActive(false),
|
|
|
|
_shouldGenerateKeyRepeatEvents(true) {
|
2007-03-17 00:07:34 +00:00
|
|
|
|
2009-07-25 01:00:37 +00:00
|
|
|
assert(boss);
|
|
|
|
|
2009-07-25 01:01:22 +00:00
|
|
|
_dispatcher.registerSource(boss, false);
|
|
|
|
_dispatcher.registerSource(&_artificialEventSource, false);
|
2009-07-25 01:00:37 +00:00
|
|
|
|
2009-07-25 01:01:41 +00:00
|
|
|
_dispatcher.registerObserver(this, kEventManPriority, false);
|
2007-03-17 15:44:26 +00:00
|
|
|
|
|
|
|
// Reset key repeat
|
2013-09-22 11:47:37 +03:00
|
|
|
_keyRepeatTime = 0;
|
|
|
|
|
2008-09-30 13:51:01 +00:00
|
|
|
#ifdef ENABLE_VKEYBD
|
2017-11-28 05:48:01 +01:00
|
|
|
_vk = nullptr;
|
2008-09-30 13:51:01 +00:00
|
|
|
#endif
|
2008-08-06 14:21:05 +00:00
|
|
|
_keymapper = new Common::Keymapper(this);
|
2009-07-25 01:01:05 +00:00
|
|
|
// EventDispatcher will automatically free the keymapper
|
2009-07-25 01:01:22 +00:00
|
|
|
_dispatcher.registerMapper(_keymapper);
|
2008-08-13 19:24:52 +00:00
|
|
|
_remap = false;
|
2007-09-19 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DefaultEventManager::~DefaultEventManager() {
|
2008-09-30 13:51:01 +00:00
|
|
|
#ifdef ENABLE_VKEYBD
|
2008-07-09 13:33:36 +00:00
|
|
|
delete _vk;
|
2008-09-30 13:51:01 +00:00
|
|
|
#endif
|
2007-09-19 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2008-08-18 10:07:11 +00:00
|
|
|
void DefaultEventManager::init() {
|
2008-09-30 13:51:01 +00:00
|
|
|
#ifdef ENABLE_VKEYBD
|
2017-11-28 05:48:01 +01:00
|
|
|
_vk = new Common::VirtualKeyboard();
|
|
|
|
|
2008-08-18 10:07:11 +00:00
|
|
|
if (ConfMan.hasKey("vkeybd_pack_name")) {
|
|
|
|
_vk->loadKeyboardPack(ConfMan.get("vkeybd_pack_name"));
|
|
|
|
} else {
|
2009-06-08 14:47:38 +00:00
|
|
|
_vk->loadKeyboardPack("vkeybd_default");
|
2008-08-18 10:07:11 +00:00
|
|
|
}
|
2008-09-30 13:51:01 +00:00
|
|
|
#endif
|
2008-08-18 10:07:11 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 19:02:05 +00:00
|
|
|
bool DefaultEventManager::pollEvent(Common::Event &event) {
|
2009-07-25 01:01:22 +00:00
|
|
|
_dispatcher.dispatch();
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2017-09-09 15:47:37 +02:00
|
|
|
if (_shouldGenerateKeyRepeatEvents) {
|
|
|
|
handleKeyRepeat();
|
|
|
|
}
|
2010-07-17 18:41:38 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
if (_eventQueue.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-24 21:15:30 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
event = _eventQueue.pop();
|
|
|
|
bool forwardEvent = true;
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case Common::EVENT_KEYDOWN:
|
|
|
|
_modifierState = event.kbd.flags;
|
|
|
|
|
|
|
|
if (event.kbd.keycode == Common::KEYCODE_BACKSPACE) {
|
|
|
|
// WORKAROUND: Some engines incorrectly attempt to use the
|
|
|
|
// ascii value instead of the keycode to detect the backspace
|
|
|
|
// key (a non-portable behavior). This fails at least on
|
|
|
|
// Mac OS X, possibly also on other systems.
|
|
|
|
// As a workaround, we force the ascii value for backspace
|
|
|
|
// key pressed. A better fix would be for engines to stop
|
|
|
|
// making invalid assumptions about ascii values.
|
|
|
|
event.kbd.ascii = Common::KEYCODE_BACKSPACE;
|
|
|
|
_currentKeyDown.ascii = Common::KEYCODE_BACKSPACE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_KEYUP:
|
|
|
|
_modifierState = event.kbd.flags;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_MOUSEMOVE:
|
|
|
|
_mousePos = event.mouse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_LBUTTONDOWN:
|
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState |= LBUTTON;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_LBUTTONUP:
|
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState &= ~LBUTTON;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_RBUTTONDOWN:
|
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState |= RBUTTON;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_RBUTTONUP:
|
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState &= ~RBUTTON;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_MAINMENU:
|
|
|
|
if (g_engine && !g_engine->isPaused())
|
|
|
|
g_engine->openMainMenuDialog();
|
|
|
|
|
|
|
|
if (_shouldQuit)
|
|
|
|
event.type = Common::EVENT_QUIT;
|
|
|
|
else if (_shouldRTL)
|
|
|
|
event.type = Common::EVENT_RTL;
|
|
|
|
break;
|
|
|
|
#ifdef ENABLE_VKEYBD
|
|
|
|
case Common::EVENT_VIRTUAL_KEYBOARD:
|
2017-11-28 05:48:01 +01:00
|
|
|
if (!_vk)
|
|
|
|
break;
|
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
if (_vk->isDisplaying()) {
|
|
|
|
_vk->close(true);
|
|
|
|
} else {
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(true);
|
|
|
|
_vk->show();
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(false);
|
|
|
|
forwardEvent = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case Common::EVENT_RTL:
|
|
|
|
if (ConfMan.getBool("confirm_exit")) {
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(true);
|
|
|
|
GUI::MessageDialog alert(_("Do you really want to return to the Launcher?"), _("Launcher"), _("Cancel"));
|
|
|
|
forwardEvent = _shouldRTL = (alert.runModal() == GUI::kMessageOK);
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(false);
|
|
|
|
} else
|
|
|
|
_shouldRTL = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_MUTE:
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->flipMute();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Common::EVENT_QUIT:
|
|
|
|
if (ConfMan.getBool("confirm_exit")) {
|
|
|
|
if (_confirmExitDialogActive) {
|
|
|
|
forwardEvent = false;
|
|
|
|
break;
|
2007-03-17 15:44:26 +00:00
|
|
|
}
|
2017-09-09 15:44:08 +02:00
|
|
|
_confirmExitDialogActive = true;
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(true);
|
|
|
|
GUI::MessageDialog alert(_("Do you really want to quit?"), _("Quit"), _("Cancel"));
|
|
|
|
forwardEvent = _shouldQuit = (alert.runModal() == GUI::kMessageOK);
|
|
|
|
if (g_engine)
|
|
|
|
g_engine->pauseEngine(false);
|
|
|
|
_confirmExitDialogActive = false;
|
|
|
|
} else
|
|
|
|
_shouldQuit = true;
|
2008-06-24 21:15:30 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
break;
|
2007-03-17 00:07:34 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-06-24 21:15:30 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
return forwardEvent;
|
|
|
|
}
|
2007-03-17 00:07:34 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
void DefaultEventManager::handleKeyRepeat() {
|
|
|
|
uint32 time = g_system->getMillis(true);
|
2008-07-16 04:22:56 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
if (!_eventQueue.empty()) {
|
|
|
|
// Peek in the event queue
|
|
|
|
const Common::Event &nextEvent = _eventQueue.front();
|
2008-06-24 21:15:30 +00:00
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
switch (nextEvent.type) {
|
|
|
|
case Common::EVENT_KEYDOWN:
|
|
|
|
// init continuous event stream
|
|
|
|
_currentKeyDown = nextEvent.kbd;
|
|
|
|
_keyRepeatTime = time + kKeyRepeatInitialDelay;
|
2009-06-06 17:36:06 +00:00
|
|
|
break;
|
|
|
|
|
2017-09-09 15:44:08 +02:00
|
|
|
case Common::EVENT_KEYUP:
|
|
|
|
if (nextEvent.kbd.keycode == _currentKeyDown.keycode) {
|
|
|
|
// Only stop firing events if it's the current key
|
|
|
|
_currentKeyDown.keycode = Common::KEYCODE_INVALID;
|
|
|
|
}
|
2007-03-17 00:07:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-03-17 15:44:26 +00:00
|
|
|
} else {
|
|
|
|
// Check if event should be sent again (keydown)
|
2017-11-19 20:07:51 +01:00
|
|
|
if (_currentKeyDown.keycode != Common::KEYCODE_INVALID && _keyRepeatTime <= time) {
|
2007-03-17 15:44:26 +00:00
|
|
|
// fire event
|
2017-09-09 15:44:08 +02:00
|
|
|
Common::Event repeatEvent;
|
|
|
|
repeatEvent.type = Common::EVENT_KEYDOWN;
|
|
|
|
repeatEvent.kbdRepeat = true;
|
|
|
|
repeatEvent.kbd = _currentKeyDown;
|
2007-03-17 15:44:26 +00:00
|
|
|
_keyRepeatTime = time + kKeyRepeatSustainDelay;
|
2017-09-09 15:44:08 +02:00
|
|
|
|
|
|
|
_eventQueue.push(repeatEvent);
|
2007-03-17 15:44:26 +00:00
|
|
|
}
|
2007-03-17 00:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-21 18:53:55 +00:00
|
|
|
void DefaultEventManager::pushEvent(const Common::Event &event) {
|
2008-07-13 20:41:39 +00:00
|
|
|
// If already received an EVENT_QUIT, don't add another one
|
|
|
|
if (event.type == Common::EVENT_QUIT) {
|
|
|
|
if (!_shouldQuit)
|
2009-07-25 00:59:53 +00:00
|
|
|
_artificialEventSource.addEvent(event);
|
2009-01-01 15:06:43 +00:00
|
|
|
} else
|
2009-07-25 00:59:53 +00:00
|
|
|
_artificialEventSource.addEvent(event);
|
2008-07-07 22:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:59:18 -05:00
|
|
|
void DefaultEventManager::purgeMouseEvents() {
|
|
|
|
_dispatcher.dispatch();
|
|
|
|
|
|
|
|
Common::Queue<Common::Event> filteredQueue;
|
|
|
|
while (!_eventQueue.empty()) {
|
|
|
|
Common::Event event = _eventQueue.pop();
|
|
|
|
switch (event.type) {
|
2019-05-21 17:42:55 +02:00
|
|
|
// Update button state even when purging events to avoid desynchronisation with real button state
|
2017-09-15 22:59:18 -05:00
|
|
|
case Common::EVENT_LBUTTONDOWN:
|
2019-05-21 17:42:55 +02:00
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState |= LBUTTON;
|
|
|
|
break;
|
|
|
|
|
2017-09-15 22:59:18 -05:00
|
|
|
case Common::EVENT_LBUTTONUP:
|
2019-05-21 17:42:55 +02:00
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState &= ~LBUTTON;
|
|
|
|
break;
|
|
|
|
|
2017-09-15 22:59:18 -05:00
|
|
|
case Common::EVENT_RBUTTONDOWN:
|
2019-05-21 17:42:55 +02:00
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState |= RBUTTON;
|
|
|
|
break;
|
|
|
|
|
2017-09-15 22:59:18 -05:00
|
|
|
case Common::EVENT_RBUTTONUP:
|
2019-05-21 17:42:55 +02:00
|
|
|
_mousePos = event.mouse;
|
|
|
|
_buttonState &= ~RBUTTON;
|
|
|
|
break;
|
|
|
|
|
2017-09-15 22:59:18 -05:00
|
|
|
case Common::EVENT_WHEELUP:
|
|
|
|
case Common::EVENT_WHEELDOWN:
|
|
|
|
case Common::EVENT_MBUTTONDOWN:
|
|
|
|
case Common::EVENT_MBUTTONUP:
|
2019-05-21 17:42:55 +02:00
|
|
|
case Common::EVENT_MOUSEMOVE:
|
2017-09-15 22:59:18 -05:00
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filteredQueue.push(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_eventQueue = filteredQueue;
|
|
|
|
}
|
|
|
|
|
2020-01-23 10:50:52 +01:00
|
|
|
Common::Keymap *DefaultEventManager::getGlobalKeymap() {
|
|
|
|
using namespace Common;
|
|
|
|
|
|
|
|
// Now create the global keymap
|
|
|
|
Keymap *globalKeymap = new Keymap(Keymap::kKeymapTypeGlobal, kGlobalKeymapName);
|
|
|
|
|
|
|
|
Action *act;
|
|
|
|
act = new Action("MENU", _("Menu"));
|
|
|
|
act->addDefaultInputMapping("C+F5");
|
|
|
|
act->setEvent(EVENT_MAINMENU);
|
|
|
|
globalKeymap->addAction(act);
|
|
|
|
|
|
|
|
#ifdef ENABLE_VKEYBD
|
|
|
|
act = new Action("VIRT", _("Display keyboard"));
|
|
|
|
act->addDefaultInputMapping("C+F7");
|
|
|
|
act->setEvent(EVENT_VIRTUAL_KEYBOARD);
|
|
|
|
globalKeymap->addAction(act);
|
|
|
|
#endif
|
|
|
|
|
2020-01-24 11:13:34 +01:00
|
|
|
act = new Action("MUTE", _("Toggle mute"));
|
|
|
|
act->addDefaultInputMapping("C+u");
|
|
|
|
act->setEvent(EVENT_MUTE);
|
2020-01-23 10:50:52 +01:00
|
|
|
globalKeymap->addAction(act);
|
|
|
|
|
2020-01-24 11:13:34 +01:00
|
|
|
act = new Action("QUIT", _("Quit"));
|
|
|
|
act->setEvent(EVENT_QUIT);
|
|
|
|
|
|
|
|
#if defined(MACOSX)
|
|
|
|
// On Macintosh, Cmd-Q quits
|
|
|
|
act->addDefaultInputMapping("M+q");
|
|
|
|
#elif defined(POSIX)
|
|
|
|
// On other *nix systems, Control-Q quits
|
|
|
|
act->addDefaultInputMapping("C+q");
|
|
|
|
#else
|
|
|
|
// Ctrl-z quits
|
|
|
|
act->addDefaultInputMapping("C+z");
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
// On Windows, also use the default Alt-F4 quit combination
|
|
|
|
act->addDefaultInputMapping("A+F4");
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-01-23 10:50:52 +01:00
|
|
|
|
|
|
|
globalKeymap->addAction(act);
|
|
|
|
|
|
|
|
return globalKeymap;
|
|
|
|
}
|
|
|
|
|
2007-03-17 00:07:34 +00:00
|
|
|
#endif // !defined(DISABLE_DEFAULT_EVENTMANAGER)
|