vid_svgalib.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <termios.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/stat.h>
  18. #include <sys/vt.h>
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <asm/io.h>
  23. #include "vga.h"
  24. #include "vgakeyboard.h"
  25. #include "vgamouse.h"
  26. #include "quakedef.h"
  27. #include "d_local.h"
  28. #define stringify(m) { #m, m }
  29. unsigned short d_8to16table[256];
  30. static byte *vid_surfcache;
  31. static int VID_highhunkmark;
  32. int num_modes;
  33. vga_modeinfo *modes;
  34. int current_mode;
  35. int num_shades=32;
  36. struct
  37. {
  38. char *name;
  39. int num;
  40. } mice[] =
  41. {
  42. stringify(MOUSE_MICROSOFT),
  43. stringify(MOUSE_MOUSESYSTEMS),
  44. stringify(MOUSE_MMSERIES),
  45. stringify(MOUSE_LOGITECH),
  46. stringify(MOUSE_BUSMOUSE),
  47. stringify(MOUSE_PS2),
  48. };
  49. static unsigned char scantokey[128];
  50. static byte vid_current_palette[768];
  51. int num_mice = sizeof (mice) / sizeof(mice[0]);
  52. int d_con_indirect = 0;
  53. int svgalib_inited=0;
  54. int UseMouse = 1;
  55. int UseDisplay = 1;
  56. int UseKeyboard = 1;
  57. int mouserate = MOUSE_DEFAULTSAMPLERATE;
  58. cvar_t vid_mode = {"vid_mode","5",false};
  59. cvar_t vid_redrawfull = {"vid_redrawfull","0",false};
  60. cvar_t vid_waitforrefresh = {"vid_waitforrefresh","0",true};
  61. char *framebuffer_ptr;
  62. cvar_t mouse_button_commands[3] =
  63. {
  64. {"mouse1","+attack"},
  65. {"mouse2","+strafe"},
  66. {"mouse3","+forward"},
  67. };
  68. int mouse_buttons;
  69. int mouse_buttonstate;
  70. int mouse_oldbuttonstate;
  71. float mouse_x, mouse_y;
  72. float old_mouse_x, old_mouse_y;
  73. int mx, my;
  74. cvar_t m_filter = {"m_filter","0"};
  75. static byte backingbuf[48*24];
  76. int VGA_width, VGA_height, VGA_rowbytes, VGA_bufferrowbytes, VGA_planar;
  77. byte *VGA_pagebase;
  78. void VGA_UpdatePlanarScreen (void *srcbuffer);
  79. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  80. {
  81. int i, j, k, plane, reps, repshift, offset, vidpage, off;
  82. if (!svgalib_inited || !vid.direct || !vga_oktowrite()) return;
  83. if (vid.aspect > 1.5)
  84. {
  85. reps = 2;
  86. repshift = 1;
  87. } else {
  88. reps = 1;
  89. repshift = 0;
  90. }
  91. vidpage = 0;
  92. vga_setpage(0);
  93. if (VGA_planar)
  94. {
  95. for (plane=0 ; plane<4 ; plane++)
  96. {
  97. // select the correct plane for reading and writing
  98. outb(0x02, 0x3C4);
  99. outb(1 << plane, 0x3C5);
  100. outb(4, 0x3CE);
  101. outb(plane, 0x3CF);
  102. for (i=0 ; i<(height << repshift) ; i += reps)
  103. {
  104. for (k=0 ; k<reps ; k++)
  105. {
  106. for (j=0 ; j<(width >> 2) ; j++)
  107. {
  108. backingbuf[(i + k) * 24 + (j << 2) + plane] =
  109. vid.direct[(y + i + k) * VGA_rowbytes +
  110. (x >> 2) + j];
  111. vid.direct[(y + i + k) * VGA_rowbytes + (x>>2) + j] =
  112. pbitmap[(i >> repshift) * 24 +
  113. (j << 2) + plane];
  114. }
  115. }
  116. }
  117. }
  118. } else {
  119. for (i=0 ; i<(height << repshift) ; i += reps)
  120. {
  121. for (j=0 ; j<reps ; j++)
  122. {
  123. offset = x + ((y << repshift) + i + j) * vid.rowbytes;
  124. off = offset % 0x10000;
  125. if ((offset / 0x10000) != vidpage) {
  126. vidpage=offset / 0x10000;
  127. vga_setpage(vidpage);
  128. }
  129. memcpy (&backingbuf[(i + j) * 24],
  130. vid.direct + off, width);
  131. memcpy (vid.direct + off,
  132. &pbitmap[(i >> repshift)*width], width);
  133. }
  134. }
  135. }
  136. }
  137. void D_EndDirectRect (int x, int y, int width, int height)
  138. {
  139. int i, j, k, plane, reps, repshift, offset, vidpage, off;
  140. if (!svgalib_inited || !vid.direct || !vga_oktowrite()) return;
  141. if (vid.aspect > 1.5)
  142. {
  143. reps = 2;
  144. repshift = 1;
  145. } else {
  146. reps = 1;
  147. repshift = 0;
  148. }
  149. vidpage = 0;
  150. vga_setpage(0);
  151. if (VGA_planar)
  152. {
  153. for (plane=0 ; plane<4 ; plane++)
  154. {
  155. // select the correct plane for writing
  156. outb(2, 0x3C4);
  157. outb(1 << plane, 0x3C5);
  158. outb(4, 0x3CE);
  159. outb(plane, 0x3CF);
  160. for (i=0 ; i<(height << repshift) ; i += reps)
  161. {
  162. for (k=0 ; k<reps ; k++)
  163. {
  164. for (j=0 ; j<(width >> 2) ; j++)
  165. {
  166. vid.direct[(y + i + k) * VGA_rowbytes + (x>>2) + j] =
  167. backingbuf[(i + k) * 24 + (j << 2) + plane];
  168. }
  169. }
  170. }
  171. }
  172. } else {
  173. for (i=0 ; i<(height << repshift) ; i += reps)
  174. {
  175. for (j=0 ; j<reps ; j++)
  176. {
  177. offset = x + ((y << repshift) + i + j) * vid.rowbytes;
  178. off = offset % 0x10000;
  179. if ((offset / 0x10000) != vidpage) {
  180. vidpage=offset / 0x10000;
  181. vga_setpage(vidpage);
  182. }
  183. memcpy (vid.direct + off,
  184. &backingbuf[(i +j)*24],
  185. width);
  186. }
  187. }
  188. }
  189. }
  190. /*
  191. =================
  192. VID_Gamma_f
  193. Keybinding command
  194. =================
  195. */
  196. void VID_Gamma_f (void)
  197. {
  198. float gamma, f, inf;
  199. unsigned char palette[768];
  200. int i;
  201. if (Cmd_Argc () == 2)
  202. {
  203. gamma = Q_atof (Cmd_Argv(1));
  204. for (i=0 ; i<768 ; i++)
  205. {
  206. f = pow ( (host_basepal[i]+1)/256.0 , gamma );
  207. inf = f*255 + 0.5;
  208. if (inf < 0)
  209. inf = 0;
  210. if (inf > 255)
  211. inf = 255;
  212. palette[i] = inf;
  213. }
  214. VID_SetPalette (palette);
  215. vid.recalc_refdef = 1; // force a surface cache flush
  216. }
  217. }
  218. void VID_DescribeMode_f (void)
  219. {
  220. int modenum;
  221. modenum = Q_atoi (Cmd_Argv(1));
  222. if ((modenum >= num_modes) || (modenum < 0 ) || !modes[modenum].width)
  223. Con_Printf("Invalid video mode: %d!\n",modenum);
  224. Con_Printf("%d: %d x %d - ",modenum,modes[modenum].width,modes[modenum].height);
  225. if (modes[modenum].bytesperpixel == 0)
  226. Con_Printf("ModeX\n");
  227. else
  228. Con_Printf("%d bpp\n", modes[modenum].bytesperpixel<<3);
  229. }
  230. void VID_DescribeModes_f (void)
  231. {
  232. int i;
  233. for (i=0;i<num_modes;i++)
  234. if (modes[i].width) {
  235. Con_Printf("%d: %d x %d - ", i, modes[i].width,modes[i].height);
  236. if (modes[i].bytesperpixel == 0)
  237. Con_Printf("ModeX\n");
  238. else
  239. Con_Printf("%d bpp\n", modes[i].bytesperpixel<<3);
  240. }
  241. }
  242. /*
  243. ================
  244. VID_NumModes
  245. ================
  246. */
  247. int VID_NumModes ()
  248. {
  249. int i,i1=0;
  250. for (i=0;i<num_modes;i++)
  251. i1+=(modes[i].width?1:0);
  252. return (i1);
  253. }
  254. void VID_NumModes_f (void)
  255. {
  256. Con_Printf("%d modes\n",VID_NumModes());
  257. }
  258. void VID_Debug_f (void)
  259. {
  260. Con_Printf("mode: %d\n",current_mode);
  261. Con_Printf("height x width: %d x %d\n",vid.height,vid.width);
  262. Con_Printf("bpp: %d\n",modes[current_mode].bytesperpixel*8);
  263. Con_Printf("vid.aspect: %f\n",vid.aspect);
  264. }
  265. void VID_InitModes(void)
  266. {
  267. int i;
  268. // get complete information on all modes
  269. num_modes = vga_lastmodenumber()+1;
  270. modes = Z_Malloc(num_modes * sizeof(vga_modeinfo));
  271. for (i=0 ; i<num_modes ; i++)
  272. {
  273. if (vga_hasmode(i))
  274. Q_memcpy(&modes[i], vga_getmodeinfo(i), sizeof (vga_modeinfo));
  275. else
  276. modes[i].width = 0; // means not available
  277. }
  278. // filter for modes i don't support
  279. for (i=0 ; i<num_modes ; i++)
  280. {
  281. if (modes[i].bytesperpixel != 1 && modes[i].colors != 256)
  282. modes[i].width = 0;
  283. }
  284. }
  285. int get_mode(char *name, int width, int height, int depth)
  286. {
  287. int i;
  288. int ok, match;
  289. match = (!!width) + (!!height)*2 + (!!depth)*4;
  290. if (name)
  291. {
  292. i = vga_getmodenumber(name);
  293. if (!modes[i].width)
  294. {
  295. Sys_Printf("Mode [%s] not supported\n", name);
  296. i = G320x200x256;
  297. }
  298. }
  299. else
  300. {
  301. for (i=0 ; i<num_modes ; i++)
  302. if (modes[i].width)
  303. {
  304. ok = (modes[i].width == width)
  305. + (modes[i].height == height)*2
  306. + (modes[i].bytesperpixel == depth/8)*4;
  307. if ((ok & match) == ok)
  308. break;
  309. }
  310. if (i==num_modes)
  311. {
  312. Sys_Printf("Mode %dx%d (%d bits) not supported\n",
  313. width, height, depth);
  314. i = G320x200x256;
  315. }
  316. }
  317. return i;
  318. }
  319. int matchmouse(int mouse, char *name)
  320. {
  321. int i;
  322. for (i=0 ; i<num_mice ; i++)
  323. if (!strcmp(mice[i].name, name))
  324. return i;
  325. return mouse;
  326. }
  327. #if 0
  328. void vtswitch(int newconsole)
  329. {
  330. int fd;
  331. struct vt_stat x;
  332. // switch consoles and wait until reactivated
  333. fd = open("/dev/console", O_RDONLY);
  334. ioctl(fd, VT_GETSTATE, &x);
  335. ioctl(fd, VT_ACTIVATE, newconsole);
  336. ioctl(fd, VT_WAITACTIVE, x.v_active);
  337. close(fd);
  338. }
  339. #endif
  340. void keyhandler(int scancode, int state)
  341. {
  342. int sc;
  343. sc = scancode & 0x7f;
  344. // Con_Printf("scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
  345. Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
  346. }
  347. void VID_Shutdown(void)
  348. {
  349. if (!svgalib_inited) return;
  350. // printf("shutdown graphics called\n");
  351. if (UseKeyboard)
  352. keyboard_close();
  353. if (UseDisplay)
  354. vga_setmode(TEXT);
  355. // printf("shutdown graphics finished\n");
  356. svgalib_inited = 0;
  357. }
  358. void VID_ShiftPalette(unsigned char *p)
  359. {
  360. VID_SetPalette(p);
  361. }
  362. void VID_SetPalette(byte *palette)
  363. {
  364. static int tmppal[256*3];
  365. int *tp;
  366. int i;
  367. if (!svgalib_inited)
  368. return;
  369. memcpy(vid_current_palette, palette, sizeof(vid_current_palette));
  370. if (vga_getcolors() == 256)
  371. {
  372. tp = tmppal;
  373. for (i=256*3 ; i ; i--)
  374. *(tp++) = *(palette++) >> 2;
  375. if (UseDisplay && vga_oktowrite())
  376. vga_setpalvec(0, 256, tmppal);
  377. }
  378. }
  379. int VID_SetMode (int modenum, unsigned char *palette)
  380. {
  381. int bsize, zsize, tsize;
  382. if ((modenum >= num_modes) || (modenum < 0) || !modes[modenum].width)
  383. {
  384. Cvar_SetValue ("vid_mode", (float)current_mode);
  385. Con_Printf("No such video mode: %d\n",modenum);
  386. return 0;
  387. }
  388. Cvar_SetValue ("vid_mode", (float)modenum);
  389. current_mode=modenum;
  390. vid.width = modes[current_mode].width;
  391. vid.height = modes[current_mode].height;
  392. VGA_width = modes[current_mode].width;
  393. VGA_height = modes[current_mode].height;
  394. VGA_planar = modes[current_mode].bytesperpixel == 0;
  395. VGA_rowbytes = modes[current_mode].linewidth;
  396. vid.rowbytes = modes[current_mode].linewidth;
  397. if (VGA_planar) {
  398. VGA_bufferrowbytes = modes[current_mode].linewidth * 4;
  399. vid.rowbytes = modes[current_mode].linewidth*4;
  400. }
  401. vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
  402. vid.colormap = (pixel_t *) host_colormap;
  403. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  404. vid.conrowbytes = vid.rowbytes;
  405. vid.conwidth = vid.width;
  406. vid.conheight = vid.height;
  407. vid.numpages = 1;
  408. vid.maxwarpwidth = WARP_WIDTH;
  409. vid.maxwarpheight = WARP_HEIGHT;
  410. // alloc zbuffer and surface cache
  411. if (d_pzbuffer) {
  412. D_FlushCaches();
  413. Hunk_FreeToHighMark (VID_highhunkmark);
  414. d_pzbuffer = NULL;
  415. vid_surfcache = NULL;
  416. }
  417. bsize = vid.rowbytes * vid.height;
  418. tsize = D_SurfaceCacheForRes (vid.width, vid.height);
  419. zsize = vid.width * vid.height * sizeof(*d_pzbuffer);
  420. VID_highhunkmark = Hunk_HighMark ();
  421. d_pzbuffer = Hunk_HighAllocName (bsize+tsize+zsize, "video");
  422. vid_surfcache = ((byte *)d_pzbuffer) + zsize;
  423. vid.conbuffer = vid.buffer = (pixel_t *)(((byte *)d_pzbuffer) + zsize + tsize);
  424. D_InitCaches (vid_surfcache, tsize);
  425. // get goin'
  426. vga_setmode(current_mode);
  427. VID_SetPalette(palette);
  428. VGA_pagebase = vid.direct = framebuffer_ptr = (char *) vga_getgraphmem();
  429. // if (vga_setlinearaddressing()>0)
  430. // framebuffer_ptr = (char *) vga_getgraphmem();
  431. if (!framebuffer_ptr)
  432. Sys_Error("This mode isn't hapnin'\n");
  433. vga_setpage(0);
  434. svgalib_inited=1;
  435. vid.recalc_refdef = 1; // force a surface cache flush
  436. return 0;
  437. }
  438. void VID_Init(unsigned char *palette)
  439. {
  440. int i;
  441. int w, h, d;
  442. if (svgalib_inited)
  443. return;
  444. // Cmd_AddCommand ("gamma", VID_Gamma_f);
  445. if (UseDisplay)
  446. {
  447. vga_init();
  448. VID_InitModes();
  449. Cvar_RegisterVariable (&vid_mode);
  450. Cvar_RegisterVariable (&vid_redrawfull);
  451. Cvar_RegisterVariable (&vid_waitforrefresh);
  452. Cmd_AddCommand("vid_nummodes", VID_NumModes_f);
  453. Cmd_AddCommand("vid_describemode", VID_DescribeMode_f);
  454. Cmd_AddCommand("vid_describemodes", VID_DescribeModes_f);
  455. Cmd_AddCommand("vid_debug", VID_Debug_f);
  456. // interpret command-line params
  457. w = h = d = 0;
  458. if (getenv("GSVGAMODE"))
  459. current_mode = get_mode(getenv("GSVGAMODE"), w, h, d);
  460. else if (COM_CheckParm("-mode"))
  461. current_mode = get_mode(com_argv[COM_CheckParm("-mode")+1], w, h, d);
  462. else if (COM_CheckParm("-w") || COM_CheckParm("-h")
  463. || COM_CheckParm("-d"))
  464. {
  465. if (COM_CheckParm("-w"))
  466. w = Q_atoi(com_argv[COM_CheckParm("-w")+1]);
  467. if (COM_CheckParm("-h"))
  468. h = Q_atoi(com_argv[COM_CheckParm("-h")+1]);
  469. if (COM_CheckParm("-d"))
  470. d = Q_atoi(com_argv[COM_CheckParm("-d")+1]);
  471. current_mode = get_mode(0, w, h, d);
  472. }
  473. else
  474. current_mode = G320x200x256;
  475. // set vid parameters
  476. VID_SetMode(current_mode, palette);
  477. VID_SetPalette(palette);
  478. // we do want to run in the background when switched away
  479. vga_runinbackground(1);
  480. }
  481. if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
  482. if (UseKeyboard)
  483. {
  484. for (i=0 ; i<128 ; i++)
  485. scantokey[i] = ' ';
  486. scantokey[42] = K_SHIFT;
  487. scantokey[54] = K_SHIFT;
  488. scantokey[72] = K_UPARROW;
  489. scantokey[103] = K_UPARROW;
  490. scantokey[80] = K_DOWNARROW;
  491. scantokey[108] = K_DOWNARROW;
  492. scantokey[75] = K_LEFTARROW;
  493. scantokey[105] = K_LEFTARROW;
  494. scantokey[77] = K_RIGHTARROW;
  495. scantokey[106] = K_RIGHTARROW;
  496. scantokey[29] = K_CTRL;
  497. scantokey[97] = K_CTRL;
  498. scantokey[56] = K_ALT;
  499. scantokey[100] = K_ALT;
  500. // scantokey[58] = JK_CAPS;
  501. // scantokey[69] = JK_NUM_LOCK;
  502. scantokey[71] = K_HOME;
  503. scantokey[73] = K_PGUP;
  504. scantokey[79] = K_END;
  505. scantokey[81] = K_PGDN;
  506. scantokey[82] = K_INS;
  507. scantokey[83] = K_DEL;
  508. scantokey[1 ] = K_ESCAPE;
  509. scantokey[28] = K_ENTER;
  510. scantokey[15] = K_TAB;
  511. scantokey[14] = K_BACKSPACE;
  512. scantokey[119] = K_PAUSE;
  513. scantokey[57] = ' ';
  514. scantokey[102] = K_HOME;
  515. scantokey[104] = K_PGUP;
  516. scantokey[107] = K_END;
  517. scantokey[109] = K_PGDN;
  518. scantokey[110] = K_INS;
  519. scantokey[111] = K_DEL;
  520. scantokey[2] = '1';
  521. scantokey[3] = '2';
  522. scantokey[4] = '3';
  523. scantokey[5] = '4';
  524. scantokey[6] = '5';
  525. scantokey[7] = '6';
  526. scantokey[8] = '7';
  527. scantokey[9] = '8';
  528. scantokey[10] = '9';
  529. scantokey[11] = '0';
  530. scantokey[12] = '-';
  531. scantokey[13] = '=';
  532. scantokey[41] = '`';
  533. scantokey[26] = '[';
  534. scantokey[27] = ']';
  535. scantokey[39] = ';';
  536. scantokey[40] = '\'';
  537. scantokey[51] = ',';
  538. scantokey[52] = '.';
  539. scantokey[53] = '/';
  540. scantokey[43] = '\\';
  541. scantokey[59] = K_F1;
  542. scantokey[60] = K_F2;
  543. scantokey[61] = K_F3;
  544. scantokey[62] = K_F4;
  545. scantokey[63] = K_F5;
  546. scantokey[64] = K_F6;
  547. scantokey[65] = K_F7;
  548. scantokey[66] = K_F8;
  549. scantokey[67] = K_F9;
  550. scantokey[68] = K_F10;
  551. scantokey[87] = K_F11;
  552. scantokey[88] = K_F12;
  553. scantokey[30] = 'a';
  554. scantokey[48] = 'b';
  555. scantokey[46] = 'c';
  556. scantokey[32] = 'd';
  557. scantokey[18] = 'e';
  558. scantokey[33] = 'f';
  559. scantokey[34] = 'g';
  560. scantokey[35] = 'h';
  561. scantokey[23] = 'i';
  562. scantokey[36] = 'j';
  563. scantokey[37] = 'k';
  564. scantokey[38] = 'l';
  565. scantokey[50] = 'm';
  566. scantokey[49] = 'n';
  567. scantokey[24] = 'o';
  568. scantokey[25] = 'p';
  569. scantokey[16] = 'q';
  570. scantokey[19] = 'r';
  571. scantokey[31] = 's';
  572. scantokey[20] = 't';
  573. scantokey[22] = 'u';
  574. scantokey[47] = 'v';
  575. scantokey[17] = 'w';
  576. scantokey[45] = 'x';
  577. scantokey[21] = 'y';
  578. scantokey[44] = 'z';
  579. if (keyboard_init())
  580. Sys_Error("keyboard_init() failed");
  581. keyboard_seteventhandler(keyhandler);
  582. }
  583. }
  584. void VID_Update(vrect_t *rects)
  585. {
  586. if (!svgalib_inited)
  587. return;
  588. if (!vga_oktowrite())
  589. return; // can't update screen if it's not active
  590. if (vid_waitforrefresh.value)
  591. vga_waitretrace();
  592. if (VGA_planar)
  593. VGA_UpdatePlanarScreen (vid.buffer);
  594. else if (vid_redrawfull.value) {
  595. int total = vid.rowbytes * vid.height;
  596. int offset;
  597. for (offset=0;offset<total;offset+=0x10000) {
  598. vga_setpage(offset/0x10000);
  599. memcpy(framebuffer_ptr,
  600. vid.buffer + offset,
  601. ((total-offset>0x10000)?0x10000:(total-offset)));
  602. }
  603. } else {
  604. int ycount;
  605. int offset;
  606. int vidpage=0;
  607. vga_setpage(0);
  608. while (rects)
  609. {
  610. ycount = rects->height;
  611. offset = rects->y * vid.rowbytes + rects->x;
  612. while (ycount--)
  613. {
  614. register int i = offset % 0x10000;
  615. if ((offset / 0x10000) != vidpage) {
  616. vidpage=offset / 0x10000;
  617. vga_setpage(vidpage);
  618. }
  619. if (rects->width + i > 0x10000) {
  620. memcpy(framebuffer_ptr + i,
  621. vid.buffer + offset,
  622. 0x10000 - i);
  623. vga_setpage(++vidpage);
  624. memcpy(framebuffer_ptr,
  625. vid.buffer + offset + 0x10000 - i,
  626. rects->width - 0x10000 + i);
  627. } else
  628. memcpy(framebuffer_ptr + i,
  629. vid.buffer + offset,
  630. rects->width);
  631. offset += vid.rowbytes;
  632. }
  633. rects = rects->pnext;
  634. }
  635. }
  636. if (vid_mode.value != current_mode)
  637. VID_SetMode ((int)vid_mode.value, vid_current_palette);
  638. }
  639. static int dither;
  640. void VID_DitherOn(void)
  641. {
  642. if (dither == 0)
  643. {
  644. // R_ViewChanged (&vrect, sb_lines, vid.aspect);
  645. dither = 1;
  646. }
  647. }
  648. void VID_DitherOff(void)
  649. {
  650. if (dither)
  651. {
  652. // R_ViewChanged (&vrect, sb_lines, vid.aspect);
  653. dither = 0;
  654. }
  655. }
  656. void Sys_SendKeyEvents(void)
  657. {
  658. if (!svgalib_inited)
  659. return;
  660. if (UseKeyboard)
  661. while (keyboard_update());
  662. }
  663. void Force_CenterView_f (void)
  664. {
  665. cl.viewangles[PITCH] = 0;
  666. }
  667. void mousehandler(int buttonstate, int dx, int dy)
  668. {
  669. mouse_buttonstate = buttonstate;
  670. mx += dx;
  671. my += dy;
  672. }
  673. void IN_Init(void)
  674. {
  675. int mtype;
  676. char *mousedev;
  677. int mouserate;
  678. if (UseMouse)
  679. {
  680. Cvar_RegisterVariable (&mouse_button_commands[0]);
  681. Cvar_RegisterVariable (&mouse_button_commands[1]);
  682. Cvar_RegisterVariable (&mouse_button_commands[2]);
  683. Cvar_RegisterVariable (&m_filter);
  684. Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  685. mouse_buttons = 3;
  686. mtype = vga_getmousetype();
  687. mousedev = "/dev/mouse";
  688. if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
  689. if (COM_CheckParm("-mdev"))
  690. mousedev = com_argv[COM_CheckParm("-mdev")+1];
  691. mouserate = 1200;
  692. if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
  693. if (COM_CheckParm("-mrate"))
  694. mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
  695. // printf("Mouse: dev=%s,type=%s,speed=%d\n",
  696. // mousedev, mice[mtype].name, mouserate);
  697. if (mouse_init(mousedev, mtype, mouserate))
  698. {
  699. Con_Printf("No mouse found\n");
  700. UseMouse = 0;
  701. }
  702. else
  703. mouse_seteventhandler(mousehandler);
  704. }
  705. }
  706. void IN_Shutdown(void)
  707. {
  708. if (UseMouse)
  709. mouse_close();
  710. }
  711. /*
  712. ===========
  713. IN_Commands
  714. ===========
  715. */
  716. void IN_Commands (void)
  717. {
  718. if (UseMouse && cls.state != ca_dedicated)
  719. {
  720. // poll mouse values
  721. while (mouse_update())
  722. ;
  723. // perform button actions
  724. if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  725. !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  726. Key_Event (K_MOUSE1, true);
  727. else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  728. (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  729. Key_Event (K_MOUSE1, false);
  730. if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  731. !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  732. Key_Event (K_MOUSE2, true);
  733. else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  734. (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  735. Key_Event (K_MOUSE2, false);
  736. if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  737. !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  738. Key_Event (K_MOUSE3, true);
  739. else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  740. (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  741. Key_Event (K_MOUSE3, false);
  742. mouse_oldbuttonstate = mouse_buttonstate;
  743. }
  744. }
  745. /*
  746. ===========
  747. IN_Move
  748. ===========
  749. */
  750. void IN_MouseMove (usercmd_t *cmd)
  751. {
  752. if (!UseMouse)
  753. return;
  754. // poll mouse values
  755. while (mouse_update())
  756. ;
  757. if (m_filter.value)
  758. {
  759. mouse_x = (mx + old_mouse_x) * 0.5;
  760. mouse_y = (my + old_mouse_y) * 0.5;
  761. }
  762. else
  763. {
  764. mouse_x = mx;
  765. mouse_y = my;
  766. }
  767. old_mouse_x = mx;
  768. old_mouse_y = my;
  769. mx = my = 0; // clear for next update
  770. mouse_x *= sensitivity.value;
  771. mouse_y *= sensitivity.value;
  772. // add mouse X/Y movement to cmd
  773. if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  774. cmd->sidemove += m_side.value * mouse_x;
  775. else
  776. cl.viewangles[YAW] -= m_yaw.value * mouse_x;
  777. if (in_mlook.state & 1)
  778. V_StopPitchDrift ();
  779. if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
  780. {
  781. cl.viewangles[PITCH] += m_pitch.value * mouse_y;
  782. if (cl.viewangles[PITCH] > 80)
  783. cl.viewangles[PITCH] = 80;
  784. if (cl.viewangles[PITCH] < -70)
  785. cl.viewangles[PITCH] = -70;
  786. }
  787. else
  788. {
  789. if ((in_strafe.state & 1) && noclip_anglehack)
  790. cmd->upmove -= m_forward.value * mouse_y;
  791. else
  792. cmd->forwardmove -= m_forward.value * mouse_y;
  793. }
  794. }
  795. void IN_Move (usercmd_t *cmd)
  796. {
  797. IN_MouseMove(cmd);
  798. }
  799. /*
  800. ================
  801. VID_ModeInfo
  802. ================
  803. */
  804. char *VID_ModeInfo (int modenum)
  805. {
  806. static char *badmodestr = "Bad mode number";
  807. static char modestr[40];
  808. if (modenum == 0)
  809. {
  810. sprintf (modestr, "%d x %d, %d bpp",
  811. vid.width, vid.height, modes[current_mode].bytesperpixel*8);
  812. return (modestr);
  813. }
  814. else
  815. {
  816. return (badmodestr);
  817. }
  818. }