vid_svgalib.c 21 KB

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