xref: /inferno-os/os/boot/rpcg/libg.h (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1 #pragma	src	"/sys/src/libg"
2 #pragma	lib	"libg.a"
3 
4 enum	/* constants for I/O to devgraphics */
5 {
6 	Tilehdr = 40,
7 	Tilesize = 8000
8 };
9 
10 /*
11  *  you may think it's a blit, but it's gnot
12  */
13 enum
14 {
15 	EMAXMSG = 128+8192,	/* size of 9p header+data */
16 };
17 
18 /*
19  * Types
20  */
21 
22 typedef struct	Bitmap		Bitmap;
23 typedef struct	Display	Display;
24 typedef struct	Point		Point;
25 typedef struct	Rectangle 	Rectangle;
26 typedef struct	Cursor		Cursor;
27 typedef struct	Mouse		Mouse;
28 typedef struct	Menu		Menu;
29 typedef struct	Font		Font;
30 typedef struct	Fontchar	Fontchar;
31 typedef struct	Subfont		Subfont;
32 typedef struct	Cachefont	Cachefont;
33 typedef struct	Cacheinfo	Cacheinfo;
34 typedef struct	Cachesubf	Cachesubf;
35 typedef struct	Event		Event;
36 typedef struct	Slave		Slave;
37 typedef struct	Ebuf		Ebuf;
38 typedef struct	RGB		RGB;
39 typedef struct	Linedesc	Linedesc;
40 typedef struct	DRefret	DRefret;
41 
42 struct DRefret
43 {
44 	int		n;	/* number of bytes */
45 	int		dy;	/* number of lines */
46 	uchar	*dp;	/* pointer to data */
47 };
48 
49 struct	Point
50 {
51 	int	x;
52 	int	y;
53 };
54 
55 struct Rectangle
56 {
57 	Point min;
58 	Point max;
59 };
60 
61 typedef	DRefret DRefresh(Display*, int, Rectangle, uchar*, uchar*, int);
62 
63 struct	Bitmap
64 {
65 	Rectangle		r;		/* rectangle in data area, local coords */
66 	Rectangle 	clipr;		/* clipping region */
67 	int			ldepth;	/* log base 2 of number of bits per pixel */
68 	ulong		*base;	/* pointer to start of data */
69 	int			zero;		/* base+zero=&word containing (0,0) */
70 	ulong		width;	/* width in words of total data area */
71 	Display		*display;	/* if present */
72 };
73 
74 struct	Display
75 {
76 	uchar		*data;	/* transfer buffer */
77 	Rectangle		r;
78 	int			ldepth;
79 	Rectangle		bb;		/* bounding box of changes */
80 	int			waste;	/* unused part of bb */
81 	Rectangle		bound;	/* memory for boundin/boundout */
82 	Bitmap		*image;	/* owner */
83 	int			id;
84 	int			fd;
85 	int			ctlfd;
86 	int			local;
87 	int			bytewidth;
88 	void			*drdata1;	/* storage for drefresh() */
89 	void			*drdata2;	/* storage for drefresh() */
90 	DRefresh		*drefresh;
91 };
92 
93 
94 struct	Mouse
95 {
96 	int	buttons;	/* bit array: LMR=124 */
97 	Point	xy;
98 	ulong	msec;
99 };
100 
101 struct	Cursor
102 {
103 	Point	offset;
104 	uchar	clr[2*16];
105 	uchar	set[2*16];
106 };
107 
108 struct Menu
109 {
110 	char	**item;
111 	char	*(*gen)(int);
112 	int	lasthit;
113 };
114 
115 struct Linedesc
116 {
117 	int	x0;
118 	int	y0;
119 	char	xmajor;
120 	char	slopeneg;
121 	long	dminor;
122 	long	dmajor;
123 };
124 
125 /*
126  * Subfonts
127  *
128  * given char c, Subfont *f, Fontchar *i, and Point p, one says
129  *	i = f->info+c;
130  *	bitblt(b, Pt(p.x+i->left,p.y+i->top),
131  *		bitmap, Rect(i->x,i->top,(i+1)->x,i->bottom),
132  *		fc);
133  *	p.x += i->width;
134  * where bitmap b is the repository of the images.
135  *
136  */
137 
138 struct	Fontchar
139 {
140 	short	x;		/* left edge of bits */
141 	uchar	top;		/* first non-zero scan-line */
142 	uchar	bottom;		/* last non-zero scan-line + 1 */
143 	char	left;		/* offset of baseline */
144 	uchar	width;		/* width of baseline */
145 };
146 
147 struct	Subfont
148 {
149 	short	n;		/* number of chars in font */
150 	uchar	height;		/* height of bitmap */
151 	char	ascent;		/* top of bitmap to baseline */
152 	Fontchar *info;		/* n+1 character descriptors */
153 	Bitmap	*bits;		/* of font */
154 };
155 
156 enum
157 {
158 	/* starting values */
159 	LOG2NFCACHE =	6,
160 	NFCACHE =	(1<<LOG2NFCACHE),	/* #chars cached */
161 	NFLOOK =	5,			/* #chars to scan in cache */
162 	NFSUBF =	2,			/* #subfonts to cache */
163 	/* max value */
164 	MAXFCACHE =	2048+NFLOOK,		/* generous upper limit */
165 	MAXSUBF =	50,			/* generous upper limit */
166 	/* deltas */
167 	DSUBF = 	4,
168 	/* expiry ages */
169 	SUBFAGE	=	10000,
170 	CACHEAGE =	10000,
171 };
172 
173 struct Cachefont
174 {
175 	Rune	min;	/* lowest rune value to be taken from subfont */
176 	Rune	max;	/* highest rune value+1 to be taken from subfont */
177 	int	offset;	/* position in subfont of character at min */
178 	int	abs;	/* name has been made absolute */
179 	char	*name;
180 };
181 
182 struct Cacheinfo
183 {
184 	Rune		value;	/* value of character at this slot in cache */
185 	ushort		age;
186 	ulong		xright;	/* right edge of bits */
187 	Fontchar;
188 };
189 
190 struct Cachesubf
191 {
192 	ulong		age;	/* for replacement */
193 	Cachefont	*cf;	/* font info that owns us */
194 	Subfont		*f;	/* attached subfont */
195 };
196 
197 struct Font
198 {
199 	char		*name;
200 	short		height;	/* max height of bitmap, interline spacing */
201 	short		ascent;	/* top of bitmap to baseline */
202 	int			maxldepth;	/* over all loaded subfonts */
203 	short		width;	/* widest so far; used in caching only */
204 	short		ldepth;	/* of images */
205 	short		nsub;	/* number of subfonts */
206 	ulong		age;	/* increasing counter; used for LRU */
207 	int		ncache;	/* size of cache */
208 	int		nsubf;	/* size of subfont list */
209 	Cacheinfo	*cache;
210 	Cachesubf	*subf;
211 	Cachefont	**sub;	/* as read from file */
212 	Bitmap	*cacheimage;
213 };
214 
215 struct	Event
216 {
217 	int	kbdc;
218 	Mouse	mouse;
219 	int	n;		/* number of characters in mesage */
220 	uchar	data[EMAXMSG];	/* message from an arbitrary file descriptor */
221 };
222 
223 struct Slave{
224 	int	pid;
225 	Ebuf	*head;		/* queue of messages for this descriptor */
226 	Ebuf	*tail;
227 };
228 
229 struct Ebuf{
230 	Ebuf	*next;
231 	int	n;		/* number of bytes in buf */
232 	uchar	buf[EMAXMSG];
233 };
234 
235 struct RGB
236 {
237 	ulong	red;
238 	ulong	green;
239 	ulong	blue;
240 };
241 
242 /*
243  * Codes for bitblt etc.
244  *
245  *	       D
246  *	     0   1
247  *         ---------
248  *	 0 | 1 | 2 |
249  *     S   |---|---|
250  * 	 1 | 4 | 8 |
251  *         ---------
252  *
253  *	Usually used as D|S; DorS is so tracebacks are readable.
254  */
255 typedef
256 enum	Fcode
257 {
258 	Zero		= 0x0,
259 	DnorS		= 0x1,
260 	DandnotS	= 0x2,
261 	notS		= 0x3,
262 	notDandS	= 0x4,
263 	notD		= 0x5,
264 	DxorS		= 0x6,
265 	DnandS		= 0x7,
266 	DandS		= 0x8,
267 	DxnorS		= 0x9,
268 	D		= 0xA,
269 	DornotS		= 0xB,
270 	S		= 0xC,
271 	notDorS		= 0xD,
272 	DorS		= 0xE,
273 	F		= 0xF,
274 } Fcode;
275 
276 /*
277  * Miscellany
278  */
279 
280 extern Point	 add(Point, Point), sub(Point, Point);
281 extern Point	 mul(Point, int), div(Point, int);
282 extern Rectangle rsubp(Rectangle, Point), raddp(Rectangle, Point), inset(Rectangle, int);
283 extern Rectangle rmul(Rectangle, int), rdiv(Rectangle, int);
284 extern Rectangle rshift(Rectangle, int), rcanon(Rectangle);
285 extern Bitmap*	 balloc(Rectangle, int);
286 extern Bitmap*	 ballocnomem(Rectangle, int);
287 extern Bitmap*	 brealloc(Bitmap*, Rectangle, int);
288 extern Bitmap*	 breallocnomem(Bitmap*, Rectangle, int);
289 extern int		bbytewidth(Bitmap*, int*, int*);
290 extern void	 bfree(Bitmap*);
291 extern void	 bfreemem(Bitmap*);
292 extern int	 rectclip(Rectangle*, Rectangle);
293 extern void	 binit(void(*)(char*), char*, char*);
294 extern void	 binit1(void(*)(char*), char*, char*, int);
295 extern void	 bclose(void);
296 extern void	 berror(char*);
297 extern void	 bitblt(Bitmap*, Point, Bitmap*, Rectangle, Fcode);
298 extern int	 bitbltclip(void*);
299 extern Font*	 rdfontfile(char*, int);
300 extern void	 ffree(Font*);
301 extern void	fminldepth(Font*);
302 extern Font*	 mkfont(Subfont*, Rune);
303 extern Subfont*	 subfalloc(int, int, int, Fontchar*, Bitmap*);
304 extern void	 subffree(Subfont*);
305 extern int	 cachechars(Font*, char**, ushort*, int, int*);
306 extern Point	 string(Bitmap*, Point, Font*, char*, Fcode);
307 extern Point	 subfstring(Bitmap*, Point, Subfont*, char*, Fcode);
308 extern void	 segment(Bitmap*, Point, Point, int, Fcode);
309 extern void	 point(Bitmap*, Point, int, Fcode);
310 extern void	 arc(Bitmap*, Point, Point, Point, int, Fcode);
311 extern void	 circle(Bitmap*, Point, int, int, Fcode);
312 extern void	 disc(Bitmap*, Point, int, int, Fcode);
313 extern void	 ellipse(Bitmap*, Point, int, int, int, Fcode);
314 extern long	 strwidth(Font*, char*);
315 extern void	 agefont(Font*);
316 extern int	 loadchar(Font*, Rune, Cacheinfo*, int, int);
317 extern Point	 strsize(Font*, char*);
318 extern long	 charwidth(Font*, Rune);
319 extern void	 texture(Bitmap*, Rectangle, Bitmap*, Fcode);
320 extern void	 wrbitmap(Bitmap*, int, int, uchar*);
321 extern void	 rdbitmap(Bitmap*, int, int, uchar*);
322 extern void	 wrbitmapfile(int, Bitmap*);
323 extern Bitmap*	 rdbitmapfile(int);
324 extern void	 wrsubfontfile(int, Subfont*);
325 extern void	 wrcolmap(Bitmap*, RGB*);
326 extern void	 rdcolmap(Bitmap*, RGB*);
327 extern Subfont*	 rdsubfontfile(int, Bitmap*);
328 extern void	_unpackinfo(Fontchar*, uchar*, int);
329 
330 extern int	 ptinrect(Point, Rectangle), rectinrect(Rectangle, Rectangle);
331 extern int	 rectXrect(Rectangle, Rectangle);
332 extern int	 eqpt(Point, Point), eqrect(Rectangle, Rectangle);
333 extern void	 border(Bitmap*, Rectangle, int, Fcode);
334 extern void	 cursorswitch(Cursor*);
335 extern void	 cursorset(Point);
336 extern Rectangle bscreenrect(Rectangle*);
337 extern void	 bflush(void);
338 extern void	 bexit(void);
339 extern int	 _clipline(Rectangle, Point*, Point*, Linedesc*);
340 extern int	 clipline(Rectangle, Point*, Point*);
341 extern int	 clipr(Bitmap*, Rectangle);
342 
343 extern void	 einit(ulong);
344 extern ulong	 estart(ulong, int, int);
345 extern ulong	 etimer(ulong, int);
346 extern ulong	 event(Event*);
347 extern ulong	 eread(ulong, Event*);
348 extern Ebuf*	 ebread(Slave*);
349 extern Mouse	 emouse(void);
350 extern int	 ekbd(void);
351 extern int	 ecanread(ulong);
352 extern int	 ecanmouse(void);
353 extern int	 ecankbd(void);
354 extern void	 ereshaped(Rectangle);	/* supplied by user */
355 extern int	 menuhit(int, Mouse*, Menu*);
356 extern Rectangle getrect(int, Mouse*);
357 extern ulong	 rgbpix(Bitmap*, RGB);
358 extern int	_gminor(long, Linedesc*);
359 
360 enum{
361 	Emouse		= 1,
362 	Ekeyboard	= 2,
363 };
364 
365 enum
366 {
367 	MAXSLAVE = 32,
368 };
369 
370 #define	Pt(x, y)		((Point){(x), (y)})
371 #define	Rect(x1, y1, x2, y2)	((Rectangle){Pt(x1, y1), Pt(x2, y2)})
372 #define	Rpt(p1, p2)		((Rectangle){(p1), (p2)})
373 
374 
375 #define	Dx(r)	((r).max.x-(r).min.x)
376 #define	Dy(r)	((r).max.y-(r).min.y)
377 
378 extern	Bitmap	screen;
379 extern	Font	*font;
380 extern	uchar	_btmp[8192];
381 
382 extern	int	_mousefd;
383 extern	int	_cursorfd;
384 
385 #define	BGSHORT(p)		(((p)[0]<<0) | ((p)[1]<<8))
386 #define	BGLONG(p)		((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
387 #define	BPSHORT(p, v)		((p)[0]=(v), (p)[1]=((v)>>8))
388 #define	BPLONG(p, v)		(BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
389 
390 ulong	*wordaddr(Bitmap*, Point);
391 uchar	*byteaddr(Bitmap*, Point);
392 int		dfree(Display*);
393 int		dwritectl(Display*, char*, int);
394 int		dreadctl(Display*, char*, int);
395 int		dinfo(Display*, int, int*, Rectangle*);
396 void*	dinit(Display*, Bitmap*, int, int);
397 int		ddelete(Display*);
398 void		dfreemem(Display*);
399 int		dreadctl(Display*, char*, int);
400 int		dwritectl(Display*, char*, int);
401 void	dbound(Display*, Rectangle);
402 void	bload(Bitmap*, Rectangle, uchar*);
403 ulong	bunload(Bitmap*, Rectangle, uchar*);
404 void		drefresh(Display*, Rectangle);
405 Display	*dopen(char*, int, DRefresh*);
406 Bitmap*	dbitmap(Display*, DRefresh*, int);
407 void		dclose(Display*);
408 void		dflush(Display*);
409 void		_bltinit(void);
410 Bitmap*	battach(Bitmap*, int, int);
411 int		readmouse(Mouse*);
412 int		atomouse(Mouse*, char*, int);
413 
414 /*
415  * Refresh functions
416  */
417 DRefresh	drtexture;
418 DRefresh	drbackstore;
419