textui.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_irqsave(&window->lock, rflags);
  188. uart_send(COM1, character);
  189. if (unlikely(character == '\n'))
  190. {
  191. __textui_new_line(window, window->vline_operating);
  192. spin_unlock_irqrestore(&window->lock, rflags);
  193. return 0;
  194. }
  195. else if (character == '\t') // 输出制表符
  196. {
  197. int space_to_print = 8 - window->vlines.chromatic[window->vline_operating].index % 8;
  198. while (space_to_print--)
  199. {
  200. __textui_putchar_window(window, ' ', FRcolor, BKcolor);
  201. }
  202. }
  203. else if (character == '\b') // 退格
  204. {
  205. char bufff[128] = {0};
  206. --(window->vlines.chromatic[window->vline_operating].index);
  207. {
  208. uint16_t tmp = window->vlines.chromatic[window->vline_operating].index;
  209. if (tmp >= 0)
  210. {
  211. window->vlines.chromatic[window->vline_operating].chars[tmp].c = ' ';
  212. textui_refresh_characters(window, window->vline_operating, tmp, 1);
  213. }
  214. }
  215. // 需要向上缩一行
  216. if (window->vlines.chromatic[window->vline_operating].index <= 0)
  217. {
  218. window->vlines.chromatic[window->vline_operating].index = 0;
  219. memset(window->vlines.chromatic[window->vline_operating].chars, 0, sizeof(struct textui_char_chromatic_t) * window->chars_per_line);
  220. --(window->vline_operating);
  221. if (unlikely(window->vline_operating < 0))
  222. window->vline_operating = window->vlines_num - 1;
  223. // 考虑是否向上滚动
  224. if (likely(window->vlines_used > __private_info.actual_line))
  225. {
  226. --window->top_vline;
  227. if (unlikely(window->top_vline < 0))
  228. window->top_vline = window->vlines_num - 1;
  229. }
  230. --window->vlines_used;
  231. textui_refresh_vlines(window, window->top_vline, __private_info.actual_line);
  232. }
  233. }
  234. else
  235. {
  236. if (window->vlines.chromatic[window->vline_operating].index == window->chars_per_line)
  237. __textui_new_line(window, window->vline_operating);
  238. __textui_putchar_window(window, character, FRcolor, BKcolor);
  239. }
  240. spin_unlock_irqrestore(&window->lock, rflags);
  241. return 0;
  242. }
  243. /**
  244. * @brief 在默认窗口上输出一个字符
  245. *
  246. * @param character 字符
  247. * @param FRcolor 前景色(RGB)
  248. * @param BKcolor 背景色(RGB)
  249. * @return int
  250. */
  251. int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
  252. {
  253. return textui_putchar_window(__private_info.default_window, character, FRcolor, BKcolor);
  254. }
  255. /**
  256. * @brief 初始化text ui框架
  257. *
  258. * @return int
  259. */
  260. int textui_init()
  261. {
  262. spin_init(&__window_id_lock);
  263. __window_max_id = 0;
  264. list_init(&__windows_list);
  265. memset(&textui_framework, 0, sizeof(struct scm_ui_framework_t));
  266. memset(&__private_info, 0, sizeof(struct textui_private_info_t));
  267. io_mfence();
  268. char name[] = "textUI";
  269. strcpy(textui_framework.name, name);
  270. textui_framework.ui_ops = &textui_ops;
  271. textui_framework.type = 0;
  272. // 注册框架到屏幕管理器
  273. int retval = scm_register(&textui_framework);
  274. if (retval != 0)
  275. {
  276. uart_send_str(COM1, "text ui init failed\n");
  277. while (1)
  278. pause();
  279. }
  280. uint16_t chars_per_vline = textui_framework.buf->width / TEXTUI_CHAR_WIDTH;
  281. uint16_t total_vlines = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  282. int cnt = chars_per_vline * total_vlines;
  283. struct textui_vline_chromatic_t *vl_ptr = __initial_vlines;
  284. struct textui_char_chromatic_t *ch_ptr = __initial_chars;
  285. // 初始化虚拟行
  286. for (int i = 0; i < total_vlines; ++i)
  287. {
  288. __textui_init_vline((vl_ptr + i), (ch_ptr + i * chars_per_vline));
  289. }
  290. // 初始化窗口
  291. __textui_init_window((&__initial_window), TEXTUI_WF_CHROMATIC, total_vlines, __initial_vlines, chars_per_vline);
  292. __private_info.current_window = &__initial_window;
  293. __private_info.default_window = &__initial_window;
  294. __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
  295. uart_send_str(COM1, "text ui initialized\n");
  296. return 0;
  297. }