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"
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 "
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()) {
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
);
34 throw std::runtime_error("Expected optional modifiers and key");
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
;
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()) {
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
);
59 throw std::runtime_error("Expected optional modifiers and key");
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
;
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();
77 std::string
fixup_command_polarity(std::string cmd
, bool polarity
) throw(std::bad_alloc
)
79 if(cmd
== "" || cmd
== "*")
82 if(cmd
[0] != '+' && polarity
)
84 if(cmd
[0] == '+' && !polarity
)
87 if(cmd
[1] != '+' && polarity
)
89 if(cmd
[1] == '+' && !polarity
)
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()))
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())
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
)
151 std::string
modifier::linked_name() const throw(std::bad_alloc
)
153 const modifier
* p
= get_linked_modifier(this);
160 void modifier_set::add(const modifier
& mod
, bool really
) throw(std::bad_alloc
)
166 void modifier_set::remove(const modifier
& mod
, bool really
) throw(std::bad_alloc
)
172 modifier_set
modifier_set::construct(const std::string
& _modifiers
) throw(std::bad_alloc
, std::runtime_error
)
175 std::string modifiers
= _modifiers
;
176 while(modifiers
!= "") {
177 std::string mod
= modifiers
;
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
));
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
))
198 for(auto i
: mask
.set
) {
199 const modifier
* j
= get_linked_modifier(i
);
200 if(i
!= j
&& mask
.set
.count(j
))
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
))
212 bool modifier_set::operator==(const modifier_set
& m
) const throw()
223 std::ostream
& operator<<(std::ostream
& os
, const modifier_set
& m
)
227 os
<< i
->name() << " ";
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
) {
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
))
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
);
249 if(!trigger
.set
.count(j
))
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
);
258 if(!trigger
.set
.count(i
))
262 //Nothing linked is found from neither set nor trigger.
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
);
275 std::string
keygroup::name() throw(std::bad_alloc
)
280 struct keygroup::parameters
keygroup::get_parameters()
284 p
.cal_left
= cal_left
;
285 p
.cal_center
= cal_center
;
286 p
.cal_right
= cal_right
;
287 p
.cal_tolerance
= cal_tolerance
;
292 keygroup::keygroup(const std::string
& name
, enum type t
) throw(std::bad_alloc
)
294 keygroups()[keyname
= name
] = this;
303 keygroup::~keygroup() throw()
305 keygroups().erase(keyname
);
308 void keygroup::change_type(enum type t
) throw()
314 std::pair
<keygroup
*, unsigned> keygroup::lookup(const std::string
& name
) throw(std::bad_alloc
,
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
];
328 return std::make_pair(g
, 0);
331 return std::make_pair(g
, 1);
333 return std::make_pair(g
, 2);
335 return std::make_pair(g
, 3);
337 throw std::runtime_error("Invalid key");
341 void keygroup::change_calibration(short left
, short center
, short right
, double tolerance
)
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
)
355 else if(value
>= cal_right
)
357 else if(value
== cal_center
)
359 else if(value
< cal_center
)
360 return (static_cast<double>(value
) - cal_center
) / (static_cast<double>(cal_center
) - cal_left
);
362 return (static_cast<double>(value
) - cal_center
) / (static_cast<double>(cal_right
) - cal_center
);
365 double keygroup::compensate2(double value
)
369 return 0; //Always neutral.
372 return value
; //No mapping.
380 return (1 + value
) / 2;
384 return (1 - value
) / 2;
387 case KT_AXIS_PAIR_INVERSE
:
392 void keygroup::set_position(short pos
, const modifier_set
& modifiers
) throw()
394 double x
= compensate2(compensate(pos
));
396 bool left
, right
, up
, down
;
397 bool oleft
, oright
, oup
, odown
;
408 tmp
= (x
>= cal_tolerance
);
409 run_listeners(modifiers
, 0, true, (!state
&& tmp
), x
);
410 run_listeners(modifiers
, 0, false, (state
&& !tmp
), x
);
414 case KT_AXIS_PAIR_INVERSE
:
415 if(x
<= -cal_tolerance
)
417 else if(x
>= cal_tolerance
)
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
);
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
);
449 void keygroup::run_listeners(const modifier_set
& modifiers
, unsigned subkey
, bool polarity
, bool really
, double x
)
453 std::string name
= keyname
;
454 if(ktype
== KT_AXIS_PAIR
&& subkey
== 0)
456 if(ktype
== KT_AXIS_PAIR
&& subkey
== 1)
458 if(ktype
== KT_HAT
&& subkey
== 0)
460 if(ktype
== KT_HAT
&& subkey
== 1)
462 if(ktype
== KT_HAT
&& subkey
== 2)
464 if(ktype
== KT_HAT
&& subkey
== 3)
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
];
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
= "";
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
:
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
) {
517 case KT_AXIS_PAIR_INVERSE
:
518 r
.insert(i
.first
+ "+");
519 r
.insert(i
.first
+ "-");
522 r
.insert(i
.first
+ "n");
523 r
.insert(i
.first
+ "e");
524 r
.insert(i
.first
+ "s");
525 r
.insert(i
.first
+ "w");
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"
542 [](tokensplitter
& t
) throw(std::bad_alloc
, std::runtime_error
) {
543 struct keygroup::parameters p
;
544 std::string axis
= t
;
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();
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
:
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;
570 std::string spec
= t
;
571 if(spec
== "disabled") {
573 p
.ktype
= keygroup::KT_DISABLED
;
575 throw std::runtime_error("Conflicting axis modes");
576 found_axismode
= true;
577 } else if(spec
== "axis") {
579 p
.ktype
= keygroup::KT_AXIS_PAIR
;
581 throw std::runtime_error("Conflicting axis modes");
582 found_axismode
= true;
583 } else if(spec
== "axis-inverse") {
585 p
.ktype
= keygroup::KT_AXIS_PAIR_INVERSE
;
587 throw std::runtime_error("Conflicting axis modes");
588 found_axismode
= true;
589 } else if(spec
== "pressure0-") {
591 p
.ktype
= keygroup::KT_PRESSURE_0M
;
593 throw std::runtime_error("Conflicting axis modes");
594 found_axismode
= true;
595 } else if(spec
== "pressure0+") {
597 p
.ktype
= keygroup::KT_PRESSURE_0P
;
599 throw std::runtime_error("Conflicting axis modes");
600 found_axismode
= true;
601 } else if(spec
== "pressure-0") {
603 p
.ktype
= keygroup::KT_PRESSURE_M0
;
605 throw std::runtime_error("Conflicting axis modes");
606 found_axismode
= true;
607 } else if(spec
== "pressure-+") {
609 p
.ktype
= keygroup::KT_PRESSURE_MP
;
611 throw std::runtime_error("Conflicting axis modes");
612 found_axismode
= true;
613 } else if(spec
== "pressure+0") {
615 p
.ktype
= keygroup::KT_PRESSURE_P0
;
617 throw std::runtime_error("Conflicting axis modes");
618 found_axismode
= true;
619 } else if(spec
== "pressure+-") {
621 p
.ktype
= keygroup::KT_PRESSURE_PM
;
623 throw std::runtime_error("Conflicting axis modes");
624 found_axismode
= true;
625 } else if(spec
.substr(0, 6) == "minus=") {
627 p
.cal_left
= parse_value
<int16_t>(spec
.substr(6));
629 throw std::runtime_error("Conflicting minus value");
631 } else if(spec
.substr(0, 5) == "zero=") {
633 p
.cal_center
= parse_value
<int16_t>(spec
.substr(5));
635 throw std::runtime_error("Conflicting zero value");
637 } else if(spec
.substr(0, 5) == "plus=") {
639 p
.cal_right
= parse_value
<int16_t>(spec
.substr(5));
641 throw std::runtime_error("Conflicting plus value");
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");
649 throw std::runtime_error("Conflicting tolerance value");
650 found_tolerance
= true;
652 throw std::runtime_error("Unknown axis modifier");
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
= "";
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;
677 window::out() << i
->first
<< " " << type
<< " -:" << p
.cal_left
<< " 0:"
678 << p
.cal_center
<< " +:" << p
.cal_right
<< " t:" << p
.cal_tolerance
686 triple(const std::string
& _a
, const std::string
& _b
, const std::string
& _c
)
695 bool operator==(const triple
& t
) const
697 bool x
= (a
== t
.a
&& b
== t
.b
&& c
== t
.c
);
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
));
706 struct keybind_data
: public information_dispatch
709 modifier_set modmask
;
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
))
721 if(subkey
!= _subkey
)
723 if(&keygroup
!= group
)
725 std::string cmd
= fixup_command_polarity(command
, polarity
);
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);
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
,
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
);
795 std::string
keymapper::get_command_for(const std::string
& keyspec
) throw(std::bad_alloc
)
797 triple
k("", "", "");
799 k
= parse_to_triple(keyspec
);
800 } catch(std::exception
& e
) {
803 if(!keybindings
.count(k
))
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
);
813 bind(k
.a
, k
.b
, k
.c
, cmd
);
815 unbind(k
.a
, k
.b
, k
.c
);