1 /* SCCS Id: @(#)wintty.c 3.4 2002/09/27 */
2 /* Copyright (c) David Cohrs, 1991 */
3 /* NetHack may be freely redistributed. See license for details. */
6 * Neither a standard out nor character-based control codes should be
7 * part of the "tty look" windowing implementation.
13 #ifdef SHORT_FILENAMES
16 #include "patchlevel.h"
22 # define MICRO /* The Mac is a MICRO only for this file, not in general! */
24 extern void msmsg(const char *,...);
35 #ifdef CLIPPING /* might want SIGWINCH */
36 # if defined(BSD) || defined(ULTRIX) || defined(AIX_31) || defined(_BULL_SOURCE)
41 extern char mapped_menu_cmds[]; /* from options.c */
43 /* Interface definition, for windows.c */
44 struct window_procs tty_procs = {
47 WC_TILED_MAP|WC_ASCII_MAP|
52 WC_COLOR|WC_HILITE_PET|WC_INVERSE|WC_EIGHT_BIT_IN,
59 tty_suspend_nhwindows,
80 tty_update_positionbar,
94 #ifdef CHANGE_COLOR /* the Mac uses a palette device */
97 tty_change_background,
100 tty_get_color_string,
103 /* other defs that really should go away (they're tty specific) */
107 #if defined(WIN32CON)
108 nttty_preference_update,
110 genl_preference_update,
114 static int maxwin = 0; /* number of windows in use */
116 struct WinDesc *wins[MAXWIN];
117 struct DisplayDesc *ttyDisplay; /* the tty display descriptor */
119 extern void FDECL(cmov, (int,int)); /* from termcap.c */
120 extern void FDECL(nocmov, (int,int)); /* from termcap.c */
121 #if defined(UNIX) || defined(VMS)
122 static char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
125 static char winpanicstr[] = "Bad window id %d";
126 char defmorestr[] = "--More--";
129 # if defined(USE_TILES) && defined(MSDOS)
130 boolean clipping = FALSE; /* clipping on? */
131 int clipx = 0, clipxmax = 0;
133 static boolean clipping = FALSE; /* clipping on? */
134 static int clipx = 0, clipxmax = 0;
136 static int clipy = 0, clipymax = 0;
137 #endif /* CLIPPING */
139 #if defined(USE_TILES) && defined(MSDOS)
140 extern void FDECL(adjust_cursor_flags, (struct WinDesc *));
143 #if defined(ASCIIGRAPH) && !defined(NO_TERMS)
144 boolean GFlag = FALSE;
145 boolean HE_resets_AS; /* see termcap.c */
148 #if defined(MICRO) || defined(WIN32CON)
149 static const char to_continue[] = "to continue";
150 #define getret() getreturn(to_continue)
152 STATIC_DCL void NDECL(getret);
154 STATIC_DCL void FDECL(erase_menu_or_text, (winid, struct WinDesc *, BOOLEAN_P));
155 STATIC_DCL void FDECL(free_window_info, (struct WinDesc *, BOOLEAN_P));
156 STATIC_DCL void FDECL(dmore,(struct WinDesc *, const char *));
157 STATIC_DCL void FDECL(set_item_state, (winid, int, tty_menu_item *));
158 STATIC_DCL void FDECL(set_all_on_page, (winid,tty_menu_item *,tty_menu_item *));
159 STATIC_DCL void FDECL(unset_all_on_page, (winid,tty_menu_item *,tty_menu_item *));
160 STATIC_DCL void FDECL(invert_all_on_page, (winid,tty_menu_item *,tty_menu_item *, CHAR_P));
161 STATIC_DCL void FDECL(invert_all, (winid,tty_menu_item *,tty_menu_item *, CHAR_P));
162 STATIC_DCL void FDECL(process_menu_window, (winid,struct WinDesc *));
163 STATIC_DCL void FDECL(process_text_window, (winid,struct WinDesc *));
164 STATIC_DCL tty_menu_item *FDECL(reverse, (tty_menu_item *));
165 STATIC_DCL const char * FDECL(compress_str, (const char *));
166 STATIC_DCL void FDECL(tty_putsym, (winid, int, int, CHAR_P));
167 static char *FDECL(copy_of, (const char *));
168 STATIC_DCL void FDECL(bail, (const char *)); /* __attribute__((noreturn)) */
171 * A string containing all the default commands -- to add to a list
172 * of acceptable inputs.
174 static const char default_menu_cmds[] = {
185 0 /* null terminator */
189 /* clean up and quit */
195 tty_exit_nhwindows(mesg);
196 terminate(EXIT_SUCCESS);
200 #if defined(SIGWINCH) && defined(CLIPPING)
204 int oldLI = LI, oldCO = CO, i;
205 register struct WinDesc *cw;
208 if((oldLI != LI || oldCO != CO) && ttyDisplay) {
209 ttyDisplay->rows = LI;
210 ttyDisplay->cols = CO;
212 cw = wins[BASE_WINDOW];
213 cw->rows = ttyDisplay->rows;
214 cw->cols = ttyDisplay->cols;
216 if(iflags.window_inited) {
217 cw = wins[WIN_MESSAGE];
218 cw->curx = cw->cury = 0;
220 tty_destroy_nhwindow(WIN_STATUS);
221 WIN_STATUS = tty_create_nhwindow(NHW_STATUS);
225 if(CO < COLNO || LI < ROWNO+3) {
227 tty_cliparound(u.ux, u.uy);
233 i = ttyDisplay->toplin;
234 ttyDisplay->toplin = 0;
237 ttyDisplay->toplin = i;
242 for(i=WIN_INVEN; i < MAXWIN; i++)
243 if(wins[i] && wins[i]->active) {
245 addtopl("Press Return to continue: ");
248 (void) fflush(stdout);
249 if(i < 2) flush_screen(1);
258 tty_init_nhwindows(argcp,argv)
265 * Remember tty modes, to be restored on exit.
267 * gettty() must be called before tty_startup()
268 * due to ordering of LI/CO settings
269 * tty_startup() must be called before initoptions()
270 * due to ordering of graphics settings
272 #if defined(UNIX) || defined(VMS)
277 /* to port dependant tty setup */
278 tty_startup(&wid, &hgt);
279 setftty(); /* calls start_screen */
281 /* set up tty descriptor */
282 ttyDisplay = (struct DisplayDesc*) alloc(sizeof(struct DisplayDesc));
283 ttyDisplay->toplin = 0;
284 ttyDisplay->rows = hgt;
285 ttyDisplay->cols = wid;
286 ttyDisplay->curx = ttyDisplay->cury = 0;
287 ttyDisplay->inmore = ttyDisplay->inread = ttyDisplay->intr = 0;
288 ttyDisplay->dismiss_more = 0;
290 ttyDisplay->color = NO_COLOR;
292 ttyDisplay->attrs = 0;
294 /* set up the default windows */
295 BASE_WINDOW = tty_create_nhwindow(NHW_BASE);
296 wins[BASE_WINDOW]->active = 1;
298 ttyDisplay->lastwin = WIN_ERR;
300 #if defined(SIGWINCH) && defined(CLIPPING)
301 (void) signal(SIGWINCH, winch);
304 /* add one a space forward menu command alias */
305 add_menu_cmd_alias(' ', MENU_NEXT_PAGE);
307 tty_clear_nhwindow(BASE_WINDOW);
309 tty_putstr(BASE_WINDOW, 0, "");
310 tty_putstr(BASE_WINDOW, 0, COPYRIGHT_BANNER_A);
311 tty_putstr(BASE_WINDOW, 0, COPYRIGHT_BANNER_B);
312 tty_putstr(BASE_WINDOW, 0, COPYRIGHT_BANNER_C);
313 tty_putstr(BASE_WINDOW, 0, "");
314 tty_display_nhwindow(BASE_WINDOW, FALSE);
318 tty_player_selection()
321 char pick4u = 'n', thisch, lastch = 0;
322 char pbuf[QBUFSZ], plbuf[QBUFSZ];
325 menu_item *selected = 0;
327 /* prevent an unnecessary prompt */
330 /* Should we randomly pick for the player? */
331 if (!flags.randomall &&
332 (flags.initrole == ROLE_NONE || flags.initrace == ROLE_NONE ||
333 flags.initgend == ROLE_NONE || flags.initalign == ROLE_NONE)) {
335 char *prompt = build_plselection_prompt(pbuf, QBUFSZ, flags.initrole,
336 flags.initrace, flags.initgend, flags.initalign);
338 tty_putstr(BASE_WINDOW, 0, "");
339 echoline = wins[BASE_WINDOW]->cury;
340 tty_putstr(BASE_WINDOW, 0, prompt);
342 pick4u = lowc(readchar());
343 if (index(quitchars, pick4u)) pick4u = 'y';
344 } while(!index(ynqchars, pick4u));
345 if ((int)strlen(prompt) + 1 < CO) {
346 /* Echo choice and move back down line */
347 tty_putsym(BASE_WINDOW, (int)strlen(prompt)+1, echoline, pick4u);
348 tty_putstr(BASE_WINDOW, 0, "");
350 /* Otherwise it's hard to tell where to echo, and things are
351 * wrapping a bit messily anyway, so (try to) make sure the next
352 * question shows up well and doesn't get wrapped at the
353 * bottom of the window.
355 tty_clear_nhwindow(BASE_WINDOW);
357 if (pick4u != 'y' && pick4u != 'n') {
359 if (selected) free((genericptr_t) selected);
366 (void) root_plselection_prompt(plbuf, QBUFSZ - 1,
367 flags.initrole, flags.initrace, flags.initgend, flags.initalign);
369 /* Select a role, if necessary */
370 /* we'll try to be compatible with pre-selected race/gender/alignment,
371 * but may not succeed */
372 if (flags.initrole < 0) {
373 char rolenamebuf[QBUFSZ];
374 /* Process the choice */
375 if (pick4u == 'y' || flags.initrole == ROLE_RANDOM || flags.randomall) {
376 /* Pick a random role */
377 flags.initrole = pick_role(flags.initrace, flags.initgend,
378 flags.initalign, PICK_RANDOM);
379 if (flags.initrole < 0) {
380 tty_putstr(BASE_WINDOW, 0, "Incompatible role!");
381 flags.initrole = randrole();
384 tty_clear_nhwindow(BASE_WINDOW);
385 tty_putstr(BASE_WINDOW, 0, "Choosing Character's Role");
386 /* Prompt for a role */
387 win = create_nhwindow(NHW_MENU);
389 any.a_void = 0; /* zero out all bits */
390 for (i = 0; roles[i].name.m; i++) {
391 if (ok_role(i, flags.initrace, flags.initgend,
393 any.a_int = i+1; /* must be non-zero */
394 thisch = lowc(roles[i].name.m[0]);
395 if (thisch == lastch) thisch = highc(thisch);
396 if (flags.initgend != ROLE_NONE && flags.initgend != ROLE_RANDOM) {
397 if (flags.initgend == 1 && roles[i].name.f)
398 Strcpy(rolenamebuf, roles[i].name.f);
400 Strcpy(rolenamebuf, roles[i].name.m);
402 if (roles[i].name.f) {
403 Strcpy(rolenamebuf, roles[i].name.m);
404 Strcat(rolenamebuf, "/");
405 Strcat(rolenamebuf, roles[i].name.f);
407 Strcpy(rolenamebuf, roles[i].name.m);
409 add_menu(win, NO_GLYPH, &any, thisch,
410 0, ATR_NONE, an(rolenamebuf), MENU_UNSELECTED);
414 any.a_int = pick_role(flags.initrace, flags.initgend,
415 flags.initalign, PICK_RANDOM)+1;
416 if (any.a_int == 0) /* must be non-zero */
417 any.a_int = randrole()+1;
418 add_menu(win, NO_GLYPH, &any , '*', 0, ATR_NONE,
419 "Random", MENU_UNSELECTED);
420 any.a_int = i+1; /* must be non-zero */
421 add_menu(win, NO_GLYPH, &any , 'q', 0, ATR_NONE,
422 "Quit", MENU_UNSELECTED);
423 Sprintf(pbuf, "Pick a role for your %s", plbuf);
425 n = select_menu(win, PICK_ONE, &selected);
426 destroy_nhwindow(win);
428 /* Process the choice */
429 if (n != 1 || selected[0].item.a_int == any.a_int)
430 goto give_up; /* Selected quit */
432 flags.initrole = selected[0].item.a_int - 1;
433 free((genericptr_t) selected), selected = 0;
435 (void) root_plselection_prompt(plbuf, QBUFSZ - 1,
436 flags.initrole, flags.initrace, flags.initgend, flags.initalign);
439 /* Select a race, if necessary */
440 /* force compatibility with role, try for compatibility with
441 * pre-selected gender/alignment */
442 if (flags.initrace < 0 || !validrace(flags.initrole, flags.initrace)) {
443 /* pre-selected race not valid */
444 if (pick4u == 'y' || flags.initrace == ROLE_RANDOM || flags.randomall) {
445 flags.initrace = pick_race(flags.initrole, flags.initgend,
446 flags.initalign, PICK_RANDOM);
447 if (flags.initrace < 0) {
448 tty_putstr(BASE_WINDOW, 0, "Incompatible race!");
449 flags.initrace = randrace(flags.initrole);
451 } else { /* pick4u == 'n' */
452 /* Count the number of valid races */
453 n = 0; /* number valid */
454 k = 0; /* valid race */
455 for (i = 0; races[i].noun; i++) {
456 if (ok_race(flags.initrole, i, flags.initgend,
463 for (i = 0; races[i].noun; i++) {
464 if (validrace(flags.initrole, i)) {
471 /* Permit the user to pick, if there is more than one */
473 tty_clear_nhwindow(BASE_WINDOW);
474 tty_putstr(BASE_WINDOW, 0, "Choosing Race");
475 win = create_nhwindow(NHW_MENU);
477 any.a_void = 0; /* zero out all bits */
478 for (i = 0; races[i].noun; i++)
479 if (ok_race(flags.initrole, i, flags.initgend,
481 any.a_int = i+1; /* must be non-zero */
482 add_menu(win, NO_GLYPH, &any, races[i].noun[0],
483 0, ATR_NONE, races[i].noun, MENU_UNSELECTED);
485 any.a_int = pick_race(flags.initrole, flags.initgend,
486 flags.initalign, PICK_RANDOM)+1;
487 if (any.a_int == 0) /* must be non-zero */
488 any.a_int = randrace(flags.initrole)+1;
489 add_menu(win, NO_GLYPH, &any , '*', 0, ATR_NONE,
490 "Random", MENU_UNSELECTED);
491 any.a_int = i+1; /* must be non-zero */
492 add_menu(win, NO_GLYPH, &any , 'q', 0, ATR_NONE,
493 "Quit", MENU_UNSELECTED);
494 Sprintf(pbuf, "Pick the race of your %s", plbuf);
496 n = select_menu(win, PICK_ONE, &selected);
497 destroy_nhwindow(win);
498 if (n != 1 || selected[0].item.a_int == any.a_int)
499 goto give_up; /* Selected quit */
501 k = selected[0].item.a_int - 1;
502 free((genericptr_t) selected), selected = 0;
506 (void) root_plselection_prompt(plbuf, QBUFSZ - 1,
507 flags.initrole, flags.initrace, flags.initgend, flags.initalign);
510 /* Select a gender, if necessary */
511 /* force compatibility with role/race, try for compatibility with
512 * pre-selected alignment */
513 if (flags.initgend < 0 || !validgend(flags.initrole, flags.initrace,
515 /* pre-selected gender not valid */
516 if (pick4u == 'y' || flags.initgend == ROLE_RANDOM || flags.randomall) {
517 flags.initgend = pick_gend(flags.initrole, flags.initrace,
518 flags.initalign, PICK_RANDOM);
519 if (flags.initgend < 0) {
520 tty_putstr(BASE_WINDOW, 0, "Incompatible gender!");
521 flags.initgend = randgend(flags.initrole, flags.initrace);
523 } else { /* pick4u == 'n' */
524 /* Count the number of valid genders */
525 n = 0; /* number valid */
526 k = 0; /* valid gender */
527 for (i = 0; i < ROLE_GENDERS; i++) {
528 if (ok_gend(flags.initrole, flags.initrace, i,
535 for (i = 0; i < ROLE_GENDERS; i++) {
536 if (validgend(flags.initrole, flags.initrace, i)) {
543 /* Permit the user to pick, if there is more than one */
545 tty_clear_nhwindow(BASE_WINDOW);
546 tty_putstr(BASE_WINDOW, 0, "Choosing Gender");
547 win = create_nhwindow(NHW_MENU);
549 any.a_void = 0; /* zero out all bits */
550 for (i = 0; i < ROLE_GENDERS; i++)
551 if (ok_gend(flags.initrole, flags.initrace, i,
554 add_menu(win, NO_GLYPH, &any, genders[i].adj[0],
555 0, ATR_NONE, genders[i].adj, MENU_UNSELECTED);
557 any.a_int = pick_gend(flags.initrole, flags.initrace,
558 flags.initalign, PICK_RANDOM)+1;
559 if (any.a_int == 0) /* must be non-zero */
560 any.a_int = randgend(flags.initrole, flags.initrace)+1;
561 add_menu(win, NO_GLYPH, &any , '*', 0, ATR_NONE,
562 "Random", MENU_UNSELECTED);
563 any.a_int = i+1; /* must be non-zero */
564 add_menu(win, NO_GLYPH, &any , 'q', 0, ATR_NONE,
565 "Quit", MENU_UNSELECTED);
566 Sprintf(pbuf, "Pick the gender of your %s", plbuf);
568 n = select_menu(win, PICK_ONE, &selected);
569 destroy_nhwindow(win);
570 if (n != 1 || selected[0].item.a_int == any.a_int)
571 goto give_up; /* Selected quit */
573 k = selected[0].item.a_int - 1;
574 free((genericptr_t) selected), selected = 0;
578 (void) root_plselection_prompt(plbuf, QBUFSZ - 1,
579 flags.initrole, flags.initrace, flags.initgend, flags.initalign);
582 /* Select an alignment, if necessary */
583 /* force compatibility with role/race/gender */
584 if (flags.initalign < 0 || !validalign(flags.initrole, flags.initrace,
586 /* pre-selected alignment not valid */
587 if (pick4u == 'y' || flags.initalign == ROLE_RANDOM || flags.randomall) {
588 flags.initalign = pick_align(flags.initrole, flags.initrace,
589 flags.initgend, PICK_RANDOM);
590 if (flags.initalign < 0) {
591 tty_putstr(BASE_WINDOW, 0, "Incompatible alignment!");
592 flags.initalign = randalign(flags.initrole, flags.initrace);
594 } else { /* pick4u == 'n' */
595 /* Count the number of valid alignments */
596 n = 0; /* number valid */
597 k = 0; /* valid alignment */
598 for (i = 0; i < ROLE_ALIGNS; i++) {
599 if (ok_align(flags.initrole, flags.initrace, flags.initgend,
606 for (i = 0; i < ROLE_ALIGNS; i++) {
607 if (validalign(flags.initrole, flags.initrace, i)) {
614 /* Permit the user to pick, if there is more than one */
616 tty_clear_nhwindow(BASE_WINDOW);
617 tty_putstr(BASE_WINDOW, 0, "Choosing Alignment");
618 win = create_nhwindow(NHW_MENU);
620 any.a_void = 0; /* zero out all bits */
621 for (i = 0; i < ROLE_ALIGNS; i++)
622 if (ok_align(flags.initrole, flags.initrace,
623 flags.initgend, i)) {
625 add_menu(win, NO_GLYPH, &any, aligns[i].adj[0],
626 0, ATR_NONE, aligns[i].adj, MENU_UNSELECTED);
628 any.a_int = pick_align(flags.initrole, flags.initrace,
629 flags.initgend, PICK_RANDOM)+1;
630 if (any.a_int == 0) /* must be non-zero */
631 any.a_int = randalign(flags.initrole, flags.initrace)+1;
632 add_menu(win, NO_GLYPH, &any , '*', 0, ATR_NONE,
633 "Random", MENU_UNSELECTED);
634 any.a_int = i+1; /* must be non-zero */
635 add_menu(win, NO_GLYPH, &any , 'q', 0, ATR_NONE,
636 "Quit", MENU_UNSELECTED);
637 Sprintf(pbuf, "Pick the alignment of your %s", plbuf);
639 n = select_menu(win, PICK_ONE, &selected);
640 destroy_nhwindow(win);
641 if (n != 1 || selected[0].item.a_int == any.a_int)
642 goto give_up; /* Selected quit */
644 k = selected[0].item.a_int - 1;
645 free((genericptr_t) selected), selected = 0;
651 tty_display_nhwindow(BASE_WINDOW, FALSE);
655 * plname is filled either by an option (-u Player or -uPlayer) or
656 * explicitly (by being the wizard) or by askname.
657 * It may still contain a suffix denoting the role, etc.
658 * Always called after init_nhwindows() and before display_gamewindows().
663 static char who_are_you[] = "Who are you? ";
664 register int c, ct, tryct = 0;
666 tty_putstr(BASE_WINDOW, 0, "");
669 if (tryct > 10) bail("Giving up after 10 tries.\n");
670 tty_curs(BASE_WINDOW, 1, wins[BASE_WINDOW]->cury - 1);
671 tty_putstr(BASE_WINDOW, 0, "Enter a name for your character...");
672 /* erase previous prompt (in case of ESC after partial response) */
673 tty_curs(BASE_WINDOW, 1, wins[BASE_WINDOW]->cury), cl_end();
675 tty_putstr(BASE_WINDOW, 0, who_are_you);
676 tty_curs(BASE_WINDOW, (int)(sizeof who_are_you),
677 wins[BASE_WINDOW]->cury - 1);
679 while((c = tty_nhgetch()) != '\n') {
680 if(c == EOF) error("End of input\n");
681 if (c == '\033') { ct = 0; break; } /* continue outer loop */
682 #if defined(WIN32CON)
683 if (c == '\003') bail("^C abort.\n");
685 /* some people get confused when their erase char is not ^H */
686 if (c == '\b' || c == '\177') {
692 #if defined(MICRO) || defined(WIN32CON)
693 # if defined(WIN32CON) || defined(MSDOS)
694 backsp(); /* \b is visible on NT */
701 (void) putchar('\b');
703 (void) putchar('\b');
708 #if defined(UNIX) || defined(VMS)
709 if(c != '-' && c != '@')
710 if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z') c = '_';
712 if (ct < (int)(sizeof plname) - 1) {
732 /* move to next line to simulate echo of user's <return> */
733 tty_curs(BASE_WINDOW, 1, wins[BASE_WINDOW]->cury + 1);
742 #if !defined(MICRO) && !defined(WIN32CON)
750 xputs(iflags.cbreak ? "space" : "return");
751 xputs(" to continue: ");
759 tty_suspend_nhwindows(str)
762 settty(str); /* calls end_screen, perhaps raw_print */
763 if (!str) tty_raw_print(""); /* calls fflush(stdout) */
767 tty_resume_nhwindows()
770 setftty(); /* calls start_screen */
775 tty_exit_nhwindows(str)
780 tty_suspend_nhwindows(str);
781 /* Just forget any windows existed, since we're about to exit anyway.
782 * Disable windows to avoid calls to window routines.
784 for(i=0; i<MAXWIN; i++)
785 if (wins[i] && (i != BASE_WINDOW)) {
786 #ifdef FREE_ALL_MEMORY
787 free_window_info(wins[i], TRUE);
788 free((genericptr_t) wins[i]);
792 #ifndef NO_TERMS /*(until this gets added to the window interface)*/
793 tty_shutdown(); /* cleanup termcap/terminfo/whatever */
795 iflags.window_inited = 0;
799 tty_create_nhwindow(type)
802 struct WinDesc* newwin;
809 newwin = (struct WinDesc*) alloc(sizeof(struct WinDesc));
812 newwin->active = FALSE;
813 newwin->curx = newwin->cury = 0;
815 newwin->mlist = (tty_menu_item *) 0;
816 newwin->plist = (tty_menu_item **) 0;
817 newwin->npages = newwin->plist_size = newwin->nitems = newwin->how = 0;
820 /* base window, used for absolute movement on the screen */
821 newwin->offx = newwin->offy = 0;
822 newwin->rows = ttyDisplay->rows;
823 newwin->cols = ttyDisplay->cols;
824 newwin->maxrow = newwin->maxcol = 0;
827 /* message window, 1 line long, very wide, top of screen */
828 newwin->offx = newwin->offy = 0;
830 if(iflags.msg_history < 20) iflags.msg_history = 20;
831 else if(iflags.msg_history > 60) iflags.msg_history = 60;
832 newwin->maxrow = newwin->rows = iflags.msg_history;
833 newwin->maxcol = newwin->cols = 0;
836 /* status window, 2 lines long, full width, bottom of screen */
838 #if defined(USE_TILES) && defined(MSDOS)
840 newwin->offy = ttyDisplay->rows-2;
843 newwin->offy = min((int)ttyDisplay->rows-2, ROWNO+1);
844 newwin->rows = newwin->maxrow = 2;
845 newwin->cols = newwin->maxcol = min(ttyDisplay->cols, COLNO);
848 /* map window, ROWNO lines long, full width, below message window */
851 newwin->rows = ROWNO;
852 newwin->cols = COLNO;
853 newwin->maxrow = 0; /* no buffering done -- let gbuf do it */
858 /* inventory/menu window, variable length, full width, top of screen */
859 /* help window, the same, different semantics for display, etc */
860 newwin->offx = newwin->offy = 0;
862 newwin->cols = ttyDisplay->cols;
863 newwin->maxrow = newwin->maxcol = 0;
866 panic("Tried to create window type %d\n", (int) type);
870 for(newid = 0; newid<MAXWIN; newid++) {
871 if(wins[newid] == 0) {
872 wins[newid] = newwin;
876 if(newid == MAXWIN) {
877 panic("No window slots!");
883 (char **) alloc(sizeof(char *) * (unsigned)newwin->maxrow);
885 (short *) alloc(sizeof(short) * (unsigned)newwin->maxrow);
887 for (i = 0; i < newwin->maxrow; i++) {
888 newwin->data[i] = (char *) alloc((unsigned)newwin->maxcol);
889 newwin->datlen[i] = newwin->maxcol;
892 for (i = 0; i < newwin->maxrow; i++) {
893 newwin->data[i] = (char *) 0;
894 newwin->datlen[i] = 0;
897 if(newwin->type == NHW_MESSAGE)
900 newwin->data = (char **)0;
901 newwin->datlen = (short *)0;
908 erase_menu_or_text(window, cw, clear)
915 tty_curs(window, 1, 0);
922 docorner((int)cw->offx, cw->maxrow+1);
926 free_window_info(cw, free_data)
933 if (cw == wins[WIN_MESSAGE] && cw->rows > cw->maxrow)
934 cw->maxrow = cw->rows; /* topl data */
935 for(i=0; i<cw->maxrow; i++)
937 free((genericptr_t)cw->data[i]);
938 cw->data[i] = (char *)0;
939 if (cw->datlen) cw->datlen[i] = 0;
942 free((genericptr_t)cw->data);
943 cw->data = (char **)0;
944 if (cw->datlen) free((genericptr_t)cw->datlen);
945 cw->datlen = (short *)0;
949 cw->maxrow = cw->maxcol = 0;
952 while ((temp = cw->mlist) != 0) {
953 cw->mlist = cw->mlist->next;
954 if (temp->str) free((genericptr_t)temp->str);
955 free((genericptr_t)temp);
959 free((genericptr_t)cw->plist);
962 cw->plist_size = cw->npages = cw->nitems = cw->how = 0;
964 free((genericptr_t)cw->morestr);
970 tty_clear_nhwindow(window)
973 register struct WinDesc *cw = 0;
975 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
976 panic(winpanicstr, window);
977 ttyDisplay->lastwin = window;
981 if(ttyDisplay->toplin) {
985 docorner(1, cw->cury+1);
986 ttyDisplay->toplin = 0;
990 tty_curs(window, 1, 0);
992 tty_curs(window, 1, 1);
996 /* cheap -- clear the whole thing and tell nethack to redraw botl */
1005 erase_menu_or_text(window, cw, TRUE);
1006 free_window_info(cw, FALSE);
1009 cw->curx = cw->cury = 0;
1014 register struct WinDesc *cw;
1015 const char *s; /* valid responses */
1017 const char *prompt = cw->morestr ? cw->morestr : defmorestr;
1018 int offset = (cw->type == NHW_TEXT) ? 1 : 2;
1020 tty_curs(BASE_WINDOW,
1021 (int)ttyDisplay->curx + offset, (int)ttyDisplay->cury);
1025 ttyDisplay->curx += strlen(prompt);
1033 set_item_state(window, lineno, item)
1036 tty_menu_item *item;
1038 char ch = item->selected ? (item->count == -1L ? '+' : '#') : '-';
1039 tty_curs(window, 4, lineno);
1040 term_start_attr(item->attr);
1043 term_end_attr(item->attr);
1047 set_all_on_page(window, page_start, page_end)
1049 tty_menu_item *page_start, *page_end;
1051 tty_menu_item *curr;
1054 for (n = 0, curr = page_start; curr != page_end; n++, curr = curr->next)
1055 if (curr->identifier.a_void && !curr->selected) {
1056 curr->selected = TRUE;
1057 set_item_state(window, n, curr);
1062 unset_all_on_page(window, page_start, page_end)
1064 tty_menu_item *page_start, *page_end;
1066 tty_menu_item *curr;
1069 for (n = 0, curr = page_start; curr != page_end; n++, curr = curr->next)
1070 if (curr->identifier.a_void && curr->selected) {
1071 curr->selected = FALSE;
1073 set_item_state(window, n, curr);
1078 invert_all_on_page(window, page_start, page_end, acc)
1080 tty_menu_item *page_start, *page_end;
1081 char acc; /* group accelerator, 0 => all */
1083 tty_menu_item *curr;
1086 for (n = 0, curr = page_start; curr != page_end; n++, curr = curr->next)
1087 if (curr->identifier.a_void && (acc == 0 || curr->gselector == acc)) {
1088 if (curr->selected) {
1089 curr->selected = FALSE;
1092 curr->selected = TRUE;
1093 set_item_state(window, n, curr);
1098 * Invert all entries that match the give group accelerator (or all if
1102 invert_all(window, page_start, page_end, acc)
1104 tty_menu_item *page_start, *page_end;
1105 char acc; /* group accelerator, 0 => all */
1107 tty_menu_item *curr;
1108 boolean on_curr_page;
1109 struct WinDesc *cw = wins[window];
1111 invert_all_on_page(window, page_start, page_end, acc);
1113 /* invert the rest */
1114 for (on_curr_page = FALSE, curr = cw->mlist; curr; curr = curr->next) {
1115 if (curr == page_start)
1116 on_curr_page = TRUE;
1117 else if (curr == page_end)
1118 on_curr_page = FALSE;
1120 if (!on_curr_page && curr->identifier.a_void
1121 && (acc == 0 || curr->gselector == acc)) {
1122 if (curr->selected) {
1123 curr->selected = FALSE;
1126 curr->selected = TRUE;
1132 process_menu_window(window, cw)
1136 tty_menu_item *page_start, *page_end, *curr;
1138 int n, curr_page, page_lines;
1139 boolean finished, counting, reset_count;
1140 char *cp, *rp, resp[QBUFSZ], gacc[QBUFSZ],
1143 curr_page = page_lines = 0;
1144 page_start = page_end = 0;
1145 msave = cw->morestr; /* save the morestr */
1146 cw->morestr = morestr = (char*) alloc((unsigned) QBUFSZ);
1152 /* collect group accelerators; for PICK_NONE, they're ignored;
1153 for PICK_ONE, only those which match exactly one entry will be
1154 accepted; for PICK_ANY, those which match any entry are okay */
1156 if (cw->how != PICK_NONE) {
1158 #define GSELIDX(c) (c & 127) /* guard against `signed char' */
1160 for (i = 0; i < SIZE(gcnt); i++) gcnt[i] = 0;
1161 for (n = 0, curr = cw->mlist; curr; curr = curr->next)
1162 if (curr->gselector && curr->gselector != curr->selector) {
1164 ++gcnt[GSELIDX(curr->gselector)];
1167 if (n > 0) /* at least one group accelerator found */
1168 for (rp = gacc, curr = cw->mlist; curr; curr = curr->next)
1169 if (curr->gselector && !index(gacc, curr->gselector) &&
1170 (cw->how == PICK_ANY ||
1171 gcnt[GSELIDX(curr->gselector)] == 1)) {
1172 *rp++ = curr->gselector;
1173 *rp = '\0'; /* re-terminate for index() */
1177 /* loop until finished */
1186 /* new page to be displayed */
1187 if (curr_page < 0 || (cw->npages > 0 && curr_page >= cw->npages))
1188 panic("bad menu screen page #%d", curr_page);
1191 if (!cw->offx) { /* if not corner, do clearscreen */
1193 tty_curs(window, 1, 0);
1200 if (cw->npages > 0) {
1201 /* collect accelerators */
1202 page_start = cw->plist[curr_page];
1203 page_end = cw->plist[curr_page + 1];
1204 for (page_lines = 0, curr = page_start;
1206 page_lines++, curr = curr->next) {
1208 *rp++ = curr->selector;
1210 tty_curs(window, 1, page_lines);
1211 if (cw->offx) cl_end();
1213 (void) putchar(' ');
1216 * Don't use xputs() because (1) under unix it calls
1217 * tputstr() which will interpret a '*' as some kind
1218 * of padding information and (2) it calls xputc to
1219 * actually output the character. We're faster doing
1222 term_start_attr(curr->attr);
1223 for (n = 0, cp = curr->str;
1225 *cp && (int) ++ttyDisplay->curx < (int) ttyDisplay->cols;
1228 *cp && (int) ttyDisplay->curx < (int) ttyDisplay->cols;
1229 cp++, n++, ttyDisplay->curx++)
1231 if (n == 2 && curr->identifier.a_void != 0 &&
1233 if (curr->count == -1L)
1234 (void) putchar('+'); /* all selected */
1236 (void) putchar('#'); /* count selected */
1238 (void) putchar(*cp);
1239 term_end_attr(curr->attr);
1248 /* corner window - clear extra lines from last page */
1250 for (n = page_lines + 1; n < cw->maxrow; n++) {
1251 tty_curs(window, 1, n);
1256 /* set extra chars.. */
1257 Strcat(resp, default_menu_cmds);
1258 Strcat(resp, "0123456789\033\n\r"); /* counts, quit */
1259 Strcat(resp, gacc); /* group accelerators */
1260 Strcat(resp, mapped_menu_cmds);
1263 Sprintf(cw->morestr, "(%d of %d)",
1264 curr_page + 1, (int) cw->npages);
1266 Strcpy(cw->morestr, msave);
1268 Strcpy(cw->morestr, defmorestr);
1270 tty_curs(window, 1, page_lines);
1274 /* just put the cursor back... */
1275 tty_curs(window, (int) strlen(cw->morestr) + 2, page_lines);
1276 xwaitforspace(resp);
1279 morc = map_menu_cmd(morc);
1282 /* special case: '0' is also the default ball class */
1283 if (!counting && index(gacc, morc)) goto group_accel;
1284 /* fall through to count the zero */
1285 case '1': case '2': case '3': case '4':
1286 case '5': case '6': case '7': case '8': case '9':
1287 count = (count * 10L) + (long) (morc - '0');
1289 * It is debatable whether we should allow 0 to
1290 * start a count. There is no difference if the
1291 * item is selected. If not selected, then
1294 * count starting zero: "zero b's"
1295 * ignore starting zero: "select b"
1297 * At present I don't know which is better.
1299 if (count != 0L) { /* ignore leading zeros */
1301 reset_count = FALSE;
1304 case '\033': /* cancel - from counting or loop */
1306 /* deselect everything */
1307 for (curr = cw->mlist; curr; curr = curr->next) {
1308 curr->selected = FALSE;
1311 cw->flags |= WIN_CANCELLED;
1314 /* else only stop count */
1316 case '\0': /* finished (commit) */
1319 /* only finished if we are actually picking something */
1320 if (cw->how != PICK_NONE) {
1324 /* else fall through */
1325 case MENU_NEXT_PAGE:
1326 if (cw->npages > 0 && curr_page != cw->npages - 1) {
1330 finished = TRUE; /* questionable behavior */
1332 case MENU_PREVIOUS_PAGE:
1333 if (cw->npages > 0 && curr_page != 0) {
1338 case MENU_FIRST_PAGE:
1339 if (cw->npages > 0 && curr_page != 0) {
1344 case MENU_LAST_PAGE:
1345 if (cw->npages > 0 && curr_page != cw->npages - 1) {
1347 curr_page = cw->npages - 1;
1350 case MENU_SELECT_PAGE:
1351 if (cw->how == PICK_ANY)
1352 set_all_on_page(window, page_start, page_end);
1354 case MENU_UNSELECT_PAGE:
1355 unset_all_on_page(window, page_start, page_end);
1357 case MENU_INVERT_PAGE:
1358 if (cw->how == PICK_ANY)
1359 invert_all_on_page(window, page_start, page_end, 0);
1361 case MENU_SELECT_ALL:
1362 if (cw->how == PICK_ANY) {
1363 set_all_on_page(window, page_start, page_end);
1365 for (curr = cw->mlist; curr; curr = curr->next)
1366 if (curr->identifier.a_void && !curr->selected)
1367 curr->selected = TRUE;
1370 case MENU_UNSELECT_ALL:
1371 unset_all_on_page(window, page_start, page_end);
1372 /* unset the rest */
1373 for (curr = cw->mlist; curr; curr = curr->next)
1374 if (curr->identifier.a_void && curr->selected) {
1375 curr->selected = FALSE;
1379 case MENU_INVERT_ALL:
1380 if (cw->how == PICK_ANY)
1381 invert_all(window, page_start, page_end, 0);
1384 if (cw->how == PICK_NONE || !index(resp, morc)) {
1385 /* unacceptable input received */
1388 } else if (index(gacc, morc)) {
1390 /* group accelerator; for the PICK_ONE case, we know that
1391 it matches exactly one item in order to be in gacc[] */
1392 invert_all(window, page_start, page_end, morc);
1393 if (cw->how == PICK_ONE) finished = TRUE;
1396 /* find, toggle, and possibly update */
1397 for (n = 0, curr = page_start;
1399 n++, curr = curr->next)
1400 if (morc == curr->selector) {
1401 if (curr->selected) {
1402 if (counting && count > 0) {
1403 curr->count = count;
1404 set_item_state(window, n, curr);
1405 } else { /* change state */
1406 curr->selected = FALSE;
1408 set_item_state(window, n, curr);
1410 } else { /* !selected */
1411 if (counting && count > 0) {
1412 curr->count = count;
1413 curr->selected = TRUE;
1414 set_item_state(window, n, curr);
1415 } else if (!counting) {
1416 curr->selected = TRUE;
1417 set_item_state(window, n, curr);
1419 /* do nothing counting&&count==0 */
1422 if (cw->how == PICK_ONE) finished = TRUE;
1423 break; /* from `for' loop */
1429 cw->morestr = msave;
1430 free((genericptr_t)morestr);
1434 process_text_window(window, cw)
1441 for (n = 0, i = 0; i < cw->maxrow; i++) {
1442 if (!cw->offx && (n + cw->offy == ttyDisplay->rows - 1)) {
1443 tty_curs(window, 1, n);
1445 dmore(cw, quitchars);
1446 if (morc == '\033') {
1447 cw->flags |= WIN_CANCELLED;
1451 tty_curs(window, 1, 0);
1457 tty_curs(window, 1, n++);
1458 if (cw->offx) cl_end();
1460 attr = cw->data[i][0] - 1;
1462 (void) putchar(' '); ++ttyDisplay->curx;
1464 term_start_attr(attr);
1465 for (cp = &cw->data[i][1];
1467 *cp && (int) ++ttyDisplay->curx < (int) ttyDisplay->cols;
1470 *cp && (int) ttyDisplay->curx < (int) ttyDisplay->cols;
1471 cp++, ttyDisplay->curx++)
1473 (void) putchar(*cp);
1474 term_end_attr(attr);
1477 if (i == cw->maxrow) {
1478 tty_curs(BASE_WINDOW, (int)cw->offx + 1,
1479 (cw->type == NHW_TEXT) ? (int) ttyDisplay->rows - 1 : n);
1481 dmore(cw, quitchars);
1483 cw->flags |= WIN_CANCELLED;
1489 tty_display_nhwindow(window, blocking)
1491 boolean blocking; /* with ttys, all windows are blocking */
1493 register struct WinDesc *cw = 0;
1495 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
1496 panic(winpanicstr, window);
1497 if(cw->flags & WIN_CANCELLED)
1499 ttyDisplay->lastwin = window;
1500 ttyDisplay->rawprint = 0;
1504 if(ttyDisplay->toplin == 1) {
1506 ttyDisplay->toplin = 1; /* more resets this */
1507 tty_clear_nhwindow(window);
1509 ttyDisplay->toplin = 0;
1510 cw->curx = cw->cury = 0;
1512 iflags.window_inited = TRUE;
1517 if(!ttyDisplay->toplin) ttyDisplay->toplin = 1;
1518 tty_display_nhwindow(WIN_MESSAGE, TRUE);
1522 (void) fflush(stdout);
1525 cw->maxcol = ttyDisplay->cols; /* force full-screen mode */
1529 /* avoid converting to uchar before calculations are finished */
1530 cw->offx = (uchar) (int)
1531 max((int) 10, (int) (ttyDisplay->cols - cw->maxcol - 1));
1532 if(cw->type == NHW_MENU)
1534 if(ttyDisplay->toplin == 1)
1535 tty_display_nhwindow(WIN_MESSAGE, TRUE);
1536 if(cw->offx == 10 || cw->maxrow >= (int) ttyDisplay->rows) {
1539 tty_curs(window, 1, 0);
1543 ttyDisplay->toplin = 0;
1545 tty_clear_nhwindow(WIN_MESSAGE);
1547 if (cw->data || !cw->maxrow)
1548 process_text_window(window, cw);
1550 process_menu_window(window, cw);
1557 tty_dismiss_nhwindow(window)
1560 register struct WinDesc *cw = 0;
1562 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
1563 panic(winpanicstr, window);
1567 if (ttyDisplay->toplin)
1568 tty_display_nhwindow(WIN_MESSAGE, TRUE);
1574 * these should only get dismissed when the game is going away
1577 tty_curs(BASE_WINDOW, 1, (int)ttyDisplay->rows-1);
1583 if (iflags.window_inited) {
1584 /* otherwise dismissing the text endwin after other windows
1585 * are dismissed tries to redraw the map and panics. since
1586 * the whole reason for dismissing the other windows was to
1587 * leave the ending window on the screen, we don't want to
1590 erase_menu_or_text(window, cw, FALSE);
1600 tty_destroy_nhwindow(window)
1603 register struct WinDesc *cw = 0;
1605 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
1606 panic(winpanicstr, window);
1609 tty_dismiss_nhwindow(window);
1610 if(cw->type == NHW_MESSAGE)
1611 iflags.window_inited = 0;
1612 if(cw->type == NHW_MAP)
1615 free_window_info(cw, TRUE);
1616 free((genericptr_t)cw);
1621 tty_curs(window, x, y)
1623 register int x, y; /* not xchar: perhaps xchar is unsigned and
1624 curx-x would be unsigned as well */
1626 struct WinDesc *cw = 0;
1627 int cx = ttyDisplay->curx;
1628 int cy = ttyDisplay->cury;
1630 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
1631 panic(winpanicstr, window);
1632 ttyDisplay->lastwin = window;
1634 #if defined(USE_TILES) && defined(MSDOS)
1635 adjust_cursor_flags(cw);
1637 cw->curx = --x; /* column 0 is never used */
1640 if(x<0 || y<0 || y >= cw->rows || x > cw->cols) {
1641 const char *s = "[unknown type]";
1643 case NHW_MESSAGE: s = "[topl window]"; break;
1644 case NHW_STATUS: s = "[status window]"; break;
1645 case NHW_MAP: s = "[map window]"; break;
1646 case NHW_MENU: s = "[corner window]"; break;
1647 case NHW_TEXT: s = "[text window]"; break;
1648 case NHW_BASE: s = "[base window]"; break;
1650 impossible("bad curs positioning win %d %s (%d,%d)", window, s, x, y);
1658 if(clipping && window == WIN_MAP) {
1664 if (y == cy && x == cx)
1667 if(cw->type == NHW_MAP)
1671 if(!nh_ND && (cx != x || x <= 3)) { /* Extremely primitive */
1672 cmov(x, y); /* bunker!wtm */
1677 if((cy -= y) < 0) cy = -cy;
1678 if((cx -= x) < 0) cx = -cx;
1679 if(cy <= 3 && cx <= 3) {
1682 } else if ((x <= 3 && cy <= 3) || (!nh_CM && x < cx)) {
1683 (void) putchar('\r');
1684 ttyDisplay->curx = 0;
1686 } else if (!nh_CM) {
1692 ttyDisplay->curx = x;
1693 ttyDisplay->cury = y;
1697 tty_putsym(window, x, y, ch)
1702 register struct WinDesc *cw = 0;
1704 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0)
1705 panic(winpanicstr, window);
1711 tty_curs(window, x, y);
1719 impossible("Can't putsym to window type %d", cw->type);
1725 STATIC_OVL const char*
1729 static char cbuf[BUFSZ];
1730 /* compress in case line too long */
1731 if((int)strlen(str) >= CO) {
1732 register const char *bp0 = str;
1733 register char *bp1 = cbuf;
1737 if(*bp0 != ' ' || bp0[1] != ' ')
1739 if(*bp0 != ' ' || bp0[1] != ' ' || bp0[2] != ' ')
1749 tty_putstr(window, attr, str)
1754 register struct WinDesc *cw = 0;
1756 register const char *nb;
1757 register int i, j, n0;
1759 /* Assume there's a real problem if the window is missing --
1760 * probably a panic message
1762 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0) {
1767 if(str == (const char*)0 ||
1768 ((cw->flags & WIN_CANCELLED) && (cw->type != NHW_MESSAGE)))
1770 if(cw->type != NHW_MESSAGE)
1771 str = compress_str(str);
1773 ttyDisplay->lastwin = window;
1777 /* really do this later */
1778 #if defined(USER_SOUNDS) && defined(WIN32CON)
1779 play_sound_for_message(str);
1785 ob = &cw->data[cw->cury][j = cw->curx];
1786 if(flags.botlx) *ob = 0;
1787 if(!cw->cury && (int)strlen(str) >= CO) {
1788 /* the characters before "St:" are unnecessary */
1789 nb = index(str, ':');
1790 if(nb && nb > str+2)
1794 for(i = cw->curx+1, n0 = cw->cols; i < n0; i++, nb++) {
1796 if(*ob || flags.botlx) {
1797 /* last char printed may be in middle of line */
1798 tty_curs(WIN_STATUS, i, cw->cury);
1804 tty_putsym(WIN_STATUS, i, cw->cury, *nb);
1808 (void) strncpy(&cw->data[cw->cury][j], str, cw->cols - j - 1);
1809 cw->data[cw->cury][cw->cols-1] = '\0'; /* null terminate */
1810 cw->cury = (cw->cury+1) % 2;
1814 tty_curs(window, cw->curx+1, cw->cury);
1815 term_start_attr(attr);
1816 while(*str && (int) ttyDisplay->curx < (int) ttyDisplay->cols-1) {
1817 (void) putchar(*str);
1823 term_end_attr(attr);
1826 tty_curs(window, cw->curx+1, cw->cury);
1827 term_start_attr(attr);
1829 if ((int) ttyDisplay->curx >= (int) ttyDisplay->cols-1) {
1832 tty_curs(window, cw->curx+1, cw->cury);
1834 (void) putchar(*str);
1840 term_end_attr(attr);
1844 if(cw->type == NHW_TEXT && cw->cury == ttyDisplay->rows-1) {
1845 /* not a menu, so save memory and output 1 page at a time */
1846 cw->maxcol = ttyDisplay->cols; /* force full-screen mode */
1847 tty_display_nhwindow(window, TRUE);
1848 for(i=0; i<cw->maxrow; i++)
1850 free((genericptr_t)cw->data[i]);
1853 cw->maxrow = cw->cury = 0;
1855 /* always grows one at a time, but alloc 12 at a time */
1856 if(cw->cury >= cw->rows) {
1860 tmp = (char **) alloc(sizeof(char *) * (unsigned)cw->rows);
1861 for(i=0; i<cw->maxrow; i++)
1862 tmp[i] = cw->data[i];
1864 free((genericptr_t)cw->data);
1867 for(i=cw->maxrow; i<cw->rows; i++)
1870 if(cw->data[cw->cury])
1871 free((genericptr_t)cw->data[cw->cury]);
1872 n0 = strlen(str) + 1;
1873 ob = cw->data[cw->cury] = (char *)alloc((unsigned)n0 + 1);
1874 *ob++ = (char)(attr + 1); /* avoid nuls, for convenience */
1879 if(++cw->cury > cw->maxrow)
1880 cw->maxrow = cw->cury;
1882 /* attempt to break the line */
1883 for(i = CO-1; i && str[i] != ' ' && str[i] != '\n';)
1886 cw->data[cw->cury-1][++i] = '\0';
1887 tty_putstr(window, attr, &str[i]);
1896 tty_display_file(fname, complain)
1900 #ifdef DEF_PAGER /* this implies that UNIX is defined */
1902 /* use external pager; this may give security problems */
1903 register int fd = open(fname, 0);
1906 if(complain) pline("Cannot open %s.", fname);
1911 /* Now that child() does a setuid(getuid()) and a chdir(),
1912 we may not be able to open file fname anymore, so make
1916 if(complain) raw_printf("Cannot open %s as stdin.", fname);
1918 (void) execlp(catmore, "page", (char *)0);
1919 if(complain) raw_printf("Cannot exec %s.", catmore);
1921 if(complain) sleep(10); /* want to wait_synch() but stdin is gone */
1922 terminate(EXIT_FAILURE);
1926 #else /* DEF_PAGER */
1932 tty_clear_nhwindow(WIN_MESSAGE);
1933 f = dlb_fopen(fname, "r");
1936 home(); tty_mark_synch(); tty_raw_print("");
1937 perror(fname); tty_wait_synch();
1938 pline("Cannot open \"%s\".", fname);
1939 } else if(u.ux) docrt();
1941 winid datawin = tty_create_nhwindow(NHW_TEXT);
1942 boolean empty = TRUE;
1949 /* attempt to scroll text below map window if there's room */
1950 wins[datawin]->offy = wins[WIN_STATUS]->offy+3;
1951 if((int) wins[datawin]->offy + 12 > (int) ttyDisplay->rows)
1952 wins[datawin]->offy = 0;
1954 while (dlb_fgets(buf, BUFSZ, f)) {
1955 if ((cr = index(buf, '\n')) != 0) *cr = 0;
1957 if ((cr = index(buf, '\r')) != 0) *cr = 0;
1959 if (index(buf, '\t') != 0) (void) tabexpand(buf);
1961 tty_putstr(datawin, 0, buf);
1962 if(wins[datawin]->flags & WIN_CANCELLED)
1965 if (!empty) tty_display_nhwindow(datawin, FALSE);
1966 tty_destroy_nhwindow(datawin);
1967 (void) dlb_fclose(f);
1970 #endif /* DEF_PAGER */
1974 tty_start_menu(window)
1977 tty_clear_nhwindow(window);
1983 * Add a menu item to the beginning of the menu list. This list is reversed
1987 tty_add_menu(window, glyph, identifier, ch, gch, attr, str, preselected)
1988 winid window; /* window to use, must be of type NHW_MENU */
1989 int glyph; /* glyph to display with item (unused) */
1990 const anything *identifier; /* what to return if selected */
1991 char ch; /* keyboard accelerator (0 = pick our own) */
1992 char gch; /* group accelerator (0 = no group) */
1993 int attr; /* attribute for string (like tty_putstr()) */
1994 const char *str; /* menu string */
1995 boolean preselected; /* item is marked as selected */
1997 register struct WinDesc *cw = 0;
1998 tty_menu_item *item;
2002 if (str == (const char*) 0)
2005 if (window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0
2006 || cw->type != NHW_MENU)
2007 panic(winpanicstr, window);
2010 if (identifier->a_void) {
2011 int len = strlen(str);
2013 /* We *think* everything's coming in off at most BUFSZ bufs... */
2014 impossible("Menu item too long (%d).", len);
2017 Sprintf(buf, "%c - ", ch ? ch : '?');
2018 (void) strncpy(buf+4, str, len);
2024 item = (tty_menu_item *) alloc(sizeof(tty_menu_item));
2025 item->identifier = *identifier;
2027 item->selected = preselected;
2028 item->selector = ch;
2029 item->gselector = gch;
2031 item->str = copy_of(newstr);
2033 item->next = cw->mlist;
2037 /* Invert the given list, can handle NULL as an input. */
2038 STATIC_OVL tty_menu_item *
2040 tty_menu_item *curr;
2042 tty_menu_item *next, *head = 0;
2054 * End a menu in this window, window must a type NHW_MENU. This routine
2055 * processes the string list. We calculate the # of pages, then assign
2056 * keyboard accelerators as needed. Finally we decide on the width and
2057 * height of the window.
2060 tty_end_menu(window, prompt)
2061 winid window; /* menu to use */
2062 const char *prompt; /* prompt to for menu */
2064 struct WinDesc *cw = 0;
2065 tty_menu_item *curr;
2070 if (window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0 ||
2071 cw->type != NHW_MENU)
2072 panic(winpanicstr, window);
2074 /* Reverse the list so that items are in correct order. */
2075 cw->mlist = reverse(cw->mlist);
2077 /* Put the promt at the beginning of the menu. */
2081 any.a_void = 0; /* not selectable */
2082 tty_add_menu(window, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
2083 tty_add_menu(window, NO_GLYPH, &any, 0, 0, ATR_NONE, prompt, MENU_UNSELECTED);
2086 lmax = min(52, (int)ttyDisplay->rows - 1); /* # lines per page */
2087 cw->npages = (cw->nitems + (lmax - 1)) / lmax; /* # of pages */
2089 /* make sure page list is large enough */
2090 if (cw->plist_size < cw->npages+1 /*need 1 slot beyond last*/) {
2091 if (cw->plist) free((genericptr_t)cw->plist);
2092 cw->plist_size = cw->npages + 1;
2093 cw->plist = (tty_menu_item **)
2094 alloc(cw->plist_size * sizeof(tty_menu_item *));
2097 cw->cols = 0; /* cols is set when the win is initialized... (why?) */
2098 menu_ch = '?'; /* lint suppression */
2099 for (n = 0, curr = cw->mlist; curr; n++, curr = curr->next) {
2100 /* set page boundaries and character accelerators */
2101 if ((n % lmax) == 0) {
2103 cw->plist[n/lmax] = curr;
2105 if (curr->identifier.a_void && !curr->selector) {
2106 curr->str[0] = curr->selector = menu_ch;
2107 if (menu_ch++ == 'z') menu_ch = 'A';
2110 /* cut off any lines that are too long */
2111 len = strlen(curr->str) + 2; /* extra space at beg & end */
2112 if (len > (int)ttyDisplay->cols) {
2113 curr->str[ttyDisplay->cols-2] = 0;
2114 len = ttyDisplay->cols;
2116 if (len > cw->cols) cw->cols = len;
2118 cw->plist[cw->npages] = 0; /* plist terminator */
2121 * If greater than 1 page, morestr is "(x of y) " otherwise, "(end) "
2123 if (cw->npages > 1) {
2125 /* produce the largest demo string */
2126 Sprintf(buf, "(%d of %d) ", cw->npages, cw->npages);
2128 cw->morestr = copy_of("");
2130 cw->morestr = copy_of("(end) ");
2131 len = strlen(cw->morestr);
2134 if (len > (int)ttyDisplay->cols) {
2135 /* truncate the prompt if its too long for the screen */
2136 if (cw->npages <= 1) /* only str in single page case */
2137 cw->morestr[ttyDisplay->cols] = 0;
2138 len = ttyDisplay->cols;
2140 if (len > cw->cols) cw->cols = len;
2142 cw->maxcol = cw->cols;
2145 * The number of lines in the first page plus the morestr will be the
2146 * maximum size of the window.
2149 cw->maxrow = cw->rows = lmax + 1;
2151 cw->maxrow = cw->rows = cw->nitems + 1;
2155 tty_select_menu(window, how, menu_list)
2158 menu_item **menu_list;
2160 register struct WinDesc *cw = 0;
2161 tty_menu_item *curr;
2165 if(window == WIN_ERR || (cw = wins[window]) == (struct WinDesc *) 0
2166 || cw->type != NHW_MENU)
2167 panic(winpanicstr, window);
2169 *menu_list = (menu_item *) 0;
2170 cw->how = (short) how;
2172 tty_display_nhwindow(window, TRUE);
2173 cancelled = !!(cw->flags & WIN_CANCELLED);
2174 tty_dismiss_nhwindow(window); /* does not destroy window data */
2179 for (n = 0, curr = cw->mlist; curr; curr = curr->next)
2180 if (curr->selected) n++;
2184 *menu_list = (menu_item *) alloc(n * sizeof(menu_item));
2185 for (mi = *menu_list, curr = cw->mlist; curr; curr = curr->next)
2186 if (curr->selected) {
2187 mi->item = curr->identifier;
2188 mi->count = curr->count;
2196 /* special hack for treating top line --More-- as a one item menu */
2198 tty_message_menu(let, how, mesg)
2203 /* "menu" without selection; use ordinary pline, no more() */
2204 if (how == PICK_NONE) {
2209 ttyDisplay->dismiss_more = let;
2211 /* barebones pline(); since we're only supposed to be called after
2212 response to a prompt, we'll assume that the display is up to date */
2213 tty_putstr(WIN_MESSAGE, 0, mesg);
2214 /* if `mesg' didn't wrap (triggering --More--), force --More-- now */
2215 if (ttyDisplay->toplin == 1) {
2217 ttyDisplay->toplin = 1; /* more resets this */
2218 tty_clear_nhwindow(WIN_MESSAGE);
2220 /* normally <ESC> means skip further messages, but in this case
2221 it means cancel the current prompt; any other messages should
2222 continue to be output normally */
2223 wins[WIN_MESSAGE]->flags &= ~WIN_CANCELLED;
2224 ttyDisplay->dismiss_more = 0;
2226 return ((how == PICK_ONE && morc == let) || morc == '\033') ? morc : '\0';
2230 tty_update_inventory()
2238 (void) fflush(stdout);
2244 /* we just need to make sure all windows are synch'd */
2245 if(!ttyDisplay || ttyDisplay->rawprint) {
2247 if(ttyDisplay) ttyDisplay->rawprint = 0;
2249 tty_display_nhwindow(WIN_MAP, FALSE);
2250 if(ttyDisplay->inmore) {
2251 addtopl("--More--");
2252 (void) fflush(stdout);
2253 } else if(ttyDisplay->inread > program_state.gameover) {
2254 /* this can only happen if we were reading and got interrupted */
2255 ttyDisplay->toplin = 3;
2256 /* do this twice; 1st time gets the Quit? message again */
2257 (void) tty_doprev_message();
2258 (void) tty_doprev_message();
2260 (void) fflush(stdout);
2266 docorner(xmin, ymax)
2267 register int xmin, ymax;
2270 register struct WinDesc *cw = wins[WIN_MAP];
2272 if (u.uswallow) { /* Can be done more efficiently */
2277 #if defined(SIGWINCH) && defined(CLIPPING)
2278 if(ymax > LI) ymax = LI; /* can happen if window gets smaller */
2280 for (y = 0; y < ymax; y++) {
2281 tty_curs(BASE_WINDOW, xmin,y); /* move cursor */
2282 cl_end(); /* clear to end of line */
2284 if (y<(int) cw->offy || y+clipy > ROWNO)
2285 continue; /* only refresh board */
2286 #if defined(USE_TILES) && defined(MSDOS)
2287 if (iflags.tile_view)
2288 row_refresh((xmin/2)+clipx-((int)cw->offx/2),COLNO-1,y+clipy-(int)cw->offy);
2291 row_refresh(xmin+clipx-(int)cw->offx,COLNO-1,y+clipy-(int)cw->offy);
2293 if (y<cw->offy || y > ROWNO) continue; /* only refresh board */
2294 row_refresh(xmin-(int)cw->offx,COLNO-1,y-(int)cw->offy);
2299 if (ymax >= (int) wins[WIN_STATUS]->offy) {
2300 /* we have wrecked the bottom line */
2309 #if defined(ASCIIGRAPH) && !defined(NO_TERMS)
2316 if(ttyDisplay->color != NO_COLOR) {
2318 ttyDisplay->color = NO_COLOR;
2328 register char ch = (char)in_ch;
2330 # if defined(ASCIIGRAPH) && !defined(NO_TERMS)
2331 if (iflags.IBMgraphics || iflags.eight_bit_tty) {
2332 /* IBM-compatible displays don't need other stuff */
2334 } else if (ch & 0x80) {
2335 if (!GFlag || HE_resets_AS) {
2339 (void) putchar((ch ^ 0x80)); /* Strip 8th bit */
2351 #endif /* ASCIIGRAPH && !NO_TERMS */
2368 tty_cliparound(x, y)
2371 extern boolean restoring;
2372 int oldx = clipx, oldy = clipy;
2374 if (!clipping) return;
2375 if (x < clipx + 5) {
2376 clipx = max(0, x - 20);
2377 clipxmax = clipx + CO;
2379 else if (x > clipxmax - 5) {
2380 clipxmax = min(COLNO, clipxmax + 20);
2381 clipx = clipxmax - CO;
2383 if (y < clipy + 2) {
2384 clipy = max(0, y - (clipymax - clipy) / 2);
2385 clipymax = clipy + (LI - 3);
2387 else if (y > clipymax - 2) {
2388 clipymax = min(ROWNO, clipymax + (clipymax - clipy) / 2);
2389 clipy = clipymax - (LI - 3);
2391 if (clipx != oldx || clipy != oldy) {
2392 if (on_level(&u.uz0, &u.uz) && !restoring)
2396 #endif /* CLIPPING */
2402 * Print the glyph to the output device. Don't flush the output device.
2404 * Since this is only called from show_glyph(), it is assumed that the
2405 * position and glyph are always correct (checked there)!
2409 tty_print_glyph(window, x, y, glyph)
2415 boolean reverse_on = FALSE;
2421 if(x <= clipx || y < clipy || x >= clipxmax || y >= clipymax)
2425 /* map glyph to character and color */
2426 mapglyph(glyph, &ch, &color, &special, x, y);
2428 /* Move the cursor. */
2429 tty_curs(window, x,y);
2432 if (ul_hack && ch == '_') { /* non-destructive underscore */
2433 (void) putchar((char) ' ');
2439 if (color != ttyDisplay->color) {
2440 if(ttyDisplay->color != NO_COLOR)
2442 ttyDisplay->color = color;
2443 if(color != NO_COLOR)
2444 term_start_color(color);
2446 #endif /* TEXTCOLOR */
2448 /* must be after color check; term_end_color may turn off inverse too */
2449 if (((special & MG_PET) && iflags.hilite_pet) ||
2450 ((special & MG_DETECT) && iflags.use_inverse)) {
2451 term_start_attr(ATR_INVERSE);
2455 #if defined(USE_TILES) && defined(MSDOS)
2456 if (iflags.grmode && iflags.tile_view)
2457 xputg(glyph,ch,special);
2460 g_putch(ch); /* print the character */
2463 term_end_attr(ATR_INVERSE);
2465 /* turn off color as well, ATR_INVERSE may have done this already */
2466 if(ttyDisplay->color != NO_COLOR) {
2468 ttyDisplay->color = NO_COLOR;
2473 wins[window]->curx++; /* one character over */
2474 ttyDisplay->curx++; /* the real cursor moved too */
2481 if(ttyDisplay) ttyDisplay->rawprint++;
2482 #if defined(MICRO) || defined(WIN32CON)
2485 puts(str); (void) fflush(stdout);
2490 tty_raw_print_bold(str)
2493 if(ttyDisplay) ttyDisplay->rawprint++;
2494 term_start_raw_bold();
2495 #if defined(MICRO) || defined(WIN32CON)
2498 (void) fputs(str, stdout);
2500 term_end_raw_bold();
2501 #if defined(MICRO) || defined(WIN32CON)
2505 (void) fflush(stdout);
2514 /* kludge alert: Some Unix variants return funny values if getc()
2515 * is called, interrupted, and then called again. There
2516 * is non-reentrant code in the internal _filbuf() routine, called by
2519 static volatile int nesting = 0;
2523 (void) fflush(stdout);
2524 /* Note: if raw_print() and wait_synch() get called to report terminal
2525 * initialization problems, then wins[] and ttyDisplay might not be
2526 * available yet. Such problems will probably be fatal before we get
2527 * here, but validate those pointers just in case...
2529 if (WIN_MESSAGE != WIN_ERR && wins[WIN_MESSAGE])
2530 wins[WIN_MESSAGE]->flags &= ~WIN_STOP;
2532 i = ((++nesting == 1) ? tgetch() :
2533 (read(fileno(stdin), (genericptr_t)&nestbuf,1) == 1 ? (int)nestbuf :
2539 if (!i) i = '\033'; /* map NUL to ESC since nethack doesn't expect NUL */
2540 if (ttyDisplay && ttyDisplay->toplin == 1)
2541 ttyDisplay->toplin = 2;
2546 * return a key, or 0, in which case a mouse button was pressed
2547 * mouse events should be returned as character postitions in the map window.
2548 * Since normal tty's don't have mice, just return a key.
2552 tty_nh_poskey(x, y, mod)
2555 # if defined(WIN32CON)
2557 (void) fflush(stdout);
2558 /* Note: if raw_print() and wait_synch() get called to report terminal
2559 * initialization problems, then wins[] and ttyDisplay might not be
2560 * available yet. Such problems will probably be fatal before we get
2561 * here, but validate those pointers just in case...
2563 if (WIN_MESSAGE != WIN_ERR && wins[WIN_MESSAGE])
2564 wins[WIN_MESSAGE]->flags &= ~WIN_STOP;
2565 i = ntposkey(x, y, mod);
2566 if (!i && mod && *mod == 0)
2567 i = '\033'; /* map NUL to ESC since nethack doesn't expect NUL */
2568 if (ttyDisplay && ttyDisplay->toplin == 1)
2569 ttyDisplay->toplin = 2;
2572 return tty_nhgetch();
2579 # if defined(WIN32CON)
2587 tty_update_positionbar(posbar)
2591 video_update_positionbar(posbar);
2597 * Allocate a copy of the given string. If null, return a string of
2600 * This is an exact duplicate of copy_of() in X11/winmenu.c.
2607 return strcpy((char *) alloc((unsigned) (strlen(s) + 1)), s);
2610 #endif /* TTY_GRAPHICS */