x.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "x.hpp"
  2. slop::XEngine* xengine = new slop::XEngine();
  3. int slop::XEngineErrorHandler( Display* dpy, XErrorEvent* event ) {
  4. // Ignore XGrabKeyboard BadAccess errors, we can work without it.
  5. // 31 = XGrabKeyboard's request code
  6. if ( event->request_code == 31 && event->error_code == BadAccess ) {
  7. fprintf( stderr, "_X Error \"BadAccess\" for XGrabKeyboard ignored...\n" );
  8. return 0;
  9. }
  10. // Everything else should be fatal as I don't like undefined behavior.
  11. char buffer[1024];
  12. XGetErrorText( dpy, event->error_code, buffer, 1024 );
  13. fprintf( stderr,
  14. "_X Error of failed request: %s\n_ Major opcode of failed request: % 3d\n_ Serial number of failed request:% 5d\n_ Current serial number in output stream:?????\n",
  15. buffer,
  16. event->request_code,
  17. event->serial );
  18. exit(1);
  19. }
  20. slop::XEngine::XEngine() {
  21. m_display = NULL;
  22. m_visual = NULL;
  23. m_screen = NULL;
  24. m_good = false;
  25. m_mousex = -1;
  26. m_mousey = -1;
  27. m_hoverWindow = None;
  28. }
  29. slop::XEngine::~XEngine() {
  30. if ( !m_good ) {
  31. return;
  32. }
  33. for ( unsigned int i=0; i<m_cursors.size(); i++ ) {
  34. if ( m_cursors.at( i ) ) {
  35. XFreeCursor( m_display, m_cursors[i] );
  36. }
  37. }
  38. XCloseDisplay( m_display );
  39. }
  40. bool slop::XEngine::mouseDown( unsigned int button ) {
  41. if ( button >= m_mouse.size() ) {
  42. return false;
  43. }
  44. return m_mouse.at( button );
  45. }
  46. int slop::XEngine::init( std::string display ) {
  47. // Initialize display
  48. m_display = XOpenDisplay( display.c_str() );
  49. if ( !m_display ) {
  50. fprintf( stderr, "Error: Failed to open X display %s\n", display.c_str() );
  51. return 1;
  52. }
  53. m_screen = ScreenOfDisplay( m_display, DefaultScreen( m_display ) );
  54. m_visual = DefaultVisual ( m_display, XScreenNumberOfScreen( m_screen ) );
  55. m_colormap = DefaultColormap( m_display, XScreenNumberOfScreen( m_screen ) );
  56. //m_root = RootWindow ( m_display, XScreenNumberOfScreen( m_screen ) );
  57. m_root = DefaultRootWindow( m_display );
  58. m_good = true;
  59. XSetErrorHandler( slop::XEngineErrorHandler );
  60. selectAllInputs( m_root, EnterWindowMask );
  61. return 0;
  62. }
  63. bool slop::XEngine::anyKeyPressed() {
  64. if ( !m_good ) {
  65. return false;
  66. }
  67. // Thanks to SFML for some reliable key state grabbing.
  68. // Get the whole keyboard state
  69. char keys[ 32 ];
  70. XQueryKeymap( m_display, keys );
  71. // Each bit indicates a different key, 1 for pressed, 0 otherwise.
  72. // Every bit should be 0 if nothing is pressed.
  73. for ( unsigned int i = 0; i < 32; i++ ) {
  74. if ( keys[ i ] != 0 ) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. int slop::XEngine::grabKeyboard() {
  81. if ( !m_good ) {
  82. return 1;
  83. }
  84. int err = XGrabKeyboard( m_display, m_root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
  85. if ( err != GrabSuccess ) {
  86. fprintf( stderr, "Warning: Failed to grab X keyboard.\n" );
  87. fprintf( stderr, " This happens when something's already grabbed your keybaord.\n" );
  88. fprintf( stderr, " slop should still run properly though.\n" );
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. int slop::XEngine::releaseKeyboard() {
  94. if ( !m_good ) {
  95. return 1;
  96. }
  97. XUngrabKeyboard( m_display, CurrentTime );
  98. return 0;
  99. }
  100. void slop::XEngine::selectAllInputs( Window win, long event_mask) {
  101. Window root, parent;
  102. Window* children;
  103. unsigned int nchildren;
  104. XQueryTree( m_display, win, &root, &parent, &children, &nchildren );
  105. for ( unsigned int i=0;i<nchildren;i++ ) {
  106. XSelectInput( m_display, children[ i ], event_mask );
  107. selectAllInputs( children[ i ], event_mask );
  108. }
  109. }
  110. // Grabs the cursor, be wary that setCursor changes the mouse masks.
  111. int slop::XEngine::grabCursor( slop::CursorType type ) {
  112. if ( !m_good ) {
  113. return 1;
  114. }
  115. int xfontcursor = getCursor( type );
  116. int err = XGrabPointer( m_display, m_root, True,
  117. PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask,
  118. GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
  119. if ( err != GrabSuccess ) {
  120. fprintf( stderr, "Error: Failed to grab X cursor.\n" );
  121. fprintf( stderr, " This can be caused by launching slop weirdly.\n" );
  122. return 1;
  123. }
  124. // Quickly set the mouse position so we don't have to worry about x11 generating an event.
  125. Window root, child;
  126. int mx, my;
  127. int wx, wy;
  128. unsigned int mask;
  129. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  130. m_mousex = mx;
  131. m_mousey = my;
  132. // Get the deepest available window.
  133. Window test = child;
  134. while( test ) {
  135. child = test;
  136. XQueryPointer( m_display, child, &root, &test, &mx, &my, &wx, &wy, &mask );
  137. }
  138. m_hoverWindow = child;
  139. return 0;
  140. }
  141. int slop::XEngine::releaseCursor() {
  142. if ( !m_good ) {
  143. return 1;
  144. }
  145. XUngrabPointer( m_display, CurrentTime );
  146. return 0;
  147. }
  148. void slop::XEngine::tick() {
  149. if ( !m_good ) {
  150. return;
  151. }
  152. XFlush( m_display );
  153. XEvent event;
  154. while ( XPending( m_display ) ) {
  155. XNextEvent( m_display, &event );
  156. switch ( event.type ) {
  157. case MotionNotify: {
  158. m_mousex = event.xmotion.x;
  159. m_mousey = event.xmotion.y;
  160. break;
  161. }
  162. case ButtonPress: {
  163. // Our pitiful mouse manager--
  164. if ( m_mouse.size() > event.xbutton.button ) {
  165. m_mouse.at( event.xbutton.button ) = true;
  166. } else {
  167. m_mouse.resize( event.xbutton.button+2, false );
  168. m_mouse.at( event.xbutton.button ) = true;
  169. }
  170. break;
  171. }
  172. case EnterNotify: {
  173. if ( event.xcrossing.subwindow != None ) {
  174. m_hoverWindow = event.xcrossing.subwindow;
  175. } else {
  176. m_hoverWindow = event.xcrossing.window;
  177. }
  178. break;
  179. }
  180. case LeaveNotify: {
  181. break;
  182. }
  183. case ButtonRelease: {
  184. if ( m_mouse.size() > event.xbutton.button ) {
  185. m_mouse.at( event.xbutton.button ) = false;
  186. } else {
  187. m_mouse.resize( event.xbutton.button+2, false );
  188. m_mouse.at( event.xbutton.button ) = false;
  189. }
  190. break;
  191. }
  192. // Due to X11 really hating applications grabbing the keyboard, we use XQueryKeymap to check for downed keys elsewhere.
  193. case KeyPress: {
  194. break;
  195. }
  196. case KeyRelease: {
  197. break;
  198. }
  199. default: break;
  200. }
  201. }
  202. }
  203. // This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
  204. Cursor slop::XEngine::getCursor( slop::CursorType type ) {
  205. int xfontcursor;
  206. switch ( type ) {
  207. default:
  208. case Left: xfontcursor = XC_left_ptr; break;
  209. case Crosshair: xfontcursor = XC_crosshair; break;
  210. case Cross: xfontcursor = XC_cross; break;
  211. case UpperLeftCorner: xfontcursor = XC_ul_angle; break;
  212. case UpperRightCorner: xfontcursor = XC_ur_angle; break;
  213. case LowerLeftCorner: xfontcursor = XC_ll_angle; break;
  214. case LowerRightCorner: xfontcursor = XC_lr_angle; break;
  215. }
  216. Cursor newcursor = 0;
  217. if ( m_cursors.size() > xfontcursor ) {
  218. newcursor = m_cursors.at( xfontcursor );
  219. }
  220. if ( !newcursor ) {
  221. newcursor = XCreateFontCursor( m_display, xfontcursor );
  222. m_cursors.resize( xfontcursor+2, 0 );
  223. m_cursors.at( xfontcursor ) = newcursor;
  224. }
  225. return newcursor;
  226. }
  227. // Swaps out the current cursor, bewary that XChangeActivePointerGrab also resets masks, so if you change the mouse masks on grab you need to change them here too.
  228. void slop::XEngine::setCursor( slop::CursorType type ) {
  229. if ( !m_good ) {
  230. return;
  231. }
  232. Cursor xfontcursor = getCursor( type );
  233. XChangeActivePointerGrab( m_display,
  234. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  235. xfontcursor, CurrentTime );
  236. }
  237. void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
  238. if ( decorations ) {
  239. Window root, parent, test, junk;
  240. Window* childlist;
  241. unsigned int ujunk;
  242. unsigned int depth;
  243. // Try to find the actual decorations.
  244. test = win;
  245. int status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  246. while( parent != root ) {
  247. if ( !parent || !status ) {
  248. break;
  249. }
  250. test = parent;
  251. status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  252. }
  253. // Once found, proceed normally.
  254. if ( test && parent == root && status ) {
  255. XWindowAttributes attr;
  256. XGetWindowAttributes( xengine->m_display, test, &attr );
  257. m_width = attr.width;
  258. m_height = attr.height;
  259. m_border = attr.border_width;
  260. XTranslateCoordinates( xengine->m_display, test, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  261. // We make sure we include borders, since we want decorations.
  262. m_width += m_border * 2;
  263. m_height += m_border * 2;
  264. }
  265. return;
  266. }
  267. Window junk;
  268. // Now here we should be able to just use whatever we get.
  269. XWindowAttributes attr;
  270. // We use XGetWindowAttributes to know our root window.
  271. XGetWindowAttributes( xengine->m_display, win, &attr );
  272. //m_x = attr.x;
  273. //m_y = attr.y;
  274. m_width = attr.width;
  275. m_height = attr.height;
  276. m_border = attr.border_width;
  277. XTranslateCoordinates( xengine->m_display, win, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  278. }