2 * Copyright (c) 1999 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 *---------------------------------------------------------------------------
27 * i4b daemon - curses fullscreen output
28 * -------------------------------------
30 * $Id: curses.c,v 1.10 1999/12/13 21:25:25 hm Exp $
32 * $FreeBSD: src/usr.sbin/i4b/isdnmonitor/curses.c,v 1.1.2.1 2001/08/01 17:45:06 obrien Exp $
34 * last edit-date: [Mon Dec 13 21:51:47 1999]
36 *---------------------------------------------------------------------------*/
38 #include "monprivate.h"
42 static void display_bell(void);
43 static void display_chans(void);
45 /*---------------------------------------------------------------------------*
47 *---------------------------------------------------------------------------*/
56 /*---------------------------------------------------------------------------*
57 * init curses fullscreen display
58 *---------------------------------------------------------------------------*/
66 initscr(); /* curses init */
68 if((COLS < 80) || (LINES < 24))
71 fprintf(stderr, "ERROR, minimal screensize must be 80x24, is %dx%d, terminating!",COLS, LINES);
78 uheight = nctrl * 2; /* cards * b-channels */
79 lheight = LINES - uheight - 6 + 1; /* rest of display */
81 if((upper_w = newwin(uheight, COLS, UPPER_B, 0)) == NULL)
84 fprintf(stderr, "ERROR, curses init upper window, terminating!");
88 if((mid_w = newwin(1, COLS, UPPER_B+uheight+1, 0)) == NULL)
91 fprintf(stderr, "ERROR, curses init mid window, terminating!");
95 if((lower_w = newwin(lheight, COLS, UPPER_B+uheight+3, 0)) == NULL)
98 fprintf(stderr, "ERROR, curses init lower window, LINES = %d, lheight = %d, uheight = %d, terminating!", LINES, lheight, uheight);
102 scrollok(lower_w, 1);
104 sprintf(buffer, "----- isdn controller channel state ------------- isdnmonitor %02d.%02d.%d -", VERSION, REL, STEP);
106 while(strlen(buffer) < COLS)
115 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
116 addstr("c tei b remote iface dir outbytes obps inbytes ibps units");
119 sprintf(buffer, "----- isdn userland interface state ------------- %s:%d -", hostname, portno);
121 sprintf(buffer, "----- isdn userland interface state ------------- %s -", sockpath);
123 while(strlen(buffer) < COLS)
131 sprintf(buffer, "----- isdnd logfile display --------------------------------------------------");
132 while(strlen(buffer) < COLS)
142 for(i=0, j=0; i <= nctrl; i++, j+=2)
144 mvwprintw(upper_w, j, H_CNTL, "%d --- 1 ", i); /*TEI*/
145 mvwprintw(upper_w, j+1, H_CNTL, " L12 2 ");
150 for(i=0, j=0; i < nentries; i++) /* walk thru all entries */
152 p = &cfg_entry_tab[i]; /* get ptr to enry */
154 mvwprintw(mid_w, 0, j, "%s%d ", bdrivername(p->usrdevicename), p->usrdeviceunit);
158 j += ((strlen(bdrivername(p->usrdevicename)) + (p->usrdeviceunit > 9 ? 2 : 1) + 1));
161 mvwprintw(mid_w, 0, 0, "%s", devbuf);
165 wmove(lower_w, 0, 0);
171 /*---------------------------------------------------------------------------*
172 * display the charge in units
173 *---------------------------------------------------------------------------*/
175 display_charge(int pos, int charge)
177 mvwprintw(upper_w, pos, H_UNITS, "%d", charge);
182 /*---------------------------------------------------------------------------*
183 * display the calculated charge in units
184 *---------------------------------------------------------------------------*/
186 display_ccharge(int pos, int units)
188 mvwprintw(upper_w, pos, H_UNITS, "(%d)", units);
193 /*---------------------------------------------------------------------------*
194 * display accounting information
195 *---------------------------------------------------------------------------*/
197 display_acct(int pos, int obyte, int obps, int ibyte, int ibps)
199 mvwprintw(upper_w, pos, H_OUT, "%-10d", obyte);
200 mvwprintw(upper_w, pos, H_OUTBPS, "%-4d", obps);
201 mvwprintw(upper_w, pos, H_IN, "%-10d", ibyte);
202 mvwprintw(upper_w, pos, H_INBPS, "%-4d", ibps);
206 /*---------------------------------------------------------------------------*
207 * erase line at disconnect time
208 *---------------------------------------------------------------------------*/
210 display_disconnect(int pos)
212 wmove(upper_w, pos, H_TELN);
220 /*---------------------------------------------------------------------------*
221 * display interface up/down information
222 *---------------------------------------------------------------------------*/
224 display_updown(int pos, int updown, char *device)
231 mvwprintw(mid_w, 0, pos, "%s ", device);
237 /*---------------------------------------------------------------------------*
238 * display interface up/down information
239 *---------------------------------------------------------------------------*/
241 display_l12stat(int controller, int layer, int state)
243 if(controller > nctrl)
246 if(!(layer == 1 || layer == 2))
256 mvwprintw(upper_w, (controller*2)+1, H_TEI+1, "1");
259 mvwprintw(upper_w, (controller*2)+1, H_TEI+2, "2");
263 mvwprintw(upper_w, (controller*2)+1, H_TEI+2, "2");
265 mvwprintw(upper_w, (controller*2)+1, H_TEI+1, "1");
272 /*---------------------------------------------------------------------------*
274 *---------------------------------------------------------------------------*/
276 display_tei(int controller, int tei)
278 if(controller > nctrl)
282 mvwprintw(upper_w, controller*2, H_TEI, "---");
284 mvwprintw(upper_w, controller*2, H_TEI, "%3d", tei);
289 /*---------------------------------------------------------------------------*
291 *---------------------------------------------------------------------------*/
295 static char bell[1] = { 0x07 };
296 write(STDOUT_FILENO, &bell[0], 1);
299 /*---------------------------------------------------------------------------*
300 * curses menu for fullscreen command mode
301 *---------------------------------------------------------------------------*/
305 static char *menu[WMITEMS] =
307 "1 - (D)isplay refresh",
308 "2 - (H)angup (choose a channel)",
309 "3 - (R)eread config file",
310 "4 - (Q)uit the program",
317 struct timeval timeout;
319 /* create a new window in the lower screen area */
321 if((menu_w = newwin(WMENU_HGT, WMENU_LEN, WMENU_POSLN, WMENU_POSCO )) == NULL)
326 /* create a border around the window */
328 box(menu_w, '|', '-');
333 mvwaddstr(menu_w, 0, (WMENU_LEN / 2) - (strlen(WMENU_TITLE) / 2), WMENU_TITLE);
336 /* fill the window with the menu options */
338 for(mpos=0; mpos <= (WMITEMS-1); mpos++)
339 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
341 /* highlight the first menu option */
345 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
355 FD_SET(STDIN_FILENO, &set);
356 timeout.tv_sec = WMTIMEOUT;
359 /* if no char is available within timeout, exit menu*/
361 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
369 case '\t': /* hilite next option */
370 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
375 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
379 case ('0'+WREFRESH+1): /* display refresh */
385 case ('0'+WQUIT+1): /* quit program */
392 case ('0'+WHANGUP+1): /* hangup connection */
398 case ('0'+WREREAD+1): /* reread config file */
405 case '\r': /* exec highlighted option */
434 /* delete the menu window */
438 /* re-display the original lower window contents */
444 /*---------------------------------------------------------------------------*
445 * display connect information
446 *---------------------------------------------------------------------------*/
448 display_connect(int pos, int dir, char *name, char *remtel, char *dev)
452 /* remote telephone number */
454 sprintf(buffer, "%s/%s", name, remtel);
456 buffer[H_IFN - H_TELN - 1] = '\0';
458 mvwprintw(upper_w, pos, H_TELN, "%s", buffer);
462 mvwprintw(upper_w, pos, H_IFN, "%s ", dev);
464 mvwprintw(upper_w, pos, H_IO, dir ? "out" : "in");
466 mvwprintw(upper_w, pos, H_OUT, "-");
467 mvwprintw(upper_w, pos, H_OUTBPS, "-");
468 mvwprintw(upper_w, pos, H_IN, "-");
469 mvwprintw(upper_w, pos, H_INBPS, "-");
477 /*---------------------------------------------------------------------------*
478 * display channel information for shutdown
479 *---------------------------------------------------------------------------*/
487 int nlines, ncols, pos_x, pos_y;
489 struct timeval timeout;
491 /* need this later to close the connection */
497 for(i = 0; i < nctrl; i++)
499 if(remstate[i].ch1state)
501 if(remstate[i].ch2state)
507 if ((cc = (struct ctlr_chan *)malloc (cnt *
508 sizeof (struct ctlr_chan))) == NULL)
521 pos_y = WMENU_POSLN + 4;
522 pos_x = WMENU_POSCO + 10;
524 /* create a new window in the lower screen area */
526 if((chan_w = newwin(nlines, ncols, pos_y, pos_x )) == NULL)
533 /* create a border around the window */
535 box(chan_w, '|', '-');
540 mvwaddstr(chan_w, 0, (ncols / 2) - (strlen("Channels") / 2), "Channels");
543 /* no active channels */
546 mvwaddstr(chan_w, 2, 2, "No active channels");
550 /* delete the channels window */
559 for (i = 0; i < nctrl; i++)
561 if(remstate[i].ch1state)
563 sprintf(buffer, "%d - Controller %d channel %s", ncols, i, "B1");
564 mvwaddstr(chan_w, nlines, 2, buffer);
565 cc[ncols - 1].cntl = i;
566 cc[ncols - 1].chn = CHAN_B1;
570 if(remstate[i].ch2state)
572 sprintf(buffer, "%d - Controller %d channel %s", ncols, i, "B2");
573 mvwaddstr(chan_w, nlines, 2, buffer);
574 cc[ncols - 1].cntl = i;
575 cc[ncols - 1].chn = CHAN_B2;
586 FD_SET(STDIN_FILENO, &set);
587 timeout.tv_sec = WMTIMEOUT;
590 /* if no char is available within timeout, exit menu*/
592 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
595 ncols = wgetch(chan_w);
597 if (!(isdigit(ncols)))
603 nlines = ncols - '0';
605 if ((nlines == 0) || (nlines > cnt))
611 hangup(cc[nlines-1].cntl, cc[nlines-1].chn);
617 /* delete the channels window */