main.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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;
  188. if ( options.xdisplay_given ) {
  189. xdisplay = options.xdisplay_arg;
  190. } else {
  191. // If we weren't specifically given a xdisplay, we try
  192. // to parse it from environment variables
  193. char* display = getenv( "DISPLAY" );
  194. if ( display ) {
  195. xdisplay = display;
  196. } else {
  197. fprintf( stderr, "Warning: Failed to parse environment variable: DISPLAY. Using \":0\" instead.\n" );
  198. xdisplay = ":0";
  199. }
  200. }
  201. int padding = options.padding_arg;
  202. int borderSize = options.bordersize_arg;
  203. int tolerance = options.tolerance_arg;
  204. float r, g, b, a;
  205. err = parseColor( options.color_arg, &r, &g, &b, &a );
  206. if ( err != EXIT_SUCCESS ) {
  207. fprintf( stderr, "Error parsing color %s\n", options.color_arg );
  208. return EXIT_FAILURE;
  209. }
  210. float gracetime;
  211. err = sscanf( options.gracetime_arg, "%f", &gracetime );
  212. if ( err != 1 ) {
  213. fprintf( stderr, "Error parsing %s as a float for gracetime!\n", options.gracetime_arg );
  214. return EXIT_FAILURE;
  215. }
  216. bool highlight = options.highlight_flag;
  217. bool keyboard = !options.nokeyboard_flag;
  218. bool decorations = !options.nodecorations_flag;
  219. timespec start, time;
  220. int xoffset = 0;
  221. int yoffset = 0;
  222. int cx = 0;
  223. int cy = 0;
  224. int xmem = 0;
  225. int ymem = 0;
  226. int wmem = 0;
  227. int hmem = 0;
  228. int minimumsize = options.min_arg;
  229. int maximumsize = options.max_arg;
  230. bool pressedMemory[4];
  231. double pressedTime[4];
  232. for ( int i=0;i<4;i++ ) {
  233. pressedMemory[ i ] = false;
  234. pressedTime[ i ] = 0;
  235. }
  236. std::string format = options.format_arg;
  237. cmdline_parser_free( &options );
  238. // First we set up the x interface and grab the mouse,
  239. // if we fail for either we exit immediately.
  240. err = xengine->init( xdisplay.c_str() );
  241. if ( err != EXIT_SUCCESS ) {
  242. printSelection( format, true, 0, 0, 0, 0, None );
  243. return EXIT_FAILURE;
  244. }
  245. err = xengine->grabCursor( slop::Cross );
  246. if ( err != EXIT_SUCCESS ) {
  247. printSelection( format, true, 0, 0, 0, 0, None );
  248. return EXIT_FAILURE;
  249. }
  250. if ( keyboard ) {
  251. err = xengine->grabKeyboard();
  252. if ( err ) {
  253. fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
  254. }
  255. }
  256. clock_gettime( CLOCK_REALTIME, &start );
  257. while ( running ) {
  258. clock_gettime( CLOCK_REALTIME, &time );
  259. // "ticking" the xengine makes it process all queued events.
  260. xengine->tick();
  261. // If the user presses any key on the keyboard, exit the application.
  262. // Make sure at least gracetime has passed before allowing canceling
  263. double curtime = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  264. double starttime = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  265. if ( curtime - starttime > gracetime ) {
  266. if ( keyRepeat( XK_Up, curtime, 0.5, &pressedTime[ 0 ], &pressedMemory[ 0 ] ) ) {
  267. yoffset -= 1;
  268. }
  269. if ( keyRepeat( XK_Down, curtime, 0.5, &pressedTime[ 1 ], &pressedMemory[ 1 ] ) ) {
  270. yoffset += 1;
  271. }
  272. if ( keyRepeat( XK_Left, curtime, 0.5, &pressedTime[ 2 ], &pressedMemory[ 2 ] ) ) {
  273. xoffset -= 1;
  274. }
  275. if ( keyRepeat( XK_Right, curtime, 0.5, &pressedTime[ 3 ], &pressedMemory[ 3 ] ) ) {
  276. xoffset += 1;
  277. }
  278. // If we pressed enter we move the state onward.
  279. if ( xengine->keyPressed( XK_Return ) ) {
  280. // If we're highlight windows, just select the active window.
  281. if ( state == 0 ) {
  282. state = 1;
  283. // If we're making a custom selection, select the custom selection.
  284. } else if ( state == 2 ) {
  285. state = 3;
  286. }
  287. }
  288. // If we press any key other than the arrow keys or enter key, we shut down!
  289. if ( !( xengine->keyPressed( XK_Up ) || xengine->keyPressed( XK_Down ) || xengine->keyPressed( XK_Left ) || xengine->keyPressed( XK_Right ) ) &&
  290. !( xengine->keyPressed( XK_Return ) ) &&
  291. ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) ) {
  292. printSelection( format, true, 0, 0, 0, 0, None );
  293. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  294. state = -1;
  295. running = false;
  296. }
  297. }
  298. // Our adorable little state manager will handle what state we're in.
  299. switch ( state ) {
  300. default: {
  301. break;
  302. }
  303. case 0: {
  304. // If xengine has found a window we're hovering over (or if it changed)
  305. // create a rectangle around it so the user knows he/she can click on it.
  306. // --but only if the user wants us to
  307. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  308. slop::WindowRectangle t;
  309. t.setGeometry( xengine->m_hoverWindow, decorations );
  310. t.applyPadding( padding );
  311. t.applyMinMaxSize( minimumsize, maximumsize );
  312. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  313. if ( !selection ) {
  314. selection = new slop::Rectangle( t.m_x,
  315. t.m_y,
  316. t.m_x + t.m_width,
  317. t.m_y + t.m_height,
  318. borderSize,
  319. highlight,
  320. r, g, b, a );
  321. } else {
  322. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  323. }
  324. //window = xengine->m_hoverWindow;
  325. // Since WindowRectangle can select different windows depending on click location...
  326. window = t.getWindow();
  327. }
  328. // If the user clicked we move on to the next state.
  329. if ( xengine->mouseDown( 1 ) ) {
  330. state++;
  331. }
  332. break;
  333. }
  334. case 1: {
  335. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  336. cx = xengine->m_mousex;
  337. cy = xengine->m_mousey;
  338. // Make sure we don't have un-seen applied offsets.
  339. xoffset = 0;
  340. yoffset = 0;
  341. // Also remember where the original selection was
  342. if ( selection ) {
  343. xmem = selection->m_x;
  344. ymem = selection->m_y;
  345. wmem = selection->m_width;
  346. hmem = selection->m_height;
  347. } else {
  348. xmem = cx;
  349. ymem = cy;
  350. }
  351. state++;
  352. break;
  353. }
  354. case 2: {
  355. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  356. if ( !selection ) {
  357. int sx, sy, ex, ey;
  358. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  359. selection = new slop::Rectangle( sx,
  360. sy,
  361. ex,
  362. ey,
  363. borderSize,
  364. highlight,
  365. r, g, b, a );
  366. }
  367. windowmemory = window;
  368. // If the user has let go of the mouse button, we'll just
  369. // continue to the next state.
  370. if ( !xengine->mouseDown( 1 ) ) {
  371. state++;
  372. break;
  373. }
  374. // Check to make sure the user actually wants to drag for a selection before moving things around.
  375. int w = xengine->m_mousex - cx;
  376. int h = xengine->m_mousey - cy;
  377. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  378. // We make sure the selection rectangle stays on the window we had selected
  379. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  380. xengine->setCursor( slop::Left );
  381. // Make sure
  382. window = windowmemory;
  383. continue;
  384. }
  385. // If we're not selecting a window.
  386. windowmemory = window;
  387. window = None;
  388. // We also detect which way the user is pulling and set the mouse icon accordingly.
  389. bool x = cx > xengine->m_mousex;
  390. bool y = cy > xengine->m_mousey;
  391. if ( ( selection->m_width <= 1 && selection->m_height <= 1 ) || ( minimumsize == maximumsize && minimumsize != 0 && maximumsize != 0 ) ) {
  392. xengine->setCursor( slop::Cross );
  393. } else if ( !x && !y ) {
  394. xengine->setCursor( slop::LowerRightCorner );
  395. } else if ( x && !y ) {
  396. xengine->setCursor( slop::LowerLeftCorner );
  397. } else if ( !x && y ) {
  398. xengine->setCursor( slop::UpperRightCorner );
  399. } else if ( x && y ) {
  400. xengine->setCursor( slop::UpperLeftCorner );
  401. }
  402. // Apply padding and minimum size adjustments.
  403. int sx, sy, ex, ey;
  404. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  405. // Set the selection rectangle's dimensions to mouse movement.
  406. selection->setGeo( sx + xoffset, sy + yoffset, ex, ey );
  407. break;
  408. }
  409. case 3: {
  410. int x, y, w, h;
  411. // Exit the utility after this state runs once.
  412. running = false;
  413. // We pull the dimensions and positions from the selection rectangle.
  414. // The selection rectangle automatically converts the positions and
  415. // dimensions to absolute coordinates when we set them earilier.
  416. x = selection->m_x;
  417. y = selection->m_y;
  418. w = selection->m_width;
  419. h = selection->m_height;
  420. // Delete the rectangle, which will remove it from the screen.
  421. delete selection;
  422. // Print the selection :)
  423. printSelection( format, false, x, y, w, h, window );
  424. break;
  425. }
  426. }
  427. // 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.
  428. // 0.01 seconds
  429. usleep( 10000 );
  430. }
  431. xengine->releaseCursor();
  432. xengine->releaseKeyboard();
  433. // Try to process any last-second requests.
  434. //xengine->tick();
  435. // Clean up global classes.
  436. delete xengine;
  437. // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
  438. usleep( 50000 );
  439. // If we canceled the selection, return error.
  440. if ( state == -1 ) {
  441. return EXIT_FAILURE;
  442. }
  443. return EXIT_SUCCESS;
  444. }
  445. int main( int argc, char** argv ) {
  446. try {
  447. return app( argc, argv );
  448. } catch( std::bad_alloc const& exception ) {
  449. fprintf( stderr, "Couldn't allocate enough memory! No space left in RAM." );
  450. return EXIT_FAILURE;
  451. } catch( std::exception const& exception ) {
  452. fprintf( stderr, "Unhandled Exception Thrown: %s\n", exception.what() );
  453. return EXIT_FAILURE;
  454. } catch( ... ) {
  455. fprintf( stderr, "Unknown Exception Thrown!\n" );
  456. return EXIT_FAILURE;
  457. }
  458. }