WEBOS: Complete rewrite of key/mouse events handling

Touchscreen is now always used like a touch pad.
This commit is contained in:
Klaus Reimer 2011-04-09 18:03:19 +02:00 committed by Max Horn
parent e5dab6672c
commit 6c94353eff
6 changed files with 223 additions and 267 deletions

View file

@ -24,90 +24,218 @@
*/
#include "common/scummsys.h"
#include <stdio.h>
#include "sys/time.h"
#include "time.h"
#ifdef WEBOS
#include "backends/events/webossdl/webossdl-events.h"
#include "gui/message.h"
static int mouseButton = 0;
// Inidicates if gesture area is pressed down or not.
static bool gestureDown = false;
// WebOS devices only have a shift key and a ctrl key. There is also an alt
// key (the orange key) but this is already processed by WebOS to change the
// mode of the keys so ScummVM must not use this key as a modifier.
void WebOSSdlEventSource::SDLModToOSystemKeyFlags(SDLMod mod, Common::Event &event) {
event.kbd.flags = 0;
// The timestamp when gesture area was pressed down.
static int gestureDownTime = 0;
if (mod & KMOD_SHIFT)
event.kbd.flags |= Common::KBD_SHIFT;
if (mod & KMOD_CTRL)
event.kbd.flags |= Common::KBD_CTRL;
// The timestamp when screen was pressed down.
static int screenDownTime = 0;
// The timestamp when a possible drag operation was triggered.
static int dragStartTime = 0;
// The index of the motion pointer.
static int motionPtrIndex = -1;
// The maximum horizontal motion during dragging (For tap recognition).
static int dragDiffX = 0;
// The maximum vertical motion during dragging (For tap recognition).
static int dragDiffY = 0;
// Indicates if we are in drag mode.
static bool dragging = false;
// The current mouse position on the screen.
static int curX = 0, curY = 0;
// The time (seconds after 1/1/1970) when program started.
static time_t programStartTime = time(0);
/**
* Returns the number of passed milliseconds since program start.
*
* @return The number of passed milliseconds.
*/
static time_t getMillis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (time(0) - programStartTime) * 1000 + tv.tv_usec / 1000;
}
// Capture Shift and Control to decide the active mouse button
bool WebOSSdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) {
int sym = ev.key.keysym.sym;
switch (ev.type) {
case SDL_KEYDOWN:
if (sym == 304) {
mouseButton = 1;
return true;
}
else if (sym == 305) {
mouseButton = 2;
return true;
}
break;
case SDL_KEYUP:
if (sym == 304 || sym == 305) {
mouseButton = 0;
return true;
}
break;
/**
* WebOS devices only have a Shift key and a CTRL key. There is also an Alt
* key (the orange key) but this is already processed by WebOS to change the
* mode of the keys so ScummVM must not use this key as a modifier. Instead
* pressing down the gesture area is used as Alt key.
*
* @param mod The pressed key modifier as detected by SDL.
* @param event The ScummVM event to setup.
*/
void WebOSSdlEventSource::SDLModToOSystemKeyFlags(SDLMod mod,
Common::Event &event) {
event.kbd.flags = 0;
if (mod & KMOD_SHIFT)
event.kbd.flags |= Common::KBD_SHIFT;
if (mod & KMOD_CTRL)
event.kbd.flags |= Common::KBD_CTRL;
if (gestureDown)
event.kbd.flags |= Common::KBD_ALT;
}
/**
* Before calling the original SDL implementation this method checks if the
* gesture area is pressed down.
*
* @param ev The SDL event
* @param event The ScummVM event.
* @return True if event was processed, false if not.
*/
bool WebOSSdlEventSource::handleKeyDown(SDL_Event &ev, Common::Event &event) {
// Handle gesture area tap.
if (ev.key.keysym.sym == SDLK_WORLD_71) {
gestureDown = true;
gestureDownTime = getMillis();
return true;
}
// Invoke parent implementation of this method
return SdlEventSource::remapKey(ev, event);
// Call original SDL key handler.
return SdlEventSource::handleKeyDown(ev, event);
}
/**
* Before calling the original SDL implementation this method checks if the
* gesture area has been released.
*
* @param ev The SDL event
* @param event The ScummVM event.
* @return True if event was processed, false if not.
*/
bool WebOSSdlEventSource::handleKeyUp(SDL_Event &ev, Common::Event &event) {
// Handle gesture area tap.
if (ev.key.keysym.sym == SDLK_WORLD_71) {
gestureDown = false;
return true;
}
// Call original SDL key handler.
return SdlEventSource::handleKeyUp(ev, event);
}
/**
* Handles mouse button press.
*
* @param ev The SDL event
* @param event The ScummVM event.
* @return True if event was processed, false if not.
*/
bool WebOSSdlEventSource::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
// Simulate right mouse button
if (ev.button.button == SDL_BUTTON_LEFT && mouseButton == 1) {
event.type = Common::EVENT_RBUTTONDOWN;
fillMouseEvent(event, ev.button.x, ev.button.y);
printf("rbutton down\n");
return true;
}
if (motionPtrIndex == -1) {
motionPtrIndex = ev.button.which;
dragDiffX = 0;
dragDiffY = 0;
screenDownTime = getMillis();
// Simulate middle mouse button
if (ev.button.button == SDL_BUTTON_LEFT && mouseButton == 2) {
event.type = Common::EVENT_MBUTTONDOWN;
fillMouseEvent(event, ev.button.x, ev.button.y);
printf("rbutton down\n");
return true;
// Start dragging when pressing the screen shortly after a tap.
if (getMillis() - dragStartTime < 250) {
dragging = true;
event.type = Common::EVENT_LBUTTONDOWN;
fillMouseEvent(event, curX, curY);
}
}
// Invoke parent implementation of this method
return SdlEventSource::handleMouseButtonDown(ev, event);
return true;
}
/**
* Handles mouse button release.
*
* @param ev The SDL event
* @param event The ScummVM event.
* @return True if event was processed, false if not.
*/
bool WebOSSdlEventSource::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
// Simulate right mouse button
if (ev.button.button == SDL_BUTTON_LEFT && mouseButton == 1) {
event.type = Common::EVENT_RBUTTONUP;
fillMouseEvent(event, ev.button.x, ev.button.y);
return true;
}
if (motionPtrIndex == ev.button.which) {
motionPtrIndex = -1;
// Simulate right mouse button
if (ev.button.button == SDL_BUTTON_LEFT && mouseButton == 2) {
event.type = Common::EVENT_MBUTTONUP;
fillMouseEvent(event, ev.button.x, ev.button.y);
return true;
}
// When drag mode was active then simply send a mouse up event
if (dragging)
{
event.type = Common::EVENT_LBUTTONUP;
fillMouseEvent(event, curX, curY);
dragging = false;
return true;
}
// Invoke parent implementation of this method
return SdlEventSource::handleMouseButtonUp(ev, event);
// When mouse was moved 5 pixels or less then emulate a mouse button
// click.
if (ABS(dragDiffX) < 6 && ABS(dragDiffY) < 6)
{
int duration = getMillis() - screenDownTime;
// When screen was pressed for less than 500ms then emulate a
// left mouse click.
if (duration < 500) {
event.type = Common::EVENT_LBUTTONUP;
fillMouseEvent(event, curX, curY);
g_system->getEventManager()->pushEvent(event);
event.type = Common::EVENT_LBUTTONDOWN;
dragStartTime = getMillis();
}
// When screen was pressed for less than 1000ms then emulate a
// right mouse click.
else if (duration < 1000) {
event.type = Common::EVENT_RBUTTONUP;
fillMouseEvent(event, curX, curY);
g_system->getEventManager()->pushEvent(event);
event.type = Common::EVENT_RBUTTONDOWN;
}
// When screen was pressed for more than 1000ms then emulate a
// middle mouse click.
else {
event.type = Common::EVENT_MBUTTONUP;
fillMouseEvent(event, curX, curY);
g_system->getEventManager()->pushEvent(event);
event.type = Common::EVENT_MBUTTONDOWN;
}
}
}
return true;
}
/**
* Handles mouse motion.
*
* @param ev The SDL event
* @param event The ScummVM event.
* @return True if event was processed, false if not.
*/
bool WebOSSdlEventSource::handleMouseMotion(SDL_Event &ev, Common::Event &event) {
if (ev.motion.which == motionPtrIndex) {
int screenX = g_system->getWidth();
int screenY = g_system->getHeight();
curX = MIN(screenX, MAX(0, curX + ev.motion.xrel));
curY = MIN(screenY, MAX(0, curY + ev.motion.yrel));
dragDiffX += ev.motion.xrel;
dragDiffY += ev.motion.yrel;
event.type = Common::EVENT_MOUSEMOVE;
fillMouseEvent(event, curX, curY);
}
return true;
}
#endif

View file

@ -34,9 +34,11 @@
class WebOSSdlEventSource : public SdlEventSource {
protected:
virtual void SDLModToOSystemKeyFlags(SDLMod mod, Common::Event &event);
virtual bool remapKey(SDL_Event &ev, Common::Event &event);
virtual bool handleKeyDown(SDL_Event &ev, Common::Event &event);
virtual bool handleKeyUp(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 handleMouseMotion(SDL_Event &ev, Common::Event &event);
};
#endif

View file

@ -1,164 +0,0 @@
/* 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$
*
*/
#include "backends/platform/webos/webos.h"
#include "backends/platform/webos/keyboard.h"
#include "backends/keymapper/keymapper.h"
#include "common/keyboard.h"
#ifdef ENABLE_KEYMAPPER
using namespace Common;
struct Key {
const char *hwId;
KeyCode keycode;
uint16 ascii;
const char *desc;
KeyType preferredAction;
bool shiftable;
};
static const Key keys[] = {
{"BACKSPACE", KEYCODE_BACKSPACE, ASCII_BACKSPACE, "Backspace", kActionKeyType, false},
{"RETURN", KEYCODE_RETURN, ASCII_RETURN, "Return", kActionKeyType, false},
{"ESCAPE", KEYCODE_ESCAPE, ASCII_ESCAPE, "Esc", kStartKeyType, false},
{"SPACE", KEYCODE_SPACE, ASCII_SPACE, "Space", kActionKeyType, false},
{"EXCLAIM", KEYCODE_EXCLAIM, '!', "!", kActionKeyType, false},
{"QUOTEDBL", KEYCODE_QUOTEDBL, '"', "\"", kActionKeyType, false},
{"HASH", KEYCODE_HASH, '#', "#", kActionKeyType, false},
{"DOLLAR", KEYCODE_DOLLAR, '$', "$", kActionKeyType, false},
{"AMPERSAND", KEYCODE_AMPERSAND, '&', "&", kActionKeyType, false},
{"QUOTE", KEYCODE_QUOTE, '\'', "'", kActionKeyType, false},
{"LEFTPAREN", KEYCODE_LEFTPAREN, '(', "(", kActionKeyType, false},
{"RIGHTPAREN", KEYCODE_RIGHTPAREN, ')', ")", kActionKeyType, false},
{"ASTERISK", KEYCODE_ASTERISK, '*', "*", kActionKeyType, false},
{"PLUS", KEYCODE_PLUS, '+', "+", kActionKeyType, false},
{"COMMA", KEYCODE_COMMA, ',', ",", kActionKeyType, false},
{"MINUS", KEYCODE_MINUS, '-', "-", kActionKeyType, false},
{"PERIOD", KEYCODE_PERIOD, '.', ".", kActionKeyType, false},
{"SLASH", KEYCODE_SLASH, '/', "/", kActionKeyType, false},
{"0", KEYCODE_0, '0', "0", kActionKeyType, false},
{"1", KEYCODE_1, '1', "1", kActionKeyType, false},
{"2", KEYCODE_2, '2', "2", kActionKeyType, false},
{"3", KEYCODE_3, '3', "3", kActionKeyType, false},
{"4", KEYCODE_4, '4', "4", kActionKeyType, false},
{"5", KEYCODE_5, '5', "5", kActionKeyType, false},
{"6", KEYCODE_6, '6', "6", kActionKeyType, false},
{"7", KEYCODE_7, '7', "7", kActionKeyType, false},
{"8", KEYCODE_8, '8', "8", kActionKeyType, false},
{"9", KEYCODE_9, '9', "9", kActionKeyType, false},
{"COLON", KEYCODE_COLON, ':', ":", kActionKeyType, false},
{"SEMICOLON", KEYCODE_SEMICOLON, ';', ";", kActionKeyType, false},
{"EQUALS", KEYCODE_EQUALS, '=', "=", kActionKeyType, false},
{"QUESTION", KEYCODE_QUESTION, '?', "?", kActionKeyType, false},
{"AT", KEYCODE_AT, '@', "@", kActionKeyType, false},
{"UNDERSCORE", KEYCODE_UNDERSCORE, '_', "_", kActionKeyType, false},
{"a", KEYCODE_a, 'a', "a", kActionKeyType, true},
{"b", KEYCODE_b, 'b', "b", kActionKeyType, true},
{"c", KEYCODE_c, 'c', "c", kActionKeyType, true},
{"d", KEYCODE_d, 'd', "d", kActionKeyType, true},
{"e", KEYCODE_e, 'e', "e", kActionKeyType, true},
{"f", KEYCODE_f, 'f', "f", kActionKeyType, true},
{"g", KEYCODE_g, 'g', "g", kActionKeyType, true},
{"h", KEYCODE_h, 'h', "h", kActionKeyType, true},
{"i", KEYCODE_i, 'i', "i", kActionKeyType, true},
{"j", KEYCODE_j, 'j', "j", kActionKeyType, true},
{"k", KEYCODE_k, 'k', "k", kActionKeyType, true},
{"l", KEYCODE_l, 'l', "l", kActionKeyType, true},
{"m", KEYCODE_m, 'm', "m", kActionKeyType, true},
{"n", KEYCODE_n, 'n', "n", kActionKeyType, true},
{"o", KEYCODE_o, 'o', "o", kActionKeyType, true},
{"p", KEYCODE_p, 'p', "p", kActionKeyType, true},
{"q", KEYCODE_q, 'q', "q", kActionKeyType, true},
{"r", KEYCODE_r, 'r', "r", kActionKeyType, true},
{"s", KEYCODE_s, 's', "s", kActionKeyType, true},
{"t", KEYCODE_t, 't', "t", kActionKeyType, true},
{"u", KEYCODE_u, 'u', "u", kActionKeyType, true},
{"v", KEYCODE_v, 'v', "v", kActionKeyType, true},
{"w", KEYCODE_w, 'w', "w", kActionKeyType, true},
{"x", KEYCODE_x, 'x', "x", kActionKeyType, true},
{"y", KEYCODE_y, 'y', "y", kActionKeyType, true},
{"z", KEYCODE_z, 'z', "z", kActionKeyType, true},
// Miscellaneous function keys
{ "FORWARD", (KeyCode) KEYCODE_FORWARD, ASCII_FORWARD, "Forward", kActionKeyType, false },
{0, KEYCODE_INVALID, 0, 0, kGenericKeyType, false}
};
struct Mod {
byte flag;
const char *id;
const char *desc;
bool shiftable;
};
static const Mod modifiers[] = {
{ 0, "", "", false },
{ KBD_CTRL, "C+", "Ctrl+", false },
{ KBD_SHIFT, "", "", true },
{ KBD_SHIFT | KBD_CTRL, "S+C+", "Shift+Ctrl+", true },
{ 0, 0, 0, false }
};
#endif
Common::HardwareKeySet *OSystem_SDL_WebOS::getHardwareKeySet() {
#ifdef ENABLE_KEYMAPPER
HardwareKeySet *keySet = new HardwareKeySet();
const Key *key;
const Mod *mod;
char fullKeyId[50];
char fullKeyDesc[100];
uint16 ascii;
for (mod = modifiers; mod->id; mod++) {
for (key = keys; key->hwId; key++) {
ascii = key->ascii;
if (mod->shiftable && key->shiftable) {
snprintf(fullKeyId, 50, "%s%c", mod->id, toupper(key->hwId[0]));
snprintf(fullKeyDesc, 100, "%s%c", mod->desc, toupper(key->desc[0]));
ascii = toupper(key->ascii);
} else if (mod->shiftable) {
snprintf(fullKeyId, 50, "S+%s%s", mod->id, key->hwId);
snprintf(fullKeyDesc, 100, "Shift+%s%s", mod->desc, key->desc);
} else {
snprintf(fullKeyId, 50, "%s%s", mod->id, key->hwId);
snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc);
}
keySet->addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc, key->preferredAction ));
}
}
return keySet;
#else
return 0;
#endif
}

