summaryrefslogtreecommitdiff
path: root/src/main-cap.c
blob: 6d44af2839bd6e1ceee82fe64820a895b26d70da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
/* File: main-cap.c */

/* Purpose: Support for "term.c" using "termcap" calls */

#include "angband.h"


#ifdef USE_CAP


/*
 * This file is a total hack, but is often very helpful.  :-)
 *
 * This file allows use of the terminal without requiring the
 * "curses" routines.  In fact, if "USE_HARDCODE" is defined,
 * this file will attempt to use various hard-coded "vt100"
 * escape sequences to also avoid the use of the "termcap"
 * routines.  I do not know if this will work on System V.
 *
 * This file is intended for use only on those machines which are
 * unable, for whatever reason, to compile the "main-gcu.c" file,
 * but which seem to be able to support the "termcap" library, or
 * which at least seem able to support "vt100" terminals.
 *
 * Large portions of this file were stolen from "main-gcu.c"
 *
 * This file incorrectly handles output to column 80, I think.
 */


/*
 * Require a "system"
 */
#if !defined(USE_TERMCAP) && !defined(USE_HARDCODE)
# define USE_TERMCAP
#endif

/*
 * Hack -- try to guess which systems use what commands
 * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
 * Mega-Hack -- try to guess when "POSIX" is available.
 * If the user defines two of these, we will probably crash.
 */
#if !defined(USE_TPOSIX)
# if !defined(USE_TERMIO) && !defined(USE_TCHARS)
# if defined(_POSIX_VERSION)
# define USE_TPOSIX
# else
# if defined(USG) || defined(linux) || defined(SOLARIS)
# define USE_TERMIO
# else
# define USE_TCHARS
# endif 
# endif 
# endif 
#endif



/*
 * POSIX stuff
 */
#ifdef USE_TPOSIX
# include <sys/ioctl.h>
# include <termios.h>
#endif

/*
 * One version needs these files
 */
#ifdef USE_TERMIO
# include <sys/ioctl.h>
# include <termio.h>
#endif

/*
 * The other needs these files
 */
#ifdef USE_TCHARS
# include <sys/ioctl.h>
# include <sys/resource.h>
# include <sys/param.h>
# include <sys/file.h>
# include <sys/types.h>
#endif


/*
 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
 *
 * They should both work due to the "(i != 1)" test in the code
 * which checks for the result of the "read()" command.
 */
#ifndef O_NDELAY
# define O_NDELAY O_NONBLOCK
#endif




#ifdef USE_TERMCAP

/*
 * Termcap string information
 */

static char blob[1024]; 		/* The "termcap" entry */
static char area[1024]; 		/* The string extraction buffer */
static char *next = area; 	/* The current "index" into "area" */
static char *desc; 		/* The terminal name */

#endif


/*
 * Pointers into the "area"
 */

static char *cm; 	/* Move cursor */
static char *ch; 	/* Move cursor to horizontal location */
static char *cv; 	/* Move cursor to vertical location */
static char *ho; 	/* Move cursor to top left */
static char *ll; 	/* Move cursor to bottom left */
static char *cs; 	/* Set scroll area */
static char *cl; 	/* Clear screen */
static char *cd; 	/* Clear to end of display */
static char *ce; 	/* Clear to end of line */
static char *cr; 	/* Move to start of line */
static char *so; 	/* Turn on standout */
static char *se; 	/* Turn off standout */
static char *md; 	/* Turn on bold */
static char *me; 	/* Turn off bold */
static char *vi; 	/* Cursor - invisible */
static char *ve; 	/* Cursor - normal */
static char *vs; 	/* Cursor - bright */


/*
 * State variables
 */

static int rows; 	/* Screen size (Y) */
static int cols; 	/* Screen size (X) */
static int curx; 	/* Cursor location (X) */
static int cury; 	/* Cursor location (Y) */
static int curv; 	/* Cursor visibility */


/*
 * Extern functions
 */
extern char *getenv();
extern char *tgoto();
extern char *tgetstr();


