menu_sys.def revision 1.19
1/* $NetBSD: menu_sys.def,v 1.19 1999/06/20 02:07:18 cgd Exp $ */ 2 3/* 4 * Copyright 1997 Piermont Information Systems Inc. 5 * All rights reserved. 6 * 7 * Written by Philip A. Nelson for Piermont Information Systems Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software develooped for the NetBSD Project by 20 * Piermont Information Systems Inc. 21 * 4. The name of Piermont Information Systems Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 35 * THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 */ 38 39/* menu_sys.defs -- Menu system standard routines. */ 40 41#include <string.h> 42#include <ctype.h> 43 44#define REQ_EXECUTE 1000 45#define REQ_NEXT_ITEM 1001 46#define REQ_PREV_ITEM 1002 47#define REQ_REDISPLAY 1003 48#define REQ_SCROLLDOWN 1004 49#define REQ_SCROLLUP 1005 50#define REQ_HELP 1006 51 52/* Multiple key support */ 53#define KEYSEQ_FIRST 256 54#define KEYSEQ_DOWN_ARROW 256 55#define KEYSEQ_UP_ARROW 257 56#define KEYSEQ_LEFT_ARROW 258 57#define KEYSEQ_RIGHT_ARROW 259 58#define KEYSEQ_PAGE_DOWN 260 59#define KEYSEQ_PAGE_UP 261 60 61struct keyseq { 62 char *termcap_name; 63 char *chars; 64 int numchars; 65 int keyseq_val; 66 struct keyseq *next; 67}; 68 69/* keypad and other definitions */ 70struct keyseq _mc_key_seq[] = { 71 /* Cludge for xterm ... */ 72 { NULL, "\033[B", 0, KEYSEQ_DOWN_ARROW, NULL }, 73 { NULL, "\033[D", 0, KEYSEQ_LEFT_ARROW, NULL }, 74 { NULL, "\033[C", 0, KEYSEQ_RIGHT_ARROW, NULL }, 75 { NULL, "\033[A", 0, KEYSEQ_UP_ARROW, NULL }, 76 /* Termcap defined */ 77 { "kd", NULL, 0, KEYSEQ_DOWN_ARROW, NULL }, 78 { "kl", NULL, 0, KEYSEQ_LEFT_ARROW, NULL }, 79 { "kr", NULL, 0, KEYSEQ_RIGHT_ARROW, NULL }, 80 { "ku", NULL, 0, KEYSEQ_UP_ARROW, NULL }, 81 { "kf", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* scroll forward */ 82 { "kN", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* next page */ 83 { "kP", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* scroll backward */ 84 { "kR", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* prev page */ 85 /* other definitions */ 86 { NULL, "\033v", 0, KEYSEQ_PAGE_UP, NULL }, /* ESC-v */ 87 { NULL, "\026", 0, KEYSEQ_PAGE_DOWN, NULL }, /* CTL-v */ 88}; 89 90int _mc_num_key_seq = sizeof(_mc_key_seq) / sizeof(struct keyseq); 91struct keyseq *pad_list = NULL; 92static char str_area [512]; 93static char *str_ptr = str_area; 94 95/* Macros */ 96#define MAX(x,y) ((x)>(y)?(x):(y)) 97#define MIN(x,y) ((x)<(y)?(x):(y)) 98 99/* Initialization state. */ 100static int __menu_init = 0; 101int __m_endwin = 0; 102static int max_lines = 0, max_cols = 0; 103static char *scrolltext = " <: page up, >: page down"; 104 105static menudesc *menus = menu_def; 106 107#ifdef DYNAMIC_MENUS 108static int num_menus = 0; 109static int num_avail = 0; 110#define DYN_INIT_NUM 32 111#endif 112 113/* prototypes for in here! */ 114static void ins_keyseq (struct keyseq **seq, struct keyseq *ins); 115static void init_keyseq (void); 116static void init_menu (struct menudesc *m); 117static char opt_ch (int op_no); 118static void post_menu (struct menudesc *m); 119static void process_help (struct menudesc *m, int num); 120static void process_req (struct menudesc *m, int num, int req); 121static void mbeep (void); 122static int menucmd (WINDOW *w); 123 124#ifndef NULL 125#define NULL (void *)0 126#endif 127 128/* menu system processing routines */ 129 130static void mbeep (void) 131{ 132 fprintf (stderr,"\a"); 133} 134 135static void ins_keyseq (struct keyseq **seq, struct keyseq *ins) 136{ 137 if (*seq == NULL) { 138 ins->next = NULL; 139 *seq = ins; 140 } else if (ins->numchars <= (*seq)->numchars) { 141 ins->next = *seq; 142 *seq = ins; 143 } else 144 ins_keyseq (&(*seq)->next, ins); 145} 146 147static void init_keyseq (void) 148{ 149 int i; 150 for (i=0; i<_mc_num_key_seq; i++) { 151 if (_mc_key_seq[i].termcap_name) 152 _mc_key_seq[i].chars = 153 tgetstr (_mc_key_seq[i].termcap_name, 154 &str_ptr); 155 if (_mc_key_seq[i].chars != NULL && 156 (_mc_key_seq[i].numchars = strlen(_mc_key_seq[i].chars)) 157 > 0) 158 ins_keyseq (&pad_list,&_mc_key_seq[i]); 159 } 160} 161 162static int mgetch(WINDOW *w) 163{ 164 static char buf[20]; 165 static int num = 0; 166 struct keyseq *list = pad_list; 167 int i, ret; 168 169 /* key pad processing */ 170 while (list) { 171 for (i=0; i< list->numchars; i++) { 172 if (i >= num) 173 buf[num++] = wgetch(w); 174 if (buf[i] != list->chars[i]) 175 break; 176 } 177 if (i == list->numchars) { 178 num = 0; 179 return list->keyseq_val; 180 } 181 list = list->next; 182 } 183 184 ret = buf[0]; 185 for (i = 0; i < strlen(buf); i++) 186 buf[i] = buf[i+1]; 187 num--; 188 return ret; 189} 190 191static int menucmd (WINDOW *w) 192{ 193 int ch; 194 195 while (TRUE) { 196 ch = mgetch(w); 197 198 switch (ch) { 199 case '\n': 200 return REQ_EXECUTE; 201 case '\016': /* Contnrol-P */ 202 case KEYSEQ_DOWN_ARROW: 203 return REQ_NEXT_ITEM; 204 case '\020': /* Control-N */ 205 case KEYSEQ_UP_ARROW: 206 return REQ_PREV_ITEM; 207 case '\014': /* Control-L */ 208 return REQ_REDISPLAY; 209 case '<': 210 case '\010': /* Control-H (backspace) */ 211 case KEYSEQ_PAGE_UP: 212 return REQ_SCROLLUP; 213 case '>': 214 case ' ': 215 case KEYSEQ_PAGE_DOWN: 216 return REQ_SCROLLDOWN; 217 case '?': 218 return REQ_HELP; 219 } 220 221 if (isalpha(ch)) 222 return (ch); 223 224 mbeep(); 225 wrefresh(w); 226 } 227} 228 229static void init_menu (struct menudesc *m) 230{ 231 int wmax; 232 int hadd, wadd, exithadd; 233 int i; 234 235 hadd = ((m->mopt & MC_NOBOX) ? 0 : 2); 236 wadd = ((m->mopt & MC_NOBOX) ? 2 : 4); 237 238 hadd += strlen(m->title) != 0 ? 2 : 0; 239 exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1); 240 241 wmax = strlen(m->title); 242 243 /* Calculate h? h == number of visible options. */ 244 if (m->h == 0) { 245 m->h = m->numopts + exithadd; 246 if (m->h + m->y + hadd >= max_lines && (m->mopt & MC_SCROLL)) 247 m->h = max_lines - m->y - hadd ; 248 } 249 250 /* Check window heights and set scrolling */ 251 if (m->h < m->numopts + exithadd) { 252 if (!(m->mopt & MC_SCROLL) || m->h < 3) { 253 endwin(); 254 (void) fprintf (stderr, 255 "Window too short for menu \"%s\"\n", 256 m->title); 257 exit(1); 258 } 259 } else 260 m->mopt &= ~MC_SCROLL; 261 262 /* check for screen fit */ 263 if (m->y + m->h + hadd > max_lines) { 264 endwin(); 265 (void) fprintf (stderr, 266 "Screen too short for menu \"%s\"\n", m->title); 267 exit(1); 268 269 } 270 271 /* Calculate w? */ 272 if (m->w == 0) { 273 if (m->mopt & MC_SCROLL) 274 wmax = MAX(wmax,strlen(scrolltext)); 275 for (i=0; i < m->numopts; i++ ) 276 wmax = MAX(wmax,strlen(m->opts[i].opt_name)+3); 277 m->w = wmax; 278 } 279 280 /* check and adjust for screen fit */ 281 if (m->w + wadd > max_cols) { 282 endwin(); 283 (void) fprintf (stderr, 284 "Screen too narrow for menu \"%s\"\n", m->title); 285 exit(1); 286 287 } 288 if (m->x + m->w + wadd > max_cols) 289 m->x = max_cols - (m->w + wadd); 290 291 /* Get the windows. */ 292 m->mw = newwin(m->h+hadd, m->w+wadd, m->y, m->x); 293 294 if (m->mw == NULL) { 295 endwin(); 296 (void) fprintf (stderr, 297 "Could not create window for menu \"%s\"\n", m->title); 298 exit(1); 299 } 300} 301 302static char opt_ch (int op_no) 303{ 304 char c; 305 if (op_no < 25) { 306 c = 'a' + op_no; 307 if (c >= 'x') c++; 308 } else 309 c = 'A' + op_no - 25; 310 return (char) c; 311} 312 313static void post_menu (struct menudesc *m) 314{ 315 int i; 316 int hasbox, cury, maxy, selrow, lastopt; 317 int tadd; 318 char optstr[5]; 319 320 if (m->mopt & MC_NOBOX) { 321 cury = 0; 322 maxy = m->h; 323 hasbox = 0; 324 } else { 325 cury = 1; 326 maxy = m->h+1; 327 hasbox = 1; 328 } 329 330 /* Clear the window */ 331 wclear (m->mw); 332 333 tadd = strlen(m->title) ? 2 : 0; 334 335 if (tadd) { 336 mvwaddstr(m->mw, cury, cury, " "); 337 mvwaddstr(m->mw, cury, cury + 1, m->title); 338 cury += 2; 339 maxy += 2; 340 } 341 342 /* Set defaults, calculate lastopt. */ 343 selrow = -1; 344 if (m->mopt & MC_SCROLL) { 345 lastopt = MIN(m->numopts, m->topline+m->h-1); 346 maxy -= 1; 347 } else 348 lastopt = m->numopts; 349 350 for (i=m->topline; i<lastopt; i++, cury++) { 351 if (m->cursel == i) { 352 mvwaddstr (m->mw, cury, hasbox, ">"); 353 wstandout(m->mw); 354 selrow = cury; 355 } else 356 mvwaddstr (m->mw, cury, hasbox, " "); 357 (void) sprintf (optstr, "%c: ", opt_ch(i)); 358 waddstr (m->mw, optstr); 359 waddstr (m->mw, m->opts[i].opt_name); 360 if (m->cursel == i) 361 wstandend(m->mw); 362 } 363 364 /* Add the exit option. */ 365 if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) { 366 if (m->cursel >= m->numopts) { 367 mvwaddstr (m->mw, cury, hasbox, ">"); 368 wstandout(m->mw); 369 selrow = cury; 370 } else 371 mvwaddstr (m->mw, cury, hasbox, " "); 372 waddstr (m->mw, "x: "); 373 waddstr (m->mw, m->exitstr); 374 if (m->cursel >= m->numopts) 375 wstandend(m->mw); 376 cury++; 377 } 378 379 /* Add the scroll line */ 380 if (m->mopt & MC_SCROLL) { 381 mvwaddstr (m->mw, cury, hasbox, scrolltext); 382 if (selrow < 0) 383 selrow = cury; 384 } 385 386 /* Add the box. */ 387 if (!(m->mopt & MC_NOBOX)) 388 box(m->mw, '*', '*'); 389 390 wmove(m->mw, selrow, hasbox); 391} 392 393static void process_help (struct menudesc *m, int num) 394{ 395 char *help = m->helpstr; 396 int lineoff = 0; 397 int curoff = 0; 398 int again; 399 int winin; 400 401 /* Is there help? */ 402 if (!help) { 403 mbeep(); 404 return; 405 } 406 407 /* Display the help information. */ 408 do { 409 if (lineoff < curoff) { 410 help = m->helpstr; 411 curoff = 0; 412 } 413 while (*help && curoff < lineoff) { 414 if (*help == '\n') 415 curoff++; 416 help++; 417 } 418 419 wclear(stdscr); 420 mvwaddstr (stdscr, 0, 0, 421 "Help: exit: x, page up: u <, page down: d >"); 422 mvwaddstr (stdscr, 2, 0, help); 423 wmove (stdscr, 1, 0); 424 wrefresh(stdscr); 425 426 do { 427 winin = mgetch(stdscr); 428 if (winin < KEYSEQ_FIRST) 429 winin = tolower(winin); 430 again = 0; 431 switch (winin) { 432 case '<': 433 case 'u': 434 case KEYSEQ_UP_ARROW: 435 case KEYSEQ_LEFT_ARROW: 436 case KEYSEQ_PAGE_UP: 437 if (lineoff) 438 lineoff -= max_lines - 2; 439 else 440 again = 1; 441 break; 442 case '>': 443 case 'd': 444 case KEYSEQ_DOWN_ARROW: 445 case KEYSEQ_RIGHT_ARROW: 446 case KEYSEQ_PAGE_DOWN: 447 if (*help) 448 lineoff += max_lines - 2; 449 else 450 again = 1; 451 break; 452 case 'q': 453 break; 454 case 'x': 455 winin = 'q'; 456 break; 457 default: 458 again = 1; 459 } 460 if (again) 461 mbeep(); 462 } while (again); 463 } while (winin != 'q'); 464 465 /* Restore current menu */ 466 wclear(stdscr); 467 wrefresh(stdscr); 468 if (m->post_act) 469 (*m->post_act)(); 470} 471 472static void process_req (struct menudesc *m, int num, int req) 473{ 474 int ch; 475 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 ); 476 int refresh = 0; 477 int scroll_sel = 0; 478 479 if (req == REQ_EXECUTE) 480 return; 481 482 else if (req == REQ_NEXT_ITEM) { 483 if (m->cursel < m->numopts + hasexit - 1) { 484 m->cursel++; 485 scroll_sel = 1; 486 refresh = 1; 487 if (m->mopt & MC_SCROLL && 488 m->cursel >= m->topline + m->h -1 ) 489 m->topline += 1; 490 } else 491 mbeep(); 492 493 } else if (req == REQ_PREV_ITEM) { 494 if (m->cursel > 0) { 495 m->cursel--; 496 scroll_sel = 1; 497 refresh = 1; 498 if (m->cursel < m->topline ) 499 m->topline -= 1; 500 } else 501 mbeep(); 502 503 } else if (req == REQ_REDISPLAY) { 504 wclear(stdscr); 505 wrefresh(stdscr); 506 if (m->post_act) 507 (*m->post_act)(); 508 refresh = 1; 509 510 } else if (req == REQ_HELP) { 511 process_help (m, num); 512 refresh = 1; 513 514 } else if (req == REQ_SCROLLUP) { 515 if (!(m->mopt & MC_SCROLL)) 516 mbeep(); 517 else if (m->topline == 0) 518 mbeep(); 519 else { 520 m->topline = MAX(0,m->topline-m->h+1); 521 wclear (m->mw); 522 refresh = 1; 523 } 524 525 } else if (req == REQ_SCROLLDOWN) { 526 if (!(m->mopt & MC_SCROLL)) 527 mbeep(); 528 else if (m->topline + m->h - 1 >= m->numopts + hasexit) 529 mbeep(); 530 else { 531 m->topline = MIN(m->topline+m->h-1, 532 m->numopts+hasexit-m->h+1); 533 wclear (m->mw); 534 refresh = 1; 535 } 536 537 } else { 538 ch = req; 539 if (ch == 'x' && hasexit) { 540 m->cursel = m->numopts; 541 scroll_sel = 1; 542 refresh = 1; 543 } else { 544 if (ch > 'z') 545 ch = 255; 546 if (ch >= 'a') { 547 if (ch > 'x') ch--; 548 ch = ch - 'a'; 549 } else 550 ch = 25 + ch - 'A'; 551 if (ch < 0 || ch >= m->numopts) 552 mbeep(); 553 else { 554 m->cursel = ch; 555 scroll_sel = 1; 556 refresh = 1; 557 } 558 } 559 } 560 561 if (m->mopt & MC_SCROLL && scroll_sel) { 562 while (m->cursel >= m->topline + m->h -1 ) 563 m->topline = MIN(m->topline+m->h-1, 564 m->numopts+hasexit-m->h+1); 565 while (m->cursel < m->topline) 566 m->topline = MAX(0,m->topline-m->h+1); 567 } 568 569 if (refresh) { 570 post_menu (m); 571 wrefresh (m->mw); 572 } 573} 574 575int menu_init (void) 576{ 577 578 if (__menu_init) 579 return 0; 580 581 if (initscr() == NULL) 582 return 1; 583 584 cbreak(); 585 noecho(); 586 max_lines = getmaxy(stdscr); 587 max_cols = getmaxx(stdscr); 588 init_keyseq(); 589#ifdef DYNAMIC_MENUS 590 num_menus = DYN_INIT_NUM; 591 while (num_menus < DYN_MENU_START) 592 num_menus *= 2; 593 menus = (menudesc *) malloc(sizeof(menudesc)*num_menus); 594 if (menus == NULL) 595 return 2; 596 (void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus); 597 (void) memcpy ((void *)menus, (void *)menu_def, 598 sizeof(menudesc)*DYN_MENU_START); 599 num_avail = num_menus - DYN_MENU_START; 600#endif 601 602 __menu_init = 1; 603 return (0); 604} 605 606void process_menu (int num) 607{ 608 int sel = 0; 609 int req, done; 610 int last_num; 611 612 struct menudesc *m; 613 614 m = &menus[num]; 615 616 done = FALSE; 617 618 /* Initialize? */ 619 if (menu_init()) { 620 __menu_initerror(); 621 return; 622 } 623 624 if (__m_endwin) { 625 wclear(stdscr); 626 wrefresh(stdscr); 627 __m_endwin = 0; 628 } 629 if (m->mw == NULL) 630 init_menu (m); 631 632 /* Always preselect option 0 and display from 0! */ 633 m->cursel = 0; 634 m->topline = 0; 635 636 while (!done) { 637 last_num = num; 638 if (__m_endwin) { 639 wclear(stdscr); 640 wrefresh(stdscr); 641 __m_endwin = 0; 642 } 643 /* Process the display action */ 644 if (m->post_act) 645 (*m->post_act)(); 646 post_menu (m); 647 wrefresh (m->mw); 648 649 while ((req = menucmd (m->mw)) != REQ_EXECUTE) 650 process_req (m, num, req); 651 652 sel = m->cursel; 653 wclear (m->mw); 654 wrefresh (m->mw); 655 656 /* Process the items */ 657 if (sel < m->numopts) { 658 if (m->opts[sel].opt_flags & OPT_ENDWIN) { 659 endwin(); 660 __m_endwin = 1; 661 } 662 if (m->opts[sel].opt_action) 663 done = (*m->opts[sel].opt_action)(); 664 if (m->opts[sel].opt_menu != -1) { 665 if (m->opts[sel].opt_flags & OPT_SUB) 666 process_menu (m->opts[sel].opt_menu); 667 else 668 num = m->opts[sel].opt_menu; 669 } 670 671 if (m->opts[sel].opt_flags & OPT_EXIT) 672 done = TRUE; 673 674 } else 675 done = TRUE; 676 677 /* Reselect m just in case */ 678 if (num != last_num) { 679 m = &menus[num]; 680 681 /* Initialize? */ 682 if (m->mw == NULL) 683 init_menu (m); 684 if (m->post_act) 685 (*m->post_act)(); 686 } 687 } 688 689 /* Process the exit action */ 690 if (m->exit_act) 691 (*m->exit_act)(); 692} 693 694/* Control L is end of standard routines, remaining only for dynamic. */ 695 696/* Beginning of routines for dynamic menus. */ 697 698/* local prototypes */ 699static int double_menus (void); 700 701static int double_menus (void) 702{ 703 menudesc *temp; 704 705 temp = (menudesc *) malloc(sizeof(menudesc)*num_menus*2); 706 if (temp == NULL) 707 return 0; 708 (void) memset ((void *)temp, 0, 709 sizeof(menudesc)*num_menus*2); 710 (void) memcpy ((void *)temp, (void *)menus, 711 sizeof(menudesc)*num_menus); 712 free (menus); 713 menus = temp; 714 num_avail = num_menus; 715 num_menus *= 2; 716 717 return 1; 718} 719 720int new_menu (char * title, menu_ent * opts, int numopts, 721 int x, int y, int h, int w, int mopt, 722 void (*post_act)(void), void (*exit_act), char * help) 723{ 724 int ix; 725 726 /* Check for free menu entry. */ 727 if (num_avail == 0) 728 if (!double_menus ()) 729 return -1; 730 731 /* Find free menu entry. */ 732 for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID; 733 ix++) /* do nothing */; 734 735 /* if ix == num_menus ... panic */ 736 737 /* Set Entries */ 738 menus[ix].title = title; 739 menus[ix].opts = opts; 740 menus[ix].numopts = numopts; 741 menus[ix].x = x; 742 menus[ix].y = y; 743 menus[ix].h = h; 744 menus[ix].w = w; 745 menus[ix].mopt = mopt | MC_VALID; 746 menus[ix].post_act = post_act; 747 menus[ix].exit_act = exit_act; 748 menus[ix].helpstr = help; 749 750 init_menu (&menus[ix]); 751 752 return ix; 753} 754 755void free_menu (int menu_no) 756{ 757 if (menu_no < num_menus) { 758 menus[menu_no].mopt &= ~MC_VALID; 759 if (menus[menu_no].mw != NULL) 760 delwin (menus[menu_no].mw); 761 } 762} 763