x.cpp 978B

123456789101112131415161718192021222324252627282930
  1. #include "x.hpp"
  2. X11::X11( std::string displayName ) {
  3. // Initialize display
  4. display = XOpenDisplay( displayName.c_str() );
  5. if ( !display ) {
  6. throw new std::runtime_error(std::string("Error: Failed to open X display: ") + displayName );
  7. }
  8. screen = ScreenOfDisplay( display, DefaultScreen( display ) );
  9. visual = DefaultVisual( display, XScreenNumberOfScreen( screen ) );
  10. root = DefaultRootWindow( display );
  11. selectAllInputs( root );
  12. }
  13. X11::~X11() {
  14. XCloseDisplay( display );
  15. }
  16. // This cheesy function makes sure we get all EnterNotify events on ALL the windows.
  17. void X11::selectAllInputs( Window win ) {
  18. Window root, parent;
  19. Window* children;
  20. unsigned int nchildren;
  21. XQueryTree( display, win, &root, &parent, &children, &nchildren );
  22. for ( unsigned int i=0;i<nchildren;i++ ) {
  23. XSelectInput( display, children[ i ], EnterWindowMask );
  24. selectAllInputs( children[i] );
  25. }
  26. free( children );
  27. }