1 //------------------------------------------------------------------------
2 //  KEY BINDINGS
3 //------------------------------------------------------------------------
4 //
5 //  Eureka DOOM Editor
6 //
7 //  Copyright (C) 2013-2016 Andrew Apted
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; either version 2
12 //  of the License, or (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU General Public License for more details.
18 //
19 //------------------------------------------------------------------------
20 
21 #ifndef __EUREKA_M_KEYS_H__
22 #define __EUREKA_M_KEYS_H__
23 
24 
25 /* Key value:
26  *   - can be a printable ASCII character, e.g. 'a', '2', ';'
27  *   - spacebar is ' ' (ASCII code 32)
28  *   - letter keys are lowercase
29  *   - all other keys use the FLTK code (e.g. FL_Enter, FL_Up, etc)
30  *   - control keys (like CTRL-A) use MOD_COMMAND flag (never '\001')
31  *
32  * Modifier (MOD_XXXX value) is or-ed with the bare key.
33  *   - uppercase letters are the lowercase letter + MOD_SHIFT
34  *   - can extract bare key with FL_KEY_MASK
35  *   - can extract modifier with MOD_ALL_MASK
36  *   - only a single modifier can be present:
37  *       MOD_COMMAND > MOD_META > MOD_ALT > MOD_SHIFT
38  *   - using my own names since "FL_CONTROL" is fucking confusing
39  */
40 typedef unsigned int keycode_t;
41 
42 #define MOD_none     0
43 
44 #undef  MOD_ALT    // these clash with Windows defines
45 #undef  MOD_SHIFT  //
46 
47 #define MOD_COMMAND  FL_COMMAND
48 #define MOD_META     FL_CONTROL
49 #define MOD_ALT      FL_ALT
50 #define MOD_SHIFT    FL_SHIFT
51 
52 #define MOD_ALL_MASK  (MOD_COMMAND | MOD_META | MOD_ALT | MOD_SHIFT)
53 
54 
55 // made-up values to represent the mouse wheel
56 #define FL_WheelUp		0xEF91
57 #define FL_WheelDown	0xEF92
58 #define FL_WheelLeft	0xEF93
59 #define FL_WheelRight	0xEF94
60 
61 
62 bool is_mouse_wheel (keycode_t key);
63 bool is_mouse_button(keycode_t key);
64 
65 
66 typedef enum
67 {
68 	KCTX_NONE = 0,  /* INVALID */
69 
70 	KCTX_Browser,
71 	KCTX_Render,
72 
73 	KCTX_Vertex,
74 	KCTX_Thing,
75 	KCTX_Sector,
76 	KCTX_Line,
77 
78 	KCTX_General
79 
80 } key_context_e;
81 
82 
83 /* --- general manipulation --- */
84 
85 int M_KeyCmp(keycode_t A, keycode_t B);
86 
87 key_context_e M_ParseKeyContext(const char *str);
88 const char * M_KeyContextString(key_context_e context);
89 
90 keycode_t M_ParseKeyString(const char *str);
91 const char * M_KeyToString(keycode_t key);
92 
93 
94 keycode_t M_TranslateKey(int key, int state);
95 
96 int M_KeyToShortcut(keycode_t key);
97 
98 key_context_e M_ModeToKeyContext(obj_type_e mode);
99 
100 
101 /* --- preferences dialog stuff --- */
102 
103 void M_CopyBindings(bool from_defaults = false);
104 void M_SortBindings(char column, bool reverse);
105 void M_ApplyBindings();
106 
107 int  M_NumBindings();
108 void M_DetectConflictingBinds();
109 
110 const char * M_StringForFunc(int index);
111 const char * M_StringForBinding(int index, bool changing_key = false);
112 
113 void M_GetBindingInfo(int index, keycode_t *key, key_context_e *context);
114 bool M_IsBindingFuncValid(key_context_e context, const char * func_str);
115 
116 void M_ChangeBindingKey(int index, keycode_t key);
117 const char * M_SetLocalBinding(int index, keycode_t key, key_context_e context,
118                                const char *func_str);
119 const char * M_AddLocalBinding(int after, keycode_t key, key_context_e context,
120                                const char *func_str);
121 void M_DeleteLocalBinding(int index);
122 
123 
124 void M_LoadBindings();
125 void M_SaveBindings();
126 
127 bool M_IsKeyBound   (keycode_t key, key_context_e context);
128 void M_RemoveBinding(keycode_t key, key_context_e context);
129 
130 
131 /* --- command execution stuff --- */
132 
133 typedef void (* command_func_t)(void);
134 
135 typedef struct
136 {
137 	const char *name;
138 
139 	// used in the Key binding preferences
140 	// when NULL, will be computed from 'req_context'
141 	const char *group_name;
142 
143 	command_func_t func;
144 
145 	const char *flag_list;
146 	const char *keyword_list;
147 
148 	// this value is computed when registering
149 	key_context_e req_context;
150 
151 } editor_command_t;
152 
153 
154 void M_RegisterCommandList(editor_command_t * list);
155 
156 const editor_command_t *   FindEditorCommand(const char *name);
157 const editor_command_t * LookupEditorCommand(int index);
158 
159 
160 
161 // parameter(s) for command function -- must be valid strings
162 #define MAX_EXEC_PARAM   16
163 #define MAX_BIND_LENGTH  64
164 
165 extern const char * EXEC_Param[MAX_EXEC_PARAM];
166 extern const char * EXEC_Flags[MAX_EXEC_PARAM];
167 
168 // result from command function, 0 is OK
169 extern int EXEC_Errno;
170 
171 // key or mouse button pressed for command, 0 when none
172 extern keycode_t EXEC_CurKey;
173 
174 bool Exec_HasFlag(const char *flag);
175 
176 bool ExecuteKey(keycode_t key, key_context_e context);
177 
178 bool ExecuteCommand(const editor_command_t *cmd,
179 					const char *param1 = "", const char *param2 = "",
180                     const char *param3 = "", const char *param4 = "");
181 
182 bool ExecuteCommand(const char *name,
183 					const char *param1 = "", const char *param2 = "",
184                     const char *param3 = "", const char *param4 = "");
185 
186 #endif  /* __EUREKA_M_KEYS_H__ */
187 
188 //--- editor settings ---
189 // vi:ts=4:sw=4:noexpandtab
190