main.cpp 19KB

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