Skip to content

Commit 4636637

Browse files
Merge pull request #16 from RedHoodedWraith/15-refactor-code-to-make-it-ready-to-be-compiled-for-different-oss
15 refactor code to make it ready to be compiled for different operating systems
2 parents a55422f + aecfbc3 commit 4636637

File tree

9 files changed

+367
-196
lines changed

9 files changed

+367
-196
lines changed

CMakeLists.txt

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,41 @@ project(StenoByte_Prototype C)
33

44
set(CMAKE_C_STANDARD 23)
55

6-
find_package(PkgConfig REQUIRED)
7-
pkg_search_module(LIBEVDEV REQUIRED libevdev)
8-
9-
add_executable(StenoByte_Prototype main.c
10-
StenoByte_Helper.c
11-
StenoByte_Helper.h)
12-
target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS})
13-
target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES})
6+
# Linux Variant
7+
if (LINUX)
8+
# Finds Libevdev Library
9+
find_package(PkgConfig REQUIRED)
10+
pkg_search_module(LIBEVDEV REQUIRED libevdev)
11+
12+
# Prints Location of Libevdev Library
13+
message(STATUS "libevdev include dirs: ${LIBEVDEV_INCLUDE_DIRS}")
14+
message(STATUS "libevdev libraries: ${LIBEVDEV_LIBRARIES}")
15+
16+
## StenoByte Library for Linux Library
17+
add_library(StenoByte_Library STATIC
18+
includes/StenoByte_Helper_for_Linux.c
19+
includes/StenoByte_Core.c)
20+
target_include_directories(StenoByte_Library PRIVATE
21+
${LIBEVDEV_INCLUDE_DIRS}
22+
includes)
23+
target_link_libraries(StenoByte_Library PRIVATE ${LIBEVDEV_LIBRARIES})
24+
25+
# MacOS Variant
26+
elseif (APPLE)
27+
message(FATAL_ERROR "Unfortunately StenoByte is not yet compatible with MacOS,
28+
but support for MacOS is in the works!")
29+
30+
# Windows Variant
31+
elseif (WIN32)
32+
message(FATAL_ERROR "Unfortunately StenoByte is not yet compatible with Windows,
33+
but support for Windows is in the works!")
34+
35+
# Throws an error if system is incompatible
36+
else ()
37+
message(FATAL_ERROR "Unfortunately your system is not compatible with StenoByte")
38+
endif ()
39+
40+
# Main App
41+
add_executable(StenoByte_Prototype main.c)
42+
target_include_directories(StenoByte_Prototype PRIVATE ${LIBEVDEV_INCLUDE_DIRS} StenoByte_Library)
43+
target_link_libraries(StenoByte_Prototype PRIVATE ${LIBEVDEV_LIBRARIES} StenoByte_Library)

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
A stenotype inspired terminal app for typing out bytes quickly.
33

44
This program is designed to allow you to build up a byte using eight keys from your keyboard, with each key
5-
corresponding to a bit in a byte (which is made up of eight bits). When you press & hold down those keys, their
5+
corresponding to a bit in a byte (which is made up of eight bits). When you press and hold down those keys, their
66
associated bits are set to 1. When the keys are released or are not being pushed, their associated bits are set to 0.
77

8-
When you are ready to turn those bits into a byte, press the Space Bar and the application will convert those bits into
8+
When you are ready to turn those bits into a byte, press the Space Bar, and the application will convert those bits into
99
a byte and display it as a decimal.
1010

11-
This application is currently designed to work in Linux.
11+
This application is currently designed to work on Linux.
1212

1313
For the time-being, this app requires elevated privileges (e.g. `sudo`) to run.
1414

@@ -40,6 +40,11 @@ sudo apt -y install build-essential cmake git libevdev-dev
4040
sudo pacman -S gcc cmake git libevdev
4141
````
4242

43+
#### Install Dependencies on Alpine
44+
```shell
45+
apk add --update build-base 'cmake>3.30' libevdev libevdev-dev gdb
46+
```
47+
4348
### Step 2: Clone this repository
4449
Run:
4550
```shell
@@ -80,5 +85,5 @@ maintaining this repository.
8085
## License
8186
Copyright (C) 2025 Asami De Almeida
8287

83-
Released under the Apache License 2.0 License. See the following pages for more information:
84-
* [Apache License 2.0](LICENSE)
88+
Released under the Apache Licence 2.0 Licence. See the following pages for more information:
89+
* [Apache Licence 2.0](LICENSE)

dockerfiles/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Dockerfiles
2+
3+
This directory will contain directories of Dockerfiles used for building a local Linux sandbox environment.
4+
This is useful for developing for Linux while using a Mac or Windows machine.
5+
6+
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.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM alpine:latest
2+
3+
RUN apk add --update build-base 'cmake>3.30' libevdev libevdev-dev gdb

includes/StenoByte_Core.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
StenoByte: a stenotype inspired keyboard app for typing out bytes.
3+
4+
StenoByte_Core.c is the source file for implementing resources related to processing the Bits into a Byte and
5+
for producing the result as an output.
6+
7+
This file is intended to be Operating System agnostic and to be compiled for any Operating System.
8+
9+
Copyright 2025 Asami De Almeida
10+
11+
Licensed under the Apache License, Version 2.0 (the "License");
12+
you may not use this file except in compliance with the License.
13+
You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
*/
23+
24+
#include "StenoByte_Core.h"
25+
26+
// Arrays & Variables
27+
// Bit Array that contains the bits that forms a byte
28+
bool bit_arr[BITS_ARR_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; // ordered from b0 to b7 during initialisation
29+
char keys_arr[BITS_ARR_SIZE] = {';', 'L', 'K', 'J', 'F', 'D', 'S', 'A'}; // ';' = b0, 'L' = b1, ... 'A' = b7
30+
u_int8_t subvalues_arr[BITS_ARR_SIZE];
31+
bool ready_to_compute_byte = false; // the state for whether to convert the bit array into a byte and process it
32+
33+
u_int8_t current_byte = 0x00; // The byte last computed from the bit array
34+
35+
36+
// Methods & Functions
37+
38+
/*
39+
* Generates the Byte based on the bits in the array
40+
*/
41+
void compute_byte() {
42+
current_byte = 0x00; // Resets the Byte to zero
43+
for (int i = 0; i < BITS_ARR_SIZE; i++) {
44+
current_byte = current_byte ^ bit_arr[i] << i;
45+
}
46+
ready_to_compute_byte = false;
47+
}
48+
49+
/*
50+
* Sets up the sub-values array for labelling
51+
*/
52+
void setup_subvalues_array() {
53+
for (int i = BITS_ARR_SIZE; i >= 0; i--) {
54+
const u_int8_t val = 0;
55+
subvalues_arr[i] = val ^ 1 << i;
56+
}
57+
}
58+
59+
/*
60+
* Gets Byte Summary as a String (an array of chars)
61+
* Assumes msg has a minimum length of 35. Assumes value of current_byte will not exceed 255.
62+
*/
63+
void get_byte_summary(char* msg) {
64+
sprintf(msg + strlen(msg), "Last Computed Byte as decimal: %d\n", current_byte); // Prints between 33 and 35 chars
65+
}
66+
67+
/*
68+
* Prints the Byte Summary
69+
*/
70+
void print_byte_summary() {
71+
char msg[35];
72+
get_byte_summary(msg);
73+
printf("%s", msg);
74+
}
75+
76+
/*
77+
* Prints the current state of the Bit Array
78+
* TODO: The repeated for loops could probably be simplified into a dedicated method
79+
*/
80+
void print_bit_arr_summary() {
81+
char msg[654] = "";
82+
83+
sprintf(msg + strlen(msg), "\nBits in Array:\n"); // Prints 16 chars
84+
sprintf(msg + strlen(msg), "\tBit Value:\t| "); // Prints 14 chars
85+
for (int i = 7; i >= 0; i--) { // Repeats 8 times
86+
sprintf(msg + strlen(msg), "\t%d\t|", bit_arr[i]); // Prints between 4 and 6 chars
87+
}
88+
sprintf(msg + strlen(msg), "\n"); // Prints 1 char
89+
90+
for (int i=0; i<24+16*BITS_ARR_SIZE; i++) { // Repeats 24+(16*8) times, which is 152
91+
sprintf(msg + strlen(msg), "-"); // Prints 1 char
92+
}
93+
94+
sprintf(msg + strlen(msg), "\n\tSub-Value:\t|"); // Prints 14 chars
95+
for (int i = 7; i >= 0; i--) { // Repeats 8 times
96+
sprintf(msg + strlen(msg), "\t[%d]\t|", subvalues_arr[i]); // Prints between 6 and 8 chars
97+
}
98+
99+
sprintf(msg + strlen(msg), "\n\tBit Index:\t|"); // Prints 14 chars
100+
for (int i = 7; i >= 0; i--) { // Repeats 8 times
101+
sprintf(msg + strlen(msg), "\t[b%d]\t|", i); // Prints 7 chars
102+
}
103+
sprintf(msg + strlen(msg), "\n\tKey:\t\t|"); // Prints 9 times
104+
105+
for (int i = 7; i >= 0; i--) { // Repeats 8 times
106+
sprintf(msg + strlen(msg), "\t[%c]\t|", keys_arr[i]); // Prints 6 chars
107+
}
108+
sprintf(msg + strlen(msg), "\n"); // Prints 1 char
109+
get_byte_summary(msg); // Prints between 33 and 35 chars
110+
sprintf(msg + strlen(msg), "\nPress & Hold the keys corresponding to the bits in the"
111+
" byte you would like to set to 1."); // Prints 88 chars
112+
sprintf(msg + strlen(msg), "\nBits will be 0 if keys are not pressed."); // Prints 40 chars
113+
sprintf(msg + strlen(msg), "\nPress SPACE BAR to compute Byte\t\t|\t"
114+
"Press ESC to exit\n"); // Prints 53 chars
115+
116+
printf("%s", msg);
117+
}
Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/**
22
StenoByte: a stenotype inspired keyboard app for typing out bytes.
33
4-
Copyright 2025 Asami De Almeida
4+
StenoByte_Core.h is the header file for defining resources related to processing the Bits into a Byte and
5+
for producing the result as an output.
6+
7+
Copyright 2025 Asami De Almeida
58
69
Licensed under the Apache License, Version 2.0 (the "License");
710
you may not use this file except in compliance with the License.
@@ -16,57 +19,34 @@
1619
limitations under the License.
1720
*/
1821

19-
#ifndef STENOBYTE_HELPER_H
20-
#define STENOBYTE_HELPER_H
21-
22-
#include <linux/input.h>
23-
#include <libevdev/libevdev.h>
24-
#include <fcntl.h>
25-
#include <termios.h>
26-
#include <unistd.h>
27-
#include <stdio.h>
28-
#include <stdbool.h>
22+
#ifndef STENOBYTE_CORE_H
23+
#define STENOBYTE_CORE_H
2924

30-
// Key Press States
31-
#define EV_KEY_RELEASED 0
32-
#define EV_KEY_PRESSED 1
33-
#define EV_KEY_REPEATED 2
25+
#include "StenoByte_Helper.h"
3426

3527
// Number of Bits in the Bit Array (should be 8)
3628
# define BITS_ARR_SIZE 8
3729

38-
extern struct termios original_terminal_settings; // Termios Struct to store original terminal settings
39-
4030
// Arrays & Variables
4131
// Bit Array that contains the bits that forms a byte
4232
extern bool bit_arr[BITS_ARR_SIZE]; // ordered from b0 to b7 during initialisation
4333
extern char keys_arr[BITS_ARR_SIZE]; // ';' = b0, 'L' = b1, ... 'A' = b7
4434
extern u_int8_t subvalues_arr[BITS_ARR_SIZE];
4535
extern bool ready_to_compute_byte; // the state for whether to convert the bit array into a byte and process it
46-
4736
extern u_int8_t current_byte; // The byte last computed from the bit array
4837

4938

50-
// Methods & Functions
51-
52-
bool is_valid_key(int key_code);
53-
54-
void update_bit_arr(int key_code, bool new_state);
39+
// Externally Declared Methods & Functions (expected to be declared & defined StenoByte_Helper files)
40+
extern void update_bit_arr(int key_code, bool new_state);
41+
extern int setup_stenobyte();
42+
extern void run_stenobyte();
43+
extern void end_stenobyte();
5544

45+
// Methods & Functions
5646
void compute_byte();
57-
5847
void setup_subvalues_array();
59-
60-
void print_event_summary(const struct input_event* current_event);
61-
48+
void get_byte_summary(char* msg);
6249
void print_byte_summary();
63-
6450
void print_bit_arr_summary();
6551

66-
void process_key_presses(const struct input_event* current_event);
67-
68-
void disable_echo();
69-
70-
void restore_terminal();
71-
72-
#endif //STENOBYTE_HELPER_H
52+
#endif //STENOBYTE_CORE_H

includes/StenoByte_Helper.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
StenoByte: a stenotype inspired keyboard app for typing out bytes.
3+
4+
StenoByte_Helper.h is the header file for defining resources for Keyboard Event Reading for a range of
5+
Operating Systems.
6+
7+
TODO: Implement conditional declarations that will be dependent on the target OS.
8+
9+
Copyright 2025 Asami De Almeida
10+
11+
Licensed under the Apache License, Version 2.0 (the "License");
12+
you may not use this file except in compliance with the License.
13+
You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
*/
23+
24+
#ifndef STENOBYTE_HELPER_H
25+
#define STENOBYTE_HELPER_H
26+
27+
#include <linux/input.h>
28+
#include <libevdev/libevdev.h>
29+
#include <fcntl.h>
30+
#include <termios.h>
31+
#include <unistd.h>
32+
#include <stdio.h>
33+
#include <stdbool.h>
34+
#include <string.h>
35+
36+
// Key Press States
37+
#define EV_KEY_RELEASED 0
38+
#define EV_KEY_PRESSED 1
39+
#define EV_KEY_REPEATED 2
40+
41+
// Externally Declared Structs and Variables (expected to be declared and implemented in dependencies)
42+
extern struct libevdev *keyboard_device; // Struct to store the evdev device
43+
extern struct termios original_terminal_settings; // Termios Struct to store original terminal settings
44+
extern const int event_file_device;
45+
46+
47+
// Externally Declared Methods & Functions
48+
// (expected to be declared & defined in dependent libraries or in StenoByte_Core files)
49+
extern void setup_subvalues_array(); // StenoByte_Core.h/c
50+
extern void compute_byte();
51+
extern void print_bit_arr_summary();
52+
extern void update_bit_arr(int key_code, bool new_state);
53+
54+
55+
// Methods & Functions
56+
int setup_stenobyte();
57+
void update_bit_arr(int key_code, bool new_state);
58+
void run_stenobyte();
59+
void end_stenobyte();
60+
void process_key_presses(const struct input_event* current_event);
61+
bool is_valid_key(int key_code);
62+
void print_event_summary(const struct input_event* current_event);
63+
void disable_echo();
64+
void restore_terminal();
65+
66+
#endif //STENOBYTE_HELPER_H

0 commit comments

Comments
 (0)