From 412e69f85f2c5627453368d2fcc9ecb9cba65c18 Mon Sep 17 00:00:00 2001 From: yutsuo Date: Wed, 8 Dec 2021 21:37:39 -0300 Subject: [PATCH] first commit --- .gitignore | 4 + bool-test.c | 12 +++ cursor-test.c | 123 ++++++++++++++++++++++++++ detect-key.c | 27 ++++++ features-showcase.c | 182 +++++++++++++++++++++++++++++++++++++++ kbhitzzor.c | 30 +++++++ key-reader.c | 45 ++++++++++ main.c | 38 ++++++++ sgr-terminal-sequences.c | 37 ++++++++ 9 files changed, 498 insertions(+) create mode 100644 .gitignore create mode 100644 bool-test.c create mode 100644 cursor-test.c create mode 100644 detect-key.c create mode 100644 features-showcase.c create mode 100644 kbhitzzor.c create mode 100644 key-reader.c create mode 100644 main.c create mode 100644 sgr-terminal-sequences.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ad08b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode +*.obj +*.pdb +*.exe \ No newline at end of file diff --git a/bool-test.c b/bool-test.c new file mode 100644 index 0000000..38633c6 --- /dev/null +++ b/bool-test.c @@ -0,0 +1,12 @@ +#include +#include + +int main(int argc, char const *argv[]) +{ + bool mark[2] = {'X','O'}; + int test = mark; + printf("mark: %c", test); + printf("\nnot-mark: %c", !test); + + return 0; +} diff --git a/cursor-test.c b/cursor-test.c new file mode 100644 index 0000000..335a0f7 --- /dev/null +++ b/cursor-test.c @@ -0,0 +1,123 @@ +// System headers +#include + +// Standard library C-style +#include +#include +#include +#include +#include + +#define ESC "\x1b" +#define CSI "\x1b[" + +bool EnableVTMode() +{ + // Set output mode to handle virtual terminal sequences + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) + { + return false; + } + + DWORD dwMode = 0; + if (!GetConsoleMode(hOut, &dwMode)) + { + return false; + } + + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(hOut, dwMode)) + { + return false; + } + return true; +} + +void gibberish() +{ + system("cls"); + printf(CSI "1;32m"); + printf("HELLO THERE\n"); + printf("WHAT IF\n"); + printf("YOU COULD\n"); + printf("MOVE THE CURSOR\n"); +} + +int get_key() +{ + int c = getch(); + switch (c) + { + case 0: + return getch() + 256; + case 224: + return getch() + 512; + } + return c; +} + +void printGrid() +{ + printf("%s", " _ _ _ \n"); + printf("%s", "|_|_|_|\n"); + printf("%s", "|_|_|_|\n"); + printf("%s", "|_|_|_|\n"); +} + +// int main() +// { +// system("clear"); +// printf(CSI "1;32m"); +// printf("HELLO THERE\n"); +// printf("WHAT IF\n"); +// printf("YOU COULD\n"); +// printf("MOVE THE CURSOR\n"); +// // printf(CSI "2;2H"); +// printf(CSI "1C"); +// _getch(); +// } + +// ┌─┬─┬─┐ +// │ +// └─┴─┴─┘ + +int main(int argc, char const *argv[]) +{ + EnableVTMode(); + system("clear"); + // gibberish(); + printGrid(); + printf(CSI "2;2H"); + + int key = 0; + for (;;) + { + if (kbhit()) + { + key = get_key(); + switch (key) + { + case 584: + printf(CSI "1A"); //up + break; + case 592: + printf(CSI "1B"); //down + break; + case 589: + printf(CSI "1C"); //right + break; + case 587: + printf(CSI "1D"); //left + break; + case 32: + printf("X"); + break; + + default: + break; + } + } + } + return 0; +} diff --git a/detect-key.c b/detect-key.c new file mode 100644 index 0000000..9398d6b --- /dev/null +++ b/detect-key.c @@ -0,0 +1,27 @@ +#include + +int main() +{ + char ch; + //infinite loop + while(1) + { + printf("Enter any character: "); + //read a single character + ch=fgetc(stdin); + + if(ch==0x0A) + { + printf("ENTER KEY is pressed.\n"); + break; + } + else + { + printf("%c is pressed.\n",ch); + } + //read dummy character to clear + //input buffer, which inserts after character input + ch=getchar(); + } + return 0; +} diff --git a/features-showcase.c b/features-showcase.c new file mode 100644 index 0000000..5fbe358 --- /dev/null +++ b/features-showcase.c @@ -0,0 +1,182 @@ +// System headers +#include + +// Standard library C-style +#include +#include +#include +#include + +#define ESC "\x1b" +#define CSI "\x1b[" + +bool EnableVTMode() +{ + // Set output mode to handle virtual terminal sequences + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) + { + return false; + } + + DWORD dwMode = 0; + if (!GetConsoleMode(hOut, &dwMode)) + { + return false; + } + + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(hOut, dwMode)) + { + return false; + } + return true; +} + +void PrintVerticalBorder() +{ + printf(ESC "(0"); // Enter Line drawing mode + printf(CSI "104;93m"); // bright yellow on bright blue + printf("x"); // in line drawing mode, \x78 -> \u2502 "Vertical Bar" + printf(CSI "0m"); // restore color + printf(ESC "(B"); // exit line drawing mode +} + +void PrintHorizontalBorder(COORD const Size, bool fIsTop) +{ + printf(ESC "(0"); // Enter Line drawing mode + printf(CSI "104;93m"); // Make the border bright yellow on bright blue + printf(fIsTop ? "l" : "m"); // print left corner + + for (int i = 1; i < Size.X - 1; i++) + printf("q"); // in line drawing mode, \x71 -> \u2500 "HORIZONTAL SCAN LINE-5" + + printf(fIsTop ? "k" : "j"); // print right corner + printf(CSI "0m"); + printf(ESC "(B"); // exit line drawing mode +} + +void PrintStatusLine(const char* const pszMessage, COORD const Size) +{ + printf(CSI "%d;1H", Size.Y); + printf(CSI "K"); // clear the line + printf(pszMessage); +} + +int __cdecl wmain(int argc, WCHAR* argv[]) +{ + argc; // unused + argv; // unused + //First, enable VT mode + bool fSuccess = EnableVTMode(); + if (!fSuccess) + { + printf("Unable to enter VT processing mode. Quitting.\n"); + return -1; + } + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) + { + printf("Couldn't get the console handle. Quitting.\n"); + return -1; + } + + CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo; + GetConsoleScreenBufferInfo(hOut, &ScreenBufferInfo); + COORD Size; + Size.X = ScreenBufferInfo.srWindow.Right - ScreenBufferInfo.srWindow.Left + 1; + Size.Y = ScreenBufferInfo.srWindow.Bottom - ScreenBufferInfo.srWindow.Top + 1; + + // Enter the alternate buffer + printf(CSI "?1049h"); + + // Clear screen, tab stops, set, stop at columns 16, 32 + printf(CSI "1;1H"); + printf(CSI "2J"); // Clear screen + + int iNumTabStops = 4; // (0, 20, 40, width) + printf(CSI "3g"); // clear all tab stops + printf(CSI "1;20H"); // Move to column 20 + printf(ESC "H"); // set a tab stop + + printf(CSI "1;40H"); // Move to column 40 + printf(ESC "H"); // set a tab stop + + // Set scrolling margins to 3, h-2 + printf(CSI "3;%dr", Size.Y - 2); + int iNumLines = Size.Y - 4; + + printf(CSI "1;1H"); + printf(CSI "102;30m"); + printf("Windows 10 Anniversary Update - VT Example"); + printf(CSI "0m"); + + // Print a top border - Yellow + printf(CSI "2;1H"); + PrintHorizontalBorder(Size, true); + + // // Print a bottom border + printf(CSI "%d;1H", Size.Y - 1); + PrintHorizontalBorder(Size, false); + + wchar_t wch; + + // draw columns + printf(CSI "3;1H"); + int line = 0; + for (line = 0; line < iNumLines * iNumTabStops; line++) + { + PrintVerticalBorder(); + if (line + 1 != iNumLines * iNumTabStops) // don't advance to next line if this is the last line + printf("\t"); // advance to next tab stop + + } + + PrintStatusLine("Press any key to see text printed between tab stops.", Size); + wch = _getwch(); + + // Fill columns with output + printf(CSI "3;1H"); + for (line = 0; line < iNumLines; line++) + { + int tab = 0; + for (tab = 0; tab < iNumTabStops - 1; tab++) + { + PrintVerticalBorder(); + printf("line=%d", line); + printf("\t"); // advance to next tab stop + } + PrintVerticalBorder();// print border at right side + if (line + 1 != iNumLines) + printf("\t"); // advance to next tab stop, (on the next line) + } + + PrintStatusLine("Press any key to demonstrate scroll margins", Size); + wch = _getwch(); + + printf(CSI "3;1H"); + for (line = 0; line < iNumLines * 2; line++) + { + printf(CSI "K"); // clear the line + int tab = 0; + for (tab = 0; tab < iNumTabStops - 1; tab++) + { + PrintVerticalBorder(); + printf("line=%d", line); + printf("\t"); // advance to next tab stop + } + PrintVerticalBorder(); // print border at right side + if (line + 1 != iNumLines * 2) + { + printf("\n"); //Advance to next line. If we're at the bottom of the margins, the text will scroll. + printf("\r"); //return to first col in buffer + } + } + + PrintStatusLine("Press any key to exit", Size); + wch = _getwch(); + + // Exit the alternate buffer + printf(CSI "?1049l"); + +} \ No newline at end of file diff --git a/kbhitzzor.c b/kbhitzzor.c new file mode 100644 index 0000000..87393e7 --- /dev/null +++ b/kbhitzzor.c @@ -0,0 +1,30 @@ +#include +#include + +int notmain() +{ + char c='N'; //N is assigned to c + + for(;;) //infinite loop + { + printf("%c\t",c); //N is printed over and over + if(_kbhit()) //until a key is hit, the keyvalue is stored in the buffer + { + c=_getch(); //c is fed the value from the buffer. The program does not pause here + printf("%c",c); //print the key + break; //break the loop + } + } + _getch(); //yes, the program does pause here. + return 0; +} + +int main() +{ + /* Display message until key is pressed. */ + while( !_kbhit() ) + _cputs( "Hit me!! " ); + + /* Use _getch to throw key away. */ + printf( "\nKey struck was '%c'\n", _getch() ); +} \ No newline at end of file diff --git a/key-reader.c b/key-reader.c new file mode 100644 index 0000000..8d276b8 --- /dev/null +++ b/key-reader.c @@ -0,0 +1,45 @@ +#include +#include + +int get_key() +{ + int c=getch(); + switch(c) + { + case 0: return getch()+256; + case 224: return getch()+512; + } + return c; +} + +int main(int argc, char const *argv[]) +{ + int key = 0; + for(;;) + { + if (kbhit()) + { + key = get_key(); + printf("\n%d", key); + switch (key) + { + case 584: + printf("\n↑"); + break; + case 592: + printf("\n↓"); + break; + case 587: + printf("\n←"); + break; + case 589: + printf("\n→"); + break; + + default: + break; + } + } + } + return 0; +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..bc2ab6c --- /dev/null +++ b/main.c @@ -0,0 +1,38 @@ +#include + +int charFunctions() +{ + char c; + + printf("Enter character: "); + c = getchar(); + + printf("Character entered: "); + if (c == '\n') + printf("\\n\n"); + else + putchar(c); + printf("\n"); + + return (0); +} + +int renderKinda() +{ + char ender = 0; + while (!ender) + { + printf("%s", " _ _ _ \n"); + printf("%s", "|_|_|_|\n"); + printf("%s", "|_|_|_|\n"); + printf("%s", "|_|_|_|\n"); + printf("press ENTER to exit:"); + ender = getchar(); + } + return 0; +} + +int main(int argc, char const *argv[]) +{ + return 0; +} diff --git a/sgr-terminal-sequences.c b/sgr-terminal-sequences.c new file mode 100644 index 0000000..89700ca --- /dev/null +++ b/sgr-terminal-sequences.c @@ -0,0 +1,37 @@ +#include +#include +#include + +int main() +{ + // Set output mode to handle virtual terminal sequences + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (hOut == INVALID_HANDLE_VALUE) + { + return GetLastError(); + } + + DWORD dwMode = 0; + if (!GetConsoleMode(hOut, &dwMode)) + { + return GetLastError(); + } + + dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if (!SetConsoleMode(hOut, dwMode)) + { + return GetLastError(); + } + + // Try some Set Graphics Rendition (SGR) terminal escape sequences + wprintf(L"\x1b[31mThis text has a red foreground using SGR.31.\r\n"); + wprintf(L"\x1b[1mThis text has a bright (bold) red foreground using SGR.1 to affect the previous color setting.\r\n"); + wprintf(L"\x1b[mThis text has returned to default colors using SGR.0 implicitly.\r\n"); + wprintf(L"\x1b[34;46mThis text shows the foreground and background change at the same time.\r\n"); + wprintf(L"\x1b[0mThis text has returned to default colors using SGR.0 explicitly.\r\n"); + wprintf(L"\x1b[31;32;33;34;35;36;101;102;103;104;105;106;107mThis text attempts to apply many colors in the same command. Note the colors are applied from left to right so only the right-most option of foreground cyan (SGR.36) and background bright white (SGR.107) is effective.\r\n"); + wprintf(L"\x1b[39mThis text has restored the foreground color only.\r\n"); + wprintf(L"\x1b[49mThis text has restored the background color only.\r\n"); + + return 0; +} \ No newline at end of file