main.cpp 12KB

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