hex_editor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <dialogs/dialogs.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. #include <storage/storage.h>
  9. #include <stream/stream.h>
  10. #include <stream/buffered_file_stream.h>
  11. #include <toolbox/stream/file_stream.h>
  12. #include <hex_editor_icons.h>
  13. #include <assets_icons.h>
  14. #define TAG "HexEditor"
  15. typedef struct {
  16. uint32_t file_offset;
  17. uint32_t file_read_bytes;
  18. uint32_t file_size;
  19. uint8_t string_offset;
  20. char editable_char;
  21. Stream* stream;
  22. bool mode;
  23. } HexEditorModel;
  24. typedef struct {
  25. HexEditorModel* model;
  26. FuriMutex** mutex;
  27. FuriMessageQueue* input_queue;
  28. ViewPort* view_port;
  29. Gui* gui;
  30. Storage* storage;
  31. FuriString* buffer;
  32. } HexEditor;
  33. static void draw_callback(Canvas* canvas, void* ctx) {
  34. HexEditor* hex_editor = ctx;
  35. canvas_clear(canvas);
  36. canvas_set_font(canvas, FontPrimary);
  37. canvas_draw_str(canvas, 0, 10, "Line and mode:");
  38. canvas_set_font(canvas, FontSecondary);
  39. canvas_draw_str_aligned(
  40. canvas,
  41. 0,
  42. 20,
  43. AlignLeft,
  44. AlignBottom,
  45. furi_string_get_cstr(hex_editor->buffer) + hex_editor->model->string_offset);
  46. canvas_draw_icon(canvas, 0, 20, &I_Pin_arrow_up_7x9);
  47. if(hex_editor->model->mode) {
  48. elements_button_left(canvas, "ASCII -");
  49. elements_button_right(canvas, "ASCII +");
  50. } else {
  51. elements_button_left(canvas, "");
  52. elements_button_right(canvas, "");
  53. }
  54. canvas_set_font(canvas, FontPrimary);
  55. canvas_draw_glyph(canvas, 0, 45, '0' + hex_editor->model->mode);
  56. canvas_draw_glyph(canvas, 30, 45, hex_editor->model->editable_char);
  57. }
  58. static void input_callback(InputEvent* input_event, void* ctx) {
  59. // Проверяем, что контекст не нулевой
  60. furi_assert(ctx);
  61. HexEditor* hex_editor = ctx;
  62. furi_message_queue_put(hex_editor->input_queue, input_event, 100);
  63. }
  64. static HexEditor* hex_editor_alloc() {
  65. HexEditor* instance = malloc(sizeof(HexEditor));
  66. instance->model = malloc(sizeof(HexEditorModel));
  67. memset(instance->model, 0x0, sizeof(HexEditorModel));
  68. instance->model->editable_char = ' ';
  69. instance->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  70. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  71. instance->view_port = view_port_alloc();
  72. view_port_draw_callback_set(instance->view_port, draw_callback, instance);
  73. view_port_input_callback_set(instance->view_port, input_callback, instance);
  74. instance->gui = furi_record_open(RECORD_GUI);
  75. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  76. instance->storage = furi_record_open(RECORD_STORAGE);
  77. instance->buffer = furi_string_alloc();
  78. return instance;
  79. }
  80. static void hex_editor_free(HexEditor* instance) {
  81. furi_record_close(RECORD_STORAGE);
  82. gui_remove_view_port(instance->gui, instance->view_port);
  83. furi_record_close(RECORD_GUI);
  84. view_port_free(instance->view_port);
  85. furi_message_queue_free(instance->input_queue);
  86. furi_mutex_free(instance->mutex);
  87. if(instance->model->stream) buffered_file_stream_close(instance->model->stream);
  88. furi_string_free(instance->buffer);
  89. free(instance->model);
  90. free(instance);
  91. }
  92. static bool hex_editor_open_file(HexEditor* hex_editor, const char* file_path) {
  93. furi_assert(hex_editor);
  94. furi_assert(file_path);
  95. hex_editor->model->stream = buffered_file_stream_alloc(hex_editor->storage);
  96. bool isOk = true;
  97. do {
  98. if(!buffered_file_stream_open(
  99. hex_editor->model->stream, file_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  100. FURI_LOG_E(TAG, "Unable to open stream: %s", file_path);
  101. isOk = false;
  102. break;
  103. };
  104. hex_editor->model->file_size = stream_size(hex_editor->model->stream);
  105. } while(false);
  106. return isOk;
  107. }
  108. int32_t hex_editor_app(void* p) {
  109. UNUSED(p);
  110. HexEditor* hex_editor = hex_editor_alloc();
  111. FuriString* file_path;
  112. file_path = furi_string_alloc();
  113. do {
  114. if(p && strlen(p)) {
  115. furi_string_set(file_path, (const char*)p);
  116. } else {
  117. furi_string_set(file_path, STORAGE_EXT_PATH_PREFIX);
  118. DialogsFileBrowserOptions browser_options;
  119. dialog_file_browser_set_basic_options(&browser_options, "*", &I_edit_10px);
  120. browser_options.hide_ext = false;
  121. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  122. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  123. furi_record_close(RECORD_DIALOGS);
  124. if(!res) {
  125. FURI_LOG_I(TAG, "No file selected");
  126. break;
  127. }
  128. }
  129. FURI_LOG_I(TAG, "File selected: %s", furi_string_get_cstr(file_path));
  130. if(!hex_editor_open_file(hex_editor, furi_string_get_cstr(file_path))) break;
  131. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  132. FURI_LOG_T(TAG, "No keys left in dict");
  133. break;
  134. }
  135. InputEvent event;
  136. int8_t off;
  137. while(1) {
  138. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  139. // и проверяем, что у нас получилось это сделать
  140. furi_check(
  141. furi_message_queue_get(hex_editor->input_queue, &event, FuriWaitForever) ==
  142. FuriStatusOk);
  143. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  144. if(event.type == InputTypeShort || event.type == InputTypeRepeat) {
  145. if(!hex_editor->model->mode) {
  146. off = 1;
  147. if(event.type == InputTypeRepeat) {
  148. off = 2;
  149. }
  150. if(event.key == InputKeyRight) {
  151. hex_editor->model->string_offset += off;
  152. if(hex_editor->model->string_offset >=
  153. furi_string_size(hex_editor->buffer)) {
  154. // dengeros
  155. hex_editor->model->string_offset -=
  156. furi_string_size(hex_editor->buffer);
  157. }
  158. }
  159. if(event.key == InputKeyLeft) {
  160. if(hex_editor->model->string_offset - off < 0) {
  161. // dengeros
  162. hex_editor->model->string_offset +=
  163. furi_string_size(hex_editor->buffer);
  164. }
  165. hex_editor->model->string_offset -= off;
  166. }
  167. if(event.key == InputKeyDown) {
  168. hex_editor->model->string_offset = 0;
  169. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  170. FURI_LOG_T(TAG, "No keys left in dict");
  171. }
  172. }
  173. if(event.key == InputKeyUp) {
  174. hex_editor->model->string_offset = 0;
  175. // TODO asert
  176. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  177. FURI_LOG_E(TAG, "Unable to seek stream");
  178. break;
  179. }
  180. // NOT work on first line
  181. stream_seek_to_char(
  182. hex_editor->model->stream, '\n', StreamDirectionBackward);
  183. if(!stream_seek_to_char(
  184. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  185. stream_rewind(hex_editor->model->stream);
  186. } else {
  187. if(!stream_seek(
  188. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  189. FURI_LOG_E(TAG, "Unable to seek stream");
  190. break;
  191. }
  192. }
  193. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  194. FURI_LOG_T(TAG, "No keys left in dict");
  195. break;
  196. }
  197. }
  198. if(event.key == InputKeyOk) {
  199. hex_editor->model->editable_char = furi_string_get_char(
  200. hex_editor->buffer, hex_editor->model->string_offset);
  201. hex_editor->model->mode = 1;
  202. }
  203. } else {
  204. off = 1;
  205. if(event.type == InputTypeRepeat) {
  206. off = 4;
  207. }
  208. if(event.key == InputKeyRight) {
  209. hex_editor->model->editable_char += off;
  210. }
  211. if(event.key == InputKeyLeft) {
  212. hex_editor->model->editable_char -= off;
  213. }
  214. if(event.key == InputKeyOk) {
  215. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  216. FURI_LOG_E(TAG, "Unable to seek stream");
  217. break;
  218. }
  219. stream_seek_to_char(
  220. hex_editor->model->stream, '\n', StreamDirectionBackward);
  221. stream_seek(
  222. hex_editor->model->stream,
  223. hex_editor->model->string_offset + 1,
  224. StreamOffsetFromCurrent);
  225. stream_write_char(
  226. hex_editor->model->stream, hex_editor->model->editable_char);
  227. hex_editor->model->editable_char = ' ';
  228. hex_editor->model->mode = 0;
  229. stream_seek_to_char(
  230. hex_editor->model->stream, '\n', StreamDirectionBackward);
  231. if(!stream_seek(hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  232. FURI_LOG_E(TAG, "Unable to seek stream");
  233. break;
  234. }
  235. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  236. FURI_LOG_T(TAG, "No keys left in dict");
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. if(event.key == InputKeyBack) {
  243. break;
  244. }
  245. // ?
  246. view_port_update(hex_editor->view_port);
  247. }
  248. } while(false);
  249. furi_string_free(file_path);
  250. hex_editor_free(hex_editor);
  251. return 0;
  252. }