ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/command.C
Revision: 1.267
Committed: Mon Jan 16 15:00:20 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.266: +1 -42 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*
2 * File: command.C
3 *----------------------------------------------------------------------*
4 *
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>
7 * - original version
8 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
9 * - extensive modifications
10 * Copyright (c) 1995 Garrett D'Amore <garrett@netcom.com>
11 * - vt100 printing
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
17 * keys properly.
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
22 * and Linux 1.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-2006 Marc Lehmann <pcg@goof.com>
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 *----------------------------------------------------------------------*/
46
47 /*{{{ includes: */
48 #include "../config.h"
49 #include "rxvt.h"
50 #include "rxvtperl.h"
51 #include "version.h"
52 #include "command.h"
53
54 #if HAVE_SCHED_YIELD
55 #undef HAVE_SCHED_YIELD // disabled for the time being
56 //# include <sched.h>
57 #endif
58
59 #ifdef KEYSYM_RESOURCE
60 # include "keyboard.h"
61 #endif
62
63 #include <csignal>
64
65 /*----------------------------------------------------------------------*/
66
67 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
68
69 // exception thrown when the command parser runs out of input data
70 class out_of_input { } out_of_input;
71
72 #if ENABLE_FRILLS || ISO_14755
73
74 #define ISO_14755_STARTED 0x80000000UL
75 #define ISO_14755_51 0x40000000UL // basic (section 5.1)
76 #define ISO_14755_52 0x20000000UL // keycap (section 5.2)
77 #define ISO_14755_54 0x10000000UL // code feedback (section 5.4)
78 #define ISO_14755_MASK 0x0fffffffUL
79
80 #if ISO_14755
81 static unsigned short iso14755_symtab[] = {
82 // keysym, unicode
83 XK_Left, 0x2190,
84 XK_KP_Left, 0x2190,
85 XK_Up, 0x2191,
86 XK_KP_Up, 0x2191,
87 XK_Right, 0x2192,
88 XK_KP_Right, 0x2192,
89 XK_Down, 0x2193,
90 XK_KP_Down, 0x2193,
91 XK_Linefeed, 0x21b4,
92 XK_Return, 0x21b5,
93 XK_KP_Enter, 0x21b5,
94
95 XK_Prior, 0x21de,
96 XK_Next, 0x21df,
97 XK_Tab, 0x21e5,
98 XK_ISO_Left_Tab, 0x21e6,
99 XK_Shift_L, 0x21e7,
100 XK_Shift_R, 0x21e7,
101
102 XK_Shift_Lock, 0x21eb,
103 XK_ISO_Lock, 0x21eb,
104 XK_ISO_Lock, 0x21eb,
105 XK_Caps_Lock, 0x21ec,
106 XK_Num_Lock, 0x21ed,
107 XK_ISO_Level3_Shift, 0x21ee,
108 XK_ISO_Level3_Lock, 0x21ef,
109 XK_ISO_Group_Lock, 0x21f0,
110 XK_Home, 0x21f1,
111 XK_End, 0x21f2,
112
113 XK_Execute, 0x2318,
114 XK_Begin, 0x2320,
115 XK_Delete, 0x2326,
116 XK_Clear, 0x2327,
117 XK_BackSpace, 0x232b,
118 XK_Insert, 0x2380,
119 XK_Control_L, 0x2388,
120 XK_Control_R, 0x2388,
121 XK_Pause, 0x2389,
122 XK_Break, 0x238a,
123 XK_Escape, 0x238b,
124 XK_Undo, 0x238c,
125 XK_Print, 0x2399,
126
127 XK_space, 0x2423,
128
129 #ifdef XK_KP_Begin
130 XK_KP_Prior, 0x21de,
131 XK_KP_Next, 0x21df,
132 XK_KP_Begin, 0x2320,
133 XK_KP_Insert, 0x2380,
134 XK_KP_Delete, 0x2326,
135 XK_KP_Space, 0x2422,
136 #endif
137 0,
138 };
139
140 void
141 rxvt_term::iso14755_54 (int x, int y)
142 {
143 x = Pixel2Col (x);
144 y = Pixel2Row (y);
145
146 if (!IN_RANGE_EXC (x, 0, ncol)
147 || !IN_RANGE_EXC (y, 0, nrow))
148 return;
149
150 for (;;)
151 {
152 const line_t &l = ROW(y + view_start);
153
154 text_t t = l.t[x];
155
156 if (t != NOCHAR || !x)
157 {
158 iso14755_51 (l.t[x], l.r[x], x, y);
159 iso14755buf = ISO_14755_54;
160 break;
161 }
162
163 x--;
164 }
165 }
166
167 void
168 rxvt_term::iso14755_51 (unicode_t ch, rend_t r, int x, int y)
169 {
170 rxvt_fontset *fs = FONTSET (r);
171 rxvt_font *f = (*fs)[fs->find_font (ch)];
172 wchar_t *chr, *alloc, ch2, *fname;
173 int len;
174
175 fname = rxvt_utf8towcs (f->name);
176
177 # if ENABLE_COMBINING
178 if (IS_COMPOSE (ch))
179 {
180 len = rxvt_composite.expand (ch, 0);
181 alloc = chr = new wchar_t[len];
182 rxvt_composite.expand (ch, chr);
183 }
184 else
185 # endif
186 {
187 ch2 = ch;
188
189 alloc = 0;
190 chr = &ch2;
191 len = 1;
192 }
193
194 char attr[80]; // plenty
195
196 sprintf (attr, "%08x = fg %d bg %d%s%s%s%s%s%s",
197 (int)r,
198 fgcolor_of (r), bgcolor_of (r),
199 r & RS_Bold ? " bold" : "",
200 r & RS_Italic ? " italic" : "",
201 r & RS_Blink ? " blink" : "",
202 r & RS_RVid ? " rvid" : "",
203 r & RS_Uline ? " uline" : "",
204 r & RS_Careful ? " careful" : "");
205
206 int width = wcswidth (fname, wcslen (fname));
207
208 max_it (width, 8+5); // for char + hey
209 max_it (width, strlen (attr));
210
211 if (y >= 0)
212 {
213 y = (y >= nrow - len - 4 && x < width + 2) ? 0 : -1;
214 x = 0;
215 }
216
217 scr_overlay_new (x, y, width, len + 2);
218
219 r = SET_STYLE (OVERLAY_RSTYLE, GET_STYLE (r));
220
221 for (int y = 0; y < len; y++)
222 {
223 char buf[9];
224
225 ch = *chr++;
226
227 sprintf (buf, "%8x", ch);
228 scr_overlay_set (0, y, buf);
229 scr_overlay_set (9, y, '=');
230 # if !UNICODE3
231 if (ch >= 0x10000)
232 ch = 0xfffd;
233 # endif
234 scr_overlay_set (11, y, ch, r);
235 scr_overlay_set (12, y, NOCHAR, r);
236 }
237
238 scr_overlay_set (0, len , attr);
239 scr_overlay_set (0, len + 1, fname);
240
241 free (fname);
242
243 # if ENABLE_COMBINING
244 if (alloc)
245 delete [] alloc;
246 # endif
247 }
248 #endif
249
250 void
251 rxvt_term::commit_iso14755 ()
252 {
253 wchar_t ch[2];
254
255 ch[0] = iso14755buf & ISO_14755_MASK;
256 ch[1] = 0;
257
258 if (iso14755buf & ISO_14755_51)
259 {
260 char mb[16];
261 int len;
262
263 // allow verbatim 0-bytes and control-bytes to be entered
264 if (ch[0] >= 0x20)
265 len = wcstombs (mb, ch, 16);
266 else
267 {
268 mb[0] = ch[0];
269 len = 1;
270 }
271
272 if (len > 0)
273 tt_write (mb, len);
274 else
275 scr_bell ();
276 }
277
278 iso14755buf = 0;
279 }
280
281 int
282 rxvt_term::hex_keyval (XKeyEvent &ev)
283 {
284 // check wether this event corresponds to a hex digit
285 // if the modifiers had not been pressed.
286 for (int index = 0; index < 8; index++)
287 {
288 KeySym k = XLookupKeysym (&ev, index);
289
290 if (k >= XK_KP_0 && k <= XK_KP_9) return k - XK_KP_0;
291 else if (k >= XK_0 && k <= XK_9) return k - XK_0;
292 else if (k >= XK_a && k <= XK_f) return k - XK_a + 10;
293 else if (k >= XK_A && k <= XK_F) return k - XK_A + 10;
294 }
295
296 return -1;
297 }
298 #endif
299
300 /*{{{ Convert the keypress event into a string */
301 void
302 rxvt_term::lookup_key (XKeyEvent &ev)
303 {
304 int ctrl, meta, shft, len;
305 unsigned int newlen;
306 KeySym keysym;
307 #ifdef DEBUG_CMD
308 static int debug_key = 1; /* accessible by a debugger only */
309 #endif
310 int valid_keysym;
311 char kbuf[KBUFSZ];
312
313 /*
314 * use Num_Lock to toggle Keypad on/off. If Num_Lock is off, allow an
315 * escape sequence to toggle the Keypad.
316 *
317 * Always permit `shift' to override the current setting
318 */
319 shft = ev.state & ShiftMask;
320 ctrl = ev.state & ControlMask;
321 meta = ev.state & ModMetaMask;
322
323 if (numlock_state || (ev.state & ModNumLockMask))
324 {
325 numlock_state = (ev.state & ModNumLockMask);
326 PrivMode ((!numlock_state), PrivMode_aplKP);
327 }
328
329 kbuf[0] = 0;
330
331 #ifdef USE_XIM
332 if (Input_Context)
333 {
334 Status status_return;
335
336 #if 0
337 #ifdef X_HAVE_UTF8_STRING
338 if (enc_utf8 && 0) // currently disabled, doesn't seem to work, nor is useful
339 len = Xutf8LookupString (Input_Context, &ev, kbuf,
340 KBUFSZ, &keysym, &status_return);
341 else
342 #endif
343 #endif
344 {
345 wchar_t wkbuf[KBUFSZ + 1];
346
347 // the XOpenIM manpage lies about hardcoding the locale
348 // at the point of XOpenIM, so temporarily switch locales
349 if (rs[Rs_imLocale])
350 SET_LOCALE (rs[Rs_imLocale]);
351
352 // assume wchar_t == unicode or better
353 len = XwcLookupString (Input_Context, &ev, wkbuf,
354 KBUFSZ, &keysym, &status_return);
355
356 if (rs[Rs_imLocale])
357 SET_LOCALE (locale);
358
359 if (status_return == XLookupChars
360 || status_return == XLookupBoth)
361 {
362 /* make sure the user can type ctrl-@, i.e. NUL */
363 if (len == 1 && *wkbuf == 0)
364 {
365 kbuf[0] = 0;
366 len = 1;
367 }
368 else
369 {
370 wkbuf[len] = 0;
371 len = wcstombs ((char *)kbuf, wkbuf, KBUFSZ);
372 if (len < 0)
373 len = 0;
374 }
375 }
376 else
377 len = 0;
378 }
379
380 valid_keysym = status_return == XLookupKeySym
381 || status_return == XLookupBoth;
382 }
383 else
384 #endif
385 {
386 len = XLookupString (&ev, kbuf, KBUFSZ, &keysym, &compose);
387 valid_keysym = keysym != NoSymbol;
388 }
389
390 if (valid_keysym)
391 {
392 #ifdef KEYSYM_RESOURCE
393 if (keyboard->dispatch (this, keysym, ev.state))
394 return;
395 #endif
396
397 if (saveLines)
398 {
399 #ifdef UNSHIFTED_SCROLLKEYS
400 if (!ctrl && !meta)
401 #else
402 if (IS_SCROLL_MOD)
403 #endif
404 {
405 int lnsppg;
406
407 #ifdef PAGING_CONTEXT_LINES
408 lnsppg = nrow - PAGING_CONTEXT_LINES;
409 #else
410 lnsppg = nrow * 4 / 5;
411 #endif
412 if (keysym == XK_Prior)
413 {
414 scr_page (UP, lnsppg);
415 return;
416 }
417 else if (keysym == XK_Next)
418 {
419 scr_page (DN, lnsppg);
420 return;
421 }
422 }
423 #ifdef SCROLL_ON_UPDOWN_KEYS
424 if (IS_SCROLL_MOD)
425 {
426 if (keysym == XK_Up)
427 {
428 scr_page (UP, 1);
429 return;
430 }
431 else if (keysym == XK_Down)
432 {
433 scr_page (DN, 1);
434 return;
435 }
436 }
437 #endif
438 #ifdef SCROLL_ON_HOMEEND_KEYS
439 if (IS_SCROLL_MOD)
440 {
441 if (keysym == XK_Home)
442 {
443 scr_move_to (0, 1);
444 return;
445 }
446 else if (keysym == XK_End)
447 {
448 scr_move_to (1, 1);
449 return;
450 }
451 }
452 #endif
453 }
454
455 if (shft)
456 {
457 /* Shift + F1 - F10 generates F11 - F20 */
458 if (keysym >= XK_F1 && keysym <= XK_F10)
459 {
460 keysym += (XK_F11 - XK_F1);
461 shft = 0; /* turn off Shift */
462 }
463 else if (!ctrl && !meta && (priv_modes & PrivMode_ShiftKeys))
464 {
465 switch (keysym)
466 {
467 /* normal XTerm key bindings */
468 case XK_Insert: /* Shift+Insert = paste mouse selection */
469 selection_request (ev.time, 0, 0);
470 return;
471 #if TODO
472 /* rxvt extras */
473 case XK_KP_Add: /* Shift+KP_Add = bigger font */
474 change_font (FONT_UP);
475 return;
476 case XK_KP_Subtract: /* Shift+KP_Subtract = smaller font */
477 change_font (FONT_DN);
478 return;
479 #endif
480 }
481 }
482 }
483
484 #if ENABLE_FRILLS || ISO_14755
485 // ISO 14755 support
486 if (shft && ctrl)
487 {
488 int hv;
489
490 if (iso14755buf & ISO_14755_51
491 && (keysym == XK_space || keysym == XK_KP_Space
492 || keysym == XK_Return || keysym == XK_KP_Enter))
493 {
494 commit_iso14755 ();
495 iso14755buf = ISO_14755_51;
496 # if ISO_14755
497 iso14755_51 (0);
498 # endif
499 return;
500 }
501 else if ((hv = hex_keyval (ev)) >= 0)
502 {
503 iso14755buf = ((iso14755buf << 4) & ISO_14755_MASK)
504 | hv | ISO_14755_51;
505 # if ISO_14755
506 iso14755_51 (iso14755buf & ISO_14755_MASK);
507 # endif
508 return;
509 }
510 else
511 {
512 # if ENABLE_OVERLAY
513 scr_overlay_off ();
514 # endif
515 iso14755buf = 0;
516 }
517 }
518 else if ((ctrl && (keysym == XK_Shift_L || keysym == XK_Shift_R))
519 || (shft && (keysym == XK_Control_L || keysym == XK_Control_R)))
520 if (!(iso14755buf & ISO_14755_STARTED))
521 {
522 iso14755buf |= ISO_14755_STARTED;
523 # if ENABLE_OVERLAY
524 scr_overlay_new (0, -1, sizeof ("ISO 14755 mode") - 1, 1);
525 scr_overlay_set (0, 0, "ISO 14755 mode");
526 # endif
527 }
528 #endif
529
530 #ifdef PRINTPIPE
531 if (keysym == XK_Print)
532 {
533 scr_printscreen (ctrl | shft);
534 return;
535 }
536 #endif
537
538 if (keysym >= 0xFF00 && keysym <= 0xFFFF)
539 {
540 {
541 newlen = 1;
542 switch (keysym)
543 {
544 #ifndef NO_BACKSPACE_KEY
545 case XK_BackSpace:
546 if (priv_modes & PrivMode_HaveBackSpace)
547 {
548 kbuf[0] = (!! (priv_modes & PrivMode_BackSpace)
549 ^ !!ctrl) ? '\b' : '\177';
550 kbuf[1] = '\0';
551 }
552 else
553 strcpy (kbuf, key_backspace);
554 break;
555 #endif
556 #ifndef NO_DELETE_KEY
557 # ifdef XK_KP_Prior
558 case XK_KP_Delete:
559 /* allow shift to override */
560 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
561 {
562 strcpy (kbuf, "\033On");
563 break;
564 }
565 /* FALLTHROUGH */
566 # endif
567 case XK_Delete:
568 strcpy (kbuf, key_delete);
569 break;
570 #endif
571 case XK_Tab:
572 if (shft)
573 strcpy (kbuf, "\033[Z");
574 else
575 {
576 #ifdef CTRL_TAB_MAKES_META
577 if (ctrl)
578 meta = 1;
579 #endif
580 #ifdef MOD4_TAB_MAKES_META
581 if (ev.state & Mod4Mask)
582 meta = 1;
583 #endif
584 newlen = 0;
585 }
586 break;
587
588 #ifdef XK_KP_Left
589 case XK_KP_Up: /* \033Ox or standard */
590 case XK_KP_Down: /* \033Or or standard */
591 case XK_KP_Right: /* \033Ov or standard */
592 case XK_KP_Left: /* \033Ot or standard */
593 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
594 {
595 strcpy (kbuf, "\033OZ");
596 kbuf[2] = "txvr"[keysym - XK_KP_Left];
597 break;
598 }
599 else
600 /* translate to std. cursor key */
601 keysym = XK_Left + (keysym - XK_KP_Left);
602 /* FALLTHROUGH */
603 #endif
604 case XK_Up: /* "\033[A" */
605 case XK_Down: /* "\033[B" */
606 case XK_Right: /* "\033[C" */
607 case XK_Left: /* "\033[D" */
608 strcpy (kbuf, "\033[Z");
609 kbuf[2] = "DACB"[keysym - XK_Left];
610 /* do Shift first */
611 if (shft)
612 kbuf[2] = "dacb"[keysym - XK_Left];
613 else if (ctrl)
614 {
615 kbuf[1] = 'O';
616 kbuf[2] = "dacb"[keysym - XK_Left];
617 }
618 else if (priv_modes & PrivMode_aplCUR)
619 kbuf[1] = 'O';
620 break;
621
622 #ifndef UNSHIFTED_SCROLLKEYS
623 # ifdef XK_KP_Prior
624 case XK_KP_Prior:
625 /* allow shift to override */
626 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
627 {
628 strcpy (kbuf, "\033Oy");
629 break;
630 }
631 /* FALLTHROUGH */
632 # endif
633 case XK_Prior:
634 strcpy (kbuf, "\033[5~");
635 break;
636 # ifdef XK_KP_Next
637 case XK_KP_Next:
638 /* allow shift to override */
639 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
640 {
641 strcpy (kbuf, "\033Os");
642 break;
643 }
644 /* FALLTHROUGH */
645 # endif
646 case XK_Next:
647 strcpy (kbuf, "\033[6~");
648 break;
649 #endif
650 case XK_KP_Enter:
651 /* allow shift to override */
652 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
653 {
654 strcpy (kbuf, "\033OM");
655 break;
656 }
657
658 /* FALLTHROUGH */
659
660 case XK_Return:
661 if (priv_modes & PrivMode_LFNL)
662 {
663 kbuf[0] = '\015';
664 kbuf[1] = '\012';
665 kbuf[2] = '\0';
666 }
667 else
668 {
669 kbuf[0] = '\015';
670 kbuf[1] = '\0';
671 }
672 break;
673
674 #ifdef XK_KP_Begin
675 case XK_KP_Begin:
676 strcpy (kbuf, "\033Ou");
677 break;
678
679 #endif
680 case XK_KP_F1: /* "\033OP" */
681 case XK_KP_F2: /* "\033OQ" */
682 case XK_KP_F3: /* "\033OR" */
683 case XK_KP_F4: /* "\033OS" */
684 strcpy (kbuf, "\033OP");
685 kbuf[2] += (keysym - XK_KP_F1);
686 break;
687
688 case XK_KP_Multiply: /* "\033Oj" : "*" */
689 case XK_KP_Add: /* "\033Ok" : "+" */
690 case XK_KP_Separator: /* "\033Ol" : "," */
691 case XK_KP_Subtract: /* "\033Om" : "-" */
692 case XK_KP_Decimal: /* "\033On" : "." */
693 case XK_KP_Divide: /* "\033Oo" : "/" */
694 case XK_KP_0: /* "\033Op" : "0" */
695 case XK_KP_1: /* "\033Oq" : "1" */
696 case XK_KP_2: /* "\033Or" : "2" */
697 case XK_KP_3: /* "\033Os" : "3" */
698 case XK_KP_4: /* "\033Ot" : "4" */
699 case XK_KP_5: /* "\033Ou" : "5" */
700 case XK_KP_6: /* "\033Ov" : "6" */
701 case XK_KP_7: /* "\033Ow" : "7" */
702 case XK_KP_8: /* "\033Ox" : "8" */
703 case XK_KP_9: /* "\033Oy" : "9" */
704 /* allow shift to override */
705 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
706 {
707 strcpy (kbuf, "\033Oj");
708 kbuf[2] += (keysym - XK_KP_Multiply);
709 }
710 else
711 {
712 kbuf[0] = ('*' + (keysym - XK_KP_Multiply));
713 kbuf[1] = '\0';
714 }
715 break;
716
717 case XK_Find:
718 strcpy (kbuf, "\033[1~");
719 break;
720
721 #ifdef XK_KP_End
722 case XK_KP_Insert:
723 /* allow shift to override */
724 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
725 {
726 strcpy (kbuf, "\033Op");
727 break;
728 }
729 /* FALLTHROUGH */
730 #endif
731 case XK_Insert:
732 strcpy (kbuf, "\033[2~");
733 break;
734 #ifdef DXK_Remove /* support for DEC remove like key */
735 case DXK_Remove:
736 /* FALLTHROUGH */
737 #endif
738 case XK_Execute:
739 strcpy (kbuf, "\033[3~");
740 break;
741 case XK_Select:
742 strcpy (kbuf, "\033[4~");
743 break;
744 #ifdef XK_KP_End
745 case XK_KP_End:
746 /* allow shift to override */
747 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
748 {
749 strcpy (kbuf, "\033Oq");
750 break;
751 }
752 /* FALLTHROUGH */
753 #endif
754 case XK_End:
755 strcpy (kbuf, KS_END);
756 break;
757 #ifdef XK_KP_Home
758 case XK_KP_Home:
759 /* allow shift to override */
760 if ((priv_modes & PrivMode_aplKP) ? !shft : shft)
761 {
762 strcpy (kbuf, "\033Ow");
763 break;
764 }
765 /* FALLTHROUGH */
766 #endif
767 case XK_Home:
768 strcpy (kbuf, KS_HOME);
769 break;
770
771 #define FKEY(n, fkey) \
772 sprintf ((char *)kbuf,"\033[%2d~", (int) ((n) + (keysym - fkey)))
773
774 case XK_F1: /* "\033[11~" */
775 case XK_F2: /* "\033[12~" */
776 case XK_F3: /* "\033[13~" */
777 case XK_F4: /* "\033[14~" */
778 case XK_F5: /* "\033[15~" */
779 FKEY (11, XK_F1);
780 break;
781 case XK_F6: /* "\033[17~" */
782 case XK_F7: /* "\033[18~" */
783 case XK_F8: /* "\033[19~" */
784 case XK_F9: /* "\033[20~" */
785 case XK_F10: /* "\033[21~" */
786 FKEY (17, XK_F6);
787 break;
788 case XK_F11: /* "\033[23~" */
789 case XK_F12: /* "\033[24~" */
790 case XK_F13: /* "\033[25~" */
791 case XK_F14: /* "\033[26~" */
792 FKEY (23, XK_F11);
793 break;
794 case XK_F15: /* "\033[28~" */
795 case XK_F16: /* "\033[29~" */
796 FKEY (28, XK_F15);
797 break;
798 case XK_Help: /* "\033[28~" */
799 FKEY (28, XK_Help);
800 break;
801 case XK_Menu: /* "\033[29~" */
802 FKEY (29, XK_Menu);
803 break;
804 case XK_F17: /* "\033[31~" */
805 case XK_F18: /* "\033[32~" */
806 case XK_F19: /* "\033[33~" */
807 case XK_F20: /* "\033[34~" */
808 case XK_F21: /* "\033[35~" */
809 case XK_F22: /* "\033[36~" */
810 case XK_F23: /* "\033[37~" */
811 case XK_F24: /* "\033[38~" */
812 case XK_F25: /* "\033[39~" */
813 case XK_F26: /* "\033[40~" */
814 case XK_F27: /* "\033[41~" */
815 case XK_F28: /* "\033[42~" */
816 case XK_F29: /* "\033[43~" */
817 case XK_F30: /* "\033[44~" */
818 case XK_F31: /* "\033[45~" */
819 case XK_F32: /* "\033[46~" */
820 case XK_F33: /* "\033[47~" */
821 case XK_F34: /* "\033[48~" */
822 case XK_F35: /* "\033[49~" */
823 FKEY (31, XK_F17);
824 break;
825 #undef FKEY
826 default:
827 newlen = 0;
828 break;
829 }
830
831 if (newlen)
832 len = strlen (kbuf);
833 }
834
835 /*
836 * Pass meta for all function keys, if 'meta' option set
837 */
838 #ifdef META8_OPTION
839 if (meta && (meta_char == 0x80) && len > 0)
840 kbuf[len - 1] |= 0x80;
841 #endif
842
843 }
844 else if (ctrl && keysym == XK_minus)
845 {
846 len = 1;
847 kbuf[0] = '\037'; /* Ctrl-Minus generates ^_ (31) */
848 }
849 else if (keysym == XK_ISO_Left_Tab)
850 {
851 strcpy (kbuf, "\033[Z");
852 len = 3;
853 }
854 else
855 {
856 #ifdef META8_OPTION
857 /* set 8-bit on */
858 if (meta && (meta_char == 0x80))
859 {
860 char *ch;
861
862 for (ch = kbuf; ch < kbuf + len; ch++)
863 *ch |= 0x80;
864
865 meta = 0;
866 }
867 #endif
868 /* nil */ ;
869 }
870 }
871
872 if (HOOK_INVOKE ((this, HOOK_KEY_PRESS, DT_XEVENT, &ev, DT_INT, keysym, DT_STR_LEN, kbuf, len, DT_END)))
873 return;
874
875 if (len <= 0)
876 return; /* not mapped */
877
878 if (OPTION (Opt_scrollTtyKeypress))
879 if (view_start)
880 {
881 view_start = 0;
882 want_refresh = 1;
883 }
884
885 /*
886 * these modifications only affect the static keybuffer
887 * pass Shift/Control indicators for function keys ending with `~'
888 *
889 * eg,
890 * Prior = "ESC[5~"
891 * Shift+Prior = "ESC[5$"
892 * Ctrl+Prior = "ESC[5^"
893 * Ctrl+Shift+Prior = "ESC[5@"
894 * Meta adds an Escape prefix (with META8_OPTION, if meta == <escape>).
895 */
896 if (kbuf[0] == C0_ESC && kbuf[1] == '[' && kbuf[len - 1] == '~')
897 kbuf[len - 1] = (shft ? (ctrl ? '@' : '$') : (ctrl ? '^' : '~'));
898
899 /* escape prefix */
900 if (meta
901 #ifdef META8_OPTION
902 && meta_char == C0_ESC
903 #endif
904 )
905 {
906 const char ch = C0_ESC;
907 tt_write (&ch, 1);
908 }
909
910 #if defined(DEBUG_CMD)
911 /* Display keyboard buffer contents */
912 unsigned char *p;
913 int i;
914
915 fprintf (stderr, "key 0x%04X [%d]: `", (unsigned int)keysym, len);
916 for (i = 0, p = kbuf; i < len; i++, p++)
917 fprintf (stderr, (*p >= ' ' && *p < '\177' ? "%c" : "\\%03o"), *p);
918 fprintf (stderr, "'\n");
919 #endif /* DEBUG_CMD */
920 tt_write (kbuf, (unsigned int)len);
921 }
922 /*}}} */
923
924 #if defined (KEYSYM_RESOURCE)
925 unsigned int
926 rxvt_term::cmd_write (const char *str, unsigned int count)
927 {
928 unsigned int n, s;
929
930 n = cmdbuf_ptr - cmdbuf_base;
931 s = cmdbuf_base + CBUFSIZ - 1 - cmdbuf_endp;
932
933 if (n > 0 && s < count)
934 {
935 memmove (cmdbuf_base, cmdbuf_ptr,
936 (unsigned int) (cmdbuf_endp - cmdbuf_ptr));
937 cmdbuf_ptr = cmdbuf_base;
938 cmdbuf_endp -= n;
939 s += n;
940 }
941
942 if (count > s)
943 {
944 rxvt_warn ("data loss: cmd_write too large, continuing.\n");
945 count = s;
946 }
947
948 for (; count--;)
949 *cmdbuf_endp++ = *str++;
950
951 cmd_parse ();
952
953 return 0;
954 }
955 #endif
956
957 void
958 rxvt_term::flush ()
959 {
960 flush_ev.stop ();
961
962 #ifdef TRANSPARENT
963 if (want_full_refresh)
964 {
965 want_full_refresh = 0;
966 scr_clear ();
967 scr_touch (false);
968 }
969 #endif
970
971 if (want_refresh)
972 {
973 if (SHOULD_INVOKE (HOOK_LINE_UPDATE))
974 {
975 int row = view_start;
976 int end_row = row + nrow;
977
978 while (row > top_row && ROW (row - 1).is_longer ())
979 --row;
980
981 do
982 {
983 int start_row = row;
984 line_t *l;
985
986 do
987 {
988 l = &ROW (row++);
989
990 if (!(l->f & LINE_FILTERED))
991 {
992 // line not filtered, mark it as filtered
993 l->f |= LINE_FILTERED;
994 while (l->is_longer ())
995 {
996 l = &ROW (row++);
997 l->f |= LINE_FILTERED;
998 }
999
1000 // and filter it
1001 HOOK_INVOKE ((this, HOOK_LINE_UPDATE, DT_INT, start_row, DT_END));
1002
1003 break;
1004 }
1005 }
1006 while (l->is_longer () && row < end_row);
1007 }
1008 while (row < end_row);
1009 }
1010
1011 scr_refresh (refresh_type);
1012 scrollbar_show (1);
1013 #ifdef USE_XIM
1014 IMSendSpot ();
1015 #endif
1016 }
1017
1018 display->flush ();
1019 }
1020
1021 void
1022 rxvt_term::check_cb (check_watcher &w)
1023 {
1024 make_current ();
1025
1026 display->flush ();
1027
1028 if (want_refresh && !flush_ev.active)
1029 flush_ev.start (NOW + 1. / 60.); // refresh at max. 60 hz normally
1030 }
1031
1032 void
1033 rxvt_term::flush_cb (time_watcher &w)
1034 {
1035 make_current ();
1036
1037 refresh_limit = 1;
1038 refresh_count = 0;
1039 flush ();
1040 }
1041
1042 #ifdef CURSOR_BLINK
1043 void
1044 rxvt_term::cursor_blink_cb (time_watcher &w)
1045 {
1046 hidden_cursor = !hidden_cursor;
1047 want_refresh = 1;
1048
1049 w.start (w.at + BLINK_INTERVAL);
1050 }
1051 #endif
1052
1053 #ifdef TEXT_BLINK
1054 void
1055 rxvt_term::text_blink_cb (time_watcher &w)
1056 {
1057 if (scr_refresh_rend (RS_Blink, RS_Blink))
1058 {
1059 hidden_text = !hidden_text;
1060 want_refresh = 1;
1061 w.start (w.at + TEXT_BLINK_INTERVAL);
1062 }
1063 }
1064 #endif
1065
1066 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
1067 void
1068 rxvt_term::cont_scroll_cb (time_watcher &w)
1069 {
1070 if ((scrollbar_isUp() || scrollbar_isDn()) &&
1071 scr_page (scrollbar_isUp() ? UP : DN, 1))
1072 {
1073 refresh_type |= SMOOTH_REFRESH;
1074 want_refresh = 1;
1075 w.start (w.at + SCROLLBAR_CONTINUOUS_DELAY);
1076 }
1077 }
1078 #endif
1079
1080 #ifdef SELECTION_SCROLLING
1081 void
1082 rxvt_term::sel_scroll_cb (time_watcher &w)
1083 {
1084 if (scr_page (scroll_selection_dir, scroll_selection_lines))
1085 {
1086 selection_extend (selection_save_x, selection_save_y, selection_save_state);
1087 refresh_type |= SMOOTH_REFRESH;
1088 want_refresh = 1;
1089 w.start (w.at + SCROLLBAR_CONTINUOUS_DELAY);
1090 }
1091 }
1092 #endif
1093
1094 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1095 void
1096 rxvt_term::slip_wheel_cb (time_watcher &w)
1097 {
1098 if (mouse_slip_wheel_speed == 0
1099 || mouse_slip_wheel_speed < 0 ? scr_page (DN, -mouse_slip_wheel_speed)
1100 : scr_page (UP, mouse_slip_wheel_speed))
1101 {
1102 if (view_start == top_row || view_start == 0)
1103 mouse_slip_wheel_speed = 0;
1104
1105 refresh_type |= SMOOTH_REFRESH;
1106 want_refresh = 1;
1107 w.start (w.at + SCROLLBAR_CONTINUOUS_DELAY);
1108 }
1109 }
1110 #endif
1111
1112 #if HAVE_SCHED_YIELD
1113 static struct event_handler
1114 {
1115 check_watcher cw_yield;
1116
1117 void yield (check_watcher &w)
1118 {
1119 sched_yield ();
1120 w.stop ();
1121 }
1122
1123 event_handler ()
1124 : cw_yield (this, &event_handler::yield)
1125 {
1126 }
1127 } event_handler;
1128 #endif
1129
1130 bool
1131 rxvt_term::pty_fill ()
1132 {
1133 ssize_t n = cmdbuf_endp - cmdbuf_ptr;
1134
1135 if (CBUFSIZ == n)
1136 {
1137 rxvt_warn ("PLEASE REPORT: pty_fill on full buffer, draining input, continuing.\n");
1138 n = 0;
1139 }
1140
1141 memmove (cmdbuf_base, cmdbuf_ptr, n);
1142 cmdbuf_ptr = cmdbuf_base;
1143 cmdbuf_endp = cmdbuf_ptr + n;
1144
1145 ssize_t r = read (pty.pty, cmdbuf_endp, CBUFSIZ - n);
1146
1147 if (r > 0)
1148 {
1149 cmdbuf_endp += r;
1150 return true;
1151 }
1152 else if (r < 0 && (errno == EAGAIN || errno == EINTR))
1153 {
1154 #if HAVE_SCHED_YIELD
1155 if (display->is_local)
1156 event_handler.cw_yield.start ();
1157 #endif
1158 }
1159 else
1160 {
1161 pty_ev.stop ();
1162
1163 if (!OPTION (Opt_hold))
1164 destroy ();
1165 }
1166
1167 return false;
1168 }
1169
1170 void
1171 rxvt_term::pty_cb (io_watcher &w, short revents)
1172 {
1173 make_current ();
1174
1175 if (revents & EVENT_READ)
1176 // loop, but don't allow a single term to monopolize us
1177 while (pty_fill ())
1178 if (cmd_parse ())
1179 break;
1180
1181 if (revents & EVENT_WRITE)
1182 pty_write ();
1183 }
1184
1185 void
1186 rxvt_term::pointer_unblank ()
1187 {
1188 XDefineCursor (display->display, vt, TermWin_cursor);
1189 recolour_cursor ();
1190
1191 #ifdef POINTER_BLANK
1192 hidden_pointer = 0;
1193
1194 if (OPTION (Opt_pointerBlank))
1195 pointer_ev.start (NOW + pointerBlankDelay);
1196 #endif
1197 }
1198
1199 #ifdef POINTER_BLANK
1200 void
1201 rxvt_term::pointer_blank ()
1202 {
1203 if (!OPTION (Opt_pointerBlank))
1204 return;
1205
1206 XDefineCursor (display->display, vt, display->blank_cursor);
1207 XFlush (display->display);
1208
1209 hidden_pointer = 1;
1210 }
1211
1212 void
1213 rxvt_term::pointer_cb (time_watcher &w)
1214 {
1215 make_current ();
1216
1217 pointer_blank ();
1218 }
1219 #endif
1220
1221 void
1222 rxvt_term::mouse_report (XButtonEvent &ev)
1223 {
1224 int button_number, key_state = 0;
1225 int x, y;
1226
1227 x = ev.x;
1228 y = ev.y;
1229 pixel_position (&x, &y);
1230
1231 if (MEvent.button == AnyButton)
1232 button_number = 3;
1233 else
1234 {
1235 button_number = MEvent.button - Button1;
1236 /* add 0x3D for wheel events, like xterm does */
1237 if (button_number >= 3)
1238 button_number += (64 - 3);
1239 }
1240
1241 if (priv_modes & PrivMode_MouseX10)
1242 {
1243 /*
1244 * do not report ButtonRelease
1245 * no state info allowed
1246 */
1247 key_state = 0;
1248 if (button_number == 3)
1249 return;
1250 }
1251 else
1252 {
1253 /* XTerm mouse reporting needs these values:
1254 * 4 = Shift
1255 * 8 = Meta
1256 * 16 = Control
1257 * plus will add in our own Double-Click reporting
1258 * 32 = Double Click
1259 */
1260 key_state = ((MEvent.state & ShiftMask) ? 4 : 0)
1261 + ((MEvent.state & ModMetaMask) ? 8 : 0)
1262 + ((MEvent.state & ControlMask) ? 16 : 0);
1263 #ifdef MOUSE_REPORT_DOUBLECLICK
1264 key_state += ((MEvent.clicks > 1) ? 32 : 0);
1265 #endif
1266 }
1267
1268 #if DEBUG_MOUSEREPORT
1269 fprintf (stderr, "Mouse [");
1270 if (key_state & 16)
1271 fputc ('C', stderr);
1272 if (key_state & 4)
1273 fputc ('S', stderr);
1274 if (key_state & 8)
1275 fputc ('A', stderr);
1276 if (key_state & 32)
1277 fputc ('2', stderr);
1278 fprintf (stderr, "]: <%d>, %d/%d\n",
1279 button_number,
1280 x + 1,
1281 y + 1);
1282 #endif
1283
1284 tt_printf ("\033[M%c%c%c",
1285 (32 + button_number + key_state),
1286 (32 + x + 1),
1287 (32 + y + 1));
1288 }
1289
1290 #ifdef USING_W11LIB
1291 void
1292 rxvt_W11_process_x_event (XEvent *ev)
1293 {
1294 rxvt_t *r = rxvt_get_r ();
1295
1296 x_cb (*ev);
1297 }
1298 #endif
1299
1300 /*{{{ process an X event */
1301 void
1302 rxvt_term::x_cb (XEvent &ev)
1303 {
1304 make_current ();
1305
1306 dDisp;
1307
1308 if (ev.xany.window == vt
1309 && SHOULD_INVOKE (HOOK_X_EVENT)
1310 && HOOK_INVOKE ((this, HOOK_X_EVENT, DT_XEVENT, &ev, DT_END)))
1311 return;
1312
1313 // for XQueryPointer
1314 Window unused_root, unused_child;
1315 int unused_root_x, unused_root_y;
1316 unsigned int unused_mask;
1317
1318 switch (ev.type)
1319 {
1320 case KeyPress:
1321 #if ISO_14755
1322 if (!(iso14755buf & ISO_14755_52))
1323 #endif
1324 lookup_key (ev.xkey);
1325
1326 break;
1327
1328 case KeyRelease:
1329 {
1330 #if (MOUSE_WHEEL && MOUSE_SLIP_WHEELING) || ISO_14755 || ENABLE_PERL
1331 KeySym keysym;
1332
1333 keysym = XLookupKeysym (&ev.xkey, ev.xkey.state & ShiftMask ? 1 : 0); // sorry, only shift supported :/
1334 #endif
1335
1336 #if ENABLE_FRILLS || ISO_14755
1337 // ISO 14755 support
1338 if (iso14755buf)
1339 if (iso14755buf & ISO_14755_52)
1340 {
1341 # if ENABLE_OVERLAY
1342 scr_overlay_off ();
1343 # endif
1344 # if ISO_14755
1345 // iso14755 part 5.2 handling: release time
1346 // first: controls
1347 if ((ev.xkey.state & ControlMask)
1348 && ((keysym >= 0x40 && keysym <= 0x5f)
1349 || (keysym >= 0x61 && keysym <= 0x7f)))
1350 {
1351 iso14755buf = ISO_14755_51 | 0x2400 | (keysym & 0x1f);
1352 commit_iso14755 ();
1353 goto skip_switch;
1354 }
1355
1356 for (unsigned short *i = iso14755_symtab; i[0]; i+= 2)
1357 if (i[0] == keysym)
1358 {
1359 iso14755buf = ISO_14755_51 | i[1];
1360 commit_iso14755 ();
1361 goto skip_switch;
1362 }
1363
1364 scr_bell ();
1365 # endif
1366 iso14755buf = 0;
1367 break;
1368 }
1369 else if ((ev.xkey.state & (ShiftMask | ControlMask)) != (ShiftMask | ControlMask))
1370 {
1371 # if ENABLE_OVERLAY
1372 scr_overlay_off ();
1373 # endif
1374 if (iso14755buf & ISO_14755_51)
1375 commit_iso14755 ();
1376 #if ISO_14755
1377 else if (iso14755buf & ISO_14755_STARTED)
1378 {
1379 iso14755buf = ISO_14755_52; // iso14755 part 5.2: remember empty begin/end pair
1380
1381 scr_overlay_new (0, -1, sizeof ("KEYCAP PICTURE INSERT MODE") - 1, 1);
1382 scr_overlay_set (0, 0, "KEYCAP PICTURE INSERT MODE");
1383 }
1384 # endif
1385 else
1386 iso14755buf = 0;
1387 }
1388 #endif
1389
1390 if (ev.xany.window == vt
1391 && HOOK_INVOKE ((this, HOOK_KEY_RELEASE, DT_XEVENT, &ev, DT_INT, keysym, DT_END)))
1392 break;
1393
1394 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1395 if (!(ev.xkey.state & ControlMask))
1396 slip_wheel_ev.stop ();
1397 else if (keysym == XK_Control_L || keysym == XK_Control_R)
1398 mouse_slip_wheel_speed = 0;
1399 #endif
1400 break;
1401 }
1402
1403 case ButtonPress:
1404 button_press (ev.xbutton);
1405 break;
1406
1407 case ButtonRelease:
1408 button_release (ev.xbutton);
1409 break;
1410
1411 case ClientMessage:
1412 if (ev.xclient.format == 32
1413 && ev.xclient.message_type == xa[XA_WM_PROTOCOLS])
1414 {
1415 if (ev.xclient.data.l[0] == xa[XA_WM_DELETE_WINDOW])
1416 destroy ();
1417 #if ENABLE_EWMH
1418 else if (ev.xclient.data.l[0] == xa[XA_NET_WM_PING])
1419 XSendEvent (disp, ev.xclient.window = display->root,
1420 False, SubstructureRedirectMask | SubstructureNotifyMask,
1421 &ev);
1422 #endif
1423 }
1424 #if ENABLE_XEMBED
1425 else if (ev.xclient.format == 32 && ev.xclient.message_type == xa[XA_XEMBED])
1426 {
1427 if (ev.xclient.data.l[1] == XEMBED_FOCUS_IN)
1428 focus_in ();
1429 else if (ev.xclient.data.l[1] == XEMBED_FOCUS_OUT)
1430 focus_out ();
1431 }
1432 #endif
1433 #ifdef OFFIX_DND
1434 /* OffiX Dnd (drag 'n' drop) protocol */
1435 else if (ev.xclient.message_type == xa[XA_DNDPROTOCOL]
1436 && (ev.xclient.data.l[0] == DndFile
1437 || ev.xclient.data.l[0] == DndDir
1438 || ev.xclient.data.l[0] == DndLink))
1439 {
1440 /* Get Dnd data */
1441 Atom ActualType;
1442 int ActualFormat;
1443 unsigned char *data;
1444 unsigned long Size, RemainingBytes;
1445
1446 XGetWindowProperty (disp, display->root,
1447 xa[XA_DNDSELECTION],
1448 0L, 1000000L,
1449 False, AnyPropertyType,
1450 &ActualType, &ActualFormat,
1451 &Size, &RemainingBytes,
1452 &data);
1453 set_string_property (XA_CUT_BUFFER0, data);
1454 XFree (data);
1455 selection_paste (display->root, XA_CUT_BUFFER0, true);
1456 XSetInputFocus (disp, display->root, RevertToNone, CurrentTime);
1457 }
1458 #endif /* OFFIX_DND */
1459 break;
1460
1461 case MappingNotify:
1462 XRefreshKeyboardMapping (&ev.xmapping);
1463 break;
1464
1465 /*
1466 * XXX: this is not the _current_ arrangement
1467 * Here's my conclusion:
1468 * If the window is completely unobscured, use bitblt's
1469 * to scroll. Even then, they're only used when doing partial
1470 * screen scrolling. When partially obscured, we have to fill
1471 * in the GraphicsExpose parts, which means that after each refresh,
1472 * we need to wait for the graphics expose or Noexpose events,
1473 * which ought to make things real slow!
1474 */
1475 case VisibilityNotify:
1476 switch (ev.xvisibility.state)
1477 {
1478 case VisibilityUnobscured:
1479 refresh_type = FAST_REFRESH;
1480 break;
1481 case VisibilityPartiallyObscured:
1482 refresh_type = SLOW_REFRESH;
1483 break;
1484 default:
1485 refresh_type = NO_REFRESH;
1486 break;
1487 }
1488 break;
1489
1490 case FocusIn:
1491 focus_in ();
1492 break;
1493
1494 case FocusOut:
1495 focus_out ();
1496 break;
1497
1498 case ConfigureNotify:
1499 if (ev.xconfigure.window == parent[0])
1500 {
1501 while (XCheckTypedWindowEvent (disp, ev.xconfigure.window, ConfigureNotify, &ev))
1502 ;
1503
1504 if (szHint.width != ev.xconfigure.width || szHint.height != ev.xconfigure.height)
1505 {
1506 seen_resize = 1;
1507 resize_all_windows (ev.xconfigure.width, ev.xconfigure.height, 1);
1508 }
1509
1510 #ifdef TRANSPARENT /* XXX: maybe not needed - leave in for now */
1511 if (OPTION (Opt_transparent))
1512 check_our_parents ();
1513 #endif
1514 }
1515 break;
1516
1517 case PropertyNotify:
1518 if (ev.xproperty.atom == xa[XA_VT_SELECTION]
1519 && ev.xproperty.state == PropertyNewValue)
1520 selection_property (ev.xproperty.window, ev.xproperty.atom);
1521
1522 break;
1523
1524 case SelectionClear:
1525 selection_clear ();
1526 break;
1527
1528 case SelectionNotify:
1529 if (selection_wait == Sel_normal)
1530 selection_paste (ev.xselection.requestor, ev.xselection.property, true);
1531 break;
1532
1533 case SelectionRequest:
1534 selection_send (ev.xselectionrequest);
1535 break;
1536
1537 case MapNotify:
1538 mapped = 1;
1539 #ifdef TEXT_BLINK
1540 text_blink_ev.start (NOW + TEXT_BLINK_INTERVAL);
1541 #endif
1542 HOOK_INVOKE ((this, HOOK_MAP_NOTIFY, DT_XEVENT, &ev, DT_END));
1543 break;
1544
1545 case UnmapNotify:
1546 mapped = 0;
1547 #ifdef TEXT_BLINK
1548 text_blink_ev.stop ();
1549 #endif
1550 HOOK_INVOKE ((this, HOOK_UNMAP_NOTIFY, DT_XEVENT, &ev, DT_END));
1551 break;
1552
1553 #ifdef TRANSPARENT
1554 case ReparentNotify:
1555 rootwin_cb (ev);
1556 break;
1557 #endif /* TRANSPARENT */
1558
1559 case GraphicsExpose:
1560 case Expose:
1561 if (ev.xany.window == vt)
1562 {
1563 do
1564 scr_expose (ev.xexpose.x, ev.xexpose.y,
1565 ev.xexpose.width, ev.xexpose.height, False);
1566 while (XCheckTypedWindowEvent (disp, vt, ev.xany.type, &ev));
1567
1568 ev.xany.type = ev.xany.type == Expose ? GraphicsExpose : Expose;
1569
1570 while (XCheckTypedWindowEvent (disp, vt, ev.xany.type, &ev))
1571 scr_expose (ev.xexpose.x, ev.xexpose.y,
1572 ev.xexpose.width, ev.xexpose.height, False);
1573
1574 want_refresh = 1;
1575 }
1576 else
1577 {
1578 XEvent unused_event;
1579
1580 while (XCheckTypedWindowEvent (disp, ev.xany.window, Expose, &unused_event))
1581 ;
1582 while (XCheckTypedWindowEvent (disp, ev.xany.window, GraphicsExpose, &unused_event))
1583 ;
1584
1585 if (isScrollbarWindow (ev.xany.window))
1586 {
1587 scrollBar.setIdle ();
1588 scrollbar_show (0);
1589 }
1590
1591 #ifdef TRANSPARENT
1592 if (am_transparent && ev.xany.window == parent[0])
1593 XClearWindow (disp, ev.xany.window);
1594 #endif
1595 }
1596 break;
1597
1598 case MotionNotify:
1599 #ifdef POINTER_BLANK
1600 if (hidden_pointer)
1601 pointer_unblank ();
1602 #endif
1603 if ((priv_modes & PrivMode_mouse_report) && !bypass_keystate)
1604 break;
1605
1606 if (ev.xany.window == vt)
1607 {
1608 if (SHOULD_INVOKE (HOOK_MOTION_NOTIFY)
1609 && HOOK_INVOKE ((this, HOOK_MOTION_NOTIFY, DT_XEVENT, &ev, DT_END)))
1610 ; // nop
1611 else if (ev.xbutton.state & (Button1Mask | Button3Mask))
1612 {
1613 while (XCheckTypedWindowEvent (disp, vt, MotionNotify, &ev))
1614 ;
1615
1616 XQueryPointer (disp, vt,
1617 &unused_root, &unused_child,
1618 &unused_root_x, &unused_root_y,
1619 &ev.xbutton.x, &ev.xbutton.y,
1620 &ev.xbutton.state);
1621 #ifdef MOUSE_THRESHOLD
1622 /* deal with a `jumpy' mouse */
1623 if ((ev.xmotion.time - MEvent.time) > MOUSE_THRESHOLD)
1624 {
1625 #endif
1626 #if ISO_14755
1627 // 5.4
1628 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
1629 {
1630 iso14755_54 (ev.xbutton.x, ev.xbutton.y);
1631 break;
1632 }
1633 #endif
1634 selection_extend (ev.xbutton.x, ev.xbutton.y,
1635 ev.xbutton.state & Button3Mask ? 2 : 0);
1636
1637 #ifdef SELECTION_SCROLLING
1638 if (ev.xbutton.y < int_bwidth
1639 || Pixel2Row (ev.xbutton.y) > (nrow-1))
1640 {
1641 int dist;
1642
1643 /* don't clobber the current delay if we are
1644 * already in the middle of scrolling.
1645 */
1646 if (!sel_scroll_ev.active)
1647 sel_scroll_ev.start (NOW + SCROLLBAR_INITIAL_DELAY);
1648
1649 /* save the event params so we can highlight
1650 * the selection in the pending-scroll loop
1651 */
1652 selection_save_x = ev.xbutton.x;
1653 selection_save_y = ev.xbutton.y;
1654 selection_save_state = (ev.xbutton.state & Button3Mask) ? 2 : 0;
1655
1656 /* calc number of lines to scroll */
1657 if (ev.xbutton.y < int_bwidth)
1658 {
1659 scroll_selection_dir = UP;
1660 dist = int_bwidth - ev.xbutton.y;
1661 }
1662 else
1663 {
1664 scroll_selection_dir = DN;
1665 dist = ev.xbutton.y - (int_bwidth + height);
1666 }
1667
1668 scroll_selection_lines = Pixel2Height (dist)
1669 / SELECTION_SCROLL_LINE_SPEEDUP
1670 + 1;
1671 min_it (scroll_selection_lines,
1672 SELECTION_SCROLL_MAX_LINES);
1673 }
1674 else
1675 {
1676 /* we are within the text window, so we
1677 * shouldn't be scrolling
1678 */
1679 if (sel_scroll_ev.active)
1680 sel_scroll_ev.stop();
1681 }
1682 #endif
1683 #ifdef MOUSE_THRESHOLD
1684 }
1685 #endif
1686 }
1687 }
1688 else if (isScrollbarWindow (ev.xany.window) && scrollbar_isMotion ())
1689 {
1690 while (XCheckTypedWindowEvent (disp, scrollBar.win,
1691 MotionNotify, &ev))
1692 ;
1693
1694 XQueryPointer (disp, scrollBar.win,
1695 &unused_root, &unused_child,
1696 &unused_root_x, &unused_root_y,
1697 &ev.xbutton.x, &ev.xbutton.y,
1698 &unused_mask);
1699 scr_move_to (scrollbar_position (ev.xbutton.y) - csrO,
1700 scrollbar_size ());
1701 want_refresh = 1;
1702 refresh_limit = 0;
1703 scrollbar_show (1);
1704 }
1705 break;
1706 }
1707
1708 skip_switch: ;
1709
1710 #if defined(CURSOR_BLINK)
1711 if (OPTION (Opt_cursorBlink) && ev.type == KeyPress)
1712 {
1713 if (hidden_cursor)
1714 {
1715 hidden_cursor = 0;
1716 want_refresh = 1;
1717 }
1718
1719 cursor_blink_ev.start (NOW + BLINK_INTERVAL);
1720 }
1721 #endif
1722
1723 #if defined(POINTER_BLANK)
1724 if (OPTION (Opt_pointerBlank) && pointerBlankDelay > 0)
1725 {
1726 if (ev.type == MotionNotify
1727 || ev.type == ButtonPress
1728 || ev.type == ButtonRelease)
1729 if (hidden_pointer)
1730 pointer_unblank ();
1731
1732 if (ev.type == KeyPress && hidden_pointer == 0)
1733 pointer_blank ();
1734 }
1735 #endif
1736 }
1737
1738 void
1739 rxvt_term::focus_in ()
1740 {
1741 if (!focus)
1742 {
1743 focus = 1;
1744 want_refresh = 1;
1745
1746 HOOK_INVOKE ((this, HOOK_FOCUS_OUT, DT_END));
1747
1748 #if USE_XIM
1749 if (Input_Context != NULL)
1750 {
1751 IMSetStatusPosition ();
1752 XSetICFocus (Input_Context);
1753 }
1754 #endif
1755 #if CURSOR_BLINK
1756 if (OPTION (Opt_cursorBlink))
1757 cursor_blink_ev.start (NOW + BLINK_INTERVAL);
1758 #endif
1759 #if OFF_FOCUS_FADING
1760 if (rs[Rs_fade])
1761 {
1762 pix_colors = pix_colors_focused;
1763 scr_recolour ();
1764 }
1765 #endif
1766 }
1767 }
1768
1769 void
1770 rxvt_term::focus_out ()
1771 {
1772 if (focus)
1773 {
1774 focus = 0;
1775 want_refresh = 1;
1776
1777 HOOK_INVOKE ((this, HOOK_FOCUS_OUT, DT_END));
1778
1779 #if ENABLE_FRILLS || ISO_14755
1780 if (iso14755buf)
1781 {
1782 iso14755buf = 0;
1783 # if ENABLE_OVERLAY
1784 scr_overlay_off ();
1785 # endif
1786 }
1787 #endif
1788 #if USE_XIM
1789 if (Input_Context != NULL)
1790 XUnsetICFocus (Input_Context);
1791 #endif
1792 #if CURSOR_BLINK
1793 if (OPTION (Opt_cursorBlink))
1794 cursor_blink_ev.stop ();
1795 hidden_cursor = 0;
1796 #endif
1797 #if OFF_FOCUS_FADING
1798 if (rs[Rs_fade])
1799 {
1800 pix_colors = pix_colors_unfocused;
1801 scr_recolour ();
1802 }
1803 #endif
1804 }
1805 }
1806
1807 #if TRANSPARENT
1808 void
1809 rxvt_term::rootwin_cb (XEvent &ev)
1810 {
1811 make_current ();
1812
1813 switch (ev.type)
1814 {
1815 case PropertyNotify:
1816 /*
1817 * if user used some Esetroot compatible prog to set the root bg,
1818 * use the property to determine the pixmap. We use it later on.
1819 */
1820 if (ev.xproperty.atom != xa[XA_XROOTPMAP_ID]
1821 && ev.xproperty.atom != xa[XA_ESETROOT_PMAP_ID])
1822 return;
1823
1824 /* FALLTHROUGH */
1825 case ReparentNotify:
1826 if (OPTION (Opt_transparent) && check_our_parents () && am_transparent)
1827 want_refresh = want_full_refresh = 1;
1828 break;
1829 }
1830 }
1831 #endif
1832
1833 void
1834 rxvt_term::button_press (XButtonEvent &ev)
1835 {
1836 int reportmode = 0, clickintime;
1837
1838 bypass_keystate = ev.state & (ModMetaMask | ShiftMask);
1839
1840 if (!bypass_keystate)
1841 reportmode = !! (priv_modes & PrivMode_mouse_report);
1842
1843 /*
1844 * VT window processing of button press
1845 */
1846 if (ev.window == vt)
1847 {
1848 #if ISO_14755
1849 // 5.4
1850 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
1851 {
1852 iso14755_54 (ev.x, ev.y);
1853 return;
1854 }
1855 #endif
1856
1857 clickintime = ev.time - MEvent.time < MULTICLICK_TIME;
1858
1859 if (reportmode)
1860 {
1861 /* mouse report from vt window */
1862 /* save the xbutton state (for ButtonRelease) */
1863 MEvent.state = ev.state;
1864 #ifdef MOUSE_REPORT_DOUBLECLICK
1865 if (ev.button == MEvent.button && clickintime)
1866 {
1867 /* same button, within alloted time */
1868 MEvent.clicks++;
1869
1870 if (MEvent.clicks > 1)
1871 {
1872 /* only report double clicks */
1873 MEvent.clicks = 2;
1874 mouse_report (ev);
1875
1876 /* don't report the release */
1877 MEvent.clicks = 0;
1878 MEvent.button = AnyButton;
1879 }
1880 }
1881 else
1882 {
1883 /* different button, or time expired */
1884 MEvent.clicks = 1;
1885 MEvent.button = ev.button;
1886 mouse_report (ev);
1887 }
1888 #else
1889 MEvent.button = ev.button;
1890 mouse_report (ev);
1891 #endif /* MOUSE_REPORT_DOUBLECLICK */
1892
1893 }
1894 else
1895 {
1896 if (ev.button != MEvent.button)
1897 MEvent.clicks = 0;
1898
1899 if (!HOOK_INVOKE ((this, HOOK_BUTTON_PRESS, DT_XEVENT, &ev, DT_END)))
1900 switch (ev.button)
1901 {
1902 case Button1:
1903 /* allow meta + click to select rectangular areas */
1904 /* should be done in screen.C */
1905 #if ENABLE_FRILLS
1906 selection.rect = !!(ev.state & ModMetaMask);
1907 #else
1908 selection.rect = false;
1909 #endif
1910
1911 /* allow shift+left click to extend selection */
1912 if (ev.state & ShiftMask && !(priv_modes & PrivMode_mouse_report))
1913 {
1914 if (MEvent.button == Button1 && clickintime)
1915 selection_rotate (ev.x, ev.y);
1916 else
1917 selection_extend (ev.x, ev.y, 1);
1918 }
1919 else
1920 {
1921 if (MEvent.button == Button1 && clickintime)
1922 MEvent.clicks++;
1923 else
1924 MEvent.clicks = 1;
1925
1926 selection_click (MEvent.clicks, ev.x, ev.y);
1927 }
1928
1929 MEvent.button = Button1;
1930 break;
1931
1932 case Button3:
1933 if (MEvent.button == Button3 && clickintime)
1934 selection_rotate (ev.x, ev.y);
1935 else
1936 selection_extend (ev.x, ev.y, 1);
1937
1938 MEvent.button = Button3;
1939 break;
1940 }
1941 }
1942
1943 MEvent.time = ev.time;
1944 return;
1945 }
1946
1947 /*
1948 * Scrollbar window processing of button press
1949 */
1950 if (isScrollbarWindow (ev.window))
1951 {
1952 scrollBar.setIdle ();
1953 /*
1954 * Rxvt-style scrollbar:
1955 * move up if mouse is above slider
1956 * move dn if mouse is below slider
1957 *
1958 * XTerm-style scrollbar:
1959 * Move display proportional to pointer location
1960 * pointer near top -> scroll one line
1961 * pointer near bot -> scroll full page
1962 */
1963 #ifndef NO_SCROLLBAR_REPORT
1964 if (reportmode)
1965 {
1966 /*
1967 * Mouse report disabled scrollbar:
1968 * arrow buttons - send up/down
1969 * click on scrollbar - send pageup/down
1970 */
1971 if ((scrollBar.style == R_SB_NEXT
1972 && scrollbarnext_upButton (ev.y))
1973 || (scrollBar.style == R_SB_RXVT
1974 && scrollbarrxvt_upButton (ev.y)))
1975 tt_printf ("\033[A");
1976 else if ((scrollBar.style == R_SB_NEXT
1977 && scrollbarnext_dnButton (ev.y))
1978 || (scrollBar.style == R_SB_RXVT
1979 && scrollbarrxvt_dnButton (ev.y)))
1980 tt_printf ("\033[B");
1981 else
1982 switch (ev.button)
1983 {
1984 case Button2:
1985 tt_printf ("\014");
1986 break;
1987 case Button1:
1988 tt_printf ("\033[6~");
1989 break;
1990 case Button3:
1991 tt_printf ("\033[5~");
1992 break;
1993 }
1994 }
1995 else
1996 #endif /* NO_SCROLLBAR_REPORT */
1997
1998 {
1999 char upordown = 0;
2000
2001 if (scrollBar.style == R_SB_NEXT)
2002 {
2003 if (scrollbarnext_upButton (ev.y))
2004 upordown = -1; /* up */
2005 else if (scrollbarnext_dnButton (ev.y))
2006 upordown = 1; /* down */
2007 }
2008 else if (scrollBar.style == R_SB_RXVT)
2009 {
2010 if (scrollbarrxvt_upButton (ev.y))
2011 upordown = -1; /* up */
2012 else if (scrollbarrxvt_dnButton (ev.y))
2013 upordown = 1; /* down */
2014 }
2015 if (upordown)
2016 {
2017 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
2018 cont_scroll_ev.start (NOW + SCROLLBAR_INITIAL_DELAY);
2019 #endif
2020 if (scr_page (upordown < 0 ? UP : DN, 1))
2021 {
2022 if (upordown < 0)
2023 scrollBar.setUp ();
2024 else
2025 scrollBar.setDn ();
2026 }
2027 }
2028 else
2029 switch (ev.button)
2030 {
2031 case Button2:
2032 switch (scrollbar_align)
2033 {
2034 case R_SB_ALIGN_TOP:
2035 csrO = 0;
2036 break;
2037 case R_SB_ALIGN_CENTRE:
2038 csrO = (scrollBar.bot - scrollBar.top) / 2;
2039 break;
2040 case R_SB_ALIGN_BOTTOM:
2041 csrO = scrollBar.bot - scrollBar.top;
2042 break;
2043 }
2044
2045 if (scrollBar.style == R_SB_XTERM
2046 || scrollbar_above_slider (ev.y)
2047 || scrollbar_below_slider (ev.y))
2048 scr_move_to (scrollbar_position (ev.y) - csrO, scrollbar_size ());
2049
2050 scrollBar.setMotion ();
2051 break;
2052
2053 case Button1:
2054 if (scrollbar_align == R_SB_ALIGN_CENTRE)
2055 csrO = ev.y - scrollBar.top;
2056 /* FALLTHROUGH */
2057
2058 case Button3:
2059 if (scrollBar.style != R_SB_XTERM)
2060 {
2061 if (scrollbar_above_slider (ev.y))
2062 # ifdef RXVT_SCROLL_FULL
2063 scr_page (UP, nrow - 1);
2064 # else
2065 scr_page (UP, nrow / 4);
2066 # endif
2067 else if (scrollbar_below_slider (ev.y))
2068 # ifdef RXVT_SCROLL_FULL
2069 scr_page (DN, nrow - 1);
2070 # else
2071 scr_page (DN, nrow / 4);
2072 # endif
2073 else
2074 scrollBar.setMotion ();
2075 }
2076 else
2077 {
2078 scr_page ((ev.button == Button1 ? DN : UP),
2079 (nrow
2080 * scrollbar_position (ev.y)
2081 / scrollbar_size ()));
2082 }
2083
2084 break;
2085 }
2086 }
2087
2088 return;
2089 }
2090 }
2091
2092 void
2093 rxvt_term::button_release (XButtonEvent &ev)
2094 {
2095 int reportmode = 0;
2096
2097 csrO = 0; /* reset csr Offset */
2098 if (!bypass_keystate)
2099 reportmode = !! (priv_modes & PrivMode_mouse_report);
2100
2101 if (scrollbar_isUpDn ())
2102 {
2103 scrollBar.setIdle ();
2104 scrollbar_show (0);
2105 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
2106 refresh_type &= ~SMOOTH_REFRESH;
2107 #endif
2108 }
2109
2110 #ifdef SELECTION_SCROLLING
2111 if (sel_scroll_ev.active)
2112 sel_scroll_ev.stop();
2113 #endif
2114
2115 if (ev.window == vt)
2116 {
2117 #if ISO_14755
2118 // 5.4
2119 if (iso14755buf & (ISO_14755_STARTED | ISO_14755_54))
2120 return;
2121 #endif
2122 if (reportmode)
2123 {
2124 /* mouse report from vt window */
2125 /* don't report release of wheel "buttons" */
2126 if (ev.button >= 4)
2127 return;
2128 #ifdef MOUSE_REPORT_DOUBLECLICK
2129 /* only report the release of 'slow' single clicks */
2130 if (MEvent.button != AnyButton
2131 && (ev.button != MEvent.button
2132 || (ev.time - MEvent.time
2133 > MULTICLICK_TIME / 2)))
2134 {
2135 MEvent.clicks = 0;
2136 MEvent.button = AnyButton;
2137 mouse_report (ev);
2138 }
2139 #else /* MOUSE_REPORT_DOUBLECLICK */
2140 MEvent.button = AnyButton;
2141 mouse_report (ev);
2142 #endif /* MOUSE_REPORT_DOUBLECLICK */
2143 return;
2144 }
2145
2146 /*
2147 * dumb hack to compensate for the failure of click-and-drag
2148 * when overriding mouse reporting
2149 */
2150 if (priv_modes & PrivMode_mouse_report
2151 && bypass_keystate
2152 && ev.button == Button1 && MEvent.clicks <= 1)
2153 selection_extend (ev.x, ev.y, 0);
2154
2155 if (HOOK_INVOKE ((this, HOOK_BUTTON_RELEASE, DT_XEVENT, &ev, DT_END)))
2156 return;
2157
2158 switch (ev.button)
2159 {
2160 case Button1:
2161 case Button3:
2162 selection_make (ev.time);
2163 break;
2164 case Button2:
2165 selection_request (ev.time, ev.x, ev.y);
2166 break;
2167 #ifdef MOUSE_WHEEL
2168 case Button4:
2169 case Button5:
2170 {
2171 int i;
2172 page_dirn v;
2173
2174 v = ev.button == Button4 ? UP : DN;
2175
2176 if (ev.state & ShiftMask)
2177 i = 1;
2178 else if (OPTION (Opt_mouseWheelScrollPage))
2179 i = nrow - 1;
2180 else
2181 i = 5;
2182
2183 # ifdef MOUSE_SLIP_WHEELING
2184 if (ev.state & ControlMask)
2185 {
2186 mouse_slip_wheel_speed += v ? -1 : 1;
2187 if (mouse_slip_wheel_speed < -nrow) mouse_slip_wheel_speed = -nrow;
2188 if (mouse_slip_wheel_speed > +nrow) mouse_slip_wheel_speed = +nrow;
2189
2190 if (slip_wheel_ev.at < NOW)
2191 slip_wheel_ev.at = NOW + SCROLLBAR_CONTINUOUS_DELAY;
2192
2193 slip_wheel_ev.start ();
2194 }
2195 else
2196 {
2197 # endif
2198 scr_page (v, i);
2199 scrollbar_show (1);
2200 # ifdef MOUSE_SLIP_WHEELING
2201 }
2202 # endif
2203 }
2204 break;
2205 #endif
2206 }
2207 }
2208 }
2209
2210 #ifdef TRANSPARENT
2211 #if TINTING
2212 /* taken from aterm-0.4.2 */
2213
2214 typedef uint32_t RUINT32T;
2215
2216 void ShadeXImage(rxvt_display *display, XImage* srcImage, int shade, int rm, int gm, int bm)
2217 {
2218 int sh_r, sh_g, sh_b;
2219 RUINT32T mask_r, mask_g, mask_b;
2220 RUINT32T *lookup, *lookup_r, *lookup_g, *lookup_b;
2221 unsigned int lower_lim_r, lower_lim_g, lower_lim_b;
2222 unsigned int upper_lim_r, upper_lim_g, upper_lim_b;
2223 int i;
2224
2225 Visual *visual = display->visual;
2226
2227 if( visual->c_class != TrueColor || srcImage->format != ZPixmap ) return ;
2228
2229 /* for convenience */
2230 mask_r = visual->red_mask;
2231 mask_g = visual->green_mask;
2232 mask_b = visual->blue_mask;
2233
2234 /* boring lookup table pre-initialization */
2235 switch (srcImage->bits_per_pixel) {
2236 case 15:
2237 if ((mask_r != 0x7c00) ||
2238 (mask_g != 0x03e0) ||
2239 (mask_b != 0x001f))
2240 return;
2241 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(32+32+32));
2242 lookup_r = lookup;
2243 lookup_g = lookup+32;
2244 lookup_b = lookup+32+32;
2245 sh_r = 10;
2246 sh_g = 5;
2247 sh_b = 0;
2248 break;
2249 case 16:
2250 if ((mask_r != 0xf800) ||
2251 (mask_g != 0x07e0) ||
2252 (mask_b != 0x001f))
2253 return;
2254 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(32+64+32));
2255 lookup_r = lookup;
2256 lookup_g = lookup+32;
2257 lookup_b = lookup+32+64;
2258 sh_r = 11;
2259 sh_g = 5;
2260 sh_b = 0;
2261 break;
2262 case 24:
2263 if ((mask_r != 0xff0000) ||
2264 (mask_g != 0x00ff00) ||
2265 (mask_b != 0x0000ff))
2266 return;
2267 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(256+256+256));
2268 lookup_r = lookup;
2269 lookup_g = lookup+256;
2270 lookup_b = lookup+256+256;
2271 sh_r = 16;
2272 sh_g = 8;
2273 sh_b = 0;
2274 break;
2275 case 32:
2276 if ((mask_r != 0xff0000) ||
2277 (mask_g != 0x00ff00) ||
2278 (mask_b != 0x0000ff))
2279 return;
2280 lookup = (RUINT32T *) malloc (sizeof (RUINT32T)*(256+256+256));
2281 lookup_r = lookup;
2282 lookup_g = lookup+256;
2283 lookup_b = lookup+256+256;
2284 sh_r = 16;
2285 sh_g = 8;
2286 sh_b = 0;
2287 break;
2288 default:
2289 return; /* we do not support this color depth */
2290 }
2291
2292 /* prepare limits for color transformation (each channel is handled separately) */
2293 if (shade < 0) {
2294 shade = -shade;
2295 if (shade < 0) shade = 0;
2296 if (shade > 100) shade = 100;
2297
2298 lower_lim_r = 65535-rm;
2299 lower_lim_g = 65535-gm;
2300 lower_lim_b = 65535-bm;
2301
2302 lower_lim_r = 65535-(unsigned int)(((RUINT32T)lower_lim_r)*((RUINT32T)shade)/100);
2303 lower_lim_g = 65535-(unsigned int)(((RUINT32T)lower_lim_g)*((RUINT32T)shade)/100);
2304 lower_lim_b = 65535-(unsigned int)(((RUINT32T)lower_lim_b)*((RUINT32T)shade)/100);
2305
2306 upper_lim_r = upper_lim_g = upper_lim_b = 65535;
2307 } else {
2308 if (shade < 0) shade = 0;
2309 if (shade > 100) shade = 100;
2310
2311 lower_lim_r = lower_lim_g = lower_lim_b = 0;
2312
2313 upper_lim_r = (unsigned int)((((RUINT32T)rm)*((RUINT32T)shade))/100);
2314 upper_lim_g = (unsigned int)((((RUINT32T)gm)*((RUINT32T)shade))/100);
2315 upper_lim_b = (unsigned int)((((RUINT32T)bm)*((RUINT32T)shade))/100);
2316 }
2317
2318 /* switch red and blue bytes if necessary, we need it for some weird XServers like XFree86 3.3.3.1 */
2319 if ((srcImage->bits_per_pixel == 24) && (mask_r >= 0xFF0000 ))
2320 {
2321 unsigned int tmp;
2322
2323 tmp = lower_lim_r;
2324 lower_lim_r = lower_lim_b;
2325 lower_lim_b = tmp;
2326
2327 tmp = upper_lim_r;
2328 upper_lim_r = upper_lim_b;
2329 upper_lim_b = tmp;
2330 }
2331
2332 /* fill our lookup tables */
2333 for (i = 0; i <= mask_r>>sh_r; i++)
2334 {
2335 RUINT32T tmp;
2336 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_r-lower_lim_r));
2337 tmp += ((RUINT32T)(mask_r>>sh_r))*((RUINT32T)lower_lim_r);
2338 lookup_r[i] = (tmp/65535)<<sh_r;
2339 }
2340 for (i = 0; i <= mask_g>>sh_g; i++)
2341 {
2342 RUINT32T tmp;
2343 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_g-lower_lim_g));
2344 tmp += ((RUINT32T)(mask_g>>sh_g))*((RUINT32T)lower_lim_g);
2345 lookup_g[i] = (tmp/65535)<<sh_g;
2346 }
2347 for (i = 0; i <= mask_b>>sh_b; i++)
2348 {
2349 RUINT32T tmp;
2350 tmp = ((RUINT32T)i)*((RUINT32T)(upper_lim_b-lower_lim_b));
2351 tmp += ((RUINT32T)(mask_b>>sh_b))*((RUINT32T)lower_lim_b);
2352 lookup_b[i] = (tmp/65535)<<sh_b;
2353 }
2354
2355 /* apply table to input image (replacing colors by newly calculated ones) */
2356 switch (srcImage->bits_per_pixel)
2357 {
2358 case 15:
2359 {
2360 unsigned short *p1, *pf, *p, *pl;
2361 p1 = (unsigned short *) srcImage->data;
2362 pf = (unsigned short *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
2363 while (p1 < pf)
2364 {
2365 p = p1;
2366 pl = p1 + srcImage->width;
2367 for (; p < pl; p++)
2368 {
2369 *p = lookup_r[(*p & 0x7c00)>>10] |
2370 lookup_g[(*p & 0x03e0)>> 5] |
2371 lookup_b[(*p & 0x001f)];
2372 }
2373 p1 = (unsigned short *) ((char *) p1 + srcImage->bytes_per_line);
2374 }
2375 break;
2376 }
2377 case 16:
2378 {
2379 unsigned short *p1, *pf, *p, *pl;
2380 p1 = (unsigned short *) srcImage->data;
2381 pf = (unsigned short *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
2382 while (p1 < pf)
2383 {
2384 p = p1;
2385 pl = p1 + srcImage->width;
2386 for (; p < pl; p++)
2387 {
2388 *p = lookup_r[(*p & 0xf800)>>11] |
2389 lookup_g[(*p & 0x07e0)>> 5] |
2390 lookup_b[(*p & 0x001f)];
2391 }
2392 p1 = (unsigned short *) ((char *) p1 + srcImage->bytes_per_line);
2393 }
2394 break;
2395 }
2396 case 24:
2397 {
2398 unsigned char *p1, *pf, *p, *pl;
2399 p1 = (unsigned char *) srcImage->data;
2400 pf = (unsigned char *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
2401 while (p1 < pf)
2402 {
2403 p = p1;
2404 pl = p1 + srcImage->width * 3;
2405 for (; p < pl; p += 3)
2406 {
2407 p[0] = lookup_r[(p[0] & 0xff0000)>>16];
2408 p[1] = lookup_r[(p[1] & 0x00ff00)>> 8];
2409 p[2] = lookup_r[(p[2] & 0x0000ff)];
2410 }
2411 p1 = (unsigned char *) ((char *) p1 + srcImage->bytes_per_line);
2412 }
2413 break;
2414 }
2415 case 32:
2416 {
2417 RUINT32T *p1, *pf, *p, *pl;
2418 p1 = (RUINT32T *) srcImage->data;
2419 pf = (RUINT32T *) (srcImage->data + srcImage->height * srcImage->bytes_per_line);
2420
2421 while (p1 < pf)
2422 {
2423 p = p1;
2424 pl = p1 + srcImage->width;
2425 for (; p < pl; p++)
2426 {
2427 *p = lookup_r[(*p & 0xff0000)>>16] |
2428 lookup_g[(*p & 0x00ff00)>> 8] |
2429 lookup_b[(*p & 0x0000ff)] |
2430 (*p & ~0xffffff);
2431 }
2432 p1 = (RUINT32T *) ((char *) p1 + srcImage->bytes_per_line);
2433 }
2434 break;
2435 }
2436 }
2437
2438 free (lookup);
2439 }
2440 #endif
2441
2442 /*
2443 * Check our parents are still who we think they are.
2444 * Do transparency updates if required
2445 */
2446 int
2447 rxvt_term::check_our_parents ()
2448 {
2449 int i, pchanged, aformat, have_pixmap, rootdepth;
2450 unsigned long nitems, bytes_after;
2451 Atom atype;
2452 unsigned char *prop = NULL;
2453 Window root, oldp, *list;
2454 Pixmap rootpixmap = None;
2455 XWindowAttributes wattr, wrootattr;
2456 dDisp;
2457
2458 pchanged = 0;
2459
2460 if (!OPTION (Opt_transparent))
2461 return pchanged; /* Don't try any more */
2462
2463 XGetWindowAttributes (disp, display->root, &wrootattr);
2464 rootdepth = wrootattr.depth;
2465
2466 XGetWindowAttributes (disp, parent[0], &wattr);
2467
2468 if (rootdepth != wattr.depth)
2469 {
2470 if (am_transparent)
2471 {
2472 pchanged = 1;
2473 XSetWindowBackground (disp, vt, pix_colors_focused[Color_bg]);
2474 am_transparent = am_pixmap_trans = 0;
2475 }
2476
2477 return pchanged; /* Don't try any more */
2478 }
2479
2480 /* Get all X ops out of the queue so that our information is up-to-date. */
2481 XSync (disp, False);
2482
2483 /*
2484 * Make the frame window set by the window manager have
2485 * the root background. Some window managers put multiple nested frame
2486 * windows for each client, so we have to take care about that.
2487 */
2488 i = (xa[XA_XROOTPMAP_ID]
2489 && XGetWindowProperty (disp, display->root, xa[XA_XROOTPMAP_ID],
2490 0L, 1L, False, XA_PIXMAP, &atype, &aformat,
2491 &nitems, &bytes_after, &prop) == Success);
2492
2493 if (!i || prop == NULL)
2494 i = (xa[XA_ESETROOT_PMAP_ID]
2495 && XGetWindowProperty (disp, display->root, xa[XA_ESETROOT_PMAP_ID],
2496 0L, 1L, False, XA_PIXMAP, &atype, &aformat,
2497 &nitems, &bytes_after, &prop) == Success);
2498
2499 if (!i || prop == NULL
2500 #if TINTING
2501 || !rs[Rs_color + Color_tint]
2502 #endif
2503 )
2504 have_pixmap = 0;
2505 else
2506 {
2507 have_pixmap = 1;
2508 rootpixmap = *(Pixmap *)prop;
2509 XFree (prop);
2510 }
2511
2512 if (have_pixmap)
2513 {
2514 /*
2515 * Copy display->root pixmap transparency
2516 */
2517 int sx, sy, nx, ny;
2518 unsigned int nw, nh;
2519 Window cr;
2520 XImage *image;
2521 GC gc;
2522 XGCValues gcvalue;
2523
2524 XTranslateCoordinates (disp, parent[0], display->root,
2525 0, 0, &sx, &sy, &cr);
2526 nw = (unsigned int)szHint.width;
2527 nh = (unsigned int)szHint.height;
2528 nx = ny = 0;
2529
2530 if (sx < 0)
2531 {
2532 nw += sx;
2533 nx = -sx;
2534 sx = 0;
2535 }
2536
2537 if (sy < 0)
2538 {
2539 nh += sy;
2540 ny = -sy;
2541 sy = 0;
2542 }
2543
2544 min_it (nw, (unsigned int) (wrootattr.width - sx));
2545 min_it (nh, (unsigned int) (wrootattr.height - sy));
2546
2547 XSync (disp, False);
2548 allowedxerror = -1;
2549 image = XGetImage (disp, rootpixmap, sx, sy, nw, nh, AllPlanes, ZPixmap);
2550
2551 /* XXX: handle BadMatch - usually because we're outside the pixmap */
2552 /* XXX: may need a delay here? */
2553 allowedxerror = 0;
2554
2555 if (image == NULL)
2556 {
2557 if (am_transparent && am_pixmap_trans)
2558 {
2559 pchanged = 1;
2560 if (pixmap != None)
2561 {
2562 XFreePixmap (disp, pixmap);
2563 pixmap = None;
2564 }
2565 }
2566
2567 am_pixmap_trans = 0;
2568 }
2569 else
2570 {
2571 if (pixmap != None)
2572 XFreePixmap (disp, pixmap);
2573
2574 #if TINTING
2575 if (ISSET_PIXCOLOR (Color_tint))
2576 {
2577 unsigned short rm, gm, bm;
2578 int shade = rs[Rs_shade] ? atoi (rs[Rs_shade]) : 100;
2579
2580 pix_colors_focused[Color_tint].get (display, rm, gm, bm);
2581
2582 ShadeXImage (display, image, shade, rm, gm, bm);
2583 }
2584 #endif
2585
2586 pixmap = XCreatePixmap (disp, vt,
2587 szHint.width, szHint.height, image->depth);
2588 gc = XCreateGC (disp, vt, 0UL, &gcvalue);
2589 XPutImage (disp, pixmap, gc, image, 0, 0,
2590 nx, ny, image->width, image->height);
2591 XFreeGC (disp, gc);
2592 XDestroyImage (image);
2593 XSetWindowBackgroundPixmap (disp, parent[0], pixmap);
2594 XClearWindow (disp, parent[0]);
2595
2596 if (!am_transparent || !am_pixmap_trans)
2597 pchanged = 1;
2598
2599 am_transparent = am_pixmap_trans = 1;
2600 }
2601 }
2602
2603 if (am_pixmap_trans)
2604 XSetWindowBackgroundPixmap (disp, vt, ParentRelative);
2605 else
2606 {
2607 unsigned int n;
2608 /*
2609 * InheritPixmap transparency
2610 */
2611 for (i = 1; i < (int) (sizeof (parent) / sizeof (Window)); i++)
2612 {
2613 oldp = parent[i];
2614 XQueryTree (disp, parent[i - 1], &root,
2615 &parent[i], &list, &n);
2616 XFree (list);
2617
2618 if (parent[i] == display->root)
2619 {
2620 if (oldp != None)
2621 pchanged = 1;
2622
2623 break;
2624 }
2625
2626 if (oldp != parent[i])
2627 pchanged = 1;
2628 }
2629
2630 n = 0;
2631
2632 if (pchanged)
2633 {
2634 for (; n < (unsigned int)i; n++)
2635 {
2636 XGetWindowAttributes (disp, parent[n], &wattr);
2637 if (wattr.depth != rootdepth || wattr.c_class == InputOnly)
2638 {
2639 n = (int) (sizeof (parent) / sizeof (Window)) + 1;
2640 break;
2641 }
2642 }
2643 }
2644
2645 if (n > (int) (sizeof (parent) / sizeof (parent[0])))
2646 {
2647 XSetWindowBackground (disp, parent[0], pix_colors_focused[Color_border]);
2648 XSetWindowBackground (disp, vt, pix_colors_focused[Color_bg]);
2649 am_transparent = 0;
2650 /* XXX: also turn off Opt_transparent? */
2651 }
2652 else
2653 {
2654 #if WAIT_FOR_WM
2655 /* wait (an arbitrary period) for the WM to do its thing
2656 * needed for fvwm2.2.2 (and before?) */
2657 sleep (1);
2658 #endif
2659 for (n = 0; n < (unsigned int)i; n++)
2660 {
2661 XSetWindowBackgroundPixmap (disp, parent[n], ParentRelative);
2662 XClearWindow (disp, parent[n]);
2663 }
2664
2665 XSetWindowBackgroundPixmap (disp, vt, ParentRelative);
2666 am_transparent = 1;
2667 }
2668
2669 for (; i < (int) (sizeof (parent) / sizeof (Window)); i++)
2670 parent[i] = None;
2671 }
2672
2673 if (scrollBar.win)
2674 {
2675 XSetWindowBackgroundPixmap (disp, scrollBar.win, ParentRelative);
2676 scrollBar.setIdle ();
2677 scrollbar_show (0);
2678 }
2679
2680 if (am_transparent)
2681 {
2682 want_refresh = want_full_refresh = 1;
2683 if (am_pixmap_trans)
2684 flush ();
2685 }
2686
2687 return pchanged;
2688 }
2689 #endif
2690
2691 /*}}} */
2692
2693 bool
2694 rxvt_term::cmd_parse ()
2695 {
2696 bool flag = false;
2697 wchar_t ch = NOCHAR;
2698 char *seq_begin; // remember start of esc-sequence here
2699
2700 for (;;)
2701 {
2702 if (ch == NOCHAR)
2703 {
2704 seq_begin = cmdbuf_ptr;
2705 ch = next_char ();
2706
2707 if (ch == NOCHAR)
2708 break;
2709 }
2710
2711 if (!IS_CONTROL (ch) || ch == C0_LF || ch == C0_CR || ch == C0_HT)
2712 {
2713 if (!seen_input)
2714 {
2715 seen_input = 1;
2716 // many badly-written programs (e.g. jed) contain a race condition:
2717 // they first read the screensize and then install a SIGWINCH handler.
2718 // some window managers resize the window early, and these programs
2719 // then sometimes get the size wrong.
2720 // unfortunately other programs are even more buggy and dislike
2721 // being sent SIGWINCH, so only do it when we were in fact being
2722 // resized.
2723 if (seen_resize && cmd_pid)
2724 kill (-cmd_pid, SIGWINCH);
2725 }
2726
2727 /* Read a text string from the input buffer */
2728 wchar_t buf[UBUFSIZ];
2729 bool refreshnow = false;
2730 int nlines = 0;
2731 wchar_t *str = buf;
2732 wchar_t *eol = str + min (ncol, UBUFSIZ);
2733
2734 for (;;)
2735 {
2736 if (ch == NOCHAR || (IS_CONTROL (ch) && ch != C0_LF && ch != C0_CR && ch != C0_HT))
2737 break;
2738
2739 *str++ = ch;
2740
2741 if (ch == C0_LF || str >= eol)
2742 {
2743 if (ch == C0_LF)
2744 nlines++;
2745
2746 refresh_count++;
2747
2748 if (!OPTION (Opt_jumpScroll)
2749 || (refresh_count >= refresh_limit * (nrow - 1)))
2750 {
2751 refreshnow = true;
2752 refresh_count = 0;
2753 ch = NOCHAR;
2754 break;
2755 }
2756
2757 // scr_add_lines only works for nlines <= nrow - 1.
2758 if (nlines >= nrow - 1)
2759 {
2760 if (!(SHOULD_INVOKE (HOOK_ADD_LINES)
2761 && HOOK_INVOKE ((this, HOOK_ADD_LINES, DT_WCS_LEN, buf, str - buf, DT_END))))
2762 scr_add_lines (buf, str - buf, nlines);
2763
2764 nlines = 0;
2765 str = buf;
2766 eol = str + min (ncol, UBUFSIZ);
2767 }
2768
2769 if (str >= eol)
2770 {
2771 if (eol >= buf + UBUFSIZ)
2772 {
2773 ch = NOCHAR;
2774 break;
2775 }
2776 else
2777 eol = min (eol + ncol, buf + UBUFSIZ);
2778 }
2779
2780 }
2781
2782 seq_begin = cmdbuf_ptr;
2783 ch = next_char ();
2784 }
2785
2786 if (!(SHOULD_INVOKE (HOOK_ADD_LINES)
2787 && HOOK_INVOKE ((this, HOOK_ADD_LINES, DT_WCS_LEN, buf, str - buf, DT_END))))
2788 scr_add_lines (buf, str - buf, nlines);
2789
2790 /*
2791 * If there have been a lot of new lines, then update the screen
2792 * What the heck I'll cheat and only refresh less than every page-full.
2793 * the number of pages between refreshes is refresh_limit, which
2794 * is incremented here because we must be doing flat-out scrolling.
2795 */
2796 if (refreshnow)
2797 {
2798 if (OPTION (Opt_jumpScroll) && refresh_limit < REFRESH_PERIOD)
2799 refresh_limit++;
2800 else
2801 {
2802 flag = true;
2803 scr_refresh (refresh_type);
2804 want_refresh = 1;
2805 }
2806 }
2807
2808 }
2809 else
2810 {
2811 try
2812 {
2813 process_nonprinting (ch);
2814 }
2815 catch (const class out_of_input &o)
2816 {
2817 // we ran out of input, retry later
2818 cmdbuf_ptr = seq_begin;
2819 break;
2820 }
2821
2822 ch = NOCHAR;
2823 }
2824 }
2825
2826 return flag;
2827 }
2828
2829 // read the next character
2830 wchar_t
2831 rxvt_term::next_char ()
2832 {
2833 while (cmdbuf_ptr < cmdbuf_endp)
2834 {
2835 // assume 7-bit to be ascii ALWAYS
2836 if ((unsigned char)*cmdbuf_ptr <= 0x7f && *cmdbuf_ptr != 0x1b)
2837 return *cmdbuf_ptr++;
2838
2839 wchar_t wc;
2840 size_t len = mbrtowc (&wc, cmdbuf_ptr, cmdbuf_endp - cmdbuf_ptr, mbstate);
2841
2842 if (len == (size_t)-2)
2843 {
2844 // the mbstate stores incomplete sequences. didn't know this :/
2845 cmdbuf_ptr = cmdbuf_endp;
2846 break;
2847 }
2848
2849 if (len == (size_t)-1)
2850 return (unsigned char)*cmdbuf_ptr++; // the _occasional_ latin1 character is allowed to slip through
2851
2852 // assume wchar == unicode
2853 cmdbuf_ptr += len;
2854 return wc & UNICODE_MASK;
2855 }
2856
2857 return NOCHAR;
2858 }
2859
2860 // read the next octet
2861 uint32_t
2862 rxvt_term::next_octet ()
2863 {
2864 return cmdbuf_ptr < cmdbuf_endp
2865 ? (unsigned char)*cmdbuf_ptr++
2866 : NOCHAR;
2867 }
2868
2869 /* rxvt_cmd_getc () - Return next input character */
2870 /*
2871 * Return the next input character after first passing any keyboard input
2872 * to the command.
2873 */
2874 wchar_t
2875 rxvt_term::cmd_getc ()
2876 {
2877 wchar_t c = next_char ();
2878
2879 if (c == NOCHAR)
2880 throw out_of_input;
2881
2882 return c;
2883 }
2884
2885 uint32_t
2886 rxvt_term::cmd_get8 ()
2887 {
2888 uint32_t c = next_octet ();
2889
2890 if (c == NOCHAR)
2891 throw out_of_input;
2892
2893 return c;
2894 }
2895
2896 /*{{{ print pipe */
2897 /*----------------------------------------------------------------------*/
2898 #ifdef PRINTPIPE
2899 FILE *
2900 rxvt_term::popen_printer ()
2901 {
2902 FILE *stream = popen (rs[Rs_print_pipe], "w");
2903
2904 if (stream == NULL)
2905 rxvt_warn ("can't open printer pipe, not printing.\n");
2906
2907 return stream;
2908 }
2909
2910 int
2911 rxvt_term::pclose_printer (FILE *stream)
2912 {
2913 fflush (stream);
2914 return pclose (stream);
2915 }
2916
2917 /*
2918 * simulate attached vt100 printer
2919 */
2920 void
2921 rxvt_term::process_print_pipe ()
2922 {
2923 int done;
2924 FILE *fd;
2925
2926 if ((fd = popen_printer ()) == NULL)
2927 return;
2928
2929 /*
2930 * Send all input to the printer until either ESC[4i or ESC[?4i
2931 * is received.
2932 */
2933 for (done = 0; !done;)
2934 {
2935 unsigned char buf[8];
2936 unicode_t ch;
2937 unsigned int i, len;
2938
2939 if ((ch = cmd_getc ()) != C0_ESC)
2940 {
2941 if (putc (ch, fd) == EOF)
2942 break; /* done = 1 */
2943 }
2944 else
2945 {
2946 len = 0;
2947 buf[len++] = ch;
2948
2949 if ((buf[len++] = cmd_getc ()) == '[')
2950 {
2951 if ((ch = cmd_getc ()) == '?')
2952 {
2953 buf[len++] = '?';
2954 ch = cmd_getc ();
2955 }
2956 if ((buf[len++] = ch) == '4')
2957 {
2958 if ((buf[len++] = cmd_getc ()) == 'i')
2959 break; /* done = 1 */
2960 }
2961 }
2962
2963 for (i = 0; i < len; i++)
2964 if (putc (buf[i], fd) == EOF)
2965 {
2966 done = 1;
2967 break;
2968 }
2969 }
2970 }
2971
2972 pclose_printer (fd);
2973 }
2974 #endif /* PRINTPIPE */
2975 /*}}} */
2976
2977 /* *INDENT-OFF* */
2978 enum {
2979 C1_40 = 0x40,
2980 C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
2981 C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
2982 C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
2983 C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
2984 };
2985 /* *INDENT-ON* */
2986
2987 /*{{{ process non-printing single characters */
2988 void
2989 rxvt_term::process_nonprinting (unicode_t ch)
2990 {
2991 switch (ch)
2992 {
2993 case C0_ESC:
2994 process_escape_seq ();
2995 break;
2996 case C0_ENQ: /* terminal Status */
2997 if (rs[Rs_answerbackstring])
2998 tt_write (rs [Rs_answerbackstring], strlen (rs [Rs_answerbackstring]));
2999 else
3000 tt_write (VT100_ANS, strlen (VT100_ANS));
3001 break;
3002 case C0_BEL: /* bell */
3003 scr_bell ();
3004 break;
3005 case C0_BS: /* backspace */
3006 scr_backspace ();
3007 break;
3008 case C0_HT: /* tab */
3009 scr_tab (1);
3010 break;
3011 case C0_CR: /* carriage return */
3012 scr_gotorc (0, 0, R_RELATIVE);
3013 break;
3014 case C0_VT: /* vertical tab, form feed */
3015 case C0_FF:
3016 case C0_LF: /* line feed */
3017 scr_index (UP);
3018 break;
3019 case C0_SO: /* shift out - acs */
3020 scr_charset_choose (1);
3021 break;
3022 case C0_SI: /* shift in - acs */
3023 scr_charset_choose (0);
3024 break;
3025
3026 #ifdef EIGHT_BIT_CONTROLS
3027 // 8-bit controls
3028 case 0x90: /* DCS */
3029 process_dcs_seq ();
3030 break;
3031 case 0x9b: /* CSI */
3032 process_csi_seq ();
3033 break;
3034 case 0x9d: /* CSI */
3035 process_osc_seq ();
3036 break;
3037 #endif
3038 }
3039 }
3040 /*}}} */
3041
3042
3043 /*{{{ process VT52 escape sequences */
3044 void
3045 rxvt_term::process_escape_vt52 (unicode_t ch)
3046 {
3047 int row, col;
3048
3049 switch (ch)
3050 {
3051 case 'A': /* cursor up */
3052 scr_gotorc (-1, 0, R_RELATIVE | C_RELATIVE);
3053 break;
3054 case 'B': /* cursor down */
3055 scr_gotorc (1, 0, R_RELATIVE | C_RELATIVE);
3056 break;
3057 case 'C': /* cursor right */
3058 scr_gotorc (0, 1, R_RELATIVE | C_RELATIVE);
3059 break;
3060 case 'D': /* cursor left */
3061 scr_gotorc (0, -1, R_RELATIVE | C_RELATIVE);
3062 break;
3063 case 'H': /* cursor home */
3064 scr_gotorc (0, 0, 0);
3065 break;
3066 case 'I': /* cursor up and scroll down if needed */
3067 scr_index (DN);
3068 break;
3069 case 'J': /* erase to end of screen */
3070 scr_erase_screen (0);
3071 break;
3072 case 'K': /* erase to end of line */
3073 scr_erase_line (0);
3074 break;
3075 case 'Y': /* move to specified row and col */
3076 /* full command is 'ESC Y row col' where row and col
3077 * are encoded by adding 32 and sending the ascii
3078 * character. eg. SPACE = 0, '+' = 13, '0' = 18,
3079 * etc. */
3080 row = cmd_getc () - ' ';
3081 col = cmd_getc () - ' ';
3082 scr_gotorc (row, col, 0);
3083 break;
3084 case 'Z': /* identify the terminal type */
3085 tt_printf ("\033/Z"); /* I am a VT100 emulating a VT52 */
3086 break;
3087 case '<': /* turn off VT52 mode */
3088 PrivMode (0, PrivMode_vt52);
3089 break;
3090 case 'F': /* use special graphics character set */
3091 case 'G': /* use regular character set */
3092 /* unimplemented */
3093 break;
3094 case '=': /* use alternate keypad mode */
3095 case '>': /* use regular keypad mode */
3096 /* unimplemented */
3097 break;
3098 }
3099 }
3100 /*}}} */
3101
3102
3103 /*{{{ process escape sequences */
3104 void
3105 rxvt_term::process_escape_seq ()
3106 {
3107 unicode_t ch = cmd_getc ();
3108
3109 if (priv_modes & PrivMode_vt52)
3110 {
3111 process_escape_vt52 (ch);
3112 return;
3113 }
3114
3115 switch (ch)
3116 {
3117 /* case 1: do_tek_mode (); break; */
3118 case '#':
3119 if (cmd_getc () == '8')
3120 scr_E ();
3121 break;
3122 case '(':
3123 scr_charset_set (0, (unsigned int)cmd_getc ());
3124 break;
3125 case ')':
3126 scr_charset_set (1, (unsigned int)cmd_getc ());
3127 break;
3128 case '*':
3129 scr_charset_set (2, (unsigned int)cmd_getc ());
3130 break;
3131 case '+':
3132 scr_charset_set (3, (unsigned int)cmd_getc ());
3133 break;
3134 #if ENABLE_FRILLS
3135 case '6':
3136 scr_backindex ();
3137 break;
3138 #endif
3139 case '7':
3140 scr_cursor (SAVE);
3141 break;
3142 case '8':
3143 scr_cursor (RESTORE);
3144 break;
3145 #if ENABLE_FRILLS
3146 case '9':
3147 scr_forwardindex ();
3148 break;
3149 #endif
3150 case '=':
3151 case '>':
3152 PrivMode ((ch == '='), PrivMode_aplKP);
3153 break;
3154
3155 case C1_40:
3156 cmd_getc ();
3157 break;
3158 case C1_44:
3159 scr_index (UP);
3160 break;
3161
3162 /* 8.3.87: NEXT LINE */
3163 case C1_NEL: /* ESC E */
3164 {
3165 wchar_t nlcr[] = { C0_LF, C0_CR };
3166 scr_add_lines (nlcr, sizeof (nlcr) / sizeof (nlcr [0]), 1);
3167 }
3168 break;
3169
3170 /* kidnapped escape sequence: Should be 8.3.48 */
3171 case C1_ESA: /* ESC G */
3172 process_graphics ();
3173 break;
3174
3175 /* 8.3.63: CHARACTER TABULATION SET */
3176 case C1_HTS: /* ESC H */
3177 scr_set_tab (1);
3178 break;
3179
3180 /* 8.3.105: REVERSE LINE FEED */
3181 case C1_RI: /* ESC M */
3182 scr_index (DN);
3183 break;
3184
3185 /* 8.3.142: SINGLE-SHIFT TWO */
3186 /*case C1_SS2: scr_single_shift (2); break; */
3187
3188 /* 8.3.143: SINGLE-SHIFT THREE */
3189 /*case C1_SS3: scr_single_shift (3); break; */
3190
3191 /* 8.3.27: DEVICE CONTROL STRING */
3192 case C1_DCS: /* ESC P */
3193 process_dcs_seq ();
3194 break;
3195
3196 /* 8.3.110: SINGLE CHARACTER INTRODUCER */
3197 case C1_SCI: /* ESC Z */
3198 tt_write (ESCZ_ANSWER, sizeof (ESCZ_ANSWER) - 1);
3199 break; /* steal obsolete ESC [ c */
3200
3201 /* 8.3.16: CONTROL SEQUENCE INTRODUCER */
3202 case C1_CSI: /* ESC [ */
3203 process_csi_seq ();
3204 break;
3205
3206 /* 8.3.90: OPERATING SYSTEM COMMAND */
3207 case C1_OSC: /* ESC ] */
3208 process_osc_seq ();
3209 break;
3210
3211 /* 8.3.106: RESET TO INITIAL STATE */
3212 case 'c':
3213 mbstate.reset ();
3214 scr_poweron ();
3215 scrollbar_show (1);
3216 break;
3217
3218 /* 8.3.79: LOCKING-SHIFT TWO (see ISO2022) */
3219 case 'n':
3220 scr_charset_choose (2);
3221 break;
3222
3223 /* 8.3.81: LOCKING-SHIFT THREE (see ISO2022) */
3224 case 'o':
3225 scr_charset_choose (3);
3226 break;
3227 }
3228 }
3229 /*}}} */
3230
3231 /*{{{ process CONTROL SEQUENCE INTRODUCER (CSI) sequences `ESC[' */
3232 /* *INDENT-OFF* */
3233 enum {
3234 CSI_ICH = 0x40,
3235 CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
3236 CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
3237 CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
3238 CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
3239 CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
3240 CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
3241 CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
3242 CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
3243 };
3244
3245 #define make_byte(b7,b6,b5,b4,b3,b2,b1,b0) \
3246 (((b7) << 7) | ((b6) << 6) | ((b5) << 5) | ((b4) << 4) \
3247 | ((b3) << 3) | ((b2) << 2) | ((b1) << 1) | (b0))
3248 #define get_byte_array_bit(array, bit) \
3249 (!! ((array)[ (bit) / 8] & (128 >> ((bit) & 7))))
3250
3251 const unsigned char csi_defaults[] =
3252 {
3253 make_byte (1,1,1,1,1,1,1,1), /* @, A, B, C, D, E, F, G, */
3254 make_byte (1,1,0,0,1,1,0,0), /* H, I, J, K, L, M, N, O, */
3255 make_byte (1,0,1,1,1,1,1,0), /* P, Q, R, S, T, U, V, W, */
3256 make_byte (1,1,1,0,0,0,1,0), /* X, Y, Z, [, \, ], ^, _, */
3257 make_byte (1,1,1,0,1,1,1,0), /* `, a, b, c, d, e, f, g, */
3258 make_byte (0,0,1,1,0,0,0,0), /* h, i, j, k, l, m, n, o, */
3259 make_byte (0,0,0,0,0,0,0,0), /* p, q, r, s, t, u, v, w, */
3260 make_byte (0,0,0,0,0,0,0,0), /* x, y, z, {, |, }, ~, */
3261 };
3262 /* *INDENT-ON* */
3263
3264 void
3265 rxvt_term::process_csi_seq ()
3266 {
3267 unicode_t ch, priv, i;
3268 unsigned int nargs, p;
3269 int n, ndef;
3270 int arg[ESC_ARGS];
3271
3272 for (nargs = ESC_ARGS; nargs > 0;)
3273 arg[--nargs] = 0;
3274
3275 priv = 0;
3276 ch = cmd_getc ();
3277 if (ch >= '<' && ch <= '?')
3278 { /* '<' '=' '>' '?' */
3279 priv = ch;
3280 ch = cmd_getc ();
3281 }
3282
3283 /* read any numerical arguments */
3284 for (n = -1; ch < CSI_ICH; )
3285 {
3286 if (isdigit (ch))
3287 {
3288 if (n < 0)
3289 n = ch - '0';
3290 else
3291 n = n * 10 + ch - '0';
3292 }
3293 else if (ch == ';')
3294 {
3295 if (nargs < ESC_ARGS)
3296 arg[nargs++] = n;
3297 n = -1;
3298 }
3299 else if (IS_CONTROL (ch))
3300 process_nonprinting (ch);
3301
3302 ch = cmd_getc ();
3303 }
3304
3305 if (ch > CSI_7F)
3306 return;
3307
3308 if (nargs < ESC_ARGS)
3309 arg[nargs++] = n;
3310
3311 i = ch - CSI_ICH;
3312 ndef = get_byte_array_bit (csi_defaults, i);
3313 for (p = 0; p < nargs; p++)
3314 if (arg[p] == -1)
3315 arg[p] = ndef;
3316
3317 #ifdef DEBUG_CMD
3318 fprintf (stderr, "CSI ");
3319 for (p = 0; p < nargs; p++)
3320 fprintf (stderr, "%d%s", arg[p], p < nargs - 1 ? ";" : "");
3321 fprintf (stderr, "%c\n", ch);
3322 #endif
3323
3324 /*
3325 * private mode handling
3326 */
3327 if (priv)
3328 {
3329 switch (priv)
3330 {
3331 case '>':
3332 if (ch == CSI_DA) /* secondary device attributes */
3333 tt_printf ("\033[>%d;%c%c;0c", 'U', VERSION[0], VERSION[2]);
3334 break;
3335 case '?':
3336 if (ch == 'h' || ch == 'l' || ch == 'r' || ch == 's' || ch == 't')
3337 process_terminal_mode (ch, priv, nargs, arg);
3338 break;
3339 }
3340 return;
3341 }
3342
3343 switch (ch)
3344 {
3345 /*
3346 * ISO/IEC 6429:1992 (E) CSI sequences (defaults in parentheses)
3347 */
3348 #ifdef PRINTPIPE
3349 case CSI_MC: /* 8.3.83: (0) MEDIA COPY */
3350 switch (arg[0])
3351 {
3352 case 0: /* initiate transfer to primary aux device */
3353 scr_printscreen (0);
3354 break;
3355 case 5: /* start relay to primary aux device */
3356 process_print_pipe ();
3357 break;
3358 }
3359 break;
3360 #endif
3361
3362 case CSI_CUU: /* 8.3.22: (1) CURSOR UP */
3363 case CSI_VPR: /* 8.3.161: (1) LINE POSITION FORWARD */
3364 arg[0] = -arg[0];
3365 /* FALLTHROUGH */
3366 case CSI_CUD: /* 8.3.19: (1) CURSOR DOWN */
3367 case CSI_VPB: /* 8.3.160: (1) LINE POSITION BACKWARD */
3368 scr_gotorc (arg[0], 0, RELATIVE);
3369 break;
3370
3371 case CSI_CUB: /* 8.3.18: (1) CURSOR LEFT */
3372 case CSI_HPB: /* 8.3.59: (1) CHARACTER POSITION BACKWARD */
3373 #ifdef ISO6429
3374 arg[0] = -arg[0];
3375 #else /* emulate common DEC VTs */
3376 arg[0] = arg[0] ? -arg[0] : -1;
3377 #endif
3378 /* FALLTHROUGH */
3379 case CSI_CUF: /* 8.3.20: (1) CURSOR RIGHT */
3380 case CSI_HPR: /* 8.3.60: (1) CHARACTER POSITION FORWARD */
3381 #ifdef ISO6429
3382 scr_gotorc (0, arg[0], RELATIVE);
3383 #else /* emulate common DEC VTs */
3384 scr_gotorc (0, arg[0] ? arg[0] : 1, RELATIVE);
3385 #endif
3386 break;
3387
3388 case CSI_CPL: /* 8.3.13: (1) CURSOR PRECEDING LINE */
3389 arg[0] = -arg[0];
3390 /* FALLTHROUGH */
3391 case CSI_CNL: /* 8.3.12: (1) CURSOR NEXT LINE */
3392 scr_gotorc (arg[0], 0, R_RELATIVE);
3393 break;
3394
3395 case CSI_CHA: /* 8.3.9: (1) CURSOR CHARACTER ABSOLUTE */
3396 case CSI_HPA: /* 8.3.58: (1) CURSOR POSITION ABSOLUTE */
3397 scr_gotorc (0, arg[0] - 1, R_RELATIVE);
3398 break;
3399
3400 case CSI_VPA: /* 8.3.159: (1) LINE POSITION ABSOLUTE */
3401 scr_gotorc (arg[0] - 1, 0, C_RELATIVE);
3402 break;
3403
3404 case CSI_CUP: /* 8.3.21: (1,1) CURSOR POSITION */
3405 case CSI_HVP: /* 8.3.64: (1,1) CHARACTER AND LINE POSITION */
3406 scr_gotorc (arg[0] - 1, nargs < 2 ? 0 : (arg[1] - 1), 0);
3407 break;
3408
3409 case CSI_CBT: /* 8.3.7: (1) CURSOR BACKWARD TABULATION */
3410 arg[0] = -arg[0];
3411 /* FALLTHROUGH */
3412 case CSI_CHT: /* 8.3.10: (1) CURSOR FORWARD TABULATION */
3413 scr_tab (arg[0]);
3414 break;
3415
3416 case CSI_ED: /* 8.3.40: (0) ERASE IN PAGE */
3417 scr_erase_screen (arg[0]);
3418 break;
3419
3420 case CSI_EL: /* 8.3.42: (0) ERASE IN LINE */
3421 scr_erase_line (arg[0]);
3422 break;
3423
3424 case CSI_ICH: /* 8.3.65: (1) INSERT CHARACTER */
3425 scr_insdel_chars (arg[0], INSERT);
3426 break;
3427
3428 case CSI_IL: /* 8.3.68: (1) INSERT LINE */
3429 scr_insdel_lines (arg[0], INSERT);
3430 break;
3431
3432 case CSI_DL: /* 8.3.33: (1) DELETE LINE */
3433 scr_insdel_lines (arg[0], DELETE);
3434 break;
3435
3436 case CSI_ECH: /* 8.3.39: (1) ERASE CHARACTER */
3437 scr_insdel_chars (arg[0], ERASE);
3438 break;
3439
3440 case CSI_DCH: /* 8.3.26: (1) DELETE CHARACTER */
3441 scr_insdel_chars (arg[0], DELETE);
3442 break;
3443
3444 case CSI_SD: /* 8.3.114: (1) SCROLL DOWN */
3445 arg[0] = -arg[0];
3446 /* FALLTHROUGH */
3447 case CSI_SU: /* 8.3.148: (1) SCROLL UP */
3448 scr_scroll_text (screen.tscroll, screen.bscroll, arg[0]);
3449 break;
3450
3451 case CSI_DA: /* 8.3.24: (0) DEVICE ATTRIBUTES */
3452 tt_write (VT100_ANS, sizeof (VT100_ANS) - 1);
3453 break;
3454
3455 case CSI_SGR: /* 8.3.118: (0) SELECT GRAPHIC RENDITION */
3456 process_sgr_mode (nargs, arg);
3457 break;
3458
3459 case CSI_DSR: /* 8.3.36: (0) DEVICE STATUS REPORT */
3460 switch (arg[0])
3461 {
3462 case 5: /* DSR requested */
3463 tt_printf ("\033[0n");
3464 break;
3465 case 6: /* CPR requested */
3466 scr_report_position ();
3467 break;
3468 case 7: /* unofficial extension */
3469 if (OPTION (Opt_insecure))
3470 tt_printf ("%-.250s\012", rs[Rs_display_name]);
3471 break;
3472 case 8: /* unofficial extension */
3473 process_xterm_seq (XTerm_title, RESNAME "-" VERSION, CHAR_ST);
3474 break;
3475 }
3476 break;
3477
3478 case CSI_TBC: /* 8.3.155: (0) TABULATION CLEAR */
3479 switch (arg[0])
3480 {
3481 case 0: /* char tab stop cleared at active position */
3482 scr_set_tab (0);
3483 break;
3484 /* case 1: */ /* line tab stop cleared in active line */
3485 /* case 2: */ /* char tab stops cleared in active line */
3486 case 3: /* all char tab stops are cleared */
3487 /* case 4: */ /* all line tab stops are cleared */
3488 case 5: /* all tab stops are cleared */
3489 scr_set_tab (-1);
3490 break;
3491 }
3492 break;
3493
3494 case CSI_CTC: /* 8.3.17: (0) CURSOR TABULATION CONTROL */
3495 switch (arg[0])
3496 {
3497 case 0: /* char tab stop set at active position */
3498 scr_set_tab (1);
3499 break; /* = ESC H */
3500 /* case 1: */ /* line tab stop set at active line */
3501 case 2: /* char tab stop cleared at active position */
3502 scr_set_tab (0);
3503 break; /* = ESC [ 0 g */
3504 /* case 3: */ /* line tab stop cleared at active line */
3505 /* case 4: */ /* char tab stops cleared at active line */
3506 case 5: /* all char tab stops are cleared */
3507 scr_set_tab (-1);
3508 break; /* = ESC [ 3 g */
3509 /* case 6: */ /* all line tab stops are cleared */
3510 }
3511 break;
3512
3513 case CSI_RM: /* 8.3.107: RESET MODE */
3514 if (arg[0] == 4)
3515 scr_insert_mode (0);
3516 else if (arg[0] == 20)
3517 priv_modes &= ~PrivMode_LFNL;
3518 break;
3519
3520 case CSI_SM: /* 8.3.126: SET MODE */
3521 if (arg[0] == 4)
3522 scr_insert_mode (1);
3523 else if (arg[0] == 20)
3524 priv_modes |= PrivMode_LFNL;
3525 break;
3526
3527 /*
3528 * PRIVATE USE beyond this point. All CSI_7? sequences here
3529 */
3530 case CSI_72: /* DECSTBM: set top and bottom margins */
3531 if (nargs == 1)
3532 scr_scroll_region (arg[0] - 1, MAX_ROWS - 1);
3533 else if (nargs == 0 || arg[0] >= arg[1])
3534 scr_scroll_region (0, MAX_ROWS - 1);
3535 else
3536 scr_scroll_region (arg[0] - 1, arg[1] - 1);
3537 break;
3538
3539 case CSI_73:
3540 scr_cursor (SAVE);
3541 break;
3542 case CSI_75:
3543 scr_cursor (RESTORE);
3544 break;
3545
3546 #if ENABLE_FRILLS
3547 case CSI_74:
3548 process_window_ops (arg, nargs);
3549 break;
3550 #endif
3551
3552 case CSI_78: /* DECREQTPARM */
3553 if (arg[0] == 0 || arg[0] == 1)
3554 tt_printf ("\033[%d;1;1;128;128;1;0x", arg[0] + 2);
3555 break;
3556
3557 default:
3558 break;
3559 }
3560 }
3561 /*}}} */
3562
3563 #if ENABLE_FRILLS
3564 /* ARGSUSED */
3565 void
3566 rxvt_term::process_window_ops (const int *args, unsigned int nargs)
3567 {
3568 int x, y;
3569 XWindowAttributes wattr;
3570 Window wdummy;
3571 dDisp;
3572
3573 if (nargs == 0)
3574 return;
3575
3576 switch (args[0])
3577 {
3578 /*
3579 * commands
3580 */
3581 case 1: /* deiconify window */
3582 XMapWindow (disp, parent[0]);
3583 break;
3584 case 2: /* iconify window */
3585 XIconifyWindow (disp, parent[0], display->screen);
3586 break;
3587 case 3: /* set position (pixels) */
3588 XMoveWindow (disp, parent[0], args[1], args[2]);
3589 break;
3590 case 4: /* set size (pixels) */
3591 set_widthheight ((unsigned int)args[2], (unsigned int)args[1]);
3592 break;
3593 case 5: /* raise window */
3594 XRaiseWindow (disp, parent[0]);
3595 break;
3596 case 6: /* lower window */
3597 XLowerWindow (disp, parent[0]);
3598 break;
3599 case 7: /* refresh window */
3600 scr_touch (true);
3601 break;
3602 case 8: /* set size (chars) */
3603 set_widthheight ((unsigned int) (args[2] * fwidth),
3604 (unsigned int) (args[1] * fheight));
3605 break;
3606
3607 //case 9: NYI, TODO, restore maximized window or maximize window
3608 default:
3609 if (args[0] >= 24) /* set height (chars) */
3610 set_widthheight ((unsigned int)width,
3611 (unsigned int) (args[1] * fheight));
3612 break;
3613
3614
3615 /*
3616 * reports - some output format copied from XTerm
3617 */
3618 case 11: /* report window state */
3619 XGetWindowAttributes (disp, parent[0], &wattr);
3620 tt_printf ("\033[%dt", wattr.map_state == IsViewable ? 1 : 2);
3621 break;
3622 case 13: /* report window position */
3623 XGetWindowAttributes (disp, parent[0], &wattr);
3624 XTranslateCoordinates (disp, parent[0], wattr.root,
3625 -wattr.border_width, -wattr.border_width,
3626 &x, &y, &wdummy);
3627 tt_printf ("\033[3;%d;%dt", x, y);
3628 break;
3629 case 14: /* report window size (pixels) */
3630 XGetWindowAttributes (disp, parent[0], &wattr);
3631 tt_printf ("\033[4;%d;%dt", wattr.height, wattr.width);
3632 break;
3633 case 18: /* report text area size (chars) */
3634 tt_printf ("\033[8;%d;%dt", nrow, ncol);
3635 break;
3636 case 19: /* report window size (chars) */
3637 tt_printf ("\033[9;%d;%dt", nrow, ncol);
3638 break;
3639 case 20: /* report icon label */
3640 {
3641 char *s;
3642 XGetIconName (disp, parent[0], &s);
3643 tt_printf ("\033]L%-.250s\234", OPTION (Opt_insecure) && s ? s : ""); /* 8bit ST */
3644 XFree (s);
3645 }
3646 break;
3647 case 21: /* report window title */
3648 {
3649 char *s;
3650 XFetchName (disp, parent[0], &s);
3651 tt_printf ("\033]l%-.250s\234", OPTION (Opt_insecure) && s ? s : ""); /* 8bit ST */
3652 XFree (s);
3653 }
3654 break;
3655 }
3656 }
3657 #endif
3658
3659 /*----------------------------------------------------------------------*/
3660 /*
3661 * get input up until STRING TERMINATOR (or BEL)
3662 * ends_how is terminator used. returned input must be free()'d
3663 */
3664 char *
3665 rxvt_term::get_to_st (unicode_t &ends_how)
3666 {
3667 unicode_t ch;
3668 bool seen_esc = false;
3669 unsigned int n = 0;
3670 wchar_t string[STRING_MAX];
3671
3672 while ((ch = cmd_getc ()) != NOCHAR)
3673 {
3674 if (seen_esc)
3675 {
3676 if (ch == 0x5c) /* 7bit ST */
3677 break;
3678 else
3679 return NULL;
3680 }
3681 else if (ch == C0_ESC)
3682 {
3683 seen_esc = true;
3684 continue;
3685 }
3686 else if (ch == C0_BEL || ch == CHAR_ST)
3687 break;
3688 else if (ch < 0x20)
3689 return NULL; /* other control character - exit */
3690
3691 seen_esc = false;
3692
3693 if (n >= STRING_MAX - 1)
3694 // stop at some sane length
3695 return NULL;
3696
3697 if (ch == C0_SYN)
3698 string[n++] = cmd_get8 ();
3699 else
3700 string[n++] = ch;
3701 }
3702
3703 string[n++] = '\0';
3704
3705 ends_how = (ch == 0x5c ? C0_ESC : ch);
3706
3707 return rxvt_wcstombs (string);
3708 }
3709
3710 /*----------------------------------------------------------------------*/
3711 /*
3712 * process DEVICE CONTROL STRING `ESC P ... (ST|BEL)' or `0x90 ... (ST|BEL)'
3713 */
3714 void
3715 rxvt_term::process_dcs_seq ()
3716 {
3717 char *s;
3718 unicode_t eh;
3719
3720 /*
3721 * Not handled yet
3722 */
3723 s = get_to_st (eh);
3724 if (s)
3725 free (s);
3726
3727 return;
3728 }
3729
3730 /*----------------------------------------------------------------------*/
3731 /*
3732 * process OPERATING SYSTEM COMMAND sequence `ESC ] Ps ; Pt (ST|BEL)'
3733 */
3734 void
3735 rxvt_term::process_osc_seq ()
3736 {
3737 unicode_t ch, eh;
3738 int arg;
3739
3740 ch = cmd_getc ();
3741 for (arg = 0; isdigit (ch); ch = cmd_getc ())
3742 arg = arg * 10 + (ch - '0');
3743
3744 if (ch == ';')
3745 {
3746 char *s = get_to_st (eh);
3747
3748 if (s)
3749 {
3750 process_xterm_seq (arg, s, eh);
3751 free (s);
3752 }
3753 }
3754 }
3755
3756 void
3757 rxvt_term::process_color_seq (int report, int color, const char *str, char resp)
3758 {
3759 if (str[0] == '?' && !str[1])
3760 {
3761 unsigned short r, g, b;
3762 pix_colors_focused[color].get (display, r, g, b);
3763 tt_printf ("\033]%d;rgb:%04x/%04x/%04x%c", report, r, g, b, resp);
3764 }
3765 else
3766 set_window_color (color, str);
3767 }
3768
3769 /*
3770 * XTerm escape sequences: ESC ] Ps;Pt (ST|BEL)
3771 */
3772 void
3773 rxvt_term::process_xterm_seq (int op, const char *str, char resp)
3774 {
3775 int changed = 0;
3776 int color;
3777 char *buf, *name;
3778 bool query = str[0] == '?' && !str[1];
3779 int saveop = op;
3780 dDisp;
3781
3782 assert (str != NULL);
3783 switch (op)
3784 {
3785 case XTerm_name:
3786 set_title (str);
3787 /* FALLTHROUGH */
3788 case XTerm_iconName:
3789 set_icon_name (str);
3790 break;
3791 case XTerm_title:
3792 set_title (str);
3793 break;
3794 case XTerm_property:
3795 if (str[0] == '?')
3796 {
3797 Atom prop = display->atom (str + 1);
3798 Atom actual_type;
3799 int actual_format;
3800 unsigned long nitems;
3801 unsigned long bytes_after;
3802 unsigned char *value = 0;
3803 const char *str = "";
3804
3805 if (prop
3806 && XGetWindowProperty (disp, parent[0],
3807 prop, 0, 1<<16, 0, AnyPropertyType,
3808 &actual_type, &actual_format,
3809 &nitems, &bytes_after, &value) == Success
3810 && actual_type != None
3811 && actual_format == 8)
3812 str = (const char *)(value);
3813
3814 tt_printf ("\033]%d;%s%c", XTerm_property, str, resp);
3815
3816 XFree (value);
3817 }
3818 else
3819 {
3820 char *eq = strchr (str, '='); // constness lost, but verified to be ok
3821
3822 if (eq)
3823 {
3824 *eq = 0;
3825 set_utf8_property (display->atom (str), eq + 1);
3826 }
3827 else
3828 XDeleteProperty (disp, parent[0],
3829 display->atom (str));
3830 }
3831 break;
3832
3833 case XTerm_Color:
3834 for (buf = (char *)str; buf && *buf;)
3835 {
3836 if ((name = strchr (buf, ';')) == NULL)
3837 break;
3838
3839 *name++ = '\0';
3840 color = atoi (buf) + minCOLOR;
3841
3842 if (!IN_RANGE_INC (color, minCOLOR, maxTermCOLOR))
3843 break;
3844
3845 if ((buf = strchr (name, ';')) != NULL)
3846 *buf++ = '\0';
3847
3848 if (name[0] == '?' && !name[1])
3849 {
3850 unsigned short r, g, b;
3851 pix_colors_focused[color].get (display, r, g, b);
3852 tt_printf ("\033]%d;%d;rgb:%04x/%04x/%04x%c", XTerm_Color, color, r, g, b, resp);
3853 }
3854 else
3855 set_window_color (color, name);
3856 }
3857 break;
3858 case XTerm_Color00:
3859 process_color_seq (XTerm_Color00, Color_fg, str, resp);
3860 break;
3861 case XTerm_Color01:
3862 process_color_seq (XTerm_Color01, Color_bg, str, resp);
3863 break;
3864 #ifndef NO_CURSORCOLOR
3865 case XTerm_Color_cursor:
3866 process_color_seq (XTerm_Color_cursor, Color_cursor, str, resp);
3867 break;
3868 #endif
3869 case XTerm_Color_pointer_fg:
3870 process_color_seq (XTerm_Color_pointer_fg, Color_pointer_fg, str, resp);
3871 break;
3872 case XTerm_Color_pointer_bg:
3873 process_color_seq (XTerm_Color_pointer_bg, Color_pointer_bg, str, resp);
3874 break;
3875 #ifndef NO_BOLD_UNDERLINE_REVERSE
3876 case XTerm_Color_RV:
3877 process_color_seq (XTerm_Color_RV, Color_RV, str, resp);
3878 break;
3879 case Rxvt_Color_BD:
3880 case URxvt_Color_BD:
3881 process_color_seq (op, Color_BD, str, resp);
3882 break;
3883 case Rxvt_Color_UL:
3884 case URxvt_Color_UL:
3885 process_color_seq (op, Color_UL, str, resp);
3886 break;
3887 case URxvt_Color_IT:
3888 process_color_seq (URxvt_Color_IT, Color_IT, str, resp);
3889 break;
3890 #endif
3891 #if TRANSPARENT && TINTING
3892 case URxvt_Color_tint:
3893 process_color_seq (URxvt_Color_tint, Color_tint, str, resp);
3894 check_our_parents ();
3895 if (am_transparent)
3896 want_full_refresh = want_refresh = 1;
3897 break;
3898 #endif
3899
3900 case Rxvt_Pixmap:
3901 if (*str != ';')
3902 {
3903 #if XPM_BACKGROUND
3904 scale_pixmap (""); /* reset to default scaling */
3905 set_bgPixmap (str); /* change pixmap */
3906 scr_touch (true);
3907 #endif
3908 }
3909
3910 while ((str = strchr (str, ';')) != NULL)
3911 {
3912 str++;
3913 #if XPM_BACKGROUND
3914 changed += scale_pixmap (str);
3915 #endif
3916 }
3917
3918 if (changed)
3919 {
3920 #ifdef XPM_BACKGROUND
3921 resize_pixmap ();
3922 scr_touch (true);
3923 #endif
3924 }
3925 break;
3926
3927 case Rxvt_restoreFG:
3928 set_window_color (Color_fg, str);
3929 break;
3930 case Rxvt_restoreBG:
3931 set_window_color (Color_bg, str);
3932 break;
3933
3934 case XTerm_logfile:
3935 // TODO, when secure mode?
3936 break;
3937
3938 #if 0
3939 case Rxvt_dumpscreen: /* no error notices */
3940 {
3941 int fd;
3942 if ((fd = open (str, O_RDWR | O_CREAT | O_EXCL, 0600)) >= 0)
3943 {
3944 scr_dump (fd);
3945 close (fd);
3946 }
3947 }
3948 break;
3949 #endif
3950 case XTerm_font:
3951 op = URxvt_font;
3952 case URxvt_font:
3953 #if ENABLE_STYLES
3954 case URxvt_boldFont:
3955 case URxvt_italicFont:
3956 case URxvt_boldItalicFont:
3957 #endif
3958 if (query)
3959 tt_printf ("\33]%d;%-.250s%c", saveop,
3960 OPTION (Opt_insecure) && fontset[op - URxvt_font]->fontdesc
3961 ? fontset[op - URxvt_font]->fontdesc : "",
3962 resp);
3963 else
3964 {
3965 const char *&res = rs[Rs_font + (op - URxvt_font)];
3966
3967 res = strdup (str);
3968 allocated.push_back ((void *)res);
3969 set_fonts ();
3970 }
3971 break;
3972
3973 #if ENABLE_FRILLS
3974 case URxvt_locale:
3975 if (query)
3976 tt_printf ("\33]%d;%-.250s%c", URxvt_locale, OPTION (Opt_insecure) ? locale : "", resp);
3977 else
3978 {
3979 set_locale (str);
3980 pty.set_utf8_mode (enc_utf8);
3981 init_xlocale ();
3982 }
3983 break;
3984
3985 case URxvt_view_up:
3986 case URxvt_view_down:
3987 {
3988 int lines = atoi (str);
3989
3990 if (lines)
3991 scr_page (op == URxvt_view_up ? UP : DN, lines);
3992 else
3993 scr_erase_savelines ();
3994 }
3995
3996 break;
3997 #endif
3998
3999 #if ENABLE_PERL
4000 case URxvt_perl:
4001 if (HOOK_INVOKE ((this, HOOK_OSC_SEQ, DT_STR, str, DT_END)))
4002 ; // no responses yet
4003 break;
4004 #endif
4005 }
4006 }
4007 /*----------------------------------------------------------------------*/
4008
4009 /*{{{ process DEC private mode sequences `ESC [ ? Ps mode' */
4010 /*
4011 * mode can only have the following values:
4012 * 'l' = low
4013 * 'h' = high
4014 * 's' = save
4015 * 'r' = restore
4016 * 't' = toggle
4017 * so no need for fancy checking
4018 */
4019 int
4020 rxvt_term::privcases (int mode, unsigned long bit)
4021 {
4022 int state;
4023
4024 if (mode == 's')
4025 {
4026 SavedModes |= (priv_modes & bit);
4027 return -1;
4028 }
4029 else
4030 {
4031 if (mode == 'r')
4032 state = (SavedModes & bit) ? 1 : 0; /* no overlapping */
4033 else
4034 state = (mode == 't') ? ! (priv_modes & bit) : mode;
4035 PrivMode (state, bit);
4036 }
4037
4038 return state;
4039 }
4040
4041 /* we're not using priv _yet_ */
4042 void
4043 rxvt_term::process_terminal_mode (int mode, int priv __attribute__ ((unused)), unsigned int nargs, const int *arg)
4044 {
4045 unsigned int i, j;
4046 int state;
4047
4048 static const struct
4049 {
4050 const int argval;
4051 const unsigned long bit;
4052 }
4053
4054 argtopriv[] = {
4055 { 1, PrivMode_aplCUR },
4056 { 2, PrivMode_vt52 },
4057 { 3, PrivMode_132 },
4058 { 4, PrivMode_smoothScroll },
4059 { 5, PrivMode_rVideo },
4060 { 6, PrivMode_relOrigin },
4061 { 7, PrivMode_Autowrap },
4062 // 8, bi-directional support mode
4063 { 9, PrivMode_MouseX10 },
4064 // 18, 19 printing-related
4065 { 25, PrivMode_VisibleCursor },
4066 #ifdef scrollBar_esc
4067 { scrollBar_esc, PrivMode_scrollBar },
4068 #endif
4069 { 35, PrivMode_ShiftKeys }, // rxvt extension
4070 { 40, PrivMode_132OK },
4071 // 41 xterm more fixes NYI
4072 // 45 margin bell NYI
4073 // 46 start logging
4074 { 47, PrivMode_Screen },
4075 { 66, PrivMode_aplKP },
4076 #ifndef NO_BACKSPACE_KEY
4077 { 67, PrivMode_BackSpace },
4078 #endif
4079 { 1000, PrivMode_MouseX11 },
4080 // 1001 Use Hilite Mouse Tracking. NYI, TODO
4081 // 1002 Use Cell Motion Mouse Tracking. NYI, TODO
4082 // 1003 Use All Motion Mouse Tracking. NYI, TODO
4083 { 1010, PrivMode_TtyOutputInh }, // rxvt extension
4084 { 1011, PrivMode_Keypress }, // rxvt extension
4085 // 1035 enable modifiers for alt, numlock NYI
4086 // 1036 send ESC for meta keys NYI
4087 // 1037 send DEL for keypad delete NYI
4088 { 1047, PrivMode_Screen },
4089 // 1048 save and restore cursor
4090 { 1049, PrivMode_Screen }, /* xterm extension, clear screen on ti rather than te */
4091 // 1051, 1052, 1060, 1061 keyboard emulation NYI
4092 };
4093
4094 if (nargs == 0)
4095 return;
4096
4097 /* make lo/hi boolean */
4098 if (mode == 'l')
4099 mode = 0; /* reset */
4100 else if (mode == 'h')
4101 mode = 1; /* set */
4102
4103 for (i = 0; i < nargs; i++)
4104 {
4105 state = -1;
4106
4107 /* basic handling */
4108 for (j = 0; j < (sizeof (argtopriv)/sizeof (argtopriv[0])); j++)
4109 if (argtopriv[j].argval == arg[i])
4110 {
4111 state = privcases (mode, argtopriv[j].bit);
4112 break;
4113 }
4114
4115 /* extra handling for values with state unkept */
4116 switch (arg[i])
4117 {
4118 #if ENABLE_STYLES
4119 case 1021:
4120 set_option (Opt_intensityStyles, mode);
4121
4122 scr_touch (true);
4123 break;
4124 #endif
4125 case 1048: /* alternative cursor save */
4126 case 1049:
4127 if (OPTION (Opt_secondaryScreen))
4128 if (mode == 0)
4129 scr_cursor (RESTORE);
4130 else if (mode == 1)
4131 scr_cursor (SAVE);
4132 break;
4133 }
4134
4135 if (state >= 0)
4136 /* extra handling for values with valid 0 or 1 state */
4137 switch (arg[i])
4138 {
4139 /* case 1: - application cursor keys */
4140 case 2: /* VT52 mode */
4141 /* oddball mode. should be set regardless of set/reset
4142 * parameter. Return from VT52 mode with an ESC < from
4143 * within VT52 mode
4144 */
4145 PrivMode (1, PrivMode_vt52);
4146 break;
4147 case 3: /* 80/132 */
4148 if (priv_modes & PrivMode_132OK)
4149 set_widthheight (((state ? 132 : 80) * fwidth), height);
4150 break;
4151 case 4: /* smooth scrolling */
4152 set_option (Opt_jumpScroll, !state);
4153 break;
4154 case 5: /* reverse video */
4155 scr_rvideo_mode (state);
4156 break;
4157 case 6: /* relative/absolute origins */
4158 scr_relative_origin (state);
4159 break;
4160 case 7: /* autowrap */
4161 scr_autowrap (state);
4162 break;
4163 /* case 8: - auto repeat, can't do on a per window basis */
4164 case 9: /* X10 mouse reporting */
4165 if (state) /* orthogonal */
4166 priv_modes &= ~PrivMode_MouseX11;
4167 break;
4168 #ifdef scrollBar_esc
4169 case scrollBar_esc:
4170 if (scrollbar_mapping (state))
4171 {
4172 resize_all_windows (0, 0, 0);
4173 scr_touch (true);
4174 }
4175 break;
4176 #endif
4177 case 25: /* visible/invisible cursor */
4178 scr_cursor_visible (state);
4179 break;
4180 /* case 35: - shift keys */
4181 /* case 40: - 80 <--> 132 mode */
4182 case 47: /* secondary screen */
4183 scr_change_screen (state);
4184 break;
4185 /* case 66: - application key pad */
4186 /* case 67: - backspace key */
4187 case 1000: /* X11 mouse reporting */
4188 if (state) /* orthogonal */
4189 priv_modes &= ~PrivMode_MouseX10;
4190 break;
4191 #if 0
4192 case 1001:
4193 break; /* X11 mouse highlighting */
4194 #endif
4195 case 1010: /* scroll to bottom on TTY output inhibit */
4196 set_option (Opt_scrollTtyOutput, !state);
4197 break;
4198 case 1011: /* scroll to bottom on key press */
4199 set_option (Opt_scrollTtyKeypress, state);
4200 break;
4201 case 1047: /* secondary screen w/ clearing last */
4202 if (OPTION (Opt_secondaryScreen))
4203 if (current_screen != PRIMARY)
4204 scr_erase_screen (2);
4205 scr_change_screen (state);
4206 break;
4207 case 1049: /* secondary screen w/ clearing first */
4208 scr_change_screen (state);
4209 if (OPTION (Opt_secondaryScreen))
4210 if (current_screen != PRIMARY)
4211 scr_erase_screen (2);
4212 break;
4213 default:
4214 break;
4215 }
4216 }
4217 }
4218 /*}}} */
4219
4220 /*{{{ process sgr sequences */
4221 void
4222 rxvt_term::process_sgr_mode (unsigned int nargs, const int *arg)
4223 {
4224 unsigned int i;
4225 short rendset;
4226 int rendstyle;
4227
4228 if (nargs == 0)
4229 {
4230 scr_rendition (0, ~RS_None);
4231 return;
4232 }
4233
4234 for (i = 0; i < nargs; i++)
4235 {
4236 rendset = -1;
4237 switch (arg[i])
4238 {
4239 case 0:
4240 rendset = 0, rendstyle = ~RS_None;
4241 break;
4242 case 1:
4243 rendset = 1, rendstyle = RS_Bold;
4244 break;
4245 //case 2: // low intensity
4246 case 3:
4247 rendset = 1, rendstyle = RS_Italic;
4248 break;
4249 case 4:
4250 rendset = 1, rendstyle = RS_Uline;
4251 break;
4252 case 5: // slowly blinking
4253 case 6: // rapidly blinking
4254 rendset = 1, rendstyle = RS_Blink;
4255 break;
4256 //case 6: // scoansi light background
4257 case 7:
4258 rendset = 1, rendstyle = RS_RVid;
4259 break;
4260 case 8:
4261 // invisible. NYI
4262 break;
4263 //case 9: // crossed out
4264 //case 10: // scoansi acs off, primary font
4265 //case 11: // scoansi acs on, first alt font
4266 //case 12: // scoansi acs on, |0x80, second alt font
4267 //...
4268 //case 19: // ninth alt font
4269 //case 20: // gothic
4270 case 21: // disable bold, faint, sometimes doubly underlined (iso 8613)
4271 rendset = 0, rendstyle = RS_Bold;
4272 break;
4273 case 22: // normal intensity
4274 rendset = 0, rendstyle = RS_Bold;
4275 break;
4276 case 23: // disable italic
4277 rendset = 0, rendstyle = RS_Italic;
4278 break;
4279 case 24:
4280 rendset = 0, rendstyle = RS_Uline;
4281 break;
4282 case 25:
4283 rendset = 0, rendstyle = RS_Blink;
4284 break;
4285 case 26: // variable spacing (iso 8613)
4286 rendset = 0, rendstyle = RS_Blink;
4287 break;
4288 case 27:
4289 rendset = 0, rendstyle = RS_RVid;
4290 break;
4291 //case 28: // visible. NYI
4292 //case 29: // not crossed-out
4293 }
4294
4295 if (rendset != -1)
4296 {
4297 scr_rendition (rendset, rendstyle);
4298 continue; /* for (;i;) */
4299 }
4300
4301 switch (arg[i])
4302 {
4303 case 30:
4304 case 31: /* set fg color */
4305 case 32:
4306 case 33:
4307 case 34:
4308 case 35:
4309 case 36:
4310 case 37:
4311 scr_color ((unsigned int) (minCOLOR + (arg[i] - 30)), Color_fg);
4312 break;
4313 case 38: // set fg color, ISO 8613-6
4314 if (nargs > i + 2 && arg[i + 1] == 5)
4315 {
4316 scr_color ((unsigned int) (minCOLOR + arg[i + 2]), Color_fg);
4317 i += 2;
4318 }
4319 break;
4320 case 39: /* default fg */
4321 scr_color (Color_fg, Color_fg);
4322 break;
4323
4324 case 40:
4325 case 41: /* set bg color */
4326 case 42:
4327 case 43:
4328 case 44:
4329 case 45:
4330 case 46:
4331 case 47:
4332 scr_color ((unsigned int) (minCOLOR + (arg[i] - 40)), Color_bg);
4333 break;
4334 case 48: // set bg color, ISO 8613-6
4335 if (nargs > i + 2 && arg[i + 1] == 5)
4336 {
4337 scr_color ((unsigned int) (minCOLOR + arg[i + 2]), Color_bg);
4338 i += 2;
4339 }
4340 break;
4341 case 49: /* default bg */
4342 scr_color (Color_bg, Color_bg);
4343 break;
4344
4345 //case 50: // not variable spacing
4346
4347 #if ENABLE_FRILLS
4348 case 90:
4349 case 91: /* set bright fg color */
4350 case 92:
4351 case 93:
4352 case 94:
4353 case 95:
4354 case 96:
4355 case 97:
4356 scr_color ((unsigned int) (minBrightCOLOR + (arg[i] - 90)), Color_fg);
4357 break;
4358 case 100:
4359 case 101: /* set bright bg color */
4360 case 102:
4361 case 103:
4362 case 104:
4363 case 105:
4364 case 106:
4365 case 107:
4366 scr_color ((unsigned int) (minBrightCOLOR + (arg[i] - 100)), Color_bg);
4367 break;
4368 #endif
4369 }
4370 }
4371 }
4372 /*}}} */
4373
4374 /*{{{ (do not) process Rob Nation's own graphics mode sequences */
4375 void
4376 rxvt_term::process_graphics ()
4377 {
4378 unicode_t ch, cmd = cmd_getc ();
4379
4380 if (cmd == 'Q')
4381 { /* query graphics */
4382 tt_printf ("\033G0\012"); /* no graphics */
4383 return;
4384 }
4385 /* swallow other graphics sequences until terminating ':' */
4386 do
4387 ch = cmd_getc ();
4388 while (ch != ':');
4389 }
4390 /*}}} */
4391
4392 /* ------------------------------------------------------------------------- */
4393
4394 /*
4395 * Send printf () formatted output to the command.
4396 * Only use for small amounts of data.
4397 */
4398 void
4399 rxvt_term::tt_printf (const char *fmt,...)
4400 {
4401 va_list arg_ptr;
4402 char buf[256];
4403
4404 va_start (arg_ptr, fmt);
4405 vsnprintf ((char *)buf, 256, fmt, arg_ptr);
4406 va_end (arg_ptr);
4407 tt_write (buf, strlen (buf));
4408 }
4409
4410 /* ---------------------------------------------------------------------- */
4411 /* Write data to the pty as typed by the user, pasted with the mouse,
4412 * or generated by us in response to a query ESC sequence.
4413 */
4414 const unsigned int MAX_PTY_WRITE = 255; // minimum MAX_INPUT
4415
4416 void
4417 rxvt_term::tt_write (const char *data, unsigned int len)
4418 {
4419 if (HOOK_INVOKE ((this, HOOK_TT_WRITE, DT_STR_LEN, data, len, DT_END)))
4420 return;
4421
4422 if (pty.pty < 0)
4423 return;
4424
4425 if (v_buflen == 0)
4426 {
4427 ssize_t written = write (pty.pty, data, min (len, MAX_PTY_WRITE));
4428
4429 if ((unsigned int)written == len)
4430 return;
4431
4432 data += written;
4433 len -= written;
4434 }
4435
4436 v_buffer = (char *)realloc (v_buffer, v_buflen + len);
4437
4438 memcpy (v_buffer + v_buflen, data, len);
4439 v_buflen += len;
4440
4441 pty_ev.set (EVENT_READ | EVENT_WRITE);
4442 }
4443
4444 void rxvt_term::pty_write ()
4445 {
4446 int written = write (pty.pty, v_buffer, min (v_buflen, MAX_PTY_WRITE));
4447
4448 if (written > 0)
4449 {
4450 v_buflen -= written;
4451
4452 if (v_buflen == 0)
4453 {
4454 free (v_buffer);
4455 v_buffer = 0;
4456 v_buflen = 0;
4457
4458 pty_ev.set (EVENT_READ);
4459 return;
4460 }
4461
4462 memmove (v_buffer, v_buffer + written, v_buflen);
4463 }
4464 else if (written != -1 || (errno != EAGAIN && errno != EINTR))
4465 pty_ev.set (EVENT_READ);
4466 }
4467
4468 /*----------------------- end-of-file (C source) -----------------------*/
4469