home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Doc / vtrmdoc.ms < prev    next >
Encoding:
Text File  |  1986-11-26  |  13.7 KB  |  387 lines  |  [TEXT/????]

  1. .TL
  2. .if n .po 0
  3. .if n .ll 72
  4. .if n .nr LL 72n
  5. .if t .po 3.5c \" For LaserWriter only
  6. Interfacing to Keyboard and Screen with VTRM
  7. .br
  8. (Draft)
  9. .AU
  10. Guido van Rossum
  11. .AB
  12. Since both groups of `interns' at the ABC project will soon need to
  13. interface their editors to a terminal, here is a description of the VTRM
  14. package, together with some hints on writing a simple displayer using
  15. this package.  Footnotes are used to indicate portions of the interface
  16. that may change in the future.
  17. .AE
  18. .SH
  19. 1. Introduction
  20. .LP
  21. The VTRM package can significantly ease terminal-independent input and
  22. output for a screen-oriented program like a text editor, written in C.
  23. There are
  24. about a dozen routines, with few and simple parameters, that allow
  25. optimized output to almost any ASCII terminal designed in the Western
  26. hemisphere, and input of single characters without echoing.  Features
  27. include adaptation to any screen size, scrolling, and inverse video or a
  28. similar `stand-out' mode of displaying selected characters.  Screen
  29. refreshes use available terminal features such as character insert or
  30. delete to minimize the number of characters actually output to the
  31. terminal, thus obtaining reasonable performance even at low baud rates.
  32. .PP
  33. Implementations of the VTRM package are available for
  34. .UX ,
  35. MS-DOS and the Apple Macintosh, and the package can be ported to
  36. other similar micro-computers.  It is also possible to write an
  37. interface to windowing systems like those found on Sun or Whitechapel
  38. workstations.
  39. .SH
  40. 2. Theory of operation
  41. .LP
  42. VTRM is a set of subroutines callable from a C program.  There is a
  43. certain discipline required in calling these routines; there are
  44. an initialization call, several data manipulation and input calls, and a
  45. clean-up call.  The C program that is making the calls to VTRM is
  46. further referred to as `the application'.  In fact, on different
  47. systems, different soubroutines are used; but they all conform to the
  48. same interface, which is what is described in this document.
  49. .PP
  50. VTRM has a simple model for a terminal screen: there are YMAX lines,
  51. each consisting XMAX characters.  YMAX and XMAX are not constants,
  52. but remain constant during one particular program run.*
  53. .FS
  54. *This causes problems with resizable windows as currently available
  55. under 4.3 BSD.  It is possible that future versions can adapt themselves
  56. to changing window sizes, but this will require adaptation of the
  57. programs calling VTRM.
  58. .FE
  59. .PP
  60. The coordinate system used by VTRM is different from that normally used
  61. in geometry, but more convenient for referencing screen positions:
  62. whereever a position on the screen is referenced, it has the form (Y, X)
  63. where Y is the line number and X is the column number.  The top line is
  64. line 0, the bottom line is YMAX\-1.  Likewise, the leftmost
  65. column is column 0, the rightmost is XMAX\-1.  (Note how this is
  66. compatible with array indexing practices in C.) **
  67. .FS
  68. ** This will get us into trouble with bit-mapped displays, where often
  69. the preferred character set or `font' has characters of different
  70. widths, so that the number of characters displayable on a line is not a
  71. constant.  This issue is ignored by VTRM; implementations on such
  72. systems currently use a `fixed-width' font.
  73. .FE
  74. .PP
  75. Each character position contains one character, which is in the ASCII
  76. range SPACE to `~', i.e., SPACE and all printable characters.  Each
  77. character can be displayed in one of two modes: normal or `stand-out',
  78. which is usually inverse video or underlined (this is a choice made by
  79. the VTRM package).  `Empty' parts of the screen contain spaces in normal
  80. mode. ***
  81. .FS
  82. *** Eventually the character set allowed will have to be extended to all
  83. or most characters in the machine's standard character set that can be
  84. displayed.  There is a tendency for systems to extend the 7-bit ASCII
  85. code to an 8-bit code, where the characters in the range 128-255 are
  86. used for special graphics, accented characters etc.  VTRM currently uses
  87. the 8th bit to indicate stand-out mode; this will have to be changed in
  88. the interface.  Applications are discouraged to use the 8th bit of
  89. characters as a `stand-out' flag except in the immediate interface with
  90. VTRM.
  91. .FE
  92. .PP
  93. VTRM is strongly line-oriented.  The application must offer data to VTRM
  94. in chunks of one or more lines, and scrolling operations can only be
  95. applied to entire lines.  This is not likely to change in the near
  96. future.  VTRM does not necessarily send whole lines to the screen: only
  97. changed parts of a line are actually transmitted.  VTRM recognizes most
  98. cases of insertion or deletion of small strings in a line, which can
  99. often be handled with character insertions and/or deletions.
  100. .PP
  101. VTRM may buffer its output.  There is one routine which flushes the
  102. buffer and moves the terminal's cursor to a given (Y, X) position;
  103. during the screen update process the screen contents and cursor position
  104. may temporarily be undefined.  Not all implementations actually buffer
  105. their output; micro-computer screen operations are often fast enough.
  106. In all cases, the cursor position is undefined during the screen update.
  107. .PP
  108. VTRM does not automatically clear the screen when first called; it
  109. allows an application to be written in such a manner that its output
  110. appears at the bottom of the screen and gradually scrolls upwards,
  111. mimicking the behaviour of older programs, if it so wishes.  On the
  112. other hand, it is trivial to clear the screen after the first call.
  113. .PP
  114. On
  115. .UX ,
  116. VTRM sets the terminal in a special mode (formally: cbreak, no echo, no
  117. cr to nl mapping).  This makes it
  118. necessary that an application takes measures to call the VTRM clean-up
  119. routine whenever it exits.
  120. .PP
  121. On
  122. .UX ,
  123. VTRM uses the `termcap' database to find out the terminal's
  124. capabilities.  It requires that the <:TERM:> environment variable is
  125. set, and optionally acknowledges the <:TERMCAP:> variable.  Information
  126. on the proper use of these variables should be readily accessible to all
  127. users of the application.
  128. .SH
  129. 3. Description of the interface
  130. .SH
  131. 3.1 Set-up and clean-up calls
  132. .SH
  133. TRMSTART
  134. .LP
  135. .DS
  136. <:int trmstart(p_lines, p_columns, p_flags)
  137. int *p_lines, *p_columns;
  138. int *p_flags;:>
  139. .DE
  140. This call must be made before any of the other calls are allowed.  The
  141. function result is 0 if initialization is successful, nonzero if an
  142. error occurred.  In the latter case the applications should usually
  143. print an error message and exit; the terminal state has not been
  144. altered.  The meaning of the error codes is described in the appendix.
  145. .PP
  146. The three arguments must be the addresses of three integer variables
  147. (call as, e.g., <:trmstart(&lines, &columns, &flags):>).
  148. In <:*p_lines:> and <:*p_columns:>, YMAX and XMAX are returned.
  149. In <:*p_flags:>, some flag bits are returned, but these can usually be
  150. ignored by the applications (some flag bits are defined but never used).
  151. .SH
  152. TRMEND
  153. .LP
  154. .DS
  155. <:trmend();:>
  156. .DE
  157. This function cleans up before the application exits.  If it is not
  158. called (when <:trmstart:> has been called), the application may leave
  159. the terminal in a weird mode, from which it is hard to recover for the
  160. user (on
  161. .UX -systems non-
  162. other weird things may happen).
  163. .PP
  164. So that it can be safely called from within an interrupt handler,
  165. <:trmend:> may be called at any time, before or even during a call to
  166. <:trmstart:>.  Calls at any other time than after a successful call
  167. to <:trmstart:> are ignored.  After a call to <:trmend:>, as before a
  168. call to <:trmstart:>, calls to all other routines of VTRM are forbidden
  169. (this is not enforced by all implementations \- but your program may
  170. crash).
  171. .PP
  172. An application may engage in a sequence of interactive sessions, each
  173. bracketed between calls to <:trmstart:> and <:trmend:>.  Outside these
  174. sessions, normal print-style output can safely be used, thusly:
  175. .DS
  176. <:trmstart(...);
  177. \fIVTRM interaction 1\fP
  178. trmend();
  179. printf("Hello, world\\n"); /* Etc. */
  180. trmstart(...);
  181. \fIVTRM interaction 2\fP
  182. trmend();
  183. :><:...:>
  184. .DE
  185. .SH
  186. 3.2 Output calls
  187. .SH
  188. TRMPUTDATA
  189. .LP
  190. .DS
  191. <:trmputdata(y0, y1, x, data)
  192. int y0, y1;
  193. int x;
  194. char *data;:>
  195. .DE
  196. The characters in `data' are placed on the screen, starting at line y0,
  197. position x, and continuing up to the last position of line y1.  If data
  198. is shorter than this space, the remaining positions are filled with
  199. spaces; if tdata is too long, it is truncated.  The positions 0 through
  200. x\-1 on line y0 are left unchanged.
  201. .PP
  202. Characters with their 8th bit on (or-ed with 0200 octal or 0x80 hex)
  203. are displayed in `stand-out' mode.
  204. .PP
  205. The following is an easy way of clearing the screen:
  206. .DS
  207. <:trmputdata(0, YMAX-1, 0, "");:>
  208. .DE
  209. .SH
  210. TRMSCROLLUP
  211. .LP
  212. .DS
  213. <:trmscrollup(y0, y1, n)
  214. int y0, y1;
  215. int n;:>
  216. .DE
  217. Scrolls the screen lines y0 to y1, inclusive, up by n lines.
  218. If n is negative, it scrolls down.  When scrolling up, the top n lines
  219. starting at y0 disappear, the lines from y0+n to y1 move up n lines,
  220. and n blank lines are `shifted in' at and above line y1.  Scrolling down
  221. is similar.  If abs(n) > y1\-y0, lines y0 to y1 are blanked.
  222. .SH
  223. TRMSYNC
  224. .LP
  225. .DS
  226. <:trmsync(y, x)
  227. int y, x;:>
  228. .DE
  229. Completes any pending operations, flushes the output buffer, and moves
  230. the cursor to position (y, x) on the screen.
  231. .SH
  232. TRMUNDEFINED
  233. .LP
  234. .DS
  235. <:trmundefined();:>
  236. .DE
  237. Tells VTRM to forget whatever it has remembered about the current screen
  238. contents.  This is necessary before doing a complete screen refresh in
  239. response to a user command like control-L, since such a refresh is
  240. usually intended to correct the effect of transmission errors or other
  241. processes clobbering the screen.
  242. .SH
  243. TRMBELL
  244. .LP
  245. .DS
  246. <:trmbell();:>
  247. .DE
  248. Issues an alarm to the user in a way most appropriate to the output
  249. device being used.  This may either be an audible bell or beep,
  250. or a `visible bell', meaning a flash of (part of) the screen.
  251. .PP
  252. (On
  253. .UX ,
  254. a control-G is sent to the terminal, unless the termcap entry specifies
  255. the `vb' property (visible bell).  On MS-DOS, the bell is sounded
  256. unconditionally.  On the Macintosh, `SysBeep' is called, which gives a
  257. beep with a level determined by the volume control on the Control Panel,
  258. or flashes the menu bar if the volume is set to 0.)
  259. .SH
  260. 3.3 Input calls
  261. .SH
  262. TRMINPUT
  263. .LP
  264. .DS
  265. <:int trminput();:>
  266. .DE
  267. Returns the next input character typed at the keyboard, waiting if
  268. necessary until one is typed.  When an error occurs, \-1 is returned;
  269. this is usually permanent, so further input would be futile.  This could
  270. happen, for example, when the user `hangs up'.
  271. .SH
  272. TRMAVAIL
  273. .LP
  274. .DS
  275. <:int trmavail();:>
  276. .DE
  277. Returns 1 if an input character is immediately available for
  278. <:trminput:>; 0 if no such character is available; and \-1 if the system
  279. can't find out (this is not an error condition; it means that the system
  280. cannot do a `non-blocking read').
  281. .SH
  282. TRMSENSE
  283. .LP
  284. .DS
  285. <:int trmsense(p_y, p_x)
  286. int *p_y, *p_x;:>
  287. .DE
  288. Senses the current cursor or mouse position, and returns its position,
  289. converted to screen coordinates, in <:*p_y:> and <:*p_x:>.  If the
  290. terminal is not capable of sensing the cursor position, both values are
  291. set to \-1 and the functions returns 0; after a successful sense, the
  292. function returns 1.
  293. .SH
  294. 3.4 Interrupt handling calls
  295. .SH
  296. TRMINTERRUPT
  297. .LP
  298. .DS
  299. <:int trminterrupt();:>
  300. .DE
  301. Checks for keyboard-generated interrupt.  Returns 1 if one is found, 0
  302. if not.  This may set a signal handler for <:SIGINT:>, so it may
  303. interfere with the application's signal handling.*  It may also flush
  304. type-ahead and (unfortunately enough) discard output buffers.
  305. .FS
  306. * This is an experimental feature.  Use at own risk.  Check the source
  307. code to see if its actually implemented, and how.
  308. .FE
  309. .SH
  310. TRMSUSPEND
  311. .LP
  312. .DS
  313. <:trmsuspend();:>
  314. .DE
  315. This call does nothing except on Berkeley
  316. .UX
  317. supporting job control.  Because the terminal state and screen contents
  318. are not restored when a process is suspended or resumed, programs using
  319. VTRM must be aware of suspension.  The character control-Z is received
  320. as a normal input character by <:trminput:>, because <:trmstart:> turns
  321. off most special character processing.  When a control-Z is received,
  322. the application should react as follows:
  323. .DS
  324. <:trmend();
  325. trmsuspend();
  326. trmstart(...);
  327. \fIrepaint the screen\fR:>
  328. .DE
  329. The <:trmsuspend:> call suspends the application and its `process group';
  330. this includes any subprocesses, and possibly parent processes (if the
  331. application was run from a shell script or from another program, maybe
  332. using a `shell escape').  It only returns when the process group is resumed
  333. again (with the shell's `fg' command).
  334. .SH
  335. 4. Examples
  336. .LP
  337. Here is a very small sample program:
  338. .DS
  339. <:main() {
  340.     int lines, columns, flags;
  341.     if (trmstart(&lines, &columns, &flags) != 0) exit(1);
  342.     trmputdata(0, lines-1, 0, "Hello, \\327orld!");
  343.     trmsync(1, 0);
  344.     trmend();
  345.     exit(0);
  346. }:>
  347. .DE
  348. It prints the text <:Hello, \fBW\fRorld!:> at the top of the screen,
  349. cleans the rest of the screen, and moves the cursor the the beginning of
  350. the second line.  The `W' is displayed in stand-out mode.
  351. .TL
  352. Appendix \- <:\fBtrmstart:> error codes and flags
  353. .sp 2
  354. .LP
  355. The file "trm.h" can be included to get definitions for the flags and
  356. error codes returned by <:trmstart:>.  It contains the following:
  357. .sp 1
  358. .nf
  359. <:
  360. .ta 8n 16n 24n 32n 40n 48n 56n 64n 72n
  361. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1986. */
  362.  
  363. /*
  364.  * Terminal capabilities.  These correspond to bits set by trmstart in its
  365.  * parameter flags parameter.
  366.  */
  367.  
  368. #define HAS_STANDOUT    1    /* Terminal has inverse video or underline */
  369. #define CAN_SCROLL    2    /* Terminal can insert/delete lines */
  370. #define CAN_OPTIMISE    4    /* Terminal can insert/delete characters */
  371. #define CAN_SENSE    8    /* Terminal can send cursor position */
  372.  
  373. /*
  374.  * Error codes returned by trmstart.
  375.  */
  376.  
  377. #define TE_OK        0    /* No errors */
  378. #define TE_TWICE    1    /* Trmstart called again */
  379. #define TE_NOTERM    2    /* $TERM not set or empty */
  380. #define TE_BADTERM    3    /* $TERM not found in termcap database */
  381. #define TE_DUMB        4    /* Terminal too dumb */
  382. #define TE_NOTTY    5    /* Stdout not a tty device */
  383. #define TE_NOMEM    6    /* Can't get enough memory */
  384. #define TE_OTHER    7    /* This and higher are reserved errors */
  385. :>
  386. .fi
  387.