1 #ifndef EL__CONFIG_KBDBIND_H
2 #define EL__CONFIG_KBDBIND_H
3 
4 #include "config/options.h"
5 #include "main/event.h"
6 #include "main/object.h"
7 #include "terminal/terminal.h"
8 #include "util/string.h"
9 
10 struct listbox_item;
11 
12 /* Used for holding enum <keymap>_action values. */
13 typedef long action_id_T;
14 
15 enum keymap_id {
16 	KEYMAP_INVALID = -1,
17 	KEYMAP_MAIN,
18 	KEYMAP_EDIT,
19 	KEYMAP_MENU,
20 	KEYMAP_MAX
21 };
22 
23 struct action {
24 	unsigned char *str;
25 	action_id_T num;
26 	enum keymap_id keymap_id;
27 	unsigned char *desc;
28 	unsigned int flags;
29 };
30 
31 struct action_list {
32 	struct action *actions;
33 	int num_actions;
34 };
35 struct keymap {
36 	unsigned char *str;
37 	enum keymap_id keymap_id;
38 	unsigned char *desc;
39 };
40 
41 enum action_flags {
42 	ACTION_RESTRICT_ANONYMOUS	=    (1 << 16),
43 	ACTION_REQUIRE_VIEW_STATE	=    (1 << 17),
44 	ACTION_REQUIRE_LOCATION		=    (1 << 18),
45 	ACTION_JUMP_TO_LINK		=    (1 << 19),
46 	ACTION_REQUIRE_LINK		=    (1 << 20),
47 	ACTION_REQUIRE_FORM		=    (1 << 21),
48 	ACTION_FLAGS_MASK		= (0xFF << 16),
49 };
50 
51 /* Note: if you add anything here, please keep it in alphabetical order,
52  * and also update the table action_table[] in kbdbind.c.  */
53 
54 #define ACTION_(map, name, action, caption, flags)	\
55 	ACT_##map##_OFFSET_##action
56 
57 enum main_action_offset {
58 #include "config/actions-main.inc"
59 
60 	MAIN_ACTIONS,
61 };
62 
63 enum edit_action_offset {
64 #include "config/actions-edit.inc"
65 
66 	EDIT_ACTIONS
67 };
68 
69 enum menu_action_offset {
70 #include "config/actions-menu.inc"
71 
72 	MENU_ACTIONS
73 };
74 
75 #undef	ACTION_
76 #define ACTION_(map, name, action, caption, flags)	\
77 	ACT_##map##_##action
78 
79 enum main_action {
80 #include "config/actions-main.inc"
81 };
82 
83 enum edit_action {
84 #include "config/actions-edit.inc"
85 };
86 
87 enum menu_action {
88 #include "config/actions-menu.inc"
89 };
90 
91 #undef	ACTION_
92 
93 enum kbdbind_flags {
94 	KBDB_WATERMARK = 1,
95 	KBDB_TOUCHED = 2,
96 
97 	/* Marks whether the binding has a key that is used
98 	 * by one of the default bindings. */
99 	KBDB_DEFAULT_KEY = 4,
100 
101 	/* Marks whether the binding itself (the combination of key
102 	 * _and_ action) is default. */
103 	KBDB_DEFAULT_BINDING = 8,
104 };
105 
106 struct keybinding {
107 	OBJECT_HEAD(struct keybinding);
108 
109 	enum keymap_id keymap_id;
110 	action_id_T action_id;
111 	struct term_event_keyboard kbd;
112 	int event;
113 	enum kbdbind_flags flags;
114 	struct listbox_item *box_item;
115 };
116 
117 
118 void init_keymaps(void);
119 void free_keymaps(void);
120 
121 struct keybinding *add_keybinding(enum keymap_id keymap_id, action_id_T action_id, struct term_event_keyboard *kbd, int event);
122 int keybinding_exists(enum keymap_id keymap_id, struct term_event_keyboard *kbd, action_id_T *action_id);
123 void free_keybinding(struct keybinding *);
124 
125 struct action *get_action(enum keymap_id keymap_id, action_id_T action_id);
126 unsigned char *get_action_name(enum keymap_id keymap_id, action_id_T action_id);
127 action_id_T get_action_from_string(enum keymap_id keymap_id, unsigned char *str);
128 unsigned char *get_action_name_from_keystroke(enum keymap_id keymap_id,
129                                               unsigned char *keystroke_str);
130 
131 static inline unsigned int
action_is_anonymous_safe(enum keymap_id keymap_id,action_id_T action_id)132 action_is_anonymous_safe(enum keymap_id keymap_id, action_id_T action_id)
133 {
134 	struct action *action = get_action(keymap_id, action_id);
135 
136 	return action && !(action->flags & ACTION_RESTRICT_ANONYMOUS);
137 }
138 
139 static inline unsigned int
action_requires_view_state(enum keymap_id keymap_id,action_id_T action_id)140 action_requires_view_state(enum keymap_id keymap_id, action_id_T action_id)
141 {
142 	struct action *action = get_action(keymap_id, action_id);
143 
144 	return action && (action->flags & ACTION_REQUIRE_VIEW_STATE);
145 }
146 
147 static inline unsigned int
action_requires_location(enum keymap_id keymap_id,action_id_T action_id)148 action_requires_location(enum keymap_id keymap_id, action_id_T action_id)
149 {
150 	struct action *action = get_action(keymap_id, action_id);
151 
152 	return action && (action->flags & ACTION_REQUIRE_LOCATION);
153 }
154 
155 static inline unsigned int
action_prefix_is_link_number(enum keymap_id keymap_id,action_id_T action_id)156 action_prefix_is_link_number(enum keymap_id keymap_id, action_id_T action_id)
157 {
158 	struct action *action = get_action(keymap_id, action_id);
159 
160 	return action && (action->flags & ACTION_JUMP_TO_LINK);
161 }
162 
163 static inline unsigned int
action_requires_link(enum keymap_id keymap_id,action_id_T action_id)164 action_requires_link(enum keymap_id keymap_id, action_id_T action_id)
165 {
166 	struct action *action = get_action(keymap_id, action_id);
167 
168 	return action && (action->flags & ACTION_REQUIRE_LINK);
169 }
170 
171 static inline unsigned int
action_requires_form(enum keymap_id keymap_id,action_id_T action_id)172 action_requires_form(enum keymap_id keymap_id, action_id_T action_id)
173 {
174 	struct action *action = get_action(keymap_id, action_id);
175 
176 	return action && (action->flags & ACTION_REQUIRE_FORM);
177 }
178 
179 long read_key(unsigned char *);
180 unsigned char *get_keymap_name(enum keymap_id);
181 
182 int parse_keystroke(unsigned char *, struct term_event_keyboard *);
183 void add_keystroke_to_string(struct string *str, struct term_event_keyboard *kbd, int escape);
184 
185 #define add_accesskey_to_string(str, accesskey) do { 		\
186 	struct term_event_keyboard kbd; 			\
187 	kbd.key = accesskey; /* FIXME: unicode_val_T to int */	\
188 	kbd.modifier = 0; 					\
189 	add_keystroke_to_string(str, &kbd, 0); 			\
190 } while (0)
191 
192 action_id_T kbd_action(enum keymap_id, struct term_event *, int *);
193 struct keybinding *kbd_ev_lookup(enum keymap_id, struct term_event_keyboard *kbd, int *);
194 struct keybinding *kbd_nm_lookup(enum keymap_id, unsigned char *);
195 
196 int bind_do(unsigned char *, unsigned char *, unsigned char *, int);
197 unsigned char *bind_act(unsigned char *, unsigned char *);
198 void bind_config_string(struct string *);
199 
200 #ifdef CONFIG_SCRIPTING
201 int bind_key_to_event_name(unsigned char *, unsigned char *, unsigned char *,
202 			   unsigned char **);
203 #endif
204 
205 void add_keystroke_action_to_string(struct string *string, action_id_T action_id, enum keymap_id keymap_id);
206 unsigned char *get_keystroke(action_id_T action_id, enum keymap_id keymap_id);
207 
208 void add_actions_to_string(struct string *string, action_id_T actions[],
209 			   enum keymap_id keymap_id, struct terminal *term);
210 
211 #endif
212