Browse Source

new: textui支持彩色

fslongjin 2 years ago
parent
commit
db024b34e0

+ 7 - 321
kernel/common/printk.c

@@ -3,63 +3,15 @@
 //
 #include "printk.h"
 #include "kprint.h"
-#include <driver/multiboot2/multiboot2.h>
+
 #include <mm/mm.h>
 #include <common/spinlock.h>
 #include <lib/libUI/textui.h>
-#include <lib/libUI/screen_manager.h>
 
-#include <driver/uart/uart.h>
-#include <driver/video/video.h>
 #include "math.h"
 #include <common/string.h>
-#include <lib/libUI/textui.h>
-
-struct printk_screen_info pos;
-
-static spinlock_t printk_lock={1};
-static bool sw_show_scroll_animation = false; // 显示换行动画的开关
-
-/**
- * @brief Set the printk pos object
- *
- * @param x 列坐标
- * @param y 行坐标
- */
-static int set_printk_pos(const int x, const int y);
-
-/**
- * @brief 在屏幕上指定位置打印字符
- *
- * @param fb 帧缓存线性地址
- * @param Xsize 行分辨率
- * @param x 左上角列像素点位置
- * @param y 左上角行像素点位置
- * @param FRcolor 字体颜色
- * @param BKcolor 背景颜色
- * @param font 字符的bitmap
- */
-static void putchar(uint *fb, int Xsize, int x, int y, unsigned int FRcolor, unsigned int BKcolor, unsigned char font);
-
-static uint *get_pos_VBE_FB_addr();
-
-/**
- * @brief 清屏
- *
- */
-static int cls();
 
-#pragma GCC push_options
-#pragma GCC optimize("O0")
-/**
- * @brief 滚动窗口(尚不支持向下滚动)
- *
- * @param direction  方向,向上滑动为true,否则为false
- * @param pixels 要滑动的像素数量
- * @param animation 是否包含滑动动画
- */
-static int scroll(bool direction, int pixels, bool animation);
-#pragma GCC pop_options
+static spinlock_t __printk_lock = {1};
 /**
  * @brief 将数字按照指定的要求转换成对应的字符串(2~36进制)
  *
@@ -74,56 +26,6 @@ static char *write_num(char *str, ul num, int base, int field_width, int precisi
 
 static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
 
-static int calculate_max_charNum(int len, int size)
-{
-    /**
-     * @brief 计算屏幕上能有多少行
-     * @param len 屏幕长/宽
-     * @param size 字符长/宽
-     */
-    return len / size - 1;
-}
-
-int printk_init(struct scm_buffer_info_t *buf)
-{
-
-    pos.width = buf->width;
-    pos.height = buf->height;
-
-    pos.char_size_x = 8;
-    pos.char_size_y = 16;
-    pos.max_x = calculate_max_charNum(pos.width, pos.char_size_x);
-    pos.max_y = calculate_max_charNum(pos.height, pos.char_size_y);
-
-    pos.FB_address = buf->vaddr;
-    pos.FB_length = 1UL * pos.width * pos.height;
-
-    // 初始化自旋锁
-    spin_init(&printk_lock);
-
-    pos.x = 0;
-    pos.y = 0;
-
-    cls();
-
-    io_mfence();
-    kdebug("width=%d\theight=%d", pos.width, pos.height);
-    // 由于此时系统并未启用双缓冲,因此关闭滚动动画
-    printk_disable_animation();
-
-    io_mfence();
-    return 0;
-}
-
-static int set_printk_pos(const int x, const int y)
-{
-    // 指定的坐标不在屏幕范围内
-    if (!((x >= 0 && x <= pos.max_x) && (y >= 0 && y <= pos.max_y)))
-        return EPOS_OVERFLOW;
-    pos.x = x;
-    pos.y = y;
-    return 0;
-}
 static int skip_and_atoi(const char **s)
 {
     /**
@@ -139,37 +41,6 @@ static int skip_and_atoi(const char **s)
     return ans;
 }
 
-static void auto_newline()
-{
-    /**
-     * @brief 超过每行最大字符数,自动换行
-     *
-     */
-
-    if (pos.x > pos.max_x)
-    {
-#ifdef DEBUG
-        uart_send(COM1, '\r');
-        uart_send(COM1, '\n');
-#endif
-        pos.x = 0;
-        ++pos.y;
-    }
-    if (pos.y > pos.max_y)
-    {
-#ifdef DEBUG
-        uart_send(COM1, '\r');
-        uart_send(COM1, '\n');
-#endif
-        pos.y = pos.max_y;
-        int lines_to_scroll = 1;
-        barrier();
-        scroll(true, lines_to_scroll * pos.char_size_y, sw_show_scroll_animation);
-        barrier();
-        pos.y -= (lines_to_scroll - 1);
-    }
-}
-
 int vsprintf(char *buf, const char *fmt, va_list args)
 {
     /**
@@ -637,8 +508,6 @@ static char *write_float_point_num(char *str, double num, int field_width, int p
         *str++ = sign;
 
     // 输出整数部分
-    // while (js_num_z-- > 0)
-    //     *str++ = tmp_num_z[js_num_z];
     while (js_num_z > 0)
     {
         *str++ = tmp_num_z[js_num_z - 1];
@@ -672,7 +541,8 @@ static char *write_float_point_num(char *str, double num, int field_width, int p
  */
 int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ...)
 {
-    
+    uint64_t rflags;
+    spin_lock_irqsave(&__printk_lock, rflags);
     va_list args;
     va_start(args, fmt);
     char buf[4096]; // vsprintf()的缓冲区
@@ -685,197 +555,13 @@ int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ..
     for (i = 0; i < len; ++i)
     {
         current = *(buf + i);
-        //输出换行
-        if (current == '\n')
-        {
-
-            textui_putchar(current);
-        }
-        // else if (current == '\t') // 输出制表符
-        // {
-        //     int space_to_print = 8 - pos.x % 8;
-
-        //     while (space_to_print--)
-        //     {
-        //         textui_putchar(' ');
-        //         ++pos.x;
-        //     }
-        // }
-        // else if (current == '\b') // 退格
-        // {
-        //     --pos.x;
-        //     if (pos.x < 0)
-        //     {
-        //         --pos.y;
-        //         if (pos.y <= 0)
-        //             pos.x = pos.y = 0;
-        //         else
-        //             pos.x = pos.max_x;
-        //     }
-
-        //     textui_putchar(' ');
-
-        //     auto_newline();
-        // }
-        else
-        {
-            if (current != '\0')
-                textui_putchar(current);
-        }
+        // 输出
+        textui_putchar(current, FRcolor, BKcolor);
     }
-
-    // spin_unlock_irqrestore(&printk_lock, rflags);
+    spin_unlock_irqrestore(&__printk_lock, rflags);
     return i;
 }
 
