-/*
- * open and prepare the PCM handle for writing
- *
- * Install PCM software and hardware configuration. Exit on errors.
- */
-static int alsa_open(struct writer_node *w)
-{
- snd_pcm_hw_params_t *hwparams;
- snd_pcm_sw_params_t *swparams;
- snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
- stop_threshold;
- unsigned buffer_time = 0;
- int err;
- snd_pcm_info_t *info;
- snd_output_t *log;
- snd_pcm_uframes_t period_size;
- struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
- w->private_data = pad;
-
- snd_pcm_info_alloca(&info);
- if (snd_output_stdio_attach(&log, stderr, 0) < 0)
- return -E_ALSA_LOG;
- err = snd_pcm_open(&pad->handle, conf.device_arg,
- SND_PCM_STREAM_PLAYBACK, 0);
- if (err < 0)
- return -E_PCM_OPEN;
- if ((err = snd_pcm_info(pad->handle, info)) < 0)
- return -E_SND_PCM_INFO;
-
- snd_pcm_hw_params_alloca(&hwparams);
- snd_pcm_sw_params_alloca(&swparams);
- if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
- return -E_BROKEN_CONF;
- if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
- SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
- return -E_ACCESS_TYPE;
- if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
- return -E_SAMPLE_FORMAT;
- if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
- conf.channels_arg) < 0)
- return -E_CHANNEL_COUNT;
- if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
- (unsigned int*) &conf.sample_rate_arg, 0) < 0)
- return -E_SET_RATE;
- err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
- if (err < 0 || !buffer_time)
- return -E_GET_BUFFER_TIME;
- PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
- if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
- &buffer_time, 0) < 0)
- return -E_SET_BUFFER_TIME;
- if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
- return -E_HW_PARAMS;
- snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
- snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
- PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
- period_size);
- if (period_size == buffer_size)
- return -E_BAD_PERIOD;
- snd_pcm_sw_params_current(pad->handle, swparams);
- err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
- if (err < 0 || !xfer_align)
- return -E_GET_XFER;
- snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
- /* round to closest transfer boundary */
- start_threshold = (buffer_size / xfer_align) * xfer_align;
- if (start_threshold < 1)
- start_threshold = 1;
- if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
- start_threshold) < 0)
- return -E_START_THRESHOLD;
- stop_threshold = buffer_size;
- if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
- stop_threshold) < 0)
- return -E_STOP_THRESHOLD;
- if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
- xfer_align) < 0)
- return -E_SET_XFER;
- if (snd_pcm_sw_params(pad->handle, swparams) < 0)
- return -E_SW_PARAMS;
- pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
- * conf.channels_arg / 8;
- return period_size * pad->bytes_per_frame;
+static void parse_config_or_die(int argc, char *argv[])
+{
+ int i, ret;
+ char *config_file;
+ struct play_cmdline_parser_params params = {
+ .override = 0,
+ .initialize = 1,
+ .check_required = 0,
+ .check_ambiguity = 0,
+ .print_errors = 1
+ };
+
+ play_cmdline_parser_ext(argc, argv, &conf, ¶ms);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ version_handle_flag("play", conf.version_given);
+ if (conf.help_given || conf.detailed_help_given)
+ print_help_and_die();
+ if (conf.config_file_given)
+ config_file = para_strdup(conf.config_file_arg);
+ else {
+ char *home = para_homedir();
+ config_file = make_message("%s/.paraslash/play.conf", home);
+ free(home);
+ }
+ ret = file_exists(config_file);
+ if (conf.config_file_given && !ret) {
+ PARA_EMERG_LOG("can not read config file %s\n", config_file);
+ goto err;
+ }
+ if (ret) {
+ params.initialize = 0;
+ params.check_required = 1;
+ play_cmdline_parser_config_file(config_file, &conf, ¶ms);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ }
+ for (i = 0; i < conf.key_map_given; i++) {
+ char *s = strchr(conf.key_map_arg[i] + 1, ':');
+ if (s)
+ continue;
+ PARA_EMERG_LOG("invalid key map arg: %s\n", conf.key_map_arg[i]);
+ goto err;
+ }
+ free(config_file);
+ return;
+err:
+ free(config_file);
+ exit(EXIT_FAILURE);