main.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. selection = new slop::Rectangle( cx,
  203. cy,
  204. // We add one because pixels start at 0
  205. xengine->m_mousex + 1,
  206. xengine->m_mousey + 1,
  207. borderSize,
  208. highlight,
  209. r, g, b, a );
  210. }
  211. // If the user has let go of the mouse button, we'll just
  212. // continue to the next state.
  213. if ( !xengine->mouseDown( 1 ) ) {
  214. state++;
  215. break;
  216. }
  217. // Check to make sure the user actually wants to drag for a selection before moving things around.
  218. int w = xengine->m_mousex - cx;
  219. int h = xengine->m_mousey - cy;
  220. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  221. // We make sure the selection rectangle stays on the window we had selected
  222. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  223. xengine->setCursor( slop::Left );
  224. continue;
  225. }
  226. // We also detect which way the user is pulling and set the mouse icon accordingly.
  227. bool x = cx > xengine->m_mousex;
  228. bool y = cy > xengine->m_mousey;
  229. if ( selection->m_width <= 1 && selection->m_height <= 1 || minimumsize == maximumsize ) {
  230. xengine->setCursor( slop::Cross );
  231. } else if ( !x && !y ) {
  232. xengine->setCursor( slop::LowerRightCorner );
  233. } else if ( x && !y ) {
  234. xengine->setCursor( slop::LowerLeftCorner );
  235. } else if ( !x && y ) {
  236. xengine->setCursor( slop::UpperRightCorner );
  237. } else if ( x && y ) {
  238. xengine->setCursor( slop::UpperLeftCorner );
  239. }
  240. // Apply padding and minimum size adjustments.
  241. int sx, sy, ex, ey;
  242. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  243. // Set the selection rectangle's dimensions to mouse movement.
  244. selection->setGeo( sx, sy, ex, ey );
  245. break;
  246. }
  247. case 3: {
  248. int x, y, w, h;
  249. // Exit the utility after this state runs once.
  250. running = false;
  251. // We pull the dimensions and positions from the selection rectangle.
  252. // The selection rectangle automatically converts the positions and
  253. // dimensions to absolute coordinates when we set them earilier.
  254. x = selection->m_x;
  255. y = selection->m_y;
  256. w = selection->m_width;
  257. h = selection->m_height;
  258. // Delete the rectangle, which will remove it from the screen.
  259. delete selection;
  260. // Print the selection :)
  261. printSelection( false, x, y, w, h );
  262. break;
  263. }
  264. }
  265. // 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.
  266. // 0.01 seconds
  267. usleep( 10000 );
  268. }
  269. xengine->releaseCursor();
  270. xengine->releaseKeyboard();
  271. // Try to process any last-second requests.
  272. //xengine->tick();
  273. // Clean up global classes.
  274. delete xengine;
  275. delete options;
  276. // If we canceled the selection, return error.
  277. if ( state == -1 ) {
  278. return 1;
  279. }
  280. return 0;
  281. }