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