textui.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. set_pos_VBE_FB_addr((uint *)buf->vaddr);
  84. return 0;
  85. }
  86. struct scm_ui_framework_operations_t textui_ops =
  87. {
  88. .install = &textui_install_handler,
  89. .uninstall = &textui_uninstall_handler,
  90. .change = &textui_change_handler,
  91. .enable = &textui_enable_handler,
  92. .disable = &textui_disable_handler,
  93. };
  94. /**
  95. * @brief 获取textui的帧缓冲区能容纳的内容的行数
  96. *
  97. * @return uint16_t
  98. */
  99. uint16_t __textui_get_actual_lines()
  100. {
  101. return __private_info.actual_line;
  102. }
  103. /**
  104. * @brief 获取当前渲染的窗口的id
  105. *
  106. * @return uint16_t
  107. */
  108. uint32_t __textui_get_current_window_id()
  109. {
  110. return __private_info.current_window->id;
  111. }
  112. /**
  113. * @brief 插入换行
  114. *
  115. * @param window 窗口结构体
  116. * @param vline_id 虚拟行号
  117. * @return int
  118. */
  119. static int __textui_new_line(struct textui_window_t *window, uint16_t vline_id)
  120. {
  121. // todo: 支持在两个虚拟行之间插入一个新行
  122. ++window->vline_operating;
  123. if (unlikely(window->vline_operating == window->vlines_num))
  124. window->vline_operating = 0;
  125. struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
  126. memset(vline->chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  127. vline->index = 0;
  128. if (likely(window->vlines_used == window->vlines_num)) // 需要滚动屏幕
  129. {
  130. // uart_send_str(COM1, " scroll, top vline= ");
  131. ++window->top_vline;
  132. // uart_send(COM1, '0' + window->top_vline);
  133. if (unlikely(window->top_vline >= window->vlines_num))
  134. window->top_vline = 0;
  135. // int delta = ABS((int)window->vline_operating - (int)window->top_vline);
  136. // 刷新所有行
  137. textui_refresh_vlines(window, window->top_vline, window->vlines_num);
  138. }
  139. else
  140. ++window->vlines_used;
  141. return 0;
  142. }
  143. static int __textui_putchar_window(struct textui_window_t *window, uint16_t character)
  144. {
  145. if (textui_is_chromatic(window->flags)) // 启用彩色字符
  146. {
  147. struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
  148. vline->chars[vline->index].c = character;
  149. vline->chars[vline->index].Fr = 0xff;
  150. vline->chars[vline->index].Fg = 0xff;
  151. vline->chars[vline->index].Fb = 0xff;
  152. vline->chars[vline->index].Br = 0;
  153. vline->chars[vline->index].Bg = 0;
  154. vline->chars[vline->index].Bb = 0;
  155. ++vline->index;
  156. textui_refresh_characters(window, window->vline_operating, vline->index - 1, 1);
  157. // 换行
  158. if (vline->index >= window->chars_per_line)
  159. {
  160. __textui_new_line(window, window->vline_operating);
  161. }
  162. }
  163. else
  164. {
  165. // todo: 支持纯文本字符
  166. while (1)
  167. pause();
  168. }
  169. return 0;
  170. }
  171. /**
  172. * @brief 在指定窗口上输出一个字符
  173. *
  174. * @param window 窗口
  175. * @param character 字符
  176. * @return int
  177. */
  178. int textui_putchar_window(struct textui_window_t *window, uint16_t character)
  179. {
  180. if (unlikely(character == '\0'))
  181. return 0;
  182. if (!textui_is_chromatic(window->flags)) // 暂不支持纯文本窗口
  183. return 0;
  184. uint64_t rflags = 0; // 加锁后rflags存储到这里
  185. spin_lock_irqsave(&window->lock, rflags);
  186. uart_send(COM1, character);
  187. if (unlikely(character == '\n'))
  188. {
  189. __textui_new_line(window, window->vline_operating);
  190. spin_unlock_irqrestore(&window->lock, rflags);
  191. return 0;
  192. }
  193. else if (character == '\t') // 输出制表符
  194. {
  195. int space_to_print = 8 - window->vlines.chromatic[window->vline_operating].index % 8;
  196. while (space_to_print--)
  197. {
  198. __textui_putchar_window(window, ' ');
  199. }
  200. }
  201. else if (character == '\b') // 退格
  202. {
  203. --window->vlines.chromatic[window->vline_operating].index;
  204. {
  205. uint16_t tmp = window->vlines.chromatic[window->vline_operating].index;
  206. window->vlines.chromatic[window->vline_operating].chars[tmp].c = ' ';
  207. textui_refresh_characters(window, window->vline_operating, tmp, 1);
  208. }
  209. // 需要向上缩一行
  210. if (window->vlines.chromatic[window->vline_operating].index < 0)
  211. {
  212. window->vlines.chromatic[window->vline_operating].index = 0;
  213. memset(window->vlines.chromatic[window->vline_operating].chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  214. --window->vline_operating;
  215. if (unlikely(window->vline_operating < 0))
  216. window->vline_operating = window->vlines_num - 1;
  217. // 考虑是否向上滚动
  218. if (likely(window->vlines_used >= __private_info.actual_line))
  219. {
  220. --window->top_vline;
  221. if (unlikely(window->top_vline < 0))
  222. window->top_vline = window->vlines_num - 1;
  223. }
  224. --window->vlines_used;
  225. textui_refresh_vlines(window, window->top_vline, __private_info.actual_line);
  226. }
  227. }
  228. else
  229. __textui_putchar_window(window, character);
  230. spin_unlock_irqrestore(&window->lock, rflags);
  231. return 0;
  232. }
  233. /**
  234. * @brief 在默认窗口上输出一个字符
  235. *
  236. * @param character 字符
  237. * @return int
  238. */
  239. int textui_putchar(uint16_t character)
  240. {
  241. return textui_putchar_window(__private_info.default_window, character);
  242. }
  243. /**
  244. * @brief 初始化text ui框架
  245. *
  246. * @return int
  247. */
  248. int textui_init()
  249. {
  250. spin_init(&__window_id_lock);
  251. __window_max_id = 0;
  252. list_init(&__windows_list);
  253. memset(&textui_framework, 0, sizeof(struct scm_ui_framework_t));
  254. memset(&__private_info, 0, sizeof(struct textui_private_info_t));
  255. io_mfence();
  256. char name[] = "textUI";
  257. strcpy(textui_framework.name, name);
  258. textui_framework.ui_ops = &textui_ops;
  259. textui_framework.type = 0;
  260. // 注册框架到屏幕管理器
  261. int retval = scm_register(&textui_framework);
  262. if (retval != 0)
  263. {
  264. uart_send_str(COM1, "text ui init failed");
  265. while (1)
  266. pause();
  267. }
  268. uint16_t chars_per_vline = textui_framework.buf->width / TEXTUI_CHAR_WIDTH;
  269. uint16_t total_vlines = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  270. int cnt = chars_per_vline * total_vlines;
  271. struct textui_vline_chromatic_t *vl_ptr = __initial_vlines;
  272. struct textui_char_chromatic_t *ch_ptr = __initial_chars;
  273. // 初始化虚拟行
  274. for (int i = 0; i < total_vlines; ++i)
  275. {
  276. __textui_init_vline((vl_ptr + i), (ch_ptr + i * chars_per_vline));
  277. }
  278. // 初始化窗口
  279. __textui_init_window((&__initial_window), TEXTUI_WF_CHROMATIC, total_vlines, __initial_vlines, chars_per_vline);
  280. __private_info.current_window = &__initial_window;
  281. __private_info.default_window = &__initial_window;
  282. __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  283. uart_send_str(COM1, "text ui initialized");
  284. return 0;
  285. }