rectangle.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Try to erase the window before destroying it.
  10. XSetWindowBackground( xengine->m_display, m_window, 0 );
  11. XClearWindow( xengine->m_display, m_window );
  12. // Sleep for 0.1 seconds in hope that the rectangle was erased.
  13. usleep( 10000 );
  14. // Free up our color.
  15. XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
  16. XDestroyWindow( xengine->m_display, m_window );
  17. XEvent event;
  18. // Block until the window is actually completely removed.
  19. XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
  20. // Sleep for 0.1 seconds in hope that the screen actually cleared the window.
  21. usleep( 10000 );
  22. }
  23. slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
  24. m_x = std::min( sx, ex );
  25. m_y = std::min( sy, ey );
  26. m_width = std::max( sx, ex ) - m_x;
  27. m_height = std::max( sy, ey ) - m_y;
  28. m_border = border;
  29. m_window = None;
  30. m_highlight = highlight;
  31. // If we don't have a border, we don't exist, so just die.
  32. if ( m_border == 0 ) {
  33. return;
  34. }
  35. if ( m_highlight ) {
  36. m_border = 0;
  37. }
  38. m_color = convertColor( r, g, b );
  39. XSetWindowAttributes attributes;
  40. // Set up the window so it's our color
  41. attributes.background_pixel = m_color.pixel;
  42. // Disable window decorations.
  43. attributes.override_redirect = True;
  44. // Make sure we know when we've been successfully destroyed later!
  45. attributes.event_mask = StructureNotifyMask;
  46. unsigned long valueMask = CWBackPixel | CWOverrideRedirect | CWEventMask;
  47. // Create the window
  48. m_window = XCreateWindow( xengine->m_display, xengine->m_root, 0, 0, WidthOfScreen( xengine->m_screen ), HeightOfScreen( xengine->m_screen ),
  49. 0, CopyFromParent, InputOutput,
  50. CopyFromParent, valueMask, &attributes );
  51. if ( a < 1 ) {
  52. // Change the window opacity
  53. unsigned int cardinal_alpha = (unsigned int) (a * (unsigned int)-1) ;
  54. XChangeProperty( xengine->m_display, m_window, XInternAtom( xengine->m_display, "_NET_WM_WINDOW_OPACITY", 0),
  55. XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&cardinal_alpha, 1 );
  56. }
  57. XClassHint classhints;
  58. char name[] = "slop";
  59. classhints.res_name = name;
  60. classhints.res_class = name;
  61. XSetClassHint( xengine->m_display, m_window, &classhints );
  62. // Now punch a hole into it so it looks like a selection rectangle, but only if we're not highlighting.
  63. if ( !m_highlight ) {
  64. XRectangle rects[4];
  65. // Left
  66. rects[0].x = m_x-m_border;
  67. rects[0].y = m_y-m_border;
  68. rects[0].width = m_border;
  69. rects[0].height = m_height+m_border*2;
  70. // Top
  71. rects[1].x = m_x;
  72. rects[1].y = m_y-m_border;
  73. rects[1].width = m_width+m_border;
  74. rects[1].height = m_border;
  75. // Right
  76. rects[2].x = m_x+m_width;
  77. rects[2].y = m_y-m_border;
  78. rects[2].width = m_border;
  79. rects[2].height = m_height+m_border*2;
  80. // Bottom
  81. rects[3].x = m_x;
  82. rects[3].y = m_y+m_height;
  83. rects[3].width = m_width+m_border;
  84. rects[3].height = m_border;
  85. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, rects, 4, ShapeSet, 0);
  86. } else {
  87. XRectangle rect;
  88. rect.x = m_x;
  89. rect.y = m_y;
  90. rect.width = m_width;
  91. rect.height = m_height;
  92. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  93. }
  94. // Make it so all input falls through
  95. XRectangle rect;
  96. rect.x = rect.y = rect.width = rect.height = 0;
  97. XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
  98. XMapWindow( xengine->m_display, m_window );
  99. }
  100. void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
  101. int x = std::min( sx, ex );
  102. int y = std::min( sy, ey );
  103. int w = std::max( sx, ex ) - x;
  104. int h = std::max( sy, ey ) - y;
  105. // Only resize or move if we have to, because they're oddly expensive.
  106. m_x = x;
  107. m_y = y;
  108. m_width = w;
  109. m_height = h;
  110. if ( m_border > 0 ) {
  111. XRectangle rects[4];
  112. // Left
  113. rects[0].x = m_x-m_border;
  114. rects[0].y = m_y-m_border;
  115. rects[0].width = m_border;
  116. rects[0].height = m_height+m_border*2;
  117. // Top
  118. rects[1].x = m_x;
  119. rects[1].y = m_y-m_border;
  120. rects[1].width = m_width+m_border;
  121. rects[1].height = m_border;
  122. // Right
  123. rects[2].x = m_x+m_width;
  124. rects[2].y = m_y-m_border;
  125. rects[2].width = m_border;
  126. rects[2].height = m_height+m_border*2;
  127. // Bottom
  128. rects[3].x = m_x;
  129. rects[3].y = m_y+m_height;
  130. rects[3].width = m_width+m_border;
  131. rects[3].height = m_border;
  132. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, rects, 4, ShapeSet, 0);
  133. } else {
  134. XRectangle rect;
  135. rect.x = m_x;
  136. rect.y = m_y;
  137. rect.width = m_width;
  138. rect.height = m_height;
  139. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  140. }
  141. }
  142. XColor slop::Rectangle::convertColor( float r, float g, float b ) {
  143. // Convert float colors to shorts.
  144. short red = short( floor( r * 65535.f ) );
  145. short green = short( floor( g * 65535.f ) );
  146. short blue = short( floor( b * 65535.f ) );
  147. XColor color;
  148. color.red = red;
  149. color.green = green;
  150. color.blue = blue;
  151. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  152. if ( err == BadColor ) {
  153. fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
  154. }
  155. return color;
  156. }