zuke: simplify volume control logic

This commit is contained in:
Sigrid Solveig Haflínudóttir 2021-10-28 14:59:46 +00:00
parent 4b7e72689d
commit a84f3ef581

View file

@ -116,14 +116,17 @@ static int Scrollheight;
static int Coversz; static int Coversz;
static char * static char *
matchvname(char *s) matchvname(char **s)
{ {
char *names[] = {"master", "pcm out"}; char *names[] = {"master", "pcm out"};
int i; int i, l;
for(i = 0; i < nelem(names); i++){ for(i = 0; i < nelem(names); i++){
if(strncmp(s, names[i], strlen(names[i])) == 0) l = strlen(names[i]);
if(strncmp(*s, names[i], l) == 0){
*s += l;
return names[i]; return names[i];
}
} }
return nil; return nil;
@ -132,53 +135,39 @@ matchvname(char *s)
static void static void
chvolume(int d) chvolume(int d)
{ {
int f, l, r, ol, or; int f, x, ox, want, try;
char *s, *a[2], *n; char *s, *e;
Biobuf b; Biobuf b;
char *n;
if((f = open("/dev/volume", ORDWR)) < 0) if((f = open("/dev/volume", ORDWR)) < 0)
return; return;
Binit(&b, f, OREAD); Binit(&b, f, OREAD);
l = r = 0; want = x = -1;
for(; (s = Brdline(&b, '\n')) != nil;){ ox = 0;
memset(a, 0, sizeof(a)); for(try = 0; try < 10; try++){
if((n = matchvname(s)) != nil && tokenize(s+strlen(n), a, 2) >= 1){ for(n = nil; (s = Brdline(&b, '\n')) != nil;){
if(a[1] == nil) if((n = matchvname(&s)) != nil && (ox = strtol(s, &e, 10)) >= 0 && s != e)
a[1] = a[0];
l = ol = atoi(a[0]);
r = or = atoi(a[1]);
if(d == 0)
break; break;
for(;;){ n = nil;
l += d;
r += d;
fprint(f, "%s %d %d\n", n, l, r);
Bseek(&b, 0, 0);
for(; (s = Brdline(&b, '\n')) != nil;){
memset(a, 0, sizeof(a));
if((n = matchvname(s)) != nil && tokenize(s+strlen(n), a, 2) >= 1){
if(a[1] == nil)
a[1] = a[0];
if(atoi(a[0]) == l && atoi(a[1]) == r)
goto end;
if(atoi(a[0]) != ol && atoi(a[1]) != or)
goto end;
if(l < 0 || r < 0 || l > 100 || r > 100)
goto end;
break;
}
}
}
} }
if(want < 0){
want = CLAMP(ox+d, 0, 100);
x = ox;
}
if(n == nil || (d > 0 && ox >= want) || (d < 0 && ox <= want))
break;
x = CLAMP(x+d, 0, 100);
if(fprint(f, "%s %d\n", n, x) < 0)
break;
/* go to eof and back */
while(Brdline(&b, '\n') != nil);
Bseek(&b, 0, 0);
} }
end: volume = CLAMP(ox, 0, 100);
volume = (l+r)/2;
if(volume > 100)
volume = 100;
else if(volume < 0)
volume = 0;
Bterm(&b); Bterm(&b);
close(f); close(f);