textui.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "textui.h"
  2. #include "screen_manager.h"
  3. #include "driver/uart/uart.h"
  4. #include <common/string.h>
  5. #include <common/printk.h>
  6. #include <common/atomic.h>
  7. struct scm_ui_framework_t textui_framework;
  8. static spinlock_t __window_id_lock = {1};
  9. static uint32_t __window_max_id = 0;
  10. // 暂时初始化16080个初始字符对象以及67个虚拟行对象
  11. #define INITIAL_CHARS 16080
  12. #define INITIAL_VLINES (int)(1080 / 16)
  13. static struct textui_char_chromatic_t __initial_chars[INITIAL_CHARS] = {0};
  14. static struct textui_vline_chromatic_t __initial_vlines[INITIAL_VLINES] = {0};
  15. static struct textui_window_t __initial_window = {0}; // 初始窗口
  16. static struct textui_private_info_t __private_info = {0};
  17. static struct List __windows_list;
  18. /**
  19. * @brief 初始化window对象
  20. *
  21. * @param window 窗口对象
  22. * @param flags 标志位
  23. * @param vlines_num 虚拟行的总数
  24. * @param vlines_ptr 虚拟行数组指针
  25. * @param cperline 每行最大的字符数
  26. */
  27. static int __textui_init_window(struct textui_window_t *window, uint8_t flags, uint16_t vlines_num, void *vlines_ptr, uint16_t cperline)
  28. {
  29. memset((window), 0, sizeof(struct textui_window_t));
  30. list_init(&(window)->list);
  31. window->lock.lock = 1;
  32. spin_lock(&__window_id_lock);
  33. window->id = __window_max_id++;
  34. spin_unlock(&__window_id_lock);
  35. window->flags = flags;
  36. window->vlines_num = vlines_num;
  37. window->vlines_used = 1;
  38. window->top_vline = 0;
  39. window->vline_operating = 0;
  40. window->chars_per_line = cperline;
  41. if (textui_is_chromatic(flags))
  42. window->vlines.chromatic = vlines_ptr;
  43. else
  44. window->vlines.normal = vlines_ptr;
  45. list_add(&__windows_list, &(window)->list);
  46. }
  47. /**
  48. * @brief 初始化虚拟行对象
  49. *
  50. * @param vline 虚拟行对象指针
  51. * @param chars_ptr 字符对象数组指针
  52. */
  53. #define __textui_init_vline(vline, chars_ptr) \
  54. do \
  55. { \
  56. memset(vline, 0, sizeof(struct textui_vline_chromatic_t)); \
  57. (vline)->index = 0; \
  58. (vline)->chars = chars_ptr; \
  59. } while (0)
  60. int textui_install_handler(struct scm_buffer_info_t *buf)
  61. {
  62. // return printk_init(buf);
  63. uart_send_str(COM1, "textui_install_handler");
  64. return 0;
  65. }
  66. int textui_uninstall_handler(void *args)
  67. {
  68. return 0;
  69. }
  70. int textui_enable_handler(void *args)
  71. {
  72. uart_send_str(COM1, "textui_enable_handler");
  73. return 0;
  74. }
  75. int textui_disable_handler(void *args)
  76. {
  77. return 0;
  78. }
  79. int textui_change_handler(struct scm_buffer_info_t *buf)
  80. {
  81. memcpy((void *)buf->vaddr, (void *)(textui_framework.buf->vaddr), textui_framework.buf->size);
  82. textui_framework.buf = buf;
  83. return 0;
  84. }
  85. struct scm_ui_framework_operations_t textui_ops =
  86. {
  87. .install = &textui_install_handler,
  88. .uninstall = &textui_uninstall_handler,
  89. .change = &textui_change_handler,
  90. .enable = &textui_enable_handler,
  91. .disable = &textui_disable_handler,
  92. };
  93. /**
  94. * @brief 获取textui的帧缓冲区能容纳的内容的行数
  95. *
  96. * @return uint16_t
  97. */
  98. uint16_t __textui_get_actual_lines()
  99. {
  100. return __private_info.actual_line;
  101. }
  102. /**
  103. * @brief 获取当前渲染的窗口的id
  104. *
  105. * @return uint16_t
  106. */
  107. uint32_t __textui_get_current_window_id()
  108. {
  109. return __private_info.current_window->id;
  110. }
  111. /**
  112. * @brief 插入换行
  113. *
  114. * @param window 窗口结构体
  115. * @param vline_id 虚拟行号
  116. * @return int
  117. */
  118. static int __textui_new_line(struct textui_window_t *window, uint16_t vline_id)
  119. {
  120. // todo: 支持在两个虚拟行之间插入一个新行
  121. ++window->vline_operating;
  122. if (unlikely(window->vline_operating == window->vlines_num))
  123. window->vline_operating = 0;
  124. struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
  125. memset(vline->chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  126. vline->index = 0;
  127. if (likely(window->vlines_used == window->vlines_num)) // 需要滚动屏幕
  128. {
  129. ++window->top_vline;
  130. if (unlikely(window->top_vline >= window->vlines_num))
  131. window->top_vline = 0;
  132. // 刷新所有行
  133. textui_refresh_vlines(window, window->top_vline, window->vlines_num);
  134. }
  135. else
  136. ++window->vlines_used;
  137. return 0;
  138. }
  139. /**
  140. * @brief 真正向屏幕上输出字符的函数
  141. *
  142. * @param window
  143. * @param character
  144. * @return int
  145. */
  146. static int __textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  147. {
  148. if (textui_is_chromatic(window->flags)) // 启用彩色字符
  149. {
  150. struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
  151. vline->chars[vline->index].c = character;
  152. vline->chars[vline->index].FRcolor = FRcolor & 0xffffff;
  153. vline->chars[vline->index].BKcolor = BKcolor & 0xffffff;
  154. ++vline->index;
  155. textui_refresh_characters(window, window->vline_operating, vline->index - 1, 1);
  156. // 换行
  157. if (vline->index >= window->chars_per_line)
  158. {
  159. __textui_new_line(window, window->vline_operating);
  160. }
  161. }
  162. else
  163. {
  164. // todo: 支持纯文本字符
  165. while (1)
  166. pause();
  167. }
  168. return 0;
  169. }
  170. /**
  171. * @brief 在指定窗口上输出一个字符
  172. *
  173. * @param window 窗口
  174. * @param character 字符
  175. * @param FRcolor 前景色(RGB)
  176. * @param BKcolor 背景色(RGB)
  177. * @return int
  178. */
  179. int textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  180. {
  181. if (unlikely(character == '\0'))
  182. return 0;
  183. if (!textui_is_chromatic(window->flags)) // 暂不支持纯文本窗口
  184. return 0;
  185. uint64_t rflags = 0; // 加锁后rflags存储到这里
  186. spin_lock_irqsave(&window->lock, rflags);
  187. uart_send(COM1, character);
  188. if (unlikely(character == '\n'))
  189. {
  190. __textui_new_line(window, window->vline_operating);
  191. spin_unlock_irqrestore(&window->lock, rflags);
  192. return 0;
  193. }
  194. else if (character == '\t') // 输出制表符
  195. {
  196. int space_to_print = 8 - window->vlines.chromatic[window->vline_operating].index % 8;
  197. while (space_to_print--)
  198. {
  199. __textui_putchar_window(window, ' ', FRcolor, BKcolor);
  200. }
  201. }
  202. else if (character == '\b') // 退格
  203. {
  204. char bufff[128] = {0};
  205. --(window->vlines.chromatic[window->vline_operating].index);
  206. {
  207. uint16_t tmp = window->vlines.chromatic[window->vline_operating].index;
  208. if (tmp >= 0)
  209. {
  210. window->vlines.chromatic[window->vline_operating].chars[tmp].c = ' ';
  211. textui_refresh_characters(window, window->vline_operating, tmp, 1);
  212. }
  213. }
  214. // 需要向上缩一行
  215. if (window->vlines.chromatic[window->vline_operating].index <= 0)
  216. {
  217. window->vlines.chromatic[window->vline_operating].index = 0;
  218. memset(window->vlines.chromatic[window->vline_operating].chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  219. --(window->vline_operating);
  220. if (unlikely(window->vline_operating < 0))
  221. window->vline_operating = window->vlines_num - 1;
  222. // 考虑是否向上滚动
  223. if (likely(window->vlines_used > __private_info.actual_line))
  224. {
  225. --window->top_vline;
  226. if (unlikely(window->top_vline < 0))
  227. window->top_vline = window->vlines_num - 1;
  228. }
  229. --window->vlines_used;
  230. textui_refresh_vlines(window, window->top_vline, __private_info.actual_line);
  231. }
  232. }
  233. else
  234. {
  235. if (window->vlines.chromatic[window->vline_operating].index == window->chars_per_line)
  236. __textui_new_line(window, window->vline_operating);
  237. __textui_putchar_window(window, character, FRcolor, BKcolor);
  238. }
  239. spin_unlock_irqrestore(&window->lock, rflags);
  240. return 0;
  241. }
  242. /**
  243. * @brief 在默认窗口上输出一个字符
  244. *
  245. * @param character 字符
  246. * @param FRcolor 前景色(RGB)
  247. * @param BKcolor 背景色(RGB)
  248. * @return int
  249. */
  250. int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  251. {
  252. return textui_putchar_window(__private_info.default_window, character, FRcolor, BKcolor);
  253. }
  254. /**
  255. * @brief 初始化text ui框架
  256. *
  257. * @return int
  258. */
  259. int textui_init()
  260. {
  261. spin_init(&__window_id_lock);
  262. __window_max_id = 0;
  263. list_init(&__windows_list);
  264. memset(&textui_framework, 0, sizeof(struct scm_ui_framework_t));
  265. memset(&__private_info, 0, sizeof(struct textui_private_info_t));
  266. io_mfence();
  267. char name[] = "textUI";
  268. strcpy(textui_framework.name, name);
  269. textui_framework.ui_ops = &textui_ops;
  270. textui_framework.type = 0;
  271. // 注册框架到屏幕管理器
  272. int retval = scm_register(&textui_framework);
  273. if (retval != 0)
  274. {
  275. uart_send_str(COM1, "text ui init failed\n");
  276. while (1)
  277. pause();
  278. }
  279. uint16_t chars_per_vline = textui_framework.buf->width / TEXTUI_CHAR_WIDTH;
  280. uint16_t total_vlines = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  281. int cnt = chars_per_vline * total_vlines;
  282. struct textui_vline_chromatic_t *vl_ptr = __initial_vlines;
  283. struct textui_char_chromatic_t *ch_ptr = __initial_chars;
  284. // 初始化虚拟行
  285. for (int i = 0; i < total_vlines; ++i)
  286. {
  287. __textui_init_vline((vl_ptr + i), (ch_ptr + i * chars_per_vline));
  288. }
  289. // 初始化窗口
  290. __textui_init_window((&__initial_window), TEXTUI_WF_CHROMATIC, total_vlines, __initial_vlines, chars_per_vline);
  291. __private_info.current_window = &__initial_window;
  292. __private_info.default_window = &__initial_window;
  293. __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  294. uart_send_str(COM1, "text ui initialized\n");
  295. return 0;
  296. }