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