main.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <unistd.h>
  2. #include <cstdio>
  3. #include "x.hpp"
  4. #include "options.hpp"
  5. int main( int argc, char** argv ) {
  6. int err = options->parseOptions( argc, argv );
  7. if ( err ) {
  8. return err;
  9. }
  10. int state = 0;
  11. bool running = true;
  12. slop::Rectangle* selection = NULL;
  13. slop::Rectangle* windowselection = NULL;
  14. Window window = None;
  15. std::string xdisplay = options->m_xdisplay;
  16. int padding = options->m_padding;
  17. int borderSize = options->m_borderSize;
  18. int tolerance = options->m_tolerance;
  19. float r = options->m_red;
  20. float g = options->m_green;
  21. float b = options->m_blue;
  22. bool keyboard = options->m_keyboard;
  23. timespec start, time;
  24. int cx = 0;
  25. int cy = 0;
  26. int cxoffset = 0;
  27. int cyoffset = 0;
  28. int woffset = 0;
  29. int hoffset = 0;
  30. // First we set up the x interface and grab the mouse,
  31. // if we fail for either we exit immediately.
  32. err = xengine->init( xdisplay.c_str() );
  33. if ( err ) {
  34. printf( "X=0\n" );
  35. printf( "Y=0\n" );
  36. printf( "W=0\n" );
  37. printf( "H=0\n" );
  38. return err;
  39. }
  40. err = xengine->grabCursor( slop::Cross );
  41. if ( err ) {
  42. printf( "X=0\n" );
  43. printf( "Y=0\n" );
  44. printf( "W=0\n" );
  45. printf( "H=0\n" );
  46. return err;
  47. }
  48. if ( keyboard ) {
  49. err = xengine->grabKeyboard();
  50. if ( err ) {
  51. printf( "X=0\n" );
  52. printf( "Y=0\n" );
  53. printf( "W=0\n" );
  54. printf( "H=0\n" );
  55. return err;
  56. }
  57. }
  58. clock_gettime( CLOCK_REALTIME, &start );
  59. while ( running ) {
  60. clock_gettime( CLOCK_REALTIME, &time );
  61. // "ticking" the xengine makes it process all queued events.
  62. xengine->tick();
  63. // If the user presses any key on the keyboard, exit the application.
  64. // Make sure at least options->m_gracetime has passed before allowing canceling
  65. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  66. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  67. if ( timei - starti > options->m_gracetime ) {
  68. if ( xengine->anyKeyPressed() && keyboard || xengine->mouseDown( 3 ) ) {
  69. printf( "X=0\n" );
  70. printf( "Y=0\n" );
  71. printf( "W=0\n" );
  72. printf( "H=0\n" );
  73. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  74. state = -1;
  75. running = false;
  76. }
  77. }
  78. // Our adorable little state manager will handle what state we're in.
  79. switch ( state ) {
  80. default: {
  81. break;
  82. }
  83. case 0: {
  84. // If xengine has found a window we're hovering over (or if it changed)
  85. // create a rectangle around it so the user knows he/she can click on it.
  86. // --but only if the user wants us to
  87. if ( window != xengine->m_hoverXWindow && tolerance > 0 ) {
  88. // Make sure to delete the old selection rectangle.
  89. if ( windowselection ) {
  90. xengine->removeRect( windowselection ); // removeRect also dealloc's the rectangle for us.
  91. }
  92. slop::WindowRectangle t = xengine->m_hoverWindow;
  93. windowselection = new slop::Rectangle( t.m_x,
  94. t.m_y,
  95. t.m_width + t.m_border * 2,
  96. t.m_height + t.m_border * 2,
  97. borderSize, padding,
  98. r, g, b );
  99. xengine->addRect( windowselection );
  100. window = xengine->m_hoverXWindow;
  101. }
  102. // If the user clicked, remove the old selection rectangle and then
  103. // move on to the next state.
  104. if ( xengine->mouseDown( 1 ) ) {
  105. if ( windowselection ) {
  106. xengine->removeRect( windowselection );
  107. }
  108. state++;
  109. }
  110. break;
  111. }
  112. case 1: {
  113. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  114. cx = xengine->m_mousex;
  115. cy = xengine->m_mousey;
  116. state++;
  117. break;
  118. }
  119. case 2: {
  120. // If the user has let go of the mouse button, we'll just
  121. // continue to the next state.
  122. if ( !xengine->mouseDown( 1 ) ) {
  123. state++;
  124. break;
  125. }
  126. // Check to make sure the user actually wants to drag for a selection before creating a rectangle.
  127. int w = xengine->m_mousex - cx;
  128. int h = xengine->m_mousey - cy;
  129. if ( ( std::abs( w ) >= tolerance || std::abs( h ) >= tolerance ) && !selection ) {
  130. selection = new slop::Rectangle( cx, cy, 0, 0, borderSize, padding, r, g, b );
  131. xengine->addRect( selection );
  132. } else if ( !selection ) {
  133. continue;
  134. }
  135. // We also detect which way the user is pulling and set the mouse icon accordingly.
  136. // and offset the rectangle to be accurate, this is because the mouse actually selects a pixel up and to the left.
  137. bool x = selection->m_flippedx;
  138. bool y = selection->m_flippedy;
  139. if ( !x && !y ) {
  140. cxoffset = 0;
  141. cyoffset = 0;
  142. woffset = 1;
  143. hoffset = 1;
  144. xengine->setCursor( slop::LowerRightCorner );
  145. } else if ( x && !y ) {
  146. cxoffset = 1;
  147. cyoffset = 0;
  148. woffset = -1;
  149. hoffset = 1;
  150. xengine->setCursor( slop::LowerLeftCorner );
  151. } else if ( !x && y ) {
  152. cxoffset = 0;
  153. cyoffset = 1;
  154. woffset = 1;
  155. hoffset = -1;
  156. xengine->setCursor( slop::UpperRightCorner );
  157. } else {
  158. cxoffset = 1;
  159. cyoffset = 1;
  160. woffset = -1;
  161. hoffset = -1;
  162. xengine->setCursor( slop::UpperLeftCorner );
  163. }
  164. // Set the selection rectangle's dimensions to mouse movement.
  165. // We use the function setDim since rectangles can't have negative widths,
  166. // and because the rectangles have borders and padding to worry about.
  167. selection->setPos( cx + cxoffset, cy + cyoffset );
  168. selection->setDim( w + woffset, h + hoffset );
  169. break;
  170. }
  171. case 3: {
  172. int x, y, w, h;
  173. // Exit the utility after this state runs once.
  174. running = false;
  175. if ( selection ) {
  176. // We pull the dimensions and positions from the selection rectangle.
  177. // The selection rectangle automatically converts the positions and
  178. // dimensions to absolute coordinates when we set them earilier.
  179. x = selection->m_x+selection->m_xoffset;
  180. y = selection->m_y+selection->m_yoffset;
  181. w = selection->m_width;
  182. h = selection->m_height;
  183. // Delete the rectangle.
  184. xengine->removeRect( selection );
  185. // if we're not hovering over a window, or our selection is larger than our tolerance
  186. // just print the selection.
  187. if ( w >= tolerance || h >= tolerance || xengine->m_hoverXWindow == None ) {
  188. printf( "X=%i\n", x );
  189. printf( "Y=%i\n", y );
  190. printf( "W=%i\n", w );
  191. printf( "H=%i\n", h );
  192. break;
  193. }
  194. }
  195. // Otherwise lets grab the window's dimensions and use those (with padding).
  196. // --but only if the user lets us, if the user doesn't just select a single pixel there.
  197. if ( tolerance > 0 ) {
  198. slop::WindowRectangle t = xengine->m_hoverWindow;
  199. x = t.m_x - padding;
  200. y = t.m_y - padding;
  201. w = t.m_width + t.m_border * 2 + padding * 2;
  202. h = t.m_height + t.m_border * 2 + padding * 2;
  203. } else {
  204. x = cx;
  205. y = cy;
  206. w = 1;
  207. h = 1;
  208. }
  209. printf( "X=%i\n", x );
  210. printf( "Y=%i\n", y );
  211. printf( "W=%i\n", w );
  212. printf( "H=%i\n", h );
  213. break;
  214. }
  215. }
  216. // No need to max out CPU
  217. // FIXME: This could be adjusted to measure how much time has passed,
  218. // we may very well need to max out the CPU if someone has a really- really
  219. // bad computer.
  220. usleep( 1000 );
  221. }
  222. xengine->releaseCursor();
  223. xengine->releaseKeyboard();
  224. // Try to process any last-second requests.
  225. //xengine->tick();
  226. // Clean up global classes.
  227. delete xengine;
  228. delete options;
  229. // If we canceled the selection, return error.
  230. if ( state == -1 ) {
  231. return 1;
  232. }
  233. return 0;
  234. }