summaryrefslogtreecommitdiff
path: root/client/hot_keys.cpp
blob: 65b1bc34fd342d2bf566a93499375028da9ef231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "common.h"
#include "hot_keys.h"
#include "utils.h"
#include "debug.h"

HotKeysParser::HotKeysParser(const std::string& hotkeys, const CommandsMap& commands_map)
{
    DBG(0, "hotkeys = %s", hotkeys.c_str());

    std::istringstream is(hotkeys);
    std::string hotkey;
    while (std::getline(is, hotkey, ',')) {
        add_hotkey(hotkey, commands_map);
    }
}

void HotKeysParser::parse_keys(int command_id, const std::string& hotkey)
{
    HotkeySet& keys = _hot_keys[command_id];
    std::istringstream is(hotkey);
    std::string key;
    while (std::getline(is, key, '+')) {
        add_key(keys, key.c_str());
    }
}

void HotKeysParser::add_key(HotkeySet& keys, const char* key)
{
    ASSERT(key != NULL);

    static const struct {
        const char* name;
        RedKey main;
        RedKey alter;
    } keyboard[] = {
        { "alt", REDKEY_R_ALT, REDKEY_L_ALT },
        { "ralt", REDKEY_R_ALT, REDKEY_INVALID },
        { "rightalt", REDKEY_R_ALT, REDKEY_INVALID },
        { "right-alt", REDKEY_R_ALT, REDKEY_INVALID },
        { "lalt", REDKEY_L_ALT, REDKEY_INVALID },
        { "leftalt", REDKEY_L_ALT, REDKEY_INVALID },
        { "left-alt", REDKEY_L_ALT, REDKEY_INVALID },
        { "ctrl", REDKEY_R_CTRL, REDKEY_L_CTRL },
        { "rctrl", REDKEY_R_CTRL, REDKEY_INVALID },
        { "rightctrl", REDKEY_R_CTRL, REDKEY_INVALID },
        { "right-ctrl", REDKEY_R_CTRL, REDKEY_INVALID },
        { "lctrl", REDKEY_L_CTRL, REDKEY_INVALID },
        { "leftctrl", REDKEY_L_CTRL, REDKEY_INVALID },
        { "left-ctrl", REDKEY_L_CTRL, REDKEY_INVALID },
        { "shift", REDKEY_R_SHIFT, REDKEY_L_SHIFT },
        { "rshift", REDKEY_R_SHIFT, REDKEY_INVALID },
        { "rightshift", REDKEY_R_SHIFT, REDKEY_INVALID },
        { "right-shift", REDKEY_R_SHIFT, REDKEY_INVALID },
        { "lshift", REDKEY_L_SHIFT, REDKEY_INVALID },
        { "leftshift", REDKEY_L_SHIFT, REDKEY_INVALID },
        { "left-shift", REDKEY_L_SHIFT, REDKEY_INVALID },
        { "cmd", REDKEY_RIGHT_CMD, REDKEY_LEFT_CMD },
        { "rcmd", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "rightcmd", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "right-cmd", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "lcmd", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "leftcmd", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "left-cmd", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "win", REDKEY_RIGHT_CMD, REDKEY_LEFT_CMD },
        { "rwin", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "rightwin", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "right-win", REDKEY_RIGHT_CMD, REDKEY_INVALID },
        { "lwin", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "leftwin", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "left-win", REDKEY_LEFT_CMD, REDKEY_INVALID },
        { "esc", REDKEY_ESCAPE, REDKEY_INVALID },
        { "escape", REDKEY_ESCAPE, REDKEY_INVALID },
        { "ins", REDKEY_INSERT, REDKEY_INVALID },
        { "insert", REDKEY_INSERT, REDKEY_INVALID },
        { "del", REDKEY_DELETE, REDKEY_INVALID },
        { "delete", REDKEY_DELETE, REDKEY_INVALID },
        { "pgup", REDKEY_PAGEUP, REDKEY_INVALID },
        { "pageup", REDKEY_PAGEUP, REDKEY_INVALID },
        { "pgdn", REDKEY_PAGEDOWN, REDKEY_INVALID },
        { "pagedown", REDKEY_PAGEDOWN, REDKEY_INVALID },
        { "home", REDKEY_HOME, REDKEY_INVALID },
        { "end", REDKEY_END, REDKEY_INVALID },
        { "space", REDKEY_SPACE, REDKEY_INVALID },
        { "enter", REDKEY_ENTER, REDKEY_INVALID },
        { "tab", REDKEY_TAB, REDKEY_INVALID },
        { "f1", REDKEY_F1, REDKEY_INVALID },
        { "f2", REDKEY_F2, REDKEY_INVALID },
        { "f3", REDKEY_F3, REDKEY_INVALID },
        { "f4", REDKEY_F4, REDKEY_INVALID },
        { "f5", REDKEY_F5, REDKEY_INVALID },
        { "f6", REDKEY_F6, REDKEY_INVALID },
        { "f7", REDKEY_F7, REDKEY_INVALID },
        { "f8", REDKEY_F8, REDKEY_INVALID },
        { "f9", REDKEY_F9, REDKEY_INVALID },
        { "f10", REDKEY_F10, REDKEY_INVALID },
        { "f11", REDKEY_F11, REDKEY_INVALID },
        { "f12", REDKEY_F12, REDKEY_INVALID }
    };

    for (unsigned i = 0; i < (sizeof(keyboard) / sizeof(keyboard[0])); ++i) {
        if (strcasecmp(key, keyboard[i].name) == 0) {
            HotkeyKey hotkey;
            hotkey.main = keyboard[i].main;
            hotkey.alter = keyboard[i].alter;
            DBG(0, "keys = %s", keyboard[i].name);
            keys.push_back(hotkey);
            return;
        }
    }
    THROW("unknown key name %s", key);
}

void HotKeysParser::add_hotkey(const std::string& hotkey, const CommandsMap& commands_map)
{
    std::string::size_type key_start = hotkey.find('=', 0);
    if (key_start == std::string::npos) {
        THROW("unable to parse hot keys");
    }
    std::string command_name = hotkey.substr(0, key_start);

    if (commands_map.find(command_name) == commands_map.end()) {
        char buf[1000];
        sprintf(buf, "invalid action bname %s", command_name.c_str());
        THROW(buf);
    }
    int command_id = commands_map.find(command_name)->second;
    std::string keys = hotkey.substr(key_start + 1);
    parse_keys(command_id, keys);
}