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