ref: c0bc11f51c1220c8262499d6fccb8182a8803713
parent: 4973be1f0bfec6883eda0a794a3d43f42a91fdf7
author: Jacob Moody <jsmoody@iastate.edu>
date: Sun Oct 6 19:56:18 EDT 2019
Move volume control into it's own file
--- a/dat.h
+++ b/dat.h
@@ -6,6 +6,13 @@
PAUSE,
};
+enum volmsg{
+ UP,
+ DOWN,
+ MUTE,
+ UNMUTE,
+};
+
/*
* ID3v1 represents the first version of ID3 metainformation.
* The spec does not define character set, so we treat it as
--- a/fncs.h
+++ b/fncs.h
@@ -39,4 +39,7 @@
void* mapget(Hmap*,char*);
/* lib.c */
-void spawnlib(Channel*,Channel*,char*);
\ No newline at end of file
+void spawnlib(Channel*,Channel*,char*);
+
+/* vol.c */
+void spawnvol(Channel*,Channel*);
\ No newline at end of file
--- a/mkfile
+++ b/mkfile
@@ -12,6 +12,7 @@
dir.$O \
dat.$O \
lib.$O \
+ vol.$O \
</sys/src/cmd/mkone
\ No newline at end of file
--- a/mpl.c
+++ b/mpl.c
@@ -16,14 +16,6 @@
NONE
};
-enum volmsg{
- UP,
- DOWN,
- MUTE,
- UNMUTE,
-};
-
-
Mousectl *mctl;
Keyboardctl *kctl;
Channel *ctl, *lout;
@@ -110,96 +102,8 @@
eresized(0);
}
-void
-readvol(int fd, int *level)
-{
- int n;
- char *dot;
- char buf[512];
- n = pread(fd, buf, (sizeof buf) - 1, 0);
- buf[n] = '\0';
- dot = strchr(buf, '\n');
- if(dot == nil)
- goto error;
- *dot = '\0';
- dot = strstr(buf, "left");
- if(dot != nil){
- *level = atoi(dot+4+1);
- return;
- }
-
- dot = strstr(buf, "out");
- if(dot == nil)
- goto error;
-
- *level = atoi(dot+3);
- return;
-
-error:
- *level = -1;
-}
-
void
-writevol(int fd, int level)
-{
- char buf[16];
- int n = snprint(buf, sizeof buf, "%d", level);
- write(fd, buf, n);
-}
-
-void
-volthread(void *arg)
-{
- Channel **chans = arg;
- Channel *ctl = chans[0];
- Channel *out = chans[1];
-
- int fd;
- int level;
- int muted = 0;
- enum volmsg vmsg;
-
- if((fd = open("/dev/volume", ORDWR))<0){
- /* Make volume controls NOP */
- chanclose(ctl);
- chanclose(out);
- return;
- }
-
- Alt alts[] = {
- {ctl, &vmsg, CHANRCV},
- {out, &level, CHANSND},
- {nil, nil, CHANEND},
- };
-
- readvol(fd, &level);
- for(;;){
- if(alt(alts) != 0)
- continue;
- readvol(fd, &level);
- switch(vmsg){
- case UP:
- level+=5;
- writevol(fd, muted == 0 ? level : 0);
- break;
- case DOWN:
- level-=5;
- writevol(fd, muted == 0 ? level : 0);
- break;
- case MUTE:
- muted = 1;
- writevol(fd, 0);
- break;
- case UNMUTE:
- muted = 0;
- writevol(fd, level);
- break;
- }
- }
-}
-
-void
usage(void)
{
fprint(2, "Usage: %s file", argv0);
@@ -212,7 +116,6 @@
Mouse mouse;
Rune kbd;
int resize[2];
- Channel *vchans[2];
ctl = vctl = vlevel = nil;
//TODO: Use ARGBEGIN
@@ -235,9 +138,7 @@
vctl = chancreate(sizeof(enum volmsg), 0);
vlevel = chancreate(sizeof(int), 0);
- vchans[0] = vctl;
- vchans[1] = vlevel;
- threadcreate(volthread, vchans, 8192);
+ spawnvol(vctl, vlevel);
red = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DBlue);
black = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DBlack);
--- /dev/null
+++ b/vol.c
@@ -1,0 +1,107 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+
+#include "dat.h"
+#include "fncs.h"
+
+void
+readvol(int fd, int *level)
+{
+ int n;
+ char *dot;
+ char buf[512];
+ n = pread(fd, buf, (sizeof buf) - 1, 0);
+ buf[n] = '\0';
+ dot = strchr(buf, '\n');
+ if(dot == nil)
+ goto error;
+
+ *dot = '\0';
+ dot = strstr(buf, "left");
+ if(dot != nil){
+ *level = atoi(dot+4+1);
+ return;
+ }
+
+ dot = strstr(buf, "out");
+ if(dot == nil)
+ goto error;
+
+ *level = atoi(dot+3);
+ return;
+
+error:
+ *level = -1;
+}
+
+void
+writevol(int fd, int level)
+{
+ char buf[16];
+ int n = snprint(buf, sizeof buf, "%d", level);
+ write(fd, buf, n);
+}
+
+void
+volthread(void *arg)
+{
+ Channel **chans = arg;
+ Channel *ctl = chans[0];
+ Channel *out = chans[1];
+ free(chans);
+
+ int fd;
+ int level;
+ int muted = 0;
+ enum volmsg vmsg;
+
+ if((fd = open("/dev/volume", ORDWR))<0){
+ /* Make volume controls NOP */
+ chanclose(ctl);
+ chanclose(out);
+ return;
+ }
+
+ Alt alts[] = {
+ {ctl, &vmsg, CHANRCV},
+ {out, &level, CHANSND},
+ {nil, nil, CHANEND},
+ };
+
+ readvol(fd, &level);
+ for(;;){
+ if(alt(alts) != 0)
+ continue;
+ readvol(fd, &level);
+ switch(vmsg){
+ case UP:
+ level+=5;
+ writevol(fd, muted == 0 ? level : 0);
+ break;
+ case DOWN:
+ level-=5;
+ writevol(fd, muted == 0 ? level : 0);
+ break;
+ case MUTE:
+ muted = 1;
+ writevol(fd, 0);
+ break;
+ case UNMUTE:
+ muted = 0;
+ writevol(fd, level);
+ break;
+ }
+ }
+}
+
+void
+spawnvol(Channel *ctl, Channel *out)
+{
+ Channel **chans;
+ chans = emalloc(sizeof(Channel*)*2);
+ chans[0] = ctl;
+ chans[1] = out;
+ threadcreate(volthread, chans, 8192);
+}
\ No newline at end of file