view_build.c 10 KB

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