-
Notifications
You must be signed in to change notification settings - Fork 3
/
caesar_cipher.c
146 lines (123 loc) · 4.53 KB
/
caesar_cipher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <furi.h>
#include <input/input.h>
#include <stdlib.h>
#include <gui/gui.h>
#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/text_input.h>
#include <gui/modules/text_box.h>
#define TEXT_BUFFER_SIZE 256
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
typedef struct {
ViewDispatcher* view_dispatcher;
TextInput* text_input;
TextBox* text_box;
char input[TEXT_BUFFER_SIZE];
char output[(TEXT_BUFFER_SIZE*26) + (26)]; // linebreaks
} CaesarState;
static void string_to_uppercase(char* input) {
int i;
for (i=0; input[i] != '\0'; i++) {
if (input[i] >= 'a' && input[i] <= 'z') {
input[i] = input[i] - 32;
} else {
input[i] = input[i];
}
}
}
static void build_output(char* input, char* output) {
int out = 0;
for ( int rot = 1; rot < 26; rot++) {
int in;
for(in = 0; input[in] != '\0'; in++){
if (input[in] >= 'A' && input[in] <= 'Z') {
output[out] = 65 + ( ((input[in] - 65) + rot ) % 26);
} else {
output[out] = input[in];
}
out++;
}
output[out]= '\n';
out++;
}
output[out]='\0';
}
static void text_input_callback(void* ctx) {
CaesarState* caesar_state = acquire_mutex((ValueMutex*)ctx, 25);
FURI_LOG_D("caesar_cipher", "Input text: %s", caesar_state->input);
// this is where we build the output.
string_to_uppercase(caesar_state->input);
FURI_LOG_D("caesar_cipher", "Upper text: %s", caesar_state->input);
build_output(caesar_state->input, caesar_state->output);
text_box_set_text(caesar_state->text_box, caesar_state->output);
view_dispatcher_switch_to_view(caesar_state->view_dispatcher, 1);
release_mutex((ValueMutex*)ctx, caesar_state);
}
static bool back_event_callback(void* ctx) {
const CaesarState* caesar_state = acquire_mutex((ValueMutex*)ctx, 25);
view_dispatcher_stop(caesar_state->view_dispatcher);
release_mutex((ValueMutex*)ctx, caesar_state);
return true;
}
static void caesar_cipher_state_init(CaesarState* const caesar_state) {
caesar_state->view_dispatcher = view_dispatcher_alloc();
caesar_state->text_input = text_input_alloc();
caesar_state->text_box = text_box_alloc();
text_box_set_font(caesar_state->text_box, TextBoxFontText);
}
static void caesar_cipher_state_free(CaesarState* const caesar_state) {
text_input_free(caesar_state->text_input);
text_box_free(caesar_state->text_box);
view_dispatcher_remove_view(caesar_state->view_dispatcher, 0);
view_dispatcher_remove_view(caesar_state->view_dispatcher, 1);
view_dispatcher_free(caesar_state->view_dispatcher);
free(caesar_state);
}
int32_t caesar_cipher_app() {
CaesarState* caesar_state = malloc(sizeof(CaesarState));
FURI_LOG_D("caesar_cipher", "Running caesar_cipher_state_init");
caesar_cipher_state_init(caesar_state);
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, caesar_state, sizeof(CaesarState))) {
FURI_LOG_E("caesar_cipher", "cannot create mutex\r\n");
free(caesar_state);
return 255;
}
FURI_LOG_D("caesar_cipher", "Assigning text input callback");
text_input_set_result_callback(
caesar_state->text_input,
text_input_callback,
&state_mutex,
caesar_state->input,
TEXT_BUFFER_SIZE,
//clear default text
true
);
text_input_set_header_text(caesar_state->text_input, "Input");
// Open GUI and register view_port
Gui* gui = furi_record_open("gui");
//gui_add_view_port(gui, view_port, GuiLayerFullscreen);
FURI_LOG_D("caesar_cipher", "Enabling view dispatcher queue");
view_dispatcher_enable_queue(caesar_state->view_dispatcher);
FURI_LOG_D("caesar_cipher", "Adding text input view to dispatcher");
view_dispatcher_add_view(caesar_state->view_dispatcher, 0, text_input_get_view(caesar_state->text_input));
view_dispatcher_add_view(caesar_state->view_dispatcher, 1, text_box_get_view(caesar_state->text_box));
FURI_LOG_D("caesar_cipher", "Attaching view dispatcher to GUI");
view_dispatcher_attach_to_gui(caesar_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
FURI_LOG_D("ceasar_cipher", "starting view dispatcher");
view_dispatcher_set_navigation_event_callback(caesar_state->view_dispatcher, back_event_callback);
view_dispatcher_set_event_callback_context(caesar_state->view_dispatcher, &state_mutex);
view_dispatcher_switch_to_view(caesar_state->view_dispatcher, 0);
view_dispatcher_run(caesar_state->view_dispatcher);
furi_record_close("gui");
delete_mutex(&state_mutex);
caesar_cipher_state_free(caesar_state);
return 0;
}