hex_editor.c 12 KB

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