cmd-switch-client.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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. * Switch client to a different session.
  23. */
  24. enum cmd_retval cmd_switch_client_exec(struct cmd *, struct cmd_q *);
  25. const struct cmd_entry cmd_switch_client_entry = {
  26. .name = "switch-client",
  27. .alias = "switchc",
  28. .args = { "lc:Enpt:rT:", 0, 0 },
  29. .usage = "[-Elnpr] [-c target-client] [-t target-session] "
  30. "[-T key-table]",
  31. .cflag = CMD_CLIENT,
  32. .tflag = CMD_SESSION_WITHPANE,
  33. .flags = CMD_READONLY,
  34. .exec = cmd_switch_client_exec
  35. };
  36. enum cmd_retval
  37. cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq)
  38. {
  39. struct args *args = self->args;
  40. struct cmd_state *state = &cmdq->state;
  41. struct client *c = state->c;
  42. struct session *s = cmdq->state.tflag.s;
  43. struct window_pane *wp;
  44. const char *tablename, *update;
  45. struct key_table *table;
  46. if (args_has(args, 'r'))
  47. c->flags ^= CLIENT_READONLY;
  48. tablename = args_get(args, 'T');
  49. if (tablename != NULL) {
  50. table = key_bindings_get_table(tablename, 0);
  51. if (table == NULL) {
  52. cmdq_error(cmdq, "table %s doesn't exist", tablename);
  53. return (CMD_RETURN_ERROR);
  54. }
  55. table->references++;
  56. key_bindings_unref_table(c->keytable);
  57. c->keytable = table;
  58. return (CMD_RETURN_NORMAL);
  59. }
  60. if (args_has(args, 'n')) {
  61. if ((s = session_next_session(c->session)) == NULL) {
  62. cmdq_error(cmdq, "can't find next session");
  63. return (CMD_RETURN_ERROR);
  64. }
  65. } else if (args_has(args, 'p')) {
  66. if ((s = session_previous_session(c->session)) == NULL) {
  67. cmdq_error(cmdq, "can't find previous session");
  68. return (CMD_RETURN_ERROR);
  69. }
  70. } else if (args_has(args, 'l')) {
  71. if (c->last_session != NULL && session_alive(c->last_session))
  72. s = c->last_session;
  73. else
  74. s = NULL;
  75. if (s == NULL) {
  76. cmdq_error(cmdq, "can't find last session");
  77. return (CMD_RETURN_ERROR);
  78. }
  79. } else {
  80. if (cmdq->client == NULL)
  81. return (CMD_RETURN_NORMAL);
  82. if (state->tflag.wl != NULL) {
  83. wp = state->tflag.wp;
  84. if (wp != NULL)
  85. window_set_active_pane(wp->window, wp);
  86. session_set_current(s, state->tflag.wl);
  87. }
  88. }
  89. if (c != NULL && !args_has(args, 'E')) {
  90. update = options_get_string(s->options, "update-environment");
  91. environ_update(update, c->environ, s->environ);
  92. }
  93. if (c->session != NULL && c->session != s)
  94. c->last_session = c->session;
  95. c->session = s;
  96. server_client_set_key_table(c, NULL);
  97. status_timer_start(c);
  98. session_update_activity(s, NULL);
  99. gettimeofday(&s->last_attached_time, NULL);
  100. recalculate_sizes();
  101. server_check_unattached();
  102. server_redraw_client(c);
  103. s->curw->flags &= ~WINLINK_ALERTFLAGS;
  104. alerts_check_session(s);
  105. return (CMD_RETURN_NORMAL);
  106. }