posix_input.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #include "posix_public.h"
  22. typedef struct poll_keyboard_event_s
  23. {
  24. int key;
  25. bool state;
  26. } poll_keyboard_event_t;
  27. typedef struct poll_mouse_event_s
  28. {
  29. int action;
  30. int value;
  31. } poll_mouse_event_t;
  32. #define MAX_POLL_EVENTS 50
  33. #define POLL_EVENTS_HEADROOM 2 // some situations require to add several events
  34. static poll_keyboard_event_t poll_events_keyboard[MAX_POLL_EVENTS + POLL_EVENTS_HEADROOM];
  35. static int poll_keyboard_event_count;
  36. static poll_mouse_event_t poll_events_mouse[MAX_POLL_EVENTS + POLL_EVENTS_HEADROOM];
  37. static int poll_mouse_event_count;
  38. /*
  39. ==========
  40. Posix_AddKeyboardPollEvent
  41. ==========
  42. */
  43. bool Posix_AddKeyboardPollEvent(int key, bool state) {
  44. if (poll_keyboard_event_count >= MAX_POLL_EVENTS + POLL_EVENTS_HEADROOM)
  45. common->FatalError( "poll_keyboard_event_count exceeded MAX_POLL_EVENT + POLL_EVENTS_HEADROOM\n");
  46. poll_events_keyboard[poll_keyboard_event_count].key = key;
  47. poll_events_keyboard[poll_keyboard_event_count++].state = state;
  48. if (poll_keyboard_event_count >= MAX_POLL_EVENTS) {
  49. common->DPrintf("WARNING: reached MAX_POLL_EVENT poll_keyboard_event_count\n");
  50. return false;
  51. }
  52. return true;
  53. }
  54. /*
  55. ==========
  56. Posix_AddMousePollEvent
  57. ==========
  58. */
  59. bool Posix_AddMousePollEvent(int action, int value) {
  60. if (poll_mouse_event_count >= MAX_POLL_EVENTS + POLL_EVENTS_HEADROOM)
  61. common->FatalError( "poll_mouse_event_count exceeded MAX_POLL_EVENT + POLL_EVENTS_HEADROOM\n");
  62. poll_events_mouse[poll_mouse_event_count].action = action;
  63. poll_events_mouse[poll_mouse_event_count++].value = value;
  64. if (poll_mouse_event_count >= MAX_POLL_EVENTS) {
  65. common->DPrintf("WARNING: reached MAX_POLL_EVENT poll_mouse_event_count\n");
  66. return false;
  67. }
  68. return true;
  69. }
  70. /*
  71. ===========================================================================
  72. polled from GetDirectUsercmd
  73. async input polling is obsolete
  74. we have a single entry point for both mouse and keyboard
  75. the mouse/keyboard seperation is API legacy
  76. ===========================================================================
  77. */
  78. #ifndef USE_SDL
  79. int Sys_PollKeyboardInputEvents( void ) {
  80. return poll_keyboard_event_count;
  81. }
  82. int Sys_ReturnKeyboardInputEvent( const int n, int &key, bool &state ) {
  83. if ( n >= poll_keyboard_event_count ) {
  84. return 0;
  85. }
  86. key = poll_events_keyboard[n].key;
  87. state = poll_events_keyboard[n].state;
  88. return 1;
  89. }
  90. void Sys_EndKeyboardInputEvents( void ) {
  91. //isn't this were it's supposed to be, was missing some key strokes with it set below
  92. poll_keyboard_event_count = 0;
  93. }
  94. int Sys_PollMouseInputEvents( void ) {
  95. #if 0 //moved to the Sys_End functions
  96. poll_keyboard_event_count = 0;
  97. poll_mouse_event_count = 0;
  98. #endif
  99. // that's OS specific, implemented in osx/ and linux/
  100. Posix_PollInput( );
  101. return poll_mouse_event_count;
  102. }
  103. int Sys_ReturnMouseInputEvent( const int n, int &action, int &value )
  104. {
  105. if ( n>=poll_mouse_event_count ) {
  106. return 0;
  107. }
  108. action = poll_events_mouse[ n ].action;
  109. value = poll_events_mouse[ n ].value;
  110. return 1;
  111. }
  112. void Sys_EndMouseInputEvents( void ) {
  113. // moved out of the Sys_PollMouseInputEvents
  114. poll_mouse_event_count = 0;
  115. }
  116. #endif