2 * Copyright (c) 1999, 2001 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 * isdnphone - main module
28 * =======================
30 * $Id: main.c,v 1.12 1999/12/13 21:25:26 hm Exp $
32 * $FreeBSD: src/usr.sbin/i4b/isdnphone/main.c,v 1.1.2.2 2001/12/16 15:13:38 hm Exp $
33 * $DragonFly: src/usr.sbin/i4b/isdnphone/main.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
35 * last edit-date: [Mon Dec 13 21:53:25 1999]
37 *---------------------------------------------------------------------------*/
42 static void kbd_hdlr(void);
44 /*---------------------------------------------------------------------------*
45 * usage display and exit
46 *---------------------------------------------------------------------------*/
50 fprintf(stderr, "\n");
51 fprintf(stderr, "isdnphone - i4b phone program, version %d.%d.%d, compiled %s %s\n",VERSION, REL, STEP, __DATE__, __TIME__);
52 fprintf(stderr, "usage: isdnphone -d -h -k <string> -n <number> -u <unit>\n");
53 fprintf(stderr, " -d debug\n");
54 fprintf(stderr, " -h hangup\n");
55 fprintf(stderr, " -k string keypad string\n");
56 fprintf(stderr, " -n number dial number\n");
57 fprintf(stderr, " -u unit set unit number\n");
58 fprintf(stderr, "\n");
62 /*---------------------------------------------------------------------------*
64 *---------------------------------------------------------------------------*/
66 main(int argc, char **argv)
77 numberbuffer[0] = '\0';
79 while ((c = getopt(argc, argv, "dhk:n:u:")) != -1)
102 opt_unit = atoi(optarg);
103 if(opt_unit < 0 || opt_unit > 9)
114 sprintf(namebuffer,"%s%d", I4BTELDDEVICE, opt_unit);
116 if((dialerfd = init_dial(namebuffer)) == -1)
119 if(opt_n || opt_h || opt_k)
121 char commandbuffer[80];
123 /* commandline operation goes here */
127 sprintf(commandbuffer, "D%s", number);
132 sprintf(commandbuffer, "K%s", number);
137 sprintf(commandbuffer, "H");
140 if((ret = write(dialerfd, commandbuffer, strlen(commandbuffer))) < 0)
142 fprintf(stderr, "write commandbuffer failed: %s", strerror(errno));
151 if((audiofd = init_audio(AUDIODEVICE)) == -1)
154 /* fullscreen operation here */
158 bschar = erasechar();
161 wmove(main_w, MW_NUMY, MW_NUX + curx);
169 struct timeval timeout;
173 FD_SET(STDIN_FILENO, &set);
174 if(STDIN_FILENO > maxfd)
175 maxfd = STDIN_FILENO;
177 FD_SET(dialerfd, &set);
181 if(state == ST_ACTIVE)
185 FD_SET(audiofd, &set);
203 /* if no char is available within timeout, do something */
206 ret = select(maxfd+1, &set, NULL, NULL, &timeout);
208 ret = select(maxfd+1, &set, NULL, NULL, NULL);
213 if((telfd != -1) && (FD_ISSET(telfd, &set)))
215 message("select from ISDN");
218 if((audiofd != -1) && (FD_ISSET(audiofd, &set)))
220 message("select from audio");
223 if(FD_ISSET(dialerfd, &set))
225 message("select from tel");
228 if(FD_ISSET(STDIN_FILENO, &set))
230 message("select from kbd");
240 /*---------------------------------------------------------------------------*
241 * keyboard character available handler
242 *---------------------------------------------------------------------------*/
248 kchar = wgetch(main_w); /* get char */
257 if((state == ST_IDLE) &&
258 (numberbuffer[0] != '\0'))
260 message("dialing .....");
261 do_dial(&numberbuffer[0]);
276 message("cannot exit while not idle!");
282 case CNTRL_L: /* refresh */
293 mvwaddch(main_w, MW_NUMY, MW_NUX + curx, ' ');
294 numberbuffer[curx] = '\0';
295 wmove(main_w, MW_NUMY, MW_NUX + curx);
312 if(curx > (TELNO_MAX-1))
315 mvwaddch(main_w, MW_NUMY, MW_NUX + curx, kchar);
317 numberbuffer[curx] = kchar;
321 numberbuffer[curx] = '\0';
323 message("press ENTER to dial number .....");
328 /*---------------------------------------------------------------------------*
330 *---------------------------------------------------------------------------*/
342 /*---------------------------------------------------------------------------*
344 *---------------------------------------------------------------------------*/
346 fatal(char *fmt, ...)
352 do_hangup(); /* failsafe */
363 fprintf(stderr, "\nFatal error: ");
364 vfprintf(stderr, fmt, ap);
365 fprintf(stderr, "\n\n");
372 /*---------------------------------------------------------------------------*
374 *---------------------------------------------------------------------------*/
376 message(char *fmt, ...)
387 wmove(main_w, MW_MSGY, MW_MSX);
388 vsnprintf(sbuf, MW_WIDTH-MW_MSX-1, fmt, ap);
389 waddstr(main_w, sbuf);
390 for(i=strlen(sbuf);i < MW_WIDTH-MW_MSX-2; i++)
392 wmove(main_w, MW_NUMY, MW_NUX + curx);
397 fprintf(stderr, "ERROR: ");
398 vfprintf(stderr, fmt, ap);
399 fprintf(stderr, "\n");
405 /*---------------------------------------------------------------------------*
407 *---------------------------------------------------------------------------*/
409 debug(char *fmt, ...)
418 vwprintw(dbg_w, fmt, ap);
424 /*---------------------------------------------------------------------------*
426 *---------------------------------------------------------------------------*/
428 newstate(int newstate)
432 if(newstate < 0 || newstate > ST_MAX)
434 message("newstate %d undefined!", newstate);
440 if(newstate == ST_ACTIVE)
442 char namebuffer[128];
444 sprintf(namebuffer,"%s%d", I4BTELDEVICE, opt_unit);
445 telfd = init_tel(namebuffer);
448 if(newstate == ST_IDLE)
454 wmove(main_w, MW_STATEY, MW_STX);
455 waddstr(main_w, states[newstate]);
457 for(i=strlen(states[newstate]);i < MW_WIDTH-MW_STX-2; i++)
460 wmove(main_w, MW_NUMY, MW_NUX + curx);