main.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <unistd.h>
  2. #include <cstdio>
  3. #include "x.hpp"
  4. #include "rectangle.hpp"
  5. #include "options.hpp"
  6. void printSelection( bool cancelled, int x, int y, int w, int h ) {
  7. printf( "X=%i\n", x );
  8. printf( "Y=%i\n", y );
  9. printf( "W=%i\n", w );
  10. printf( "H=%i\n", h );
  11. printf( "G=%ix%i", w, h );
  12. if ( x >= 0 ) {
  13. printf( "+%i", x );
  14. } else {
  15. // Negative is already included
  16. printf( "%i", x );
  17. }
  18. if ( y >= 0 ) {
  19. printf( "+%i", y );
  20. } else {
  21. // Negative is already included
  22. printf( "%i", y );
  23. }
  24. printf( "\n" );
  25. if ( cancelled ) {
  26. printf( "Cancel=true\n" );
  27. } else {
  28. printf( "Cancel=false\n" );
  29. }
  30. }
  31. int main( int argc, char** argv ) {
  32. int err = options->parseOptions( argc, argv );
  33. if ( err ) {
  34. return err;
  35. }
  36. int state = 0;
  37. bool running = true;
  38. slop::Rectangle* selection = NULL;
  39. Window window = None;
  40. std::string xdisplay = options->m_xdisplay;
  41. int padding = options->m_padding;
  42. int borderSize = options->m_borderSize;
  43. int tolerance = options->m_tolerance;
  44. float r = options->m_red;
  45. float g = options->m_green;
  46. float b = options->m_blue;
  47. bool keyboard = options->m_keyboard;
  48. bool decorations = options->m_decorations;
  49. timespec start, time;
  50. int cx = 0;
  51. int cy = 0;
  52. int xmem = 0;
  53. int ymem = 0;
  54. int wmem = 0;
  55. int hmem = 0;
  56. // First we set up the x interface and grab the mouse,
  57. // if we fail for either we exit immediately.
  58. err = xengine->init( xdisplay.c_str() );
  59. if ( err ) {
  60. printSelection( true, 0, 0, 0, 0 );
  61. return err;
  62. }
  63. err = xengine->grabCursor( slop::Cross );
  64. if ( err ) {
  65. printSelection( true, 0, 0, 0, 0 );
  66. return err;
  67. }
  68. if ( keyboard ) {
  69. err = xengine->grabKeyboard();
  70. if ( err ) {
  71. printSelection( true, 0, 0, 0, 0 );
  72. return err;
  73. }
  74. }
  75. clock_gettime( CLOCK_REALTIME, &start );
  76. while ( running ) {
  77. clock_gettime( CLOCK_REALTIME, &time );
  78. // "ticking" the xengine makes it process all queued events.
  79. xengine->tick();
  80. // If the user presses any key on the keyboard, exit the application.
  81. // Make sure at least options->m_gracetime has passed before allowing canceling
  82. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  83. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  84. if ( timei - starti > options->m_gracetime ) {
  85. if ( xengine->anyKeyPressed() && keyboard || xengine->mouseDown( 3 ) ) {
  86. printSelection( true, 0, 0, 0, 0 );
  87. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  88. state = -1;
  89. running = false;
  90. }
  91. }
  92. // Our adorable little state manager will handle what state we're in.
  93. switch ( state ) {
  94. default: {
  95. break;
  96. }
  97. case 0: {
  98. // If xengine has found a window we're hovering over (or if it changed)
  99. // create a rectangle around it so the user knows he/she can click on it.
  100. // --but only if the user wants us to
  101. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  102. slop::WindowRectangle t;
  103. t.setGeometry( xengine->m_hoverWindow, decorations );
  104. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  105. if ( !selection ) {
  106. selection = new slop::Rectangle( t.m_x,
  107. t.m_y,
  108. t.m_width,
  109. t.m_height,
  110. borderSize, padding,
  111. r, g, b );
  112. } else {
  113. selection->setGeo( t.m_x, t.m_y, t.m_width, t.m_height );
  114. }
  115. window = xengine->m_hoverWindow;
  116. }
  117. // If the user clicked we move on to the next state.
  118. if ( xengine->mouseDown( 1 ) ) {
  119. state++;
  120. }
  121. break;
  122. }
  123. case 1: {
  124. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  125. cx = xengine->m_mousex;
  126. cy = xengine->m_mousey;
  127. // Also remember where the original selection was
  128. if ( selection ) {
  129. xmem = selection->m_x;
  130. ymem = selection->m_y;
  131. wmem = selection->m_width;
  132. hmem = selection->m_height;
  133. } else {
  134. xmem = cx;
  135. ymem = cy;
  136. }
  137. state++;
  138. break;
  139. }
  140. case 2: {
  141. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  142. if ( !selection ) {
  143. selection = new slop::Rectangle( cx,
  144. cy,
  145. xengine->m_mousex - cx,
  146. xengine->m_mousey - cy,
  147. borderSize, padding,
  148. r, g, b );
  149. }
  150. // If the user has let go of the mouse button, we'll just
  151. // continue to the next state.
  152. if ( !xengine->mouseDown( 1 ) ) {
  153. state++;
  154. break;
  155. }
  156. // Check to make sure the user actually wants to drag for a selection before moving things around.
  157. int w = xengine->m_mousex - cx;
  158. int h = xengine->m_mousey - cy;
  159. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  160. // We make sure the selection rectangle stays on the window we had selected
  161. selection->setGeo( xmem, ymem, wmem, hmem );
  162. xengine->setCursor( slop::Left );
  163. continue;
  164. }
  165. // We also detect which way the user is pulling and set the mouse icon accordingly.
  166. bool x = selection->m_flippedx;
  167. bool y = selection->m_flippedy;
  168. if ( selection->m_width == 0 && selection->m_height == 0 ) {
  169. xengine->setCursor( slop::Cross );
  170. } else if ( !x && !y ) {
  171. xengine->setCursor( slop::LowerRightCorner );
  172. } else if ( x && !y ) {
  173. xengine->setCursor( slop::LowerLeftCorner );
  174. } else if ( !x && y ) {
  175. xengine->setCursor( slop::UpperRightCorner );
  176. } else if ( x && y ) {
  177. xengine->setCursor( slop::UpperLeftCorner );
  178. }
  179. // Set the selection rectangle's dimensions to mouse movement.
  180. // We use the function setDim since rectangles can't have negative widths,
  181. // and because the rectangles have borders and padding to worry about.
  182. selection->setGeo( cx, cy, w, h );
  183. break;
  184. }
  185. case 3: {
  186. int x, y, w, h;
  187. // Exit the utility after this state runs once.
  188. running = false;
  189. // We pull the dimensions and positions from the selection rectangle.
  190. // The selection rectangle automatically converts the positions and
  191. // dimensions to absolute coordinates when we set them earilier.
  192. x = selection->m_x+selection->m_xoffset;
  193. y = selection->m_y+selection->m_yoffset;
  194. w = selection->m_width;
  195. h = selection->m_height;
  196. // Delete the rectangle, which will remove it from the screen.
  197. delete selection;
  198. // Print the selection :)
  199. printSelection( false, x, y, w, h );
  200. break;
  201. }
  202. }
  203. // This sleep is required because drawing the rectangles is a very expensive task that acts really weird with Xorg when called as fast as possible.
  204. // 0.01 seconds
  205. usleep( 10000 );
  206. }
  207. xengine->releaseCursor();
  208. xengine->releaseKeyboard();
  209. // Try to process any last-second requests.
  210. //xengine->tick();
  211. // Clean up global classes.
  212. delete xengine;
  213. delete options;
  214. // If we canceled the selection, return error.
  215. if ( state == -1 ) {
  216. return 1;
  217. }
  218. return 0;
  219. }