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 27 RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp); 28 RB_GENERATE(key_tables, key_table, entry, key_table_cmp); 29 struct key_tables key_tables = RB_INITIALIZER(&key_tables); 30 31 int 32 key_table_cmp(struct key_table *e1, struct key_table *e2) 33 { 34 return (strcmp(e1->name, e2->name)); 35 } 36 37 int 38 key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2) 39 { 40 return (bd1->key - bd2->key); 41 } 42 43 struct key_table * 44 key_bindings_get_table(const char *name, int create) 45 { 46 struct key_table table_find, *table; 47 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); 61 } 62 63 void 64 key_bindings_unref_table(struct key_table *table) 65 { 66 struct key_binding *bd; 67 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(__UNCONST(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 } 98 99 bd = xmalloc(sizeof *bd); 100 bd->key = key; 101 RB_INSERT(key_bindings, &table->key_bindings, bd); 102 103 bd->can_repeat = can_repeat; 104 bd->cmdlist = cmdlist; 105 } 106 107 void 108 key_bindings_remove(const char *name, int key) 109 { 110 struct key_table *table; 111 struct key_binding bd_find, *bd; 112 113 table = key_bindings_get_table(name, 0); 114 if (table == NULL) 115 return; 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); 123 cmd_list_free(bd->cmdlist); 124 free(bd); 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 } 142 } 143 144 void 145 key_bindings_init(void) 146 { 147 static const char *defaults[] = { 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 M select-pane -M", 181 "bind [ copy-mode", 182 "bind ] paste-buffer", 183 "bind c new-window", 184 "bind d detach-client", 185 "bind f command-prompt \"find-window '%%'\"", 186 "bind i display-message", 187 "bind l last-window", 188 "bind m select-pane -m", 189 "bind n next-window", 190 "bind o select-pane -t:.+", 191 "bind p previous-window", 192 "bind q display-panes", 193 "bind r refresh-client", 194 "bind s choose-tree", 195 "bind t clock-mode", 196 "bind w choose-window", 197 "bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane", 198 "bind z resize-pane -Z", 199 "bind { swap-pane -U", 200 "bind } swap-pane -D", 201 "bind '~' show-messages", 202 "bind PPage copy-mode -u", 203 "bind -r Up select-pane -U", 204 "bind -r Down select-pane -D", 205 "bind -r Left select-pane -L", 206 "bind -r Right select-pane -R", 207 "bind M-1 select-layout even-horizontal", 208 "bind M-2 select-layout even-vertical", 209 "bind M-3 select-layout main-horizontal", 210 "bind M-4 select-layout main-vertical", 211 "bind M-5 select-layout tiled", 212 "bind M-n next-window -a", 213 "bind M-o rotate-window -D", 214 "bind M-p previous-window -a", 215 "bind -r M-Up resize-pane -U 5", 216 "bind -r M-Down resize-pane -D 5", 217 "bind -r M-Left resize-pane -L 5", 218 "bind -r M-Right resize-pane -R 5", 219 "bind -r C-Up resize-pane -U", 220 "bind -r C-Down resize-pane -D", 221 "bind -r C-Left resize-pane -L", 222 "bind -r C-Right resize-pane -R", 223 "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M", 224 "bind -n MouseDrag1Border resize-pane -M", 225 "bind -n MouseDown1Status select-window -t=", 226 "bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'", 227 "bind -n MouseDown3Pane select-pane -mt=", 228 }; 229 u_int i; 230 struct cmd_list *cmdlist; 231 char *cause; 232 int error; 233 struct cmd_q *cmdq; 234 235 cmdq = cmdq_new(NULL); 236 for (i = 0; i < nitems(defaults); i++) { 237 error = cmd_string_parse(defaults[i], &cmdlist, 238 "<default-keys>", i, &cause); 239 if (error != 0) 240 fatalx("bad default key"); 241 cmdq_run(cmdq, cmdlist, NULL); 242 cmd_list_free(cmdlist); 243 } 244 cmdq_free(cmdq); 245 } 246 247 void 248 key_bindings_dispatch(struct key_binding *bd, struct client *c, 249 struct mouse_event *m) 250 { 251 struct cmd *cmd; 252 int readonly; 253 254 readonly = 1; 255 TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) { 256 if (!(cmd->entry->flags & CMD_READONLY)) 257 readonly = 0; 258 } 259 if (!readonly && (c->flags & CLIENT_READONLY)) { 260 cmdq_error(c->cmdq, "client is read-only"); 261 return; 262 } 263 264 cmdq_run(c->cmdq, bd->cmdlist, m); 265 } 266