1 /*----------------------------------------------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
8 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
9 * - extensive modifications
10 * Copyright (c) 1995 Garrett D'Amore <garrett@netcom.com>
12 * Copyright (c) 1995 Steven Hirsch <hirsch@emba.uvm.edu>
13 * - X11 mouse report mode and support for
14 * DEC "private mode" save/restore functions.
15 * Copyright (c) 1995 Jakub Jelinek <jj@gnu.ai.mit.edu>
16 * - key-related changes to handle Shift+function
18 * Copyright (c) 1997 MJ Olesen <olesen@me.queensu.ca>
19 * - extensive modifications
20 * Copyright (c) 1997 Raul Garcia Garcia <rgg@tid.es>
21 * - modification and cleanups for Solaris 2.x
23 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
24 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
25 * - extensive modifications
26 * Copyright (c) 1998 Alfredo K. Kojima <kojima@windowmaker.org>
27 * Copyright (c) 2001 Marius Gedminas
28 * - Ctrl/Mod4+Tab works like Meta+Tab (options)
29 * Copyright (c) 2003 Rob McMullen <robm@flipturn.org>
30 * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com>
31 * Copyright (c) 2007 Emanuele Giaquinta <e.giaquinta@glauco.it>
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46 *----------------------------------------------------------------------*/
49 #include "../config.h"
55 #ifdef KEYSYM_RESOURCE
56 # include "keyboard.h"
65 /*----------------------------------------------------------------------*/
67 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
69 #if ENABLE_FRILLS || ISO_14755
71 #define ISO_14755_STARTED 0x80000000UL
72 #define ISO_14755_51 0x40000000UL // basic (section 5.1)
73 #define ISO_14755_52 0x20000000UL // keycap (section 5.2)
74 #define ISO_14755_54 0x10000000UL // code feedback (section 5.4)
75 #define ISO_14755_MASK 0x0fffffffUL
78 static unsigned short iso14755_symtab[] = {
95 XK_ISO_Left_Tab, 0x21e6,
99 XK_Shift_Lock, 0x21eb,
101 XK_Caps_Lock, 0x21ec,
103 XK_ISO_Level3_Shift, 0x21ee,
104 XK_ISO_Level3_Lock, 0x21ef,
105 XK_ISO_Group_Lock, 0x21f0,
113 XK_BackSpace, 0x232b,
115 XK_Control_L, 0x2388,
116 XK_Control_R, 0x2388,
129 XK_KP_Insert, 0x2380,
130 XK_KP_Delete, 0x2326,
137 rxvt_term::iso14755_54 (int x, int y)
142 if (!IN_RANGE_EXC (x, 0, ncol)
143 || !IN_RANGE_EXC (y, 0, nrow))
148 const line_t &l = ROW(y + view_start);
152 if (t != NOCHAR || !x)
154 iso14755_51 (l.t[x], l.r[x], x, y);
155 iso14755buf = ISO_14755_54;
164 rxvt_term::iso14755_51 (unicode_t ch, rend_t r, int x, int y)
166 rxvt_fontset *fs = FONTSET (r);
167 wchar_t *chr, *alloc, ch2, **fname;
170 # if ENABLE_COMBINING
173 len = rxvt_composite.expand (ch, 0);
174 alloc = chr = new wchar_t[len];
175 rxvt_composite.expand (ch, chr);
187 char attr[80]; // plenty
189 sprintf (attr, "%08x = fg %d bg %d%s%s%s%s%s%s",
191 fgcolor_of (r), bgcolor_of (r),
192 r & RS_Bold ? " bold" : "",
193 r & RS_Italic ? " italic" : "",
194 r & RS_Blink ? " blink" : "",
195 r & RS_RVid ? " rvid" : "",
196 r & RS_Uline ? " uline" : "",
197 r & RS_Careful ? " careful" : "");
200 fname = rxvt_temp_buf<wchar_t *> (len);
201 for (int i = 0; i < len; i++)
203 rxvt_font *f = (*fs)[fs->find_font_idx (chr[i])];
204 fname[i] = rxvt_utf8towcs (f->name);
205 max_it (width, wcswidth (fname[i], wcslen (fname[i])));
208 max_it (width, 8+5); // for char + hex
209 max_it (width, strlen (attr));
213 y = (y >= nrow - len - 4 && x < width + 2) ? 0 : -1;
217 scr_overlay_new (x, y, width, len * 2 + 1);
219 r = SET_STYLE (OVERLAY_RSTYLE, GET_STYLE (r));
221 for (int y = 0; y < len; y++)
227 sprintf (buf, "%8x", ch);
228 scr_overlay_set (0, y, buf);
229 scr_overlay_set (9, y, '=');
234 scr_overlay_set (11, y, ch, r);
236 if (WCWIDTH (ch) >= 2)
237 scr_overlay_set (12, y, NOCHAR, r);
241 // char buf[4+4+3+1];
242 // snprintf (buf, sizeof (buf), "(%.4d|%.4d)", x, y);
243 // scr_overlay_set (0, 0, buf);
245 scr_overlay_set (0, len , attr);
246 for (int i = 0; i < len; i++)
248 scr_overlay_set (0, len + 1 + i, fname[i]);
252 # if ENABLE_COMBINING
260 rxvt_term::commit_iso14755 ()
264 ch[0] = iso14755buf & ISO_14755_MASK;
267 if (iso14755buf & ISO_14755_51)
272 // allow verbatim 0-bytes and control-bytes to be entered
274 len = wcstombs (mb, ch, 16);
291 hex_keyval (XKeyEvent &ev)
293 // check whether this event corresponds to a hex digit
294 // if the modifiers had not been pressed.
295 for (int index = 0; index < 8; index++)
297 KeySym k = XLookupKeysym (&ev, index);
299 if (k >= XK_KP_0 && k <= XK_KP_9) return k - XK_KP_0;
300 else if (k >= XK_0 && k <= XK_9) return k - XK_0;
301 else if (k >= XK_a && k <= XK_f) return k - XK_a + 10;
302 else if (k >= XK_A && k <= XK_F) return k - XK_A + 10;
310 translate_keypad (KeySym keysym, bool kp)
313 static const KeySym keypadtrans[] = {
314 XK_KP_7, // XK_KP_Home
315 XK_KP_4, // XK_KP_Left
317 XK_KP_6, // XK_KP_Right
318 XK_KP_2, // XK_KP_Down
319 # ifndef UNSHIFTED_SCROLLKEYS
320 XK_KP_9, // XK_KP_Prior
321 XK_KP_3, // XK_KP_Next
326 XK_KP_1, // XK_KP_End
327 XK_KP_5, // XK_KP_Begin
330 if (IN_RANGE_INC (keysym, XK_KP_Home, XK_KP_Begin))
332 unsigned int index = keysym - XK_KP_Home;
333 keysym = kp ? keypadtrans[index] : XK_Home + index;
335 else if (keysym == XK_KP_Insert)
336 keysym = kp ? XK_KP_0 : XK_Insert;
337 # ifndef NO_DELETE_KEY
338 else if (keysym == XK_KP_Delete)
339 keysym = kp ? XK_KP_Decimal : XK_Delete;
346 map_function_key (KeySym keysym)
350 if (IN_RANGE_INC (keysym, XK_F1, XK_F35))
352 param = 11 + keysym - XK_F1;
353 if (keysym >= XK_F17)
355 else if (keysym >= XK_F15)
357 else if (keysym >= XK_F11)
359 else if (keysym >= XK_F6)
380 #ifndef UNSHIFTED_SCROLLKEYS
405 rxvt_term::update_user_time (Time time)
407 XChangeProperty (dpy, parent[0], xa[XA_NET_WM_USER_TIME], XA_CARDINAL,
408 32, PropModeReplace, (unsigned char*)&time, 1);
412 rxvt_term::key_press (XKeyEvent &ev)
414 int ctrl, meta, shft, len;
420 if (iso14755buf & ISO_14755_52)
425 * use Num_Lock to toggle Keypad on/off. If Num_Lock is off, allow an
426 * escape sequence to toggle the Keypad.
428 * Always permit `shift' to override the current setting
430 shft = ev.state & ShiftMask;
431 ctrl = ev.state & ControlMask;
432 meta = ev.state & ModMetaMask;
439 Status status_return;
442 #ifdef X_HAVE_UTF8_STRING
443 if (enc_utf8 && 0) // currently disabled, doesn't seem to work, nor is useful
444 len = Xutf8LookupString (Input_Context, &ev, kbuf,
445 KBUFSZ, &keysym, &status_return);
450 wchar_t wkbuf[KBUFSZ + 1];
452 // the XOpenIM manpage lies about hardcoding the locale
453 // at the point of XOpenIM, so temporarily switch locales
455 SET_LOCALE (rs[Rs_imLocale]);
457 // assume wchar_t == unicode or better
458 len = XwcLookupString (Input_Context, &ev, wkbuf,
459 KBUFSZ, &keysym, &status_return);
464 if (status_return == XLookupChars
465 || status_return == XLookupBoth)
467 /* make sure the user can type ctrl-@, i.e. NUL */
468 if (len == 1 && *wkbuf == 0)
476 len = wcstombs ((char *)kbuf, wkbuf, KBUFSZ);
485 valid_keysym = status_return == XLookupKeySym
486 || status_return == XLookupBoth;
491 len = XLookupString (&ev, kbuf, KBUFSZ, &keysym, &compose);
492 valid_keysym = keysym != NoSymbol;
497 #ifdef KEYSYM_RESOURCE
498 if (keyboard->dispatch (this, keysym, ev.state))
504 #ifdef UNSHIFTED_SCROLLKEYS
512 #ifdef PAGING_CONTEXT_LINES
513 lnsppg = nrow - PAGING_CONTEXT_LINES;
515 lnsppg = nrow * 4 / 5;
519 if (keysym == XK_Prior)
521 scr_page (UP, lnsppg);
524 else if (keysym == XK_Next)
526 scr_page (DN, lnsppg);
530 #ifdef SCROLL_ON_UPDOWN_KEYS
538 else if (keysym == XK_Down)
545 #ifdef SCROLL_ON_HOMEEND_KEYS
548 if (keysym == XK_Home)
553 else if (keysym == XK_End)
564 /* Shift + F1 - F10 generates F11 - F20 */
565 if (keysym >= XK_F1 && keysym <= XK_F10)
567 keysym += (XK_F11 - XK_F1);
568 shft = 0; /* turn off Shift */
570 else if (!ctrl && !meta && (priv_modes & PrivMode_ShiftKeys))
574 /* normal XTerm key bindings */
575 case XK_Insert: /* Shift+Insert = paste mouse selection */
576 selection_request (ev.time);
580 case XK_KP_Add: /* Shift+KP_Add = bigger font */
581 change_font (FONT_UP);
583 case XK_KP_Subtract: /* Shift+KP_Subtract = smaller font */
584 change_font (FONT_DN);
591 #if ENABLE_FRILLS || ISO_14755
593 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_51))
597 if (iso14755buf & ISO_14755_51
598 && (keysym == XK_space || keysym == XK_KP_Space
599 || keysym == XK_Return || keysym == XK_KP_Enter))
602 iso14755buf = ISO_14755_51;
608 else if (keysym == XK_C)
610 selection_make (ev.time, true);
617 else if (keysym == XK_V)
619 selection_request (ev.time, Sel_Clipboard);
626 else if (keysym == XK_BackSpace)
628 iso14755buf = ((iso14755buf & ISO_14755_MASK) >> 4) | ISO_14755_51;
630 iso14755_51 (iso14755buf & ISO_14755_MASK);
634 else if ((hv = hex_keyval (ev)) >= 0)
636 iso14755buf = ((iso14755buf << 4) & ISO_14755_MASK)
639 iso14755_51 (iso14755buf & ISO_14755_MASK);
651 else if (option (Opt_iso14755) &&
652 ((ctrl && (keysym == XK_Shift_L || keysym == XK_Shift_R))
653 || (shft && (keysym == XK_Control_L || keysym == XK_Control_R))))
654 if (!(iso14755buf & ISO_14755_STARTED))
656 iso14755buf |= ISO_14755_STARTED;
658 scr_overlay_new (0, -1, sizeof ("ISO 14755 mode") - 1, 1);
659 scr_overlay_set (0, 0, "ISO 14755 mode");
665 if (keysym == XK_Print)
667 scr_printscreen (ctrl | shft);
672 if (keysym >= 0xFF00 && keysym <= 0xFFFF)
674 bool kp = priv_modes & PrivMode_aplKP ? !shft : shft;
675 unsigned int newlen = 1;
677 if (ev.state & ModNumLockMask)
680 keysym = translate_keypad (keysym, kp);
684 #ifndef NO_BACKSPACE_KEY
686 if (priv_modes & PrivMode_HaveBackSpace)
688 kbuf[0] = (!! (priv_modes & PrivMode_BackSpace)
689 ^ !!ctrl) ? '\b' : '\177';
693 strcpy (kbuf, rs[Rs_backspace_key]);
696 #ifndef NO_DELETE_KEY
698 strcpy (kbuf, rs[Rs_delete_key]);
703 strcpy (kbuf, "\033[Z");
706 #ifdef CTRL_TAB_MAKES_META
710 #ifdef MOD4_TAB_MAKES_META
711 if (ev.state & Mod4Mask)
718 case XK_Up: /* "\033[A" */
719 case XK_Down: /* "\033[B" */
720 case XK_Right: /* "\033[C" */
721 case XK_Left: /* "\033[D" */
722 strcpy (kbuf, "\033[Z");
723 kbuf[2] = "DACB"[keysym - XK_Left];
726 kbuf[2] = "dacb"[keysym - XK_Left];
730 kbuf[2] = "dacb"[keysym - XK_Left];
732 else if (priv_modes & PrivMode_aplCUR)
737 /* allow shift to override */
740 strcpy (kbuf, "\033OM");
747 if (priv_modes & PrivMode_LFNL)
760 case XK_KP_F1: /* "\033OP" */
761 case XK_KP_F2: /* "\033OQ" */
762 case XK_KP_F3: /* "\033OR" */
763 case XK_KP_F4: /* "\033OS" */
764 strcpy (kbuf, "\033OP");
765 kbuf[2] += (keysym - XK_KP_F1);
768 case XK_KP_Multiply: /* "\033Oj" : "*" */
769 case XK_KP_Add: /* "\033Ok" : "+" */
770 case XK_KP_Separator: /* "\033Ol" : "," */
771 case XK_KP_Subtract: /* "\033Om" : "-" */
772 case XK_KP_Decimal: /* "\033On" : "." */
773 case XK_KP_Divide: /* "\033Oo" : "/" */
774 case XK_KP_0: /* "\033Op" : "0" */
775 case XK_KP_1: /* "\033Oq" : "1" */
776 case XK_KP_2: /* "\033Or" : "2" */
777 case XK_KP_3: /* "\033Os" : "3" */
778 case XK_KP_4: /* "\033Ot" : "4" */
779 case XK_KP_5: /* "\033Ou" : "5" */
780 case XK_KP_6: /* "\033Ov" : "6" */
781 case XK_KP_7: /* "\033Ow" : "7" */
782 case XK_KP_8: /* "\033Ox" : "8" */
783 case XK_KP_9: /* "\033Oy" : "9" */
784 /* allow shift to override */
787 strcpy (kbuf, "\033Oj");
788 kbuf[2] += (keysym - XK_KP_Multiply);
792 kbuf[0] = ('*' + (keysym - XK_KP_Multiply));
799 int param = map_function_key (keysym);
801 sprintf (kbuf,"\033[%d~", param);
812 * Pass meta for all function keys, if 'meta' option set
815 if (meta && (meta_char == 0x80) && len > 0)
816 kbuf[len - 1] |= 0x80;
820 else if (ctrl && keysym == XK_minus)
823 kbuf[0] = '\037'; /* Ctrl-Minus generates ^_ (31) */
825 else if (keysym == XK_ISO_Left_Tab)
827 strcpy (kbuf, "\033[Z");
834 if (meta && (meta_char == 0x80))
838 for (ch = kbuf; ch < kbuf + len; ch++)
848 if (HOOK_INVOKE ((this, HOOK_KEY_PRESS, DT_XEVENT, &ev, DT_INT, keysym, DT_STR_LEN, kbuf, len, DT_END)))
852 return; /* not mapped */
854 if (option (Opt_scrollTtyKeypress))
862 * these modifications only affect the static keybuffer
863 * pass Shift/Control indicators for function keys ending with `~'
867 * Shift+Prior = "ESC[5$"
868 * Ctrl+Prior = "ESC[5^"
869 * Ctrl+Shift+Prior = "ESC[5@"
870 * Meta adds an Escape prefix (with META8_OPTION, if meta == <escape>).
872 if (kbuf[0] == C0_ESC && kbuf[1] == '[' && kbuf[len - 1] == '~')
873 kbuf[len - 1] = (shft ? (ctrl ? '@' : '$') : (ctrl ? '^' : '~'));
878 && meta_char == C0_ESC
882 const char ch = C0_ESC;
886 tt_write (kbuf, (unsigned int)len);
890 rxvt_term::key_release (XKeyEvent &ev)
892 #if (MOUSE_WHEEL && MOUSE_SLIP_WHEELING) || ISO_14755 || ENABLE_PERL
895 keysym = XLookupKeysym (&ev, ev.state & ShiftMask ? 1 : 0); // sorry, only shift supported :/
898 #if ENABLE_FRILLS || ISO_14755
901 if (iso14755buf & ISO_14755_52)
907 // iso14755 part 5.2 handling: release time
909 if ((ev.state & ControlMask)
910 && ((keysym >= 0x40 && keysym <= 0x5f)
911 || (keysym >= 0x61 && keysym <= 0x7f)))
913 iso14755buf = ISO_14755_51 | 0x2400 | (keysym & 0x1f);
919 for (unsigned short *i = iso14755_symtab; i[0]; i+= 2)
922 iso14755buf = ISO_14755_51 | i[1];
934 else if ((ev.state & (ShiftMask | ControlMask)) != (ShiftMask | ControlMask))
939 if (iso14755buf & ISO_14755_51)
942 else if (option (Opt_iso14755_52) && iso14755buf & ISO_14755_STARTED)
944 iso14755buf = ISO_14755_52; // iso14755 part 5.2: remember empty begin/end pair
946 scr_overlay_new (0, -1, sizeof ("KEYCAP PICTURE INSERT MODE") - 1, 1);
947 scr_overlay_set (0, 0, "KEYCAP PICTURE INSERT MODE");
955 if (HOOK_INVOKE ((this, HOOK_KEY_RELEASE, DT_XEVENT, &ev, DT_INT, keysym, DT_END)))
958 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
959 if (!(ev.state & ControlMask))
960 slip_wheel_ev.stop ();
961 else if (keysym == XK_Control_L || keysym == XK_Control_R)
962 mouse_slip_wheel_speed = 0;
966 #if defined (KEYSYM_RESOURCE)
968 rxvt_term::cmd_write (const char *str, unsigned int count)
972 n = cmdbuf_ptr - cmdbuf_base;
973 s = cmdbuf_base + CBUFSIZ - 1 - cmdbuf_endp;
975 if (n > 0 && s < count)
977 memmove (cmdbuf_base, cmdbuf_ptr,
978 (unsigned int) (cmdbuf_endp - cmdbuf_ptr));
979 cmdbuf_ptr = cmdbuf_base;
986 rxvt_warn ("data loss: cmd_write too large, continuing.\n");
991 *cmdbuf_endp++ = *str++;
1004 #ifdef HAVE_BG_PIXMAP
1005 if (bgPixmap.check_clearChanged ())
1007 // scr_clear (true); This needs to be researched further!
1014 if (SHOULD_INVOKE (HOOK_LINE_UPDATE))
1016 int row = view_start;
1017 int end_row = row + nrow;
1019 while (row > top_row && ROW (row - 1).is_longer ())
1024 int start_row = row;
1031 if (!(l->f & LINE_FILTERED))
1033 // line not filtered, mark it as filtered
1034 l->f |= LINE_FILTERED;
1035 while (l->is_longer ())
1038 l->f |= LINE_FILTERED;
1042 HOOK_INVOKE ((this, HOOK_LINE_UPDATE, DT_INT, start_row, DT_END));
1047 while (l->is_longer () && row < end_row);
1049 while (row < end_row);
1062 /* checks whether a refresh is requested and starts the refresh timer */
1064 rxvt_term::refresh_check ()
1066 if (want_refresh && !flush_ev.is_active ())
1067 flush_ev.start (1. / 60.); // refresh at max. 60 Hz normally
1073 rxvt_term::flush_cb (ev::timer &w, int revents)
1083 rxvt_term::cursor_blink_cb (ev::timer &w, int revents)
1085 hidden_cursor = !hidden_cursor;
1093 rxvt_term::text_blink_cb (ev::timer &w, int revents)
1095 if (scr_refresh_rend (RS_Blink, RS_Blink))
1097 hidden_text = !hidden_text;
1106 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
1108 rxvt_term::cont_scroll_cb (ev::timer &w, int revents)
1110 if ((scrollBar.state == STATE_UP || scrollBar.state == STATE_DOWN)
1111 && scr_page (scrollBar.state == STATE_UP ? UP : DN, 1))
1121 #ifdef SELECTION_SCROLLING
1123 rxvt_term::sel_scroll_cb (ev::timer &w, int revents)
1125 if (scr_page (scroll_selection_dir, scroll_selection_lines))
1127 selection_extend (selection_save_x, selection_save_y, selection_save_state);
1136 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1138 rxvt_term::slip_wheel_cb (ev::timer &w, int revents)
1140 if (scr_changeview (view_start - mouse_slip_wheel_speed))
1146 if (view_start == top_row || view_start == 0 || mouse_slip_wheel_speed == 0)
1148 mouse_slip_wheel_speed = 0;
1154 #if LINUX_YIELD_HACK
1155 static struct event_handler
1157 ev::prepare yield_ev;
1159 void yield_cb (ev::prepare &w, int revents)
1161 // this should really be sched_yield(), but the linux guys thought
1162 // that giving a process calling sched_yield () less cpu time than
1163 // ones with high nice levels is a useful thing to do. It surely is is
1164 // allowed by the sus... as is returning ENOSYS.
1165 // since the linux guys additionally thought that breaking the only
1166 // known workaround against their unusable sched_yield hack is cool,
1167 // we just nanosleep a bit and hope for the best.
1169 struct timespec ts = { 0, 1000 };
1176 : yield_ev (this, &event_handler::yield_cb)
1183 rxvt_term::pty_fill ()
1185 ssize_t n = cmdbuf_endp - cmdbuf_ptr;
1189 rxvt_warn ("PLEASE REPORT: pty_fill on full buffer, draining input, continuing.\n");
1193 memmove (cmdbuf_base, cmdbuf_ptr, n);
1194 cmdbuf_ptr = cmdbuf_base;
1195 cmdbuf_endp = cmdbuf_ptr + n;
1197 ssize_t r = read (pty->pty, cmdbuf_endp, CBUFSIZ - n);
1204 else if (r < 0 && (errno == EAGAIN || errno == EINTR))
1206 #if LINUX_YIELD_HACK
1207 if (display->is_local)
1208 event_handler.yield_ev.start ();
1215 if (!option (Opt_hold))
1223 rxvt_term::pty_cb (ev::io &w, int revents)
1227 if (revents & ev::READ)
1228 // loop, but don't allow a single term to monopolize us
1229 for (int i = CBUFCNT; i-- && pty_fill (); )
1232 if (revents & ev::WRITE)
1239 rxvt_term::pointer_unblank ()
1241 XDefineCursor (dpy, vt, TermWin_cursor);
1244 #ifdef POINTER_BLANK
1247 if (option (Opt_pointerBlank))
1248 pointer_ev.start (pointerBlankDelay);
1252 #ifdef POINTER_BLANK
1254 rxvt_term::pointer_blank ()
1256 if (!option (Opt_pointerBlank))
1259 XDefineCursor (dpy, vt, display->blank_cursor);
1266 rxvt_term::pointer_cb (ev::timer &w, int revents)
1275 rxvt_term::mouse_report (XButtonEvent &ev)
1277 int button_number, key_state = 0;
1281 x = Pixel2Col (ev.x);
1282 y = Pixel2Row (ev.y);
1283 if (ev.type == MotionNotify)
1285 if (x == mouse_row && y == mouse_col)
1292 if (MEvent.button == AnyButton)
1296 button_number = MEvent.button - Button1;
1297 /* add 0x3D for wheel events, like xterm does */
1298 if (button_number >= 3)
1299 button_number += (64 - 3);
1302 if (priv_modes & PrivMode_MouseX10)
1305 * do not report ButtonRelease
1306 * no state info allowed
1309 if (button_number == 3)
1314 /* XTerm mouse reporting needs these values:
1318 * plus will add in our own Double-Click reporting
1321 key_state = ((MEvent.state & ShiftMask) ? 4 : 0)
1322 + ((MEvent.state & ModMetaMask) ? 8 : 0)
1323 + ((MEvent.state & ControlMask) ? 16 : 0);
1324 #ifdef MOUSE_REPORT_DOUBLECLICK
1325 key_state += ((MEvent.clicks > 1) ? 32 : 0);
1329 #if DEBUG_MOUSEREPORT
1330 fprintf (stderr, "Mouse [");
1332 fputc ('C', stderr);
1334 fputc ('S', stderr);
1336 fputc ('A', stderr);
1338 fputc ('2', stderr);
1339 fprintf (stderr, "]: <%d>, %d/%d\n",
1345 tt_printf ("\033[M%c%c%c",
1346 (code + button_number + key_state),
1351 /*{{{ process an X event */
1353 rxvt_term::x_cb (XEvent &ev)
1357 dLocal (Display *, dpy);
1359 if (ev.xany.window == vt
1360 && SHOULD_INVOKE (HOOK_X_EVENT)
1361 && HOOK_INVOKE ((this, HOOK_X_EVENT, DT_XEVENT, &ev, DT_END)))
1364 // for XQueryPointer
1365 Window unused_root, unused_child;
1366 int unused_root_x, unused_root_y;
1367 unsigned int unused_mask;
1372 update_user_time (ev.xkey.time);
1373 key_press (ev.xkey);
1377 key_release (ev.xkey);
1381 update_user_time (ev.xbutton.time);
1382 button_press (ev.xbutton);
1386 button_release (ev.xbutton);
1390 if (ev.xclient.format == 32
1391 && !HOOK_INVOKE ((this, HOOK_CLIENT_MESSAGE, DT_XEVENT, &ev, DT_END)))
1393 if (ev.xclient.message_type == xa[XA_WM_PROTOCOLS])
1395 if (!HOOK_INVOKE ((this, HOOK_WM_PROTOCOLS, DT_XEVENT, &ev, DT_END)))
1397 if (ev.xclient.data.l[0] == xa[XA_WM_DELETE_WINDOW])
1399 if (!HOOK_INVOKE ((this, HOOK_WM_DELETE_WINDOW, DT_XEVENT, &ev, DT_END)))
1403 else if (ev.xclient.data.l[0] == xa[XA_NET_WM_PING])
1404 XSendEvent (dpy, ev.xclient.window = display->root,
1405 False, SubstructureRedirectMask | SubstructureNotifyMask,
1411 else if (ev.xclient.format == 32 && ev.xclient.message_type == xa[XA_XEMBED])
1413 if (ev.xclient.data.l[1] == XEMBED_FOCUS_IN)
1415 else if (ev.xclient.data.l[1] == XEMBED_FOCUS_OUT)
1423 * XXX: this is not the _current_ arrangement
1424 * Here's my conclusion:
1425 * If the window is completely unobscured, use bitblt's
1426 * to scroll. Even then, they're only used when doing partial
1427 * screen scrolling. When partially obscured, we have to fill
1428 * in the GraphicsExpose parts, which means that after each refresh,
1429 * we need to wait for the graphics expose or Noexpose events,
1430 * which ought to make things real slow!
1432 case VisibilityNotify:
1433 switch (ev.xvisibility.state)
1435 case VisibilityUnobscured:
1436 refresh_type = FAST_REFRESH;
1438 case VisibilityPartiallyObscured:
1439 refresh_type = SLOW_REFRESH;
1442 refresh_type = NO_REFRESH;
1448 if (ev.xfocus.detail != NotifyInferior
1449 && ev.xfocus.detail != NotifyPointer
1450 && ev.xfocus.mode != NotifyGrab)
1455 if (ev.xfocus.detail != NotifyInferior
1456 && ev.xfocus.detail != NotifyPointer
1457 && ev.xfocus.mode != NotifyGrab)
1461 case ConfigureNotify:
1462 /*fprintf (stderr, "ConfigureNotify for %X, parent is %X, geom is %dx%d%+d%+d, old geom was %dx%d\n",
1463 ev.xconfigure.window, parent[0], ev.xconfigure.width, ev.xconfigure.height, ev.xconfigure.x, ev.xconfigure.y,
1464 szHint.width, szHint.height);*/
1465 if (ev.xconfigure.window == parent[0])
1467 while (XCheckTypedWindowEvent (dpy, ev.xconfigure.window, ConfigureNotify, &ev))
1470 if (szHint.width != ev.xconfigure.width || szHint.height != ev.xconfigure.height)
1473 resize_all_windows (ev.xconfigure.width, ev.xconfigure.height, 1);
1477 #ifdef HAVE_BG_PIXMAP
1478 if (bgPixmap.window_position_sensitive ())
1481 update_background ();
1483 bgPixmap.invalidate ();
1488 HOOK_INVOKE ((this, HOOK_CONFIGURE_NOTIFY, DT_XEVENT, &ev, DT_END));
1492 case PropertyNotify:
1493 if (!HOOK_INVOKE ((this, HOOK_PROPERTY_NOTIFY, DT_XEVENT, &ev, DT_END)))
1494 if (ev.xproperty.atom == xa[XA_VT_SELECTION]
1495 && ev.xproperty.state == PropertyNewValue)
1496 selection_property (ev.xproperty.window, ev.xproperty.atom);
1500 case SelectionClear:
1501 selection_clear (ev.xselectionclear.selection == xa[XA_CLIPBOARD]);
1504 case SelectionNotify:
1505 if (selection_wait == Sel_normal)
1506 selection_paste (ev.xselection.requestor, ev.xselection.property, true);
1509 case SelectionRequest:
1510 selection_send (ev.xselectionrequest);
1514 #ifdef HAVE_BG_PIXMAP
1515 /* This is needed spcifically to fix the case of no window manager or a
1516 * non-reparenting window manager. In those cases we never get first
1517 * ConfigureNotify. Also that speeds startup under normal WM, by taking
1518 * care of multiplicity of ConfigureNotify events arriwing while WM does
1520 * We should not render background immidiately, as there could be several
1521 * ConfigureNotify's to follow. Lets take care of all of them in one scoop
1522 * by scheduling background redraw as soon as we can, but giving a short
1523 * bit of time for ConfigureNotifies to arrive.
1524 * We should render background PRIOR to drawing any text, but AFTER all
1525 * of ConfigureNotifys for the best results.
1527 if (bgPixmap.flags & bgPixmap_t::isInvalid)
1528 update_background_ev.start (0.025);
1532 text_blink_ev.start ();
1534 HOOK_INVOKE ((this, HOOK_MAP_NOTIFY, DT_XEVENT, &ev, DT_END));
1540 text_blink_ev.stop ();
1542 HOOK_INVOKE ((this, HOOK_UNMAP_NOTIFY, DT_XEVENT, &ev, DT_END));
1545 case GraphicsExpose:
1547 if (ev.xany.window == vt)
1551 scr_expose (ev.xexpose.x, ev.xexpose.y,
1552 ev.xexpose.width, ev.xexpose.height, False);
1554 while (XCheckTypedWindowEvent (dpy, vt, ev.xany.type, &ev));
1556 ev.xany.type = ev.xany.type == Expose ? GraphicsExpose : Expose;
1558 while (XCheckTypedWindowEvent (dpy, vt, ev.xany.type, &ev))
1560 scr_expose (ev.xexpose.x, ev.xexpose.y,
1561 ev.xexpose.width, ev.xexpose.height, False);
1568 XEvent unused_event;
1570 while (XCheckTypedWindowEvent (dpy, ev.xany.window, Expose, &unused_event))
1572 while (XCheckTypedWindowEvent (dpy, ev.xany.window, GraphicsExpose, &unused_event))
1575 if (scrollBar.state && ev.xany.window == scrollBar.win)
1577 scrollBar.state = STATE_IDLE;
1584 #ifdef POINTER_BLANK
1588 if ((priv_modes & PrivMode_MouseBtnEvent && ev.xbutton.state & (Button1Mask|Button2Mask|Button3Mask))
1589 || priv_modes & PrivMode_MouseAnyEvent)
1590 mouse_report (ev.xbutton);
1591 if ((priv_modes & PrivMode_mouse_report) && !bypass_keystate)
1594 if (ev.xany.window == vt)
1596 if (SHOULD_INVOKE (HOOK_MOTION_NOTIFY)
1597 && HOOK_INVOKE ((this, HOOK_MOTION_NOTIFY, DT_XEVENT, &ev, DT_END)))
1599 else if (ev.xbutton.state & (Button1Mask | Button3Mask))
1601 while (XCheckTypedWindowEvent (dpy, vt, MotionNotify, &ev))
1604 XQueryPointer (dpy, vt,
1605 &unused_root, &unused_child,
1606 &unused_root_x, &unused_root_y,
1607 &ev.xbutton.x, &ev.xbutton.y,
1609 #ifdef MOUSE_THRESHOLD
1610 /* deal with a `jumpy' mouse */
1611 if ((ev.xmotion.time - MEvent.time) > MOUSE_THRESHOLD)
1616 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
1618 iso14755_54 (ev.xbutton.x, ev.xbutton.y);
1622 selection_extend (ev.xbutton.x, ev.xbutton.y,
1623 ev.xbutton.state & Button3Mask ? 2 : 0);
1625 #ifdef SELECTION_SCROLLING
1626 if (ev.xbutton.y < int_bwidth
1627 || Pixel2Row (ev.xbutton.y) > (nrow-1))
1631 /* don't clobber the current delay if we are
1632 * already in the middle of scrolling.
1634 if (!sel_scroll_ev.is_active ())
1635 sel_scroll_ev.start (SCROLLBAR_INITIAL_DELAY, SCROLLBAR_CONTINUOUS_DELAY);
1637 /* save the event params so we can highlight
1638 * the selection in the pending-scroll loop
1640 selection_save_x = ev.xbutton.x;
1641 selection_save_y = ev.xbutton.y;
1642 selection_save_state = (ev.xbutton.state & Button3Mask) ? 2 : 0;
1644 /* calc number of lines to scroll */
1645 if (ev.xbutton.y < int_bwidth)
1647 scroll_selection_dir = UP;
1648 dist = int_bwidth - ev.xbutton.y;
1652 scroll_selection_dir = DN;
1653 dist = ev.xbutton.y - (int_bwidth + height);
1656 scroll_selection_lines = Pixel2Height (dist)
1657 / SELECTION_SCROLL_LINE_SPEEDUP
1659 min_it (scroll_selection_lines,
1660 SELECTION_SCROLL_MAX_LINES);
1664 /* we are within the text window, so we
1665 * shouldn't be scrolling
1667 sel_scroll_ev.stop();
1670 #ifdef MOUSE_THRESHOLD
1675 else if (scrollBar.state == STATE_MOTION && ev.xany.window == scrollBar.win)
1677 while (XCheckTypedWindowEvent (dpy, scrollBar.win,
1681 XQueryPointer (dpy, scrollBar.win,
1682 &unused_root, &unused_child,
1683 &unused_root_x, &unused_root_y,
1684 &ev.xbutton.x, &ev.xbutton.y,
1686 scr_move_to (scrollbar_position (ev.xbutton.y) - csrO,
1694 #if defined(CURSOR_BLINK)
1695 if (option (Opt_cursorBlink) && ev.type == KeyPress)
1703 cursor_blink_ev.again ();
1707 #if defined(POINTER_BLANK)
1708 if (option (Opt_pointerBlank) && pointerBlankDelay > 0)
1710 if (ev.type == MotionNotify
1711 || ev.type == ButtonPress
1712 || ev.type == ButtonRelease)
1716 if (ev.type == KeyPress && hidden_pointer == 0)
1726 rxvt_term::set_urgency (bool enable)
1728 if (enable == urgency_hint)
1731 if (XWMHints *h = XGetWMHints (dpy, parent[0]))
1733 h->flags = h->flags & ~XUrgencyHint | (enable ? XUrgencyHint : 0);
1734 XSetWMHints (dpy, parent[0], h);
1735 urgency_hint = enable;
1741 rxvt_term::focus_in ()
1749 if (Input_Context != NULL)
1752 XSetICFocus (Input_Context);
1756 if (option (Opt_cursorBlink))
1757 cursor_blink_ev.again ();
1759 #if OFF_FOCUS_FADING
1762 pix_colors = pix_colors_focused;
1767 if (option (Opt_urgentOnBell))
1771 HOOK_INVOKE ((this, HOOK_FOCUS_IN, DT_END));
1776 rxvt_term::focus_out ()
1784 if (option (Opt_urgentOnBell))
1787 #if ENABLE_FRILLS || ISO_14755
1797 if (Input_Context != NULL)
1798 XUnsetICFocus (Input_Context);
1801 if (option (Opt_cursorBlink))
1802 cursor_blink_ev.stop ();
1806 #if OFF_FOCUS_FADING
1809 pix_colors = pix_colors_unfocused;
1814 HOOK_INVOKE ((this, HOOK_FOCUS_OUT, DT_END));
1819 rxvt_term::update_fade_color (unsigned int idx)
1821 #if OFF_FOCUS_FADING
1825 pix_colors [Color_fade].get (c);
1826 pix_colors_focused [idx].fade (this, atoi (rs[Rs_fade]), pix_colors_unfocused [idx], c);
1831 #if ENABLE_TRANSPARENCY || ENABLE_PERL
1833 rxvt_term::rootwin_cb (XEvent &ev)
1837 if (SHOULD_INVOKE (HOOK_ROOT_EVENT)
1838 && HOOK_INVOKE ((this, HOOK_ROOT_EVENT, DT_XEVENT, &ev, DT_END)))
1841 # if ENABLE_TRANSPARENCY
1844 case PropertyNotify:
1846 * if user used some Esetroot compatible prog to set the root bg,
1847 * use the property to determine the pixmap. We use it later on.
1849 if (ev.xproperty.atom == xa[XA_XROOTPMAP_ID]
1850 || ev.xproperty.atom == xa[XA_ESETROOT_PMAP_ID])
1852 bgPixmap.set_root_pixmap ();
1853 update_background ();
1865 rxvt_term::button_press (XButtonEvent &ev)
1867 int reportmode = 0, clickintime;
1869 button_state[ev.button - Button1] = true;
1871 bypass_keystate = ev.state & (ModMetaMask | ShiftMask);
1873 if (!bypass_keystate)
1874 reportmode = !! (priv_modes & PrivMode_mouse_report);
1877 * VT window processing of button press
1879 if (ev.window == vt)
1881 if (HOOK_INVOKE ((this, HOOK_BUTTON_PRESS, DT_XEVENT, &ev, DT_END)))
1886 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
1888 iso14755_54 (ev.x, ev.y);
1893 clickintime = ev.time - MEvent.time < MULTICLICK_TIME;
1897 /* mouse report from vt window */
1898 /* save the xbutton state (for ButtonRelease) */
1899 MEvent.state = ev.state;
1900 #ifdef MOUSE_REPORT_DOUBLECLICK
1901 if (ev.button == MEvent.button && clickintime)
1903 /* same button, within alloted time */
1906 if (MEvent.clicks > 1)
1908 /* only report double clicks */
1912 /* don't report the release */
1914 MEvent.button = AnyButton;
1919 /* different button, or time expired */
1921 MEvent.button = ev.button;
1925 MEvent.button = ev.button;
1927 #endif /* MOUSE_REPORT_DOUBLECLICK */
1932 if (ev.button != MEvent.button)
1938 /* allow meta + click to select rectangular areas */
1939 /* should be done in screen.C */
1941 selection.rect = !!(ev.state & ModMetaMask);
1943 selection.rect = false;
1946 /* allow shift+left click to extend selection */
1947 if (ev.state & ShiftMask && !(priv_modes & PrivMode_mouse_report))
1949 if (MEvent.button == Button1 && clickintime)
1950 selection_rotate (ev.x, ev.y);
1952 selection_extend (ev.x, ev.y, 1);
1956 if (MEvent.button == Button1 && clickintime)
1961 selection_click (MEvent.clicks, ev.x, ev.y);
1964 MEvent.button = Button1;
1968 if (MEvent.button == Button3 && clickintime)
1969 selection_rotate (ev.x, ev.y);
1971 selection_extend (ev.x, ev.y, 1);
1973 MEvent.button = Button3;
1978 MEvent.time = ev.time;
1983 * Scrollbar window processing of button press
1985 if (scrollBar.state && ev.window == scrollBar.win)
1987 page_dirn direction = NO_DIR;
1989 if (scrollBar.upButton (ev.y))
1990 direction = UP; /* up */
1991 else if (scrollBar.dnButton (ev.y))
1992 direction = DN; /* down */
1994 scrollBar.state = STATE_IDLE;
1996 * Rxvt-style scrollbar:
1997 * move up if mouse is above slider
1998 * move dn if mouse is below slider
2000 * XTerm-style scrollbar:
2001 * Move display proportional to pointer location
2002 * pointer near top -> scroll one line
2003 * pointer near bot -> scroll full page
2005 #ifndef NO_SCROLLBAR_REPORT
2009 * Mouse report disabled scrollbar:
2010 * arrow buttons - send up/down
2011 * click on scrollbar - send pageup/down
2013 if (direction == UP)
2014 tt_printf ("\033[A");
2015 else if (direction == DN)
2016 tt_printf ("\033[B");
2024 tt_printf ("\033[6~");
2027 tt_printf ("\033[5~");
2032 #endif /* NO_SCROLLBAR_REPORT */
2034 if (direction != NO_DIR)
2036 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
2037 if (!cont_scroll_ev.is_active ())
2038 cont_scroll_ev.start (SCROLLBAR_INITIAL_DELAY, SCROLLBAR_CONTINUOUS_DELAY);
2040 if (scr_page (direction, 1))
2042 if (direction == UP)
2043 scrollBar.state = STATE_UP;
2045 scrollBar.state = STATE_DOWN;
2052 switch (scrollBar.align)
2054 case R_SB_ALIGN_TOP:
2057 case R_SB_ALIGN_CENTRE:
2058 csrO = (scrollBar.bot - scrollBar.top) / 2;
2060 case R_SB_ALIGN_BOTTOM:
2061 csrO = scrollBar.bot - scrollBar.top;
2065 if (scrollBar.style == R_SB_XTERM
2066 || scrollbar_above_slider (ev.y)
2067 || scrollbar_below_slider (ev.y))
2068 scr_move_to (scrollbar_position (ev.y) - csrO, scrollBar.size ());
2070 scrollBar.state = STATE_MOTION;
2074 if (scrollBar.align == R_SB_ALIGN_CENTRE)
2075 csrO = ev.y - scrollBar.top;
2079 if (scrollBar.style != R_SB_XTERM)
2081 if (scrollbar_above_slider (ev.y))
2082 # ifdef RXVT_SCROLL_FULL
2083 scr_page (UP, nrow - 1);
2085 scr_page (UP, nrow / 4);
2087 else if (scrollbar_below_slider (ev.y))
2088 # ifdef RXVT_SCROLL_FULL
2089 scr_page (DN, nrow - 1);
2091 scr_page (DN, nrow / 4);
2094 scrollBar.state = STATE_MOTION;
2098 scr_page ((ev.button == Button1 ? DN : UP),
2100 * scrollbar_position (ev.y)
2101 / scrollBar.size ()));
2113 rxvt_term::button_release (XButtonEvent &ev)
2117 if (button_state[ev.button - Button1] == false)
2118 return; /* it wasn't pressed so why is it released? */
2120 button_state[ev.button - Button1] = false;
2122 csrO = 0; /* reset csr Offset */
2123 if (!bypass_keystate)
2124 reportmode = !! (priv_modes & PrivMode_mouse_report);
2126 if (scrollBar.state == STATE_UP || scrollBar.state == STATE_DOWN)
2128 scrollBar.state = STATE_IDLE;
2132 #ifdef SELECTION_SCROLLING
2133 sel_scroll_ev.stop();
2136 if (ev.window == vt)
2138 if (HOOK_INVOKE ((this, HOOK_BUTTON_RELEASE, DT_XEVENT, &ev, DT_END)))
2143 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
2149 /* mouse report from vt window */
2150 /* don't report release of wheel "buttons" */
2153 #ifdef MOUSE_REPORT_DOUBLECLICK
2154 /* only report the release of 'slow' single clicks */
2155 if (MEvent.button != AnyButton
2156 && (ev.button != MEvent.button
2157 || (ev.time - MEvent.time
2158 > MULTICLICK_TIME / 2)))
2161 MEvent.button = AnyButton;
2164 #else /* MOUSE_REPORT_DOUBLECLICK */
2165 MEvent.button = AnyButton;
2167 #endif /* MOUSE_REPORT_DOUBLECLICK */
2172 * dumb hack to compensate for the failure of click-and-drag
2173 * when overriding mouse reporting
2175 if (priv_modes & PrivMode_mouse_report
2177 && ev.button == Button1 && MEvent.clicks <= 1)
2178 selection_extend (ev.x, ev.y, 0);
2184 selection_make (ev.time);
2188 if (IN_RANGE_EXC (ev.x, 0, width) && IN_RANGE_EXC (ev.y, 0, height)) // inside window?
2189 selection_request (ev.time, ev.state & ModMetaMask ? Sel_Clipboard : Sel_Primary);
2199 v = ev.button == Button4 ? UP : DN;
2201 if (ev.state & ShiftMask)
2203 else if (option (Opt_mouseWheelScrollPage))
2208 # ifdef MOUSE_SLIP_WHEELING
2209 if (ev.state & ControlMask)
2211 mouse_slip_wheel_speed += v ? -1 : 1;
2212 if (mouse_slip_wheel_speed < -nrow) mouse_slip_wheel_speed = -nrow;
2213 if (mouse_slip_wheel_speed > +nrow) mouse_slip_wheel_speed = +nrow;
2215 if (!slip_wheel_ev.is_active ())
2216 slip_wheel_ev.start (SCROLLBAR_CONTINUOUS_DELAY, SCROLLBAR_CONTINUOUS_DELAY);
2234 rxvt_term::cmd_parse ()
2236 wchar_t ch = NOCHAR;
2237 char *seq_begin; // remember start of esc-sequence here
2241 if (expect_false (ch == NOCHAR))
2243 seq_begin = cmdbuf_ptr;
2250 if (expect_true (!IS_CONTROL (ch) || ch == C0_LF || ch == C0_CR || ch == C0_HT))
2252 if (expect_false (!seen_input))
2255 // many badly-written programs (e.g. jed) contain a race condition:
2256 // they first read the screensize and then install a SIGWINCH handler.
2257 // some window managers resize the window early, and these programs
2258 // then sometimes get the size wrong.
2259 // unfortunately other programs are even more buggy and dislike
2260 // being sent SIGWINCH, so only do it when we were in fact being
2262 if (seen_resize && cmd_pid)
2263 kill (-cmd_pid, SIGWINCH);
2266 /* Read a text string from the input buffer */
2267 wchar_t buf[UBUFSIZ];
2268 bool refreshnow = false;
2271 wchar_t *eol = str + min (ncol, UBUFSIZ);
2275 if (expect_false (ch == NOCHAR || (IS_CONTROL (ch) && ch != C0_LF && ch != C0_CR && ch != C0_HT)))
2280 if (expect_false (ch == C0_LF || str >= eol))
2287 if (!option (Opt_jumpScroll) || refresh_count >= nrow - 1)
2291 if (!option (Opt_skipScroll) || ev_time () > ev::now () + 1. / 60.)
2299 // scr_add_lines only works for nlines <= nrow - 1.
2300 if (nlines >= nrow - 1)
2302 if (!(SHOULD_INVOKE (HOOK_ADD_LINES)
2303 && HOOK_INVOKE ((this, HOOK_ADD_LINES, DT_WCS_LEN, buf, str - buf, DT_END))))
2304 scr_add_lines (buf, str - buf, nlines);
2308 eol = str + min (ncol, UBUFSIZ);
2313 if (eol >= buf + UBUFSIZ)
2319 eol = min (eol + ncol, buf + UBUFSIZ);
2324 seq_begin = cmdbuf_ptr;
2328 if (!(SHOULD_INVOKE (HOOK_ADD_LINES)
2329 && HOOK_INVOKE ((this, HOOK_ADD_LINES, DT_WCS_LEN, buf, str - buf, DT_END))))
2330 scr_add_lines (buf, str - buf, nlines);
2333 * If there have been a lot of new lines, then update the screen
2334 * What the heck we'll cheat and only refresh less than every page-full.
2335 * if skipScroll is enabled.
2347 process_nonprinting (ch);
2349 catch (const class out_of_input &o)
2351 // we ran out of input, retry later
2352 cmdbuf_ptr = seq_begin;
2361 // read the next character
2363 rxvt_term::next_char () NOTHROW
2365 while (cmdbuf_ptr < cmdbuf_endp)
2367 // assume 7-bit to be ascii ALWAYS
2368 if (expect_true ((unsigned char)*cmdbuf_ptr <= 0x7f && *cmdbuf_ptr != 0x1b))
2369 return *cmdbuf_ptr++;
2372 size_t len = mbrtowc (&wc, cmdbuf_ptr, cmdbuf_endp - cmdbuf_ptr, mbstate);
2374 if (len == (size_t)-2)
2376 // the mbstate stores incomplete sequences. didn't know this :/
2377 cmdbuf_ptr = cmdbuf_endp;
2381 if (len == (size_t)-1)
2383 mbstate.reset (); // reset now undefined conversion state
2384 return (unsigned char)*cmdbuf_ptr++; // the _occasional_ latin1 character is allowed to slip through
2387 // assume wchar == unicode
2389 return wc & UNICODE_MASK;
2395 // read the next octet
2397 rxvt_term::next_octet () NOTHROW
2399 return cmdbuf_ptr < cmdbuf_endp
2400 ? (unsigned char)*cmdbuf_ptr++
2404 static class out_of_input out_of_input;
2406 /* rxvt_cmd_getc () - Return next input character */
2408 * Return the next input character after first passing any keyboard input
2412 rxvt_term::cmd_getc () THROW ((class out_of_input))
2414 wchar_t c = next_char ();
2423 rxvt_term::cmd_get8 () THROW ((class out_of_input))
2425 uint32_t c = next_octet ();
2434 /*----------------------------------------------------------------------*/
2437 rxvt_term::popen_printer ()
2439 FILE *stream = popen (rs[Rs_print_pipe] ? rs[Rs_print_pipe] : PRINTPIPE, "w");
2442 rxvt_warn ("can't open printer pipe, not printing.\n");
2448 rxvt_term::pclose_printer (FILE *stream)
2451 return pclose (stream);
2455 * simulate attached vt100 printer
2458 rxvt_term::process_print_pipe ()
2460 FILE *fd = popen_printer ();
2466 * Send all input to the printer until either ESC[4i or ESC[?4i
2469 for (int done = 0; !done; )
2471 unsigned char buf[8];
2473 unsigned int i, len;
2475 if ((ch = cmd_getc ()) != C0_ESC)
2477 if (putc (ch, fd) == EOF)
2478 break; /* done = 1 */
2485 if ((buf[len++] = cmd_getc ()) == '[')
2487 if ((ch = cmd_getc ()) == '?')
2492 if ((buf[len++] = ch) == '4')
2494 if ((buf[len++] = cmd_getc ()) == 'i')
2495 break; /* done = 1 */
2499 for (i = 0; i < len; i++)
2500 if (putc (buf[i], fd) == EOF)
2508 pclose_printer (fd);
2510 #endif /* PRINTPIPE */
2516 C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
2517 C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
2518 C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
2519 C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
2523 /*{{{ process non-printing single characters */
2525 rxvt_term::process_nonprinting (unicode_t ch)
2530 process_escape_seq ();
2532 case C0_ENQ: /* terminal Status */
2533 if (rs[Rs_answerbackstring])
2534 tt_write (rs [Rs_answerbackstring], strlen (rs [Rs_answerbackstring]));
2536 tt_write (VT100_ANS, strlen (VT100_ANS));
2538 case C0_BEL: /* bell */
2541 case C0_BS: /* backspace */
2544 case C0_HT: /* tab */
2547 case C0_CR: /* carriage return */
2548 scr_gotorc (0, 0, R_RELATIVE);
2550 case C0_VT: /* vertical tab, form feed */
2552 case C0_LF: /* line feed */
2555 case C0_SO: /* shift out - acs */
2556 scr_charset_choose (1);
2558 case C0_SI: /* shift in - acs */
2559 scr_charset_choose (0);
2562 #ifdef EIGHT_BIT_CONTROLS
2564 case 0x90: /* DCS */
2567 case 0x9b: /* CSI */
2570 case 0x9d: /* OSC */
2579 /*{{{ process VT52 escape sequences */
2581 rxvt_term::process_escape_vt52 (unicode_t ch)
2587 case 'A': /* cursor up */
2588 scr_gotorc (-1, 0, R_RELATIVE | C_RELATIVE);
2590 case 'B': /* cursor down */
2591 scr_gotorc (1, 0, R_RELATIVE | C_RELATIVE);
2593 case 'C': /* cursor right */
2594 scr_gotorc (0, 1, R_RELATIVE | C_RELATIVE);
2596 case 'D': /* cursor left */
2597 scr_gotorc (0, -1, R_RELATIVE | C_RELATIVE);
2599 case 'H': /* cursor home */
2600 scr_gotorc (0, 0, 0);
2602 case 'I': /* cursor up and scroll down if needed */
2605 case 'J': /* erase to end of screen */
2606 scr_erase_screen (0);
2608 case 'K': /* erase to end of line */
2611 case 'Y': /* move to specified row and col */
2612 /* full command is 'ESC Y row col' where row and col
2613 * are encoded by adding 32 and sending the ascii
2614 * character. eg. SPACE = 0, '+' = 13, '0' = 18,
2616 row = cmd_getc () - ' ';
2617 col = cmd_getc () - ' ';
2618 scr_gotorc (row, col, 0);
2620 case 'Z': /* identify the terminal type */
2621 tt_printf ("\033/Z"); /* I am a VT100 emulating a VT52 */
2623 case '<': /* turn off VT52 mode */
2624 set_privmode (PrivMode_vt52, 0);
2626 case 'F': /* use special graphics character set */
2627 case 'G': /* use regular character set */
2630 case '=': /* use alternate keypad mode */
2631 case '>': /* use regular keypad mode */
2639 /*{{{ process escape sequences */
2641 rxvt_term::process_escape_seq ()
2643 unicode_t ch = cmd_getc ();
2645 if (priv_modes & PrivMode_vt52)
2647 process_escape_vt52 (ch);
2653 /* case 1: do_tek_mode (); break; */
2655 if (cmd_getc () == '8')
2659 scr_charset_set (0, (unsigned int)cmd_getc ());
2662 scr_charset_set (1, (unsigned int)cmd_getc ());
2665 scr_charset_set (2, (unsigned int)cmd_getc ());
2668 scr_charset_set (3, (unsigned int)cmd_getc ());
2679 scr_cursor (RESTORE);
2683 scr_forwardindex ();
2688 set_privmode (PrivMode_aplKP, ch == '=');
2698 /* 8.3.87: NEXT LINE */
2699 case C1_NEL: /* ESC E */
2701 wchar_t nlcr[] = { C0_LF, C0_CR };
2702 scr_add_lines (nlcr, sizeof (nlcr) / sizeof (nlcr [0]), 1);
2706 /* kidnapped escape sequence: Should be 8.3.48 */
2707 case C1_ESA: /* ESC G */
2708 process_graphics ();
2711 /* 8.3.63: CHARACTER TABULATION SET */
2712 case C1_HTS: /* ESC H */
2716 /* 8.3.105: REVERSE LINE FEED */
2717 case C1_RI: /* ESC M */
2721 /* 8.3.142: SINGLE-SHIFT TWO */
2722 /*case C1_SS2: scr_single_shift (2); break; */
2724 /* 8.3.143: SINGLE-SHIFT THREE */
2725 /*case C1_SS3: scr_single_shift (3); break; */
2727 /* 8.3.27: DEVICE CONTROL STRING */
2728 case C1_DCS: /* ESC P */
2732 /* 8.3.110: SINGLE CHARACTER INTRODUCER */
2733 case C1_SCI: /* ESC Z */
2734 tt_write (ESCZ_ANSWER, sizeof (ESCZ_ANSWER) - 1);
2735 break; /* steal obsolete ESC [ c */
2737 /* 8.3.16: CONTROL SEQUENCE INTRODUCER (CSI) */
2738 case C1_CSI: /* ESC [ */
2742 /* 8.3.90: OPERATING SYSTEM COMMAND (OSC) */
2743 case C1_OSC: /* ESC ] */
2747 /* 8.3.106: RESET TO INITIAL STATE (RIS) */
2754 /* 8.3.79: LOCKING-SHIFT TWO (see ISO2022) */
2756 scr_charset_choose (2);
2759 /* 8.3.81: LOCKING-SHIFT THREE (see ISO2022) */
2761 scr_charset_choose (3);
2767 /*{{{ process CONTROL SEQUENCE INTRODUCER (CSI) sequences `ESC[' */
2771 CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
2772 CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
2773 CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
2774 CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
2775 CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
2776 CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
2777 CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
2778 CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
2781 #define make_byte(b7,b6,b5,b4,b3,b2,b1,b0) \
2782 (((b7) << 7) | ((b6) << 6) | ((b5) << 5) | ((b4) << 4) \
2783 | ((b3) << 3) | ((b2) << 2) | ((b1) << 1) | (b0))
2784 #define get_byte_array_bit(array, bit) \
2785 (!! ((array)[ (bit) / 8] & (128 >> ((bit) & 7))))
2787 const unsigned char csi_defaults[] =
2789 make_byte (1,1,1,1,1,1,1,1), /* @, A, B, C, D, E, F, G, */
2790 make_byte (1,1,0,0,1,1,0,0), /* H, I, J, K, L, M, N, O, */
2791 make_byte (1,0,1,1,1,1,1,0), /* P, Q, R, S, T, U, V, W, */
2792 make_byte (1,1,1,0,0,0,1,0), /* X, Y, Z, [, \, ], ^, _, */
2793 make_byte (1,1,1,0,1,1,1,0), /* `, a, b, c, d, e, f, g, */
2794 make_byte (0,0,1,1,0,0,0,0), /* h, i, j, k, l, m, n, o, */
2795 make_byte (0,0,0,0,0,0,0,0), /* p, q, r, s, t, u, v, w, */
2796 make_byte (0,0,0,0,0,0,0,0), /* x, y, z, {, |, }, ~, */
2801 rxvt_term::process_csi_seq ()
2803 unicode_t ch, priv, i;
2804 unsigned int nargs, p;
2806 int arg[ESC_ARGS] = { };
2812 if ((ch >= '<' && ch <= '?') || ch == '!')
2814 /* '<' '=' '>' '?' '!' */
2819 /* read any numerical arguments */
2820 for (n = -1; ch < CSI_ICH; )
2827 n = n * 10 + ch - '0';
2831 if (nargs < ESC_ARGS)
2835 else if (IS_CONTROL (ch))
2836 process_nonprinting (ch);
2844 if (nargs < ESC_ARGS)
2848 ndef = get_byte_array_bit (csi_defaults, i);
2849 for (p = 0; p < nargs; p++)
2854 * private mode handling
2861 if (ch == CSI_DA) /* secondary device attributes */
2863 // first parameter is normally 0 for vt100, 1 for vt220, 'R' for rxvt,
2864 // 'U' for rxvt-unicode != 7.[34] (where it was broken).
2866 // second parameter is xterm patch level for xterm, MMmmpp (e.g. 20703) for rxvt
2867 // and Mm (e.g. 72 for 7.2) for urxvt <= 7.2, 94 for urxvt <= 8.3, and 95 for later
2870 tt_printf ("\033[>%d;95;0c", 'U');
2875 if (ch == 'h' || ch == 'l' || ch == 'r' || ch == 's' || ch == 't')
2876 process_terminal_mode (ch, priv, nargs, arg);
2882 /* DECSTR: soft terminal reset, used by our terminfo since 9.06 */
2885 static const int pm_h[] = { 7, 25 };
2886 static const int pm_l[] = { 1, 3, 4, 5, 6, 9, 66, 1000, 1001, 1049 };
2888 process_terminal_mode ('h', 0, sizeof (pm_h) / sizeof (pm_h[0]), pm_h);
2889 process_terminal_mode ('l', 0, sizeof (pm_l) / sizeof (pm_l[0]), pm_l);
2900 * ISO/IEC 6429:1992 (E) CSI sequences (defaults in parentheses)
2903 case CSI_MC: /* 8.3.83: (0) MEDIA COPY */
2906 case 0: /* initiate transfer to primary aux device */
2907 scr_printscreen (0);
2909 case 5: /* start relay to primary aux device */
2910 process_print_pipe ();
2916 case CSI_CUU: /* 8.3.22: (1) CURSOR UP */
2917 case CSI_VPR: /* 8.3.161: (1) LINE POSITION FORWARD */
2920 case CSI_CUD: /* 8.3.19: (1) CURSOR DOWN */
2921 case CSI_VPB: /* 8.3.160: (1) LINE POSITION BACKWARD */
2922 scr_gotorc (arg[0], 0, RELATIVE);
2925 case CSI_CUB: /* 8.3.18: (1) CURSOR LEFT */
2926 case CSI_HPB: /* 8.3.59: (1) CHARACTER POSITION BACKWARD */
2929 #else /* emulate common DEC VTs */
2930 arg[0] = arg[0] ? -arg[0] : -1;
2933 case CSI_CUF: /* 8.3.20: (1) CURSOR RIGHT */
2934 case CSI_HPR: /* 8.3.60: (1) CHARACTER POSITION FORWARD */
2936 scr_gotorc (0, arg[0], RELATIVE);
2937 #else /* emulate common DEC VTs */
2938 scr_gotorc (0, arg[0] ? arg[0] : 1, RELATIVE);
2942 case CSI_CPL: /* 8.3.13: (1) CURSOR PRECEDING LINE */
2945 case CSI_CNL: /* 8.3.12: (1) CURSOR NEXT LINE */
2946 scr_gotorc (arg[0], 0, R_RELATIVE);
2949 case CSI_CHA: /* 8.3.9: (1) CURSOR CHARACTER ABSOLUTE */
2950 case CSI_HPA: /* 8.3.58: (1) CURSOR POSITION ABSOLUTE */
2951 scr_gotorc (0, arg[0] - 1, R_RELATIVE);
2954 case CSI_VPA: /* 8.3.159: (1) LINE POSITION ABSOLUTE */
2955 scr_gotorc (arg[0] - 1, 0, C_RELATIVE);
2958 case CSI_CUP: /* 8.3.21: (1,1) CURSOR POSITION */
2959 case CSI_HVP: /* 8.3.64: (1,1) CHARACTER AND LINE POSITION */
2960 scr_gotorc (arg[0] - 1, nargs < 2 ? 0 : (arg[1] - 1), 0);
2963 case CSI_CBT: /* 8.3.7: (1) CURSOR BACKWARD TABULATION */
2966 case CSI_CHT: /* 8.3.10: (1) CURSOR FORWARD TABULATION */
2970 case CSI_ED: /* 8.3.40: (0) ERASE IN PAGE */
2971 scr_erase_screen (arg[0]);
2974 case CSI_EL: /* 8.3.42: (0) ERASE IN LINE */
2975 scr_erase_line (arg[0]);
2978 case CSI_ICH: /* 8.3.65: (1) INSERT CHARACTER */
2979 scr_insdel_chars (arg[0], INSERT);
2982 case CSI_IL: /* 8.3.68: (1) INSERT LINE */
2983 scr_insdel_lines (arg[0], INSERT);
2986 case CSI_DL: /* 8.3.33: (1) DELETE LINE */
2987 scr_insdel_lines (arg[0], DELETE);
2990 case CSI_ECH: /* 8.3.39: (1) ERASE CHARACTER */
2991 scr_insdel_chars (arg[0], ERASE);
2994 case CSI_DCH: /* 8.3.26: (1) DELETE CHARACTER */
2995 scr_insdel_chars (arg[0], DELETE);
2998 case CSI_SD: /* 8.3.114: (1) SCROLL DOWN */
3001 case CSI_SU: /* 8.3.148: (1) SCROLL UP */
3002 scr_scroll_text (screen.tscroll, screen.bscroll, arg[0]);
3005 case CSI_DA: /* 8.3.24: (0) DEVICE ATTRIBUTES */
3006 tt_write (VT100_ANS, sizeof (VT100_ANS) - 1);
3009 case CSI_SGR: /* 8.3.118: (0) SELECT GRAPHIC RENDITION */
3010 process_sgr_mode (nargs, arg);
3013 case CSI_DSR: /* 8.3.36: (0) DEVICE STATUS REPORT */
3016 case 5: /* DSR requested */
3017 tt_printf ("\033[0n");
3019 case 6: /* CPR requested */
3020 scr_report_position ();
3022 case 7: /* unofficial extension */
3023 if (option (Opt_insecure))
3024 tt_printf ("%-.250s\012", rs[Rs_display_name]);
3026 case 8: /* unofficial extension */
3027 process_xterm_seq (XTerm_title, RESNAME "-" VERSION, CHAR_ST);
3032 case CSI_TBC: /* 8.3.155: (0) TABULATION CLEAR */
3035 case 0: /* char tab stop cleared at active position */
3038 /* case 1: */ /* line tab stop cleared in active line */
3039 /* case 2: */ /* char tab stops cleared in active line */
3040 case 3: /* all char tab stops are cleared */
3041 /* case 4: */ /* all line tab stops are cleared */
3042 case 5: /* all tab stops are cleared */
3048 case CSI_CTC: /* 8.3.17: (0) CURSOR TABULATION CONTROL */
3051 case 0: /* char tab stop set at active position */
3053 break; /* = ESC H */
3054 /* case 1: */ /* line tab stop set at active line */
3055 case 2: /* char tab stop cleared at active position */
3057 break; /* = ESC [ 0 g */
3058 /* case 3: */ /* line tab stop cleared at active line */
3059 /* case 4: */ /* char tab stops cleared at active line */
3060 case 5: /* all char tab stops are cleared */
3062 break; /* = ESC [ 3 g */
3063 /* case 6: */ /* all line tab stops are cleared */
3067 case CSI_RM: /* 8.3.107: RESET MODE */
3069 scr_insert_mode (0);
3070 else if (arg[0] == 20)
3071 priv_modes &= ~PrivMode_LFNL;
3074 case CSI_SM: /* 8.3.126: SET MODE */
3076 scr_insert_mode (1);
3077 else if (arg[0] == 20)
3078 priv_modes |= PrivMode_LFNL;
3082 * PRIVATE USE beyond this point. All CSI_7? sequences here
3084 case CSI_72: /* DECSTBM: set top and bottom margins */
3086 scr_scroll_region (arg[0] - 1, MAX_ROWS - 1);
3087 else if (nargs == 0 || arg[0] >= arg[1])
3088 scr_scroll_region (0, MAX_ROWS - 1);
3090 scr_scroll_region (arg[0] - 1, arg[1] - 1);
3097 scr_cursor (RESTORE);
3102 process_window_ops (arg, nargs);
3106 case CSI_78: /* DECREQTPARM */
3107 if (arg[0] == 0 || arg[0] == 1)
3108 tt_printf ("\033[%d;1;1;128;128;1;0x", arg[0] + 2);
3120 rxvt_term::process_window_ops (const int *args, unsigned int nargs)
3123 XWindowAttributes wattr;
3126 dLocal (Display *, dpy);
3136 case 1: /* deiconify window */
3137 XMapWindow (dpy, parent[0]);
3139 case 2: /* iconify window */
3140 XIconifyWindow (dpy, parent[0], display->screen);
3142 case 3: /* set position (pixels) */
3143 XMoveWindow (dpy, parent[0], args[1], args[2]);
3145 case 4: /* set size (pixels) */
3146 set_widthheight ((unsigned int)args[2], (unsigned int)args[1]);
3148 case 5: /* raise window */
3149 XRaiseWindow (dpy, parent[0]);
3151 case 6: /* lower window */
3152 XLowerWindow (dpy, parent[0]);
3154 case 7: /* refresh window */
3157 case 8: /* set size (chars) */
3158 set_widthheight ((unsigned int) (args[2] * fwidth),
3159 (unsigned int) (args[1] * fheight));
3162 //case 9: NYI, TODO, restore maximized window or maximize window
3164 if (args[0] >= 24) /* set height (chars) */
3165 set_widthheight ((unsigned int)width,
3166 (unsigned int) (args[1] * fheight));
3170 * reports - some output format copied from XTerm
3172 case 11: /* report window state */
3173 XGetWindowAttributes (dpy, parent[0], &wattr);
3174 tt_printf ("\033[%dt", wattr.map_state == IsViewable ? 1 : 2);
3176 case 13: /* report window position */
3177 XGetWindowAttributes (dpy, parent[0], &wattr);
3178 XTranslateCoordinates (dpy, parent[0], wattr.root,
3179 -wattr.border_width, -wattr.border_width,
3181 tt_printf ("\033[3;%d;%dt", x, y);
3183 case 14: /* report window size (pixels) */
3184 XGetWindowAttributes (dpy, parent[0], &wattr);
3185 tt_printf ("\033[4;%d;%dt", wattr.height, wattr.width);
3187 case 18: /* report text area size (chars) */
3188 tt_printf ("\033[8;%d;%dt", nrow, ncol);
3190 case 19: /* report window size (chars) */
3191 tt_printf ("\033[9;%d;%dt", nrow, ncol);
3193 case 20: /* report icon label */
3196 XGetIconName (dpy, parent[0], &s);
3197 tt_printf ("\033]L%-.250s\234", option (Opt_insecure) && s ? s : ""); /* 8bit ST */
3201 case 21: /* report window title */
3204 XFetchName (dpy, parent[0], &s);
3205 tt_printf ("\033]l%-.250s\234", option (Opt_insecure) && s ? s : ""); /* 8bit ST */
3213 /*----------------------------------------------------------------------*/
3215 * get input up until STRING TERMINATOR (or BEL)
3216 * ends_how is terminator used. returned input must be free()'d
3219 rxvt_term::get_to_st (unicode_t &ends_how)
3222 bool seen_esc = false;
3224 wchar_t string[STRING_MAX];
3226 while ((ch = cmd_getc ()) != NOCHAR)
3230 if (ch == 0x5c) /* 7bit ST */
3235 else if (ch == C0_ESC)
3240 else if (ch == C0_BEL || ch == CHAR_ST)
3242 else if (ch == C0_SYN)
3245 return NULL; /* other control character - exit */
3249 if (n >= STRING_MAX - 1)
3250 // stop at some sane length
3258 ends_how = (ch == 0x5c ? C0_ESC : ch);
3260 return rxvt_wcstombs (string);
3263 /*----------------------------------------------------------------------*/
3265 * process DEVICE CONTROL STRING `ESC P ... (ST|BEL)' or `0x90 ... (ST|BEL)'
3268 rxvt_term::process_dcs_seq ()
3283 /*----------------------------------------------------------------------*/
3285 * process OPERATING SYSTEM COMMAND sequence `ESC ] Ps ; Pt (ST|BEL)'
3288 rxvt_term::process_osc_seq ()
3294 for (arg = 0; isdigit (ch); ch = cmd_getc ())
3295 arg = arg * 10 + (ch - '0');
3299 char *s = get_to_st (eh);
3303 process_xterm_seq (arg, s, eh);
3310 rxvt_term::process_color_seq (int report, int color, const char *str, char resp)
3312 if (str[0] == '?' && !str[1])
3315 pix_colors_focused[color].get (c);
3318 if (c.a != rgba::MAX_CC)
3319 tt_printf ("\033]%d;rgba:%04x/%04x/%04x/%04x%c", report, c.a, c.r, c.g, c.b, resp);
3322 tt_printf ("\033]%d;rgb:%04x/%04x/%04x%c", report, c.r, c.g, c.b, resp);
3325 set_window_color (color, str);
3329 * XTerm escape sequences: ESC ] Ps;Pt (ST|BEL)
3332 rxvt_term::process_xterm_seq (int op, char *str, char resp)
3336 bool query = str[0] == '?' && !str[1];
3339 dLocal (Display *, dpy);
3341 assert (str != NULL);
3343 if (HOOK_INVOKE ((this, HOOK_OSC_SEQ, DT_INT, op, DT_STR, str, DT_END)))
3351 case XTerm_iconName:
3352 set_icon_name (str);
3357 case XTerm_property:
3360 Atom prop = display->atom (str + 1);
3363 unsigned long nitems;
3364 unsigned long bytes_after;
3365 unsigned char *value = 0;
3366 const char *str = "";
3369 && XGetWindowProperty (dpy, parent[0],
3370 prop, 0, 1<<16, 0, AnyPropertyType,
3371 &actual_type, &actual_format,
3372 &nitems, &bytes_after, &value) == Success
3373 && actual_type != None
3374 && actual_format == 8)
3375 str = (const char *)(value);
3377 tt_printf ("\033]%d;%s%c", op, str, resp);
3383 char *eq = strchr (str, '=');
3388 set_utf8_property (display->atom (str), eq + 1);
3391 XDeleteProperty (dpy, parent[0],
3392 display->atom (str));
3397 for (buf = (char *)str; buf && *buf;)
3399 if ((name = strchr (buf, ';')) == NULL)
3403 color = atoi (buf) + minCOLOR;
3405 if (!IN_RANGE_INC (color, minCOLOR, maxTermCOLOR))
3408 if ((buf = strchr (name, ';')) != NULL)
3411 process_color_seq (op, color, name, resp);
3414 case Rxvt_restoreFG:
3416 process_color_seq (op, Color_fg, str, resp);
3418 case Rxvt_restoreBG:
3420 process_color_seq (op, Color_bg, str, resp);
3422 #ifndef NO_CURSORCOLOR
3423 case XTerm_Color_cursor:
3424 process_color_seq (op, Color_cursor, str, resp);
3427 case XTerm_Color_pointer_fg:
3428 process_color_seq (op, Color_pointer_fg, str, resp);
3430 case XTerm_Color_pointer_bg:
3431 process_color_seq (op, Color_pointer_bg, str, resp);
3434 case XTerm_Color_HC:
3435 process_color_seq (op, Color_HC, str, resp);
3437 case XTerm_Color_HTC:
3438 process_color_seq (op, Color_HTC, str, resp);
3441 #ifndef NO_BOLD_UNDERLINE_REVERSE
3442 case URxvt_Color_BD:
3443 process_color_seq (op, Color_BD, str, resp);
3445 case URxvt_Color_UL:
3446 process_color_seq (op, Color_UL, str, resp);
3448 case URxvt_Color_IT:
3449 process_color_seq (op, Color_IT, str, resp);
3452 case URxvt_Color_border:
3453 process_color_seq (op, Color_border, str, resp);
3455 #if ENABLE_TRANSPARENCY
3456 case URxvt_Color_tint:
3457 process_color_seq (op, Color_tint, str, resp);
3459 bool changed = false;
3461 if (ISSET_PIXCOLOR (Color_tint))
3462 changed = bgPixmap.set_tint (pix_colors_focused [Color_tint]);
3464 changed = bgPixmap.unset_tint ();
3467 update_background ();
3473 #if BG_IMAGE_FROM_FILE
3475 if (!strcmp (str, "?"))
3479 sprintf (str, "[%dx%d+%d+%d]",
3480 min (bgPixmap.h_scale, 32767), min (bgPixmap.v_scale, 32767),
3481 min (bgPixmap.h_align, 32767), min (bgPixmap.v_align, 32767));
3482 process_xterm_seq (XTerm_title, str, CHAR_ST);
3490 /* reset to default scaling :*/
3491 bgPixmap.unset_geometry ();
3492 if (bgPixmap.set_file (str)) /* change pixmap */
3494 str = strchr (str, ';');
3496 bgPixmap.set_defaultGeometry ();
3502 if (bgPixmap.set_geometry (str))
3504 str = strchr (str, ';');
3508 update_background ();
3514 // TODO, when secure mode?
3518 case Rxvt_dumpscreen: /* no error notices */
3521 if ((fd = open (str, O_RDWR | O_CREAT | O_EXCL, 0600)) >= 0)
3533 case URxvt_boldFont:
3534 case URxvt_italicFont:
3535 case URxvt_boldItalicFont:
3538 tt_printf ("\33]%d;%-.250s%c", saveop,
3539 option (Opt_insecure) && fontset[op - URxvt_font]->fontdesc
3540 ? fontset[op - URxvt_font]->fontdesc : "",
3544 const char *&res = rs[Rs_font + (op - URxvt_font)];
3547 allocated.push_back ((void *)res);
3554 tt_printf ("\33]%d;rxvt-unicode;%-.20s;%c;%c%c",
3556 rs[Rs_name], VERSION[0], VERSION[2],
3563 tt_printf ("\33]%d;%-.250s%c", op, option (Opt_insecure) ? locale : "", resp);
3567 pty->set_utf8_mode (enc_utf8);
3573 case URxvt_view_down:
3575 int lines = atoi (str);
3578 scr_page (op == URxvt_view_up ? UP : DN, lines);
3580 scr_erase_savelines ();
3588 HOOK_INVOKE ((this, HOOK_OSC_SEQ_PERL, DT_STR, str, DT_STR_LEN, &resp, 1, DT_END));
3593 /*----------------------------------------------------------------------*/
3595 /*{{{ process DEC private mode sequences `ESC [ ? Ps mode' */
3597 * mode can only have the following values:
3603 * so no need for fancy checking
3606 rxvt_term::privcases (int mode, unsigned long bit)
3612 SavedModes |= (priv_modes & bit);
3618 state = (SavedModes & bit) ? 1 : 0; /* no overlapping */
3620 state = (mode == 't') ? ! (priv_modes & bit) : mode;
3621 set_privmode (bit, state);
3627 /* we're not using priv _yet_ */
3629 rxvt_term::process_terminal_mode (int mode, int priv UNUSED, unsigned int nargs, const int *arg)
3637 const unsigned long bit;
3639 { 1, PrivMode_aplCUR }, // DECCKM
3640 { 2, PrivMode_vt52 },
3641 { 3, PrivMode_132 }, // DECCOLM
3642 { 4, PrivMode_smoothScroll }, // DECSCLM
3643 { 5, PrivMode_rVideo }, // DECSCNM
3644 { 6, PrivMode_relOrigin }, // DECOM
3645 { 7, PrivMode_Autowrap }, // DECAWM
3646 // 8, auto-repeat keys // DECARM
3647 { 9, PrivMode_MouseX10 },
3648 // 18 end FF to printer after print screen
3649 // 19 Print screen prints full screen/scorll region
3650 { 25, PrivMode_VisibleCursor }, // cnorm/cvvis/civis
3651 #ifdef scrollBar_esc
3652 { scrollBar_esc, PrivMode_scrollBar },
3654 { 35, PrivMode_ShiftKeys }, // rxvt extension
3655 // 38, tektronix mode // DECTEK
3656 { 40, PrivMode_132OK },
3657 // 41 xterm more fixes NYI
3658 // 45 margin bell NYI
3660 { 47, PrivMode_Screen },
3661 { 66, PrivMode_aplKP }, // DECPAM/DECPNM
3662 #ifndef NO_BACKSPACE_KEY
3663 { 67, PrivMode_BackSpace }, // DECBKM
3665 { 1000, PrivMode_MouseX11 },
3666 { 1002, PrivMode_MouseBtnEvent },
3667 { 1003, PrivMode_MouseAnyEvent },
3668 { 1010, PrivMode_TtyOutputInh }, // rxvt extension
3669 { 1011, PrivMode_Keypress }, // rxvt extension
3670 // 1035 enable modifiers for alt, numlock NYI
3671 // 1036 send ESC for meta keys NYI
3672 // 1037 send DEL for keypad delete NYI
3673 { 1047, PrivMode_Screen },
3674 // 1048 save and restore cursor
3675 { 1049, PrivMode_Screen }, /* xterm extension, clear screen on ti rather than te */
3676 // 1051, 1052, 1060, 1061 keyboard emulation NYI
3677 { 2004, PrivMode_BracketPaste },
3683 /* make lo/hi boolean */
3685 mode = 0; /* reset */
3686 else if (mode == 'h')
3689 for (i = 0; i < nargs; i++)
3693 /* basic handling */
3694 for (j = 0; j < (sizeof (argtopriv)/sizeof (argtopriv[0])); j++)
3695 if (argtopriv[j].argval == arg[i])
3697 state = privcases (mode, argtopriv[j].bit);
3701 /* extra handling for values with state unkept */
3706 set_option (Opt_intensityStyles, mode);
3711 case 1048: /* alternative cursor save */
3712 if (option (Opt_secondaryScreen))
3714 scr_cursor (RESTORE);
3721 /* extra handling for values with valid 0 or 1 state */
3724 /* case 1: - application cursor keys */
3725 case 2: /* VT52 mode */
3726 /* oddball mode. should be set regardless of set/reset
3727 * parameter. Return from VT52 mode with an ESC < from
3730 set_privmode (PrivMode_vt52, 1);
3732 case 3: /* 80/132 */
3733 if (priv_modes & PrivMode_132OK)
3734 set_widthheight ((state ? 132 : 80) * fwidth, 24 * fheight);
3736 case 4: /* smooth scrolling */
3737 set_option (Opt_jumpScroll, !state);
3739 case 5: /* reverse video */
3740 scr_rvideo_mode (state);
3742 case 6: /* relative/absolute origins */
3743 scr_relative_origin (state);
3745 case 7: /* autowrap */
3746 scr_autowrap (state);
3748 /* case 8: - auto repeat, can't do on a per window basis */
3749 case 9: /* X10 mouse reporting */
3750 if (state) /* orthogonal */
3751 priv_modes &= ~(PrivMode_MouseX11|PrivMode_MouseBtnEvent|PrivMode_MouseAnyEvent);
3753 #ifdef scrollBar_esc
3755 if (scrollBar.map (state))
3757 resize_all_windows (0, 0, 0);
3762 case 25: /* visible/invisible cursor */
3763 scr_cursor_visible (state);
3765 /* case 35: - shift keys */
3766 /* case 40: - 80 <--> 132 mode */
3767 case 47: /* secondary screen */
3768 scr_change_screen (state);
3770 /* case 66: - application key pad */
3771 /* case 67: - backspace key */
3772 case 1000: /* X11 mouse reporting */
3773 if (state) /* orthogonal */
3774 priv_modes &= ~(PrivMode_MouseX10|PrivMode_MouseBtnEvent|PrivMode_MouseAnyEvent);
3780 priv_modes &= ~(PrivMode_MouseX10|PrivMode_MouseX11);
3781 priv_modes &= arg[i] == 1003 ? ~PrivMode_MouseBtnEvent : ~PrivMode_MouseAnyEvent;
3782 vt_emask_mouse = PointerMotionMask;
3785 vt_emask_mouse = NoEventMask;
3789 case 1010: /* scroll to bottom on TTY output inhibit */
3790 set_option (Opt_scrollTtyOutput, !state);
3792 case 1011: /* scroll to bottom on key press */
3793 set_option (Opt_scrollTtyKeypress, state);
3795 case 1047: /* secondary screen w/ clearing last */
3796 if (option (Opt_secondaryScreen))
3798 scr_erase_screen (2);
3800 scr_change_screen (state);
3802 case 1049: /* secondary screen w/ clearing first */
3803 if (option (Opt_secondaryScreen))
3807 scr_change_screen (state);
3809 if (option (Opt_secondaryScreen))
3811 scr_erase_screen (2);
3813 scr_cursor (RESTORE);
3822 /*{{{ process sgr sequences */
3824 rxvt_term::process_sgr_mode (unsigned int nargs, const int *arg)
3832 scr_rendition (0, ~RS_None);
3836 for (i = 0; i < nargs; i++)
3842 rendset = 0, rendstyle = ~RS_None;
3845 rendset = 1, rendstyle = RS_Bold;
3847 //case 2: // low intensity
3849 rendset = 1, rendstyle = RS_Italic;
3852 rendset = 1, rendstyle = RS_Uline;
3854 case 5: // slowly blinking
3855 case 6: // rapidly blinking
3856 rendset = 1, rendstyle = RS_Blink;
3858 //case 6: // scoansi light background
3860 rendset = 1, rendstyle = RS_RVid;
3865 //case 9: // crossed out
3866 //case 10: // scoansi acs off, primary font
3867 //case 11: // scoansi acs on, first alt font
3868 //case 12: // scoansi acs on, |0x80, second alt font
3870 //case 19: // ninth alt font
3871 //case 20: // gothic
3872 case 21: // disable bold, faint, sometimes doubly underlined (iso 8613)
3873 rendset = 0, rendstyle = RS_Bold;
3875 case 22: // normal intensity
3876 rendset = 0, rendstyle = RS_Bold;
3878 case 23: // disable italic
3879 rendset = 0, rendstyle = RS_Italic;
3882 rendset = 0, rendstyle = RS_Uline;
3885 rendset = 0, rendstyle = RS_Blink;
3887 case 26: // variable spacing (iso 8613)
3888 rendset = 0, rendstyle = RS_Blink;
3891 rendset = 0, rendstyle = RS_RVid;
3893 //case 28: // visible. NYI
3894 //case 29: // not crossed-out
3899 scr_rendition (rendset, rendstyle);
3900 continue; /* for (;i;) */
3906 case 31: /* set fg color */
3913 scr_color ((unsigned int) (minCOLOR + (arg[i] - 30)), Color_fg);
3915 case 38: // set fg color, ISO 8613-6
3916 if (nargs > i + 2 && arg[i + 1] == 5)
3918 scr_color ((unsigned int) (minCOLOR + arg[i + 2]), Color_fg);
3922 case 39: /* default fg */
3923 scr_color (Color_fg, Color_fg);
3927 case 41: /* set bg color */
3934 scr_color ((unsigned int) (minCOLOR + (arg[i] - 40)), Color_bg);
3936 case 48: // set bg color, ISO 8613-6
3937 if (nargs > i + 2 && arg[i + 1] == 5)
3939 scr_color ((unsigned int) (minCOLOR + arg[i + 2]), Color_bg);
3943 case 49: /* default bg */
3944 scr_color (Color_bg, Color_bg);
3947 //case 50: // not variable spacing
3951 case 91: /* set bright fg color */
3958 scr_color ((unsigned int) (minBrightCOLOR + (arg[i] - 90)), Color_fg);
3961 case 101: /* set bright bg color */
3968 scr_color ((unsigned int) (minBrightCOLOR + (arg[i] - 100)), Color_bg);
3976 /*{{{ (do not) process Rob Nation's own graphics mode sequences */
3978 rxvt_term::process_graphics ()
3980 unicode_t ch, cmd = cmd_getc ();
3984 /* query graphics */
3985 tt_printf ("\033G0\012"); /* no graphics */
3988 /* swallow other graphics sequences until terminating ':' */
3995 /* ------------------------------------------------------------------------- */
3998 * Send printf () formatted output to the command.
3999 * Only use for small amounts of data.
4002 rxvt_term::tt_printf (const char *fmt,...)
4007 va_start (arg_ptr, fmt);
4008 vsnprintf ((char *)buf, 256, fmt, arg_ptr);
4010 tt_write (buf, strlen (buf));
4013 /* ---------------------------------------------------------------------- */
4014 /* Write data to the pty as typed by the user, pasted with the mouse,
4015 * or generated by us in response to a query ESC sequence.
4017 const unsigned int MAX_PTY_WRITE = 255; // minimum MAX_INPUT
4020 rxvt_term::tt_write (const char *data, unsigned int len)
4022 if (HOOK_INVOKE ((this, HOOK_TT_WRITE, DT_STR_LEN, data, len, DT_END)))
4030 ssize_t written = write (pty->pty, data, min (len, MAX_PTY_WRITE));
4032 if ((unsigned int)written == len)
4039 v_buffer = (char *)realloc (v_buffer, v_buflen + len);
4041 memcpy (v_buffer + v_buflen, data, len);
4044 pty_ev.set (ev::READ | ev::WRITE);
4047 void rxvt_term::pty_write ()
4049 int written = write (pty->pty, v_buffer, min (v_buflen, MAX_PTY_WRITE));
4053 v_buflen -= written;
4060 pty_ev.set (ev::READ);
4064 memmove (v_buffer, v_buffer + written, v_buflen);
4066 else if (written != -1 || (errno != EAGAIN && errno != EINTR))
4067 pty_ev.set (ev::READ);
4070 /*----------------------- end-of-file (C source) -----------------------*/