gl_vidlinux.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 "GL/fxmesa.h"
  28. #define WARP_WIDTH 320
  29. #define WARP_HEIGHT 200
  30. static fxMesaContext fc = NULL;
  31. #define stringify(m) { #m, m }
  32. unsigned short d_8to16table[256];
  33. unsigned d_8to24table[256];
  34. unsigned char d_15to8table[65536];
  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. int num_mice = sizeof (mice) / sizeof(mice[0]);
  51. int d_con_indirect = 0;
  52. int svgalib_inited=0;
  53. int UseMouse = 1;
  54. int UseKeyboard = 1;
  55. int mouserate = MOUSE_DEFAULTSAMPLERATE;
  56. cvar_t _windowed_mouse = {"_windowed_mouse","0", true};
  57. cvar_t vid_mode = {"vid_mode","5",false};
  58. cvar_t vid_redrawfull = {"vid_redrawfull","0",false};
  59. cvar_t vid_waitforrefresh = {"vid_waitforrefresh","0",true};
  60. char *framebuffer_ptr;
  61. cvar_t mouse_button_commands[3] =
  62. {
  63. {"mouse1","+attack"},
  64. {"mouse2","+strafe"},
  65. {"mouse3","+forward"},
  66. };
  67. int mouse_buttons;
  68. int mouse_buttonstate;
  69. int mouse_oldbuttonstate;
  70. float mouse_x, mouse_y;
  71. float old_mouse_x, old_mouse_y;
  72. int mx, my;
  73. cvar_t m_filter = {"m_filter","1"};
  74. int scr_width, scr_height;
  75. /*-----------------------------------------------------------------------*/
  76. //int texture_mode = GL_NEAREST;
  77. //int texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  78. //int texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  79. int texture_mode = GL_LINEAR;
  80. //int texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  81. //int texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  82. int texture_extension_number = 1;
  83. float gldepthmin, gldepthmax;
  84. cvar_t gl_ztrick = {"gl_ztrick","1"};
  85. const char *gl_vendor;
  86. const char *gl_renderer;
  87. const char *gl_version;
  88. const char *gl_extensions;
  89. qboolean is8bit = false;
  90. qboolean isPermedia = false;
  91. qboolean gl_mtexable = false;
  92. /*-----------------------------------------------------------------------*/
  93. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  94. {
  95. }
  96. void D_EndDirectRect (int x, int y, int width, int height)
  97. {
  98. }
  99. int matchmouse(int mouse, char *name)
  100. {
  101. int i;
  102. for (i=0 ; i<num_mice ; i++)
  103. if (!strcmp(mice[i].name, name))
  104. return i;
  105. return mouse;
  106. }
  107. #if 0
  108. void vtswitch(int newconsole)
  109. {
  110. int fd;
  111. struct vt_stat x;
  112. // switch consoles and wait until reactivated
  113. fd = open("/dev/console", O_RDONLY);
  114. ioctl(fd, VT_GETSTATE, &x);
  115. ioctl(fd, VT_ACTIVATE, newconsole);
  116. ioctl(fd, VT_WAITACTIVE, x.v_active);
  117. close(fd);
  118. }
  119. #endif
  120. void keyhandler(int scancode, int state)
  121. {
  122. int sc;
  123. sc = scancode & 0x7f;
  124. Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
  125. }
  126. void VID_Shutdown(void)
  127. {
  128. if (!fc)
  129. return;
  130. fxMesaDestroyContext(fc);
  131. if (UseKeyboard)
  132. keyboard_close();
  133. }
  134. void signal_handler(int sig)
  135. {
  136. printf("Received signal %d, exiting...\n", sig);
  137. Sys_Quit();
  138. exit(0);
  139. }
  140. void InitSig(void)
  141. {
  142. signal(SIGHUP, signal_handler);
  143. signal(SIGINT, signal_handler);
  144. signal(SIGQUIT, signal_handler);
  145. signal(SIGILL, signal_handler);
  146. signal(SIGTRAP, signal_handler);
  147. signal(SIGIOT, signal_handler);
  148. signal(SIGBUS, signal_handler);
  149. signal(SIGFPE, signal_handler);
  150. signal(SIGSEGV, signal_handler);
  151. signal(SIGTERM, signal_handler);
  152. }
  153. void VID_ShiftPalette(unsigned char *p)
  154. {
  155. // VID_SetPalette(p);
  156. }
  157. void VID_SetPalette (unsigned char *palette)
  158. {
  159. byte *pal;
  160. unsigned r,g,b;
  161. unsigned v;
  162. int r1,g1,b1;
  163. int k;
  164. unsigned short i;
  165. unsigned *table;
  166. FILE *f;
  167. char s[255];
  168. float dist, bestdist;
  169. static qboolean palflag = false;
  170. //
  171. // 8 8 8 encoding
  172. //
  173. Con_Printf("Converting 8to24\n");
  174. pal = palette;
  175. table = d_8to24table;
  176. for (i=0 ; i<256 ; i++)
  177. {
  178. r = pal[0];
  179. g = pal[1];
  180. b = pal[2];
  181. pal += 3;
  182. // v = (255<<24) + (r<<16) + (g<<8) + (b<<0);
  183. // v = (255<<0) + (r<<8) + (g<<16) + (b<<24);
  184. v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
  185. *table++ = v;
  186. }
  187. d_8to24table[255] &= 0xffffff; // 255 is transparent
  188. // JACK: 3D distance calcs - k is last closest, l is the distance.
  189. // FIXME: Precalculate this and cache to disk.
  190. if (palflag)
  191. return;
  192. palflag = true;
  193. COM_FOpenFile("glquake/15to8.pal", &f);
  194. if (f) {
  195. fread(d_15to8table, 1<<15, 1, f);
  196. fclose(f);
  197. } else {
  198. for (i=0; i < (1<<15); i++) {
  199. /* Maps
  200. 000000000000000
  201. 000000000011111 = Red = 0x1F
  202. 000001111100000 = Blue = 0x03E0
  203. 111110000000000 = Grn = 0x7C00
  204. */
  205. r = ((i & 0x1F) << 3)+4;
  206. g = ((i & 0x03E0) >> 2)+4;
  207. b = ((i & 0x7C00) >> 7)+4;
  208. pal = (unsigned char *)d_8to24table;
  209. for (v=0,k=0,bestdist=10000.0; v<256; v++,pal+=4) {
  210. r1 = (int)r - (int)pal[0];
  211. g1 = (int)g - (int)pal[1];
  212. b1 = (int)b - (int)pal[2];
  213. dist = sqrt(((r1*r1)+(g1*g1)+(b1*b1)));
  214. if (dist < bestdist) {
  215. k=v;
  216. bestdist = dist;
  217. }
  218. }
  219. d_15to8table[i]=k;
  220. }
  221. sprintf(s, "%s/glquake", com_gamedir);
  222. Sys_mkdir (s);
  223. sprintf(s, "%s/glquake/15to8.pal", com_gamedir);
  224. if ((f = fopen(s, "wb")) != NULL) {
  225. fwrite(d_15to8table, 1<<15, 1, f);
  226. fclose(f);
  227. }
  228. }
  229. }
  230. /*
  231. ===============
  232. GL_Init
  233. ===============
  234. */
  235. void GL_Init (void)
  236. {
  237. gl_vendor = glGetString (GL_VENDOR);
  238. Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
  239. gl_renderer = glGetString (GL_RENDERER);
  240. Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
  241. gl_version = glGetString (GL_VERSION);
  242. Con_Printf ("GL_VERSION: %s\n", gl_version);
  243. gl_extensions = glGetString (GL_EXTENSIONS);
  244. Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
  245. // Con_Printf ("%s %s\n", gl_renderer, gl_version);
  246. glClearColor (1,0,0,0);
  247. glCullFace(GL_FRONT);
  248. glEnable(GL_TEXTURE_2D);
  249. glEnable(GL_ALPHA_TEST);
  250. glAlphaFunc(GL_GREATER, 0.666);
  251. glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  252. glShadeModel (GL_FLAT);
  253. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  254. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  255. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  256. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  257. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  258. // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  259. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  260. }
  261. /*
  262. =================
  263. GL_BeginRendering
  264. =================
  265. */
  266. void GL_BeginRendering (int *x, int *y, int *width, int *height)
  267. {
  268. extern cvar_t gl_clear;
  269. *x = *y = 0;
  270. *width = scr_width;
  271. *height = scr_height;
  272. // if (!wglMakeCurrent( maindc, baseRC ))
  273. // Sys_Error ("wglMakeCurrent failed");
  274. // glViewport (*x, *y, *width, *height);
  275. }
  276. void GL_EndRendering (void)
  277. {
  278. glFlush();
  279. fxMesaSwapBuffers();
  280. }
  281. void Init_KBD(void)
  282. {
  283. int i;
  284. if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
  285. if (UseKeyboard)
  286. {
  287. for (i=0 ; i<128 ; i++)
  288. scantokey[i] = ' ';
  289. scantokey[42] = K_SHIFT;
  290. scantokey[54] = K_SHIFT;
  291. scantokey[72] = K_UPARROW;
  292. scantokey[103] = K_UPARROW;
  293. scantokey[80] = K_DOWNARROW;
  294. scantokey[108] = K_DOWNARROW;
  295. scantokey[75] = K_LEFTARROW;
  296. scantokey[105] = K_LEFTARROW;
  297. scantokey[77] = K_RIGHTARROW;
  298. scantokey[106] = K_RIGHTARROW;
  299. scantokey[29] = K_CTRL;
  300. scantokey[97] = K_CTRL;
  301. scantokey[56] = K_ALT;
  302. scantokey[100] = K_ALT;
  303. // scantokey[58] = JK_CAPS;
  304. // scantokey[69] = JK_NUM_LOCK;
  305. scantokey[71] = K_HOME;
  306. scantokey[73] = K_PGUP;
  307. scantokey[79] = K_END;
  308. scantokey[81] = K_PGDN;
  309. scantokey[82] = K_INS;
  310. scantokey[83] = K_DEL;
  311. scantokey[1 ] = K_ESCAPE;
  312. scantokey[28] = K_ENTER;
  313. scantokey[15] = K_TAB;
  314. scantokey[14] = K_BACKSPACE;
  315. scantokey[119] = K_PAUSE;
  316. scantokey[57] = ' ';
  317. scantokey[102] = K_HOME;
  318. scantokey[104] = K_PGUP;
  319. scantokey[107] = K_END;
  320. scantokey[109] = K_PGDN;
  321. scantokey[110] = K_INS;
  322. scantokey[111] = K_DEL;
  323. scantokey[2] = '1';
  324. scantokey[3] = '2';
  325. scantokey[4] = '3';
  326. scantokey[5] = '4';
  327. scantokey[6] = '5';
  328. scantokey[7] = '6';
  329. scantokey[8] = '7';
  330. scantokey[9] = '8';
  331. scantokey[10] = '9';
  332. scantokey[11] = '0';
  333. scantokey[12] = '-';
  334. scantokey[13] = '=';
  335. scantokey[41] = '`';
  336. scantokey[26] = '[';
  337. scantokey[27] = ']';
  338. scantokey[39] = ';';
  339. scantokey[40] = '\'';
  340. scantokey[51] = ',';
  341. scantokey[52] = '.';
  342. scantokey[53] = '/';
  343. scantokey[43] = '\\';
  344. scantokey[59] = K_F1;
  345. scantokey[60] = K_F2;
  346. scantokey[61] = K_F3;
  347. scantokey[62] = K_F4;
  348. scantokey[63] = K_F5;
  349. scantokey[64] = K_F6;
  350. scantokey[65] = K_F7;
  351. scantokey[66] = K_F8;
  352. scantokey[67] = K_F9;
  353. scantokey[68] = K_F10;
  354. scantokey[87] = K_F11;
  355. scantokey[88] = K_F12;
  356. scantokey[30] = 'a';
  357. scantokey[48] = 'b';
  358. scantokey[46] = 'c';
  359. scantokey[32] = 'd';
  360. scantokey[18] = 'e';
  361. scantokey[33] = 'f';
  362. scantokey[34] = 'g';
  363. scantokey[35] = 'h';
  364. scantokey[23] = 'i';
  365. scantokey[36] = 'j';
  366. scantokey[37] = 'k';
  367. scantokey[38] = 'l';
  368. scantokey[50] = 'm';
  369. scantokey[49] = 'n';
  370. scantokey[24] = 'o';
  371. scantokey[25] = 'p';
  372. scantokey[16] = 'q';
  373. scantokey[19] = 'r';
  374. scantokey[31] = 's';
  375. scantokey[20] = 't';
  376. scantokey[22] = 'u';
  377. scantokey[47] = 'v';
  378. scantokey[17] = 'w';
  379. scantokey[45] = 'x';
  380. scantokey[21] = 'y';
  381. scantokey[44] = 'z';
  382. scantokey[78] = '+';
  383. scantokey[74] = '-';
  384. if (keyboard_init())
  385. Sys_Error("keyboard_init() failed");
  386. keyboard_seteventhandler(keyhandler);
  387. }
  388. }
  389. #define NUM_RESOLUTIONS 3
  390. static resolutions[NUM_RESOLUTIONS][3]={
  391. { 512, 384, GR_RESOLUTION_512x384 },
  392. { 640, 400, GR_RESOLUTION_640x400 },
  393. { 640, 480, GR_RESOLUTION_640x480 }
  394. };
  395. int findres(int *width, int *height)
  396. {
  397. int i;
  398. for(i=0;i<NUM_RESOLUTIONS;i++)
  399. if((*width<=resolutions[i][0]) && (*height<=resolutions[i][1])) {
  400. *width = resolutions[i][0];
  401. *height = resolutions[i][1];
  402. return resolutions[i][2];
  403. }
  404. *width = 640;
  405. *height = 480;
  406. return GR_RESOLUTION_640x480;
  407. }
  408. qboolean VID_Is8bit(void)
  409. {
  410. return is8bit;
  411. }
  412. #ifdef GL_EXT_SHARED
  413. void VID_Init8bitPalette()
  414. {
  415. // Check for 8bit Extensions and initialize them.
  416. int i;
  417. char thePalette[256*3];
  418. char *oldPalette, *newPalette;
  419. if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") == NULL)
  420. return;
  421. Con_SafePrintf("8-bit GL extensions enabled.\n");
  422. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  423. oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
  424. newPalette = thePalette;
  425. for (i=0;i<256;i++) {
  426. *newPalette++ = *oldPalette++;
  427. *newPalette++ = *oldPalette++;
  428. *newPalette++ = *oldPalette++;
  429. oldPalette++;
  430. }
  431. glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
  432. is8bit = true;
  433. }
  434. #else
  435. extern void gl3DfxSetPaletteEXT(GLuint *pal);
  436. void VID_Init8bitPalette(void)
  437. {
  438. // Check for 8bit Extensions and initialize them.
  439. int i;
  440. GLubyte table[256][4];
  441. char *oldpal;
  442. if (strstr(gl_extensions, "3DFX_set_global_palette") == NULL)
  443. return;
  444. Con_SafePrintf("8-bit GL extensions enabled.\n");
  445. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  446. oldpal = (char *) d_8to24table; //d_8to24table3dfx;
  447. for (i=0;i<256;i++) {
  448. table[i][2] = *oldpal++;
  449. table[i][1] = *oldpal++;
  450. table[i][0] = *oldpal++;
  451. table[i][3] = 255;
  452. oldpal++;
  453. }
  454. gl3DfxSetPaletteEXT((GLuint *)table);
  455. is8bit = true;
  456. }
  457. #endif
  458. void VID_Init(unsigned char *palette)
  459. {
  460. int i;
  461. GLint attribs[32];
  462. char gldir[MAX_OSPATH];
  463. int width = 640, height = 480;
  464. S_Init();
  465. Init_KBD();
  466. Cvar_RegisterVariable (&vid_mode);
  467. Cvar_RegisterVariable (&vid_redrawfull);
  468. Cvar_RegisterVariable (&vid_waitforrefresh);
  469. Cvar_RegisterVariable (&gl_ztrick);
  470. vid.maxwarpwidth = WARP_WIDTH;
  471. vid.maxwarpheight = WARP_HEIGHT;
  472. vid.colormap = host_colormap;
  473. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  474. // interpret command-line params
  475. // set vid parameters
  476. attribs[0] = FXMESA_DOUBLEBUFFER;
  477. attribs[1] = FXMESA_ALPHA_SIZE;
  478. attribs[2] = 1;
  479. attribs[3] = FXMESA_DEPTH_SIZE;
  480. attribs[4] = 1;
  481. attribs[5] = FXMESA_NONE;
  482. if ((i = COM_CheckParm("-width")) != 0)
  483. width = atoi(com_argv[i+1]);
  484. if ((i = COM_CheckParm("-height")) != 0)
  485. height = atoi(com_argv[i+1]);
  486. if ((i = COM_CheckParm("-conwidth")) != 0)
  487. vid.conwidth = Q_atoi(com_argv[i+1]);
  488. else
  489. vid.conwidth = 640;
  490. vid.conwidth &= 0xfff8; // make it a multiple of eight
  491. if (vid.conwidth < 320)
  492. vid.conwidth = 320;
  493. // pick a conheight that matches with correct aspect
  494. vid.conheight = vid.conwidth*3 / 4;
  495. if ((i = COM_CheckParm("-conheight")) != 0)
  496. vid.conheight = Q_atoi(com_argv[i+1]);
  497. if (vid.conheight < 200)
  498. vid.conheight = 200;
  499. fc = fxMesaCreateContext(0, findres(&width, &height), GR_REFRESH_75Hz,
  500. attribs);
  501. if (!fc)
  502. Sys_Error("Unable to create 3DFX context.\n");
  503. scr_width = width;
  504. scr_height = height;
  505. fxMesaMakeCurrent(fc);
  506. if (vid.conheight > height)
  507. vid.conheight = height;
  508. if (vid.conwidth > width)
  509. vid.conwidth = width;
  510. vid.width = vid.conwidth;
  511. vid.height = vid.conheight;
  512. vid.aspect = ((float)vid.height / (float)vid.width) *
  513. (320.0 / 240.0);
  514. vid.numpages = 2;
  515. InitSig(); // trap evil signals
  516. GL_Init();
  517. sprintf (gldir, "%s/glquake", com_gamedir);
  518. Sys_mkdir (gldir);
  519. VID_SetPalette(palette);
  520. // Check for 3DFX Extensions and initialize them.
  521. VID_Init8bitPalette();
  522. Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
  523. vid.recalc_refdef = 1; // force a surface cache flush
  524. }
  525. void Sys_SendKeyEvents(void)
  526. {
  527. if (UseKeyboard)
  528. while (keyboard_update());
  529. }
  530. void Force_CenterView_f (void)
  531. {
  532. cl.viewangles[PITCH] = 0;
  533. }
  534. void mousehandler(int buttonstate, int dx, int dy)
  535. {
  536. mouse_buttonstate = buttonstate;
  537. mx += dx;
  538. my += dy;
  539. }
  540. void IN_Init(void)
  541. {
  542. int mtype;
  543. char *mousedev;
  544. int mouserate;
  545. if (UseMouse)
  546. {
  547. Cvar_RegisterVariable (&mouse_button_commands[0]);
  548. Cvar_RegisterVariable (&mouse_button_commands[1]);
  549. Cvar_RegisterVariable (&mouse_button_commands[2]);
  550. Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  551. mouse_buttons = 3;
  552. mtype = vga_getmousetype();
  553. mousedev = "/dev/mouse";
  554. if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
  555. if (COM_CheckParm("-mdev"))
  556. mousedev = com_argv[COM_CheckParm("-mdev")+1];
  557. mouserate = 1200;
  558. if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
  559. if (COM_CheckParm("-mrate"))
  560. mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
  561. if (mouse_init(mousedev, mtype, mouserate))
  562. {
  563. Con_Printf("No mouse found\n");
  564. UseMouse = 0;
  565. }
  566. else
  567. mouse_seteventhandler(mousehandler);
  568. }
  569. }
  570. void IN_Shutdown(void)
  571. {
  572. if (UseMouse)
  573. mouse_close();
  574. }
  575. /*
  576. ===========
  577. IN_Commands
  578. ===========
  579. */
  580. void IN_Commands (void)
  581. {
  582. if (UseMouse)
  583. {
  584. // poll mouse values
  585. while (mouse_update())
  586. ;
  587. // perform button actions
  588. if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  589. !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  590. Key_Event (K_MOUSE1, true);
  591. else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  592. (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  593. Key_Event (K_MOUSE1, false);
  594. if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  595. !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  596. Key_Event (K_MOUSE2, true);
  597. else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  598. (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  599. Key_Event (K_MOUSE2, false);
  600. if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  601. !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  602. Key_Event (K_MOUSE3, true);
  603. else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  604. (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  605. Key_Event (K_MOUSE3, false);
  606. mouse_oldbuttonstate = mouse_buttonstate;
  607. }
  608. }
  609. /*
  610. ===========
  611. IN_Move
  612. ===========
  613. */
  614. void IN_MouseMove (usercmd_t *cmd)
  615. {
  616. if (!UseMouse)
  617. return;
  618. // poll mouse values
  619. while (mouse_update())
  620. ;
  621. if (m_filter.value)
  622. {
  623. mouse_x = (mx + old_mouse_x) * 0.5;
  624. mouse_y = (my + old_mouse_y) * 0.5;
  625. }
  626. else
  627. {
  628. mouse_x = mx;
  629. mouse_y = my;
  630. }
  631. old_mouse_x = mx;
  632. old_mouse_y = my;
  633. mx = my = 0; // clear for next update
  634. mouse_x *= sensitivity.value;
  635. mouse_y *= sensitivity.value;
  636. // add mouse X/Y movement to cmd
  637. if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  638. cmd->sidemove += m_side.value * mouse_x;
  639. else
  640. cl.viewangles[YAW] -= m_yaw.value * mouse_x;
  641. if (in_mlook.state & 1)
  642. V_StopPitchDrift ();
  643. if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
  644. {
  645. cl.viewangles[PITCH] += m_pitch.value * mouse_y;
  646. if (cl.viewangles[PITCH] > 80)
  647. cl.viewangles[PITCH] = 80;
  648. if (cl.viewangles[PITCH] < -70)
  649. cl.viewangles[PITCH] = -70;
  650. }
  651. else
  652. {
  653. if ((in_strafe.state & 1) && noclip_anglehack)
  654. cmd->upmove -= m_forward.value * mouse_y;
  655. else
  656. cmd->forwardmove -= m_forward.value * mouse_y;
  657. }
  658. }
  659. void IN_Move (usercmd_t *cmd)
  660. {
  661. IN_MouseMove(cmd);
  662. }
  663. void VID_UnlockBuffer() {}
  664. void VID_LockBuffer() {}