main.cpp 9.0KB

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