From ac8901fb96d1eb9627fef151c846591483eec8b4 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 15:38:45 +1200 Subject: [PATCH 01/10] Moved Code into Dedicated Methods --- StenoByte_Helper.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ StenoByte_Helper.h | 6 ++++ main.c | 70 ++------------------------------------- 3 files changed, 91 insertions(+), 67 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index c81429c..843da79 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -218,4 +218,86 @@ void disable_echo() { */ void restore_terminal() { tcsetattr(STDIN_FILENO, TCSANOW, &original_terminal_settings); // restore settings +} + +/* + * Sets up the application and configures the devices to read from + */ +void setup_stenobyte() { + printf("Starting StenoType...\n"); + + setup_subvalues_array(); + + // Struct to store the evdev device + struct libevdev *keyboard_device = nullptr; + + // Opens the keyboard event file (usually event3) in Read Only and Non-Blocking Modes + // Reports an error if something went wrong + const int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to correct device + if (event_file_device < 0) { + perror("Failed to open device"); + return 1; + } + + // Disables printing inputs to the terminal + disable_echo(); + + // Initialises the evdev device (stored in libevdev struct named "keyboard_device") + // Reports an error if evdev initialisation failed + if (libevdev_new_from_fd(event_file_device, &keyboard_device) < 0) { + perror("Failed to init libevdev"); + return 1; + } + + // Prints evdev device name + printf("Input device name: %s\n", libevdev_get_name(keyboard_device)); + printf("Press ESC to exit\n"); +} + +/* + * Runs the loop that constantly checks the keyboard events and performs the associated actions + */ +void run_stenobyte() { + struct input_event current_event; // The current event struct + + print_bit_arr_summary(); // Initial Print Summary + + // Infinitely loops by default + while (1) { + // Gets the next event + const int next_event_result_code = libevdev_next_event(keyboard_device, + LIBEVDEV_READ_FLAG_NORMAL, ¤t_event); + + // Ignores Non-Success Read Events + if (next_event_result_code != LIBEVDEV_READ_STATUS_SUCCESS) { + continue; + } + + // Ensures a Key Event Type Occurred, ignores otherwise + if (current_event.type != EV_KEY) { + continue; + } + + // If ESC Key is pushed, then exit app + if (current_event.code == KEY_ESC) { + printf("ESC pressed\nExiting...\n"); + break; + } + + process_key_presses(¤t_event); + print_bit_arr_summary(); + + if (ready_to_compute_byte) { + compute_byte(); + } + + usleep(1000); // Small delay + } +} + +void end_stenobyte() { + // Frees up resources before application ends + libevdev_free(keyboard_device); + restore_terminal(); // Restores printing inputs to the terminal + close(event_file_device); } \ No newline at end of file diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index 89ab1f6..da1ae4c 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -49,6 +49,12 @@ extern u_int8_t current_byte; // The byte last computed from the bit array // Methods & Functions +void setup_stenobyte(); + +void run_stenobyte(); + +void end_stenobyte(); + bool is_valid_key(int key_code); void update_bit_arr(int key_code, bool new_state); diff --git a/main.c b/main.c index cea7c89..3ea3f06 100644 --- a/main.c +++ b/main.c @@ -20,74 +20,10 @@ int main() { - printf("Starting StenoType...\n"); - // Struct to store the evdev device - struct libevdev *keyboard_device = nullptr; + setup_stenobyte(); - setup_subvalues_array(); + run_stenobyte(); - // Opens the keyboard event file (usually event3) in Read Only and Non-Blocking Modes - // Reports an error if something went wrong - const int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to correct device - if (event_file_device < 0) { - perror("Failed to open device"); - return 1; - } - - // Disables printing inputs to the terminal - disable_echo(); - - // Initialises the evdev device (stored in libevdev struct named "keyboard_device") - // Reports an error if evdev initialisation failed - if (libevdev_new_from_fd(event_file_device, &keyboard_device) < 0) { - perror("Failed to init libevdev"); - return 1; - } - - // Prints evdev device name - printf("Input device name: %s\n", libevdev_get_name(keyboard_device)); - printf("Press ESC to exit\n"); - - - struct input_event current_event; // The current event struct - - print_bit_arr_summary(); // Initial Print Summary - - // Infinitely loops by default - while (1) { - // Gets the next event - const int next_event_result_code = libevdev_next_event(keyboard_device, - LIBEVDEV_READ_FLAG_NORMAL, ¤t_event); - - // Ignores Non-Success Read Events - if (next_event_result_code != LIBEVDEV_READ_STATUS_SUCCESS) { - continue; - } - - // Ensures a Key Event Type Occurred, ignores otherwise - if (current_event.type != EV_KEY) { - continue; - } - - // If ESC Key is pushed, then exit app - if (current_event.code == KEY_ESC) { - printf("ESC pressed\nExiting...\n"); - break; - } - - process_key_presses(¤t_event); - print_bit_arr_summary(); - - if (ready_to_compute_byte) { - compute_byte(); - } - - usleep(1000); // Small delay - } - - // Frees up resources before application ends - libevdev_free(keyboard_device); - restore_terminal(); // Restores printing inputs to the terminal - close(event_file_device); + end_stenobyte(); return 0; } From ad8bb9fa29f9879d44c64d7ed797e52d4acd75c4 Mon Sep 17 00:00:00 2001 From: Asami De Almeida Date: Fri, 16 May 2025 16:00:51 +1200 Subject: [PATCH 02/10] In The Middle of Refactoring --- StenoByte_Helper.c | 5 ++++- StenoByte_Helper.h | 4 +++- main.c | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index 843da79..218d9a9 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -222,8 +222,10 @@ void restore_terminal() { /* * Sets up the application and configures the devices to read from + * + * Returns 0 if there were no errors, 1 if there were errors */ -void setup_stenobyte() { +int setup_stenobyte() { printf("Starting StenoType...\n"); setup_subvalues_array(); @@ -252,6 +254,7 @@ void setup_stenobyte() { // Prints evdev device name printf("Input device name: %s\n", libevdev_get_name(keyboard_device)); printf("Press ESC to exit\n"); + return 0; } /* diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index da1ae4c..e361589 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -35,7 +35,9 @@ // Number of Bits in the Bit Array (should be 8) # define BITS_ARR_SIZE 8 +extern struct libevdev keyboard_device; // Struct to store the evdev device extern struct termios original_terminal_settings; // Termios Struct to store original terminal settings +int *event_file_device = nullptr; // Arrays & Variables // Bit Array that contains the bits that forms a byte @@ -49,7 +51,7 @@ extern u_int8_t current_byte; // The byte last computed from the bit array // Methods & Functions -void setup_stenobyte(); +int setup_stenobyte(); void run_stenobyte(); diff --git a/main.c b/main.c index 3ea3f06..63c7fa4 100644 --- a/main.c +++ b/main.c @@ -20,7 +20,10 @@ int main() { - setup_stenobyte(); + int setup_result = setup_stenobyte(); + if (setup_result != 0) { + return setup_result; + } run_stenobyte(); From 349eb8ae96e63091cd9d18869e1d3107c2bd46ba Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 17:19:03 +1200 Subject: [PATCH 03/10] Added Dockerfile to build a Sandbox Environment --- dockerfiles/README.md | 6 ++++++ dockerfiles/alpine-dev-test/Dockerfile | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 dockerfiles/README.md create mode 100644 dockerfiles/alpine-dev-test/Dockerfile diff --git a/dockerfiles/README.md b/dockerfiles/README.md new file mode 100644 index 0000000..8f80b70 --- /dev/null +++ b/dockerfiles/README.md @@ -0,0 +1,6 @@ +# Dockerfiles + +This directory will contain directories of Dockerfiles used for building a local Linux sandbox environment. +This is useful for developing for Linux while using a Mac or Windows machine. + +If you are using CLion, visit [this tutorial](https://www.jetbrains.com/help/clion/clion-toolchains-in-docker.html) to learn how to connect a Docker image to your project for development. \ No newline at end of file diff --git a/dockerfiles/alpine-dev-test/Dockerfile b/dockerfiles/alpine-dev-test/Dockerfile new file mode 100644 index 0000000..27dd2e8 --- /dev/null +++ b/dockerfiles/alpine-dev-test/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine:latest + +RUN apk add --update build-base 'cmake>3.30' libevdev libevdev-dev gdb From ce68872b8c08c998e29cdfb45c938c2ebdfb62c8 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 17:55:50 +1200 Subject: [PATCH 04/10] Finished Main Refactoring and Fixed Variables --- StenoByte_Helper.c | 172 +++++++++++++++++++++++---------------------- StenoByte_Helper.h | 4 +- 2 files changed, 89 insertions(+), 87 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index 218d9a9..a60b321 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -18,7 +18,10 @@ #include "StenoByte_Helper.h" +// Struct to store the evdev device +struct libevdev *keyboard_device = nullptr; struct termios original_terminal_settings; // Termios Struct to store original terminal settings +const int event_file_device; // Arrays & Variables // Bit Array that contains the bits that forms a byte @@ -29,6 +32,90 @@ bool ready_to_compute_byte = false; // the state for whether to convert the bit u_int8_t current_byte = 0x00; // The byte last computed from the bit array +/* + * Sets up the application and configures the devices to read from + * + * Returns 0 if there were no errors, 1 if there were errors + */ +int setup_stenobyte() { + printf("Starting StenoType...\n"); + + setup_subvalues_array(); + + int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to the correct device + + // Opens the keyboard event file (usually event3) in Read-Only and Non-Blocking Modes + // Reports an error if something went wrong + + if (event_file_device < 0) { + perror("Failed to open device"); + return 1; + } + + // Disables printing inputs to the terminal + disable_echo(); + + // Initialises the evdev device (stored in libevdev struct named "keyboard_device") + // Reports an error if evdev initialisation failed + if (libevdev_new_from_fd(event_file_device, &keyboard_device) < 0) { + perror("Failed to init libevdev"); + return 1; + } + + // Prints evdev device name + printf("Input device name: %s\n", libevdev_get_name(keyboard_device)); + printf("Press ESC to exit\n"); + return 0; +} + +/* + * Runs the loop that constantly checks the keyboard events and performs the associated actions + */ +void run_stenobyte() { + struct input_event current_event; // The current event struct + + print_bit_arr_summary(); // Initial Print Summary + + // Infinitely loops by default + while (1) { + // Gets the next event + const int next_event_result_code = libevdev_next_event(keyboard_device, + LIBEVDEV_READ_FLAG_NORMAL, ¤t_event); + + // Ignores Non-Success Read Events + if (next_event_result_code != LIBEVDEV_READ_STATUS_SUCCESS) { + continue; + } + + // Ensures a Key Event Type Occurred, ignores otherwise + if (current_event.type != EV_KEY) { + continue; + } + + // If ESC Key is pushed, then exit app + if (current_event.code == KEY_ESC) { + printf("ESC pressed\nExiting...\n"); + break; + } + + process_key_presses(¤t_event); + print_bit_arr_summary(); + + if (ready_to_compute_byte) { + compute_byte(); + } + + usleep(1000); // Small delay + } +} + +void end_stenobyte() { + // Frees up resources before application ends + libevdev_free(keyboard_device); + restore_terminal(); // Restores printing inputs to the terminal + close(event_file_device); +} + /* * Checks whether a valid key is pressed */ @@ -219,88 +306,3 @@ void disable_echo() { void restore_terminal() { tcsetattr(STDIN_FILENO, TCSANOW, &original_terminal_settings); // restore settings } - -/* - * Sets up the application and configures the devices to read from - * - * Returns 0 if there were no errors, 1 if there were errors - */ -int setup_stenobyte() { - printf("Starting StenoType...\n"); - - setup_subvalues_array(); - - // Struct to store the evdev device - struct libevdev *keyboard_device = nullptr; - - // Opens the keyboard event file (usually event3) in Read Only and Non-Blocking Modes - // Reports an error if something went wrong - const int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to correct device - if (event_file_device < 0) { - perror("Failed to open device"); - return 1; - } - - // Disables printing inputs to the terminal - disable_echo(); - - // Initialises the evdev device (stored in libevdev struct named "keyboard_device") - // Reports an error if evdev initialisation failed - if (libevdev_new_from_fd(event_file_device, &keyboard_device) < 0) { - perror("Failed to init libevdev"); - return 1; - } - - // Prints evdev device name - printf("Input device name: %s\n", libevdev_get_name(keyboard_device)); - printf("Press ESC to exit\n"); - return 0; -} - -/* - * Runs the loop that constantly checks the keyboard events and performs the associated actions - */ -void run_stenobyte() { - struct input_event current_event; // The current event struct - - print_bit_arr_summary(); // Initial Print Summary - - // Infinitely loops by default - while (1) { - // Gets the next event - const int next_event_result_code = libevdev_next_event(keyboard_device, - LIBEVDEV_READ_FLAG_NORMAL, ¤t_event); - - // Ignores Non-Success Read Events - if (next_event_result_code != LIBEVDEV_READ_STATUS_SUCCESS) { - continue; - } - - // Ensures a Key Event Type Occurred, ignores otherwise - if (current_event.type != EV_KEY) { - continue; - } - - // If ESC Key is pushed, then exit app - if (current_event.code == KEY_ESC) { - printf("ESC pressed\nExiting...\n"); - break; - } - - process_key_presses(¤t_event); - print_bit_arr_summary(); - - if (ready_to_compute_byte) { - compute_byte(); - } - - usleep(1000); // Small delay - } -} - -void end_stenobyte() { - // Frees up resources before application ends - libevdev_free(keyboard_device); - restore_terminal(); // Restores printing inputs to the terminal - close(event_file_device); -} \ No newline at end of file diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index e361589..562dd75 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -35,9 +35,9 @@ // Number of Bits in the Bit Array (should be 8) # define BITS_ARR_SIZE 8 -extern struct libevdev keyboard_device; // Struct to store the evdev device +extern struct libevdev *keyboard_device; // Struct to store the evdev device extern struct termios original_terminal_settings; // Termios Struct to store original terminal settings -int *event_file_device = nullptr; +extern const int event_file_device; // Arrays & Variables // Bit Array that contains the bits that forms a byte From 6308c878daa867702383e1f44518b52769d33ab4 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 18:56:44 +1200 Subject: [PATCH 05/10] Started on Printing Optimisations Counted up how many characters the print summary is going to print and split the print_byte_summary() method. --- StenoByte_Helper.c | 57 +++++++++++++++++++++++++++------------------- StenoByte_Helper.h | 2 ++ main.c | 2 +- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index a60b321..03e3cfd 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -196,7 +196,7 @@ void setup_subvalues_array() { } /* - * Prints the event summary of a key press * + * Prints the event summary of a key press. Used for debugging. */ void print_event_summary(const struct input_event* current_event) { if (current_event == NULL) { @@ -214,7 +214,18 @@ void print_event_summary(const struct input_event* current_event) { } void print_byte_summary() { - printf("Last Computed Byte as decimal: %d\n", current_byte); + // printf("Last Computed Byte as decimal: %d\n", current_byte); + printf("%s", get_byte_summary()); +} + +/* + * Gets Byte Summary as a String (an array of chars) + * Prints between 33 and 35 chars but assumes current_byte will not be a value that exceeds 255. + */ +const char* get_byte_summary() { + char* msg[35]; + sprintf(*msg, "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars + return *msg; } /* @@ -222,36 +233,36 @@ void print_byte_summary() { * TODO: The repeated for loops could probably be simplified into a dedicated method */ void print_bit_arr_summary() { - printf("\nBits in Array:\n"); - printf("\tBit Value:\t| "); - for (int i = 7; i >= 0; i--) { - printf("\t%d\t|", bit_arr[i]); + printf("\nBits in Array:\n"); // Prints 16 chars + printf("\tBit Value:\t| "); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + printf("\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars } - printf("\n"); + printf("\n"); // Prints 1 char - for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { - printf("-"); + for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { // Repeats 24+(16*8) times, which is 152 + printf("-"); // Prints 1 char } - printf("\n\tSub-Value:\t|"); - for (int i = 7; i >= 0; i--) { - printf("\t[%d]\t|", subvalues_arr[i]); + printf("\n\tSub-Value:\t|"); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + printf("\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars } - printf("\n\tBit Index:\t|"); - for (int i = 7; i >= 0; i--) { - printf("\t[b%d]\t|", i); + printf("\n\tBit Index:\t|"); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + printf("\t[b%d]\t|", i); // Prints 7 times } - printf("\n\tKey:\t\t|"); + printf("\n\tKey:\t\t|"); // Prints 9 times - for (int i = 7; i >= 0; i--) { - printf("\t[%c]\t|", keys_arr[i]); + for (int i = 7; i >= 0; i--) { // Repeats 8 times + printf("\t[%c]\t|", keys_arr[i]); // Prints 6 char } - printf("\n"); - print_byte_summary(); - printf("\nPress & Hold the keys corresponding to the bits in the byte you would like to set to 1."); - printf("\nBits will be 0 if keys are not pressed."); - printf("\nPress SPACE BAR to compute Byte\t\t|\tPress ESC to exit\n"); + printf("\n"); // Prints 1 char + print_byte_summary(); // Prints between 33 and 35 chars + printf("\nPress & Hold the keys corresponding to the bits in the byte you would like to set to 1."); // Prints 88 chars + printf("\nBits will be 0 if keys are not pressed."); // Prints 40 chars + printf("\nPress SPACE BAR to compute Byte\t\t|\tPress ESC to exit\n"); // Prints 53 chars } /* diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index 562dd75..2db70ae 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -65,6 +65,8 @@ void compute_byte(); void setup_subvalues_array(); +const char* get_byte_summary(); + void print_event_summary(const struct input_event* current_event); void print_byte_summary(); diff --git a/main.c b/main.c index 63c7fa4..0933b2a 100644 --- a/main.c +++ b/main.c @@ -20,7 +20,7 @@ int main() { - int setup_result = setup_stenobyte(); + const int setup_result = setup_stenobyte(); if (setup_result != 0) { return setup_result; } From 14836ec04631581c5d557e68154f2a1827ea7a66 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 20:21:33 +1200 Subject: [PATCH 06/10] Fixed Summary Getter and Printer --- StenoByte_Helper.c | 10 +++++----- StenoByte_Helper.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index 03e3cfd..2eb9e6e 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -215,17 +215,17 @@ void print_event_summary(const struct input_event* current_event) { void print_byte_summary() { // printf("Last Computed Byte as decimal: %d\n", current_byte); - printf("%s", get_byte_summary()); + char msg[35]; + get_byte_summary(msg); + printf("%s", msg); } /* * Gets Byte Summary as a String (an array of chars) * Prints between 33 and 35 chars but assumes current_byte will not be a value that exceeds 255. */ -const char* get_byte_summary() { - char* msg[35]; - sprintf(*msg, "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars - return *msg; +void get_byte_summary(char* msg) { + sprintf(msg, "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars } /* diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index 2db70ae..e4705ab 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -65,7 +65,7 @@ void compute_byte(); void setup_subvalues_array(); -const char* get_byte_summary(); +void get_byte_summary(char* msg); void print_event_summary(const struct input_event* current_event); From 8aa5b2bf76754043a44650970afb3aa61b52fc2f Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 21:24:29 +1200 Subject: [PATCH 07/10] Implemented String Building Before Printing --- StenoByte_Helper.c | 46 ++++++++++++++++++++++++++-------------------- StenoByte_Helper.h | 1 + 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index 2eb9e6e..37be43f 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -213,8 +213,10 @@ void print_event_summary(const struct input_event* current_event) { current_event->code); // Prints the ID for the key that is affected } +/* + * Prints the Byte Summary + */ void print_byte_summary() { - // printf("Last Computed Byte as decimal: %d\n", current_byte); char msg[35]; get_byte_summary(msg); printf("%s", msg); @@ -222,10 +224,10 @@ void print_byte_summary() { /* * Gets Byte Summary as a String (an array of chars) - * Prints between 33 and 35 chars but assumes current_byte will not be a value that exceeds 255. + * Assumes msg has a minimum length of 35. Assumes value of current_byte will not exceed 255. */ void get_byte_summary(char* msg) { - sprintf(msg, "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars + sprintf(msg + strlen(msg), "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars } /* @@ -233,36 +235,40 @@ void get_byte_summary(char* msg) { * TODO: The repeated for loops could probably be simplified into a dedicated method */ void print_bit_arr_summary() { - printf("\nBits in Array:\n"); // Prints 16 chars - printf("\tBit Value:\t| "); // Prints 14 chars + char msg[654] = ""; + + sprintf(msg + strlen(msg), "\nBits in Array:\n"); // Prints 16 chars + sprintf(msg + strlen(msg), "\tBit Value:\t| "); // Prints 14 chars for (int i = 7; i >= 0; i--) { // Repeats 8 times - printf("\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars + sprintf(msg + strlen(msg), "\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars } - printf("\n"); // Prints 1 char + sprintf(msg + strlen(msg), "\n"); // Prints 1 char for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { // Repeats 24+(16*8) times, which is 152 - printf("-"); // Prints 1 char + sprintf(msg + strlen(msg), "-"); // Prints 1 char } - printf("\n\tSub-Value:\t|"); // Prints 14 chars + sprintf(msg + strlen(msg), "\n\tSub-Value:\t|"); // Prints 14 chars for (int i = 7; i >= 0; i--) { // Repeats 8 times - printf("\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars + sprintf(msg + strlen(msg), "\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars } - printf("\n\tBit Index:\t|"); // Prints 14 chars + sprintf(msg + strlen(msg), "\n\tBit Index:\t|"); // Prints 14 chars for (int i = 7; i >= 0; i--) { // Repeats 8 times - printf("\t[b%d]\t|", i); // Prints 7 times + sprintf(msg + strlen(msg), "\t[b%d]\t|", i); // Prints 7 chars } - printf("\n\tKey:\t\t|"); // Prints 9 times + sprintf(msg + strlen(msg), "\n\tKey:\t\t|"); // Prints 9 times for (int i = 7; i >= 0; i--) { // Repeats 8 times - printf("\t[%c]\t|", keys_arr[i]); // Prints 6 char + sprintf(msg + strlen(msg), "\t[%c]\t|", keys_arr[i]); // Prints 6 chars } - printf("\n"); // Prints 1 char - print_byte_summary(); // Prints between 33 and 35 chars - printf("\nPress & Hold the keys corresponding to the bits in the byte you would like to set to 1."); // Prints 88 chars - printf("\nBits will be 0 if keys are not pressed."); // Prints 40 chars - printf("\nPress SPACE BAR to compute Byte\t\t|\tPress ESC to exit\n"); // Prints 53 chars + sprintf(msg + strlen(msg), "\n"); // Prints 1 char + get_byte_summary(msg); // Prints between 33 and 35 chars + sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the byte you would like to set to 1."); // Prints 88 chars + sprintf(msg + strlen(msg), "\nBits will be 0 if keys are not pressed."); // Prints 40 chars + sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\tPress ESC to exit\n"); // Prints 53 chars + + printf("%s", msg); } /* @@ -274,7 +280,7 @@ void process_key_presses(const struct input_event* current_event) { return; } - // Exits method if event type is not related to a key event + // Exits method if the event_type variable is not related to a key event if (current_event->type != EV_KEY) { return; } diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index e4705ab..f278a72 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -26,6 +26,7 @@ #include #include #include +#include // Key Press States #define EV_KEY_RELEASED 0 From 11a8909eb9bbc3b82d8b087fe60c4fb80483de60 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 21:40:06 +1200 Subject: [PATCH 08/10] Minor Refactoring --- StenoByte_Helper.c | 18 ++++++++++-------- StenoByte_Helper.h | 4 ++-- main.c | 3 +++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/StenoByte_Helper.c b/StenoByte_Helper.c index 37be43f..e4c5a5a 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper.c @@ -42,7 +42,7 @@ int setup_stenobyte() { setup_subvalues_array(); - int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to the correct device + const int event_file_device = open("/dev/input/event3", O_RDONLY | O_NONBLOCK); // Change to the correct device // Opens the keyboard event file (usually event3) in Read-Only and Non-Blocking Modes // Reports an error if something went wrong @@ -92,7 +92,7 @@ void run_stenobyte() { continue; } - // If ESC Key is pushed, then exit app + // If the ESC Key is pushed, then exit the app if (current_event.code == KEY_ESC) { printf("ESC pressed\nExiting...\n"); break; @@ -190,7 +190,7 @@ void compute_byte() { */ void setup_subvalues_array() { for (int i = BITS_ARR_SIZE; i >= 0; i--) { - u_int8_t val = 0; + constexpr u_int8_t val = 0; subvalues_arr[i] = val ^ 1 << i; } } @@ -264,9 +264,11 @@ void print_bit_arr_summary() { } sprintf(msg + strlen(msg), "\n"); // Prints 1 char get_byte_summary(msg); // Prints between 33 and 35 chars - sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the byte you would like to set to 1."); // Prints 88 chars + sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the" + " byte you would like to set to 1."); // Prints 88 chars sprintf(msg + strlen(msg), "\nBits will be 0 if keys are not pressed."); // Prints 40 chars - sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\tPress ESC to exit\n"); // Prints 53 chars + sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\t" + "Press ESC to exit\n"); // Prints 53 chars printf("%s", msg); } @@ -290,8 +292,8 @@ void process_key_presses(const struct input_event* current_event) { return; } - // Sets the bit value in array to zero if the associated key is released - // Then exits method + // Sets the bit value in the array to zero if the associated key is released + // Then exits the method if (current_event->value == EV_KEY_RELEASED) { update_bit_arr(current_event->code, false); return; @@ -318,7 +320,7 @@ void disable_echo() { } /* - * Restores original terminal settings prior to this program running + * Restores original terminal settings before this program running */ void restore_terminal() { tcsetattr(STDIN_FILENO, TCSANOW, &original_terminal_settings); // restore settings diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index f278a72..dda1b5d 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -58,6 +58,8 @@ void run_stenobyte(); void end_stenobyte(); +void process_key_presses(const struct input_event* current_event); + bool is_valid_key(int key_code); void update_bit_arr(int key_code, bool new_state); @@ -74,8 +76,6 @@ void print_byte_summary(); void print_bit_arr_summary(); -void process_key_presses(const struct input_event* current_event); - void disable_echo(); void restore_terminal(); diff --git a/main.c b/main.c index 0933b2a..9c30aa0 100644 --- a/main.c +++ b/main.c @@ -20,13 +20,16 @@ int main() { + // Performs setup; exits app if there was an error while setting up const int setup_result = setup_stenobyte(); if (setup_result != 0) { return setup_result; } + // Runs the loop run_stenobyte(); + // Frees up Memory Safely end_stenobyte(); return 0; } From 4b12755a139b1d2d13490d1ce8b3df0021ad83a5 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Fri, 16 May 2025 23:04:52 +1200 Subject: [PATCH 09/10] Split Helper Files into Core & Helper Files The StenoByte_Core.c Source File is intended to remain consistent and compiled universally no matter the OS. The StenoByte_Helper_for_Linux.c Source File is intended to be included when compiling for Linux, and excluded when compiling for other Operating Systems (OS's). StenoByte_Core.h holds the definitions for StenoByte_Core.h and is the main dependency for the main.c file. StenoByte_Helper.h holds the definitions for the Helper Source Files and will conditionally apply different declarations for different OS's. --- CMakeLists.txt | 6 +- StenoByte_Core.c | 117 ++++++++++++ StenoByte_Core.h | 52 ++++++ StenoByte_Helper.h | 43 ++--- ...e_Helper.c => StenoByte_Helper_for_Linux.c | 170 +++++------------- main.c | 2 +- 6 files changed, 230 insertions(+), 160 deletions(-) create mode 100644 StenoByte_Core.c create mode 100644 StenoByte_Core.h rename StenoByte_Helper.c => StenoByte_Helper_for_Linux.c (67%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a11924a..1c6834a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,9 @@ find_package(PkgConfig REQUIRED) pkg_search_module(LIBEVDEV REQUIRED libevdev) add_executable(StenoByte_Prototype main.c - StenoByte_Helper.c - StenoByte_Helper.h) + StenoByte_Helper_for_Linux.c + StenoByte_Helper.h + StenoByte_Core.c + StenoByte_Core.h) target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS}) target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES}) \ No newline at end of file diff --git a/StenoByte_Core.c b/StenoByte_Core.c new file mode 100644 index 0000000..fcb248b --- /dev/null +++ b/StenoByte_Core.c @@ -0,0 +1,117 @@ +/** + StenoByte: a stenotype inspired keyboard app for typing out bytes. + + StenoByte_Core.c is the source file for implementing resources related to processing the Bits into a Byte and + for producing the result as an output. + + This file is intended to be Operating System agnostic and to be compiled for any Operating System. + + Copyright 2025 Asami De Almeida + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include "StenoByte_Core.h" + +// Arrays & Variables +// Bit Array that contains the bits that forms a byte +bool bit_arr[BITS_ARR_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; // ordered from b0 to b7 during initialisation +char keys_arr[BITS_ARR_SIZE] = {';', 'L', 'K', 'J', 'F', 'D', 'S', 'A'}; // ';' = b0, 'L' = b1, ... 'A' = b7 +u_int8_t subvalues_arr[BITS_ARR_SIZE]; +bool ready_to_compute_byte = false; // the state for whether to convert the bit array into a byte and process it + +u_int8_t current_byte = 0x00; // The byte last computed from the bit array + + +// Methods & Functions + +/* + * Generates the Byte based on the bits in the array + */ +void compute_byte() { + current_byte = 0x00; // Resets the Byte to zero + for (int i = 0; i < BITS_ARR_SIZE; i++) { + current_byte = current_byte ^ bit_arr[i] << i; + } + ready_to_compute_byte = false; +} + +/* + * Sets up the sub-values array for labelling + */ +void setup_subvalues_array() { + for (int i = BITS_ARR_SIZE; i >= 0; i--) { + constexpr u_int8_t val = 0; + subvalues_arr[i] = val ^ 1 << i; + } +} + +/* + * Gets Byte Summary as a String (an array of chars) + * Assumes msg has a minimum length of 35. Assumes value of current_byte will not exceed 255. + */ +void get_byte_summary(char* msg) { + sprintf(msg + strlen(msg), "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars +} + +/* + * Prints the Byte Summary + */ +void print_byte_summary() { + char msg[35]; + get_byte_summary(msg); + printf("%s", msg); +} + +/* + * Prints the current state of the Bit Array + * TODO: The repeated for loops could probably be simplified into a dedicated method + */ +void print_bit_arr_summary() { + char msg[654] = ""; + + sprintf(msg + strlen(msg), "\nBits in Array:\n"); // Prints 16 chars + sprintf(msg + strlen(msg), "\tBit Value:\t| "); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + sprintf(msg + strlen(msg), "\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars + } + sprintf(msg + strlen(msg), "\n"); // Prints 1 char + + for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { // Repeats 24+(16*8) times, which is 152 + sprintf(msg + strlen(msg), "-"); // Prints 1 char + } + + sprintf(msg + strlen(msg), "\n\tSub-Value:\t|"); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + sprintf(msg + strlen(msg), "\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars + } + + sprintf(msg + strlen(msg), "\n\tBit Index:\t|"); // Prints 14 chars + for (int i = 7; i >= 0; i--) { // Repeats 8 times + sprintf(msg + strlen(msg), "\t[b%d]\t|", i); // Prints 7 chars + } + sprintf(msg + strlen(msg), "\n\tKey:\t\t|"); // Prints 9 times + + for (int i = 7; i >= 0; i--) { // Repeats 8 times + sprintf(msg + strlen(msg), "\t[%c]\t|", keys_arr[i]); // Prints 6 chars + } + sprintf(msg + strlen(msg), "\n"); // Prints 1 char + get_byte_summary(msg); // Prints between 33 and 35 chars + sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the" + " byte you would like to set to 1."); // Prints 88 chars + sprintf(msg + strlen(msg), "\nBits will be 0 if keys are not pressed."); // Prints 40 chars + sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\t" + "Press ESC to exit\n"); // Prints 53 chars + + printf("%s", msg); +} \ No newline at end of file diff --git a/StenoByte_Core.h b/StenoByte_Core.h new file mode 100644 index 0000000..650faf2 --- /dev/null +++ b/StenoByte_Core.h @@ -0,0 +1,52 @@ +/** + StenoByte: a stenotype inspired keyboard app for typing out bytes. + + StenoByte_Core.h is the header file for defining resources related to processing the Bits into a Byte and + for producing the result as an output. + + Copyright 2025 Asami De Almeida + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#ifndef STENOBYTE_CORE_H +#define STENOBYTE_CORE_H + +#include "StenoByte_Helper.h" + +// Number of Bits in the Bit Array (should be 8) +# define BITS_ARR_SIZE 8 + +// Arrays & Variables +// Bit Array that contains the bits that forms a byte +extern bool bit_arr[BITS_ARR_SIZE]; // ordered from b0 to b7 during initialisation +extern char keys_arr[BITS_ARR_SIZE]; // ';' = b0, 'L' = b1, ... 'A' = b7 +extern u_int8_t subvalues_arr[BITS_ARR_SIZE]; +extern bool ready_to_compute_byte; // the state for whether to convert the bit array into a byte and process it +extern u_int8_t current_byte; // The byte last computed from the bit array + + +// Externally Declared Methods & Functions (expected to be declared & defined StenoByte_Helper files) +extern void update_bit_arr(int key_code, bool new_state); +extern int setup_stenobyte(); +extern void run_stenobyte(); +extern void end_stenobyte(); + +// Methods & Functions +void compute_byte(); +void setup_subvalues_array(); +void get_byte_summary(char* msg); +void print_byte_summary(); +void print_bit_arr_summary(); + +#endif //STENOBYTE_CORE_H diff --git a/StenoByte_Helper.h b/StenoByte_Helper.h index dda1b5d..aa3fa5d 100644 --- a/StenoByte_Helper.h +++ b/StenoByte_Helper.h @@ -1,6 +1,11 @@ /** StenoByte: a stenotype inspired keyboard app for typing out bytes. + StenoByte_Helper.h is the header file for defining resources for Keyboard Event Reading for a range of + Operating Systems. + + TODO: Implement conditional declarations that will be dependent on the target OS. + Copyright 2025 Asami De Almeida Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,51 +38,29 @@ #define EV_KEY_PRESSED 1 #define EV_KEY_REPEATED 2 -// Number of Bits in the Bit Array (should be 8) -# define BITS_ARR_SIZE 8 - +// Externally Declared Structs and Variables (expected to be declared and implemented in dependencies) extern struct libevdev *keyboard_device; // Struct to store the evdev device extern struct termios original_terminal_settings; // Termios Struct to store original terminal settings extern const int event_file_device; -// Arrays & Variables -// Bit Array that contains the bits that forms a byte -extern bool bit_arr[BITS_ARR_SIZE]; // ordered from b0 to b7 during initialisation -extern char keys_arr[BITS_ARR_SIZE]; // ';' = b0, 'L' = b1, ... 'A' = b7 -extern u_int8_t subvalues_arr[BITS_ARR_SIZE]; -extern bool ready_to_compute_byte; // the state for whether to convert the bit array into a byte and process it -extern u_int8_t current_byte; // The byte last computed from the bit array +// Externally Declared Methods & Functions +// (expected to be declared & defined in dependent libraries or in StenoByte_Core files) +extern void setup_subvalues_array(); // StenoByte_Core.h/c +extern void compute_byte(); +extern void print_bit_arr_summary(); +extern void update_bit_arr(int key_code, bool new_state); // Methods & Functions - int setup_stenobyte(); - +void update_bit_arr(int key_code, bool new_state); void run_stenobyte(); - void end_stenobyte(); - void process_key_presses(const struct input_event* current_event); - bool is_valid_key(int key_code); - -void update_bit_arr(int key_code, bool new_state); - -void compute_byte(); - -void setup_subvalues_array(); - -void get_byte_summary(char* msg); - void print_event_summary(const struct input_event* current_event); - -void print_byte_summary(); - -void print_bit_arr_summary(); - void disable_echo(); - void restore_terminal(); #endif //STENOBYTE_HELPER_H diff --git a/StenoByte_Helper.c b/StenoByte_Helper_for_Linux.c similarity index 67% rename from StenoByte_Helper.c rename to StenoByte_Helper_for_Linux.c index e4c5a5a..281fe3a 100644 --- a/StenoByte_Helper.c +++ b/StenoByte_Helper_for_Linux.c @@ -1,6 +1,10 @@ /** StenoByte: a stenotype inspired keyboard app for typing out bytes. + StenoByte_Helper.c is the source file for implementing the Reading & Processing of Keyboard Events. + + This file is intended to be modular and to be used when building for Linux Operating Systems. + Copyright 2025 Asami De Almeida Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,20 +21,13 @@ */ #include "StenoByte_Helper.h" +#include "StenoByte_Core.h" // Struct to store the evdev device struct libevdev *keyboard_device = nullptr; struct termios original_terminal_settings; // Termios Struct to store original terminal settings const int event_file_device; -// Arrays & Variables -// Bit Array that contains the bits that forms a byte -bool bit_arr[BITS_ARR_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; // ordered from b0 to b7 during initialisation -char keys_arr[BITS_ARR_SIZE] = {';', 'L', 'K', 'J', 'F', 'D', 'S', 'A'}; // ';' = b0, 'L' = b1, ... 'A' = b7 -u_int8_t subvalues_arr[BITS_ARR_SIZE]; -bool ready_to_compute_byte = false; // the state for whether to convert the bit array into a byte and process it - -u_int8_t current_byte = 0x00; // The byte last computed from the bit array /* * Sets up the application and configures the devices to read from @@ -68,6 +65,44 @@ int setup_stenobyte() { return 0; } +/* + * Updates the current bits in the array and whether the array is ready to be computed into a byte (which is when + * the space bar is pressed) + */ +void update_bit_arr(const int key_code, const bool new_state) { + switch (key_code) { + case KEY_A: + bit_arr[7] = new_state; + return; + case KEY_S: + bit_arr[6] = new_state; + return; + case KEY_D: + bit_arr[5] = new_state; + return; + case KEY_F: + bit_arr[4] = new_state; + return; + case KEY_J: + bit_arr[3] = new_state; + return; + case KEY_K: + bit_arr[2] = new_state; + return; + case KEY_L: + bit_arr[1] = new_state; + return; + case KEY_SEMICOLON: + bit_arr[0] = new_state; + return; + case KEY_SPACE: + // sets ready_to_compute_byte to true if new_state is true, else leaves it as it is + // ready_to_compute_byte should to be set to false after it computing the byte + ready_to_compute_byte = new_state ? true : ready_to_compute_byte; + default: ; + } +} + /* * Runs the loop that constantly checks the keyboard events and performs the associated actions */ @@ -136,65 +171,6 @@ bool is_valid_key(const int key_code) { } } -/* - * Updates the current bits in the array and whether the array is ready to be computed into a byte (which is when - * the space bar is pressed) - */ -void update_bit_arr(const int key_code, const bool new_state) { - switch (key_code) { - case KEY_A: - bit_arr[7] = new_state; - return; - case KEY_S: - bit_arr[6] = new_state; - return; - case KEY_D: - bit_arr[5] = new_state; - return; - case KEY_F: - bit_arr[4] = new_state; - return; - case KEY_J: - bit_arr[3] = new_state; - return; - case KEY_K: - bit_arr[2] = new_state; - return; - case KEY_L: - bit_arr[1] = new_state; - return; - case KEY_SEMICOLON: - bit_arr[0] = new_state; - return; - case KEY_SPACE: - // sets ready_to_compute_byte to true if new_state is true, else leaves it as it is - // ready_to_compute_byte should to be set to false after it computing the byte - ready_to_compute_byte = new_state ? true : ready_to_compute_byte; - default: ; - } -} - -/* - * Generates the Byte based on the bits in the array - */ -void compute_byte() { - current_byte = 0x00; // Resets the Byte to zero - for (int i = 0; i < BITS_ARR_SIZE; i++) { - current_byte = current_byte ^ bit_arr[i] << i; - } - ready_to_compute_byte = false; -} - -/* - * Sets up the sub-values array for labelling - */ -void setup_subvalues_array() { - for (int i = BITS_ARR_SIZE; i >= 0; i--) { - constexpr u_int8_t val = 0; - subvalues_arr[i] = val ^ 1 << i; - } -} - /* * Prints the event summary of a key press. Used for debugging. */ @@ -213,66 +189,6 @@ void print_event_summary(const struct input_event* current_event) { current_event->code); // Prints the ID for the key that is affected } -/* - * Prints the Byte Summary - */ -void print_byte_summary() { - char msg[35]; - get_byte_summary(msg); - printf("%s", msg); -} - -/* - * Gets Byte Summary as a String (an array of chars) - * Assumes msg has a minimum length of 35. Assumes value of current_byte will not exceed 255. - */ -void get_byte_summary(char* msg) { - sprintf(msg + strlen(msg), "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars -} - -/* - * Prints the current state of the Bit Array - * TODO: The repeated for loops could probably be simplified into a dedicated method - */ -void print_bit_arr_summary() { - char msg[654] = ""; - - sprintf(msg + strlen(msg), "\nBits in Array:\n"); // Prints 16 chars - sprintf(msg + strlen(msg), "\tBit Value:\t| "); // Prints 14 chars - for (int i = 7; i >= 0; i--) { // Repeats 8 times - sprintf(msg + strlen(msg), "\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars - } - sprintf(msg + strlen(msg), "\n"); // Prints 1 char - - for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { // Repeats 24+(16*8) times, which is 152 - sprintf(msg + strlen(msg), "-"); // Prints 1 char - } - - sprintf(msg + strlen(msg), "\n\tSub-Value:\t|"); // Prints 14 chars - for (int i = 7; i >= 0; i--) { // Repeats 8 times - sprintf(msg + strlen(msg), "\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars - } - - sprintf(msg + strlen(msg), "\n\tBit Index:\t|"); // Prints 14 chars - for (int i = 7; i >= 0; i--) { // Repeats 8 times - sprintf(msg + strlen(msg), "\t[b%d]\t|", i); // Prints 7 chars - } - sprintf(msg + strlen(msg), "\n\tKey:\t\t|"); // Prints 9 times - - for (int i = 7; i >= 0; i--) { // Repeats 8 times - sprintf(msg + strlen(msg), "\t[%c]\t|", keys_arr[i]); // Prints 6 chars - } - sprintf(msg + strlen(msg), "\n"); // Prints 1 char - get_byte_summary(msg); // Prints between 33 and 35 chars - sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the" - " byte you would like to set to 1."); // Prints 88 chars - sprintf(msg + strlen(msg), "\nBits will be 0 if keys are not pressed."); // Prints 40 chars - sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\t" - "Press ESC to exit\n"); // Prints 53 chars - - printf("%s", msg); -} - /* * Processes the key events, such as key presses, key releases, and key repeats (when the key is held down) */ diff --git a/main.c b/main.c index 9c30aa0..918027d 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ limitations under the License. */ -#include "StenoByte_Helper.h" +#include "StenoByte_Core.h" int main() { From aecfbc3b60d1be2d3da769efe5be7622a16cb771 Mon Sep 17 00:00:00 2001 From: Rachel Delgado Date: Sat, 17 May 2025 01:29:36 +1200 Subject: [PATCH 10/10] Unified Library & CMake OS Conditional Check StenoByte Core & Helper files are now compiled and included as a library called "StenoByte_Library". Moved Core & Helper Files to a new directory called "includes". CMakeLists.txt is now configured to check the OS the application is being built for and to change it accordingly. Minor updates to README. --- CMakeLists.txt | 48 +++++++++++++++---- README.md | 15 ++++-- StenoByte_Core.c => includes/StenoByte_Core.c | 2 +- StenoByte_Core.h => includes/StenoByte_Core.h | 0 .../StenoByte_Helper.h | 0 .../StenoByte_Helper_for_Linux.c | 0 main.c | 2 +- 7 files changed, 50 insertions(+), 17 deletions(-) rename StenoByte_Core.c => includes/StenoByte_Core.c (99%) rename StenoByte_Core.h => includes/StenoByte_Core.h (100%) rename StenoByte_Helper.h => includes/StenoByte_Helper.h (100%) rename StenoByte_Helper_for_Linux.c => includes/StenoByte_Helper_for_Linux.c (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c6834a..2bb79bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,13 +3,41 @@ project(StenoByte_Prototype C) set(CMAKE_C_STANDARD 23) -find_package(PkgConfig REQUIRED) -pkg_search_module(LIBEVDEV REQUIRED libevdev) - -add_executable(StenoByte_Prototype main.c - StenoByte_Helper_for_Linux.c - StenoByte_Helper.h - StenoByte_Core.c - StenoByte_Core.h) -target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS}) -target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES}) \ No newline at end of file +# Linux Variant +if (LINUX) + # Finds Libevdev Library + find_package(PkgConfig REQUIRED) + pkg_search_module(LIBEVDEV REQUIRED libevdev) + + # Prints Location of Libevdev Library + message(STATUS "libevdev include dirs: ${LIBEVDEV_INCLUDE_DIRS}") + message(STATUS "libevdev libraries: ${LIBEVDEV_LIBRARIES}") + + ## StenoByte Library for Linux Library + add_library(StenoByte_Library STATIC + includes/StenoByte_Helper_for_Linux.c + includes/StenoByte_Core.c) + target_include_directories(StenoByte_Library PRIVATE + ${LIBEVDEV_INCLUDE_DIRS} + includes) + target_link_libraries(StenoByte_Library PRIVATE ${LIBEVDEV_LIBRARIES}) + +# MacOS Variant +elseif (APPLE) + message(FATAL_ERROR "Unfortunately StenoByte is not yet compatible with MacOS, + but support for MacOS is in the works!") + +# Windows Variant +elseif (WIN32) + message(FATAL_ERROR "Unfortunately StenoByte is not yet compatible with Windows, + but support for Windows is in the works!") + +# Throws an error if system is incompatible +else () + message(FATAL_ERROR "Unfortunately your system is not compatible with StenoByte") +endif () + +# Main App +add_executable(StenoByte_Prototype main.c) +target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS} StenoByte_Library) +target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES} StenoByte_Library) \ No newline at end of file diff --git a/README.md b/README.md index 29d6f1d..2097f66 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ A stenotype inspired terminal app for typing out bytes quickly. This program is designed to allow you to build up a byte using eight keys from your keyboard, with each key -corresponding to a bit in a byte (which is made up of eight bits). When you press & hold down those keys, their +corresponding to a bit in a byte (which is made up of eight bits). When you press and hold down those keys, their associated bits are set to 1. When the keys are released or are not being pushed, their associated bits are set to 0. -When you are ready to turn those bits into a byte, press the Space Bar and the application will convert those bits into +When you are ready to turn those bits into a byte, press the Space Bar, and the application will convert those bits into a byte and display it as a decimal. -This application is currently designed to work in Linux. +This application is currently designed to work on Linux. For the time-being, this app requires elevated privileges (e.g. `sudo`) to run. @@ -40,6 +40,11 @@ sudo apt -y install build-essential cmake git libevdev-dev sudo pacman -S gcc cmake git libevdev ```` +#### Install Dependencies on Alpine +```shell +apk add --update build-base 'cmake>3.30' libevdev libevdev-dev gdb +``` + ### Step 2: Clone this repository Run: ```shell @@ -80,5 +85,5 @@ maintaining this repository. ## License Copyright (C) 2025 Asami De Almeida -Released under the Apache License 2.0 License. See the following pages for more information: -* [Apache License 2.0](LICENSE) +Released under the Apache Licence 2.0 Licence. See the following pages for more information: +* [Apache Licence 2.0](LICENSE) diff --git a/StenoByte_Core.c b/includes/StenoByte_Core.c similarity index 99% rename from StenoByte_Core.c rename to includes/StenoByte_Core.c index fcb248b..1d4121b 100644 --- a/StenoByte_Core.c +++ b/includes/StenoByte_Core.c @@ -51,7 +51,7 @@ void compute_byte() { */ void setup_subvalues_array() { for (int i = BITS_ARR_SIZE; i >= 0; i--) { - constexpr u_int8_t val = 0; + const u_int8_t val = 0; subvalues_arr[i] = val ^ 1 << i; } } diff --git a/StenoByte_Core.h b/includes/StenoByte_Core.h similarity index 100% rename from StenoByte_Core.h rename to includes/StenoByte_Core.h diff --git a/StenoByte_Helper.h b/includes/StenoByte_Helper.h similarity index 100% rename from StenoByte_Helper.h rename to includes/StenoByte_Helper.h diff --git a/StenoByte_Helper_for_Linux.c b/includes/StenoByte_Helper_for_Linux.c similarity index 100% rename from StenoByte_Helper_for_Linux.c rename to includes/StenoByte_Helper_for_Linux.c diff --git a/main.c b/main.c index 918027d..953185e 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ limitations under the License. */ -#include "StenoByte_Core.h" +#include "includes/StenoByte_Core.h" int main() {