~kris/9p

9hist

ref: ecf04c5e69733ed6f74adbc7ef55aabff8f089c4 9hist/port/devbit.c -rw-r--r-- 2.7 KiB
ecf04c5e — David du Colombier Plan 9 from Bell Labs 1990-03-25 36 years ago
                                                                                
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"

#include	"devtab.h"

#include	"gnot.h"

extern	Bitmap	screen;

struct{
	Ref;
	int	bltuse;
	QLock	blt;		/* a group of bitblts in a single write is atomic */
}bit;

enum{
	Qdir,
	Qbitblt,
};

Dirtab bitdir[]={
	"bitblt",	Qbitblt,	0,			0600,
};

#define	NBIT	(sizeof bitdir/sizeof(Dirtab))

void
bitreset(void)
{
}

void
bitinit(void)
{
	lock(&bit);
	bit.bltuse = 0;
	unlock(&bit);
}

Chan*
bitattach(char *spec)
{
	return devattach('b', spec);
}

Chan*
bitclone(Chan *c, Chan *nc)
{
	nc = devclone(c, nc);
	if(c->qid != CHDIR)
		incref(&bit);
}

int
bitwalk(Chan *c, char *name)
{
	return devwalk(c, name, bitdir, NBIT, devgen);
}

void
bitstat(Chan *c, char *db)
{
	devstat(c, db, bitdir, NBIT, devgen);
}

Chan *
bitopen(Chan *c, int omode)
{
	if(c->qid == CHDIR){
		if(omode != OREAD)
			error(0, Eperm);
	}else{
		/*
		 * Always open #b/bitblt first
		 */
		lock(&bit);
		if((c->qid==Qbitblt && bit.bltuse)
		|| (c->qid!=Qbitblt && !bit.bltuse)){
			unlock(&bit);
			error(0, Einuse);
		}
		unlock(&bit);
		incref(&bit);
	}
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

void
bitcreate(Chan *c, char *name, int omode, ulong perm)
{
	error(0, Eperm);
}

void
bitremove(Chan *c)
{
	error(0, Eperm);
}

void
bitwstat(Chan *c, char *db)
{
	error(0, Eperm);
}

void
bitclose(Chan *c)
{
	if(c->qid != CHDIR){
		lock(&bit);
		if(--bit.ref == 0)
			bit.bltuse = 0;
		unlock(&bit);
	}
}

long
bitread(Chan *c, void *va, long n)
{
	if(c->qid & CHDIR)
		return devdirread(c, va, n, bitdir, NBIT, devgen);

	error(0, Egreg);
}

#define	SHORT(p)	(((p)[0]<<0) | ((p)[1]<<8))
#define	LONG(p)		((SHORT(p)<<0) | (SHORT(p+2)<<16))

long
bitwrite(Chan *c, void *va, long n)
{
	uchar *p;
	long m;
	long v;
	Point pt;
	Rectangle rect;
	Bitmap *src, *dst;

	if(c->qid == CHDIR)
		error(0, Eisdir);

	p = va;
	m = n;
	switch(c->qid){
	case Qbitblt:
		qlock(&bit.blt);
		if(waserror()){
			qunlock(&bit.blt);
			nexterror();
		}
		while(m > 0)
			switch(*p){
			case 'b':
				if(m < 31)
					error(0, Ebadblt);
				v = SHORT(p+1);
				if(v != 0)		/* BUG */
					error(0, Ebadblt);
				dst = &screen;
				pt.x = LONG(p+3);
				pt.y = LONG(p+7);
				v = SHORT(p+11);
				if(v != 0)		/* BUG */
					error(0, Ebadblt);
				src = &screen;
				rect.min.x = LONG(p+13);
				rect.min.y = LONG(p+17);
				rect.max.x = LONG(p+21);
				rect.max.y = LONG(p+25);
				v = SHORT(p+29);
				bitblt(dst, pt, src, rect, v);
				m -= 31;
				p += 31;
				break;
			default:
				error(0, Ebadblt);
			}
		qunlock(&bit.blt);
		break;

	default:
		error(0, Egreg);
	}
	return n;
}

void
bituserstr(Error *e, char *buf)
{
	consuserstr(e, buf);
}

void
biterrstr(Error *e, char *buf)
{
	rooterrstr(e, buf);
}