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