rw_x11.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. ** RW_X11.C
  3. **
  4. ** This file contains ALL Linux specific stuff having to do with the
  5. ** software refresh. When a port is being made the following functions
  6. ** must be implemented by the port:
  7. **
  8. ** SWimp_EndFrame
  9. ** SWimp_Init
  10. ** SWimp_InitGraphics
  11. ** SWimp_SetPalette
  12. ** SWimp_Shutdown
  13. ** SWimp_SwitchFullscreen
  14. */
  15. #include <ctype.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/ipc.h>
  24. #include <sys/shm.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/Xatom.h>
  28. #include <X11/keysym.h>
  29. #include <X11/extensions/XShm.h>
  30. #include "../ref_soft/r_local.h"
  31. #include "../client/keys.h"
  32. #include "../linux/rw_linux.h"
  33. /*****************************************************************************/
  34. static qboolean doShm;
  35. static Display *x_disp;
  36. static Colormap x_cmap;
  37. static Window x_win;
  38. static GC x_gc;
  39. static Visual *x_vis;
  40. static XVisualInfo *x_visinfo;
  41. //static XImage *x_image;
  42. #define STD_EVENT_MASK (StructureNotifyMask | KeyPressMask \
  43. | KeyReleaseMask | ExposureMask | PointerMotionMask | \
  44. ButtonPressMask | ButtonReleaseMask)
  45. static int x_shmeventtype;
  46. //static XShmSegmentInfo x_shminfo;
  47. static qboolean oktodraw = false;
  48. static qboolean X11_active = false;
  49. int XShmQueryExtension(Display *);
  50. int XShmGetEventBase(Display *);
  51. int current_framebuffer;
  52. static XImage *x_framebuffer[2] = { 0, 0 };
  53. static XShmSegmentInfo x_shminfo[2];
  54. struct
  55. {
  56. int key;
  57. int down;
  58. } keyq[64];
  59. int keyq_head=0;
  60. int keyq_tail=0;
  61. int config_notify=0;
  62. int config_notify_width;
  63. int config_notify_height;
  64. typedef unsigned short PIXEL;
  65. // Console variables that we need to access from this module
  66. /*****************************************************************************/
  67. /* MOUSE */
  68. /*****************************************************************************/
  69. // this is inside the renderer shared lib, so these are called from vid_so
  70. static qboolean mouse_avail;
  71. static int mouse_buttonstate;
  72. static int mouse_oldbuttonstate;
  73. static int mouse_x, mouse_y;
  74. static int old_mouse_x, old_mouse_y;
  75. static int mx, my;
  76. static float old_windowed_mouse;
  77. static int p_mouse_x, p_mouse_y;
  78. static cvar_t *_windowed_mouse;
  79. static cvar_t *m_filter;
  80. static cvar_t *in_mouse;
  81. static qboolean mlooking;
  82. // state struct passed in Init
  83. static in_state_t *in_state;
  84. static cvar_t *sensitivity;
  85. static cvar_t *lookstrafe;
  86. static cvar_t *m_side;
  87. static cvar_t *m_yaw;
  88. static cvar_t *m_pitch;
  89. static cvar_t *m_forward;
  90. static cvar_t *freelook;
  91. static void Force_CenterView_f (void)
  92. {
  93. in_state->viewangles[PITCH] = 0;
  94. }
  95. static void RW_IN_MLookDown (void)
  96. {
  97. mlooking = true;
  98. }
  99. static void RW_IN_MLookUp (void)
  100. {
  101. mlooking = false;
  102. in_state->IN_CenterView_fp ();
  103. }
  104. void RW_IN_Init(in_state_t *in_state_p)
  105. {
  106. int mtype;
  107. int i;
  108. in_state = in_state_p;
  109. // mouse variables
  110. _windowed_mouse = ri.Cvar_Get ("_windowed_mouse", "0", CVAR_ARCHIVE);
  111. m_filter = ri.Cvar_Get ("m_filter", "0", 0);
  112. in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
  113. freelook = ri.Cvar_Get( "freelook", "0", 0 );
  114. lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
  115. sensitivity = ri.Cvar_Get ("sensitivity", "3", 0);
  116. m_pitch = ri.Cvar_Get ("m_pitch", "0.022", 0);
  117. m_yaw = ri.Cvar_Get ("m_yaw", "0.022", 0);
  118. m_forward = ri.Cvar_Get ("m_forward", "1", 0);
  119. m_side = ri.Cvar_Get ("m_side", "0.8", 0);
  120. ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown);
  121. ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp);
  122. ri.Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  123. mouse_x = mouse_y = 0.0;
  124. mouse_avail = true;
  125. }
  126. void RW_IN_Shutdown(void)
  127. {
  128. mouse_avail = false;
  129. }
  130. /*
  131. ===========
  132. IN_Commands
  133. ===========
  134. */
  135. void RW_IN_Commands (void)
  136. {
  137. int i;
  138. if (!mouse_avail)
  139. return;
  140. for (i=0 ; i<3 ; i++) {
  141. if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
  142. in_state->Key_Event_fp (K_MOUSE1 + i, true);
  143. if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
  144. in_state->Key_Event_fp (K_MOUSE1 + i, false);
  145. }
  146. mouse_oldbuttonstate = mouse_buttonstate;
  147. }
  148. /*
  149. ===========
  150. IN_Move
  151. ===========
  152. */
  153. void RW_IN_Move (usercmd_t *cmd)
  154. {
  155. if (!mouse_avail)
  156. return;
  157. if (m_filter->value)
  158. {
  159. mouse_x = (mx + old_mouse_x) * 0.5;
  160. mouse_y = (my + old_mouse_y) * 0.5;
  161. } else {
  162. mouse_x = mx;
  163. mouse_y = my;
  164. }
  165. old_mouse_x = mx;
  166. old_mouse_y = my;
  167. if (!mouse_x && !mouse_y)
  168. return;
  169. mouse_x *= sensitivity->value;
  170. mouse_y *= sensitivity->value;
  171. // add mouse X/Y movement to cmd
  172. if ( (*in_state->in_strafe_state & 1) ||
  173. (lookstrafe->value && mlooking ))
  174. cmd->sidemove += m_side->value * mouse_x;
  175. else
  176. in_state->viewangles[YAW] -= m_yaw->value * mouse_x;
  177. if ( (mlooking || freelook->value) &&
  178. !(*in_state->in_strafe_state & 1))
  179. {
  180. in_state->viewangles[PITCH] += m_pitch->value * mouse_y;
  181. }
  182. else
  183. {
  184. cmd->forwardmove -= m_forward->value * mouse_y;
  185. }
  186. mx = my = 0;
  187. }
  188. void RW_IN_Frame (void)
  189. {
  190. }
  191. void RW_IN_Activate(void)
  192. {
  193. }
  194. /*****************************************************************************/
  195. static PIXEL st2d_8to16table[256];
  196. static int shiftmask_fl=0;
  197. static long r_shift,g_shift,b_shift;
  198. static unsigned long r_mask,g_mask,b_mask;
  199. void shiftmask_init()
  200. {
  201. unsigned int x;
  202. r_mask=x_vis->red_mask;
  203. g_mask=x_vis->green_mask;
  204. b_mask=x_vis->blue_mask;
  205. for(r_shift=-8,x=1;x<r_mask;x=x<<1)r_shift++;
  206. for(g_shift=-8,x=1;x<g_mask;x=x<<1)g_shift++;
  207. for(b_shift=-8,x=1;x<b_mask;x=x<<1)b_shift++;
  208. shiftmask_fl=1;
  209. }
  210. PIXEL xlib_rgb(int r,int g,int b)
  211. {
  212. PIXEL p;
  213. if(shiftmask_fl==0) shiftmask_init();
  214. p=0;
  215. if(r_shift>0) {
  216. p=(r<<(r_shift))&r_mask;
  217. } else if(r_shift<0) {
  218. p=(r>>(-r_shift))&r_mask;
  219. } else p|=(r&r_mask);
  220. if(g_shift>0) {
  221. p|=(g<<(g_shift))&g_mask;
  222. } else if(g_shift<0) {
  223. p|=(g>>(-g_shift))&g_mask;
  224. } else p|=(g&g_mask);
  225. if(b_shift>0) {
  226. p|=(b<<(b_shift))&b_mask;
  227. } else if(b_shift<0) {
  228. p|=(b>>(-b_shift))&b_mask;
  229. } else p|=(b&b_mask);
  230. return p;
  231. }
  232. void st2_fixup( XImage *framebuf, int x, int y, int width, int height)
  233. {
  234. int xi,yi;
  235. unsigned char *src;
  236. PIXEL *dest;
  237. if( (x<0)||(y<0) )return;
  238. for (yi = y; yi < (y+height); yi++) {
  239. src = &framebuf->data [yi * framebuf->bytes_per_line];
  240. dest = (PIXEL*)src;
  241. for(xi = (x+width-1); xi >= x; xi -= 8) {
  242. dest[xi ] = st2d_8to16table[src[xi ]];
  243. dest[xi-1] = st2d_8to16table[src[xi-1]];
  244. dest[xi-2] = st2d_8to16table[src[xi-2]];
  245. dest[xi-3] = st2d_8to16table[src[xi-3]];
  246. dest[xi-4] = st2d_8to16table[src[xi-4]];
  247. dest[xi-5] = st2d_8to16table[src[xi-5]];
  248. dest[xi-6] = st2d_8to16table[src[xi-6]];
  249. dest[xi-7] = st2d_8to16table[src[xi-7]];
  250. }
  251. }
  252. }
  253. // ========================================================================
  254. // makes a null cursor
  255. // ========================================================================
  256. static Cursor CreateNullCursor(Display *display, Window root)
  257. {
  258. Pixmap cursormask;
  259. XGCValues xgc;
  260. GC gc;
  261. XColor dummycolour;
  262. Cursor cursor;
  263. cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
  264. xgc.function = GXclear;
  265. gc = XCreateGC(display, cursormask, GCFunction, &xgc);
  266. XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
  267. dummycolour.pixel = 0;
  268. dummycolour.red = 0;
  269. dummycolour.flags = 04;
  270. cursor = XCreatePixmapCursor(display, cursormask, cursormask,
  271. &dummycolour,&dummycolour, 0,0);
  272. XFreePixmap(display,cursormask);
  273. XFreeGC(display,gc);
  274. return cursor;
  275. }
  276. void ResetFrameBuffer(void)
  277. {
  278. int mem;
  279. int pwidth;
  280. if (x_framebuffer[0])
  281. {
  282. free(x_framebuffer[0]->data);
  283. free(x_framebuffer[0]);
  284. }
  285. // alloc an extra line in case we want to wrap, and allocate the z-buffer
  286. pwidth = x_visinfo->depth / 8;
  287. if (pwidth == 3) pwidth = 4;
  288. mem = ((vid.width*pwidth+7)&~7) * vid.height;
  289. x_framebuffer[0] = XCreateImage( x_disp,
  290. x_vis,
  291. x_visinfo->depth,
  292. ZPixmap,
  293. 0,
  294. malloc(mem),
  295. vid.width, vid.height,
  296. 32,
  297. 0);
  298. if (!x_framebuffer[0])
  299. Sys_Error("VID: XCreateImage failed\n");
  300. vid.buffer = (byte*) (x_framebuffer[0]);
  301. }
  302. void ResetSharedFrameBuffers(void)
  303. {
  304. int size;
  305. int key;
  306. int minsize = getpagesize();
  307. int frm;
  308. for (frm=0 ; frm<2 ; frm++)
  309. {
  310. // free up old frame buffer memory
  311. if (x_framebuffer[frm])
  312. {
  313. XShmDetach(x_disp, &x_shminfo[frm]);
  314. free(x_framebuffer[frm]);
  315. shmdt(x_shminfo[frm].shmaddr);
  316. }
  317. // create the image
  318. x_framebuffer[frm] = XShmCreateImage( x_disp,
  319. x_vis,
  320. x_visinfo->depth,
  321. ZPixmap,
  322. 0,
  323. &x_shminfo[frm],
  324. vid.width,
  325. vid.height );
  326. // grab shared memory
  327. size = x_framebuffer[frm]->bytes_per_line
  328. * x_framebuffer[frm]->height;
  329. if (size < minsize)
  330. Sys_Error("VID: Window must use at least %d bytes\n", minsize);
  331. key = random();
  332. x_shminfo[frm].shmid = shmget((key_t)key, size, IPC_CREAT|0777);
  333. if (x_shminfo[frm].shmid==-1)
  334. Sys_Error("VID: Could not get any shared memory\n");
  335. // attach to the shared memory segment
  336. x_shminfo[frm].shmaddr =
  337. (void *) shmat(x_shminfo[frm].shmid, 0, 0);
  338. ri.Con_Printf(PRINT_ALL,
  339. "MITSHM shared memory (id=%d, addr=0x%lx)\n",
  340. x_shminfo[frm].shmid,
  341. (long) x_shminfo[frm].shmaddr);
  342. x_framebuffer[frm]->data = x_shminfo[frm].shmaddr;
  343. // get the X server to attach to it
  344. if (!XShmAttach(x_disp, &x_shminfo[frm]))
  345. Sys_Error("VID: XShmAttach() failed\n");
  346. XSync(x_disp, 0);
  347. shmctl(x_shminfo[frm].shmid, IPC_RMID, 0);
  348. }
  349. }
  350. // ========================================================================
  351. // Tragic death handler
  352. // ========================================================================
  353. void TragicDeath(int signal_num)
  354. {
  355. XAutoRepeatOn(x_disp);
  356. XCloseDisplay(x_disp);
  357. Sys_Error("This death brought to you by the number %d\n", signal_num);
  358. }
  359. int XLateKey(XKeyEvent *ev)
  360. {
  361. int key;
  362. char buf[64];
  363. KeySym keysym;
  364. key = 0;
  365. XLookupString(ev, buf, sizeof buf, &keysym, 0);
  366. switch(keysym)
  367. {
  368. case XK_KP_Page_Up: key = K_KP_PGUP; break;
  369. case XK_Page_Up: key = K_PGUP; break;
  370. case XK_KP_Page_Down: key = K_KP_PGDN; break;
  371. case XK_Page_Down: key = K_PGDN; break;
  372. case XK_KP_Home: key = K_KP_HOME; break;
  373. case XK_Home: key = K_HOME; break;
  374. case XK_KP_End: key = K_KP_END; break;
  375. case XK_End: key = K_END; break;
  376. case XK_KP_Left: key = K_KP_LEFTARROW; break;
  377. case XK_Left: key = K_LEFTARROW; break;
  378. case XK_KP_Right: key = K_KP_RIGHTARROW; break;
  379. case XK_Right: key = K_RIGHTARROW; break;
  380. case XK_KP_Down: key = K_KP_DOWNARROW; break;
  381. case XK_Down: key = K_DOWNARROW; break;
  382. case XK_KP_Up: key = K_KP_UPARROW; break;
  383. case XK_Up: key = K_UPARROW; break;
  384. case XK_Escape: key = K_ESCAPE; break;
  385. case XK_KP_Enter: key = K_KP_ENTER; break;
  386. case XK_Return: key = K_ENTER; break;
  387. case XK_Tab: key = K_TAB; break;
  388. case XK_F1: key = K_F1; break;
  389. case XK_F2: key = K_F2; break;
  390. case XK_F3: key = K_F3; break;
  391. case XK_F4: key = K_F4; break;
  392. case XK_F5: key = K_F5; break;
  393. case XK_F6: key = K_F6; break;
  394. case XK_F7: key = K_F7; break;
  395. case XK_F8: key = K_F8; break;
  396. case XK_F9: key = K_F9; break;
  397. case XK_F10: key = K_F10; break;
  398. case XK_F11: key = K_F11; break;
  399. case XK_F12: key = K_F12; break;
  400. case XK_BackSpace: key = K_BACKSPACE; break;
  401. case XK_KP_Delete: key = K_KP_DEL; break;
  402. case XK_Delete: key = K_DEL; break;
  403. case XK_Pause: key = K_PAUSE; break;
  404. case XK_Shift_L:
  405. case XK_Shift_R: key = K_SHIFT; break;
  406. case XK_Execute:
  407. case XK_Control_L:
  408. case XK_Control_R: key = K_CTRL; break;
  409. case XK_Alt_L:
  410. case XK_Meta_L:
  411. case XK_Alt_R:
  412. case XK_Meta_R: key = K_ALT; break;
  413. case XK_KP_Begin: key = K_KP_5; break;
  414. case XK_Insert:key = K_INS; break;
  415. case XK_KP_Insert: key = K_KP_INS; break;
  416. case XK_KP_Multiply: key = '*'; break;
  417. case XK_KP_Add: key = K_KP_PLUS; break;
  418. case XK_KP_Subtract: key = K_KP_MINUS; break;
  419. case XK_KP_Divide: key = K_KP_SLASH; break;
  420. #if 0
  421. case 0x021: key = '1';break;/* [!] */
  422. case 0x040: key = '2';break;/* [@] */
  423. case 0x023: key = '3';break;/* [#] */
  424. case 0x024: key = '4';break;/* [$] */
  425. case 0x025: key = '5';break;/* [%] */
  426. case 0x05e: key = '6';break;/* [^] */
  427. case 0x026: key = '7';break;/* [&] */
  428. case 0x02a: key = '8';break;/* [*] */
  429. case 0x028: key = '9';;break;/* [(] */
  430. case 0x029: key = '0';break;/* [)] */
  431. case 0x05f: key = '-';break;/* [_] */
  432. case 0x02b: key = '=';break;/* [+] */
  433. case 0x07c: key = '\'';break;/* [|] */
  434. case 0x07d: key = '[';break;/* [}] */
  435. case 0x07b: key = ']';break;/* [{] */
  436. case 0x022: key = '\'';break;/* ["] */
  437. case 0x03a: key = ';';break;/* [:] */
  438. case 0x03f: key = '/';break;/* [?] */
  439. case 0x03e: key = '.';break;/* [>] */
  440. case 0x03c: key = ',';break;/* [<] */
  441. #endif
  442. default:
  443. key = *(unsigned char*)buf;
  444. if (key >= 'A' && key <= 'Z')
  445. key = key - 'A' + 'a';
  446. break;
  447. }
  448. return key;
  449. }
  450. void GetEvent(void)
  451. {
  452. XEvent x_event;
  453. int b;
  454. XNextEvent(x_disp, &x_event);
  455. switch(x_event.type) {
  456. case KeyPress:
  457. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  458. keyq[keyq_head].down = true;
  459. keyq_head = (keyq_head + 1) & 63;
  460. break;
  461. case KeyRelease:
  462. keyq[keyq_head].key = XLateKey(&x_event.xkey);
  463. keyq[keyq_head].down = false;
  464. keyq_head = (keyq_head + 1) & 63;
  465. break;
  466. case MotionNotify:
  467. if (_windowed_mouse->value) {
  468. mx += ((int)x_event.xmotion.x - (int)(vid.width/2));
  469. my += ((int)x_event.xmotion.y - (int)(vid.height/2));
  470. /* move the mouse to the window center again */
  471. XSelectInput(x_disp,x_win, STD_EVENT_MASK & ~PointerMotionMask);
  472. XWarpPointer(x_disp,None,x_win,0,0,0,0,
  473. (vid.width/2),(vid.height/2));
  474. XSelectInput(x_disp,x_win, STD_EVENT_MASK);
  475. } else {
  476. mx = ((int)x_event.xmotion.x - (int)p_mouse_x);
  477. my = ((int)x_event.xmotion.y - (int)p_mouse_y);
  478. p_mouse_x=x_event.xmotion.x;
  479. p_mouse_y=x_event.xmotion.y;
  480. }
  481. break;
  482. case ButtonPress:
  483. b=-1;
  484. if (x_event.xbutton.button == 1)
  485. b = 0;
  486. else if (x_event.xbutton.button == 2)
  487. b = 2;
  488. else if (x_event.xbutton.button == 3)
  489. b = 1;
  490. if (b>=0)
  491. mouse_buttonstate |= 1<<b;
  492. break;
  493. case ButtonRelease:
  494. b=-1;
  495. if (x_event.xbutton.button == 1)
  496. b = 0;
  497. else if (x_event.xbutton.button == 2)
  498. b = 2;
  499. else if (x_event.xbutton.button == 3)
  500. b = 1;
  501. if (b>=0)
  502. mouse_buttonstate &= ~(1<<b);
  503. break;
  504. case ConfigureNotify:
  505. config_notify_width = x_event.xconfigure.width;
  506. config_notify_height = x_event.xconfigure.height;
  507. config_notify = 1;
  508. break;
  509. default:
  510. if (doShm && x_event.type == x_shmeventtype)
  511. oktodraw = true;
  512. }
  513. if (old_windowed_mouse != _windowed_mouse->value) {
  514. old_windowed_mouse = _windowed_mouse->value;
  515. if (!_windowed_mouse->value) {
  516. /* ungrab the pointer */
  517. XUngrabPointer(x_disp,CurrentTime);
  518. } else {
  519. /* grab the pointer */
  520. XGrabPointer(x_disp,x_win,True,0,GrabModeAsync,
  521. GrabModeAsync,x_win,None,CurrentTime);
  522. }
  523. }
  524. }
  525. /*****************************************************************************/
  526. /*
  527. ** SWimp_Init
  528. **
  529. ** This routine is responsible for initializing the implementation
  530. ** specific stuff in a software rendering subsystem.
  531. */
  532. int SWimp_Init( void *hInstance, void *wndProc )
  533. {
  534. // open the display
  535. x_disp = XOpenDisplay(0);
  536. if (!x_disp)
  537. {
  538. if (getenv("DISPLAY"))
  539. Sys_Error("VID: Could not open display [%s]\n",
  540. getenv("DISPLAY"));
  541. else
  542. Sys_Error("VID: Could not open local display\n");
  543. }
  544. // catch signals so i can turn on auto-repeat
  545. {
  546. struct sigaction sa;
  547. sigaction(SIGINT, 0, &sa);
  548. sa.sa_handler = TragicDeath;
  549. sigaction(SIGINT, &sa, 0);
  550. sigaction(SIGTERM, &sa, 0);
  551. }
  552. return true;
  553. }
  554. /*
  555. ** SWimp_InitGraphics
  556. **
  557. ** This initializes the software refresh's implementation specific
  558. ** graphics subsystem. In the case of Windows it creates DIB or
  559. ** DDRAW surfaces.
  560. **
  561. ** The necessary width and height parameters are grabbed from
  562. ** vid.width and vid.height.
  563. */
  564. static qboolean SWimp_InitGraphics( qboolean fullscreen )
  565. {
  566. int pnum, i;
  567. XVisualInfo template;
  568. int num_visuals;
  569. int template_mask;
  570. srandom(getpid());
  571. // free resources in use
  572. SWimp_Shutdown ();
  573. // let the sound and input subsystems know about the new window
  574. ri.Vid_NewWindow (vid.width, vid.height);
  575. XAutoRepeatOff(x_disp);
  576. // for debugging only
  577. XSynchronize(x_disp, True);
  578. // check for command-line window size
  579. template_mask = 0;
  580. #if 0
  581. // specify a visual id
  582. if ((pnum=COM_CheckParm("-visualid")))
  583. {
  584. if (pnum >= com_argc-1)
  585. Sys_Error("VID: -visualid <id#>\n");
  586. template.visualid = Q_atoi(com_argv[pnum+1]);
  587. template_mask = VisualIDMask;
  588. }
  589. // If not specified, use default visual
  590. else
  591. #endif
  592. {
  593. int screen;
  594. screen = XDefaultScreen(x_disp);
  595. template.visualid =
  596. XVisualIDFromVisual(XDefaultVisual(x_disp, screen));
  597. template_mask = VisualIDMask;
  598. }
  599. // pick a visual- warn if more than one was available
  600. x_visinfo = XGetVisualInfo(x_disp, template_mask, &template, &num_visuals);
  601. if (num_visuals > 1)
  602. {
  603. printf("Found more than one visual id at depth %d:\n", template.depth);
  604. for (i=0 ; i<num_visuals ; i++)
  605. printf(" -visualid %d\n", (int)(x_visinfo[i].visualid));
  606. }
  607. else if (num_visuals == 0)
  608. {
  609. if (template_mask == VisualIDMask)
  610. Sys_Error("VID: Bad visual id %d\n", template.visualid);
  611. else
  612. Sys_Error("VID: No visuals at depth %d\n", template.depth);
  613. }
  614. #if 0
  615. if (verbose)
  616. {
  617. printf("Using visualid %d:\n", (int)(x_visinfo->visualid));
  618. printf(" screen %d\n", x_visinfo->screen);
  619. printf(" red_mask 0x%x\n", (int)(x_visinfo->red_mask));
  620. printf(" green_mask 0x%x\n", (int)(x_visinfo->green_mask));
  621. printf(" blue_mask 0x%x\n", (int)(x_visinfo->blue_mask));
  622. printf(" colormap_size %d\n", x_visinfo->colormap_size);
  623. printf(" bits_per_rgb %d\n", x_visinfo->bits_per_rgb);
  624. }
  625. #endif
  626. x_vis = x_visinfo->visual;
  627. // setup attributes for main window
  628. {
  629. int attribmask = CWEventMask | CWColormap | CWBorderPixel;
  630. XSetWindowAttributes attribs;
  631. Colormap tmpcmap;
  632. tmpcmap = XCreateColormap(x_disp, XRootWindow(x_disp,
  633. x_visinfo->screen), x_vis, AllocNone);
  634. attribs.event_mask = STD_EVENT_MASK;
  635. attribs.border_pixel = 0;
  636. attribs.colormap = tmpcmap;
  637. // create the main window
  638. x_win = XCreateWindow( x_disp,
  639. XRootWindow(x_disp, x_visinfo->screen),
  640. 0, 0, // x, y
  641. vid.width, vid.height,
  642. 0, // borderwidth
  643. x_visinfo->depth,
  644. InputOutput,
  645. x_vis,
  646. attribmask,
  647. &attribs );
  648. XStoreName(x_disp, x_win, "Quake II");
  649. if (x_visinfo->class != TrueColor)
  650. XFreeColormap(x_disp, tmpcmap);
  651. }
  652. if (x_visinfo->depth == 8)
  653. {
  654. // create and upload the palette
  655. if (x_visinfo->class == PseudoColor)
  656. {
  657. x_cmap = XCreateColormap(x_disp, x_win, x_vis, AllocAll);
  658. XSetWindowColormap(x_disp, x_win, x_cmap);
  659. }
  660. }
  661. // inviso cursor
  662. XDefineCursor(x_disp, x_win, CreateNullCursor(x_disp, x_win));
  663. // create the GC
  664. {
  665. XGCValues xgcvalues;
  666. int valuemask = GCGraphicsExposures;
  667. xgcvalues.graphics_exposures = False;
  668. x_gc = XCreateGC(x_disp, x_win, valuemask, &xgcvalues );
  669. }
  670. // map the window
  671. XMapWindow(x_disp, x_win);
  672. // wait for first exposure event
  673. {
  674. XEvent event;
  675. do
  676. {
  677. XNextEvent(x_disp, &event);
  678. if (event.type == Expose && !event.xexpose.count)
  679. oktodraw = true;
  680. } while (!oktodraw);
  681. }
  682. // now safe to draw
  683. // even if MITSHM is available, make sure it's a local connection
  684. if (XShmQueryExtension(x_disp))
  685. {
  686. char *displayname;
  687. doShm = true;
  688. displayname = (char *) getenv("DISPLAY");
  689. if (displayname)
  690. {
  691. char *d = displayname;
  692. while (*d && (*d != ':')) d++;
  693. if (*d) *d = 0;
  694. if (!(!strcasecmp(displayname, "unix") || !*displayname))
  695. doShm = false;
  696. }
  697. }
  698. if (doShm)
  699. {
  700. x_shmeventtype = XShmGetEventBase(x_disp) + ShmCompletion;
  701. ResetSharedFrameBuffers();
  702. }
  703. else
  704. ResetFrameBuffer();
  705. current_framebuffer = 0;
  706. vid.rowbytes = x_framebuffer[0]->bytes_per_line;
  707. vid.buffer = x_framebuffer[0]->data;
  708. // XSynchronize(x_disp, False);
  709. X11_active = true;
  710. return true;
  711. }
  712. /*
  713. ** SWimp_EndFrame
  714. **
  715. ** This does an implementation specific copy from the backbuffer to the
  716. ** front buffer. In the Win32 case it uses BitBlt or BltFast depending
  717. ** on whether we're using DIB sections/GDI or DDRAW.
  718. */
  719. void SWimp_EndFrame (void)
  720. {
  721. // if the window changes dimension, skip this frame
  722. #if 0
  723. if (config_notify)
  724. {
  725. fprintf(stderr, "config notify\n");
  726. config_notify = 0;
  727. vid.width = config_notify_width & ~7;
  728. vid.height = config_notify_height;
  729. if (doShm)
  730. ResetSharedFrameBuffers();
  731. else
  732. ResetFrameBuffer();
  733. vid.rowbytes = x_framebuffer[0]->bytes_per_line;
  734. vid.buffer = x_framebuffer[current_framebuffer]->data;
  735. vid.recalc_refdef = 1; // force a surface cache flush
  736. Con_CheckResize();
  737. Con_Clear_f();
  738. return;
  739. }
  740. #endif
  741. if (doShm)
  742. {
  743. if (x_visinfo->depth != 8)
  744. st2_fixup( x_framebuffer[current_framebuffer],
  745. 0, 0, vid.width, vid.height);
  746. if (!XShmPutImage(x_disp, x_win, x_gc,
  747. x_framebuffer[current_framebuffer], 0, 0,
  748. 0, 0, vid.width, vid.height, True))
  749. Sys_Error("VID_Update: XShmPutImage failed\n");
  750. oktodraw = false;
  751. while (!oktodraw)
  752. GetEvent();
  753. current_framebuffer = !current_framebuffer;
  754. vid.buffer = x_framebuffer[current_framebuffer]->data;
  755. XSync(x_disp, False);
  756. }
  757. else
  758. {
  759. if (x_visinfo->depth != 8)
  760. st2_fixup( x_framebuffer[current_framebuffer],
  761. 0, 0, vid.width, vid.height);
  762. XPutImage(x_disp, x_win, x_gc, x_framebuffer[0],
  763. 0, 0, 0, 0, vid.width, vid.height);
  764. XSync(x_disp, False);
  765. }
  766. }
  767. /*
  768. ** SWimp_SetMode
  769. */
  770. rserr_t SWimp_SetMode( int *pwidth, int *pheight, int mode, qboolean fullscreen )
  771. {
  772. rserr_t retval = rserr_ok;
  773. ri.Con_Printf (PRINT_ALL, "setting mode %d:", mode );
  774. if ( !ri.Vid_GetModeInfo( pwidth, pheight, mode ) )
  775. {
  776. ri.Con_Printf( PRINT_ALL, " invalid mode\n" );
  777. return rserr_invalid_mode;
  778. }
  779. ri.Con_Printf( PRINT_ALL, " %d %d\n", *pwidth, *pheight);
  780. if ( !SWimp_InitGraphics( false ) ) {
  781. // failed to set a valid mode in windowed mode
  782. return rserr_invalid_mode;
  783. }
  784. R_GammaCorrectAndSetPalette( ( const unsigned char * ) d_8to24table );
  785. return retval;
  786. }
  787. /*
  788. ** SWimp_SetPalette
  789. **
  790. ** System specific palette setting routine. A NULL palette means
  791. ** to use the existing palette. The palette is expected to be in
  792. ** a padded 4-byte xRGB format.
  793. */
  794. void SWimp_SetPalette( const unsigned char *palette )
  795. {
  796. int i;
  797. XColor colors[256];
  798. if (!X11_active)
  799. return;
  800. if ( !palette )
  801. palette = ( const unsigned char * ) sw_state.currentpalette;
  802. for(i=0;i<256;i++)
  803. st2d_8to16table[i]= xlib_rgb(palette[i*4],
  804. palette[i*4+1],palette[i*4+2]);
  805. if (x_visinfo->class == PseudoColor && x_visinfo->depth == 8)
  806. {
  807. for (i=0 ; i<256 ; i++)
  808. {
  809. colors[i].pixel = i;
  810. colors[i].flags = DoRed|DoGreen|DoBlue;
  811. colors[i].red = palette[i*4] * 257;
  812. colors[i].green = palette[i*4+1] * 257;
  813. colors[i].blue = palette[i*4+2] * 257;
  814. }
  815. XStoreColors(x_disp, x_cmap, colors, 256);
  816. }
  817. }
  818. /*
  819. ** SWimp_Shutdown
  820. **
  821. ** System specific graphics subsystem shutdown routine. Destroys
  822. ** DIBs or DDRAW surfaces as appropriate.
  823. */
  824. void SWimp_Shutdown( void )
  825. {
  826. int i;
  827. if (!X11_active)
  828. return;
  829. if (doShm) {
  830. for (i = 0; i < 2; i++)
  831. if (x_framebuffer[i]) {
  832. XShmDetach(x_disp, &x_shminfo[i]);
  833. free(x_framebuffer[i]);
  834. shmdt(x_shminfo[i].shmaddr);
  835. x_framebuffer[i] = NULL;
  836. }
  837. } else if (x_framebuffer[0]) {
  838. free(x_framebuffer[0]->data);
  839. free(x_framebuffer[0]);
  840. x_framebuffer[0] = NULL;
  841. }
  842. XDestroyWindow( x_disp, x_win );
  843. XAutoRepeatOn(x_disp);
  844. // XCloseDisplay(x_disp);
  845. X11_active = false;
  846. }
  847. /*
  848. ** SWimp_AppActivate
  849. */
  850. void SWimp_AppActivate( qboolean active )
  851. {
  852. }
  853. //===============================================================================
  854. /*
  855. ================
  856. Sys_MakeCodeWriteable
  857. ================
  858. */
  859. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  860. {
  861. int r;
  862. unsigned long addr;
  863. int psize = getpagesize();
  864. addr = (startaddr & ~(psize-1)) - psize;
  865. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  866. // addr, startaddr+length, length);
  867. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  868. if (r < 0)
  869. Sys_Error("Protection change failed\n");
  870. }
  871. /*****************************************************************************/
  872. /* KEYBOARD */
  873. /*****************************************************************************/
  874. Key_Event_fp_t Key_Event_fp;
  875. void KBD_Init(Key_Event_fp_t fp)
  876. {
  877. Key_Event_fp = fp;
  878. }
  879. void KBD_Update(void)
  880. {
  881. // get events from x server
  882. if (x_disp)
  883. {
  884. while (XPending(x_disp))
  885. GetEvent();
  886. while (keyq_head != keyq_tail)
  887. {
  888. Key_Event_fp(keyq[keyq_tail].key, keyq[keyq_tail].down);
  889. keyq_tail = (keyq_tail + 1) & 63;
  890. }
  891. }
  892. }
  893. void KBD_Close(void)
  894. {
  895. }