Blob


1 .TH GRAPHICS 3
2 .SH NAME
3 Display, Point, Rectangle, Cursor, initdraw, geninitdraw, drawerror, initdisplay, closedisplay, getdefont, getwindow, gengetwindow, flushimage, bufimage, lockdisplay, unlockdisplay, cursorswitch, cursorset, openfont, buildfont, freefont, Pfmt, Rfmt, strtochan, chantostr, chantodepth \- interactive graphics
4 .SH SYNOPSIS
5 .nf
6 .PP
7 .ft L
8 #include <u.h>
9 #include <libc.h>
10 #include <draw.h>
11 #include <cursor.h>
12 .ft P
13 .PP
14 .ta \w'\fLFont* 'u
15 .B
16 int initdraw(void (*errfun)(Display*, char*), char *font,
17 .B
18 char *label)
19 .PP
20 .B
21 int geninitdraw(char *devdir, void(*errfun)(Display*, char*),
22 .PP
23 .B
24 char *font, char *label, char *mousedir, char *windir,
25 .B
26 int ref)
27 .PP
28 .B
29 int newwindow(char *str)
30 .PP
31 .B
32 void drawerror(Display *d, char *msg)
33 .PP
34 .B
35 Display* initdisplay(char *devdir, char *win, void(*errfun)(Display*, char*))
36 .PP
37 .B
38 void closedisplay(Display *d)
39 .PP
40 .B
41 Font* getdefont(Display *d)
42 .PP
43 .B
44 int flushimage(Display *d, int vis)
45 .PP
46 .B
47 int bufimage(Display *d, int n)
48 .PP
49 .B
50 int lockdisplay(Display *d)
51 .PP
52 .B
53 int unlockdisplay(Display *d)
54 .PP
55 .B
56 int getwindow(Display *d, int ref)
57 .PP
58 .B
59 int gengetwindow(Display *d, char *winname,
60 .br
61 .B
62 Image **ip, Screen **sp, int ref)
63 .PP
64 .B
65 void cursorswitch(Cursor *curs)
66 .PP
67 .B
68 void cursorset(Point p)
69 .PP
70 .B
71 Font* openfont(Display *d, char *name)
72 .PP
73 .B
74 Font* buildfont(Display *d, char *desc, char *name)
75 .PP
76 .B
77 void freefont(Font *f)
78 .PP
79 .B
80 int Pfmt(Fmt*)
81 .PP
82 .B
83 int Rfmt(Fmt*)
84 .PP
85 .B
86 ulong strtochan(char *s)
87 .PP
88 .B
89 char* chantostr(char *s, ulong chan)
90 .PP
91 .B
92 int chantodepth(ulong chan)
93 .PP
94 .B
95 extern Display *display
96 .PP
97 .B
98 extern Image *screen
99 .PP
100 .B
101 extern Screen *_screen
102 .PP
103 .B
104 extern Font *font
105 .fi
106 .SH DESCRIPTION
108 .B Display
109 structure represents a connection to the graphics device,
110 .IR draw (3),
111 holding all graphics resources associated with the connection,
112 including in particular raster image data in use by the client program.
113 The structure is defined (in part) as:
114 .IP
115 .EX
116 .ta 6n +10n
117 typedef
118 struct Display
120 ...
121 void (*error)(Display*, char*);
122 ...
123 Image *black;
124 Image *white;
125 Image *opaque;
126 Image *transparent;
127 Image *image;
128 Font *defaultfont;
129 Subfont *defaultsubfont;
130 ...
131 };
132 .EE
133 .PP
135 .B Point
136 is a location in an Image
137 (see below and
138 .IR draw (3)),
139 such as the display, and is defined as:
140 .IP
141 .EX
142 .ta 6n
143 typedef
144 struct Point {
145 int x;
146 int y;
147 } Point;
148 .EE
149 .PP
150 The coordinate system has
151 .I x
152 increasing to the right and
153 .I y
154 increasing down.
155 .PP
157 .B Rectangle
158 is a rectangular area in an image.
159 .IP
160 .EX
161 .ta 6n
162 typedef
163 struct Rectangle {
164 Point min; /* upper left */
165 Point max; /* lower right */
166 } Rectangle;
167 .EE
168 .PP
169 By definition,
170 .BR min.x ≤ max.x
171 and
172 .BR min.y ≤ max.y .
173 By convention, the right (maximum
174 .IR x )
175 and bottom (maximum
176 .IR y )
177 edges are
178 excluded from the represented rectangle, so abutting rectangles have no
179 points in common.
180 Thus,
181 .B max
182 contains the coordinates of the first point beyond the rectangle.
183 .PP
184 The
185 .B Image
186 data structure is defined in
187 .IR draw (3).
188 .PP
190 .B Font
191 is a set of character images, indexed by runes (see
192 .IR utf (7)).
193 The images are organized into
194 .BR Subfonts ,
195 each containing the images for a small, contiguous set of runes.
196 The detailed format of these data structures,
197 which are described in detail in
198 .IR cachechars (3),
199 is immaterial for most applications.
200 .B Font
201 and
202 .B Subfont
203 structures contain two interrelated fields:
204 .LR ascent ,
205 the distance from the top of the highest character
206 (actually the top of the image holding all the characters)
207 to the baseline,
208 and
209 .LR height ,
210 the distance from the top of the highest character to the bottom of
211 the lowest character (and hence, the interline spacing).
212 See
213 .IR cachechars (3)
214 for more details.
215 .PP
216 .I Buildfont
217 parses the font description in the buffer
218 .BR desc ,
219 returning a
220 .B Font*
221 pointer that can be used by
222 .B string
223 (see
224 .IR draw (3))
225 to draw characters from the font.
226 .I Openfont
227 does the same, but reads the description
228 from the named file.
229 .I Freefont
230 frees a font.
231 The convention for naming font files is:
232 .IP
233 .B /lib/font/bit/\fIname\fP/\fIrange\fP.\fIsize\fP.font
234 .PD
235 .PP
236 where
237 .I size
238 is approximately the height in pixels of the lower case letters
239 (without ascenders or descenders).
240 .I Range
241 gives some indication of which characters will be available: for example
242 .BR ascii ,
243 .BR latin1 ,
244 .BR euro ,
245 or
246 .BR unicode .
247 .B Euro
248 includes most European languages, punctuation marks, the International Phonetic
249 Alphabet, etc., but no Oriental languages.
250 .B Unicode
251 includes every character for which appropriate-sized images exist on the system.
252 .PP
254 .I Cursor
255 is defined:
256 .IP
257 .EX
258 .ta 6n +\w'Point 'u
259 typedef struct
260 Cursor {
261 Point offset;
262 uchar clr[2*16];
263 uchar set[2*16];
264 } Cursor;
265 .EE
266 .PP
267 The arrays are arranged in rows, two bytes per row, left to
268 right in big-endian order to give 16 rows
269 of 16 bits each.
270 A cursor is displayed on the screen by adding
271 .B offset
272 to the current mouse position, using
273 .B clr
274 as a mask to draw white at the pixels where
275 .B clr
276 is one, and then drawing black at the pixels where
277 .B set
278 is one.
279 .PP
280 The routine
281 .I initdraw
282 connects to the display; it returns \-1 if it fails and sets the error string.
283 .I Initdraw
284 sets up the global variables
285 .B display
286 (the
287 .B Display
288 structure representing the connection),
289 .B screen
290 (an
291 .B Image
292 representing the display memory itself or, if
293 .IR rio (1)
294 is running, the client's window),
295 and
296 .B font
297 (the default font for text).
298 The arguments to
299 .I initdraw
300 include a
301 .IR label ,
302 which is written to
303 .B /dev/label
304 if non-nil
305 so that it can be used to identify the window when hidden (see
306 .IR rio (1)).
307 The font is created by reading the named
308 .I font
309 file. If
310 .B font
311 is null,
312 .I initdraw
313 reads the file named in the environment variable
314 .BR $font ;
315 if
316 .B $font
317 is not set, it imports the default (usually minimal)
318 font from the operating system.
319 The global
320 .I font
321 will be set to point to the resulting
322 .B Font
323 structure.
324 The
325 .I errfun
326 argument is a
327 .I graphics error function
328 to call in the event of a fatal error in the library; it must never return.
329 Its arguments are the
330 display pointer and an error string.
331 If
332 .I errfun
333 is nil, the library provides a default, called
334 .IR drawerror .
335 Another effect of
336 .I initdraw
337 is that it installs
338 .IR print (3)
339 formats
340 .I Pfmt
341 and
342 .I Rfmt
343 as
344 .L %P
345 and
346 .L %R
347 for printing
348 .B Points
349 and
350 .BR Rectangles .
351 .PP
352 The
353 .I geninitdraw
354 function provides a less automated way to establish a connection, for programs
355 that wish to connect to multiple displays.
356 .I Devdir
357 is the name of the directory containing the device files for the display
358 (if nil, default
359 .BR /dev );
360 .IR errfun ,
361 .IR font ,
362 and
363 .I label
364 are as in
365 .IR initdraw ;
366 .I mousedir
367 and
368 .I windir
369 are the directories holding the
370 .B mouse
371 and
372 .B winname
373 files; and
374 .I ref
375 specifies the refresh function to be used to create the window, if running under
376 .IR rio (1)
377 (see
378 .IR window (3)).
379 .\" .PP
380 .\" The function
381 .\" .I newwindow
382 .\" may be called before
383 .\" .I initdraw
384 .\" or
385 .\" .IR geninitdraw
386 .\" to cause the program to occupy a newly created window rather than take over the one in
387 .\" which it is running when it starts.
388 .\" The
389 .\" .I str
390 .\" argument, if non-null, is concatenated to the string \f5\&"new\ "\fP
391 .\" that is used to create the window (see
392 .\" .IR rio (4)).
393 .\" For example,
394 .\" .B newwindow("-hide -dy 100")
395 .\" will cause the program to run in a newly created, hidden window
396 .\" 100 pixels high.
397 .PP
398 .I Initdisplay
399 is part of
400 .IR geninitdraw ;
401 it sets up the display structures but does not allocate any fonts or call
402 .IR getwindow .
403 The arguments are similar to those of
404 .IR initdraw ;
405 .I win
406 names the directory, default
407 .BR /dev ,
408 in which the files associated with the window reside.
409 .I Closedisplay
410 disconnects the display and frees the associated data structures.
411 .I Getdefont
412 builds a
413 .B Font
414 structure from in-core data describing a default font.
415 None of these routines is needed by most programs, since
416 .I initdraw
417 calls them as needed.
418 .PP
419 The data structures associated with the display must be protected in a multi-process program,
420 because they assume only one process will be using them at a time.
421 Multi-process programs should set
422 .B display->locking
423 to
424 .BR 1 ,
425 to notify the library to use a locking protocol for its own accesses,
426 and call
427 .I lockdisplay
428 and
429 .I unlockdisplay
430 around any calls to the graphics library that will cause messages to be sent to the display device.
431 .I Initdraw
432 and
433 .I geninitdraw
434 initialize the display to the locked state.
435 .PP
436 .I Getwindow
437 returns a pointer to the window associated with the application; it is called
438 automatically by
439 .I initdraw
440 to establish the
441 .B screen
442 pointer but must be called after each resizing of the window to restore
443 the library's connection to the window.
444 If
445 .B rio
446 is not running, it returns
447 .BR display->image ;
448 otherwise it negotiates with
449 .B rio
450 by looking in
451 .B /dev/winname
452 to find the name of the window and opening it using
453 .B namedimage
454 (see
455 .IR allocimage (3)).
456 The resulting window will be created using the refresh method
457 .I ref
458 (see
459 .IR window (3));
460 this should almost always be
461 .B Refnone
462 because
463 .B rio
464 provides backing store for the window.
465 .PP
466 .I Getwindow
467 overwrites the global variables
468 .BR screen ,
469 a pointer to the
470 .B Image
471 defining the window (or the overall display, if no window system is running); and
472 .BR _screen ,
473 a pointer to the
474 .B Screen
475 representing the root of the window's hierarchy. (See
476 .IR window (3).
477 The overloading of the
478 .B screen
479 word is an unfortunate historical accident.)
480 .I Getwindow
481 arranges that
482 .B screen
483 point to the portion of the window inside the border;
484 sophisticated clients may use
485 .B _screen
486 to make further subwindows.
487 .\" Programs desiring multiple independent windows
488 .\" may use the mechanisms of
489 .\" .IR rio (4)
490 .\" to create more windows (usually by a fresh mount of the window sytem
491 .\" in a directory other than
492 .\" .BR /dev ),
493 .\" then use
494 .\" .I gengetwindow
495 .\" to connect to them.
496 .IR Gengetwindow 's
497 extra arguments are the full path of the window's
498 .B winname
499 file and pointers to be overwritten with the values of the `global'
500 .B Image
501 and
502 .B Screen
503 variables for the new window.
504 .PP
505 The mouse cursor is always displayed.
506 The initial cursor is an arrow.
507 .I Cursorswitch
508 causes the argument cursor to be displayed instead.
509 A zero argument causes a switch back to the arrow cursor.
510 .I Cursorset
511 moves the mouse cursor to position
512 .IR p ,
513 provided (if in a window) that the requesting program is
514 executing in the current window and the mouse is within
515 the window boundaries; otherwise
516 .I cursorset
517 is a no-op.
518 .PP
519 The graphics functions described in
520 .IR draw (3),
521 .IR allocimage (3),
522 .IR cachechars (3),
523 and
524 .IR subfont (3)
525 are implemented by writing commands to files under
526 .B /dev/draw
527 (see
528 .IR draw (3));
529 the writes are buffered, so the functions may not take effect immediately.
530 .I Flushimage
531 flushes the buffer, doing all pending graphics operations.
532 If
533 .I vis
534 is non-zero, any changes are also copied from the `soft screen' (if any) in the
535 driver to the visible frame buffer.
536 The various allocation routines in the library flush automatically, as does the event
537 package (see
538 .IR event (3));
539 most programs do not need to call
540 .IR flushimage .
541 It returns \-1 on error.
542 .PP
543 .I Bufimage
544 is used to allocate space for
545 .I n
546 bytes in the display buffer.
547 It is used by all the graphics routines to send messages to the display.
548 .PP
549 The functions
550 .I strtochan
551 and
552 .I chantostr
553 convert between the channel descriptor strings
554 used by
555 .IR image (7)
556 and the internal
557 .B ulong
558 representation
559 used by the graphics protocol
560 (see
561 .IR draw (3)'s
562 .B b
563 message).
564 .B Chantostr
565 writes at most nine bytes into the buffer pointed at by
566 .I s
567 and returns
568 .I s
569 on success,
571 on failure.
572 .B Chantodepth
573 returns the number of bits per pixel used by the
574 format specified by
575 .IR chan .
576 Both
577 .B chantodepth
578 and
579 .B strtochan
580 return 0 when presented
581 with bad input.
582 .SH EXAMPLES
583 To reconnect to the window after a resize event,
584 .IP
585 .EX
586 if(getwindow(display, Refnone) < 0)
587 sysfatal("resize failed: %r");
588 .EE
589 .PP
590 To create and set up a new
591 .IR rio (1)
592 window,
593 .IP
594 .EX
595 Image *screen2;
596 Screen *_screen2;
598 srvwsys = getenv("wsys");
599 if(srvwsys == nil)
600 sysfatal("can't find $wsys: %r");
601 rfork(RFNAMEG); /* keep mount of rio private */
603 fd = open(srvwsys, ORDWR);
604 if(fd < 0)
605 sysfatal("can't open $wsys: %r");
607 /* mount creates window; see \f2rio\fP(4) */
608 if(mount(fd, -1, "/tmp", MREPL, "new -dx 300-dy 200") < 0)
609 sysfatal("can't mount new window: %r");
610 if(gengetwindow(display, "/tmp/winname",
611 &screen2, &_screen2, Refnone) < 0)
612 sysfatal("resize failed: %r");
614 /* now open /tmp/cons, /tmp/mouse */
615 \&...
616 .EE
617 .SH FILES
618 .BR \*9/font/bit " directory of fonts
619 .SH SOURCE
620 .B \*9/src/libdraw
621 .SH "SEE ALSO"
622 .IR rio (1),
623 .IR addpt (3),
624 .IR allocimage (3),
625 .IR cachechars (3),
626 .IR subfont (3),
627 .IR draw (3),
628 .IR event (3),
629 .IR frame (3),
630 .IR print (3),
631 .IR window (3),
632 .IR draw (3),
633 .\" .IR rio (4),
634 .IR image (7),
635 .IR font (7)
636 .SH DIAGNOSTICS
637 An error function may call
638 .IR errstr (3)
639 for further diagnostics.
640 .SH BUGS
641 The names
642 .B clr
643 and
644 .B set
645 in the
646 .B Cursor
647 structure are reminders of an archaic color map
648 and might be more appropriately called
649 .B white
650 and
651 .BR black .
652 .PP
653 These manual pages contain many references to
654 the now-fictitious
655 .BR /dev/draw .