mp_flipper_runtime.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <mp_flipper_runtime.h>
  4. #include <mp_flipper_modflipperzero.h>
  5. #include "mp_flipper_context.h"
  6. static void on_input_callback(InputEvent* event, void* ctx) {
  7. uint16_t button = 1 << event->key;
  8. uint16_t type = 1 << (InputKeyMAX + event->type);
  9. mp_flipper_on_input(button, type);
  10. }
  11. void mp_flipper_save_file(const char* file_path, const char* data, size_t size) {
  12. Storage* storage = furi_record_open(RECORD_STORAGE);
  13. File* file = storage_file_alloc(storage);
  14. do {
  15. if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  16. storage_file_free(file);
  17. mp_flipper_raise_os_error_with_filename(MP_ENOENT, file_path);
  18. break;
  19. }
  20. storage_file_write(file, data, size);
  21. } while(false);
  22. storage_file_free(file);
  23. furi_record_close(RECORD_STORAGE);
  24. }
  25. inline void mp_flipper_nlr_jump_fail(void* val) {
  26. furi_crash();
  27. }
  28. inline void mp_flipper_assert(const char* file, int line, const char* func, const char* expr) {
  29. }
  30. inline void mp_flipper_fatal_error(const char* msg) {
  31. furi_crash(msg);
  32. }
  33. const char* mp_flipper_print_get_data(void* data) {
  34. return furi_string_get_cstr(data);
  35. }
  36. size_t mp_flipper_print_get_data_length(void* data) {
  37. return furi_string_size(data);
  38. }
  39. void* mp_flipper_print_data_alloc() {
  40. return furi_string_alloc();
  41. }
  42. void mp_flipper_print_strn(void* data, const char* str, size_t length) {
  43. for(size_t i = 0; i < length; i++) {
  44. furi_string_push_back(data, str[i]);
  45. }
  46. }
  47. void mp_flipper_print_data_free(void* data) {
  48. furi_string_free(data);
  49. }
  50. void* mp_flipper_context_alloc() {
  51. mp_flipper_context_t* ctx = malloc(sizeof(mp_flipper_context_t));
  52. ctx->gui = furi_record_open(RECORD_GUI);
  53. ctx->view_port = view_port_alloc();
  54. ctx->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  55. ctx->input_event = furi_pubsub_subscribe(ctx->input_event_queue, on_input_callback, NULL);
  56. gui_add_view_port(ctx->gui, ctx->view_port, GuiLayerFullscreen);
  57. ctx->canvas = gui_direct_draw_acquire(ctx->gui);
  58. return ctx;
  59. }
  60. void mp_flipper_context_free(void* context) {
  61. mp_flipper_context_t* ctx = context;
  62. gui_direct_draw_release(ctx->gui);
  63. furi_pubsub_unsubscribe(ctx->input_event_queue, ctx->input_event);
  64. gui_remove_view_port(ctx->gui, ctx->view_port);
  65. view_port_free(ctx->view_port);
  66. furi_record_close(RECORD_GUI);
  67. furi_record_close(RECORD_INPUT_EVENTS);
  68. free(ctx);
  69. }