graphics 12 KB

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