PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Core/InputCommon/ControllerInterface/Xlib/Xlib.cpp

https://github.com/bunnei/dolphin
C++ | 162 lines | 123 code | 31 blank | 8 comment | 16 complexity | f81570afd453928af35f2a757cd5326d MD5 | raw file
Possible License(s): GPL-2.0
  1. #include <X11/XKBlib.h>
  2. #include "InputCommon/ControllerInterface/Xlib/Xlib.h"
  3. namespace ciface
  4. {
  5. namespace Xlib
  6. {
  7. void Init(std::vector<Core::Device*>& devices, void* const hwnd)
  8. {
  9. devices.push_back(new KeyboardMouse((Window)hwnd));
  10. }
  11. KeyboardMouse::KeyboardMouse(Window window) : m_window(window)
  12. {
  13. memset(&m_state, 0, sizeof(m_state));
  14. m_display = XOpenDisplay(nullptr);
  15. int min_keycode, max_keycode;
  16. XDisplayKeycodes(m_display, &min_keycode, &max_keycode);
  17. // Keyboard Keys
  18. for (int i = min_keycode; i <= max_keycode; ++i)
  19. {
  20. Key *temp_key = new Key(m_display, i, m_state.keyboard);
  21. if (temp_key->m_keyname.length())
  22. AddInput(temp_key);
  23. else
  24. delete temp_key;
  25. }
  26. // Mouse Buttons
  27. AddInput(new Button(Button1Mask, m_state.buttons));
  28. AddInput(new Button(Button2Mask, m_state.buttons));
  29. AddInput(new Button(Button3Mask, m_state.buttons));
  30. AddInput(new Button(Button4Mask, m_state.buttons));
  31. AddInput(new Button(Button5Mask, m_state.buttons));
  32. // Mouse Cursor, X-/+ and Y-/+
  33. for (int i = 0; i != 4; ++i)
  34. AddInput(new Cursor(!!(i & 2), !!(i & 1), (&m_state.cursor.x)[!!(i & 2)]));
  35. }
  36. KeyboardMouse::~KeyboardMouse()
  37. {
  38. XCloseDisplay(m_display);
  39. }
  40. bool KeyboardMouse::UpdateInput()
  41. {
  42. XQueryKeymap(m_display, m_state.keyboard);
  43. int root_x, root_y, win_x, win_y;
  44. Window root, child;
  45. XQueryPointer(m_display, m_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &m_state.buttons);
  46. // update mouse cursor
  47. XWindowAttributes win_attribs;
  48. XGetWindowAttributes(m_display, m_window, &win_attribs);
  49. // the mouse position as a range from -1 to 1
  50. m_state.cursor.x = (float)win_x / (float)win_attribs.width * 2 - 1;
  51. m_state.cursor.y = (float)win_y / (float)win_attribs.height * 2 - 1;
  52. return true;
  53. }
  54. bool KeyboardMouse::UpdateOutput()
  55. {
  56. return true;
  57. }
  58. std::string KeyboardMouse::GetName() const
  59. {
  60. return "Keyboard Mouse";
  61. }
  62. std::string KeyboardMouse::GetSource() const
  63. {
  64. return "Xlib";
  65. }
  66. int KeyboardMouse::GetId() const
  67. {
  68. return 0;
  69. }
  70. KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard)
  71. : m_display(display), m_keyboard(keyboard), m_keycode(keycode)
  72. {
  73. int i = 0;
  74. KeySym keysym = 0;
  75. do
  76. {
  77. keysym = XkbKeycodeToKeysym(m_display, keycode, i, 0);
  78. i++;
  79. }
  80. while (keysym == NoSymbol && i < 8);
  81. // Convert to upper case for the keyname
  82. if (keysym >= 97 && keysym <= 122)
  83. keysym -= 32;
  84. // 0x0110ffff is the top of the unicode character range according
  85. // to keysymdef.h although it is probably more than we need.
  86. if (keysym == NoSymbol || keysym > 0x0110ffff ||
  87. XKeysymToString(keysym) == nullptr)
  88. m_keyname = std::string();
  89. else
  90. m_keyname = std::string(XKeysymToString(keysym));
  91. }
  92. ControlState KeyboardMouse::Key::GetState() const
  93. {
  94. return (m_keyboard[m_keycode / 8] & (1 << (m_keycode % 8))) != 0;
  95. }
  96. ControlState KeyboardMouse::Button::GetState() const
  97. {
  98. return ((m_buttons & m_index) != 0);
  99. }
  100. ControlState KeyboardMouse::Cursor::GetState() const
  101. {
  102. return std::max(0.0f, m_cursor / (m_positive ? 1.0f : -1.0f));
  103. }
  104. std::string KeyboardMouse::Key::GetName() const
  105. {
  106. return m_keyname;
  107. }
  108. std::string KeyboardMouse::Cursor::GetName() const
  109. {
  110. static char tmpstr[] = "Cursor ..";
  111. tmpstr[7] = (char)('X' + m_index);
  112. tmpstr[8] = (m_positive ? '+' : '-');
  113. return tmpstr;
  114. }
  115. std::string KeyboardMouse::Button::GetName() const
  116. {
  117. char button = '0';
  118. switch (m_index)
  119. {
  120. case Button1Mask: button = '1'; break;
  121. case Button2Mask: button = '2'; break;
  122. case Button3Mask: button = '3'; break;
  123. case Button4Mask: button = '4'; break;
  124. case Button5Mask: button = '5'; break;
  125. }
  126. static char tmpstr[] = "Click .";
  127. tmpstr[6] = button;
  128. return tmpstr;
  129. }
  130. }
  131. }