-int do_scroll(bool direction, int pixels)
-{
-    if (direction == true) // 向上滚动
-    {
-        pixels = pixels;
-        if (pixels > pos.height)
-            return EPOS_OVERFLOW;
-        // 无需滚动
-        if (pixels == 0)
-            return 0;
-        unsigned int src = pixels * pos.width;
-        unsigned int count = pos.FB_length - src;
-
-        memcpy(pos.FB_address, (pos.FB_address + src), sizeof(unsigned int) * (pos.FB_length - src));
-        memset(pos.FB_address + (pos.FB_length - src), 0, sizeof(unsigned int) * (src));
-
-        return 0;
-    }
-    else
-        return EUNSUPPORTED;
-    return 0;
-}
-/**
- * @brief 滚动窗口(尚不支持向下滚动)
- *
- * @param direction  方向,向上滑动为true,否则为false
- * @param pixels 要滑动的像素数量
- * @param animation 是否包含滑动动画
- */
-static int scroll(bool direction, int pixels, bool animation)
-{
-    // 暂时不支持反方向滚动
-    if (direction == false)
-        return EUNSUPPORTED;
-    // 为了保证打印字符正确,需要对pixel按照字体高度对齐
-    int md = pixels % pos.char_size_y;
-    if (md)
-        pixels = pixels + pos.char_size_y - md;
-    if (animation == false)
-        return do_scroll(direction, pixels);
-    else
-    {
-
-        int steps;
-        if (pixels > 10)
-            steps = 5;
-        else
-            steps = pixels % 10;
-        int half_steps = steps / 2;
-
-        // 计算加速度
-        double accelerate = 0.5 * pixels / (half_steps * half_steps);
-        int current_pixels = 0;
-        double delta_x;
-
-        int trace[13] = {0};
-        int js_trace = 0;
-        // 加速阶段
-        for (int i = 1; i <= half_steps; ++i)
-        {
-            trace[js_trace] = (int)(accelerate * i + 0.5);
-            current_pixels += trace[js_trace];
-            do_scroll(direction, trace[js_trace]);
-
-            ++js_trace;
-        }
-
-        // 强制使得位置位于1/2*pixels
-        if (current_pixels < pixels / 2)
-        {
-            delta_x = pixels / 2 - current_pixels;
-            current_pixels += delta_x;
-            do_scroll(direction, delta_x);
-        }
-
-        // 减速阶段,是加速阶段的重放
-        for (int i = js_trace - 1; i >= 0; --i)
-        {
-            current_pixels += trace[i];
-            do_scroll(direction, trace[i]);
-        }
-
-        if (current_pixels > pixels)
-            kerror("During scrolling: scrolled pixels over bound!");
-
-        // 强制使得位置位于pixels
-        if (current_pixels < pixels)
-        {
-            delta_x = pixels - current_pixels;
-            current_pixels += delta_x;
-            do_scroll(direction, delta_x);
-        }
-    }
-
-    return 0;
-}
-
-/**
- * @brief 清屏
- *
- */
-static int cls()
-{
-    memset(pos.FB_address, BLACK, pos.FB_length * sizeof(unsigned int));
-    pos.x = 0;
-    pos.y = 0;
-    return 0;
-}
-
-/**
- * @brief 获取VBE帧缓冲区长度
- */
-ul get_VBE_FB_length()
-{
-    return pos.FB_length;
-}
-
-/**
- * @brief 设置pos变量中的VBE帧缓存区的线性地址
- * @param virt_addr VBE帧缓存区线性地址
- */
-void set_pos_VBE_FB_addr(uint *virt_addr)
-{
-    pos.FB_address = (uint *)virt_addr;
-}
-
-static uint *get_pos_VBE_FB_addr()
-{
-    return pos.FB_address;
-}
-
-/**
- * @brief 使能滚动动画
- *
- */
-void printk_enable_animation()
-{
-    sw_show_scroll_animation = true;
-}
-/**
- * @brief 禁用滚动动画
- *
- */
-void printk_disable_animation()
-{
-    sw_show_scroll_animation = false;
-}
-
 int sprintk(char *buf, const char *fmt, ...)
 {
     int count = 0;

+ 1 - 57
kernel/common/printk.h

@@ -36,33 +36,8 @@
 #include <lib/libUI/screen_manager.h>
 #include <stdarg.h>
 
-struct printk_screen_info
-{
-    int width, height; //屏幕大小
-
-    int max_x, max_y; // 最大x、y字符数
-
-    int x, y; //光标位置
-
-    int char_size_x, char_size_y;
-
-    uint *FB_address; //帧缓冲区首地址
-
-    unsigned long FB_length; // 帧缓冲区长度(乘以4才是字节数)
-};
-
 extern unsigned char font_ascii[256][16]; //导出ascii字体的bitmap(8*16大小) ps:位于font.h中
 
-
-
-/**
- * @brief 初始化printk的屏幕信息
- *
- * @param char_size_x 字符的列坐标
- * @param char_size_y 字符的行坐标
- */
-int printk_init(struct scm_buffer_info_t* buf);
-
 /**
  * @brief 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中
  *
@@ -73,9 +48,6 @@ int printk_init(struct scm_buffer_info_t* buf);
  */
 int vsprintf(char *buf, const char *fmt, va_list args);
 
-
-
-
 /**
  * @brief 格式化打印字符串
  *
@@ -88,37 +60,9 @@ int vsprintf(char *buf, const char *fmt, va_list args);
 
 int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ...);
 
-
-
-
-/**
- * @brief 获取VBE帧缓冲区长度
-
- */
-ul get_VBE_FB_length();
-
-/**
- * @brief 设置pos变量中的VBE帧缓存区的线性地址
- * @param virt_addr VBE帧缓存区线性地址
- */
-void set_pos_VBE_FB_addr(uint* virt_addr);
-
-
-
-/**
- * @brief 使能滚动动画
- * 
- */
-void printk_enable_animation();
-/**
- * @brief 禁用滚动动画
- * 
- */
-void printk_disable_animation();
-
 /**
  * @brief 格式化字符串并输出到buf
- * 
+ *
  * @param buf 输出缓冲区
  * @param fmt 格式
  * @param ... 参数

+ 5 - 9
kernel/lib/libUI/textui-render.c

@@ -44,7 +44,7 @@ int textui_refresh_vlines(struct textui_window_t *window, uint16_t start, uint16
         textui_refresh_vline(window, i);
     }
     start = 0;
-    while (count>0)
+    while (count > 0)
     {
         // sprintk(bufff, "[ 2fresh: %d ] ", start);
         // uart_send_str(COM1, bufff);
@@ -116,17 +116,13 @@ static void __textui_render_chromatic(uint16_t actual_line, uint16_t index, stru
      * @param font 字符的bitmap
      */
 
-    // #if DEBUG
-    // uart_send(COM1, font);
-    // #endif
-
     unsigned char *font_ptr = font_ascii[(uint8_t)character->c];
     unsigned int *addr;
     uint32_t *fb = (uint32_t *)textui_framework.buf->vaddr;
-    // uint32_t FRcolor = YELLOW;
-    uint32_t FRcolor = calculate_color(character->Fr, character->Fg, character->Fb);
-    // uint32_t BKcolor = BLACK;
-    uint32_t BKcolor = calculate_color(character->Br, character->Bg, character->Bb);
+
+    uint32_t FRcolor = character->FRcolor & 0x00ffffff;
+
+    uint32_t BKcolor = character->BKcolor & 0x00ffffff;
 
     uint32_t x = index * TEXTUI_CHAR_WIDTH;
     uint32_t y = actual_line * TEXTUI_CHAR_HEIGHT;

+ 27 - 20
kernel/lib/libUI/textui.c

@@ -90,7 +90,6 @@ int textui_change_handler(struct scm_buffer_info_t *buf)
 {
     memcpy((void *)buf->vaddr, (void *)(textui_framework.buf->vaddr), textui_framework.buf->size);
     textui_framework.buf = buf;
-    set_pos_VBE_FB_addr((uint *)buf->vaddr);
     return 0;
 }
 
@@ -144,13 +143,12 @@ static int __textui_new_line(struct textui_window_t *window, uint16_t vline_id)
 
     if (likely(window->vlines_used == window->vlines_num)) // 需要滚动屏幕
     {
-        // uart_send_str(COM1, " scroll, top vline= ");
+
         ++window->top_vline;
-        // uart_send(COM1, '0' + window->top_vline);
+
         if (unlikely(window->top_vline >= window->vlines_num))
             window->top_vline = 0;
 
-        // int delta = ABS((int)window->vline_operating - (int)window->top_vline);
         // 刷新所有行
         textui_refresh_vlines(window, window->top_vline, window->vlines_num);
     }
@@ -159,19 +157,23 @@ static int __textui_new_line(struct textui_window_t *window, uint16_t vline_id)
 
     return 0;
 }
