main.cpp 6.5KB

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