main.cpp 11KB

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