x.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* x.cpp: Handles starting and managing X.
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "x.hpp"
  21. slop::XEngine* xengine = new slop::XEngine();
  22. int slop::XEngineErrorHandler( Display* dpy, XErrorEvent* event ) {
  23. // Ignore XGrabKeyboard BadAccess errors, we can work without it.
  24. // 31 = XGrabKeyboard's request code
  25. if ( event->request_code == 31 && event->error_code == BadAccess ) {
  26. fprintf( stderr, "_X Error \"BadAccess\" for XGrabKeyboard ignored...\n" );
  27. return 0;
  28. }
  29. // Everything else should be fatal as I don't like undefined behavior.
  30. char buffer[1024];
  31. XGetErrorText( dpy, event->error_code, buffer, 1024 );
  32. fprintf( stderr,
  33. "_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",
  34. buffer,
  35. event->request_code,
  36. event->serial );
  37. exit(1);
  38. }
  39. int slop::XEngine::getWidth() {
  40. if ( !m_good ) {
  41. return -1;
  42. }
  43. return (int)WidthOfScreen( m_screen );
  44. }
  45. int slop::XEngine::getHeight() {
  46. if ( !m_good ) {
  47. return -1;
  48. }
  49. return (int)HeightOfScreen( m_screen );
  50. }
  51. slop::XEngine::XEngine() {
  52. m_display = NULL;
  53. m_visual = NULL;
  54. m_screen = NULL;
  55. m_good = false;
  56. m_mousex = -1;
  57. m_mousey = -1;
  58. m_hoverWindow = None;
  59. }
  60. slop::XEngine::~XEngine() {
  61. if ( !m_good ) {
  62. return;
  63. }
  64. for ( unsigned int i=0; i<m_cursors.size(); i++ ) {
  65. if ( m_cursors.at( i ) ) {
  66. XFreeCursor( m_display, m_cursors[i] );
  67. }
  68. }
  69. XCloseDisplay( m_display );
  70. }
  71. bool slop::XEngine::mouseDown( unsigned int button ) {
  72. if ( button >= m_mouse.size() ) {
  73. return false;
  74. }
  75. return m_mouse.at( button );
  76. }
  77. int slop::XEngine::init( std::string display ) {
  78. // Initialize display
  79. m_display = XOpenDisplay( display.c_str() );
  80. if ( !m_display ) {
  81. fprintf( stderr, "Error: Failed to open X display %s\n", display.c_str() );
  82. return 1;
  83. }
  84. m_screen = ScreenOfDisplay( m_display, DefaultScreen( m_display ) );
  85. m_visual = DefaultVisual ( m_display, XScreenNumberOfScreen( m_screen ) );
  86. m_colormap = DefaultColormap( m_display, XScreenNumberOfScreen( m_screen ) );
  87. //m_root = RootWindow ( m_display, XScreenNumberOfScreen( m_screen ) );
  88. m_root = DefaultRootWindow( m_display );
  89. m_good = true;
  90. XSetErrorHandler( slop::XEngineErrorHandler );
  91. selectAllInputs( m_root, EnterWindowMask );
  92. return 0;
  93. }
  94. bool slop::XEngine::anyKeyPressed() {
  95. if ( !m_good ) {
  96. return false;
  97. }
  98. // Thanks to SFML for some reliable key state grabbing.
  99. // Get the whole keyboard state
  100. char keys[ 32 ];
  101. XQueryKeymap( m_display, keys );
  102. // Each bit indicates a different key, 1 for pressed, 0 otherwise.
  103. // Every bit should be 0 if nothing is pressed.
  104. for ( unsigned int i = 0; i < 32; i++ ) {
  105. if ( keys[ i ] != 0 ) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. int slop::XEngine::grabKeyboard() {
  112. if ( !m_good ) {
  113. return 1;
  114. }
  115. int err = XGrabKeyboard( m_display, m_root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
  116. if ( err != GrabSuccess ) {
  117. fprintf( stderr, "Warning: Failed to grab X keyboard.\n" );
  118. fprintf( stderr, " This happens when something has already grabbed your keybaord.\n" );
  119. fprintf( stderr, " slop should still run properly though.\n" );
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. int slop::XEngine::releaseKeyboard() {
  125. if ( !m_good ) {
  126. return 1;
  127. }
  128. XUngrabKeyboard( m_display, CurrentTime );
  129. return 0;
  130. }
  131. void slop::XEngine::selectAllInputs( Window win, long event_mask) {
  132. Window root, parent;
  133. Window* children;
  134. unsigned int nchildren;
  135. XQueryTree( m_display, win, &root, &parent, &children, &nchildren );
  136. for ( unsigned int i=0;i<nchildren;i++ ) {
  137. XSelectInput( m_display, children[ i ], event_mask );
  138. selectAllInputs( children[ i ], event_mask );
  139. }
  140. free( children );
  141. }
  142. // Grabs the cursor, be wary that setCursor changes the mouse masks.
  143. int slop::XEngine::grabCursor( slop::CursorType type ) {
  144. if ( !m_good ) {
  145. return 1;
  146. }
  147. int xfontcursor = getCursor( type );
  148. int err = XGrabPointer( m_display, m_root, True,
  149. PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
  150. GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
  151. if ( err != GrabSuccess ) {
  152. fprintf( stderr, "Error: Failed to grab X cursor.\n" );
  153. fprintf( stderr, " This can be caused by launching slop weirdly.\n" );
  154. return 1;
  155. }
  156. // Quickly set the mouse position so we don't have to worry about x11 generating an event.
  157. Window root, child;
  158. int mx, my;
  159. int wx, wy;
  160. unsigned int mask;
  161. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  162. m_mousex = mx;
  163. m_mousey = my;
  164. // Get the deepest available window.
  165. Window test = child;
  166. while( test ) {
  167. child = test;
  168. XQueryPointer( m_display, child, &root, &test, &mx, &my, &wx, &wy, &mask );
  169. }
  170. m_hoverWindow = child;
  171. return 0;
  172. }
  173. int slop::XEngine::releaseCursor() {
  174. if ( !m_good ) {
  175. return 1;
  176. }
  177. XUngrabPointer( m_display, CurrentTime );
  178. return 0;
  179. }
  180. void slop::XEngine::tick() {
  181. if ( !m_good ) {
  182. return;
  183. }
  184. XEvent event;
  185. while ( XPending( m_display ) ) {
  186. XNextEvent( m_display, &event );
  187. switch ( event.type ) {
  188. case MotionNotify: {
  189. m_mousex = event.xmotion.x;
  190. m_mousey = event.xmotion.y;
  191. break;
  192. }
  193. case ButtonPress: {
  194. // Our pitiful mouse manager--
  195. if ( m_mouse.size() > event.xbutton.button ) {
  196. m_mouse.at( event.xbutton.button ) = true;
  197. } else {
  198. m_mouse.resize( event.xbutton.button+2, false );
  199. m_mouse.at( event.xbutton.button ) = true;
  200. }
  201. break;
  202. }
  203. case EnterNotify: {
  204. if ( event.xcrossing.subwindow != None ) {
  205. m_hoverWindow = event.xcrossing.subwindow;
  206. } else {
  207. m_hoverWindow = event.xcrossing.window;
  208. }
  209. break;
  210. }
  211. case ButtonRelease: {
  212. if ( m_mouse.size() > event.xbutton.button ) {
  213. m_mouse.at( event.xbutton.button ) = false;
  214. } else {
  215. m_mouse.resize( event.xbutton.button+2, false );
  216. m_mouse.at( event.xbutton.button ) = false;
  217. }
  218. break;
  219. }
  220. default: break;
  221. }
  222. }
  223. }
  224. // This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
  225. Cursor slop::XEngine::getCursor( slop::CursorType type ) {
  226. int xfontcursor;
  227. switch ( type ) {
  228. default:
  229. case Left: xfontcursor = XC_left_ptr; break;
  230. case Crosshair: xfontcursor = XC_crosshair; break;
  231. case Cross: xfontcursor = XC_cross; break;
  232. case UpperLeftCorner: xfontcursor = XC_ul_angle; break;
  233. case UpperRightCorner: xfontcursor = XC_ur_angle; break;
  234. case LowerLeftCorner: xfontcursor = XC_ll_angle; break;
  235. case LowerRightCorner: xfontcursor = XC_lr_angle; break;
  236. case Dot: xfontcursor = XC_dot; break;
  237. case Box: xfontcursor = 40; break;
  238. }
  239. Cursor newcursor = 0;
  240. if ( m_cursors.size() > xfontcursor ) {
  241. newcursor = m_cursors.at( xfontcursor );
  242. }
  243. if ( !newcursor ) {
  244. newcursor = XCreateFontCursor( m_display, xfontcursor );
  245. m_cursors.resize( xfontcursor+2, 0 );
  246. m_cursors.at( xfontcursor ) = newcursor;
  247. }
  248. return newcursor;
  249. }
  250. // 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.
  251. void slop::XEngine::setCursor( slop::CursorType type ) {
  252. if ( !m_good ) {
  253. return;
  254. }
  255. Cursor xfontcursor = getCursor( type );
  256. XChangeActivePointerGrab( m_display,
  257. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  258. xfontcursor, CurrentTime );
  259. }
  260. void slop::WindowRectangle::applyPadding( int padding ) {
  261. if ( m_width + padding*2 >= 0 ) {
  262. m_x -= padding;
  263. m_width += padding*2;
  264. }
  265. if ( m_height + padding*2 >= 0 ) {
  266. m_y -= padding;
  267. m_height += padding*2;
  268. }
  269. }
  270. Window slop::WindowRectangle::getWindow() {
  271. return m_window;
  272. }
  273. void slop::WindowRectangle::applyMinMaxSize( int minimumsize, int maximumsize ) {
  274. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  275. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  276. exit( 1 );
  277. }
  278. if ( m_width < minimumsize ) {
  279. int diff = minimumsize - m_width;
  280. m_width = minimumsize;
  281. m_x -= diff/2;
  282. }
  283. if ( m_height < minimumsize ) {
  284. int diff = minimumsize - m_height;
  285. m_height = minimumsize;
  286. m_y -= diff/2;
  287. }
  288. if ( maximumsize > 0 ) {
  289. if ( m_width > maximumsize ) {
  290. int diff = m_width;
  291. m_width = maximumsize;
  292. // Center in the center of the window
  293. m_x += diff/2 - maximumsize/2;
  294. }
  295. if ( m_height > maximumsize ) {
  296. int diff = m_height;
  297. m_height = maximumsize;
  298. // Center in the center of the window
  299. m_y += diff/2 - maximumsize/2;
  300. }
  301. }
  302. }
  303. void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
  304. if ( decorations ) {
  305. Window root, parent, test, junk;
  306. Window* childlist;
  307. unsigned int ujunk;
  308. unsigned int depth;
  309. // Try to find the actual decorations.
  310. test = win;
  311. int status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  312. free( childlist );
  313. while( parent != root ) {
  314. if ( !parent || !status ) {
  315. break;
  316. }
  317. test = parent;
  318. status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
  319. free( childlist );
  320. }
  321. // test contains the window we're screenshotting.
  322. m_window = test;
  323. // Once found, proceed normally.
  324. if ( test && parent == root && status ) {
  325. XWindowAttributes attr;
  326. XGetWindowAttributes( xengine->m_display, test, &attr );
  327. m_width = attr.width;
  328. m_height = attr.height;
  329. m_border = attr.border_width;
  330. XTranslateCoordinates( xengine->m_display, test, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  331. // We make sure we include borders, since we want decorations.
  332. m_width += m_border * 2;
  333. m_height += m_border * 2;
  334. }
  335. return;
  336. }
  337. Window junk;
  338. // Now here we should be able to just use whatever we get.
  339. XWindowAttributes attr;
  340. // We use XGetWindowAttributes to know our root window.
  341. XGetWindowAttributes( xengine->m_display, win, &attr );
  342. //m_x = attr.x;
  343. //m_y = attr.y;
  344. m_width = attr.width;
  345. m_height = attr.height;
  346. m_border = attr.border_width;
  347. XTranslateCoordinates( xengine->m_display, win, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
  348. m_window = win;
  349. }