From 3df0171dc0276721d68675334372c03c2f104741 Mon Sep 17 00:00:00 2001 From: Abramo Bagnara Date: Mon, 5 Feb 2001 15:44:45 +0000 Subject: [PATCH] Encapsulated hwdep. Converted all enums to type safety --- alsactl/alsactl.c | 30 ++++++++------- alsamixer/alsamixer.c | 90 +++++++++++++++++++++++-------------------- amixer/amixer.c | 67 +++++++++++++++++--------------- aplay/aplay.c | 2 +- 4 files changed, 104 insertions(+), 85 deletions(-) diff --git a/alsactl/alsactl.c b/alsactl/alsactl.c index 1e88ecc..1c0953b 100644 --- a/alsactl/alsactl.c +++ b/alsactl/alsactl.c @@ -142,7 +142,8 @@ static int get_control(snd_ctl_t *handle, snd_control_id_t *id, snd_config_t *to snd_config_t *control, *comment, *item, *value; char *s; char buf[256]; - int idx, err; + unsigned int idx; + int err; memset(&info, 0, sizeof(info)); info.id = *id; @@ -316,7 +317,7 @@ static int get_control(snd_ctl_t *handle, snd_control_id_t *id, snd_config_t *to case SND_CONTROL_TYPE_BYTES: case SND_CONTROL_TYPE_IEC958: { - int count = info.type == SND_CONTROL_TYPE_BYTES ? + size_t count = info.type == SND_CONTROL_TYPE_BYTES ? info.values_count : sizeof(snd_aes_iec958_t); char buf[count * 2 + 1]; char *p = buf; @@ -433,7 +434,8 @@ static int get_controls(int cardno, snd_config_t *top) snd_ctl_hw_info_t info; snd_config_t *state, *card, *control; snd_control_list_t list; - int idx, err; + unsigned int idx; + int err; char name[32]; sprintf(name, "hw:%d", cardno); @@ -538,12 +540,13 @@ static int config_iface(snd_config_t *n) { SND_CONTROL_IFACE_TIMER, "timer" }, { SND_CONTROL_IFACE_SEQUENCER, "sequencer" } }; - long idx; + unsigned long i; + unsigned int idx; char *str; - switch (snd_config_type(n)) { + switch (snd_enum_to_int(snd_config_type(n))) { case SND_CONFIG_TYPE_INTEGER: - snd_config_integer_get(n, &idx); - return idx; + snd_config_integer_get(n, &i); + return i; case SND_CONFIG_TYPE_STRING: snd_config_string_get(n, &str); break; @@ -561,7 +564,7 @@ static int config_bool(snd_config_t *n) { char *str; long val; - switch (snd_config_type(n)) { + switch (snd_enum_to_int(snd_config_type(n))) { case SND_CONFIG_TYPE_INTEGER: snd_config_integer_get(n, &val); if (val < 0 || val > 1) @@ -585,8 +588,8 @@ static int config_enumerated(snd_config_t *n, snd_ctl_t *handle, { char *str; long val; - int idx; - switch (snd_config_type(n)) { + unsigned int idx; + switch (snd_enum_to_int(snd_config_type(n))) { case SND_CONFIG_TYPE_INTEGER: snd_config_integer_get(n, &val); return val; @@ -615,7 +618,7 @@ static int set_control(snd_ctl_t *handle, snd_config_t *control) snd_control_t ctl; snd_control_info_t info; snd_config_iterator_t i; - int numid; + unsigned int numid; long iface = -1; long device = -1; long subdevice = -1; @@ -623,7 +626,8 @@ static int set_control(snd_ctl_t *handle, snd_config_t *control) long index = -1; snd_config_t *value = NULL; long val; - int idx, err; + unsigned int idx; + int err; char *set; if (snd_config_type(control) != SND_CONFIG_TYPE_COMPOUND) { error("control is not a compound"); @@ -768,7 +772,7 @@ static int set_control(snd_ctl_t *handle, snd_config_t *control) if (err >= 0) { int c1 = 0; int len = strlen(buf); - int idx = 0; + unsigned int idx = 0; int count = info.type == SND_CONTROL_TYPE_BYTES ? info.values_count : sizeof(snd_aes_iec958_t); if (count * 2 != len) { diff --git a/alsamixer/alsamixer.c b/alsamixer/alsamixer.c index 92c6264..c379e66 100644 --- a/alsamixer/alsamixer.c +++ b/alsamixer/alsamixer.c @@ -207,11 +207,11 @@ static int mixer_elem_mask[] = { }; /* left and right channels for each type */ -static int mixer_elem_chn[][2] = { +static snd_mixer_channel_id_t mixer_elem_chn[][2] = { { SND_MIXER_CHN_FRONT_LEFT, SND_MIXER_CHN_FRONT_RIGHT }, { SND_MIXER_CHN_REAR_LEFT, SND_MIXER_CHN_REAR_RIGHT }, - { SND_MIXER_CHN_FRONT_CENTER, -1 }, - { SND_MIXER_CHN_WOOFER, -1 }, + { SND_MIXER_CHN_FRONT_CENTER, SND_MIXER_CHN_UNKNOWN }, + { SND_MIXER_CHN_WOOFER, SND_MIXER_CHN_UNKNOWN }, }; static snd_mixer_sid_t *mixer_sid = NULL; @@ -469,10 +469,9 @@ mixer_conv(int val, int omin, int omax, int nmin, int nmax) } static int -mixer_calc_volume(snd_mixer_simple_control_t *scontrol, int vol, int chn) +mixer_calc_volume(snd_mixer_simple_control_t *scontrol, int vol, snd_mixer_channel_id_t chn) { int vol1; - vol1 = (vol < 0) ? -vol : vol; if (vol1 > 0) { if (vol1 > 100) @@ -484,7 +483,7 @@ mixer_calc_volume(snd_mixer_simple_control_t *scontrol, int vol, int chn) if (vol < 0) vol1 = -vol1; } - vol1 += scontrol->volume.values[chn]; + vol1 += scontrol->volume.values[snd_enum_to_int(chn)]; return CLAMP(vol1, scontrol->min, scontrol->max); } @@ -495,8 +494,9 @@ mixer_write_cbar (int elem_index) { snd_mixer_simple_control_t scontrol; int vleft, vright, vbalance; - int type, chn_left, chn_right; - int i, err, changed; + int type; + snd_mixer_channel_id_t chn_left, chn_right, chn; + int err, changed; bzero(&scontrol, sizeof(scontrol)); if (mixer_sid == NULL) @@ -507,11 +507,11 @@ mixer_write_cbar (int elem_index) type = mixer_type[elem_index]; chn_left = mixer_elem_chn[type][MIXER_CHN_LEFT]; - if (! (scontrol.channels & (1 << chn_left))) + if (! (scontrol.channels & (1 << snd_enum_to_int(chn_left)))) return; /* ..??.. */ chn_right = mixer_elem_chn[type][MIXER_CHN_RIGHT]; - if (chn_right >= 0 && ! (scontrol.channels & (1 << chn_right))) - chn_right = -1; + if (chn_right != SND_MIXER_CHN_UNKNOWN && ! (scontrol.channels & (1 << snd_enum_to_int(chn_right)))) + chn_right = SND_MIXER_CHN_UNKNOWN; changed = 0; @@ -522,7 +522,7 @@ mixer_write_cbar (int elem_index) mixer_balance_volumes) && (scontrol.caps & SND_MIXER_SCTCAP_VOLUME)) { int mono = - (chn_right < 0 || (scontrol.caps & SND_MIXER_SCTCAP_JOINTLY_VOLUME)); + (chn_right == SND_MIXER_CHN_UNKNOWN || (scontrol.caps & SND_MIXER_SCTCAP_JOINTLY_VOLUME)); if (mono && !mixer_volume_delta[MIXER_CHN_LEFT]) mixer_volume_delta[MIXER_CHN_LEFT] = mixer_volume_delta[MIXER_CHN_RIGHT]; vleft = mixer_calc_volume(&scontrol, mixer_volume_delta[MIXER_CHN_LEFT], chn_left); @@ -535,16 +535,17 @@ mixer_write_cbar (int elem_index) vright = vleft; if (vleft >= 0 && vright >= 0) { if (scontrol.caps & SND_MIXER_SCTCAP_JOINTLY_VOLUME) { - for (i = 0; i < SND_MIXER_CHN_LAST; i++) { - if (scontrol.channels & (1 << i)) - scontrol.volume.values[i] = vleft; + for (chn = 0; chn < SND_MIXER_CHN_LAST; snd_enum_incr(chn)) { + int c = snd_enum_to_int(chn); + if (scontrol.channels & (1 << c)) + scontrol.volume.values[c] = vleft; } } else { if (mixer_balance_volumes) vleft = vright = vbalance; - scontrol.volume.values[chn_left] = vleft; + scontrol.volume.values[snd_enum_to_int(chn_left)] = vleft; if (! mono) - scontrol.volume.values[chn_right] = vright; + scontrol.volume.values[snd_enum_to_int(chn_right)] = vright; } changed = 1; } @@ -560,9 +561,10 @@ mixer_write_cbar (int elem_index) scontrol.mute = scontrol.mute ? 0 : scontrol.channels; else { if (mixer_toggle_mute & MIXER_MASK_LEFT) - scontrol.mute ^= (1 << chn_left); - if (chn_right >= 0 && (mixer_toggle_mute & MIXER_MASK_RIGHT)) - scontrol.mute ^= (1 << chn_right); + scontrol.mute ^= (1 << snd_enum_to_int(chn_left)); + if (chn_right != SND_MIXER_CHN_UNKNOWN && + (mixer_toggle_mute & MIXER_MASK_RIGHT)) + scontrol.mute ^= (1 << snd_enum_to_int(chn_right)); } changed = 1; } @@ -576,9 +578,9 @@ mixer_write_cbar (int elem_index) scontrol.capture = scontrol.capture ? 0 : scontrol.channels; else { if (mixer_toggle_capture & MIXER_MASK_LEFT) - scontrol.capture ^= (1 << chn_left); - if (chn_right >= 0 && (mixer_toggle_capture & MIXER_MASK_RIGHT)) - scontrol.capture ^= (1 << chn_right); + scontrol.capture ^= (1 << snd_enum_to_int(chn_left)); + if (chn_right != SND_MIXER_CHN_UNKNOWN && (mixer_toggle_capture & MIXER_MASK_RIGHT)) + scontrol.capture ^= (1 << snd_enum_to_int(chn_right)); } changed = 1; } @@ -598,8 +600,10 @@ mixer_update_cbar (int elem_index) int err, dc; snd_mixer_simple_control_t scontrol; int vleft, vright; - int type, chn_left, chn_right; + int type; + snd_mixer_channel_id_t chn_left, chn_right; int x, y, i; + int c_left, c_right; /* set new scontrol indices and read info */ @@ -612,21 +616,24 @@ mixer_update_cbar (int elem_index) type = mixer_type[elem_index]; chn_left = mixer_elem_chn[type][MIXER_CHN_LEFT]; - if (! (scontrol.channels & (1 << chn_left))) + if (! (scontrol.channels & (1 << snd_enum_to_int(chn_left)))) return; /* ..??.. */ chn_right = mixer_elem_chn[type][MIXER_CHN_RIGHT]; - if (chn_right >= 0 && ! (scontrol.channels & (1 << chn_right))) - chn_right = -1; + if (chn_right != SND_MIXER_CHN_UNKNOWN && + ! (scontrol.channels & (1 << snd_enum_to_int(chn_right)))) + chn_right = SND_MIXER_CHN_UNKNOWN; + c_left = snd_enum_to_int(chn_left); + c_right = snd_enum_to_int(chn_right); /* first, read values for the numbers to be displayed */ if ((err = snd_mixer_simple_control_read (mixer_handle, &scontrol)) < 0) CHECK_ABORT (ERR_FCN, "snd_mixer_simple_control_read()", err); - vleft = scontrol.volume.values[chn_left]; + vleft = scontrol.volume.values[c_left]; vleft = mixer_conv(vleft, scontrol.min, scontrol.max, 0, 100); - if (chn_right >= 0) { - vright = scontrol.volume.values[chn_right]; + if (chn_right != SND_MIXER_CHN_UNKNOWN) { + vright = scontrol.volume.values[c_right]; vright = mixer_conv(vright, scontrol.min, scontrol.max, 0, 100); } else { vright = vleft; @@ -722,10 +729,10 @@ mixer_update_cbar (int elem_index) mvaddstr (y, x, " "); mixer_dc (DC_CBAR_FRAME); mvaddch (y, x + 2, ACS_ULCORNER); - dc = scontrol.mute & (1 << chn_left) ? DC_CBAR_MUTE : DC_CBAR_NOMUTE; + dc = scontrol.mute & (1 << c_left) ? DC_CBAR_MUTE : DC_CBAR_NOMUTE; mvaddch (y, x + 3, mixer_dc (dc)); - if (chn_right >= 0) - dc = scontrol.mute & (1 << chn_right) ? DC_CBAR_MUTE : DC_CBAR_NOMUTE; + if (chn_right != SND_MIXER_CHN_UNKNOWN) + dc = scontrol.mute & (1 << c_right) ? DC_CBAR_MUTE : DC_CBAR_NOMUTE; mvaddch (y, x + 4, mixer_dc (dc)); mixer_dc (DC_CBAR_FRAME); mvaddch (y, x + 5, ACS_URCORNER); @@ -733,17 +740,18 @@ mixer_update_cbar (int elem_index) /* capture input? */ - if ((scontrol.capture & (1 << chn_left)) || - (chn_right >= 0 && (scontrol.capture & (1 << chn_right)))) + if ((scontrol.capture & (1 << c_left)) || + (chn_right != SND_MIXER_CHN_UNKNOWN && (scontrol.capture & (1 << c_right)))) { mixer_dc (DC_CBAR_CAPTURE); mvaddstr (y, x + 1, "CAPTUR"); - if (scontrol.capture & (1 << chn_left)) { + if (scontrol.capture & (1 << c_left)) { mvaddstr (y + 1, x + 1, "L"); - if (chn_right < 0) + if (chn_right == SND_MIXER_CHN_UNKNOWN) mvaddstr (y + 1, x + 6, "R"); } - if (chn_right >= 0 && (scontrol.capture & (1 << chn_right))) + if (chn_right != SND_MIXER_CHN_UNKNOWN && + (scontrol.capture & (1 << c_right))) mvaddstr (y + 1, x + 6, "R"); } else if (scontrol.caps & SND_MIXER_SCTCAP_CAPTURE) @@ -1398,7 +1406,7 @@ mixer_resize (void) } static void -mixer_callback_rebuild (snd_mixer_t *handle, void *private_data) +mixer_callback_rebuild (snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED) { /* we don't actually need to update the individual channels because * we redraw the whole screen upon every main iteration anyways. @@ -1407,7 +1415,7 @@ mixer_callback_rebuild (snd_mixer_t *handle, void *private_data) } static void -mixer_callback_scontrol (snd_mixer_t *handle, void *private_data, snd_mixer_sid_t *gid) +mixer_callback_scontrol (snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED, snd_mixer_sid_t *gid ATTRIBUTE_UNUSED) { mixer_reinit (); } @@ -1433,7 +1441,7 @@ mixer_add_delta(int delta) static int mixer_iteration (void) { - struct timeval delay = { 0, }; + struct timeval delay = { 0, 0 }; snd_mixer_simple_callbacks_t callbacks = { 0, }; int mixer_fd; fd_set rfds; diff --git a/amixer/amixer.c b/amixer/amixer.c index fa0731e..f9398fb 100644 --- a/amixer/amixer.c +++ b/amixer/amixer.c @@ -416,7 +416,8 @@ static int show_control(const char *space, snd_ctl_t *handle, snd_control_id_t * static int controls(int level) { - int err, idx; + int err; + unsigned int idx; snd_ctl_t *handle; snd_hcontrol_list_t list; @@ -456,7 +457,8 @@ static int controls(int level) static int show_simple_control(void *handle, snd_mixer_sid_t *sid, const char *space, int level) { - int err, chn; + int err; + snd_mixer_channel_id_t chn; snd_mixer_simple_control_t scontrol; bzero(&scontrol, sizeof(scontrol)); @@ -490,8 +492,8 @@ static int show_simple_control(void *handle, snd_mixer_sid_t *sid, const char *s if (scontrol.channels == SND_MIXER_CHN_MASK_MONO) { printf("Mono"); } else { - for (chn = 0; chn <= SND_MIXER_CHN_LAST; chn++) { - if (!(scontrol.channels & (1<id); printf("\n"); } -static void events_value(snd_ctl_t *handle, snd_hcontrol_t *hcontrol) +static void events_value(snd_ctl_t *handle ATTRIBUTE_UNUSED, snd_hcontrol_t *hcontrol) { printf("event value: "); show_control_id(&hcontrol->id); printf("\n"); } -static void events_remove(snd_ctl_t *handle, snd_hcontrol_t *hcontrol) +static void events_remove(snd_ctl_t *handle ATTRIBUTE_UNUSED, snd_hcontrol_t *hcontrol) { printf("event remove: "); show_control_id(&hcontrol->id); printf("\n"); } -static void events_rebuild(snd_ctl_t *handle, void *private_data) +static void events_rebuild(snd_ctl_t *handle ATTRIBUTE_UNUSED, void *private_data) { assert(private_data != (void *)1); printf("event rebuild\n"); } -static void events_add(snd_ctl_t *handle, void *private_data, snd_hcontrol_t *hcontrol) +static void events_add(snd_ctl_t *handle ATTRIBUTE_UNUSED, void *private_data, snd_hcontrol_t *hcontrol) { assert(private_data != (void *)1); printf("event add: "); @@ -931,7 +938,7 @@ static void events_add(snd_ctl_t *handle, void *private_data, snd_hcontrol_t *hc hcontrol->event_remove = events_remove; } -static int events(int argc, char *argv[]) +static int events(int argc ATTRIBUTE_UNUSED, char *argv[] ATTRIBUTE_UNUSED) { snd_ctl_t *handle; snd_hcontrol_t *hcontrol; @@ -975,40 +982,40 @@ static int events(int argc, char *argv[]) snd_ctl_close(handle); } -static void sevents_rebuild(snd_mixer_t *handle, void *private_data) +static void sevents_rebuild(snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED) { printf("event rebuild\n"); } -static void sevents_value(snd_mixer_t *handle, void *private_data, snd_mixer_sid_t *sid) +static void sevents_value(snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED, snd_mixer_sid_t *sid) { char name[simple_name_size]; printf("event value: '%s',%i\n", simple_name(sid->name, name), sid->index); } -static void sevents_change(snd_mixer_t *handle, void *private_data, snd_mixer_sid_t *sid) +static void sevents_change(snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED, snd_mixer_sid_t *sid) { char name[simple_name_size]; printf("event change: '%s',%i\n", simple_name(sid->name, name), sid->index); } -static void sevents_add(snd_mixer_t *handle, void *private_data, snd_mixer_sid_t *sid) +static void sevents_add(snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED, snd_mixer_sid_t *sid) { char name[simple_name_size]; printf("event add: '%s',%i\n", simple_name(sid->name, name), sid->index); } -static void sevents_remove(snd_mixer_t *handle, void *private_data, snd_mixer_sid_t *sid) +static void sevents_remove(snd_mixer_t *handle ATTRIBUTE_UNUSED, void *private_data ATTRIBUTE_UNUSED, snd_mixer_sid_t *sid) { char name[simple_name_size]; printf("event remove: '%s',%i\n", simple_name(sid->name, name), sid->index); } -static int sevents(int argc, char *argv[]) +static int sevents(int argc ATTRIBUTE_UNUSED, char *argv[] ATTRIBUTE_UNUSED) { snd_mixer_t *handle; static snd_mixer_simple_callbacks_t callbacks = { diff --git a/aplay/aplay.c b/aplay/aplay.c index 6cc3e81..ee4ed08 100644 --- a/aplay/aplay.c +++ b/aplay/aplay.c @@ -364,7 +364,7 @@ int main(int argc, char *argv[]) rhwparams.channels = 2; } else { rhwparams.format = snd_pcm_format_value(optarg); - if (rhwparams.format < 0) { + if (rhwparams.format == SND_PCM_FORMAT_UNKNOWN) { error("wrong extended format '%s'", optarg); exit(EXIT_FAILURE); }