8½.ms 31 KB

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