File: qrs.c

package info (click to toggle)
glfer 0.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 924 kB
  • sloc: ansic: 7,288; sh: 781; makefile: 57
file content (824 lines) | stat: -rw-r--r-- 19,497 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/* qrs.c
 * 
 * Copyright (C) 2001 Claudio Girardi
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>		/* for toupper() */

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <signal.h>
#include <sys/kd.h>		/* for KIOCSOUND */
#include <fcntl.h>		/* file control definitions */
#include <sys/ioctl.h>
/* #include <asm/io.h>  *//* for inb() and outb() in libc5 only ? */
#include "qrs.h"
#include "glfer.h"
#include "util.h"

#include <string.h>		/* string function definitions */

/* ioperm is in unistd.h  in libc5 */
#include <unistd.h>		/* UNIX standard function definitions */
/* ioperm is in sys/io.h in glibc */
/* conditionals for architectures without sys/io.h added by Joop Stakenborg */
#if HAVE_SYS_IO_H
# include <sys/io.h>
#endif

#include <errno.h>		/* error number definitions */
#include <termios.h>		/* POSIX terminal control definitions */

#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>		/* dmalloc.h should be included last */
#endif


#define PP_BIT0  1		/* parallel port DATA0 line */
#define PP_BIT1  2		/* parallel port DATA1 line */
#define PP_PTT PP_BIT1
#define PP_KEY PP_BIT0
#define PP_BASE0  0x378		/* printer port 0 base address */
#define PP_BASE1  0x278		/* printer port 1 base address */


extern opt_t opt;

static int ksound_fd = -1;	/* console file descriptor */
static int serial_fd = -1;	/* serial port file descriptor */
static int ksound_opened = FALSE;
static int repeat_msg = FALSE; /* repeat message transmission (beacon mode) */


/* function prototypes */
static void send_next_element(int signal);
static void set_alarm_timer(float sec);
void send_next_char(int trig_signal);
static void emit_sound(float freq);
/* morse code characters table */
static struct
{
  char key;
  char *cw;
}
cw_chars[] =
{
  { 'A', ".-"}, 
  { 'B', "-..."},
  { 'C', "-.-."},
  { 'D', "-.."},
  { 'E', "."},
  { 'F', "..-."},
  { 'G', "--."},
  { 'H', "...."},
  { 'I', ".."},
  { 'J', ".---"},
  { 'K', "-.-"},
  { 'L', ".-.."},
  { 'M', "--"},
  { 'N', "-."},
  { 'O', "---"},
  { 'P', ".--."},
  { 'Q', "--.-"},
  { 'R', ".-."},
  { 'S', "..."},
  { 'T', "-"},
  { 'U', "..-"},
  { 'V', "...-"},
  { 'W', ".--"},
  { 'X', "-..-"},
  { 'Y', "-.--"},
  { 'Z', "--.."},
  { '0', "-----"},
  { '1', ".----"},
  { '2', "..---"},
  { '3', "...--"},
  { '4', "....-"},
  { '5', "....."},
  { '6', "-...."},
  { '7', "--..."},
  { '8', "---.."},
  { '9', "----."},
  { '?', "..--.."},
  { '/', "-..-."},
  { '.', ".-.-.-"},
  { '@', ".-.-."},				/* AR */
  { '$', "...-.-"},				/* SK */
  { '%', "-...-.-"},				/* BK */
  { '*', "-.-.-"} ,				/* CT */
/*{ '(', "-.--." },
  { ')', "-.--.-" },
  { '"', ".-..-." },
  { '\'', ".----." },
  { ',', "--..--" },
  { '-', "-....-" },
  { ':', "---..." },
  { ';', "-.-.-." },
  { '=', "-...-" },
  { '_', "..--.-" },*/
  { ' ', " "},
  { 0, ""}			/* end of CW char list */
};


char *s;
static int str_index;		/* string index */
static int str_len;		/* string length */
static int cw_char_index;	/* cw char string index */
static int tab_index;		/* cw lookup table index */
static int cw_char_len;		/* cw char string length */


static void set_alarm_timer(float msec)
{
  struct itimerval element_time;
  long sec, usec;

  sec = floor(msec / 1000.0);
  usec = 1e03 * (msec - 1000.0 * sec);

  element_time.it_interval.tv_sec = 0;
  element_time.it_interval.tv_usec = 0;
  element_time.it_value.tv_sec = sec;
  element_time.it_value.tv_usec = usec;

  setitimer(ITIMER_REAL, &element_time, NULL);
}


