Download Dynamic Visualizations - Advanced System Programming - Lecture Slides and more Slides Computer Applications in PDF only on Docsity! Dynamic visualizations On ‘non-canonical’ keyboard-input and terminal escape-sequences for visualization effects Docsity.com Our course’s theme Using the computer to study the computer Docsity.com The ‘tty’ interface • ‘tty’ is an acronyn for ‘TeleTYpe’ terminal • Such devices have a keyboard and screen • Behavior emulates technology from 1950s • Usually a tty operates in ‘canonical’ mode: – Each user-keystroke is ‘echoed’ to screen – Some editing is allowed (e.g., backspace) – The keyboard-input is internally buffered – The <ENTER>-key signals an ‘end-of-line’ – Programs receive input one-line-at-a-time Docsity.com ‘tty’ customization • Sometimes canonical mode isn’t suitable (an example: animated computer games) • The terminal’s behavior can be modified! • UNIX provides a convenient interface: – #include <termios.h> – struct termios tty; – int tcgetattr( int fd, struct termios *tty ); – int tcsetattr( int fd, int flag, struct termios *tty ); Docsity.com How does the ‘tty’ work? TeleTYpe display device HARDWARE SOFTWARE application tty_driver c_lflag input handling c_iflag c_cc output handling c_oflag terminal_driver c_cflag User space Kernel space struct tty { c_iflag; c_oflag; c_cflag; c_lflag; c_line; c_cc[ ]; }; Docsity.com The ‘c_cc[ ]’ array • ‘struct termios’ objects include an array • The array-indices have symbolic names • Symbol-names are standardized in UNIX • Array entries are ‘tty’ operating parameters • Two useful ones for our purposes are: tty.c_cc[ VMIN ] and tty.c_cc[ VTIME ] Docsity.com How to setup ‘raw’ terminal-mode • Step 1: Use ‘tcgetattr()’ to get a copy of the current tty’s ‘struct termios’ settings • Step 2: Make a working copy of that object • Step 3: Modify its flags and control-codes • Step 4: Use ‘tcsetattr()’ to install changes • Step 5: Perform desired ‘raw’ mode input • Step 6: Use ‘tcsetattr()’ to restore the terminal to its original default settings Docsity.com Input-mode needs five settings • tty.c_cc[ VMIN ] = 0; – so the ‘read()’ function will return -- even if there is not at least one new input-character available • tty.c_cc[ VTIME ] = 0; – so there will be no time-delay, after each new key pressed, until the ‘read()’ function returns • tty.c_lflag &= ~ECHO; // no input-echoing • tty.c_lflag &= ~ICANON; // no buffering • tty.c_lflag &= ~ISIG; // no <CTRL>-C Docsity.com ANSI command-sequences A look at some terminal emulation features utilized in the “console- redirection” mechanism Docsity.com Clearing the screen • Here is an ANSI command-sequence that clears the terminal’s display-screen: char cmd[] = “\033[2J”; int len = strlen( cmd ); write( 1, cmd, len ); Docsity.com Reposition the cursor • Here is an ANSI command-sequence that moves the cursor to row 12, column 40: char cmd[] = “\033[12;40H”; int len = strlen( cmd ); write( 1, cmd, len ); Docsity.com Cursor visibility commands • Here are ANSI command-sequences that will ‘hide’ or ‘show’ the terminal’s cursor: char hide[] = “\033[?25l”; // lowercase L char show[] = “\033[?25h”; // lowercase H Docsity.com In-class exercise #1 • Modify this simple C++ program so that it will print its “Hello” message in colors and be located in the center of the screen: #include <stdio.h> int main( void ) { printf( “Hello, world! \n” ); } Docsity.com In-class exercise #2 • Compile and install our ‘pkmaps.c’ module • Then download, compile and execute our ‘mapwatch.cpp’ visualization-application • While ‘mapwatch’ continues to run in one window of your graphical desktop, open a second window nearby and execute some common commands, for example: $ ls $ mmake pkmaps Docsity.com