Skip to content

Commit 2c72217

Browse files
authored
Closing #162 and prepared for OFW catalog (#163)
1 parent f9ce761 commit 2c72217

File tree

8 files changed

+93
-343
lines changed

8 files changed

+93
-343
lines changed

CHANGELOG.md totp/.ofwcatalog/CHANGELOG.md

+16-164
Large diffs are not rendered by default.
File renamed without changes.

totp/application.fam

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ App(
1919
fap_author="Alexander Kopachov (@akopachov)",
2020
fap_description="Software-based TOTP authenticator for Flipper Zero device",
2121
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",
22-
fap_category="Misc",
22+
fap_category="Tools",
2323
fap_icon_assets="images",
2424
fap_icon="totp_10px.png",
2525
fap_private_libs=[
+36-64
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,53 @@
11
#include "totp_input_text.h"
2-
#include <gui/view_i.h>
32

4-
void view_draw(View* view, Canvas* canvas) {
5-
furi_assert(view);
6-
if(view->draw_callback) {
7-
void* data = view_get_model(view);
8-
view->draw_callback(canvas, data);
9-
view_unlock_model(view);
10-
}
11-
}
3+
#include <gui/view_dispatcher.h>
4+
#include <gui/view.h>
5+
#include <gui/modules/text_input.h>
126

13-
bool view_input(View* view, InputEvent* event) {
14-
furi_assert(view);
15-
if(view->input_callback) {
16-
return view->input_callback(event, view->context);
17-
} else {
18-
return false;
19-
}
20-
}
7+
typedef struct {
8+
InputTextResult* result;
9+
ViewDispatcher* view_dispatcher;
10+
} InputTextContext;
2111

22-
void view_unlock_model(View* view) {
23-
furi_assert(view);
24-
if(view->model_type == ViewModelTypeLocking) {
25-
ViewModelLocking* model = (ViewModelLocking*)(view->model);
26-
furi_check(furi_mutex_release(model->mutex) == FuriStatusOk);
27-
}
12+
static void commit_text_input_callback(void* ctx) {
13+
InputTextContext* context = ctx;
14+
context->result->user_input_length = strnlen(context->result->user_input, INPUT_BUFFER_SIZE);
15+
context->result->success = true;
16+
view_dispatcher_stop(context->view_dispatcher);
2817
}
2918

30-
static void commit_text_input_callback(void* context) {
31-
InputTextSceneState* text_input_state = (InputTextSceneState*)context;
32-
if(text_input_state->callback != NULL) {
33-
InputTextSceneCallbackResult* result = malloc(sizeof(InputTextSceneCallbackResult));
34-
furi_check(result != NULL);
35-
result->user_input_length =
36-
strnlen(text_input_state->text_input_buffer, INPUT_BUFFER_SIZE);
37-
result->user_input = malloc(result->user_input_length + 1);
38-
furi_check(result->user_input != NULL);
39-
result->callback_data = text_input_state->callback_data;
40-
strlcpy(
41-
result->user_input,
42-
text_input_state->text_input_buffer,
43-
result->user_input_length + 1);
44-
text_input_state->callback(result);
45-
}
19+
static bool back_event_callback(void* ctx) {
20+
InputTextContext* context = ctx;
21+
context->result->success = false;
22+
view_dispatcher_stop(context->view_dispatcher);
23+
return false;
4624
}
4725

48-
InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context) {
49-
InputTextSceneState* text_input_state = malloc(sizeof(InputTextSceneState));
50-
furi_check(text_input_state != NULL);
51-
text_input_state->text_input = text_input_alloc();
52-
text_input_state->text_input_view = text_input_get_view(text_input_state->text_input);
53-
text_input_state->callback = context->callback;
54-
text_input_state->callback_data = context->callback_data;
55-
text_input_set_header_text(text_input_state->text_input, context->header_text);
26+
void totp_input_text(Gui* gui, const char* header_text, InputTextResult* result) {
27+
ViewDispatcher* view_dispatcher = view_dispatcher_alloc();
28+
TextInput* text_input = text_input_alloc();
29+
InputTextContext context = {.result = result, .view_dispatcher = view_dispatcher};
30+
text_input_set_header_text(text_input, header_text);
5631
text_input_set_result_callback(
57-
text_input_state->text_input,
32+
text_input,
5833
commit_text_input_callback,
59-
text_input_state,
60-
&text_input_state->text_input_buffer[0],
34+
&context,
35+
result->user_input,
6136
INPUT_BUFFER_SIZE,
6237
true);
63-
return text_input_state;
64-
}
6538

66-
void totp_input_text_render(Canvas* const canvas, InputTextSceneState* text_input_state) {
67-
view_draw(text_input_state->text_input_view, canvas);
68-
}
39+
view_dispatcher_enable_queue(view_dispatcher);
40+
view_dispatcher_add_view(view_dispatcher, 0, text_input_get_view(text_input));
6941

70-
bool totp_input_text_handle_event(PluginEvent* const event, InputTextSceneState* text_input_state) {
71-
if(event->type == EventTypeKey) {
72-
view_input(text_input_state->text_input_view, &event->input);
73-
}
42+
view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen);
7443

75-
return true;
76-
}
44+
view_dispatcher_set_navigation_event_callback(view_dispatcher, &back_event_callback);
45+
view_dispatcher_set_event_callback_context(view_dispatcher, &context);
46+
view_dispatcher_switch_to_view(view_dispatcher, 0);
47+
48+
view_dispatcher_run(view_dispatcher);
7749

78-
void totp_input_text_free(InputTextSceneState* state) {
79-
text_input_free(state->text_input);
80-
free(state);
50+
view_dispatcher_remove_view(view_dispatcher, 0);
51+
view_dispatcher_free(view_dispatcher);
52+
text_input_free(text_input);
8153
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
#pragma once
22

33
#include <gui/gui.h>
4-
#include <gui/view.h>
5-
#include <gui/modules/text_input.h>
6-
#include "../../../types/plugin_state.h"
7-
#include "../../../types/plugin_event.h"
84

95
#define INPUT_BUFFER_SIZE (255)
106

117
typedef struct {
12-
char* user_input;
8+
char user_input[INPUT_BUFFER_SIZE];
139
size_t user_input_length;
14-
void* callback_data;
15-
} InputTextSceneCallbackResult;
10+
bool success;
11+
} InputTextResult;
1612

17-
typedef void (*InputTextSceneCallback)(InputTextSceneCallbackResult* result);
18-
19-
typedef struct {
20-
InputTextSceneCallback callback;
21-
char* header_text;
22-
void* callback_data;
23-
} InputTextSceneContext;
24-
25-
typedef struct {
26-
TextInput* text_input;
27-
View* text_input_view;
28-
char text_input_buffer[INPUT_BUFFER_SIZE];
29-
InputTextSceneCallback callback;
30-
void* callback_data;
31-
} InputTextSceneState;
32-
33-
InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context);
34-
void totp_input_text_render(Canvas* const canvas, InputTextSceneState* text_input_state);
35-
bool totp_input_text_handle_event(PluginEvent* const event, InputTextSceneState* text_input_state);
36-
void totp_input_text_free(InputTextSceneState* state);
13+
void totp_input_text(Gui* gui, const char* header_text, InputTextResult* result);

totp/ui/scenes/add_new_token/totp_scene_add_new_token.c

+33-84
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ typedef struct {
3333
size_t token_secret_length;
3434
bool saved;
3535
Control selected_control;
36-
InputTextSceneContext* token_name_input_context;
37-
InputTextSceneContext* token_secret_input_context;
38-
InputTextSceneState* input_state;
39-
bool text_input_mode;
4036
int16_t screen_y_offset;
4137
TokenHashAlgo algo;
4238
uint8_t digits_count_index;
@@ -51,24 +47,6 @@ struct TotpAddContext {
5147

5248
enum TotpIteratorUpdateTokenResultsEx { TotpIteratorUpdateTokenResultInvalidSecret = 1 };
5349

54-
static void on_token_name_user_comitted(InputTextSceneCallbackResult* result) {
55-
SceneState* scene_state = result->callback_data;
56-
free(scene_state->token_name);
57-
scene_state->token_name = result->user_input;
58-
scene_state->token_name_length = result->user_input_length;
59-
scene_state->text_input_mode = false;
60-
free(result);
61-
}
62-
63-
static void on_token_secret_user_comitted(InputTextSceneCallbackResult* result) {
64-
SceneState* scene_state = result->callback_data;
65-
free(scene_state->token_secret);
66-
scene_state->token_secret = result->user_input;
67-
scene_state->token_secret_length = result->user_input_length;
68-
scene_state->text_input_mode = false;
69-
free(result);
70-
}
71-
7250
static void update_duration_text(SceneState* scene_state) {
7351
furi_string_printf(scene_state->duration_text, "%d sec.", scene_state->duration);
7452
}
@@ -95,6 +73,26 @@ static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* tokenInfo, con
9573
return TotpIteratorUpdateTokenResultSuccess;
9674
}
9775

76+
static void ask_user_input(
77+
const PluginState* plugin_state,
78+
const char* header,
79+
char** user_input,
80+
size_t* user_input_length) {
81+
InputTextResult input_result;
82+
if(*user_input != NULL) {
83+
strlcpy(input_result.user_input, *user_input, INPUT_BUFFER_SIZE);
84+
}
85+
86+
totp_input_text(plugin_state->gui, header, &input_result);
87+
if(input_result.success) {
88+
if(*user_input != NULL) {
89+
free(*user_input);
90+
}
91+
*user_input = strdup(input_result.user_input);
92+
*user_input_length = input_result.user_input_length;
93+
}
94+
}
95+
9896
void totp_scene_add_new_token_activate(PluginState* plugin_state) {
9997
SceneState* scene_state = malloc(sizeof(SceneState));
10098
furi_check(scene_state != NULL);
@@ -104,34 +102,17 @@ void totp_scene_add_new_token_activate(PluginState* plugin_state) {
104102
scene_state->token_secret = "Secret";
105103
scene_state->token_secret_length = strlen(scene_state->token_secret);
106104

107-
scene_state->token_name_input_context = malloc(sizeof(InputTextSceneContext));
108-
furi_check(scene_state->token_name_input_context != NULL);
109-
scene_state->token_name_input_context->header_text = "Enter token name";
110-
scene_state->token_name_input_context->callback_data = scene_state;
111-
scene_state->token_name_input_context->callback = on_token_name_user_comitted;
112-
113-
scene_state->token_secret_input_context = malloc(sizeof(InputTextSceneContext));
114-
furi_check(scene_state->token_secret_input_context != NULL);
115-
scene_state->token_secret_input_context->header_text = "Enter token secret";
116-
scene_state->token_secret_input_context->callback_data = scene_state;
117-
scene_state->token_secret_input_context->callback = on_token_secret_user_comitted;
118-
119105
scene_state->screen_y_offset = 0;
120106

121107
scene_state->digits_count_index = 1;
122108

123-
scene_state->input_state = NULL;
124109
scene_state->duration = TOTP_TOKEN_DURATION_DEFAULT;
125110
scene_state->duration_text = furi_string_alloc();
126111
update_duration_text(scene_state);
127112
}
128113

129-
void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_state) {
130-
SceneState* scene_state = plugin_state->current_scene_state;
131-
if(scene_state->text_input_mode) {
132-
totp_input_text_render(canvas, scene_state->input_state);
133-
return;
134-
}
114+
void totp_scene_add_new_token_render(Canvas* const canvas, const PluginState* plugin_state) {
115+
const SceneState* scene_state = plugin_state->current_scene_state;
135116

136117
ui_control_text_box_render(
137118
canvas,
@@ -195,31 +176,13 @@ void update_screen_y_offset(SceneState* scene_state) {
195176
}
196177
}
197178

198-
bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState* plugin_state) {
179+
bool totp_scene_add_new_token_handle_event(const PluginEvent* const event, PluginState* plugin_state) {
199180
if(event->type != EventTypeKey) {
200181
return true;
201182
}
202183

203184
SceneState* scene_state = plugin_state->current_scene_state;
204185

205-
if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
206-
if(scene_state->text_input_mode) {
207-
scene_state->text_input_mode = false;
208-
} else {
209-
return false;
210-
}
211-
}
212-
213-
if(scene_state->text_input_mode) {
214-
if(event->input.type == InputTypeShort && event->input.key == InputKeyBack) {
215-
PluginEvent long_back_cb_evt = {
216-
.type = event->type, .input.key = InputKeyBack, .input.type = InputTypeLong};
217-
return totp_input_text_handle_event(&long_back_cb_evt, scene_state->input_state);
218-
}
219-
220-
return totp_input_text_handle_event(event, scene_state->input_state);
221-
}
222-
223186
if(event->input.type == InputTypePress) {
224187
switch(event->input.key) {
225188
case InputKeyUp:
@@ -277,22 +240,18 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
277240
} else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
278241
switch(scene_state->selected_control) {
279242
case TokenNameTextBox:
280-
if(scene_state->input_state != NULL) {
281-
totp_input_text_free(scene_state->input_state);
282-
}
283-
scene_state->input_state =
284-
totp_input_text_activate(scene_state->token_name_input_context);
285-
286-
scene_state->text_input_mode = true;
243+
ask_user_input(
244+
plugin_state,
245+
"Token name",
246+
&scene_state->token_name,
247+
&scene_state->token_name_length);
287248
break;
288249
case TokenSecretTextBox:
289-
if(scene_state->input_state != NULL) {
290-
totp_input_text_free(scene_state->input_state);
291-
}
292-
scene_state->input_state =
293-
totp_input_text_activate(scene_state->token_secret_input_context);
294-
295-
scene_state->text_input_mode = true;
250+
ask_user_input(
251+
plugin_state,
252+
"Token secret",
253+
&scene_state->token_secret,
254+
&scene_state->token_secret_length);
296255
break;
297256
case TokenAlgoSelect:
298257
break;
@@ -344,18 +303,8 @@ void totp_scene_add_new_token_deactivate(PluginState* plugin_state) {
344303
free(scene_state->token_name);
345304
free(scene_state->token_secret);
346305

347-
free(scene_state->token_name_input_context->header_text);
348-
free(scene_state->token_name_input_context);
349-
350-
free(scene_state->token_secret_input_context->header_text);
351-
free(scene_state->token_secret_input_context);
352-
353306
furi_string_free(scene_state->duration_text);
354307

355-
if(scene_state->input_state != NULL) {
356-
totp_input_text_free(scene_state->input_state);
357-
}
358-
359308
free(plugin_state->current_scene_state);
360309
plugin_state->current_scene_state = NULL;
361310
}

totp/ui/scenes/add_new_token/totp_scene_add_new_token.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#include "../../../types/plugin_event.h"
66

77
void totp_scene_add_new_token_activate(PluginState* plugin_state);
8-
void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_state);
9-
bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState* plugin_state);
8+
void totp_scene_add_new_token_render(Canvas* const canvas, const PluginState* plugin_state);
9+
bool totp_scene_add_new_token_handle_event(const PluginEvent* const event, PluginState* plugin_state);
1010
void totp_scene_add_new_token_deactivate(PluginState* plugin_state);

totp/workers/bt_type_code/bt_type_code.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "bt_type_code.h"
2+
#include <furi_hal_bt.h>
23
#include <furi_hal_bt_hid.h>
34
#include <furi_hal_version.h>
4-
#include <bt/bt_service/bt_i.h>
55
#include <furi/core/thread.h>
66
#include <furi/core/mutex.h>
77
#include <furi/core/string.h>

0 commit comments

Comments
 (0)