glw_imp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. ** GLW_IMP.C
  3. **
  4. ** This file contains ALL Linux specific stuff having to do with the
  5. ** OpenGL refresh. When a port is being made the following functions
  6. ** must be implemented by the port:
  7. **
  8. ** GLimp_EndFrame
  9. ** GLimp_Init
  10. ** GLimp_Shutdown
  11. ** GLimp_SwitchFullscreen
  12. **
  13. */
  14. #include <signal.h>
  15. #include <X11/Xlib.h>
  16. #include <X11/Xutil.h>
  17. #include <X11/Xatom.h>
  18. #include <X11/keysym.h>
  19. #include <X11/extensions/XShm.h>
  20. #include <Xm/MwmUtil.h>
  21. #include <GL/glx.h>
  22. #include "../ref_gl/gl_local.h"
  23. #include "../client/keys.h"
  24. #include "../linux/rw_linux.h"
  25. GLXContext gl_cx;
  26. static qboolean doShm;
  27. static Display *x_disp;
  28. static Colormap x_cmap;
  29. static Window x_win;
  30. static GC x_gc;
  31. static Visual *x_vis;
  32. static XVisualInfo *x_visinfo;
  33. static int StudlyRGBattributes[] =
  34. {
  35. GLX_DOUBLEBUFFER,
  36. GLX_RGBA,
  37. GLX_RED_SIZE, 4,
  38. GLX_GREEN_SIZE, 4,
  39. GLX_BLUE_SIZE, 4,
  40. GLX_DEPTH_SIZE, 1,
  41. GLX_SAMPLES_SGIS, 4, /* for better AA */
  42. None,
  43. };
  44. static int RGBattributes[] =
  45. {
  46. GLX_DOUBLEBUFFER,
  47. GLX_RGBA,
  48. GLX_RED_SIZE, 4,
  49. GLX_GREEN_SIZE, 4,
  50. GLX_BLUE_SIZE, 4,
  51. GLX_DEPTH_SIZE, 1,
  52. None,
  53. };
  54. #define STD_EVENT_MASK (StructureNotifyMask | KeyPressMask \
  55. | KeyReleaseMask | ExposureMask | PointerMotionMask | \
  56. ButtonPressMask | ButtonReleaseMask)
  57. int current_framebuffer;
  58. static int x_shmeventtype;
  59. //static XShmSegmentInfo x_shminfo;
  60. static qboolean oktodraw = false;
  61. static qboolean X11_active = false;
  62. struct
  63. {
  64. int key;
  65. int down;
  66. } keyq[64];
  67. int keyq_head=0;
  68. int keyq_tail=0;
  69. static int mx, my;
  70. static int p_mouse_x, p_mouse_y;
  71. static cvar_t *_windowed_mouse;
  72. static cvar_t *sensitivity;
  73. static cvar_t *lookstrafe;
  74. static cvar_t *m_side;
  75. static cvar_t *m_yaw;
  76. static cvar_t *m_pitch;
  77. static cvar_t *m_forward;
  78. static cvar_t *freelook;
  79. int config_notify=0;
  80. int config_notify_width;
  81. int config_notify_height;
  82. typedef unsigned short PIXEL;
  83. // Console variables that we need to access from this module
  84. /*****************************************************************************/
  85. /* MOUSE */
  86. /*****************************************************************************/
  87. // this is inside the renderer shared lib, so these are called from vid_so
  88. static qboolean mouse_avail;
  89. static int mouse_buttonstate;
  90. static int mouse_oldbuttonstate;
  91. static int mouse_x, mouse_y;
  92. static int old_mouse_x, old_mouse_y;
  93. static float old_windowed_mouse;
  94. static int p_mouse_x, p_mouse_y;
  95. static cvar_t *_windowed_mouse;
  96. static cvar_t *m_filter;
  97. static cvar_t *in_mouse;
  98. static qboolean mlooking;
  99. // state struct passed in Init
  100. static in_state_t *in_state;
  101. int XShmQueryExtension(Display *);
  102. int XShmGetEventBase(Display *);
  103. static void signal_handler(int sig)
  104. {
  105. fprintf(stderr, "Received signal %d, exiting...\n", sig);
  106. GLimp_Shutdown();
  107. _exit(0);
  108. }
  109. static void InitSig(void)
  110. {
  111. struct sigaction sa;
  112. sigaction(SIGINT, 0, &sa);
  113. sa.sa_handler = signal_handler;
  114. sigaction(SIGINT, &sa, 0);
  115. sigaction(SIGTERM, &sa, 0);
  116. }
  117. /*
  118. ** GLimp_SetMode
  119. */
  120. int GLimp_SetMode( int *pwidth, int *pheight, int mode, qboolean fullscreen )
  121. {
  122. int width, height;
  123. GLint attribs[32];
  124. fprintf(stderr, "GLimp_SetMode\n");
  125. ri.Con_Printf( PRINT_ALL, "Initializing OpenGL display\n");
  126. ri.Con_Printf (PRINT_ALL, "...setting mode %d:", mode );
  127. if ( !ri.Vid_GetModeInfo( &width, &height, mode ) )
  128. {
  129. ri.Con_Printf( PRINT_ALL, " invalid mode\n" );
  130. return rserr_invalid_mode;
  131. }
  132. ri.Con_Printf( PRINT_ALL, " %d %d\n", width, height );
  133. // destroy the existing window
  134. GLimp_Shutdown ();
  135. *pwidth = width;
  136. *pheight = height;
  137. if ( !GLimp_InitGraphics( fullscreen ) ) {
  138. // failed to set a valid mode in windowed mode
  139. return rserr_invalid_mode;
  140. }
  141. /* gl_cx = glXCreateContext( x_disp, x_visinfo, 0, True ); */
  142. // let the sound and input subsystems know about the new window
  143. ri.Vid_NewWindow (width, height);
  144. return rserr_ok;
  145. }
  146. /*
  147. ** GLimp_Shutdown
  148. **
  149. ** This routine does all OS specific shutdown procedures for the OpenGL
  150. ** subsystem. Under OpenGL this means NULLing out the current DC and
  151. ** HGLRC, deleting the rendering context, and releasing the DC acquired
  152. ** for the window. The state structure is also nulled out.
  153. **
  154. */
  155. void GLimp_Shutdown( void )
  156. {
  157. fprintf(stderr, "GLimp_Shutdown\n");
  158. if (!x_disp)
  159. return;
  160. XSynchronize( x_disp, True );
  161. XAutoRepeatOn(x_disp);
  162. XCloseDisplay(x_disp);
  163. x_disp = NULL;
  164. }
  165. /*
  166. ** GLimp_Init
  167. **
  168. ** This routine is responsible for initializing the OS specific portions
  169. ** of OpenGL.
  170. */
  171. int GLimp_Init( void *hinstance, void *wndproc )
  172. {
  173. // catch signals so i can turn on auto-repeat and stuff
  174. InitSig();
  175. return true;
  176. }
  177. /*
  178. ** GLimp_BeginFrame
  179. */
  180. void GLimp_BeginFrame( float camera_seperation )
  181. {
  182. }
  183. /*
  184. ** GLimp_EndFrame
  185. **
  186. ** Responsible for doing a swapbuffers and possibly for other stuff
  187. ** as yet to be determined. Probably better not to make this a GLimp
  188. ** function and instead do a call to GLimp_SwapBuffers.
  189. */
  190. void GLimp_EndFrame (void)
  191. {
  192. glFlush();
  193. glXSwapBuffers( x_disp, x_win );
  194. }
  195. /*
  196. ** GLimp_AppActivate
  197. */
  198. void GLimp_AppActivate( qboolean active )
  199. {
  200. }
  201. // ========================================================================
  202. // makes a null cursor
  203. // ========================================================================
  204. static Cursor CreateNullCursor(Display *display, Window root)
  205. {
  206. Pixmap cursormask;
  207. XGCValues xgc;
  208. GC gc;
  209. XColor dummycolour;
  210. Cursor cursor;
  211. cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
  212. xgc.function = GXclear;
  213. gc = XCreateGC(display, cursormask, GCFunction, &xgc);
  214. XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
  215. dummycolour.pixel = 0;
  216. dummycolour.red = 0;
  217. dummycolour.flags = 04;
  218. cursor = XCreatePixmapCursor(display, cursormask, cursormask,
  219. &dummycolour,&dummycolour, 0,0);
  220. XFreePixmap(display,cursormask);
  221. XFreeGC(display,gc);
  222. return cursor;
  223. }
  224. /*
  225. ** GLimp_InitGraphics
  226. **
  227. ** This initializes the GL implementation specific
  228. ** graphics subsystem.
  229. **
  230. ** The necessary width and height parameters are grabbed from
  231. ** vid.width and vid.height.
  232. */
  233. qboolean GLimp_InitGraphics( qboolean fullscreen )
  234. {
  235. int pnum, i;
  236. XVisualInfo template;
  237. int num_visuals;
  238. int template_mask;
  239. fprintf(stderr, "GLimp_InitGraphics\n");
  240. srandom(getpid());
  241. // let the sound and input subsystems know about the new window
  242. ri.Vid_NewWindow (vid.width, vid.height);
  243. // open the display
  244. x_disp = XOpenDisplay(NULL);
  245. if (!x_disp)
  246. {
  247. if (getenv("DISPLAY"))
  248. Sys_Error("VID: Could not open display [%s]\n",
  249. getenv("DISPLAY"));
  250. else
  251. Sys_Error("VID: Could not open local display\n");
  252. }
  253. else
  254. fprintf(stderr, "VID: Opened display %s\n", getenv("DISPLAY"));
  255. XAutoRepeatOff(x_disp);
  256. // for debugging only
  257. XSynchronize(x_disp, True);
  258. // check for command-line window size
  259. template_mask = 0;
  260. #if 0
  261. // specify a visual id
  262. if ((pnum=COM_CheckParm("-visualid")))
  263. {
  264. if (pnum >= com_argc-1)
  265. Sys_Error("VID: -visualid <id#>\n");
  266. template.visualid = Q_atoi(com_argv[pnum+1]);
  267. template_mask = VisualIDMask;
  268. }
  269. // If not specified, use default visual
  270. else
  271. #endif
  272. {
  273. int screen;
  274. screen = XDefaultScreen(x_disp);
  275. template.visualid =
  276. XVisualIDFromVisual(XDefaultVisual(x_disp, screen));
  277. template_mask = VisualIDMask;
  278. }
  279. // pick a visual- warn if more than one was available
  280. x_visinfo = glXChooseVisual( x_disp, DefaultScreen( x_disp ),
  281. StudlyRGBattributes );
  282. if (!x_visinfo)
  283. {
  284. fprintf(stderr, "Using non studly RGB attributes\n");
  285. x_visinfo = glXChooseVisual( x_disp, DefaultScreen( x_disp ),
  286. RGBattributes );
  287. if (!x_visinfo) Sys_Error( "No matching visual available!\n" );
  288. }
  289. ri.Con_Printf(PRINT_ALL, "Using visualid 0x%x:\n",
  290. (int)(x_visinfo->visualid));
  291. #if 0
  292. if (verbose)
  293. {
  294. printf("Using visualid %d:\n", (int)(x_visinfo->visualid));
  295. printf(" screen %d\n", x_visinfo->screen);
  296. printf(" red_mask 0x%x\n", (int)(x_visinfo->red_mask));
  297. printf(" green_mask 0x%x\n", (int)(x_visinfo->green_mask));
  298. printf(" blue_mask 0x%x\n", (int)(x_visinfo->blue_mask));
  299. printf(" colormap_size %d\n", x_visinfo->colormap_size);
  300. printf(" bits_per_rgb %d\n", x_visinfo->bits_per_rgb);
  301. }
  302. #endif
  303. x_vis = x_visinfo->visual;
  304. // setup attributes for main window
  305. {
  306. int attribmask = CWEventMask | CWColormap | CWBorderPixel;
  307. XSetWindowAttributes attribs;
  308. Colormap tmpcmap;
  309. Window root_win = XRootWindow(x_disp, x_visinfo->screen);
  310. tmpcmap = XCreateColormap(x_disp, root_win, x_vis, AllocNone);
  311. attribs.event_mask = STD_EVENT_MASK;
  312. attribs.border_pixel = 0;
  313. attribs.colormap = tmpcmap;
  314. // create the main window
  315. x_win = XCreateWindow( x_disp,
  316. root_win,
  317. 0, 0, // x, y
  318. vid.width, vid.height,
  319. 0, // borderwidth
  320. x_visinfo->depth,
  321. InputOutput,
  322. x_vis,
  323. attribmask,
  324. &attribs );
  325. XStoreName(x_disp, x_win, "Quake II");
  326. if (x_visinfo->class != TrueColor)
  327. XFreeColormap(x_disp, tmpcmap);
  328. }
  329. if (x_visinfo->depth == 8)
  330. {
  331. // create and upload the palette
  332. if (x_visinfo->class == PseudoColor)
  333. {
  334. x_cmap = XCreateColormap(x_disp, x_win, x_vis, AllocAll);
  335. XSetWindowColormap(x_disp, x_win, x_cmap);
  336. }
  337. }
  338. // inviso cursor
  339. XDefineCursor(x_disp, x_win, CreateNullCursor(x_disp, x_win));
  340. // create the GC
  341. {
  342. XGCValues xgcvalues;
  343. int valuemask = GCGraphicsExposures;
  344. xgcvalues.graphics_exposures = False;
  345. x_gc = XCreateGC(x_disp, x_win, valuemask, &xgcvalues );
  346. }
  347. // set window properties for full screen
  348. if (fullscreen) {
  349. MotifWmHints wmhints;
  350. Atom aHints;
  351. XSizeHints sizehints;
  352. XWindowChanges changes;
  353. aHints = XInternAtom( x_disp, "_MOTIF_WM_HINTS", 0 );
  354. if (aHints == None)
  355. {
  356. ri.Con_Printf( PRINT_ALL, "Could not intern X atom for _MOTIF_WM_HINTS." );
  357. /* return( false ); */
  358. }
  359. else {
  360. wmhints.flags = MWM_HINTS_DECORATIONS;
  361. wmhints.decorations = 0; // Absolutely no decorations.
  362. XChangeProperty(x_disp, x_win, aHints, aHints, 32,
  363. PropModeReplace, (unsigned char *)&wmhints,
  364. 4 );
  365. sizehints.flags = USPosition | USSize;
  366. sizehints.x = 0;
  367. sizehints.y = 0;
  368. sizehints.width = vid.width;
  369. sizehints.height = vid.height;
  370. XSetWMNormalHints( x_disp, x_win, &sizehints );
  371. changes.x = 0;
  372. changes.y = 0;
  373. changes.width = vid.width;
  374. changes.height = vid.height;
  375. changes.stack_mode = TopIf;
  376. XConfigureWindow(x_disp, x_win,
  377. CWX | CWY | CWWidth | CWHeight | CWStackMode,
  378. &changes);
  379. }
  380. }
  381. // map the window
  382. XMapWindow(x_disp, x_win);
  383. // wait for first exposure event
  384. {
  385. XEvent event;
  386. do
  387. {
  388. XNextEvent(x_disp, &event);
  389. if (event.type == Expose && !event.xexpose.count)
  390. oktodraw = true;
  391. } while (!oktodraw);
  392. }
  393. // now safe to draw
  394. gl_cx = glXCreateContext( x_disp, x_visinfo, 0, True );
  395. if (!glXMakeCurrent( x_disp, x_win, gl_cx ))
  396. Sys_Error( "Can't make window current to context\n" );
  397. // even if MITSHM is available, make sure it's a local connection
  398. #if 0
  399. // This is messing up the DISPLAY environment variable so can't close and
  400. // reopen the window (it lops off the :0.0)...
  401. if (XShmQueryExtension(x_disp))
  402. {
  403. char *displayname;
  404. doShm = true;
  405. displayname = (char *) getenv("DISPLAY");
  406. if (displayname)
  407. {
  408. char *d = displayname;
  409. while (*d && (*d != ':')) d++;
  410. if (*d) *d = 0;
  411. if (!(!strcasecmp(displayname, "unix") || !*displayname))
  412. doShm = false;
  413. }
  414. }
  415. #endif
  416. #if 0
  417. if (doShm)
  418. {
  419. x_shmeventtype = XShmGetEventBase(x_disp) + ShmCompletion;
  420. ResetSharedFrameBuffers();
  421. }
  422. else
  423. ResetFrameBuffer();
  424. #endif
  425. current_framebuffer = 0;
  426. /* vid.rowbytes = x_framebuffer[0]->bytes_per_line; */
  427. /* vid.buffer = x_framebuffer[0]->data; */
  428. // XSynchronize(x_disp, False);
  429. X11_active = true;
  430. return true;
  431. }
  432. /*****************************************************************************/
  433. int XLateKey(XKeyEvent *ev)
  434. {
  435. int key;
  436. char buf[64];
  437. KeySym keysym;
  438. key = 0;
  439. XLookupString(ev, buf, sizeof buf, &keysym, 0);
  440. switch(keysym)
  441. {
  442. case XK_KP_Page_Up: key = K_KP_PGUP; break;
  443. case XK_Page_Up: key = K_PGUP; break;
  444. case XK_KP_Page_Down: key = K_KP_PGDN; break;
  445. case XK_Page_Down: key = K_PGDN; break;
  446. case XK_KP_Home: key = K_KP_HOME; break;
  447. case XK_Home: key = K_HOME; break;
  448. case XK_KP_End: key = K_KP_END; break;
  449. case XK_End: key = K_END; break;
  450. case XK_KP_Left: key = K_KP_LEFTARROW; break;
  451. case XK_Left: key = K_LEFTARROW; break;
  452. case XK_KP_Right: key = K_KP_RIGHTARROW; break;
  453. case XK_Right: key = K_RIGHTARROW; break;
  454. case XK_KP_Down: key = K_KP_DOWNARROW; break;
  455. case XK_Down: key = K_DOWNARROW; break;
  456. case XK_KP_Up: key = K_KP_UPARROW; break;
  457. case XK_Up: key = K_UPARROW; break;
  458. case XK_Escape: key = K_ESCAPE; break;
  459. case XK_KP_Enter: key = K_KP_ENTER; break;
  460. case XK_Return: key = K_ENTER; break;
  461. case XK_Tab: key = K_TAB; break;
  462. case XK_F1: key = K_F1; break;
  463. case XK_F2: key = K_F2; break;
  464. case XK_F3: key = K_F3; break;
  465. case XK_F4: key = K_F4; break;
  466. case XK_F5: key = K_F5; break;
  467. case XK_F6: key = K_F6; break;
  468. case XK_F7: key = K_F7; break;
  469. case XK_F8: key = K_F8; break;
  470. case XK_F9: key = K_F9; break;
  471. case XK_F10: key = K_F10; break;
  472. case XK_F11: key = K_F11; break;
  473. case XK_F12: key = K_F12; break;
  474. case XK_BackSpace: key = K_BACKSPACE; break;
  475. case XK_KP_Delete: key = K_KP_DEL; break;
  476. case XK_Delete: key = K_DEL; break;
  477. case XK_Pause: key = K_PAUSE; break;
  478. case XK_Shift_L:
  479. case XK_Shift_R: key = K_SHIFT; break;
  480. case XK_Execute:
  481. case XK_Control_L:
  482. case XK_Control_R: key = K_CTRL; break;
  483. case XK_Alt_L:
  484. case XK_Meta_L:
  485. case XK_Alt_R:
  486. case XK_Meta_R: key = K_ALT; break;
  487. case XK_KP_Begin: key = K_KP_5; break;
  488. case XK_Insert:key = K_INS; break;
  489. case XK_KP_Insert: key = K_KP_INS; break;
  490. case XK_KP_Multiply: key = '*'; break;
  491. case XK_KP_Add: key = K_KP_PLUS; break;
  492. case XK_KP_Subtract: key = K_KP_MINUS; break;
  493. case XK_KP_Divide: key = K_KP_SLASH; break;
  494. #if 0
  495. case 0x021: key = '1';break;/* [!] */
  496. case 0x040: key = '2';break;/* [@] */
  497. case 0x023: key = '3';break;/* [#] */
  498. case 0x024: key = '4';break;/* [$] */
  499. case 0x025: key = '5';break;/* [%] */
  500. case 0x05e: key = '6';break;/* [^] */
  501. case 0x026: key = '7';break;/* [&] */
  502. case 0x02a: key = '8';break;/* [*] */
  503. case 0x028: key = '9';;break;/* [(] */
  504. case 0x029: key = '0';break;/* [)] */
  505. case 0x05f: key = '-';break;/* [_] */
  506. case 0x02b: key = '=';break;/* [+] */
  507. case 0x07c: key = '\'';break;/* [|] */
  508. case 0x07d: key = '[';break;/* [}] */
  509. case 0x07b: key = ']';break;/* [{] */
  510. case 0x022: key = '\'';break;/* ["] */
  511. case 0x03a: key = ';';break;/* [:] */
  512. case 0x03f: key = '/';break;/* [?] */
  513. case 0x03e: key = '.';break;/* [>] */
  514. case 0x03c: key = ',';break;/* [<] */
  515. #endif
  516. default:
  517. key = *(unsigned char*)buf;
  518. if (key >= 'A' && key <= 'Z')
  519. key = key - 'A' + 'a';
  520. break;
  521. }
  522. return key;
  523. }
  524. void GetEvent(void)
  525. {
  526. XEvent x_event;
  527. int b;
  528. XNextEvent(x_disp, &x_event);
  529. switch(x_event.type) {
  530. case KeyPress:
  531. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  532. keyq[keyq_head].down = true;
  533. keyq_head = (keyq_head + 1) & 63;
  534. break;
  535. case KeyRelease:
  536. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  537. keyq[keyq_head].down = false;
  538. keyq_head = (keyq_head + 1) & 63;
  539. break;
  540. case MotionNotify:
  541. if (_windowed_mouse->value) {
  542. mx += ((int)x_event.xmotion.x - (int)(vid.width/2));
  543. my += ((int)x_event.xmotion.y - (int)(vid.height/2));
  544. /* move the mouse to the window center again */
  545. XSelectInput(x_disp,x_win, STD_EVENT_MASK & ~PointerMotionMask);
  546. XWarpPointer(x_disp,None,x_win,0,0,0,0,
  547. (vid.width/2),(vid.height/2));
  548. XSelectInput(x_disp,x_win, STD_EVENT_MASK);
  549. } else {
  550. mx = ((int)x_event.xmotion.x - (int)p_mouse_x);
  551. my = ((int)x_event.xmotion.y - (int)p_mouse_y);
  552. p_mouse_x=x_event.xmotion.x;
  553. p_mouse_y=x_event.xmotion.y;
  554. }
  555. break;
  556. case ButtonPress:
  557. b=-1;
  558. if (x_event.xbutton.button == 1)
  559. b = 0;
  560. else if (x_event.xbutton.button == 2)
  561. b = 2;
  562. else if (x_event.xbutton.button == 3)
  563. b = 1;
  564. if (b>=0)
  565. mouse_buttonstate |= 1<<b;
  566. break;
  567. case ButtonRelease:
  568. b=-1;
  569. if (x_event.xbutton.button == 1)
  570. b = 0;
  571. else if (x_event.xbutton.button == 2)
  572. b = 2;
  573. else if (x_event.xbutton.button == 3)
  574. b = 1;
  575. if (b>=0)
  576. mouse_buttonstate &= ~(1<<b);
  577. break;
  578. case ConfigureNotify:
  579. config_notify_width = x_event.xconfigure.width;
  580. config_notify_height = x_event.xconfigure.height;
  581. config_notify = 1;
  582. break;
  583. default:
  584. if (doShm && x_event.type == x_shmeventtype)
  585. oktodraw = true;
  586. }
  587. if (old_windowed_mouse != _windowed_mouse->value) {
  588. old_windowed_mouse = _windowed_mouse->value;
  589. if (!_windowed_mouse->value) {
  590. /* ungrab the pointer */
  591. XUngrabPointer(x_disp,CurrentTime);
  592. } else {
  593. /* grab the pointer */
  594. XGrabPointer(x_disp,x_win,True,0,GrabModeAsync,
  595. GrabModeAsync,x_win,None,CurrentTime);
  596. }
  597. }
  598. }
  599. /*****************************************************************************/
  600. /*****************************************************************************/
  601. /* KEYBOARD */
  602. /*****************************************************************************/
  603. Key_Event_fp_t Key_Event_fp;
  604. void KBD_Init(Key_Event_fp_t fp)
  605. {
  606. _windowed_mouse = ri.Cvar_Get ("_windowed_mouse", "0", CVAR_ARCHIVE);
  607. Key_Event_fp = fp;
  608. }
  609. void KBD_Update(void)
  610. {
  611. // get events from x server
  612. if (x_disp)
  613. {
  614. while (XPending(x_disp))
  615. GetEvent();
  616. while (keyq_head != keyq_tail)
  617. {
  618. Key_Event_fp(keyq[keyq_tail].key, keyq[keyq_tail].down);
  619. keyq_tail = (keyq_tail + 1) & 63;
  620. }
  621. }
  622. }
  623. void KBD_Close(void)
  624. {
  625. }
  626. static void Force_CenterView_f (void)
  627. {
  628. in_state->viewangles[PITCH] = 0;
  629. }
  630. static void RW_IN_MLookDown (void)
  631. {
  632. mlooking = true;
  633. }
  634. static void RW_IN_MLookUp (void)
  635. {
  636. mlooking = false;
  637. in_state->IN_CenterView_fp ();
  638. }
  639. void RW_IN_Init(in_state_t *in_state_p)
  640. {
  641. int mtype;
  642. int i;
  643. fprintf(stderr, "GL RW_IN_Init\n");
  644. in_state = in_state_p;
  645. // mouse variables
  646. _windowed_mouse = ri.Cvar_Get ("_windowed_mouse", "0", CVAR_ARCHIVE);
  647. m_filter = ri.Cvar_Get ("m_filter", "0", 0);
  648. in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
  649. freelook = ri.Cvar_Get( "freelook", "0", 0 );
  650. lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
  651. sensitivity = ri.Cvar_Get ("sensitivity", "3", 0);
  652. m_pitch = ri.Cvar_Get ("m_pitch", "0.022", 0);
  653. m_yaw = ri.Cvar_Get ("m_yaw", "0.022", 0);
  654. m_forward = ri.Cvar_Get ("m_forward", "1", 0);
  655. m_side = ri.Cvar_Get ("m_side", "0.8", 0);
  656. ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown);
  657. ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp);
  658. ri.Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  659. mouse_x = mouse_y = 0.0;
  660. mouse_avail = true;
  661. }
  662. void RW_IN_Shutdown(void)
  663. {
  664. mouse_avail = false;
  665. ri.Cmd_RemoveCommand ("force_centerview");
  666. ri.Cmd_RemoveCommand ("+mlook");
  667. ri.Cmd_RemoveCommand ("-mlook");
  668. }
  669. /*
  670. ===========
  671. IN_Commands
  672. ===========
  673. */
  674. void RW_IN_Commands (void)
  675. {
  676. int i;
  677. if (!mouse_avail)
  678. return;
  679. for (i=0 ; i<3 ; i++) {
  680. if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
  681. in_state->Key_Event_fp (K_MOUSE1 + i, true);
  682. if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
  683. in_state->Key_Event_fp (K_MOUSE1 + i, false);
  684. }
  685. mouse_oldbuttonstate = mouse_buttonstate;
  686. }
  687. /*
  688. ===========
  689. IN_Move
  690. ===========
  691. */
  692. void RW_IN_Move (usercmd_t *cmd)
  693. {
  694. if (!mouse_avail)
  695. return;
  696. if (m_filter->value)
  697. {
  698. mouse_x = (mx + old_mouse_x) * 0.5;
  699. mouse_y = (my + old_mouse_y) * 0.5;
  700. } else {
  701. mouse_x = mx;
  702. mouse_y = my;
  703. }
  704. old_mouse_x = mx;
  705. old_mouse_y = my;
  706. if (!mouse_x && !mouse_y)
  707. return;
  708. mouse_x *= sensitivity->value;
  709. mouse_y *= sensitivity->value;
  710. // add mouse X/Y movement to cmd
  711. if ( (*in_state->in_strafe_state & 1) ||
  712. (lookstrafe->value && mlooking ))
  713. cmd->sidemove += m_side->value * mouse_x;
  714. else
  715. in_state->viewangles[YAW] -= m_yaw->value * mouse_x;
  716. if ( (mlooking || freelook->value) &&
  717. !(*in_state->in_strafe_state & 1))
  718. {
  719. in_state->viewangles[PITCH] += m_pitch->value * mouse_y;
  720. }
  721. else
  722. {
  723. cmd->forwardmove -= m_forward->value * mouse_y;
  724. }
  725. mx = my = 0;
  726. }
  727. void RW_IN_Frame (void)
  728. {
  729. }
  730. void RW_IN_Activate(void)
  731. {
  732. }
  733. //===============================================================================
  734. /*
  735. ================
  736. Sys_MakeCodeWriteable
  737. ================
  738. */
  739. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  740. {
  741. int r;
  742. unsigned long addr;
  743. int psize = getpagesize();
  744. addr = (startaddr & ~(psize-1)) - psize;
  745. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  746. // addr, startaddr+length, length);
  747. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  748. if (r < 0)
  749. Sys_Error("Protection change failed\n");
  750. }