From 29030b54c98b0aee65fbc10ebf7ba36bed98c02c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 10 Aug 2013 18:02:44 +0200 Subject: util/Error: new error passing library Replaces GLib's GError. --- src/mixer/AlsaMixerPlugin.cxx | 86 ++++++++++++++++++--------------------- src/mixer/OssMixerPlugin.cxx | 72 +++++++++++++------------------- src/mixer/PulseMixerPlugin.cxx | 25 +++++------- src/mixer/RoarMixerPlugin.cxx | 6 +-- src/mixer/SoftwareMixerPlugin.cxx | 17 +++++--- src/mixer/WinmmMixerPlugin.cxx | 20 ++++----- 6 files changed, 102 insertions(+), 124 deletions(-) (limited to 'src/mixer') diff --git a/src/mixer/AlsaMixerPlugin.cxx b/src/mixer/AlsaMixerPlugin.cxx index 587ec699e..1ea26d0c1 100644 --- a/src/mixer/AlsaMixerPlugin.cxx +++ b/src/mixer/AlsaMixerPlugin.cxx @@ -25,6 +25,8 @@ #include "event/MultiSocketMonitor.hxx" #include "event/Loop.hxx" #include "util/ReusableArray.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include @@ -68,23 +70,15 @@ public: AlsaMixer():Mixer(alsa_mixer_plugin) {} void Configure(const config_param ¶m); - bool Setup(GError **error_r); - bool Open(GError **error_r); + bool Setup(Error &error); + bool Open(Error &error); void Close(); - int GetVolume(GError **error_r); - bool SetVolume(unsigned volume, GError **error_r); + int GetVolume(Error &error); + bool SetVolume(unsigned volume, Error &error); }; -/** - * The quark used for GError.domain. - */ -gcc_const -static inline GQuark -alsa_mixer_quark(void) -{ - return g_quark_from_static_string("alsa_mixer"); -} +static constexpr Domain alsa_mixer_domain("alsa_mixer"); int AlsaMixerMonitor::PrepareSockets() @@ -158,7 +152,7 @@ AlsaMixer::Configure(const config_param ¶m) static Mixer * alsa_mixer_init(gcc_unused void *ao, const config_param ¶m, - gcc_unused GError **error_r) + gcc_unused Error &error) { AlsaMixer *am = new AlsaMixer(); am->Configure(param); @@ -194,35 +188,35 @@ alsa_mixer_lookup_elem(snd_mixer_t *handle, const char *name, unsigned idx) } inline bool -AlsaMixer::Setup(GError **error_r) +AlsaMixer::Setup(Error &error) { int err; if ((err = snd_mixer_attach(handle, device)) < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "failed to attach to %s: %s", - device, snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "failed to attach to %s: %s", + device, snd_strerror(err)); return false; } if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "snd_mixer_selem_register() failed: %s", - snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "snd_mixer_selem_register() failed: %s", + snd_strerror(err)); return false; } if ((err = snd_mixer_load(handle)) < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "snd_mixer_load() failed: %s\n", - snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "snd_mixer_load() failed: %s\n", + snd_strerror(err)); return false; } elem = alsa_mixer_lookup_elem(handle, control, index); if (elem == NULL) { - g_set_error(error_r, alsa_mixer_quark(), 0, + error.Format(alsa_mixer_domain, 0, "no such mixer control: %s", control); return false; } @@ -238,7 +232,7 @@ AlsaMixer::Setup(GError **error_r) } inline bool -AlsaMixer::Open(GError **error_r) +AlsaMixer::Open(Error &error) { int err; @@ -246,12 +240,12 @@ AlsaMixer::Open(GError **error_r) err = snd_mixer_open(&handle, 0); if (err < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "snd_mixer_open() failed: %s", snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "snd_mixer_open() failed: %s", snd_strerror(err)); return false; } - if (!Setup(error_r)) { + if (!Setup(error)) { snd_mixer_close(handle); return false; } @@ -260,11 +254,11 @@ AlsaMixer::Open(GError **error_r) } static bool -alsa_mixer_open(Mixer *data, GError **error_r) +alsa_mixer_open(Mixer *data, Error &error) { AlsaMixer *am = (AlsaMixer *)data; - return am->Open(error_r); + return am->Open(error); } inline void @@ -286,7 +280,7 @@ alsa_mixer_close(Mixer *data) } inline int -AlsaMixer::GetVolume(GError **error_r) +AlsaMixer::GetVolume(Error &error) { int err; int ret; @@ -296,9 +290,9 @@ AlsaMixer::GetVolume(GError **error_r) err = snd_mixer_handle_events(handle); if (err < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "snd_mixer_handle_events() failed: %s", - snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "snd_mixer_handle_events() failed: %s", + snd_strerror(err)); return false; } @@ -306,9 +300,9 @@ AlsaMixer::GetVolume(GError **error_r) SND_MIXER_SCHN_FRONT_LEFT, &level); if (err < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "failed to read ALSA volume: %s", - snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "failed to read ALSA volume: %s", + snd_strerror(err)); return false; } @@ -325,14 +319,14 @@ AlsaMixer::GetVolume(GError **error_r) } static int -alsa_mixer_get_volume(Mixer *mixer, GError **error_r) +alsa_mixer_get_volume(Mixer *mixer, Error &error) { AlsaMixer *am = (AlsaMixer *)mixer; - return am->GetVolume(error_r); + return am->GetVolume(error); } inline bool -AlsaMixer::SetVolume(unsigned volume, GError **error_r) +AlsaMixer::SetVolume(unsigned volume, Error &error) { float vol; long level; @@ -351,9 +345,9 @@ AlsaMixer::SetVolume(unsigned volume, GError **error_r) err = snd_mixer_selem_set_playback_volume_all(elem, level); if (err < 0) { - g_set_error(error_r, alsa_mixer_quark(), err, - "failed to set ALSA volume: %s", - snd_strerror(err)); + error.Format(alsa_mixer_domain, err, + "failed to set ALSA volume: %s", + snd_strerror(err)); return false; } @@ -361,10 +355,10 @@ AlsaMixer::SetVolume(unsigned volume, GError **error_r) } static bool -alsa_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) +alsa_mixer_set_volume(Mixer *mixer, unsigned volume, Error &error) { AlsaMixer *am = (AlsaMixer *)mixer; - return am->SetVolume(volume, error_r); + return am->SetVolume(volume, error); } const struct mixer_plugin alsa_mixer_plugin = { diff --git a/src/mixer/OssMixerPlugin.cxx b/src/mixer/OssMixerPlugin.cxx index 231f38432..84cd223e6 100644 --- a/src/mixer/OssMixerPlugin.cxx +++ b/src/mixer/OssMixerPlugin.cxx @@ -21,6 +21,8 @@ #include "MixerInternal.hxx" #include "OutputAPI.hxx" #include "system/fd_util.h" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include @@ -51,22 +53,15 @@ class OssMixer : public Mixer { public: OssMixer():Mixer(oss_mixer_plugin) {} - bool Configure(const config_param ¶m, GError **error_r); - bool Open(GError **error_r); + bool Configure(const config_param ¶m, Error &error); + bool Open(Error &error); void Close(); - int GetVolume(GError **error_r); - bool SetVolume(unsigned volume, GError **error_r); + int GetVolume(Error &error); + bool SetVolume(unsigned volume, Error &error); }; -/** - * The quark used for GError.domain. - */ -static inline GQuark -oss_mixer_quark(void) -{ - return g_quark_from_static_string("oss_mixer"); -} +static constexpr Domain oss_mixer_domain("oss_mixer"); static int oss_find_mixer(const char *name) @@ -84,17 +79,16 @@ oss_find_mixer(const char *name) } inline bool -OssMixer::Configure(const config_param ¶m, GError **error_r) +OssMixer::Configure(const config_param ¶m, Error &error) { - device = param.GetBlockValue("mixer_device", - VOLUME_MIXER_OSS_DEFAULT); + device = param.GetBlockValue("mixer_device", VOLUME_MIXER_OSS_DEFAULT); control = param.GetBlockValue("mixer_control"); if (control != NULL) { volume_control = oss_find_mixer(control); if (volume_control < 0) { - g_set_error(error_r, oss_mixer_quark(), 0, - "no such mixer control: %s", control); + error.Format(oss_mixer_domain, 0, + "no such mixer control: %s", control); return false; } } else @@ -105,11 +99,11 @@ OssMixer::Configure(const config_param ¶m, GError **error_r) static Mixer * oss_mixer_init(gcc_unused void *ao, const config_param ¶m, - GError **error_r) + Error &error) { OssMixer *om = new OssMixer(); - if (!om->Configure(param, error_r)) { + if (!om->Configure(param, error)) { delete om; return nullptr; } @@ -141,13 +135,11 @@ oss_mixer_close(Mixer *data) } inline bool -OssMixer::Open(GError **error_r) +OssMixer::Open(Error &error) { device_fd = open_cloexec(device, O_RDONLY, 0); if (device_fd < 0) { - g_set_error(error_r, oss_mixer_quark(), errno, - "failed to open %s: %s", - device, g_strerror(errno)); + error.FormatErrno("failed to open %s", device); return false; } @@ -155,17 +147,15 @@ OssMixer::Open(GError **error_r) int devmask = 0; if (ioctl(device_fd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { - g_set_error(error_r, oss_mixer_quark(), errno, - "READ_DEVMASK failed: %s", - g_strerror(errno)); + error.SetErrno("READ_DEVMASK failed"); Close(); return false; } if (((1 << volume_control) & devmask) == 0) { - g_set_error(error_r, oss_mixer_quark(), 0, - "mixer control \"%s\" not usable", - control); + error.Format(oss_mixer_domain, 0, + "mixer control \"%s\" not usable", + control); Close(); return false; } @@ -175,15 +165,15 @@ OssMixer::Open(GError **error_r) } static bool -oss_mixer_open(Mixer *data, GError **error_r) +oss_mixer_open(Mixer *data, Error &error) { OssMixer *om = (OssMixer *) data; - return om->Open(error_r); + return om->Open(error); } inline int -OssMixer::GetVolume(GError **error_r) +OssMixer::GetVolume(Error &error) { int left, right, level; int ret; @@ -192,9 +182,7 @@ OssMixer::GetVolume(GError **error_r) ret = ioctl(device_fd, MIXER_READ(volume_control), &level); if (ret < 0) { - g_set_error(error_r, oss_mixer_quark(), errno, - "failed to read OSS volume: %s", - g_strerror(errno)); + error.SetErrno("failed to read OSS volume"); return false; } @@ -210,14 +198,14 @@ OssMixer::GetVolume(GError **error_r) } static int -oss_mixer_get_volume(Mixer *mixer, GError **error_r) +oss_mixer_get_volume(Mixer *mixer, Error &error) { OssMixer *om = (OssMixer *)mixer; - return om->GetVolume(error_r); + return om->GetVolume(error); } inline bool -OssMixer::SetVolume(unsigned volume, GError **error_r) +OssMixer::SetVolume(unsigned volume, Error &error) { int level; int ret; @@ -229,9 +217,7 @@ OssMixer::SetVolume(unsigned volume, GError **error_r) ret = ioctl(device_fd, MIXER_WRITE(volume_control), &level); if (ret < 0) { - g_set_error(error_r, oss_mixer_quark(), errno, - "failed to set OSS volume: %s", - g_strerror(errno)); + error.SetErrno("failed to set OSS volume"); return false; } @@ -239,10 +225,10 @@ OssMixer::SetVolume(unsigned volume, GError **error_r) } static bool -oss_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) +oss_mixer_set_volume(Mixer *mixer, unsigned volume, Error &error) { OssMixer *om = (OssMixer *)mixer; - return om->SetVolume(volume, error_r); + return om->SetVolume(volume, error); } const struct mixer_plugin oss_mixer_plugin = { diff --git a/src/mixer/PulseMixerPlugin.cxx b/src/mixer/PulseMixerPlugin.cxx index 1c99b2469..069f4e36f 100644 --- a/src/mixer/PulseMixerPlugin.cxx +++ b/src/mixer/PulseMixerPlugin.cxx @@ -23,6 +23,8 @@ #include "output/PulseOutputPlugin.hxx" #include "conf.h" #include "GlobalEvents.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include @@ -52,14 +54,7 @@ struct PulseMixer final : public Mixer { } }; -/** - * The quark used for GError.domain. - */ -static inline GQuark -pulse_mixer_quark(void) -{ - return g_quark_from_static_string("pulse_mixer"); -} +static constexpr Domain pulse_mixer_domain("pulse_mixer"); static void pulse_mixer_offline(PulseMixer *pm) @@ -154,13 +149,13 @@ pulse_mixer_on_change(PulseMixer *pm, static Mixer * pulse_mixer_init(void *ao, gcc_unused const config_param ¶m, - GError **error_r) + Error &error) { PulseOutput *po = (PulseOutput *)ao; if (ao == NULL) { - g_set_error(error_r, pulse_mixer_quark(), 0, - "The pulse mixer cannot work without the audio output"); + error.Set(pulse_mixer_domain, + "The pulse mixer cannot work without the audio output"); return nullptr; } @@ -182,7 +177,7 @@ pulse_mixer_finish(Mixer *data) } static int -pulse_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) +pulse_mixer_get_volume(Mixer *mixer, gcc_unused Error &error) { PulseMixer *pm = (PulseMixer *) mixer; int ret; @@ -199,7 +194,7 @@ pulse_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) } static bool -pulse_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) +pulse_mixer_set_volume(Mixer *mixer, unsigned volume, Error &error) { PulseMixer *pm = (PulseMixer *) mixer; struct pa_cvolume cvolume; @@ -209,13 +204,13 @@ pulse_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) if (!pm->online) { pulse_output_unlock(pm->output); - g_set_error(error_r, pulse_mixer_quark(), 0, "disconnected"); + error.Set(pulse_mixer_domain, "disconnected"); return false; } pa_cvolume_set(&cvolume, pm->volume.channels, (pa_volume_t)volume * PA_VOLUME_NORM / 100 + 0.5); - success = pulse_output_set_volume(pm->output, &cvolume, error_r); + success = pulse_output_set_volume(pm->output, &cvolume, error); if (success) pm->volume = cvolume; diff --git a/src/mixer/RoarMixerPlugin.cxx b/src/mixer/RoarMixerPlugin.cxx index 90d54ddaa..6bd700551 100644 --- a/src/mixer/RoarMixerPlugin.cxx +++ b/src/mixer/RoarMixerPlugin.cxx @@ -35,7 +35,7 @@ struct RoarMixer final : public Mixer { static Mixer * roar_mixer_init(void *ao, gcc_unused const config_param ¶m, - gcc_unused GError **error_r) + gcc_unused Error &error) { return new RoarMixer((RoarOutput *)ao); } @@ -49,7 +49,7 @@ roar_mixer_finish(Mixer *data) } static int -roar_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) +roar_mixer_get_volume(Mixer *mixer, gcc_unused Error &error) { RoarMixer *self = (RoarMixer *)mixer; return roar_output_get_volume(self->self); @@ -57,7 +57,7 @@ roar_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) static bool roar_mixer_set_volume(Mixer *mixer, unsigned volume, - gcc_unused GError **error_r) + gcc_unused Error &error) { RoarMixer *self = (RoarMixer *)mixer; return roar_output_set_volume(self->self, volume); diff --git a/src/mixer/SoftwareMixerPlugin.cxx b/src/mixer/SoftwareMixerPlugin.cxx index 8a268aaf1..b7eb8ff0f 100644 --- a/src/mixer/SoftwareMixerPlugin.cxx +++ b/src/mixer/SoftwareMixerPlugin.cxx @@ -26,10 +26,18 @@ #include "filter/VolumeFilterPlugin.hxx" #include "pcm/PcmVolume.hxx" #include "ConfigData.hxx" +#include "util/Error.hxx" #include #include +static Filter * +CreateVolumeFilter() +{ + Error error; + return filter_new(&volume_filter_plugin, config_param(), error); +} + struct SoftwareMixer final : public Mixer { Filter *filter; @@ -37,8 +45,7 @@ struct SoftwareMixer final : public Mixer { SoftwareMixer() :Mixer(software_mixer_plugin), - filter(filter_new(&volume_filter_plugin, config_param(), - nullptr)), + filter(CreateVolumeFilter()), volume(100) { assert(filter != nullptr); @@ -52,7 +59,7 @@ struct SoftwareMixer final : public Mixer { static Mixer * software_mixer_init(gcc_unused void *ao, gcc_unused const config_param ¶m, - gcc_unused GError **error_r) + gcc_unused Error &error) { return new SoftwareMixer(); } @@ -66,7 +73,7 @@ software_mixer_finish(Mixer *data) } static int -software_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) +software_mixer_get_volume(Mixer *mixer, gcc_unused Error &error) { SoftwareMixer *sm = (SoftwareMixer *)mixer; @@ -75,7 +82,7 @@ software_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r) static bool software_mixer_set_volume(Mixer *mixer, unsigned volume, - gcc_unused GError **error_r) + gcc_unused Error &error) { SoftwareMixer *sm = (SoftwareMixer *)mixer; diff --git a/src/mixer/WinmmMixerPlugin.cxx b/src/mixer/WinmmMixerPlugin.cxx index cbfb8f6cb..ae25b532c 100644 --- a/src/mixer/WinmmMixerPlugin.cxx +++ b/src/mixer/WinmmMixerPlugin.cxx @@ -21,6 +21,8 @@ #include "MixerInternal.hxx" #include "OutputAPI.hxx" #include "output/WinmmOutputPlugin.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include @@ -40,11 +42,7 @@ struct WinmmMixer final : public Mixer { } }; -static inline GQuark -winmm_mixer_quark(void) -{ - return g_quark_from_static_string("winmm_mixer"); -} +static constexpr Domain winmm_mixer_domain("winmm_mixer"); static inline int winmm_volume_decode(DWORD volume) @@ -61,7 +59,7 @@ winmm_volume_encode(int volume) static Mixer * winmm_mixer_init(void *ao, gcc_unused const config_param ¶m, - gcc_unused GError **error_r) + gcc_unused Error &error) { assert(ao != nullptr); @@ -77,7 +75,7 @@ winmm_mixer_finish(Mixer *data) } static int -winmm_mixer_get_volume(Mixer *mixer, GError **error_r) +winmm_mixer_get_volume(Mixer *mixer, Error &error) { WinmmMixer *wm = (WinmmMixer *) mixer; DWORD volume; @@ -85,8 +83,7 @@ winmm_mixer_get_volume(Mixer *mixer, GError **error_r) MMRESULT result = waveOutGetVolume(handle, &volume); if (result != MMSYSERR_NOERROR) { - g_set_error(error_r, 0, winmm_mixer_quark(), - "Failed to get winmm volume"); + error.Set(winmm_mixer_domain, "Failed to get winmm volume"); return -1; } @@ -94,7 +91,7 @@ winmm_mixer_get_volume(Mixer *mixer, GError **error_r) } static bool -winmm_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) +winmm_mixer_set_volume(Mixer *mixer, unsigned volume, Error &error) { WinmmMixer *wm = (WinmmMixer *) mixer; DWORD value = winmm_volume_encode(volume); @@ -102,8 +99,7 @@ winmm_mixer_set_volume(Mixer *mixer, unsigned volume, GError **error_r) MMRESULT result = waveOutSetVolume(handle, value); if (result != MMSYSERR_NOERROR) { - g_set_error(error_r, 0, winmm_mixer_quark(), - "Failed to set winmm volume"); + error.Set(winmm_mixer_domain, "Failed to set winmm volume"); return false; } -- cgit v1.2.3