static void emit_sound(float freq)
{
  int kparam;

  /* sanity check */
  if (ksound_opened == TRUE) {
    /* parameter for KIOCSOUND ioctl for setting the frequency */
    kparam = (freq != 0.0 ? (1193180 / freq) : 0);

    signal(SIGINT, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    if (ioctl(ksound_fd, KIOCSOUND, kparam) == -1)
      perror("emit_sound(): KIOCSOUND ioctl returned -1");
    signal(SIGINT, SIG_DFL);
    signal(SIGQUIT, SIG_DFL);
  }
}


int open_parport(char *device)
{
#if HAVE_SYS_IO_H
  uid_t real_uid;
  gid_t real_gid;

  /* checking I/O permissions */
  if (ioperm(PP_BASE0, 3, 1)) {
    /* can not access the parallel port */
    fprintf(stderr, "Error: Couldn't get the parallel port at %x\n", PP_BASE0);
    fprintf(stderr, "You need root privileges to run this program.\n");
    return -1;
  } else {
    /* OK, now drop root privileges */
    real_uid = getuid();
    setuid(real_uid);
    real_gid = getgid();
    setgid(real_gid);
    /* make sure PTT and KEY are off */
    ptt_off();
    key_up();
  }
#endif
  return 0;
}


/* open_serial_port() - open serial port
 *
 * returns the file descriptor on success or -1 on error
 */
int open_serial_port(char *device)
{
  struct termios options;
  int status;
  char *dev_full_name;
  char error_string[] = "open_serial_port: unable to open ";
  char *error_message;

  /* allocate enough space for the complete device name */
  dev_full_name = malloc((strlen(device) + 6) * sizeof(char));
  /* it will be in "/dev" */
  strcpy(dev_full_name, "/dev/");
  /* add the device name */
  strcat(dev_full_name, device);

  /* try to open the device */
  serial_fd = open(dev_full_name, O_RDWR | O_NOCTTY | O_NDELAY);

  if (serial_fd == -1) {
    /* could not open the port */
    error_message = (char *) malloc((strlen(error_string) + strlen(dev_full_name) + 1) * sizeof(char));
    strcpy(error_message, error_string);
    strcat(error_message, dev_full_name);
    perror(error_message);
    free(dev_full_name);
    free(error_message);
    return -1;
  } else {
    /* set normal behaviour */
    fcntl(serial_fd, F_SETFL, 0);
  }

  /* get the current options */
  if (tcgetattr(serial_fd, &options) < 0) {
    fprintf(stderr, "open_serial_port: error in tcgetattr\n");
    return -1;
  }

  /* set raw input, 1 second timeout  */
  options.c_iflag = IGNBRK;
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_oflag &= ~OPOST;
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 10;

  /* set the options */
  if (tcsetattr(serial_fd, TCSANOW, &options) < 0) {
    fprintf(stderr, "open_serial_port: error in tcsetattr\n");
    return -1;
  }

  /* get serial port current status */
  ioctl(serial_fd, TIOCMGET, &status);
  /* clear DTR and RTS */
  status &= ~(TIOCM_DTR | TIOCM_RTS);
  ioctl(serial_fd, TIOCMSET, &status);
  /* device name is not used anynmore here */
  free(dev_full_name);

  return (serial_fd);
}


void close_serial_port(void)
{
  if (serial_fd != -1) {	/* port opened */
    if (close(serial_fd) == -1) {
      /* an error occurred */
      perror("close_serial_port");
    } else {
      /* serial port released */
      serial_fd = -1;
    }
  }
}


void ptt_on(void)
{
  int status;
#if HAVE_SYS_IO_H
  unsigned short pp_status;	/* parallel port status */
#endif

  switch (opt.device_type) {
  case DEV_SERIAL:
    /* get serial port current status */
    ioctl(serial_fd, TIOCMGET, &status);
    /* set DTR */
    status |= TIOCM_DTR;
    ioctl(serial_fd, TIOCMSET, &status);
    break;
#if HAVE_SYS_IO_H
  case DEV_PARALLEL:
    pp_status = inb(PP_BASE0);
    pp_status |= PP_BIT1;
    outb((unsigned char) pp_status, PP_BASE0);
    break;
#endif
  default:
    fprintf(stderr, "ptt_on: invalid opt.device_type\n");
    break;
  }
  D(printf("PTT ON\n"));	/* for debug */
}


void ptt_off(void)
{
  int status;
#if HAVE_SYS_IO_H
  unsigned short pp_status;	/* parallel port status */
#endif

  switch (opt.device_type) {
  case DEV_SERIAL:
    /* get serial port current status */
    ioctl(serial_fd, TIOCMGET, &status);
    /* clear DTR */
    status &= ~TIOCM_DTR;
    ioctl(serial_fd, TIOCMSET, &status);
    break;
#if HAVE_SYS_IO_H
  case DEV_PARALLEL:
    pp_status = inb(PP_BASE0);
    pp_status &= ~PP_BIT1;
    outb((unsigned char) pp_status, PP_BASE0);
    break;
#endif
  default:
    fprintf(stderr, "ptt_off: invalid opt.device_type\n");
    break;
  }
  D(printf("PTT OFF\n"));	/* for debug */
}


void key_down(void)
{
  int status;
#if HAVE_SYS_IO_H
  unsigned short pp_status;	/* parallel port status */
#endif

  switch (opt.device_type) {
  case DEV_SERIAL:
    /* get serial port current status */
    ioctl(serial_fd, TIOCMGET, &status);
    /* set RTS */
    status |= TIOCM_RTS;
    ioctl(serial_fd, TIOCMSET, &status);
    break;
#if HAVE_SYS_IO_H
  case DEV_PARALLEL:
    pp_status = inb(PP_BASE0);
    pp_status |= PP_BIT0;
    outb((unsigned char) pp_status, PP_BASE0);
    break;
#endif
  default:
    fprintf(stderr, "key_down: invalid opt.device_type\n");
    break;
  }
  D(printf("KEY DOWN\n"));	/* for debug */
}


void key_up(void)
{
  int status;
#if HAVE_SYS_IO_H
  unsigned short pp_status;	/* parallel port status */
#endif

  switch (opt.device_type) {
  case DEV_SERIAL:
    /* get serial port current status */
    ioctl(serial_fd, TIOCMGET, &status);
    /* clear RTS */
    status &= ~TIOCM_RTS;
    ioctl(serial_fd, TIOCMSET, &status);
    break;
#if HAVE_SYS_IO_H
  case DEV_PARALLEL:
    pp_status = inb(PP_BASE0);
    pp_status &= ~PP_BIT0;
    outb((unsigned char) pp_status, PP_BASE0);
    break;
#endif
  default:
    fprintf(stderr, "key_up: invalid opt.device_type\n");
    break;
  }
  D(printf("KEY UP\n"));	/* for debug */
}


static void dfcw_dot_pause(int trig_signal)
{
  if (cw_char_index < (cw_char_len - 1)) {
    /* not at the end of the char */
    cw_char_index++;
    /* if previous element was a dot insert a gap */
    if (cw_chars[tab_index].cw[cw_char_index - 1] == '.') {
      /* stop sidetone */
      emit_sound(0);
      /* TX off */
      key_up();
      set_alarm_timer(opt.dfcw_gap_time);
      signal(SIGALRM, send_next_element);
    } else {
      /* otherwise make no pause and start sending next char */
      send_next_element(SIGALRM);
    }
  } else if (str_index < (str_len - 1)) {
    /* stop sidetone */
    emit_sound(0);
    /* TX off */
    key_up();
    /* not at the end of the string to be transmitted */
    str_index++;
    D(printf("<next char>, str_index = %i\n", str_index));	/* for debug */
    /* pause between chars */
    set_alarm_timer(3.0 * opt.dot_time);
    signal(SIGALRM, send_next_char);
  } else {
    /* stop sidetone */
    emit_sound(0);
    /* end of transmission */
    key_up();
    /* release PTT (not needed) */
    ptt_off();
  }
}


static void qrss_dot_pause(int trig_signal)
{
  /* switch off emission */
  key_up();
  /* stop sidetone */
  emit_sound(0);

  if (cw_char_index < (cw_char_len - 1)) {
    /* not at the end of the char */
    cw_char_index++;
    /* one dot pause between chars */
    signal(SIGALRM, send_next_element);
    set_alarm_timer(opt.dot_time);
  } else if (str_index < (str_len - 1)) {
    /* not at the end of the string to be transmitted */
    str_index++;
    D(printf("<next char>, str_index = %i\n", str_index));	/* for debug */
    /* pause between chars */
    signal(SIGALRM, send_next_char);
    set_alarm_timer(5.0 * opt.dot_time);
  } else if (str_index < str_len) {
    /* end of the string, end of transmission */
    if (repeat_msg == FALSE) { /* not in beacon mode */
      str_index++; /* signal that last character was sent */
      signal(SIGALRM, qrss_dot_pause);
      set_alarm_timer(opt.ptt_delay);
    } else { /* in beacon mode */
      if (opt.beacon_tx_pause == TRUE) {
	key_down();
	if (opt.sidetone == TRUE)
	  emit_sound(opt.sidetone_freq);
      }
      str_index = 0; /* reset pointer to beginning of string */
      signal(SIGALRM, send_next_char);
      /* start transmissions after  */
      set_alarm_timer(opt.beacon_pause * 1000); /* pause is in seconds */
    }
  } else { /* not in beacon mode */
    ptt_off(); /* transmission ends here */
  }
}


static void send_dot(void)
{
  switch (opt.tx_mode) {
  case QRSS:
    /* make sure the TX is off */
    key_up();
    /* stop sidetone */
    emit_sound(0);
    signal(SIGALRM, qrss_dot_pause);
    set_alarm_timer(opt.dot_time);
    D(putchar('.');
      fflush(stdout));		/* for debug */
    if (opt.sidetone == TRUE)
      emit_sound(opt.sidetone_freq);
    key_down();
    break;
  case DFCW:
    key_down();
    /* "normal" DFCW has DTR=high for dash */
    ptt_on();
    if (opt.sidetone == TRUE)
      emit_sound(opt.dfcw_dot_freq);
    signal(SIGALRM, dfcw_dot_pause);
    set_alarm_timer(opt.dot_time - opt.dfcw_gap_time);
    break;
  default:
    break;
  }
}


static void send_dash(void)
{
  switch (opt.tx_mode) {
  case QRSS:
    /* make sure the TX is off */
    key_up();
    /* stop sidetone */
    emit_sound(0);
    signal(SIGALRM, qrss_dot_pause);
    set_alarm_timer(opt.dash_dot_ratio * opt.dot_time);
    D(putchar('-');
      fflush(stdout);
      )				/* for debug */
      if (opt.sidetone == TRUE)
      emit_sound(opt.sidetone_freq);
    key_down();
    break;
  case DFCW:
    key_down();
    /* "normal" DFCW has DTR=low for dot */
    ptt_off();
    if (opt.sidetone == TRUE)
      emit_sound(opt.dfcw_dash_freq);
    signal(SIGALRM, dfcw_dot_pause);
    set_alarm_timer(opt.dot_time);
    break;
  default:
    break;
  }
}


static void send_space(void)
{
  switch (opt.tx_mode) {
  case QRSS:
    signal(SIGALRM, qrss_dot_pause);
    set_alarm_timer(5.0 * opt.dot_time);
    D(putchar(' ');
      fflush(stdout));		/* for debug */
    break;
  case DFCW:
    signal(SIGALRM, dfcw_dot_pause);
    set_alarm_timer(3.0 * opt.dot_time);
    D(putchar(' ');
      fflush(stdout));		/* for debug */
    break;
  default:
    break;
  }
}


static void send_next_element(int trig_signal)
{
  /* signal type is ignored, only SIGALRM should trigger this function */
  /* send a dot, dash or space */
  if (cw_chars[tab_index].cw[cw_char_index] == '-') {
    send_dash();
  } else if (cw_chars[tab_index].cw[cw_char_index] == '.') {
    send_dot();
  } else {			/* is space */
    send_space();
  }
  //key_down();
}


/*
 * send_char - routine to send a given ASCII character as cw, and echo
 *             to stdout, and optionally wait for the post-character gap
 */
void send_next_char(int trig_signal)
{
  char c;

  c = s[str_index];
  /* ensure uppercase */
  c = toupper(c);

  /* check table to find character to send */
  tab_index = 0;
  while (cw_chars[tab_index].key != 0) {
    if (cw_chars[tab_index].key == c) {
      /* if found, send character */
      /* echo the character */
      D(putchar(c);
	putchar(':');
	putchar(' ');
	fflush(stdout));	/* for debug */
      /* sound the elements of the cw equivalent */
      cw_char_len = strlen(cw_chars[tab_index].cw);
      cw_char_index = 0;
      send_next_element(SIGALRM);
      return;
    } else {
      tab_index++;
    }
  }
  /* not found - print an error message */
  fprintf(stderr, "send_next_char: char not recognized\n");
}


int cw_char_allowed(char c)
{
  /* ensure uppercase */
  c = toupper(c);

  /* scan table to find character */
  tab_index = 0;
  while (cw_chars[tab_index].key != 0) {
    if (cw_chars[tab_index].key == c) {
      /* found, character is allowed */
      return TRUE;
    } else {
      tab_index++;
    }
  }
  /* not found - character is not allowed */
  return FALSE;
}


int get_qrss_char_index(void)
{
  return str_index;
}

/*
 * send_string - routine to send a given ASCII string as cw, and echo
 *               to stdout; can also send multi-char items
 */
void send_string(char *str)
{
  s = str;
  /* simply send every character in the string */
  str_index = 0;
  str_len = strlen(s);

  repeat_msg = opt.beacon_mode;

  switch (opt.tx_mode) {
  case QRSS:
    /* close PTT */
    ptt_on();
    signal(SIGALRM, send_next_char);
    /* start transmissions after ptt_delay */
    set_alarm_timer(opt.ptt_delay);
    break;
  case DFCW:
    /* close KEY */
    key_down();
    signal(SIGALRM, send_next_char);
    /* start transmissions after ptt_delay */
    set_alarm_timer(opt.ptt_delay);
    break;
  default:
    break;
  }
}


void stop_tx(void)
{
  /* avoid restart of message transmission */
  repeat_msg = FALSE;
  /* stop sidetone */
  emit_sound(0);
  /* hack, pretend we are at the end of the string */
  cw_char_index = cw_char_len;
  str_index = str_len - 1;

  switch (opt.tx_mode) {
  case QRSS:
    /* switch off TX */
    key_up();
    signal(SIGALRM, qrss_dot_pause);
    break;
  case DFCW:
    signal(SIGALRM, dfcw_dot_pause);
    break;
  default:
    break;
  }
  /* schedule next event to be (almost) now */
  set_alarm_timer(1e-03);
}


float string_duration(char *str)
{
  int i, j, k;
  char c;
  float overall_time = 0.0;

  /* add the PTT delay at start and end of transmissions */
  overall_time += 2.0 * opt.ptt_delay;

  for (i = 0; i < strlen(str); i++) {
    /* ensure uppercase */
    c = toupper(str[i]);
    /* scan table to find character */
    for (j = 0; cw_chars[j].key != 0; j++)
      if (cw_chars[j].key == c) {
	for (k = 0; k < strlen(cw_chars[j].cw); k++) {
	  if (cw_chars[j].cw[k] == '-') {
	    /* dash duration; different between QRSS and DFCW */
	    switch (opt.tx_mode) {
	    case QRSS:
	      overall_time += opt.dash_dot_ratio * opt.dot_time;
	      break;
	    case DFCW:
	      overall_time += opt.dot_time;
	      break;
	    default:
	      break;
	    }
	  } else if (cw_chars[j].cw[k] == '.') {
	    /* dot duration */
	    overall_time += opt.dot_time;
	  } else {
	    /* space duration; different between QRSS and DFCW */
	    switch (opt.tx_mode) {
	    case QRSS:
	      overall_time += 5.0 * opt.dot_time;
	      break;
	    case DFCW:
	      overall_time += 3.0 * opt.dot_time;
	      break;
	    default:
	      break;
	    }
	  }
	  if (k < (strlen(cw_chars[j].cw) - 1)) {
	    /* not at end of char */
	    /* space between elements */
	    switch (opt.tx_mode) {
	    case QRSS:
	      overall_time += opt.dot_time;
	      break;
	    case DFCW:
	      /* no space between elements */
	      break;
	    default:
	      break;
	    }
	  } else if (i < (strlen(str) - 1)) {
	    /* at end of char but not at end of string */
	    /* space between chars */
	    switch (opt.tx_mode) {
	    case QRSS:
	      overall_time += 5.0 * opt.dot_time;
	      break;
	    case DFCW:
	      overall_time += 3.0 * opt.dot_time;
	      break;
	    default:
	      break;
	    }
	  }			/* else string is finished */
	}
      }
  }
  return overall_time;
}


int open_ksound(void)
{
  /* To be used as the ksound_fd in ioctl(). */
  if ((ksound_fd = open("/dev/console", O_NOCTTY)) == -1) {
    perror("open_ksound: unable to open /dev/console");
    return (ksound_opened = FALSE);
  } else
    return (ksound_opened = TRUE);
}


void close_ksound(void)
{
  if (ksound_opened == TRUE)
    close(ksound_fd);
}