x.cpp 10KB

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