QTfrontend/util/KeyMap.cpp
author nemo
Thu, 12 Mar 2020 15:09:16 -0400
changeset 15550 0b16baefefd1
parent 14925 d2f5b6b01846
permissions -rw-r--r--
er... nevermind. back this out. ApplyDamage should pick this up just fine.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14848
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     1
/*
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     3
 * Copyright (c) 2004-2019 Andrey Korotaev <unC0Rr@gmail.com>
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     4
 *
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     8
 *
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    12
 * GNU General Public License for more details.
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    13
 *
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    17
 */
14925
d2f5b6b01846 just to get it to build
nemo
parents: 14922
diff changeset
    18
#include <QStringList>
14848
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    19
#include "KeyMap.h"
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    20
#include "SDL.h"
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    21
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    22
KeyMap & KeyMap::instance()
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    23
{
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    24
    static KeyMap instance;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    25
    instance.getKeyMap();
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    26
    return instance;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    27
}
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    28
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    29
bool KeyMap::getKeyMap()
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    30
{
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    31
    if (keyMapGenerated)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    32
        return true;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    33
    QFile keyFile(":keys.csv");
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    34
    if (!keyFile.open(QIODevice::ReadOnly))
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    35
    {
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    36
        qWarning("ERROR: keys.csv could not be opened!");
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    37
        return false;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    38
    }
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    39
    QString keyString = keyFile.readAll();
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    40
    QStringList cells = QStringList() << QString("") << QString("");
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    41
    QChar currChar;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    42
    bool isInQuote = false;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    43
    int cell = 0;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    44
    int charInCell = 0;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    45
    QString scancode = "";
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    46
    QString keyname = "";
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    47
    for(long long int i = 0; i < keyString.length(); i++)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    48
    {
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    49
        currChar = keyString.at(i);
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    50
        if (currChar == '\"') {
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    51
            isInQuote = !isInQuote;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    52
        }
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    53
        if (currChar == ',' && !isInQuote) {
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    54
            cell++;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    55
            continue;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    56
        }
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    57
        if (currChar == '\n') {
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    58
            mapOfKeynames[(SDL_Scancode) scancode.toInt()] = keyname;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    59
            mapOfScancodes[keyname] = (SDL_Scancode) scancode.toInt();
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    60
            if ((SDL_Scancode) scancode.toInt() == SDL_SCANCODE_UNKNOWN)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    61
                continue;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    62
            cell = 0;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    63
            scancode = "";
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    64
            keyname = "";
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    65
            continue;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    66
        }
14922
b3890f4311f7 Fix clang C compilation
Anton Malmygin <antonc27@mail.ru>
parents: 14848
diff changeset
    67
        if (cell == 0 && currChar != '\"') {
14848
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    68
            scancode += currChar;
14922
b3890f4311f7 Fix clang C compilation
Anton Malmygin <antonc27@mail.ru>
parents: 14848
diff changeset
    69
        } else if (cell == 1 && currChar != '\"') {
14848
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    70
            keyname += currChar;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    71
        }
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    72
        charInCell++;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    73
    }
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    74
    keyMapGenerated = true;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    75
    keyFile.close();
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    76
    return true;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    77
}
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    78
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    79
SDL_Scancode KeyMap::getScancodeFromKeyname(QString keyname)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    80
{
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    81
    if (mapOfScancodes.contains(keyname))
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    82
        return mapOfScancodes[keyname];
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    83
    else
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    84
        return SDL_SCANCODE_UNKNOWN;
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    85
}
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    86
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    87
QString KeyMap::getKeynameFromScancode(int scancode)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    88
{
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    89
    if (mapOfKeynames.contains((SDL_Scancode) scancode))
14922
b3890f4311f7 Fix clang C compilation
Anton Malmygin <antonc27@mail.ru>
parents: 14848
diff changeset
    90
        if ((SDL_Scancode) scancode == SDL_SCANCODE_UNKNOWN)
14848
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    91
            return QString("none");
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    92
        else
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    93
            return mapOfKeynames[(SDL_Scancode) scancode];
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    94
    else
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    95
        return QString("");
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    96
}
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    97
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    98
QString KeyMap::getKeynameFromScancodeConverted(int scancode)
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
    99
{
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
   100
    SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode);
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
   101
    return SDL_GetKeyName(keycode);
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
   102
}
bb412d8e435f Frontend: Fix wrong keyboard key names being displayed with regard to keyboard layout
Wuzzy <Wuzzy2@mail.ru>
parents:
diff changeset
   103