main.cpp 12KB

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