printk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. //
  2. // Created by longjin on 2022/1/22.
  3. //
  4. #include "printk.h"
  5. #include "kprint.h"
  6. #include <driver/multiboot2/multiboot2.h>
  7. #include <mm/mm.h>
  8. #include <common/spinlock.h>
  9. #include <lib/libUI/textui.h>
  10. #include <lib/libUI/screen_manager.h>
  11. #include <driver/uart/uart.h>
  12. #include <driver/video/video.h>
  13. #include "math.h"
  14. #include <common/string.h>
  15. #include <lib/libUI/textui.h>
  16. struct printk_screen_info pos;
  17. static spinlock_t printk_lock={1};
  18. static bool sw_show_scroll_animation = false; // 显示换行动画的开关
  19. /**
  20. * @brief Set the printk pos object
  21. *
  22. * @param x 列坐标
  23. * @param y 行坐标
  24. */
  25. static int set_printk_pos(const int x, const int y);
  26. /**
  27. * @brief 在屏幕上指定位置打印字符
  28. *
  29. * @param fb 帧缓存线性地址
  30. * @param Xsize 行分辨率
  31. * @param x 左上角列像素点位置
  32. * @param y 左上角行像素点位置
  33. * @param FRcolor 字体颜色
  34. * @param BKcolor 背景颜色
  35. * @param font 字符的bitmap
  36. */
  37. static void putchar(uint *fb, int Xsize, int x, int y, unsigned int FRcolor, unsigned int BKcolor, unsigned char font);
  38. static uint *get_pos_VBE_FB_addr();
  39. /**
  40. * @brief 清屏
  41. *
  42. */
  43. static int cls();
  44. #pragma GCC push_options
  45. #pragma GCC optimize("O0")
  46. /**
  47. * @brief 滚动窗口(尚不支持向下滚动)
  48. *
  49. * @param direction 方向,向上滑动为true,否则为false
  50. * @param pixels 要滑动的像素数量
  51. * @param animation 是否包含滑动动画
  52. */
  53. static int scroll(bool direction, int pixels, bool animation);
  54. #pragma GCC pop_options
  55. /**
  56. * @brief 将数字按照指定的要求转换成对应的字符串(2~36进制)
  57. *
  58. * @param str 要返回的字符串
  59. * @param num 要打印的数值
  60. * @param base 基数
  61. * @param field_width 区域宽度
  62. * @param precision 精度
  63. * @param flags 标志位
  64. */
  65. static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags);
  66. static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
  67. static int calculate_max_charNum(int len, int size)
  68. {
  69. /**
  70. * @brief 计算屏幕上能有多少行
  71. * @param len 屏幕长/宽
  72. * @param size 字符长/宽
  73. */
  74. return len / size - 1;
  75. }
  76. int printk_init(struct scm_buffer_info_t *buf)
  77. {
  78. pos.width = buf->width;
  79. pos.height = buf->height;
  80. pos.char_size_x = 8;
  81. pos.char_size_y = 16;
  82. pos.max_x = calculate_max_charNum(pos.width, pos.char_size_x);
  83. pos.max_y = calculate_max_charNum(pos.height, pos.char_size_y);
  84. pos.FB_address = buf->vaddr;
  85. pos.FB_length = 1UL * pos.width * pos.height;
  86. // 初始化自旋锁
  87. spin_init(&printk_lock);
  88. pos.x = 0;
  89. pos.y = 0;
  90. cls();
  91. io_mfence();
  92. kdebug("width=%d\theight=%d", pos.width, pos.height);
  93. // 由于此时系统并未启用双缓冲,因此关闭滚动动画
  94. printk_disable_animation();
  95. io_mfence();
  96. return 0;
  97. }
  98. static int set_printk_pos(const int x, const int y)
  99. {
  100. // 指定的坐标不在屏幕范围内
  101. if (!((x >= 0 && x <= pos.max_x) && (y >= 0 && y <= pos.max_y)))
  102. return EPOS_OVERFLOW;
  103. pos.x = x;
  104. pos.y = y;
  105. return 0;
  106. }
  107. static int skip_and_atoi(const char **s)
  108. {
  109. /**
  110. * @brief 获取连续的一段字符对应整数的值
  111. * @param:**s 指向 指向字符串的指针 的指针
  112. */
  113. int ans = 0;
  114. while (is_digit(**s))
  115. {
  116. ans = ans * 10 + (**s) - '0';
  117. ++(*s);
  118. }
  119. return ans;
  120. }
  121. static void auto_newline()
  122. {
  123. /**
  124. * @brief 超过每行最大字符数,自动换行
  125. *
  126. */
  127. if (pos.x > pos.max_x)
  128. {
  129. #ifdef DEBUG
  130. uart_send(COM1, '\r');
  131. uart_send(COM1, '\n');
  132. #endif
  133. pos.x = 0;
  134. ++pos.y;
  135. }
  136. if (pos.y > pos.max_y)
  137. {
  138. #ifdef DEBUG
  139. uart_send(COM1, '\r');
  140. uart_send(COM1, '\n');
  141. #endif
  142. pos.y = pos.max_y;
  143. int lines_to_scroll = 1;
  144. barrier();
  145. scroll(true, lines_to_scroll * pos.char_size_y, sw_show_scroll_animation);
  146. barrier();
  147. pos.y -= (lines_to_scroll - 1);
  148. }
  149. }
  150. int vsprintf(char *buf, const char *fmt, va_list args)
  151. {
  152. /**
  153. * 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中
  154. * @param buf 结果缓冲区
  155. * @param fmt 格式化字符串
  156. * @param args 内容
  157. * @return 最终字符串的长度
  158. */
  159. char *str, *s;
  160. str = buf;
  161. int flags; // 用来存储格式信息的bitmap
  162. int field_width; //区域宽度
  163. int precision; //精度
  164. int qualifier; //数据显示的类型
  165. int len;
  166. //开始解析字符串
  167. for (; *fmt; ++fmt)
  168. {
  169. //内容不涉及到格式化,直接输出
  170. if (*fmt != '%')
  171. {
  172. *str = *fmt;
  173. ++str;
  174. continue;
  175. }
  176. //开始格式化字符串
  177. //清空标志位和field宽度
  178. field_width = flags = 0;
  179. bool flag_tmp = true;
  180. bool flag_break = false;
  181. ++fmt;
  182. while (flag_tmp)
  183. {
  184. switch (*fmt)
  185. {
  186. case '\0':
  187. //结束解析
  188. flag_break = true;
  189. flag_tmp = false;
  190. break;
  191. case '-':
  192. // 左对齐
  193. flags |= LEFT;
  194. ++fmt;
  195. break;
  196. case '+':
  197. //在正数前面显示加号
  198. flags |= PLUS;
  199. ++fmt;
  200. break;
  201. case ' ':
  202. flags |= SPACE;
  203. ++fmt;
  204. break;
  205. case '#':
  206. //在八进制数前面显示 '0o',在十六进制数前面显示 '0x' 或 '0X'
  207. flags |= SPECIAL;
  208. ++fmt;
  209. break;
  210. case '0':
  211. //显示的数字之前填充‘0’来取代空格
  212. flags |= PAD_ZERO;
  213. ++fmt;
  214. break;
  215. default:
  216. flag_tmp = false;
  217. break;
  218. }
  219. }
  220. if (flag_break)
  221. break;
  222. //获取区域宽度
  223. field_width = -1;
  224. if (*fmt == '*')
  225. {
  226. field_width = va_arg(args, int);
  227. ++fmt;
  228. }
  229. else if (is_digit(*fmt))
  230. {
  231. field_width = skip_and_atoi(&fmt);
  232. if (field_width < 0)
  233. {
  234. field_width = -field_width;
  235. flags |= LEFT;
  236. }
  237. }
  238. //获取小数精度
  239. precision = -1;
  240. if (*fmt == '.')
  241. {
  242. ++fmt;
  243. if (*fmt == '*')
  244. {
  245. precision = va_arg(args, int);
  246. ++fmt;
  247. }
  248. else if is_digit (*fmt)
  249. {
  250. precision = skip_and_atoi(&fmt);
  251. }
  252. }
  253. //获取要显示的数据的类型
  254. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z')
  255. {
  256. qualifier = *fmt;
  257. ++fmt;
  258. }
  259. //为了支持lld
  260. if (qualifier == 'l' && *fmt == 'l', *(fmt + 1) == 'd')
  261. ++fmt;
  262. //转化成字符串
  263. long long *ip;
  264. switch (*fmt)
  265. {
  266. //输出 %
  267. case '%':
  268. *str++ = '%';
  269. break;
  270. // 显示一个字符
  271. case 'c':
  272. //靠右对齐
  273. if (!(flags & LEFT))
  274. {
  275. while (--field_width > 0)
  276. {
  277. *str = ' ';
  278. ++str;
  279. }
  280. }
  281. *str++ = (unsigned char)va_arg(args, int);
  282. while (--field_width > 0)
  283. {
  284. *str = ' ';
  285. ++str;
  286. }
  287. break;
  288. //显示一个字符串
  289. case 's':
  290. s = va_arg(args, char *);
  291. if (!s)
  292. s = '\0';
  293. len = strlen(s);
  294. if (precision < 0)
  295. {
  296. //未指定精度
  297. precision = len;
  298. }
  299. else if (len > precision)
  300. {
  301. len = precision;
  302. }
  303. //靠右对齐
  304. if (!(flags & LEFT))
  305. while (len < field_width--)
  306. {
  307. *str = ' ';
  308. ++str;
  309. }
  310. for (int i = 0; i < len; i++)
  311. {
  312. *str = *s;
  313. ++s;
  314. ++str;
  315. }
  316. while (len < field_width--)
  317. {
  318. *str = ' ';
  319. ++str;
  320. }
  321. break;
  322. //以八进制显示字符串
  323. case 'o':
  324. flags |= SMALL;
  325. case 'O':
  326. flags |= SPECIAL;
  327. if (qualifier == 'l')
  328. str = write_num(str, va_arg(args, long long), 8, field_width, precision, flags);
  329. else
  330. str = write_num(str, va_arg(args, int), 8, field_width, precision, flags);
  331. break;
  332. //打印指针指向的地址
  333. case 'p':
  334. if (field_width == 0)
  335. {
  336. field_width = 2 * sizeof(void *);
  337. flags |= PAD_ZERO;
  338. }
  339. str = write_num(str, (unsigned long)va_arg(args, void *), 16, field_width, precision, flags);
  340. break;
  341. //打印十六进制
  342. case 'x':
  343. flags |= SMALL;
  344. case 'X':
  345. // flags |= SPECIAL;
  346. if (qualifier == 'l')
  347. str = write_num(str, va_arg(args, ll), 16, field_width, precision, flags);
  348. else
  349. str = write_num(str, va_arg(args, int), 16, field_width, precision, flags);
  350. break;
  351. //打印十进制有符号整数
  352. case 'i':
  353. case 'd':
  354. flags |= SIGN;
  355. if (qualifier == 'l')
  356. str = write_num(str, va_arg(args, long long), 10, field_width, precision, flags);
  357. else
  358. str = write_num(str, va_arg(args, int), 10, field_width, precision, flags);
  359. break;
  360. //打印十进制无符号整数
  361. case 'u':
  362. if (qualifier == 'l')
  363. str = write_num(str, va_arg(args, unsigned long long), 10, field_width, precision, flags);
  364. else
  365. str = write_num(str, va_arg(args, unsigned int), 10, field_width, precision, flags);
  366. break;
  367. //输出有效字符数量到*ip对应的变量
  368. case 'n':
  369. if (qualifier == 'l')
  370. ip = va_arg(args, long long *);
  371. else
  372. ip = (ll *)va_arg(args, int *);
  373. *ip = str - buf;
  374. break;
  375. case 'f':
  376. // 默认精度为3
  377. // printk("1111\n");
  378. // va_arg(args, double);
  379. // printk("222\n");
  380. if (precision < 0)
  381. precision = 3;
  382. str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags);
  383. break;
  384. //对于不识别的控制符,直接输出
  385. default:
  386. *str++ = '%';
  387. if (*fmt)
  388. *str++ = *fmt;
  389. else
  390. --fmt;
  391. break;
  392. }
  393. }
  394. *str = '\0';
  395. //返回缓冲区已有字符串的长度。
  396. return str - buf;
  397. }
  398. static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags)
  399. {
  400. /**
  401. * @brief 将数字按照指定的要求转换成对应的字符串
  402. *
  403. * @param str 要返回的字符串
  404. * @param num 要打印的数值
  405. * @param base 基数
  406. * @param field_width 区域宽度
  407. * @param precision 精度
  408. * @param flags 标志位
  409. */
  410. // 首先判断是否支持该进制
  411. if (base < 2 || base > 36)
  412. return 0;
  413. char pad, sign, tmp_num[100];
  414. const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  415. // 显示小写字母
  416. if (flags & SMALL)
  417. digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  418. if (flags & LEFT)
  419. flags &= ~PAD_ZERO;
  420. // 设置填充元素
  421. pad = (flags & PAD_ZERO) ? '0' : ' ';
  422. sign = 0;
  423. if (flags & SIGN)
  424. {
  425. int64_t signed_num = (int64_t)num;
  426. if (signed_num < 0)
  427. {
  428. sign = '-';
  429. num = -signed_num;
  430. }
  431. else
  432. num = signed_num;
  433. }
  434. else
  435. {
  436. // 设置符号
  437. sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0);
  438. }
  439. // sign占用了一个宽度
  440. if (sign)
  441. --field_width;
  442. if (flags & SPECIAL)
  443. if (base == 16) // 0x占用2个位置
  444. field_width -= 2;
  445. else if (base == 8) // O占用一个位置
  446. --field_width;
  447. int js_num = 0; // 临时数字字符串tmp_num的长度
  448. if (num == 0)
  449. tmp_num[js_num++] = '0';
  450. else
  451. {
  452. num = ABS(num);
  453. //进制转换
  454. while (num > 0)
  455. {
  456. tmp_num[js_num++] = digits[num % base]; // 注意这里,输出的数字,是小端对齐的。低位存低位
  457. num /= base;
  458. }
  459. }
  460. if (js_num > precision)
  461. precision = js_num;
  462. field_width -= precision;
  463. // 靠右对齐
  464. if (!(flags & (LEFT + PAD_ZERO)))
  465. while (field_width-- > 0)
  466. *str++ = ' ';
  467. if (sign)
  468. *str++ = sign;
  469. if (flags & SPECIAL)
  470. if (base == 16)
  471. {
  472. *str++ = '0';
  473. *str++ = digits[33];
  474. }
  475. else if (base == 8)
  476. *str++ = digits[24]; //注意这里是英文字母O或者o
  477. if (!(flags & LEFT))
  478. while (field_width-- > 0)
  479. *str++ = pad;
  480. while (js_num < precision)
  481. {
  482. --precision;
  483. *str++ = '0';
  484. }
  485. while (js_num-- > 0)
  486. *str++ = tmp_num[js_num];
  487. while (field_width-- > 0)
  488. *str++ = ' ';
  489. return str;
  490. }
  491. static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags)
  492. {
  493. /**
  494. * @brief 将浮点数按照指定的要求转换成对应的字符串
  495. *
  496. * @param str 要返回的字符串
  497. * @param num 要打印的数值
  498. * @param field_width 区域宽度
  499. * @param precision 精度
  500. * @param flags 标志位
  501. */
  502. char pad, sign, tmp_num_z[100], tmp_num_d[350];
  503. const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  504. // 显示小写字母
  505. if (flags & SMALL)
  506. digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  507. // 设置填充元素
  508. pad = (flags & PAD_ZERO) ? '0' : ' ';
  509. sign = 0;
  510. if (flags & SIGN && num < 0)
  511. {
  512. sign = '-';
  513. num = -num;
  514. }
  515. else
  516. {
  517. // 设置符号
  518. sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0);
  519. }
  520. // sign占用了一个宽度
  521. if (sign)
  522. --field_width;
  523. int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度
  524. uint64_t num_z = (uint64_t)(num); // 获取整数部分
  525. uint64_t num_decimal = (uint64_t)(round(1.0 * (num - num_z) * pow(10, precision))); // 获取小数部分
  526. if (num == 0 || num_z == 0)
  527. tmp_num_z[js_num_z++] = '0';
  528. else
  529. {
  530. //存储整数部分
  531. while (num_z > 0)
  532. {
  533. tmp_num_z[js_num_z++] = digits[num_z % 10]; // 注意这里,输出的数字,是小端对齐的。低位存低位
  534. num_z /= 10;
  535. }
  536. }
  537. while (num_decimal > 0)
  538. {
  539. tmp_num_d[js_num_d++] = digits[num_decimal % 10];
  540. num_decimal /= 10;
  541. }
  542. field_width -= (precision + 1 + js_num_z);
  543. // 靠右对齐
  544. if (!(flags & LEFT))
  545. while (field_width-- > 0)
  546. *str++ = pad;
  547. if (sign)
  548. *str++ = sign;
  549. // 输出整数部分
  550. // while (js_num_z-- > 0)
  551. // *str++ = tmp_num_z[js_num_z];
  552. while (js_num_z > 0)
  553. {
  554. *str++ = tmp_num_z[js_num_z - 1];
  555. --js_num_z;
  556. }
  557. *str++ = '.';
  558. // 输出小数部分
  559. int total_dec_count = js_num_d;
  560. for (int i = 0; i < precision && js_num_d-- > 0; ++i)
  561. *str++ = tmp_num_d[js_num_d];
  562. while (total_dec_count < precision)
  563. {
  564. ++total_dec_count;
  565. *str++ = '0';
  566. }
  567. while (field_width-- > 0)
  568. *str++ = ' ';
  569. return str;
  570. }
  571. /**
  572. * @brief 格式化打印字符串
  573. *
  574. * @param FRcolor 前景色
  575. * @param BKcolor 背景色
  576. * @param ... 格式化字符串
  577. */
  578. int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ...)
  579. {
  580. va_list args;
  581. va_start(args, fmt);
  582. char buf[4096]; // vsprintf()的缓冲区
  583. int len = vsprintf(buf, fmt, args);
  584. va_end(args);
  585. unsigned char current;
  586. int i; // 总共输出的字符数
  587. for (i = 0; i < len; ++i)
  588. {
  589. current = *(buf + i);
  590. //输出换行
  591. if (current == '\n')
  592. {
  593. textui_putchar(current);
  594. }
  595. // else if (current == '\t') // 输出制表符
  596. // {
  597. // int space_to_print = 8 - pos.x % 8;
  598. // while (space_to_print--)
  599. // {
  600. // textui_putchar(' ');
  601. // ++pos.x;
  602. // }
  603. // }
  604. // else if (current == '\b') // 退格
  605. // {
  606. // --pos.x;
  607. // if (pos.x < 0)
  608. // {
  609. // --pos.y;
  610. // if (pos.y <= 0)
  611. // pos.x = pos.y = 0;
  612. // else
  613. // pos.x = pos.max_x;
  614. // }
  615. // textui_putchar(' ');
  616. // auto_newline();
  617. // }
  618. else
  619. {
  620. if (current != '\0')
  621. textui_putchar(current);
  622. }
  623. }
  624. // spin_unlock_irqrestore(&printk_lock, rflags);
  625. return i;
  626. }
  627. int do_scroll(bool direction, int pixels)
  628. {
  629. if (direction == true) // 向上滚动
  630. {
  631. pixels = pixels;
  632. if (pixels > pos.height)
  633. return EPOS_OVERFLOW;
  634. // 无需滚动
  635. if (pixels == 0)
  636. return 0;
  637. unsigned int src = pixels * pos.width;
  638. unsigned int count = pos.FB_length - src;
  639. memcpy(pos.FB_address, (pos.FB_address + src), sizeof(unsigned int) * (pos.FB_length - src));
  640. memset(pos.FB_address + (pos.FB_length - src), 0, sizeof(unsigned int) * (src));
  641. return 0;
  642. }
  643. else
  644. return EUNSUPPORTED;
  645. return 0;
  646. }
  647. /**
  648. * @brief 滚动窗口(尚不支持向下滚动)
  649. *
  650. * @param direction 方向,向上滑动为true,否则为false
  651. * @param pixels 要滑动的像素数量
  652. * @param animation 是否包含滑动动画
  653. */
  654. static int scroll(bool direction, int pixels, bool animation)
  655. {
  656. // 暂时不支持反方向滚动
  657. if (direction == false)
  658. return EUNSUPPORTED;
  659. // 为了保证打印字符正确,需要对pixel按照字体高度对齐
  660. int md = pixels % pos.char_size_y;
  661. if (md)
  662. pixels = pixels + pos.char_size_y - md;
  663. if (animation == false)
  664. return do_scroll(direction, pixels);
  665. else
  666. {
  667. int steps;
  668. if (pixels > 10)
  669. steps = 5;
  670. else
  671. steps = pixels % 10;
  672. int half_steps = steps / 2;
  673. // 计算加速度
  674. double accelerate = 0.5 * pixels / (half_steps * half_steps);
  675. int current_pixels = 0;
  676. double delta_x;
  677. int trace[13] = {0};
  678. int js_trace = 0;
  679. // 加速阶段
  680. for (int i = 1; i <= half_steps; ++i)
  681. {
  682. trace[js_trace] = (int)(accelerate * i + 0.5);
  683. current_pixels += trace[js_trace];
  684. do_scroll(direction, trace[js_trace]);
  685. ++js_trace;
  686. }
  687. // 强制使得位置位于1/2*pixels
  688. if (current_pixels < pixels / 2)
  689. {
  690. delta_x = pixels / 2 - current_pixels;
  691. current_pixels += delta_x;
  692. do_scroll(direction, delta_x);
  693. }
  694. // 减速阶段,是加速阶段的重放
  695. for (int i = js_trace - 1; i >= 0; --i)
  696. {
  697. current_pixels += trace[i];
  698. do_scroll(direction, trace[i]);
  699. }
  700. if (current_pixels > pixels)
  701. kerror("During scrolling: scrolled pixels over bound!");
  702. // 强制使得位置位于pixels
  703. if (current_pixels < pixels)
  704. {
  705. delta_x = pixels - current_pixels;
  706. current_pixels += delta_x;
  707. do_scroll(direction, delta_x);
  708. }
  709. }
  710. return 0;
  711. }
  712. /**
  713. * @brief 清屏
  714. *
  715. */
  716. static int cls()
  717. {
  718. memset(pos.FB_address, BLACK, pos.FB_length * sizeof(unsigned int));
  719. pos.x = 0;
  720. pos.y = 0;
  721. return 0;
  722. }
  723. /**
  724. * @brief 获取VBE帧缓冲区长度
  725. */
  726. ul get_VBE_FB_length()
  727. {
  728. return pos.FB_length;
  729. }
  730. /**
  731. * @brief 设置pos变量中的VBE帧缓存区的线性地址
  732. * @param virt_addr VBE帧缓存区线性地址
  733. */
  734. void set_pos_VBE_FB_addr(uint *virt_addr)
  735. {
  736. pos.FB_address = (uint *)virt_addr;
  737. }
  738. static uint *get_pos_VBE_FB_addr()
  739. {
  740. return pos.FB_address;
  741. }
  742. /**
  743. * @brief 使能滚动动画
  744. *
  745. */
  746. void printk_enable_animation()
  747. {
  748. sw_show_scroll_animation = true;
  749. }
  750. /**
  751. * @brief 禁用滚动动画
  752. *
  753. */
  754. void printk_disable_animation()
  755. {
  756. sw_show_scroll_animation = false;
  757. }
  758. int sprintk(char *buf, const char *fmt, ...)
  759. {
  760. int count = 0;
  761. va_list args;
  762. va_start(args, fmt);
  763. count = vsprintf(buf, fmt, args);
  764. va_end(args);
  765. return count;
  766. }