xremote_common_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*!
  2. * @file flipper-xremote/views/xremote_common_view.c
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Common view and canvas functionality shared between the pages.
  7. */
  8. #include "xremote_common_view.h"
  9. #include "../xremote_app.h"
  10. typedef struct {
  11. int index;
  12. const char* name;
  13. } XRemoteButton;
  14. static const XRemoteButton g_buttons[XREMOTE_BUTTON_COUNT + 1] = {
  15. {0, XREMOTE_COMMAND_POWER},
  16. {1, XREMOTE_COMMAND_SETUP},
  17. {2, XREMOTE_COMMAND_INPUT},
  18. {3, XREMOTE_COMMAND_EJECT},
  19. {4, XREMOTE_COMMAND_MENU},
  20. {5, XREMOTE_COMMAND_LIST},
  21. {6, XREMOTE_COMMAND_INFO},
  22. {7, XREMOTE_COMMAND_BACK},
  23. {8, XREMOTE_COMMAND_OK},
  24. {9, XREMOTE_COMMAND_UP},
  25. {10, XREMOTE_COMMAND_DOWN},
  26. {11, XREMOTE_COMMAND_LEFT},
  27. {12, XREMOTE_COMMAND_RIGHT},
  28. {13, XREMOTE_COMMAND_JUMP_FORWARD},
  29. {14, XREMOTE_COMMAND_JUMP_BACKWARD},
  30. {15, XREMOTE_COMMAND_FAST_FORWARD},
  31. {16, XREMOTE_COMMAND_FAST_BACKWARD},
  32. {17, XREMOTE_COMMAND_PLAY_PAUSE},
  33. {18, XREMOTE_COMMAND_PAUSE},
  34. {19, XREMOTE_COMMAND_PLAY},
  35. {20, XREMOTE_COMMAND_STOP},
  36. {21, XREMOTE_COMMAND_MUTE},
  37. {22, XREMOTE_COMMAND_MODE},
  38. {23, XREMOTE_COMMAND_VOL_UP},
  39. {24, XREMOTE_COMMAND_VOL_DOWN},
  40. {25, XREMOTE_COMMAND_NEXT_CHAN},
  41. {26, XREMOTE_COMMAND_PREV_CHAN},
  42. {-1, NULL}};
  43. const char* xremote_button_get_name(int index) {
  44. if(index > XREMOTE_BUTTON_COUNT) return NULL;
  45. return g_buttons[index].name;
  46. }
  47. int xremote_button_get_index(const char* name) {
  48. size_t i;
  49. for(i = 0; i < XREMOTE_BUTTON_COUNT; i++) {
  50. if(!strcmp(name, g_buttons[i].name)) return g_buttons[i].index;
  51. }
  52. return -1;
  53. }
  54. struct XRemoteView {
  55. XRemoteClearCallback on_clear;
  56. XRemoteAppContext* app_ctx;
  57. View* view;
  58. void* context;
  59. };
  60. XRemoteView* xremote_view_alloc_empty() {
  61. XRemoteView* remote_view = malloc(sizeof(XRemoteView));
  62. return remote_view;
  63. }
  64. XRemoteView*
  65. xremote_view_alloc(void* app_ctx, ViewInputCallback input_cb, ViewDrawCallback draw_cb) {
  66. XRemoteView* remote_view = xremote_view_alloc_empty();
  67. remote_view->app_ctx = app_ctx;
  68. remote_view->view = view_alloc();
  69. remote_view->context = NULL;
  70. remote_view->on_clear = NULL;
  71. view_set_orientation(
  72. remote_view->view, ((XRemoteAppContext*)app_ctx)->app_settings->orientation);
  73. view_allocate_model(remote_view->view, ViewModelTypeLocking, sizeof(XRemoteViewModel));
  74. view_set_input_callback(remote_view->view, input_cb);
  75. view_set_draw_callback(remote_view->view, draw_cb);
  76. view_set_context(remote_view->view, remote_view);
  77. return remote_view;
  78. }
  79. void xremote_view_clear_context(XRemoteView* rview) {
  80. furi_assert(rview);
  81. if(rview->context && rview->on_clear) rview->on_clear(rview->context);
  82. rview->context = NULL;
  83. }
  84. void xremote_view_set_context(XRemoteView* rview, void* context, XRemoteClearCallback on_clear) {
  85. xremote_view_clear_context(rview);
  86. rview->context = context;
  87. rview->on_clear = on_clear;
  88. }
  89. void xremote_view_set_view(XRemoteView* rview, View* view) {
  90. xremote_view_clear_context(rview);
  91. rview->view = view;
  92. }
  93. void* xremote_view_get_context(XRemoteView* rview) {
  94. furi_assert(rview);
  95. return rview->context;
  96. }
  97. void xremote_view_set_app_context(XRemoteView* rview, void* app_ctx) {
  98. furi_assert(rview);
  99. rview->app_ctx = app_ctx;
  100. }
  101. void* xremote_view_get_app_context(XRemoteView* rview) {
  102. furi_assert(rview);
  103. return rview->app_ctx;
  104. }
  105. void xremote_view_free(XRemoteView* rview) {
  106. furi_assert(rview);
  107. xremote_view_clear_context(rview);
  108. view_free(rview->view);
  109. free(rview);
  110. }
  111. View* xremote_view_get_view(XRemoteView* rview) {
  112. furi_assert(rview);
  113. return rview->view;
  114. }
  115. static InfraredRemoteButton*
  116. infrared_remote_get_button_by_alt_name(InfraredRemote* remote, const char* name, bool try_low) {
  117. Storage* storage = furi_record_open(RECORD_STORAGE);
  118. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  119. FuriString* header = furi_string_alloc();
  120. FURI_LOG_I(XREMOTE_APP_TAG, "loading alt_names file: \'%s\'", XREMOTE_ALT_NAMES);
  121. InfraredRemoteButton* button = NULL;
  122. char key[XREMOTE_NAME_MAX] = {0};
  123. bool key_found = false;
  124. uint32_t version = 0;
  125. do {
  126. /* Open file and read the header */
  127. if(!flipper_format_buffered_file_open_existing(ff, XREMOTE_ALT_NAMES)) break;
  128. if(!flipper_format_read_header(ff, header, &version)) break;
  129. if(!furi_string_equal(header, "XRemote Alt-Names") || (version != 1)) break;
  130. FuriString* value = furi_string_alloc();
  131. key_found = flipper_format_read_string(ff, name, value);
  132. if(!key_found) {
  133. if(!try_low) break;
  134. size_t i;
  135. /* Convert name to lowercase and try again */
  136. for(i = 0; name[i] != '\0' && i < sizeof(key) - 1; i++) {
  137. key[i] = tolower(name[i]);
  138. }
  139. key[i] = '\0';
  140. break;
  141. }
  142. size_t start = 0;
  143. size_t posit = furi_string_search_str(value, ",", start);
  144. if(posit == FURI_STRING_FAILURE) {
  145. const char* alt_name_cstr = furi_string_get_cstr(value);
  146. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  147. } else {
  148. FuriString* alt_name = furi_string_alloc();
  149. while(posit != FURI_STRING_FAILURE) {
  150. furi_string_set_n(alt_name, value, start, posit - start);
  151. const char* alt_name_cstr = furi_string_get_cstr(alt_name);
  152. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  153. furi_string_reset(alt_name);
  154. if(button != NULL) break;
  155. start = posit + 1; // Move to the next position
  156. posit = furi_string_search_str(value, ",", start);
  157. }
  158. if(posit == FURI_STRING_FAILURE && button == NULL) {
  159. size_t str_len = furi_string_utf8_length(value);
  160. furi_string_set_n(alt_name, value, start, str_len - start);
  161. const char* alt_name_cstr = furi_string_get_cstr(alt_name);
  162. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  163. }
  164. furi_string_free(alt_name);
  165. }
  166. } while(false);
  167. furi_record_close(RECORD_STORAGE);
  168. furi_string_free(header);
  169. flipper_format_free(ff);
  170. if(!key_found && try_low) return infrared_remote_get_button_by_alt_name(remote, key, false);
  171. return button;
  172. }
  173. InfraredRemoteButton* xremote_view_get_button_by_name(XRemoteView* rview, const char* name) {
  174. xremote_app_assert(rview->context, NULL);
  175. xremote_app_assert(rview->app_ctx, NULL);
  176. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  177. XRemoteAppButtons* buttons = (XRemoteAppButtons*)rview->context;
  178. InfraredRemoteButton* button = infrared_remote_get_button_by_name(buttons->remote, name);
  179. if(button == NULL && settings->alt_names)
  180. button = infrared_remote_get_button_by_alt_name(buttons->remote, name, true);
  181. return button;
  182. }
  183. bool xremote_view_press_button(XRemoteView* rview, InfraredRemoteButton* button) {
  184. xremote_app_assert(button, false);
  185. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  186. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  187. xremote_app_assert(signal, false);
  188. infrared_signal_transmit_times(signal, settings->repeat_count);
  189. xremote_app_context_notify_led(rview->app_ctx);
  190. return true;
  191. }
  192. bool xremote_view_send_ir_msg_by_name(XRemoteView* rview, const char* name) {
  193. InfraredRemoteButton* button = xremote_view_get_button_by_name(rview, name);
  194. return (button != NULL) ? xremote_view_press_button(rview, button) : false;
  195. }
  196. void xremote_view_model_context_set(XRemoteView* rview, void* model_ctx) {
  197. with_view_model(
  198. xremote_view_get_view(rview),
  199. XRemoteViewModel * model,
  200. {
  201. model->context = model_ctx;
  202. model->up_pressed = false;
  203. model->down_pressed = false;
  204. model->left_pressed = false;
  205. model->right_pressed = false;
  206. model->back_pressed = false;
  207. model->ok_pressed = false;
  208. model->hold = false;
  209. },
  210. true);
  211. }
  212. void xremote_canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, XRemoteIcon icon) {
  213. if(icon == XRemoteIconEnter) {
  214. canvas_draw_circle(canvas, x - 2, y, 4);
  215. canvas_draw_disc(canvas, x - 2, y, 2);
  216. } else if(icon == XRemoteIconBack) {
  217. canvas_draw_triangle(canvas, x - 4, y - 2, 5, 3, CanvasDirectionRightToLeft);
  218. canvas_draw_line(canvas, x + 2, y + 1, x + 1, y + 2);
  219. canvas_draw_line(canvas, x + 2, y, x + 1, y - 1);
  220. canvas_draw_line(canvas, x, y - 2, x - 5, y - 2);
  221. canvas_draw_line(canvas, x, y + 3, x - 3, y + 3);
  222. } else if(icon == XRemoteIconArrowUp) {
  223. canvas_draw_triangle(canvas, x - 2, y - 2, 5, 3, CanvasDirectionBottomToTop);
  224. canvas_draw_line(canvas, x - 2, y - 3, x - 2, y + 4);
  225. } else if(icon == XRemoteIconArrowDown) {
  226. canvas_draw_triangle(canvas, x - 2, y + 2, 5, 3, CanvasDirectionTopToBottom);
  227. canvas_draw_line(canvas, x - 2, y - 4, x - 2, y + 3);
  228. } else if(icon == XRemoteIconArrowLeft) {
  229. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionRightToLeft);
  230. canvas_draw_line(canvas, x + 2, y, x - 5, y);
  231. } else if(icon == XRemoteIconArrowRight) {
  232. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionLeftToRight);
  233. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  234. } else if(icon == XRemoteIconJumpForward) {
  235. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionLeftToRight);
  236. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  237. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  238. canvas_draw_line(canvas, x - 4, y, x, y);
  239. } else if(icon == XRemoteIconJumpBackward) {
  240. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionRightToLeft);
  241. canvas_draw_triangle(canvas, x + 1, y, 5, 3, CanvasDirectionRightToLeft);
  242. canvas_draw_line(canvas, x - 5, y - 2, x - 5, y + 2);
  243. canvas_draw_line(canvas, x, y, x - 4, y);
  244. } else if(icon == XRemoteIconFastForward) {
  245. canvas_draw_triangle(canvas, x - 1, y, 5, 3, CanvasDirectionLeftToRight);
  246. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionLeftToRight);
  247. canvas_draw_line(canvas, x - 3, y, x, y);
  248. } else if(icon == XRemoteIconFastBackward) {
  249. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionRightToLeft);
  250. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionRightToLeft);
  251. canvas_draw_line(canvas, x - 1, y, x - 4, y);
  252. } else if(icon == XRemoteIconPlayPause) {
  253. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  254. canvas_draw_dot(canvas, x - 4, y);
  255. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  256. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  257. } else if(icon == XRemoteIconPlay) {
  258. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionLeftToRight);
  259. canvas_draw_dot(canvas, x - 2, y);
  260. } else if(icon == XRemoteIconPause) {
  261. canvas_draw_line(canvas, x - 3, y - 2, x - 3, y + 2);
  262. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  263. } else if(icon == XRemoteIconStop) {
  264. canvas_draw_box(canvas, x - 4, y - 2, 5, 5);
  265. } else if(icon == XRemoteIconOk) {
  266. canvas_draw_str(canvas, x - 7, y + 4, "OK");
  267. }
  268. }
  269. void xremote_canvas_draw_header(Canvas* canvas, ViewOrientation orient, const char* section) {
  270. Align align = AlignLeft;
  271. uint8_t x = 0;
  272. if(orient == ViewOrientationHorizontal) {
  273. align = AlignRight;
  274. x = 128;
  275. }
  276. canvas_set_font(canvas, FontPrimary);
  277. elements_multiline_text_aligned(canvas, x, 0, align, AlignTop, "XRemote");
  278. canvas_set_font(canvas, FontSecondary);
  279. if(section != NULL) elements_multiline_text_aligned(canvas, x, 12, align, AlignTop, section);
  280. }
  281. void xremote_canvas_draw_exit_footer(Canvas* canvas, ViewOrientation orient, const char* text) {
  282. canvas_set_font(canvas, FontSecondary);
  283. if(orient == ViewOrientationVertical) {
  284. xremote_canvas_draw_icon(canvas, 6, 124, XRemoteIconBack);
  285. elements_multiline_text_aligned(canvas, 12, 128, AlignLeft, AlignBottom, text);
  286. } else {
  287. uint8_t x = strncmp(text, "Hold", 4) ? 71 : 76;
  288. xremote_canvas_draw_icon(canvas, x, 60, XRemoteIconBack);
  289. elements_multiline_text_aligned(canvas, 128, 64, AlignRight, AlignBottom, text);
  290. }
  291. }
  292. void xremote_canvas_draw_button(
  293. Canvas* canvas,
  294. bool pressed,
  295. uint8_t x,
  296. uint8_t y,
  297. XRemoteIcon icon) {
  298. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  299. if(pressed) {
  300. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  301. canvas_set_color(canvas, ColorWhite);
  302. }
  303. xremote_canvas_draw_icon(canvas, x + 11, y + 8, icon);
  304. canvas_set_color(canvas, ColorBlack);
  305. }
  306. void xremote_canvas_draw_button_png(
  307. Canvas* canvas,
  308. bool pressed,
  309. uint8_t x,
  310. uint8_t y,
  311. const Icon* icon) {
  312. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  313. if(pressed) {
  314. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  315. canvas_set_color(canvas, ColorWhite);
  316. }
  317. canvas_draw_icon(canvas, x + 4, y + 3, icon);
  318. canvas_set_color(canvas, ColorBlack);
  319. }
  320. void xremote_canvas_draw_button_wide(
  321. Canvas* canvas,
  322. bool pressed,
  323. uint8_t x,
  324. uint8_t y,
  325. const char* text,
  326. XRemoteIcon icon) {
  327. elements_slightly_rounded_frame(canvas, x + 4, y, 56, 15);
  328. if(pressed) {
  329. elements_slightly_rounded_box(canvas, x + 6, y + 2, 52, 11);
  330. canvas_set_color(canvas, ColorWhite);
  331. }
  332. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  333. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  334. canvas_set_color(canvas, ColorBlack);
  335. }
  336. void xremote_canvas_draw_button_size(
  337. Canvas* canvas,
  338. bool pressed,
  339. uint8_t x,
  340. uint8_t y,
  341. uint8_t xy,
  342. char* text,
  343. XRemoteIcon icon) {
  344. elements_slightly_rounded_frame(canvas, x + 4, y, xy, 15);
  345. if(pressed) {
  346. elements_slightly_rounded_box(canvas, x + 6, y + 2, xy - 4, 11);
  347. canvas_set_color(canvas, ColorWhite);
  348. }
  349. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  350. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  351. canvas_set_color(canvas, ColorBlack);
  352. }
  353. void xremote_canvas_draw_frame(
  354. Canvas* canvas,
  355. bool pressed,
  356. uint8_t x,
  357. uint8_t y,
  358. uint8_t xl,
  359. const char* text) {
  360. elements_slightly_rounded_frame(canvas, x, y, xl, 15);
  361. if(pressed) {
  362. elements_slightly_rounded_box(canvas, x + 2, y + 2, xl - 4, 11);
  363. canvas_set_color(canvas, ColorWhite);
  364. }
  365. canvas_draw_str(canvas, x + 3, y + 11, text);
  366. canvas_set_color(canvas, ColorBlack);
  367. }