xremote_common_view.c 11 KB

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