xremote_navigation_view.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. * @file flipper-xremote/views/xremote_navigation_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 Navigation page view callbacks and infrared functionality.
  7. */
  8. #include "xremote_navigation_view.h"
  9. #include "../xremote_app.h"
  10. static void xremote_navigation_view_draw_vertical(Canvas* canvas, XRemoteViewModel* model)
  11. {
  12. XRemoteAppContext *app_ctx = model->context;
  13. xremote_canvas_draw_button(canvas, model->up_pressed, 23, 30, XRemoteIconArrowUp);
  14. xremote_canvas_draw_button(canvas, model->down_pressed, 23, 72, XRemoteIconArrowDown);
  15. xremote_canvas_draw_button(canvas, model->left_pressed, 2, 51, XRemoteIconArrowLeft);
  16. xremote_canvas_draw_button(canvas, model->right_pressed, 44, 51, XRemoteIconArrowRight);
  17. xremote_canvas_draw_button(canvas, model->ok_pressed, 23, 51, XRemoteIconOk);
  18. xremote_canvas_draw_button(canvas, model->back_pressed, 2, 95, XRemoteIconBack);
  19. if (app_ctx->app_settings->exit_behavior == XRemoteAppExitPress)
  20. canvas_draw_icon(canvas, 22, 107, &I_Hold_Text_17x4);
  21. }
  22. static void xremote_navigation_view_draw_horizontal(Canvas* canvas, XRemoteViewModel* model)
  23. {
  24. XRemoteAppContext *app_ctx = model->context;
  25. xremote_canvas_draw_button(canvas, model->up_pressed, 23, 2, XRemoteIconArrowUp);
  26. xremote_canvas_draw_button(canvas, model->down_pressed, 23, 44, XRemoteIconArrowDown);
  27. xremote_canvas_draw_button(canvas, model->left_pressed, 2, 23, XRemoteIconArrowLeft);
  28. xremote_canvas_draw_button(canvas, model->right_pressed, 44, 23, XRemoteIconArrowRight);
  29. xremote_canvas_draw_button(canvas, model->ok_pressed, 23, 23, XRemoteIconOk);
  30. xremote_canvas_draw_button(canvas, model->back_pressed, 70, 33, XRemoteIconBack);
  31. if (app_ctx->app_settings->exit_behavior == XRemoteAppExitPress)
  32. canvas_draw_icon(canvas, 90, 45, &I_Hold_Text_17x4);
  33. }
  34. static void xremote_navigation_view_draw_callback(Canvas* canvas, void* context)
  35. {
  36. furi_assert(context);
  37. XRemoteViewModel* model = context;
  38. XRemoteAppContext *app_ctx = model->context;
  39. ViewOrientation orientation = app_ctx->app_settings->orientation;
  40. const char *exit_str = xremote_app_context_get_exit_str(app_ctx);
  41. XRemoteViewDrawFunction xremote_navigation_view_draw_body;
  42. xremote_navigation_view_draw_body = orientation == ViewOrientationVertical ?
  43. xremote_navigation_view_draw_vertical : xremote_navigation_view_draw_horizontal;
  44. xremote_canvas_draw_header(canvas, orientation, "Navigation");
  45. xremote_navigation_view_draw_body(canvas, model);
  46. xremote_canvas_draw_exit_footer(canvas, orientation, exit_str);
  47. }
  48. static void xremote_navigation_view_process(XRemoteView* view, InputEvent* event)
  49. {
  50. with_view_model(
  51. xremote_view_get_view(view),
  52. XRemoteViewModel* model,
  53. {
  54. XRemoteAppContext* app_ctx = xremote_view_get_app_context(view);
  55. XRemoteAppExit exit = app_ctx->app_settings->exit_behavior;
  56. InfraredRemoteButton* button = NULL;
  57. model->context = app_ctx;
  58. if (event->type == InputTypePress)
  59. {
  60. if (event->key == InputKeyUp)
  61. {
  62. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_UP);
  63. if (xremote_view_press_button(view, button)) model->up_pressed = true;
  64. }
  65. else if (event->key == InputKeyDown)
  66. {
  67. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_DOWN);
  68. if (xremote_view_press_button(view, button)) model->down_pressed = true;
  69. }
  70. else if (event->key == InputKeyLeft)
  71. {
  72. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_LEFT);
  73. if (xremote_view_press_button(view, button)) model->left_pressed = true;
  74. }
  75. else if (event->key == InputKeyRight)
  76. {
  77. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_RIGHT);
  78. if (xremote_view_press_button(view, button)) model->right_pressed = true;
  79. }
  80. else if (event->key == InputKeyOk)
  81. {
  82. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_OK);
  83. if (xremote_view_press_button(view, button)) model->ok_pressed = true;
  84. }
  85. }
  86. else if (event->type == InputTypeShort &&
  87. event->key == InputKeyBack &&
  88. exit == XRemoteAppExitHold)
  89. {
  90. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_BACK);
  91. if (xremote_view_press_button(view, button)) model->back_pressed = true;
  92. }
  93. else if (event->type == InputTypeLong &&
  94. event->key == InputKeyBack &&
  95. exit == XRemoteAppExitPress)
  96. {
  97. button = xremote_view_get_button_by_name(view, XREMOTE_COMMAND_BACK);
  98. if (xremote_view_press_button(view, button)) model->back_pressed = true;
  99. }
  100. else if (event->type == InputTypeRelease)
  101. {
  102. if (event->key == InputKeyUp) model->up_pressed = false;
  103. else if (event->key == InputKeyDown) model->down_pressed = false;
  104. else if (event->key == InputKeyLeft) model->left_pressed = false;
  105. else if (event->key == InputKeyRight) model->right_pressed = false;
  106. else if (event->key == InputKeyOk) model->ok_pressed = false;
  107. else if (event->key == InputKeyBack) model->back_pressed = false;
  108. }
  109. },
  110. true);
  111. }
  112. static bool xremote_navigation_view_input_callback(InputEvent* event, void* context)
  113. {
  114. furi_assert(context);
  115. XRemoteView* view = (XRemoteView*)context;
  116. XRemoteAppContext* app_ctx = xremote_view_get_app_context(view);
  117. XRemoteAppExit exit = app_ctx->app_settings->exit_behavior;
  118. if (event->key == InputKeyBack &&
  119. event->type == InputTypeShort &&
  120. exit == XRemoteAppExitPress) return false;
  121. else if (event->key == InputKeyBack &&
  122. event->type == InputTypeLong &&
  123. exit == XRemoteAppExitHold) return false;
  124. xremote_navigation_view_process(view, event);
  125. return true;
  126. }
  127. XRemoteView* xremote_navigation_view_alloc(void* app_ctx)
  128. {
  129. XRemoteView *view = xremote_view_alloc(app_ctx,
  130. xremote_navigation_view_input_callback,
  131. xremote_navigation_view_draw_callback);
  132. with_view_model(
  133. xremote_view_get_view(view),
  134. XRemoteViewModel* model,
  135. {
  136. model->context = app_ctx;
  137. model->up_pressed = false;
  138. model->down_pressed = false;
  139. model->left_pressed = false;
  140. model->right_pressed = false;
  141. model->back_pressed = false;
  142. model->ok_pressed = false;
  143. },
  144. true
  145. );
  146. return view;
  147. }