textui.c 11 KB

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