x.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 | 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::applyPadding( int padding ) {
  252. if ( m_width + padding*2 >= 0 ) {
  253. m_x -= padding;
  254. m_width += padding*2;
  255. }
  256. if ( m_height + padding*2 >= 0 ) {
  257. m_y -= padding;
  258. m_height += padding*2;
  259. }
  260. }
  261. void slop::WindowRectangle::applyMinMaxSize( int minimumsize, int maximumsize ) {
  262. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  263. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  264. exit( 1 );
  265. }
  266. if ( m_width < minimumsize ) {
  267. int diff = minimumsize - m_width;
  268. m_width = minimumsize;
  269. m_x -= diff/2;
  270. }
  271. if ( m_height < minimumsize ) {
  272. int diff = minimumsize - m_height;
  273. m_height = minimumsize;
  274. m_y -= diff/2;
  275. }
  276. if ( maximumsize > 0 ) {
  277. if ( m_width > maximumsize ) {
  278. int diff = m_width;
  279. m_width = maximumsize;
  280. // Center in the center of the window
  281. m_x += diff/2 - maximumsize/2;
  282. }
  283. if ( m_height > maximumsize ) {
  284. int diff = m_height;
  285. m_height = maximumsize;
  286. // Center in the center of the window
  287. m_y += diff/2 - maximumsize/2;
  288. }
  289. }
  290. }
  291. void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
  292. if ( decorations ) {
  293. Window root, parent, test, junk;
  294. Window* childlist;
  295. unsigned int ujunk;
  296. unsigned int depth;
  297. // Try to find the actual decorations.
  298. test = win;
  299. int status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  300. while( parent != root ) {
  301. if ( !parent || !status ) {
  302. break;
  303. }
  304. test = parent;
  305. status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  306. }
  307. // Once found, proceed normally.
  308. if ( test && parent == root && status ) {
  309. XWindowAttributes attr;
  310. XGetWindowAttributes( xengine->m_display, test, &attr );
  311. m_width = attr.width;
  312. m_height = attr.height;
  313. m_border = attr.border_width;
  314. XTranslateCoordinates( xengine->m_display, test, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  315. // We make sure we include borders, since we want decorations.
  316. m_width += m_border * 2;
  317. m_height += m_border * 2;
  318. }
  319. return;
  320. }
  321. Window junk;
  322. // Now here we should be able to just use whatever we get.
  323. XWindowAttributes attr;
  324. // We use XGetWindowAttributes to know our root window.
  325. XGetWindowAttributes( xengine->m_display, win, &attr );
  326. //m_x = attr.x;
  327. //m_y = attr.y;
  328. m_width = attr.width;
  329. m_height = attr.height;
  330. m_border = attr.border_width;
  331. XTranslateCoordinates( xengine->m_display, win, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  332. }