From 42ad454f4f79dbb10530f1c67b4b541e362155b8 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 7 Feb 2021 19:59:20 +0000 Subject: [PATCH] Move all window-title management into Terminal. Previously, window title management happened in a bipartisan sort of way: front ends would choose their initial window title once they knew what host name they were connecting to, but then Terminal would override that later if the server set the window title by escape sequences. Now it's all done the same way round: the Terminal object is always where titles are invented, and they only propagate in one direction, from the Terminal to the TermWin. This allows us to avoid duplicating in multiple front ends the logic for what the initial window title should be. The frontend just has to make one initial call to term_setup_window_titles, to tell the terminal what hostname should go in the default title (if the Conf doesn't override even that). Thereafter, all it has to do is respond to the TermWin title-setting methods. Similarly, the logic that handles window-title changes as a result of the Change Settings dialog is also centralised into terminal.c. This involved introducing an extra term_pre_reconfig() call that each frontend can call to modify the Conf that will be used for the GUI configurer; that's where the code now lives that copies the current window title into there. (This also means that GTK PuTTY now behaves consistently with Windows PuTTY on that point; GTK's previous behaviour was less well thought out.) It also means there's no longer any need for Terminal to talk to the front end when a remote query wants to _find out_ the window title: the Terminal knows the answer already. So TermWin's get_title method can go. --- fuzzterm.c | 2 -- putty.h | 5 ++-- terminal.c | 60 ++++++++++++++++++++++++++++++++++++++++++++---- terminal.h | 2 ++ unix/gtkwin.c | 40 +++----------------------------- unix/unix.h | 3 --- unix/uxpterm.c | 5 ---- unix/uxputty.c | 5 ---- windows/window.c | 39 ++----------------------------- 9 files changed, 65 insertions(+), 96 deletions(-) diff --git a/fuzzterm.c b/fuzzterm.c index c1fc2b3c..4a6b745c 100644 --- a/fuzzterm.c +++ b/fuzzterm.c @@ -98,7 +98,6 @@ static void fuzz_palette_set(TermWin *tw, int n, int r, int g, int b) {} static void fuzz_palette_reset(TermWin *tw) {} static void fuzz_get_pos(TermWin *tw, int *x, int *y) { *x = *y = 0; } static void fuzz_get_pixels(TermWin *tw, int *x, int *y) { *x = *y = 0; } -static const char *fuzz_get_title(TermWin *tw, bool icon) { return "moo"; } static const TermWinVtable fuzz_termwin_vt = { .setup_draw_ctx = fuzz_setup_draw_ctx, @@ -127,7 +126,6 @@ static const TermWinVtable fuzz_termwin_vt = { .palette_reset = fuzz_palette_reset, .get_pos = fuzz_get_pos, .get_pixels = fuzz_get_pixels, - .get_title = fuzz_get_title, }; void ldisc_send(Ldisc *ldisc, const void *buf, int len, bool interactive) {} diff --git a/putty.h b/putty.h index 44aea084..fd00019f 100644 --- a/putty.h +++ b/putty.h @@ -1149,7 +1149,6 @@ struct TermWinVtable { void (*get_pos)(TermWin *, int *x, int *y); void (*get_pixels)(TermWin *, int *x, int *y); - const char *(*get_title)(TermWin *, bool icon); }; static inline bool win_setup_draw_ctx(TermWin *win) @@ -1210,8 +1209,6 @@ static inline void win_get_pos(TermWin *win, int *x, int *y) { win->vt->get_pos(win, x, y); } static inline void win_get_pixels(TermWin *win, int *x, int *y) { win->vt->get_pixels(win, x, y); } -static inline const char *win_get_title(TermWin *win, bool icon) -{ return win->vt->get_title(win, icon); } /* * Global functions not specific to a connection instance. @@ -1625,6 +1622,7 @@ void term_blink(Terminal *, bool set_cursor); void term_do_paste(Terminal *, const wchar_t *, int); void term_nopaste(Terminal *); void term_copyall(Terminal *, const int *, int); +void term_pre_reconfig(Terminal *, Conf *); void term_reconfig(Terminal *, Conf *); void term_request_copy(Terminal *, const int *clipboards, int n_clipboards); void term_request_paste(Terminal *, int clipboard); @@ -1639,6 +1637,7 @@ void term_set_trust_status(Terminal *term, bool trusted); void term_keyinput(Terminal *, int codepage, const void *buf, int len); void term_keyinputw(Terminal *, const wchar_t * widebuf, int len); void term_get_cursor_position(Terminal *term, int *x, int *y); +void term_setup_window_titles(Terminal *term, const char *title_hostname); typedef enum SmallKeypadKey { SKK_HOME, SKK_END, SKK_INSERT, SKK_DELETE, SKK_PGUP, SKK_PGDN, diff --git a/terminal.c b/terminal.c index 46d9bd16..b1f6bc60 100644 --- a/terminal.c +++ b/terminal.c @@ -1515,6 +1515,17 @@ void term_copy_stuff_from_conf(Terminal *term) } } +void term_pre_reconfig(Terminal *term, Conf *conf) +{ + + /* + * Copy the current window title into the stored previous + * configuration, so that doing nothing to the window title field + * in the config box doesn't reset the title to its startup state. + */ + conf_set_str(conf, CONF_wintitle, term->window_title); +} + /* * When the user reconfigures us, we need to check the forbidden- * alternate-screen config option, disable raw mouse mode if the @@ -1565,6 +1576,16 @@ void term_reconfig(Terminal *term, Conf *conf) } } + { + const char *old_title = conf_get_str(term->conf, CONF_wintitle); + const char *new_title = conf_get_str(conf, CONF_wintitle); + if (strcmp(old_title, new_title)) { + sfree(term->window_title); + term->window_title = dupstr(new_title); + win_set_title(term->win, term->window_title); + } + } + conf_free(term->conf); term->conf = conf_copy(conf); @@ -1658,6 +1679,25 @@ void term_clrsb(Terminal *term) const optionalrgb optionalrgb_none = {0, 0, 0, 0}; +void term_setup_window_titles(Terminal *term, const char *title_hostname) +{ + const char *conf_title = conf_get_str(term->conf, CONF_wintitle); + sfree(term->window_title); + sfree(term->icon_title); + if (*conf_title) { + term->window_title = dupstr(conf_title); + term->icon_title = dupstr(conf_title); + } else { + if (title_hostname) + term->window_title = dupcat(title_hostname, " - ", appname); + else + term->window_title = dupstr(appname); + term->icon_title = dupstr(term->window_title); + } + win_set_title(term->win, term->window_title); + win_set_icon_title(term->win, term->icon_title); +} + /* * Initialise the terminal. */ @@ -1750,6 +1790,9 @@ Terminal *term_init(Conf *myconf, struct unicode_data *ucsdata, TermWin *win) term->bracketed_paste_active = false; + term->window_title = dupstr(""); + term->icon_title = dupstr(""); + return term; } @@ -1804,6 +1847,9 @@ void term_free(Terminal *term) conf_free(term->conf); + sfree(term->window_title); + sfree(term->icon_title); + sfree(term); } @@ -2794,15 +2840,21 @@ static void do_osc(Terminal *term) switch (term->esc_args[0]) { case 0: case 1: - if (!term->no_remote_wintitle) + if (!term->no_remote_wintitle) { win_set_icon_title(term->win, term->osc_string); + sfree(term->icon_title); + term->icon_title = dupstr(term->osc_string); + } if (term->esc_args[0] == 1) break; /* fall through: parameter 0 means set both */ case 2: case 21: - if (!term->no_remote_wintitle) + if (!term->no_remote_wintitle) { win_set_title(term->win, term->osc_string); + sfree(term->window_title); + term->window_title = dupstr(term->osc_string); + } break; case 4: if (term->ldisc && !strcmp(term->osc_string, "?")) { @@ -4422,7 +4474,7 @@ static void term_out(Terminal *term) if (term->ldisc && term->remote_qtitle_action != TITLE_NONE) { if(term->remote_qtitle_action == TITLE_REAL) - p = win_get_title(term->win, true); + p = term->icon_title; else p = EMPTY_WINDOW_TITLE; len = strlen(p); @@ -4437,7 +4489,7 @@ static void term_out(Terminal *term) if (term->ldisc && term->remote_qtitle_action != TITLE_NONE) { if(term->remote_qtitle_action == TITLE_REAL) - p = win_get_title(term->win, false); + p = term->window_title; else p = EMPTY_WINDOW_TITLE; len = strlen(p); diff --git a/terminal.h b/terminal.h index 1bfeecfa..281e9f24 100644 --- a/terminal.h +++ b/terminal.h @@ -342,6 +342,8 @@ struct terminal_tag { int mouse_select_clipboards[N_CLIPBOARDS]; int n_mouse_select_clipboards; int mouse_paste_clipboard; + + char *window_title, *icon_title; }; static inline bool in_utf(Terminal *term) diff --git a/unix/gtkwin.c b/unix/gtkwin.c index abc53fed..fc69c454 100644 --- a/unix/gtkwin.c +++ b/unix/gtkwin.c @@ -629,15 +629,6 @@ static bool find_and_raise_dialog(GtkFrontend *inst, enum DialogSlot slot) return true; } -/* - * Return the window or icon title. - */ -static const char *gtkwin_get_title(TermWin *tw, bool icon) -{ - GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); - return icon ? inst->icontitle : inst->wintitle; -} - static void warn_on_close_callback(void *vctx, int result) { GtkFrontend *inst = (GtkFrontend *)vctx; @@ -3330,15 +3321,6 @@ static void gtkwin_set_icon_title(TermWin *tw, const char *title) set_window_titles(inst); } -void set_title_and_icon(GtkFrontend *inst, char *title, char *icon) -{ - sfree(inst->wintitle); - inst->wintitle = dupstr(title); - sfree(inst->icontitle); - inst->icontitle = dupstr(icon); - set_window_titles(inst); -} - static void gtkwin_set_scrollbar(TermWin *tw, int total, int start, int page) { GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); @@ -4630,6 +4612,8 @@ void change_settings_menuitem(GtkMenuItem *item, gpointer data) ctx->inst = inst; ctx->newconf = conf_copy(inst->conf); + term_pre_reconfig(inst->term, ctx->newconf); + dialog = create_config_box( title, ctx->newconf, true, inst->backend ? backend_cfg_info(inst->backend) : 0, @@ -4731,15 +4715,6 @@ static void after_change_settings_dialog(void *vctx, int retval) ? 0 : 1); } - /* - * Change the window title, if required. - */ - if (strcmp(conf_get_str(oldconf, CONF_wintitle), - conf_get_str(newconf, CONF_wintitle))) - win_set_title(&inst->termwin, - conf_get_str(newconf, CONF_wintitle)); - set_window_titles(inst); - /* * Redo the whole tangled fonts and Unicode mess if * necessary. @@ -5097,7 +5072,6 @@ static void start_backend(GtkFrontend *inst) { const struct BackendVtable *vt; char *error, *realhost; - char *s; vt = select_backend(inst->conf); @@ -5119,14 +5093,7 @@ static void start_backend(GtkFrontend *inst) return; } - s = conf_get_str(inst->conf, CONF_wintitle); - if (s[0]) { - set_title_and_icon(inst, s, s); - } else { - char *title = make_default_wintitle(realhost); - set_title_and_icon(inst, title, title); - sfree(title); - } + term_setup_window_titles(inst->term, realhost); sfree(realhost); term_provide_backend(inst->term, inst->backend); @@ -5190,7 +5157,6 @@ static const TermWinVtable gtk_termwin_vt = { .palette_reset = gtkwin_palette_reset, .get_pos = gtkwin_get_pos, .get_pixels = gtkwin_get_pixels, - .get_title = gtkwin_get_title, }; void new_session_window(Conf *conf, const char *geometry_string) diff --git a/unix/unix.h b/unix/unix.h index 80bfbeaf..d050dccd 100644 --- a/unix/unix.h +++ b/unix/unix.h @@ -245,9 +245,6 @@ GtkWidget *create_message_box( post_dialog_fn_t after, void *afterctx); #endif -/* Things gtkwin.c needs from {ptermm,uxputty}.c */ -char *make_default_wintitle(char *hostname); - /* gtkwin.c needs this special function in xkeysym.c */ int keysym_to_unicode(int keysym); diff --git a/unix/uxpterm.c b/unix/uxpterm.c index 9d768a4c..b18e3442 100644 --- a/unix/uxpterm.c +++ b/unix/uxpterm.c @@ -41,11 +41,6 @@ void cleanup_exit(int code) exit(code); } -char *make_default_wintitle(char *hostname) -{ - return dupstr("pterm"); -} - void setup(bool single) { settings_set_default_protocol(-1); diff --git a/unix/uxputty.c b/unix/uxputty.c index 1b44c2fd..7a808087 100644 --- a/unix/uxputty.c +++ b/unix/uxputty.c @@ -56,11 +56,6 @@ void initial_config_box(Conf *conf, post_dialog_fn_t after, void *afterctx) const bool use_event_log = true, new_session = true, saved_sessions = true; const bool dup_check_launchable = true; -char *make_default_wintitle(char *hostname) -{ - return dupcat(hostname, " - ", appname); -} - /* * X11-forwarding-related things suitable for Gtk app. */ diff --git a/windows/window.c b/windows/window.c index e3c810ef..7ec9aa82 100644 --- a/windows/window.c +++ b/windows/window.c @@ -253,7 +253,6 @@ static void wintw_palette_set(TermWin *, int n, int r, int g, int b); static void wintw_palette_reset(TermWin *); static void wintw_get_pos(TermWin *, int *x, int *y); static void wintw_get_pixels(TermWin *, int *x, int *y); -static const char *wintw_get_title(TermWin *, bool icon); static const TermWinVtable windows_termwin_vt = { .setup_draw_ctx = wintw_setup_draw_ctx, @@ -282,7 +281,6 @@ static const TermWinVtable windows_termwin_vt = { .palette_reset = wintw_palette_reset, .get_pos = wintw_get_pos, .get_pixels = wintw_get_pixels, - .get_title = wintw_get_title, }; static TermWin wintw[1]; @@ -361,7 +359,6 @@ static WinGuiSeat wgs = { .seat.vt = &win_seat_vt, static void start_backend(void) { const struct BackendVtable *vt; - const char *title; char *error, *realhost; int i; @@ -395,16 +392,8 @@ static void start_backend(void) sfree(msg); exit(0); } - window_name = icon_name = NULL; - char *title_to_free = NULL; - title = conf_get_str(conf, CONF_wintitle); - if (!*title) { - title_to_free = dupprintf("%s - %s", realhost, appname); - title = title_to_free; - } + term_setup_window_titles(term, realhost); sfree(realhost); - win_set_title(wintw, title); - win_set_icon_title(wintw, title); /* * Connect the terminal to the backend for resize purposes. @@ -428,8 +417,6 @@ static void start_backend(void) } session_closed = false; - - sfree(title_to_free); } static void close_session(void *ignored_context) @@ -2328,14 +2315,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, else reconfiguring = true; - /* - * Copy the current window title into the stored - * previous configuration, so that doing nothing to - * the window title field in the config box doesn't - * reset the title to its startup state. - */ - conf_set_str(conf, CONF_wintitle, window_name); - + term_pre_reconfig(term, conf); prev_conf = conf_copy(conf); reconfig_result = do_reconfig( @@ -2467,13 +2447,6 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, init_lvl = 2; } - win_set_title(wintw, conf_get_str(conf, CONF_wintitle)); - if (IsIconic(hwnd)) { - SetWindowText(hwnd, - conf_get_bool(conf, CONF_win_name_always) ? - window_name : icon_name); - } - { FontSpec *font = conf_get_fontspec(conf, CONF_font); FontSpec *prev_font = conf_get_fontspec(prev_conf, @@ -5668,14 +5641,6 @@ static void wintw_get_pixels(TermWin *tw, int *x, int *y) *y = r.bottom - r.top; } -/* - * Return the window or icon title. - */ -static const char *wintw_get_title(TermWin *tw, bool icon) -{ - return icon ? icon_name : window_name; -} - /* * See if we're in full-screen mode. */