Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 1 | /* $OpenBSD$ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
| 15 | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 16 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <ctype.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "tmux.h" |
| 26 | |
Nicholas Marriott | 8ed9124 | 2012-01-21 11:12:13 +0000 | [diff] [blame] | 27 | RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp); |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 28 | RB_GENERATE(key_tables, key_table, entry, key_table_cmp); |
| 29 | struct key_tables key_tables = RB_INITIALIZER(&key_tables); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 30 | |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 31 | int |
| 32 | key_table_cmp(struct key_table *e1, struct key_table *e2) |
| 33 | { |
| 34 | return (strcmp(e1->name, e2->name)); |
| 35 | } |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 36 | |
| 37 | int |
| 38 | key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2) |
| 39 | { |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 40 | return (bd1->key - bd2->key); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 41 | } |
| 42 | |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 43 | struct key_table * |
| 44 | key_bindings_get_table(const char *name, int create) |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 45 | { |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 46 | struct key_table table_find, *table; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 47 | |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 48 | table_find.name = name; |
| 49 | table = RB_FIND(key_tables, &key_tables, &table_find); |
| 50 | if (table != NULL || !create) |
| 51 | return (table); |
| 52 | |
| 53 | table = xmalloc(sizeof *table); |
| 54 | table->name = xstrdup(name); |
| 55 | RB_INIT(&table->key_bindings); |
| 56 | |
| 57 | table->references = 1; /* one reference in key_tables */ |
| 58 | RB_INSERT(key_tables, &key_tables, table); |
| 59 | |
| 60 | return (table); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 64 | key_bindings_unref_table(struct key_table *table) |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 65 | { |
| 66 | struct key_binding *bd; |
| 67 | |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 68 | if (--table->references != 0) |
| 69 | return; |
| 70 | |
| 71 | while (!RB_EMPTY(&table->key_bindings)) { |
| 72 | bd = RB_ROOT(&table->key_bindings); |
| 73 | RB_REMOVE(key_bindings, &table->key_bindings, bd); |
| 74 | cmd_list_free(bd->cmdlist); |
| 75 | free(bd); |
| 76 | } |
| 77 | |
| 78 | free((void *)table->name); |
| 79 | free(table); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | key_bindings_add(const char *name, int key, int can_repeat, |
| 84 | struct cmd_list *cmdlist) |
| 85 | { |
| 86 | struct key_table *table; |
| 87 | struct key_binding bd_find, *bd; |
| 88 | |
| 89 | table = key_bindings_get_table(name, 1); |
| 90 | |
| 91 | bd_find.key = key; |
| 92 | bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find); |
| 93 | if (bd != NULL) { |
| 94 | RB_REMOVE(key_bindings, &table->key_bindings, bd); |
| 95 | cmd_list_free(bd->cmdlist); |
| 96 | free(bd); |
| 97 | } |
Nicholas Marriott | 15a64b8 | 2009-12-03 22:50:09 +0000 | [diff] [blame] | 98 | |
Nicholas Marriott | 9e49ec6 | 2009-07-12 17:33:18 +0000 | [diff] [blame] | 99 | bd = xmalloc(sizeof *bd); |
| 100 | bd->key = key; |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 101 | RB_INSERT(key_bindings, &table->key_bindings, bd); |
Nicholas Marriott | 15a64b8 | 2009-12-03 22:50:09 +0000 | [diff] [blame] | 102 | |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 103 | bd->can_repeat = can_repeat; |
| 104 | bd->cmdlist = cmdlist; |
| 105 | } |
| 106 | |
| 107 | void |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 108 | key_bindings_remove(const char *name, int key) |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 109 | { |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 110 | struct key_table *table; |
| 111 | struct key_binding bd_find, *bd; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 112 | |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 113 | table = key_bindings_get_table(name, 0); |
| 114 | if (table == NULL) |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 115 | return; |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 116 | |
| 117 | bd_find.key = key; |
| 118 | bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find); |
| 119 | if (bd == NULL) |
| 120 | return; |
| 121 | |
| 122 | RB_REMOVE(key_bindings, &table->key_bindings, bd); |
nicm | 53cbae5 | 2014-05-14 06:21:19 +0000 | [diff] [blame] | 123 | cmd_list_free(bd->cmdlist); |
| 124 | free(bd); |
nicm | bded743 | 2015-04-20 15:34:56 +0000 | [diff] [blame] | 125 | |
| 126 | if (RB_EMPTY(&table->key_bindings)) { |
| 127 | RB_REMOVE(key_tables, &key_tables, table); |
| 128 | key_bindings_unref_table(table); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | key_bindings_remove_table(const char *name) |
| 134 | { |
| 135 | struct key_table *table; |
| 136 | |
| 137 | table = key_bindings_get_table(name, 0); |
| 138 | if (table != NULL) { |
| 139 | RB_REMOVE(key_tables, &key_tables, table); |
| 140 | key_bindings_unref_table(table); |
| 141 | } |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | key_bindings_init(void) |
| 146 | { |
nicm | 1d1208e | 2015-04-25 18:49:01 +0000 | [diff] [blame] | 147 | static const char *defaults[] = { |
nicm | 45dfc5a | 2014-10-20 22:29:25 +0000 | [diff] [blame] | 148 | "bind C-b send-prefix", |
| 149 | "bind C-o rotate-window", |
| 150 | "bind C-z suspend-client", |
| 151 | "bind Space next-layout", |
| 152 | "bind ! break-pane", |
| 153 | "bind '\"' split-window", |
| 154 | "bind '#' list-buffers", |
| 155 | "bind '$' command-prompt -I'#S' \"rename-session '%%'\"", |
| 156 | "bind % split-window -h", |
| 157 | "bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window", |
| 158 | "bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"", |
| 159 | "bind ( switch-client -p", |
| 160 | "bind ) switch-client -n", |
| 161 | "bind , command-prompt -I'#W' \"rename-window '%%'\"", |
| 162 | "bind - delete-buffer", |
| 163 | "bind . command-prompt \"move-window -t '%%'\"", |
| 164 | "bind 0 select-window -t:0", |
| 165 | "bind 1 select-window -t:1", |
| 166 | "bind 2 select-window -t:2", |
| 167 | "bind 3 select-window -t:3", |
| 168 | "bind 4 select-window -t:4", |
| 169 | "bind 5 select-window -t:5", |
| 170 | "bind 6 select-window -t:6", |
| 171 | "bind 7 select-window -t:7", |
| 172 | "bind 8 select-window -t:8", |
| 173 | "bind 9 select-window -t:9", |
| 174 | "bind : command-prompt", |
| 175 | "bind \\; last-pane", |
| 176 | "bind = choose-buffer", |
| 177 | "bind ? list-keys", |
| 178 | "bind D choose-client", |
| 179 | "bind L switch-client -l", |
| 180 | "bind [ copy-mode", |
| 181 | "bind ] paste-buffer", |
| 182 | "bind c new-window", |
| 183 | "bind d detach-client", |
| 184 | "bind f command-prompt \"find-window '%%'\"", |
| 185 | "bind i display-message", |
| 186 | "bind l last-window", |
| 187 | "bind n next-window", |
| 188 | "bind o select-pane -t:.+", |
| 189 | "bind p previous-window", |
| 190 | "bind q display-panes", |
| 191 | "bind r refresh-client", |
| 192 | "bind s choose-tree", |
| 193 | "bind t clock-mode", |
| 194 | "bind w choose-window", |
| 195 | "bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane", |
| 196 | "bind z resize-pane -Z", |
| 197 | "bind { swap-pane -U", |
| 198 | "bind } swap-pane -D", |
| 199 | "bind '~' show-messages", |
| 200 | "bind PPage copy-mode -u", |
| 201 | "bind -r Up select-pane -U", |
| 202 | "bind -r Down select-pane -D", |
| 203 | "bind -r Left select-pane -L", |
| 204 | "bind -r Right select-pane -R", |
| 205 | "bind M-1 select-layout even-horizontal", |
| 206 | "bind M-2 select-layout even-vertical", |
| 207 | "bind M-3 select-layout main-horizontal", |
| 208 | "bind M-4 select-layout main-vertical", |
| 209 | "bind M-5 select-layout tiled", |
| 210 | "bind M-n next-window -a", |
| 211 | "bind M-o rotate-window -D", |
| 212 | "bind M-p previous-window -a", |
| 213 | "bind -r M-Up resize-pane -U 5", |
| 214 | "bind -r M-Down resize-pane -D 5", |
| 215 | "bind -r M-Left resize-pane -L 5", |
| 216 | "bind -r M-Right resize-pane -R 5", |
| 217 | "bind -r C-Up resize-pane -U", |
| 218 | "bind -r C-Down resize-pane -D", |
| 219 | "bind -r C-Left resize-pane -L", |
| 220 | "bind -r C-Right resize-pane -R", |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 221 | "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M", |
| 222 | "bind -n MouseDrag1Border resize-pane -M", |
| 223 | "bind -n MouseDown1Status select-window -t=", |
nicm | d133705 | 2015-04-21 15:34:32 +0000 | [diff] [blame] | 224 | "bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'", |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 225 | }; |
| 226 | u_int i; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 227 | struct cmd_list *cmdlist; |
nicm | 1d1208e | 2015-04-25 18:49:01 +0000 | [diff] [blame] | 228 | char *cause; |
nicm | 45dfc5a | 2014-10-20 22:29:25 +0000 | [diff] [blame] | 229 | int error; |
| 230 | struct cmd_q *cmdq; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 231 | |
nicm | abfb965 | 2014-10-22 23:18:53 +0000 | [diff] [blame] | 232 | cmdq = cmdq_new(NULL); |
nicm | 45dfc5a | 2014-10-20 22:29:25 +0000 | [diff] [blame] | 233 | for (i = 0; i < nitems(defaults); i++) { |
| 234 | error = cmd_string_parse(defaults[i], &cmdlist, |
| 235 | "<default-keys>", i, &cause); |
| 236 | if (error != 0) |
| 237 | fatalx("bad default key"); |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 238 | cmdq_run(cmdq, cmdlist, NULL); |
| 239 | cmd_list_free (cmdlist); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 240 | } |
nicm | abfb965 | 2014-10-22 23:18:53 +0000 | [diff] [blame] | 241 | cmdq_free(cmdq); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 244 | void |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 245 | key_bindings_dispatch(struct key_binding *bd, struct client *c, |
| 246 | struct mouse_event *m) |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 247 | { |
Nicholas Marriott | bb53c20 | 2010-02-06 22:55:31 +0000 | [diff] [blame] | 248 | struct cmd *cmd; |
| 249 | int readonly; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 250 | |
Nicholas Marriott | bb53c20 | 2010-02-06 22:55:31 +0000 | [diff] [blame] | 251 | readonly = 1; |
Nicholas Marriott | 42e2413 | 2010-06-26 18:20:53 +0000 | [diff] [blame] | 252 | TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) { |
Nicholas Marriott | bb53c20 | 2010-02-06 22:55:31 +0000 | [diff] [blame] | 253 | if (!(cmd->entry->flags & CMD_READONLY)) |
| 254 | readonly = 0; |
| 255 | } |
Nicholas Marriott | 20636d9 | 2013-03-24 09:54:10 +0000 | [diff] [blame] | 256 | if (!readonly && (c->flags & CLIENT_READONLY)) { |
nicm | 2740490 | 2014-04-17 07:55:43 +0000 | [diff] [blame] | 257 | cmdq_error(c->cmdq, "client is read-only"); |
Nicholas Marriott | bb53c20 | 2010-02-06 22:55:31 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 261 | cmdq_run(c->cmdq, bd->cmdlist, m); |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 262 | } |