xremote_common_view.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. InfraredRemoteButton*
  116. infrared_remote_get_button_by_alt_name(InfraredRemote* remote, const char* name) {
  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. uint32_t version = 0;
  123. do {
  124. /* Open file and read the header */
  125. if(!flipper_format_buffered_file_open_existing(ff, XREMOTE_ALT_NAMES)) break;
  126. if(!flipper_format_read_header(ff, header, &version)) break;
  127. if(!furi_string_equal(header, "XRemote Alt-Names") || (version != 1)) break;
  128. FuriString* value = furi_string_alloc();
  129. if(!flipper_format_read_string(ff, name, value)) break;
  130. size_t start = 0;
  131. size_t posit = furi_string_search_str(value, ",", start);
  132. if(posit == FURI_STRING_FAILURE) {
  133. const char* alt_name_cstr = furi_string_get_cstr(value);
  134. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  135. } else {
  136. FuriString* alt_name = furi_string_alloc();
  137. while(posit != FURI_STRING_FAILURE) {
  138. furi_string_set_n(alt_name, value, start, posit - start);
  139. const char* alt_name_cstr = furi_string_get_cstr(alt_name);
  140. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  141. furi_string_reset(alt_name);
  142. if(button != NULL) break;
  143. start = posit + 1; // Move to the next position
  144. posit = furi_string_search_str(value, ",", start);
  145. }
  146. if(posit == FURI_STRING_FAILURE && button == NULL) {
  147. size_t str_len = furi_string_utf8_length(value);
  148. furi_string_set_n(alt_name, value, start, str_len - start);
  149. const char* alt_name_cstr = furi_string_get_cstr(alt_name);
  150. button = infrared_remote_get_button_by_name(remote, alt_name_cstr);
  151. }
  152. furi_string_free(alt_name);
  153. }
  154. } while(false);
  155. furi_record_close(RECORD_STORAGE);
  156. furi_string_free(header);
  157. flipper_format_free(ff);
  158. return button;
  159. }
  160. InfraredRemoteButton* xremote_view_get_button_by_name(XRemoteView* rview, const char* name) {
  161. xremote_app_assert(rview->context, NULL);
  162. xremote_app_assert(rview->app_ctx, NULL);
  163. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  164. XRemoteAppButtons* buttons = (XRemoteAppButtons*)rview->context;
  165. InfraredRemoteButton* button = infrared_remote_get_button_by_name(buttons->remote, name);
  166. if(button == NULL && settings->alt_names)
  167. button = infrared_remote_get_button_by_alt_name(buttons->remote, name);
  168. return button;
  169. }
  170. bool xremote_view_press_button(XRemoteView* rview, InfraredRemoteButton* button) {
  171. xremote_app_assert(button, false);
  172. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  173. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  174. xremote_app_assert(signal, false);
  175. infrared_signal_transmit_times(signal, settings->repeat_count);
  176. xremote_app_context_notify_led(rview->app_ctx);
  177. return true;
  178. }
  179. bool xremote_view_send_ir_msg_by_name(XRemoteView* rview, const char* name) {
  180. InfraredRemoteButton* button = xremote_view_get_button_by_name(rview, name);
  181. return (button != NULL) ? xremote_view_press_button(rview, button) : false;
  182. }
  183. void xremote_view_model_context_set(XRemoteView* rview, void* model_ctx) {
  184. with_view_model(
  185. xremote_view_get_view(rview),
  186. XRemoteViewModel * model,
  187. {
  188. model->context = model_ctx;
  189. model->up_pressed = false;
  190. model->down_pressed = false;
  191. model->left_pressed = false;
  192. model->right_pressed = false;
  193. model->back_pressed = false;
  194. model->ok_pressed = false;
  195. model->hold = false;
  196. },
  197. true);
  198. }
  199. void xremote_canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, XRemoteIcon icon) {
  200. if(icon == XRemoteIconEnter) {
  201. canvas_draw_circle(canvas, x - 2, y, 4);
  202. canvas_draw_disc(canvas, x - 2, y, 2);
  203. } else if(icon == XRemoteIconBack) {
  204. canvas_draw_triangle(canvas, x - 4, y - 2, 5, 3, CanvasDirectionRightToLeft);
  205. canvas_draw_line(canvas, x + 2, y + 1, x + 1, y + 2);
  206. canvas_draw_line(canvas, x + 2, y, x + 1, y - 1);
  207. canvas_draw_line(canvas, x, y - 2, x - 5, y - 2);
  208. canvas_draw_line(canvas, x, y + 3, x - 3, y + 3);
  209. } else if(icon == XRemoteIconArrowUp) {
  210. canvas_draw_triangle(canvas, x - 2, y - 2, 5, 3, CanvasDirectionBottomToTop);
  211. canvas_draw_line(canvas, x - 2, y - 3, x - 2, y + 4);
  212. } else if(icon == XRemoteIconArrowDown) {
  213. canvas_draw_triangle(canvas, x - 2, y + 2, 5, 3, CanvasDirectionTopToBottom);
  214. canvas_draw_line(canvas, x - 2, y - 4, x - 2, y + 3);
  215. } else if(icon == XRemoteIconArrowLeft) {
  216. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionRightToLeft);
  217. canvas_draw_line(canvas, x + 2, y, x - 5, y);
  218. } else if(icon == XRemoteIconArrowRight) {
  219. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionLeftToRight);
  220. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  221. } else if(icon == XRemoteIconJumpForward) {
  222. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionLeftToRight);
  223. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  224. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  225. canvas_draw_line(canvas, x - 4, y, x, y);
  226. } else if(icon == XRemoteIconJumpBackward) {
  227. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionRightToLeft);
  228. canvas_draw_triangle(canvas, x + 1, y, 5, 3, CanvasDirectionRightToLeft);
  229. canvas_draw_line(canvas, x - 5, y - 2, x - 5, y + 2);
  230. canvas_draw_line(canvas, x, y, x - 4, y);
  231. } else if(icon == XRemoteIconFastForward) {
  232. canvas_draw_triangle(canvas, x - 1, y, 5, 3, CanvasDirectionLeftToRight);
  233. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionLeftToRight);
  234. canvas_draw_line(canvas, x - 3, y, x, y);
  235. } else if(icon == XRemoteIconFastBackward) {
  236. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionRightToLeft);
  237. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionRightToLeft);
  238. canvas_draw_line(canvas, x - 1, y, x - 4, y);
  239. } else if(icon == XRemoteIconPlayPause) {
  240. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  241. canvas_draw_dot(canvas, x - 4, y);
  242. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  243. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  244. } else if(icon == XRemoteIconPlay) {
  245. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionLeftToRight);
  246. canvas_draw_dot(canvas, x - 2, y);
  247. } else if(icon == XRemoteIconPause) {
  248. canvas_draw_line(canvas, x - 3, y - 2, x - 3, y + 2);
  249. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  250. } else if(icon == XRemoteIconStop) {
  251. canvas_draw_box(canvas, x - 4, y - 2, 5, 5);
  252. } else if(icon == XRemoteIconOk) {
  253. canvas_draw_str(canvas, x - 7, y + 4, "OK");
  254. }
  255. }
  256. void xremote_canvas_draw_header(Canvas* canvas, ViewOrientation orient, const char* section) {
  257. Align align = AlignLeft;
  258. uint8_t x = 0;
  259. if(orient == ViewOrientationHorizontal) {
  260. align = AlignRight;
  261. x = 128;
  262. }
  263. canvas_set_font(canvas, FontPrimary);
  264. elements_multiline_text_aligned(canvas, x, 0, align, AlignTop, "XRemote");
  265. canvas_set_font(canvas, FontSecondary);
  266. if(section != NULL) elements_multiline_text_aligned(canvas, x, 12, align, AlignTop, section);
  267. }
  268. void xremote_canvas_draw_exit_footer(Canvas* canvas, ViewOrientation orient, const char* text) {
  269. canvas_set_font(canvas, FontSecondary);
  270. if(orient == ViewOrientationVertical) {
  271. xremote_canvas_draw_icon(canvas, 6, 124, XRemoteIconBack);
  272. elements_multiline_text_aligned(canvas, 12, 128, AlignLeft, AlignBottom, text);
  273. } else {
  274. uint8_t x = strncmp(text, "Hold", 4) ? 71 : 76;
  275. xremote_canvas_draw_icon(canvas, x, 60, XRemoteIconBack);
  276. elements_multiline_text_aligned(canvas, 128, 64, AlignRight, AlignBottom, text);
  277. }
  278. }
  279. void xremote_canvas_draw_button(
  280. Canvas* canvas,
  281. bool pressed,
  282. uint8_t x,
  283. uint8_t y,
  284. XRemoteIcon icon) {
  285. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  286. if(pressed) {
  287. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  288. canvas_set_color(canvas, ColorWhite);
  289. }
  290. xremote_canvas_draw_icon(canvas, x + 11, y + 8, icon);
  291. canvas_set_color(canvas, ColorBlack);
  292. }
  293. void xremote_canvas_draw_button_png(
  294. Canvas* canvas,
  295. bool pressed,
  296. uint8_t x,
  297. uint8_t y,
  298. const Icon* icon) {
  299. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  300. if(pressed) {
  301. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  302. canvas_set_color(canvas, ColorWhite);
  303. }
  304. canvas_draw_icon(canvas, x + 4, y + 3, icon);
  305. canvas_set_color(canvas, ColorBlack);
  306. }
  307. void xremote_canvas_draw_button_wide(
  308. Canvas* canvas,
  309. bool pressed,
  310. uint8_t x,
  311. uint8_t y,
  312. const char* text,
  313. XRemoteIcon icon) {
  314. elements_slightly_rounded_frame(canvas, x + 4, y, 56, 15);
  315. if(pressed) {
  316. elements_slightly_rounded_box(canvas, x + 6, y + 2, 52, 11);
  317. canvas_set_color(canvas, ColorWhite);
  318. }
  319. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  320. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  321. canvas_set_color(canvas, ColorBlack);
  322. }
  323. void xremote_canvas_draw_button_size(
  324. Canvas* canvas,
  325. bool pressed,
  326. uint8_t x,
  327. uint8_t y,
  328. uint8_t xy,
  329. char* text,
  330. XRemoteIcon icon) {
  331. elements_slightly_rounded_frame(canvas, x + 4, y, xy, 15);
  332. if(pressed) {
  333. elements_slightly_rounded_box(canvas, x + 6, y + 2, xy - 4, 11);
  334. canvas_set_color(canvas, ColorWhite);
  335. }
  336. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  337. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  338. canvas_set_color(canvas, ColorBlack);
  339. }
  340. void xremote_canvas_draw_frame(
  341. Canvas* canvas,
  342. bool pressed,
  343. uint8_t x,
  344. uint8_t y,
  345. uint8_t xl,
  346. const char* text) {
  347. elements_slightly_rounded_frame(canvas, x, y, xl, 15);
  348. if(pressed) {
  349. elements_slightly_rounded_box(canvas, x + 2, y + 2, xl - 4, 11);
  350. canvas_set_color(canvas, ColorWhite);
  351. }
  352. canvas_draw_str(canvas, x + 3, y + 11, text);
  353. canvas_set_color(canvas, ColorBlack);
  354. }