/*
 * Write some chars to the terminal
 */
static void ewrite(char *str)
{
	int numtowrite, numwritten;

	/* See how much work we have */
	numtowrite = strlen(str);

	/* Write until done */
	while (numtowrite > 0)
	{
		/* Try to write the chars */
		numwritten = write(1, str, numtowrite);

		/* Handle FIFOs and EINTR */
		if (numwritten < 0) numwritten = 0;

		/* See what we completed */
		numtowrite -= numwritten;
		str += numwritten;

		/* Hack -- sleep if not done */
		if (numtowrite > 0) sleep(1);
	}
}



#ifdef USE_TERMCAP

static char write_buffer[128];
static char *write_buffer_ptr;

static void output_one(char c)
{
	*write_buffer_ptr++ = c;
}

static void tp(char *s)
{
	/* Dump the string into us */
	write_buffer_ptr = write_buffer;

	/* Write the string with padding */
	tputs (s, 1, output_one);

	/* Finish the string */
	*write_buffer_ptr = '\0';

	/* Dump the recorded buffer */
	ewrite (write_buffer);
}

#endif

#ifdef USE_HARDCODE

static void tp(char *s)
{
	ewrite(s);
}

#endif







/*
 * Clear the screen
 */
static void do_cl(void)
{
	if (cl) tp (cl);
}

/*
 * Clear to the end of the line
 */
static void do_ce(void)
{
	if (ce) tp(ce);
}


/*
 * Set the cursor visibility (0 = invis, 1 = normal, 2 = bright)
 */
static void curs_set(int vis)
{
	char *v = NULL;

	if (!vis)
	{
		v = vi;
	}
	else if (vis > 1)
	{
		v = vs ? vs : ve;
	}
	else
	{
		v = ve ? ve : vs;
	}

	if (v) tp(v);
}



/*
 * Restrict scrolling to within these rows
 */
static void do_cs(int y1, int y2)
{

#ifdef USE_TERMCAP
	if (cs) tp(tgoto(cs, y2, y1));
#endif

#ifdef USE_HARDCODE
	char temp[64];
	sprintf(temp, cs, y1, y2);
	tp (temp);
#endif

}



/*
 * Go to the given screen location directly
 */
static void do_cm(int x, int y)
{

#ifdef USE_TERMCAP
	if (cm) tp(tgoto(cm, x, y));
#endif

#ifdef USE_HARDCODE
	char temp[64];
	sprintf(temp, cm, y + 1, x + 1);
	tp(temp);
#endif

}


/*
 * Go to the given screen location in a "clever" manner
 *
 * XXX XXX XXX This function could use some work!
 */
static void do_move(int x1, int y1, int x2, int y2)
{
	/* Hack -- unknown start location */
	if ((x1 == x2) && (y1 == y2)) do_cm(x2, y2);

	/* Left edge */
	else if (x2 == 0)
	{
		if ((y2 <= 0) && ho) tp(ho);
		else if ((y2 >= rows - 1) && ll) tp(ll);
		else if ((y2 == y1) && cr) tp(cr);
#if 0
		else if ((y2 == y1 + 1) && cr && dn)
		{
			tp(cr);
			tp(dn);
		}
		else if ((y2 == y1 - 1) && cr && up)
		{
			tp(cr);
			tp(up);
		}
#endif
		else do_cm(x2, y2);
	}

#if 0
	/* Up/Down one line */
	else if ((x2 == x1) && (y2 == y1 + 1) && dn) tp(dn);
	else if ((x2 == x1) && (y2 == y1 - 1) && up) tp(up);
#endif

	/* Default -- go directly there */
	else do_cm(x2, y2);
}




/*
 * Help initialize this file (see below)
 */
errr init_cap_aux(void)
{

#ifdef USE_TERMCAP

	/* Get the terminal name (if possible) */
	desc = getenv("TERM");
	if (!desc) return (1);

	/* Get the terminal info */
	if (tgetent(blob, desc) != 1) return (2);

	/* Get the (initial) columns and rows, or default */
	if ((cols = tgetnum("co")) == -1) cols = 80;
	if ((rows = tgetnum("li")) == -1) rows = 24;

	/* Find out how to move the cursor to a given location */
	cm = tgetstr("cm", &next);
	if (!cm) return (10);

	/* Find out how to move the cursor to a given position */
	ch = tgetstr("ch", &next);
	cv = tgetstr("cv", &next);

	/* Find out how to "home" the screen */
	ho = tgetstr("ho", &next);

	/* Find out how to "last-line" the screen */
	ll = tgetstr("ll", &next);

	/* Find out how to do a "carriage return" */
	cr = tgetstr("cr", &next);
	if (!cr) cr = "\r";

	/* Find out how to clear the screen */
	cl = tgetstr("cl", &next);
	if (!cl) return (11);

	/* Find out how to clear to the end of display */
	cd = tgetstr("cd", &next);

	/* Find out how to clear to the end of the line */
	ce = tgetstr("ce", &next);

	/* Find out how to scroll (set the scroll region) */
	cs = tgetstr("cs", &next);

	/* Find out how to hilite */
	so = tgetstr("so", &next);
	se = tgetstr("se", &next);
	if (!so || !se) so = se = NULL;

	/* Find out how to bold */
	md = tgetstr("md", &next);
	me = tgetstr("me", &next);
	if (!md || !me) md = me = NULL;

	/* Check the cursor visibility stuff */
	vi = tgetstr("vi", &next);
	vs = tgetstr("vs", &next);
	ve = tgetstr("ve", &next);

#endif

#ifdef USE_HARDCODE

	/* Assume some defualt information */
	rows = 24;
	cols = 80;

	/* Clear screen */
	cl = "\033[2J\033[H"; 	/* --]--]-- */

	/* Clear to end of line */
	ce = "\033[K"; 	/* --]-- */

	/* Hilite on/off */
	so = "\033[7m"; 	/* --]-- */
	se = "\033[m"; 	/* --]-- */

	/* Scroll region */
	cs = "\033[%d;%dr"; 	/* --]-- */

	/* Move cursor */
	cm = "\033[%d;%dH"; 	/* --]-- */

#endif

	/* Success */
	return (0);
}







/*
 * Save the "normal" and "angband" terminal settings
 */

#ifdef USE_TPOSIX

static struct termios norm_termios;

static struct termios game_termios;

#endif

#ifdef USE_TERMIO

static struct termio norm_termio;

static struct termio game_termio;

#endif

#ifdef USE_TCHARS

static struct sgttyb norm_ttyb;
static struct tchars norm_tchars;
static struct ltchars norm_ltchars;
static int norm_local_chars;

static struct sgttyb game_ttyb;
static struct tchars game_tchars;
static struct ltchars game_ltchars;
static int game_local_chars;

#endif



/*
 * Are we active?  Not really needed.
 */
static int active = FALSE;


/*
 * The main screen (no sub-screens)
 */
static term term_screen_body;



/*
 * Place the "keymap" into its "normal" state
 */
