Added SdlEventManager.

svn-id: r49635
This commit is contained in:
Alejandro Marzini 2010-06-13 20:31:25 +00:00
parent 3cfa482b43
commit 360b82858c
4 changed files with 156 additions and 89 deletions

View File

@ -23,10 +23,11 @@
*
*/
#if defined(WIN32) || defined(UNIX) || defined(MACOSX)
#include "backends/events/sdl/sdl-events.h"
#include "backends/platform/sdl/sdl.h"
#include "common/util.h"
#include "common/events.h"
#include "graphics/scaler/aspect.h" // for aspect2Real
#include "common/config-manager.h"
// FIXME move joystick defines out and replace with confile file options
// we should really allow users to map any key to a joystick button
@ -47,8 +48,35 @@
#define JOY_BUT_SPACE 4
#define JOY_BUT_F5 5
SdlEventManager::SdlEventManager(Common::EventSource *boss)
:
_scrollLock(false),
_joystick(0),
DefaultEventManager(boss) {
// reset mouse state
memset(&_km, 0, sizeof(_km));
int joystick_num = ConfMan.getInt("joystick_num");
if (joystick_num > -1) {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) {
error("Could not initialize SDL: %s", SDL_GetError());
}
}
// enable joystick
if (joystick_num > -1 && SDL_NumJoysticks() > 0) {
printf("Using joystick: %s\n", SDL_JoystickName(0));
_joystick = SDL_JoystickOpen(joystick_num);
}
}
SdlEventManager::~SdlEventManager() {
if (_joystick)
SDL_JoystickClose(_joystick);
}
static int mapKey(SDLKey key, SDLMod mod, Uint16 unicode) {
if (key >= SDLK_F1 && key <= SDLK_F9) {
@ -67,7 +95,7 @@ static int mapKey(SDLKey key, SDLMod mod, Uint16 unicode) {
return key;
}
void OSystem_SDL::fillMouseEvent(Common::Event &event, int x, int y) {
void SdlEventManager::fillMouseEvent(Common::Event &event, int x, int y) {
event.mouse.x = x;
event.mouse.y = y;
@ -84,8 +112,8 @@ void OSystem_SDL::fillMouseEvent(Common::Event &event, int x, int y) {
}*/
}
void OSystem_SDL::handleKbdMouse() {
uint32 curTime = getMillis();
void SdlEventManager::handleKbdMouse() {
uint32 curTime = g_system->getMillis();
if (curTime >= _km.last_time + _km.delay_time) {
_km.last_time = curTime;
if (_km.x_down_count == 1) {
@ -178,7 +206,7 @@ static void SDLModToOSystemKeyFlags(SDLMod mod, Common::Event &event) {
event.kbd.flags |= Common::KBD_CAPS;
}
bool OSystem_SDL::pollEvent(Common::Event &event) {
bool SdlEventManager::pollSdlEvent(Common::Event &event) {
SDL_Event ev;
handleKbdMouse();
@ -198,7 +226,7 @@ bool OSystem_SDL::pollEvent(Common::Event &event) {
return false;
}
bool OSystem_SDL::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
switch (ev.type) {
case SDL_KEYDOWN:
return handleKeyDown(ev, event);
@ -218,7 +246,7 @@ bool OSystem_SDL::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
return handleJoyAxisMotion(ev, event);
case SDL_VIDEOEXPOSE:
((SdlGraphicsManager *)_graphicsManager)->forceFullRedraw();
((OSystem_SDL *) g_system)->getGraphicsManager()->forceFullRedraw();
break;
case SDL_QUIT:
@ -231,7 +259,7 @@ bool OSystem_SDL::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
}
bool OSystem_SDL::handleKeyDown(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleKeyDown(SDL_Event &ev, Common::Event &event) {
SDLModToOSystemKeyFlags(SDL_GetModState(), event);
@ -312,7 +340,7 @@ bool OSystem_SDL::handleKeyDown(SDL_Event &ev, Common::Event &event) {
// Ctrl-Alt-<key> will change the GFX mode
if ((event.kbd.flags & (Common::KBD_CTRL|Common::KBD_ALT)) == (Common::KBD_CTRL|Common::KBD_ALT)) {
if (((SdlGraphicsManager *)_graphicsManager)->handleScalerHotkeys(ev.key))
if (((OSystem_SDL *) g_system)->getGraphicsManager()->handleScalerHotkeys(ev.key))
return false;
}
@ -326,7 +354,7 @@ bool OSystem_SDL::handleKeyDown(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::handleKeyUp(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleKeyUp(SDL_Event &ev, Common::Event &event) {
if (remapKey(ev, event))
return true;
@ -341,22 +369,22 @@ bool OSystem_SDL::handleKeyUp(SDL_Event &ev, Common::Event &event) {
if (_scrollLock)
event.kbd.flags |= Common::KBD_SCRL;
if (((SdlGraphicsManager *)_graphicsManager)->isScalerHotkey(event))
if (((OSystem_SDL *) g_system)->getGraphicsManager()->isScalerHotkey(event))
// Swallow these key up events
return false;
return true;
}
bool OSystem_SDL::handleMouseMotion(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleMouseMotion(SDL_Event &ev, Common::Event &event) {
event.type = Common::EVENT_MOUSEMOVE;
fillMouseEvent(event, ev.motion.x, ev.motion.y);
((SdlGraphicsManager *)_graphicsManager)->setMousePos(event.mouse.x, event.mouse.y);
((OSystem_SDL *) g_system)->getGraphicsManager()->setMousePos(event.mouse.x, event.mouse.y);
return true;
}
bool OSystem_SDL::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
if (ev.button.button == SDL_BUTTON_LEFT)
event.type = Common::EVENT_LBUTTONDOWN;
else if (ev.button.button == SDL_BUTTON_RIGHT)
@ -379,7 +407,7 @@ bool OSystem_SDL::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
if (ev.button.button == SDL_BUTTON_LEFT)
event.type = Common::EVENT_LBUTTONUP;
else if (ev.button.button == SDL_BUTTON_RIGHT)
@ -395,7 +423,7 @@ bool OSystem_SDL::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::handleJoyButtonDown(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleJoyButtonDown(SDL_Event &ev, Common::Event &event) {
if (ev.jbutton.button == JOY_BUT_LMOUSE) {
event.type = Common::EVENT_LBUTTONDOWN;
fillMouseEvent(event, _km.x, _km.y);
@ -426,7 +454,7 @@ bool OSystem_SDL::handleJoyButtonDown(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::handleJoyButtonUp(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleJoyButtonUp(SDL_Event &ev, Common::Event &event) {
if (ev.jbutton.button == JOY_BUT_LMOUSE) {
event.type = Common::EVENT_LBUTTONUP;
fillMouseEvent(event, _km.x, _km.y);
@ -457,7 +485,7 @@ bool OSystem_SDL::handleJoyButtonUp(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::handleJoyAxisMotion(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::handleJoyAxisMotion(SDL_Event &ev, Common::Event &event) {
int axis = ev.jaxis.value;
if ( axis > JOY_DEADZONE) {
axis -= JOY_DEADZONE;
@ -505,7 +533,7 @@ bool OSystem_SDL::handleJoyAxisMotion(SDL_Event &ev, Common::Event &event) {
return true;
}
bool OSystem_SDL::remapKey(SDL_Event &ev, Common::Event &event) {
bool SdlEventManager::remapKey(SDL_Event &ev, Common::Event &event) {
#ifdef LINUPY
// On Yopy map the End button to quit
if ((ev.key.keysym.sym == 293)) {
@ -573,9 +601,11 @@ bool OSystem_SDL::remapKey(SDL_Event &ev, Common::Event &event) {
return false;
}
void OSystem_SDL::toggleMouseGrab() {
void SdlEventManager::toggleMouseGrab() {
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF)
SDL_WM_GrabInput(SDL_GRAB_ON);
else
SDL_WM_GrabInput(SDL_GRAB_OFF);
}
#endif

View File

@ -0,0 +1,85 @@
/* 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.
*
* 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.
*
* $URL$
* $Id$
*
*/
#if !defined(BACKEND_EVENTS_SDL_H) && !defined(DISABLE_DEFAULT_EVENTMANAGER)
#define BACKEND_EVENTS_SDL_H
#include "backends/events/default/default-events.h"
#if defined(__SYMBIAN32__)
#include <esdl\SDL.h>
#else
#include <SDL.h>
#endif
class SdlEventManager : public DefaultEventManager {
public:
SdlEventManager(Common::EventSource *boss);
~SdlEventManager();
virtual bool pollSdlEvent(Common::Event &event);
protected:
virtual void preprocessEvents(SDL_Event *event) {}
// Keyboard mouse emulation. Disabled by fingolfin 2004-12-18.
// I am keeping the rest of the code in for now, since the joystick
// code (or rather, "hack") uses it, too.
struct KbdMouse {
int16 x, y, x_vel, y_vel, x_max, y_max, x_down_count, y_down_count;
uint32 last_time, delay_time, x_down_time, y_down_time;
};
KbdMouse _km;
// Scroll lock state - since SDL doesn't track it
bool _scrollLock;
// joystick
SDL_Joystick *_joystick;
virtual bool dispatchSDLEvent(SDL_Event &ev, Common::Event &event);
// Handlers for specific SDL events, called by pollEvent.
// This way, if a backend inherits fromt the SDL backend, it can
// change the behavior of only a single event, without having to override all
// of pollEvent.
virtual bool handleKeyDown(SDL_Event &ev, Common::Event &event);
virtual bool handleKeyUp(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseMotion(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseButtonDown(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseButtonUp(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyButtonDown(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyButtonUp(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyAxisMotion(SDL_Event &ev, Common::Event &event);
virtual void fillMouseEvent(Common::Event &event, int x, int y); // overloaded by CE backend
void toggleMouseGrab();
void handleKbdMouse();
virtual bool remapKey(SDL_Event &ev, Common::Event &event);
};
#endif

View File

@ -45,6 +45,8 @@
#include "backends/mixer/sdl/sdl-mixer.h"
#include "backends/events/sdl/sdl-events.h"
#include "icons/scummvm.xpm"
#include <time.h> // for getTimeAndDate()
@ -86,15 +88,11 @@ static Uint32 timer_handler(Uint32 interval, void *param) {
void OSystem_SDL::initBackend() {
assert(!_inited);
int joystick_num = ConfMan.getInt("joystick_num");
uint32 sdlFlags = 0;
if (ConfMan.hasKey("disable_sdl_parachute"))
sdlFlags |= SDL_INIT_NOPARACHUTE;
if (joystick_num > -1)
sdlFlags |= SDL_INIT_JOYSTICK;
if (SDL_Init(sdlFlags) == -1) {
error("Could not initialize SDL: %s", SDL_GetError());
}
@ -108,16 +106,10 @@ void OSystem_SDL::initBackend() {
_mutexManager = new SdlMutexManager();
}
// enable joystick
if (joystick_num > -1 && SDL_NumJoysticks() > 0) {
printf("Using joystick: %s\n", SDL_JoystickName(0));
_joystick = SDL_JoystickOpen(joystick_num);
}
// Create and hook up the event manager, if none exists yet (we check for
// this to allow subclasses to provide their own).
if (_eventManager == 0) {
_eventManager = new DefaultEventManager(this);
_eventManager = new SdlEventManager(this);
}
// Create the savefile manager, if none exists yet (we check for this to
@ -177,14 +169,7 @@ void OSystem_SDL::initBackend() {
OSystem_SDL::OSystem_SDL()
:
_scrollLock(false),
_joystick(0) {
// reset mouse state
memset(&_km, 0, sizeof(_km));
_inited = false;
_inited(false) {
#if defined(__amigaos4__)
_fsFactory = new AmigaOSFilesystemFactory();
#elif defined(UNIX)
@ -351,9 +336,6 @@ void OSystem_SDL::setWindowCaption(const char *caption) {
}
void OSystem_SDL::deinit() {
if (_joystick)
SDL_JoystickClose(_joystick);
SDL_ShowCursor(SDL_ENABLE);
SDL_RemoveTimer(_timerID);
@ -366,7 +348,7 @@ void OSystem_SDL::deinit() {
// Event Manager requires save manager for storing
// recorded events
delete getEventManager();
delete _eventManager;
delete _savefileManager;
}
@ -429,3 +411,13 @@ void OSystem_SDL::setupIcon() {
SDL_FreeSurface(sdl_surf);
free(icon);
}
SdlGraphicsManager *OSystem_SDL::getGraphicsManager() {
assert(_graphicsManager);
return (SdlGraphicsManager *)_graphicsManager;
}
bool OSystem_SDL::pollEvent(Common::Event &event) {
assert(_eventManager);
return ((SdlEventManager *)_eventManager)->pollSdlEvent(event);
}

View File

@ -65,15 +65,9 @@ public:
virtual void getTimeAndDate(TimeDate &t) const;
// Get the next event.
// Returns true if an event was retrieved.
virtual bool pollEvent(Common::Event &event); // overloaded by CE backend
// Define all hardware keys for keymapper
virtual Common::HardwareKeySet *getHardwareKeySet();
virtual void preprocessEvents(SDL_Event *event) {}
// Quit
virtual void quit(); // overloaded by CE backend
@ -85,50 +79,16 @@ public:
virtual Common::SeekableReadStream *createConfigReadStream();
virtual Common::WriteStream *createConfigWriteStream();
SdlGraphicsManager *getGraphicsManager();
virtual bool pollEvent(Common::Event &event);
protected:
bool _inited;
// Keyboard mouse emulation. Disabled by fingolfin 2004-12-18.
// I am keeping the rest of the code in for now, since the joystick
// code (or rather, "hack") uses it, too.
struct KbdMouse {
int16 x, y, x_vel, y_vel, x_max, y_max, x_down_count, y_down_count;
uint32 last_time, delay_time, x_down_time, y_down_time;
};
KbdMouse _km;
// Scroll lock state - since SDL doesn't track it
bool _scrollLock;
// joystick
SDL_Joystick *_joystick;
virtual bool dispatchSDLEvent(SDL_Event &ev, Common::Event &event);
// Handlers for specific SDL events, called by pollEvent.
// This way, if a backend inherits fromt the SDL backend, it can
// change the behavior of only a single event, without having to override all
// of pollEvent.
virtual bool handleKeyDown(SDL_Event &ev, Common::Event &event);
virtual bool handleKeyUp(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseMotion(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseButtonDown(SDL_Event &ev, Common::Event &event);
virtual bool handleMouseButtonUp(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyButtonDown(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyButtonUp(SDL_Event &ev, Common::Event &event);
virtual bool handleJoyAxisMotion(SDL_Event &ev, Common::Event &event);
SDL_TimerID _timerID;
virtual void fillMouseEvent(Common::Event &event, int x, int y); // overloaded by CE backend
void toggleMouseGrab();
void handleKbdMouse();
void setupIcon();
virtual bool remapKey(SDL_Event &ev, Common::Event &event);
};
#endif