gui.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "gui.h"
  2. #include "gui_i.h"
  3. #include <flipper.h>
  4. #include <flipper_v2.h>
  5. #include <m-array.h>
  6. #include <stdio.h>
  7. #include "gui_event.h"
  8. #include "canvas.h"
  9. #include "canvas_i.h"
  10. #include "widget.h"
  11. #include "widget_i.h"
  12. ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST);
  13. struct Gui {
  14. GuiEvent* event;
  15. Canvas* canvas;
  16. WidgetArray_t layers[GuiLayerMAX];
  17. osMutexId_t mutex;
  18. };
  19. Widget* gui_widget_find_enabled(WidgetArray_t array) {
  20. size_t widgets_count = WidgetArray_size(array);
  21. for(size_t i = 0; i < widgets_count; i++) {
  22. Widget* widget = *WidgetArray_get(array, widgets_count - i - 1);
  23. if(widget_is_enabled(widget)) {
  24. return widget;
  25. }
  26. }
  27. return NULL;
  28. }
  29. void gui_update(Gui* gui) {
  30. furi_assert(gui);
  31. GuiMessage message;
  32. message.type = GuiMessageTypeRedraw;
  33. gui_event_messsage_send(gui->event, &message);
  34. }
  35. bool gui_redraw_fs(Gui* gui) {
  36. canvas_frame_set(gui->canvas, 0, 0, GUI_DISPLAY_WIDTH, GUI_DISPLAY_HEIGHT);
  37. Widget* widget = gui_widget_find_enabled(gui->layers[GuiLayerFullscreen]);
  38. if(widget) {
  39. widget_draw(widget, gui->canvas);
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. void gui_redraw_status_bar(Gui* gui) {
  46. WidgetArray_it_t it;
  47. uint8_t x;
  48. uint8_t x_used = 0;
  49. uint8_t width;
  50. Widget* widget;
  51. // Right side
  52. x = 128;
  53. WidgetArray_it(it, gui->layers[GuiLayerStatusBarRight]);
  54. while(!WidgetArray_end_p(it) && x_used < GUI_STATUS_BAR_WIDTH) {
  55. // Render widget;
  56. widget = *WidgetArray_ref(it);
  57. if(widget_is_enabled(widget)) {
  58. width = widget_get_width(widget);
  59. if(!width) width = 8;
  60. x_used += width;
  61. x -= (width + 2);
  62. canvas_frame_set(gui->canvas, x, GUI_STATUS_BAR_Y, width, GUI_STATUS_BAR_HEIGHT);
  63. widget_draw(widget, gui->canvas);
  64. }
  65. WidgetArray_next(it);
  66. }
  67. // Left side
  68. x = 0;
  69. WidgetArray_it(it, gui->layers[GuiLayerStatusBarLeft]);
  70. while(!WidgetArray_end_p(it) && x_used < GUI_STATUS_BAR_WIDTH) {
  71. // Render widget;
  72. widget = *WidgetArray_ref(it);
  73. if(widget_is_enabled(widget)) {
  74. width = widget_get_width(widget);
  75. if(!width) width = 8;
  76. x_used += width;
  77. canvas_frame_set(gui->canvas, x, GUI_STATUS_BAR_Y, width, GUI_STATUS_BAR_HEIGHT);
  78. widget_draw(widget, gui->canvas);
  79. x += (width + 2);
  80. }
  81. WidgetArray_next(it);
  82. }
  83. }
  84. bool gui_redraw_normal(Gui* gui) {
  85. canvas_frame_set(gui->canvas, GUI_MAIN_X, GUI_MAIN_Y, GUI_MAIN_WIDTH, GUI_MAIN_HEIGHT);
  86. Widget* widget = gui_widget_find_enabled(gui->layers[GuiLayerMain]);
  87. if(widget) {
  88. widget_draw(widget, gui->canvas);
  89. return true;
  90. }
  91. return false;
  92. }
  93. bool gui_redraw_none(Gui* gui) {
  94. canvas_frame_set(gui->canvas, GUI_MAIN_X, GUI_MAIN_Y, GUI_MAIN_WIDTH, GUI_MAIN_HEIGHT);
  95. Widget* widget = gui_widget_find_enabled(gui->layers[GuiLayerNone]);
  96. if(widget) {
  97. widget_draw(widget, gui->canvas);
  98. return true;
  99. }
  100. return false;
  101. }
  102. void gui_redraw(Gui* gui) {
  103. furi_assert(gui);
  104. gui_lock(gui);
  105. canvas_reset(gui->canvas);
  106. if(!gui_redraw_fs(gui)) {
  107. if(!gui_redraw_normal(gui)) {
  108. gui_redraw_none(gui);
  109. }
  110. gui_redraw_status_bar(gui);
  111. }
  112. canvas_commit(gui->canvas);
  113. gui_unlock(gui);
  114. }
  115. void gui_input(Gui* gui, InputEvent* input_event) {
  116. furi_assert(gui);
  117. furi_assert(input_event);
  118. gui_lock(gui);
  119. Widget* widget = gui_widget_find_enabled(gui->layers[GuiLayerFullscreen]);
  120. if(!widget) widget = gui_widget_find_enabled(gui->layers[GuiLayerMain]);
  121. if(!widget) widget = gui_widget_find_enabled(gui->layers[GuiLayerNone]);
  122. if(widget) {
  123. widget_input(widget, input_event);
  124. }
  125. gui_unlock(gui);
  126. }
  127. void gui_lock(Gui* gui) {
  128. furi_assert(gui);
  129. furi_check(osMutexAcquire(gui->mutex, osWaitForever) == osOK);
  130. }
  131. void gui_unlock(Gui* gui) {
  132. furi_assert(gui);
  133. furi_check(osMutexRelease(gui->mutex) == osOK);
  134. }
  135. void gui_add_widget(Gui* gui, Widget* widget, GuiLayer layer) {
  136. furi_assert(gui);
  137. furi_assert(widget);
  138. furi_check(layer < GuiLayerMAX);
  139. gui_lock(gui);
  140. WidgetArray_push_back(gui->layers[layer], widget);
  141. widget_gui_set(widget, gui);
  142. gui_unlock(gui);
  143. gui_update(gui);
  144. }
  145. void gui_remove_widget(Gui* gui, Widget* widget) {
  146. furi_assert(gui);
  147. furi_assert(widget);
  148. gui_lock(gui);
  149. widget_gui_set(widget, NULL);
  150. WidgetArray_it_t it;
  151. for(size_t i = 0; i < GuiLayerMAX; i++) {
  152. WidgetArray_it(it, gui->layers[i]);
  153. while(!WidgetArray_end_p(it)) {
  154. if(*WidgetArray_ref(it) == widget) {
  155. WidgetArray_remove(gui->layers[i], it);
  156. }
  157. WidgetArray_next(it);
  158. }
  159. }
  160. gui_unlock(gui);
  161. }
  162. Gui* gui_alloc() {
  163. Gui* gui = furi_alloc(sizeof(Gui));
  164. // Allocate mutex
  165. gui->mutex = osMutexNew(NULL);
  166. furi_check(gui->mutex);
  167. // Event dispatcher
  168. gui->event = gui_event_alloc();
  169. // Drawing canvas
  170. gui->canvas = canvas_init();
  171. // Compose Layers
  172. for(size_t i = 0; i < GuiLayerMAX; i++) {
  173. WidgetArray_init(gui->layers[i]);
  174. }
  175. return gui;
  176. }
  177. void gui_task(void* p) {
  178. Gui* gui = gui_alloc();
  179. // Create FURI record
  180. if(!furi_create("gui", gui)) {
  181. printf("[gui_task] cannot create the gui record\n");
  182. furiac_exit(NULL);
  183. }
  184. furiac_ready();
  185. // Forever dispatch
  186. while(1) {
  187. GuiMessage message = gui_event_message_next(gui->event);
  188. if(message.type == GuiMessageTypeRedraw) {
  189. gui_redraw(gui);
  190. } else if(message.type == GuiMessageTypeInput) {
  191. gui_input(gui, &message.input);
  192. }
  193. }
  194. }