main.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <sstream>
  23. #include "x.hpp"
  24. #include "rectangle.hpp"
  25. #include "cmdline.h"
  26. int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
  27. size_t pos = 0;
  28. while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
  29. if ( pos + 1 > format.size() ) {
  30. fprintf( stderr, "Format error: %% found at the end of format string.\n", format[ pos + 1 ] );
  31. return 1;
  32. }
  33. std::stringstream foo;
  34. switch( format[ pos + 1 ] ) {
  35. case '%':
  36. format.replace( pos, 2, "%" );
  37. pos += 1;
  38. break;
  39. case 'x':
  40. case 'X':
  41. foo << x;
  42. format.replace( pos, 2, foo.str() );
  43. break;
  44. case 'y':
  45. case 'Y':
  46. foo << y;
  47. format.replace( pos, 2, foo.str() );
  48. break;
  49. case 'w':
  50. case 'W':
  51. foo << w;
  52. format.replace( pos, 2, foo.str() );
  53. break;
  54. case 'h':
  55. case 'H':
  56. foo << h;
  57. format.replace( pos, 2, foo.str() );
  58. break;
  59. case 'g':
  60. case 'G':
  61. foo << w << 'x' << h << '+' << x << '+' << y;
  62. format.replace( pos, 2, foo.str() );
  63. break;
  64. case 'i':
  65. case 'I':
  66. foo << window;
  67. format.replace( pos, 2, foo.str() );
  68. break;
  69. case 'c':
  70. case 'C':
  71. format.replace( pos, 2, cancelled ? "true" : "false" );
  72. break;
  73. default:
  74. fprintf( stderr, "Format error: %%%c is an unknown replacement identifier.\n", format[ pos + 1 ] );
  75. fprintf( stderr, "Valid replacements: %%x, %%y, %%w, %%h, %%i, %%c, %%.\n" );
  76. return 1;
  77. break;
  78. }
  79. }
  80. pos = 0;
  81. while ( ( pos = format.find( "\\", pos ) ) != std::string::npos ) {
  82. if ( pos + 1 > format.size() ) {
  83. break;
  84. }
  85. if ( format[ pos + 1 ] == 'n' ) {
  86. format.replace( pos, 2, "\n" );
  87. }
  88. pos = pos + 1;
  89. }
  90. printf( "%s", format.c_str() );
  91. return 0;
  92. }
  93. int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
  94. std::string copy = arg;
  95. int find = copy.find( "," );
  96. while( find != copy.npos ) {
  97. copy.at( find ) = ' ';
  98. find = copy.find( "," );
  99. }
  100. // Just in case we didn't include an alpha value
  101. *a = 1;
  102. int num = sscanf( copy.c_str(), "%f %f %f %f", r, g, b, a );
  103. if ( num != 3 && num != 4 ) {
  104. fprintf( stderr, "Error parsing color %s\n", arg.c_str() );
  105. return 1;
  106. }
  107. return 0;
  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. int main( int argc, char** argv ) {
  161. gengetopt_args_info options;
  162. int err = cmdline_parser( argc, argv, &options );
  163. if ( err ) {
  164. return err;
  165. }
  166. int state = 0;
  167. bool running = true;
  168. slop::Rectangle* selection = NULL;
  169. Window window = None;
  170. Window windowmemory = None;
  171. std::string xdisplay = options.xdisplay_arg;
  172. int padding = options.padding_arg;
  173. int borderSize = options.bordersize_arg;
  174. int tolerance = options.tolerance_arg;
  175. float r, g, b, a;
  176. parseColor( options.color_arg, &r, &g, &b, &a );
  177. float gracetime = atof( options.gracetime_arg );
  178. bool highlight = options.highlight_flag;
  179. bool keyboard = !options.nokeyboard_flag;
  180. bool decorations = !options.nodecorations_flag;
  181. timespec start, time;
  182. int cx = 0;
  183. int cy = 0;
  184. int xmem = 0;
  185. int ymem = 0;
  186. int wmem = 0;
  187. int hmem = 0;
  188. int minimumsize = options.min_arg;
  189. int maximumsize = options.max_arg;
  190. std::string format = options.format_arg;
  191. cmdline_parser_free( &options );
  192. // First we set up the x interface and grab the mouse,
  193. // if we fail for either we exit immediately.
  194. err = xengine->init( xdisplay.c_str() );
  195. if ( err ) {
  196. printSelection( format, true, 0, 0, 0, 0, None );
  197. return err;
  198. }
  199. err = xengine->grabCursor( slop::Cross );
  200. if ( err ) {
  201. printSelection( format, true, 0, 0, 0, 0, None );
  202. return err;
  203. }
  204. if ( keyboard ) {
  205. err = xengine->grabKeyboard();
  206. if ( err ) {
  207. fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
  208. }
  209. }
  210. clock_gettime( CLOCK_REALTIME, &start );
  211. while ( running ) {
  212. clock_gettime( CLOCK_REALTIME, &time );
  213. // "ticking" the xengine makes it process all queued events.
  214. xengine->tick();
  215. // If the user presses any key on the keyboard, exit the application.
  216. // Make sure at least gracetime has passed before allowing canceling
  217. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  218. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  219. if ( timei - starti > gracetime ) {
  220. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  221. printSelection( format, true, 0, 0, 0, 0, None );
  222. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  223. state = -1;
  224. running = false;
  225. }
  226. }
  227. // Our adorable little state manager will handle what state we're in.
  228. switch ( state ) {
  229. default: {
  230. break;
  231. }
  232. case 0: {
  233. // If xengine has found a window we're hovering over (or if it changed)
  234. // create a rectangle around it so the user knows he/she can click on it.
  235. // --but only if the user wants us to
  236. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  237. slop::WindowRectangle t;
  238. t.setGeometry( xengine->m_hoverWindow, decorations );
  239. t.applyPadding( padding );
  240. t.applyMinMaxSize( minimumsize, maximumsize );
  241. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  242. if ( !selection ) {
  243. selection = new slop::Rectangle( t.m_x,
  244. t.m_y,
  245. t.m_x + t.m_width,
  246. t.m_y + t.m_height,
  247. borderSize,
  248. highlight,
  249. r, g, b, a );
  250. } else {
  251. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  252. }
  253. //window = xengine->m_hoverWindow;
  254. // Since WindowRectangle can select different windows depending on click location...
  255. window = t.getWindow();
  256. }
  257. // If the user clicked we move on to the next state.
  258. if ( xengine->mouseDown( 1 ) ) {
  259. state++;
  260. }
  261. break;
  262. }
  263. case 1: {
  264. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  265. cx = xengine->m_mousex;
  266. cy = xengine->m_mousey;
  267. // Also remember where the original selection was
  268. if ( selection ) {
  269. xmem = selection->m_x;
  270. ymem = selection->m_y;
  271. wmem = selection->m_width;
  272. hmem = selection->m_height;
  273. } else {
  274. xmem = cx;
  275. ymem = cy;
  276. }
  277. state++;
  278. break;
  279. }
  280. case 2: {
  281. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  282. if ( !selection ) {
  283. int sx, sy, ex, ey;
  284. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  285. selection = new slop::Rectangle( sx,
  286. sy,
  287. ex,
  288. ey,
  289. borderSize,
  290. highlight,
  291. r, g, b, a );
  292. }
  293. windowmemory = window;
  294. // If the user has let go of the mouse button, we'll just
  295. // continue to the next state.
  296. if ( !xengine->mouseDown( 1 ) ) {
  297. state++;
  298. break;
  299. }
  300. // Check to make sure the user actually wants to drag for a selection before moving things around.
  301. int w = xengine->m_mousex - cx;
  302. int h = xengine->m_mousey - cy;
  303. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  304. // We make sure the selection rectangle stays on the window we had selected
  305. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  306. xengine->setCursor( slop::Left );
  307. // Make sure
  308. window = windowmemory;
  309. continue;
  310. }
  311. // If we're not selecting a window.
  312. windowmemory = window;
  313. window = None;
  314. // We also detect which way the user is pulling and set the mouse icon accordingly.
  315. bool x = cx > xengine->m_mousex;
  316. bool y = cy > xengine->m_mousey;
  317. if ( selection->m_width <= 1 && selection->m_height <= 1 || ( minimumsize == maximumsize && minimumsize != 0 && maximumsize != 0 ) ) {
  318. xengine->setCursor( slop::Cross );
  319. } else if ( !x && !y ) {
  320. xengine->setCursor( slop::LowerRightCorner );
  321. } else if ( x && !y ) {
  322. xengine->setCursor( slop::LowerLeftCorner );
  323. } else if ( !x && y ) {
  324. xengine->setCursor( slop::UpperRightCorner );
  325. } else if ( x && y ) {
  326. xengine->setCursor( slop::UpperLeftCorner );
  327. }
  328. // Apply padding and minimum size adjustments.
  329. int sx, sy, ex, ey;
  330. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  331. // Set the selection rectangle's dimensions to mouse movement.
  332. selection->setGeo( sx, sy, ex, ey );
  333. break;
  334. }
  335. case 3: {
  336. int x, y, w, h;
  337. // Exit the utility after this state runs once.
  338. running = false;
  339. // We pull the dimensions and positions from the selection rectangle.
  340. // The selection rectangle automatically converts the positions and
  341. // dimensions to absolute coordinates when we set them earilier.
  342. x = selection->m_x;
  343. y = selection->m_y;
  344. w = selection->m_width;
  345. h = selection->m_height;
  346. // Delete the rectangle, which will remove it from the screen.
  347. delete selection;
  348. // Print the selection :)
  349. printSelection( format, false, x, y, w, h, window );
  350. break;
  351. }
  352. }
  353. // 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.
  354. // 0.01 seconds
  355. usleep( 10000 );
  356. }
  357. xengine->releaseCursor();
  358. xengine->releaseKeyboard();
  359. // Try to process any last-second requests.
  360. //xengine->tick();
  361. // Clean up global classes.
  362. delete xengine;
  363. // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
  364. usleep( 50000 );
  365. // If we canceled the selection, return error.
  366. if ( state == -1 ) {
  367. return 1;
  368. }
  369. return 0;
  370. }