Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Terminal Handling with S-Lang

It encapsulates all the low-level terminal handling


through a set of routines that allow direct access to
the video terminal and automatically handle
scrolling and color.
S-Lang's terminal handling abilities two categories
1. set of functions for reading keystrokes from the
terminal in a controlled manner.
2. routines for full-screen output to the terminal.
Commands to install slang (only root can install)
sudo apt-get update
sudo apt-get install slang2_2.2.2
(slangversion)
Compile
gcc –slang program.c
Initializing S-Lang Input Handling

int SLang_init_tty(int abort_char, int flow_ctrl, int


opost);
abort_char - If -1 is passed, the current tty interrupt
character (usually, Ctrl-C) is retained; otherwise , the
interrupt character is set to the value passed.
Terminal-level flow control allows the user to pause
output to the terminal to prevent scrolling and then
restart it.
opost - output post processing on the terminal.
Restoring the Terminal State
SLang_reset_tty()
Reading Characters from the Terminal
SLang_getkey() - returns a single character from the
terminal.
Checking for Pending Input

To check for available characters without blocking.


int SLang_input_pending(int timeout);
SLang_input_pending() returns true if characters become
available within n tenths of seconds. It returns as soon as
the characters are available; it returns false if no characters
become available within the timeout period.
If a timeout period of zero is given, SLang_input_pending()
tells whether characters are currently available.
while (ch != 'q' && SLang_input_pending(20))
The program now waits a maximum of two seconds for more
input. Once two seconds pass without any input, it exits.
Output Handling

two sets
1. terminal-handling functions (the SLtt family)
2. higher-level screen management functions (the SLsmg
family) ---- string output, line drawing, and screen-
querying functions
 SLtt function family works directly with the
terminal; functions that map closely with capabilities
defined in the terminal database.
 Includes routines for defining foreground and
background color pairs and turning the cursor on and
off.
Initializing Screen Management

void SLtt_get_terminfo(void); - to look up the current


terminal
SLtt_get_terminfo() is to set the physical size of the
screen to the size listed in the terminal database.
The number of rows and columns on the terminal
are stored in SLtt_Screen_Rows and
SLtt_Screen_Cols , respectively.
Initializing the S-Lang's screen management layer is
straightforward:
void SLsmg_init_smg(void);
SLsmg_init_smg()
Updating the Display

SLsmg_refresh()
updates the physical terminal with the results of any
screen drawing that has been done since the previous
time it was called.
Moving the Cursor

extern void SLsmg_gotorc (int row, int column);

upper-left corner of the screen is (0, 0)


bottom-right corner is (SLtt_Screen_Rows - 1,
SLtt_Screen_Cols - 1) .
void SLsmg_set_char_set(int useAlternate)
nonzero argument - new characters written to the display
are mapped through the alternate character set.
Zero - disables this mapping, allowing characters to appear
normally.

SLSMG_HLINE_CHAR

SLSMG_VLINE_CHAR

SLSMG_ULCORN_CHAR
Writing to the Screen

1. void SLsmg_write_char(char ch); - writes the


character
2. void SLsmg_write_string(char * str); - writes the
string
3. void SLsmg_write_nchars(char * chars, int length); -
The length characters pointed to by chars are written to
the screen.
4. void SLsmg_write_nstring(char * str, int length);
5. void SLsmg_printf(char * format, ...);
6. void void SLsmg_write_wrapped_string(char * str, int
row, int column, int height, int width, int fill);
Drawing Lines and Boxes

1. void SLsmg_draw_hline(int row);


2. void SLsmg_draw_vline(int column);
3. void SLsmg_draw_box(int row, int column, int
height, int width);
Using Color

SLtt_set_color()
void SLtt_set_color(int entry, char * name, char * fg,
char * bg);
ioctl - control device
#include <sys/ioctl.h>
int ioctl(int d, int request, ...);
The ioctl() function manipulates the underlying device
parameters of special files.
terminals controlled with ioctl() requests.
The argument d must be an open file descriptor.
The second argument is a device-dependent request code.
The third argument is an untyped pointer to memory.
TIOCGWINSZ - Fill in the winsize structure pointed to by the
third argument with the screen width and height.
7.#include <slang/slang.h>
8: int main(void) {
 9: struct winsize ws;
 10:
 11: /* get the size of the terminal connected to stdout */
 12: if (ioctl(1, TIOCGWINSZ, &ws)) {
 13: perror("failed to get window size");
 14: return 1;
 15: }
 16:
 17: SLtt_get_terminfo();
 18:
 19: SLtt_Screen_Rows = ws.ws_row;
 20: SLtt_Screen_Cols = ws.ws_col;
 21:
 22: SLsmg_init_smg();
 23:
 24: /* heart of the program goes here */
 25:
 26: SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
 27: SLsmg_refresh();
 28: SLsmg_reset_smg();
 29: SLang_reset_tty();
 30:
 31: return 0;

You might also like