xref: /plan9-contrib/sys/src/cmd/vt/consctl.c (revision ac84fd08a8c8d942164771116b1cd72c58cf8dc5)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include "cons.h"
5 
6 /*
7  *  bind a pipe onto consctl and keep reading it to
8  *  get changes to console state.
9  */
10 Consstate*
consctl(void)11 consctl(void)
12 {
13 	int i, n, fd, tries;
14 	char buf[128];
15 	Consstate *x;
16 	char *field[10];
17 
18 	x = segattach(0, "shared", 0, sizeof *x);
19 	if(x == (void*)-1)
20 		sysfatal("segattach: %r");
21 
22 	/* a pipe to simulate consctl */
23 	if(bind("#|", "/mnt/cons/consctl", MBEFORE) < 0
24 	|| bind("/mnt/cons/consctl/data1", "/dev/consctl", MREPL) < 0)
25 		sysfatal("bind consctl: %r");
26 
27 	/* a pipe to simulate the /dev/cons */
28 	if(bind("#|", "/mnt/cons/cons", MREPL) < 0
29 	|| bind("/mnt/cons/cons/data1", "/dev/cons", MREPL) < 0)
30 		sysfatal("bind cons: %r");
31 
32 	switch(fork()){
33 	case -1:
34 		sysfatal("fork: %r");
35 	case 0:
36 		break;
37 	default:
38 		return x;
39 	}
40 
41 	notify(0);
42 
43 	for(tries = 0; tries < 100; tries++){
44 		x->raw = 0;
45 		x->hold = 0;
46 		fd = open("/mnt/cons/consctl/data", OREAD);
47 		if(fd < 0)
48 			break;
49 		tries = 0;
50 		for(;;){
51 			n = read(fd, buf, sizeof(buf)-1);
52 			if(n <= 0)
53 				break;
54 			buf[n] = 0;
55 			n = getfields(buf, field, 10, 1, " ");
56 			for(i = 0; i < n; i++){
57 				if(strcmp(field[i], "rawon") == 0)
58 					x->raw = 1;
59 				else if(strcmp(field[i], "rawoff") == 0)
60 					x->raw = 0;
61 				else if(strcmp(field[i], "holdon") == 0)
62 					x->hold = 1;
63 				else if(strcmp(field[i], "holdoff") == 0)
64 					x->hold = 0;
65 			}
66 		}
67 		close(fd);
68 	}
69 	exits(0);
70 	return 0;		/* dummy to keep compiler quiet*/
71 }
72