OSDN Git Service

2chのスレッド "ローグ系のRPG地下29階" の248氏のパッチを取り込み, タイ
[hengband/hengband.git] / src / z-term.c
1 /* File: z-term.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.
9  */
10
11 /* Purpose: a generic, efficient, terminal window package -BEN- */
12 #include "angband.h"
13
14 #include "z-term.h"
15
16 #include "z-virt.h"
17
18 /* Special flags in the attr data */
19 #define AF_BIGTILE2 0xf0
20 #define AF_TILE1   0x80
21
22 #ifdef JP
23 #define AF_KANJI1  0x10
24 #define AF_KANJI2  0x20
25 #define AF_KANJIC  0x0f
26 /*
27  * Á´³Ñʸ»úÂбþ¡£
28  * Â°À­¤ËÁ´³Ñʸ»ú¤Î£±¥Ð¥¤¥ÈÌÜ¡¢£²¥Ð¥¤¥ÈÌܤ⵭²±¡£
29  * By FIRST
30  */
31 #endif
32 /*
33  * This file provides a generic, efficient, terminal window package,
34  * which can be used not only on standard terminal environments such
35  * as dumb terminals connected to a Unix box, but also in more modern
36  * "graphic" environments, such as the Macintosh or Unix/X11.
37  *
38  * Each "window" works like a standard "dumb terminal", that is, it
39  * can display a two dimensional array of grids containing colored
40  * textual symbols, plus an optional cursor, and it can be used to
41  * get keypress events from the user.
42  *
43  * In fact, this package can simply be used, if desired, to support
44  * programs which will look the same on a dumb terminal as they do
45  * on a graphic platform such as the Macintosh.
46  *
47  * This package was designed to help port the game "Angband" to a wide
48  * variety of different platforms.  Angband, like many other games in
49  * the "rogue-like" heirarchy, requires, at the minimum, the ability
50  * to display "colored textual symbols" in a standard 80x24 "window",
51  * such as that provided by most dumb terminals, and many old personal
52  * computers, and to check for "keypresses" from the user.  The major
53  * concerns were thus portability and efficiency, so Angband could be
54  * easily ported to many different systems, with minimal effort, and
55  * yet would run quickly on each of these systems, no matter what kind
56  * of underlying hardware/software support was being used.
57  *
58  * It is important to understand the differences between the older
59  * "dumb terminals" and the newer "graphic interface" machines, since
60  * this package was designed to work with both types of systems.
61  *
62  * New machines:
63  *   waiting for a keypress is complex
64  *   checking for a keypress is often cheap
65  *   changing "colors" may be expensive
66  *   the "color" of a "blank" is rarely important
67  *   moving the "cursor" is relatively cheap
68  *   use a "software" cursor (only moves when requested)
69  *   drawing characters normally will not erase old ones
70  *   drawing a character on the cursor often erases it
71  *   may have fast routines for "clear a region"
72  *   the bottom right corner is usually not special
73  *
74  * Old machines:
75  *   waiting for a keypress is simple
76  *   checking for a keypress is often expensive
77  *   changing "colors" is usually cheap
78  *   the "color" of a "blank" may be important
79  *   moving the "cursor" may be expensive
80  *   use a "hardware" cursor (moves during screen updates)
81  *   drawing new symbols automatically erases old ones
82  *   characters may only be drawn at the cursor location
83  *   drawing a character on the cursor will move the cursor
84  *   may have fast routines for "clear entire window"
85  *   may have fast routines for "clear to end of line"
86  *   the bottom right corner is often dangerous
87  *
88  *
89  * This package provides support for multiple windows, each of an
90  * arbitrary size (up to 255x255), each with its own set of flags,
91  * and its own hooks to handle several low-level procedures which
92  * differ from platform to platform.  Then the main program simply
93  * creates one or more "term" structures, setting the various flags
94  * and hooks in a manner appropriate for the current platform, and
95  * then it can use the various "term" structures without worrying
96  * about the underlying platform.
97  *
98  *
99  * This package allows each "grid" in each window to hold an attr/char
100  * pair, with each ranging from 0 to 255, and makes very few assumptions
101  * about the meaning of any attr/char values.  Normally, we assume that
102  * "attr 0" is "black", with the semantics that "black" text should be
103  * sent to "Term_wipe()" instead of "Term_text()", but this sematics is
104  * modified if either the "always_pict" or the "always_text" flags are
105  * set.  We assume that "char 0" is "dangerous", since placing such a
106  * "char" in the middle of a string "terminates" the string, and usually
107  * we prevent its use.
108  *
109  * Finally, we use a special attr/char pair, defaulting to "attr 0" and
110  * "char 32", also known as "black space", when we "erase" or "clear"
111  * any window, but this pair can be redefined to any pair, including
112  * the standard "white space", or the bizarre "emptiness" ("attr 0"
113  * and "char 0"), as long as various obscure restrictions are met.
114  *
115  *
116  * This package provides several functions which allow a program to
117  * interact with the "term" structures.  Most of the functions allow
118  * the program to "request" certain changes to the current "term",
119  * such as moving the cursor, drawing an attr/char pair, erasing a
120  * region of grids, hiding the cursor, etc.  Then there is a special
121  * function which causes all of the "pending" requests to be performed
122  * in an efficient manner.  There is another set of functions which
123  * allow the program to query the "requested state" of the current
124  * "term", such as asking for the cursor location, or what attr/char
125  * is at a given location, etc.  There is another set of functions
126  * dealing with "keypress" events, which allows the program to ask if
127  * the user has pressed any keys, or to forget any keys the user pressed.
128  * There is a pair of functions to allow this package to memorize the
129  * contents of the current "term", and to restore these contents at
130  * a later time.  There is a special function which allows the program
131  * to specify which "term" structure should be the "current" one.  At
132  * the lowest level, there is a set of functions which allow a new
133  * "term" to be initialized or destroyed, and which allow this package,
134  * or a program, to access the special "hooks" defined for the current
135  * "term", and a set of functions which those "hooks" can use to inform
136  * this package of the results of certain occurances, for example, one
137  * such function allows this package to learn about user keypresses,
138  * detected by one of the special "hooks".
139  *
140  * We provide, among other things, the functions "Term_keypress()"
141  * to "react" to keypress events, and "Term_redraw()" to redraw the
142  * entire window, plus "Term_resize()" to note a new size.
143  *
144  *
145  * Note that the current "term" contains two "window images".  One of
146  * these images represents the "requested" contents of the "term", and
147  * the other represents the "actual" contents of the "term", at the time
148  * of the last performance of pending requests.  This package uses these
149  * two images to determine the "minimal" amount of work needed to make
150  * the "actual" contents of the "term" match the "requested" contents of
151  * the "term".  This method is not perfect, but it often reduces the
152  * amount of work needed to perform the pending requests, which thus
153  * increases the speed of the program itself.  This package promises
154  * that the requested changes will appear to occur either "all at once"
155  * or in a "top to bottom" order.  In addition, a "cursor" is maintained,
156  * and this cursor is updated along with the actual window contents.
157  *
158  * Currently, the "Term_fresh()" routine attempts to perform the "minimum"
159  * number of physical updates, in terms of total "work" done by the hooks
160  * Term_wipe(), Term_text(), and Term_pict(), making use of the fact that
161  * adjacent characters of the same color can both be drawn together using
162  * the "Term_text()" hook, and that "black" text can often be sent to the
163  * "Term_wipe()" hook instead of the "Term_text()" hook, and if something
164  * is already displayed in a window, then it is not necessary to display
165  * it again.  Unfortunately, this may induce slightly non-optimal results
166  * in some cases, in particular, those in which, say, a string of ten
167  * characters needs to be written, but the fifth character has already
168  * been displayed.  Currently, this will cause the "Term_text()" routine
169  * to be called once for each half of the string, instead of once for the
170  * whole string, which, on some machines, may be non-optimal behavior.
171  *
172  * The new formalism includes a "displayed" screen image (old) which
173  * is actually seen by the user, a "requested" screen image (scr)
174  * which is being prepared for display, a "memorized" screen image
175  * (mem) which is used to save and restore screen images, and a
176  * "temporary" screen image (tmp) which is currently unused.
177  *
178  *
179  * Several "flags" are available in each "term" to allow the underlying
180  * visual system (which initializes the "term" structure) to "optimize"
181  * the performance of this package for the given system, or to request
182  * certain behavior which is helpful/required for the given system.
183  *
184  * The "soft_cursor" flag indicates the use of a "soft" cursor, which
185  * only moves when explicitly requested,and which is "erased" when
186  * any characters are drawn on top of it.  This flag is used for all
187  * "graphic" systems which handle the cursor by "drawing" it.
188  *
189  * The "icky_corner" flag indicates that the bottom right "corner"
190  * of the windows are "icky", and "printing" anything there may
191  * induce "messy" behavior, such as "scrolling".  This flag is used
192  * for most old "dumb terminal" systems.
193  *
194  *
195  * The "term" structure contains the following function "hooks":
196  *
197  *   Term->init_hook = Init the term
198  *   Term->nuke_hook = Nuke the term
199  *   Term->user_hook = Perform user actions
200  *   Term->xtra_hook = Perform extra actions
201  *   Term->curs_hook = Draw (or Move) the cursor
202  *   Term->bigcurs_hook = Draw (or Move) the big cursor (bigtile mode)
203  *   Term->wipe_hook = Draw some blank spaces
204  *   Term->text_hook = Draw some text in the window
205  *   Term->pict_hook = Draw some attr/chars in the window
206  *
207  * The "Term->user_hook" hook provides a simple hook to an implementation
208  * defined function, with application defined semantics.  It is available
209  * to the program via the "Term_user()" function.
210  *
211  * The "Term->xtra_hook" hook provides a variety of different functions,
212  * based on the first parameter (which should be taken from the various
213  * TERM_XTRA_* defines) and the second parameter (which may make sense
214  * only for some first parameters).  It is available to the program via
215  * the "Term_xtra()" function, though some first parameters are only
216  * "legal" when called from inside this package.
217  *
218  * The "Term->curs_hook" hook provides this package with a simple way
219  * to "move" or "draw" the cursor to the grid "x,y", depending on the
220  * setting of the "soft_cursor" flag.  Note that the cursor is never
221  * redrawn if "nothing" has happened to the screen (even temporarily).
222  * This hook is required.
223  *
224  * The "Term->wipe_hook" hook provides this package with a simple way
225  * to "erase", starting at "x,y", the next "n" grids.  This hook assumes
226  * that the input is valid.  This hook is required, unless the setting
227  * of the "always_pict" or "always_text" flags makes it optional.
228  *
229  * The "Term->text_hook" hook provides this package with a simple way
230  * to "draw", starting at "x,y", the "n" chars contained in "cp", using
231  * the attr "a".  This hook assumes that the input is valid, and that
232  * "n" is between 1 and 256 inclusive, but it should NOT assume that
233  * the contents of "cp" are null-terminated.  This hook is required,
234  * unless the setting of the "always_pict" flag makes it optional.
235  *
236  * The "Term->pict_hook" hook provides this package with a simple way
237  * to "draw", starting at "x,y", the "n" attr/char pairs contained in
238  * the arrays "ap" and "cp".  This hook assumes that the input is valid,
239  * and that "n" is between 1 and 256 inclusive, but it should NOT assume
240  * that the contents of "cp" are null-terminated.  This hook is optional,
241  * unless the setting of the "always_pict" or "higher_pict" flags make
242  * it required.  Note that recently, this hook was changed from taking
243  * a byte "a" and a char "c" to taking a length "n", an array of bytes
244  * "ap" and an array of chars "cp".  Old implementations of this hook
245  * should now iterate over all "n" attr/char pairs.
246  *
247  *
248  * The game "Angband" uses a set of files called "main-xxx.c", for
249  * various "xxx" suffixes.  Most of these contain a function called
250  * "init_xxx()", that will prepare the underlying visual system for
251  * use with Angband, and then create one or more "term" structures,
252  * using flags and hooks appropriate to the given platform, so that
253  * the "main()" function can call one (or more) of the "init_xxx()"
254  * functions, as appropriate, to prepare the required "term" structs
255  * (one for each desired sub-window), and these "init_xxx()" functions
256  * are called from a centralized "main()" function in "main.c".  Other
257  * "main-xxx.c" systems contain their own "main()" function which, in
258  * addition to doing everything needed to initialize the actual program,
259  * also does everything that the normal "init_xxx()" functions would do.
260  *
261  * The game "Angband" defines, in addition to "attr 0", all of the
262  * attr codes from 1 to 15, using definitions in "defines.h", and
263  * thus the "main-xxx.c" files used by Angband must handle these
264  * attr values correctly.  Also, they must handle all other attr
265  * values, though they may do so in any way they wish, for example,
266  * by always taking every attr code mod 16.  Many of the "main-xxx.c"
267  * files use "white space" ("attr 1" / "char 32") to "erase" or "clear"
268  * any window, for efficiency.
269  *
270  * The game "Angband" uses the "Term_user" hook to allow any of the
271  * "main-xxx.c" files to interact with the user, by calling this hook
272  * whenever the user presses the "!" key when the game is waiting for
273  * a new command.  This could be used, for example, to provide "unix
274  * shell commands" to the Unix versions of the game.
275  *
276  * See "main-xxx.c" for a simple skeleton file which can be used to
277  * create a "visual system" for a new platform when porting Angband.
278  */
279
280
281
282
283
284
285 /*
286  * The current "term"
287  */
288 term *Term = NULL;
289
290
291
292
293 /*** Local routines ***/
294
295
296 /*
297  * Nuke a term_win (see below)
298  */
299 static errr term_win_nuke(term_win *s, int w, int h)
300 {
301         /* Free the window access arrays */
302         C_KILL(s->a, h, byte*);
303         C_KILL(s->c, h, char*);
304
305         /* Free the window content arrays */
306         C_KILL(s->va, h * w, byte);
307         C_KILL(s->vc, h * w, char);
308
309         /* Free the terrain access arrays */
310         C_KILL(s->ta, h, byte*);
311         C_KILL(s->tc, h, char*);
312
313         /* Free the terrain content arrays */
314         C_KILL(s->vta, h * w, byte);
315         C_KILL(s->vtc, h * w, char);
316
317         /* Success */
318         return (0);
319 }
320
321
322 /*
323  * Initialize a "term_win" (using the given window size)
324  */
325 static errr term_win_init(term_win *s, int w, int h)
326 {
327         int y;
328
329         /* Make the window access arrays */
330         C_MAKE(s->a, h, byte*);
331         C_MAKE(s->c, h, char*);
332
333         /* Make the window content arrays */
334         C_MAKE(s->va, h * w, byte);
335         C_MAKE(s->vc, h * w, char);
336
337         /* Make the terrain access arrays */
338         C_MAKE(s->ta, h, byte*);
339         C_MAKE(s->tc, h, char*);
340
341         /* Make the terrain content arrays */
342         C_MAKE(s->vta, h * w, byte);
343         C_MAKE(s->vtc, h * w, char);
344
345
346         /* Prepare the window access arrays */
347         for (y = 0; y < h; y++)
348         {
349                 s->a[y] = s->va + w * y;
350                 s->c[y] = s->vc + w * y;
351
352                 s->ta[y] = s->vta + w * y;
353                 s->tc[y] = s->vtc + w * y;
354         }
355
356         /* Success */
357         return (0);
358 }
359
360
361 /*
362  * Copy a "term_win" from another
363  */
364 static errr term_win_copy(term_win *s, term_win *f, int w, int h)
365 {
366         int x, y;
367
368         /* Copy contents */
369         for (y = 0; y < h; y++)
370         {
371                 byte *f_aa = f->a[y];
372                 char *f_cc = f->c[y];
373
374                 byte *s_aa = s->a[y];
375                 char *s_cc = s->c[y];
376
377                 byte *f_taa = f->ta[y];
378                 char *f_tcc = f->tc[y];
379
380                 byte *s_taa = s->ta[y];
381                 char *s_tcc = s->tc[y];
382
383                 for (x = 0; x < w; x++)
384                 {
385                         *s_aa++ = *f_aa++;
386                         *s_cc++ = *f_cc++;
387
388                         *s_taa++ = *f_taa++;
389                         *s_tcc++ = *f_tcc++;
390                 }
391         }
392
393         /* Copy cursor */
394         s->cx = f->cx;
395         s->cy = f->cy;
396         s->cu = f->cu;
397         s->cv = f->cv;
398
399         /* Success */
400         return (0);
401 }
402
403
404
405 /*** External hooks ***/
406
407
408 /*
409  * Execute the "Term->user_hook" hook, if available (see above).
410  */
411 errr Term_user(int n)
412 {
413         /* Verify the hook */
414         if (!Term->user_hook) return (-1);
415
416         /* Call the hook */
417         return ((*Term->user_hook)(n));
418 }
419
420 /*
421  * Execute the "Term->xtra_hook" hook, if available (see above).
422  */
423 errr Term_xtra(int n, int v)
424 {
425         /* Verify the hook */
426         if (!Term->xtra_hook) return (-1);
427
428         /* Call the hook */
429         return ((*Term->xtra_hook)(n, v));
430 }
431
432
433
434 /*** Fake hooks ***/
435
436
437 /*
438  * Hack -- fake hook for "Term_curs()" (see above)
439  */
440 static errr Term_curs_hack(int x, int y)
441 {
442         /* Unused */
443         (void)x;
444         (void)y;
445
446         /* Oops */
447         return (-1);
448 }
449
450 /*
451  * Hack -- fake hook for "Term_bigcurs()" (see above)
452  */
453 static errr Term_bigcurs_hack(int x, int y)
454 {
455         return (*Term->curs_hook)(x, y);
456 }
457
458 /*
459  * Hack -- fake hook for "Term_wipe()" (see above)
460  */
461 static errr Term_wipe_hack(int x, int y, int n)
462 {
463         /* Unused */
464         (void)x;
465         (void)y;
466         (void)n;
467
468         /* Oops */
469         return (-1);
470 }
471
472 /*
473  * Hack -- fake hook for "Term_text()" (see above)
474  */
475 static errr Term_text_hack(int x, int y, int n, byte a, cptr cp)
476 {
477         /* Unused */
478         (void)x;
479         (void)y;
480         (void)n;
481         (void)a;
482         (void)cp;
483
484         /* Oops */
485         return (-1);
486 }
487
488 /*
489  * Hack -- fake hook for "Term_pict()" (see above)
490  */
491 static errr Term_pict_hack(int x, int y, int n, const byte *ap, cptr cp, const byte *tap, cptr tcp)
492 {
493         /* Unused */
494         (void)x;
495         (void)y;
496         (void)n;
497         (void)ap;
498         (void)cp;
499         (void)tap;
500         (void)tcp;
501
502         /* Oops */
503         return (-1);
504 }
505
506
507
508 /*** Efficient routines ***/
509
510
511 /*
512  * Mentally draw an attr/char at a given location
513  *
514  * Assumes given location and values are valid.
515  */
516 void Term_queue_char(int x, int y, byte a, char c, byte ta, char tc)
517 {
518         term_win *scrn = Term->scr; 
519         
520         byte *scr_aa = &scrn->a[y][x];
521         char *scr_cc = &scrn->c[y][x];
522
523         byte *scr_taa = &scrn->ta[y][x];
524         char *scr_tcc = &scrn->tc[y][x];
525
526         /* Hack -- Ignore non-changes */
527         if ((*scr_aa == a) && (*scr_cc == c) &&
528                  (*scr_taa == ta) && (*scr_tcc == tc)) return;
529
530         /* Save the "literal" information */
531         *scr_aa = a;
532         *scr_cc = c;
533
534         *scr_taa = ta;
535         *scr_tcc = tc;
536
537         /* Check for new min/max row info */
538         if (y < Term->y1) Term->y1 = y;
539         if (y > Term->y2) Term->y2 = y;
540
541         /* Check for new min/max col info for this row */
542         if (x < Term->x1[y]) Term->x1[y] = x;
543         if (x > Term->x2[y]) Term->x2[y] = x;
544
545         if (((scrn->a[y][x] & AF_BIGTILE2) == AF_BIGTILE2) ||
546             (scrn->a[y][x] & AF_KANJI2))
547                 if ((x - 1) < Term->x1[y]) Term->x1[y]--;
548 }
549
550
551 /*
552  * Bigtile version of Term_queue_char().
553  *
554  * If use_bigtile is FALSE, simply call Term_queue_char().
555  *
556  * Otherwise, mentally draw a pair of attr/char at a given location.
557  *
558  * Assumes given location and values are valid.
559  */
560 void Term_queue_bigchar(int x, int y, byte a, char c, byte ta, char tc)
561 {
562
563 #ifdef JP
564         /*
565          * A table which relates each ascii character to a multibyte
566          * character.
567          *
568          * ¡Ö¢£¡×¤ÏÆóÇÜÉýƦÉå¤ÎÆâÉô¥³¡¼¥É¤Ë»ÈÍÑ¡£
569          */
570         static char ascii_to_zenkaku[] =
571                 "¡¡¡ª¡É¡ô¡ð¡ó¡õ¡Ç¡Ê¡Ë¡ö¡Ü¡¤¡Ý¡¥¡¿"
572                 "£°£±£²£³£´£µ£¶£·£¸£¹¡§¡¨¡ã¡á¡ä¡©"
573                 "¡÷£Á£Â£Ã£Ä£Å£Æ£Ç£È£É£Ê£Ë£Ì£Í£Î£Ï"
574                 "£Ð£Ñ£Ò£Ó£Ô£Õ£Ö£×£Ø£Ù£Ú¡Î¡À¡Ï¡°¡²"
575                 "¡Æ£á£â£ã£ä£å£æ£ç£è£é£ê£ë£ì£í£î£ï"
576                 "£ð£ñ£ò£ó£ô£õ£ö£÷£ø£ù£ú¡Ð¡Ã¡Ñ¡Á¢£";
577 #endif
578
579         byte a2;
580         char c2;
581
582         /* If non bigtile mode, call orginal function */
583         if (!use_bigtile)
584         {
585                 Term_queue_char(x, y, a, c, ta, tc);
586                 return;
587         }
588
589         /* A tile becomes a Bigtile */
590         if ((a & AF_TILE1) && (c & 0x80))
591         {
592                 /* Mark it as a Bigtile */
593                 a2 = AF_BIGTILE2;
594
595                 c2 = -1;
596
597                 /* Ignore non-tile background */
598                 if (!((ta & AF_TILE1) && (tc & 0x80)))
599                 {
600                         ta = 0;
601                         tc = 0;
602                 }
603         }
604
605 #ifdef JP
606         /*
607          * Use a multibyte character instead of a dirty pair of ASCII
608          * characters.
609          */
610         else if (' ' <= c) /* isprint(c) */
611         {
612                 c2 = ascii_to_zenkaku[2 * (c - ' ') + 1];
613                 c = ascii_to_zenkaku[2 * (c - ' ')];
614
615                 /* Mark it as a Kanji */
616                 a2 = a | AF_KANJI2;
617                 a |= AF_KANJI1;
618         }
619 #endif
620
621         else
622         {
623                 /* Dirty pair of ASCII characters */
624                 a2 = TERM_WHITE;
625                 c2 = ' ';
626         }
627
628         /* Display pair of attr/char */
629         Term_queue_char(x, y, a, c, ta, tc);
630         Term_queue_char(x + 1, y, a2, c2, 0, 0);
631 }
632
633
634 /*
635  * Mentally draw a string of attr/chars at a given location
636  *
637  * Assumes given location and values are valid.
638  *
639  * This function is designed to be fast, with no consistancy checking.
640  * It is used to update the map in the game.
641  */
642 void Term_queue_line(int x, int y, int n, byte *a, char *c, byte *ta, char *tc)
643 {
644         term_win *scrn = Term->scr;
645
646         int x1 = -1;
647         int x2 = -1;
648
649         byte *scr_aa = &scrn->a[y][x];
650         char *scr_cc = &scrn->c[y][x];
651
652         byte *scr_taa = &scrn->ta[y][x];
653         char *scr_tcc = &scrn->tc[y][x];
654
655         while (n--)
656         {
657                 /* Hack -- Ignore non-changes */
658                 if ((*scr_aa == *a) && (*scr_cc == *c) &&
659                         (*scr_taa == *ta) && (*scr_tcc == *tc))
660                 {
661                         x++;
662                         a++;
663                         c++;
664                         ta++;
665                         tc++;
666                         scr_aa++;
667                         scr_cc++;
668                         scr_taa++;
669                         scr_tcc++;
670                         continue;
671                 }
672
673                 /* Save the "literal" information */
674                 *scr_taa++ = *ta++;
675                 *scr_tcc++ = *tc++;
676
677                 /* Save the "literal" information */
678                 *scr_aa++ = *a++;
679                 *scr_cc++ = *c++;
680
681                 /* Track minimum changed column */
682                 if (x1 < 0) x1 = x;
683
684                 /* Track maximum changed column */
685                 x2 = x;
686
687                 x++;
688         }
689
690         /* Expand the "change area" as needed */
691         if (x1 >= 0)
692         {
693                 /* Check for new min/max row info */
694                 if (y < Term->y1) Term->y1 = y;
695                 if (y > Term->y2) Term->y2 = y;
696
697                 /* Check for new min/max col info in this row */
698                 if (x1 < Term->x1[y]) Term->x1[y] = x1;
699                 if (x2 > Term->x2[y]) Term->x2[y] = x2;
700         }
701 }
702
703
704
705 /*
706  * Mentally draw some attr/chars at a given location
707  *
708  * Assumes that (x,y) is a valid location, that the first "n" characters
709  * of the string "s" are all valid (non-zero), and that (x+n-1,y) is also
710  * a valid location, so the first "n" characters of "s" can all be added
711  * starting at (x,y) without causing any illegal operations.
712  */
713 void Term_queue_chars(int x, int y, int n, byte a, cptr s)
714 {
715         int x1 = -1, x2 = -1;
716
717         byte *scr_aa = Term->scr->a[y];
718 #ifdef JP
719         char *scr_cc = Term->scr->c[y];
720
721         byte *scr_taa = Term->scr->ta[y];
722         char *scr_tcc = Term->scr->tc[y];
723 #else
724         char *scr_cc = Term->scr->c[y];
725
726         byte *scr_taa = Term->scr->ta[y];
727         char *scr_tcc = Term->scr->tc[y];
728 #endif
729
730
731 #ifdef JP
732         /* É½¼¨Ê¸»ú¤Ê¤· */
733         if (n == 0 || *s == 0) return;
734         /*
735          * Á´³Ñʸ»ú¤Î±¦È¾Ê¬¤«¤éʸ»ú¤òɽ¼¨¤¹¤ë¾ì¹ç¡¢
736          * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Îº¸Éôʬ¤ò¾Ãµî¡£
737          * É½¼¨³«»Ï°ÌÃÖ¤¬º¸Ã¼¤Ç¤Ê¤¤¤È²¾Äê¡£
738          */
739         if ((scr_aa[x] & AF_KANJI2) && (scr_aa[x] & AF_BIGTILE2) != AF_BIGTILE2)
740         {
741                 scr_cc[x - 1] = ' ';
742                 scr_aa[x - 1] &= AF_KANJIC;
743                 x1 = x2 = x - 1;
744         }
745 #endif
746         /* Queue the attr/chars */
747         for ( ; n; x++, s++, n--)
748         {
749 #ifdef JP
750                 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ­¤¬¤¢¤ë */
751                 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
752 /* check */
753                 if (!(a & AF_TILE1) && iskanji(*s))
754                 {
755                         char nc1 = *s++;
756                         char nc2 = *s;
757
758                         byte na1 = (a | AF_KANJI1);
759                         byte na2 = (a | AF_KANJI2);
760
761                         if((--n == 0) || !nc2) break;
762
763                         if(scr_aa[x++] == na1 && scr_aa[x] == na2 &&
764                            scr_cc[x - 1] == nc1 && scr_cc[x] == nc2 &&
765                            (scr_taa[x - 1] == 0) && (scr_taa[x]==0) &&
766                            (scr_tcc[x - 1] == 0) && (scr_tcc[x]==0)    )
767                                 continue;
768
769                         scr_aa[x - 1] = na1;
770                         scr_aa[x] = na2;
771                         scr_cc[x - 1] = nc1;
772                         scr_cc[x] = nc2;
773
774                         if(x1 < 0) x1 = x - 1;
775                         x2 = x;
776                 }
777                 else
778                 {
779 #endif
780                 byte oa = scr_aa[x];
781                 char oc = scr_cc[x];
782
783                 byte ota = scr_taa[x];
784                 char otc = scr_tcc[x];
785
786                 /* Hack -- Ignore non-changes */
787                 if ((oa == a) && (oc == *s) && (ota == 0) && (otc == 0)) continue;
788
789                 /* Save the "literal" information */
790                 scr_aa[x] = a;
791                 scr_cc[x] = *s;
792
793                 scr_taa[x] = 0;
794                 scr_tcc[x] = 0;
795
796                 /* Note the "range" of window updates */
797                 if (x1 < 0) x1 = x;
798                 x2 = x;
799 #ifdef JP
800         }
801 #endif
802         }
803
804 #ifdef JP
805         /*
806          * Á´³Ñʸ»ú¤Îº¸È¾Ê¬¤Çɽ¼¨¤ò½ªÎ»¤¹¤ë¾ì¹ç¡¢
807          * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Î±¦Éôʬ¤ò¾Ãµî¡£
808          * (¾ò·ïÄɲ᧥¿¥¤¥ë¤Î1ʸ»úÌܤǤʤ¤»ö¤ò³Î¤«¤á¤ë¤è¤¦¤Ë¡£)
809          */
810         {
811
812                 int w, h;
813                 Term_get_size(&w, &h);
814                 if (x != w && !(scr_aa[x] & AF_TILE1) && (scr_aa[x] & AF_KANJI2))
815                 {
816                         scr_cc[x] = ' ';
817                         scr_aa[x] &= AF_KANJIC;
818                         if (x1 < 0) x1 = x;
819                         x2 = x;
820                 }
821         }
822 #endif
823         /* Expand the "change area" as needed */
824         if (x1 >= 0)
825         {
826                 /* Check for new min/max row info */
827                 if (y < Term->y1) Term->y1 = y;
828                 if (y > Term->y2) Term->y2 = y;
829
830                 /* Check for new min/max col info in this row */
831                 if (x1 < Term->x1[y]) Term->x1[y] = x1;
832                 if (x2 > Term->x2[y]) Term->x2[y] = x2;
833         }
834 }
835
836
837
838 /*** Refresh routines ***/
839
840
841 /*
842  * Flush a row of the current window (see "Term_fresh")
843  *
844  * Display text using "Term_pict()"
845  */
846 static void Term_fresh_row_pict(int y, int x1, int x2)
847 {
848         int x;
849
850         byte *old_aa = Term->old->a[y];
851         char *old_cc = Term->old->c[y];
852
853         byte *scr_aa = Term->scr->a[y];
854         char *scr_cc = Term->scr->c[y];
855
856         byte *old_taa = Term->old->ta[y];
857         char *old_tcc = Term->old->tc[y];
858
859         byte *scr_taa = Term->scr->ta[y];
860         char *scr_tcc = Term->scr->tc[y];
861
862         byte ota;
863         char otc;
864
865         byte nta;
866         char ntc;
867
868
869         /* Pending length */
870         int fn = 0;
871
872         /* Pending start */
873         int fx = 0;
874
875         byte oa;
876         char oc;
877
878         byte na;
879         char nc;
880
881 #ifdef JP
882         /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
883         int kanji = 0;
884 #endif
885         /* Scan "modified" columns */
886         for (x = x1; x <= x2; x++)
887         {
888                 /* See what is currently here */
889                 oa = old_aa[x];
890                 oc = old_cc[x];
891
892                 /* See what is desired there */
893                 na = scr_aa[x];
894                 nc = scr_cc[x];
895
896 #ifdef JP
897                 if (kanji)
898                 {
899                         /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
900                         kanji = 0;
901                         old_aa[x] = na;
902                         old_cc[x] = nc;
903                         fn++;
904                         continue;
905                 }
906                 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ­¤¬¤¢¤ë */
907                 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
908 /* check */
909                 kanji = (iskanji(nc) && !(na & AF_TILE1));
910 #endif
911
912                 ota = old_taa[x];
913                 otc = old_tcc[x];
914
915                 nta = scr_taa[x];
916                 ntc = scr_tcc[x];
917
918                 /* Handle unchanged grids */
919 #ifdef JP
920                 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc)
921                     &&(!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
922                                   scr_cc[x + 1] == old_cc[x + 1] &&
923                                   scr_taa[x + 1] == old_taa[x + 1] &&
924                                   scr_tcc[x + 1] == old_tcc[x + 1])))
925 #else
926                 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc))
927 #endif
928                 {
929                         /* Flush */
930                         if (fn)
931                         {
932                                 /* Draw pending attr/char pairs */
933                                 (void)((*Term->pict_hook)(fx, y, fn,
934                                        &scr_aa[fx], &scr_cc[fx],&scr_taa[fx], &scr_tcc[fx]));
935
936                                 /* Forget */
937                                 fn = 0;
938                         }
939
940 #ifdef JP
941                         /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
942                         if(kanji)
943                         {
944                                 x++;
945                                 fx++;
946                                 kanji = 0;
947                         }
948 #endif
949                         /* Skip */
950                         continue;
951                 }
952                 /* Save new contents */
953                 old_aa[x] = na;
954                 old_cc[x] = nc;
955
956                 old_taa[x] = nta;
957                 old_tcc[x] = ntc;
958
959                 /* Restart and Advance */
960                 if (fn++ == 0) fx = x;
961         }
962
963         /* Flush */
964         if (fn)
965         {
966                 /* Draw pending attr/char pairs */
967                 (void)((*Term->pict_hook)(fx, y, fn,
968                         &scr_aa[fx], &scr_cc[fx], &scr_taa[fx], &scr_tcc[fx]));
969         }
970 }
971
972
973
974 /*
975  * Flush a row of the current window (see "Term_fresh")
976  *
977  * Display text using "Term_text()" and "Term_wipe()",
978  * but use "Term_pict()" for high-bit attr/char pairs
979  */
980 static void Term_fresh_row_both(int y, int x1, int x2)
981 {
982         int x;
983
984         byte *old_aa = Term->old->a[y];
985         char *old_cc = Term->old->c[y];
986
987         byte *scr_aa = Term->scr->a[y];
988         char *scr_cc = Term->scr->c[y];
989
990         byte *old_taa = Term->old->ta[y];
991         char *old_tcc = Term->old->tc[y];
992         byte *scr_taa = Term->scr->ta[y];
993         char *scr_tcc = Term->scr->tc[y];
994
995         byte ota;
996         char otc;
997         byte nta;
998         char ntc;
999
1000         /* The "always_text" flag */
1001         int always_text = Term->always_text;
1002
1003         /* Pending length */
1004         int fn = 0;
1005
1006         /* Pending start */
1007         int fx = 0;
1008
1009         /* Pending attr */
1010         byte fa = Term->attr_blank;
1011
1012         byte oa;
1013         char oc;
1014
1015         byte na;
1016         char nc;
1017
1018 #ifdef JP
1019         /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
1020         int kanji = 0;
1021 #endif
1022         /* Scan "modified" columns */
1023         for (x = x1; x <= x2; x++)
1024         {
1025                 /* See what is currently here */
1026                 oa = old_aa[x];
1027                 oc = old_cc[x];
1028
1029                 /* See what is desired there */
1030                 na = scr_aa[x];
1031                 nc = scr_cc[x];
1032
1033 #ifdef JP
1034                 if (kanji)
1035                 {
1036                         /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
1037                         kanji = 0;
1038                         old_aa[x] = na;
1039                         old_cc[x] = nc;
1040                         fn++;
1041                         continue;
1042                 }
1043                 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ­¤¬¤¢¤ë */
1044                 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
1045 /* check */
1046 /*              kanji = (iskanji(nc));  */
1047                 kanji = (iskanji(nc) && !(na & AF_TILE1));
1048 #endif
1049
1050                 ota = old_taa[x];
1051                 otc = old_tcc[x];
1052
1053                 nta = scr_taa[x];
1054                 ntc = scr_tcc[x];
1055
1056                 /* Handle unchanged grids */
1057 #ifdef JP
1058                 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc)&&
1059                     (!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
1060                                 scr_cc[x + 1] == old_cc[x + 1] &&
1061                                 scr_taa[x + 1] == old_taa[x + 1] &&
1062                                 scr_tcc[x + 1] == old_tcc[x + 1])))
1063 #else
1064                 if ((na == oa) && (nc == oc) && (nta == ota) && (ntc == otc))
1065 #endif
1066                 {
1067                         /* Flush */
1068                         if (fn)
1069                         {
1070                                 /* Draw pending chars (normal) */
1071                                 if (fa || always_text)
1072                                 {
1073                                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1074                                 }
1075
1076                                 /* Draw pending chars (black) */
1077                                 else
1078                                 {
1079                                         (void)((*Term->wipe_hook)(fx, y, fn));
1080                                 }
1081
1082                                 /* Forget */
1083                                 fn = 0;
1084                         }
1085
1086 #ifdef JP
1087                         /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
1088                         if(kanji)
1089                         {
1090                                 x++;
1091                                 fx++;
1092                                 kanji = 0;
1093                         }
1094 #endif
1095                         /* Skip */
1096                         continue;
1097                 }
1098
1099                 /* Save new contents */
1100                 old_aa[x] = na;
1101                 old_cc[x] = nc;
1102
1103                 old_taa[x] = nta;
1104                 old_tcc[x] = ntc;
1105
1106                 /* 2nd byte of bigtile */
1107                 if ((na & AF_BIGTILE2) == AF_BIGTILE2) continue;
1108
1109                 /* Handle high-bit attr/chars */
1110                 if ((na & AF_TILE1) && (nc & 0x80))
1111                 {
1112                         /* Flush */
1113                         if (fn)
1114                         {
1115                                 /* Draw pending chars (normal) */
1116                                 if (fa || always_text)
1117                                 {
1118                                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1119                                 }
1120
1121                                 /* Draw pending chars (black) */
1122                                 else
1123                                 {
1124                                         (void)((*Term->wipe_hook)(fx, y, fn));
1125                                 }
1126
1127                                 /* Forget */
1128                                 fn = 0;
1129                         }
1130
1131                         /* Hack -- Draw the special attr/char pair */
1132                         (void)((*Term->pict_hook)(x, y, 1, &na, &nc, &nta, &ntc));
1133
1134                         /* Skip */
1135                         continue;
1136                 }
1137
1138                 /* Notice new color */
1139 #ifdef JP
1140                 if (fa != (na & AF_KANJIC))
1141 #else
1142                 if (fa != na)
1143 #endif
1144
1145                 {
1146                         /* Flush */
1147                         if (fn)
1148                         {
1149                                 /* Draw the pending chars */
1150                                 if (fa || always_text)
1151                                 {
1152                                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1153                                 }
1154
1155                                 /* Hack -- Erase "leading" spaces */
1156                                 else
1157                                 {
1158                                         (void)((*Term->wipe_hook)(fx, y, fn));
1159                                 }
1160
1161                                 /* Forget */
1162                                 fn = 0;
1163                         }
1164
1165                         /* Save the new color */
1166 #ifdef JP
1167                         fa = (na & AF_KANJIC);
1168 #else
1169                         fa = na;
1170 #endif
1171
1172                 }
1173
1174                 /* Restart and Advance */
1175                 if (fn++ == 0) fx = x;
1176         }
1177
1178         /* Flush */
1179         if (fn)
1180         {
1181                 /* Draw pending chars (normal) */
1182                 if (fa || always_text)
1183                 {
1184                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1185                 }
1186
1187                 /* Draw pending chars (black) */
1188                 else
1189                 {
1190                         (void)((*Term->wipe_hook)(fx, y, fn));
1191                 }
1192         }
1193 }
1194
1195
1196 /*
1197  * Flush a row of the current window (see "Term_fresh")
1198  *
1199  * Display text using "Term_text()" and "Term_wipe()"
1200  */
1201 static void Term_fresh_row_text(int y, int x1, int x2)
1202 {
1203         int x;
1204
1205         byte *old_aa = Term->old->a[y];
1206         char *old_cc = Term->old->c[y];
1207
1208         byte *scr_aa = Term->scr->a[y];
1209         char *scr_cc = Term->scr->c[y];
1210
1211         /* The "always_text" flag */
1212         int always_text = Term->always_text;
1213
1214         /* Pending length */
1215         int fn = 0;
1216
1217         /* Pending start */
1218         int fx = 0;
1219
1220         /* Pending attr */
1221         byte fa = Term->attr_blank;
1222
1223         byte oa;
1224         char oc;
1225
1226         byte na;
1227         char nc;
1228
1229 #ifdef JP
1230         /* Á´³Ñʸ»ú¤Î£²¥Ð¥¤¥ÈÌܤ«¤É¤¦¤« */
1231         int kanji = 0;
1232
1233         for (x = 0; x < x1; x++)
1234                 if (!(old_aa[x] & AF_TILE1) && iskanji(old_cc[x]))
1235                 {
1236                         if (x == x1 - 1)
1237                         {
1238                                 x1--;
1239                                 break;
1240                         }
1241                         else
1242                                 x++;
1243                 }
1244 #endif
1245         /* Scan "modified" columns */
1246         for (x = x1; x <= x2; x++)
1247         {
1248                 /* See what is currently here */
1249                 oa = old_aa[x];
1250                 oc = old_cc[x];
1251
1252                 /* See what is desired there */
1253                 na = scr_aa[x];
1254                 nc = scr_cc[x];
1255
1256 #ifdef JP
1257                 if (kanji)
1258                 {
1259                         /* Á´³Ñʸ»ú£²¥Ð¥¤¥ÈÌÜ */
1260                         kanji = 0;
1261                         old_aa[x] = na;
1262                         old_cc[x] = nc;
1263                         fn++;
1264                         continue;
1265                 }
1266                 /* Æüìʸ»ú¤È¤·¤ÆMSB¤¬Î©¤Ã¤Æ¤¤¤ë²ÄǽÀ­¤¬¤¢¤ë */
1267                 /* ¤½¤Î¾ì¹çattr¤ÎMSB¤âΩ¤Ã¤Æ¤¤¤ë¤Î¤Ç¤³¤ì¤Ç¼±Ê̤¹¤ë */
1268 /* check */
1269                 kanji = (iskanji(nc) && !(na & AF_TILE1));
1270 #endif
1271                 /* Handle unchanged grids */
1272 #ifdef JP
1273                 if ((na == oa) && (nc == oc) &&
1274                     (!kanji || (scr_aa[x + 1] == old_aa[x + 1] &&
1275                                 scr_cc[x + 1] == old_cc[x + 1])))
1276 #else
1277                 if ((na == oa) && (nc == oc))
1278 #endif
1279
1280                 {
1281                         /* Flush */
1282                         if (fn)
1283                         {
1284                                 /* Draw pending chars (normal) */
1285                                 if (fa || always_text)
1286                                 {
1287                                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1288                                 }
1289
1290                                 /* Draw pending chars (black) */
1291                                 else
1292                                 {
1293                                         (void)((*Term->wipe_hook)(fx, y, fn));
1294                                 }
1295
1296                                 /* Forget */
1297                                 fn = 0;
1298                         }
1299
1300 #ifdef JP
1301                         /* Á´³Ñʸ»ú¤Î»þ¤ÏºÆ³«°ÌÃ֤ϡܣ± */
1302                         if(kanji)
1303                         {
1304                                 x++;
1305                                 fx++;
1306                                 kanji = 0;
1307                         }
1308 #endif
1309                         /* Skip */
1310                         continue;
1311                 }
1312
1313                 /* Save new contents */
1314                 old_aa[x] = na;
1315                 old_cc[x] = nc;
1316
1317                 /* Notice new color */
1318 #ifdef JP
1319                 if (fa != (na & AF_KANJIC))
1320 #else
1321                 if (fa != na)
1322 #endif
1323
1324                 {
1325                         /* Flush */
1326                         if (fn)
1327                         {
1328                                 /* Draw the pending chars */
1329                                 if (fa || always_text)
1330                                 {
1331                                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1332                                 }
1333
1334                                 /* Hack -- Erase "leading" spaces */
1335                                 else
1336                                 {
1337                                         (void)((*Term->wipe_hook)(fx, y, fn));
1338                                 }
1339
1340                                 /* Forget */
1341                                 fn = 0;
1342                         }
1343
1344                         /* Save the new color */
1345 #ifdef JP
1346                         fa = (na & AF_KANJIC);
1347 #else
1348                         fa = na;
1349 #endif
1350
1351                 }
1352
1353                 /* Restart and Advance */
1354                 if (fn++ == 0) fx = x;
1355         }
1356
1357         /* Flush */
1358         if (fn)
1359         {
1360                 /* Draw pending chars (normal) */
1361                 if (fa || always_text)
1362                 {
1363                         (void)((*Term->text_hook)(fx, y, fn, fa, &scr_cc[fx]));
1364                 }
1365
1366                 /* Draw pending chars (black) */
1367                 else
1368                 {
1369                         (void)((*Term->wipe_hook)(fx, y, fn));
1370                 }
1371         }
1372 }
1373
1374
1375
1376
1377
1378 /*
1379  * Actually perform all requested changes to the window
1380  *
1381  * If absolutely nothing has changed, not even temporarily, or if the
1382  * current "Term" is not mapped, then this function will return 1 and
1383  * do absolutely nothing.
1384  *
1385  * Note that when "soft_cursor" is true, we erase the cursor (if needed)
1386  * whenever anything has changed, and redraw it (if needed) after all of
1387  * the screen updates are complete.  This will induce a small amount of
1388  * "cursor flicker" but only when the screen has been updated.  If the
1389  * screen is updated and then restored, you may still get this flicker.
1390  *
1391  * When "soft_cursor" is not true, we make the cursor invisible before
1392  * doing anything else if it is supposed to be invisible by the time we
1393  * are done, and we make it visible after moving it to its final location
1394  * after all of the screen updates are complete.
1395  *
1396  * Note that "Term_xtra(TERM_XTRA_CLEAR,0)" must erase the entire screen,
1397  * including the cursor, if needed, and may place the cursor anywhere.
1398  *
1399  * Note that "Term_xtra(TERM_XTRA_FROSH,y)" will be always be called
1400  * after any row "y" has been "flushed", unless the "Term->never_frosh"
1401  * flag is set, and "Term_xtra(TERM_XTRA_FRESH,0)" will be called after
1402  * all of the rows have been "flushed".
1403  *
1404  * Note the use of three different functions to handle the actual flush,
1405  * based on the settings of the "Term->always_pict" and "Term->higher_pict"
1406  * flags (see below).
1407  *
1408  * The three helper functions (above) work by collecting similar adjacent
1409  * grids into stripes, and then sending each stripe to "Term->pict_hook",
1410  * "Term->text_hook", or "Term->wipe_hook", based on the settings of the
1411  * "Term->always_pict" and "Term->higher_pict" flags, which select which
1412  * of the helper functions to call to flush each row.
1413  *
1414  * The helper functions currently "skip" any grids which already contain
1415  * the desired contents.  This may or may not be the best method, especially
1416  * when the desired content fits nicely into the current stripe.  For example,
1417  * it might be better to go ahead and queue them while allowed, but keep a
1418  * count of the "trailing skipables", then, when time to flush, or when a
1419  * "non skippable" is found, force a flush if there are too many skippables.
1420  *
1421  * Perhaps an "initialization" stage, where the "text" (and "attr")
1422  * buffers are "filled" with information, converting "blanks" into
1423  * a convenient representation, and marking "skips" with "zero chars",
1424  * and then some "processing" is done to determine which chars to skip.
1425  *
1426  * Currently, the helper functions are optimal for systems which prefer
1427  * to "print a char + move a char + print a char" to "print three chars",
1428  * and for applications that do a lot of "detailed" color printing.
1429  *
1430  * In the two "queue" functions, total "non-changes" are "pre-skipped".
1431  * The helper functions must also handle situations in which the contents
1432  * of a grid are changed, but then changed back to the original value,
1433  * and situations in which two grids in the same row are changed, but
1434  * the grids between them are unchanged.
1435  *
1436  * If the "Term->always_pict" flag is set, then "Term_fresh_row_pict()"
1437  * will be used instead of "Term_fresh_row_text()".  This allows all the
1438  * modified grids to be collected into stripes of attr/char pairs, which
1439  * are then sent to the "Term->pict_hook" hook, which can draw these pairs
1440  * in whatever way it would like.
1441  *
1442  * If the "Term->higher_pict" flag is set, then "Term_fresh_row_both()"
1443  * will be used instead of "Term_fresh_row_text()".  This allows all the
1444  * "special" attr/char pairs (in which both the attr and char have the
1445  * high-bit set) to be sent (one pair at a time) to the "Term->pict_hook"
1446  * hook, which can draw these pairs in whatever way it would like.
1447  *
1448  * Normally, the "Term_wipe()" function is used only to display "blanks"
1449  * that were induced by "Term_clear()" or "Term_erase()", and then only
1450  * if the "attr_blank" and "char_blank" fields have not been redefined
1451  * to use "white space" instead of the default "black space".  Actually,
1452  * the "Term_wipe()" function is used to display all "black" text, such
1453  * as the default "spaces" created by "Term_clear()" and "Term_erase()".
1454  *
1455  * Note that the "Term->always_text" flag will disable the use of the
1456  * "Term_wipe()" function hook entirely, and force all text, even text
1457  * drawn in the color "black", to be explicitly drawn.  This is useful
1458  * for machines which implement "Term_wipe()" by just drawing spaces.
1459  *
1460  * Note that the "Term->always_pict" flag will disable the use of the
1461  * "Term_wipe()" function entirely, and force everything, even text
1462  * drawn in the attr "black", to be explicitly drawn.
1463  *
1464  * Note that if no "black" text is ever drawn, and if "attr_blank" is
1465  * not "zero", then the "Term_wipe" hook will never be used, even if
1466  * the "Term->always_text" flag is not set.
1467  *
1468  * This function does nothing unless the "Term" is "mapped", which allows
1469  * certain systems to optimize the handling of "closed" windows.
1470  *
1471  * On systems with a "soft" cursor, we must explicitly erase the cursor
1472  * before flushing the output, if needed, to prevent a "jumpy" refresh.
1473  * The actual method for this is horrible, but there is very little that
1474  * we can do to simplify it efficiently.  XXX XXX XXX
1475  *
1476  * On systems with a "hard" cursor, we will "hide" the cursor before
1477  * flushing the output, if needed, to avoid a "flickery" refresh.  It
1478  * would be nice to *always* hide the cursor during the refresh, but
1479  * this might be expensive (and/or ugly) on some machines.
1480  *
1481  * The "Term->icky_corner" flag is used to avoid calling "Term_wipe()"
1482  * or "Term_pict()" or "Term_text()" on the bottom right corner of the
1483  * window, which might induce "scrolling" or other nasty stuff on old
1484  * dumb terminals.  This flag is handled very efficiently.  We assume
1485  * that the "Term_curs()" call will prevent placing the cursor in the
1486  * corner, if needed, though I doubt such placement is ever a problem.
1487  * Currently, the use of "Term->icky_corner" and "Term->soft_cursor"
1488  * together may result in undefined behavior.
1489  */
1490 errr Term_fresh(void)
1491 {
1492         int x, y;
1493
1494         int w = Term->wid;
1495         int h = Term->hgt;
1496
1497         int y1 = Term->y1;
1498         int y2 = Term->y2;
1499
1500         term_win *old = Term->old;
1501         term_win *scr = Term->scr;
1502
1503
1504         /* Do nothing unless "mapped" */
1505         if (!Term->mapped_flag) return (1);
1506
1507
1508         /* Trivial Refresh */
1509         if ((y1 > y2) &&
1510             (scr->cu == old->cu) &&
1511             (scr->cv == old->cv) &&
1512             (scr->cx == old->cx) &&
1513             (scr->cy == old->cy) &&
1514             !(Term->total_erase))
1515         {
1516                 /* Nothing */
1517                 return (1);
1518         }
1519
1520
1521         /* Handle "total erase" */
1522         if (Term->total_erase)
1523         {
1524                 byte na = Term->attr_blank;
1525                 char nc = Term->char_blank;
1526
1527                 /* Physically erase the entire window */
1528                 Term_xtra(TERM_XTRA_CLEAR, 0);
1529
1530                 /* Hack -- clear all "cursor" data */
1531                 old->cv = old->cu = old->cx = old->cy = 0;
1532
1533                 /* Wipe each row */
1534                 for (y = 0; y < h; y++)
1535                 {
1536                         byte *aa = old->a[y];
1537                         char *cc = old->c[y];
1538
1539                         byte *taa = old->ta[y];
1540                         char *tcc = old->tc[y];
1541
1542
1543                         /* Wipe each column */
1544                         for (x = 0; x < w; x++)
1545                         {
1546                                 /* Wipe each grid */
1547                                 *aa++ = na;
1548                                 *cc++ = nc;
1549
1550                                 *taa++ = na;
1551                                 *tcc++ = nc;
1552                         }
1553                 }
1554
1555                 /* Redraw every row */
1556                 Term->y1 = y1 = 0;
1557                 Term->y2 = y2 = h - 1;
1558
1559                 /* Redraw every column */
1560                 for (y = 0; y < h; y++)
1561                 {
1562                         Term->x1[y] = 0;
1563                         Term->x2[y] = w - 1;
1564                 }
1565
1566                 /* Forget "total erase" */
1567                 Term->total_erase = FALSE;
1568         }
1569
1570
1571         /* Cursor update -- Erase old Cursor */
1572         if (Term->soft_cursor)
1573         {
1574                 /* Cursor was visible */
1575                 if (!old->cu && old->cv)
1576                 {
1577                         int csize = 1;
1578                         int tx = old->cx;
1579                         int ty = old->cy;
1580
1581                         byte *old_aa = old->a[ty];
1582                         char *old_cc = old->c[ty];
1583
1584                         byte *old_taa = old->ta[ty];
1585                         char *old_tcc = old->tc[ty];
1586
1587                         byte ota = old_taa[tx];
1588                         char otc = old_tcc[tx];
1589
1590 #ifdef JP
1591                         if (tx + 1 < Term->wid && !(old_aa[tx] & AF_TILE1)
1592                             && iskanji(old_cc[tx]))
1593                                 csize = 2;
1594 #endif
1595                         /* Hack -- use "Term_pict()" always */
1596                         if (Term->always_pict)
1597                         {
1598                                 (void)((*Term->pict_hook)(tx, ty, csize, &old_aa[tx], &old_cc[tx], &ota, &otc));
1599                         }
1600
1601                         /* Hack -- use "Term_pict()" sometimes */
1602                         else if (Term->higher_pict && (old_aa[tx] & AF_TILE1) && (old_cc[tx] & 0x80))
1603                         {
1604                                 (void)((*Term->pict_hook)(tx, ty, 1, &old_aa[tx], &old_cc[tx], &ota, &otc));
1605                         }
1606
1607                         /* Hack -- restore the actual character */
1608                         else if (old_aa[tx] || Term->always_text)
1609                         {
1610                                 (void)((*Term->text_hook)(tx, ty, csize, (unsigned char) (old_aa[tx] & 0xf), &old_cc[tx]));
1611                         }
1612
1613                         /* Hack -- erase the grid */
1614                         else
1615                         {
1616                                 (void)((*Term->wipe_hook)(tx, ty, 1));
1617                         }
1618                 }
1619         }
1620
1621         /* Cursor Update -- Erase old Cursor */
1622         else
1623         {
1624                 /* Cursor will be invisible */
1625                 if (scr->cu || !scr->cv)
1626                 {
1627                         /* Make the cursor invisible */
1628                         Term_xtra(TERM_XTRA_SHAPE, 0);
1629                 }
1630         }
1631
1632
1633         /* Something to update */
1634         if (y1 <= y2)
1635         {
1636                 /* Handle "icky corner" */
1637                 if (Term->icky_corner)
1638                 {
1639                         /* Avoid the corner */
1640                         if (y2 >= h - 1)
1641                         {
1642                                 /* Avoid the corner */
1643                                 if (Term->x2[h - 1] > w - 2)
1644                                 {
1645                                         /* Avoid the corner */
1646                                         Term->x2[h - 1] = w - 2;
1647                                 }
1648                         }
1649                 }
1650
1651
1652                 /* Scan the "modified" rows */
1653                 for (y = y1; y <= y2; ++y)
1654                 {
1655                         int x1 = Term->x1[y];
1656                         int x2 = Term->x2[y];
1657
1658                         /* Flush each "modified" row */
1659                         if (x1 <= x2)
1660                         {
1661                                 /* Always use "Term_pict()" */
1662                                 if (Term->always_pict)
1663                                 {
1664                                         /* Flush the row */
1665                                         Term_fresh_row_pict(y, x1, x2);
1666                                 }
1667
1668                                 /* Sometimes use "Term_pict()" */
1669                                 else if (Term->higher_pict)
1670                                 {
1671                                         /* Flush the row */
1672                                         Term_fresh_row_both(y, x1, x2);
1673                                 }
1674
1675                                 /* Never use "Term_pict()" */
1676                                 else
1677                                 {
1678                                         /* Flush the row */
1679                                         Term_fresh_row_text(y, x1, x2);
1680                                 }
1681
1682                                 /* This row is all done */
1683                                 Term->x1[y] = w;
1684                                 Term->x2[y] = 0;
1685
1686                                 /* Hack -- Flush that row (if allowed) */
1687                                 if (!Term->never_frosh) Term_xtra(TERM_XTRA_FROSH, y);
1688                         }
1689                 }
1690
1691                 /* No rows are invalid */
1692                 Term->y1 = h;
1693                 Term->y2 = 0;
1694         }
1695
1696
1697         /* Cursor update -- Show new Cursor */
1698         if (Term->soft_cursor)
1699         {
1700                 /* Draw the cursor */
1701                 if (!scr->cu && scr->cv)
1702                 {
1703 #ifdef JP
1704                         if ((scr->cx + 1 < w) &&
1705                             ((old->a[scr->cy][scr->cx + 1] & AF_BIGTILE2) == AF_BIGTILE2 ||
1706                              (!(old->a[scr->cy][scr->cx] & AF_TILE1) &&
1707                               iskanji(old->c[scr->cy][scr->cx]))))
1708 #else
1709                         if ((scr->cx + 1 < w) && (old->a[scr->cy][scr->cx + 1] & AF_BIGTILE2) == AF_BIGTILE2)
1710 #endif
1711                         {
1712                                 /* Double width cursor for the Bigtile mode */
1713                                 (void)((*Term->bigcurs_hook)(scr->cx, scr->cy));
1714                         }
1715                         else
1716                         {
1717                                 /* Call the cursor display routine */
1718                                 (void)((*Term->curs_hook)(scr->cx, scr->cy));
1719                         }
1720                 }
1721         }
1722
1723         /* Cursor Update -- Show new Cursor */
1724         else
1725         {
1726                 /* The cursor is useless, hide it */
1727                 if (scr->cu)
1728                 {
1729                         /* Paranoia -- Put the cursor NEAR where it belongs */
1730                         (void)((*Term->curs_hook)(w - 1, scr->cy));
1731
1732                         /* Make the cursor invisible */
1733                         /* Term_xtra(TERM_XTRA_SHAPE, 0); */
1734                 }
1735
1736                 /* The cursor is invisible, hide it */
1737                 else if (!scr->cv)
1738                 {
1739                         /* Paranoia -- Put the cursor where it belongs */
1740                         (void)((*Term->curs_hook)(scr->cx, scr->cy));
1741
1742                         /* Make the cursor invisible */
1743                         /* Term_xtra(TERM_XTRA_SHAPE, 0); */
1744                 }
1745
1746                 /* The cursor is visible, display it correctly */
1747                 else
1748                 {
1749                         /* Put the cursor where it belongs */
1750                         (void)((*Term->curs_hook)(scr->cx, scr->cy));
1751
1752                         /* Make the cursor visible */
1753                         Term_xtra(TERM_XTRA_SHAPE, 1);
1754                 }
1755         }
1756
1757
1758         /* Save the "cursor state" */
1759         old->cu = scr->cu;
1760         old->cv = scr->cv;
1761         old->cx = scr->cx;
1762         old->cy = scr->cy;
1763
1764
1765         /* Actually flush the output */
1766         Term_xtra(TERM_XTRA_FRESH, 0);
1767
1768
1769         /* Success */
1770         return (0);
1771 }
1772
1773
1774
1775 /*** Output routines ***/
1776
1777
1778 /*
1779  * Set the cursor visibility
1780  */
1781 errr Term_set_cursor(int v)
1782 {
1783         /* Already done */
1784         if (Term->scr->cv == v) return (1);
1785
1786         /* Change */
1787         Term->scr->cv = v;
1788
1789         /* Success */
1790         return (0);
1791 }
1792
1793
1794 /*
1795  * Place the cursor at a given location
1796  *
1797  * Note -- "illegal" requests do not move the cursor.
1798  */
1799 errr Term_gotoxy(int x, int y)
1800 {
1801         int w = Term->wid;
1802         int h = Term->hgt;
1803
1804         /* Verify */
1805         if ((x < 0) || (x >= w)) return (-1);
1806         if ((y < 0) || (y >= h)) return (-1);
1807
1808         /* Remember the cursor */
1809         Term->scr->cx = x;
1810         Term->scr->cy = y;
1811
1812         /* The cursor is not useless */
1813         Term->scr->cu = 0;
1814
1815         /* Success */
1816         return (0);
1817 }
1818
1819
1820 /*
1821  * At a given location, place an attr/char
1822  * Do not change the cursor position
1823  * No visual changes until "Term_fresh()".
1824  */
1825 errr Term_draw(int x, int y, byte a, char c)
1826 {
1827         int w = Term->wid;
1828         int h = Term->hgt;
1829
1830         /* Verify location */
1831         if ((x < 0) || (x >= w)) return (-1);
1832         if ((y < 0) || (y >= h)) return (-1);
1833
1834         /* Paranoia -- illegal char */
1835         if (!c) return (-2);
1836
1837         /* Queue it for later */
1838         Term_queue_char(x, y, a, c, 0, 0);
1839
1840         /* Success */
1841         return (0);
1842 }
1843
1844
1845 /*
1846  * Using the given attr, add the given char at the cursor.
1847  *
1848  * We return "-2" if the character is "illegal". XXX XXX
1849  *
1850  * We return "-1" if the cursor is currently unusable.
1851  *
1852  * We queue the given attr/char for display at the current
1853  * cursor location, and advance the cursor to the right,
1854  * marking it as unuable and returning "1" if it leaves
1855  * the screen, and otherwise returning "0".
1856  *
1857  * So when this function, or the following one, return a
1858  * positive value, future calls to either function will
1859  * return negative ones.
1860  */
1861 errr Term_addch(byte a, char c)
1862 {
1863         int w = Term->wid;
1864
1865         /* Handle "unusable" cursor */
1866         if (Term->scr->cu) return (-1);
1867
1868         /* Paranoia -- no illegal chars */
1869         if (!c) return (-2);
1870
1871         /* Queue the given character for display */
1872         Term_queue_char(Term->scr->cx, Term->scr->cy, a, c, 0, 0);
1873
1874         /* Advance the cursor */
1875         Term->scr->cx++;
1876
1877         /* Success */
1878         if (Term->scr->cx < w) return (0);
1879
1880         /* Note "Useless" cursor */
1881         Term->scr->cu = 1;
1882
1883         /* Note "Useless" cursor */
1884         return (1);
1885 }
1886
1887
1888 /*
1889  * Bigtile version of Term_addch().
1890  *
1891  * If use_bigtile is FALSE, simply call Term_addch() .
1892  *
1893  * Otherwise, queue a pair of attr/char for display at the current
1894  * cursor location, and advance the cursor to the right by two.
1895  */
1896 errr Term_add_bigch(byte a, char c)
1897 {
1898         if (!use_bigtile) return Term_addch(a, c);
1899
1900         /* Handle "unusable" cursor */
1901         if (Term->scr->cu) return (-1);
1902
1903         /* Paranoia -- no illegal chars */
1904         if (!c) return (-2);
1905
1906         /* Queue the given character for display */
1907         Term_queue_bigchar(Term->scr->cx, Term->scr->cy, a, c, 0, 0);
1908
1909         /* Advance the cursor */
1910         Term->scr->cx += 2;
1911
1912         /* Success */
1913         if (Term->scr->cx < Term->wid) return (0);
1914
1915         /* Note "Useless" cursor */
1916         Term->scr->cu = 1;
1917
1918         /* Note "Useless" cursor */
1919         return (1);
1920 }
1921
1922
1923 /*
1924  * At the current location, using an attr, add a string
1925  *
1926  * We also take a length "n", using negative values to imply
1927  * the largest possible value, and then we use the minimum of
1928  * this length and the "actual" length of the string as the
1929  * actual number of characters to attempt to display, never
1930  * displaying more characters than will actually fit, since
1931  * we do NOT attempt to "wrap" the cursor at the screen edge.
1932  *
1933  * We return "-1" if the cursor is currently unusable.
1934  * We return "N" if we were "only" able to write "N" chars,
1935  * even if all of the given characters fit on the screen,
1936  * and mark the cursor as unusable for future attempts.
1937  *
1938  * So when this function, or the preceding one, return a
1939  * positive value, future calls to either function will
1940  * return negative ones.
1941  */
1942 errr Term_addstr(int n, byte a, cptr s)
1943 {
1944         int k;
1945
1946         int w = Term->wid;
1947
1948         errr res = 0;
1949
1950         /* Handle "unusable" cursor */
1951         if (Term->scr->cu) return (-1);
1952
1953         /* Obtain maximal length */
1954         k = (n < 0) ? (w + 1) : n;
1955
1956         /* Obtain the usable string length */
1957         for (n = 0; (n < k) && s[n]; n++) /* loop */;
1958
1959         /* React to reaching the edge of the screen */
1960         if (Term->scr->cx + n >= w) res = n = w - Term->scr->cx;
1961
1962         /* Queue the first "n" characters for display */
1963         Term_queue_chars(Term->scr->cx, Term->scr->cy, n, a, s);
1964
1965         /* Advance the cursor */
1966         Term->scr->cx += n;
1967
1968         /* Hack -- Notice "Useless" cursor */
1969         if (res) Term->scr->cu = 1;
1970
1971         /* Success (usually) */
1972         return (res);
1973 }
1974
1975
1976 /*
1977  * Move to a location and, using an attr, add a char
1978  */
1979 errr Term_putch(int x, int y, byte a, char c)
1980 {
1981         errr res;
1982
1983         /* Move first */
1984         if ((res = Term_gotoxy(x, y)) != 0) return (res);
1985
1986         /* Then add the char */
1987         if ((res = Term_addch(a, c)) != 0) return (res);
1988
1989         /* Success */
1990         return (0);
1991 }
1992
1993
1994 /*
1995  * Move to a location and, using an attr, add a string
1996  */
1997 errr Term_putstr(int x, int y, int n, byte a, cptr s)
1998 {
1999         errr res;
2000
2001         /* Move first */
2002         if ((res = Term_gotoxy(x, y)) != 0) return (res);
2003
2004         /* Then add the string */
2005         if ((res = Term_addstr(n, a, s)) != 0) return (res);
2006
2007         /* Success */
2008         return (0);
2009 }
2010
2011 #ifdef JP
2012 /*
2013  * Move to a location and, using an attr, add a string vertically
2014  */
2015 errr Term_putstr_v(int x, int y, int n, byte a, cptr s)
2016 {
2017         errr res;
2018         int i;
2019         int y0 = y;
2020
2021
2022         for (i = 0; i < n && s[i] != 0; i++)
2023         {
2024           /* Move first */
2025           if ((res = Term_gotoxy(x, y0)) != 0) return (res);
2026
2027           if (iskanji(s[i]))
2028           {
2029             if ((res = Term_addstr(2, a, &s[i])) != 0) return (res);
2030             i++;
2031             y0++;
2032             if (s[i] == 0) break;
2033           } else {
2034             if ((res = Term_addstr(1, a, &s[i])) != 0) return (res);
2035             y0++;
2036           }
2037         }
2038
2039         /* Success */
2040         return (0);
2041 }
2042 #endif
2043
2044 /*
2045  * Place cursor at (x,y), and clear the next "n" chars
2046  */
2047 errr Term_erase(int x, int y, int n)
2048 {
2049         int i;
2050
2051         int w = Term->wid;
2052         /* int h = Term->hgt; */
2053
2054         int x1 = -1;
2055         int x2 = -1;
2056
2057         int na = Term->attr_blank;
2058         int nc = Term->char_blank;
2059
2060         byte *scr_aa;
2061         char *scr_cc;
2062
2063         byte *scr_taa;
2064         char *scr_tcc;
2065
2066         /* Place cursor */
2067         if (Term_gotoxy(x, y)) return (-1);
2068
2069         /* Force legal size */
2070         if (x + n > w) n = w - x;
2071
2072         /* Fast access */
2073         scr_aa = Term->scr->a[y];
2074         scr_cc = Term->scr->c[y];
2075
2076         scr_taa = Term->scr->ta[y];
2077         scr_tcc = Term->scr->tc[y];
2078
2079 #ifdef JP
2080         /*
2081          * Á´³Ñʸ»ú¤Î±¦È¾Ê¬¤«¤éʸ»ú¤òɽ¼¨¤¹¤ë¾ì¹ç¡¢
2082          * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Îº¸Éôʬ¤ò¾Ãµî¡£
2083          */
2084         if (n > 0 && (((scr_aa[x] & AF_KANJI2) && !(scr_aa[x] & AF_TILE1))
2085                       || (scr_aa[x] & AF_BIGTILE2) == AF_BIGTILE2))
2086 #else
2087         if (n > 0 && (scr_aa[x] & AF_BIGTILE2) == AF_BIGTILE2)
2088 #endif
2089         {
2090                 x--;
2091                 n++;
2092         }
2093
2094         /* Scan every column */
2095         for (i = 0; i < n; i++, x++)
2096         {
2097                 int oa = scr_aa[x];
2098                 int oc = scr_cc[x];
2099
2100                 /* Hack -- Ignore "non-changes" */
2101                 if ((oa == na) && (oc == nc)) continue;
2102
2103 #ifdef JP
2104                 /*
2105                  * Á´³Ñʸ»ú¤Îº¸È¾Ê¬¤Çɽ¼¨¤ò½ªÎ»¤¹¤ë¾ì¹ç¡¢
2106                  * ½Å¤Ê¤Ã¤¿Ê¸»ú¤Î±¦Éôʬ¤ò¾Ãµî¡£
2107                  *
2108                  * 2001/04/29 -- Habu
2109                  * ¹Ô¤Î±¦Ã¼¤Î¾ì¹ç¤Ï¤³¤Î½èÍý¤ò¤·¤Ê¤¤¤è¤¦¤Ë½¤Àµ¡£
2110                  */
2111                 if ((oa & AF_KANJI1) && (i + 1) == n && x != w - 1)
2112                         n++;
2113 #endif
2114                 /* Save the "literal" information */
2115                 scr_aa[x] = na;
2116                 scr_cc[x] = nc;
2117
2118                 scr_taa[x] = 0;
2119                 scr_tcc[x] = 0;
2120
2121                 /* Track minimum changed column */
2122                 if (x1 < 0) x1 = x;
2123
2124                 /* Track maximum changed column */
2125                 x2 = x;
2126         }
2127
2128         /* Expand the "change area" as needed */
2129         if (x1 >= 0)
2130         {
2131                 /* Check for new min/max row info */
2132                 if (y < Term->y1) Term->y1 = y;
2133                 if (y > Term->y2) Term->y2 = y;
2134
2135                 /* Check for new min/max col info in this row */
2136                 if (x1 < Term->x1[y]) Term->x1[y] = x1;
2137                 if (x2 > Term->x2[y]) Term->x2[y] = x2;
2138         }
2139
2140         /* Success */
2141         return (0);
2142 }
2143
2144
2145 /*
2146  * Clear the entire window, and move to the top left corner
2147  *
2148  * Note the use of the special "total_erase" code
2149  */
2150 errr Term_clear(void)
2151 {
2152         int x, y;
2153
2154         int w = Term->wid;
2155         int h = Term->hgt;
2156
2157         byte na = Term->attr_blank;
2158         char nc = Term->char_blank;
2159
2160         /* Cursor usable */
2161         Term->scr->cu = 0;
2162
2163         /* Cursor to the top left */
2164         Term->scr->cx = Term->scr->cy = 0;
2165
2166         /* Wipe each row */
2167         for (y = 0; y < h; y++)
2168         {
2169                 byte *scr_aa = Term->scr->a[y];
2170                 char *scr_cc = Term->scr->c[y];
2171
2172                 byte *scr_taa = Term->scr->ta[y];
2173                 char *scr_tcc = Term->scr->tc[y];
2174
2175                 /* Wipe each column */
2176                 for (x = 0; x < w; x++)
2177                 {
2178                         scr_aa[x] = na;
2179                         scr_cc[x] = nc;
2180
2181                         scr_taa[x] = 0;
2182                         scr_tcc[x] = 0;
2183                 }
2184
2185                 /* This row has changed */
2186                 Term->x1[y] = 0;
2187                 Term->x2[y] = w - 1;
2188         }
2189
2190         /* Every row has changed */
2191         Term->y1 = 0;
2192         Term->y2 = h - 1;
2193
2194         /* Force "total erase" */
2195         Term->total_erase = TRUE;
2196
2197         /* Success */
2198         return (0);
2199 }
2200
2201
2202
2203
2204
2205 /*
2206  * Redraw (and refresh) the whole window.
2207  */
2208 errr Term_redraw(void)
2209 {
2210         /* Force "total erase" */
2211         Term->total_erase = TRUE;
2212
2213         /* Hack -- Refresh */
2214         Term_fresh();
2215
2216         /* Success */
2217         return (0);
2218 }
2219
2220
2221 /*
2222  * Redraw part of a widow.
2223  */
2224 errr Term_redraw_section(int x1, int y1, int x2, int y2)
2225 {
2226         int i, j;
2227
2228         char *c_ptr;
2229
2230         /* Bounds checking */
2231         if (y2 >= Term->hgt) y2 = Term->hgt - 1;
2232         if (x2 >= Term->wid) x2 = Term->wid - 1;
2233         if (y1 < 0) y1 = 0;
2234         if (x1 < 0) x1 = 0;
2235
2236         /* Set y limits */
2237         Term->y1 = y1;
2238         Term->y2 = y2;
2239
2240         /* Set the x limits */
2241         for (i = Term->y1; i <= Term->y2; i++)
2242         {
2243 #ifdef JP
2244                 int x1j = x1;
2245                 int x2j = x2;
2246    
2247                 if (x1j > 0)
2248                 {
2249                         if (Term->scr->a[i][x1j] & AF_KANJI2) x1j--;
2250                 }
2251    
2252                 if (x2j < Term->wid - 1)
2253                 {
2254                         if (Term->scr->a[i][x2j] & AF_KANJI1) x2j++;
2255                 }
2256    
2257                 Term->x1[i] = x1j;
2258                 Term->x2[i] = x2j;
2259    
2260                 c_ptr = Term->old->c[i];
2261    
2262                 /* Clear the section so it is redrawn */
2263                 for (j = x1j; j <= x2j; j++)
2264                 {
2265                         /* Hack - set the old character to "none" */
2266                         c_ptr[j] = 0;
2267                 }
2268 #else
2269                 Term->x1[i] = x1;
2270                 Term->x2[i] = x2;
2271
2272                 c_ptr = Term->old->c[i];
2273
2274                 /* Clear the section so it is redrawn */
2275                 for (j = x1; j <= x2; j++)
2276                 {
2277                         /* Hack - set the old character to "none" */
2278                         c_ptr[j] = 0;
2279                 }
2280 #endif
2281         }
2282
2283         /* Hack -- Refresh */
2284         Term_fresh();
2285
2286         /* Success */
2287         return (0);
2288 }
2289
2290
2291
2292 /*** Access routines ***/
2293
2294
2295 /*
2296  * Extract the cursor visibility
2297  */
2298 errr Term_get_cursor(int *v)
2299 {
2300         /* Extract visibility */
2301         (*v) = Term->scr->cv;
2302
2303         /* Success */
2304         return (0);
2305 }
2306
2307
2308 /*
2309  * Extract the current window size
2310  */
2311 errr Term_get_size(int *w, int *h)
2312 {
2313         /* Access the cursor */
2314         (*w) = Term->wid;
2315         (*h) = Term->hgt;
2316
2317         /* Success */
2318         return (0);
2319 }
2320
2321
2322 /*
2323  * Extract the current cursor location
2324  */
2325 errr Term_locate(int *x, int *y)
2326 {
2327         /* Access the cursor */
2328         (*x) = Term->scr->cx;
2329         (*y) = Term->scr->cy;
2330
2331         /* Warn about "useless" cursor */
2332         if (Term->scr->cu) return (1);
2333
2334         /* Success */
2335         return (0);
2336 }
2337
2338
2339 /*
2340  * At a given location, determine the "current" attr and char
2341  * Note that this refers to what will be on the window after the
2342  * next call to "Term_fresh()".  It may or may not already be there.
2343  */
2344 errr Term_what(int x, int y, byte *a, char *c)
2345 {
2346         int w = Term->wid;
2347         int h = Term->hgt;
2348
2349         /* Verify location */
2350         if ((x < 0) || (x >= w)) return (-1);
2351         if ((y < 0) || (y >= h)) return (-1);
2352
2353         /* Direct access */
2354         (*a) = Term->scr->a[y][x];
2355         (*c) = Term->scr->c[y][x];
2356
2357         /* Success */
2358         return (0);
2359 }
2360
2361
2362
2363 /*** Input routines ***/
2364
2365
2366 /*
2367  * Flush and forget the input
2368  */
2369 errr Term_flush(void)
2370 {
2371         /* Hack -- Flush all events */
2372         Term_xtra(TERM_XTRA_FLUSH, 0);
2373
2374         /* Forget all keypresses */
2375         Term->key_head = Term->key_tail = 0;
2376
2377         /* Success */
2378         return (0);
2379 }
2380
2381
2382
2383 /*
2384  * Add a keypress to the "queue"
2385  */
2386 errr Term_keypress(int k)
2387 {
2388         /* Hack -- Refuse to enqueue non-keys */
2389         if (!k) return (-1);
2390
2391         /* Store the char, advance the queue */
2392         Term->key_queue[Term->key_head++] = k;
2393
2394         /* Circular queue, handle wrap */
2395         if (Term->key_head == Term->key_size) Term->key_head = 0;
2396
2397         /* Success (unless overflow) */
2398         if (Term->key_head != Term->key_tail) return (0);
2399
2400 #if 0
2401         /* Hack -- Forget the oldest key */
2402         if (++Term->key_tail == Term->key_size) Term->key_tail = 0;
2403 #endif
2404
2405         /* Problem */
2406         return (1);
2407 }
2408
2409
2410 /*
2411  * Add a keypress to the FRONT of the "queue"
2412  */
2413 errr Term_key_push(int k)
2414 {
2415         /* Hack -- Refuse to enqueue non-keys */
2416         if (!k) return (-1);
2417
2418         /* Hack -- Overflow may induce circular queue */
2419         if (Term->key_tail == 0) Term->key_tail = Term->key_size;
2420
2421         /* Back up, Store the char */
2422         Term->key_queue[--Term->key_tail] = k;
2423
2424         /* Success (unless overflow) */
2425         if (Term->key_head != Term->key_tail) return (0);
2426
2427 #if 0
2428         /* Hack -- Forget the oldest key */
2429         if (++Term->key_tail == Term->key_size) Term->key_tail = 0;
2430 #endif
2431
2432         /* Problem */
2433         return (1);
2434 }
2435
2436
2437
2438
2439
2440 /*
2441  * Check for a pending keypress on the key queue.
2442  *
2443  * Store the keypress, if any, in "ch", and return "0".
2444  * Otherwise store "zero" in "ch", and return "1".
2445  *
2446  * Wait for a keypress if "wait" is true.
2447  *
2448  * Remove the keypress if "take" is true.
2449  */
2450 errr Term_inkey(char *ch, bool wait, bool take)
2451 {
2452         /* Assume no key */
2453         (*ch) = '\0';
2454
2455 #ifdef CHUUKEI
2456         flush_ringbuf();
2457 #endif
2458
2459         /* Hack -- get bored */
2460         if (!Term->never_bored)
2461         {
2462                 /* Process random events */
2463                 Term_xtra(TERM_XTRA_BORED, 0);
2464         }
2465
2466         /* Wait */
2467         if (wait)
2468         {
2469                 /* Process pending events while necessary */
2470                 while (Term->key_head == Term->key_tail)
2471                 {
2472                         /* Process events (wait for one) */
2473                         Term_xtra(TERM_XTRA_EVENT, TRUE);
2474                 }
2475         }
2476
2477         /* Do not Wait */
2478         else
2479         {
2480                 /* Process pending events if necessary */
2481                 if (Term->key_head == Term->key_tail)
2482                 {
2483                         /* Process events (do not wait) */
2484                         Term_xtra(TERM_XTRA_EVENT, FALSE);
2485                 }
2486         }
2487
2488         /* No keys are ready */
2489         if (Term->key_head == Term->key_tail) return (1);
2490
2491         /* Extract the next keypress */
2492         (*ch) = Term->key_queue[Term->key_tail];
2493
2494         /* If requested, advance the queue, wrap around if necessary */
2495         if (take && (++Term->key_tail == Term->key_size)) Term->key_tail = 0;
2496
2497         /* Success */
2498         return (0);
2499 }
2500
2501
2502
2503 /*** Extra routines ***/
2504
2505
2506 /*
2507  * Save the "requested" screen into the "memorized" screen
2508  *
2509  * Every "Term_save()" should match exactly one "Term_load()"
2510  */
2511 errr Term_save(void)
2512 {
2513         int w = Term->wid;
2514         int h = Term->hgt;
2515
2516         /* Create */
2517         if (!Term->mem)
2518         {
2519                 /* Allocate window */
2520                 MAKE(Term->mem, term_win);
2521
2522                 /* Initialize window */
2523                 term_win_init(Term->mem, w, h);
2524         }
2525
2526         /* Grab */
2527         term_win_copy(Term->mem, Term->scr, w, h);
2528
2529         /* Success */
2530         return (0);
2531 }
2532
2533
2534 /*
2535  * Restore the "requested" contents (see above).
2536  *
2537  * Every "Term_save()" should match exactly one "Term_load()"
2538  */
2539 errr Term_load(void)
2540 {
2541         int y;
2542
2543         int w = Term->wid;
2544         int h = Term->hgt;
2545
2546         /* Create */
2547         if (!Term->mem)
2548         {
2549                 /* Allocate window */
2550                 MAKE(Term->mem, term_win);
2551
2552                 /* Initialize window */
2553                 term_win_init(Term->mem, w, h);
2554         }
2555
2556         /* Load */
2557         term_win_copy(Term->scr, Term->mem, w, h);
2558
2559         /* Assume change */
2560         for (y = 0; y < h; y++)
2561         {
2562                 /* Assume change */
2563                 Term->x1[y] = 0;
2564                 Term->x2[y] = w - 1;
2565         }
2566
2567         /* Assume change */
2568         Term->y1 = 0;
2569         Term->y2 = h - 1;
2570
2571         /* Success */
2572         return (0);
2573 }
2574
2575
2576 /*
2577  * Exchange the "requested" screen with the "tmp" screen
2578  */
2579 errr Term_exchange(void)
2580 {
2581         int y;
2582
2583         int w = Term->wid;
2584         int h = Term->hgt;
2585
2586         term_win *exchanger;
2587
2588
2589         /* Create */
2590         if (!Term->tmp)
2591         {
2592                 /* Allocate window */
2593                 MAKE(Term->tmp, term_win);
2594
2595                 /* Initialize window */
2596                 term_win_init(Term->tmp, w, h);
2597         }
2598
2599         /* Swap */
2600         exchanger = Term->scr;
2601         Term->scr = Term->tmp;
2602         Term->tmp = exchanger;
2603
2604         /* Assume change */
2605         for (y = 0; y < h; y++)
2606         {
2607                 /* Assume change */
2608                 Term->x1[y] = 0;
2609                 Term->x2[y] = w - 1;
2610         }
2611
2612         /* Assume change */
2613         Term->y1 = 0;
2614         Term->y2 = h - 1;
2615
2616         /* Success */
2617         return (0);
2618 }
2619
2620
2621
2622 /*
2623  * React to a new physical window size.
2624  */
2625 errr Term_resize(int w, int h)
2626 {
2627         int i;
2628
2629         int wid, hgt;
2630
2631         byte *hold_x1;
2632         byte *hold_x2;
2633
2634         term_win *hold_old;
2635         term_win *hold_scr;
2636         term_win *hold_mem;
2637         term_win *hold_tmp;
2638
2639         /* Resizing is forbidden */
2640         if (Term->fixed_shape) return (-1);
2641
2642         /* Ignore illegal changes */
2643         if ((w < 1) || (h < 1)) return (-1);
2644
2645
2646         /* Ignore non-changes */
2647         if ((Term->wid == w) && (Term->hgt == h) && (arg_bigtile == use_bigtile))
2648                 return (1);
2649
2650         use_bigtile = arg_bigtile;
2651
2652         /* Minimum dimensions */
2653         wid = MIN(Term->wid, w);
2654         hgt = MIN(Term->hgt, h);
2655
2656         /* Save scanners */
2657         hold_x1 = Term->x1;
2658         hold_x2 = Term->x2;
2659
2660         /* Save old window */
2661         hold_old = Term->old;
2662
2663         /* Save old window */
2664         hold_scr = Term->scr;
2665
2666         /* Save old window */
2667         hold_mem = Term->mem;
2668
2669         /* Save old window */
2670         hold_tmp = Term->tmp;
2671
2672         /* Create new scanners */
2673         C_MAKE(Term->x1, h, byte);
2674         C_MAKE(Term->x2, h, byte);
2675
2676         /* Create new window */
2677         MAKE(Term->old, term_win);
2678
2679         /* Initialize new window */
2680         term_win_init(Term->old, w, h);
2681
2682         /* Save the contents */
2683         term_win_copy(Term->old, hold_old, wid, hgt);
2684
2685         /* Create new window */
2686         MAKE(Term->scr, term_win);
2687
2688         /* Initialize new window */
2689         term_win_init(Term->scr, w, h);
2690
2691         /* Save the contents */
2692         term_win_copy(Term->scr, hold_scr, wid, hgt);
2693
2694         /* If needed */
2695         if (hold_mem)
2696         {
2697                 /* Create new window */
2698                 MAKE(Term->mem, term_win);
2699
2700                 /* Initialize new window */
2701                 term_win_init(Term->mem, w, h);
2702
2703                 /* Save the contents */
2704                 term_win_copy(Term->mem, hold_mem, wid, hgt);
2705         }
2706
2707         /* If needed */
2708         if (hold_tmp)
2709         {
2710                 /* Create new window */
2711                 MAKE(Term->tmp, term_win);
2712
2713                 /* Initialize new window */
2714                 term_win_init(Term->tmp, w, h);
2715
2716                 /* Save the contents */
2717                 term_win_copy(Term->tmp, hold_tmp, wid, hgt);
2718         }
2719
2720         /* Free some arrays */
2721         C_KILL(hold_x1, Term->hgt, byte);
2722         C_KILL(hold_x2, Term->hgt, byte);
2723
2724         /* Nuke */
2725         term_win_nuke(hold_old, Term->wid, Term->hgt);
2726
2727         /* Kill */
2728         KILL(hold_old, term_win);
2729
2730         /* Illegal cursor */
2731         if (Term->old->cx >= w) Term->old->cu = 1;
2732         if (Term->old->cy >= h) Term->old->cu = 1;
2733
2734         /* Nuke */
2735         term_win_nuke(hold_scr, Term->wid, Term->hgt);
2736
2737         /* Kill */
2738         KILL(hold_scr, term_win);
2739
2740         /* Illegal cursor */
2741         if (Term->scr->cx >= w) Term->scr->cu = 1;
2742         if (Term->scr->cy >= h) Term->scr->cu = 1;
2743
2744         /* If needed */
2745         if (hold_mem)
2746         {
2747                 /* Nuke */
2748                 term_win_nuke(hold_mem, Term->wid, Term->hgt);
2749
2750                 /* Kill */
2751                 KILL(hold_mem, term_win);
2752
2753                 /* Illegal cursor */
2754                 if (Term->mem->cx >= w) Term->mem->cu = 1;
2755                 if (Term->mem->cy >= h) Term->mem->cu = 1;
2756         }
2757
2758         /* If needed */
2759         if (hold_tmp)
2760         {
2761                 /* Nuke */
2762                 term_win_nuke(hold_tmp, Term->wid, Term->hgt);
2763
2764                 /* Kill */
2765                 KILL(hold_tmp, term_win);
2766
2767                 /* Illegal cursor */
2768                 if (Term->tmp->cx >= w) Term->tmp->cu = 1;
2769                 if (Term->tmp->cy >= h) Term->tmp->cu = 1;
2770         }
2771
2772         /* Save new size */
2773         Term->wid = w;
2774         Term->hgt = h;
2775
2776         /* Force "total erase" */
2777         Term->total_erase = TRUE;
2778
2779         /* Assume change */
2780         for (i = 0; i < h; i++)
2781         {
2782                 /* Assume change */
2783                 Term->x1[i] = 0;
2784                 Term->x2[i] = w - 1;
2785         }
2786
2787         /* Assume change */
2788         Term->y1 = 0;
2789         Term->y2 = h - 1;
2790
2791         /* Execute the "resize_hook" hook, if available */
2792         if (Term->resize_hook)
2793         {
2794                 Term->resize_hook();
2795         }
2796
2797         /* Success */
2798         return (0);
2799 }
2800
2801
2802
2803 /*
2804  * Activate a new Term (and deactivate the current Term)
2805  *
2806  * This function is extremely important, and also somewhat bizarre.
2807  * It is the only function that should "modify" the value of "Term".
2808  *
2809  * To "create" a valid "term", one should do "term_init(t)", then
2810  * set the various flags and hooks, and then do "Term_activate(t)".
2811  */
2812 errr Term_activate(term *t)
2813 {
2814         /* Hack -- already done */
2815         if (Term == t) return (1);
2816
2817         /* Deactivate the old Term */
2818         if (Term) Term_xtra(TERM_XTRA_LEVEL, 0);
2819
2820         /* Hack -- Call the special "init" hook */
2821         if (t && !t->active_flag)
2822         {
2823                 /* Call the "init" hook */
2824                 if (t->init_hook) (*t->init_hook)(t);
2825
2826                 /* Remember */
2827                 t->active_flag = TRUE;
2828
2829                 /* Assume mapped */
2830                 t->mapped_flag = TRUE;
2831         }
2832
2833         /* Remember the Term */
2834         Term = t;
2835
2836         /* Activate the new Term */
2837         if (Term) Term_xtra(TERM_XTRA_LEVEL, 1);
2838
2839         /* Success */
2840         return (0);
2841 }
2842
2843
2844
2845 /*
2846  * Nuke a term
2847  */
2848 errr term_nuke(term *t)
2849 {
2850         int w = t->wid;
2851         int h = t->hgt;
2852
2853
2854         /* Hack -- Call the special "nuke" hook */
2855         if (t->active_flag)
2856         {
2857                 /* Call the "nuke" hook */
2858                 if (t->nuke_hook) (*t->nuke_hook)(t);
2859
2860                 /* Remember */
2861                 t->active_flag = FALSE;
2862
2863                 /* Assume not mapped */
2864                 t->mapped_flag = FALSE;
2865         }
2866
2867
2868         /* Nuke "displayed" */
2869         term_win_nuke(t->old, w, h);
2870
2871         /* Kill "displayed" */
2872         KILL(t->old, term_win);
2873
2874         /* Nuke "requested" */
2875         term_win_nuke(t->scr, w, h);
2876
2877         /* Kill "requested" */
2878         KILL(t->scr, term_win);
2879
2880         /* If needed */
2881         if (t->mem)
2882         {
2883                 /* Nuke "memorized" */
2884                 term_win_nuke(t->mem, w, h);
2885
2886                 /* Kill "memorized" */
2887                 KILL(t->mem, term_win);
2888         }
2889
2890         /* If needed */
2891         if (t->tmp)
2892         {
2893                 /* Nuke "temporary" */
2894                 term_win_nuke(t->tmp, w, h);
2895
2896                 /* Kill "temporary" */
2897                 KILL(t->tmp, term_win);
2898         }
2899
2900         /* Free some arrays */
2901         C_KILL(t->x1, h, byte);
2902         C_KILL(t->x2, h, byte);
2903
2904         /* Free the input queue */
2905         C_KILL(t->key_queue, t->key_size, char);
2906
2907         /* Success */
2908         return (0);
2909 }
2910
2911
2912 /*
2913  * Initialize a term, using a window of the given size.
2914  * Also prepare the "input queue" for "k" keypresses
2915  * By default, the cursor starts out "invisible"
2916  * By default, we "erase" using "black spaces"
2917  */
2918 errr term_init(term *t, int w, int h, int k)
2919 {
2920         int y;
2921
2922
2923         /* Wipe it */
2924         (void)WIPE(t, term);
2925
2926
2927         /* Prepare the input queue */
2928         t->key_head = t->key_tail = 0;
2929
2930         /* Determine the input queue size */
2931         t->key_size = k;
2932
2933         /* Allocate the input queue */
2934         C_MAKE(t->key_queue, t->key_size, char);
2935
2936
2937         /* Save the size */
2938         t->wid = w;
2939         t->hgt = h;
2940
2941         /* Allocate change arrays */
2942         C_MAKE(t->x1, h, byte);
2943         C_MAKE(t->x2, h, byte);
2944
2945
2946         /* Allocate "displayed" */
2947         MAKE(t->old, term_win);
2948
2949         /* Initialize "displayed" */
2950         term_win_init(t->old, w, h);
2951
2952
2953         /* Allocate "requested" */
2954         MAKE(t->scr, term_win);
2955
2956         /* Initialize "requested" */
2957         term_win_init(t->scr, w, h);
2958
2959
2960         /* Assume change */
2961         for (y = 0; y < h; y++)
2962         {
2963                 /* Assume change */
2964                 t->x1[y] = 0;
2965                 t->x2[y] = w - 1;
2966         }
2967
2968         /* Assume change */
2969         t->y1 = 0;
2970         t->y2 = h - 1;
2971
2972         /* Force "total erase" */
2973         t->total_erase = TRUE;
2974
2975
2976         /* Default "blank" */
2977         t->attr_blank = 0;
2978         t->char_blank = ' ';
2979
2980
2981         /* Prepare "fake" hooks to prevent core dumps */
2982         t->curs_hook = Term_curs_hack;
2983         t->bigcurs_hook = Term_bigcurs_hack;
2984         t->wipe_hook = Term_wipe_hack;
2985         t->text_hook = Term_text_hack;
2986         t->pict_hook = Term_pict_hack;
2987
2988
2989         /* Success */
2990         return (0);
2991 }
2992
2993