main.cpp 7.5KB

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