【问题标题】:Adding scrolling functionality添加滚动功能
【发布时间】:2016-02-25 12:02:31
【问题描述】:

我已经设法在我的程序中添加了对换行符的支持,我现在正试图弄清楚如何实现滚动功能,以便当终端已满时,用户可以上下滚动。目前我正在输出 25 条消息“Hello World”,但由于终端上没有空间,因此并未显示所有消息。因此,我想实现滚动功能,以便用户可以阅读终端上显示的所有消息。我试图实现缓冲区,但我似乎无法让它工作,因为我不确定我哪里出错了。代码是这样的:

任何可以帮助我解决此问题的提示/建议将不胜感激。

uint8_t make_color(enum vga_color fg, enum vga_color bg) {
  return fg | bg << 4;
}

uint16_t make_vgaentry(char c, uint8_t color) {
  uint16_t c16 = c;
  uint16_t color16 = color;
  return c16 | color16 << 8;
}

size_t strlen(const char* str) {
  size_t ret = 0;
  while ( str[ret] != 0 )
    ret++;
  return ret;
}

static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;

size_t term_row;
size_t term_column;
uint8_t term_color;
uint16_t* term_buffer;

    void terminal_initialize() {
      term_row = 0;
      term_column = 0;
      term_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK); /*Terminal Background*/
      term_buffer = (uint16_t*) 0xB8000;
      for (size_t y = 0; y < VGA_HEIGHT; y++) {
        for (size_t x = 0; x < VGA_WIDTH; x++) {
          const size_t index = y * VGA_WIDTH + x;
          term_buffer[index] = make_vgaentry(' ', term_color);
          term_buffer[y * VGA_WIDTH + x] = term_buffer[(y + 1) * VGA_WIDTH + x];
        }
      }
    }

    void term_setcolor(uint8_t color) {
      term_color = color;
    }

void *my_memmove(void *dest, const void *src, size_t n) {
  signed char operation;
  size_t end;
  size_t current;

  if(dest != src) {
    if(dest < src) {
      operation = 1;
      current = 0;
      end = n;
    } else {
      operation = -1;
      current = n - 1;
      end = -1;
    }

    for( ; current != end; current += operation) {
      *(((unsigned char*)dest) + current) = *(((unsigned char*)src) + current);
    }
  }
  return dest;
}

void term_scroll()
{
  /* scroll up one line*/
  my_memmove(term_buffer, term_buffer + VGA_WIDTH * sizeof(uint16_t), 
          (VGA_WIDTH - 1) * VGA_HEIGHT * sizeof(uint16_t)) ;

  /* zero out the last line*/
  for (unsigned int i = (VGA_HEIGHT-1) * VGA_WIDTH; i < (VGA_HEIGHT) * VGA_WIDTH; i++)
  {
    term_buffer[i] = make_vgaentry(' ', term_color);
  }
}

    void term_putentryat(char c, uint8_t color, size_t x, size_t y) {
      const size_t index = y * VGA_WIDTH + x;
      term_buffer[index] = make_vgaentry(c, color);
    }

    void term_putchar(char c) {
      if (c == '\n')
      {
        term_column = 0;
        if (++term_row == VGA_HEIGHT)
        {
           /*perform scroll; Trying to implement scrolling functionality here*/
           term_column = 0;

        }
      }
      else

         term_putentryat(c, term_color, term_column, term_row);
         if (++term_column == VGA_WIDTH) {
            term_column = 0;
         if (++term_row == VGA_HEIGHT) {
          /*perform scroll; Trying to implement scrolling functionality here*/
            term_row = 0;
          }
      }
    }

    void term_writestring(const char* data) {
      size_t datalen = strlen(data);
      for (size_t i = 0; i < datalen; i++)
        term_putchar(data[i]);
    }

主要功能如下:

 void main(){
    term_initialize();
    term_writestring(" Kernel World1!\n");
    term_writestring(" Kernel World2!\n");
    term_writestring(" Kernel World3!\n");
    term_writestring(" Kernel World4!\n");
    .
    .
    .
    term_writestring(" Kernel World25!\n");
    }

【问题讨论】:

  • 请不要在得到答案后试图破坏您的问题。此类行为可能会导致 Stack Overflow 被暂时或永久禁止。

标签: c


【解决方案1】:

试试这个:

void terminal_scroll()
{
  // scroll up one line
  memmove(term_buffer, term_buffer + VGA_WIDTH * sizeof(uint16_t), 
          (VGA_WIDTH - 1) * VGA_HEIGHT * sizeof(uint16_t)) ;

  // zero out the last line
  for (int i = (VGA_HEIGHT-1) * VGA_WIDTH; i < (VGA_HEIGHT) * VGA_WIDTH; i++)
  {
    term_buffer[i] = vgaentry(' ', terminal_color);
  }
}

这是未经测试的代码,可能无法编译也不会出错,但它至少应该让您知道该怎么做。

【讨论】:

  • memmove 是标准函数。如果你不能在你的上下文中使用这个标准函数,那么你必须编写它。谷歌“memmove 源代码”,你会发现大量的实现
  • @user2977378 " 我假设我需要在 terminal_putchar 中调用 terminal_scroll 函数?" .. 嗯...是的。
  • @user2977378 欢迎您投票/接受答案。
猜你喜欢
  • 2017-04-05
  • 2016-08-15
  • 2014-02-14
  • 2020-03-02
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
相关资源
最近更新 更多