1 /*
2 * vid_svgalib.c: Linux SVGALIB specific video driver.
3 * from quake1 source with minor adaptations for uhexen2.
4 * $Id: vid_svgalib.c 5676 2016-07-20 12:56:03Z sezero $
5 *
6 * Copyright (C) 1996-1997 Id Software, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #if 0
25 /* these should be for vt stuff */
26 #include <termios.h>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/vt.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <signal.h>
33 #endif
34
35 #include "q_stdinc.h"
36 /*#include <sys/io.h>*/ /* outb()*/
37
38 #include "vga.h"
39
40 #include "quakedef.h"
41 #include "d_local.h"
42
43
44 unsigned short d_8to16table[256]; /* not used in 8 bpp mode */
45 unsigned int d_8to24table[256]; /* not used in 8 bpp mode */
46
47 byte globalcolormap[VID_GRADES*256], lastglobalcolor = 0;
48 byte *lastsourcecolormap = NULL;
49
50 viddef_t vid; /* global video state */
51
52 int VGA_width, VGA_height, VGA_rowbytes, VGA_bufferrowbytes;
53 byte *VGA_pagebase;
54 static byte *framebuffer_ptr;
55 static int VGA_planar;
56
57 static byte backingbuf[48*24];
58
59 static byte *vid_surfcache;
60 static int VID_highhunkmark;
61
62 static int num_modes;
63 static vga_modeinfo *modes;
64 static int current_mode;
65
66 static byte vid_current_palette[768]; /* save for mode changes */
67
68 static int svgalib_inited = 0;
69 static int svgalib_backgrounded = 0;
70
71 static cvar_t vid_mode = {"vid_mode", "5", CVAR_NONE};
72 static cvar_t vid_redrawfull = {"vid_redrawfull", "0", CVAR_NONE};
73 static cvar_t vid_waitforrefresh = {"vid_waitforrefresh", "0", CVAR_ARCHIVE};
74
75 /* globals for compatibility: */
76 modestate_t modestate = MS_UNINIT;
77 cvar_t _enable_mouse = {"_enable_mouse", "1", CVAR_ARCHIVE};
78
79
80 #if 0
81 static void vtswitch (int newconsole)
82 {
83 int fd;
84 struct vt_stat x;
85
86 /* switch consoles and wait until reactivated */
87 fd = open("/dev/console", O_RDONLY);
88 ioctl(fd, VT_GETSTATE, &x);
89 ioctl(fd, VT_ACTIVATE, newconsole);
90 ioctl(fd, VT_WAITACTIVE, x.v_active);
91 close(fd);
92 }
93 #endif
94
95 /*
96 =================
97 VID_Gamma_f
98
99 Keybinding command
100 =================
101 */
102 #if 0
103 static void VID_Gamma_f (void)
104 {
105 float gamma, f, inf;
106 unsigned char palette[768];
107 int i;
108
109 if (Cmd_Argc () == 2)
110 {
111 gamma = atof (Cmd_Argv(1));
112
113 for (i = 0; i < 768; i++)
114 {
115 f = pow ((host_basepal[i] + 1) / 256.0, gamma);
116 inf = f*255 + 0.5;
117 if (inf < 0)
118 inf = 0;
119 if (inf > 255)
120 inf = 255;
121 palette[i] = inf;
122 }
123
124 VID_SetPalette (palette);
125
126 vid.recalc_refdef = 1; /* force a surface cache flush */
127 }
128 }
129 #endif
130
VID_DescribeMode_f(void)131 static void VID_DescribeMode_f (void)
132 {
133 int modenum;
134
135 modenum = atoi (Cmd_Argv(1));
136 if ((modenum >= num_modes) || (modenum < 0 ) || !modes[modenum].width)
137 Con_Printf("Invalid video mode: %d!\n", modenum);
138 else
139 {
140 Con_Printf("%d: %d x %d - ", modenum, modes[modenum].width, modes[modenum].height);
141 if (modes[modenum].bytesperpixel == 0)
142 Con_Printf("ModeX\n");
143 else
144 Con_Printf("%d bpp\n", modes[modenum].bytesperpixel<<3);
145 }
146 }
147
VID_DescribeModes_f(void)148 static void VID_DescribeModes_f (void)
149 {
150 int i;
151
152 for (i = 0; i < num_modes; i++)
153 {
154 if (modes[i].width)
155 {
156 Con_Printf("%d: %d x %d - ", i, modes[i].width, modes[i].height);
157 if (modes[i].bytesperpixel == 0)
158 Con_Printf("ModeX\n");
159 else
160 Con_Printf("%d bpp\n", modes[i].bytesperpixel<<3);
161 }
162 }
163 }
164
165 /*
166 ================
167 VID_NumModes
168 ================
169 */
VID_NumModes(void)170 static int VID_NumModes (void)
171 {
172 int i, i1 = 0;
173
174 for (i = 0; i < num_modes; i++)
175 i1 += (modes[i].width ? 1 : 0);
176 return (i1);
177 }
178
VID_NumModes_f(void)179 static void VID_NumModes_f (void)
180 {
181 Con_Printf("%d modes\n", VID_NumModes());
182 }
183
VID_Debug_f(void)184 static void VID_Debug_f (void)
185 {
186 Con_Printf("mode: %d\n", current_mode);
187 Con_Printf("height x width: %d x %d\n", vid.height, vid.width);
188 Con_Printf("bpp: %d\n", modes[current_mode].bytesperpixel*8);
189 Con_Printf("vid.aspect: %f\n", vid.aspect);
190 }
191
192
VID_InitModes(void)193 static void VID_InitModes (void)
194 {
195 int i;
196
197 /* get complete information on all modes */
198 num_modes = vga_lastmodenumber() + 1;
199 modes = (vga_modeinfo *) Z_Malloc(num_modes * sizeof(vga_modeinfo), Z_MAINZONE);
200 for (i = 0; i < num_modes; i++)
201 {
202 if (vga_hasmode(i))
203 memcpy(&modes[i], vga_getmodeinfo(i), sizeof(vga_modeinfo));
204 else
205 modes[i].width = 0; /* means not available */
206 }
207
208 /* filter for modes i don't support */
209 for (i = 0; i < num_modes; i++)
210 {
211 if (modes[i].bytesperpixel != 1 && modes[i].colors != 256)
212 modes[i].width = 0;
213 }
214 }
215
get_mode(char * name,int width,int height,int depth)216 static int get_mode (char *name, int width, int height, int depth)
217 {
218 int i;
219
220 if (name)
221 {
222 i = vga_getmodenumber(name);
223 if (i < 0 || !modes[i].width)
224 {
225 Sys_Printf("Mode [%s] not supported\n", name);
226 i = -1;
227 }
228 }
229 else
230 {
231 int need, match;
232 need = (!!width) | ((!!height) << 1) | ((!!depth) << 2);
233
234 for (i = 0; i < num_modes; i++)
235 {
236 if (!modes[i].width)
237 continue;
238 if (width && modes[i].width != width)
239 continue;
240 if (height && modes[i].height != height)
241 continue;
242 if (depth && modes[i].bytesperpixel != depth/8)
243 continue;
244 match = ((modes[i].width == width) << 0) |
245 ((modes[i].height == height) << 1) |
246 ((modes[i].bytesperpixel == depth/8) << 2);
247 if (match & need)
248 break; /* got a match */
249 }
250 if (i == num_modes)
251 {
252 Sys_Printf("Mode %dx%d (%d bits) not supported\n", width, height, depth);
253 i = -1;
254 }
255 }
256
257 return i;
258 }
259
VID_Shutdown(void)260 void VID_Shutdown (void)
261 {
262 if (!svgalib_inited)
263 return;
264
265 vga_setmode(TEXT);
266
267 svgalib_inited = 0;
268 }
269
VID_SetPalette(const unsigned char * palette)270 void VID_SetPalette (const unsigned char *palette)
271 {
272 static int tmppal[256*3];
273 int *tp;
274 int i;
275
276 if (!svgalib_inited)
277 return;
278 if (svgalib_backgrounded)
279 return;
280
281 if (palette != vid_current_palette)
282 memcpy(vid_current_palette, palette, sizeof(vid_current_palette));
283
284 if (vga_getcolors() == 256)
285 {
286 tp = tmppal;
287 for (i = 256 * 3; i; i--)
288 *(tp++) = *(palette++) >> 2;
289
290 if (vga_oktowrite())
291 vga_setpalvec(0, 256, tmppal);
292 }
293 }
294
VID_ShiftPalette(const unsigned char * palette)295 void VID_ShiftPalette (const unsigned char *palette)
296 {
297 VID_SetPalette (palette);
298 }
299
VID_SetMode(int modenum,const unsigned char * palette)300 static qboolean VID_SetMode (int modenum, const unsigned char *palette)
301 {
302 int bsize, zsize, tsize;
303
304 if ((modenum >= num_modes) || (modenum < 0) || !modes[modenum].width)
305 {
306 Cvar_SetValueQuick (&vid_mode, (float)current_mode);
307 Con_Printf("No such video mode: %d\n",modenum);
308 return false;
309 }
310
311 Cvar_SetValueQuick (&vid_mode, (float)modenum);
312
313 current_mode = modenum;
314
315 vid.width = modes[current_mode].width;
316 vid.height = modes[current_mode].height;
317
318 VGA_width = modes[current_mode].width;
319 VGA_height = modes[current_mode].height;
320 VGA_planar = modes[current_mode].bytesperpixel == 0;
321 VGA_rowbytes = modes[current_mode].linewidth;
322 vid.rowbytes = modes[current_mode].linewidth;
323 if (VGA_planar)
324 {
325 VGA_bufferrowbytes = modes[current_mode].linewidth * 4;
326 vid.rowbytes = modes[current_mode].linewidth*4;
327 }
328
329 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
330 vid.colormap = (pixel_t *) host_colormap;
331 vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
332 vid.conrowbytes = vid.rowbytes;
333 vid.conwidth = vid.width;
334 vid.conheight = vid.height;
335 vid.numpages = 1;
336
337 vid.maxwarpwidth = WARP_WIDTH;
338 vid.maxwarpheight = WARP_HEIGHT;
339
340 /* alloc zbuffer and surface cache */
341 if (d_pzbuffer)
342 {
343 D_FlushCaches();
344 Hunk_FreeToHighMark (VID_highhunkmark);
345 d_pzbuffer = NULL;
346 vid_surfcache = NULL;
347 }
348
349 bsize = vid.rowbytes * vid.height;
350 tsize = D_SurfaceCacheForRes (vid.width, vid.height);
351 zsize = vid.width * vid.height * sizeof(*d_pzbuffer);
352
353 VID_highhunkmark = Hunk_HighMark ();
354
355 d_pzbuffer = (short *) Hunk_HighAllocName (bsize + tsize + zsize, "video");
356
357 vid_surfcache = ((byte *)d_pzbuffer) + zsize;
358
359 vid.conbuffer = vid.buffer = (pixel_t *)(((byte *)d_pzbuffer) + zsize + tsize);
360
361 D_InitCaches (vid_surfcache, tsize);
362
363 /* get goin' */
364 if (vga_setmode(current_mode) != 0)
365 Sys_Error("Unable to set mode %d", current_mode);
366
367 /*if (vga_setlinearaddressing() > 0)*/
368 VGA_pagebase = vid.direct = framebuffer_ptr = (byte *) vga_getgraphmem();
369 if (!framebuffer_ptr)
370 Sys_Error("Unable to get framebuffer ptr for mode %d", current_mode);
371
372 VID_SetPalette(palette);
373 vid.recalc_refdef = 1; /* force a surface cache flush */
374
375 vga_setpage(0);
376 svgalib_inited = 1;
377
378 return true;
379 }
380
381 /* backgrounding fixes (quakeforge) */
goto_background(void)382 static void goto_background (void)
383 {
384 svgalib_backgrounded = 1;
385 }
386
comefrom_background(void)387 static void comefrom_background (void)
388 {
389 svgalib_backgrounded = 0;
390 VID_SetPalette(vid_current_palette);
391 }
392
VID_Init(const unsigned char * palette)393 void VID_Init (const unsigned char *palette)
394 {
395 int i, w, h, d;
396 char *modename;
397
398 if (svgalib_inited)
399 return;
400
401 if (vga_init() != 0)
402 Sys_Error ("SVGALib failed to allocate a new VC");
403
404 i = vga_runinbackground_version();
405 if (i > 0)
406 {
407 Sys_Printf ("SVGALIB background support %i detected\n", i);
408 vga_runinbackground (VGA_GOTOBACK, goto_background);
409 vga_runinbackground (VGA_COMEFROMBACK, comefrom_background);
410 vga_runinbackground (1);
411 }
412 else
413 {
414 vga_runinbackground (0);
415 }
416
417 VID_InitModes();
418 if (!VID_NumModes())
419 Sys_Error ("No supported modes reported by SVGAlib");
420
421 // Cvar_RegisterVariable (&_enable_mouse);
422 Cvar_RegisterVariable (&vid_mode);
423 Cvar_RegisterVariable (&vid_redrawfull);
424 Cvar_RegisterVariable (&vid_waitforrefresh);
425
426 // Cmd_AddCommand ("gamma", VID_Gamma_f);
427 Cmd_AddCommand("vid_nummodes", VID_NumModes_f);
428 Cmd_AddCommand("vid_describemode", VID_DescribeMode_f);
429 Cmd_AddCommand("vid_describemodes", VID_DescribeModes_f);
430 Cmd_AddCommand("vid_debug", VID_Debug_f);
431
432 /* interpret command-line params */
433 current_mode = -1;
434 w = h = d = 0;
435 modename = getenv("GSVGAMODE");
436 if (modename)
437 current_mode = get_mode(modename, 0, 0, 0);
438 else
439 {
440 i = COM_CheckParm("-mode");
441 if (i)
442 {
443 if (i >= com_argc - 1)
444 Sys_Error("%s: -mode <modename>", __thisfunc__);
445 current_mode = get_mode(com_argv[i + 1], 0, 0, 0);
446 }
447 }
448 if (current_mode == -1)
449 {
450 i = COM_CheckParm("-width");
451 if (i)
452 {
453 if (i >= com_argc - 1)
454 Sys_Error("%s: -width <width>", __thisfunc__);
455 w = atoi(com_argv[i + 1]);
456 }
457 i = COM_CheckParm("-height");
458 if (i)
459 {
460 if (i >= com_argc - 1)
461 Sys_Error("%s: -height <height>", __thisfunc__);
462 h = atoi(com_argv[i + 1]);
463 }
464 i = COM_CheckParm("-bpp");
465 if (i)
466 {
467 if (i >= com_argc - 1)
468 Sys_Error("%s: -bpp <depth>", __thisfunc__);
469 d = atoi(com_argv[i + 1]);
470 }
471 if (w || h || d)
472 current_mode = get_mode(NULL, w, h, d);
473 }
474 if (current_mode == -1)
475 {
476 if (vga_hasmode(G320x200x256))
477 current_mode = G320x200x256;
478 else
479 {
480 Sys_Printf ("Mode 13h not supported\n");
481 #if defined(G320x200x256V) /* svgalib-1.9 may do this */
482 if (vga_hasmode(G320x200x256V))
483 current_mode = G320x200x256V;
484 else
485 #endif
486 /* try the first available */
487 for (i = 0; i < num_modes; i++)
488 {
489 if (modes[i].width != 0)
490 {
491 current_mode = i;
492 break;
493 }
494 }
495 Sys_Printf ("Will try setting mode %d\n", current_mode);
496 }
497 }
498
499 /* set vid parameters */
500 if (!VID_SetMode (current_mode, palette))
501 Sys_Error ("Unable to set a video mode");
502
503 VID_SetPalette(palette);
504 }
505
VID_Update(vrect_t * rects)506 void VID_Update (vrect_t *rects)
507 {
508 if (!svgalib_inited)
509 return;
510 if (svgalib_backgrounded)
511 return;
512 if (!vga_oktowrite())
513 return; /* can't update screen if it's not active */
514
515 if (vid_waitforrefresh.integer)
516 vga_waitretrace();
517
518 if (VGA_planar)
519 {
520 /* VGA_UpdatePlanarScreen() of d_copy.asm only works with
521 * /dev/mem, i.e. without the svgalib_helper kernel module
522 * introduced in svgalib-1.9.x. must use an appropriate
523 * svgalib api function, such as vga_copytoplanar256() */
524 vga_copytoplanar256(vid.buffer, VGA_bufferrowbytes, 0, VGA_rowbytes, VGA_width, VGA_height);
525 }
526 else if (vid_redrawfull.integer)
527 {
528 int total = vid.rowbytes * vid.height;
529 int offset;
530
531 for (offset = 0; offset < total; offset += 0x10000)
532 {
533 vga_setpage(offset / 0x10000);
534 memcpy(framebuffer_ptr, vid.buffer + offset, ((total-offset > 0x10000) ? 0x10000 : (total - offset)));
535 }
536 }
537 else
538 {
539 int ycount;
540 int offset;
541 int vidpage = 0;
542
543 vga_setpage(0);
544
545 while (rects)
546 {
547 ycount = rects->height;
548 offset = rects->y * vid.rowbytes + rects->x;
549 while (ycount--)
550 {
551 register int i = offset % 0x10000;
552
553 if ((offset / 0x10000) != vidpage)
554 {
555 vidpage = offset / 0x10000;
556 vga_setpage(vidpage);
557 }
558 if (rects->width + i > 0x10000)
559 {
560 memcpy(framebuffer_ptr + i, vid.buffer + offset, 0x10000 - i);
561 vga_setpage(++vidpage);
562 memcpy(framebuffer_ptr, vid.buffer + offset + 0x10000 - i, rects->width - 0x10000 + i);
563 }
564 else
565 memcpy(framebuffer_ptr + i, vid.buffer + offset, rects->width);
566
567 offset += vid.rowbytes;
568 }
569
570 rects = rects->pnext;
571 }
572 }
573
574 if (vid_mode.integer != current_mode)
575 VID_SetMode (vid_mode.integer, vid_current_palette);
576 }
577
578
579 /*
580 ================
581 D_BeginDirectRect
582 ================
583 */
D_BeginDirectRect(int x,int y,byte * pbitmap,int width,int height)584 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
585 {
586 int i, j, reps, repshift, offset, vidpage, off;
587
588 if (!svgalib_inited || !vid.direct || !vga_oktowrite())
589 return;
590 if (svgalib_backgrounded)
591 return;
592
593 if (vid.aspect > 1.5)
594 {
595 reps = 2;
596 repshift = 1;
597 }
598 else
599 {
600 reps = 1;
601 repshift = 0;
602 }
603
604 vidpage = 0;
605 vga_setpage(0);
606
607 if (VGA_planar)
608 {
609 #if 0
610 /* with svgalib-1.9.x, we can't use outb() directly because
611 * it only works for /dev/mem, i.e. without svgalib_helper
612 * kernel module. the equivalent __svgalib_port_out () is
613 * an internal helper function of svgalib (libvga.h) and is
614 * not officially exported. therefore disabling this part
615 * which isn't an important functionality at all. */
616 int k, plane;
617 for (plane = 0; plane < 4; plane++)
618 {
619 /* select the correct plane for reading and writing */
620 outb(0x02, 0x3C4);
621 outb(1 << plane, 0x3C5);
622 outb(4, 0x3CE);
623 outb(plane, 0x3CF);
624
625 for (i = 0; i < (height << repshift); i += reps)
626 {
627 for (k = 0; k < reps; k++)
628 {
629 for (j = 0; j < (width >> 2); j++)
630 {
631 backingbuf[(i + k) * 24 + (j << 2) + plane] =
632 vid.direct[(y + i + k) * VGA_rowbytes +
633 (x >> 2) + j];
634 vid.direct[(y + i + k) * VGA_rowbytes + (x>>2) + j] =
635 pbitmap[(i >> repshift) * 24 +
636 (j << 2) + plane];
637 }
638 }
639 }
640 }
641 #endif
642 }
643 else
644 {
645 for (i = 0; i < (height << repshift); i += reps)
646 {
647 for (j = 0; j < reps; j++)
648 {
649 offset = x + ((y << repshift) + i + j) * vid.rowbytes;
650 off = offset % 0x10000;
651 if ((offset / 0x10000) != vidpage)
652 {
653 vidpage = offset / 0x10000;
654 vga_setpage(vidpage);
655 }
656 memcpy (&backingbuf[(i + j) * 24], vid.direct + off, width);
657 memcpy (vid.direct + off, &pbitmap[(i >> repshift)*width], width);
658 }
659 }
660 }
661 }
662
663 /*
664 ================
665 D_EndDirectRect
666 ================
667 */
D_EndDirectRect(int x,int y,int width,int height)668 void D_EndDirectRect (int x, int y, int width, int height)
669 {
670 int i, j, reps, repshift, offset, vidpage, off;
671
672 if (!svgalib_inited || !vid.direct || !vga_oktowrite())
673 return;
674 if (svgalib_backgrounded)
675 return;
676
677 if (vid.aspect > 1.5)
678 {
679 reps = 2;
680 repshift = 1;
681 }
682 else
683 {
684 reps = 1;
685 repshift = 0;
686 }
687
688 vidpage = 0;
689 vga_setpage(0);
690
691 if (VGA_planar)
692 {
693 #if 0 /* see in D_BeginDirectRect() */
694 int k, plane;
695 for (plane = 0; plane < 4; plane++)
696 {
697 /* select the correct plane for writing */
698 outb(2, 0x3C4);
699 outb(1 << plane, 0x3C5);
700 outb(4, 0x3CE);
701 outb(plane, 0x3CF);
702
703 for (i = 0; i < (height << repshift); i += reps)
704 {
705 for (k = 0; k < reps; k++)
706 {
707 for (j = 0; j < (width >> 2); j++)
708 {
709 vid.direct[(y + i + k) * VGA_rowbytes + (x>>2) + j] =
710 backingbuf[(i + k) * 24 + (j << 2) + plane];
711 }
712 }
713 }
714 }
715 #endif
716 }
717 else
718 {
719 for (i = 0; i < (height << repshift); i += reps)
720 {
721 for (j = 0; j < reps; j++)
722 {
723 offset = x + ((y << repshift) + i + j) * vid.rowbytes;
724 off = offset % 0x10000;
725 if ((offset / 0x10000) != vidpage)
726 {
727 vidpage = offset / 0x10000;
728 vga_setpage(vidpage);
729 }
730 memcpy (vid.direct + off, &backingbuf[(i +j)*24], width);
731 }
732 }
733 }
734 }
735
D_ShowLoadingSize(void)736 void D_ShowLoadingSize (void)
737 {
738 #if defined(DRAW_PROGRESSBARS)
739 /* to be implemented. */
740 #endif /* !DRAW_PROGRESSBARS */
741 }
742
VID_LockBuffer(void)743 void VID_LockBuffer (void)
744 {
745 /* nothing to do */
746 }
747
VID_UnlockBuffer(void)748 void VID_UnlockBuffer (void)
749 {
750 /* nothing to do */
751 }
752
753
VID_HandlePause(qboolean paused)754 void VID_HandlePause (qboolean paused)
755 {
756 if (paused) IN_DeactivateMouse ();
757 else IN_ActivateMouse ();
758 }
759
760