diff --git a/audio.c b/audio.c index 47c9900e..b2630106 100644 --- a/audio.c +++ b/audio.c @@ -309,9 +309,8 @@ static void go_to_another_file () if (plist_count(&queue) && go_next) { logit ("Playing file from queue"); - if (!before_queue_fname && curr_playing_fname) { + if (!before_queue_fname && curr_playing_fname) before_queue_fname = xstrdup (curr_playing_fname); - } curr_plist = &queue; curr_playing = plist_next (&queue, -1); @@ -816,34 +815,29 @@ int audio_send_pcm (const char *buf, const size_t size) char *softmixed = NULL; char *equalized = NULL; - if(equalizer_is_active()) + if (equalizer_is_active ()) { - equalized = xmalloc(size); - memcpy(equalized, buf, size); + equalized = xmalloc (size); + memcpy (equalized, buf, size); - equalizer_process_buffer(equalized, size, &driver_sound_params); + equalizer_process_buffer (equalized, size, &driver_sound_params); buf = equalized; } - if(softmixer_is_active()) + if (softmixer_is_active ()) { - if(equalized) + if (equalized) { softmixed = equalized; } else { - softmixed = xmalloc(size); - memcpy(softmixed, buf, size); + softmixed = xmalloc (size); + memcpy (softmixed, buf, size); } - softmixer_process_buffer - ( - softmixed - , size - , &driver_sound_params - ); + softmixer_process_buffer (softmixed, size, &driver_sound_params); buf = softmixed; } @@ -855,11 +849,11 @@ int audio_send_pcm (const char *buf, const size_t size) if (played == 0) fatal ("Audio output error!"); - if(softmixed && !equalized) - free(softmixed); + if (softmixed && !equalized) + free (softmixed); - if(equalized) - free(equalized); + if (equalized) + free (equalized); return played; } @@ -1100,23 +1094,23 @@ char *audio_get_sname () int audio_get_mixer () { - if(current_mixer==2) - return softmixer_get_value(); + if (current_mixer == 2) + return softmixer_get_value (); - return hw.read_mixer (); + return hw.read_mixer (); } void audio_set_mixer (const int val) { - if (val >= 0 && val <= 100) - { - if(current_mixer==2) - softmixer_set_value(val); - else - hw.set_mixer (val); - } - else + if (val < 0 || val > 100) { logit ("Tried to set mixer to volume out of range."); + return; + } + + if (current_mixer == 2) + softmixer_set_value (val); + else + hw.set_mixer (val); } void audio_plist_delete (const char *file) @@ -1151,18 +1145,22 @@ int audio_get_ftime (const char *file) { int i; int time; - time_t mtime = get_mtime (file); + time_t mtime; + + mtime = get_mtime (file); LOCK (plist_mut); - if ((i = plist_find_fname(&playlist, file)) != -1 - && (time = get_item_time(&playlist, i)) != -1) { - if (playlist.items[i].mtime == mtime) { - debug ("Found time for %s", file); - UNLOCK (plist_mut); - return time; - } - else + i = plist_find_fname (&playlist, file); + if (i != -1) { + time = get_item_time (&playlist, i); + if (time != -1) { + if (playlist.items[i].mtime == mtime) { + debug ("Found time for %s", file); + UNLOCK (plist_mut); + return time; + } logit ("mtime for %s has changed", file); + } } UNLOCK (plist_mut); @@ -1249,15 +1247,15 @@ struct file_tags *audio_get_curr_tags () char *audio_get_mixer_channel_name () { - if(current_mixer==2) - return softmixer_name(); + if (current_mixer == 2) + return softmixer_name (); - return hw.get_mixer_channel_name (); + return hw.get_mixer_channel_name (); } void audio_toggle_mixer_channel () { - current_mixer=(current_mixer+1)%3; - if(current_mixer<2) - hw.toggle_mixer_channel (); + current_mixer = (current_mixer + 1) % 3; + if (current_mixer < 2) + hw.toggle_mixer_channel (); } diff --git a/decoder.c b/decoder.c index 9bb9f2f8..74af1956 100644 --- a/decoder.c +++ b/decoder.c @@ -661,28 +661,39 @@ void decoder_init (int debug_info) load_preferences (); } -void decoder_cleanup () +static void cleanup_decoders () { - int i; - decoder_t_preference *pref, *next; + int ix; - for (i = 0; i < plugins_num; i++) { - if (plugins[i].decoder->destroy) - plugins[i].decoder->destroy (); - free (plugins[i].name); + for (ix = 0; ix < plugins_num; ix++) { + if (plugins[ix].decoder->destroy) + plugins[ix].decoder->destroy (); + free (plugins[ix].name); } if (lt_dlexit ()) logit ("lt_exit() failed: %s", lt_dlerror ()); +} + +static void cleanup_preferences () +{ + decoder_t_preference *pref, *next; pref = preferences; for (pref = preferences; pref; pref = next) { next = pref->next; free (pref); } + preferences = NULL; } +void decoder_cleanup () +{ + cleanup_decoders (); + cleanup_preferences (); +} + /* Fill the error structure with an error of a given type and message. * strerror(add_errno) is appended at the end of the message if add_errno != 0. * The old error message is free()ed. diff --git a/decoder_plugins/flac/flac.c b/decoder_plugins/flac/flac.c index 061cbb6a..4f0e167b 100644 --- a/decoder_plugins/flac/flac.c +++ b/decoder_plugins/flac/flac.c @@ -324,13 +324,10 @@ static void *flac_open_internal (const char *file, const int buffered) return data; } - data->ok = 1; - #ifdef LEGACY_FLAC if (!(data->decoder = FLAC__seekable_stream_decoder_new())) { decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__seekable_stream_decoder_new() failed"); - data->ok = 0; return data; } @@ -361,7 +358,6 @@ static void *flac_open_internal (const char *file, const int buffered) != FLAC__SEEKABLE_STREAM_DECODER_OK) { decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__seekable_stream_decoder_init() failed"); - data->ok = 0; return data; } @@ -370,14 +366,12 @@ static void *flac_open_internal (const char *file, const int buffered) decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__seekable_stream_decoder_process_until_end_of_metadata()" " failed."); - data->ok = 0; return data; } #else if (!(data->decoder = FLAC__stream_decoder_new())) { decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__stream_decoder_new() failed"); - data->ok = 0; return data; } @@ -391,7 +385,6 @@ static void *flac_open_internal (const char *file, const int buffered) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__stream_decoder_init() failed"); - data->ok = 0; return data; } @@ -399,10 +392,11 @@ static void *flac_open_internal (const char *file, const int buffered) decoder_error (&data->error, ERROR_FATAL, 0, "FLAC__stream_decoder_process_until_end_of_metadata()" " failed."); - data->ok = 0; return data; } #endif + + data->ok = 1; data->avg_bitrate = (data->bits_per_sample) * data->sample_rate; return data; diff --git a/decoder_plugins/speex/speex.c b/decoder_plugins/speex/speex.c index 65fc6ab0..02d50f1c 100644 --- a/decoder_plugins/speex/speex.c +++ b/decoder_plugins/speex/speex.c @@ -249,7 +249,7 @@ static void *spx_open (const char *file) struct spx_data *data; stream = io_open (file, 1); - if (io_ok(stream)) + if (io_ok (stream)) data = spx_open_internal (stream); else { data = (struct spx_data *)xmalloc (sizeof(struct spx_data)); @@ -437,7 +437,7 @@ static void spx_info (const char *file_name, struct file_tags *tags, struct io_stream *s; s = io_open (file_name, 0); - if (io_ok(s)) { + if (io_ok (s)) { struct spx_data *data = spx_open_internal (s); if (data->ok) { @@ -559,10 +559,9 @@ static int spx_decode (void *prv_data, char *sound_buf, int nbytes, /* First see if there is anything left in the output buffer and * empty it out */ if (data->output_left > 0) { - int to_copy = nbytes / 2; + int to_copy = nbytes / sizeof(int16_t); - to_copy = data->output_left < to_copy - ? data->output_left : to_copy; + to_copy = MIN(data->output_left, to_copy); memcpy (out, data->output + data->output_start, to_copy * sizeof(int16_t)); @@ -571,8 +570,9 @@ static int spx_decode (void *prv_data, char *sound_buf, int nbytes, data->output_start += to_copy; data->output_left -= to_copy; - nbytes -= to_copy * 2; - } else if (ogg_stream_packetout(&data->os, &data->op) == 1) { + nbytes -= to_copy * sizeof(int16_t); + } + else if (ogg_stream_packetout (&data->os, &data->op) == 1) { int16_t *temp_output = data->output; /* Decode some more samples */ diff --git a/files.c b/files.c index 7905296d..2c2ede18 100644 --- a/files.c +++ b/files.c @@ -305,46 +305,39 @@ void resolve_path (char *buf, const int size, const char *file) * If some tags are already present, don't read them. * If present_tags is NULL, allocate new tags. */ struct file_tags *read_file_tags (const char *file, - struct file_tags *present_tags, const int tags_sel) + struct file_tags *tags, const int tags_sel) { - struct file_tags *tags; struct decoder *df; int needed_tags; assert (file != NULL); - if (present_tags) { - tags = present_tags; - needed_tags = ~tags->filled & tags_sel; - } - else { + if (tags == NULL) tags = tags_new (); - needed_tags = tags_sel; - } - if (file_type(file) == F_URL) + if (file_type (file) == F_URL) return tags; - df = get_decoder (file); + needed_tags = ~tags->filled & tags_sel; + if (!needed_tags) { + debug ("No need to read any tags"); + return tags; + } + df = get_decoder (file); if (!df) { logit ("Can't find decoder functions for %s", file); return tags; } - if (needed_tags) { - - /* This makes sure that we don't cause a memory leak */ - assert (!((needed_tags & TAGS_COMMENTS) && + /* This makes sure that we don't cause a memory leak */ + assert (!((needed_tags & TAGS_COMMENTS) && (tags->title || tags->artist || tags->album))); - df->info (file, tags, needed_tags); - tags->filled |= tags_sel; - } - else - debug ("No need to read any tags"); + df->info (file, tags, needed_tags); + tags->filled |= tags_sel; return tags; } diff --git a/interface.c b/interface.c index 11003223..7e045ae1 100644 --- a/interface.c +++ b/interface.c @@ -459,8 +459,9 @@ static char *get_curr_file () static void update_mixer_value () { - int val = get_mixer_value (); + int val; + val = get_mixer_value (); iface_set_mixer_value MAX(val, 0); } diff --git a/keys.c b/keys.c index 95c10f00..eeae473f 100644 --- a/keys.c +++ b/keys.c @@ -38,8 +38,6 @@ #include "log.h" #include "files.h" -#define CTRL_KEY_CODE 0x1F - /* ^c version of c */ #ifndef CTRL # define CTRL(c) ((c) & CTRL_KEY_CODE) diff --git a/keys.h b/keys.h index d761d537..2000e6d2 100644 --- a/keys.h +++ b/keys.h @@ -123,6 +123,7 @@ enum key_context #endif #define META_KEY_FLAG 0x80 +#define CTRL_KEY_CODE 0x1F struct iface_key; enum key_cmd get_key_cmd (const enum key_context context, const struct iface_key *key); diff --git a/tags_cache.c b/tags_cache.c index 5a9c76ca..d6f75801 100644 --- a/tags_cache.c +++ b/tags_cache.c @@ -46,6 +46,12 @@ #include "log.h" #include "audio.h" +/* The name of the version tag file in the cache directory. */ +#define MOC_VERSION_TAG "moc_version_tag" + +/* The maximum length of the version tag (including trailing NULL). */ +#define VERSION_TAG_MAX 64 + /* Number used to create cache version tag to detect incompatibilities * between cache version stored on the disk and MOC/BerkeleyDB environment. * @@ -432,7 +438,7 @@ static void tags_cache_add (struct tags_cache *c, const char *file, static struct file_tags *tags_cache_read_add (struct tags_cache *c, const int client_id, const char *file, int tags_sel) { - struct file_tags *tags; + struct file_tags *tags = NULL; DBT key; DBT serialized_cache_rec; DB_LOCK lock; @@ -470,16 +476,14 @@ static struct file_tags *tags_cache_read_add (struct tags_cache *c, if (ret == 0) { struct cache_record rec; - if (cache_record_deserialize(&rec, serialized_cache_rec.data, - serialized_cache_rec.size, 0)) { - time_t curr_mtime = get_mtime(file); + if (cache_record_deserialize (&rec, serialized_cache_rec.data, + serialized_cache_rec.size, 0)) { + time_t curr_mtime = get_mtime (file); if (rec.mod_time != curr_mtime) { /* outdated tags - remove them and reread */ tags_free (rec.tags); - rec.tags = tags_new (); - rec.mod_time = curr_mtime; debug ("Tags in the cache are outdated"); } @@ -496,11 +500,10 @@ static struct file_tags *tags_cache_read_add (struct tags_cache *c, debug ("Tags in the cache are not what we want."); } } - tags = tags_new (); } - else { + + if (tags == NULL) tags = tags_new (); - } if (tags_sel & TAGS_TIME) { int time; @@ -714,6 +717,7 @@ void tags_cache_add_request (struct tags_cache *c, const char *file, goto end; } + tags_free (rec.tags); debug ("Found outdated or incomplete tags in the cache"); } } @@ -761,12 +765,12 @@ void tags_cache_clear_up_to (struct tags_cache *c, const char *file, UNLOCK (c->mutex); } -void tags_cache_save (struct tags_cache *c ATTR_UNUSED, const char *file_name ATTR_UNUSED) +void tags_cache_save (struct tags_cache *c ATTR_UNUSED, const char *cache_dir ATTR_UNUSED) { //TODO: to remove assert (c != NULL); - assert (file_name != NULL); + assert (cache_dir != NULL); } /* Purge content of a directory. */ @@ -837,7 +841,7 @@ static int purge_directory (const char *dir_path) /* Create a MOC/db version string. * - * @param buf Output buffer (at least 64 chars long) + * @param buf Output buffer (at least VERSION_TAG_MAX chars long) */ static const char *create_version_tag (char *buf) { @@ -846,8 +850,8 @@ static const char *create_version_tag (char *buf) db_version (&db_major, &db_minor, NULL); - snprintf (buf, 64, "%d %d %d", CACHE_DB_FORMAT_VERSION, db_major, - db_minor); + snprintf (buf, VERSION_TAG_MAX, "%d %d %d", + CACHE_DB_FORMAT_VERSION, db_major, db_minor); return buf; } @@ -858,17 +862,17 @@ static const char *create_version_tag (char *buf) static int cache_version_matches (const char *cache_dir) { char *fname = NULL; - char disk_version_tag[65]; + char disk_version_tag[VERSION_TAG_MAX]; ssize_t rres; FILE *f; int compare_result = 0; - fname = (char *)xmalloc (strlen (cache_dir) + sizeof ("/moc_version_tag")); - sprintf (fname, "%s/moc_version_tag", cache_dir); + fname = (char *)xmalloc (strlen (cache_dir) + sizeof (MOC_VERSION_TAG) + 1); + sprintf (fname, "%s/%s", cache_dir, MOC_VERSION_TAG); f = fopen (fname, "r"); if (!f) { - logit ("No moc_version_tag in cache directory"); + logit ("No %s in cache directory", MOC_VERSION_TAG); free (fname); return 0; } @@ -878,7 +882,7 @@ static int cache_version_matches (const char *cache_dir) logit ("On-disk version tag too long"); } else { - char cur_version_tag[64]; + char cur_version_tag[VERSION_TAG_MAX]; disk_version_tag[rres] = '\0'; create_version_tag (cur_version_tag); @@ -893,13 +897,13 @@ static int cache_version_matches (const char *cache_dir) static void write_cache_version (const char *cache_dir) { - char cur_version_tag[64]; + char cur_version_tag[VERSION_TAG_MAX]; char *fname = NULL; FILE *f; size_t rc; - fname = (char *)xmalloc (strlen (cache_dir) + sizeof ("/moc_version_tag")); - sprintf (fname, "%s/moc_version_tag", cache_dir); + fname = (char *)xmalloc (strlen (cache_dir) + sizeof (MOC_VERSION_TAG) + 1); + sprintf (fname, "%s/%s", cache_dir, MOC_VERSION_TAG); f = fopen (fname, "w"); if (!f) { @@ -920,9 +924,9 @@ static void write_cache_version (const char *cache_dir) /* Make sure that the cache directory exists and clear it if necessary. */ -static int prepare_cache_dir (const char *file_name) +static int prepare_cache_dir (const char *cache_dir) { - if (mkdir (file_name, 0700) == 0) + if (mkdir (cache_dir, 0700) == 0) return 1; if (errno != EEXIST) { @@ -931,22 +935,22 @@ static int prepare_cache_dir (const char *file_name) return 0; } - if (!cache_version_matches (file_name)) { + if (!cache_version_matches (cache_dir)) { logit ("Tags cache directory is the wrong version, purging...."); - if (!purge_directory (file_name)) + if (!purge_directory (cache_dir)) return 0; - write_cache_version (file_name); + write_cache_version (cache_dir); } return 1; } -void tags_cache_load (struct tags_cache *c, const char *file_name) +void tags_cache_load (struct tags_cache *c, const char *cache_dir) { int ret; - if (!prepare_cache_dir (file_name)) + if (!prepare_cache_dir (cache_dir)) return; ret = db_env_create (&c->db_env, 0); @@ -955,11 +959,11 @@ void tags_cache_load (struct tags_cache *c, const char *file_name) return; } - ret = c->db_env->open (c->db_env, file_name, + ret = c->db_env->open (c->db_env, cache_dir, DB_CREATE | DB_INIT_MPOOL | DB_THREAD | DB_INIT_LOCK, 0); if (ret) { logit ("Can't open DB environment (%s): %s", - file_name, db_strerror (ret)); + cache_dir, db_strerror (ret)); c->db_env->close (c->db_env, 0); c->db_env = NULL; return; diff --git a/tags_cache.h b/tags_cache.h index 7a888c69..93c200de 100644 --- a/tags_cache.h +++ b/tags_cache.h @@ -53,8 +53,8 @@ void tags_cache_destroy (struct tags_cache *c); void tags_cache_init (struct tags_cache *c, const size_t max_size); void tags_cache_clear_up_to (struct tags_cache *c, const char *file, const int client_id); -void tags_cache_save (struct tags_cache *c, const char *file_name); -void tags_cache_load (struct tags_cache *c, const char *file_name); +void tags_cache_save (struct tags_cache *c, const char *cache_dir); +void tags_cache_load (struct tags_cache *c, const char *cache_dir); #ifdef __cplusplus }