README: add build instructions
[rofl0r-df-libgraphics.git] / g_src / enabler_input.h
blob3473fcb20b0af7e9706610129128b63f916df42b
1 #ifndef ENABLER_INPUT_H
2 #define ENABLER_INPUT_H
4 #include <SDL/SDL.h>
5 #include <string>
6 #include <set>
7 #include <list>
9 #include "ViewBase.h"
10 #include "keybindings.h"
11 #include "curses.h"
13 typedef Uint32 Time;
15 enum Repeat {
16 REPEAT_NOT, // Don't repeat at all. Furthermore, cancel other repeats.
17 REPEAT_SLOW, // Repeat normally.
18 REPEAT_FAST // Repeat instantly, without waiting for the first-repeat interval.
21 enum MatchType { type_unicode, type_key, type_button };
23 Uint8 getModState();
24 std::string translate_mod(Uint8 mod);
25 int decode_utf8(const std::string &s);
26 int decode_utf8_predict_length(char byte);
27 std::string encode_utf8(int unicode);
29 #define DFMOD_SHIFT 1
30 #define DFMOD_CTRL 2
31 #define DFMOD_ALT 4
33 struct EventMatch {
34 MatchType type;
35 Uint8 mod; // not defined for type=unicode. 1: shift, 2: ctrl, 4:alt
36 Uint8 scancode; // not defined for type=button
37 union {
38 Uint16 unicode;
39 SDLKey key;
40 Uint8 button;
43 bool operator== (const EventMatch &other) const {
44 if (mod != other.mod) return false;
45 if (type != other.type) return false;
46 switch (type) {
47 case type_unicode: return unicode == other.unicode;
48 case type_key: return key == other.key;
49 case type_button: return button == other.button;
50 default: return false;
54 bool operator< (const EventMatch &other) const {
55 if (mod != other.mod) return mod < other.mod;
56 if (type != other.type) return type < other.type;
57 switch (type) {
58 case type_unicode: return unicode < other.unicode;
59 case type_key: return key < other.key;
60 case type_button: return button < other.button;
61 default: return false;
66 struct KeyEvent {
67 bool release;
68 EventMatch match;
71 typedef std::list<std::set<InterfaceKey> > macro;
73 struct RegisteredKey {
74 MatchType type;
75 string display;
78 class enabler_inputst {
79 std::set<InterfaceKey> key_translation(EventMatch &match);
80 public:
81 Repeat key_repeat(InterfaceKey);
82 void key_repeat(InterfaceKey, Repeat);
83 void load_macro_from_file(const std::string &file);
84 void save_macro_to_file(const std::string &file, const std::string &name, const macro &);
86 // In practice.. do not use this one.
87 void add_input(SDL_Event &e, Time now);
88 // Use this one. It's much nicer.
89 void add_input_refined(KeyEvent &e, Time now, int serial);
90 // Made specifically for curses. <0 = unicode, >0 = ncurses symbols.
91 #ifdef CURSES
92 void add_input_ncurses(int key, Time now, bool esc);
93 #endif
94 std::set<InterfaceKey> get_input(Time now);
95 void clear_input();
97 void load_keybindings(const std::string &file);
98 void save_keybindings(const std::string &file);
99 void save_keybindings();
100 std::string GetKeyDisplay(int binding);
101 std::string GetBindingDisplay(int binding);
102 std::string GetBindingTextDisplay(int binding);
104 // Macros
105 void record_input(); // Records input until such a time as you say stop
106 void record_stop(); // Stops recording, saving it as the active macro
107 bool is_recording();
108 void play_macro(); // Runs the active macro, if any
109 bool is_macro_playing();
110 std::list<string> list_macros();
111 void load_macro(string name); // Loads some macro as the active one
112 void save_macro(string name); // Saves the active macro under some name
113 void delete_macro(string name);
115 // Prefix commands
116 bool prefix_building();
117 void prefix_toggle();
118 void prefix_add_digit(char digit);
119 int prefix_end();
120 string prefix();
122 // Updating the key-bindings
123 void register_key(); // Sets the next key-press to be stored instead of executed.
124 list<RegisteredKey> getRegisteredKey(); // Returns a description of stored keys. Max one of each type.
125 void bindRegisteredKey(MatchType type, InterfaceKey key); // Binds one of the stored keys to key
126 bool is_registering(); // Returns true if we're still waiting for a key-hit
128 std::list<EventMatch> list_keys(InterfaceKey key); // Returns a list of events matching this interfacekey
129 void remove_key(InterfaceKey key, EventMatch ev); // Removes a particular matcher from the keymap.
133 #endif