summaryrefslogtreecommitdiff
path: root/sys/src/cmd/reform/shortcuts.c
blob: dc3fdc85f52cdb89ce85a261291f653ecf5651d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <u.h>
#include <libc.h>
#include <keyboard.h>

static int lightstep = 5, volstep = 3;
static int light, vol, actl, mod;

static void
process(char *s)
{
	char b[128], *p;
	int n, o;
	Rune r;

	if(*s == 'K' && s[1] == 0)
		mod = 0;

	o = 0;
	b[o++] = *s;
	for(p = s+1; *p != 0; p += n){
		if((n = chartorune(&r, p)) == 1 && r == Runeerror){
			/* bail out */
			n = strlen(p);
			memmove(b+o, p, n);
			o += n;
			p += n;
			break;
		}

		if(*s == 'k' && r == Kmod4){
			mod = 1;
		}else if(*s == 'K'){
			if(mod && r >= (KF|1) && r <= (KF|4))
				continue;
			if(r == Kmod4)
				mod = 0;
		}else if(mod && ((r >= (KF|1) && r <= (KF|4) || r == Kesc))){
			if(*s == 'c'){
				if(r == (KF|1))
					fprint(light, "lcd %+d", -lightstep);
				else if(r == (KF|2))
					fprint(light, "lcd %+d", lightstep);
				else if(r == (KF|3))
					fprint(vol, "master %+d", -volstep);
				else if(r == (KF|4))
					fprint(vol, "master %+d", volstep);
				else if(r == Kesc)
					fprint(actl, "master toggle");
			}
			continue;
		}

		memmove(b+o, p, n);
		o += n;
	}

	/* all runes filtered out - ignore completely */
	if(o == 1 && p-s > 1)
		return;

	b[o++] = 0;
	if(write(1, b, o) != o)
		exits(nil);
}

static void
usage(void)
{
	fprint(2, "usage: [-l light_step] [-v vol_step] %s\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	char b[128];
	int i, j, n;

	ARGBEGIN{
	case 'l':
		lightstep = atoi(EARGF(usage()));
		break;
	case 'v':
		volstep = atoi(EARGF(usage()));
		break;
	default:
		usage();
	}ARGEND

	light = open("/dev/light", OWRITE);
	vol = open("/dev/volume", OWRITE);
	actl = open("/dev/audioctl", OWRITE);
	for(i = 0;;){
		if((n = read(0, b+i, sizeof(b)-i)) <= 0)
			break;
		n += i;
		for(j = 0; j < n; j++){
			if(b[j] == 0){
				process(b+i);
				i = j+1;
			}
		}
		memmove(b, b+i, j-i);
		i -= j;
	}

	exits(nil);
}