xref: /plan9-contrib/sys/src/cmd/samterm/plan9.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <libg.h>
4 #include <frame.h>
5 #include "flayer.h"
6 #include "samterm.h"
7 
8 static char exname[64];
9 
10 void
11 getscreen(int argc, char **argv)
12 {
13 	USED(argc);
14 	USED(argv);
15 	binit(panic, 0, "sam");
16 	bitblt(&screen, screen.clipr.min, &screen, screen.clipr, 0);
17 }
18 
19 int
20 screensize(int *w, int *h)
21 {
22 	int fd, n;
23 	char buf[5*12+1];
24 
25 	fd = open("/dev/screen", OREAD);
26 	if(fd < 0)
27 		return 0;
28 	n = read(fd, buf, sizeof(buf)-1);
29 	close(fd);
30 	if(n != sizeof(buf)-1)
31 		return 0;
32 	buf[n] = 0;
33 	if(h){
34 		*h = atoi(buf+4*12)-atoi(buf+2*12);
35 		if (*h < 0)
36 			return 0;
37 	}
38 	if(w){
39 		*w = atoi(buf+3*12)-atoi(buf+1*12);
40 		if (*w < 0)
41 			return 0;
42 	}
43 	return 1;
44 }
45 
46 int
47 snarfswap(char *fromsam, int nc, char **tosam)
48 {
49 	char *s1;
50 	int f, n;
51 
52 	f = open("/dev/snarf", 0);
53 	if(f < 0)
54 		return -1;
55 	*tosam = s1 = alloc(SNARFSIZE);
56 	n = read(f, s1, SNARFSIZE-1);
57 	close(f);
58 	if(n < 0)
59 		n = 0;
60 	if(n == 0){
61 		*tosam = 0;
62 		free(s1);
63 	}else
64 		s1[n] = 0;
65 	f = create("/dev/snarf", 1, 0666);
66 	if(f >= 0){
67 		write(f, fromsam, nc);
68 		close(f);
69 	}
70 	return n;
71 }
72 
73 void
74 dumperrmsg(int count, int type, int count0, int c)
75 {
76 	fprint(2, "samterm: host mesg: count %d %ux %ux %ux %s...ignored\n",
77 		count, type, count0, c, rcvstring());
78 }
79 
80 void
81 removeextern(void)
82 {
83 	remove(exname);
84 }
85 
86 void
87 extstart(void)
88 {
89 	char buf[32];
90 	int fd, p[2];
91 
92 	if(pipe(p) < 0)
93 		return;
94 	sprint(exname, "/srv/sam.%s", getuser());
95 	fd = create(exname, 1, 0600);
96 	if(fd < 0){	/* assume existing guy is more important */
97     Err:
98 		close(p[0]);
99 		close(p[1]);
100 		return;
101 	}
102 	sprint(buf, "%d", p[0]);
103 	if(write(fd, buf, strlen(buf)) <= 0)
104 		goto Err;
105 	close(fd);
106 	/*
107 	 * leave p[0] open so if the file is removed the event
108 	 * library won't get an error
109 	 */
110 	estart(Eextern, p[1], 8192);
111 	atexit(removeextern);
112 }
113