This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
keymap.cpp
171 lines (155 loc) · 5.51 KB
/
keymap.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <keymap.h>
#include <logger.h>
#include <vector>
#include <iostream>
#include <config.h>
namespace newsbeuter {
struct op_desc {
operation op;
char * opstr;
char * default_key;
char * help_text;
unsigned short flags;
};
static op_desc opdescs[] = {
{ OP_OPEN, "open", "enter", _("Open feed/article"), KM_NEWSBEUTER },
{ OP_QUIT, "quit", "q", _("Return to previous dialog/Quit"), KM_BOTH },
{ OP_RELOAD, "reload", "r", _("Reload currently selected feed"), KM_NEWSBEUTER },
{ OP_RELOADALL, "reload-all", "R", _("Reload all feeds"), KM_NEWSBEUTER },
{ OP_MARKFEEDREAD, "mark-feed-read", "A", _("Mark feed read"), KM_NEWSBEUTER },
{ OP_MARKALLFEEDSREAD, "mark-all-feeds-read", "C", _("Mark all feeds read"), KM_NEWSBEUTER },
{ OP_SAVE, "save", "s", _("Save article"), KM_NEWSBEUTER },
{ OP_NEXTUNREAD, "next-unread", "n", _("Go to next unread article"), KM_NEWSBEUTER },
{ OP_OPENINBROWSER, "open-in-browser", "o", _("Open article in browser"), KM_NEWSBEUTER },
{ OP_HELP, "help", "?", _("Open help dialog"), KM_BOTH },
{ OP_TOGGLESOURCEVIEW, "toggle-source-view", "^u", _("Toggle source view"), KM_NEWSBEUTER },
{ OP_TOGGLEITEMREAD, "toggle-article-read", "N", _("Toggle read status for article"), KM_NEWSBEUTER },
{ OP_TOGGLESHOWREAD, "toggle-show-read-feeds", "l", _("Toggle show read feeds"), KM_NEWSBEUTER },
{ OP_SHOWURLS, "show-urls", "u", _("Show URLs in current article"), KM_NEWSBEUTER },
{ OP_CLEARTAG, "clear-tag", "^t", _("Clear current tag"), KM_NEWSBEUTER },
{ OP_SETTAG, "set-tag", "t", _("Select tag"), KM_NEWSBEUTER },
{ OP_SEARCH, "open-search", "/", _("Open search dialog"), KM_NEWSBEUTER },
{ OP_ENQUEUE, "enqueue", "e", _("Add download to queue"), KM_NEWSBEUTER },
{ OP_PB_DOWNLOAD, "pb-download", "d", _("Download file"), KM_PODBEUTER },
{ OP_PB_CANCEL, "pb-cancel", "c", _("Cancel download"), KM_PODBEUTER },
{ OP_PB_DELETE, "pb-delete", "D", _("Mark download as deleted"), KM_PODBEUTER },
{ OP_PB_PURGE, "pb-purge", "P", _("Purge finished and deleted downloads from queue"), KM_PODBEUTER },
{ OP_PB_TOGGLE_DLALL, "pb-toggle-download-all", "a", _("Toggle automatic download on/off"), KM_PODBEUTER },
{ OP_PB_PLAY, "pb-play", "p", _("Start player with currently selected download"), KM_PODBEUTER },
{ OP_PB_MOREDL, "pb-increase-max-dls", "+", _("Increase the number of concurrent downloads"), KM_PODBEUTER },
{ OP_PB_LESSDL, "pb-decreate-max-dls", "-", _("Decrease the number of concurrent downloads"), KM_PODBEUTER },
{ OP_REDRAW, "redraw", "^l", _("Redraw screen"), KM_BOTH },
{ OP_NIL, NULL, NULL, NULL, 0 }
};
keymap::keymap() {
for (int i=0;opdescs[i].help_text;++i) {
keymap_[opdescs[i].default_key] = opdescs[i].op;
}
}
void keymap::get_keymap_descriptions(std::vector<std::pair<std::string,std::string> >& descs, unsigned short flags) {
for (std::map<std::string,operation>::iterator it=keymap_.begin();it!=keymap_.end();++it) {
operation op = it->second;
if (op != OP_NIL) {
std::string helptext;
bool add = false;
for (int i=0;opdescs[i].help_text;++i) {
if (opdescs[i].op == op && opdescs[i].flags & flags) {
helptext = gettext(opdescs[i].help_text);
add = true;
}
}
if (add) {
descs.push_back(std::pair<std::string,std::string>(it->first, helptext));
}
}
}
}
keymap::~keymap() { }
void keymap::set_key(operation op, const std::string& key) {
GetLogger().log(LOG_DEBUG,"keymap::set_key(%d,%s) called", op, key.c_str());
keymap_[key] = op;
}
void keymap::unset_key(const std::string& key) {
GetLogger().log(LOG_DEBUG,"keymap::unset_key(%s) called", key.c_str());
keymap_[key] = OP_NIL;
}
operation keymap::get_opcode(const std::string& opstr) {
for (int i=0;opdescs[i].opstr;++i) {
if (opstr == opdescs[i].opstr) {
return opdescs[i].op;
}
}
return OP_NIL;
}
char keymap::get_key(const std::string& keycode) {
if (strncmp(keycode.c_str(),"CHAR(",5)==0) {
unsigned int x;
char c;
sscanf(keycode.c_str(),"CHAR(%u)",&x);
if (x >= 32 && x <= 126) {
c = static_cast<char>(x);
return c;
}
}
return 0;
}
operation keymap::get_operation(const std::string& keycode) {
std::string key;
if (keycode.length() > 0) {
if (keycode == "ENTER") {
key = "enter";
} else if (keycode == "ESC") {
key = "esc";
} else if (keycode[0] == 'F') {
key = keycode;
key[0] = 'f';
} else if (strncmp(keycode.c_str(),"CHAR(",5)==0) {
unsigned int x;
char c;
sscanf(keycode.c_str(),"CHAR(%u)",&x);
// std::cerr << x << std::endl;
if (32 == x) {
key.append("space");
} else if (x > 32 && x <= 126) {
c = static_cast<char>(x);
key.append(1,c);
} else if (x<=26) {
key.append("^");
key.append(1,static_cast<char>(0x60 + x));
} else {
// TODO: handle special keys
}
}
} else {
key = "NIL";
}
return keymap_[key];
}
action_handler_status keymap::handle_action(const std::string& action, const std::vector<std::string>& params) {
GetLogger().log(LOG_DEBUG,"keymap::handle_action(%s, ...) called",action.c_str());
if (action == "bind-key") {
if (params.size() < 2) {
return AHS_TOO_FEW_PARAMS;
} else {
set_key(get_opcode(params[1]), params[0]);
// keymap_[params[0]] = get_opcode(params[1]);
return AHS_OK;
}
} else if (action == "unbind-key") {
if (params.size() < 1) {
return AHS_TOO_FEW_PARAMS;
} else {
unset_key(params[0]);
return AHS_OK;
}
} else
return AHS_INVALID_PARAMS;
}
std::string keymap::getkey(operation op) {
for (std::map<std::string,operation>::iterator it=keymap_.begin(); it!=keymap_.end(); ++it) {
if (it->second == op)
return it->first;
}
return "<none>";
}
}