123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "rectangle.hpp"
  2. static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
  3. return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
  4. }
  5. slop::Rectangle::~Rectangle() {
  6. if ( m_window == None ) {
  7. return;
  8. }
  9. // Free up our color.
  10. XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
  11. XDestroyWindow( xengine->m_display, m_window );
  12. XEvent event;
  13. // Block until the window is actually completely removed.
  14. XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
  15. // Sleep for 0.1 seconds in hope that the screen actually cleared the window.
  16. usleep( 10000 );
  17. }
  18. slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
  19. m_x = std::min( sx, ex );
  20. m_y = std::min( sy, ey );
  21. m_width = std::max( sx, ex ) - m_x;
  22. m_height = std::max( sy, ey ) - m_y;
  23. m_border = border;
  24. m_window = None;
  25. m_highlight = highlight;
  26. // If we don't have a border, we don't exist, so just die.
  27. if ( m_border == 0 ) {
  28. return;
  29. }
  30. if ( m_highlight ) {
  31. m_border = 0;
  32. }
  33. // This sets up m_color
  34. int err = convertColor( r, g, b );
  35. if ( err ) {
  36. fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
  37. }
  38. XSetWindowAttributes attributes;
  39. // Set up the window so it's our color
  40. attributes.background_pixmap = None;
  41. attributes.background_pixel = m_color.pixel;
  42. attributes.border_pixel = m_color.pixel;
  43. // Not actually sure what this does, but it keeps the window from bugging out :u.
  44. attributes.override_redirect = True;
  45. // We must use our color map, because that's where our color is allocated.
  46. attributes.colormap = xengine->m_colormap;
  47. // Make sure we know when we've been successfully destroyed later!
  48. attributes.event_mask = StructureNotifyMask;
  49. unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
  50. // Create the window
  51. m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x-m_border, m_y-m_border, m_width+m_border*2, m_height+m_border*2,
  52. 0, CopyFromParent, InputOutput,
  53. CopyFromParent, valueMask, &attributes );
  54. if ( a < 1 ) {
  55. unsigned int cardinal_alpha = (unsigned int) (a * (unsigned int)-1) ;
  56. XChangeProperty( xengine->m_display, m_window, XInternAtom( xengine->m_display, "_NET_WM_WINDOW_OPACITY", 0),
  57. XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&cardinal_alpha, 1 );
  58. }
  59. XClassHint classhints;
  60. char name[] = "slop";
  61. classhints.res_name = name;
  62. classhints.res_class = name;
  63. XSetClassHint( xengine->m_display, m_window, &classhints );
  64. // Now punch a hole into it so it looks like a selection rectangle, but only if we're not highlighting.
  65. if ( !m_highlight ) {
  66. XRectangle rect;
  67. rect.x = rect.y = m_border;
  68. rect.width = m_width;
  69. rect.height = m_height;
  70. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  71. }
  72. // Make it so all input falls through
  73. XRectangle rect;
  74. rect.x = rect.y = rect.width = rect.height = 0;
  75. XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
  76. XMapWindow( xengine->m_display, m_window );
  77. }
  78. void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
  79. int x = std::min( sx, ex );
  80. int y = std::min( sy, ey );
  81. int w = std::max( sx, ex ) - x;
  82. int h = std::max( sy, ey ) - y;
  83. if ( m_x == x && m_y == y && m_width == w && m_height == h ) {
  84. return;
  85. }
  86. m_x = x;
  87. m_y = y;
  88. m_width = w;
  89. m_height = h;
  90. // Change the window size
  91. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  92. if ( m_border > 0 ) {
  93. // Fill up our old hole
  94. XRectangle rect;
  95. rect.x = rect.y = 0;
  96. rect.width = m_width+m_border*2;
  97. rect.height = m_height+m_border*2;
  98. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  99. // Then punch out another.
  100. rect.x = rect.y = m_border;
  101. rect.width = m_width;
  102. rect.height = m_height;
  103. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  104. // Then make it so all input falls through.
  105. rect.x = rect.y = rect.width = rect.height = 0;
  106. XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
  107. }
  108. XMoveWindow( xengine->m_display, m_window, m_x-m_border, m_y-m_border );
  109. }
  110. int slop::Rectangle::convertColor( float r, float g, float b ) {
  111. // Convert float colors to shorts.
  112. short red = short( floor( r * 65535.f ) );
  113. short green = short( floor( g * 65535.f ) );
  114. short blue = short( floor( b * 65535.f ) );
  115. XColor color;
  116. color.red = red;
  117. color.green = green;
  118. color.blue = blue;
  119. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  120. if ( err == BadColor ) {
  121. return err;
  122. }
  123. m_color = color;
  124. return 0;
  125. }