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
|
/*
* UnixCW - Unix CW (Morse code) training program
* Copyright (C) 2001, 2002 Simon Baldwin (simonb@caldera.com)
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Main.cpp - Main module for Qt-based CW tutor program.
*
*/
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(HAVE_STRING_H)
# include <string.h>
#endif /* HAVE_STRING_H */
#if defined(HAVE_STRINGS_H)
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#if defined(HAVE_GETOPT_H)
# include <getopt.h> /* Linux */
#endif /* HAVE_GETOPT_H */
#if !defined(MAXPATHLEN)
# define MAXPATHLEN _POSIX_PATH_MAX /* OpenServer */
#endif /* not MAXPATHLEN */
/* CW library functions header file. */
#include <cwlib.h>
/* Required C++ headers for classes instantiated from here. */
#include <qapplication.h>
#include "XcwcpApplication.h"
/* Strings displayed for the -v option. */
const char *version = "Xcwcp version 2.2";
const char *copyright = \
"Xcwcp version 2.2 Copyright (C) 2001, 2002 Simon Baldwin\n\n"
"This program comes with ABSOLUTELY NO WARRANTY; for details\n"
"please see the file 'COPYING' supplied with the source code.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions; again, see 'COPYING' for details.\n"
"This program is released under the GNU General Public License.";
/* Argument handling macros. */
#define MAXARGS 128 /* Args to exec */
#define MAXOPTSTR 1024 /* Longest _OPTIONS env var */
#define ARGS_WHITESPACE " \t" /* Argument separators */
#define ENV_OPTIONS "XCWCP_OPTIONS" /* Env string holding options */
/* Base name of the program, from argv[0]. */
static char *argv0 = NULL;
/*
* print_usage()
*
* Print out a brief message directing the user to the help function.
*/
static void
print_usage ()
{
fprintf (stderr,
#if defined(HAVE_GETOPT_LONG)
"Try '%s --help' for more information.\n",
#else /* not HAVE_GETOPT_LONG */
"Try '%s -h' for more information.\n",
#endif /* not HAVE_GETOPT_LONG */
argv0);
exit (1);
}
/*
* print_help()
*
* Print out a brief page of help information.
*/
static void
print_help ()
{
int min_speed, max_speed; /* Cw library speed limits */
int min_frequency, max_frequency; /* Cw library frequncy limits */
int min_volume, max_volume; /* Cw library volume limits */
int min_gap, max_gap; /* Cw library gap limits */
assert (argv0 != NULL);
/* Read the cw library limits on its operating parameters. */
cw_get_speed_limits (&min_speed, &max_speed);
cw_get_frequency_limits (&min_frequency, &max_frequency);
cw_get_volume_limits (&min_volume, &max_volume);
cw_get_gap_limits (&min_gap, &max_gap);
printf (
#if defined(HAVE_GETOPT_LONG)
"Usage: %s [options...]\n\n\
-s, --sound=SOURCE generate sound on SOURCE [default 'soundcard']\n\
one of 's[oundcard]', 'c[onsole]', or 'b[oth]'\n\
-x, --sdevice=SDEVICE use SDEVICE for soundcard [default %s]\n\
-y, --mdevice=MDEVICE use MDEVICE for sound mixer [default %s]\n\
-d, --cdevice=CDEVICE use CDEVICE for sound ioctl [default %s]\n\
-w, --wpm=WPM set initial words per minute [default %d]\n\
valid WPM values are between %d and %d\n\
-t, --hz,--tone=HZ set initial tone to HZ [default %d]\n\
valid HZ values are between %d and %d\n\
-v, --volume=PERCENT set initial volume to PERCENT [default %d]\n\
valid PERCENT values are between %d and %d\n\
-g, --gap=GAP set extra gap between letters [default %d]\n\
valid GAP values are between %d and %d\n\
-h, --help print this message\n\
-v, --version output version information and exit\n\n",
#else /* not HAVE_GETOPT_LONG */
"Usage: %s [options...]\n\n\
-s SOURCE generate sound on SOURCE [default 'console']\n\
one of 'c[onsole]', 's[oundcard]', or 'b[oth]'\n\
-x SDEVICE use SDEVICE for soundcard [default %s]\n\
-y MDEVICE use MDEVICE for sound mixer [default %s]\n\
-d CDEVICE use CDEVICE for sound ioctl [default %s]\n\
-w WPM set initial words per minute [default %d]\n\
valid WPM values are between %d and %d\n\
-t HZ set initial tone to HZ [default %d]\n\
valid HZ values are between %d and %d\n\
-v PERCENT set initial volume to PERCENT [default %d]\n\
valid PERCENT values are between %d and %d\n\
-g GAP set extra gap between letters [default %d]\n\
valid GAP values are between %d and %d\n\
-h print this message\n\
-v output version information and exit\n\n",
#endif /* not HAVE_GETOPT_LONG */
argv0,
cw_get_soundcard_file (), cw_get_soundmixer_file (),
cw_get_console_file (),
cw_get_send_speed (), min_speed, max_speed,
cw_get_frequency (), min_frequency, max_frequency,
cw_get_volume (), min_volume, max_volume,
cw_get_gap (), min_gap, max_gap);
}
/*
* parse_cmdline()
*
* Parse the command line options for initial values for the various
* global and flag definitions.
*/
static void
parse_cmdline (int argc, char **argv)
{
int c; /* Option character */
int csound = FALSE, ssound = TRUE; /* Sound source options */
char *cdevice = NULL, *sdevice = NULL,
*mdevice = NULL; /* Sound generation options */
int intarg; /* General integer argument */
int argind; /* Loop index */
char env_options[ MAXOPTSTR ]; /* Env options string */
char *sptr; /* String pointer */
char *local_argv[ MAXARGS ]; /* Local argv array */
int local_argc; /* Local argc */
#if defined(HAVE_GETOPT_LONG)
int option_index; /* Option index */
static const struct option long_options[] = { /* Options table */
{ "sound", 1, 0, 's' },
{ "cdevice", 1, 0, 'd' },
{ "sdevice", 1, 0, 'x' },
{ "mdevice", 1, 0, 'y' }, { "tone", 1, 0, 't' },
{ "volume", 1, 0, 'v' }, { "hz", 1, 0, 't' },
{ "wpm", 1, 0, 'w' },
{ "gap", 1, 0, 'g' },
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }};
#endif /* HAVE_GETOPT_LONG */
/* Set argv0 to be the basename part of the program name. */
argv0 = argv[0] + strlen (argv[0]);
while (*argv0 != '/' && argv0 > argv[0])
argv0--;
if (*argv0 == '/')
argv0++;
/*
* Build a new view of argc and argv by first prepending
* the strings from ..._OPTIONS, if defined, then putting the
* command line args on (so they take precedence).
*/
local_argc = 0;
local_argv[ local_argc++ ] = argv[0];
if (getenv (ENV_OPTIONS) != NULL)
{
strcpy (env_options, getenv (ENV_OPTIONS));
sptr = env_options;
while (local_argc < MAXARGS - 1)
{
while (strchr (ARGS_WHITESPACE, *sptr) != NULL
&& *sptr != '\0')
sptr++;
if ( *sptr == '\0' )
break;
else
{
local_argv[ local_argc++ ] = sptr;
while (strchr (ARGS_WHITESPACE, *sptr)
== NULL && *sptr != '\0')
sptr++;
if (strchr (ARGS_WHITESPACE, *sptr)
!= NULL && *sptr != '\0')
{
*sptr = '\0';
sptr++;
}
}
}
}
for (argind = 1; argind < argc; argind++)
{
local_argv[ local_argc++ ] = argv[ argind ];
}
/* Process every option. */
while (TRUE)
{
#if defined(HAVE_GETOPT_LONG)
c = getopt_long (local_argc, local_argv, "s:d:x:y:t:v:w:g:hv",
long_options, &option_index);
#else /* not HAVE_GETOPT_LONG */
c = getopt (local_argc, local_argv, "s:d:x:y:t:v:w:g:hv");
#endif /* not HAVE_GETOPT_LONG */
if (c == -1)
break;
switch (c)
{
case 's':
if (!strcmp (optarg, "console")
|| !strcmp (optarg, "c"))
{
csound = TRUE; ssound = FALSE;
}
else if (!strcmp (optarg, "soundcard")
|| !strcmp (optarg, "s"))
{
csound = FALSE; ssound = TRUE;
}
else if (!strcmp (optarg, "both")
|| !strcmp (optarg, "b"))
{
csound = TRUE; ssound = TRUE;
}
else
{
fprintf (stderr,
"%s: invalid sound source\n",
argv0);
exit (1);
}
break;
case 'd':
cdevice = optarg;
break;
case 'x':
sdevice = optarg;
break;
case 'y':
mdevice = optarg;
break;
case 't':
if (sscanf (optarg, "%d", &intarg) != 1
|| cw_set_frequency (intarg) != 0)
{
fprintf (stderr,
"%s: invalid tone value\n", argv0);
exit (1);
}
break;
case 'v':
if (sscanf (optarg, "%d", &intarg) != 1
|| cw_set_volume (intarg) != 0 )
{
fprintf (stderr,
"%s: invalid volume value\n", argv0);
exit (1);
}
break;
case 'w':
if (sscanf (optarg, "%d", &intarg) != 1
|| cw_set_send_speed (intarg) != 0)
{
fprintf (stderr,
"%s: invalid wpm value\n", argv0);
exit (1);
}
break;
case 'g':
if (sscanf (optarg, "%d", &intarg) != 1
|| cw_set_gap (intarg) != 0)
{
fprintf (stderr,
"%s: invalid gap value\n", argv0);
exit (1);
}
break;
case 'h':
print_help ();
exit (0);
case 'V':
printf ("%s, ", version);
printf ("%s\n", copyright);
exit (0);
case '?':
print_usage ();
default:
fprintf (stderr,
"%s: getopts returned %c\n",
argv0, c);
exit (1);
}
}
if (optind != local_argc)
print_usage ();
/* Deal with odd argument combinations. */
if (!csound && cdevice != NULL)
{
fprintf (stderr, "%s: no console sound: -d invalid\n", argv0);
print_usage ();
}
if (!ssound && sdevice != NULL)
{
fprintf (stderr, "%s: no soundcard sound: -x invalid\n", argv0);
print_usage ();
}
if (!ssound && mdevice != NULL)
{
fprintf (stderr, "%s: no soundcard sound: -y invalid\n", argv0);
print_usage ();
}
/* First set up soundcard sound if required. */
if (ssound)
{
if (sdevice != NULL)
cw_set_soundcard_file (sdevice);
if (cw_soundcard_possible () != 0)
{
fprintf (stderr,
"%s: cannot set up soundcard sound\n", argv0);
perror (cw_get_soundcard_file ());
exit (1);
}
if (mdevice != NULL)
cw_set_soundmixer_file (mdevice);
}
cw_set_soundcard_sound (ssound);
/* Now set up console sound, again if required. */
if (csound)
{
if (cdevice != NULL)
cw_set_console_file (cdevice);
if (cw_console_possible () != 0)
{
fprintf (stderr,
"%s: cannot set up console sound\n", argv0);
perror (cw_get_console_file ());
exit (1);
}
}
cw_set_console_sound (csound);
}
/*
* main
*
* Parse the command line, initialize a few things, then instantiate the
* XcwcpApplication and wait.
*/
int
main (int argc, char **argv)
{
/* Parse the command line parameters and arguments. */
parse_cmdline (argc, argv);
/* Create the application, and display its windows. */
QApplication application (argc, argv);
XcwcpApplication *xcwcp = new XcwcpApplication ();
xcwcp->setCaption ("Xcwcp");
xcwcp->show ();
application.connect (&application, SIGNAL (lastWindowClosed ()),
&application, SLOT (quit ()));
/* Enter the application event loop. */
return application.exec ();
}
|