main.cpp 12KB

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