summaryrefslogtreecommitdiff
path: root/src/dged/binding.h
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-04-06 23:23:46 +0200
committerAlbert Cervin <albert@acervin.com>2023-05-01 22:19:14 +0200
commita123725a12e948d78badb2cb686d38548f1c633b (patch)
treec92c46134ef5536fbbf3bf08983c4f0dea1aaf58 /src/dged/binding.h
parentb5ed4cf757afc50afb6ac499eee7b87a2648fa4c (diff)
downloaddged-a123725a12e948d78badb2cb686d38548f1c633b.tar.gz
dged-a123725a12e948d78badb2cb686d38548f1c633b.tar.xz
dged-a123725a12e948d78badb2cb686d38548f1c633b.zip
Implement window handling
Also implement searching. fix undo boundaries when it checked for other save point, it used && instead of == which caused it to overwrite other types. Fix bytes vs chars bug in text_get_region
Diffstat (limited to 'src/dged/binding.h')
-rw-r--r--src/dged/binding.h136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/dged/binding.h b/src/dged/binding.h
new file mode 100644
index 0000000..f2a531d
--- /dev/null
+++ b/src/dged/binding.h
@@ -0,0 +1,136 @@
+#include "hash.h"
+#include "keyboard.h"
+
+/**
+ * Directory of keyboard mappings.
+ */
+struct keymap {
+ /** Keymap name */
+ const char *name;
+
+ /** The bindings in this keymap */
+ struct binding *bindings;
+
+ /** The number of bindings in this keymap */
+ uint32_t nbindings;
+
+ /** The number of bindings this keymap can currently hold */
+ uint32_t capacity;
+};
+
+/**
+ * Type of a keyboard binding
+ */
+enum binding_type {
+ /** This binding is to a command */
+ BindingType_Command,
+
+ /** This binding is to another keymap */
+ BindingType_Keymap,
+
+ /** This binding is to an already resolved command,
+ * a.k.a. anonymous binding.
+ */
+ BindingType_DirectCommand
+};
+
+#define BINDING_INNER(mod_, c_, command_) \
+ (struct binding) { \
+ .key = {.mod = mod_, .key = c_}, .type = BindingType_Command, \
+ .command = hash_name(command_) \
+ }
+
+#define ANONYMOUS_BINDING_INNER(mod_, c_, command_) \
+ (struct binding) { \
+ .key = {.mod = mod_, .key = c_}, .type = BindingType_DirectCommand, \
+ .direct_command = command_ \
+ }
+
+#define PREFIX_INNER(mod_, c_, keymap_) \
+ (struct binding) { \
+ .key = {.mod = mod_, .key = c_}, .type = BindingType_Keymap, \
+ .keymap = keymap_ \
+ }
+
+#define BINDING(...) BINDING_INNER(__VA_ARGS__)
+#define PREFIX(...) PREFIX_INNER(__VA_ARGS__)
+#define ANONYMOUS_BINDING(...) ANONYMOUS_BINDING_INNER(__VA_ARGS__)
+
+/**
+ * A keyboard key bound to an action
+ */
+struct binding {
+ /** The keyboard key that triggers the action in this binding */
+ struct key key;
+
+ /** Type of this binding, see @ref binding_type */
+ uint8_t type;
+
+ union {
+ /** A hash of a command name */
+ uint32_t command;
+ /** A command */
+ struct command *direct_command;
+ /** A keymap */
+ struct keymap *keymap;
+ };
+};
+
+/**
+ * Result of a binding lookup
+ */
+struct lookup_result {
+ /** True if a binding was found */
+ bool found;
+
+ /** Type of binding in the result */
+ uint8_t type;
+
+ union {
+ /** A command */
+ struct command *command;
+ /** A keymap */
+ struct keymap *keymap;
+ };
+};
+
+struct commands;
+
+/**
+ * Create a new keymap
+ *
+ * @param name Name of the keymap
+ * @param capacity Initial capacity of the keymap.
+ * @returns The created keymap
+ */
+struct keymap keymap_create(const char *name, uint32_t capacity);
+
+/**
+ * Bind keys in a keymap.
+ *
+ * @param keymap The keymap to bind keys in.
+ * @param bindings Bindings to add.
+ * @param nbindings Number of bindings in @ref bindings.
+ */
+void keymap_bind_keys(struct keymap *keymap, struct binding *bindings,
+ uint32_t nbindings);
+
+/**
+ * Destroy a keymap.
+ *
+ * This clears all keybindings associated with the keymap.
+ * @param keymap Keymap to destroy.
+ */
+void keymap_destroy(struct keymap *keymap);
+
+/**
+ * Lookup the binding for a key in a set of keymaps.
+ *
+ * @param keymaps The keymaps to look in.
+ * @param nkeymaps The number of keymaps in @ref keymaps.
+ * @param key The keystroke to look up bindings for.
+ * @param commands Available commands for lookup.
+ * @returns A @ref lookup_result with the result of the lookup.
+ */
+struct lookup_result lookup_key(struct keymap *keymaps, uint32_t nkeymaps,
+ struct key *key, struct commands *commands);