xremote_common_view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. {
  16. { 0, XREMOTE_COMMAND_POWER },
  17. { 1, XREMOTE_COMMAND_SETUP },
  18. { 2, XREMOTE_COMMAND_INPUT },
  19. { 3, XREMOTE_COMMAND_MENU },
  20. { 4, XREMOTE_COMMAND_LIST },
  21. { 5, XREMOTE_COMMAND_INFO },
  22. { 6, XREMOTE_COMMAND_BACK },
  23. { 7, XREMOTE_COMMAND_OK },
  24. { 8, XREMOTE_COMMAND_UP },
  25. { 9, XREMOTE_COMMAND_DOWN },
  26. { 10, XREMOTE_COMMAND_LEFT },
  27. { 11, XREMOTE_COMMAND_RIGHT },
  28. { 12, XREMOTE_COMMAND_JUMP_FORWARD },
  29. { 13, XREMOTE_COMMAND_JUMP_BACKWARD },
  30. { 14, XREMOTE_COMMAND_FAST_FORWARD },
  31. { 15, XREMOTE_COMMAND_FAST_BACKWARD },
  32. { 16, XREMOTE_COMMAND_PLAY_PAUSE },
  33. { 17, XREMOTE_COMMAND_PAUSE },
  34. { 18, XREMOTE_COMMAND_PLAY },
  35. { 19, XREMOTE_COMMAND_STOP },
  36. { 20, XREMOTE_COMMAND_MUTE },
  37. { 21, XREMOTE_COMMAND_MODE },
  38. { 22, XREMOTE_COMMAND_VOL_UP },
  39. { 23, XREMOTE_COMMAND_VOL_DOWN },
  40. { 24, XREMOTE_COMMAND_NEXT_CHAN },
  41. { 25, XREMOTE_COMMAND_PREV_CHAN },
  42. { -1, NULL }
  43. };
  44. const char* xremote_button_get_name(int index)
  45. {
  46. if (index > XREMOTE_BUTTON_COUNT) return NULL;
  47. return g_buttons[index].name;
  48. }
  49. struct XRemoteView {
  50. XRemoteClearCallback on_clear;
  51. XRemoteAppContext* app_ctx;
  52. View* view;
  53. void *context;
  54. };
  55. XRemoteView* xremote_view_alloc(void* app_ctx, ViewInputCallback input_cb, ViewDrawCallback draw_cb)
  56. {
  57. XRemoteView* remote_view = malloc(sizeof(XRemoteView));
  58. remote_view->app_ctx = app_ctx;
  59. remote_view->view = view_alloc();
  60. remote_view->context = NULL;
  61. remote_view->on_clear = NULL;
  62. view_set_orientation(remote_view->view, ((XRemoteAppContext*)app_ctx)->app_settings->orientation);
  63. view_allocate_model(remote_view->view, ViewModelTypeLocking, sizeof(XRemoteViewModel));
  64. view_set_input_callback(remote_view->view, input_cb);
  65. view_set_draw_callback(remote_view->view, draw_cb);
  66. view_set_context(remote_view->view, remote_view);
  67. return remote_view;
  68. }
  69. void xremote_view_clear_context(XRemoteView* rview)
  70. {
  71. furi_assert(rview);
  72. if (rview->context && rview->on_clear)
  73. rview->on_clear(rview->context);
  74. rview->context = NULL;
  75. }
  76. void xremote_view_set_context(XRemoteView* rview, void *context, XRemoteClearCallback on_clear)
  77. {
  78. xremote_view_clear_context(rview);
  79. rview->context = context;
  80. rview->on_clear = on_clear;
  81. }
  82. void* xremote_view_get_context(XRemoteView* rview)
  83. {
  84. furi_assert(rview);
  85. return rview->context;
  86. }
  87. void* xremote_view_get_app_context(XRemoteView* rview)
  88. {
  89. furi_assert(rview);
  90. return rview->app_ctx;
  91. }
  92. void xremote_view_free(XRemoteView* rview)
  93. {
  94. furi_assert(rview);
  95. xremote_view_clear_context(rview);
  96. view_free(rview->view);
  97. free(rview);
  98. }
  99. View* xremote_view_get_view(XRemoteView* rview)
  100. {
  101. furi_assert(rview);
  102. return rview->view;
  103. }
  104. InfraredRemoteButton* xremote_view_get_button_by_name(XRemoteView *rview, const char* name)
  105. {
  106. xremote_app_assert(rview->context, NULL);
  107. InfraredRemote* remote = (InfraredRemote*)rview->context;
  108. return infrared_remote_get_button_by_name(remote, name);
  109. }
  110. bool xremote_view_press_button(XRemoteView *rview, InfraredRemoteButton* button)
  111. {
  112. xremote_app_assert(button, false);
  113. XRemoteAppSettings* settings = rview->app_ctx->app_settings;
  114. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  115. xremote_app_assert(signal, false);
  116. infrared_signal_transmit_times(signal, settings->repeat_count);
  117. xremote_app_context_notify_led(rview->app_ctx);
  118. return true;
  119. }
  120. bool xremote_view_send_ir_msg_by_name(XRemoteView *rview, const char *name)
  121. {
  122. InfraredRemoteButton* button = xremote_view_get_button_by_name(rview, name);
  123. return (button != NULL) ? xremote_view_press_button(rview, button) : false;
  124. }
  125. void xremote_canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, XRemoteIcon icon)
  126. {
  127. if (icon == XRemoteIconEnter)
  128. {
  129. canvas_draw_circle(canvas, x - 2, y, 4);
  130. canvas_draw_disc(canvas, x - 2, y, 2);
  131. }
  132. else if (icon == XRemoteIconBack)
  133. {
  134. canvas_draw_triangle(canvas, x - 4, y - 2, 5, 3, CanvasDirectionRightToLeft);
  135. canvas_draw_line(canvas, x + 2, y + 1, x + 1, y + 2);
  136. canvas_draw_line(canvas, x + 2, y, x + 1, y - 1);
  137. canvas_draw_line(canvas, x, y - 2, x - 5, y - 2);
  138. canvas_draw_line(canvas, x, y + 3, x - 3, y + 3);
  139. }
  140. else if (icon == XRemoteIconArrowUp)
  141. {
  142. canvas_draw_triangle(canvas, x - 2, y - 2, 5, 3, CanvasDirectionBottomToTop);
  143. canvas_draw_line(canvas, x - 2, y - 3, x - 2, y + 4);
  144. }
  145. else if (icon == XRemoteIconArrowDown)
  146. {
  147. canvas_draw_triangle(canvas, x - 2, y + 2, 5, 3, CanvasDirectionTopToBottom);
  148. canvas_draw_line(canvas, x - 2, y - 4, x - 2, y + 3);
  149. }
  150. else if (icon == XRemoteIconArrowLeft)
  151. {
  152. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionRightToLeft);
  153. canvas_draw_line(canvas, x + 2, y, x - 5, y);
  154. }
  155. else if (icon == XRemoteIconArrowRight)
  156. {
  157. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionLeftToRight);
  158. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  159. }
  160. else if (icon == XRemoteIconJumpForward)
  161. {
  162. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionLeftToRight);
  163. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  164. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  165. canvas_draw_line(canvas, x - 4, y, x, y);
  166. }
  167. else if (icon == XRemoteIconJumpBackward)
  168. {
  169. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionRightToLeft);
  170. canvas_draw_triangle(canvas, x + 1, y, 5, 3, CanvasDirectionRightToLeft);
  171. canvas_draw_line(canvas, x - 5, y - 2, x - 5, y + 2);
  172. canvas_draw_line(canvas, x, y, x - 4, y);
  173. }
  174. else if (icon == XRemoteIconFastForward)
  175. {
  176. canvas_draw_triangle(canvas, x - 1, y, 5, 3, CanvasDirectionLeftToRight);
  177. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionLeftToRight);
  178. canvas_draw_line(canvas, x - 3, y, x, y);
  179. }
  180. else if (icon == XRemoteIconFastBackward)
  181. {
  182. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionRightToLeft);
  183. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionRightToLeft);
  184. canvas_draw_line(canvas, x - 1, y, x - 4, y);
  185. }
  186. else if (icon == XRemoteIconPlayPause)
  187. {
  188. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  189. canvas_draw_dot(canvas, x - 4, y);
  190. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  191. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  192. }
  193. else if (icon == XRemoteIconPlay)
  194. {
  195. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionLeftToRight);
  196. canvas_draw_dot(canvas, x - 2, y);
  197. }
  198. else if (icon == XRemoteIconPause)
  199. {
  200. canvas_draw_line(canvas, x - 3, y - 2, x - 3, y + 2);
  201. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  202. }
  203. else if (icon == XRemoteIconStop)
  204. {
  205. canvas_draw_box(canvas, x - 4, y - 2, 5, 5);
  206. }
  207. else if (icon == XRemoteIconOk)
  208. {
  209. canvas_draw_str(canvas, x - 7, y + 4, "OK");
  210. }
  211. }
  212. void xremote_canvas_draw_header(Canvas* canvas, ViewOrientation orient, const char* section)
  213. {
  214. Align align = AlignLeft;
  215. uint8_t x = 0;
  216. if (orient == ViewOrientationHorizontal)
  217. {
  218. align = AlignRight;
  219. x = 128;
  220. }
  221. canvas_set_font(canvas, FontPrimary);
  222. elements_multiline_text_aligned(canvas, x, 0, align, AlignTop, "XRemote");
  223. canvas_set_font(canvas, FontSecondary);
  224. if (section != NULL)
  225. elements_multiline_text_aligned(canvas, x, 12, align, AlignTop, section);
  226. }
  227. void xremote_canvas_draw_exit_footer(Canvas* canvas, ViewOrientation orient, const char *text)
  228. {
  229. canvas_set_font(canvas, FontSecondary);
  230. if (orient == ViewOrientationVertical)
  231. {
  232. xremote_canvas_draw_icon(canvas, 6, 124, XRemoteIconBack);
  233. elements_multiline_text_aligned(canvas, 12, 128, AlignLeft, AlignBottom, text);
  234. }
  235. else
  236. {
  237. uint8_t x = strncmp(text, "Hold", 4) ? 71 : 76;
  238. xremote_canvas_draw_icon(canvas, x, 60, XRemoteIconBack);
  239. elements_multiline_text_aligned(canvas, 128, 64, AlignRight, AlignBottom, text);
  240. }
  241. }
  242. void xremote_canvas_draw_button(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, XRemoteIcon icon)
  243. {
  244. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  245. if (pressed)
  246. {
  247. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  248. canvas_set_color(canvas, ColorWhite);
  249. }
  250. xremote_canvas_draw_icon(canvas, x + 11, y + 8, icon);
  251. canvas_set_color(canvas, ColorBlack);
  252. }
  253. void xremote_canvas_draw_button_png(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, const Icon* icon)
  254. {
  255. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  256. if (pressed)
  257. {
  258. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  259. canvas_set_color(canvas, ColorWhite);
  260. }
  261. canvas_draw_icon(canvas, x + 4, y + 3, icon);
  262. canvas_set_color(canvas, ColorBlack);
  263. }
  264. void xremote_canvas_draw_button_wide(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, char* text, XRemoteIcon icon)
  265. {
  266. elements_slightly_rounded_frame(canvas, x + 4, y, 56, 15);
  267. if (pressed)
  268. {
  269. elements_slightly_rounded_box(canvas, x + 6, y + 2, 52, 11);
  270. canvas_set_color(canvas, ColorWhite);
  271. }
  272. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  273. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  274. canvas_set_color(canvas, ColorBlack);
  275. }
  276. void xremote_canvas_draw_button_size(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, uint8_t xy, char* text, XRemoteIcon icon)
  277. {
  278. elements_slightly_rounded_frame(canvas, x + 4, y, xy, 15);
  279. if (pressed)
  280. {
  281. elements_slightly_rounded_box(canvas, x + 6, y + 2, xy - 4, 11);
  282. canvas_set_color(canvas, ColorWhite);
  283. }
  284. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  285. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  286. canvas_set_color(canvas, ColorBlack);
  287. }
  288. void xremote_canvas_draw_frame(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, uint8_t xl, const char *text)
  289. {
  290. elements_slightly_rounded_frame(canvas, x, y, xl, 15);
  291. if (pressed)
  292. {
  293. elements_slightly_rounded_box(canvas, x + 2, y + 2, xl - 4, 11);
  294. canvas_set_color(canvas, ColorWhite);
  295. }
  296. canvas_draw_str(canvas, x + 3, y + 11, text);
  297. canvas_set_color(canvas, ColorBlack);
  298. }