gl_vidlinux_x11.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 <X11/cursorfont.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/Xatom.h>
  27. #include <X11/keysym.h>
  28. #include "GL/gl.h"
  29. #include "GL/glx.h"
  30. #include "quakedef.h"
  31. #define WARP_WIDTH 320
  32. #define WARP_HEIGHT 200
  33. static Display *dpy = NULL;
  34. static Window win;
  35. static GLXContext ctx = NULL;
  36. unsigned short d_8to16table[256];
  37. unsigned int d_8to24table[256];
  38. unsigned char d_15to8table[65536];
  39. static qboolean usedga = false;
  40. #define stringify(m) { #m, m }
  41. cvar_t vid_mode = {"vid_mode","0",false};
  42. cvar_t mouse_button_commands[3] =
  43. {
  44. {"mouse1","+attack"},
  45. {"mouse2","+strafe"},
  46. {"mouse3","+forward"},
  47. };
  48. static int mouse_buttons=3;
  49. static int mouse_buttonstate;
  50. static int mouse_oldbuttonstate;
  51. static float mouse_x, mouse_y;
  52. static float p_mouse_x, p_mouse_y;
  53. static float old_mouse_x, old_mouse_y;
  54. cvar_t _windowed_mouse = {"_windowed_mouse", "1", true};
  55. cvar_t m_filter = {"m_filter","0"};
  56. static float old_windowed_mouse;
  57. static int scr_width, scr_height;
  58. #define KEY_MASK (KeyPressMask | KeyReleaseMask)
  59. #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
  60. PointerMotionMask | ButtonMotionMask)
  61. /*-----------------------------------------------------------------------*/
  62. //int texture_mode = GL_NEAREST;
  63. //int texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  64. //int texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  65. int texture_mode = GL_LINEAR;
  66. //int texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  67. //int texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  68. int texture_extension_number = 1;
  69. float gldepthmin, gldepthmax;
  70. cvar_t gl_ztrick = {"gl_ztrick","1"};
  71. const char *gl_vendor;
  72. const char *gl_renderer;
  73. const char *gl_version;
  74. const char *gl_extensions;
  75. qboolean is8bit = false;
  76. qboolean isPermedia = false;
  77. qboolean gl_mtexable = false;
  78. /*-----------------------------------------------------------------------*/
  79. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  80. {
  81. }
  82. void D_EndDirectRect (int x, int y, int width, int height)
  83. {
  84. }
  85. /*
  86. =================
  87. VID_Gamma_f
  88. Keybinding command
  89. =================
  90. */
  91. void VID_Gamma_f (void)
  92. {
  93. float gamma, f, inf;
  94. unsigned char palette[768];
  95. int i;
  96. if (Cmd_Argc () == 2)
  97. {
  98. gamma = Q_atof (Cmd_Argv(1));
  99. for (i=0 ; i<768 ; i++)
  100. {
  101. f = pow ( (host_basepal[i]+1)/256.0 , gamma );
  102. inf = f*255 + 0.5;
  103. if (inf < 0)
  104. inf = 0;
  105. if (inf > 255)
  106. inf = 255;
  107. palette[i] = inf;
  108. }
  109. VID_SetPalette (palette);
  110. vid.recalc_refdef = 1; // force a surface cache flush
  111. }
  112. }
  113. void VID_Shutdown(void)
  114. {
  115. if (!ctx)
  116. return;
  117. XUngrabPointer(dpy,CurrentTime);
  118. XUngrabKeyboard(dpy,CurrentTime);
  119. glXDestroyContext(dpy,ctx);
  120. #ifdef USE_DGA
  121. if (usedga)
  122. XF86DGADirectVideo(dpy,DefaultScreen(dpy),0);
  123. #endif
  124. }
  125. int XLateKey(XKeyEvent *ev)
  126. {
  127. int key;
  128. char buf[64];
  129. KeySym keysym;
  130. key = 0;
  131. XLookupString(ev, buf, sizeof buf, &keysym, 0);
  132. switch(keysym)
  133. {
  134. case XK_KP_Page_Up:
  135. case XK_Page_Up: key = K_PGUP; break;
  136. case XK_KP_Page_Down:
  137. case XK_Page_Down: key = K_PGDN; break;
  138. case XK_KP_Home:
  139. case XK_Home: key = K_HOME; break;
  140. case XK_KP_End:
  141. case XK_End: key = K_END; break;
  142. case XK_KP_Left:
  143. case XK_Left: key = K_LEFTARROW; break;
  144. case XK_KP_Right:
  145. case XK_Right: key = K_RIGHTARROW; break;
  146. case XK_KP_Down:
  147. case XK_Down: key = K_DOWNARROW; break;
  148. case XK_KP_Up:
  149. case XK_Up: key = K_UPARROW; break;
  150. case XK_Escape: key = K_ESCAPE; break;
  151. case XK_KP_Enter:
  152. case XK_Return: key = K_ENTER; break;
  153. case XK_Tab: key = K_TAB; break;
  154. case XK_F1: key = K_F1; break;
  155. case XK_F2: key = K_F2; break;
  156. case XK_F3: key = K_F3; break;
  157. case XK_F4: key = K_F4; break;
  158. case XK_F5: key = K_F5; break;
  159. case XK_F6: key = K_F6; break;
  160. case XK_F7: key = K_F7; break;
  161. case XK_F8: key = K_F8; break;
  162. case XK_F9: key = K_F9; break;
  163. case XK_F10: key = K_F10; break;
  164. case XK_F11: key = K_F11; break;
  165. case XK_F12: key = K_F12; break;
  166. case XK_BackSpace: key = K_BACKSPACE; break;
  167. case XK_KP_Delete:
  168. case XK_Delete: key = K_DEL; break;
  169. case XK_Pause: key = K_PAUSE; break;
  170. case XK_Shift_L:
  171. case XK_Shift_R: key = K_SHIFT; break;
  172. case XK_Execute:
  173. case XK_Control_L:
  174. case XK_Control_R: key = K_CTRL; break;
  175. case XK_Alt_L:
  176. case XK_Meta_L:
  177. case XK_Alt_R:
  178. case XK_Meta_R: key = K_ALT; break;
  179. case XK_KP_Begin: key = K_AUX30; break;
  180. case XK_Insert:
  181. case XK_KP_Insert: key = K_INS; break;
  182. case XK_KP_Multiply: key = '*'; break;
  183. case XK_KP_Add: key = '+'; break;
  184. case XK_KP_Subtract: key = '-'; break;
  185. case XK_KP_Divide: key = '/'; break;
  186. #if 0
  187. case 0x021: key = '1';break;/* [!] */
  188. case 0x040: key = '2';break;/* [@] */
  189. case 0x023: key = '3';break;/* [#] */
  190. case 0x024: key = '4';break;/* [$] */
  191. case 0x025: key = '5';break;/* [%] */
  192. case 0x05e: key = '6';break;/* [^] */
  193. case 0x026: key = '7';break;/* [&] */
  194. case 0x02a: key = '8';break;/* [*] */
  195. case 0x028: key = '9';;break;/* [(] */
  196. case 0x029: key = '0';break;/* [)] */
  197. case 0x05f: key = '-';break;/* [_] */
  198. case 0x02b: key = '=';break;/* [+] */
  199. case 0x07c: key = '\'';break;/* [|] */
  200. case 0x07d: key = '[';break;/* [}] */
  201. case 0x07b: key = ']';break;/* [{] */
  202. case 0x022: key = '\'';break;/* ["] */
  203. case 0x03a: key = ';';break;/* [:] */
  204. case 0x03f: key = '/';break;/* [?] */
  205. case 0x03e: key = '.';break;/* [>] */
  206. case 0x03c: key = ',';break;/* [<] */
  207. #endif
  208. default:
  209. key = *(unsigned char*)buf;
  210. if (key >= 'A' && key <= 'Z')
  211. key = key - 'A' + 'a';
  212. // fprintf(stdout, "case 0x0%x: key = ___;break;/* [%c] */\n", keysym);
  213. break;
  214. }
  215. return key;
  216. }
  217. struct
  218. {
  219. int key;
  220. int down;
  221. } keyq[64];
  222. int keyq_head=0;
  223. int keyq_tail=0;
  224. int config_notify=0;
  225. int config_notify_width;
  226. int config_notify_height;
  227. qboolean Keyboard_Update(void)
  228. {
  229. XEvent x_event;
  230. if(!XCheckMaskEvent(dpy,KEY_MASK,&x_event))
  231. return false;
  232. switch(x_event.type) {
  233. case KeyPress:
  234. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  235. keyq[keyq_head].down = true;
  236. keyq_head = (keyq_head + 1) & 63;
  237. break;
  238. case KeyRelease:
  239. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  240. keyq[keyq_head].down = false;
  241. keyq_head = (keyq_head + 1) & 63;
  242. break;
  243. }
  244. return true;
  245. }
  246. qboolean Mouse_Update(void)
  247. {
  248. XEvent x_event;
  249. int b;
  250. if(!XCheckMaskEvent(dpy,MOUSE_MASK,&x_event))
  251. return false;
  252. switch(x_event.type) {
  253. case MotionNotify:
  254. if (usedga) {
  255. mouse_x += x_event.xmotion.x_root;
  256. mouse_y += x_event.xmotion.y_root;
  257. } else if (_windowed_mouse.value) {
  258. mouse_x += (float) ((int)x_event.xmotion.x - (int)(scr_width/2));
  259. mouse_y += (float) ((int)x_event.xmotion.y - (int)(scr_height/2));
  260. /* move the mouse to the window center again */
  261. XSelectInput(dpy,win, (KEY_MASK | MOUSE_MASK) & ~PointerMotionMask);
  262. XWarpPointer(dpy,None,win,0,0,0,0, (scr_width/2),(scr_height/2));
  263. XSelectInput(dpy,win, KEY_MASK | MOUSE_MASK);
  264. } else {
  265. mouse_x = (float) (x_event.xmotion.x-p_mouse_x);
  266. mouse_y = (float) (x_event.xmotion.y-p_mouse_y);
  267. p_mouse_x=x_event.xmotion.x;
  268. p_mouse_y=x_event.xmotion.y;
  269. }
  270. break;
  271. case ButtonPress:
  272. b=-1;
  273. if (x_event.xbutton.button == 1)
  274. b = 0;
  275. else if (x_event.xbutton.button == 2)
  276. b = 2;
  277. else if (x_event.xbutton.button == 3)
  278. b = 1;
  279. if (b>=0)
  280. mouse_buttonstate |= 1<<b;
  281. break;
  282. case ButtonRelease:
  283. b=-1;
  284. if (x_event.xbutton.button == 1)
  285. b = 0;
  286. else if (x_event.xbutton.button == 2)
  287. b = 2;
  288. else if (x_event.xbutton.button == 3)
  289. b = 1;
  290. if (b>=0)
  291. mouse_buttonstate &= ~(1<<b);
  292. break;
  293. }
  294. if (old_windowed_mouse != _windowed_mouse.value) {
  295. old_windowed_mouse = _windowed_mouse.value;
  296. if (!_windowed_mouse.value) {
  297. /* ungrab the pointer */
  298. Con_Printf("Releasing mouse.\n");
  299. XUngrabPointer(dpy,CurrentTime);
  300. XUngrabKeyboard(dpy,CurrentTime);
  301. } else {
  302. /* grab the pointer */
  303. Con_Printf("Grabbing mouse.\n");
  304. XGrabPointer(dpy,win,False,MOUSE_MASK,GrabModeAsync,
  305. GrabModeAsync,win,None,CurrentTime);
  306. XWarpPointer(dpy,None,win, 0,0,0,0, scr_width/2, scr_height/2);
  307. XGrabKeyboard(dpy,win,
  308. False,
  309. GrabModeAsync,GrabModeAsync,
  310. CurrentTime);
  311. //XSync(dpy,True);
  312. }
  313. }
  314. return true;
  315. }
  316. void signal_handler(int sig)
  317. {
  318. printf("Received signal %d, exiting...\n", sig);
  319. VID_Shutdown();
  320. exit(0);
  321. }
  322. void InitSig(void)
  323. {
  324. signal(SIGHUP, signal_handler);
  325. signal(SIGQUIT, signal_handler);
  326. signal(SIGILL, signal_handler);
  327. signal(SIGTRAP, signal_handler);
  328. signal(SIGIOT, signal_handler);
  329. signal(SIGBUS, signal_handler);
  330. signal(SIGFPE, signal_handler);
  331. signal(SIGSEGV, signal_handler);
  332. signal(SIGTERM, signal_handler);
  333. }
  334. void VID_ShiftPalette(unsigned char *p)
  335. {
  336. VID_SetPalette(p);
  337. }
  338. void VID_SetPalette (unsigned char *palette)
  339. {
  340. byte *pal;
  341. unsigned short r,g,b;
  342. int v;
  343. int r1,g1,b1;
  344. int k;
  345. unsigned short i;
  346. unsigned *table;
  347. FILE *f;
  348. char s[255];
  349. float dist, bestdist;
  350. static qboolean palflag = false;
  351. //
  352. // 8 8 8 encoding
  353. //
  354. pal = palette;
  355. table = d_8to24table;
  356. for (i=0 ; i<256 ; i++)
  357. {
  358. r = pal[0];
  359. g = pal[1];
  360. b = pal[2];
  361. pal += 3;
  362. // v = (255<<24) + (r<<16) + (g<<8) + (b<<0);
  363. // v = (255<<0) + (r<<8) + (g<<16) + (b<<24);
  364. v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
  365. *table++ = v;
  366. }
  367. d_8to24table[255] &= 0xffffff; // 255 is transparent
  368. // JACK: 3D distance calcs - k is last closest, l is the distance.
  369. // FIXME: Precalculate this and cache to disk.
  370. if (palflag)
  371. return;
  372. palflag = true;
  373. COM_FOpenFile("glquake/15to8.pal", &f);
  374. if (f) {
  375. fread(d_15to8table, 1<<15, 1, f);
  376. fclose(f);
  377. } else {
  378. for (i=0; i < (1<<15); i++) {
  379. /* Maps
  380. 000000000000000
  381. 000000000011111 = Red = 0x1F
  382. 000001111100000 = Blue = 0x03E0
  383. 111110000000000 = Grn = 0x7C00
  384. */
  385. r = ((i & 0x1F) << 3)+4;
  386. g = ((i & 0x03E0) >> 2)+4;
  387. b = ((i & 0x7C00) >> 7)+4;
  388. pal = (unsigned char *)d_8to24table;
  389. for (v=0,k=0,bestdist=10000.0; v<256; v++,pal+=4) {
  390. r1 = (int)r - (int)pal[0];
  391. g1 = (int)g - (int)pal[1];
  392. b1 = (int)b - (int)pal[2];
  393. dist = sqrt(((r1*r1)+(g1*g1)+(b1*b1)));
  394. if (dist < bestdist) {
  395. k=v;
  396. bestdist = dist;
  397. }
  398. }
  399. d_15to8table[i]=k;
  400. }
  401. sprintf(s, "%s/glquake", com_gamedir);
  402. Sys_mkdir (s);
  403. sprintf(s, "%s/glquake/15to8.pal", com_gamedir);
  404. if ((f = fopen(s, "wb")) != NULL) {
  405. fwrite(d_15to8table, 1<<15, 1, f);
  406. fclose(f);
  407. }
  408. }
  409. }
  410. /*
  411. ===============
  412. GL_Init
  413. ===============
  414. */
  415. void GL_Init (void)
  416. {
  417. gl_vendor = glGetString (GL_VENDOR);
  418. Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
  419. gl_renderer = glGetString (GL_RENDERER);
  420. Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
  421. gl_version = glGetString (GL_VERSION);
  422. Con_Printf ("GL_VERSION: %s\n", gl_version);
  423. gl_extensions = glGetString (GL_EXTENSIONS);
  424. Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
  425. // Con_Printf ("%s %s\n", gl_renderer, gl_version);
  426. glClearColor (1,0,0,0);
  427. glCullFace(GL_FRONT);
  428. glEnable(GL_TEXTURE_2D);
  429. glEnable(GL_ALPHA_TEST);
  430. glAlphaFunc(GL_GREATER, 0.666);
  431. glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  432. glShadeModel (GL_FLAT);
  433. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  434. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  435. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  436. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  437. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  438. // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  439. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  440. }
  441. /*
  442. =================
  443. GL_BeginRendering
  444. =================
  445. */
  446. void GL_BeginRendering (int *x, int *y, int *width, int *height)
  447. {
  448. extern cvar_t gl_clear;
  449. *x = *y = 0;
  450. *width = scr_width;
  451. *height = scr_height;
  452. // if (!wglMakeCurrent( maindc, baseRC ))
  453. // Sys_Error ("wglMakeCurrent failed");
  454. // glViewport (*x, *y, *width, *height);
  455. }
  456. void GL_EndRendering (void)
  457. {
  458. glFlush();
  459. glXSwapBuffers(dpy,win);
  460. }
  461. qboolean VID_Is8bit(void)
  462. {
  463. return is8bit;
  464. }
  465. #ifdef GL_EXT_SHARED
  466. void VID_Init8bitPalette()
  467. {
  468. // Check for 8bit Extensions and initialize them.
  469. int i;
  470. char thePalette[256*3];
  471. char *oldPalette, *newPalette;
  472. if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") == NULL)
  473. return;
  474. Con_SafePrintf("8-bit GL extensions enabled.\n");
  475. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  476. oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
  477. newPalette = thePalette;
  478. for (i=0;i<256;i++) {
  479. *newPalette++ = *oldPalette++;
  480. *newPalette++ = *oldPalette++;
  481. *newPalette++ = *oldPalette++;
  482. oldPalette++;
  483. }
  484. glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
  485. is8bit = true;
  486. }
  487. #else
  488. extern void gl3DfxSetPaletteEXT(GLuint *pal);
  489. void VID_Init8bitPalette(void)
  490. {
  491. // Check for 8bit Extensions and initialize them.
  492. int i;
  493. GLubyte table[256][4];
  494. char *oldpal;
  495. if (strstr(gl_extensions, "3DFX_set_global_palette") == NULL)
  496. return;
  497. Con_SafePrintf("8-bit GL extensions enabled.\n");
  498. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  499. oldpal = (char *) d_8to24table; //d_8to24table3dfx;
  500. for (i=0;i<256;i++) {
  501. table[i][2] = *oldpal++;
  502. table[i][1] = *oldpal++;
  503. table[i][0] = *oldpal++;
  504. table[i][3] = 255;
  505. oldpal++;
  506. }
  507. gl3DfxSetPaletteEXT((GLuint *)table);
  508. is8bit = true;
  509. }
  510. #endif
  511. void VID_Init(unsigned char *palette)
  512. {
  513. int i;
  514. char gldir[MAX_OSPATH];
  515. int width = 640, height = 480;
  516. int attrib[] = {
  517. GLX_RGBA,
  518. GLX_RED_SIZE, 1,
  519. GLX_GREEN_SIZE, 1,
  520. GLX_BLUE_SIZE, 1,
  521. GLX_DOUBLEBUFFER,
  522. GLX_DEPTH_SIZE, 1,
  523. None };
  524. int scrnum;
  525. XSetWindowAttributes attr;
  526. unsigned long mask;
  527. Window root;
  528. XVisualInfo *visinfo;
  529. S_Init();
  530. Cvar_RegisterVariable (&vid_mode);
  531. Cvar_RegisterVariable (&gl_ztrick);
  532. vid.maxwarpwidth = WARP_WIDTH;
  533. vid.maxwarpheight = WARP_HEIGHT;
  534. vid.colormap = host_colormap;
  535. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  536. // interpret command-line params
  537. // set vid parameters
  538. if ((i = COM_CheckParm("-width")) != 0)
  539. width = atoi(com_argv[i+1]);
  540. if ((i = COM_CheckParm("-height")) != 0)
  541. height = atoi(com_argv[i+1]);
  542. if ((i = COM_CheckParm("-conwidth")) != 0)
  543. vid.conwidth = Q_atoi(com_argv[i+1]);
  544. else
  545. vid.conwidth = 640;
  546. vid.conwidth &= 0xfff8; // make it a multiple of eight
  547. if (vid.conwidth < 320)
  548. vid.conwidth = 320;
  549. // pick a conheight that matches with correct aspect
  550. vid.conheight = vid.conwidth*3 / 4;
  551. if ((i = COM_CheckParm("-conheight")) != 0)
  552. vid.conheight = Q_atoi(com_argv[i+1]);
  553. if (vid.conheight < 200)
  554. vid.conheight = 200;
  555. if (!(dpy = XOpenDisplay(NULL))) {
  556. fprintf(stderr, "Error couldn't open the X display\n");
  557. exit(1);
  558. }
  559. scrnum = DefaultScreen(dpy);
  560. root = RootWindow(dpy, scrnum);
  561. visinfo=glXChooseVisual(dpy,scrnum,attrib);
  562. if (!visinfo) {
  563. fprintf(stderr, "Error couldn't get an RGB, Double-buffered, Depth visual\n");
  564. exit(1);
  565. }
  566. /* window attributes */
  567. attr.background_pixel=0;
  568. attr.border_pixel=0;
  569. attr.colormap=XCreateColormap(dpy,root,visinfo->visual,AllocNone);
  570. attr.event_mask=KEY_MASK|MOUSE_MASK|VisibilityChangeMask;
  571. mask=CWBackPixel|CWBorderPixel|CWColormap|CWEventMask;
  572. win=XCreateWindow(dpy,root,0,0,width,height,
  573. 0,visinfo->depth,InputOutput,
  574. visinfo->visual,mask,&attr);
  575. XMapWindow(dpy,win);
  576. XMoveWindow(dpy,win,0,0);
  577. XFlush(dpy);
  578. if (COM_CheckParm("-window"))
  579. putenv("MESA_GLX_FX=window");
  580. else
  581. putenv("MESA_GLX_FX=fullscreen");
  582. ctx = glXCreateContext(dpy,visinfo,NULL,True);
  583. if (!ctx) {
  584. fprintf(stderr, "Unable to create glX context.\n");
  585. exit(1);
  586. }
  587. glXMakeCurrent(dpy,win,ctx);
  588. scr_width = width;
  589. scr_height = height;
  590. if (vid.conheight > height)
  591. vid.conheight = height;
  592. if (vid.conwidth > width)
  593. vid.conwidth = width;
  594. vid.width = vid.conwidth;
  595. vid.height = vid.conheight;
  596. vid.aspect = ((float)vid.height / (float)vid.width) *
  597. (320.0 / 240.0);
  598. vid.numpages = 2;
  599. InitSig(); // trap evil signals
  600. GL_Init();
  601. sprintf (gldir, "%s/glquake", com_gamedir);
  602. Sys_mkdir (gldir);
  603. VID_SetPalette(palette);
  604. // Check for 3DFX Extensions and initialize them.
  605. VID_Init8bitPalette();
  606. Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
  607. vid.recalc_refdef = 1; // force a surface cache flush
  608. }
  609. void Sys_SendKeyEvents(void)
  610. {
  611. if (dpy)
  612. {
  613. while (Keyboard_Update())
  614. ;
  615. while (keyq_head != keyq_tail)
  616. {
  617. Key_Event(keyq[keyq_tail].key, keyq[keyq_tail].down);
  618. keyq_tail = (keyq_tail + 1) & 63;
  619. }
  620. }
  621. }
  622. void Force_CenterView_f (void)
  623. {
  624. cl.viewangles[PITCH] = 0;
  625. }
  626. void IN_Init(void)
  627. {
  628. Cvar_RegisterVariable (&_windowed_mouse);
  629. Cvar_RegisterVariable (&m_filter);
  630. Cvar_RegisterVariable (&mouse_button_commands[0]);
  631. Cvar_RegisterVariable (&mouse_button_commands[1]);
  632. Cvar_RegisterVariable (&mouse_button_commands[2]);
  633. Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  634. }
  635. void IN_Shutdown(void)
  636. {
  637. }
  638. /*
  639. ===========
  640. IN_Commands
  641. ===========
  642. */
  643. void IN_Commands (void)
  644. {
  645. int i;
  646. for (i=0 ; i<mouse_buttons ; i++) {
  647. if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
  648. Key_Event (K_MOUSE1 + i, true);
  649. if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
  650. Key_Event (K_MOUSE1 + i, false);
  651. }
  652. mouse_oldbuttonstate = mouse_buttonstate;
  653. }
  654. /*
  655. ===========
  656. IN_Move
  657. ===========
  658. */
  659. void IN_Move (usercmd_t *cmd)
  660. {
  661. while (Mouse_Update())
  662. ;
  663. if (m_filter.value) {
  664. mouse_x = (mouse_x + old_mouse_x) * 0.5;
  665. mouse_y = (mouse_y + old_mouse_y) * 0.5;
  666. }
  667. old_mouse_x = mouse_x;
  668. old_mouse_y = mouse_y;
  669. mouse_x *= sensitivity.value;
  670. mouse_y *= sensitivity.value;
  671. if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  672. cmd->sidemove += m_side.value * mouse_x;
  673. else
  674. cl.viewangles[YAW] -= m_yaw.value * mouse_x;
  675. if (in_mlook.state & 1)
  676. V_StopPitchDrift ();
  677. if ( (in_mlook.state & 1) && !(in_strafe.state & 1)) {
  678. cl.viewangles[PITCH] += m_pitch.value * mouse_y;
  679. if (cl.viewangles[PITCH] > 80)
  680. cl.viewangles[PITCH] = 80;
  681. if (cl.viewangles[PITCH] < -70)
  682. cl.viewangles[PITCH] = -70;
  683. } else {
  684. if ((in_strafe.state & 1) && noclip_anglehack)
  685. cmd->upmove -= m_forward.value * mouse_y;
  686. else
  687. cmd->forwardmove -= m_forward.value * mouse_y;
  688. }
  689. mouse_x = mouse_y = 0.0;
  690. }
  691. void VID_LockBuffer (void) {}
  692. void VID_UnlockBuffer (void) {}