+ /*
+ * This is a heinous hack dreamed up by the gnome-terminal
+ * people to get around a limitation in gtk. The problem is
+ * that in order to set the size correctly we really need to be
+ * calling gtk_window_resize - but that needs to know the size
+ * of the _whole window_, not the drawing area. So what we do
+ * is to set an artificially huge size request on the drawing
+ * area, recompute the resulting size request on the window,
+ * and look at the difference between the two. That gives us
+ * the x and y offsets we need to translate drawing area size
+ * into window size for real, and then we call
+ * gtk_window_resize.
+ */
+
+ /*
+ * We start by retrieving the current size of the whole window.
+ * Adding a bit to _that_ will give us a value we can use as a
+ * bogus size request which guarantees to be bigger than the
+ * current size of the drawing area.
+ */
+ get_window_pixels(inst, &large_x, &large_y);
+ large_x += 32;
+ large_y += 32;
+
+#if GTK_CHECK_VERSION(2,0,0)
+ gtk_widget_set_size_request(inst->area, large_x, large_y);
+#else
+ gtk_widget_set_usize(inst->area, large_x, large_y);
+#endif
+ gtk_widget_size_request(inst->area, &inner);
+ gtk_widget_size_request(inst->window, &outer);
+
+ offset_x = outer.width - inner.width;
+ offset_y = outer.height - inner.height;
+
+ area_x = inst->font_width * w + 2*inst->cfg.window_border;
+ area_y = inst->font_height * h + 2*inst->cfg.window_border;
+
+ /*
+ * Now we must set the size request on the drawing area back to
+ * something sensible before we commit the real resize. Best
+ * way to do this, I think, is to set it to what the size is
+ * really going to end up being.
+ */
+#if GTK_CHECK_VERSION(2,0,0)
+ gtk_widget_set_size_request(inst->area, area_x, area_y);
+#else
+ gtk_widget_set_usize(inst->area, area_x, area_y);
+ gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
+#endif
+
+ gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
+
+#if GTK_CHECK_VERSION(2,0,0)
+ gtk_window_resize(GTK_WINDOW(inst->window),
+ area_x + offset_x, area_y + offset_y);
+#else
+ gdk_window_resize(inst->window->window,
+ area_x + offset_x, area_y + offset_y);
+#endif
+}
+
+static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
+{
+ gboolean success[1];
+
+ inst->cols[n].red = r * 0x0101;
+ inst->cols[n].green = g * 0x0101;
+ inst->cols[n].blue = b * 0x0101;
+
+ gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
+ gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
+ FALSE, FALSE, success);
+ if (!success[0])
+ g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
+ n, r, g, b);
+}
+
+void set_window_background(struct gui_data *inst)
+{
+ if (inst->area && inst->area->window)
+ gdk_window_set_background(inst->area->window, &inst->cols[18]);
+ if (inst->window && inst->window->window)
+ gdk_window_set_background(inst->window->window, &inst->cols[18]);
+}
+
+void palette_set(void *frontend, int n, int r, int g, int b)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ static const int first[21] = {
+ 0, 2, 4, 6, 8, 10, 12, 14,
+ 1, 3, 5, 7, 9, 11, 13, 15,
+ 16, 17, 18, 20, 22
+ };
+ real_palette_set(inst, first[n], r, g, b);
+ if (first[n] >= 18)
+ real_palette_set(inst, first[n] + 1, r, g, b);
+ if (first[n] == 18)
+ set_window_background(inst);
+}
+
+void palette_reset(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ /* 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
+ };
+ gboolean success[NCOLOURS];
+ int i;
+
+ assert(lenof(ww) == NCOLOURS);
+
+ if (!inst->colmap) {
+ inst->colmap = gdk_colormap_get_system();
+ } else {
+ gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
+ }
+
+ for (i = 0; i < NCOLOURS; i++) {
+ inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
+ inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
+ inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
+ }
+
+ gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
+ FALSE, FALSE, success);
+ for (i = 0; i < NCOLOURS; i++) {
+ if (!success[i])
+ g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
+ appname, i, inst->cfg.colours[i][0],
+ inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
+ }
+
+ set_window_background(inst);
+}
+
+/* Ensure that all the cut buffers exist - according to the ICCCM, we must
+ * do this before we start using cut buffers.
+ */
+void init_cutbuffers()
+{
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0);
+ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
+ XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0);
+}
+
+/* Store the data in a cut-buffer. */
+void store_cutbuffer(char * ptr, int len)
+{
+ /* ICCCM says we must rotate the buffers before storing to buffer 0. */
+ XRotateBuffers(GDK_DISPLAY(), 1);
+ XStoreBytes(GDK_DISPLAY(), ptr, len);
+}
+
+/* Retrieve data from a cut-buffer.
+ * Returned data needs to be freed with XFree().
+ */
+char * retrieve_cutbuffer(int * nbytes)
+{
+ char * ptr;
+ ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
+ if (nbytes <= 0 && ptr != 0) {
+ XFree(ptr);
+ ptr = 0;
+ }
+ return ptr;
+}
+
+void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ if (inst->pasteout_data)
+ sfree(inst->pasteout_data);
+ if (inst->pasteout_data_ctext)
+ sfree(inst->pasteout_data_ctext);
+ if (inst->pasteout_data_utf8)
+ sfree(inst->pasteout_data_utf8);
+
+ /*
+ * Set up UTF-8 and compound text paste data. This only happens
+ * if we aren't in direct-to-font mode using the D800 hack.
+ */
+ if (!inst->direct_to_font) {
+ wchar_t *tmp = data;
+ int tmplen = len;
+ XTextProperty tp;
+ char *list[1];
+
+ inst->pasteout_data_utf8 = snewn(len*6, char);
+ inst->pasteout_data_utf8_len = len*6;
+ inst->pasteout_data_utf8_len =
+ charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
+ inst->pasteout_data_utf8_len,
+ CS_UTF8, NULL, NULL, 0);
+ if (inst->pasteout_data_utf8_len == 0) {
+ sfree(inst->pasteout_data_utf8);
+ inst->pasteout_data_utf8 = NULL;
+ } else {
+ inst->pasteout_data_utf8 =
+ sresize(inst->pasteout_data_utf8,
+ inst->pasteout_data_utf8_len + 1, char);
+ inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
+ }
+
+ /*
+ * Now let Xlib convert our UTF-8 data into compound text.
+ */
+ list[0] = inst->pasteout_data_utf8;
+ if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
+ XCompoundTextStyle, &tp) == 0) {
+ inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
+ memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
+ inst->pasteout_data_ctext_len = tp.nitems;
+ XFree(tp.value);
+ }
+ } else {
+ inst->pasteout_data_utf8 = NULL;
+ inst->pasteout_data_utf8_len = 0;
+ inst->pasteout_data_ctext = NULL;
+ inst->pasteout_data_ctext_len = 0;
+ }
+
+ inst->pasteout_data = snewn(len*6, char);
+ inst->pasteout_data_len = len*6;
+ inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
+ data, len, inst->pasteout_data,
+ inst->pasteout_data_len,
+ NULL, NULL, NULL);
+ if (inst->pasteout_data_len == 0) {
+ sfree(inst->pasteout_data);
+ inst->pasteout_data = NULL;
+ } else {
+ inst->pasteout_data =
+ sresize(inst->pasteout_data, inst->pasteout_data_len, char);
+ }
+
+ store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
+
+ if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
+ inst->input_event_time)) {
+ gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING, 1);
+ if (inst->pasteout_data_ctext)
+ gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+ compound_text_atom, 1);
+ if (inst->pasteout_data_utf8)
+ gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
+ utf8_string_atom, 1);
+ }
+
+ if (must_deselect)
+ term_deselect(inst->term);
+}
+
+void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
+ guint info, guint time_stamp, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ if (seldata->target == utf8_string_atom)
+ gtk_selection_data_set(seldata, seldata->target, 8,
+ inst->pasteout_data_utf8,
+ inst->pasteout_data_utf8_len);
+ else if (seldata->target == compound_text_atom)
+ gtk_selection_data_set(seldata, seldata->target, 8,
+ inst->pasteout_data_ctext,
+ inst->pasteout_data_ctext_len);
+ else
+ gtk_selection_data_set(seldata, seldata->target, 8,
+ inst->pasteout_data, inst->pasteout_data_len);
+}
+
+gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
+ gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ term_deselect(inst->term);
+ if (inst->pasteout_data)
+ sfree(inst->pasteout_data);
+ if (inst->pasteout_data_ctext)
+ sfree(inst->pasteout_data_ctext);
+ if (inst->pasteout_data_utf8)
+ sfree(inst->pasteout_data_utf8);
+ inst->pasteout_data = NULL;
+ inst->pasteout_data_len = 0;
+ inst->pasteout_data_ctext = NULL;
+ inst->pasteout_data_ctext_len = 0;
+ inst->pasteout_data_utf8 = NULL;
+ inst->pasteout_data_utf8_len = 0;
+ return TRUE;
+}
+
+void request_paste(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ /*
+ * In Unix, pasting is asynchronous: all we can do at the
+ * moment is to call gtk_selection_convert(), and when the data
+ * comes back _then_ we can call term_do_paste().
+ */
+
+ if (!inst->direct_to_font) {
+ /*
+ * First we attempt to retrieve the selection as a UTF-8
+ * string (which we will convert to the correct code page
+ * before sending to the session, of course). If that
+ * fails, selection_received() will be informed and will
+ * fall back to an ordinary string.
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ utf8_string_atom,
+ inst->input_event_time);
+ } else {
+ /*
+ * If we're in direct-to-font mode, we disable UTF-8
+ * pasting, and go straight to ordinary string data.
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING,
+ inst->input_event_time);
+ }
+}
+
+gint idle_paste_func(gpointer data); /* forward ref */
+
+void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
+ guint time, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+ XTextProperty tp;
+ char **list;
+ char *text;
+ int length, count, ret;
+ int free_list_required = 0;
+ int free_required = 0;
+ int charset;
+
+ if (seldata->target == utf8_string_atom && seldata->length <= 0) {
+ /*
+ * Failed to get a UTF-8 selection string. Try compound
+ * text next.
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ compound_text_atom,
+ inst->input_event_time);
+ return;
+ }
+
+ if (seldata->target == compound_text_atom && seldata->length <= 0) {
+ /*
+ * Failed to get UTF-8 or compound text. Try an ordinary
+ * string.
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING,
+ inst->input_event_time);
+ return;
+ }
+
+ /*
+ * If we have data, but it's not of a type we can deal with,
+ * we have to ignore the data.
+ */
+ if (seldata->length > 0 &&
+ seldata->type != GDK_SELECTION_TYPE_STRING &&
+ seldata->type != compound_text_atom &&
+ seldata->type != utf8_string_atom)
+ return;
+
+ /*
+ * If we have no data, try looking in a cut buffer.
+ */
+ if (seldata->length <= 0) {
+ text = retrieve_cutbuffer(&length);
+ if (length == 0)
+ return;
+ /* Xterm is rumoured to expect Latin-1, though I havn't checked the
+ * source, so use that as a de-facto standard. */
+ charset = CS_ISO8859_1;
+ free_required = 1;
+ } else {
+ /*
+ * Convert COMPOUND_TEXT into UTF-8.
+ */
+ if (seldata->type == compound_text_atom) {
+ tp.value = seldata->data;
+ tp.encoding = (Atom) seldata->type;
+ tp.format = seldata->format;
+ tp.nitems = seldata->length;
+ ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp, &list, &count);
+ if (ret != 0 || count != 1) {
+ /*
+ * Compound text failed; fall back to STRING.
+ */
+ gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
+ GDK_SELECTION_TYPE_STRING,
+ inst->input_event_time);
+ return;
+ }
+ text = list[0];
+ length = strlen(list[0]);
+ charset = CS_UTF8;
+ free_list_required = 1;
+ } else {
+ text = (char *)seldata->data;
+ length = seldata->length;
+ charset = (seldata->type == utf8_string_atom ?
+ CS_UTF8 : inst->ucsdata.line_codepage);
+ }
+ }
+
+ if (inst->pastein_data)
+ sfree(inst->pastein_data);
+
+ inst->pastein_data = snewn(length, wchar_t);
+ inst->pastein_data_len = length;
+ inst->pastein_data_len =
+ mb_to_wc(charset, 0, text, length,
+ inst->pastein_data, inst->pastein_data_len);
+
+ term_do_paste(inst->term);
+
+ if (term_paste_pending(inst->term))
+ inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
+
+ if (free_list_required)
+ XFreeStringList(list);
+ if (free_required)
+ XFree(text);
+}
+
+gint idle_paste_func(gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ if (term_paste_pending(inst->term))
+ term_paste(inst->term);
+ else
+ gtk_idle_remove(inst->term_paste_idle_id);
+
+ return TRUE;
+}
+
+
+void get_clip(void *frontend, wchar_t ** p, int *len)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+
+ if (p) {
+ *p = inst->pastein_data;
+ *len = inst->pastein_data_len;
+ }
+}
+
+static void set_window_titles(struct gui_data *inst)
+{
+ /*
+ * We must always call set_icon_name after calling set_title,
+ * since set_title will write both names. Irritating, but such
+ * is life.
+ */
+ gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
+ if (!inst->cfg.win_name_always)
+ gdk_window_set_icon_name(inst->window->window, inst->icontitle);
+}
+
+void set_title(void *frontend, char *title)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ strncpy(inst->wintitle, title, lenof(inst->wintitle));
+ inst->wintitle[lenof(inst->wintitle)-1] = '\0';
+ set_window_titles(inst);
+}
+
+void set_icon(void *frontend, char *title)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ strncpy(inst->icontitle, title, lenof(inst->icontitle));
+ inst->icontitle[lenof(inst->icontitle)-1] = '\0';
+ set_window_titles(inst);
+}
+
+void set_sbar(void *frontend, int total, int start, int page)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ if (!inst->cfg.scrollbar)
+ return;
+ inst->sbar_adjust->lower = 0;
+ inst->sbar_adjust->upper = total;
+ inst->sbar_adjust->value = start;
+ inst->sbar_adjust->page_size = page;
+ inst->sbar_adjust->step_increment = 1;
+ inst->sbar_adjust->page_increment = page/2;
+ inst->ignore_sbar = TRUE;
+ gtk_adjustment_changed(inst->sbar_adjust);
+ inst->ignore_sbar = FALSE;
+}
+
+void scrollbar_moved(GtkAdjustment *adj, gpointer data)
+{
+ struct gui_data *inst = (struct gui_data *)data;
+
+ if (!inst->cfg.scrollbar)
+ return;
+ if (!inst->ignore_sbar)
+ term_scroll(inst->term, 1, (int)adj->value);
+}
+
+void sys_cursor(void *frontend, int x, int y)
+{
+ /*
+ * This is meaningless under X.
+ */
+}
+
+/*
+ * This is still called when mode==BELL_VISUAL, even though the
+ * visual bell is handled entirely within terminal.c, because we
+ * may want to perform additional actions on any kind of bell (for
+ * example, taskbar flashing in Windows).
+ */
+void beep(void *frontend, int mode)
+{
+ if (mode != BELL_VISUAL)
+ gdk_beep();
+}
+
+int char_width(Context ctx, int uc)
+{
+ /*
+ * Under X, any fixed-width font really _is_ fixed-width.
+ * Double-width characters will be dealt with using a separate
+ * font. For the moment we can simply return 1.
+ */
+ return 1;
+}
+
+Context get_ctx(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ struct draw_ctx *dctx;
+
+ if (!inst->area->window)
+ return NULL;
+
+ dctx = snew(struct draw_ctx);
+ dctx->inst = inst;
+ dctx->gc = gdk_gc_new(inst->area->window);
+ return dctx;
+}
+
+void free_ctx(Context ctx)
+{
+ struct draw_ctx *dctx = (struct draw_ctx *)ctx;
+ /* struct gui_data *inst = dctx->inst; */
+ GdkGC *gc = dctx->gc;
+ gdk_gc_unref(gc);
+ sfree(dctx);
+}
+
+/*
+ * Draw a line of text in the window, at given character
+ * coordinates, in given attributes.
+ *
+ * We are allowed to fiddle with the contents of `text'.
+ */
+void do_text_internal(Context ctx, int x, int y, char *text, int len,
+ unsigned long attr, int lattr)
+{
+ struct draw_ctx *dctx = (struct draw_ctx *)ctx;
+ struct gui_data *inst = dctx->inst;
+ GdkGC *gc = dctx->gc;
+
+ int nfg, nbg, t, fontid, shadow, rlen, widefactor;
+
+ nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
+ nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
+ nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
+ nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
+ if (attr & ATTR_REVERSE) {
+ t = nfg;
+ nfg = nbg;
+ nbg = t;
+ }
+ if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
+ nfg |= 1;
+ if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
+ nbg |= 1;
+ if (attr & TATTR_ACTCURS) {
+ nfg = NCOLOURS-2;
+ nbg = NCOLOURS-1;
+ }
+
+ fontid = shadow = 0;
+
+ if (attr & ATTR_WIDE) {
+ widefactor = 2;
+ fontid |= 2;
+ } else {
+ widefactor = 1;
+ }
+
+ if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
+ if (inst->fonts[fontid | 1])
+ fontid |= 1;
+ else
+ shadow = 1;
+ }
+
+ if (lattr != LATTR_NORM) {
+ x *= 2;
+ if (x >= inst->term->cols)
+ return;
+ if (x + len*2*widefactor > inst->term->cols)
+ len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
+ rlen = len * 2;
+ } else
+ rlen = len;
+
+ {
+ GdkRectangle r;
+
+ r.x = x*inst->font_width+inst->cfg.window_border;
+ r.y = y*inst->font_height+inst->cfg.window_border;
+ r.width = rlen*widefactor*inst->font_width;
+ r.height = inst->font_height;
+ gdk_gc_set_clip_rectangle(gc, &r);
+ }
+
+ gdk_gc_set_foreground(gc, &inst->cols[nbg]);
+ gdk_draw_rectangle(inst->pixmap, gc, 1,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ rlen*widefactor*inst->font_width, inst->font_height);
+
+ gdk_gc_set_foreground(gc, &inst->cols[nfg]);
+ {
+ GdkWChar *gwcs;
+ gchar *gcs;
+ wchar_t *wcs;
+ int i;
+
+ wcs = snewn(len+1, wchar_t);
+ for (i = 0; i < len; i++) {
+ wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
+ }
+
+ if (inst->fonts[fontid] == NULL) {
+ /*
+ * The font for this contingency does not exist.
+ * Typically this means we've been given ATTR_WIDE
+ * character and have no wide font. So we display
+ * nothing at all; such is life.
+ */
+ } else if (inst->fontinfo[fontid].is_wide) {
+ /*
+ * At least one version of gdk_draw_text_wc() has a
+ * weird bug whereby it reads `len' elements of the
+ * input string, but only draws `len/2'. Hence I'm
+ * going to make its input array twice as long as it
+ * theoretically needs to be, and pass in twice the
+ * actual number of characters. If a fixed gdk actually
+ * takes the doubled length seriously, then (a) the
+ * array will stand scrutiny up to the full length, (b)
+ * the spare elements of the array are full of zeroes
+ * which will probably be an empty glyph in the font,
+ * and (c) the clip rectangle should prevent it causing
+ * trouble anyway.
+ */
+ gwcs = snewn(len*2+1, GdkWChar);
+ memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
+ /*
+ * FIXME: when we have a wide-char equivalent of
+ * from_unicode, use it instead of this.
+ */
+ for (i = 0; i <= len; i++)
+ gwcs[i] = wcs[i];
+ gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
+ gwcs, len*2);
+ sfree(gwcs);
+ } else {
+ gcs = snewn(len+1, gchar);
+ wc_to_mb(inst->fontinfo[fontid].charset, 0,
+ wcs, len, gcs, len, ".", NULL, NULL);
+ gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
+ gcs, len);
+ sfree(gcs);
+ }
+ sfree(wcs);
+ }
+
+ if (shadow) {
+ gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
+ x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
+ y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
+ text, len);
+ }
+
+ if (attr & ATTR_UNDER) {
+ int uheight = inst->fonts[0]->ascent + 1;
+ if (uheight >= inst->font_height)
+ uheight = inst->font_height - 1;
+ gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height + uheight + inst->cfg.window_border,
+ (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
+ y*inst->font_height + uheight + inst->cfg.window_border);
+ }
+
+ if (lattr != LATTR_NORM) {
+ /*
+ * I can't find any plausible StretchBlt equivalent in the
+ * X server, so I'm going to do this the slow and painful
+ * way. This will involve repeated calls to
+ * gdk_draw_pixmap() to stretch the text horizontally. It's
+ * O(N^2) in time and O(N) in network bandwidth, but you
+ * try thinking of a better way. :-(
+ */
+ int i;
+ for (i = 0; i < len * widefactor * inst->font_width; i++) {
+ gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
+ x*inst->font_width+inst->cfg.window_border + 2*i,
+ y*inst->font_height+inst->cfg.window_border,
+ x*inst->font_width+inst->cfg.window_border + 2*i+1,
+ y*inst->font_height+inst->cfg.window_border,
+ len * inst->font_width - i, inst->font_height);
+ }
+ len *= 2;
+ if (lattr != LATTR_WIDE) {
+ int dt, db;
+ /* Now stretch vertically, in the same way. */
+ if (lattr == LATTR_BOT)
+ dt = 0, db = 1;
+ else
+ dt = 1, db = 0;
+ for (i = 0; i < inst->font_height; i+=2) {
+ gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border+dt*i+db,
+ x*widefactor*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border+dt*(i+1),
+ len * inst->font_width, inst->font_height-i-1);
+ }
+ }
+ }
+}
+
+void do_text(Context ctx, int x, int y, char *text, int len,
+ unsigned long attr, int lattr)
+{
+ struct draw_ctx *dctx = (struct draw_ctx *)ctx;
+ struct gui_data *inst = dctx->inst;
+ GdkGC *gc = dctx->gc;
+ int widefactor;
+
+ do_text_internal(ctx, x, y, text, len, attr, lattr);
+
+ if (attr & ATTR_WIDE) {
+ widefactor = 2;
+ } else {
+ widefactor = 1;
+ }
+
+ if (lattr != LATTR_NORM) {
+ x *= 2;
+ if (x >= inst->term->cols)
+ return;
+ if (x + len*2*widefactor > inst->term->cols)
+ len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
+ len *= 2;
+ }
+
+ gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ len*widefactor*inst->font_width, inst->font_height);
+}
+
+void do_cursor(Context ctx, int x, int y, char *text, int len,
+ unsigned long attr, int lattr)
+{
+ struct draw_ctx *dctx = (struct draw_ctx *)ctx;
+ struct gui_data *inst = dctx->inst;
+ GdkGC *gc = dctx->gc;
+
+ int passive, widefactor;
+
+ if (attr & TATTR_PASCURS) {
+ attr &= ~TATTR_PASCURS;
+ passive = 1;
+ } else
+ passive = 0;
+ if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
+ attr &= ~TATTR_ACTCURS;
+ }
+ do_text_internal(ctx, x, y, text, len, attr, lattr);
+
+ if (attr & ATTR_WIDE) {
+ widefactor = 2;
+ } else {
+ widefactor = 1;
+ }
+
+ if (lattr != LATTR_NORM) {
+ x *= 2;
+ if (x >= inst->term->cols)
+ return;
+ if (x + len*2*widefactor > inst->term->cols)
+ len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
+ len *= 2;
+ }
+
+ if (inst->cfg.cursor_type == 0) {
+ /*
+ * An active block cursor will already have been done by
+ * the above do_text call, so we only need to do anything
+ * if it's passive.
+ */
+ if (passive) {
+ gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
+ gdk_draw_rectangle(inst->pixmap, gc, 0,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ len*inst->font_width-1, inst->font_height-1);
+ }
+ } else {
+ int uheight;
+ int startx, starty, dx, dy, length, i;
+
+ int char_width;
+
+ if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
+ char_width = 2*inst->font_width;
+ else
+ char_width = inst->font_width;
+
+ if (inst->cfg.cursor_type == 1) {
+ uheight = inst->fonts[0]->ascent + 1;
+ if (uheight >= inst->font_height)
+ uheight = inst->font_height - 1;
+
+ startx = x * inst->font_width + inst->cfg.window_border;
+ starty = y * inst->font_height + inst->cfg.window_border + uheight;
+ dx = 1;
+ dy = 0;
+ length = len * char_width;
+ } else {
+ int xadjust = 0;
+ if (attr & TATTR_RIGHTCURS)
+ xadjust = char_width - 1;
+ startx = x * inst->font_width + inst->cfg.window_border + xadjust;
+ starty = y * inst->font_height + inst->cfg.window_border;
+ dx = 0;
+ dy = 1;
+ length = inst->font_height;
+ }
+
+ gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
+ if (passive) {
+ for (i = 0; i < length; i++) {
+ if (i % 2 == 0) {
+ gdk_draw_point(inst->pixmap, gc, startx, starty);
+ }
+ startx += dx;
+ starty += dy;
+ }
+ } else {
+ gdk_draw_line(inst->pixmap, gc, startx, starty,
+ startx + (length-1) * dx, starty + (length-1) * dy);
+ }
+ }
+
+ gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ x*inst->font_width+inst->cfg.window_border,
+ y*inst->font_height+inst->cfg.window_border,
+ len*widefactor*inst->font_width, inst->font_height);
+}
+
+GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
+{
+ /*
+ * Truly hideous hack: GTK doesn't allow us to set the mouse
+ * cursor foreground and background colours unless we've _also_
+ * created our own cursor from bitmaps. Therefore, I need to
+ * load the `cursor' font and draw glyphs from it on to
+ * pixmaps, in order to construct my cursors with the fg and bg
+ * I want. This is a gross hack, but it's more self-contained
+ * than linking in Xlib to find the X window handle to
+ * inst->area and calling XRecolorCursor, and it's more
+ * futureproof than hard-coding the shapes as bitmap arrays.
+ */
+ static GdkFont *cursor_font = NULL;
+ GdkPixmap *source, *mask;
+ GdkGC *gc;
+ GdkColor cfg = { 0, 65535, 65535, 65535 };
+ GdkColor cbg = { 0, 0, 0, 0 };
+ GdkColor dfg = { 1, 65535, 65535, 65535 };
+ GdkColor dbg = { 0, 0, 0, 0 };
+ GdkCursor *ret;
+ gchar text[2];
+ gint lb, rb, wid, asc, desc, w, h, x, y;
+
+ if (cursor_val == -2) {
+ gdk_font_unref(cursor_font);
+ return NULL;
+ }
+
+ if (cursor_val >= 0 && !cursor_font)
+ cursor_font = gdk_font_load("cursor");
+
+ /*
+ * Get the text extent of the cursor in question. We use the
+ * mask character for this, because it's typically slightly
+ * bigger than the main character.
+ */
+ if (cursor_val >= 0) {
+ text[1] = '\0';
+ text[0] = (char)cursor_val + 1;
+ gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
+ w = rb-lb; h = asc+desc; x = -lb; y = asc;
+ } else {
+ w = h = 1;
+ x = y = 0;
+ }
+
+ source = gdk_pixmap_new(NULL, w, h, 1);
+ mask = gdk_pixmap_new(NULL, w, h, 1);
+
+ /*
+ * Draw the mask character on the mask pixmap.
+ */
+ gc = gdk_gc_new(mask);
+ gdk_gc_set_foreground(gc, &dbg);
+ gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
+ if (cursor_val >= 0) {
+ text[1] = '\0';
+ text[0] = (char)cursor_val + 1;
+ gdk_gc_set_foreground(gc, &dfg);
+ gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
+ }
+ gdk_gc_unref(gc);
+
+ /*
+ * Draw the main character on the source pixmap.
+ */
+ gc = gdk_gc_new(source);
+ gdk_gc_set_foreground(gc, &dbg);
+ gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
+ if (cursor_val >= 0) {
+ text[1] = '\0';
+ text[0] = (char)cursor_val;
+ gdk_gc_set_foreground(gc, &dfg);
+ gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
+ }
+ gdk_gc_unref(gc);
+
+ /*
+ * Create the cursor.
+ */
+ ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
+
+ /*
+ * Clean up.
+ */
+ gdk_pixmap_unref(source);
+ gdk_pixmap_unref(mask);
+
+ return ret;
+}
+
+void modalfatalbox(char *p, ...)
+{
+ va_list ap;
+ fprintf(stderr, "FATAL ERROR: ");
+ va_start(ap, p);
+ vfprintf(stderr, p, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+ exit(1);
+}
+
+void cmdline_error(char *p, ...)
+{
+ va_list ap;
+ fprintf(stderr, "%s: ", appname);
+ va_start(ap, p);
+ vfprintf(stderr, p, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+ exit(1);
+}
+
+char *get_x_display(void *frontend)
+{
+ return gdk_get_display();
+}
+
+long get_windowid(void *frontend)
+{
+ struct gui_data *inst = (struct gui_data *)frontend;
+ return (long)GDK_WINDOW_XWINDOW(inst->area->window);
+}
+
+static void help(FILE *fp) {
+ if(fprintf(fp,
+"pterm option summary:\n"
+"\n"
+" --display DISPLAY Specify X display to use (note '--')\n"
+" -name PREFIX Prefix when looking up resources (default: pterm)\n"
+" -fn FONT Normal text font\n"
+" -fb FONT Bold text font\n"
+" -geometry GEOMETRY Position and size of window (size in characters)\n"
+" -sl LINES Number of lines of scrollback\n"
+" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
+" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
+" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
+" -T TITLE Window title\n"
+" -ut, +ut Do(default) or do not update utmp\n"
+" -ls, +ls Do(default) or do not make shell a login shell\n"
+" -sb, +sb Do(default) or do not display a scrollbar\n"
+" -log PATH Log all output to a file\n"
+" -nethack Map numeric keypad to hjklyubn direction keys\n"
+" -xrm RESOURCE-STRING Set an X resource\n"
+" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
+ ) < 0 || fflush(fp) < 0) {
+ perror("output error");
+ exit(1);
+ }
+}
+
+int do_cmdline(int argc, char **argv, int do_everything,
+ struct gui_data *inst, Config *cfg)
+{
+ int err = 0;
+ char *val;
+
+ /*
+ * Macros to make argument handling easier. Note that because
+ * they need to call `continue', they cannot be contained in
+ * the usual do {...} while (0) wrapper to make them
+ * syntactically single statements; hence it is not legal to
+ * use one of these macros as an unbraced statement between
+ * `if' and `else'.
+ */
+#define EXPECTS_ARG { \
+ if (--argc <= 0) { \
+ err = 1; \
+ fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
+ continue; \
+ } else \
+ val = *++argv; \
+}
+#define SECOND_PASS_ONLY { if (!do_everything) continue; }
+
+ while (--argc > 0) {
+ char *p = *++argv;
+ int ret;
+
+ /*
+ * Shameless cheating. Debian requires all X terminal
+ * emulators to support `-T title'; but
+ * cmdline_process_param will eat -T (it means no-pty) and
+ * complain that pterm doesn't support it. So, in pterm
+ * only, we convert -T into -title.
+ */
+ if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
+ !strcmp(p, "-T"))
+ p = "-title";
+
+ ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
+ do_everything ? 1 : -1, cfg);
+
+ if (ret == -2) {
+ cmdline_error("option \"%s\" requires an argument", p);
+ } else if (ret == 2) {
+ --argc, ++argv; /* skip next argument */
+ continue;
+ } else if (ret == 1) {
+ continue;
+ }
+
+ if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg->font.name, val, sizeof(cfg->font.name));
+ cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
+
+ } else if (!strcmp(p, "-fb")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
+ cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
+
+ } else if (!strcmp(p, "-fw")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
+ cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
+
+ } else if (!strcmp(p, "-fwb")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
+ cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
+
+ } else if (!strcmp(p, "-cs")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
+ cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
+
+ } else if (!strcmp(p, "-geometry")) {
+ int flags, x, y, w, h;
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;