My crappy terminal OS

screen.c 2.6KB

    #include "screen.h" #include "../cpu/ports.h" #include "../libc/mem.h" #include "../util/font.h" u32 VIDEO_ADDRESS = 0; u8 COLOR_BITS = 0; u16 SCN_WIDTH; u16 SCN_HEIGHT; u16 SCN_PITCH; /* Declaration of private functions */ int get_cursor_offset(); void set_cursor_offset(int offset); int print_char(char c, int col, int row, char attr); int get_offset(int col, int row); int get_offset_row(int offset); int get_offset_col(int offset); /********************************************************** * Public Kernel API functions * **********************************************************/ void putpixel(int x, int y, u32 fg) { unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH; //TODO: Should come from above function u8 *screen = (u8*) VIDEO_ADDRESS; //TODO: Is there a way to set these all at once? for (int j = 0; j < COLOR_BITS/8; j++) { screen[where + j] = (fg >> j*8) & 255; } } void fillrect(int x, int y, u32 fg, int w, int h) { unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH; u8 *screen = (u8*) VIDEO_ADDRESS; int i, j; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { //TODO: Is there a way to set these all at once? for (int k = 0; k < COLOR_BITS/8; k++) { screen[where + (int)(j*COLOR_BITS/8) + k] = (fg >> k*8) & 255; } } where += SCN_PITCH; } } void scroll_up(u8 lines) { //NOTE: Does not clear the bottom lines! memory_copy((u8 *)(VIDEO_ADDRESS + lines * SCN_PITCH), (u8 *)VIDEO_ADDRESS, (SCN_HEIGHT - lines) * SCN_PITCH); } u32 color_to_pixel(Color_t color) { //32bpp max //Scale the values properly u8 r = color.r * ((1 << ADV_VBE_INFO->red_mask) - 1) / 255; u8 g = color.g * ((1 << ADV_VBE_INFO->green_mask) - 1) / 255; u8 b = color.b * ((1 << ADV_VBE_INFO->blue_mask) - 1) / 255; return (b << ADV_VBE_INFO->blue_position) + (g << ADV_VBE_INFO->green_position) + (r << ADV_VBE_INFO->red_position); } /********************************************************** * Private kernel functions * **********************************************************/ /** * Innermost print function for our kernel, directly accesses the video memory * * If 'col' and 'row' are negative, we will print at current cursor location * If 'attr' is zero it will use 'white on black' as default * Returns the offset of the next character * Sets the video cursor to the returned offset */ void setup_video() { struct vbe_mode_info_structure *vbe_info = (struct vbe_mode_info_structure *)0x7e00; ADV_VBE_INFO = vbe_info; VIDEO_ADDRESS = vbe_info->framebuffer; COLOR_BITS = vbe_info->bpp; SCN_WIDTH = vbe_info->width; SCN_HEIGHT = vbe_info->height; SCN_PITCH = vbe_info->pitch; }