main.cpp 8.8KB

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