<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE> S-Lang Library C Programmer's Guide, V1.4.2: Screen Management</TITLE> <LINK HREF="cslang-6.html" REL=next> <LINK HREF="cslang-4.html" REL=previous> <LINK HREF="cslang.html#toc5" REL=contents> </HEAD> <BODY> <A HREF="cslang-6.html">Next</A> <A HREF="cslang-4.html">Previous</A> <A HREF="cslang.html#toc5">Contents</A> <HR> <H2><A NAME="s5">5. Screen Management</A></H2> <P> <P>The <B>S-Lang</B> library provides two interfaces to terminal independent routines for manipulating the display on a terminal. The highest level interface, known as the <CODE>SLsmg</CODE> interface is discussed in this section. It provides high level screen management functions more manipulating the display in an optimal manner and is similar in spirit to the <CODE>curses</CODE> library. The lowest level interface, or the <CODE>SLtt</CODE> interface, is used by the <CODE>SLsmg</CODE> routines to actually perform the task of writing to the display. This interface is discussed in another section. Like the keyboard routines, the <CODE>SLsmg</CODE> routines are <EM>platform independent</EM> and work the same on MSDOS, OS/2, Unix, and VMS. <P>The screen management, or <CODE>SLsmg</CODE>, routines are initialized by function <CODE>SLsmg_init_smg</CODE>. Once initialized, the application uses various <CODE>SLsmg</CODE> functions to write to a <EM>virtual</EM> display. This does not cause the <EM>physical</EM> terminal display to be updated immediately. The physical display is updated to look like the virtual display only after a call to the function <CODE>SLsmg_refresh</CODE>. Before exiting, the application using these routines is required to call <CODE>SLsmg_reset_smg</CODE> to reset the display system. <P>The following subsections explore <B>S-Lang</B>'s screen management system in greater detail. <P> <H2><A NAME="ss5.1">5.1 Initialization</A> </H2> <P> <P>The function <CODE>SLsmg_init_smg</CODE> must be called before any other <CODE>SLsmg</CODE> function can be used. It has the simple prototype: <BLOCKQUOTE><CODE> <PRE> int SLsmg_init_smg (void); </PRE> </CODE></BLOCKQUOTE> It returns zero if successful or -1 if it cannot allocate space for the virtual display. <P>For this routine to properly initialize the virtual display, the capabilities of the terminal must be known as well as the size of the <EM>physical</EM> display. For these reasons, the lower level <CODE>SLtt</CODE> routines come into play. In particular, before the first call to <CODE>SLsmg_init_smg</CODE>, the application is required to call the function <CODE>SLtt_get_terminfo</CODE> before calling <CODE>SLsmg_init_smg</CODE>. <P>The <CODE>SLtt_get_terminfo</CODE> function sets the global variables <CODE>SLtt_Screen_Rows</CODE> and <CODE>SLtt_Screen_Cols</CODE> to the values appropriate for the terminal. It does this by calling the <CODE>SLtt_get_screen_size</CODE> function to query the terminal driver for the appropriate values for these variables. From this point on, it is up to the application to maintain the correct values for these variables by calling the <CODE>SLtt_get_screen_size</CODE> function whenever the display size changes, e.g., in response to a <CODE>SIGWINCH</CODE> signal. Finally, if the application is going to read characters from the keyboard, it is also a good idea to initialize the keyboard routines at this point as well. <P> <H2><A NAME="ss5.2">5.2 Resetting SLsmg</A> </H2> <P> <P>Before the program exits or suspends, the function <CODE>SLsmg_reset_tty</CODE> should be called to shutdown the display system. This function has the prototype <BLOCKQUOTE><CODE> <PRE> void SLsmg_reset_smg (void); </PRE> </CODE></BLOCKQUOTE> This will deallocate any memory allocated for the virtual screen and reset the terminal's display. <P>Basically, a program that uses the <CODE>SLsmg</CODE> screen management functions and <B>S-Lang</B>'s keyboard interface will look something like: <BLOCKQUOTE><CODE> <PRE> #include "slang.h" int main () { SLtt_get_terminfo (); SLang_init_tty (-1, 0, 0); SLsmg_init_smg (); /* do stuff .... */ SLsmg_reset_smg (); SLang_reset_tty (); return 0; } </PRE> </CODE></BLOCKQUOTE> If this program is compiled and run, all it will do is clear the screen and position the cursor at the bottom of the display. In the following sections, other <CODE>SLsmg</CODE> functions will be introduced which may be used to make this simple program do much more. <P> <H2><A NAME="ss5.3">5.3 Handling Screen Resize Events</A> </H2> <P>The function <CODE>SLsmg_reinit_smg</CODE> is designed to be used in conjunction with resize events. <P>Under Unix-like operating systems, when the size of the display changes, the application will be sent a <CODE>SIGWINCH</CODE> signal. To properly handle this signal, the <CODE>SLsmg</CODE> routines must be reinitialized to use the new display size. This may be accomplished by calling <CODE>SLtt_get_screen_size</CODE> to get the new size, followed by <CODE>SLsmg_reinit_smg</CODE> to reinitialize the <CODE>SLsmg</CODE> interface to use the new size. Keep in mind that these routines should not be called from within the signal handler. The following code illustrates the main ideas involved in handling such events: <BLOCKQUOTE><CODE> <PRE> static volatile int Screen_Size_Changed; static sigwinch_handler (int sig) { Screen_Size_Changed = 1; SLsignal (SIGWINCH, sigwinch_handler); } int main (int argc, char **argv) { SLsignal (SIGWINCH, sigwinch_handler); SLsmg_init_smg (); . . /* Now enter main loop */ while (not_done) { if (Screen_Size_Changed) { SLtt_get_screen_size (); SLsmg_reinit_smg (); redraw_display (); } . . } return 0; } </PRE> </CODE></BLOCKQUOTE> <P> <P> <H2><A NAME="ss5.4">5.4 SLsmg Functions</A> </H2> <P> <P>In the previous sections, functions for initializing and shutting down the <CODE>SLsmg</CODE> routines were discussed. In this section, the rest of the <CODE>SLsmg</CODE> functions are presented. These functions act only on the <EM>virtual</EM> display. The <EM>physical</EM> display is updated when the <CODE>SLsmg_refresh</CODE> function is called and <EM>not until that time</EM>. This function has the simple prototype: <BLOCKQUOTE><CODE> <PRE> void SLsmg_refresh (void); </PRE> </CODE></BLOCKQUOTE> <P> <H3>Positioning the cursor</H3> <P> <P>The <CODE>SLsmg_gotorc</CODE> function is used to position the cursor at a given row and column. The prototype for this function is: <BLOCKQUOTE><CODE> <PRE> void SLsmg_gotorc (int row, int col); </PRE> </CODE></BLOCKQUOTE> The origin of the screen is at the top left corner and is given the coordinate (0, 0), i.e., the top row of the screen corresponds to <CODE>row = 0</CODE> and the first column corresponds to <CODE>col = 0</CODE>. The last row of the screen is given by <CODE>row = SLtt_Screen_Rows - 1</CODE>. <P>It is possible to change the origin of the coordinate system by using the function <CODE>SLsmg_set_screen_start</CODE> with prototype: <BLOCKQUOTE><CODE> <PRE> void SLsmg_set_screen_start (int *r, int *c); </PRE> </CODE></BLOCKQUOTE> This function takes pointers to the new values of the first row and first column. It returns the previous values by modifying the values of the integers at the addresses specified by the parameter list. A <CODE>NULL</CODE> pointer may be passed to indicate that the origin is to be set to its initial value of 0. For example, <BLOCKQUOTE><CODE> <PRE> int r = 10; SLsmg_set_screen_start (&r, NULL); </PRE> </CODE></BLOCKQUOTE> sets the origin to (10, 0) and after the function returns, the variable <CODE>r</CODE> will have the value of the previous row origin. <P> <H3>Writing to the Display</H3> <P> <P><CODE>SLsmg</CODE> has several routines for outputting text to the virtual display. The following points should be understood: <UL> <LI> The text is output at the position of the cursor of the virtual display and the cursor is advanced to the position that corresponds to the end of the text. </LI> <LI> Text does <EM>not</EM> wrap at the boundary of the display--- it is trucated. This behavior seems to be more useful in practice since most programs that would use screen management tend to be line oriented. </LI> <LI> Control characters are displayed in a two character sequence representation with <CODE>^</CODE> as the first character. That is, <CODE>Ctrl-X</CODE> is output as <CODE>^X</CODE>. </LI> <LI> The newline character does <EM>not</EM> cause the cursor to advance to the next row. Instead, when a newline character is encountered when outputting text, the output routine will return. That is, outputting a string containing a newline character will only display the contents of the string up to the newline character.</LI> </UL> <P>Although the some of the above items might appear to be too restrictive, in practice this is not seem to be the case. In fact, the design of the output routines was influenced by their actual use and modified to simplify the code of the application utilizing them. <P><CODE>void SLsmg_write_char (char ch);</CODE> Write a single character to the virtual display. <P><CODE>void SLsmg_write_nchars (char *str, int len);</CODE> Write <CODE>len</CODE> characters pointed to by <CODE>str</CODE> to the virtual display. <P><CODE>void SLsmg_write_string (char *str);</CODE> Write the null terminated string given by pointer <CODE>str</CODE> to the virtual display. This function is a wrapper around <CODE>SLsmg_write_nchars</CODE>. <P><CODE>void SLsmg_write_nstring (char *str, int n);</CODE> Write the null terminated string given by pointer <CODE>str</CODE> to the virtual display. At most, only <CODE>n</CODE> characters are written. If the length of the string is less than <CODE>n</CODE>, then the string will be padded with blanks. This function is a wrapper around <CODE>SLsmg_write_nchars</CODE>. <P><CODE>void SLsmg_printf (char *fmt, ...);</CODE> This function is similar to <CODE>printf</CODE> except that it writes to the <CODE>SLsmg</CODE> virtual display. <P><CODE>void SLsmg_vprintf (char *, va_list);</CODE> Like <CODE>SLsmg_printf</CODE> but uses a variable argument list. <P> <H3>Erasing the Display</H3> <P> <P>The following functions may be used to fill portions of the display with blank characters. The attributes of blank character are the current attributes. (See below for a discussion of character attributes) <P><CODE>void SLsmg_erase_eol (void);</CODE> Erase line from current position to the end of the line. <P><CODE>void SLsmg_erase_eos (void);</CODE> Erase from the current position to the end of the screen. <P><CODE>void SLsmg_cls (void);</CODE> Clear the entire virtual display. <P> <H3>Setting Character Attributes</H3> <P> <P>Character attributes define the visual characteristics the character possesses when it is displayed. Visual characteristics include the foreground and background colors as well as other attributes such as blinking, bold, and so on. Since <CODE>SLsmg</CODE> takes a different approach to this problem than other screen management libraries an explanation of this approach is given here. This approach has been motivated by experience with programs that require some sort of screen management. <P>Most programs that use <CODE>SLsmg</CODE> are composed of specific textual objects or objects made up of line drawing characters. For example, consider an application with a menu bar with drop down menus. The menus might be enclosed by some sort of frame or perhaps a shadow. The basic idea is to associate an integer to each of the objects (e.g., menu bar, shadow, current menu item, etc.) and create a mapping from the integer to the set of attributes. In the terminology of <CODE>SLsmg</CODE>, the integer is simply called an <EM>object</EM>. <P>For example, the menu bar might be associated with the object <CODE>1</CODE>, the drop down menu could be object <CODE>2</CODE>, the shadow could be object <CODE>3</CODE>, and so on. <P>The range of values for the object integer is restricted from 0 up to and including 255 on all systems except MSDOS where the maximum allowed integer is 15 <BLOCKQUOTE>This difference is due to memory constraints imposed by MSDOS. This restriction might be removed in a future version of the library.</BLOCKQUOTE> . The object numbered zero should not be regarding as an object at all. Rather it should be regarded as all <EM>other</EM> objects that have not explicitly been given an object number. <CODE>SLsmg</CODE>, or more precisely <CODE>SLtt</CODE>, refers to the attributes of this special object as the <EM>default</EM> or <EM>normal</EM> attributes. <P>The <CODE>SLsmg</CODE> routines know nothing about the mapping of the color to the attributes associated with the color. The actual mapping takes place at a lower level in the <CODE>SLtt</CODE> routines. Hence, to map an object to the actual set of attributes requires a call to any of the following <CODE>SLtt</CODE> routines: <BLOCKQUOTE><CODE> <PRE> void SLtt_set_color (int obj, char *name, char *fg, char *bg); void SLtt_set_color_object (int obj, SLtt_Char_Type attr); void SLtt_set_mono (int obj, char *, SLtt_Char_Type attr); </PRE> </CODE></BLOCKQUOTE> Only the first of these routines will be discussed briefly here. The latter two functions allow more fine control over the object to attribute mapping (such as assigning a ``blink'' attribute to the object). For a more full explanation on all of these routines see the section about the <CODE>SLtt</CODE> interface. <P>The <CODE>SLtt_set_color</CODE> function takes four parameters. The first parameter, <CODE>obj</CODE>, is simply the integer of the object for which attributes are to be assigned. The second parameter is currently unused by these routines. The third and forth parameters, <CODE>fg</CODE> and <CODE>bg</CODE>, are the names of the foreground and background color to be used associated with the object. The strings that one can use for the third and fourth parameters can be any one of the 16 colors: <BLOCKQUOTE><CODE> <PRE> "black" "gray" "red" "brightred" "green" "brightgreen" "brown" "yellow" "blue" "brightblue" "magenta" "brightmagenta" "cyan" "brightcyan" "lightgray" "white" </PRE> </CODE></BLOCKQUOTE> The value of the foreground parameter <CODE>fg</CODE> can be anyone of these sixteen colors. However, on most terminals, the background color will can only be one of the colors listed in the first column <BLOCKQUOTE>This is also true on the Linux console. However, it need not be the case and hopefully the designers of Linux will someday remove this restriction.</BLOCKQUOTE> . <P>Of course not all terminals are color terminals. If the <B>S-Lang</B> global variable <CODE>SLtt_Use_Ansi_Colors</CODE> is non-zero, the terminal is assumed to be a color terminal. The <CODE>SLtt_get_terminfo</CODE> will try to determine whether or not the terminal supports colors and set this variable accordingly. It does this by looking for the capability in the terminfo/termcap database. Unfortunately many Unix databases lack this information and so the <CODE>SLtt_get_terminfo</CODE> routine will check whether or not the environment variable <CODE>COLORTERM</CODE> exists. If it exists, the terminal will be assumed to support ANSI colors and <CODE>SLtt_Use_Ansi_Colors</CODE> will be set to one. Nevertheless, the application should provide some other mechanism to set this variable, e.g., via a command line parameter. <P>When the <CODE>SLtt_Use_Ansi_Colors</CODE> variable is zero, all objects with numbers greater than one will be displayed in inverse video <BLOCKQUOTE>This behavior can be modified by using the <CODE>SLtt_set_mono</CODE> function call.</BLOCKQUOTE> . <P>With this background, the <CODE>SLsmg</CODE> functions for setting the character attributes can now be defined. These functions simply set the object attributes that are to be assigned to <EM>subsequent</EM> characters written to the virtual display. For this reason, the new attribute is called the <EM>current</EM> attribute. <P><CODE>void SLsmg_set_color (int obj);</CODE> Set the current attribute to those of object <CODE>obj</CODE>. <P><CODE>void SLsmg_normal_video (void);</CODE> This function is equivalent to <CODE>SLsmg_set_color (0)</CODE>. <P><CODE>void SLsmg_reverse_video (void);</CODE> This function is equivalent to <CODE>SLsmg_set_color (1)</CODE>. On monochrome terminals, it is equivalent to setting the subsequent character attributes to inverse video. <P>Unfortunately there does not seem to be a standard way for the application or, in particular, the library to determine which color will be used by the terminal for the default background. Such information would be useful in initializing the foreground and background colors associated with the default color object (0). FOr this reason, it is up to the application to provide some means for the user to indicate what these colors are for the particular terminal setup. To facilitate this, the <CODE>SLtt_get_terminfo</CODE> function checks for the existence of the <CODE>COLORFGBG</CODE> environment variable. If this variable exists, its value will be used to initialize the colors associated with the default color object. Specifically, the value is assumed to consist of a foreground color name and a background color name separated by a semicolon. For example, if the value of <CODE>COLORTERM</CODE> is <CODE>lightgray;blue</CODE>, the default color object will be initialized to represent a <CODE>lightgray</CODE> foreground upon a <CODE>blue</CODE> background. <P> <H3>Lines and Alternate Character Sets</H3> <P>The <B>S-Lang</B> screen management library also includes routines for turning on and turning off alternate character sets. This is especially useful for drawing horizontal and vertical lines. <P><CODE>void SLsmg_set_char_set (int flag);</CODE> If <CODE>flag</CODE> is non-zero, subsequent write functions will use characters from the alternate character set. If <CODE>flag</CODE> is zero, the default, or, ordinary character set will be used. <P><CODE>void SLsmg_draw_hline (int len);</CODE> Draw a horizontal line from the current position to the column that is <CODE>len</CODE> characters to the right. <P><CODE>void SLsmg_draw_vline (int len);</CODE> Draw a horizontal line from the current position to the row that is <CODE>len</CODE> rows below. <P><CODE>void SLsmg_draw_box (int r, int c, int dr, int dc);</CODE> Draw a box whose upper right corner is at row <CODE>r</CODE> and column <CODE>c</CODE>. The box spans <CODE>dr</CODE> rows and <CODE>dc</CODE> columns. The current position will be left at row <CODE>r</CODE> and column <CODE>c</CODE>. <P> <H3>Miscellaneous Functions</H3> <P> <P><CODE>void SLsmg_touch_lines (int r, int n);</CODE> Mark screen rows numbered <CODE>r</CODE>, <CODE>r + 1</CODE>, ... <CODE>r + (n - 1)</CODE> as modified. When <CODE>SLsmg_refresh</CODE> is called, these rows will be completely redrawn. <P><CODE>unsigned short SLsmg_char_at(void);</CODE> Returns the character and its attributes object number at the current cursor position. The character itself occupies the lower byte and the object attributes number forms the upper byte. The object returned by this function call should not be written back out via any of the functions that write characters or character strings. <P> <P> <P> <H2><A NAME="ss5.5">5.5 Variables</A> </H2> <P> <P>The following <B>S-Lang</B> global variables are used by the <CODE>SLsmg</CODE> interface. Some of these have been previously discussed. <P><CODE>int SLtt_Screen_Rows;</CODE> <CODE>int SLtt_Screen_Cols;</CODE> The number of rows and columns of the <EM>physical</EM> display. If either of these numbers changes, the functions <CODE>SLsmg_reset_smg</CODE> and <CODE>SLsmg_init_smg</CODE> should be called again so that the <CODE>SLsmg</CODE> routines can re-adjust to the new size. <P><CODE>int SLsmg_Tab_Width;</CODE> Set this variable to the tab width that will be used when expanding tab characters. The default is 8. <P><CODE>int SLsmg_Display_Eight_Bit</CODE> This variable determines how characters with the high bit set are to be output. Specifically, a character with the high bit set with a value greater than or equal to this value is output as is; otherwise, it will be output in a 7-bit representation. The default value for this variable is <CODE>128</CODE> for MSDOS and <CODE>160</CODE> for other systems (ISO-Latin). <P><CODE>int SLtt_Use_Ansi_Colors;</CODE> If this value is non-zero, the terminal is assumed to support ANSI colors otherwise it is assumed to be monochrome. The default is 0. <P><CODE>int SLtt_Term_Cannot_Scroll;</CODE> If this value is zero, the <CODE>SLsmg</CODE> will attempt to scroll the physical display to optimize the update. If it is non-zero, the screen management routines will not perform this optimization. For some applications, this variable should be set to zero. The default value is set by the <CODE>SLtt_get_terminfo</CODE> function. <P> <P> <H2><A NAME="ss5.6">5.6 Hints for using SLsmg</A> </H2> <P> <P>This section discusses some general design issues that one must face when writing an application that requires some sort of screen management. <P> <P> <HR> <A HREF="cslang-6.html">Next</A> <A HREF="cslang-4.html">Previous</A> <A HREF="cslang.html#toc5">Contents</A> </BODY> </HTML>