cmd-send-keys.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Send keys to client.
  23. */
  24. enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmd_q *);
  25. const struct cmd_entry cmd_send_keys_entry = {
  26. .name = "send-keys",
  27. .alias = "send",
  28. .args = { "lRMt:", 0, -1 },
  29. .usage = "[-lRM] " CMD_TARGET_PANE_USAGE " key ...",
  30. .tflag = CMD_PANE,
  31. .flags = 0,
  32. .exec = cmd_send_keys_exec
  33. };
  34. const struct cmd_entry cmd_send_prefix_entry = {
  35. .name = "send-prefix",
  36. .alias = NULL,
  37. .args = { "2t:", 0, 0 },
  38. .usage = "[-2] " CMD_TARGET_PANE_USAGE,
  39. .tflag = CMD_PANE,
  40. .flags = 0,
  41. .exec = cmd_send_keys_exec
  42. };
  43. enum cmd_retval
  44. cmd_send_keys_exec(struct cmd *self, struct cmd_q *cmdq)
  45. {
  46. struct args *args = self->args;
  47. struct window_pane *wp = cmdq->state.tflag.wp;
  48. struct session *s = cmdq->state.tflag.s;
  49. struct mouse_event *m = &cmdq->item->mouse;
  50. const u_char *keystr;
  51. int i, literal;
  52. key_code key;
  53. if (args_has(args, 'M')) {
  54. wp = cmd_mouse_pane(m, &s, NULL);
  55. if (wp == NULL) {
  56. cmdq_error(cmdq, "no mouse target");
  57. return (CMD_RETURN_ERROR);
  58. }
  59. window_pane_key(wp, NULL, s, m->key, m);
  60. return (CMD_RETURN_NORMAL);
  61. }
  62. if (self->entry == &cmd_send_prefix_entry) {
  63. if (args_has(args, '2'))
  64. key = options_get_number(s->options, "prefix2");
  65. else
  66. key = options_get_number(s->options, "prefix");
  67. window_pane_key(wp, NULL, s, key, NULL);
  68. return (CMD_RETURN_NORMAL);
  69. }
  70. if (args_has(args, 'R'))
  71. input_reset(wp, 1);
  72. for (i = 0; i < args->argc; i++) {
  73. literal = args_has(args, 'l');
  74. if (!literal) {
  75. key = key_string_lookup_string(args->argv[i]);
  76. if (key != KEYC_NONE && key != KEYC_UNKNOWN)
  77. window_pane_key(wp, NULL, s, key, NULL);
  78. else
  79. literal = 1;
  80. }
  81. if (literal) {
  82. for (keystr = args->argv[i]; *keystr != '\0'; keystr++)
  83. window_pane_key(wp, NULL, s, *keystr, NULL);
  84. }
  85. }
  86. return (CMD_RETURN_NORMAL);
  87. }