main.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* main.cpp
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <unistd.h>
  21. #include <cstdio>
  22. #include "x.hpp"
  23. #include "rectangle.hpp"
  24. #include "cmdline.h"
  25. void printSelection( bool cancelled, int x, int y, int w, int h, int window ) {
  26. printf( "X=%i\n", x );
  27. printf( "Y=%i\n", y );
  28. printf( "W=%i\n", w );
  29. printf( "H=%i\n", h );
  30. printf( "G=%ix%i", w, h );
  31. if ( x >= 0 ) {
  32. printf( "+%i", x );
  33. } else {
  34. // Negative is already included
  35. printf( "%i", x );
  36. }
  37. if ( y >= 0 ) {
  38. printf( "+%i", y );
  39. } else {
  40. // Negative is already included
  41. printf( "%i", y );
  42. }
  43. printf( "\n" );
  44. printf( "ID=%i\n", window );
  45. if ( cancelled ) {
  46. printf( "Cancel=true\n" );
  47. } else {
  48. printf( "Cancel=false\n" );
  49. }
  50. }
  51. int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
  52. std::string copy = arg;
  53. int find = copy.find( "," );
  54. while( find != copy.npos ) {
  55. copy.at( find ) = ' ';
  56. find = copy.find( "," );
  57. }
  58. // Just in case we didn't include an alpha value
  59. *a = 1;
  60. int num = sscanf( copy.c_str(), "%f %f %f %f", r, g, b, a );
  61. if ( num != 3 && num != 4 ) {
  62. fprintf( stderr, "Error parsing color %s\n", arg.c_str() );
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, int maximumsize, int* rsx, int* rsy, int* rex, int* rey ) {
  68. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  69. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  70. exit( 1 );
  71. }
  72. int x = std::min( sx, ex );
  73. int y = std::min( sy, ey );
  74. // We add one to make sure we select the pixel under the mouse.
  75. int w = std::max( sx, ex ) - x + 1;
  76. int h = std::max( sy, ey ) - y + 1;
  77. // Make sure we don't turn inside out...
  78. if ( w + padding*2 >= 0 ) {
  79. x -= padding;
  80. w += padding*2;
  81. }
  82. if ( h + padding*2 >= 0 ) {
  83. y -= padding;
  84. h += padding*2;
  85. }
  86. if ( w < minimumsize ) {
  87. int diff = minimumsize - w;
  88. w = minimumsize;
  89. x -= diff/2;
  90. }
  91. if ( h < minimumsize ) {
  92. int diff = minimumsize - h;
  93. h = minimumsize;
  94. y -= diff/2;
  95. }
  96. if ( maximumsize > 0 ) {
  97. if ( w > maximumsize ) {
  98. int diff = w;
  99. w = maximumsize;
  100. x += diff/2 - maximumsize/2;
  101. }
  102. if ( h > maximumsize ) {
  103. int diff = h;
  104. h = maximumsize;
  105. y += diff/2 - maximumsize/2;
  106. }
  107. }
  108. // Center around mouse if we have a fixed size.
  109. if ( maximumsize == minimumsize && w == maximumsize && h == maximumsize ) {
  110. x = ex - maximumsize/2;
  111. y = ey - maximumsize/2;
  112. }
  113. *rsx = x;
  114. *rsy = y;
  115. *rex = x + w;
  116. *rey = y + h;
  117. }
  118. int main( int argc, char** argv ) {
  119. gengetopt_args_info options;
  120. int err = cmdline_parser( argc, argv, &options );
  121. if ( err ) {
  122. return err;
  123. }
  124. int state = 0;
  125. bool running = true;
  126. slop::Rectangle* selection = NULL;
  127. Window window = None;
  128. Window windowmemory = None;
  129. std::string xdisplay = options.xdisplay_arg;
  130. int padding = options.padding_arg;
  131. int borderSize = options.bordersize_arg;
  132. int tolerance = options.tolerance_arg;
  133. float r, g, b, a;
  134. parseColor( options.color_arg, &r, &g, &b, &a );
  135. float gracetime = atof( options.gracetime_arg );
  136. bool highlight = options.highlight_flag;
  137. bool keyboard = !options.nokeyboard_flag;
  138. bool decorations = !options.nodecorations_flag;
  139. timespec start, time;
  140. int cx = 0;
  141. int cy = 0;
  142. int xmem = 0;
  143. int ymem = 0;
  144. int wmem = 0;
  145. int hmem = 0;
  146. int minimumsize = options.min_arg;
  147. int maximumsize = options.max_arg;
  148. cmdline_parser_free( &options );
  149. // First we set up the x interface and grab the mouse,
  150. // if we fail for either we exit immediately.
  151. err = xengine->init( xdisplay.c_str() );
  152. if ( err ) {
  153. printSelection( true, 0, 0, 0, 0, None );
  154. return err;
  155. }
  156. err = xengine->grabCursor( slop::Cross );
  157. if ( err ) {
  158. printSelection( true, 0, 0, 0, 0, None );
  159. return err;
  160. }
  161. if ( keyboard ) {
  162. err = xengine->grabKeyboard();
  163. if ( err ) {
  164. fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
  165. }
  166. }
  167. clock_gettime( CLOCK_REALTIME, &start );
  168. while ( running ) {
  169. clock_gettime( CLOCK_REALTIME, &time );
  170. // "ticking" the xengine makes it process all queued events.
  171. xengine->tick();
  172. // If the user presses any key on the keyboard, exit the application.
  173. // Make sure at least gracetime has passed before allowing canceling
  174. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  175. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  176. if ( timei - starti > gracetime ) {
  177. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  178. printSelection( true, 0, 0, 0, 0, None );
  179. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  180. state = -1;
  181. running = false;
  182. }
  183. }
  184. // Our adorable little state manager will handle what state we're in.
  185. switch ( state ) {
  186. default: {
  187. break;
  188. }
  189. case 0: {
  190. // If xengine has found a window we're hovering over (or if it changed)
  191. // create a rectangle around it so the user knows he/she can click on it.
  192. // --but only if the user wants us to
  193. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  194. slop::WindowRectangle t;
  195. t.setGeometry( xengine->m_hoverWindow, decorations );
  196. t.applyPadding( padding );
  197. t.applyMinMaxSize( minimumsize, maximumsize );
  198. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  199. if ( !selection ) {
  200. selection = new slop::Rectangle( t.m_x,
  201. t.m_y,
  202. t.m_x + t.m_width,
  203. t.m_y + t.m_height,
  204. borderSize,
  205. highlight,
  206. r, g, b, a );
  207. } else {
  208. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  209. }
  210. //window = xengine->m_hoverWindow;
  211. // Since WindowRectangle can select different windows depending on click location...
  212. window = t.getWindow();
  213. }
  214. // If the user clicked we move on to the next state.
  215. if ( xengine->mouseDown( 1 ) ) {
  216. state++;
  217. }
  218. break;
  219. }
  220. case 1: {
  221. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  222. cx = xengine->m_mousex;
  223. cy = xengine->m_mousey;
  224. // Also remember where the original selection was
  225. if ( selection ) {
  226. xmem = selection->m_x;
  227. ymem = selection->m_y;
  228. wmem = selection->m_width;
  229. hmem = selection->m_height;
  230. } else {
  231. xmem = cx;
  232. ymem = cy;
  233. }
  234. state++;
  235. break;
  236. }
  237. case 2: {
  238. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  239. if ( !selection ) {
  240. int sx, sy, ex, ey;
  241. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  242. selection = new slop::Rectangle( sx,
  243. sy,
  244. ex,
  245. ey,
  246. borderSize,
  247. highlight,
  248. r, g, b, a );
  249. }
  250. windowmemory = window;
  251. // If the user has let go of the mouse button, we'll just
  252. // continue to the next state.
  253. if ( !xengine->mouseDown( 1 ) ) {
  254. state++;
  255. break;
  256. }
  257. // Check to make sure the user actually wants to drag for a selection before moving things around.
  258. int w = xengine->m_mousex - cx;
  259. int h = xengine->m_mousey - cy;
  260. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  261. // We make sure the selection rectangle stays on the window we had selected
  262. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  263. xengine->setCursor( slop::Left );
  264. // Make sure
  265. window = windowmemory;
  266. continue;
  267. }
  268. // If we're not selecting a window.
  269. windowmemory = window;
  270. window = None;
  271. // We also detect which way the user is pulling and set the mouse icon accordingly.
  272. bool x = cx > xengine->m_mousex;
  273. bool y = cy > xengine->m_mousey;
  274. if ( selection->m_width <= 1 && selection->m_height <= 1 || ( minimumsize == maximumsize && minimumsize != 0 && maximumsize != 0 ) ) {
  275. xengine->setCursor( slop::Cross );
  276. } else if ( !x && !y ) {
  277. xengine->setCursor( slop::LowerRightCorner );
  278. } else if ( x && !y ) {
  279. xengine->setCursor( slop::LowerLeftCorner );
  280. } else if ( !x && y ) {
  281. xengine->setCursor( slop::UpperRightCorner );
  282. } else if ( x && y ) {
  283. xengine->setCursor( slop::UpperLeftCorner );
  284. }
  285. // Apply padding and minimum size adjustments.
  286. int sx, sy, ex, ey;
  287. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  288. // Set the selection rectangle's dimensions to mouse movement.
  289. selection->setGeo( sx, sy, ex, ey );
  290. break;
  291. }
  292. case 3: {
  293. int x, y, w, h;
  294. // Exit the utility after this state runs once.
  295. running = false;
  296. // We pull the dimensions and positions from the selection rectangle.
  297. // The selection rectangle automatically converts the positions and
  298. // dimensions to absolute coordinates when we set them earilier.
  299. x = selection->m_x;
  300. y = selection->m_y;
  301. w = selection->m_width;
  302. h = selection->m_height;
  303. // Delete the rectangle, which will remove it from the screen.
  304. delete selection;
  305. // Print the selection :)
  306. printSelection( false, x, y, w, h, window );
  307. break;
  308. }
  309. }
  310. // 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.
  311. // 0.01 seconds
  312. usleep( 10000 );
  313. }
  314. xengine->releaseCursor();
  315. xengine->releaseKeyboard();
  316. // Try to process any last-second requests.
  317. //xengine->tick();
  318. // Clean up global classes.
  319. delete xengine;
  320. // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
  321. usleep( 50000 );
  322. // If we canceled the selection, return error.
  323. if ( state == -1 ) {
  324. return 1;
  325. }
  326. return 0;
  327. }