tmate/tmate-msg.c

97 lines
2.1 KiB
C
Raw Normal View History

2013-06-13 01:40:30 +02:00
#include <time.h>
#include "tmate.h"
void status_message_callback(int, short, void *);
/* Very similar to status.c:status_message_set */
static void tmate_status_message_client(struct client *c, const char *message)
{
struct timeval tv;
2015-12-23 11:41:41 +01:00
struct message_entry *msg, *msg1;
2013-06-13 01:40:30 +02:00
int delay;
2016-01-01 22:44:01 +01:00
u_int limit;
2013-06-13 01:40:30 +02:00
2015-12-24 05:19:55 +01:00
limit = options_get_number(global_options, "message-limit");
2016-01-01 01:55:12 +01:00
delay = options_get_number(c->session ? c->session->options : global_s_options,
"tmate-display-time");
2015-12-24 05:19:55 +01:00
2013-06-13 01:40:30 +02:00
status_prompt_clear(c);
status_message_clear(c);
2013-06-13 10:31:25 +02:00
xasprintf(&c->message_string, "[tmate] %s", message);
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
msg = xcalloc(1, sizeof *msg);
2013-06-13 01:40:30 +02:00
msg->msg_time = time(NULL);
2015-12-23 11:41:41 +01:00
msg->msg_num = c->message_next++;
2013-06-13 01:40:30 +02:00
msg->msg = xstrdup(c->message_string);
2015-12-23 11:41:41 +01:00
TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2016-01-01 01:55:12 +01:00
if (msg->msg_num + limit >= c->message_next)
break;
2015-12-23 11:41:41 +01:00
free(msg->msg);
TAILQ_REMOVE(&c->message_log, msg, entry);
free(msg);
2013-06-13 01:40:30 +02:00
}
2015-12-23 11:41:41 +01:00
if (delay > 0) {
tv.tv_sec = delay / 1000;
tv.tv_usec = (delay % 1000) * 1000L;
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
if (event_initialized(&c->message_timer))
evtimer_del(&c->message_timer);
evtimer_set(&c->message_timer, status_message_callback, c);
evtimer_add(&c->message_timer, &tv);
}
2013-06-13 01:40:30 +02:00
c->flags |= CLIENT_STATUS | CLIENT_FORCE_STATUS;
recalculate_sizes();
}
2019-11-06 03:32:21 +01:00
static void tmate_status_message_session(const char *message)
{
if (tmate_foreground)
return;
struct session *s;
s = RB_MIN(sessions, &sessions);
if (!s) {
cfg_add_cause("%s", message);
return;
}
struct window_pane *wp;
wp = s->curw->window->active;
if (wp->mode == &window_copy_mode)
window_copy_add(wp, "%s", message);
}
2013-06-13 01:40:30 +02:00
void __tmate_status_message(const char *fmt, va_list ap)
{
struct client *c;
char *message;
xvasprintf(&message, fmt, ap);
2019-11-03 11:11:37 +01:00
tmate_info("%s", message);
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
TAILQ_FOREACH(c, &clients, entry) {
2013-06-13 01:40:30 +02:00
if (c && !(c->flags & CLIENT_READONLY))
tmate_status_message_client(c, message);
}
2019-11-06 03:32:21 +01:00
tmate_status_message_session(message);
2013-06-13 01:40:30 +02:00
free(message);
}
void tmate_status_message(const char *fmt, ...)
2013-06-13 01:40:30 +02:00
{
va_list ap;
va_start(ap, fmt);
__tmate_status_message(fmt, ap);
va_end(ap);
}