~vilhalmer/pavc

66a6f1a5e09e919129523c639f738450557b48f2 — Bill Doyle 8 months ago b217883
Implement everything else
1 files changed, 62 insertions(+), 4 deletions(-)

M pavc.c
M pavc.c => pavc.c +62 -4
@@ 21,6 21,7 @@ struct pavc {
		SET,
	} mode;
	char * sink;
	uint32_t amount;
};

static void query_volume_sink(


@@ 37,7 38,55 @@ static void query_volume_sink(
	pa_volume_t vol = pa_cvolume_avg(&info->volume);
	char vol_str[1024] = {0};
	pa_volume_snprint(vol_str, 1023, vol);
	printf("volume: %s\n", vol_str);
	if (info->mute) {
		printf("%s mute\n", vol_str);
	}
	else {
		printf("%s\n", vol_str);
	}
}

static void adjust_volume_sink(
		pa_context * ctx,
		const pa_sink_info * info,
		int eol,
		void * userdata) {
	struct pavc * pavc = userdata;
	if (eol) {
		pa_mainloop_quit(pavc->loop, 0);
		return;
	}

	pa_cvolume vol = info->volume;
	switch (pavc->mode) {
	case UP:
		pa_cvolume_inc_clamp(&vol, pavc->amount, PA_VOLUME_NORM);
		break;
	case DOWN:
		pa_cvolume_dec(&vol, pavc->amount);
		break;
	case SET:
		pa_cvolume_set(&vol, info->channel_map.channels, pavc->amount);
		break;
	default:
		assert(!"unreachable");
	}

	pa_context_set_sink_volume_by_name(ctx, pavc->sink, &vol, NULL, NULL);
}

static void toggle_mute_sink(
		pa_context * ctx,
		const pa_sink_info * info,
		int eol,
		void * userdata) {
	struct pavc * pavc = userdata;
	if (eol) {
		pa_mainloop_quit(pavc->loop, 0);
		return;
	}

	pa_context_set_sink_mute_by_name(ctx, pavc->sink, !info->mute, NULL, NULL);
}

static void state_changed(pa_context * ctx, void * userdata) {


@@ 61,6 110,14 @@ static void state_changed(pa_context * ctx, void * userdata) {
		case UNMUTE:
			pa_context_set_sink_mute_by_name(ctx, pavc->sink, false, NULL, NULL);
			break;
		case TOGGLE:
			pa_context_get_sink_info_by_name(ctx, pavc->sink, toggle_mute_sink, pavc);
			return;
		case UP:
		case DOWN:
		case SET:
			pa_context_get_sink_info_by_name(ctx, pavc->sink, adjust_volume_sink, pavc);
			return;
		default:
			break;
		}


@@ 113,6 170,7 @@ int main(int argc, char * argv[]) {
		.loop = loop,
		.mode = PRINT,
		.sink = "@DEFAULT_SINK@",
		.amount = PA_VOLUME_NORM / 20,
	};

	int opt;


@@ 157,15 215,15 @@ int main(int argc, char * argv[]) {
		++i;
	}

	int amount = 0;
	if (i < argc) {
		if (pavc.mode == UP || pavc.mode == DOWN || pavc.mode == SET) {
			char * extra;
			amount = strtol(argv[i], &extra, 10);
			if ((*extra != '%' && *extra != '\0') || amount < 0) {
			long int amount = strtol(argv[i], &extra, 10);
			if ((*extra != '%' && *extra != '\0')) {
				fprintf(stderr, "Invalid percent '%s'\n", argv[i]);
				return 1;
			}
			pavc.amount = (amount / 100.0) * PA_VOLUME_NORM;
		}
		else {
			fprintf(stderr, "Unexpected argument\n");