123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <unistd.h>
  2. #include <cstdio>
  3. #include "x.hpp"
  4. #include "options.hpp"
  5. int main( int argc, char** argv ) {
  6. int err = options->parseOptions( argc, argv );
  7. if ( err ) {
  8. return err;
  9. }
  10. int state = 0;
  11. bool running = true;
  12. slop::Rectangle* selection = NULL;
  13. slop::Rectangle* windowselection = NULL;
  14. Window window = None;
  15. std::string xdisplay = options->m_xdisplay;
  16. int padding = options->m_padding;
  17. int borderSize = options->m_borderSize;
  18. int tolerance = options->m_tolerance;
  19. float r = options->m_red;
  20. float g = options->m_green;
  21. float b = options->m_blue;
  22. timespec start, time;
  23. int cx = 0;
  24. int cy = 0;
  25. // First we set up the x interface and grab the mouse,
  26. // if we fail for either we exit immediately.
  27. err = xengine->init( xdisplay.c_str() );
  28. if ( err ) {
  29. return err;
  30. }
  31. err = xengine->grabCursor( slop::Cross );
  32. if ( err ) {
  33. return err;
  34. }
  35. err = xengine->grabKeyboard();
  36. if ( err ) {
  37. return err;
  38. }
  39. clock_gettime( CLOCK_REALTIME, &start );
  40. while ( running ) {
  41. clock_gettime( CLOCK_REALTIME, &time );
  42. // "ticking" the xengine makes it process all queued events.
  43. xengine->tick();
  44. // If the user presses any key on the keyboard, exit the application.
  45. // Make sure at least options->m_gracetime has passed before allowing canceling
  46. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  47. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  48. if ( timei - starti > options->m_gracetime ) {
  49. if ( xengine->m_keypressed ) {
  50. printf( "X=0\n" );
  51. printf( "Y=0\n" );
  52. printf( "W=0\n" );
  53. printf( "H=0\n" );
  54. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  55. state = -1;
  56. running = false;
  57. }
  58. } else {
  59. xengine->m_keypressed = false;
  60. }
  61. if ( xengine->mouseDown( 3 ) ) {
  62. printf( "X=0\n" );
  63. printf( "Y=0\n" );
  64. printf( "W=0\n" );
  65. printf( "H=0\n" );
  66. fprintf( stderr, "User right-clicked. Canceled selection.\n" );
  67. state = -1;
  68. running = false;
  69. }
  70. // Our adorable little state manager will handle what state we're in.
  71. switch ( state ) {
  72. default: {
  73. break;
  74. }
  75. case 0: {
  76. // If xengine has found a window we're hovering over (or if it changed)
  77. // create a rectangle around it so the user knows he/she can click on it.
  78. if ( window != xengine->m_hoverXWindow ) {
  79. // Make sure to delete the old selection rectangle.
  80. if ( windowselection ) {
  81. xengine->removeRect( windowselection ); // removeRect also dealloc's the rectangle for us.
  82. }
  83. slop::WindowRectangle t = xengine->m_hoverWindow;
  84. windowselection = new slop::Rectangle( t.m_x - t.m_border,
  85. t.m_y - t.m_border,
  86. t.m_width + t.m_border,
  87. t.m_height + t.m_border,
  88. borderSize, padding,
  89. r, g, b );
  90. xengine->addRect( windowselection );
  91. window = xengine->m_hoverXWindow;
  92. }
  93. // If the user clicked, remove the old selection rectangle and then
  94. // move on to the next state.
  95. if ( xengine->mouseDown( 1 ) ) {
  96. if ( windowselection ) {
  97. xengine->removeRect( windowselection );
  98. }
  99. state++;
  100. }
  101. break;
  102. }
  103. case 1: {
  104. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  105. cx = xengine->m_mousex;
  106. cy = xengine->m_mousey;
  107. state++;
  108. break;
  109. }
  110. case 2: {
  111. // If the user has let go of the mouse button, we'll just
  112. // continue to the next state.
  113. if ( !xengine->mouseDown( 1 ) ) {
  114. state++;
  115. break;
  116. }
  117. // Check to make sure the user actually wants to drag for a selection before creating a rectangle.
  118. int w = xengine->m_mousex - cx;
  119. int h = xengine->m_mousey - cy;
  120. if ( ( std::abs( w ) > tolerance || std::abs( h ) > tolerance ) && !selection ) {
  121. selection = new slop::Rectangle( cx, cy, 0, 0, borderSize, padding, r, g, b );
  122. xengine->addRect( selection );
  123. } else if ( std::abs( w ) <= tolerance && std::abs( h ) <= tolerance ) {
  124. continue;
  125. }
  126. // Set the selection rectangle's dimensions to mouse movement.
  127. // We use the function setDim since rectangles can't have negative widths,
  128. // and because the rectangles have borders and padding to worry about.
  129. selection->setDim( w, h );
  130. // We also detect which way the user is pulling and set the mouse icon accordingly.
  131. bool x = selection->m_flippedx;
  132. bool y = selection->m_flippedy;
  133. if ( !x && !y ) {
  134. xengine->setCursor( slop::LowerRightCorner );
  135. } else if ( x && !y ) {
  136. xengine->setCursor( slop::LowerLeftCorner );
  137. } else if ( !x && y ) {
  138. xengine->setCursor( slop::UpperRightCorner );
  139. } else {
  140. xengine->setCursor( slop::UpperLeftCorner );
  141. }
  142. break;
  143. }
  144. case 3: {
  145. int x, y, w, h;
  146. // Exit the utility after this state runs once.
  147. running = false;
  148. if ( selection ) {
  149. // We pull the dimensions and positions from the selection rectangle.
  150. // The selection rectangle automatically converts the positions and
  151. // dimensions to absolute coordinates when we set them earilier.
  152. x = selection->m_x+selection->m_xoffset;
  153. y = selection->m_y+selection->m_yoffset;
  154. w = selection->m_width;
  155. h = selection->m_height;
  156. // Delete the rectangle.
  157. xengine->removeRect( selection );
  158. // If the user simply clicked (and thus made the width and height smaller than
  159. // our tolerance) or if we're not hovering over a window, just print the selection
  160. // rectangle's stuff.
  161. if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
  162. printf( "X=%i\n", x );
  163. printf( "Y=%i\n", y );
  164. printf( "W=%i\n", w + 1 );
  165. printf( "H=%i\n", h + 1 );
  166. break;
  167. }
  168. }
  169. // Otherwise lets grab the window's dimensions and use those (with padding).
  170. slop::WindowRectangle t = xengine->m_hoverWindow;
  171. x = t.m_x - padding - t.m_border;
  172. y = t.m_y - padding - t.m_border;
  173. w = t.m_width + t.m_border + padding*2;
  174. h = t.m_height + t.m_border + padding*2;
  175. printf( "X=%i\n", x );
  176. printf( "Y=%i\n", y );
  177. printf( "W=%i\n", w );
  178. printf( "H=%i\n", h );
  179. break;
  180. }
  181. }
  182. // No need to max out CPU
  183. // FIXME: This could be adjusted to measure how much time has passed,
  184. // we may very well need to max out the CPU if someone has a really- really
  185. // bad computer.
  186. usleep( 1000 );
  187. }
  188. xengine->releaseCursor();
  189. xengine->releaseKeyboard();
  190. // Clean up global classes.
  191. delete xengine;
  192. delete options;
  193. // If we canceled the selection, return error.
  194. if ( state == -1 ) {
  195. return 1;
  196. }
  197. return 0;
  198. }