xremote_common_view.c 14 KB

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