From 645083d3a72fdeabca9c4412ca3f1b83d4ca01d5 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Mon, 1 Sep 2025 17:26:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=9F=B3=E9=87=8F=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=8F=8A=E6=B8=85=E9=99=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/status.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/status.c b/src/status.c index 2380570..14629c6 100644 --- a/src/status.c +++ b/src/status.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,7 @@ static status_changed_notify listener = NULL; static bool pulse_inited = false; static pa_cvolume current_volume; static pa_context *context = NULL; +static pa_glib_mainloop *loop = NULL; static double calc_cpu_usage(cpu_stat_t curr, cpu_stat_t prev) { uint64_t curr_idle = curr.idle + curr.iowait; @@ -291,7 +293,6 @@ static void sink_info_callback(pa_context *c, const pa_sink_info *info, int eol, } if (info) { - context = c; parse_sink(info, &status.pulse); current_volume = info->volume; pulse_inited = true; @@ -307,15 +308,11 @@ static void server_info_callback(pa_context *c, const pa_server_info *info, static void subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) { - pa_subscription_event_type_t event = t & PA_SUBSCRIPTION_EVENT_TYPE_MASK; uint32_t facility = t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK; - if (facility != PA_SUBSCRIPTION_EVENT_SINK) { - return; - } - - if (event == PA_SUBSCRIPTION_EVENT_CHANGE) { + if (facility == PA_SUBSCRIPTION_EVENT_SINK) { pa_context_get_server_info(c, server_info_callback, NULL); + return; } } @@ -327,7 +324,9 @@ static void clean_pulse() { pa_context_unref(context); context = NULL; } + if (loop) pa_glib_mainloop_free(loop); pulse_inited = false; + loop = NULL; } static void init_pulse(GMainContext *g_context); @@ -336,7 +335,7 @@ static void state_callback(pa_context *c, void *userdata) { if (PA_CONTEXT_IS_GOOD(state)) { pa_context_set_subscribe_callback(c, subscribe_callback, NULL); pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK, NULL, NULL); - pa_context_get_server_info(c, server_info_callback, userdata); + pa_context_get_server_info(c, server_info_callback, NULL); } else if (state == PA_CONTEXT_FAILED) { clean_pulse(); init_pulse((GMainContext *)userdata); @@ -344,11 +343,12 @@ static void state_callback(pa_context *c, void *userdata) { } static void init_pulse(GMainContext *g_context) { + if (loop) pa_glib_mainloop_free(loop); pa_glib_mainloop *loop = pa_glib_mainloop_new(g_context); pa_mainloop_api *api = pa_glib_mainloop_get_api(loop); - pa_context *ctx = pa_context_new(api, "ZSWM-Status-Volume"); - pa_context_connect(ctx, NULL, PA_CONTEXT_NOFAIL, NULL); - pa_context_set_state_callback(ctx, state_callback, g_context); + context = pa_context_new(api, "ZSWM-Status-Volume"); + pa_context_connect(context, NULL, PA_CONTEXT_NOFAIL, NULL); + pa_context_set_state_callback(context, state_callback, g_context); } static gboolean update_status(gpointer data) {