hex_editor.c 13 KB

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