textui.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include <common/glib.h>
  3. #include <common/sys/types.h>
  4. #include <common/spinlock.h>
  5. /*
  6. textui中的几个对象的关系:
  7. textui_vline_normal_t
  8. +--------------------------------+
  9. | | textui_char_normal_t
  10. textui_window_t | chars: textui_char_normal_t * | +--------------------------+
  11. +----------------------------+ | | | |
  12. | | +------> +--------> c: char |
  13. | list:List | | | index: int16_t | +--------------------------+
  14. | vlines_num:int16_t | | | |
  15. | vlines_used:int16_t | | +--------------------------------+
  16. | | |
  17. | vlines +-----+ textui_char_chromatic_t
  18. | | | textui_vline_chromatic_t +--------------------------+
  19. | top_vline:int16_t | | +-------------------------------------+ | |
  20. | vline_operating:int16_t | | | | | c: uint16_t |
  21. | chars_per_line:int16_t | | | chars: textui_char_chromatic_t * | | |
  22. | flags:uint8_t | | | | | FRcolor:24 |
  23. | lock:spinlock_t | +------> +---> |
  24. | | | index: int16_t | | BKcolor:24 |
  25. | | | | | |
  26. +----------------------------+ +-------------------------------------+ +--------------------------+
  27. */
  28. // 文本窗口标志位
  29. // 文本窗口是否为彩色
  30. #define TEXTUI_WF_CHROMATIC (1 << 0)
  31. // 窗口是否启用彩色字符
  32. #define textui_is_chromatic(flag) ((flag)&TEXTUI_WF_CHROMATIC)
  33. // 每个字符的宽度和高度(像素)
  34. #define TEXTUI_CHAR_WIDTH 8
  35. #define TEXTUI_CHAR_HEIGHT 16
  36. /**
  37. * @brief 黑白字符对象
  38. *
  39. */
  40. struct textui_char_normal_t
  41. {
  42. char c;
  43. };
  44. /**
  45. * @brief 彩色字符对象
  46. *
  47. */
  48. struct textui_char_chromatic_t
  49. {
  50. unsigned c : 16;
  51. // 前景色
  52. unsigned FRcolor : 24; // rgb
  53. // 背景色
  54. unsigned BKcolor : 24; // rgb
  55. };
  56. // 注意!!! 请保持vline结构体的大小、成员变量命名相等!
  57. /**
  58. * @brief 单色显示的虚拟行结构体
  59. *
  60. */
  61. struct textui_vline_normal_t
  62. {
  63. struct textui_char_normal_t *chars; // 字符对象数组
  64. int16_t index; // 当前操作的位置
  65. };
  66. /**
  67. * @brief 彩色显示的虚拟行结构体
  68. *
  69. */
  70. struct textui_vline_chromatic_t
  71. {
  72. struct textui_char_chromatic_t *chars;
  73. int16_t index; // 当前操作的位置
  74. };
  75. /**
  76. * @brief textu ui 框架的文本窗口结构体
  77. *
  78. */
  79. struct textui_window_t
  80. {
  81. struct List list;
  82. uint32_t id; // 窗口id
  83. int16_t vlines_num; // 虚拟行总数
  84. int16_t vlines_used; // 当前已经使用了的虚拟行总数
  85. // 指向虚拟行的数组的指针(二选一)
  86. union
  87. {
  88. struct textui_vline_normal_t *normal;
  89. struct textui_vline_chromatic_t *chromatic;
  90. } vlines;
  91. int16_t top_vline; // 位于最顶上的那一个虚拟行的行号
  92. int16_t vline_operating; // 正在操作的vline
  93. int16_t chars_per_line; // 每行最大容纳的字符数
  94. uint8_t flags; // 窗口flag
  95. spinlock_t lock; // 窗口操作锁
  96. };
  97. struct textui_private_info_t
  98. {
  99. int16_t actual_line; // 真实行的数量
  100. struct textui_window_t *current_window; // 当前的主窗口
  101. struct textui_window_t *default_window; // 默认print到的窗口
  102. };
  103. /**
  104. * @brief 重新渲染整个虚拟行
  105. *
  106. * @param window 窗口结构体
  107. * @param vline_id 虚拟行号
  108. * @return int 错误码
  109. */
  110. int textui_refresh_vline(struct textui_window_t *window, uint16_t vline_id);
  111. int textui_refresh_vlines(struct textui_window_t *window, uint16_t start, uint16_t count);
  112. /**
  113. * @brief 刷新某个虚拟行的连续n个字符对象
  114. *
  115. * @param window 窗口结构体
  116. * @param vline_id 虚拟行号
  117. * @param start 起始字符号
  118. * @param count 要刷新的字符数量
  119. * @return int 错误码
  120. */
  121. int textui_refresh_characters(struct textui_window_t *window, uint16_t vline_id, uint16_t start, uint16_t count);
  122. /**
  123. * @brief 在指定窗口上输出一个字符
  124. *
  125. * @param window 窗口
  126. * @param character 字符
  127. * @param FRcolor 前景色(RGB)
  128. * @param BKcolor 背景色(RGB)
  129. * @return int
  130. */
  131. int textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor);
  132. /**
  133. * @brief 在默认窗口上输出一个字符
  134. *
  135. * @param character 字符
  136. * @param FRcolor 前景色(RGB)
  137. * @param BKcolor 背景色(RGB)
  138. * @return int
  139. */
  140. int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor);
  141. /**
  142. * @brief 获取textui的帧缓冲区能容纳的内容的行数
  143. *
  144. * @return uint16_t
  145. */
  146. uint16_t __textui_get_actual_lines();
  147. /**
  148. * @brief 获取当前渲染的窗口的id
  149. *
  150. * @return uint16_t
  151. */
  152. uint32_t __textui_get_current_window_id();
  153. /**
  154. * @brief 初始化text ui框架
  155. *
  156. * @return int
  157. */
  158. int textui_init();