+void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ term_pwron(inst->term);
+ if (inst->ldisc)
+ ldisc_send(inst->ldisc, NULL, 0, 0);
+}
+
+void copy_all_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ term_copyall(inst->term);
+}
+
+void special_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
+ "user-data"));
+
+ if (inst->back)
+ inst->back->special(inst->backhandle, code);
+}
+
+void about_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ about_box(inst->window);
+}
+
+void event_log_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ showeventlog(inst->eventlogstuff, inst->window);
+}
+
+void change_settings_menuitem(GtkMenuItem *item, gpointer data)
+{
+ /* This maps colour indices in inst->cfg to those used in inst->cols. */
+ static const int ww[] = {
+ 6, 7, 8, 9, 10, 11, 12, 13,
+ 14, 15, 16, 17, 18, 19, 20, 21,
+ 0, 1, 2, 3, 4, 5
+ };
+ struct gui_data *inst = (struct gui_data *)data;
+ char *title = dupcat(appname, " Reconfiguration", NULL);
+ Config cfg2, oldcfg;
+ int i, need_size;
+
+ cfg2 = inst->cfg; /* structure copy */
+
+ if (do_config_box(title, &cfg2, 1)) {
+
+ oldcfg = inst->cfg; /* structure copy */
+ inst->cfg = cfg2; /* structure copy */
+
+ /* Pass new config data to the logging module */
+ log_reconfig(inst->logctx, &cfg2);
+ /*
+ * Flush the line discipline's edit buffer in the case
+ * where local editing has just been disabled.
+ */
+ if (inst->ldisc)
+ ldisc_send(inst->ldisc, NULL, 0, 0);
+ /* Pass new config data to the terminal */
+ term_reconfig(inst->term, &cfg2);
+ /* Pass new config data to the back end */
+ if (inst->back)
+ inst->back->reconfig(inst->backhandle, &cfg2);
+
+ /*
+ * Just setting inst->cfg is sufficient to cause colour
+ * setting changes to appear on the next ESC]R palette
+ * reset. But we should also check whether any colour
+ * settings have been changed, and revert the ones that
+ * have to the new default, on the assumption that the user
+ * is most likely to want an immediate update.
+ */
+ for (i = 0; i < NCOLOURS; i++) {
+ if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
+ oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
+ oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2]) {
+ real_palette_set(inst, i, cfg2.colours[ww[i]][0],
+ cfg2.colours[ww[i]][1],
+ cfg2.colours[ww[i]][2]);
+
+ /*
+ * If the default background has changed, we must
+ * repaint the space in between the window border
+ * and the text area.
+ */
+ if (i == 18) {
+ set_window_background(inst);
+ draw_backing_rect(inst);
+ }
+ }
+ }
+
+ /*
+ * If the scrollbar needs to be shown, hidden, or moved
+ * from one end to the other of the window, do so now.
+ */
+ if (oldcfg.scrollbar != cfg2.scrollbar) {
+ if (cfg2.scrollbar)
+ gtk_widget_show(inst->sbar);
+ else
+ gtk_widget_hide(inst->sbar);
+ }
+ if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
+ gtk_box_reorder_child(inst->hbox, inst->sbar,
+ cfg2.scrollbar_on_left ? 0 : 1);
+ }
+
+ /*
+ * Change the window title, if required.
+ */
+ if (strcmp(oldcfg.wintitle, cfg2.wintitle))
+ set_title(inst, cfg2.wintitle);
+ set_window_titles(inst);
+
+ /*
+ * Redo the whole tangled fonts and Unicode mess if
+ * necessary.
+ */
+ if (strcmp(oldcfg.font.name, cfg2.font.name) ||
+ strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
+ strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
+ strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
+ strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
+ oldcfg.vtmode != cfg2.vtmode ||
+ oldcfg.shadowbold != cfg2.shadowbold) {
+ setup_fonts_ucs(inst);
+ need_size = 1;
+ } else
+ need_size = 0;
+
+ /*
+ * Resize the window.
+ */
+ if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
+ oldcfg.window_border != cfg2.window_border || need_size) {
+ set_geom_hints(inst);
+ request_resize(inst, cfg2.width, cfg2.height);
+ } else {
+ /*
+ * The above will have caused a call to term_size() for
+ * us if it happened. If the user has fiddled with only
+ * the scrollback size, the above will not have
+ * happened and we will need an explicit term_size()
+ * here.
+ */
+ if (oldcfg.savelines != cfg2.savelines)
+ term_size(inst->term, inst->term->rows, inst->term->cols,
+ cfg2.savelines);
+ }
+
+ term_invalidate(inst->term);
+
+ /*
+ * We do an explicit full redraw here to ensure the window
+ * border has been redrawn as well as the text area.
+ */
+ gtk_widget_queue_draw(inst->area);
+ }
+ sfree(title);
+}
+
+void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
+{
+ /*
+ * Re-execing ourself is not an exact science under Unix. I do
+ * the best I can by using /proc/self/exe if available and by
+ * assuming argv[0] can be found on $PATH if not.
+ *
+ * Note that we also have to reconstruct the elements of the
+ * original argv which gtk swallowed, since the user wants the
+ * new session to appear on the same X display as the old one.
+ */
+ char **args;
+ va_list ap;
+ int i, n;
+ int pid;
+
+ /*
+ * Collect the arguments with which to re-exec ourself.
+ */
+ va_start(ap, fd_to_close);
+ n = 2; /* progname and terminating NULL */
+ n += inst->ngtkargs;
+ while (va_arg(ap, char *) != NULL)
+ n++;
+ va_end(ap);
+
+ args = snewn(n, char *);
+ args[0] = inst->progname;
+ args[n-1] = NULL;
+ for (i = 0; i < inst->ngtkargs; i++)
+ args[i+1] = inst->gtkargvstart[i];
+
+ i++;
+ va_start(ap, fd_to_close);
+ while ((args[i++] = va_arg(ap, char *)) != NULL);
+ va_end(ap);
+
+ assert(i == n);
+
+ /*
+ * Do the double fork.
+ */
+ pid = fork();
+ if (pid < 0) {
+ perror("fork");
+ return;
+ }
+
+ if (pid == 0) {
+ int pid2 = fork();
+ if (pid2 < 0) {
+ perror("fork");
+ _exit(1);
+ } else if (pid2 > 0) {
+ /*
+ * First child has successfully forked second child. My
+ * Work Here Is Done. Note the use of _exit rather than
+ * exit: the latter appears to cause destroy messages
+ * to be sent to the X server. I suspect gtk uses
+ * atexit.
+ */
+ _exit(0);
+ }
+
+ /*
+ * If we reach here, we are the second child, so we now
+ * actually perform the exec.
+ */
+ if (fd_to_close >= 0)
+ close(fd_to_close);
+
+ execv("/proc/self/exe", args);
+ execvp(inst->progname, args);
+ perror("exec");
+ _exit(127);
+
+ } else {
+ int status;
+ waitpid(pid, &status, 0);
+ }
+
+}
+
+void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
+{
+ struct gui_data *inst = (struct gui_data *)gdata;
+ /*
+ * For this feature we must marshal cfg and (possibly) pty_argv
+ * into a byte stream, create a pipe, and send this byte stream
+ * to the child through the pipe.
+ */
+ int i, ret, size;
+ char *data;
+ char option[80];
+ int pipefd[2];
+
+ if (pipe(pipefd) < 0) {
+ perror("pipe");
+ return;
+ }
+
+ size = sizeof(inst->cfg);
+ if (use_pty_argv && pty_argv) {
+ for (i = 0; pty_argv[i]; i++)
+ size += strlen(pty_argv[i]) + 1;
+ }
+
+ data = snewn(size, char);
+ memcpy(data, &inst->cfg, sizeof(inst->cfg));
+ if (use_pty_argv && pty_argv) {
+ int p = sizeof(inst->cfg);
+ for (i = 0; pty_argv[i]; i++) {
+ strcpy(data + p, pty_argv[i]);
+ p += strlen(pty_argv[i]) + 1;
+ }
+ assert(p == size);
+ }
+
+ sprintf(option, "---[%d,%d]", pipefd[0], size);
+ fcntl(pipefd[0], F_SETFD, 0);
+ fork_and_exec_self(inst, pipefd[1], option, NULL);
+ close(pipefd[0]);
+
+ i = ret = 0;
+ while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
+ i += ret;
+ if (ret < 0)
+ perror("write to pipe");
+ close(pipefd[1]);
+ sfree(data);
+}
+
+int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
+{
+ int fd, i, ret, size;
+ char *data;
+
+ if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
+ fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
+ exit(1);
+ }
+
+ data = snewn(size, char);
+ i = ret = 0;
+ while (i < size && (ret = read(fd, data + i, size - i)) > 0)
+ i += ret;
+ if (ret < 0) {
+ perror("read from pipe");
+ exit(1);
+ } else if (i < size) {
+ fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
+ appname);
+ exit(1);
+ }
+
+ memcpy(cfg, data, sizeof(Config));
+ if (use_pty_argv && size > sizeof(Config)) {
+ int n = 0;
+ i = sizeof(Config);
+ while (i < size) {
+ while (i < size && data[i]) i++;
+ if (i >= size) {
+ fprintf(stderr, "%s: malformed Duplicate Session data\n",
+ appname);
+ exit(1);
+ }
+ i++;
+ n++;
+ }
+ pty_argv = snewn(n+1, char *);
+ pty_argv[n] = NULL;
+ n = 0;
+ i = sizeof(Config);
+ while (i < size) {
+ char *p = data + i;
+ while (i < size && data[i]) i++;
+ assert(i < size);
+ i++;
+ pty_argv[n++] = dupstr(p);
+ }
+ }
+
+ return 0;
+}
+
+void new_session_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ fork_and_exec_self(inst, -1, NULL);
+}
+
+void restart_session_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ if (!inst->back) {
+ logevent(inst, "----- Session restarted -----");
+ start_backend(inst);
+ inst->exited = FALSE;
+ }
+}
+
+void saved_session_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
+
+ fork_and_exec_self(inst, -1, "-load", str, NULL);
+}
+
+void saved_session_freedata(GtkMenuItem *item, gpointer data)
+{
+ char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
+
+ sfree(str);
+}
+
+void update_specials_menu(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+
+ const struct telnet_special *specials;
+
+ if (inst->back)
+ specials = inst->back->get_specials(inst->backhandle);