123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  16. slop::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b ) {
  17. m_xoffset = 0;
  18. m_yoffset = 0;
  19. m_x = x;
  20. m_y = y;
  21. m_width = width;
  22. m_height = height;
  23. m_border = border;
  24. m_padding = padding;
  25. m_window = None;
  26. // Convert the width, height, x, and y to coordinates that don't have negative values.
  27. // (also adjust for padding and border size.)
  28. constrain( width, height );
  29. // If we don't have a border, we don't exist, so just die.
  30. if ( m_border == 0 ) {
  31. return;
  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. // Not actually sure what this does, but it keeps the window from bugging out :u.
  43. attributes.override_redirect = True;
  44. // We must use our color map, because that's where our color is allocated.
  45. attributes.colormap = xengine->m_colormap;
  46. // Make sure we know when we've been successfully destroyed later!
  47. attributes.event_mask = StructureNotifyMask;
  48. unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
  49. // Create the window offset by our generated offsets (see constrain( float, float ))
  50. m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border, m_width+m_border*2, m_height+m_border*2,
  51. 0, CopyFromParent, InputOutput,
  52. CopyFromParent, valueMask, &attributes );
  53. // Now punch a hole into it so it looks like a selection rectangle!
  54. XRectangle rect;
  55. rect.x = rect.y = m_border;
  56. rect.width = m_width;
  57. rect.height = m_height;
  58. XClassHint classhints;
  59. char name[] = "slop";
  60. classhints.res_name = name;
  61. classhints.res_class = name;
  62. XSetClassHint( xengine->m_display, m_window, &classhints );
  63. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  64. XMapWindow( xengine->m_display, m_window );
  65. }
  66. void slop::Rectangle::setPos( int x, int y ) {
  67. if ( m_x == x && m_y == y ) {
  68. return;
  69. }
  70. m_x = x;
  71. m_y = y;
  72. // If we don't have a border, we don't exist, so just die.
  73. if ( m_border == 0 ) {
  74. return;
  75. }
  76. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
  77. }
  78. void slop::Rectangle::setDim( int w, int h ) {
  79. if ( m_width == w && m_height == h ) {
  80. return;
  81. }
  82. constrain( w, h );
  83. // If we don't have a border, we don't exist, so just die.
  84. if ( m_border == 0 ) {
  85. return;
  86. }
  87. // Change the window size and location to our generated offsets (see constrain( float, float ))
  88. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  89. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
  90. // Regenerate our hole
  91. XRectangle rect;
  92. rect.x = rect.y = 0;
  93. rect.width = m_width+m_border*2;
  94. rect.height = m_height+m_border*2;
  95. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  96. // Then punch out another.
  97. rect.x = rect.y = m_border;
  98. rect.width = m_width;
  99. rect.height = m_height;
  100. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  101. }
  102. void slop::Rectangle::setGeo( int x, int y, int w, int h ) {
  103. if ( m_x == x && m_y == y && m_width == w && m_height == h ) {
  104. return;
  105. }
  106. m_x = x;
  107. m_y = y;
  108. constrain( w, h );
  109. // If we don't have a border, we don't exist, so just die.
  110. if ( m_border == 0 ) {
  111. return;
  112. }
  113. // Change the window size and location to our generated offsets (see constrain( float, float ))
  114. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  115. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
  116. // Regenerate our hole
  117. XRectangle rect;
  118. rect.x = rect.y = 0;
  119. rect.width = m_width+m_border*2;
  120. rect.height = m_height+m_border*2;
  121. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  122. // Then punch out another.
  123. rect.x = rect.y = m_border;
  124. rect.width = m_width;
  125. rect.height = m_height;
  126. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  127. }
  128. // Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
  129. // It also keeps our values in absolute coordinates which is nice.
  130. void slop::Rectangle::constrain( int w, int h ) {
  131. int pad = m_padding;
  132. if ( pad < 0 && std::abs( w ) < std::abs( pad ) * 2 ) {
  133. pad = 0;
  134. }
  135. if ( w < 0 ) {
  136. m_flippedx = true;
  137. m_xoffset = w - pad;
  138. m_width = -w + pad * 2;
  139. } else {
  140. m_flippedx = false;
  141. m_xoffset = -pad;
  142. m_width = w + pad * 2;
  143. }
  144. pad = m_padding;
  145. if ( pad < 0 && std::abs( h ) < std::abs( pad ) * 2 ) {
  146. pad = 0;
  147. }
  148. if ( h < 0 ) {
  149. m_flippedy = true;
  150. m_yoffset = h - pad;
  151. m_height = -h + pad * 2;
  152. } else {
  153. m_flippedy = false;
  154. m_yoffset = -pad;
  155. m_height = h + pad * 2;
  156. }
  157. }
  158. int slop::Rectangle::convertColor( float r, float g, float b ) {
  159. // Convert float colors to shorts.
  160. short red = short( floor( r * 65535.f ) );
  161. short green = short( floor( g * 65535.f ) );
  162. short blue = short( floor( b * 65535.f ) );
  163. XColor color;
  164. color.red = red;
  165. color.green = green;
  166. color.blue = blue;
  167. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  168. if ( err == BadColor ) {
  169. return err;
  170. }
  171. m_color = color;
  172. return 0;
  173. }