static void keymap_norm(void)
{

#ifdef USE_TPOSIX

	/* restore the saved values of the special chars */
	(void)tcsetattr(0, TCSAFLUSH, &norm_termios);

#endif

#ifdef USE_TERMIO

	/* restore the saved values of the special chars */
	(void)ioctl(0, TCSETA, (char *)&norm_termio);

#endif

#ifdef USE_TCHARS

	/* restore the saved values of the special chars */
	(void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
	(void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
	(void)ioctl(0, TIOCSLTC, (char *)&norm_ltchars);
	(void)ioctl(0, TIOCLSET, (char *)&norm_local_chars);

#endif

}


/*
 * Place the "keymap" into the "game" state
 */
static void keymap_game(void)
{

#ifdef USE_TPOSIX

	/* restore the saved values of the special chars */
	(void)tcsetattr(0, TCSAFLUSH, &game_termios);

#endif

#ifdef USE_TERMIO

	/* restore the saved values of the special chars */
	(void)ioctl(0, TCSETA, (char *)&game_termio);

#endif

#ifdef USE_TCHARS

	/* restore the saved values of the special chars */
	(void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
	(void)ioctl(0, TIOCSETC, (char *)&game_tchars);
	(void)ioctl(0, TIOCSLTC, (char *)&game_ltchars);
	(void)ioctl(0, TIOCLSET, (char *)&game_local_chars);

#endif

}


/*
 * Save the normal keymap
 */
static void keymap_norm_prepare(void)
{

#ifdef USE_TPOSIX

	/* Get the normal keymap */
	tcgetattr(0, &norm_termios);

#endif

#ifdef USE_TERMIO

	/* Get the normal keymap */
	(void)ioctl(0, TCGETA, (char *)&norm_termio);

#endif

#ifdef USE_TCHARS

	/* Get the normal keymap */
	(void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
	(void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
	(void)ioctl(0, TIOCGLTC, (char *)&norm_ltchars);
	(void)ioctl(0, TIOCLGET, (char *)&norm_local_chars);

#endif

}


/*
 * Save the keymaps (normal and game)
 */
static void keymap_game_prepare(void)
{

#ifdef USE_TPOSIX

	/* Acquire the current mapping */
	tcgetattr(0, &game_termios);

	/* Force "Ctrl-C" to interupt */
	game_termios.c_cc[VINTR] = (char)3;

	/* Force "Ctrl-Z" to suspend */
	game_termios.c_cc[VSUSP] = (char)26;

	/* Hack -- Leave "VSTART/VSTOP" alone */

	/* Disable the standard control characters */
	game_termios.c_cc[VQUIT] = (char) - 1;
	game_termios.c_cc[VERASE] = (char) - 1;
	game_termios.c_cc[VKILL] = (char) - 1;
	game_termios.c_cc[VEOF] = (char) - 1;
	game_termios.c_cc[VEOL] = (char) - 1;

	/* Normally, block until a character is read */
	game_termios.c_cc[VMIN] = 1;
	game_termios.c_cc[VTIME] = 0;

	/* Hack -- Turn off "echo" and "canonical" mode */
	game_termios.c_lflag &= ~(ECHO | ICANON);

#endif

#ifdef USE_TERMIO

	/* Acquire the current mapping */
	(void)ioctl(0, TCGETA, (char *)&game_termio);

	/* Force "Ctrl-C" to interupt */
	game_termio.c_cc[VINTR] = (char)3;

	/* Force "Ctrl-Z" to suspend */
	game_termio.c_cc[VSUSP] = (char)26;

	/* Hack -- Leave "VSTART/VSTOP" alone */

	/* Disable the standard control characters */
	game_termio.c_cc[VQUIT] = (char) - 1;
	game_termio.c_cc[VERASE] = (char) - 1;
	game_termio.c_cc[VKILL] = (char) - 1;
	game_termio.c_cc[VEOF] = (char) - 1;
	game_termio.c_cc[VEOL] = (char) - 1;

#if 0
	/* Disable the non-posix control characters */
	game_termio.c_cc[VEOL2] = (char) - 1;
	game_termio.c_cc[VSWTCH] = (char) - 1;
	game_termio.c_cc[VDSUSP] = (char) - 1;
	game_termio.c_cc[VREPRINT] = (char) - 1;
	game_termio.c_cc[VDISCARD] = (char) - 1;
	game_termio.c_cc[VWERASE] = (char) - 1;
	game_termio.c_cc[VLNEXT] = (char) - 1;
	game_termio.c_cc[VSTATUS] = (char) - 1;
#endif

	/* Normally, block until a character is read */
	game_termio.c_cc[VMIN] = 1;
	game_termio.c_cc[VTIME] = 0;

	/* Hack -- Turn off "echo" and "canonical" mode */
	game_termio.c_lflag &= ~(ECHO | ICANON);

#endif

#ifdef USE_TCHARS

	/* Get the default game characters */
	(void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
	(void)ioctl(0, TIOCGETC, (char *)&game_tchars);
	(void)ioctl(0, TIOCGLTC, (char *)&game_ltchars);
	(void)ioctl(0, TIOCLGET, (char *)&game_local_chars);

	/* Force interupt (^C) */
	game_tchars.t_intrc = (char)3;

	/* Force start/stop (^Q, ^S) */
	game_tchars.t_startc = (char)17;
	game_tchars.t_stopc = (char)19;

	/* Cancel some things */
	game_tchars.t_quitc = (char) - 1;
	game_tchars.t_eofc = (char) - 1;
	game_tchars.t_brkc = (char) - 1;

	/* Force suspend (^Z) */
	game_ltchars.t_suspc = (char)26;

	/* Cancel some things */
	game_ltchars.t_dsuspc = (char) - 1;
	game_ltchars.t_rprntc = (char) - 1;
	game_ltchars.t_flushc = (char) - 1;
	game_ltchars.t_werasc = (char) - 1;
	game_ltchars.t_lnextc = (char) - 1;

	/* XXX XXX XXX XXX Verify this before use */
	/* Hack -- Turn off "echo" and "canonical" mode */
	/* game_termios.c_lflag &= ~(ECHO | ICANON); */
	game_ttyb.flag &= ~(ECHO | ICANON);

#endif

}








/*
 * Suspend/Resume
 */
static errr Term_xtra_cap_alive(int v)
{
	/* Suspend */
	if (!v)
	{
		if (!active) return (1);

		/* Hack -- make sure the cursor is visible */
		curs_set(1);

		/* Move to bottom right */
		do_move(0, rows - 1, 0, rows - 1);

		/* Go to normal keymap mode */
		keymap_norm();

		/* No longer active */
		active = FALSE;
	}

	/* Resume */
	else
	{
		if (active) return (1);

		/* Hack -- restore the cursor location */
		do_move(curx, cury, curx, cury);

		/* Hack -- restore the cursor visibility */
		curs_set(curv);

		/* Go to angband keymap mode */
		keymap_game();

		/* Now we are active */
		active = TRUE;
	}

	/* Success */
	return (0);
}



/*
 * Process an event
 */
static errr Term_xtra_cap_event(int v)
{
	int i, arg;
	char buf[2];

	/* Wait */
	if (v)
	{
		/* Wait for one byte */
		i = read(0, buf, 1);

		/* Hack -- Handle "errors" */
		if ((i <= 0) && (errno != EINTR)) exit_game_panic();
	}

	/* Do not wait */
	else
	{
		/* Get the current flags for stdin */
		if ((arg = fcntl(0, F_GETFL, 0)) < 1) return (1);

		/* Tell stdin not to block */
		if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) return (1);

		/* Read one byte, if possible */
		i = read(0, buf, 1);

		/* Replace the flags for stdin */
		if (fcntl(0, F_SETFL, arg)) return (1);
	}

	/* No keys ready */
	if ((i != 1) || (!buf[0])) return (1);

	/* Enqueue the keypress */
	Term_keypress(buf[0]);

	/* Success */
	return (0);
}




/*
 * Actually move the hardware cursor
 */
static errr Term_curs_cap(int x, int y)
{
	/* Literally move the cursor */
	do_move(curx, cury, x, y);

	/* Save the cursor location */
	curx = x;
	cury = y;

	/* Success */
	return (0);
}


/*
 * Erase a grid of space
 *
 * XXX XXX XXX Note that we will never be asked to clear the
 * bottom line all the way to the bottom right edge, since we
 * have set the "avoid the bottom right corner" flag.
 */
static errr Term_wipe_cap(int x, int y, int n)
{
	int dx;

	/* Place the cursor */
	Term_curs_cap(x, y);

	/* Wipe to end of line */
	if (x + n >= 80)
	{
		do_ce();
	}

	/* Wipe region */
	else
	{
		for (dx = 0; dx < n; ++dx)
		{
			putc(' ', stdout);
			curx++;
		}
	}

	/* Success */
	return (0);
}


/*
 * Place some text on the screen using an attribute
 */
static errr Term_text_cap(int x, int y, int n, byte a, cptr s)
{
	int i;

	/* Move the cursor */
	Term_curs_cap(x, y);

	/* Dump the text, advance the cursor */
	for (i = 0; s[i]; i++)
	{
		/* Dump the char */
		putc(s[i], stdout);

		/* Advance cursor 'X', and wrap */
		if (++curx >= cols)
		{
			/* Reset cursor 'X' */
			curx = 0;

			/* Hack -- Advance cursor 'Y', and wrap */
			if (++cury == rows) cury = 0;
		}
	}

	/* Success */
	return (0);
}


/*
 * Handle a "special request"
 */
static errr Term_xtra_cap(int n, int v)
{
	/* Analyze the request */
	switch (n)
	{
		/* Clear the screen */
	case TERM_XTRA_CLEAR:
		do_cl();
		do_move(0, 0, 0, 0);
		return (0);

		/* Make a noise */
	case TERM_XTRA_NOISE:
		(void)write(1, "\007", 1);
		return (0);

		/* Change the cursor visibility */
	case TERM_XTRA_SHAPE:
		curv = v;
		curs_set(v);
		return (0);

		/* Suspend/Resume */
	case TERM_XTRA_ALIVE:
		return (Term_xtra_cap_alive(v));

		/* Process events */
	case TERM_XTRA_EVENT:
		return (Term_xtra_cap_event(v));

		/* Flush events */
	case TERM_XTRA_FLUSH:
		while (!Term_xtra_cap_event(FALSE));
		return (0);

		/* Delay */
	case TERM_XTRA_DELAY:
		usleep(1000 * v);
		return (0);
	}

	/* Not parsed */
	return (1);
}




/*
 * Init a "term" for this file
 */
static void Term_init_cap(term *t)
{
	if (active) return;

	/* Assume cursor at top left */
	curx = 0;
	cury = 0;

	/* Assume visible cursor */
	curv = 1;

	/* Clear the screen */
	do_cl();

	/* Hack -- visible cursor */
	curs_set(1);

	/* Assume active */
	active = TRUE;
}


/*
 * Nuke a "term" for this file
 */
static void Term_nuke_cap(term *t)
{
	if (!active) return;

	/* Hack -- make sure the cursor is visible */
	curs_set(1);

	/* Move to bottom right */
	do_move(0, rows - 1, 0, rows - 1);

	/* Normal keymap */
	keymap_norm();

	/* No longer active */
	active = FALSE;
}










/*
 * Prepare this file for Angband usage
 */
errr init_cap(void)
{
	term *t = &term_screen_body;


	/*** Initialize ***/

	/* Initialize the screen */
	if (init_cap_aux()) return ( -1);

	/* Hack -- Require large screen, or Quit with message */
	if ((rows < 24) || (cols < 80)) quit("Screen too small!");


	/*** Prepare to play ***/

	/* Extract the normal keymap */
	keymap_norm_prepare();

	/* Extract the game keymap */
	keymap_game_prepare();

	/* Hack -- activate the game keymap */
	keymap_game();

	/* Hack -- Do NOT buffer stdout */
	setbuf(stdout, NULL);


	/*** Now prepare the term ***/

	/* Initialize the term */
	term_init(t, 80, 24, 256);

	/* Avoid the bottom right corner */
	t->icky_corner = TRUE;

	/* Erase with "white space" */
	t->attr_blank = TERM_WHITE;
	t->char_blank = ' ';

	/* Set some hooks */
	t->init_hook = Term_init_cap;
	t->nuke_hook = Term_nuke_cap;

	/* Set some more hooks */
	t->text_hook = Term_text_cap;
	t->wipe_hook = Term_wipe_cap;
	t->curs_hook = Term_curs_cap;
	t->xtra_hook = Term_xtra_cap;

	/* Save the term */
	term_screen = t;

	/* Activate it */
	Term_activate(term_screen);

	/* Success */
	return (0);
}


#endif /* USE_CAP */