123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <unistd.h>
  2. #include <cstdio>
  3. #include "x.hpp"
  4. #include "rectangle.hpp"
  5. #include "options.hpp"
  6. void printSelection( bool cancelled, int x, int y, int w, int h ) {
  7. printf( "X=%i\n", x );
  8. printf( "Y=%i\n", y );
  9. printf( "W=%i\n", w );
  10. printf( "H=%i\n", h );
  11. printf( "G=%ix%i", w, h );
  12. if ( x >= 0 ) {
  13. printf( "+%i", x );
  14. } else {
  15. // Negative is already included
  16. printf( "%i", x );
  17. }
  18. if ( y >= 0 ) {
  19. printf( "+%i", y );
  20. } else {
  21. // Negative is already included
  22. printf( "%i", y );
  23. }
  24. printf( "\n" );
  25. if ( cancelled ) {
  26. printf( "Cancel=true\n" );
  27. } else {
  28. printf( "Cancel=false\n" );
  29. }
  30. }
  31. int main( int argc, char** argv ) {
  32. int err = options->parseOptions( argc, argv );
  33. if ( err ) {
  34. return err;
  35. }
  36. int state = 0;
  37. bool running = true;
  38. slop::Rectangle* selection = NULL;
  39. Window window = None;
  40. std::string xdisplay = options->m_xdisplay;
  41. int padding = options->m_padding;
  42. int borderSize = options->m_borderSize;
  43. int tolerance = options->m_tolerance;
  44. float r = options->m_red;
  45. float g = options->m_green;
  46. float b = options->m_blue;
  47. bool keyboard = options->m_keyboard;
  48. bool decorations = options->m_decorations;
  49. timespec start, time;
  50. int cx = 0;
  51. int cy = 0;
  52. int xmem = 0;
  53. int ymem = 0;
  54. int wmem = 0;
  55. int hmem = 0;
  56. int minimumsize = options->m_minimumsize;
  57. // First we set up the x interface and grab the mouse,
  58. // if we fail for either we exit immediately.
  59. err = xengine->init( xdisplay.c_str() );
  60. if ( err ) {
  61. printSelection( true, 0, 0, 0, 0 );
  62. return err;
  63. }
  64. err = xengine->grabCursor( slop::Cross );
  65. if ( err ) {
  66. printSelection( true, 0, 0, 0, 0 );
  67. return err;
  68. }
  69. if ( keyboard ) {
  70. err = xengine->grabKeyboard();
  71. // We shouldn't error out from failing to grab the keyboard.
  72. //if ( err ) {
  73. //printSelection( true, 0, 0, 0, 0 );
  74. //return err;
  75. //}
  76. }
  77. clock_gettime( CLOCK_REALTIME, &start );
  78. while ( running ) {
  79. clock_gettime( CLOCK_REALTIME, &time );
  80. // "ticking" the xengine makes it process all queued events.
  81. xengine->tick();
  82. // If the user presses any key on the keyboard, exit the application.
  83. // Make sure at least options->m_gracetime has passed before allowing canceling
  84. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  85. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  86. if ( timei - starti > options->m_gracetime ) {
  87. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  88. printSelection( true, 0, 0, 0, 0 );
  89. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  90. state = -1;
  91. running = false;
  92. }
  93. }
  94. // Our adorable little state manager will handle what state we're in.
  95. switch ( state ) {
  96. default: {
  97. break;
  98. }
  99. case 0: {
  100. // If xengine has found a window we're hovering over (or if it changed)
  101. // create a rectangle around it so the user knows he/she can click on it.
  102. // --but only if the user wants us to
  103. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  104. slop::WindowRectangle t;
  105. t.setGeometry( xengine->m_hoverWindow, decorations );
  106. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  107. if ( !selection ) {
  108. selection = new slop::Rectangle( t.m_x,
  109. t.m_y,
  110. t.m_width,
  111. t.m_height,
  112. borderSize, padding,
  113. minimumsize, minimumsize,
  114. r, g, b );
  115. } else {
  116. selection->setGeo( t.m_x, t.m_y, t.m_width, t.m_height );
  117. }
  118. window = xengine->m_hoverWindow;
  119. }
  120. // If the user clicked we move on to the next state.
  121. if ( xengine->mouseDown( 1 ) ) {
  122. state++;
  123. }
  124. break;
  125. }
  126. case 1: {
  127. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  128. cx = xengine->m_mousex;
  129. cy = xengine->m_mousey;
  130. // Also remember where the original selection was
  131. if ( selection ) {
  132. xmem = selection->m_x;
  133. ymem = selection->m_y;
  134. wmem = selection->m_width;
  135. hmem = selection->m_height;
  136. } else {
  137. xmem = cx;
  138. ymem = cy;
  139. }
  140. state++;
  141. break;
  142. }
  143. case 2: {
  144. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  145. if ( !selection ) {
  146. selection = new slop::Rectangle( cx,
  147. cy,
  148. xengine->m_mousex - cx,
  149. xengine->m_mousey - cy,
  150. borderSize, padding,
  151. minimumsize, minimumsize,
  152. r, g, b );
  153. }
  154. // If the user has let go of the mouse button, we'll just
  155. // continue to the next state.
  156. if ( !xengine->mouseDown( 1 ) ) {
  157. state++;
  158. break;
  159. }
  160. // Check to make sure the user actually wants to drag for a selection before moving things around.
  161. int w = xengine->m_mousex - cx;
  162. int h = xengine->m_mousey - cy;
  163. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  164. // We make sure the selection rectangle stays on the window we had selected
  165. selection->setGeo( xmem, ymem, wmem, hmem );
  166. xengine->setCursor( slop::Left );
  167. continue;
  168. }
  169. // We also detect which way the user is pulling and set the mouse icon accordingly.
  170. bool x = selection->m_flippedx;
  171. bool y = selection->m_flippedy;
  172. if ( selection->m_width == 0 && selection->m_height == 0 ) {
  173. xengine->setCursor( slop::Cross );
  174. } else if ( !x && !y ) {
  175. xengine->setCursor( slop::LowerRightCorner );
  176. } else if ( x && !y ) {
  177. xengine->setCursor( slop::LowerLeftCorner );
  178. } else if ( !x && y ) {
  179. xengine->setCursor( slop::UpperRightCorner );
  180. } else if ( x && y ) {
  181. xengine->setCursor( slop::UpperLeftCorner );
  182. }
  183. // We're 100% accurate, but the mouse can't select the very bottom row or very right column of pixels.
  184. // We detect if either are 1 pixel off and attempt to correct it.
  185. if ( w == xengine->getWidth() - 1 ) {
  186. w = xengine->getWidth();
  187. }
  188. if ( h == xengine->getHeight() - 1 ) {
  189. h = xengine->getHeight();
  190. }
  191. // Set the selection rectangle's dimensions to mouse movement.
  192. // We use the function setDim since rectangles can't have negative widths,
  193. // and because the rectangles have borders and padding to worry about.
  194. selection->setGeo( cx, cy, w, h );
  195. break;
  196. }
  197. case 3: {
  198. int x, y, w, h;
  199. // Exit the utility after this state runs once.
  200. running = false;
  201. // We pull the dimensions and positions from the selection rectangle.
  202. // The selection rectangle automatically converts the positions and
  203. // dimensions to absolute coordinates when we set them earilier.
  204. x = selection->m_x+selection->m_xoffset;
  205. y = selection->m_y+selection->m_yoffset;
  206. w = selection->m_width;
  207. h = selection->m_height;
  208. // Delete the rectangle, which will remove it from the screen.
  209. delete selection;
  210. // Print the selection :)
  211. printSelection( false, x, y, w, h );
  212. break;
  213. }
  214. }
  215. // This sleep is required because drawing the rectangles is a very expensive task that acts really weird with Xorg when called as fast as possible.
  216. // 0.01 seconds
  217. usleep( 10000 );
  218. }
  219. xengine->releaseCursor();
  220. xengine->releaseKeyboard();
  221. // Try to process any last-second requests.
  222. //xengine->tick();
  223. // Clean up global classes.
  224. delete xengine;
  225. delete options;
  226. // If we canceled the selection, return error.
  227. if ( state == -1 ) {
  228. return 1;
  229. }
  230. return 0;
  231. }