x.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "x.hpp"
  2. using namespace slop;
  3. glm::vec4 slop::getWindowGeometry( Window win, bool removeDecoration) {
  4. XWindowAttributes attr;
  5. XGetWindowAttributes( x11->display, win, &attr );
  6. unsigned int width = attr.width;
  7. unsigned int height = attr.height;
  8. unsigned int border = attr.border_width;
  9. int x, y;
  10. Window junk;
  11. XTranslateCoordinates( x11->display, win, attr.root, -attr.border_width, -attr.border_width, &x, &y, &junk );
  12. if ( !removeDecoration ) {
  13. width += border*2;
  14. height += border*2;
  15. return glm::vec4( x, y, width, height );
  16. }
  17. // This is required to be defined by the window manager, so we can assume it exists.
  18. Atom actual_type;
  19. int actual_format;
  20. unsigned long nitems, bytes_after;
  21. unsigned char *data;
  22. int result = XGetWindowProperty(x11->display, win,
  23. XInternAtom(x11->display, "_NET_FRAME_EXTENTS", true),
  24. 0, 4, False, AnyPropertyType,
  25. &actual_type, &actual_format, &nitems, &bytes_after, &data);
  26. if ( result == Success ) {
  27. // Make sure we got the data we expect...
  28. if ((nitems == 4) && (bytes_after == 0)) {
  29. x += (int)data[0];
  30. width -= (int)data[1];
  31. y += (int)data[2];
  32. height -= (int)data[3];
  33. }
  34. }
  35. return glm::vec4( x, y, width, height );
  36. }
  37. bool X11::hasCompositor() {
  38. std::stringstream prop_name;
  39. prop_name << "_NET_WM_CM_S" << XScreenNumberOfScreen( screen );
  40. Atom prop_atom = XInternAtom(display, prop_name.str().c_str(), False);
  41. return XGetSelectionOwner(display, prop_atom) != None;
  42. }
  43. X11::X11( std::string displayName ) {
  44. // Initialize display
  45. display = XOpenDisplay( displayName.c_str() );
  46. if ( !display ) {
  47. throw new std::runtime_error(std::string("Error: Failed to open X display: ") + displayName );
  48. }
  49. screen = ScreenOfDisplay( display, DefaultScreen( display ) );
  50. visual = DefaultVisual( display, XScreenNumberOfScreen( screen ) );
  51. root = DefaultRootWindow( display );
  52. }
  53. X11::~X11() {
  54. XCloseDisplay( display );
  55. }