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 $
33 * $DragonFly: src/usr.sbin/i4b/isdnmonitor/curses.c,v 1.2 2003/06/17 06:29:55 dillon Exp $
35 * last edit-date: [Mon Dec 13 21:51:47 1999]
37 *---------------------------------------------------------------------------*/
39 #include "monprivate.h"
43 static void display_bell(void);
44 static void display_chans(void);
46 /*---------------------------------------------------------------------------*
48 *---------------------------------------------------------------------------*/
57 /*---------------------------------------------------------------------------*
58 * init curses fullscreen display
59 *---------------------------------------------------------------------------*/
67 initscr(); /* curses init */
69 if((COLS
< 80) || (LINES
< 24))
72 fprintf(stderr
, "ERROR, minimal screensize must be 80x24, is %dx%d, terminating!",COLS
, LINES
);
79 uheight
= nctrl
* 2; /* cards * b-channels */
80 lheight
= LINES
- uheight
- 6 + 1; /* rest of display */
82 if((upper_w
= newwin(uheight
, COLS
, UPPER_B
, 0)) == NULL
)
85 fprintf(stderr
, "ERROR, curses init upper window, terminating!");
89 if((mid_w
= newwin(1, COLS
, UPPER_B
+uheight
+1, 0)) == NULL
)
92 fprintf(stderr
, "ERROR, curses init mid window, terminating!");
96 if((lower_w
= newwin(lheight
, COLS
, UPPER_B
+uheight
+3, 0)) == NULL
)
99 fprintf(stderr
, "ERROR, curses init lower window, LINES = %d, lheight = %d, uheight = %d, terminating!", LINES
, lheight
, uheight
);
103 scrollok(lower_w
, 1);
105 sprintf(buffer
, "----- isdn controller channel state ------------- isdnmonitor %02d.%02d.%d -", VERSION
, REL
, STEP
);
107 while(strlen(buffer
) < COLS
)
116 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
117 addstr("c tei b remote iface dir outbytes obps inbytes ibps units");
120 sprintf(buffer
, "----- isdn userland interface state ------------- %s:%d -", hostname
, portno
);
122 sprintf(buffer
, "----- isdn userland interface state ------------- %s -", sockpath
);
124 while(strlen(buffer
) < COLS
)
132 sprintf(buffer
, "----- isdnd logfile display --------------------------------------------------");
133 while(strlen(buffer
) < COLS
)
143 for(i
=0, j
=0; i
<= nctrl
; i
++, j
+=2)
145 mvwprintw(upper_w
, j
, H_CNTL
, "%d --- 1 ", i
); /*TEI*/
146 mvwprintw(upper_w
, j
+1, H_CNTL
, " L12 2 ");
151 for(i
=0, j
=0; i
< nentries
; i
++) /* walk thru all entries */
153 p
= &cfg_entry_tab
[i
]; /* get ptr to enry */
155 mvwprintw(mid_w
, 0, j
, "%s%d ", bdrivername(p
->usrdevicename
), p
->usrdeviceunit
);
159 j
+= ((strlen(bdrivername(p
->usrdevicename
)) + (p
->usrdeviceunit
> 9 ? 2 : 1) + 1));
162 mvwprintw(mid_w
, 0, 0, "%s", devbuf
);
166 wmove(lower_w
, 0, 0);
172 /*---------------------------------------------------------------------------*
173 * display the charge in units
174 *---------------------------------------------------------------------------*/
176 display_charge(int pos
, int charge
)
178 mvwprintw(upper_w
, pos
, H_UNITS
, "%d", charge
);
183 /*---------------------------------------------------------------------------*
184 * display the calculated charge in units
185 *---------------------------------------------------------------------------*/
187 display_ccharge(int pos
, int units
)
189 mvwprintw(upper_w
, pos
, H_UNITS
, "(%d)", units
);
194 /*---------------------------------------------------------------------------*
195 * display accounting information
196 *---------------------------------------------------------------------------*/
198 display_acct(int pos
, int obyte
, int obps
, int ibyte
, int ibps
)
200 mvwprintw(upper_w
, pos
, H_OUT
, "%-10d", obyte
);
201 mvwprintw(upper_w
, pos
, H_OUTBPS
, "%-4d", obps
);
202 mvwprintw(upper_w
, pos
, H_IN
, "%-10d", ibyte
);
203 mvwprintw(upper_w
, pos
, H_INBPS
, "%-4d", ibps
);
207 /*---------------------------------------------------------------------------*
208 * erase line at disconnect time
209 *---------------------------------------------------------------------------*/
211 display_disconnect(int pos
)
213 wmove(upper_w
, pos
, H_TELN
);
221 /*---------------------------------------------------------------------------*
222 * display interface up/down information
223 *---------------------------------------------------------------------------*/
225 display_updown(int pos
, int updown
, char *device
)
232 mvwprintw(mid_w
, 0, pos
, "%s ", device
);
238 /*---------------------------------------------------------------------------*
239 * display interface up/down information
240 *---------------------------------------------------------------------------*/
242 display_l12stat(int controller
, int layer
, int state
)
244 if(controller
> nctrl
)
247 if(!(layer
== 1 || layer
== 2))
257 mvwprintw(upper_w
, (controller
*2)+1, H_TEI
+1, "1");
260 mvwprintw(upper_w
, (controller
*2)+1, H_TEI
+2, "2");
264 mvwprintw(upper_w
, (controller
*2)+1, H_TEI
+2, "2");
266 mvwprintw(upper_w
, (controller
*2)+1, H_TEI
+1, "1");
273 /*---------------------------------------------------------------------------*
275 *---------------------------------------------------------------------------*/
277 display_tei(int controller
, int tei
)
279 if(controller
> nctrl
)
283 mvwprintw(upper_w
, controller
*2, H_TEI
, "---");
285 mvwprintw(upper_w
, controller
*2, H_TEI
, "%3d", tei
);
290 /*---------------------------------------------------------------------------*
292 *---------------------------------------------------------------------------*/
296 static char bell
[1] = { 0x07 };
297 write(STDOUT_FILENO
, &bell
[0], 1);
300 /*---------------------------------------------------------------------------*
301 * curses menu for fullscreen command mode
302 *---------------------------------------------------------------------------*/
306 static char *menu
[WMITEMS
] =
308 "1 - (D)isplay refresh",
309 "2 - (H)angup (choose a channel)",
310 "3 - (R)eread config file",
311 "4 - (Q)uit the program",
318 struct timeval timeout
;
320 /* create a new window in the lower screen area */
322 if((menu_w
= newwin(WMENU_HGT
, WMENU_LEN
, WMENU_POSLN
, WMENU_POSCO
)) == NULL
)
327 /* create a border around the window */
329 box(menu_w
, '|', '-');
334 mvwaddstr(menu_w
, 0, (WMENU_LEN
/ 2) - (strlen(WMENU_TITLE
) / 2), WMENU_TITLE
);
337 /* fill the window with the menu options */
339 for(mpos
=0; mpos
<= (WMITEMS
-1); mpos
++)
340 mvwaddstr(menu_w
, mpos
+ 2, 2, menu
[mpos
]);
342 /* highlight the first menu option */
346 mvwaddstr(menu_w
, mpos
+ 2, 2, menu
[mpos
]);
356 FD_SET(STDIN_FILENO
, &set
);
357 timeout
.tv_sec
= WMTIMEOUT
;
360 /* if no char is available within timeout, exit menu*/
362 if((select(STDIN_FILENO
+ 1, &set
, NULL
, NULL
, &timeout
)) <= 0)
370 case '\t': /* hilite next option */
371 mvwaddstr(menu_w
, mpos
+ 2, 2, menu
[mpos
]);
376 mvwaddstr(menu_w
, mpos
+ 2, 2, menu
[mpos
]);
380 case ('0'+WREFRESH
+1): /* display refresh */
386 case ('0'+WQUIT
+1): /* quit program */
393 case ('0'+WHANGUP
+1): /* hangup connection */
399 case ('0'+WREREAD
+1): /* reread config file */
406 case '\r': /* exec highlighted option */
435 /* delete the menu window */
439 /* re-display the original lower window contents */
445 /*---------------------------------------------------------------------------*
446 * display connect information
447 *---------------------------------------------------------------------------*/
449 display_connect(int pos
, int dir
, char *name
, char *remtel
, char *dev
)
453 /* remote telephone number */
455 sprintf(buffer
, "%s/%s", name
, remtel
);
457 buffer
[H_IFN
- H_TELN
- 1] = '\0';
459 mvwprintw(upper_w
, pos
, H_TELN
, "%s", buffer
);
463 mvwprintw(upper_w
, pos
, H_IFN
, "%s ", dev
);
465 mvwprintw(upper_w
, pos
, H_IO
, dir
? "out" : "in");
467 mvwprintw(upper_w
, pos
, H_OUT
, "-");
468 mvwprintw(upper_w
, pos
, H_OUTBPS
, "-");
469 mvwprintw(upper_w
, pos
, H_IN
, "-");
470 mvwprintw(upper_w
, pos
, H_INBPS
, "-");
478 /*---------------------------------------------------------------------------*
479 * display channel information for shutdown
480 *---------------------------------------------------------------------------*/
488 int nlines
, ncols
, pos_x
, pos_y
;
490 struct timeval timeout
;
492 /* need this later to close the connection */
498 for(i
= 0; i
< nctrl
; i
++)
500 if(remstate
[i
].ch1state
)
502 if(remstate
[i
].ch2state
)
508 if ((cc
= (struct ctlr_chan
*)malloc (cnt
*
509 sizeof (struct ctlr_chan
))) == NULL
)
522 pos_y
= WMENU_POSLN
+ 4;
523 pos_x
= WMENU_POSCO
+ 10;
525 /* create a new window in the lower screen area */
527 if((chan_w
= newwin(nlines
, ncols
, pos_y
, pos_x
)) == NULL
)
534 /* create a border around the window */
536 box(chan_w
, '|', '-');
541 mvwaddstr(chan_w
, 0, (ncols
/ 2) - (strlen("Channels") / 2), "Channels");
544 /* no active channels */
547 mvwaddstr(chan_w
, 2, 2, "No active channels");
551 /* delete the channels window */
560 for (i
= 0; i
< nctrl
; i
++)
562 if(remstate
[i
].ch1state
)
564 sprintf(buffer
, "%d - Controller %d channel %s", ncols
, i
, "B1");
565 mvwaddstr(chan_w
, nlines
, 2, buffer
);
566 cc
[ncols
- 1].cntl
= i
;
567 cc
[ncols
- 1].chn
= CHAN_B1
;
571 if(remstate
[i
].ch2state
)
573 sprintf(buffer
, "%d - Controller %d channel %s", ncols
, i
, "B2");
574 mvwaddstr(chan_w
, nlines
, 2, buffer
);
575 cc
[ncols
- 1].cntl
= i
;
576 cc
[ncols
- 1].chn
= CHAN_B2
;
587 FD_SET(STDIN_FILENO
, &set
);
588 timeout
.tv_sec
= WMTIMEOUT
;
591 /* if no char is available within timeout, exit menu*/
593 if((select(STDIN_FILENO
+ 1, &set
, NULL
, NULL
, &timeout
)) <= 0)
596 ncols
= wgetch(chan_w
);
598 if (!(isdigit(ncols
)))
604 nlines
= ncols
- '0';
606 if ((nlines
== 0) || (nlines
> cnt
))
612 hangup(cc
[nlines
-1].cntl
, cc
[nlines
-1].chn
);
618 /* delete the channels window */