forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyConfig.cpp
226 lines (200 loc) · 6.32 KB
/
KeyConfig.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (C) 2022 by Michael J. Walsh
* Copyright (C) 2013 by Mitch Draves
*
* This file 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 file 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 libdvdnav; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string>
#include <unordered_map>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "KeyConfig.h"
using namespace std;
namespace KeyConfig {
struct action_lookup
{
const char* k;
enum Action v;
};
const struct action_lookup table[] = {
// NB this list is CASE SENSITIVELY sorted
{"DECREASE_SPEED", ACTION_DECREASE_SPEED},
{"DECREASE_SUBTITLE_DELAY", ACTION_DECREASE_SUBTITLE_DELAY},
{"DECREASE_VOLUME", ACTION_DECREASE_VOLUME},
{"EXIT", ACTION_EXIT},
{"HIDE_SUBTITLES", ACTION_HIDE_SUBTITLES},
{"INCREASE_SPEED", ACTION_INCREASE_SPEED},
{"INCREASE_SUBTITLE_DELAY", ACTION_INCREASE_SUBTITLE_DELAY},
{"INCREASE_VOLUME", ACTION_INCREASE_VOLUME},
{"NEXT_AUDIO", ACTION_NEXT_AUDIO},
{"NEXT_CHAPTER", ACTION_NEXT_CHAPTER},
{"NEXT_FILE", ACTION_NEXT_FILE},
{"NEXT_SUBTITLE", ACTION_NEXT_SUBTITLE},
{"PAUSE", ACTION_PLAYPAUSE},
{"PREVIOUS_AUDIO", ACTION_PREVIOUS_AUDIO},
{"PREVIOUS_CHAPTER", ACTION_PREVIOUS_CHAPTER},
{"PREVIOUS_FILE", ACTION_PREVIOUS_FILE},
{"PREVIOUS_SUBTITLE", ACTION_PREVIOUS_SUBTITLE},
{"SEEK_BACK_LARGE", ACTION_SEEK_BACK_LARGE},
{"SEEK_BACK_SMALL", ACTION_SEEK_BACK_SMALL},
{"SEEK_FORWARD_LARGE", ACTION_SEEK_FORWARD_LARGE},
{"SEEK_FORWARD_SMALL", ACTION_SEEK_FORWARD_SMALL},
{"SHOW_SUBTITLES", ACTION_SHOW_SUBTITLES},
{"STEP", ACTION_STEP},
{"TOGGLE_SUBTITLE", ACTION_TOGGLE_SUBTITLE},
};
const int item_size = sizeof(const action_lookup);
const int item_count = sizeof(table) / sizeof(const action_lookup);
static int compare(const void *a, const void *b)
{
const char *ia = (const char *)a;
const struct action_lookup *ib = (const struct action_lookup *)b;
return strcmp(ia, ib->k);
}
/* Converts the action string from the config file into
* the corresponding enum value
*/
static int convertStringToAction(const string &str_action)
{
struct action_lookup *result = (struct action_lookup *)
bsearch(str_action.c_str(), table, item_count, item_size, compare);
return result == NULL ? -1 : result->v;
}
/* Parses a line from the config file in the mode 'action:key'. Looks up
the action in the relevant enum array. Returns true on success. */
static bool getActionAndKeyFromString(const string &line, int &int_action, string &key)
{
string str_action;
if(line[0] == '#')
return false;
unsigned int colonIndex = line.find(":");
if(colonIndex == string::npos)
return false;
str_action = line.substr(0, colonIndex);
key = line.substr(colonIndex+1);
int_action = convertStringToAction(str_action);
if(int_action == -1 || key.size() < 1)
return false;
return true;
}
/* Returns a keymap consisting of the default
* keybinds specified with the -k option
*/
static void buildDefaultKeymap(unordered_map<int,int> &keymap)
{
keymap['<'] = ACTION_DECREASE_SPEED;
keymap['>'] = ACTION_INCREASE_SPEED;
keymap[','] = ACTION_DECREASE_SPEED;
keymap['.'] = ACTION_INCREASE_SPEED;
keymap['j'] = ACTION_PREVIOUS_AUDIO;
keymap['k'] = ACTION_NEXT_AUDIO;
keymap['i'] = ACTION_PREVIOUS_CHAPTER;
keymap['o'] = ACTION_NEXT_CHAPTER;
keymap['9'] = ACTION_PREVIOUS_FILE;
keymap['0'] = ACTION_NEXT_FILE;
keymap['n'] = ACTION_PREVIOUS_SUBTITLE;
keymap['m'] = ACTION_NEXT_SUBTITLE;
keymap['s'] = ACTION_TOGGLE_SUBTITLE;
keymap['d'] = ACTION_DECREASE_SUBTITLE_DELAY;
keymap['f'] = ACTION_INCREASE_SUBTITLE_DELAY;
keymap['q'] = ACTION_EXIT;
keymap[KEY_ESC] = ACTION_EXIT;
keymap['p'] = ACTION_PLAYPAUSE;
keymap[' '] = ACTION_PLAYPAUSE;
keymap['-'] = ACTION_DECREASE_VOLUME;
keymap['+'] = ACTION_INCREASE_VOLUME;
keymap['='] = ACTION_INCREASE_VOLUME;
keymap[KEY_LEFT] = ACTION_SEEK_BACK_SMALL;
keymap[KEY_RIGHT] = ACTION_SEEK_FORWARD_SMALL;
keymap[KEY_DOWN] = ACTION_SEEK_BACK_LARGE;
keymap[KEY_UP] = ACTION_SEEK_FORWARD_LARGE;
keymap['v'] = ACTION_STEP;
keymap['w'] = ACTION_SHOW_SUBTITLES;
keymap['x'] = ACTION_HIDE_SUBTITLES;
}
/* Parses the supplied config file and turns it into a map object.
*/
static void parseConfigFile(const char *filepath, unordered_map<int, int> &keymap)
{
ifstream config_file(filepath);
if(!config_file.is_open())
{
cerr << "Failed to open key config file: " << filepath << endl;
buildDefaultKeymap(keymap);
return;
}
string line;
int key_action;
string key;
while(getline(config_file, line))
{
if(getActionAndKeyFromString(line, key_action, key))
{
if(key.substr(0,4) == "left")
{
keymap[KEY_LEFT] = key_action;
}
else if(key.substr(0,5) == "right")
{
keymap[KEY_RIGHT] = key_action;
}
else if(key.substr(0,2) == "up")
{
keymap[KEY_UP] = key_action;
}
else if(key.substr(0,4) == "down")
{
keymap[KEY_DOWN] = key_action;
}
else if(key.substr(0,3) == "esc")
{
keymap[KEY_ESC] = key_action;
}
else if(key.substr(0,5) == "space")
{
keymap[' '] = key_action;
}
else if(key.substr(0,3) == "num" || key.substr(0,3) == "hex")
{
if(key.size() > 4)
{
int n = strtoul(key.substr(4).c_str(), 0, 0);
if (n > 0)
keymap[n] = key_action;
}
}
else
{
// this deals with unicode chars
int len = std::min((int)key.length(), 4);
int k = key[0];
for(int i = 1; i < len; i++)
k = (k << 8) | key[i];
keymap[k] = key_action;
}
}
}
}
void buildKeymap(const char *filename, unordered_map<int, int> &map)
{
if(filename == NULL) {
buildDefaultKeymap(map);
} else {
parseConfigFile(filename, map);
}
}
}