view_build.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. extern ProtoViewDecoder* Decoders[]; // Defined in signal.c.
  5. /* Our view private data. */
  6. #define USER_VALUE_LEN 64
  7. typedef struct {
  8. ProtoViewDecoder* decoder; /* Decoder we are using to create a
  9. message. */
  10. uint32_t cur_decoder; /* Decoder index when we are yet selecting
  11. a decoder. Used when decoder is NULL. */
  12. ProtoViewFieldSet* fieldset; /* The fields to populate. */
  13. uint32_t cur_field; /* Field we are editing right now. This
  14. is the index inside the 'fieldset'
  15. fields. */
  16. char* user_value; /* Keyboard input to replace the current
  17. field value goes here. */
  18. } BuildViewPrivData;
  19. /* Not all the decoders support message bulding, so we can't just
  20. * increment / decrement the cur_decoder index here. */
  21. static void select_next_decoder(ProtoViewApp* app) {
  22. BuildViewPrivData* privdata = app->view_privdata;
  23. do {
  24. privdata->cur_decoder++;
  25. if(Decoders[privdata->cur_decoder] == NULL) privdata->cur_decoder = 0;
  26. } while(Decoders[privdata->cur_decoder]->get_fields == NULL);
  27. }
  28. /* Like select_next_decoder() but goes backward. */
  29. static void select_prev_decoder(ProtoViewApp* app) {
  30. BuildViewPrivData* privdata = app->view_privdata;
  31. do {
  32. if(privdata->cur_decoder == 0) {
  33. /* Go one after the last one to wrap around. */
  34. while(Decoders[privdata->cur_decoder])
  35. privdata->cur_decoder++;
  36. }
  37. privdata->cur_decoder--;
  38. } while(Decoders[privdata->cur_decoder]->get_fields == NULL);
  39. }
  40. /* Render the view to select the decoder, among the ones that
  41. * support message building. */
  42. static void render_view_select_decoder(Canvas* const canvas, ProtoViewApp* app) {
  43. BuildViewPrivData* privdata = app->view_privdata;
  44. canvas_set_font(canvas, FontPrimary);
  45. canvas_draw_str(canvas, 0, 9, "Signal creator");
  46. canvas_set_font(canvas, FontSecondary);
  47. canvas_draw_str(canvas, 0, 19, "up/down: select, ok: choose");
  48. canvas_set_font(canvas, FontPrimary);
  49. canvas_draw_str_aligned(
  50. canvas, 64, 38, AlignCenter, AlignCenter, Decoders[privdata->cur_decoder]->name);
  51. }
  52. /* Render the view that allows the user to populate the fields needed
  53. * for the selected decoder to build a message. */
  54. static void render_view_set_fields(Canvas* const canvas, ProtoViewApp* app) {
  55. BuildViewPrivData* privdata = app->view_privdata;
  56. char buf[32];
  57. snprintf(
  58. buf,
  59. sizeof(buf),
  60. "%s field %d/%d",
  61. privdata->decoder->name,
  62. (int)privdata->cur_field + 1,
  63. (int)privdata->fieldset->numfields);
  64. canvas_set_color(canvas, ColorBlack);
  65. canvas_draw_box(canvas, 0, 0, 128, 21);
  66. canvas_set_color(canvas, ColorWhite);
  67. canvas_set_font(canvas, FontPrimary);
  68. canvas_draw_str(canvas, 1, 9, buf);
  69. canvas_set_font(canvas, FontSecondary);
  70. canvas_draw_str(canvas, 1, 19, "up/down: next field, ok: edit");
  71. /* Write the field name, type, current content. */
  72. canvas_set_color(canvas, ColorBlack);
  73. ProtoViewField* field = privdata->fieldset->fields[privdata->cur_field];
  74. snprintf(
  75. buf, sizeof(buf), "%s %s:%d", field->name, field_get_type_name(field), (int)field->len);
  76. buf[0] = toupper(buf[0]);
  77. canvas_set_font(canvas, FontPrimary);
  78. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignCenter, buf);
  79. canvas_set_font(canvas, FontSecondary);
  80. /* Render the current value between "" */
  81. unsigned int written = (unsigned int)field_to_string(buf + 1, sizeof(buf) - 1, field);
  82. buf[0] = '"';
  83. if(written + 3 < sizeof(buf)) memcpy(buf + written + 1, "\"\x00", 2);
  84. canvas_draw_str_aligned(canvas, 63, 45, AlignCenter, AlignCenter, buf);
  85. /* Footer instructions. */
  86. canvas_draw_str(canvas, 0, 62, "Long ok: create, < > incr/decr");
  87. }
  88. /* Render the build message view. */
  89. void render_view_build_message(Canvas* const canvas, ProtoViewApp* app) {
  90. BuildViewPrivData* privdata = app->view_privdata;
  91. if(privdata->decoder)
  92. render_view_set_fields(canvas, app);
  93. else
  94. render_view_select_decoder(canvas, app);
  95. }
  96. /* Handle input for the decoder selection. */
  97. static void process_input_select_decoder(ProtoViewApp* app, InputEvent input) {
  98. BuildViewPrivData* privdata = app->view_privdata;
  99. if(input.type == InputTypeShort) {
  100. if(input.key == InputKeyOk) {
  101. privdata->decoder = Decoders[privdata->cur_decoder];
  102. privdata->fieldset = fieldset_new();
  103. privdata->decoder->get_fields(privdata->fieldset);
  104. /* If the currently decoded message was produced with the
  105. * same decoder the user selected, let's populate the
  106. * defaults with the current values. So the user will
  107. * actaully edit the current message. */
  108. if(app->signal_decoded && app->msg_info->decoder == privdata->decoder) {
  109. fieldset_copy_matching_fields(privdata->fieldset, app->msg_info->fieldset);
  110. }
  111. /* Now we use the subview system in order to protect the
  112. message editing mode from accidental < or > presses.
  113. Since we are technically into a subview now, we'll have
  114. control of < and >. */
  115. InputEvent ii = {.type = InputTypePress, .key = InputKeyDown};
  116. ui_process_subview_updown(app, ii, 2);
  117. } else if(input.key == InputKeyDown) {
  118. select_next_decoder(app);
  119. } else if(input.key == InputKeyUp) {
  120. select_prev_decoder(app);
  121. }
  122. }
  123. }
  124. /* Called after the user typed the new field value in the keyboard.
  125. * Let's save it and remove the keyboard view. */
  126. static void text_input_done_callback(void* context) {
  127. ProtoViewApp* app = context;
  128. BuildViewPrivData* privdata = app->view_privdata;
  129. if(field_set_from_string(
  130. privdata->fieldset->fields[privdata->cur_field],
  131. privdata->user_value,
  132. strlen(privdata->user_value)) == false) {
  133. ui_show_alert(app, "Invalid value", 1500);
  134. }
  135. free(privdata->user_value);
  136. privdata->user_value = NULL;
  137. ui_dismiss_keyboard(app);
  138. }
  139. /* Handles the effects of < and > keys in field editing mode.
  140. * Instead of force the user to enter the text input mode, delete
  141. * the old value, enter the one, we allow to increment and
  142. * decrement the current field in a much simpler way.
  143. *
  144. * The current filed is changed by 'incr' amount. */
  145. static bool increment_current_field(ProtoViewApp* app, int incr) {
  146. BuildViewPrivData* privdata = app->view_privdata;
  147. ProtoViewFieldSet* fs = privdata->fieldset;
  148. ProtoViewField* f = fs->fields[privdata->cur_field];
  149. return field_incr_value(f, incr);
  150. }
  151. /* Handle input for fields editing mode. */
  152. static void process_input_set_fields(ProtoViewApp* app, InputEvent input) {
  153. BuildViewPrivData* privdata = app->view_privdata;
  154. ProtoViewFieldSet* fs = privdata->fieldset;
  155. if(input.type == InputTypeShort && input.key == InputKeyOk) {
  156. /* Show the keyboard to let the user type the new
  157. * value. */
  158. if(privdata->user_value == NULL) privdata->user_value = malloc(USER_VALUE_LEN);
  159. field_to_string(privdata->user_value, USER_VALUE_LEN, fs->fields[privdata->cur_field]);
  160. ui_show_keyboard(app, privdata->user_value, USER_VALUE_LEN, text_input_done_callback);
  161. } else if(input.type == InputTypeShort && input.key == InputKeyDown) {
  162. privdata->cur_field = (privdata->cur_field + 1) % fs->numfields;
  163. } else if(input.type == InputTypeShort && input.key == InputKeyUp) {
  164. if(privdata->cur_field == 0)
  165. privdata->cur_field = fs->numfields - 1;
  166. else
  167. privdata->cur_field--;
  168. } else if(input.type == InputTypeShort && input.key == InputKeyRight) {
  169. increment_current_field(app, 1);
  170. } else if(input.type == InputTypeShort && input.key == InputKeyLeft) {
  171. increment_current_field(app, -1);
  172. } else if(input.type == InputTypeRepeat && input.key == InputKeyRight) {
  173. // The reason why we don't use a large increment directly
  174. // is that certain field types only support +1 -1 increments.
  175. int times = 10;
  176. while(times--)
  177. increment_current_field(app, 1);
  178. } else if(input.type == InputTypeRepeat && input.key == InputKeyLeft) {
  179. int times = 10;
  180. while(times--)
  181. increment_current_field(app, -1);
  182. } else if(input.type == InputTypeLong && input.key == InputKeyOk) {
  183. // Build the message in a fresh raw buffer.
  184. if(privdata->decoder->build_message) {
  185. RawSamplesBuffer* rs = raw_samples_alloc();
  186. privdata->decoder->build_message(rs, privdata->fieldset);
  187. app->signal_decoded = false; // So that the new signal will be
  188. // accepted as the current signal.
  189. scan_for_signal(app, rs, 5);
  190. raw_samples_free(rs);
  191. ui_show_alert(app, "Done: press back key", 3000);
  192. }
  193. }
  194. }
  195. /* Handle input for the build message view. */
  196. void process_input_build_message(ProtoViewApp* app, InputEvent input) {
  197. BuildViewPrivData* privdata = app->view_privdata;
  198. if(privdata->decoder)
  199. process_input_set_fields(app, input);
  200. else
  201. process_input_select_decoder(app, input);
  202. }
  203. /* Enter view callback. */
  204. void view_enter_build_message(ProtoViewApp* app) {
  205. BuildViewPrivData* privdata = app->view_privdata;
  206. // When we enter the view, the current decoder is just set to zero.
  207. // Seek the next valid if needed.
  208. if(Decoders[privdata->cur_decoder]->get_fields == NULL) {
  209. select_next_decoder(app);
  210. }
  211. // However if there is currently a decoded message, and the
  212. // decoder of such message supports message building, let's
  213. // select it.
  214. if(app->signal_decoded && app->msg_info->decoder->get_fields &&
  215. app->msg_info->decoder->build_message) {
  216. while(Decoders[privdata->cur_decoder] != app->msg_info->decoder)
  217. select_next_decoder(app);
  218. }
  219. }
  220. /* Called on exit for cleanup. */
  221. void view_exit_build_message(ProtoViewApp* app) {
  222. BuildViewPrivData* privdata = app->view_privdata;
  223. if(privdata->fieldset) fieldset_free(privdata->fieldset);
  224. if(privdata->user_value) free(privdata->user_value);
  225. }