textui.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <common/errno.h>
  8. struct scm_ui_framework_t textui_framework;
  9. static spinlock_t __window_id_lock = {1};
  10. static uint32_t __window_max_id = 0;
  11. // 暂时初始化16080个初始字符对象以及67个虚拟行对象
  12. #define INITIAL_CHARS 16080
  13. #define INITIAL_VLINES (int)(1080 / 16)
  14. static struct textui_char_chromatic_t __initial_chars[INITIAL_CHARS] = {0};
  15. static struct textui_vline_chromatic_t __initial_vlines[INITIAL_VLINES] = {0};
  16. static struct textui_window_t __initial_window = {0}; // 初始窗口
  17. static struct textui_private_info_t __private_info = {0};
  18. static struct List __windows_list;
  19. /**
  20. * @brief 初始化window对象
  21. *
  22. * @param window 窗口对象
  23. * @param flags 标志位
  24. * @param vlines_num 虚拟行的总数
  25. * @param vlines_ptr 虚拟行数组指针
  26. * @param cperline 每行最大的字符数
  27. */
  28. static int __textui_init_window(struct textui_window_t *window, uint8_t flags, uint16_t vlines_num, void *vlines_ptr, uint16_t cperline)
  29. {
  30. memset((window), 0, sizeof(struct textui_window_t));
  31. list_init(&(window)->list);
  32. window->lock.lock = 1;
  33. spin_lock(&__window_id_lock);
  34. window->id = __window_max_id++;
  35. spin_unlock(&__window_id_lock);
  36. window->flags = flags;
  37. window->vlines_num = vlines_num;
  38. window->vlines_used = 1;
  39. window->top_vline = 0;
  40. window->vline_operating = 0;
  41. window->chars_per_line = cperline;
  42. if (textui_is_chromatic(flags))
  43. window->vlines.chromatic = vlines_ptr;
  44. else
  45. window->vlines.normal = vlines_ptr;
  46. list_add(&__windows_list, &(window)->list);
  47. }
  48. /**
  49. * @brief 初始化虚拟行对象
  50. *
  51. * @param vline 虚拟行对象指针
  52. * @param chars_ptr 字符对象数组指针
  53. */
  54. #define __textui_init_vline(vline, chars_ptr) \
  55. do \
  56. { \
  57. memset(vline, 0, sizeof(struct textui_vline_chromatic_t)); \
  58. (vline)->index = 0; \
  59. (vline)->chars = chars_ptr; \
  60. } while (0)
  61. int textui_install_handler(struct scm_buffer_info_t *buf)
  62. {
  63. // return printk_init(buf);
  64. uart_send_str(COM1, "textui_install_handler");
  65. return 0;
  66. }
  67. int textui_uninstall_handler(void *args)
  68. {
  69. return 0;
  70. }
  71. int textui_enable_handler(void *args)
  72. {
  73. uart_send_str(COM1, "textui_enable_handler");
  74. return 0;
  75. }
  76. int textui_disable_handler(void *args)
  77. {
  78. return 0;
  79. }
  80. int textui_change_handler(struct scm_buffer_info_t *buf)
  81. {
  82. memcpy((void *)buf->vaddr, (void *)(textui_framework.buf->vaddr), textui_framework.buf->size);
  83. textui_framework.buf = buf;
  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. ++window->top_vline;
  131. if (unlikely(window->top_vline >= window->vlines_num))
  132. window->top_vline = 0;
  133. // 刷新所有行
  134. textui_refresh_vlines(window, window->top_vline, window->vlines_num);
  135. }
  136. else
  137. ++window->vlines_used;
  138. return 0;
  139. }
  140. /**
  141. * @brief 真正向屏幕上输出字符的函数
  142. *
  143. * @param window
  144. * @param character
  145. * @return int
  146. */
  147. static int __textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  148. {
  149. if (textui_is_chromatic(window->flags)) // 启用彩色字符
  150. {
  151. struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
  152. vline->chars[vline->index].c = character;
  153. vline->chars[vline->index].FRcolor = FRcolor & 0xffffff;
  154. vline->chars[vline->index].BKcolor = BKcolor & 0xffffff;
  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. * @param FRcolor 前景色(RGB)
  177. * @param BKcolor 背景色(RGB)
  178. * @return int
  179. */
  180. int textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  181. {
  182. if (unlikely(character == '\0'))
  183. return 0;
  184. if (!textui_is_chromatic(window->flags)) // 暂不支持纯文本窗口
  185. return 0;
  186. // uint64_t rflags = 0; // 加锁后rflags存储到这里
  187. spin_lock(&window->lock);
  188. uart_send(COM1, character);
  189. if (unlikely(character == '\n'))
  190. {
  191. // 换行时还需要输出\r
  192. uart_send(COM1, '\r');
  193. __textui_new_line(window, window->vline_operating);
  194. // spin_unlock_irqrestore(&window->lock, rflags);
  195. spin_unlock(&window->lock);
  196. return 0;
  197. }
  198. else if (character == '\t') // 输出制表符
  199. {
  200. int space_to_print = 8 - window->vlines.chromatic[window->vline_operating].index % 8;
  201. while (space_to_print--)
  202. {
  203. __textui_putchar_window(window, ' ', FRcolor, BKcolor);
  204. }
  205. }
  206. else if (character == '\b') // 退格
  207. {
  208. char bufff[128] = {0};
  209. --(window->vlines.chromatic[window->vline_operating].index);
  210. {
  211. uint16_t tmp = window->vlines.chromatic[window->vline_operating].index;
  212. if (tmp >= 0)
  213. {
  214. window->vlines.chromatic[window->vline_operating].chars[tmp].c = ' ';
  215. window->vlines.chromatic[window->vline_operating].chars[tmp].BKcolor = BKcolor & 0xffffff;
  216. textui_refresh_characters(window, window->vline_operating, tmp, 1);
  217. }
  218. }
  219. // 需要向上缩一行
  220. if (window->vlines.chromatic[window->vline_operating].index <= 0)
  221. {
  222. window->vlines.chromatic[window->vline_operating].index = 0;
  223. memset(window->vlines.chromatic[window->vline_operating].chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  224. --(window->vline_operating);
  225. if (unlikely(window->vline_operating < 0))
  226. window->vline_operating = window->vlines_num - 1;
  227. // 考虑是否向上滚动
  228. if (likely(window->vlines_used > __private_info.actual_line))
  229. {
  230. --window->top_vline;
  231. if (unlikely(window->top_vline < 0))
  232. window->top_vline = window->vlines_num - 1;
  233. }
  234. --window->vlines_used;
  235. textui_refresh_vlines(window, window->top_vline, __private_info.actual_line);
  236. }
  237. }
  238. else
  239. {
  240. if (window->vlines.chromatic[window->vline_operating].index == window->chars_per_line)
  241. __textui_new_line(window, window->vline_operating);
  242. __textui_putchar_window(window, character, FRcolor, BKcolor);
  243. }
  244. // spin_unlock_irqrestore(&window->lock, rflags);
  245. spin_unlock(&window->lock);
  246. return 0;
  247. }
  248. /**
  249. * @brief 在默认窗口上输出一个字符
  250. *
  251. * @param character 字符
  252. * @param FRcolor 前景色(RGB)
  253. * @param BKcolor 背景色(RGB)
  254. * @return int
  255. */
  256. int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  257. {
  258. return textui_putchar_window(__private_info.default_window, character, FRcolor, BKcolor);
  259. }
  260. /**
  261. * @brief 初始化text ui框架
  262. *
  263. * @return int
  264. */
  265. int textui_init()
  266. {
  267. spin_init(&__window_id_lock);
  268. __window_max_id = 0;
  269. list_init(&__windows_list);
  270. memset(&textui_framework, 0, sizeof(struct scm_ui_framework_t));
  271. memset(&__private_info, 0, sizeof(struct textui_private_info_t));
  272. io_mfence();
  273. char name[] = "textUI";
  274. strcpy(textui_framework.name, name);
  275. textui_framework.ui_ops = &textui_ops;
  276. textui_framework.type = 0;
  277. // 注册框架到屏幕管理器
  278. int retval = scm_register(&textui_framework);
  279. if (retval != 0)
  280. {
  281. uart_send_str(COM1, "text ui init failed\n");
  282. while (1)
  283. pause();
  284. }
  285. uint16_t chars_per_vline = textui_framework.buf->width / TEXTUI_CHAR_WIDTH;
  286. uint16_t total_vlines = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  287. int cnt = chars_per_vline * total_vlines;
  288. struct textui_vline_chromatic_t *vl_ptr = __initial_vlines;
  289. struct textui_char_chromatic_t *ch_ptr = __initial_chars;
  290. // 初始化虚拟行
  291. for (int i = 0; i < total_vlines; ++i)
  292. {
  293. __textui_init_vline((vl_ptr + i), (ch_ptr + i * chars_per_vline));
  294. }
  295. // 初始化窗口
  296. __textui_init_window((&__initial_window), TEXTUI_WF_CHROMATIC, total_vlines, __initial_vlines, chars_per_vline);
  297. __private_info.current_window = &__initial_window;
  298. __private_info.default_window = &__initial_window;
  299. __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  300. uart_send_str(COM1, "text ui initialized\n");
  301. return 0;
  302. }