Entering raw mode
Entering Raw Mode in C
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
int main() {
struct termios orig_termios;
tcgetattr(STDIN_FILENO, &orig_termios);
struct termios raw = orig_termios;
cfmakeraw(&raw);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q');
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
return 0;
}
- Include Necessary Libraries:
Include the necessary libraries for handling terminal input and output in C.
Main Function:
Define the main function to contain the program logic.
Retrieve Original Terminal Attributes:
Retrieve the original terminal attributes using
tcgetattr
and store them in theorig_termios
struct.Create Raw Terminal Attributes:
Create a copy of the original terminal attributes and modify them to create raw mode using
cfmakeraw
.Set Raw Terminal Attributes:
Set the raw terminal attributes using
tcsetattr
with theTCSAFLUSH
option to apply the changes immediately.Read Input in Raw Mode:
Read input character by character in a loop using
read
until the character 'q' is entered.Restore Original Terminal Attributes:
Restore the original terminal attributes using
tcsetattr
to revert the terminal to its original mode.Return:
- Return 0 to indicate successful execution.