xremote_common_view.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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* xremote_view_get_button_by_name(XRemoteView* rview, const char* name) {
  115. xremote_app_assert(rview->context, NULL);
  116. XRemoteAppButtons* buttons = (XRemoteAppButtons*)rview->context;
  117. return infrared_remote_get_button_by_name(buttons->remote, name);
  118. }
  119. bool xremote_view_press_button(XRemoteView* rview, InfraredRemoteButton* button) {
  120. xremote_app_assert(button, false);
  121. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  122. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  123. xremote_app_assert(signal, false);
  124. infrared_signal_transmit_times(signal, settings->repeat_count);
  125. xremote_app_context_notify_led(rview->app_ctx);
  126. return true;
  127. }
  128. bool xremote_view_send_ir_msg_by_name(XRemoteView* rview, const char* name) {
  129. InfraredRemoteButton* button = xremote_view_get_button_by_name(rview, name);
  130. return (button != NULL) ? xremote_view_press_button(rview, button) : false;
  131. }
  132. void xremote_view_model_context_set(XRemoteView* rview, void* model_ctx) {
  133. with_view_model(
  134. xremote_view_get_view(rview),
  135. XRemoteViewModel * model,
  136. {
  137. model->context = model_ctx;
  138. model->up_pressed = false;
  139. model->down_pressed = false;
  140. model->left_pressed = false;
  141. model->right_pressed = false;
  142. model->back_pressed = false;
  143. model->ok_pressed = false;
  144. model->hold = false;
  145. },
  146. true);
  147. }
  148. void xremote_canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, XRemoteIcon icon) {
  149. if(icon == XRemoteIconEnter) {
  150. canvas_draw_circle(canvas, x - 2, y, 4);
  151. canvas_draw_disc(canvas, x - 2, y, 2);
  152. } else if(icon == XRemoteIconBack) {
  153. canvas_draw_triangle(canvas, x - 4, y - 2, 5, 3, CanvasDirectionRightToLeft);
  154. canvas_draw_line(canvas, x + 2, y + 1, x + 1, y + 2);
  155. canvas_draw_line(canvas, x + 2, y, x + 1, y - 1);
  156. canvas_draw_line(canvas, x, y - 2, x - 5, y - 2);
  157. canvas_draw_line(canvas, x, y + 3, x - 3, y + 3);
  158. } else if(icon == XRemoteIconArrowUp) {
  159. canvas_draw_triangle(canvas, x - 2, y - 2, 5, 3, CanvasDirectionBottomToTop);
  160. canvas_draw_line(canvas, x - 2, y - 3, x - 2, y + 4);
  161. } else if(icon == XRemoteIconArrowDown) {
  162. canvas_draw_triangle(canvas, x - 2, y + 2, 5, 3, CanvasDirectionTopToBottom);
  163. canvas_draw_line(canvas, x - 2, y - 4, x - 2, y + 3);
  164. } else if(icon == XRemoteIconArrowLeft) {
  165. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionRightToLeft);
  166. canvas_draw_line(canvas, x + 2, y, x - 5, y);
  167. } else if(icon == XRemoteIconArrowRight) {
  168. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionLeftToRight);
  169. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  170. } else if(icon == XRemoteIconJumpForward) {
  171. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionLeftToRight);
  172. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  173. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  174. canvas_draw_line(canvas, x - 4, y, x, y);
  175. } else if(icon == XRemoteIconJumpBackward) {
  176. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionRightToLeft);
  177. canvas_draw_triangle(canvas, x + 1, y, 5, 3, CanvasDirectionRightToLeft);
  178. canvas_draw_line(canvas, x - 5, y - 2, x - 5, y + 2);
  179. canvas_draw_line(canvas, x, y, x - 4, y);
  180. } else if(icon == XRemoteIconFastForward) {
  181. canvas_draw_triangle(canvas, x - 1, y, 5, 3, CanvasDirectionLeftToRight);
  182. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionLeftToRight);
  183. canvas_draw_line(canvas, x - 3, y, x, y);
  184. } else if(icon == XRemoteIconFastBackward) {
  185. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionRightToLeft);
  186. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionRightToLeft);
  187. canvas_draw_line(canvas, x - 1, y, x - 4, y);
  188. } else if(icon == XRemoteIconPlayPause) {
  189. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  190. canvas_draw_dot(canvas, x - 4, y);
  191. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  192. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  193. } else if(icon == XRemoteIconPlay) {
  194. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionLeftToRight);
  195. canvas_draw_dot(canvas, x - 2, y);
  196. } else if(icon == XRemoteIconPause) {
  197. canvas_draw_line(canvas, x - 3, y - 2, x - 3, y + 2);
  198. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  199. } else if(icon == XRemoteIconStop) {
  200. canvas_draw_box(canvas, x - 4, y - 2, 5, 5);
  201. } else if(icon == XRemoteIconOk) {
  202. canvas_draw_str(canvas, x - 7, y + 4, "OK");
  203. }
  204. }
  205. void xremote_canvas_draw_header(Canvas* canvas, ViewOrientation orient, const char* section) {
  206. Align align = AlignLeft;
  207. uint8_t x = 0;
  208. if(orient == ViewOrientationHorizontal) {
  209. align = AlignRight;
  210. x = 128;
  211. }
  212. canvas_set_font(canvas, FontPrimary);
  213. elements_multiline_text_aligned(canvas, x, 0, align, AlignTop, "XRemote");
  214. canvas_set_font(canvas, FontSecondary);
  215. if(section != NULL) elements_multiline_text_aligned(canvas, x, 12, align, AlignTop, section);
  216. }
  217. void xremote_canvas_draw_exit_footer(Canvas* canvas, ViewOrientation orient, const char* text) {
  218. canvas_set_font(canvas, FontSecondary);
  219. if(orient == ViewOrientationVertical) {
  220. xremote_canvas_draw_icon(canvas, 6, 124, XRemoteIconBack);
  221. elements_multiline_text_aligned(canvas, 12, 128, AlignLeft, AlignBottom, text);
  222. } else {
  223. uint8_t x = strncmp(text, "Hold", 4) ? 71 : 76;
  224. xremote_canvas_draw_icon(canvas, x, 60, XRemoteIconBack);
  225. elements_multiline_text_aligned(canvas, 128, 64, AlignRight, AlignBottom, text);
  226. }
  227. }
  228. void xremote_canvas_draw_button(
  229. Canvas* canvas,
  230. bool pressed,
  231. uint8_t x,
  232. uint8_t y,
  233. XRemoteIcon icon) {
  234. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  235. if(pressed) {
  236. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  237. canvas_set_color(canvas, ColorWhite);
  238. }
  239. xremote_canvas_draw_icon(canvas, x + 11, y + 8, icon);
  240. canvas_set_color(canvas, ColorBlack);
  241. }
  242. void xremote_canvas_draw_button_png(
  243. Canvas* canvas,
  244. bool pressed,
  245. uint8_t x,
  246. uint8_t y,
  247. const Icon* icon) {
  248. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  249. if(pressed) {
  250. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  251. canvas_set_color(canvas, ColorWhite);
  252. }
  253. canvas_draw_icon(canvas, x + 4, y + 3, icon);
  254. canvas_set_color(canvas, ColorBlack);
  255. }
  256. void xremote_canvas_draw_button_wide(
  257. Canvas* canvas,
  258. bool pressed,
  259. uint8_t x,
  260. uint8_t y,
  261. const char* text,
  262. XRemoteIcon icon) {
  263. elements_slightly_rounded_frame(canvas, x + 4, y, 56, 15);
  264. if(pressed) {
  265. elements_slightly_rounded_box(canvas, x + 6, y + 2, 52, 11);
  266. canvas_set_color(canvas, ColorWhite);
  267. }
  268. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  269. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  270. canvas_set_color(canvas, ColorBlack);
  271. }
  272. void xremote_canvas_draw_button_size(
  273. Canvas* canvas,
  274. bool pressed,
  275. uint8_t x,
  276. uint8_t y,
  277. uint8_t xy,
  278. char* text,
  279. XRemoteIcon icon) {
  280. elements_slightly_rounded_frame(canvas, x + 4, y, xy, 15);
  281. if(pressed) {
  282. elements_slightly_rounded_box(canvas, x + 6, y + 2, xy - 4, 11);
  283. canvas_set_color(canvas, ColorWhite);
  284. }
  285. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  286. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  287. canvas_set_color(canvas, ColorBlack);
  288. }
  289. void xremote_canvas_draw_frame(
  290. Canvas* canvas,
  291. bool pressed,
  292. uint8_t x,
  293. uint8_t y,
  294. uint8_t xl,
  295. const char* text) {
  296. elements_slightly_rounded_frame(canvas, x, y, xl, 15);
  297. if(pressed) {
  298. elements_slightly_rounded_box(canvas, x + 2, y + 2, xl - 4, 11);
  299. canvas_set_color(canvas, ColorWhite);
  300. }
  301. canvas_draw_str(canvas, x + 3, y + 11, text);
  302. canvas_set_color(canvas, ColorBlack);
  303. }