123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <time.h>
  22. #include <cstdio>
  23. #include <sstream>
  24. #include "x.hpp"
  25. #include "rectangle.hpp"
  26. #include "cmdline.h"
  27. int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
  28. size_t pos = 0;
  29. while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
  30. if ( pos + 1 > format.size() ) {
  31. fprintf( stderr, "Format error: %% found at the end of format string.\n" );
  32. return EXIT_FAILURE;
  33. }
  34. std::stringstream foo;
  35. switch( format[ pos + 1 ] ) {
  36. case '%':
  37. format.replace( pos, 2, "%" );
  38. pos += 1;
  39. break;
  40. case 'x':
  41. case 'X':
  42. foo << x;
  43. format.replace( pos, 2, foo.str() );
  44. break;
  45. case 'y':
  46. case 'Y':
  47. foo << y;
  48. format.replace( pos, 2, foo.str() );
  49. break;
  50. case 'w':
  51. case 'W':
  52. foo << w;
  53. format.replace( pos, 2, foo.str() );
  54. break;
  55. case 'h':
  56. case 'H':
  57. foo << h;
  58. format.replace( pos, 2, foo.str() );
  59. break;
  60. case 'g':
  61. case 'G':
  62. foo << w << 'x' << h << '+' << x << '+' << y;
  63. format.replace( pos, 2, foo.str() );
  64. break;
  65. case 'i':
  66. case 'I':
  67. foo << window;
  68. format.replace( pos, 2, foo.str() );
  69. break;
  70. case 'c':
  71. case 'C':
  72. format.replace( pos, 2, cancelled ? "true" : "false" );
  73. break;
  74. default:
  75. fprintf( stderr, "Format error: %%%c is an unknown replacement identifier.\n", format[ pos + 1 ] );
  76. fprintf( stderr, "Valid replacements: %%x, %%y, %%w, %%h, %%i, %%c, %%.\n" );
  77. return EXIT_FAILURE;
  78. break;
  79. }
  80. }
  81. pos = 0;
  82. while ( ( pos = format.find( "\\", pos ) ) != std::string::npos ) {
  83. if ( pos + 1 > format.size() ) {
  84. break;
  85. }
  86. if ( format[ pos + 1 ] == 'n' ) {
  87. format.replace( pos, 2, "\n" );
  88. }
  89. pos = pos + 1;
  90. }
  91. printf( "%s", format.c_str() );
  92. return EXIT_SUCCESS;
  93. }
  94. int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
  95. std::string copy = arg;
  96. int find = copy.find( "," );
  97. while( find != (int)copy.npos ) {
  98. copy.at( find ) = ' ';
  99. find = copy.find( "," );
  100. }
  101. // Just in case we didn't include an alpha value
  102. *a = 1;
  103. int num = sscanf( copy.c_str(), "%f %f %f %f", r, g, b, a );
  104. if ( num != 3 && num != 4 ) {
  105. return EXIT_FAILURE;
  106. }
  107. return EXIT_SUCCESS;
  108. }
  109. void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, int maximumsize, int* rsx, int* rsy, int* rex, int* rey ) {
  110. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  111. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  112. exit( 1 );
  113. }
  114. int x = std::min( sx, ex );
  115. int y = std::min( sy, ey );
  116. // We add one to make sure we select the pixel under the mouse.
  117. int w = std::max( sx, ex ) - x + 1;
  118. int h = std::max( sy, ey ) - y + 1;
  119. // Make sure we don't turn inside out...
  120. if ( w + padding*2 >= 0 ) {
  121. x -= padding;
  122. w += padding*2;
  123. }
  124. if ( h + padding*2 >= 0 ) {
  125. y -= padding;
  126. h += padding*2;
  127. }
  128. if ( w < minimumsize ) {
  129. int diff = minimumsize - w;
  130. w = minimumsize;
  131. x -= diff/2;
  132. }
  133. if ( h < minimumsize ) {
  134. int diff = minimumsize - h;
  135. h = minimumsize;
  136. y -= diff/2;
  137. }
  138. if ( maximumsize > 0 ) {
  139. if ( w > maximumsize ) {
  140. int diff = w;
  141. w = maximumsize;
  142. x += diff/2 - maximumsize/2;
  143. }
  144. if ( h > maximumsize ) {
  145. int diff = h;
  146. h = maximumsize;
  147. y += diff/2 - maximumsize/2;
  148. }
  149. }
  150. // Center around mouse if we have a fixed size.
  151. if ( maximumsize == minimumsize && w == maximumsize && h == maximumsize ) {
  152. x = ex - maximumsize/2;
  153. y = ey - maximumsize/2;
  154. }
  155. *rsx = x;
  156. *rsy = y;
  157. *rex = x + w;
  158. *rey = y + h;
  159. }
  160. // Some complicated key detection to replicate key repeating
  161. bool keyRepeat( KeySym key, double curtime, double repeatdelay, double* time, bool* memory ) {
  162. if ( xengine->keyPressed( key ) != *memory ) {
  163. if ( xengine->keyPressed( key ) ) {
  164. *memory = true;
  165. *time = curtime;
  166. return true;
  167. } else {
  168. *memory = false;
  169. }
  170. }
  171. if ( xengine->keyPressed( key ) && curtime - *time > repeatdelay ) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. int app( int argc, char** argv ) {
  177. gengetopt_args_info options;
  178. int err = cmdline_parser( argc, argv, &options );
  179. if ( err != EXIT_SUCCESS ) {
  180. return EXIT_FAILURE;
  181. }
  182. int state = 0;
  183. bool running = true;
  184. slop::Rectangle* selection = NULL;
  185. Window window = None;
  186. Window windowmemory = None;
  187. std::string xdisplay = options.xdisplay_arg;
  188. int padding = options.padding_arg;
  189. int borderSize = options.bordersize_arg;
  190. int tolerance = options.tolerance_arg;
  191. float r, g, b, a;
  192. err = parseColor( options.color_arg, &r, &g, &b, &a );
  193. if ( err != EXIT_SUCCESS ) {
  194. fprintf( stderr, "Error parsing color %s\n", options.color_arg );
  195. return EXIT_FAILURE;
  196. }
  197. float gracetime;
  198. err = sscanf( options.gracetime_arg, "%f", &gracetime );
  199. if ( err != 1 ) {
  200. fprintf( stderr, "Error parsing %s as a float for gracetime!\n", options.gracetime_arg );
  201. return EXIT_FAILURE;
  202. }
  203. bool highlight = options.highlight_flag;
  204. bool keyboard = !options.nokeyboard_flag;
  205. bool decorations = !options.nodecorations_flag;
  206. timespec start, time;
  207. int xoffset = 0;
  208. int yoffset = 0;
  209. int cx = 0;
  210. int cy = 0;
  211. int xmem = 0;
  212. int ymem = 0;
  213. int wmem = 0;
  214. int hmem = 0;
  215. int minimumsize = options.min_arg;
  216. int maximumsize = options.max_arg;
  217. bool pressedMemory[4];
  218. double pressedTime[4];
  219. for ( int i=0;i<4;i++ ) {
  220. pressedMemory[ i ] = false;
  221. pressedTime[ i ] = 0;
  222. }
  223. std::string format = options.format_arg;
  224. cmdline_parser_free( &options );
  225. // First we set up the x interface and grab the mouse,
  226. // if we fail for either we exit immediately.
  227. err = xengine->init( xdisplay.c_str() );
  228. if ( err != EXIT_SUCCESS ) {
  229. printSelection( format, true, 0, 0, 0, 0, None );
  230. return EXIT_FAILURE;
  231. }
  232. err = xengine->grabCursor( slop::Cross );
  233. if ( err != EXIT_SUCCESS ) {
  234. printSelection( format, true, 0, 0, 0, 0, None );
  235. return EXIT_FAILURE;
  236. }
  237. if ( keyboard ) {
  238. err = xengine->grabKeyboard();
  239. if ( err ) {
  240. fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
  241. }
  242. }
  243. clock_gettime( CLOCK_REALTIME, &start );
  244. while ( running ) {
  245. clock_gettime( CLOCK_REALTIME, &time );
  246. // "ticking" the xengine makes it process all queued events.
  247. xengine->tick();
  248. // If the user presses any key on the keyboard, exit the application.
  249. // Make sure at least gracetime has passed before allowing canceling
  250. double curtime = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  251. double starttime = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  252. if ( curtime - starttime > gracetime ) {
  253. if ( keyRepeat( XK_Up, curtime, 0.5, &pressedTime[ 0 ], &pressedMemory[ 0 ] ) ) {
  254. yoffset -= 1;
  255. }
  256. if ( keyRepeat( XK_Down, curtime, 0.5, &pressedTime[ 1 ], &pressedMemory[ 1 ] ) ) {
  257. yoffset += 1;
  258. }
  259. if ( keyRepeat( XK_Left, curtime, 0.5, &pressedTime[ 2 ], &pressedMemory[ 2 ] ) ) {
  260. xoffset -= 1;
  261. }
  262. if ( keyRepeat( XK_Right, curtime, 0.5, &pressedTime[ 3 ], &pressedMemory[ 3 ] ) ) {
  263. xoffset += 1;
  264. }
  265. // If we pressed enter we move the state onward.
  266. if ( xengine->keyPressed( XK_Return ) ) {
  267. // If we're highlight windows, just select the active window.
  268. if ( state == 0 ) {
  269. state = 1;
  270. // If we're making a custom selection, select the custom selection.
  271. } else if ( state == 2 ) {
  272. state = 3;
  273. }
  274. }
  275. // If we press any key other than the arrow keys or enter key, we shut down!
  276. if ( !( xengine->keyPressed( XK_Up ) || xengine->keyPressed( XK_Down ) || xengine->keyPressed( XK_Left ) || xengine->keyPressed( XK_Right ) ) &&
  277. !( xengine->keyPressed( XK_Return ) ) &&
  278. ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) ) {
  279. printSelection( format, true, 0, 0, 0, 0, None );
  280. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  281. state = -1;
  282. running = false;
  283. }
  284. }
  285. // Our adorable little state manager will handle what state we're in.
  286. switch ( state ) {
  287. default: {
  288. break;
  289. }
  290. case 0: {
  291. // If xengine has found a window we're hovering over (or if it changed)
  292. // create a rectangle around it so the user knows he/she can click on it.
  293. // --but only if the user wants us to
  294. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  295. slop::WindowRectangle t;
  296. t.setGeometry( xengine->m_hoverWindow, decorations );
  297. t.applyPadding( padding );
  298. t.applyMinMaxSize( minimumsize, maximumsize );
  299. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  300. if ( !selection ) {
  301. selection = new slop::Rectangle( t.m_x,
  302. t.m_y,
  303. t.m_x + t.m_width,
  304. t.m_y + t.m_height,
  305. borderSize,
  306. highlight,
  307. r, g, b, a );
  308. } else {
  309. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  310. }
  311. //window = xengine->m_hoverWindow;
  312. // Since WindowRectangle can select different windows depending on click location...
  313. window = t.getWindow();
  314. }
  315. // If the user clicked we move on to the next state.
  316. if ( xengine->mouseDown( 1 ) ) {
  317. state++;
  318. }
  319. break;
  320. }
  321. case 1: {
  322. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  323. cx = xengine->m_mousex;
  324. cy = xengine->m_mousey;
  325. // Make sure we don't have un-seen applied offsets.
  326. xoffset = 0;
  327. yoffset = 0;
  328. // Also remember where the original selection was
  329. if ( selection ) {
  330. xmem = selection->m_x;
  331. ymem = selection->m_y;
  332. wmem = selection->m_width;
  333. hmem = selection->m_height;
  334. } else {
  335. xmem = cx;
  336. ymem = cy;
  337. }
  338. state++;
  339. break;
  340. }
  341. case 2: {
  342. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  343. if ( !selection ) {
  344. int sx, sy, ex, ey;
  345. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  346. selection = new slop::Rectangle( sx,
  347. sy,
  348. ex,
  349. ey,
  350. borderSize,
  351. highlight,
  352. r, g, b, a );
  353. }
  354. windowmemory = window;
  355. // If the user has let go of the mouse button, we'll just
  356. // continue to the next state.
  357. if ( !xengine->mouseDown( 1 ) ) {
  358. state++;
  359. break;
  360. }
  361. // Check to make sure the user actually wants to drag for a selection before moving things around.
  362. int w = xengine->m_mousex - cx;
  363. int h = xengine->m_mousey - cy;
  364. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  365. // We make sure the selection rectangle stays on the window we had selected
  366. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  367. xengine->setCursor( slop::Left );
  368. // Make sure
  369. window = windowmemory;
  370. continue;
  371. }
  372. // If we're not selecting a window.
  373. windowmemory = window;
  374. window = None;
  375. // We also detect which way the user is pulling and set the mouse icon accordingly.
  376. bool x = cx > xengine->m_mousex;
  377. bool y = cy > xengine->m_mousey;
  378. if ( ( selection->m_width <= 1 && selection->m_height <= 1 ) || ( minimumsize == maximumsize && minimumsize != 0 && maximumsize != 0 ) ) {
  379. xengine->setCursor( slop::Cross );
  380. } else if ( !x && !y ) {
  381. xengine->setCursor( slop::LowerRightCorner );
  382. } else if ( x && !y ) {
  383. xengine->setCursor( slop::LowerLeftCorner );
  384. } else if ( !x && y ) {
  385. xengine->setCursor( slop::UpperRightCorner );
  386. } else if ( x && y ) {
  387. xengine->setCursor( slop::UpperLeftCorner );
  388. }
  389. // Apply padding and minimum size adjustments.
  390. int sx, sy, ex, ey;
  391. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  392. // Set the selection rectangle's dimensions to mouse movement.
  393. selection->setGeo( sx + xoffset, sy + yoffset, ex, ey );
  394. break;
  395. }
  396. case 3: {
  397. int x, y, w, h;
  398. // Exit the utility after this state runs once.
  399. running = false;
  400. // We pull the dimensions and positions from the selection rectangle.
  401. // The selection rectangle automatically converts the positions and
  402. // dimensions to absolute coordinates when we set them earilier.
  403. x = selection->m_x;
  404. y = selection->m_y;
  405. w = selection->m_width;
  406. h = selection->m_height;
  407. // Delete the rectangle, which will remove it from the screen.
  408. delete selection;
  409. // Print the selection :)
  410. printSelection( format, false, x, y, w, h, window );
  411. break;
  412. }
  413. }
  414. // 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.
  415. // 0.01 seconds
  416. usleep( 10000 );
  417. }
  418. xengine->releaseCursor();
  419. xengine->releaseKeyboard();
  420. // Try to process any last-second requests.
  421. //xengine->tick();
  422. // Clean up global classes.
  423. delete xengine;
  424. // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
  425. usleep( 50000 );
  426. // If we canceled the selection, return error.
  427. if ( state == -1 ) {
  428. return EXIT_FAILURE;
  429. }
  430. return EXIT_SUCCESS;
  431. }
  432. int main( int argc, char** argv ) {
  433. try {
  434. return app( argc, argv );
  435. } catch( std::bad_alloc const& exception ) {
  436. fprintf( stderr, "Couldn't allocate enough memory! No space left in RAM." );
  437. return EXIT_FAILURE;
  438. } catch( std::exception const& exception ) {
  439. fprintf( stderr, "Unhandled Exception Thrown: %s\n", exception.what() );
  440. return EXIT_FAILURE;
  441. } catch( ... ) {
  442. fprintf( stderr, "Unknown Exception Thrown!\n" );
  443. return EXIT_FAILURE;
  444. }
  445. }