main.cpp 7.8KB

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