8½.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. <html>
  2. <title>
  3. data
  4. </title>
  5. <body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#330088" ALINK="#FF0044">
  6. <H1>8&#189;, the Plan 9 Window System
  7. </H1>
  8. <DL><DD><I>Rob Pike<br>
  9. rob@plan9.bell-labs.com<br>
  10. </I></DL>
  11. <DL><DD><H4>ABSTRACT</H4>
  12. <DL>
  13. <DT><DT>&#32;<DD>
  14. NOTE:<I> Originally appeared, in a slightly different form, in
  15. Proc. of the Summer 1991 USENIX Conf.,
  16. pp. 257-265,
  17. Nashville.
  18. Note that
  19. <TT>8&#189;</TT>
  20. has been replaced by
  21. <TT>rio</TT>
  22. (see
  23. <A href="/magic/man2html/1/rio"><I>rio</I>(1)).
  24. </A></I><DT>&#32;<DD></dl>
  25. <br>
  26. The Plan 9 window system, 8&#189;, is a modest-sized program of novel design.
  27. It provides textual I/O and bitmap graphic services to both
  28. local and remote client programs by offering a multiplexed file service to those clients.
  29. It serves traditional UNIX files like
  30. <TT>/dev/tty</TT>
  31. as well as more unusual ones that provide access to the mouse
  32. and the raw screen.
  33. Bitmap graphics operations are provided by serving a file called
  34. <TT>/dev/bitblt</TT>
  35. that interprets client messages to perform raster operations.
  36. The file service that 8&#189; offers its clients is identical to that it uses for
  37. its own implementation, so it is fundamentally no more than
  38. a multiplexer.
  39. This architecture has some rewarding symmetries and can be implemented
  40. compactly.
  41. </DL>
  42. <H4>Introduction
  43. </H4>
  44. <P>
  45. In 1989 I constructed a toy window system from only a few hundred
  46. lines of source code using a custom language and an unusual architecture
  47. involving concurrent processes [Pike89].
  48. Although that system was rudimentary at best, it demonstrated that
  49. window systems are not inherently complicated.
  50. The following year, for the new Plan 9 distributed system [Pike92], I applied some of
  51. the lessons from that toy project to write, in C,
  52. a production-quality window system
  53. called 8&#189;.
  54. 8&#189; provides, on black-and-white, grey-scale, or color displays,
  55. the services required of a modern window system, including
  56. programmability and support for remote graphics.
  57. The entire system, including the default program that runs in the
  58. window &#173; the equivalent of
  59. <TT>xterm</TT>
  60. [Far89] with `cutting and pasting' between windows &#173;
  61. is well under 90 kilobytes of text on a Motorola 68020 processor, about
  62. half the size of the
  63. operating system
  64. kernel that supports it and a tenth the size of the X server
  65. [Sche86]
  66. <I>without</I>
  67. <TT>xterm</TT>.
  68. </P>
  69. <P>
  70. What makes 8&#189; so compact? Much of the saving comes from overall simplicity:
  71. 8&#189; has little graphical fanciness, a concise programming interface, and
  72. a simple, fixed user interface.
  73. 8&#189; also makes some decisions by fiat
  74. &#173; three-button mouse, overlapping windows, built-in terminal program and
  75. window manager, etc. &#173;
  76. rather than trying to appeal to all tastes.
  77. Although compact, 8&#189; is not ascetic.
  78. It provides the fundamentals and
  79. enough extras to make them comfortable to use.
  80. The most important contributor to its small size, though, is its
  81. overall design as a file server.
  82. This structure may be applicable to window systems
  83. on traditional UNIX-like operating systems.
  84. </P>
  85. <P>
  86. The small size of 8&#189; does not reflect reduced functionality:
  87. 8&#189; provides service roughly equivalent to the X window system.
  88. 8&#189;'s clients may of course be as complex as they choose,
  89. although the tendency to mimic 8&#189;'s design
  90. and the clean programming interface means they
  91. are not nearly as bloated as X applications.
  92. </P>
  93. <H4>User's Model
  94. </H4>
  95. <P>
  96. 8&#189; turns the single screen, mouse, and keyboard of the terminal
  97. (in Plan 9 terminology) or workstation (in commercial terminology) into an array
  98. of independent virtual terminals that may be textual terminals supporting a shell and
  99. the usual suite of tools
  100. or graphical applications using the full power of the bitmap screen and mouse.
  101. Text is represented in UTF, an encoding of the Unicode Standard [Pike93].
  102. The entire programming interface is provided through
  103. reading and writing files in
  104. <TT>/dev</TT>.
  105. </P>
  106. <P>
  107. Primarily for reasons of history and familiarity,
  108. the general model and appearance of 8&#189; are similar to those of
  109. <TT>mux</TT>
  110. [Pike88].
  111. The right button has a short menu for controlling window creation, destruction,
  112. and placement.
  113. When a window is created, it runs the default shell,
  114. <TT>rc</TT>
  115. [Duff90], with standard input
  116. and output directed to the window and accessible through the file
  117. <TT>/dev/cons</TT>
  118. (`console'),
  119. analogous to the
  120. <TT>/dev/tty</TT>
  121. of UNIX.
  122. The name change represents a break with the past: Plan 9 does not provide a
  123. Teletype-style model of terminals. 8&#189; provides the only way
  124. most users ever access Plan 9.
  125. </P>
  126. <P>
  127. Graphical applications,
  128. like ordinary programs,
  129. may be run by typing their names
  130. to the shell running in a window.
  131. This runs the application in the same window;
  132. to run the application in a new window one may use an external program,
  133. <TT>window</TT>,
  134. described below.
  135. For graphical applications, the virtual terminal model
  136. is extended somewhat to allow programs to perform graphical operations,
  137. access the
  138. mouse, and perform related functions by reading and writing files with
  139. suggestive names such as
  140. <TT>/dev/mouse</TT>
  141. and
  142. <TT>/dev/window</TT>
  143. multiplexed per-window
  144. much like
  145. <TT>/dev/cons</TT>.
  146. The implementation and semantics of these files,
  147. described below, is central to the structure of 8&#189;.
  148. </P>
  149. <P>
  150. The default program that runs in a window is familiar to users of Blit terminals [Pike83].
  151. It is very similar to that of
  152. <TT>mux</TT>
  153. [Pike88], providing mouse-based editing of input and output text,
  154. the ability to scroll back to see earlier output, and so on.
  155. It also has a new feature, toggled by typing ESC,
  156. that enables the user to control when
  157. typed characters may be read by the shell or application,
  158. instead of (for example) after each newline.
  159. This feature makes the window program directly useful for many text-editing
  160. tasks such as composing mail messages before sending them.
  161. </P>
  162. <H4>Plan 9 and 8&#189;
  163. </H4>
  164. <P>
  165. Plan 9 is a distributed system that provides support for UNIX-like applications
  166. in an environment built from distinct CPU servers, file servers, and terminals
  167. connected by a variety of networks [Pike90].
  168. The terminals are comparable to modest workstations that, once connected to a file
  169. server over a medium-bandwidth network such as Ethernet, are self-sufficient computers
  170. running a full operating system.
  171. Unlike workstations, however, their role is just to
  172. provide an affordable multiplexed user interface to the rest of the system:
  173. they run the window system and support simple interactive
  174. tasks such as text editing.
  175. Thus they lie somewhere between workstations and X terminals in design,
  176. cost, performance, and function.
  177. (The terminals can be used
  178. for general computing, but in practice Plan 9 users do their
  179. computing on the CPU servers.)
  180. The Plan 9 terminal software, including 8&#189;,
  181. was developed on a 68020-based
  182. machine called a Gnot
  183. and has been ported to
  184. the NeXTstation,
  185. the MIPS Magnum 3000,
  186. SGI Indigos,
  187. and Sun SPARCstations&#173;all small workstations that we use as terminals&#173;as
  188. well as PCs.
  189. </P>
  190. <P>
  191. Heavy computations such as compilation, text processing,
  192. or scientific calculation are done on the CPU servers, which are connected
  193. to the file servers by high-bandwidth networks.
  194. For interactive work,
  195. these computations can access the terminal that instantiated them.
  196. The terminal and CPU server being used by a particular user are connected to the
  197. same file server, although over different networks; Plan 9 provides a view of the
  198. file server that is independent of location in the network.
  199. </P>
  200. <P>
  201. The components of Plan 9 are connected by a common protocol based on the sharing of files.
  202. All resources in the network are implemented as file servers; programs that wish to
  203. access them connect to them over the network and communicate using ordinary file
  204. operations.
  205. An unusual aspect of Plan 9 is that the
  206. name space
  207. of a process, the set of files that can be accessed by name
  208. (for example by an
  209. <TT>open</TT>
  210. system call) is not global to all processes on a machine; distinct processes
  211. may have distinct name spaces. The system provides methods by which processes
  212. may change their name spaces, such as the ability to
  213. <I>mount</I>
  214. a service upon an existing directory, making the files of the service
  215. visible in the directory.
  216. (This is a different operation from its
  217. UNIX
  218. namesake.)
  219. Multiple services may be mounted upon the same directory,
  220. allowing the files from multiple services to be accessed in the same directory.
  221. Options to the
  222. <TT>mount</TT>
  223. system call control the order of searching for files in such a
  224. union directory.
  225. </P>
  226. <P>
  227. The most obvious example of a network resource is a file server, where permanent
  228. files reside. There are a number of unusual services, however, whose design in
  229. a different environment would likely not be file-based. Many are described
  230. elsewhere [Pike92]; some examples are the representation
  231. of processes for debugging,
  232. much like Killian's process files for the 8th edition [Kill84],
  233. and the implementation of the name/value pairs of the
  234. UNIX
  235. <TT>exec</TT>
  236. environment as files.
  237. User processes may also implement a file service and make it available to clients
  238. in the network, much like the `mounted streams' in the 9th Edition
  239. [Pres90].
  240. A typical example is a program that interprets an externally-defined file system
  241. such as that on a CD-ROM or a standard
  242. UNIX
  243. system and makes the contents available to Plan 9 programs.
  244. This design is used by all distributed applications in Plan 9, including 8&#189;.
  245. </P>
  246. <P>
  247. 8&#189; serves a set of files in the conventional directory
  248. <TT>/dev</TT>
  249. with names like
  250. <TT>cons</TT>,
  251. <TT>mouse</TT>,
  252. and
  253. <TT>screen</TT>.
  254. Clients of 8&#189; communicate with the window system by reading and writing
  255. these files.
  256. For example, a client program, such as a shell,
  257. can print text by writing its standard output, which is automatically
  258. connected to
  259. <TT>/dev/cons</TT>,
  260. or it may open and write that file explicitly.
  261. Unlike files served by a traditional file server, however, the instance of
  262. <TT>/dev/cons</TT>
  263. served in each window by 8&#189; is a distinct file;
  264. the per-process name spaces of Plan 9 allow 8&#189; to provide a unique
  265. <TT>/dev/cons</TT>
  266. to each client.
  267. This mechanism is best illustrated by the creation of a new 8&#189; client.
  268. </P>
  269. <P>
  270. When 8&#189; starts, it creates a full-duplex pipe to be the communication
  271. medium for the messages that implement the file service it will provide.
  272. One end will be shared by all the clients; the other end is held by
  273. 8&#189; to accept requests for I/O.
  274. When a user makes a new window using the mouse,
  275. 8&#189; allocates the window data structures and forks a child process.
  276. The child's name space,
  277. initially shared with the parent,
  278. is then duplicated
  279. so that changes the child makes to its name space will not affect the parent.
  280. The child then attaches its end of the communication pipe,
  281. <TT>cfd</TT>,
  282. to the directory
  283. <TT>/dev</TT>
  284. by doing a
  285. <TT>mount</TT>
  286. system call:
  287. <DL><DT><DD><TT><PRE>
  288. mount(cfd, "/dev", MBEFORE, buf)
  289. </PRE></TT></DL>
  290. This call attaches the service associated with the file descriptor
  291. <TT>cfd</TT>
  292. &#173; the client end of the pipe &#173; to the beginning of
  293. <TT>/dev</TT>
  294. so that the files in the new service take priority over existing files
  295. in the directory.
  296. This makes the new files
  297. <TT>cons</TT>,
  298. <TT>mouse</TT>,
  299. and so on,
  300. available in
  301. <TT>/dev</TT>
  302. in a way that hides any files with the same names already in place.
  303. The argument
  304. <TT>buf</TT>
  305. is a character string (null in this case),
  306. described below.
  307. </P>
  308. <P>
  309. The client process then closes file descriptors 0, 1, and 2 and opens
  310. <TT>/dev/cons</TT>
  311. repeatedly to connect the standard
  312. input, output, and error files to the window's
  313. <TT>/dev/cons</TT>.
  314. It then does an
  315. <TT>exec</TT>
  316. system call to begin executing the shell in the window.
  317. This entire sequence, complete with error handling, is 33 lines of C.
  318. </P>
  319. <P>
  320. The view of these events from 8&#189;'s end of the pipe is a sequence
  321. of file protocol messages from the new client generated by the
  322. intervening operating
  323. system in response to the
  324. <TT>mount</TT>
  325. and
  326. <TT>open</TT>
  327. system calls executed by the client.
  328. The message generated by the
  329. <TT>mount</TT>
  330. informs 8&#189; that a new client has attached to the file service it provides;
  331. 8&#189;'s response is a unique identifier kept by the operating system and
  332. passed in all messages generated by I/O on the files derived from that
  333. <TT>mount</TT>.
  334. This identifier is used by 8&#189; to distinguish the various clients so
  335. each sees a unique
  336. <TT>/dev/cons</TT>;
  337. most servers do not need to make this distinction.
  338. </P>
  339. <P>
  340. A process unrelated to 8&#189; may create windows by a variant of this mechanism.
  341. When 8&#189; begins, it uses a Plan 9 service to `post' the client end of the
  342. communication pipe in a public place.
  343. A process may open that pipe and
  344. <TT>mount</TT>
  345. it to attach to the window system,
  346. much in the way an X client may connect to a
  347. UNIX
  348. domain socket to the server bound to the file system.
  349. The final argument to
  350. <TT>mount</TT>
  351. is passed through uninterpreted by the operating
  352. system.
  353. It provides a way for the client and server to
  354. exchange information at the time of the
  355. <TT>mount</TT>.
  356. 8&#189; interprets it as the dimensions of the window to be
  357. created for the new client. (In the case above, the window has been
  358. created by the time the mount occurs, and
  359. <TT>buf</TT>
  360. carries no information.)
  361. When the
  362. <TT>mount</TT>
  363. returns, the process can open the files of the new window and begin I/O to
  364. use it.
  365. </P>
  366. <P>
  367. Because 8&#189;'s interface is based on files,
  368. standard system utilities can be used to control its services.
  369. For example,
  370. its method of creating windows externally is packaged in a
  371. 16-line shell script, called
  372. <TT>window</TT>,
  373. the core of which is just a
  374. <TT>mount</TT>
  375. operation that prefixes 8&#189;'s directory to
  376. <TT>/dev</TT>
  377. and runs a command passed on the argument line:
  378. <DL><DT><DD><TT><PRE>
  379. mount -b $'8&#189;serv' /dev
  380. $* &#60; /dev/cons &#62; /dev/cons &#62;[2] /dev/cons &amp;
  381. </PRE></TT></DL>
  382. The
  383. <TT>window</TT>
  384. program is typically employed by users to create their
  385. initial working environment when they boot the system, although
  386. it has more general possibilities.
  387. </P>
  388. <P>
  389. Other basic features of the system fall out naturally from the
  390. file-based model.
  391. When the user deletes a window, 8&#189; sends the equivalent of a
  392. UNIX
  393. signal to the process group &#173; the clients &#173; in the window,
  394. removes the window from the screen, and poisons the incoming connections
  395. to the files that drive it. If a client ignores the signal and
  396. continues to write to the window, it will get I/O errors.
  397. If, on the other hand, all the processes in a window exit spontaneously,
  398. they will automatically close all connections to the window.
  399. 8&#189; counts references to the window's files; when none are left,
  400. it shuts down the window and removes it from the screen.
  401. As a different example, when the user hits the DEL key to generate an
  402. interrupt,
  403. 8&#189; writes a message to a special file, provided by Plan 9's
  404. process control interface, that interrupts all the processes
  405. in the window.
  406. In all these examples, the implementation works seamlessly
  407. across a network.
  408. </P>
  409. <P>
  410. There are two valuable side effects of implementing
  411. a window system by multiplexing
  412. <TT>/dev/cons</TT>
  413. and other such files.
  414. First, the problem of giving a meaningful
  415. interpretation to the file
  416. <TT>/dev/cons</TT>
  417. (<TT>/dev/tty</TT>)
  418. in each window is solved automatically.
  419. To provide
  420. <TT>/dev/cons</TT>
  421. is the fundamental job of the window system, rather than just an awkward burden;
  422. other systems must often make special and otherwise irrelevant arrangements for
  423. <TT>/dev/tty</TT>
  424. to behave as expected in a window.
  425. Second, any program that can access the server, including a
  426. process on a remote machine, can access the files using standard
  427. read and write system calls to communicate with the window system,
  428. and standard open and close calls to connect to it.
  429. Again, no special arrangements need to be made for remote processes to
  430. use all the graphics facilities of 8&#189;.
  431. </P>
  432. <H4>Graphical input
  433. </H4>
  434. <P>
  435. Of course 8&#189; offers more than ASCII I/O to its clients.
  436. The state of the mouse may be discovered by reading the file
  437. <TT>/dev/mouse</TT>,
  438. which returns a ten-byte message encoding the state
  439. of the buttons and the position of the cursor.
  440. If the mouse has not moved since the last read of
  441. <TT>/dev/mouse</TT>,
  442. or if the window associated with the instance of
  443. <TT>/dev/mouse</TT>
  444. is not the `input focus', the read blocks.
  445. </P>
  446. <P>
  447. The format of the message is:
  448. <DL><DT><DD><TT><PRE>
  449. <TT>'m'</TT>
  450. 1 byte of button state
  451. 4 bytes of x, low byte first
  452. 4 bytes of y, low byte first
  453. </PRE></TT></DL>
  454. As in all shared data structures in Plan 9,
  455. the order of every byte in the message is defined
  456. so all clients can execute the same code to unpack the message
  457. into a local data structure.
  458. </P>
  459. <P>
  460. For keyboard input, clients can read
  461. <TT>/dev/cons</TT>
  462. or, if they need character-at-a-time input,
  463. <TT>/dev/rcons</TT>
  464. (`raw console').
  465. There is no explicit event mechanism to help clients that need to read
  466. from multiple sources.
  467. Instead, a small (365 line) external
  468. support library can be used.
  469. It attaches a process
  470. to the various blocking input sources &#173; mouse, keyboard, and perhaps
  471. a third user-provided file descriptor &#173;
  472. and funnels their input into a single pipe from which may be read
  473. the various types of
  474. events in the traditional style.
  475. This package is a compromise. As discussed in a previous paper
  476. [Pike89] I prefer
  477. to free applications from event-based programming. Unfortunately, though, I see
  478. no easy way to achieve this in single-threaded C programs, and am unwilling
  479. to require all programmers to master concurrent programming.
  480. It should be noted, though, that even this compromise results in a small
  481. and easily understood interface. An example program that uses it is
  482. given near the end of the paper.
  483. </P>
  484. <H4>Graphical output
  485. </H4>
  486. <P>
  487. The file
  488. <TT>/dev/screen</TT>
  489. may be read by any client to recover the contents of the entire screen,
  490. such as for printing (see Figure 1).
  491. Similarly,
  492. <TT>/dev/window</TT>
  493. holds the contents of the current window.
  494. These are read-only files.
  495. </P>
  496. <P>
  497. To perform graphics operations in their windows, client programs access
  498. <TT>/dev/bitblt</TT>.
  499. It implements a protocol that encodes bitmap graphics operations.
  500. Most of the messages in the protocol (there are 23 messages in all, about
  501. half to manage the multi-level fonts necessary for efficient handling
  502. of Unicode characters)
  503. are transmissions (via a write)
  504. from the client to the window system to perform a graphical
  505. operation such as a
  506. <TT>bitblt</TT>
  507. [PLR85] or character-drawing operation; a few include return information
  508. (recovered via a read) to the client.
  509. As with
  510. <TT>/dev/mouse</TT>,
  511. the
  512. <TT>/dev/bitblt</TT>
  513. protocol is in a defined byte order.
  514. Here, for example, is the layout of the
  515. <TT>bitblt</TT>
  516. message:
  517. <DL><DT><DD><TT><PRE>
  518. <TT>'b'</TT>
  519. 2 bytes of destination id
  520. 2x4 bytes of destination point
  521. 2 bytes of source id
  522. 4x4 bytes of source rectangle
  523. 2 bytes of boolean function code
  524. </PRE></TT></DL>
  525. </P>
  526. <DL>
  527. <DT><DT>&#32;<DD>
  528. Figure 1.
  529. A representative 8&#189; screen, running on a NeXTstation under Plan 9
  530. (with no NeXT software). In the upper right, a program announces the
  531. arrival of mail. In the top and left are a broswer for astronomical
  532. databases and an image of a galaxy produced by the browser.
  533. In the lower left there is a screen editor,
  534. <TT>sam</TT>
  535. [Pike87],
  536. editing Japanese text encoded in UTF,
  537. and in the lower right an 8&#189; running recursively and, inside that instantiation,
  538. a previewer for
  539. <TT>troff</TT>
  540. output.
  541. Underneath the faces is a small window running the command that
  542. prints the screen by passing
  543. <TT>/dev/screen</TT>
  544. to the bitmap printing utility.
  545. <br>&#32;<br>
  546. </dl>
  547. <P>
  548. The message is trivially constructed from the
  549. <TT>bitblt</TT>
  550. subroutine in the library, defined as
  551. <DL><DT><DD><TT><PRE>
  552. void bitblt(Bitmap *dst, Point dp,
  553. Bitmap *src, Rectangle sr, Fcode c).
  554. </PRE></TT></DL>
  555. </P>
  556. <P>
  557. The `id'
  558. fields in the message indicate another property of 8&#189;:
  559. the clients do not store the actual data for any of their bitmaps locally.
  560. Instead, the protocol provides a message to allocate a bitmap, to be
  561. stored in the server, and returns to the client an integer identifier,
  562. much like a
  563. UNIX
  564. file descriptor, to be used in operations on that bitmap.
  565. Bitmap number 0 is conventionally the client's window,
  566. analogous to standard input for file I/O.
  567. In fact, no bitmap graphics operations are executed in the client at all;
  568. they are all performed on its behalf by the server.
  569. Again, using the standard remote file operations in Plan 9,
  570. this permits remote machines having no graphics capability, such
  571. as the CPU server,
  572. to run graphics applications.
  573. Analogous features of the original Andrew window system [Gos86]
  574. and of X [Sche86] require more complex mechanisms.
  575. </P>
  576. <P>
  577. Nor does 8&#189; itself operate directly on bitmaps.
  578. Instead, it calls another server to do its graphics operations for it,
  579. using an identical protocol.
  580. The operating system for the Plan 9 terminals contains an internal
  581. server that implements that protocol, exactly as does 8&#189;, but for a single
  582. client. That server stores the actual bytes for the bitmaps
  583. and implements the fundamental bitmap graphics operations.
  584. Thus the environment in which 8&#189; runs
  585. has exactly the structure it provides for its clients;
  586. 8&#189; reproduces the environment for its clients,
  587. multiplexing the interface to keep the clients separate.
  588. </P>
  589. <P>
  590. This idea of multiplexing by simulation is applicable to more
  591. than window systems, of course, and has some side effects.
  592. Since 8&#189; simulates its own environment for its clients, it may run
  593. in one of its own windows (see Figure 1).
  594. A useful and common application of this
  595. technique is to connect a window to a remote machine, such as a CPU
  596. server, and run the window system there so that each subwindow is automatically
  597. on the remote machine.
  598. It is also a handy way to debug a new version of the window system
  599. or to create an environment with, for example, a different default font.
  600. </P>
  601. <H4>Implementation
  602. </H4>
  603. <P>
  604. To provide graphics to its clients, 8&#189; mostly just multiplexes and passes
  605. through to its own server the clients' requests, occasionally rearranging
  606. the messages to maintain the fiction that the clients have unique screens
  607. (windows).
  608. To manage the overlapping windows it uses the layers model,
  609. which is handled by a separate library [Pike83a].
  610. Thus it has little work to do and is a fairly simple program;
  611. it is dominated by a couple of switch statements to interpret
  612. the bitmap and file server protocols.
  613. The built-in window program and its associated menus and text-management
  614. support are responsible for most of the code.
  615. </P>
  616. <P>
  617. The operating system's server is also compact:
  618. the version for the 68020 processor, excluding the implementation
  619. of a half dozen bitmap graphics operations, is 2295 lines of C
  620. (again, about half dealing with fonts);
  621. the graphics operations are another 2214 lines.
  622. </P>
  623. <P>
  624. 8&#189; is structured as a set of communicating coroutines,
  625. much as discussed in a 1989 paper [Pike89].
  626. One coroutine manages the mouse, another the keyboard, and another
  627. is instantiated to manage the state of each window and associated client.
  628. When no coroutine wishes to run, 8&#189; reads the next file I/O request from
  629. its clients, which arrive serially on the full-duplex communication pipe.
  630. Thus 8&#189; is entirely synchronous.
  631. </P>
  632. <P>
  633. The program source is small and compiles in about 10 seconds
  634. in our Plan 9 environment. There are ten source files and
  635. one
  636. <TT>makefile</TT>
  637. totaling 5100 lines.
  638. This includes the source for the window management process,
  639. the cut-and-paste terminal program,
  640. the window/file server itself,
  641. and a small coroutine library
  642. (<TT>proc.c</TT>).
  643. It does not include the layer library
  644. (another 1031 lines)
  645. or the library to handle the cutting and pasting of text
  646. displayed in a window (960 lines),
  647. or the general graphics support library that manages all the
  648. non-drawing aspects of graphics &#173; arithmetic on points and rectangles,
  649. memory management, error handling, clipping, &#173; plus fonts,
  650. events, and non-primitive drawing operations such as circles and ellipses
  651. (a final 3051 lines).
  652. Not all the pieces of these libraries are used by 8&#189; itself;
  653. a large part of the graphics library in particular is used only by clients.
  654. Thus it is somewhat unfair to 8&#189; just to sum these numbers, including
  655. the 4509 lines of support in the kernel, and arrive
  656. at a total implementation size of 14651 lines of source to implement
  657. all of 8&#189; from the lowest levels to the highest.
  658. But that number gives a fair measure of the complexity of the overall system.
  659. </P>
  660. <P>
  661. The implementation is also efficient.
  662. 8&#189;'s performance is competitive to X windows'.
  663. Compared using Dunwoody's and Linton's
  664. <TT>gbench</TT>
  665. benchmarks on the 68020,
  666. distributed with the ``X Test Suite'',
  667. circles and arcs are drawn about half as fast in 8&#189; as in
  668. X11 release 4 compiled with
  669. <TT>gcc</TT>
  670. for equivalent hardware,
  671. probably because they are currently implemented in a user library
  672. by calls to the
  673. <TT>point</TT>
  674. primitive.
  675. Line drawing speed is about equal between the two systems.
  676. Unicode text is drawn about the same speed by 8&#189; as ASCII text by
  677. X, and
  678. the
  679. <TT>bitblt</TT>
  680. test is runs four times faster for 8&#189;.
  681. These numbers vary enough to caution against drawing sweeping
  682. conclusions, but they
  683. suggest that 8&#189;'s architecture does not penalize its performance.
  684. Finally, 8&#189; boots in under a second and creates a new window
  685. apparently instantaneously.
  686. </P>
  687. <H4>An example
  688. </H4>
  689. <P>
  690. Here is a complete program that runs under 8&#189;.
  691. It prints the string
  692. <TT>"hello world"</TT>
  693. wherever the left mouse button is depressed, and exits when the
  694. right mouse button is depressed.
  695. It also prints the string in the center of its window, and maintains
  696. that string when the window is resized.
  697. <DL><DT><DD><TT><PRE>
  698. #include &#60;u.h&#62;
  699. #include &#60;libc.h&#62;
  700. #include &#60;libg.h&#62;
  701. void
  702. ereshaped(Rectangle r)
  703. {
  704. Point p;
  705. screen.r = r;
  706. bitblt(&amp;screen, screen.r.min, &amp;screen, r, Zero); /* clear */
  707. p.x = screen.r.min.x + Dx(screen.r)/2;
  708. p.y = screen.r.min.y + Dy(screen.r)/2;
  709. p = sub(p, div(strsize(font, "hello world"), 2));
  710. string(&amp;screen, p, font, "hello world", S);
  711. }
  712. main(void)
  713. {
  714. Mouse m;
  715. binit(0, 0, 0); /* initialize graphics library */
  716. einit(Emouse); /* initialize event library */
  717. ereshaped(screen.r);
  718. for(;;){
  719. m = emouse();
  720. if(m.buttons &amp; RIGHTB)
  721. break;
  722. if(m.buttons &amp; LEFTB){
  723. string(&amp;screen, m.xy, font, "hello world", S);
  724. /* wait for release of button */
  725. do; while(emouse().buttons &amp; LEFTB);
  726. }
  727. }
  728. }
  729. </PRE></TT></DL>
  730. The complete loaded binary is a little over 26K bytes on a 68020.
  731. This program should be compared to the similar ones in the excellent paper
  732. by Rosenthal [Rose88].
  733. (The current program does more: it also employs the mouse.)
  734. The clumsiest part is
  735. <TT>ereshaped</TT>,
  736. a function with a known name that is called from the event library
  737. whenever the window is
  738. reshaped or moved, as is discovered inelegantly but adequately
  739. by a special case of a mouse message.
  740. (Simple so-called expose events are not events
  741. at all in 8&#189;; the layer library takes care of them transparently.)
  742. The lesson of this program, with deference to Rosenthal, is that if
  743. the window system is cleanly designed a toolkit should be unnecessary
  744. for simple tasks.
  745. </P>
  746. <H4>Status
  747. </H4>
  748. <P>
  749. As of 1992, 8&#189; is in regular daily use by almost all the 60 people in our
  750. research center. Some of those people use it to access Plan 9 itself; others
  751. use it as a front end to remote
  752. UNIX
  753. systems, much as one would use an X terminal.
  754. </P>
  755. <P>
  756. Some things about 8&#189; may change.
  757. It would be nice if its capabilities were more easily accessible
  758. from the shell.
  759. A companion to this paper [Pike91] proposes one way to do this,
  760. but that does not include any graphics functionality.
  761. Perhaps a textual version of the
  762. <TT>/dev/bitblt</TT>
  763. file is a way to proceed; that would allow, for example,
  764. <TT>awk</TT>
  765. programs to draw graphs directly.
  766. </P>
  767. <P>
  768. Can this style of window system be built on other operating systems?
  769. A major part of the design of 8&#189; depends on its structure as a file server.
  770. In principle this could be done for any system that supports user processes
  771. that serve files, such as any system running NFS or AFS [Sun89, Kaza87].
  772. One requirement, however, is 8&#189;'s need
  773. to respond to its clients' requests out of order:
  774. if one client reads
  775. <TT>/dev/cons</TT>
  776. in a window with no characters to be read,
  777. other clients should be able to perform I/O in their windows, or even
  778. the same window.
  779. Another constraint is that the 8&#189; files are like devices,
  780. and must not be cached by the client.
  781. NFS cannot honor these requirements; AFS may be able to.
  782. Of course, other interprocess communication mechanisms such as sockets
  783. could be used as a basis for a window system. One may even argue that
  784. X's model fits into this overall scheme. It may prove easy and worthwhile
  785. to write a small 8&#189;-like system for commercial
  786. UNIX
  787. systems to demonstrate that its merits can be won in systems other than
  788. Plan 9.
  789. </P>
  790. <H4>Conclusion
  791. </H4>
  792. <P>
  793. In conclusion, 8&#189; uses an unusual architecture in
  794. concert with the file-oriented interprocess communication of Plan 9
  795. to provide network-based interactive graphics to client programs.
  796. It demonstrates that even production-quality window systems are not
  797. inherently large or complicated
  798. and may be simple to use and to program.
  799. </P>
  800. <H4>Acknowledgements
  801. </H4>
  802. <P>
  803. Helpful comments on early drafts of this paper were made by
  804. Doug Blewett,
  805. Stu Feldman,
  806. Chris Fraser,
  807. Brian Kernighan,
  808. Dennis Ritchie,
  809. and Phil Winterbottom.
  810. 8&#189;'s support for color was added by Howard Trickey.
  811. Many of the ideas leading to 8&#189; were tried out in earlier, sometimes less
  812. successful, programs. I would like to thank those users who suffered
  813. through some of my previous 7&#189; window systems.
  814. </P>
  815. <H4>References
  816. </H4>
  817. <br>&#32;<br>
  818. [Duff90] Tom Duff, ``Rc - A Shell for Plan 9 and UNIX systems'', Proc. of the Summer 1990 UKUUG Conf., London, July, 1990, pp. 21-33, reprinted, in a different form, in this volume.
  819. <br>&#32;<br>
  820. [Far89] Far too many people, XTERM(1), Massachusetts Institute of Technology, 1989.
  821. <br>&#32;<br>
  822. [Gos86] James Gosling and David Rosenthal,
  823. ``A window manager for bitmapped displays and UNIX'', in Methodology of Window Management, edited by F.R.A. Hopgood et al., Springer, 1986.
  824. <br>&#32;<br>
  825. [Kaza87] Mike Kazar, ``Synchronization and Caching issues in the Andrew File System'', Tech. Rept. CMU-ITC-058, Information Technology Center, Carnegie Mellon University, June, 1987.
  826. <br>&#32;<br>
  827. [Kill84] Tom Killian, ``Processes as Files'', USENIX Summer Conf. Proc., Salt Lake City June, 1984.
  828. <br>&#32;<br>
  829. [Pike83] Rob Pike, ``The Blit: A Multiplexed Graphics Terminal'', Bell Labs Tech. J., V63, #8, part 2, pp. 1607-1631.
  830. <br>&#32;<br>
  831. [Pike83a] Rob Pike, ``Graphics in Overlapping Bitmap Layers'', Trans. on Graph., Vol 2, #2, 135-160, reprinted in Proc. SIGGRAPH '83, pp. 331-356.
  832. <br>&#32;<br>
  833. [Pike87] Rob Pike, ``The Text Editor <TT>sam</TT>'', Softw. - Prac. and Exp., Nov 1987, Vol 17 #11, pp. 813-845, reprinted in this volume.
  834. <br>&#32;<br>
  835. [Pike88] Rob Pike, ``Window Systems Should Be Transparent'', Comp. Sys., Summer 1988, Vol 1 #3, pp. 279-296.
  836. <br>&#32;<br>
  837. [Pike89] Rob Pike, ``A Concurrent Window System'', Comp. Sys., Spring 1989, Vol 2 #2, pp. 133-153.
  838. <br>&#32;<br>
  839. [Pike91] Rob Pike, ``A Minimalist Global User Interface'', USENIX Summer Conf. Proc., Nashville, June, 1991.
  840. <br>&#32;<br>
  841. [Pike92] Rob Pike, Dave Presotto, Ken Thompson, Howard Trickey, and Phil Winterbottom,
  842. Operating Systems Review
  843. Vol 27, #2, Apr 1993, pp. 72-76
  844. (reprinted from Proceedings of the 5th ACM SIGOPS European Workshop, Mont Saint-Michel, 1992, Paper n&#186; 34, and reprinted in this volume).
  845. <br>&#32;<br>
  846. [Pike94] Rob Pike and Ken Thompson, ``Hello World or &#191;ALPHA&#191;&#191;MU&#191;&#191;ALPHA &#191;&#191;&#191;MUEPSILON or &#191;&#191;&#191;&#191;&#191; &#191;&#191;'', USENIX Winter Conf. Proc., San Diego, Jan, 1993, reprinted in this volume.
  847. <br>&#32;<br>
  848. [PLR85] Rob Pike, Bart Locanthi and John Reiser, ``Hardware/Software Tradeoffs for Bitmap Graphics on the Blit'', Softw. - Prac. and Exp., Feb 1985, Vol 15 #2, pp. 131-152.
  849. <br>&#32;<br>
  850. [Pres90] David L. Presotto and Dennis M. Ritchie, ``Interprocess Communication in the Ninth Edition Unix System'', Softw. - Prac. and Exp., June 1990, Vol 20 #S1, pp. S1/3-S1/17.
  851. <br>&#32;<br>
  852. [Rose88] David Rosenthal, ``A Simple X11 Client Program -or- How hard can it really be to write ``Hello, World''?'', USENIX Winter Conf. Proc., Dallas, Jan, 1988, pp. 229-242.
  853. <br>&#32;<br>
  854. [Sche86] Robert W. Scheifler and Jim Gettys,
  855. ``The X Window System'',
  856. ACM Trans. on Graph., Vol 5 #2, pp. 79-109.
  857. <br>&#32;<br>
  858. [Sun89] Sun Microsystems, NFS: Network file system protocol specification,
  859. RFC 1094, Network Information Center, SRI International, March, 1989.
  860. <br>
  861. <br>&#32;<br>
  862. <A href=http://www.lucent.com/copyright.html>
  863. Copyright</A> &#169; 2000 Lucent Technologies Inc. All rights reserved.
  864. </body></html>