2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
struct key_bindings key_bindings;
|
2009-07-12 19:33:18 +02:00
|
|
|
struct key_bindings dead_key_bindings;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
|
|
|
|
{
|
2009-07-24 16:52:47 +02:00
|
|
|
int key1, key2;
|
|
|
|
|
|
|
|
key1 = bd1->key & ~KEYC_PREFIX;
|
|
|
|
key2 = bd2->key & ~KEYC_PREFIX;
|
|
|
|
if (key1 != key2)
|
|
|
|
return (key1 - key2);
|
|
|
|
|
|
|
|
if (bd1->key & KEYC_PREFIX && !(bd2->key & KEYC_PREFIX))
|
|
|
|
return (-1);
|
|
|
|
if (bd2->key & KEYC_PREFIX && !(bd1->key & KEYC_PREFIX))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct key_binding *
|
|
|
|
key_bindings_lookup(int key)
|
|
|
|
{
|
|
|
|
struct key_binding bd;
|
|
|
|
|
|
|
|
bd.key = key;
|
2012-01-21 12:12:13 +01:00
|
|
|
return (RB_FIND(key_bindings, &key_bindings, &bd));
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist)
|
|
|
|
{
|
|
|
|
struct key_binding *bd;
|
|
|
|
|
2009-07-12 19:33:18 +02:00
|
|
|
key_bindings_remove(key);
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2009-07-12 19:33:18 +02:00
|
|
|
bd = xmalloc(sizeof *bd);
|
|
|
|
bd->key = key;
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_INSERT(key_bindings, &key_bindings, bd);
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2009-06-02 00:58:49 +02:00
|
|
|
bd->can_repeat = can_repeat;
|
|
|
|
bd->cmdlist = cmdlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_remove(int key)
|
|
|
|
{
|
|
|
|
struct key_binding *bd;
|
|
|
|
|
|
|
|
if ((bd = key_bindings_lookup(key)) == NULL)
|
|
|
|
return;
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_REMOVE(key_bindings, &key_bindings, bd);
|
|
|
|
RB_INSERT(key_bindings, &dead_key_bindings, bd);
|
2009-07-12 19:33:18 +02:00
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2009-07-12 19:33:18 +02:00
|
|
|
void
|
|
|
|
key_bindings_clean(void)
|
|
|
|
{
|
|
|
|
struct key_binding *bd;
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
while (!RB_EMPTY(&dead_key_bindings)) {
|
|
|
|
bd = RB_ROOT(&dead_key_bindings);
|
|
|
|
RB_REMOVE(key_bindings, &dead_key_bindings, bd);
|
2009-07-12 19:33:18 +02:00
|
|
|
cmd_list_free(bd->cmdlist);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(bd);
|
2009-07-12 19:33:18 +02:00
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
int key;
|
|
|
|
int can_repeat;
|
|
|
|
const struct cmd_entry *entry;
|
|
|
|
} table[] = {
|
|
|
|
{ ' ', 0, &cmd_next_layout_entry },
|
|
|
|
{ '!', 0, &cmd_break_pane_entry },
|
2009-12-03 23:50:09 +01:00
|
|
|
{ '"', 0, &cmd_split_window_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '#', 0, &cmd_list_buffers_entry },
|
2011-07-03 20:18:15 +02:00
|
|
|
{ '$', 0, &cmd_command_prompt_entry },
|
2009-12-03 23:50:09 +01:00
|
|
|
{ '%', 0, &cmd_split_window_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '&', 0, &cmd_confirm_before_entry },
|
2010-09-09 00:02:28 +02:00
|
|
|
{ '(', 0, &cmd_switch_client_entry },
|
|
|
|
{ ')', 0, &cmd_switch_client_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ ',', 0, &cmd_command_prompt_entry },
|
|
|
|
{ '-', 0, &cmd_delete_buffer_entry },
|
|
|
|
{ '.', 0, &cmd_command_prompt_entry },
|
|
|
|
{ '0', 0, &cmd_select_window_entry },
|
|
|
|
{ '1', 0, &cmd_select_window_entry },
|
|
|
|
{ '2', 0, &cmd_select_window_entry },
|
|
|
|
{ '3', 0, &cmd_select_window_entry },
|
|
|
|
{ '4', 0, &cmd_select_window_entry },
|
|
|
|
{ '5', 0, &cmd_select_window_entry },
|
|
|
|
{ '6', 0, &cmd_select_window_entry },
|
|
|
|
{ '7', 0, &cmd_select_window_entry },
|
|
|
|
{ '8', 0, &cmd_select_window_entry },
|
|
|
|
{ '9', 0, &cmd_select_window_entry },
|
|
|
|
{ ':', 0, &cmd_command_prompt_entry },
|
2010-10-23 15:04:34 +02:00
|
|
|
{ ';', 0, &cmd_last_pane_entry },
|
2010-06-21 23:44:09 +02:00
|
|
|
{ '=', 0, &cmd_choose_buffer_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '?', 0, &cmd_list_keys_entry },
|
2009-11-18 14:16:33 +01:00
|
|
|
{ 'D', 0, &cmd_choose_client_entry },
|
2010-12-11 19:39:25 +01:00
|
|
|
{ 'L', 0, &cmd_switch_client_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '[', 0, &cmd_copy_mode_entry },
|
2010-05-06 01:24:23 +02:00
|
|
|
{ '\'', 0, &cmd_command_prompt_entry },
|
2009-11-18 14:16:33 +01:00
|
|
|
{ '\002', /* C-b */ 0, &cmd_send_prefix_entry },
|
|
|
|
{ '\017', /* C-o */ 0, &cmd_rotate_window_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '\032', /* C-z */ 0, &cmd_suspend_client_entry },
|
|
|
|
{ ']', 0, &cmd_paste_buffer_entry },
|
|
|
|
{ 'c', 0, &cmd_new_window_entry },
|
|
|
|
{ 'd', 0, &cmd_detach_client_entry },
|
|
|
|
{ 'f', 0, &cmd_command_prompt_entry },
|
2009-07-17 20:45:08 +02:00
|
|
|
{ 'i', 0, &cmd_display_message_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ 'l', 0, &cmd_last_window_entry },
|
|
|
|
{ 'n', 0, &cmd_next_window_entry },
|
2010-03-22 20:07:52 +01:00
|
|
|
{ 'o', 0, &cmd_select_pane_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ 'p', 0, &cmd_previous_window_entry },
|
2009-08-31 22:46:19 +02:00
|
|
|
{ 'q', 0, &cmd_display_panes_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ 'r', 0, &cmd_refresh_client_entry },
|
2012-07-08 18:04:38 +02:00
|
|
|
{ 's', 0, &cmd_choose_tree_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ 't', 0, &cmd_clock_mode_entry },
|
|
|
|
{ 'w', 0, &cmd_choose_window_entry },
|
|
|
|
{ 'x', 0, &cmd_confirm_before_entry },
|
2013-03-24 10:57:59 +01:00
|
|
|
{ 'z', 0, &cmd_resize_pane_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
{ '{', 0, &cmd_swap_pane_entry },
|
|
|
|
{ '}', 0, &cmd_swap_pane_entry },
|
2009-11-18 14:16:33 +01:00
|
|
|
{ '~', 0, &cmd_show_messages_entry },
|
2009-07-21 19:57:29 +02:00
|
|
|
{ '1' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '2' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '3' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '4' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
2010-04-25 22:28:13 +02:00
|
|
|
{ '5' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
2009-10-06 09:19:32 +02:00
|
|
|
{ KEYC_PPAGE, 0, &cmd_copy_mode_entry },
|
2009-07-21 19:57:29 +02:00
|
|
|
{ 'n' | KEYC_ESCAPE, 0, &cmd_next_window_entry },
|
2009-11-18 14:16:33 +01:00
|
|
|
{ 'o' | KEYC_ESCAPE, 0, &cmd_rotate_window_entry },
|
2009-07-21 19:57:29 +02:00
|
|
|
{ 'p' | KEYC_ESCAPE, 0, &cmd_previous_window_entry },
|
2010-03-22 20:07:52 +01:00
|
|
|
{ KEYC_UP, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_DOWN, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_LEFT, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_RIGHT, 1, &cmd_select_pane_entry },
|
2009-07-21 19:57:29 +02:00
|
|
|
{ KEYC_UP | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_DOWN | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_LEFT | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_RIGHT | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_UP | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2009-12-03 23:50:09 +01:00
|
|
|
{ KEYC_DOWN | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2009-07-21 19:57:29 +02:00
|
|
|
{ KEYC_LEFT | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_RIGHT | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2009-06-02 00:58:49 +02:00
|
|
|
};
|
|
|
|
u_int i;
|
|
|
|
struct cmd *cmd;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_INIT(&key_bindings);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nitems(table); i++) {
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdlist = xcalloc(1, sizeof *cmdlist);
|
2010-06-26 20:20:53 +02:00
|
|
|
cmdlist->references = 1;
|
2013-03-24 10:54:10 +01:00
|
|
|
TAILQ_INIT(&cmdlist->list);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cmd = xcalloc(1, sizeof *cmd);
|
2009-06-02 00:58:49 +02:00
|
|
|
cmd->entry = table[i].entry;
|
2011-01-04 01:42:46 +01:00
|
|
|
if (cmd->entry->key_binding != NULL)
|
|
|
|
cmd->entry->key_binding(cmd, table[i].key);
|
|
|
|
else
|
|
|
|
cmd->args = args_create(0);
|
2010-06-26 20:20:53 +02:00
|
|
|
TAILQ_INSERT_HEAD(&cmdlist->list, cmd, qentry);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2009-07-24 16:52:47 +02:00
|
|
|
key_bindings_add(
|
|
|
|
table[i].key | KEYC_PREFIX, table[i].can_repeat, cmdlist);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_dispatch(struct key_binding *bd, struct client *c)
|
|
|
|
{
|
2010-02-06 23:55:31 +01:00
|
|
|
struct cmd *cmd;
|
|
|
|
int readonly;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2010-02-06 23:55:31 +01:00
|
|
|
readonly = 1;
|
2010-06-26 20:20:53 +02:00
|
|
|
TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
|
2010-02-06 23:55:31 +01:00
|
|
|
if (!(cmd->entry->flags & CMD_READONLY))
|
|
|
|
readonly = 0;
|
|
|
|
}
|
2013-03-24 10:54:10 +01:00
|
|
|
if (!readonly && (c->flags & CLIENT_READONLY)) {
|
|
|
|
cmdq_info(c->cmdq, "client is read-only");
|
2010-02-06 23:55:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdq_run(c->cmdq, bd->cmdlist);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|