-static int __textui_putchar_window(struct textui_window_t *window, uint16_t character)
+
+/**
+ * @brief 真正向屏幕上输出字符的函数
+ *
+ * @param window
+ * @param character
+ * @return int
+ */
+static int __textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
 {
     if (textui_is_chromatic(window->flags)) // 启用彩色字符
     {
         struct textui_vline_chromatic_t *vline = &window->vlines.chromatic[window->vline_operating];
 
         vline->chars[vline->index].c = character;
-        vline->chars[vline->index].Fr = 0xff;
-        vline->chars[vline->index].Fg = 0xff;
-        vline->chars[vline->index].Fb = 0xff;
-        vline->chars[vline->index].Br = 0;
-        vline->chars[vline->index].Bg = 0;
-        vline->chars[vline->index].Bb = 0;
+        vline->chars[vline->index].FRcolor = FRcolor & 0xffffff;
+        vline->chars[vline->index].BKcolor = BKcolor & 0xffffff;
         ++vline->index;
         textui_refresh_characters(window, window->vline_operating, vline->index - 1, 1);
         // 换行
@@ -188,20 +190,23 @@ static int __textui_putchar_window(struct textui_window_t *window, uint16_t char
     }
     return 0;
 }
+
 /**
  * @brief 在指定窗口上输出一个字符
  *
  * @param window 窗口
  * @param character 字符
+ * @param FRcolor 前景色(RGB)
+ * @param BKcolor 背景色(RGB)
  * @return int
  */
-int textui_putchar_window(struct textui_window_t *window, uint16_t character)
+int textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
 {
     if (unlikely(character == '\0'))
         return 0;
     if (!textui_is_chromatic(window->flags)) // 暂不支持纯文本窗口
         return 0;
-    
+
     uint64_t rflags = 0; // 加锁后rflags存储到这里
     spin_lock_irqsave(&window->lock, rflags);
     uart_send(COM1, character);
@@ -217,7 +222,7 @@ int textui_putchar_window(struct textui_window_t *window, uint16_t character)
 
         while (space_to_print--)
         {
-            __textui_putchar_window(window, ' ');
+            __textui_putchar_window(window, ' ', FRcolor, BKcolor);
         }
     }
     else if (character == '\b') // 退格
@@ -251,7 +256,7 @@ int textui_putchar_window(struct textui_window_t *window, uint16_t character)
         }
     }
     else
