📄 vga.c
字号:
/* set default palette */ if (CI.colors <= 256) restorepalette(default_red, default_green, default_blue); /* clear screen (sets current color to 15) */ __svgalib_currentpage = -1; vga_clear(); if (SVGAMODE(__svgalib_cur_mode)) vga_setpage(0); } __svgalib_currentpage = -1; currentlogicalwidth = CI.xbytes; currentdisplaystart = 0; usleep(MODESWITCHDELAY); /* wait for signal to stabilize */ /* enable video */#ifndef OSKIT if (!flip)#endif /* !OSKIT */ vga_screenon();#ifndef OSKIT if (mouse_support) {#ifdef DEBUG printf("svgalib: Opening mouse (type = %x).\n", mouse_type | mouse_modem_ctl);#endif if (mouse_init(mouse_device, mouse_type | mouse_modem_ctl, MOUSE_DEFAULTSAMPLERATE)) printf("svgalib: Failed to initialize mouse.\n"); else { /* vga_lockvc(); */ mouse_setxrange(0, CI.xdim - 1); mouse_setyrange(0, CI.ydim - 1); mouse_setwrap(MOUSE_NOWRAP); mouse_mode = mode; } } #endif /* !OSKIT */ { vga_modeinfo *modeinfo; modeinfo = vga_getmodeinfo(mode); MODEX = ((MODEFLAGS = modeinfo->flags) & IS_MODEX); }#ifndef OSKIT if (!flip) /* set graphics mode termio */ set_graphtermio(); else if (__svgalib_kbd_fd < 0) enable_interrupt();#endif /* !OSKIT */ }/* if (!flip) vga_unlockvc(); */#ifdef BACKGROUND __svgalib_is_vt_switching_needed();#endif return 0;}void vga_gettextfont(void *font){ unsigned int getsize; getsize = fontbufsize; if (getsize > FONT_SIZE) getsize = FONT_SIZE; memcpy(font, font_buf1, getsize); if (fontbufsize > getsize) memset(((char *)font) + getsize, 0, (size_t)(fontbufsize - getsize));}void vga_puttextfont(void *font){ unsigned int putsize;#ifdef BACKGROUND if (-1 == mprotect(font_buf1,FONT_SIZE*2,PROT_READ|PROT_WRITE)) { printf("svgalib: Memory protect error\n"); exit(-1); }#endif putsize = fontbufsize; if (putsize > FONT_SIZE) putsize = FONT_SIZE; memcpy(font_buf1, font, putsize); memcpy(font_buf2, font, putsize); if (putsize < FONT_SIZE) { memset(font_buf1 + putsize, 0, (size_t)(FONT_SIZE - putsize)); memset(font_buf2 + putsize, 0, (size_t)(FONT_SIZE - putsize)); }#ifdef BACKGROUND if (-1 == mprotect(font_buf1,FONT_SIZE*2,PROT_READ)) { printf("svgalib: Memory protect error\n"); exit(-1); }#endif}void vga_gettextmoderegs(void *regs){ memcpy(regs, text_regs, MAX_REGS);}void vga_settextmoderegs(void *regs){ memcpy(text_regs, regs, MAX_REGS);}int vga_getcurrentmode(void){ return CM;}int vga_getcurrentchipset(void){ return __svgalib_getchipset();}void vga_disabledriverreport(void){ DREP = 0;}vga_modeinfo *vga_getmodeinfo(int mode){ static vga_modeinfo modeinfo; int is_modeX = (CM == mode) && MODEX; modeinfo.linewidth = infotable[mode].xbytes; __svgalib_getchipset(); if (mode > vga_lastmodenumber()) return NULL; modeinfo.width = infotable[mode].xdim; modeinfo.height = infotable[mode].ydim; modeinfo.bytesperpixel = infotable[mode].bytesperpixel; modeinfo.colors = infotable[mode].colors; if (is_modeX) { modeinfo.linewidth = modeinfo.width / 4; modeinfo.bytesperpixel = 0; } if (mode == TEXT) { modeinfo.flags = HAVE_EXT_SET; return &modeinfo; } modeinfo.flags = 0; if ((STDVGAMODE(mode) && mode != G320x200x256) || is_modeX) __svgalib_vga_driverspecs.getmodeinfo(mode, &modeinfo); else /* Get chipset specific info for SVGA modes and */ /* 320x200x256 (chipsets may support more pages) */ chipset_getmodeinfo(mode, &modeinfo); if (modeinfo.colors == 256 && modeinfo.bytesperpixel == 0) modeinfo.flags |= IS_MODEX; if (mode > __GLASTMODE) modeinfo.flags |= IS_DYNAMICMODE; /* Maskout CAPABLE_LINEAR if requested by config file */ modeinfo.flags &= modeinfo_mask; /* If all needed info is here, signal if linear support has been enabled */ if ((modeinfo.flags & (CAPABLE_LINEAR | EXT_INFO_AVAILABLE)) == (CAPABLE_LINEAR | EXT_INFO_AVAILABLE)) { modeinfo.flags |= __svgalib_modeinfo_linearset; }#ifdef BACKGROUND if (__svgalib_runinbackground) /* these cannot be provided if we are really in background */ modeinfo.flags &= ~HAVE_RWPAGE;#endif return &modeinfo;}int vga_hasmode(int mode){ readconfigfile(); /* Make sure the config file is read. */ __svgalib_getchipset(); /* Make sure the chipset is known. */ if (mode == TEXT) return 1; if (mode < 0 || mode > lastmodenumber) return 0; return (chipset_modeavailable(mode) != 0);}int vga_lastmodenumber(void){ __svgalib_getchipset(); return lastmodenumber;}int __svgalib_addmode(int xdim, int ydim, int cols, int xbytes, int bytespp){ int i; for (i = 0; i <= lastmodenumber; ++i) if (infotable[i].xdim == xdim && infotable[i].ydim == ydim && infotable[i].colors == cols && infotable[i].bytesperpixel == bytespp && infotable[i].xbytes == xbytes) return i; if (lastmodenumber >= MAX_MODES - 1) return -1; /* no more space available */ ++lastmodenumber; infotable[lastmodenumber].xdim = xdim; infotable[lastmodenumber].ydim = ydim; infotable[lastmodenumber].colors = cols; infotable[lastmodenumber].xbytes = xbytes; infotable[lastmodenumber].bytesperpixel = bytespp; return lastmodenumber;}int vga_setcolor(int color){ switch (CI.colors) { case 2: if (color != 0) color = 15; case 16: /* update set/reset register */#ifdef BACKGROUND __svgalib_dont_switch_vt_yet(); if (!__svgalib_oktowrite) { color=color & 15; COL = color; } else {#endif port_out(0x00, GRA_I); port_out((color & 15), GRA_D);#ifdef BACKGROUND } __svgalib_is_vt_switching_needed();#endif break; default: COL = color; break; } return 0;}int vga_screenoff(void){ int tmp = 0; SCREENON = 0; if(__svgalib_novga) return 0; #ifdef BACKGROUND __svgalib_dont_switch_vt_yet(); if (!__svgalib_oktowrite) { __svgalib_is_vt_switching_needed(); return(0); }#endif if (__svgalib_driverspecs->emul && __svgalib_driverspecs->emul->screenoff) { tmp = __svgalib_driverspecs->emul->screenoff(); } else { /* turn off screen for faster VGA memory acces */ if (CHIPSET != EGA) { port_out(0x01, SEQ_I); port_out(port_in(SEQ_D) | 0x20, SEQ_D); } /* Disable video output */#ifdef DISABLE_VIDEO_OUTPUT port_in(__svgalib_IS1_R); __svgalib_delay(); port_out(0x00, ATT_IW);#endif }#ifdef BACKGROUND __svgalib_is_vt_switching_needed();#endif return tmp;}int vga_screenon(void){ int tmp = 0; SCREENON = 1; if(__svgalib_novga) return 0; #ifdef BACKGROUND __svgalib_dont_switch_vt_yet(); if (!__svgalib_oktowrite) { __svgalib_is_vt_switching_needed(); return(0); }#endif if (__svgalib_driverspecs->emul && __svgalib_driverspecs->emul->screenon) { tmp = __svgalib_driverspecs->emul->screenon(); } else { /* turn screen back on */ if (CHIPSET != EGA) { port_out(0x01, SEQ_I); port_out(port_in(SEQ_D) & 0xDF, SEQ_D); }/* #ifdef DISABLE_VIDEO_OUTPUT */ /* enable video output */ port_in(__svgalib_IS1_R); __svgalib_delay(); port_out(0x20, ATT_IW);/* #endif */ }#ifdef BACKGROUND __svgalib_is_vt_switching_needed();#endif return 0;}int vga_getxdim(void){ return CI.xdim;}int vga_getydim(void){ return CI.ydim;}int vga_getcolors(void){ return CI.colors;}int vga_white(void){ switch (CI.colors) { case 2: case 16: case 256: return 15; case 1 << 15: return 32767; case 1 << 16: return 65535; case 1 << 24: return (1 << 24) - 1; } return CI.colors - 1;}int vga_claimvideomemory(int m){ vga_modeinfo *modeinfo; int cardmemory; modeinfo = vga_getmodeinfo(CM); if (m < VMEM) return 0; if (modeinfo->colors == 16) cardmemory = modeinfo->maxpixels / 2; else cardmemory = (modeinfo->maxpixels * modeinfo->bytesperpixel + 2) & 0xffff0000; /* maxpixels * bytesperpixel can be 2 less than video memory in */ /* 3 byte-per-pixel modes; assume memory is multiple of 64K */ if (m > cardmemory) return -1; VMEM = m; return 0;}int vga_setmodeX(void){ switch (CM) { case TEXT:/* case G320x200x256: */ case G320x240x256: case G320x400x256: case G360x480x256: return 0; } if (CI.colors == 256 && VMEM < 256 * 1024) { port_out(4, SEQ_I); /* switch from linear to plane memory */ port_out((port_in(SEQ_D) & 0xF7) | 0x04, SEQ_D); port_out(0x14, __svgalib_CRT_I); /* switch double word mode off */ port_out((port_in(__svgalib_CRT_D) & 0xBF), __svgalib_CRT_D); port_out(0x17, __svgalib_CRT_I); port_out((port_in(__svgalib_CRT_D) | 0x40), __svgalib_CRT_D); CI.xbytes = CI.xdim / 4; vga_setpage(0); MODEX = 1; return 1; } return 0;}#ifdef NOTYETstatic int saved_page;static int saved_logicalwidth;static int saved_displaystart;static int saved_modeX;static void savestate(void){ int i; vga_screenoff(); savepalette(graph_red, graph_green, graph_blue); saved_page = __svgalib_currentpage; saved_logicalwidth = currentlogicalwidth; saved_displaystart = currentdisplaystart; saved_modeX = MODEX; if (CM == G320x200x256 && VMEM <= 65536) {#ifndef BACKGROUND /* 320x200x256 is a special case; only 64K is addressable */ /* (unless more has been claimed, in which case we assume */ /* SVGA bank-switching) */ if ((graph_buf = malloc(GRAPH_SIZE)) == NULL) {#endif #ifdef BACKGROUND#if BACKGROUND == 1 if ((graph_buf = valloc(GRAPH_SIZE)) == NULL) {#endif#if BACKGROUND == 2 if ((graph_buf = malloc(GRAPH_SIZE)) == NULL) {#endif#endif printf("Cannot allocate memory for VGA state\n"); vga_setmode(TEXT); exit(1); } memcpy(graph_buf, GM, GRAPH_SIZE); } else if (MODEX || CM == G800x600x16 || (STDVGAMODE(CM) && CM != G320x200x256)) { /* for planar VGA modes, save the full 256K */ __svgalib_vga_driverspecs.setmode(GPLANE16, prv_mode);#ifndef BACKGROUND if ((graph_buf = malloc(4 * GRAPH_SIZE)) == NULL) {#endif #ifdef BACKGROUND#if BACKGROUND == 1 if ((graph_buf = valloc(4 * GRAPH_SIZE)) == NULL) {#endif#if BACKGROUND == 2 if ((graph_buf = malloc(4 * GRAPH_SIZE)) == NULL) {#endif #endif printf("Cannot allocate memory for VGA state\n"); vga_setmode(TEXT); exit(1); } for (i = 0; i < 4; i++) { /* save plane i */ port_out(0x04, GRA_I); port_out(i, GRA_D); memcpy(graph_buf + i * GRAPH_SIZE, GM, GRAPH_SIZE); } } else if (CI.colors == 16) { int page, size, sbytes; unsigned char *sp; size = VMEM;#ifndef BACKGROUND if ((graph_buf = malloc(4 * size)) == NULL) {#endif#ifdef BACKGROUND#if BACKGROUND == 1 if ((graph_buf = valloc(4 * size)) == NULL) {#endif#if BACKGROUND == 2 if ((graph_buf = malloc(4 * size)) == NULL) {#endif#endif printf("Cannot allocate memory for VGA state\n"); vga_setmode(TEXT); exit(1); } sp = graph_buf; for (page = 0; size > 0; ++page) { vga_setpage(page); sbytes = (size > GRAPH_SIZE) ? GRAPH_SIZE : size; for (i = 0; i < 4; i++) { /* save plane i */ port_out(0x04, GRA_I); port_out(i, GRA_D); memcpy(sp, GM, sbytes); sp += sbytes; } size -= sbytes; } } else { /* SVGA, and SVGA 320x200x256 if videomemoryused > 65536 */ int size; int page; size = VMEM;#ifdef DEBUG printf("Saving %dK of video memory.\n", (size + 2) / 1024);#endif#ifndef BACKGROUND if ((graph_buf = malloc(size)) == NULL) {#endif#ifdef BACKGROUND#if BACKGROUND == 1 /* Must allocate hole videopage. */ if ((graph_buf = valloc((size/GRAPH_SIZE+1)*GRAPH_SIZE)) == NULL) {#endif#if BACKGROUND == 2 if ((graph_buf = malloc(size)) == NULL) {#endif#endif printf("Cannot allocate memory for SVGA state.\n"); vga_setmode(TEXT); exit(1); } page = 0; while (size >= 65536) { vga_setpage(page); memcpy(graph_buf + page * 65536, GM, 65536); page++; size -= 65536; }#ifdef BACKGROUND#if BACKGROUND == 1 /* Whole page must be written for mmap(). */ if (size > 0) { vga_setpage(page); memcpy(graph_buf + page * 65536, GM, GRAPH_SIZE); }#endif#if BACKGROUND == 2 if (size > 0) { vga_setpage(page); memcpy(graph_buf + page * 65536, GM, size); }#endif#endif#ifndef BACKGROUND if (size > 0) { vga_setpage(page); memcpy(graph_buf + page * 65536, GM, size); }#endif }}static void restorestate(void){ int i; vga_screenoff();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -