main.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, int maximumsize, int* rsx, int* rsy, int* rex, int* rey ) {
  32. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  33. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  34. exit( 1 );
  35. }
  36. int x = std::min( sx, ex );
  37. int y = std::min( sy, ey );
  38. // We add one to make sure we select the pixel under the mouse.
  39. int w = std::max( sx, ex ) - x + 1;
  40. int h = std::max( sy, ey ) - y + 1;
  41. // Make sure we don't turn inside out...
  42. if ( w + padding*2 >= 0 ) {
  43. x -= padding;
  44. w += padding*2;
  45. }
  46. if ( h + padding*2 >= 0 ) {
  47. y -= padding;
  48. h += padding*2;
  49. }
  50. if ( w < minimumsize ) {
  51. int diff = minimumsize - w;
  52. w = minimumsize;
  53. x -= diff/2;
  54. }
  55. if ( h < minimumsize ) {
  56. int diff = minimumsize - h;
  57. h = minimumsize;
  58. y -= diff/2;
  59. }
  60. if ( maximumsize > 0 ) {
  61. if ( w > maximumsize ) {
  62. int diff = w;
  63. w = maximumsize;
  64. x += diff/2 - maximumsize/2;
  65. }
  66. if ( h > maximumsize ) {
  67. int diff = h;
  68. h = maximumsize;
  69. y += diff/2 - maximumsize/2;
  70. }
  71. }
  72. // Center around mouse if we have a fixed size.
  73. if ( maximumsize == minimumsize && w == maximumsize && h == maximumsize ) {
  74. x = ex - maximumsize/2;
  75. y = ey - maximumsize/2;
  76. xengine->setCursor( slop::Cross );
  77. }
  78. *rsx = x;
  79. *rsy = y;
  80. *rex = x + w;
  81. *rey = y + h;
  82. }
  83. int main( int argc, char** argv ) {
  84. int err = options->parseOptions( argc, argv );
  85. if ( err ) {
  86. return err;
  87. }
  88. int state = 0;
  89. bool running = true;
  90. slop::Rectangle* selection = NULL;
  91. Window window = None;
  92. std::string xdisplay = options->m_xdisplay;
  93. int padding = options->m_padding;
  94. int borderSize = options->m_borderSize;
  95. int tolerance = options->m_tolerance;
  96. float r = options->m_red;
  97. float g = options->m_green;
  98. float b = options->m_blue;
  99. bool keyboard = options->m_keyboard;
  100. bool decorations = options->m_decorations;
  101. timespec start, time;
  102. int cx = 0;
  103. int cy = 0;
  104. int xmem = 0;
  105. int ymem = 0;
  106. int wmem = 0;
  107. int hmem = 0;
  108. int minimumsize = options->m_minimumsize;
  109. int maximumsize = options->m_maximumsize;
  110. // First we set up the x interface and grab the mouse,
  111. // if we fail for either we exit immediately.
  112. err = xengine->init( xdisplay.c_str() );
  113. if ( err ) {
  114. printSelection( true, 0, 0, 0, 0 );
  115. return err;
  116. }
  117. err = xengine->grabCursor( slop::Cross );
  118. if ( err ) {
  119. printSelection( true, 0, 0, 0, 0 );
  120. return err;
  121. }
  122. if ( keyboard ) {
  123. err = xengine->grabKeyboard();
  124. // We shouldn't error out from failing to grab the keyboard.
  125. //if ( err ) {
  126. //printSelection( true, 0, 0, 0, 0 );
  127. //return err;
  128. //}
  129. }
  130. clock_gettime( CLOCK_REALTIME, &start );
  131. while ( running ) {
  132. clock_gettime( CLOCK_REALTIME, &time );
  133. // "ticking" the xengine makes it process all queued events.
  134. xengine->tick();
  135. // If the user presses any key on the keyboard, exit the application.
  136. // Make sure at least options->m_gracetime has passed before allowing canceling
  137. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  138. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  139. if ( timei - starti > options->m_gracetime ) {
  140. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  141. printSelection( true, 0, 0, 0, 0 );
  142. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  143. state = -1;
  144. running = false;
  145. }
  146. }
  147. // Our adorable little state manager will handle what state we're in.
  148. switch ( state ) {
  149. default: {
  150. break;
  151. }
  152. case 0: {
  153. // If xengine has found a window we're hovering over (or if it changed)
  154. // create a rectangle around it so the user knows he/she can click on it.
  155. // --but only if the user wants us to
  156. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  157. slop::WindowRectangle t;
  158. t.setGeometry( xengine->m_hoverWindow, decorations );
  159. t.applyPadding( padding );
  160. t.applyMinMaxSize( minimumsize, maximumsize );
  161. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  162. if ( !selection ) {
  163. selection = new slop::Rectangle( t.m_x,
  164. t.m_y,
  165. t.m_x + t.m_width,
  166. t.m_y + t.m_height,
  167. borderSize,
  168. r, g, b );
  169. } else {
  170. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  171. }
  172. window = xengine->m_hoverWindow;
  173. }
  174. // If the user clicked we move on to the next state.
  175. if ( xengine->mouseDown( 1 ) ) {
  176. state++;
  177. }
  178. break;
  179. }
  180. case 1: {
  181. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  182. cx = xengine->m_mousex;
  183. cy = xengine->m_mousey;
  184. // Also remember where the original selection was
  185. if ( selection ) {
  186. xmem = selection->m_x;
  187. ymem = selection->m_y;
  188. wmem = selection->m_width;
  189. hmem = selection->m_height;
  190. } else {
  191. xmem = cx;
  192. ymem = cy;
  193. }
  194. state++;
  195. break;
  196. }
  197. case 2: {
  198. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  199. if ( !selection ) {
  200. selection = new slop::Rectangle( cx,
  201. cy,
  202. // We add one because pixels start at 0
  203. xengine->m_mousex + 1,
  204. xengine->m_mousey + 1,
  205. borderSize,
  206. r, g, b );
  207. }
  208. // If the user has let go of the mouse button, we'll just
  209. // continue to the next state.
  210. if ( !xengine->mouseDown( 1 ) ) {
  211. state++;
  212. break;
  213. }
  214. // Check to make sure the user actually wants to drag for a selection before moving things around.
  215. int w = xengine->m_mousex - cx;
  216. int h = xengine->m_mousey - cy;
  217. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  218. // We make sure the selection rectangle stays on the window we had selected
  219. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  220. xengine->setCursor( slop::Left );
  221. continue;
  222. }
  223. // We also detect which way the user is pulling and set the mouse icon accordingly.
  224. bool x = cx > xengine->m_mousex;
  225. bool y = cy > xengine->m_mousey;
  226. if ( selection->m_width <= 1 && selection->m_height <= 1 ) {
  227. xengine->setCursor( slop::Cross );
  228. } else if ( !x && !y ) {
  229. xengine->setCursor( slop::LowerRightCorner );
  230. } else if ( x && !y ) {
  231. xengine->setCursor( slop::LowerLeftCorner );
  232. } else if ( !x && y ) {
  233. xengine->setCursor( slop::UpperRightCorner );
  234. } else if ( x && y ) {
  235. xengine->setCursor( slop::UpperLeftCorner );
  236. }
  237. // Apply padding and minimum size adjustments.
  238. int sx, sy, ex, ey;
  239. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  240. // Set the selection rectangle's dimensions to mouse movement.
  241. selection->setGeo( sx, sy, ex, ey );
  242. break;
  243. }
  244. case 3: {
  245. int x, y, w, h;
  246. // Exit the utility after this state runs once.
  247. running = false;
  248. // We pull the dimensions and positions from the selection rectangle.
  249. // The selection rectangle automatically converts the positions and
  250. // dimensions to absolute coordinates when we set them earilier.
  251. x = selection->m_x;
  252. y = selection->m_y;
  253. w = selection->m_width;
  254. h = selection->m_height;
  255. // Delete the rectangle, which will remove it from the screen.
  256. delete selection;
  257. // Print the selection :)
  258. printSelection( false, x, y, w, h );
  259. break;
  260. }
  261. }
  262. // 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.
  263. // 0.01 seconds
  264. usleep( 10000 );
  265. }
  266. xengine->releaseCursor();
  267. xengine->releaseKeyboard();
  268. // Try to process any last-second requests.
  269. //xengine->tick();
  270. // Clean up global classes.
  271. delete xengine;
  272. delete options;
  273. // If we canceled the selection, return error.
  274. if ( state == -1 ) {
  275. return 1;
  276. }
  277. return 0;
  278. }