~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/keyboardconfig.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Custom keyboard shortcuts configuration
3
 
 *  Copyright (C) 2007  Joshua Langley <joshlangley@optusnet.com.au>
4
 
 *  Copyright (C) 2009-2010  The Mana Developers
5
 
 *  Copyright (C) 2011-2013  The ManaPlus Developers
6
 
 *
7
 
 *  This file is part of The ManaPlus Client.
8
 
 *
9
 
 *  This program is free software; you can redistribute it and/or modify
10
 
 *  it under the terms of the GNU General Public License as published by
11
 
 *  the Free Software Foundation; either version 2 of the License, or
12
 
 *  any later version.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
 */
22
 
 
23
 
#include "keyboardconfig.h"
24
 
 
25
 
#include "configuration.h"
26
 
#include "inputmanager.h"
27
 
#include "logger.h"
28
 
 
29
 
#include "utils/gettext.h"
30
 
 
31
 
#include <SDL_events.h>
32
 
 
33
 
#include "debug.h"
34
 
 
35
 
extern volatile int tick_time;
36
 
 
37
 
KeyboardConfig::KeyboardConfig() :
38
 
    mEnabled(true),
39
 
    mActiveKeys(nullptr),
40
 
    mActiveKeys2(nullptr),
41
 
    mRepeatTime(0),
42
 
    mKeyToAction(),
43
 
    mKeyToId(),
44
 
    mKeyTimeMap()
45
 
{
46
 
}
47
 
 
48
 
void KeyboardConfig::init()
49
 
{
50
 
    mEnabled = true;
51
 
    delete mActiveKeys2;
52
 
    mActiveKeys2 = new uint8_t[500];
53
 
    mRepeatTime = config.getIntValue("repeateInterval2") / 10;
54
 
}
55
 
 
56
 
void KeyboardConfig::deinit()
57
 
{
58
 
    delete [] mActiveKeys2;
59
 
    mActiveKeys2 = nullptr;
60
 
}
61
 
 
62
 
int KeyboardConfig::getKeyValueFromEvent(const SDL_Event &event) const
63
 
{
64
 
#ifdef USE_SDL2
65
 
    return event.key.keysym.scancode;
66
 
#else
67
 
    if (event.key.keysym.sym)
68
 
        return event.key.keysym.sym;
69
 
    else if (event.key.keysym.scancode > 1)
70
 
        return -event.key.keysym.scancode;
71
 
#endif
72
 
    return 0;
73
 
}
74
 
 
75
 
int KeyboardConfig::getKeyIndex(const SDL_Event &event, const int grp) const
76
 
{
77
 
    const int keyValue = getKeyValueFromEvent(event);
78
 
    return inputManager.getKeyIndex(keyValue, grp, INPUT_KEYBOARD);
79
 
}
80
 
 
81
 
void KeyboardConfig::refreshActiveKeys()
82
 
{
83
 
    mActiveKeys = SDL_GetKeyState(nullptr);
84
 
}
85
 
 
86
 
std::string KeyboardConfig::getKeyName(const int key)
87
 
{
88
 
    if (key == Input::KEY_NO_VALUE)
89
 
        return "";
90
 
    if (key >= 0)
91
 
    {
92
 
#ifdef USE_SDL2
93
 
        return SDL_GetKeyName(SDL_GetKeyFromScancode(
94
 
            static_cast<SDL_Scancode>(key)));
95
 
#else
96
 
        return SDL_GetKeyName(static_cast<SDLKey>(key));
97
 
#endif
98
 
    }
99
 
 
100
 
    // TRANSLATORS: long key name, should be short
101
 
    return strprintf(_("key_%d"), key);
102
 
}
103
 
 
104
 
std::string KeyboardConfig::getKeyShortString(const std::string &key)
105
 
{
106
 
    if (key == "backspace")
107
 
        return "bksp";
108
 
    else if (key == "numlock")
109
 
        return "numlock";
110
 
    else if (key == "caps lock")
111
 
        return "caplock";
112
 
    else if (key == "scroll lock")
113
 
        return "scrlock";
114
 
    else if (key == "right shift")
115
 
        return "rshift";
116
 
    else if (key == "left shift")
117
 
        return "lshift";
118
 
    else if (key == "right ctrl")
119
 
        return "rctrl";
120
 
    else if (key == "left ctrl")
121
 
        return "lctrl";
122
 
    else if (key == "right alt")
123
 
        return "ralt";
124
 
    else if (key == "left alt")
125
 
        return "lalt";
126
 
    else if (key == "right meta")
127
 
        return "rmeta";
128
 
    else if (key == "left meta")
129
 
        return "lmeta";
130
 
    else if (key == "right super")
131
 
        return "rsuper";
132
 
    else if (key == "left super")
133
 
        return "lsuper";
134
 
    else if (key == "print screen")
135
 
        return "print screen";
136
 
    else if (key == "page up")
137
 
        return "pg up";
138
 
    else if (key == "page down")
139
 
        return "pg down";
140
 
 
141
 
    if (key == "unknown key")
142
 
    {
143
 
        // TRANSLATORS: Unknown key short string.
144
 
        // TRANSLATORS: This string must be maximum 5 chars
145
 
        return _("u key");
146
 
    }
147
 
    return key;
148
 
}
149
 
 
150
 
SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event)
151
 
{
152
 
#ifdef USE_SDL2
153
 
    return event.key.keysym.scancode;
154
 
#else
155
 
    return event.key.keysym.sym;
156
 
#endif
157
 
}
158
 
 
159
 
KeysVector *KeyboardConfig::getActionVector(const SDL_Event &event)
160
 
{
161
 
    const int i = getKeyValueFromEvent(event);
162
 
//    logger->log("key triggerAction: %d", i);
163
 
    if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
164
 
        return &mKeyToAction[i];
165
 
    return nullptr;
166
 
}
167
 
 
168
 
KeysVector *KeyboardConfig::getActionVectorByKey(const int i)
169
 
{
170
 
//    logger->log("key triggerAction: %d", i);
171
 
    if (i != 0 && i < SDLK_LAST && mKeyToAction.find(i) != mKeyToAction.end())
172
 
        return &mKeyToAction[i];
173
 
    return nullptr;
174
 
}
175
 
 
176
 
int KeyboardConfig::getActionId(const SDL_Event &event)
177
 
{
178
 
    const int i = getKeyValueFromEvent(event);
179
 
//    logger->log("getActionId: %d", i);
180
 
    if (i != 0 && i < SDLK_LAST && mKeyToId.find(i) != mKeyToId.end())
181
 
        return mKeyToId[i];
182
 
    return -1;
183
 
}
184
 
 
185
 
bool KeyboardConfig::isActionActive(const int index) const
186
 
{
187
 
    if (!mActiveKeys)
188
 
        return false;
189
 
 
190
 
    const KeyFunction &key = inputManager.getKey(index);
191
 
    for (size_t i = 0; i < KeyFunctionSize; i ++)
192
 
    {
193
 
        const KeyItem &val = key.values[i];
194
 
        if (val.type != INPUT_KEYBOARD)
195
 
            continue;
196
 
 
197
 
        const int value = val.value;
198
 
        if (value >= 0)
199
 
        {
200
 
            if (mActiveKeys[value])
201
 
                return true;
202
 
        }
203
 
        else if (value < -1 && value > -500)
204
 
        {
205
 
            if (mActiveKeys2[-value])
206
 
                return true;
207
 
        }
208
 
    }
209
 
    return false;
210
 
}
211
 
 
212
 
void KeyboardConfig::update()
213
 
{
214
 
    inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
215
 
        mKeyTimeMap, INPUT_KEYBOARD);
216
 
}
217
 
 
218
 
void KeyboardConfig::handleActicateKey(const SDL_Event &event)
219
 
{
220
 
    const int key = getKeyValueFromEvent(event);
221
 
    if (key < -1 && key > -500)
222
 
        mActiveKeys2[-key] = 1;
223
 
    resetRepeat(key);
224
 
}
225
 
 
226
 
void KeyboardConfig::handleActicateKey(const int key)
227
 
{
228
 
    if (key < -1 && key > -500)
229
 
        mActiveKeys2[-key] = 1;
230
 
    resetRepeat(key);
231
 
}
232
 
 
233
 
void KeyboardConfig::handleDeActicateKey(const SDL_Event &event)
234
 
{
235
 
    const int key = getKeyValueFromEvent(event);
236
 
    if (key < -1 && key > -500)
237
 
        mActiveKeys2[-key] = 0;
238
 
    resetRepeat(key);
239
 
}
240
 
 
241
 
void KeyboardConfig::handleDeActicateKey(const int key)
242
 
{
243
 
    if (key < -1 && key > -500)
244
 
        mActiveKeys2[-key] = 0;
245
 
    resetRepeat(key);
246
 
}
247
 
 
248
 
void KeyboardConfig::handleRepeat(const int time)
249
 
{
250
 
    FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap)
251
 
    {
252
 
        bool repeat(false);
253
 
        const int key = (*it).first;
254
 
        int &keyTime = (*it).second;
255
 
        if (key >= 0)
256
 
        {
257
 
            if (mActiveKeys && mActiveKeys[key])
258
 
                repeat = true;
259
 
        }
260
 
        else if (key < -1 && key > -500)
261
 
        {
262
 
            if (mActiveKeys2 && mActiveKeys2[-key])
263
 
                repeat = true;
264
 
        }
265
 
        if (repeat)
266
 
        {
267
 
            if (time > keyTime && abs(time - keyTime)
268
 
                > static_cast<signed>(mRepeatTime))
269
 
            {
270
 
                keyTime = time;
271
 
                inputManager.triggerAction(getActionVectorByKey(key));
272
 
            }
273
 
        }
274
 
    }
275
 
}
276
 
 
277
 
void KeyboardConfig::resetRepeat(const int key)
278
 
{
279
 
    const KeyTimeMapIter it = mKeyTimeMap.find(key);
280
 
    if (it != mKeyTimeMap.end())
281
 
        (*it).second = tick_time;
282
 
}