-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxremote_learn.c
410 lines (326 loc) · 14.9 KB
/
xremote_learn.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*!
* @file flipper-xremote/xremote_learn.c
@license This project is released under the GNU GPLv3 License
* @copyright (c) 2023 Sandro Kalatozishvili ([email protected])
*
* @brief Functionality to read and store input from new remote.
*/
#include "xremote_learn.h"
#include "views/xremote_learn_view.h"
struct XRemoteLearnContext {
/* XRemote context */
XRemoteSignalReceiver* ir_receiver;
XRemoteAppContext* app_ctx;
XRemoteView* signal_view;
XRemoteViewID curr_view;
XRemoteViewID prev_view;
/* Main infrared app context */
InfraredRemote* ir_remote;
InfraredSignal* ir_signal;
/* User interactions */
TextInput* text_input;
DialogEx* dialog_ex;
/* User context and clear callback */
XRemoteClearCallback on_clear;
void* context;
/* Private control flags */
char text_store[XREMOTE_APP_TEXT_MAX + 1];
uint8_t current_button;
bool finish_learning;
bool stop_receiver;
bool is_dirty;
};
void xremote_learn_send_event(XRemoteLearnContext* learn_ctx, XRemoteEvent event) {
xremote_app_assert_void(learn_ctx);
if(event == XRemoteEventSignalFinish) learn_ctx->finish_learning = true;
ViewDispatcher* view_disp = learn_ctx->app_ctx->view_dispatcher;
view_dispatcher_send_custom_event(view_disp, event);
}
const char* xremote_learn_get_curr_button_name(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, NULL);
return xremote_button_get_name(learn_ctx->current_button);
}
int xremote_learn_get_curr_button_index(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, -1);
return learn_ctx->current_button;
}
InfraredRemote* xremote_learn_get_ir_remote(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, NULL);
return learn_ctx->ir_remote;
}
InfraredSignal* xremote_learn_get_ir_signal(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, NULL);
return learn_ctx->ir_signal;
}
XRemoteSignalReceiver* xremote_learn_get_ir_receiver(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, NULL);
return learn_ctx->ir_receiver;
}
XRemoteAppContext* xremote_learn_get_app_context(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, NULL);
return learn_ctx->app_ctx;
}
bool xremote_learn_has_buttons(XRemoteLearnContext* learn_ctx) {
xremote_app_assert(learn_ctx, false);
return infrared_remote_get_button_count(learn_ctx->ir_remote) > 0;
}
static void xremote_learn_switch_to_view(XRemoteLearnContext* learn_ctx, XRemoteViewID view_id) {
xremote_app_assert_void(learn_ctx);
learn_ctx->prev_view = learn_ctx->curr_view;
learn_ctx->curr_view = view_id;
ViewDispatcher* view_disp = learn_ctx->app_ctx->view_dispatcher;
view_dispatcher_switch_to_view(view_disp, view_id);
}
static void xremote_learn_context_rx_stop(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
learn_ctx->stop_receiver = true;
xremote_signal_receiver_stop(learn_ctx->ir_receiver);
}
static void xremote_learn_context_rx_start(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
learn_ctx->finish_learning = false;
learn_ctx->stop_receiver = false;
xremote_signal_receiver_start(learn_ctx->ir_receiver);
}
static void xremote_learn_exit_dialog_free(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
xremote_app_assert_void(learn_ctx->dialog_ex);
ViewDispatcher* view_disp = learn_ctx->app_ctx->view_dispatcher;
view_dispatcher_remove_view(view_disp, XRemoteViewDialogExit);
dialog_ex_free(learn_ctx->dialog_ex);
learn_ctx->dialog_ex = NULL;
}
static void xremote_learn_exit_dialog_alloc(
XRemoteLearnContext* learn_ctx,
DialogExResultCallback callback) {
xremote_app_assert_void(learn_ctx);
xremote_learn_exit_dialog_free(learn_ctx);
ViewDispatcher* view_disp = learn_ctx->app_ctx->view_dispatcher;
const char* dialog_text = "All unsaved data\nwill be lost!";
const char* header_text = "Exit to XRemote Menu?";
learn_ctx->dialog_ex = dialog_ex_alloc();
View* view = dialog_ex_get_view(learn_ctx->dialog_ex);
view_dispatcher_add_view(view_disp, XRemoteViewDialogExit, view);
dialog_ex_set_header(learn_ctx->dialog_ex, header_text, 64, 11, AlignCenter, AlignTop);
dialog_ex_set_text(learn_ctx->dialog_ex, dialog_text, 64, 25, AlignCenter, AlignTop);
dialog_ex_set_icon(learn_ctx->dialog_ex, 0, 0, NULL);
dialog_ex_set_left_button_text(learn_ctx->dialog_ex, "Exit");
dialog_ex_set_center_button_text(learn_ctx->dialog_ex, "Save");
dialog_ex_set_right_button_text(learn_ctx->dialog_ex, "Stay");
dialog_ex_set_result_callback(learn_ctx->dialog_ex, callback);
dialog_ex_set_context(learn_ctx->dialog_ex, learn_ctx);
}
static uint32_t xremote_learn_view_exit_callback(void* context) {
UNUSED(context);
return XRemoteViewLearn;
}
static void xremote_learn_dialog_exit_callback(DialogExResult result, void* context) {
XRemoteLearnContext* learn_ctx = (XRemoteLearnContext*)context;
xremote_learn_switch_to_view(learn_ctx, XRemoteViewSubmenu);
if(result == DialogExResultLeft)
xremote_learn_send_event(learn_ctx, XRemoteEventSignalExit);
else if(result == DialogExResultRight)
xremote_learn_send_event(learn_ctx, XRemoteEventSignalRetry);
else if(result == DialogExResultCenter)
xremote_learn_send_event(learn_ctx, XRemoteEventSignalFinish);
}
static uint32_t xremote_learn_text_input_exit_callback(void* context) {
TextInput* text_input = context;
XRemoteLearnContext* learn_ctx;
XRemoteEvent event;
learn_ctx = text_input_get_validator_callback_context(text_input);
xremote_app_assert(learn_ctx, XRemoteViewSubmenu);
event = learn_ctx->prev_view == XRemoteViewSignal ? XRemoteEventSignalReceived :
XRemoteEventSignalRetry;
if(learn_ctx->current_button >= XREMOTE_BUTTON_COUNT)
learn_ctx->current_button = XREMOTE_BUTTON_COUNT - 1;
learn_ctx->finish_learning = false;
xremote_learn_send_event(learn_ctx, event);
return XRemoteViewTextInput;
}
static void xremote_learn_text_input_callback(void* context) {
xremote_app_assert_void(context);
XRemoteLearnContext* learn_ctx = context;
if(learn_ctx->is_dirty) {
const char* name = xremote_learn_get_curr_button_name(learn_ctx);
if(!infrared_remote_get_button_by_name(learn_ctx->ir_remote, name)) {
InfraredSignal* signal = xremote_learn_get_ir_signal(learn_ctx);
infrared_remote_push_button(learn_ctx->ir_remote, name, signal);
}
learn_ctx->is_dirty = false;
}
if(infrared_remote_get_button_count(learn_ctx->ir_remote) &&
learn_ctx->text_store[0] != '\0') {
char output_file[256];
snprintf(
output_file,
sizeof(output_file),
"%s/%s%s",
XREMOTE_APP_FOLDER,
learn_ctx->text_store,
XREMOTE_APP_EXTENSION);
infrared_remote_set_name(learn_ctx->ir_remote, learn_ctx->text_store);
infrared_remote_set_path(learn_ctx->ir_remote, output_file);
infrared_remote_store(learn_ctx->ir_remote);
infrared_remote_reset(learn_ctx->ir_remote);
}
xremote_learn_send_event(learn_ctx, XRemoteEventSignalExit);
}
static void xremote_learn_signal_callback(void* context, InfraredSignal* signal) {
XRemoteLearnContext* learn_ctx = context;
xremote_app_assert_void(!learn_ctx->stop_receiver);
xremote_app_assert_void(!learn_ctx->finish_learning);
learn_ctx->stop_receiver = true;
learn_ctx->is_dirty = true;
infrared_signal_set_signal(learn_ctx->ir_signal, signal);
xremote_learn_send_event(learn_ctx, XRemoteEventSignalReceived);
}
static void xremote_learn_exit_dialog_ask(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
if(infrared_remote_get_button_count(learn_ctx->ir_remote) || learn_ctx->is_dirty) {
xremote_learn_exit_dialog_alloc(learn_ctx, xremote_learn_dialog_exit_callback);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewDialogExit);
return;
}
learn_ctx->is_dirty = false;
xremote_learn_switch_to_view(learn_ctx, XRemoteViewSubmenu);
}
static void xremote_learn_finish(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
xremote_app_assert_void(learn_ctx->text_input);
if(infrared_remote_get_button_count(learn_ctx->ir_remote) || learn_ctx->is_dirty) {
snprintf(learn_ctx->text_store, XREMOTE_APP_TEXT_MAX, "Remote_");
text_input_set_header_text(learn_ctx->text_input, "Name new remote");
text_input_set_result_callback(
learn_ctx->text_input,
xremote_learn_text_input_callback,
learn_ctx,
learn_ctx->text_store,
XREMOTE_APP_TEXT_MAX,
true);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewTextInput);
return;
}
learn_ctx->is_dirty = false;
xremote_learn_switch_to_view(learn_ctx, XRemoteViewSubmenu);
}
static bool xremote_learn_custom_event_dummy_callback(void* context, uint32_t event) {
UNUSED(context);
UNUSED(event);
return true;
}
static bool xremote_learn_custom_event_callback(void* context, uint32_t event) {
xremote_app_assert(context, false);
XRemoteLearnContext* learn_ctx = context;
if(learn_ctx->finish_learning && event != XRemoteEventSignalFinish &&
event != XRemoteEventSignalAskExit && event != XRemoteEventSignalExit)
return true;
if(event == XRemoteEventSignalReceived) {
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewSignal);
} else if(event == XRemoteEventSignalSave) {
const char* name = xremote_learn_get_curr_button_name(learn_ctx);
infrared_remote_delete_button_by_name(learn_ctx->ir_remote, name);
InfraredSignal* signal = xremote_learn_get_ir_signal(learn_ctx);
infrared_remote_push_button(learn_ctx->ir_remote, name, signal);
learn_ctx->is_dirty = false;
if(++learn_ctx->current_button >= XREMOTE_BUTTON_COUNT) {
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_finish(learn_ctx);
return true;
}
xremote_learn_context_rx_start(learn_ctx);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewLearn);
} else if(event == XRemoteEventSignalSkip) {
learn_ctx->current_button++;
learn_ctx->is_dirty = false;
if(learn_ctx->current_button >= XREMOTE_BUTTON_COUNT) {
if(xremote_learn_has_buttons(learn_ctx)) {
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_finish(learn_ctx);
return true;
}
learn_ctx->current_button = 0;
}
xremote_learn_context_rx_start(learn_ctx);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewLearn);
} else if(event == XRemoteEventSignalRetry) {
learn_ctx->is_dirty = false;
xremote_learn_context_rx_start(learn_ctx);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewLearn);
} else if(event == XRemoteEventSignalFinish) {
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_finish(learn_ctx);
} else if(event == XRemoteEventSignalAskExit) {
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_exit_dialog_ask(learn_ctx);
} else if(event == XRemoteEventSignalExit) {
learn_ctx->is_dirty = false;
xremote_learn_context_rx_stop(learn_ctx);
xremote_learn_switch_to_view(learn_ctx, XRemoteViewSubmenu);
}
return true;
}
static XRemoteLearnContext* xremote_learn_context_alloc(XRemoteAppContext* app_ctx) {
XRemoteLearnContext* learn_ctx = malloc(sizeof(XRemoteLearnContext));
learn_ctx->ir_signal = infrared_signal_alloc();
learn_ctx->ir_remote = infrared_remote_alloc();
learn_ctx->app_ctx = app_ctx;
learn_ctx->dialog_ex = NULL;
learn_ctx->text_store[0] = 0;
learn_ctx->current_button = 0;
learn_ctx->curr_view = XRemoteViewLearn;
learn_ctx->prev_view = XRemoteViewNone;
learn_ctx->finish_learning = false;
learn_ctx->stop_receiver = false;
learn_ctx->is_dirty = false;
learn_ctx->signal_view = xremote_learn_success_view_alloc(app_ctx, learn_ctx);
View* view = xremote_view_get_view(learn_ctx->signal_view);
view_set_previous_callback(view, xremote_learn_view_exit_callback);
view_dispatcher_add_view(app_ctx->view_dispatcher, XRemoteViewSignal, view);
learn_ctx->text_input = text_input_alloc();
text_input_set_validator(learn_ctx->text_input, NULL, learn_ctx);
view = text_input_get_view(learn_ctx->text_input);
view_set_previous_callback(view, xremote_learn_text_input_exit_callback);
view_dispatcher_add_view(app_ctx->view_dispatcher, XRemoteViewTextInput, view);
view_dispatcher_set_custom_event_callback(
app_ctx->view_dispatcher, xremote_learn_custom_event_callback);
view_dispatcher_set_event_callback_context(app_ctx->view_dispatcher, learn_ctx);
learn_ctx->ir_receiver = xremote_signal_receiver_alloc(app_ctx);
xremote_signal_receiver_set_context(learn_ctx->ir_receiver, learn_ctx, NULL);
xremote_signal_receiver_set_rx_callback(learn_ctx->ir_receiver, xremote_learn_signal_callback);
return learn_ctx;
}
static void xremote_learn_context_free(XRemoteLearnContext* learn_ctx) {
xremote_app_assert_void(learn_ctx);
xremote_signal_receiver_stop(learn_ctx->ir_receiver);
xremote_learn_exit_dialog_free(learn_ctx);
ViewDispatcher* view_disp = learn_ctx->app_ctx->view_dispatcher;
view_dispatcher_set_custom_event_callback(
view_disp, xremote_learn_custom_event_dummy_callback);
view_dispatcher_remove_view(view_disp, XRemoteViewTextInput);
text_input_free(learn_ctx->text_input);
view_dispatcher_remove_view(view_disp, XRemoteViewSignal);
xremote_view_free(learn_ctx->signal_view);
xremote_signal_receiver_free(learn_ctx->ir_receiver);
infrared_signal_free(learn_ctx->ir_signal);
infrared_remote_free(learn_ctx->ir_remote);
free(learn_ctx);
}
static void xremote_learn_context_clear_callback(void* context) {
XRemoteLearnContext* learn = context;
xremote_learn_context_free(learn);
}
XRemoteApp* xremote_learn_alloc(XRemoteAppContext* app_ctx) {
XRemoteApp* app = xremote_app_alloc(app_ctx);
app->view_id = XRemoteViewLearn;
XRemoteLearnContext* learn = xremote_learn_context_alloc(app_ctx);
app->view_ctx = xremote_learn_view_alloc(app->app_ctx, learn);
View* view = xremote_view_get_view(app->view_ctx);
ViewDispatcher* view_disp = app_ctx->view_dispatcher;
view_dispatcher_add_view(view_disp, app->view_id, view);
xremote_app_view_set_previous_callback(app, xremote_learn_view_exit_callback);
xremote_app_set_view_context(app, learn, xremote_learn_context_clear_callback);
xremote_signal_receiver_start(learn->ir_receiver);
return app;
}