-        __textui_putchar_window(window, character);
+        __textui_putchar_window(window, character, FRcolor, BKcolor);
 
     spin_unlock_irqrestore(&window->lock, rflags);
     return 0;
@@ -261,12 +266,14 @@ int textui_putchar_window(struct textui_window_t *window, uint16_t character)
  * @brief 在默认窗口上输出一个字符
  *
  * @param character 字符
+ * @param FRcolor 前景色(RGB)
+ * @param BKcolor 背景色(RGB)
  * @return int
  */
-int textui_putchar(uint16_t character)
+int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
 {
-    
-    return textui_putchar_window(__private_info.default_window, character);
+
+    return textui_putchar_window(__private_info.default_window, character, FRcolor, BKcolor);
 }
 
 /**
@@ -293,7 +300,7 @@ int textui_init()
     int retval = scm_register(&textui_framework);
     if (retval != 0)
     {
-        uart_send_str(COM1, "text ui init failed");
+        uart_send_str(COM1, "text ui init failed\n");
         while (1)
             pause();
     }
@@ -317,6 +324,6 @@ int textui_init()
     __private_info.default_window = &__initial_window;
     __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT;
 
-    uart_send_str(COM1, "text ui initialized");
+    uart_send_str(COM1, "text ui initialized\n");
     return 0;
 }

+ 10 - 9
kernel/lib/libUI/textui.h

@@ -29,16 +29,13 @@ struct textui_char_normal_t
  */
 struct textui_char_chromatic_t
 {
-    uint16_t c; // 字符
+    unsigned c : 16;
+
     // 前景色
-    uint8_t Fr; // 红
-    uint8_t Fg; // 绿
-    uint8_t Fb; // 蓝
+    unsigned FRcolor : 24; // rgb
 
     // 背景色
-    uint8_t Br;
-    uint8_t Bg;
-    uint8_t Bb;
+    unsigned BKcolor : 24; // rgb
 };
 
 // 注意!!! 请保持vline结构体的大小、成员变量命名相等!
