Don't act on ButtonReleases when we didn't see the corresponding ButtonPress
[dana/urxvt.git] / src / rxvt.h
1 #ifndef RXVT_H_                /* include once only */
2 #define RXVT_H_
3
4 #include <cstdio>
5 #include <cctype>
6 #include <cerrno>
7 #include <cstdarg>
8 #include <cstdlib>
9 #ifdef HAVE_STDINT_H
10 #include <stdint.h>
11 #endif
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <cstring>
15 #include <assert.h>
16 #ifdef HAVE_SYS_IOCTL_H
17 #include <sys/ioctl.h>
18 #endif
19 #ifdef HAVE_SYS_STRREDIR_H
20 #include <sys/strredir.h>
21 #endif
22
23 #if HAVE_CWCHAR
24 # include <cwchar>
25 #elif HAVE_WCHAR_H
26 # include <wchar.h>
27 #else
28 // stdlib.h might provide it
29 #endif
30
31 using namespace std;
32
33 extern "C" {
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/Xresource.h>
37 }
38
39 #if UNICODE_3
40 typedef uint32_t text_t;
41 #else
42 typedef uint16_t text_t; // saves lots of memory
43 #endif
44 typedef uint32_t rend_t;
45 typedef  int32_t tlen_t;  // was int16_t, but this results in smaller code and memory use
46 typedef  int32_t tlen_t_; // specifically for use in the line_t structure
47
48 #include "feature.h"
49
50 #if defined (ISO_14755) || defined (ENABLE_PERL)
51 # define ENABLE_OVERLAY 1
52 #endif
53
54 #if ENABLE_PERL
55 # define ENABLE_FRILLS    1
56 # define ENABLE_COMBINING 1
57 #endif
58
59 #if ENABLE_FRILLS
60 # define ENABLE_XEMBED        1
61 # define ENABLE_EWMH          1
62 # define ENABLE_XIM_ONTHESPOT 1
63 # define CURSOR_BLINK         1
64 # define OPTION_HC            1
65 # define BUILTIN_GLYPHS       1
66 #else
67 # define ENABLE_MINIMAL 1
68 #endif
69
70 #include <limits.h>
71
72 #include <X11/cursorfont.h>
73 #include <X11/keysym.h>
74 #include <X11/keysymdef.h>
75 #include <X11/Xatom.h>
76
77 #include "encoding.h"
78 #include "rxvtutil.h"
79 #include "rxvtfont.h"
80 #include "rxvttoolkit.h"
81 #include "scrollbar.h"
82 #include "ev_cpp.h"
83 #include "salloc.h"
84 #include "libptytty.h"
85
86 #include "rxvtperl.h"
87
88 // try to avoid some macros to decrease code size, on some systems
89 #if ENABLE_MINIMAL
90 # define strcmp(a,b)   (strcmp)(a,b)
91 # define strlen(a)     (strlen)(a)
92 # define strcpy(a,b)   (strcpy)(a,b)
93 # define memset(a,c,l) (memset)(a,c,l)
94 # define memcpy(a,b,l) (memcpy)(a,b,l)
95 #endif
96
97 /*
98  *****************************************************************************
99  * SYSTEM HACKS
100  *****************************************************************************
101  */
102
103 #ifndef HAVE_XPOINTER
104 typedef char *XPointer;
105 #endif
106
107 #include <termios.h>
108
109 #include "background.h"
110
111 #ifndef STDIN_FILENO
112 # define STDIN_FILENO   0
113 # define STDOUT_FILENO  1
114 # define STDERR_FILENO  2
115 #endif
116
117 #ifndef EXIT_SUCCESS            /* missing from <stdlib.h> */
118 # define EXIT_SUCCESS           0       /* exit function success */
119 # define EXIT_FAILURE           1       /* exit function failure */
120 #endif
121
122 #ifndef PATH_MAX
123 # define PATH_MAX 16384
124 #endif
125
126 /****************************************************************************/
127
128 // exception thrown on fatal (per-instance) errors
129 class rxvt_failure_exception { };
130
131 // exception thrown when the command parser runs out of input data
132 class out_of_input { };
133
134 /*
135  *****************************************************************************
136  * PROTOTYPES
137  *****************************************************************************
138  */
139 // main.C
140 #define SET_LOCALE(locale) rxvt_set_locale (locale)
141 extern bool rxvt_set_locale (const char *locale) NOTHROW;
142 extern void rxvt_push_locale (const char *locale) NOTHROW;
143 extern void rxvt_pop_locale () NOTHROW;
144 void rxvt_init ();
145
146 // misc.C
147 char *           rxvt_wcstombs                    (const wchar_t *str, int len = -1);
148 wchar_t *        rxvt_mbstowcs                    (const char *str, int len = -1);
149 char *           rxvt_wcstoutf8                   (const wchar_t *str, int len = -1);
150 wchar_t *        rxvt_utf8towcs                   (const char *str, int len = -1);
151
152 const char *     rxvt_basename                    (const char *str) NOTHROW;
153 void             rxvt_vlog                        (const char *fmt, va_list arg_ptr) NOTHROW;
154 void             rxvt_log                         (const char *fmt,...) NOTHROW;
155 void             rxvt_warn                        (const char *fmt,...) NOTHROW;
156 void             rxvt_fatal                       (const char *fmt, ...) THROW ((class rxvt_failure_exception)) NORETURN;
157 void             rxvt_exit_failure                () THROW ((class rxvt_failure_exception)) NORETURN;
158
159 char           * rxvt_strtrim                     (char *str) NOTHROW;
160 char          ** rxvt_splitcommastring            (const char *cs) NOTHROW;
161 void             rxvt_freecommastring             (char **cs) NOTHROW;
162
163 void           * rxvt_malloc                      (size_t size);
164 void           * rxvt_calloc                      (size_t number, size_t size);
165 void           * rxvt_realloc                     (void *ptr, size_t size);
166
167 /////////////////////////////////////////////////////////////////////////////
168
169 // temporarily replace the process environment
170 extern char **environ;
171 extern char **rxvt_environ; // the original environ pointer
172
173 static inline void
174 set_environ (stringvec *envv)
175 {
176 #if ENABLE_PERL
177   assert (envv);
178 #else
179   if (envv)
180 #endif
181     environ = (char **)envv->begin ();
182 }
183
184 static inline void
185 set_environ (char **envv)
186 {
187 #if ENABLE_PERL
188   assert (envv);
189 #else
190   if (envv)
191 #endif
192     environ = envv;
193 }
194
195 /*
196  *****************************************************************************
197  * STRUCTURES AND TYPEDEFS
198  *****************************************************************************
199  */
200
201 /*
202  * the 'essential' information for reporting Mouse Events
203  * pared down from XButtonEvent
204  */
205 struct mouse_event
206 {
207   int clicks;
208   Time time;             /* milliseconds */
209   unsigned int state;    /* key or button mask */
210   unsigned int button;   /* detail */
211 };
212
213 #if ENABLE_XEMBED
214 // XEMBED messages
215 # define XEMBED_EMBEDDED_NOTIFY          0
216 # define XEMBED_WINDOW_ACTIVATE          1
217 # define XEMBED_WINDOW_DEACTIVATE        2
218 # define XEMBED_REQUEST_FOCUS            3
219 # define XEMBED_FOCUS_IN                 4
220 # define XEMBED_FOCUS_OUT                5
221 # define XEMBED_FOCUS_NEXT               6
222 # define XEMBED_FOCUS_PREV               7
223
224 # define XEMBED_MODALITY_ON              10
225 # define XEMBED_MODALITY_OFF             11
226 # define XEMBED_REGISTER_ACCELERATOR     12
227 # define XEMBED_UNREGISTER_ACCELERATOR   13
228 # define XEMBED_ACTIVATE_ACCELERATOR     14
229
230 // XEMBED detail code
231 # define XEMBED_FOCUS_CURRENT            0
232 # define XEMBED_FOCUS_FIRST              1
233 # define XEMBED_FOCUS_LAST               2
234
235 # define XEMBED_MAPPED                  (1 << 0)
236 #endif
237
238 /*
239  *****************************************************************************
240  * NORMAL DEFINES
241  *****************************************************************************
242  */
243
244 /* COLORTERM, TERM environment variables */
245 #define COLORTERMENV    "rxvt"
246 #ifdef BG_IMAGE_FROM_FILE
247 # define COLORTERMENVFULL COLORTERMENV "-xpm"
248 #else
249 # define COLORTERMENVFULL COLORTERMENV
250 #endif
251 #ifndef TERMENV
252 # ifdef USE_256_COLORS
253 #  define TERMENV        "rxvt-unicode-256color"
254 # else
255 #  define TERMENV        "rxvt-unicode"
256 # endif
257 #endif
258
259 #if defined (NO_MOUSE_REPORT) && !defined (NO_MOUSE_REPORT_SCROLLBAR)
260 # define NO_MOUSE_REPORT_SCROLLBAR 1
261 #endif
262
263 #define scrollBar_esc           30
264
265 #if !defined (RXVT_SCROLLBAR) && !defined (NEXT_SCROLLBAR)
266 # define NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING 1
267 #endif
268
269 enum {
270   NO_REFRESH       = 0,  /* Window not visible at all!        */
271   FAST_REFRESH     = 1,  /* Fully exposed window              */
272   SLOW_REFRESH     = 2,  /* Partially exposed window          */
273 };
274
275 #ifdef NO_SECONDARY_SCREEN
276 # define NSCREENS               0
277 #else
278 # define NSCREENS               1
279 #endif
280
281 /* special (internal) prefix for font commands */
282 #define FONT_CMD                '#'
283 #define FONT_DN                 "#-"
284 #define FONT_UP                 "#+"
285
286 /* flags for rxvt_scr_gotorc () */
287 enum {
288   C_RELATIVE = 1,       /* col movement is relative */
289   R_RELATIVE = 2,       /* row movement is relative */
290   RELATIVE   = C_RELATIVE | R_RELATIVE,
291 };
292
293 /* modes for rxvt_scr_insdel_chars (), rxvt_scr_insdel_lines () */
294 enum {
295   INSERT = -1,                          /* don't change these values */
296   DELETE = +1,
297   ERASE  = +2,
298 };
299
300 /* modes for rxvt_scr_page () - scroll page. used by scrollbar window */
301 enum page_dirn {
302   UP,
303   DN,
304   NO_DIR,
305 };
306
307 /* arguments for rxvt_scr_change_screen () */
308 enum {
309   PRIMARY = 0,
310   SECONDARY,
311 };
312
313 // define various rendition bits and masks. the rendition word
314 // is 32 bits in size, and we should use it as efficiently as possible
315
316 #define RS_None                 0
317
318 // GET_BGATTR depends on RS_fgShift > RS_bgShift
319 #define RS_colorMask            ((1UL << Color_Bits) - 1UL)
320 #define RS_bgShift              0
321 #define RS_fgShift              (RS_bgShift + Color_Bits)
322 #define RS_bgMask               (RS_colorMask << RS_bgShift)
323 #define RS_fgMask               (RS_colorMask << RS_fgShift)
324
325 // must have space for rxvt_fontset::fontCount * 2 + 2 values
326 #define RS_fontShift            (RS_fgShift + Color_Bits)
327 #define RS_Careful              (1UL << RS_fontShift)   /* be careful when drawing these */
328 #define RS_fontMask             ((rxvt_fontset::fontCount << (RS_fontShift + 1)) | RS_Careful)   // includes RS_Careful
329
330 // toggle this to force redraw, must be != RS_Careful and otherwise "pretty neutral"
331 #define RS_redraw               (2UL << RS_fontShift)
332
333 #define RS_Sel                  (1UL << 22)
334
335 // 5 custom bits for extensions
336 #define RS_customCount          16UL
337 #define RS_customShift          23
338 #define RS_customMask           ((RS_customCount - 1UL) << RS_customShift)
339
340 // font styles
341 #define RS_Bold                 (1UL << RS_styleShift)
342 #define RS_Italic               (2UL << RS_styleShift)
343
344 #define RS_styleCount           4
345 #define RS_styleShift           27
346 #define RS_styleMask            (RS_Bold | RS_Italic)
347
348 // fake styles
349 #define RS_Blink                (1UL << 29)
350 #define RS_RVid                 (1UL << 30)    // reverse video
351 #define RS_Uline                (1UL << 31)    // underline
352
353 #define RS_baseattrMask         (RS_Italic | RS_Bold | RS_Blink | RS_RVid | RS_Uline)
354 #define RS_attrMask             (RS_baseattrMask | RS_fontMask)
355
356 #define DEFAULT_RSTYLE  (RS_None | (Color_fg    << RS_fgShift) | (Color_bg     << RS_bgShift))
357 #define OVERLAY_RSTYLE  (RS_None | (Color_Black << RS_fgShift) | (Color_Yellow << RS_bgShift))
358
359 #define Sel_none                0       /* Not waiting */
360 #define Sel_normal              0x01    /* normal selection */
361 #define Sel_incr                0x02    /* incremental selection */
362 #define Sel_direct              0x00
363 #define Sel_Primary             0x01
364 #define Sel_Secondary           0x02
365 #define Sel_Clipboard           0x03
366 #define Sel_whereMask           0x0f
367 #define Sel_CompoundText        0x10    /* last request was COMPOUND_TEXT */
368 #define Sel_UTF8String          0x20    /* last request was UTF8_STRING */
369
370 enum {
371   C0_NUL = 0x00,
372           C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
373   C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
374   C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
375   C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
376 };
377 #define CHAR_ST                 0x9c    /* 0234 */
378
379 /*
380  * XTerm Operating System Commands: ESC ] Ps;Pt (ST|BEL)
381  * colour extensions by Christian W. Zuckschwerdt <zany@triq.net>
382  */
383 enum {
384   XTerm_name             =  0,
385   XTerm_iconName         =  1,
386   XTerm_title            =  2,
387   XTerm_property         =  3,      // change X property
388   XTerm_Color            =  4,      // change colors
389   XTerm_Color00          = 10,      // change fg color
390   XTerm_Color01          = 11,      // change bg color
391   XTerm_Color_cursor     = 12,      // change actual 'Cursor' color
392   XTerm_Color_pointer_fg = 13,      // change actual 'Pointer' fg color
393   XTerm_Color_pointer_bg = 14,      // change actual 'Pointer' bg color
394   XTerm_Color05          = 15,      // not implemented (tektronix fg)
395   XTerm_Color06          = 16,      // not implemented (tektronix bg)
396   XTerm_Color_HC         = 17,      // change actual 'Highlight' bg color
397   XTerm_Color_HTC        = 19,      // change actual 'Highlight' fg color
398   XTerm_logfile          = 46,      // not implemented
399   XTerm_font             = 50,
400
401   XTerm_konsole30        = 30,      // reserved for konsole
402   XTerm_konsole31        = 31,      // reserved for konsole
403   XTerm_emacs51          = 51,      // reserved for emacs shell
404   /*
405    * rxvt extensions of XTerm OSCs: ESC ] Ps;Pt (ST|BEL)
406    */
407
408   // deprecated
409   Rxvt_restoreFG         = 39,
410   Rxvt_restoreBG         = 49,
411
412   Rxvt_Pixmap            = 20,      // new bg pixmap
413   Rxvt_dumpscreen        = 55,      // dump scrollback and all of screen
414
415   URxvt_locale           = 701,     // change locale
416   URxvt_version          = 702,     // request version
417
418   URxvt_Color_IT         = 704,     // change actual 'Italic' colour
419   URxvt_Color_tint       = 705,     // change actual tint colour
420   URxvt_Color_BD         = 706,     // change actual 'Bold' color
421   URxvt_Color_UL         = 707,     // change actual 'Underline' color
422   URxvt_Color_border     = 708,
423
424   URxvt_font             = 710,
425   URxvt_boldFont         = 711,
426   URxvt_italicFont       = 712,
427   URxvt_boldItalicFont   = 713,
428
429   URxvt_view_up          = 720,
430   URxvt_view_down        = 721,
431
432   URxvt_perl             = 777,     // for use by perl extensions, starts with "extension-name;"
433 };
434
435 /* Words starting with `Color_' are colours.  Others are counts */
436 /*
437  * The PixColor and rendition colour usage should probably be decoupled
438  * on the unnecessary items, e.g. Color_pointer, but won't bother
439  * until we need to.  Also, be aware of usage in pixcolor_set
440  */
441
442 enum colour_list {
443   Color_none = -2,
444   Color_transparent = -1,
445   Color_fg = 0,
446   Color_bg,
447   minCOLOR,                   /* 2 */
448   Color_Black = minCOLOR,
449   Color_Red3,
450   Color_Green3,
451   Color_Yellow3,
452   Color_Blue3,
453   Color_Magenta3,
454   Color_Cyan3,
455   maxCOLOR,                   /* minCOLOR + 7 */
456 #ifndef NO_BRIGHTCOLOR
457   Color_AntiqueWhite = maxCOLOR,
458   minBrightCOLOR,             /* maxCOLOR + 1 */
459   Color_Grey25 = minBrightCOLOR,
460   Color_Red,
461   Color_Green,
462   Color_Yellow,
463   Color_Blue,
464   Color_Magenta,
465   Color_Cyan,
466   maxBrightCOLOR,             /* minBrightCOLOR + 7 */
467   Color_White = maxBrightCOLOR,
468 #else
469   Color_White = maxCOLOR,
470 #endif
471   minTermCOLOR = Color_White + 1,
472 #ifndef USE_256_COLORS
473   maxTermCOLOR = Color_White + 72,
474 #else
475   maxTermCOLOR = Color_White + 240,
476 #endif
477 #ifndef NO_CURSORCOLOR
478   Color_cursor,
479   Color_cursor2,
480 #endif
481   Color_pointer_fg,
482   Color_pointer_bg,
483   Color_border,
484 #ifndef NO_BOLD_UNDERLINE_REVERSE
485   Color_BD,
486   Color_IT,
487   Color_UL,
488   Color_RV,
489 #endif
490 #if ENABLE_FRILLS
491   Color_underline,
492 #endif
493 #ifdef OPTION_HC
494   Color_HC,
495   Color_HTC,
496 #endif
497   Color_scroll,
498 #ifdef RXVT_SCROLLBAR
499   Color_trough,
500 #endif
501 #if ENABLE_TRANSPARENCY
502   Color_tint,
503 #endif
504 #if OFF_FOCUS_FADING
505   Color_fade,
506 #endif
507   NRS_COLORS,                 /* */
508 #ifdef RXVT_SCROLLBAR
509   Color_topShadow = NRS_COLORS,
510   Color_bottomShadow,
511   TOTAL_COLORS
512 #else
513   TOTAL_COLORS = NRS_COLORS
514 #endif
515 };
516
517 #ifndef USE_256_COLORS
518 #define Color_Bits      7 // 0 .. maxTermCOLOR
519 #else
520 #define Color_Bits      9 // 0 .. maxTermCOLOR
521 #endif
522
523 /*
524  * Resource list
525  */
526 enum {
527 # define def(name) Rs_ ## name,
528 # define reserve(name,count) Rs_ ## name ## _ = Rs_ ## name + (count) - 1,
529 # include "rsinc.h"
530 # undef def
531 # undef reserve
532   NUM_RESOURCES
533 };
534
535 /* DEC private modes */
536 #define PrivMode_132            (1UL<<0)
537 #define PrivMode_132OK          (1UL<<1)
538 #define PrivMode_rVideo         (1UL<<2)
539 #define PrivMode_relOrigin      (1UL<<3)
540 #define PrivMode_Screen         (1UL<<4)
541 #define PrivMode_Autowrap       (1UL<<5)
542 #define PrivMode_aplCUR         (1UL<<6)
543 #define PrivMode_aplKP          (1UL<<7)
544 #define PrivMode_HaveBackSpace  (1UL<<8)
545 #define PrivMode_BackSpace      (1UL<<9)
546 #define PrivMode_ShiftKeys      (1UL<<10)
547 #define PrivMode_VisibleCursor  (1UL<<11)
548 #define PrivMode_MouseX10       (1UL<<12)
549 #define PrivMode_MouseX11       (1UL<<13)
550 #define PrivMode_scrollBar      (1UL<<14)
551 #define PrivMode_TtyOutputInh   (1UL<<15)
552 #define PrivMode_Keypress       (1UL<<16)
553 #define PrivMode_smoothScroll   (1UL<<17)
554 #define PrivMode_vt52           (1UL<<18)
555 #define PrivMode_LFNL           (1UL<<19)
556 #define PrivMode_MouseBtnEvent  (1UL<<20)
557 #define PrivMode_MouseAnyEvent  (1UL<<21)
558 #define PrivMode_BracketPaste   (1UL<<22)
559
560 #define PrivMode_mouse_report   (PrivMode_MouseX10|PrivMode_MouseX11|PrivMode_MouseBtnEvent|PrivMode_MouseAnyEvent)
561
562 #ifdef ALLOW_132_MODE
563 # define PrivMode_Default (PrivMode_Autowrap|PrivMode_ShiftKeys|PrivMode_VisibleCursor|PrivMode_132OK)
564 #else
565 # define PrivMode_Default (PrivMode_Autowrap|PrivMode_ShiftKeys|PrivMode_VisibleCursor)
566 #endif
567
568 // do not change these constants lightly, there are many interdependencies
569 #define IMBUFSIZ               128     // input modifier buffer sizes
570 #define KBUFSZ                 512     // size of keyboard mapping buffer
571 #define CBUFSIZ                2048    // size of command buffer
572 #define CBUFCNT                8       // never call pty_fill/cmd_parse more than this often in a row
573 #define UBUFSIZ                2048    // character buffer
574
575 #if ENABLE_FRILLS
576 # include <X11/Xmd.h>
577 typedef struct _mwmhints
578 {
579   CARD32 flags;
580   CARD32 functions;
581   CARD32 decorations;
582   INT32  input_mode;
583   CARD32 status;
584 } MWMHints;
585 #endif
586
587 /* Motif window hints */
588 #define MWM_HINTS_FUNCTIONS     (1L << 0)
589 #define MWM_HINTS_DECORATIONS   (1L << 1)
590 #define MWM_HINTS_INPUT_MODE    (1L << 2)
591 #define MWM_HINTS_STATUS        (1L << 3)
592 /* bit definitions for MwmHints.functions */
593 #define MWM_FUNC_ALL            (1L << 0)
594 #define MWM_FUNC_RESIZE         (1L << 1)
595 #define MWM_FUNC_MOVE           (1L << 2)
596 #define MWM_FUNC_MINIMIZE       (1L << 3)
597 #define MWM_FUNC_MAXIMIZE       (1L << 4)
598 #define MWM_FUNC_CLOSE          (1L << 5)
599 /* bit definitions for MwmHints.decorations */
600 #define MWM_DECOR_ALL           (1L << 0)
601 #define MWM_DECOR_BORDER        (1L << 1)
602 #define MWM_DECOR_RESIZEH       (1L << 2)
603 #define MWM_DECOR_TITLE         (1L << 3)
604 #define MWM_DECOR_MENU          (1L << 4)
605 #define MWM_DECOR_MINIMIZE      (1L << 5)
606 #define MWM_DECOR_MAXIMIZE      (1L << 6)
607 /* bit definitions for MwmHints.inputMode */
608 #define MWM_INPUT_MODELESS                  0
609 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
610 #define MWM_INPUT_SYSTEM_MODAL              2
611 #define MWM_INPUT_FULL_APPLICATION_MODAL    3
612 #define PROP_MWM_HINTS_ELEMENTS             5
613
614 /*
615  *****************************************************************************
616  * MACRO DEFINES
617  *****************************************************************************
618  */
619 #define dLocal(type,name)       type const name = this->name
620
621 // for speed reasons, we assume that all codepoints 32 to 126 are
622 // single-width.
623 #define WCWIDTH(c)              (IN_RANGE_INC (c, 0x20, 0x7e) ? 1 : wcwidth (c))
624
625 /* convert pixel dimensions to row/column values.  Everything as int32_t */
626 #define Pixel2Col(x)            Pixel2Width((int32_t)(x))
627 #define Pixel2Row(y)            Pixel2Height((int32_t)(y))
628 #define Pixel2Width(x)          ((int32_t)(x) / (int32_t)fwidth)
629 #define Pixel2Height(y)         ((int32_t)(y) / (int32_t)fheight)
630 #define Col2Pixel(col)          ((int32_t)Width2Pixel(col))
631 #define Row2Pixel(row)          ((int32_t)Height2Pixel(row))
632 #define Width2Pixel(n)          ((int32_t)(n) * (int32_t)fwidth)
633 #define Height2Pixel(n)         ((int32_t)(n) * (int32_t)fheight)
634
635 #define LINENO_of(t,n) MOD ((t)->term_start + int(n), (t)->total_rows)
636 #define ROW_of(t,n) (t)->row_buf [LINENO_of ((t), n)]
637
638 #define LINENO(n) LINENO_of (this, n)
639 #define ROW(n) ROW_of (this, n)
640
641 /* how to build & extract colors and attributes */
642 #define GET_BASEFG(x)           (((x) & RS_fgMask) >> RS_fgShift)
643 #define GET_BASEBG(x)           (((x) & RS_bgMask) >> RS_bgShift)
644
645 #define GET_FONT(x)             (((x) & RS_fontMask) >> RS_fontShift)
646 #define SET_FONT(x,fid)         (((x) & ~RS_fontMask) | ((fid) << RS_fontShift))
647
648 #define GET_STYLE(x)            (((x) & RS_styleMask) >> RS_styleShift)
649 #define SET_STYLE(x,style)      (((x) & ~RS_styleMask) | ((style) << RS_styleShift))
650
651 #define GET_ATTR(x)             (((x) & RS_attrMask))
652 // return attributes defining the background, encoding doesn't matter
653 // depends on RS_fgShift > RS_bgShift
654 #define GET_BGATTR(x)                                      \
655   (expect_false ((x) & RS_RVid)                            \
656     ? (((x) & (RS_attrMask & ~RS_RVid))                    \
657       | (((x) & RS_fgMask) >> (RS_fgShift - RS_bgShift)))  \
658     : ((x) & (RS_attrMask | RS_bgMask)))
659 #define SET_FGCOLOR(x,fg)       (((x) & ~RS_fgMask)   | ((fg) << RS_fgShift))
660 #define SET_BGCOLOR(x,bg)       (((x) & ~RS_bgMask)   | ((bg) << RS_bgShift))
661 #define SET_ATTR(x,a)           (((x) & ~RS_attrMask) | (a))
662
663 #define RS_SAME(a,b)            (!(((a) ^ (b)) & ~RS_Careful))
664
665 #define PIXCOLOR_NAME(idx)      rs[Rs_color + (idx)]
666 #define ISSET_PIXCOLOR(idx)     (!!rs[Rs_color + (idx)])
667
668 #if ENABLE_STYLES
669 # define FONTSET_of(t,style) (t)->fontset[GET_STYLE (style)]
670 #else
671 # define FONTSET_of(t,style) (t)->fontset[0]
672 #endif
673
674 #define FONTSET(style) FONTSET_of (this, style)
675
676 typedef callback<void (const char *)> log_callback;
677 typedef callback<int (int)> getfd_callback;
678
679 /****************************************************************************/
680
681 #define LINE_LONGER     0x0001 // line is continued on the next row
682 #define LINE_FILTERED   0x0002 // line has been filtered
683 #define LINE_COMPRESSED 0x0004 // line has been compressed (NYI)
684 #define LINE_FILTER     0x0008 // line needs to be filtered before display (NYI)
685 #define LINE_BIDI       0x0010 // line needs bidi (NYI)
686
687 struct line_t
688 {
689    text_t *t; // terminal the text
690    rend_t *r; // rendition, uses RS_ flags
691    tlen_t_ l; // length of each text line
692    uint32_t f; // flags
693
694    bool is_longer ()
695    {
696      return f & LINE_LONGER;
697    }
698
699    void is_longer (int set)
700    {
701      if (set)
702        f |= LINE_LONGER;
703      else
704        f &= ~LINE_LONGER;
705    }
706
707    void clear ()
708    {
709      t = 0;
710      r = 0;
711      l = 0;
712      f = 0;
713    }
714
715    void touch () // call whenever a line is changed/touched/updated
716    {
717 #if ENABLE_PERL
718      f &= ~LINE_FILTERED;
719 #endif
720    }
721
722    void touch (int col)
723    {
724      max_it (l, col);
725      touch ();
726    }
727 };
728
729 /****************************************************************************/
730
731 // primitive wrapper around mbstate_t to ensure initialisation
732 struct mbstate
733 {
734   mbstate_t mbs;
735
736   operator mbstate_t *() { return &mbs; }
737   void reset () { memset (&mbs, 0, sizeof (mbs)); }
738   mbstate () { reset (); }
739 };
740
741 /****************************************************************************/
742
743 #define UNICODE_MASK 0x1fffffUL
744
745 #if UNICODE_3
746 # define COMPOSE_LO 0x40000000UL
747 # define COMPOSE_HI 0x400fffffUL
748 # define IS_COMPOSE(n) ((int32_t)(n) >= COMPOSE_LO)
749 #else
750 # if ENABLE_PERL
751 #  define COMPOSE_LO 0xe000UL // our _own_ functions don't like (illegal) surrogates
752 #  define COMPOSE_HI 0xf8ffUL // in utf-8, so use private use area only
753 # else
754 #  define COMPOSE_LO 0xd800UL
755 #  define COMPOSE_HI 0xf8ffUL
756 # endif
757 # define IS_COMPOSE(n) IN_RANGE_INC ((n), COMPOSE_LO, COMPOSE_HI)
758 #endif
759
760 #if ENABLE_COMBINING
761 // compose chars are used to represent composite characters
762 // that are not representable in unicode, as well as characters
763 // not fitting in the BMP.
764 struct compose_char
765 {
766   unicode_t c1, c2; // any chars != NOCHAR are valid
767   compose_char (unicode_t c1, unicode_t c2)
768   : c1(c1), c2(c2)
769   { }
770 };
771
772 class rxvt_composite_vec
773 {
774   vector<compose_char> v;
775 public:
776   text_t compose (unicode_t c1, unicode_t c2 = NOCHAR);
777   int expand (unicode_t c, wchar_t *r);
778   compose_char *operator [](text_t c)
779   {
780     return c >= COMPOSE_LO && c < COMPOSE_LO + v.size ()
781            ? &v[c - COMPOSE_LO]
782            : 0;
783   }
784 };
785
786 extern class rxvt_composite_vec rxvt_composite;
787 #endif
788
789 /****************************************************************************/
790
791 #ifdef KEYSYM_RESOURCE
792 class keyboard_manager;
793 #endif
794
795 typedef struct rxvt_term *rxvt_t;
796
797 extern rxvt_t rxvt_current_term;
798
799 #define SET_R(r) rxvt_current_term = const_cast<rxvt_term *>(r)
800 #define GET_R rxvt_current_term
801
802 /* ------------------------------------------------------------------------- */
803 struct overlay_base
804 {
805   int x, y, w, h; // overlay dimensions
806   text_t **text;
807   rend_t **rend;
808
809   // while tempting to add swap() etc. here, it effetcively only increases code size
810 };
811
812 /* ------------------------------------------------------------------------- */
813
814 typedef struct
815 {
816   int row;
817   int col;
818 } row_col_t;
819
820 /*
821  * terminal limits:
822  *
823  *  width      : 1 <= width
824  *  height     : 1 <= height
825  *  ncol       : 1 <= ncol       <= MAX(tlen_t)
826  *  nrow       : 1 <= nrow       <= MAX(int)
827  *  saveLines  : 0 <= saveLines  <= MAX(int)
828  *  term_start : 0 <= term_start < saveLines
829  *  total_rows : nrow + saveLines
830  *
831  *  top_row    : -saveLines <= top_row    <= 0
832  *  view_start : top_row    <= view_start <= 0
833  *
834  *          | most coordinates are stored relative to term_start,
835  *  ROW_BUF | which is the first line of the terminal screen
836  *  |························= row_buf[0]
837  *  |························= row_buf[1]
838  *  |························= row_buf[2] etc.
839  *  |
840  *  +------------+···········= term_start + top_row
841  *  | scrollback |
842  *  | scrollback +---------+·= term_start + view_start
843  *  | scrollback | display |
844  *  | scrollback | display |
845  *  +------------+·display·+·= term_start
846  *  |  terminal  | display |
847  *  |  terminal  +---------+
848  *  |  terminal  |
849  *  |  terminal  |
850  *  +------------+···········= term_start + nrow - 1
851  *  |
852  *  |
853  *  END······················= total_rows
854  */
855
856 struct TermWin_t
857 {
858   int            width;         /* window width                    [pixels] */
859   int            height;        /* window height                   [pixels] */
860   int            fwidth;        /* font width                      [pixels] */
861   int            fheight;       /* font height                     [pixels] */
862   int            fbase;         /* font ascent (baseline)          [pixels] */
863   int            ncol;          /* window columns              [characters] */
864   int            nrow;          /* window rows                 [characters] */
865   int            focus;         /* window has focus                         */
866   int            mapped;        /* window state mapped?                     */
867   int            int_bwidth;    /* internal border width                    */
868   int            ext_bwidth;    /* external border width                    */
869   int            lineSpace;     /* number of extra pixels between rows      */
870   int            letterSpace;   /* number of extra pixels between columns   */
871   int            saveLines;     /* number of lines that fit in scrollback   */
872   int            total_rows;    /* total number of rows in this terminal    */
873   int            term_start;    /* term lines start here                    */
874   int            view_start;    /* scrollback view starts here              */
875   int            top_row;       /* topmost row index of scrollback          */
876   Window         parent[6];     /* parent identifiers - we're parent[0]     */
877   Window         vt;            /* vt100 window                             */
878   GC             gc;            /* GC for drawing                           */
879   Pixmap         pixmap;
880   rxvt_drawable *drawable;
881   rxvt_fontset  *fontset[4];
882 };
883
884 /*
885  * screen accounting:
886  * screen_t elements
887  *   row:       Cursor row position                   : 0 <= row < nrow
888  *   col:       Cursor column position                : 0 <= col < ncol
889  *   tscroll:   Scrolling region top row inclusive    : 0 <= row < nrow
890  *   bscroll:   Scrolling region bottom row inclusive : 0 <= row < nrow
891  *
892  * selection_t elements
893  *   clicks:    1, 2 or 3 clicks - 4 indicates a special condition of 1 where
894  *              nothing is selected
895  *   beg:       row/column of beginning of selection  : never past mark
896  *   mark:      row/column of initial click           : never past end
897  *   end:       row/column of one character past end of selection
898  * * Note: top_row <= beg.row <= mark.row <= end.row < nrow
899  * * Note: col == -1 ==> we're left of screen
900  *
901  */
902 struct screen_t
903 {
904   row_col_t       cur;          /* cursor position on the screen             */
905   int             tscroll;      /* top of settable scroll region             */
906   int             bscroll;      /* bottom of settable scroll region          */
907   unsigned int    charset;      /* character set number [0..3]               */
908   unsigned int    flags;        /* see below                                 */
909   row_col_t       s_cur;        /* saved cursor position                     */
910   unsigned int    s_charset;    /* saved character set number [0..3]         */
911   char            s_charset_char;
912   rend_t          s_rstyle;     /* saved rendition style                     */
913 };
914
915 enum selection_op_t
916 {
917   SELECTION_CLEAR = 0,  /* nothing selected                          */
918   SELECTION_INIT,       /* marked a point                            */
919   SELECTION_BEGIN,      /* started a selection                       */
920   SELECTION_CONT,       /* continued selection                       */
921   SELECTION_DONE        /* selection put in CUT_BUFFER0              */
922 };
923
924 struct selection_t
925 {
926   wchar_t          *text;       /* selected text                             */
927   unsigned int      len;        /* length of selected text                   */
928   unsigned int      screen;     /* screen being used                         */
929   unsigned int      clicks;     /* number of clicks                          */
930   selection_op_t    op;         /* current operation                         */
931   bool              rect;       /* rectangular selection?                    */
932   row_col_t         beg;        /* beginning of selection   <= mark          */
933   row_col_t         mark;       /* point of initial click   <= end           */
934   row_col_t         end;        /* one character past end point              */
935   wchar_t          *clip_text;  /* text copied to the clipboard              */
936   unsigned int      clip_len;   /* length of clipboard text                  */
937 };
938
939 /* ------------------------------------------------------------------------- */
940
941 /* screen_t flags */
942 #define Screen_Relative          (1<<0)  /* relative origin mode flag         */
943 #define Screen_VisibleCursor     (1<<1)  /* cursor visible?                   */
944 #define Screen_Autowrap          (1<<2)  /* auto-wrap flag                    */
945 #define Screen_Insert            (1<<3)  /* insert mode (vs. overstrike)      */
946 #define Screen_WrapNext          (1<<4)  /* need to wrap for next char?       */
947 #define Screen_DefaultFlags      (Screen_VisibleCursor | Screen_Autowrap)
948
949 /* rxvt_vars.options */
950 enum {
951 # define def(name,idx) Opt_ ## name = idx,
952 # define nodef(name)   Opt_ ## name = 0,
953 # include "optinc.h"
954 # undef nodef
955 # undef def
956 Opt_count
957 };
958
959 /* ------------------------------------------------------------------------- */
960
961 struct rxvt_vars : TermWin_t
962 {
963   scrollBar_t     scrollBar;
964   uint8_t         options[(Opt_count + 7) >> 3];
965   XSizeHints      szHint;
966   rxvt_color     *pix_colors;
967   Cursor          TermWin_cursor;       /* cursor for vt window */
968   line_t         *row_buf;      // all lines, scrollback + terminal, circular, followed by temp_buf
969   line_t         *drawn_buf;    // text on screen
970   line_t         *swap_buf;     // lines for swap buffer
971   char           *tabs;         /* per location: 1 == tab-stop               */
972   screen_t        screen;
973   screen_t        swap;
974   selection_t     selection;
975   rxvt_color      pix_colors_focused[TOTAL_COLORS];
976 #ifdef OFF_FOCUS_FADING
977   rxvt_color      pix_colors_unfocused[TOTAL_COLORS];
978 #endif
979 };
980
981 struct rxvt_term : zero_initialized, rxvt_vars, rxvt_screen
982 {
983
984   // special markers with magic addresses
985   static const char resval_undef [];    // options specifically unset
986   static const char resval_on [];       // boolean options switched on
987   static const char resval_off [];      // or off
988
989   log_callback   *log_hook;             // log error messages through this hook, if != 0
990   getfd_callback *getfd_hook;           // convert remote to local fd, if != 0
991 #if ENABLE_PERL
992   rxvt_perl_term  perl;
993 #endif
994   struct mbstate  mbstate;              // current input multibyte state
995
996   unsigned char   want_refresh:1,
997                   current_screen:1,     /* primary or secondary              */
998                   num_scr_allow:1,
999                   bypass_keystate:1,
1000 #if ENABLE_FRILLS
1001                   urgency_hint:1,
1002 #endif
1003 #if CURSOR_BLINK
1004                   hidden_cursor:1,
1005 #endif
1006 #if TEXT_BLINK
1007                   hidden_text:1,
1008 #endif
1009 #if POINTER_BLANK
1010                   hidden_pointer:1,
1011 #endif
1012                   enc_utf8:1,           /* whether locale uses utf-8 */
1013                   seen_input:1,         /* whether we have seen some program output yet */
1014                   seen_resize:1,        /* whether we had a resize event */
1015                   parsed_geometry:1;
1016
1017   unsigned char   refresh_type,
1018 #ifdef META8_OPTION
1019                   meta_char,            /* Alt-key prefix */
1020 #endif
1021                   selection_wait,
1022                   selection_type;
1023 /* ---------- */
1024   bool            rvideo_state, rvideo_mode;
1025 #ifndef NO_BELL
1026   bool            rvideo_bell;
1027 #endif
1028   int             num_scr;              /* screen: number of lines scrolled */
1029   int             prev_ncol,            /* screen: previous number of columns */
1030                   prev_nrow;            /* screen: previous number of rows */
1031 /* ---------- */
1032   rend_t          rstyle;
1033 /* ---------- */
1034 #ifdef SELECTION_SCROLLING
1035   int             scroll_selection_lines;
1036   enum page_dirn  scroll_selection_dir;
1037   int             selection_save_x,
1038                   selection_save_y,
1039                   selection_save_state;
1040 #endif
1041 /* ---------- */
1042   int             csrO,       /* Hops - csr offset in thumb/slider to      */
1043                               /*   give proper Scroll behaviour            */
1044 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1045                   mouse_slip_wheel_speed,
1046 #endif
1047                   refresh_count,
1048                   window_vt_x,
1049                   window_vt_y,
1050                   mouse_row,
1051                   mouse_col,
1052 # ifdef POINTER_BLANK
1053                   pointerBlankDelay,
1054 # endif
1055                   allowedxerror;
1056 /* ---------- */
1057   unsigned int    ModLevel3Mask,
1058                   ModMetaMask,
1059                   ModNumLockMask;
1060   int             old_width,  /* last used width in screen resize          */
1061                   old_height; /* last used height in screen resize         */
1062   unsigned long   priv_modes,
1063                   SavedModes;
1064   bool            button_state[5];
1065 /* ---------- */
1066   Atom            *xa;
1067 /* ---------- */
1068   Time            selection_time,
1069                   selection_request_time,
1070                   clipboard_time;
1071   pid_t           cmd_pid;    /* process id of child */
1072   char *          incr_buf;
1073   size_t          incr_buf_size, incr_buf_fill;
1074 /* ---------- */
1075   struct mouse_event MEvent;
1076   XComposeStatus  compose;
1077   struct termios  tio;
1078   row_col_t       oldcursor;
1079 #ifdef HAVE_BG_PIXMAP
1080   bgPixmap_t      bgPixmap;
1081 #endif
1082 #ifdef HAVE_AFTERIMAGE
1083   ASVisual       *asv;
1084   ASImageManager *asimman;
1085
1086   void init_asv ()
1087   {
1088     if (!asv)
1089       asv = create_asvisual_for_id (dpy, display->screen, depth, XVisualIDFromVisual (visual), cmap, NULL);
1090   }
1091 #endif
1092
1093 #if ENABLE_OVERLAY
1094   overlay_base ov;
1095
1096   void scr_swap_overlay () NOTHROW;
1097   void scr_overlay_new (int x, int y, int w, int h) NOTHROW;
1098   void scr_overlay_off () NOTHROW;
1099   void scr_overlay_set (int x, int y,
1100                         text_t text,
1101                         rend_t rend = OVERLAY_RSTYLE) NOTHROW;
1102   void scr_overlay_set (int x, int y, const char *s) NOTHROW;
1103   void scr_overlay_set (int x, int y, const wchar_t *s) NOTHROW;
1104 #endif
1105
1106   vector<void *> allocated;           // free these memory blocks with free()
1107
1108   char            env_windowid[21];   /* environmental variable WINDOWID */
1109   char            env_colorfgbg[sizeof ("COLORFGBG=default;default;bg") + 1];
1110   char           *env_display;        /* environmental variable DISPLAY  */
1111   char           *env_term;           /* environmental variable TERM     */
1112
1113   char           *locale;
1114   char            charsets[4];
1115   char           *v_buffer;           /* pointer to physical buffer */
1116   unsigned int    v_buflen;           /* size of area to write */
1117   stringvec      *argv, *envv;        /* if != 0, will be freed at destroy time */
1118
1119 #ifdef KEYSYM_RESOURCE
1120   keyboard_manager *keyboard;
1121 #endif
1122 #ifndef NO_RESOURCES
1123   XrmDatabase option_db;
1124 #endif
1125
1126   const char     *rs[NUM_RESOURCES];
1127   /* command input buffering */
1128   char           *cmdbuf_ptr, *cmdbuf_endp;
1129   char            cmdbuf_base[CBUFSIZ];
1130
1131   ptytty         *pty;
1132
1133   rxvt_salloc    *talloc;             // text line allocator
1134   rxvt_salloc    *ralloc;             // rend line allocator
1135
1136   static vector<rxvt_term *> termlist; // a vector of all running rxvt_term's
1137
1138 #if ENABLE_FRILLS || ISO_14755
1139   // ISO 14755 entry support
1140   unicode_t iso14755buf;
1141   void commit_iso14755 ();
1142 # if ISO_14755
1143   void iso14755_51 (unicode_t ch, rend_t r = DEFAULT_RSTYLE, int x = 0, int y = -1);
1144   void iso14755_54 (int x, int y);
1145 # endif
1146 #endif
1147
1148   long vt_emask, vt_emask_perl, vt_emask_xim, vt_emask_mouse;
1149
1150   void vt_select_input () const NOTHROW
1151   {
1152     XSelectInput (dpy, vt, vt_emask | vt_emask_perl | vt_emask_xim | vt_emask_mouse);
1153   }
1154
1155 #if ENABLE_TRANSPARENCY || ENABLE_PERL
1156   void rootwin_cb (XEvent &xev);
1157   xevent_watcher rootwin_ev;
1158 #endif
1159 #ifdef HAVE_BG_PIXMAP
1160   void update_background ();
1161 #if TRACE_PIXMAPS
1162   void trace_update_background (const char *file, int line);
1163 # define update_background() trace_update_background (__FILE__, __LINE__)
1164 #endif
1165   void update_background_cb (ev::timer &w, int revents);
1166   ev::timer update_background_ev;
1167 #endif
1168
1169   void x_cb (XEvent &xev);
1170   xevent_watcher termwin_ev;
1171   xevent_watcher vt_ev;
1172   xevent_watcher scrollbar_ev;
1173
1174   void child_cb (ev::child &w, int revents); ev::child child_ev;
1175   void destroy_cb (ev::idle &w, int revents); ev::idle destroy_ev;
1176   void refresh_check ();
1177   void flush ();
1178   void flush_cb (ev::timer &w, int revents); ev::timer flush_ev;
1179   bool pty_fill ();
1180   void pty_cb (ev::io &w, int revents); ev::io pty_ev;
1181   void incr_cb (ev::timer &w, int revents) NOTHROW; ev::timer incr_ev;
1182
1183 #ifdef CURSOR_BLINK
1184   void cursor_blink_cb (ev::timer &w, int revents); ev::timer cursor_blink_ev;
1185 #endif
1186 #ifdef TEXT_BLINK
1187   void text_blink_cb (ev::timer &w, int revents); ev::timer text_blink_ev;
1188 #endif
1189 #ifndef NO_BELL
1190   void bell_cb (ev::timer &w, int revents); ev::timer bell_ev;
1191 #endif
1192
1193 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
1194   void cont_scroll_cb (ev::timer &w, int revents); ev::timer cont_scroll_ev;
1195 #endif
1196 #ifdef SELECTION_SCROLLING
1197   void sel_scroll_cb (ev::timer &w, int revents); ev::timer sel_scroll_ev;
1198 #endif
1199 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1200   void slip_wheel_cb (ev::timer &w, int revents); ev::timer slip_wheel_ev;
1201 #endif
1202
1203 #ifdef POINTER_BLANK
1204   void pointer_cb (ev::timer &w, int revents); ev::timer pointer_ev;
1205   void pointer_blank ();
1206 #endif
1207   void pointer_unblank ();
1208
1209   void tt_printf (const char *fmt,...);
1210   void tt_write (const char *data, unsigned int len);
1211   void pty_write ();
1212
1213   void init (stringvec *argv, stringvec *envv)
1214   {
1215     this->argv = argv;
1216     init (argv->size (), argv->begin (), envv);
1217   }
1218
1219   void make_current () const // make this the "currently active" urxvt instance
1220   {
1221     SET_R (this);
1222     set_environ (envv);
1223     rxvt_set_locale (locale);
1224   }
1225
1226 #if USE_XIM
1227   rxvt_xim *input_method;
1228   XIC      Input_Context;
1229   XIMStyle input_style;
1230   XPoint   spot; // most recently sent spot position
1231
1232   void im_destroy ();
1233   void im_cb (); im_watcher im_ev;
1234   void im_set_size (XRectangle &size);
1235   void im_set_position (XPoint &pos) NOTHROW;
1236   void im_set_color (unsigned long &fg, unsigned long &bg);
1237   void im_set_preedit_area (XRectangle &preedit_rect, XRectangle &status_rect, const XRectangle &needed_rect);
1238
1239   bool IMisRunning ();
1240   void IMSendSpot ();
1241   bool IM_get_IC (const char *modifiers);
1242   void IMSetPosition ();
1243 #endif
1244
1245   // command.C
1246   void key_press (XKeyEvent &ev);
1247   void key_release (XKeyEvent &ev);
1248   unsigned int cmd_write (const char *str, unsigned int count);
1249
1250   wchar_t next_char () NOTHROW;
1251   wchar_t cmd_getc () THROW ((class out_of_input));
1252   uint32_t next_octet () NOTHROW;
1253   uint32_t cmd_get8 () THROW ((class out_of_input));
1254
1255   void cmd_parse ();
1256   void mouse_report (XButtonEvent &ev);
1257   void button_press (XButtonEvent &ev);
1258   void button_release (XButtonEvent &ev);
1259   void focus_in ();
1260   void focus_out ();
1261 #if ENABLE_FRILLS
1262   void set_urgency (bool enable);
1263 #else
1264   void set_urgency (bool enable) { }
1265 #endif
1266   void update_fade_color (unsigned int idx);
1267 #ifdef PRINTPIPE
1268   FILE *popen_printer ();
1269   int pclose_printer (FILE *stream);
1270 #endif
1271   void process_print_pipe ();
1272   void process_nonprinting (unicode_t ch);
1273   void process_escape_vt52 (unicode_t ch);
1274   void process_escape_seq ();
1275   void process_csi_seq ();
1276   void process_window_ops (const int *args, unsigned int nargs);
1277   char *get_to_st (unicode_t &ends_how);
1278   void process_dcs_seq ();
1279   void process_osc_seq ();
1280   void process_color_seq (int report, int color, const char *str, char resp);
1281   void process_xterm_seq (int op, char *str, char resp);
1282   int privcases (int mode, unsigned long bit);
1283   void process_terminal_mode (int mode, int priv, unsigned int nargs, const int *arg);
1284   void process_sgr_mode (unsigned int nargs, const int *arg);
1285   void process_graphics ();
1286   // init.C
1287   void init_vars ();
1288   const char **init_resources (int argc, const char *const *argv);
1289   void init (int argc, const char *const *argv, stringvec *envv);
1290   void init_env ();
1291   void set_locale (const char *locale);
1292   void init_xlocale ();
1293   void init_command (const char *const *argv);
1294   void run_command (const char *const *argv);
1295   int run_child (const char *const *argv);
1296   void color_aliases (int idx);
1297   void create_windows (int argc, const char *const *argv);
1298   void get_colours ();
1299   void get_ourmods ();
1300   // main.C
1301   void tt_winch ();
1302   rxvt_term ();
1303   ~rxvt_term ();
1304   void destroy ();
1305   void emergency_cleanup ();
1306   void recolour_cursor ();
1307   void resize_all_windows (unsigned int newwidth, unsigned int newheight, int ignoreparent);
1308   void window_calc (unsigned int newwidth, unsigned int newheight);
1309   bool set_fonts ();
1310   void set_string_property (Atom prop, const char *str, int len = -1);
1311   void set_utf8_property (Atom prop, const char *str, int len = -1);
1312   void set_title (const char *str);
1313   void set_icon_name (const char *str);
1314   void set_window_color (int idx, const char *color);
1315   void set_colorfgbg ();
1316   bool set_color (rxvt_color &color, const char *name);
1317   void alias_color (int dst, int src);
1318   void set_widthheight (unsigned int newwidth, unsigned int newheight);
1319   void get_window_origin (int &x, int &y);
1320   Pixmap get_pixmap_property (int prop_id);
1321
1322   // screen.C
1323
1324   void lalloc (line_t &l) const
1325   {
1326     l.t = (text_t *)talloc->alloc ();
1327     l.r = (rend_t *)ralloc->alloc ();
1328   }
1329
1330 #if 0
1331   void lfree (line_t &l)
1332   {
1333     talloc->free (l.t);
1334     ralloc->free (l.r);
1335   }
1336 #endif
1337
1338   void lresize (line_t &l) const
1339   {
1340     if (!l.t)
1341       return;
1342
1343     l.t = (text_t *)talloc->alloc (l.t, prev_ncol * sizeof (text_t));
1344     l.r = (rend_t *)ralloc->alloc (l.r, prev_ncol * sizeof (rend_t));
1345
1346     l.l = min (l.l, ncol);
1347
1348     if (ncol > prev_ncol)
1349       scr_blank_line (l, prev_ncol, ncol - prev_ncol, DEFAULT_RSTYLE);
1350   }
1351
1352   int fgcolor_of (rend_t r) const NOTHROW
1353   {
1354     int base = GET_BASEFG (r);
1355 #ifndef NO_BRIGHTCOLOR
1356     if (r & RS_Bold
1357 # if ENABLE_STYLES
1358         && option (Opt_intensityStyles)
1359 # endif
1360         && IN_RANGE_INC (base, minCOLOR, minBrightCOLOR))
1361       base += minBrightCOLOR - minCOLOR;
1362 #endif
1363     return base;
1364   }
1365
1366   int bgcolor_of (rend_t r) const NOTHROW
1367   {
1368     int base = GET_BASEBG (r);
1369 #ifndef NO_BRIGHTCOLOR
1370     if (r & RS_Blink
1371 # if ENABLE_STYLES
1372         && option (Opt_intensityStyles)
1373 # endif
1374         && IN_RANGE_INC (base, minCOLOR, minBrightCOLOR))
1375       base += minBrightCOLOR - minCOLOR;
1376 #endif
1377     return base;
1378   }
1379
1380   bool option (uint8_t opt) const NOTHROW
1381   {
1382     if (!opt)
1383       return 0;
1384
1385     --opt;
1386     return options[opt >> 3] & (1 << (opt & 7));
1387   }
1388
1389   void set_option (uint8_t opt, bool set = true) NOTHROW
1390   {
1391     if (!opt)
1392       return;
1393
1394     --opt;
1395     if (set)
1396       options[opt >> 3] |= (1 << (opt & 7));
1397     else
1398       options[opt >> 3] &= ~(1 << (opt & 7));
1399   }
1400
1401   void set_privmode (unsigned bit, int set) NOTHROW
1402   {
1403     if (set)
1404       priv_modes |= bit;
1405     else
1406       priv_modes &= ~bit;
1407   }
1408
1409   // modifies first argument(!)
1410   void paste (char *data, unsigned int len) NOTHROW;
1411   void scr_blank_line (line_t &l, unsigned int col, unsigned int width, rend_t efs) const NOTHROW;
1412   void scr_blank_screen_mem (line_t &l, rend_t efs) const NOTHROW;
1413   void scr_kill_char (line_t &l, int col) const NOTHROW;
1414   int scr_scroll_text (int row1, int row2, int count) NOTHROW;
1415   void scr_reset ();
1416   void scr_release () NOTHROW;
1417   void scr_clear (bool really = false) NOTHROW;
1418   void scr_refresh () NOTHROW;
1419   bool scr_refresh_rend (rend_t mask, rend_t value) NOTHROW;
1420   void scr_erase_screen (int mode) NOTHROW;
1421 #if ENABLE_FRILLS
1422   void scr_erase_savelines () NOTHROW;
1423   void scr_backindex () NOTHROW;
1424   void scr_forwardindex () NOTHROW;
1425 #endif
1426   void scr_touch (bool refresh) NOTHROW;
1427   void scr_expose (int x, int y, int width, int height, bool refresh) NOTHROW;
1428   void scr_recolour () NOTHROW;
1429   void scr_remap_chars () NOTHROW;
1430   void scr_remap_chars (line_t &l) NOTHROW;
1431
1432   enum cursor_mode { SAVE, RESTORE };
1433
1434   void scr_poweron ();
1435   void scr_soft_reset () NOTHROW;
1436   void scr_cursor (cursor_mode mode) NOTHROW;
1437   void scr_do_wrap () NOTHROW;
1438   void scr_swap_screen () NOTHROW;
1439   void scr_change_screen (int scrn);
1440   void scr_color (unsigned int color, int fgbg) NOTHROW;
1441   void scr_rendition (int set, int style) NOTHROW;
1442   void scr_add_lines (const wchar_t *str, int len, int minlines = 0) NOTHROW;
1443   void scr_backspace () NOTHROW;
1444   void scr_tab (int count, bool ht = false) NOTHROW;
1445   void scr_gotorc (int row, int col, int relative) NOTHROW;
1446   void scr_index (enum page_dirn direction) NOTHROW;
1447   void scr_erase_line (int mode) NOTHROW;
1448   void scr_E () NOTHROW;
1449   void scr_insdel_lines (int count, int insdel) NOTHROW;
1450   void scr_insdel_chars (int count, int insdel) NOTHROW;
1451   void scr_scroll_region (int top, int bot) NOTHROW;
1452   void scr_cursor_visible (int mode) NOTHROW;
1453   void scr_autowrap (int mode) NOTHROW;
1454   void scr_relative_origin (int mode) NOTHROW;
1455   void scr_insert_mode (int mode) NOTHROW;
1456   void scr_set_tab (int mode) NOTHROW;
1457   void scr_rvideo_mode (bool on) NOTHROW;
1458   void scr_report_position () NOTHROW;
1459   void set_font_style () NOTHROW;
1460   void scr_charset_choose (int set) NOTHROW;
1461   void scr_charset_set (int set, unsigned int ch) NOTHROW;
1462   void scr_move_to (int y, int len) NOTHROW;
1463   bool scr_page (enum page_dirn direction, int nlines) NOTHROW;
1464   bool scr_changeview (int new_view_start) NOTHROW;
1465   void scr_bell () NOTHROW;
1466   void scr_printscreen (int fullhist) NOTHROW;
1467   void scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle1, rend_t rstyle2) NOTHROW;
1468   void scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle) NOTHROW;
1469   void scr_reverse_selection () NOTHROW;
1470   void scr_dump (int fd) NOTHROW;
1471
1472   void selection_check (int check_more) NOTHROW;
1473   void selection_paste (Window win, Atom prop, bool delete_prop) NOTHROW;
1474   void selection_property (Window win, Atom prop) NOTHROW;
1475   void selection_request (Time tm, int selnum = Sel_Primary) NOTHROW;
1476   int selection_request_other (Atom target, int selnum) NOTHROW;
1477   void selection_clear (bool clipboard = false) NOTHROW;
1478   void clipboard_copy (Time tm);
1479   void selection_make (Time tm, bool clipboard = false);
1480   bool selection_grab (Time tm, bool clipboard = false) NOTHROW;
1481   void selection_start_colrow (int col, int row) NOTHROW;
1482   void selection_delimit_word (enum page_dirn dirn, const row_col_t *mark, row_col_t *ret) NOTHROW;
1483   void selection_extend_colrow (int32_t col, int32_t row, int button3, int buttonpress, int clickchange) NOTHROW;
1484   void selection_remove_trailing_spaces () NOTHROW;
1485   void selection_send (const XSelectionRequestEvent &rq) NOTHROW;
1486   void selection_click (int clicks, int x, int y) NOTHROW;
1487   void selection_extend (int x, int y, int flag) NOTHROW;
1488   void selection_rotate (int x, int y) NOTHROW;
1489
1490   // xdefaults.C
1491   void get_options (int argc, const char *const *argv);
1492   int parse_keysym (const char *str, const char *arg);
1493   const char *x_resource (const char *name);
1494   void extract_resources ();
1495 };
1496
1497 #endif /* _RXVT_H_ */
1498