typing_10_fingers git · main
10 finger typing trainer (CMake)
C 97.4% Markdown 2.6%git clone https://git.christianimmanuel.de/web-tools/typing_10_fingers.gitwget https://git.christianimmanuel.de/web-tools/typing_10_fingers/archive/typing_10_fingers.tar.gzFix: use widechar so umlauts work
inout.c | 26 ++++++++++++++----------- inout.h | 9 +++++---- main.c | 29 +++++++++++++++------------- test_input.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 28 deletions(-) diff --git a/inout.c b/inout.c index b11dd2a..12c166e 100644 --- a/inout.c +++ b/inout.c @@ -6,19 +6,18 @@ #include <sys/ioctl.h> // window size #include <string.h> #include <time.h> +#include <locale.h> -void printAt(int x, int y, char c, int color){ +void printAt(int x, int y, wchar_t c, int color){ //printf("%c\033[0m", c); - printf("\033[%d;%dH", y, x); if (color == RED) { - printf("\033[48;5;124m"); + wprintf(L"\033[%d;%dH\033[48;5;124m%lc\033[0m", y, x, c); } else { - printf("\033[48;5;28m"); + wprintf(L"\033[%d;%dH\033[48;5;28m%lc\033[0m", y, x, c); } - printf("%c\033[0m", c); } -void getLine(char* line, char* chars, int length, int wc){ +void getLine(wchar_t* line, wchar_t* chars, int length, int wc){ srand(time(NULL)); int word_length = 3; int count_word_chars = 0; @@ -39,26 +38,31 @@ void getLine(char* line, char* chars, int length, int wc){ int ran = rand() % 4 + 0; line[i] = chars[ran]; } - line[i] = '\0'; + line[i] = L'\0'; } -void printChar(char* lines, int color, char c, struct position_s position){ +void printChar(wchar_t* lines, int color, wchar_t c, struct position_s position){ printAt(position.x, position.y, lines[position.x-1], color); if (c != lines[position.x-1]) { printAt(position.x, position.y+1, c, RED); } } -int readChar(){ +wchar_t readChar() { struct termios old_settings, new_settings; + wchar_t c = L'\0'; + tcgetattr(STDIN_FILENO, &old_settings); new_settings = old_settings; - new_settings.c_lflag &= (~ICANON & ~ECHO); // disables output + new_settings.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &new_settings); - char c = getc(stdin); + + c = fgetwc(stdin); + tcsetattr(STDIN_FILENO, TCSANOW, &old_settings); + return c; } diff --git a/inout.h b/inout.h index 48c3f7c..af03c93 100644 --- a/inout.h +++ b/inout.h @@ -1,12 +1,13 @@ #ifndef TYPING_10_FINGERS_INOUT_H #define TYPING_10_FINGERS_INOUT_H +#include <wchar.h> enum color_b {RED, GREEN}; struct position_s {int x; int y;}; -void printAt(int, int, char, int); -void getLine(char* , char*, int, int); -int readChar(void); -void printChar(char* , int , char, struct position_s); +void printAt(int, int, wchar_t, int); +void getLine(wchar_t* , wchar_t*, int, int); +wchar_t readChar(void); +void printChar(wchar_t* , int , wchar_t, struct position_s); #endif //TYPING_10_FINGERS_INOUT_H diff --git a/main.c b/main.c index e2d1c6b..bb9ea9d 100644 --- a/main.c +++ b/main.c @@ -5,10 +5,11 @@ #include <sys/ioctl.h> // window size #include <string.h> #include <time.h> +#include <locale.h> #include "inout.h" -int runLevel(char *lines, int chars_max, struct position_s position) { - int c; +int runLevel(wchar_t *lines, int chars_max, struct position_s position) { + wchar_t c = L'\0'; int mistakes = 0; do { c = readChar(); @@ -22,17 +23,19 @@ int runLevel(char *lines, int chars_max, struct position_s position) { } -char left[] = "asdfg"; -char left_t[] = "qwert"; -char left_b[] = "<yxcvb"; +wchar_t left[] = { L'a', L's', L'd', L'f', L'g', L'\0' }; +wchar_t left_t[] = { L'q', L'w', L'e', L'r', L't', L'\0' }; +wchar_t left_b[] = { L'<', L'y', L'x', L'c', L'v', L'b', L'\0' }; -char right[] = "jklöhä#"; -char right_t[] = "uiopüz+"; -char right_b[] = "m,.-n"; +wchar_t right[] = { L'j', L'k', L'l', L'ö', L'h', L'ä', L'#', L'\0' }; +wchar_t right_t[] = { L'u', L'i', L'o', L'p', L'ü', L'z', L'+', L'\0' }; +wchar_t right_b[] = { L'm', L',', L'.', L'-', L'n', L'\0' }; int main() { + setlocale(LC_ALL, ""); + struct winsize w; struct position_s position = {1, 1}; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); @@ -42,18 +45,18 @@ int main() { int chars_max = 15; - char lines[chars_max]; + wchar_t lines[chars_max]; int level = 1; int mistakes = 1; - char lvl_arr[100]; + wchar_t lvl_arr[100]; while(1) { switch (level) { case 1: - strcpy(lvl_arr, left); + wcscpy(lvl_arr, left); break; case 2: - strcpy(lvl_arr, right); + wcscpy(lvl_arr, right); break; } @@ -62,7 +65,7 @@ int main() { getLine(lines, lvl_arr, chars_max, w.ws_col); - printf("\033[1;1H%s", lines); + wprintf(L"\033[1;1H%ls", lines); mistakes = runLevel(lines, chars_max, position); if (mistakes == 0) diff --git a/test_input.c b/test_input.c new file mode 100644 index 0000000..392512b --- /dev/null +++ b/test_input.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <termios.h> +#include <sys/ioctl.h> // window size +#include <string.h> +#include <time.h> +#include <locale.h> +#include <wchar.h> + +wchar_t readChar() { + struct termios old_settings, new_settings; + + wchar_t c = L'\0'; + + tcgetattr(STDIN_FILENO, &old_settings); + new_settings = old_settings; + + new_settings.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &new_settings); + + c = fgetwc(stdin); + wprintf(L"\033[1;1H%lc", c); + + tcsetattr(STDIN_FILENO, TCSANOW, &old_settings); + + return c; +} + + +int main() { + struct termios old_settings, new_settings; + setlocale(LC_ALL, ""); + + + wchar_t c = L'\0'; + /* + setlocale(LC_ALL, ""); + + tcgetattr(STDIN_FILENO, &old_settings); + new_settings = old_settings; + + new_settings.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &new_settings); + + c = fgetwc(stdin); + + tcsetattr(STDIN_FILENO, TCSANOW, &old_settings); + + wprintf(L"|%lc|", c); + */ + wprintf(L"|%lc|", readChar()); + + return 1; + + + + + + printf("|%c|", c); + + return 0; +}