main.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // We shouldn't error out from failing to grab the keyboard.
  71. //if ( err ) {
  72. //printSelection( true, 0, 0, 0, 0 );
  73. //return err;
  74. //}
  75. }
  76. clock_gettime( CLOCK_REALTIME, &start );
  77. while ( running ) {
  78. clock_gettime( CLOCK_REALTIME, &time );
  79. // "ticking" the xengine makes it process all queued events.
  80. xengine->tick();
  81. // If the user presses any key on the keyboard, exit the application.
  82. // Make sure at least options->m_gracetime has passed before allowing canceling
  83. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  84. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  85. if ( timei - starti > options->m_gracetime ) {
  86. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  87. printSelection( true, 0, 0, 0, 0 );
  88. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  89. state = -1;
  90. running = false;
  91. }
  92. }
  93. // Our adorable little state manager will handle what state we're in.
  94. switch ( state ) {
  95. default: {
  96. break;
  97. }
  98. case 0: {
  99. // If xengine has found a window we're hovering over (or if it changed)
  100. // create a rectangle around it so the user knows he/she can click on it.
  101. // --but only if the user wants us to
  102. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  103. slop::WindowRectangle t;
  104. t.setGeometry( xengine->m_hoverWindow, decorations );
  105. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  106. if ( !selection ) {
  107. selection = new slop::Rectangle( t.m_x,
  108. t.m_y,
  109. t.m_width,
  110. t.m_height,
  111. borderSize, padding,
  112. r, g, b );
  113. } else {
  114. selection->setGeo( t.m_x, t.m_y, t.m_width, t.m_height );
  115. }
  116. window = xengine->m_hoverWindow;
  117. }
  118. // If the user clicked we move on to the next state.
  119. if ( xengine->mouseDown( 1 ) ) {
  120. state++;
  121. }
  122. break;
  123. }
  124. case 1: {
  125. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  126. cx = xengine->m_mousex;
  127. cy = xengine->m_mousey;
  128. // Also remember where the original selection was
  129. if ( selection ) {
  130. xmem = selection->m_x;
  131. ymem = selection->m_y;
  132. wmem = selection->m_width;
  133. hmem = selection->m_height;
  134. } else {
  135. xmem = cx;
  136. ymem = cy;
  137. }
  138. state++;
  139. break;
  140. }
  141. case 2: {
  142. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  143. if ( !selection ) {
  144. selection = new slop::Rectangle( cx,
  145. cy,
  146. xengine->m_mousex - cx,
  147. xengine->m_mousey - cy,
  148. borderSize, padding,
  149. r, g, b );
  150. }
  151. // If the user has let go of the mouse button, we'll just
  152. // continue to the next state.
  153. if ( !xengine->mouseDown( 1 ) ) {
  154. state++;
  155. break;
  156. }
  157. // Check to make sure the user actually wants to drag for a selection before moving things around.
  158. int w = xengine->m_mousex - cx;
  159. int h = xengine->m_mousey - cy;
  160. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  161. // We make sure the selection rectangle stays on the window we had selected
  162. selection->setGeo( xmem, ymem, wmem, hmem );
  163. xengine->setCursor( slop::Left );
  164. continue;
  165. }
  166. // We also detect which way the user is pulling and set the mouse icon accordingly.
  167. bool x = selection->m_flippedx;
  168. bool y = selection->m_flippedy;
  169. if ( selection->m_width == 0 && selection->m_height == 0 ) {
  170. xengine->setCursor( slop::Cross );
  171. } else if ( !x && !y ) {
  172. xengine->setCursor( slop::LowerRightCorner );
  173. } else if ( x && !y ) {
  174. xengine->setCursor( slop::LowerLeftCorner );
  175. } else if ( !x && y ) {
  176. xengine->setCursor( slop::UpperRightCorner );
  177. } else if ( x && y ) {
  178. xengine->setCursor( slop::UpperLeftCorner );
  179. }
  180. // Set the selection rectangle's dimensions to mouse movement.
  181. // We use the function setDim since rectangles can't have negative widths,
  182. // and because the rectangles have borders and padding to worry about.
  183. selection->setGeo( cx, cy, w, h );
  184. break;
  185. }
  186. case 3: {
  187. int x, y, w, h;
  188. // Exit the utility after this state runs once.
  189. running = false;
  190. // We pull the dimensions and positions from the selection rectangle.
  191. // The selection rectangle automatically converts the positions and
  192. // dimensions to absolute coordinates when we set them earilier.
  193. x = selection->m_x+selection->m_xoffset;
  194. y = selection->m_y+selection->m_yoffset;
  195. w = selection->m_width;
  196. h = selection->m_height;
  197. // Delete the rectangle, which will remove it from the screen.
  198. delete selection;
  199. // Print the selection :)
  200. printSelection( false, x, y, w, h );
  201. break;
  202. }
  203. }
  204. // 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.
  205. // 0.01 seconds
  206. usleep( 10000 );
  207. }
  208. xengine->releaseCursor();
  209. xengine->releaseKeyboard();
  210. // Try to process any last-second requests.
  211. //xengine->tick();
  212. // Clean up global classes.
  213. delete xengine;
  214. delete options;
  215. // If we canceled the selection, return error.
  216. if ( state == -1 ) {
  217. return 1;
  218. }
  219. return 0;
  220. }