-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
cligui_main.c
134 lines (117 loc) · 4.74 KB
/
cligui_main.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
#include "cligui_main_i.h"
#include "cli_control.h"
#include "text_input.h"
#include "console_output.h"
static bool cligui_custom_event_cb(void* context, uint32_t event) {
UNUSED(event);
CliguiApp* app = context;
UNUSED(app);
return true;
}
static bool cligui_back_event_cb(void* context) {
CliguiApp* app = context;
UNUSED(app);
return true;
}
static void cligui_tick_event_cb(void* context) {
CliguiApp* app = context;
size_t available = furi_stream_buffer_bytes_available(app->data->streams.app_rx);
for(size_t i = 0; i < available; i++) {
char c = 0;
size_t len = furi_stream_buffer_receive(app->data->streams.app_rx, &c, 1, 100);
if(len > 0) {
furi_string_push_back(app->text_box_store, c);
}
}
if(available > 0) {
text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
}
// Set input header stuff
size_t len = furi_string_size(app->text_box_store);
size_t idx = len - 2;
while(idx > 0) {
if(furi_string_get_char(app->text_box_store, idx) == '\n') {
idx++;
break;
}
idx--;
}
text_input_set_header_text(app->text_input, furi_string_get_cstr(app->text_box_store) + idx);
UNUSED(app);
}
volatile bool persistent_exit = false;
static void input_callback(const void* event_ptr, void* context) {
InputEvent* event = (InputEvent*)event_ptr;
CliguiApp* app = context;
if(event->type == InputTypeLong && event->key == InputKeyBack) {
persistent_exit = false;
view_dispatcher_stop(app->view_dispatcher);
}
if(event->type == InputTypeLong && event->key == InputKeyOk) {
if(app->data->state == ViewConsoleOutput) {
persistent_exit = true;
view_dispatcher_stop(app->view_dispatcher);
}
}
if(app->data->state == ViewTextInput) {
text_input_input_handler(app, event);
} else {
console_output_input_handler(app, event);
}
}
int32_t cligui_main(void* p) {
UNUSED(p);
CliguiApp* cligui = malloc(sizeof(CliguiApp));
cligui->data = malloc(sizeof(CliguiData));
clicontrol_hijack(512, 512);
cligui->data->streams.app_tx = cli_rx_stream;
cligui->data->streams.app_rx = cli_tx_stream;
cligui->gui = furi_record_open(RECORD_GUI);
cligui->view_dispatcher = view_dispatcher_alloc();
FuriPubSub* input_events = furi_record_open(RECORD_INPUT_EVENTS);
FuriPubSubSubscription* input_events_sub =
furi_pubsub_subscribe(input_events, input_callback, (void*)cligui);
view_dispatcher_enable_queue(cligui->view_dispatcher);
view_dispatcher_set_event_callback_context(cligui->view_dispatcher, cligui);
view_dispatcher_set_custom_event_callback(cligui->view_dispatcher, cligui_custom_event_cb);
view_dispatcher_set_navigation_event_callback(cligui->view_dispatcher, cligui_back_event_cb);
view_dispatcher_set_tick_event_callback(cligui->view_dispatcher, cligui_tick_event_cb, 100);
view_dispatcher_attach_to_gui(
cligui->view_dispatcher, cligui->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_send_to_front(cligui->view_dispatcher);
cligui->text_box = text_box_alloc();
view_dispatcher_add_view(
cligui->view_dispatcher, ViewConsoleOutput, text_box_get_view(cligui->text_box));
cligui->text_box_store = furi_string_alloc();
furi_string_reserve(cligui->text_box_store, TEXT_BOX_STORE_SIZE);
furi_string_set_char(cligui->text_box_store, 0, 0);
text_box_set_text(cligui->text_box, furi_string_get_cstr(cligui->text_box_store));
text_box_set_focus(cligui->text_box, TextBoxFocusEnd);
cligui->text_input = text_input_alloc();
text_input_set_result_callback(
cligui->text_input,
text_input_result_callback,
cligui,
cligui->text_input_store,
TEXT_INPUT_STORE_SIZE,
true);
text_input_show_illegal_symbols(cligui->text_input, true);
view_dispatcher_add_view(
cligui->view_dispatcher, ViewTextInput, text_input_get_view(cligui->text_input));
view_dispatcher_switch_to_view(cligui->view_dispatcher, ViewTextInput);
cligui->data->state = ViewTextInput;
view_dispatcher_run(cligui->view_dispatcher);
view_dispatcher_remove_view(cligui->view_dispatcher, ViewConsoleOutput);
view_dispatcher_remove_view(cligui->view_dispatcher, ViewTextInput);
text_box_free(cligui->text_box);
furi_string_free(cligui->text_box_store);
text_input_free(cligui->text_input);
view_dispatcher_free(cligui->view_dispatcher);
clicontrol_unhijack(persistent_exit);
furi_pubsub_unsubscribe(input_events, input_events_sub);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_INPUT_EVENTS);
free(cligui->data);
free(cligui);
return 0;
}