1 /*
2  * $Id: xvt.h,v 1.163 2003/03/07 01:00:32 gcw Exp $
3  */
4 
5 #ifndef _XVT_H_		/* include once only */
6 #define _XVT_H_
7 
8 #include "xvtlib.h"
9 
10 #include "feature.h"
11 
12 #include <X11/cursorfont.h>
13 #include <X11/keysym.h>
14 #include <X11/keysymdef.h>
15 #include <X11/Xatom.h>
16 
17 /*
18  *****************************************************************************
19  * SYSTEM HACKS
20  *****************************************************************************
21  */
22 /* Consistent defines - please report on the necessity
23  * @ Unixware: defines (__svr4__)
24  */
25 #if defined (SVR4) && !defined (__svr4__)
26 # define __svr4__
27 #endif
28 #if defined (sun) && !defined (__sun__)
29 # define __sun__
30 #endif
31 
32 
33 #ifndef HAVE_XPOINTER
34 typedef char   *XPointer;
35 #endif
36 
37 #ifdef HAVE_TERMIOS_H
38 # include <termios.h>
39 typedef struct termios ttymode_t;
40 #else
41 # include <sgtty.h>
42 typedef struct {
43     struct sgttyb   sg;
44     struct tchars   tc;
45     struct ltchars  lc;
46     int             line;
47     int             local;
48 } ttymode_t;
49 #endif
50 
51 #ifdef GREEK_SUPPORT
52 # include "grkelot.h"
53 #endif
54 #ifdef XPM_BACKGROUND
55 # ifdef XPM_INC_X11
56 #  include <X11/xpm.h>
57 # else
58 #  include <xpm.h>
59 # endif
60 #endif
61 
62 #ifndef STDIN_FILENO
63 # define STDIN_FILENO	0
64 # define STDOUT_FILENO	1
65 # define STDERR_FILENO	2
66 #endif
67 
68 #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT)
69 # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_PTMX)
70 #  define NO_SETOWNER_TTYDEV
71 # endif
72 #endif
73 #if defined(__CYGWIN32__) || defined(PTYS_ARE_OPENPTY)
74 # define NO_SETOWNER_TTYDEV
75 #endif
76 
77 /*
78  *****************************************************************************
79  * STRUCTURES AND TYPEDEFS
80  *****************************************************************************
81  */
82 struct xvt_vars;		/* Later REDEFINED and typedef'd to xvt_t */
83 struct xvt_hidden;
84 struct grwin_t;
85 
86 
87 /* Sanitize menubar info */
88 #ifndef MENUBAR
89 # undef MENUBAR_MAX
90 #endif
91 #ifndef MENUBAR_MAX
92 # define MENUBAR_MAX	0
93 #endif
94 
95 /* If we're using either the xvt scrollbar or menu bars, keep the
96  * scrollColor resource.
97  */
98 #if defined(XVT_SCROLLBAR) || defined(MENUBAR)
99 # define KEEP_SCROLLCOLOR 1
100 #else
101 # undef KEEP_SCROLLCOLOR
102 #endif
103 
104 #ifdef XPM_BACKGROUND
105 typedef struct {
106     short           w, h, x, y;
107     Pixmap          pixmap;
108 } bgPixmap_t;
109 #endif
110 
111 /*
112  * the 'essential' information for reporting Mouse Events
113  * pared down from XButtonEvent
114  */
115 struct mouse_event {
116     int             clicks;
117     Time            time;	/* milliseconds */
118     unsigned int    state;	/* key or button mask */
119     unsigned int    button;	/* detail */
120 };
121 
122 #ifndef min
123 # define min(a,b)	(((a) < (b)) ? (a) : (b))
124 # define max(a,b)	(((a) > (b)) ? (a) : (b))
125 #endif
126 
127 #define MAX_IT(current, other)	if ((other) > (current)) (current) = (other)
128 #define MIN_IT(current, other)	if ((other) < (current)) (current) = (other)
129 #define SWAP_IT(one, two, typeof)					\
130     do {								\
131 	typeof          swapittmp;					\
132 	(swapittmp) = (one); (one) = (two); (two) = (swapittmp);	\
133     } while (/* CONSTCOND */ 0)
134 #define BOUND_POSITIVE_INT16(val)			\
135     (int16_t)((val) <= 0				\
136 	      ? 0					\
137 	      : min((val), (((u_int16_t)-1)>>1)))
138 
139 /*
140  *****************************************************************************
141  * NORMAL DEFINES
142  *****************************************************************************
143  */
144 
145 #if defined (NO_OLD_SELECTION) && defined(NO_NEW_SELECTION)
146 # error if you disable both selection styles, how can you select, silly?
147 #endif
148 
149 #define APL_CLASS	"XTerm"	/* class name */
150 #define APL_SUBCLASS	"xvt"	/* also check resources under this name */
151 #define APL_NAME	"xvt"	/* normal name */
152 
153 /* COLORTERM, TERM environment variables */
154 #define COLORTERMENV	"xvt"
155 #ifdef XPM_BACKGROUND
156 # define COLORTERMENVFULL COLORTERMENV "-xpm"
157 #else
158 # define COLORTERMENVFULL COLORTERMENV
159 #endif
160 #ifndef TERMENV
161 # define TERMENV	"xterm"
162 #endif
163 
164 #if defined (NO_MOUSE_REPORT) && !defined (NO_MOUSE_REPORT_SCROLLBAR)
165 # define NO_MOUSE_REPORT_SCROLLBAR
166 #endif
167 
168 #ifdef NO_RESOURCES
169 # undef USE_XGETDEFAULT
170 #endif
171 
172 /* now look for other badly set stuff */
173 
174 #if !defined (EACCESS) && defined(EAGAIN)
175 # define EACCESS EAGAIN
176 #endif
177 
178 #ifndef EXIT_SUCCESS		/* missing from <stdlib.h> */
179 # define EXIT_SUCCESS		0	/* exit function success */
180 # define EXIT_FAILURE		1	/* exit function failure */
181 #endif
182 
183 #define menuBar_esc		10
184 #define scrollBar_esc		30
185 #define menuBar_margin		2	/* margin below text */
186 
187 #if defined(XVT_SCROLLBAR) || defined(NEXT_SCROLLBAR) || defined(XTERM_SCROLLBAR)
188 # define HAVE_SCROLLBARS
189 #endif
190 
191 /* width of scrollBar, menuBar shadow, must be 1 or 2 */
192 #ifdef HALFSHADOW
193 # define SHADOW 1
194 #else
195 # define SHADOW 2
196 #endif
197 
198 #define R_SB_ALIGN_CENTRE	0
199 #define R_SB_ALIGN_TOP		1
200 #define R_SB_ALIGN_BOTTOM	2
201 
202 #define R_SB_XVT		0
203 #define R_SB_NEXT		1
204 #define R_SB_XTERM		2
205 
206 #define SB_WIDTH_NEXT		19
207 #define SB_WIDTH_XTERM		15
208 #ifndef SB_WIDTH_XVT
209 # define SB_WIDTH_XVT		10
210 #endif
211 
212 /*
213  * NeXT scrollbar defines
214  */
215 #define SB_PADDING		1
216 #define SB_BORDER_WIDTH		1
217 #define SB_BEVEL_WIDTH_UPPER_LEFT	1
218 #define SB_BEVEL_WIDTH_LOWER_RIGHT	2
219 #define SB_LEFT_PADDING		(SB_PADDING + SB_BORDER_WIDTH)
220 #define SB_MARGIN_SPACE		(SB_PADDING * 2)
221 #define SB_BUTTON_WIDTH		(SB_WIDTH_NEXT - SB_MARGIN_SPACE - SB_BORDER_WIDTH)
222 #define SB_BUTTON_HEIGHT	(SB_BUTTON_WIDTH)
223 #define SB_BUTTON_SINGLE_HEIGHT	(SB_BUTTON_HEIGHT + SB_PADDING)
224 #define SB_BUTTON_BOTH_HEIGHT	(SB_BUTTON_SINGLE_HEIGHT * 2)
225 #define SB_BUTTON_TOTAL_HEIGHT	(SB_BUTTON_BOTH_HEIGHT + SB_PADDING)
226 #define SB_BUTTON_BEVEL_X	(SB_LEFT_PADDING)
227 #define SB_BUTTON_FACE_X	(SB_BUTTON_BEVEL_X + SB_BEVEL_WIDTH_UPPER_LEFT)
228 #define SB_THUMB_MIN_HEIGHT	(SB_BUTTON_WIDTH - (SB_PADDING * 2))
229  /*
230   *    +-------------+
231   *    |             | <---< SB_PADDING
232   *    | ::::::::::: |
233   *    | ::::::::::: |
234   *   '''''''''''''''''
235   *   ,,,,,,,,,,,,,,,,,
236   *    | ::::::::::: |
237   *    | ::::::::::: |
238   *    |  +---------------< SB_BEVEL_WIDTH_UPPER_LEFT
239   *    |  | :::::::: |
240   *    |  V :::: vv-------< SB_BEVEL_WIDTH_LOWER_RIGHT
241   *    | +---------+ |
242   *    | | ......%%| |
243   *    | | ......%%| |
244   *    | | ..()..%%| |
245   *    | | ......%%| |
246   *    | | %%%%%%%%| |
247   *    | +---------+ | <.........................
248   *    |             | <---< SB_PADDING         :
249   *    | +---------+ | <-+..........            :---< SB_BUTTON_TOTAL_HEIGHT
250   *    | | ......%%| |   |         :            :
251   *    | | ../\..%%| |   |---< SB_BUTTON_HEIGHT :
252   *    | | %%%%%%%%| |   |         :            :
253   *    | +---------+ | <-+         :            :
254   *    |             |             :            :
255   *    | +---------+ | <-+         :---< SB_BUTTON_BOTH_HEIGHT
256   *    | | ......%%| |   |         :            :
257   *    | | ..\/..%%| |   |         :            :
258   *    | | %%%%%%%%| |   |---< SB_BUTTON_SINGLE_HEIGHT
259   *    | +---------+ |   |         :            :
260   *    |             |   |         :            :
261   *    +-------------+ <-+.........:............:
262   *    ^^|_________| :
263   *    ||     |      :
264   *    ||     +---< SB_BUTTON_WIDTH
265   *    ||            :
266   *    |+------< SB_PADDING
267   *    |:            :
268   *    +----< SB_BORDER_WIDTH
269   *     :            :
270   *     :............:
271   *           |
272   *           +---< SB_WIDTH_NEXT
273   */
274 
275 #define NO_REFRESH		0	/* Window not visible at all!        */
276 #define FAST_REFRESH		(1<<0)	/* Fully exposed window              */
277 #define SLOW_REFRESH		(1<<1)	/* Partially exposed window          */
278 #define SMOOTH_REFRESH		(1<<2)	/* Do sync'ing to make it smooth     */
279 #define REFRESH_BOUNDS		(1<<3)
280 
281 #ifdef NO_SECONDARY_SCREEN
282 # define NSCREENS		0
283 #else
284 # define NSCREENS		1
285 #endif
286 
287 #define IGNORE			0
288 #define SAVE			's'
289 #define RESTORE			'r'
290 
291 /* special (internal) prefix for font commands */
292 #define FONT_CMD		'#'
293 #define FONT_DN			"#-"
294 #define FONT_UP			"#+"
295 
296 /* flags for xvt_scr_gotorc() */
297 #define C_RELATIVE		1	/* col movement is relative */
298 #define R_RELATIVE		2	/* row movement is relative */
299 #define RELATIVE		(R_RELATIVE|C_RELATIVE)
300 
301 /* modes for xvt_scr_insdel_chars(), xvt_scr_insdel_lines() */
302 #define INSERT			-1	/* don't change these values */
303 #define DELETE			+1
304 #define ERASE			+2
305 
306 /* modes for xvt_scr_page() - scroll page. used by scrollbar window */
307 enum page_dirn {
308     UP,
309     DN,
310     NO_DIR
311 };
312 
313 /* arguments for xvt_scr_change_screen() */
314 enum {
315     PRIMARY = 0,
316     SECONDARY
317 };
318 
319 enum {
320     SBYTE = 0,
321     WBYTE
322 };
323 
324 
325 #define RS_None			0	/* Normal */
326 
327 #if defined(TTY_256COLOR)
328 /* have at least 32 bits to use */
329 # define RS_fgMask		0x000001FFu	/* 512 colors */
330 # define RS_bgMask		0x0003FE00u	/* 512 colors */
331 # define RS_Bold		0x00040000u	/* bold */
332 # define RS_Blink		0x00080000u	/* blink */
333 # define RS_RVid		0x00100000u	/* reverse video */
334 # define RS_Uline		0x00200000u	/* underline */
335 # define RS_acsFont		0x00400000u	/* ACS graphics char set */
336 # define RS_ukFont		0x00800000u	/* UK character set */
337 #else
338 /* may only have 16 bits to use so squash them in */
339 # define RS_fgMask		0x0000001Fu	/* 32 colors */
340 # define RS_bgMask		0x000003E0u	/* 32 colors */
341 # define RS_Bold		0x00000400u	/* bold */
342 # define RS_Blink		0x00000800u	/* blink */
343 # define RS_RVid		0x00001000u	/* reverse video */
344 # define RS_Uline		0x00002000u	/* underline */
345 # define RS_acsFont		0x00004000u	/* ACS graphics char set */
346 # define RS_ukFont		0x00008000u	/* UK character set */
347 #endif
348 
349 #ifdef MULTICHAR_SET
350 # define RS_multi0		0x10000000u	/* only multibyte characters */
351 # define RS_multi1		0x20000000u	/* multibyte 1st byte */
352 # define RS_multi2		(RS_multi0|RS_multi1)	/* multibyte 2nd byte */
353 # define RS_multiMask		(RS_multi0|RS_multi1)	/* multibyte mask */
354 # define IS_MULTI1(r)		(((r) & RS_multiMask) == RS_multi1)
355 # define IS_MULTI2(r)		(((r) & RS_multiMask) == RS_multi2)
356 #else
357 # define RS_multiMask		0
358 # define IS_MULTI1(r)		(0)
359 # define IS_MULTI2(r)		(0)
360 #endif
361 
362 #define RS_fontMask		(RS_acsFont|RS_ukFont)
363 #define RS_baseattrMask		(RS_Bold|RS_Blink|RS_RVid|RS_Uline)
364 #define RS_attrMask		(RS_baseattrMask|RS_fontMask|RS_multiMask)
365 
366 #define Sel_none		0	/* Not waiting */
367 #define Sel_normal		0x01	/* normal selection */
368 #define Sel_incr		0x02	/* incremental selection */
369 #define Sel_direct		0x00
370 #define Sel_Primary		0x01
371 #define Sel_Secondary		0x02
372 #define Sel_Clipboard		0x03
373 #define Sel_whereMask		0x0f
374 #define Sel_CompoundText	0x10	/* last request was Compound */
375 
376 enum {
377     C0_NUL = 0x00,
378             C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
379     C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
380     C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
381     C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1
382 };
383 #define CHAR_ST			0x9c	/* 0234 */
384 
385 /*
386  * XTerm Operating System Commands: ESC ] Ps;Pt (ST|BEL)
387  * colour extensions by Christian W. Zuckschwerdt <zany@triq.net>
388  */
389 #define XTerm_name		0
390 #define XTerm_iconName		1
391 #define XTerm_title		2
392 #define XTerm_Color		4	/* change colors */
393 #define XTerm_Color_cursor	12	/* change actual 'Cursor' color */
394 #define XTerm_Color_pointer	13	/* change actual 'Pointer' color */
395 #define XTerm_Color_RV		17	/* change actual 'Highlight' color */
396 #define XTerm_Color_BD		18	/* change actual 'Bold' color */
397 #define XTerm_Color_UL		19	/* change actual 'Underline' color */
398 #define XTerm_logfile		46	/* not implemented */
399 #define XTerm_font		50
400 
401 /*
402  * xvt extensions of XTerm OSCs: ESC ] Ps;Pt (ST|BEL)
403  */
404 #define XTerm_Menu		10	/* set menu item */
405 #define XTerm_Pixmap		20	/* new bg pixmap */
406 #define XTerm_restoreFG		39	/* change default fg color */
407 #define XTerm_restoreBG		49	/* change default bg color */
408 #define XTerm_dumpscreen	55	/* dump scrollback and all of screen */
409 
410 /* Words starting with `Color_' are colours.  Others are counts */
411 /*
412  * The following comment is mostly obsolete since pixcolor_set was expanded:
413  * We're currently upto 29 colours.  Only 3 more available.  The
414  * PixColor and rendition colour usage should probably be decoupled
415  * on the unnecessary items, e.g. Color_pointer, but won't bother
416  * until we need to.  Also, be aware of usage in pixcolor_set
417  */
418 
419 enum colour_list {
420     Color_fg = 0,
421     Color_bg,
422     minCOLOR,			/* 2 */
423     Color_Black = minCOLOR,
424     Color_Red3,
425     Color_Green3,
426     Color_Yellow3,
427     Color_Blue3,
428     Color_Magenta3,
429     Color_Cyan3,
430     maxCOLOR,			/* minCOLOR + 7 */
431 #ifndef NO_BRIGHTCOLOR
432     Color_AntiqueWhite = maxCOLOR,
433     minBrightCOLOR,		/* maxCOLOR + 1 */
434     Color_Grey25 = minBrightCOLOR,
435     Color_Red,
436     Color_Green,
437     Color_Yellow,
438     Color_Blue,
439     Color_Magenta,
440     Color_Cyan,
441     maxBrightCOLOR,		/* minBrightCOLOR + 7 */
442     Color_White = maxBrightCOLOR,
443 #else
444     Color_White = maxCOLOR,
445 #endif
446 #ifdef TTY_256COLOR
447     min256COLOR = Color_White + 1,
448     max256COLOR = minCOLOR + 255,
449 #endif
450 #ifndef NO_CURSORCOLOR
451     Color_cursor,
452     Color_cursor2,
453 #endif
454     Color_pointer,
455     Color_border,
456 #ifndef NO_BOLD_UNDERLINE_REVERSE
457     Color_BD,
458     Color_UL,
459     Color_RV,
460 #endif
461 #ifdef OPTION_HC
462     Color_HC,
463 #endif
464 #ifdef KEEP_SCROLLCOLOR
465     Color_scroll,
466     Color_trough,
467 #endif
468     NRS_COLORS,			/* */
469 #ifdef KEEP_SCROLLCOLOR
470     Color_topShadow = NRS_COLORS,
471     Color_bottomShadow,
472     TOTAL_COLORS		/* upto 30 */
473 #else
474     TOTAL_COLORS = NRS_COLORS	/* */
475 #endif
476 };
477 
478 #ifdef TTY_256COLOR
479 # define Color_Bits	9
480 # define NPIXCLR_SETS	9	/* (256 + 14) bits / 32 bits */
481 #else
482 # define Color_Bits	5
483 # define NPIXCLR_SETS	1	/* (16 + 14) bits / 32 bits */
484 #endif
485 #define NPIXCLR_BITS	32
486 
487 #define DEFAULT_RSTYLE		(RS_None | (Color_fg) | (Color_bg<<Color_Bits))
488 
489 /*
490  * Resource list
491  */
492 enum {
493     Rs_display_name = 0,
494     Rs_term_name,
495     Rs_iconName,
496     Rs_geometry,
497     Rs_reverseVideo,
498     Rs_color,
499     _Rs_color = Rs_color + NRS_COLORS - 1,
500     Rs_font,
501     _Rs_font = Rs_font + MAX_NFONTS - 1,
502 #ifdef MULTICHAR_SET
503     Rs_mfont,
504     _Rs_mfont = Rs_mfont + MAX_NFONTS - 1,
505     Rs_multichar_encoding,
506     Rs_mc_hack,
507 #endif
508     Rs_name,
509     Rs_title,
510 #if defined (XPM_BACKGROUND) || (MENUBAR_MAX)
511     Rs_path,
512 #endif
513 #ifdef XPM_BACKGROUND
514     Rs_backgroundPixmap,
515 #endif
516 #if (MENUBAR_MAX)
517     Rs_menu,
518 #endif
519 #ifndef NO_BOLDFONT
520     Rs_boldFont,
521 #endif
522 #ifdef GREEK_SUPPORT
523     Rs_greek_keyboard,
524     Rs_greektoggle_key,
525 #endif
526     Rs_loginShell,
527     Rs_jumpScroll,
528 #ifdef HAVE_SCROLLBARS
529     Rs_scrollBar,
530     Rs_scrollBar_right,
531     Rs_scrollBar_floating,
532     Rs_scrollBar_align,
533 #endif
534     Rs_scrollstyle,	/* Rs_scrollBar_style */
535     Rs_scrollTtyOutput,
536     Rs_scrollTtyKeypress,
537     Rs_scrollWithBuffer,
538     Rs_saveLines,
539     Rs_utmpInhibit,
540     Rs_visualBell,
541 #if ! defined(NO_MAPALERT) && defined(MAPALERT_OPTION)
542     Rs_mapAlert,
543 #endif
544 #ifdef META8_OPTION
545     Rs_meta8,
546 #endif
547 #ifdef MOUSE_WHEEL
548     Rs_mouseWheelScrollPage,
549 #endif
550 #ifndef NO_BACKSPACE_KEY
551     Rs_backspace_key,
552 #endif
553 #ifndef NO_DELETE_KEY
554     Rs_delete_key,
555 #endif
556     Rs_selectstyle,
557 #ifdef PRINTPIPE
558     Rs_print_pipe,
559 #endif
560 #ifdef USE_XIM
561     Rs_preeditType,
562     Rs_inputMethod,
563 #endif
564 #if defined (HOTKEY_CTRL) || defined (HOTKEY_META)
565     Rs_bigfont_key,
566     Rs_smallfont_key,
567 #endif
568 #ifdef TRANSPARENT
569     Rs_transparent,
570     Rs_transparent_all,
571 #endif
572 #ifndef NO_FRILLS
573     Rs_ext_bwidth,
574     Rs_int_bwidth,
575 #endif
576     Rs_scrollBar_thickness,
577 #ifndef NO_LINESPACE
578     Rs_lineSpace,
579 #endif
580     Rs_cutchars,
581     Rs_modifier,
582     Rs_answerbackstring,
583     Rs_tripleclickwords,
584     NUM_RESOURCES
585 } ;
586 
587 enum {
588     TIMEOUT_INCR = 0,
589     NUM_TIMEOUTS
590 } ;
591 
592 enum {
593     XA_COMPOUND_TEXT = 0,
594     XA_MULTIPLE,
595     XA_TARGETS,
596     XA_TEXT,
597     XA_TIMESTAMP,
598     XA_VT_SELECTION,
599     XA_INCR,
600     XA_WMDELETEWINDOW,
601 #ifdef TRANSPARENT
602     XA_XROOTPMAPID,
603 #endif
604 #ifdef OFFIX_DND		/* OffiX Dnd (drag 'n' drop) support */
605     XA_DNDPROTOCOL,
606     XA_DNDSELECTION,
607 #endif				/* OFFIX_DND */
608     XA_CLIPBOARD,
609     NUM_XA
610 } ;
611 
612 /*
613  * number of graphics points
614  * divisible by 2 (num lines)
615  * divisible by 4 (num rect)
616  */
617 #define	NGRX_PTS	1000
618 
619 /* DEC private modes */
620 #define PrivMode_132		(1LU<<0)
621 #define PrivMode_132OK		(1LU<<1)
622 #define PrivMode_rVideo		(1LU<<2)
623 #define PrivMode_relOrigin	(1LU<<3)
624 #define PrivMode_Screen		(1LU<<4)
625 #define PrivMode_Autowrap	(1LU<<5)
626 #define PrivMode_aplCUR		(1LU<<6)
627 #define PrivMode_aplKP		(1LU<<7)
628 #define PrivMode_HaveBackSpace	(1LU<<8)
629 #define PrivMode_BackSpace	(1LU<<9)
630 #define PrivMode_ShiftKeys	(1LU<<10)
631 #define PrivMode_VisibleCursor	(1LU<<11)
632 #define PrivMode_MouseX10	(1LU<<12)
633 #define PrivMode_MouseX11	(1LU<<13)
634 #define PrivMode_scrollBar	(1LU<<14)
635 #define PrivMode_menuBar	(1LU<<15)
636 #define PrivMode_TtyOutputInh	(1LU<<16)
637 #define PrivMode_Keypress	(1LU<<17)
638 #define PrivMode_smoothScroll	(1LU<<18)
639 #define PrivMode_vt52		(1LU<<19)
640 /* too annoying to implement X11 highlight tracking */
641 /* #define PrivMode_MouseX11Track       (1LU<<18) */
642 
643 #define PrivMode_mouse_report	(PrivMode_MouseX10|PrivMode_MouseX11)
644 #define PrivMode(test,bit)		\
645     if (test)				\
646 	r->h->PrivateModes |= (bit);	\
647     else				\
648 	r->h->PrivateModes &= ~(bit)
649 
650 #ifdef ALLOW_132_MODE
651 # define PrivMode_Default						 \
652 (PrivMode_Autowrap|PrivMode_aplKP|PrivMode_ShiftKeys|PrivMode_VisibleCursor|PrivMode_132OK)
653 #else
654 # define PrivMode_Default						 \
655 (PrivMode_Autowrap|PrivMode_aplKP|PrivMode_ShiftKeys|PrivMode_VisibleCursor)
656 #endif
657 
658 #ifdef PREFER_24BIT
659 # define XDEPTH			r->Xdepth
660 # define XCMAP			r->Xcmap
661 # define XVISUAL		r->h->Xvisual
662 #else
663 # ifdef DEBUG_DEPTH
664 #  define XDEPTH		DEBUG_DEPTH
665 # else
666 #  define XDEPTH		DefaultDepth(r->Xdisplay,Xscreen)
667 #  define XCMAP			DefaultColormap(r->Xdisplay,Xscreen)
668 #  define XVISUAL		DefaultVisual(r->Xdisplay,Xscreen)
669 # endif
670 #endif
671 #define IMBUFSIZ		128	/* input modifier buffer sizes */
672 #ifndef BUFSIZ
673 # define BUFSIZ			4096
674 #endif
675 #define KBUFSZ			512	/* size of keyboard mapping buffer */
676 
677 /*
678  *****************************************************************************
679  * MACRO DEFINES
680  *****************************************************************************
681  */
682 #define MEMSET(x, y, z)		memset((x), (y), (size_t)(z))
683 #define MEMCPY(x, y, z)		memcpy((void *)(x), (const void *)(y), (z))
684 #define MEMMOVE(x, y, z)	memmove((void *)(x), (const void *)(y), (z))
685 #define STRCASECMP(x, y)	strcasecmp((x), (y))
686 #define STRNCASECMP(x, y, z)	strncasecmp((x), (y), (z))
687 #define STRCPY(x, y)		strcpy((char *)(x), (const char *)(y))
688 #define STRNCPY(x, y, z)	strncpy((char *)(x), (const char *)(y), (z))
689 #define STRCMP(x, y)		strcmp((const char *)(x), (const char *)(y))
690 #define STRNCMP(x, y, z)	strncmp((const char *)(x), (const char *)(y), (z))
691 #define STRCAT(x, y)		strcat((char *)(x), (const char *)(y))
692 #define STRNCAT(x, y, z)	strncat((char *)(x), (const char *)(y), (z))
693 #define STRDUP(x)		strdup((const char *)(x))
694 #define STRLEN(x)		strlen((const char *)(x))
695 #define STRCHR(x, y)		strchr((const char *)(x), (int)(y))
696 #define STRRCHR(x, y)		strrchr((const char *)(x), (int)(y))
697 
698 /* convert pixel dimensions to row/column values.  Everything as int32_t */
699 #define Pixel2Col(x)		Pixel2Width((int32_t)(x) - (int32_t)r->TermWin.int_bwidth)
700 #define Pixel2Row(y)		Pixel2Height((int32_t)(y) - (int32_t)r->TermWin.int_bwidth)
701 #define Pixel2Width(x)		((int32_t)(x) / (int32_t)r->TermWin.fwidth)
702 #define Pixel2Height(y)		((int32_t)(y) / (int32_t)r->TermWin.fheight)
703 #define Col2Pixel(col)		((int32_t)Width2Pixel(col) + (int32_t)r->TermWin.int_bwidth)
704 #define Row2Pixel(row)		((int32_t)Height2Pixel(row) + (int32_t)r->TermWin.int_bwidth)
705 #define Width2Pixel(n)		((int32_t)(n) * (int32_t)r->TermWin.fwidth)
706 #define Height2Pixel(n)		((int32_t)(n) * (int32_t)r->TermWin.fheight)
707 
708 #define TermWin_TotalWidth()	((int32_t)r->TermWin.width  + 2 * (int32_t)r->TermWin.int_bwidth)
709 #define TermWin_TotalHeight()	((int32_t)r->TermWin.height + 2 * (int32_t)r->TermWin.int_bwidth)
710 
711 #define Xscreen			DefaultScreen(r->Xdisplay)
712 #define Xroot			DefaultRootWindow(r->Xdisplay)
713 
714 /* how to build & extract colors and attributes */
715 #define GET_BASEFG(x)		(((x) & RS_fgMask))
716 #define GET_BASEBG(x)		(((x) & RS_bgMask)>>Color_Bits)
717 #ifndef NO_BRIGHTCOLOR
718 # define GET_FGCOLOR(x)						\
719     ((((x) & RS_Bold) == 0					\
720       || GET_BASEFG(x) < minCOLOR				\
721       || GET_BASEFG(x) >= minBrightCOLOR)			\
722      ? GET_BASEFG(x)						\
723      : (GET_BASEFG(x) + (minBrightCOLOR - minCOLOR)))
724 # define GET_BGCOLOR(x)						\
725     ((((x) & RS_Blink) == 0					\
726       || GET_BASEBG(x) < minCOLOR				\
727       || GET_BASEBG(x) >= minBrightCOLOR)			\
728      ? GET_BASEBG(x)						\
729      : (GET_BASEBG(x) + (minBrightCOLOR - minCOLOR)))
730 #else
731 # define GET_FGCOLOR(x)		GET_BASEFG(x)
732 # define GET_BGCOLOR(x)		GET_BASEBG(x)
733 #endif
734 
735 #define GET_ATTR(x)		(((x) & RS_attrMask))
736 #define GET_BGATTR(x)							\
737     (((x) & RS_RVid) ? (((x) & (RS_attrMask & ~RS_RVid))		\
738 			| (((x) & RS_fgMask)<<Color_Bits))		\
739 		     : ((x) & (RS_attrMask | RS_bgMask)))
740 #define SET_FGCOLOR(x,fg)	(((x) & ~RS_fgMask)  | (fg))
741 #define SET_BGCOLOR(x,bg)	(((x) & ~RS_bgMask)  | ((bg)<<Color_Bits))
742 #define SET_ATTR(x,a)		(((x) & ~RS_attrMask)| (a))
743 
744 #define SET_PIXCOLOR(h, x)	((h)->pixcolor_set[(x) / NPIXCLR_BITS] |= (1 << ((x) % NPIXCLR_BITS)))
745 #define ISSET_PIXCOLOR(h, x)	((h)->pixcolor_set[(x) / NPIXCLR_BITS] & (1 << ((x) % NPIXCLR_BITS)))
746 
747 #ifdef HAVE_SCROLLBARS
748 # define scrollbar_TotalWidth()	(r->scrollBar.width + r->sb_shadow * 2)
749 #else
750 # define scrollbar_TotalWidth()	(0)
751 #endif
752 #define scrollbar_isMotion()	(r->scrollBar.state == 'm')
753 #define scrollbar_isUp()	(r->scrollBar.state == 'U')
754 #define scrollbar_isDn()	(r->scrollBar.state == 'D')
755 #define scrollbar_isUpDn()	isupper (r->scrollBar.state)
756 #define isScrollbarWindow(w)	(r->scrollBar.state && (w) == r->scrollBar.win)
757 
758 #define scrollbar_setIdle()	r->scrollBar.state = 1
759 #define scrollbar_setMotion()	r->scrollBar.state = 'm'
760 #define scrollbar_setUp()	r->scrollBar.state = 'U'
761 #define scrollbar_setDn()	r->scrollBar.state = 'D'
762 
763 #define scrollbarnext_dnval()	(r->scrollBar.end + (r->scrollBar.width + 1))
764 #define scrollbarnext_upButton(y)	((y) > r->scrollBar.end \
765 					 && (y) <= scrollbarnext_dnval())
766 #define scrollbarnext_dnButton(y)	((y) > scrollbarnext_dnval())
767 #define SCROLLNEXT_MINHEIGHT	SB_THUMB_MIN_HEIGHT
768 #define scrollbarxvt_upButton(y)	((y) < r->scrollBar.beg)
769 #define scrollbarxvt_dnButton(y)	((y) > r->scrollBar.end)
770 #define SCROLLXVT_MINHEIGHT	10
771 #define SCROLLXTERM_MINHEIGHT	10
772 
773 #define scrollbar_minheight()	(r->scrollBar.style == R_SB_NEXT	\
774 				 ? SCROLLNEXT_MINHEIGHT			\
775 				 : SCROLLXVT_MINHEIGHT)
776 #define scrollbar_above_slider(y)	((y) < r->scrollBar.top)
777 #define scrollbar_below_slider(y)	((y) > r->scrollBar.bot)
778 #define scrollbar_position(y)		((y) - r->scrollBar.beg)
779 #define scrollbar_size()		(r->scrollBar.end - r->scrollBar.beg \
780 					 - scrollbar_minheight())
781 
782 #if (MENUBAR_MAX > 1)
783 /* rendition style flags */
784 # define menuBar_height()	(r->TermWin.fheight + SHADOW)
785 # define menuBar_TotalHeight()	(menuBar_height() + SHADOW + menuBar_margin)
786 # define isMenuBarWindow(w)	((w) == r->menuBar.win)
787 #else
788 # define menuBar_height()	(0)
789 # define menuBar_TotalHeight()	(0)
790 # define isMenuBarWindow(w)	(0)
791 #endif
792 
793 #ifdef XPM_BACKGROUND
794 # define XPMClearArea(a, b, c, d, e, f, g)	XClearArea((a), (b), (c), (d), (e), (f), (g))
795 #else
796 # define XPMClearArea(a, b, c, d, e, f, g)
797 #endif
798 
799 #ifndef STRICT_FONT_CHECKING
800 # define xvt_get_fontwidest(font)	((font)->max_bounds.width)
801 #endif
802 
803 #define xvt_Gr_ButtonPress(x,y)	xvt_Gr_ButtonReport (r, 'P',(x),(y))
804 #define xvt_Gr_ButtonRelease(x,y)	xvt_Gr_ButtonReport (r, 'R',(x),(y))
805 
806 #ifdef UTMP_SUPPORT
807 # if !defined(HAVE_STRUCT_UTMPX)
808 #  undef HAVE_UTMPX_H
809 #  undef HAVE_STRUCT_UTMPX
810 # endif
811 # if !defined(XVT_UTMP_FILE) || !defined(HAVE_STRUCT_UTMP)
812 #  undef HAVE_UTMP_H
813 #  undef HAVE_STRUCT_UTMP
814 # endif
815 
816 # ifdef HAVE_UTMPX_H
817 #  include <utmpx.h>
818 # endif
819 # ifdef HAVE_UTMP_H
820 #  include <utmp.h>
821 # endif
822 #endif
823 
824 #ifdef DEBUG_CMD
825 # define D_CMD(x)		fprintf x ; fputc('\n', stderr)
826 #else
827 # define D_CMD(x)
828 #endif
829 #ifdef DEBUG_INIT
830 # define D_INIT(x)		fprintf x ; fputc('\n', stderr)
831 #else
832 # define D_INIT(x)
833 #endif
834 #ifdef DEBUG_MAIN
835 # define D_MAIN(x)		fprintf x ; fputc('\n', stderr)
836 #else
837 # define D_MAIN(x)
838 #endif
839 #ifdef DEBUG_SCREEN
840 # define D_SCREEN(x)		fprintf x ; fputc('\n', stderr)
841 #else
842 # define D_SCREEN(x)
843 #endif
844 #ifdef DEBUG_SELECT
845 # define D_SELECT(x)		fprintf x ; fputc('\n', stderr)
846 #else
847 # define D_SELECT(x)
848 #endif
849 #ifdef DEBUG_X
850 # define D_X(x)			fprintf x ; fputc('\n', stderr)
851 #else
852 # define D_X(x)
853 #endif
854 
855 /*
856  *****************************************************************************
857  * VARIABLES
858  *****************************************************************************
859  */
860 #ifdef MENUBAR
861 # include "menubar.h"
862 #endif
863 
864 struct xvt_hidden {
865 #ifdef __GNUC__
866     unsigned char   want_refresh:1,
867                     want_full_refresh:1,
868                     am_transparent:1,
869                     am_pixmap_trans:1,
870                     current_screen:1,
871                     hate_those_clicks:1,
872                     num_scr_allow:1,
873                     bypass_keystate:1;
874     unsigned char   chstat:1,
875                     lost_multi:1,
876                     multi_byte:1,
877                     parsed_geometry:1;
878 #else
879     unsigned char   want_refresh,
880 # ifdef TRANSPARENT
881                     want_full_refresh,	/* awaiting full screen refresh      */
882 # endif
883 # if defined(XPM_BACKGROUND) || defined(TRANSPARENT)
884                     am_transparent,	/* is a transparent term             */
885                     am_pixmap_trans,	/* transparency w/known root pixmap  */
886 # endif
887                     current_screen,	/* primary or secondary              */
888                     hate_those_clicks,	/* a.k.a. keep mark position         */
889                     num_scr_allow,
890                     bypass_keystate,
891 # ifdef MULTICHAR_SET
892                     chstat,
893                     lost_multi,	/* set ==> we only got half a glyph */
894                     multi_byte,	/* set ==> currently using 2 bytes per glyph */
895 # endif
896                     parsed_geometry;
897 #endif	/* !__GNUC__ */
898 
899     unsigned char   refresh_type,
900 #ifdef UTMP_SUPPORT
901                     next_utmp_action,
902 #endif
903 #ifndef NO_SETOWNER_TTYDEV
904                     next_tty_action,
905 #endif
906 #ifdef META8_OPTION
907                     meta_char,	/* Alt-key prefix                            */
908 #endif
909 		    scrollbar_align,
910                     selection_wait,
911                     selection_type;
912 /* ---------- */
913 #ifdef GREEK_SUPPORT
914     short           greek_mode;		/* greek keyboard mode               */
915 #endif
916     short           rvideo;
917     int16_t         num_scr;	/* screen: number lines scrolled             */
918     u_int16_t       prev_ncol,	/* screen: previous number of columns        */
919                     prev_nrow;	/* screen: previous number of rows           */
920 #ifdef XVT_GRAPHICS
921     u_int16_t       gr_prev_start;
922 #endif
923 /* ---------- */
924     rend_t          rstyle;
925 /* ---------- */
926     u_int32_t       pixcolor_set[NPIXCLR_SETS];
927 /* ---------- */
928     int             csrO,	/* Hops - csr offset in thumb/slider to      */
929 				/*   give proper Scroll behaviour            */
930 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
931                     scroll_arrow_delay,
932 #endif
933 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
934 		    mouse_slip_wheel_delay,
935 		    mouse_slip_wheel_speed,
936 #endif
937                     refresh_count,
938                     refresh_limit,
939                     fnum,	/* logical font number                       */
940                     last_bot,	/* scrollbar last bottom position            */
941                     last_top,	/* scrollbar last top position               */
942                     last_state,	/* scrollbar last state                      */
943                     scrollbar_len,
944                     currmaxcol,
945 #ifdef MENUBAR
946                     menu_readonly,	/* okay to alter menu? */
947                     Arrows_x,
948 #endif
949 #if (MENUBAR_MAX > 1)
950                     Nbars,
951 #endif
952                     window_vt_x,
953                     window_vt_y,
954                     window_sb_x,
955                     allowedxerror;
956 /* ---------- */
957     unsigned int    ModMetaMask,
958                     ModNumLockMask,
959                     old_width,	/* last used width in screen resize          */
960                     old_height,	/* last used height in screen resize         */
961 #ifndef NO_BRIGHTCOLOR
962                     colorfgbg,
963 #endif
964                     ttymode;
965     unsigned long   PrivateModes,
966                     SavedModes;
967 /* ---------- */
968 #ifdef PREFER_24BIT
969     Visual         *Xvisual;
970 #endif
971 /* ---------- */
972     Atom            xa[NUM_XA];
973 /* ---------- */
974 #ifdef MENUBAR
975     GC              menubarGC;
976 #endif
977 #if defined(MENUBAR) || defined(XVT_SCROLLBAR)
978     GC              scrollbarGC,
979                     topShadowGC,
980                     botShadowGC;
981 #endif
982 #ifdef XTERM_SCROLLBAR
983     GC              xscrollbarGC,
984                     ShadowGC;
985 #endif
986 #ifdef NEXT_SCROLLBAR
987     GC              blackGC,
988                     whiteGC,
989                     grayGC,
990                     darkGC,
991                     stippleGC;
992     Pixmap          dimple,
993                     upArrow,
994                     downArrow,
995                     upArrowHi,
996                     downArrowHi;
997 #endif
998 /* ---------- */
999     Time            selection_time;
1000     Time            selection_request_time;
1001     pid_t           cmd_pid;	/* process id of child */
1002     gid_t           ttygid;
1003 #if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
1004     uid_t           euid;
1005     gid_t           egid;
1006 #endif
1007 /* ---------- */
1008     Cursor          cursor_leftptr;
1009 /* ---------- */
1010     const char     *ttydev;	/* pty/tty name */
1011 #ifndef NO_BACKSPACE_KEY
1012     const char     *key_backspace;
1013 #endif
1014 #ifndef NO_DELETE_KEY
1015     const char     *key_delete;
1016 #endif
1017 #if defined (HOTKEY_CTRL) || defined (HOTKEY_META)
1018     KeySym          ks_bigfont, ks_smallfont;
1019 #endif
1020 #ifdef GREEK_SUPPORT
1021     KeySym          ks_greekmodeswith;
1022 #endif
1023 #ifdef USE_XIM
1024     XIC             Input_Context;
1025     XIMStyle        input_style;
1026     int             event_type;
1027 #endif
1028     struct mouse_event MEvent;
1029     XComposeStatus  compose;
1030 #ifdef XVT_GRAPHICS
1031     int             graphics_up;
1032     struct grwin_t *gr_root;
1033 #endif
1034     ttymode_t       tio;
1035 #ifdef UTMP_SUPPORT
1036 # ifdef HAVE_STRUCT_UTMP
1037     struct utmp     ut;
1038 # endif
1039 # ifdef HAVE_STRUCT_UTMPX
1040     struct utmpx    utx;
1041 # endif
1042 # if (defined(HAVE_STRUCT_UTMP) && defined(HAVE_UTMP_PID)) || defined(HAVE_STRUCT_UTMPX)
1043     char            ut_id[5];
1044 # endif
1045     int             utmp_pos;
1046 #endif
1047     row_col_t       oldcursor;
1048 #ifdef XPM_BACKGROUND
1049     bgPixmap_t      bgPixmap;
1050     XpmAttributes   xpmAttr;	/* originally loaded pixmap and its scaling */
1051 #endif
1052 #ifdef MULTICHAR_SET
1053     int             oldcursormulti;
1054     void            (*multichar_decode)(unsigned char *str, int len);
1055 #endif
1056 #ifndef RESET_TTY_TO_COMMON_DEFAULTS
1057     struct stat     ttyfd_stat;	/* original status of our tty */
1058 #endif
1059 #ifdef MENUBAR
1060     menu_t         *ActiveMenu,		/* currently active menu */
1061                    *BuildMenu;		/* the menu currently being built */
1062     bar_t          *CurrentBar;
1063 # if !(MENUBAR_MAX > 1)
1064     bar_t           BarList;
1065 # endif				/* (MENUBAR_MAX > 1) */
1066 #endif
1067 #ifdef XVT_GRAPHICS
1068     Window          gr_last_id;
1069 #endif
1070     struct timeval  timeout[NUM_TIMEOUTS];
1071 
1072 /* these three don't need to be kept but do so to placate some mem checkers */
1073     char           *env_windowid;	/* environmental variable WINDOWID */
1074     char	   *env_display;	/* environmental variable DISPLAY  */
1075     char	   *env_term;		/* environmental variable TERM     */
1076     char           *env_colorfgbg;
1077     char           *buffer;
1078     char           *locale;
1079     char            charsets[4];
1080     unsigned char  *v_buffer;	/* pointer to physical buffer */
1081     unsigned char  *v_bufstr;	/* beginning of area to write */
1082     unsigned char  *v_bufptr;	/* end of area to write */
1083     unsigned char  *v_bufend;	/* end of physical buffer */
1084     char           *newfont[MAX_NFONTS];
1085 #ifdef KEYSYM_RESOURCE
1086     const unsigned char *Keysym_map[256];
1087 #endif
1088     const char     *rs[NUM_RESOURCES];
1089 /* command input buffering */
1090     unsigned char  *cmdbuf_ptr, *cmdbuf_endp;
1091     unsigned char   cmdbuf_base[BUFSIZ];
1092     unsigned char   kbuf[KBUFSZ];
1093 };
1094 
1095 #ifndef __attribute__
1096 # ifdef __GNUC__
1097 #  if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2)
1098 #   define __attribute__(x)
1099 #  endif
1100 # endif
1101 # define __attribute__(x)
1102 #endif
1103 
1104 /*
1105  *****************************************************************************
1106  * PROTOTYPES
1107  *****************************************************************************
1108  */
1109 #ifdef PROTOTYPES
1110 # define __PROTO(p)	p
1111 #else
1112 # define __PROTO(p)	()
1113 #endif
1114 #include "protos.h"
1115 
1116 #ifndef XVT_GRAPHICS		/* sync functions with graphics.extpro */
1117 # define xvt_Gr_ButtonReport(r, but, x, y)
1118 # define xvt_Gr_do_graphics(r, cmd, nargs, args, text)
1119 # define xvt_Gr_scroll(r, count)
1120 # define xvt_Gr_ClearScreen(r)
1121 # define xvt_Gr_ChangeScreen(r)
1122 # define xvt_Gr_expose(r, win)
1123 # define xvt_Gr_Resize(r, w, h)
1124 # define xvt_Gr_reset(r)
1125 # define xvt_Gr_Displayed(r)		(0)
1126 #endif
1127 
1128 #ifndef MENUBAR			/* sync functions with menubar.extpro */
1129 # define xvt_menubar_read(r, filename)
1130 # define xvt_menubar_dispatch(r, str)
1131 # define xvt_menubar_expose(r)
1132 # define xvt_menubar_mapping(r, map)	(0)
1133 # define xvt_menubar_control(r, ev)
1134 # define xvt_map_menuBar(r, map)
1135 # define xvt_create_menuBar(r, cursor)
1136 # define xvt_Resize_menuBar(r, x, y, width, height)
1137 #endif
1138 
1139 #ifndef XPM_BACKGROUND		/* sync functions with xpm.extpro */
1140 # define xvt_scale_pixmap(r, geom)	(0)
1141 # define xvt_resize_pixmap(r)
1142 # define xvt_set_bgPixmap(r, file)	(0)
1143 #endif
1144 
1145 #ifndef GREEK_SUPPORT		/* sync functions with grkelot.extpro */
1146 # define greek_init()
1147 # define greek_end()
1148 # define greek_reset()
1149 # define greek_setmode(greek_mode)
1150 # define greek_getmode()	(0)
1151 # define greek_xlat(s, num_chars)	(0)
1152 #endif
1153 
1154 #ifndef USE_XIM
1155 # define xvt_setTermFontSet(r, idx)	(0)
1156 #endif
1157 
1158 #ifndef UTMP_SUPPORT
1159 # define xvt_privileged_utmp(r, action)	(0)
1160 #endif
1161 
1162 #ifdef NO_SETOWNER_TTYDEV
1163 # define xvt_privileged_ttydev(r, action)	(0)
1164 #endif
1165 
1166 #ifndef XTERM_COLOR_CHANGE
1167 # define xvt_set_window_color(r, idx, color)	(0)
1168 #endif
1169 
1170 #ifdef __CYGWIN32__
1171 # define xvt_privileged_ttydev(r, action)	(0)
1172 #endif
1173 
1174 #ifdef DEBUG_malloc
1175 # include "dmalloc.h"		/* This comes last */
1176 #endif
1177 
1178 #endif				/* _XVT_H_ */
1179