<commit_before>#include <gtk/gtk.h>
#include <libxfce4util/libxfce4util.h>
#include <libxfce4panel/xfce-panel-plugin.h>
#include <libxfce4panel/xfce-hvbox.h>
#include <sys/sysctl.h>
#include <assert.h>
#include <sys/stat.h>

#include "defaultoutput.h"

#define ICON "preferences-desktop-sound"
#define TARGET_SYSCTL "hw.snd.default_unit"
#define MAX_UNIT_COUNT 100

static const char *icons[] = {
	"audio-volume-high",
	"audio-volume-low",
	NULL
};

// get & set
static int defaultoutput_get_current_unit() {
	int buf;
	size_t len = sizeof(buf);
	int ret;

	ret = sysctlbyname(TARGET_SYSCTL, &buf, &len, NULL, 0);
	if (ret < 0) return -1;
	return buf;
}

static int defaultoutput_set_current_unit(int new_default) {
	char *buf;
	asprintf(&buf, "sudo sysctl %s=%d", TARGET_SYSCTL, new_default);
	int ret = system(buf);
	free(buf);
	return ret;
//	return sysctlbyname(TARGET_SYSCTL, NULL, NULL, &new_default, sizeof(new_default));
}

// update
static void defaultoutput_update_units(DefaultOutputPlugin *defaultoutput) {
	for (int i=0; i<MAX_UNIT_COUNT; i++) {
		gchar *devname = g_strdup_printf("/dev/dsp%d", i);
		struct stat st;
		int ret = stat(devname, &st);
		if (ret < 0) {
			defaultoutput->unit_count = i;
			return;
		}
	}
	g_warning("DefaultOutputPlugin: too many devices, more than %d", MAX_UNIT_COUNT);
	defaultoutput->unit_count = MAX_UNIT_COUNT;
}

static void defaultoutput_update_state(DefaultOutputPlugin *defaultoutput) {
	defaultoutput_update_units(defaultoutput);

	int current_unit = defaultoutput_get_current_unit();
	if (current_unit < 0) {
		g_critical("DefaultOutputPlugin: fail to get current unit\n");
		return;
	}
	assert(current_unit < defaultoutput->unit_count);
	defaultoutput->current_unit = current_unit;

	GdkPixbuf *pixbuf = defaultoutput->pixbufs[current_unit];
	xfce_panel_image_set_from_pixbuf(XFCE_PANEL_IMAGE(defaultoutput->image),
			pixbuf);

	gchar *tip = g_strdup_printf(_("Current unit: %d"), current_unit);
	gtk_widget_set_tooltip_text(GTK_WIDGET(defaultoutput->button), tip);
	g_free(tip);
}<commit_msg>handle unit_count > pixlib_count<commit_after>#include <gtk/gtk.h>
#include <libxfce4util/libxfce4util.h>
#include <libxfce4panel/xfce-panel-plugin.h>
#include <libxfce4panel/xfce-hvbox.h>
#include <sys/sysctl.h>
#include <assert.h>
#include <sys/stat.h>

#include "defaultoutput.h"

#define ICON "preferences-desktop-sound"
#define TARGET_SYSCTL "hw.snd.default_unit"
#define MAX_UNIT_COUNT 100

static const char *icons[] = {
	"audio-volume-high",
	"audio-volume-low",
	NULL
};

// get & set
static int defaultoutput_get_current_unit() {
	int buf;
	size_t len = sizeof(buf);
	int ret;

	ret = sysctlbyname(TARGET_SYSCTL, &buf, &len, NULL, 0);
	if (ret < 0) return -1;
	return buf;
}

static int defaultoutput_set_current_unit(int new_default) {
	char *buf;
	asprintf(&buf, "sudo sysctl %s=%d", TARGET_SYSCTL, new_default);
	int ret = system(buf);
	free(buf);
	return ret;
//	return sysctlbyname(TARGET_SYSCTL, NULL, NULL, &new_default, sizeof(new_default));
}

// update
static void defaultoutput_update_units(DefaultOutputPlugin *defaultoutput) {
	for (int i=0; i<MAX_UNIT_COUNT; i++) {
		gchar *devname = g_strdup_printf("/dev/dsp%d", i);
		struct stat st;
		int ret = stat(devname, &st);
		if (ret < 0) {
			defaultoutput->unit_count = i;
			return;
		}
	}
	g_warning("DefaultOutputPlugin: too many devices, more than %d", MAX_UNIT_COUNT);
	defaultoutput->unit_count = MAX_UNIT_COUNT;
}

static void defaultoutput_update_state(DefaultOutputPlugin *defaultoutput) {
	defaultoutput_update_units(defaultoutput);

	int current_unit = defaultoutput_get_current_unit();
	if (current_unit < 0) {
		g_critical("DefaultOutputPlugin: fail to get current unit\n");
		return;
	}
	assert(current_unit < defaultoutput->unit_count);
	defaultoutput->current_unit = current_unit;

	GdkPixbuf *pixbuf =
		defaultoutput->pixbufs[current_unit % defaultoutput->piclib_count];
	xfce_panel_image_set_from_pixbuf(XFCE_PANEL_IMAGE(defaultoutput->image),
			pixbuf);

	gchar *tip = g_strdup_printf(_("Current unit: %d"), current_unit);
	gtk_widget_set_tooltip_text(GTK_WIDGET(defaultoutput->button), tip);
	g_free(tip);
}
{'fraction_non_alphanumeric': 0.13111593154977103, 'fraction_numerical': 0.006266570257893469, 'mean_word_length': 5.95142378559464, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333328', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1533, 'n_words': 311}