easy_flipper.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include <easy_flipper/easy_flipper.h>
  2. /**
  3. * @brief Navigation callback for exiting the application
  4. * @param context The context - unused
  5. * @return next view id (VIEW_NONE to exit the app)
  6. */
  7. uint32_t easy_flipper_callback_exit_app(void* context) {
  8. // Exit the application
  9. if(!context) {
  10. FURI_LOG_E(EASY_TAG, "Context is NULL");
  11. return VIEW_NONE;
  12. }
  13. UNUSED(context);
  14. return VIEW_NONE; // Return VIEW_NONE to exit the app
  15. }
  16. /**
  17. * @brief Initialize a buffer
  18. * @param buffer The buffer to initialize
  19. * @param buffer_size The size of the buffer
  20. * @return true if successful, false otherwise
  21. */
  22. bool easy_flipper_set_buffer(char** buffer, uint32_t buffer_size) {
  23. if(!buffer) {
  24. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_buffer");
  25. return false;
  26. }
  27. *buffer = (char*)malloc(buffer_size);
  28. if(!*buffer) {
  29. FURI_LOG_E(EASY_TAG, "Failed to allocate buffer");
  30. return false;
  31. }
  32. *buffer[0] = '\0';
  33. return true;
  34. }
  35. /**
  36. * @brief Initialize a View object
  37. * @param view The View object to initialize
  38. * @param view_id The ID/Index of the view
  39. * @param draw_callback The draw callback function (set to NULL if not needed)
  40. * @param input_callback The input callback function (set to NULL if not needed)
  41. * @param previous_callback The previous callback function (can be set to NULL)
  42. * @param view_dispatcher The ViewDispatcher object
  43. * @return true if successful, false otherwise
  44. */
  45. bool easy_flipper_set_view(
  46. View** view,
  47. int32_t view_id,
  48. void draw_callback(Canvas*, void*),
  49. bool input_callback(InputEvent*, void*),
  50. uint32_t (*previous_callback)(void*),
  51. ViewDispatcher** view_dispatcher,
  52. void* context) {
  53. if(!view || !view_dispatcher) {
  54. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_view");
  55. return false;
  56. }
  57. *view = view_alloc();
  58. if(!*view) {
  59. FURI_LOG_E(EASY_TAG, "Failed to allocate View");
  60. return false;
  61. }
  62. if(draw_callback) {
  63. view_set_draw_callback(*view, draw_callback);
  64. }
  65. if(input_callback) {
  66. view_set_input_callback(*view, input_callback);
  67. }
  68. if(context) {
  69. view_set_context(*view, context);
  70. }
  71. if(previous_callback) {
  72. view_set_previous_callback(*view, previous_callback);
  73. }
  74. view_dispatcher_add_view(*view_dispatcher, view_id, *view);
  75. return true;
  76. }
  77. /**
  78. * @brief Initialize a ViewDispatcher object
  79. * @param view_dispatcher The ViewDispatcher object to initialize
  80. * @param gui The GUI object
  81. * @param context The context to pass to the event callback
  82. * @return true if successful, false otherwise
  83. */
  84. bool easy_flipper_set_view_dispatcher(ViewDispatcher** view_dispatcher, Gui* gui, void* context) {
  85. if(!view_dispatcher) {
  86. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_view_dispatcher");
  87. return false;
  88. }
  89. *view_dispatcher = view_dispatcher_alloc();
  90. if(!*view_dispatcher) {
  91. FURI_LOG_E(EASY_TAG, "Failed to allocate ViewDispatcher");
  92. return false;
  93. }
  94. view_dispatcher_attach_to_gui(*view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  95. if(context) {
  96. view_dispatcher_set_event_callback_context(*view_dispatcher, context);
  97. }
  98. return true;
  99. }
  100. /**
  101. * @brief Initialize a Submenu object
  102. * @note This does not set the items in the submenu
  103. * @param submenu The Submenu object to initialize
  104. * @param view_id The ID/Index of the view
  105. * @param title The title/header of the submenu
  106. * @param previous_callback The previous callback function (can be set to NULL)
  107. * @param view_dispatcher The ViewDispatcher object
  108. * @return true if successful, false otherwise
  109. */
  110. bool easy_flipper_set_submenu(
  111. Submenu** submenu,
  112. int32_t view_id,
  113. char* title,
  114. uint32_t(previous_callback)(void*),
  115. ViewDispatcher** view_dispatcher) {
  116. if(!submenu) {
  117. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_submenu");
  118. return false;
  119. }
  120. *submenu = submenu_alloc();
  121. if(!*submenu) {
  122. FURI_LOG_E(EASY_TAG, "Failed to allocate Submenu");
  123. return false;
  124. }
  125. if(title) {
  126. submenu_set_header(*submenu, title);
  127. }
  128. if(previous_callback) {
  129. view_set_previous_callback(submenu_get_view(*submenu), previous_callback);
  130. }
  131. view_dispatcher_add_view(*view_dispatcher, view_id, submenu_get_view(*submenu));
  132. return true;
  133. }
  134. /**
  135. * @brief Initialize a Menu object
  136. * @note This does not set the items in the menu
  137. * @param menu The Menu object to initialize
  138. * @param view_id The ID/Index of the view
  139. * @param item_callback The item callback function
  140. * @param previous_callback The previous callback function (can be set to NULL)
  141. * @param view_dispatcher The ViewDispatcher object
  142. * @return true if successful, false otherwise
  143. */
  144. bool easy_flipper_set_menu(
  145. Menu** menu,
  146. int32_t view_id,
  147. uint32_t(previous_callback)(void*),
  148. ViewDispatcher** view_dispatcher) {
  149. if(!menu) {
  150. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_menu");
  151. return false;
  152. }
  153. *menu = menu_alloc();
  154. if(!*menu) {
  155. FURI_LOG_E(EASY_TAG, "Failed to allocate Menu");
  156. return false;
  157. }
  158. if(previous_callback) {
  159. view_set_previous_callback(menu_get_view(*menu), previous_callback);
  160. }
  161. view_dispatcher_add_view(*view_dispatcher, view_id, menu_get_view(*menu));
  162. return true;
  163. }
  164. /**
  165. * @brief Initialize a Widget object
  166. * @param widget The Widget object to initialize
  167. * @param view_id The ID/Index of the view
  168. * @param text The text to display in the widget
  169. * @param previous_callback The previous callback function (can be set to NULL)
  170. * @param view_dispatcher The ViewDispatcher object
  171. * @return true if successful, false otherwise
  172. */
  173. bool easy_flipper_set_widget(
  174. Widget** widget,
  175. int32_t view_id,
  176. char* text,
  177. uint32_t(previous_callback)(void*),
  178. ViewDispatcher** view_dispatcher) {
  179. if(!widget) {
  180. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_widget");
  181. return false;
  182. }
  183. *widget = widget_alloc();
  184. if(!*widget) {
  185. FURI_LOG_E(EASY_TAG, "Failed to allocate Widget");
  186. return false;
  187. }
  188. if(text) {
  189. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, text);
  190. }
  191. if(previous_callback) {
  192. view_set_previous_callback(widget_get_view(*widget), previous_callback);
  193. }
  194. view_dispatcher_add_view(*view_dispatcher, view_id, widget_get_view(*widget));
  195. return true;
  196. }
  197. /**
  198. * @brief Initialize a VariableItemList object
  199. * @note This does not set the items in the VariableItemList
  200. * @param variable_item_list The VariableItemList object to initialize
  201. * @param view_id The ID/Index of the view
  202. * @param enter_callback The enter callback function (can be set to NULL)
  203. * @param previous_callback The previous callback function (can be set to NULL)
  204. * @param view_dispatcher The ViewDispatcher object
  205. * @param context The context to pass to the enter callback (usually the app)
  206. * @return true if successful, false otherwise
  207. */
  208. bool easy_flipper_set_variable_item_list(
  209. VariableItemList** variable_item_list,
  210. int32_t view_id,
  211. void (*enter_callback)(void*, uint32_t),
  212. uint32_t(previous_callback)(void*),
  213. ViewDispatcher** view_dispatcher,
  214. void* context) {
  215. if(!variable_item_list) {
  216. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_variable_item_list");
  217. return false;
  218. }
  219. *variable_item_list = variable_item_list_alloc();
  220. if(!*variable_item_list) {
  221. FURI_LOG_E(EASY_TAG, "Failed to allocate VariableItemList");
  222. return false;
  223. }
  224. if(enter_callback) {
  225. variable_item_list_set_enter_callback(*variable_item_list, enter_callback, context);
  226. }
  227. if(previous_callback) {
  228. view_set_previous_callback(
  229. variable_item_list_get_view(*variable_item_list), previous_callback);
  230. }
  231. view_dispatcher_add_view(
  232. *view_dispatcher, view_id, variable_item_list_get_view(*variable_item_list));
  233. return true;
  234. }
  235. /**
  236. * @brief Initialize a TextInput object
  237. * @param text_input The TextInput object to initialize
  238. * @param view_id The ID/Index of the view
  239. * @param previous_callback The previous callback function (can be set to NULL)
  240. * @param view_dispatcher The ViewDispatcher object
  241. * @return true if successful, false otherwise
  242. */
  243. bool easy_flipper_set_text_input(
  244. TextInput** text_input,
  245. int32_t view_id,
  246. char* header_text,
  247. char* text_input_temp_buffer,
  248. uint32_t text_input_buffer_size,
  249. void (*result_callback)(void*),
  250. uint32_t(previous_callback)(void*),
  251. ViewDispatcher** view_dispatcher,
  252. void* context) {
  253. if(!text_input) {
  254. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_text_input");
  255. return false;
  256. }
  257. *text_input = text_input_alloc();
  258. if(!*text_input) {
  259. FURI_LOG_E(EASY_TAG, "Failed to allocate TextInput");
  260. return false;
  261. }
  262. if(previous_callback) {
  263. view_set_previous_callback(text_input_get_view(*text_input), previous_callback);
  264. }
  265. if(header_text) {
  266. text_input_set_header_text(*text_input, header_text);
  267. }
  268. if(text_input_temp_buffer && text_input_buffer_size && result_callback) {
  269. text_input_set_result_callback(
  270. *text_input,
  271. result_callback,
  272. context,
  273. text_input_temp_buffer,
  274. text_input_buffer_size,
  275. false);
  276. }
  277. view_dispatcher_add_view(*view_dispatcher, view_id, text_input_get_view(*text_input));
  278. return true;
  279. }
  280. /**
  281. * @brief Initialize a TextInput object with extra symbols
  282. * @param uart_text_input The TextInput object to initialize
  283. * @param view_id The ID/Index of the view
  284. * @param previous_callback The previous callback function (can be set to NULL)
  285. * @param view_dispatcher The ViewDispatcher object
  286. * @return true if successful, false otherwise
  287. */
  288. bool easy_flipper_set_uart_text_input(
  289. TextInput** uart_text_input,
  290. int32_t view_id,
  291. char* header_text,
  292. char* uart_text_input_temp_buffer,
  293. uint32_t uart_text_input_buffer_size,
  294. void (*result_callback)(void*),
  295. uint32_t(previous_callback)(void*),
  296. ViewDispatcher** view_dispatcher,
  297. void* context) {
  298. if(!uart_text_input) {
  299. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_uart_text_input");
  300. return false;
  301. }
  302. *uart_text_input = text_input_alloc();
  303. if(!*uart_text_input) {
  304. FURI_LOG_E(EASY_TAG, "Failed to allocate UART_TextInput");
  305. return false;
  306. }
  307. if(previous_callback) {
  308. view_set_previous_callback(text_input_get_view(*uart_text_input), previous_callback);
  309. }
  310. if(header_text) {
  311. text_input_set_header_text(*uart_text_input, header_text);
  312. }
  313. if(uart_text_input_temp_buffer && uart_text_input_buffer_size && result_callback) {
  314. text_input_set_result_callback(
  315. *uart_text_input,
  316. result_callback,
  317. context,
  318. uart_text_input_temp_buffer,
  319. uart_text_input_buffer_size,
  320. false);
  321. }
  322. text_input_show_illegal_symbols(*uart_text_input, true);
  323. view_dispatcher_add_view(*view_dispatcher, view_id, text_input_get_view(*uart_text_input));
  324. return true;
  325. }
  326. /**
  327. * @brief Initialize a DialogEx object
  328. * @param dialog_ex The DialogEx object to initialize
  329. * @param view_id The ID/Index of the view
  330. * @param header The header of the dialog
  331. * @param header_x The x coordinate of the header
  332. * @param header_y The y coordinate of the header
  333. * @param text The text of the dialog
  334. * @param text_x The x coordinate of the dialog
  335. * @param text_y The y coordinate of the dialog
  336. * @param left_button_text The text of the left button
  337. * @param right_button_text The text of the right button
  338. * @param center_button_text The text of the center button
  339. * @param result_callback The result callback function
  340. * @param previous_callback The previous callback function (can be set to NULL)
  341. * @param view_dispatcher The ViewDispatcher object
  342. * @param context The context to pass to the result callback
  343. * @return true if successful, false otherwise
  344. */
  345. bool easy_flipper_set_dialog_ex(
  346. DialogEx** dialog_ex,
  347. int32_t view_id,
  348. char* header,
  349. uint16_t header_x,
  350. uint16_t header_y,
  351. char* text,
  352. uint16_t text_x,
  353. uint16_t text_y,
  354. char* left_button_text,
  355. char* right_button_text,
  356. char* center_button_text,
  357. void (*result_callback)(DialogExResult, void*),
  358. uint32_t(previous_callback)(void*),
  359. ViewDispatcher** view_dispatcher,
  360. void* context) {
  361. if(!dialog_ex) {
  362. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_dialog_ex");
  363. return false;
  364. }
  365. *dialog_ex = dialog_ex_alloc();
  366. if(!*dialog_ex) {
  367. FURI_LOG_E(EASY_TAG, "Failed to allocate DialogEx");
  368. return false;
  369. }
  370. if(header) {
  371. dialog_ex_set_header(*dialog_ex, header, header_x, header_y, AlignLeft, AlignTop);
  372. }
  373. if(text) {
  374. dialog_ex_set_text(*dialog_ex, text, text_x, text_y, AlignLeft, AlignTop);
  375. }
  376. if(left_button_text) {
  377. dialog_ex_set_left_button_text(*dialog_ex, left_button_text);
  378. }
  379. if(right_button_text) {
  380. dialog_ex_set_right_button_text(*dialog_ex, right_button_text);
  381. }
  382. if(center_button_text) {
  383. dialog_ex_set_center_button_text(*dialog_ex, center_button_text);
  384. }
  385. if(result_callback) {
  386. dialog_ex_set_result_callback(*dialog_ex, result_callback);
  387. }
  388. if(previous_callback) {
  389. view_set_previous_callback(dialog_ex_get_view(*dialog_ex), previous_callback);
  390. }
  391. if(context) {
  392. dialog_ex_set_context(*dialog_ex, context);
  393. }
  394. view_dispatcher_add_view(*view_dispatcher, view_id, dialog_ex_get_view(*dialog_ex));
  395. return true;
  396. }
  397. /**
  398. * @brief Initialize a Popup object
  399. * @param popup The Popup object to initialize
  400. * @param view_id The ID/Index of the view
  401. * @param header The header of the dialog
  402. * @param header_x The x coordinate of the header
  403. * @param header_y The y coordinate of the header
  404. * @param text The text of the dialog
  405. * @param text_x The x coordinate of the dialog
  406. * @param text_y The y coordinate of the dialog
  407. * @param result_callback The result callback function
  408. * @param previous_callback The previous callback function (can be set to NULL)
  409. * @param view_dispatcher The ViewDispatcher object
  410. * @param context The context to pass to the result callback
  411. * @return true if successful, false otherwise
  412. */
  413. bool easy_flipper_set_popup(
  414. Popup** popup,
  415. int32_t view_id,
  416. char* header,
  417. uint16_t header_x,
  418. uint16_t header_y,
  419. char* text,
  420. uint16_t text_x,
  421. uint16_t text_y,
  422. void (*result_callback)(void*),
  423. uint32_t(previous_callback)(void*),
  424. ViewDispatcher** view_dispatcher,
  425. void* context) {
  426. if(!popup) {
  427. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_popup");
  428. return false;
  429. }
  430. *popup = popup_alloc();
  431. if(!*popup) {
  432. FURI_LOG_E(EASY_TAG, "Failed to allocate Popup");
  433. return false;
  434. }
  435. if(header) {
  436. popup_set_header(*popup, header, header_x, header_y, AlignLeft, AlignTop);
  437. }
  438. if(text) {
  439. popup_set_text(*popup, text, text_x, text_y, AlignLeft, AlignTop);
  440. }
  441. if(result_callback) {
  442. popup_set_callback(*popup, result_callback);
  443. }
  444. if(previous_callback) {
  445. view_set_previous_callback(popup_get_view(*popup), previous_callback);
  446. }
  447. if(context) {
  448. popup_set_context(*popup, context);
  449. }
  450. view_dispatcher_add_view(*view_dispatcher, view_id, popup_get_view(*popup));
  451. return true;
  452. }
  453. /**
  454. * @brief Initialize a Loading object
  455. * @param loading The Loading object to initialize
  456. * @param view_id The ID/Index of the view
  457. * @param previous_callback The previous callback function (can be set to NULL)
  458. * @param view_dispatcher The ViewDispatcher object
  459. * @return true if successful, false otherwise
  460. */
  461. bool easy_flipper_set_loading(
  462. Loading** loading,
  463. int32_t view_id,
  464. uint32_t(previous_callback)(void*),
  465. ViewDispatcher** view_dispatcher) {
  466. if(!loading) {
  467. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_loading");
  468. return false;
  469. }
  470. *loading = loading_alloc();
  471. if(!*loading) {
  472. FURI_LOG_E(EASY_TAG, "Failed to allocate Loading");
  473. return false;
  474. }
  475. if(previous_callback) {
  476. view_set_previous_callback(loading_get_view(*loading), previous_callback);
  477. }
  478. view_dispatcher_add_view(*view_dispatcher, view_id, loading_get_view(*loading));
  479. return true;
  480. }
  481. /**
  482. * @brief Set a char butter to a FuriString
  483. * @param furi_string The FuriString object
  484. * @param buffer The buffer to copy the string to
  485. * @return true if successful, false otherwise
  486. */
  487. bool easy_flipper_set_char_to_furi_string(FuriString** furi_string, char* buffer) {
  488. if(!furi_string) {
  489. FURI_LOG_E(EASY_TAG, "Invalid arguments provided to set_buffer_to_furi_string");
  490. return false;
  491. }
  492. *furi_string = furi_string_alloc();
  493. if(!furi_string) {
  494. FURI_LOG_E(EASY_TAG, "Failed to allocate FuriString");
  495. return false;
  496. }
  497. furi_string_set_str(*furi_string, buffer);
  498. return true;
  499. }