keyboard.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <chrono>
  2. #include <thread>
  3. #include "keyboard.hpp"
  4. bool slop::Keyboard::getKey( KeySym key ) {
  5. KeyCode keycode = XKeysymToKeycode( x11->display, key );
  6. if ( keycode != 0 ) {
  7. // Get the whole keyboard state
  8. char keys[32];
  9. XQueryKeymap( x11->display, keys );
  10. // Check our keycode
  11. return ( keys[ keycode / 8 ] & ( 1 << ( keycode % 8 ) ) ) != 0;
  12. } else {
  13. return false;
  14. }
  15. }
  16. // This returns if a key is currently pressed.
  17. // Ignores arrow key presses specifically so users can
  18. // adjust their selection.
  19. bool slop::Keyboard::anyKeyDown() {
  20. return keyDown;
  21. }
  22. void slop::Keyboard::update() {
  23. char keys[32];
  24. XQueryKeymap( x11->display, keys );
  25. // We first delete the arrow key buttons from the mapping.
  26. // This allows the user to press the arrow keys without triggering anyKeyDown
  27. KeyCode keycode = XKeysymToKeycode( x11->display, XK_Left );
  28. keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
  29. keycode = XKeysymToKeycode( x11->display, XK_Right );
  30. keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
  31. keycode = XKeysymToKeycode( x11->display, XK_Up );
  32. keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
  33. keycode = XKeysymToKeycode( x11->display, XK_Down );
  34. keys[ keycode / 8 ] = keys[ keycode / 8 ] & ~( 1 << ( keycode % 8 ) );
  35. keyDown = false;
  36. for ( int i=0;i<32;i++ ) {
  37. if ( deltaState[i] == keys[i] ) {
  38. continue;
  39. }
  40. // Found a key in a group of 4 that's different
  41. char a = deltaState[i];
  42. char b = keys[i];
  43. // Find the "different" bits
  44. char c = a^b;
  45. // A new key was pressed since the last update.
  46. if ( c && b&c ) {
  47. keyDown = true;
  48. }
  49. deltaState[i] = keys[i];
  50. }
  51. }
  52. slop::Keyboard::Keyboard( X11* x11 ) {
  53. this->x11 = x11;
  54. int err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
  55. int tries = 0;
  56. while( err != GrabSuccess && tries < 5 ) {
  57. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  58. err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
  59. tries++;
  60. }
  61. // Non-fatal.
  62. if ( err != GrabSuccess ) {
  63. //throw new std::runtime_error( "Couldn't grab the keyboard after 10 tries." );
  64. }
  65. XQueryKeymap( x11->display, deltaState );
  66. keyDown = false;
  67. }
  68. slop::Keyboard::~Keyboard() {
  69. XUngrabKeyboard( x11->display, CurrentTime );
  70. }