x.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 has 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,
  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. XEvent event;
  165. while ( XPending( m_display ) ) {
  166. XNextEvent( m_display, &event );
  167. switch ( event.type ) {
  168. case MotionNotify: {
  169. m_mousex = event.xmotion.x;
  170. m_mousey = event.xmotion.y;
  171. break;
  172. }
  173. case ButtonPress: {
  174. // Our pitiful mouse manager--
  175. if ( m_mouse.size() > event.xbutton.button ) {
  176. m_mouse.at( event.xbutton.button ) = true;
  177. } else {
  178. m_mouse.resize( event.xbutton.button+2, false );
  179. m_mouse.at( event.xbutton.button ) = true;
  180. }
  181. break;
  182. }
  183. case EnterNotify: {
  184. if ( event.xcrossing.subwindow != None ) {
  185. m_hoverWindow = event.xcrossing.subwindow;
  186. } else {
  187. m_hoverWindow = event.xcrossing.window;
  188. }
  189. break;
  190. }
  191. case ButtonRelease: {
  192. if ( m_mouse.size() > event.xbutton.button ) {
  193. m_mouse.at( event.xbutton.button ) = false;
  194. } else {
  195. m_mouse.resize( event.xbutton.button+2, false );
  196. m_mouse.at( event.xbutton.button ) = false;
  197. }
  198. break;
  199. }
  200. default: break;
  201. }
  202. }
  203. }
  204. // This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
  205. Cursor slop::XEngine::getCursor( slop::CursorType type ) {
  206. int xfontcursor;
  207. switch ( type ) {
  208. default:
  209. case Left: xfontcursor = XC_left_ptr; break;
  210. case Crosshair: xfontcursor = XC_crosshair; break;
  211. case Cross: xfontcursor = XC_cross; break;
  212. case UpperLeftCorner: xfontcursor = XC_ul_angle; break;
  213. case UpperRightCorner: xfontcursor = XC_ur_angle; break;
  214. case LowerLeftCorner: xfontcursor = XC_ll_angle; break;
  215. case LowerRightCorner: xfontcursor = XC_lr_angle; break;
  216. case Dot: xfontcursor = XC_dot; break;
  217. case Box: xfontcursor = 40; break;
  218. }
  219. Cursor newcursor = 0;
  220. if ( m_cursors.size() > xfontcursor ) {
  221. newcursor = m_cursors.at( xfontcursor );
  222. }
  223. if ( !newcursor ) {
  224. newcursor = XCreateFontCursor( m_display, xfontcursor );
  225. m_cursors.resize( xfontcursor+2, 0 );
  226. m_cursors.at( xfontcursor ) = newcursor;
  227. }
  228. return newcursor;
  229. }
  230. // 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.
  231. void slop::XEngine::setCursor( slop::CursorType type ) {
  232. if ( !m_good ) {
  233. return;
  234. }
  235. Cursor xfontcursor = getCursor( type );
  236. XChangeActivePointerGrab( m_display,
  237. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  238. xfontcursor, CurrentTime );
  239. }
  240. void slop::WindowRectangle::applyPadding( int padding ) {
  241. if ( m_width + padding*2 >= 0 ) {
  242. m_x -= padding;
  243. m_width += padding*2;
  244. }
  245. if ( m_height + padding*2 >= 0 ) {
  246. m_y -= padding;
  247. m_height += padding*2;
  248. }
  249. }
  250. void slop::WindowRectangle::applyMinMaxSize( int minimumsize, int maximumsize ) {
  251. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  252. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  253. exit( 1 );
  254. }
  255. if ( m_width < minimumsize ) {
  256. int diff = minimumsize - m_width;
  257. m_width = minimumsize;
  258. m_x -= diff/2;
  259. }
  260. if ( m_height < minimumsize ) {
  261. int diff = minimumsize - m_height;
  262. m_height = minimumsize;
  263. m_y -= diff/2;
  264. }
  265. if ( maximumsize > 0 ) {
  266. if ( m_width > maximumsize ) {
  267. int diff = m_width;
  268. m_width = maximumsize;
  269. // Center in the center of the window
  270. m_x += diff/2 - maximumsize/2;
  271. }
  272. if ( m_height > maximumsize ) {
  273. int diff = m_height;
  274. m_height = maximumsize;
  275. // Center in the center of the window
  276. m_y += diff/2 - maximumsize/2;
  277. }
  278. }
  279. }
  280. void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
  281. if ( decorations ) {
  282. Window root, parent, test, junk;
  283. Window* childlist;
  284. unsigned int ujunk;
  285. unsigned int depth;
  286. // Try to find the actual decorations.
  287. test = win;
  288. int status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  289. while( parent != root ) {
  290. if ( !parent || !status ) {
  291. break;
  292. }
  293. test = parent;
  294. status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  295. }
  296. // Once found, proceed normally.
  297. if ( test && parent == root && status ) {
  298. XWindowAttributes attr;
  299. XGetWindowAttributes( xengine->m_display, test, &attr );
  300. m_width = attr.width;
  301. m_height = attr.height;
  302. m_border = attr.border_width;
  303. XTranslateCoordinates( xengine->m_display, test, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  304. // We make sure we include borders, since we want decorations.
  305. m_width += m_border * 2;
  306. m_height += m_border * 2;
  307. }
  308. return;
  309. }
  310. Window junk;
  311. // Now here we should be able to just use whatever we get.
  312. XWindowAttributes attr;
  313. // We use XGetWindowAttributes to know our root window.
  314. XGetWindowAttributes( xengine->m_display, win, &attr );
  315. //m_x = attr.x;
  316. //m_y = attr.y;
  317. m_width = attr.width;
  318. m_height = attr.height;
  319. m_border = attr.border_width;
  320. XTranslateCoordinates( xengine->m_display, win, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  321. }