1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <libgen.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "tmux.h" 27 28 void name_time_callback(int, short, void *); 29 int name_time_expired(struct window *, struct timeval *); 30 31 void 32 name_time_callback(unused int fd, unused short events, void *arg) 33 { 34 struct window *w = arg; 35 36 /* The event loop will call check_window_name for us on the way out. */ 37 log_debug("@%u name timer expired", w->id); 38 } 39 40 int 41 name_time_expired(struct window *w, struct timeval *tv) 42 { 43 struct timeval offset; 44 45 timersub(tv, &w->name_time, &offset); 46 if (offset.tv_sec != 0 || offset.tv_usec > NAME_INTERVAL) 47 return (0); 48 return (NAME_INTERVAL - offset.tv_usec); 49 } 50 51 void 52 check_window_name(struct window *w) 53 { 54 struct timeval tv, next; 55 char *name; 56 int left; 57 58 if (w->active == NULL) 59 return; 60 61 if (!options_get_number(&w->options, "automatic-rename")) 62 return; 63 64 if (~w->active->flags & PANE_CHANGED) { 65 log_debug("@%u active pane not changed", w->id); 66 return; 67 } 68 log_debug("@%u active pane changed", w->id); 69 70 gettimeofday(&tv, NULL); 71 left = name_time_expired(w, &tv); 72 if (left != 0) { 73 if (!event_initialized(&w->name_event)) 74 evtimer_set(&w->name_event, name_time_callback, w); 75 if (!evtimer_pending(&w->name_event, NULL)) { 76 log_debug("@%u name timer queued (%d left)", w->id, left); 77 timerclear(&next); 78 next.tv_usec = left; 79 event_add(&w->name_event, &next); 80 } else 81 log_debug("@%u name timer already queued (%d left)", w->id, left); 82 return; 83 } 84 memcpy(&w->name_time, &tv, sizeof w->name_time); 85 if (event_initialized(&w->name_event)) 86 evtimer_del(&w->name_event); 87 88 w->active->flags &= ~PANE_CHANGED; 89 90 name = format_window_name(w); 91 if (strcmp(name, w->name) != 0) { 92 log_debug("@%u new name %s (was %s)", w->id, name, w->name); 93 window_set_name(w, name); 94 server_status_window(w); 95 } else 96 log_debug("@%u name not changed (still %s)", w->id, w->name); 97 98 free(name); 99 } 100 101 char * 102 default_window_name(struct window *w) 103 { 104 char *cmd, *s; 105 106 cmd = cmd_stringify_argv(w->active->argc, w->active->argv); 107 if (cmd != NULL && *cmd != '\0') 108 s = parse_window_name(cmd); 109 else 110 s = parse_window_name(w->active->shell); 111 free(cmd); 112 return (s); 113 } 114 115 char * 116 format_window_name(struct window *w) 117 { 118 struct format_tree *ft; 119 char *fmt, *name; 120 121 ft = format_create(); 122 format_defaults_window(ft, w); 123 format_defaults_pane(ft, w->active); 124 125 fmt = options_get_string(&w->options, "automatic-rename-format"); 126 name = format_expand(ft, fmt); 127 128 format_free(ft); 129 return (name); 130 } 131 132 char * 133 parse_window_name(const char *in) 134 { 135 char *copy, *name, *ptr; 136 137 name = copy = xstrdup(in); 138 if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0) 139 name = name + (sizeof "exec ") - 1; 140 141 while (*name == ' ' || *name == '-') 142 name++; 143 if ((ptr = strchr(name, ' ')) != NULL) 144 *ptr = '\0'; 145 146 if (*name != '\0') { 147 ptr = name + strlen(name) - 1; 148 while (ptr > name && !isalnum((u_char)*ptr)) 149 *ptr-- = '\0'; 150 } 151 152 if (*name == '/') 153 name = basename(name); 154 name = xstrdup(name); 155 free(copy); 156 return (name); 157 } 158