charproc.c revision d522f475
1d522f475Smrg/* $XTermId: charproc.c,v 1.842 2008/04/20 22:08:42 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
4d522f475Smrg
5d522f475SmrgCopyright 1999-2007,2008 by Thomas E. Dickey
6d522f475Smrg
7d522f475Smrg                        All Rights Reserved
8d522f475Smrg
9d522f475SmrgPermission is hereby granted, free of charge, to any person obtaining a
10d522f475Smrgcopy of this software and associated documentation files (the
11d522f475Smrg"Software"), to deal in the Software without restriction, including
12d522f475Smrgwithout limitation the rights to use, copy, modify, merge, publish,
13d522f475Smrgdistribute, sublicense, and/or sell copies of the Software, and to
14d522f475Smrgpermit persons to whom the Software is furnished to do so, subject to
15d522f475Smrgthe following conditions:
16d522f475Smrg
17d522f475SmrgThe above copyright notice and this permission notice shall be included
18d522f475Smrgin all copies or substantial portions of the Software.
19d522f475Smrg
20d522f475SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21d522f475SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22d522f475SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23d522f475SmrgIN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
24d522f475SmrgCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25d522f475SmrgTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26d522f475SmrgSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27d522f475Smrg
28d522f475SmrgExcept as contained in this notice, the name(s) of the above copyright
29d522f475Smrgholders shall not be used in advertising or otherwise to promote the
30d522f475Smrgsale, use or other dealings in this Software without prior written
31d522f475Smrgauthorization.
32d522f475Smrg
33d522f475SmrgCopyright 1988  The Open Group
34d522f475Smrg
35d522f475SmrgPermission to use, copy, modify, distribute, and sell this software and its
36d522f475Smrgdocumentation for any purpose is hereby granted without fee, provided that
37d522f475Smrgthe above copyright notice appear in all copies and that both that
38d522f475Smrgcopyright notice and this permission notice appear in supporting
39d522f475Smrgdocumentation.
40d522f475Smrg
41d522f475SmrgThe above copyright notice and this permission notice shall be included in
42d522f475Smrgall copies or substantial portions of the Software.
43d522f475Smrg
44d522f475SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45d522f475SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46d522f475SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
47d522f475SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
48d522f475SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
49d522f475SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50d522f475Smrg
51d522f475SmrgExcept as contained in this notice, the name of The Open Group shall not be
52d522f475Smrgused in advertising or otherwise to promote the sale, use or other dealings
53d522f475Smrgin this Software without prior written authorization from The Open Group.
54d522f475Smrg
55d522f475Smrg*/
56d522f475Smrg/*
57d522f475Smrg * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
58d522f475Smrg *
59d522f475Smrg *                         All Rights Reserved
60d522f475Smrg *
61d522f475Smrg * Permission to use, copy, modify, and distribute this software and its
62d522f475Smrg * documentation for any purpose and without fee is hereby granted,
63d522f475Smrg * provided that the above copyright notice appear in all copies and that
64d522f475Smrg * both that copyright notice and this permission notice appear in
65d522f475Smrg * supporting documentation, and that the name of Digital Equipment
66d522f475Smrg * Corporation not be used in advertising or publicity pertaining to
67d522f475Smrg * distribution of the software without specific, written prior permission.
68d522f475Smrg *
69d522f475Smrg *
70d522f475Smrg * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
71d522f475Smrg * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
72d522f475Smrg * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
73d522f475Smrg * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
74d522f475Smrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
75d522f475Smrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
76d522f475Smrg * SOFTWARE.
77d522f475Smrg */
78d522f475Smrg
79d522f475Smrg/* charproc.c */
80d522f475Smrg
81d522f475Smrg#include <version.h>
82d522f475Smrg#include <xterm.h>
83d522f475Smrg
84d522f475Smrg#include <X11/Xatom.h>
85d522f475Smrg#include <X11/Xutil.h>
86d522f475Smrg#include <X11/cursorfont.h>
87d522f475Smrg#include <X11/Xmu/Atoms.h>
88d522f475Smrg#include <X11/Xmu/CharSet.h>
89d522f475Smrg#include <X11/Xmu/Converters.h>
90d522f475Smrg
91d522f475Smrg#if OPT_INPUT_METHOD
92d522f475Smrg
93d522f475Smrg#if defined(HAVE_LIB_XAW)
94d522f475Smrg#include <X11/Xaw/XawImP.h>
95d522f475Smrg#elif defined(HAVE_LIB_XAW3D)
96d522f475Smrg#include <X11/Xaw3d/XawImP.h>
97d522f475Smrg#elif defined(HAVE_LIB_NEXTAW)
98d522f475Smrg#include <X11/neXtaw/XawImP.h>
99d522f475Smrg#elif defined(HAVE_LIB_XAWPLUS)
100d522f475Smrg#include <X11/XawPlus/XawImP.h>
101d522f475Smrg#endif
102d522f475Smrg
103d522f475Smrg#endif
104d522f475Smrg
105d522f475Smrg#if OPT_WIDE_CHARS
106d522f475Smrg#include <wcwidth.h>
107d522f475Smrg#include <precompose.h>
108d522f475Smrg#ifdef HAVE_LANGINFO_CODESET
109d522f475Smrg#include <langinfo.h>
110d522f475Smrg#endif
111d522f475Smrg#endif
112d522f475Smrg
113d522f475Smrg#if OPT_INPUT_METHOD
114d522f475Smrg#include <X11/Xlocale.h>
115d522f475Smrg#endif
116d522f475Smrg
117d522f475Smrg#include <stdio.h>
118d522f475Smrg#include <ctype.h>
119d522f475Smrg
120d522f475Smrg#if defined(HAVE_SCHED_YIELD)
121d522f475Smrg#include <sched.h>
122d522f475Smrg#endif
123d522f475Smrg
124d522f475Smrg#include <VTparse.h>
125d522f475Smrg#include <data.h>
126d522f475Smrg#include <error.h>
127d522f475Smrg#include <menu.h>
128d522f475Smrg#include <main.h>
129d522f475Smrg#include <fontutils.h>
130d522f475Smrg#include <xcharmouse.h>
131d522f475Smrg#include <charclass.h>
132d522f475Smrg#include <xstrings.h>
133d522f475Smrg
134d522f475Smrgstatic IChar doinput(void);
135d522f475Smrgstatic int set_character_class(char *s);
136d522f475Smrgstatic void FromAlternate(XtermWidget /* xw */ );
137d522f475Smrgstatic void RequestResize(XtermWidget termw, int rows, int cols, Bool text);
138d522f475Smrgstatic void SwitchBufs(XtermWidget xw);
139d522f475Smrgstatic void ToAlternate(XtermWidget /* xw */ );
140d522f475Smrgstatic void VTallocbuf(void);
141d522f475Smrgstatic void ansi_modes(XtermWidget termw,
142d522f475Smrg		       void (*func) (unsigned *p, unsigned mask));
143d522f475Smrgstatic void bitclr(unsigned *p, unsigned mask);
144d522f475Smrgstatic void bitcpy(unsigned *p, unsigned q, unsigned mask);
145d522f475Smrgstatic void bitset(unsigned *p, unsigned mask);
146d522f475Smrgstatic void dpmodes(XtermWidget termw, void (*func) (unsigned *p, unsigned mask));
147d522f475Smrgstatic void restoremodes(XtermWidget termw);
148d522f475Smrgstatic void savemodes(XtermWidget termw);
149d522f475Smrgstatic void window_ops(XtermWidget termw);
150d522f475Smrg
151d522f475Smrg#define DoStartBlinking(s) ((s)->cursor_blink ^ (s)->cursor_blink_esc)
152d522f475Smrg
153d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
154d522f475Smrgstatic void HandleBlinking(XtPointer closure, XtIntervalId * id);
155d522f475Smrgstatic void StartBlinking(TScreen * screen);
156d522f475Smrgstatic void StopBlinking(TScreen * screen);
157d522f475Smrg#else
158d522f475Smrg#define StartBlinking(screen)	/* nothing */
159d522f475Smrg#define StopBlinking(screen)	/* nothing */
160d522f475Smrg#endif
161d522f475Smrg
162d522f475Smrg#if OPT_INPUT_METHOD
163d522f475Smrgstatic void PreeditPosition(TScreen * screen);
164d522f475Smrg#endif
165d522f475Smrg
166d522f475Smrg#define	DEFAULT		-1
167d522f475Smrg#define BELLSUPPRESSMSEC 200
168d522f475Smrg
169d522f475Smrgstatic int nparam;
170d522f475Smrgstatic ANSI reply;
171d522f475Smrgstatic int param[NPARAM];
172d522f475Smrg
173d522f475Smrgstatic jmp_buf vtjmpbuf;
174d522f475Smrg
175d522f475Smrg/* event handlers */
176d522f475Smrgstatic void HandleBell PROTO_XT_ACTIONS_ARGS;
177d522f475Smrgstatic void HandleIgnore PROTO_XT_ACTIONS_ARGS;
178d522f475Smrgstatic void HandleKeymapChange PROTO_XT_ACTIONS_ARGS;
179d522f475Smrgstatic void HandleVisualBell PROTO_XT_ACTIONS_ARGS;
180d522f475Smrg#if HANDLE_STRUCT_NOTIFY
181d522f475Smrgstatic void HandleStructNotify PROTO_XT_EV_HANDLER_ARGS;
182d522f475Smrg#endif
183d522f475Smrg
184d522f475Smrg/*
185d522f475Smrg * NOTE: VTInitialize zeros out the entire ".screen" component of the
186d522f475Smrg * XtermWidget, so make sure to add an assignment statement in VTInitialize()
187d522f475Smrg * for each new ".screen" field added to this resource list.
188d522f475Smrg */
189d522f475Smrg
190d522f475Smrg/* Defaults */
191d522f475Smrg#if OPT_ISO_COLORS
192d522f475Smrg
193d522f475Smrg/*
194d522f475Smrg * If we default to colorMode enabled, compile-in defaults for the ANSI colors.
195d522f475Smrg */
196d522f475Smrg#if DFT_COLORMODE
197d522f475Smrg#define DFT_COLOR(name) name
198d522f475Smrg#else
199d522f475Smrg#define DFT_COLOR(name) XtDefaultForeground
200d522f475Smrg#endif
201d522f475Smrg#endif
202d522f475Smrg
203d522f475Smrgstatic char *_Font_Selected_ = "yes";	/* string is arbitrary */
204d522f475Smrg
205d522f475Smrgstatic char defaultTranslations[] =
206d522f475Smrg"\
207d522f475Smrg          Shift <KeyPress> Prior:scroll-back(1,halfpage) \n\
208d522f475Smrg           Shift <KeyPress> Next:scroll-forw(1,halfpage) \n\
209d522f475Smrg         Shift <KeyPress> Select:select-cursor-start() select-cursor-end(SELECT, CUT_BUFFER0) \n\
210d522f475Smrg         Shift <KeyPress> Insert:insert-selection(SELECT, CUT_BUFFER0) \n\
211d522f475Smrg"
212d522f475Smrg#if OPT_SHIFT_FONTS
213d522f475Smrg"\
214d522f475Smrg    Shift~Ctrl <KeyPress> KP_Add:larger-vt-font() \n\
215d522f475Smrg    Shift Ctrl <KeyPress> KP_Add:smaller-vt-font() \n\
216d522f475Smrg    Shift <KeyPress> KP_Subtract:smaller-vt-font() \n\
217d522f475Smrg"
218d522f475Smrg#endif
219d522f475Smrg"\
220d522f475Smrg                ~Meta <KeyPress>:insert-seven-bit() \n\
221d522f475Smrg                 Meta <KeyPress>:insert-eight-bit() \n\
222d522f475Smrg                !Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
223d522f475Smrg           !Lock Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
224d522f475Smrg !Lock Ctrl @Num_Lock <Btn1Down>:popup-menu(mainMenu) \n\
225d522f475Smrg     ! @Num_Lock Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
226d522f475Smrg                ~Meta <Btn1Down>:select-start() \n\
227d522f475Smrg              ~Meta <Btn1Motion>:select-extend() \n\
228d522f475Smrg                !Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
229d522f475Smrg           !Lock Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
230d522f475Smrg !Lock Ctrl @Num_Lock <Btn2Down>:popup-menu(vtMenu) \n\
231d522f475Smrg     ! @Num_Lock Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
232d522f475Smrg          ~Ctrl ~Meta <Btn2Down>:ignore() \n\
233d522f475Smrg                 Meta <Btn2Down>:clear-saved-lines() \n\
234d522f475Smrg            ~Ctrl ~Meta <Btn2Up>:insert-selection(SELECT, CUT_BUFFER0) \n\
235d522f475Smrg                !Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
236d522f475Smrg           !Lock Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
237d522f475Smrg !Lock Ctrl @Num_Lock <Btn3Down>:popup-menu(fontMenu) \n\
238d522f475Smrg     ! @Num_Lock Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
239d522f475Smrg          ~Ctrl ~Meta <Btn3Down>:start-extend() \n\
240d522f475Smrg              ~Meta <Btn3Motion>:select-extend() \n\
241d522f475Smrg                 Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
242d522f475Smrg            Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
243d522f475Smrg  Lock @Num_Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
244d522f475Smrg       @Num_Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
245d522f475Smrg                      <Btn4Down>:scroll-back(5,line,m)     \n\
246d522f475Smrg                 Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
247d522f475Smrg            Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
248d522f475Smrg  Lock @Num_Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
249d522f475Smrg       @Num_Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
250d522f475Smrg                      <Btn5Down>:scroll-forw(5,line,m)     \n\
251d522f475Smrg                         <BtnUp>:select-end(SELECT, CUT_BUFFER0) \n\
252d522f475Smrg                       <BtnDown>:ignore() \
253d522f475Smrg";				/* PROCURA added "Meta <Btn2Down>:clear-saved-lines()" */
254d522f475Smrg/* *INDENT-OFF* */
255d522f475Smrgstatic XtActionsRec actionsList[] = {
256d522f475Smrg    { "allow-send-events",	HandleAllowSends },
257d522f475Smrg    { "bell",			HandleBell },
258d522f475Smrg    { "clear-saved-lines",	HandleClearSavedLines },
259d522f475Smrg    { "create-menu",		HandleCreateMenu },
260d522f475Smrg    { "delete-is-del",		HandleDeleteIsDEL },
261d522f475Smrg    { "dired-button",		DiredButton },
262d522f475Smrg    { "hard-reset",		HandleHardReset },
263d522f475Smrg    { "ignore",			HandleIgnore },
264d522f475Smrg    { "insert",			HandleKeyPressed },  /* alias for insert-seven-bit */
265d522f475Smrg    { "insert-eight-bit",	HandleEightBitKeyPressed },
266d522f475Smrg    { "insert-selection",	HandleInsertSelection },
267d522f475Smrg    { "insert-seven-bit",	HandleKeyPressed },
268d522f475Smrg    { "interpret",		HandleInterpret },
269d522f475Smrg    { "keymap",			HandleKeymapChange },
270d522f475Smrg    { "popup-menu",		HandlePopupMenu },
271d522f475Smrg    { "print",			HandlePrintScreen },
272d522f475Smrg    { "print-redir",		HandlePrintControlMode },
273d522f475Smrg    { "quit",			HandleQuit },
274d522f475Smrg    { "redraw",			HandleRedraw },
275d522f475Smrg    { "scroll-back",		HandleScrollBack },
276d522f475Smrg    { "scroll-forw",		HandleScrollForward },
277d522f475Smrg    { "secure",			HandleSecure },
278d522f475Smrg    { "select-cursor-end",	HandleKeyboardSelectEnd },
279d522f475Smrg    { "select-cursor-extend",   HandleKeyboardSelectExtend },
280d522f475Smrg    { "select-cursor-start",	HandleKeyboardSelectStart },
281d522f475Smrg    { "select-end",		HandleSelectEnd },
282d522f475Smrg    { "select-extend",		HandleSelectExtend },
283d522f475Smrg    { "select-set",		HandleSelectSet },
284d522f475Smrg    { "select-start",		HandleSelectStart },
285d522f475Smrg    { "send-signal",		HandleSendSignal },
286d522f475Smrg    { "set-8-bit-control",	Handle8BitControl },
287d522f475Smrg    { "set-allow132",		HandleAllow132 },
288d522f475Smrg    { "set-altscreen",		HandleAltScreen },
289d522f475Smrg    { "set-appcursor",		HandleAppCursor },
290d522f475Smrg    { "set-appkeypad",		HandleAppKeypad },
291d522f475Smrg    { "set-autolinefeed",	HandleAutoLineFeed },
292d522f475Smrg    { "set-autowrap",		HandleAutoWrap },
293d522f475Smrg    { "set-backarrow",		HandleBackarrow },
294d522f475Smrg    { "set-bellIsUrgent",	HandleBellIsUrgent },
295d522f475Smrg    { "set-cursesemul",		HandleCursesEmul },
296d522f475Smrg    { "set-jumpscroll",		HandleJumpscroll },
297d522f475Smrg    { "set-keep-selection",	HandleKeepSelection },
298d522f475Smrg    { "set-marginbell",		HandleMarginBell },
299d522f475Smrg    { "set-old-function-keys",	HandleOldFunctionKeys },
300d522f475Smrg    { "set-pop-on-bell",	HandleSetPopOnBell },
301d522f475Smrg    { "set-reverse-video",	HandleReverseVideo },
302d522f475Smrg    { "set-reversewrap",	HandleReverseWrap },
303d522f475Smrg    { "set-scroll-on-key",	HandleScrollKey },
304d522f475Smrg    { "set-scroll-on-tty-output", HandleScrollTtyOutput },
305d522f475Smrg    { "set-scrollbar",		HandleScrollbar },
306d522f475Smrg    { "set-select",		HandleSetSelect },
307d522f475Smrg    { "set-sun-keyboard",	HandleSunKeyboard },
308d522f475Smrg    { "set-titeInhibit",	HandleTiteInhibit },
309d522f475Smrg    { "set-visual-bell",	HandleSetVisualBell },
310d522f475Smrg    { "set-vt-font",		HandleSetFont },
311d522f475Smrg    { "soft-reset",		HandleSoftReset },
312d522f475Smrg    { "start-cursor-extend",	HandleKeyboardStartExtend },
313d522f475Smrg    { "start-extend",		HandleStartExtend },
314d522f475Smrg    { "string",			HandleStringEvent },
315d522f475Smrg    { "vi-button",		ViButton },
316d522f475Smrg    { "visual-bell",		HandleVisualBell },
317d522f475Smrg#ifdef ALLOWLOGGING
318d522f475Smrg    { "set-logging",		HandleLogging },
319d522f475Smrg#endif
320d522f475Smrg#if OPT_BLINK_CURS
321d522f475Smrg    { "set-cursorblink",	HandleCursorBlink },
322d522f475Smrg#endif
323d522f475Smrg#if OPT_BOX_CHARS
324d522f475Smrg    { "set-font-linedrawing",	HandleFontBoxChars },
325d522f475Smrg#endif
326d522f475Smrg#if OPT_DABBREV
327d522f475Smrg    { "dabbrev-expand",		HandleDabbrevExpand },
328d522f475Smrg#endif
329d522f475Smrg#if OPT_DEC_CHRSET
330d522f475Smrg    { "set-font-doublesize",	HandleFontDoublesize },
331d522f475Smrg#endif
332d522f475Smrg#if OPT_DEC_SOFTFONT
333d522f475Smrg    { "set-font-loading",	HandleFontLoading },
334d522f475Smrg#endif
335d522f475Smrg#if OPT_EXEC_XTERM
336d522f475Smrg    { "spawn-new-terminal",	HandleSpawnTerminal },
337d522f475Smrg#endif
338d522f475Smrg#if OPT_HP_FUNC_KEYS
339d522f475Smrg    { "set-hp-function-keys",	HandleHpFunctionKeys },
340d522f475Smrg#endif
341d522f475Smrg#if OPT_LOAD_VTFONTS
342d522f475Smrg    { "load-vt-fonts",		HandleLoadVTFonts },
343d522f475Smrg#endif
344d522f475Smrg#if OPT_MAXIMIZE
345d522f475Smrg    { "deiconify",		HandleDeIconify },
346d522f475Smrg    { "iconify",		HandleIconify },
347d522f475Smrg    { "maximize",		HandleMaximize },
348d522f475Smrg    { "restore",		HandleRestoreSize },
349d522f475Smrg#endif
350d522f475Smrg#if OPT_NUM_LOCK
351d522f475Smrg    { "alt-sends-escape",	HandleAltEsc },
352d522f475Smrg    { "meta-sends-escape",	HandleMetaEsc },
353d522f475Smrg    { "set-num-lock",		HandleNumLock },
354d522f475Smrg#endif
355d522f475Smrg#if OPT_READLINE
356d522f475Smrg    { "readline-button",	ReadLineButton },
357d522f475Smrg#endif
358d522f475Smrg#if OPT_RENDERFONT
359d522f475Smrg    { "set-render-font",	HandleRenderFont },
360d522f475Smrg#endif
361d522f475Smrg#if OPT_SCO_FUNC_KEYS
362d522f475Smrg    { "set-sco-function-keys",	HandleScoFunctionKeys },
363d522f475Smrg#endif
364d522f475Smrg#if OPT_SHIFT_FONTS
365d522f475Smrg    { "larger-vt-font",		HandleLargerFont },
366d522f475Smrg    { "smaller-vt-font",	HandleSmallerFont },
367d522f475Smrg#endif
368d522f475Smrg#if OPT_SUN_FUNC_KEYS
369d522f475Smrg    { "set-sun-function-keys",	HandleSunFunctionKeys },
370d522f475Smrg#endif
371d522f475Smrg#if OPT_TEK4014
372d522f475Smrg    { "set-terminal-type",	HandleSetTerminalType },
373d522f475Smrg    { "set-visibility",		HandleVisibility },
374d522f475Smrg    { "set-tek-text",		HandleSetTekText },
375d522f475Smrg    { "tek-page",		HandleTekPage },
376d522f475Smrg    { "tek-reset",		HandleTekReset },
377d522f475Smrg    { "tek-copy",		HandleTekCopy },
378d522f475Smrg#endif
379d522f475Smrg#if OPT_TOOLBAR
380d522f475Smrg    { "set-toolbar",		HandleToolbar },
381d522f475Smrg#endif
382d522f475Smrg#if OPT_WIDE_CHARS
383d522f475Smrg    { "set-utf8-mode",		HandleUTF8Mode },
384d522f475Smrg    { "set-utf8-title",		HandleUTF8Title },
385d522f475Smrg#endif
386d522f475Smrg};
387d522f475Smrg/* *INDENT-ON* */
388d522f475Smrg
389d522f475Smrgstatic XtResource resources[] =
390d522f475Smrg{
391d522f475Smrg    Bres(XtNallowSendEvents, XtCAllowSendEvents, screen.allowSendEvent0, False),
392d522f475Smrg    Bres(XtNallowTitleOps, XtCAllowTitleOps, screen.allowTitleOp0, True),
393d522f475Smrg    Bres(XtNallowWindowOps, XtCAllowWindowOps, screen.allowWindowOp0, True),
394d522f475Smrg    Bres(XtNaltIsNotMeta, XtCAltIsNotMeta, screen.alt_is_not_meta, False),
395d522f475Smrg    Bres(XtNaltSendsEscape, XtCAltSendsEscape, screen.alt_sends_esc, False),
396d522f475Smrg    Bres(XtNalwaysBoldMode, XtCAlwaysBoldMode, screen.always_bold_mode, False),
397d522f475Smrg    Bres(XtNalwaysHighlight, XtCAlwaysHighlight, screen.always_highlight, False),
398d522f475Smrg    Bres(XtNappcursorDefault, XtCAppcursorDefault, misc.appcursorDefault, False),
399d522f475Smrg    Bres(XtNappkeypadDefault, XtCAppkeypadDefault, misc.appkeypadDefault, False),
400d522f475Smrg    Bres(XtNautoWrap, XtCAutoWrap, misc.autoWrap, True),
401d522f475Smrg    Bres(XtNawaitInput, XtCAwaitInput, screen.awaitInput, False),
402d522f475Smrg    Bres(XtNfreeBoldBox, XtCFreeBoldBox, screen.free_bold_box, False),
403d522f475Smrg    Bres(XtNbackarrowKey, XtCBackarrowKey, screen.backarrow_key, True),
404d522f475Smrg    Bres(XtNbellIsUrgent, XtCBellIsUrgent, screen.bellIsUrgent, False),
405d522f475Smrg    Bres(XtNbellOnReset, XtCBellOnReset, screen.bellOnReset, True),
406d522f475Smrg    Bres(XtNboldMode, XtCBoldMode, screen.bold_mode, True),
407d522f475Smrg    Bres(XtNbrokenSelections, XtCBrokenSelections, screen.brokenSelections, False),
408d522f475Smrg    Bres(XtNc132, XtCC132, screen.c132, False),
409d522f475Smrg    Bres(XtNcurses, XtCCurses, screen.curses, False),
410d522f475Smrg    Bres(XtNcutNewline, XtCCutNewline, screen.cutNewline, True),
411d522f475Smrg    Bres(XtNcutToBeginningOfLine, XtCCutToBeginningOfLine,
412d522f475Smrg	 screen.cutToBeginningOfLine, True),
413d522f475Smrg    Bres(XtNdeleteIsDEL, XtCDeleteIsDEL, screen.delete_is_del, DEFDELETE_DEL),
414d522f475Smrg    Bres(XtNdynamicColors, XtCDynamicColors, misc.dynamicColors, True),
415d522f475Smrg    Bres(XtNeightBitControl, XtCEightBitControl, screen.control_eight_bits, False),
416d522f475Smrg    Bres(XtNeightBitInput, XtCEightBitInput, screen.input_eight_bits, True),
417d522f475Smrg    Bres(XtNeightBitOutput, XtCEightBitOutput, screen.output_eight_bits, True),
418d522f475Smrg    Bres(XtNhighlightSelection, XtCHighlightSelection,
419d522f475Smrg	 screen.highlight_selection, False),
420d522f475Smrg    Bres(XtNhpLowerleftBugCompat, XtCHpLowerleftBugCompat, screen.hp_ll_bc, False),
421d522f475Smrg    Bres(XtNi18nSelections, XtCI18nSelections, screen.i18nSelections, True),
422d522f475Smrg    Bres(XtNjumpScroll, XtCJumpScroll, screen.jumpscroll, True),
423d522f475Smrg    Bres(XtNkeepSelection, XtCKeepSelection, screen.keepSelection, False),
424d522f475Smrg    Bres(XtNloginShell, XtCLoginShell, misc.login_shell, False),
425d522f475Smrg    Bres(XtNmarginBell, XtCMarginBell, screen.marginbell, False),
426d522f475Smrg    Bres(XtNmetaSendsEscape, XtCMetaSendsEscape, screen.meta_sends_esc, False),
427d522f475Smrg    Bres(XtNmultiScroll, XtCMultiScroll, screen.multiscroll, False),
428d522f475Smrg    Bres(XtNoldXtermFKeys, XtCOldXtermFKeys, screen.old_fkeys, False),
429d522f475Smrg    Bres(XtNpopOnBell, XtCPopOnBell, screen.poponbell, False),
430d522f475Smrg    Bres(XtNprinterAutoClose, XtCPrinterAutoClose, screen.printer_autoclose, False),
431d522f475Smrg    Bres(XtNprinterExtent, XtCPrinterExtent, screen.printer_extent, False),
432d522f475Smrg    Bres(XtNprinterFormFeed, XtCPrinterFormFeed, screen.printer_formfeed, False),
433d522f475Smrg    Bres(XtNquietGrab, XtCQuietGrab, screen.quiet_grab, False),
434d522f475Smrg    Bres(XtNreverseVideo, XtCReverseVideo, misc.re_verse, False),
435d522f475Smrg    Bres(XtNreverseWrap, XtCReverseWrap, misc.reverseWrap, False),
436d522f475Smrg    Bres(XtNscrollBar, XtCScrollBar, misc.scrollbar, False),
437d522f475Smrg    Bres(XtNscrollKey, XtCScrollCond, screen.scrollkey, False),
438d522f475Smrg    Bres(XtNscrollTtyOutput, XtCScrollCond, screen.scrollttyoutput, True),
439d522f475Smrg    Bres(XtNselectToClipboard, XtCSelectToClipboard,
440d522f475Smrg	 screen.selectToClipboard, False),
441d522f475Smrg    Bres(XtNsignalInhibit, XtCSignalInhibit, misc.signalInhibit, False),
442d522f475Smrg    Bres(XtNtiteInhibit, XtCTiteInhibit, misc.titeInhibit, False),
443d522f475Smrg    Bres(XtNtiXtraScroll, XtCTiXtraScroll, misc.tiXtraScroll, False),
444d522f475Smrg    Bres(XtNtrimSelection, XtCTrimSelection, screen.trim_selection, False),
445d522f475Smrg    Bres(XtNunderLine, XtCUnderLine, screen.underline, True),
446d522f475Smrg    Bres(XtNvisualBell, XtCVisualBell, screen.visualbell, False),
447d522f475Smrg
448d522f475Smrg    Ires(XtNbellSuppressTime, XtCBellSuppressTime, screen.bellSuppressTime, BELLSUPPRESSMSEC),
449d522f475Smrg    Ires(XtNinternalBorder, XtCBorderWidth, screen.border, DEFBORDER),
450d522f475Smrg    Ires(XtNlimitResize, XtCLimitResize, misc.limit_resize, 1),
451d522f475Smrg    Ires(XtNmultiClickTime, XtCMultiClickTime, screen.multiClickTime, MULTICLICKTIME),
452d522f475Smrg    Ires(XtNnMarginBell, XtCColumn, screen.nmarginbell, N_MARGINBELL),
453d522f475Smrg    Ires(XtNpointerMode, XtCPointerMode, screen.pointer_mode, DEF_POINTER_MODE),
454d522f475Smrg    Ires(XtNprinterControlMode, XtCPrinterControlMode,
455d522f475Smrg	 screen.printer_controlmode, 0),
456d522f475Smrg    Ires(XtNvisualBellDelay, XtCVisualBellDelay, screen.visualBellDelay, 100),
457d522f475Smrg    Ires(XtNsaveLines, XtCSaveLines, screen.savelines, SAVELINES),
458d522f475Smrg    Ires(XtNscrollBarBorder, XtCScrollBarBorder, screen.scrollBarBorder, 1),
459d522f475Smrg    Ires(XtNscrollLines, XtCScrollLines, screen.scrolllines, SCROLLLINES),
460d522f475Smrg
461d522f475Smrg    Sres(XtNinitialFont, XtCInitialFont, screen.initial_font, NULL),
462d522f475Smrg    Sres(XtNfont1, XtCFont1, screen.MenuFontName(fontMenu_font1), NULL),
463d522f475Smrg    Sres(XtNfont2, XtCFont2, screen.MenuFontName(fontMenu_font2), NULL),
464d522f475Smrg    Sres(XtNfont3, XtCFont3, screen.MenuFontName(fontMenu_font3), NULL),
465d522f475Smrg    Sres(XtNfont4, XtCFont4, screen.MenuFontName(fontMenu_font4), NULL),
466d522f475Smrg    Sres(XtNfont5, XtCFont5, screen.MenuFontName(fontMenu_font5), NULL),
467d522f475Smrg    Sres(XtNfont6, XtCFont6, screen.MenuFontName(fontMenu_font6), NULL),
468d522f475Smrg    Sres(XtNanswerbackString, XtCAnswerbackString, screen.answer_back, ""),
469d522f475Smrg    Sres(XtNboldFont, XtCBoldFont, misc.default_font.f_b, DEFBOLDFONT),
470d522f475Smrg    Sres(XtNcharClass, XtCCharClass, screen.charClass, NULL),
471d522f475Smrg    Sres(XtNdecTerminalID, XtCDecTerminalID, screen.term_id, DFT_DECID),
472d522f475Smrg    Sres(XtNfont, XtCFont, misc.default_font.f_n, DEFFONT),
473d522f475Smrg    Sres(XtNgeometry, XtCGeometry, misc.geo_metry, NULL),
474d522f475Smrg    Sres(XtNkeyboardDialect, XtCKeyboardDialect, screen.keyboard_dialect, DFT_KBD_DIALECT),
475d522f475Smrg    Sres(XtNprinterCommand, XtCPrinterCommand, screen.printer_command, ""),
476d522f475Smrg    Sres(XtNtekGeometry, XtCGeometry, misc.T_geometry, NULL),
477d522f475Smrg
478d522f475Smrg    Tres(XtNcursorColor, XtCCursorColor, TEXT_CURSOR, XtDefaultForeground),
479d522f475Smrg    Tres(XtNforeground, XtCForeground, TEXT_FG, XtDefaultForeground),
480d522f475Smrg    Tres(XtNpointerColor, XtCPointerColor, MOUSE_FG, XtDefaultForeground),
481d522f475Smrg    Tres(XtNbackground, XtCBackground, TEXT_BG, XtDefaultBackground),
482d522f475Smrg    Tres(XtNpointerColorBackground, XtCBackground, MOUSE_BG, XtDefaultBackground),
483d522f475Smrg
484d522f475Smrg    {XtNresizeGravity, XtCResizeGravity, XtRGravity, sizeof(XtGravity),
485d522f475Smrg     XtOffsetOf(XtermWidgetRec, misc.resizeGravity),
486d522f475Smrg     XtRImmediate, (XtPointer) SouthWestGravity},
487d522f475Smrg
488d522f475Smrg    {XtNpointerShape, XtCCursor, XtRCursor, sizeof(Cursor),
489d522f475Smrg     XtOffsetOf(XtermWidgetRec, screen.pointer_cursor),
490d522f475Smrg     XtRString, (XtPointer) "xterm"},
491d522f475Smrg
492d522f475Smrg#ifdef ALLOWLOGGING
493d522f475Smrg    Bres(XtNlogInhibit, XtCLogInhibit, misc.logInhibit, False),
494d522f475Smrg    Bres(XtNlogging, XtCLogging, misc.log_on, False),
495d522f475Smrg    Sres(XtNlogFile, XtCLogfile, screen.logfile, NULL),
496d522f475Smrg#endif
497d522f475Smrg
498d522f475Smrg#ifndef NO_ACTIVE_ICON
499d522f475Smrg    Bres("activeIcon", "ActiveIcon", misc.active_icon, False),
500d522f475Smrg    Ires("iconBorderWidth", XtCBorderWidth, misc.icon_border_width, 2),
501d522f475Smrg    Fres("iconFont", "IconFont", screen.fnt_icon.fs, XtDefaultFont),
502d522f475Smrg    Cres("iconBorderColor", XtCBorderColor, misc.icon_border_pixel, XtDefaultBackground),
503d522f475Smrg#endif				/* NO_ACTIVE_ICON */
504d522f475Smrg
505d522f475Smrg#if OPT_BLINK_CURS
506d522f475Smrg    Bres(XtNcursorBlink, XtCCursorBlink, screen.cursor_blink, False),
507d522f475Smrg#endif
508d522f475Smrg
509d522f475Smrg#if OPT_BLINK_TEXT
510d522f475Smrg    Bres(XtNshowBlinkAsBold, XtCCursorBlink, screen.blink_as_bold, DEFBLINKASBOLD),
511d522f475Smrg#endif
512d522f475Smrg
513d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
514d522f475Smrg    Ires(XtNcursorOnTime, XtCCursorOnTime, screen.blink_on, 600),
515d522f475Smrg    Ires(XtNcursorOffTime, XtCCursorOffTime, screen.blink_off, 300),
516d522f475Smrg#endif
517d522f475Smrg
518d522f475Smrg#if OPT_BOX_CHARS
519d522f475Smrg    Bres(XtNforceBoxChars, XtCForceBoxChars, screen.force_box_chars, False),
520d522f475Smrg    Bres(XtNshowMissingGlyphs, XtCShowMissingGlyphs, screen.force_all_chars, False),
521d522f475Smrg#endif
522d522f475Smrg
523d522f475Smrg#if OPT_BROKEN_OSC
524d522f475Smrg    Bres(XtNbrokenLinuxOSC, XtCBrokenLinuxOSC, screen.brokenLinuxOSC, True),
525d522f475Smrg#endif
526d522f475Smrg
527d522f475Smrg#if OPT_BROKEN_ST
528d522f475Smrg    Bres(XtNbrokenStringTerm, XtCBrokenStringTerm, screen.brokenStringTerm, True),
529d522f475Smrg#endif
530d522f475Smrg
531d522f475Smrg#if OPT_C1_PRINT
532d522f475Smrg    Bres(XtNallowC1Printable, XtCAllowC1Printable, screen.c1_printable, False),
533d522f475Smrg#endif
534d522f475Smrg
535d522f475Smrg#if OPT_CLIP_BOLD
536d522f475Smrg    Bres(XtNuseClipping, XtCUseClipping, screen.use_clipping, True),
537d522f475Smrg#endif
538d522f475Smrg
539d522f475Smrg#if OPT_DEC_CHRSET
540d522f475Smrg    Bres(XtNfontDoublesize, XtCFontDoublesize, screen.font_doublesize, True),
541d522f475Smrg    Ires(XtNcacheDoublesize, XtCCacheDoublesize, screen.cache_doublesize, NUM_CHRSET),
542d522f475Smrg#endif
543d522f475Smrg
544d522f475Smrg#if OPT_HIGHLIGHT_COLOR
545d522f475Smrg    Tres(XtNhighlightColor, XtCHighlightColor, HIGHLIGHT_BG, XtDefaultForeground),
546d522f475Smrg    Tres(XtNhighlightTextColor, XtCHighlightTextColor, HIGHLIGHT_FG, XtDefaultBackground),
547d522f475Smrg    Bres(XtNhighlightReverse, XtCHighlightReverse, screen.hilite_reverse, True),
548d522f475Smrg    Bres(XtNhighlightColorMode, XtCHighlightColorMode, screen.hilite_color, Maybe),
549d522f475Smrg#endif				/* OPT_HIGHLIGHT_COLOR */
550d522f475Smrg
551d522f475Smrg#if OPT_INPUT_METHOD
552d522f475Smrg    Bres(XtNopenIm, XtCOpenIm, misc.open_im, True),
553d522f475Smrg    Sres(XtNinputMethod, XtCInputMethod, misc.input_method, NULL),
554d522f475Smrg    Sres(XtNpreeditType, XtCPreeditType, misc.preedit_type,
555d522f475Smrg	 "OverTheSpot,Root"),
556d522f475Smrg#endif
557d522f475Smrg
558d522f475Smrg#if OPT_ISO_COLORS
559d522f475Smrg    Bres(XtNboldColors, XtCColorMode, screen.boldColors, True),
560d522f475Smrg    Ires(XtNveryBoldColors, XtCVeryBoldColors, screen.veryBoldColors, 0),
561d522f475Smrg    Bres(XtNcolorMode, XtCColorMode, screen.colorMode, DFT_COLORMODE),
562d522f475Smrg
563d522f475Smrg    Bres(XtNcolorAttrMode, XtCColorAttrMode, screen.colorAttrMode, False),
564d522f475Smrg    Bres(XtNcolorBDMode, XtCColorAttrMode, screen.colorBDMode, False),
565d522f475Smrg    Bres(XtNcolorBLMode, XtCColorAttrMode, screen.colorBLMode, False),
566d522f475Smrg    Bres(XtNcolorRVMode, XtCColorAttrMode, screen.colorRVMode, False),
567d522f475Smrg    Bres(XtNcolorULMode, XtCColorAttrMode, screen.colorULMode, False),
568d522f475Smrg    Bres(XtNitalicULMode, XtCColorAttrMode, screen.italicULMode, False),
569d522f475Smrg
570d522f475Smrg    COLOR_RES("0", screen.Acolors[COLOR_0], DFT_COLOR("black")),
571d522f475Smrg    COLOR_RES("1", screen.Acolors[COLOR_1], DFT_COLOR("red3")),
572d522f475Smrg    COLOR_RES("2", screen.Acolors[COLOR_2], DFT_COLOR("green3")),
573d522f475Smrg    COLOR_RES("3", screen.Acolors[COLOR_3], DFT_COLOR("yellow3")),
574d522f475Smrg    COLOR_RES("4", screen.Acolors[COLOR_4], DFT_COLOR(DEF_COLOR4)),
575d522f475Smrg    COLOR_RES("5", screen.Acolors[COLOR_5], DFT_COLOR("magenta3")),
576d522f475Smrg    COLOR_RES("6", screen.Acolors[COLOR_6], DFT_COLOR("cyan3")),
577d522f475Smrg    COLOR_RES("7", screen.Acolors[COLOR_7], DFT_COLOR("gray90")),
578d522f475Smrg    COLOR_RES("8", screen.Acolors[COLOR_8], DFT_COLOR("gray50")),
579d522f475Smrg    COLOR_RES("9", screen.Acolors[COLOR_9], DFT_COLOR("red")),
580d522f475Smrg    COLOR_RES("10", screen.Acolors[COLOR_10], DFT_COLOR("green")),
581d522f475Smrg    COLOR_RES("11", screen.Acolors[COLOR_11], DFT_COLOR("yellow")),
582d522f475Smrg    COLOR_RES("12", screen.Acolors[COLOR_12], DFT_COLOR(DEF_COLOR12)),
583d522f475Smrg    COLOR_RES("13", screen.Acolors[COLOR_13], DFT_COLOR("magenta")),
584d522f475Smrg    COLOR_RES("14", screen.Acolors[COLOR_14], DFT_COLOR("cyan")),
585d522f475Smrg    COLOR_RES("15", screen.Acolors[COLOR_15], DFT_COLOR("white")),
586d522f475Smrg    COLOR_RES("BD", screen.Acolors[COLOR_BD], DFT_COLOR(XtDefaultForeground)),
587d522f475Smrg    COLOR_RES("BL", screen.Acolors[COLOR_BL], DFT_COLOR(XtDefaultForeground)),
588d522f475Smrg    COLOR_RES("UL", screen.Acolors[COLOR_UL], DFT_COLOR(XtDefaultForeground)),
589d522f475Smrg    COLOR_RES("RV", screen.Acolors[COLOR_RV], DFT_COLOR(XtDefaultForeground)),
590d522f475Smrg
591d522f475Smrg    CLICK_RES("2", screen.onClick[1], "word"),
592d522f475Smrg    CLICK_RES("3", screen.onClick[2], "line"),
593d522f475Smrg    CLICK_RES("4", screen.onClick[3], 0),
594d522f475Smrg    CLICK_RES("5", screen.onClick[4], 0),
595d522f475Smrg
596d522f475Smrg#if !OPT_COLOR_RES2
597d522f475Smrg#if OPT_256_COLORS
598d522f475Smrg# include <256colres.h>
599d522f475Smrg#elif OPT_88_COLORS
600d522f475Smrg# include <88colres.h>
601d522f475Smrg#endif
602d522f475Smrg#endif				/* !OPT_COLOR_RES2 */
603d522f475Smrg
604d522f475Smrg#endif				/* OPT_ISO_COLORS */
605d522f475Smrg
606d522f475Smrg#if OPT_MOD_FKEYS
607d522f475Smrg    Ires(XtNmodifyCursorKeys, XtCModifyCursorKeys,
608d522f475Smrg	 keyboard.modify_1st.cursor_keys, 2),
609d522f475Smrg    Ires(XtNmodifyFunctionKeys, XtCModifyFunctionKeys,
610d522f475Smrg	 keyboard.modify_1st.function_keys, 2),
611d522f475Smrg    Ires(XtNmodifyKeypadKeys, XtCModifyKeypadKeys,
612d522f475Smrg	 keyboard.modify_1st.keypad_keys, 0),
613d522f475Smrg    Ires(XtNmodifyOtherKeys, XtCModifyOtherKeys,
614d522f475Smrg	 keyboard.modify_1st.other_keys, 0),
615d522f475Smrg    Ires(XtNmodifyStringKeys, XtCModifyStringKeys,
616d522f475Smrg	 keyboard.modify_1st.string_keys, 0),
617d522f475Smrg    Ires(XtNformatOtherKeys, XtCFormatOtherKeys,
618d522f475Smrg	 keyboard.format_keys, 0),
619d522f475Smrg#endif
620d522f475Smrg
621d522f475Smrg#if OPT_NUM_LOCK
622d522f475Smrg    Bres(XtNalwaysUseMods, XtCAlwaysUseMods, misc.alwaysUseMods, False),
623d522f475Smrg    Bres(XtNnumLock, XtCNumLock, misc.real_NumLock, True),
624d522f475Smrg#endif
625d522f475Smrg
626d522f475Smrg#if OPT_PRINT_COLORS
627d522f475Smrg    Ires(XtNprintAttributes, XtCPrintAttributes, screen.print_attributes, 1),
628d522f475Smrg#endif
629d522f475Smrg
630d522f475Smrg#if OPT_SHIFT_FONTS
631d522f475Smrg    Bres(XtNshiftFonts, XtCShiftFonts, misc.shift_fonts, True),
632d522f475Smrg#endif
633d522f475Smrg
634d522f475Smrg#if OPT_SUNPC_KBD
635d522f475Smrg    Ires(XtNctrlFKeys, XtCCtrlFKeys, misc.ctrl_fkeys, 10),
636d522f475Smrg#endif
637d522f475Smrg
638d522f475Smrg#if OPT_TEK4014
639d522f475Smrg    Bres(XtNtekInhibit, XtCTekInhibit, misc.tekInhibit, False),
640d522f475Smrg    Bres(XtNtekSmall, XtCTekSmall, misc.tekSmall, False),
641d522f475Smrg    Bres(XtNtekStartup, XtCTekStartup, misc.TekEmu, False),
642d522f475Smrg#endif
643d522f475Smrg
644d522f475Smrg#if OPT_TOOLBAR
645d522f475Smrg    Wres(XtNmenuBar, XtCMenuBar, VT100_TB_INFO(menu_bar), 0),
646d522f475Smrg    Ires(XtNmenuHeight, XtCMenuHeight, VT100_TB_INFO(menu_height), 25),
647d522f475Smrg#endif
648d522f475Smrg
649d522f475Smrg#if OPT_WIDE_CHARS
650d522f475Smrg    Bres(XtNcjkWidth, XtCCjkWidth, misc.cjk_width, False),
651d522f475Smrg    Bres(XtNmkWidth, XtCMkWidth, misc.mk_width, False),
652d522f475Smrg    Bres(XtNutf8Latin1, XtCUtf8Latin1, screen.utf8_latin1, False),
653d522f475Smrg    Bres(XtNutf8Title, XtCUtf8Title, screen.utf8_title, False),
654d522f475Smrg    Bres(XtNvt100Graphics, XtCVT100Graphics, screen.vt100_graphics, True),
655d522f475Smrg    Bres(XtNwideChars, XtCWideChars, screen.wide_chars, False),
656d522f475Smrg    Ires(XtNcombiningChars, XtCCombiningChars, screen.max_combining, 2),
657d522f475Smrg    Ires(XtNmkSamplePass, XtCMkSamplePass, misc.mk_samplepass, 256),
658d522f475Smrg    Ires(XtNmkSampleSize, XtCMkSampleSize, misc.mk_samplesize, 1024),
659d522f475Smrg    Ires(XtNutf8, XtCUtf8, screen.utf8_mode, uDefault),
660d522f475Smrg    Sres(XtNwideBoldFont, XtCWideBoldFont, misc.default_font.f_wb, DEFWIDEBOLDFONT),
661d522f475Smrg    Sres(XtNwideFont, XtCWideFont, misc.default_font.f_w, DEFWIDEFONT),
662d522f475Smrg#endif
663d522f475Smrg
664d522f475Smrg#if OPT_LUIT_PROG
665d522f475Smrg    Sres(XtNlocale, XtCLocale, misc.locale_str, "medium"),
666d522f475Smrg    Sres(XtNlocaleFilter, XtCLocaleFilter, misc.localefilter, DEFLOCALEFILTER),
667d522f475Smrg#endif
668d522f475Smrg
669d522f475Smrg#if OPT_INPUT_METHOD
670d522f475Smrg    Sres(XtNximFont, XtCXimFont, misc.f_x, DEFXIMFONT),
671d522f475Smrg#endif
672d522f475Smrg
673d522f475Smrg#if OPT_XMC_GLITCH
674d522f475Smrg    Bres(XtNxmcInline, XtCXmcInline, screen.xmc_inline, False),
675d522f475Smrg    Bres(XtNxmcMoveSGR, XtCXmcMoveSGR, screen.move_sgr_ok, True),
676d522f475Smrg    Ires(XtNxmcAttributes, XtCXmcAttributes, screen.xmc_attributes, 1),
677d522f475Smrg    Ires(XtNxmcGlitch, XtCXmcGlitch, screen.xmc_glitch, 0),
678d522f475Smrg#endif
679d522f475Smrg
680d522f475Smrg#ifdef SCROLLBAR_RIGHT
681d522f475Smrg    Bres(XtNrightScrollBar, XtCRightScrollBar, misc.useRight, False),
682d522f475Smrg#endif
683d522f475Smrg
684d522f475Smrg#if OPT_RENDERFONT
685d522f475Smrg#define RES_FACESIZE(n) Dres(XtNfaceSize #n, XtCFaceSize #n, misc.face_size[n], "0.0")
686d522f475Smrg    RES_FACESIZE(1),
687d522f475Smrg    RES_FACESIZE(2),
688d522f475Smrg    RES_FACESIZE(3),
689d522f475Smrg    RES_FACESIZE(4),
690d522f475Smrg    RES_FACESIZE(5),
691d522f475Smrg    RES_FACESIZE(6),
692d522f475Smrg    Dres(XtNfaceSize, XtCFaceSize, misc.face_size[0], DEFFACESIZE),
693d522f475Smrg    Sres(XtNfaceName, XtCFaceName, misc.face_name, DEFFACENAME),
694d522f475Smrg    Sres(XtNfaceNameDoublesize, XtCFaceNameDoublesize, misc.face_wide_name, DEFFACENAME),
695d522f475Smrg    Bres(XtNrenderFont, XtCRenderFont, misc.render_font, True),
696d522f475Smrg#endif
697d522f475Smrg};
698d522f475Smrg
699d522f475Smrgstatic Boolean VTSetValues(Widget cur, Widget request, Widget new_arg,
700d522f475Smrg			   ArgList args, Cardinal *num_args);
701d522f475Smrgstatic void VTClassInit(void);
702d522f475Smrgstatic void VTDestroy(Widget w);
703d522f475Smrgstatic void VTExpose(Widget w, XEvent * event, Region region);
704d522f475Smrgstatic void VTInitialize(Widget wrequest, Widget new_arg, ArgList args,
705d522f475Smrg			 Cardinal *num_args);
706d522f475Smrgstatic void VTRealize(Widget w, XtValueMask * valuemask,
707d522f475Smrg		      XSetWindowAttributes * values);
708d522f475Smrgstatic void VTResize(Widget w);
709d522f475Smrg
710d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
711d522f475Smrgstatic void VTInitI18N(void);
712d522f475Smrg#endif
713d522f475Smrg
714d522f475Smrg#ifdef VMS
715d522f475Smrgglobaldef {
716d522f475Smrg    "xtermclassrec"
717d522f475Smrg} noshare
718d522f475Smrg
719d522f475Smrg#else
720d522f475Smrgstatic
721d522f475Smrg#endif				/* VMS */
722d522f475SmrgWidgetClassRec xtermClassRec =
723d522f475Smrg{
724d522f475Smrg    {
725d522f475Smrg/* core_class fields */
726d522f475Smrg	(WidgetClass) & widgetClassRec,		/* superclass     */
727d522f475Smrg	"VT100",		/* class_name                   */
728d522f475Smrg	sizeof(XtermWidgetRec),	/* widget_size                  */
729d522f475Smrg	VTClassInit,		/* class_initialize             */
730d522f475Smrg	NULL,			/* class_part_initialize        */
731d522f475Smrg	False,			/* class_inited                 */
732d522f475Smrg	VTInitialize,		/* initialize                   */
733d522f475Smrg	NULL,			/* initialize_hook              */
734d522f475Smrg	VTRealize,		/* realize                      */
735d522f475Smrg	actionsList,		/* actions                      */
736d522f475Smrg	XtNumber(actionsList),	/* num_actions                  */
737d522f475Smrg	resources,		/* resources                    */
738d522f475Smrg	XtNumber(resources),	/* num_resources                */
739d522f475Smrg	NULLQUARK,		/* xrm_class                    */
740d522f475Smrg	True,			/* compress_motion              */
741d522f475Smrg	False,			/* compress_exposure            */
742d522f475Smrg	True,			/* compress_enterleave          */
743d522f475Smrg	False,			/* visible_interest             */
744d522f475Smrg	VTDestroy,		/* destroy                      */
745d522f475Smrg	VTResize,		/* resize                       */
746d522f475Smrg	VTExpose,		/* expose                       */
747d522f475Smrg	VTSetValues,		/* set_values                   */
748d522f475Smrg	NULL,			/* set_values_hook              */
749d522f475Smrg	XtInheritSetValuesAlmost,	/* set_values_almost    */
750d522f475Smrg	NULL,			/* get_values_hook              */
751d522f475Smrg	NULL,			/* accept_focus                 */
752d522f475Smrg	XtVersion,		/* version                      */
753d522f475Smrg	NULL,			/* callback_offsets             */
754d522f475Smrg	defaultTranslations,	/* tm_table                     */
755d522f475Smrg	XtInheritQueryGeometry,	/* query_geometry               */
756d522f475Smrg	XtInheritDisplayAccelerator,	/* display_accelerator  */
757d522f475Smrg	NULL			/* extension                    */
758d522f475Smrg    }
759d522f475Smrg};
760d522f475Smrg
761d522f475Smrg#ifdef VMS
762d522f475Smrgglobaldef {
763d522f475Smrg    "xtermwidgetclass"
764d522f475Smrg}
765d522f475Smrgnoshare
766d522f475Smrg#endif /* VMS */
767d522f475SmrgWidgetClass xtermWidgetClass = (WidgetClass) & xtermClassRec;
768d522f475Smrg
769d522f475Smrg/*
770d522f475Smrg * Add input-actions for widgets that are overlooked (scrollbar and toolbar):
771d522f475Smrg *
772d522f475Smrg *	a) Sometimes the scrollbar passes through translations, sometimes it
773d522f475Smrg *	   doesn't.  We add the KeyPress translations here, just to be sure.
774d522f475Smrg *	b) In the normal (non-toolbar) configuration, the xterm widget covers
775d522f475Smrg *	   almost all of the window.  With a toolbar, there's a relatively
776d522f475Smrg *	   large area that the user would expect to enter keystrokes since the
777d522f475Smrg *	   program can get the focus.
778d522f475Smrg */
779d522f475Smrgvoid
780d522f475SmrgxtermAddInput(Widget w)
781d522f475Smrg{
782d522f475Smrg#if OPT_TOOLBAR
783d522f475Smrg    /* *INDENT-OFF* */
784d522f475Smrg    XtActionsRec input_actions[] = {
785d522f475Smrg	{ "insert",		    HandleKeyPressed }, /* alias */
786d522f475Smrg	{ "insert-eight-bit",	    HandleEightBitKeyPressed },
787d522f475Smrg	{ "insert-seven-bit",	    HandleKeyPressed },
788d522f475Smrg	{ "secure",		    HandleSecure },
789d522f475Smrg	{ "string",		    HandleStringEvent },
790d522f475Smrg	{ "scroll-back",	    HandleScrollBack },
791d522f475Smrg	{ "scroll-forw",	    HandleScrollForward },
792d522f475Smrg	{ "select-cursor-end",	    HandleKeyboardSelectEnd },
793d522f475Smrg	{ "select-cursor-extend",   HandleKeyboardSelectExtend },
794d522f475Smrg	{ "select-cursor-start",    HandleKeyboardSelectStart },
795d522f475Smrg	{ "insert-selection",	    HandleInsertSelection },
796d522f475Smrg	{ "select-start",	    HandleSelectStart },
797d522f475Smrg	{ "select-extend",	    HandleSelectExtend },
798d522f475Smrg	{ "start-extend",	    HandleStartExtend },
799d522f475Smrg	{ "select-end",		    HandleSelectEnd },
800d522f475Smrg	{ "clear-saved-lines",	    HandleClearSavedLines },
801d522f475Smrg	{ "popup-menu",		    HandlePopupMenu },
802d522f475Smrg	{ "bell",		    HandleBell },
803d522f475Smrg	{ "ignore",		    HandleIgnore },
804d522f475Smrg#if OPT_DABBREV
805d522f475Smrg	{ "dabbrev-expand",	    HandleDabbrevExpand },
806d522f475Smrg#endif
807d522f475Smrg#if OPT_SHIFT_FONTS
808d522f475Smrg	{ "larger-vt-font",	    HandleLargerFont },
809d522f475Smrg	{ "smaller-vt-font",	    HandleSmallerFont },
810d522f475Smrg#endif
811d522f475Smrg    };
812d522f475Smrg    /* *INDENT-ON* */
813d522f475Smrg
814d522f475Smrg    XtAppAddActions(app_con, input_actions, XtNumber(input_actions));
815d522f475Smrg#endif
816d522f475Smrg    XtAugmentTranslations(w, XtParseTranslationTable(defaultTranslations));
817d522f475Smrg
818d522f475Smrg#if OPT_EXTRA_PASTE
819d522f475Smrg    if (term && term->keyboard.extra_translations)
820d522f475Smrg	XtOverrideTranslations((Widget) term, XtParseTranslationTable(term->keyboard.extra_translations));
821d522f475Smrg#endif
822d522f475Smrg}
823d522f475Smrg
824d522f475Smrg#if OPT_ISO_COLORS
825d522f475Smrg/*
826d522f475Smrg * The terminal's foreground and background colors are set via two mechanisms:
827d522f475Smrg *	text (cur_foreground, cur_background values that are passed down to
828d522f475Smrg *		XDrawImageString and XDrawString)
829d522f475Smrg *	area (X11 graphics context used in XClearArea and XFillRectangle)
830d522f475Smrg */
831d522f475Smrgvoid
832d522f475SmrgSGR_Foreground(XtermWidget xw, int color)
833d522f475Smrg{
834d522f475Smrg    TScreen *screen = &xw->screen;
835d522f475Smrg    Pixel fg;
836d522f475Smrg
837d522f475Smrg    if (color >= 0) {
838d522f475Smrg	xw->flags |= FG_COLOR;
839d522f475Smrg    } else {
840d522f475Smrg	xw->flags &= ~FG_COLOR;
841d522f475Smrg    }
842d522f475Smrg    fg = getXtermForeground(xw, xw->flags, color);
843d522f475Smrg    xw->cur_foreground = color;
844d522f475Smrg
845d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcNorm, fg);
846d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcNormReverse, fg);
847d522f475Smrg
848d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcBold, fg);
849d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcBoldReverse, fg);
850d522f475Smrg}
851d522f475Smrg
852d522f475Smrgvoid
853d522f475SmrgSGR_Background(XtermWidget xw, int color)
854d522f475Smrg{
855d522f475Smrg    TScreen *screen = &xw->screen;
856d522f475Smrg    Pixel bg;
857d522f475Smrg
858d522f475Smrg    /*
859d522f475Smrg     * An indexing operation may have set screen->scroll_amt, which would
860d522f475Smrg     * normally result in calling FlushScroll() in WriteText().  However,
861d522f475Smrg     * if we're changing the background color now, then the new value
862d522f475Smrg     * should not apply to the pending blank lines.
863d522f475Smrg     */
864d522f475Smrg    if (screen->scroll_amt && (color != xw->cur_background))
865d522f475Smrg	FlushScroll(xw);
866d522f475Smrg
867d522f475Smrg    if (color >= 0) {
868d522f475Smrg	xw->flags |= BG_COLOR;
869d522f475Smrg    } else {
870d522f475Smrg	xw->flags &= ~BG_COLOR;
871d522f475Smrg    }
872d522f475Smrg    bg = getXtermBackground(xw, xw->flags, color);
873d522f475Smrg    xw->cur_background = color;
874d522f475Smrg
875d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcNorm, bg);
876d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcNormReverse, bg);
877d522f475Smrg
878d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcBold, bg);
879d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcBoldReverse, bg);
880d522f475Smrg}
881d522f475Smrg
882d522f475Smrg/* Invoked after updating bold/underline flags, computes the extended color
883d522f475Smrg * index to use for foreground.  (See also 'extract_fg()').
884d522f475Smrg */
885d522f475Smrgstatic void
886d522f475SmrgsetExtendedFG(XtermWidget xw)
887d522f475Smrg{
888d522f475Smrg    int fg = xw->sgr_foreground;
889d522f475Smrg
890d522f475Smrg    if (xw->screen.colorAttrMode
891d522f475Smrg	|| (fg < 0)) {
892d522f475Smrg	if (xw->screen.colorULMode && (xw->flags & UNDERLINE))
893d522f475Smrg	    fg = COLOR_UL;
894d522f475Smrg	if (xw->screen.colorBDMode && (xw->flags & BOLD))
895d522f475Smrg	    fg = COLOR_BD;
896d522f475Smrg	if (xw->screen.colorBLMode && (xw->flags & BLINK))
897d522f475Smrg	    fg = COLOR_BL;
898d522f475Smrg    }
899d522f475Smrg
900d522f475Smrg    /* This implements the IBM PC-style convention of 8-colors, with one
901d522f475Smrg     * bit for bold, thus mapping the 0-7 codes to 8-15.  It won't make
902d522f475Smrg     * much sense for 16-color applications, but we keep it to retain
903d522f475Smrg     * compatiblity with ANSI-color applications.
904d522f475Smrg     */
905d522f475Smrg#if OPT_PC_COLORS		/* XXXJTL should be settable at runtime (resource or OSC?) */
906d522f475Smrg    if (xw->screen.boldColors
907d522f475Smrg	&& (!xw->sgr_extended)
908d522f475Smrg	&& (fg >= 0)
909d522f475Smrg	&& (fg < 8)
910d522f475Smrg	&& (xw->flags & BOLD))
911d522f475Smrg	fg |= 8;
912d522f475Smrg#endif
913d522f475Smrg
914d522f475Smrg    SGR_Foreground(xw, fg);
915d522f475Smrg}
916d522f475Smrg
917d522f475Smrg/* Invoked after updating inverse flag, computes the extended color
918d522f475Smrg * index to use for background.  (See also 'extract_bg()').
919d522f475Smrg */
920d522f475Smrgstatic void
921d522f475SmrgsetExtendedBG(XtermWidget xw)
922d522f475Smrg{
923d522f475Smrg    int bg = xw->sgr_background;
924d522f475Smrg
925d522f475Smrg    if (xw->screen.colorAttrMode
926d522f475Smrg	|| (bg < 0)) {
927d522f475Smrg	if (xw->screen.colorRVMode && (xw->flags & INVERSE))
928d522f475Smrg	    bg = COLOR_RV;
929d522f475Smrg    }
930d522f475Smrg
931d522f475Smrg    SGR_Background(xw, bg);
932d522f475Smrg}
933d522f475Smrg
934d522f475Smrgstatic void
935d522f475Smrgreset_SGR_Foreground(XtermWidget xw)
936d522f475Smrg{
937d522f475Smrg    xw->sgr_foreground = -1;
938d522f475Smrg    xw->sgr_extended = False;
939d522f475Smrg    setExtendedFG(xw);
940d522f475Smrg}
941d522f475Smrg
942d522f475Smrgstatic void
943d522f475Smrgreset_SGR_Background(XtermWidget xw)
944d522f475Smrg{
945d522f475Smrg    xw->sgr_background = -1;
946d522f475Smrg    setExtendedBG(xw);
947d522f475Smrg}
948d522f475Smrg
949d522f475Smrgstatic void
950d522f475Smrgreset_SGR_Colors(XtermWidget xw)
951d522f475Smrg{
952d522f475Smrg    reset_SGR_Foreground(xw);
953d522f475Smrg    reset_SGR_Background(xw);
954d522f475Smrg}
955d522f475Smrg#endif /* OPT_ISO_COLORS */
956d522f475Smrg
957d522f475Smrgvoid
958d522f475SmrgresetCharsets(TScreen * screen)
959d522f475Smrg{
960d522f475Smrg    TRACE(("resetCharsets\n"));
961d522f475Smrg
962d522f475Smrg    screen->gsets[0] = 'B';	/* ASCII_G              */
963d522f475Smrg    screen->gsets[1] = 'B';	/* ASCII_G              */
964d522f475Smrg    screen->gsets[2] = 'B';	/* ASCII_G              */
965d522f475Smrg    screen->gsets[3] = 'B';	/* ASCII_G              */
966d522f475Smrg
967d522f475Smrg    screen->curgl = 0;		/* G0 => GL.            */
968d522f475Smrg    screen->curgr = 2;		/* G2 => GR.            */
969d522f475Smrg    screen->curss = 0;		/* No single shift.     */
970d522f475Smrg
971d522f475Smrg#if OPT_VT52_MODE
972d522f475Smrg    if (screen->vtXX_level == 0)
973d522f475Smrg	screen->gsets[1] = '0';	/* Graphics             */
974d522f475Smrg#endif
975d522f475Smrg}
976d522f475Smrg
977d522f475Smrg/*
978d522f475Smrg * VT300 and up support three ANSI conformance levels, defined according to
979d522f475Smrg * the dpANSI X3.134.1 standard.  DEC's manuals equate levels 1 and 2, and
980d522f475Smrg * are unclear.  This code is written based on the manuals.
981d522f475Smrg */
982d522f475Smrgstatic void
983d522f475Smrgset_ansi_conformance(TScreen * screen, int level)
984d522f475Smrg{
985d522f475Smrg    TRACE(("set_ansi_conformance(%d) terminal_id %d, ansi_level %d\n",
986d522f475Smrg	   level,
987d522f475Smrg	   screen->terminal_id,
988d522f475Smrg	   screen->ansi_level));
989d522f475Smrg    if (screen->vtXX_level >= 3) {
990d522f475Smrg	switch (screen->ansi_level = level) {
991d522f475Smrg	case 1:
992d522f475Smrg	    /* FALLTHRU */
993d522f475Smrg	case 2:
994d522f475Smrg	    screen->gsets[0] = 'B';	/* G0 is ASCII */
995d522f475Smrg	    screen->gsets[1] = 'B';	/* G1 is ISO Latin-1 (FIXME) */
996d522f475Smrg	    screen->curgl = 0;
997d522f475Smrg	    screen->curgr = 1;
998d522f475Smrg	    break;
999d522f475Smrg	case 3:
1000d522f475Smrg	    screen->gsets[0] = 'B';	/* G0 is ASCII */
1001d522f475Smrg	    screen->curgl = 0;
1002d522f475Smrg	    break;
1003d522f475Smrg	}
1004d522f475Smrg    }
1005d522f475Smrg}
1006d522f475Smrg
1007d522f475Smrg/*
1008d522f475Smrg * Set scrolling margins.  VTxxx terminals require that the top/bottom are
1009d522f475Smrg * different, so we have at least two lines in the scrolling region.
1010d522f475Smrg */
1011d522f475Smrgvoid
1012d522f475Smrgset_tb_margins(TScreen * screen, int top, int bottom)
1013d522f475Smrg{
1014d522f475Smrg    TRACE(("set_tb_margins %d..%d, prior %d..%d\n",
1015d522f475Smrg	   top, bottom,
1016d522f475Smrg	   screen->top_marg,
1017d522f475Smrg	   screen->bot_marg));
1018d522f475Smrg    if (bottom > top) {
1019d522f475Smrg	screen->top_marg = top;
1020d522f475Smrg	screen->bot_marg = bottom;
1021d522f475Smrg    }
1022d522f475Smrg    if (screen->top_marg > screen->max_row)
1023d522f475Smrg	screen->top_marg = screen->max_row;
1024d522f475Smrg    if (screen->bot_marg > screen->max_row)
1025d522f475Smrg	screen->bot_marg = screen->max_row;
1026d522f475Smrg}
1027d522f475Smrg
1028d522f475Smrgvoid
1029d522f475Smrgset_max_col(TScreen * screen, int cols)
1030d522f475Smrg{
1031d522f475Smrg    TRACE(("set_max_col %d, prior %d\n", cols, screen->max_col));
1032d522f475Smrg    if (cols < 0)
1033d522f475Smrg	cols = 0;
1034d522f475Smrg    screen->max_col = cols;
1035d522f475Smrg}
1036d522f475Smrg
1037d522f475Smrgvoid
1038d522f475Smrgset_max_row(TScreen * screen, int rows)
1039d522f475Smrg{
1040d522f475Smrg    TRACE(("set_max_row %d, prior %d\n", rows, screen->max_row));
1041d522f475Smrg    if (rows < 0)
1042d522f475Smrg	rows = 0;
1043d522f475Smrg    screen->max_row = rows;
1044d522f475Smrg}
1045d522f475Smrg
1046d522f475Smrg#if OPT_MOD_FKEYS
1047d522f475Smrgstatic void
1048d522f475Smrgset_mod_fkeys(XtermWidget xw, int which, int what, Bool enabled)
1049d522f475Smrg{
1050d522f475Smrg#define SET_MOD_FKEYS(field) \
1051d522f475Smrg    xw->keyboard.modify_now.field = ((what == DEFAULT) && enabled) \
1052d522f475Smrg				     ? xw->keyboard.modify_1st.field \
1053d522f475Smrg				     : what; \
1054d522f475Smrg    TRACE(("set modify_now.%s to %d\n", #field, \
1055d522f475Smrg	   xw->keyboard.modify_now.field));
1056d522f475Smrg
1057d522f475Smrg    switch (which) {
1058d522f475Smrg    case 1:
1059d522f475Smrg	SET_MOD_FKEYS(cursor_keys);
1060d522f475Smrg	break;
1061d522f475Smrg    case 2:
1062d522f475Smrg	SET_MOD_FKEYS(function_keys);
1063d522f475Smrg	break;
1064d522f475Smrg    case 3:
1065d522f475Smrg	SET_MOD_FKEYS(keypad_keys);
1066d522f475Smrg	break;
1067d522f475Smrg    case 4:
1068d522f475Smrg	SET_MOD_FKEYS(other_keys);
1069d522f475Smrg	break;
1070d522f475Smrg    case 5:
1071d522f475Smrg	SET_MOD_FKEYS(string_keys);
1072d522f475Smrg	break;
1073d522f475Smrg    }
1074d522f475Smrg}
1075d522f475Smrg#endif /* OPT_MOD_FKEYS */
1076d522f475Smrg
1077d522f475Smrg#if OPT_TRACE
1078d522f475Smrg#define WHICH_TABLE(name) if (table == name) result = #name
1079d522f475Smrgstatic char *
1080d522f475Smrgwhich_table(Const PARSE_T * table)
1081d522f475Smrg{
1082d522f475Smrg    char *result = "?";
1083d522f475Smrg    /* *INDENT-OFF* */
1084d522f475Smrg    WHICH_TABLE (ansi_table);
1085d522f475Smrg    else WHICH_TABLE (csi_table);
1086d522f475Smrg    else WHICH_TABLE (csi2_table);
1087d522f475Smrg    else WHICH_TABLE (csi_ex_table);
1088d522f475Smrg    else WHICH_TABLE (csi_quo_table);
1089d522f475Smrg#if OPT_DEC_LOCATOR
1090d522f475Smrg    else WHICH_TABLE (csi_tick_table);
1091d522f475Smrg#endif
1092d522f475Smrg#if OPT_DEC_RECTOPS
1093d522f475Smrg    else WHICH_TABLE (csi_dollar_table);
1094d522f475Smrg    else WHICH_TABLE (csi_star_table);
1095d522f475Smrg#endif
1096d522f475Smrg    else WHICH_TABLE (dec_table);
1097d522f475Smrg    else WHICH_TABLE (dec2_table);
1098d522f475Smrg    else WHICH_TABLE (dec3_table);
1099d522f475Smrg    else WHICH_TABLE (cigtable);
1100d522f475Smrg    else WHICH_TABLE (eigtable);
1101d522f475Smrg    else WHICH_TABLE (esc_table);
1102d522f475Smrg    else WHICH_TABLE (esc_sp_table);
1103d522f475Smrg    else WHICH_TABLE (scrtable);
1104d522f475Smrg    else WHICH_TABLE (scstable);
1105d522f475Smrg    else WHICH_TABLE (sos_table);
1106d522f475Smrg#if OPT_WIDE_CHARS
1107d522f475Smrg    else WHICH_TABLE (esc_pct_table);
1108d522f475Smrg#endif
1109d522f475Smrg#if OPT_VT52_MODE
1110d522f475Smrg    else WHICH_TABLE (vt52_table);
1111d522f475Smrg    else WHICH_TABLE (vt52_esc_table);
1112d522f475Smrg    else WHICH_TABLE (vt52_ignore_table);
1113d522f475Smrg#endif
1114d522f475Smrg    /* *INDENT-ON* */
1115d522f475Smrg
1116d522f475Smrg    return result;
1117d522f475Smrg}
1118d522f475Smrg#endif
1119d522f475Smrg
1120d522f475Smrg	/* allocate larger buffer if needed/possible */
1121d522f475Smrg#define SafeAlloc(type, area, used, size) \
1122d522f475Smrg		type *new_string = area; \
1123d522f475Smrg		unsigned new_length = size; \
1124d522f475Smrg		if (new_length == 0) { \
1125d522f475Smrg		    new_length = 256; \
1126d522f475Smrg		    new_string = TypeMallocN(type, new_length); \
1127d522f475Smrg		} else if (used+1 >= new_length) { \
1128d522f475Smrg		    new_length = size * 2; \
1129d522f475Smrg		    new_string = TypeMallocN(type, new_length); \
1130d522f475Smrg		    if (new_string != 0 \
1131d522f475Smrg		     && area != 0 \
1132d522f475Smrg		     && used != 0) \
1133d522f475Smrg			memcpy(new_string, area, used * sizeof(type)); \
1134d522f475Smrg		}
1135d522f475Smrg
1136d522f475Smrg#define WriteNow() {						\
1137d522f475Smrg	    unsigned single = 0;				\
1138d522f475Smrg								\
1139d522f475Smrg	    if (screen->curss) {				\
1140d522f475Smrg		dotext(xw,					\
1141d522f475Smrg		       screen->gsets[(int) (screen->curss)],	\
1142d522f475Smrg		       print_area, 1);				\
1143d522f475Smrg		screen->curss = 0;				\
1144d522f475Smrg		single++;					\
1145d522f475Smrg	    }							\
1146d522f475Smrg	    if (print_used > single) {				\
1147d522f475Smrg		dotext(xw,					\
1148d522f475Smrg		       screen->gsets[(int) (screen->curgl)],	\
1149d522f475Smrg		       print_area + single,			\
1150d522f475Smrg		       print_used - single);			\
1151d522f475Smrg	    }							\
1152d522f475Smrg	    print_used = 0;					\
1153d522f475Smrg	}							\
1154d522f475Smrg
1155d522f475Smrgstruct ParseState {
1156d522f475Smrg#if OPT_VT52_MODE
1157d522f475Smrg    Bool vt52_cup;
1158d522f475Smrg#endif
1159d522f475Smrg    Const PARSE_T *groundtable;
1160d522f475Smrg    Const PARSE_T *parsestate;
1161d522f475Smrg    int scstype;
1162d522f475Smrg    Bool private_function;	/* distinguish private-mode from standard */
1163d522f475Smrg    int string_mode;		/* nonzero iff we're processing a string */
1164d522f475Smrg    int lastchar;		/* positive iff we had a graphic character */
1165d522f475Smrg    int nextstate;
1166d522f475Smrg#if OPT_WIDE_CHARS
1167d522f475Smrg    int last_was_wide;
1168d522f475Smrg#endif
1169d522f475Smrg};
1170d522f475Smrg
1171d522f475Smrgstatic struct ParseState myState;
1172d522f475Smrg
1173d522f475Smrgstatic Boolean
1174d522f475Smrgdoparsing(XtermWidget xw, unsigned c, struct ParseState *sp)
1175d522f475Smrg{
1176d522f475Smrg    /* Buffer for processing printable text */
1177d522f475Smrg    static IChar *print_area;
1178d522f475Smrg    static size_t print_size, print_used;
1179d522f475Smrg
1180d522f475Smrg    /* Buffer for processing strings (e.g., OSC ... ST) */
1181d522f475Smrg    static Char *string_area;
1182d522f475Smrg    static size_t string_size, string_used;
1183d522f475Smrg
1184d522f475Smrg    TScreen *screen = &xw->screen;
1185d522f475Smrg    int row;
1186d522f475Smrg    int col;
1187d522f475Smrg    int top;
1188d522f475Smrg    int bot;
1189d522f475Smrg    int count;
1190d522f475Smrg    int laststate;
1191d522f475Smrg    int thischar = -1;
1192d522f475Smrg    XTermRect myRect;
1193d522f475Smrg
1194d522f475Smrg    do {
1195d522f475Smrg#if OPT_WIDE_CHARS
1196d522f475Smrg
1197d522f475Smrg	/*
1198d522f475Smrg	 * Handle zero-width combining characters.  Make it faster by noting
1199d522f475Smrg	 * that according to the Unicode charts, the majority of Western
1200d522f475Smrg	 * character sets do not use this feature.  There are some unassigned
1201d522f475Smrg	 * codes at 0x242, but no zero-width characters until past 0x300.
1202d522f475Smrg	 */
1203d522f475Smrg	if (c >= 0x300 && screen->wide_chars
1204d522f475Smrg	    && my_wcwidth((int) c) == 0
1205d522f475Smrg	    && !isWideControl(c)) {
1206d522f475Smrg	    int prev, precomposed;
1207d522f475Smrg
1208d522f475Smrg	    WriteNow();
1209d522f475Smrg
1210d522f475Smrg	    prev = XTERM_CELL(screen->last_written_row,
1211d522f475Smrg			      screen->last_written_col);
1212d522f475Smrg	    precomposed = do_precomposition(prev, (int) c);
1213d522f475Smrg	    TRACE(("do_precomposition (U+%04X [%d], U+%04X [%d]) -> U+%04X [%d]\n",
1214d522f475Smrg		   prev, my_wcwidth(prev),
1215d522f475Smrg		   (int) c, my_wcwidth((int) c),
1216d522f475Smrg		   precomposed, my_wcwidth(precomposed)));
1217d522f475Smrg
1218d522f475Smrg	    /* substitute combined character with precomposed character
1219d522f475Smrg	     * only if it does not change the width of the base character
1220d522f475Smrg	     */
1221d522f475Smrg	    if (precomposed != -1 && my_wcwidth(precomposed) == my_wcwidth(prev)) {
1222d522f475Smrg		putXtermCell(screen,
1223d522f475Smrg			     screen->last_written_row,
1224d522f475Smrg			     screen->last_written_col, precomposed);
1225d522f475Smrg	    } else {
1226d522f475Smrg		addXtermCombining(screen,
1227d522f475Smrg				  screen->last_written_row,
1228d522f475Smrg				  screen->last_written_col, c);
1229d522f475Smrg	    }
1230d522f475Smrg
1231d522f475Smrg	    if (!screen->scroll_amt)
1232d522f475Smrg		ScrnUpdate(xw,
1233d522f475Smrg			   screen->last_written_row,
1234d522f475Smrg			   screen->last_written_col, 1, 1, 1);
1235d522f475Smrg	    continue;
1236d522f475Smrg	}
1237d522f475Smrg#endif
1238d522f475Smrg
1239d522f475Smrg	/* Intercept characters for printer controller mode */
1240d522f475Smrg	if (screen->printer_controlmode == 2) {
1241d522f475Smrg	    if ((c = xtermPrinterControl((int) c)) == 0)
1242d522f475Smrg		continue;
1243d522f475Smrg	}
1244d522f475Smrg
1245d522f475Smrg	/*
1246d522f475Smrg	 * VT52 is a little ugly in the one place it has a parameterized
1247d522f475Smrg	 * control sequence, since the parameter falls after the character
1248d522f475Smrg	 * that denotes the type of sequence.
1249d522f475Smrg	 */
1250d522f475Smrg#if OPT_VT52_MODE
1251d522f475Smrg	if (sp->vt52_cup) {
1252d522f475Smrg	    if (nparam < NPARAM)
1253d522f475Smrg		param[nparam++] = (c & 0x7f) - 32;
1254d522f475Smrg	    if (nparam < 2)
1255d522f475Smrg		continue;
1256d522f475Smrg	    sp->vt52_cup = False;
1257d522f475Smrg	    if ((row = param[0]) < 0)
1258d522f475Smrg		row = 0;
1259d522f475Smrg	    if ((col = param[1]) < 0)
1260d522f475Smrg		col = 0;
1261d522f475Smrg	    CursorSet(screen, row, col, xw->flags);
1262d522f475Smrg	    sp->parsestate = vt52_table;
1263d522f475Smrg	    param[0] = 0;
1264d522f475Smrg	    param[1] = 0;
1265d522f475Smrg	    continue;
1266d522f475Smrg	}
1267d522f475Smrg#endif
1268d522f475Smrg
1269d522f475Smrg	/*
1270d522f475Smrg	 * The parsing tables all have 256 entries.  If we're supporting
1271d522f475Smrg	 * wide characters, we handle them by treating them the same as
1272d522f475Smrg	 * printing characters.
1273d522f475Smrg	 */
1274d522f475Smrg	laststate = sp->nextstate;
1275d522f475Smrg#if OPT_WIDE_CHARS
1276d522f475Smrg	if (c > 255) {
1277d522f475Smrg	    if (sp->parsestate == sp->groundtable) {
1278d522f475Smrg		sp->nextstate = CASE_PRINT;
1279d522f475Smrg	    } else if (sp->parsestate == sos_table) {
1280d522f475Smrg		c &= 0xffff;
1281d522f475Smrg		if (c > 255) {
1282d522f475Smrg		    TRACE(("Found code > 255 while in SOS state: %04X\n", c));
1283d522f475Smrg		    c = '?';
1284d522f475Smrg		}
1285d522f475Smrg	    } else {
1286d522f475Smrg		sp->nextstate = CASE_GROUND_STATE;
1287d522f475Smrg	    }
1288d522f475Smrg	} else
1289d522f475Smrg#endif
1290d522f475Smrg	    sp->nextstate = sp->parsestate[E2A(c)];
1291d522f475Smrg
1292d522f475Smrg#if OPT_BROKEN_OSC
1293d522f475Smrg	/*
1294d522f475Smrg	 * Linux console palette escape sequences start with an OSC, but do
1295d522f475Smrg	 * not terminate correctly.  Some scripts do not check before writing
1296d522f475Smrg	 * them, making xterm appear to hang (it's awaiting a valid string
1297d522f475Smrg	 * terminator).  Just ignore these if we see them - there's no point
1298d522f475Smrg	 * in emulating bad code.
1299d522f475Smrg	 */
1300d522f475Smrg	if (screen->brokenLinuxOSC
1301d522f475Smrg	    && sp->parsestate == sos_table) {
1302d522f475Smrg	    if (string_used) {
1303d522f475Smrg		switch (string_area[0]) {
1304d522f475Smrg		case 'P':
1305d522f475Smrg		    if (string_used <= 7)
1306d522f475Smrg			break;
1307d522f475Smrg		    /* FALLTHRU */
1308d522f475Smrg		case 'R':
1309d522f475Smrg		    sp->parsestate = sp->groundtable;
1310d522f475Smrg		    sp->nextstate = sp->parsestate[E2A(c)];
1311d522f475Smrg		    TRACE(("Reset to ground state (brokenLinuxOSC)\n"));
1312d522f475Smrg		    break;
1313d522f475Smrg		}
1314d522f475Smrg	    }
1315d522f475Smrg	}
1316d522f475Smrg#endif
1317d522f475Smrg
1318d522f475Smrg#if OPT_BROKEN_ST
1319d522f475Smrg	/*
1320d522f475Smrg	 * Before patch #171, carriage control embedded within an OSC string
1321d522f475Smrg	 * would terminate it.  Some (buggy, of course) applications rely on
1322d522f475Smrg	 * this behavior.  Accommodate them by allowing one to compile xterm
1323d522f475Smrg	 * and emulate the old behavior.
1324d522f475Smrg	 */
1325d522f475Smrg	if (screen->brokenStringTerm
1326d522f475Smrg	    && sp->parsestate == sos_table
1327d522f475Smrg	    && c < 32) {
1328d522f475Smrg	    switch (c) {
1329d522f475Smrg	    case 5:		/* FALLTHRU */
1330d522f475Smrg	    case 8:		/* FALLTHRU */
1331d522f475Smrg	    case 9:		/* FALLTHRU */
1332d522f475Smrg	    case 10:		/* FALLTHRU */
1333d522f475Smrg	    case 11:		/* FALLTHRU */
1334d522f475Smrg	    case 12:		/* FALLTHRU */
1335d522f475Smrg	    case 13:		/* FALLTHRU */
1336d522f475Smrg	    case 14:		/* FALLTHRU */
1337d522f475Smrg	    case 15:		/* FALLTHRU */
1338d522f475Smrg	    case 24:
1339d522f475Smrg		sp->parsestate = sp->groundtable;
1340d522f475Smrg		sp->nextstate = sp->parsestate[E2A(c)];
1341d522f475Smrg		TRACE(("Reset to ground state (brokenStringTerm)\n"));
1342d522f475Smrg		break;
1343d522f475Smrg	    }
1344d522f475Smrg	}
1345d522f475Smrg#endif
1346d522f475Smrg
1347d522f475Smrg#if OPT_C1_PRINT
1348d522f475Smrg	/*
1349d522f475Smrg	 * This is not completely foolproof, but will allow an application
1350d522f475Smrg	 * with values in the C1 range to use them as printable characters,
1351d522f475Smrg	 * provided that they are not intermixed with an escape sequence.
1352d522f475Smrg	 */
1353d522f475Smrg	if (screen->c1_printable
1354d522f475Smrg	    && (c >= 128 && c < 160)) {
1355d522f475Smrg	    sp->nextstate = (sp->parsestate == esc_table
1356d522f475Smrg			     ? CASE_ESC_IGNORE
1357d522f475Smrg			     : sp->parsestate[E2A(160)]);
1358d522f475Smrg	}
1359d522f475Smrg#endif
1360d522f475Smrg
1361d522f475Smrg#if OPT_WIDE_CHARS
1362d522f475Smrg	/*
1363d522f475Smrg	 * If we have a C1 code and the c1_printable flag is not set, simply
1364d522f475Smrg	 * ignore it when it was translated from UTF-8.  That is because the
1365d522f475Smrg	 * value could not have been present as-is in the UTF-8.
1366d522f475Smrg	 *
1367d522f475Smrg	 * To see that CASE_IGNORE is a consistent value, note that it is
1368d522f475Smrg	 * always used for NUL and other uninteresting C0 controls.
1369d522f475Smrg	 */
1370d522f475Smrg#if OPT_C1_PRINT
1371d522f475Smrg	if (!screen->c1_printable)
1372d522f475Smrg#endif
1373d522f475Smrg	    if (screen->wide_chars
1374d522f475Smrg		&& (c >= 128 && c < 160)) {
1375d522f475Smrg		sp->nextstate = CASE_IGNORE;
1376d522f475Smrg	    }
1377d522f475Smrg
1378d522f475Smrg	/*
1379d522f475Smrg	 * If this character is a different width than the last one, put the
1380d522f475Smrg	 * previous text into the buffer and draw it now.
1381d522f475Smrg	 */
1382d522f475Smrg	if (iswide((int) c) != sp->last_was_wide) {
1383d522f475Smrg	    WriteNow();
1384d522f475Smrg	}
1385d522f475Smrg#endif
1386d522f475Smrg
1387d522f475Smrg	/*
1388d522f475Smrg	 * Accumulate string for printable text.  This may be 8/16-bit
1389d522f475Smrg	 * characters.
1390d522f475Smrg	 */
1391d522f475Smrg	if (sp->nextstate == CASE_PRINT) {
1392d522f475Smrg	    SafeAlloc(IChar, print_area, print_used, print_size);
1393d522f475Smrg	    if (new_string == 0) {
1394d522f475Smrg		fprintf(stderr,
1395d522f475Smrg			"Cannot allocate %u bytes for printable text\n",
1396d522f475Smrg			new_length);
1397d522f475Smrg		continue;
1398d522f475Smrg	    }
1399d522f475Smrg#if OPT_VT52_MODE
1400d522f475Smrg	    /*
1401d522f475Smrg	     * Strip output text to 7-bits for VT52.  We should do this for
1402d522f475Smrg	     * VT100 also (which is a 7-bit device), but xterm has been
1403d522f475Smrg	     * doing this for so long we shouldn't change this behavior.
1404d522f475Smrg	     */
1405d522f475Smrg	    if (screen->vtXX_level < 1)
1406d522f475Smrg		c &= 0x7f;
1407d522f475Smrg#endif
1408d522f475Smrg	    print_area = new_string;
1409d522f475Smrg	    print_size = new_length;
1410d522f475Smrg	    print_area[print_used++] = sp->lastchar = thischar = c;
1411d522f475Smrg#if OPT_WIDE_CHARS
1412d522f475Smrg	    sp->last_was_wide = iswide((int) c);
1413d522f475Smrg#endif
1414d522f475Smrg	    if (morePtyData(screen, VTbuffer)) {
1415d522f475Smrg		continue;
1416d522f475Smrg	    }
1417d522f475Smrg	}
1418d522f475Smrg
1419d522f475Smrg	if (sp->nextstate == CASE_PRINT
1420d522f475Smrg	    || (laststate == CASE_PRINT && print_used)) {
1421d522f475Smrg	    WriteNow();
1422d522f475Smrg	}
1423d522f475Smrg
1424d522f475Smrg	/*
1425d522f475Smrg	 * Accumulate string for APC, DCS, PM, OSC, SOS controls
1426d522f475Smrg	 * This should always be 8-bit characters.
1427d522f475Smrg	 */
1428d522f475Smrg	if (sp->parsestate == sos_table) {
1429d522f475Smrg	    SafeAlloc(Char, string_area, string_used, string_size);
1430d522f475Smrg	    if (new_string == 0) {
1431d522f475Smrg		fprintf(stderr,
1432d522f475Smrg			"Cannot allocate %u bytes for string mode %d\n",
1433d522f475Smrg			new_length, sp->string_mode);
1434d522f475Smrg		continue;
1435d522f475Smrg	    }
1436d522f475Smrg#if OPT_WIDE_CHARS
1437d522f475Smrg	    /*
1438d522f475Smrg	     * We cannot display codes above 255, but let's try to
1439d522f475Smrg	     * accommodate the application a little by not aborting the
1440d522f475Smrg	     * string.
1441d522f475Smrg	     */
1442d522f475Smrg	    if ((c & 0xffff) > 255) {
1443d522f475Smrg		sp->nextstate = CASE_PRINT;
1444d522f475Smrg		c = '?';
1445d522f475Smrg	    }
1446d522f475Smrg#endif
1447d522f475Smrg	    string_area = new_string;
1448d522f475Smrg	    string_size = new_length;
1449d522f475Smrg	    string_area[string_used++] = c;
1450d522f475Smrg	} else if (sp->parsestate != esc_table) {
1451d522f475Smrg	    /* if we were accumulating, we're not any more */
1452d522f475Smrg	    sp->string_mode = 0;
1453d522f475Smrg	    string_used = 0;
1454d522f475Smrg	}
1455d522f475Smrg
1456d522f475Smrg	TRACE(("parse %04X -> %d %s\n", c, sp->nextstate, which_table(sp->parsestate)));
1457d522f475Smrg
1458d522f475Smrg	switch (sp->nextstate) {
1459d522f475Smrg	case CASE_PRINT:
1460d522f475Smrg	    TRACE(("CASE_PRINT - printable characters\n"));
1461d522f475Smrg	    break;
1462d522f475Smrg
1463d522f475Smrg	case CASE_GROUND_STATE:
1464d522f475Smrg	    TRACE(("CASE_GROUND_STATE - exit ignore mode\n"));
1465d522f475Smrg	    sp->parsestate = sp->groundtable;
1466d522f475Smrg	    break;
1467d522f475Smrg
1468d522f475Smrg	case CASE_IGNORE:
1469d522f475Smrg	    TRACE(("CASE_IGNORE - Ignore character %02X\n", c));
1470d522f475Smrg	    break;
1471d522f475Smrg
1472d522f475Smrg	case CASE_ENQ:
1473d522f475Smrg	    TRACE(("CASE_ENQ - answerback\n"));
1474d522f475Smrg	    for (count = 0; screen->answer_back[count] != 0; count++)
1475d522f475Smrg		unparseputc(xw, screen->answer_back[count]);
1476d522f475Smrg	    unparse_end(xw);
1477d522f475Smrg	    break;
1478d522f475Smrg
1479d522f475Smrg	case CASE_BELL:
1480d522f475Smrg	    TRACE(("CASE_BELL - bell\n"));
1481d522f475Smrg	    if (sp->string_mode == ANSI_OSC) {
1482d522f475Smrg		if (string_used)
1483d522f475Smrg		    string_area[--string_used] = '\0';
1484d522f475Smrg		do_osc(xw, string_area, string_used, (int) c);
1485d522f475Smrg		sp->parsestate = sp->groundtable;
1486d522f475Smrg	    } else {
1487d522f475Smrg		/* bell */
1488d522f475Smrg		Bell(XkbBI_TerminalBell, 0);
1489d522f475Smrg	    }
1490d522f475Smrg	    break;
1491d522f475Smrg
1492d522f475Smrg	case CASE_BS:
1493d522f475Smrg	    TRACE(("CASE_BS - backspace\n"));
1494d522f475Smrg	    CursorBack(xw, 1);
1495d522f475Smrg	    break;
1496d522f475Smrg
1497d522f475Smrg	case CASE_CR:
1498d522f475Smrg	    /* CR */
1499d522f475Smrg	    CarriageReturn(screen);
1500d522f475Smrg	    break;
1501d522f475Smrg
1502d522f475Smrg	case CASE_ESC:
1503d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1504d522f475Smrg		sp->parsestate = vt52_esc_table;
1505d522f475Smrg		break;
1506d522f475Smrg	    });
1507d522f475Smrg	    sp->parsestate = esc_table;
1508d522f475Smrg	    break;
1509d522f475Smrg
1510d522f475Smrg#if OPT_VT52_MODE
1511d522f475Smrg	case CASE_VT52_CUP:
1512d522f475Smrg	    TRACE(("CASE_VT52_CUP - VT52 cursor addressing\n"));
1513d522f475Smrg	    sp->vt52_cup = True;
1514d522f475Smrg	    nparam = 0;
1515d522f475Smrg	    break;
1516d522f475Smrg
1517d522f475Smrg	case CASE_VT52_IGNORE:
1518d522f475Smrg	    TRACE(("CASE_VT52_IGNORE - VT52 ignore-character\n"));
1519d522f475Smrg	    sp->parsestate = vt52_ignore_table;
1520d522f475Smrg	    break;
1521d522f475Smrg#endif
1522d522f475Smrg
1523d522f475Smrg	case CASE_VMOT:
1524d522f475Smrg	    /*
1525d522f475Smrg	     * form feed, line feed, vertical tab
1526d522f475Smrg	     */
1527d522f475Smrg	    xtermAutoPrint(c);
1528d522f475Smrg	    xtermIndex(xw, 1);
1529d522f475Smrg	    if (xw->flags & LINEFEED)
1530d522f475Smrg		CarriageReturn(screen);
1531d522f475Smrg	    else
1532d522f475Smrg		do_xevents();
1533d522f475Smrg	    break;
1534d522f475Smrg
1535d522f475Smrg	case CASE_CBT:
1536d522f475Smrg	    /* cursor backward tabulation */
1537d522f475Smrg	    if ((count = param[0]) == DEFAULT)
1538d522f475Smrg		count = 1;
1539d522f475Smrg	    while ((count-- > 0)
1540d522f475Smrg		   && (TabToPrevStop(xw))) ;
1541d522f475Smrg	    sp->parsestate = sp->groundtable;
1542d522f475Smrg	    break;
1543d522f475Smrg
1544d522f475Smrg	case CASE_CHT:
1545d522f475Smrg	    /* cursor forward tabulation */
1546d522f475Smrg	    if ((count = param[0]) == DEFAULT)
1547d522f475Smrg		count = 1;
1548d522f475Smrg	    while ((count-- > 0)
1549d522f475Smrg		   && (TabToNextStop(xw))) ;
1550d522f475Smrg	    sp->parsestate = sp->groundtable;
1551d522f475Smrg	    break;
1552d522f475Smrg
1553d522f475Smrg	case CASE_TAB:
1554d522f475Smrg	    /* tab */
1555d522f475Smrg	    TabToNextStop(xw);
1556d522f475Smrg	    break;
1557d522f475Smrg
1558d522f475Smrg	case CASE_SI:
1559d522f475Smrg	    screen->curgl = 0;
1560d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1561d522f475Smrg		sp->parsestate = sp->groundtable;
1562d522f475Smrg	    });
1563d522f475Smrg	    break;
1564d522f475Smrg
1565d522f475Smrg	case CASE_SO:
1566d522f475Smrg	    screen->curgl = 1;
1567d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1568d522f475Smrg		sp->parsestate = sp->groundtable;
1569d522f475Smrg	    });
1570d522f475Smrg	    break;
1571d522f475Smrg
1572d522f475Smrg	case CASE_DECDHL:
1573d522f475Smrg	    xterm_DECDHL(xw, c == '3');
1574d522f475Smrg	    sp->parsestate = sp->groundtable;
1575d522f475Smrg	    break;
1576d522f475Smrg
1577d522f475Smrg	case CASE_DECSWL:
1578d522f475Smrg	    xterm_DECSWL(xw);
1579d522f475Smrg	    sp->parsestate = sp->groundtable;
1580d522f475Smrg	    break;
1581d522f475Smrg
1582d522f475Smrg	case CASE_DECDWL:
1583d522f475Smrg	    xterm_DECDWL(xw);
1584d522f475Smrg	    sp->parsestate = sp->groundtable;
1585d522f475Smrg	    break;
1586d522f475Smrg
1587d522f475Smrg	case CASE_SCR_STATE:
1588d522f475Smrg	    /* enter scr state */
1589d522f475Smrg	    sp->parsestate = scrtable;
1590d522f475Smrg	    break;
1591d522f475Smrg
1592d522f475Smrg	case CASE_SCS0_STATE:
1593d522f475Smrg	    /* enter scs state 0 */
1594d522f475Smrg	    sp->scstype = 0;
1595d522f475Smrg	    sp->parsestate = scstable;
1596d522f475Smrg	    break;
1597d522f475Smrg
1598d522f475Smrg	case CASE_SCS1_STATE:
1599d522f475Smrg	    /* enter scs state 1 */
1600d522f475Smrg	    sp->scstype = 1;
1601d522f475Smrg	    sp->parsestate = scstable;
1602d522f475Smrg	    break;
1603d522f475Smrg
1604d522f475Smrg	case CASE_SCS2_STATE:
1605d522f475Smrg	    /* enter scs state 2 */
1606d522f475Smrg	    sp->scstype = 2;
1607d522f475Smrg	    sp->parsestate = scstable;
1608d522f475Smrg	    break;
1609d522f475Smrg
1610d522f475Smrg	case CASE_SCS3_STATE:
1611d522f475Smrg	    /* enter scs state 3 */
1612d522f475Smrg	    sp->scstype = 3;
1613d522f475Smrg	    sp->parsestate = scstable;
1614d522f475Smrg	    break;
1615d522f475Smrg
1616d522f475Smrg	case CASE_ESC_IGNORE:
1617d522f475Smrg	    /* unknown escape sequence */
1618d522f475Smrg	    sp->parsestate = eigtable;
1619d522f475Smrg	    break;
1620d522f475Smrg
1621d522f475Smrg	case CASE_ESC_DIGIT:
1622d522f475Smrg	    /* digit in csi or dec mode */
1623d522f475Smrg	    if ((row = param[nparam - 1]) == DEFAULT)
1624d522f475Smrg		row = 0;
1625d522f475Smrg	    param[nparam - 1] = 10 * row + (c - '0');
1626d522f475Smrg	    if (param[nparam - 1] > 65535)
1627d522f475Smrg		param[nparam - 1] = 65535;
1628d522f475Smrg	    if (sp->parsestate == csi_table)
1629d522f475Smrg		sp->parsestate = csi2_table;
1630d522f475Smrg	    break;
1631d522f475Smrg
1632d522f475Smrg	case CASE_ESC_SEMI:
1633d522f475Smrg	    /* semicolon in csi or dec mode */
1634d522f475Smrg	    if (nparam < NPARAM)
1635d522f475Smrg		param[nparam++] = DEFAULT;
1636d522f475Smrg	    if (sp->parsestate == csi_table)
1637d522f475Smrg		sp->parsestate = csi2_table;
1638d522f475Smrg	    break;
1639d522f475Smrg
1640d522f475Smrg	case CASE_DEC_STATE:
1641d522f475Smrg	    /* enter dec mode */
1642d522f475Smrg	    sp->parsestate = dec_table;
1643d522f475Smrg	    break;
1644d522f475Smrg
1645d522f475Smrg	case CASE_DEC2_STATE:
1646d522f475Smrg	    /* enter dec2 mode */
1647d522f475Smrg	    sp->parsestate = dec2_table;
1648d522f475Smrg	    break;
1649d522f475Smrg
1650d522f475Smrg	case CASE_DEC3_STATE:
1651d522f475Smrg	    /* enter dec3 mode */
1652d522f475Smrg	    sp->parsestate = dec3_table;
1653d522f475Smrg	    break;
1654d522f475Smrg
1655d522f475Smrg	case CASE_ICH:
1656d522f475Smrg	    TRACE(("CASE_ICH - insert char\n"));
1657d522f475Smrg	    if ((row = param[0]) < 1)
1658d522f475Smrg		row = 1;
1659d522f475Smrg	    InsertChar(xw, (unsigned) row);
1660d522f475Smrg	    sp->parsestate = sp->groundtable;
1661d522f475Smrg	    break;
1662d522f475Smrg
1663d522f475Smrg	case CASE_CUU:
1664d522f475Smrg	    TRACE(("CASE_CUU - cursor up\n"));
1665d522f475Smrg	    if ((row = param[0]) < 1)
1666d522f475Smrg		row = 1;
1667d522f475Smrg	    CursorUp(screen, row);
1668d522f475Smrg	    sp->parsestate = sp->groundtable;
1669d522f475Smrg	    break;
1670d522f475Smrg
1671d522f475Smrg	case CASE_CUD:
1672d522f475Smrg	    TRACE(("CASE_CUD - cursor down\n"));
1673d522f475Smrg	    if ((row = param[0]) < 1)
1674d522f475Smrg		row = 1;
1675d522f475Smrg	    CursorDown(screen, row);
1676d522f475Smrg	    sp->parsestate = sp->groundtable;
1677d522f475Smrg	    break;
1678d522f475Smrg
1679d522f475Smrg	case CASE_CUF:
1680d522f475Smrg	    TRACE(("CASE_CUF - cursor forward\n"));
1681d522f475Smrg	    if ((col = param[0]) < 1)
1682d522f475Smrg		col = 1;
1683d522f475Smrg	    CursorForward(screen, col);
1684d522f475Smrg	    sp->parsestate = sp->groundtable;
1685d522f475Smrg	    break;
1686d522f475Smrg
1687d522f475Smrg	case CASE_CUB:
1688d522f475Smrg	    TRACE(("CASE_CUB - cursor backward\n"));
1689d522f475Smrg	    if ((col = param[0]) < 1)
1690d522f475Smrg		col = 1;
1691d522f475Smrg	    CursorBack(xw, col);
1692d522f475Smrg	    sp->parsestate = sp->groundtable;
1693d522f475Smrg	    break;
1694d522f475Smrg
1695d522f475Smrg	case CASE_CUP:
1696d522f475Smrg	    TRACE(("CASE_CUP - cursor position\n"));
1697d522f475Smrg	    if_OPT_XMC_GLITCH(screen, {
1698d522f475Smrg		Jump_XMC(xw);
1699d522f475Smrg	    });
1700d522f475Smrg	    if ((row = param[0]) < 1)
1701d522f475Smrg		row = 1;
1702d522f475Smrg	    if (nparam < 2 || (col = param[1]) < 1)
1703d522f475Smrg		col = 1;
1704d522f475Smrg	    CursorSet(screen, row - 1, col - 1, xw->flags);
1705d522f475Smrg	    sp->parsestate = sp->groundtable;
1706d522f475Smrg	    break;
1707d522f475Smrg
1708d522f475Smrg	case CASE_VPA:
1709d522f475Smrg	    TRACE(("CASE_VPA - vertical position\n"));
1710d522f475Smrg	    if ((row = param[0]) < 1)
1711d522f475Smrg		row = 1;
1712d522f475Smrg	    CursorSet(screen, row - 1, screen->cur_col, xw->flags);
1713d522f475Smrg	    sp->parsestate = sp->groundtable;
1714d522f475Smrg	    break;
1715d522f475Smrg
1716d522f475Smrg	case CASE_HPA:
1717d522f475Smrg	    TRACE(("CASE_HPA - horizontal position\n"));
1718d522f475Smrg	    if ((col = param[0]) < 1)
1719d522f475Smrg		col = 1;
1720d522f475Smrg	    CursorSet(screen, screen->cur_row, col - 1, xw->flags);
1721d522f475Smrg	    sp->parsestate = sp->groundtable;
1722d522f475Smrg	    break;
1723d522f475Smrg
1724d522f475Smrg	case CASE_HP_BUGGY_LL:
1725d522f475Smrg	    TRACE(("CASE_HP_BUGGY_LL\n"));
1726d522f475Smrg	    /* Some HP-UX applications have the bug that they
1727d522f475Smrg	       assume ESC F goes to the lower left corner of
1728d522f475Smrg	       the screen, regardless of what terminfo says. */
1729d522f475Smrg	    if (screen->hp_ll_bc)
1730d522f475Smrg		CursorSet(screen, screen->max_row, 0, xw->flags);
1731d522f475Smrg	    sp->parsestate = sp->groundtable;
1732d522f475Smrg	    break;
1733d522f475Smrg
1734d522f475Smrg	case CASE_ED:
1735d522f475Smrg	    TRACE(("CASE_ED - erase display\n"));
1736d522f475Smrg	    do_erase_display(xw, param[0], OFF_PROTECT);
1737d522f475Smrg	    sp->parsestate = sp->groundtable;
1738d522f475Smrg	    break;
1739d522f475Smrg
1740d522f475Smrg	case CASE_EL:
1741d522f475Smrg	    TRACE(("CASE_EL - erase line\n"));
1742d522f475Smrg	    do_erase_line(xw, param[0], OFF_PROTECT);
1743d522f475Smrg	    sp->parsestate = sp->groundtable;
1744d522f475Smrg	    break;
1745d522f475Smrg
1746d522f475Smrg	case CASE_ECH:
1747d522f475Smrg	    TRACE(("CASE_ECH - erase char\n"));
1748d522f475Smrg	    /* ECH */
1749d522f475Smrg	    ClearRight(xw, param[0] < 1 ? 1 : param[0]);
1750d522f475Smrg	    sp->parsestate = sp->groundtable;
1751d522f475Smrg	    break;
1752d522f475Smrg
1753d522f475Smrg	case CASE_IL:
1754d522f475Smrg	    TRACE(("CASE_IL - insert line\n"));
1755d522f475Smrg	    if ((row = param[0]) < 1)
1756d522f475Smrg		row = 1;
1757d522f475Smrg	    InsertLine(xw, row);
1758d522f475Smrg	    sp->parsestate = sp->groundtable;
1759d522f475Smrg	    break;
1760d522f475Smrg
1761d522f475Smrg	case CASE_DL:
1762d522f475Smrg	    TRACE(("CASE_DL - delete line\n"));
1763d522f475Smrg	    if ((row = param[0]) < 1)
1764d522f475Smrg		row = 1;
1765d522f475Smrg	    DeleteLine(xw, row);
1766d522f475Smrg	    sp->parsestate = sp->groundtable;
1767d522f475Smrg	    break;
1768d522f475Smrg
1769d522f475Smrg	case CASE_DCH:
1770d522f475Smrg	    TRACE(("CASE_DCH - delete char\n"));
1771d522f475Smrg	    if ((row = param[0]) < 1)
1772d522f475Smrg		row = 1;
1773d522f475Smrg	    DeleteChar(xw, (unsigned) row);
1774d522f475Smrg	    sp->parsestate = sp->groundtable;
1775d522f475Smrg	    break;
1776d522f475Smrg
1777d522f475Smrg	case CASE_TRACK_MOUSE:
1778d522f475Smrg	    /*
1779d522f475Smrg	     * A single parameter other than zero is always scroll-down.
1780d522f475Smrg	     * A zero-parameter is used to reset the mouse mode, and is
1781d522f475Smrg	     * not useful for scrolling anyway.
1782d522f475Smrg	     */
1783d522f475Smrg	    if (nparam > 1 || param[0] == 0) {
1784d522f475Smrg		CELL start;
1785d522f475Smrg
1786d522f475Smrg		TRACE(("CASE_TRACK_MOUSE\n"));
1787d522f475Smrg		/* Track mouse as long as in window and between
1788d522f475Smrg		 * specified rows
1789d522f475Smrg		 */
1790d522f475Smrg		start.row = param[2] - 1;
1791d522f475Smrg		start.col = param[1] - 1;
1792d522f475Smrg		TrackMouse(xw,
1793d522f475Smrg			   param[0],
1794d522f475Smrg			   &start,
1795d522f475Smrg			   param[3] - 1, param[4] - 2);
1796d522f475Smrg	    } else {
1797d522f475Smrg		TRACE(("CASE_SD - scroll down\n"));
1798d522f475Smrg		/* SD */
1799d522f475Smrg		if ((count = param[0]) < 1)
1800d522f475Smrg		    count = 1;
1801d522f475Smrg		RevScroll(xw, count);
1802d522f475Smrg		do_xevents();
1803d522f475Smrg	    }
1804d522f475Smrg	    sp->parsestate = sp->groundtable;
1805d522f475Smrg	    break;
1806d522f475Smrg
1807d522f475Smrg	case CASE_DECID:
1808d522f475Smrg	    TRACE(("CASE_DECID\n"));
1809d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1810d522f475Smrg		unparseputc(xw, ANSI_ESC);
1811d522f475Smrg		unparseputc(xw, '/');
1812d522f475Smrg		unparseputc(xw, 'Z');
1813d522f475Smrg		unparse_end(xw);
1814d522f475Smrg		sp->parsestate = sp->groundtable;
1815d522f475Smrg		break;
1816d522f475Smrg	    });
1817d522f475Smrg	    param[0] = DEFAULT;	/* Default ID parameter */
1818d522f475Smrg	    /* FALLTHRU */
1819d522f475Smrg	case CASE_DA1:
1820d522f475Smrg	    TRACE(("CASE_DA1\n"));
1821d522f475Smrg	    if (param[0] <= 0) {	/* less than means DEFAULT */
1822d522f475Smrg		count = 0;
1823d522f475Smrg		reply.a_type = ANSI_CSI;
1824d522f475Smrg		reply.a_pintro = '?';
1825d522f475Smrg
1826d522f475Smrg		/* The first param corresponds to the highest
1827d522f475Smrg		 * operating level (i.e., service level) of the
1828d522f475Smrg		 * emulation.  A DEC terminal can be setup to
1829d522f475Smrg		 * respond with a different DA response, but
1830d522f475Smrg		 * there's no control sequence that modifies this.
1831d522f475Smrg		 * We set it via a resource.
1832d522f475Smrg		 */
1833d522f475Smrg		if (screen->terminal_id < 200) {
1834d522f475Smrg		    switch (screen->terminal_id) {
1835d522f475Smrg		    case 102:
1836d522f475Smrg			reply.a_param[count++] = 6;	/* VT102 */
1837d522f475Smrg			break;
1838d522f475Smrg		    case 101:
1839d522f475Smrg			reply.a_param[count++] = 1;	/* VT101 */
1840d522f475Smrg			reply.a_param[count++] = 0;	/* no options */
1841d522f475Smrg			break;
1842d522f475Smrg		    default:	/* VT100 */
1843d522f475Smrg			reply.a_param[count++] = 1;	/* VT100 */
1844d522f475Smrg			reply.a_param[count++] = 2;	/* AVO */
1845d522f475Smrg			break;
1846d522f475Smrg		    }
1847d522f475Smrg		} else {
1848d522f475Smrg		    reply.a_param[count++] = 60 + screen->terminal_id / 100;
1849d522f475Smrg		    reply.a_param[count++] = 1;		/* 132-columns */
1850d522f475Smrg		    reply.a_param[count++] = 2;		/* printer */
1851d522f475Smrg		    reply.a_param[count++] = 6;		/* selective-erase */
1852d522f475Smrg#if OPT_SUNPC_KBD
1853d522f475Smrg		    if (xw->keyboard.type == keyboardIsVT220)
1854d522f475Smrg#endif
1855d522f475Smrg			reply.a_param[count++] = 8;	/* user-defined-keys */
1856d522f475Smrg		    reply.a_param[count++] = 9;		/* national replacement charsets */
1857d522f475Smrg		    reply.a_param[count++] = 15;	/* technical characters */
1858d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1859d522f475Smrg			reply.a_param[count++] = 22;	/* ANSI color, VT525 */
1860d522f475Smrg		    });
1861d522f475Smrg#if OPT_DEC_LOCATOR
1862d522f475Smrg		    reply.a_param[count++] = 29;	/* ANSI text locator */
1863d522f475Smrg#endif
1864d522f475Smrg		}
1865d522f475Smrg		reply.a_nparam = count;
1866d522f475Smrg		reply.a_inters = 0;
1867d522f475Smrg		reply.a_final = 'c';
1868d522f475Smrg		unparseseq(xw, &reply);
1869d522f475Smrg	    }
1870d522f475Smrg	    sp->parsestate = sp->groundtable;
1871d522f475Smrg	    break;
1872d522f475Smrg
1873d522f475Smrg	case CASE_DA2:
1874d522f475Smrg	    TRACE(("CASE_DA2\n"));
1875d522f475Smrg	    if (param[0] <= 0) {	/* less than means DEFAULT */
1876d522f475Smrg		count = 0;
1877d522f475Smrg		reply.a_type = ANSI_CSI;
1878d522f475Smrg		reply.a_pintro = '>';
1879d522f475Smrg
1880d522f475Smrg		if (screen->terminal_id >= 200)
1881d522f475Smrg		    reply.a_param[count++] = 1;		/* VT220 */
1882d522f475Smrg		else
1883d522f475Smrg		    reply.a_param[count++] = 0;		/* VT100 (nonstandard) */
1884d522f475Smrg		reply.a_param[count++] = XTERM_PATCH;	/* Version */
1885d522f475Smrg		reply.a_param[count++] = 0;	/* options (none) */
1886d522f475Smrg		reply.a_nparam = count;
1887d522f475Smrg		reply.a_inters = 0;
1888d522f475Smrg		reply.a_final = 'c';
1889d522f475Smrg		unparseseq(xw, &reply);
1890d522f475Smrg	    }
1891d522f475Smrg	    sp->parsestate = sp->groundtable;
1892d522f475Smrg	    break;
1893d522f475Smrg
1894d522f475Smrg	case CASE_DECRPTUI:
1895d522f475Smrg	    TRACE(("CASE_DECRPTUI\n"));
1896d522f475Smrg	    if ((screen->terminal_id >= 400)
1897d522f475Smrg		&& (param[0] <= 0)) {	/* less than means DEFAULT */
1898d522f475Smrg		unparseputc1(xw, ANSI_DCS);
1899d522f475Smrg		unparseputc(xw, '!');
1900d522f475Smrg		unparseputc(xw, '|');
1901d522f475Smrg		unparseputc(xw, '0');
1902d522f475Smrg		unparseputc1(xw, ANSI_ST);
1903d522f475Smrg		unparse_end(xw);
1904d522f475Smrg	    }
1905d522f475Smrg	    sp->parsestate = sp->groundtable;
1906d522f475Smrg	    break;
1907d522f475Smrg
1908d522f475Smrg	case CASE_TBC:
1909d522f475Smrg	    TRACE(("CASE_TBC - tab clear\n"));
1910d522f475Smrg	    if ((row = param[0]) <= 0)	/* less than means default */
1911d522f475Smrg		TabClear(xw->tabs, screen->cur_col);
1912d522f475Smrg	    else if (row == 3)
1913d522f475Smrg		TabZonk(xw->tabs);
1914d522f475Smrg	    sp->parsestate = sp->groundtable;
1915d522f475Smrg	    break;
1916d522f475Smrg
1917d522f475Smrg	case CASE_SET:
1918d522f475Smrg	    TRACE(("CASE_SET - set mode\n"));
1919d522f475Smrg	    ansi_modes(xw, bitset);
1920d522f475Smrg	    sp->parsestate = sp->groundtable;
1921d522f475Smrg	    break;
1922d522f475Smrg
1923d522f475Smrg	case CASE_RST:
1924d522f475Smrg	    TRACE(("CASE_RST - reset mode\n"));
1925d522f475Smrg	    ansi_modes(xw, bitclr);
1926d522f475Smrg	    sp->parsestate = sp->groundtable;
1927d522f475Smrg	    break;
1928d522f475Smrg
1929d522f475Smrg	case CASE_SGR:
1930d522f475Smrg	    for (row = 0; row < nparam; ++row) {
1931d522f475Smrg		if_OPT_XMC_GLITCH(screen, {
1932d522f475Smrg		    Mark_XMC(xw, param[row]);
1933d522f475Smrg		});
1934d522f475Smrg		TRACE(("CASE_SGR %d\n", param[row]));
1935d522f475Smrg		switch (param[row]) {
1936d522f475Smrg		case DEFAULT:
1937d522f475Smrg		case 0:
1938d522f475Smrg		    xw->flags &=
1939d522f475Smrg			~(INVERSE | BOLD | BLINK | UNDERLINE | INVISIBLE);
1940d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1941d522f475Smrg			reset_SGR_Colors(xw);
1942d522f475Smrg		    });
1943d522f475Smrg		    break;
1944d522f475Smrg		case 1:	/* Bold                 */
1945d522f475Smrg		    xw->flags |= BOLD;
1946d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1947d522f475Smrg			setExtendedFG(xw);
1948d522f475Smrg		    });
1949d522f475Smrg		    break;
1950d522f475Smrg		case 5:	/* Blink                */
1951d522f475Smrg		    xw->flags |= BLINK;
1952d522f475Smrg		    StartBlinking(screen);
1953d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1954d522f475Smrg			setExtendedFG(xw);
1955d522f475Smrg		    });
1956d522f475Smrg		    break;
1957d522f475Smrg		case 4:	/* Underscore           */
1958d522f475Smrg		    xw->flags |= UNDERLINE;
1959d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1960d522f475Smrg			setExtendedFG(xw);
1961d522f475Smrg		    });
1962d522f475Smrg		    break;
1963d522f475Smrg		case 7:
1964d522f475Smrg		    xw->flags |= INVERSE;
1965d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1966d522f475Smrg			setExtendedBG(xw);
1967d522f475Smrg		    });
1968d522f475Smrg		    break;
1969d522f475Smrg		case 8:
1970d522f475Smrg		    xw->flags |= INVISIBLE;
1971d522f475Smrg		    break;
1972d522f475Smrg		case 22:	/* reset 'bold' */
1973d522f475Smrg		    xw->flags &= ~BOLD;
1974d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1975d522f475Smrg			setExtendedFG(xw);
1976d522f475Smrg		    });
1977d522f475Smrg		    break;
1978d522f475Smrg		case 24:
1979d522f475Smrg		    xw->flags &= ~UNDERLINE;
1980d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1981d522f475Smrg			setExtendedFG(xw);
1982d522f475Smrg		    });
1983d522f475Smrg		    break;
1984d522f475Smrg		case 25:	/* reset 'blink' */
1985d522f475Smrg		    xw->flags &= ~BLINK;
1986d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1987d522f475Smrg			setExtendedFG(xw);
1988d522f475Smrg		    });
1989d522f475Smrg		    break;
1990d522f475Smrg		case 27:
1991d522f475Smrg		    xw->flags &= ~INVERSE;
1992d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
1993d522f475Smrg			setExtendedBG(xw);
1994d522f475Smrg		    });
1995d522f475Smrg		    break;
1996d522f475Smrg		case 28:
1997d522f475Smrg		    xw->flags &= ~INVISIBLE;
1998d522f475Smrg		    break;
1999d522f475Smrg		case 30:
2000d522f475Smrg		case 31:
2001d522f475Smrg		case 32:
2002d522f475Smrg		case 33:
2003d522f475Smrg		case 34:
2004d522f475Smrg		case 35:
2005d522f475Smrg		case 36:
2006d522f475Smrg		case 37:
2007d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2008d522f475Smrg			xw->sgr_foreground = (param[row] - 30);
2009d522f475Smrg			xw->sgr_extended = False;
2010d522f475Smrg			setExtendedFG(xw);
2011d522f475Smrg		    });
2012d522f475Smrg		    break;
2013d522f475Smrg		case 38:
2014d522f475Smrg		    /* This is more complicated than I'd
2015d522f475Smrg		       like, but it should properly eat all
2016d522f475Smrg		       the parameters for unsupported modes
2017d522f475Smrg		     */
2018d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2019d522f475Smrg			row++;
2020d522f475Smrg			if (row < nparam) {
2021d522f475Smrg			    switch (param[row]) {
2022d522f475Smrg			    case 5:
2023d522f475Smrg				row++;
2024d522f475Smrg				if (row < nparam &&
2025d522f475Smrg				    param[row] < NUM_ANSI_COLORS) {
2026d522f475Smrg				    xw->sgr_foreground = param[row];
2027d522f475Smrg				    xw->sgr_extended = True;
2028d522f475Smrg				    setExtendedFG(xw);
2029d522f475Smrg				}
2030d522f475Smrg				break;
2031d522f475Smrg			    default:
2032d522f475Smrg				row += 7;
2033d522f475Smrg				break;
2034d522f475Smrg			    }
2035d522f475Smrg			}
2036d522f475Smrg		    });
2037d522f475Smrg		    break;
2038d522f475Smrg		case 39:
2039d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2040d522f475Smrg			reset_SGR_Foreground(xw);
2041d522f475Smrg		    });
2042d522f475Smrg		    break;
2043d522f475Smrg		case 40:
2044d522f475Smrg		case 41:
2045d522f475Smrg		case 42:
2046d522f475Smrg		case 43:
2047d522f475Smrg		case 44:
2048d522f475Smrg		case 45:
2049d522f475Smrg		case 46:
2050d522f475Smrg		case 47:
2051d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2052d522f475Smrg			xw->sgr_background = (param[row] - 40);
2053d522f475Smrg			setExtendedBG(xw);
2054d522f475Smrg		    });
2055d522f475Smrg		    break;
2056d522f475Smrg		case 48:
2057d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2058d522f475Smrg			row++;
2059d522f475Smrg			if (row < nparam) {
2060d522f475Smrg			    switch (param[row]) {
2061d522f475Smrg			    case 5:
2062d522f475Smrg				row++;
2063d522f475Smrg				if (row < nparam &&
2064d522f475Smrg				    param[row] < NUM_ANSI_COLORS) {
2065d522f475Smrg				    xw->sgr_background = param[row];
2066d522f475Smrg				    setExtendedBG(xw);
2067d522f475Smrg				}
2068d522f475Smrg				break;
2069d522f475Smrg			    default:
2070d522f475Smrg				row += 7;
2071d522f475Smrg				break;
2072d522f475Smrg			    }
2073d522f475Smrg			}
2074d522f475Smrg		    });
2075d522f475Smrg		    break;
2076d522f475Smrg		case 49:
2077d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2078d522f475Smrg			reset_SGR_Background(xw);
2079d522f475Smrg		    });
2080d522f475Smrg		    break;
2081d522f475Smrg		case 90:
2082d522f475Smrg		case 91:
2083d522f475Smrg		case 92:
2084d522f475Smrg		case 93:
2085d522f475Smrg		case 94:
2086d522f475Smrg		case 95:
2087d522f475Smrg		case 96:
2088d522f475Smrg		case 97:
2089d522f475Smrg		    if_OPT_AIX_COLORS(screen, {
2090d522f475Smrg			xw->sgr_foreground = (param[row] - 90 + 8);
2091d522f475Smrg			xw->sgr_extended = False;
2092d522f475Smrg			setExtendedFG(xw);
2093d522f475Smrg		    });
2094d522f475Smrg		    break;
2095d522f475Smrg		case 100:
2096d522f475Smrg#if !OPT_AIX_COLORS
2097d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2098d522f475Smrg			reset_SGR_Foreground(xw);
2099d522f475Smrg			reset_SGR_Background(xw);
2100d522f475Smrg		    });
2101d522f475Smrg		    break;
2102d522f475Smrg#endif
2103d522f475Smrg		case 101:
2104d522f475Smrg		case 102:
2105d522f475Smrg		case 103:
2106d522f475Smrg		case 104:
2107d522f475Smrg		case 105:
2108d522f475Smrg		case 106:
2109d522f475Smrg		case 107:
2110d522f475Smrg		    if_OPT_AIX_COLORS(screen, {
2111d522f475Smrg			xw->sgr_background = (param[row] - 100 + 8);
2112d522f475Smrg			setExtendedBG(xw);
2113d522f475Smrg		    });
2114d522f475Smrg		    break;
2115d522f475Smrg		}
2116d522f475Smrg	    }
2117d522f475Smrg	    sp->parsestate = sp->groundtable;
2118d522f475Smrg	    break;
2119d522f475Smrg
2120d522f475Smrg	    /* DSR (except for the '?') is a superset of CPR */
2121d522f475Smrg	case CASE_DSR:
2122d522f475Smrg	    sp->private_function = True;
2123d522f475Smrg
2124d522f475Smrg	    /* FALLTHRU */
2125d522f475Smrg	case CASE_CPR:
2126d522f475Smrg	    TRACE(("CASE_CPR - cursor position\n"));
2127d522f475Smrg	    count = 0;
2128d522f475Smrg	    reply.a_type = ANSI_CSI;
2129d522f475Smrg	    reply.a_pintro = sp->private_function ? '?' : 0;
2130d522f475Smrg	    reply.a_inters = 0;
2131d522f475Smrg	    reply.a_final = 'n';
2132d522f475Smrg
2133d522f475Smrg	    switch (param[0]) {
2134d522f475Smrg	    case 5:
2135d522f475Smrg		/* operating status */
2136d522f475Smrg		reply.a_param[count++] = 0;	/* (no malfunction ;-) */
2137d522f475Smrg		break;
2138d522f475Smrg	    case 6:
2139d522f475Smrg		/* CPR */
2140d522f475Smrg		/* DECXCPR (with page=0) */
2141d522f475Smrg		reply.a_param[count++] = screen->cur_row + 1;
2142d522f475Smrg		reply.a_param[count++] = screen->cur_col + 1;
2143d522f475Smrg		reply.a_final = 'R';
2144d522f475Smrg		break;
2145d522f475Smrg	    case 15:
2146d522f475Smrg		/* printer status */
2147d522f475Smrg		reply.a_param[count++] = 13;	/* implement printer */
2148d522f475Smrg		break;
2149d522f475Smrg	    case 25:
2150d522f475Smrg		/* UDK status */
2151d522f475Smrg		reply.a_param[count++] = 20;	/* UDK always unlocked */
2152d522f475Smrg		break;
2153d522f475Smrg	    case 26:
2154d522f475Smrg		/* keyboard status */
2155d522f475Smrg		reply.a_param[count++] = 27;
2156d522f475Smrg		reply.a_param[count++] = 1;	/* North American */
2157d522f475Smrg		if (screen->terminal_id >= 400) {
2158d522f475Smrg		    reply.a_param[count++] = 0;		/* ready */
2159d522f475Smrg		    reply.a_param[count++] = 0;		/* LK201 */
2160d522f475Smrg		}
2161d522f475Smrg		break;
2162d522f475Smrg	    case 53:
2163d522f475Smrg		/* Locator status */
2164d522f475Smrg#if OPT_DEC_LOCATOR
2165d522f475Smrg		reply.a_param[count++] = 50;	/* locator ready */
2166d522f475Smrg#else
2167d522f475Smrg		reply.a_param[count++] = 53;	/* no locator */
2168d522f475Smrg#endif
2169d522f475Smrg		break;
2170d522f475Smrg	    }
2171d522f475Smrg
2172d522f475Smrg	    if ((reply.a_nparam = count) != 0)
2173d522f475Smrg		unparseseq(xw, &reply);
2174d522f475Smrg
2175d522f475Smrg	    sp->parsestate = sp->groundtable;
2176d522f475Smrg	    sp->private_function = False;
2177d522f475Smrg	    break;
2178d522f475Smrg
2179d522f475Smrg	case CASE_MC:
2180d522f475Smrg	    TRACE(("CASE_MC - media control\n"));
2181d522f475Smrg	    xtermMediaControl(param[0], False);
2182d522f475Smrg	    sp->parsestate = sp->groundtable;
2183d522f475Smrg	    break;
2184d522f475Smrg
2185d522f475Smrg	case CASE_DEC_MC:
2186d522f475Smrg	    TRACE(("CASE_DEC_MC - DEC media control\n"));
2187d522f475Smrg	    xtermMediaControl(param[0], True);
2188d522f475Smrg	    sp->parsestate = sp->groundtable;
2189d522f475Smrg	    break;
2190d522f475Smrg
2191d522f475Smrg	case CASE_HP_MEM_LOCK:
2192d522f475Smrg	case CASE_HP_MEM_UNLOCK:
2193d522f475Smrg	    TRACE(("%s\n", ((sp->parsestate[c] == CASE_HP_MEM_LOCK)
2194d522f475Smrg			    ? "CASE_HP_MEM_LOCK"
2195d522f475Smrg			    : "CASE_HP_MEM_UNLOCK")));
2196d522f475Smrg	    if (screen->scroll_amt)
2197d522f475Smrg		FlushScroll(xw);
2198d522f475Smrg	    if (sp->parsestate[c] == CASE_HP_MEM_LOCK)
2199d522f475Smrg		set_tb_margins(screen, screen->cur_row, screen->bot_marg);
2200d522f475Smrg	    else
2201d522f475Smrg		set_tb_margins(screen, 0, screen->bot_marg);
2202d522f475Smrg	    sp->parsestate = sp->groundtable;
2203d522f475Smrg	    break;
2204d522f475Smrg
2205d522f475Smrg	case CASE_DECSTBM:
2206d522f475Smrg	    TRACE(("CASE_DECSTBM - set scrolling region\n"));
2207d522f475Smrg	    if ((top = param[0]) < 1)
2208d522f475Smrg		top = 1;
2209d522f475Smrg	    if (nparam < 2 || (bot = param[1]) == DEFAULT
2210d522f475Smrg		|| bot > MaxRows(screen)
2211d522f475Smrg		|| bot == 0)
2212d522f475Smrg		bot = MaxRows(screen);
2213d522f475Smrg	    if (bot > top) {
2214d522f475Smrg		if (screen->scroll_amt)
2215d522f475Smrg		    FlushScroll(xw);
2216d522f475Smrg		set_tb_margins(screen, top - 1, bot - 1);
2217d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
2218d522f475Smrg	    }
2219d522f475Smrg	    sp->parsestate = sp->groundtable;
2220d522f475Smrg	    break;
2221d522f475Smrg
2222d522f475Smrg	case CASE_DECREQTPARM:
2223d522f475Smrg	    TRACE(("CASE_DECREQTPARM\n"));
2224d522f475Smrg	    if (screen->terminal_id < 200) {	/* VT102 */
2225d522f475Smrg		if ((row = param[0]) == DEFAULT)
2226d522f475Smrg		    row = 0;
2227d522f475Smrg		if (row == 0 || row == 1) {
2228d522f475Smrg		    reply.a_type = ANSI_CSI;
2229d522f475Smrg		    reply.a_pintro = 0;
2230d522f475Smrg		    reply.a_nparam = 7;
2231d522f475Smrg		    reply.a_param[0] = row + 2;
2232d522f475Smrg		    reply.a_param[1] = 1;	/* no parity */
2233d522f475Smrg		    reply.a_param[2] = 1;	/* eight bits */
2234d522f475Smrg		    reply.a_param[3] = 128;	/* transmit 38.4k baud */
2235d522f475Smrg		    reply.a_param[4] = 128;	/* receive 38.4k baud */
2236d522f475Smrg		    reply.a_param[5] = 1;	/* clock multiplier ? */
2237d522f475Smrg		    reply.a_param[6] = 0;	/* STP flags ? */
2238d522f475Smrg		    reply.a_inters = 0;
2239d522f475Smrg		    reply.a_final = 'x';
2240d522f475Smrg		    unparseseq(xw, &reply);
2241d522f475Smrg		}
2242d522f475Smrg	    }
2243d522f475Smrg	    sp->parsestate = sp->groundtable;
2244d522f475Smrg	    break;
2245d522f475Smrg
2246d522f475Smrg	case CASE_DECSET:
2247d522f475Smrg	    /* DECSET */
2248d522f475Smrg#if OPT_VT52_MODE
2249d522f475Smrg	    if (screen->vtXX_level != 0)
2250d522f475Smrg#endif
2251d522f475Smrg		dpmodes(xw, bitset);
2252d522f475Smrg	    sp->parsestate = sp->groundtable;
2253d522f475Smrg#if OPT_TEK4014
2254d522f475Smrg	    if (TEK4014_ACTIVE(xw))
2255d522f475Smrg		return False;
2256d522f475Smrg#endif
2257d522f475Smrg	    break;
2258d522f475Smrg
2259d522f475Smrg	case CASE_DECRST:
2260d522f475Smrg	    /* DECRST */
2261d522f475Smrg	    dpmodes(xw, bitclr);
2262d522f475Smrg#if OPT_VT52_MODE
2263d522f475Smrg	    if (screen->vtXX_level == 0)
2264d522f475Smrg		sp->groundtable = vt52_table;
2265d522f475Smrg	    else if (screen->terminal_id >= 100)
2266d522f475Smrg		sp->groundtable = ansi_table;
2267d522f475Smrg#endif
2268d522f475Smrg	    sp->parsestate = sp->groundtable;
2269d522f475Smrg	    break;
2270d522f475Smrg
2271d522f475Smrg	case CASE_DECALN:
2272d522f475Smrg	    TRACE(("CASE_DECALN - alignment test\n"));
2273d522f475Smrg	    if (screen->cursor_state)
2274d522f475Smrg		HideCursor();
2275d522f475Smrg	    set_tb_margins(screen, 0, screen->max_row);
2276d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
2277d522f475Smrg	    xtermParseRect(xw, 0, 0, &myRect);
2278d522f475Smrg	    ScrnFillRectangle(xw, &myRect, 'E', 0, False);
2279d522f475Smrg	    sp->parsestate = sp->groundtable;
2280d522f475Smrg	    break;
2281d522f475Smrg
2282d522f475Smrg	case CASE_GSETS:
2283d522f475Smrg	    TRACE(("CASE_GSETS(%d) = '%c'\n", sp->scstype, c));
2284d522f475Smrg	    if (screen->vtXX_level != 0)
2285d522f475Smrg		screen->gsets[sp->scstype] = c;
2286d522f475Smrg	    sp->parsestate = sp->groundtable;
2287d522f475Smrg	    break;
2288d522f475Smrg
2289d522f475Smrg	case CASE_DECSC:
2290d522f475Smrg	    TRACE(("CASE_DECSC - save cursor\n"));
2291d522f475Smrg	    CursorSave(xw);
2292d522f475Smrg	    sp->parsestate = sp->groundtable;
2293d522f475Smrg	    break;
2294d522f475Smrg
2295d522f475Smrg	case CASE_DECRC:
2296d522f475Smrg	    TRACE(("CASE_DECRC - restore cursor\n"));
2297d522f475Smrg	    CursorRestore(xw);
2298d522f475Smrg	    if_OPT_ISO_COLORS(screen, {
2299d522f475Smrg		setExtendedFG(xw);
2300d522f475Smrg	    });
2301d522f475Smrg	    sp->parsestate = sp->groundtable;
2302d522f475Smrg	    break;
2303d522f475Smrg
2304d522f475Smrg	case CASE_DECKPAM:
2305d522f475Smrg	    TRACE(("CASE_DECKPAM\n"));
2306d522f475Smrg	    xw->keyboard.flags |= MODE_DECKPAM;
2307d522f475Smrg	    update_appkeypad();
2308d522f475Smrg	    sp->parsestate = sp->groundtable;
2309d522f475Smrg	    break;
2310d522f475Smrg
2311d522f475Smrg	case CASE_DECKPNM:
2312d522f475Smrg	    TRACE(("CASE_DECKPNM\n"));
2313d522f475Smrg	    xw->keyboard.flags &= ~MODE_DECKPAM;
2314d522f475Smrg	    update_appkeypad();
2315d522f475Smrg	    sp->parsestate = sp->groundtable;
2316d522f475Smrg	    break;
2317d522f475Smrg
2318d522f475Smrg	case CASE_CSI_QUOTE_STATE:
2319d522f475Smrg	    sp->parsestate = csi_quo_table;
2320d522f475Smrg	    break;
2321d522f475Smrg
2322d522f475Smrg#if OPT_VT52_MODE
2323d522f475Smrg	case CASE_VT52_FINISH:
2324d522f475Smrg	    TRACE(("CASE_VT52_FINISH terminal_id %d, vtXX_level %d\n",
2325d522f475Smrg		   screen->terminal_id,
2326d522f475Smrg		   screen->vtXX_level));
2327d522f475Smrg	    if (screen->terminal_id >= 100
2328d522f475Smrg		&& screen->vtXX_level == 0) {
2329d522f475Smrg		sp->groundtable =
2330d522f475Smrg		    sp->parsestate = ansi_table;
2331d522f475Smrg		screen->vtXX_level = screen->vt52_save_level;
2332d522f475Smrg		screen->curgl = screen->vt52_save_curgl;
2333d522f475Smrg		screen->curgr = screen->vt52_save_curgr;
2334d522f475Smrg		screen->curss = screen->vt52_save_curss;
2335d522f475Smrg		memmove(screen->gsets, screen->vt52_save_gsets, sizeof(screen->gsets));
2336d522f475Smrg	    }
2337d522f475Smrg	    break;
2338d522f475Smrg#endif
2339d522f475Smrg
2340d522f475Smrg	case CASE_ANSI_LEVEL_1:
2341d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_1\n"));
2342d522f475Smrg	    set_ansi_conformance(screen, 1);
2343d522f475Smrg	    sp->parsestate = sp->groundtable;
2344d522f475Smrg	    break;
2345d522f475Smrg
2346d522f475Smrg	case CASE_ANSI_LEVEL_2:
2347d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_2\n"));
2348d522f475Smrg	    set_ansi_conformance(screen, 2);
2349d522f475Smrg	    sp->parsestate = sp->groundtable;
2350d522f475Smrg	    break;
2351d522f475Smrg
2352d522f475Smrg	case CASE_ANSI_LEVEL_3:
2353d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_3\n"));
2354d522f475Smrg	    set_ansi_conformance(screen, 3);
2355d522f475Smrg	    sp->parsestate = sp->groundtable;
2356d522f475Smrg	    break;
2357d522f475Smrg
2358d522f475Smrg	case CASE_DECSCL:
2359d522f475Smrg	    TRACE(("CASE_DECSCL(%d,%d)\n", param[0], param[1]));
2360d522f475Smrg	    if (param[0] >= 61 && param[0] <= 65) {
2361d522f475Smrg		/*
2362d522f475Smrg		 * VT300, VT420, VT520 manuals claim that DECSCL does a hard
2363d522f475Smrg		 * reset (RIS).  VT220 manual states that it is a soft reset.
2364d522f475Smrg		 * Perhaps both are right (unlikely).  Kermit says it's soft.
2365d522f475Smrg		 */
2366d522f475Smrg		VTReset(xw, False, False);
2367d522f475Smrg		screen->vtXX_level = param[0] - 60;
2368d522f475Smrg		if (param[0] > 61) {
2369d522f475Smrg		    if (param[1] == 1)
2370d522f475Smrg			show_8bit_control(False);
2371d522f475Smrg		    else if (param[1] == 0 || param[1] == 2)
2372d522f475Smrg			show_8bit_control(True);
2373d522f475Smrg		}
2374d522f475Smrg	    }
2375d522f475Smrg	    sp->parsestate = sp->groundtable;
2376d522f475Smrg	    break;
2377d522f475Smrg
2378d522f475Smrg	case CASE_DECSCA:
2379d522f475Smrg	    TRACE(("CASE_DECSCA\n"));
2380d522f475Smrg	    screen->protected_mode = DEC_PROTECT;
2381d522f475Smrg	    if (param[0] <= 0 || param[0] == 2)
2382d522f475Smrg		xw->flags &= ~PROTECTED;
2383d522f475Smrg	    else if (param[0] == 1)
2384d522f475Smrg		xw->flags |= PROTECTED;
2385d522f475Smrg	    sp->parsestate = sp->groundtable;
2386d522f475Smrg	    break;
2387d522f475Smrg
2388d522f475Smrg	case CASE_DECSED:
2389d522f475Smrg	    TRACE(("CASE_DECSED\n"));
2390d522f475Smrg	    do_erase_display(xw, param[0], DEC_PROTECT);
2391d522f475Smrg	    sp->parsestate = sp->groundtable;
2392d522f475Smrg	    break;
2393d522f475Smrg
2394d522f475Smrg	case CASE_DECSEL:
2395d522f475Smrg	    TRACE(("CASE_DECSEL\n"));
2396d522f475Smrg	    do_erase_line(xw, param[0], DEC_PROTECT);
2397d522f475Smrg	    sp->parsestate = sp->groundtable;
2398d522f475Smrg	    break;
2399d522f475Smrg
2400d522f475Smrg	case CASE_ST:
2401d522f475Smrg	    TRACE(("CASE_ST: End of String (%d bytes)\n", string_used));
2402d522f475Smrg	    sp->parsestate = sp->groundtable;
2403d522f475Smrg	    if (!string_used)
2404d522f475Smrg		break;
2405d522f475Smrg	    string_area[--string_used] = '\0';
2406d522f475Smrg	    switch (sp->string_mode) {
2407d522f475Smrg	    case ANSI_APC:
2408d522f475Smrg		/* ignored */
2409d522f475Smrg		break;
2410d522f475Smrg	    case ANSI_DCS:
2411d522f475Smrg		do_dcs(xw, string_area, string_used);
2412d522f475Smrg		break;
2413d522f475Smrg	    case ANSI_OSC:
2414d522f475Smrg		do_osc(xw, string_area, string_used, ANSI_ST);
2415d522f475Smrg		break;
2416d522f475Smrg	    case ANSI_PM:
2417d522f475Smrg		/* ignored */
2418d522f475Smrg		break;
2419d522f475Smrg	    case ANSI_SOS:
2420d522f475Smrg		/* ignored */
2421d522f475Smrg		break;
2422d522f475Smrg	    }
2423d522f475Smrg	    break;
2424d522f475Smrg
2425d522f475Smrg	case CASE_SOS:
2426d522f475Smrg	    TRACE(("CASE_SOS: Start of String\n"));
2427d522f475Smrg	    sp->string_mode = ANSI_SOS;
2428d522f475Smrg	    sp->parsestate = sos_table;
2429d522f475Smrg	    break;
2430d522f475Smrg
2431d522f475Smrg	case CASE_PM:
2432d522f475Smrg	    TRACE(("CASE_PM: Privacy Message\n"));
2433d522f475Smrg	    sp->string_mode = ANSI_PM;
2434d522f475Smrg	    sp->parsestate = sos_table;
2435d522f475Smrg	    break;
2436d522f475Smrg
2437d522f475Smrg	case CASE_DCS:
2438d522f475Smrg	    TRACE(("CASE_DCS: Device Control String\n"));
2439d522f475Smrg	    sp->string_mode = ANSI_DCS;
2440d522f475Smrg	    sp->parsestate = sos_table;
2441d522f475Smrg	    break;
2442d522f475Smrg
2443d522f475Smrg	case CASE_APC:
2444d522f475Smrg	    TRACE(("CASE_APC: Application Program Command\n"));
2445d522f475Smrg	    sp->string_mode = ANSI_APC;
2446d522f475Smrg	    sp->parsestate = sos_table;
2447d522f475Smrg	    break;
2448d522f475Smrg
2449d522f475Smrg	case CASE_SPA:
2450d522f475Smrg	    TRACE(("CASE_SPA - start protected area\n"));
2451d522f475Smrg	    screen->protected_mode = ISO_PROTECT;
2452d522f475Smrg	    xw->flags |= PROTECTED;
2453d522f475Smrg	    sp->parsestate = sp->groundtable;
2454d522f475Smrg	    break;
2455d522f475Smrg
2456d522f475Smrg	case CASE_EPA:
2457d522f475Smrg	    TRACE(("CASE_EPA - end protected area\n"));
2458d522f475Smrg	    xw->flags &= ~PROTECTED;
2459d522f475Smrg	    sp->parsestate = sp->groundtable;
2460d522f475Smrg	    break;
2461d522f475Smrg
2462d522f475Smrg	case CASE_SU:
2463d522f475Smrg	    TRACE(("CASE_SU - scroll up\n"));
2464d522f475Smrg	    if ((count = param[0]) < 1)
2465d522f475Smrg		count = 1;
2466d522f475Smrg	    xtermScroll(xw, count);
2467d522f475Smrg	    sp->parsestate = sp->groundtable;
2468d522f475Smrg	    break;
2469d522f475Smrg
2470d522f475Smrg	case CASE_IND:
2471d522f475Smrg	    TRACE(("CASE_IND - index\n"));
2472d522f475Smrg	    xtermIndex(xw, 1);
2473d522f475Smrg	    do_xevents();
2474d522f475Smrg	    sp->parsestate = sp->groundtable;
2475d522f475Smrg	    break;
2476d522f475Smrg
2477d522f475Smrg	case CASE_CPL:
2478d522f475Smrg	    TRACE(("CASE_CPL - cursor prev line\n"));
2479d522f475Smrg	    CursorPrevLine(screen, param[0]);
2480d522f475Smrg	    sp->parsestate = sp->groundtable;
2481d522f475Smrg	    break;
2482d522f475Smrg
2483d522f475Smrg	case CASE_CNL:
2484d522f475Smrg	    TRACE(("CASE_CNL - cursor next line\n"));
2485d522f475Smrg	    CursorNextLine(screen, param[0]);
2486d522f475Smrg	    sp->parsestate = sp->groundtable;
2487d522f475Smrg	    break;
2488d522f475Smrg
2489d522f475Smrg	case CASE_NEL:
2490d522f475Smrg	    TRACE(("CASE_NEL\n"));
2491d522f475Smrg	    xtermIndex(xw, 1);
2492d522f475Smrg	    CarriageReturn(screen);
2493d522f475Smrg	    sp->parsestate = sp->groundtable;
2494d522f475Smrg	    break;
2495d522f475Smrg
2496d522f475Smrg	case CASE_HTS:
2497d522f475Smrg	    TRACE(("CASE_HTS - horizontal tab set\n"));
2498d522f475Smrg	    TabSet(xw->tabs, screen->cur_col);
2499d522f475Smrg	    sp->parsestate = sp->groundtable;
2500d522f475Smrg	    break;
2501d522f475Smrg
2502d522f475Smrg	case CASE_RI:
2503d522f475Smrg	    TRACE(("CASE_RI - reverse index\n"));
2504d522f475Smrg	    RevIndex(xw, 1);
2505d522f475Smrg	    sp->parsestate = sp->groundtable;
2506d522f475Smrg	    break;
2507d522f475Smrg
2508d522f475Smrg	case CASE_SS2:
2509d522f475Smrg	    TRACE(("CASE_SS2\n"));
2510d522f475Smrg	    screen->curss = 2;
2511d522f475Smrg	    sp->parsestate = sp->groundtable;
2512d522f475Smrg	    break;
2513d522f475Smrg
2514d522f475Smrg	case CASE_SS3:
2515d522f475Smrg	    TRACE(("CASE_SS3\n"));
2516d522f475Smrg	    screen->curss = 3;
2517d522f475Smrg	    sp->parsestate = sp->groundtable;
2518d522f475Smrg	    break;
2519d522f475Smrg
2520d522f475Smrg	case CASE_CSI_STATE:
2521d522f475Smrg	    /* enter csi state */
2522d522f475Smrg	    nparam = 1;
2523d522f475Smrg	    param[0] = DEFAULT;
2524d522f475Smrg	    sp->parsestate = csi_table;
2525d522f475Smrg	    break;
2526d522f475Smrg
2527d522f475Smrg	case CASE_ESC_SP_STATE:
2528d522f475Smrg	    /* esc space */
2529d522f475Smrg	    sp->parsestate = esc_sp_table;
2530d522f475Smrg	    break;
2531d522f475Smrg
2532d522f475Smrg	case CASE_CSI_EX_STATE:
2533d522f475Smrg	    /* csi exclamation */
2534d522f475Smrg	    sp->parsestate = csi_ex_table;
2535d522f475Smrg	    break;
2536d522f475Smrg
2537d522f475Smrg#if OPT_DEC_LOCATOR
2538d522f475Smrg	case CASE_CSI_TICK_STATE:
2539d522f475Smrg	    /* csi tick (') */
2540d522f475Smrg	    sp->parsestate = csi_tick_table;
2541d522f475Smrg	    break;
2542d522f475Smrg
2543d522f475Smrg	case CASE_DECEFR:
2544d522f475Smrg	    TRACE(("CASE_DECEFR - Enable Filter Rectangle\n"));
2545d522f475Smrg	    if (screen->send_mouse_pos == DEC_LOCATOR) {
2546d522f475Smrg		MotionOff(screen, xw);
2547d522f475Smrg		if ((screen->loc_filter_top = param[0]) < 1)
2548d522f475Smrg		    screen->loc_filter_top = LOC_FILTER_POS;
2549d522f475Smrg		if (nparam < 2 || (screen->loc_filter_left = param[1]) < 1)
2550d522f475Smrg		    screen->loc_filter_left = LOC_FILTER_POS;
2551d522f475Smrg		if (nparam < 3 || (screen->loc_filter_bottom = param[2]) < 1)
2552d522f475Smrg		    screen->loc_filter_bottom = LOC_FILTER_POS;
2553d522f475Smrg		if (nparam < 4 || (screen->loc_filter_right = param[3]) < 1)
2554d522f475Smrg		    screen->loc_filter_right = LOC_FILTER_POS;
2555d522f475Smrg		InitLocatorFilter(xw);
2556d522f475Smrg	    }
2557d522f475Smrg	    sp->parsestate = sp->groundtable;
2558d522f475Smrg	    break;
2559d522f475Smrg
2560d522f475Smrg	case CASE_DECELR:
2561d522f475Smrg	    MotionOff(screen, xw);
2562d522f475Smrg	    if (param[0] <= 0 || param[0] > 2) {
2563d522f475Smrg		screen->send_mouse_pos = MOUSE_OFF;
2564d522f475Smrg		TRACE(("DECELR - Disable Locator Reports\n"));
2565d522f475Smrg	    } else {
2566d522f475Smrg		TRACE(("DECELR - Enable Locator Reports\n"));
2567d522f475Smrg		screen->send_mouse_pos = DEC_LOCATOR;
2568d522f475Smrg		xtermShowPointer(xw, True);
2569d522f475Smrg		if (param[0] == 2) {
2570d522f475Smrg		    screen->locator_reset = True;
2571d522f475Smrg		} else {
2572d522f475Smrg		    screen->locator_reset = False;
2573d522f475Smrg		}
2574d522f475Smrg		if (nparam < 2 || param[1] != 1) {
2575d522f475Smrg		    screen->locator_pixels = False;
2576d522f475Smrg		} else {
2577d522f475Smrg		    screen->locator_pixels = True;
2578d522f475Smrg		}
2579d522f475Smrg		screen->loc_filter = False;
2580d522f475Smrg	    }
2581d522f475Smrg	    sp->parsestate = sp->groundtable;
2582d522f475Smrg	    break;
2583d522f475Smrg
2584d522f475Smrg	case CASE_DECSLE:
2585d522f475Smrg	    TRACE(("DECSLE - Select Locator Events\n"));
2586d522f475Smrg	    for (count = 0; count < nparam; ++count) {
2587d522f475Smrg		switch (param[count]) {
2588d522f475Smrg		case DEFAULT:
2589d522f475Smrg		case 0:
2590d522f475Smrg		    MotionOff(screen, xw);
2591d522f475Smrg		    screen->loc_filter = False;
2592d522f475Smrg		    screen->locator_events = 0;
2593d522f475Smrg		    break;
2594d522f475Smrg		case 1:
2595d522f475Smrg		    screen->locator_events |= LOC_BTNS_DN;
2596d522f475Smrg		    break;
2597d522f475Smrg		case 2:
2598d522f475Smrg		    screen->locator_events &= ~LOC_BTNS_DN;
2599d522f475Smrg		    break;
2600d522f475Smrg		case 3:
2601d522f475Smrg		    screen->locator_events |= LOC_BTNS_UP;
2602d522f475Smrg		    break;
2603d522f475Smrg		case 4:
2604d522f475Smrg		    screen->locator_events &= ~LOC_BTNS_UP;
2605d522f475Smrg		    break;
2606d522f475Smrg		}
2607d522f475Smrg	    }
2608d522f475Smrg	    sp->parsestate = sp->groundtable;
2609d522f475Smrg	    break;
2610d522f475Smrg
2611d522f475Smrg	case CASE_DECRQLP:
2612d522f475Smrg	    TRACE(("DECRQLP - Request Locator Position\n"));
2613d522f475Smrg	    if (param[0] < 2) {
2614d522f475Smrg		/* Issue DECLRP Locator Position Report */
2615d522f475Smrg		GetLocatorPosition(xw);
2616d522f475Smrg	    }
2617d522f475Smrg	    sp->parsestate = sp->groundtable;
2618d522f475Smrg	    break;
2619d522f475Smrg#endif /* OPT_DEC_LOCATOR */
2620d522f475Smrg
2621d522f475Smrg#if OPT_DEC_RECTOPS
2622d522f475Smrg	case CASE_CSI_DOLLAR_STATE:
2623d522f475Smrg	    /* csi dollar ($) */
2624d522f475Smrg	    if (screen->vtXX_level >= 4)
2625d522f475Smrg		sp->parsestate = csi_dollar_table;
2626d522f475Smrg	    else
2627d522f475Smrg		sp->parsestate = eigtable;
2628d522f475Smrg	    break;
2629d522f475Smrg
2630d522f475Smrg	case CASE_CSI_STAR_STATE:
2631d522f475Smrg	    /* csi dollar (*) */
2632d522f475Smrg	    if (screen->vtXX_level >= 4)
2633d522f475Smrg		sp->parsestate = csi_star_table;
2634d522f475Smrg	    else
2635d522f475Smrg		sp->parsestate = eigtable;
2636d522f475Smrg	    break;
2637d522f475Smrg
2638d522f475Smrg	case CASE_DECCRA:
2639d522f475Smrg	    TRACE(("CASE_DECCRA - Copy rectangular area\n"));
2640d522f475Smrg	    xtermParseRect(xw, nparam, param, &myRect);
2641d522f475Smrg	    ScrnCopyRectangle(xw, &myRect, nparam - 5, param + 5);
2642d522f475Smrg	    sp->parsestate = sp->groundtable;
2643d522f475Smrg	    break;
2644d522f475Smrg
2645d522f475Smrg	case CASE_DECERA:
2646d522f475Smrg	    TRACE(("CASE_DECERA - Erase rectangular area\n"));
2647d522f475Smrg	    xtermParseRect(xw, nparam, param, &myRect);
2648d522f475Smrg	    ScrnFillRectangle(xw, &myRect, ' ', 0, True);
2649d522f475Smrg	    sp->parsestate = sp->groundtable;
2650d522f475Smrg	    break;
2651d522f475Smrg
2652d522f475Smrg	case CASE_DECFRA:
2653d522f475Smrg	    TRACE(("CASE_DECFRA - Fill rectangular area\n"));
2654d522f475Smrg	    if (nparam > 0
2655d522f475Smrg		&& ((param[0] >= 32 && param[0] <= 126)
2656d522f475Smrg		    || (param[0] >= 160 && param[0] <= 255))) {
2657d522f475Smrg		xtermParseRect(xw, nparam - 1, param + 1, &myRect);
2658d522f475Smrg		ScrnFillRectangle(xw, &myRect, param[0], xw->flags, True);
2659d522f475Smrg	    }
2660d522f475Smrg	    sp->parsestate = sp->groundtable;
2661d522f475Smrg	    break;
2662d522f475Smrg
2663d522f475Smrg	case CASE_DECSERA:
2664d522f475Smrg	    TRACE(("CASE_DECSERA - Selective erase rectangular area\n"));
2665d522f475Smrg	    xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2666d522f475Smrg	    ScrnWipeRectangle(xw, &myRect);
2667d522f475Smrg	    sp->parsestate = sp->groundtable;
2668d522f475Smrg	    break;
2669d522f475Smrg
2670d522f475Smrg	case CASE_DECSACE:
2671d522f475Smrg	    TRACE(("CASE_DECSACE - Select attribute change extent\n"));
2672d522f475Smrg	    screen->cur_decsace = param[0];
2673d522f475Smrg	    sp->parsestate = sp->groundtable;
2674d522f475Smrg	    break;
2675d522f475Smrg
2676d522f475Smrg	case CASE_DECCARA:
2677d522f475Smrg	    TRACE(("CASE_DECCARA - Change attributes in rectangular area\n"));
2678d522f475Smrg	    xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2679d522f475Smrg	    ScrnMarkRectangle(xw, &myRect, False, nparam - 4, param + 4);
2680d522f475Smrg	    sp->parsestate = sp->groundtable;
2681d522f475Smrg	    break;
2682d522f475Smrg
2683d522f475Smrg	case CASE_DECRARA:
2684d522f475Smrg	    TRACE(("CASE_DECRARA - Reverse attributes in rectangular area\n"));
2685d522f475Smrg	    xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2686d522f475Smrg	    ScrnMarkRectangle(xw, &myRect, True, nparam - 4, param + 4);
2687d522f475Smrg	    sp->parsestate = sp->groundtable;
2688d522f475Smrg	    break;
2689d522f475Smrg#else
2690d522f475Smrg	case CASE_CSI_DOLLAR_STATE:
2691d522f475Smrg	    /* csi dollar ($) */
2692d522f475Smrg	    sp->parsestate = eigtable;
2693d522f475Smrg	    break;
2694d522f475Smrg
2695d522f475Smrg	case CASE_CSI_STAR_STATE:
2696d522f475Smrg	    /* csi dollar (*) */
2697d522f475Smrg	    sp->parsestate = eigtable;
2698d522f475Smrg	    break;
2699d522f475Smrg#endif /* OPT_DEC_RECTOPS */
2700d522f475Smrg
2701d522f475Smrg	case CASE_S7C1T:
2702d522f475Smrg	    TRACE(("CASE_S7C1T\n"));
2703d522f475Smrg	    show_8bit_control(False);
2704d522f475Smrg	    sp->parsestate = sp->groundtable;
2705d522f475Smrg	    break;
2706d522f475Smrg
2707d522f475Smrg	case CASE_S8C1T:
2708d522f475Smrg	    TRACE(("CASE_S8C1T\n"));
2709d522f475Smrg#if OPT_VT52_MODE
2710d522f475Smrg	    if (screen->vtXX_level <= 1)
2711d522f475Smrg		break;
2712d522f475Smrg#endif
2713d522f475Smrg	    show_8bit_control(True);
2714d522f475Smrg	    sp->parsestate = sp->groundtable;
2715d522f475Smrg	    break;
2716d522f475Smrg
2717d522f475Smrg	case CASE_OSC:
2718d522f475Smrg	    TRACE(("CASE_OSC: Operating System Command\n"));
2719d522f475Smrg	    sp->parsestate = sos_table;
2720d522f475Smrg	    sp->string_mode = ANSI_OSC;
2721d522f475Smrg	    break;
2722d522f475Smrg
2723d522f475Smrg	case CASE_RIS:
2724d522f475Smrg	    TRACE(("CASE_RIS\n"));
2725d522f475Smrg	    VTReset(xw, True, True);
2726d522f475Smrg	    sp->parsestate = sp->groundtable;
2727d522f475Smrg	    break;
2728d522f475Smrg
2729d522f475Smrg	case CASE_DECSTR:
2730d522f475Smrg	    TRACE(("CASE_DECSTR\n"));
2731d522f475Smrg	    VTReset(xw, False, False);
2732d522f475Smrg	    sp->parsestate = sp->groundtable;
2733d522f475Smrg	    break;
2734d522f475Smrg
2735d522f475Smrg	case CASE_REP:
2736d522f475Smrg	    TRACE(("CASE_REP\n"));
2737d522f475Smrg	    if (sp->lastchar >= 0 &&
2738d522f475Smrg		sp->lastchar < 256 &&
2739d522f475Smrg		sp->groundtable[E2A(sp->lastchar)] == CASE_PRINT) {
2740d522f475Smrg		IChar repeated[2];
2741d522f475Smrg		count = (param[0] < 1) ? 1 : param[0];
2742d522f475Smrg		repeated[0] = sp->lastchar;
2743d522f475Smrg		while (count-- > 0) {
2744d522f475Smrg		    dotext(xw,
2745d522f475Smrg			   screen->gsets[(int) (screen->curgl)],
2746d522f475Smrg			   repeated, 1);
2747d522f475Smrg		}
2748d522f475Smrg	    }
2749d522f475Smrg	    sp->parsestate = sp->groundtable;
2750d522f475Smrg	    break;
2751d522f475Smrg
2752d522f475Smrg	case CASE_LS2:
2753d522f475Smrg	    TRACE(("CASE_LS2\n"));
2754d522f475Smrg	    screen->curgl = 2;
2755d522f475Smrg	    sp->parsestate = sp->groundtable;
2756d522f475Smrg	    break;
2757d522f475Smrg
2758d522f475Smrg	case CASE_LS3:
2759d522f475Smrg	    TRACE(("CASE_LS3\n"));
2760d522f475Smrg	    screen->curgl = 3;
2761d522f475Smrg	    sp->parsestate = sp->groundtable;
2762d522f475Smrg	    break;
2763d522f475Smrg
2764d522f475Smrg	case CASE_LS3R:
2765d522f475Smrg	    TRACE(("CASE_LS3R\n"));
2766d522f475Smrg	    screen->curgr = 3;
2767d522f475Smrg	    sp->parsestate = sp->groundtable;
2768d522f475Smrg	    break;
2769d522f475Smrg
2770d522f475Smrg	case CASE_LS2R:
2771d522f475Smrg	    TRACE(("CASE_LS2R\n"));
2772d522f475Smrg	    screen->curgr = 2;
2773d522f475Smrg	    sp->parsestate = sp->groundtable;
2774d522f475Smrg	    break;
2775d522f475Smrg
2776d522f475Smrg	case CASE_LS1R:
2777d522f475Smrg	    TRACE(("CASE_LS1R\n"));
2778d522f475Smrg	    screen->curgr = 1;
2779d522f475Smrg	    sp->parsestate = sp->groundtable;
2780d522f475Smrg	    break;
2781d522f475Smrg
2782d522f475Smrg	case CASE_XTERM_SAVE:
2783d522f475Smrg	    savemodes(xw);
2784d522f475Smrg	    sp->parsestate = sp->groundtable;
2785d522f475Smrg	    break;
2786d522f475Smrg
2787d522f475Smrg	case CASE_XTERM_RESTORE:
2788d522f475Smrg	    restoremodes(xw);
2789d522f475Smrg	    sp->parsestate = sp->groundtable;
2790d522f475Smrg	    break;
2791d522f475Smrg
2792d522f475Smrg	case CASE_XTERM_WINOPS:
2793d522f475Smrg	    TRACE(("CASE_XTERM_WINOPS\n"));
2794d522f475Smrg	    if (screen->allowWindowOps)
2795d522f475Smrg		window_ops(xw);
2796d522f475Smrg	    sp->parsestate = sp->groundtable;
2797d522f475Smrg	    break;
2798d522f475Smrg#if OPT_WIDE_CHARS
2799d522f475Smrg	case CASE_ESC_PERCENT:
2800d522f475Smrg	    sp->parsestate = esc_pct_table;
2801d522f475Smrg	    break;
2802d522f475Smrg
2803d522f475Smrg	case CASE_UTF8:
2804d522f475Smrg	    /* If we did not set UTF-8 mode from resource or the
2805d522f475Smrg	     * command-line, allow it to be enabled/disabled by
2806d522f475Smrg	     * control sequence.
2807d522f475Smrg	     */
2808d522f475Smrg	    if (!screen->wide_chars) {
2809d522f475Smrg		WriteNow();
2810d522f475Smrg		ChangeToWide(xw);
2811d522f475Smrg	    }
2812d522f475Smrg	    if (screen->wide_chars
2813d522f475Smrg		&& screen->utf8_mode != uAlways) {
2814d522f475Smrg		switchPtyData(screen, c == 'G');
2815d522f475Smrg		TRACE(("UTF8 mode %s\n",
2816d522f475Smrg		       BtoS(screen->utf8_mode)));
2817d522f475Smrg	    } else {
2818d522f475Smrg		TRACE(("UTF8 mode NOT turned %s (%s)\n",
2819d522f475Smrg		       BtoS(c == 'G'),
2820d522f475Smrg		       (screen->utf8_mode == uAlways)
2821d522f475Smrg		       ? "UTF-8 mode set from command-line"
2822d522f475Smrg		       : "wideChars resource was not set"));
2823d522f475Smrg	    }
2824d522f475Smrg	    sp->parsestate = sp->groundtable;
2825d522f475Smrg	    break;
2826d522f475Smrg#endif
2827d522f475Smrg#if OPT_MOD_FKEYS
2828d522f475Smrg	case CASE_SET_MOD_FKEYS:
2829d522f475Smrg	    TRACE(("CASE_SET_MOD_FKEYS\n"));
2830d522f475Smrg	    if (nparam >= 1) {
2831d522f475Smrg		set_mod_fkeys(xw, param[0], nparam > 1 ? param[1] : DEFAULT, True);
2832d522f475Smrg	    } else {
2833d522f475Smrg		for (row = 1; row <= 5; ++row)
2834d522f475Smrg		    set_mod_fkeys(xw, row, DEFAULT, True);
2835d522f475Smrg	    }
2836d522f475Smrg	    break;
2837d522f475Smrg	case CASE_SET_MOD_FKEYS0:
2838d522f475Smrg	    TRACE(("CASE_SET_MOD_FKEYS0\n"));
2839d522f475Smrg	    if (nparam >= 1 && param[0] != DEFAULT) {
2840d522f475Smrg		set_mod_fkeys(xw, param[0], -1, False);
2841d522f475Smrg	    } else {
2842d522f475Smrg		xw->keyboard.modify_now.function_keys = -1;
2843d522f475Smrg	    }
2844d522f475Smrg	    break;
2845d522f475Smrg#endif
2846d522f475Smrg	case CASE_HIDE_POINTER:
2847d522f475Smrg	    TRACE(("CASE_HIDE_POINTER\n"));
2848d522f475Smrg	    if (nparam >= 1 && param[0] != DEFAULT) {
2849d522f475Smrg		screen->pointer_mode = param[0];
2850d522f475Smrg	    } else {
2851d522f475Smrg		screen->pointer_mode = DEF_POINTER_MODE;
2852d522f475Smrg	    }
2853d522f475Smrg	    break;
2854d522f475Smrg
2855d522f475Smrg	case CASE_CSI_IGNORE:
2856d522f475Smrg	    sp->parsestate = cigtable;
2857d522f475Smrg	    break;
2858d522f475Smrg	}
2859d522f475Smrg	if (sp->parsestate == sp->groundtable)
2860d522f475Smrg	    sp->lastchar = thischar;
2861d522f475Smrg    } while (0);
2862d522f475Smrg
2863d522f475Smrg#if OPT_WIDE_CHARS
2864d522f475Smrg    screen->utf8_inparse = (screen->utf8_mode != uFalse
2865d522f475Smrg			    && sp->parsestate != sos_table);
2866d522f475Smrg#endif
2867d522f475Smrg
2868d522f475Smrg    return True;
2869d522f475Smrg}
2870d522f475Smrg
2871d522f475Smrgstatic void
2872d522f475SmrgVTparse(XtermWidget xw)
2873d522f475Smrg{
2874d522f475Smrg    TScreen *screen;
2875d522f475Smrg
2876d522f475Smrg    /* We longjmp back to this point in VTReset() */
2877d522f475Smrg    (void) setjmp(vtjmpbuf);
2878d522f475Smrg    screen = &xw->screen;
2879d522f475Smrg    memset(&myState, 0, sizeof(myState));
2880d522f475Smrg#if OPT_VT52_MODE
2881d522f475Smrg    myState.groundtable = screen->vtXX_level ? ansi_table : vt52_table;
2882d522f475Smrg#else
2883d522f475Smrg    myState.groundtable = ansi_table;
2884d522f475Smrg#endif
2885d522f475Smrg    myState.parsestate = myState.groundtable;
2886d522f475Smrg    myState.lastchar = -1;	/* not a legal IChar */
2887d522f475Smrg    myState.nextstate = -1;	/* not a legal state */
2888d522f475Smrg
2889d522f475Smrg    do {
2890d522f475Smrg    } while (doparsing(xw, doinput(), &myState));
2891d522f475Smrg}
2892d522f475Smrg
2893d522f475Smrgstatic Char *v_buffer;		/* pointer to physical buffer */
2894d522f475Smrgstatic Char *v_bufstr = NULL;	/* beginning of area to write */
2895d522f475Smrgstatic Char *v_bufptr;		/* end of area to write */
2896d522f475Smrgstatic Char *v_bufend;		/* end of physical buffer */
2897d522f475Smrg
2898d522f475Smrg/* Write data to the pty as typed by the user, pasted with the mouse,
2899d522f475Smrg   or generated by us in response to a query ESC sequence. */
2900d522f475Smrg
2901d522f475Smrgint
2902d522f475Smrgv_write(int f, Char * data, unsigned len)
2903d522f475Smrg{
2904d522f475Smrg    int riten;
2905d522f475Smrg    unsigned c = len;
2906d522f475Smrg
2907d522f475Smrg    if (v_bufstr == NULL && len > 0) {
2908d522f475Smrg	v_buffer = (Char *) XtMalloc(len);
2909d522f475Smrg	v_bufstr = v_buffer;
2910d522f475Smrg	v_bufptr = v_buffer;
2911d522f475Smrg	v_bufend = v_buffer + len;
2912d522f475Smrg    }
2913d522f475Smrg#ifdef DEBUG
2914d522f475Smrg    if (debug) {
2915d522f475Smrg	fprintf(stderr, "v_write called with %d bytes (%d left over)",
2916d522f475Smrg		len, v_bufptr - v_bufstr);
2917d522f475Smrg	if (len > 1 && len < 10)
2918d522f475Smrg	    fprintf(stderr, " \"%.*s\"", len, (char *) data);
2919d522f475Smrg	fprintf(stderr, "\n");
2920d522f475Smrg    }
2921d522f475Smrg#endif
2922d522f475Smrg
2923d522f475Smrg#ifdef VMS
2924d522f475Smrg    if ((1 << f) != pty_mask)
2925d522f475Smrg	return (tt_write((char *) data, len));
2926d522f475Smrg#else /* VMS */
2927d522f475Smrg    if (!FD_ISSET(f, &pty_mask))
2928d522f475Smrg	return (write(f, (char *) data, len));
2929d522f475Smrg#endif /* VMS */
2930d522f475Smrg
2931d522f475Smrg    /*
2932d522f475Smrg     * Append to the block we already have.
2933d522f475Smrg     * Always doing this simplifies the code, and
2934d522f475Smrg     * isn't too bad, either.  If this is a short
2935d522f475Smrg     * block, it isn't too expensive, and if this is
2936d522f475Smrg     * a long block, we won't be able to write it all
2937d522f475Smrg     * anyway.
2938d522f475Smrg     */
2939d522f475Smrg
2940d522f475Smrg    if (len > 0) {
2941d522f475Smrg#if OPT_DABBREV
2942d522f475Smrg	term->screen.dabbrev_working = 0;	/* break dabbrev sequence */
2943d522f475Smrg#endif
2944d522f475Smrg	if (v_bufend < v_bufptr + len) {	/* we've run out of room */
2945d522f475Smrg	    if (v_bufstr != v_buffer) {
2946d522f475Smrg		/* there is unused space, move everything down */
2947d522f475Smrg		/* possibly overlapping memmove here */
2948d522f475Smrg#ifdef DEBUG
2949d522f475Smrg		if (debug)
2950d522f475Smrg		    fprintf(stderr, "moving data down %d\n",
2951d522f475Smrg			    v_bufstr - v_buffer);
2952d522f475Smrg#endif
2953d522f475Smrg		memmove(v_buffer, v_bufstr, (unsigned) (v_bufptr - v_bufstr));
2954d522f475Smrg		v_bufptr -= v_bufstr - v_buffer;
2955d522f475Smrg		v_bufstr = v_buffer;
2956d522f475Smrg	    }
2957d522f475Smrg	    if (v_bufend < v_bufptr + len) {
2958d522f475Smrg		/* still won't fit: get more space */
2959d522f475Smrg		/* Don't use XtRealloc because an error is not fatal. */
2960d522f475Smrg		int size = v_bufptr - v_buffer;		/* save across realloc */
2961d522f475Smrg		v_buffer = TypeRealloc(Char, size + len, v_buffer);
2962d522f475Smrg		if (v_buffer) {
2963d522f475Smrg#ifdef DEBUG
2964d522f475Smrg		    if (debug)
2965d522f475Smrg			fprintf(stderr, "expanded buffer to %d\n",
2966d522f475Smrg				size + len);
2967d522f475Smrg#endif
2968d522f475Smrg		    v_bufstr = v_buffer;
2969d522f475Smrg		    v_bufptr = v_buffer + size;
2970d522f475Smrg		    v_bufend = v_bufptr + len;
2971d522f475Smrg		} else {
2972d522f475Smrg		    /* no memory: ignore entire write request */
2973d522f475Smrg		    fprintf(stderr, "%s: cannot allocate buffer space\n",
2974d522f475Smrg			    xterm_name);
2975d522f475Smrg		    v_buffer = v_bufstr;	/* restore clobbered pointer */
2976d522f475Smrg		    c = 0;
2977d522f475Smrg		}
2978d522f475Smrg	    }
2979d522f475Smrg	}
2980d522f475Smrg	if (v_bufend >= v_bufptr + len) {
2981d522f475Smrg	    /* new stuff will fit */
2982d522f475Smrg	    memmove(v_bufptr, data, len);
2983d522f475Smrg	    v_bufptr += len;
2984d522f475Smrg	}
2985d522f475Smrg    }
2986d522f475Smrg
2987d522f475Smrg    /*
2988d522f475Smrg     * Write out as much of the buffer as we can.
2989d522f475Smrg     * Be careful not to overflow the pty's input silo.
2990d522f475Smrg     * We are conservative here and only write
2991d522f475Smrg     * a small amount at a time.
2992d522f475Smrg     *
2993d522f475Smrg     * If we can't push all the data into the pty yet, we expect write
2994d522f475Smrg     * to return a non-negative number less than the length requested
2995d522f475Smrg     * (if some data written) or -1 and set errno to EAGAIN,
2996d522f475Smrg     * EWOULDBLOCK, or EINTR (if no data written).
2997d522f475Smrg     *
2998d522f475Smrg     * (Not all systems do this, sigh, so the code is actually
2999d522f475Smrg     * a little more forgiving.)
3000d522f475Smrg     */
3001d522f475Smrg
3002d522f475Smrg#define MAX_PTY_WRITE 128	/* 1/2 POSIX minimum MAX_INPUT */
3003d522f475Smrg
3004d522f475Smrg    if (v_bufptr > v_bufstr) {
3005d522f475Smrg#ifdef VMS
3006d522f475Smrg	riten = tt_write(v_bufstr,
3007d522f475Smrg			 ((v_bufptr - v_bufstr <= VMS_TERM_BUFFER_SIZE)
3008d522f475Smrg			  ? v_bufptr - v_bufstr
3009d522f475Smrg			  : VMS_TERM_BUFFER_SIZE));
3010d522f475Smrg	if (riten == 0)
3011d522f475Smrg	    return (riten);
3012d522f475Smrg#else /* VMS */
3013d522f475Smrg	riten = write(f, v_bufstr,
3014d522f475Smrg		      (size_t) ((v_bufptr - v_bufstr <= MAX_PTY_WRITE)
3015d522f475Smrg				? v_bufptr - v_bufstr
3016d522f475Smrg				: MAX_PTY_WRITE));
3017d522f475Smrg	if (riten < 0)
3018d522f475Smrg#endif /* VMS */
3019d522f475Smrg	{
3020d522f475Smrg#ifdef DEBUG
3021d522f475Smrg	    if (debug)
3022d522f475Smrg		perror("write");
3023d522f475Smrg#endif
3024d522f475Smrg	    riten = 0;
3025d522f475Smrg	}
3026d522f475Smrg#ifdef DEBUG
3027d522f475Smrg	if (debug)
3028d522f475Smrg	    fprintf(stderr, "write called with %d, wrote %d\n",
3029d522f475Smrg		    v_bufptr - v_bufstr <= MAX_PTY_WRITE ?
3030d522f475Smrg		    v_bufptr - v_bufstr : MAX_PTY_WRITE,
3031d522f475Smrg		    riten);
3032d522f475Smrg#endif
3033d522f475Smrg	v_bufstr += riten;
3034d522f475Smrg	if (v_bufstr >= v_bufptr)	/* we wrote it all */
3035d522f475Smrg	    v_bufstr = v_bufptr = v_buffer;
3036d522f475Smrg    }
3037d522f475Smrg
3038d522f475Smrg    /*
3039d522f475Smrg     * If we have lots of unused memory allocated, return it
3040d522f475Smrg     */
3041d522f475Smrg    if (v_bufend - v_bufptr > 1024) {	/* arbitrary hysteresis */
3042d522f475Smrg	/* save pointers across realloc */
3043d522f475Smrg	int start = v_bufstr - v_buffer;
3044d522f475Smrg	int size = v_bufptr - v_buffer;
3045d522f475Smrg	unsigned allocsize = (unsigned) (size ? size : 1);
3046d522f475Smrg
3047d522f475Smrg	v_buffer = TypeRealloc(Char, allocsize, v_buffer);
3048d522f475Smrg	if (v_buffer) {
3049d522f475Smrg	    v_bufstr = v_buffer + start;
3050d522f475Smrg	    v_bufptr = v_buffer + size;
3051d522f475Smrg	    v_bufend = v_buffer + allocsize;
3052d522f475Smrg#ifdef DEBUG
3053d522f475Smrg	    if (debug)
3054d522f475Smrg		fprintf(stderr, "shrunk buffer to %d\n", allocsize);
3055d522f475Smrg#endif
3056d522f475Smrg	} else {
3057d522f475Smrg	    /* should we print a warning if couldn't return memory? */
3058d522f475Smrg	    v_buffer = v_bufstr - start;	/* restore clobbered pointer */
3059d522f475Smrg	}
3060d522f475Smrg    }
3061d522f475Smrg    return (c);
3062d522f475Smrg}
3063d522f475Smrg
3064d522f475Smrg#ifdef VMS
3065d522f475Smrg#define	ptymask()	(v_bufptr > v_bufstr ? pty_mask : 0)
3066d522f475Smrg
3067d522f475Smrgstatic void
3068d522f475Smrgin_put(XtermWidget xw)
3069d522f475Smrg{
3070d522f475Smrg    static PtySelect select_mask;
3071d522f475Smrg    static PtySelect write_mask;
3072d522f475Smrg    int update = VTbuffer->update;
3073d522f475Smrg    int size;
3074d522f475Smrg
3075d522f475Smrg    int status;
3076d522f475Smrg    Dimension replyWidth, replyHeight;
3077d522f475Smrg    XtGeometryResult stat;
3078d522f475Smrg
3079d522f475Smrg    TScreen *screen = &xw->screen;
3080d522f475Smrg    char *cp;
3081d522f475Smrg    int i;
3082d522f475Smrg
3083d522f475Smrg    select_mask = pty_mask;	/* force initial read */
3084d522f475Smrg    for (;;) {
3085d522f475Smrg
3086d522f475Smrg	/* if the terminal changed size, resize the widget */
3087d522f475Smrg	if (tt_changed) {
3088d522f475Smrg	    tt_changed = False;
3089d522f475Smrg
3090d522f475Smrg	    stat = XtMakeResizeRequest((Widget) xw,
3091d522f475Smrg				       ((Dimension) FontWidth(screen)
3092d522f475Smrg					* (tt_width)
3093d522f475Smrg					+ 2 * screen->border
3094d522f475Smrg					+ screen->fullVwin.sb_info.width),
3095d522f475Smrg				       ((Dimension) FontHeight(screen)
3096d522f475Smrg					* (tt_length)
3097d522f475Smrg					+ 2 * screen->border),
3098d522f475Smrg				       &replyWidth, &replyHeight);
3099d522f475Smrg
3100d522f475Smrg	    if (stat == XtGeometryYes || stat == XtGeometryDone) {
3101d522f475Smrg		xw->core.width = replyWidth;
3102d522f475Smrg		xw->core.height = replyHeight;
3103d522f475Smrg
3104d522f475Smrg		ScreenResize(xw, replyWidth, replyHeight, &xw->flags);
3105d522f475Smrg	    }
3106d522f475Smrg	    repairSizeHints();
3107d522f475Smrg	}
3108d522f475Smrg
3109d522f475Smrg	if (screen->eventMode == NORMAL
3110d522f475Smrg	    && readPtyData(screen, &select_mask, VTbuffer)) {
3111d522f475Smrg	    if (screen->scrollWidget
3112d522f475Smrg		&& screen->scrollttyoutput
3113d522f475Smrg		&& screen->topline < 0)
3114d522f475Smrg		/* Scroll to bottom */
3115d522f475Smrg		WindowScroll(xw, 0);
3116d522f475Smrg	    break;
3117d522f475Smrg	}
3118d522f475Smrg	if (screen->scroll_amt)
3119d522f475Smrg	    FlushScroll(xw);
3120d522f475Smrg	if (screen->cursor_set && CursorMoved(screen)) {
3121d522f475Smrg	    if (screen->cursor_state)
3122d522f475Smrg		HideCursor();
3123d522f475Smrg	    ShowCursor();
3124d522f475Smrg#if OPT_INPUT_METHOD
3125d522f475Smrg	    PreeditPosition(screen);
3126d522f475Smrg#endif
3127d522f475Smrg	} else if (screen->cursor_set != screen->cursor_state) {
3128d522f475Smrg	    if (screen->cursor_set)
3129d522f475Smrg		ShowCursor();
3130d522f475Smrg	    else
3131d522f475Smrg		HideCursor();
3132d522f475Smrg	}
3133d522f475Smrg
3134d522f475Smrg	if (QLength(screen->display)) {
3135d522f475Smrg	    select_mask = X_mask;
3136d522f475Smrg	} else {
3137d522f475Smrg	    write_mask = ptymask();
3138d522f475Smrg	    XFlush(screen->display);
3139d522f475Smrg	    select_mask = Select_mask;
3140d522f475Smrg	    if (screen->eventMode != NORMAL)
3141d522f475Smrg		select_mask = X_mask;
3142d522f475Smrg	}
3143d522f475Smrg	if (write_mask & ptymask()) {
3144d522f475Smrg	    v_write(screen->respond, 0, 0);	/* flush buffer */
3145d522f475Smrg	}
3146d522f475Smrg
3147d522f475Smrg	if (select_mask & X_mask) {
3148d522f475Smrg	    xevents();
3149d522f475Smrg	    if (VTbuffer->update != update)
3150d522f475Smrg		break;
3151d522f475Smrg	}
3152d522f475Smrg    }
3153d522f475Smrg}
3154d522f475Smrg#else /* VMS */
3155d522f475Smrg
3156d522f475Smrgstatic void
3157d522f475Smrgin_put(XtermWidget xw)
3158d522f475Smrg{
3159d522f475Smrg    static PtySelect select_mask;
3160d522f475Smrg    static PtySelect write_mask;
3161d522f475Smrg
3162d522f475Smrg    TScreen *screen = &xw->screen;
3163d522f475Smrg    int i, time_select;
3164d522f475Smrg    int size;
3165d522f475Smrg    int update = VTbuffer->update;
3166d522f475Smrg
3167d522f475Smrg    static struct timeval select_timeout;
3168d522f475Smrg
3169d522f475Smrg#if OPT_BLINK_CURS
3170d522f475Smrg    /*
3171d522f475Smrg     * Compute the timeout for the blinking cursor to be much smaller than
3172d522f475Smrg     * the "on" or "off" interval.
3173d522f475Smrg     */
3174d522f475Smrg    int tick = ((screen->blink_on < screen->blink_off)
3175d522f475Smrg		? screen->blink_on
3176d522f475Smrg		: screen->blink_off);
3177d522f475Smrg    tick *= (1000 / 8);		/* 1000 for msec/usec, 8 for "much" smaller */
3178d522f475Smrg    if (tick < 1)
3179d522f475Smrg	tick = 1;
3180d522f475Smrg#endif
3181d522f475Smrg
3182d522f475Smrg    for (;;) {
3183d522f475Smrg	if (screen->eventMode == NORMAL
3184d522f475Smrg	    && (size = readPtyData(screen, &select_mask, VTbuffer)) != 0) {
3185d522f475Smrg	    if (screen->scrollWidget
3186d522f475Smrg		&& screen->scrollttyoutput
3187d522f475Smrg		&& screen->topline < 0)
3188d522f475Smrg		WindowScroll(xw, 0);	/* Scroll to bottom */
3189d522f475Smrg	    /* stop speed reading at some point to look for X stuff */
3190d522f475Smrg	    TRACE(("VTbuffer uses %d/%d\n",
3191d522f475Smrg		   VTbuffer->last - VTbuffer->buffer,
3192d522f475Smrg		   BUF_SIZE));
3193d522f475Smrg	    if ((VTbuffer->last - VTbuffer->buffer) > BUF_SIZE) {
3194d522f475Smrg		FD_CLR(screen->respond, &select_mask);
3195d522f475Smrg		break;
3196d522f475Smrg	    }
3197d522f475Smrg#if defined(HAVE_SCHED_YIELD)
3198d522f475Smrg	    /*
3199d522f475Smrg	     * If we've read a full (small/fragment) buffer, let the operating
3200d522f475Smrg	     * system have a turn, and we'll resume reading until we've either
3201d522f475Smrg	     * read only a fragment of the buffer, or we've filled the large
3202d522f475Smrg	     * buffer (see above).  Doing this helps keep up with large bursts
3203d522f475Smrg	     * of output.
3204d522f475Smrg	     */
3205d522f475Smrg	    if (size == FRG_SIZE) {
3206d522f475Smrg		select_timeout.tv_sec = 0;
3207d522f475Smrg		i = Select(max_plus1, &select_mask, &write_mask, 0,
3208d522f475Smrg			   &select_timeout);
3209d522f475Smrg		if (i > 0) {
3210d522f475Smrg		    sched_yield();
3211d522f475Smrg		} else
3212d522f475Smrg		    break;
3213d522f475Smrg	    } else {
3214d522f475Smrg		break;
3215d522f475Smrg	    }
3216d522f475Smrg#else
3217d522f475Smrg	    (void) size;	/* unused in this branch */
3218d522f475Smrg	    break;
3219d522f475Smrg#endif
3220d522f475Smrg	}
3221d522f475Smrg	/* update the screen */
3222d522f475Smrg	if (screen->scroll_amt)
3223d522f475Smrg	    FlushScroll(xw);
3224d522f475Smrg	if (screen->cursor_set && CursorMoved(screen)) {
3225d522f475Smrg	    if (screen->cursor_state)
3226d522f475Smrg		HideCursor();
3227d522f475Smrg	    ShowCursor();
3228d522f475Smrg#if OPT_INPUT_METHOD
3229d522f475Smrg	    PreeditPosition(screen);
3230d522f475Smrg#endif
3231d522f475Smrg	} else if (screen->cursor_set != screen->cursor_state) {
3232d522f475Smrg	    if (screen->cursor_set)
3233d522f475Smrg		ShowCursor();
3234d522f475Smrg	    else
3235d522f475Smrg		HideCursor();
3236d522f475Smrg	}
3237d522f475Smrg
3238d522f475Smrg	XFlush(screen->display);	/* always flush writes before waiting */
3239d522f475Smrg
3240d522f475Smrg	/* Update the masks and, unless X events are already in the queue,
3241d522f475Smrg	   wait for I/O to be possible. */
3242d522f475Smrg	XFD_COPYSET(&Select_mask, &select_mask);
3243d522f475Smrg	/* in selection mode xterm does not read pty */
3244d522f475Smrg	if (screen->eventMode != NORMAL)
3245d522f475Smrg	    FD_CLR(screen->respond, &select_mask);
3246d522f475Smrg
3247d522f475Smrg	if (v_bufptr > v_bufstr) {
3248d522f475Smrg	    XFD_COPYSET(&pty_mask, &write_mask);
3249d522f475Smrg	} else
3250d522f475Smrg	    FD_ZERO(&write_mask);
3251d522f475Smrg	select_timeout.tv_sec = 0;
3252d522f475Smrg	time_select = 0;
3253d522f475Smrg
3254d522f475Smrg	/*
3255d522f475Smrg	 * if there's either an XEvent or an XtTimeout pending, just take
3256d522f475Smrg	 * a quick peek, i.e. timeout from the select() immediately.  If
3257d522f475Smrg	 * there's nothing pending, let select() block a little while, but
3258d522f475Smrg	 * for a shorter interval than the arrow-style scrollbar timeout.
3259d522f475Smrg	 * The blocking is optional, because it tends to increase the load
3260d522f475Smrg	 * on the host.
3261d522f475Smrg	 */
3262d522f475Smrg	if (XtAppPending(app_con)) {
3263d522f475Smrg	    select_timeout.tv_usec = 0;
3264d522f475Smrg	    time_select = 1;
3265d522f475Smrg	} else if (screen->awaitInput) {
3266d522f475Smrg	    select_timeout.tv_usec = 50000;
3267d522f475Smrg	    time_select = 1;
3268d522f475Smrg#if OPT_BLINK_CURS
3269d522f475Smrg	} else if ((screen->blink_timer != 0 &&
3270d522f475Smrg		    ((screen->select & FOCUS) || screen->always_highlight)) ||
3271d522f475Smrg		   (screen->cursor_state == BLINKED_OFF)) {
3272d522f475Smrg	    select_timeout.tv_usec = tick;
3273d522f475Smrg	    while (select_timeout.tv_usec > 1000000) {
3274d522f475Smrg		select_timeout.tv_usec -= 1000000;
3275d522f475Smrg		select_timeout.tv_sec++;
3276d522f475Smrg	    }
3277d522f475Smrg	    time_select = 1;
3278d522f475Smrg#endif
3279d522f475Smrg#if OPT_SESSION_MGT
3280d522f475Smrg	} else if (resource.sessionMgt) {
3281d522f475Smrg	    if (ice_fd >= 0)
3282d522f475Smrg		FD_SET(ice_fd, &select_mask);
3283d522f475Smrg#endif
3284d522f475Smrg	}
3285d522f475Smrg	if (need_cleanup)
3286d522f475Smrg	    Cleanup(0);
3287d522f475Smrg	i = Select(max_plus1, &select_mask, &write_mask, 0,
3288d522f475Smrg		   (time_select ? &select_timeout : 0));
3289d522f475Smrg	if (i < 0) {
3290d522f475Smrg	    if (errno != EINTR)
3291d522f475Smrg		SysError(ERROR_SELECT);
3292d522f475Smrg	    continue;
3293d522f475Smrg	}
3294d522f475Smrg
3295d522f475Smrg	/* if there is room to write more data to the pty, go write more */
3296d522f475Smrg	if (FD_ISSET(screen->respond, &write_mask)) {
3297d522f475Smrg	    v_write(screen->respond, (Char *) 0, 0);	/* flush buffer */
3298d522f475Smrg	}
3299d522f475Smrg
3300d522f475Smrg	/* if there are X events already in our queue, it
3301d522f475Smrg	   counts as being readable */
3302d522f475Smrg	if (XtAppPending(app_con) ||
3303d522f475Smrg	    FD_ISSET(ConnectionNumber(screen->display), &select_mask)) {
3304d522f475Smrg	    xevents();
3305d522f475Smrg	    if (VTbuffer->update != update)	/* HandleInterpret */
3306d522f475Smrg		break;
3307d522f475Smrg	}
3308d522f475Smrg
3309d522f475Smrg    }
3310d522f475Smrg}
3311d522f475Smrg#endif /* VMS */
3312d522f475Smrg
3313d522f475Smrgstatic IChar
3314d522f475Smrgdoinput(void)
3315d522f475Smrg{
3316d522f475Smrg    TScreen *screen = TScreenOf(term);
3317d522f475Smrg
3318d522f475Smrg    while (!morePtyData(screen, VTbuffer))
3319d522f475Smrg	in_put(term);
3320d522f475Smrg    return nextPtyData(screen, VTbuffer);
3321d522f475Smrg}
3322d522f475Smrg
3323d522f475Smrg#if OPT_INPUT_METHOD
3324d522f475Smrg/*
3325d522f475Smrg *  For OverTheSpot, client has to inform the position for XIM preedit.
3326d522f475Smrg */
3327d522f475Smrgstatic void
3328d522f475SmrgPreeditPosition(TScreen * screen)
3329d522f475Smrg{
3330d522f475Smrg    XPoint spot;
3331d522f475Smrg    XVaNestedList list;
3332d522f475Smrg
3333d522f475Smrg    if (!screen->xic)
3334d522f475Smrg	return;
3335d522f475Smrg    spot.x = CurCursorX(screen, screen->cur_row, screen->cur_col);
3336d522f475Smrg    spot.y = CursorY(screen, screen->cur_row) + screen->fs_ascent;
3337d522f475Smrg    list = XVaCreateNestedList(0,
3338d522f475Smrg			       XNSpotLocation, &spot,
3339d522f475Smrg			       XNForeground, T_COLOR(screen, TEXT_FG),
3340d522f475Smrg			       XNBackground, T_COLOR(screen, TEXT_BG),
3341d522f475Smrg			       NULL);
3342d522f475Smrg    XSetICValues(screen->xic, XNPreeditAttributes, list, NULL);
3343d522f475Smrg    XFree(list);
3344d522f475Smrg}
3345d522f475Smrg#endif
3346d522f475Smrg
3347d522f475Smrgstatic void
3348d522f475SmrgWrapLine(XtermWidget xw)
3349d522f475Smrg{
3350d522f475Smrg    TScreen *screen = &(xw->screen);
3351d522f475Smrg
3352d522f475Smrg    /* mark that we had to wrap this line */
3353d522f475Smrg    ScrnSetFlag(screen, screen->cur_row, LINEWRAPPED);
3354d522f475Smrg    xtermAutoPrint('\n');
3355d522f475Smrg    xtermIndex(xw, 1);
3356d522f475Smrg    set_cur_col(screen, 0);
3357d522f475Smrg}
3358d522f475Smrg
3359d522f475Smrg/*
3360d522f475Smrg * process a string of characters according to the character set indicated
3361d522f475Smrg * by charset.  worry about end of line conditions (wraparound if selected).
3362d522f475Smrg */
3363d522f475Smrgvoid
3364d522f475Smrgdotext(XtermWidget xw,
3365d522f475Smrg       int charset,
3366d522f475Smrg       IChar * buf,		/* start of characters to process */
3367d522f475Smrg       Cardinal len)		/* end */
3368d522f475Smrg{
3369d522f475Smrg    TScreen *screen = &(xw->screen);
3370d522f475Smrg#if OPT_WIDE_CHARS
3371d522f475Smrg    Cardinal chars_chomped = 1;
3372d522f475Smrg    int next_col = screen->cur_col;
3373d522f475Smrg#else
3374d522f475Smrg    int next_col, last_col, this_col;	/* must be signed */
3375d522f475Smrg#endif
3376d522f475Smrg    Cardinal offset;
3377d522f475Smrg
3378d522f475Smrg#if OPT_WIDE_CHARS
3379d522f475Smrg    /* don't translate if we use UTF-8, and are not handling legacy support
3380d522f475Smrg     * for line-drawing characters.
3381d522f475Smrg     */
3382d522f475Smrg    if ((screen->utf8_mode == uFalse)
3383d522f475Smrg	|| (screen->vt100_graphics && charset == '0'))
3384d522f475Smrg#endif
3385d522f475Smrg
3386d522f475Smrg	if (!xtermCharSetOut(buf, buf + len, charset))
3387d522f475Smrg	    return;
3388d522f475Smrg
3389d522f475Smrg    if_OPT_XMC_GLITCH(screen, {
3390d522f475Smrg	Cardinal n;
3391d522f475Smrg	if (charset != '?') {
3392d522f475Smrg	    for (n = 0; n < len; n++) {
3393d522f475Smrg		if (buf[n] == XMC_GLITCH)
3394d522f475Smrg		    buf[n] = XMC_GLITCH + 1;
3395d522f475Smrg	    }
3396d522f475Smrg	}
3397d522f475Smrg    });
3398d522f475Smrg
3399d522f475Smrg#if OPT_WIDE_CHARS
3400d522f475Smrg    for (offset = 0;
3401d522f475Smrg	 offset < len && (chars_chomped > 0 || screen->do_wrap);
3402d522f475Smrg	 offset += chars_chomped) {
3403d522f475Smrg	int width_available = MaxCols(screen) - screen->cur_col;
3404d522f475Smrg	int width_here = 0;
3405d522f475Smrg	int need_wrap = 0;
3406d522f475Smrg	int last_chomp = 0;
3407d522f475Smrg	chars_chomped = 0;
3408d522f475Smrg
3409d522f475Smrg	if (screen->do_wrap) {
3410d522f475Smrg	    screen->do_wrap = 0;
3411d522f475Smrg	    if ((xw->flags & WRAPAROUND)) {
3412d522f475Smrg		WrapLine(xw);
3413d522f475Smrg		width_available = MaxCols(screen) - screen->cur_col;
3414d522f475Smrg		next_col = screen->cur_col;
3415d522f475Smrg	    }
3416d522f475Smrg	}
3417d522f475Smrg
3418d522f475Smrg	while (width_here <= width_available && chars_chomped < (len - offset)) {
3419d522f475Smrg	    if (!screen->utf8_mode
3420d522f475Smrg		|| (screen->vt100_graphics && charset == '0'))
3421d522f475Smrg		last_chomp = 1;
3422d522f475Smrg	    else
3423d522f475Smrg		last_chomp = my_wcwidth((int) buf[chars_chomped + offset]);
3424d522f475Smrg	    width_here += last_chomp;
3425d522f475Smrg	    chars_chomped++;
3426d522f475Smrg	}
3427d522f475Smrg
3428d522f475Smrg	if (width_here > width_available) {
3429d522f475Smrg	    if (last_chomp > MaxCols(screen))
3430d522f475Smrg		break;		/* give up - it is too big */
3431d522f475Smrg	    chars_chomped--;
3432d522f475Smrg	    width_here -= last_chomp;
3433d522f475Smrg	    if (chars_chomped > 0) {
3434d522f475Smrg		need_wrap = 1;
3435d522f475Smrg	    }
3436d522f475Smrg	} else if (width_here == width_available) {
3437d522f475Smrg	    need_wrap = 1;
3438d522f475Smrg	} else if (chars_chomped != (len - offset)) {
3439d522f475Smrg	    need_wrap = 1;
3440d522f475Smrg	}
3441d522f475Smrg
3442d522f475Smrg	/*
3443d522f475Smrg	 * Split the wide characters back into separate arrays of 8-bit
3444d522f475Smrg	 * characters so we can use the existing interface.
3445d522f475Smrg	 *
3446d522f475Smrg	 * FIXME:  If we rewrote this interface, it would involve
3447d522f475Smrg	 * rewriting all of the memory-management for the screen
3448d522f475Smrg	 * buffers (perhaps this is simpler).
3449d522f475Smrg	 */
3450d522f475Smrg	if (chars_chomped != 0 && next_col <= screen->max_col) {
3451d522f475Smrg	    static unsigned limit;
3452d522f475Smrg	    static Char *hibyte, *lobyte;
3453d522f475Smrg	    Bool both = False;
3454d522f475Smrg	    unsigned j, k;
3455d522f475Smrg
3456d522f475Smrg	    if (chars_chomped >= limit) {
3457d522f475Smrg		limit = (chars_chomped + 1) * 2;
3458d522f475Smrg		lobyte = (Char *) XtRealloc((char *) lobyte, limit);
3459d522f475Smrg		hibyte = (Char *) XtRealloc((char *) hibyte, limit);
3460d522f475Smrg	    }
3461d522f475Smrg	    for (j = offset, k = 0; j < offset + chars_chomped; j++) {
3462d522f475Smrg		if (buf[j] == HIDDEN_CHAR)
3463d522f475Smrg		    continue;
3464d522f475Smrg		lobyte[k] = LO_BYTE(buf[j]);
3465d522f475Smrg		if (buf[j] > 255) {
3466d522f475Smrg		    hibyte[k] = HI_BYTE(buf[j]);
3467d522f475Smrg		    both = True;
3468d522f475Smrg		} else {
3469d522f475Smrg		    hibyte[k] = 0;
3470d522f475Smrg		}
3471d522f475Smrg		++k;
3472d522f475Smrg	    }
3473d522f475Smrg
3474d522f475Smrg	    WriteText(xw, PAIRED_CHARS(lobyte,
3475d522f475Smrg				       (both ? hibyte : 0)),
3476d522f475Smrg		      k);
3477d522f475Smrg#ifdef NO_LEAKS
3478d522f475Smrg	    if (limit != 0) {
3479d522f475Smrg		limit = 0;
3480d522f475Smrg		XtFree((char *) lobyte);
3481d522f475Smrg		XtFree((char *) hibyte);
3482d522f475Smrg		lobyte = 0;
3483d522f475Smrg		hibyte = 0;
3484d522f475Smrg	    }
3485d522f475Smrg#endif
3486d522f475Smrg	}
3487d522f475Smrg	next_col += width_here;
3488d522f475Smrg	screen->do_wrap = need_wrap;
3489d522f475Smrg    }
3490d522f475Smrg#else /* ! OPT_WIDE_CHARS */
3491d522f475Smrg
3492d522f475Smrg    for (offset = 0; offset < len; offset += this_col) {
3493d522f475Smrg	last_col = CurMaxCol(screen, screen->cur_row);
3494d522f475Smrg	this_col = last_col - screen->cur_col + 1;
3495d522f475Smrg	if (this_col <= 1) {
3496d522f475Smrg	    if (screen->do_wrap) {
3497d522f475Smrg		screen->do_wrap = 0;
3498d522f475Smrg		if ((xw->flags & WRAPAROUND)) {
3499d522f475Smrg		    WrapLine(xw);
3500d522f475Smrg		}
3501d522f475Smrg	    }
3502d522f475Smrg	    this_col = 1;
3503d522f475Smrg	}
3504d522f475Smrg	if (offset + this_col > len) {
3505d522f475Smrg	    this_col = len - offset;
3506d522f475Smrg	}
3507d522f475Smrg	next_col = screen->cur_col + this_col;
3508d522f475Smrg
3509d522f475Smrg	WriteText(xw, PAIRED_CHARS(buf + offset,
3510d522f475Smrg				   buf2 ? buf2 + offset : 0),
3511d522f475Smrg		  (unsigned) this_col);
3512d522f475Smrg
3513d522f475Smrg	/*
3514d522f475Smrg	 * The call to WriteText updates screen->cur_col.
3515d522f475Smrg	 * If screen->cur_col is less than next_col, we must have
3516d522f475Smrg	 * hit the right margin - so set the do_wrap flag.
3517d522f475Smrg	 */
3518d522f475Smrg	screen->do_wrap = (screen->cur_col < next_col);
3519d522f475Smrg    }
3520d522f475Smrg
3521d522f475Smrg#endif /* OPT_WIDE_CHARS */
3522d522f475Smrg}
3523d522f475Smrg
3524d522f475Smrg#if OPT_WIDE_CHARS
3525d522f475Smrgunsigned
3526d522f475Smrgvisual_width(PAIRED_CHARS(Char * str, Char * str2), Cardinal len)
3527d522f475Smrg{
3528d522f475Smrg    /* returns the visual width of a string (doublewide characters count
3529d522f475Smrg       as 2, normalwide characters count as 1) */
3530d522f475Smrg    int my_len = 0;
3531d522f475Smrg    while (len) {
3532d522f475Smrg	int ch = *str;
3533d522f475Smrg	if (str2)
3534d522f475Smrg	    ch |= *str2 << 8;
3535d522f475Smrg	if (str)
3536d522f475Smrg	    str++;
3537d522f475Smrg	if (str2)
3538d522f475Smrg	    str2++;
3539d522f475Smrg	if (iswide(ch))
3540d522f475Smrg	    my_len += 2;
3541d522f475Smrg	else
3542d522f475Smrg	    my_len++;
3543d522f475Smrg	len--;
3544d522f475Smrg    }
3545d522f475Smrg    return my_len;
3546d522f475Smrg}
3547d522f475Smrg#endif
3548d522f475Smrg
3549d522f475Smrg#if HANDLE_STRUCT_NOTIFY
3550d522f475Smrg/* Flag icon name with "***"  on window output when iconified.
3551d522f475Smrg */
3552d522f475Smrgstatic void
3553d522f475SmrgHandleStructNotify(Widget w GCC_UNUSED,
3554d522f475Smrg		   XtPointer closure GCC_UNUSED,
3555d522f475Smrg		   XEvent * event,
3556d522f475Smrg		   Boolean * cont GCC_UNUSED)
3557d522f475Smrg{
3558d522f475Smrg    static char *icon_name;
3559d522f475Smrg    static Arg args[] =
3560d522f475Smrg    {
3561d522f475Smrg	{XtNiconName, (XtArgVal) & icon_name}
3562d522f475Smrg    };
3563d522f475Smrg    TScreen *screen = TScreenOf(term);
3564d522f475Smrg
3565d522f475Smrg    switch (event->type) {
3566d522f475Smrg    case MapNotify:
3567d522f475Smrg	TRACE(("HandleStructNotify(MapNotify)\n"));
3568d522f475Smrg#if OPT_ZICONBEEP
3569d522f475Smrg	if (screen->zIconBeep_flagged) {
3570d522f475Smrg	    screen->zIconBeep_flagged = False;
3571d522f475Smrg	    icon_name = NULL;
3572d522f475Smrg	    XtGetValues(toplevel, args, XtNumber(args));
3573d522f475Smrg	    if (icon_name != NULL) {
3574d522f475Smrg		char *buf = CastMallocN(char, strlen(icon_name));
3575d522f475Smrg		if (buf == NULL) {
3576d522f475Smrg		    screen->zIconBeep_flagged = True;
3577d522f475Smrg		    return;
3578d522f475Smrg		}
3579d522f475Smrg		strcpy(buf, icon_name + 4);
3580d522f475Smrg		ChangeIconName(buf);
3581d522f475Smrg		free(buf);
3582d522f475Smrg	    }
3583d522f475Smrg	}
3584d522f475Smrg#endif /* OPT_ZICONBEEP */
3585d522f475Smrg	mapstate = !IsUnmapped;
3586d522f475Smrg	break;
3587d522f475Smrg    case UnmapNotify:
3588d522f475Smrg	TRACE(("HandleStructNotify(UnmapNotify)\n"));
3589d522f475Smrg	mapstate = IsUnmapped;
3590d522f475Smrg	break;
3591d522f475Smrg    case ConfigureNotify:
3592d522f475Smrg	if (event->xconfigure.window == XtWindow(toplevel)) {
3593d522f475Smrg#if OPT_TOOLBAR
3594d522f475Smrg	    TRACE(("HandleStructNotify(ConfigureNotify)\n"));
3595d522f475Smrg	    /*
3596d522f475Smrg	     * The notification is for the top-level widget, but we care about
3597d522f475Smrg	     * vt100 (ignore the tek4014 window).
3598d522f475Smrg	     */
3599d522f475Smrg	    if (term->screen.Vshow) {
3600d522f475Smrg		VTwin *Vwin = WhichVWin(&(term->screen));
3601d522f475Smrg		TbInfo *info = &(Vwin->tb_info);
3602d522f475Smrg		TbInfo save = *info;
3603d522f475Smrg
3604d522f475Smrg		if (info->menu_bar) {
3605d522f475Smrg		    XtVaGetValues(info->menu_bar,
3606d522f475Smrg				  XtNheight, &info->menu_height,
3607d522f475Smrg				  XtNborderWidth, &info->menu_border,
3608d522f475Smrg				  (XtPointer) 0);
3609d522f475Smrg
3610d522f475Smrg		    if (save.menu_height != info->menu_height
3611d522f475Smrg			|| save.menu_border != info->menu_border) {
3612d522f475Smrg
3613d522f475Smrg			TRACE(("...menu_height %d\n", info->menu_height));
3614d522f475Smrg			TRACE(("...menu_border %d\n", info->menu_border));
3615d522f475Smrg			TRACE(("...had height  %d, border %d\n",
3616d522f475Smrg			       save.menu_height,
3617d522f475Smrg			       save.menu_border));
3618d522f475Smrg
3619d522f475Smrg			/*
3620d522f475Smrg			 * FIXME:  Window manager still may be using the old
3621d522f475Smrg			 * values.  Try to fool it.
3622d522f475Smrg			 */
3623d522f475Smrg			XtMakeResizeRequest((Widget) term,
3624d522f475Smrg					    screen->fullVwin.fullwidth,
3625d522f475Smrg					    info->menu_height
3626d522f475Smrg					    - save.menu_height
3627d522f475Smrg					    + screen->fullVwin.fullheight,
3628d522f475Smrg					    NULL, NULL);
3629d522f475Smrg			repairSizeHints();
3630d522f475Smrg		    }
3631d522f475Smrg		}
3632d522f475Smrg	    }
3633d522f475Smrg#else
3634d522f475Smrg	    int height, width;
3635d522f475Smrg
3636d522f475Smrg	    /*
3637d522f475Smrg	     * Some window managers modify the configuration during
3638d522f475Smrg	     * initialization.  Skip notification events that we know are
3639d522f475Smrg	     * obsolete because there is already another in the queue.
3640d522f475Smrg	     */
3641d522f475Smrg	    do {
3642d522f475Smrg		height = event->xconfigure.height;
3643d522f475Smrg		width = event->xconfigure.width;
3644d522f475Smrg		TRACE(("HandleStructNotify(ConfigureNotify) %dx%d\n",
3645d522f475Smrg		       event->xconfigure.height, event->xconfigure.width));
3646d522f475Smrg
3647d522f475Smrg	    } while (XCheckTypedWindowEvent(XtDisplay(term),
3648d522f475Smrg					    event->xconfigure.window,
3649d522f475Smrg					    ConfigureNotify, event));
3650d522f475Smrg
3651d522f475Smrg	    if (height != term->hints.height || width != term->hints.width)
3652d522f475Smrg		RequestResize(term, height, width, False);
3653d522f475Smrg#endif /* OPT_TOOLBAR */
3654d522f475Smrg	}
3655d522f475Smrg	break;
3656d522f475Smrg    case ReparentNotify:
3657d522f475Smrg	TRACE(("HandleStructNotify(ReparentNotify)\n"));
3658d522f475Smrg	break;
3659d522f475Smrg    default:
3660d522f475Smrg	TRACE(("HandleStructNotify(event %s)\n",
3661d522f475Smrg	       visibleEventType(event->type)));
3662d522f475Smrg	break;
3663d522f475Smrg    }
3664d522f475Smrg}
3665d522f475Smrg#endif /* HANDLE_STRUCT_NOTIFY */
3666d522f475Smrg
3667d522f475Smrg#if OPT_BLINK_CURS
3668d522f475Smrgstatic void
3669d522f475SmrgSetCursorBlink(TScreen * screen, int enable)
3670d522f475Smrg{
3671d522f475Smrg    screen->cursor_blink = enable;
3672d522f475Smrg    if (DoStartBlinking(screen)) {
3673d522f475Smrg	StartBlinking(screen);
3674d522f475Smrg    } else {
3675d522f475Smrg#if !OPT_BLINK_TEXT
3676d522f475Smrg	StopBlinking(screen);
3677d522f475Smrg#endif
3678d522f475Smrg    }
3679d522f475Smrg    update_cursorblink();
3680d522f475Smrg}
3681d522f475Smrg
3682d522f475Smrgvoid
3683d522f475SmrgToggleCursorBlink(TScreen * screen)
3684d522f475Smrg{
3685d522f475Smrg    SetCursorBlink(screen, !(screen->cursor_blink));
3686d522f475Smrg}
3687d522f475Smrg#endif
3688d522f475Smrg
3689d522f475Smrg/*
3690d522f475Smrg * process ANSI modes set, reset
3691d522f475Smrg */
3692d522f475Smrgstatic void
3693d522f475Smrgansi_modes(XtermWidget xw,
3694d522f475Smrg	   void (*func) (unsigned *p, unsigned mask))
3695d522f475Smrg{
3696d522f475Smrg    int i;
3697d522f475Smrg
3698d522f475Smrg    for (i = 0; i < nparam; ++i) {
3699d522f475Smrg	switch (param[i]) {
3700d522f475Smrg	case 2:		/* KAM (if set, keyboard locked */
3701d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_KAM);
3702d522f475Smrg	    break;
3703d522f475Smrg
3704d522f475Smrg	case 4:		/* IRM                          */
3705d522f475Smrg	    (*func) (&xw->flags, INSERT);
3706d522f475Smrg	    break;
3707d522f475Smrg
3708d522f475Smrg	case 12:		/* SRM (if set, local echo      */
3709d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_SRM);
3710d522f475Smrg	    break;
3711d522f475Smrg
3712d522f475Smrg	case 20:		/* LNM                          */
3713d522f475Smrg	    (*func) (&xw->flags, LINEFEED);
3714d522f475Smrg	    update_autolinefeed();
3715d522f475Smrg	    break;
3716d522f475Smrg	}
3717d522f475Smrg    }
3718d522f475Smrg}
3719d522f475Smrg
3720d522f475Smrg#define IsSM() (func == bitset)
3721d522f475Smrg
3722d522f475Smrg#define set_bool_mode(flag) \
3723d522f475Smrg	flag = (IsSM()) ? ON : OFF
3724d522f475Smrg
3725d522f475Smrgstatic void
3726d522f475Smrgreally_set_mousemode(XtermWidget xw,
3727d522f475Smrg		     Bool enabled,
3728d522f475Smrg		     unsigned mode)
3729d522f475Smrg{
3730d522f475Smrg    xw->screen.send_mouse_pos = enabled ? mode : MOUSE_OFF;
3731d522f475Smrg    if (xw->screen.send_mouse_pos != MOUSE_OFF)
3732d522f475Smrg	xtermShowPointer(xw, True);
3733d522f475Smrg}
3734d522f475Smrg
3735d522f475Smrg#define set_mousemode(mode) really_set_mousemode(xw, IsSM(), mode)
3736d522f475Smrg
3737d522f475Smrg#if OPT_READLINE
3738d522f475Smrg#define set_mouseflag(f)		\
3739d522f475Smrg	(IsSM()				\
3740d522f475Smrg	 ? SCREEN_FLAG_set(screen, f)	\
3741d522f475Smrg	 : SCREEN_FLAG_unset(screen, f))
3742d522f475Smrg#endif
3743d522f475Smrg
3744d522f475Smrg/*
3745d522f475Smrg * process DEC private modes set, reset
3746d522f475Smrg */
3747d522f475Smrgstatic void
3748d522f475Smrgdpmodes(XtermWidget xw,
3749d522f475Smrg	void (*func) (unsigned *p, unsigned mask))
3750d522f475Smrg{
3751d522f475Smrg    TScreen *screen = &xw->screen;
3752d522f475Smrg    int i, j;
3753d522f475Smrg
3754d522f475Smrg    for (i = 0; i < nparam; ++i) {
3755d522f475Smrg	TRACE(("%s %d\n", IsSM()? "DECSET" : "DECRST", param[i]));
3756d522f475Smrg	switch (param[i]) {
3757d522f475Smrg	case 1:		/* DECCKM                       */
3758d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECCKM);
3759d522f475Smrg	    update_appcursor();
3760d522f475Smrg	    break;
3761d522f475Smrg	case 2:		/* DECANM - ANSI/VT52 mode      */
3762d522f475Smrg	    if (IsSM()) {	/* ANSI (VT100) */
3763d522f475Smrg		/*
3764d522f475Smrg		 * Setting DECANM should have no effect, since this function
3765d522f475Smrg		 * cannot be reached from vt52 mode.
3766d522f475Smrg		 */
3767d522f475Smrg		;
3768d522f475Smrg	    }
3769d522f475Smrg#if OPT_VT52_MODE
3770d522f475Smrg	    else if (screen->terminal_id >= 100) {	/* VT52 */
3771d522f475Smrg		TRACE(("DECANM terminal_id %d, vtXX_level %d\n",
3772d522f475Smrg		       screen->terminal_id,
3773d522f475Smrg		       screen->vtXX_level));
3774d522f475Smrg		screen->vt52_save_level = screen->vtXX_level;
3775d522f475Smrg		screen->vtXX_level = 0;
3776d522f475Smrg		screen->vt52_save_curgl = screen->curgl;
3777d522f475Smrg		screen->vt52_save_curgr = screen->curgr;
3778d522f475Smrg		screen->vt52_save_curss = screen->curss;
3779d522f475Smrg		memmove(screen->vt52_save_gsets, screen->gsets, sizeof(screen->gsets));
3780d522f475Smrg		resetCharsets(screen);
3781d522f475Smrg		nparam = 0;	/* ignore the remaining params, if any */
3782d522f475Smrg	    }
3783d522f475Smrg#endif
3784d522f475Smrg	    break;
3785d522f475Smrg	case 3:		/* DECCOLM                      */
3786d522f475Smrg	    if (screen->c132) {
3787d522f475Smrg		ClearScreen(xw);
3788d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
3789d522f475Smrg		if ((j = IsSM()? 132 : 80) !=
3790d522f475Smrg		    ((xw->flags & IN132COLUMNS) ? 132 : 80) ||
3791d522f475Smrg		    j != MaxCols(screen))
3792d522f475Smrg		    RequestResize(xw, -1, j, True);
3793d522f475Smrg		(*func) (&xw->flags, IN132COLUMNS);
3794d522f475Smrg	    }
3795d522f475Smrg	    break;
3796d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
3797d522f475Smrg	    if (IsSM()) {
3798d522f475Smrg		screen->jumpscroll = 0;
3799d522f475Smrg		if (screen->scroll_amt)
3800d522f475Smrg		    FlushScroll(xw);
3801d522f475Smrg	    } else
3802d522f475Smrg		screen->jumpscroll = 1;
3803d522f475Smrg	    (*func) (&xw->flags, SMOOTHSCROLL);
3804d522f475Smrg	    update_jumpscroll();
3805d522f475Smrg	    break;
3806d522f475Smrg	case 5:		/* DECSCNM                      */
3807d522f475Smrg	    j = xw->flags;
3808d522f475Smrg	    (*func) (&xw->flags, REVERSE_VIDEO);
3809d522f475Smrg	    if ((xw->flags ^ j) & REVERSE_VIDEO)
3810d522f475Smrg		ReverseVideo(xw);
3811d522f475Smrg	    /* update_reversevideo done in RevVid */
3812d522f475Smrg	    break;
3813d522f475Smrg
3814d522f475Smrg	case 6:		/* DECOM                        */
3815d522f475Smrg	    (*func) (&xw->flags, ORIGIN);
3816d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
3817d522f475Smrg	    break;
3818d522f475Smrg
3819d522f475Smrg	case 7:		/* DECAWM                       */
3820d522f475Smrg	    (*func) (&xw->flags, WRAPAROUND);
3821d522f475Smrg	    update_autowrap();
3822d522f475Smrg	    break;
3823d522f475Smrg	case 8:		/* DECARM                       */
3824d522f475Smrg	    /* ignore autorepeat
3825d522f475Smrg	     * XAutoRepeatOn() and XAutoRepeatOff() can do this, but only
3826d522f475Smrg	     * for the whole display - not limited to a given window.
3827d522f475Smrg	     */
3828d522f475Smrg	    break;
3829d522f475Smrg	case SET_X10_MOUSE:	/* MIT bogus sequence           */
3830d522f475Smrg	    MotionOff(screen, xw);
3831d522f475Smrg	    set_mousemode(X10_MOUSE);
3832d522f475Smrg	    break;
3833d522f475Smrg#if OPT_TOOLBAR
3834d522f475Smrg	case 10:		/* rxvt */
3835d522f475Smrg	    ShowToolbar(IsSM());
3836d522f475Smrg	    break;
3837d522f475Smrg#endif
3838d522f475Smrg#if OPT_BLINK_CURS
3839d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
3840d522f475Smrg	    if (screen->cursor_blink_res) {
3841d522f475Smrg		set_bool_mode(screen->cursor_blink_esc);
3842d522f475Smrg		SetCursorBlink(screen, screen->cursor_blink);
3843d522f475Smrg	    }
3844d522f475Smrg	    break;
3845d522f475Smrg#endif
3846d522f475Smrg	case 18:		/* DECPFF: print form feed */
3847d522f475Smrg	    set_bool_mode(screen->printer_formfeed);
3848d522f475Smrg	    break;
3849d522f475Smrg	case 19:		/* DECPEX: print extent */
3850d522f475Smrg	    set_bool_mode(screen->printer_extent);
3851d522f475Smrg	    break;
3852d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
3853d522f475Smrg	    set_bool_mode(screen->cursor_set);
3854d522f475Smrg	    break;
3855d522f475Smrg	case 30:		/* rxvt */
3856d522f475Smrg	    if (screen->fullVwin.sb_info.width != (IsSM()? ON : OFF))
3857d522f475Smrg		ToggleScrollBar(xw);
3858d522f475Smrg	    break;
3859d522f475Smrg#if OPT_SHIFT_FONTS
3860d522f475Smrg	case 35:		/* rxvt */
3861d522f475Smrg	    set_bool_mode(xw->misc.shift_fonts);
3862d522f475Smrg	    break;
3863d522f475Smrg#endif
3864d522f475Smrg	case 38:		/* DECTEK                       */
3865d522f475Smrg#if OPT_TEK4014
3866d522f475Smrg	    if (IsSM() && !(screen->inhibit & I_TEK)) {
3867d522f475Smrg		FlushLog(screen);
3868d522f475Smrg		TEK4014_ACTIVE(xw) = True;
3869d522f475Smrg	    }
3870d522f475Smrg#endif
3871d522f475Smrg	    break;
3872d522f475Smrg	case 40:		/* 132 column mode              */
3873d522f475Smrg	    set_bool_mode(screen->c132);
3874d522f475Smrg	    update_allow132();
3875d522f475Smrg	    break;
3876d522f475Smrg	case 41:		/* curses hack                  */
3877d522f475Smrg	    set_bool_mode(screen->curses);
3878d522f475Smrg	    update_cursesemul();
3879d522f475Smrg	    break;
3880d522f475Smrg	case 42:		/* DECNRCM national charset (VT220) */
3881d522f475Smrg	    (*func) (&xw->flags, NATIONAL);
3882d522f475Smrg	    break;
3883d522f475Smrg	case 44:		/* margin bell                  */
3884d522f475Smrg	    set_bool_mode(screen->marginbell);
3885d522f475Smrg	    if (!screen->marginbell)
3886d522f475Smrg		screen->bellarmed = -1;
3887d522f475Smrg	    update_marginbell();
3888d522f475Smrg	    break;
3889d522f475Smrg	case 45:		/* reverse wraparound   */
3890d522f475Smrg	    (*func) (&xw->flags, REVERSEWRAP);
3891d522f475Smrg	    update_reversewrap();
3892d522f475Smrg	    break;
3893d522f475Smrg#ifdef ALLOWLOGGING
3894d522f475Smrg	case 46:		/* logging              */
3895d522f475Smrg#ifdef ALLOWLOGFILEONOFF
3896d522f475Smrg	    /*
3897d522f475Smrg	     * if this feature is enabled, logging may be
3898d522f475Smrg	     * enabled and disabled via escape sequences.
3899d522f475Smrg	     */
3900d522f475Smrg	    if (IsSM())
3901d522f475Smrg		StartLog(screen);
3902d522f475Smrg	    else
3903d522f475Smrg		CloseLog(screen);
3904d522f475Smrg#else
3905d522f475Smrg	    Bell(XkbBI_Info, 0);
3906d522f475Smrg	    Bell(XkbBI_Info, 0);
3907d522f475Smrg#endif /* ALLOWLOGFILEONOFF */
3908d522f475Smrg	    break;
3909d522f475Smrg#endif
3910d522f475Smrg	case 1049:		/* alternate buffer & cursor */
3911d522f475Smrg	    if (!xw->misc.titeInhibit) {
3912d522f475Smrg		if (IsSM()) {
3913d522f475Smrg		    CursorSave(xw);
3914d522f475Smrg		    ToAlternate(xw);
3915d522f475Smrg		    ClearScreen(xw);
3916d522f475Smrg		} else {
3917d522f475Smrg		    FromAlternate(xw);
3918d522f475Smrg		    CursorRestore(xw);
3919d522f475Smrg		}
3920d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
3921d522f475Smrg		if (IsSM()) {
3922d522f475Smrg		    xtermScroll(xw, screen->max_row);
3923d522f475Smrg		}
3924d522f475Smrg	    }
3925d522f475Smrg	    break;
3926d522f475Smrg	case 1047:
3927d522f475Smrg	    /* FALLTHRU */
3928d522f475Smrg	case 47:		/* alternate buffer */
3929d522f475Smrg	    if (!xw->misc.titeInhibit) {
3930d522f475Smrg		if (IsSM()) {
3931d522f475Smrg		    ToAlternate(xw);
3932d522f475Smrg		} else {
3933d522f475Smrg		    if (screen->alternate
3934d522f475Smrg			&& (param[i] == 1047))
3935d522f475Smrg			ClearScreen(xw);
3936d522f475Smrg		    FromAlternate(xw);
3937d522f475Smrg		}
3938d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
3939d522f475Smrg		if (IsSM()) {
3940d522f475Smrg		    xtermScroll(xw, screen->max_row);
3941d522f475Smrg		}
3942d522f475Smrg	    }
3943d522f475Smrg	    break;
3944d522f475Smrg	case 66:		/* DECNKM */
3945d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECKPAM);
3946d522f475Smrg	    update_appkeypad();
3947d522f475Smrg	    break;
3948d522f475Smrg	case 67:		/* DECBKM */
3949d522f475Smrg	    /* back-arrow mapped to backspace or delete(D) */
3950d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECBKM);
3951d522f475Smrg	    TRACE(("DECSET DECBKM %s\n",
3952d522f475Smrg		   BtoS(xw->keyboard.flags & MODE_DECBKM)));
3953d522f475Smrg	    update_decbkm();
3954d522f475Smrg	    break;
3955d522f475Smrg	case SET_VT200_MOUSE:	/* xterm bogus sequence         */
3956d522f475Smrg	    MotionOff(screen, xw);
3957d522f475Smrg	    set_mousemode(VT200_MOUSE);
3958d522f475Smrg	    break;
3959d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:	/* xterm sequence w/hilite tracking */
3960d522f475Smrg	    MotionOff(screen, xw);
3961d522f475Smrg	    set_mousemode(VT200_HIGHLIGHT_MOUSE);
3962d522f475Smrg	    break;
3963d522f475Smrg	case SET_BTN_EVENT_MOUSE:
3964d522f475Smrg	    MotionOff(screen, xw);
3965d522f475Smrg	    set_mousemode(BTN_EVENT_MOUSE);
3966d522f475Smrg	    break;
3967d522f475Smrg	case SET_ANY_EVENT_MOUSE:
3968d522f475Smrg	    set_mousemode(ANY_EVENT_MOUSE);
3969d522f475Smrg	    if (screen->send_mouse_pos == MOUSE_OFF) {
3970d522f475Smrg		MotionOff(screen, xw);
3971d522f475Smrg	    } else {
3972d522f475Smrg		MotionOn(screen, xw);
3973d522f475Smrg	    }
3974d522f475Smrg	    break;
3975d522f475Smrg#if OPT_FOCUS_EVENT
3976d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
3977d522f475Smrg	    set_bool_mode(screen->send_focus_pos);
3978d522f475Smrg	    break;
3979d522f475Smrg#endif
3980d522f475Smrg	case 1010:		/* rxvt */
3981d522f475Smrg	    set_bool_mode(screen->scrollttyoutput);
3982d522f475Smrg	    update_scrollttyoutput();
3983d522f475Smrg	    break;
3984d522f475Smrg	case 1011:		/* rxvt */
3985d522f475Smrg	    set_bool_mode(screen->scrollkey);
3986d522f475Smrg	    update_scrollkey();
3987d522f475Smrg	    break;
3988d522f475Smrg	case 1034:
3989d522f475Smrg	    set_bool_mode(xw->screen.input_eight_bits);
3990d522f475Smrg	    update_alt_esc();
3991d522f475Smrg	    break;
3992d522f475Smrg#if OPT_NUM_LOCK
3993d522f475Smrg	case 1035:
3994d522f475Smrg	    set_bool_mode(xw->misc.real_NumLock);
3995d522f475Smrg	    update_num_lock();
3996d522f475Smrg	    break;
3997d522f475Smrg	case 1036:
3998d522f475Smrg	    set_bool_mode(screen->meta_sends_esc);
3999d522f475Smrg	    update_meta_esc();
4000d522f475Smrg	    break;
4001d522f475Smrg#endif
4002d522f475Smrg	case 1037:
4003d522f475Smrg	    set_bool_mode(screen->delete_is_del);
4004d522f475Smrg	    update_delete_del();
4005d522f475Smrg	    break;
4006d522f475Smrg#if OPT_NUM_LOCK
4007d522f475Smrg	case 1039:
4008d522f475Smrg	    set_bool_mode(screen->alt_sends_esc);
4009d522f475Smrg	    update_alt_esc();
4010d522f475Smrg	    break;
4011d522f475Smrg#endif
4012d522f475Smrg	case 1040:
4013d522f475Smrg	    set_bool_mode(screen->keepSelection);
4014d522f475Smrg	    update_keepSelection();
4015d522f475Smrg	    break;
4016d522f475Smrg	case 1041:
4017d522f475Smrg	    set_bool_mode(screen->selectToClipboard);
4018d522f475Smrg	    update_selectToClipboard();
4019d522f475Smrg	    break;
4020d522f475Smrg	case 1042:
4021d522f475Smrg	    set_bool_mode(screen->bellIsUrgent);
4022d522f475Smrg	    update_bellIsUrgent();
4023d522f475Smrg	    break;
4024d522f475Smrg	case 1043:
4025d522f475Smrg	    set_bool_mode(screen->poponbell);
4026d522f475Smrg	    update_poponbell();
4027d522f475Smrg	    break;
4028d522f475Smrg	case 1048:
4029d522f475Smrg	    if (!xw->misc.titeInhibit) {
4030d522f475Smrg		if (IsSM())
4031d522f475Smrg		    CursorSave(xw);
4032d522f475Smrg		else
4033d522f475Smrg		    CursorRestore(xw);
4034d522f475Smrg	    }
4035d522f475Smrg	    break;
4036d522f475Smrg#if OPT_TCAP_FKEYS
4037d522f475Smrg	case 1050:
4038d522f475Smrg	    set_keyboard_type(xw, keyboardIsTermcap, IsSM());
4039d522f475Smrg	    break;
4040d522f475Smrg#endif
4041d522f475Smrg#if OPT_SUN_FUNC_KEYS
4042d522f475Smrg	case 1051:
4043d522f475Smrg	    set_keyboard_type(xw, keyboardIsSun, IsSM());
4044d522f475Smrg	    break;
4045d522f475Smrg#endif
4046d522f475Smrg#if OPT_HP_FUNC_KEYS
4047d522f475Smrg	case 1052:
4048d522f475Smrg	    set_keyboard_type(xw, keyboardIsHP, IsSM());
4049d522f475Smrg	    break;
4050d522f475Smrg#endif
4051d522f475Smrg#if OPT_SCO_FUNC_KEYS
4052d522f475Smrg	case 1053:
4053d522f475Smrg	    set_keyboard_type(xw, keyboardIsSCO, IsSM());
4054d522f475Smrg	    break;
4055d522f475Smrg#endif
4056d522f475Smrg	case 1060:
4057d522f475Smrg	    set_keyboard_type(xw, keyboardIsLegacy, IsSM());
4058d522f475Smrg	    break;
4059d522f475Smrg#if OPT_SUNPC_KBD
4060d522f475Smrg	case 1061:
4061d522f475Smrg	    set_keyboard_type(xw, keyboardIsVT220, IsSM());
4062d522f475Smrg	    break;
4063d522f475Smrg#endif
4064d522f475Smrg#if OPT_READLINE
4065d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4066d522f475Smrg	    set_mouseflag(click1_moves);
4067d522f475Smrg	    break;
4068d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4069d522f475Smrg	    set_mouseflag(paste_moves);
4070d522f475Smrg	    break;
4071d522f475Smrg	case SET_DBUTTON3_DELETE:
4072d522f475Smrg	    set_mouseflag(dclick3_deletes);
4073d522f475Smrg	    break;
4074d522f475Smrg	case SET_PASTE_IN_BRACKET:
4075d522f475Smrg	    set_mouseflag(paste_brackets);
4076d522f475Smrg	    break;
4077d522f475Smrg	case SET_PASTE_QUOTE:
4078d522f475Smrg	    set_mouseflag(paste_quotes);
4079d522f475Smrg	    break;
4080d522f475Smrg	case SET_PASTE_LITERAL_NL:
4081d522f475Smrg	    set_mouseflag(paste_literal_nl);
4082d522f475Smrg	    break;
4083d522f475Smrg#endif /* OPT_READLINE */
4084d522f475Smrg	}
4085d522f475Smrg    }
4086d522f475Smrg}
4087d522f475Smrg
4088d522f475Smrg/*
4089d522f475Smrg * process xterm private modes save
4090d522f475Smrg */
4091d522f475Smrgstatic void
4092d522f475Smrgsavemodes(XtermWidget xw)
4093d522f475Smrg{
4094d522f475Smrg    TScreen *screen = &xw->screen;
4095d522f475Smrg    int i;
4096d522f475Smrg
4097d522f475Smrg    for (i = 0; i < nparam; i++) {
4098d522f475Smrg	TRACE(("savemodes %d\n", param[i]));
4099d522f475Smrg	switch (param[i]) {
4100d522f475Smrg	case 1:		/* DECCKM                       */
4101d522f475Smrg	    DoSM(DP_DECCKM, xw->keyboard.flags & MODE_DECCKM);
4102d522f475Smrg	    break;
4103d522f475Smrg	case 3:		/* DECCOLM                      */
4104d522f475Smrg	    if (screen->c132)
4105d522f475Smrg		DoSM(DP_DECCOLM, xw->flags & IN132COLUMNS);
4106d522f475Smrg	    break;
4107d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
4108d522f475Smrg	    DoSM(DP_DECSCLM, xw->flags & SMOOTHSCROLL);
4109d522f475Smrg	    break;
4110d522f475Smrg	case 5:		/* DECSCNM                      */
4111d522f475Smrg	    DoSM(DP_DECSCNM, xw->flags & REVERSE_VIDEO);
4112d522f475Smrg	    break;
4113d522f475Smrg	case 6:		/* DECOM                        */
4114d522f475Smrg	    DoSM(DP_DECOM, xw->flags & ORIGIN);
4115d522f475Smrg	    break;
4116d522f475Smrg	case 7:		/* DECAWM                       */
4117d522f475Smrg	    DoSM(DP_DECAWM, xw->flags & WRAPAROUND);
4118d522f475Smrg	    break;
4119d522f475Smrg	case 8:		/* DECARM                       */
4120d522f475Smrg	    /* ignore autorepeat */
4121d522f475Smrg	    break;
4122d522f475Smrg	case SET_X10_MOUSE:	/* mouse bogus sequence */
4123d522f475Smrg	    DoSM(DP_X_X10MSE, screen->send_mouse_pos);
4124d522f475Smrg	    break;
4125d522f475Smrg#if OPT_TOOLBAR
4126d522f475Smrg	case 10:		/* rxvt */
4127d522f475Smrg	    DoSM(DP_TOOLBAR, resource.toolBar);
4128d522f475Smrg	    break;
4129d522f475Smrg#endif
4130d522f475Smrg#if OPT_BLINK_CURS
4131d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
4132d522f475Smrg	    if (screen->cursor_blink_res) {
4133d522f475Smrg		DoSM(DP_CRS_BLINK, screen->cursor_blink_esc);
4134d522f475Smrg	    }
4135d522f475Smrg	    break;
4136d522f475Smrg#endif
4137d522f475Smrg	case 18:		/* DECPFF: print form feed */
4138d522f475Smrg	    DoSM(DP_PRN_FORMFEED, screen->printer_formfeed);
4139d522f475Smrg	    break;
4140d522f475Smrg	case 19:		/* DECPEX: print extent */
4141d522f475Smrg	    DoSM(DP_PRN_EXTENT, screen->printer_extent);
4142d522f475Smrg	    break;
4143d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
4144d522f475Smrg	    DoSM(DP_CRS_VISIBLE, screen->cursor_set);
4145d522f475Smrg	    break;
4146d522f475Smrg	case 40:		/* 132 column mode              */
4147d522f475Smrg	    DoSM(DP_X_DECCOLM, screen->c132);
4148d522f475Smrg	    break;
4149d522f475Smrg	case 41:		/* curses hack                  */
4150d522f475Smrg	    DoSM(DP_X_MORE, screen->curses);
4151d522f475Smrg	    break;
4152d522f475Smrg	case 42:		/* DECNRCM national charset (VT220) */
4153d522f475Smrg	    /* do nothing */
4154d522f475Smrg	    break;
4155d522f475Smrg	case 44:		/* margin bell                  */
4156d522f475Smrg	    DoSM(DP_X_MARGIN, screen->marginbell);
4157d522f475Smrg	    break;
4158d522f475Smrg	case 45:		/* reverse wraparound   */
4159d522f475Smrg	    DoSM(DP_X_REVWRAP, xw->flags & REVERSEWRAP);
4160d522f475Smrg	    break;
4161d522f475Smrg#ifdef ALLOWLOGGING
4162d522f475Smrg	case 46:		/* logging              */
4163d522f475Smrg	    DoSM(DP_X_LOGGING, screen->logging);
4164d522f475Smrg	    break;
4165d522f475Smrg#endif
4166d522f475Smrg	case 1047:		/* alternate buffer             */
4167d522f475Smrg	    /* FALLTHRU */
4168d522f475Smrg	case 47:		/* alternate buffer             */
4169d522f475Smrg	    DoSM(DP_X_ALTSCRN, screen->alternate);
4170d522f475Smrg	    break;
4171d522f475Smrg	case SET_VT200_MOUSE:	/* mouse bogus sequence         */
4172d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:
4173d522f475Smrg	case SET_BTN_EVENT_MOUSE:
4174d522f475Smrg	case SET_ANY_EVENT_MOUSE:
4175d522f475Smrg	    DoSM(DP_X_MOUSE, screen->send_mouse_pos);
4176d522f475Smrg	    break;
4177d522f475Smrg#if OPT_FOCUS_EVENT
4178d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
4179d522f475Smrg	    DoSM(DP_X_FOCUS, screen->send_focus_pos);
4180d522f475Smrg	    break;
4181d522f475Smrg#endif
4182d522f475Smrg	case 1048:
4183d522f475Smrg	    if (!xw->misc.titeInhibit) {
4184d522f475Smrg		CursorSave(xw);
4185d522f475Smrg	    }
4186d522f475Smrg	    break;
4187d522f475Smrg#if OPT_READLINE
4188d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4189d522f475Smrg	    SCREEN_FLAG_save(screen, click1_moves);
4190d522f475Smrg	    break;
4191d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4192d522f475Smrg	    SCREEN_FLAG_save(screen, paste_moves);
4193d522f475Smrg	    break;
4194d522f475Smrg	case SET_DBUTTON3_DELETE:
4195d522f475Smrg	    SCREEN_FLAG_save(screen, dclick3_deletes);
4196d522f475Smrg	    break;
4197d522f475Smrg	case SET_PASTE_IN_BRACKET:
4198d522f475Smrg	    SCREEN_FLAG_save(screen, paste_brackets);
4199d522f475Smrg	    break;
4200d522f475Smrg	case SET_PASTE_QUOTE:
4201d522f475Smrg	    SCREEN_FLAG_save(screen, paste_quotes);
4202d522f475Smrg	    break;
4203d522f475Smrg	case SET_PASTE_LITERAL_NL:
4204d522f475Smrg	    SCREEN_FLAG_save(screen, paste_literal_nl);
4205d522f475Smrg	    break;
4206d522f475Smrg#endif /* OPT_READLINE */
4207d522f475Smrg	}
4208d522f475Smrg    }
4209d522f475Smrg}
4210d522f475Smrg
4211d522f475Smrg/*
4212d522f475Smrg * process xterm private modes restore
4213d522f475Smrg */
4214d522f475Smrgstatic void
4215d522f475Smrgrestoremodes(XtermWidget xw)
4216d522f475Smrg{
4217d522f475Smrg    TScreen *screen = &xw->screen;
4218d522f475Smrg    int i, j;
4219d522f475Smrg
4220d522f475Smrg    for (i = 0; i < nparam; i++) {
4221d522f475Smrg	TRACE(("restoremodes %d\n", param[i]));
4222d522f475Smrg	switch (param[i]) {
4223d522f475Smrg	case 1:		/* DECCKM                       */
4224d522f475Smrg	    bitcpy(&xw->keyboard.flags,
4225d522f475Smrg		   screen->save_modes[DP_DECCKM], MODE_DECCKM);
4226d522f475Smrg	    update_appcursor();
4227d522f475Smrg	    break;
4228d522f475Smrg	case 3:		/* DECCOLM                      */
4229d522f475Smrg	    if (screen->c132) {
4230d522f475Smrg		ClearScreen(xw);
4231d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
4232d522f475Smrg		if ((j = (screen->save_modes[DP_DECCOLM] & IN132COLUMNS)
4233d522f475Smrg		     ? 132 : 80) != ((xw->flags & IN132COLUMNS)
4234d522f475Smrg				     ? 132 : 80) || j != MaxCols(screen))
4235d522f475Smrg		    RequestResize(xw, -1, j, True);
4236d522f475Smrg		bitcpy(&xw->flags,
4237d522f475Smrg		       screen->save_modes[DP_DECCOLM],
4238d522f475Smrg		       IN132COLUMNS);
4239d522f475Smrg	    }
4240d522f475Smrg	    break;
4241d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
4242d522f475Smrg	    if (screen->save_modes[DP_DECSCLM] & SMOOTHSCROLL) {
4243d522f475Smrg		screen->jumpscroll = 0;
4244d522f475Smrg		if (screen->scroll_amt)
4245d522f475Smrg		    FlushScroll(xw);
4246d522f475Smrg	    } else
4247d522f475Smrg		screen->jumpscroll = 1;
4248d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECSCLM], SMOOTHSCROLL);
4249d522f475Smrg	    update_jumpscroll();
4250d522f475Smrg	    break;
4251d522f475Smrg	case 5:		/* DECSCNM                      */
4252d522f475Smrg	    if ((screen->save_modes[DP_DECSCNM] ^ xw->flags) & REVERSE_VIDEO) {
4253d522f475Smrg		bitcpy(&xw->flags, screen->save_modes[DP_DECSCNM], REVERSE_VIDEO);
4254d522f475Smrg		ReverseVideo(xw);
4255d522f475Smrg		/* update_reversevideo done in RevVid */
4256d522f475Smrg	    }
4257d522f475Smrg	    break;
4258d522f475Smrg	case 6:		/* DECOM                        */
4259d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECOM], ORIGIN);
4260d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
4261d522f475Smrg	    break;
4262d522f475Smrg
4263d522f475Smrg	case 7:		/* DECAWM                       */
4264d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECAWM], WRAPAROUND);
4265d522f475Smrg	    update_autowrap();
4266d522f475Smrg	    break;
4267d522f475Smrg	case 8:		/* DECARM                       */
4268d522f475Smrg	    /* ignore autorepeat */
4269d522f475Smrg	    break;
4270d522f475Smrg	case SET_X10_MOUSE:	/* MIT bogus sequence           */
4271d522f475Smrg	    DoRM(DP_X_X10MSE, screen->send_mouse_pos);
4272d522f475Smrg	    break;
4273d522f475Smrg#if OPT_TOOLBAR
4274d522f475Smrg	case 10:		/* rxvt */
4275d522f475Smrg	    DoRM(DP_TOOLBAR, resource.toolBar);
4276d522f475Smrg	    ShowToolbar(resource.toolBar);
4277d522f475Smrg	    break;
4278d522f475Smrg#endif
4279d522f475Smrg#if OPT_BLINK_CURS
4280d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
4281d522f475Smrg	    if (screen->cursor_blink_res) {
4282d522f475Smrg		DoRM(DP_CRS_BLINK, screen->cursor_blink_esc);
4283d522f475Smrg		SetCursorBlink(screen, screen->cursor_blink);
4284d522f475Smrg	    }
4285d522f475Smrg	    break;
4286d522f475Smrg#endif
4287d522f475Smrg	case 18:		/* DECPFF: print form feed */
4288d522f475Smrg	    DoRM(DP_PRN_FORMFEED, screen->printer_formfeed);
4289d522f475Smrg	    break;
4290d522f475Smrg	case 19:		/* DECPEX: print extent */
4291d522f475Smrg	    DoRM(DP_PRN_EXTENT, screen->printer_extent);
4292d522f475Smrg	    break;
4293d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
4294d522f475Smrg	    DoRM(DP_CRS_VISIBLE, screen->cursor_set);
4295d522f475Smrg	    break;
4296d522f475Smrg	case 40:		/* 132 column mode              */
4297d522f475Smrg	    DoRM(DP_X_DECCOLM, screen->c132);
4298d522f475Smrg	    update_allow132();
4299d522f475Smrg	    break;
4300d522f475Smrg	case 41:		/* curses hack                  */
4301d522f475Smrg	    DoRM(DP_X_MORE, screen->curses);
4302d522f475Smrg	    update_cursesemul();
4303d522f475Smrg	    break;
4304d522f475Smrg	case 44:		/* margin bell                  */
4305d522f475Smrg	    if ((DoRM(DP_X_MARGIN, screen->marginbell)) == 0)
4306d522f475Smrg		screen->bellarmed = -1;
4307d522f475Smrg	    update_marginbell();
4308d522f475Smrg	    break;
4309d522f475Smrg	case 45:		/* reverse wraparound   */
4310d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_X_REVWRAP], REVERSEWRAP);
4311d522f475Smrg	    update_reversewrap();
4312d522f475Smrg	    break;
4313d522f475Smrg#ifdef ALLOWLOGGING
4314d522f475Smrg	case 46:		/* logging              */
4315d522f475Smrg#ifdef ALLOWLOGFILEONOFF
4316d522f475Smrg	    if (screen->save_modes[DP_X_LOGGING])
4317d522f475Smrg		StartLog(screen);
4318d522f475Smrg	    else
4319d522f475Smrg		CloseLog(screen);
4320d522f475Smrg#endif /* ALLOWLOGFILEONOFF */
4321d522f475Smrg	    /* update_logging done by StartLog and CloseLog */
4322d522f475Smrg	    break;
4323d522f475Smrg#endif
4324d522f475Smrg	case 1047:		/* alternate buffer */
4325d522f475Smrg	    /* FALLTHRU */
4326d522f475Smrg	case 47:		/* alternate buffer */
4327d522f475Smrg	    if (!xw->misc.titeInhibit) {
4328d522f475Smrg		if (screen->save_modes[DP_X_ALTSCRN])
4329d522f475Smrg		    ToAlternate(xw);
4330d522f475Smrg		else
4331d522f475Smrg		    FromAlternate(xw);
4332d522f475Smrg		/* update_altscreen done by ToAlt and FromAlt */
4333d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
4334d522f475Smrg		if (screen->save_modes[DP_X_ALTSCRN]) {
4335d522f475Smrg		    xtermScroll(xw, screen->max_row);
4336d522f475Smrg		}
4337d522f475Smrg	    }
4338d522f475Smrg	    break;
4339d522f475Smrg	case SET_VT200_MOUSE:	/* mouse bogus sequence         */
4340d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:
4341d522f475Smrg	case SET_BTN_EVENT_MOUSE:
4342d522f475Smrg	case SET_ANY_EVENT_MOUSE:
4343d522f475Smrg	    DoRM(DP_X_MOUSE, screen->send_mouse_pos);
4344d522f475Smrg	    break;
4345d522f475Smrg#if OPT_FOCUS_EVENT
4346d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
4347d522f475Smrg	    DoRM(DP_X_FOCUS, screen->send_focus_pos);
4348d522f475Smrg	    break;
4349d522f475Smrg#endif
4350d522f475Smrg	case 1048:
4351d522f475Smrg	    if (!xw->misc.titeInhibit) {
4352d522f475Smrg		CursorRestore(xw);
4353d522f475Smrg	    }
4354d522f475Smrg	    break;
4355d522f475Smrg#if OPT_READLINE
4356d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4357d522f475Smrg	    SCREEN_FLAG_restore(screen, click1_moves);
4358d522f475Smrg	    break;
4359d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4360d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_moves);
4361d522f475Smrg	    break;
4362d522f475Smrg	case SET_DBUTTON3_DELETE:
4363d522f475Smrg	    SCREEN_FLAG_restore(screen, dclick3_deletes);
4364d522f475Smrg	    break;
4365d522f475Smrg	case SET_PASTE_IN_BRACKET:
4366d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_brackets);
4367d522f475Smrg	    break;
4368d522f475Smrg	case SET_PASTE_QUOTE:
4369d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_quotes);
4370d522f475Smrg	    break;
4371d522f475Smrg	case SET_PASTE_LITERAL_NL:
4372d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_literal_nl);
4373d522f475Smrg	    break;
4374d522f475Smrg#endif /* OPT_READLINE */
4375d522f475Smrg	}
4376d522f475Smrg    }
4377d522f475Smrg}
4378d522f475Smrg
4379d522f475Smrg/*
4380d522f475Smrg * Report window label (icon or title) in dtterm protocol
4381d522f475Smrg * ESC ] code label ESC backslash
4382d522f475Smrg */
4383d522f475Smrgstatic void
4384d522f475Smrgreport_win_label(XtermWidget xw,
4385d522f475Smrg		 int code,
4386d522f475Smrg		 XTextProperty * text,
4387d522f475Smrg		 Status ok)
4388d522f475Smrg{
4389d522f475Smrg    char **list;
4390d522f475Smrg    int length = 0;
4391d522f475Smrg
4392d522f475Smrg    reply.a_type = ANSI_ESC;
4393d522f475Smrg    unparseputc(xw, ANSI_ESC);
4394d522f475Smrg    unparseputc(xw, ']');
4395d522f475Smrg    unparseputc(xw, code);
4396d522f475Smrg
4397d522f475Smrg    if (ok) {
4398d522f475Smrg	if (XTextPropertyToStringList(text, &list, &length)) {
4399d522f475Smrg	    int n, c;
4400d522f475Smrg	    for (n = 0; n < length; n++) {
4401d522f475Smrg		char *s = list[n];
4402d522f475Smrg		while ((c = *s++) != '\0')
4403d522f475Smrg		    unparseputc(xw, c);
4404d522f475Smrg	    }
4405d522f475Smrg	    XFreeStringList(list);
4406d522f475Smrg	}
4407d522f475Smrg	if (text->value != 0)
4408d522f475Smrg	    XFree(text->value);
4409d522f475Smrg    }
4410d522f475Smrg
4411d522f475Smrg    unparseputc(xw, ANSI_ESC);
4412d522f475Smrg    unparseputc(xw, '\\');	/* should be ST */
4413d522f475Smrg    unparse_end(xw);
4414d522f475Smrg}
4415d522f475Smrg
4416d522f475Smrg/*
4417d522f475Smrg * Window operations (from CDE dtterm description, as well as extensions).
4418d522f475Smrg * See also "allowWindowOps" resource.
4419d522f475Smrg */
4420d522f475Smrgstatic void
4421d522f475Smrgwindow_ops(XtermWidget xw)
4422d522f475Smrg{
4423d522f475Smrg    TScreen *screen = &xw->screen;
4424d522f475Smrg    XWindowChanges values;
4425d522f475Smrg    XWindowAttributes win_attrs;
4426d522f475Smrg    XTextProperty text;
4427d522f475Smrg    unsigned value_mask;
4428d522f475Smrg#if OPT_MAXIMIZE
4429d522f475Smrg    unsigned root_width;
4430d522f475Smrg    unsigned root_height;
4431d522f475Smrg#endif
4432d522f475Smrg
4433d522f475Smrg    TRACE(("window_ops %d\n", param[0]));
4434d522f475Smrg    switch (param[0]) {
4435d522f475Smrg    case 1:			/* Restore (de-iconify) window */
4436d522f475Smrg	XMapWindow(screen->display,
4437d522f475Smrg		   VShellWindow);
4438d522f475Smrg	break;
4439d522f475Smrg
4440d522f475Smrg    case 2:			/* Minimize (iconify) window */
4441d522f475Smrg	XIconifyWindow(screen->display,
4442d522f475Smrg		       VShellWindow,
4443d522f475Smrg		       DefaultScreen(screen->display));
4444d522f475Smrg	break;
4445d522f475Smrg
4446d522f475Smrg    case 3:			/* Move the window to the given position */
4447d522f475Smrg	values.x = param[1];
4448d522f475Smrg	values.y = param[2];
4449d522f475Smrg	value_mask = (CWX | CWY);
4450d522f475Smrg	XReconfigureWMWindow(screen->display,
4451d522f475Smrg			     VShellWindow,
4452d522f475Smrg			     DefaultScreen(screen->display),
4453d522f475Smrg			     value_mask,
4454d522f475Smrg			     &values);
4455d522f475Smrg	break;
4456d522f475Smrg
4457d522f475Smrg    case 4:			/* Resize the window to given size in pixels */
4458d522f475Smrg	RequestResize(xw, param[1], param[2], False);
4459d522f475Smrg	break;
4460d522f475Smrg
4461d522f475Smrg    case 5:			/* Raise the window to the front of the stack */
4462d522f475Smrg	XRaiseWindow(screen->display, VShellWindow);
4463d522f475Smrg	break;
4464d522f475Smrg
4465d522f475Smrg    case 6:			/* Lower the window to the bottom of the stack */
4466d522f475Smrg	XLowerWindow(screen->display, VShellWindow);
4467d522f475Smrg	break;
4468d522f475Smrg
4469d522f475Smrg    case 7:			/* Refresh the window */
4470d522f475Smrg	Redraw();
4471d522f475Smrg	break;
4472d522f475Smrg
4473d522f475Smrg    case 8:			/* Resize the text-area, in characters */
4474d522f475Smrg	RequestResize(xw, param[1], param[2], True);
4475d522f475Smrg	break;
4476d522f475Smrg
4477d522f475Smrg#if OPT_MAXIMIZE
4478d522f475Smrg    case 9:			/* Maximize or restore */
4479d522f475Smrg	RequestMaximize(xw, param[1]);
4480d522f475Smrg	break;
4481d522f475Smrg#endif
4482d522f475Smrg
4483d522f475Smrg    case 11:			/* Report the window's state */
4484d522f475Smrg	XGetWindowAttributes(screen->display,
4485d522f475Smrg			     VWindow(screen),
4486d522f475Smrg			     &win_attrs);
4487d522f475Smrg	reply.a_type = ANSI_CSI;
4488d522f475Smrg	reply.a_pintro = 0;
4489d522f475Smrg	reply.a_nparam = 1;
4490d522f475Smrg	reply.a_param[0] = (win_attrs.map_state == IsViewable) ? 1 : 2;
4491d522f475Smrg	reply.a_inters = 0;
4492d522f475Smrg	reply.a_final = 't';
4493d522f475Smrg	unparseseq(xw, &reply);
4494d522f475Smrg	break;
4495d522f475Smrg
4496d522f475Smrg    case 13:			/* Report the window's position */
4497d522f475Smrg	XGetWindowAttributes(screen->display,
4498d522f475Smrg			     WMFrameWindow(xw),
4499d522f475Smrg			     &win_attrs);
4500d522f475Smrg	reply.a_type = ANSI_CSI;
4501d522f475Smrg	reply.a_pintro = 0;
4502d522f475Smrg	reply.a_nparam = 3;
4503d522f475Smrg	reply.a_param[0] = 3;
4504d522f475Smrg	reply.a_param[1] = win_attrs.x;
4505d522f475Smrg	reply.a_param[2] = win_attrs.y;
4506d522f475Smrg	reply.a_inters = 0;
4507d522f475Smrg	reply.a_final = 't';
4508d522f475Smrg	unparseseq(xw, &reply);
4509d522f475Smrg	break;
4510d522f475Smrg
4511d522f475Smrg    case 14:			/* Report the window's size in pixels */
4512d522f475Smrg	XGetWindowAttributes(screen->display,
4513d522f475Smrg			     VWindow(screen),
4514d522f475Smrg			     &win_attrs);
4515d522f475Smrg	reply.a_type = ANSI_CSI;
4516d522f475Smrg	reply.a_pintro = 0;
4517d522f475Smrg	reply.a_nparam = 3;
4518d522f475Smrg	reply.a_param[0] = 4;
4519d522f475Smrg	/*FIXME: find if dtterm uses
4520d522f475Smrg	 *    win_attrs.height or Height
4521d522f475Smrg	 *      win_attrs.width  or Width
4522d522f475Smrg	 */
4523d522f475Smrg	reply.a_param[1] = Height(screen);
4524d522f475Smrg	reply.a_param[2] = Width(screen);
4525d522f475Smrg	reply.a_inters = 0;
4526d522f475Smrg	reply.a_final = 't';
4527d522f475Smrg	unparseseq(xw, &reply);
4528d522f475Smrg	break;
4529d522f475Smrg
4530d522f475Smrg    case 18:			/* Report the text's size in characters */
4531d522f475Smrg	reply.a_type = ANSI_CSI;
4532d522f475Smrg	reply.a_pintro = 0;
4533d522f475Smrg	reply.a_nparam = 3;
4534d522f475Smrg	reply.a_param[0] = 8;
4535d522f475Smrg	reply.a_param[1] = MaxRows(screen);
4536d522f475Smrg	reply.a_param[2] = MaxCols(screen);
4537d522f475Smrg	reply.a_inters = 0;
4538d522f475Smrg	reply.a_final = 't';
4539d522f475Smrg	unparseseq(xw, &reply);
4540d522f475Smrg	break;
4541d522f475Smrg
4542d522f475Smrg#if OPT_MAXIMIZE
4543d522f475Smrg    case 19:			/* Report the screen's size, in characters */
4544d522f475Smrg	if (!QueryMaximize(xw, &root_height, &root_width)) {
4545d522f475Smrg	    root_height = 0;
4546d522f475Smrg	    root_width = 0;
4547d522f475Smrg	}
4548d522f475Smrg	reply.a_type = ANSI_CSI;
4549d522f475Smrg	reply.a_pintro = 0;
4550d522f475Smrg	reply.a_nparam = 3;
4551d522f475Smrg	reply.a_param[0] = 9;
4552d522f475Smrg	reply.a_param[1] = root_height / FontHeight(screen);
4553d522f475Smrg	reply.a_param[2] = root_width / FontWidth(screen);
4554d522f475Smrg	reply.a_inters = 0;
4555d522f475Smrg	reply.a_final = 't';
4556d522f475Smrg	unparseseq(xw, &reply);
4557d522f475Smrg	break;
4558d522f475Smrg#endif
4559d522f475Smrg
4560d522f475Smrg    case 20:			/* Report the icon's label */
4561d522f475Smrg	report_win_label(xw, 'L', &text,
4562d522f475Smrg			 XGetWMIconName(screen->display, VShellWindow, &text));
4563d522f475Smrg	break;
4564d522f475Smrg
4565d522f475Smrg    case 21:			/* Report the window's title */
4566d522f475Smrg	report_win_label(xw, 'l', &text,
4567d522f475Smrg			 XGetWMName(screen->display, VShellWindow, &text));
4568d522f475Smrg	break;
4569d522f475Smrg
4570d522f475Smrg    default:			/* DECSLPP (24, 25, 36, 48, 72, 144) */
4571d522f475Smrg	if (param[0] >= 24)
4572d522f475Smrg	    RequestResize(xw, param[0], -1, True);
4573d522f475Smrg	break;
4574d522f475Smrg    }
4575d522f475Smrg}
4576d522f475Smrg
4577d522f475Smrg/*
4578d522f475Smrg * set a bit in a word given a pointer to the word and a mask.
4579d522f475Smrg */
4580d522f475Smrgstatic void
4581d522f475Smrgbitset(unsigned *p, unsigned mask)
4582d522f475Smrg{
4583d522f475Smrg    *p |= mask;
4584d522f475Smrg}
4585d522f475Smrg
4586d522f475Smrg/*
4587d522f475Smrg * clear a bit in a word given a pointer to the word and a mask.
4588d522f475Smrg */
4589d522f475Smrgstatic void
4590d522f475Smrgbitclr(unsigned *p, unsigned mask)
4591d522f475Smrg{
4592d522f475Smrg    *p &= ~mask;
4593d522f475Smrg}
4594d522f475Smrg
4595d522f475Smrg/*
4596d522f475Smrg * Copy bits from one word to another, given a mask
4597d522f475Smrg */
4598d522f475Smrgstatic void
4599d522f475Smrgbitcpy(unsigned *p, unsigned q, unsigned mask)
4600d522f475Smrg{
4601d522f475Smrg    bitclr(p, mask);
4602d522f475Smrg    bitset(p, q & mask);
4603d522f475Smrg}
4604d522f475Smrg
4605d522f475Smrgvoid
4606d522f475Smrgunparseputc1(XtermWidget xw, int c)
4607d522f475Smrg{
4608d522f475Smrg    if (c >= 0x80 && c <= 0x9F) {
4609d522f475Smrg	if (!xw->screen.control_eight_bits) {
4610d522f475Smrg	    unparseputc(xw, A2E(ANSI_ESC));
4611d522f475Smrg	    c = A2E(c - 0x40);
4612d522f475Smrg	}
4613d522f475Smrg    }
4614d522f475Smrg    unparseputc(xw, c);
4615d522f475Smrg}
4616d522f475Smrg
4617d522f475Smrgvoid
4618d522f475Smrgunparseseq(XtermWidget xw, ANSI * ap)
4619d522f475Smrg{
4620d522f475Smrg    int c;
4621d522f475Smrg    int i;
4622d522f475Smrg    int inters;
4623d522f475Smrg
4624d522f475Smrg    unparseputc1(xw, c = ap->a_type);
4625d522f475Smrg    if (c == ANSI_ESC
4626d522f475Smrg	|| c == ANSI_DCS
4627d522f475Smrg	|| c == ANSI_CSI
4628d522f475Smrg	|| c == ANSI_OSC
4629d522f475Smrg	|| c == ANSI_PM
4630d522f475Smrg	|| c == ANSI_APC
4631d522f475Smrg	|| c == ANSI_SS3) {
4632d522f475Smrg	if (ap->a_pintro != 0)
4633d522f475Smrg	    unparseputc(xw, ap->a_pintro);
4634d522f475Smrg	for (i = 0; i < ap->a_nparam; ++i) {
4635d522f475Smrg	    if (i != 0)
4636d522f475Smrg		unparseputc(xw, ';');
4637d522f475Smrg	    unparseputn(xw, (unsigned int) ap->a_param[i]);
4638d522f475Smrg	}
4639d522f475Smrg	if ((inters = ap->a_inters) != 0) {
4640d522f475Smrg	    for (i = 3; i >= 0; --i) {
4641d522f475Smrg		c = CharOf(inters >> (8 * i));
4642d522f475Smrg		if (c != 0)
4643d522f475Smrg		    unparseputc(xw, c);
4644d522f475Smrg	    }
4645d522f475Smrg	}
4646d522f475Smrg	unparseputc(xw, (char) ap->a_final);
4647d522f475Smrg    }
4648d522f475Smrg    unparse_end(xw);
4649d522f475Smrg}
4650d522f475Smrg
4651d522f475Smrgvoid
4652d522f475Smrgunparseputn(XtermWidget xw, unsigned int n)
4653d522f475Smrg{
4654d522f475Smrg    unsigned int q;
4655d522f475Smrg
4656d522f475Smrg    q = n / 10;
4657d522f475Smrg    if (q != 0)
4658d522f475Smrg	unparseputn(xw, q);
4659d522f475Smrg    unparseputc(xw, (char) ('0' + (n % 10)));
4660d522f475Smrg}
4661d522f475Smrg
4662d522f475Smrgvoid
4663d522f475Smrgunparseputs(XtermWidget xw, char *s)
4664d522f475Smrg{
4665d522f475Smrg    while (*s)
4666d522f475Smrg	unparseputc(xw, *s++);
4667d522f475Smrg}
4668d522f475Smrg
4669d522f475Smrgvoid
4670d522f475Smrgunparseputc(XtermWidget xw, int c)
4671d522f475Smrg{
4672d522f475Smrg    IChar *buf = xw->screen.unparse_bfr;
4673d522f475Smrg    unsigned len;
4674d522f475Smrg
4675d522f475Smrg    if ((xw->screen.unparse_len + 2) >= sizeof(xw->screen.unparse_bfr))
4676d522f475Smrg	unparse_end(xw);
4677d522f475Smrg
4678d522f475Smrg    len = xw->screen.unparse_len;
4679d522f475Smrg
4680d522f475Smrg#if OPT_TCAP_QUERY
4681d522f475Smrg    /*
4682d522f475Smrg     * If we're returning a termcap string, it has to be translated since
4683d522f475Smrg     * a DCS must not contain any characters except for the normal 7-bit
4684d522f475Smrg     * printable ASCII (counting tab, carriage return, etc).  For now,
4685d522f475Smrg     * just use hexadecimal for the whole thing.
4686d522f475Smrg     */
4687d522f475Smrg    if (xw->screen.tc_query_code >= 0) {
4688d522f475Smrg	char tmp[3];
4689d522f475Smrg	sprintf(tmp, "%02X", c & 0xFF);
4690d522f475Smrg	buf[len++] = tmp[0];
4691d522f475Smrg	buf[len++] = tmp[1];
4692d522f475Smrg    } else
4693d522f475Smrg#endif
4694d522f475Smrg    if ((buf[len++] = c) == '\r' && (xw->flags & LINEFEED)) {
4695d522f475Smrg	buf[len++] = '\n';
4696d522f475Smrg    }
4697d522f475Smrg
4698d522f475Smrg    xw->screen.unparse_len = len;
4699d522f475Smrg
4700d522f475Smrg    /* If send/receive mode is reset, we echo characters locally */
4701d522f475Smrg    if ((xw->keyboard.flags & MODE_SRM) == 0) {
4702d522f475Smrg	(void) doparsing(xw, (unsigned) c, &myState);
4703d522f475Smrg    }
4704d522f475Smrg}
4705d522f475Smrg
4706d522f475Smrgvoid
4707d522f475Smrgunparse_end(XtermWidget xw)
4708d522f475Smrg{
4709d522f475Smrg    if (xw->screen.unparse_len) {
4710d522f475Smrg#ifdef VMS
4711d522f475Smrg	tt_write(xw->screen.unparse_bfr, xw->screen.unparse_len);
4712d522f475Smrg#else /* VMS */
4713d522f475Smrg	writePtyData(xw->screen.respond, xw->screen.unparse_bfr, xw->screen.unparse_len);
4714d522f475Smrg#endif /* VMS */
4715d522f475Smrg	xw->screen.unparse_len = 0;
4716d522f475Smrg    }
4717d522f475Smrg}
4718d522f475Smrg
4719d522f475Smrgvoid
4720d522f475SmrgToggleAlternate(XtermWidget xw)
4721d522f475Smrg{
4722d522f475Smrg    if (xw->screen.alternate)
4723d522f475Smrg	FromAlternate(xw);
4724d522f475Smrg    else
4725d522f475Smrg	ToAlternate(xw);
4726d522f475Smrg}
4727d522f475Smrg
4728d522f475Smrgstatic void
4729d522f475SmrgToAlternate(XtermWidget xw)
4730d522f475Smrg{
4731d522f475Smrg    TScreen *screen = &(xw->screen);
4732d522f475Smrg
4733d522f475Smrg    if (!screen->alternate) {
4734d522f475Smrg	TRACE(("ToAlternate\n"));
4735d522f475Smrg	if (!screen->altbuf)
4736d522f475Smrg	    screen->altbuf = Allocate(MaxRows(screen), MaxCols(screen),
4737d522f475Smrg				      &screen->abuf_address);
4738d522f475Smrg	SwitchBufs(xw);
4739d522f475Smrg	screen->alternate = True;
4740d522f475Smrg	update_altscreen();
4741d522f475Smrg    }
4742d522f475Smrg}
4743d522f475Smrg
4744d522f475Smrgstatic void
4745d522f475SmrgFromAlternate(XtermWidget xw)
4746d522f475Smrg{
4747d522f475Smrg    TScreen *screen = &(xw->screen);
4748d522f475Smrg
4749d522f475Smrg    if (screen->alternate) {
4750d522f475Smrg	TRACE(("FromAlternate\n"));
4751d522f475Smrg	if (screen->scroll_amt)
4752d522f475Smrg	    FlushScroll(xw);
4753d522f475Smrg	screen->alternate = False;
4754d522f475Smrg	SwitchBufs(xw);
4755d522f475Smrg	update_altscreen();
4756d522f475Smrg    }
4757d522f475Smrg}
4758d522f475Smrg
4759d522f475Smrgstatic void
4760d522f475SmrgSwitchBufs(XtermWidget xw)
4761d522f475Smrg{
4762d522f475Smrg    TScreen *screen = &(xw->screen);
4763d522f475Smrg    int rows, top;
4764d522f475Smrg
4765d522f475Smrg    if (screen->cursor_state)
4766d522f475Smrg	HideCursor();
4767d522f475Smrg
4768d522f475Smrg    rows = MaxRows(screen);
4769d522f475Smrg    SwitchBufPtrs(screen);
4770d522f475Smrg
4771d522f475Smrg    if ((top = INX2ROW(screen, 0)) < rows) {
4772d522f475Smrg	if (screen->scroll_amt)
4773d522f475Smrg	    FlushScroll(xw);
4774d522f475Smrg	XClearArea(screen->display,
4775d522f475Smrg		   VWindow(screen),
4776d522f475Smrg		   (int) OriginX(screen),
4777d522f475Smrg		   (int) top * FontHeight(screen) + screen->border,
4778d522f475Smrg		   (unsigned) Width(screen),
4779d522f475Smrg		   (unsigned) (rows - top) * FontHeight(screen),
4780d522f475Smrg		   False);
4781d522f475Smrg    }
4782d522f475Smrg    ScrnUpdate(xw, 0, 0, rows, MaxCols(screen), False);
4783d522f475Smrg}
4784d522f475Smrg
4785d522f475SmrgBool
4786d522f475SmrgCheckBufPtrs(TScreen * screen)
4787d522f475Smrg{
4788d522f475Smrg    return (screen->visbuf != 0
4789d522f475Smrg	    && screen->altbuf != 0);
4790d522f475Smrg}
4791d522f475Smrg
4792d522f475Smrg/*
4793d522f475Smrg * Swap buffer line pointers between alternate and regular screens.
4794d522f475Smrg * visbuf contains pointers from allbuf or altbuf for the visible screen,
4795d522f475Smrg * and pointers from allbuf for the saved lines.  That makes it simple to
4796d522f475Smrg * scroll back over the saved lines without juggling pointers for the
4797d522f475Smrg * regular and alternate screens.
4798d522f475Smrg */
4799d522f475Smrgvoid
4800d522f475SmrgSwitchBufPtrs(TScreen * screen)
4801d522f475Smrg{
4802d522f475Smrg    if (CheckBufPtrs(screen)) {
4803d522f475Smrg	size_t len = ScrnPointers(screen, (unsigned) MaxRows(screen));
4804d522f475Smrg
4805d522f475Smrg	memcpy((char *) screen->save_ptr, (char *) screen->visbuf, len);
4806d522f475Smrg	memcpy((char *) screen->visbuf, (char *) screen->altbuf, len);
4807d522f475Smrg	memcpy((char *) screen->altbuf, (char *) screen->save_ptr, len);
4808d522f475Smrg    }
4809d522f475Smrg}
4810d522f475Smrg
4811d522f475Smrgvoid
4812d522f475SmrgVTRun(void)
4813d522f475Smrg{
4814d522f475Smrg    TScreen *screen = TScreenOf(term);
4815d522f475Smrg
4816d522f475Smrg    TRACE(("VTRun ...\n"));
4817d522f475Smrg
4818d522f475Smrg    if (!screen->Vshow) {
4819d522f475Smrg	set_vt_visibility(True);
4820d522f475Smrg    }
4821d522f475Smrg    update_vttekmode();
4822d522f475Smrg    update_vtshow();
4823d522f475Smrg    update_tekshow();
4824d522f475Smrg    set_vthide_sensitivity();
4825d522f475Smrg
4826d522f475Smrg    if (screen->allbuf == NULL)
4827d522f475Smrg	VTallocbuf();
4828d522f475Smrg
4829d522f475Smrg    screen->cursor_state = OFF;
4830d522f475Smrg    screen->cursor_set = ON;
4831d522f475Smrg#if OPT_BLINK_CURS
4832d522f475Smrg    if (DoStartBlinking(screen))
4833d522f475Smrg	StartBlinking(screen);
4834d522f475Smrg#endif
4835d522f475Smrg
4836d522f475Smrg#if OPT_TEK4014
4837d522f475Smrg    if (Tpushb > Tpushback) {
4838d522f475Smrg	fillPtyData(screen, VTbuffer, (char *) Tpushback, Tpushb - Tpushback);
4839d522f475Smrg	Tpushb = Tpushback;
4840d522f475Smrg    }
4841d522f475Smrg#endif
4842d522f475Smrg    screen->is_running = True;
4843d522f475Smrg    if (!setjmp(VTend))
4844d522f475Smrg	VTparse(term);
4845d522f475Smrg    StopBlinking(screen);
4846d522f475Smrg    HideCursor();
4847d522f475Smrg    screen->cursor_set = OFF;
4848d522f475Smrg    TRACE(("... VTRun\n"));
4849d522f475Smrg}
4850d522f475Smrg
4851d522f475Smrg/*ARGSUSED*/
4852d522f475Smrgstatic void
4853d522f475SmrgVTExpose(Widget w GCC_UNUSED,
4854d522f475Smrg	 XEvent * event,
4855d522f475Smrg	 Region region GCC_UNUSED)
4856d522f475Smrg{
4857d522f475Smrg#ifdef DEBUG
4858d522f475Smrg    if (debug)
4859d522f475Smrg	fputs("Expose\n", stderr);
4860d522f475Smrg#endif /* DEBUG */
4861d522f475Smrg    if (event->type == Expose)
4862d522f475Smrg	HandleExposure(term, event);
4863d522f475Smrg}
4864d522f475Smrg
4865d522f475Smrgstatic void
4866d522f475SmrgVTGraphicsOrNoExpose(XEvent * event)
4867d522f475Smrg{
4868d522f475Smrg    TScreen *screen = TScreenOf(term);
4869d522f475Smrg    if (screen->incopy <= 0) {
4870d522f475Smrg	screen->incopy = 1;
4871d522f475Smrg	if (screen->scrolls > 0)
4872d522f475Smrg	    screen->scrolls--;
4873d522f475Smrg    }
4874d522f475Smrg    if (event->type == GraphicsExpose)
4875d522f475Smrg	if (HandleExposure(term, event))
4876d522f475Smrg	    screen->cursor_state = OFF;
4877d522f475Smrg    if ((event->type == NoExpose)
4878d522f475Smrg	|| ((XGraphicsExposeEvent *) event)->count == 0) {
4879d522f475Smrg	if (screen->incopy <= 0 && screen->scrolls > 0)
4880d522f475Smrg	    screen->scrolls--;
4881d522f475Smrg	if (screen->scrolls)
4882d522f475Smrg	    screen->incopy = -1;
4883d522f475Smrg	else
4884d522f475Smrg	    screen->incopy = 0;
4885d522f475Smrg    }
4886d522f475Smrg}
4887d522f475Smrg
4888d522f475Smrg/*ARGSUSED*/
4889d522f475Smrgstatic void
4890d522f475SmrgVTNonMaskableEvent(Widget w GCC_UNUSED,
4891d522f475Smrg		   XtPointer closure GCC_UNUSED,
4892d522f475Smrg		   XEvent * event,
4893d522f475Smrg		   Boolean * cont GCC_UNUSED)
4894d522f475Smrg{
4895d522f475Smrg    switch (event->type) {
4896d522f475Smrg    case GraphicsExpose:
4897d522f475Smrg    case NoExpose:
4898d522f475Smrg	VTGraphicsOrNoExpose(event);
4899d522f475Smrg	break;
4900d522f475Smrg    }
4901d522f475Smrg}
4902d522f475Smrg
4903d522f475Smrgstatic void
4904d522f475SmrgVTResize(Widget w)
4905d522f475Smrg{
4906d522f475Smrg    if (XtIsRealized(w)) {
4907d522f475Smrg	XtermWidget xw = (XtermWidget) w;
4908d522f475Smrg	ScreenResize(xw, xw->core.width, xw->core.height, &xw->flags);
4909d522f475Smrg    }
4910d522f475Smrg}
4911d522f475Smrg
4912d522f475Smrg#define okDimension(src,dst) ((src <= 32767) && ((dst = src) == src))
4913d522f475Smrg
4914d522f475Smrgstatic void
4915d522f475SmrgRequestResize(XtermWidget xw, int rows, int cols, Bool text)
4916d522f475Smrg{
4917d522f475Smrg    TScreen *screen = &xw->screen;
4918d522f475Smrg    unsigned long value;
4919d522f475Smrg    Dimension replyWidth, replyHeight;
4920d522f475Smrg    Dimension askedWidth, askedHeight;
4921d522f475Smrg    XtGeometryResult status;
4922d522f475Smrg    XWindowAttributes attrs;
4923d522f475Smrg
4924d522f475Smrg    TRACE(("RequestResize(rows=%d, cols=%d, text=%d)\n", rows, cols, text));
4925d522f475Smrg
4926d522f475Smrg    if ((askedWidth = cols) < cols
4927d522f475Smrg	|| (askedHeight = rows) < rows)
4928d522f475Smrg	return;
4929d522f475Smrg
4930d522f475Smrg    if (askedHeight == 0
4931d522f475Smrg	|| askedWidth == 0
4932d522f475Smrg	|| xw->misc.limit_resize > 0) {
4933d522f475Smrg	XGetWindowAttributes(XtDisplay(xw),
4934d522f475Smrg			     RootWindowOfScreen(XtScreen(xw)), &attrs);
4935d522f475Smrg    }
4936d522f475Smrg
4937d522f475Smrg    if (text) {
4938d522f475Smrg	if ((value = rows) != 0) {
4939d522f475Smrg	    if (rows < 0)
4940d522f475Smrg		value = MaxRows(screen);
4941d522f475Smrg	    value *= FontHeight(screen);
4942d522f475Smrg	    value += (2 * screen->border);
4943d522f475Smrg	    if (!okDimension(value, askedHeight))
4944d522f475Smrg		return;
4945d522f475Smrg	}
4946d522f475Smrg
4947d522f475Smrg	if ((value = cols) != 0) {
4948d522f475Smrg	    if (cols < 0)
4949d522f475Smrg		value = MaxCols(screen);
4950d522f475Smrg	    value *= FontWidth(screen);
4951d522f475Smrg	    value += (2 * screen->border) + ScrollbarWidth(screen);
4952d522f475Smrg	    if (!okDimension(value, askedWidth))
4953d522f475Smrg		return;
4954d522f475Smrg	}
4955d522f475Smrg
4956d522f475Smrg    } else {
4957d522f475Smrg	if (rows < 0)
4958d522f475Smrg	    askedHeight = FullHeight(screen);
4959d522f475Smrg	if (cols < 0)
4960d522f475Smrg	    askedWidth = FullWidth(screen);
4961d522f475Smrg    }
4962d522f475Smrg
4963d522f475Smrg    if (rows == 0)
4964d522f475Smrg	askedHeight = attrs.height;
4965d522f475Smrg    if (cols == 0)
4966d522f475Smrg	askedWidth = attrs.width;
4967d522f475Smrg
4968d522f475Smrg    if (xw->misc.limit_resize > 0) {
4969d522f475Smrg	Dimension high = xw->misc.limit_resize * attrs.height;
4970d522f475Smrg	Dimension wide = xw->misc.limit_resize * attrs.width;
4971d522f475Smrg	if (high < attrs.height)
4972d522f475Smrg	    high = attrs.height;
4973d522f475Smrg	if (askedHeight > high)
4974d522f475Smrg	    askedHeight = high;
4975d522f475Smrg	if (wide < attrs.width)
4976d522f475Smrg	    wide = attrs.width;
4977d522f475Smrg	if (askedWidth > wide)
4978d522f475Smrg	    askedWidth = wide;
4979d522f475Smrg    }
4980d522f475Smrg#ifndef nothack
4981d522f475Smrg    getXtermSizeHints(xw);
4982d522f475Smrg#endif
4983d522f475Smrg
4984d522f475Smrg    status = XtMakeResizeRequest((Widget) xw,
4985d522f475Smrg				 askedWidth, askedHeight,
4986d522f475Smrg				 &replyWidth, &replyHeight);
4987d522f475Smrg    TRACE(("...RequestResize XtMakeResizeRequest %dx%d -> %dx%d (status %d)\n",
4988d522f475Smrg	   askedHeight, askedWidth,
4989d522f475Smrg	   replyHeight, replyWidth,
4990d522f475Smrg	   status));
4991d522f475Smrg
4992d522f475Smrg    if (status == XtGeometryYes ||
4993d522f475Smrg	status == XtGeometryDone) {
4994d522f475Smrg	ScreenResize(xw, replyWidth, replyHeight, &xw->flags);
4995d522f475Smrg    }
4996d522f475Smrg#ifndef nothack
4997d522f475Smrg    /*
4998d522f475Smrg     * XtMakeResizeRequest() has the undesirable side-effect of clearing
4999d522f475Smrg     * the window manager's hints, even on a failed request.  This would
5000d522f475Smrg     * presumably be fixed if the shell did its own work.
5001d522f475Smrg     */
5002d522f475Smrg    if (xw->hints.flags
5003d522f475Smrg	&& replyHeight
5004d522f475Smrg	&& replyWidth) {
5005d522f475Smrg	xw->hints.height = replyHeight;
5006d522f475Smrg	xw->hints.width = replyWidth;
5007d522f475Smrg
5008d522f475Smrg	TRACE(("%s@%d -- ", __FILE__, __LINE__));
5009d522f475Smrg	TRACE_HINTS(&xw->hints);
5010d522f475Smrg	XSetWMNormalHints(screen->display, VShellWindow, &xw->hints);
5011d522f475Smrg	TRACE(("%s@%d -- ", __FILE__, __LINE__));
5012d522f475Smrg	TRACE_WM_HINTS(xw);
5013d522f475Smrg    }
5014d522f475Smrg#endif
5015d522f475Smrg
5016d522f475Smrg    XSync(screen->display, False);	/* synchronize */
5017d522f475Smrg    if (XtAppPending(app_con))
5018d522f475Smrg	xevents();
5019d522f475Smrg
5020d522f475Smrg    TRACE(("...RequestResize done\n"));
5021d522f475Smrg}
5022d522f475Smrg
5023d522f475Smrgstatic String xterm_trans =
5024d522f475Smrg"<ClientMessage>WM_PROTOCOLS: DeleteWindow()\n\
5025d522f475Smrg     <MappingNotify>: KeyboardMapping()\n";
5026d522f475Smrg
5027d522f475Smrgint
5028d522f475SmrgVTInit(void)
5029d522f475Smrg{
5030d522f475Smrg    TScreen *screen = TScreenOf(term);
5031d522f475Smrg    Widget vtparent = SHELL_OF(term);
5032d522f475Smrg
5033d522f475Smrg    TRACE(("VTInit {{\n"));
5034d522f475Smrg
5035d522f475Smrg    XtRealizeWidget(vtparent);
5036d522f475Smrg    XtOverrideTranslations(vtparent, XtParseTranslationTable(xterm_trans));
5037d522f475Smrg    (void) XSetWMProtocols(XtDisplay(vtparent), XtWindow(vtparent),
5038d522f475Smrg			   &wm_delete_window, 1);
5039d522f475Smrg    TRACE_TRANS("shell", vtparent);
5040d522f475Smrg    TRACE_TRANS("vt100", (Widget) (term));
5041d522f475Smrg
5042d522f475Smrg    if (screen->allbuf == NULL)
5043d522f475Smrg	VTallocbuf();
5044d522f475Smrg
5045d522f475Smrg    TRACE(("...}} VTInit\n"));
5046d522f475Smrg    return (1);
5047d522f475Smrg}
5048d522f475Smrg
5049d522f475Smrgstatic void
5050d522f475SmrgVTallocbuf(void)
5051d522f475Smrg{
5052d522f475Smrg    TScreen *screen = TScreenOf(term);
5053d522f475Smrg    int nrows = MaxRows(screen);
5054d522f475Smrg
5055d522f475Smrg    /* allocate screen buffer now, if necessary. */
5056d522f475Smrg    if (screen->scrollWidget)
5057d522f475Smrg	nrows += screen->savelines;
5058d522f475Smrg    screen->allbuf = Allocate(nrows, MaxCols(screen),
5059d522f475Smrg			      &screen->sbuf_address);
5060d522f475Smrg    if (screen->scrollWidget)
5061d522f475Smrg	screen->visbuf = &screen->allbuf[MAX_PTRS * screen->savelines];
5062d522f475Smrg    else
5063d522f475Smrg	screen->visbuf = screen->allbuf;
5064d522f475Smrg    return;
5065d522f475Smrg}
5066d522f475Smrg
5067d522f475Smrgstatic void
5068d522f475SmrgVTClassInit(void)
5069d522f475Smrg{
5070d522f475Smrg    XtAddConverter(XtRString, XtRGravity, XmuCvtStringToGravity,
5071d522f475Smrg		   (XtConvertArgList) NULL, (Cardinal) 0);
5072d522f475Smrg}
5073d522f475Smrg
5074d522f475Smrg/*
5075d522f475Smrg * The whole wnew->screen struct is zeroed in VTInitialize.  Use these macros
5076d522f475Smrg * where applicable for copying the pieces from the request widget into the
5077d522f475Smrg * new widget.  We do not have to use them for wnew->misc, but the associated
5078d522f475Smrg * traces are very useful for debugging.
5079d522f475Smrg */
5080d522f475Smrg#if OPT_TRACE
5081d522f475Smrg#define init_Bres(name) \
5082d522f475Smrg	TRACE(("init " #name " = %s\n", \
5083d522f475Smrg		BtoS(wnew->name = request->name)))
5084d522f475Smrg#define init_Dres(name) \
5085d522f475Smrg	TRACE(("init " #name " = %f\n", \
5086d522f475Smrg		wnew->name = request->name))
5087d522f475Smrg#define init_Ires(name) \
5088d522f475Smrg	TRACE(("init " #name " = %d\n", \
5089d522f475Smrg		wnew->name = request->name))
5090d522f475Smrg#define init_Sres(name) \
5091d522f475Smrg	TRACE(("init " #name " = \"%s\"\n", \
5092d522f475Smrg		(wnew->name = x_strtrim(request->name)) != NULL \
5093d522f475Smrg			? wnew->name : "<null>"))
5094d522f475Smrg#define init_Tres(offset) \
5095d522f475Smrg	TRACE(("init screen.Tcolors[" #offset "] = %#lx\n", \
5096d522f475Smrg		fill_Tres(wnew, request, offset)))
5097d522f475Smrg#else
5098d522f475Smrg#define init_Bres(name) wnew->name = request->name
5099d522f475Smrg#define init_Dres(name) wnew->name = request->name
5100d522f475Smrg#define init_Ires(name) wnew->name = request->name
5101d522f475Smrg#define init_Sres(name) wnew->name = x_strtrim(request->name)
5102d522f475Smrg#define init_Tres(offset) fill_Tres(wnew, request, offset)
5103d522f475Smrg#endif
5104d522f475Smrg
5105d522f475Smrg#if OPT_COLOR_RES
5106d522f475Smrg/*
5107d522f475Smrg * Override the use of XtDefaultForeground/XtDefaultBackground to make some
5108d522f475Smrg * colors, such as cursor color, use the actual foreground/background value
5109d522f475Smrg * if there is no explicit resource value used.
5110d522f475Smrg */
5111d522f475Smrgstatic Pixel
5112d522f475Smrgfill_Tres(XtermWidget target, XtermWidget source, int offset)
5113d522f475Smrg{
5114d522f475Smrg    char *name;
5115d522f475Smrg    ScrnColors temp;
5116d522f475Smrg
5117d522f475Smrg    target->screen.Tcolors[offset] = source->screen.Tcolors[offset];
5118d522f475Smrg    target->screen.Tcolors[offset].mode = False;
5119d522f475Smrg
5120d522f475Smrg    if ((name = x_strtrim(target->screen.Tcolors[offset].resource)) != 0)
5121d522f475Smrg	target->screen.Tcolors[offset].resource = name;
5122d522f475Smrg
5123d522f475Smrg    if (name == 0) {
5124d522f475Smrg	target->screen.Tcolors[offset].value = target->dft_foreground;
5125d522f475Smrg    } else if (isDefaultForeground(name)) {
5126d522f475Smrg	target->screen.Tcolors[offset].value =
5127d522f475Smrg	    ((offset == TEXT_FG || offset == TEXT_BG)
5128d522f475Smrg	     ? target->dft_foreground
5129d522f475Smrg	     : target->screen.Tcolors[TEXT_FG].value);
5130d522f475Smrg    } else if (isDefaultBackground(name)) {
5131d522f475Smrg	target->screen.Tcolors[offset].value =
5132d522f475Smrg	    ((offset == TEXT_FG || offset == TEXT_BG)
5133d522f475Smrg	     ? target->dft_background
5134d522f475Smrg	     : target->screen.Tcolors[TEXT_BG].value);
5135d522f475Smrg    } else {
5136d522f475Smrg	memset(&temp, 0, sizeof(temp));
5137d522f475Smrg	if (AllocateTermColor(target, &temp, offset, name)) {
5138d522f475Smrg	    if (COLOR_DEFINED(&(temp), offset))
5139d522f475Smrg		free(temp.names[offset]);
5140d522f475Smrg	    target->screen.Tcolors[offset].value = temp.colors[offset];
5141d522f475Smrg	}
5142d522f475Smrg    }
5143d522f475Smrg    return target->screen.Tcolors[offset].value;
5144d522f475Smrg}
5145d522f475Smrg#else
5146d522f475Smrg#define fill_Tres(target, source, offset) \
5147d522f475Smrg	target->screen.Tcolors[offset] = source->screen.Tcolors[offset]
5148d522f475Smrg#endif
5149d522f475Smrg
5150d522f475Smrg#if OPT_WIDE_CHARS
5151d522f475Smrgstatic void
5152d522f475SmrgVTInitialize_locale(XtermWidget request)
5153d522f475Smrg{
5154d522f475Smrg    Bool is_utf8 = xtermEnvUTF8();
5155d522f475Smrg
5156d522f475Smrg    TRACE(("VTInitialize_locale\n"));
5157d522f475Smrg    TRACE(("... request screen.utf8_mode = %d\n", request->screen.utf8_mode));
5158d522f475Smrg
5159d522f475Smrg    if (request->screen.utf8_mode < 0)
5160d522f475Smrg	request->screen.utf8_mode = uFalse;
5161d522f475Smrg
5162d522f475Smrg    if (request->screen.utf8_mode > 3)
5163d522f475Smrg	request->screen.utf8_mode = uDefault;
5164d522f475Smrg
5165d522f475Smrg    request->screen.latin9_mode = 0;
5166d522f475Smrg    request->screen.unicode_font = 0;
5167d522f475Smrg#if OPT_LUIT_PROG
5168d522f475Smrg    request->misc.callfilter = 0;
5169d522f475Smrg    request->misc.use_encoding = 0;
5170d522f475Smrg
5171d522f475Smrg    TRACE(("... setup for luit:\n"));
5172d522f475Smrg    TRACE(("... request misc.locale_str = \"%s\"\n", request->misc.locale_str));
5173d522f475Smrg
5174d522f475Smrg    if (request->screen.utf8_mode == uFalse) {
5175d522f475Smrg	TRACE(("... command-line +u8 overrides\n"));
5176d522f475Smrg    } else
5177d522f475Smrg#if OPT_MINI_LUIT
5178d522f475Smrg    if (x_strcasecmp(request->misc.locale_str, "CHECKFONT") == 0) {
5179d522f475Smrg	int fl = (request->misc.default_font.f_n
5180d522f475Smrg		  ? strlen(request->misc.default_font.f_n)
5181d522f475Smrg		  : 0);
5182d522f475Smrg	if (fl > 11
5183d522f475Smrg	    && x_strcasecmp(request->misc.default_font.f_n + fl - 11,
5184d522f475Smrg			    "-ISO10646-1") == 0) {
5185d522f475Smrg	    request->screen.unicode_font = 1;
5186d522f475Smrg	    /* unicode font, use True */
5187d522f475Smrg#ifdef HAVE_LANGINFO_CODESET
5188d522f475Smrg	    if (!strcmp(xtermEnvEncoding(), "ANSI_X3.4-1968")
5189d522f475Smrg		|| !strcmp(xtermEnvEncoding(), "ISO-8859-1")) {
5190d522f475Smrg		if (request->screen.utf8_mode == uDefault)
5191d522f475Smrg		    request->screen.utf8_mode = uFalse;
5192d522f475Smrg	    } else if (!strcmp(xtermEnvEncoding(), "ISO-8859-15")) {
5193d522f475Smrg		if (request->screen.utf8_mode == uDefault)
5194d522f475Smrg		    request->screen.utf8_mode = uFalse;
5195d522f475Smrg		request->screen.latin9_mode = 1;
5196d522f475Smrg	    } else {
5197d522f475Smrg		request->misc.callfilter = is_utf8 ? 0 : 1;
5198d522f475Smrg		request->screen.utf8_mode = uAlways;
5199d522f475Smrg	    }
5200d522f475Smrg#else
5201d522f475Smrg	    request->misc.callfilter = is_utf8 ? 0 : 1;
5202d522f475Smrg	    request->screen.utf8_mode = uAlways;
5203d522f475Smrg#endif
5204d522f475Smrg	} else {
5205d522f475Smrg	    /* other encoding, use False */
5206d522f475Smrg	    if (request->screen.utf8_mode == uDefault) {
5207d522f475Smrg		request->screen.utf8_mode = is_utf8 ? uAlways : uFalse;
5208d522f475Smrg	    }
5209d522f475Smrg	}
5210d522f475Smrg    } else
5211d522f475Smrg#endif /* OPT_MINI_LUIT */
5212d522f475Smrg	if (x_strcasecmp(request->misc.locale_str, "TRUE") == 0 ||
5213d522f475Smrg	    x_strcasecmp(request->misc.locale_str, "ON") == 0 ||
5214d522f475Smrg	    x_strcasecmp(request->misc.locale_str, "YES") == 0 ||
5215d522f475Smrg	    x_strcasecmp(request->misc.locale_str, "AUTO") == 0 ||
5216d522f475Smrg	    strcmp(request->misc.locale_str, "1") == 0) {
5217d522f475Smrg	/* when true ... fully obeying LC_CTYPE locale */
5218d522f475Smrg	request->misc.callfilter = is_utf8 ? 0 : 1;
5219d522f475Smrg	request->screen.utf8_mode = uAlways;
5220d522f475Smrg    } else if (x_strcasecmp(request->misc.locale_str, "FALSE") == 0 ||
5221d522f475Smrg	       x_strcasecmp(request->misc.locale_str, "OFF") == 0 ||
5222d522f475Smrg	       x_strcasecmp(request->misc.locale_str, "NO") == 0 ||
5223d522f475Smrg	       strcmp(request->misc.locale_str, "0") == 0) {
5224d522f475Smrg	/* when false ... original value of utf8_mode is effective */
5225d522f475Smrg	if (request->screen.utf8_mode == uDefault) {
5226d522f475Smrg	    request->screen.utf8_mode = is_utf8 ? uAlways : uFalse;
5227d522f475Smrg	}
5228d522f475Smrg    } else if (x_strcasecmp(request->misc.locale_str, "MEDIUM") == 0 ||
5229d522f475Smrg	       x_strcasecmp(request->misc.locale_str, "SEMIAUTO") == 0) {
5230d522f475Smrg	/* when medium ... obeying locale only for UTF-8 and Asian */
5231d522f475Smrg	if (is_utf8) {
5232d522f475Smrg	    request->screen.utf8_mode = uAlways;
5233d522f475Smrg	} else if (
5234d522f475Smrg#ifdef MB_CUR_MAX
5235d522f475Smrg		      MB_CUR_MAX > 1 ||
5236d522f475Smrg#else
5237d522f475Smrg		      !strncmp(xtermEnvLocale(), "ja", 2) ||
5238d522f475Smrg		      !strncmp(xtermEnvLocale(), "ko", 2) ||
5239d522f475Smrg		      !strncmp(xtermEnvLocale(), "zh", 2) ||
5240d522f475Smrg#endif
5241d522f475Smrg		      !strncmp(xtermEnvLocale(), "th", 2) ||
5242d522f475Smrg		      !strncmp(xtermEnvLocale(), "vi", 2)) {
5243d522f475Smrg	    request->misc.callfilter = 1;
5244d522f475Smrg	    request->screen.utf8_mode = uAlways;
5245d522f475Smrg	} else {
5246d522f475Smrg	    request->screen.utf8_mode = uFalse;
5247d522f475Smrg	}
5248d522f475Smrg    } else if (x_strcasecmp(request->misc.locale_str, "UTF-8") == 0 ||
5249d522f475Smrg	       x_strcasecmp(request->misc.locale_str, "UTF8") == 0) {
5250d522f475Smrg	/* when UTF-8 ... UTF-8 mode */
5251d522f475Smrg	request->screen.utf8_mode = uAlways;
5252d522f475Smrg    } else {
5253d522f475Smrg	/* other words are regarded as encoding name passed to luit */
5254d522f475Smrg	request->misc.callfilter = 1;
5255d522f475Smrg	request->screen.utf8_mode = uAlways;
5256d522f475Smrg	request->misc.use_encoding = 1;
5257d522f475Smrg    }
5258d522f475Smrg    TRACE(("... updated misc.callfilter = %s\n", BtoS(request->misc.callfilter)));
5259d522f475Smrg    TRACE(("... updated misc.use_encoding = %s\n", BtoS(request->misc.use_encoding)));
5260d522f475Smrg#else
5261d522f475Smrg    if (request->screen.utf8_mode == uDefault) {
5262d522f475Smrg	request->screen.utf8_mode = is_utf8 ? uAlways : uFalse;
5263d522f475Smrg    }
5264d522f475Smrg#endif /* OPT_LUIT_PROG */
5265d522f475Smrg
5266d522f475Smrg    request->screen.utf8_inparse = (request->screen.utf8_mode != uFalse);
5267d522f475Smrg
5268d522f475Smrg    TRACE(("... updated screen.utf8_mode = %d\n", request->screen.utf8_mode));
5269d522f475Smrg    TRACE(("...VTInitialize_locale done\n"));
5270d522f475Smrg}
5271d522f475Smrg#endif
5272d522f475Smrg
5273d522f475Smrgstatic void
5274d522f475SmrgParseOnClicks(XtermWidget wnew, XtermWidget wreq, Cardinal item)
5275d522f475Smrg{
5276d522f475Smrg    /* *INDENT-OFF* */
5277d522f475Smrg    static struct {
5278d522f475Smrg	const String	name;
5279d522f475Smrg	SelectUnit	code;
5280d522f475Smrg    } table[] = {
5281d522f475Smrg    	{ "char",	Select_CHAR },
5282d522f475Smrg    	{ "word",	Select_WORD },
5283d522f475Smrg    	{ "line",	Select_LINE },
5284d522f475Smrg    	{ "group",	Select_GROUP },
5285d522f475Smrg    	{ "page",	Select_PAGE },
5286d522f475Smrg    	{ "all",	Select_ALL },
5287d522f475Smrg#if OPT_SELECT_REGEX
5288d522f475Smrg    	{ "regex",	Select_REGEX },
5289d522f475Smrg#endif
5290d522f475Smrg    };
5291d522f475Smrg    /* *INDENT-ON* */
5292d522f475Smrg
5293d522f475Smrg    String res = wreq->screen.onClick[item];
5294d522f475Smrg    String next = x_skip_nonblanks(res);
5295d522f475Smrg    Cardinal n;
5296d522f475Smrg
5297d522f475Smrg    wnew->screen.selectMap[item] = NSELECTUNITS;
5298d522f475Smrg    for (n = 0; n < XtNumber(table); ++n) {
5299d522f475Smrg	if (!x_strncasecmp(table[n].name, res, (unsigned) (next - res))) {
5300d522f475Smrg	    wnew->screen.selectMap[item] = table[n].code;
5301d522f475Smrg#if OPT_SELECT_REGEX
5302d522f475Smrg	    if (table[n].code == Select_REGEX) {
5303d522f475Smrg		wnew->screen.selectExpr[item] = x_strtrim(next);
5304d522f475Smrg		TRACE(("Parsed regex \"%s\"\n", wnew->screen.selectExpr[item]));
5305d522f475Smrg	    }
5306d522f475Smrg#endif
5307d522f475Smrg	    break;
5308d522f475Smrg	}
5309d522f475Smrg    }
5310d522f475Smrg}
5311d522f475Smrg
5312d522f475Smrg/* ARGSUSED */
5313d522f475Smrgstatic void
5314d522f475SmrgVTInitialize(Widget wrequest,
5315d522f475Smrg	     Widget new_arg,
5316d522f475Smrg	     ArgList args GCC_UNUSED,
5317d522f475Smrg	     Cardinal *num_args GCC_UNUSED)
5318d522f475Smrg{
5319d522f475Smrg#define Kolor(name) wnew->screen.name.resource
5320d522f475Smrg#define TxtFg(name) !x_strcasecmp(Kolor(Tcolors[TEXT_FG]), Kolor(name))
5321d522f475Smrg#define TxtBg(name) !x_strcasecmp(Kolor(Tcolors[TEXT_BG]), Kolor(name))
5322d522f475Smrg#define DftFg(name) isDefaultForeground(Kolor(name))
5323d522f475Smrg#define DftBg(name) isDefaultBackground(Kolor(name))
5324d522f475Smrg
5325d522f475Smrg    XtermWidget request = (XtermWidget) wrequest;
5326d522f475Smrg    XtermWidget wnew = (XtermWidget) new_arg;
5327d522f475Smrg    Widget my_parent = SHELL_OF(wnew);
5328d522f475Smrg    int i;
5329d522f475Smrg    char *s;
5330d522f475Smrg
5331d522f475Smrg#if OPT_ISO_COLORS
5332d522f475Smrg    Bool color_ok;
5333d522f475Smrg#endif
5334d522f475Smrg
5335d522f475Smrg#if OPT_COLOR_RES2 && (MAXCOLORS > MIN_ANSI_COLORS)
5336d522f475Smrg    static XtResource fake_resources[] =
5337d522f475Smrg    {
5338d522f475Smrg#if OPT_256_COLORS
5339d522f475Smrg# include <256colres.h>
5340d522f475Smrg#elif OPT_88_COLORS
5341d522f475Smrg# include <88colres.h>
5342d522f475Smrg#endif
5343d522f475Smrg    };
5344d522f475Smrg#endif /* OPT_COLOR_RES2 */
5345d522f475Smrg
5346d522f475Smrg    TRACE(("VTInitialize\n"));
5347d522f475Smrg
5348d522f475Smrg    /* Zero out the entire "screen" component of "wnew" widget, then do
5349d522f475Smrg     * field-by-field assignment of "screen" fields that are named in the
5350d522f475Smrg     * resource list.
5351d522f475Smrg     */
5352d522f475Smrg    bzero((char *) &wnew->screen, sizeof(wnew->screen));
5353d522f475Smrg
5354d522f475Smrg    /* DESCO Sys#67660
5355d522f475Smrg     * Zero out the entire "keyboard" component of "wnew" widget.
5356d522f475Smrg     */
5357d522f475Smrg    bzero((char *) &wnew->keyboard, sizeof(wnew->keyboard));
5358d522f475Smrg
5359d522f475Smrg    /* dummy values so that we don't try to Realize the parent shell with height
5360d522f475Smrg     * or width of 0, which is illegal in X.  The real size is computed in the
5361d522f475Smrg     * xtermWidget's Realize proc, but the shell's Realize proc is called first,
5362d522f475Smrg     * and must see a valid size.
5363d522f475Smrg     */
5364d522f475Smrg    wnew->core.height = wnew->core.width = 1;
5365d522f475Smrg
5366d522f475Smrg    /*
5367d522f475Smrg     * The definition of -rv now is that it changes the definition of
5368d522f475Smrg     * XtDefaultForeground and XtDefaultBackground.  So, we no longer
5369d522f475Smrg     * need to do anything special.
5370d522f475Smrg     */
5371d522f475Smrg    wnew->screen.display = wnew->core.screen->display;
5372d522f475Smrg
5373d522f475Smrg    /*
5374d522f475Smrg     * We use the default foreground/background colors to compare/check if a
5375d522f475Smrg     * color-resource has been set.
5376d522f475Smrg     */
5377d522f475Smrg#define MyBlackPixel(dpy) BlackPixel(dpy,DefaultScreen(dpy))
5378d522f475Smrg#define MyWhitePixel(dpy) WhitePixel(dpy,DefaultScreen(dpy))
5379d522f475Smrg
5380d522f475Smrg    if (request->misc.re_verse) {
5381d522f475Smrg	wnew->dft_foreground = MyWhitePixel(wnew->screen.display);
5382d522f475Smrg	wnew->dft_background = MyBlackPixel(wnew->screen.display);
5383d522f475Smrg    } else {
5384d522f475Smrg	wnew->dft_foreground = MyBlackPixel(wnew->screen.display);
5385d522f475Smrg	wnew->dft_background = MyWhitePixel(wnew->screen.display);
5386d522f475Smrg    }
5387d522f475Smrg    init_Tres(TEXT_FG);
5388d522f475Smrg    init_Tres(TEXT_BG);
5389d522f475Smrg
5390d522f475Smrg    TRACE(("Color resource initialization:\n"));
5391d522f475Smrg    TRACE(("   Default foreground %#lx\n", wnew->dft_foreground));
5392d522f475Smrg    TRACE(("   Default background %#lx\n", wnew->dft_background));
5393d522f475Smrg    TRACE(("   Screen foreground  %#lx\n", T_COLOR(&(wnew->screen), TEXT_FG)));
5394d522f475Smrg    TRACE(("   Screen background  %#lx\n", T_COLOR(&(wnew->screen), TEXT_BG)));
5395d522f475Smrg
5396d522f475Smrg    wnew->screen.mouse_button = -1;
5397d522f475Smrg    wnew->screen.mouse_row = -1;
5398d522f475Smrg    wnew->screen.mouse_col = -1;
5399d522f475Smrg
5400d522f475Smrg#if OPT_BOX_CHARS
5401d522f475Smrg    init_Bres(screen.force_box_chars);
5402d522f475Smrg    init_Bres(screen.force_all_chars);
5403d522f475Smrg#endif
5404d522f475Smrg    init_Bres(screen.free_bold_box);
5405d522f475Smrg
5406d522f475Smrg    init_Bres(screen.c132);
5407d522f475Smrg    init_Bres(screen.curses);
5408d522f475Smrg    init_Bres(screen.hp_ll_bc);
5409d522f475Smrg#if OPT_XMC_GLITCH
5410d522f475Smrg    init_Ires(screen.xmc_glitch);
5411d522f475Smrg    init_Ires(screen.xmc_attributes);
5412d522f475Smrg    init_Bres(screen.xmc_inline);
5413d522f475Smrg    init_Bres(screen.move_sgr_ok);
5414d522f475Smrg#endif
5415d522f475Smrg#if OPT_BLINK_CURS
5416d522f475Smrg    init_Bres(screen.cursor_blink);
5417d522f475Smrg    init_Ires(screen.blink_on);
5418d522f475Smrg    init_Ires(screen.blink_off);
5419d522f475Smrg    wnew->screen.cursor_blink_res = wnew->screen.cursor_blink;
5420d522f475Smrg#endif
5421d522f475Smrg#if OPT_BLINK_TEXT
5422d522f475Smrg    init_Ires(screen.blink_as_bold);
5423d522f475Smrg#endif
5424d522f475Smrg    init_Ires(screen.border);
5425d522f475Smrg    init_Bres(screen.jumpscroll);
5426d522f475Smrg    init_Bres(screen.old_fkeys);
5427d522f475Smrg    init_Bres(screen.delete_is_del);
5428d522f475Smrg    wnew->keyboard.type = wnew->screen.old_fkeys
5429d522f475Smrg	? keyboardIsLegacy
5430d522f475Smrg	: keyboardIsDefault;
5431d522f475Smrg#ifdef ALLOWLOGGING
5432d522f475Smrg    init_Sres(screen.logfile);
5433d522f475Smrg#endif
5434d522f475Smrg    init_Bres(screen.bellIsUrgent);
5435d522f475Smrg    init_Bres(screen.bellOnReset);
5436d522f475Smrg    init_Bres(screen.marginbell);
5437d522f475Smrg    init_Bres(screen.multiscroll);
5438d522f475Smrg    init_Ires(screen.nmarginbell);
5439d522f475Smrg    init_Ires(screen.savelines);
5440d522f475Smrg    init_Ires(screen.scrollBarBorder);
5441d522f475Smrg    init_Ires(screen.scrolllines);
5442d522f475Smrg    init_Bres(screen.scrollttyoutput);
5443d522f475Smrg    init_Bres(screen.scrollkey);
5444d522f475Smrg
5445d522f475Smrg    init_Sres(screen.term_id);
5446d522f475Smrg    for (s = request->screen.term_id; *s; s++) {
5447d522f475Smrg	if (!isalpha(CharOf(*s)))
5448d522f475Smrg	    break;
5449d522f475Smrg    }
5450d522f475Smrg    wnew->screen.terminal_id = atoi(s);
5451d522f475Smrg    if (wnew->screen.terminal_id < MIN_DECID)
5452d522f475Smrg	wnew->screen.terminal_id = MIN_DECID;
5453d522f475Smrg    if (wnew->screen.terminal_id > MAX_DECID)
5454d522f475Smrg	wnew->screen.terminal_id = MAX_DECID;
5455d522f475Smrg    TRACE(("term_id '%s' -> terminal_id %d\n",
5456d522f475Smrg	   wnew->screen.term_id,
5457d522f475Smrg	   wnew->screen.terminal_id));
5458d522f475Smrg
5459d522f475Smrg    wnew->screen.vtXX_level = (wnew->screen.terminal_id / 100);
5460d522f475Smrg    init_Bres(screen.visualbell);
5461d522f475Smrg    init_Ires(screen.visualBellDelay);
5462d522f475Smrg    init_Bres(screen.poponbell);
5463d522f475Smrg    init_Ires(misc.limit_resize);
5464d522f475Smrg#if OPT_NUM_LOCK
5465d522f475Smrg    init_Bres(misc.real_NumLock);
5466d522f475Smrg    init_Bres(misc.alwaysUseMods);
5467d522f475Smrg    wnew->misc.num_lock = 0;
5468d522f475Smrg    wnew->misc.alt_mods = 0;
5469d522f475Smrg    wnew->misc.meta_mods = 0;
5470d522f475Smrg    wnew->misc.other_mods = 0;
5471d522f475Smrg#endif
5472d522f475Smrg#if OPT_SHIFT_FONTS
5473d522f475Smrg    init_Bres(misc.shift_fonts);
5474d522f475Smrg#endif
5475d522f475Smrg#if OPT_SUNPC_KBD
5476d522f475Smrg    init_Ires(misc.ctrl_fkeys);
5477d522f475Smrg#endif
5478d522f475Smrg#if OPT_TEK4014
5479d522f475Smrg    TEK4014_SHOWN(wnew) = False;	/* not a resource... */
5480d522f475Smrg    init_Bres(misc.tekInhibit);
5481d522f475Smrg    init_Bres(misc.tekSmall);
5482d522f475Smrg    init_Bres(misc.TekEmu);
5483d522f475Smrg#endif
5484d522f475Smrg#if OPT_TCAP_QUERY
5485d522f475Smrg    wnew->screen.tc_query_code = -1;
5486d522f475Smrg#endif
5487d522f475Smrg    wnew->misc.re_verse0 = request->misc.re_verse;
5488d522f475Smrg    init_Bres(misc.re_verse);
5489d522f475Smrg    init_Ires(screen.multiClickTime);
5490d522f475Smrg    init_Ires(screen.bellSuppressTime);
5491d522f475Smrg    init_Sres(screen.charClass);
5492d522f475Smrg
5493d522f475Smrg    init_Bres(screen.always_highlight);
5494d522f475Smrg    init_Bres(screen.brokenSelections);
5495d522f475Smrg    init_Bres(screen.cutNewline);
5496d522f475Smrg    init_Bres(screen.cutToBeginningOfLine);
5497d522f475Smrg    init_Bres(screen.highlight_selection);
5498d522f475Smrg    init_Bres(screen.i18nSelections);
5499d522f475Smrg    init_Bres(screen.keepSelection);
5500d522f475Smrg    init_Bres(screen.selectToClipboard);
5501d522f475Smrg    init_Bres(screen.trim_selection);
5502d522f475Smrg
5503d522f475Smrg    wnew->screen.pointer_cursor = request->screen.pointer_cursor;
5504d522f475Smrg    init_Ires(screen.pointer_mode);
5505d522f475Smrg
5506d522f475Smrg    init_Sres(screen.answer_back);
5507d522f475Smrg
5508d522f475Smrg    init_Sres(screen.printer_command);
5509d522f475Smrg    init_Bres(screen.printer_autoclose);
5510d522f475Smrg    init_Bres(screen.printer_extent);
5511d522f475Smrg    init_Bres(screen.printer_formfeed);
5512d522f475Smrg    init_Ires(screen.printer_controlmode);
5513d522f475Smrg#if OPT_PRINT_COLORS
5514d522f475Smrg    init_Ires(screen.print_attributes);
5515d522f475Smrg#endif
5516d522f475Smrg
5517d522f475Smrg    init_Sres(screen.keyboard_dialect);
5518d522f475Smrg
5519d522f475Smrg    init_Bres(screen.input_eight_bits);
5520d522f475Smrg    init_Bres(screen.output_eight_bits);
5521d522f475Smrg    init_Bres(screen.control_eight_bits);
5522d522f475Smrg    init_Bres(screen.backarrow_key);
5523d522f475Smrg    init_Bres(screen.alt_is_not_meta);
5524d522f475Smrg    init_Bres(screen.alt_sends_esc);
5525d522f475Smrg    init_Bres(screen.meta_sends_esc);
5526d522f475Smrg
5527d522f475Smrg    init_Bres(screen.allowSendEvent0);
5528d522f475Smrg    init_Bres(screen.allowTitleOp0);
5529d522f475Smrg    init_Bres(screen.allowWindowOp0);
5530d522f475Smrg
5531d522f475Smrg    /* make a copy so that editres cannot change the resource after startup */
5532d522f475Smrg    wnew->screen.allowSendEvents = wnew->screen.allowSendEvent0;
5533d522f475Smrg    wnew->screen.allowTitleOps = wnew->screen.allowTitleOp0;
5534d522f475Smrg    wnew->screen.allowWindowOps = wnew->screen.allowWindowOp0;
5535d522f475Smrg
5536d522f475Smrg    init_Bres(screen.quiet_grab);
5537d522f475Smrg
5538d522f475Smrg#ifndef NO_ACTIVE_ICON
5539d522f475Smrg    wnew->screen.fnt_icon.fs = request->screen.fnt_icon.fs;
5540d522f475Smrg    init_Bres(misc.active_icon);
5541d522f475Smrg    init_Ires(misc.icon_border_width);
5542d522f475Smrg    wnew->misc.icon_border_pixel = request->misc.icon_border_pixel;
5543d522f475Smrg#endif /* NO_ACTIVE_ICON */
5544d522f475Smrg    init_Bres(misc.titeInhibit);
5545d522f475Smrg    init_Bres(misc.tiXtraScroll);
5546d522f475Smrg    init_Bres(misc.dynamicColors);
5547d522f475Smrg    for (i = fontMenu_font1; i <= fontMenu_lastBuiltin; i++) {
5548d522f475Smrg	init_Sres(screen.MenuFontName(i));
5549d522f475Smrg    }
5550d522f475Smrg    wnew->screen.MenuFontName(fontMenu_default) = wnew->misc.default_font.f_n;
5551d522f475Smrg    wnew->screen.MenuFontName(fontMenu_fontescape) = NULL;
5552d522f475Smrg    wnew->screen.MenuFontName(fontMenu_fontsel) = NULL;
5553d522f475Smrg
5554d522f475Smrg    wnew->screen.menu_font_number = fontMenu_default;
5555d522f475Smrg    init_Sres(screen.initial_font);
5556d522f475Smrg    if (wnew->screen.initial_font != 0) {
5557d522f475Smrg	int result = xtermGetFont(wnew->screen.initial_font);
5558d522f475Smrg	if (result >= 0)
5559d522f475Smrg	    wnew->screen.menu_font_number = result;
5560d522f475Smrg    }
5561d522f475Smrg#if OPT_BROKEN_OSC
5562d522f475Smrg    init_Bres(screen.brokenLinuxOSC);
5563d522f475Smrg#endif
5564d522f475Smrg
5565d522f475Smrg#if OPT_BROKEN_ST
5566d522f475Smrg    init_Bres(screen.brokenStringTerm);
5567d522f475Smrg#endif
5568d522f475Smrg
5569d522f475Smrg#if OPT_C1_PRINT
5570d522f475Smrg    init_Bres(screen.c1_printable);
5571d522f475Smrg#endif
5572d522f475Smrg
5573d522f475Smrg#if OPT_CLIP_BOLD
5574d522f475Smrg    init_Bres(screen.use_clipping);
5575d522f475Smrg#endif
5576d522f475Smrg
5577d522f475Smrg#if OPT_DEC_CHRSET
5578d522f475Smrg    init_Bres(screen.font_doublesize);
5579d522f475Smrg    init_Ires(screen.cache_doublesize);
5580d522f475Smrg    if (wnew->screen.cache_doublesize > NUM_CHRSET)
5581d522f475Smrg	wnew->screen.cache_doublesize = NUM_CHRSET;
5582d522f475Smrg    if (wnew->screen.cache_doublesize == 0)
5583d522f475Smrg	wnew->screen.font_doublesize = False;
5584d522f475Smrg    TRACE(("Doublesize%s enabled, up to %d fonts\n",
5585d522f475Smrg	   wnew->screen.font_doublesize ? "" : " not",
5586d522f475Smrg	   wnew->screen.cache_doublesize));
5587d522f475Smrg#endif
5588d522f475Smrg
5589d522f475Smrg#if OPT_WIDE_CHARS
5590d522f475Smrg    wnew->num_ptrs = (OFF_CHARS + 1);	/* minimum needed for cell */
5591d522f475Smrg#endif
5592d522f475Smrg#if OPT_ISO_COLORS
5593d522f475Smrg    init_Ires(screen.veryBoldColors);
5594d522f475Smrg    init_Bres(screen.boldColors);
5595d522f475Smrg    init_Bres(screen.colorAttrMode);
5596d522f475Smrg    init_Bres(screen.colorBDMode);
5597d522f475Smrg    init_Bres(screen.colorBLMode);
5598d522f475Smrg    init_Bres(screen.colorMode);
5599d522f475Smrg    init_Bres(screen.colorULMode);
5600d522f475Smrg    init_Bres(screen.italicULMode);
5601d522f475Smrg    init_Bres(screen.colorRVMode);
5602d522f475Smrg
5603d522f475Smrg    for (i = 0, color_ok = False; i < MAXCOLORS; i++) {
5604d522f475Smrg
5605d522f475Smrg#if OPT_COLOR_RES2 && (MAXCOLORS > MIN_ANSI_COLORS)
5606d522f475Smrg	/*
5607d522f475Smrg	 * Xt has a hardcoded limit on the maximum number of resources that can
5608d522f475Smrg	 * be used in a widget.  If we configure both luit (which implies
5609d522f475Smrg	 * wide-characters) and 256-colors, it goes over that limit.  Most
5610d522f475Smrg	 * people would not need a resource-file with 256-colors; the default
5611d522f475Smrg	 * values in our table are sufficient.  In that case, fake the resource
5612d522f475Smrg	 * setting by copying the default value from the table.  The #define's
5613d522f475Smrg	 * can be overridden to make these true resources.
5614d522f475Smrg	 */
5615d522f475Smrg	if (i >= MIN_ANSI_COLORS && i < NUM_ANSI_COLORS) {
5616d522f475Smrg	    wnew->screen.Acolors[i].resource
5617d522f475Smrg		= ((char *) fake_resources[i - MIN_ANSI_COLORS].default_addr);
5618d522f475Smrg	    if (wnew->screen.Acolors[i].resource == 0)
5619d522f475Smrg		wnew->screen.Acolors[i].resource = XtDefaultForeground;
5620d522f475Smrg	} else
5621d522f475Smrg#endif /* OPT_COLOR_RES2 */
5622d522f475Smrg	    wnew->screen.Acolors[i] = request->screen.Acolors[i];
5623d522f475Smrg
5624d522f475Smrg#if OPT_COLOR_RES
5625d522f475Smrg	TRACE(("Acolors[%d] = %s\n", i, wnew->screen.Acolors[i].resource));
5626d522f475Smrg	wnew->screen.Acolors[i].mode = False;
5627d522f475Smrg	if (DftFg(Acolors[i])) {
5628d522f475Smrg	    wnew->screen.Acolors[i].value = T_COLOR(&(wnew->screen), TEXT_FG);
5629d522f475Smrg	    wnew->screen.Acolors[i].mode = True;
5630d522f475Smrg	} else if (DftBg(Acolors[i])) {
5631d522f475Smrg	    wnew->screen.Acolors[i].value = T_COLOR(&(wnew->screen), TEXT_BG);
5632d522f475Smrg	    wnew->screen.Acolors[i].mode = True;
5633d522f475Smrg	} else {
5634d522f475Smrg	    color_ok = True;
5635d522f475Smrg	}
5636d522f475Smrg#else
5637d522f475Smrg	TRACE(("Acolors[%d] = %#lx\n", i, request->screen.Acolors[i]));
5638d522f475Smrg	if (wnew->screen.Acolors[i] != wnew->dft_foreground &&
5639d522f475Smrg	    wnew->screen.Acolors[i] != T_COLOR(&(wnew->screen), TEXT_FG) &&
5640d522f475Smrg	    wnew->screen.Acolors[i] != T_COLOR(&(wnew->screen), TEXT_BG))
5641d522f475Smrg	    color_ok = True;
5642d522f475Smrg#endif
5643d522f475Smrg    }
5644d522f475Smrg
5645d522f475Smrg    /*
5646d522f475Smrg     * Check if we're trying to use color in a monochrome screen.  Disable
5647d522f475Smrg     * color in that case, since that would make ANSI colors unusable.  A 4-bit
5648d522f475Smrg     * or 8-bit display is usable, so we do not have to check for anything more
5649d522f475Smrg     * specific.
5650d522f475Smrg     */
5651d522f475Smrg    if (color_ok) {
5652d522f475Smrg	Display *display = wnew->screen.display;
5653d522f475Smrg	XVisualInfo myTemplate, *visInfoPtr;
5654d522f475Smrg	int numFound;
5655d522f475Smrg
5656d522f475Smrg	myTemplate.visualid = XVisualIDFromVisual(DefaultVisual(display,
5657d522f475Smrg								XDefaultScreen(display)));
5658d522f475Smrg	visInfoPtr = XGetVisualInfo(display, (long) VisualIDMask,
5659d522f475Smrg				    &myTemplate, &numFound);
5660d522f475Smrg	if (visInfoPtr == 0
5661d522f475Smrg	    || numFound == 0
5662d522f475Smrg	    || visInfoPtr->depth <= 1) {
5663d522f475Smrg	    TRACE(("disabling color since screen is monochrome\n"));
5664d522f475Smrg	    color_ok = False;
5665d522f475Smrg	} else {
5666d522f475Smrg	    XFree(visInfoPtr);
5667d522f475Smrg	}
5668d522f475Smrg    }
5669d522f475Smrg
5670d522f475Smrg    /* If none of the colors are anything other than the foreground or
5671d522f475Smrg     * background, we'll assume this isn't color, no matter what the colorMode
5672d522f475Smrg     * resource says.  (There doesn't seem to be any good way to determine if
5673d522f475Smrg     * the resource lookup failed versus the user having misconfigured this).
5674d522f475Smrg     */
5675d522f475Smrg    if (!color_ok) {
5676d522f475Smrg	wnew->screen.colorMode = False;
5677d522f475Smrg	TRACE(("All colors are foreground or background: disable colorMode\n"));
5678d522f475Smrg    }
5679d522f475Smrg    wnew->sgr_foreground = -1;
5680d522f475Smrg    wnew->sgr_background = -1;
5681d522f475Smrg    wnew->sgr_extended = False;
5682d522f475Smrg#endif /* OPT_ISO_COLORS */
5683d522f475Smrg
5684d522f475Smrg    /*
5685d522f475Smrg     * Decode the resources that control the behavior on multiple mouse clicks.
5686d522f475Smrg     * A single click is always bound to normal character selection, but the
5687d522f475Smrg     * other flavors can be changed.
5688d522f475Smrg     */
5689d522f475Smrg    for (i = 0; i < NSELECTUNITS; ++i) {
5690d522f475Smrg	int ck = (i + 1);
5691d522f475Smrg	wnew->screen.maxClicks = ck;
5692d522f475Smrg	if (i == Select_CHAR)
5693d522f475Smrg	    wnew->screen.selectMap[i] = Select_CHAR;
5694d522f475Smrg	else if (request->screen.onClick[i] != 0)
5695d522f475Smrg	    ParseOnClicks(wnew, request, (unsigned) i);
5696d522f475Smrg	else if (i <= Select_LINE)
5697d522f475Smrg	    wnew->screen.selectMap[i] = (SelectUnit) i;
5698d522f475Smrg	else
5699d522f475Smrg	    break;
5700d522f475Smrg	TRACE(("on%dClicks %s=%d\n", ck,
5701d522f475Smrg	       NonNull(request->screen.onClick[i]),
5702d522f475Smrg	       wnew->screen.selectMap[i]));
5703d522f475Smrg	if (wnew->screen.selectMap[i] == NSELECTUNITS)
5704d522f475Smrg	    break;
5705d522f475Smrg    }
5706d522f475Smrg    TRACE(("maxClicks %d\n", wnew->screen.maxClicks));
5707d522f475Smrg
5708d522f475Smrg    init_Tres(MOUSE_FG);
5709d522f475Smrg    init_Tres(MOUSE_BG);
5710d522f475Smrg    init_Tres(TEXT_CURSOR);
5711d522f475Smrg#if OPT_HIGHLIGHT_COLOR
5712d522f475Smrg    init_Tres(HIGHLIGHT_BG);
5713d522f475Smrg    init_Tres(HIGHLIGHT_FG);
5714d522f475Smrg    init_Bres(screen.hilite_reverse);
5715d522f475Smrg    init_Bres(screen.hilite_color);
5716d522f475Smrg    if (wnew->screen.hilite_color == Maybe) {
5717d522f475Smrg	wnew->screen.hilite_color = False;
5718d522f475Smrg#if OPT_COLOR_RES
5719d522f475Smrg	/*
5720d522f475Smrg	 * If the highlight text/background are both set, and if they are
5721d522f475Smrg	 * not equal to either the text/background or background/text, then
5722d522f475Smrg	 * set the highlightColorMode automatically.
5723d522f475Smrg	 */
5724d522f475Smrg	if (!DftFg(Tcolors[HIGHLIGHT_BG])
5725d522f475Smrg	    && !DftBg(Tcolors[HIGHLIGHT_FG])
5726d522f475Smrg	    && !TxtFg(Tcolors[HIGHLIGHT_BG])
5727d522f475Smrg	    && !TxtBg(Tcolors[HIGHLIGHT_FG])
5728d522f475Smrg	    && !TxtBg(Tcolors[HIGHLIGHT_BG])
5729d522f475Smrg	    && !TxtFg(Tcolors[HIGHLIGHT_FG])) {
5730d522f475Smrg	    TRACE(("...setting hilite_color automatically\n"));
5731d522f475Smrg	    wnew->screen.hilite_color = True;
5732d522f475Smrg	}
5733d522f475Smrg#endif
5734d522f475Smrg    }
5735d522f475Smrg#endif
5736d522f475Smrg
5737d522f475Smrg#if OPT_TEK4014
5738d522f475Smrg    /*
5739d522f475Smrg     * The Tek4014 window has no separate resources for foreground, background
5740d522f475Smrg     * and cursor color.  Since xterm always creates the vt100 widget first, we
5741d522f475Smrg     * can set the Tektronix colors here.  That lets us use escape sequences to
5742d522f475Smrg     * set its dynamic colors and get consistent behavior whether or not the
5743d522f475Smrg     * window is displayed.
5744d522f475Smrg     */
5745d522f475Smrg    T_COLOR(&(wnew->screen), TEK_BG) = T_COLOR(&(wnew->screen), TEXT_BG);
5746d522f475Smrg    T_COLOR(&(wnew->screen), TEK_FG) = T_COLOR(&(wnew->screen), TEXT_FG);
5747d522f475Smrg    T_COLOR(&(wnew->screen), TEK_CURSOR) = T_COLOR(&(wnew->screen), TEXT_CURSOR);
5748d522f475Smrg#endif
5749d522f475Smrg
5750d522f475Smrg#if OPT_WIDE_CHARS
5751d522f475Smrg    VTInitialize_locale(request);
5752d522f475Smrg    init_Bres(screen.utf8_latin1);
5753d522f475Smrg    init_Bres(screen.utf8_title);
5754d522f475Smrg
5755d522f475Smrg#if OPT_LUIT_PROG
5756d522f475Smrg    init_Bres(misc.callfilter);
5757d522f475Smrg    init_Bres(misc.use_encoding);
5758d522f475Smrg    init_Sres(misc.locale_str);
5759d522f475Smrg    init_Sres(misc.localefilter);
5760d522f475Smrg#endif
5761d522f475Smrg
5762d522f475Smrg#if OPT_RENDERFONT
5763d522f475Smrg    for (i = 0; i <= fontMenu_lastBuiltin; ++i) {
5764d522f475Smrg	init_Dres(misc.face_size[i]);
5765d522f475Smrg    }
5766d522f475Smrg    init_Sres(misc.face_name);
5767d522f475Smrg    init_Sres(misc.face_wide_name);
5768d522f475Smrg    init_Bres(misc.render_font);
5769d522f475Smrg    /* minor tweak to make debug traces consistent: */
5770d522f475Smrg    if (wnew->misc.render_font) {
5771d522f475Smrg	if (wnew->misc.face_name == 0) {
5772d522f475Smrg	    wnew->misc.render_font = False;
5773d522f475Smrg	    TRACE(("reset render_font since there is no face_name\n"));
5774d522f475Smrg	}
5775d522f475Smrg    }
5776d522f475Smrg#endif
5777d522f475Smrg
5778d522f475Smrg    init_Ires(screen.utf8_inparse);
5779d522f475Smrg    init_Ires(screen.utf8_mode);
5780d522f475Smrg    init_Ires(screen.max_combining);
5781d522f475Smrg
5782d522f475Smrg    if (wnew->screen.max_combining < 0) {
5783d522f475Smrg	wnew->screen.max_combining = 0;
5784d522f475Smrg    }
5785d522f475Smrg    if (wnew->screen.max_combining > 5) {
5786d522f475Smrg	wnew->screen.max_combining = 5;
5787d522f475Smrg    }
5788d522f475Smrg
5789d522f475Smrg    init_Bres(screen.vt100_graphics);
5790d522f475Smrg    init_Bres(screen.wide_chars);
5791d522f475Smrg    init_Bres(misc.mk_width);
5792d522f475Smrg    init_Bres(misc.cjk_width);
5793d522f475Smrg
5794d522f475Smrg    init_Ires(misc.mk_samplesize);
5795d522f475Smrg    init_Ires(misc.mk_samplepass);
5796d522f475Smrg
5797d522f475Smrg    if (wnew->misc.mk_samplesize > 0xffff)
5798d522f475Smrg	wnew->misc.mk_samplesize = 0xffff;
5799d522f475Smrg    if (wnew->misc.mk_samplesize < 0)
5800d522f475Smrg	wnew->misc.mk_samplesize = 0;
5801d522f475Smrg
5802d522f475Smrg    if (wnew->misc.mk_samplepass > wnew->misc.mk_samplesize)
5803d522f475Smrg	wnew->misc.mk_samplepass = wnew->misc.mk_samplesize;
5804d522f475Smrg    if (wnew->misc.mk_samplepass < 0)
5805d522f475Smrg	wnew->misc.mk_samplepass = 0;
5806d522f475Smrg
5807d522f475Smrg    if (request->screen.utf8_mode) {
5808d522f475Smrg	TRACE(("setting wide_chars on\n"));
5809d522f475Smrg	wnew->screen.wide_chars = True;
5810d522f475Smrg    } else {
5811d522f475Smrg	TRACE(("setting utf8_mode to 0\n"));
5812d522f475Smrg	wnew->screen.utf8_mode = uFalse;
5813d522f475Smrg    }
5814d522f475Smrg    TRACE(("initialized UTF-8 mode to %d\n", wnew->screen.utf8_mode));
5815d522f475Smrg
5816d522f475Smrg#if OPT_MINI_LUIT
5817d522f475Smrg    if (request->screen.latin9_mode) {
5818d522f475Smrg	wnew->screen.latin9_mode = True;
5819d522f475Smrg    }
5820d522f475Smrg    if (request->screen.unicode_font) {
5821d522f475Smrg	wnew->screen.unicode_font = True;
5822d522f475Smrg    }
5823d522f475Smrg    TRACE(("initialized Latin9 mode to %d\n", wnew->screen.latin9_mode));
5824d522f475Smrg    TRACE(("initialized unicode_font to %d\n", wnew->screen.unicode_font));
5825d522f475Smrg#endif
5826d522f475Smrg
5827d522f475Smrg    if (wnew->screen.wide_chars != False)
5828d522f475Smrg	wnew->num_ptrs = OFF_FINAL + (wnew->screen.max_combining * 2);
5829d522f475Smrg
5830d522f475Smrg    decode_wcwidth((wnew->misc.cjk_width ? 2 : 0)
5831d522f475Smrg		   + (wnew->misc.mk_width ? 1 : 0)
5832d522f475Smrg		   + 1,
5833d522f475Smrg		   wnew->misc.mk_samplesize,
5834d522f475Smrg		   wnew->misc.mk_samplepass);
5835d522f475Smrg#endif /* OPT_WIDE_CHARS */
5836d522f475Smrg
5837d522f475Smrg    init_Bres(screen.always_bold_mode);
5838d522f475Smrg    init_Bres(screen.bold_mode);
5839d522f475Smrg    init_Bres(screen.underline);
5840d522f475Smrg
5841d522f475Smrg    wnew->cur_foreground = 0;
5842d522f475Smrg    wnew->cur_background = 0;
5843d522f475Smrg
5844d522f475Smrg    wnew->keyboard.flags = MODE_SRM;
5845d522f475Smrg    if (wnew->screen.backarrow_key)
5846d522f475Smrg	wnew->keyboard.flags |= MODE_DECBKM;
5847d522f475Smrg    TRACE(("initialized DECBKM %s\n",
5848d522f475Smrg	   BtoS(wnew->keyboard.flags & MODE_DECBKM)));
5849d522f475Smrg
5850d522f475Smrg    /* look for focus related events on the shell, because we need
5851d522f475Smrg     * to care about the shell's border being part of our focus.
5852d522f475Smrg     */
5853d522f475Smrg    XtAddEventHandler(my_parent, EnterWindowMask, False,
5854d522f475Smrg		      HandleEnterWindow, (Opaque) NULL);
5855d522f475Smrg    XtAddEventHandler(my_parent, LeaveWindowMask, False,
5856d522f475Smrg		      HandleLeaveWindow, (Opaque) NULL);
5857d522f475Smrg    XtAddEventHandler(my_parent, FocusChangeMask, False,
5858d522f475Smrg		      HandleFocusChange, (Opaque) NULL);
5859d522f475Smrg    XtAddEventHandler((Widget) wnew, 0L, True,
5860d522f475Smrg		      VTNonMaskableEvent, (Opaque) NULL);
5861d522f475Smrg    XtAddEventHandler((Widget) wnew, PropertyChangeMask, False,
5862d522f475Smrg		      HandleBellPropertyChange, (Opaque) NULL);
5863d522f475Smrg
5864d522f475Smrg#if HANDLE_STRUCT_NOTIFY
5865d522f475Smrg#if OPT_TOOLBAR
5866d522f475Smrg    wnew->VT100_TB_INFO(menu_bar) = request->VT100_TB_INFO(menu_bar);
5867d522f475Smrg    init_Ires(VT100_TB_INFO(menu_height));
5868d522f475Smrg#else
5869d522f475Smrg    /* Flag icon name with "***"  on window output when iconified.
5870d522f475Smrg     * Put in a handler that will tell us when we get Map/Unmap events.
5871d522f475Smrg     */
5872d522f475Smrg    if (resource.zIconBeep)
5873d522f475Smrg#endif
5874d522f475Smrg	XtAddEventHandler(my_parent, StructureNotifyMask, False,
5875d522f475Smrg			  HandleStructNotify, (Opaque) 0);
5876d522f475Smrg#endif /* HANDLE_STRUCT_NOTIFY */
5877d522f475Smrg
5878d522f475Smrg    wnew->screen.bellInProgress = False;
5879d522f475Smrg
5880d522f475Smrg    set_character_class(wnew->screen.charClass);
5881d522f475Smrg
5882d522f475Smrg    /* create it, but don't realize it */
5883d522f475Smrg    ScrollBarOn(wnew, True, False);
5884d522f475Smrg
5885d522f475Smrg    /* make sure that the resize gravity acceptable */
5886d522f475Smrg    if (wnew->misc.resizeGravity != NorthWestGravity &&
5887d522f475Smrg	wnew->misc.resizeGravity != SouthWestGravity) {
5888d522f475Smrg	char value[80];
5889d522f475Smrg	char *temp[2];
5890d522f475Smrg	Cardinal nparams = 1;
5891d522f475Smrg
5892d522f475Smrg	sprintf(temp[0] = value, "%d", wnew->misc.resizeGravity);
5893d522f475Smrg	temp[1] = 0;
5894d522f475Smrg	XtAppWarningMsg(app_con, "rangeError", "resizeGravity", "XTermError",
5895d522f475Smrg			"unsupported resizeGravity resource value (%s)",
5896d522f475Smrg			temp, &nparams);
5897d522f475Smrg	wnew->misc.resizeGravity = SouthWestGravity;
5898d522f475Smrg    }
5899d522f475Smrg#ifndef NO_ACTIVE_ICON
5900d522f475Smrg    wnew->screen.whichVwin = &wnew->screen.fullVwin;
5901d522f475Smrg#endif /* NO_ACTIVE_ICON */
5902d522f475Smrg
5903d522f475Smrg    if (wnew->screen.savelines < 0)
5904d522f475Smrg	wnew->screen.savelines = 0;
5905d522f475Smrg
5906d522f475Smrg    init_Bres(screen.awaitInput);
5907d522f475Smrg
5908d522f475Smrg    wnew->flags = 0;
5909d522f475Smrg    if (!wnew->screen.jumpscroll)
5910d522f475Smrg	wnew->flags |= SMOOTHSCROLL;
5911d522f475Smrg    if (wnew->misc.reverseWrap)
5912d522f475Smrg	wnew->flags |= REVERSEWRAP;
5913d522f475Smrg    if (wnew->misc.autoWrap)
5914d522f475Smrg	wnew->flags |= WRAPAROUND;
5915d522f475Smrg    if (wnew->misc.re_verse != wnew->misc.re_verse0)
5916d522f475Smrg	wnew->flags |= REVERSE_VIDEO;
5917d522f475Smrg    if (wnew->screen.c132)
5918d522f475Smrg	wnew->flags |= IN132COLUMNS;
5919d522f475Smrg
5920d522f475Smrg    wnew->initflags = wnew->flags;
5921d522f475Smrg
5922d522f475Smrg#if OPT_MOD_FKEYS
5923d522f475Smrg    init_Ires(keyboard.modify_1st.cursor_keys);
5924d522f475Smrg    init_Ires(keyboard.modify_1st.function_keys);
5925d522f475Smrg    init_Ires(keyboard.modify_1st.keypad_keys);
5926d522f475Smrg    init_Ires(keyboard.modify_1st.other_keys);
5927d522f475Smrg    init_Ires(keyboard.modify_1st.string_keys);
5928d522f475Smrg    init_Ires(keyboard.format_keys);
5929d522f475Smrg    wnew->keyboard.modify_now = wnew->keyboard.modify_1st;
5930d522f475Smrg#endif
5931d522f475Smrg
5932d522f475Smrg    init_Ires(misc.appcursorDefault);
5933d522f475Smrg    if (wnew->misc.appcursorDefault)
5934d522f475Smrg	wnew->keyboard.flags |= MODE_DECCKM;
5935d522f475Smrg
5936d522f475Smrg    init_Ires(misc.appkeypadDefault);
5937d522f475Smrg    if (wnew->misc.appkeypadDefault)
5938d522f475Smrg	wnew->keyboard.flags |= MODE_DECKPAM;
5939d522f475Smrg
5940d522f475Smrg    return;
5941d522f475Smrg}
5942d522f475Smrg
5943d522f475Smrgvoid
5944d522f475SmrgreleaseCursorGCs(XtermWidget xw)
5945d522f475Smrg{
5946d522f475Smrg    TScreen *screen = &xw->screen;
5947d522f475Smrg    VTwin *win = WhichVWin(screen);
5948d522f475Smrg    int n;
5949d522f475Smrg
5950d522f475Smrg    for_each_curs_gc(n) {
5951d522f475Smrg	freeCgs(xw, win, (CgsEnum) n);
5952d522f475Smrg    }
5953d522f475Smrg}
5954d522f475Smrg
5955d522f475Smrgvoid
5956d522f475SmrgreleaseWindowGCs(XtermWidget xw, VTwin * win)
5957d522f475Smrg{
5958d522f475Smrg    int n;
5959d522f475Smrg
5960d522f475Smrg    for_each_text_gc(n) {
5961d522f475Smrg	freeCgs(xw, win, (CgsEnum) n);
5962d522f475Smrg    }
5963d522f475Smrg}
5964d522f475Smrg
5965d522f475Smrg#define TRACE_FREE_LEAK(name) \
5966d522f475Smrg	if (name) { \
5967d522f475Smrg	    free(name); \
5968d522f475Smrg	    name = 0; \
5969d522f475Smrg	    TRACE(("freed " #name "\n")); \
5970d522f475Smrg	}
5971d522f475Smrg
5972d522f475Smrg#define FREE_LEAK(name) \
5973d522f475Smrg	if (name) { \
5974d522f475Smrg	    free(name); \
5975d522f475Smrg	    name = 0; \
5976d522f475Smrg	}
5977d522f475Smrg
5978d522f475Smrg#ifdef NO_LEAKS
5979d522f475Smrg#if OPT_RENDERFONT
5980d522f475Smrgstatic void
5981d522f475SmrgxtermCloseXft(TScreen * screen, XftFont ** pub)
5982d522f475Smrg{
5983d522f475Smrg    if (*pub != 0) {
5984d522f475Smrg	XftFontClose(screen->display, *pub);
5985d522f475Smrg	*pub = 0;
5986d522f475Smrg    }
5987d522f475Smrg}
5988d522f475Smrg#endif
5989d522f475Smrg#endif
5990d522f475Smrg
5991d522f475Smrgstatic void
5992d522f475SmrgVTDestroy(Widget w GCC_UNUSED)
5993d522f475Smrg{
5994d522f475Smrg#ifdef NO_LEAKS
5995d522f475Smrg    XtermWidget xw = (XtermWidget) w;
5996d522f475Smrg    TScreen *screen = &xw->screen;
5997d522f475Smrg    Cardinal n;
5998d522f475Smrg
5999d522f475Smrg    StopBlinking(screen);
6000d522f475Smrg
6001d522f475Smrg    if (screen->scrollWidget)
6002d522f475Smrg	XtDestroyWidget(screen->scrollWidget);
6003d522f475Smrg
6004d522f475Smrg    TRACE_FREE_LEAK(screen->save_ptr);
6005d522f475Smrg    TRACE_FREE_LEAK(screen->sbuf_address);
6006d522f475Smrg    TRACE_FREE_LEAK(screen->allbuf);
6007d522f475Smrg    TRACE_FREE_LEAK(screen->abuf_address);
6008d522f475Smrg    TRACE_FREE_LEAK(screen->altbuf);
6009d522f475Smrg    TRACE_FREE_LEAK(screen->keyboard_dialect);
6010d522f475Smrg    TRACE_FREE_LEAK(screen->term_id);
6011d522f475Smrg#if OPT_WIDE_CHARS
6012d522f475Smrg    TRACE_FREE_LEAK(screen->draw_buf);
6013d522f475Smrg#if OPT_LUIT_PROG
6014d522f475Smrg    TRACE_FREE_LEAK(xw->misc.locale_str);
6015d522f475Smrg    TRACE_FREE_LEAK(xw->misc.localefilter);
6016d522f475Smrg#endif
6017d522f475Smrg#endif
6018d522f475Smrg#if OPT_INPUT_METHOD
6019d522f475Smrg    if (screen->xim) {
6020d522f475Smrg	XCloseIM(screen->xim);
6021d522f475Smrg	TRACE(("freed screen->xim\n"));
6022d522f475Smrg    }
6023d522f475Smrg#endif
6024d522f475Smrg    releaseCursorGCs(xw);
6025d522f475Smrg    releaseWindowGCs(xw, &(screen->fullVwin));
6026d522f475Smrg#ifndef NO_ACTIVE_ICON
6027d522f475Smrg    releaseWindowGCs(xw, &(screen->iconVwin));
6028d522f475Smrg#endif
6029d522f475Smrg
6030d522f475Smrg    if (screen->hidden_cursor)
6031d522f475Smrg	XFreeCursor(screen->display, screen->hidden_cursor);
6032d522f475Smrg
6033d522f475Smrg    xtermCloseFonts(xw, screen->fnts);
6034d522f475Smrg    noleaks_cachedCgs(xw);
6035d522f475Smrg
6036d522f475Smrg#if OPT_RENDERFONT
6037d522f475Smrg    for (n = 0; n < NMENUFONTS; ++n) {
6038d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontNorm[n]));
6039d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontBold[n]));
6040d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontItal[n]));
6041d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideNorm[n]));
6042d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideBold[n]));
6043d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideItal[n]));
6044d522f475Smrg    }
6045d522f475Smrg#endif
6046d522f475Smrg
6047d522f475Smrg#if 0				/* some strings may be owned by X libraries */
6048d522f475Smrg    for (n = 0; n <= fontMenu_lastBuiltin; ++n) {
6049d522f475Smrg	int k;
6050d522f475Smrg	for (k = 0; k < fMAX; ++k) {
6051d522f475Smrg	    char *s = screen->menu_font_names[n][k];
6052d522f475Smrg	    if (s != 0)
6053d522f475Smrg		free(s);
6054d522f475Smrg	}
6055d522f475Smrg    }
6056d522f475Smrg#endif
6057d522f475Smrg
6058d522f475Smrg#if OPT_COLOR_RES
6059d522f475Smrg    /* free local copies of resource strings */
6060d522f475Smrg    for (n = 0; n < NCOLORS; ++n) {
6061d522f475Smrg	FREE_LEAK(screen->Tcolors[n].resource);
6062d522f475Smrg    }
6063d522f475Smrg#endif
6064d522f475Smrg#if OPT_SELECT_REGEX
6065d522f475Smrg    for (n = 0; n < NSELECTUNITS; ++n) {
6066d522f475Smrg	FREE_LEAK(screen->selectExpr[n]);
6067d522f475Smrg    }
6068d522f475Smrg#endif
6069d522f475Smrg
6070d522f475Smrg    if (screen->selection_atoms)
6071d522f475Smrg	XtFree((char *) (screen->selection_atoms));
6072d522f475Smrg
6073d522f475Smrg    XtFree((char *) (screen->selection_data));
6074d522f475Smrg
6075d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.extra_translations);
6076d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.shell_translations);
6077d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.xterm_translations);
6078d522f475Smrg#endif /* defined(NO_LEAKS) */
6079d522f475Smrg}
6080d522f475Smrg
6081d522f475Smrg/*ARGSUSED*/
6082d522f475Smrgstatic void
6083d522f475SmrgVTRealize(Widget w,
6084d522f475Smrg	  XtValueMask * valuemask,
6085d522f475Smrg	  XSetWindowAttributes * values)
6086d522f475Smrg{
6087d522f475Smrg    XtermWidget xw = (XtermWidget) w;
6088d522f475Smrg    TScreen *screen = &xw->screen;
6089d522f475Smrg
6090d522f475Smrg    const VTFontNames *myfont;
6091d522f475Smrg    unsigned width, height;
6092d522f475Smrg    int xpos, ypos, pr;
6093d522f475Smrg    Atom pid_atom;
6094d522f475Smrg    int i;
6095d522f475Smrg
6096d522f475Smrg    TRACE(("VTRealize\n"));
6097d522f475Smrg
6098d522f475Smrg    TabReset(xw->tabs);
6099d522f475Smrg
6100d522f475Smrg    if (screen->menu_font_number == fontMenu_default) {
6101d522f475Smrg	myfont = &(xw->misc.default_font);
6102d522f475Smrg    } else {
6103d522f475Smrg	myfont = xtermFontName(screen->MenuFontName(screen->menu_font_number));
6104d522f475Smrg    }
6105d522f475Smrg    memset(screen->fnts, 0, sizeof(screen->fnts));
6106d522f475Smrg
6107d522f475Smrg    if (!xtermLoadFont(xw,
6108d522f475Smrg		       myfont,
6109d522f475Smrg		       False,
6110d522f475Smrg		       screen->menu_font_number)) {
6111d522f475Smrg	if (XmuCompareISOLatin1(myfont->f_n, DEFFONT) != 0) {
6112d522f475Smrg	    fprintf(stderr,
6113d522f475Smrg		    "%s:  unable to open font \"%s\", trying \"%s\"....\n",
6114d522f475Smrg		    xterm_name, myfont->f_n, DEFFONT);
6115d522f475Smrg	    (void) xtermLoadFont(xw,
6116d522f475Smrg				 xtermFontName(DEFFONT),
6117d522f475Smrg				 False,
6118d522f475Smrg				 screen->menu_font_number);
6119d522f475Smrg	    screen->MenuFontName(screen->menu_font_number) = DEFFONT;
6120d522f475Smrg	}
6121d522f475Smrg    }
6122d522f475Smrg
6123d522f475Smrg    /* really screwed if we couldn't open default font */
6124d522f475Smrg    if (!screen->fnts[fNorm].fs) {
6125d522f475Smrg	fprintf(stderr, "%s:  unable to locate a suitable font\n",
6126d522f475Smrg		xterm_name);
6127d522f475Smrg	Exit(1);
6128d522f475Smrg    }
6129d522f475Smrg#if OPT_WIDE_CHARS
6130d522f475Smrg    if (xw->screen.utf8_mode) {
6131d522f475Smrg	TRACE(("check if this is a wide font, if not try again\n"));
6132d522f475Smrg	if (xtermLoadWideFonts(xw, False))
6133d522f475Smrg	    SetVTFont(xw, screen->menu_font_number, True, NULL);
6134d522f475Smrg    }
6135d522f475Smrg#endif
6136d522f475Smrg
6137d522f475Smrg    /* making cursor */
6138d522f475Smrg    if (!screen->pointer_cursor) {
6139d522f475Smrg	screen->pointer_cursor =
6140d522f475Smrg	    make_colored_cursor(XC_xterm,
6141d522f475Smrg				T_COLOR(screen, MOUSE_FG),
6142d522f475Smrg				T_COLOR(screen, MOUSE_BG));
6143d522f475Smrg    } else {
6144d522f475Smrg	recolor_cursor(screen,
6145d522f475Smrg		       screen->pointer_cursor,
6146d522f475Smrg		       T_COLOR(screen, MOUSE_FG),
6147d522f475Smrg		       T_COLOR(screen, MOUSE_BG));
6148d522f475Smrg    }
6149d522f475Smrg
6150d522f475Smrg    /* set defaults */
6151d522f475Smrg    xpos = 1;
6152d522f475Smrg    ypos = 1;
6153d522f475Smrg    width = 80;
6154d522f475Smrg    height = 24;
6155d522f475Smrg
6156d522f475Smrg    TRACE(("parsing geo_metry %s\n", NonNull(xw->misc.geo_metry)));
6157d522f475Smrg    pr = XParseGeometry(xw->misc.geo_metry, &xpos, &ypos,
6158d522f475Smrg			&width, &height);
6159d522f475Smrg    TRACE(("... position %d,%d size %dx%d\n", ypos, xpos, height, width));
6160d522f475Smrg
6161d522f475Smrg    set_max_col(screen, (int) (width - 1));	/* units in character cells */
6162d522f475Smrg    set_max_row(screen, (int) (height - 1));	/* units in character cells */
6163d522f475Smrg    xtermUpdateFontInfo(xw, False);
6164d522f475Smrg
6165d522f475Smrg    width = screen->fullVwin.fullwidth;
6166d522f475Smrg    height = screen->fullVwin.fullheight;
6167d522f475Smrg
6168d522f475Smrg    TRACE(("... border widget %d parent %d shell %d\n",
6169d522f475Smrg	   BorderWidth(xw),
6170d522f475Smrg	   BorderWidth(XtParent(xw)),
6171d522f475Smrg	   BorderWidth(SHELL_OF(xw))));
6172d522f475Smrg
6173d522f475Smrg    if ((pr & XValue) && (XNegative & pr)) {
6174d522f475Smrg	xpos += DisplayWidth(screen->display, DefaultScreen(screen->display))
6175d522f475Smrg	    - width - (BorderWidth(XtParent(xw)) * 2);
6176d522f475Smrg    }
6177d522f475Smrg    if ((pr & YValue) && (YNegative & pr)) {
6178d522f475Smrg	ypos += DisplayHeight(screen->display, DefaultScreen(screen->display))
6179d522f475Smrg	    - height - (BorderWidth(XtParent(xw)) * 2);
6180d522f475Smrg    }
6181d522f475Smrg
6182d522f475Smrg    /* set up size hints for window manager; min 1 char by 1 char */
6183d522f475Smrg    getXtermSizeHints(xw);
6184d522f475Smrg    xtermSizeHints(xw, (xw->misc.scrollbar
6185d522f475Smrg			? (screen->scrollWidget->core.width
6186d522f475Smrg			   + BorderWidth(screen->scrollWidget))
6187d522f475Smrg			: 0));
6188d522f475Smrg
6189d522f475Smrg    xw->hints.x = xpos;
6190d522f475Smrg    xw->hints.y = ypos;
6191d522f475Smrg    if ((XValue & pr) || (YValue & pr)) {
6192d522f475Smrg	xw->hints.flags |= USSize | USPosition;
6193d522f475Smrg	xw->hints.flags |= PWinGravity;
6194d522f475Smrg	switch (pr & (XNegative | YNegative)) {
6195d522f475Smrg	case 0:
6196d522f475Smrg	    xw->hints.win_gravity = NorthWestGravity;
6197d522f475Smrg	    break;
6198d522f475Smrg	case XNegative:
6199d522f475Smrg	    xw->hints.win_gravity = NorthEastGravity;
6200d522f475Smrg	    break;
6201d522f475Smrg	case YNegative:
6202d522f475Smrg	    xw->hints.win_gravity = SouthWestGravity;
6203d522f475Smrg	    break;
6204d522f475Smrg	default:
6205d522f475Smrg	    xw->hints.win_gravity = SouthEastGravity;
6206d522f475Smrg	    break;
6207d522f475Smrg	}
6208d522f475Smrg    } else {
6209d522f475Smrg	/* set a default size, but do *not* set position */
6210d522f475Smrg	xw->hints.flags |= PSize;
6211d522f475Smrg    }
6212d522f475Smrg    xw->hints.height = xw->hints.base_height
6213d522f475Smrg	+ xw->hints.height_inc * MaxRows(screen);
6214d522f475Smrg    xw->hints.width = xw->hints.base_width
6215d522f475Smrg	+ xw->hints.width_inc * MaxCols(screen);
6216d522f475Smrg
6217d522f475Smrg    if ((WidthValue & pr) || (HeightValue & pr))
6218d522f475Smrg	xw->hints.flags |= USSize;
6219d522f475Smrg    else
6220d522f475Smrg	xw->hints.flags |= PSize;
6221d522f475Smrg
6222d522f475Smrg    /*
6223d522f475Smrg     * Note that the size-hints are for the shell, while the resize-request
6224d522f475Smrg     * is for the vt100 widget.  They are not the same size.
6225d522f475Smrg     */
6226d522f475Smrg    TRACE(("make resize request %dx%d\n", height, width));
6227d522f475Smrg    (void) XtMakeResizeRequest((Widget) xw,
6228d522f475Smrg			       (Dimension) width, (Dimension) height,
6229d522f475Smrg			       &xw->core.width, &xw->core.height);
6230d522f475Smrg    TRACE(("...made resize request %dx%d\n", xw->core.height, xw->core.width));
6231d522f475Smrg
6232d522f475Smrg    /* XXX This is bogus.  We are parsing geometries too late.  This
6233d522f475Smrg     * is information that the shell widget ought to have before we get
6234d522f475Smrg     * realized, so that it can do the right thing.
6235d522f475Smrg     */
6236d522f475Smrg    if (xw->hints.flags & USPosition)
6237d522f475Smrg	XMoveWindow(XtDisplay(xw), XtWindow(SHELL_OF(xw)),
6238d522f475Smrg		    xw->hints.x, xw->hints.y);
6239d522f475Smrg
6240d522f475Smrg    TRACE(("%s@%d -- ", __FILE__, __LINE__));
6241d522f475Smrg    TRACE_HINTS(&xw->hints);
6242d522f475Smrg    XSetWMNormalHints(XtDisplay(xw), XtWindow(SHELL_OF(xw)), &xw->hints);
6243d522f475Smrg    TRACE(("%s@%d -- ", __FILE__, __LINE__));
6244d522f475Smrg    TRACE_WM_HINTS(xw);
6245d522f475Smrg
6246d522f475Smrg    if ((pid_atom = XInternAtom(XtDisplay(xw), "_NET_WM_PID", False)) != None) {
6247d522f475Smrg	/* XChangeProperty format 32 really is "long" */
6248d522f475Smrg	unsigned long pid_l = (unsigned long) getpid();
6249d522f475Smrg	TRACE(("Setting _NET_WM_PID property to %lu\n", pid_l));
6250d522f475Smrg	XChangeProperty(XtDisplay(xw), VShellWindow,
6251d522f475Smrg			pid_atom, XA_CARDINAL, 32, PropModeReplace,
6252d522f475Smrg			(unsigned char *) &pid_l, 1);
6253d522f475Smrg    }
6254d522f475Smrg
6255d522f475Smrg    XFlush(XtDisplay(xw));	/* get it out to window manager */
6256d522f475Smrg
6257d522f475Smrg    /* use ForgetGravity instead of SouthWestGravity because translating
6258d522f475Smrg       the Expose events for ConfigureNotifys is too hard */
6259d522f475Smrg    values->bit_gravity = ((xw->misc.resizeGravity == NorthWestGravity)
6260d522f475Smrg			   ? NorthWestGravity
6261d522f475Smrg			   : ForgetGravity);
6262d522f475Smrg    xw->screen.fullVwin.window = XtWindow(xw) =
6263d522f475Smrg	XCreateWindow(XtDisplay(xw), XtWindow(XtParent(xw)),
6264d522f475Smrg		      xw->core.x, xw->core.y,
6265d522f475Smrg		      xw->core.width, xw->core.height, BorderWidth(xw),
6266d522f475Smrg		      (int) xw->core.depth,
6267d522f475Smrg		      InputOutput, CopyFromParent,
6268d522f475Smrg		      *valuemask | CWBitGravity, values);
6269d522f475Smrg    screen->event_mask = values->event_mask;
6270d522f475Smrg
6271d522f475Smrg#ifndef NO_ACTIVE_ICON
6272d522f475Smrg    if (xw->misc.active_icon && screen->fnt_icon.fs) {
6273d522f475Smrg	int iconX = 0, iconY = 0;
6274d522f475Smrg	Widget shell = SHELL_OF(xw);
6275d522f475Smrg	VTwin *win = &(screen->iconVwin);
6276d522f475Smrg
6277d522f475Smrg	TRACE(("Initializing active-icon\n"));
6278d522f475Smrg	XtVaGetValues(shell, XtNiconX, &iconX, XtNiconY, &iconY, (XtPointer) 0);
6279d522f475Smrg	xtermComputeFontInfo(xw, &(screen->iconVwin), screen->fnt_icon.fs, 0);
6280d522f475Smrg
6281d522f475Smrg	/* since only one client is permitted to select for Button
6282d522f475Smrg	 * events, we have to let the window manager get 'em...
6283d522f475Smrg	 */
6284d522f475Smrg	values->event_mask &= ~(ButtonPressMask | ButtonReleaseMask);
6285d522f475Smrg	values->border_pixel = xw->misc.icon_border_pixel;
6286d522f475Smrg
6287d522f475Smrg	screen->iconVwin.window =
6288d522f475Smrg	    XCreateWindow(XtDisplay(xw),
6289d522f475Smrg			  RootWindowOfScreen(XtScreen(shell)),
6290d522f475Smrg			  iconX, iconY,
6291d522f475Smrg			  screen->iconVwin.fullwidth,
6292d522f475Smrg			  screen->iconVwin.fullheight,
6293d522f475Smrg			  xw->misc.icon_border_width,
6294d522f475Smrg			  (int) xw->core.depth,
6295d522f475Smrg			  InputOutput, CopyFromParent,
6296d522f475Smrg			  *valuemask | CWBitGravity | CWBorderPixel,
6297d522f475Smrg			  values);
6298d522f475Smrg	XtVaSetValues(shell,
6299d522f475Smrg		      XtNiconWindow, screen->iconVwin.window,
6300d522f475Smrg		      (XtPointer) 0);
6301d522f475Smrg	XtRegisterDrawable(XtDisplay(xw), screen->iconVwin.window, w);
6302d522f475Smrg
6303d522f475Smrg	setCgsFont(xw, win, gcNorm, &(screen->fnt_icon));
6304d522f475Smrg	setCgsFore(xw, win, gcNorm, T_COLOR(screen, TEXT_FG));
6305d522f475Smrg	setCgsBack(xw, win, gcNorm, T_COLOR(screen, TEXT_BG));
6306d522f475Smrg
6307d522f475Smrg	copyCgs(xw, win, gcBold, gcNorm);
6308d522f475Smrg
6309d522f475Smrg	setCgsFont(xw, win, gcNormReverse, &(screen->fnt_icon));
6310d522f475Smrg	setCgsFore(xw, win, gcNormReverse, T_COLOR(screen, TEXT_BG));
6311d522f475Smrg	setCgsBack(xw, win, gcNormReverse, T_COLOR(screen, TEXT_FG));
6312d522f475Smrg
6313d522f475Smrg	copyCgs(xw, win, gcBoldReverse, gcNormReverse);
6314d522f475Smrg
6315d522f475Smrg#if OPT_TOOLBAR
6316d522f475Smrg	/*
6317d522f475Smrg	 * Toolbar is initialized before we get here.  Enable the menu item
6318d522f475Smrg	 * and set it properly.
6319d522f475Smrg	 */
6320d522f475Smrg	SetItemSensitivity(vtMenuEntries[vtMenu_activeicon].widget, True);
6321d522f475Smrg	update_activeicon();
6322d522f475Smrg#endif
6323d522f475Smrg    } else {
6324d522f475Smrg	TRACE(("Disabled active-icon\n"));
6325d522f475Smrg	xw->misc.active_icon = False;
6326d522f475Smrg    }
6327d522f475Smrg#endif /* NO_ACTIVE_ICON */
6328d522f475Smrg
6329d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
6330d522f475Smrg    VTInitI18N();
6331d522f475Smrg#else
6332d522f475Smrg    xw->screen.xic = NULL;
6333d522f475Smrg#endif
6334d522f475Smrg#if OPT_NUM_LOCK
6335d522f475Smrg    VTInitModifiers(xw);
6336d522f475Smrg#if OPT_EXTRA_PASTE
6337d522f475Smrg    if (xw->keyboard.extra_translations) {
6338d522f475Smrg	XtOverrideTranslations((Widget) xw,
6339d522f475Smrg			       XtParseTranslationTable(xw->keyboard.extra_translations));
6340d522f475Smrg    }
6341d522f475Smrg#endif
6342d522f475Smrg#endif
6343d522f475Smrg
6344d522f475Smrg    set_cursor_gcs(xw);
6345d522f475Smrg
6346d522f475Smrg    /* Reset variables used by ANSI emulation. */
6347d522f475Smrg
6348d522f475Smrg    resetCharsets(screen);
6349d522f475Smrg
6350d522f475Smrg    XDefineCursor(screen->display, VShellWindow, screen->pointer_cursor);
6351d522f475Smrg
6352d522f475Smrg    set_cur_col(screen, 0);
6353d522f475Smrg    set_cur_row(screen, 0);
6354d522f475Smrg    set_max_col(screen, Width(screen) / screen->fullVwin.f_width - 1);
6355d522f475Smrg    set_max_row(screen, Height(screen) / screen->fullVwin.f_height - 1);
6356d522f475Smrg    set_tb_margins(screen, 0, screen->max_row);
6357d522f475Smrg
6358d522f475Smrg    memset(screen->sc, 0, sizeof(screen->sc));
6359d522f475Smrg
6360d522f475Smrg    /* Mark screen buffer as unallocated.  We wait until the run loop so
6361d522f475Smrg       that the child process does not fork and exec with all the dynamic
6362d522f475Smrg       memory it will never use.  If we were to do it here, the
6363d522f475Smrg       swap space for new process would be huge for huge savelines. */
6364d522f475Smrg#if OPT_TEK4014
6365d522f475Smrg    if (!tekWidget)		/* if not called after fork */
6366d522f475Smrg#endif
6367d522f475Smrg	screen->visbuf = screen->allbuf = NULL;
6368d522f475Smrg
6369d522f475Smrg    screen->do_wrap = 0;
6370d522f475Smrg    screen->scrolls = screen->incopy = 0;
6371d522f475Smrg    xtermSetCursorBox(screen);
6372d522f475Smrg
6373d522f475Smrg    screen->savedlines = 0;
6374d522f475Smrg
6375d522f475Smrg    for (i = 0; i < 2; ++i) {
6376d522f475Smrg	screen->alternate = !screen->alternate;
6377d522f475Smrg	CursorSave(xw);
6378d522f475Smrg    }
6379d522f475Smrg
6380d522f475Smrg    /*
6381d522f475Smrg     * Do this last, since it may change the layout via a resize.
6382d522f475Smrg     */
6383d522f475Smrg    if (xw->misc.scrollbar) {
6384d522f475Smrg	screen->fullVwin.sb_info.width = 0;
6385d522f475Smrg	ScrollBarOn(xw, False, True);
6386d522f475Smrg    }
6387d522f475Smrg    return;
6388d522f475Smrg}
6389d522f475Smrg
6390d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
6391d522f475Smrg
6392d522f475Smrg/* limit this feature to recent XFree86 since X11R6.x core dumps */
6393d522f475Smrg#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && defined(X_HAVE_UTF8_STRING)
6394d522f475Smrg#define USE_XIM_INSTANTIATE_CB
6395d522f475Smrg
6396d522f475Smrgstatic void
6397d522f475Smrgxim_instantiate_cb(Display * display,
6398d522f475Smrg		   XPointer client_data GCC_UNUSED,
6399d522f475Smrg		   XPointer call_data GCC_UNUSED)
6400d522f475Smrg{
6401d522f475Smrg    if (display != XtDisplay(term))
6402d522f475Smrg	return;
6403d522f475Smrg
6404d522f475Smrg    VTInitI18N();
6405d522f475Smrg}
6406d522f475Smrg
6407d522f475Smrgstatic void
6408d522f475Smrgxim_destroy_cb(XIM im GCC_UNUSED,
6409d522f475Smrg	       XPointer client_data GCC_UNUSED,
6410d522f475Smrg	       XPointer call_data GCC_UNUSED)
6411d522f475Smrg{
6412d522f475Smrg    term->screen.xic = NULL;
6413d522f475Smrg
6414d522f475Smrg    XRegisterIMInstantiateCallback(XtDisplay(term), NULL, NULL, NULL,
6415d522f475Smrg				   xim_instantiate_cb, NULL);
6416d522f475Smrg}
6417d522f475Smrg#endif /* X11R6+ */
6418d522f475Smrg
6419d522f475Smrgstatic void
6420d522f475Smrgxim_real_init(void)
6421d522f475Smrg{
6422d522f475Smrg    unsigned i, j;
6423d522f475Smrg    char *p, *s, *t, *ns, *end, buf[32];
6424d522f475Smrg    XIMStyles *xim_styles;
6425d522f475Smrg    XIMStyle input_style = 0;
6426d522f475Smrg    Bool found;
6427d522f475Smrg    static struct {
6428d522f475Smrg	char *name;
6429d522f475Smrg	unsigned long code;
6430d522f475Smrg    } known_style[] = {
6431d522f475Smrg	{
6432d522f475Smrg	    "OverTheSpot", (XIMPreeditPosition | XIMStatusNothing)
6433d522f475Smrg	},
6434d522f475Smrg	{
6435d522f475Smrg	    "OffTheSpot", (XIMPreeditArea | XIMStatusArea)
6436d522f475Smrg	},
6437d522f475Smrg	{
6438d522f475Smrg	    "Root", (XIMPreeditNothing | XIMStatusNothing)
6439d522f475Smrg	},
6440d522f475Smrg    };
6441d522f475Smrg
6442d522f475Smrg    term->screen.xic = NULL;
6443d522f475Smrg
6444d522f475Smrg    if (term->misc.cannot_im) {
6445d522f475Smrg	return;
6446d522f475Smrg    }
6447d522f475Smrg
6448d522f475Smrg    if (!term->misc.input_method || !*term->misc.input_method) {
6449d522f475Smrg	if ((p = XSetLocaleModifiers("")) != NULL && *p)
6450d522f475Smrg	    term->screen.xim = XOpenIM(XtDisplay(term), NULL, NULL, NULL);
6451d522f475Smrg    } else {
6452d522f475Smrg	s = term->misc.input_method;
6453d522f475Smrg	i = 5 + strlen(s);
6454d522f475Smrg	t = (char *) MyStackAlloc(i, buf);
6455d522f475Smrg	if (t == NULL)
6456d522f475Smrg	    SysError(ERROR_VINIT);
6457d522f475Smrg
6458d522f475Smrg	for (ns = s; ns && *s;) {
6459d522f475Smrg	    while (*s && isspace(CharOf(*s)))
6460d522f475Smrg		s++;
6461d522f475Smrg	    if (!*s)
6462d522f475Smrg		break;
6463d522f475Smrg	    if ((ns = end = strchr(s, ',')) == 0)
6464d522f475Smrg		end = s + strlen(s);
6465d522f475Smrg	    while ((end != s) && isspace(CharOf(end[-1])))
6466d522f475Smrg		end--;
6467d522f475Smrg
6468d522f475Smrg	    if (end != s) {
6469d522f475Smrg		strcpy(t, "@im=");
6470d522f475Smrg		strncat(t, s, (unsigned) (end - s));
6471d522f475Smrg
6472d522f475Smrg		if ((p = XSetLocaleModifiers(t)) != 0 && *p
6473d522f475Smrg		    && (term->screen.xim = XOpenIM(XtDisplay(term),
6474d522f475Smrg						   NULL,
6475d522f475Smrg						   NULL,
6476d522f475Smrg						   NULL)) != 0)
6477d522f475Smrg		    break;
6478d522f475Smrg
6479d522f475Smrg	    }
6480d522f475Smrg	    s = ns + 1;
6481d522f475Smrg	}
6482d522f475Smrg	MyStackFree(t, buf);
6483d522f475Smrg    }
6484d522f475Smrg
6485d522f475Smrg    if (term->screen.xim == NULL
6486d522f475Smrg	&& (p = XSetLocaleModifiers("@im=none")) != NULL
6487d522f475Smrg	&& *p) {
6488d522f475Smrg	term->screen.xim = XOpenIM(XtDisplay(term), NULL, NULL, NULL);
6489d522f475Smrg    }
6490d522f475Smrg
6491d522f475Smrg    if (!term->screen.xim) {
6492d522f475Smrg	fprintf(stderr, "Failed to open input method\n");
6493d522f475Smrg	return;
6494d522f475Smrg    }
6495d522f475Smrg    TRACE(("VTInitI18N opened input method\n"));
6496d522f475Smrg
6497d522f475Smrg    if (XGetIMValues(term->screen.xim, XNQueryInputStyle, &xim_styles, NULL)
6498d522f475Smrg	|| !xim_styles
6499d522f475Smrg	|| !xim_styles->count_styles) {
6500d522f475Smrg	fprintf(stderr, "input method doesn't support any style\n");
6501d522f475Smrg	XCloseIM(term->screen.xim);
6502d522f475Smrg	term->misc.cannot_im = True;
6503d522f475Smrg	return;
6504d522f475Smrg    }
6505d522f475Smrg
6506d522f475Smrg    found = False;
6507d522f475Smrg    for (s = term->misc.preedit_type; s && !found;) {
6508d522f475Smrg	while (*s && isspace(CharOf(*s)))
6509d522f475Smrg	    s++;
6510d522f475Smrg	if (!*s)
6511d522f475Smrg	    break;
6512d522f475Smrg	if ((ns = end = strchr(s, ',')) != 0)
6513d522f475Smrg	    ns++;
6514d522f475Smrg	else
6515d522f475Smrg	    end = s + strlen(s);
6516d522f475Smrg	while ((end != s) && isspace(CharOf(end[-1])))
6517d522f475Smrg	    end--;
6518d522f475Smrg
6519d522f475Smrg	if (end != s) {		/* just in case we have a spurious comma */
6520d522f475Smrg	    TRACE(("looking for style '%.*s'\n", end - s, s));
6521d522f475Smrg	    for (i = 0; i < XtNumber(known_style); i++) {
6522d522f475Smrg		if ((int) strlen(known_style[i].name) == (end - s)
6523d522f475Smrg		    && !strncmp(s, known_style[i].name, (unsigned) (end - s))) {
6524d522f475Smrg		    input_style = known_style[i].code;
6525d522f475Smrg		    for (j = 0; j < xim_styles->count_styles; j++) {
6526d522f475Smrg			if (input_style == xim_styles->supported_styles[j]) {
6527d522f475Smrg			    found = True;
6528d522f475Smrg			    break;
6529d522f475Smrg			}
6530d522f475Smrg		    }
6531d522f475Smrg		    if (found)
6532d522f475Smrg			break;
6533d522f475Smrg		}
6534d522f475Smrg	    }
6535d522f475Smrg	}
6536d522f475Smrg
6537d522f475Smrg	s = ns;
6538d522f475Smrg    }
6539d522f475Smrg    XFree(xim_styles);
6540d522f475Smrg
6541d522f475Smrg    if (!found) {
6542d522f475Smrg	fprintf(stderr,
6543d522f475Smrg		"input method doesn't support my preedit type (%s)\n",
6544d522f475Smrg		term->misc.preedit_type);
6545d522f475Smrg	XCloseIM(term->screen.xim);
6546d522f475Smrg	term->misc.cannot_im = True;
6547d522f475Smrg	return;
6548d522f475Smrg    }
6549d522f475Smrg
6550d522f475Smrg    /*
6551d522f475Smrg     * Check for styles we do not yet support.
6552d522f475Smrg     */
6553d522f475Smrg    TRACE(("input_style %#lx\n", input_style));
6554d522f475Smrg    if (input_style == (XIMPreeditArea | XIMStatusArea)) {
6555d522f475Smrg	fprintf(stderr,
6556d522f475Smrg		"This program doesn't support the 'OffTheSpot' preedit type\n");
6557d522f475Smrg	XCloseIM(term->screen.xim);
6558d522f475Smrg	term->misc.cannot_im = True;
6559d522f475Smrg	return;
6560d522f475Smrg    }
6561d522f475Smrg
6562d522f475Smrg    /*
6563d522f475Smrg     * For XIMPreeditPosition (or OverTheSpot), XIM client has to
6564d522f475Smrg     * prepare a font.
6565d522f475Smrg     * The font has to be locale-dependent XFontSet, whereas
6566d522f475Smrg     * XTerm use Unicode font.  This leads a problem that the
6567d522f475Smrg     * same font cannot be used for XIM preedit.
6568d522f475Smrg     */
6569d522f475Smrg    if (input_style != (XIMPreeditNothing | XIMStatusNothing)) {
6570d522f475Smrg	char **missing_charset_list;
6571d522f475Smrg	int missing_charset_count;
6572d522f475Smrg	char *def_string;
6573d522f475Smrg	XVaNestedList p_list;
6574d522f475Smrg	XPoint spot =
6575d522f475Smrg	{0, 0};
6576d522f475Smrg	XFontStruct **fonts;
6577d522f475Smrg	char **font_name_list;
6578d522f475Smrg
6579d522f475Smrg	term->screen.fs = XCreateFontSet(XtDisplay(term),
6580d522f475Smrg					 term->misc.f_x,
6581d522f475Smrg					 &missing_charset_list,
6582d522f475Smrg					 &missing_charset_count,
6583d522f475Smrg					 &def_string);
6584d522f475Smrg	if (term->screen.fs == NULL) {
6585d522f475Smrg	    fprintf(stderr, "Preparation of font set "
6586d522f475Smrg		    "\"%s\" for XIM failed.\n", term->misc.f_x);
6587d522f475Smrg	    term->screen.fs = XCreateFontSet(XtDisplay(term),
6588d522f475Smrg					     DEFXIMFONT,
6589d522f475Smrg					     &missing_charset_list,
6590d522f475Smrg					     &missing_charset_count,
6591d522f475Smrg					     &def_string);
6592d522f475Smrg	}
6593d522f475Smrg	if (term->screen.fs == NULL) {
6594d522f475Smrg	    fprintf(stderr, "Preparation of default font set "
6595d522f475Smrg		    "\"%s\" for XIM failed.\n", DEFXIMFONT);
6596d522f475Smrg	    XCloseIM(term->screen.xim);
6597d522f475Smrg	    term->misc.cannot_im = True;
6598d522f475Smrg	    return;
6599d522f475Smrg	}
6600d522f475Smrg	(void) XExtentsOfFontSet(term->screen.fs);
6601d522f475Smrg	j = XFontsOfFontSet(term->screen.fs, &fonts, &font_name_list);
6602d522f475Smrg	for (i = 0, term->screen.fs_ascent = 0; i < j; i++) {
6603d522f475Smrg	    if (term->screen.fs_ascent < (*fonts)->ascent)
6604d522f475Smrg		term->screen.fs_ascent = (*fonts)->ascent;
6605d522f475Smrg	}
6606d522f475Smrg	p_list = XVaCreateNestedList(0,
6607d522f475Smrg				     XNSpotLocation, &spot,
6608d522f475Smrg				     XNFontSet, term->screen.fs,
6609d522f475Smrg				     NULL);
6610d522f475Smrg	term->screen.xic = XCreateIC(term->screen.xim,
6611d522f475Smrg				     XNInputStyle, input_style,
6612d522f475Smrg				     XNClientWindow, XtWindow(term),
6613d522f475Smrg				     XNFocusWindow, XtWindow(term),
6614d522f475Smrg				     XNPreeditAttributes, p_list,
6615d522f475Smrg				     NULL);
6616d522f475Smrg    } else {
6617d522f475Smrg	term->screen.xic = XCreateIC(term->screen.xim, XNInputStyle, input_style,
6618d522f475Smrg				     XNClientWindow, XtWindow(term),
6619d522f475Smrg				     XNFocusWindow, XtWindow(term),
6620d522f475Smrg				     NULL);
6621d522f475Smrg    }
6622d522f475Smrg
6623d522f475Smrg    if (!term->screen.xic) {
6624d522f475Smrg	fprintf(stderr, "Failed to create input context\n");
6625d522f475Smrg	XCloseIM(term->screen.xim);
6626d522f475Smrg    }
6627d522f475Smrg#if defined(USE_XIM_INSTANTIATE_CB)
6628d522f475Smrg    else {
6629d522f475Smrg	XIMCallback destroy_cb;
6630d522f475Smrg
6631d522f475Smrg	destroy_cb.callback = xim_destroy_cb;
6632d522f475Smrg	destroy_cb.client_data = NULL;
6633d522f475Smrg	if (XSetIMValues(term->screen.xim, XNDestroyCallback, &destroy_cb, NULL))
6634d522f475Smrg	    fprintf(stderr, "Could not set destroy callback to IM\n");
6635d522f475Smrg    }
6636d522f475Smrg#endif
6637d522f475Smrg
6638d522f475Smrg    return;
6639d522f475Smrg}
6640d522f475Smrg
6641d522f475Smrgstatic void
6642d522f475SmrgVTInitI18N(void)
6643d522f475Smrg{
6644d522f475Smrg    if (term->misc.open_im) {
6645d522f475Smrg	xim_real_init();
6646d522f475Smrg
6647d522f475Smrg#if defined(USE_XIM_INSTANTIATE_CB)
6648d522f475Smrg	if (term->screen.xic == NULL && !term->misc.cannot_im) {
6649d522f475Smrg	    sleep(3);
6650d522f475Smrg	    XRegisterIMInstantiateCallback(XtDisplay(term), NULL, NULL, NULL,
6651d522f475Smrg					   xim_instantiate_cb, NULL);
6652d522f475Smrg	}
6653d522f475Smrg#endif
6654d522f475Smrg    }
6655d522f475Smrg}
6656d522f475Smrg#endif /* OPT_I18N_SUPPORT && OPT_INPUT_METHOD */
6657d522f475Smrg
6658d522f475Smrgstatic Boolean
6659d522f475SmrgVTSetValues(Widget cur,
6660d522f475Smrg	    Widget request GCC_UNUSED,
6661d522f475Smrg	    Widget wnew,
6662d522f475Smrg	    ArgList args GCC_UNUSED,
6663d522f475Smrg	    Cardinal *num_args GCC_UNUSED)
6664d522f475Smrg{
6665d522f475Smrg    XtermWidget curvt = (XtermWidget) cur;
6666d522f475Smrg    XtermWidget newvt = (XtermWidget) wnew;
6667d522f475Smrg    Bool refresh_needed = False;
6668d522f475Smrg    Bool fonts_redone = False;
6669d522f475Smrg
6670d522f475Smrg    if ((T_COLOR(&(curvt->screen), TEXT_BG) !=
6671d522f475Smrg	 T_COLOR(&(newvt->screen), TEXT_BG)) ||
6672d522f475Smrg	(T_COLOR(&(curvt->screen), TEXT_FG) !=
6673d522f475Smrg	 T_COLOR(&(newvt->screen), TEXT_FG)) ||
6674d522f475Smrg	(curvt->screen.MenuFontName(curvt->screen.menu_font_number) !=
6675d522f475Smrg	 newvt->screen.MenuFontName(newvt->screen.menu_font_number)) ||
6676d522f475Smrg	(curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)) {
6677d522f475Smrg	if (curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)
6678d522f475Smrg	    newvt->screen.MenuFontName(fontMenu_default) = newvt->misc.default_font.f_n;
6679d522f475Smrg	if (xtermLoadFont(newvt,
6680d522f475Smrg			  xtermFontName(newvt->screen.MenuFontName(curvt->screen.menu_font_number)),
6681d522f475Smrg			  True, newvt->screen.menu_font_number)) {
6682d522f475Smrg	    /* resizing does the redisplay, so don't ask for it here */
6683d522f475Smrg	    refresh_needed = True;
6684d522f475Smrg	    fonts_redone = True;
6685d522f475Smrg	} else if (curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)
6686d522f475Smrg	    newvt->screen.MenuFontName(fontMenu_default) = curvt->misc.default_font.f_n;
6687d522f475Smrg    }
6688d522f475Smrg    if (!fonts_redone
6689d522f475Smrg	&& (T_COLOR(&(curvt->screen), TEXT_CURSOR) !=
6690d522f475Smrg	    T_COLOR(&(newvt->screen), TEXT_CURSOR))) {
6691d522f475Smrg	set_cursor_gcs(newvt);
6692d522f475Smrg	refresh_needed = True;
6693d522f475Smrg    }
6694d522f475Smrg    if (curvt->misc.re_verse != newvt->misc.re_verse) {
6695d522f475Smrg	newvt->flags ^= REVERSE_VIDEO;
6696d522f475Smrg	ReverseVideo(newvt);
6697d522f475Smrg	newvt->misc.re_verse = !newvt->misc.re_verse;	/* ReverseVideo toggles */
6698d522f475Smrg	refresh_needed = True;
6699d522f475Smrg    }
6700d522f475Smrg    if ((T_COLOR(&(curvt->screen), MOUSE_FG) !=
6701d522f475Smrg	 T_COLOR(&(newvt->screen), MOUSE_FG)) ||
6702d522f475Smrg	(T_COLOR(&(curvt->screen), MOUSE_BG) !=
6703d522f475Smrg	 T_COLOR(&(newvt->screen), MOUSE_BG))) {
6704d522f475Smrg	recolor_cursor(&(newvt->screen),
6705d522f475Smrg		       newvt->screen.pointer_cursor,
6706d522f475Smrg		       T_COLOR(&(newvt->screen), MOUSE_FG),
6707d522f475Smrg		       T_COLOR(&(newvt->screen), MOUSE_BG));
6708d522f475Smrg	refresh_needed = True;
6709d522f475Smrg    }
6710d522f475Smrg    if (curvt->misc.scrollbar != newvt->misc.scrollbar) {
6711d522f475Smrg	ToggleScrollBar(newvt);
6712d522f475Smrg    }
6713d522f475Smrg
6714d522f475Smrg    return refresh_needed;
6715d522f475Smrg}
6716d522f475Smrg
6717d522f475Smrg#define setGC(code) set_at = __LINE__, currentCgs = code
6718d522f475Smrg
6719d522f475Smrg#define OutsideSelection(screen,srow,scol)  \
6720d522f475Smrg	 ((srow) > (screen)->endH.row || \
6721d522f475Smrg	  ((srow) == (screen)->endH.row && \
6722d522f475Smrg	   (scol) >= (screen)->endH.col) || \
6723d522f475Smrg	  (srow) < (screen)->startH.row || \
6724d522f475Smrg	  ((srow) == (screen)->startH.row && \
6725d522f475Smrg	   (scol) < (screen)->startH.col))
6726d522f475Smrg
6727d522f475Smrg/*
6728d522f475Smrg * Shows cursor at new cursor position in screen.
6729d522f475Smrg */
6730d522f475Smrgvoid
6731d522f475SmrgShowCursor(void)
6732d522f475Smrg{
6733d522f475Smrg    XtermWidget xw = term;
6734d522f475Smrg    TScreen *screen = &xw->screen;
6735d522f475Smrg    int x, y;
6736d522f475Smrg    Char clo;
6737d522f475Smrg    unsigned flags;
6738d522f475Smrg    unsigned fg_bg = 0;
6739d522f475Smrg    GC currentGC;
6740d522f475Smrg    CgsEnum currentCgs = gcMAX;
6741d522f475Smrg    VTwin *currentWin = WhichVWin(screen);
6742d522f475Smrg    int set_at;
6743d522f475Smrg    Bool in_selection;
6744d522f475Smrg    Bool reversed;
6745d522f475Smrg    Bool filled;
6746d522f475Smrg    Pixel fg_pix;
6747d522f475Smrg    Pixel bg_pix;
6748d522f475Smrg    Pixel tmp;
6749d522f475Smrg#if OPT_HIGHLIGHT_COLOR
6750d522f475Smrg    Pixel selbg_pix = T_COLOR(screen, HIGHLIGHT_BG);
6751d522f475Smrg    Pixel selfg_pix = T_COLOR(screen, HIGHLIGHT_FG);
6752d522f475Smrg    Boolean use_selbg;
6753d522f475Smrg    Boolean use_selfg;
6754d522f475Smrg#endif
6755d522f475Smrg#if OPT_WIDE_CHARS
6756d522f475Smrg    Char chi = 0;
6757d522f475Smrg    int base;
6758d522f475Smrg    int off;
6759d522f475Smrg    int my_col = 0;
6760d522f475Smrg#endif
6761d522f475Smrg    int cursor_col;
6762d522f475Smrg
6763d522f475Smrg    if (screen->cursor_state == BLINKED_OFF)
6764d522f475Smrg	return;
6765d522f475Smrg
6766d522f475Smrg    if (screen->eventMode != NORMAL)
6767d522f475Smrg	return;
6768d522f475Smrg
6769d522f475Smrg    if (INX2ROW(screen, screen->cur_row) > screen->max_row)
6770d522f475Smrg	return;
6771d522f475Smrg
6772d522f475Smrg    screen->cursorp.row = screen->cur_row;
6773d522f475Smrg    cursor_col = screen->cursorp.col = screen->cur_col;
6774d522f475Smrg    screen->cursor_moved = False;
6775d522f475Smrg
6776d522f475Smrg#ifndef NO_ACTIVE_ICON
6777d522f475Smrg    if (IsIcon(screen)) {
6778d522f475Smrg	screen->cursor_state = ON;
6779d522f475Smrg	return;
6780d522f475Smrg    }
6781d522f475Smrg#endif /* NO_ACTIVE_ICON */
6782d522f475Smrg
6783d522f475Smrg#if OPT_WIDE_CHARS
6784d522f475Smrg    base =
6785d522f475Smrg#endif
6786d522f475Smrg	clo = SCRN_BUF_CHARS(screen, screen->cursorp.row)[cursor_col];
6787d522f475Smrg
6788d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
6789d522f475Smrg	chi = SCRN_BUF_WIDEC(screen, screen->cursorp.row)[cursor_col];
6790d522f475Smrg	if (clo == HIDDEN_LO && chi == HIDDEN_HI && cursor_col > 0) {
6791d522f475Smrg	    /* if cursor points to non-initial part of wide character,
6792d522f475Smrg	     * back it up
6793d522f475Smrg	     */
6794d522f475Smrg	    --cursor_col;
6795d522f475Smrg	    clo = SCRN_BUF_CHARS(screen, screen->cursorp.row)[cursor_col];
6796d522f475Smrg	    chi = SCRN_BUF_WIDEC(screen, screen->cursorp.row)[cursor_col];
6797d522f475Smrg	}
6798d522f475Smrg	my_col = cursor_col;
6799d522f475Smrg	base = (chi << 8) | clo;
6800d522f475Smrg	if (iswide(base))
6801d522f475Smrg	    my_col += 1;
6802d522f475Smrg    });
6803d522f475Smrg
6804d522f475Smrg    flags = SCRN_BUF_ATTRS(screen, screen->cursorp.row)[cursor_col];
6805d522f475Smrg
6806d522f475Smrg    if (clo == 0
6807d522f475Smrg#if OPT_WIDE_CHARS
6808d522f475Smrg	&& chi == 0
6809d522f475Smrg#endif
6810d522f475Smrg	) {
6811d522f475Smrg	clo = ' ';
6812d522f475Smrg    }
6813d522f475Smrg
6814d522f475Smrg    /*
6815d522f475Smrg     * If the cursor happens to be on blanks, and the foreground color is set
6816d522f475Smrg     * but not the background, do not treat it as a colored cell.
6817d522f475Smrg     */
6818d522f475Smrg#if OPT_ISO_COLORS
6819d522f475Smrg    if ((flags & TERM_COLOR_FLAGS(xw)) == BG_COLOR
6820d522f475Smrg#if OPT_WIDE_CHARS
6821d522f475Smrg	&& chi == 0
6822d522f475Smrg#endif
6823d522f475Smrg	&& clo == ' ') {
6824d522f475Smrg	flags &= ~TERM_COLOR_FLAGS(xw);
6825d522f475Smrg    }
6826d522f475Smrg#endif
6827d522f475Smrg
6828d522f475Smrg    /*
6829d522f475Smrg     * Compare the current cell to the last set of colors used for the
6830d522f475Smrg     * cursor and update the GC's if needed.
6831d522f475Smrg     */
6832d522f475Smrg    (void) fg_bg;
6833d522f475Smrg    if_OPT_EXT_COLORS(screen, {
6834d522f475Smrg	fg_bg = (SCRN_BUF_FGRND(screen, screen->cursorp.row)[cursor_col] << 8)
6835d522f475Smrg	    | (SCRN_BUF_BGRND(screen, screen->cursorp.row)[cursor_col]);
6836d522f475Smrg    });
6837d522f475Smrg    if_OPT_ISO_TRADITIONAL_COLORS(screen, {
6838d522f475Smrg	fg_bg = SCRN_BUF_COLOR(screen, screen->cursorp.row)[cursor_col];
6839d522f475Smrg    });
6840d522f475Smrg    fg_pix = getXtermForeground(xw, flags, extract_fg(xw, fg_bg, flags));
6841d522f475Smrg    bg_pix = getXtermBackground(xw, flags, extract_bg(xw, fg_bg, flags));
6842d522f475Smrg
6843d522f475Smrg    if (OutsideSelection(screen, screen->cur_row, screen->cur_col))
6844d522f475Smrg	in_selection = False;
6845d522f475Smrg    else
6846d522f475Smrg	in_selection = True;
6847d522f475Smrg
6848d522f475Smrg    reversed = ReverseOrHilite(screen, flags, in_selection);
6849d522f475Smrg
6850d522f475Smrg    /* This is like updatedXtermGC(), except that we have to worry about
6851d522f475Smrg     * whether the window has focus, since in that case we want just an
6852d522f475Smrg     * outline for the cursor.
6853d522f475Smrg     */
6854d522f475Smrg    filled = (screen->select || screen->always_highlight);
6855d522f475Smrg#if OPT_HIGHLIGHT_COLOR
6856d522f475Smrg    use_selbg = isNotForeground(xw, fg_pix, bg_pix, selbg_pix);
6857d522f475Smrg    use_selfg = isNotBackground(xw, fg_pix, bg_pix, selfg_pix);
6858d522f475Smrg#endif
6859d522f475Smrg    if (filled) {
6860d522f475Smrg	if (reversed) {		/* text is reverse video */
6861d522f475Smrg	    if (getCgsGC(xw, currentWin, gcVTcursNormal)) {
6862d522f475Smrg		setGC(gcVTcursNormal);
6863d522f475Smrg	    } else {
6864d522f475Smrg		if (flags & BOLDATTR(screen)) {
6865d522f475Smrg		    setGC(gcBold);
6866d522f475Smrg		} else {
6867d522f475Smrg		    setGC(gcNorm);
6868d522f475Smrg		}
6869d522f475Smrg	    }
6870d522f475Smrg	    EXCHANGE(fg_pix, bg_pix, tmp);
6871d522f475Smrg#if OPT_HIGHLIGHT_COLOR
6872d522f475Smrg	    if (screen->hilite_reverse) {
6873d522f475Smrg		if (use_selbg && !use_selfg)
6874d522f475Smrg		    fg_pix = bg_pix;
6875d522f475Smrg		if (use_selfg && !use_selbg)
6876d522f475Smrg		    bg_pix = fg_pix;
6877d522f475Smrg		if (use_selbg)
6878d522f475Smrg		    bg_pix = selbg_pix;
6879d522f475Smrg		if (use_selfg)
6880d522f475Smrg		    fg_pix = selfg_pix;
6881d522f475Smrg	    }
6882d522f475Smrg#endif
6883d522f475Smrg	} else {		/* normal video */
6884d522f475Smrg	    if (getCgsGC(xw, currentWin, gcVTcursReverse)) {
6885d522f475Smrg		setGC(gcVTcursReverse);
6886d522f475Smrg	    } else {
6887d522f475Smrg		if (flags & BOLDATTR(screen)) {
6888d522f475Smrg		    setGC(gcBoldReverse);
6889d522f475Smrg		} else {
6890d522f475Smrg		    setGC(gcNormReverse);
6891d522f475Smrg		}
6892d522f475Smrg	    }
6893d522f475Smrg	}
6894d522f475Smrg	if (T_COLOR(screen, TEXT_CURSOR) == xw->dft_foreground) {
6895d522f475Smrg	    setCgsBack(xw, currentWin, currentCgs, fg_pix);
6896d522f475Smrg	}
6897d522f475Smrg	setCgsFore(xw, currentWin, currentCgs, bg_pix);
6898d522f475Smrg    } else {			/* not selected */
6899d522f475Smrg	if (reversed) {		/* text is reverse video */
6900d522f475Smrg	    EXCHANGE(fg_pix, bg_pix, tmp);
6901d522f475Smrg	    setGC(gcNormReverse);
6902d522f475Smrg	} else {		/* normal video */
6903d522f475Smrg	    setGC(gcNorm);
6904d522f475Smrg	}
6905d522f475Smrg#if OPT_HIGHLIGHT_COLOR
6906d522f475Smrg	if (screen->hilite_reverse) {
6907d522f475Smrg	    if (in_selection && !reversed) {
6908d522f475Smrg		;		/* really INVERSE ... */
6909d522f475Smrg	    } else if (in_selection || reversed) {
6910d522f475Smrg		if (use_selbg) {
6911d522f475Smrg		    if (use_selfg) {
6912d522f475Smrg			bg_pix = fg_pix;
6913d522f475Smrg		    } else {
6914d522f475Smrg			fg_pix = bg_pix;
6915d522f475Smrg		    }
6916d522f475Smrg		}
6917d522f475Smrg		if (use_selbg) {
6918d522f475Smrg		    bg_pix = selbg_pix;
6919d522f475Smrg		}
6920d522f475Smrg		if (use_selfg) {
6921d522f475Smrg		    fg_pix = selfg_pix;
6922d522f475Smrg		}
6923d522f475Smrg	    }
6924d522f475Smrg	} else {
6925d522f475Smrg	    if (in_selection) {
6926d522f475Smrg		if (use_selbg) {
6927d522f475Smrg		    bg_pix = selbg_pix;
6928d522f475Smrg		}
6929d522f475Smrg		if (use_selfg) {
6930d522f475Smrg		    fg_pix = selfg_pix;
6931d522f475Smrg		}
6932d522f475Smrg	    }
6933d522f475Smrg	}
6934d522f475Smrg#endif
6935d522f475Smrg	setCgsFore(xw, currentWin, currentCgs, fg_pix);
6936d522f475Smrg	setCgsBack(xw, currentWin, currentCgs, bg_pix);
6937d522f475Smrg    }
6938d522f475Smrg
6939d522f475Smrg    if (screen->cursor_busy == 0
6940d522f475Smrg	&& (screen->cursor_state != ON || screen->cursor_GC != set_at)) {
6941d522f475Smrg
6942d522f475Smrg	screen->cursor_GC = set_at;
6943d522f475Smrg	TRACE(("ShowCursor calling drawXtermText cur(%d,%d) %s\n",
6944d522f475Smrg	       screen->cur_row, screen->cur_col,
6945d522f475Smrg	       (filled ? "filled" : "outline")));
6946d522f475Smrg
6947d522f475Smrg	currentGC = getCgsGC(xw, currentWin, currentCgs);
6948d522f475Smrg	drawXtermText(xw, flags & DRAWX_MASK, currentGC,
6949d522f475Smrg		      x = CurCursorX(screen, screen->cur_row, cursor_col),
6950d522f475Smrg		      y = CursorY(screen, screen->cur_row),
6951d522f475Smrg		      curXtermChrSet(xw, screen->cur_row),
6952d522f475Smrg		      PAIRED_CHARS(&clo, &chi), 1, 0);
6953d522f475Smrg
6954d522f475Smrg#if OPT_WIDE_CHARS
6955d522f475Smrg	if_OPT_WIDE_CHARS(screen, {
6956d522f475Smrg	    for (off = OFF_FINAL; off < MAX_PTRS; off += 2) {
6957d522f475Smrg		clo = SCREEN_PTR(screen, screen->cursorp.row, off + 0)[my_col];
6958d522f475Smrg		chi = SCREEN_PTR(screen, screen->cursorp.row, off + 1)[my_col];
6959d522f475Smrg		if (!(clo || chi))
6960d522f475Smrg		    break;
6961d522f475Smrg		drawXtermText(xw, (flags & DRAWX_MASK) | NOBACKGROUND,
6962d522f475Smrg			      currentGC, x, y,
6963d522f475Smrg			      curXtermChrSet(xw, screen->cur_row),
6964d522f475Smrg			      PAIRED_CHARS(&clo, &chi), 1, iswide(base));
6965d522f475Smrg	    }
6966d522f475Smrg	});
6967d522f475Smrg#endif
6968d522f475Smrg
6969d522f475Smrg	if (!filled) {
6970d522f475Smrg	    GC outlineGC = getCgsGC(xw, currentWin, gcVTcursOutline);
6971d522f475Smrg	    if (outlineGC == 0)
6972d522f475Smrg		outlineGC = currentGC;
6973d522f475Smrg
6974d522f475Smrg	    screen->box->x = x;
6975d522f475Smrg	    screen->box->y = y;
6976d522f475Smrg	    XDrawLines(screen->display, VWindow(screen), outlineGC,
6977d522f475Smrg		       screen->box, NBOX, CoordModePrevious);
6978d522f475Smrg	}
6979d522f475Smrg    }
6980d522f475Smrg    screen->cursor_state = ON;
6981d522f475Smrg}
6982d522f475Smrg
6983d522f475Smrg/*
6984d522f475Smrg * hide cursor at previous cursor position in screen.
6985d522f475Smrg */
6986d522f475Smrgvoid
6987d522f475SmrgHideCursor(void)
6988d522f475Smrg{
6989d522f475Smrg    XtermWidget xw = term;
6990d522f475Smrg    TScreen *screen = &xw->screen;
6991d522f475Smrg    GC currentGC;
6992d522f475Smrg    unsigned flags;
6993d522f475Smrg    unsigned fg_bg = 0;
6994d522f475Smrg    int x, y;
6995d522f475Smrg    Char clo;
6996d522f475Smrg    Bool in_selection;
6997d522f475Smrg#if OPT_WIDE_CHARS
6998d522f475Smrg    Char chi = 0;
6999d522f475Smrg    int base;
7000d522f475Smrg    int off;
7001d522f475Smrg    int my_col = 0;
7002d522f475Smrg#endif
7003d522f475Smrg    int cursor_col;
7004d522f475Smrg
7005d522f475Smrg    if (screen->cursor_state == OFF)	/* FIXME */
7006d522f475Smrg	return;
7007d522f475Smrg    if (INX2ROW(screen, screen->cursorp.row) > screen->max_row)
7008d522f475Smrg	return;
7009d522f475Smrg
7010d522f475Smrg    cursor_col = screen->cursorp.col;
7011d522f475Smrg
7012d522f475Smrg#ifndef NO_ACTIVE_ICON
7013d522f475Smrg    if (IsIcon(screen)) {
7014d522f475Smrg	screen->cursor_state = OFF;
7015d522f475Smrg	return;
7016d522f475Smrg    }
7017d522f475Smrg#endif /* NO_ACTIVE_ICON */
7018d522f475Smrg
7019d522f475Smrg#if OPT_WIDE_CHARS
7020d522f475Smrg    base =
7021d522f475Smrg#endif
7022d522f475Smrg	clo = SCRN_BUF_CHARS(screen, screen->cursorp.row)[cursor_col];
7023d522f475Smrg    flags = SCRN_BUF_ATTRS(screen, screen->cursorp.row)[cursor_col];
7024d522f475Smrg
7025d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
7026d522f475Smrg	chi = SCRN_BUF_WIDEC(screen, screen->cursorp.row)[cursor_col];
7027d522f475Smrg	if (clo == HIDDEN_LO && chi == HIDDEN_HI) {
7028d522f475Smrg	    /* if cursor points to non-initial part of wide character,
7029d522f475Smrg	     * back it up
7030d522f475Smrg	     */
7031d522f475Smrg	    --cursor_col;
7032d522f475Smrg	    clo = SCRN_BUF_CHARS(screen, screen->cursorp.row)[cursor_col];
7033d522f475Smrg	    chi = SCRN_BUF_WIDEC(screen, screen->cursorp.row)[cursor_col];
7034d522f475Smrg	}
7035d522f475Smrg	my_col = cursor_col;
7036d522f475Smrg	base = (chi << 8) | clo;
7037d522f475Smrg	if (iswide(base))
7038d522f475Smrg	    my_col += 1;
7039d522f475Smrg    });
7040d522f475Smrg
7041d522f475Smrg    if_OPT_EXT_COLORS(screen, {
7042d522f475Smrg	fg_bg = (SCRN_BUF_FGRND(screen, screen->cursorp.row)[cursor_col] << 8)
7043d522f475Smrg	    | (SCRN_BUF_BGRND(screen, screen->cursorp.row)[cursor_col]);
7044d522f475Smrg    });
7045d522f475Smrg    if_OPT_ISO_TRADITIONAL_COLORS(screen, {
7046d522f475Smrg	fg_bg = SCRN_BUF_COLOR(screen, screen->cursorp.row)[cursor_col];
7047d522f475Smrg    });
7048d522f475Smrg
7049d522f475Smrg    if (OutsideSelection(screen, screen->cursorp.row, screen->cursorp.col))
7050d522f475Smrg	in_selection = False;
7051d522f475Smrg    else
7052d522f475Smrg	in_selection = True;
7053d522f475Smrg
7054d522f475Smrg    currentGC = updatedXtermGC(xw, flags, fg_bg, in_selection);
7055d522f475Smrg
7056d522f475Smrg    if (clo == 0
7057d522f475Smrg#if OPT_WIDE_CHARS
7058d522f475Smrg	&& chi == 0
7059d522f475Smrg#endif
7060d522f475Smrg	) {
7061d522f475Smrg	clo = ' ';
7062d522f475Smrg    }
7063d522f475Smrg
7064d522f475Smrg    TRACE(("HideCursor calling drawXtermText cur(%d,%d)\n",
7065d522f475Smrg	   screen->cursorp.row, screen->cursorp.col));
7066d522f475Smrg    drawXtermText(xw, flags & DRAWX_MASK, currentGC,
7067d522f475Smrg		  x = CurCursorX(screen, screen->cursorp.row, cursor_col),
7068d522f475Smrg		  y = CursorY(screen, screen->cursorp.row),
7069d522f475Smrg		  curXtermChrSet(xw, screen->cursorp.row),
7070d522f475Smrg		  PAIRED_CHARS(&clo, &chi), 1, 0);
7071d522f475Smrg
7072d522f475Smrg#if OPT_WIDE_CHARS
7073d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
7074d522f475Smrg	for (off = OFF_FINAL; off < MAX_PTRS; off += 2) {
7075d522f475Smrg	    clo = SCREEN_PTR(screen, screen->cursorp.row, off + 0)[my_col];
7076d522f475Smrg	    chi = SCREEN_PTR(screen, screen->cursorp.row, off + 1)[my_col];
7077d522f475Smrg	    if (!(clo || chi))
7078d522f475Smrg		break;
7079d522f475Smrg	    drawXtermText(xw, (flags & DRAWX_MASK) | NOBACKGROUND,
7080d522f475Smrg			  currentGC, x, y,
7081d522f475Smrg			  curXtermChrSet(xw, screen->cur_row),
7082d522f475Smrg			  PAIRED_CHARS(&clo, &chi), 1, iswide(base));
7083d522f475Smrg	}
7084d522f475Smrg    });
7085d522f475Smrg#endif
7086d522f475Smrg    screen->cursor_state = OFF;
7087d522f475Smrg    resetXtermGC(xw, flags, in_selection);
7088d522f475Smrg}
7089d522f475Smrg
7090d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
7091d522f475Smrgstatic void
7092d522f475SmrgStartBlinking(TScreen * screen)
7093d522f475Smrg{
7094d522f475Smrg    if (screen->blink_timer == 0) {
7095d522f475Smrg	unsigned long interval = (screen->cursor_state == ON ?
7096d522f475Smrg				  screen->blink_on : screen->blink_off);
7097d522f475Smrg	if (interval == 0)	/* wow! */
7098d522f475Smrg	    interval = 1;	/* let's humor him anyway */
7099d522f475Smrg	screen->blink_timer = XtAppAddTimeOut(app_con,
7100d522f475Smrg					      interval,
7101d522f475Smrg					      HandleBlinking,
7102d522f475Smrg					      screen);
7103d522f475Smrg    }
7104d522f475Smrg}
7105d522f475Smrg
7106d522f475Smrgstatic void
7107d522f475SmrgStopBlinking(TScreen * screen)
7108d522f475Smrg{
7109d522f475Smrg    if (screen->blink_timer)
7110d522f475Smrg	XtRemoveTimeOut(screen->blink_timer);
7111d522f475Smrg    screen->blink_timer = 0;
7112d522f475Smrg}
7113d522f475Smrg
7114d522f475Smrg#if OPT_BLINK_TEXT
7115d522f475Smrgstatic Bool
7116d522f475SmrgScrnHasBlinking(TScreen * screen, int row)
7117d522f475Smrg{
7118d522f475Smrg    Char *attrs = SCRN_BUF_ATTRS(screen, row);
7119d522f475Smrg    int col;
7120d522f475Smrg    Bool result = False;
7121d522f475Smrg
7122d522f475Smrg    for (col = 0; col < MaxCols(screen); ++col) {
7123d522f475Smrg	if (attrs[col] & BLINK) {
7124d522f475Smrg	    result = True;
7125d522f475Smrg	    break;
7126d522f475Smrg	}
7127d522f475Smrg    }
7128d522f475Smrg    return result;
7129d522f475Smrg}
7130d522f475Smrg#endif
7131d522f475Smrg
7132d522f475Smrg/*
7133d522f475Smrg * Blink the cursor by alternately showing/hiding cursor.  We leave the timer
7134d522f475Smrg * running all the time (even though that's a little inefficient) to make the
7135d522f475Smrg * logic simple.
7136d522f475Smrg */
7137d522f475Smrgstatic void
7138d522f475SmrgHandleBlinking(XtPointer closure, XtIntervalId * id GCC_UNUSED)
7139d522f475Smrg{
7140d522f475Smrg    TScreen *screen = (TScreen *) closure;
7141d522f475Smrg    Bool resume = False;
7142d522f475Smrg
7143d522f475Smrg    screen->blink_timer = 0;
7144d522f475Smrg    screen->blink_state = !screen->blink_state;
7145d522f475Smrg
7146d522f475Smrg#if OPT_BLINK_CURS
7147d522f475Smrg    if (DoStartBlinking(screen)) {
7148d522f475Smrg	if (screen->cursor_state == ON) {
7149d522f475Smrg	    if (screen->select || screen->always_highlight) {
7150d522f475Smrg		HideCursor();
7151d522f475Smrg		if (screen->cursor_state == OFF)
7152d522f475Smrg		    screen->cursor_state = BLINKED_OFF;
7153d522f475Smrg	    }
7154d522f475Smrg	} else if (screen->cursor_state == BLINKED_OFF) {
7155d522f475Smrg	    screen->cursor_state = OFF;
7156d522f475Smrg	    ShowCursor();
7157d522f475Smrg	    if (screen->cursor_state == OFF)
7158d522f475Smrg		screen->cursor_state = BLINKED_OFF;
7159d522f475Smrg	}
7160d522f475Smrg	resume = True;
7161d522f475Smrg    }
7162d522f475Smrg#endif
7163d522f475Smrg
7164d522f475Smrg#if OPT_BLINK_TEXT
7165d522f475Smrg    /*
7166d522f475Smrg     * Inspect the line on the current screen to see if any have the BLINK flag
7167d522f475Smrg     * associated with them.  Prune off any that have had the corresponding
7168d522f475Smrg     * cells reset.  If any are left, repaint those lines with ScrnRefresh().
7169d522f475Smrg     */
7170d522f475Smrg    if (!(screen->blink_as_bold)) {
7171d522f475Smrg	int row;
7172d522f475Smrg	int first_row = screen->max_row;
7173d522f475Smrg	int last_row = -1;
7174d522f475Smrg
7175d522f475Smrg	for (row = screen->max_row; row >= 0; row--) {
7176d522f475Smrg	    if (ScrnTstBlinked(screen, row)) {
7177d522f475Smrg		if (ScrnHasBlinking(screen, row)) {
7178d522f475Smrg		    resume = True;
7179d522f475Smrg		    if (row > last_row)
7180d522f475Smrg			last_row = row;
7181d522f475Smrg		    if (row < first_row)
7182d522f475Smrg			first_row = row;
7183d522f475Smrg		} else {
7184d522f475Smrg		    ScrnClrBlinked(screen, row);
7185d522f475Smrg		}
7186d522f475Smrg	    }
7187d522f475Smrg	}
7188d522f475Smrg	/*
7189d522f475Smrg	 * FIXME: this could be a little more efficient, e.g,. by limiting the
7190d522f475Smrg	 * columns which are updated.
7191d522f475Smrg	 */
7192d522f475Smrg	if (first_row <= last_row) {
7193d522f475Smrg	    ScrnRefresh(term,
7194d522f475Smrg			first_row,
7195d522f475Smrg			0,
7196d522f475Smrg			last_row + 1 - first_row,
7197d522f475Smrg			MaxCols(screen),
7198d522f475Smrg			True);
7199d522f475Smrg	}
7200d522f475Smrg    }
7201d522f475Smrg#endif
7202d522f475Smrg
7203d522f475Smrg    /*
7204d522f475Smrg     * If either the cursor or text is blinking, restart the timer.
7205d522f475Smrg     */
7206d522f475Smrg    if (resume)
7207d522f475Smrg	StartBlinking(screen);
7208d522f475Smrg}
7209d522f475Smrg#endif /* OPT_BLINK_CURS || OPT_BLINK_TEXT */
7210d522f475Smrg
7211d522f475Smrg/*
7212d522f475Smrg * Implement soft or hard (full) reset of the VTxxx emulation.  There are a
7213d522f475Smrg * couple of differences from real DEC VTxxx terminals (to avoid breaking
7214d522f475Smrg * applications which have come to rely on xterm doing this):
7215d522f475Smrg *
7216d522f475Smrg *	+ autowrap mode should be reset (instead it's reset to the resource
7217d522f475Smrg *	  default).
7218d522f475Smrg *	+ the popup menu offers a choice of resetting the savedLines, or not.
7219d522f475Smrg *	  (but the control sequence does this anyway).
7220d522f475Smrg */
7221d522f475Smrgvoid
7222d522f475SmrgVTReset(XtermWidget xw, Bool full, Bool saved)
7223d522f475Smrg{
7224d522f475Smrg    TScreen *screen = &xw->screen;
7225d522f475Smrg
7226d522f475Smrg    if (!XtIsRealized((Widget) xw) || (CURRENT_EMU() != (Widget) xw)) {
7227d522f475Smrg	Bell(XkbBI_MinorError, 0);
7228d522f475Smrg	return;
7229d522f475Smrg    }
7230d522f475Smrg
7231d522f475Smrg    if (saved) {
7232d522f475Smrg	screen->savedlines = 0;
7233d522f475Smrg	ScrollBarDrawThumb(screen->scrollWidget);
7234d522f475Smrg    }
7235d522f475Smrg
7236d522f475Smrg    /* make cursor visible */
7237d522f475Smrg    screen->cursor_set = ON;
7238d522f475Smrg
7239d522f475Smrg    /* reset scrolling region */
7240d522f475Smrg    set_tb_margins(screen, 0, screen->max_row);
7241d522f475Smrg
7242d522f475Smrg    bitclr(&xw->flags, ORIGIN);
7243d522f475Smrg
7244d522f475Smrg    if_OPT_ISO_COLORS(screen, {
7245d522f475Smrg	reset_SGR_Colors(xw);
7246d522f475Smrg    });
7247d522f475Smrg
7248d522f475Smrg    /* Reset character-sets to initial state */
7249d522f475Smrg    resetCharsets(screen);
7250d522f475Smrg
7251d522f475Smrg#if OPT_MOD_FKEYS
7252d522f475Smrg    /* Reset modifier-resources to initial state */
7253d522f475Smrg    xw->keyboard.modify_now = xw->keyboard.modify_1st;
7254d522f475Smrg#endif
7255d522f475Smrg
7256d522f475Smrg    /* Reset DECSCA */
7257d522f475Smrg    bitclr(&xw->flags, PROTECTED);
7258d522f475Smrg    screen->protected_mode = OFF_PROTECT;
7259d522f475Smrg
7260d522f475Smrg    if (full) {			/* RIS */
7261d522f475Smrg	if (screen->bellOnReset)
7262d522f475Smrg	    Bell(XkbBI_TerminalBell, 0);
7263d522f475Smrg
7264d522f475Smrg	/* reset the mouse mode */
7265d522f475Smrg	screen->send_mouse_pos = MOUSE_OFF;
7266d522f475Smrg	screen->send_focus_pos = OFF;
7267d522f475Smrg	screen->waitingForTrackInfo = False;
7268d522f475Smrg	screen->eventMode = NORMAL;
7269d522f475Smrg
7270d522f475Smrg	xtermShowPointer(xw, True);
7271d522f475Smrg
7272d522f475Smrg	TabReset(xw->tabs);
7273d522f475Smrg	xw->keyboard.flags = MODE_SRM;
7274d522f475Smrg#if OPT_INITIAL_ERASE
7275d522f475Smrg	if (xw->keyboard.reset_DECBKM == 1)
7276d522f475Smrg	    xw->keyboard.flags |= MODE_DECBKM;
7277d522f475Smrg	else if (xw->keyboard.reset_DECBKM == 2)
7278d522f475Smrg#endif
7279d522f475Smrg	    if (xw->screen.backarrow_key)
7280d522f475Smrg		xw->keyboard.flags |= MODE_DECBKM;
7281d522f475Smrg	TRACE(("full reset DECBKM %s\n",
7282d522f475Smrg	       BtoS(xw->keyboard.flags & MODE_DECBKM)));
7283d522f475Smrg	update_appcursor();
7284d522f475Smrg	update_appkeypad();
7285d522f475Smrg	update_decbkm();
7286d522f475Smrg	show_8bit_control(False);
7287d522f475Smrg	reset_decudk();
7288d522f475Smrg
7289d522f475Smrg	FromAlternate(xw);
7290d522f475Smrg	ClearScreen(xw);
7291d522f475Smrg	screen->cursor_state = OFF;
7292d522f475Smrg	if (xw->flags & REVERSE_VIDEO)
7293d522f475Smrg	    ReverseVideo(xw);
7294d522f475Smrg
7295d522f475Smrg	xw->flags = xw->initflags;
7296d522f475Smrg	update_reversevideo();
7297d522f475Smrg	update_autowrap();
7298d522f475Smrg	update_reversewrap();
7299d522f475Smrg	update_autolinefeed();
7300d522f475Smrg
7301d522f475Smrg	screen->jumpscroll = !(xw->flags & SMOOTHSCROLL);
7302d522f475Smrg	update_jumpscroll();
7303d522f475Smrg
7304d522f475Smrg	if (screen->c132 && (xw->flags & IN132COLUMNS)) {
7305d522f475Smrg	    Dimension reqWidth = (80 * FontWidth(screen)
7306d522f475Smrg				  + 2 * screen->border + ScrollbarWidth(screen));
7307d522f475Smrg	    Dimension reqHeight = (FontHeight(screen)
7308d522f475Smrg				   * MaxRows(screen) + 2 * screen->border);
7309d522f475Smrg	    Dimension replyWidth;
7310d522f475Smrg	    Dimension replyHeight;
7311d522f475Smrg
7312d522f475Smrg	    TRACE(("Making resize-request to restore 80-columns %dx%d\n",
7313d522f475Smrg		   reqHeight, reqWidth));
7314d522f475Smrg	    XtMakeResizeRequest((Widget) xw,
7315d522f475Smrg				reqWidth,
7316d522f475Smrg				reqHeight,
7317d522f475Smrg				&replyWidth, &replyHeight);
7318d522f475Smrg	    TRACE(("...result %dx%d\n", replyHeight, replyWidth));
7319d522f475Smrg	    repairSizeHints();
7320d522f475Smrg	    XSync(screen->display, False);	/* synchronize */
7321d522f475Smrg	    if (XtAppPending(app_con))
7322d522f475Smrg		xevents();
7323d522f475Smrg	}
7324d522f475Smrg
7325d522f475Smrg	CursorSet(screen, 0, 0, xw->flags);
7326d522f475Smrg	CursorSave(xw);
7327d522f475Smrg    } else {			/* DECSTR */
7328d522f475Smrg	/*
7329d522f475Smrg	 * There's a tiny difference, to accommodate usage of xterm.
7330d522f475Smrg	 * We reset autowrap to the resource values rather than turning
7331d522f475Smrg	 * it off.
7332d522f475Smrg	 */
7333d522f475Smrg	xw->keyboard.flags &= ~(MODE_DECCKM | MODE_KAM | MODE_DECKPAM);
7334d522f475Smrg	bitcpy(&xw->flags, xw->initflags, WRAPAROUND | REVERSEWRAP);
7335d522f475Smrg	bitclr(&xw->flags, INSERT | INVERSE | BOLD | BLINK | UNDERLINE | INVISIBLE);
7336d522f475Smrg	if_OPT_ISO_COLORS(screen, {
7337d522f475Smrg	    reset_SGR_Colors(xw);
7338d522f475Smrg	});
7339d522f475Smrg	update_appcursor();
7340d522f475Smrg	update_autowrap();
7341d522f475Smrg	update_reversewrap();
7342d522f475Smrg
7343d522f475Smrg	CursorSave(xw);
7344d522f475Smrg	screen->sc[screen->alternate != False].row =
7345d522f475Smrg	    screen->sc[screen->alternate != False].col = 0;
7346d522f475Smrg    }
7347d522f475Smrg    longjmp(vtjmpbuf, 1);	/* force ground state in parser */
7348d522f475Smrg}
7349d522f475Smrg
7350d522f475Smrg/*
7351d522f475Smrg * set_character_class - takes a string of the form
7352d522f475Smrg *
7353d522f475Smrg *   low[-high]:val[,low[-high]:val[...]]
7354d522f475Smrg *
7355d522f475Smrg * and sets the indicated ranges to the indicated values.
7356d522f475Smrg */
7357d522f475Smrgstatic int
7358d522f475Smrgset_character_class(char *s)
7359d522f475Smrg{
7360d522f475Smrg    int i;			/* iterator, index into s */
7361d522f475Smrg    int len;			/* length of s */
7362d522f475Smrg    int acc;			/* accumulator */
7363d522f475Smrg    int low, high;		/* bounds of range [0..127] */
7364d522f475Smrg    int base;			/* 8, 10, 16 (octal, decimal, hex) */
7365d522f475Smrg    int numbers;		/* count of numbers per range */
7366d522f475Smrg    int digits;			/* count of digits in a number */
7367d522f475Smrg    static char *errfmt = "%s:  %s in range string \"%s\" (position %d)\n";
7368d522f475Smrg
7369d522f475Smrg    if (!s || !s[0])
7370d522f475Smrg	return -1;
7371d522f475Smrg
7372d522f475Smrg    base = 10;			/* in case we ever add octal, hex */
7373d522f475Smrg    low = high = -1;		/* out of range */
7374d522f475Smrg
7375d522f475Smrg    for (i = 0, len = strlen(s), acc = 0, numbers = digits = 0;
7376d522f475Smrg	 i < len; i++) {
7377d522f475Smrg	Char c = s[i];
7378d522f475Smrg
7379d522f475Smrg	if (isspace(c)) {
7380d522f475Smrg	    continue;
7381d522f475Smrg	} else if (isdigit(c)) {
7382d522f475Smrg	    acc = acc * base + (c - '0');
7383d522f475Smrg	    digits++;
7384d522f475Smrg	    continue;
7385d522f475Smrg	} else if (c == '-') {
7386d522f475Smrg	    low = acc;
7387d522f475Smrg	    acc = 0;
7388d522f475Smrg	    if (digits == 0) {
7389d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "missing number", s, i);
7390d522f475Smrg		return (-1);
7391d522f475Smrg	    }
7392d522f475Smrg	    digits = 0;
7393d522f475Smrg	    numbers++;
7394d522f475Smrg	    continue;
7395d522f475Smrg	} else if (c == ':') {
7396d522f475Smrg	    if (numbers == 0)
7397d522f475Smrg		low = acc;
7398d522f475Smrg	    else if (numbers == 1)
7399d522f475Smrg		high = acc;
7400d522f475Smrg	    else {
7401d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "too many numbers",
7402d522f475Smrg			s, i);
7403d522f475Smrg		return (-1);
7404d522f475Smrg	    }
7405d522f475Smrg	    digits = 0;
7406d522f475Smrg	    numbers++;
7407d522f475Smrg	    acc = 0;
7408d522f475Smrg	    continue;
7409d522f475Smrg	} else if (c == ',') {
7410d522f475Smrg	    /*
7411d522f475Smrg	     * now, process it
7412d522f475Smrg	     */
7413d522f475Smrg
7414d522f475Smrg	    if (high < 0) {
7415d522f475Smrg		high = low;
7416d522f475Smrg		numbers++;
7417d522f475Smrg	    }
7418d522f475Smrg	    if (numbers != 2) {
7419d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "bad value number",
7420d522f475Smrg			s, i);
7421d522f475Smrg	    } else if (SetCharacterClassRange(low, high, acc) != 0) {
7422d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "bad range", s, i);
7423d522f475Smrg	    }
7424d522f475Smrg
7425d522f475Smrg	    low = high = -1;
7426d522f475Smrg	    acc = 0;
7427d522f475Smrg	    digits = 0;
7428d522f475Smrg	    numbers = 0;
7429d522f475Smrg	    continue;
7430d522f475Smrg	} else {
7431d522f475Smrg	    fprintf(stderr, errfmt, ProgramName, "bad character", s, i);
7432d522f475Smrg	    return (-1);
7433d522f475Smrg	}			/* end if else if ... else */
7434d522f475Smrg
7435d522f475Smrg    }
7436d522f475Smrg
7437d522f475Smrg    if (low < 0 && high < 0)
7438d522f475Smrg	return (0);
7439d522f475Smrg
7440d522f475Smrg    /*
7441d522f475Smrg     * now, process it
7442d522f475Smrg     */
7443d522f475Smrg
7444d522f475Smrg    if (high < 0)
7445d522f475Smrg	high = low;
7446d522f475Smrg    if (numbers < 1 || numbers > 2) {
7447d522f475Smrg	fprintf(stderr, errfmt, ProgramName, "bad value number", s, i);
7448d522f475Smrg    } else if (SetCharacterClassRange(low, high, acc) != 0) {
7449d522f475Smrg	fprintf(stderr, errfmt, ProgramName, "bad range", s, i);
7450d522f475Smrg    }
7451d522f475Smrg
7452d522f475Smrg    return (0);
7453d522f475Smrg}
7454d522f475Smrg
7455d522f475Smrg/* ARGSUSED */
7456d522f475Smrgstatic void
7457d522f475SmrgHandleKeymapChange(Widget w,
7458d522f475Smrg		   XEvent * event GCC_UNUSED,
7459d522f475Smrg		   String * params,
7460d522f475Smrg		   Cardinal *param_count)
7461d522f475Smrg{
7462d522f475Smrg    static XtTranslations keymap, original;
7463d522f475Smrg    static XtResource key_resources[] =
7464d522f475Smrg    {
7465d522f475Smrg	{XtNtranslations, XtCTranslations, XtRTranslationTable,
7466d522f475Smrg	 sizeof(XtTranslations), 0, XtRTranslationTable, (XtPointer) NULL}
7467d522f475Smrg    };
7468d522f475Smrg    char mapName[1000];
7469d522f475Smrg    char mapClass[1000];
7470d522f475Smrg    char *pmapName;
7471d522f475Smrg    char *pmapClass;
7472d522f475Smrg    size_t len;
7473d522f475Smrg
7474d522f475Smrg    if (*param_count != 1)
7475d522f475Smrg	return;
7476d522f475Smrg
7477d522f475Smrg    if (original == NULL)
7478d522f475Smrg	original = w->core.tm.translations;
7479d522f475Smrg
7480d522f475Smrg    if (strcmp(params[0], "None") == 0) {
7481d522f475Smrg	XtOverrideTranslations(w, original);
7482d522f475Smrg	return;
7483d522f475Smrg    }
7484d522f475Smrg
7485d522f475Smrg    len = strlen(params[0]) + 7;
7486d522f475Smrg
7487d522f475Smrg    pmapName = (char *) MyStackAlloc(len, mapName);
7488d522f475Smrg    pmapClass = (char *) MyStackAlloc(len, mapClass);
7489d522f475Smrg    if (pmapName == NULL
7490d522f475Smrg	|| pmapClass == NULL)
7491d522f475Smrg	SysError(ERROR_KMMALLOC1);
7492d522f475Smrg
7493d522f475Smrg    (void) sprintf(pmapName, "%sKeymap", params[0]);
7494d522f475Smrg    (void) strcpy(pmapClass, pmapName);
7495d522f475Smrg    if (islower(CharOf(pmapClass[0])))
7496d522f475Smrg	pmapClass[0] = toupper(CharOf(pmapClass[0]));
7497d522f475Smrg    XtGetSubresources(w, (XtPointer) &keymap, pmapName, pmapClass,
7498d522f475Smrg		      key_resources, (Cardinal) 1, NULL, (Cardinal) 0);
7499d522f475Smrg    if (keymap != NULL)
7500d522f475Smrg	XtOverrideTranslations(w, keymap);
7501d522f475Smrg
7502d522f475Smrg    MyStackFree(pmapName, mapName);
7503d522f475Smrg    MyStackFree(pmapClass, mapClass);
7504d522f475Smrg}
7505d522f475Smrg
7506d522f475Smrg/* ARGSUSED */
7507d522f475Smrgstatic void
7508d522f475SmrgHandleBell(Widget w GCC_UNUSED,
7509d522f475Smrg	   XEvent * event GCC_UNUSED,
7510d522f475Smrg	   String * params,	/* [0] = volume */
7511d522f475Smrg	   Cardinal *param_count)	/* 0 or 1 */
7512d522f475Smrg{
7513d522f475Smrg    int percent = (*param_count) ? atoi(params[0]) : 0;
7514d522f475Smrg
7515d522f475Smrg    Bell(XkbBI_TerminalBell, percent);
7516d522f475Smrg}
7517d522f475Smrg
7518d522f475Smrg/* ARGSUSED */
7519d522f475Smrgstatic void
7520d522f475SmrgHandleVisualBell(Widget w GCC_UNUSED,
7521d522f475Smrg		 XEvent * event GCC_UNUSED,
7522d522f475Smrg		 String * params GCC_UNUSED,
7523d522f475Smrg		 Cardinal *param_count GCC_UNUSED)
7524d522f475Smrg{
7525d522f475Smrg    VisualBell();
7526d522f475Smrg}
7527d522f475Smrg
7528d522f475Smrg/* ARGSUSED */
7529d522f475Smrgstatic void
7530d522f475SmrgHandleIgnore(Widget w,
7531d522f475Smrg	     XEvent * event,
7532d522f475Smrg	     String * params GCC_UNUSED,
7533d522f475Smrg	     Cardinal *param_count GCC_UNUSED)
7534d522f475Smrg{
7535d522f475Smrg    if (IsXtermWidget(w)) {
7536d522f475Smrg	/* do nothing, but check for funny escape sequences */
7537d522f475Smrg	(void) SendMousePosition((XtermWidget) w, event);
7538d522f475Smrg    }
7539d522f475Smrg}
7540d522f475Smrg
7541d522f475Smrg/* ARGSUSED */
7542d522f475Smrgstatic void
7543d522f475SmrgDoSetSelectedFont(Widget w,
7544d522f475Smrg		  XtPointer client_data GCC_UNUSED,
7545d522f475Smrg		  Atom * selection GCC_UNUSED,
7546d522f475Smrg		  Atom * type,
7547d522f475Smrg		  XtPointer value,
7548d522f475Smrg		  unsigned long *length,
7549d522f475Smrg		  int *format)
7550d522f475Smrg{
7551d522f475Smrg    if (!IsXtermWidget(w) || *type != XA_STRING || *format != 8) {
7552d522f475Smrg	Bell(XkbBI_MinorError, 0);
7553d522f475Smrg    } else {
7554d522f475Smrg	Boolean failed = False;
7555d522f475Smrg	XtermWidget xw = (XtermWidget) w;
7556d522f475Smrg	int oldFont = xw->screen.menu_font_number;
7557d522f475Smrg	char *save = xw->screen.MenuFontName(fontMenu_fontsel);
7558d522f475Smrg	char *val;
7559d522f475Smrg	char *test = 0;
7560d522f475Smrg	char *used = 0;
7561d522f475Smrg	unsigned len = *length;
7562d522f475Smrg	unsigned tst;
7563d522f475Smrg
7564d522f475Smrg	/*
7565d522f475Smrg	 * Some versions of X deliver null-terminated selections, some do not.
7566d522f475Smrg	 */
7567d522f475Smrg	for (tst = 0; tst < len; ++tst) {
7568d522f475Smrg	    if (((char *) value)[tst] == '\0') {
7569d522f475Smrg		len = tst;
7570d522f475Smrg		break;
7571d522f475Smrg	    }
7572d522f475Smrg	}
7573d522f475Smrg
7574d522f475Smrg	if (len > 0 && (val = TypeMallocN(char, len + 1)) != 0) {
7575d522f475Smrg	    memcpy(val, value, len);
7576d522f475Smrg	    val[len] = '\0';
7577d522f475Smrg	    used = x_strtrim(val);
7578d522f475Smrg	    TRACE(("DoSetSelectedFont(%s)\n", val));
7579d522f475Smrg	    /* Do some sanity checking to avoid sending a long selection
7580d522f475Smrg	       back to the server in an OpenFont that is unlikely to succeed.
7581d522f475Smrg	       XLFD allows up to 255 characters and no control characters;
7582d522f475Smrg	       we are a little more liberal here. */
7583d522f475Smrg	    if (len < 1000
7584d522f475Smrg		&& !strchr(val, '\n')
7585d522f475Smrg		&& (test = x_strdup(val)) != 0) {
7586d522f475Smrg		xw->screen.MenuFontName(fontMenu_fontsel) = test;
7587d522f475Smrg		if (!xtermLoadFont(term,
7588d522f475Smrg				   xtermFontName(val),
7589d522f475Smrg				   True,
7590d522f475Smrg				   fontMenu_fontsel)) {
7591d522f475Smrg		    failed = True;
7592d522f475Smrg		    free(test);
7593d522f475Smrg		    xw->screen.MenuFontName(fontMenu_fontsel) = save;
7594d522f475Smrg		}
7595d522f475Smrg	    } else {
7596d522f475Smrg		failed = True;
7597d522f475Smrg	    }
7598d522f475Smrg	    if (failed) {
7599d522f475Smrg		(void) xtermLoadFont(term,
7600d522f475Smrg				     xtermFontName(xw->screen.MenuFontName(oldFont)),
7601d522f475Smrg				     True,
7602d522f475Smrg				     oldFont);
7603d522f475Smrg		Bell(XkbBI_MinorError, 0);
7604d522f475Smrg	    }
7605d522f475Smrg	    if (used != val)
7606d522f475Smrg		free(used);
7607d522f475Smrg	    free(val);
7608d522f475Smrg	}
7609d522f475Smrg    }
7610d522f475Smrg}
7611d522f475Smrg
7612d522f475Smrgvoid
7613d522f475SmrgFindFontSelection(XtermWidget xw, const char *atom_name, Bool justprobe)
7614d522f475Smrg{
7615d522f475Smrg    static AtomPtr *atoms;
7616d522f475Smrg    static int atomCount = 0;
7617d522f475Smrg    AtomPtr *pAtom;
7618d522f475Smrg    int a;
7619d522f475Smrg    Atom target;
7620d522f475Smrg
7621d522f475Smrg    if (!atom_name)
7622d522f475Smrg	atom_name = (xw->screen.mappedSelect
7623d522f475Smrg		     ? xw->screen.mappedSelect[0]
7624d522f475Smrg		     : "PRIMARY");
7625d522f475Smrg    TRACE(("FindFontSelection(%s)\n", atom_name));
7626d522f475Smrg
7627d522f475Smrg    for (pAtom = atoms, a = atomCount; a; a--, pAtom++) {
7628d522f475Smrg	if (strcmp(atom_name, XmuNameOfAtom(*pAtom)) == 0)
7629d522f475Smrg	    break;
7630d522f475Smrg    }
7631d522f475Smrg    if (!a) {
7632d522f475Smrg	atoms = (AtomPtr *) XtRealloc((char *) atoms,
7633d522f475Smrg				      sizeof(AtomPtr) * (atomCount + 1));
7634d522f475Smrg	*(pAtom = &atoms[atomCount++]) = XmuMakeAtom(atom_name);
7635d522f475Smrg    }
7636d522f475Smrg
7637d522f475Smrg    target = XmuInternAtom(XtDisplay(xw), *pAtom);
7638d522f475Smrg    if (justprobe) {
7639d522f475Smrg	xw->screen.MenuFontName(fontMenu_fontsel) =
7640d522f475Smrg	    XGetSelectionOwner(XtDisplay(xw), target) ? _Font_Selected_ : 0;
7641d522f475Smrg    } else {
7642d522f475Smrg	XtGetSelectionValue((Widget) xw, target, XA_STRING,
7643d522f475Smrg			    DoSetSelectedFont, NULL,
7644d522f475Smrg			    XtLastTimestampProcessed(XtDisplay(xw)));
7645d522f475Smrg    }
7646d522f475Smrg    return;
7647d522f475Smrg}
7648d522f475Smrg
7649d522f475Smrgvoid
7650d522f475Smrgset_cursor_gcs(XtermWidget xw)
7651d522f475Smrg{
7652d522f475Smrg    TScreen *screen = &(xw->screen);
7653d522f475Smrg    VTwin *win = WhichVWin(screen);
7654d522f475Smrg
7655d522f475Smrg    Pixel cc = T_COLOR(screen, TEXT_CURSOR);
7656d522f475Smrg    Pixel fg = T_COLOR(screen, TEXT_FG);
7657d522f475Smrg    Pixel bg = T_COLOR(screen, TEXT_BG);
7658d522f475Smrg    Boolean changed = False;
7659d522f475Smrg
7660d522f475Smrg    /*
7661d522f475Smrg     * Let's see, there are three things that have "color":
7662d522f475Smrg     *
7663d522f475Smrg     *     background
7664d522f475Smrg     *     text
7665d522f475Smrg     *     cursorblock
7666d522f475Smrg     *
7667d522f475Smrg     * And, there are four situations when drawing a cursor, if we decide
7668d522f475Smrg     * that we like have a solid block of cursor color with the letter
7669d522f475Smrg     * that it is highlighting shown in the background color to make it
7670d522f475Smrg     * stand out:
7671d522f475Smrg     *
7672d522f475Smrg     *     selected window, normal video - background on cursor
7673d522f475Smrg     *     selected window, reverse video - foreground on cursor
7674d522f475Smrg     *     unselected window, normal video - foreground on background
7675d522f475Smrg     *     unselected window, reverse video - background on foreground
7676d522f475Smrg     *
7677d522f475Smrg     * Since the last two are really just normalGC and reverseGC, we only
7678d522f475Smrg     * need two new GC's.  Under monochrome, we get the same effect as
7679d522f475Smrg     * above by setting cursor color to foreground.
7680d522f475Smrg     */
7681d522f475Smrg
7682d522f475Smrg    TRACE(("set_cursor_gcs cc=%#lx, fg=%#lx, bg=%#lx\n", cc, fg, bg));
7683d522f475Smrg    if (win != 0 && (cc != bg)) {
7684d522f475Smrg	/* set the fonts to the current one */
7685d522f475Smrg	setCgsFont(xw, win, gcVTcursNormal, 0);
7686d522f475Smrg	setCgsFont(xw, win, gcVTcursFilled, 0);
7687d522f475Smrg	setCgsFont(xw, win, gcVTcursReverse, 0);
7688d522f475Smrg	setCgsFont(xw, win, gcVTcursOutline, 0);
7689d522f475Smrg
7690d522f475Smrg	/* we have a colored cursor */
7691d522f475Smrg	setCgsFore(xw, win, gcVTcursNormal, fg);
7692d522f475Smrg	setCgsBack(xw, win, gcVTcursNormal, cc);
7693d522f475Smrg
7694d522f475Smrg	setCgsFore(xw, win, gcVTcursFilled, cc);
7695d522f475Smrg	setCgsBack(xw, win, gcVTcursFilled, fg);
7696d522f475Smrg
7697d522f475Smrg	if (screen->always_highlight) {
7698d522f475Smrg	    /* both GC's use the same color */
7699d522f475Smrg	    setCgsFore(xw, win, gcVTcursReverse, bg);
7700d522f475Smrg	    setCgsBack(xw, win, gcVTcursReverse, cc);
7701d522f475Smrg
7702d522f475Smrg	    setCgsFore(xw, win, gcVTcursOutline, bg);
7703d522f475Smrg	    setCgsBack(xw, win, gcVTcursOutline, cc);
7704d522f475Smrg	} else {
7705d522f475Smrg	    setCgsFore(xw, win, gcVTcursReverse, bg);
7706d522f475Smrg	    setCgsBack(xw, win, gcVTcursReverse, cc);
7707d522f475Smrg
7708d522f475Smrg	    setCgsFore(xw, win, gcVTcursOutline, cc);
7709d522f475Smrg	    setCgsBack(xw, win, gcVTcursOutline, bg);
7710d522f475Smrg	}
7711d522f475Smrg	changed = True;
7712d522f475Smrg    }
7713d522f475Smrg
7714d522f475Smrg    if (changed) {
7715d522f475Smrg	TRACE(("...set_cursor_gcs - done\n"));
7716d522f475Smrg    }
7717d522f475Smrg}
7718d522f475Smrg
7719d522f475Smrg#ifdef NO_LEAKS
7720d522f475Smrgvoid
7721d522f475Smrgnoleaks_charproc(void)
7722d522f475Smrg{
7723d522f475Smrg    if (v_buffer != 0)
7724d522f475Smrg	free(v_buffer);
7725d522f475Smrg}
7726d522f475Smrg#endif
7727