main.cpp 7.5KB

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