charproc.c revision a1f3da82
1a1f3da82Smrg/* $XTermId: charproc.c,v 1.1112 2011/02/20 00:50:46 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
4a1f3da82Smrg * Copyright 1999-2010,2011 by Thomas E. Dickey
5a1f3da82Smrg *
6a1f3da82Smrg *                         All Rights Reserved
7a1f3da82Smrg *
8a1f3da82Smrg * Permission is hereby granted, free of charge, to any person obtaining a
9a1f3da82Smrg * copy of this software and associated documentation files (the
10a1f3da82Smrg * "Software"), to deal in the Software without restriction, including
11a1f3da82Smrg * without limitation the rights to use, copy, modify, merge, publish,
12a1f3da82Smrg * distribute, sublicense, and/or sell copies of the Software, and to
13a1f3da82Smrg * permit persons to whom the Software is furnished to do so, subject to
14a1f3da82Smrg * the following conditions:
15a1f3da82Smrg *
16a1f3da82Smrg * The above copyright notice and this permission notice shall be included
17a1f3da82Smrg * in all copies or substantial portions of the Software.
18a1f3da82Smrg *
19a1f3da82Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20a1f3da82Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21a1f3da82Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22a1f3da82Smrg * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23a1f3da82Smrg * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24a1f3da82Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25a1f3da82Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26a1f3da82Smrg *
27a1f3da82Smrg * Except as contained in this notice, the name(s) of the above copyright
28a1f3da82Smrg * holders shall not be used in advertising or otherwise to promote the
29a1f3da82Smrg * sale, use or other dealings in this Software without prior written
30a1f3da82Smrg * authorization.
31a1f3da82Smrg *
32a1f3da82Smrg *
33a1f3da82Smrg * Copyright 1988  The Open Group
34a1f3da82Smrg *
35a1f3da82Smrg * Permission to use, copy, modify, distribute, and sell this software and its
36a1f3da82Smrg * documentation for any purpose is hereby granted without fee, provided that
37a1f3da82Smrg * the above copyright notice appear in all copies and that both that
38a1f3da82Smrg * copyright notice and this permission notice appear in supporting
39a1f3da82Smrg * documentation.
40a1f3da82Smrg *
41a1f3da82Smrg * The above copyright notice and this permission notice shall be included in
42a1f3da82Smrg * all copies or substantial portions of the Software.
43a1f3da82Smrg *
44a1f3da82Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45a1f3da82Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46a1f3da82Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
47a1f3da82Smrg * OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
48a1f3da82Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
49a1f3da82Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50a1f3da82Smrg *
51a1f3da82Smrg * Except as contained in this notice, the name of The Open Group shall not be
52a1f3da82Smrg * used in advertising or otherwise to promote the sale, use or other dealings
53a1f3da82Smrg * in this Software without prior written authorization from The Open Group.
54a1f3da82Smrg *
55a1f3da82Smrg */
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>
11920d2c4d2Smrg#include <assert.h>
120d522f475Smrg
121d522f475Smrg#if defined(HAVE_SCHED_YIELD)
122d522f475Smrg#include <sched.h>
123d522f475Smrg#endif
124d522f475Smrg
125d522f475Smrg#include <VTparse.h>
126d522f475Smrg#include <data.h>
127d522f475Smrg#include <error.h>
128d522f475Smrg#include <menu.h>
129d522f475Smrg#include <main.h>
130d522f475Smrg#include <fontutils.h>
131d522f475Smrg#include <xcharmouse.h>
132d522f475Smrg#include <charclass.h>
133d522f475Smrg#include <xstrings.h>
134d522f475Smrg
13520d2c4d2Smrgtypedef void (*BitFunc) (unsigned * /* p */ ,
13620d2c4d2Smrg			 unsigned /* mask */ );
13720d2c4d2Smrg
138d522f475Smrgstatic IChar doinput(void);
13920d2c4d2Smrgstatic int set_character_class(char * /*s */ );
140d522f475Smrgstatic void FromAlternate(XtermWidget /* xw */ );
14120d2c4d2Smrgstatic void RequestResize(XtermWidget /* xw */ ,
14220d2c4d2Smrg			  int /* rows */ ,
14320d2c4d2Smrg			  int /* cols */ ,
14420d2c4d2Smrg			  Bool /* text */ );
14520d2c4d2Smrgstatic void SwitchBufs(XtermWidget /* xw */ ,
14620d2c4d2Smrg		       int /* toBuf */ );
147d522f475Smrgstatic void ToAlternate(XtermWidget /* xw */ );
148d522f475Smrgstatic void ansi_modes(XtermWidget termw,
14920d2c4d2Smrg		       BitFunc /* func */ );
150d522f475Smrgstatic void bitclr(unsigned *p, unsigned mask);
151d522f475Smrgstatic void bitcpy(unsigned *p, unsigned q, unsigned mask);
152d522f475Smrgstatic void bitset(unsigned *p, unsigned mask);
15320d2c4d2Smrgstatic void dpmodes(XtermWidget /* xw */ ,
15420d2c4d2Smrg		    BitFunc /* func */ );
15520d2c4d2Smrgstatic void restoremodes(XtermWidget /* xw */ );
15620d2c4d2Smrgstatic void savemodes(XtermWidget /* xw */ );
15720d2c4d2Smrgstatic void window_ops(XtermWidget /* xw */ );
158d522f475Smrg
159d522f475Smrg#define DoStartBlinking(s) ((s)->cursor_blink ^ (s)->cursor_blink_esc)
160d522f475Smrg
161d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
16220d2c4d2Smrgstatic void SetCursorBlink(TScreen * /* screen */ ,
16320d2c4d2Smrg			   Bool /* enable */ );
16420d2c4d2Smrgstatic void HandleBlinking(XtPointer /* closure */ ,
16520d2c4d2Smrg			   XtIntervalId * /* id */ );
16620d2c4d2Smrgstatic void StartBlinking(TScreen * /* screen */ );
16720d2c4d2Smrgstatic void StopBlinking(TScreen * /* screen */ );
168d522f475Smrg#else
169d522f475Smrg#define StartBlinking(screen)	/* nothing */
170d522f475Smrg#define StopBlinking(screen)	/* nothing */
171d522f475Smrg#endif
172d522f475Smrg
173d522f475Smrg#if OPT_INPUT_METHOD
174d522f475Smrgstatic void PreeditPosition(TScreen * screen);
175d522f475Smrg#endif
176d522f475Smrg
177d522f475Smrg#define	DEFAULT		-1
178d522f475Smrg#define BELLSUPPRESSMSEC 200
179d522f475Smrg
180d522f475Smrgstatic int nparam;
181d522f475Smrgstatic ANSI reply;
182d522f475Smrgstatic int param[NPARAM];
183d522f475Smrg
184d522f475Smrgstatic jmp_buf vtjmpbuf;
185d522f475Smrg
186d522f475Smrg/* event handlers */
187d522f475Smrgstatic void HandleBell PROTO_XT_ACTIONS_ARGS;
188d522f475Smrgstatic void HandleIgnore PROTO_XT_ACTIONS_ARGS;
189d522f475Smrgstatic void HandleKeymapChange PROTO_XT_ACTIONS_ARGS;
190d522f475Smrgstatic void HandleVisualBell PROTO_XT_ACTIONS_ARGS;
191d522f475Smrg#if HANDLE_STRUCT_NOTIFY
192d522f475Smrgstatic void HandleStructNotify PROTO_XT_EV_HANDLER_ARGS;
193d522f475Smrg#endif
194d522f475Smrg
195d522f475Smrg/*
196d522f475Smrg * NOTE: VTInitialize zeros out the entire ".screen" component of the
197d522f475Smrg * XtermWidget, so make sure to add an assignment statement in VTInitialize()
198d522f475Smrg * for each new ".screen" field added to this resource list.
199d522f475Smrg */
200d522f475Smrg
201d522f475Smrg/* Defaults */
202d522f475Smrg#if OPT_ISO_COLORS
203d522f475Smrg
204d522f475Smrg/*
205d522f475Smrg * If we default to colorMode enabled, compile-in defaults for the ANSI colors.
206d522f475Smrg */
207d522f475Smrg#if DFT_COLORMODE
208d522f475Smrg#define DFT_COLOR(name) name
209d522f475Smrg#else
210d522f475Smrg#define DFT_COLOR(name) XtDefaultForeground
211d522f475Smrg#endif
212d522f475Smrg#endif
213d522f475Smrg
21420d2c4d2Smrgstatic String _Font_Selected_ = "yes";	/* string is arbitrary */
215d522f475Smrg
216a1f3da82Smrgstatic const char *defaultTranslations;
217d522f475Smrg/* *INDENT-OFF* */
218d522f475Smrgstatic XtActionsRec actionsList[] = {
219d522f475Smrg    { "allow-send-events",	HandleAllowSends },
220d522f475Smrg    { "bell",			HandleBell },
221d522f475Smrg    { "clear-saved-lines",	HandleClearSavedLines },
2226879286fSmrg    { "copy-selection",		HandleCopySelection },
223d522f475Smrg    { "create-menu",		HandleCreateMenu },
224d522f475Smrg    { "delete-is-del",		HandleDeleteIsDEL },
225d522f475Smrg    { "dired-button",		DiredButton },
226d522f475Smrg    { "hard-reset",		HandleHardReset },
227d522f475Smrg    { "ignore",			HandleIgnore },
228d522f475Smrg    { "insert",			HandleKeyPressed },  /* alias for insert-seven-bit */
229d522f475Smrg    { "insert-eight-bit",	HandleEightBitKeyPressed },
230d522f475Smrg    { "insert-selection",	HandleInsertSelection },
231d522f475Smrg    { "insert-seven-bit",	HandleKeyPressed },
232d522f475Smrg    { "interpret",		HandleInterpret },
233d522f475Smrg    { "keymap",			HandleKeymapChange },
234d522f475Smrg    { "popup-menu",		HandlePopupMenu },
235d522f475Smrg    { "print",			HandlePrintScreen },
236956cc18dSsnj    { "print-everything",	HandlePrintEverything },
237d522f475Smrg    { "print-redir",		HandlePrintControlMode },
238d522f475Smrg    { "quit",			HandleQuit },
239d522f475Smrg    { "redraw",			HandleRedraw },
240d522f475Smrg    { "scroll-back",		HandleScrollBack },
241d522f475Smrg    { "scroll-forw",		HandleScrollForward },
242d522f475Smrg    { "secure",			HandleSecure },
243d522f475Smrg    { "select-cursor-end",	HandleKeyboardSelectEnd },
244d522f475Smrg    { "select-cursor-extend",   HandleKeyboardSelectExtend },
245d522f475Smrg    { "select-cursor-start",	HandleKeyboardSelectStart },
246d522f475Smrg    { "select-end",		HandleSelectEnd },
247d522f475Smrg    { "select-extend",		HandleSelectExtend },
248d522f475Smrg    { "select-set",		HandleSelectSet },
249d522f475Smrg    { "select-start",		HandleSelectStart },
250d522f475Smrg    { "send-signal",		HandleSendSignal },
251d522f475Smrg    { "set-8-bit-control",	Handle8BitControl },
252d522f475Smrg    { "set-allow132",		HandleAllow132 },
253d522f475Smrg    { "set-altscreen",		HandleAltScreen },
254d522f475Smrg    { "set-appcursor",		HandleAppCursor },
255d522f475Smrg    { "set-appkeypad",		HandleAppKeypad },
256d522f475Smrg    { "set-autolinefeed",	HandleAutoLineFeed },
257d522f475Smrg    { "set-autowrap",		HandleAutoWrap },
258d522f475Smrg    { "set-backarrow",		HandleBackarrow },
259d522f475Smrg    { "set-bellIsUrgent",	HandleBellIsUrgent },
260d522f475Smrg    { "set-cursesemul",		HandleCursesEmul },
261d522f475Smrg    { "set-jumpscroll",		HandleJumpscroll },
262d522f475Smrg    { "set-keep-selection",	HandleKeepSelection },
263d522f475Smrg    { "set-marginbell",		HandleMarginBell },
264d522f475Smrg    { "set-old-function-keys",	HandleOldFunctionKeys },
265d522f475Smrg    { "set-pop-on-bell",	HandleSetPopOnBell },
266d522f475Smrg    { "set-reverse-video",	HandleReverseVideo },
267d522f475Smrg    { "set-reversewrap",	HandleReverseWrap },
268d522f475Smrg    { "set-scroll-on-key",	HandleScrollKey },
269d522f475Smrg    { "set-scroll-on-tty-output", HandleScrollTtyOutput },
270d522f475Smrg    { "set-scrollbar",		HandleScrollbar },
271d522f475Smrg    { "set-select",		HandleSetSelect },
272d522f475Smrg    { "set-sun-keyboard",	HandleSunKeyboard },
273d522f475Smrg    { "set-titeInhibit",	HandleTiteInhibit },
274d522f475Smrg    { "set-visual-bell",	HandleSetVisualBell },
275d522f475Smrg    { "set-vt-font",		HandleSetFont },
276d522f475Smrg    { "soft-reset",		HandleSoftReset },
277d522f475Smrg    { "start-cursor-extend",	HandleKeyboardStartExtend },
278d522f475Smrg    { "start-extend",		HandleStartExtend },
279d522f475Smrg    { "string",			HandleStringEvent },
280d522f475Smrg    { "vi-button",		ViButton },
281d522f475Smrg    { "visual-bell",		HandleVisualBell },
282d522f475Smrg#ifdef ALLOWLOGGING
283d522f475Smrg    { "set-logging",		HandleLogging },
284d522f475Smrg#endif
285956cc18dSsnj#if OPT_ALLOW_XXX_OPS
28620d2c4d2Smrg    { "allow-color-ops",	HandleAllowColorOps },
287956cc18dSsnj    { "allow-font-ops",		HandleAllowFontOps },
288956cc18dSsnj    { "allow-tcap-ops",		HandleAllowTcapOps },
289956cc18dSsnj    { "allow-title-ops",	HandleAllowTitleOps },
290956cc18dSsnj    { "allow-window-ops",	HandleAllowWindowOps },
291956cc18dSsnj#endif
292d522f475Smrg#if OPT_BLINK_CURS
293d522f475Smrg    { "set-cursorblink",	HandleCursorBlink },
294d522f475Smrg#endif
295d522f475Smrg#if OPT_BOX_CHARS
296d522f475Smrg    { "set-font-linedrawing",	HandleFontBoxChars },
29720d2c4d2Smrg    { "set-font-packed",	HandleFontPacked },
298d522f475Smrg#endif
299d522f475Smrg#if OPT_DABBREV
300d522f475Smrg    { "dabbrev-expand",		HandleDabbrevExpand },
301d522f475Smrg#endif
302d522f475Smrg#if OPT_DEC_CHRSET
303d522f475Smrg    { "set-font-doublesize",	HandleFontDoublesize },
304d522f475Smrg#endif
305d522f475Smrg#if OPT_DEC_SOFTFONT
306d522f475Smrg    { "set-font-loading",	HandleFontLoading },
307d522f475Smrg#endif
308d522f475Smrg#if OPT_EXEC_XTERM
309d522f475Smrg    { "spawn-new-terminal",	HandleSpawnTerminal },
310d522f475Smrg#endif
311d522f475Smrg#if OPT_HP_FUNC_KEYS
312d522f475Smrg    { "set-hp-function-keys",	HandleHpFunctionKeys },
313d522f475Smrg#endif
314d522f475Smrg#if OPT_LOAD_VTFONTS
315d522f475Smrg    { "load-vt-fonts",		HandleLoadVTFonts },
316d522f475Smrg#endif
317d522f475Smrg#if OPT_MAXIMIZE
318d522f475Smrg    { "deiconify",		HandleDeIconify },
319a1f3da82Smrg    { "fullscreen",		HandleFullscreen },
320d522f475Smrg    { "iconify",		HandleIconify },
321d522f475Smrg    { "maximize",		HandleMaximize },
322d522f475Smrg    { "restore",		HandleRestoreSize },
323d522f475Smrg#endif
324d522f475Smrg#if OPT_NUM_LOCK
325d522f475Smrg    { "alt-sends-escape",	HandleAltEsc },
326d522f475Smrg    { "meta-sends-escape",	HandleMetaEsc },
327d522f475Smrg    { "set-num-lock",		HandleNumLock },
328d522f475Smrg#endif
329d522f475Smrg#if OPT_READLINE
330d522f475Smrg    { "readline-button",	ReadLineButton },
331d522f475Smrg#endif
332d522f475Smrg#if OPT_RENDERFONT
333d522f475Smrg    { "set-render-font",	HandleRenderFont },
334d522f475Smrg#endif
335d522f475Smrg#if OPT_SCO_FUNC_KEYS
336d522f475Smrg    { "set-sco-function-keys",	HandleScoFunctionKeys },
337d522f475Smrg#endif
33820d2c4d2Smrg#if OPT_SCROLL_LOCK
33920d2c4d2Smrg    { "scroll-lock",		HandleScrollLock },
34020d2c4d2Smrg#endif
341d522f475Smrg#if OPT_SHIFT_FONTS
342d522f475Smrg    { "larger-vt-font",		HandleLargerFont },
343d522f475Smrg    { "smaller-vt-font",	HandleSmallerFont },
344d522f475Smrg#endif
345d522f475Smrg#if OPT_SUN_FUNC_KEYS
346d522f475Smrg    { "set-sun-function-keys",	HandleSunFunctionKeys },
347d522f475Smrg#endif
348d522f475Smrg#if OPT_TEK4014
349d522f475Smrg    { "set-terminal-type",	HandleSetTerminalType },
350d522f475Smrg    { "set-visibility",		HandleVisibility },
351d522f475Smrg    { "set-tek-text",		HandleSetTekText },
352d522f475Smrg    { "tek-page",		HandleTekPage },
353d522f475Smrg    { "tek-reset",		HandleTekReset },
354d522f475Smrg    { "tek-copy",		HandleTekCopy },
355d522f475Smrg#endif
356d522f475Smrg#if OPT_TOOLBAR
357d522f475Smrg    { "set-toolbar",		HandleToolbar },
358d522f475Smrg#endif
359d522f475Smrg#if OPT_WIDE_CHARS
360d522f475Smrg    { "set-utf8-mode",		HandleUTF8Mode },
361d522f475Smrg    { "set-utf8-title",		HandleUTF8Title },
362d522f475Smrg#endif
363d522f475Smrg};
364d522f475Smrg/* *INDENT-ON* */
365d522f475Smrg
36620d2c4d2Smrgstatic XtResource xterm_resources[] =
367d522f475Smrg{
368d522f475Smrg    Bres(XtNallowSendEvents, XtCAllowSendEvents, screen.allowSendEvent0, False),
36920d2c4d2Smrg    Bres(XtNallowColorOps, XtCAllowColorOps, screen.allowColorOp0, DEF_ALLOW_COLOR),
370956cc18dSsnj    Bres(XtNallowFontOps, XtCAllowFontOps, screen.allowFontOp0, DEF_ALLOW_FONT),
371956cc18dSsnj    Bres(XtNallowTcapOps, XtCAllowTcapOps, screen.allowTcapOp0, DEF_ALLOW_TCAP),
372956cc18dSsnj    Bres(XtNallowTitleOps, XtCAllowTitleOps, screen.allowTitleOp0, DEF_ALLOW_TITLE),
373956cc18dSsnj    Bres(XtNallowWindowOps, XtCAllowWindowOps, screen.allowWindowOp0, DEF_ALLOW_WINDOW),
374d522f475Smrg    Bres(XtNaltIsNotMeta, XtCAltIsNotMeta, screen.alt_is_not_meta, False),
375d522f475Smrg    Bres(XtNaltSendsEscape, XtCAltSendsEscape, screen.alt_sends_esc, False),
376d522f475Smrg    Bres(XtNalwaysBoldMode, XtCAlwaysBoldMode, screen.always_bold_mode, False),
377d522f475Smrg    Bres(XtNalwaysHighlight, XtCAlwaysHighlight, screen.always_highlight, False),
378d522f475Smrg    Bres(XtNappcursorDefault, XtCAppcursorDefault, misc.appcursorDefault, False),
379d522f475Smrg    Bres(XtNappkeypadDefault, XtCAppkeypadDefault, misc.appkeypadDefault, False),
380d522f475Smrg    Bres(XtNautoWrap, XtCAutoWrap, misc.autoWrap, True),
381d522f475Smrg    Bres(XtNawaitInput, XtCAwaitInput, screen.awaitInput, False),
382d522f475Smrg    Bres(XtNfreeBoldBox, XtCFreeBoldBox, screen.free_bold_box, False),
383d522f475Smrg    Bres(XtNbackarrowKey, XtCBackarrowKey, screen.backarrow_key, True),
384d522f475Smrg    Bres(XtNbellIsUrgent, XtCBellIsUrgent, screen.bellIsUrgent, False),
385d522f475Smrg    Bres(XtNbellOnReset, XtCBellOnReset, screen.bellOnReset, True),
386d522f475Smrg    Bres(XtNboldMode, XtCBoldMode, screen.bold_mode, True),
387d522f475Smrg    Bres(XtNbrokenSelections, XtCBrokenSelections, screen.brokenSelections, False),
388d522f475Smrg    Bres(XtNc132, XtCC132, screen.c132, False),
389d522f475Smrg    Bres(XtNcurses, XtCCurses, screen.curses, False),
390d522f475Smrg    Bres(XtNcutNewline, XtCCutNewline, screen.cutNewline, True),
391d522f475Smrg    Bres(XtNcutToBeginningOfLine, XtCCutToBeginningOfLine,
392d522f475Smrg	 screen.cutToBeginningOfLine, True),
393d522f475Smrg    Bres(XtNdeleteIsDEL, XtCDeleteIsDEL, screen.delete_is_del, DEFDELETE_DEL),
394d522f475Smrg    Bres(XtNdynamicColors, XtCDynamicColors, misc.dynamicColors, True),
395d522f475Smrg    Bres(XtNeightBitControl, XtCEightBitControl, screen.control_eight_bits, False),
396d522f475Smrg    Bres(XtNeightBitInput, XtCEightBitInput, screen.input_eight_bits, True),
397d522f475Smrg    Bres(XtNeightBitOutput, XtCEightBitOutput, screen.output_eight_bits, True),
398d522f475Smrg    Bres(XtNhighlightSelection, XtCHighlightSelection,
399d522f475Smrg	 screen.highlight_selection, False),
400492d43a5Smrg    Bres(XtNshowWrapMarks, XtCShowWrapMarks, screen.show_wrap_marks, False),
401d522f475Smrg    Bres(XtNhpLowerleftBugCompat, XtCHpLowerleftBugCompat, screen.hp_ll_bc, False),
402d522f475Smrg    Bres(XtNi18nSelections, XtCI18nSelections, screen.i18nSelections, True),
403956cc18dSsnj    Bres(XtNfastScroll, XtCFastScroll, screen.fastscroll, False),
404d522f475Smrg    Bres(XtNjumpScroll, XtCJumpScroll, screen.jumpscroll, True),
405956cc18dSsnj    Bres(XtNkeepSelection, XtCKeepSelection, screen.keepSelection, True),
406d522f475Smrg    Bres(XtNloginShell, XtCLoginShell, misc.login_shell, False),
407d522f475Smrg    Bres(XtNmarginBell, XtCMarginBell, screen.marginbell, False),
408d522f475Smrg    Bres(XtNmetaSendsEscape, XtCMetaSendsEscape, screen.meta_sends_esc, False),
409d522f475Smrg    Bres(XtNmultiScroll, XtCMultiScroll, screen.multiscroll, False),
410d522f475Smrg    Bres(XtNoldXtermFKeys, XtCOldXtermFKeys, screen.old_fkeys, False),
411d522f475Smrg    Bres(XtNpopOnBell, XtCPopOnBell, screen.poponbell, False),
412d522f475Smrg    Bres(XtNprinterAutoClose, XtCPrinterAutoClose, screen.printer_autoclose, False),
413d522f475Smrg    Bres(XtNprinterExtent, XtCPrinterExtent, screen.printer_extent, False),
414d522f475Smrg    Bres(XtNprinterFormFeed, XtCPrinterFormFeed, screen.printer_formfeed, False),
41520d2c4d2Smrg    Bres(XtNprinterNewLine, XtCPrinterNewLine, screen.printer_newline, True),
416d522f475Smrg    Bres(XtNquietGrab, XtCQuietGrab, screen.quiet_grab, False),
417d522f475Smrg    Bres(XtNreverseVideo, XtCReverseVideo, misc.re_verse, False),
418d522f475Smrg    Bres(XtNreverseWrap, XtCReverseWrap, misc.reverseWrap, False),
419d522f475Smrg    Bres(XtNscrollBar, XtCScrollBar, misc.scrollbar, False),
420d522f475Smrg    Bres(XtNscrollKey, XtCScrollCond, screen.scrollkey, False),
421d522f475Smrg    Bres(XtNscrollTtyOutput, XtCScrollCond, screen.scrollttyoutput, True),
422d522f475Smrg    Bres(XtNselectToClipboard, XtCSelectToClipboard,
423d522f475Smrg	 screen.selectToClipboard, False),
424d522f475Smrg    Bres(XtNsignalInhibit, XtCSignalInhibit, misc.signalInhibit, False),
425d522f475Smrg    Bres(XtNtiteInhibit, XtCTiteInhibit, misc.titeInhibit, False),
426d522f475Smrg    Bres(XtNtiXtraScroll, XtCTiXtraScroll, misc.tiXtraScroll, False),
427d522f475Smrg    Bres(XtNtrimSelection, XtCTrimSelection, screen.trim_selection, False),
428d522f475Smrg    Bres(XtNunderLine, XtCUnderLine, screen.underline, True),
429d522f475Smrg    Bres(XtNvisualBell, XtCVisualBell, screen.visualbell, False),
430d522f475Smrg
431d522f475Smrg    Ires(XtNbellSuppressTime, XtCBellSuppressTime, screen.bellSuppressTime, BELLSUPPRESSMSEC),
432956cc18dSsnj    Ires(XtNfontWarnings, XtCFontWarnings, misc.fontWarnings, fwResource),
433d522f475Smrg    Ires(XtNinternalBorder, XtCBorderWidth, screen.border, DEFBORDER),
434d522f475Smrg    Ires(XtNlimitResize, XtCLimitResize, misc.limit_resize, 1),
435d522f475Smrg    Ires(XtNmultiClickTime, XtCMultiClickTime, screen.multiClickTime, MULTICLICKTIME),
436d522f475Smrg    Ires(XtNnMarginBell, XtCColumn, screen.nmarginbell, N_MARGINBELL),
437d522f475Smrg    Ires(XtNpointerMode, XtCPointerMode, screen.pointer_mode, DEF_POINTER_MODE),
438d522f475Smrg    Ires(XtNprinterControlMode, XtCPrinterControlMode,
439d522f475Smrg	 screen.printer_controlmode, 0),
44020d2c4d2Smrg    Ires(XtNtitleModes, XtCTitleModes, screen.title_modes, DEF_TITLE_MODES),
441d522f475Smrg    Ires(XtNvisualBellDelay, XtCVisualBellDelay, screen.visualBellDelay, 100),
442d522f475Smrg    Ires(XtNsaveLines, XtCSaveLines, screen.savelines, SAVELINES),
443d522f475Smrg    Ires(XtNscrollBarBorder, XtCScrollBarBorder, screen.scrollBarBorder, 1),
444d522f475Smrg    Ires(XtNscrollLines, XtCScrollLines, screen.scrolllines, SCROLLLINES),
445d522f475Smrg
446d522f475Smrg    Sres(XtNinitialFont, XtCInitialFont, screen.initial_font, NULL),
447d522f475Smrg    Sres(XtNfont1, XtCFont1, screen.MenuFontName(fontMenu_font1), NULL),
448d522f475Smrg    Sres(XtNfont2, XtCFont2, screen.MenuFontName(fontMenu_font2), NULL),
449d522f475Smrg    Sres(XtNfont3, XtCFont3, screen.MenuFontName(fontMenu_font3), NULL),
450d522f475Smrg    Sres(XtNfont4, XtCFont4, screen.MenuFontName(fontMenu_font4), NULL),
451d522f475Smrg    Sres(XtNfont5, XtCFont5, screen.MenuFontName(fontMenu_font5), NULL),
452d522f475Smrg    Sres(XtNfont6, XtCFont6, screen.MenuFontName(fontMenu_font6), NULL),
453956cc18dSsnj
454d522f475Smrg    Sres(XtNanswerbackString, XtCAnswerbackString, screen.answer_back, ""),
455d522f475Smrg    Sres(XtNboldFont, XtCBoldFont, misc.default_font.f_b, DEFBOLDFONT),
456d522f475Smrg    Sres(XtNcharClass, XtCCharClass, screen.charClass, NULL),
457d522f475Smrg    Sres(XtNdecTerminalID, XtCDecTerminalID, screen.term_id, DFT_DECID),
458956cc18dSsnj    Sres(XtNdefaultString, XtCDefaultString, screen.default_string, "#"),
45920d2c4d2Smrg    Sres(XtNdisallowedColorOps, XtCDisallowedColorOps,
46020d2c4d2Smrg	 screen.disallowedColorOps, DEF_DISALLOWED_COLOR),
46120d2c4d2Smrg    Sres(XtNdisallowedFontOps, XtCDisallowedFontOps,
46220d2c4d2Smrg	 screen.disallowedFontOps, DEF_DISALLOWED_FONT),
46320d2c4d2Smrg    Sres(XtNdisallowedTcapOps, XtCDisallowedTcapOps,
46420d2c4d2Smrg	 screen.disallowedTcapOps, DEF_DISALLOWED_TCAP),
46520d2c4d2Smrg    Sres(XtNdisallowedWindowOps, XtCDisallowedWindowOps,
46620d2c4d2Smrg	 screen.disallowedWinOps, DEF_DISALLOWED_WINDOW),
467956cc18dSsnj    Sres(XtNeightBitSelectTypes, XtCEightBitSelectTypes,
468956cc18dSsnj	 screen.eightbit_select_types, NULL),
469d522f475Smrg    Sres(XtNfont, XtCFont, misc.default_font.f_n, DEFFONT),
470d522f475Smrg    Sres(XtNgeometry, XtCGeometry, misc.geo_metry, NULL),
471d522f475Smrg    Sres(XtNkeyboardDialect, XtCKeyboardDialect, screen.keyboard_dialect, DFT_KBD_DIALECT),
472d522f475Smrg    Sres(XtNprinterCommand, XtCPrinterCommand, screen.printer_command, ""),
473d522f475Smrg    Sres(XtNtekGeometry, XtCGeometry, misc.T_geometry, NULL),
474d522f475Smrg
475d522f475Smrg    Tres(XtNcursorColor, XtCCursorColor, TEXT_CURSOR, XtDefaultForeground),
476d522f475Smrg    Tres(XtNforeground, XtCForeground, TEXT_FG, XtDefaultForeground),
477d522f475Smrg    Tres(XtNpointerColor, XtCPointerColor, MOUSE_FG, XtDefaultForeground),
478d522f475Smrg    Tres(XtNbackground, XtCBackground, TEXT_BG, XtDefaultBackground),
479d522f475Smrg    Tres(XtNpointerColorBackground, XtCBackground, MOUSE_BG, XtDefaultBackground),
480d522f475Smrg
481d522f475Smrg    {XtNresizeGravity, XtCResizeGravity, XtRGravity, sizeof(XtGravity),
482d522f475Smrg     XtOffsetOf(XtermWidgetRec, misc.resizeGravity),
483d522f475Smrg     XtRImmediate, (XtPointer) SouthWestGravity},
484d522f475Smrg
485d522f475Smrg    {XtNpointerShape, XtCCursor, XtRCursor, sizeof(Cursor),
486d522f475Smrg     XtOffsetOf(XtermWidgetRec, screen.pointer_cursor),
487d522f475Smrg     XtRString, (XtPointer) "xterm"},
488d522f475Smrg
489d522f475Smrg#ifdef ALLOWLOGGING
490d522f475Smrg    Bres(XtNlogInhibit, XtCLogInhibit, misc.logInhibit, False),
491d522f475Smrg    Bres(XtNlogging, XtCLogging, misc.log_on, False),
492d522f475Smrg    Sres(XtNlogFile, XtCLogfile, screen.logfile, NULL),
493d522f475Smrg#endif
494d522f475Smrg
495d522f475Smrg#ifndef NO_ACTIVE_ICON
496d522f475Smrg    Bres("activeIcon", "ActiveIcon", misc.active_icon, False),
497d522f475Smrg    Ires("iconBorderWidth", XtCBorderWidth, misc.icon_border_width, 2),
498492d43a5Smrg    Sres("iconFont", "IconFont", screen.icon_fontname, "nil2"),
499d522f475Smrg    Cres("iconBorderColor", XtCBorderColor, misc.icon_border_pixel, XtDefaultBackground),
500d522f475Smrg#endif				/* NO_ACTIVE_ICON */
501d522f475Smrg
502d522f475Smrg#if OPT_BLINK_CURS
503d522f475Smrg    Bres(XtNcursorBlink, XtCCursorBlink, screen.cursor_blink, False),
504d522f475Smrg#endif
5052eaa94a1Schristos    Bres(XtNcursorUnderline, XtCCursorUnderline, screen.cursor_underline, False),
506d522f475Smrg
507d522f475Smrg#if OPT_BLINK_TEXT
508d522f475Smrg    Bres(XtNshowBlinkAsBold, XtCCursorBlink, screen.blink_as_bold, DEFBLINKASBOLD),
509d522f475Smrg#endif
510d522f475Smrg
511d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
512d522f475Smrg    Ires(XtNcursorOnTime, XtCCursorOnTime, screen.blink_on, 600),
513d522f475Smrg    Ires(XtNcursorOffTime, XtCCursorOffTime, screen.blink_off, 300),
514d522f475Smrg#endif
515d522f475Smrg
516d522f475Smrg#if OPT_BOX_CHARS
517d522f475Smrg    Bres(XtNforceBoxChars, XtCForceBoxChars, screen.force_box_chars, False),
51820d2c4d2Smrg    Bres(XtNforcePackedFont, XtCForcePackedFont, screen.force_packed, True),
519d522f475Smrg    Bres(XtNshowMissingGlyphs, XtCShowMissingGlyphs, screen.force_all_chars, False),
520d522f475Smrg#endif
521d522f475Smrg
522d522f475Smrg#if OPT_BROKEN_OSC
523d522f475Smrg    Bres(XtNbrokenLinuxOSC, XtCBrokenLinuxOSC, screen.brokenLinuxOSC, True),
524d522f475Smrg#endif
525d522f475Smrg
526d522f475Smrg#if OPT_BROKEN_ST
527d522f475Smrg    Bres(XtNbrokenStringTerm, XtCBrokenStringTerm, screen.brokenStringTerm, True),
528d522f475Smrg#endif
529d522f475Smrg
530d522f475Smrg#if OPT_C1_PRINT
531d522f475Smrg    Bres(XtNallowC1Printable, XtCAllowC1Printable, screen.c1_printable, False),
532d522f475Smrg#endif
533d522f475Smrg
534d522f475Smrg#if OPT_CLIP_BOLD
535d522f475Smrg    Bres(XtNuseClipping, XtCUseClipping, screen.use_clipping, True),
536d522f475Smrg#endif
537d522f475Smrg
538d522f475Smrg#if OPT_DEC_CHRSET
539d522f475Smrg    Bres(XtNfontDoublesize, XtCFontDoublesize, screen.font_doublesize, True),
540d522f475Smrg    Ires(XtNcacheDoublesize, XtCCacheDoublesize, screen.cache_doublesize, NUM_CHRSET),
541d522f475Smrg#endif
542d522f475Smrg
543d522f475Smrg#if OPT_HIGHLIGHT_COLOR
544d522f475Smrg    Tres(XtNhighlightColor, XtCHighlightColor, HIGHLIGHT_BG, XtDefaultForeground),
545d522f475Smrg    Tres(XtNhighlightTextColor, XtCHighlightTextColor, HIGHLIGHT_FG, XtDefaultBackground),
546d522f475Smrg    Bres(XtNhighlightReverse, XtCHighlightReverse, screen.hilite_reverse, True),
547d522f475Smrg    Bres(XtNhighlightColorMode, XtCHighlightColorMode, screen.hilite_color, Maybe),
548d522f475Smrg#endif				/* OPT_HIGHLIGHT_COLOR */
549d522f475Smrg
550d522f475Smrg#if OPT_INPUT_METHOD
551d522f475Smrg    Bres(XtNopenIm, XtCOpenIm, misc.open_im, True),
552d522f475Smrg    Sres(XtNinputMethod, XtCInputMethod, misc.input_method, NULL),
553d522f475Smrg    Sres(XtNpreeditType, XtCPreeditType, misc.preedit_type,
554d522f475Smrg	 "OverTheSpot,Root"),
555956cc18dSsnj    Ires(XtNretryInputMethod, XtCRetryInputMethod, misc.retry_im, 3),
556d522f475Smrg#endif
557d522f475Smrg
558d522f475Smrg#if OPT_ISO_COLORS
559d522f475Smrg    Bres(XtNboldColors, XtCColorMode, screen.boldColors, True),
560d522f475Smrg    Ires(XtNveryBoldColors, XtCVeryBoldColors, screen.veryBoldColors, 0),
561d522f475Smrg    Bres(XtNcolorMode, XtCColorMode, screen.colorMode, DFT_COLORMODE),
562d522f475Smrg
563d522f475Smrg    Bres(XtNcolorAttrMode, XtCColorAttrMode, screen.colorAttrMode, False),
564d522f475Smrg    Bres(XtNcolorBDMode, XtCColorAttrMode, screen.colorBDMode, False),
565d522f475Smrg    Bres(XtNcolorBLMode, XtCColorAttrMode, screen.colorBLMode, False),
566d522f475Smrg    Bres(XtNcolorRVMode, XtCColorAttrMode, screen.colorRVMode, False),
567d522f475Smrg    Bres(XtNcolorULMode, XtCColorAttrMode, screen.colorULMode, False),
568d522f475Smrg    Bres(XtNitalicULMode, XtCColorAttrMode, screen.italicULMode, False),
569d522f475Smrg
570d522f475Smrg    COLOR_RES("0", screen.Acolors[COLOR_0], DFT_COLOR("black")),
571d522f475Smrg    COLOR_RES("1", screen.Acolors[COLOR_1], DFT_COLOR("red3")),
572d522f475Smrg    COLOR_RES("2", screen.Acolors[COLOR_2], DFT_COLOR("green3")),
573d522f475Smrg    COLOR_RES("3", screen.Acolors[COLOR_3], DFT_COLOR("yellow3")),
574d522f475Smrg    COLOR_RES("4", screen.Acolors[COLOR_4], DFT_COLOR(DEF_COLOR4)),
575d522f475Smrg    COLOR_RES("5", screen.Acolors[COLOR_5], DFT_COLOR("magenta3")),
576d522f475Smrg    COLOR_RES("6", screen.Acolors[COLOR_6], DFT_COLOR("cyan3")),
577d522f475Smrg    COLOR_RES("7", screen.Acolors[COLOR_7], DFT_COLOR("gray90")),
578d522f475Smrg    COLOR_RES("8", screen.Acolors[COLOR_8], DFT_COLOR("gray50")),
579d522f475Smrg    COLOR_RES("9", screen.Acolors[COLOR_9], DFT_COLOR("red")),
580d522f475Smrg    COLOR_RES("10", screen.Acolors[COLOR_10], DFT_COLOR("green")),
581d522f475Smrg    COLOR_RES("11", screen.Acolors[COLOR_11], DFT_COLOR("yellow")),
582d522f475Smrg    COLOR_RES("12", screen.Acolors[COLOR_12], DFT_COLOR(DEF_COLOR12)),
583d522f475Smrg    COLOR_RES("13", screen.Acolors[COLOR_13], DFT_COLOR("magenta")),
584d522f475Smrg    COLOR_RES("14", screen.Acolors[COLOR_14], DFT_COLOR("cyan")),
585d522f475Smrg    COLOR_RES("15", screen.Acolors[COLOR_15], DFT_COLOR("white")),
586d522f475Smrg    COLOR_RES("BD", screen.Acolors[COLOR_BD], DFT_COLOR(XtDefaultForeground)),
587d522f475Smrg    COLOR_RES("BL", screen.Acolors[COLOR_BL], DFT_COLOR(XtDefaultForeground)),
588d522f475Smrg    COLOR_RES("UL", screen.Acolors[COLOR_UL], DFT_COLOR(XtDefaultForeground)),
589d522f475Smrg    COLOR_RES("RV", screen.Acolors[COLOR_RV], DFT_COLOR(XtDefaultForeground)),
590d522f475Smrg
591d522f475Smrg#if !OPT_COLOR_RES2
592d522f475Smrg#if OPT_256_COLORS
593d522f475Smrg# include <256colres.h>
594d522f475Smrg#elif OPT_88_COLORS
595d522f475Smrg# include <88colres.h>
596d522f475Smrg#endif
597d522f475Smrg#endif				/* !OPT_COLOR_RES2 */
598d522f475Smrg
599d522f475Smrg#endif				/* OPT_ISO_COLORS */
600d522f475Smrg
60120d2c4d2Smrg    CLICK_RES("2", screen.onClick[1], "word"),
60220d2c4d2Smrg    CLICK_RES("3", screen.onClick[2], "line"),
60320d2c4d2Smrg    CLICK_RES("4", screen.onClick[3], 0),
60420d2c4d2Smrg    CLICK_RES("5", screen.onClick[4], 0),
60520d2c4d2Smrg
606d522f475Smrg#if OPT_MOD_FKEYS
607d522f475Smrg    Ires(XtNmodifyCursorKeys, XtCModifyCursorKeys,
608d522f475Smrg	 keyboard.modify_1st.cursor_keys, 2),
609d522f475Smrg    Ires(XtNmodifyFunctionKeys, XtCModifyFunctionKeys,
610d522f475Smrg	 keyboard.modify_1st.function_keys, 2),
611d522f475Smrg    Ires(XtNmodifyKeypadKeys, XtCModifyKeypadKeys,
612d522f475Smrg	 keyboard.modify_1st.keypad_keys, 0),
613d522f475Smrg    Ires(XtNmodifyOtherKeys, XtCModifyOtherKeys,
614d522f475Smrg	 keyboard.modify_1st.other_keys, 0),
615d522f475Smrg    Ires(XtNmodifyStringKeys, XtCModifyStringKeys,
616d522f475Smrg	 keyboard.modify_1st.string_keys, 0),
617d522f475Smrg    Ires(XtNformatOtherKeys, XtCFormatOtherKeys,
618d522f475Smrg	 keyboard.format_keys, 0),
619d522f475Smrg#endif
620d522f475Smrg
621d522f475Smrg#if OPT_NUM_LOCK
622d522f475Smrg    Bres(XtNalwaysUseMods, XtCAlwaysUseMods, misc.alwaysUseMods, False),
623d522f475Smrg    Bres(XtNnumLock, XtCNumLock, misc.real_NumLock, True),
624d522f475Smrg#endif
625d522f475Smrg
626d522f475Smrg#if OPT_PRINT_COLORS
627d522f475Smrg    Ires(XtNprintAttributes, XtCPrintAttributes, screen.print_attributes, 1),
628d522f475Smrg#endif
629d522f475Smrg
630d522f475Smrg#if OPT_SHIFT_FONTS
631d522f475Smrg    Bres(XtNshiftFonts, XtCShiftFonts, misc.shift_fonts, True),
632d522f475Smrg#endif
633d522f475Smrg
634d522f475Smrg#if OPT_SUNPC_KBD
635d522f475Smrg    Ires(XtNctrlFKeys, XtCCtrlFKeys, misc.ctrl_fkeys, 10),
636d522f475Smrg#endif
637d522f475Smrg
638d522f475Smrg#if OPT_TEK4014
639d522f475Smrg    Bres(XtNtekInhibit, XtCTekInhibit, misc.tekInhibit, False),
640d522f475Smrg    Bres(XtNtekSmall, XtCTekSmall, misc.tekSmall, False),
641d522f475Smrg    Bres(XtNtekStartup, XtCTekStartup, misc.TekEmu, False),
642d522f475Smrg#endif
643d522f475Smrg
644d522f475Smrg#if OPT_TOOLBAR
645d522f475Smrg    Wres(XtNmenuBar, XtCMenuBar, VT100_TB_INFO(menu_bar), 0),
646d522f475Smrg    Ires(XtNmenuHeight, XtCMenuHeight, VT100_TB_INFO(menu_height), 25),
647d522f475Smrg#endif
648d522f475Smrg
649d522f475Smrg#if OPT_WIDE_CHARS
650d522f475Smrg    Bres(XtNcjkWidth, XtCCjkWidth, misc.cjk_width, False),
651d522f475Smrg    Bres(XtNmkWidth, XtCMkWidth, misc.mk_width, False),
652d522f475Smrg    Bres(XtNutf8Latin1, XtCUtf8Latin1, screen.utf8_latin1, False),
653d522f475Smrg    Bres(XtNutf8Title, XtCUtf8Title, screen.utf8_title, False),
654d522f475Smrg    Bres(XtNvt100Graphics, XtCVT100Graphics, screen.vt100_graphics, True),
655d522f475Smrg    Bres(XtNwideChars, XtCWideChars, screen.wide_chars, False),
656d522f475Smrg    Ires(XtNcombiningChars, XtCCombiningChars, screen.max_combining, 2),
657d522f475Smrg    Ires(XtNmkSamplePass, XtCMkSamplePass, misc.mk_samplepass, 256),
658d522f475Smrg    Ires(XtNmkSampleSize, XtCMkSampleSize, misc.mk_samplesize, 1024),
659d522f475Smrg    Ires(XtNutf8, XtCUtf8, screen.utf8_mode, uDefault),
660d522f475Smrg    Sres(XtNwideBoldFont, XtCWideBoldFont, misc.default_font.f_wb, DEFWIDEBOLDFONT),
661d522f475Smrg    Sres(XtNwideFont, XtCWideFont, misc.default_font.f_w, DEFWIDEFONT),
662956cc18dSsnj    Sres(XtNutf8SelectTypes, XtCUtf8SelectTypes, screen.utf8_select_types, NULL),
663d522f475Smrg#endif
664d522f475Smrg
665d522f475Smrg#if OPT_LUIT_PROG
666d522f475Smrg    Sres(XtNlocale, XtCLocale, misc.locale_str, "medium"),
667d522f475Smrg    Sres(XtNlocaleFilter, XtCLocaleFilter, misc.localefilter, DEFLOCALEFILTER),
668d522f475Smrg#endif
669d522f475Smrg
670d522f475Smrg#if OPT_INPUT_METHOD
671d522f475Smrg    Sres(XtNximFont, XtCXimFont, misc.f_x, DEFXIMFONT),
672d522f475Smrg#endif
673d522f475Smrg
67420d2c4d2Smrg#if OPT_SCROLL_LOCK
67520d2c4d2Smrg    Bres(XtNallowScrollLock, XtCAllowScrollLock, screen.allowScrollLock0, False),
67620d2c4d2Smrg#endif
67720d2c4d2Smrg
678d522f475Smrg#if OPT_XMC_GLITCH
679d522f475Smrg    Bres(XtNxmcInline, XtCXmcInline, screen.xmc_inline, False),
680d522f475Smrg    Bres(XtNxmcMoveSGR, XtCXmcMoveSGR, screen.move_sgr_ok, True),
681d522f475Smrg    Ires(XtNxmcAttributes, XtCXmcAttributes, screen.xmc_attributes, 1),
682d522f475Smrg    Ires(XtNxmcGlitch, XtCXmcGlitch, screen.xmc_glitch, 0),
683d522f475Smrg#endif
684d522f475Smrg
685d522f475Smrg#ifdef SCROLLBAR_RIGHT
686d522f475Smrg    Bres(XtNrightScrollBar, XtCRightScrollBar, misc.useRight, False),
687d522f475Smrg#endif
688d522f475Smrg
689d522f475Smrg#if OPT_RENDERFONT
690d522f475Smrg#define RES_FACESIZE(n) Dres(XtNfaceSize #n, XtCFaceSize #n, misc.face_size[n], "0.0")
691d522f475Smrg    RES_FACESIZE(1),
692d522f475Smrg    RES_FACESIZE(2),
693d522f475Smrg    RES_FACESIZE(3),
694d522f475Smrg    RES_FACESIZE(4),
695d522f475Smrg    RES_FACESIZE(5),
696d522f475Smrg    RES_FACESIZE(6),
697d522f475Smrg    Dres(XtNfaceSize, XtCFaceSize, misc.face_size[0], DEFFACESIZE),
698d522f475Smrg    Sres(XtNfaceName, XtCFaceName, misc.face_name, DEFFACENAME),
699d522f475Smrg    Sres(XtNfaceNameDoublesize, XtCFaceNameDoublesize, misc.face_wide_name, DEFFACENAME),
70020d2c4d2Smrg    Sres(XtNrenderFont, XtCRenderFont, misc.render_font_s, "default"),
701d522f475Smrg#endif
702d522f475Smrg};
703d522f475Smrg
704d522f475Smrgstatic Boolean VTSetValues(Widget cur, Widget request, Widget new_arg,
705d522f475Smrg			   ArgList args, Cardinal *num_args);
706d522f475Smrgstatic void VTClassInit(void);
707d522f475Smrgstatic void VTDestroy(Widget w);
708d522f475Smrgstatic void VTExpose(Widget w, XEvent * event, Region region);
709d522f475Smrgstatic void VTInitialize(Widget wrequest, Widget new_arg, ArgList args,
710d522f475Smrg			 Cardinal *num_args);
711d522f475Smrgstatic void VTRealize(Widget w, XtValueMask * valuemask,
712d522f475Smrg		      XSetWindowAttributes * values);
713d522f475Smrgstatic void VTResize(Widget w);
714d522f475Smrg
715d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
716956cc18dSsnjstatic void VTInitI18N(XtermWidget);
717d522f475Smrg#endif
718d522f475Smrg
719d522f475Smrg#ifdef VMS
720d522f475Smrgglobaldef {
721d522f475Smrg    "xtermclassrec"
722d522f475Smrg} noshare
723d522f475Smrg
724d522f475Smrg#else
725d522f475Smrgstatic
726d522f475Smrg#endif				/* VMS */
727d522f475SmrgWidgetClassRec xtermClassRec =
728d522f475Smrg{
729d522f475Smrg    {
73020d2c4d2Smrg	/* core_class fields */
73120d2c4d2Smrg	(WidgetClass) & widgetClassRec,		/* superclass   */
732d522f475Smrg	"VT100",		/* class_name                   */
733d522f475Smrg	sizeof(XtermWidgetRec),	/* widget_size                  */
734d522f475Smrg	VTClassInit,		/* class_initialize             */
735d522f475Smrg	NULL,			/* class_part_initialize        */
736d522f475Smrg	False,			/* class_inited                 */
737d522f475Smrg	VTInitialize,		/* initialize                   */
738d522f475Smrg	NULL,			/* initialize_hook              */
739d522f475Smrg	VTRealize,		/* realize                      */
740d522f475Smrg	actionsList,		/* actions                      */
741d522f475Smrg	XtNumber(actionsList),	/* num_actions                  */
74220d2c4d2Smrg	xterm_resources,	/* resources                    */
74320d2c4d2Smrg	XtNumber(xterm_resources),	/* num_resources        */
744d522f475Smrg	NULLQUARK,		/* xrm_class                    */
745d522f475Smrg	True,			/* compress_motion              */
746d522f475Smrg	False,			/* compress_exposure            */
747d522f475Smrg	True,			/* compress_enterleave          */
748d522f475Smrg	False,			/* visible_interest             */
749d522f475Smrg	VTDestroy,		/* destroy                      */
750d522f475Smrg	VTResize,		/* resize                       */
751d522f475Smrg	VTExpose,		/* expose                       */
752d522f475Smrg	VTSetValues,		/* set_values                   */
753d522f475Smrg	NULL,			/* set_values_hook              */
754d522f475Smrg	XtInheritSetValuesAlmost,	/* set_values_almost    */
755d522f475Smrg	NULL,			/* get_values_hook              */
756d522f475Smrg	NULL,			/* accept_focus                 */
757d522f475Smrg	XtVersion,		/* version                      */
758d522f475Smrg	NULL,			/* callback_offsets             */
759a1f3da82Smrg	0,			/* tm_table                     */
760d522f475Smrg	XtInheritQueryGeometry,	/* query_geometry               */
761d522f475Smrg	XtInheritDisplayAccelerator,	/* display_accelerator  */
762d522f475Smrg	NULL			/* extension                    */
763d522f475Smrg    }
764d522f475Smrg};
765d522f475Smrg
766d522f475Smrg#ifdef VMS
767d522f475Smrgglobaldef {
768d522f475Smrg    "xtermwidgetclass"
769d522f475Smrg}
770d522f475Smrgnoshare
771d522f475Smrg#endif /* VMS */
772d522f475SmrgWidgetClass xtermWidgetClass = (WidgetClass) & xtermClassRec;
773d522f475Smrg
774d522f475Smrg/*
775d522f475Smrg * Add input-actions for widgets that are overlooked (scrollbar and toolbar):
776d522f475Smrg *
777d522f475Smrg *	a) Sometimes the scrollbar passes through translations, sometimes it
778d522f475Smrg *	   doesn't.  We add the KeyPress translations here, just to be sure.
779d522f475Smrg *	b) In the normal (non-toolbar) configuration, the xterm widget covers
780d522f475Smrg *	   almost all of the window.  With a toolbar, there's a relatively
781d522f475Smrg *	   large area that the user would expect to enter keystrokes since the
782d522f475Smrg *	   program can get the focus.
783d522f475Smrg */
784d522f475Smrgvoid
785d522f475SmrgxtermAddInput(Widget w)
786d522f475Smrg{
787d522f475Smrg    /* *INDENT-OFF* */
788d522f475Smrg    XtActionsRec input_actions[] = {
789d522f475Smrg	{ "insert",		    HandleKeyPressed }, /* alias */
790d522f475Smrg	{ "insert-eight-bit",	    HandleEightBitKeyPressed },
791d522f475Smrg	{ "insert-seven-bit",	    HandleKeyPressed },
792d522f475Smrg	{ "secure",		    HandleSecure },
793d522f475Smrg	{ "string",		    HandleStringEvent },
794d522f475Smrg	{ "scroll-back",	    HandleScrollBack },
795d522f475Smrg	{ "scroll-forw",	    HandleScrollForward },
796d522f475Smrg	{ "select-cursor-end",	    HandleKeyboardSelectEnd },
797d522f475Smrg	{ "select-cursor-extend",   HandleKeyboardSelectExtend },
798d522f475Smrg	{ "select-cursor-start",    HandleKeyboardSelectStart },
799d522f475Smrg	{ "insert-selection",	    HandleInsertSelection },
800d522f475Smrg	{ "select-start",	    HandleSelectStart },
801d522f475Smrg	{ "select-extend",	    HandleSelectExtend },
802d522f475Smrg	{ "start-extend",	    HandleStartExtend },
803d522f475Smrg	{ "select-end",		    HandleSelectEnd },
804d522f475Smrg	{ "clear-saved-lines",	    HandleClearSavedLines },
805d522f475Smrg	{ "popup-menu",		    HandlePopupMenu },
806d522f475Smrg	{ "bell",		    HandleBell },
807d522f475Smrg	{ "ignore",		    HandleIgnore },
808d522f475Smrg#if OPT_DABBREV
809d522f475Smrg	{ "dabbrev-expand",	    HandleDabbrevExpand },
810d522f475Smrg#endif
811a1f3da82Smrg#if OPT_MAXIMIZE
812a1f3da82Smrg	{ "fullscreen",		    HandleFullscreen },
813a1f3da82Smrg#endif
81420d2c4d2Smrg#if OPT_SCROLL_LOCK
81520d2c4d2Smrg	{ "scroll-lock",	    HandleScrollLock },
81620d2c4d2Smrg#endif
817d522f475Smrg#if OPT_SHIFT_FONTS
818d522f475Smrg	{ "larger-vt-font",	    HandleLargerFont },
819d522f475Smrg	{ "smaller-vt-font",	    HandleSmallerFont },
820d522f475Smrg#endif
821d522f475Smrg    };
822d522f475Smrg    /* *INDENT-ON* */
823d522f475Smrg
824956cc18dSsnj    TRACE_TRANS("BEFORE", w);
825d522f475Smrg    XtAppAddActions(app_con, input_actions, XtNumber(input_actions));
826d522f475Smrg    XtAugmentTranslations(w, XtParseTranslationTable(defaultTranslations));
827956cc18dSsnj    TRACE_TRANS("AFTER:", w);
828d522f475Smrg
829d522f475Smrg#if OPT_EXTRA_PASTE
830d522f475Smrg    if (term && term->keyboard.extra_translations)
831d522f475Smrg	XtOverrideTranslations((Widget) term, XtParseTranslationTable(term->keyboard.extra_translations));
832d522f475Smrg#endif
833d522f475Smrg}
834d522f475Smrg
835d522f475Smrg#if OPT_ISO_COLORS
836956cc18dSsnj#ifdef EXP_BOGUS_FG
837956cc18dSsnjstatic Bool
838956cc18dSsnjCheckBogusForeground(TScreen * screen, const char *tag)
839956cc18dSsnj{
840956cc18dSsnj    int row = -1, col = -1, pass;
841956cc18dSsnj    Bool isClear = True;
842956cc18dSsnj
843956cc18dSsnj    (void) tag;
844956cc18dSsnj    for (pass = 0; pass < 2; ++pass) {
845956cc18dSsnj	row = screen->cur_row;
846956cc18dSsnj	for (; isClear && (row <= screen->max_row); ++row) {
847956cc18dSsnj	    LineData *ld = getLineData(screen, row)->;
84820d2c4d2Smrg
84920d2c4d2Smrg	    if (ld != 0) {
85020d2c4d2Smrg		Char *attribs = ld->attribs;
85120d2c4d2Smrg
85220d2c4d2Smrg		col = (row == screen->cur_row) ? screen->cur_col : 0;
85320d2c4d2Smrg		for (; isClear && (col <= screen->max_col); ++col) {
85420d2c4d2Smrg		    unsigned flags = attribs[col];
85520d2c4d2Smrg		    if (pass) {
85620d2c4d2Smrg			flags &= ~FG_COLOR;
85720d2c4d2Smrg			attribs[col] = (Char) flags;
85820d2c4d2Smrg		    } else if ((flags & BG_COLOR)) {
85920d2c4d2Smrg			isClear = False;
86020d2c4d2Smrg		    } else if ((flags & FG_COLOR)) {
86120d2c4d2Smrg			unsigned ch = ld->charData[col];
86220d2c4d2Smrg			isClear = ((ch == ' ') || (ch == 0));
86320d2c4d2Smrg		    } else {
86420d2c4d2Smrg			isClear = False;
86520d2c4d2Smrg		    }
866956cc18dSsnj		}
867956cc18dSsnj	    }
868956cc18dSsnj	}
869956cc18dSsnj    }
870956cc18dSsnj    TRACE(("%s checked %d,%d to %d,%d %s pass %d\n",
871956cc18dSsnj	   tag, screen->cur_row, screen->cur_col,
872956cc18dSsnj	   row, col,
873956cc18dSsnj	   isClear && pass ? "cleared" : "unchanged",
874956cc18dSsnj	   pass));
875956cc18dSsnj
876956cc18dSsnj    return isClear;
877956cc18dSsnj}
878956cc18dSsnj#endif
879956cc18dSsnj
880d522f475Smrg/*
881d522f475Smrg * The terminal's foreground and background colors are set via two mechanisms:
882d522f475Smrg *	text (cur_foreground, cur_background values that are passed down to
883d522f475Smrg *		XDrawImageString and XDrawString)
884d522f475Smrg *	area (X11 graphics context used in XClearArea and XFillRectangle)
885d522f475Smrg */
886d522f475Smrgvoid
887d522f475SmrgSGR_Foreground(XtermWidget xw, int color)
888d522f475Smrg{
88920d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
890d522f475Smrg    Pixel fg;
891d522f475Smrg
892d522f475Smrg    if (color >= 0) {
89320d2c4d2Smrg	UIntSet(xw->flags, FG_COLOR);
894d522f475Smrg    } else {
89520d2c4d2Smrg	UIntClr(xw->flags, FG_COLOR);
896d522f475Smrg    }
897d522f475Smrg    fg = getXtermForeground(xw, xw->flags, color);
898d522f475Smrg    xw->cur_foreground = color;
899d522f475Smrg
900d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcNorm, fg);
901d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcNormReverse, fg);
902d522f475Smrg
903d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcBold, fg);
904d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcBoldReverse, fg);
905956cc18dSsnj
906956cc18dSsnj#ifdef EXP_BOGUS_FG
907956cc18dSsnj    /*
908956cc18dSsnj     * If we've just turned off the foreground color, check for blank cells
909956cc18dSsnj     * which have no background color, but do have foreground color.  This
910956cc18dSsnj     * could happen due to setting the foreground color just before scrolling.
911956cc18dSsnj     *
912956cc18dSsnj     * Those cells look uncolored, but will confuse ShowCursor(), which looks
91320d2c4d2Smrg     * for the colors in the current cell, and will see the foreground color.
914956cc18dSsnj     * In that case, remove the foreground color from the blank cells.
915956cc18dSsnj     */
916956cc18dSsnj    if (color < 0) {
917956cc18dSsnj	CheckBogusForeground(screen, "SGR_Foreground");
918956cc18dSsnj    }
919956cc18dSsnj#endif
920d522f475Smrg}
921d522f475Smrg
922d522f475Smrgvoid
923d522f475SmrgSGR_Background(XtermWidget xw, int color)
924d522f475Smrg{
92520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
926d522f475Smrg    Pixel bg;
927d522f475Smrg
928d522f475Smrg    /*
929d522f475Smrg     * An indexing operation may have set screen->scroll_amt, which would
930d522f475Smrg     * normally result in calling FlushScroll() in WriteText().  However,
931d522f475Smrg     * if we're changing the background color now, then the new value
932d522f475Smrg     * should not apply to the pending blank lines.
933d522f475Smrg     */
934d522f475Smrg    if (screen->scroll_amt && (color != xw->cur_background))
935d522f475Smrg	FlushScroll(xw);
936d522f475Smrg
937d522f475Smrg    if (color >= 0) {
93820d2c4d2Smrg	UIntSet(xw->flags, BG_COLOR);
939d522f475Smrg    } else {
94020d2c4d2Smrg	UIntClr(xw->flags, BG_COLOR);
941d522f475Smrg    }
942d522f475Smrg    bg = getXtermBackground(xw, xw->flags, color);
943d522f475Smrg    xw->cur_background = color;
944d522f475Smrg
945d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcNorm, bg);
946d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcNormReverse, bg);
947d522f475Smrg
948d522f475Smrg    setCgsBack(xw, WhichVWin(screen), gcBold, bg);
949d522f475Smrg    setCgsFore(xw, WhichVWin(screen), gcBoldReverse, bg);
950d522f475Smrg}
951d522f475Smrg
952d522f475Smrg/* Invoked after updating bold/underline flags, computes the extended color
953d522f475Smrg * index to use for foreground.  (See also 'extract_fg()').
954d522f475Smrg */
955d522f475Smrgstatic void
956d522f475SmrgsetExtendedFG(XtermWidget xw)
957d522f475Smrg{
958d522f475Smrg    int fg = xw->sgr_foreground;
959d522f475Smrg
96020d2c4d2Smrg    if (TScreenOf(xw)->colorAttrMode
961d522f475Smrg	|| (fg < 0)) {
96220d2c4d2Smrg	fg = MapToColorMode(fg, TScreenOf(xw), xw->flags);
963d522f475Smrg    }
964d522f475Smrg
965d522f475Smrg    /* This implements the IBM PC-style convention of 8-colors, with one
966d522f475Smrg     * bit for bold, thus mapping the 0-7 codes to 8-15.  It won't make
967d522f475Smrg     * much sense for 16-color applications, but we keep it to retain
968d522f475Smrg     * compatiblity with ANSI-color applications.
969d522f475Smrg     */
970d522f475Smrg#if OPT_PC_COLORS		/* XXXJTL should be settable at runtime (resource or OSC?) */
97120d2c4d2Smrg    if (TScreenOf(xw)->boldColors
972d522f475Smrg	&& (!xw->sgr_extended)
973d522f475Smrg	&& (fg >= 0)
974d522f475Smrg	&& (fg < 8)
975d522f475Smrg	&& (xw->flags & BOLD))
976d522f475Smrg	fg |= 8;
977d522f475Smrg#endif
978d522f475Smrg
979d522f475Smrg    SGR_Foreground(xw, fg);
980d522f475Smrg}
981d522f475Smrg
982d522f475Smrg/* Invoked after updating inverse flag, computes the extended color
983d522f475Smrg * index to use for background.  (See also 'extract_bg()').
984d522f475Smrg */
985d522f475Smrgstatic void
986d522f475SmrgsetExtendedBG(XtermWidget xw)
987d522f475Smrg{
988d522f475Smrg    int bg = xw->sgr_background;
989d522f475Smrg
99020d2c4d2Smrg    if (TScreenOf(xw)->colorAttrMode
991d522f475Smrg	|| (bg < 0)) {
99220d2c4d2Smrg	if (TScreenOf(xw)->colorRVMode && (xw->flags & INVERSE))
993d522f475Smrg	    bg = COLOR_RV;
994d522f475Smrg    }
995d522f475Smrg
996d522f475Smrg    SGR_Background(xw, bg);
997d522f475Smrg}
998d522f475Smrg
999d522f475Smrgstatic void
1000d522f475Smrgreset_SGR_Foreground(XtermWidget xw)
1001d522f475Smrg{
1002d522f475Smrg    xw->sgr_foreground = -1;
1003d522f475Smrg    xw->sgr_extended = False;
1004d522f475Smrg    setExtendedFG(xw);
1005d522f475Smrg}
1006d522f475Smrg
1007d522f475Smrgstatic void
1008d522f475Smrgreset_SGR_Background(XtermWidget xw)
1009d522f475Smrg{
1010d522f475Smrg    xw->sgr_background = -1;
1011d522f475Smrg    setExtendedBG(xw);
1012d522f475Smrg}
1013d522f475Smrg
1014d522f475Smrgstatic void
1015d522f475Smrgreset_SGR_Colors(XtermWidget xw)
1016d522f475Smrg{
1017d522f475Smrg    reset_SGR_Foreground(xw);
1018d522f475Smrg    reset_SGR_Background(xw);
1019d522f475Smrg}
1020d522f475Smrg#endif /* OPT_ISO_COLORS */
1021d522f475Smrg
1022d522f475Smrgvoid
1023d522f475SmrgresetCharsets(TScreen * screen)
1024d522f475Smrg{
1025d522f475Smrg    TRACE(("resetCharsets\n"));
1026d522f475Smrg
1027d522f475Smrg    screen->gsets[0] = 'B';	/* ASCII_G              */
1028d522f475Smrg    screen->gsets[1] = 'B';	/* ASCII_G              */
1029d522f475Smrg    screen->gsets[2] = 'B';	/* ASCII_G              */
1030d522f475Smrg    screen->gsets[3] = 'B';	/* ASCII_G              */
1031d522f475Smrg
1032d522f475Smrg    screen->curgl = 0;		/* G0 => GL.            */
1033d522f475Smrg    screen->curgr = 2;		/* G2 => GR.            */
1034d522f475Smrg    screen->curss = 0;		/* No single shift.     */
1035d522f475Smrg
1036d522f475Smrg#if OPT_VT52_MODE
1037d522f475Smrg    if (screen->vtXX_level == 0)
1038d522f475Smrg	screen->gsets[1] = '0';	/* Graphics             */
1039d522f475Smrg#endif
1040d522f475Smrg}
1041d522f475Smrg
1042d522f475Smrg/*
1043d522f475Smrg * VT300 and up support three ANSI conformance levels, defined according to
1044d522f475Smrg * the dpANSI X3.134.1 standard.  DEC's manuals equate levels 1 and 2, and
1045d522f475Smrg * are unclear.  This code is written based on the manuals.
1046d522f475Smrg */
1047d522f475Smrgstatic void
1048d522f475Smrgset_ansi_conformance(TScreen * screen, int level)
1049d522f475Smrg{
1050d522f475Smrg    TRACE(("set_ansi_conformance(%d) terminal_id %d, ansi_level %d\n",
1051d522f475Smrg	   level,
1052d522f475Smrg	   screen->terminal_id,
1053d522f475Smrg	   screen->ansi_level));
1054d522f475Smrg    if (screen->vtXX_level >= 3) {
1055d522f475Smrg	switch (screen->ansi_level = level) {
1056d522f475Smrg	case 1:
1057d522f475Smrg	    /* FALLTHRU */
1058d522f475Smrg	case 2:
1059d522f475Smrg	    screen->gsets[0] = 'B';	/* G0 is ASCII */
1060d522f475Smrg	    screen->gsets[1] = 'B';	/* G1 is ISO Latin-1 (FIXME) */
1061d522f475Smrg	    screen->curgl = 0;
1062d522f475Smrg	    screen->curgr = 1;
1063d522f475Smrg	    break;
1064d522f475Smrg	case 3:
1065d522f475Smrg	    screen->gsets[0] = 'B';	/* G0 is ASCII */
1066d522f475Smrg	    screen->curgl = 0;
1067d522f475Smrg	    break;
1068d522f475Smrg	}
1069d522f475Smrg    }
1070d522f475Smrg}
1071d522f475Smrg
1072d522f475Smrg/*
1073d522f475Smrg * Set scrolling margins.  VTxxx terminals require that the top/bottom are
1074d522f475Smrg * different, so we have at least two lines in the scrolling region.
1075d522f475Smrg */
1076d522f475Smrgvoid
1077d522f475Smrgset_tb_margins(TScreen * screen, int top, int bottom)
1078d522f475Smrg{
1079d522f475Smrg    TRACE(("set_tb_margins %d..%d, prior %d..%d\n",
1080d522f475Smrg	   top, bottom,
1081d522f475Smrg	   screen->top_marg,
1082d522f475Smrg	   screen->bot_marg));
1083d522f475Smrg    if (bottom > top) {
1084d522f475Smrg	screen->top_marg = top;
1085d522f475Smrg	screen->bot_marg = bottom;
1086d522f475Smrg    }
1087d522f475Smrg    if (screen->top_marg > screen->max_row)
1088d522f475Smrg	screen->top_marg = screen->max_row;
1089d522f475Smrg    if (screen->bot_marg > screen->max_row)
1090d522f475Smrg	screen->bot_marg = screen->max_row;
1091d522f475Smrg}
1092d522f475Smrg
1093d522f475Smrgvoid
1094d522f475Smrgset_max_col(TScreen * screen, int cols)
1095d522f475Smrg{
1096d522f475Smrg    TRACE(("set_max_col %d, prior %d\n", cols, screen->max_col));
1097d522f475Smrg    if (cols < 0)
1098d522f475Smrg	cols = 0;
1099d522f475Smrg    screen->max_col = cols;
1100d522f475Smrg}
1101d522f475Smrg
1102d522f475Smrgvoid
1103d522f475Smrgset_max_row(TScreen * screen, int rows)
1104d522f475Smrg{
1105d522f475Smrg    TRACE(("set_max_row %d, prior %d\n", rows, screen->max_row));
1106d522f475Smrg    if (rows < 0)
1107d522f475Smrg	rows = 0;
1108d522f475Smrg    screen->max_row = rows;
1109d522f475Smrg}
1110d522f475Smrg
1111d522f475Smrg#if OPT_MOD_FKEYS
1112d522f475Smrgstatic void
1113d522f475Smrgset_mod_fkeys(XtermWidget xw, int which, int what, Bool enabled)
1114d522f475Smrg{
1115d522f475Smrg#define SET_MOD_FKEYS(field) \
1116d522f475Smrg    xw->keyboard.modify_now.field = ((what == DEFAULT) && enabled) \
1117d522f475Smrg				     ? xw->keyboard.modify_1st.field \
1118d522f475Smrg				     : what; \
1119d522f475Smrg    TRACE(("set modify_now.%s to %d\n", #field, \
1120d522f475Smrg	   xw->keyboard.modify_now.field));
1121d522f475Smrg
1122d522f475Smrg    switch (which) {
1123d522f475Smrg    case 1:
1124d522f475Smrg	SET_MOD_FKEYS(cursor_keys);
1125d522f475Smrg	break;
1126d522f475Smrg    case 2:
1127d522f475Smrg	SET_MOD_FKEYS(function_keys);
1128d522f475Smrg	break;
1129d522f475Smrg    case 3:
1130d522f475Smrg	SET_MOD_FKEYS(keypad_keys);
1131d522f475Smrg	break;
1132d522f475Smrg    case 4:
1133d522f475Smrg	SET_MOD_FKEYS(other_keys);
1134d522f475Smrg	break;
1135d522f475Smrg    case 5:
1136d522f475Smrg	SET_MOD_FKEYS(string_keys);
1137d522f475Smrg	break;
1138d522f475Smrg    }
1139d522f475Smrg}
1140d522f475Smrg#endif /* OPT_MOD_FKEYS */
1141d522f475Smrg
1142d522f475Smrg#if OPT_TRACE
1143d522f475Smrg#define WHICH_TABLE(name) if (table == name) result = #name
114420d2c4d2Smrgstatic const char *
1145d522f475Smrgwhich_table(Const PARSE_T * table)
1146d522f475Smrg{
114720d2c4d2Smrg    const char *result = "?";
1148d522f475Smrg    /* *INDENT-OFF* */
1149d522f475Smrg    WHICH_TABLE (ansi_table);
11502eaa94a1Schristos    else WHICH_TABLE (cigtable);
1151d522f475Smrg    else WHICH_TABLE (csi2_table);
1152d522f475Smrg    else WHICH_TABLE (csi_ex_table);
1153d522f475Smrg    else WHICH_TABLE (csi_quo_table);
11542eaa94a1Schristos    else WHICH_TABLE (csi_table);
1155d522f475Smrg    else WHICH_TABLE (dec2_table);
1156d522f475Smrg    else WHICH_TABLE (dec3_table);
11572eaa94a1Schristos    else WHICH_TABLE (dec_table);
1158d522f475Smrg    else WHICH_TABLE (eigtable);
1159d522f475Smrg    else WHICH_TABLE (esc_sp_table);
11602eaa94a1Schristos    else WHICH_TABLE (esc_table);
1161d522f475Smrg    else WHICH_TABLE (scrtable);
11622eaa94a1Schristos    else WHICH_TABLE (scs96table);
1163d522f475Smrg    else WHICH_TABLE (scstable);
1164d522f475Smrg    else WHICH_TABLE (sos_table);
116520d2c4d2Smrg#if OPT_BLINK_CURS
116620d2c4d2Smrg    else WHICH_TABLE (csi_sp_table);
116720d2c4d2Smrg#endif
11682eaa94a1Schristos#if OPT_DEC_LOCATOR
11692eaa94a1Schristos    else WHICH_TABLE (csi_tick_table);
11702eaa94a1Schristos#endif
11712eaa94a1Schristos#if OPT_DEC_RECTOPS
11722eaa94a1Schristos    else WHICH_TABLE (csi_dollar_table);
11732eaa94a1Schristos    else WHICH_TABLE (csi_star_table);
1174492d43a5Smrg    else WHICH_TABLE (csi_dec_dollar_table);
11752eaa94a1Schristos#endif
1176d522f475Smrg#if OPT_WIDE_CHARS
1177d522f475Smrg    else WHICH_TABLE (esc_pct_table);
1178d522f475Smrg#endif
1179d522f475Smrg#if OPT_VT52_MODE
1180d522f475Smrg    else WHICH_TABLE (vt52_table);
1181d522f475Smrg    else WHICH_TABLE (vt52_esc_table);
1182d522f475Smrg    else WHICH_TABLE (vt52_ignore_table);
1183d522f475Smrg#endif
1184d522f475Smrg    /* *INDENT-ON* */
1185d522f475Smrg
1186d522f475Smrg    return result;
1187d522f475Smrg}
1188d522f475Smrg#endif
1189d522f475Smrg
1190d522f475Smrg	/* allocate larger buffer if needed/possible */
1191d522f475Smrg#define SafeAlloc(type, area, used, size) \
1192d522f475Smrg		type *new_string = area; \
119320d2c4d2Smrg		size_t new_length = size; \
1194d522f475Smrg		if (new_length == 0) { \
1195d522f475Smrg		    new_length = 256; \
1196d522f475Smrg		    new_string = TypeMallocN(type, new_length); \
1197d522f475Smrg		} else if (used+1 >= new_length) { \
1198d522f475Smrg		    new_length = size * 2; \
1199d522f475Smrg		    new_string = TypeMallocN(type, new_length); \
1200d522f475Smrg		    if (new_string != 0 \
1201d522f475Smrg		     && area != 0 \
1202d522f475Smrg		     && used != 0) \
1203d522f475Smrg			memcpy(new_string, area, used * sizeof(type)); \
1204d522f475Smrg		}
1205d522f475Smrg
1206d522f475Smrg#define WriteNow() {						\
1207d522f475Smrg	    unsigned single = 0;				\
1208d522f475Smrg								\
1209d522f475Smrg	    if (screen->curss) {				\
1210d522f475Smrg		dotext(xw,					\
1211d522f475Smrg		       screen->gsets[(int) (screen->curss)],	\
1212956cc18dSsnj		       sp->print_area,				\
1213956cc18dSsnj		       (Cardinal) 1);				\
1214d522f475Smrg		screen->curss = 0;				\
1215d522f475Smrg		single++;					\
1216d522f475Smrg	    }							\
1217956cc18dSsnj	    if (sp->print_used > single) {			\
1218d522f475Smrg		dotext(xw,					\
1219d522f475Smrg		       screen->gsets[(int) (screen->curgl)],	\
1220956cc18dSsnj		       sp->print_area + single,			\
1221956cc18dSsnj		       (Cardinal) (sp->print_used - single));	\
1222d522f475Smrg	    }							\
1223956cc18dSsnj	    sp->print_used = 0;					\
1224d522f475Smrg	}							\
1225d522f475Smrg
1226d522f475Smrgstruct ParseState {
1227d522f475Smrg#if OPT_VT52_MODE
1228d522f475Smrg    Bool vt52_cup;
1229d522f475Smrg#endif
1230d522f475Smrg    Const PARSE_T *groundtable;
1231d522f475Smrg    Const PARSE_T *parsestate;
1232d522f475Smrg    int scstype;
12332eaa94a1Schristos    int scssize;
1234d522f475Smrg    Bool private_function;	/* distinguish private-mode from standard */
1235d522f475Smrg    int string_mode;		/* nonzero iff we're processing a string */
1236d522f475Smrg    int lastchar;		/* positive iff we had a graphic character */
1237d522f475Smrg    int nextstate;
1238d522f475Smrg#if OPT_WIDE_CHARS
1239d522f475Smrg    int last_was_wide;
1240d522f475Smrg#endif
1241956cc18dSsnj    /* Buffer for processing printable text */
1242956cc18dSsnj    IChar *print_area;
1243956cc18dSsnj    size_t print_size;
1244956cc18dSsnj    size_t print_used;
1245956cc18dSsnj    /* Buffer for processing strings (e.g., OSC ... ST) */
1246956cc18dSsnj    Char *string_area;
1247956cc18dSsnj    size_t string_size;
1248956cc18dSsnj    size_t string_used;
1249d522f475Smrg};
1250d522f475Smrg
1251d522f475Smrgstatic struct ParseState myState;
1252d522f475Smrg
12532eaa94a1Schristosstatic void
12542eaa94a1Schristosinit_groundtable(TScreen * screen, struct ParseState *sp)
12552eaa94a1Schristos{
1256956cc18dSsnj    (void) screen;
1257956cc18dSsnj
12582eaa94a1Schristos#if OPT_VT52_MODE
12592eaa94a1Schristos    if (!(screen->vtXX_level)) {
12602eaa94a1Schristos	sp->groundtable = vt52_table;
12612eaa94a1Schristos    } else if (screen->terminal_id >= 100)
12622eaa94a1Schristos#endif
12632eaa94a1Schristos    {
12642eaa94a1Schristos	sp->groundtable = ansi_table;
12652eaa94a1Schristos    }
12662eaa94a1Schristos}
12672eaa94a1Schristos
12682eaa94a1Schristosstatic void
12692eaa94a1Schristosselect_charset(struct ParseState *sp, int type, int size)
12702eaa94a1Schristos{
12712eaa94a1Schristos    TRACE(("select_charset %#x %d\n", type, size));
12722eaa94a1Schristos    sp->scstype = type;
12732eaa94a1Schristos    sp->scssize = size;
12742eaa94a1Schristos    if (size == 94) {
12752eaa94a1Schristos	sp->parsestate = scstable;
12762eaa94a1Schristos    } else {
12772eaa94a1Schristos	sp->parsestate = scs96table;
12782eaa94a1Schristos    }
12792eaa94a1Schristos}
12802eaa94a1Schristos
128120d2c4d2Smrgstatic int
128220d2c4d2Smrgzero_if_default(int which)
128320d2c4d2Smrg{
128420d2c4d2Smrg    int result = (nparam > which) ? param[which] : 0;
128520d2c4d2Smrg    if (result <= 0)
128620d2c4d2Smrg	result = 0;
128720d2c4d2Smrg    return result;
128820d2c4d2Smrg}
128920d2c4d2Smrg
129020d2c4d2Smrgstatic int
129120d2c4d2Smrgone_if_default(int which)
129220d2c4d2Smrg{
129320d2c4d2Smrg    int result = (nparam > which) ? param[which] : 0;
129420d2c4d2Smrg    if (result <= 0)
129520d2c4d2Smrg	result = 1;
129620d2c4d2Smrg    return result;
129720d2c4d2Smrg}
129820d2c4d2Smrg
129920d2c4d2Smrg#if OPT_C1_PRINT || OPT_WIDE_CHARS
130020d2c4d2Smrg#define ParseSOS(screen) ((screen)->c1_printable == 0)
130120d2c4d2Smrg#else
130220d2c4d2Smrg#define ParseSOS(screen) 0
130320d2c4d2Smrg#endif
130420d2c4d2Smrg
130520d2c4d2Smrg#define ResetState(sp) (sp)->parsestate = (sp)->groundtable
130620d2c4d2Smrg
130720d2c4d2Smrgstatic void
130820d2c4d2Smrgillegal_parse(XtermWidget xw, unsigned c, struct ParseState *sp)
130920d2c4d2Smrg{
131020d2c4d2Smrg    ResetState(sp);
131120d2c4d2Smrg    sp->nextstate = sp->parsestate[E2A(c)];
131220d2c4d2Smrg    Bell(xw, XkbBI_MinorError, 0);
131320d2c4d2Smrg}
131420d2c4d2Smrg
1315d522f475Smrgstatic Boolean
1316d522f475Smrgdoparsing(XtermWidget xw, unsigned c, struct ParseState *sp)
1317d522f475Smrg{
131820d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
1319d522f475Smrg    int row;
1320d522f475Smrg    int col;
1321d522f475Smrg    int top;
1322d522f475Smrg    int bot;
1323d522f475Smrg    int count;
1324d522f475Smrg    int laststate;
1325d522f475Smrg    int thischar = -1;
1326d522f475Smrg    XTermRect myRect;
1327d522f475Smrg
1328d522f475Smrg    do {
1329d522f475Smrg#if OPT_WIDE_CHARS
1330956cc18dSsnj	int this_is_wide = 0;
1331d522f475Smrg
1332d522f475Smrg	/*
1333d522f475Smrg	 * Handle zero-width combining characters.  Make it faster by noting
1334d522f475Smrg	 * that according to the Unicode charts, the majority of Western
1335d522f475Smrg	 * character sets do not use this feature.  There are some unassigned
1336d522f475Smrg	 * codes at 0x242, but no zero-width characters until past 0x300.
1337d522f475Smrg	 */
1338d522f475Smrg	if (c >= 0x300 && screen->wide_chars
1339d522f475Smrg	    && my_wcwidth((int) c) == 0
1340d522f475Smrg	    && !isWideControl(c)) {
1341d522f475Smrg	    int prev, precomposed;
1342d522f475Smrg
1343d522f475Smrg	    WriteNow();
1344d522f475Smrg
13452eaa94a1Schristos	    prev = (int) XTERM_CELL(screen->last_written_row,
13462eaa94a1Schristos				    screen->last_written_col);
1347d522f475Smrg	    precomposed = do_precomposition(prev, (int) c);
1348d522f475Smrg	    TRACE(("do_precomposition (U+%04X [%d], U+%04X [%d]) -> U+%04X [%d]\n",
1349d522f475Smrg		   prev, my_wcwidth(prev),
1350d522f475Smrg		   (int) c, my_wcwidth((int) c),
1351d522f475Smrg		   precomposed, my_wcwidth(precomposed)));
1352d522f475Smrg
1353d522f475Smrg	    /* substitute combined character with precomposed character
1354d522f475Smrg	     * only if it does not change the width of the base character
1355d522f475Smrg	     */
1356d522f475Smrg	    if (precomposed != -1 && my_wcwidth(precomposed) == my_wcwidth(prev)) {
1357d522f475Smrg		putXtermCell(screen,
1358d522f475Smrg			     screen->last_written_row,
1359d522f475Smrg			     screen->last_written_col, precomposed);
1360d522f475Smrg	    } else {
1361d522f475Smrg		addXtermCombining(screen,
1362d522f475Smrg				  screen->last_written_row,
1363d522f475Smrg				  screen->last_written_col, c);
1364d522f475Smrg	    }
1365d522f475Smrg
1366d522f475Smrg	    if (!screen->scroll_amt)
1367d522f475Smrg		ScrnUpdate(xw,
1368d522f475Smrg			   screen->last_written_row,
1369d522f475Smrg			   screen->last_written_col, 1, 1, 1);
1370d522f475Smrg	    continue;
1371d522f475Smrg	}
1372d522f475Smrg#endif
1373d522f475Smrg
1374d522f475Smrg	/* Intercept characters for printer controller mode */
1375d522f475Smrg	if (screen->printer_controlmode == 2) {
1376956cc18dSsnj	    if ((c = (unsigned) xtermPrinterControl(xw, (int) c)) == 0)
1377d522f475Smrg		continue;
1378d522f475Smrg	}
1379d522f475Smrg
1380d522f475Smrg	/*
1381d522f475Smrg	 * VT52 is a little ugly in the one place it has a parameterized
1382d522f475Smrg	 * control sequence, since the parameter falls after the character
1383d522f475Smrg	 * that denotes the type of sequence.
1384d522f475Smrg	 */
1385d522f475Smrg#if OPT_VT52_MODE
1386d522f475Smrg	if (sp->vt52_cup) {
1387d522f475Smrg	    if (nparam < NPARAM)
13882eaa94a1Schristos		param[nparam++] = (int) (c & 0x7f) - 32;
1389d522f475Smrg	    if (nparam < 2)
1390d522f475Smrg		continue;
1391d522f475Smrg	    sp->vt52_cup = False;
1392d522f475Smrg	    if ((row = param[0]) < 0)
1393d522f475Smrg		row = 0;
1394d522f475Smrg	    if ((col = param[1]) < 0)
1395d522f475Smrg		col = 0;
1396d522f475Smrg	    CursorSet(screen, row, col, xw->flags);
1397d522f475Smrg	    sp->parsestate = vt52_table;
1398d522f475Smrg	    param[0] = 0;
1399d522f475Smrg	    param[1] = 0;
1400d522f475Smrg	    continue;
1401d522f475Smrg	}
1402d522f475Smrg#endif
1403d522f475Smrg
1404d522f475Smrg	laststate = sp->nextstate;
14052eaa94a1Schristos	if (c == ANSI_DEL
14062eaa94a1Schristos	    && sp->parsestate == sp->groundtable
14072eaa94a1Schristos	    && sp->scssize == 96
14082eaa94a1Schristos	    && sp->scstype != 0) {
14092eaa94a1Schristos	    /*
14102eaa94a1Schristos	     * Handle special case of shifts for 96-character sets by checking
14112eaa94a1Schristos	     * if we have a DEL.  The other special case for SPACE will always
14122eaa94a1Schristos	     * be printable.
14132eaa94a1Schristos	     */
14142eaa94a1Schristos	    sp->nextstate = CASE_PRINT;
14152eaa94a1Schristos	} else
1416d522f475Smrg#if OPT_WIDE_CHARS
1417d522f475Smrg	if (c > 255) {
14182eaa94a1Schristos	    /*
14192eaa94a1Schristos	     * The parsing tables all have 256 entries.  If we're supporting
14202eaa94a1Schristos	     * wide characters, we handle them by treating them the same as
14212eaa94a1Schristos	     * printing characters.
14222eaa94a1Schristos	     */
1423d522f475Smrg	    if (sp->parsestate == sp->groundtable) {
1424d522f475Smrg		sp->nextstate = CASE_PRINT;
1425d522f475Smrg	    } else if (sp->parsestate == sos_table) {
1426d522f475Smrg		c &= 0xffff;
1427d522f475Smrg		if (c > 255) {
1428d522f475Smrg		    TRACE(("Found code > 255 while in SOS state: %04X\n", c));
1429d522f475Smrg		    c = '?';
1430d522f475Smrg		}
1431d522f475Smrg	    } else {
1432d522f475Smrg		sp->nextstate = CASE_GROUND_STATE;
1433d522f475Smrg	    }
1434d522f475Smrg	} else
1435d522f475Smrg#endif
1436d522f475Smrg	    sp->nextstate = sp->parsestate[E2A(c)];
1437d522f475Smrg
1438d522f475Smrg#if OPT_BROKEN_OSC
1439d522f475Smrg	/*
1440d522f475Smrg	 * Linux console palette escape sequences start with an OSC, but do
1441d522f475Smrg	 * not terminate correctly.  Some scripts do not check before writing
1442d522f475Smrg	 * them, making xterm appear to hang (it's awaiting a valid string
1443d522f475Smrg	 * terminator).  Just ignore these if we see them - there's no point
1444d522f475Smrg	 * in emulating bad code.
1445d522f475Smrg	 */
1446d522f475Smrg	if (screen->brokenLinuxOSC
1447d522f475Smrg	    && sp->parsestate == sos_table) {
1448956cc18dSsnj	    if (sp->string_used) {
1449956cc18dSsnj		switch (sp->string_area[0]) {
1450d522f475Smrg		case 'P':
1451956cc18dSsnj		    if (sp->string_used <= 7)
1452d522f475Smrg			break;
1453d522f475Smrg		    /* FALLTHRU */
1454d522f475Smrg		case 'R':
145520d2c4d2Smrg		    illegal_parse(xw, c, sp);
1456d522f475Smrg		    TRACE(("Reset to ground state (brokenLinuxOSC)\n"));
1457d522f475Smrg		    break;
1458d522f475Smrg		}
1459d522f475Smrg	    }
1460d522f475Smrg	}
1461d522f475Smrg#endif
1462d522f475Smrg
1463d522f475Smrg#if OPT_BROKEN_ST
1464d522f475Smrg	/*
1465d522f475Smrg	 * Before patch #171, carriage control embedded within an OSC string
1466d522f475Smrg	 * would terminate it.  Some (buggy, of course) applications rely on
1467d522f475Smrg	 * this behavior.  Accommodate them by allowing one to compile xterm
1468d522f475Smrg	 * and emulate the old behavior.
1469d522f475Smrg	 */
1470d522f475Smrg	if (screen->brokenStringTerm
1471d522f475Smrg	    && sp->parsestate == sos_table
1472d522f475Smrg	    && c < 32) {
1473d522f475Smrg	    switch (c) {
147420d2c4d2Smrg	    case ANSI_EOT:	/* FALLTHRU */
147520d2c4d2Smrg	    case ANSI_BS:	/* FALLTHRU */
147620d2c4d2Smrg	    case ANSI_HT:	/* FALLTHRU */
147720d2c4d2Smrg	    case ANSI_LF:	/* FALLTHRU */
147820d2c4d2Smrg	    case ANSI_VT:	/* FALLTHRU */
147920d2c4d2Smrg	    case ANSI_FF:	/* FALLTHRU */
148020d2c4d2Smrg	    case ANSI_CR:	/* FALLTHRU */
148120d2c4d2Smrg	    case ANSI_SO:	/* FALLTHRU */
148220d2c4d2Smrg	    case ANSI_SI:	/* FALLTHRU */
148320d2c4d2Smrg	    case ANSI_XON:	/* FALLTHRU */
148420d2c4d2Smrg	    case ANSI_CAN:
148520d2c4d2Smrg		illegal_parse(xw, c, sp);
1486d522f475Smrg		TRACE(("Reset to ground state (brokenStringTerm)\n"));
1487d522f475Smrg		break;
1488d522f475Smrg	    }
1489d522f475Smrg	}
1490d522f475Smrg#endif
1491d522f475Smrg
1492d522f475Smrg#if OPT_C1_PRINT
1493d522f475Smrg	/*
1494d522f475Smrg	 * This is not completely foolproof, but will allow an application
1495d522f475Smrg	 * with values in the C1 range to use them as printable characters,
1496d522f475Smrg	 * provided that they are not intermixed with an escape sequence.
1497d522f475Smrg	 */
1498d522f475Smrg	if (screen->c1_printable
1499d522f475Smrg	    && (c >= 128 && c < 160)) {
1500d522f475Smrg	    sp->nextstate = (sp->parsestate == esc_table
1501d522f475Smrg			     ? CASE_ESC_IGNORE
1502d522f475Smrg			     : sp->parsestate[E2A(160)]);
1503d522f475Smrg	}
1504d522f475Smrg#endif
1505d522f475Smrg
1506d522f475Smrg#if OPT_WIDE_CHARS
1507d522f475Smrg	/*
1508d522f475Smrg	 * If we have a C1 code and the c1_printable flag is not set, simply
1509d522f475Smrg	 * ignore it when it was translated from UTF-8.  That is because the
1510d522f475Smrg	 * value could not have been present as-is in the UTF-8.
1511d522f475Smrg	 *
1512d522f475Smrg	 * To see that CASE_IGNORE is a consistent value, note that it is
1513d522f475Smrg	 * always used for NUL and other uninteresting C0 controls.
1514d522f475Smrg	 */
1515d522f475Smrg#if OPT_C1_PRINT
1516d522f475Smrg	if (!screen->c1_printable)
1517d522f475Smrg#endif
1518d522f475Smrg	    if (screen->wide_chars
1519d522f475Smrg		&& (c >= 128 && c < 160)) {
1520d522f475Smrg		sp->nextstate = CASE_IGNORE;
1521d522f475Smrg	    }
1522d522f475Smrg
1523d522f475Smrg	/*
1524d522f475Smrg	 * If this character is a different width than the last one, put the
1525d522f475Smrg	 * previous text into the buffer and draw it now.
1526d522f475Smrg	 */
1527956cc18dSsnj	this_is_wide = isWide((int) c);
1528956cc18dSsnj	if (this_is_wide != sp->last_was_wide) {
1529d522f475Smrg	    WriteNow();
1530d522f475Smrg	}
1531d522f475Smrg#endif
1532d522f475Smrg
1533d522f475Smrg	/*
1534d522f475Smrg	 * Accumulate string for printable text.  This may be 8/16-bit
1535d522f475Smrg	 * characters.
1536d522f475Smrg	 */
1537d522f475Smrg	if (sp->nextstate == CASE_PRINT) {
1538956cc18dSsnj	    SafeAlloc(IChar, sp->print_area, sp->print_used, sp->print_size);
1539d522f475Smrg	    if (new_string == 0) {
1540d522f475Smrg		fprintf(stderr,
154120d2c4d2Smrg			"Cannot allocate %lu bytes for printable text\n",
154220d2c4d2Smrg			(unsigned long) new_length);
1543d522f475Smrg		continue;
1544d522f475Smrg	    }
1545d522f475Smrg#if OPT_VT52_MODE
1546d522f475Smrg	    /*
1547d522f475Smrg	     * Strip output text to 7-bits for VT52.  We should do this for
1548d522f475Smrg	     * VT100 also (which is a 7-bit device), but xterm has been
1549d522f475Smrg	     * doing this for so long we shouldn't change this behavior.
1550d522f475Smrg	     */
1551d522f475Smrg	    if (screen->vtXX_level < 1)
1552d522f475Smrg		c &= 0x7f;
1553d522f475Smrg#endif
1554956cc18dSsnj	    sp->print_area = new_string;
1555956cc18dSsnj	    sp->print_size = new_length;
1556956cc18dSsnj	    sp->print_area[sp->print_used++] = (IChar) c;
15572eaa94a1Schristos	    sp->lastchar = thischar = (int) c;
1558d522f475Smrg#if OPT_WIDE_CHARS
1559956cc18dSsnj	    sp->last_was_wide = this_is_wide;
1560d522f475Smrg#endif
1561d522f475Smrg	    if (morePtyData(screen, VTbuffer)) {
1562d522f475Smrg		continue;
1563d522f475Smrg	    }
1564d522f475Smrg	}
1565d522f475Smrg
1566d522f475Smrg	if (sp->nextstate == CASE_PRINT
1567956cc18dSsnj	    || (laststate == CASE_PRINT && sp->print_used)) {
1568d522f475Smrg	    WriteNow();
1569d522f475Smrg	}
1570d522f475Smrg
1571d522f475Smrg	/*
1572d522f475Smrg	 * Accumulate string for APC, DCS, PM, OSC, SOS controls
1573d522f475Smrg	 * This should always be 8-bit characters.
1574d522f475Smrg	 */
1575d522f475Smrg	if (sp->parsestate == sos_table) {
1576956cc18dSsnj	    SafeAlloc(Char, sp->string_area, sp->string_used, sp->string_size);
1577d522f475Smrg	    if (new_string == 0) {
1578d522f475Smrg		fprintf(stderr,
157920d2c4d2Smrg			"Cannot allocate %lu bytes for string mode %d\n",
158020d2c4d2Smrg			(unsigned long) new_length, sp->string_mode);
1581d522f475Smrg		continue;
1582d522f475Smrg	    }
1583d522f475Smrg#if OPT_WIDE_CHARS
1584d522f475Smrg	    /*
1585d522f475Smrg	     * We cannot display codes above 255, but let's try to
1586d522f475Smrg	     * accommodate the application a little by not aborting the
1587d522f475Smrg	     * string.
1588d522f475Smrg	     */
1589d522f475Smrg	    if ((c & 0xffff) > 255) {
1590d522f475Smrg		sp->nextstate = CASE_PRINT;
1591d522f475Smrg		c = '?';
1592d522f475Smrg	    }
1593d522f475Smrg#endif
1594956cc18dSsnj	    sp->string_area = new_string;
1595956cc18dSsnj	    sp->string_size = new_length;
1596956cc18dSsnj	    sp->string_area[(sp->string_used)++] = CharOf(c);
1597d522f475Smrg	} else if (sp->parsestate != esc_table) {
1598d522f475Smrg	    /* if we were accumulating, we're not any more */
1599d522f475Smrg	    sp->string_mode = 0;
1600956cc18dSsnj	    sp->string_used = 0;
1601d522f475Smrg	}
1602d522f475Smrg
1603d522f475Smrg	TRACE(("parse %04X -> %d %s\n", c, sp->nextstate, which_table(sp->parsestate)));
1604d522f475Smrg
1605d522f475Smrg	switch (sp->nextstate) {
1606d522f475Smrg	case CASE_PRINT:
1607d522f475Smrg	    TRACE(("CASE_PRINT - printable characters\n"));
1608d522f475Smrg	    break;
1609d522f475Smrg
1610d522f475Smrg	case CASE_GROUND_STATE:
1611d522f475Smrg	    TRACE(("CASE_GROUND_STATE - exit ignore mode\n"));
161220d2c4d2Smrg	    ResetState(sp);
1613d522f475Smrg	    break;
1614d522f475Smrg
1615d522f475Smrg	case CASE_IGNORE:
1616d522f475Smrg	    TRACE(("CASE_IGNORE - Ignore character %02X\n", c));
1617d522f475Smrg	    break;
1618d522f475Smrg
1619d522f475Smrg	case CASE_ENQ:
1620d522f475Smrg	    TRACE(("CASE_ENQ - answerback\n"));
1621d522f475Smrg	    for (count = 0; screen->answer_back[count] != 0; count++)
1622d522f475Smrg		unparseputc(xw, screen->answer_back[count]);
1623d522f475Smrg	    unparse_end(xw);
1624d522f475Smrg	    break;
1625d522f475Smrg
1626d522f475Smrg	case CASE_BELL:
1627d522f475Smrg	    TRACE(("CASE_BELL - bell\n"));
1628d522f475Smrg	    if (sp->string_mode == ANSI_OSC) {
1629956cc18dSsnj		if (sp->string_used)
1630956cc18dSsnj		    sp->string_area[--(sp->string_used)] = '\0';
1631956cc18dSsnj		do_osc(xw, sp->string_area, sp->string_used, (int) c);
163220d2c4d2Smrg		ResetState(sp);
1633d522f475Smrg	    } else {
1634d522f475Smrg		/* bell */
163520d2c4d2Smrg		Bell(xw, XkbBI_TerminalBell, 0);
1636d522f475Smrg	    }
1637d522f475Smrg	    break;
1638d522f475Smrg
1639d522f475Smrg	case CASE_BS:
1640d522f475Smrg	    TRACE(("CASE_BS - backspace\n"));
1641d522f475Smrg	    CursorBack(xw, 1);
1642d522f475Smrg	    break;
1643d522f475Smrg
1644d522f475Smrg	case CASE_CR:
1645d522f475Smrg	    /* CR */
1646d522f475Smrg	    CarriageReturn(screen);
1647d522f475Smrg	    break;
1648d522f475Smrg
1649d522f475Smrg	case CASE_ESC:
1650d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1651d522f475Smrg		sp->parsestate = vt52_esc_table;
1652d522f475Smrg		break;
1653d522f475Smrg	    });
1654d522f475Smrg	    sp->parsestate = esc_table;
1655d522f475Smrg	    break;
1656d522f475Smrg
1657d522f475Smrg#if OPT_VT52_MODE
1658d522f475Smrg	case CASE_VT52_CUP:
1659d522f475Smrg	    TRACE(("CASE_VT52_CUP - VT52 cursor addressing\n"));
1660d522f475Smrg	    sp->vt52_cup = True;
1661d522f475Smrg	    nparam = 0;
1662d522f475Smrg	    break;
1663d522f475Smrg
1664d522f475Smrg	case CASE_VT52_IGNORE:
1665d522f475Smrg	    TRACE(("CASE_VT52_IGNORE - VT52 ignore-character\n"));
1666d522f475Smrg	    sp->parsestate = vt52_ignore_table;
1667d522f475Smrg	    break;
1668d522f475Smrg#endif
1669d522f475Smrg
1670d522f475Smrg	case CASE_VMOT:
1671d522f475Smrg	    /*
1672d522f475Smrg	     * form feed, line feed, vertical tab
1673d522f475Smrg	     */
1674956cc18dSsnj	    xtermAutoPrint(xw, c);
1675d522f475Smrg	    xtermIndex(xw, 1);
1676d522f475Smrg	    if (xw->flags & LINEFEED)
1677d522f475Smrg		CarriageReturn(screen);
1678d522f475Smrg	    else
1679d522f475Smrg		do_xevents();
1680d522f475Smrg	    break;
1681d522f475Smrg
1682d522f475Smrg	case CASE_CBT:
1683d522f475Smrg	    /* cursor backward tabulation */
1684d522f475Smrg	    if ((count = param[0]) == DEFAULT)
1685d522f475Smrg		count = 1;
1686d522f475Smrg	    while ((count-- > 0)
1687d522f475Smrg		   && (TabToPrevStop(xw))) ;
168820d2c4d2Smrg	    ResetState(sp);
1689d522f475Smrg	    break;
1690d522f475Smrg
1691d522f475Smrg	case CASE_CHT:
1692d522f475Smrg	    /* cursor forward tabulation */
1693d522f475Smrg	    if ((count = param[0]) == DEFAULT)
1694d522f475Smrg		count = 1;
1695d522f475Smrg	    while ((count-- > 0)
1696d522f475Smrg		   && (TabToNextStop(xw))) ;
169720d2c4d2Smrg	    ResetState(sp);
1698d522f475Smrg	    break;
1699d522f475Smrg
1700d522f475Smrg	case CASE_TAB:
1701d522f475Smrg	    /* tab */
1702d522f475Smrg	    TabToNextStop(xw);
1703d522f475Smrg	    break;
1704d522f475Smrg
1705d522f475Smrg	case CASE_SI:
1706d522f475Smrg	    screen->curgl = 0;
1707d522f475Smrg	    if_OPT_VT52_MODE(screen, {
170820d2c4d2Smrg		ResetState(sp);
1709d522f475Smrg	    });
1710d522f475Smrg	    break;
1711d522f475Smrg
1712d522f475Smrg	case CASE_SO:
1713d522f475Smrg	    screen->curgl = 1;
1714d522f475Smrg	    if_OPT_VT52_MODE(screen, {
171520d2c4d2Smrg		ResetState(sp);
1716d522f475Smrg	    });
1717d522f475Smrg	    break;
1718d522f475Smrg
1719d522f475Smrg	case CASE_DECDHL:
1720d522f475Smrg	    xterm_DECDHL(xw, c == '3');
172120d2c4d2Smrg	    ResetState(sp);
1722d522f475Smrg	    break;
1723d522f475Smrg
1724d522f475Smrg	case CASE_DECSWL:
1725d522f475Smrg	    xterm_DECSWL(xw);
172620d2c4d2Smrg	    ResetState(sp);
1727d522f475Smrg	    break;
1728d522f475Smrg
1729d522f475Smrg	case CASE_DECDWL:
1730d522f475Smrg	    xterm_DECDWL(xw);
173120d2c4d2Smrg	    ResetState(sp);
1732d522f475Smrg	    break;
1733d522f475Smrg
1734d522f475Smrg	case CASE_SCR_STATE:
1735d522f475Smrg	    /* enter scr state */
1736d522f475Smrg	    sp->parsestate = scrtable;
1737d522f475Smrg	    break;
1738d522f475Smrg
1739d522f475Smrg	case CASE_SCS0_STATE:
1740d522f475Smrg	    /* enter scs state 0 */
17412eaa94a1Schristos	    select_charset(sp, 0, 94);
1742d522f475Smrg	    break;
1743d522f475Smrg
1744d522f475Smrg	case CASE_SCS1_STATE:
1745d522f475Smrg	    /* enter scs state 1 */
17462eaa94a1Schristos	    select_charset(sp, 1, 94);
1747d522f475Smrg	    break;
1748d522f475Smrg
1749d522f475Smrg	case CASE_SCS2_STATE:
1750d522f475Smrg	    /* enter scs state 2 */
17512eaa94a1Schristos	    select_charset(sp, 2, 94);
1752d522f475Smrg	    break;
1753d522f475Smrg
1754d522f475Smrg	case CASE_SCS3_STATE:
1755d522f475Smrg	    /* enter scs state 3 */
17562eaa94a1Schristos	    select_charset(sp, 3, 94);
17572eaa94a1Schristos	    break;
17582eaa94a1Schristos
17592eaa94a1Schristos	case CASE_SCS1A_STATE:
17602eaa94a1Schristos	    /* enter scs state 1 */
17612eaa94a1Schristos	    select_charset(sp, 1, 96);
17622eaa94a1Schristos	    break;
17632eaa94a1Schristos
17642eaa94a1Schristos	case CASE_SCS2A_STATE:
17652eaa94a1Schristos	    /* enter scs state 2 */
17662eaa94a1Schristos	    select_charset(sp, 2, 96);
17672eaa94a1Schristos	    break;
17682eaa94a1Schristos
17692eaa94a1Schristos	case CASE_SCS3A_STATE:
17702eaa94a1Schristos	    /* enter scs state 3 */
17712eaa94a1Schristos	    select_charset(sp, 3, 96);
1772d522f475Smrg	    break;
1773d522f475Smrg
1774d522f475Smrg	case CASE_ESC_IGNORE:
1775d522f475Smrg	    /* unknown escape sequence */
1776d522f475Smrg	    sp->parsestate = eigtable;
1777d522f475Smrg	    break;
1778d522f475Smrg
1779d522f475Smrg	case CASE_ESC_DIGIT:
1780d522f475Smrg	    /* digit in csi or dec mode */
178120d2c4d2Smrg	    if (nparam > 0) {
178220d2c4d2Smrg		if ((row = param[nparam - 1]) == DEFAULT)
178320d2c4d2Smrg		    row = 0;
178420d2c4d2Smrg		param[nparam - 1] = (10 * row) + ((int) c - '0');
178520d2c4d2Smrg		if (param[nparam - 1] > 65535)
178620d2c4d2Smrg		    param[nparam - 1] = 65535;
178720d2c4d2Smrg		if (sp->parsestate == csi_table)
178820d2c4d2Smrg		    sp->parsestate = csi2_table;
178920d2c4d2Smrg	    }
1790d522f475Smrg	    break;
1791d522f475Smrg
1792d522f475Smrg	case CASE_ESC_SEMI:
1793d522f475Smrg	    /* semicolon in csi or dec mode */
1794d522f475Smrg	    if (nparam < NPARAM)
1795d522f475Smrg		param[nparam++] = DEFAULT;
1796d522f475Smrg	    if (sp->parsestate == csi_table)
1797d522f475Smrg		sp->parsestate = csi2_table;
1798d522f475Smrg	    break;
1799d522f475Smrg
1800d522f475Smrg	case CASE_DEC_STATE:
1801d522f475Smrg	    /* enter dec mode */
1802d522f475Smrg	    sp->parsestate = dec_table;
1803d522f475Smrg	    break;
1804d522f475Smrg
1805d522f475Smrg	case CASE_DEC2_STATE:
1806d522f475Smrg	    /* enter dec2 mode */
1807d522f475Smrg	    sp->parsestate = dec2_table;
1808d522f475Smrg	    break;
1809d522f475Smrg
1810d522f475Smrg	case CASE_DEC3_STATE:
1811d522f475Smrg	    /* enter dec3 mode */
1812d522f475Smrg	    sp->parsestate = dec3_table;
1813d522f475Smrg	    break;
1814d522f475Smrg
1815d522f475Smrg	case CASE_ICH:
1816d522f475Smrg	    TRACE(("CASE_ICH - insert char\n"));
1817d522f475Smrg	    if ((row = param[0]) < 1)
1818d522f475Smrg		row = 1;
1819d522f475Smrg	    InsertChar(xw, (unsigned) row);
182020d2c4d2Smrg	    ResetState(sp);
1821d522f475Smrg	    break;
1822d522f475Smrg
1823d522f475Smrg	case CASE_CUU:
1824d522f475Smrg	    TRACE(("CASE_CUU - cursor up\n"));
1825d522f475Smrg	    if ((row = param[0]) < 1)
1826d522f475Smrg		row = 1;
1827d522f475Smrg	    CursorUp(screen, row);
182820d2c4d2Smrg	    ResetState(sp);
1829d522f475Smrg	    break;
1830d522f475Smrg
1831d522f475Smrg	case CASE_CUD:
1832d522f475Smrg	    TRACE(("CASE_CUD - cursor down\n"));
1833d522f475Smrg	    if ((row = param[0]) < 1)
1834d522f475Smrg		row = 1;
1835d522f475Smrg	    CursorDown(screen, row);
183620d2c4d2Smrg	    ResetState(sp);
1837d522f475Smrg	    break;
1838d522f475Smrg
1839d522f475Smrg	case CASE_CUF:
1840d522f475Smrg	    TRACE(("CASE_CUF - cursor forward\n"));
1841d522f475Smrg	    if ((col = param[0]) < 1)
1842d522f475Smrg		col = 1;
1843d522f475Smrg	    CursorForward(screen, col);
184420d2c4d2Smrg	    ResetState(sp);
1845d522f475Smrg	    break;
1846d522f475Smrg
1847d522f475Smrg	case CASE_CUB:
1848d522f475Smrg	    TRACE(("CASE_CUB - cursor backward\n"));
1849d522f475Smrg	    if ((col = param[0]) < 1)
1850d522f475Smrg		col = 1;
1851d522f475Smrg	    CursorBack(xw, col);
185220d2c4d2Smrg	    ResetState(sp);
1853d522f475Smrg	    break;
1854d522f475Smrg
1855d522f475Smrg	case CASE_CUP:
1856d522f475Smrg	    TRACE(("CASE_CUP - cursor position\n"));
1857d522f475Smrg	    if_OPT_XMC_GLITCH(screen, {
1858d522f475Smrg		Jump_XMC(xw);
1859d522f475Smrg	    });
1860d522f475Smrg	    if ((row = param[0]) < 1)
1861d522f475Smrg		row = 1;
1862d522f475Smrg	    if (nparam < 2 || (col = param[1]) < 1)
1863d522f475Smrg		col = 1;
1864d522f475Smrg	    CursorSet(screen, row - 1, col - 1, xw->flags);
186520d2c4d2Smrg	    ResetState(sp);
1866d522f475Smrg	    break;
1867d522f475Smrg
1868d522f475Smrg	case CASE_VPA:
1869d522f475Smrg	    TRACE(("CASE_VPA - vertical position\n"));
1870d522f475Smrg	    if ((row = param[0]) < 1)
1871d522f475Smrg		row = 1;
1872d522f475Smrg	    CursorSet(screen, row - 1, screen->cur_col, xw->flags);
187320d2c4d2Smrg	    ResetState(sp);
1874d522f475Smrg	    break;
1875d522f475Smrg
1876d522f475Smrg	case CASE_HPA:
1877d522f475Smrg	    TRACE(("CASE_HPA - horizontal position\n"));
1878d522f475Smrg	    if ((col = param[0]) < 1)
1879d522f475Smrg		col = 1;
1880d522f475Smrg	    CursorSet(screen, screen->cur_row, col - 1, xw->flags);
188120d2c4d2Smrg	    ResetState(sp);
1882d522f475Smrg	    break;
1883d522f475Smrg
1884d522f475Smrg	case CASE_HP_BUGGY_LL:
1885d522f475Smrg	    TRACE(("CASE_HP_BUGGY_LL\n"));
1886d522f475Smrg	    /* Some HP-UX applications have the bug that they
1887d522f475Smrg	       assume ESC F goes to the lower left corner of
1888d522f475Smrg	       the screen, regardless of what terminfo says. */
1889d522f475Smrg	    if (screen->hp_ll_bc)
1890d522f475Smrg		CursorSet(screen, screen->max_row, 0, xw->flags);
189120d2c4d2Smrg	    ResetState(sp);
1892d522f475Smrg	    break;
1893d522f475Smrg
1894d522f475Smrg	case CASE_ED:
1895d522f475Smrg	    TRACE(("CASE_ED - erase display\n"));
1896d522f475Smrg	    do_erase_display(xw, param[0], OFF_PROTECT);
189720d2c4d2Smrg	    ResetState(sp);
1898d522f475Smrg	    break;
1899d522f475Smrg
1900d522f475Smrg	case CASE_EL:
1901d522f475Smrg	    TRACE(("CASE_EL - erase line\n"));
1902d522f475Smrg	    do_erase_line(xw, param[0], OFF_PROTECT);
190320d2c4d2Smrg	    ResetState(sp);
1904d522f475Smrg	    break;
1905d522f475Smrg
1906d522f475Smrg	case CASE_ECH:
1907d522f475Smrg	    TRACE(("CASE_ECH - erase char\n"));
1908d522f475Smrg	    /* ECH */
1909d522f475Smrg	    ClearRight(xw, param[0] < 1 ? 1 : param[0]);
191020d2c4d2Smrg	    ResetState(sp);
1911d522f475Smrg	    break;
1912d522f475Smrg
1913d522f475Smrg	case CASE_IL:
1914d522f475Smrg	    TRACE(("CASE_IL - insert line\n"));
1915d522f475Smrg	    if ((row = param[0]) < 1)
1916d522f475Smrg		row = 1;
1917d522f475Smrg	    InsertLine(xw, row);
191820d2c4d2Smrg	    ResetState(sp);
1919d522f475Smrg	    break;
1920d522f475Smrg
1921d522f475Smrg	case CASE_DL:
1922d522f475Smrg	    TRACE(("CASE_DL - delete line\n"));
1923d522f475Smrg	    if ((row = param[0]) < 1)
1924d522f475Smrg		row = 1;
1925d522f475Smrg	    DeleteLine(xw, row);
192620d2c4d2Smrg	    ResetState(sp);
1927d522f475Smrg	    break;
1928d522f475Smrg
1929d522f475Smrg	case CASE_DCH:
1930d522f475Smrg	    TRACE(("CASE_DCH - delete char\n"));
1931d522f475Smrg	    if ((row = param[0]) < 1)
1932d522f475Smrg		row = 1;
1933d522f475Smrg	    DeleteChar(xw, (unsigned) row);
193420d2c4d2Smrg	    ResetState(sp);
1935d522f475Smrg	    break;
1936d522f475Smrg
1937d522f475Smrg	case CASE_TRACK_MOUSE:
1938d522f475Smrg	    /*
1939d522f475Smrg	     * A single parameter other than zero is always scroll-down.
1940d522f475Smrg	     * A zero-parameter is used to reset the mouse mode, and is
1941d522f475Smrg	     * not useful for scrolling anyway.
1942d522f475Smrg	     */
1943d522f475Smrg	    if (nparam > 1 || param[0] == 0) {
1944d522f475Smrg		CELL start;
1945d522f475Smrg
1946d522f475Smrg		TRACE(("CASE_TRACK_MOUSE\n"));
1947d522f475Smrg		/* Track mouse as long as in window and between
1948d522f475Smrg		 * specified rows
1949d522f475Smrg		 */
195020d2c4d2Smrg		start.row = one_if_default(2) - 1;
1951d522f475Smrg		start.col = param[1] - 1;
1952d522f475Smrg		TrackMouse(xw,
1953d522f475Smrg			   param[0],
1954d522f475Smrg			   &start,
1955d522f475Smrg			   param[3] - 1, param[4] - 2);
1956d522f475Smrg	    } else {
1957d522f475Smrg		TRACE(("CASE_SD - scroll down\n"));
1958d522f475Smrg		/* SD */
1959d522f475Smrg		if ((count = param[0]) < 1)
1960d522f475Smrg		    count = 1;
1961d522f475Smrg		RevScroll(xw, count);
1962d522f475Smrg		do_xevents();
1963d522f475Smrg	    }
196420d2c4d2Smrg	    ResetState(sp);
1965d522f475Smrg	    break;
1966d522f475Smrg
1967d522f475Smrg	case CASE_DECID:
1968d522f475Smrg	    TRACE(("CASE_DECID\n"));
1969d522f475Smrg	    if_OPT_VT52_MODE(screen, {
1970d522f475Smrg		unparseputc(xw, ANSI_ESC);
1971d522f475Smrg		unparseputc(xw, '/');
1972d522f475Smrg		unparseputc(xw, 'Z');
1973d522f475Smrg		unparse_end(xw);
197420d2c4d2Smrg		ResetState(sp);
1975d522f475Smrg		break;
1976d522f475Smrg	    });
1977d522f475Smrg	    param[0] = DEFAULT;	/* Default ID parameter */
1978d522f475Smrg	    /* FALLTHRU */
1979d522f475Smrg	case CASE_DA1:
1980d522f475Smrg	    TRACE(("CASE_DA1\n"));
1981d522f475Smrg	    if (param[0] <= 0) {	/* less than means DEFAULT */
1982d522f475Smrg		count = 0;
1983d522f475Smrg		reply.a_type = ANSI_CSI;
1984d522f475Smrg		reply.a_pintro = '?';
1985d522f475Smrg
1986d522f475Smrg		/* The first param corresponds to the highest
1987d522f475Smrg		 * operating level (i.e., service level) of the
1988d522f475Smrg		 * emulation.  A DEC terminal can be setup to
1989d522f475Smrg		 * respond with a different DA response, but
1990d522f475Smrg		 * there's no control sequence that modifies this.
1991d522f475Smrg		 * We set it via a resource.
1992d522f475Smrg		 */
1993d522f475Smrg		if (screen->terminal_id < 200) {
1994d522f475Smrg		    switch (screen->terminal_id) {
1995d522f475Smrg		    case 102:
1996d522f475Smrg			reply.a_param[count++] = 6;	/* VT102 */
1997d522f475Smrg			break;
1998d522f475Smrg		    case 101:
1999d522f475Smrg			reply.a_param[count++] = 1;	/* VT101 */
2000d522f475Smrg			reply.a_param[count++] = 0;	/* no options */
2001d522f475Smrg			break;
2002d522f475Smrg		    default:	/* VT100 */
2003d522f475Smrg			reply.a_param[count++] = 1;	/* VT100 */
2004d522f475Smrg			reply.a_param[count++] = 2;	/* AVO */
2005d522f475Smrg			break;
2006d522f475Smrg		    }
2007d522f475Smrg		} else {
20082eaa94a1Schristos		    reply.a_param[count++] = (ParmType) (60
20092eaa94a1Schristos							 + screen->terminal_id
20102eaa94a1Schristos							 / 100);
2011d522f475Smrg		    reply.a_param[count++] = 1;		/* 132-columns */
2012d522f475Smrg		    reply.a_param[count++] = 2;		/* printer */
2013d522f475Smrg		    reply.a_param[count++] = 6;		/* selective-erase */
2014d522f475Smrg#if OPT_SUNPC_KBD
2015d522f475Smrg		    if (xw->keyboard.type == keyboardIsVT220)
2016d522f475Smrg#endif
2017d522f475Smrg			reply.a_param[count++] = 8;	/* user-defined-keys */
2018d522f475Smrg		    reply.a_param[count++] = 9;		/* national replacement charsets */
2019d522f475Smrg		    reply.a_param[count++] = 15;	/* technical characters */
2020d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2021d522f475Smrg			reply.a_param[count++] = 22;	/* ANSI color, VT525 */
2022d522f475Smrg		    });
2023d522f475Smrg#if OPT_DEC_LOCATOR
2024d522f475Smrg		    reply.a_param[count++] = 29;	/* ANSI text locator */
2025d522f475Smrg#endif
2026d522f475Smrg		}
20272eaa94a1Schristos		reply.a_nparam = (ParmType) count;
2028d522f475Smrg		reply.a_inters = 0;
2029d522f475Smrg		reply.a_final = 'c';
2030d522f475Smrg		unparseseq(xw, &reply);
2031d522f475Smrg	    }
203220d2c4d2Smrg	    ResetState(sp);
2033d522f475Smrg	    break;
2034d522f475Smrg
2035d522f475Smrg	case CASE_DA2:
2036d522f475Smrg	    TRACE(("CASE_DA2\n"));
2037d522f475Smrg	    if (param[0] <= 0) {	/* less than means DEFAULT */
2038d522f475Smrg		count = 0;
2039d522f475Smrg		reply.a_type = ANSI_CSI;
2040d522f475Smrg		reply.a_pintro = '>';
2041d522f475Smrg
2042d522f475Smrg		if (screen->terminal_id >= 200)
2043d522f475Smrg		    reply.a_param[count++] = 1;		/* VT220 */
2044d522f475Smrg		else
2045d522f475Smrg		    reply.a_param[count++] = 0;		/* VT100 (nonstandard) */
2046d522f475Smrg		reply.a_param[count++] = XTERM_PATCH;	/* Version */
2047d522f475Smrg		reply.a_param[count++] = 0;	/* options (none) */
20482eaa94a1Schristos		reply.a_nparam = (ParmType) count;
2049d522f475Smrg		reply.a_inters = 0;
2050d522f475Smrg		reply.a_final = 'c';
2051d522f475Smrg		unparseseq(xw, &reply);
2052d522f475Smrg	    }
205320d2c4d2Smrg	    ResetState(sp);
2054d522f475Smrg	    break;
2055d522f475Smrg
2056d522f475Smrg	case CASE_DECRPTUI:
2057d522f475Smrg	    TRACE(("CASE_DECRPTUI\n"));
2058d522f475Smrg	    if ((screen->terminal_id >= 400)
2059d522f475Smrg		&& (param[0] <= 0)) {	/* less than means DEFAULT */
2060d522f475Smrg		unparseputc1(xw, ANSI_DCS);
2061d522f475Smrg		unparseputc(xw, '!');
2062d522f475Smrg		unparseputc(xw, '|');
2063d522f475Smrg		unparseputc(xw, '0');
2064d522f475Smrg		unparseputc1(xw, ANSI_ST);
2065d522f475Smrg		unparse_end(xw);
2066d522f475Smrg	    }
206720d2c4d2Smrg	    ResetState(sp);
2068d522f475Smrg	    break;
2069d522f475Smrg
2070d522f475Smrg	case CASE_TBC:
2071d522f475Smrg	    TRACE(("CASE_TBC - tab clear\n"));
2072d522f475Smrg	    if ((row = param[0]) <= 0)	/* less than means default */
2073d522f475Smrg		TabClear(xw->tabs, screen->cur_col);
2074d522f475Smrg	    else if (row == 3)
2075d522f475Smrg		TabZonk(xw->tabs);
207620d2c4d2Smrg	    ResetState(sp);
2077d522f475Smrg	    break;
2078d522f475Smrg
2079d522f475Smrg	case CASE_SET:
2080d522f475Smrg	    TRACE(("CASE_SET - set mode\n"));
2081d522f475Smrg	    ansi_modes(xw, bitset);
208220d2c4d2Smrg	    ResetState(sp);
2083d522f475Smrg	    break;
2084d522f475Smrg
2085d522f475Smrg	case CASE_RST:
2086d522f475Smrg	    TRACE(("CASE_RST - reset mode\n"));
2087d522f475Smrg	    ansi_modes(xw, bitclr);
208820d2c4d2Smrg	    ResetState(sp);
2089d522f475Smrg	    break;
2090d522f475Smrg
2091d522f475Smrg	case CASE_SGR:
2092d522f475Smrg	    for (row = 0; row < nparam; ++row) {
2093d522f475Smrg		if_OPT_XMC_GLITCH(screen, {
2094d522f475Smrg		    Mark_XMC(xw, param[row]);
2095d522f475Smrg		});
2096d522f475Smrg		TRACE(("CASE_SGR %d\n", param[row]));
2097d522f475Smrg		switch (param[row]) {
2098d522f475Smrg		case DEFAULT:
2099d522f475Smrg		case 0:
210020d2c4d2Smrg		    UIntClr(xw->flags,
210120d2c4d2Smrg			    (INVERSE | BOLD | BLINK | UNDERLINE | INVISIBLE));
2102d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2103d522f475Smrg			reset_SGR_Colors(xw);
2104d522f475Smrg		    });
2105d522f475Smrg		    break;
2106d522f475Smrg		case 1:	/* Bold                 */
2107d522f475Smrg		    xw->flags |= BOLD;
2108d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2109d522f475Smrg			setExtendedFG(xw);
2110d522f475Smrg		    });
2111d522f475Smrg		    break;
2112d522f475Smrg		case 5:	/* Blink                */
2113d522f475Smrg		    xw->flags |= BLINK;
2114d522f475Smrg		    StartBlinking(screen);
2115d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2116d522f475Smrg			setExtendedFG(xw);
2117d522f475Smrg		    });
2118d522f475Smrg		    break;
2119d522f475Smrg		case 4:	/* Underscore           */
2120d522f475Smrg		    xw->flags |= UNDERLINE;
2121d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2122d522f475Smrg			setExtendedFG(xw);
2123d522f475Smrg		    });
2124d522f475Smrg		    break;
2125d522f475Smrg		case 7:
2126d522f475Smrg		    xw->flags |= INVERSE;
2127d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2128d522f475Smrg			setExtendedBG(xw);
2129d522f475Smrg		    });
2130d522f475Smrg		    break;
2131d522f475Smrg		case 8:
2132d522f475Smrg		    xw->flags |= INVISIBLE;
2133d522f475Smrg		    break;
2134d522f475Smrg		case 22:	/* reset 'bold' */
213520d2c4d2Smrg		    UIntClr(xw->flags, BOLD);
2136d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2137d522f475Smrg			setExtendedFG(xw);
2138d522f475Smrg		    });
2139d522f475Smrg		    break;
2140d522f475Smrg		case 24:
214120d2c4d2Smrg		    UIntClr(xw->flags, UNDERLINE);
2142d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2143d522f475Smrg			setExtendedFG(xw);
2144d522f475Smrg		    });
2145d522f475Smrg		    break;
2146d522f475Smrg		case 25:	/* reset 'blink' */
214720d2c4d2Smrg		    UIntClr(xw->flags, BLINK);
2148d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2149d522f475Smrg			setExtendedFG(xw);
2150d522f475Smrg		    });
2151d522f475Smrg		    break;
2152d522f475Smrg		case 27:
215320d2c4d2Smrg		    UIntClr(xw->flags, INVERSE);
2154d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2155d522f475Smrg			setExtendedBG(xw);
2156d522f475Smrg		    });
2157d522f475Smrg		    break;
2158d522f475Smrg		case 28:
215920d2c4d2Smrg		    UIntClr(xw->flags, INVISIBLE);
2160d522f475Smrg		    break;
2161d522f475Smrg		case 30:
2162d522f475Smrg		case 31:
2163d522f475Smrg		case 32:
2164d522f475Smrg		case 33:
2165d522f475Smrg		case 34:
2166d522f475Smrg		case 35:
2167d522f475Smrg		case 36:
2168d522f475Smrg		case 37:
2169d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2170d522f475Smrg			xw->sgr_foreground = (param[row] - 30);
2171d522f475Smrg			xw->sgr_extended = False;
2172d522f475Smrg			setExtendedFG(xw);
2173d522f475Smrg		    });
2174d522f475Smrg		    break;
2175d522f475Smrg		case 38:
2176d522f475Smrg		    /* This is more complicated than I'd
2177d522f475Smrg		       like, but it should properly eat all
2178d522f475Smrg		       the parameters for unsupported modes
2179d522f475Smrg		     */
2180d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2181d522f475Smrg			row++;
2182d522f475Smrg			if (row < nparam) {
2183d522f475Smrg			    switch (param[row]) {
2184d522f475Smrg			    case 5:
2185d522f475Smrg				row++;
2186d522f475Smrg				if (row < nparam &&
2187d522f475Smrg				    param[row] < NUM_ANSI_COLORS) {
2188d522f475Smrg				    xw->sgr_foreground = param[row];
2189d522f475Smrg				    xw->sgr_extended = True;
2190d522f475Smrg				    setExtendedFG(xw);
2191d522f475Smrg				}
2192d522f475Smrg				break;
2193d522f475Smrg			    default:
2194d522f475Smrg				row += 7;
2195d522f475Smrg				break;
2196d522f475Smrg			    }
2197d522f475Smrg			}
2198d522f475Smrg		    });
2199d522f475Smrg		    break;
2200d522f475Smrg		case 39:
2201d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2202d522f475Smrg			reset_SGR_Foreground(xw);
2203d522f475Smrg		    });
2204d522f475Smrg		    break;
2205d522f475Smrg		case 40:
2206d522f475Smrg		case 41:
2207d522f475Smrg		case 42:
2208d522f475Smrg		case 43:
2209d522f475Smrg		case 44:
2210d522f475Smrg		case 45:
2211d522f475Smrg		case 46:
2212d522f475Smrg		case 47:
2213d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2214d522f475Smrg			xw->sgr_background = (param[row] - 40);
2215d522f475Smrg			setExtendedBG(xw);
2216d522f475Smrg		    });
2217d522f475Smrg		    break;
2218d522f475Smrg		case 48:
2219d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2220d522f475Smrg			row++;
2221d522f475Smrg			if (row < nparam) {
2222d522f475Smrg			    switch (param[row]) {
2223d522f475Smrg			    case 5:
2224d522f475Smrg				row++;
2225d522f475Smrg				if (row < nparam &&
2226d522f475Smrg				    param[row] < NUM_ANSI_COLORS) {
2227d522f475Smrg				    xw->sgr_background = param[row];
2228d522f475Smrg				    setExtendedBG(xw);
2229d522f475Smrg				}
2230d522f475Smrg				break;
2231d522f475Smrg			    default:
2232d522f475Smrg				row += 7;
2233d522f475Smrg				break;
2234d522f475Smrg			    }
2235d522f475Smrg			}
2236d522f475Smrg		    });
2237d522f475Smrg		    break;
2238d522f475Smrg		case 49:
2239d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2240d522f475Smrg			reset_SGR_Background(xw);
2241d522f475Smrg		    });
2242d522f475Smrg		    break;
2243d522f475Smrg		case 90:
2244d522f475Smrg		case 91:
2245d522f475Smrg		case 92:
2246d522f475Smrg		case 93:
2247d522f475Smrg		case 94:
2248d522f475Smrg		case 95:
2249d522f475Smrg		case 96:
2250d522f475Smrg		case 97:
2251d522f475Smrg		    if_OPT_AIX_COLORS(screen, {
2252d522f475Smrg			xw->sgr_foreground = (param[row] - 90 + 8);
2253d522f475Smrg			xw->sgr_extended = False;
2254d522f475Smrg			setExtendedFG(xw);
2255d522f475Smrg		    });
2256d522f475Smrg		    break;
2257d522f475Smrg		case 100:
2258d522f475Smrg#if !OPT_AIX_COLORS
2259d522f475Smrg		    if_OPT_ISO_COLORS(screen, {
2260d522f475Smrg			reset_SGR_Foreground(xw);
2261d522f475Smrg			reset_SGR_Background(xw);
2262d522f475Smrg		    });
2263d522f475Smrg		    break;
2264d522f475Smrg#endif
2265d522f475Smrg		case 101:
2266d522f475Smrg		case 102:
2267d522f475Smrg		case 103:
2268d522f475Smrg		case 104:
2269d522f475Smrg		case 105:
2270d522f475Smrg		case 106:
2271d522f475Smrg		case 107:
2272d522f475Smrg		    if_OPT_AIX_COLORS(screen, {
2273d522f475Smrg			xw->sgr_background = (param[row] - 100 + 8);
2274d522f475Smrg			setExtendedBG(xw);
2275d522f475Smrg		    });
2276d522f475Smrg		    break;
2277d522f475Smrg		}
2278d522f475Smrg	    }
227920d2c4d2Smrg	    ResetState(sp);
2280d522f475Smrg	    break;
2281d522f475Smrg
2282d522f475Smrg	    /* DSR (except for the '?') is a superset of CPR */
2283d522f475Smrg	case CASE_DSR:
2284d522f475Smrg	    sp->private_function = True;
2285d522f475Smrg
2286d522f475Smrg	    /* FALLTHRU */
2287d522f475Smrg	case CASE_CPR:
2288d522f475Smrg	    TRACE(("CASE_CPR - cursor position\n"));
2289d522f475Smrg	    count = 0;
2290d522f475Smrg	    reply.a_type = ANSI_CSI;
22912eaa94a1Schristos	    reply.a_pintro = CharOf(sp->private_function ? '?' : 0);
2292d522f475Smrg	    reply.a_inters = 0;
2293d522f475Smrg	    reply.a_final = 'n';
2294d522f475Smrg
2295d522f475Smrg	    switch (param[0]) {
2296d522f475Smrg	    case 5:
2297d522f475Smrg		/* operating status */
2298d522f475Smrg		reply.a_param[count++] = 0;	/* (no malfunction ;-) */
2299d522f475Smrg		break;
2300d522f475Smrg	    case 6:
2301d522f475Smrg		/* CPR */
2302d522f475Smrg		/* DECXCPR (with page=0) */
23032eaa94a1Schristos		reply.a_param[count++] = (ParmType) (screen->cur_row + 1);
23042eaa94a1Schristos		reply.a_param[count++] = (ParmType) (screen->cur_col + 1);
2305d522f475Smrg		reply.a_final = 'R';
2306d522f475Smrg		break;
2307d522f475Smrg	    case 15:
2308d522f475Smrg		/* printer status */
23092eaa94a1Schristos		if (screen->terminal_id >= 200) {	/* VT220 */
23102eaa94a1Schristos		    reply.a_param[count++] = 13;	/* implement printer */
23112eaa94a1Schristos		}
2312d522f475Smrg		break;
2313d522f475Smrg	    case 25:
2314d522f475Smrg		/* UDK status */
23152eaa94a1Schristos		if (screen->terminal_id >= 200) {	/* VT220 */
23162eaa94a1Schristos		    reply.a_param[count++] = 20;	/* UDK always unlocked */
23172eaa94a1Schristos		}
2318d522f475Smrg		break;
2319d522f475Smrg	    case 26:
2320d522f475Smrg		/* keyboard status */
23212eaa94a1Schristos		if (screen->terminal_id >= 200) {	/* VT220 */
23222eaa94a1Schristos		    reply.a_param[count++] = 27;
23232eaa94a1Schristos		    reply.a_param[count++] = 1;		/* North American */
23242eaa94a1Schristos		    if (screen->terminal_id >= 400) {
23252eaa94a1Schristos			reply.a_param[count++] = 0;	/* ready */
23262eaa94a1Schristos			reply.a_param[count++] = 0;	/* LK201 */
23272eaa94a1Schristos		    }
2328d522f475Smrg		}
2329d522f475Smrg		break;
2330d522f475Smrg	    case 53:
2331d522f475Smrg		/* Locator status */
23322eaa94a1Schristos		if (screen->terminal_id >= 200) {	/* VT220 */
2333d522f475Smrg#if OPT_DEC_LOCATOR
23342eaa94a1Schristos		    reply.a_param[count++] = 50;	/* locator ready */
2335d522f475Smrg#else
23362eaa94a1Schristos		    reply.a_param[count++] = 53;	/* no locator */
2337d522f475Smrg#endif
23382eaa94a1Schristos		}
23392eaa94a1Schristos		break;
23402eaa94a1Schristos	    default:
2341d522f475Smrg		break;
2342d522f475Smrg	    }
2343d522f475Smrg
23442eaa94a1Schristos	    if ((reply.a_nparam = (ParmType) count) != 0)
2345d522f475Smrg		unparseseq(xw, &reply);
2346d522f475Smrg
234720d2c4d2Smrg	    ResetState(sp);
2348d522f475Smrg	    sp->private_function = False;
2349d522f475Smrg	    break;
2350d522f475Smrg
2351d522f475Smrg	case CASE_MC:
2352d522f475Smrg	    TRACE(("CASE_MC - media control\n"));
2353956cc18dSsnj	    xtermMediaControl(xw, param[0], False);
235420d2c4d2Smrg	    ResetState(sp);
2355d522f475Smrg	    break;
2356d522f475Smrg
2357d522f475Smrg	case CASE_DEC_MC:
2358d522f475Smrg	    TRACE(("CASE_DEC_MC - DEC media control\n"));
2359956cc18dSsnj	    xtermMediaControl(xw, param[0], True);
236020d2c4d2Smrg	    ResetState(sp);
2361d522f475Smrg	    break;
2362d522f475Smrg
2363d522f475Smrg	case CASE_HP_MEM_LOCK:
2364d522f475Smrg	case CASE_HP_MEM_UNLOCK:
2365d522f475Smrg	    TRACE(("%s\n", ((sp->parsestate[c] == CASE_HP_MEM_LOCK)
2366d522f475Smrg			    ? "CASE_HP_MEM_LOCK"
2367d522f475Smrg			    : "CASE_HP_MEM_UNLOCK")));
2368d522f475Smrg	    if (screen->scroll_amt)
2369d522f475Smrg		FlushScroll(xw);
2370d522f475Smrg	    if (sp->parsestate[c] == CASE_HP_MEM_LOCK)
2371d522f475Smrg		set_tb_margins(screen, screen->cur_row, screen->bot_marg);
2372d522f475Smrg	    else
2373d522f475Smrg		set_tb_margins(screen, 0, screen->bot_marg);
237420d2c4d2Smrg	    ResetState(sp);
2375d522f475Smrg	    break;
2376d522f475Smrg
2377d522f475Smrg	case CASE_DECSTBM:
2378d522f475Smrg	    TRACE(("CASE_DECSTBM - set scrolling region\n"));
2379d522f475Smrg	    if ((top = param[0]) < 1)
2380d522f475Smrg		top = 1;
2381d522f475Smrg	    if (nparam < 2 || (bot = param[1]) == DEFAULT
2382d522f475Smrg		|| bot > MaxRows(screen)
2383d522f475Smrg		|| bot == 0)
2384d522f475Smrg		bot = MaxRows(screen);
2385d522f475Smrg	    if (bot > top) {
2386d522f475Smrg		if (screen->scroll_amt)
2387d522f475Smrg		    FlushScroll(xw);
2388d522f475Smrg		set_tb_margins(screen, top - 1, bot - 1);
2389d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
2390d522f475Smrg	    }
239120d2c4d2Smrg	    ResetState(sp);
2392d522f475Smrg	    break;
2393d522f475Smrg
2394d522f475Smrg	case CASE_DECREQTPARM:
2395d522f475Smrg	    TRACE(("CASE_DECREQTPARM\n"));
2396d522f475Smrg	    if (screen->terminal_id < 200) {	/* VT102 */
2397d522f475Smrg		if ((row = param[0]) == DEFAULT)
2398d522f475Smrg		    row = 0;
2399d522f475Smrg		if (row == 0 || row == 1) {
2400d522f475Smrg		    reply.a_type = ANSI_CSI;
2401d522f475Smrg		    reply.a_pintro = 0;
2402d522f475Smrg		    reply.a_nparam = 7;
24032eaa94a1Schristos		    reply.a_param[0] = (ParmType) (row + 2);
2404d522f475Smrg		    reply.a_param[1] = 1;	/* no parity */
2405d522f475Smrg		    reply.a_param[2] = 1;	/* eight bits */
2406d522f475Smrg		    reply.a_param[3] = 128;	/* transmit 38.4k baud */
2407d522f475Smrg		    reply.a_param[4] = 128;	/* receive 38.4k baud */
2408d522f475Smrg		    reply.a_param[5] = 1;	/* clock multiplier ? */
2409d522f475Smrg		    reply.a_param[6] = 0;	/* STP flags ? */
2410d522f475Smrg		    reply.a_inters = 0;
2411d522f475Smrg		    reply.a_final = 'x';
2412d522f475Smrg		    unparseseq(xw, &reply);
2413d522f475Smrg		}
2414d522f475Smrg	    }
241520d2c4d2Smrg	    ResetState(sp);
2416d522f475Smrg	    break;
2417d522f475Smrg
2418d522f475Smrg	case CASE_DECSET:
2419d522f475Smrg	    /* DECSET */
2420d522f475Smrg#if OPT_VT52_MODE
2421d522f475Smrg	    if (screen->vtXX_level != 0)
2422d522f475Smrg#endif
2423d522f475Smrg		dpmodes(xw, bitset);
242420d2c4d2Smrg	    ResetState(sp);
2425d522f475Smrg#if OPT_TEK4014
2426d522f475Smrg	    if (TEK4014_ACTIVE(xw))
2427d522f475Smrg		return False;
2428d522f475Smrg#endif
2429d522f475Smrg	    break;
2430d522f475Smrg
2431d522f475Smrg	case CASE_DECRST:
2432d522f475Smrg	    /* DECRST */
2433d522f475Smrg	    dpmodes(xw, bitclr);
24342eaa94a1Schristos	    init_groundtable(screen, sp);
243520d2c4d2Smrg	    ResetState(sp);
2436d522f475Smrg	    break;
2437d522f475Smrg
2438d522f475Smrg	case CASE_DECALN:
2439d522f475Smrg	    TRACE(("CASE_DECALN - alignment test\n"));
2440d522f475Smrg	    if (screen->cursor_state)
2441d522f475Smrg		HideCursor();
2442d522f475Smrg	    set_tb_margins(screen, 0, screen->max_row);
2443d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
2444d522f475Smrg	    xtermParseRect(xw, 0, 0, &myRect);
2445d522f475Smrg	    ScrnFillRectangle(xw, &myRect, 'E', 0, False);
244620d2c4d2Smrg	    ResetState(sp);
2447d522f475Smrg	    break;
2448d522f475Smrg
2449d522f475Smrg	case CASE_GSETS:
2450d522f475Smrg	    TRACE(("CASE_GSETS(%d) = '%c'\n", sp->scstype, c));
2451d522f475Smrg	    if (screen->vtXX_level != 0)
24522eaa94a1Schristos		screen->gsets[sp->scstype] = CharOf(c);
245320d2c4d2Smrg	    ResetState(sp);
2454d522f475Smrg	    break;
2455d522f475Smrg
2456d522f475Smrg	case CASE_DECSC:
2457d522f475Smrg	    TRACE(("CASE_DECSC - save cursor\n"));
2458d522f475Smrg	    CursorSave(xw);
245920d2c4d2Smrg	    ResetState(sp);
2460d522f475Smrg	    break;
2461d522f475Smrg
2462d522f475Smrg	case CASE_DECRC:
2463d522f475Smrg	    TRACE(("CASE_DECRC - restore cursor\n"));
2464d522f475Smrg	    CursorRestore(xw);
2465d522f475Smrg	    if_OPT_ISO_COLORS(screen, {
2466d522f475Smrg		setExtendedFG(xw);
2467d522f475Smrg	    });
246820d2c4d2Smrg	    ResetState(sp);
2469d522f475Smrg	    break;
2470d522f475Smrg
2471d522f475Smrg	case CASE_DECKPAM:
2472d522f475Smrg	    TRACE(("CASE_DECKPAM\n"));
2473d522f475Smrg	    xw->keyboard.flags |= MODE_DECKPAM;
2474d522f475Smrg	    update_appkeypad();
247520d2c4d2Smrg	    ResetState(sp);
2476d522f475Smrg	    break;
2477d522f475Smrg
2478d522f475Smrg	case CASE_DECKPNM:
2479d522f475Smrg	    TRACE(("CASE_DECKPNM\n"));
248020d2c4d2Smrg	    UIntClr(xw->keyboard.flags, MODE_DECKPAM);
2481d522f475Smrg	    update_appkeypad();
248220d2c4d2Smrg	    ResetState(sp);
2483d522f475Smrg	    break;
2484d522f475Smrg
2485d522f475Smrg	case CASE_CSI_QUOTE_STATE:
2486d522f475Smrg	    sp->parsestate = csi_quo_table;
2487d522f475Smrg	    break;
2488d522f475Smrg
248920d2c4d2Smrg#if OPT_BLINK_CURS
249020d2c4d2Smrg	case CASE_CSI_SPACE_STATE:
249120d2c4d2Smrg	    sp->parsestate = csi_sp_table;
249220d2c4d2Smrg	    break;
249320d2c4d2Smrg
249420d2c4d2Smrg	case CASE_DECSCUSR:
249520d2c4d2Smrg	    TRACE(("CASE_DECSCUSR\n"));
249620d2c4d2Smrg	    {
249720d2c4d2Smrg		Boolean change = True;
249820d2c4d2Smrg		Boolean blinks = screen->cursor_blink;
249920d2c4d2Smrg
250020d2c4d2Smrg		HideCursor();
250120d2c4d2Smrg
250220d2c4d2Smrg		switch (param[0]) {
250320d2c4d2Smrg		case 0:
250420d2c4d2Smrg		case 1:
250520d2c4d2Smrg		case DEFAULT:
250620d2c4d2Smrg		    /* blinking block */
250720d2c4d2Smrg		    screen->cursor_underline = False;
250820d2c4d2Smrg		    blinks = True;
250920d2c4d2Smrg		    break;
251020d2c4d2Smrg		case 2:
251120d2c4d2Smrg		    /* steady block */
251220d2c4d2Smrg		    screen->cursor_underline = False;
251320d2c4d2Smrg		    blinks = False;
251420d2c4d2Smrg		    break;
251520d2c4d2Smrg		case 3:
251620d2c4d2Smrg		    /* blinking underline */
251720d2c4d2Smrg		    screen->cursor_underline = True;
251820d2c4d2Smrg		    blinks = True;
251920d2c4d2Smrg		    break;
252020d2c4d2Smrg		case 4:
252120d2c4d2Smrg		    /* steady underline */
252220d2c4d2Smrg		    screen->cursor_underline = True;
252320d2c4d2Smrg		    blinks = False;
252420d2c4d2Smrg		    break;
252520d2c4d2Smrg		default:
252620d2c4d2Smrg		    change = False;
252720d2c4d2Smrg		    break;
252820d2c4d2Smrg		}
252920d2c4d2Smrg
253020d2c4d2Smrg		if (change) {
253120d2c4d2Smrg		    xtermSetCursorBox(screen);
253220d2c4d2Smrg		    SetCursorBlink(screen, blinks);
253320d2c4d2Smrg		}
253420d2c4d2Smrg	    }
253520d2c4d2Smrg	    ResetState(sp);
253620d2c4d2Smrg	    break;
253720d2c4d2Smrg#endif
253820d2c4d2Smrg
253920d2c4d2Smrg#if OPT_SCROLL_LOCK
254020d2c4d2Smrg	case CASE_DECLL:
254120d2c4d2Smrg	    TRACE(("CASE_DECLL\n"));
254220d2c4d2Smrg	    if (nparam > 0) {
254320d2c4d2Smrg		for (count = 0; count < nparam; ++count) {
254420d2c4d2Smrg		    switch (param[count]) {
254520d2c4d2Smrg		    case 0:
254620d2c4d2Smrg		    case DEFAULT:
254720d2c4d2Smrg			xtermClearLEDs(screen);
254820d2c4d2Smrg			break;
254920d2c4d2Smrg		    case 1:
255020d2c4d2Smrg		    case 2:
255120d2c4d2Smrg		    case 3:
255220d2c4d2Smrg			xtermShowLED(screen, (Cardinal) param[count], True);
255320d2c4d2Smrg			break;
255420d2c4d2Smrg		    case 21:
255520d2c4d2Smrg		    case 22:
255620d2c4d2Smrg		    case 23:
255720d2c4d2Smrg			xtermShowLED(screen, (Cardinal) (param[count] - 20), True);
255820d2c4d2Smrg			break;
255920d2c4d2Smrg		    }
256020d2c4d2Smrg		}
256120d2c4d2Smrg	    } else {
256220d2c4d2Smrg		xtermClearLEDs(screen);
256320d2c4d2Smrg	    }
256420d2c4d2Smrg	    ResetState(sp);
256520d2c4d2Smrg	    break;
256620d2c4d2Smrg#endif
256720d2c4d2Smrg
2568d522f475Smrg#if OPT_VT52_MODE
2569d522f475Smrg	case CASE_VT52_FINISH:
2570d522f475Smrg	    TRACE(("CASE_VT52_FINISH terminal_id %d, vtXX_level %d\n",
2571d522f475Smrg		   screen->terminal_id,
2572d522f475Smrg		   screen->vtXX_level));
2573d522f475Smrg	    if (screen->terminal_id >= 100
2574d522f475Smrg		&& screen->vtXX_level == 0) {
2575d522f475Smrg		sp->groundtable =
2576d522f475Smrg		    sp->parsestate = ansi_table;
2577d522f475Smrg		screen->vtXX_level = screen->vt52_save_level;
2578d522f475Smrg		screen->curgl = screen->vt52_save_curgl;
2579d522f475Smrg		screen->curgr = screen->vt52_save_curgr;
2580d522f475Smrg		screen->curss = screen->vt52_save_curss;
2581d522f475Smrg		memmove(screen->gsets, screen->vt52_save_gsets, sizeof(screen->gsets));
2582d522f475Smrg	    }
2583d522f475Smrg	    break;
2584d522f475Smrg#endif
2585d522f475Smrg
2586d522f475Smrg	case CASE_ANSI_LEVEL_1:
2587d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_1\n"));
2588d522f475Smrg	    set_ansi_conformance(screen, 1);
258920d2c4d2Smrg	    ResetState(sp);
2590d522f475Smrg	    break;
2591d522f475Smrg
2592d522f475Smrg	case CASE_ANSI_LEVEL_2:
2593d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_2\n"));
2594d522f475Smrg	    set_ansi_conformance(screen, 2);
259520d2c4d2Smrg	    ResetState(sp);
2596d522f475Smrg	    break;
2597d522f475Smrg
2598d522f475Smrg	case CASE_ANSI_LEVEL_3:
2599d522f475Smrg	    TRACE(("CASE_ANSI_LEVEL_3\n"));
2600d522f475Smrg	    set_ansi_conformance(screen, 3);
260120d2c4d2Smrg	    ResetState(sp);
2602d522f475Smrg	    break;
2603d522f475Smrg
2604d522f475Smrg	case CASE_DECSCL:
2605d522f475Smrg	    TRACE(("CASE_DECSCL(%d,%d)\n", param[0], param[1]));
2606d522f475Smrg	    if (param[0] >= 61 && param[0] <= 65) {
2607d522f475Smrg		/*
2608d522f475Smrg		 * VT300, VT420, VT520 manuals claim that DECSCL does a hard
2609d522f475Smrg		 * reset (RIS).  VT220 manual states that it is a soft reset.
2610d522f475Smrg		 * Perhaps both are right (unlikely).  Kermit says it's soft.
2611d522f475Smrg		 */
2612d522f475Smrg		VTReset(xw, False, False);
2613d522f475Smrg		screen->vtXX_level = param[0] - 60;
2614d522f475Smrg		if (param[0] > 61) {
261520d2c4d2Smrg		    switch (zero_if_default(1)) {
261620d2c4d2Smrg		    case 1:
2617d522f475Smrg			show_8bit_control(False);
261820d2c4d2Smrg			break;
261920d2c4d2Smrg		    case 0:
262020d2c4d2Smrg		    case 2:
2621d522f475Smrg			show_8bit_control(True);
262220d2c4d2Smrg			break;
262320d2c4d2Smrg		    }
2624d522f475Smrg		}
2625d522f475Smrg	    }
262620d2c4d2Smrg	    ResetState(sp);
2627d522f475Smrg	    break;
2628d522f475Smrg
2629d522f475Smrg	case CASE_DECSCA:
2630d522f475Smrg	    TRACE(("CASE_DECSCA\n"));
2631d522f475Smrg	    screen->protected_mode = DEC_PROTECT;
2632d522f475Smrg	    if (param[0] <= 0 || param[0] == 2)
263320d2c4d2Smrg		UIntClr(xw->flags, PROTECTED);
2634d522f475Smrg	    else if (param[0] == 1)
2635d522f475Smrg		xw->flags |= PROTECTED;
263620d2c4d2Smrg	    ResetState(sp);
2637d522f475Smrg	    break;
2638d522f475Smrg
2639d522f475Smrg	case CASE_DECSED:
2640d522f475Smrg	    TRACE(("CASE_DECSED\n"));
2641d522f475Smrg	    do_erase_display(xw, param[0], DEC_PROTECT);
264220d2c4d2Smrg	    ResetState(sp);
2643d522f475Smrg	    break;
2644d522f475Smrg
2645d522f475Smrg	case CASE_DECSEL:
2646d522f475Smrg	    TRACE(("CASE_DECSEL\n"));
2647d522f475Smrg	    do_erase_line(xw, param[0], DEC_PROTECT);
264820d2c4d2Smrg	    ResetState(sp);
2649d522f475Smrg	    break;
2650d522f475Smrg
2651d522f475Smrg	case CASE_ST:
265220d2c4d2Smrg	    TRACE(("CASE_ST: End of String (%lu bytes)\n", (unsigned long) sp->string_used));
265320d2c4d2Smrg	    ResetState(sp);
2654956cc18dSsnj	    if (!sp->string_used)
2655d522f475Smrg		break;
2656956cc18dSsnj	    sp->string_area[--(sp->string_used)] = '\0';
2657d522f475Smrg	    switch (sp->string_mode) {
2658d522f475Smrg	    case ANSI_APC:
2659d522f475Smrg		/* ignored */
2660d522f475Smrg		break;
2661d522f475Smrg	    case ANSI_DCS:
2662956cc18dSsnj		do_dcs(xw, sp->string_area, sp->string_used);
2663d522f475Smrg		break;
2664d522f475Smrg	    case ANSI_OSC:
2665956cc18dSsnj		do_osc(xw, sp->string_area, sp->string_used, ANSI_ST);
2666d522f475Smrg		break;
2667d522f475Smrg	    case ANSI_PM:
2668d522f475Smrg		/* ignored */
2669d522f475Smrg		break;
2670d522f475Smrg	    case ANSI_SOS:
2671d522f475Smrg		/* ignored */
2672d522f475Smrg		break;
2673d522f475Smrg	    }
2674d522f475Smrg	    break;
2675d522f475Smrg
2676d522f475Smrg	case CASE_SOS:
2677d522f475Smrg	    TRACE(("CASE_SOS: Start of String\n"));
267820d2c4d2Smrg	    if (ParseSOS(screen)) {
267920d2c4d2Smrg		sp->string_mode = ANSI_SOS;
268020d2c4d2Smrg		sp->parsestate = sos_table;
268120d2c4d2Smrg	    } else {
268220d2c4d2Smrg		illegal_parse(xw, c, sp);
268320d2c4d2Smrg	    }
2684d522f475Smrg	    break;
2685d522f475Smrg
2686d522f475Smrg	case CASE_PM:
2687d522f475Smrg	    TRACE(("CASE_PM: Privacy Message\n"));
268820d2c4d2Smrg	    if (ParseSOS(screen)) {
268920d2c4d2Smrg		sp->string_mode = ANSI_PM;
269020d2c4d2Smrg		sp->parsestate = sos_table;
269120d2c4d2Smrg	    } else {
269220d2c4d2Smrg		illegal_parse(xw, c, sp);
269320d2c4d2Smrg	    }
2694d522f475Smrg	    break;
2695d522f475Smrg
2696d522f475Smrg	case CASE_DCS:
2697d522f475Smrg	    TRACE(("CASE_DCS: Device Control String\n"));
2698d522f475Smrg	    sp->string_mode = ANSI_DCS;
2699d522f475Smrg	    sp->parsestate = sos_table;
2700d522f475Smrg	    break;
2701d522f475Smrg
2702d522f475Smrg	case CASE_APC:
2703d522f475Smrg	    TRACE(("CASE_APC: Application Program Command\n"));
270420d2c4d2Smrg	    if (ParseSOS(screen)) {
270520d2c4d2Smrg		sp->string_mode = ANSI_APC;
270620d2c4d2Smrg		sp->parsestate = sos_table;
270720d2c4d2Smrg	    } else {
270820d2c4d2Smrg		illegal_parse(xw, c, sp);
270920d2c4d2Smrg	    }
2710d522f475Smrg	    break;
2711d522f475Smrg
2712d522f475Smrg	case CASE_SPA:
2713d522f475Smrg	    TRACE(("CASE_SPA - start protected area\n"));
2714d522f475Smrg	    screen->protected_mode = ISO_PROTECT;
2715d522f475Smrg	    xw->flags |= PROTECTED;
271620d2c4d2Smrg	    ResetState(sp);
2717d522f475Smrg	    break;
2718d522f475Smrg
2719d522f475Smrg	case CASE_EPA:
2720d522f475Smrg	    TRACE(("CASE_EPA - end protected area\n"));
272120d2c4d2Smrg	    UIntClr(xw->flags, PROTECTED);
272220d2c4d2Smrg	    ResetState(sp);
2723d522f475Smrg	    break;
2724d522f475Smrg
2725d522f475Smrg	case CASE_SU:
2726d522f475Smrg	    TRACE(("CASE_SU - scroll up\n"));
2727d522f475Smrg	    if ((count = param[0]) < 1)
2728d522f475Smrg		count = 1;
2729d522f475Smrg	    xtermScroll(xw, count);
273020d2c4d2Smrg	    ResetState(sp);
2731d522f475Smrg	    break;
2732d522f475Smrg
2733d522f475Smrg	case CASE_IND:
2734d522f475Smrg	    TRACE(("CASE_IND - index\n"));
2735d522f475Smrg	    xtermIndex(xw, 1);
2736d522f475Smrg	    do_xevents();
273720d2c4d2Smrg	    ResetState(sp);
2738d522f475Smrg	    break;
2739d522f475Smrg
2740d522f475Smrg	case CASE_CPL:
2741d522f475Smrg	    TRACE(("CASE_CPL - cursor prev line\n"));
2742d522f475Smrg	    CursorPrevLine(screen, param[0]);
274320d2c4d2Smrg	    ResetState(sp);
2744d522f475Smrg	    break;
2745d522f475Smrg
2746d522f475Smrg	case CASE_CNL:
2747d522f475Smrg	    TRACE(("CASE_CNL - cursor next line\n"));
2748d522f475Smrg	    CursorNextLine(screen, param[0]);
274920d2c4d2Smrg	    ResetState(sp);
2750d522f475Smrg	    break;
2751d522f475Smrg
2752d522f475Smrg	case CASE_NEL:
2753d522f475Smrg	    TRACE(("CASE_NEL\n"));
2754d522f475Smrg	    xtermIndex(xw, 1);
2755d522f475Smrg	    CarriageReturn(screen);
275620d2c4d2Smrg	    ResetState(sp);
2757d522f475Smrg	    break;
2758d522f475Smrg
2759d522f475Smrg	case CASE_HTS:
2760d522f475Smrg	    TRACE(("CASE_HTS - horizontal tab set\n"));
2761d522f475Smrg	    TabSet(xw->tabs, screen->cur_col);
276220d2c4d2Smrg	    ResetState(sp);
2763d522f475Smrg	    break;
2764d522f475Smrg
2765d522f475Smrg	case CASE_RI:
2766d522f475Smrg	    TRACE(("CASE_RI - reverse index\n"));
2767d522f475Smrg	    RevIndex(xw, 1);
276820d2c4d2Smrg	    ResetState(sp);
2769d522f475Smrg	    break;
2770d522f475Smrg
2771d522f475Smrg	case CASE_SS2:
2772d522f475Smrg	    TRACE(("CASE_SS2\n"));
2773d522f475Smrg	    screen->curss = 2;
277420d2c4d2Smrg	    ResetState(sp);
2775d522f475Smrg	    break;
2776d522f475Smrg
2777d522f475Smrg	case CASE_SS3:
2778d522f475Smrg	    TRACE(("CASE_SS3\n"));
2779d522f475Smrg	    screen->curss = 3;
278020d2c4d2Smrg	    ResetState(sp);
2781d522f475Smrg	    break;
2782d522f475Smrg
2783d522f475Smrg	case CASE_CSI_STATE:
2784d522f475Smrg	    /* enter csi state */
2785d522f475Smrg	    nparam = 1;
2786d522f475Smrg	    param[0] = DEFAULT;
2787d522f475Smrg	    sp->parsestate = csi_table;
2788d522f475Smrg	    break;
2789d522f475Smrg
2790d522f475Smrg	case CASE_ESC_SP_STATE:
2791d522f475Smrg	    /* esc space */
2792d522f475Smrg	    sp->parsestate = esc_sp_table;
2793d522f475Smrg	    break;
2794d522f475Smrg
2795d522f475Smrg	case CASE_CSI_EX_STATE:
2796d522f475Smrg	    /* csi exclamation */
2797d522f475Smrg	    sp->parsestate = csi_ex_table;
2798d522f475Smrg	    break;
2799d522f475Smrg
2800d522f475Smrg#if OPT_DEC_LOCATOR
2801d522f475Smrg	case CASE_CSI_TICK_STATE:
2802d522f475Smrg	    /* csi tick (') */
2803d522f475Smrg	    sp->parsestate = csi_tick_table;
2804d522f475Smrg	    break;
2805d522f475Smrg
2806d522f475Smrg	case CASE_DECEFR:
2807d522f475Smrg	    TRACE(("CASE_DECEFR - Enable Filter Rectangle\n"));
2808d522f475Smrg	    if (screen->send_mouse_pos == DEC_LOCATOR) {
2809d522f475Smrg		MotionOff(screen, xw);
2810d522f475Smrg		if ((screen->loc_filter_top = param[0]) < 1)
2811d522f475Smrg		    screen->loc_filter_top = LOC_FILTER_POS;
2812d522f475Smrg		if (nparam < 2 || (screen->loc_filter_left = param[1]) < 1)
2813d522f475Smrg		    screen->loc_filter_left = LOC_FILTER_POS;
2814d522f475Smrg		if (nparam < 3 || (screen->loc_filter_bottom = param[2]) < 1)
2815d522f475Smrg		    screen->loc_filter_bottom = LOC_FILTER_POS;
2816d522f475Smrg		if (nparam < 4 || (screen->loc_filter_right = param[3]) < 1)
2817d522f475Smrg		    screen->loc_filter_right = LOC_FILTER_POS;
2818d522f475Smrg		InitLocatorFilter(xw);
2819d522f475Smrg	    }
282020d2c4d2Smrg	    ResetState(sp);
2821d522f475Smrg	    break;
2822d522f475Smrg
2823d522f475Smrg	case CASE_DECELR:
2824d522f475Smrg	    MotionOff(screen, xw);
2825d522f475Smrg	    if (param[0] <= 0 || param[0] > 2) {
2826d522f475Smrg		screen->send_mouse_pos = MOUSE_OFF;
2827d522f475Smrg		TRACE(("DECELR - Disable Locator Reports\n"));
2828d522f475Smrg	    } else {
2829d522f475Smrg		TRACE(("DECELR - Enable Locator Reports\n"));
2830d522f475Smrg		screen->send_mouse_pos = DEC_LOCATOR;
2831d522f475Smrg		xtermShowPointer(xw, True);
2832d522f475Smrg		if (param[0] == 2) {
2833d522f475Smrg		    screen->locator_reset = True;
2834d522f475Smrg		} else {
2835d522f475Smrg		    screen->locator_reset = False;
2836d522f475Smrg		}
2837d522f475Smrg		if (nparam < 2 || param[1] != 1) {
2838d522f475Smrg		    screen->locator_pixels = False;
2839d522f475Smrg		} else {
2840d522f475Smrg		    screen->locator_pixels = True;
2841d522f475Smrg		}
2842d522f475Smrg		screen->loc_filter = False;
2843d522f475Smrg	    }
284420d2c4d2Smrg	    ResetState(sp);
2845d522f475Smrg	    break;
2846d522f475Smrg
2847d522f475Smrg	case CASE_DECSLE:
2848d522f475Smrg	    TRACE(("DECSLE - Select Locator Events\n"));
2849d522f475Smrg	    for (count = 0; count < nparam; ++count) {
2850d522f475Smrg		switch (param[count]) {
2851d522f475Smrg		case DEFAULT:
2852d522f475Smrg		case 0:
2853d522f475Smrg		    MotionOff(screen, xw);
2854d522f475Smrg		    screen->loc_filter = False;
2855d522f475Smrg		    screen->locator_events = 0;
2856d522f475Smrg		    break;
2857d522f475Smrg		case 1:
2858d522f475Smrg		    screen->locator_events |= LOC_BTNS_DN;
2859d522f475Smrg		    break;
2860d522f475Smrg		case 2:
286120d2c4d2Smrg		    UIntClr(screen->locator_events, LOC_BTNS_DN);
2862d522f475Smrg		    break;
2863d522f475Smrg		case 3:
2864d522f475Smrg		    screen->locator_events |= LOC_BTNS_UP;
2865d522f475Smrg		    break;
2866d522f475Smrg		case 4:
286720d2c4d2Smrg		    UIntClr(screen->locator_events, LOC_BTNS_UP);
2868d522f475Smrg		    break;
2869d522f475Smrg		}
2870d522f475Smrg	    }
287120d2c4d2Smrg	    ResetState(sp);
2872d522f475Smrg	    break;
2873d522f475Smrg
2874d522f475Smrg	case CASE_DECRQLP:
2875d522f475Smrg	    TRACE(("DECRQLP - Request Locator Position\n"));
2876d522f475Smrg	    if (param[0] < 2) {
2877d522f475Smrg		/* Issue DECLRP Locator Position Report */
2878d522f475Smrg		GetLocatorPosition(xw);
2879d522f475Smrg	    }
288020d2c4d2Smrg	    ResetState(sp);
2881d522f475Smrg	    break;
2882d522f475Smrg#endif /* OPT_DEC_LOCATOR */
2883d522f475Smrg
2884d522f475Smrg#if OPT_DEC_RECTOPS
2885d522f475Smrg	case CASE_CSI_DOLLAR_STATE:
2886492d43a5Smrg	    TRACE(("CASE_CSI_DOLLAR_STATE\n"));
2887d522f475Smrg	    /* csi dollar ($) */
2888492d43a5Smrg	    if (screen->vtXX_level >= 3)
2889d522f475Smrg		sp->parsestate = csi_dollar_table;
2890d522f475Smrg	    else
2891d522f475Smrg		sp->parsestate = eigtable;
2892d522f475Smrg	    break;
2893d522f475Smrg
2894d522f475Smrg	case CASE_CSI_STAR_STATE:
2895492d43a5Smrg	    TRACE(("CASE_CSI_STAR_STATE\n"));
2896492d43a5Smrg	    /* csi star (*) */
2897d522f475Smrg	    if (screen->vtXX_level >= 4)
2898d522f475Smrg		sp->parsestate = csi_star_table;
2899d522f475Smrg	    else
2900d522f475Smrg		sp->parsestate = eigtable;
2901d522f475Smrg	    break;
2902d522f475Smrg
2903d522f475Smrg	case CASE_DECCRA:
2904492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2905492d43a5Smrg		TRACE(("CASE_DECCRA - Copy rectangular area\n"));
2906492d43a5Smrg		xtermParseRect(xw, nparam, param, &myRect);
2907492d43a5Smrg		ScrnCopyRectangle(xw, &myRect, nparam - 5, param + 5);
2908492d43a5Smrg	    }
290920d2c4d2Smrg	    ResetState(sp);
2910d522f475Smrg	    break;
2911d522f475Smrg
2912d522f475Smrg	case CASE_DECERA:
2913492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2914492d43a5Smrg		TRACE(("CASE_DECERA - Erase rectangular area\n"));
2915492d43a5Smrg		xtermParseRect(xw, nparam, param, &myRect);
2916492d43a5Smrg		ScrnFillRectangle(xw, &myRect, ' ', 0, True);
2917492d43a5Smrg	    }
291820d2c4d2Smrg	    ResetState(sp);
2919d522f475Smrg	    break;
2920d522f475Smrg
2921d522f475Smrg	case CASE_DECFRA:
2922492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2923492d43a5Smrg		TRACE(("CASE_DECFRA - Fill rectangular area\n"));
2924492d43a5Smrg		if (nparam > 0
2925492d43a5Smrg		    && ((param[0] >= 32 && param[0] <= 126)
2926492d43a5Smrg			|| (param[0] >= 160 && param[0] <= 255))) {
2927492d43a5Smrg		    xtermParseRect(xw, nparam - 1, param + 1, &myRect);
2928492d43a5Smrg		    ScrnFillRectangle(xw, &myRect, param[0], xw->flags, True);
2929492d43a5Smrg		}
2930d522f475Smrg	    }
293120d2c4d2Smrg	    ResetState(sp);
2932d522f475Smrg	    break;
2933d522f475Smrg
2934d522f475Smrg	case CASE_DECSERA:
2935492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2936492d43a5Smrg		TRACE(("CASE_DECSERA - Selective erase rectangular area\n"));
2937492d43a5Smrg		xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2938492d43a5Smrg		ScrnWipeRectangle(xw, &myRect);
2939492d43a5Smrg	    }
294020d2c4d2Smrg	    ResetState(sp);
2941d522f475Smrg	    break;
2942d522f475Smrg
2943d522f475Smrg	case CASE_DECSACE:
2944d522f475Smrg	    TRACE(("CASE_DECSACE - Select attribute change extent\n"));
2945d522f475Smrg	    screen->cur_decsace = param[0];
294620d2c4d2Smrg	    ResetState(sp);
2947d522f475Smrg	    break;
2948d522f475Smrg
2949d522f475Smrg	case CASE_DECCARA:
2950492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2951492d43a5Smrg		TRACE(("CASE_DECCARA - Change attributes in rectangular area\n"));
2952492d43a5Smrg		xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2953492d43a5Smrg		ScrnMarkRectangle(xw, &myRect, False, nparam - 4, param + 4);
2954492d43a5Smrg	    }
295520d2c4d2Smrg	    ResetState(sp);
2956d522f475Smrg	    break;
2957d522f475Smrg
2958d522f475Smrg	case CASE_DECRARA:
2959492d43a5Smrg	    if (screen->vtXX_level >= 4) {
2960492d43a5Smrg		TRACE(("CASE_DECRARA - Reverse attributes in rectangular area\n"));
2961492d43a5Smrg		xtermParseRect(xw, nparam > 4 ? 4 : nparam, param, &myRect);
2962492d43a5Smrg		ScrnMarkRectangle(xw, &myRect, True, nparam - 4, param + 4);
2963492d43a5Smrg	    }
2964492d43a5Smrg	    ResetState(sp);
2965492d43a5Smrg	    break;
2966492d43a5Smrg
2967492d43a5Smrg	case CASE_RQM:
2968492d43a5Smrg	    TRACE(("CASE_RQM\n"));
2969492d43a5Smrg	    do_rpm(xw, nparam, param);
2970492d43a5Smrg	    ResetState(sp);
2971492d43a5Smrg	    break;
2972492d43a5Smrg
2973492d43a5Smrg	case CASE_DECRQM:
2974492d43a5Smrg	    TRACE(("CASE_DECRQM\n"));
2975492d43a5Smrg	    do_decrpm(xw, nparam, param);
297620d2c4d2Smrg	    ResetState(sp);
2977d522f475Smrg	    break;
2978492d43a5Smrg
2979492d43a5Smrg	case CASE_CSI_DEC_DOLLAR_STATE:
2980492d43a5Smrg	    TRACE(("CASE_CSI_DEC_DOLLAR_STATE\n"));
2981492d43a5Smrg	    /* csi ? dollar ($) */
2982492d43a5Smrg	    sp->parsestate = csi_dec_dollar_table;
2983492d43a5Smrg	    break;
2984d522f475Smrg#else
2985d522f475Smrg	case CASE_CSI_DOLLAR_STATE:
2986d522f475Smrg	    /* csi dollar ($) */
2987d522f475Smrg	    sp->parsestate = eigtable;
2988d522f475Smrg	    break;
2989d522f475Smrg
2990d522f475Smrg	case CASE_CSI_STAR_STATE:
2991d522f475Smrg	    /* csi dollar (*) */
2992d522f475Smrg	    sp->parsestate = eigtable;
2993d522f475Smrg	    break;
2994492d43a5Smrg
2995492d43a5Smrg	case CASE_CSI_DEC_DOLLAR_STATE:
2996492d43a5Smrg	    /* csi ? dollar ($) */
2997492d43a5Smrg	    sp->parsestate = eigtable;
2998492d43a5Smrg	    break;
2999d522f475Smrg#endif /* OPT_DEC_RECTOPS */
3000d522f475Smrg
3001d522f475Smrg	case CASE_S7C1T:
3002d522f475Smrg	    TRACE(("CASE_S7C1T\n"));
3003d522f475Smrg	    show_8bit_control(False);
300420d2c4d2Smrg	    ResetState(sp);
3005d522f475Smrg	    break;
3006d522f475Smrg
3007d522f475Smrg	case CASE_S8C1T:
3008d522f475Smrg	    TRACE(("CASE_S8C1T\n"));
3009d522f475Smrg#if OPT_VT52_MODE
3010d522f475Smrg	    if (screen->vtXX_level <= 1)
3011d522f475Smrg		break;
3012d522f475Smrg#endif
3013d522f475Smrg	    show_8bit_control(True);
301420d2c4d2Smrg	    ResetState(sp);
3015d522f475Smrg	    break;
3016d522f475Smrg
3017d522f475Smrg	case CASE_OSC:
3018d522f475Smrg	    TRACE(("CASE_OSC: Operating System Command\n"));
3019d522f475Smrg	    sp->parsestate = sos_table;
3020d522f475Smrg	    sp->string_mode = ANSI_OSC;
3021d522f475Smrg	    break;
3022d522f475Smrg
3023d522f475Smrg	case CASE_RIS:
3024d522f475Smrg	    TRACE(("CASE_RIS\n"));
3025d522f475Smrg	    VTReset(xw, True, True);
302620d2c4d2Smrg	    ResetState(sp);
3027d522f475Smrg	    break;
3028d522f475Smrg
3029d522f475Smrg	case CASE_DECSTR:
3030d522f475Smrg	    TRACE(("CASE_DECSTR\n"));
3031d522f475Smrg	    VTReset(xw, False, False);
303220d2c4d2Smrg	    ResetState(sp);
3033d522f475Smrg	    break;
3034d522f475Smrg
3035d522f475Smrg	case CASE_REP:
3036d522f475Smrg	    TRACE(("CASE_REP\n"));
3037d522f475Smrg	    if (sp->lastchar >= 0 &&
3038d522f475Smrg		sp->lastchar < 256 &&
3039d522f475Smrg		sp->groundtable[E2A(sp->lastchar)] == CASE_PRINT) {
3040d522f475Smrg		IChar repeated[2];
3041d522f475Smrg		count = (param[0] < 1) ? 1 : param[0];
30422eaa94a1Schristos		repeated[0] = (IChar) sp->lastchar;
3043d522f475Smrg		while (count-- > 0) {
3044d522f475Smrg		    dotext(xw,
3045d522f475Smrg			   screen->gsets[(int) (screen->curgl)],
3046d522f475Smrg			   repeated, 1);
3047d522f475Smrg		}
3048d522f475Smrg	    }
304920d2c4d2Smrg	    ResetState(sp);
3050d522f475Smrg	    break;
3051d522f475Smrg
3052d522f475Smrg	case CASE_LS2:
3053d522f475Smrg	    TRACE(("CASE_LS2\n"));
3054d522f475Smrg	    screen->curgl = 2;
305520d2c4d2Smrg	    ResetState(sp);
3056d522f475Smrg	    break;
3057d522f475Smrg
3058d522f475Smrg	case CASE_LS3:
3059d522f475Smrg	    TRACE(("CASE_LS3\n"));
3060d522f475Smrg	    screen->curgl = 3;
306120d2c4d2Smrg	    ResetState(sp);
3062d522f475Smrg	    break;
3063d522f475Smrg
3064d522f475Smrg	case CASE_LS3R:
3065d522f475Smrg	    TRACE(("CASE_LS3R\n"));
3066d522f475Smrg	    screen->curgr = 3;
306720d2c4d2Smrg	    ResetState(sp);
3068d522f475Smrg	    break;
3069d522f475Smrg
3070d522f475Smrg	case CASE_LS2R:
3071d522f475Smrg	    TRACE(("CASE_LS2R\n"));
3072d522f475Smrg	    screen->curgr = 2;
307320d2c4d2Smrg	    ResetState(sp);
3074d522f475Smrg	    break;
3075d522f475Smrg
3076d522f475Smrg	case CASE_LS1R:
3077d522f475Smrg	    TRACE(("CASE_LS1R\n"));
3078d522f475Smrg	    screen->curgr = 1;
307920d2c4d2Smrg	    ResetState(sp);
3080d522f475Smrg	    break;
3081d522f475Smrg
3082d522f475Smrg	case CASE_XTERM_SAVE:
3083d522f475Smrg	    savemodes(xw);
308420d2c4d2Smrg	    ResetState(sp);
3085d522f475Smrg	    break;
3086d522f475Smrg
3087d522f475Smrg	case CASE_XTERM_RESTORE:
3088d522f475Smrg	    restoremodes(xw);
308920d2c4d2Smrg	    ResetState(sp);
3090d522f475Smrg	    break;
3091d522f475Smrg
3092d522f475Smrg	case CASE_XTERM_WINOPS:
3093d522f475Smrg	    TRACE(("CASE_XTERM_WINOPS\n"));
309420d2c4d2Smrg	    window_ops(xw);
309520d2c4d2Smrg	    ResetState(sp);
3096d522f475Smrg	    break;
3097d522f475Smrg#if OPT_WIDE_CHARS
3098d522f475Smrg	case CASE_ESC_PERCENT:
3099d522f475Smrg	    sp->parsestate = esc_pct_table;
3100d522f475Smrg	    break;
3101d522f475Smrg
3102d522f475Smrg	case CASE_UTF8:
3103d522f475Smrg	    /* If we did not set UTF-8 mode from resource or the
3104d522f475Smrg	     * command-line, allow it to be enabled/disabled by
3105d522f475Smrg	     * control sequence.
3106d522f475Smrg	     */
3107d522f475Smrg	    if (screen->wide_chars
3108d522f475Smrg		&& screen->utf8_mode != uAlways) {
310920d2c4d2Smrg		if (!screen->wide_chars) {
311020d2c4d2Smrg		    WriteNow();
311120d2c4d2Smrg		    ChangeToWide(xw);
311220d2c4d2Smrg		}
3113d522f475Smrg		switchPtyData(screen, c == 'G');
3114d522f475Smrg		TRACE(("UTF8 mode %s\n",
3115d522f475Smrg		       BtoS(screen->utf8_mode)));
3116d522f475Smrg	    } else {
3117d522f475Smrg		TRACE(("UTF8 mode NOT turned %s (%s)\n",
3118d522f475Smrg		       BtoS(c == 'G'),
3119d522f475Smrg		       (screen->utf8_mode == uAlways)
3120d522f475Smrg		       ? "UTF-8 mode set from command-line"
3121d522f475Smrg		       : "wideChars resource was not set"));
3122d522f475Smrg	    }
312320d2c4d2Smrg	    ResetState(sp);
3124d522f475Smrg	    break;
3125d522f475Smrg#endif
3126d522f475Smrg#if OPT_MOD_FKEYS
3127d522f475Smrg	case CASE_SET_MOD_FKEYS:
3128d522f475Smrg	    TRACE(("CASE_SET_MOD_FKEYS\n"));
3129d522f475Smrg	    if (nparam >= 1) {
3130d522f475Smrg		set_mod_fkeys(xw, param[0], nparam > 1 ? param[1] : DEFAULT, True);
3131d522f475Smrg	    } else {
3132d522f475Smrg		for (row = 1; row <= 5; ++row)
3133d522f475Smrg		    set_mod_fkeys(xw, row, DEFAULT, True);
3134d522f475Smrg	    }
3135d522f475Smrg	    break;
3136d522f475Smrg	case CASE_SET_MOD_FKEYS0:
3137d522f475Smrg	    TRACE(("CASE_SET_MOD_FKEYS0\n"));
3138d522f475Smrg	    if (nparam >= 1 && param[0] != DEFAULT) {
3139d522f475Smrg		set_mod_fkeys(xw, param[0], -1, False);
3140d522f475Smrg	    } else {
3141d522f475Smrg		xw->keyboard.modify_now.function_keys = -1;
3142d522f475Smrg	    }
3143d522f475Smrg	    break;
3144d522f475Smrg#endif
3145d522f475Smrg	case CASE_HIDE_POINTER:
3146d522f475Smrg	    TRACE(("CASE_HIDE_POINTER\n"));
3147d522f475Smrg	    if (nparam >= 1 && param[0] != DEFAULT) {
3148d522f475Smrg		screen->pointer_mode = param[0];
3149d522f475Smrg	    } else {
3150d522f475Smrg		screen->pointer_mode = DEF_POINTER_MODE;
3151d522f475Smrg	    }
3152d522f475Smrg	    break;
3153d522f475Smrg
315420d2c4d2Smrg	case CASE_SM_TITLE:
315520d2c4d2Smrg	    TRACE(("CASE_SM_TITLE\n"));
315620d2c4d2Smrg	    if (nparam >= 1) {
315720d2c4d2Smrg		int n;
315820d2c4d2Smrg		for (n = 0; n < nparam; ++n) {
315920d2c4d2Smrg		    if (param[n] != DEFAULT)
316020d2c4d2Smrg			screen->title_modes |= (1 << param[n]);
316120d2c4d2Smrg		}
316220d2c4d2Smrg	    } else {
316320d2c4d2Smrg		screen->title_modes = DEF_TITLE_MODES;
316420d2c4d2Smrg	    }
316520d2c4d2Smrg	    TRACE(("...title_modes %#x\n", screen->title_modes));
316620d2c4d2Smrg	    break;
316720d2c4d2Smrg
316820d2c4d2Smrg	case CASE_RM_TITLE:
316920d2c4d2Smrg	    TRACE(("CASE_RM_TITLE\n"));
317020d2c4d2Smrg	    if (nparam >= 1) {
317120d2c4d2Smrg		int n;
317220d2c4d2Smrg		for (n = 0; n < nparam; ++n) {
317320d2c4d2Smrg		    if (param[n] != DEFAULT)
317420d2c4d2Smrg			screen->title_modes &= ~(1 << param[n]);
317520d2c4d2Smrg		}
317620d2c4d2Smrg	    } else {
317720d2c4d2Smrg		screen->title_modes = DEF_TITLE_MODES;
317820d2c4d2Smrg	    }
317920d2c4d2Smrg	    TRACE(("...title_modes %#x\n", screen->title_modes));
318020d2c4d2Smrg	    break;
318120d2c4d2Smrg
3182d522f475Smrg	case CASE_CSI_IGNORE:
3183d522f475Smrg	    sp->parsestate = cigtable;
3184d522f475Smrg	    break;
318520d2c4d2Smrg
318620d2c4d2Smrg	case CASE_DECSWBV:
318720d2c4d2Smrg	    TRACE(("CASE_DECSWBV\n"));
318820d2c4d2Smrg	    switch ((nparam >= 1) ? param[0] : DEFAULT) {
318920d2c4d2Smrg	    case 2:
319020d2c4d2Smrg	    case 3:
319120d2c4d2Smrg	    case 4:
319220d2c4d2Smrg		screen->warningVolume = bvLow;
319320d2c4d2Smrg		break;
319420d2c4d2Smrg	    case 5:
319520d2c4d2Smrg	    case 6:
319620d2c4d2Smrg	    case 7:
319720d2c4d2Smrg	    case 8:
319820d2c4d2Smrg		screen->warningVolume = bvHigh;
319920d2c4d2Smrg		break;
320020d2c4d2Smrg	    default:
320120d2c4d2Smrg		screen->warningVolume = bvOff;
320220d2c4d2Smrg		break;
320320d2c4d2Smrg	    }
320420d2c4d2Smrg	    TRACE(("...warningVolume %d\n", screen->warningVolume));
320520d2c4d2Smrg	    ResetState(sp);
320620d2c4d2Smrg	    break;
320720d2c4d2Smrg
320820d2c4d2Smrg	case CASE_DECSMBV:
320920d2c4d2Smrg	    TRACE(("CASE_DECSMBV\n"));
321020d2c4d2Smrg	    switch ((nparam >= 1) ? param[0] : DEFAULT) {
321120d2c4d2Smrg	    case 2:
321220d2c4d2Smrg	    case 3:
321320d2c4d2Smrg	    case 4:
321420d2c4d2Smrg		screen->marginVolume = bvLow;
321520d2c4d2Smrg		break;
321620d2c4d2Smrg	    case 0:
321720d2c4d2Smrg	    case DEFAULT:
321820d2c4d2Smrg	    case 5:
321920d2c4d2Smrg	    case 6:
322020d2c4d2Smrg	    case 7:
322120d2c4d2Smrg	    case 8:
322220d2c4d2Smrg		screen->marginVolume = bvHigh;
322320d2c4d2Smrg		break;
322420d2c4d2Smrg	    default:
322520d2c4d2Smrg		screen->marginVolume = bvOff;
322620d2c4d2Smrg		break;
322720d2c4d2Smrg	    }
322820d2c4d2Smrg	    TRACE(("...marginVolume %d\n", screen->marginVolume));
322920d2c4d2Smrg	    ResetState(sp);
323020d2c4d2Smrg	    break;
3231d522f475Smrg	}
3232d522f475Smrg	if (sp->parsestate == sp->groundtable)
3233d522f475Smrg	    sp->lastchar = thischar;
3234d522f475Smrg    } while (0);
3235d522f475Smrg
3236d522f475Smrg#if OPT_WIDE_CHARS
32372eaa94a1Schristos    screen->utf8_inparse = (Boolean) ((screen->utf8_mode != uFalse)
32382eaa94a1Schristos				      && (sp->parsestate != sos_table));
3239d522f475Smrg#endif
3240d522f475Smrg
3241d522f475Smrg    return True;
3242d522f475Smrg}
3243d522f475Smrg
3244d522f475Smrgstatic void
3245d522f475SmrgVTparse(XtermWidget xw)
3246d522f475Smrg{
3247d522f475Smrg    TScreen *screen;
3248d522f475Smrg
3249d522f475Smrg    /* We longjmp back to this point in VTReset() */
3250d522f475Smrg    (void) setjmp(vtjmpbuf);
325120d2c4d2Smrg    screen = TScreenOf(xw);
3252d522f475Smrg    memset(&myState, 0, sizeof(myState));
32532eaa94a1Schristos    myState.scssize = 94;	/* number of printable/nonspace ASCII */
3254d522f475Smrg    myState.lastchar = -1;	/* not a legal IChar */
3255d522f475Smrg    myState.nextstate = -1;	/* not a legal state */
3256d522f475Smrg
32572eaa94a1Schristos    init_groundtable(screen, &myState);
32582eaa94a1Schristos    myState.parsestate = myState.groundtable;
32592eaa94a1Schristos
3260d522f475Smrg    do {
3261d522f475Smrg    } while (doparsing(xw, doinput(), &myState));
3262d522f475Smrg}
3263d522f475Smrg
3264d522f475Smrgstatic Char *v_buffer;		/* pointer to physical buffer */
3265d522f475Smrgstatic Char *v_bufstr = NULL;	/* beginning of area to write */
3266d522f475Smrgstatic Char *v_bufptr;		/* end of area to write */
3267d522f475Smrgstatic Char *v_bufend;		/* end of physical buffer */
3268d522f475Smrg
3269d522f475Smrg/* Write data to the pty as typed by the user, pasted with the mouse,
3270d522f475Smrg   or generated by us in response to a query ESC sequence. */
3271d522f475Smrg
327220d2c4d2Smrgvoid
3273492d43a5Smrgv_write(int f, const Char * data, unsigned len)
3274d522f475Smrg{
3275d522f475Smrg    int riten;
3276d522f475Smrg
327720d2c4d2Smrg    TRACE2(("v_write(%d:%s)\n", len, visibleChars(data, len)));
3278d522f475Smrg    if (v_bufstr == NULL && len > 0) {
3279956cc18dSsnj	v_buffer = (Char *) XtMalloc((Cardinal) len);
3280d522f475Smrg	v_bufstr = v_buffer;
3281d522f475Smrg	v_bufptr = v_buffer;
3282d522f475Smrg	v_bufend = v_buffer + len;
3283d522f475Smrg    }
3284d522f475Smrg#ifdef DEBUG
3285d522f475Smrg    if (debug) {
3286d522f475Smrg	fprintf(stderr, "v_write called with %d bytes (%d left over)",
3287d522f475Smrg		len, v_bufptr - v_bufstr);
3288d522f475Smrg	if (len > 1 && len < 10)
3289492d43a5Smrg	    fprintf(stderr, " \"%.*s\"", len, (const char *) data);
3290d522f475Smrg	fprintf(stderr, "\n");
3291d522f475Smrg    }
3292d522f475Smrg#endif
3293d522f475Smrg
3294d522f475Smrg#ifdef VMS
329520d2c4d2Smrg    if ((1 << f) != pty_mask) {
3296492d43a5Smrg	tt_write((const char *) data, len);
329720d2c4d2Smrg	return;
329820d2c4d2Smrg    }
3299d522f475Smrg#else /* VMS */
330020d2c4d2Smrg    if (!FD_ISSET(f, &pty_mask)) {
3301492d43a5Smrg	IGNORE_RC(write(f, (const char *) data, (size_t) len));
330220d2c4d2Smrg	return;
330320d2c4d2Smrg    }
3304d522f475Smrg#endif /* VMS */
3305d522f475Smrg
3306d522f475Smrg    /*
3307d522f475Smrg     * Append to the block we already have.
3308d522f475Smrg     * Always doing this simplifies the code, and
3309d522f475Smrg     * isn't too bad, either.  If this is a short
3310d522f475Smrg     * block, it isn't too expensive, and if this is
3311d522f475Smrg     * a long block, we won't be able to write it all
3312d522f475Smrg     * anyway.
3313d522f475Smrg     */
3314d522f475Smrg
3315d522f475Smrg    if (len > 0) {
3316d522f475Smrg#if OPT_DABBREV
331720d2c4d2Smrg	TScreenOf(term)->dabbrev_working = False;	/* break dabbrev sequence */
3318d522f475Smrg#endif
3319d522f475Smrg	if (v_bufend < v_bufptr + len) {	/* we've run out of room */
3320d522f475Smrg	    if (v_bufstr != v_buffer) {
3321d522f475Smrg		/* there is unused space, move everything down */
3322d522f475Smrg		/* possibly overlapping memmove here */
3323d522f475Smrg#ifdef DEBUG
3324d522f475Smrg		if (debug)
3325d522f475Smrg		    fprintf(stderr, "moving data down %d\n",
3326d522f475Smrg			    v_bufstr - v_buffer);
3327d522f475Smrg#endif
332820d2c4d2Smrg		memmove(v_buffer, v_bufstr, (size_t) (v_bufptr - v_bufstr));
3329d522f475Smrg		v_bufptr -= v_bufstr - v_buffer;
3330d522f475Smrg		v_bufstr = v_buffer;
3331d522f475Smrg	    }
3332d522f475Smrg	    if (v_bufend < v_bufptr + len) {
3333d522f475Smrg		/* still won't fit: get more space */
3334d522f475Smrg		/* Don't use XtRealloc because an error is not fatal. */
33352eaa94a1Schristos		unsigned size = (unsigned) (v_bufptr - v_buffer);
3336d522f475Smrg		v_buffer = TypeRealloc(Char, size + len, v_buffer);
3337d522f475Smrg		if (v_buffer) {
3338d522f475Smrg#ifdef DEBUG
3339d522f475Smrg		    if (debug)
3340d522f475Smrg			fprintf(stderr, "expanded buffer to %d\n",
3341d522f475Smrg				size + len);
3342d522f475Smrg#endif
3343d522f475Smrg		    v_bufstr = v_buffer;
3344d522f475Smrg		    v_bufptr = v_buffer + size;
3345d522f475Smrg		    v_bufend = v_bufptr + len;
3346d522f475Smrg		} else {
3347d522f475Smrg		    /* no memory: ignore entire write request */
3348d522f475Smrg		    fprintf(stderr, "%s: cannot allocate buffer space\n",
3349a1f3da82Smrg			    ProgramName);
3350d522f475Smrg		    v_buffer = v_bufstr;	/* restore clobbered pointer */
3351d522f475Smrg		}
3352d522f475Smrg	    }
3353d522f475Smrg	}
3354d522f475Smrg	if (v_bufend >= v_bufptr + len) {
3355d522f475Smrg	    /* new stuff will fit */
335620d2c4d2Smrg	    memmove(v_bufptr, data, (size_t) len);
3357d522f475Smrg	    v_bufptr += len;
3358d522f475Smrg	}
3359d522f475Smrg    }
3360d522f475Smrg
3361d522f475Smrg    /*
3362d522f475Smrg     * Write out as much of the buffer as we can.
3363d522f475Smrg     * Be careful not to overflow the pty's input silo.
3364d522f475Smrg     * We are conservative here and only write
3365d522f475Smrg     * a small amount at a time.
3366d522f475Smrg     *
3367d522f475Smrg     * If we can't push all the data into the pty yet, we expect write
3368d522f475Smrg     * to return a non-negative number less than the length requested
3369d522f475Smrg     * (if some data written) or -1 and set errno to EAGAIN,
3370d522f475Smrg     * EWOULDBLOCK, or EINTR (if no data written).
3371d522f475Smrg     *
3372d522f475Smrg     * (Not all systems do this, sigh, so the code is actually
3373d522f475Smrg     * a little more forgiving.)
3374d522f475Smrg     */
3375d522f475Smrg
3376d522f475Smrg#define MAX_PTY_WRITE 128	/* 1/2 POSIX minimum MAX_INPUT */
3377d522f475Smrg
3378d522f475Smrg    if (v_bufptr > v_bufstr) {
3379d522f475Smrg#ifdef VMS
3380d522f475Smrg	riten = tt_write(v_bufstr,
3381d522f475Smrg			 ((v_bufptr - v_bufstr <= VMS_TERM_BUFFER_SIZE)
3382d522f475Smrg			  ? v_bufptr - v_bufstr
3383d522f475Smrg			  : VMS_TERM_BUFFER_SIZE));
3384d522f475Smrg	if (riten == 0)
3385d522f475Smrg	    return (riten);
3386d522f475Smrg#else /* VMS */
338720d2c4d2Smrg	riten = (int) write(f, v_bufstr,
338820d2c4d2Smrg			    (size_t) ((v_bufptr - v_bufstr <= MAX_PTY_WRITE)
338920d2c4d2Smrg				      ? v_bufptr - v_bufstr
339020d2c4d2Smrg				      : MAX_PTY_WRITE));
3391d522f475Smrg	if (riten < 0)
3392d522f475Smrg#endif /* VMS */
3393d522f475Smrg	{
3394d522f475Smrg#ifdef DEBUG
3395d522f475Smrg	    if (debug)
3396d522f475Smrg		perror("write");
3397d522f475Smrg#endif
3398d522f475Smrg	    riten = 0;
3399d522f475Smrg	}
3400d522f475Smrg#ifdef DEBUG
3401d522f475Smrg	if (debug)
3402d522f475Smrg	    fprintf(stderr, "write called with %d, wrote %d\n",
3403d522f475Smrg		    v_bufptr - v_bufstr <= MAX_PTY_WRITE ?
3404d522f475Smrg		    v_bufptr - v_bufstr : MAX_PTY_WRITE,
3405d522f475Smrg		    riten);
3406d522f475Smrg#endif
3407d522f475Smrg	v_bufstr += riten;
3408d522f475Smrg	if (v_bufstr >= v_bufptr)	/* we wrote it all */
3409d522f475Smrg	    v_bufstr = v_bufptr = v_buffer;
3410d522f475Smrg    }
3411d522f475Smrg
3412d522f475Smrg    /*
3413d522f475Smrg     * If we have lots of unused memory allocated, return it
3414d522f475Smrg     */
3415d522f475Smrg    if (v_bufend - v_bufptr > 1024) {	/* arbitrary hysteresis */
3416d522f475Smrg	/* save pointers across realloc */
341720d2c4d2Smrg	int start = (int) (v_bufstr - v_buffer);
341820d2c4d2Smrg	int size = (int) (v_bufptr - v_buffer);
3419d522f475Smrg	unsigned allocsize = (unsigned) (size ? size : 1);
3420d522f475Smrg
3421d522f475Smrg	v_buffer = TypeRealloc(Char, allocsize, v_buffer);
3422d522f475Smrg	if (v_buffer) {
3423d522f475Smrg	    v_bufstr = v_buffer + start;
3424d522f475Smrg	    v_bufptr = v_buffer + size;
3425d522f475Smrg	    v_bufend = v_buffer + allocsize;
3426d522f475Smrg#ifdef DEBUG
3427d522f475Smrg	    if (debug)
3428d522f475Smrg		fprintf(stderr, "shrunk buffer to %d\n", allocsize);
3429d522f475Smrg#endif
3430d522f475Smrg	} else {
3431d522f475Smrg	    /* should we print a warning if couldn't return memory? */
3432d522f475Smrg	    v_buffer = v_bufstr - start;	/* restore clobbered pointer */
3433d522f475Smrg	}
3434d522f475Smrg    }
3435d522f475Smrg}
3436d522f475Smrg
3437d522f475Smrg#ifdef VMS
3438d522f475Smrg#define	ptymask()	(v_bufptr > v_bufstr ? pty_mask : 0)
3439d522f475Smrg
3440d522f475Smrgstatic void
3441d522f475Smrgin_put(XtermWidget xw)
3442d522f475Smrg{
3443d522f475Smrg    static PtySelect select_mask;
3444d522f475Smrg    static PtySelect write_mask;
3445d522f475Smrg    int update = VTbuffer->update;
3446d522f475Smrg    int size;
3447d522f475Smrg
3448d522f475Smrg    int status;
3449d522f475Smrg    Dimension replyWidth, replyHeight;
3450d522f475Smrg    XtGeometryResult stat;
3451d522f475Smrg
345220d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
3453d522f475Smrg    char *cp;
3454d522f475Smrg    int i;
3455d522f475Smrg
3456d522f475Smrg    select_mask = pty_mask;	/* force initial read */
3457d522f475Smrg    for (;;) {
3458d522f475Smrg
3459d522f475Smrg	/* if the terminal changed size, resize the widget */
3460d522f475Smrg	if (tt_changed) {
3461d522f475Smrg	    tt_changed = False;
3462d522f475Smrg
34632eaa94a1Schristos	    stat = REQ_RESIZE((Widget) xw,
34642eaa94a1Schristos			      ((Dimension) FontWidth(screen)
34652eaa94a1Schristos			       * (tt_width)
34662eaa94a1Schristos			       + 2 * screen->border
34672eaa94a1Schristos			       + screen->fullVwin.sb_info.width),
34682eaa94a1Schristos			      ((Dimension) FontHeight(screen)
34692eaa94a1Schristos			       * (tt_length)
34702eaa94a1Schristos			       + 2 * screen->border),
34712eaa94a1Schristos			      &replyWidth, &replyHeight);
3472d522f475Smrg
3473d522f475Smrg	    if (stat == XtGeometryYes || stat == XtGeometryDone) {
3474d522f475Smrg		xw->core.width = replyWidth;
3475d522f475Smrg		xw->core.height = replyHeight;
3476d522f475Smrg
3477d522f475Smrg		ScreenResize(xw, replyWidth, replyHeight, &xw->flags);
3478d522f475Smrg	    }
3479d522f475Smrg	    repairSizeHints();
3480d522f475Smrg	}
3481d522f475Smrg
3482d522f475Smrg	if (screen->eventMode == NORMAL
348320d2c4d2Smrg	    && readPtyData(xw, &select_mask, VTbuffer)) {
3484d522f475Smrg	    if (screen->scrollWidget
3485d522f475Smrg		&& screen->scrollttyoutput
3486d522f475Smrg		&& screen->topline < 0)
3487d522f475Smrg		/* Scroll to bottom */
348820d2c4d2Smrg		WindowScroll(xw, 0, False);
3489d522f475Smrg	    break;
3490d522f475Smrg	}
3491d522f475Smrg	if (screen->scroll_amt)
3492d522f475Smrg	    FlushScroll(xw);
3493d522f475Smrg	if (screen->cursor_set && CursorMoved(screen)) {
3494d522f475Smrg	    if (screen->cursor_state)
3495d522f475Smrg		HideCursor();
3496d522f475Smrg	    ShowCursor();
3497d522f475Smrg#if OPT_INPUT_METHOD
3498d522f475Smrg	    PreeditPosition(screen);
3499d522f475Smrg#endif
3500d522f475Smrg	} else if (screen->cursor_set != screen->cursor_state) {
3501d522f475Smrg	    if (screen->cursor_set)
3502d522f475Smrg		ShowCursor();
3503d522f475Smrg	    else
3504d522f475Smrg		HideCursor();
3505d522f475Smrg	}
3506d522f475Smrg
3507d522f475Smrg	if (QLength(screen->display)) {
3508d522f475Smrg	    select_mask = X_mask;
3509d522f475Smrg	} else {
3510d522f475Smrg	    write_mask = ptymask();
3511d522f475Smrg	    XFlush(screen->display);
3512d522f475Smrg	    select_mask = Select_mask;
3513d522f475Smrg	    if (screen->eventMode != NORMAL)
3514d522f475Smrg		select_mask = X_mask;
3515d522f475Smrg	}
3516d522f475Smrg	if (write_mask & ptymask()) {
3517d522f475Smrg	    v_write(screen->respond, 0, 0);	/* flush buffer */
3518d522f475Smrg	}
3519d522f475Smrg
3520d522f475Smrg	if (select_mask & X_mask) {
3521d522f475Smrg	    xevents();
3522d522f475Smrg	    if (VTbuffer->update != update)
3523d522f475Smrg		break;
3524d522f475Smrg	}
3525d522f475Smrg    }
3526d522f475Smrg}
3527d522f475Smrg#else /* VMS */
3528d522f475Smrg
3529d522f475Smrgstatic void
3530d522f475Smrgin_put(XtermWidget xw)
3531d522f475Smrg{
3532d522f475Smrg    static PtySelect select_mask;
3533d522f475Smrg    static PtySelect write_mask;
3534d522f475Smrg
353520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
3536d522f475Smrg    int i, time_select;
3537d522f475Smrg    int size;
3538d522f475Smrg    int update = VTbuffer->update;
3539d522f475Smrg
3540d522f475Smrg    static struct timeval select_timeout;
3541d522f475Smrg
3542d522f475Smrg#if OPT_BLINK_CURS
3543d522f475Smrg    /*
3544d522f475Smrg     * Compute the timeout for the blinking cursor to be much smaller than
3545d522f475Smrg     * the "on" or "off" interval.
3546d522f475Smrg     */
3547d522f475Smrg    int tick = ((screen->blink_on < screen->blink_off)
3548d522f475Smrg		? screen->blink_on
3549d522f475Smrg		: screen->blink_off);
3550d522f475Smrg    tick *= (1000 / 8);		/* 1000 for msec/usec, 8 for "much" smaller */
3551d522f475Smrg    if (tick < 1)
3552d522f475Smrg	tick = 1;
3553d522f475Smrg#endif
3554d522f475Smrg
3555d522f475Smrg    for (;;) {
3556d522f475Smrg	if (screen->eventMode == NORMAL
355720d2c4d2Smrg	    && (size = readPtyData(xw, &select_mask, VTbuffer)) != 0) {
3558d522f475Smrg	    if (screen->scrollWidget
3559d522f475Smrg		&& screen->scrollttyoutput
3560d522f475Smrg		&& screen->topline < 0)
356120d2c4d2Smrg		WindowScroll(xw, 0, False);	/* Scroll to bottom */
3562d522f475Smrg	    /* stop speed reading at some point to look for X stuff */
356320d2c4d2Smrg	    TRACE(("VTbuffer uses %ld/%d\n",
356420d2c4d2Smrg		   (long) (VTbuffer->last - VTbuffer->buffer),
3565d522f475Smrg		   BUF_SIZE));
3566d522f475Smrg	    if ((VTbuffer->last - VTbuffer->buffer) > BUF_SIZE) {
3567d522f475Smrg		FD_CLR(screen->respond, &select_mask);
3568d522f475Smrg		break;
3569d522f475Smrg	    }
3570d522f475Smrg#if defined(HAVE_SCHED_YIELD)
3571d522f475Smrg	    /*
3572d522f475Smrg	     * If we've read a full (small/fragment) buffer, let the operating
3573d522f475Smrg	     * system have a turn, and we'll resume reading until we've either
3574d522f475Smrg	     * read only a fragment of the buffer, or we've filled the large
3575d522f475Smrg	     * buffer (see above).  Doing this helps keep up with large bursts
3576d522f475Smrg	     * of output.
3577d522f475Smrg	     */
3578d522f475Smrg	    if (size == FRG_SIZE) {
3579d522f475Smrg		select_timeout.tv_sec = 0;
3580d522f475Smrg		i = Select(max_plus1, &select_mask, &write_mask, 0,
3581d522f475Smrg			   &select_timeout);
3582d522f475Smrg		if (i > 0) {
3583d522f475Smrg		    sched_yield();
3584d522f475Smrg		} else
3585d522f475Smrg		    break;
3586d522f475Smrg	    } else {
3587d522f475Smrg		break;
3588d522f475Smrg	    }
3589d522f475Smrg#else
3590d522f475Smrg	    (void) size;	/* unused in this branch */
3591d522f475Smrg	    break;
3592d522f475Smrg#endif
3593d522f475Smrg	}
3594d522f475Smrg	/* update the screen */
3595d522f475Smrg	if (screen->scroll_amt)
3596d522f475Smrg	    FlushScroll(xw);
3597d522f475Smrg	if (screen->cursor_set && CursorMoved(screen)) {
3598d522f475Smrg	    if (screen->cursor_state)
3599d522f475Smrg		HideCursor();
3600d522f475Smrg	    ShowCursor();
3601d522f475Smrg#if OPT_INPUT_METHOD
3602d522f475Smrg	    PreeditPosition(screen);
3603d522f475Smrg#endif
3604d522f475Smrg	} else if (screen->cursor_set != screen->cursor_state) {
3605d522f475Smrg	    if (screen->cursor_set)
3606d522f475Smrg		ShowCursor();
3607d522f475Smrg	    else
3608d522f475Smrg		HideCursor();
3609d522f475Smrg	}
3610d522f475Smrg
3611d522f475Smrg	XFlush(screen->display);	/* always flush writes before waiting */
3612d522f475Smrg
3613d522f475Smrg	/* Update the masks and, unless X events are already in the queue,
3614d522f475Smrg	   wait for I/O to be possible. */
3615d522f475Smrg	XFD_COPYSET(&Select_mask, &select_mask);
3616d522f475Smrg	/* in selection mode xterm does not read pty */
3617d522f475Smrg	if (screen->eventMode != NORMAL)
3618d522f475Smrg	    FD_CLR(screen->respond, &select_mask);
3619d522f475Smrg
3620d522f475Smrg	if (v_bufptr > v_bufstr) {
3621d522f475Smrg	    XFD_COPYSET(&pty_mask, &write_mask);
3622d522f475Smrg	} else
3623d522f475Smrg	    FD_ZERO(&write_mask);
3624d522f475Smrg	select_timeout.tv_sec = 0;
3625d522f475Smrg	time_select = 0;
3626d522f475Smrg
3627d522f475Smrg	/*
3628d522f475Smrg	 * if there's either an XEvent or an XtTimeout pending, just take
3629d522f475Smrg	 * a quick peek, i.e. timeout from the select() immediately.  If
3630d522f475Smrg	 * there's nothing pending, let select() block a little while, but
3631d522f475Smrg	 * for a shorter interval than the arrow-style scrollbar timeout.
3632d522f475Smrg	 * The blocking is optional, because it tends to increase the load
3633d522f475Smrg	 * on the host.
3634d522f475Smrg	 */
3635d522f475Smrg	if (XtAppPending(app_con)) {
3636d522f475Smrg	    select_timeout.tv_usec = 0;
3637d522f475Smrg	    time_select = 1;
3638d522f475Smrg	} else if (screen->awaitInput) {
3639d522f475Smrg	    select_timeout.tv_usec = 50000;
3640d522f475Smrg	    time_select = 1;
3641d522f475Smrg#if OPT_BLINK_CURS
3642d522f475Smrg	} else if ((screen->blink_timer != 0 &&
3643d522f475Smrg		    ((screen->select & FOCUS) || screen->always_highlight)) ||
3644d522f475Smrg		   (screen->cursor_state == BLINKED_OFF)) {
3645d522f475Smrg	    select_timeout.tv_usec = tick;
3646d522f475Smrg	    while (select_timeout.tv_usec > 1000000) {
3647d522f475Smrg		select_timeout.tv_usec -= 1000000;
3648d522f475Smrg		select_timeout.tv_sec++;
3649d522f475Smrg	    }
3650d522f475Smrg	    time_select = 1;
3651d522f475Smrg#endif
3652d522f475Smrg#if OPT_SESSION_MGT
3653d522f475Smrg	} else if (resource.sessionMgt) {
3654d522f475Smrg	    if (ice_fd >= 0)
3655d522f475Smrg		FD_SET(ice_fd, &select_mask);
3656d522f475Smrg#endif
3657d522f475Smrg	}
3658d522f475Smrg	if (need_cleanup)
3659d522f475Smrg	    Cleanup(0);
3660d522f475Smrg	i = Select(max_plus1, &select_mask, &write_mask, 0,
3661d522f475Smrg		   (time_select ? &select_timeout : 0));
3662d522f475Smrg	if (i < 0) {
3663d522f475Smrg	    if (errno != EINTR)
3664d522f475Smrg		SysError(ERROR_SELECT);
3665d522f475Smrg	    continue;
3666d522f475Smrg	}
3667d522f475Smrg
3668d522f475Smrg	/* if there is room to write more data to the pty, go write more */
3669d522f475Smrg	if (FD_ISSET(screen->respond, &write_mask)) {
3670d522f475Smrg	    v_write(screen->respond, (Char *) 0, 0);	/* flush buffer */
3671d522f475Smrg	}
3672d522f475Smrg
3673d522f475Smrg	/* if there are X events already in our queue, it
3674d522f475Smrg	   counts as being readable */
3675d522f475Smrg	if (XtAppPending(app_con) ||
3676d522f475Smrg	    FD_ISSET(ConnectionNumber(screen->display), &select_mask)) {
3677d522f475Smrg	    xevents();
3678d522f475Smrg	    if (VTbuffer->update != update)	/* HandleInterpret */
3679d522f475Smrg		break;
3680d522f475Smrg	}
3681d522f475Smrg
3682d522f475Smrg    }
3683d522f475Smrg}
3684d522f475Smrg#endif /* VMS */
3685d522f475Smrg
3686d522f475Smrgstatic IChar
3687d522f475Smrgdoinput(void)
3688d522f475Smrg{
3689d522f475Smrg    TScreen *screen = TScreenOf(term);
3690d522f475Smrg
3691d522f475Smrg    while (!morePtyData(screen, VTbuffer))
3692d522f475Smrg	in_put(term);
3693d522f475Smrg    return nextPtyData(screen, VTbuffer);
3694d522f475Smrg}
3695d522f475Smrg
3696d522f475Smrg#if OPT_INPUT_METHOD
3697d522f475Smrg/*
3698d522f475Smrg *  For OverTheSpot, client has to inform the position for XIM preedit.
3699d522f475Smrg */
3700d522f475Smrgstatic void
3701d522f475SmrgPreeditPosition(TScreen * screen)
3702d522f475Smrg{
3703956cc18dSsnj    LineData *ld;
3704d522f475Smrg    XPoint spot;
3705d522f475Smrg    XVaNestedList list;
3706d522f475Smrg
3707956cc18dSsnj    if (screen->xic
3708956cc18dSsnj	&& (ld = getLineData(screen, screen->cur_row)) != 0) {
3709956cc18dSsnj	spot.x = (short) LineCursorX(screen, ld, screen->cur_col);
3710956cc18dSsnj	spot.y = (short) (CursorY(screen, screen->cur_row) + screen->fs_ascent);
3711956cc18dSsnj	list = XVaCreateNestedList(0,
3712956cc18dSsnj				   XNSpotLocation, &spot,
3713956cc18dSsnj				   XNForeground, T_COLOR(screen, TEXT_FG),
3714956cc18dSsnj				   XNBackground, T_COLOR(screen, TEXT_BG),
3715956cc18dSsnj				   NULL);
3716956cc18dSsnj	XSetICValues(screen->xic, XNPreeditAttributes, list, NULL);
3717956cc18dSsnj	XFree(list);
3718956cc18dSsnj    }
3719d522f475Smrg}
3720d522f475Smrg#endif
3721d522f475Smrg
3722d522f475Smrgstatic void
3723d522f475SmrgWrapLine(XtermWidget xw)
3724d522f475Smrg{
372520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
3726956cc18dSsnj    LineData *ld = getLineData(screen, screen->cur_row);
3727d522f475Smrg
3728956cc18dSsnj    if (ld != 0) {
3729956cc18dSsnj	/* mark that we had to wrap this line */
3730956cc18dSsnj	LineSetFlag(ld, LINEWRAPPED);
3731492d43a5Smrg	if (screen->show_wrap_marks) {
3732492d43a5Smrg	    ShowWrapMarks(xw, screen->cur_row, ld);
3733492d43a5Smrg	}
3734956cc18dSsnj	xtermAutoPrint(xw, '\n');
3735956cc18dSsnj	xtermIndex(xw, 1);
3736956cc18dSsnj	set_cur_col(screen, 0);
3737956cc18dSsnj    }
3738d522f475Smrg}
3739d522f475Smrg
3740d522f475Smrg/*
3741d522f475Smrg * process a string of characters according to the character set indicated
3742d522f475Smrg * by charset.  worry about end of line conditions (wraparound if selected).
3743d522f475Smrg */
3744d522f475Smrgvoid
3745d522f475Smrgdotext(XtermWidget xw,
3746d522f475Smrg       int charset,
3747d522f475Smrg       IChar * buf,		/* start of characters to process */
3748d522f475Smrg       Cardinal len)		/* end */
3749d522f475Smrg{
375020d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
3751d522f475Smrg#if OPT_WIDE_CHARS
3752d522f475Smrg    Cardinal chars_chomped = 1;
3753d522f475Smrg    int next_col = screen->cur_col;
3754d522f475Smrg#else
3755d522f475Smrg    int next_col, last_col, this_col;	/* must be signed */
3756d522f475Smrg#endif
3757d522f475Smrg    Cardinal offset;
3758d522f475Smrg
3759d522f475Smrg#if OPT_WIDE_CHARS
3760d522f475Smrg    /* don't translate if we use UTF-8, and are not handling legacy support
3761d522f475Smrg     * for line-drawing characters.
3762d522f475Smrg     */
3763d522f475Smrg    if ((screen->utf8_mode == uFalse)
37642eaa94a1Schristos	|| (screen->vt100_graphics))
3765d522f475Smrg#endif
37662eaa94a1Schristos	if (!xtermCharSetOut(xw, buf, buf + len, charset))
3767d522f475Smrg	    return;
3768d522f475Smrg
3769d522f475Smrg    if_OPT_XMC_GLITCH(screen, {
3770d522f475Smrg	Cardinal n;
3771d522f475Smrg	if (charset != '?') {
3772d522f475Smrg	    for (n = 0; n < len; n++) {
3773d522f475Smrg		if (buf[n] == XMC_GLITCH)
3774d522f475Smrg		    buf[n] = XMC_GLITCH + 1;
3775d522f475Smrg	    }
3776d522f475Smrg	}
3777d522f475Smrg    });
3778d522f475Smrg
3779d522f475Smrg#if OPT_WIDE_CHARS
3780d522f475Smrg    for (offset = 0;
3781d522f475Smrg	 offset < len && (chars_chomped > 0 || screen->do_wrap);
3782d522f475Smrg	 offset += chars_chomped) {
3783d522f475Smrg	int width_available = MaxCols(screen) - screen->cur_col;
3784d522f475Smrg	int width_here = 0;
37852eaa94a1Schristos	Boolean need_wrap = False;
3786d522f475Smrg	int last_chomp = 0;
3787d522f475Smrg	chars_chomped = 0;
3788d522f475Smrg
3789d522f475Smrg	if (screen->do_wrap) {
37902eaa94a1Schristos	    screen->do_wrap = False;
3791d522f475Smrg	    if ((xw->flags & WRAPAROUND)) {
3792d522f475Smrg		WrapLine(xw);
3793d522f475Smrg		width_available = MaxCols(screen) - screen->cur_col;
3794d522f475Smrg		next_col = screen->cur_col;
3795d522f475Smrg	    }
3796d522f475Smrg	}
3797d522f475Smrg
3798d522f475Smrg	while (width_here <= width_available && chars_chomped < (len - offset)) {
3799d522f475Smrg	    if (!screen->utf8_mode
3800d522f475Smrg		|| (screen->vt100_graphics && charset == '0'))
3801d522f475Smrg		last_chomp = 1;
3802d522f475Smrg	    else
3803d522f475Smrg		last_chomp = my_wcwidth((int) buf[chars_chomped + offset]);
3804d522f475Smrg	    width_here += last_chomp;
3805d522f475Smrg	    chars_chomped++;
3806d522f475Smrg	}
3807d522f475Smrg
3808d522f475Smrg	if (width_here > width_available) {
3809d522f475Smrg	    if (last_chomp > MaxCols(screen))
3810d522f475Smrg		break;		/* give up - it is too big */
3811d522f475Smrg	    chars_chomped--;
3812d522f475Smrg	    width_here -= last_chomp;
3813d522f475Smrg	    if (chars_chomped > 0) {
38142eaa94a1Schristos		need_wrap = True;
3815d522f475Smrg	    }
3816d522f475Smrg	} else if (width_here == width_available) {
38172eaa94a1Schristos	    need_wrap = True;
3818d522f475Smrg	} else if (chars_chomped != (len - offset)) {
38192eaa94a1Schristos	    need_wrap = True;
3820d522f475Smrg	}
3821d522f475Smrg
3822d522f475Smrg	/*
3823d522f475Smrg	 * Split the wide characters back into separate arrays of 8-bit
3824d522f475Smrg	 * characters so we can use the existing interface.
3825d522f475Smrg	 *
3826d522f475Smrg	 * FIXME:  If we rewrote this interface, it would involve
3827d522f475Smrg	 * rewriting all of the memory-management for the screen
3828d522f475Smrg	 * buffers (perhaps this is simpler).
3829d522f475Smrg	 */
3830d522f475Smrg	if (chars_chomped != 0 && next_col <= screen->max_col) {
3831956cc18dSsnj	    WriteText(xw, buf + offset, chars_chomped);
3832d522f475Smrg	}
3833d522f475Smrg	next_col += width_here;
3834d522f475Smrg	screen->do_wrap = need_wrap;
3835d522f475Smrg    }
3836d522f475Smrg#else /* ! OPT_WIDE_CHARS */
3837d522f475Smrg
3838a1f3da82Smrg    for (offset = 0; offset < len; offset += (Cardinal) this_col) {
3839956cc18dSsnj#if OPT_DEC_CHRSET
3840956cc18dSsnj	LineData *ld = getLineData(screen, screen->cur_row);
3841956cc18dSsnj#endif
3842956cc18dSsnj
3843956cc18dSsnj	last_col = LineMaxCol(screen, ld);
3844d522f475Smrg	this_col = last_col - screen->cur_col + 1;
3845d522f475Smrg	if (this_col <= 1) {
3846d522f475Smrg	    if (screen->do_wrap) {
38472eaa94a1Schristos		screen->do_wrap = False;
3848d522f475Smrg		if ((xw->flags & WRAPAROUND)) {
3849d522f475Smrg		    WrapLine(xw);
3850d522f475Smrg		}
3851d522f475Smrg	    }
3852d522f475Smrg	    this_col = 1;
3853d522f475Smrg	}
3854a1f3da82Smrg	if (offset + (Cardinal) this_col > len) {
3855a1f3da82Smrg	    this_col = (int) (len - offset);
3856d522f475Smrg	}
3857d522f475Smrg	next_col = screen->cur_col + this_col;
3858d522f475Smrg
3859956cc18dSsnj	WriteText(xw, buf + offset, (unsigned) this_col);
3860d522f475Smrg
3861d522f475Smrg	/*
3862d522f475Smrg	 * The call to WriteText updates screen->cur_col.
3863d522f475Smrg	 * If screen->cur_col is less than next_col, we must have
3864d522f475Smrg	 * hit the right margin - so set the do_wrap flag.
3865d522f475Smrg	 */
3866a1f3da82Smrg	screen->do_wrap = (Boolean) (screen->cur_col < next_col);
3867d522f475Smrg    }
3868d522f475Smrg
3869d522f475Smrg#endif /* OPT_WIDE_CHARS */
3870d522f475Smrg}
3871d522f475Smrg
3872d522f475Smrg#if OPT_WIDE_CHARS
3873d522f475Smrgunsigned
3874956cc18dSsnjvisual_width(IChar * str, Cardinal len)
3875d522f475Smrg{
3876d522f475Smrg    /* returns the visual width of a string (doublewide characters count
3877d522f475Smrg       as 2, normalwide characters count as 1) */
38782eaa94a1Schristos    unsigned my_len = 0;
3879d522f475Smrg    while (len) {
3880956cc18dSsnj	int ch = (int) *str++;
3881956cc18dSsnj	if (isWide(ch))
3882d522f475Smrg	    my_len += 2;
3883d522f475Smrg	else
3884d522f475Smrg	    my_len++;
3885d522f475Smrg	len--;
3886d522f475Smrg    }
3887d522f475Smrg    return my_len;
3888d522f475Smrg}
3889d522f475Smrg#endif
3890d522f475Smrg
3891d522f475Smrg#if HANDLE_STRUCT_NOTIFY
389220d2c4d2Smrg/* Flag icon name with "***" on window output when iconified.
3893d522f475Smrg */
3894d522f475Smrgstatic void
3895d522f475SmrgHandleStructNotify(Widget w GCC_UNUSED,
3896d522f475Smrg		   XtPointer closure GCC_UNUSED,
3897d522f475Smrg		   XEvent * event,
3898d522f475Smrg		   Boolean * cont GCC_UNUSED)
3899d522f475Smrg{
3900d522f475Smrg    static char *icon_name;
3901d522f475Smrg    static Arg args[] =
3902d522f475Smrg    {
3903d522f475Smrg	{XtNiconName, (XtArgVal) & icon_name}
3904d522f475Smrg    };
39052eaa94a1Schristos    XtermWidget xw = term;
39062eaa94a1Schristos    TScreen *screen = TScreenOf(xw);
3907d522f475Smrg
3908d522f475Smrg    switch (event->type) {
3909d522f475Smrg    case MapNotify:
3910d522f475Smrg	TRACE(("HandleStructNotify(MapNotify)\n"));
3911d522f475Smrg#if OPT_ZICONBEEP
3912d522f475Smrg	if (screen->zIconBeep_flagged) {
3913d522f475Smrg	    screen->zIconBeep_flagged = False;
3914d522f475Smrg	    icon_name = NULL;
3915d522f475Smrg	    XtGetValues(toplevel, args, XtNumber(args));
3916d522f475Smrg	    if (icon_name != NULL) {
3917d522f475Smrg		char *buf = CastMallocN(char, strlen(icon_name));
3918d522f475Smrg		if (buf == NULL) {
3919d522f475Smrg		    screen->zIconBeep_flagged = True;
3920d522f475Smrg		    return;
3921d522f475Smrg		}
3922d522f475Smrg		strcpy(buf, icon_name + 4);
3923956cc18dSsnj		ChangeIconName(xw, buf);
3924d522f475Smrg		free(buf);
3925d522f475Smrg	    }
3926d522f475Smrg	}
3927d522f475Smrg#endif /* OPT_ZICONBEEP */
3928d522f475Smrg	mapstate = !IsUnmapped;
3929d522f475Smrg	break;
3930d522f475Smrg    case UnmapNotify:
3931d522f475Smrg	TRACE(("HandleStructNotify(UnmapNotify)\n"));
3932d522f475Smrg	mapstate = IsUnmapped;
3933d522f475Smrg	break;
3934d522f475Smrg    case ConfigureNotify:
3935d522f475Smrg	if (event->xconfigure.window == XtWindow(toplevel)) {
393620d2c4d2Smrg#if !OPT_TOOLBAR
39372eaa94a1Schristos	    int height, width;
39382eaa94a1Schristos
39392eaa94a1Schristos	    height = event->xconfigure.height;
39402eaa94a1Schristos	    width = event->xconfigure.width;
394120d2c4d2Smrg#endif
39422eaa94a1Schristos	    TRACE(("HandleStructNotify(ConfigureNotify) %d,%d %dx%d\n",
39432eaa94a1Schristos		   event->xconfigure.y, event->xconfigure.x,
39442eaa94a1Schristos		   event->xconfigure.height, event->xconfigure.width));
39452eaa94a1Schristos
3946d522f475Smrg#if OPT_TOOLBAR
3947d522f475Smrg	    /*
3948d522f475Smrg	     * The notification is for the top-level widget, but we care about
3949d522f475Smrg	     * vt100 (ignore the tek4014 window).
3950d522f475Smrg	     */
395120d2c4d2Smrg	    if (TScreenOf(xw)->Vshow) {
395220d2c4d2Smrg		VTwin *Vwin = WhichVWin(TScreenOf(xw));
3953d522f475Smrg		TbInfo *info = &(Vwin->tb_info);
3954d522f475Smrg		TbInfo save = *info;
3955d522f475Smrg
3956d522f475Smrg		if (info->menu_bar) {
3957d522f475Smrg		    XtVaGetValues(info->menu_bar,
3958d522f475Smrg				  XtNheight, &info->menu_height,
3959d522f475Smrg				  XtNborderWidth, &info->menu_border,
3960d522f475Smrg				  (XtPointer) 0);
3961d522f475Smrg
3962d522f475Smrg		    if (save.menu_height != info->menu_height
3963d522f475Smrg			|| save.menu_border != info->menu_border) {
3964d522f475Smrg
3965d522f475Smrg			TRACE(("...menu_height %d\n", info->menu_height));
3966d522f475Smrg			TRACE(("...menu_border %d\n", info->menu_border));
3967d522f475Smrg			TRACE(("...had height  %d, border %d\n",
3968d522f475Smrg			       save.menu_height,
3969d522f475Smrg			       save.menu_border));
3970d522f475Smrg
3971d522f475Smrg			/*
3972d522f475Smrg			 * FIXME:  Window manager still may be using the old
3973d522f475Smrg			 * values.  Try to fool it.
3974d522f475Smrg			 */
39752eaa94a1Schristos			REQ_RESIZE((Widget) xw,
39762eaa94a1Schristos				   screen->fullVwin.fullwidth,
39772eaa94a1Schristos				   (Dimension) (info->menu_height
39782eaa94a1Schristos						- save.menu_height
39792eaa94a1Schristos						+ screen->fullVwin.fullheight),
39802eaa94a1Schristos				   NULL, NULL);
3981d522f475Smrg			repairSizeHints();
3982d522f475Smrg		    }
3983d522f475Smrg		}
3984d522f475Smrg	    }
3985d522f475Smrg#else
39862eaa94a1Schristos	    if (height != xw->hints.height || width != xw->hints.width)
39872eaa94a1Schristos		RequestResize(xw, height, width, False);
3988d522f475Smrg#endif /* OPT_TOOLBAR */
3989d522f475Smrg	}
3990d522f475Smrg	break;
3991d522f475Smrg    case ReparentNotify:
3992d522f475Smrg	TRACE(("HandleStructNotify(ReparentNotify)\n"));
3993d522f475Smrg	break;
3994d522f475Smrg    default:
3995d522f475Smrg	TRACE(("HandleStructNotify(event %s)\n",
3996d522f475Smrg	       visibleEventType(event->type)));
3997d522f475Smrg	break;
3998d522f475Smrg    }
3999d522f475Smrg}
4000d522f475Smrg#endif /* HANDLE_STRUCT_NOTIFY */
4001d522f475Smrg
4002d522f475Smrg#if OPT_BLINK_CURS
4003d522f475Smrgstatic void
4004956cc18dSsnjSetCursorBlink(TScreen * screen, Bool enable)
4005d522f475Smrg{
4006956cc18dSsnj    screen->cursor_blink = (Boolean) enable;
4007d522f475Smrg    if (DoStartBlinking(screen)) {
4008d522f475Smrg	StartBlinking(screen);
4009d522f475Smrg    } else {
4010a1f3da82Smrg	/* EMPTY */
4011d522f475Smrg#if !OPT_BLINK_TEXT
4012d522f475Smrg	StopBlinking(screen);
4013d522f475Smrg#endif
4014d522f475Smrg    }
4015d522f475Smrg    update_cursorblink();
4016d522f475Smrg}
4017d522f475Smrg
4018d522f475Smrgvoid
4019d522f475SmrgToggleCursorBlink(TScreen * screen)
4020d522f475Smrg{
4021956cc18dSsnj    SetCursorBlink(screen, (Bool) (!(screen->cursor_blink)));
4022d522f475Smrg}
4023d522f475Smrg#endif
4024d522f475Smrg
4025d522f475Smrg/*
4026d522f475Smrg * process ANSI modes set, reset
4027d522f475Smrg */
4028d522f475Smrgstatic void
402920d2c4d2Smrgansi_modes(XtermWidget xw, BitFunc func)
4030d522f475Smrg{
4031d522f475Smrg    int i;
4032d522f475Smrg
4033d522f475Smrg    for (i = 0; i < nparam; ++i) {
4034d522f475Smrg	switch (param[i]) {
4035d522f475Smrg	case 2:		/* KAM (if set, keyboard locked */
4036d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_KAM);
4037d522f475Smrg	    break;
4038d522f475Smrg
4039d522f475Smrg	case 4:		/* IRM                          */
4040d522f475Smrg	    (*func) (&xw->flags, INSERT);
4041d522f475Smrg	    break;
4042d522f475Smrg
4043d522f475Smrg	case 12:		/* SRM (if set, local echo      */
4044d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_SRM);
4045d522f475Smrg	    break;
4046d522f475Smrg
4047d522f475Smrg	case 20:		/* LNM                          */
4048d522f475Smrg	    (*func) (&xw->flags, LINEFEED);
4049d522f475Smrg	    update_autolinefeed();
4050d522f475Smrg	    break;
4051d522f475Smrg	}
4052d522f475Smrg    }
4053d522f475Smrg}
4054d522f475Smrg
4055d522f475Smrg#define IsSM() (func == bitset)
4056d522f475Smrg
4057d522f475Smrg#define set_bool_mode(flag) \
40582eaa94a1Schristos	flag = (Boolean) IsSM()
4059d522f475Smrg
4060d522f475Smrgstatic void
4061d522f475Smrgreally_set_mousemode(XtermWidget xw,
4062d522f475Smrg		     Bool enabled,
40632eaa94a1Schristos		     XtermMouseModes mode)
4064d522f475Smrg{
406520d2c4d2Smrg    TScreenOf(xw)->send_mouse_pos = enabled ? mode : MOUSE_OFF;
406620d2c4d2Smrg    if (TScreenOf(xw)->send_mouse_pos != MOUSE_OFF)
4067d522f475Smrg	xtermShowPointer(xw, True);
4068d522f475Smrg}
4069d522f475Smrg
4070d522f475Smrg#define set_mousemode(mode) really_set_mousemode(xw, IsSM(), mode)
4071d522f475Smrg
4072d522f475Smrg#if OPT_READLINE
4073d522f475Smrg#define set_mouseflag(f)		\
4074d522f475Smrg	(IsSM()				\
4075d522f475Smrg	 ? SCREEN_FLAG_set(screen, f)	\
4076d522f475Smrg	 : SCREEN_FLAG_unset(screen, f))
4077d522f475Smrg#endif
4078d522f475Smrg
4079d522f475Smrg/*
4080d522f475Smrg * process DEC private modes set, reset
4081d522f475Smrg */
4082d522f475Smrgstatic void
408320d2c4d2Smrgdpmodes(XtermWidget xw, BitFunc func)
4084d522f475Smrg{
408520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
4086d522f475Smrg    int i, j;
40872eaa94a1Schristos    unsigned myflags;
4088d522f475Smrg
4089d522f475Smrg    for (i = 0; i < nparam; ++i) {
4090d522f475Smrg	TRACE(("%s %d\n", IsSM()? "DECSET" : "DECRST", param[i]));
4091d522f475Smrg	switch (param[i]) {
4092d522f475Smrg	case 1:		/* DECCKM                       */
4093d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECCKM);
4094d522f475Smrg	    update_appcursor();
4095d522f475Smrg	    break;
4096d522f475Smrg	case 2:		/* DECANM - ANSI/VT52 mode      */
4097d522f475Smrg	    if (IsSM()) {	/* ANSI (VT100) */
4098d522f475Smrg		/*
4099d522f475Smrg		 * Setting DECANM should have no effect, since this function
4100d522f475Smrg		 * cannot be reached from vt52 mode.
4101d522f475Smrg		 */
4102a1f3da82Smrg		/* EMPTY */ ;
4103d522f475Smrg	    }
4104d522f475Smrg#if OPT_VT52_MODE
4105d522f475Smrg	    else if (screen->terminal_id >= 100) {	/* VT52 */
4106d522f475Smrg		TRACE(("DECANM terminal_id %d, vtXX_level %d\n",
4107d522f475Smrg		       screen->terminal_id,
4108d522f475Smrg		       screen->vtXX_level));
4109d522f475Smrg		screen->vt52_save_level = screen->vtXX_level;
4110d522f475Smrg		screen->vtXX_level = 0;
4111d522f475Smrg		screen->vt52_save_curgl = screen->curgl;
4112d522f475Smrg		screen->vt52_save_curgr = screen->curgr;
4113d522f475Smrg		screen->vt52_save_curss = screen->curss;
4114d522f475Smrg		memmove(screen->vt52_save_gsets, screen->gsets, sizeof(screen->gsets));
4115d522f475Smrg		resetCharsets(screen);
4116d522f475Smrg		nparam = 0;	/* ignore the remaining params, if any */
4117d522f475Smrg	    }
4118d522f475Smrg#endif
4119d522f475Smrg	    break;
4120d522f475Smrg	case 3:		/* DECCOLM                      */
4121d522f475Smrg	    if (screen->c132) {
4122d522f475Smrg		ClearScreen(xw);
4123d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
4124d522f475Smrg		if ((j = IsSM()? 132 : 80) !=
4125d522f475Smrg		    ((xw->flags & IN132COLUMNS) ? 132 : 80) ||
4126d522f475Smrg		    j != MaxCols(screen))
4127d522f475Smrg		    RequestResize(xw, -1, j, True);
4128d522f475Smrg		(*func) (&xw->flags, IN132COLUMNS);
4129d522f475Smrg	    }
4130d522f475Smrg	    break;
4131d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
4132d522f475Smrg	    if (IsSM()) {
4133d522f475Smrg		screen->jumpscroll = 0;
4134d522f475Smrg		if (screen->scroll_amt)
4135d522f475Smrg		    FlushScroll(xw);
4136d522f475Smrg	    } else
4137d522f475Smrg		screen->jumpscroll = 1;
4138d522f475Smrg	    (*func) (&xw->flags, SMOOTHSCROLL);
4139d522f475Smrg	    update_jumpscroll();
4140d522f475Smrg	    break;
4141d522f475Smrg	case 5:		/* DECSCNM                      */
41422eaa94a1Schristos	    myflags = xw->flags;
4143d522f475Smrg	    (*func) (&xw->flags, REVERSE_VIDEO);
41442eaa94a1Schristos	    if ((xw->flags ^ myflags) & REVERSE_VIDEO)
4145d522f475Smrg		ReverseVideo(xw);
4146d522f475Smrg	    /* update_reversevideo done in RevVid */
4147d522f475Smrg	    break;
4148d522f475Smrg
4149d522f475Smrg	case 6:		/* DECOM                        */
4150d522f475Smrg	    (*func) (&xw->flags, ORIGIN);
4151d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
4152d522f475Smrg	    break;
4153d522f475Smrg
4154d522f475Smrg	case 7:		/* DECAWM                       */
4155d522f475Smrg	    (*func) (&xw->flags, WRAPAROUND);
4156d522f475Smrg	    update_autowrap();
4157d522f475Smrg	    break;
4158d522f475Smrg	case 8:		/* DECARM                       */
4159d522f475Smrg	    /* ignore autorepeat
4160d522f475Smrg	     * XAutoRepeatOn() and XAutoRepeatOff() can do this, but only
4161d522f475Smrg	     * for the whole display - not limited to a given window.
4162d522f475Smrg	     */
4163d522f475Smrg	    break;
4164d522f475Smrg	case SET_X10_MOUSE:	/* MIT bogus sequence           */
4165d522f475Smrg	    MotionOff(screen, xw);
4166d522f475Smrg	    set_mousemode(X10_MOUSE);
4167d522f475Smrg	    break;
4168d522f475Smrg#if OPT_TOOLBAR
4169d522f475Smrg	case 10:		/* rxvt */
4170d522f475Smrg	    ShowToolbar(IsSM());
4171d522f475Smrg	    break;
4172d522f475Smrg#endif
4173d522f475Smrg#if OPT_BLINK_CURS
4174d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
4175d522f475Smrg	    if (screen->cursor_blink_res) {
4176d522f475Smrg		set_bool_mode(screen->cursor_blink_esc);
4177d522f475Smrg		SetCursorBlink(screen, screen->cursor_blink);
4178d522f475Smrg	    }
4179d522f475Smrg	    break;
4180d522f475Smrg#endif
4181d522f475Smrg	case 18:		/* DECPFF: print form feed */
4182d522f475Smrg	    set_bool_mode(screen->printer_formfeed);
4183d522f475Smrg	    break;
4184d522f475Smrg	case 19:		/* DECPEX: print extent */
4185d522f475Smrg	    set_bool_mode(screen->printer_extent);
4186d522f475Smrg	    break;
4187d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
4188d522f475Smrg	    set_bool_mode(screen->cursor_set);
4189d522f475Smrg	    break;
4190d522f475Smrg	case 30:		/* rxvt */
4191d522f475Smrg	    if (screen->fullVwin.sb_info.width != (IsSM()? ON : OFF))
4192d522f475Smrg		ToggleScrollBar(xw);
4193d522f475Smrg	    break;
4194d522f475Smrg#if OPT_SHIFT_FONTS
4195d522f475Smrg	case 35:		/* rxvt */
4196d522f475Smrg	    set_bool_mode(xw->misc.shift_fonts);
4197d522f475Smrg	    break;
4198d522f475Smrg#endif
4199d522f475Smrg	case 38:		/* DECTEK                       */
4200d522f475Smrg#if OPT_TEK4014
4201d522f475Smrg	    if (IsSM() && !(screen->inhibit & I_TEK)) {
420220d2c4d2Smrg		FlushLog(xw);
4203d522f475Smrg		TEK4014_ACTIVE(xw) = True;
4204d522f475Smrg	    }
4205d522f475Smrg#endif
4206d522f475Smrg	    break;
4207d522f475Smrg	case 40:		/* 132 column mode              */
4208d522f475Smrg	    set_bool_mode(screen->c132);
4209d522f475Smrg	    update_allow132();
4210d522f475Smrg	    break;
4211d522f475Smrg	case 41:		/* curses hack                  */
4212d522f475Smrg	    set_bool_mode(screen->curses);
4213d522f475Smrg	    update_cursesemul();
4214d522f475Smrg	    break;
4215d522f475Smrg	case 42:		/* DECNRCM national charset (VT220) */
4216d522f475Smrg	    (*func) (&xw->flags, NATIONAL);
4217d522f475Smrg	    break;
4218d522f475Smrg	case 44:		/* margin bell                  */
4219d522f475Smrg	    set_bool_mode(screen->marginbell);
4220d522f475Smrg	    if (!screen->marginbell)
422120d2c4d2Smrg		screen->bellArmed = -1;
4222d522f475Smrg	    update_marginbell();
4223d522f475Smrg	    break;
4224d522f475Smrg	case 45:		/* reverse wraparound   */
4225d522f475Smrg	    (*func) (&xw->flags, REVERSEWRAP);
4226d522f475Smrg	    update_reversewrap();
4227d522f475Smrg	    break;
4228d522f475Smrg#ifdef ALLOWLOGGING
4229d522f475Smrg	case 46:		/* logging              */
4230d522f475Smrg#ifdef ALLOWLOGFILEONOFF
4231d522f475Smrg	    /*
4232d522f475Smrg	     * if this feature is enabled, logging may be
4233d522f475Smrg	     * enabled and disabled via escape sequences.
4234d522f475Smrg	     */
4235d522f475Smrg	    if (IsSM())
423620d2c4d2Smrg		StartLog(xw);
4237d522f475Smrg	    else
423820d2c4d2Smrg		CloseLog(xw);
4239d522f475Smrg#else
424020d2c4d2Smrg	    Bell(xw, XkbBI_Info, 0);
424120d2c4d2Smrg	    Bell(xw, XkbBI_Info, 0);
4242d522f475Smrg#endif /* ALLOWLOGFILEONOFF */
4243d522f475Smrg	    break;
4244d522f475Smrg#endif
4245d522f475Smrg	case 1049:		/* alternate buffer & cursor */
4246d522f475Smrg	    if (!xw->misc.titeInhibit) {
4247d522f475Smrg		if (IsSM()) {
4248d522f475Smrg		    CursorSave(xw);
4249d522f475Smrg		    ToAlternate(xw);
4250d522f475Smrg		    ClearScreen(xw);
4251d522f475Smrg		} else {
4252d522f475Smrg		    FromAlternate(xw);
4253d522f475Smrg		    CursorRestore(xw);
4254d522f475Smrg		}
4255d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
4256d522f475Smrg		if (IsSM()) {
4257d522f475Smrg		    xtermScroll(xw, screen->max_row);
4258d522f475Smrg		}
4259d522f475Smrg	    }
4260d522f475Smrg	    break;
4261d522f475Smrg	case 1047:
4262d522f475Smrg	    /* FALLTHRU */
4263d522f475Smrg	case 47:		/* alternate buffer */
4264d522f475Smrg	    if (!xw->misc.titeInhibit) {
4265d522f475Smrg		if (IsSM()) {
4266d522f475Smrg		    ToAlternate(xw);
4267d522f475Smrg		} else {
4268956cc18dSsnj		    if (screen->whichBuf
4269d522f475Smrg			&& (param[i] == 1047))
4270d522f475Smrg			ClearScreen(xw);
4271d522f475Smrg		    FromAlternate(xw);
4272d522f475Smrg		}
4273d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
4274d522f475Smrg		if (IsSM()) {
4275d522f475Smrg		    xtermScroll(xw, screen->max_row);
4276d522f475Smrg		}
4277d522f475Smrg	    }
4278d522f475Smrg	    break;
4279d522f475Smrg	case 66:		/* DECNKM */
4280d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECKPAM);
4281d522f475Smrg	    update_appkeypad();
4282d522f475Smrg	    break;
4283d522f475Smrg	case 67:		/* DECBKM */
4284d522f475Smrg	    /* back-arrow mapped to backspace or delete(D) */
4285d522f475Smrg	    (*func) (&xw->keyboard.flags, MODE_DECBKM);
4286d522f475Smrg	    TRACE(("DECSET DECBKM %s\n",
4287d522f475Smrg		   BtoS(xw->keyboard.flags & MODE_DECBKM)));
4288d522f475Smrg	    update_decbkm();
4289d522f475Smrg	    break;
4290d522f475Smrg	case SET_VT200_MOUSE:	/* xterm bogus sequence         */
4291d522f475Smrg	    MotionOff(screen, xw);
4292d522f475Smrg	    set_mousemode(VT200_MOUSE);
4293d522f475Smrg	    break;
4294d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:	/* xterm sequence w/hilite tracking */
4295d522f475Smrg	    MotionOff(screen, xw);
4296d522f475Smrg	    set_mousemode(VT200_HIGHLIGHT_MOUSE);
4297d522f475Smrg	    break;
4298d522f475Smrg	case SET_BTN_EVENT_MOUSE:
4299d522f475Smrg	    MotionOff(screen, xw);
4300d522f475Smrg	    set_mousemode(BTN_EVENT_MOUSE);
4301d522f475Smrg	    break;
4302d522f475Smrg	case SET_ANY_EVENT_MOUSE:
4303d522f475Smrg	    set_mousemode(ANY_EVENT_MOUSE);
4304d522f475Smrg	    if (screen->send_mouse_pos == MOUSE_OFF) {
4305d522f475Smrg		MotionOff(screen, xw);
4306d522f475Smrg	    } else {
4307d522f475Smrg		MotionOn(screen, xw);
4308d522f475Smrg	    }
4309d522f475Smrg	    break;
4310d522f475Smrg#if OPT_FOCUS_EVENT
4311d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
4312d522f475Smrg	    set_bool_mode(screen->send_focus_pos);
4313d522f475Smrg	    break;
4314d522f475Smrg#endif
4315492d43a5Smrg	case SET_EXT_MODE_MOUSE:
4316492d43a5Smrg	    set_bool_mode(screen->ext_mode_mouse);
4317492d43a5Smrg	    break;
4318d522f475Smrg	case 1010:		/* rxvt */
4319d522f475Smrg	    set_bool_mode(screen->scrollttyoutput);
4320d522f475Smrg	    update_scrollttyoutput();
4321d522f475Smrg	    break;
4322d522f475Smrg	case 1011:		/* rxvt */
4323d522f475Smrg	    set_bool_mode(screen->scrollkey);
4324d522f475Smrg	    update_scrollkey();
4325d522f475Smrg	    break;
4326d522f475Smrg	case 1034:
4327492d43a5Smrg	    set_bool_mode(screen->input_eight_bits);
4328d522f475Smrg	    update_alt_esc();
4329d522f475Smrg	    break;
4330d522f475Smrg#if OPT_NUM_LOCK
4331d522f475Smrg	case 1035:
4332d522f475Smrg	    set_bool_mode(xw->misc.real_NumLock);
4333d522f475Smrg	    update_num_lock();
4334d522f475Smrg	    break;
4335d522f475Smrg	case 1036:
4336d522f475Smrg	    set_bool_mode(screen->meta_sends_esc);
4337d522f475Smrg	    update_meta_esc();
4338d522f475Smrg	    break;
4339d522f475Smrg#endif
4340d522f475Smrg	case 1037:
4341d522f475Smrg	    set_bool_mode(screen->delete_is_del);
4342d522f475Smrg	    update_delete_del();
4343d522f475Smrg	    break;
4344d522f475Smrg#if OPT_NUM_LOCK
4345d522f475Smrg	case 1039:
4346d522f475Smrg	    set_bool_mode(screen->alt_sends_esc);
4347d522f475Smrg	    update_alt_esc();
4348d522f475Smrg	    break;
4349d522f475Smrg#endif
4350d522f475Smrg	case 1040:
4351d522f475Smrg	    set_bool_mode(screen->keepSelection);
4352d522f475Smrg	    update_keepSelection();
4353d522f475Smrg	    break;
4354d522f475Smrg	case 1041:
4355d522f475Smrg	    set_bool_mode(screen->selectToClipboard);
4356d522f475Smrg	    update_selectToClipboard();
4357d522f475Smrg	    break;
4358d522f475Smrg	case 1042:
4359d522f475Smrg	    set_bool_mode(screen->bellIsUrgent);
4360d522f475Smrg	    update_bellIsUrgent();
4361d522f475Smrg	    break;
4362d522f475Smrg	case 1043:
4363d522f475Smrg	    set_bool_mode(screen->poponbell);
4364d522f475Smrg	    update_poponbell();
4365d522f475Smrg	    break;
4366d522f475Smrg	case 1048:
4367d522f475Smrg	    if (!xw->misc.titeInhibit) {
4368d522f475Smrg		if (IsSM())
4369d522f475Smrg		    CursorSave(xw);
4370d522f475Smrg		else
4371d522f475Smrg		    CursorRestore(xw);
4372d522f475Smrg	    }
4373d522f475Smrg	    break;
4374d522f475Smrg#if OPT_TCAP_FKEYS
4375d522f475Smrg	case 1050:
4376d522f475Smrg	    set_keyboard_type(xw, keyboardIsTermcap, IsSM());
4377d522f475Smrg	    break;
4378d522f475Smrg#endif
4379d522f475Smrg#if OPT_SUN_FUNC_KEYS
4380d522f475Smrg	case 1051:
4381d522f475Smrg	    set_keyboard_type(xw, keyboardIsSun, IsSM());
4382d522f475Smrg	    break;
4383d522f475Smrg#endif
4384d522f475Smrg#if OPT_HP_FUNC_KEYS
4385d522f475Smrg	case 1052:
4386d522f475Smrg	    set_keyboard_type(xw, keyboardIsHP, IsSM());
4387d522f475Smrg	    break;
4388d522f475Smrg#endif
4389d522f475Smrg#if OPT_SCO_FUNC_KEYS
4390d522f475Smrg	case 1053:
4391d522f475Smrg	    set_keyboard_type(xw, keyboardIsSCO, IsSM());
4392d522f475Smrg	    break;
4393d522f475Smrg#endif
4394d522f475Smrg	case 1060:
4395d522f475Smrg	    set_keyboard_type(xw, keyboardIsLegacy, IsSM());
4396d522f475Smrg	    break;
4397d522f475Smrg#if OPT_SUNPC_KBD
4398d522f475Smrg	case 1061:
4399d522f475Smrg	    set_keyboard_type(xw, keyboardIsVT220, IsSM());
4400d522f475Smrg	    break;
4401d522f475Smrg#endif
4402d522f475Smrg#if OPT_READLINE
4403d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4404d522f475Smrg	    set_mouseflag(click1_moves);
4405d522f475Smrg	    break;
4406d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4407d522f475Smrg	    set_mouseflag(paste_moves);
4408d522f475Smrg	    break;
4409d522f475Smrg	case SET_DBUTTON3_DELETE:
4410d522f475Smrg	    set_mouseflag(dclick3_deletes);
4411d522f475Smrg	    break;
4412d522f475Smrg	case SET_PASTE_IN_BRACKET:
4413d522f475Smrg	    set_mouseflag(paste_brackets);
4414d522f475Smrg	    break;
4415d522f475Smrg	case SET_PASTE_QUOTE:
4416d522f475Smrg	    set_mouseflag(paste_quotes);
4417d522f475Smrg	    break;
4418d522f475Smrg	case SET_PASTE_LITERAL_NL:
4419d522f475Smrg	    set_mouseflag(paste_literal_nl);
4420d522f475Smrg	    break;
4421d522f475Smrg#endif /* OPT_READLINE */
4422d522f475Smrg	}
4423d522f475Smrg    }
4424d522f475Smrg}
4425d522f475Smrg
4426d522f475Smrg/*
4427d522f475Smrg * process xterm private modes save
4428d522f475Smrg */
4429d522f475Smrgstatic void
4430d522f475Smrgsavemodes(XtermWidget xw)
4431d522f475Smrg{
443220d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
4433d522f475Smrg    int i;
4434d522f475Smrg
4435d522f475Smrg    for (i = 0; i < nparam; i++) {
4436d522f475Smrg	TRACE(("savemodes %d\n", param[i]));
4437d522f475Smrg	switch (param[i]) {
4438d522f475Smrg	case 1:		/* DECCKM                       */
4439d522f475Smrg	    DoSM(DP_DECCKM, xw->keyboard.flags & MODE_DECCKM);
4440d522f475Smrg	    break;
4441d522f475Smrg	case 3:		/* DECCOLM                      */
4442d522f475Smrg	    if (screen->c132)
4443d522f475Smrg		DoSM(DP_DECCOLM, xw->flags & IN132COLUMNS);
4444d522f475Smrg	    break;
4445d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
4446d522f475Smrg	    DoSM(DP_DECSCLM, xw->flags & SMOOTHSCROLL);
4447d522f475Smrg	    break;
4448d522f475Smrg	case 5:		/* DECSCNM                      */
4449d522f475Smrg	    DoSM(DP_DECSCNM, xw->flags & REVERSE_VIDEO);
4450d522f475Smrg	    break;
4451d522f475Smrg	case 6:		/* DECOM                        */
4452d522f475Smrg	    DoSM(DP_DECOM, xw->flags & ORIGIN);
4453d522f475Smrg	    break;
4454d522f475Smrg	case 7:		/* DECAWM                       */
4455d522f475Smrg	    DoSM(DP_DECAWM, xw->flags & WRAPAROUND);
4456d522f475Smrg	    break;
4457d522f475Smrg	case 8:		/* DECARM                       */
4458d522f475Smrg	    /* ignore autorepeat */
4459d522f475Smrg	    break;
4460d522f475Smrg	case SET_X10_MOUSE:	/* mouse bogus sequence */
4461d522f475Smrg	    DoSM(DP_X_X10MSE, screen->send_mouse_pos);
4462d522f475Smrg	    break;
4463d522f475Smrg#if OPT_TOOLBAR
4464d522f475Smrg	case 10:		/* rxvt */
4465d522f475Smrg	    DoSM(DP_TOOLBAR, resource.toolBar);
4466d522f475Smrg	    break;
4467d522f475Smrg#endif
4468d522f475Smrg#if OPT_BLINK_CURS
4469d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
4470d522f475Smrg	    if (screen->cursor_blink_res) {
4471d522f475Smrg		DoSM(DP_CRS_BLINK, screen->cursor_blink_esc);
4472d522f475Smrg	    }
4473d522f475Smrg	    break;
4474d522f475Smrg#endif
4475d522f475Smrg	case 18:		/* DECPFF: print form feed */
4476d522f475Smrg	    DoSM(DP_PRN_FORMFEED, screen->printer_formfeed);
4477d522f475Smrg	    break;
4478d522f475Smrg	case 19:		/* DECPEX: print extent */
4479d522f475Smrg	    DoSM(DP_PRN_EXTENT, screen->printer_extent);
4480d522f475Smrg	    break;
4481d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
4482d522f475Smrg	    DoSM(DP_CRS_VISIBLE, screen->cursor_set);
4483d522f475Smrg	    break;
4484d522f475Smrg	case 40:		/* 132 column mode              */
4485d522f475Smrg	    DoSM(DP_X_DECCOLM, screen->c132);
4486d522f475Smrg	    break;
4487d522f475Smrg	case 41:		/* curses hack                  */
4488d522f475Smrg	    DoSM(DP_X_MORE, screen->curses);
4489d522f475Smrg	    break;
4490d522f475Smrg	case 42:		/* DECNRCM national charset (VT220) */
4491d522f475Smrg	    /* do nothing */
4492d522f475Smrg	    break;
4493d522f475Smrg	case 44:		/* margin bell                  */
4494d522f475Smrg	    DoSM(DP_X_MARGIN, screen->marginbell);
4495d522f475Smrg	    break;
4496d522f475Smrg	case 45:		/* reverse wraparound   */
4497d522f475Smrg	    DoSM(DP_X_REVWRAP, xw->flags & REVERSEWRAP);
4498d522f475Smrg	    break;
4499d522f475Smrg#ifdef ALLOWLOGGING
4500d522f475Smrg	case 46:		/* logging              */
4501d522f475Smrg	    DoSM(DP_X_LOGGING, screen->logging);
4502d522f475Smrg	    break;
4503d522f475Smrg#endif
4504d522f475Smrg	case 1047:		/* alternate buffer             */
4505d522f475Smrg	    /* FALLTHRU */
4506d522f475Smrg	case 47:		/* alternate buffer             */
4507956cc18dSsnj	    DoSM(DP_X_ALTSCRN, screen->whichBuf);
4508d522f475Smrg	    break;
4509d522f475Smrg	case SET_VT200_MOUSE:	/* mouse bogus sequence         */
4510d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:
4511d522f475Smrg	case SET_BTN_EVENT_MOUSE:
4512d522f475Smrg	case SET_ANY_EVENT_MOUSE:
4513d522f475Smrg	    DoSM(DP_X_MOUSE, screen->send_mouse_pos);
4514d522f475Smrg	    break;
4515d522f475Smrg#if OPT_FOCUS_EVENT
4516d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
4517d522f475Smrg	    DoSM(DP_X_FOCUS, screen->send_focus_pos);
4518d522f475Smrg	    break;
4519d522f475Smrg#endif
4520492d43a5Smrg	case SET_EXT_MODE_MOUSE:
4521492d43a5Smrg	    DoSM(DP_X_EXT_MOUSE, screen->ext_mode_mouse);
4522492d43a5Smrg	    break;
4523d522f475Smrg	case 1048:
4524d522f475Smrg	    if (!xw->misc.titeInhibit) {
4525d522f475Smrg		CursorSave(xw);
4526d522f475Smrg	    }
4527d522f475Smrg	    break;
4528d522f475Smrg#if OPT_READLINE
4529d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4530d522f475Smrg	    SCREEN_FLAG_save(screen, click1_moves);
4531d522f475Smrg	    break;
4532d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4533d522f475Smrg	    SCREEN_FLAG_save(screen, paste_moves);
4534d522f475Smrg	    break;
4535d522f475Smrg	case SET_DBUTTON3_DELETE:
4536d522f475Smrg	    SCREEN_FLAG_save(screen, dclick3_deletes);
4537d522f475Smrg	    break;
4538d522f475Smrg	case SET_PASTE_IN_BRACKET:
4539d522f475Smrg	    SCREEN_FLAG_save(screen, paste_brackets);
4540d522f475Smrg	    break;
4541d522f475Smrg	case SET_PASTE_QUOTE:
4542d522f475Smrg	    SCREEN_FLAG_save(screen, paste_quotes);
4543d522f475Smrg	    break;
4544d522f475Smrg	case SET_PASTE_LITERAL_NL:
4545d522f475Smrg	    SCREEN_FLAG_save(screen, paste_literal_nl);
4546d522f475Smrg	    break;
4547d522f475Smrg#endif /* OPT_READLINE */
4548d522f475Smrg	}
4549d522f475Smrg    }
4550d522f475Smrg}
4551d522f475Smrg
4552d522f475Smrg/*
4553d522f475Smrg * process xterm private modes restore
4554d522f475Smrg */
4555d522f475Smrgstatic void
4556d522f475Smrgrestoremodes(XtermWidget xw)
4557d522f475Smrg{
455820d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
4559d522f475Smrg    int i, j;
4560d522f475Smrg
4561d522f475Smrg    for (i = 0; i < nparam; i++) {
4562d522f475Smrg	TRACE(("restoremodes %d\n", param[i]));
4563d522f475Smrg	switch (param[i]) {
4564d522f475Smrg	case 1:		/* DECCKM                       */
4565d522f475Smrg	    bitcpy(&xw->keyboard.flags,
4566d522f475Smrg		   screen->save_modes[DP_DECCKM], MODE_DECCKM);
4567d522f475Smrg	    update_appcursor();
4568d522f475Smrg	    break;
4569d522f475Smrg	case 3:		/* DECCOLM                      */
4570d522f475Smrg	    if (screen->c132) {
4571d522f475Smrg		ClearScreen(xw);
4572d522f475Smrg		CursorSet(screen, 0, 0, xw->flags);
4573d522f475Smrg		if ((j = (screen->save_modes[DP_DECCOLM] & IN132COLUMNS)
4574d522f475Smrg		     ? 132 : 80) != ((xw->flags & IN132COLUMNS)
4575d522f475Smrg				     ? 132 : 80) || j != MaxCols(screen))
4576d522f475Smrg		    RequestResize(xw, -1, j, True);
4577d522f475Smrg		bitcpy(&xw->flags,
4578d522f475Smrg		       screen->save_modes[DP_DECCOLM],
4579d522f475Smrg		       IN132COLUMNS);
4580d522f475Smrg	    }
4581d522f475Smrg	    break;
4582d522f475Smrg	case 4:		/* DECSCLM (slow scroll)        */
4583d522f475Smrg	    if (screen->save_modes[DP_DECSCLM] & SMOOTHSCROLL) {
4584d522f475Smrg		screen->jumpscroll = 0;
4585d522f475Smrg		if (screen->scroll_amt)
4586d522f475Smrg		    FlushScroll(xw);
4587d522f475Smrg	    } else
4588d522f475Smrg		screen->jumpscroll = 1;
4589d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECSCLM], SMOOTHSCROLL);
4590d522f475Smrg	    update_jumpscroll();
4591d522f475Smrg	    break;
4592d522f475Smrg	case 5:		/* DECSCNM                      */
4593d522f475Smrg	    if ((screen->save_modes[DP_DECSCNM] ^ xw->flags) & REVERSE_VIDEO) {
4594d522f475Smrg		bitcpy(&xw->flags, screen->save_modes[DP_DECSCNM], REVERSE_VIDEO);
4595d522f475Smrg		ReverseVideo(xw);
4596d522f475Smrg		/* update_reversevideo done in RevVid */
4597d522f475Smrg	    }
4598d522f475Smrg	    break;
4599d522f475Smrg	case 6:		/* DECOM                        */
4600d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECOM], ORIGIN);
4601d522f475Smrg	    CursorSet(screen, 0, 0, xw->flags);
4602d522f475Smrg	    break;
4603d522f475Smrg
4604d522f475Smrg	case 7:		/* DECAWM                       */
4605d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_DECAWM], WRAPAROUND);
4606d522f475Smrg	    update_autowrap();
4607d522f475Smrg	    break;
4608d522f475Smrg	case 8:		/* DECARM                       */
4609d522f475Smrg	    /* ignore autorepeat */
4610d522f475Smrg	    break;
4611d522f475Smrg	case SET_X10_MOUSE:	/* MIT bogus sequence           */
46122eaa94a1Schristos	    DoRM0(DP_X_X10MSE, screen->send_mouse_pos);
4613d522f475Smrg	    break;
4614d522f475Smrg#if OPT_TOOLBAR
4615d522f475Smrg	case 10:		/* rxvt */
4616d522f475Smrg	    DoRM(DP_TOOLBAR, resource.toolBar);
4617d522f475Smrg	    ShowToolbar(resource.toolBar);
4618d522f475Smrg	    break;
4619d522f475Smrg#endif
4620d522f475Smrg#if OPT_BLINK_CURS
4621d522f475Smrg	case 12:		/* att610: Start/stop blinking cursor */
4622d522f475Smrg	    if (screen->cursor_blink_res) {
4623d522f475Smrg		DoRM(DP_CRS_BLINK, screen->cursor_blink_esc);
4624d522f475Smrg		SetCursorBlink(screen, screen->cursor_blink);
4625d522f475Smrg	    }
4626d522f475Smrg	    break;
4627d522f475Smrg#endif
4628d522f475Smrg	case 18:		/* DECPFF: print form feed */
4629d522f475Smrg	    DoRM(DP_PRN_FORMFEED, screen->printer_formfeed);
4630d522f475Smrg	    break;
4631d522f475Smrg	case 19:		/* DECPEX: print extent */
4632d522f475Smrg	    DoRM(DP_PRN_EXTENT, screen->printer_extent);
4633d522f475Smrg	    break;
4634d522f475Smrg	case 25:		/* DECTCEM: Show/hide cursor (VT200) */
4635d522f475Smrg	    DoRM(DP_CRS_VISIBLE, screen->cursor_set);
4636d522f475Smrg	    break;
4637d522f475Smrg	case 40:		/* 132 column mode              */
4638d522f475Smrg	    DoRM(DP_X_DECCOLM, screen->c132);
4639d522f475Smrg	    update_allow132();
4640d522f475Smrg	    break;
4641d522f475Smrg	case 41:		/* curses hack                  */
4642d522f475Smrg	    DoRM(DP_X_MORE, screen->curses);
4643d522f475Smrg	    update_cursesemul();
4644d522f475Smrg	    break;
4645d522f475Smrg	case 44:		/* margin bell                  */
4646d522f475Smrg	    if ((DoRM(DP_X_MARGIN, screen->marginbell)) == 0)
464720d2c4d2Smrg		screen->bellArmed = -1;
4648d522f475Smrg	    update_marginbell();
4649d522f475Smrg	    break;
4650d522f475Smrg	case 45:		/* reverse wraparound   */
4651d522f475Smrg	    bitcpy(&xw->flags, screen->save_modes[DP_X_REVWRAP], REVERSEWRAP);
4652d522f475Smrg	    update_reversewrap();
4653d522f475Smrg	    break;
4654d522f475Smrg#ifdef ALLOWLOGGING
4655d522f475Smrg	case 46:		/* logging              */
4656d522f475Smrg#ifdef ALLOWLOGFILEONOFF
4657d522f475Smrg	    if (screen->save_modes[DP_X_LOGGING])
465820d2c4d2Smrg		StartLog(xw);
4659d522f475Smrg	    else
466020d2c4d2Smrg		CloseLog(xw);
4661d522f475Smrg#endif /* ALLOWLOGFILEONOFF */
4662d522f475Smrg	    /* update_logging done by StartLog and CloseLog */
4663d522f475Smrg	    break;
4664d522f475Smrg#endif
4665d522f475Smrg	case 1047:		/* alternate buffer */
4666d522f475Smrg	    /* FALLTHRU */
4667d522f475Smrg	case 47:		/* alternate buffer */
4668d522f475Smrg	    if (!xw->misc.titeInhibit) {
4669d522f475Smrg		if (screen->save_modes[DP_X_ALTSCRN])
4670d522f475Smrg		    ToAlternate(xw);
4671d522f475Smrg		else
4672d522f475Smrg		    FromAlternate(xw);
4673d522f475Smrg		/* update_altscreen done by ToAlt and FromAlt */
4674d522f475Smrg	    } else if (xw->misc.tiXtraScroll) {
4675d522f475Smrg		if (screen->save_modes[DP_X_ALTSCRN]) {
4676d522f475Smrg		    xtermScroll(xw, screen->max_row);
4677d522f475Smrg		}
4678d522f475Smrg	    }
4679d522f475Smrg	    break;
4680d522f475Smrg	case SET_VT200_MOUSE:	/* mouse bogus sequence         */
4681d522f475Smrg	case SET_VT200_HIGHLIGHT_MOUSE:
4682d522f475Smrg	case SET_BTN_EVENT_MOUSE:
4683d522f475Smrg	case SET_ANY_EVENT_MOUSE:
46842eaa94a1Schristos	    DoRM0(DP_X_MOUSE, screen->send_mouse_pos);
4685d522f475Smrg	    break;
4686d522f475Smrg#if OPT_FOCUS_EVENT
4687d522f475Smrg	case SET_FOCUS_EVENT_MOUSE:
4688d522f475Smrg	    DoRM(DP_X_FOCUS, screen->send_focus_pos);
4689d522f475Smrg	    break;
4690d522f475Smrg#endif
4691492d43a5Smrg	case SET_EXT_MODE_MOUSE:
4692492d43a5Smrg	    DoRM(DP_X_EXT_MOUSE, screen->ext_mode_mouse);
4693492d43a5Smrg	    break;
4694d522f475Smrg	case 1048:
4695d522f475Smrg	    if (!xw->misc.titeInhibit) {
4696d522f475Smrg		CursorRestore(xw);
4697d522f475Smrg	    }
4698d522f475Smrg	    break;
4699d522f475Smrg#if OPT_READLINE
4700d522f475Smrg	case SET_BUTTON1_MOVE_POINT:
4701d522f475Smrg	    SCREEN_FLAG_restore(screen, click1_moves);
4702d522f475Smrg	    break;
4703d522f475Smrg	case SET_BUTTON2_MOVE_POINT:
4704d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_moves);
4705d522f475Smrg	    break;
4706d522f475Smrg	case SET_DBUTTON3_DELETE:
4707d522f475Smrg	    SCREEN_FLAG_restore(screen, dclick3_deletes);
4708d522f475Smrg	    break;
4709d522f475Smrg	case SET_PASTE_IN_BRACKET:
4710d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_brackets);
4711d522f475Smrg	    break;
4712d522f475Smrg	case SET_PASTE_QUOTE:
4713d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_quotes);
4714d522f475Smrg	    break;
4715d522f475Smrg	case SET_PASTE_LITERAL_NL:
4716d522f475Smrg	    SCREEN_FLAG_restore(screen, paste_literal_nl);
4717d522f475Smrg	    break;
4718d522f475Smrg#endif /* OPT_READLINE */
4719d522f475Smrg	}
4720d522f475Smrg    }
4721d522f475Smrg}
4722d522f475Smrg
472320d2c4d2Smrg/*
472420d2c4d2Smrg * Convert an XTextProperty to a string.
472520d2c4d2Smrg *
472620d2c4d2Smrg * This frees the data owned by the XTextProperty, and returns in its place the
472720d2c4d2Smrg * string, which must be freed by the caller.
472820d2c4d2Smrg */
472920d2c4d2Smrgstatic char *
473020d2c4d2Smrgproperty_to_string(XtermWidget xw, XTextProperty * text)
473120d2c4d2Smrg{
473220d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
473320d2c4d2Smrg    Display *dpy = screen->display;
473420d2c4d2Smrg    char *result = 0;
473520d2c4d2Smrg    char **list;
473620d2c4d2Smrg    int length = 0;
473720d2c4d2Smrg    int rc;
473820d2c4d2Smrg
473920d2c4d2Smrg    TRACE(("property_to_string value %p, encoding %s, format %d, nitems %ld\n",
474020d2c4d2Smrg	   text->value,
474120d2c4d2Smrg	   XGetAtomName(dpy, text->encoding),
474220d2c4d2Smrg	   text->format,
474320d2c4d2Smrg	   text->nitems));
474420d2c4d2Smrg
474520d2c4d2Smrg#if OPT_WIDE_CHARS
474620d2c4d2Smrg    /*
474720d2c4d2Smrg     * We will use the XmbTextPropertyToTextList call to extract UTF-8 data.
474820d2c4d2Smrg     * The xtermUtf8ToTextList() call is used to convert UTF-8 explicitly to
474920d2c4d2Smrg     * ISO-8859-1.
475020d2c4d2Smrg     */
475120d2c4d2Smrg    if ((text->format != 8)
475220d2c4d2Smrg	|| IsTitleMode(xw, tmGetUtf8)
475320d2c4d2Smrg	|| (rc = xtermUtf8ToTextList(xw, text, &list, &length)) < 0)
475420d2c4d2Smrg#endif
475520d2c4d2Smrg	if ((rc = XmbTextPropertyToTextList(dpy, text, &list, &length)) < 0)
475620d2c4d2Smrg	    rc = XTextPropertyToStringList(text, &list, &length);
475720d2c4d2Smrg
475820d2c4d2Smrg    if (rc >= 0) {
475920d2c4d2Smrg	int n, c, pass;
476020d2c4d2Smrg	size_t need = 0;
476120d2c4d2Smrg
476220d2c4d2Smrg	for (pass = 0; pass < 2; ++pass) {
476320d2c4d2Smrg	    for (n = 0, need = 0; n < length; n++) {
476420d2c4d2Smrg		char *s = list[n];
476520d2c4d2Smrg		while ((c = *s++) != '\0') {
476620d2c4d2Smrg		    if (pass)
476720d2c4d2Smrg			result[need] = (char) c;
476820d2c4d2Smrg		    ++need;
476920d2c4d2Smrg		}
477020d2c4d2Smrg	    }
477120d2c4d2Smrg	    if (pass)
477220d2c4d2Smrg		result[need] = '\0';
477320d2c4d2Smrg	    else
477420d2c4d2Smrg		result = malloc(need + 1);
477520d2c4d2Smrg	    if (result == 0)
477620d2c4d2Smrg		break;
477720d2c4d2Smrg	}
477820d2c4d2Smrg	XFreeStringList(list);
477920d2c4d2Smrg    }
478020d2c4d2Smrg    if (text->value != 0)
478120d2c4d2Smrg	XFree(text->value);
478220d2c4d2Smrg
478320d2c4d2Smrg    return result;
478420d2c4d2Smrg}
478520d2c4d2Smrg
478620d2c4d2Smrgstatic char *
478720d2c4d2Smrgget_icon_label(XtermWidget xw)
478820d2c4d2Smrg{
478920d2c4d2Smrg    XTextProperty text;
479020d2c4d2Smrg    char *result = 0;
479120d2c4d2Smrg
4792a1f3da82Smrg    if (XGetWMIconName(TScreenOf(xw)->display, VShellWindow(xw), &text)) {
479320d2c4d2Smrg	result = property_to_string(xw, &text);
479420d2c4d2Smrg    }
479520d2c4d2Smrg    return result;
479620d2c4d2Smrg}
479720d2c4d2Smrg
479820d2c4d2Smrgstatic char *
479920d2c4d2Smrgget_window_label(XtermWidget xw)
480020d2c4d2Smrg{
480120d2c4d2Smrg    XTextProperty text;
480220d2c4d2Smrg    char *result = 0;
480320d2c4d2Smrg
4804a1f3da82Smrg    if (XGetWMName(TScreenOf(xw)->display, VShellWindow(xw), &text)) {
480520d2c4d2Smrg	result = property_to_string(xw, &text);
480620d2c4d2Smrg    }
480720d2c4d2Smrg    return result;
480820d2c4d2Smrg}
480920d2c4d2Smrg
4810d522f475Smrg/*
4811d522f475Smrg * Report window label (icon or title) in dtterm protocol
4812d522f475Smrg * ESC ] code label ESC backslash
4813d522f475Smrg */
4814d522f475Smrgstatic void
4815d522f475Smrgreport_win_label(XtermWidget xw,
4816d522f475Smrg		 int code,
481720d2c4d2Smrg		 char *text)
4818d522f475Smrg{
4819d522f475Smrg    reply.a_type = ANSI_ESC;
4820d522f475Smrg    unparseputc(xw, ANSI_ESC);
4821d522f475Smrg    unparseputc(xw, ']');
4822d522f475Smrg    unparseputc(xw, code);
4823d522f475Smrg
482420d2c4d2Smrg    if (text != 0) {
482520d2c4d2Smrg	int copy = IsTitleMode(xw, tmGetBase16);
482620d2c4d2Smrg	if (copy) {
482720d2c4d2Smrg	    TRACE(("Encoding hex:%s\n", text));
482820d2c4d2Smrg	    text = x_encode_hex(text);
4829d522f475Smrg	}
483020d2c4d2Smrg	unparseputs(xw, text);
483120d2c4d2Smrg	if (copy)
483220d2c4d2Smrg	    free(text);
4833d522f475Smrg    }
4834d522f475Smrg
4835d522f475Smrg    unparseputc(xw, ANSI_ESC);
4836d522f475Smrg    unparseputc(xw, '\\');	/* should be ST */
4837d522f475Smrg    unparse_end(xw);
4838d522f475Smrg}
4839d522f475Smrg
4840d522f475Smrg/*
4841d522f475Smrg * Window operations (from CDE dtterm description, as well as extensions).
4842d522f475Smrg * See also "allowWindowOps" resource.
4843d522f475Smrg */
4844d522f475Smrgstatic void
4845d522f475Smrgwindow_ops(XtermWidget xw)
4846d522f475Smrg{
484720d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
4848d522f475Smrg    XWindowChanges values;
4849d522f475Smrg    XWindowAttributes win_attrs;
4850d522f475Smrg    unsigned value_mask;
4851d522f475Smrg#if OPT_MAXIMIZE
4852d522f475Smrg    unsigned root_width;
4853d522f475Smrg    unsigned root_height;
4854d522f475Smrg#endif
4855d522f475Smrg
4856d522f475Smrg    TRACE(("window_ops %d\n", param[0]));
4857d522f475Smrg    switch (param[0]) {
485820d2c4d2Smrg    case ewRestoreWin:		/* Restore (de-iconify) window */
485920d2c4d2Smrg	if (AllowWindowOps(xw, ewRestoreWin)) {
486020d2c4d2Smrg	    TRACE(("...de-iconify window\n"));
486120d2c4d2Smrg	    XMapWindow(screen->display,
4862a1f3da82Smrg		       VShellWindow(xw));
486320d2c4d2Smrg	}
4864d522f475Smrg	break;
4865d522f475Smrg
486620d2c4d2Smrg    case ewMinimizeWin:	/* Minimize (iconify) window */
486720d2c4d2Smrg	if (AllowWindowOps(xw, ewMinimizeWin)) {
486820d2c4d2Smrg	    TRACE(("...iconify window\n"));
486920d2c4d2Smrg	    XIconifyWindow(screen->display,
4870a1f3da82Smrg			   VShellWindow(xw),
487120d2c4d2Smrg			   DefaultScreen(screen->display));
487220d2c4d2Smrg	}
4873d522f475Smrg	break;
4874d522f475Smrg
487520d2c4d2Smrg    case ewSetWinPosition:	/* Move the window to the given position */
487620d2c4d2Smrg	if (AllowWindowOps(xw, ewSetWinPosition)) {
487720d2c4d2Smrg	    values.x = zero_if_default(1);
487820d2c4d2Smrg	    values.y = zero_if_default(2);
487920d2c4d2Smrg	    TRACE(("...move window to %d,%d\n", values.x, values.y));
488020d2c4d2Smrg	    value_mask = (CWX | CWY);
488120d2c4d2Smrg	    XReconfigureWMWindow(screen->display,
4882a1f3da82Smrg				 VShellWindow(xw),
488320d2c4d2Smrg				 DefaultScreen(screen->display),
488420d2c4d2Smrg				 value_mask,
488520d2c4d2Smrg				 &values);
488620d2c4d2Smrg	}
4887d522f475Smrg	break;
4888d522f475Smrg
488920d2c4d2Smrg    case ewSetWinSizePixels:	/* Resize the window to given size in pixels */
489020d2c4d2Smrg	if (AllowWindowOps(xw, ewSetWinSizePixels)) {
489120d2c4d2Smrg	    RequestResize(xw, zero_if_default(1), zero_if_default(2), False);
489220d2c4d2Smrg	}
4893d522f475Smrg	break;
4894d522f475Smrg
489520d2c4d2Smrg    case ewRaiseWin:		/* Raise the window to the front of the stack */
489620d2c4d2Smrg	if (AllowWindowOps(xw, ewRaiseWin)) {
489720d2c4d2Smrg	    TRACE(("...raise window\n"));
4898a1f3da82Smrg	    XRaiseWindow(screen->display, VShellWindow(xw));
489920d2c4d2Smrg	}
4900d522f475Smrg	break;
4901d522f475Smrg
490220d2c4d2Smrg    case ewLowerWin:		/* Lower the window to the bottom of the stack */
490320d2c4d2Smrg	if (AllowWindowOps(xw, ewLowerWin)) {
490420d2c4d2Smrg	    TRACE(("...lower window\n"));
4905a1f3da82Smrg	    XLowerWindow(screen->display, VShellWindow(xw));
490620d2c4d2Smrg	}
4907d522f475Smrg	break;
4908d522f475Smrg
490920d2c4d2Smrg    case ewRefreshWin:		/* Refresh the window */
491020d2c4d2Smrg	if (AllowWindowOps(xw, ewRefreshWin)) {
491120d2c4d2Smrg	    TRACE(("...redraw window\n"));
491220d2c4d2Smrg	    Redraw();
491320d2c4d2Smrg	}
4914d522f475Smrg	break;
4915d522f475Smrg
491620d2c4d2Smrg    case ewSetWinSizeChars:	/* Resize the text-area, in characters */
491720d2c4d2Smrg	if (AllowWindowOps(xw, ewSetWinSizeChars)) {
491820d2c4d2Smrg	    RequestResize(xw, zero_if_default(1), zero_if_default(2), True);
491920d2c4d2Smrg	}
4920d522f475Smrg	break;
4921d522f475Smrg
4922d522f475Smrg#if OPT_MAXIMIZE
492320d2c4d2Smrg    case ewMaximizeWin:	/* Maximize or restore */
492420d2c4d2Smrg	if (AllowWindowOps(xw, ewMaximizeWin)) {
492520d2c4d2Smrg	    RequestMaximize(xw, zero_if_default(1));
492620d2c4d2Smrg	}
4927d522f475Smrg	break;
4928a1f3da82Smrg    case ewFullscreenWin:	/* Fullscreen or restore */
4929a1f3da82Smrg	if (AllowWindowOps(xw, ewFullscreenWin)) {
4930a1f3da82Smrg	    FullScreen(xw, zero_if_default(1));
4931a1f3da82Smrg	}
4932a1f3da82Smrg	break;
4933d522f475Smrg#endif
4934d522f475Smrg
493520d2c4d2Smrg    case ewGetWinState:	/* Report the window's state */
493620d2c4d2Smrg	if (AllowWindowOps(xw, ewGetWinState)) {
493720d2c4d2Smrg	    TRACE(("...get window attributes\n"));
493820d2c4d2Smrg	    XGetWindowAttributes(screen->display,
493920d2c4d2Smrg				 VWindow(screen),
494020d2c4d2Smrg				 &win_attrs);
494120d2c4d2Smrg	    reply.a_type = ANSI_CSI;
494220d2c4d2Smrg	    reply.a_pintro = 0;
494320d2c4d2Smrg	    reply.a_nparam = 1;
494420d2c4d2Smrg	    reply.a_param[0] = (ParmType) ((win_attrs.map_state == IsViewable)
494520d2c4d2Smrg					   ? 1
494620d2c4d2Smrg					   : 2);
494720d2c4d2Smrg	    reply.a_inters = 0;
494820d2c4d2Smrg	    reply.a_final = 't';
494920d2c4d2Smrg	    unparseseq(xw, &reply);
495020d2c4d2Smrg	}
4951d522f475Smrg	break;
4952d522f475Smrg
495320d2c4d2Smrg    case ewGetWinPosition:	/* Report the window's position */
495420d2c4d2Smrg	if (AllowWindowOps(xw, ewGetWinPosition)) {
495520d2c4d2Smrg	    TRACE(("...get window position\n"));
495620d2c4d2Smrg	    XGetWindowAttributes(screen->display,
495720d2c4d2Smrg				 WMFrameWindow(xw),
495820d2c4d2Smrg				 &win_attrs);
495920d2c4d2Smrg	    reply.a_type = ANSI_CSI;
496020d2c4d2Smrg	    reply.a_pintro = 0;
496120d2c4d2Smrg	    reply.a_nparam = 3;
496220d2c4d2Smrg	    reply.a_param[0] = 3;
496320d2c4d2Smrg	    reply.a_param[1] = (ParmType) win_attrs.x;
496420d2c4d2Smrg	    reply.a_param[2] = (ParmType) win_attrs.y;
496520d2c4d2Smrg	    reply.a_inters = 0;
496620d2c4d2Smrg	    reply.a_final = 't';
496720d2c4d2Smrg	    unparseseq(xw, &reply);
496820d2c4d2Smrg	}
4969d522f475Smrg	break;
4970d522f475Smrg
497120d2c4d2Smrg    case ewGetWinSizePixels:	/* Report the window's size in pixels */
497220d2c4d2Smrg	if (AllowWindowOps(xw, ewGetWinSizePixels)) {
497320d2c4d2Smrg	    TRACE(("...get window size in pixels\n"));
497420d2c4d2Smrg	    XGetWindowAttributes(screen->display,
497520d2c4d2Smrg				 VWindow(screen),
497620d2c4d2Smrg				 &win_attrs);
497720d2c4d2Smrg	    reply.a_type = ANSI_CSI;
497820d2c4d2Smrg	    reply.a_pintro = 0;
497920d2c4d2Smrg	    reply.a_nparam = 3;
498020d2c4d2Smrg	    reply.a_param[0] = 4;
498120d2c4d2Smrg	    /*FIXME: find if dtterm uses
498220d2c4d2Smrg	     *    win_attrs.height or Height
498320d2c4d2Smrg	     *      win_attrs.width  or Width
498420d2c4d2Smrg	     */
498520d2c4d2Smrg	    reply.a_param[1] = (ParmType) Height(screen);
498620d2c4d2Smrg	    reply.a_param[2] = (ParmType) Width(screen);
498720d2c4d2Smrg	    reply.a_inters = 0;
498820d2c4d2Smrg	    reply.a_final = 't';
498920d2c4d2Smrg	    unparseseq(xw, &reply);
499020d2c4d2Smrg	}
4991d522f475Smrg	break;
4992d522f475Smrg
499320d2c4d2Smrg    case ewGetWinSizeChars:	/* Report the text's size in characters */
499420d2c4d2Smrg	if (AllowWindowOps(xw, ewGetWinSizeChars)) {
499520d2c4d2Smrg	    TRACE(("...get window size in characters\n"));
499620d2c4d2Smrg	    reply.a_type = ANSI_CSI;
499720d2c4d2Smrg	    reply.a_pintro = 0;
499820d2c4d2Smrg	    reply.a_nparam = 3;
499920d2c4d2Smrg	    reply.a_param[0] = 8;
500020d2c4d2Smrg	    reply.a_param[1] = (ParmType) MaxRows(screen);
500120d2c4d2Smrg	    reply.a_param[2] = (ParmType) MaxCols(screen);
500220d2c4d2Smrg	    reply.a_inters = 0;
500320d2c4d2Smrg	    reply.a_final = 't';
500420d2c4d2Smrg	    unparseseq(xw, &reply);
500520d2c4d2Smrg	}
5006d522f475Smrg	break;
5007d522f475Smrg
5008d522f475Smrg#if OPT_MAXIMIZE
500920d2c4d2Smrg    case ewGetScreenSizeChars:	/* Report the screen's size, in characters */
501020d2c4d2Smrg	if (AllowWindowOps(xw, ewGetScreenSizeChars)) {
501120d2c4d2Smrg	    TRACE(("...get screen size in characters\n"));
501220d2c4d2Smrg	    if (!QueryMaximize(xw, &root_height, &root_width)) {
501320d2c4d2Smrg		root_height = 0;
501420d2c4d2Smrg		root_width = 0;
501520d2c4d2Smrg	    }
501620d2c4d2Smrg	    reply.a_type = ANSI_CSI;
501720d2c4d2Smrg	    reply.a_pintro = 0;
501820d2c4d2Smrg	    reply.a_nparam = 3;
501920d2c4d2Smrg	    reply.a_param[0] = 9;
502020d2c4d2Smrg	    reply.a_param[1] = (ParmType) (root_height
502120d2c4d2Smrg					   / (unsigned) FontHeight(screen));
502220d2c4d2Smrg	    reply.a_param[2] = (ParmType) (root_width
502320d2c4d2Smrg					   / (unsigned) FontWidth(screen));
502420d2c4d2Smrg	    reply.a_inters = 0;
502520d2c4d2Smrg	    reply.a_final = 't';
502620d2c4d2Smrg	    unparseseq(xw, &reply);
5027d522f475Smrg	}
5028d522f475Smrg	break;
5029d522f475Smrg#endif
5030d522f475Smrg
503120d2c4d2Smrg    case ewGetIconTitle:	/* Report the icon's label */
503220d2c4d2Smrg	if (AllowWindowOps(xw, ewGetIconTitle)) {
503320d2c4d2Smrg	    TRACE(("...get icon's label\n"));
503420d2c4d2Smrg	    report_win_label(xw, 'L', get_icon_label(xw));
503520d2c4d2Smrg	}
5036d522f475Smrg	break;
5037d522f475Smrg
503820d2c4d2Smrg    case ewGetWinTitle:	/* Report the window's title */
503920d2c4d2Smrg	if (AllowWindowOps(xw, ewGetWinTitle)) {
504020d2c4d2Smrg	    TRACE(("...get window's label\n"));
504120d2c4d2Smrg	    report_win_label(xw, 'l', get_window_label(xw));
504220d2c4d2Smrg	}
504320d2c4d2Smrg	break;
504420d2c4d2Smrg
504520d2c4d2Smrg    case ewPushTitle:		/* save the window's title(s) on stack */
504620d2c4d2Smrg	if (AllowWindowOps(xw, ewPushTitle)) {
504720d2c4d2Smrg	    SaveTitle *last = screen->save_title;
504820d2c4d2Smrg	    SaveTitle *item = TypeCalloc(SaveTitle);
504920d2c4d2Smrg
505020d2c4d2Smrg	    TRACE(("...push title onto stack\n"));
505120d2c4d2Smrg	    if (item != 0) {
505220d2c4d2Smrg		switch (zero_if_default(1)) {
505320d2c4d2Smrg		case 0:
505420d2c4d2Smrg		    item->iconName = get_icon_label(xw);
505520d2c4d2Smrg		    item->windowName = get_window_label(xw);
505620d2c4d2Smrg		    break;
505720d2c4d2Smrg		case 1:
505820d2c4d2Smrg		    item->iconName = get_icon_label(xw);
505920d2c4d2Smrg		    break;
506020d2c4d2Smrg		case 2:
506120d2c4d2Smrg		    item->windowName = get_window_label(xw);
506220d2c4d2Smrg		    break;
506320d2c4d2Smrg		}
506420d2c4d2Smrg		item->next = last;
506520d2c4d2Smrg		if (item->iconName == 0) {
506620d2c4d2Smrg		    item->iconName = ((last == 0)
506720d2c4d2Smrg				      ? get_icon_label(xw)
506820d2c4d2Smrg				      : x_strdup(last->iconName));
506920d2c4d2Smrg		}
507020d2c4d2Smrg		if (item->windowName == 0) {
507120d2c4d2Smrg		    item->windowName = ((last == 0)
507220d2c4d2Smrg					? get_window_label(xw)
507320d2c4d2Smrg					: x_strdup(last->windowName));
507420d2c4d2Smrg		}
507520d2c4d2Smrg		screen->save_title = item;
507620d2c4d2Smrg	    }
507720d2c4d2Smrg	}
507820d2c4d2Smrg	break;
507920d2c4d2Smrg
508020d2c4d2Smrg    case ewPopTitle:		/* restore the window's title(s) from stack */
508120d2c4d2Smrg	if (AllowWindowOps(xw, ewPopTitle)) {
508220d2c4d2Smrg	    SaveTitle *item = screen->save_title;
508320d2c4d2Smrg
508420d2c4d2Smrg	    TRACE(("...pop title off stack\n"));
508520d2c4d2Smrg	    if (item != 0) {
508620d2c4d2Smrg		switch (zero_if_default(1)) {
508720d2c4d2Smrg		case 0:
508820d2c4d2Smrg		    ChangeIconName(xw, item->iconName);
508920d2c4d2Smrg		    ChangeTitle(xw, item->windowName);
509020d2c4d2Smrg		    break;
509120d2c4d2Smrg		case 1:
509220d2c4d2Smrg		    ChangeIconName(xw, item->iconName);
509320d2c4d2Smrg		    break;
509420d2c4d2Smrg		case 2:
509520d2c4d2Smrg		    ChangeTitle(xw, item->windowName);
509620d2c4d2Smrg		    break;
509720d2c4d2Smrg		}
509820d2c4d2Smrg		screen->save_title = item->next;
509920d2c4d2Smrg		free(item->iconName);
510020d2c4d2Smrg		free(item->windowName);
510120d2c4d2Smrg		free(item);
510220d2c4d2Smrg	    }
510320d2c4d2Smrg	}
5104d522f475Smrg	break;
5105d522f475Smrg
5106d522f475Smrg    default:			/* DECSLPP (24, 25, 36, 48, 72, 144) */
510720d2c4d2Smrg	if (AllowWindowOps(xw, ewSetWinLines)) {
510820d2c4d2Smrg	    if (param[0] >= 24)
510920d2c4d2Smrg		RequestResize(xw, param[0], -1, True);
511020d2c4d2Smrg	}
5111d522f475Smrg	break;
5112d522f475Smrg    }
5113d522f475Smrg}
5114d522f475Smrg
5115d522f475Smrg/*
5116d522f475Smrg * set a bit in a word given a pointer to the word and a mask.
5117d522f475Smrg */
5118d522f475Smrgstatic void
5119d522f475Smrgbitset(unsigned *p, unsigned mask)
5120d522f475Smrg{
5121d522f475Smrg    *p |= mask;
5122d522f475Smrg}
5123d522f475Smrg
5124d522f475Smrg/*
5125d522f475Smrg * clear a bit in a word given a pointer to the word and a mask.
5126d522f475Smrg */
5127d522f475Smrgstatic void
5128d522f475Smrgbitclr(unsigned *p, unsigned mask)
5129d522f475Smrg{
5130d522f475Smrg    *p &= ~mask;
5131d522f475Smrg}
5132d522f475Smrg
5133d522f475Smrg/*
5134d522f475Smrg * Copy bits from one word to another, given a mask
5135d522f475Smrg */
5136d522f475Smrgstatic void
5137d522f475Smrgbitcpy(unsigned *p, unsigned q, unsigned mask)
5138d522f475Smrg{
5139d522f475Smrg    bitclr(p, mask);
5140d522f475Smrg    bitset(p, q & mask);
5141d522f475Smrg}
5142d522f475Smrg
5143d522f475Smrgvoid
5144d522f475Smrgunparseputc1(XtermWidget xw, int c)
5145d522f475Smrg{
5146d522f475Smrg    if (c >= 0x80 && c <= 0x9F) {
514720d2c4d2Smrg	if (!TScreenOf(xw)->control_eight_bits) {
5148d522f475Smrg	    unparseputc(xw, A2E(ANSI_ESC));
5149d522f475Smrg	    c = A2E(c - 0x40);
5150d522f475Smrg	}
5151d522f475Smrg    }
5152d522f475Smrg    unparseputc(xw, c);
5153d522f475Smrg}
5154d522f475Smrg
5155d522f475Smrgvoid
5156d522f475Smrgunparseseq(XtermWidget xw, ANSI * ap)
5157d522f475Smrg{
5158d522f475Smrg    int c;
5159d522f475Smrg    int i;
5160d522f475Smrg    int inters;
5161d522f475Smrg
5162d522f475Smrg    unparseputc1(xw, c = ap->a_type);
5163d522f475Smrg    if (c == ANSI_ESC
5164d522f475Smrg	|| c == ANSI_DCS
5165d522f475Smrg	|| c == ANSI_CSI
5166d522f475Smrg	|| c == ANSI_OSC
5167d522f475Smrg	|| c == ANSI_PM
5168d522f475Smrg	|| c == ANSI_APC
5169d522f475Smrg	|| c == ANSI_SS3) {
5170d522f475Smrg	if (ap->a_pintro != 0)
5171d522f475Smrg	    unparseputc(xw, ap->a_pintro);
5172d522f475Smrg	for (i = 0; i < ap->a_nparam; ++i) {
5173d522f475Smrg	    if (i != 0)
5174d522f475Smrg		unparseputc(xw, ';');
5175d522f475Smrg	    unparseputn(xw, (unsigned int) ap->a_param[i]);
5176d522f475Smrg	}
5177d522f475Smrg	if ((inters = ap->a_inters) != 0) {
5178d522f475Smrg	    for (i = 3; i >= 0; --i) {
5179d522f475Smrg		c = CharOf(inters >> (8 * i));
5180d522f475Smrg		if (c != 0)
5181d522f475Smrg		    unparseputc(xw, c);
5182d522f475Smrg	    }
5183d522f475Smrg	}
5184d522f475Smrg	unparseputc(xw, (char) ap->a_final);
5185d522f475Smrg    }
5186d522f475Smrg    unparse_end(xw);
5187d522f475Smrg}
5188d522f475Smrg
5189d522f475Smrgvoid
5190d522f475Smrgunparseputn(XtermWidget xw, unsigned int n)
5191d522f475Smrg{
5192d522f475Smrg    unsigned int q;
5193d522f475Smrg
5194d522f475Smrg    q = n / 10;
5195d522f475Smrg    if (q != 0)
5196d522f475Smrg	unparseputn(xw, q);
5197d522f475Smrg    unparseputc(xw, (char) ('0' + (n % 10)));
5198d522f475Smrg}
5199d522f475Smrg
5200d522f475Smrgvoid
520120d2c4d2Smrgunparseputs(XtermWidget xw, const char *s)
5202d522f475Smrg{
520320d2c4d2Smrg    if (s != 0) {
520420d2c4d2Smrg	while (*s)
520520d2c4d2Smrg	    unparseputc(xw, *s++);
520620d2c4d2Smrg    }
5207d522f475Smrg}
5208d522f475Smrg
5209d522f475Smrgvoid
5210d522f475Smrgunparseputc(XtermWidget xw, int c)
5211d522f475Smrg{
5212492d43a5Smrg    TScreen *screen = TScreenOf(xw);
5213492d43a5Smrg    IChar *buf = screen->unparse_bfr;
5214d522f475Smrg    unsigned len;
5215d522f475Smrg
5216492d43a5Smrg    if ((screen->unparse_len + 2) >= sizeof(screen->unparse_bfr))
5217d522f475Smrg	unparse_end(xw);
5218d522f475Smrg
5219492d43a5Smrg    len = screen->unparse_len;
5220d522f475Smrg
5221d522f475Smrg#if OPT_TCAP_QUERY
5222d522f475Smrg    /*
5223d522f475Smrg     * If we're returning a termcap string, it has to be translated since
5224d522f475Smrg     * a DCS must not contain any characters except for the normal 7-bit
5225d522f475Smrg     * printable ASCII (counting tab, carriage return, etc).  For now,
5226d522f475Smrg     * just use hexadecimal for the whole thing.
5227d522f475Smrg     */
5228492d43a5Smrg    if (screen->tc_query_code >= 0) {
5229d522f475Smrg	char tmp[3];
5230d522f475Smrg	sprintf(tmp, "%02X", c & 0xFF);
52312eaa94a1Schristos	buf[len++] = CharOf(tmp[0]);
52322eaa94a1Schristos	buf[len++] = CharOf(tmp[1]);
5233d522f475Smrg    } else
5234d522f475Smrg#endif
52352eaa94a1Schristos    if ((buf[len++] = (IChar) c) == '\r' && (xw->flags & LINEFEED)) {
5236d522f475Smrg	buf[len++] = '\n';
5237d522f475Smrg    }
5238d522f475Smrg
5239492d43a5Smrg    screen->unparse_len = len;
5240d522f475Smrg
5241d522f475Smrg    /* If send/receive mode is reset, we echo characters locally */
5242d522f475Smrg    if ((xw->keyboard.flags & MODE_SRM) == 0) {
5243d522f475Smrg	(void) doparsing(xw, (unsigned) c, &myState);
5244d522f475Smrg    }
5245d522f475Smrg}
5246d522f475Smrg
5247d522f475Smrgvoid
5248d522f475Smrgunparse_end(XtermWidget xw)
5249d522f475Smrg{
525020d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
525120d2c4d2Smrg
525220d2c4d2Smrg    if (screen->unparse_len) {
5253d522f475Smrg#ifdef VMS
525420d2c4d2Smrg	tt_write(screen->unparse_bfr, screen->unparse_len);
5255d522f475Smrg#else /* VMS */
525620d2c4d2Smrg	writePtyData(screen->respond, screen->unparse_bfr, screen->unparse_len);
5257d522f475Smrg#endif /* VMS */
525820d2c4d2Smrg	screen->unparse_len = 0;
5259d522f475Smrg    }
5260d522f475Smrg}
5261d522f475Smrg
5262d522f475Smrgvoid
5263d522f475SmrgToggleAlternate(XtermWidget xw)
5264d522f475Smrg{
526520d2c4d2Smrg    if (TScreenOf(xw)->whichBuf)
5266d522f475Smrg	FromAlternate(xw);
5267d522f475Smrg    else
5268d522f475Smrg	ToAlternate(xw);
5269d522f475Smrg}
5270d522f475Smrg
5271d522f475Smrgstatic void
5272d522f475SmrgToAlternate(XtermWidget xw)
5273d522f475Smrg{
527420d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
5275d522f475Smrg
5276956cc18dSsnj    if (screen->whichBuf == 0) {
5277d522f475Smrg	TRACE(("ToAlternate\n"));
5278956cc18dSsnj	if (!screen->editBuf_index[1])
5279956cc18dSsnj	    screen->editBuf_index[1] = allocScrnBuf(xw,
5280956cc18dSsnj						    (unsigned) MaxRows(screen),
5281956cc18dSsnj						    (unsigned) MaxCols(screen),
5282956cc18dSsnj						    &screen->editBuf_data[1]);
528320d2c4d2Smrg	SwitchBufs(xw, 1);
5284956cc18dSsnj	screen->whichBuf = 1;
5285956cc18dSsnj#if OPT_SAVE_LINES
5286956cc18dSsnj	screen->visbuf = screen->editBuf_index[screen->whichBuf];
5287956cc18dSsnj#endif
5288d522f475Smrg	update_altscreen();
5289d522f475Smrg    }
5290d522f475Smrg}
5291d522f475Smrg
5292d522f475Smrgstatic void
5293d522f475SmrgFromAlternate(XtermWidget xw)
5294d522f475Smrg{
529520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
5296d522f475Smrg
5297956cc18dSsnj    if (screen->whichBuf != 0) {
5298d522f475Smrg	TRACE(("FromAlternate\n"));
5299d522f475Smrg	if (screen->scroll_amt)
5300d522f475Smrg	    FlushScroll(xw);
5301956cc18dSsnj	screen->whichBuf = 0;
530220d2c4d2Smrg	SwitchBufs(xw, 0);
5303956cc18dSsnj#if OPT_SAVE_LINES
5304956cc18dSsnj	screen->visbuf = screen->editBuf_index[screen->whichBuf];
5305956cc18dSsnj#endif
5306d522f475Smrg	update_altscreen();
5307d522f475Smrg    }
5308d522f475Smrg}
5309d522f475Smrg
5310d522f475Smrgstatic void
531120d2c4d2SmrgSwitchBufs(XtermWidget xw, int toBuf)
5312d522f475Smrg{
531320d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
5314d522f475Smrg    int rows, top;
5315d522f475Smrg
5316d522f475Smrg    if (screen->cursor_state)
5317d522f475Smrg	HideCursor();
5318d522f475Smrg
5319d522f475Smrg    rows = MaxRows(screen);
532020d2c4d2Smrg    SwitchBufPtrs(screen, toBuf);
5321d522f475Smrg
5322d522f475Smrg    if ((top = INX2ROW(screen, 0)) < rows) {
532320d2c4d2Smrg	if (screen->scroll_amt) {
5324d522f475Smrg	    FlushScroll(xw);
532520d2c4d2Smrg	}
5326d522f475Smrg	XClearArea(screen->display,
5327d522f475Smrg		   VWindow(screen),
5328d522f475Smrg		   (int) OriginX(screen),
5329d522f475Smrg		   (int) top * FontHeight(screen) + screen->border,
5330d522f475Smrg		   (unsigned) Width(screen),
53312eaa94a1Schristos		   (unsigned) ((rows - top) * FontHeight(screen)),
5332d522f475Smrg		   False);
5333d522f475Smrg    }
5334d522f475Smrg    ScrnUpdate(xw, 0, 0, rows, MaxCols(screen), False);
5335d522f475Smrg}
5336d522f475Smrg
5337d522f475SmrgBool
5338d522f475SmrgCheckBufPtrs(TScreen * screen)
5339d522f475Smrg{
5340d522f475Smrg    return (screen->visbuf != 0
5341956cc18dSsnj#if OPT_SAVE_LINES
5342956cc18dSsnj	    && screen->editBuf_index[0] != 0
5343956cc18dSsnj#endif
5344956cc18dSsnj	    && screen->editBuf_index[1] != 0);
5345d522f475Smrg}
5346d522f475Smrg
5347d522f475Smrg/*
5348d522f475Smrg * Swap buffer line pointers between alternate and regular screens.
5349d522f475Smrg */
5350d522f475Smrgvoid
535120d2c4d2SmrgSwitchBufPtrs(TScreen * screen, int toBuf GCC_UNUSED)
5352d522f475Smrg{
5353d522f475Smrg    if (CheckBufPtrs(screen)) {
5354956cc18dSsnj#if OPT_SAVE_LINES
535520d2c4d2Smrg	screen->visbuf = screen->editBuf_index[toBuf];
5356956cc18dSsnj#else
535720d2c4d2Smrg	size_t len = ScrnPointers(screen, (size_t) MaxRows(screen));
5358d522f475Smrg
5359956cc18dSsnj	memcpy(screen->save_ptr, screen->visbuf, len);
5360956cc18dSsnj	memcpy(screen->visbuf, screen->editBuf_index[1], len);
5361956cc18dSsnj	memcpy(screen->editBuf_index[1], screen->save_ptr, len);
5362956cc18dSsnj#endif
5363d522f475Smrg    }
5364d522f475Smrg}
5365d522f475Smrg
5366d522f475Smrgvoid
5367956cc18dSsnjVTRun(XtermWidget xw)
5368d522f475Smrg{
5369956cc18dSsnj    TScreen *screen = TScreenOf(xw);
5370d522f475Smrg
5371d522f475Smrg    TRACE(("VTRun ...\n"));
5372d522f475Smrg
5373d522f475Smrg    if (!screen->Vshow) {
5374d522f475Smrg	set_vt_visibility(True);
5375d522f475Smrg    }
5376d522f475Smrg    update_vttekmode();
5377d522f475Smrg    update_vtshow();
5378d522f475Smrg    update_tekshow();
5379d522f475Smrg    set_vthide_sensitivity();
5380d522f475Smrg
5381956cc18dSsnj    ScrnAllocBuf(xw);
5382d522f475Smrg
5383d522f475Smrg    screen->cursor_state = OFF;
5384d522f475Smrg    screen->cursor_set = ON;
5385d522f475Smrg#if OPT_BLINK_CURS
5386d522f475Smrg    if (DoStartBlinking(screen))
5387d522f475Smrg	StartBlinking(screen);
5388d522f475Smrg#endif
5389d522f475Smrg
5390d522f475Smrg#if OPT_TEK4014
5391d522f475Smrg    if (Tpushb > Tpushback) {
539220d2c4d2Smrg	fillPtyData(xw, VTbuffer, (char *) Tpushback, (int) (Tpushb - Tpushback));
5393d522f475Smrg	Tpushb = Tpushback;
5394d522f475Smrg    }
5395d522f475Smrg#endif
5396d522f475Smrg    screen->is_running = True;
5397a1f3da82Smrg#if OPT_MAXIMIZE
5398a1f3da82Smrg    if (resource.fullscreen == esTrue || resource.fullscreen == esAlways)
5399a1f3da82Smrg	FullScreen(term, True);
5400a1f3da82Smrg#endif
5401d522f475Smrg    if (!setjmp(VTend))
5402956cc18dSsnj	VTparse(xw);
5403d522f475Smrg    StopBlinking(screen);
5404d522f475Smrg    HideCursor();
5405d522f475Smrg    screen->cursor_set = OFF;
5406d522f475Smrg    TRACE(("... VTRun\n"));
5407d522f475Smrg}
5408d522f475Smrg
5409d522f475Smrg/*ARGSUSED*/
5410d522f475Smrgstatic void
5411d522f475SmrgVTExpose(Widget w GCC_UNUSED,
5412d522f475Smrg	 XEvent * event,
5413d522f475Smrg	 Region region GCC_UNUSED)
5414d522f475Smrg{
5415d522f475Smrg#ifdef DEBUG
5416d522f475Smrg    if (debug)
5417d522f475Smrg	fputs("Expose\n", stderr);
5418d522f475Smrg#endif /* DEBUG */
5419d522f475Smrg    if (event->type == Expose)
5420d522f475Smrg	HandleExposure(term, event);
5421d522f475Smrg}
5422d522f475Smrg
5423d522f475Smrgstatic void
5424d522f475SmrgVTGraphicsOrNoExpose(XEvent * event)
5425d522f475Smrg{
5426d522f475Smrg    TScreen *screen = TScreenOf(term);
5427d522f475Smrg    if (screen->incopy <= 0) {
5428d522f475Smrg	screen->incopy = 1;
5429d522f475Smrg	if (screen->scrolls > 0)
5430d522f475Smrg	    screen->scrolls--;
5431d522f475Smrg    }
5432d522f475Smrg    if (event->type == GraphicsExpose)
5433d522f475Smrg	if (HandleExposure(term, event))
5434d522f475Smrg	    screen->cursor_state = OFF;
5435d522f475Smrg    if ((event->type == NoExpose)
5436d522f475Smrg	|| ((XGraphicsExposeEvent *) event)->count == 0) {
5437d522f475Smrg	if (screen->incopy <= 0 && screen->scrolls > 0)
5438d522f475Smrg	    screen->scrolls--;
5439d522f475Smrg	if (screen->scrolls)
5440d522f475Smrg	    screen->incopy = -1;
5441d522f475Smrg	else
5442d522f475Smrg	    screen->incopy = 0;
5443d522f475Smrg    }
5444d522f475Smrg}
5445d522f475Smrg
5446d522f475Smrg/*ARGSUSED*/
5447d522f475Smrgstatic void
5448d522f475SmrgVTNonMaskableEvent(Widget w GCC_UNUSED,
5449d522f475Smrg		   XtPointer closure GCC_UNUSED,
5450d522f475Smrg		   XEvent * event,
5451d522f475Smrg		   Boolean * cont GCC_UNUSED)
5452d522f475Smrg{
5453d522f475Smrg    switch (event->type) {
5454d522f475Smrg    case GraphicsExpose:
5455d522f475Smrg    case NoExpose:
5456d522f475Smrg	VTGraphicsOrNoExpose(event);
5457d522f475Smrg	break;
5458d522f475Smrg    }
5459d522f475Smrg}
5460d522f475Smrg
5461d522f475Smrgstatic void
5462d522f475SmrgVTResize(Widget w)
5463d522f475Smrg{
5464d522f475Smrg    if (XtIsRealized(w)) {
5465d522f475Smrg	XtermWidget xw = (XtermWidget) w;
5466d522f475Smrg	ScreenResize(xw, xw->core.width, xw->core.height, &xw->flags);
5467d522f475Smrg    }
5468d522f475Smrg}
5469d522f475Smrg
54702eaa94a1Schristos#define okDimension(src,dst) ((src <= 32767) \
54712eaa94a1Schristos			  && ((dst = (Dimension) src) == src))
5472d522f475Smrg
5473d522f475Smrgstatic void
5474d522f475SmrgRequestResize(XtermWidget xw, int rows, int cols, Bool text)
5475d522f475Smrg{
547620d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
5477d522f475Smrg    unsigned long value;
5478d522f475Smrg    Dimension replyWidth, replyHeight;
5479d522f475Smrg    Dimension askedWidth, askedHeight;
5480d522f475Smrg    XtGeometryResult status;
5481d522f475Smrg    XWindowAttributes attrs;
5482d522f475Smrg
5483d522f475Smrg    TRACE(("RequestResize(rows=%d, cols=%d, text=%d)\n", rows, cols, text));
5484d522f475Smrg
548520d2c4d2Smrg    if ((int) (askedWidth = (Dimension) cols) < cols
548620d2c4d2Smrg	|| (int) (askedHeight = (Dimension) rows) < rows)
5487d522f475Smrg	return;
5488d522f475Smrg
5489d522f475Smrg    if (askedHeight == 0
5490d522f475Smrg	|| askedWidth == 0
5491d522f475Smrg	|| xw->misc.limit_resize > 0) {
5492d522f475Smrg	XGetWindowAttributes(XtDisplay(xw),
5493d522f475Smrg			     RootWindowOfScreen(XtScreen(xw)), &attrs);
5494d522f475Smrg    }
5495d522f475Smrg
5496d522f475Smrg    if (text) {
54972eaa94a1Schristos	if ((value = (unsigned long) rows) != 0) {
5498d522f475Smrg	    if (rows < 0)
54992eaa94a1Schristos		value = (unsigned long) MaxRows(screen);
55002eaa94a1Schristos	    value *= (unsigned long) FontHeight(screen);
55012eaa94a1Schristos	    value += (unsigned long) (2 * screen->border);
5502d522f475Smrg	    if (!okDimension(value, askedHeight))
5503d522f475Smrg		return;
5504d522f475Smrg	}
5505d522f475Smrg
55062eaa94a1Schristos	if ((value = (unsigned long) cols) != 0) {
5507d522f475Smrg	    if (cols < 0)
55082eaa94a1Schristos		value = (unsigned long) MaxCols(screen);
55092eaa94a1Schristos	    value *= (unsigned long) FontWidth(screen);
55102eaa94a1Schristos	    value += (unsigned long) ((2 * screen->border)
55112eaa94a1Schristos				      + ScrollbarWidth(screen));
5512d522f475Smrg	    if (!okDimension(value, askedWidth))
5513d522f475Smrg		return;
5514d522f475Smrg	}
5515d522f475Smrg
5516d522f475Smrg    } else {
5517d522f475Smrg	if (rows < 0)
5518d522f475Smrg	    askedHeight = FullHeight(screen);
5519d522f475Smrg	if (cols < 0)
5520d522f475Smrg	    askedWidth = FullWidth(screen);
5521d522f475Smrg    }
5522d522f475Smrg
5523d522f475Smrg    if (rows == 0)
55242eaa94a1Schristos	askedHeight = (Dimension) attrs.height;
5525d522f475Smrg    if (cols == 0)
55262eaa94a1Schristos	askedWidth = (Dimension) attrs.width;
5527d522f475Smrg
5528d522f475Smrg    if (xw->misc.limit_resize > 0) {
55292eaa94a1Schristos	Dimension high = (Dimension) (xw->misc.limit_resize * attrs.height);
55302eaa94a1Schristos	Dimension wide = (Dimension) (xw->misc.limit_resize * attrs.width);
553120d2c4d2Smrg	if ((int) high < attrs.height)
55322eaa94a1Schristos	    high = (Dimension) attrs.height;
5533d522f475Smrg	if (askedHeight > high)
5534d522f475Smrg	    askedHeight = high;
553520d2c4d2Smrg	if ((int) wide < attrs.width)
55362eaa94a1Schristos	    wide = (Dimension) attrs.width;
5537d522f475Smrg	if (askedWidth > wide)
5538d522f475Smrg	    askedWidth = wide;
5539d522f475Smrg    }
5540d522f475Smrg#ifndef nothack
5541d522f475Smrg    getXtermSizeHints(xw);
5542d522f475Smrg#endif
5543d522f475Smrg
5544956cc18dSsnj    TRACE(("...requesting resize %dx%d\n", askedHeight, askedWidth));
55452eaa94a1Schristos    status = REQ_RESIZE((Widget) xw,
55462eaa94a1Schristos			askedWidth, askedHeight,
55472eaa94a1Schristos			&replyWidth, &replyHeight);
5548d522f475Smrg
5549d522f475Smrg    if (status == XtGeometryYes ||
5550d522f475Smrg	status == XtGeometryDone) {
5551d522f475Smrg	ScreenResize(xw, replyWidth, replyHeight, &xw->flags);
5552d522f475Smrg    }
5553d522f475Smrg#ifndef nothack
5554d522f475Smrg    /*
5555d522f475Smrg     * XtMakeResizeRequest() has the undesirable side-effect of clearing
5556d522f475Smrg     * the window manager's hints, even on a failed request.  This would
5557d522f475Smrg     * presumably be fixed if the shell did its own work.
5558d522f475Smrg     */
5559d522f475Smrg    if (xw->hints.flags
5560d522f475Smrg	&& replyHeight
5561d522f475Smrg	&& replyWidth) {
5562d522f475Smrg	xw->hints.height = replyHeight;
5563d522f475Smrg	xw->hints.width = replyWidth;
5564d522f475Smrg
5565d522f475Smrg	TRACE(("%s@%d -- ", __FILE__, __LINE__));
5566d522f475Smrg	TRACE_HINTS(&xw->hints);
5567a1f3da82Smrg	XSetWMNormalHints(screen->display, VShellWindow(xw), &xw->hints);
5568d522f475Smrg	TRACE(("%s@%d -- ", __FILE__, __LINE__));
5569d522f475Smrg	TRACE_WM_HINTS(xw);
5570d522f475Smrg    }
5571d522f475Smrg#endif
5572d522f475Smrg
5573d522f475Smrg    XSync(screen->display, False);	/* synchronize */
5574d522f475Smrg    if (XtAppPending(app_con))
5575d522f475Smrg	xevents();
5576d522f475Smrg
5577d522f475Smrg    TRACE(("...RequestResize done\n"));
5578d522f475Smrg}
5579d522f475Smrg
5580d522f475Smrgstatic String xterm_trans =
5581d522f475Smrg"<ClientMessage>WM_PROTOCOLS: DeleteWindow()\n\
5582d522f475Smrg     <MappingNotify>: KeyboardMapping()\n";
5583d522f475Smrg
5584d522f475Smrgint
5585956cc18dSsnjVTInit(XtermWidget xw)
5586d522f475Smrg{
5587956cc18dSsnj    Widget vtparent = SHELL_OF(xw);
5588d522f475Smrg
5589d522f475Smrg    TRACE(("VTInit {{\n"));
5590d522f475Smrg
5591d522f475Smrg    XtRealizeWidget(vtparent);
5592d522f475Smrg    XtOverrideTranslations(vtparent, XtParseTranslationTable(xterm_trans));
5593d522f475Smrg    (void) XSetWMProtocols(XtDisplay(vtparent), XtWindow(vtparent),
5594d522f475Smrg			   &wm_delete_window, 1);
5595d522f475Smrg    TRACE_TRANS("shell", vtparent);
5596956cc18dSsnj    TRACE_TRANS("vt100", (Widget) (xw));
5597d522f475Smrg
5598956cc18dSsnj    ScrnAllocBuf(xw);
5599d522f475Smrg
5600d522f475Smrg    TRACE(("...}} VTInit\n"));
5601d522f475Smrg    return (1);
5602d522f475Smrg}
5603d522f475Smrg
5604d522f475Smrgstatic void
5605d522f475SmrgVTClassInit(void)
5606d522f475Smrg{
5607d522f475Smrg    XtAddConverter(XtRString, XtRGravity, XmuCvtStringToGravity,
5608d522f475Smrg		   (XtConvertArgList) NULL, (Cardinal) 0);
5609d522f475Smrg}
5610d522f475Smrg
5611d522f475Smrg#if OPT_COLOR_RES
5612d522f475Smrg/*
5613d522f475Smrg * Override the use of XtDefaultForeground/XtDefaultBackground to make some
5614d522f475Smrg * colors, such as cursor color, use the actual foreground/background value
5615d522f475Smrg * if there is no explicit resource value used.
5616d522f475Smrg */
5617d522f475Smrgstatic Pixel
5618d522f475Smrgfill_Tres(XtermWidget target, XtermWidget source, int offset)
5619d522f475Smrg{
5620d522f475Smrg    char *name;
5621d522f475Smrg    ScrnColors temp;
5622d522f475Smrg
562320d2c4d2Smrg    TScreenOf(target)->Tcolors[offset] = TScreenOf(source)->Tcolors[offset];
562420d2c4d2Smrg    TScreenOf(target)->Tcolors[offset].mode = False;
5625d522f475Smrg
562620d2c4d2Smrg    if ((name = x_strtrim(TScreenOf(target)->Tcolors[offset].resource)) != 0)
562720d2c4d2Smrg	TScreenOf(target)->Tcolors[offset].resource = name;
5628d522f475Smrg
5629d522f475Smrg    if (name == 0) {
563020d2c4d2Smrg	TScreenOf(target)->Tcolors[offset].value = target->dft_foreground;
5631d522f475Smrg    } else if (isDefaultForeground(name)) {
563220d2c4d2Smrg	TScreenOf(target)->Tcolors[offset].value =
5633d522f475Smrg	    ((offset == TEXT_FG || offset == TEXT_BG)
5634d522f475Smrg	     ? target->dft_foreground
563520d2c4d2Smrg	     : TScreenOf(target)->Tcolors[TEXT_FG].value);
5636d522f475Smrg    } else if (isDefaultBackground(name)) {
563720d2c4d2Smrg	TScreenOf(target)->Tcolors[offset].value =
5638d522f475Smrg	    ((offset == TEXT_FG || offset == TEXT_BG)
5639d522f475Smrg	     ? target->dft_background
564020d2c4d2Smrg	     : TScreenOf(target)->Tcolors[TEXT_BG].value);
5641d522f475Smrg    } else {
5642d522f475Smrg	memset(&temp, 0, sizeof(temp));
564320d2c4d2Smrg	if (AllocateTermColor(target, &temp, offset, name, True)) {
5644d522f475Smrg	    if (COLOR_DEFINED(&(temp), offset))
5645d522f475Smrg		free(temp.names[offset]);
564620d2c4d2Smrg	    TScreenOf(target)->Tcolors[offset].value = temp.colors[offset];
5647d522f475Smrg	}
5648d522f475Smrg    }
564920d2c4d2Smrg    return TScreenOf(target)->Tcolors[offset].value;
5650d522f475Smrg}
5651d522f475Smrg#else
5652d522f475Smrg#define fill_Tres(target, source, offset) \
565320d2c4d2Smrg	TScreenOf(target)->Tcolors[offset] = TScreenOf(source)->Tcolors[offset]
5654d522f475Smrg#endif
5655d522f475Smrg
5656d522f475Smrg#if OPT_WIDE_CHARS
5657d522f475Smrgstatic void
565820d2c4d2SmrgVTInitialize_locale(XtermWidget xw)
5659d522f475Smrg{
566020d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
5661d522f475Smrg    Bool is_utf8 = xtermEnvUTF8();
5662d522f475Smrg
5663d522f475Smrg    TRACE(("VTInitialize_locale\n"));
566420d2c4d2Smrg    TRACE(("... request screen.utf8_mode = %d\n", screen->utf8_mode));
5665d522f475Smrg
566620d2c4d2Smrg    if (screen->utf8_mode < 0)
566720d2c4d2Smrg	screen->utf8_mode = uFalse;
5668d522f475Smrg
566920d2c4d2Smrg    if (screen->utf8_mode > 3)
567020d2c4d2Smrg	screen->utf8_mode = uDefault;
5671d522f475Smrg
567220d2c4d2Smrg    screen->latin9_mode = 0;
567320d2c4d2Smrg    screen->unicode_font = 0;
5674d522f475Smrg#if OPT_LUIT_PROG
567520d2c4d2Smrg    xw->misc.callfilter = 0;
567620d2c4d2Smrg    xw->misc.use_encoding = 0;
5677d522f475Smrg
5678d522f475Smrg    TRACE(("... setup for luit:\n"));
567920d2c4d2Smrg    TRACE(("... request misc.locale_str = \"%s\"\n", xw->misc.locale_str));
5680d522f475Smrg
568120d2c4d2Smrg    if (screen->utf8_mode == uFalse) {
5682d522f475Smrg	TRACE(("... command-line +u8 overrides\n"));
5683d522f475Smrg    } else
5684d522f475Smrg#if OPT_MINI_LUIT
568520d2c4d2Smrg    if (x_strcasecmp(xw->misc.locale_str, "CHECKFONT") == 0) {
568620d2c4d2Smrg	int fl = (xw->misc.default_font.f_n
568720d2c4d2Smrg		  ? (int) strlen(xw->misc.default_font.f_n)
5688d522f475Smrg		  : 0);
5689d522f475Smrg	if (fl > 11
569020d2c4d2Smrg	    && x_strcasecmp(xw->misc.default_font.f_n + fl - 11,
5691d522f475Smrg			    "-ISO10646-1") == 0) {
569220d2c4d2Smrg	    screen->unicode_font = 1;
5693d522f475Smrg	    /* unicode font, use True */
5694d522f475Smrg#ifdef HAVE_LANGINFO_CODESET
5695d522f475Smrg	    if (!strcmp(xtermEnvEncoding(), "ANSI_X3.4-1968")
5696d522f475Smrg		|| !strcmp(xtermEnvEncoding(), "ISO-8859-1")) {
569720d2c4d2Smrg		if (screen->utf8_mode == uDefault)
569820d2c4d2Smrg		    screen->utf8_mode = uFalse;
5699d522f475Smrg	    } else if (!strcmp(xtermEnvEncoding(), "ISO-8859-15")) {
570020d2c4d2Smrg		if (screen->utf8_mode == uDefault)
570120d2c4d2Smrg		    screen->utf8_mode = uFalse;
570220d2c4d2Smrg		screen->latin9_mode = 1;
5703d522f475Smrg	    } else {
570420d2c4d2Smrg		xw->misc.callfilter = (Boolean) (is_utf8 ? 0 : 1);
570520d2c4d2Smrg		screen->utf8_mode = uAlways;
5706d522f475Smrg	    }
5707d522f475Smrg#else
570820d2c4d2Smrg	    xw->misc.callfilter = is_utf8 ? 0 : 1;
570920d2c4d2Smrg	    screen->utf8_mode = uAlways;
5710d522f475Smrg#endif
5711d522f475Smrg	} else {
5712d522f475Smrg	    /* other encoding, use False */
571320d2c4d2Smrg	    if (screen->utf8_mode == uDefault) {
571420d2c4d2Smrg		screen->utf8_mode = is_utf8 ? uAlways : uFalse;
5715d522f475Smrg	    }
5716d522f475Smrg	}
5717d522f475Smrg    } else
5718d522f475Smrg#endif /* OPT_MINI_LUIT */
571920d2c4d2Smrg	if (x_strcasecmp(xw->misc.locale_str, "TRUE") == 0 ||
572020d2c4d2Smrg	    x_strcasecmp(xw->misc.locale_str, "ON") == 0 ||
572120d2c4d2Smrg	    x_strcasecmp(xw->misc.locale_str, "YES") == 0 ||
572220d2c4d2Smrg	    x_strcasecmp(xw->misc.locale_str, "AUTO") == 0 ||
572320d2c4d2Smrg	    strcmp(xw->misc.locale_str, "1") == 0) {
5724d522f475Smrg	/* when true ... fully obeying LC_CTYPE locale */
572520d2c4d2Smrg	xw->misc.callfilter = (Boolean) (is_utf8 ? 0 : 1);
572620d2c4d2Smrg	screen->utf8_mode = uAlways;
572720d2c4d2Smrg    } else if (x_strcasecmp(xw->misc.locale_str, "FALSE") == 0 ||
572820d2c4d2Smrg	       x_strcasecmp(xw->misc.locale_str, "OFF") == 0 ||
572920d2c4d2Smrg	       x_strcasecmp(xw->misc.locale_str, "NO") == 0 ||
573020d2c4d2Smrg	       strcmp(xw->misc.locale_str, "0") == 0) {
5731d522f475Smrg	/* when false ... original value of utf8_mode is effective */
573220d2c4d2Smrg	if (screen->utf8_mode == uDefault) {
573320d2c4d2Smrg	    screen->utf8_mode = is_utf8 ? uAlways : uFalse;
5734d522f475Smrg	}
573520d2c4d2Smrg    } else if (x_strcasecmp(xw->misc.locale_str, "MEDIUM") == 0 ||
573620d2c4d2Smrg	       x_strcasecmp(xw->misc.locale_str, "SEMIAUTO") == 0) {
5737d522f475Smrg	/* when medium ... obeying locale only for UTF-8 and Asian */
5738d522f475Smrg	if (is_utf8) {
573920d2c4d2Smrg	    screen->utf8_mode = uAlways;
5740d522f475Smrg	} else if (
5741d522f475Smrg#ifdef MB_CUR_MAX
5742d522f475Smrg		      MB_CUR_MAX > 1 ||
5743d522f475Smrg#else
574420d2c4d2Smrg		      !strncmp(xtermEnvLocale(), "ja", (size_t) 2) ||
574520d2c4d2Smrg		      !strncmp(xtermEnvLocale(), "ko", (size_t) 2) ||
574620d2c4d2Smrg		      !strncmp(xtermEnvLocale(), "zh", (size_t) 2) ||
574720d2c4d2Smrg#endif
574820d2c4d2Smrg		      !strncmp(xtermEnvLocale(), "th", (size_t) 2) ||
574920d2c4d2Smrg		      !strncmp(xtermEnvLocale(), "vi", (size_t) 2)) {
575020d2c4d2Smrg	    xw->misc.callfilter = 1;
575120d2c4d2Smrg	    screen->utf8_mode = uAlways;
5752d522f475Smrg	} else {
575320d2c4d2Smrg	    screen->utf8_mode = uFalse;
5754d522f475Smrg	}
575520d2c4d2Smrg    } else if (x_strcasecmp(xw->misc.locale_str, "UTF-8") == 0 ||
575620d2c4d2Smrg	       x_strcasecmp(xw->misc.locale_str, "UTF8") == 0) {
5757d522f475Smrg	/* when UTF-8 ... UTF-8 mode */
575820d2c4d2Smrg	screen->utf8_mode = uAlways;
5759d522f475Smrg    } else {
5760d522f475Smrg	/* other words are regarded as encoding name passed to luit */
576120d2c4d2Smrg	xw->misc.callfilter = 1;
576220d2c4d2Smrg	screen->utf8_mode = uAlways;
576320d2c4d2Smrg	xw->misc.use_encoding = 1;
5764d522f475Smrg    }
576520d2c4d2Smrg    TRACE(("... updated misc.callfilter = %s\n", BtoS(xw->misc.callfilter)));
576620d2c4d2Smrg    TRACE(("... updated misc.use_encoding = %s\n", BtoS(xw->misc.use_encoding)));
5767d522f475Smrg#else
576820d2c4d2Smrg    if (screen->utf8_mode == uDefault) {
576920d2c4d2Smrg	screen->utf8_mode = is_utf8 ? uAlways : uFalse;
5770d522f475Smrg    }
5771d522f475Smrg#endif /* OPT_LUIT_PROG */
5772d522f475Smrg
577320d2c4d2Smrg    screen->utf8_inparse = (Boolean) (screen->utf8_mode != uFalse);
5774d522f475Smrg
577520d2c4d2Smrg    TRACE(("... updated screen.utf8_mode = %d\n", screen->utf8_mode));
5776d522f475Smrg    TRACE(("...VTInitialize_locale done\n"));
5777d522f475Smrg}
5778d522f475Smrg#endif
5779d522f475Smrg
5780d522f475Smrgstatic void
5781d522f475SmrgParseOnClicks(XtermWidget wnew, XtermWidget wreq, Cardinal item)
5782d522f475Smrg{
5783d522f475Smrg    /* *INDENT-OFF* */
5784d522f475Smrg    static struct {
578520d2c4d2Smrg	const char *	name;
5786d522f475Smrg	SelectUnit	code;
5787d522f475Smrg    } table[] = {
5788d522f475Smrg    	{ "char",	Select_CHAR },
5789d522f475Smrg    	{ "word",	Select_WORD },
5790d522f475Smrg    	{ "line",	Select_LINE },
5791d522f475Smrg    	{ "group",	Select_GROUP },
5792d522f475Smrg    	{ "page",	Select_PAGE },
5793d522f475Smrg    	{ "all",	Select_ALL },
5794d522f475Smrg#if OPT_SELECT_REGEX
5795d522f475Smrg    	{ "regex",	Select_REGEX },
5796d522f475Smrg#endif
5797d522f475Smrg    };
5798d522f475Smrg    /* *INDENT-ON* */
5799d522f475Smrg
580020d2c4d2Smrg    String res = TScreenOf(wreq)->onClick[item];
5801d522f475Smrg    String next = x_skip_nonblanks(res);
5802d522f475Smrg    Cardinal n;
5803d522f475Smrg
580420d2c4d2Smrg    TScreenOf(wnew)->selectMap[item] = NSELECTUNITS;
5805d522f475Smrg    for (n = 0; n < XtNumber(table); ++n) {
5806d522f475Smrg	if (!x_strncasecmp(table[n].name, res, (unsigned) (next - res))) {
580720d2c4d2Smrg	    TScreenOf(wnew)->selectMap[item] = table[n].code;
5808d522f475Smrg#if OPT_SELECT_REGEX
5809d522f475Smrg	    if (table[n].code == Select_REGEX) {
581020d2c4d2Smrg		TScreenOf(wnew)->selectExpr[item] = x_strtrim(next);
581120d2c4d2Smrg		TRACE(("Parsed regex \"%s\"\n", TScreenOf(wnew)->selectExpr[item]));
5812d522f475Smrg	    }
5813d522f475Smrg#endif
5814d522f475Smrg	    break;
5815d522f475Smrg	}
5816d522f475Smrg    }
5817d522f475Smrg}
5818d522f475Smrg
581920d2c4d2Smrg/*
582020d2c4d2Smrg * Parse a comma-separated list, returning a string which the caller must
582120d2c4d2Smrg * free, and updating the source pointer.
582220d2c4d2Smrg */
582320d2c4d2Smrgstatic char *
582420d2c4d2SmrgParseList(const char **source)
582520d2c4d2Smrg{
582620d2c4d2Smrg    const char *base = *source;
582720d2c4d2Smrg    const char *next;
582820d2c4d2Smrg    size_t size;
582920d2c4d2Smrg    char *value = 0;
583020d2c4d2Smrg    char *result;
583120d2c4d2Smrg
583220d2c4d2Smrg    /* ignore empty values */
583320d2c4d2Smrg    while (*base == ',')
583420d2c4d2Smrg	++base;
583520d2c4d2Smrg    if (*base != '\0') {
583620d2c4d2Smrg	next = base;
583720d2c4d2Smrg	while (*next != '\0' && *next != ',')
583820d2c4d2Smrg	    ++next;
583920d2c4d2Smrg	size = (size_t) (1 + next - base);
584020d2c4d2Smrg	value = malloc(size);
584120d2c4d2Smrg	if (value != 0) {
584220d2c4d2Smrg	    memcpy(value, base, size);
584320d2c4d2Smrg	    value[size - 1] = '\0';
584420d2c4d2Smrg	}
584520d2c4d2Smrg	*source = next;
584620d2c4d2Smrg    } else {
584720d2c4d2Smrg	*source = base;
584820d2c4d2Smrg    }
584920d2c4d2Smrg    result = x_strtrim(value);
585020d2c4d2Smrg    free(value);
585120d2c4d2Smrg    return result;
585220d2c4d2Smrg}
585320d2c4d2Smrg
585420d2c4d2Smrgstatic void
585520d2c4d2Smrgset_flags_from_list(char *target,
585620d2c4d2Smrg		    const char *source,
585720d2c4d2Smrg		    FlagList * list,
585820d2c4d2Smrg		    Cardinal limit)
585920d2c4d2Smrg{
586020d2c4d2Smrg    Cardinal n;
586120d2c4d2Smrg    int value = -1;
586220d2c4d2Smrg
586320d2c4d2Smrg    while (*source != '\0') {
586420d2c4d2Smrg	char *next = ParseList(&source);
586520d2c4d2Smrg	Boolean found = False;
586620d2c4d2Smrg
586720d2c4d2Smrg	if (next == 0)
586820d2c4d2Smrg	    break;
586920d2c4d2Smrg	if (isdigit(CharOf(*next))) {
587020d2c4d2Smrg	    char *temp;
587120d2c4d2Smrg
587220d2c4d2Smrg	    value = (int) strtol(next, &temp, 0);
587320d2c4d2Smrg	    if (!IsEmpty(temp)) {
587420d2c4d2Smrg		fprintf(stderr, "Expected a number: %s\n", next);
587520d2c4d2Smrg	    } else {
587620d2c4d2Smrg		for (n = 0; n < limit; ++n) {
587720d2c4d2Smrg		    if (list[n].code == value) {
587820d2c4d2Smrg			target[value] = 1;
587920d2c4d2Smrg			found = True;
588020d2c4d2Smrg			break;
588120d2c4d2Smrg		    }
588220d2c4d2Smrg		}
588320d2c4d2Smrg	    }
588420d2c4d2Smrg	} else {
588520d2c4d2Smrg	    for (n = 0; n < limit; ++n) {
588620d2c4d2Smrg		if (!x_strcasecmp(next, list[n].name)) {
588720d2c4d2Smrg		    value = list[n].code;
588820d2c4d2Smrg		    target[value] = 1;
588920d2c4d2Smrg		    found = True;
589020d2c4d2Smrg		    break;
589120d2c4d2Smrg		}
589220d2c4d2Smrg	    }
589320d2c4d2Smrg	}
589420d2c4d2Smrg	if (!found) {
589520d2c4d2Smrg	    fprintf(stderr, "Unrecognized keyword: %s\n", next);
589620d2c4d2Smrg	} else {
589720d2c4d2Smrg	    TRACE(("...found %s (%d)\n", next, value));
589820d2c4d2Smrg	}
589920d2c4d2Smrg	free(next);
590020d2c4d2Smrg    }
590120d2c4d2Smrg}
590220d2c4d2Smrg
5903d522f475Smrg/* ARGSUSED */
5904d522f475Smrgstatic void
5905d522f475SmrgVTInitialize(Widget wrequest,
5906d522f475Smrg	     Widget new_arg,
5907d522f475Smrg	     ArgList args GCC_UNUSED,
5908d522f475Smrg	     Cardinal *num_args GCC_UNUSED)
5909d522f475Smrg{
591020d2c4d2Smrg#define Kolor(name) TScreenOf(wnew)->name.resource
5911d522f475Smrg#define TxtFg(name) !x_strcasecmp(Kolor(Tcolors[TEXT_FG]), Kolor(name))
5912d522f475Smrg#define TxtBg(name) !x_strcasecmp(Kolor(Tcolors[TEXT_BG]), Kolor(name))
5913d522f475Smrg#define DftFg(name) isDefaultForeground(Kolor(name))
5914d522f475Smrg#define DftBg(name) isDefaultBackground(Kolor(name))
5915d522f475Smrg
591620d2c4d2Smrg#define DATA(name) { #name, ec##name }
591720d2c4d2Smrg    static FlagList tblColorOps[] =
591820d2c4d2Smrg    {
591920d2c4d2Smrg	DATA(SetColor)
592020d2c4d2Smrg	,DATA(GetColor)
592120d2c4d2Smrg	,DATA(GetAnsiColor)
592220d2c4d2Smrg    };
592320d2c4d2Smrg#undef DATA
592420d2c4d2Smrg
592520d2c4d2Smrg#define DATA(name) { #name, ef##name }
592620d2c4d2Smrg    static FlagList tblFontOps[] =
592720d2c4d2Smrg    {
592820d2c4d2Smrg	DATA(SetFont)
592920d2c4d2Smrg	,DATA(GetFont)
593020d2c4d2Smrg    };
593120d2c4d2Smrg#undef DATA
593220d2c4d2Smrg
593320d2c4d2Smrg#define DATA(name) { #name, et##name }
593420d2c4d2Smrg    static FlagList tblTcapOps[] =
593520d2c4d2Smrg    {
593620d2c4d2Smrg	DATA(SetTcap)
593720d2c4d2Smrg	,DATA(GetTcap)
593820d2c4d2Smrg    };
593920d2c4d2Smrg#undef DATA
594020d2c4d2Smrg
594120d2c4d2Smrg#define DATA(name) { #name, ew##name }
594220d2c4d2Smrg    static FlagList tblWindowOps[] =
594320d2c4d2Smrg    {
594420d2c4d2Smrg	DATA(RestoreWin)
594520d2c4d2Smrg	,DATA(MinimizeWin)
594620d2c4d2Smrg	,DATA(SetWinPosition)
594720d2c4d2Smrg	,DATA(SetWinSizePixels)
594820d2c4d2Smrg	,DATA(RaiseWin)
594920d2c4d2Smrg	,DATA(LowerWin)
595020d2c4d2Smrg	,DATA(RefreshWin)
595120d2c4d2Smrg	,DATA(SetWinSizeChars)
595220d2c4d2Smrg#if OPT_MAXIMIZE
595320d2c4d2Smrg	,DATA(MaximizeWin)
5954a1f3da82Smrg	,DATA(FullscreenWin)
595520d2c4d2Smrg#endif
595620d2c4d2Smrg	,DATA(GetWinState)
595720d2c4d2Smrg	,DATA(GetWinPosition)
595820d2c4d2Smrg	,DATA(GetWinSizePixels)
595920d2c4d2Smrg	,DATA(GetWinSizeChars)
596020d2c4d2Smrg#if OPT_MAXIMIZE
596120d2c4d2Smrg	,DATA(GetScreenSizeChars)
596220d2c4d2Smrg#endif
596320d2c4d2Smrg	,DATA(GetIconTitle)
596420d2c4d2Smrg	,DATA(GetWinTitle)
596520d2c4d2Smrg	,DATA(PushTitle)
596620d2c4d2Smrg	,DATA(PopTitle)
596720d2c4d2Smrg	,DATA(SetWinLines)
596820d2c4d2Smrg	,DATA(SetXprop)
596920d2c4d2Smrg	,DATA(GetSelection)
597020d2c4d2Smrg	,DATA(SetSelection)
597120d2c4d2Smrg    };
597220d2c4d2Smrg#undef DATA
597320d2c4d2Smrg
597420d2c4d2Smrg#if OPT_RENDERFONT
597520d2c4d2Smrg#define DATA(name) { #name, er##name }
597620d2c4d2Smrg    static FlagList tblRenderFont[] =
597720d2c4d2Smrg    {
597820d2c4d2Smrg	DATA(Default)
597920d2c4d2Smrg    };
598020d2c4d2Smrg#undef DATA
598120d2c4d2Smrg#endif
598220d2c4d2Smrg
5983d522f475Smrg    XtermWidget request = (XtermWidget) wrequest;
5984d522f475Smrg    XtermWidget wnew = (XtermWidget) new_arg;
5985d522f475Smrg    Widget my_parent = SHELL_OF(wnew);
5986d522f475Smrg    int i;
5987492d43a5Smrg    const char *s;
5988d522f475Smrg
5989d522f475Smrg#if OPT_ISO_COLORS
5990d522f475Smrg    Bool color_ok;
5991d522f475Smrg#endif
5992d522f475Smrg
599320d2c4d2Smrg#if OPT_COLOR_RES2
5994d522f475Smrg    static XtResource fake_resources[] =
5995d522f475Smrg    {
5996d522f475Smrg#if OPT_256_COLORS
5997d522f475Smrg# include <256colres.h>
5998d522f475Smrg#elif OPT_88_COLORS
5999d522f475Smrg# include <88colres.h>
6000d522f475Smrg#endif
6001d522f475Smrg    };
6002d522f475Smrg#endif /* OPT_COLOR_RES2 */
6003d522f475Smrg
600420d2c4d2Smrg    TRACE(("VTInitialize wnew %p, %d / %d resources\n",
600520d2c4d2Smrg	   (void *) wnew, XtNumber(xterm_resources), MAXRESOURCES));
600620d2c4d2Smrg    assert(XtNumber(xterm_resources) < MAXRESOURCES);
6007d522f475Smrg
6008d522f475Smrg    /* Zero out the entire "screen" component of "wnew" widget, then do
6009d522f475Smrg     * field-by-field assignment of "screen" fields that are named in the
6010d522f475Smrg     * resource list.
6011d522f475Smrg     */
601220d2c4d2Smrg    memset(TScreenOf(wnew), 0, sizeof(wnew->screen));
6013d522f475Smrg
6014d522f475Smrg    /* DESCO Sys#67660
6015d522f475Smrg     * Zero out the entire "keyboard" component of "wnew" widget.
6016d522f475Smrg     */
6017956cc18dSsnj    memset(&wnew->keyboard, 0, sizeof(wnew->keyboard));
6018d522f475Smrg
6019d522f475Smrg    /* dummy values so that we don't try to Realize the parent shell with height
6020d522f475Smrg     * or width of 0, which is illegal in X.  The real size is computed in the
6021d522f475Smrg     * xtermWidget's Realize proc, but the shell's Realize proc is called first,
6022d522f475Smrg     * and must see a valid size.
6023d522f475Smrg     */
6024d522f475Smrg    wnew->core.height = wnew->core.width = 1;
6025d522f475Smrg
6026d522f475Smrg    /*
6027d522f475Smrg     * The definition of -rv now is that it changes the definition of
6028d522f475Smrg     * XtDefaultForeground and XtDefaultBackground.  So, we no longer
6029d522f475Smrg     * need to do anything special.
6030d522f475Smrg     */
603120d2c4d2Smrg    TScreenOf(wnew)->display = wnew->core.screen->display;
6032d522f475Smrg
6033d522f475Smrg    /*
6034d522f475Smrg     * We use the default foreground/background colors to compare/check if a
6035d522f475Smrg     * color-resource has been set.
6036d522f475Smrg     */
6037d522f475Smrg#define MyBlackPixel(dpy) BlackPixel(dpy,DefaultScreen(dpy))
6038d522f475Smrg#define MyWhitePixel(dpy) WhitePixel(dpy,DefaultScreen(dpy))
6039d522f475Smrg
6040d522f475Smrg    if (request->misc.re_verse) {
604120d2c4d2Smrg	wnew->dft_foreground = MyWhitePixel(TScreenOf(wnew)->display);
604220d2c4d2Smrg	wnew->dft_background = MyBlackPixel(TScreenOf(wnew)->display);
6043d522f475Smrg    } else {
604420d2c4d2Smrg	wnew->dft_foreground = MyBlackPixel(TScreenOf(wnew)->display);
604520d2c4d2Smrg	wnew->dft_background = MyWhitePixel(TScreenOf(wnew)->display);
6046d522f475Smrg    }
6047d522f475Smrg    init_Tres(TEXT_FG);
6048d522f475Smrg    init_Tres(TEXT_BG);
6049d522f475Smrg
6050d522f475Smrg    TRACE(("Color resource initialization:\n"));
6051d522f475Smrg    TRACE(("   Default foreground %#lx\n", wnew->dft_foreground));
6052d522f475Smrg    TRACE(("   Default background %#lx\n", wnew->dft_background));
605320d2c4d2Smrg    TRACE(("   Screen foreground  %#lx\n", T_COLOR(TScreenOf(wnew), TEXT_FG)));
605420d2c4d2Smrg    TRACE(("   Screen background  %#lx\n", T_COLOR(TScreenOf(wnew), TEXT_BG)));
6055d522f475Smrg
605620d2c4d2Smrg    TScreenOf(wnew)->mouse_button = -1;
605720d2c4d2Smrg    TScreenOf(wnew)->mouse_row = -1;
605820d2c4d2Smrg    TScreenOf(wnew)->mouse_col = -1;
6059d522f475Smrg
6060d522f475Smrg#if OPT_BOX_CHARS
6061d522f475Smrg    init_Bres(screen.force_box_chars);
606220d2c4d2Smrg    init_Bres(screen.force_packed);
6063d522f475Smrg    init_Bres(screen.force_all_chars);
6064d522f475Smrg#endif
6065d522f475Smrg    init_Bres(screen.free_bold_box);
6066d522f475Smrg
6067d522f475Smrg    init_Bres(screen.c132);
6068d522f475Smrg    init_Bres(screen.curses);
6069d522f475Smrg    init_Bres(screen.hp_ll_bc);
6070d522f475Smrg#if OPT_XMC_GLITCH
6071d522f475Smrg    init_Ires(screen.xmc_glitch);
6072d522f475Smrg    init_Ires(screen.xmc_attributes);
6073d522f475Smrg    init_Bres(screen.xmc_inline);
6074d522f475Smrg    init_Bres(screen.move_sgr_ok);
6075d522f475Smrg#endif
6076d522f475Smrg#if OPT_BLINK_CURS
6077d522f475Smrg    init_Bres(screen.cursor_blink);
6078d522f475Smrg    init_Ires(screen.blink_on);
6079d522f475Smrg    init_Ires(screen.blink_off);
608020d2c4d2Smrg    TScreenOf(wnew)->cursor_blink_res = TScreenOf(wnew)->cursor_blink;
6081d522f475Smrg#endif
60822eaa94a1Schristos    init_Bres(screen.cursor_underline);
6083d522f475Smrg#if OPT_BLINK_TEXT
6084d522f475Smrg    init_Ires(screen.blink_as_bold);
6085d522f475Smrg#endif
6086d522f475Smrg    init_Ires(screen.border);
6087d522f475Smrg    init_Bres(screen.jumpscroll);
6088956cc18dSsnj    init_Bres(screen.fastscroll);
6089d522f475Smrg    init_Bres(screen.old_fkeys);
6090d522f475Smrg    init_Bres(screen.delete_is_del);
609120d2c4d2Smrg    wnew->keyboard.type = TScreenOf(wnew)->old_fkeys
6092d522f475Smrg	? keyboardIsLegacy
6093d522f475Smrg	: keyboardIsDefault;
6094d522f475Smrg#ifdef ALLOWLOGGING
6095492d43a5Smrg    init_Bres(misc.logInhibit);
6096492d43a5Smrg    init_Bres(misc.log_on);
6097d522f475Smrg    init_Sres(screen.logfile);
6098d522f475Smrg#endif
6099d522f475Smrg    init_Bres(screen.bellIsUrgent);
6100d522f475Smrg    init_Bres(screen.bellOnReset);
6101d522f475Smrg    init_Bres(screen.marginbell);
6102d522f475Smrg    init_Bres(screen.multiscroll);
6103d522f475Smrg    init_Ires(screen.nmarginbell);
6104d522f475Smrg    init_Ires(screen.savelines);
6105d522f475Smrg    init_Ires(screen.scrollBarBorder);
6106d522f475Smrg    init_Ires(screen.scrolllines);
6107d522f475Smrg    init_Bres(screen.scrollttyoutput);
6108d522f475Smrg    init_Bres(screen.scrollkey);
6109d522f475Smrg
6110492d43a5Smrg    init_Bres(misc.autoWrap);
6111492d43a5Smrg    init_Bres(misc.login_shell);
6112492d43a5Smrg    init_Bres(misc.reverseWrap);
6113492d43a5Smrg    init_Bres(misc.scrollbar);
6114492d43a5Smrg    init_Sres(misc.geo_metry);
6115492d43a5Smrg    init_Sres(misc.T_geometry);
6116492d43a5Smrg
6117d522f475Smrg    init_Sres(screen.term_id);
611820d2c4d2Smrg    for (s = TScreenOf(request)->term_id; *s; s++) {
6119d522f475Smrg	if (!isalpha(CharOf(*s)))
6120d522f475Smrg	    break;
6121d522f475Smrg    }
612220d2c4d2Smrg    TScreenOf(wnew)->terminal_id = atoi(s);
612320d2c4d2Smrg    if (TScreenOf(wnew)->terminal_id < MIN_DECID)
612420d2c4d2Smrg	TScreenOf(wnew)->terminal_id = MIN_DECID;
612520d2c4d2Smrg    if (TScreenOf(wnew)->terminal_id > MAX_DECID)
612620d2c4d2Smrg	TScreenOf(wnew)->terminal_id = MAX_DECID;
6127d522f475Smrg    TRACE(("term_id '%s' -> terminal_id %d\n",
612820d2c4d2Smrg	   TScreenOf(wnew)->term_id,
612920d2c4d2Smrg	   TScreenOf(wnew)->terminal_id));
613020d2c4d2Smrg
613120d2c4d2Smrg    TScreenOf(wnew)->vtXX_level = (TScreenOf(wnew)->terminal_id / 100);
6132d522f475Smrg
613320d2c4d2Smrg    init_Ires(screen.title_modes);
6134d522f475Smrg    init_Bres(screen.visualbell);
6135d522f475Smrg    init_Ires(screen.visualBellDelay);
6136d522f475Smrg    init_Bres(screen.poponbell);
6137d522f475Smrg    init_Ires(misc.limit_resize);
613820d2c4d2Smrg
6139d522f475Smrg#if OPT_NUM_LOCK
6140d522f475Smrg    init_Bres(misc.real_NumLock);
6141d522f475Smrg    init_Bres(misc.alwaysUseMods);
6142d522f475Smrg    wnew->misc.num_lock = 0;
6143d522f475Smrg    wnew->misc.alt_mods = 0;
6144d522f475Smrg    wnew->misc.meta_mods = 0;
6145d522f475Smrg    wnew->misc.other_mods = 0;
6146d522f475Smrg#endif
614720d2c4d2Smrg
6148492d43a5Smrg#if OPT_INPUT_METHOD
6149492d43a5Smrg    init_Bres(misc.open_im);
6150492d43a5Smrg    init_Ires(misc.retry_im);
6151492d43a5Smrg    init_Sres(misc.f_x);
6152492d43a5Smrg    init_Sres(misc.input_method);
6153492d43a5Smrg    init_Sres(misc.preedit_type);
6154492d43a5Smrg#endif
6155492d43a5Smrg
6156d522f475Smrg#if OPT_SHIFT_FONTS
6157d522f475Smrg    init_Bres(misc.shift_fonts);
6158d522f475Smrg#endif
6159d522f475Smrg#if OPT_SUNPC_KBD
6160d522f475Smrg    init_Ires(misc.ctrl_fkeys);
6161d522f475Smrg#endif
6162d522f475Smrg#if OPT_TEK4014
6163d522f475Smrg    TEK4014_SHOWN(wnew) = False;	/* not a resource... */
6164d522f475Smrg    init_Bres(misc.tekInhibit);
6165d522f475Smrg    init_Bres(misc.tekSmall);
6166d522f475Smrg    init_Bres(misc.TekEmu);
6167d522f475Smrg#endif
6168d522f475Smrg#if OPT_TCAP_QUERY
616920d2c4d2Smrg    TScreenOf(wnew)->tc_query_code = -1;
6170d522f475Smrg#endif
6171d522f475Smrg    wnew->misc.re_verse0 = request->misc.re_verse;
6172d522f475Smrg    init_Bres(misc.re_verse);
6173d522f475Smrg    init_Ires(screen.multiClickTime);
6174d522f475Smrg    init_Ires(screen.bellSuppressTime);
6175d522f475Smrg    init_Sres(screen.charClass);
6176d522f475Smrg
6177d522f475Smrg    init_Bres(screen.always_highlight);
6178d522f475Smrg    init_Bres(screen.brokenSelections);
6179d522f475Smrg    init_Bres(screen.cutNewline);
6180d522f475Smrg    init_Bres(screen.cutToBeginningOfLine);
6181d522f475Smrg    init_Bres(screen.highlight_selection);
6182492d43a5Smrg    init_Bres(screen.show_wrap_marks);
6183d522f475Smrg    init_Bres(screen.i18nSelections);
6184d522f475Smrg    init_Bres(screen.keepSelection);
6185d522f475Smrg    init_Bres(screen.selectToClipboard);
6186d522f475Smrg    init_Bres(screen.trim_selection);
6187d522f475Smrg
618820d2c4d2Smrg    TScreenOf(wnew)->pointer_cursor = TScreenOf(request)->pointer_cursor;
6189d522f475Smrg    init_Ires(screen.pointer_mode);
6190d522f475Smrg
6191d522f475Smrg    init_Sres(screen.answer_back);
6192d522f475Smrg
6193d522f475Smrg    init_Sres(screen.printer_command);
6194d522f475Smrg    init_Bres(screen.printer_autoclose);
6195d522f475Smrg    init_Bres(screen.printer_extent);
6196d522f475Smrg    init_Bres(screen.printer_formfeed);
619720d2c4d2Smrg    init_Bres(screen.printer_newline);
6198d522f475Smrg    init_Ires(screen.printer_controlmode);
6199d522f475Smrg#if OPT_PRINT_COLORS
6200d522f475Smrg    init_Ires(screen.print_attributes);
6201d522f475Smrg#endif
6202d522f475Smrg
6203d522f475Smrg    init_Sres(screen.keyboard_dialect);
6204d522f475Smrg
6205d522f475Smrg    init_Bres(screen.input_eight_bits);
6206d522f475Smrg    init_Bres(screen.output_eight_bits);
6207d522f475Smrg    init_Bres(screen.control_eight_bits);
6208d522f475Smrg    init_Bres(screen.backarrow_key);
6209d522f475Smrg    init_Bres(screen.alt_is_not_meta);
6210d522f475Smrg    init_Bres(screen.alt_sends_esc);
6211d522f475Smrg    init_Bres(screen.meta_sends_esc);
6212d522f475Smrg
6213d522f475Smrg    init_Bres(screen.allowSendEvent0);
621420d2c4d2Smrg    init_Bres(screen.allowColorOp0);
62152eaa94a1Schristos    init_Bres(screen.allowFontOp0);
62162eaa94a1Schristos    init_Bres(screen.allowTcapOp0);
6217d522f475Smrg    init_Bres(screen.allowTitleOp0);
6218d522f475Smrg    init_Bres(screen.allowWindowOp0);
6219d522f475Smrg
622020d2c4d2Smrg#if OPT_SCROLL_LOCK
622120d2c4d2Smrg    init_Bres(screen.allowScrollLock0);
622220d2c4d2Smrg#endif
622320d2c4d2Smrg
622420d2c4d2Smrg    init_Sres(screen.disallowedColorOps);
622520d2c4d2Smrg
622620d2c4d2Smrg    set_flags_from_list(TScreenOf(wnew)->disallow_color_ops,
622720d2c4d2Smrg			TScreenOf(wnew)->disallowedColorOps,
622820d2c4d2Smrg			tblColorOps,
622920d2c4d2Smrg			ecLAST);
623020d2c4d2Smrg
623120d2c4d2Smrg    init_Sres(screen.disallowedFontOps);
623220d2c4d2Smrg
623320d2c4d2Smrg    set_flags_from_list(TScreenOf(wnew)->disallow_font_ops,
623420d2c4d2Smrg			TScreenOf(wnew)->disallowedFontOps,
623520d2c4d2Smrg			tblFontOps,
623620d2c4d2Smrg			efLAST);
623720d2c4d2Smrg
623820d2c4d2Smrg    init_Sres(screen.disallowedTcapOps);
623920d2c4d2Smrg
624020d2c4d2Smrg    set_flags_from_list(TScreenOf(wnew)->disallow_tcap_ops,
624120d2c4d2Smrg			TScreenOf(wnew)->disallowedTcapOps,
624220d2c4d2Smrg			tblTcapOps,
624320d2c4d2Smrg			etLAST);
624420d2c4d2Smrg
624520d2c4d2Smrg    init_Sres(screen.disallowedWinOps);
624620d2c4d2Smrg
624720d2c4d2Smrg    set_flags_from_list(TScreenOf(wnew)->disallow_win_ops,
624820d2c4d2Smrg			TScreenOf(wnew)->disallowedWinOps,
624920d2c4d2Smrg			tblWindowOps,
625020d2c4d2Smrg			ewLAST);
625120d2c4d2Smrg
6252956cc18dSsnj    init_Sres(screen.default_string);
6253956cc18dSsnj    init_Sres(screen.eightbit_select_types);
6254956cc18dSsnj#if OPT_WIDE_CHARS
6255956cc18dSsnj    init_Sres(screen.utf8_select_types);
6256956cc18dSsnj#endif
6257956cc18dSsnj
6258d522f475Smrg    /* make a copy so that editres cannot change the resource after startup */
625920d2c4d2Smrg    TScreenOf(wnew)->allowSendEvents = TScreenOf(wnew)->allowSendEvent0;
626020d2c4d2Smrg    TScreenOf(wnew)->allowColorOps = TScreenOf(wnew)->allowColorOp0;
626120d2c4d2Smrg    TScreenOf(wnew)->allowFontOps = TScreenOf(wnew)->allowFontOp0;
626220d2c4d2Smrg    TScreenOf(wnew)->allowTcapOps = TScreenOf(wnew)->allowTcapOp0;
626320d2c4d2Smrg    TScreenOf(wnew)->allowTitleOps = TScreenOf(wnew)->allowTitleOp0;
626420d2c4d2Smrg    TScreenOf(wnew)->allowWindowOps = TScreenOf(wnew)->allowWindowOp0;
626520d2c4d2Smrg
626620d2c4d2Smrg#if OPT_SCROLL_LOCK
626720d2c4d2Smrg    TScreenOf(wnew)->allowScrollLock = TScreenOf(wnew)->allowScrollLock0;
626820d2c4d2Smrg#endif
6269d522f475Smrg
6270d522f475Smrg    init_Bres(screen.quiet_grab);
6271d522f475Smrg
6272d522f475Smrg#ifndef NO_ACTIVE_ICON
6273492d43a5Smrg    init_Sres(screen.icon_fontname);
6274492d43a5Smrg    TScreenOf(wnew)->fnt_icon.fs = XLoadQueryFont(TScreenOf(wnew)->display,
6275492d43a5Smrg						  TScreenOf(wnew)->icon_fontname);
6276492d43a5Smrg    TRACE(("iconFont '%s' %sloaded successfully\n",
6277492d43a5Smrg	   TScreenOf(wnew)->icon_fontname,
6278492d43a5Smrg	   TScreenOf(wnew)->fnt_icon.fs ? "" : "NOT "));
6279d522f475Smrg    init_Bres(misc.active_icon);
6280d522f475Smrg    init_Ires(misc.icon_border_width);
6281d522f475Smrg    wnew->misc.icon_border_pixel = request->misc.icon_border_pixel;
6282d522f475Smrg#endif /* NO_ACTIVE_ICON */
6283492d43a5Smrg
6284492d43a5Smrg    init_Bres(misc.signalInhibit);
6285d522f475Smrg    init_Bres(misc.titeInhibit);
6286d522f475Smrg    init_Bres(misc.tiXtraScroll);
6287d522f475Smrg    init_Bres(misc.dynamicColors);
6288d522f475Smrg    for (i = fontMenu_font1; i <= fontMenu_lastBuiltin; i++) {
62892eaa94a1Schristos	init_Sres2(screen.MenuFontName, i);
6290d522f475Smrg    }
6291956cc18dSsnj    init_Ires(misc.fontWarnings);
629220d2c4d2Smrg#define DefaultFontNames TScreenOf(wnew)->menu_font_names[fontMenu_default]
6293492d43a5Smrg    init_Sres(misc.default_font.f_n);
6294492d43a5Smrg    init_Sres(misc.default_font.f_b);
6295492d43a5Smrg    DefaultFontNames[fNorm] = x_strdup(wnew->misc.default_font.f_n);
6296492d43a5Smrg    DefaultFontNames[fBold] = x_strdup(wnew->misc.default_font.f_b);
6297956cc18dSsnj#if OPT_WIDE_CHARS
6298492d43a5Smrg    init_Sres(misc.default_font.f_w);
6299492d43a5Smrg    init_Sres(misc.default_font.f_wb);
6300492d43a5Smrg    DefaultFontNames[fWide] = x_strdup(wnew->misc.default_font.f_w);
6301492d43a5Smrg    DefaultFontNames[fWBold] = x_strdup(wnew->misc.default_font.f_wb);
6302956cc18dSsnj#endif
630320d2c4d2Smrg    TScreenOf(wnew)->MenuFontName(fontMenu_fontescape) = NULL;
630420d2c4d2Smrg    TScreenOf(wnew)->MenuFontName(fontMenu_fontsel) = NULL;
6305d522f475Smrg
630620d2c4d2Smrg    TScreenOf(wnew)->menu_font_number = fontMenu_default;
6307d522f475Smrg    init_Sres(screen.initial_font);
630820d2c4d2Smrg    if (TScreenOf(wnew)->initial_font != 0) {
630920d2c4d2Smrg	int result = xtermGetFont(TScreenOf(wnew)->initial_font);
6310d522f475Smrg	if (result >= 0)
631120d2c4d2Smrg	    TScreenOf(wnew)->menu_font_number = result;
6312d522f475Smrg    }
6313d522f475Smrg#if OPT_BROKEN_OSC
6314d522f475Smrg    init_Bres(screen.brokenLinuxOSC);
6315d522f475Smrg#endif
6316d522f475Smrg
6317d522f475Smrg#if OPT_BROKEN_ST
6318d522f475Smrg    init_Bres(screen.brokenStringTerm);
6319d522f475Smrg#endif
6320d522f475Smrg
6321d522f475Smrg#if OPT_C1_PRINT
6322d522f475Smrg    init_Bres(screen.c1_printable);
6323d522f475Smrg#endif
6324d522f475Smrg
6325d522f475Smrg#if OPT_CLIP_BOLD
6326d522f475Smrg    init_Bres(screen.use_clipping);
6327d522f475Smrg#endif
6328d522f475Smrg
6329d522f475Smrg#if OPT_DEC_CHRSET
6330d522f475Smrg    init_Bres(screen.font_doublesize);
6331d522f475Smrg    init_Ires(screen.cache_doublesize);
633220d2c4d2Smrg    if (TScreenOf(wnew)->cache_doublesize > NUM_CHRSET)
633320d2c4d2Smrg	TScreenOf(wnew)->cache_doublesize = NUM_CHRSET;
633420d2c4d2Smrg    if (TScreenOf(wnew)->cache_doublesize == 0)
633520d2c4d2Smrg	TScreenOf(wnew)->font_doublesize = False;
6336d522f475Smrg    TRACE(("Doublesize%s enabled, up to %d fonts\n",
633720d2c4d2Smrg	   TScreenOf(wnew)->font_doublesize ? "" : " not",
633820d2c4d2Smrg	   TScreenOf(wnew)->cache_doublesize));
6339d522f475Smrg#endif
6340d522f475Smrg
6341d522f475Smrg#if OPT_ISO_COLORS
6342d522f475Smrg    init_Ires(screen.veryBoldColors);
6343d522f475Smrg    init_Bres(screen.boldColors);
6344d522f475Smrg    init_Bres(screen.colorAttrMode);
6345d522f475Smrg    init_Bres(screen.colorBDMode);
6346d522f475Smrg    init_Bres(screen.colorBLMode);
6347d522f475Smrg    init_Bres(screen.colorMode);
6348d522f475Smrg    init_Bres(screen.colorULMode);
6349d522f475Smrg    init_Bres(screen.italicULMode);
6350d522f475Smrg    init_Bres(screen.colorRVMode);
6351d522f475Smrg
635220d2c4d2Smrg#if OPT_COLOR_RES2
635320d2c4d2Smrg    TRACE(("...will fake resources for color%d to color%d\n",
635420d2c4d2Smrg	   MIN_ANSI_COLORS,
635520d2c4d2Smrg	   NUM_ANSI_COLORS - 1));
635620d2c4d2Smrg#endif
6357d522f475Smrg    for (i = 0, color_ok = False; i < MAXCOLORS; i++) {
6358d522f475Smrg
635920d2c4d2Smrg#if OPT_COLOR_RES2
6360d522f475Smrg	/*
6361d522f475Smrg	 * Xt has a hardcoded limit on the maximum number of resources that can
6362d522f475Smrg	 * be used in a widget.  If we configure both luit (which implies
6363d522f475Smrg	 * wide-characters) and 256-colors, it goes over that limit.  Most
6364d522f475Smrg	 * people would not need a resource-file with 256-colors; the default
6365d522f475Smrg	 * values in our table are sufficient.  In that case, fake the resource
6366d522f475Smrg	 * setting by copying the default value from the table.  The #define's
6367d522f475Smrg	 * can be overridden to make these true resources.
6368d522f475Smrg	 */
6369d522f475Smrg	if (i >= MIN_ANSI_COLORS && i < NUM_ANSI_COLORS) {
63706879286fSmrg	    TScreenOf(wnew)->Acolors[i].resource =
63716879286fSmrg		x_strtrim(fake_resources[i - MIN_ANSI_COLORS].default_addr);
637220d2c4d2Smrg	    if (TScreenOf(wnew)->Acolors[i].resource == 0)
637320d2c4d2Smrg		TScreenOf(wnew)->Acolors[i].resource = XtDefaultForeground;
6374d522f475Smrg	} else
6375d522f475Smrg#endif /* OPT_COLOR_RES2 */
63766879286fSmrg	{
637720d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i] = TScreenOf(request)->Acolors[i];
63786879286fSmrg	    TScreenOf(wnew)->Acolors[i].resource =
63796879286fSmrg		x_strtrim(TScreenOf(wnew)->Acolors[i].resource);
63806879286fSmrg	}
6381d522f475Smrg
6382d522f475Smrg#if OPT_COLOR_RES
638320d2c4d2Smrg	TRACE(("Acolors[%d] = %s\n", i, TScreenOf(wnew)->Acolors[i].resource));
638420d2c4d2Smrg	TScreenOf(wnew)->Acolors[i].mode = False;
6385d522f475Smrg	if (DftFg(Acolors[i])) {
638620d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i].value = T_COLOR(TScreenOf(wnew), TEXT_FG);
638720d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i].mode = True;
6388d522f475Smrg	} else if (DftBg(Acolors[i])) {
638920d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i].value = T_COLOR(TScreenOf(wnew), TEXT_BG);
639020d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i].mode = True;
6391d522f475Smrg	} else {
6392d522f475Smrg	    color_ok = True;
6393d522f475Smrg	}
6394d522f475Smrg#else
639520d2c4d2Smrg	TRACE(("Acolors[%d] = %#lx\n", i, TScreenOf(request)->Acolors[i]));
639620d2c4d2Smrg	if (TScreenOf(wnew)->Acolors[i] != wnew->dft_foreground &&
639720d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i] != T_COLOR(TScreenOf(wnew), TEXT_FG) &&
639820d2c4d2Smrg	    TScreenOf(wnew)->Acolors[i] != T_COLOR(TScreenOf(wnew), TEXT_BG))
6399d522f475Smrg	    color_ok = True;
6400d522f475Smrg#endif
6401d522f475Smrg    }
6402d522f475Smrg
6403d522f475Smrg    /*
6404d522f475Smrg     * Check if we're trying to use color in a monochrome screen.  Disable
6405d522f475Smrg     * color in that case, since that would make ANSI colors unusable.  A 4-bit
6406d522f475Smrg     * or 8-bit display is usable, so we do not have to check for anything more
6407d522f475Smrg     * specific.
6408d522f475Smrg     */
6409d522f475Smrg    if (color_ok) {
641020d2c4d2Smrg	Display *display = TScreenOf(wnew)->display;
6411d522f475Smrg	XVisualInfo myTemplate, *visInfoPtr;
6412d522f475Smrg	int numFound;
6413d522f475Smrg
6414d522f475Smrg	myTemplate.visualid = XVisualIDFromVisual(DefaultVisual(display,
6415d522f475Smrg								XDefaultScreen(display)));
6416d522f475Smrg	visInfoPtr = XGetVisualInfo(display, (long) VisualIDMask,
6417d522f475Smrg				    &myTemplate, &numFound);
6418d522f475Smrg	if (visInfoPtr == 0
6419d522f475Smrg	    || numFound == 0
6420d522f475Smrg	    || visInfoPtr->depth <= 1) {
6421d522f475Smrg	    TRACE(("disabling color since screen is monochrome\n"));
6422d522f475Smrg	    color_ok = False;
6423d522f475Smrg	} else {
6424d522f475Smrg	    XFree(visInfoPtr);
6425d522f475Smrg	}
6426d522f475Smrg    }
6427d522f475Smrg
6428d522f475Smrg    /* If none of the colors are anything other than the foreground or
6429d522f475Smrg     * background, we'll assume this isn't color, no matter what the colorMode
6430d522f475Smrg     * resource says.  (There doesn't seem to be any good way to determine if
6431d522f475Smrg     * the resource lookup failed versus the user having misconfigured this).
6432d522f475Smrg     */
6433d522f475Smrg    if (!color_ok) {
643420d2c4d2Smrg	TScreenOf(wnew)->colorMode = False;
6435d522f475Smrg	TRACE(("All colors are foreground or background: disable colorMode\n"));
6436d522f475Smrg    }
6437d522f475Smrg    wnew->sgr_foreground = -1;
6438d522f475Smrg    wnew->sgr_background = -1;
6439d522f475Smrg    wnew->sgr_extended = False;
6440d522f475Smrg#endif /* OPT_ISO_COLORS */
6441d522f475Smrg
6442d522f475Smrg    /*
6443d522f475Smrg     * Decode the resources that control the behavior on multiple mouse clicks.
6444d522f475Smrg     * A single click is always bound to normal character selection, but the
6445d522f475Smrg     * other flavors can be changed.
6446d522f475Smrg     */
6447d522f475Smrg    for (i = 0; i < NSELECTUNITS; ++i) {
6448d522f475Smrg	int ck = (i + 1);
644920d2c4d2Smrg	TScreenOf(wnew)->maxClicks = ck;
6450d522f475Smrg	if (i == Select_CHAR)
645120d2c4d2Smrg	    TScreenOf(wnew)->selectMap[i] = Select_CHAR;
645220d2c4d2Smrg	else if (TScreenOf(request)->onClick[i] != 0)
6453d522f475Smrg	    ParseOnClicks(wnew, request, (unsigned) i);
6454d522f475Smrg	else if (i <= Select_LINE)
645520d2c4d2Smrg	    TScreenOf(wnew)->selectMap[i] = (SelectUnit) i;
6456d522f475Smrg	else
6457d522f475Smrg	    break;
6458d522f475Smrg	TRACE(("on%dClicks %s=%d\n", ck,
645920d2c4d2Smrg	       NonNull(TScreenOf(request)->onClick[i]),
646020d2c4d2Smrg	       TScreenOf(wnew)->selectMap[i]));
646120d2c4d2Smrg	if (TScreenOf(wnew)->selectMap[i] == NSELECTUNITS)
6462d522f475Smrg	    break;
6463d522f475Smrg    }
646420d2c4d2Smrg    TRACE(("maxClicks %d\n", TScreenOf(wnew)->maxClicks));
6465d522f475Smrg
6466d522f475Smrg    init_Tres(MOUSE_FG);
6467d522f475Smrg    init_Tres(MOUSE_BG);
6468d522f475Smrg    init_Tres(TEXT_CURSOR);
6469d522f475Smrg#if OPT_HIGHLIGHT_COLOR
6470d522f475Smrg    init_Tres(HIGHLIGHT_BG);
6471d522f475Smrg    init_Tres(HIGHLIGHT_FG);
6472d522f475Smrg    init_Bres(screen.hilite_reverse);
6473d522f475Smrg    init_Bres(screen.hilite_color);
647420d2c4d2Smrg    if (TScreenOf(wnew)->hilite_color == Maybe) {
647520d2c4d2Smrg	TScreenOf(wnew)->hilite_color = False;
6476d522f475Smrg#if OPT_COLOR_RES
6477d522f475Smrg	/*
6478d522f475Smrg	 * If the highlight text/background are both set, and if they are
6479d522f475Smrg	 * not equal to either the text/background or background/text, then
6480d522f475Smrg	 * set the highlightColorMode automatically.
6481d522f475Smrg	 */
6482d522f475Smrg	if (!DftFg(Tcolors[HIGHLIGHT_BG])
6483d522f475Smrg	    && !DftBg(Tcolors[HIGHLIGHT_FG])
6484d522f475Smrg	    && !TxtFg(Tcolors[HIGHLIGHT_BG])
6485d522f475Smrg	    && !TxtBg(Tcolors[HIGHLIGHT_FG])
6486d522f475Smrg	    && !TxtBg(Tcolors[HIGHLIGHT_BG])
6487d522f475Smrg	    && !TxtFg(Tcolors[HIGHLIGHT_FG])) {
6488d522f475Smrg	    TRACE(("...setting hilite_color automatically\n"));
648920d2c4d2Smrg	    TScreenOf(wnew)->hilite_color = True;
6490d522f475Smrg	}
6491d522f475Smrg#endif
6492d522f475Smrg    }
6493d522f475Smrg#endif
6494d522f475Smrg
6495d522f475Smrg#if OPT_TEK4014
6496d522f475Smrg    /*
6497d522f475Smrg     * The Tek4014 window has no separate resources for foreground, background
6498d522f475Smrg     * and cursor color.  Since xterm always creates the vt100 widget first, we
6499d522f475Smrg     * can set the Tektronix colors here.  That lets us use escape sequences to
6500d522f475Smrg     * set its dynamic colors and get consistent behavior whether or not the
6501d522f475Smrg     * window is displayed.
6502d522f475Smrg     */
650320d2c4d2Smrg    TScreenOf(wnew)->Tcolors[TEK_BG] = TScreenOf(wnew)->Tcolors[TEXT_BG];
650420d2c4d2Smrg    TScreenOf(wnew)->Tcolors[TEK_FG] = TScreenOf(wnew)->Tcolors[TEXT_FG];
650520d2c4d2Smrg    TScreenOf(wnew)->Tcolors[TEK_CURSOR] = TScreenOf(wnew)->Tcolors[TEXT_CURSOR];
6506d522f475Smrg#endif
6507d522f475Smrg
6508492d43a5Smrg#ifdef SCROLLBAR_RIGHT
6509492d43a5Smrg    init_Bres(misc.useRight);
6510492d43a5Smrg#endif
6511492d43a5Smrg
6512d522f475Smrg#if OPT_RENDERFONT
6513d522f475Smrg    for (i = 0; i <= fontMenu_lastBuiltin; ++i) {
65142eaa94a1Schristos	init_Dres2(misc.face_size, i);
6515d522f475Smrg    }
6516d522f475Smrg    init_Sres(misc.face_name);
6517d522f475Smrg    init_Sres(misc.face_wide_name);
651820d2c4d2Smrg    init_Sres(misc.render_font_s);
651920d2c4d2Smrg    wnew->misc.render_font =
652020d2c4d2Smrg	(Boolean) extendedBoolean(wnew->misc.render_font_s,
652120d2c4d2Smrg				  tblRenderFont, erLast);
652220d2c4d2Smrg    if (wnew->misc.render_font == erDefault) {
652320d2c4d2Smrg	if (IsEmpty(wnew->misc.face_name)) {
652420d2c4d2Smrg	    wnew->misc.face_name = x_strdup(DEFFACENAME_AUTO);
652520d2c4d2Smrg	    TRACE(("will allow runtime switch to render_font using \"%s\"\n",
652620d2c4d2Smrg		   wnew->misc.face_name));
652720d2c4d2Smrg	} else {
652820d2c4d2Smrg	    wnew->misc.render_font = erTrue;
652920d2c4d2Smrg	    TRACE(("initially using TrueType font\n"));
653020d2c4d2Smrg	}
653120d2c4d2Smrg    }
6532d522f475Smrg    /* minor tweak to make debug traces consistent: */
6533d522f475Smrg    if (wnew->misc.render_font) {
653420d2c4d2Smrg	if (IsEmpty(wnew->misc.face_name)) {
6535d522f475Smrg	    wnew->misc.render_font = False;
6536d522f475Smrg	    TRACE(("reset render_font since there is no face_name\n"));
6537d522f475Smrg	}
6538d522f475Smrg    }
6539d522f475Smrg#endif
6540d522f475Smrg
654120d2c4d2Smrg#if OPT_WIDE_CHARS
654220d2c4d2Smrg    VTInitialize_locale(request);
654320d2c4d2Smrg    init_Bres(screen.utf8_latin1);
654420d2c4d2Smrg    init_Bres(screen.utf8_title);
654520d2c4d2Smrg
654620d2c4d2Smrg#if OPT_LUIT_PROG
654720d2c4d2Smrg    init_Bres(misc.callfilter);
654820d2c4d2Smrg    init_Bres(misc.use_encoding);
654920d2c4d2Smrg    init_Sres(misc.locale_str);
655020d2c4d2Smrg    init_Sres(misc.localefilter);
655120d2c4d2Smrg#endif
655220d2c4d2Smrg
6553d522f475Smrg    init_Ires(screen.utf8_inparse);
6554d522f475Smrg    init_Ires(screen.utf8_mode);
6555d522f475Smrg    init_Ires(screen.max_combining);
6556d522f475Smrg
655720d2c4d2Smrg    if (TScreenOf(wnew)->max_combining < 0) {
655820d2c4d2Smrg	TScreenOf(wnew)->max_combining = 0;
6559d522f475Smrg    }
656020d2c4d2Smrg    if (TScreenOf(wnew)->max_combining > 5) {
656120d2c4d2Smrg	TScreenOf(wnew)->max_combining = 5;
6562d522f475Smrg    }
6563d522f475Smrg
6564d522f475Smrg    init_Bres(screen.vt100_graphics);
6565d522f475Smrg    init_Bres(screen.wide_chars);
6566d522f475Smrg    init_Bres(misc.mk_width);
6567d522f475Smrg    init_Bres(misc.cjk_width);
6568d522f475Smrg
6569d522f475Smrg    init_Ires(misc.mk_samplesize);
6570d522f475Smrg    init_Ires(misc.mk_samplepass);
6571d522f475Smrg
6572d522f475Smrg    if (wnew->misc.mk_samplesize > 0xffff)
6573d522f475Smrg	wnew->misc.mk_samplesize = 0xffff;
6574d522f475Smrg    if (wnew->misc.mk_samplesize < 0)
6575d522f475Smrg	wnew->misc.mk_samplesize = 0;
6576d522f475Smrg
6577d522f475Smrg    if (wnew->misc.mk_samplepass > wnew->misc.mk_samplesize)
6578d522f475Smrg	wnew->misc.mk_samplepass = wnew->misc.mk_samplesize;
6579d522f475Smrg    if (wnew->misc.mk_samplepass < 0)
6580d522f475Smrg	wnew->misc.mk_samplepass = 0;
6581d522f475Smrg
658220d2c4d2Smrg    if (TScreenOf(request)->utf8_mode) {
6583d522f475Smrg	TRACE(("setting wide_chars on\n"));
658420d2c4d2Smrg	TScreenOf(wnew)->wide_chars = True;
6585d522f475Smrg    } else {
6586d522f475Smrg	TRACE(("setting utf8_mode to 0\n"));
658720d2c4d2Smrg	TScreenOf(wnew)->utf8_mode = uFalse;
6588d522f475Smrg    }
658920d2c4d2Smrg    TRACE(("initialized UTF-8 mode to %d\n", TScreenOf(wnew)->utf8_mode));
6590d522f475Smrg
6591d522f475Smrg#if OPT_MINI_LUIT
659220d2c4d2Smrg    if (TScreenOf(request)->latin9_mode) {
659320d2c4d2Smrg	TScreenOf(wnew)->latin9_mode = True;
6594d522f475Smrg    }
659520d2c4d2Smrg    if (TScreenOf(request)->unicode_font) {
659620d2c4d2Smrg	TScreenOf(wnew)->unicode_font = True;
6597d522f475Smrg    }
659820d2c4d2Smrg    TRACE(("initialized Latin9 mode to %d\n", TScreenOf(wnew)->latin9_mode));
659920d2c4d2Smrg    TRACE(("initialized unicode_font to %d\n", TScreenOf(wnew)->unicode_font));
6600d522f475Smrg#endif
6601d522f475Smrg
660220d2c4d2Smrg    decode_wcwidth(wnew);
6603d522f475Smrg#endif /* OPT_WIDE_CHARS */
6604d522f475Smrg
6605d522f475Smrg    init_Bres(screen.always_bold_mode);
6606d522f475Smrg    init_Bres(screen.bold_mode);
6607d522f475Smrg    init_Bres(screen.underline);
6608d522f475Smrg
6609d522f475Smrg    wnew->cur_foreground = 0;
6610d522f475Smrg    wnew->cur_background = 0;
6611d522f475Smrg
6612d522f475Smrg    wnew->keyboard.flags = MODE_SRM;
661320d2c4d2Smrg    if (TScreenOf(wnew)->backarrow_key)
6614d522f475Smrg	wnew->keyboard.flags |= MODE_DECBKM;
6615d522f475Smrg    TRACE(("initialized DECBKM %s\n",
6616d522f475Smrg	   BtoS(wnew->keyboard.flags & MODE_DECBKM)));
6617d522f475Smrg
6618d522f475Smrg    /* look for focus related events on the shell, because we need
6619d522f475Smrg     * to care about the shell's border being part of our focus.
6620d522f475Smrg     */
662120d2c4d2Smrg    TRACE(("adding event handlers for my_parent %p\n", (void *) my_parent));
6622d522f475Smrg    XtAddEventHandler(my_parent, EnterWindowMask, False,
6623d522f475Smrg		      HandleEnterWindow, (Opaque) NULL);
6624d522f475Smrg    XtAddEventHandler(my_parent, LeaveWindowMask, False,
6625d522f475Smrg		      HandleLeaveWindow, (Opaque) NULL);
6626d522f475Smrg    XtAddEventHandler(my_parent, FocusChangeMask, False,
6627d522f475Smrg		      HandleFocusChange, (Opaque) NULL);
6628d522f475Smrg    XtAddEventHandler((Widget) wnew, 0L, True,
6629d522f475Smrg		      VTNonMaskableEvent, (Opaque) NULL);
6630d522f475Smrg    XtAddEventHandler((Widget) wnew, PropertyChangeMask, False,
6631d522f475Smrg		      HandleBellPropertyChange, (Opaque) NULL);
6632d522f475Smrg
6633d522f475Smrg#if HANDLE_STRUCT_NOTIFY
6634d522f475Smrg#if OPT_TOOLBAR
6635d522f475Smrg    wnew->VT100_TB_INFO(menu_bar) = request->VT100_TB_INFO(menu_bar);
6636d522f475Smrg    init_Ires(VT100_TB_INFO(menu_height));
6637d522f475Smrg#else
6638d522f475Smrg    /* Flag icon name with "***"  on window output when iconified.
6639d522f475Smrg     * Put in a handler that will tell us when we get Map/Unmap events.
6640d522f475Smrg     */
6641d522f475Smrg    if (resource.zIconBeep)
6642d522f475Smrg#endif
6643d522f475Smrg	XtAddEventHandler(my_parent, StructureNotifyMask, False,
6644d522f475Smrg			  HandleStructNotify, (Opaque) 0);
6645d522f475Smrg#endif /* HANDLE_STRUCT_NOTIFY */
6646d522f475Smrg
664720d2c4d2Smrg    TScreenOf(wnew)->bellInProgress = False;
6648d522f475Smrg
664920d2c4d2Smrg    set_character_class(TScreenOf(wnew)->charClass);
6650d522f475Smrg
6651d522f475Smrg    /* create it, but don't realize it */
6652956cc18dSsnj    ScrollBarOn(wnew, True);
6653d522f475Smrg
6654d522f475Smrg    /* make sure that the resize gravity acceptable */
6655956cc18dSsnj    if (!GravityIsNorthWest(wnew) &&
6656956cc18dSsnj	!GravityIsSouthWest(wnew)) {
6657d522f475Smrg	char value[80];
665820d2c4d2Smrg	String temp[2];
6659d522f475Smrg	Cardinal nparams = 1;
6660d522f475Smrg
666120d2c4d2Smrg	sprintf(value, "%d", wnew->misc.resizeGravity);
666220d2c4d2Smrg	temp[0] = value;
6663d522f475Smrg	temp[1] = 0;
6664d522f475Smrg	XtAppWarningMsg(app_con, "rangeError", "resizeGravity", "XTermError",
6665d522f475Smrg			"unsupported resizeGravity resource value (%s)",
6666d522f475Smrg			temp, &nparams);
6667d522f475Smrg	wnew->misc.resizeGravity = SouthWestGravity;
6668d522f475Smrg    }
6669d522f475Smrg#ifndef NO_ACTIVE_ICON
667020d2c4d2Smrg    TScreenOf(wnew)->whichVwin = &TScreenOf(wnew)->fullVwin;
6671d522f475Smrg#endif /* NO_ACTIVE_ICON */
6672d522f475Smrg
667320d2c4d2Smrg    if (TScreenOf(wnew)->savelines < 0)
667420d2c4d2Smrg	TScreenOf(wnew)->savelines = 0;
6675d522f475Smrg
6676d522f475Smrg    init_Bres(screen.awaitInput);
6677d522f475Smrg
6678d522f475Smrg    wnew->flags = 0;
667920d2c4d2Smrg    if (!TScreenOf(wnew)->jumpscroll)
6680d522f475Smrg	wnew->flags |= SMOOTHSCROLL;
6681d522f475Smrg    if (wnew->misc.reverseWrap)
6682d522f475Smrg	wnew->flags |= REVERSEWRAP;
6683d522f475Smrg    if (wnew->misc.autoWrap)
6684d522f475Smrg	wnew->flags |= WRAPAROUND;
6685d522f475Smrg    if (wnew->misc.re_verse != wnew->misc.re_verse0)
6686d522f475Smrg	wnew->flags |= REVERSE_VIDEO;
668720d2c4d2Smrg    if (TScreenOf(wnew)->c132)
6688d522f475Smrg	wnew->flags |= IN132COLUMNS;
6689d522f475Smrg
6690d522f475Smrg    wnew->initflags = wnew->flags;
6691d522f475Smrg
6692d522f475Smrg#if OPT_MOD_FKEYS
6693d522f475Smrg    init_Ires(keyboard.modify_1st.cursor_keys);
6694d522f475Smrg    init_Ires(keyboard.modify_1st.function_keys);
6695d522f475Smrg    init_Ires(keyboard.modify_1st.keypad_keys);
6696d522f475Smrg    init_Ires(keyboard.modify_1st.other_keys);
6697d522f475Smrg    init_Ires(keyboard.modify_1st.string_keys);
6698d522f475Smrg    init_Ires(keyboard.format_keys);
6699d522f475Smrg    wnew->keyboard.modify_now = wnew->keyboard.modify_1st;
6700d522f475Smrg#endif
6701d522f475Smrg
6702d522f475Smrg    init_Ires(misc.appcursorDefault);
6703d522f475Smrg    if (wnew->misc.appcursorDefault)
6704d522f475Smrg	wnew->keyboard.flags |= MODE_DECCKM;
6705d522f475Smrg
6706d522f475Smrg    init_Ires(misc.appkeypadDefault);
6707d522f475Smrg    if (wnew->misc.appkeypadDefault)
6708d522f475Smrg	wnew->keyboard.flags |= MODE_DECKPAM;
6709d522f475Smrg
6710956cc18dSsnj    initLineData(wnew);
6711d522f475Smrg    return;
6712d522f475Smrg}
6713d522f475Smrg
6714d522f475Smrgvoid
6715d522f475SmrgreleaseCursorGCs(XtermWidget xw)
6716d522f475Smrg{
671720d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
6718d522f475Smrg    VTwin *win = WhichVWin(screen);
6719d522f475Smrg    int n;
6720d522f475Smrg
6721d522f475Smrg    for_each_curs_gc(n) {
6722d522f475Smrg	freeCgs(xw, win, (CgsEnum) n);
6723d522f475Smrg    }
6724d522f475Smrg}
6725d522f475Smrg
6726d522f475Smrgvoid
6727d522f475SmrgreleaseWindowGCs(XtermWidget xw, VTwin * win)
6728d522f475Smrg{
6729d522f475Smrg    int n;
6730d522f475Smrg
6731d522f475Smrg    for_each_text_gc(n) {
6732d522f475Smrg	freeCgs(xw, win, (CgsEnum) n);
6733d522f475Smrg    }
6734d522f475Smrg}
6735d522f475Smrg
6736d522f475Smrg#define TRACE_FREE_LEAK(name) \
6737d522f475Smrg	if (name) { \
673820d2c4d2Smrg	    free((void *) name); \
6739d522f475Smrg	    name = 0; \
6740d522f475Smrg	    TRACE(("freed " #name "\n")); \
6741d522f475Smrg	}
6742d522f475Smrg
6743d522f475Smrg#define FREE_LEAK(name) \
6744d522f475Smrg	if (name) { \
674520d2c4d2Smrg	    free((void *) name); \
6746d522f475Smrg	    name = 0; \
6747d522f475Smrg	}
6748d522f475Smrg
6749956cc18dSsnj#if OPT_INPUT_METHOD
6750956cc18dSsnjstatic void
6751956cc18dSsnjcleanupInputMethod(TScreen * screen)
6752956cc18dSsnj{
6753956cc18dSsnj    if (screen->xim) {
6754956cc18dSsnj	XCloseIM(screen->xim);
6755956cc18dSsnj	screen->xim = 0;
6756956cc18dSsnj	TRACE(("freed screen->xim\n"));
6757956cc18dSsnj    }
6758956cc18dSsnj}
6759956cc18dSsnj#endif
6760956cc18dSsnj
6761d522f475Smrgstatic void
6762d522f475SmrgVTDestroy(Widget w GCC_UNUSED)
6763d522f475Smrg{
6764d522f475Smrg#ifdef NO_LEAKS
6765d522f475Smrg    XtermWidget xw = (XtermWidget) w;
676620d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
6767d522f475Smrg    Cardinal n;
6768d522f475Smrg
6769d522f475Smrg    StopBlinking(screen);
6770d522f475Smrg
67712eaa94a1Schristos    if (screen->scrollWidget) {
67722eaa94a1Schristos	XtUninstallTranslations(screen->scrollWidget);
6773d522f475Smrg	XtDestroyWidget(screen->scrollWidget);
67742eaa94a1Schristos    }
6775956cc18dSsnj#if OPT_FIFO_LINES
6776956cc18dSsnj    while (screen->saved_fifo-- > 0) {
6777956cc18dSsnj	deleteScrollback(screen, 0);
6778956cc18dSsnj    }
6779956cc18dSsnj#endif
678020d2c4d2Smrg    while (screen->save_title != 0) {
678120d2c4d2Smrg	SaveTitle *last = screen->save_title;
678220d2c4d2Smrg	screen->save_title = last->next;
678320d2c4d2Smrg	free(last->iconName);
678420d2c4d2Smrg	free(last->windowName);
678520d2c4d2Smrg	free(last);
678620d2c4d2Smrg    }
6787d522f475Smrg    TRACE_FREE_LEAK(screen->save_ptr);
6788956cc18dSsnj    TRACE_FREE_LEAK(screen->saveBuf_data);
6789956cc18dSsnj    TRACE_FREE_LEAK(screen->saveBuf_index);
6790956cc18dSsnj    for (n = 0; n < 2; ++n) {
6791956cc18dSsnj	TRACE_FREE_LEAK(screen->editBuf_data[n]);
6792956cc18dSsnj	TRACE_FREE_LEAK(screen->editBuf_index[n]);
6793956cc18dSsnj    }
6794d522f475Smrg    TRACE_FREE_LEAK(screen->keyboard_dialect);
6795d522f475Smrg    TRACE_FREE_LEAK(screen->term_id);
6796d522f475Smrg#if OPT_WIDE_CHARS
6797d522f475Smrg#if OPT_LUIT_PROG
6798d522f475Smrg    TRACE_FREE_LEAK(xw->misc.locale_str);
6799d522f475Smrg    TRACE_FREE_LEAK(xw->misc.localefilter);
6800d522f475Smrg#endif
6801d522f475Smrg#endif
6802d522f475Smrg#if OPT_INPUT_METHOD
6803956cc18dSsnj    cleanupInputMethod(screen);
6804d522f475Smrg#endif
6805d522f475Smrg    releaseCursorGCs(xw);
6806d522f475Smrg    releaseWindowGCs(xw, &(screen->fullVwin));
6807d522f475Smrg#ifndef NO_ACTIVE_ICON
6808d522f475Smrg    releaseWindowGCs(xw, &(screen->iconVwin));
6809d522f475Smrg#endif
68102eaa94a1Schristos    XtUninstallTranslations((Widget) xw);
68112eaa94a1Schristos#if OPT_TOOLBAR
68122eaa94a1Schristos    XtUninstallTranslations((Widget) XtParent(xw));
68132eaa94a1Schristos#endif
68142eaa94a1Schristos    XtUninstallTranslations((Widget) SHELL_OF(xw));
6815d522f475Smrg
6816d522f475Smrg    if (screen->hidden_cursor)
6817d522f475Smrg	XFreeCursor(screen->display, screen->hidden_cursor);
6818d522f475Smrg
6819d522f475Smrg    xtermCloseFonts(xw, screen->fnts);
6820d522f475Smrg    noleaks_cachedCgs(xw);
6821d522f475Smrg
6822d522f475Smrg#if OPT_RENDERFONT
6823d522f475Smrg    for (n = 0; n < NMENUFONTS; ++n) {
6824d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontNorm[n]));
6825d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontBold[n]));
6826d522f475Smrg	xtermCloseXft(screen, &(screen->renderFontItal[n]));
6827956cc18dSsnj#if OPT_RENDERWIDE
6828d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideNorm[n]));
6829d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideBold[n]));
6830d522f475Smrg	xtermCloseXft(screen, &(screen->renderWideItal[n]));
6831956cc18dSsnj#endif
6832d522f475Smrg    }
6833d522f475Smrg#endif
6834d522f475Smrg
683520d2c4d2Smrg    /* free things allocated via init_Sres or Init_Sres2 */
683620d2c4d2Smrg#ifdef ALLOWLOGGING
683720d2c4d2Smrg    TRACE_FREE_LEAK(screen->logfile);
6838d522f475Smrg#endif
683920d2c4d2Smrg    TRACE_FREE_LEAK(screen->term_id);
684020d2c4d2Smrg    TRACE_FREE_LEAK(screen->charClass);
684120d2c4d2Smrg    TRACE_FREE_LEAK(screen->answer_back);
684220d2c4d2Smrg    TRACE_FREE_LEAK(screen->printer_command);
684320d2c4d2Smrg    TRACE_FREE_LEAK(screen->keyboard_dialect);
684420d2c4d2Smrg    TRACE_FREE_LEAK(screen->disallowedColorOps);
684520d2c4d2Smrg    TRACE_FREE_LEAK(screen->disallowedFontOps);
684620d2c4d2Smrg    TRACE_FREE_LEAK(screen->disallowedTcapOps);
684720d2c4d2Smrg    TRACE_FREE_LEAK(screen->disallowedWinOps);
684820d2c4d2Smrg    TRACE_FREE_LEAK(screen->default_string);
684920d2c4d2Smrg    TRACE_FREE_LEAK(screen->eightbit_select_types);
685020d2c4d2Smrg#if OPT_WIDE_CHARS
685120d2c4d2Smrg    TRACE_FREE_LEAK(screen->utf8_select_types);
685220d2c4d2Smrg#endif
685320d2c4d2Smrg#if 0
685420d2c4d2Smrg    for (n = fontMenu_font1; n <= fontMenu_lastBuiltin; n++) {
685520d2c4d2Smrg	TRACE_FREE_LEAK(screen->MenuFontName(n));
6856d522f475Smrg    }
6857d522f475Smrg#endif
685820d2c4d2Smrg    TRACE_FREE_LEAK(screen->initial_font);
685920d2c4d2Smrg#if OPT_LUIT_PROG
686020d2c4d2Smrg    TRACE_FREE_LEAK(xw->misc.locale_str);
686120d2c4d2Smrg    TRACE_FREE_LEAK(xw->misc.localefilter);
686220d2c4d2Smrg#endif
686320d2c4d2Smrg#if OPT_RENDERFONT
686420d2c4d2Smrg    TRACE_FREE_LEAK(xw->misc.face_name);
686520d2c4d2Smrg    TRACE_FREE_LEAK(xw->misc.face_wide_name);
686620d2c4d2Smrg#endif
686720d2c4d2Smrg
6868d522f475Smrg#if OPT_SELECT_REGEX
6869d522f475Smrg    for (n = 0; n < NSELECTUNITS; ++n) {
6870d522f475Smrg	FREE_LEAK(screen->selectExpr[n]);
6871d522f475Smrg    }
6872d522f475Smrg#endif
6873d522f475Smrg
6874d522f475Smrg    if (screen->selection_atoms)
6875d522f475Smrg	XtFree((char *) (screen->selection_atoms));
6876d522f475Smrg
6877d522f475Smrg    XtFree((char *) (screen->selection_data));
6878d522f475Smrg
6879d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.extra_translations);
6880d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.shell_translations);
6881d522f475Smrg    TRACE_FREE_LEAK(xw->keyboard.xterm_translations);
6882956cc18dSsnj
6883956cc18dSsnj#if OPT_WIDE_CHARS
6884956cc18dSsnj    FreeTypedBuffer(XChar2b);
6885956cc18dSsnj    FreeTypedBuffer(char);
6886956cc18dSsnj#endif
6887956cc18dSsnj#if OPT_RENDERFONT
6888956cc18dSsnj#if OPT_RENDERWIDE
6889956cc18dSsnj    FreeTypedBuffer(XftCharSpec);
6890956cc18dSsnj#else
6891956cc18dSsnj    FreeTypedBuffer(XftChar8);
6892956cc18dSsnj#endif
6893956cc18dSsnj#endif
6894956cc18dSsnj
6895956cc18dSsnj    TRACE_FREE_LEAK(myState.print_area);
6896956cc18dSsnj    TRACE_FREE_LEAK(myState.string_area);
6897956cc18dSsnj    memset(&myState, 0, sizeof(myState));
6898956cc18dSsnj
6899d522f475Smrg#endif /* defined(NO_LEAKS) */
6900d522f475Smrg}
6901d522f475Smrg
6902d522f475Smrg/*ARGSUSED*/
6903d522f475Smrgstatic void
6904d522f475SmrgVTRealize(Widget w,
6905d522f475Smrg	  XtValueMask * valuemask,
6906d522f475Smrg	  XSetWindowAttributes * values)
6907d522f475Smrg{
6908d522f475Smrg    XtermWidget xw = (XtermWidget) w;
690920d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
6910d522f475Smrg
6911d522f475Smrg    const VTFontNames *myfont;
6912d522f475Smrg    unsigned width, height;
6913d522f475Smrg    int xpos, ypos, pr;
6914d522f475Smrg    Atom pid_atom;
6915d522f475Smrg    int i;
6916d522f475Smrg
6917d522f475Smrg    TRACE(("VTRealize\n"));
6918d522f475Smrg
6919d522f475Smrg    TabReset(xw->tabs);
6920d522f475Smrg
6921d522f475Smrg    if (screen->menu_font_number == fontMenu_default) {
6922d522f475Smrg	myfont = &(xw->misc.default_font);
6923d522f475Smrg    } else {
6924d522f475Smrg	myfont = xtermFontName(screen->MenuFontName(screen->menu_font_number));
6925d522f475Smrg    }
6926d522f475Smrg    memset(screen->fnts, 0, sizeof(screen->fnts));
6927d522f475Smrg
6928d522f475Smrg    if (!xtermLoadFont(xw,
6929d522f475Smrg		       myfont,
6930d522f475Smrg		       False,
6931d522f475Smrg		       screen->menu_font_number)) {
6932d522f475Smrg	if (XmuCompareISOLatin1(myfont->f_n, DEFFONT) != 0) {
693320d2c4d2Smrg	    char *use_font = x_strdup(DEFFONT);
6934d522f475Smrg	    fprintf(stderr,
6935d522f475Smrg		    "%s:  unable to open font \"%s\", trying \"%s\"....\n",
6936a1f3da82Smrg		    ProgramName, myfont->f_n, use_font);
6937d522f475Smrg	    (void) xtermLoadFont(xw,
693820d2c4d2Smrg				 xtermFontName(use_font),
6939d522f475Smrg				 False,
6940d522f475Smrg				 screen->menu_font_number);
694120d2c4d2Smrg	    screen->MenuFontName(screen->menu_font_number) = use_font;
6942d522f475Smrg	}
6943d522f475Smrg    }
6944d522f475Smrg
6945d522f475Smrg    /* really screwed if we couldn't open default font */
6946d522f475Smrg    if (!screen->fnts[fNorm].fs) {
6947d522f475Smrg	fprintf(stderr, "%s:  unable to locate a suitable font\n",
6948a1f3da82Smrg		ProgramName);
6949d522f475Smrg	Exit(1);
6950d522f475Smrg    }
6951d522f475Smrg#if OPT_WIDE_CHARS
695220d2c4d2Smrg    if (TScreenOf(xw)->utf8_mode) {
6953d522f475Smrg	TRACE(("check if this is a wide font, if not try again\n"));
6954d522f475Smrg	if (xtermLoadWideFonts(xw, False))
6955d522f475Smrg	    SetVTFont(xw, screen->menu_font_number, True, NULL);
6956d522f475Smrg    }
6957d522f475Smrg#endif
6958d522f475Smrg
6959d522f475Smrg    /* making cursor */
6960d522f475Smrg    if (!screen->pointer_cursor) {
6961d522f475Smrg	screen->pointer_cursor =
6962d522f475Smrg	    make_colored_cursor(XC_xterm,
6963d522f475Smrg				T_COLOR(screen, MOUSE_FG),
6964d522f475Smrg				T_COLOR(screen, MOUSE_BG));
6965d522f475Smrg    } else {
6966d522f475Smrg	recolor_cursor(screen,
6967d522f475Smrg		       screen->pointer_cursor,
6968d522f475Smrg		       T_COLOR(screen, MOUSE_FG),
6969d522f475Smrg		       T_COLOR(screen, MOUSE_BG));
6970d522f475Smrg    }
6971d522f475Smrg
6972d522f475Smrg    /* set defaults */
6973d522f475Smrg    xpos = 1;
6974d522f475Smrg    ypos = 1;
6975d522f475Smrg    width = 80;
6976d522f475Smrg    height = 24;
6977d522f475Smrg
6978d522f475Smrg    TRACE(("parsing geo_metry %s\n", NonNull(xw->misc.geo_metry)));
6979d522f475Smrg    pr = XParseGeometry(xw->misc.geo_metry, &xpos, &ypos,
6980d522f475Smrg			&width, &height);
6981d522f475Smrg    TRACE(("... position %d,%d size %dx%d\n", ypos, xpos, height, width));
6982d522f475Smrg
6983d522f475Smrg    set_max_col(screen, (int) (width - 1));	/* units in character cells */
6984d522f475Smrg    set_max_row(screen, (int) (height - 1));	/* units in character cells */
6985d522f475Smrg    xtermUpdateFontInfo(xw, False);
6986d522f475Smrg
6987d522f475Smrg    width = screen->fullVwin.fullwidth;
6988d522f475Smrg    height = screen->fullVwin.fullheight;
6989d522f475Smrg
6990d522f475Smrg    TRACE(("... border widget %d parent %d shell %d\n",
6991d522f475Smrg	   BorderWidth(xw),
6992d522f475Smrg	   BorderWidth(XtParent(xw)),
6993d522f475Smrg	   BorderWidth(SHELL_OF(xw))));
6994d522f475Smrg
6995d522f475Smrg    if ((pr & XValue) && (XNegative & pr)) {
69962eaa94a1Schristos	xpos += (DisplayWidth(screen->display, DefaultScreen(screen->display))
69972eaa94a1Schristos		 - (int) width
69982eaa94a1Schristos		 - (BorderWidth(XtParent(xw)) * 2));
6999d522f475Smrg    }
7000d522f475Smrg    if ((pr & YValue) && (YNegative & pr)) {
70012eaa94a1Schristos	ypos += (DisplayHeight(screen->display, DefaultScreen(screen->display))
70022eaa94a1Schristos		 - (int) height
70032eaa94a1Schristos		 - (BorderWidth(XtParent(xw)) * 2));
7004d522f475Smrg    }
7005d522f475Smrg
7006d522f475Smrg    /* set up size hints for window manager; min 1 char by 1 char */
7007d522f475Smrg    getXtermSizeHints(xw);
7008d522f475Smrg    xtermSizeHints(xw, (xw->misc.scrollbar
7009d522f475Smrg			? (screen->scrollWidget->core.width
7010d522f475Smrg			   + BorderWidth(screen->scrollWidget))
7011d522f475Smrg			: 0));
7012d522f475Smrg
7013d522f475Smrg    xw->hints.x = xpos;
7014d522f475Smrg    xw->hints.y = ypos;
7015a1f3da82Smrg#if OPT_MAXIMIZE
7016a1f3da82Smrg    /* assure single-increment resize for fullscreen */
7017a1f3da82Smrg    if (term->screen.fullscreen) {
7018a1f3da82Smrg	xw->hints.width_inc = 1;
7019a1f3da82Smrg	xw->hints.height_inc = 1;
7020a1f3da82Smrg    }
7021a1f3da82Smrg#endif
7022d522f475Smrg    if ((XValue & pr) || (YValue & pr)) {
7023d522f475Smrg	xw->hints.flags |= USSize | USPosition;
7024d522f475Smrg	xw->hints.flags |= PWinGravity;
7025d522f475Smrg	switch (pr & (XNegative | YNegative)) {
7026d522f475Smrg	case 0:
7027d522f475Smrg	    xw->hints.win_gravity = NorthWestGravity;
7028d522f475Smrg	    break;
7029d522f475Smrg	case XNegative:
7030d522f475Smrg	    xw->hints.win_gravity = NorthEastGravity;
7031d522f475Smrg	    break;
7032d522f475Smrg	case YNegative:
7033d522f475Smrg	    xw->hints.win_gravity = SouthWestGravity;
7034d522f475Smrg	    break;
7035d522f475Smrg	default:
7036d522f475Smrg	    xw->hints.win_gravity = SouthEastGravity;
7037d522f475Smrg	    break;
7038d522f475Smrg	}
7039d522f475Smrg    } else {
7040d522f475Smrg	/* set a default size, but do *not* set position */
7041d522f475Smrg	xw->hints.flags |= PSize;
7042d522f475Smrg    }
7043d522f475Smrg    xw->hints.height = xw->hints.base_height
7044d522f475Smrg	+ xw->hints.height_inc * MaxRows(screen);
7045d522f475Smrg    xw->hints.width = xw->hints.base_width
7046d522f475Smrg	+ xw->hints.width_inc * MaxCols(screen);
7047d522f475Smrg
7048d522f475Smrg    if ((WidthValue & pr) || (HeightValue & pr))
7049d522f475Smrg	xw->hints.flags |= USSize;
7050d522f475Smrg    else
7051d522f475Smrg	xw->hints.flags |= PSize;
7052d522f475Smrg
7053d522f475Smrg    /*
7054d522f475Smrg     * Note that the size-hints are for the shell, while the resize-request
7055d522f475Smrg     * is for the vt100 widget.  They are not the same size.
7056d522f475Smrg     */
70572eaa94a1Schristos    (void) REQ_RESIZE((Widget) xw,
70582eaa94a1Schristos		      (Dimension) width, (Dimension) height,
70592eaa94a1Schristos		      &xw->core.width, &xw->core.height);
7060d522f475Smrg
7061d522f475Smrg    /* XXX This is bogus.  We are parsing geometries too late.  This
7062d522f475Smrg     * is information that the shell widget ought to have before we get
7063d522f475Smrg     * realized, so that it can do the right thing.
7064d522f475Smrg     */
7065d522f475Smrg    if (xw->hints.flags & USPosition)
7066a1f3da82Smrg	XMoveWindow(XtDisplay(xw), VShellWindow(xw),
7067d522f475Smrg		    xw->hints.x, xw->hints.y);
7068d522f475Smrg
7069d522f475Smrg    TRACE(("%s@%d -- ", __FILE__, __LINE__));
7070d522f475Smrg    TRACE_HINTS(&xw->hints);
7071a1f3da82Smrg    XSetWMNormalHints(XtDisplay(xw), VShellWindow(xw), &xw->hints);
7072d522f475Smrg    TRACE(("%s@%d -- ", __FILE__, __LINE__));
7073d522f475Smrg    TRACE_WM_HINTS(xw);
7074d522f475Smrg
7075d522f475Smrg    if ((pid_atom = XInternAtom(XtDisplay(xw), "_NET_WM_PID", False)) != None) {
7076d522f475Smrg	/* XChangeProperty format 32 really is "long" */
7077d522f475Smrg	unsigned long pid_l = (unsigned long) getpid();
7078d522f475Smrg	TRACE(("Setting _NET_WM_PID property to %lu\n", pid_l));
7079a1f3da82Smrg	XChangeProperty(XtDisplay(xw), VShellWindow(xw),
7080d522f475Smrg			pid_atom, XA_CARDINAL, 32, PropModeReplace,
7081d522f475Smrg			(unsigned char *) &pid_l, 1);
7082d522f475Smrg    }
7083d522f475Smrg
7084d522f475Smrg    XFlush(XtDisplay(xw));	/* get it out to window manager */
7085d522f475Smrg
7086d522f475Smrg    /* use ForgetGravity instead of SouthWestGravity because translating
7087d522f475Smrg       the Expose events for ConfigureNotifys is too hard */
7088956cc18dSsnj    values->bit_gravity = (GravityIsNorthWest(xw)
7089d522f475Smrg			   ? NorthWestGravity
7090d522f475Smrg			   : ForgetGravity);
709120d2c4d2Smrg    TScreenOf(xw)->fullVwin.window = XtWindow(xw) =
7092d522f475Smrg	XCreateWindow(XtDisplay(xw), XtWindow(XtParent(xw)),
7093d522f475Smrg		      xw->core.x, xw->core.y,
7094d522f475Smrg		      xw->core.width, xw->core.height, BorderWidth(xw),
7095d522f475Smrg		      (int) xw->core.depth,
7096d522f475Smrg		      InputOutput, CopyFromParent,
7097d522f475Smrg		      *valuemask | CWBitGravity, values);
7098d522f475Smrg    screen->event_mask = values->event_mask;
7099d522f475Smrg
7100d522f475Smrg#ifndef NO_ACTIVE_ICON
7101492d43a5Smrg    /*
7102492d43a5Smrg     * Normally, the font-number for icon fonts does not correspond with any of
7103492d43a5Smrg     * the menu-selectable fonts.  If we cannot load the font given for the
7104492d43a5Smrg     * iconFont resource, try with font1 aka "Unreadable".
7105492d43a5Smrg     */
7106492d43a5Smrg    screen->icon_fontnum = -1;
7107492d43a5Smrg    if (screen->fnt_icon.fs == 0) {
7108492d43a5Smrg	screen->fnt_icon.fs = XLoadQueryFont(screen->display,
7109492d43a5Smrg					     screen->MenuFontName(fontMenu_font1));
7110492d43a5Smrg	TRACE(("%susing font1 '%s' as iconFont\n",
7111492d43a5Smrg	       (screen->fnt_icon.fs
7112492d43a5Smrg		? ""
7113492d43a5Smrg		: "NOT "),
7114492d43a5Smrg	       screen->MenuFontName(fontMenu_font1)));
7115492d43a5Smrg    }
7116492d43a5Smrg#if OPT_RENDERFONT
7117492d43a5Smrg    /*
7118492d43a5Smrg     * If we still have no result from iconFont resource (perhaps because fonts
7119492d43a5Smrg     * are missing) but are using Xft, try to use that instead.  We prefer
7120492d43a5Smrg     * bitmap fonts in any case, since scaled fonts are usually less readable,
7121492d43a5Smrg     * particularly at small sizes.
7122492d43a5Smrg     */
7123492d43a5Smrg    if (UsingRenderFont(xw)
7124492d43a5Smrg	&& screen->fnt_icon.fs == 0) {
7125492d43a5Smrg	screen->icon_fontnum = fontMenu_default;
7126492d43a5Smrg	screen->fnt_icon.fs = screen->fnts[0].fs;	/* need for next-if */
7127492d43a5Smrg	TRACE(("using TrueType font as iconFont\n"));
7128492d43a5Smrg    }
7129492d43a5Smrg#endif
7130d522f475Smrg    if (xw->misc.active_icon && screen->fnt_icon.fs) {
7131d522f475Smrg	int iconX = 0, iconY = 0;
7132d522f475Smrg	Widget shell = SHELL_OF(xw);
7133d522f475Smrg	VTwin *win = &(screen->iconVwin);
7134492d43a5Smrg	int save_fontnum = screen->menu_font_number;
7135d522f475Smrg
7136492d43a5Smrg	TRACE(("Initializing active-icon %d\n", screen->icon_fontnum));
7137492d43a5Smrg	screen->menu_font_number = screen->icon_fontnum;
7138492d43a5Smrg	XtVaGetValues(shell,
7139492d43a5Smrg		      XtNiconX, &iconX,
7140492d43a5Smrg		      XtNiconY, &iconY,
7141492d43a5Smrg		      (XtPointer) 0);
7142d522f475Smrg	xtermComputeFontInfo(xw, &(screen->iconVwin), screen->fnt_icon.fs, 0);
7143492d43a5Smrg	screen->menu_font_number = save_fontnum;
7144d522f475Smrg
7145d522f475Smrg	/* since only one client is permitted to select for Button
7146d522f475Smrg	 * events, we have to let the window manager get 'em...
7147d522f475Smrg	 */
7148d522f475Smrg	values->event_mask &= ~(ButtonPressMask | ButtonReleaseMask);
7149d522f475Smrg	values->border_pixel = xw->misc.icon_border_pixel;
7150d522f475Smrg
7151d522f475Smrg	screen->iconVwin.window =
7152d522f475Smrg	    XCreateWindow(XtDisplay(xw),
7153d522f475Smrg			  RootWindowOfScreen(XtScreen(shell)),
7154d522f475Smrg			  iconX, iconY,
7155d522f475Smrg			  screen->iconVwin.fullwidth,
7156d522f475Smrg			  screen->iconVwin.fullheight,
7157d522f475Smrg			  xw->misc.icon_border_width,
7158d522f475Smrg			  (int) xw->core.depth,
7159d522f475Smrg			  InputOutput, CopyFromParent,
7160d522f475Smrg			  *valuemask | CWBitGravity | CWBorderPixel,
7161d522f475Smrg			  values);
7162d522f475Smrg	XtVaSetValues(shell,
7163d522f475Smrg		      XtNiconWindow, screen->iconVwin.window,
7164d522f475Smrg		      (XtPointer) 0);
7165d522f475Smrg	XtRegisterDrawable(XtDisplay(xw), screen->iconVwin.window, w);
7166d522f475Smrg
7167d522f475Smrg	setCgsFont(xw, win, gcNorm, &(screen->fnt_icon));
7168d522f475Smrg	setCgsFore(xw, win, gcNorm, T_COLOR(screen, TEXT_FG));
7169d522f475Smrg	setCgsBack(xw, win, gcNorm, T_COLOR(screen, TEXT_BG));
7170d522f475Smrg
7171d522f475Smrg	copyCgs(xw, win, gcBold, gcNorm);
7172d522f475Smrg
7173d522f475Smrg	setCgsFont(xw, win, gcNormReverse, &(screen->fnt_icon));
7174d522f475Smrg	setCgsFore(xw, win, gcNormReverse, T_COLOR(screen, TEXT_BG));
7175d522f475Smrg	setCgsBack(xw, win, gcNormReverse, T_COLOR(screen, TEXT_FG));
7176d522f475Smrg
7177d522f475Smrg	copyCgs(xw, win, gcBoldReverse, gcNormReverse);
7178d522f475Smrg
7179d522f475Smrg#if OPT_TOOLBAR
7180d522f475Smrg	/*
7181d522f475Smrg	 * Toolbar is initialized before we get here.  Enable the menu item
7182d522f475Smrg	 * and set it properly.
7183d522f475Smrg	 */
7184d522f475Smrg	SetItemSensitivity(vtMenuEntries[vtMenu_activeicon].widget, True);
7185d522f475Smrg	update_activeicon();
7186d522f475Smrg#endif
7187d522f475Smrg    } else {
7188d522f475Smrg	TRACE(("Disabled active-icon\n"));
7189d522f475Smrg	xw->misc.active_icon = False;
7190d522f475Smrg    }
7191d522f475Smrg#endif /* NO_ACTIVE_ICON */
7192d522f475Smrg
7193d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
7194956cc18dSsnj    VTInitI18N(xw);
7195d522f475Smrg#else
719620d2c4d2Smrg    TScreenOf(xw)->xic = NULL;
7197d522f475Smrg#endif
7198d522f475Smrg#if OPT_NUM_LOCK
7199d522f475Smrg    VTInitModifiers(xw);
7200d522f475Smrg#if OPT_EXTRA_PASTE
7201d522f475Smrg    if (xw->keyboard.extra_translations) {
7202d522f475Smrg	XtOverrideTranslations((Widget) xw,
7203d522f475Smrg			       XtParseTranslationTable(xw->keyboard.extra_translations));
7204d522f475Smrg    }
7205d522f475Smrg#endif
7206d522f475Smrg#endif
7207d522f475Smrg
7208d522f475Smrg    set_cursor_gcs(xw);
7209d522f475Smrg
7210d522f475Smrg    /* Reset variables used by ANSI emulation. */
7211d522f475Smrg
7212d522f475Smrg    resetCharsets(screen);
7213d522f475Smrg
7214a1f3da82Smrg    XDefineCursor(screen->display, VShellWindow(xw), screen->pointer_cursor);
7215d522f475Smrg
7216d522f475Smrg    set_cur_col(screen, 0);
7217d522f475Smrg    set_cur_row(screen, 0);
7218d522f475Smrg    set_max_col(screen, Width(screen) / screen->fullVwin.f_width - 1);
7219d522f475Smrg    set_max_row(screen, Height(screen) / screen->fullVwin.f_height - 1);
7220d522f475Smrg    set_tb_margins(screen, 0, screen->max_row);
7221d522f475Smrg
7222d522f475Smrg    memset(screen->sc, 0, sizeof(screen->sc));
7223d522f475Smrg
7224d522f475Smrg    /* Mark screen buffer as unallocated.  We wait until the run loop so
7225d522f475Smrg       that the child process does not fork and exec with all the dynamic
7226d522f475Smrg       memory it will never use.  If we were to do it here, the
7227d522f475Smrg       swap space for new process would be huge for huge savelines. */
7228d522f475Smrg#if OPT_TEK4014
7229d522f475Smrg    if (!tekWidget)		/* if not called after fork */
7230d522f475Smrg#endif
7231956cc18dSsnj    {
7232956cc18dSsnj	screen->visbuf = NULL;
7233956cc18dSsnj	screen->saveBuf_index = NULL;
7234956cc18dSsnj    }
7235d522f475Smrg
72362eaa94a1Schristos    screen->do_wrap = False;
7237d522f475Smrg    screen->scrolls = screen->incopy = 0;
7238d522f475Smrg    xtermSetCursorBox(screen);
7239d522f475Smrg
7240d522f475Smrg    screen->savedlines = 0;
7241d522f475Smrg
7242d522f475Smrg    for (i = 0; i < 2; ++i) {
7243956cc18dSsnj	screen->whichBuf = !screen->whichBuf;
7244d522f475Smrg	CursorSave(xw);
7245d522f475Smrg    }
7246d522f475Smrg
7247d522f475Smrg    /*
7248d522f475Smrg     * Do this last, since it may change the layout via a resize.
7249d522f475Smrg     */
7250d522f475Smrg    if (xw->misc.scrollbar) {
7251d522f475Smrg	screen->fullVwin.sb_info.width = 0;
7252956cc18dSsnj	ScrollBarOn(xw, False);
7253d522f475Smrg    }
7254a1f3da82Smrg
7255d522f475Smrg    return;
7256d522f475Smrg}
7257d522f475Smrg
7258d522f475Smrg#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
7259d522f475Smrg
7260d522f475Smrg/* limit this feature to recent XFree86 since X11R6.x core dumps */
7261d522f475Smrg#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && defined(X_HAVE_UTF8_STRING)
7262d522f475Smrg#define USE_XIM_INSTANTIATE_CB
7263d522f475Smrg
7264d522f475Smrgstatic void
7265d522f475Smrgxim_instantiate_cb(Display * display,
7266d522f475Smrg		   XPointer client_data GCC_UNUSED,
7267d522f475Smrg		   XPointer call_data GCC_UNUSED)
7268d522f475Smrg{
7269d522f475Smrg    if (display != XtDisplay(term))
7270d522f475Smrg	return;
7271d522f475Smrg
7272956cc18dSsnj    VTInitI18N(term);
7273d522f475Smrg}
7274d522f475Smrg
7275d522f475Smrgstatic void
7276d522f475Smrgxim_destroy_cb(XIM im GCC_UNUSED,
7277d522f475Smrg	       XPointer client_data GCC_UNUSED,
7278d522f475Smrg	       XPointer call_data GCC_UNUSED)
7279d522f475Smrg{
728020d2c4d2Smrg    TScreenOf(term)->xic = NULL;
7281d522f475Smrg
7282d522f475Smrg    XRegisterIMInstantiateCallback(XtDisplay(term), NULL, NULL, NULL,
7283d522f475Smrg				   xim_instantiate_cb, NULL);
7284d522f475Smrg}
7285d522f475Smrg#endif /* X11R6+ */
7286d522f475Smrg
7287d522f475Smrgstatic void
7288956cc18dSsnjxim_real_init(XtermWidget xw)
7289d522f475Smrg{
7290956cc18dSsnj    TScreen *screen = TScreenOf(xw);
7291d522f475Smrg    unsigned i, j;
7292d522f475Smrg    char *p, *s, *t, *ns, *end, buf[32];
7293d522f475Smrg    XIMStyles *xim_styles;
7294d522f475Smrg    XIMStyle input_style = 0;
7295d522f475Smrg    Bool found;
7296d522f475Smrg    static struct {
729720d2c4d2Smrg	const char *name;
7298d522f475Smrg	unsigned long code;
7299d522f475Smrg    } known_style[] = {
7300d522f475Smrg	{
7301d522f475Smrg	    "OverTheSpot", (XIMPreeditPosition | XIMStatusNothing)
7302d522f475Smrg	},
7303d522f475Smrg	{
7304d522f475Smrg	    "OffTheSpot", (XIMPreeditArea | XIMStatusArea)
7305d522f475Smrg	},
7306d522f475Smrg	{
7307d522f475Smrg	    "Root", (XIMPreeditNothing | XIMStatusNothing)
7308d522f475Smrg	},
7309d522f475Smrg    };
7310d522f475Smrg
7311956cc18dSsnj    screen->xic = NULL;
7312d522f475Smrg
7313956cc18dSsnj    if (xw->misc.cannot_im) {
7314d522f475Smrg	return;
7315d522f475Smrg    }
7316d522f475Smrg
7317956cc18dSsnj    if (!xw->misc.input_method || !*xw->misc.input_method) {
7318d522f475Smrg	if ((p = XSetLocaleModifiers("")) != NULL && *p)
7319956cc18dSsnj	    screen->xim = XOpenIM(XtDisplay(xw), NULL, NULL, NULL);
7320d522f475Smrg    } else {
7321956cc18dSsnj	s = xw->misc.input_method;
732220d2c4d2Smrg	i = 5 + (unsigned) strlen(s);
732320d2c4d2Smrg
7324d522f475Smrg	t = (char *) MyStackAlloc(i, buf);
732520d2c4d2Smrg	if (t == NULL) {
7326d522f475Smrg	    SysError(ERROR_VINIT);
732720d2c4d2Smrg	} else {
7328d522f475Smrg
732920d2c4d2Smrg	    for (ns = s; ns && *s;) {
733020d2c4d2Smrg		while (*s && isspace(CharOf(*s)))
733120d2c4d2Smrg		    s++;
733220d2c4d2Smrg		if (!*s)
7333d522f475Smrg		    break;
733420d2c4d2Smrg		if ((ns = end = strchr(s, ',')) == 0)
733520d2c4d2Smrg		    end = s + strlen(s);
733620d2c4d2Smrg		while ((end != s) && isspace(CharOf(end[-1])))
733720d2c4d2Smrg		    end--;
733820d2c4d2Smrg
733920d2c4d2Smrg		if (end != s) {
734020d2c4d2Smrg		    strcpy(t, "@im=");
734120d2c4d2Smrg		    strncat(t, s, (size_t) (end - s));
734220d2c4d2Smrg
734320d2c4d2Smrg		    if ((p = XSetLocaleModifiers(t)) != 0 && *p
734420d2c4d2Smrg			&& (screen->xim = XOpenIM(XtDisplay(xw),
734520d2c4d2Smrg						  NULL,
734620d2c4d2Smrg						  NULL,
734720d2c4d2Smrg						  NULL)) != 0)
734820d2c4d2Smrg			break;
7349d522f475Smrg
735020d2c4d2Smrg		}
735120d2c4d2Smrg		s = ns + 1;
7352d522f475Smrg	    }
735320d2c4d2Smrg	    MyStackFree(t, buf);
7354d522f475Smrg	}
7355d522f475Smrg    }
7356d522f475Smrg
7357956cc18dSsnj    if (screen->xim == NULL
7358d522f475Smrg	&& (p = XSetLocaleModifiers("@im=none")) != NULL
7359d522f475Smrg	&& *p) {
7360956cc18dSsnj	screen->xim = XOpenIM(XtDisplay(xw), NULL, NULL, NULL);
7361d522f475Smrg    }
7362d522f475Smrg
7363956cc18dSsnj    if (!screen->xim) {
7364d522f475Smrg	fprintf(stderr, "Failed to open input method\n");
7365d522f475Smrg	return;
7366d522f475Smrg    }
7367d522f475Smrg    TRACE(("VTInitI18N opened input method\n"));
7368d522f475Smrg
7369956cc18dSsnj    if (XGetIMValues(screen->xim, XNQueryInputStyle, &xim_styles, NULL)
7370d522f475Smrg	|| !xim_styles
7371d522f475Smrg	|| !xim_styles->count_styles) {
7372d522f475Smrg	fprintf(stderr, "input method doesn't support any style\n");
7373956cc18dSsnj	cleanupInputMethod(screen);
7374956cc18dSsnj	xw->misc.cannot_im = True;
7375d522f475Smrg	return;
7376d522f475Smrg    }
7377d522f475Smrg
7378d522f475Smrg    found = False;
7379956cc18dSsnj    for (s = xw->misc.preedit_type; s && !found;) {
7380d522f475Smrg	while (*s && isspace(CharOf(*s)))
7381d522f475Smrg	    s++;
7382d522f475Smrg	if (!*s)
7383d522f475Smrg	    break;
7384d522f475Smrg	if ((ns = end = strchr(s, ',')) != 0)
7385d522f475Smrg	    ns++;
7386d522f475Smrg	else
7387d522f475Smrg	    end = s + strlen(s);
7388d522f475Smrg	while ((end != s) && isspace(CharOf(end[-1])))
7389d522f475Smrg	    end--;
7390d522f475Smrg
7391d522f475Smrg	if (end != s) {		/* just in case we have a spurious comma */
739220d2c4d2Smrg	    TRACE(("looking for style '%.*s'\n", (int) (end - s), s));
7393d522f475Smrg	    for (i = 0; i < XtNumber(known_style); i++) {
7394d522f475Smrg		if ((int) strlen(known_style[i].name) == (end - s)
739520d2c4d2Smrg		    && !strncmp(s, known_style[i].name, (size_t) (end - s))) {
7396d522f475Smrg		    input_style = known_style[i].code;
7397d522f475Smrg		    for (j = 0; j < xim_styles->count_styles; j++) {
7398d522f475Smrg			if (input_style == xim_styles->supported_styles[j]) {
7399d522f475Smrg			    found = True;
7400d522f475Smrg			    break;
7401d522f475Smrg			}
7402d522f475Smrg		    }
7403d522f475Smrg		    if (found)
7404d522f475Smrg			break;
7405d522f475Smrg		}
7406d522f475Smrg	    }
7407d522f475Smrg	}
7408d522f475Smrg
7409d522f475Smrg	s = ns;
7410d522f475Smrg    }
7411d522f475Smrg    XFree(xim_styles);
7412d522f475Smrg
7413d522f475Smrg    if (!found) {
7414d522f475Smrg	fprintf(stderr,
7415d522f475Smrg		"input method doesn't support my preedit type (%s)\n",
7416956cc18dSsnj		xw->misc.preedit_type);
7417956cc18dSsnj	cleanupInputMethod(screen);
7418956cc18dSsnj	xw->misc.cannot_im = True;
7419d522f475Smrg	return;
7420d522f475Smrg    }
7421d522f475Smrg
7422d522f475Smrg    /*
7423d522f475Smrg     * Check for styles we do not yet support.
7424d522f475Smrg     */
7425d522f475Smrg    TRACE(("input_style %#lx\n", input_style));
7426d522f475Smrg    if (input_style == (XIMPreeditArea | XIMStatusArea)) {
7427d522f475Smrg	fprintf(stderr,
7428d522f475Smrg		"This program doesn't support the 'OffTheSpot' preedit type\n");
7429956cc18dSsnj	cleanupInputMethod(screen);
7430956cc18dSsnj	xw->misc.cannot_im = True;
7431d522f475Smrg	return;
7432d522f475Smrg    }
7433d522f475Smrg
7434d522f475Smrg    /*
7435d522f475Smrg     * For XIMPreeditPosition (or OverTheSpot), XIM client has to
7436d522f475Smrg     * prepare a font.
7437d522f475Smrg     * The font has to be locale-dependent XFontSet, whereas
7438d522f475Smrg     * XTerm use Unicode font.  This leads a problem that the
7439d522f475Smrg     * same font cannot be used for XIM preedit.
7440d522f475Smrg     */
7441d522f475Smrg    if (input_style != (XIMPreeditNothing | XIMStatusNothing)) {
7442d522f475Smrg	char **missing_charset_list;
7443d522f475Smrg	int missing_charset_count;
7444d522f475Smrg	char *def_string;
7445d522f475Smrg	XVaNestedList p_list;
7446d522f475Smrg	XPoint spot =
7447d522f475Smrg	{0, 0};
7448d522f475Smrg	XFontStruct **fonts;
7449d522f475Smrg	char **font_name_list;
7450d522f475Smrg
7451956cc18dSsnj	screen->fs = XCreateFontSet(XtDisplay(xw),
7452956cc18dSsnj				    xw->misc.f_x,
7453956cc18dSsnj				    &missing_charset_list,
7454956cc18dSsnj				    &missing_charset_count,
7455956cc18dSsnj				    &def_string);
7456956cc18dSsnj	if (screen->fs == NULL) {
7457d522f475Smrg	    fprintf(stderr, "Preparation of font set "
7458956cc18dSsnj		    "\"%s\" for XIM failed.\n", xw->misc.f_x);
7459956cc18dSsnj	    screen->fs = XCreateFontSet(XtDisplay(xw),
7460956cc18dSsnj					DEFXIMFONT,
7461956cc18dSsnj					&missing_charset_list,
7462956cc18dSsnj					&missing_charset_count,
7463956cc18dSsnj					&def_string);
7464d522f475Smrg	}
7465956cc18dSsnj	if (screen->fs == NULL) {
7466d522f475Smrg	    fprintf(stderr, "Preparation of default font set "
7467d522f475Smrg		    "\"%s\" for XIM failed.\n", DEFXIMFONT);
7468956cc18dSsnj	    cleanupInputMethod(screen);
7469956cc18dSsnj	    xw->misc.cannot_im = True;
7470d522f475Smrg	    return;
7471d522f475Smrg	}
7472956cc18dSsnj	(void) XExtentsOfFontSet(screen->fs);
7473956cc18dSsnj	j = (unsigned) XFontsOfFontSet(screen->fs, &fonts, &font_name_list);
7474956cc18dSsnj	for (i = 0, screen->fs_ascent = 0; i < j; i++) {
7475956cc18dSsnj	    if (screen->fs_ascent < (*fonts)->ascent)
7476956cc18dSsnj		screen->fs_ascent = (*fonts)->ascent;
7477d522f475Smrg	}
7478d522f475Smrg	p_list = XVaCreateNestedList(0,
7479d522f475Smrg				     XNSpotLocation, &spot,
7480956cc18dSsnj				     XNFontSet, screen->fs,
7481d522f475Smrg				     NULL);
7482956cc18dSsnj	screen->xic = XCreateIC(screen->xim,
7483956cc18dSsnj				XNInputStyle, input_style,
7484956cc18dSsnj				XNClientWindow, XtWindow(xw),
7485956cc18dSsnj				XNFocusWindow, XtWindow(xw),
7486956cc18dSsnj				XNPreeditAttributes, p_list,
7487956cc18dSsnj				NULL);
7488d522f475Smrg    } else {
7489956cc18dSsnj	screen->xic = XCreateIC(screen->xim, XNInputStyle, input_style,
7490956cc18dSsnj				XNClientWindow, XtWindow(xw),
7491956cc18dSsnj				XNFocusWindow, XtWindow(xw),
7492956cc18dSsnj				NULL);
7493d522f475Smrg    }
7494d522f475Smrg
7495956cc18dSsnj    if (!screen->xic) {
7496d522f475Smrg	fprintf(stderr, "Failed to create input context\n");
7497956cc18dSsnj	cleanupInputMethod(screen);
7498d522f475Smrg    }
7499d522f475Smrg#if defined(USE_XIM_INSTANTIATE_CB)
7500d522f475Smrg    else {
7501d522f475Smrg	XIMCallback destroy_cb;
7502d522f475Smrg
7503d522f475Smrg	destroy_cb.callback = xim_destroy_cb;
7504d522f475Smrg	destroy_cb.client_data = NULL;
7505956cc18dSsnj	if (XSetIMValues(screen->xim, XNDestroyCallback, &destroy_cb, NULL))
7506d522f475Smrg	    fprintf(stderr, "Could not set destroy callback to IM\n");
7507d522f475Smrg    }
7508d522f475Smrg#endif
7509d522f475Smrg
7510d522f475Smrg    return;
7511d522f475Smrg}
7512d522f475Smrg
7513d522f475Smrgstatic void
7514956cc18dSsnjVTInitI18N(XtermWidget xw)
7515d522f475Smrg{
7516956cc18dSsnj    if (xw->misc.open_im) {
7517956cc18dSsnj	xim_real_init(xw);
7518d522f475Smrg
7519d522f475Smrg#if defined(USE_XIM_INSTANTIATE_CB)
752020d2c4d2Smrg	if (TScreenOf(xw)->xic == NULL
7521956cc18dSsnj	    && !xw->misc.cannot_im
7522956cc18dSsnj	    && xw->misc.retry_im-- > 0) {
7523d522f475Smrg	    sleep(3);
7524956cc18dSsnj	    XRegisterIMInstantiateCallback(XtDisplay(xw), NULL, NULL, NULL,
7525d522f475Smrg					   xim_instantiate_cb, NULL);
7526d522f475Smrg	}
7527d522f475Smrg#endif
7528d522f475Smrg    }
7529d522f475Smrg}
7530d522f475Smrg#endif /* OPT_I18N_SUPPORT && OPT_INPUT_METHOD */
7531d522f475Smrg
7532a1f3da82Smrgstatic void
7533a1f3da82Smrgset_cursor_outline_gc(XtermWidget xw,
7534a1f3da82Smrg		      Bool filled,
7535a1f3da82Smrg		      Pixel fg,
7536a1f3da82Smrg		      Pixel bg,
7537a1f3da82Smrg		      Pixel cc)
7538a1f3da82Smrg{
7539a1f3da82Smrg    TScreen *screen = TScreenOf(xw);
7540a1f3da82Smrg    VTwin *win = WhichVWin(screen);
7541a1f3da82Smrg    CgsEnum cgsId = gcVTcursOutline;
7542a1f3da82Smrg
7543a1f3da82Smrg    if (cc == bg)
7544a1f3da82Smrg	cc = fg;
7545a1f3da82Smrg
7546a1f3da82Smrg    if (filled) {
7547a1f3da82Smrg	setCgsFore(xw, win, cgsId, bg);
7548a1f3da82Smrg	setCgsBack(xw, win, cgsId, cc);
7549a1f3da82Smrg    } else {
7550a1f3da82Smrg	setCgsFore(xw, win, cgsId, cc);
7551a1f3da82Smrg	setCgsBack(xw, win, cgsId, bg);
7552a1f3da82Smrg    }
7553a1f3da82Smrg}
7554a1f3da82Smrg
7555d522f475Smrgstatic Boolean
7556d522f475SmrgVTSetValues(Widget cur,
7557d522f475Smrg	    Widget request GCC_UNUSED,
7558d522f475Smrg	    Widget wnew,
7559d522f475Smrg	    ArgList args GCC_UNUSED,
7560d522f475Smrg	    Cardinal *num_args GCC_UNUSED)
7561d522f475Smrg{
7562d522f475Smrg    XtermWidget curvt = (XtermWidget) cur;
7563d522f475Smrg    XtermWidget newvt = (XtermWidget) wnew;
75642eaa94a1Schristos    Boolean refresh_needed = False;
75652eaa94a1Schristos    Boolean fonts_redone = False;
7566d522f475Smrg
756720d2c4d2Smrg    if ((T_COLOR(TScreenOf(curvt), TEXT_BG) !=
756820d2c4d2Smrg	 T_COLOR(TScreenOf(newvt), TEXT_BG)) ||
756920d2c4d2Smrg	(T_COLOR(TScreenOf(curvt), TEXT_FG) !=
757020d2c4d2Smrg	 T_COLOR(TScreenOf(newvt), TEXT_FG)) ||
757120d2c4d2Smrg	(TScreenOf(curvt)->MenuFontName(TScreenOf(curvt)->menu_font_number) !=
757220d2c4d2Smrg	 TScreenOf(newvt)->MenuFontName(TScreenOf(newvt)->menu_font_number)) ||
7573d522f475Smrg	(curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)) {
7574d522f475Smrg	if (curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)
757520d2c4d2Smrg	    TScreenOf(newvt)->MenuFontName(fontMenu_default) = newvt->misc.default_font.f_n;
7576d522f475Smrg	if (xtermLoadFont(newvt,
757720d2c4d2Smrg			  xtermFontName(TScreenOf(newvt)->MenuFontName(TScreenOf(curvt)->menu_font_number)),
757820d2c4d2Smrg			  True, TScreenOf(newvt)->menu_font_number)) {
7579d522f475Smrg	    /* resizing does the redisplay, so don't ask for it here */
7580d522f475Smrg	    refresh_needed = True;
7581d522f475Smrg	    fonts_redone = True;
7582d522f475Smrg	} else if (curvt->misc.default_font.f_n != newvt->misc.default_font.f_n)
758320d2c4d2Smrg	    TScreenOf(newvt)->MenuFontName(fontMenu_default) = curvt->misc.default_font.f_n;
7584d522f475Smrg    }
7585d522f475Smrg    if (!fonts_redone
758620d2c4d2Smrg	&& (T_COLOR(TScreenOf(curvt), TEXT_CURSOR) !=
758720d2c4d2Smrg	    T_COLOR(TScreenOf(newvt), TEXT_CURSOR))) {
7588492d43a5Smrg	if (set_cursor_gcs(newvt))
7589492d43a5Smrg	    refresh_needed = True;
7590d522f475Smrg    }
7591d522f475Smrg    if (curvt->misc.re_verse != newvt->misc.re_verse) {
7592d522f475Smrg	newvt->flags ^= REVERSE_VIDEO;
7593d522f475Smrg	ReverseVideo(newvt);
75942eaa94a1Schristos	/* ReverseVideo toggles */
75952eaa94a1Schristos	newvt->misc.re_verse = (Boolean) (!newvt->misc.re_verse);
7596d522f475Smrg	refresh_needed = True;
7597d522f475Smrg    }
759820d2c4d2Smrg    if ((T_COLOR(TScreenOf(curvt), MOUSE_FG) !=
759920d2c4d2Smrg	 T_COLOR(TScreenOf(newvt), MOUSE_FG)) ||
760020d2c4d2Smrg	(T_COLOR(TScreenOf(curvt), MOUSE_BG) !=
760120d2c4d2Smrg	 T_COLOR(TScreenOf(newvt), MOUSE_BG))) {
760220d2c4d2Smrg	recolor_cursor(TScreenOf(newvt),
760320d2c4d2Smrg		       TScreenOf(newvt)->pointer_cursor,
760420d2c4d2Smrg		       T_COLOR(TScreenOf(newvt), MOUSE_FG),
760520d2c4d2Smrg		       T_COLOR(TScreenOf(newvt), MOUSE_BG));
7606d522f475Smrg	refresh_needed = True;
7607d522f475Smrg    }
7608d522f475Smrg    if (curvt->misc.scrollbar != newvt->misc.scrollbar) {
7609d522f475Smrg	ToggleScrollBar(newvt);
7610d522f475Smrg    }
7611d522f475Smrg
7612d522f475Smrg    return refresh_needed;
7613d522f475Smrg}
7614d522f475Smrg
7615d522f475Smrg#define setGC(code) set_at = __LINE__, currentCgs = code
7616d522f475Smrg
7617d522f475Smrg#define OutsideSelection(screen,srow,scol)  \
7618d522f475Smrg	 ((srow) > (screen)->endH.row || \
7619d522f475Smrg	  ((srow) == (screen)->endH.row && \
7620d522f475Smrg	   (scol) >= (screen)->endH.col) || \
7621d522f475Smrg	  (srow) < (screen)->startH.row || \
7622d522f475Smrg	  ((srow) == (screen)->startH.row && \
7623d522f475Smrg	   (scol) < (screen)->startH.col))
7624d522f475Smrg
7625d522f475Smrg/*
7626d522f475Smrg * Shows cursor at new cursor position in screen.
7627d522f475Smrg */
7628d522f475Smrgvoid
7629d522f475SmrgShowCursor(void)
7630d522f475Smrg{
7631d522f475Smrg    XtermWidget xw = term;
763220d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
7633d522f475Smrg    int x, y;
7634956cc18dSsnj    IChar base;
7635d522f475Smrg    unsigned flags;
7636956cc18dSsnj    CellColor fg_bg = 0;
7637d522f475Smrg    GC currentGC;
763820d2c4d2Smrg    GC outlineGC;
7639d522f475Smrg    CgsEnum currentCgs = gcMAX;
7640d522f475Smrg    VTwin *currentWin = WhichVWin(screen);
7641d522f475Smrg    int set_at;
7642d522f475Smrg    Bool in_selection;
7643d522f475Smrg    Bool reversed;
7644d522f475Smrg    Bool filled;
7645d522f475Smrg    Pixel fg_pix;
7646d522f475Smrg    Pixel bg_pix;
7647d522f475Smrg    Pixel tmp;
7648d522f475Smrg#if OPT_HIGHLIGHT_COLOR
7649d522f475Smrg    Pixel selbg_pix = T_COLOR(screen, HIGHLIGHT_BG);
7650d522f475Smrg    Pixel selfg_pix = T_COLOR(screen, HIGHLIGHT_FG);
7651d522f475Smrg    Boolean use_selbg;
7652d522f475Smrg    Boolean use_selfg;
7653d522f475Smrg#endif
7654d522f475Smrg#if OPT_WIDE_CHARS
7655956cc18dSsnj    size_t off;
7656d522f475Smrg    int my_col = 0;
7657d522f475Smrg#endif
7658d522f475Smrg    int cursor_col;
7659956cc18dSsnj    LineData *ld = 0;
7660d522f475Smrg
7661d522f475Smrg    if (screen->cursor_state == BLINKED_OFF)
7662d522f475Smrg	return;
7663d522f475Smrg
7664d522f475Smrg    if (screen->eventMode != NORMAL)
7665d522f475Smrg	return;
7666d522f475Smrg
7667d522f475Smrg    if (INX2ROW(screen, screen->cur_row) > screen->max_row)
7668d522f475Smrg	return;
7669d522f475Smrg
7670d522f475Smrg    screen->cursorp.row = screen->cur_row;
7671d522f475Smrg    cursor_col = screen->cursorp.col = screen->cur_col;
7672d522f475Smrg    screen->cursor_moved = False;
7673d522f475Smrg
7674d522f475Smrg#ifndef NO_ACTIVE_ICON
7675d522f475Smrg    if (IsIcon(screen)) {
7676d522f475Smrg	screen->cursor_state = ON;
7677d522f475Smrg	return;
7678d522f475Smrg    }
7679d522f475Smrg#endif /* NO_ACTIVE_ICON */
7680d522f475Smrg
7681956cc18dSsnj    ld = getLineData(screen, screen->cur_row);
7682956cc18dSsnj
7683956cc18dSsnj    base = ld->charData[cursor_col];
7684956cc18dSsnj    flags = ld->attribs[cursor_col];
7685d522f475Smrg
7686d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
7687956cc18dSsnj	if (base == HIDDEN_CHAR && cursor_col > 0) {
7688d522f475Smrg	    /* if cursor points to non-initial part of wide character,
7689d522f475Smrg	     * back it up
7690d522f475Smrg	     */
7691d522f475Smrg	    --cursor_col;
7692956cc18dSsnj	    base = ld->charData[cursor_col];
7693d522f475Smrg	}
7694d522f475Smrg	my_col = cursor_col;
7695956cc18dSsnj	if (base == 0)
7696956cc18dSsnj	    base = ' ';
7697956cc18dSsnj	if (isWide((int) base))
7698d522f475Smrg	    my_col += 1;
7699d522f475Smrg    });
7700d522f475Smrg
7701956cc18dSsnj    if (base == 0) {
7702956cc18dSsnj	base = ' ';
7703d522f475Smrg    }
7704956cc18dSsnj#if OPT_ISO_COLORS
7705956cc18dSsnj#ifdef EXP_BOGUS_FG
7706956cc18dSsnj    /*
7707956cc18dSsnj     * If the cursor happens to be on blanks, and we have not set both
7708956cc18dSsnj     * foreground and background color, do not treat it as a colored cell.
7709956cc18dSsnj     */
7710956cc18dSsnj    if (base == ' ') {
7711956cc18dSsnj	if ((flags & (FG_COLOR | BG_COLOR)) == BG_COLOR) {
7712956cc18dSsnj	    TRACE(("ShowCursor - do not treat as a colored cell\n"));
7713956cc18dSsnj	    flags &= ~(FG_COLOR | BG_COLOR);
7714956cc18dSsnj	} else if ((flags & (FG_COLOR | BG_COLOR)) == FG_COLOR) {
7715956cc18dSsnj	    TRACE(("ShowCursor - should we treat as a colored cell?\n"));
7716956cc18dSsnj	    if (!(xw->flags & FG_COLOR))
7717956cc18dSsnj		if (CheckBogusForeground(screen, "ShowCursor"))
7718956cc18dSsnj		    flags &= ~(FG_COLOR | BG_COLOR);
7719956cc18dSsnj	}
7720956cc18dSsnj    }
7721956cc18dSsnj#else /* !EXP_BOGUS_FG */
7722d522f475Smrg    /*
7723d522f475Smrg     * If the cursor happens to be on blanks, and the foreground color is set
7724d522f475Smrg     * but not the background, do not treat it as a colored cell.
7725d522f475Smrg     */
7726d522f475Smrg    if ((flags & TERM_COLOR_FLAGS(xw)) == BG_COLOR
7727956cc18dSsnj	&& base == ' ') {
7728d522f475Smrg	flags &= ~TERM_COLOR_FLAGS(xw);
7729d522f475Smrg    }
7730956cc18dSsnj#endif
7731d522f475Smrg#endif
7732d522f475Smrg
7733d522f475Smrg    /*
7734d522f475Smrg     * Compare the current cell to the last set of colors used for the
7735d522f475Smrg     * cursor and update the GC's if needed.
7736d522f475Smrg     */
7737956cc18dSsnj    if_OPT_ISO_COLORS(screen, {
7738956cc18dSsnj	fg_bg = ld->color[cursor_col];
7739d522f475Smrg    });
774020d2c4d2Smrg
7741d522f475Smrg    fg_pix = getXtermForeground(xw, flags, extract_fg(xw, fg_bg, flags));
7742d522f475Smrg    bg_pix = getXtermBackground(xw, flags, extract_bg(xw, fg_bg, flags));
7743d522f475Smrg
7744d522f475Smrg    if (OutsideSelection(screen, screen->cur_row, screen->cur_col))
7745d522f475Smrg	in_selection = False;
7746d522f475Smrg    else
7747d522f475Smrg	in_selection = True;
7748d522f475Smrg
7749d522f475Smrg    reversed = ReverseOrHilite(screen, flags, in_selection);
7750d522f475Smrg
7751d522f475Smrg    /* This is like updatedXtermGC(), except that we have to worry about
7752d522f475Smrg     * whether the window has focus, since in that case we want just an
7753d522f475Smrg     * outline for the cursor.
7754d522f475Smrg     */
77552eaa94a1Schristos    filled = (screen->select || screen->always_highlight) && !screen->cursor_underline;
7756d522f475Smrg#if OPT_HIGHLIGHT_COLOR
7757d522f475Smrg    use_selbg = isNotForeground(xw, fg_pix, bg_pix, selbg_pix);
7758d522f475Smrg    use_selfg = isNotBackground(xw, fg_pix, bg_pix, selfg_pix);
7759d522f475Smrg#endif
7760d522f475Smrg    if (filled) {
7761d522f475Smrg	if (reversed) {		/* text is reverse video */
7762d522f475Smrg	    if (getCgsGC(xw, currentWin, gcVTcursNormal)) {
7763d522f475Smrg		setGC(gcVTcursNormal);
7764d522f475Smrg	    } else {
7765d522f475Smrg		if (flags & BOLDATTR(screen)) {
7766d522f475Smrg		    setGC(gcBold);
7767d522f475Smrg		} else {
7768d522f475Smrg		    setGC(gcNorm);
7769d522f475Smrg		}
7770d522f475Smrg	    }
7771d522f475Smrg	    EXCHANGE(fg_pix, bg_pix, tmp);
7772d522f475Smrg#if OPT_HIGHLIGHT_COLOR
7773d522f475Smrg	    if (screen->hilite_reverse) {
7774d522f475Smrg		if (use_selbg && !use_selfg)
7775d522f475Smrg		    fg_pix = bg_pix;
7776d522f475Smrg		if (use_selfg && !use_selbg)
7777d522f475Smrg		    bg_pix = fg_pix;
7778d522f475Smrg		if (use_selbg)
7779d522f475Smrg		    bg_pix = selbg_pix;
7780d522f475Smrg		if (use_selfg)
7781d522f475Smrg		    fg_pix = selfg_pix;
7782d522f475Smrg	    }
7783d522f475Smrg#endif
7784d522f475Smrg	} else {		/* normal video */
7785d522f475Smrg	    if (getCgsGC(xw, currentWin, gcVTcursReverse)) {
7786d522f475Smrg		setGC(gcVTcursReverse);
7787d522f475Smrg	    } else {
7788d522f475Smrg		if (flags & BOLDATTR(screen)) {
7789d522f475Smrg		    setGC(gcBoldReverse);
7790d522f475Smrg		} else {
7791d522f475Smrg		    setGC(gcNormReverse);
7792d522f475Smrg		}
7793d522f475Smrg	    }
7794d522f475Smrg	}
7795d522f475Smrg	if (T_COLOR(screen, TEXT_CURSOR) == xw->dft_foreground) {
7796d522f475Smrg	    setCgsBack(xw, currentWin, currentCgs, fg_pix);
7797d522f475Smrg	}
7798d522f475Smrg	setCgsFore(xw, currentWin, currentCgs, bg_pix);
7799d522f475Smrg    } else {			/* not selected */
7800d522f475Smrg	if (reversed) {		/* text is reverse video */
7801d522f475Smrg	    EXCHANGE(fg_pix, bg_pix, tmp);
7802d522f475Smrg	    setGC(gcNormReverse);
7803d522f475Smrg	} else {		/* normal video */
7804d522f475Smrg	    setGC(gcNorm);
7805d522f475Smrg	}
7806d522f475Smrg#if OPT_HIGHLIGHT_COLOR
7807d522f475Smrg	if (screen->hilite_reverse) {
7808d522f475Smrg	    if (in_selection && !reversed) {
7809a1f3da82Smrg		/* EMPTY */
7810a1f3da82Smrg		/* really INVERSE ... */
7811a1f3da82Smrg		;
7812d522f475Smrg	    } else if (in_selection || reversed) {
7813d522f475Smrg		if (use_selbg) {
7814d522f475Smrg		    if (use_selfg) {
7815d522f475Smrg			bg_pix = fg_pix;
7816d522f475Smrg		    } else {
7817d522f475Smrg			fg_pix = bg_pix;
7818d522f475Smrg		    }
7819d522f475Smrg		}
7820d522f475Smrg		if (use_selbg) {
7821d522f475Smrg		    bg_pix = selbg_pix;
7822d522f475Smrg		}
7823d522f475Smrg		if (use_selfg) {
7824d522f475Smrg		    fg_pix = selfg_pix;
7825d522f475Smrg		}
7826d522f475Smrg	    }
7827d522f475Smrg	} else {
7828d522f475Smrg	    if (in_selection) {
7829d522f475Smrg		if (use_selbg) {
7830d522f475Smrg		    bg_pix = selbg_pix;
7831d522f475Smrg		}
7832d522f475Smrg		if (use_selfg) {
7833d522f475Smrg		    fg_pix = selfg_pix;
7834d522f475Smrg		}
7835d522f475Smrg	    }
7836d522f475Smrg	}
7837d522f475Smrg#endif
7838d522f475Smrg	setCgsFore(xw, currentWin, currentCgs, fg_pix);
7839d522f475Smrg	setCgsBack(xw, currentWin, currentCgs, bg_pix);
7840d522f475Smrg    }
7841d522f475Smrg
7842d522f475Smrg    if (screen->cursor_busy == 0
7843d522f475Smrg	&& (screen->cursor_state != ON || screen->cursor_GC != set_at)) {
7844d522f475Smrg
7845d522f475Smrg	screen->cursor_GC = set_at;
784620d2c4d2Smrg	TRACE(("ShowCursor calling drawXtermText cur(%d,%d) %s-%s, set_at %d\n",
7847d522f475Smrg	       screen->cur_row, screen->cur_col,
784820d2c4d2Smrg	       (filled ? "filled" : "outline"),
784920d2c4d2Smrg	       (screen->cursor_underline ? "underline" : "box"),
785020d2c4d2Smrg	       set_at));
7851d522f475Smrg
7852d522f475Smrg	currentGC = getCgsGC(xw, currentWin, currentCgs);
785320d2c4d2Smrg	x = LineCursorX(screen, ld, cursor_col);
785420d2c4d2Smrg	y = CursorY(screen, screen->cur_row);
7855d522f475Smrg
785620d2c4d2Smrg	if (screen->cursor_underline) {
785720d2c4d2Smrg	    /*
7858a1f3da82Smrg	     * Overriding the combination of filled, reversed, in_selection is
7859a1f3da82Smrg	     * too complicated since the underline and the text-cell use
786020d2c4d2Smrg	     * different rules.  Just redraw the text-cell, and draw the
786120d2c4d2Smrg	     * underline on top of it.
786220d2c4d2Smrg	     */
786320d2c4d2Smrg	    HideCursor();
786420d2c4d2Smrg
786520d2c4d2Smrg	    /*
786620d2c4d2Smrg	     * Our current-GC is likely to have been modified in HideCursor().
7867a1f3da82Smrg	     * Set up a new request.
786820d2c4d2Smrg	     */
786920d2c4d2Smrg	    if (filled) {
787020d2c4d2Smrg		if (T_COLOR(screen, TEXT_CURSOR) == xw->dft_foreground) {
787120d2c4d2Smrg		    setCgsBack(xw, currentWin, currentCgs, fg_pix);
787220d2c4d2Smrg		}
787320d2c4d2Smrg		setCgsFore(xw, currentWin, currentCgs, bg_pix);
787420d2c4d2Smrg	    } else {
787520d2c4d2Smrg		setCgsFore(xw, currentWin, currentCgs, fg_pix);
787620d2c4d2Smrg		setCgsBack(xw, currentWin, currentCgs, bg_pix);
7877d522f475Smrg	    }
7878a1f3da82Smrg	}
7879a1f3da82Smrg
7880a1f3da82Smrg	/*
7881a1f3da82Smrg	 * Update the outline-gc, to keep the cursor color distinct from the
7882a1f3da82Smrg	 * background color.
7883a1f3da82Smrg	 */
7884a1f3da82Smrg	set_cursor_outline_gc(xw,
7885a1f3da82Smrg			      filled,
7886a1f3da82Smrg			      fg_pix,
7887a1f3da82Smrg			      bg_pix,
7888a1f3da82Smrg			      T_COLOR(screen, TEXT_CURSOR));
7889d522f475Smrg
7890a1f3da82Smrg	outlineGC = getCgsGC(xw, currentWin, gcVTcursOutline);
7891a1f3da82Smrg	if (outlineGC == 0)
7892a1f3da82Smrg	    outlineGC = currentGC;
7893a1f3da82Smrg
7894a1f3da82Smrg	if (screen->cursor_underline) {
7895d522f475Smrg
789620d2c4d2Smrg	    /*
789720d2c4d2Smrg	     * Finally, draw the underline.
789820d2c4d2Smrg	     */
78992eaa94a1Schristos	    screen->box->x = (short) x;
790020d2c4d2Smrg	    screen->box->y = (short) (y + FontHeight(screen) - 2);
7901d522f475Smrg	    XDrawLines(screen->display, VWindow(screen), outlineGC,
7902d522f475Smrg		       screen->box, NBOX, CoordModePrevious);
790320d2c4d2Smrg	} else {
790420d2c4d2Smrg
790520d2c4d2Smrg	    drawXtermText(xw, flags & DRAWX_MASK,
790620d2c4d2Smrg			  currentGC, x, y,
790720d2c4d2Smrg			  LineCharSet(screen, ld),
790820d2c4d2Smrg			  &base, 1, 0);
790920d2c4d2Smrg
791020d2c4d2Smrg#if OPT_WIDE_CHARS
791120d2c4d2Smrg	    if_OPT_WIDE_CHARS(screen, {
791220d2c4d2Smrg		for_each_combData(off, ld) {
791320d2c4d2Smrg		    if (!(ld->combData[off][my_col]))
791420d2c4d2Smrg			break;
791520d2c4d2Smrg		    drawXtermText(xw, (flags & DRAWX_MASK) | NOBACKGROUND,
791620d2c4d2Smrg				  currentGC, x, y,
791720d2c4d2Smrg				  LineCharSet(screen, ld),
791820d2c4d2Smrg				  ld->combData[off] + my_col,
791920d2c4d2Smrg				  1, isWide((int) base));
792020d2c4d2Smrg		}
792120d2c4d2Smrg	    });
792220d2c4d2Smrg#endif
792320d2c4d2Smrg
792420d2c4d2Smrg	    if (!filled) {
792520d2c4d2Smrg		screen->box->x = (short) x;
792620d2c4d2Smrg		screen->box->y = (short) y;
792720d2c4d2Smrg		XDrawLines(screen->display, VWindow(screen), outlineGC,
792820d2c4d2Smrg			   screen->box, NBOX, CoordModePrevious);
792920d2c4d2Smrg	    }
7930d522f475Smrg	}
7931d522f475Smrg    }
7932d522f475Smrg    screen->cursor_state = ON;
7933956cc18dSsnj
7934956cc18dSsnj    return;
7935d522f475Smrg}
7936d522f475Smrg
7937d522f475Smrg/*
7938d522f475Smrg * hide cursor at previous cursor position in screen.
7939d522f475Smrg */
7940d522f475Smrgvoid
7941d522f475SmrgHideCursor(void)
7942d522f475Smrg{
7943d522f475Smrg    XtermWidget xw = term;
794420d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
7945d522f475Smrg    GC currentGC;
7946d522f475Smrg    int x, y;
7947956cc18dSsnj    IChar base;
7948956cc18dSsnj    unsigned flags;
7949956cc18dSsnj    CellColor fg_bg = 0;
7950d522f475Smrg    Bool in_selection;
7951d522f475Smrg#if OPT_WIDE_CHARS
7952956cc18dSsnj    size_t off;
7953d522f475Smrg    int my_col = 0;
7954d522f475Smrg#endif
7955d522f475Smrg    int cursor_col;
7956956cc18dSsnj    LineData *ld = 0;
7957d522f475Smrg
795820d2c4d2Smrg    if (screen->cursor_state == OFF)
7959d522f475Smrg	return;
7960d522f475Smrg    if (INX2ROW(screen, screen->cursorp.row) > screen->max_row)
7961d522f475Smrg	return;
7962d522f475Smrg
7963d522f475Smrg    cursor_col = screen->cursorp.col;
7964d522f475Smrg
7965d522f475Smrg#ifndef NO_ACTIVE_ICON
7966d522f475Smrg    if (IsIcon(screen)) {
7967d522f475Smrg	screen->cursor_state = OFF;
7968d522f475Smrg	return;
7969d522f475Smrg    }
7970d522f475Smrg#endif /* NO_ACTIVE_ICON */
7971d522f475Smrg
7972956cc18dSsnj    ld = getLineData(screen, screen->cursorp.row);
7973956cc18dSsnj
7974956cc18dSsnj    base = ld->charData[cursor_col];
7975956cc18dSsnj    flags = ld->attribs[cursor_col];
7976d522f475Smrg
7977d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
7978956cc18dSsnj	if (base == HIDDEN_CHAR && cursor_col > 0) {
7979d522f475Smrg	    /* if cursor points to non-initial part of wide character,
7980d522f475Smrg	     * back it up
7981d522f475Smrg	     */
7982d522f475Smrg	    --cursor_col;
7983956cc18dSsnj	    base = ld->charData[cursor_col];
7984d522f475Smrg	}
7985d522f475Smrg	my_col = cursor_col;
7986956cc18dSsnj	if (base == 0)
7987956cc18dSsnj	    base = ' ';
7988956cc18dSsnj	if (isWide((int) base))
7989d522f475Smrg	    my_col += 1;
7990d522f475Smrg    });
7991d522f475Smrg
7992956cc18dSsnj    if (base == 0) {
7993956cc18dSsnj	base = ' ';
7994956cc18dSsnj    }
7995956cc18dSsnj#ifdef EXP_BOGUS_FG
7996956cc18dSsnj    /*
7997956cc18dSsnj     * If the cursor happens to be on blanks, and we have not set both
7998956cc18dSsnj     * foreground and background color, do not treat it as a colored cell.
7999956cc18dSsnj     */
8000956cc18dSsnj#if OPT_ISO_COLORS
8001956cc18dSsnj    if (base == ' ') {
8002956cc18dSsnj	if ((flags & (FG_COLOR | BG_COLOR)) == BG_COLOR) {
8003956cc18dSsnj	    TRACE(("HideCursor - do not treat as a colored cell\n"));
8004956cc18dSsnj	    flags &= ~(FG_COLOR | BG_COLOR);
8005956cc18dSsnj	} else if ((flags & (FG_COLOR | BG_COLOR)) == FG_COLOR) {
8006956cc18dSsnj	    TRACE(("HideCursor - should we treat as a colored cell?\n"));
8007956cc18dSsnj	    if (!(xw->flags & FG_COLOR))
8008956cc18dSsnj		if (CheckBogusForeground(screen, "HideCursor"))
8009956cc18dSsnj		    flags &= ~(FG_COLOR | BG_COLOR);
8010956cc18dSsnj	}
8011956cc18dSsnj    }
8012956cc18dSsnj#endif
8013956cc18dSsnj#endif
8014956cc18dSsnj#if OPT_ISO_COLORS
8015956cc18dSsnj    fg_bg = 0;
8016956cc18dSsnj#endif
8017956cc18dSsnj
8018956cc18dSsnj    /*
8019956cc18dSsnj     * Compare the current cell to the last set of colors used for the
8020956cc18dSsnj     * cursor and update the GC's if needed.
8021956cc18dSsnj     */
8022956cc18dSsnj    if_OPT_ISO_COLORS(screen, {
8023956cc18dSsnj	fg_bg = ld->color[cursor_col];
8024d522f475Smrg    });
8025d522f475Smrg
8026d522f475Smrg    if (OutsideSelection(screen, screen->cursorp.row, screen->cursorp.col))
8027d522f475Smrg	in_selection = False;
8028d522f475Smrg    else
8029d522f475Smrg	in_selection = True;
8030d522f475Smrg
8031d522f475Smrg    currentGC = updatedXtermGC(xw, flags, fg_bg, in_selection);
8032d522f475Smrg
8033d522f475Smrg    TRACE(("HideCursor calling drawXtermText cur(%d,%d)\n",
8034d522f475Smrg	   screen->cursorp.row, screen->cursorp.col));
803520d2c4d2Smrg
803620d2c4d2Smrg    x = LineCursorX(screen, ld, cursor_col);
803720d2c4d2Smrg    y = CursorY(screen, screen->cursorp.row);
803820d2c4d2Smrg
803920d2c4d2Smrg    drawXtermText(xw, flags & DRAWX_MASK,
804020d2c4d2Smrg		  currentGC, x, y,
8041956cc18dSsnj		  LineCharSet(screen, ld),
8042956cc18dSsnj		  &base, 1, 0);
8043d522f475Smrg
8044d522f475Smrg#if OPT_WIDE_CHARS
8045d522f475Smrg    if_OPT_WIDE_CHARS(screen, {
8046956cc18dSsnj	for_each_combData(off, ld) {
8047956cc18dSsnj	    if (!(ld->combData[off][my_col]))
8048d522f475Smrg		break;
8049d522f475Smrg	    drawXtermText(xw, (flags & DRAWX_MASK) | NOBACKGROUND,
8050d522f475Smrg			  currentGC, x, y,
8051956cc18dSsnj			  LineCharSet(screen, ld),
8052956cc18dSsnj			  ld->combData[off] + my_col,
8053956cc18dSsnj			  1, isWide((int) base));
8054d522f475Smrg	}
8055d522f475Smrg    });
8056d522f475Smrg#endif
8057d522f475Smrg    screen->cursor_state = OFF;
8058d522f475Smrg    resetXtermGC(xw, flags, in_selection);
8059956cc18dSsnj
8060956cc18dSsnj    return;
8061d522f475Smrg}
8062d522f475Smrg
8063d522f475Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
8064d522f475Smrgstatic void
8065d522f475SmrgStartBlinking(TScreen * screen)
8066d522f475Smrg{
8067d522f475Smrg    if (screen->blink_timer == 0) {
80682eaa94a1Schristos	unsigned long interval = (unsigned long) ((screen->cursor_state == ON)
80692eaa94a1Schristos						  ? screen->blink_on
80702eaa94a1Schristos						  : screen->blink_off);
8071d522f475Smrg	if (interval == 0)	/* wow! */
8072d522f475Smrg	    interval = 1;	/* let's humor him anyway */
8073d522f475Smrg	screen->blink_timer = XtAppAddTimeOut(app_con,
8074d522f475Smrg					      interval,
8075d522f475Smrg					      HandleBlinking,
8076d522f475Smrg					      screen);
8077d522f475Smrg    }
8078d522f475Smrg}
8079d522f475Smrg
8080d522f475Smrgstatic void
8081d522f475SmrgStopBlinking(TScreen * screen)
8082d522f475Smrg{
8083d522f475Smrg    if (screen->blink_timer)
8084d522f475Smrg	XtRemoveTimeOut(screen->blink_timer);
8085d522f475Smrg    screen->blink_timer = 0;
8086d522f475Smrg}
8087d522f475Smrg
8088d522f475Smrg#if OPT_BLINK_TEXT
8089d522f475Smrgstatic Bool
8090956cc18dSsnjLineHasBlinking(TScreen * screen, LineData * ld)
8091d522f475Smrg{
8092d522f475Smrg    int col;
8093d522f475Smrg    Bool result = False;
8094d522f475Smrg
8095d522f475Smrg    for (col = 0; col < MaxCols(screen); ++col) {
8096956cc18dSsnj	if (ld->attribs[col] & BLINK) {
8097d522f475Smrg	    result = True;
8098d522f475Smrg	    break;
8099d522f475Smrg	}
8100d522f475Smrg    }
8101d522f475Smrg    return result;
8102d522f475Smrg}
8103d522f475Smrg#endif
8104d522f475Smrg
8105d522f475Smrg/*
8106d522f475Smrg * Blink the cursor by alternately showing/hiding cursor.  We leave the timer
8107d522f475Smrg * running all the time (even though that's a little inefficient) to make the
8108d522f475Smrg * logic simple.
8109d522f475Smrg */
8110d522f475Smrgstatic void
8111d522f475SmrgHandleBlinking(XtPointer closure, XtIntervalId * id GCC_UNUSED)
8112d522f475Smrg{
8113d522f475Smrg    TScreen *screen = (TScreen *) closure;
8114d522f475Smrg    Bool resume = False;
8115d522f475Smrg
8116d522f475Smrg    screen->blink_timer = 0;
8117d522f475Smrg    screen->blink_state = !screen->blink_state;
8118d522f475Smrg
8119d522f475Smrg#if OPT_BLINK_CURS
8120d522f475Smrg    if (DoStartBlinking(screen)) {
8121d522f475Smrg	if (screen->cursor_state == ON) {
8122d522f475Smrg	    if (screen->select || screen->always_highlight) {
8123d522f475Smrg		HideCursor();
8124d522f475Smrg		if (screen->cursor_state == OFF)
8125d522f475Smrg		    screen->cursor_state = BLINKED_OFF;
8126d522f475Smrg	    }
8127d522f475Smrg	} else if (screen->cursor_state == BLINKED_OFF) {
8128d522f475Smrg	    screen->cursor_state = OFF;
8129d522f475Smrg	    ShowCursor();
8130d522f475Smrg	    if (screen->cursor_state == OFF)
8131d522f475Smrg		screen->cursor_state = BLINKED_OFF;
8132d522f475Smrg	}
8133d522f475Smrg	resume = True;
8134d522f475Smrg    }
8135d522f475Smrg#endif
8136d522f475Smrg
8137d522f475Smrg#if OPT_BLINK_TEXT
8138d522f475Smrg    /*
813920d2c4d2Smrg     * Inspect the lines on the current screen to see if any have the BLINK flag
8140d522f475Smrg     * associated with them.  Prune off any that have had the corresponding
8141d522f475Smrg     * cells reset.  If any are left, repaint those lines with ScrnRefresh().
8142d522f475Smrg     */
8143d522f475Smrg    if (!(screen->blink_as_bold)) {
8144d522f475Smrg	int row;
8145d522f475Smrg	int first_row = screen->max_row;
8146d522f475Smrg	int last_row = -1;
8147d522f475Smrg
8148d522f475Smrg	for (row = screen->max_row; row >= 0; row--) {
8149956cc18dSsnj	    LineData *ld = getLineData(screen, ROW2INX(screen, row));
815020d2c4d2Smrg
815120d2c4d2Smrg	    if (ld != 0 && LineTstBlinked(ld)) {
8152956cc18dSsnj		if (LineHasBlinking(screen, ld)) {
8153d522f475Smrg		    resume = True;
8154d522f475Smrg		    if (row > last_row)
8155d522f475Smrg			last_row = row;
8156d522f475Smrg		    if (row < first_row)
8157d522f475Smrg			first_row = row;
8158d522f475Smrg		} else {
8159956cc18dSsnj		    LineClrBlinked(ld);
8160d522f475Smrg		}
8161d522f475Smrg	    }
8162d522f475Smrg	}
8163d522f475Smrg	/*
8164d522f475Smrg	 * FIXME: this could be a little more efficient, e.g,. by limiting the
8165d522f475Smrg	 * columns which are updated.
8166d522f475Smrg	 */
8167d522f475Smrg	if (first_row <= last_row) {
8168d522f475Smrg	    ScrnRefresh(term,
8169d522f475Smrg			first_row,
8170d522f475Smrg			0,
8171d522f475Smrg			last_row + 1 - first_row,
8172d522f475Smrg			MaxCols(screen),
8173d522f475Smrg			True);
8174d522f475Smrg	}
8175d522f475Smrg    }
8176d522f475Smrg#endif
8177d522f475Smrg
8178d522f475Smrg    /*
8179d522f475Smrg     * If either the cursor or text is blinking, restart the timer.
8180d522f475Smrg     */
8181d522f475Smrg    if (resume)
8182d522f475Smrg	StartBlinking(screen);
8183d522f475Smrg}
8184d522f475Smrg#endif /* OPT_BLINK_CURS || OPT_BLINK_TEXT */
8185d522f475Smrg
818620d2c4d2Smrgvoid
818720d2c4d2SmrgRestartBlinking(TScreen * screen GCC_UNUSED)
818820d2c4d2Smrg{
818920d2c4d2Smrg#if OPT_BLINK_CURS || OPT_BLINK_TEXT
819020d2c4d2Smrg    if (screen->blink_timer == 0) {
819120d2c4d2Smrg	Bool resume = False;
819220d2c4d2Smrg
819320d2c4d2Smrg#if OPT_BLINK_CURS
819420d2c4d2Smrg	if (DoStartBlinking(screen)) {
819520d2c4d2Smrg	    resume = True;
819620d2c4d2Smrg	}
819720d2c4d2Smrg#endif
819820d2c4d2Smrg#if OPT_BLINK_TEXT
819920d2c4d2Smrg	if (!resume) {
820020d2c4d2Smrg	    int row;
820120d2c4d2Smrg
820220d2c4d2Smrg	    for (row = screen->max_row; row >= 0; row--) {
820320d2c4d2Smrg		LineData *ld = getLineData(screen, ROW2INX(screen, row));
820420d2c4d2Smrg
820520d2c4d2Smrg		if (ld != 0 && LineTstBlinked(ld)) {
820620d2c4d2Smrg		    if (LineHasBlinking(screen, ld)) {
820720d2c4d2Smrg			resume = True;
820820d2c4d2Smrg			break;
820920d2c4d2Smrg		    }
821020d2c4d2Smrg		}
821120d2c4d2Smrg	    }
821220d2c4d2Smrg	}
821320d2c4d2Smrg#endif
821420d2c4d2Smrg	if (resume)
821520d2c4d2Smrg	    StartBlinking(screen);
821620d2c4d2Smrg    }
821720d2c4d2Smrg#endif
821820d2c4d2Smrg}
821920d2c4d2Smrg
8220d522f475Smrg/*
8221d522f475Smrg * Implement soft or hard (full) reset of the VTxxx emulation.  There are a
8222d522f475Smrg * couple of differences from real DEC VTxxx terminals (to avoid breaking
8223d522f475Smrg * applications which have come to rely on xterm doing this):
8224d522f475Smrg *
8225d522f475Smrg *	+ autowrap mode should be reset (instead it's reset to the resource
8226d522f475Smrg *	  default).
8227d522f475Smrg *	+ the popup menu offers a choice of resetting the savedLines, or not.
8228d522f475Smrg *	  (but the control sequence does this anyway).
8229d522f475Smrg */
8230d522f475Smrgvoid
8231d522f475SmrgVTReset(XtermWidget xw, Bool full, Bool saved)
8232d522f475Smrg{
823320d2c4d2Smrg    static char empty[1];
823420d2c4d2Smrg
823520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
8236d522f475Smrg
8237d522f475Smrg    if (!XtIsRealized((Widget) xw) || (CURRENT_EMU() != (Widget) xw)) {
823820d2c4d2Smrg	Bell(xw, XkbBI_MinorError, 0);
8239d522f475Smrg	return;
8240d522f475Smrg    }
8241d522f475Smrg
8242d522f475Smrg    if (saved) {
8243d522f475Smrg	screen->savedlines = 0;
8244d522f475Smrg	ScrollBarDrawThumb(screen->scrollWidget);
8245d522f475Smrg    }
8246d522f475Smrg
8247d522f475Smrg    /* make cursor visible */
8248d522f475Smrg    screen->cursor_set = ON;
8249d522f475Smrg
8250d522f475Smrg    /* reset scrolling region */
8251d522f475Smrg    set_tb_margins(screen, 0, screen->max_row);
8252d522f475Smrg
8253d522f475Smrg    bitclr(&xw->flags, ORIGIN);
8254d522f475Smrg
8255d522f475Smrg    if_OPT_ISO_COLORS(screen, {
8256d522f475Smrg	reset_SGR_Colors(xw);
825720d2c4d2Smrg	if (ResetAnsiColorRequest(xw, empty, 0))
825820d2c4d2Smrg	    xtermRepaint(xw);
8259d522f475Smrg    });
8260d522f475Smrg
8261d522f475Smrg    /* Reset character-sets to initial state */
8262d522f475Smrg    resetCharsets(screen);
8263d522f475Smrg
8264d522f475Smrg#if OPT_MOD_FKEYS
8265d522f475Smrg    /* Reset modifier-resources to initial state */
8266d522f475Smrg    xw->keyboard.modify_now = xw->keyboard.modify_1st;
8267d522f475Smrg#endif
8268d522f475Smrg
8269d522f475Smrg    /* Reset DECSCA */
8270d522f475Smrg    bitclr(&xw->flags, PROTECTED);
8271d522f475Smrg    screen->protected_mode = OFF_PROTECT;
8272d522f475Smrg
8273d522f475Smrg    if (full) {			/* RIS */
8274d522f475Smrg	if (screen->bellOnReset)
827520d2c4d2Smrg	    Bell(xw, XkbBI_TerminalBell, 0);
8276d522f475Smrg
8277d522f475Smrg	/* reset the mouse mode */
8278d522f475Smrg	screen->send_mouse_pos = MOUSE_OFF;
8279d522f475Smrg	screen->send_focus_pos = OFF;
8280492d43a5Smrg	screen->ext_mode_mouse = OFF;
8281d522f475Smrg	screen->waitingForTrackInfo = False;
8282d522f475Smrg	screen->eventMode = NORMAL;
8283d522f475Smrg
8284d522f475Smrg	xtermShowPointer(xw, True);
8285d522f475Smrg
8286d522f475Smrg	TabReset(xw->tabs);
8287d522f475Smrg	xw->keyboard.flags = MODE_SRM;
8288d522f475Smrg#if OPT_INITIAL_ERASE
8289d522f475Smrg	if (xw->keyboard.reset_DECBKM == 1)
8290d522f475Smrg	    xw->keyboard.flags |= MODE_DECBKM;
8291d522f475Smrg	else if (xw->keyboard.reset_DECBKM == 2)
8292d522f475Smrg#endif
829320d2c4d2Smrg	    if (TScreenOf(xw)->backarrow_key)
8294d522f475Smrg		xw->keyboard.flags |= MODE_DECBKM;
8295d522f475Smrg	TRACE(("full reset DECBKM %s\n",
8296d522f475Smrg	       BtoS(xw->keyboard.flags & MODE_DECBKM)));
8297d522f475Smrg	update_appcursor();
8298d522f475Smrg	update_appkeypad();
8299d522f475Smrg	update_decbkm();
8300d522f475Smrg	show_8bit_control(False);
8301d522f475Smrg	reset_decudk();
8302d522f475Smrg
8303d522f475Smrg	FromAlternate(xw);
8304d522f475Smrg	ClearScreen(xw);
8305d522f475Smrg	screen->cursor_state = OFF;
8306d522f475Smrg	if (xw->flags & REVERSE_VIDEO)
8307d522f475Smrg	    ReverseVideo(xw);
8308d522f475Smrg
8309d522f475Smrg	xw->flags = xw->initflags;
8310d522f475Smrg	update_reversevideo();
8311d522f475Smrg	update_autowrap();
8312d522f475Smrg	update_reversewrap();
8313d522f475Smrg	update_autolinefeed();
8314d522f475Smrg
83152eaa94a1Schristos	screen->jumpscroll = (Boolean) (!(xw->flags & SMOOTHSCROLL));
8316d522f475Smrg	update_jumpscroll();
8317d522f475Smrg
8318d522f475Smrg	if (screen->c132 && (xw->flags & IN132COLUMNS)) {
83192eaa94a1Schristos	    Dimension reqWidth = (Dimension) (80 * FontWidth(screen)
83202eaa94a1Schristos					      + 2 * screen->border
83212eaa94a1Schristos					      + ScrollbarWidth(screen));
83222eaa94a1Schristos	    Dimension reqHeight = (Dimension) (FontHeight(screen)
83232eaa94a1Schristos					       * MaxRows(screen)
83242eaa94a1Schristos					       + 2 * screen->border);
8325d522f475Smrg	    Dimension replyWidth;
8326d522f475Smrg	    Dimension replyHeight;
8327d522f475Smrg
8328d522f475Smrg	    TRACE(("Making resize-request to restore 80-columns %dx%d\n",
8329d522f475Smrg		   reqHeight, reqWidth));
83302eaa94a1Schristos	    REQ_RESIZE((Widget) xw,
83312eaa94a1Schristos		       reqWidth,
83322eaa94a1Schristos		       reqHeight,
83332eaa94a1Schristos		       &replyWidth, &replyHeight);
8334d522f475Smrg	    repairSizeHints();
8335d522f475Smrg	    XSync(screen->display, False);	/* synchronize */
8336d522f475Smrg	    if (XtAppPending(app_con))
8337d522f475Smrg		xevents();
8338d522f475Smrg	}
8339d522f475Smrg
8340d522f475Smrg	CursorSet(screen, 0, 0, xw->flags);
8341d522f475Smrg	CursorSave(xw);
8342d522f475Smrg    } else {			/* DECSTR */
8343d522f475Smrg	/*
8344d522f475Smrg	 * There's a tiny difference, to accommodate usage of xterm.
8345d522f475Smrg	 * We reset autowrap to the resource values rather than turning
8346d522f475Smrg	 * it off.
8347d522f475Smrg	 */
834820d2c4d2Smrg	UIntClr(xw->keyboard.flags, (MODE_DECCKM | MODE_KAM | MODE_DECKPAM));
8349d522f475Smrg	bitcpy(&xw->flags, xw->initflags, WRAPAROUND | REVERSEWRAP);
8350d522f475Smrg	bitclr(&xw->flags, INSERT | INVERSE | BOLD | BLINK | UNDERLINE | INVISIBLE);
8351d522f475Smrg	if_OPT_ISO_COLORS(screen, {
8352d522f475Smrg	    reset_SGR_Colors(xw);
8353d522f475Smrg	});
8354d522f475Smrg	update_appcursor();
8355d522f475Smrg	update_autowrap();
8356d522f475Smrg	update_reversewrap();
8357d522f475Smrg
8358d522f475Smrg	CursorSave(xw);
8359956cc18dSsnj	screen->sc[screen->whichBuf].row =
8360956cc18dSsnj	    screen->sc[screen->whichBuf].col = 0;
8361d522f475Smrg    }
8362d522f475Smrg    longjmp(vtjmpbuf, 1);	/* force ground state in parser */
8363d522f475Smrg}
8364d522f475Smrg
8365d522f475Smrg/*
8366d522f475Smrg * set_character_class - takes a string of the form
8367d522f475Smrg *
8368d522f475Smrg *   low[-high]:val[,low[-high]:val[...]]
8369d522f475Smrg *
8370d522f475Smrg * and sets the indicated ranges to the indicated values.
8371d522f475Smrg */
8372d522f475Smrgstatic int
8373d522f475Smrgset_character_class(char *s)
8374d522f475Smrg{
8375d522f475Smrg    int i;			/* iterator, index into s */
8376d522f475Smrg    int len;			/* length of s */
8377d522f475Smrg    int acc;			/* accumulator */
8378d522f475Smrg    int low, high;		/* bounds of range [0..127] */
8379d522f475Smrg    int base;			/* 8, 10, 16 (octal, decimal, hex) */
8380d522f475Smrg    int numbers;		/* count of numbers per range */
8381d522f475Smrg    int digits;			/* count of digits in a number */
83826879286fSmrg    static const char errfmt[] = "%s:  %s in range string \"%s\" (position %d)\n";
8383d522f475Smrg
8384d522f475Smrg    if (!s || !s[0])
8385d522f475Smrg	return -1;
8386d522f475Smrg
8387d522f475Smrg    base = 10;			/* in case we ever add octal, hex */
8388d522f475Smrg    low = high = -1;		/* out of range */
8389d522f475Smrg
83902eaa94a1Schristos    for (i = 0, len = (int) strlen(s), acc = 0, numbers = digits = 0;
8391d522f475Smrg	 i < len; i++) {
83922eaa94a1Schristos	Char c = CharOf(s[i]);
8393d522f475Smrg
8394d522f475Smrg	if (isspace(c)) {
8395d522f475Smrg	    continue;
8396d522f475Smrg	} else if (isdigit(c)) {
8397d522f475Smrg	    acc = acc * base + (c - '0');
8398d522f475Smrg	    digits++;
8399d522f475Smrg	    continue;
8400d522f475Smrg	} else if (c == '-') {
8401d522f475Smrg	    low = acc;
8402d522f475Smrg	    acc = 0;
8403d522f475Smrg	    if (digits == 0) {
8404d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "missing number", s, i);
8405d522f475Smrg		return (-1);
8406d522f475Smrg	    }
8407d522f475Smrg	    digits = 0;
8408d522f475Smrg	    numbers++;
8409d522f475Smrg	    continue;
8410d522f475Smrg	} else if (c == ':') {
8411d522f475Smrg	    if (numbers == 0)
8412d522f475Smrg		low = acc;
8413d522f475Smrg	    else if (numbers == 1)
8414d522f475Smrg		high = acc;
8415d522f475Smrg	    else {
8416d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "too many numbers",
8417d522f475Smrg			s, i);
8418d522f475Smrg		return (-1);
8419d522f475Smrg	    }
8420d522f475Smrg	    digits = 0;
8421d522f475Smrg	    numbers++;
8422d522f475Smrg	    acc = 0;
8423d522f475Smrg	    continue;
8424d522f475Smrg	} else if (c == ',') {
8425d522f475Smrg	    /*
8426d522f475Smrg	     * now, process it
8427d522f475Smrg	     */
8428d522f475Smrg
8429d522f475Smrg	    if (high < 0) {
8430d522f475Smrg		high = low;
8431d522f475Smrg		numbers++;
8432d522f475Smrg	    }
8433d522f475Smrg	    if (numbers != 2) {
8434d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "bad value number",
8435d522f475Smrg			s, i);
8436d522f475Smrg	    } else if (SetCharacterClassRange(low, high, acc) != 0) {
8437d522f475Smrg		fprintf(stderr, errfmt, ProgramName, "bad range", s, i);
8438d522f475Smrg	    }
8439d522f475Smrg
8440d522f475Smrg	    low = high = -1;
8441d522f475Smrg	    acc = 0;
8442d522f475Smrg	    digits = 0;
8443d522f475Smrg	    numbers = 0;
8444d522f475Smrg	    continue;
8445d522f475Smrg	} else {
8446d522f475Smrg	    fprintf(stderr, errfmt, ProgramName, "bad character", s, i);
8447d522f475Smrg	    return (-1);
8448d522f475Smrg	}			/* end if else if ... else */
8449d522f475Smrg
8450d522f475Smrg    }
8451d522f475Smrg
8452d522f475Smrg    if (low < 0 && high < 0)
8453d522f475Smrg	return (0);
8454d522f475Smrg
8455d522f475Smrg    /*
8456d522f475Smrg     * now, process it
8457d522f475Smrg     */
8458d522f475Smrg
8459d522f475Smrg    if (high < 0)
8460d522f475Smrg	high = low;
8461d522f475Smrg    if (numbers < 1 || numbers > 2) {
8462d522f475Smrg	fprintf(stderr, errfmt, ProgramName, "bad value number", s, i);
8463d522f475Smrg    } else if (SetCharacterClassRange(low, high, acc) != 0) {
8464d522f475Smrg	fprintf(stderr, errfmt, ProgramName, "bad range", s, i);
8465d522f475Smrg    }
8466d522f475Smrg
8467d522f475Smrg    return (0);
8468d522f475Smrg}
8469d522f475Smrg
8470d522f475Smrg/* ARGSUSED */
8471d522f475Smrgstatic void
8472d522f475SmrgHandleKeymapChange(Widget w,
8473d522f475Smrg		   XEvent * event GCC_UNUSED,
8474d522f475Smrg		   String * params,
8475d522f475Smrg		   Cardinal *param_count)
8476d522f475Smrg{
8477d522f475Smrg    static XtTranslations keymap, original;
8478d522f475Smrg    static XtResource key_resources[] =
8479d522f475Smrg    {
8480d522f475Smrg	{XtNtranslations, XtCTranslations, XtRTranslationTable,
8481d522f475Smrg	 sizeof(XtTranslations), 0, XtRTranslationTable, (XtPointer) NULL}
8482d522f475Smrg    };
8483d522f475Smrg    char mapName[1000];
8484d522f475Smrg    char mapClass[1000];
8485d522f475Smrg    char *pmapName;
8486d522f475Smrg    char *pmapClass;
8487d522f475Smrg    size_t len;
8488d522f475Smrg
8489d522f475Smrg    if (*param_count != 1)
8490d522f475Smrg	return;
8491d522f475Smrg
8492d522f475Smrg    if (original == NULL)
8493d522f475Smrg	original = w->core.tm.translations;
8494d522f475Smrg
8495d522f475Smrg    if (strcmp(params[0], "None") == 0) {
8496d522f475Smrg	XtOverrideTranslations(w, original);
8497d522f475Smrg	return;
8498d522f475Smrg    }
8499d522f475Smrg
8500d522f475Smrg    len = strlen(params[0]) + 7;
8501d522f475Smrg
8502d522f475Smrg    pmapName = (char *) MyStackAlloc(len, mapName);
8503d522f475Smrg    pmapClass = (char *) MyStackAlloc(len, mapClass);
8504d522f475Smrg    if (pmapName == NULL
850520d2c4d2Smrg	|| pmapClass == NULL) {
8506d522f475Smrg	SysError(ERROR_KMMALLOC1);
850720d2c4d2Smrg    } else {
850820d2c4d2Smrg
850920d2c4d2Smrg	(void) sprintf(pmapName, "%sKeymap", params[0]);
851020d2c4d2Smrg	(void) strcpy(pmapClass, pmapName);
851120d2c4d2Smrg	if (islower(CharOf(pmapClass[0])))
851220d2c4d2Smrg	    pmapClass[0] = x_toupper(pmapClass[0]);
851320d2c4d2Smrg	XtGetSubresources(w, (XtPointer) &keymap, pmapName, pmapClass,
851420d2c4d2Smrg			  key_resources, (Cardinal) 1, NULL, (Cardinal) 0);
851520d2c4d2Smrg	if (keymap != NULL)
851620d2c4d2Smrg	    XtOverrideTranslations(w, keymap);
8517d522f475Smrg
851820d2c4d2Smrg	MyStackFree(pmapName, mapName);
851920d2c4d2Smrg	MyStackFree(pmapClass, mapClass);
852020d2c4d2Smrg    }
8521d522f475Smrg}
8522d522f475Smrg
8523d522f475Smrg/* ARGSUSED */
8524d522f475Smrgstatic void
8525d522f475SmrgHandleBell(Widget w GCC_UNUSED,
8526d522f475Smrg	   XEvent * event GCC_UNUSED,
8527d522f475Smrg	   String * params,	/* [0] = volume */
8528d522f475Smrg	   Cardinal *param_count)	/* 0 or 1 */
8529d522f475Smrg{
8530d522f475Smrg    int percent = (*param_count) ? atoi(params[0]) : 0;
8531d522f475Smrg
853220d2c4d2Smrg    Bell(term, XkbBI_TerminalBell, percent);
8533d522f475Smrg}
8534d522f475Smrg
8535d522f475Smrg/* ARGSUSED */
8536d522f475Smrgstatic void
8537d522f475SmrgHandleVisualBell(Widget w GCC_UNUSED,
8538d522f475Smrg		 XEvent * event GCC_UNUSED,
8539d522f475Smrg		 String * params GCC_UNUSED,
8540d522f475Smrg		 Cardinal *param_count GCC_UNUSED)
8541d522f475Smrg{
8542d522f475Smrg    VisualBell();
8543d522f475Smrg}
8544d522f475Smrg
8545d522f475Smrg/* ARGSUSED */
8546d522f475Smrgstatic void
8547d522f475SmrgHandleIgnore(Widget w,
8548d522f475Smrg	     XEvent * event,
8549d522f475Smrg	     String * params GCC_UNUSED,
8550d522f475Smrg	     Cardinal *param_count GCC_UNUSED)
8551d522f475Smrg{
8552956cc18dSsnj    XtermWidget xw;
8553956cc18dSsnj
855420d2c4d2Smrg    TRACE(("Handle ignore for %p\n", (void *) w));
8555956cc18dSsnj    if ((xw = getXtermWidget(w)) != 0) {
8556d522f475Smrg	/* do nothing, but check for funny escape sequences */
8557956cc18dSsnj	(void) SendMousePosition(xw, event);
8558d522f475Smrg    }
8559d522f475Smrg}
8560d522f475Smrg
8561d522f475Smrg/* ARGSUSED */
8562d522f475Smrgstatic void
8563d522f475SmrgDoSetSelectedFont(Widget w,
8564d522f475Smrg		  XtPointer client_data GCC_UNUSED,
8565d522f475Smrg		  Atom * selection GCC_UNUSED,
8566d522f475Smrg		  Atom * type,
8567d522f475Smrg		  XtPointer value,
8568d522f475Smrg		  unsigned long *length,
8569d522f475Smrg		  int *format)
8570d522f475Smrg{
8571956cc18dSsnj    XtermWidget xw = getXtermWidget(w);
8572956cc18dSsnj
8573956cc18dSsnj    if ((xw == 0) || *type != XA_STRING || *format != 8) {
857420d2c4d2Smrg	Bell(xw, XkbBI_MinorError, 0);
8575d522f475Smrg    } else {
8576d522f475Smrg	Boolean failed = False;
857720d2c4d2Smrg	int oldFont = TScreenOf(xw)->menu_font_number;
857820d2c4d2Smrg	String save = TScreenOf(xw)->MenuFontName(fontMenu_fontsel);
8579d522f475Smrg	char *val;
8580d522f475Smrg	char *test = 0;
8581d522f475Smrg	char *used = 0;
858220d2c4d2Smrg	unsigned len = (unsigned) *length;
8583d522f475Smrg	unsigned tst;
8584d522f475Smrg
8585d522f475Smrg	/*
8586d522f475Smrg	 * Some versions of X deliver null-terminated selections, some do not.
8587d522f475Smrg	 */
8588d522f475Smrg	for (tst = 0; tst < len; ++tst) {
8589d522f475Smrg	    if (((char *) value)[tst] == '\0') {
8590d522f475Smrg		len = tst;
8591d522f475Smrg		break;
8592d522f475Smrg	    }
8593d522f475Smrg	}
8594d522f475Smrg
8595d522f475Smrg	if (len > 0 && (val = TypeMallocN(char, len + 1)) != 0) {
859620d2c4d2Smrg	    memcpy(val, value, (size_t) len);
8597d522f475Smrg	    val[len] = '\0';
8598d522f475Smrg	    used = x_strtrim(val);
85996879286fSmrg	    TRACE(("DoSetSelectedFont(%s)\n", used));
8600d522f475Smrg	    /* Do some sanity checking to avoid sending a long selection
8601d522f475Smrg	       back to the server in an OpenFont that is unlikely to succeed.
8602d522f475Smrg	       XLFD allows up to 255 characters and no control characters;
8603d522f475Smrg	       we are a little more liberal here. */
8604d522f475Smrg	    if (len < 1000
86056879286fSmrg		&& !strchr(used, '\n')
86066879286fSmrg		&& (test = x_strdup(used)) != 0) {
860720d2c4d2Smrg		TScreenOf(xw)->MenuFontName(fontMenu_fontsel) = test;
8608d522f475Smrg		if (!xtermLoadFont(term,
86096879286fSmrg				   xtermFontName(used),
8610d522f475Smrg				   True,
8611d522f475Smrg				   fontMenu_fontsel)) {
8612d522f475Smrg		    failed = True;
8613d522f475Smrg		    free(test);
861420d2c4d2Smrg		    TScreenOf(xw)->MenuFontName(fontMenu_fontsel) = save;
8615d522f475Smrg		}
8616d522f475Smrg	    } else {
8617d522f475Smrg		failed = True;
8618d522f475Smrg	    }
8619d522f475Smrg	    if (failed) {
8620d522f475Smrg		(void) xtermLoadFont(term,
862120d2c4d2Smrg				     xtermFontName(TScreenOf(xw)->MenuFontName(oldFont)),
8622d522f475Smrg				     True,
8623d522f475Smrg				     oldFont);
862420d2c4d2Smrg		Bell(xw, XkbBI_MinorError, 0);
8625d522f475Smrg	    }
86266879286fSmrg	    free(used);
8627d522f475Smrg	    free(val);
8628d522f475Smrg	}
8629d522f475Smrg    }
8630d522f475Smrg}
8631d522f475Smrg
8632d522f475Smrgvoid
8633d522f475SmrgFindFontSelection(XtermWidget xw, const char *atom_name, Bool justprobe)
8634d522f475Smrg{
863520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
8636d522f475Smrg    static AtomPtr *atoms;
86372eaa94a1Schristos    unsigned int atomCount = 0;
8638d522f475Smrg    AtomPtr *pAtom;
86392eaa94a1Schristos    unsigned a;
8640d522f475Smrg    Atom target;
8641d522f475Smrg
8642d522f475Smrg    if (!atom_name)
8643956cc18dSsnj	atom_name = (screen->mappedSelect
8644956cc18dSsnj		     ? screen->mappedSelect[0]
8645d522f475Smrg		     : "PRIMARY");
8646d522f475Smrg    TRACE(("FindFontSelection(%s)\n", atom_name));
8647d522f475Smrg
8648d522f475Smrg    for (pAtom = atoms, a = atomCount; a; a--, pAtom++) {
8649d522f475Smrg	if (strcmp(atom_name, XmuNameOfAtom(*pAtom)) == 0)
8650d522f475Smrg	    break;
8651d522f475Smrg    }
8652d522f475Smrg    if (!a) {
8653a1f3da82Smrg	atoms = TypeXtReallocN(AtomPtr, atoms, atomCount + 1);
865420d2c4d2Smrg	*(pAtom = &atoms[atomCount]) = XmuMakeAtom(atom_name);
8655d522f475Smrg    }
8656d522f475Smrg
8657d522f475Smrg    target = XmuInternAtom(XtDisplay(xw), *pAtom);
8658d522f475Smrg    if (justprobe) {
8659956cc18dSsnj	screen->MenuFontName(fontMenu_fontsel) =
8660d522f475Smrg	    XGetSelectionOwner(XtDisplay(xw), target) ? _Font_Selected_ : 0;
8661956cc18dSsnj	TRACE(("...selected fontname '%s'\n",
8662956cc18dSsnj	       NonNull(screen->MenuFontName(fontMenu_fontsel))));
8663d522f475Smrg    } else {
8664d522f475Smrg	XtGetSelectionValue((Widget) xw, target, XA_STRING,
8665d522f475Smrg			    DoSetSelectedFont, NULL,
8666d522f475Smrg			    XtLastTimestampProcessed(XtDisplay(xw)));
8667d522f475Smrg    }
8668d522f475Smrg    return;
8669d522f475Smrg}
8670d522f475Smrg
8671492d43a5SmrgBool
8672d522f475Smrgset_cursor_gcs(XtermWidget xw)
8673d522f475Smrg{
867420d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
8675d522f475Smrg    VTwin *win = WhichVWin(screen);
8676d522f475Smrg
8677d522f475Smrg    Pixel cc = T_COLOR(screen, TEXT_CURSOR);
8678d522f475Smrg    Pixel fg = T_COLOR(screen, TEXT_FG);
8679d522f475Smrg    Pixel bg = T_COLOR(screen, TEXT_BG);
8680492d43a5Smrg    Bool changed = False;
8681d522f475Smrg
8682d522f475Smrg    /*
8683d522f475Smrg     * Let's see, there are three things that have "color":
8684d522f475Smrg     *
8685d522f475Smrg     *     background
8686d522f475Smrg     *     text
8687d522f475Smrg     *     cursorblock
8688d522f475Smrg     *
8689d522f475Smrg     * And, there are four situations when drawing a cursor, if we decide
8690d522f475Smrg     * that we like have a solid block of cursor color with the letter
8691d522f475Smrg     * that it is highlighting shown in the background color to make it
8692d522f475Smrg     * stand out:
8693d522f475Smrg     *
8694d522f475Smrg     *     selected window, normal video - background on cursor
8695d522f475Smrg     *     selected window, reverse video - foreground on cursor
8696d522f475Smrg     *     unselected window, normal video - foreground on background
8697d522f475Smrg     *     unselected window, reverse video - background on foreground
8698d522f475Smrg     *
8699d522f475Smrg     * Since the last two are really just normalGC and reverseGC, we only
8700d522f475Smrg     * need two new GC's.  Under monochrome, we get the same effect as
8701d522f475Smrg     * above by setting cursor color to foreground.
8702d522f475Smrg     */
8703d522f475Smrg
8704d522f475Smrg    TRACE(("set_cursor_gcs cc=%#lx, fg=%#lx, bg=%#lx\n", cc, fg, bg));
8705d522f475Smrg    if (win != 0 && (cc != bg)) {
8706d522f475Smrg	/* set the fonts to the current one */
8707d522f475Smrg	setCgsFont(xw, win, gcVTcursNormal, 0);
8708d522f475Smrg	setCgsFont(xw, win, gcVTcursFilled, 0);
8709d522f475Smrg	setCgsFont(xw, win, gcVTcursReverse, 0);
8710d522f475Smrg	setCgsFont(xw, win, gcVTcursOutline, 0);
8711d522f475Smrg
8712d522f475Smrg	/* we have a colored cursor */
8713d522f475Smrg	setCgsFore(xw, win, gcVTcursNormal, fg);
8714d522f475Smrg	setCgsBack(xw, win, gcVTcursNormal, cc);
8715d522f475Smrg
8716d522f475Smrg	setCgsFore(xw, win, gcVTcursFilled, cc);
8717d522f475Smrg	setCgsBack(xw, win, gcVTcursFilled, fg);
8718d522f475Smrg
8719d522f475Smrg	if (screen->always_highlight) {
8720d522f475Smrg	    /* both GC's use the same color */
8721d522f475Smrg	    setCgsFore(xw, win, gcVTcursReverse, bg);
8722d522f475Smrg	    setCgsBack(xw, win, gcVTcursReverse, cc);
8723d522f475Smrg	} else {
8724d522f475Smrg	    setCgsFore(xw, win, gcVTcursReverse, bg);
8725d522f475Smrg	    setCgsBack(xw, win, gcVTcursReverse, cc);
8726d522f475Smrg	}
8727a1f3da82Smrg	set_cursor_outline_gc(xw, screen->always_highlight, fg, bg, cc);
8728d522f475Smrg	changed = True;
8729d522f475Smrg    }
8730d522f475Smrg
8731d522f475Smrg    if (changed) {
8732d522f475Smrg	TRACE(("...set_cursor_gcs - done\n"));
8733d522f475Smrg    }
8734492d43a5Smrg    return changed;
8735d522f475Smrg}
8736d522f475Smrg
8737a1f3da82Smrg/*
8738a1f3da82Smrg * Build up the default translations string, allowing the user to suppress
8739a1f3da82Smrg * some of the features.
8740a1f3da82Smrg */
8741a1f3da82Smrgvoid
8742a1f3da82SmrgVTInitTranslations(void)
8743a1f3da82Smrg{
8744a1f3da82Smrg    /* *INDENT-OFF* */
8745a1f3da82Smrg    static struct {
8746a1f3da82Smrg	Boolean wanted;
8747a1f3da82Smrg	const char *name;
8748a1f3da82Smrg	const char *value;
8749a1f3da82Smrg    } table[] = {
8750a1f3da82Smrg	{
8751a1f3da82Smrg	    False,
8752a1f3da82Smrg	    "default",
8753a1f3da82Smrg"\
8754a1f3da82Smrg          Shift <KeyPress> Prior:scroll-back(1,halfpage) \n\
8755a1f3da82Smrg           Shift <KeyPress> Next:scroll-forw(1,halfpage) \n\
8756a1f3da82Smrg         Shift <KeyPress> Select:select-cursor-start() select-cursor-end(SELECT, CUT_BUFFER0) \n\
8757a1f3da82Smrg         Shift <KeyPress> Insert:insert-selection(SELECT, CUT_BUFFER0) \n\
8758a1f3da82Smrg"
8759a1f3da82Smrg	},
8760a1f3da82Smrg#if OPT_MAXIMIZE
8761a1f3da82Smrg	{
8762a1f3da82Smrg	    False,
8763a1f3da82Smrg	    "fullscreen",
8764a1f3da82Smrg"\
8765a1f3da82Smrg                 Alt <Key>Return:fullscreen() \n\
8766a1f3da82Smrg"
8767a1f3da82Smrg	},
8768a1f3da82Smrg#endif
8769a1f3da82Smrg#if OPT_SCROLL_LOCK
8770a1f3da82Smrg	{
8771a1f3da82Smrg	    False,
8772a1f3da82Smrg	    "scroll-lock",
8773a1f3da82Smrg"\
8774a1f3da82Smrg        <KeyRelease> Scroll_Lock:scroll-lock() \n\
8775a1f3da82Smrg"
8776a1f3da82Smrg	},
8777a1f3da82Smrg#endif
8778a1f3da82Smrg#if OPT_SHIFT_FONTS
8779a1f3da82Smrg	{
8780a1f3da82Smrg	    False,
8781a1f3da82Smrg	    "shift-fonts",
8782a1f3da82Smrg"\
8783a1f3da82Smrg    Shift~Ctrl <KeyPress> KP_Add:larger-vt-font() \n\
8784a1f3da82Smrg    Shift Ctrl <KeyPress> KP_Add:smaller-vt-font() \n\
8785a1f3da82Smrg    Shift <KeyPress> KP_Subtract:smaller-vt-font() \n\
8786a1f3da82Smrg"
8787a1f3da82Smrg	},
8788a1f3da82Smrg#endif
8789a1f3da82Smrg	/* PROCURA added "Meta <Btn2Down>:clear-saved-lines()" */
8790a1f3da82Smrg	{
8791a1f3da82Smrg	    False,
8792a1f3da82Smrg	    "default",
8793a1f3da82Smrg"\
8794a1f3da82Smrg                ~Meta <KeyPress>:insert-seven-bit() \n\
8795a1f3da82Smrg                 Meta <KeyPress>:insert-eight-bit() \n\
8796a1f3da82Smrg                !Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
8797a1f3da82Smrg           !Lock Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
8798a1f3da82Smrg !Lock Ctrl @Num_Lock <Btn1Down>:popup-menu(mainMenu) \n\
8799a1f3da82Smrg     ! @Num_Lock Ctrl <Btn1Down>:popup-menu(mainMenu) \n\
8800a1f3da82Smrg                ~Meta <Btn1Down>:select-start() \n\
8801a1f3da82Smrg              ~Meta <Btn1Motion>:select-extend() \n\
8802a1f3da82Smrg                !Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
8803a1f3da82Smrg           !Lock Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
8804a1f3da82Smrg !Lock Ctrl @Num_Lock <Btn2Down>:popup-menu(vtMenu) \n\
8805a1f3da82Smrg     ! @Num_Lock Ctrl <Btn2Down>:popup-menu(vtMenu) \n\
8806a1f3da82Smrg          ~Ctrl ~Meta <Btn2Down>:ignore() \n\
8807a1f3da82Smrg                 Meta <Btn2Down>:clear-saved-lines() \n\
8808a1f3da82Smrg            ~Ctrl ~Meta <Btn2Up>:insert-selection(SELECT, CUT_BUFFER0) \n\
8809a1f3da82Smrg                !Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
8810a1f3da82Smrg           !Lock Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
8811a1f3da82Smrg !Lock Ctrl @Num_Lock <Btn3Down>:popup-menu(fontMenu) \n\
8812a1f3da82Smrg     ! @Num_Lock Ctrl <Btn3Down>:popup-menu(fontMenu) \n\
8813a1f3da82Smrg          ~Ctrl ~Meta <Btn3Down>:start-extend() \n\
8814a1f3da82Smrg              ~Meta <Btn3Motion>:select-extend() \n\
8815a1f3da82Smrg"
8816a1f3da82Smrg	},
8817a1f3da82Smrg	{
8818a1f3da82Smrg	    False,
8819a1f3da82Smrg	    "wheel-mouse",
8820a1f3da82Smrg"\
8821a1f3da82Smrg                 Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
8822a1f3da82Smrg            Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
8823a1f3da82Smrg  Lock @Num_Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
8824a1f3da82Smrg       @Num_Lock Ctrl <Btn4Down>:scroll-back(1,halfpage,m) \n\
8825a1f3da82Smrg                      <Btn4Down>:scroll-back(5,line,m)     \n\
8826a1f3da82Smrg                 Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
8827a1f3da82Smrg            Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
8828a1f3da82Smrg  Lock @Num_Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
8829a1f3da82Smrg       @Num_Lock Ctrl <Btn5Down>:scroll-forw(1,halfpage,m) \n\
8830a1f3da82Smrg                      <Btn5Down>:scroll-forw(5,line,m)     \n\
8831a1f3da82Smrg"
8832a1f3da82Smrg	},
8833a1f3da82Smrg	{
8834a1f3da82Smrg	    False,
8835a1f3da82Smrg	    "default",
8836a1f3da82Smrg"\
8837a1f3da82Smrg                         <BtnUp>:select-end(SELECT, CUT_BUFFER0) \n\
8838a1f3da82Smrg                       <BtnDown>:ignore() \
8839a1f3da82Smrg"
8840a1f3da82Smrg	}
8841a1f3da82Smrg    };
8842a1f3da82Smrg    /* *INDENT-ON* */
8843a1f3da82Smrg
8844a1f3da82Smrg    size_t needed = 0;
8845a1f3da82Smrg    char *result = 0;
8846a1f3da82Smrg
8847a1f3da82Smrg    int pass;
8848a1f3da82Smrg    Cardinal item;
8849a1f3da82Smrg
8850a1f3da82Smrg    TRACE(("VTInitTranslations\n"));
8851a1f3da82Smrg    for (item = 0; item < XtNumber(table); ++item) {
8852a1f3da82Smrg	table[item].wanted = True;
8853a1f3da82Smrg    }
8854a1f3da82Smrg#if OPT_MAXIMIZE
8855a1f3da82Smrg    /*
8856a1f3da82Smrg     * As a special case, allow for disabling the alt-enter translation if
8857a1f3da82Smrg     * the resource settings prevent fullscreen from being used.  We would
8858a1f3da82Smrg     * do the same for scroll-lock and shift-fonts if they were application
8859a1f3da82Smrg     * resources too, rather than in the widget.
8860a1f3da82Smrg     */
8861a1f3da82Smrg    if (resource.fullscreen == esNever) {
8862a1f3da82Smrg	for (item = 0; item < XtNumber(table); ++item) {
8863a1f3da82Smrg	    if (!strcmp(table[item].name, "fullscreen"))
8864a1f3da82Smrg		table[item].wanted = False;
8865a1f3da82Smrg	}
8866a1f3da82Smrg    }
8867a1f3da82Smrg#endif
8868a1f3da82Smrg    if (!IsEmpty(resource.omitTranslation)) {
8869a1f3da82Smrg	char *value;
8870a1f3da82Smrg	const char *source = resource.omitTranslation;
8871a1f3da82Smrg
8872a1f3da82Smrg	while (*source != '\0' && (value = ParseList(&source)) != 0) {
8873a1f3da82Smrg	    size_t len = strlen(value);
8874a1f3da82Smrg
8875a1f3da82Smrg	    TRACE(("parsed:%s\n", value));
8876a1f3da82Smrg	    for (item = 0; item < XtNumber(table); ++item) {
8877a1f3da82Smrg		if (strlen(table[item].name) >= len
8878a1f3da82Smrg		    && x_strncasecmp(table[item].name,
8879a1f3da82Smrg				     value,
8880a1f3da82Smrg				     (unsigned) len) == 0) {
8881a1f3da82Smrg		    table[item].wanted = False;
8882a1f3da82Smrg		    TRACE(("omit(%s):\n%s\n", table[item].name, table[item].value));
8883a1f3da82Smrg		    break;
8884a1f3da82Smrg		}
8885a1f3da82Smrg	    }
8886a1f3da82Smrg	    free(value);
8887a1f3da82Smrg	}
8888a1f3da82Smrg    }
8889a1f3da82Smrg
8890a1f3da82Smrg    for (pass = 0; pass < 2; ++pass) {
8891a1f3da82Smrg	needed = 0;
8892a1f3da82Smrg	for (item = 0; item < XtNumber(table); ++item) {
8893a1f3da82Smrg	    if (table[item].wanted) {
8894a1f3da82Smrg		if (pass) {
8895a1f3da82Smrg		    strcat(result, table[item].value);
8896a1f3da82Smrg		} else {
8897a1f3da82Smrg		    needed += strlen(table[item].value) + 1;
8898a1f3da82Smrg		}
8899a1f3da82Smrg	    }
8900a1f3da82Smrg	}
8901a1f3da82Smrg	if (!pass) {
8902a1f3da82Smrg	    result = XtMalloc((Cardinal) needed);
8903a1f3da82Smrg	    *result = '\0';
8904a1f3da82Smrg	}
8905a1f3da82Smrg    }
8906a1f3da82Smrg
8907a1f3da82Smrg    TRACE(("result:\n%s\n", result));
8908a1f3da82Smrg
8909a1f3da82Smrg    defaultTranslations = result;
8910a1f3da82Smrg    xtermClassRec.core_class.tm_table = result;
8911a1f3da82Smrg}
8912a1f3da82Smrg
8913d522f475Smrg#ifdef NO_LEAKS
8914d522f475Smrgvoid
8915d522f475Smrgnoleaks_charproc(void)
8916d522f475Smrg{
8917d522f475Smrg    if (v_buffer != 0)
8918d522f475Smrg	free(v_buffer);
8919d522f475Smrg}
8920d522f475Smrg#endif
8921