rw_in_svgalib.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <termios.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/stat.h>
  4. #include <sys/vt.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include <sys/mman.h>
  9. #include <asm/io.h>
  10. #include "vga.h"
  11. #include "vgakeyboard.h"
  12. #include "vgamouse.h"
  13. #include "../ref_soft/r_local.h"
  14. #include "../client/keys.h"
  15. #include "../linux/rw_linux.h"
  16. /*****************************************************************************/
  17. /* KEYBOARD */
  18. /*****************************************************************************/
  19. static unsigned char scantokey[128];
  20. Key_Event_fp_t Key_Event_fp;
  21. static void keyhandler(int scancode, int state)
  22. {
  23. int sc;
  24. sc = scancode & 0x7f;
  25. //ri.Con_Printf(PRINT_ALL, "scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
  26. Key_Event_fp(scantokey[sc], state == KEY_EVENTPRESS);
  27. }
  28. void KBD_Init(Key_Event_fp_t fp)
  29. {
  30. int i;
  31. Key_Event_fp = fp;
  32. for (i=0 ; i<128 ; i++)
  33. scantokey[i] = ' ';
  34. scantokey[ 1] = K_ESCAPE;
  35. scantokey[ 2] = '1';
  36. scantokey[ 3] = '2';
  37. scantokey[ 4] = '3';
  38. scantokey[ 5] = '4';
  39. scantokey[ 6] = '5';
  40. scantokey[ 7] = '6';
  41. scantokey[ 8] = '7';
  42. scantokey[ 9] = '8';
  43. scantokey[ 10] = '9';
  44. scantokey[ 11] = '0';
  45. scantokey[ 12] = '-';
  46. scantokey[ 13] = '=';
  47. scantokey[ 14] = K_BACKSPACE;
  48. scantokey[ 15] = K_TAB;
  49. scantokey[ 16] = 'q';
  50. scantokey[ 17] = 'w';
  51. scantokey[ 18] = 'e';
  52. scantokey[ 19] = 'r';
  53. scantokey[ 20] = 't';
  54. scantokey[ 21] = 'y';
  55. scantokey[ 22] = 'u';
  56. scantokey[ 23] = 'i';
  57. scantokey[ 24] = 'o';
  58. scantokey[ 25] = 'p';
  59. scantokey[ 26] = '[';
  60. scantokey[ 27] = ']';
  61. scantokey[ 28] = K_ENTER;
  62. scantokey[ 29] = K_CTRL; //left
  63. scantokey[ 30] = 'a';
  64. scantokey[ 31] = 's';
  65. scantokey[ 32] = 'd';
  66. scantokey[ 33] = 'f';
  67. scantokey[ 34] = 'g';
  68. scantokey[ 35] = 'h';
  69. scantokey[ 36] = 'j';
  70. scantokey[ 37] = 'k';
  71. scantokey[ 38] = 'l';
  72. scantokey[ 39] = ';';
  73. scantokey[ 40] = '\'';
  74. scantokey[ 41] = '`';
  75. scantokey[ 42] = K_SHIFT; //left
  76. scantokey[ 43] = '\\';
  77. scantokey[ 44] = 'z';
  78. scantokey[ 45] = 'x';
  79. scantokey[ 46] = 'c';
  80. scantokey[ 47] = 'v';
  81. scantokey[ 48] = 'b';
  82. scantokey[ 49] = 'n';
  83. scantokey[ 50] = 'm';
  84. scantokey[ 51] = ',';
  85. scantokey[ 52] = '.';
  86. scantokey[ 53] = '/';
  87. scantokey[ 54] = K_SHIFT; //right
  88. scantokey[ 55] = '*'; //keypad
  89. scantokey[ 56] = K_ALT; //left
  90. scantokey[ 57] = ' ';
  91. // 58 caps lock
  92. scantokey[ 59] = K_F1;
  93. scantokey[ 60] = K_F2;
  94. scantokey[ 61] = K_F3;
  95. scantokey[ 62] = K_F4;
  96. scantokey[ 63] = K_F5;
  97. scantokey[ 64] = K_F6;
  98. scantokey[ 65] = K_F7;
  99. scantokey[ 66] = K_F8;
  100. scantokey[ 67] = K_F9;
  101. scantokey[ 68] = K_F10;
  102. // 69 numlock
  103. // 70 scrollock
  104. scantokey[ 71] = K_KP_HOME;
  105. scantokey[ 72] = K_KP_UPARROW;
  106. scantokey[ 73] = K_KP_PGUP;
  107. scantokey[ 74] = K_KP_MINUS;
  108. scantokey[ 75] = K_KP_LEFTARROW;
  109. scantokey[ 76] = K_KP_5;
  110. scantokey[ 77] = K_KP_RIGHTARROW;
  111. scantokey[ 79] = K_KP_END;
  112. scantokey[ 78] = K_KP_PLUS;
  113. scantokey[ 80] = K_KP_DOWNARROW;
  114. scantokey[ 81] = K_KP_PGDN;
  115. scantokey[ 82] = K_KP_INS;
  116. scantokey[ 83] = K_KP_DEL;
  117. // 84 to 86 not used
  118. scantokey[ 87] = K_F11;
  119. scantokey[ 88] = K_F12;
  120. // 89 to 95 not used
  121. scantokey[ 96] = K_KP_ENTER; //keypad enter
  122. scantokey[ 97] = K_CTRL; //right
  123. scantokey[ 98] = K_KP_SLASH;
  124. scantokey[ 99] = K_F12; // print screen, bind to screenshot by default
  125. scantokey[100] = K_ALT; // right
  126. scantokey[101] = K_PAUSE; // break
  127. scantokey[102] = K_HOME;
  128. scantokey[103] = K_UPARROW;
  129. scantokey[104] = K_PGUP;
  130. scantokey[105] = K_LEFTARROW;
  131. scantokey[106] = K_RIGHTARROW;
  132. scantokey[107] = K_END;
  133. scantokey[108] = K_DOWNARROW;
  134. scantokey[109] = K_PGDN;
  135. scantokey[110] = K_INS;
  136. scantokey[111] = K_DEL;
  137. scantokey[119] = K_PAUSE;
  138. if (keyboard_init())
  139. Sys_Error("keyboard_init() failed");
  140. keyboard_seteventhandler(keyhandler);
  141. keyboard_translatekeys(DONT_CATCH_CTRLC);
  142. }
  143. void KBD_Update(void)
  144. {
  145. while (keyboard_update())
  146. ;
  147. }
  148. void KBD_Close(void)
  149. {
  150. keyboard_close();
  151. }
  152. /*****************************************************************************/
  153. /* MOUSE */
  154. /*****************************************************************************/
  155. // this is inside the renderer shared lib, so these are called from vid_so
  156. static qboolean UseMouse = true;
  157. static int mouserate = MOUSE_DEFAULTSAMPLERATE;
  158. static int mouse_buttons;
  159. static int mouse_buttonstate;
  160. static int mouse_oldbuttonstate;
  161. static float mouse_x, mouse_y;
  162. static float old_mouse_x, old_mouse_y;
  163. static int mx, my;
  164. static cvar_t *m_filter;
  165. static cvar_t *in_mouse;
  166. static cvar_t *mdev;
  167. static cvar_t *mrate;
  168. static qboolean mlooking;
  169. // state struct passed in Init
  170. static in_state_t *in_state;
  171. static cvar_t *sensitivity;
  172. static cvar_t *lookstrafe;
  173. static cvar_t *m_side;
  174. static cvar_t *m_yaw;
  175. static cvar_t *m_pitch;
  176. static cvar_t *m_forward;
  177. static cvar_t *freelook;
  178. static void Force_CenterView_f (void)
  179. {
  180. in_state->viewangles[PITCH] = 0;
  181. }
  182. static void RW_IN_MLookDown (void)
  183. {
  184. mlooking = true;
  185. }
  186. static void RW_IN_MLookUp (void)
  187. {
  188. mlooking = false;
  189. in_state->IN_CenterView_fp ();
  190. }
  191. static void mousehandler(int buttonstate, int dx, int dy)
  192. {
  193. mouse_buttonstate = buttonstate;
  194. mx += dx;
  195. my += dy;
  196. }
  197. void RW_IN_Init(in_state_t *in_state_p)
  198. {
  199. int mtype;
  200. int i;
  201. in_state = in_state_p;
  202. // mouse variables
  203. m_filter = ri.Cvar_Get ("m_filter", "0", 0);
  204. in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
  205. freelook = ri.Cvar_Get( "freelook", "0", 0 );
  206. lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
  207. sensitivity = ri.Cvar_Get ("sensitivity", "3", 0);
  208. m_pitch = ri.Cvar_Get ("m_pitch", "0.022", 0);
  209. m_yaw = ri.Cvar_Get ("m_yaw", "0.022", 0);
  210. m_forward = ri.Cvar_Get ("m_forward", "1", 0);
  211. m_side = ri.Cvar_Get ("m_side", "0.8", 0);
  212. ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown);
  213. ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp);
  214. ri.Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  215. mouse_buttons = 3;
  216. mtype = vga_getmousetype();
  217. mdev = ri.Cvar_Get ("mdev", "/dev/mouse", 0);
  218. mrate = ri.Cvar_Get ("mrate", "1200", 0);
  219. // printf("Mouse: dev=%s,type=%s,speed=%d\n",
  220. // mousedev, mice[mtype].name, mouserate);
  221. if (mouse_init(mdev->string, mtype, (int)mrate->value))
  222. {
  223. ri.Con_Printf(PRINT_ALL, "No mouse found\n");
  224. UseMouse = false;
  225. }
  226. else
  227. mouse_seteventhandler(mousehandler);
  228. }
  229. void RW_IN_Shutdown(void)
  230. {
  231. mouse_close();
  232. }
  233. /*
  234. ===========
  235. IN_Commands
  236. ===========
  237. */
  238. void RW_IN_Commands (void)
  239. {
  240. if (!UseMouse)
  241. return;
  242. // poll mouse values
  243. mouse_update();
  244. // perform button actions
  245. if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  246. !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  247. in_state->Key_Event_fp (K_MOUSE1, true);
  248. else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  249. (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  250. in_state->Key_Event_fp (K_MOUSE1, false);
  251. if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  252. !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  253. in_state->Key_Event_fp (K_MOUSE2, true);
  254. else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  255. (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  256. in_state->Key_Event_fp (K_MOUSE2, false);
  257. if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  258. !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  259. Key_Event_fp (K_MOUSE3, true);
  260. else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  261. (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  262. in_state->Key_Event_fp (K_MOUSE3, false);
  263. mouse_oldbuttonstate = mouse_buttonstate;
  264. }
  265. /*
  266. ===========
  267. IN_Move
  268. ===========
  269. */
  270. void RW_IN_Move (usercmd_t *cmd)
  271. {
  272. if (!UseMouse)
  273. return;
  274. // poll mouse values
  275. mouse_update();
  276. if (m_filter->value)
  277. {
  278. mouse_x = (mx + old_mouse_x) * 0.5;
  279. mouse_y = (my + old_mouse_y) * 0.5;
  280. }
  281. else
  282. {
  283. mouse_x = mx;
  284. mouse_y = my;
  285. }
  286. old_mouse_x = mx;
  287. old_mouse_y = my;
  288. if (!mx && !my)
  289. return;
  290. mx = my = 0; // clear for next update
  291. mouse_x *= sensitivity->value;
  292. mouse_y *= sensitivity->value;
  293. // add mouse X/Y movement to cmd
  294. if ( (*in_state->in_strafe_state & 1) ||
  295. (lookstrafe->value && mlooking ))
  296. cmd->sidemove += m_side->value * mouse_x;
  297. else
  298. in_state->viewangles[YAW] -= m_yaw->value * mouse_x;
  299. if ( (mlooking || freelook->value) &&
  300. !(*in_state->in_strafe_state & 1))
  301. {
  302. in_state->viewangles[PITCH] += m_pitch->value * mouse_y;
  303. }
  304. else
  305. {
  306. cmd->forwardmove -= m_forward->value * mouse_y;
  307. }
  308. }
  309. void RW_IN_Frame (void)
  310. {
  311. }
  312. void RW_IN_Activate(void)
  313. {
  314. }