shithub: volume

ref: ccc10edd72c940d731563964683df392fd742bd7
dir: /vol.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>

int vol, muted, headphones;
Image *bg, *mlevel, *level;
Rectangle slider;

struct{
	char	*sys;
	int	phones;
	int	speakers;
} pins[] = {
	"asquith",	27,	21,
	"x201",		25,	31,
	"r400",		22,	26,
};

char*
b2menugen(int n)
{
	if(n == 0)
		return muted ? "unmute" : "mute";
	return nil;
}

char*
b3menugen(int n)
{
	if(n == 0)
		return headphones ? "speakers" : "headphones";
	return nil;
}

void
redraw(void)
{
	slider.min = screen->r.min;
	slider.max = screen->r.max;
	slider.min.y += (100 - vol) * Dy(screen->r) / 100;

	draw(screen, screen->r, bg, nil, ZP);
	draw(screen, slider, muted ? mlevel : level, nil, ZP);
	flushimage(display, 1);
}

void
eresized(int new)
{
	if(new && getwindow(display, Refnone) < 0)
		fprint(2,"can't reattach to window");

	redraw();
}

void
main()
{
	char *system;
	int i, hpin, spin, volfd, pinfd;
	Mouse m;
	Event e;
	Menu b2menu = {nil, b2menugen, 0};
	Menu b3menu = {nil, b3menugen, 0};

	volfd = open("/dev/volume", OWRITE);
	if (volfd < 0)
		sysfatal("open /dev/volume: %r");
	pinfd = open("/dev/audioctl", OWRITE);
	if (pinfd < 0)
		sysfatal("open /dev/audioctl: %r");

	hpin = spin = 0;
	system = getenv("sysname");
	for(i = 0; i < (sizeof(pins) / sizeof(pins[0])); i++){
		if(strcmp(pins[i].sys, system) == 0){
			hpin = pins[i].phones;
			spin = pins[i].speakers;
			break;
		}
	}

	if(initdraw(nil, nil, "vol") < 0)
		sysfatal("initdraw: %r");
	einit(Emouse);

	bg = allocimagemix(display, DPalebluegreen, DWhite);
	level = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
	mlevel = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
	if(bg == nil || level == nil || mlevel == nil)
		sysfatal ("allocimage");

	vol = 75;
	muted = headphones = 0;
	if(hpin && spin)
		fprint (pinfd, "pin %d\n", spin);
	fprint (volfd, "%d\n", vol);
	redraw();

	for(;;){
		if(event(&e) == Emouse){
			m = e.mouse;
			if(m.buttons & 1){
				vol = (slider.max.y - m.xy.y) * 100 / Dy(screen->r);
				redraw();
				if(!muted)
					fprint (volfd, "%d\n", vol);
				sleep(50);
			}
			if(m.buttons & 2)
				if(emenuhit(2, &m, &b2menu) == 0){
					if(muted)
						fprint (volfd, "%d\n", vol);
					else
						fprint (volfd, "0\n");
					muted ^= 1;
					redraw();
				}
			if(m.buttons & 4)
				if(emenuhit(3, &m, &b3menu) == 0){
					headphones ^= 1;
					if(hpin && spin){
						muted = 0;
						fprint (pinfd, "pin %d\n", headphones ? hpin : spin);
						fprint (volfd, "%d\n", vol);
					}
				}
		}
	}
}