PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/multirom_ui_landscape.c

https://gitlab.com/adam.lukaitis/multirom
C | 284 lines | 221 code | 47 blank | 16 comment | 13 complexity | 5a66f48495ec8e4f59471cf882edbb50 MD5 | raw file
  1. /*
  2. * This file is part of MultiROM.
  3. *
  4. * MultiROM is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * MultiROM is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "multirom_ui.h"
  18. #include "multirom_ui_themes.h"
  19. #include "multirom.h"
  20. #include "version.h"
  21. #include "lib/framebuffer.h"
  22. #include "lib/util.h"
  23. #include "lib/button.h"
  24. #include "lib/input.h"
  25. #include "lib/log.h"
  26. #include "lib/animation.h"
  27. #include "lib/notification_card.h"
  28. #include "lib/tabview.h"
  29. #include "lib/colors.h"
  30. #define HEADER_HEIGHT (80*DPI_MUL)
  31. #define TABS_HEIGHT (HEADER_HEIGHT - STATUS_HEIGHT)
  32. #define MIRI_W (60*DPI_MUL)
  33. #define LISTVIEW_MARGIN (20*DPI_MUL)
  34. #define REFRESHBTN_W (400*DPI_MUL)
  35. #define REFRESHBTN_H (60*DPI_MUL)
  36. #define MISCBTN_W (530*DPI_MUL)
  37. #define MISCBTN_H (100*DPI_MUL)
  38. #define CLRBTN_W (50*DPI_MUL)
  39. #define CLRBTN_B (10*DPI_MUL)
  40. #define CLRBTN_TOTAL (CLRBTN_W+CLRBTN_B)
  41. #define CLRBTN_Y (1150*DPI_MUL)
  42. #define CLRBTN_MARGIN (8*DPI_MUL)
  43. #define SELECTED_RECT_H (6*DPI_MUL)
  44. #define BTN_SHADOW_OFF (5*DPI_MUL)
  45. static void destroy(UNUSED multirom_theme_data *t)
  46. {
  47. }
  48. static void header_set_tab_selector_pos(multirom_theme_data *t, float pos)
  49. {
  50. const int TAB_BTN_WIDTH = t->tab_btns[0]->w;
  51. int dest_x = t->tab_btns[0]->x + TAB_BTN_WIDTH*pos;
  52. int dest_w = TAB_BTN_WIDTH;
  53. const int selected = imin(TAB_COUNT-1, imax(0, (int)(pos+0.5f)));
  54. int i, rect_i = 0;
  55. for(i = 0; i < TAB_COUNT; ++i)
  56. {
  57. if(selected == i)
  58. continue;
  59. t->selected_rect[rect_i]->x = t->tab_texts[i]->x;
  60. t->selected_rect[rect_i]->y = t->tab_texts[i]->y;
  61. t->selected_rect[rect_i]->w = t->tab_texts[i]->w;
  62. t->selected_rect[rect_i]->h = t->tab_texts[i]->h;
  63. ++rect_i;
  64. }
  65. if(dest_x < t->tab_btns[0]->x)
  66. {
  67. dest_w -= t->tab_btns[0]->x - dest_x;
  68. dest_x = t->tab_btns[0]->x;
  69. }
  70. else if(dest_x > t->tab_btns[TAB_COUNT-1]->x)
  71. {
  72. dest_w = (t->tab_btns[TAB_COUNT-1]->x + t->tab_btns[TAB_COUNT-1]->w) - dest_x;
  73. }
  74. t->selected_tab_rect->x = dest_x;
  75. t->selected_tab_rect->w = dest_w;
  76. }
  77. static void init_header(multirom_theme_data *t)
  78. {
  79. button **tab_btns = t->tab_btns;
  80. fb_text **tab_texts = t->tab_texts;
  81. const int TAB_BTN_WIDTH = fb_width*0.21;
  82. int i, x;
  83. static const char *str[] = { "INTERNAL", "EXTERNAL", "MISC" };
  84. char buff[64];
  85. fb_add_rect_lvl(100, 0, 0, fb_width, HEADER_HEIGHT, C_HIGHLIGHT_BG);
  86. fb_add_rect(0, HEADER_HEIGHT, fb_width, (3*DPI_MUL), C_BTN_FAKE_SHADOW);
  87. ncard_set_top_offset(HEADER_HEIGHT);
  88. int maxW = 0;
  89. for(i = 0; i < TAB_COUNT; ++i)
  90. {
  91. fb_text_proto *p = fb_text_create(0, 0, C_HIGHLIGHT_TEXT, SIZE_NORMAL, str[i]);
  92. p->level = 110;
  93. p->style = STYLE_MEDIUM;
  94. tab_texts[i] = fb_text_finalize(p);
  95. maxW = imax(maxW, tab_texts[i]->w);
  96. }
  97. maxW += (30*DPI_MUL);
  98. x = fb_width/2 - (maxW*TAB_COUNT)/2;
  99. snprintf(buff, sizeof(buff), ":/miri_%dx%d.png", (int)MIRI_W, (int)MIRI_W);
  100. fb_add_png_img_lvl(110, 10*DPI_MUL, HEADER_HEIGHT/2 - MIRI_W/2, MIRI_W, MIRI_W, buff);
  101. for(i = 0; i < TAB_COUNT; ++i)
  102. {
  103. center_text(tab_texts[i], x, 0, maxW, HEADER_HEIGHT);
  104. tab_btns[i] = mzalloc(sizeof(button));
  105. tab_btns[i]->x = x;
  106. tab_btns[i]->y = 0;
  107. tab_btns[i]->w = maxW;
  108. tab_btns[i]->h = HEADER_HEIGHT;
  109. tab_btns[i]->clicked_data = malloc(sizeof(int));
  110. *((int*)tab_btns[i]->clicked_data) = i;
  111. tab_btns[i]->clicked = &multirom_ui_switch_btn;
  112. tab_btns[i]->level_off = 100;
  113. button_init_ui(tab_btns[i], "", 0);
  114. keyaction_add(tab_btns[i], button_keyaction_call, tab_btns[i]);
  115. x += maxW;
  116. if(i < TAB_COUNT-1)
  117. t->selected_rect[i] = fb_add_rect_lvl(120, 0, 0, 0, 0, (0x4C << 24) | (C_HIGHLIGHT_BG & 0x00FFFFFF));
  118. }
  119. t->selected_tab_rect = fb_add_rect_lvl(110, tab_btns[0]->x, HEADER_HEIGHT-SELECTED_RECT_H + (3*DPI_MUL), maxW, SELECTED_RECT_H, C_HIGHLIGHT_TEXT);
  120. t->tabs = tabview_create(0, HEADER_HEIGHT, fb_width, fb_height-HEADER_HEIGHT);
  121. header_set_tab_selector_pos(t, 0.f);
  122. }
  123. static void tab_rom_init(UNUSED multirom_theme_data *t, tab_data_roms *d, UNUSED int tab_type)
  124. {
  125. d->list->x = fb_width/2 - fb_height/2;
  126. d->list->y = HEADER_HEIGHT + LISTVIEW_MARGIN;
  127. d->list->w = fb_height;
  128. d->list->h = fb_height - d->list->y - LISTVIEW_MARGIN;
  129. }
  130. static void tab_misc_init(multirom_theme_data *t, tab_data_misc *d, int color_scheme)
  131. {
  132. int i;
  133. int x = fb_width/2 - (MISCBTN_W + 30*DPI_MUL);
  134. int y = HEADER_HEIGHT + ((fb_height - HEADER_HEIGHT)/2 - 2*(MISCBTN_H + 30*DPI_MUL));
  135. fb_rect *shadow;
  136. y += MISCBTN_H + 30*DPI_MUL;
  137. button *b = mzalloc(sizeof(button));
  138. b->x = x;
  139. b->y = y;
  140. b->w = MISCBTN_W;
  141. b->h = MISCBTN_H;
  142. b->clicked = &multirom_ui_tab_misc_copy_log;
  143. shadow = fb_add_rect_lvl(LEVEL_RECT, b->x + BTN_SHADOW_OFF, b->y + BTN_SHADOW_OFF, b->w, b->h, C_BTN_FAKE_SHADOW);
  144. button_init_ui(b, "COPY LOG TO /SDCARD", SIZE_NORMAL);
  145. list_add(&d->buttons, b);
  146. list_add(&d->ui_elements, shadow);
  147. tabview_add_item(t->tabs, TAB_MISC, b->text);
  148. tabview_add_item(t->tabs, TAB_MISC, b->rect);
  149. tabview_add_item(t->tabs, TAB_MISC, b);
  150. const int max_colors = colors_count();
  151. x += (MISCBTN_W/2 - (max_colors*(CLRBTN_TOTAL+CLRBTN_MARGIN))/2);
  152. y += MISCBTN_H+30*DPI_MUL + (MISCBTN_H/2 - CLRBTN_TOTAL/2);
  153. fb_rect *r;
  154. for(i = 0; i < max_colors; ++i)
  155. {
  156. const struct mrom_color_theme *th = colors_get(i);
  157. r = fb_add_rect(x, y, CLRBTN_TOTAL, CLRBTN_TOTAL, i == color_scheme ? 0xFFFFCC00 : WHITE);
  158. list_add(&d->ui_elements, r);
  159. r = fb_add_rect(x+CLRBTN_B/2, y+CLRBTN_B/2, CLRBTN_W, CLRBTN_W, th->highlight_bg);
  160. list_add(&d->ui_elements, r);
  161. b = mzalloc(sizeof(button));
  162. b->x = x;
  163. b->y = y;
  164. b->w = CLRBTN_TOTAL;
  165. b->h = CLRBTN_TOTAL;
  166. b->clicked_data = malloc(sizeof(int));
  167. *((int*)b->clicked_data) = i;
  168. b->clicked = &multirom_ui_tab_misc_change_clr;
  169. button_init_ui(b, NULL, 0);
  170. list_add(&d->buttons, b);
  171. tabview_add_item(t->tabs, TAB_MISC, b);
  172. x += CLRBTN_TOTAL + CLRBTN_MARGIN;
  173. }
  174. x = fb_width/2 - (MISCBTN_W + 30*DPI_MUL) + MISCBTN_W + 30*DPI_MUL;
  175. y = HEADER_HEIGHT + ((fb_height - HEADER_HEIGHT)/2 - 2*(MISCBTN_H + 30*DPI_MUL));
  176. static const char *texts[] =
  177. {
  178. "REBOOT", // 0
  179. "REBOOT TO RECOVERY", // 1
  180. "REBOOT TO BOOTLOADER", // 2
  181. "SHUTDOWN", // 3
  182. NULL
  183. };
  184. static const int exit_codes[] = {
  185. UI_EXIT_REBOOT, UI_EXIT_REBOOT_RECOVERY,
  186. UI_EXIT_REBOOT_BOOTLOADER, UI_EXIT_SHUTDOWN
  187. };
  188. for(i = 0; texts[i]; ++i)
  189. {
  190. b = mzalloc(sizeof(button));
  191. b->x = x;
  192. b->y = y;
  193. b->w = MISCBTN_W;
  194. b->h = MISCBTN_H;
  195. b->clicked_data = malloc(sizeof(int));
  196. *((int*)b->clicked_data) = exit_codes[i];
  197. b->clicked = &multirom_ui_reboot_btn;
  198. shadow = fb_add_rect_lvl(LEVEL_RECT, b->x + BTN_SHADOW_OFF, b->y + BTN_SHADOW_OFF, b->w, b->h, C_BTN_FAKE_SHADOW);
  199. button_init_ui(b, texts[i], SIZE_NORMAL);
  200. list_add(&d->buttons, b);
  201. list_add(&d->ui_elements, shadow);
  202. tabview_add_item(t->tabs, TAB_MISC, b->text);
  203. tabview_add_item(t->tabs, TAB_MISC, b->rect);
  204. tabview_add_item(t->tabs, TAB_MISC, b);
  205. y += MISCBTN_H+30*DPI_MUL;
  206. }
  207. fb_text *text = fb_add_text(5*DPI_MUL, 0, C_TEXT_SECONDARY, SIZE_SMALL, "MultiROM v%d"VERSION_DEV_FIX" with trampoline v%d.",
  208. VERSION_MULTIROM, multirom_get_trampoline_ver());
  209. text->y = fb_height - text->h;
  210. list_add(&d->ui_elements, text);
  211. text = fb_add_text(0, 0, C_TEXT_SECONDARY, SIZE_SMALL, "Battery: %d%%", multirom_get_battery());
  212. text->x = fb_width - text->w - 5*DPI_MUL;
  213. text->y = fb_height - text->h;
  214. list_add(&d->ui_elements, text);
  215. for(i = 0; d->buttons[i]; ++i)
  216. keyaction_add(d->buttons[i], button_keyaction_call, d->buttons[i]);
  217. tabview_add_items(t->tabs, TAB_MISC, d->ui_elements);
  218. }
  219. static int get_tab_width(UNUSED multirom_theme_data *t)
  220. {
  221. return fb_width;
  222. }
  223. static int get_tab_height(UNUSED multirom_theme_data *t)
  224. {
  225. return fb_height - HEADER_HEIGHT;
  226. }
  227. const struct multirom_theme theme_info_landscape = {
  228. .width = TH_LANDSCAPE,
  229. .height = TH_LANDSCAPE,
  230. .destroy = &destroy,
  231. .init_header = &init_header,
  232. .header_set_tab_selector_pos = &header_set_tab_selector_pos,
  233. .tab_rom_init = &tab_rom_init,
  234. .tab_misc_init = &tab_misc_init,
  235. .get_tab_width = &get_tab_width,
  236. .get_tab_height = &get_tab_height,
  237. };