graphics 12 KB

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