+ return err;
+}
+
+static void block_signal(int sig, int block_it) {
+ sigset_t ss;
+
+ sigemptyset(&ss);
+ sigaddset(&ss, sig);
+ if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
+ perror("sigprocmask");
+ exit(1);
+ }
+}
+
+/*
+ * This function retrieves the character set encoding of a font. It
+ * returns the character set without the X11 hack (in case the user
+ * asks to use the font's own encoding).
+ */
+static int set_font_info(struct gui_data *inst, int fontid)
+{
+ GdkFont *font = inst->fonts[fontid];
+ XFontStruct *xfs = GDK_FONT_XFONT(font);
+ Display *disp = GDK_FONT_XDISPLAY(font);
+ Atom charset_registry, charset_encoding;
+ unsigned long registry_ret, encoding_ret;
+ int retval = CS_NONE;
+
+ charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
+ charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
+ inst->fontinfo[fontid].charset = CS_NONE;
+ inst->fontinfo[fontid].is_wide = 0;
+ if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
+ XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
+ char *reg, *enc;
+ reg = XGetAtomName(disp, (Atom)registry_ret);
+ enc = XGetAtomName(disp, (Atom)encoding_ret);
+ if (reg && enc) {
+ char *encoding = dupcat(reg, "-", enc, NULL);
+ retval = inst->fontinfo[fontid].charset =
+ charset_from_xenc(encoding);
+ /* FIXME: when libcharset supports wide encodings fix this. */
+ if (!strcasecmp(encoding, "iso10646-1")) {
+ inst->fontinfo[fontid].is_wide = 1;
+ retval = CS_UTF8;
+ }
+
+ /*
+ * Hack for X line-drawing characters: if the primary
+ * font is encoded as ISO-8859-anything, and has valid
+ * glyphs in the first 32 char positions, it is assumed
+ * that those glyphs are the VT100 line-drawing
+ * character set.
+ *
+ * Actually, we'll hack even harder by only checking
+ * position 0x19 (vertical line, VT100 linedrawing
+ * `x'). Then we can check it easily by seeing if the
+ * ascent and descent differ.
+ */
+ if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
+ int lb, rb, wid, asc, desc;
+ gchar text[2];
+
+ text[1] = '\0';
+ text[0] = '\x12';
+ gdk_string_extents(inst->fonts[fontid], text,
+ &lb, &rb, &wid, &asc, &desc);
+ if (asc != desc)
+ inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
+ }
+
+ sfree(encoding);
+ }
+ }
+
+ return retval;
+}
+
+int uxsel_input_add(int fd, int rwx) {
+ int flags = 0;
+ if (rwx & 1) flags |= GDK_INPUT_READ;
+ if (rwx & 2) flags |= GDK_INPUT_WRITE;
+ if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
+ return gdk_input_add(fd, flags, fd_input_func, NULL);
+}
+
+void uxsel_input_remove(int id) {
+ gdk_input_remove(id);
+}
+
+void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ term_clrsb(inst->term);
+}
+
+void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ term_pwron(inst->term);
+ ldisc_send(inst->ldisc, NULL, 0, 0);
+}
+
+void special_menuitem(GtkMenuItem *item, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
+
+ inst->back->special(inst->backhandle, code);
+}
+
+void about_menuitem(GtkMenuItem *item, gpointer data)
+{
+ /* struct gui_data *inst = (struct gui_data *)data; */
+ about_box();
+}
+
+void update_specials_menu(void *frontend)
+{
+ Terminal *term = (Terminal *)frontend;
+ struct gui_data *inst = (struct gui_data *)term->frontend;
+
+ const struct telnet_special *specials;
+
+ specials = inst->back->get_specials(inst->backhandle);
+ gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
+ (GtkCallback)gtk_widget_destroy, NULL);
+ if (specials) {
+ int i;
+ GtkWidget *menuitem;
+ for (i = 0; specials[i].name; i++) {
+ if (*specials[i].name) {
+ menuitem = gtk_menu_item_new_with_label(specials[i].name);
+ gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
+ (gpointer)specials[i].code);
+ gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
+ GTK_SIGNAL_FUNC(special_menuitem), inst);
+ } else
+ menuitem = gtk_menu_item_new();
+ gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
+ gtk_widget_show(menuitem);
+ }
+ gtk_widget_show(inst->specialsitem1);
+ gtk_widget_show(inst->specialsitem2);
+ } else {
+ gtk_widget_hide(inst->specialsitem1);
+ gtk_widget_hide(inst->specialsitem2);
+ }
+}
+
+int pt_main(int argc, char **argv)
+{
+ extern Backend *select_backend(Config *cfg);
+ extern int cfgbox(Config *cfg);
+ struct gui_data *inst;
+ int font_charset;
+
+ /* defer any child exit handling until we're ready to deal with
+ * it */
+ block_signal(SIGCHLD, 1);
+
+ gtk_init(&argc, &argv);
+
+ /*
+ * Create an instance structure and initialise to zeroes
+ */
+ inst = snew(struct gui_data);
+ memset(inst, 0, sizeof(*inst));
+ inst->alt_keycode = -1; /* this one needs _not_ to be zero */
+
+ if (do_cmdline(argc, argv, 0, &inst->cfg))
+ exit(1); /* pre-defaults pass to get -class */
+ do_defaults(NULL, &inst->cfg);
+ if (do_cmdline(argc, argv, 1, &inst->cfg))
+ exit(1); /* post-defaults, do everything */
+
+ cmdline_run_saved(&inst->cfg);
+
+ if (!*inst->cfg.host && !cfgbox(&inst->cfg))
+ exit(0); /* config box hit Cancel */
+
+ inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
+ if (!inst->fonts[0]) {
+ fprintf(stderr, "pterm: unable to load font \"%s\"\n",
+ inst->cfg.font.name);
+ exit(1);
+ }
+ font_charset = set_font_info(inst, 0);
+ if (inst->cfg.boldfont.name[0]) {
+ inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
+ if (!inst->fonts[1]) {
+ fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
+ inst->cfg.boldfont.name);
+ exit(1);
+ }
+ set_font_info(inst, 1);
+ } else
+ inst->fonts[1] = NULL;
+ if (inst->cfg.widefont.name[0]) {
+ inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
+ if (!inst->fonts[2]) {
+ fprintf(stderr, "pterm: unable to load wide font \"%s\"\n",
+ inst->cfg.widefont.name);
+ exit(1);
+ }
+ set_font_info(inst, 2);
+ } else
+ inst->fonts[2] = NULL;
+ if (inst->cfg.wideboldfont.name[0]) {
+ inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
+ if (!inst->fonts[3]) {
+ fprintf(stderr, "pterm: unable to load wide/bold font \"%s\"\n",
+ inst->cfg.wideboldfont.name);
+ exit(1);
+ }
+ set_font_info(inst, 3);
+ } else
+ inst->fonts[3] = NULL;
+