/* $OpenBSD$ */
/*
* Copyright (c) 2009 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 <event.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <paths.h>
#include <unistd.h>
#include "tmux.h"
void server_client_check_mouse(struct client *, struct window_pane *,
struct mouse_event *);
void server_client_repeat_timer(int, short, void *);
void server_client_check_exit(struct client *);
void server_client_check_redraw(struct client *);
void server_client_set_title(struct client *);
void server_client_reset_state(struct client *);
int server_client_msg_dispatch(struct client *);
void server_client_msg_command(struct client *, struct msg_command_data *);
void server_client_msg_identify(
struct client *, struct msg_identify_data *, int);
void server_client_msg_shell(struct client *);
void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
/* Create a new client. */
void
server_client_create(int fd)
{
struct client *c;
u_int i;
setblocking(fd, 0);
c = xcalloc(1, sizeof *c);
c->references = 0;
imsg_init(&c->ibuf, fd);
server_update_event(c);
if (gettimeofday(&c->creation_time, NULL) != 0)
fatal("gettimeofday failed");
memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
c->stdin_data = evbuffer_new ();
c->stdout_data = evbuffer_new ();
c->stderr_data = evbuffer_new ();
c->tty.fd = -1;
c->title = NULL;
c->session = NULL;
c->last_session = NULL;
c->tty.sx = 80;
c->tty.sy = 24;
screen_init(&c->status, c->tty.sx, 1, 0);
RB_INIT(&c->status_new);
RB_INIT(&c->status_old);
c->message_string = NULL;
ARRAY_INIT(&c->message_log);
c->prompt_string = NULL;
c->prompt_buffer = NULL;
c->prompt_index = 0;
c->last_mouse.b = MOUSE_UP;
c->last_mouse.x = c->last_mouse.y = -1;
evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
if (ARRAY_ITEM(&clients, i) == NULL) {
ARRAY_SET(&clients, i, c);
return;
}
}
ARRAY_ADD(&clients, c);
log_debug("new client %d", fd);
}
/* Open client terminal if needed. */
int
server_client_open(struct client *c, struct session *s, char **cause)
{
struct options *oo = s != NULL ? &s->options : &global_s_options;
char *overrides;
if (c->flags & CLIENT_CONTROL)
return (0);
if (!(c->flags & CLIENT_TERMINAL)) {
*cause = xstrdup ("not a terminal");
return (-1);
}
overrides = options_get_string(oo, "terminal-overrides");
if (tty_open(&c->tty, overrides, cause) != 0)
return (-1);
return (0);
}
/* Lost a client. */
void
server_client_lost(struct client *c)
{
struct message_entry *msg;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
if (ARRAY_ITEM(&clients, i) == c)
ARRAY_SET(&clients, i, NULL);
}
log_debug("lost client %d", c->ibuf.fd);
/*
* If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
* and tty_free might close an unrelated fd.
*/