View file

@ -1,37 +0,0 @@
/* 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$
*
*/
#ifndef WEBOS_KEYBOARD_H
#define WEBOS_KEYBOARD_H
enum {
KEYCODE_FORWARD = 229
};
enum {
ASCII_FORWARD = 229
};
#endif

View file

@ -2,8 +2,7 @@ MODULE := backends/platform/webos
MODULE_OBJS := \
main.o \
webos.o \
hardwarekeys.o
webos.o
# We don't use rules.mk but rather manually update OBJS and MODULE_DIRS.
MODULE_OBJS := $(addprefix $(MODULE)/, $(MODULE_OBJS))

View file

@ -25,14 +25,20 @@
#include "backends/platform/webos/webos.h"
#include "backends/events/webossdl/webossdl-events.h"
#include "backends/keymapper/keymapper.h"
#if defined(WEBOS)
using namespace Common;
OSystem_SDL_WebOS::OSystem_SDL_WebOS()
:
OSystem_POSIX("/media/cryptofs/apps/usr/palm/applications/org.scummvm.scummvm/scummvmrc") {
}
/**
* Initializes the backend.
*/
void OSystem_SDL_WebOS::initBackend() {
// Create the events manager
if (_eventSource == 0)
@ -42,4 +48,26 @@ void OSystem_SDL_WebOS::initBackend() {
OSystem_SDL::initBackend();
}
/**
* Gets the original SDL hardware key set, adds WebOS specific keys and
* returns the new key set.
*
* @return The hardware key set with added webOS specific keys.
*/
HardwareKeySet *OSystem_SDL_WebOS::getHardwareKeySet() {
#ifdef ENABLE_KEYMAPPER
// Get the original SDL hardware key set
HardwareKeySet *keySet = OSystem_SDL::getHardwareKeySet();
// Add WebOS specific keys
keySet->addHardwareKey(new HardwareKey("FORWARD",
KeyState((KeyCode) 229, 229, 0), "Forward", kActionKeyType));
// Return the modified hardware key set
return keySet;
#else
return 0;
#endif
}
#endif