Fix zero luma corner case
[lsnes.git] / src / core / keymapper.cpp
blob912ab4262dc399a87e1451a0c190515d8901861c
1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "core/globalwrap.hpp"
4 #include "core/keymapper.hpp"
5 #include "core/lua.hpp"
6 #include "core/memorymanip.hpp"
7 #include "core/misc.hpp"
8 #include "core/window.hpp"
10 #include <stdexcept>
11 #include <stdexcept>
12 #include <iostream>
13 #include <list>
14 #include <map>
15 #include <sstream>
16 #include <set>
18 namespace
20 function_ptr_command<tokensplitter&> bind_key("bind-key", "Bind a (pseudo-)key",
21 "Syntax: bind-key [<mod>/<modmask>] <key> <command>\nBind command to specified key (with specified "
22 " modifiers)\n",
23 [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
24 std::string mod, modmask, keyname, command;
25 std::string mod_or_key = t;
26 if(mod_or_key.find_first_of("/") < mod_or_key.length()) {
27 //Mod field.
28 size_t split = mod_or_key.find_first_of("/");
29 mod = mod_or_key.substr(0, split);
30 modmask = mod_or_key.substr(split + 1);
31 mod_or_key = static_cast<std::string>(t);
33 if(mod_or_key == "")
34 throw std::runtime_error("Expected optional modifiers and key");
35 keyname = mod_or_key;
36 command = t.tail();
37 if(command == "")
38 throw std::runtime_error("Expected command");
39 keymapper::bind(mod, modmask, keyname, command);
40 if(mod != "" || modmask != "")
41 messages << mod << "/" << modmask << " ";
42 messages << keyname << " bound to '" << command << "'" << std::endl;
44 });
46 function_ptr_command<tokensplitter&> unbind_key("unbind-key", "Unbind a (pseudo-)key",
47 "Syntax: unbind-key [<mod>/<modmask>] <key>\nUnbind specified key (with specified modifiers)\n",
48 [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
49 std::string mod, modmask, keyname, command;
50 std::string mod_or_key = t;
51 if(mod_or_key.find_first_of("/") < mod_or_key.length()) {
52 //Mod field.
53 size_t split = mod_or_key.find_first_of("/");
54 mod = mod_or_key.substr(0, split);
55 modmask = mod_or_key.substr(split + 1);
56 mod_or_key = static_cast<std::string>(t);
58 if(mod_or_key == "")
59 throw std::runtime_error("Expected optional modifiers and key");
60 keyname = mod_or_key;
61 command = t.tail();
62 if(command != "")
63 throw std::runtime_error("Unexpected argument");
64 keymapper::unbind(mod, modmask, keyname);
65 if(mod != "" || modmask != "")
66 messages << mod << "/" << modmask << " ";
67 messages << keyname << " unbound" << std::endl;
68 });
70 function_ptr_command<> show_bindings("show-bindings", "Show active bindings",
71 "Syntax: show-bindings\nShow bindings that are currently active.\n",
72 []() throw(std::bad_alloc, std::runtime_error) {
73 keymapper::dumpbindings();
74 });
77 std::string fixup_command_polarity(std::string cmd, bool polarity) throw(std::bad_alloc)
79 if(cmd == "" || cmd == "*")
80 return "";
81 if(cmd[0] != '*') {
82 if(cmd[0] != '+' && polarity)
83 return "";
84 if(cmd[0] == '+' && !polarity)
85 cmd[0] = '-';
86 } else {
87 if(cmd[1] != '+' && polarity)
88 return "";
89 if(cmd[1] == '+' && !polarity)
90 cmd[1] = '-';
92 return cmd;
95 namespace
97 globalwrap<std::map<std::string, modifier*>> known_modifiers;
98 globalwrap<std::map<std::string, std::string>> modifier_linkages;
99 globalwrap<std::map<std::string, keygroup*>> keygroups;
101 //Returns orig if not linked.
102 const modifier* get_linked_modifier(const modifier* orig)
104 if(!modifier_linkages().count(orig->name()))
105 return orig;
106 std::string l = modifier_linkages()[orig->name()];
107 return known_modifiers()[l];
111 modifier::modifier(const std::string& name) throw(std::bad_alloc)
113 known_modifiers()[modname = name] = this;
116 modifier::modifier(const std::string& name, const std::string& linkgroup) throw(std::bad_alloc)
118 known_modifiers()[modname = name] = this;
119 modifier_linkages()[name] = linkgroup;
122 modifier::~modifier() throw()
124 known_modifiers().erase(modname);
125 modifier_linkages().erase(modname);
128 std::set<std::string> modifier::get_set() throw(std::bad_alloc)
130 std::set<std::string> r;
131 for(auto i : known_modifiers())
132 r.insert(i.first);
133 return r;
136 modifier& modifier::lookup(const std::string& name) throw(std::bad_alloc, std::runtime_error)
138 if(!known_modifiers().count(name)) {
139 std::ostringstream x;
140 x << "Invalid modifier '" << name << "'";
141 throw std::runtime_error(x.str());
143 return *known_modifiers()[name];
146 std::string modifier::name() const throw(std::bad_alloc)
148 return modname;
151 std::string modifier::linked_name() const throw(std::bad_alloc)
153 const modifier* p = get_linked_modifier(this);
154 if(p == this)
155 return "";
156 return p->modname;
160 void modifier_set::add(const modifier& mod, bool really) throw(std::bad_alloc)
162 if(really)
163 set.insert(&mod);
166 void modifier_set::remove(const modifier& mod, bool really) throw(std::bad_alloc)
168 if(really)
169 set.erase(&mod);
172 modifier_set modifier_set::construct(const std::string& _modifiers) throw(std::bad_alloc, std::runtime_error)
174 modifier_set set;
175 std::string modifiers = _modifiers;
176 while(modifiers != "") {
177 std::string mod = modifiers;
178 std::string rest;
179 size_t split = modifiers.find_first_of(",");
180 if(split < modifiers.length()) {
181 mod = modifiers.substr(0, split);
182 rest = modifiers.substr(split + 1);
184 set.add(modifier::lookup(mod));
185 modifiers = rest;
187 return set;
190 bool modifier_set::valid(const modifier_set& set, const modifier_set& mask) throw(std::bad_alloc)
192 //No element can be together with its linkage group.
193 for(auto i : set.set) {
194 const modifier* j = get_linked_modifier(i);
195 if(i != j && set.set.count(j))
196 return false;
198 for(auto i : mask.set) {
199 const modifier* j = get_linked_modifier(i);
200 if(i != j && mask.set.count(j))
201 return false;
203 //For every element of set, it or its linkage group must be in mask.
204 for(auto i : set.set) {
205 const modifier* j = get_linked_modifier(i);
206 if(!mask.set.count(i) && !mask.set.count(j))
207 return false;
209 return true;
212 bool modifier_set::operator==(const modifier_set& m) const throw()
214 for(auto i : set)
215 if(!m.set.count(i))
216 return false;
217 for(auto i : m.set)
218 if(!set.count(i))
219 return false;
220 return true;
223 std::ostream& operator<<(std::ostream& os, const modifier_set& m)
225 os << "<modset:";
226 for(auto i : m.set)
227 os << i->name() << " ";
228 os << ">";
229 return os;
232 bool modifier_set::triggers(const modifier_set& set, const modifier_set& trigger, const modifier_set& mask)
233 throw(std::bad_alloc)
235 for(auto i : mask.set) {
236 bool ok = false;
237 //OK iff at least one of:
238 //Key itself appears in both set and trigger.
239 for(auto j : set.set) {
240 if(!trigger.set.count(j))
241 continue;
242 ok = true;
244 //Key with this linkage group appears in both set and trigger.
245 for(auto j : set.set) {
246 auto linked = get_linked_modifier(j);
247 if(linked != i)
248 continue;
249 if(!trigger.set.count(j))
250 continue;
251 ok = true;
253 //Key with this linkage appears in set and the key itself appears in trigger.
254 for(auto j : set.set) {
255 auto linked = get_linked_modifier(j);
256 if(linked != i)
257 continue;
258 if(!trigger.set.count(i))
259 continue;
260 ok = true;
262 //Nothing linked is found from neither set nor trigger.
263 bool found = false;
264 for(auto j : set.set)
265 found = found || (j == i || get_linked_modifier(j) == i);
266 for(auto j : trigger.set)
267 found = found || (j == i || get_linked_modifier(j) == i);
268 ok = ok || !found;
269 if(!ok)
270 return false;
272 return true;
275 std::string keygroup::name() throw(std::bad_alloc)
277 return keyname;
280 struct keygroup::parameters keygroup::get_parameters()
282 parameters p;
283 p.ktype = ktype;
284 p.cal_left = cal_left;
285 p.cal_center = cal_center;
286 p.cal_right = cal_right;
287 p.cal_tolerance = cal_tolerance;
288 return p;
292 keygroup::keygroup(const std::string& name, enum type t) throw(std::bad_alloc)
294 keygroups()[keyname = name] = this;
295 ktype = t;
296 state = 0;
297 cal_left = -32768;
298 cal_center = 0;
299 cal_right = 32767;
300 cal_tolerance = 0.5;
303 keygroup::~keygroup() throw()
305 keygroups().erase(keyname);
308 void keygroup::change_type(enum type t) throw()
310 ktype = t;
311 state = 0;
314 std::pair<keygroup*, unsigned> keygroup::lookup(const std::string& name) throw(std::bad_alloc,
315 std::runtime_error)
317 if(keygroups().count(name))
318 return std::make_pair(keygroups()[name], 0);
319 std::string prefix = name;
320 char letter = prefix[prefix.length() - 1];
321 prefix = prefix.substr(0, prefix.length() - 1);
322 if(!keygroups().count(prefix))
323 throw std::runtime_error("Invalid key");
324 keygroup* g = keygroups()[prefix];
325 switch(letter) {
326 case '+':
327 case 'n':
328 return std::make_pair(g, 0);
329 case '-':
330 case 'e':
331 return std::make_pair(g, 1);
332 case 's':
333 return std::make_pair(g, 2);
334 case 'w':
335 return std::make_pair(g, 3);
336 default:
337 throw std::runtime_error("Invalid key");
341 void keygroup::change_calibration(short left, short center, short right, double tolerance)
343 cal_left = left;
344 cal_center = center;
345 cal_right = right;
346 cal_tolerance = tolerance;
349 double keygroup::compensate(short value)
351 if(ktype == KT_HAT || ktype == KT_KEY || ktype == KT_DISABLED)
352 return value; //These can't be calibrated.
353 if(value <= cal_left)
354 return -1.0;
355 else if(value >= cal_right)
356 return 1.0;
357 else if(value == cal_center)
358 return 0.0;
359 else if(value < cal_center)
360 return (static_cast<double>(value) - cal_center) / (static_cast<double>(cal_center) - cal_left);
361 else
362 return (static_cast<double>(value) - cal_center) / (static_cast<double>(cal_right) - cal_center);
365 double keygroup::compensate2(double value)
367 switch(ktype) {
368 case KT_DISABLED:
369 return 0; //Always neutral.
370 case KT_KEY:
371 case KT_HAT:
372 return value; //No mapping.
373 case KT_PRESSURE_0M:
374 return -value;
375 case KT_PRESSURE_0P:
376 return value;
377 case KT_PRESSURE_M0:
378 return 1 + value;
379 case KT_PRESSURE_MP:
380 return (1 + value) / 2;
381 case KT_PRESSURE_P0:
382 return 1 - value;
383 case KT_PRESSURE_PM:
384 return (1 - value) / 2;
385 case KT_AXIS_PAIR:
386 return value;
387 case KT_AXIS_PAIR_INVERSE:
388 return -value;
392 void keygroup::set_position(short pos, const modifier_set& modifiers) throw()
394 double x = compensate2(compensate(pos));
395 unsigned tmp;
396 bool left, right, up, down;
397 bool oleft, oright, oup, odown;
398 switch(ktype) {
399 case KT_DISABLED:
400 return;
401 case KT_KEY:
402 case KT_PRESSURE_0M:
403 case KT_PRESSURE_0P:
404 case KT_PRESSURE_M0:
405 case KT_PRESSURE_MP:
406 case KT_PRESSURE_P0:
407 case KT_PRESSURE_PM:
408 tmp = (x >= cal_tolerance);
409 run_listeners(modifiers, 0, true, (!state && tmp), x);
410 run_listeners(modifiers, 0, false, (state && !tmp), x);
411 state = tmp;
412 break;
413 case KT_AXIS_PAIR:
414 case KT_AXIS_PAIR_INVERSE:
415 if(x <= -cal_tolerance)
416 tmp = 2;
417 else if(x >= cal_tolerance)
418 tmp = 1;
419 else
420 tmp = 0;
421 run_listeners(modifiers, 0, false, state == 1 && tmp != 1, x);
422 run_listeners(modifiers, 1, false, state == 2 && tmp != 2, x);
423 run_listeners(modifiers, 0, true, tmp == 1 && state != 1, x);
424 run_listeners(modifiers, 1, true, tmp == 2 && state != 2, x);
425 state = tmp;
426 break;
427 case KT_HAT:
428 left = ((pos & 8) != 0);
429 right = ((pos & 2) != 0);
430 up = ((pos & 1) != 0);
431 down = ((pos & 4) != 0);
432 oleft = ((state & 8) != 0);
433 oright = ((state & 2) != 0);
434 oup = ((state & 1) != 0);
435 odown = ((state & 4) != 0);
436 run_listeners(modifiers, 3, false, oleft && !left, x);
437 run_listeners(modifiers, 1, false, oright && !right, x);
438 run_listeners(modifiers, 0, false, oup && !up, x);
439 run_listeners(modifiers, 2, false, odown && !down, x);
440 run_listeners(modifiers, 2, true, !odown && down, x);
441 run_listeners(modifiers, 0, true, !oup && up, x);
442 run_listeners(modifiers, 1, true, !oright && right, x);
443 run_listeners(modifiers, 3, true, !oleft && left, x);
444 state = pos;
445 break;
449 void keygroup::run_listeners(const modifier_set& modifiers, unsigned subkey, bool polarity, bool really, double x)
451 if(!really)
452 return;
453 std::string name = keyname;
454 if(ktype == KT_AXIS_PAIR && subkey == 0)
455 name = name + "+";
456 if(ktype == KT_AXIS_PAIR && subkey == 1)
457 name = name + "-";
458 if(ktype == KT_HAT && subkey == 0)
459 name = name + "n";
460 if(ktype == KT_HAT && subkey == 1)
461 name = name + "e";
462 if(ktype == KT_HAT && subkey == 2)
463 name = name + "s";
464 if(ktype == KT_HAT && subkey == 3)
465 name = name + "w";
466 information_dispatch::do_key_event(modifiers, *this, subkey, polarity, name);
469 keygroup* keygroup::lookup_by_name(const std::string& name) throw()
471 if(keygroups().count(name))
472 return keygroups()[name];
473 else
474 return NULL;
477 std::set<std::string> keygroup::get_axis_set() throw(std::bad_alloc)
479 std::set<std::string> r;
480 for(auto i : keygroups()) {
481 keygroup::parameters p = i.second->get_parameters();
482 std::string type = "";
483 switch(p.ktype) {
484 case keygroup::KT_DISABLED:
485 case keygroup::KT_AXIS_PAIR:
486 case keygroup::KT_AXIS_PAIR_INVERSE:
487 case keygroup::KT_PRESSURE_0M:
488 case keygroup::KT_PRESSURE_0P:
489 case keygroup::KT_PRESSURE_M0:
490 case keygroup::KT_PRESSURE_MP:
491 case keygroup::KT_PRESSURE_P0:
492 case keygroup::KT_PRESSURE_PM:
493 r.insert(i.first);
494 break;
495 default:
496 break;
499 return r;
502 std::set<std::string> keygroup::get_keys() throw(std::bad_alloc)
504 std::set<std::string> r;
505 for(auto i : keygroups()) {
506 switch(i.second->ktype) {
507 case KT_KEY:
508 case KT_PRESSURE_M0:
509 case KT_PRESSURE_MP:
510 case KT_PRESSURE_0M:
511 case KT_PRESSURE_0P:
512 case KT_PRESSURE_PM:
513 case KT_PRESSURE_P0:
514 r.insert(i.first);
515 break;
516 case KT_AXIS_PAIR:
517 case KT_AXIS_PAIR_INVERSE:
518 r.insert(i.first + "+");
519 r.insert(i.first + "-");
520 break;
521 case KT_HAT:
522 r.insert(i.first + "n");
523 r.insert(i.first + "e");
524 r.insert(i.first + "s");
525 r.insert(i.first + "w");
526 break;
527 default:
528 break;
531 return r;
535 namespace
538 function_ptr_command<tokensplitter&> set_axis("set-axis", "Set mode of Joystick axis",
539 "Syntax: set-axis <axis> <options>...\nKnown options: disabled, axis, axis-inverse, pressure0-\n"
540 "pressure0+, pressure-0, pressure-+, pressure+0, pressure+-\nminus=<val>, zero=<val>, plus=<val>\n"
541 "tolerance=<val>\n",
542 [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
543 struct keygroup::parameters p;
544 std::string axis = t;
545 if(axis == "")
546 throw std::runtime_error("Axis name required");
547 if(!keygroups().count(axis))
548 throw std::runtime_error("Unknown axis name");
549 p = keygroups()[axis]->get_parameters();
550 switch(p.ktype) {
551 case keygroup::KT_DISABLED:
552 case keygroup::KT_AXIS_PAIR:
553 case keygroup::KT_AXIS_PAIR_INVERSE:
554 case keygroup::KT_PRESSURE_0M:
555 case keygroup::KT_PRESSURE_0P:
556 case keygroup::KT_PRESSURE_M0:
557 case keygroup::KT_PRESSURE_MP:
558 case keygroup::KT_PRESSURE_P0:
559 case keygroup::KT_PRESSURE_PM:
560 break;
561 default:
562 throw std::runtime_error("Not an axis");
564 bool found_axismode = false;
565 bool found_minus = false;
566 bool found_zero = false;
567 bool found_plus = false;
568 bool found_tolerance = false;
569 while(!!t) {
570 std::string spec = t;
571 if(spec == "disabled") {
572 if(!found_axismode)
573 p.ktype = keygroup::KT_DISABLED;
574 else
575 throw std::runtime_error("Conflicting axis modes");
576 found_axismode = true;
577 } else if(spec == "axis") {
578 if(!found_axismode)
579 p.ktype = keygroup::KT_AXIS_PAIR;
580 else
581 throw std::runtime_error("Conflicting axis modes");
582 found_axismode = true;
583 } else if(spec == "axis-inverse") {
584 if(!found_axismode)
585 p.ktype = keygroup::KT_AXIS_PAIR_INVERSE;
586 else
587 throw std::runtime_error("Conflicting axis modes");
588 found_axismode = true;
589 } else if(spec == "pressure0-") {
590 if(!found_axismode)
591 p.ktype = keygroup::KT_PRESSURE_0M;
592 else
593 throw std::runtime_error("Conflicting axis modes");
594 found_axismode = true;
595 } else if(spec == "pressure0+") {
596 if(!found_axismode)
597 p.ktype = keygroup::KT_PRESSURE_0P;
598 else
599 throw std::runtime_error("Conflicting axis modes");
600 found_axismode = true;
601 } else if(spec == "pressure-0") {
602 if(!found_axismode)
603 p.ktype = keygroup::KT_PRESSURE_M0;
604 else
605 throw std::runtime_error("Conflicting axis modes");
606 found_axismode = true;
607 } else if(spec == "pressure-+") {
608 if(!found_axismode)
609 p.ktype = keygroup::KT_PRESSURE_MP;
610 else
611 throw std::runtime_error("Conflicting axis modes");
612 found_axismode = true;
613 } else if(spec == "pressure+0") {
614 if(!found_axismode)
615 p.ktype = keygroup::KT_PRESSURE_P0;
616 else
617 throw std::runtime_error("Conflicting axis modes");
618 found_axismode = true;
619 } else if(spec == "pressure+-") {
620 if(!found_axismode)
621 p.ktype = keygroup::KT_PRESSURE_PM;
622 else
623 throw std::runtime_error("Conflicting axis modes");
624 found_axismode = true;
625 } else if(spec.substr(0, 6) == "minus=") {
626 if(!found_minus)
627 p.cal_left = parse_value<int16_t>(spec.substr(6));
628 else
629 throw std::runtime_error("Conflicting minus value");
630 found_minus = true;
631 } else if(spec.substr(0, 5) == "zero=") {
632 if(!found_zero)
633 p.cal_center = parse_value<int16_t>(spec.substr(5));
634 else
635 throw std::runtime_error("Conflicting zero value");
636 found_zero = true;
637 } else if(spec.substr(0, 5) == "plus=") {
638 if(!found_plus)
639 p.cal_right = parse_value<int16_t>(spec.substr(5));
640 else
641 throw std::runtime_error("Conflicting plus value");
642 found_plus = true;
643 } else if(spec.substr(0, 10) == "tolerance=") {
644 if(!found_tolerance) {
645 p.cal_tolerance = parse_value<double>(spec.substr(10));
646 if(p.cal_tolerance <= 0 || p.cal_tolerance > 1)
647 throw std::runtime_error("Tolerance out of range");
648 } else
649 throw std::runtime_error("Conflicting tolerance value");
650 found_tolerance = true;
651 } else
652 throw std::runtime_error("Unknown axis modifier");
654 if(found_axismode)
655 keygroups()[axis]->change_type(p.ktype);
656 keygroups()[axis]->change_calibration(p.cal_left, p.cal_center, p.cal_right, p.cal_tolerance);
659 function_ptr_command<> set_axismode("show-axes", "Show all joystick axes",
660 "Syntax: show-axes\n",
661 []() throw(std::bad_alloc, std::runtime_error) {
662 for(auto i = keygroups().begin(); i != keygroups().end(); ++i) {
663 keygroup::parameters p = i->second->get_parameters();
664 std::string type = "";
665 switch(p.ktype) {
666 case keygroup::KT_DISABLED: type = "disabled"; break;
667 case keygroup::KT_AXIS_PAIR: type = "axis"; break;
668 case keygroup::KT_AXIS_PAIR_INVERSE: type = "axis-inverse"; break;
669 case keygroup::KT_PRESSURE_0M: type = "pressure0-"; break;
670 case keygroup::KT_PRESSURE_0P: type = "pressure0+"; break;
671 case keygroup::KT_PRESSURE_M0: type = "pressure-0"; break;
672 case keygroup::KT_PRESSURE_MP: type = "pressure-+"; break;
673 case keygroup::KT_PRESSURE_P0: type = "pressure+0"; break;
674 case keygroup::KT_PRESSURE_PM: type = "pressure+-"; break;
675 default: continue;
677 window::out() << i->first << " " << type << " -:" << p.cal_left << " 0:"
678 << p.cal_center << " +:" << p.cal_right << " t:" << p.cal_tolerance
679 << std::endl;
684 struct triple
686 triple(const std::string& _a, const std::string& _b, const std::string& _c)
688 a = _a;
689 b = _b;
690 c = _c;
692 std::string a;
693 std::string b;
694 std::string c;
695 bool operator==(const triple& t) const
697 bool x = (a == t.a && b == t.b && c == t.c);
698 return x;
700 bool operator<(const triple& t) const
702 bool x = (a < t.a || (a == t.a && b < t.b) || (a == t.a && b == t.b && c < t.c));
703 return x;
706 struct keybind_data : public information_dispatch
708 modifier_set mod;
709 modifier_set modmask;
710 keygroup* group;
711 unsigned subkey;
712 std::string command;
714 keybind_data() : information_dispatch("keybind-listener") {}
716 void on_key_event(const modifier_set& modifiers, keygroup& keygroup, unsigned _subkey, bool polarity,
717 const std::string& name)
719 if(!modifier_set::triggers(modifiers, mod, modmask))
720 return;
721 if(subkey != _subkey)
722 return;
723 if(&keygroup != group)
724 return;
725 std::string cmd = fixup_command_polarity(command, polarity);
726 if(cmd == "")
727 return;
728 command::invokeC(cmd);
732 triple parse_to_triple(const std::string& keyspec)
734 triple k("", "", "");
735 std::string _keyspec = keyspec;
736 size_t split1 = _keyspec.find_first_of("/");
737 size_t split2 = _keyspec.find_first_of("|");
738 if(split1 >= keyspec.length() || split2 >= keyspec.length() || split1 > split2)
739 throw std::runtime_error("Bad keyspec " + keyspec);
740 k.a = _keyspec.substr(0, split1);
741 k.b = _keyspec.substr(split1 + 1, split2 - split1 - 1);
742 k.c = _keyspec.substr(split2 + 1);
743 return k;
746 std::map<triple, keybind_data*> keybindings;
749 void keymapper::bind(std::string mod, std::string modmask, std::string keyname, std::string command)
750 throw(std::bad_alloc, std::runtime_error)
752 triple k(mod, modmask, keyname);
753 modifier_set _mod = modifier_set::construct(mod);
754 modifier_set _modmask = modifier_set::construct(modmask);
755 if(!modifier_set::valid(_mod, _modmask))
756 throw std::runtime_error("Invalid modifiers");
757 auto g = keygroup::lookup(keyname);
758 if(!keybindings.count(k)) {
759 keybindings[k] = new keybind_data;
760 keybindings[k]->mod = _mod;
761 keybindings[k]->modmask = _modmask;
762 keybindings[k]->group = g.first;
763 keybindings[k]->subkey = g.second;
765 keybindings[k]->command = command;
767 void keymapper::unbind(std::string mod, std::string modmask, std::string keyname) throw(std::bad_alloc,
768 std::runtime_error)
770 triple k(mod, modmask, keyname);
771 if(!keybindings.count(k))
772 throw std::runtime_error("Key is not bound");
773 delete keybindings[k];
774 keybindings.erase(k);
777 void keymapper::dumpbindings() throw(std::bad_alloc)
779 for(auto i : keybindings) {
780 messages << "bind-key ";
781 if(i.first.a != "" || i.first.b != "")
782 messages << i.first.a << "/" << i.first.b << " ";
783 messages << i.first.c << std::endl;
787 std::set<std::string> keymapper::get_bindings() throw(std::bad_alloc)
789 std::set<std::string> r;
790 for(auto i : keybindings)
791 r.insert(i.first.a + "/" + i.first.b + "|" + i.first.c);
792 return r;
795 std::string keymapper::get_command_for(const std::string& keyspec) throw(std::bad_alloc)
797 triple k("", "", "");
798 try {
799 k = parse_to_triple(keyspec);
800 } catch(std::exception& e) {
801 return "";
803 if(!keybindings.count(k))
804 return "";
805 return keybindings[k]->command;
808 void keymapper::bind_for(const std::string& keyspec, const std::string& cmd) throw(std::bad_alloc, std::runtime_error)
810 triple k("", "", "");
811 k = parse_to_triple(keyspec);
812 if(cmd != "")
813 bind(k.a, k.b, k.c, cmd);
814 else
815 unbind(k.a, k.b, k.c);