@@ -122,17 +119,21 @@ int textui_refresh_characters(struct textui_window_t *window, uint16_t vline_id,
  *
  * @param window 窗口
  * @param character 字符
+ * @param FRcolor 前景色(RGB)
+ * @param BKcolor 背景色(RGB)
  * @return int
  */
-int textui_putchar_window(struct textui_window_t *window, uint16_t character);
+int textui_putchar_window(struct textui_window_t *window, uint16_t character, uint32_t FRcolor, uint32_t BKcolor);
 
 /**
  * @brief 在默认窗口上输出一个字符
  *
  * @param character 字符
+ * @param FRcolor 前景色(RGB)
+ * @param BKcolor 背景色(RGB)
  * @return int
  */
-int textui_putchar(uint16_t character);
+int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor);
 
 /**
  * @brief 获取textui的帧缓冲区能容纳的内容的行数

+ 2 - 2
kernel/main.c

@@ -76,7 +76,7 @@ void system_initialize()
 
     scm_init();
     textui_init();
-    // kinfo("Kernel Starting...");
+    kinfo("Kernel Starting...");
 
     // 重新加载gdt和idt
     ul tss_item_addr = (ul)phys_2_virt(0x7c00);
@@ -173,7 +173,7 @@ void system_initialize()
     // 系统初始化到此结束,剩下的初始化功能应当放在初始内核线程中执行
     apic_timer_init();
     io_mfence();
-     while(1);
+
 }
 
 //操作系统内核从这里开始执行