main.cpp 11KB

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. }
  77. *rsx = x;
  78. *rsy = y;
  79. *rex = x + w;
  80. *rey = y + h;
  81. }
  82. int main( int argc, char** argv ) {
  83. int err = options->parseOptions( argc, argv );
  84. if ( err ) {
  85. return err;
  86. }
  87. int state = 0;
  88. bool running = true;
  89. slop::Rectangle* selection = NULL;
  90. Window window = None;
  91. std::string xdisplay = options->m_xdisplay;
  92. int padding = options->m_padding;
  93. int borderSize = options->m_borderSize;
  94. int tolerance = options->m_tolerance;
  95. float r = options->m_red;
  96. float g = options->m_green;
  97. float b = options->m_blue;
  98. float a = options->m_alpha;
  99. bool highlight = options->m_highlight;
  100. bool keyboard = options->m_keyboard;
  101. bool decorations = options->m_decorations;
  102. timespec start, time;
  103. int cx = 0;
  104. int cy = 0;
  105. int xmem = 0;
  106. int ymem = 0;
  107. int wmem = 0;
  108. int hmem = 0;
  109. int minimumsize = options->m_minimumsize;
  110. int maximumsize = options->m_maximumsize;
  111. // First we set up the x interface and grab the mouse,
  112. // if we fail for either we exit immediately.
  113. err = xengine->init( xdisplay.c_str() );
  114. if ( err ) {
  115. printSelection( true, 0, 0, 0, 0 );
  116. return err;
  117. }
  118. err = xengine->grabCursor( slop::Cross );
  119. if ( err ) {
  120. printSelection( true, 0, 0, 0, 0 );
  121. return err;
  122. }
  123. if ( keyboard ) {
  124. err = xengine->grabKeyboard();
  125. // We shouldn't error out from failing to grab the keyboard.
  126. //if ( err ) {
  127. //printSelection( true, 0, 0, 0, 0 );
  128. //return err;
  129. //}
  130. }
  131. clock_gettime( CLOCK_REALTIME, &start );
  132. while ( running ) {
  133. clock_gettime( CLOCK_REALTIME, &time );
  134. // "ticking" the xengine makes it process all queued events.
  135. xengine->tick();
  136. // If the user presses any key on the keyboard, exit the application.
  137. // Make sure at least options->m_gracetime has passed before allowing canceling
  138. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  139. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  140. if ( timei - starti > options->m_gracetime ) {
  141. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  142. printSelection( true, 0, 0, 0, 0 );
  143. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  144. state = -1;
  145. running = false;
  146. }
  147. }
  148. // Our adorable little state manager will handle what state we're in.
  149. switch ( state ) {
  150. default: {
  151. break;
  152. }
  153. case 0: {
  154. // If xengine has found a window we're hovering over (or if it changed)
  155. // create a rectangle around it so the user knows he/she can click on it.
  156. // --but only if the user wants us to
  157. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  158. slop::WindowRectangle t;
  159. t.setGeometry( xengine->m_hoverWindow, decorations );
  160. t.applyPadding( padding );
  161. t.applyMinMaxSize( minimumsize, maximumsize );
  162. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  163. if ( !selection ) {
  164. selection = new slop::Rectangle( t.m_x,
  165. t.m_y,
  166. t.m_x + t.m_width,
  167. t.m_y + t.m_height,
  168. borderSize,
  169. highlight,
  170. r, g, b, a );
  171. } else {
  172. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  173. }
  174. window = xengine->m_hoverWindow;
  175. }
  176. // If the user clicked we move on to the next state.
  177. if ( xengine->mouseDown( 1 ) ) {
  178. state++;
  179. }
  180. break;
  181. }
  182. case 1: {
  183. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  184. cx = xengine->m_mousex;
  185. cy = xengine->m_mousey;
  186. // Also remember where the original selection was
  187. if ( selection ) {
  188. xmem = selection->m_x;
  189. ymem = selection->m_y;
  190. wmem = selection->m_width;
  191. hmem = selection->m_height;
  192. } else {
  193. xmem = cx;
  194. ymem = cy;
  195. }
  196. state++;
  197. break;
  198. }
  199. case 2: {
  200. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  201. if ( !selection ) {
  202. int sx, sy, ex, ey;
  203. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  204. selection = new slop::Rectangle( sx,
  205. sy,
  206. ex,
  207. ey,
  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 || minimumsize == maximumsize ) {
  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. }